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