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