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