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