]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_updgrp.c
tests: Check if Node Target Extended Communities work
[mirror_frr.git] / bgpd / bgp_updgrp.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
3f9c7369
DS
2/**
3 * bgp_updgrp.c: BGP update group structures
4 *
5 * @copyright Copyright (C) 2014 Cumulus Networks, Inc.
6 *
7 * @author Avneesh Sachdev <avneesh@sproute.net>
8 * @author Rajesh Varadarajan <rajesh@sproute.net>
9 * @author Pradosh Mohapatra <pradosh@sproute.net>
3f9c7369
DS
10 */
11
12#include <zebra.h>
13
14#include "prefix.h"
24a58196 15#include "frrevent.h"
3f9c7369
DS
16#include "buffer.h"
17#include "stream.h"
18#include "command.h"
19#include "sockunion.h"
20#include "network.h"
21#include "memory.h"
22#include "filter.h"
23#include "routemap.h"
3f9c7369
DS
24#include "log.h"
25#include "plist.h"
26#include "linklist.h"
27#include "workqueue.h"
28#include "hash.h"
29#include "jhash.h"
30#include "queue.h"
31
32#include "bgpd/bgpd.h"
33#include "bgpd/bgp_table.h"
34#include "bgpd/bgp_debug.h"
14454c9f 35#include "bgpd/bgp_errors.h"
3f9c7369 36#include "bgpd/bgp_fsm.h"
b1dd7180 37#include "bgpd/bgp_addpath.h"
3f9c7369
DS
38#include "bgpd/bgp_advertise.h"
39#include "bgpd/bgp_packet.h"
40#include "bgpd/bgp_updgrp.h"
41#include "bgpd/bgp_route.h"
42#include "bgpd/bgp_filter.h"
2fc102e1 43#include "bgpd/bgp_io.h"
3f9c7369
DS
44
45/********************
46 * PRIVATE FUNCTIONS
47 ********************/
48
49/**
50 * assign a unique ID to update group and subgroup. Mostly for display/
51 * debugging purposes. It's a 64-bit space - used leisurely without a
52 * worry about its wrapping and about filling gaps. While at it, timestamp
53 * the creation.
54 */
d62a17ae 55static void update_group_checkin(struct update_group *updgrp)
3f9c7369 56{
d62a17ae 57 updgrp->id = ++bm->updgrp_idspace;
083ec940 58 updgrp->uptime = monotime(NULL);
3f9c7369
DS
59}
60
d62a17ae 61static void update_subgroup_checkin(struct update_subgroup *subgrp,
62 struct update_group *updgrp)
3f9c7369 63{
d62a17ae 64 subgrp->id = ++bm->subgrp_idspace;
083ec940 65 subgrp->uptime = monotime(NULL);
3f9c7369
DS
66}
67
ef56aee4
DA
68static void sync_init(struct update_subgroup *subgrp,
69 struct update_group *updgrp)
3f9c7369 70{
ef56aee4
DA
71 struct peer *peer = UPDGRP_PEER(updgrp);
72
d62a17ae 73 subgrp->sync =
74 XCALLOC(MTYPE_BGP_SYNCHRONISE, sizeof(struct bgp_synchronize));
a274fef8
DL
75 bgp_adv_fifo_init(&subgrp->sync->update);
76 bgp_adv_fifo_init(&subgrp->sync->withdraw);
77 bgp_adv_fifo_init(&subgrp->sync->withdraw_low);
996c9314 78 subgrp->hash =
4d28080c
DA
79 hash_create(bgp_advertise_attr_hash_key,
80 bgp_advertise_attr_hash_cmp, "BGP SubGroup Hash");
d62a17ae 81
82 /* We use a larger buffer for subgrp->work in the event that:
83 * - We RX a BGP_UPDATE where the attributes alone are just
ef56aee4 84 * under 4096 or 65535 (if Extended Message capability negotiated).
d62a17ae 85 * - The user configures an outbound route-map that does many as-path
86 * prepends or adds many communities. At most they can have
87 * CMD_ARGC_MAX
88 * args in a route-map so there is a finite limit on how large they
89 * can
90 * make the attributes.
91 *
92 * Having a buffer with BGP_MAX_PACKET_SIZE_OVERFLOW allows us to avoid
93 * bounds
94 * checking for every single attribute as we construct an UPDATE.
95 */
ef56aee4
DA
96 subgrp->work = stream_new(peer->max_packet_size
97 + BGP_MAX_PACKET_SIZE_OVERFLOW);
98 subgrp->scratch = stream_new(peer->max_packet_size);
3f9c7369
DS
99}
100
d62a17ae 101static void sync_delete(struct update_subgroup *subgrp)
3f9c7369 102{
0a22ddfb 103 XFREE(MTYPE_BGP_SYNCHRONISE, subgrp->sync);
d8bc11a5
DS
104 hash_clean_and_free(&subgrp->hash,
105 (void (*)(void *))bgp_advertise_attr_free);
106
d62a17ae 107 if (subgrp->work)
108 stream_free(subgrp->work);
109 subgrp->work = NULL;
110 if (subgrp->scratch)
111 stream_free(subgrp->scratch);
112 subgrp->scratch = NULL;
3f9c7369
DS
113}
114
115/**
116 * conf_copy
117 *
118 * copy only those fields that are relevant to update group match
119 */
d62a17ae 120static void conf_copy(struct peer *dst, struct peer *src, afi_t afi,
121 safi_t safi)
3f9c7369 122{
d62a17ae 123 struct bgp_filter *srcfilter;
124 struct bgp_filter *dstfilter;
125
126 srcfilter = &src->filter[afi][safi];
127 dstfilter = &dst->filter[afi][safi];
128
129 dst->bgp = src->bgp;
130 dst->sort = src->sort;
131 dst->as = src->as;
132 dst->v_routeadv = src->v_routeadv;
133 dst->flags = src->flags;
134 dst->af_flags[afi][safi] = src->af_flags[afi][safi];
fde246e8 135 dst->pmax_out[afi][safi] = src->pmax_out[afi][safi];
ef56aee4 136 dst->max_packet_size = src->max_packet_size;
0a22ddfb 137 XFREE(MTYPE_BGP_PEER_HOST, dst->host);
d62a17ae 138
139 dst->host = XSTRDUP(MTYPE_BGP_PEER_HOST, src->host);
140 dst->cap = src->cap;
141 dst->af_cap[afi][safi] = src->af_cap[afi][safi];
142 dst->afc_nego[afi][safi] = src->afc_nego[afi][safi];
143 dst->orf_plist[afi][safi] = src->orf_plist[afi][safi];
dcc68b5e 144 dst->addpath_type[afi][safi] = src->addpath_type[afi][safi];
d62a17ae 145 dst->local_as = src->local_as;
146 dst->change_local_as = src->change_local_as;
147 dst->shared_network = src->shared_network;
d864dd9e 148 dst->local_role = src->local_role;
25851bf0 149 dst->as_path_loop_detection = src->as_path_loop_detection;
01da2d26
DA
150
151 if (src->soo[afi][safi]) {
152 ecommunity_free(&dst->soo[afi][safi]);
153 dst->soo[afi][safi] = ecommunity_dup(src->soo[afi][safi]);
154 }
155
d62a17ae 156 memcpy(&(dst->nexthop), &(src->nexthop), sizeof(struct bgp_nexthop));
157
158 dst->group = src->group;
159
160 if (src->default_rmap[afi][safi].name) {
161 dst->default_rmap[afi][safi].name =
162 XSTRDUP(MTYPE_ROUTE_MAP_NAME,
163 src->default_rmap[afi][safi].name);
164 dst->default_rmap[afi][safi].map =
165 src->default_rmap[afi][safi].map;
166 }
167
168 if (DISTRIBUTE_OUT_NAME(srcfilter)) {
169 DISTRIBUTE_OUT_NAME(dstfilter) = XSTRDUP(
170 MTYPE_BGP_FILTER_NAME, DISTRIBUTE_OUT_NAME(srcfilter));
171 DISTRIBUTE_OUT(dstfilter) = DISTRIBUTE_OUT(srcfilter);
172 }
173
174 if (PREFIX_LIST_OUT_NAME(srcfilter)) {
175 PREFIX_LIST_OUT_NAME(dstfilter) = XSTRDUP(
176 MTYPE_BGP_FILTER_NAME, PREFIX_LIST_OUT_NAME(srcfilter));
177 PREFIX_LIST_OUT(dstfilter) = PREFIX_LIST_OUT(srcfilter);
178 }
179
180 if (FILTER_LIST_OUT_NAME(srcfilter)) {
181 FILTER_LIST_OUT_NAME(dstfilter) = XSTRDUP(
182 MTYPE_BGP_FILTER_NAME, FILTER_LIST_OUT_NAME(srcfilter));
183 FILTER_LIST_OUT(dstfilter) = FILTER_LIST_OUT(srcfilter);
184 }
185
186 if (ROUTE_MAP_OUT_NAME(srcfilter)) {
187 ROUTE_MAP_OUT_NAME(dstfilter) = XSTRDUP(
188 MTYPE_BGP_FILTER_NAME, ROUTE_MAP_OUT_NAME(srcfilter));
189 ROUTE_MAP_OUT(dstfilter) = ROUTE_MAP_OUT(srcfilter);
190 }
191
192 if (UNSUPPRESS_MAP_NAME(srcfilter)) {
193 UNSUPPRESS_MAP_NAME(dstfilter) = XSTRDUP(
194 MTYPE_BGP_FILTER_NAME, UNSUPPRESS_MAP_NAME(srcfilter));
195 UNSUPPRESS_MAP(dstfilter) = UNSUPPRESS_MAP(srcfilter);
196 }
7f7940e6
MK
197
198 if (ADVERTISE_MAP_NAME(srcfilter)) {
199 ADVERTISE_MAP_NAME(dstfilter) = XSTRDUP(
200 MTYPE_BGP_FILTER_NAME, ADVERTISE_MAP_NAME(srcfilter));
201 ADVERTISE_MAP(dstfilter) = ADVERTISE_MAP(srcfilter);
202 ADVERTISE_CONDITION(dstfilter) = ADVERTISE_CONDITION(srcfilter);
203 }
204
205 if (CONDITION_MAP_NAME(srcfilter)) {
206 CONDITION_MAP_NAME(dstfilter) = XSTRDUP(
207 MTYPE_BGP_FILTER_NAME, CONDITION_MAP_NAME(srcfilter));
208 CONDITION_MAP(dstfilter) = CONDITION_MAP(srcfilter);
209 }
f373ce6c
QY
210
211 dstfilter->advmap.update_type = srcfilter->advmap.update_type;
3f9c7369
DS
212}
213
214/**
6e919709 215 * since we did a bunch of XSTRDUP's in conf_copy, time to free them up
3f9c7369 216 */
d62a17ae 217static void conf_release(struct peer *src, afi_t afi, safi_t safi)
3f9c7369 218{
d62a17ae 219 struct bgp_filter *srcfilter;
3f9c7369 220
d62a17ae 221 srcfilter = &src->filter[afi][safi];
3f9c7369 222
0a22ddfb 223 XFREE(MTYPE_ROUTE_MAP_NAME, src->default_rmap[afi][safi].name);
3f9c7369 224
0a22ddfb 225 XFREE(MTYPE_BGP_FILTER_NAME, srcfilter->dlist[FILTER_OUT].name);
3f9c7369 226
0a22ddfb 227 XFREE(MTYPE_BGP_FILTER_NAME, srcfilter->plist[FILTER_OUT].name);
3f9c7369 228
0a22ddfb 229 XFREE(MTYPE_BGP_FILTER_NAME, srcfilter->aslist[FILTER_OUT].name);
3f9c7369 230
0a22ddfb 231 XFREE(MTYPE_BGP_FILTER_NAME, srcfilter->map[RMAP_OUT].name);
3f9c7369 232
0a22ddfb 233 XFREE(MTYPE_BGP_FILTER_NAME, srcfilter->usmap.name);
495f0b13 234
7f7940e6
MK
235 XFREE(MTYPE_BGP_FILTER_NAME, srcfilter->advmap.aname);
236
237 XFREE(MTYPE_BGP_FILTER_NAME, srcfilter->advmap.cname);
238
0a22ddfb 239 XFREE(MTYPE_BGP_PEER_HOST, src->host);
61adcf71
DA
240
241 ecommunity_free(&src->soo[afi][safi]);
3f9c7369
DS
242}
243
d62a17ae 244static void peer2_updgrp_copy(struct update_group *updgrp, struct peer_af *paf)
3f9c7369 245{
d62a17ae 246 struct peer *src;
247 struct peer *dst;
3f9c7369 248
d62a17ae 249 if (!updgrp || !paf)
250 return;
3f9c7369 251
d62a17ae 252 src = paf->peer;
253 dst = updgrp->conf;
254 if (!src || !dst)
255 return;
3f9c7369 256
d62a17ae 257 updgrp->afi = paf->afi;
258 updgrp->safi = paf->safi;
259 updgrp->afid = paf->afid;
260 updgrp->bgp = src->bgp;
3f9c7369 261
d62a17ae 262 conf_copy(dst, src, paf->afi, paf->safi);
3f9c7369
DS
263}
264
265/**
266 * auxiliary functions to maintain the hash table.
267 * - updgrp_hash_alloc - to create a new entry, passed to hash_get
268 * - updgrp_hash_key_make - makes the key for update group search
269 * - updgrp_hash_cmp - compare two update groups.
270 */
d62a17ae 271static void *updgrp_hash_alloc(void *p)
3f9c7369 272{
d62a17ae 273 struct update_group *updgrp;
274 const struct update_group *in;
275
276 in = (const struct update_group *)p;
277 updgrp = XCALLOC(MTYPE_BGP_UPDGRP, sizeof(struct update_group));
278 memcpy(updgrp, in, sizeof(struct update_group));
279 updgrp->conf = XCALLOC(MTYPE_BGP_PEER, sizeof(struct peer));
280 conf_copy(updgrp->conf, in->conf, in->afi, in->safi);
281 return updgrp;
3f9c7369
DS
282}
283
284/**
285 * The hash value for a peer is computed from the following variables:
286 * v = f(
287 * 1. IBGP (1) or EBGP (2)
288 * 2. FLAGS based on configuration:
289 * LOCAL_AS_NO_PREPEND
290 * LOCAL_AS_REPLACE_AS
291 * 3. AF_FLAGS based on configuration:
292 * Refer to definition in bgp_updgrp.h
293 * 4. (AF-independent) Capability flags:
294 * AS4_RCV capability
295 * 5. (AF-dependent) Capability flags:
296 * ORF_PREFIX_SM_RCV (peer can send prefix ORF)
297 * 6. MRAI
298 * 7. peer-group name
299 * 8. Outbound route-map name (neighbor route-map <> out)
300 * 9. Outbound distribute-list name (neighbor distribute-list <> out)
301 * 10. Outbound prefix-list name (neighbor prefix-list <> out)
302 * 11. Outbound as-list name (neighbor filter-list <> out)
303 * 12. Unsuppress map name (neighbor unsuppress-map <>)
304 * 13. default rmap name (neighbor default-originate route-map <>)
305 * 14. encoding both global and link-local nexthop?
306 * 15. If peer is configured to be a lonesoul, peer ip address
307 * 16. Local-as should match, if configured.
e0267260 308 * 17. maximum-prefix-out
d864dd9e 309 * 18. Local-role should also match, if configured.
3f9c7369
DS
310 * )
311 */
d8b87afe 312static unsigned int updgrp_hash_key_make(const void *p)
3f9c7369 313{
d62a17ae 314 const struct update_group *updgrp;
315 const struct peer *peer;
316 const struct bgp_filter *filter;
d782e3ff 317 uint64_t flags;
d62a17ae 318 uint32_t key;
319 afi_t afi;
320 safi_t safi;
3f9c7369 321
b0d2fa38
DS
322 /*
323 * IF YOU ADD AN ADDITION TO THE HASH KEY TO ENSURE
324 * THAT THE UPDATE GROUP CALCULATION IS CORRECT THEN
325 * PLEASE ADD IT TO THE DEBUG OUTPUT TOO AT THE BOTTOM
326 */
3f9c7369
DS
327#define SEED1 999331
328#define SEED2 2147483647
329
d62a17ae 330 updgrp = p;
331 peer = updgrp->conf;
332 afi = updgrp->afi;
333 safi = updgrp->safi;
334 flags = peer->af_flags[afi][safi];
335 filter = &peer->filter[afi][safi];
336
337 key = 0;
338
339 key = jhash_1word(peer->sort, key); /* EBGP or IBGP */
340 key = jhash_1word((peer->flags & PEER_UPDGRP_FLAGS), key);
341 key = jhash_1word((flags & PEER_UPDGRP_AF_FLAGS), key);
dcc68b5e 342 key = jhash_1word((uint32_t)peer->addpath_type[afi][safi], key);
d62a17ae 343 key = jhash_1word((peer->cap & PEER_UPDGRP_CAP_FLAGS), key);
344 key = jhash_1word((peer->af_cap[afi][safi] & PEER_UPDGRP_AF_CAP_FLAGS),
345 key);
346 key = jhash_1word(peer->v_routeadv, key);
347 key = jhash_1word(peer->change_local_as, key);
cecd7358 348 key = jhash_1word(peer->max_packet_size, key);
e0267260 349 key = jhash_1word(peer->pmax_out[afi][safi], key);
d62a17ae 350
25851bf0
DA
351 if (peer->as_path_loop_detection)
352 key = jhash_2words(peer->as, peer->as_path_loop_detection, key);
353
d62a17ae 354 if (peer->group)
355 key = jhash_1word(jhash(peer->group->name,
356 strlen(peer->group->name), SEED1),
357 key);
358
359 if (filter->map[RMAP_OUT].name)
360 key = jhash_1word(jhash(filter->map[RMAP_OUT].name,
361 strlen(filter->map[RMAP_OUT].name),
362 SEED1),
363 key);
364
365 if (filter->dlist[FILTER_OUT].name)
366 key = jhash_1word(jhash(filter->dlist[FILTER_OUT].name,
367 strlen(filter->dlist[FILTER_OUT].name),
368 SEED1),
369 key);
370
371 if (filter->plist[FILTER_OUT].name)
372 key = jhash_1word(jhash(filter->plist[FILTER_OUT].name,
373 strlen(filter->plist[FILTER_OUT].name),
374 SEED1),
375 key);
376
377 if (filter->aslist[FILTER_OUT].name)
378 key = jhash_1word(jhash(filter->aslist[FILTER_OUT].name,
379 strlen(filter->aslist[FILTER_OUT].name),
380 SEED1),
381 key);
382
383 if (filter->usmap.name)
384 key = jhash_1word(jhash(filter->usmap.name,
385 strlen(filter->usmap.name), SEED1),
386 key);
387
7f7940e6
MK
388 if (filter->advmap.aname)
389 key = jhash_1word(jhash(filter->advmap.aname,
390 strlen(filter->advmap.aname), SEED1),
391 key);
392
f373ce6c
QY
393 if (filter->advmap.update_type)
394 key = jhash_1word(filter->advmap.update_type, key);
395
d62a17ae 396 if (peer->default_rmap[afi][safi].name)
397 key = jhash_1word(
398 jhash(peer->default_rmap[afi][safi].name,
399 strlen(peer->default_rmap[afi][safi].name),
400 SEED1),
401 key);
402
403 /* If peer is on a shared network and is exchanging IPv6 prefixes,
404 * it needs to include link-local address. That's different from
405 * non-shared-network peers (nexthop encoded with 32 bytes vs 16
406 * bytes). We create different update groups to take care of that.
407 */
408 key = jhash_1word(
409 (peer->shared_network && peer_afi_active_nego(peer, AFI_IP6)),
410 key);
d62a17ae 411 /*
412 * There are certain peers that must get their own update-group:
413 * - lonesoul peers
414 * - peers that negotiated ORF
a849a3fe 415 * - maximum-prefix-out is set
d62a17ae 416 */
417 if (CHECK_FLAG(peer->flags, PEER_FLAG_LONESOUL)
418 || CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
419 || CHECK_FLAG(peer->af_cap[afi][safi],
a849a3fe
DA
420 PEER_CAP_ORF_PREFIX_SM_OLD_RCV)
421 || CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_OUT))
d62a17ae 422 key = jhash_1word(jhash(peer->host, strlen(peer->host), SEED2),
423 key);
d864dd9e
EB
424 /*
425 * Multiple sessions with the same neighbor should get their own
426 * update-group if they have different roles.
427 */
428 key = jhash_1word(peer->local_role, key);
d62a17ae 429
97a52c82
DA
430 /* Neighbors configured with the AIGP attribute are put in a separate
431 * update group from other neighbors.
432 */
433 key = jhash_1word((peer->flags & PEER_FLAG_AIGP), key);
434
01da2d26
DA
435 if (peer->soo[afi][safi]) {
436 char *soo_str = ecommunity_str(peer->soo[afi][safi]);
437
438 key = jhash_1word(jhash(soo_str, strlen(soo_str), SEED1), key);
439 }
440
b0d2fa38
DS
441 /*
442 * ANY NEW ITEMS THAT ARE ADDED TO THE key, ENSURE DEBUG
443 * STATEMENT STAYS UP TO DATE
444 */
34d8aff1
DS
445 if (bgp_debug_neighbor_events(peer)) {
446 zlog_debug(
da5e1a58 447 "%pBP Update Group Hash: sort: %d UpdGrpFlags: %ju UpdGrpAFFlags: %ju",
0ebabd41 448 peer, peer->sort,
da5e1a58
DA
449 (intmax_t)CHECK_FLAG(peer->flags, PEER_UPDGRP_FLAGS),
450 (intmax_t)CHECK_FLAG(flags, PEER_UPDGRP_AF_FLAGS));
34d8aff1 451 zlog_debug(
25851bf0 452 "%pBP Update Group Hash: addpath: %u UpdGrpCapFlag: %u UpdGrpCapAFFlag: %u route_adv: %u change local as: %u, as_path_loop_detection: %d",
34d8aff1 453 peer, (uint32_t)peer->addpath_type[afi][safi],
da5e1a58
DA
454 CHECK_FLAG(peer->cap, PEER_UPDGRP_CAP_FLAGS),
455 CHECK_FLAG(peer->af_cap[afi][safi],
456 PEER_UPDGRP_AF_CAP_FLAGS),
25851bf0
DA
457 peer->v_routeadv, peer->change_local_as,
458 peer->as_path_loop_detection);
34d8aff1
DS
459 zlog_debug(
460 "%pBP Update Group Hash: max packet size: %u pmax_out: %u Peer Group: %s rmap out: %s",
461 peer, peer->max_packet_size, peer->pmax_out[afi][safi],
462 peer->group ? peer->group->name : "(NONE)",
463 ROUTE_MAP_OUT_NAME(filter) ? ROUTE_MAP_OUT_NAME(filter)
464 : "(NONE)");
465 zlog_debug(
b0d2fa38 466 "%pBP Update Group Hash: dlist out: %s plist out: %s aslist out: %s usmap out: %s advmap: %s %d",
34d8aff1
DS
467 peer,
468 DISTRIBUTE_OUT_NAME(filter)
469 ? DISTRIBUTE_OUT_NAME(filter)
470 : "(NONE)",
471 PREFIX_LIST_OUT_NAME(filter)
472 ? PREFIX_LIST_OUT_NAME(filter)
473 : "(NONE)",
474 FILTER_LIST_OUT_NAME(filter)
475 ? FILTER_LIST_OUT_NAME(filter)
476 : "(NONE)",
477 UNSUPPRESS_MAP_NAME(filter)
478 ? UNSUPPRESS_MAP_NAME(filter)
479 : "(NONE)",
480 ADVERTISE_MAP_NAME(filter) ? ADVERTISE_MAP_NAME(filter)
b0d2fa38
DS
481 : "(NONE)",
482 filter->advmap.update_type);
34d8aff1
DS
483 zlog_debug(
484 "%pBP Update Group Hash: default rmap: %s shared network and afi active network: %d",
485 peer,
486 peer->default_rmap[afi][safi].name
487 ? peer->default_rmap[afi][safi].name
488 : "(NONE)",
489 peer->shared_network &&
490 peer_afi_active_nego(peer, AFI_IP6));
491 zlog_debug(
da5e1a58 492 "%pBP Update Group Hash: Lonesoul: %d ORF prefix: %u ORF old: %u max prefix out: %ju",
37e70073 493 peer, !!CHECK_FLAG(peer->flags, PEER_FLAG_LONESOUL),
34d8aff1
DS
494 CHECK_FLAG(peer->af_cap[afi][safi],
495 PEER_CAP_ORF_PREFIX_SM_RCV),
496 CHECK_FLAG(peer->af_cap[afi][safi],
497 PEER_CAP_ORF_PREFIX_SM_OLD_RCV),
da5e1a58
DA
498 (intmax_t)CHECK_FLAG(peer->af_flags[afi][safi],
499 PEER_FLAG_MAX_PREFIX_OUT));
b0d2fa38
DS
500 zlog_debug(
501 "%pBP Update Group Hash: local role: %u AIGP: %d SOO: %s",
502 peer, peer->local_role,
503 !!CHECK_FLAG(peer->flags, PEER_FLAG_AIGP),
504 peer->soo[afi][safi]
505 ? ecommunity_str(peer->soo[afi][safi])
506 : "(NONE)");
34d8aff1
DS
507 zlog_debug("%pBP Update Group Hash key: %u", peer, key);
508 }
d62a17ae 509 return key;
3f9c7369
DS
510}
511
74df8d6d 512static bool updgrp_hash_cmp(const void *p1, const void *p2)
3f9c7369 513{
d62a17ae 514 const struct update_group *grp1;
515 const struct update_group *grp2;
516 const struct peer *pe1;
517 const struct peer *pe2;
d782e3ff
DA
518 uint64_t flags1;
519 uint64_t flags2;
d62a17ae 520 const struct bgp_filter *fl1;
521 const struct bgp_filter *fl2;
522 afi_t afi;
523 safi_t safi;
524
525 if (!p1 || !p2)
74df8d6d 526 return false;
d62a17ae 527
528 grp1 = p1;
529 grp2 = p2;
530 pe1 = grp1->conf;
531 pe2 = grp2->conf;
532 afi = grp1->afi;
533 safi = grp1->safi;
534 flags1 = pe1->af_flags[afi][safi];
535 flags2 = pe2->af_flags[afi][safi];
536 fl1 = &pe1->filter[afi][safi];
537 fl2 = &pe2->filter[afi][safi];
538
539 /* put EBGP and IBGP peers in different update groups */
540 if (pe1->sort != pe2->sort)
74df8d6d 541 return false;
d62a17ae 542
543 /* check peer flags */
544 if ((pe1->flags & PEER_UPDGRP_FLAGS)
545 != (pe2->flags & PEER_UPDGRP_FLAGS))
74df8d6d 546 return false;
d62a17ae 547
548 /* If there is 'local-as' configured, it should match. */
549 if (pe1->change_local_as != pe2->change_local_as)
74df8d6d 550 return false;
d62a17ae 551
e0267260
LS
552 if (pe1->pmax_out[afi][safi] != pe2->pmax_out[afi][safi])
553 return false;
554
d62a17ae 555 /* flags like route reflector client */
556 if ((flags1 & PEER_UPDGRP_AF_FLAGS) != (flags2 & PEER_UPDGRP_AF_FLAGS))
74df8d6d 557 return false;
d62a17ae 558
dcc68b5e 559 if (pe1->addpath_type[afi][safi] != pe2->addpath_type[afi][safi])
b08047f8 560 return false;
dcc68b5e 561
d62a17ae 562 if ((pe1->cap & PEER_UPDGRP_CAP_FLAGS)
563 != (pe2->cap & PEER_UPDGRP_CAP_FLAGS))
74df8d6d 564 return false;
d62a17ae 565
566 if ((pe1->af_cap[afi][safi] & PEER_UPDGRP_AF_CAP_FLAGS)
567 != (pe2->af_cap[afi][safi] & PEER_UPDGRP_AF_CAP_FLAGS))
74df8d6d 568 return false;
d62a17ae 569
570 if (pe1->v_routeadv != pe2->v_routeadv)
74df8d6d 571 return false;
d62a17ae 572
573 if (pe1->group != pe2->group)
74df8d6d 574 return false;
d62a17ae 575
d864dd9e
EB
576 /* Roles can affect filtering */
577 if (pe1->local_role != pe2->local_role)
578 return false;
579
d62a17ae 580 /* route-map names should be the same */
581 if ((fl1->map[RMAP_OUT].name && !fl2->map[RMAP_OUT].name)
582 || (!fl1->map[RMAP_OUT].name && fl2->map[RMAP_OUT].name)
583 || (fl1->map[RMAP_OUT].name && fl2->map[RMAP_OUT].name
584 && strcmp(fl1->map[RMAP_OUT].name, fl2->map[RMAP_OUT].name)))
74df8d6d 585 return false;
d62a17ae 586
587 if ((fl1->dlist[FILTER_OUT].name && !fl2->dlist[FILTER_OUT].name)
588 || (!fl1->dlist[FILTER_OUT].name && fl2->dlist[FILTER_OUT].name)
589 || (fl1->dlist[FILTER_OUT].name && fl2->dlist[FILTER_OUT].name
590 && strcmp(fl1->dlist[FILTER_OUT].name,
591 fl2->dlist[FILTER_OUT].name)))
74df8d6d 592 return false;
d62a17ae 593
594 if ((fl1->plist[FILTER_OUT].name && !fl2->plist[FILTER_OUT].name)
595 || (!fl1->plist[FILTER_OUT].name && fl2->plist[FILTER_OUT].name)
596 || (fl1->plist[FILTER_OUT].name && fl2->plist[FILTER_OUT].name
597 && strcmp(fl1->plist[FILTER_OUT].name,
598 fl2->plist[FILTER_OUT].name)))
74df8d6d 599 return false;
d62a17ae 600
601 if ((fl1->aslist[FILTER_OUT].name && !fl2->aslist[FILTER_OUT].name)
602 || (!fl1->aslist[FILTER_OUT].name && fl2->aslist[FILTER_OUT].name)
603 || (fl1->aslist[FILTER_OUT].name && fl2->aslist[FILTER_OUT].name
604 && strcmp(fl1->aslist[FILTER_OUT].name,
605 fl2->aslist[FILTER_OUT].name)))
74df8d6d 606 return false;
d62a17ae 607
608 if ((fl1->usmap.name && !fl2->usmap.name)
609 || (!fl1->usmap.name && fl2->usmap.name)
610 || (fl1->usmap.name && fl2->usmap.name
611 && strcmp(fl1->usmap.name, fl2->usmap.name)))
74df8d6d 612 return false;
d62a17ae 613
7f7940e6
MK
614 if ((fl1->advmap.aname && !fl2->advmap.aname)
615 || (!fl1->advmap.aname && fl2->advmap.aname)
616 || (fl1->advmap.aname && fl2->advmap.aname
617 && strcmp(fl1->advmap.aname, fl2->advmap.aname)))
618 return false;
619
f373ce6c
QY
620 if (fl1->advmap.update_type != fl2->advmap.update_type)
621 return false;
622
d62a17ae 623 if ((pe1->default_rmap[afi][safi].name
624 && !pe2->default_rmap[afi][safi].name)
625 || (!pe1->default_rmap[afi][safi].name
626 && pe2->default_rmap[afi][safi].name)
627 || (pe1->default_rmap[afi][safi].name
628 && pe2->default_rmap[afi][safi].name
629 && strcmp(pe1->default_rmap[afi][safi].name,
630 pe2->default_rmap[afi][safi].name)))
74df8d6d 631 return false;
d62a17ae 632
633 if ((afi == AFI_IP6) && (pe1->shared_network != pe2->shared_network))
74df8d6d 634 return false;
d62a17ae 635
636 if ((CHECK_FLAG(pe1->flags, PEER_FLAG_LONESOUL)
637 || CHECK_FLAG(pe1->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
638 || CHECK_FLAG(pe1->af_cap[afi][safi],
639 PEER_CAP_ORF_PREFIX_SM_OLD_RCV))
640 && !sockunion_same(&pe1->su, &pe2->su))
74df8d6d 641 return false;
d62a17ae 642
74df8d6d 643 return true;
3f9c7369
DS
644}
645
d62a17ae 646static void peer_lonesoul_or_not(struct peer *peer, int set)
3f9c7369 647{
d62a17ae 648 /* no change in status? */
649 if (set == (CHECK_FLAG(peer->flags, PEER_FLAG_LONESOUL) > 0))
650 return;
3f9c7369 651
d62a17ae 652 if (set)
653 SET_FLAG(peer->flags, PEER_FLAG_LONESOUL);
654 else
655 UNSET_FLAG(peer->flags, PEER_FLAG_LONESOUL);
3f9c7369 656
d62a17ae 657 update_group_adjust_peer_afs(peer);
3f9c7369
DS
658}
659
660/*
661 * subgroup_total_packets_enqueued
662 *
663 * Returns the total number of packets enqueued to a subgroup.
664 */
665static unsigned int
d62a17ae 666subgroup_total_packets_enqueued(struct update_subgroup *subgrp)
3f9c7369 667{
d62a17ae 668 struct bpacket *pkt;
3f9c7369 669
d62a17ae 670 pkt = bpacket_queue_last(SUBGRP_PKTQ(subgrp));
3f9c7369 671
d62a17ae 672 return pkt->ver - 1;
3f9c7369
DS
673}
674
d62a17ae 675static int update_group_show_walkcb(struct update_group *updgrp, void *arg)
3f9c7369 676{
d62a17ae 677 struct updwalk_context *ctx = arg;
678 struct vty *vty;
679 struct update_subgroup *subgrp;
680 struct peer_af *paf;
681 struct bgp_filter *filter;
0997ee26 682 struct peer *peer = UPDGRP_PEER(updgrp);
d62a17ae 683 int match = 0;
fa5a9276
AR
684 json_object *json_updgrp = NULL;
685 json_object *json_subgrps = NULL;
686 json_object *json_subgrp = NULL;
687 json_object *json_time = NULL;
688 json_object *json_subgrp_time = NULL;
689 json_object *json_subgrp_event = NULL;
690 json_object *json_peers = NULL;
691 json_object *json_pkt_info = NULL;
692 time_t epoch_tbuf, tbuf;
d62a17ae 693
694 if (!ctx)
695 return CMD_SUCCESS;
696
697 if (ctx->subgrp_id) {
a2addae8 698 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
d62a17ae 699 if (ctx->subgrp_id && (ctx->subgrp_id != subgrp->id))
700 continue;
701 else {
702 match = 1;
703 break;
704 }
705 }
706 } else {
707 match = 1;
708 }
709
710 if (!match) {
711 /* Since this routine is invoked from a walk, we cannot signal
712 * any */
713 /* error here, can only return. */
714 return CMD_SUCCESS;
715 }
716
717 vty = ctx->vty;
718
fa5a9276
AR
719 if (ctx->uj) {
720 json_updgrp = json_object_new_object();
721 /* Display json o/p */
722 tbuf = monotime(NULL);
723 tbuf -= updgrp->uptime;
724 epoch_tbuf = time(NULL) - tbuf;
725 json_time = json_object_new_object();
726 json_object_int_add(json_time, "epoch", epoch_tbuf);
727 json_object_string_add(json_time, "epochString",
728 ctime(&epoch_tbuf));
729 json_object_object_add(json_updgrp, "groupCreateTime",
730 json_time);
731 json_object_string_add(json_updgrp, "afi",
732 afi2str(updgrp->afi));
733 json_object_string_add(json_updgrp, "safi",
734 safi2str(updgrp->safi));
735 } else {
736 vty_out(vty, "Update-group %" PRIu64 ":\n", updgrp->id);
737 vty_out(vty, " Created: %s", timestamp_string(updgrp->uptime));
738 }
739
d62a17ae 740 filter = &updgrp->conf->filter[updgrp->afi][updgrp->safi];
fa5a9276
AR
741 if (filter->map[RMAP_OUT].name) {
742 if (ctx->uj)
743 json_object_string_add(json_updgrp, "outRouteMap",
744 filter->map[RMAP_OUT].name);
745 else
746 vty_out(vty, " Outgoing route map: %s\n",
747 filter->map[RMAP_OUT].name);
748 }
d62a17ae 749
fa5a9276
AR
750 if (ctx->uj)
751 json_object_int_add(json_updgrp, "minRouteAdvInt",
752 updgrp->conf->v_routeadv);
753 else
754 vty_out(vty, " MRAI value (seconds): %d\n",
755 updgrp->conf->v_routeadv);
756
757 if (updgrp->conf->change_local_as) {
758 if (ctx->uj) {
759 json_object_int_add(json_updgrp, "localAs",
760 updgrp->conf->change_local_as);
761 json_object_boolean_add(
762 json_updgrp, "noPrepend",
763 CHECK_FLAG(updgrp->conf->flags,
764 PEER_FLAG_LOCAL_AS_NO_PREPEND));
765 json_object_boolean_add(
766 json_updgrp, "replaceLocalAs",
767 CHECK_FLAG(updgrp->conf->flags,
768 PEER_FLAG_LOCAL_AS_REPLACE_AS));
769 } else {
770 vty_out(vty, " Local AS %u%s%s\n",
771 updgrp->conf->change_local_as,
772 CHECK_FLAG(updgrp->conf->flags,
773 PEER_FLAG_LOCAL_AS_NO_PREPEND)
774 ? " no-prepend"
775 : "",
776 CHECK_FLAG(updgrp->conf->flags,
777 PEER_FLAG_LOCAL_AS_REPLACE_AS)
778 ? " replace-as"
779 : "");
780 }
781 }
5691f760
DS
782 if (ctx->uj)
783 json_subgrps = json_object_new_array();
a2addae8 784 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
d62a17ae 785 if (ctx->subgrp_id && (ctx->subgrp_id != subgrp->id))
786 continue;
fa5a9276
AR
787 if (ctx->uj) {
788 json_subgrp = json_object_new_object();
789 json_object_int_add(json_subgrp, "subGroupId",
790 subgrp->id);
791 tbuf = monotime(NULL);
792 tbuf -= subgrp->uptime;
793 epoch_tbuf = time(NULL) - tbuf;
794 json_subgrp_time = json_object_new_object();
795 json_object_int_add(json_subgrp_time, "epoch",
796 epoch_tbuf);
797 json_object_string_add(json_subgrp_time, "epochString",
798 ctime(&epoch_tbuf));
799 json_object_object_add(json_subgrp, "groupCreateTime",
800 json_subgrp_time);
801 } else {
802 vty_out(vty, "\n");
803 vty_out(vty, " Update-subgroup %" PRIu64 ":\n",
804 subgrp->id);
805 vty_out(vty, " Created: %s",
806 timestamp_string(subgrp->uptime));
807 }
d62a17ae 808
809 if (subgrp->split_from.update_group_id
810 || subgrp->split_from.subgroup_id) {
fa5a9276
AR
811 if (ctx->uj) {
812 json_object_int_add(
813 json_subgrp, "splitGroupId",
814 subgrp->split_from.update_group_id);
815 json_object_int_add(
816 json_subgrp, "splitSubGroupId",
817 subgrp->split_from.subgroup_id);
818 } else {
819 vty_out(vty,
820 " Split from group id: %" PRIu64
821 "\n",
822 subgrp->split_from.update_group_id);
823 vty_out(vty,
824 " Split from subgroup id: %" PRIu64
825 "\n",
826 subgrp->split_from.subgroup_id);
827 }
d62a17ae 828 }
829
fa5a9276
AR
830 if (ctx->uj) {
831 json_subgrp_event = json_object_new_object();
832 json_object_int_add(json_subgrp_event, "joinEvents",
833 subgrp->join_events);
834 json_object_int_add(json_subgrp_event, "pruneEvents",
835 subgrp->prune_events);
836 json_object_int_add(json_subgrp_event, "mergeEvents",
837 subgrp->merge_events);
838 json_object_int_add(json_subgrp_event, "splitEvents",
839 subgrp->split_events);
840 json_object_int_add(json_subgrp_event, "switchEvents",
841 subgrp->updgrp_switch_events);
842 json_object_int_add(json_subgrp_event,
843 "peerRefreshEvents",
844 subgrp->peer_refreshes_combined);
845 json_object_int_add(json_subgrp_event,
846 "mergeCheckEvents",
847 subgrp->merge_checks_triggered);
848 json_object_object_add(json_subgrp, "statistics",
849 json_subgrp_event);
850 json_object_int_add(json_subgrp, "coalesceTime",
851 (UPDGRP_INST(subgrp->update_group))
852 ->coalesce_time);
853 json_object_int_add(json_subgrp, "version",
854 subgrp->version);
855 json_pkt_info = json_object_new_object();
856 json_object_int_add(
857 json_pkt_info, "qeueueLen",
858 bpacket_queue_length(SUBGRP_PKTQ(subgrp)));
859 json_object_int_add(
860 json_pkt_info, "queuedTotal",
861 subgroup_total_packets_enqueued(subgrp));
862 json_object_int_add(
863 json_pkt_info, "queueHwmLen",
864 bpacket_queue_hwm_length(SUBGRP_PKTQ(subgrp)));
865 json_object_int_add(
866 json_pkt_info, "totalEnqueued",
867 subgroup_total_packets_enqueued(subgrp));
868 json_object_object_add(json_subgrp, "packetQueueInfo",
869 json_pkt_info);
870 json_object_int_add(json_subgrp, "adjListCount",
871 subgrp->adj_count);
872 json_object_boolean_add(
873 json_subgrp, "needsRefresh",
874 CHECK_FLAG(subgrp->flags,
875 SUBGRP_FLAG_NEEDS_REFRESH));
876 } else {
877 vty_out(vty, " Join events: %u\n",
878 subgrp->join_events);
879 vty_out(vty, " Prune events: %u\n",
880 subgrp->prune_events);
881 vty_out(vty, " Merge events: %u\n",
882 subgrp->merge_events);
883 vty_out(vty, " Split events: %u\n",
884 subgrp->split_events);
885 vty_out(vty, " Update group switch events: %u\n",
886 subgrp->updgrp_switch_events);
887 vty_out(vty, " Peer refreshes combined: %u\n",
888 subgrp->peer_refreshes_combined);
889 vty_out(vty, " Merge checks triggered: %u\n",
890 subgrp->merge_checks_triggered);
891 vty_out(vty, " Coalesce Time: %u%s\n",
892 (UPDGRP_INST(subgrp->update_group))
893 ->coalesce_time,
894 subgrp->t_coalesce ? "(Running)" : "");
895 vty_out(vty, " Version: %" PRIu64 "\n",
896 subgrp->version);
897 vty_out(vty, " Packet queue length: %d\n",
898 bpacket_queue_length(SUBGRP_PKTQ(subgrp)));
899 vty_out(vty, " Total packets enqueued: %u\n",
900 subgroup_total_packets_enqueued(subgrp));
901 vty_out(vty, " Packet queue high watermark: %d\n",
902 bpacket_queue_hwm_length(SUBGRP_PKTQ(subgrp)));
903 vty_out(vty, " Adj-out list count: %u\n",
904 subgrp->adj_count);
905 vty_out(vty, " Advertise list: %s\n",
906 advertise_list_is_empty(subgrp) ? "empty"
907 : "not empty");
908 vty_out(vty, " Flags: %s\n",
909 CHECK_FLAG(subgrp->flags,
910 SUBGRP_FLAG_NEEDS_REFRESH)
911 ? "R"
912 : "");
913 if (peer)
914 vty_out(vty, " Max packet size: %d\n",
915 peer->max_packet_size);
916 }
d62a17ae 917 if (subgrp->peer_count > 0) {
fa5a9276
AR
918 if (ctx->uj) {
919 json_peers = json_object_new_array();
920 SUBGRP_FOREACH_PEER (subgrp, paf) {
921 json_object *peer =
922 json_object_new_string(
923 paf->peer->host);
924 json_object_array_add(json_peers, peer);
925 }
926 json_object_object_add(json_subgrp, "peers",
927 json_peers);
928 } else {
929 vty_out(vty, " Peers:\n");
930 SUBGRP_FOREACH_PEER (subgrp, paf)
931 vty_out(vty, " - %s\n",
932 paf->peer->host);
933 }
d62a17ae 934 }
fa5a9276
AR
935
936 if (ctx->uj)
937 json_object_array_add(json_subgrps, json_subgrp);
8fe8a7f6 938 }
fa5a9276
AR
939
940 if (ctx->uj) {
941 json_object_object_add(json_updgrp, "subGroup", json_subgrps);
942 json_object_object_addf(ctx->json_updategrps, json_updgrp,
943 "%" PRIu64, updgrp->id);
944 }
945
d62a17ae 946 return UPDWALK_CONTINUE;
3f9c7369
DS
947}
948
949/*
950 * Helper function to show the packet queue for each subgroup of update group.
951 * Will be constrained to a particular subgroup id if id !=0
952 */
d62a17ae 953static int updgrp_show_packet_queue_walkcb(struct update_group *updgrp,
954 void *arg)
3f9c7369 955{
d62a17ae 956 struct updwalk_context *ctx = arg;
957 struct update_subgroup *subgrp;
958 struct vty *vty;
959
960 vty = ctx->vty;
a2addae8 961 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
d62a17ae 962 if (ctx->subgrp_id && (ctx->subgrp_id != subgrp->id))
963 continue;
964 vty_out(vty, "update group %" PRIu64 ", subgroup %" PRIu64 "\n",
965 updgrp->id, subgrp->id);
966 bpacket_queue_show_vty(SUBGRP_PKTQ(subgrp), vty);
967 }
968 return UPDWALK_CONTINUE;
3f9c7369
DS
969}
970
971/*
972 * Show the packet queue for each subgroup of update group. Will be
973 * constrained to a particular subgroup id if id !=0
974 */
d62a17ae 975void update_group_show_packet_queue(struct bgp *bgp, afi_t afi, safi_t safi,
976 struct vty *vty, uint64_t id)
3f9c7369 977{
d62a17ae 978 struct updwalk_context ctx;
979
980 memset(&ctx, 0, sizeof(ctx));
981 ctx.vty = vty;
982 ctx.subgrp_id = id;
983 ctx.flags = 0;
984 update_group_af_walk(bgp, afi, safi, updgrp_show_packet_queue_walkcb,
985 &ctx);
3f9c7369
DS
986}
987
d62a17ae 988static struct update_group *update_group_find(struct peer_af *paf)
3f9c7369 989{
d62a17ae 990 struct update_group *updgrp;
991 struct update_group tmp;
992 struct peer tmp_conf;
3f9c7369 993
d62a17ae 994 if (!peer_established(PAF_PEER(paf)))
995 return NULL;
3f9c7369 996
d62a17ae 997 memset(&tmp, 0, sizeof(tmp));
998 memset(&tmp_conf, 0, sizeof(tmp_conf));
999 tmp.conf = &tmp_conf;
1000 peer2_updgrp_copy(&tmp, paf);
3f9c7369 1001
d62a17ae 1002 updgrp = hash_lookup(paf->peer->bgp->update_groups[paf->afid], &tmp);
1003 conf_release(&tmp_conf, paf->afi, paf->safi);
1004 return updgrp;
3f9c7369
DS
1005}
1006
d62a17ae 1007static struct update_group *update_group_create(struct peer_af *paf)
3f9c7369 1008{
d62a17ae 1009 struct update_group *updgrp;
1010 struct update_group tmp;
1011 struct peer tmp_conf;
3f9c7369 1012
d62a17ae 1013 memset(&tmp, 0, sizeof(tmp));
1014 memset(&tmp_conf, 0, sizeof(tmp_conf));
1015 tmp.conf = &tmp_conf;
1016 peer2_updgrp_copy(&tmp, paf);
3f9c7369 1017
d62a17ae 1018 updgrp = hash_get(paf->peer->bgp->update_groups[paf->afid], &tmp,
1019 updgrp_hash_alloc);
d62a17ae 1020 update_group_checkin(updgrp);
3f9c7369 1021
d62a17ae 1022 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
1023 zlog_debug("create update group %" PRIu64, updgrp->id);
3f9c7369 1024
d62a17ae 1025 UPDGRP_GLOBAL_STAT(updgrp, updgrps_created) += 1;
3f9c7369 1026
d62a17ae 1027 conf_release(&tmp_conf, paf->afi, paf->safi);
1028 return updgrp;
3f9c7369
DS
1029}
1030
d62a17ae 1031static void update_group_delete(struct update_group *updgrp)
3f9c7369 1032{
d62a17ae 1033 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
1034 zlog_debug("delete update group %" PRIu64, updgrp->id);
3f9c7369 1035
d62a17ae 1036 UPDGRP_GLOBAL_STAT(updgrp, updgrps_deleted) += 1;
3f9c7369 1037
d62a17ae 1038 hash_release(updgrp->bgp->update_groups[updgrp->afid], updgrp);
1039 conf_release(updgrp->conf, updgrp->afi, updgrp->safi);
3d68677e 1040
0a22ddfb 1041 XFREE(MTYPE_BGP_PEER_HOST, updgrp->conf->host);
6e919709 1042
0a22ddfb 1043 XFREE(MTYPE_BGP_PEER_IFNAME, updgrp->conf->ifname);
6e919709 1044
d62a17ae 1045 XFREE(MTYPE_BGP_PEER, updgrp->conf);
1046 XFREE(MTYPE_BGP_UPDGRP, updgrp);
3f9c7369
DS
1047}
1048
d62a17ae 1049static void update_group_add_subgroup(struct update_group *updgrp,
1050 struct update_subgroup *subgrp)
3f9c7369 1051{
d62a17ae 1052 if (!updgrp || !subgrp)
1053 return;
3f9c7369 1054
d62a17ae 1055 LIST_INSERT_HEAD(&(updgrp->subgrps), subgrp, updgrp_train);
1056 subgrp->update_group = updgrp;
3f9c7369
DS
1057}
1058
d62a17ae 1059static void update_group_remove_subgroup(struct update_group *updgrp,
1060 struct update_subgroup *subgrp)
3f9c7369 1061{
d62a17ae 1062 if (!updgrp || !subgrp)
1063 return;
3f9c7369 1064
d62a17ae 1065 LIST_REMOVE(subgrp, updgrp_train);
1066 subgrp->update_group = NULL;
1067 if (LIST_EMPTY(&(updgrp->subgrps)))
1068 update_group_delete(updgrp);
3f9c7369
DS
1069}
1070
1071static struct update_subgroup *
d62a17ae 1072update_subgroup_create(struct update_group *updgrp)
3f9c7369 1073{
d62a17ae 1074 struct update_subgroup *subgrp;
3f9c7369 1075
d62a17ae 1076 subgrp = XCALLOC(MTYPE_BGP_UPD_SUBGRP, sizeof(struct update_subgroup));
1077 update_subgroup_checkin(subgrp, updgrp);
1078 subgrp->v_coalesce = (UPDGRP_INST(updgrp))->coalesce_time;
ef56aee4 1079 sync_init(subgrp, updgrp);
d62a17ae 1080 bpacket_queue_init(SUBGRP_PKTQ(subgrp));
1081 bpacket_queue_add(SUBGRP_PKTQ(subgrp), NULL, NULL);
1082 TAILQ_INIT(&(subgrp->adjq));
1083 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
1084 zlog_debug("create subgroup u%" PRIu64 ":s%" PRIu64, updgrp->id,
1085 subgrp->id);
3f9c7369 1086
d62a17ae 1087 update_group_add_subgroup(updgrp, subgrp);
3f9c7369 1088
d62a17ae 1089 UPDGRP_INCR_STAT(updgrp, subgrps_created);
3f9c7369 1090
d62a17ae 1091 return subgrp;
3f9c7369
DS
1092}
1093
d62a17ae 1094static void update_subgroup_delete(struct update_subgroup *subgrp)
3f9c7369 1095{
d62a17ae 1096 if (!subgrp)
1097 return;
3f9c7369 1098
d62a17ae 1099 if (subgrp->update_group)
1100 UPDGRP_INCR_STAT(subgrp->update_group, subgrps_deleted);
3f9c7369 1101
e16d030c
DS
1102 EVENT_OFF(subgrp->t_merge_check);
1103 EVENT_OFF(subgrp->t_coalesce);
3f9c7369 1104
d62a17ae 1105 bpacket_queue_cleanup(SUBGRP_PKTQ(subgrp));
1106 subgroup_clear_table(subgrp);
3f9c7369 1107
d62a17ae 1108 sync_delete(subgrp);
3f9c7369 1109
4f9a63ad 1110 if (BGP_DEBUG(update_groups, UPDATE_GROUPS) && subgrp->update_group)
d62a17ae 1111 zlog_debug("delete subgroup u%" PRIu64 ":s%" PRIu64,
1112 subgrp->update_group->id, subgrp->id);
3f9c7369 1113
d62a17ae 1114 update_group_remove_subgroup(subgrp->update_group, subgrp);
3f9c7369 1115
d62a17ae 1116 XFREE(MTYPE_BGP_UPD_SUBGRP, subgrp);
3f9c7369
DS
1117}
1118
d62a17ae 1119void update_subgroup_inherit_info(struct update_subgroup *to,
1120 struct update_subgroup *from)
3f9c7369 1121{
d62a17ae 1122 if (!to || !from)
1123 return;
3f9c7369 1124
d62a17ae 1125 to->sflags = from->sflags;
3f9c7369
DS
1126}
1127
1128/*
1129 * update_subgroup_check_delete
1130 *
1131 * Delete a subgroup if it is ready to be deleted.
1132 *
2951a7a4 1133 * Returns true if the subgroup was deleted.
3f9c7369 1134 */
3dc339cd 1135static bool update_subgroup_check_delete(struct update_subgroup *subgrp)
3f9c7369 1136{
d62a17ae 1137 if (!subgrp)
3dc339cd 1138 return false;
3f9c7369 1139
d62a17ae 1140 if (!LIST_EMPTY(&(subgrp->peers)))
3dc339cd 1141 return false;
3f9c7369 1142
d62a17ae 1143 update_subgroup_delete(subgrp);
3f9c7369 1144
3dc339cd 1145 return true;
3f9c7369
DS
1146}
1147
1148/*
1149 * update_subgroup_add_peer
1150 *
1151 * @param send_enqueued_packets If true all currently enqueued packets will
1152 * also be sent to the peer.
1153 */
d62a17ae 1154static void update_subgroup_add_peer(struct update_subgroup *subgrp,
1155 struct peer_af *paf,
1156 int send_enqueued_pkts)
3f9c7369 1157{
d62a17ae 1158 struct bpacket *pkt;
3f9c7369 1159
d62a17ae 1160 if (!subgrp || !paf)
1161 return;
3f9c7369 1162
d62a17ae 1163 LIST_INSERT_HEAD(&(subgrp->peers), paf, subgrp_train);
1164 paf->subgroup = subgrp;
1165 subgrp->peer_count++;
3f9c7369 1166
d62a17ae 1167 if (bgp_debug_peer_updout_enabled(paf->peer->host)) {
1168 UPDGRP_PEER_DBG_EN(subgrp->update_group);
1169 }
3f9c7369 1170
d62a17ae 1171 SUBGRP_INCR_STAT(subgrp, join_events);
3f9c7369 1172
d62a17ae 1173 if (send_enqueued_pkts) {
1174 pkt = bpacket_queue_first(SUBGRP_PKTQ(subgrp));
1175 } else {
3f9c7369 1176
d62a17ae 1177 /*
1178 * Hang the peer off of the last, placeholder, packet in the
1179 * queue. This means it won't see any of the packets that are
1180 * currently the queue.
1181 */
1182 pkt = bpacket_queue_last(SUBGRP_PKTQ(subgrp));
1183 assert(pkt->buffer == NULL);
1184 }
3f9c7369 1185
d62a17ae 1186 bpacket_add_peer(pkt, paf);
3f9c7369 1187
7bfdba54
S
1188 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
1189 zlog_debug("peer %s added to subgroup s%" PRIu64,
1190 paf->peer->host, subgrp->id);
3f9c7369
DS
1191}
1192
1193/*
1194 * update_subgroup_remove_peer_internal
1195 *
1196 * Internal function that removes a peer from a subgroup, but does not
1197 * delete the subgroup. A call to this function must almost always be
1198 * followed by a call to update_subgroup_check_delete().
1199 *
1200 * @see update_subgroup_remove_peer
1201 */
d62a17ae 1202static void update_subgroup_remove_peer_internal(struct update_subgroup *subgrp,
1203 struct peer_af *paf)
3f9c7369 1204{
d3e51db0 1205 assert(subgrp && paf && subgrp->update_group);
3f9c7369 1206
d62a17ae 1207 if (bgp_debug_peer_updout_enabled(paf->peer->host)) {
1208 UPDGRP_PEER_DBG_DIS(subgrp->update_group);
1209 }
3f9c7369 1210
d62a17ae 1211 bpacket_queue_remove_peer(paf);
1212 LIST_REMOVE(paf, subgrp_train);
1213 paf->subgroup = NULL;
1214 subgrp->peer_count--;
3f9c7369 1215
7bfdba54
S
1216 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
1217 zlog_debug("peer %s deleted from subgroup s%"
4882d296 1218 PRIu64 " peer cnt %d",
7bfdba54 1219 paf->peer->host, subgrp->id, subgrp->peer_count);
d62a17ae 1220 SUBGRP_INCR_STAT(subgrp, prune_events);
3f9c7369
DS
1221}
1222
1223/*
1224 * update_subgroup_remove_peer
1225 */
d62a17ae 1226void update_subgroup_remove_peer(struct update_subgroup *subgrp,
1227 struct peer_af *paf)
3f9c7369 1228{
d62a17ae 1229 if (!subgrp || !paf)
1230 return;
3f9c7369 1231
d62a17ae 1232 update_subgroup_remove_peer_internal(subgrp, paf);
3f9c7369 1233
d62a17ae 1234 if (update_subgroup_check_delete(subgrp))
1235 return;
3f9c7369 1236
d62a17ae 1237 /*
1238 * The deletion of the peer may have caused some packets to be
1239 * deleted from the subgroup packet queue. Check if the subgroup can
1240 * be merged now.
1241 */
1242 update_subgroup_check_merge(subgrp, "removed peer from subgroup");
3f9c7369
DS
1243}
1244
d62a17ae 1245static struct update_subgroup *update_subgroup_find(struct update_group *updgrp,
1246 struct peer_af *paf)
3f9c7369 1247{
d62a17ae 1248 struct update_subgroup *subgrp = NULL;
1249 uint64_t version;
1250
1251 if (paf->subgroup) {
1252 assert(0);
1253 return NULL;
1254 } else
1255 version = 0;
1256
1257 if (!peer_established(PAF_PEER(paf)))
1258 return NULL;
1259
a2addae8 1260 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
d62a17ae 1261 if (subgrp->version != version
1262 || CHECK_FLAG(subgrp->sflags,
1263 SUBGRP_STATUS_DEFAULT_ORIGINATE))
1264 continue;
1265
1266 /*
1267 * The version number is not meaningful on a subgroup that needs
1268 * a refresh.
1269 */
1270 if (update_subgroup_needs_refresh(subgrp))
1271 continue;
1272
1273 break;
1274 }
1275
1276 return subgrp;
3f9c7369
DS
1277}
1278
1279/*
1280 * update_subgroup_ready_for_merge
1281 *
2951a7a4 1282 * Returns true if this subgroup is in a state that allows it to be
3f9c7369
DS
1283 * merged into another subgroup.
1284 */
3dc339cd 1285static bool update_subgroup_ready_for_merge(struct update_subgroup *subgrp)
3f9c7369
DS
1286{
1287
d62a17ae 1288 /*
1289 * Not ready if there are any encoded packets waiting to be written
1290 * out to peers.
1291 */
1292 if (!bpacket_queue_is_empty(SUBGRP_PKTQ(subgrp)))
3dc339cd 1293 return false;
d62a17ae 1294
1295 /*
1296 * Not ready if there enqueued updates waiting to be encoded.
1297 */
1298 if (!advertise_list_is_empty(subgrp))
3dc339cd 1299 return false;
d62a17ae 1300
1301 /*
1302 * Don't attempt to merge a subgroup that needs a refresh. For one,
1303 * we can't determine if the adj_out of such a group matches that of
1304 * another group.
1305 */
1306 if (update_subgroup_needs_refresh(subgrp))
3dc339cd 1307 return false;
d62a17ae 1308
3dc339cd 1309 return true;
3f9c7369
DS
1310}
1311
1312/*
1313 * update_subgrp_can_merge_into
1314 *
2951a7a4 1315 * Returns true if the first subgroup can merge into the second
3f9c7369
DS
1316 * subgroup.
1317 */
d62a17ae 1318static int update_subgroup_can_merge_into(struct update_subgroup *subgrp,
1319 struct update_subgroup *target)
3f9c7369
DS
1320{
1321
d62a17ae 1322 if (subgrp == target)
1323 return 0;
3f9c7369 1324
d62a17ae 1325 /*
1326 * Both must have processed the BRIB to the same point in order to
1327 * be merged.
1328 */
1329 if (subgrp->version != target->version)
1330 return 0;
3f9c7369 1331
d62a17ae 1332 if (CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE)
1333 != CHECK_FLAG(target->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE))
1334 return 0;
f910ef58 1335
d62a17ae 1336 if (subgrp->adj_count != target->adj_count)
1337 return 0;
3f9c7369 1338
d62a17ae 1339 return update_subgroup_ready_for_merge(target);
3f9c7369
DS
1340}
1341
1342/*
1343 * update_subgroup_merge
1344 *
1345 * Merge the first subgroup into the second one.
1346 */
d62a17ae 1347static void update_subgroup_merge(struct update_subgroup *subgrp,
1348 struct update_subgroup *target,
1349 const char *reason)
3f9c7369 1350{
d62a17ae 1351 struct peer_af *paf;
1352 int result;
1353 int peer_count;
3f9c7369 1354
d62a17ae 1355 assert(subgrp->adj_count == target->adj_count);
3f9c7369 1356
d62a17ae 1357 peer_count = subgrp->peer_count;
3f9c7369 1358
d62a17ae 1359 while (1) {
1360 paf = LIST_FIRST(&subgrp->peers);
1361 if (!paf)
1362 break;
3f9c7369 1363
d62a17ae 1364 update_subgroup_remove_peer_internal(subgrp, paf);
3f9c7369 1365
d62a17ae 1366 /*
1367 * Add the peer to the target subgroup, while making sure that
1368 * any currently enqueued packets won't be sent to it. Enqueued
1369 * packets could, for example, result in an unnecessary withdraw
1370 * followed by an advertise.
1371 */
1372 update_subgroup_add_peer(target, paf, 0);
1373 }
3f9c7369 1374
d62a17ae 1375 SUBGRP_INCR_STAT(target, merge_events);
3f9c7369 1376
d62a17ae 1377 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
6cde4b45 1378 zlog_debug("u%" PRIu64 ":s%" PRIu64" (%d peers) merged into u%" PRIu64 ":s%" PRIu64", trigger: %s",
d62a17ae 1379 subgrp->update_group->id, subgrp->id, peer_count,
1380 target->update_group->id, target->id,
1381 reason ? reason : "unknown");
3f9c7369 1382
d62a17ae 1383 result = update_subgroup_check_delete(subgrp);
1384 assert(result);
3f9c7369
DS
1385}
1386
1387/*
1388 * update_subgroup_check_merge
1389 *
1390 * Merge this subgroup into another subgroup if possible.
1391 *
2951a7a4 1392 * Returns true if the subgroup has been merged. The subgroup pointer
3f9c7369
DS
1393 * should not be accessed in this case.
1394 */
3dc339cd
DA
1395bool update_subgroup_check_merge(struct update_subgroup *subgrp,
1396 const char *reason)
3f9c7369 1397{
d62a17ae 1398 struct update_subgroup *target;
3f9c7369 1399
d62a17ae 1400 if (!update_subgroup_ready_for_merge(subgrp))
3dc339cd 1401 return false;
3f9c7369 1402
d62a17ae 1403 /*
1404 * Look for a subgroup to merge into.
1405 */
a2addae8 1406 UPDGRP_FOREACH_SUBGRP (subgrp->update_group, target) {
d62a17ae 1407 if (update_subgroup_can_merge_into(subgrp, target))
1408 break;
1409 }
3f9c7369 1410
d62a17ae 1411 if (!target)
3dc339cd 1412 return false;
3f9c7369 1413
d62a17ae 1414 update_subgroup_merge(subgrp, target, reason);
3dc339cd 1415 return true;
3f9c7369
DS
1416}
1417
d62a17ae 1418/*
9d303b37
DL
1419* update_subgroup_merge_check_thread_cb
1420*/
e6685141 1421static void update_subgroup_merge_check_thread_cb(struct event *thread)
3f9c7369 1422{
d62a17ae 1423 struct update_subgroup *subgrp;
3f9c7369 1424
e16d030c 1425 subgrp = EVENT_ARG(thread);
3f9c7369 1426
d62a17ae 1427 subgrp->t_merge_check = NULL;
3f9c7369 1428
d62a17ae 1429 update_subgroup_check_merge(subgrp, "triggered merge check");
3f9c7369
DS
1430}
1431
1432/*
1433 * update_subgroup_trigger_merge_check
1434 *
1435 * Triggers a call to update_subgroup_check_merge() on a clean context.
1436 *
1437 * @param force If true, the merge check will be triggered even if the
1438 * subgroup doesn't currently look ready for a merge.
1439 *
2951a7a4 1440 * Returns true if a merge check will be performed shortly.
3f9c7369 1441 */
3dc339cd
DA
1442bool update_subgroup_trigger_merge_check(struct update_subgroup *subgrp,
1443 int force)
3f9c7369 1444{
d62a17ae 1445 if (subgrp->t_merge_check)
3dc339cd 1446 return true;
3f9c7369 1447
d62a17ae 1448 if (!force && !update_subgroup_ready_for_merge(subgrp))
3dc339cd 1449 return false;
3f9c7369 1450
d62a17ae 1451 subgrp->t_merge_check = NULL;
907a2395
DS
1452 event_add_timer_msec(bm->master, update_subgroup_merge_check_thread_cb,
1453 subgrp, 0, &subgrp->t_merge_check);
3f9c7369 1454
d62a17ae 1455 SUBGRP_INCR_STAT(subgrp, merge_checks_triggered);
3f9c7369 1456
3dc339cd 1457 return true;
3f9c7369
DS
1458}
1459
1460/*
1461 * update_subgroup_copy_adj_out
1462 *
1463 * Helper function that clones the adj out (state about advertised
1464 * routes) from one subgroup to another. It assumes that the adj out
1465 * of the target subgroup is empty.
1466 */
d62a17ae 1467static void update_subgroup_copy_adj_out(struct update_subgroup *source,
1468 struct update_subgroup *dest)
3f9c7369 1469{
d62a17ae 1470 struct bgp_adj_out *aout, *aout_copy;
1471
a2addae8 1472 SUBGRP_FOREACH_ADJ (source, aout) {
d62a17ae 1473 /*
1474 * Copy the adj out.
1475 */
9bcb3eef
DS
1476 aout_copy = bgp_adj_out_alloc(dest, aout->dest,
1477 aout->addpath_tx_id);
d62a17ae 1478 aout_copy->attr =
7c87afac 1479 aout->attr ? bgp_attr_intern(aout->attr) : NULL;
d62a17ae 1480 }
0ab7b206
AD
1481
1482 dest->scount = source->scount;
3f9c7369
DS
1483}
1484
1485/*
1486 * update_subgroup_copy_packets
1487 *
1488 * Copy packets after and including the given packet to the subgroup
1489 * 'dest'.
1490 *
1491 * Returns the number of packets copied.
1492 */
d62a17ae 1493static int update_subgroup_copy_packets(struct update_subgroup *dest,
1494 struct bpacket *pkt)
3f9c7369 1495{
d62a17ae 1496 int count;
1497
1498 count = 0;
1499 while (pkt && pkt->buffer) {
1500 bpacket_queue_add(SUBGRP_PKTQ(dest), stream_dup(pkt->buffer),
1501 &pkt->arr);
1502 count++;
1503 pkt = bpacket_next(pkt);
1504 }
3f9c7369 1505
d62a17ae 1506 return count;
3f9c7369
DS
1507}
1508
3dc339cd
DA
1509static bool updgrp_prefix_list_update(struct update_group *updgrp,
1510 const char *name)
3f9c7369 1511{
d62a17ae 1512 struct peer *peer;
1513 struct bgp_filter *filter;
1514
1515 peer = UPDGRP_PEER(updgrp);
1516 filter = &peer->filter[UPDGRP_AFI(updgrp)][UPDGRP_SAFI(updgrp)];
1517
1518 if (PREFIX_LIST_OUT_NAME(filter)
1519 && (strcmp(name, PREFIX_LIST_OUT_NAME(filter)) == 0)) {
1520 PREFIX_LIST_OUT(filter) = prefix_list_lookup(
1521 UPDGRP_AFI(updgrp), PREFIX_LIST_OUT_NAME(filter));
3dc339cd 1522 return true;
d62a17ae 1523 }
3dc339cd 1524 return false;
3f9c7369
DS
1525}
1526
3dc339cd
DA
1527static bool updgrp_filter_list_update(struct update_group *updgrp,
1528 const char *name)
3f9c7369 1529{
d62a17ae 1530 struct peer *peer;
1531 struct bgp_filter *filter;
1532
1533 peer = UPDGRP_PEER(updgrp);
1534 filter = &peer->filter[UPDGRP_AFI(updgrp)][UPDGRP_SAFI(updgrp)];
1535
1536 if (FILTER_LIST_OUT_NAME(filter)
1537 && (strcmp(name, FILTER_LIST_OUT_NAME(filter)) == 0)) {
1538 FILTER_LIST_OUT(filter) =
1539 as_list_lookup(FILTER_LIST_OUT_NAME(filter));
3dc339cd 1540 return true;
d62a17ae 1541 }
3dc339cd 1542 return false;
3f9c7369
DS
1543}
1544
3dc339cd
DA
1545static bool updgrp_distribute_list_update(struct update_group *updgrp,
1546 const char *name)
3f9c7369 1547{
d62a17ae 1548 struct peer *peer;
1549 struct bgp_filter *filter;
1550
1551 peer = UPDGRP_PEER(updgrp);
1552 filter = &peer->filter[UPDGRP_AFI(updgrp)][UPDGRP_SAFI(updgrp)];
1553
1554 if (DISTRIBUTE_OUT_NAME(filter)
1555 && (strcmp(name, DISTRIBUTE_OUT_NAME(filter)) == 0)) {
1556 DISTRIBUTE_OUT(filter) = access_list_lookup(
1557 UPDGRP_AFI(updgrp), DISTRIBUTE_OUT_NAME(filter));
3dc339cd 1558 return true;
d62a17ae 1559 }
3dc339cd 1560 return false;
3f9c7369
DS
1561}
1562
d62a17ae 1563static int updgrp_route_map_update(struct update_group *updgrp,
1564 const char *name, int *def_rmap_changed)
3f9c7369 1565{
d62a17ae 1566 struct peer *peer;
1567 struct bgp_filter *filter;
1568 int changed = 0;
1569 afi_t afi;
1570 safi_t safi;
1571
1572 peer = UPDGRP_PEER(updgrp);
1573 afi = UPDGRP_AFI(updgrp);
1574 safi = UPDGRP_SAFI(updgrp);
1575 filter = &peer->filter[afi][safi];
1576
1577 if (ROUTE_MAP_OUT_NAME(filter)
1578 && (strcmp(name, ROUTE_MAP_OUT_NAME(filter)) == 0)) {
1579 ROUTE_MAP_OUT(filter) = route_map_lookup_by_name(name);
1580
1581 changed = 1;
1582 }
1583
1584 if (UNSUPPRESS_MAP_NAME(filter)
1585 && (strcmp(name, UNSUPPRESS_MAP_NAME(filter)) == 0)) {
1586 UNSUPPRESS_MAP(filter) = route_map_lookup_by_name(name);
1587 changed = 1;
1588 }
1589
1590 /* process default-originate route-map */
1591 if (peer->default_rmap[afi][safi].name
1592 && (strcmp(name, peer->default_rmap[afi][safi].name) == 0)) {
1593 peer->default_rmap[afi][safi].map =
1594 route_map_lookup_by_name(name);
1595 if (def_rmap_changed)
1596 *def_rmap_changed = 1;
1597 }
1598 return changed;
3f9c7369
DS
1599}
1600
1601/*
1602 * hash iteration callback function to process a policy change for an
1603 * update group. Check if the changed policy matches the updgrp's
1604 * outbound route-map or unsuppress-map or default-originate map or
1605 * filter-list or prefix-list or distribute-list.
1606 * Trigger update generation accordingly.
1607 */
d62a17ae 1608static int updgrp_policy_update_walkcb(struct update_group *updgrp, void *arg)
3f9c7369 1609{
d62a17ae 1610 struct updwalk_context *ctx = arg;
1611 struct update_subgroup *subgrp;
1612 int changed = 0;
1613 int def_changed = 0;
1614
1615 if (!updgrp || !ctx || !ctx->policy_name)
1616 return UPDWALK_CONTINUE;
1617
1618 switch (ctx->policy_type) {
1619 case BGP_POLICY_ROUTE_MAP:
1620 changed = updgrp_route_map_update(updgrp, ctx->policy_name,
1621 &def_changed);
1622 break;
1623 case BGP_POLICY_FILTER_LIST:
1624 changed = updgrp_filter_list_update(updgrp, ctx->policy_name);
1625 break;
1626 case BGP_POLICY_PREFIX_LIST:
1627 changed = updgrp_prefix_list_update(updgrp, ctx->policy_name);
1628 break;
1629 case BGP_POLICY_DISTRIBUTE_LIST:
1630 changed =
1631 updgrp_distribute_list_update(updgrp, ctx->policy_name);
1632 break;
1633 default:
1634 break;
1635 }
1636
1637 /* If not doing route update, return after updating "config" */
1638 if (!ctx->policy_route_update)
1639 return UPDWALK_CONTINUE;
1640
1641 /* If nothing has changed, return after updating "config" */
1642 if (!changed && !def_changed)
1643 return UPDWALK_CONTINUE;
1644
1645 /*
1646 * If something has changed, at the beginning of a route-map
1647 * modification
1648 * event, mark each subgroup's needs-refresh bit. For one, it signals to
1649 * whoever that the subgroup needs a refresh. Second, it prevents
1650 * premature
1651 * merge of this subgroup with another before a complete (outbound)
1652 * refresh.
1653 */
1654 if (ctx->policy_event_start_flag) {
a2addae8 1655 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
d62a17ae 1656 update_subgroup_set_needs_refresh(subgrp, 1);
1657 }
1658 return UPDWALK_CONTINUE;
1659 }
1660
a2addae8 1661 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
2adac256
DA
1662 /* Avoid supressing duplicate routes later
1663 * when processing in subgroup_announce_table().
1664 */
1665 SET_FLAG(subgrp->sflags, SUBGRP_STATUS_FORCE_UPDATES);
1666
d62a17ae 1667 if (changed) {
1668 if (bgp_debug_update(NULL, NULL, updgrp, 0))
1669 zlog_debug(
6cde4b45 1670 "u%" PRIu64 ":s%" PRIu64" announcing routes upon policy %s (type %d) change",
d62a17ae 1671 updgrp->id, subgrp->id,
1672 ctx->policy_name, ctx->policy_type);
1673 subgroup_announce_route(subgrp);
1674 }
1675 if (def_changed) {
1676 if (bgp_debug_update(NULL, NULL, updgrp, 0))
1677 zlog_debug(
6cde4b45 1678 "u%" PRIu64 ":s%" PRIu64" announcing default upon default routemap %s change",
d62a17ae 1679 updgrp->id, subgrp->id,
1680 ctx->policy_name);
a9ae9fb5
AR
1681 if (route_map_lookup_by_name(ctx->policy_name)) {
1682 /*
1683 * When there is change in routemap, this flow
1684 * is triggered. the routemap is still present
1685 * in lib, hence its a update flow. The flag
1686 * needs to be unset.
1687 */
1688 UNSET_FLAG(subgrp->sflags,
1689 SUBGRP_STATUS_DEFAULT_ORIGINATE);
1690 subgroup_default_originate(subgrp, 0);
1691 } else {
1692 /*
1693 * This is a explicit withdraw, since the
1694 * routemap is not present in routemap lib. need
1695 * to pass 1 for withdraw arg.
1696 */
1697 subgroup_default_originate(subgrp, 1);
1698 }
d62a17ae 1699 }
1700 update_subgroup_set_needs_refresh(subgrp, 0);
1701 }
1702 return UPDWALK_CONTINUE;
3f9c7369
DS
1703}
1704
e3b78da8 1705static int update_group_walkcb(struct hash_bucket *bucket, void *arg)
3f9c7369 1706{
e3b78da8 1707 struct update_group *updgrp = bucket->data;
d62a17ae 1708 struct updwalk_context *wctx = arg;
1709 int ret = (*wctx->cb)(updgrp, wctx->context);
1710 return ret;
3f9c7369
DS
1711}
1712
d62a17ae 1713static int update_group_periodic_merge_walkcb(struct update_group *updgrp,
1714 void *arg)
3f9c7369 1715{
d62a17ae 1716 struct update_subgroup *subgrp;
1717 struct update_subgroup *tmp_subgrp;
1718 const char *reason = arg;
3f9c7369 1719
a2addae8
RW
1720 UPDGRP_FOREACH_SUBGRP_SAFE (updgrp, subgrp, tmp_subgrp)
1721 update_subgroup_check_merge(subgrp, reason);
d62a17ae 1722 return UPDWALK_CONTINUE;
3f9c7369
DS
1723}
1724
1725/********************
1726 * PUBLIC FUNCTIONS
1727 ********************/
1728
1729/*
1730 * trigger function when a policy (route-map/filter-list/prefix-list/
1731 * distribute-list etc.) content changes. Go through all the
1732 * update groups and process the change.
1733 *
1734 * bgp: the bgp instance
1735 * ptype: the type of policy that got modified, see bgpd.h
1736 * pname: name of the policy
1737 * route_update: flag to control if an automatic update generation should
1738 * occur
1739 * start_event: flag that indicates if it's the beginning of the change.
1740 * Esp. when the user is changing the content interactively
1741 * over multiple statements. Useful to set dirty flag on
1742 * update groups.
1743 */
d54f55f2 1744void update_group_policy_update(struct bgp *bgp, enum bgp_policy_type ptype,
f1aa4929 1745 const char *pname, bool route_update,
d62a17ae 1746 int start_event)
3f9c7369 1747{
d62a17ae 1748 struct updwalk_context ctx;
3f9c7369 1749
d62a17ae 1750 memset(&ctx, 0, sizeof(ctx));
1751 ctx.policy_type = ptype;
1752 ctx.policy_name = pname;
1753 ctx.policy_route_update = route_update;
1754 ctx.policy_event_start_flag = start_event;
1755 ctx.flags = 0;
3f9c7369 1756
d62a17ae 1757 update_group_walk(bgp, updgrp_policy_update_walkcb, &ctx);
3f9c7369
DS
1758}
1759
1760/*
1761 * update_subgroup_split_peer
1762 *
1763 * Ensure that the given peer is in a subgroup of its own in the
1764 * specified update group.
1765 */
d62a17ae 1766void update_subgroup_split_peer(struct peer_af *paf,
1767 struct update_group *updgrp)
3f9c7369 1768{
d62a17ae 1769 struct update_subgroup *old_subgrp, *subgrp;
1770 uint64_t old_id;
1771
1772
1773 old_subgrp = paf->subgroup;
1774
1775 if (!updgrp)
1776 updgrp = old_subgrp->update_group;
1777
1778 /*
1779 * If the peer is alone in its subgroup, reuse the existing
1780 * subgroup.
1781 */
1782 if (old_subgrp->peer_count == 1) {
1783 if (updgrp == old_subgrp->update_group)
1784 return;
1785
1786 subgrp = old_subgrp;
1787 old_id = old_subgrp->update_group->id;
1788
1789 if (bgp_debug_peer_updout_enabled(paf->peer->host)) {
1790 UPDGRP_PEER_DBG_DIS(old_subgrp->update_group);
1791 }
1792
1793 update_group_remove_subgroup(old_subgrp->update_group,
1794 old_subgrp);
1795 update_group_add_subgroup(updgrp, subgrp);
1796
1797 if (bgp_debug_peer_updout_enabled(paf->peer->host)) {
1798 UPDGRP_PEER_DBG_EN(updgrp);
1799 }
1800 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
6cde4b45 1801 zlog_debug("u%" PRIu64 ":s%" PRIu64" peer %s moved to u%" PRIu64 ":s%" PRIu64,
d62a17ae 1802 old_id, subgrp->id, paf->peer->host,
1803 updgrp->id, subgrp->id);
1804
1805 /*
1806 * The state of the subgroup (adj_out, advs, packet queue etc)
1807 * is consistent internally, but may not be identical to other
1808 * subgroups in the new update group even if the version number
1809 * matches up. Make sure a full refresh is done before the
1810 * subgroup is merged with another.
1811 */
1812 update_subgroup_set_needs_refresh(subgrp, 1);
1813
1814 SUBGRP_INCR_STAT(subgrp, updgrp_switch_events);
1815 return;
1816 }
3f9c7369 1817
d62a17ae 1818 /*
1819 * Create a new subgroup under the specified update group, and copy
1820 * over relevant state to it.
1821 */
1822 subgrp = update_subgroup_create(updgrp);
1823 update_subgroup_inherit_info(subgrp, old_subgrp);
1824
1825 subgrp->split_from.update_group_id = old_subgrp->update_group->id;
1826 subgrp->split_from.subgroup_id = old_subgrp->id;
1827
1828 /*
1829 * Copy out relevant state from the old subgroup.
1830 */
1831 update_subgroup_copy_adj_out(paf->subgroup, subgrp);
1832 update_subgroup_copy_packets(subgrp, paf->next_pkt_to_send);
1833
1834 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
6cde4b45 1835 zlog_debug("u%" PRIu64 ":s%" PRIu64" peer %s split and moved into u%" PRIu64":s%" PRIu64,
d62a17ae 1836 paf->subgroup->update_group->id, paf->subgroup->id,
1837 paf->peer->host, updgrp->id, subgrp->id);
1838
1839 SUBGRP_INCR_STAT(paf->subgroup, split_events);
1840
1841 /*
1842 * Since queued advs were left behind, this new subgroup needs a
1843 * refresh.
1844 */
1845 update_subgroup_set_needs_refresh(subgrp, 1);
1846
1847 /*
1848 * Remove peer from old subgroup, and add it to the new one.
1849 */
1850 update_subgroup_remove_peer(paf->subgroup, paf);
1851
1852 update_subgroup_add_peer(subgrp, paf, 1);
3f9c7369
DS
1853}
1854
d62a17ae 1855void update_bgp_group_init(struct bgp *bgp)
3f9c7369 1856{
d62a17ae 1857 int afid;
3f9c7369 1858
a2addae8 1859 AF_FOREACH (afid)
3f65c5b1 1860 bgp->update_groups[afid] =
996c9314 1861 hash_create(updgrp_hash_key_make, updgrp_hash_cmp,
3f65c5b1 1862 "BGP Update Group Hash");
3f9c7369
DS
1863}
1864
d62a17ae 1865void update_bgp_group_free(struct bgp *bgp)
3d68677e 1866{
d62a17ae 1867 int afid;
1868
a2addae8 1869 AF_FOREACH (afid) {
d62a17ae 1870 if (bgp->update_groups[afid]) {
1871 hash_free(bgp->update_groups[afid]);
1872 bgp->update_groups[afid] = NULL;
1873 }
1874 }
3d68677e
DS
1875}
1876
d62a17ae 1877void update_group_show(struct bgp *bgp, afi_t afi, safi_t safi, struct vty *vty,
fa5a9276 1878 uint64_t subgrp_id, bool uj)
3f9c7369 1879{
d62a17ae 1880 struct updwalk_context ctx;
fa5a9276
AR
1881 json_object *json_vrf_obj = NULL;
1882
d62a17ae 1883 memset(&ctx, 0, sizeof(ctx));
1884 ctx.vty = vty;
1885 ctx.subgrp_id = subgrp_id;
fa5a9276
AR
1886 ctx.uj = uj;
1887
1888 if (uj) {
1889 ctx.json_updategrps = json_object_new_object();
1890 json_vrf_obj = json_object_new_object();
1891 }
8fe8a7f6 1892
d62a17ae 1893 update_group_af_walk(bgp, afi, safi, update_group_show_walkcb, &ctx);
fa5a9276
AR
1894
1895 if (uj) {
1896 const char *vname;
1897
1898 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
1899 vname = VRF_DEFAULT_NAME;
1900 else
1901 vname = bgp->name;
1902 json_object_object_add(json_vrf_obj, vname,
1903 ctx.json_updategrps);
1904 vty_json(vty, json_vrf_obj);
1905 }
3f9c7369
DS
1906}
1907
1908/*
1909 * update_group_show_stats
1910 *
1911 * Show global statistics about update groups.
1912 */
d62a17ae 1913void update_group_show_stats(struct bgp *bgp, struct vty *vty)
3f9c7369 1914{
d62a17ae 1915 vty_out(vty, "Update groups created: %u\n",
1916 bgp->update_group_stats.updgrps_created);
1917 vty_out(vty, "Update groups deleted: %u\n",
1918 bgp->update_group_stats.updgrps_deleted);
1919 vty_out(vty, "Update subgroups created: %u\n",
1920 bgp->update_group_stats.subgrps_created);
1921 vty_out(vty, "Update subgroups deleted: %u\n",
1922 bgp->update_group_stats.subgrps_deleted);
1923 vty_out(vty, "Join events: %u\n", bgp->update_group_stats.join_events);
1924 vty_out(vty, "Prune events: %u\n",
1925 bgp->update_group_stats.prune_events);
1926 vty_out(vty, "Merge events: %u\n",
1927 bgp->update_group_stats.merge_events);
1928 vty_out(vty, "Split events: %u\n",
1929 bgp->update_group_stats.split_events);
1930 vty_out(vty, "Update group switch events: %u\n",
1931 bgp->update_group_stats.updgrp_switch_events);
1932 vty_out(vty, "Peer route refreshes combined: %u\n",
1933 bgp->update_group_stats.peer_refreshes_combined);
1934 vty_out(vty, "Merge checks triggered: %u\n",
1935 bgp->update_group_stats.merge_checks_triggered);
3f9c7369
DS
1936}
1937
1938/*
1939 * update_group_adjust_peer
1940 */
d62a17ae 1941void update_group_adjust_peer(struct peer_af *paf)
3f9c7369 1942{
d62a17ae 1943 struct update_group *updgrp;
1944 struct update_subgroup *subgrp, *old_subgrp;
1945 struct peer *peer;
1946
1947 if (!paf)
1948 return;
1949
1950 peer = PAF_PEER(paf);
1951 if (!peer_established(peer)) {
1952 return;
1953 }
1954
1955 if (!CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE)) {
1956 return;
3f9c7369 1957 }
3f9c7369 1958
d62a17ae 1959 if (!peer->afc_nego[paf->afi][paf->safi]) {
1960 return;
1961 }
3f9c7369 1962
d62a17ae 1963 updgrp = update_group_find(paf);
1964 if (!updgrp) {
1965 updgrp = update_group_create(paf);
1966 if (!updgrp) {
e50f7cfd 1967 flog_err(EC_BGP_UPDGRP_CREATE,
1c50c1c0
QY
1968 "couldn't create update group for peer %s",
1969 paf->peer->host);
d62a17ae 1970 return;
1971 }
1972 }
3f9c7369 1973
d62a17ae 1974 old_subgrp = paf->subgroup;
3f9c7369 1975
d62a17ae 1976 if (old_subgrp) {
3f9c7369 1977
d62a17ae 1978 /*
1979 * If the update group of the peer is unchanged, the peer can
1980 * stay
1981 * in its existing subgroup and we're done.
1982 */
1983 if (old_subgrp->update_group == updgrp)
1984 return;
1985
1986 /*
1987 * The peer is switching between update groups. Put it in its
1988 * own subgroup under the new update group.
1989 */
1990 update_subgroup_split_peer(paf, updgrp);
1991 return;
1992 }
1993
1994 subgrp = update_subgroup_find(updgrp, paf);
1995 if (!subgrp) {
1996 subgrp = update_subgroup_create(updgrp);
1997 if (!subgrp)
1998 return;
1999 }
3f9c7369 2000
d62a17ae 2001 update_subgroup_add_peer(subgrp, paf, 1);
2002 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
2003 zlog_debug("u%" PRIu64 ":s%" PRIu64 " add peer %s", updgrp->id,
2004 subgrp->id, paf->peer->host);
2005
2006 return;
3f9c7369
DS
2007}
2008
d62a17ae 2009int update_group_adjust_soloness(struct peer *peer, int set)
3f9c7369 2010{
d62a17ae 2011 struct peer_group *group;
2012 struct listnode *node, *nnode;
2013
2014 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
2015 peer_lonesoul_or_not(peer, set);
feb17238 2016 if (peer_established(peer))
d62a17ae 2017 bgp_announce_route_all(peer);
2018 } else {
2019 group = peer->group;
2020 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
2021 peer_lonesoul_or_not(peer, set);
feb17238 2022 if (peer_established(peer))
d62a17ae 2023 bgp_announce_route_all(peer);
2024 }
2025 }
2026 return 0;
3f9c7369
DS
2027}
2028
2029/*
2030 * update_subgroup_rib
2031 */
d62a17ae 2032struct bgp_table *update_subgroup_rib(struct update_subgroup *subgrp)
3f9c7369 2033{
d62a17ae 2034 struct bgp *bgp;
3f9c7369 2035
d62a17ae 2036 bgp = SUBGRP_INST(subgrp);
2037 if (!bgp)
2038 return NULL;
3f9c7369 2039
d62a17ae 2040 return bgp->rib[SUBGRP_AFI(subgrp)][SUBGRP_SAFI(subgrp)];
3f9c7369
DS
2041}
2042
d62a17ae 2043void update_group_af_walk(struct bgp *bgp, afi_t afi, safi_t safi,
2044 updgrp_walkcb cb, void *ctx)
3f9c7369 2045{
d62a17ae 2046 struct updwalk_context wctx;
2047 int afid;
3f9c7369 2048
d62a17ae 2049 if (!bgp)
2050 return;
2051 afid = afindex(afi, safi);
2052 if (afid >= BGP_AF_MAX)
2053 return;
3f9c7369 2054
d62a17ae 2055 memset(&wctx, 0, sizeof(wctx));
2056 wctx.cb = cb;
2057 wctx.context = ctx;
0de4848d 2058
d62a17ae 2059 if (bgp->update_groups[afid])
2060 hash_walk(bgp->update_groups[afid], update_group_walkcb, &wctx);
3f9c7369
DS
2061}
2062
d62a17ae 2063void update_group_walk(struct bgp *bgp, updgrp_walkcb cb, void *ctx)
3f9c7369 2064{
d62a17ae 2065 afi_t afi;
2066 safi_t safi;
3f9c7369 2067
a2addae8 2068 FOREACH_AFI_SAFI (afi, safi) {
d62a17ae 2069 update_group_af_walk(bgp, afi, safi, cb, ctx);
2070 }
3f9c7369
DS
2071}
2072
d62a17ae 2073void update_group_periodic_merge(struct bgp *bgp)
3f9c7369 2074{
d62a17ae 2075 char reason[] = "periodic merge check";
3f9c7369 2076
d62a17ae 2077 update_group_walk(bgp, update_group_periodic_merge_walkcb,
2078 (void *)reason);
3f9c7369
DS
2079}
2080
0de4848d
DS
2081static int
2082update_group_default_originate_route_map_walkcb(struct update_group *updgrp,
d62a17ae 2083 void *arg)
0de4848d 2084{
d62a17ae 2085 struct update_subgroup *subgrp;
2086 struct peer *peer;
2087 afi_t afi;
2088 safi_t safi;
2089
a2addae8 2090 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
d62a17ae 2091 peer = SUBGRP_PEER(subgrp);
2092 afi = SUBGRP_AFI(subgrp);
2093 safi = SUBGRP_SAFI(subgrp);
2094
2095 if (peer->default_rmap[afi][safi].name) {
a9ae9fb5
AR
2096 /*
2097 * When there is change in routemap this flow will
2098 * be triggered. We need to unset the Flag to ensure
2099 * the update flow gets triggered.
2100 */
2101 UNSET_FLAG(subgrp->sflags,
2102 SUBGRP_STATUS_DEFAULT_ORIGINATE);
d62a17ae 2103 subgroup_default_originate(subgrp, 0);
2104 }
2105 }
2106
2107 return UPDWALK_CONTINUE;
0de4848d
DS
2108}
2109
e6685141 2110void update_group_refresh_default_originate_route_map(struct event *thread)
0de4848d 2111{
d62a17ae 2112 struct bgp *bgp;
2113 char reason[] = "refresh default-originate route-map";
0de4848d 2114
e16d030c 2115 bgp = EVENT_ARG(thread);
d62a17ae 2116 update_group_walk(bgp, update_group_default_originate_route_map_walkcb,
2117 reason);
e16d030c 2118 EVENT_OFF(bgp->t_rmap_def_originate_eval);
d62a17ae 2119 bgp_unlock(bgp);
0de4848d
DS
2120}
2121
3f9c7369
DS
2122/*
2123 * peer_af_announce_route
2124 *
2125 * Refreshes routes out to a peer_af immediately.
2126 *
2951a7a4 2127 * If the combine parameter is true, then this function will try to
3f9c7369
DS
2128 * gather other peers in the subgroup for which a route announcement
2129 * is pending and efficently announce routes to all of them.
2130 *
2131 * For now, the 'combine' option has an effect only if all peers in
2132 * the subgroup have a route announcement pending.
2133 */
d62a17ae 2134void peer_af_announce_route(struct peer_af *paf, int combine)
3f9c7369 2135{
d62a17ae 2136 struct update_subgroup *subgrp;
2137 struct peer_af *cur_paf;
2138 int all_pending;
2139
2140 subgrp = paf->subgroup;
2141 all_pending = 0;
2142
2143 if (combine) {
2144 /*
2145 * If there are other peers in the old subgroup that also need
2146 * routes to be announced, pull them into the peer's new
2147 * subgroup.
2148 * Combine route announcement with other peers if possible.
2149 *
2150 * For now, we combine only if all peers in the subgroup have an
2151 * announcement pending.
2152 */
2153 all_pending = 1;
2154
a2addae8 2155 SUBGRP_FOREACH_PEER (subgrp, cur_paf) {
d62a17ae 2156 if (cur_paf == paf)
2157 continue;
2158
2159 if (cur_paf->t_announce_route)
2160 continue;
2161
2162 all_pending = 0;
2163 break;
2164 }
2165 }
2166 /*
2167 * Announce to the peer alone if we were not asked to combine peers,
2168 * or if some peers don't have a route annoucement pending.
2169 */
2170 if (!combine || !all_pending) {
2171 update_subgroup_split_peer(paf, NULL);
7bfdba54 2172 subgrp = paf->subgroup;
d62a17ae 2173
7bfdba54 2174 assert(subgrp && subgrp->update_group);
d62a17ae 2175 if (bgp_debug_update(paf->peer, NULL, subgrp->update_group, 0))
6cde4b45 2176 zlog_debug("u%" PRIu64 ":s%" PRIu64" %s announcing routes",
d62a17ae 2177 subgrp->update_group->id, subgrp->id,
2178 paf->peer->host);
2179
2180 subgroup_announce_route(paf->subgroup);
2181 return;
3f9c7369 2182 }
3f9c7369 2183
d62a17ae 2184 /*
2185 * We will announce routes the entire subgroup.
2186 *
2187 * First stop refresh timers on all the other peers.
2188 */
a2addae8 2189 SUBGRP_FOREACH_PEER (subgrp, cur_paf) {
d62a17ae 2190 if (cur_paf == paf)
2191 continue;
3f9c7369 2192
d62a17ae 2193 bgp_stop_announce_route_timer(cur_paf);
2194 }
3f9c7369 2195
d62a17ae 2196 if (bgp_debug_update(paf->peer, NULL, subgrp->update_group, 0))
6cde4b45 2197 zlog_debug("u%" PRIu64 ":s%" PRIu64" announcing routes to %s, combined into %d peers",
d62a17ae 2198 subgrp->update_group->id, subgrp->id,
2199 paf->peer->host, subgrp->peer_count);
3f9c7369 2200
d62a17ae 2201 subgroup_announce_route(subgrp);
3f9c7369 2202
d62a17ae 2203 SUBGRP_INCR_STAT_BY(subgrp, peer_refreshes_combined,
2204 subgrp->peer_count - 1);
3f9c7369
DS
2205}
2206
2fc102e1
QY
2207void subgroup_trigger_write(struct update_subgroup *subgrp)
2208{
2209 struct peer_af *paf;
2210
becedef6
QY
2211 /*
2212 * For each peer in the subgroup, schedule a job to pull packets from
2213 * the subgroup output queue into their own output queue. This action
2214 * will trigger a write job on the I/O thread.
2215 */
996c9314 2216 SUBGRP_FOREACH_PEER (subgrp, paf)
feb17238 2217 if (peer_established(paf->peer))
907a2395 2218 event_add_timer_msec(
996c9314
LB
2219 bm->master, bgp_generate_updgrp_packets,
2220 paf->peer, 0,
2221 &paf->peer->t_generate_updgrp_packets);
2fc102e1
QY
2222}
2223
d62a17ae 2224int update_group_clear_update_dbg(struct update_group *updgrp, void *arg)
3f9c7369 2225{
d62a17ae 2226 UPDGRP_PEER_DBG_OFF(updgrp);
2227 return UPDWALK_CONTINUE;
3f9c7369 2228}
adbac85e 2229
06370dac 2230/* Return true if we should addpath encode NLRI to this peer */
be92fc9f 2231bool bgp_addpath_encode_tx(struct peer *peer, afi_t afi, safi_t safi)
adbac85e 2232{
d62a17ae 2233 return (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_TX_ADV)
2234 && CHECK_FLAG(peer->af_cap[afi][safi],
2235 PEER_CAP_ADDPATH_AF_RX_RCV));
adbac85e 2236}
b1dd7180 2237
8ccee4b8
DA
2238bool bgp_addpath_capable(struct bgp_path_info *bpi, struct peer *peer,
2239 afi_t afi, safi_t safi)
2240{
2241 return (bgp_addpath_tx_path(peer->addpath_type[afi][safi], bpi) ||
2242 (safi == SAFI_LABELED_UNICAST &&
2243 bgp_addpath_tx_path(peer->addpath_type[afi][SAFI_UNICAST],
2244 bpi)));
2245}
2246
b1dd7180
DA
2247bool bgp_check_selected(struct bgp_path_info *bpi, struct peer *peer,
2248 bool addpath_capable, afi_t afi, safi_t safi)
2249{
2250 return (CHECK_FLAG(bpi->flags, BGP_PATH_SELECTED) ||
8ccee4b8 2251 (addpath_capable && bgp_addpath_capable(bpi, peer, afi, safi)));
b1dd7180 2252}