]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_updgrp.c
Merge pull request #11932 from opensourcerouting/fix/memory_leak_bgp_soo_ecommunity_dup
[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 if (peer->soo[afi][safi]) {
440 char *soo_str = ecommunity_str(peer->soo[afi][safi]);
441
442 key = jhash_1word(jhash(soo_str, strlen(soo_str), SEED1), key);
443 }
444
445 if (bgp_debug_neighbor_events(peer)) {
446 zlog_debug(
447 "%pBP Update Group Hash: sort: %d UpdGrpFlags: %ju UpdGrpAFFlags: %ju",
448 peer, peer->sort,
449 (intmax_t)CHECK_FLAG(peer->flags, PEER_UPDGRP_FLAGS),
450 (intmax_t)CHECK_FLAG(flags, PEER_UPDGRP_AF_FLAGS));
451 zlog_debug(
452 "%pBP Update Group Hash: addpath: %u UpdGrpCapFlag: %u UpdGrpCapAFFlag: %u route_adv: %u change local as: %u",
453 peer, (uint32_t)peer->addpath_type[afi][safi],
454 CHECK_FLAG(peer->cap, PEER_UPDGRP_CAP_FLAGS),
455 CHECK_FLAG(peer->af_cap[afi][safi],
456 PEER_UPDGRP_AF_CAP_FLAGS),
457 peer->v_routeadv, peer->change_local_as);
458 zlog_debug(
459 "%pBP Update Group Hash: max packet size: %u pmax_out: %u Peer Group: %s rmap out: %s",
460 peer, peer->max_packet_size, peer->pmax_out[afi][safi],
461 peer->group ? peer->group->name : "(NONE)",
462 ROUTE_MAP_OUT_NAME(filter) ? ROUTE_MAP_OUT_NAME(filter)
463 : "(NONE)");
464 zlog_debug(
465 "%pBP Update Group Hash: dlist out: %s plist out: %s aslist out: %s usmap out: %s advmap: %s",
466 peer,
467 DISTRIBUTE_OUT_NAME(filter)
468 ? DISTRIBUTE_OUT_NAME(filter)
469 : "(NONE)",
470 PREFIX_LIST_OUT_NAME(filter)
471 ? PREFIX_LIST_OUT_NAME(filter)
472 : "(NONE)",
473 FILTER_LIST_OUT_NAME(filter)
474 ? FILTER_LIST_OUT_NAME(filter)
475 : "(NONE)",
476 UNSUPPRESS_MAP_NAME(filter)
477 ? UNSUPPRESS_MAP_NAME(filter)
478 : "(NONE)",
479 ADVERTISE_MAP_NAME(filter) ? ADVERTISE_MAP_NAME(filter)
480 : "(NONE)");
481 zlog_debug(
482 "%pBP Update Group Hash: default rmap: %s shared network and afi active network: %d",
483 peer,
484 peer->default_rmap[afi][safi].name
485 ? peer->default_rmap[afi][safi].name
486 : "(NONE)",
487 peer->shared_network &&
488 peer_afi_active_nego(peer, AFI_IP6));
489 zlog_debug(
490 "%pBP Update Group Hash: Lonesoul: %d ORF prefix: %u ORF old: %u max prefix out: %ju",
491 peer, !!CHECK_FLAG(peer->flags, PEER_FLAG_LONESOUL),
492 CHECK_FLAG(peer->af_cap[afi][safi],
493 PEER_CAP_ORF_PREFIX_SM_RCV),
494 CHECK_FLAG(peer->af_cap[afi][safi],
495 PEER_CAP_ORF_PREFIX_SM_OLD_RCV),
496 (intmax_t)CHECK_FLAG(peer->af_flags[afi][safi],
497 PEER_FLAG_MAX_PREFIX_OUT));
498 zlog_debug("%pBP Update Group Hash key: %u", peer, key);
499 }
500 return key;
501 }
502
503 static bool updgrp_hash_cmp(const void *p1, const void *p2)
504 {
505 const struct update_group *grp1;
506 const struct update_group *grp2;
507 const struct peer *pe1;
508 const struct peer *pe2;
509 uint32_t flags1;
510 uint32_t flags2;
511 const struct bgp_filter *fl1;
512 const struct bgp_filter *fl2;
513 afi_t afi;
514 safi_t safi;
515
516 if (!p1 || !p2)
517 return false;
518
519 grp1 = p1;
520 grp2 = p2;
521 pe1 = grp1->conf;
522 pe2 = grp2->conf;
523 afi = grp1->afi;
524 safi = grp1->safi;
525 flags1 = pe1->af_flags[afi][safi];
526 flags2 = pe2->af_flags[afi][safi];
527 fl1 = &pe1->filter[afi][safi];
528 fl2 = &pe2->filter[afi][safi];
529
530 /* put EBGP and IBGP peers in different update groups */
531 if (pe1->sort != pe2->sort)
532 return false;
533
534 /* check peer flags */
535 if ((pe1->flags & PEER_UPDGRP_FLAGS)
536 != (pe2->flags & PEER_UPDGRP_FLAGS))
537 return false;
538
539 /* If there is 'local-as' configured, it should match. */
540 if (pe1->change_local_as != pe2->change_local_as)
541 return false;
542
543 if (pe1->pmax_out[afi][safi] != pe2->pmax_out[afi][safi])
544 return false;
545
546 /* flags like route reflector client */
547 if ((flags1 & PEER_UPDGRP_AF_FLAGS) != (flags2 & PEER_UPDGRP_AF_FLAGS))
548 return false;
549
550 if (pe1->addpath_type[afi][safi] != pe2->addpath_type[afi][safi])
551 return false;
552
553 if ((pe1->cap & PEER_UPDGRP_CAP_FLAGS)
554 != (pe2->cap & PEER_UPDGRP_CAP_FLAGS))
555 return false;
556
557 if ((pe1->af_cap[afi][safi] & PEER_UPDGRP_AF_CAP_FLAGS)
558 != (pe2->af_cap[afi][safi] & PEER_UPDGRP_AF_CAP_FLAGS))
559 return false;
560
561 if (pe1->v_routeadv != pe2->v_routeadv)
562 return false;
563
564 if (pe1->group != pe2->group)
565 return false;
566
567 /* Roles can affect filtering */
568 if (pe1->local_role != pe2->local_role)
569 return false;
570
571 /* route-map names should be the same */
572 if ((fl1->map[RMAP_OUT].name && !fl2->map[RMAP_OUT].name)
573 || (!fl1->map[RMAP_OUT].name && fl2->map[RMAP_OUT].name)
574 || (fl1->map[RMAP_OUT].name && fl2->map[RMAP_OUT].name
575 && strcmp(fl1->map[RMAP_OUT].name, fl2->map[RMAP_OUT].name)))
576 return false;
577
578 if ((fl1->dlist[FILTER_OUT].name && !fl2->dlist[FILTER_OUT].name)
579 || (!fl1->dlist[FILTER_OUT].name && fl2->dlist[FILTER_OUT].name)
580 || (fl1->dlist[FILTER_OUT].name && fl2->dlist[FILTER_OUT].name
581 && strcmp(fl1->dlist[FILTER_OUT].name,
582 fl2->dlist[FILTER_OUT].name)))
583 return false;
584
585 if ((fl1->plist[FILTER_OUT].name && !fl2->plist[FILTER_OUT].name)
586 || (!fl1->plist[FILTER_OUT].name && fl2->plist[FILTER_OUT].name)
587 || (fl1->plist[FILTER_OUT].name && fl2->plist[FILTER_OUT].name
588 && strcmp(fl1->plist[FILTER_OUT].name,
589 fl2->plist[FILTER_OUT].name)))
590 return false;
591
592 if ((fl1->aslist[FILTER_OUT].name && !fl2->aslist[FILTER_OUT].name)
593 || (!fl1->aslist[FILTER_OUT].name && fl2->aslist[FILTER_OUT].name)
594 || (fl1->aslist[FILTER_OUT].name && fl2->aslist[FILTER_OUT].name
595 && strcmp(fl1->aslist[FILTER_OUT].name,
596 fl2->aslist[FILTER_OUT].name)))
597 return false;
598
599 if ((fl1->usmap.name && !fl2->usmap.name)
600 || (!fl1->usmap.name && fl2->usmap.name)
601 || (fl1->usmap.name && fl2->usmap.name
602 && strcmp(fl1->usmap.name, fl2->usmap.name)))
603 return false;
604
605 if ((fl1->advmap.aname && !fl2->advmap.aname)
606 || (!fl1->advmap.aname && fl2->advmap.aname)
607 || (fl1->advmap.aname && fl2->advmap.aname
608 && strcmp(fl1->advmap.aname, fl2->advmap.aname)))
609 return false;
610
611 if (fl1->advmap.update_type != fl2->advmap.update_type)
612 return false;
613
614 if ((pe1->default_rmap[afi][safi].name
615 && !pe2->default_rmap[afi][safi].name)
616 || (!pe1->default_rmap[afi][safi].name
617 && pe2->default_rmap[afi][safi].name)
618 || (pe1->default_rmap[afi][safi].name
619 && pe2->default_rmap[afi][safi].name
620 && strcmp(pe1->default_rmap[afi][safi].name,
621 pe2->default_rmap[afi][safi].name)))
622 return false;
623
624 if ((afi == AFI_IP6) && (pe1->shared_network != pe2->shared_network))
625 return false;
626
627 if ((CHECK_FLAG(pe1->flags, PEER_FLAG_LONESOUL)
628 || CHECK_FLAG(pe1->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
629 || CHECK_FLAG(pe1->af_cap[afi][safi],
630 PEER_CAP_ORF_PREFIX_SM_OLD_RCV))
631 && !sockunion_same(&pe1->su, &pe2->su))
632 return false;
633
634 return true;
635 }
636
637 static void peer_lonesoul_or_not(struct peer *peer, int set)
638 {
639 /* no change in status? */
640 if (set == (CHECK_FLAG(peer->flags, PEER_FLAG_LONESOUL) > 0))
641 return;
642
643 if (set)
644 SET_FLAG(peer->flags, PEER_FLAG_LONESOUL);
645 else
646 UNSET_FLAG(peer->flags, PEER_FLAG_LONESOUL);
647
648 update_group_adjust_peer_afs(peer);
649 }
650
651 /*
652 * subgroup_total_packets_enqueued
653 *
654 * Returns the total number of packets enqueued to a subgroup.
655 */
656 static unsigned int
657 subgroup_total_packets_enqueued(struct update_subgroup *subgrp)
658 {
659 struct bpacket *pkt;
660
661 pkt = bpacket_queue_last(SUBGRP_PKTQ(subgrp));
662
663 return pkt->ver - 1;
664 }
665
666 static int update_group_show_walkcb(struct update_group *updgrp, void *arg)
667 {
668 struct updwalk_context *ctx = arg;
669 struct vty *vty;
670 struct update_subgroup *subgrp;
671 struct peer_af *paf;
672 struct bgp_filter *filter;
673 struct peer *peer = UPDGRP_PEER(updgrp);
674 int match = 0;
675
676 if (!ctx)
677 return CMD_SUCCESS;
678
679 if (ctx->subgrp_id) {
680 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
681 if (ctx->subgrp_id && (ctx->subgrp_id != subgrp->id))
682 continue;
683 else {
684 match = 1;
685 break;
686 }
687 }
688 } else {
689 match = 1;
690 }
691
692 if (!match) {
693 /* Since this routine is invoked from a walk, we cannot signal
694 * any */
695 /* error here, can only return. */
696 return CMD_SUCCESS;
697 }
698
699 vty = ctx->vty;
700
701 vty_out(vty, "Update-group %" PRIu64 ":\n", updgrp->id);
702 vty_out(vty, " Created: %s", timestamp_string(updgrp->uptime));
703 filter = &updgrp->conf->filter[updgrp->afi][updgrp->safi];
704 if (filter->map[RMAP_OUT].name)
705 vty_out(vty, " Outgoing route map: %s\n",
706 filter->map[RMAP_OUT].name);
707 vty_out(vty, " MRAI value (seconds): %d\n", updgrp->conf->v_routeadv);
708 if (updgrp->conf->change_local_as)
709 vty_out(vty, " Local AS %u%s%s\n",
710 updgrp->conf->change_local_as,
711 CHECK_FLAG(updgrp->conf->flags,
712 PEER_FLAG_LOCAL_AS_NO_PREPEND)
713 ? " no-prepend"
714 : "",
715 CHECK_FLAG(updgrp->conf->flags,
716 PEER_FLAG_LOCAL_AS_REPLACE_AS)
717 ? " replace-as"
718 : "");
719
720 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
721 if (ctx->subgrp_id && (ctx->subgrp_id != subgrp->id))
722 continue;
723 vty_out(vty, "\n");
724 vty_out(vty, " Update-subgroup %" PRIu64 ":\n", subgrp->id);
725 vty_out(vty, " Created: %s",
726 timestamp_string(subgrp->uptime));
727
728 if (subgrp->split_from.update_group_id
729 || subgrp->split_from.subgroup_id) {
730 vty_out(vty, " Split from group id: %" PRIu64 "\n",
731 subgrp->split_from.update_group_id);
732 vty_out(vty,
733 " Split from subgroup id: %" PRIu64 "\n",
734 subgrp->split_from.subgroup_id);
735 }
736
737 vty_out(vty, " Join events: %u\n", subgrp->join_events);
738 vty_out(vty, " Prune events: %u\n", subgrp->prune_events);
739 vty_out(vty, " Merge events: %u\n", subgrp->merge_events);
740 vty_out(vty, " Split events: %u\n", subgrp->split_events);
741 vty_out(vty, " Update group switch events: %u\n",
742 subgrp->updgrp_switch_events);
743 vty_out(vty, " Peer refreshes combined: %u\n",
744 subgrp->peer_refreshes_combined);
745 vty_out(vty, " Merge checks triggered: %u\n",
746 subgrp->merge_checks_triggered);
747 vty_out(vty, " Coalesce Time: %u%s\n",
748 (UPDGRP_INST(subgrp->update_group))->coalesce_time,
749 subgrp->t_coalesce ? "(Running)" : "");
750 vty_out(vty, " Version: %" PRIu64 "\n", subgrp->version);
751 vty_out(vty, " Packet queue length: %d\n",
752 bpacket_queue_length(SUBGRP_PKTQ(subgrp)));
753 vty_out(vty, " Total packets enqueued: %u\n",
754 subgroup_total_packets_enqueued(subgrp));
755 vty_out(vty, " Packet queue high watermark: %d\n",
756 bpacket_queue_hwm_length(SUBGRP_PKTQ(subgrp)));
757 vty_out(vty, " Adj-out list count: %u\n", subgrp->adj_count);
758 vty_out(vty, " Advertise list: %s\n",
759 advertise_list_is_empty(subgrp) ? "empty"
760 : "not empty");
761 vty_out(vty, " Flags: %s\n",
762 CHECK_FLAG(subgrp->flags, SUBGRP_FLAG_NEEDS_REFRESH)
763 ? "R"
764 : "");
765 if (peer)
766 vty_out(vty, " Max packet size: %d\n",
767 peer->max_packet_size);
768 if (subgrp->peer_count > 0) {
769 vty_out(vty, " Peers:\n");
770 SUBGRP_FOREACH_PEER (subgrp, paf)
771 vty_out(vty, " - %s\n", paf->peer->host);
772 }
773 }
774 return UPDWALK_CONTINUE;
775 }
776
777 /*
778 * Helper function to show the packet queue for each subgroup of update group.
779 * Will be constrained to a particular subgroup id if id !=0
780 */
781 static int updgrp_show_packet_queue_walkcb(struct update_group *updgrp,
782 void *arg)
783 {
784 struct updwalk_context *ctx = arg;
785 struct update_subgroup *subgrp;
786 struct vty *vty;
787
788 vty = ctx->vty;
789 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
790 if (ctx->subgrp_id && (ctx->subgrp_id != subgrp->id))
791 continue;
792 vty_out(vty, "update group %" PRIu64 ", subgroup %" PRIu64 "\n",
793 updgrp->id, subgrp->id);
794 bpacket_queue_show_vty(SUBGRP_PKTQ(subgrp), vty);
795 }
796 return UPDWALK_CONTINUE;
797 }
798
799 /*
800 * Show the packet queue for each subgroup of update group. Will be
801 * constrained to a particular subgroup id if id !=0
802 */
803 void update_group_show_packet_queue(struct bgp *bgp, afi_t afi, safi_t safi,
804 struct vty *vty, uint64_t id)
805 {
806 struct updwalk_context ctx;
807
808 memset(&ctx, 0, sizeof(ctx));
809 ctx.vty = vty;
810 ctx.subgrp_id = id;
811 ctx.flags = 0;
812 update_group_af_walk(bgp, afi, safi, updgrp_show_packet_queue_walkcb,
813 &ctx);
814 }
815
816 static struct update_group *update_group_find(struct peer_af *paf)
817 {
818 struct update_group *updgrp;
819 struct update_group tmp;
820 struct peer tmp_conf;
821
822 if (!peer_established(PAF_PEER(paf)))
823 return NULL;
824
825 memset(&tmp, 0, sizeof(tmp));
826 memset(&tmp_conf, 0, sizeof(tmp_conf));
827 tmp.conf = &tmp_conf;
828 peer2_updgrp_copy(&tmp, paf);
829
830 updgrp = hash_lookup(paf->peer->bgp->update_groups[paf->afid], &tmp);
831 conf_release(&tmp_conf, paf->afi, paf->safi);
832 return updgrp;
833 }
834
835 static struct update_group *update_group_create(struct peer_af *paf)
836 {
837 struct update_group *updgrp;
838 struct update_group tmp;
839 struct peer tmp_conf;
840
841 memset(&tmp, 0, sizeof(tmp));
842 memset(&tmp_conf, 0, sizeof(tmp_conf));
843 tmp.conf = &tmp_conf;
844 peer2_updgrp_copy(&tmp, paf);
845
846 updgrp = hash_get(paf->peer->bgp->update_groups[paf->afid], &tmp,
847 updgrp_hash_alloc);
848 update_group_checkin(updgrp);
849
850 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
851 zlog_debug("create update group %" PRIu64, updgrp->id);
852
853 UPDGRP_GLOBAL_STAT(updgrp, updgrps_created) += 1;
854
855 conf_release(&tmp_conf, paf->afi, paf->safi);
856 return updgrp;
857 }
858
859 static void update_group_delete(struct update_group *updgrp)
860 {
861 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
862 zlog_debug("delete update group %" PRIu64, updgrp->id);
863
864 UPDGRP_GLOBAL_STAT(updgrp, updgrps_deleted) += 1;
865
866 hash_release(updgrp->bgp->update_groups[updgrp->afid], updgrp);
867 conf_release(updgrp->conf, updgrp->afi, updgrp->safi);
868
869 XFREE(MTYPE_BGP_PEER_HOST, updgrp->conf->host);
870
871 XFREE(MTYPE_BGP_PEER_IFNAME, updgrp->conf->ifname);
872
873 XFREE(MTYPE_BGP_PEER, updgrp->conf);
874 XFREE(MTYPE_BGP_UPDGRP, updgrp);
875 }
876
877 static void update_group_add_subgroup(struct update_group *updgrp,
878 struct update_subgroup *subgrp)
879 {
880 if (!updgrp || !subgrp)
881 return;
882
883 LIST_INSERT_HEAD(&(updgrp->subgrps), subgrp, updgrp_train);
884 subgrp->update_group = updgrp;
885 }
886
887 static void update_group_remove_subgroup(struct update_group *updgrp,
888 struct update_subgroup *subgrp)
889 {
890 if (!updgrp || !subgrp)
891 return;
892
893 LIST_REMOVE(subgrp, updgrp_train);
894 subgrp->update_group = NULL;
895 if (LIST_EMPTY(&(updgrp->subgrps)))
896 update_group_delete(updgrp);
897 }
898
899 static struct update_subgroup *
900 update_subgroup_create(struct update_group *updgrp)
901 {
902 struct update_subgroup *subgrp;
903
904 subgrp = XCALLOC(MTYPE_BGP_UPD_SUBGRP, sizeof(struct update_subgroup));
905 update_subgroup_checkin(subgrp, updgrp);
906 subgrp->v_coalesce = (UPDGRP_INST(updgrp))->coalesce_time;
907 sync_init(subgrp, updgrp);
908 bpacket_queue_init(SUBGRP_PKTQ(subgrp));
909 bpacket_queue_add(SUBGRP_PKTQ(subgrp), NULL, NULL);
910 TAILQ_INIT(&(subgrp->adjq));
911 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
912 zlog_debug("create subgroup u%" PRIu64 ":s%" PRIu64, updgrp->id,
913 subgrp->id);
914
915 update_group_add_subgroup(updgrp, subgrp);
916
917 UPDGRP_INCR_STAT(updgrp, subgrps_created);
918
919 return subgrp;
920 }
921
922 static void update_subgroup_delete(struct update_subgroup *subgrp)
923 {
924 if (!subgrp)
925 return;
926
927 if (subgrp->update_group)
928 UPDGRP_INCR_STAT(subgrp->update_group, subgrps_deleted);
929
930 THREAD_OFF(subgrp->t_merge_check);
931 THREAD_OFF(subgrp->t_coalesce);
932
933 bpacket_queue_cleanup(SUBGRP_PKTQ(subgrp));
934 subgroup_clear_table(subgrp);
935
936 sync_delete(subgrp);
937
938 if (BGP_DEBUG(update_groups, UPDATE_GROUPS) && subgrp->update_group)
939 zlog_debug("delete subgroup u%" PRIu64 ":s%" PRIu64,
940 subgrp->update_group->id, subgrp->id);
941
942 update_group_remove_subgroup(subgrp->update_group, subgrp);
943
944 XFREE(MTYPE_BGP_UPD_SUBGRP, subgrp);
945 }
946
947 void update_subgroup_inherit_info(struct update_subgroup *to,
948 struct update_subgroup *from)
949 {
950 if (!to || !from)
951 return;
952
953 to->sflags = from->sflags;
954 }
955
956 /*
957 * update_subgroup_check_delete
958 *
959 * Delete a subgroup if it is ready to be deleted.
960 *
961 * Returns true if the subgroup was deleted.
962 */
963 static bool update_subgroup_check_delete(struct update_subgroup *subgrp)
964 {
965 if (!subgrp)
966 return false;
967
968 if (!LIST_EMPTY(&(subgrp->peers)))
969 return false;
970
971 update_subgroup_delete(subgrp);
972
973 return true;
974 }
975
976 /*
977 * update_subgroup_add_peer
978 *
979 * @param send_enqueued_packets If true all currently enqueued packets will
980 * also be sent to the peer.
981 */
982 static void update_subgroup_add_peer(struct update_subgroup *subgrp,
983 struct peer_af *paf,
984 int send_enqueued_pkts)
985 {
986 struct bpacket *pkt;
987
988 if (!subgrp || !paf)
989 return;
990
991 LIST_INSERT_HEAD(&(subgrp->peers), paf, subgrp_train);
992 paf->subgroup = subgrp;
993 subgrp->peer_count++;
994
995 if (bgp_debug_peer_updout_enabled(paf->peer->host)) {
996 UPDGRP_PEER_DBG_EN(subgrp->update_group);
997 }
998
999 SUBGRP_INCR_STAT(subgrp, join_events);
1000
1001 if (send_enqueued_pkts) {
1002 pkt = bpacket_queue_first(SUBGRP_PKTQ(subgrp));
1003 } else {
1004
1005 /*
1006 * Hang the peer off of the last, placeholder, packet in the
1007 * queue. This means it won't see any of the packets that are
1008 * currently the queue.
1009 */
1010 pkt = bpacket_queue_last(SUBGRP_PKTQ(subgrp));
1011 assert(pkt->buffer == NULL);
1012 }
1013
1014 bpacket_add_peer(pkt, paf);
1015
1016 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
1017 zlog_debug("peer %s added to subgroup s%" PRIu64,
1018 paf->peer->host, subgrp->id);
1019 }
1020
1021 /*
1022 * update_subgroup_remove_peer_internal
1023 *
1024 * Internal function that removes a peer from a subgroup, but does not
1025 * delete the subgroup. A call to this function must almost always be
1026 * followed by a call to update_subgroup_check_delete().
1027 *
1028 * @see update_subgroup_remove_peer
1029 */
1030 static void update_subgroup_remove_peer_internal(struct update_subgroup *subgrp,
1031 struct peer_af *paf)
1032 {
1033 assert(subgrp && paf && subgrp->update_group);
1034
1035 if (bgp_debug_peer_updout_enabled(paf->peer->host)) {
1036 UPDGRP_PEER_DBG_DIS(subgrp->update_group);
1037 }
1038
1039 bpacket_queue_remove_peer(paf);
1040 LIST_REMOVE(paf, subgrp_train);
1041 paf->subgroup = NULL;
1042 subgrp->peer_count--;
1043
1044 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
1045 zlog_debug("peer %s deleted from subgroup s%"
1046 PRIu64 " peer cnt %d",
1047 paf->peer->host, subgrp->id, subgrp->peer_count);
1048 SUBGRP_INCR_STAT(subgrp, prune_events);
1049 }
1050
1051 /*
1052 * update_subgroup_remove_peer
1053 */
1054 void update_subgroup_remove_peer(struct update_subgroup *subgrp,
1055 struct peer_af *paf)
1056 {
1057 if (!subgrp || !paf)
1058 return;
1059
1060 update_subgroup_remove_peer_internal(subgrp, paf);
1061
1062 if (update_subgroup_check_delete(subgrp))
1063 return;
1064
1065 /*
1066 * The deletion of the peer may have caused some packets to be
1067 * deleted from the subgroup packet queue. Check if the subgroup can
1068 * be merged now.
1069 */
1070 update_subgroup_check_merge(subgrp, "removed peer from subgroup");
1071 }
1072
1073 static struct update_subgroup *update_subgroup_find(struct update_group *updgrp,
1074 struct peer_af *paf)
1075 {
1076 struct update_subgroup *subgrp = NULL;
1077 uint64_t version;
1078
1079 if (paf->subgroup) {
1080 assert(0);
1081 return NULL;
1082 } else
1083 version = 0;
1084
1085 if (!peer_established(PAF_PEER(paf)))
1086 return NULL;
1087
1088 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
1089 if (subgrp->version != version
1090 || CHECK_FLAG(subgrp->sflags,
1091 SUBGRP_STATUS_DEFAULT_ORIGINATE))
1092 continue;
1093
1094 /*
1095 * The version number is not meaningful on a subgroup that needs
1096 * a refresh.
1097 */
1098 if (update_subgroup_needs_refresh(subgrp))
1099 continue;
1100
1101 break;
1102 }
1103
1104 return subgrp;
1105 }
1106
1107 /*
1108 * update_subgroup_ready_for_merge
1109 *
1110 * Returns true if this subgroup is in a state that allows it to be
1111 * merged into another subgroup.
1112 */
1113 static bool update_subgroup_ready_for_merge(struct update_subgroup *subgrp)
1114 {
1115
1116 /*
1117 * Not ready if there are any encoded packets waiting to be written
1118 * out to peers.
1119 */
1120 if (!bpacket_queue_is_empty(SUBGRP_PKTQ(subgrp)))
1121 return false;
1122
1123 /*
1124 * Not ready if there enqueued updates waiting to be encoded.
1125 */
1126 if (!advertise_list_is_empty(subgrp))
1127 return false;
1128
1129 /*
1130 * Don't attempt to merge a subgroup that needs a refresh. For one,
1131 * we can't determine if the adj_out of such a group matches that of
1132 * another group.
1133 */
1134 if (update_subgroup_needs_refresh(subgrp))
1135 return false;
1136
1137 return true;
1138 }
1139
1140 /*
1141 * update_subgrp_can_merge_into
1142 *
1143 * Returns true if the first subgroup can merge into the second
1144 * subgroup.
1145 */
1146 static int update_subgroup_can_merge_into(struct update_subgroup *subgrp,
1147 struct update_subgroup *target)
1148 {
1149
1150 if (subgrp == target)
1151 return 0;
1152
1153 /*
1154 * Both must have processed the BRIB to the same point in order to
1155 * be merged.
1156 */
1157 if (subgrp->version != target->version)
1158 return 0;
1159
1160 if (CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE)
1161 != CHECK_FLAG(target->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE))
1162 return 0;
1163
1164 if (subgrp->adj_count != target->adj_count)
1165 return 0;
1166
1167 return update_subgroup_ready_for_merge(target);
1168 }
1169
1170 /*
1171 * update_subgroup_merge
1172 *
1173 * Merge the first subgroup into the second one.
1174 */
1175 static void update_subgroup_merge(struct update_subgroup *subgrp,
1176 struct update_subgroup *target,
1177 const char *reason)
1178 {
1179 struct peer_af *paf;
1180 int result;
1181 int peer_count;
1182
1183 assert(subgrp->adj_count == target->adj_count);
1184
1185 peer_count = subgrp->peer_count;
1186
1187 while (1) {
1188 paf = LIST_FIRST(&subgrp->peers);
1189 if (!paf)
1190 break;
1191
1192 update_subgroup_remove_peer_internal(subgrp, paf);
1193
1194 /*
1195 * Add the peer to the target subgroup, while making sure that
1196 * any currently enqueued packets won't be sent to it. Enqueued
1197 * packets could, for example, result in an unnecessary withdraw
1198 * followed by an advertise.
1199 */
1200 update_subgroup_add_peer(target, paf, 0);
1201 }
1202
1203 SUBGRP_INCR_STAT(target, merge_events);
1204
1205 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
1206 zlog_debug("u%" PRIu64 ":s%" PRIu64" (%d peers) merged into u%" PRIu64 ":s%" PRIu64", trigger: %s",
1207 subgrp->update_group->id, subgrp->id, peer_count,
1208 target->update_group->id, target->id,
1209 reason ? reason : "unknown");
1210
1211 result = update_subgroup_check_delete(subgrp);
1212 assert(result);
1213 }
1214
1215 /*
1216 * update_subgroup_check_merge
1217 *
1218 * Merge this subgroup into another subgroup if possible.
1219 *
1220 * Returns true if the subgroup has been merged. The subgroup pointer
1221 * should not be accessed in this case.
1222 */
1223 bool update_subgroup_check_merge(struct update_subgroup *subgrp,
1224 const char *reason)
1225 {
1226 struct update_subgroup *target;
1227
1228 if (!update_subgroup_ready_for_merge(subgrp))
1229 return false;
1230
1231 /*
1232 * Look for a subgroup to merge into.
1233 */
1234 UPDGRP_FOREACH_SUBGRP (subgrp->update_group, target) {
1235 if (update_subgroup_can_merge_into(subgrp, target))
1236 break;
1237 }
1238
1239 if (!target)
1240 return false;
1241
1242 update_subgroup_merge(subgrp, target, reason);
1243 return true;
1244 }
1245
1246 /*
1247 * update_subgroup_merge_check_thread_cb
1248 */
1249 static void update_subgroup_merge_check_thread_cb(struct thread *thread)
1250 {
1251 struct update_subgroup *subgrp;
1252
1253 subgrp = THREAD_ARG(thread);
1254
1255 subgrp->t_merge_check = NULL;
1256
1257 update_subgroup_check_merge(subgrp, "triggered merge check");
1258 }
1259
1260 /*
1261 * update_subgroup_trigger_merge_check
1262 *
1263 * Triggers a call to update_subgroup_check_merge() on a clean context.
1264 *
1265 * @param force If true, the merge check will be triggered even if the
1266 * subgroup doesn't currently look ready for a merge.
1267 *
1268 * Returns true if a merge check will be performed shortly.
1269 */
1270 bool update_subgroup_trigger_merge_check(struct update_subgroup *subgrp,
1271 int force)
1272 {
1273 if (subgrp->t_merge_check)
1274 return true;
1275
1276 if (!force && !update_subgroup_ready_for_merge(subgrp))
1277 return false;
1278
1279 subgrp->t_merge_check = NULL;
1280 thread_add_timer_msec(bm->master, update_subgroup_merge_check_thread_cb,
1281 subgrp, 0, &subgrp->t_merge_check);
1282
1283 SUBGRP_INCR_STAT(subgrp, merge_checks_triggered);
1284
1285 return true;
1286 }
1287
1288 /*
1289 * update_subgroup_copy_adj_out
1290 *
1291 * Helper function that clones the adj out (state about advertised
1292 * routes) from one subgroup to another. It assumes that the adj out
1293 * of the target subgroup is empty.
1294 */
1295 static void update_subgroup_copy_adj_out(struct update_subgroup *source,
1296 struct update_subgroup *dest)
1297 {
1298 struct bgp_adj_out *aout, *aout_copy;
1299
1300 SUBGRP_FOREACH_ADJ (source, aout) {
1301 /*
1302 * Copy the adj out.
1303 */
1304 aout_copy = bgp_adj_out_alloc(dest, aout->dest,
1305 aout->addpath_tx_id);
1306 aout_copy->attr =
1307 aout->attr ? bgp_attr_intern(aout->attr) : NULL;
1308 }
1309
1310 dest->scount = source->scount;
1311 }
1312
1313 /*
1314 * update_subgroup_copy_packets
1315 *
1316 * Copy packets after and including the given packet to the subgroup
1317 * 'dest'.
1318 *
1319 * Returns the number of packets copied.
1320 */
1321 static int update_subgroup_copy_packets(struct update_subgroup *dest,
1322 struct bpacket *pkt)
1323 {
1324 int count;
1325
1326 count = 0;
1327 while (pkt && pkt->buffer) {
1328 bpacket_queue_add(SUBGRP_PKTQ(dest), stream_dup(pkt->buffer),
1329 &pkt->arr);
1330 count++;
1331 pkt = bpacket_next(pkt);
1332 }
1333
1334 return count;
1335 }
1336
1337 static bool updgrp_prefix_list_update(struct update_group *updgrp,
1338 const char *name)
1339 {
1340 struct peer *peer;
1341 struct bgp_filter *filter;
1342
1343 peer = UPDGRP_PEER(updgrp);
1344 filter = &peer->filter[UPDGRP_AFI(updgrp)][UPDGRP_SAFI(updgrp)];
1345
1346 if (PREFIX_LIST_OUT_NAME(filter)
1347 && (strcmp(name, PREFIX_LIST_OUT_NAME(filter)) == 0)) {
1348 PREFIX_LIST_OUT(filter) = prefix_list_lookup(
1349 UPDGRP_AFI(updgrp), PREFIX_LIST_OUT_NAME(filter));
1350 return true;
1351 }
1352 return false;
1353 }
1354
1355 static bool updgrp_filter_list_update(struct update_group *updgrp,
1356 const char *name)
1357 {
1358 struct peer *peer;
1359 struct bgp_filter *filter;
1360
1361 peer = UPDGRP_PEER(updgrp);
1362 filter = &peer->filter[UPDGRP_AFI(updgrp)][UPDGRP_SAFI(updgrp)];
1363
1364 if (FILTER_LIST_OUT_NAME(filter)
1365 && (strcmp(name, FILTER_LIST_OUT_NAME(filter)) == 0)) {
1366 FILTER_LIST_OUT(filter) =
1367 as_list_lookup(FILTER_LIST_OUT_NAME(filter));
1368 return true;
1369 }
1370 return false;
1371 }
1372
1373 static bool updgrp_distribute_list_update(struct update_group *updgrp,
1374 const char *name)
1375 {
1376 struct peer *peer;
1377 struct bgp_filter *filter;
1378
1379 peer = UPDGRP_PEER(updgrp);
1380 filter = &peer->filter[UPDGRP_AFI(updgrp)][UPDGRP_SAFI(updgrp)];
1381
1382 if (DISTRIBUTE_OUT_NAME(filter)
1383 && (strcmp(name, DISTRIBUTE_OUT_NAME(filter)) == 0)) {
1384 DISTRIBUTE_OUT(filter) = access_list_lookup(
1385 UPDGRP_AFI(updgrp), DISTRIBUTE_OUT_NAME(filter));
1386 return true;
1387 }
1388 return false;
1389 }
1390
1391 static int updgrp_route_map_update(struct update_group *updgrp,
1392 const char *name, int *def_rmap_changed)
1393 {
1394 struct peer *peer;
1395 struct bgp_filter *filter;
1396 int changed = 0;
1397 afi_t afi;
1398 safi_t safi;
1399
1400 peer = UPDGRP_PEER(updgrp);
1401 afi = UPDGRP_AFI(updgrp);
1402 safi = UPDGRP_SAFI(updgrp);
1403 filter = &peer->filter[afi][safi];
1404
1405 if (ROUTE_MAP_OUT_NAME(filter)
1406 && (strcmp(name, ROUTE_MAP_OUT_NAME(filter)) == 0)) {
1407 ROUTE_MAP_OUT(filter) = route_map_lookup_by_name(name);
1408
1409 changed = 1;
1410 }
1411
1412 if (UNSUPPRESS_MAP_NAME(filter)
1413 && (strcmp(name, UNSUPPRESS_MAP_NAME(filter)) == 0)) {
1414 UNSUPPRESS_MAP(filter) = route_map_lookup_by_name(name);
1415 changed = 1;
1416 }
1417
1418 /* process default-originate route-map */
1419 if (peer->default_rmap[afi][safi].name
1420 && (strcmp(name, peer->default_rmap[afi][safi].name) == 0)) {
1421 peer->default_rmap[afi][safi].map =
1422 route_map_lookup_by_name(name);
1423 if (def_rmap_changed)
1424 *def_rmap_changed = 1;
1425 }
1426 return changed;
1427 }
1428
1429 /*
1430 * hash iteration callback function to process a policy change for an
1431 * update group. Check if the changed policy matches the updgrp's
1432 * outbound route-map or unsuppress-map or default-originate map or
1433 * filter-list or prefix-list or distribute-list.
1434 * Trigger update generation accordingly.
1435 */
1436 static int updgrp_policy_update_walkcb(struct update_group *updgrp, void *arg)
1437 {
1438 struct updwalk_context *ctx = arg;
1439 struct update_subgroup *subgrp;
1440 int changed = 0;
1441 int def_changed = 0;
1442
1443 if (!updgrp || !ctx || !ctx->policy_name)
1444 return UPDWALK_CONTINUE;
1445
1446 switch (ctx->policy_type) {
1447 case BGP_POLICY_ROUTE_MAP:
1448 changed = updgrp_route_map_update(updgrp, ctx->policy_name,
1449 &def_changed);
1450 break;
1451 case BGP_POLICY_FILTER_LIST:
1452 changed = updgrp_filter_list_update(updgrp, ctx->policy_name);
1453 break;
1454 case BGP_POLICY_PREFIX_LIST:
1455 changed = updgrp_prefix_list_update(updgrp, ctx->policy_name);
1456 break;
1457 case BGP_POLICY_DISTRIBUTE_LIST:
1458 changed =
1459 updgrp_distribute_list_update(updgrp, ctx->policy_name);
1460 break;
1461 default:
1462 break;
1463 }
1464
1465 /* If not doing route update, return after updating "config" */
1466 if (!ctx->policy_route_update)
1467 return UPDWALK_CONTINUE;
1468
1469 /* If nothing has changed, return after updating "config" */
1470 if (!changed && !def_changed)
1471 return UPDWALK_CONTINUE;
1472
1473 /*
1474 * If something has changed, at the beginning of a route-map
1475 * modification
1476 * event, mark each subgroup's needs-refresh bit. For one, it signals to
1477 * whoever that the subgroup needs a refresh. Second, it prevents
1478 * premature
1479 * merge of this subgroup with another before a complete (outbound)
1480 * refresh.
1481 */
1482 if (ctx->policy_event_start_flag) {
1483 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
1484 update_subgroup_set_needs_refresh(subgrp, 1);
1485 }
1486 return UPDWALK_CONTINUE;
1487 }
1488
1489 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
1490 /* Avoid supressing duplicate routes later
1491 * when processing in subgroup_announce_table().
1492 */
1493 SET_FLAG(subgrp->sflags, SUBGRP_STATUS_FORCE_UPDATES);
1494
1495 if (changed) {
1496 if (bgp_debug_update(NULL, NULL, updgrp, 0))
1497 zlog_debug(
1498 "u%" PRIu64 ":s%" PRIu64" announcing routes upon policy %s (type %d) change",
1499 updgrp->id, subgrp->id,
1500 ctx->policy_name, ctx->policy_type);
1501 subgroup_announce_route(subgrp);
1502 }
1503 if (def_changed) {
1504 if (bgp_debug_update(NULL, NULL, updgrp, 0))
1505 zlog_debug(
1506 "u%" PRIu64 ":s%" PRIu64" announcing default upon default routemap %s change",
1507 updgrp->id, subgrp->id,
1508 ctx->policy_name);
1509 if (route_map_lookup_by_name(ctx->policy_name)) {
1510 /*
1511 * When there is change in routemap, this flow
1512 * is triggered. the routemap is still present
1513 * in lib, hence its a update flow. The flag
1514 * needs to be unset.
1515 */
1516 UNSET_FLAG(subgrp->sflags,
1517 SUBGRP_STATUS_DEFAULT_ORIGINATE);
1518 subgroup_default_originate(subgrp, 0);
1519 } else {
1520 /*
1521 * This is a explicit withdraw, since the
1522 * routemap is not present in routemap lib. need
1523 * to pass 1 for withdraw arg.
1524 */
1525 subgroup_default_originate(subgrp, 1);
1526 }
1527 }
1528 update_subgroup_set_needs_refresh(subgrp, 0);
1529 }
1530 return UPDWALK_CONTINUE;
1531 }
1532
1533 static int update_group_walkcb(struct hash_bucket *bucket, void *arg)
1534 {
1535 struct update_group *updgrp = bucket->data;
1536 struct updwalk_context *wctx = arg;
1537 int ret = (*wctx->cb)(updgrp, wctx->context);
1538 return ret;
1539 }
1540
1541 static int update_group_periodic_merge_walkcb(struct update_group *updgrp,
1542 void *arg)
1543 {
1544 struct update_subgroup *subgrp;
1545 struct update_subgroup *tmp_subgrp;
1546 const char *reason = arg;
1547
1548 UPDGRP_FOREACH_SUBGRP_SAFE (updgrp, subgrp, tmp_subgrp)
1549 update_subgroup_check_merge(subgrp, reason);
1550 return UPDWALK_CONTINUE;
1551 }
1552
1553 /********************
1554 * PUBLIC FUNCTIONS
1555 ********************/
1556
1557 /*
1558 * trigger function when a policy (route-map/filter-list/prefix-list/
1559 * distribute-list etc.) content changes. Go through all the
1560 * update groups and process the change.
1561 *
1562 * bgp: the bgp instance
1563 * ptype: the type of policy that got modified, see bgpd.h
1564 * pname: name of the policy
1565 * route_update: flag to control if an automatic update generation should
1566 * occur
1567 * start_event: flag that indicates if it's the beginning of the change.
1568 * Esp. when the user is changing the content interactively
1569 * over multiple statements. Useful to set dirty flag on
1570 * update groups.
1571 */
1572 void update_group_policy_update(struct bgp *bgp, enum bgp_policy_type ptype,
1573 const char *pname, bool route_update,
1574 int start_event)
1575 {
1576 struct updwalk_context ctx;
1577
1578 memset(&ctx, 0, sizeof(ctx));
1579 ctx.policy_type = ptype;
1580 ctx.policy_name = pname;
1581 ctx.policy_route_update = route_update;
1582 ctx.policy_event_start_flag = start_event;
1583 ctx.flags = 0;
1584
1585 update_group_walk(bgp, updgrp_policy_update_walkcb, &ctx);
1586 }
1587
1588 /*
1589 * update_subgroup_split_peer
1590 *
1591 * Ensure that the given peer is in a subgroup of its own in the
1592 * specified update group.
1593 */
1594 void update_subgroup_split_peer(struct peer_af *paf,
1595 struct update_group *updgrp)
1596 {
1597 struct update_subgroup *old_subgrp, *subgrp;
1598 uint64_t old_id;
1599
1600
1601 old_subgrp = paf->subgroup;
1602
1603 if (!updgrp)
1604 updgrp = old_subgrp->update_group;
1605
1606 /*
1607 * If the peer is alone in its subgroup, reuse the existing
1608 * subgroup.
1609 */
1610 if (old_subgrp->peer_count == 1) {
1611 if (updgrp == old_subgrp->update_group)
1612 return;
1613
1614 subgrp = old_subgrp;
1615 old_id = old_subgrp->update_group->id;
1616
1617 if (bgp_debug_peer_updout_enabled(paf->peer->host)) {
1618 UPDGRP_PEER_DBG_DIS(old_subgrp->update_group);
1619 }
1620
1621 update_group_remove_subgroup(old_subgrp->update_group,
1622 old_subgrp);
1623 update_group_add_subgroup(updgrp, subgrp);
1624
1625 if (bgp_debug_peer_updout_enabled(paf->peer->host)) {
1626 UPDGRP_PEER_DBG_EN(updgrp);
1627 }
1628 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
1629 zlog_debug("u%" PRIu64 ":s%" PRIu64" peer %s moved to u%" PRIu64 ":s%" PRIu64,
1630 old_id, subgrp->id, paf->peer->host,
1631 updgrp->id, subgrp->id);
1632
1633 /*
1634 * The state of the subgroup (adj_out, advs, packet queue etc)
1635 * is consistent internally, but may not be identical to other
1636 * subgroups in the new update group even if the version number
1637 * matches up. Make sure a full refresh is done before the
1638 * subgroup is merged with another.
1639 */
1640 update_subgroup_set_needs_refresh(subgrp, 1);
1641
1642 SUBGRP_INCR_STAT(subgrp, updgrp_switch_events);
1643 return;
1644 }
1645
1646 /*
1647 * Create a new subgroup under the specified update group, and copy
1648 * over relevant state to it.
1649 */
1650 subgrp = update_subgroup_create(updgrp);
1651 update_subgroup_inherit_info(subgrp, old_subgrp);
1652
1653 subgrp->split_from.update_group_id = old_subgrp->update_group->id;
1654 subgrp->split_from.subgroup_id = old_subgrp->id;
1655
1656 /*
1657 * Copy out relevant state from the old subgroup.
1658 */
1659 update_subgroup_copy_adj_out(paf->subgroup, subgrp);
1660 update_subgroup_copy_packets(subgrp, paf->next_pkt_to_send);
1661
1662 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
1663 zlog_debug("u%" PRIu64 ":s%" PRIu64" peer %s split and moved into u%" PRIu64":s%" PRIu64,
1664 paf->subgroup->update_group->id, paf->subgroup->id,
1665 paf->peer->host, updgrp->id, subgrp->id);
1666
1667 SUBGRP_INCR_STAT(paf->subgroup, split_events);
1668
1669 /*
1670 * Since queued advs were left behind, this new subgroup needs a
1671 * refresh.
1672 */
1673 update_subgroup_set_needs_refresh(subgrp, 1);
1674
1675 /*
1676 * Remove peer from old subgroup, and add it to the new one.
1677 */
1678 update_subgroup_remove_peer(paf->subgroup, paf);
1679
1680 update_subgroup_add_peer(subgrp, paf, 1);
1681 }
1682
1683 void update_bgp_group_init(struct bgp *bgp)
1684 {
1685 int afid;
1686
1687 AF_FOREACH (afid)
1688 bgp->update_groups[afid] =
1689 hash_create(updgrp_hash_key_make, updgrp_hash_cmp,
1690 "BGP Update Group Hash");
1691 }
1692
1693 void update_bgp_group_free(struct bgp *bgp)
1694 {
1695 int afid;
1696
1697 AF_FOREACH (afid) {
1698 if (bgp->update_groups[afid]) {
1699 hash_free(bgp->update_groups[afid]);
1700 bgp->update_groups[afid] = NULL;
1701 }
1702 }
1703 }
1704
1705 void update_group_show(struct bgp *bgp, afi_t afi, safi_t safi, struct vty *vty,
1706 uint64_t subgrp_id)
1707 {
1708 struct updwalk_context ctx;
1709 memset(&ctx, 0, sizeof(ctx));
1710 ctx.vty = vty;
1711 ctx.subgrp_id = subgrp_id;
1712
1713 update_group_af_walk(bgp, afi, safi, update_group_show_walkcb, &ctx);
1714 }
1715
1716 /*
1717 * update_group_show_stats
1718 *
1719 * Show global statistics about update groups.
1720 */
1721 void update_group_show_stats(struct bgp *bgp, struct vty *vty)
1722 {
1723 vty_out(vty, "Update groups created: %u\n",
1724 bgp->update_group_stats.updgrps_created);
1725 vty_out(vty, "Update groups deleted: %u\n",
1726 bgp->update_group_stats.updgrps_deleted);
1727 vty_out(vty, "Update subgroups created: %u\n",
1728 bgp->update_group_stats.subgrps_created);
1729 vty_out(vty, "Update subgroups deleted: %u\n",
1730 bgp->update_group_stats.subgrps_deleted);
1731 vty_out(vty, "Join events: %u\n", bgp->update_group_stats.join_events);
1732 vty_out(vty, "Prune events: %u\n",
1733 bgp->update_group_stats.prune_events);
1734 vty_out(vty, "Merge events: %u\n",
1735 bgp->update_group_stats.merge_events);
1736 vty_out(vty, "Split events: %u\n",
1737 bgp->update_group_stats.split_events);
1738 vty_out(vty, "Update group switch events: %u\n",
1739 bgp->update_group_stats.updgrp_switch_events);
1740 vty_out(vty, "Peer route refreshes combined: %u\n",
1741 bgp->update_group_stats.peer_refreshes_combined);
1742 vty_out(vty, "Merge checks triggered: %u\n",
1743 bgp->update_group_stats.merge_checks_triggered);
1744 }
1745
1746 /*
1747 * update_group_adjust_peer
1748 */
1749 void update_group_adjust_peer(struct peer_af *paf)
1750 {
1751 struct update_group *updgrp;
1752 struct update_subgroup *subgrp, *old_subgrp;
1753 struct peer *peer;
1754
1755 if (!paf)
1756 return;
1757
1758 peer = PAF_PEER(paf);
1759 if (!peer_established(peer)) {
1760 return;
1761 }
1762
1763 if (!CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE)) {
1764 return;
1765 }
1766
1767 if (!peer->afc_nego[paf->afi][paf->safi]) {
1768 return;
1769 }
1770
1771 updgrp = update_group_find(paf);
1772 if (!updgrp) {
1773 updgrp = update_group_create(paf);
1774 if (!updgrp) {
1775 flog_err(EC_BGP_UPDGRP_CREATE,
1776 "couldn't create update group for peer %s",
1777 paf->peer->host);
1778 return;
1779 }
1780 }
1781
1782 old_subgrp = paf->subgroup;
1783
1784 if (old_subgrp) {
1785
1786 /*
1787 * If the update group of the peer is unchanged, the peer can
1788 * stay
1789 * in its existing subgroup and we're done.
1790 */
1791 if (old_subgrp->update_group == updgrp)
1792 return;
1793
1794 /*
1795 * The peer is switching between update groups. Put it in its
1796 * own subgroup under the new update group.
1797 */
1798 update_subgroup_split_peer(paf, updgrp);
1799 return;
1800 }
1801
1802 subgrp = update_subgroup_find(updgrp, paf);
1803 if (!subgrp) {
1804 subgrp = update_subgroup_create(updgrp);
1805 if (!subgrp)
1806 return;
1807 }
1808
1809 update_subgroup_add_peer(subgrp, paf, 1);
1810 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
1811 zlog_debug("u%" PRIu64 ":s%" PRIu64 " add peer %s", updgrp->id,
1812 subgrp->id, paf->peer->host);
1813
1814 return;
1815 }
1816
1817 int update_group_adjust_soloness(struct peer *peer, int set)
1818 {
1819 struct peer_group *group;
1820 struct listnode *node, *nnode;
1821
1822 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
1823 peer_lonesoul_or_not(peer, set);
1824 if (peer_established(peer))
1825 bgp_announce_route_all(peer);
1826 } else {
1827 group = peer->group;
1828 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
1829 peer_lonesoul_or_not(peer, set);
1830 if (peer_established(peer))
1831 bgp_announce_route_all(peer);
1832 }
1833 }
1834 return 0;
1835 }
1836
1837 /*
1838 * update_subgroup_rib
1839 */
1840 struct bgp_table *update_subgroup_rib(struct update_subgroup *subgrp)
1841 {
1842 struct bgp *bgp;
1843
1844 bgp = SUBGRP_INST(subgrp);
1845 if (!bgp)
1846 return NULL;
1847
1848 return bgp->rib[SUBGRP_AFI(subgrp)][SUBGRP_SAFI(subgrp)];
1849 }
1850
1851 void update_group_af_walk(struct bgp *bgp, afi_t afi, safi_t safi,
1852 updgrp_walkcb cb, void *ctx)
1853 {
1854 struct updwalk_context wctx;
1855 int afid;
1856
1857 if (!bgp)
1858 return;
1859 afid = afindex(afi, safi);
1860 if (afid >= BGP_AF_MAX)
1861 return;
1862
1863 memset(&wctx, 0, sizeof(wctx));
1864 wctx.cb = cb;
1865 wctx.context = ctx;
1866
1867 if (bgp->update_groups[afid])
1868 hash_walk(bgp->update_groups[afid], update_group_walkcb, &wctx);
1869 }
1870
1871 void update_group_walk(struct bgp *bgp, updgrp_walkcb cb, void *ctx)
1872 {
1873 afi_t afi;
1874 safi_t safi;
1875
1876 FOREACH_AFI_SAFI (afi, safi) {
1877 update_group_af_walk(bgp, afi, safi, cb, ctx);
1878 }
1879 }
1880
1881 void update_group_periodic_merge(struct bgp *bgp)
1882 {
1883 char reason[] = "periodic merge check";
1884
1885 update_group_walk(bgp, update_group_periodic_merge_walkcb,
1886 (void *)reason);
1887 }
1888
1889 static int
1890 update_group_default_originate_route_map_walkcb(struct update_group *updgrp,
1891 void *arg)
1892 {
1893 struct update_subgroup *subgrp;
1894 struct peer *peer;
1895 afi_t afi;
1896 safi_t safi;
1897
1898 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
1899 peer = SUBGRP_PEER(subgrp);
1900 afi = SUBGRP_AFI(subgrp);
1901 safi = SUBGRP_SAFI(subgrp);
1902
1903 if (peer->default_rmap[afi][safi].name) {
1904 /*
1905 * When there is change in routemap this flow will
1906 * be triggered. We need to unset the Flag to ensure
1907 * the update flow gets triggered.
1908 */
1909 UNSET_FLAG(subgrp->sflags,
1910 SUBGRP_STATUS_DEFAULT_ORIGINATE);
1911 subgroup_default_originate(subgrp, 0);
1912 }
1913 }
1914
1915 return UPDWALK_CONTINUE;
1916 }
1917
1918 void update_group_refresh_default_originate_route_map(struct thread *thread)
1919 {
1920 struct bgp *bgp;
1921 char reason[] = "refresh default-originate route-map";
1922
1923 bgp = THREAD_ARG(thread);
1924 update_group_walk(bgp, update_group_default_originate_route_map_walkcb,
1925 reason);
1926 THREAD_OFF(bgp->t_rmap_def_originate_eval);
1927 bgp_unlock(bgp);
1928 }
1929
1930 /*
1931 * peer_af_announce_route
1932 *
1933 * Refreshes routes out to a peer_af immediately.
1934 *
1935 * If the combine parameter is true, then this function will try to
1936 * gather other peers in the subgroup for which a route announcement
1937 * is pending and efficently announce routes to all of them.
1938 *
1939 * For now, the 'combine' option has an effect only if all peers in
1940 * the subgroup have a route announcement pending.
1941 */
1942 void peer_af_announce_route(struct peer_af *paf, int combine)
1943 {
1944 struct update_subgroup *subgrp;
1945 struct peer_af *cur_paf;
1946 int all_pending;
1947
1948 subgrp = paf->subgroup;
1949 all_pending = 0;
1950
1951 if (combine) {
1952 /*
1953 * If there are other peers in the old subgroup that also need
1954 * routes to be announced, pull them into the peer's new
1955 * subgroup.
1956 * Combine route announcement with other peers if possible.
1957 *
1958 * For now, we combine only if all peers in the subgroup have an
1959 * announcement pending.
1960 */
1961 all_pending = 1;
1962
1963 SUBGRP_FOREACH_PEER (subgrp, cur_paf) {
1964 if (cur_paf == paf)
1965 continue;
1966
1967 if (cur_paf->t_announce_route)
1968 continue;
1969
1970 all_pending = 0;
1971 break;
1972 }
1973 }
1974 /*
1975 * Announce to the peer alone if we were not asked to combine peers,
1976 * or if some peers don't have a route annoucement pending.
1977 */
1978 if (!combine || !all_pending) {
1979 update_subgroup_split_peer(paf, NULL);
1980 subgrp = paf->subgroup;
1981
1982 assert(subgrp && subgrp->update_group);
1983 if (bgp_debug_update(paf->peer, NULL, subgrp->update_group, 0))
1984 zlog_debug("u%" PRIu64 ":s%" PRIu64" %s announcing routes",
1985 subgrp->update_group->id, subgrp->id,
1986 paf->peer->host);
1987
1988 subgroup_announce_route(paf->subgroup);
1989 return;
1990 }
1991
1992 /*
1993 * We will announce routes the entire subgroup.
1994 *
1995 * First stop refresh timers on all the other peers.
1996 */
1997 SUBGRP_FOREACH_PEER (subgrp, cur_paf) {
1998 if (cur_paf == paf)
1999 continue;
2000
2001 bgp_stop_announce_route_timer(cur_paf);
2002 }
2003
2004 if (bgp_debug_update(paf->peer, NULL, subgrp->update_group, 0))
2005 zlog_debug("u%" PRIu64 ":s%" PRIu64" announcing routes to %s, combined into %d peers",
2006 subgrp->update_group->id, subgrp->id,
2007 paf->peer->host, subgrp->peer_count);
2008
2009 subgroup_announce_route(subgrp);
2010
2011 SUBGRP_INCR_STAT_BY(subgrp, peer_refreshes_combined,
2012 subgrp->peer_count - 1);
2013 }
2014
2015 void subgroup_trigger_write(struct update_subgroup *subgrp)
2016 {
2017 struct peer_af *paf;
2018
2019 /*
2020 * For each peer in the subgroup, schedule a job to pull packets from
2021 * the subgroup output queue into their own output queue. This action
2022 * will trigger a write job on the I/O thread.
2023 */
2024 SUBGRP_FOREACH_PEER (subgrp, paf)
2025 if (peer_established(paf->peer))
2026 thread_add_timer_msec(
2027 bm->master, bgp_generate_updgrp_packets,
2028 paf->peer, 0,
2029 &paf->peer->t_generate_updgrp_packets);
2030 }
2031
2032 int update_group_clear_update_dbg(struct update_group *updgrp, void *arg)
2033 {
2034 UPDGRP_PEER_DBG_OFF(updgrp);
2035 return UPDWALK_CONTINUE;
2036 }
2037
2038 /* Return true if we should addpath encode NLRI to this peer */
2039 bool bgp_addpath_encode_tx(struct peer *peer, afi_t afi, safi_t safi)
2040 {
2041 return (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_TX_ADV)
2042 && CHECK_FLAG(peer->af_cap[afi][safi],
2043 PEER_CAP_ADDPATH_AF_RX_RCV));
2044 }
2045
2046 bool bgp_check_selected(struct bgp_path_info *bpi, struct peer *peer,
2047 bool addpath_capable, afi_t afi, safi_t safi)
2048 {
2049 return (CHECK_FLAG(bpi->flags, BGP_PATH_SELECTED) ||
2050 (addpath_capable &&
2051 bgp_addpath_tx_path(peer->addpath_type[afi][safi], bpi)));
2052 }