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