]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_updgrp.c
*: update hash_create(), hash_create_size()
[mirror_frr.git] / bgpd / bgp_updgrp.c
CommitLineData
3f9c7369
DS
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 *
896014f4
DL
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
3f9c7369
DS
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"
3f9c7369
DS
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 */
67static void
68update_group_checkin (struct update_group *updgrp)
69{
70 updgrp->id = ++bm->updgrp_idspace;
71 updgrp->uptime = bgp_clock ();
72}
73
74static void
75update_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
82static void
83sync_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);
dfd19ccc 90 subgrp->hash = hash_create (baa_hash_key, baa_hash_cmp, NULL);
3f9c7369
DS
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
107static void
108sync_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 */
129static void
130conf_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;
3f9c7369
DS
141 dst->v_routeadv = src->v_routeadv;
142 dst->flags = src->flags;
143 dst->af_flags[afi][safi] = src->af_flags[afi][safi];
6e919709
DS
144 if (dst->host)
145 XFREE(MTYPE_BGP_PEER_HOST, dst->host);
146
147 dst->host = XSTRDUP(MTYPE_BGP_PEER_HOST, src->host);
3f9c7369
DS
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];
40d2700d 151 dst->orf_plist[afi][safi] = src->orf_plist[afi][safi];
3f9c7369
DS
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 =
6e919709 162 XSTRDUP(MTYPE_ROUTE_MAP_NAME, src->default_rmap[afi][safi].name);
3f9c7369
DS
163 dst->default_rmap[afi][safi].map = src->default_rmap[afi][safi].map;
164 }
165
166 if (DISTRIBUTE_OUT_NAME(srcfilter))
167 {
6e919709 168 DISTRIBUTE_OUT_NAME(dstfilter) = XSTRDUP(MTYPE_BGP_FILTER_NAME, DISTRIBUTE_OUT_NAME(srcfilter));
3f9c7369
DS
169 DISTRIBUTE_OUT(dstfilter) = DISTRIBUTE_OUT(srcfilter);
170 }
171
172 if (PREFIX_LIST_OUT_NAME(srcfilter))
173 {
6e919709 174 PREFIX_LIST_OUT_NAME(dstfilter) = XSTRDUP(MTYPE_BGP_FILTER_NAME, PREFIX_LIST_OUT_NAME(srcfilter));
3f9c7369
DS
175 PREFIX_LIST_OUT(dstfilter) = PREFIX_LIST_OUT(srcfilter);
176 }
177
178 if (FILTER_LIST_OUT_NAME(srcfilter))
179 {
6e919709 180 FILTER_LIST_OUT_NAME(dstfilter) = XSTRDUP(MTYPE_BGP_FILTER_NAME, FILTER_LIST_OUT_NAME(srcfilter));
3f9c7369
DS
181 FILTER_LIST_OUT(dstfilter) = FILTER_LIST_OUT(srcfilter);
182 }
183
184 if (ROUTE_MAP_OUT_NAME(srcfilter))
185 {
6e919709 186 ROUTE_MAP_OUT_NAME(dstfilter) = XSTRDUP(MTYPE_BGP_FILTER_NAME, ROUTE_MAP_OUT_NAME(srcfilter));
3f9c7369
DS
187 ROUTE_MAP_OUT(dstfilter) = ROUTE_MAP_OUT(srcfilter);
188 }
189
190 if (UNSUPPRESS_MAP_NAME(srcfilter))
191 {
6e919709 192 UNSUPPRESS_MAP_NAME(dstfilter) = XSTRDUP(MTYPE_BGP_FILTER_NAME, UNSUPPRESS_MAP_NAME(srcfilter));
3f9c7369
DS
193 UNSUPPRESS_MAP(dstfilter) = UNSUPPRESS_MAP(srcfilter);
194 }
195}
196
197/**
6e919709 198 * since we did a bunch of XSTRDUP's in conf_copy, time to free them up
3f9c7369
DS
199 */
200static void
201conf_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)
6e919709 208 XFREE(MTYPE_ROUTE_MAP_NAME, src->default_rmap[afi][safi].name);
3f9c7369
DS
209
210 if (srcfilter->dlist[FILTER_OUT].name)
6e919709 211 XFREE(MTYPE_BGP_FILTER_NAME, srcfilter->dlist[FILTER_OUT].name);
3f9c7369
DS
212
213 if (srcfilter->plist[FILTER_OUT].name)
6e919709 214 XFREE(MTYPE_BGP_FILTER_NAME, srcfilter->plist[FILTER_OUT].name);
3f9c7369
DS
215
216 if (srcfilter->aslist[FILTER_OUT].name)
6e919709 217 XFREE(MTYPE_BGP_FILTER_NAME, srcfilter->aslist[FILTER_OUT].name);
3f9c7369
DS
218
219 if (srcfilter->map[RMAP_OUT].name)
6e919709 220 XFREE(MTYPE_BGP_FILTER_NAME, srcfilter->map[RMAP_OUT].name);
3f9c7369
DS
221
222 if (srcfilter->usmap.name)
6e919709 223 XFREE(MTYPE_BGP_FILTER_NAME, srcfilter->usmap.name);
495f0b13
DS
224
225 if (src->host)
226 XFREE(MTYPE_BGP_PEER_HOST, src->host);
227 src->host = NULL;
3f9c7369
DS
228}
229
230static void
231peer2_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 */
258static void *
259updgrp_hash_alloc (void *p)
260{
261 struct update_group *updgrp;
ffd0c037 262 const struct update_group *in;
3f9c7369 263
ffd0c037 264 in = (const struct update_group *)p;
3f9c7369
DS
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 */
298static unsigned int
299updgrp_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 /*
40d2700d
DW
373 * There are certain peers that must get their own update-group:
374 * - lonesoul peers
40d2700d 375 * - peers that negotiated ORF
3f9c7369
DS
376 */
377 if (CHECK_FLAG (peer->flags, PEER_FLAG_LONESOUL) ||
40d2700d 378 CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV) ||
2a3d5731 379 CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_OLD_RCV))
3f9c7369
DS
380 key = jhash_1word (jhash (peer->host, strlen (peer->host), SEED2), key);
381
382 return key;
383}
384
385static int
386updgrp_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) ||
40d2700d 489 CHECK_FLAG (pe1->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV) ||
2a3d5731 490 CHECK_FLAG (pe1->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_OLD_RCV)) &&
3f9c7369
DS
491 !sockunion_same (&pe1->su, &pe2->su))
492 return 0;
493
494 return 1;
495}
496
497static void
498peer_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 */
517static unsigned int
518subgroup_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
527static int
528update_group_show_walkcb (struct update_group *updgrp, void *arg)
529{
8fe8a7f6
DS
530 struct updwalk_context *ctx = arg;
531 struct vty *vty;
3f9c7369
DS
532 struct update_subgroup *subgrp;
533 struct peer_af *paf;
534 struct bgp_filter *filter;
8fe8a7f6
DS
535 int match = 0;
536
537 if (!ctx)
ffd0c037 538 return CMD_SUCCESS;
8fe8a7f6
DS
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;
3f9c7369 566
96ade3ed 567 vty_outln (vty, "Update-group %" PRIu64 ":", updgrp->id);
3f9c7369
DS
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)
96ade3ed 571 vty_outln (vty, " Outgoing route map: %s%s",
3f9c7369 572 filter->map[RMAP_OUT].map ? "X" : "",
96ade3ed
QY
573 filter->map[RMAP_OUT].name);
574 vty_outln (vty, " MRAI value (seconds): %d",
575 updgrp->conf->v_routeadv);
3f9c7369 576 if (updgrp->conf->change_local_as)
96ade3ed 577 vty_outln (vty, " Local AS %u%s%s",
3f9c7369
DS
578 updgrp->conf->change_local_as,
579 CHECK_FLAG (updgrp->conf->flags,
580 PEER_FLAG_LOCAL_AS_NO_PREPEND) ? " no-prepend" : "",
96ade3ed 581 CHECK_FLAG(updgrp->conf->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS) ? " replace-as" : "");
3f9c7369
DS
582
583 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp)
584 {
8fe8a7f6
DS
585 if (ctx->subgrp_id && (ctx->subgrp_id != subgrp->id))
586 continue;
e31b6333 587 vty_out (vty, VTYNL);
96ade3ed 588 vty_outln (vty, " Update-subgroup %" PRIu64 ":", subgrp->id);
3f9c7369
DS
589 vty_out (vty, " Created: %s", timestamp_string (subgrp->uptime));
590
591 if (subgrp->split_from.update_group_id || subgrp->split_from.subgroup_id)
592 {
96ade3ed
QY
593 vty_outln (vty, " Split from group id: %" PRIu64 "",
594 subgrp->split_from.update_group_id);
595 vty_outln (vty, " Split from subgroup id: %" PRIu64 "",
596 subgrp->split_from.subgroup_id);
3f9c7369
DS
597 }
598
96ade3ed
QY
599 vty_outln (vty, " Join events: %u", subgrp->join_events);
600 vty_outln (vty, " Prune events: %u",
601 subgrp->prune_events);
602 vty_outln (vty, " Merge events: %u",
603 subgrp->merge_events);
604 vty_outln (vty, " Split events: %u",
605 subgrp->split_events);
606 vty_outln (vty, " Update group switch events: %u",
607 subgrp->updgrp_switch_events);
608 vty_outln (vty, " Peer refreshes combined: %u",
609 subgrp->peer_refreshes_combined);
610 vty_outln (vty, " Merge checks triggered: %u",
611 subgrp->merge_checks_triggered);
612 vty_outln (vty, " Version: %" PRIu64 "", subgrp->version);
613 vty_outln (vty, " Packet queue length: %d",
614 bpacket_queue_length(SUBGRP_PKTQ(subgrp)));
615 vty_outln (vty, " Total packets enqueued: %u",
616 subgroup_total_packets_enqueued(subgrp));
617 vty_outln (vty, " Packet queue high watermark: %d",
618 bpacket_queue_hwm_length(SUBGRP_PKTQ(subgrp)));
619 vty_outln (vty, " Adj-out list count: %u",
620 subgrp->adj_count);
621 vty_outln (vty, " Advertise list: %s",
622 advertise_list_is_empty(subgrp) ? "empty" : "not empty");
623 vty_outln (vty, " Flags: %s",
624 CHECK_FLAG(subgrp->flags, SUBGRP_FLAG_NEEDS_REFRESH) ? "R" : "");
3f9c7369
DS
625 if (subgrp->peer_count > 0)
626 {
96ade3ed 627 vty_outln (vty, " Peers:");
3f9c7369 628 SUBGRP_FOREACH_PEER (subgrp, paf)
96ade3ed 629 vty_outln (vty, " - %s", paf->peer->host);
3f9c7369
DS
630 }
631 }
632 return UPDWALK_CONTINUE;
633}
634
635/*
636 * Helper function to show the packet queue for each subgroup of update group.
637 * Will be constrained to a particular subgroup id if id !=0
638 */
639static int
640updgrp_show_packet_queue_walkcb (struct update_group *updgrp, void *arg)
641{
642 struct updwalk_context *ctx = arg;
643 struct update_subgroup *subgrp;
644 struct vty *vty;
645
646 vty = ctx->vty;
647 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp)
648 {
649 if (ctx->subgrp_id && (ctx->subgrp_id != subgrp->id))
650 continue;
96ade3ed
QY
651 vty_outln (vty, "update group %" PRIu64 ", subgroup %" PRIu64 "", updgrp->id,
652 subgrp->id);
3f9c7369
DS
653 bpacket_queue_show_vty (SUBGRP_PKTQ (subgrp), vty);
654 }
655 return UPDWALK_CONTINUE;
656}
657
658/*
659 * Show the packet queue for each subgroup of update group. Will be
660 * constrained to a particular subgroup id if id !=0
661 */
662void
663update_group_show_packet_queue (struct bgp *bgp, afi_t afi, safi_t safi,
f43e655e 664 struct vty *vty, uint64_t id)
3f9c7369
DS
665{
666 struct updwalk_context ctx;
667
668 memset (&ctx, 0, sizeof (ctx));
669 ctx.vty = vty;
670 ctx.subgrp_id = id;
671 ctx.flags = 0;
672 update_group_af_walk (bgp, afi, safi, updgrp_show_packet_queue_walkcb,
673 &ctx);
674}
675
676static struct update_group *
677update_group_find (struct peer_af *paf)
678{
679 struct update_group *updgrp;
680 struct update_group tmp;
681 struct peer tmp_conf;
682
683 if (!peer_established (PAF_PEER (paf)))
684 return NULL;
685
686 memset (&tmp, 0, sizeof (tmp));
687 memset (&tmp_conf, 0, sizeof (tmp_conf));
688 tmp.conf = &tmp_conf;
689 peer2_updgrp_copy (&tmp, paf);
690
691 updgrp = hash_lookup (paf->peer->bgp->update_groups[paf->afid], &tmp);
692 conf_release (&tmp_conf, paf->afi, paf->safi);
693 return updgrp;
694}
695
696static struct update_group *
697update_group_create (struct peer_af *paf)
698{
699 struct update_group *updgrp;
700 struct update_group tmp;
701 struct peer tmp_conf;
702
703 memset (&tmp, 0, sizeof (tmp));
704 memset (&tmp_conf, 0, sizeof (tmp_conf));
705 tmp.conf = &tmp_conf;
706 peer2_updgrp_copy (&tmp, paf);
707
708 updgrp = hash_get (paf->peer->bgp->update_groups[paf->afid], &tmp,
709 updgrp_hash_alloc);
710 if (!updgrp)
711 return NULL;
712 update_group_checkin (updgrp);
713
714 if (BGP_DEBUG (update_groups, UPDATE_GROUPS))
ffd0c037 715 zlog_debug ("create update group %" PRIu64, updgrp->id);
3f9c7369
DS
716
717 UPDGRP_GLOBAL_STAT (updgrp, updgrps_created) += 1;
718
495f0b13 719 conf_release(&tmp_conf, paf->afi, paf->safi);
3f9c7369
DS
720 return updgrp;
721}
722
723static void
724update_group_delete (struct update_group *updgrp)
725{
726 if (BGP_DEBUG (update_groups, UPDATE_GROUPS))
ffd0c037 727 zlog_debug ("delete update group %" PRIu64, updgrp->id);
3f9c7369
DS
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);
3d68677e 733
6e919709
DS
734 if (updgrp->conf->host)
735 XFREE(MTYPE_BGP_PEER_HOST, updgrp->conf->host);
736 updgrp->conf->host = NULL;
737
738 if (updgrp->conf->ifname)
739 XFREE(MTYPE_BGP_PEER_IFNAME, updgrp->conf->ifname);
740
3f9c7369
DS
741 XFREE (MTYPE_BGP_PEER, updgrp->conf);
742 XFREE (MTYPE_BGP_UPDGRP, updgrp);
743}
744
745static void
746update_group_add_subgroup (struct update_group *updgrp,
747 struct update_subgroup *subgrp)
748{
749 if (!updgrp || !subgrp)
750 return;
751
752 LIST_INSERT_HEAD (&(updgrp->subgrps), subgrp, updgrp_train);
753 subgrp->update_group = updgrp;
754}
755
756static void
757update_group_remove_subgroup (struct update_group *updgrp,
758 struct update_subgroup *subgrp)
759{
760 if (!updgrp || !subgrp)
761 return;
762
763 LIST_REMOVE (subgrp, updgrp_train);
764 subgrp->update_group = NULL;
765 if (LIST_EMPTY (&(updgrp->subgrps)))
766 update_group_delete (updgrp);
767}
768
769static struct update_subgroup *
770update_subgroup_create (struct update_group *updgrp)
771{
772 struct update_subgroup *subgrp;
773
774 subgrp = XCALLOC (MTYPE_BGP_UPD_SUBGRP, sizeof (struct update_subgroup));
775 update_subgroup_checkin (subgrp, updgrp);
776 subgrp->v_coalesce = (UPDGRP_INST (updgrp))->coalesce_time;
777 sync_init (subgrp);
778 bpacket_queue_init (SUBGRP_PKTQ (subgrp));
779 bpacket_queue_add (SUBGRP_PKTQ (subgrp), NULL, NULL);
780 TAILQ_INIT (&(subgrp->adjq));
781 if (BGP_DEBUG (update_groups, UPDATE_GROUPS))
ffd0c037 782 zlog_debug ("create subgroup u%" PRIu64 ":s%" PRIu64,
3f9c7369
DS
783 updgrp->id, subgrp->id);
784
785 update_group_add_subgroup (updgrp, subgrp);
786
787 UPDGRP_INCR_STAT (updgrp, subgrps_created);
788
789 return subgrp;
790}
791
792static void
793update_subgroup_delete (struct update_subgroup *subgrp)
794{
795 if (!subgrp)
796 return;
797
798 if (subgrp->update_group)
799 UPDGRP_INCR_STAT (subgrp->update_group, subgrps_deleted);
800
801 if (subgrp->t_merge_check)
802 THREAD_OFF (subgrp->t_merge_check);
803
804 if (subgrp->t_coalesce)
805 THREAD_TIMER_OFF (subgrp->t_coalesce);
806
807 bpacket_queue_cleanup (SUBGRP_PKTQ (subgrp));
808 subgroup_clear_table (subgrp);
809
810 if (subgrp->t_coalesce)
811 THREAD_TIMER_OFF (subgrp->t_coalesce);
812 sync_delete (subgrp);
813
814 if (BGP_DEBUG (update_groups, UPDATE_GROUPS))
ffd0c037 815 zlog_debug ("delete subgroup u%" PRIu64 ":s%" PRIu64,
3f9c7369
DS
816 subgrp->update_group->id, subgrp->id);
817
818 update_group_remove_subgroup (subgrp->update_group, subgrp);
819
820 XFREE (MTYPE_BGP_UPD_SUBGRP, subgrp);
821}
822
823void
824update_subgroup_inherit_info (struct update_subgroup *to,
825 struct update_subgroup *from)
826{
827 if (!to || !from)
828 return;
829
830 to->sflags = from->sflags;
831}
832
833/*
834 * update_subgroup_check_delete
835 *
836 * Delete a subgroup if it is ready to be deleted.
837 *
838 * Returns TRUE if the subgroup was deleted.
839 */
840static int
841update_subgroup_check_delete (struct update_subgroup *subgrp)
842{
843 if (!subgrp)
844 return 0;
845
846 if (!LIST_EMPTY (&(subgrp->peers)))
847 return 0;
848
849 update_subgroup_delete (subgrp);
850
851 return 1;
852}
853
854/*
855 * update_subgroup_add_peer
856 *
857 * @param send_enqueued_packets If true all currently enqueued packets will
858 * also be sent to the peer.
859 */
860static void
861update_subgroup_add_peer (struct update_subgroup *subgrp, struct peer_af *paf,
862 int send_enqueued_pkts)
863{
864 struct bpacket *pkt;
865
866 if (!subgrp || !paf)
867 return;
868
869 LIST_INSERT_HEAD (&(subgrp->peers), paf, subgrp_train);
870 paf->subgroup = subgrp;
871 subgrp->peer_count++;
872
ffd0c037 873 if (bgp_debug_peer_updout_enabled(paf->peer->host))
3f9c7369
DS
874 {
875 UPDGRP_PEER_DBG_EN(subgrp->update_group);
876 }
877
878 SUBGRP_INCR_STAT (subgrp, join_events);
879
880 if (send_enqueued_pkts)
881 {
882 pkt = bpacket_queue_first (SUBGRP_PKTQ (subgrp));
883 }
884 else
885 {
886
887 /*
888 * Hang the peer off of the last, placeholder, packet in the
889 * queue. This means it won't see any of the packets that are
890 * currently the queue.
891 */
892 pkt = bpacket_queue_last (SUBGRP_PKTQ (subgrp));
893 assert (pkt->buffer == NULL);
894 }
895
896 bpacket_add_peer (pkt, paf);
897
898 bpacket_queue_sanity_check (SUBGRP_PKTQ (subgrp));
899}
900
901/*
902 * update_subgroup_remove_peer_internal
903 *
904 * Internal function that removes a peer from a subgroup, but does not
905 * delete the subgroup. A call to this function must almost always be
906 * followed by a call to update_subgroup_check_delete().
907 *
908 * @see update_subgroup_remove_peer
909 */
910static void
911update_subgroup_remove_peer_internal (struct update_subgroup *subgrp,
912 struct peer_af *paf)
913{
914 assert (subgrp && paf);
915
ffd0c037 916 if (bgp_debug_peer_updout_enabled(paf->peer->host))
3f9c7369
DS
917 {
918 UPDGRP_PEER_DBG_DIS(subgrp->update_group);
919 }
920
921 bpacket_queue_remove_peer (paf);
922 LIST_REMOVE (paf, subgrp_train);
923 paf->subgroup = NULL;
924 subgrp->peer_count--;
925
926 SUBGRP_INCR_STAT (subgrp, prune_events);
927}
928
929/*
930 * update_subgroup_remove_peer
931 */
932void
933update_subgroup_remove_peer (struct update_subgroup *subgrp,
934 struct peer_af *paf)
935{
936 if (!subgrp || !paf)
937 return;
938
939 update_subgroup_remove_peer_internal (subgrp, paf);
940
941 if (update_subgroup_check_delete (subgrp))
942 return;
943
944 /*
945 * The deletion of the peer may have caused some packets to be
946 * deleted from the subgroup packet queue. Check if the subgroup can
947 * be merged now.
948 */
949 update_subgroup_check_merge (subgrp, "removed peer from subgroup");
950}
951
952static struct update_subgroup *
953update_subgroup_find (struct update_group *updgrp, struct peer_af *paf)
954{
955 struct update_subgroup *subgrp = NULL;
956 uint64_t version;
957
958 if (paf->subgroup)
959 {
960 assert (0);
961 return NULL;
962 }
963 else
964 version = 0;
965
966 if (!peer_established (PAF_PEER (paf)))
967 return NULL;
968
969 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp)
970 {
f910ef58 971 if (subgrp->version != version ||
972 CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE))
3f9c7369
DS
973 continue;
974
975 /*
976 * The version number is not meaningful on a subgroup that needs
977 * a refresh.
978 */
979 if (update_subgroup_needs_refresh (subgrp))
980 continue;
981
982 break;
983 }
984
985 return subgrp;
986}
987
988/*
989 * update_subgroup_ready_for_merge
990 *
991 * Returns TRUE if this subgroup is in a state that allows it to be
992 * merged into another subgroup.
993 */
7717b183 994static int
3f9c7369
DS
995update_subgroup_ready_for_merge (struct update_subgroup *subgrp)
996{
997
998 /*
999 * Not ready if there are any encoded packets waiting to be written
1000 * out to peers.
1001 */
1002 if (!bpacket_queue_is_empty (SUBGRP_PKTQ (subgrp)))
1003 return 0;
1004
1005 /*
1006 * Not ready if there enqueued updates waiting to be encoded.
1007 */
1008 if (!advertise_list_is_empty (subgrp))
1009 return 0;
1010
1011 /*
1012 * Don't attempt to merge a subgroup that needs a refresh. For one,
1013 * we can't determine if the adj_out of such a group matches that of
1014 * another group.
1015 */
1016 if (update_subgroup_needs_refresh (subgrp))
1017 return 0;
1018
1019 return 1;
1020}
1021
1022/*
1023 * update_subgrp_can_merge_into
1024 *
1025 * Returns TRUE if the first subgroup can merge into the second
1026 * subgroup.
1027 */
7717b183 1028static int
3f9c7369
DS
1029update_subgroup_can_merge_into (struct update_subgroup *subgrp,
1030 struct update_subgroup *target)
1031{
1032
1033 if (subgrp == target)
1034 return 0;
1035
1036 /*
1037 * Both must have processed the BRIB to the same point in order to
1038 * be merged.
1039 */
1040 if (subgrp->version != target->version)
1041 return 0;
1042
f910ef58 1043 if (CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE) !=
1044 CHECK_FLAG(target->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE))
1045 return 0;
1046
7717b183 1047 if (subgrp->adj_count != target->adj_count)
3f9c7369
DS
1048 return 0;
1049
7717b183 1050 return update_subgroup_ready_for_merge (target);
3f9c7369
DS
1051}
1052
1053/*
1054 * update_subgroup_merge
1055 *
1056 * Merge the first subgroup into the second one.
1057 */
1058static void
1059update_subgroup_merge (struct update_subgroup *subgrp,
1060 struct update_subgroup *target, const char *reason)
1061{
1062 struct peer_af *paf;
1063 int result;
1064 int peer_count;
1065
1066 assert (subgrp->adj_count == target->adj_count);
1067
1068 peer_count = subgrp->peer_count;
1069
1070 while (1)
1071 {
1072 paf = LIST_FIRST (&subgrp->peers);
1073 if (!paf)
1074 break;
1075
1076 update_subgroup_remove_peer_internal (subgrp, paf);
1077
1078 /*
1079 * Add the peer to the target subgroup, while making sure that
1080 * any currently enqueued packets won't be sent to it. Enqueued
1081 * packets could, for example, result in an unnecessary withdraw
1082 * followed by an advertise.
1083 */
1084 update_subgroup_add_peer (target, paf, 0);
1085 }
1086
1087 SUBGRP_INCR_STAT (target, merge_events);
1088
1089 if (BGP_DEBUG (update_groups, UPDATE_GROUPS))
ffd0c037 1090 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " (%d peers) merged into u%" PRIu64 ":s%" PRIu64 ", "
3f9c7369
DS
1091 "trigger: %s", subgrp->update_group->id, subgrp->id, peer_count,
1092 target->update_group->id, target->id, reason ? reason : "unknown");
1093
1094 result = update_subgroup_check_delete (subgrp);
1095 assert (result);
1096}
1097
1098/*
1099 * update_subgroup_check_merge
1100 *
1101 * Merge this subgroup into another subgroup if possible.
1102 *
1103 * Returns TRUE if the subgroup has been merged. The subgroup pointer
1104 * should not be accessed in this case.
1105 */
1106int
1107update_subgroup_check_merge (struct update_subgroup *subgrp,
1108 const char *reason)
1109{
1110 struct update_subgroup *target;
1111
1112 if (!update_subgroup_ready_for_merge (subgrp))
1113 return 0;
1114
1115 /*
1116 * Look for a subgroup to merge into.
1117 */
1118 UPDGRP_FOREACH_SUBGRP (subgrp->update_group, target)
1119 {
1120 if (update_subgroup_can_merge_into (subgrp, target))
1121 break;
1122 }
1123
1124 if (!target)
1125 return 0;
1126
1127 update_subgroup_merge (subgrp, target, reason);
1128 return 1;
1129}
1130
1131 /*
1132 * update_subgroup_merge_check_thread_cb
1133 */
1134static int
1135update_subgroup_merge_check_thread_cb (struct thread *thread)
1136{
1137 struct update_subgroup *subgrp;
1138
1139 subgrp = THREAD_ARG (thread);
1140
1141 subgrp->t_merge_check = NULL;
1142
1143 update_subgroup_check_merge (subgrp, "triggered merge check");
1144 return 0;
1145}
1146
1147/*
1148 * update_subgroup_trigger_merge_check
1149 *
1150 * Triggers a call to update_subgroup_check_merge() on a clean context.
1151 *
1152 * @param force If true, the merge check will be triggered even if the
1153 * subgroup doesn't currently look ready for a merge.
1154 *
1155 * Returns TRUE if a merge check will be performed shortly.
1156 */
1157int
1158update_subgroup_trigger_merge_check (struct update_subgroup *subgrp,
1159 int force)
1160{
1161 if (subgrp->t_merge_check)
1162 return 1;
1163
1164 if (!force && !update_subgroup_ready_for_merge (subgrp))
1165 return 0;
1166
66e78ae6 1167 subgrp->t_merge_check = NULL;
a587d00b
QY
1168 thread_add_timer_msec (bm->master, update_subgroup_merge_check_thread_cb, subgrp,
1169 0, &subgrp->t_merge_check);
3f9c7369
DS
1170
1171 SUBGRP_INCR_STAT (subgrp, merge_checks_triggered);
1172
1173 return 1;
1174}
1175
1176/*
1177 * update_subgroup_copy_adj_out
1178 *
1179 * Helper function that clones the adj out (state about advertised
1180 * routes) from one subgroup to another. It assumes that the adj out
1181 * of the target subgroup is empty.
1182 */
1183static void
1184update_subgroup_copy_adj_out (struct update_subgroup *source,
1185 struct update_subgroup *dest)
1186{
1187 struct bgp_adj_out *aout, *aout_copy;
1188
1189 SUBGRP_FOREACH_ADJ (source, aout)
1190 {
1191 /*
1192 * Copy the adj out.
1193 */
adbac85e 1194 aout_copy = bgp_adj_out_alloc (dest, aout->rn, aout->addpath_tx_id);
3f9c7369
DS
1195 aout_copy->attr = aout->attr ? bgp_attr_refcount (aout->attr) : NULL;
1196 }
1197}
1198
1199/*
1200 * update_subgroup_copy_packets
1201 *
1202 * Copy packets after and including the given packet to the subgroup
1203 * 'dest'.
1204 *
1205 * Returns the number of packets copied.
1206 */
1207static int
1208update_subgroup_copy_packets (struct update_subgroup *dest,
1209 struct bpacket *pkt)
1210{
1211 int count;
1212
1213 count = 0;
1214 while (pkt && pkt->buffer)
1215 {
1216 bpacket_queue_add (SUBGRP_PKTQ (dest), stream_dup (pkt->buffer),
1217 &pkt->arr);
1218 count++;
1219 pkt = bpacket_next (pkt);
1220 }
1221
1222 bpacket_queue_sanity_check (SUBGRP_PKTQ (dest));
1223
1224 return count;
1225}
1226
1227static int
ffd0c037 1228updgrp_prefix_list_update (struct update_group *updgrp, const char *name)
3f9c7369
DS
1229{
1230 struct peer *peer;
1231 struct bgp_filter *filter;
1232
1233 peer = UPDGRP_PEER (updgrp);
1234 filter = &peer->filter[UPDGRP_AFI(updgrp)][UPDGRP_SAFI(updgrp)];
1235
1236 if (PREFIX_LIST_OUT_NAME(filter) &&
1237 (strcmp (name, PREFIX_LIST_OUT_NAME(filter)) == 0))
1238 {
1239 PREFIX_LIST_OUT(filter) =
1240 prefix_list_lookup (UPDGRP_AFI(updgrp), PREFIX_LIST_OUT_NAME(filter));
1241 return 1;
1242 }
1243 return 0;
1244}
1245
1246static int
ffd0c037 1247updgrp_filter_list_update (struct update_group *updgrp, const char *name)
3f9c7369
DS
1248{
1249 struct peer *peer;
1250 struct bgp_filter *filter;
1251
1252 peer = UPDGRP_PEER (updgrp);
1253 filter = &peer->filter[UPDGRP_AFI(updgrp)][UPDGRP_SAFI(updgrp)];
1254
1255 if (FILTER_LIST_OUT_NAME(filter) &&
1256 (strcmp (name, FILTER_LIST_OUT_NAME(filter)) == 0))
1257 {
1258 FILTER_LIST_OUT(filter) = as_list_lookup (FILTER_LIST_OUT_NAME(filter));
1259 return 1;
1260 }
1261 return 0;
1262}
1263
1264static int
ffd0c037 1265updgrp_distribute_list_update (struct update_group *updgrp, const char *name)
3f9c7369
DS
1266{
1267 struct peer *peer;
1268 struct bgp_filter *filter;
1269
1270 peer = UPDGRP_PEER(updgrp);
1271 filter = &peer->filter[UPDGRP_AFI(updgrp)][UPDGRP_SAFI(updgrp)];
1272
1273 if (DISTRIBUTE_OUT_NAME(filter) &&
1274 (strcmp (name, DISTRIBUTE_OUT_NAME(filter)) == 0))
1275 {
1276 DISTRIBUTE_OUT(filter) = access_list_lookup(UPDGRP_AFI(updgrp),
1277 DISTRIBUTE_OUT_NAME(filter));
1278 return 1;
1279 }
1280 return 0;
1281}
1282
1283static int
ffd0c037 1284updgrp_route_map_update (struct update_group *updgrp, const char *name,
3f9c7369
DS
1285 int *def_rmap_changed)
1286{
1287 struct peer *peer;
1288 struct bgp_filter *filter;
1289 int changed = 0;
1290 afi_t afi;
1291 safi_t safi;
1292
1293 peer = UPDGRP_PEER (updgrp);
1294 afi = UPDGRP_AFI (updgrp);
1295 safi = UPDGRP_SAFI (updgrp);
1296 filter = &peer->filter[afi][safi];
1297
1298 if (ROUTE_MAP_OUT_NAME(filter) &&
1299 (strcmp (name, ROUTE_MAP_OUT_NAME(filter)) == 0))
1300 {
1301 ROUTE_MAP_OUT(filter) = route_map_lookup_by_name (name);
1302
1303 changed = 1;
1304 }
1305
1306 if (UNSUPPRESS_MAP_NAME(filter) &&
1307 (strcmp (name, UNSUPPRESS_MAP_NAME(filter)) == 0))
1308 {
1309 UNSUPPRESS_MAP(filter) = route_map_lookup_by_name (name);
1310 changed = 1;
1311 }
1312
1313 /* process default-originate route-map */
1314 if (peer->default_rmap[afi][safi].name &&
1315 (strcmp (name, peer->default_rmap[afi][safi].name) == 0))
1316 {
1317 peer->default_rmap[afi][safi].map = route_map_lookup_by_name (name);
1318 if (def_rmap_changed)
1319 *def_rmap_changed = 1;
1320 }
1321 return changed;
1322}
1323
1324/*
1325 * hash iteration callback function to process a policy change for an
1326 * update group. Check if the changed policy matches the updgrp's
1327 * outbound route-map or unsuppress-map or default-originate map or
1328 * filter-list or prefix-list or distribute-list.
1329 * Trigger update generation accordingly.
1330 */
1331static int
1332updgrp_policy_update_walkcb (struct update_group *updgrp, void *arg)
1333{
1334 struct updwalk_context *ctx = arg;
1335 struct update_subgroup *subgrp;
1336 int changed = 0;
1337 int def_changed = 0;
1338
1339 if (!updgrp || !ctx || !ctx->policy_name)
1340 return UPDWALK_CONTINUE;
1341
1342 switch (ctx->policy_type) {
1343 case BGP_POLICY_ROUTE_MAP:
1344 changed = updgrp_route_map_update(updgrp, ctx->policy_name, &def_changed);
1345 break;
1346 case BGP_POLICY_FILTER_LIST:
1347 changed = updgrp_filter_list_update(updgrp, ctx->policy_name);
1348 break;
1349 case BGP_POLICY_PREFIX_LIST:
1350 changed = updgrp_prefix_list_update(updgrp, ctx->policy_name);
1351 break;
1352 case BGP_POLICY_DISTRIBUTE_LIST:
1353 changed = updgrp_distribute_list_update(updgrp, ctx->policy_name);
1354 break;
1355 default:
1356 break;
1357 }
1358
1359 /* If not doing route update, return after updating "config" */
1360 if (!ctx->policy_route_update)
1361 return UPDWALK_CONTINUE;
1362
1363 /* If nothing has changed, return after updating "config" */
1364 if (!changed && !def_changed)
1365 return UPDWALK_CONTINUE;
1366
1367 /*
1368 * If something has changed, at the beginning of a route-map modification
1369 * event, mark each subgroup's needs-refresh bit. For one, it signals to
1370 * whoever that the subgroup needs a refresh. Second, it prevents premature
1371 * merge of this subgroup with another before a complete (outbound) refresh.
1372 */
1373 if (ctx->policy_event_start_flag)
1374 {
1375 UPDGRP_FOREACH_SUBGRP(updgrp, subgrp)
1376 {
1377 update_subgroup_set_needs_refresh(subgrp, 1);
1378 }
1379 return UPDWALK_CONTINUE;
1380 }
1381
1382 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp)
1383 {
1384 if (changed)
1385 {
1386 if (bgp_debug_update(NULL, NULL, updgrp, 0))
ffd0c037 1387 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " announcing routes upon policy %s (type %d) change",
3f9c7369
DS
1388 updgrp->id, subgrp->id, ctx->policy_name, ctx->policy_type);
1389 subgroup_announce_route (subgrp);
1390 }
1391 if (def_changed)
1392 {
1393 if (bgp_debug_update(NULL, NULL, updgrp, 0))
ffd0c037 1394 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " announcing default upon default routemap %s change",
3f9c7369
DS
1395 updgrp->id, subgrp->id, ctx->policy_name);
1396 subgroup_default_originate (subgrp, 0);
1397 }
1398 update_subgroup_set_needs_refresh(subgrp, 0);
1399 }
1400 return UPDWALK_CONTINUE;
1401}
1402
1403static int
1404update_group_walkcb (struct hash_backet *backet, void *arg)
1405{
1406 struct update_group *updgrp = backet->data;
1407 struct updwalk_context *wctx = arg;
1408 int ret = (*wctx->cb) (updgrp, wctx->context);
1409 return ret;
1410}
1411
1412static int
1413update_group_periodic_merge_walkcb (struct update_group *updgrp, void *arg)
1414{
1415 struct update_subgroup *subgrp;
1416 struct update_subgroup *tmp_subgrp;
1417 const char *reason = arg;
1418
1419 UPDGRP_FOREACH_SUBGRP_SAFE (updgrp, subgrp, tmp_subgrp)
1420 update_subgroup_check_merge (subgrp, reason);
1421 return UPDWALK_CONTINUE;
1422}
1423
1424/********************
1425 * PUBLIC FUNCTIONS
1426 ********************/
1427
1428/*
1429 * trigger function when a policy (route-map/filter-list/prefix-list/
1430 * distribute-list etc.) content changes. Go through all the
1431 * update groups and process the change.
1432 *
1433 * bgp: the bgp instance
1434 * ptype: the type of policy that got modified, see bgpd.h
1435 * pname: name of the policy
1436 * route_update: flag to control if an automatic update generation should
1437 * occur
1438 * start_event: flag that indicates if it's the beginning of the change.
1439 * Esp. when the user is changing the content interactively
1440 * over multiple statements. Useful to set dirty flag on
1441 * update groups.
1442 */
1443void
1444update_group_policy_update (struct bgp *bgp, bgp_policy_type_e ptype,
ffd0c037 1445 const char *pname, int route_update, int start_event)
3f9c7369
DS
1446{
1447 struct updwalk_context ctx;
1448
1449 memset (&ctx, 0, sizeof (ctx));
1450 ctx.policy_type = ptype;
1451 ctx.policy_name = pname;
1452 ctx.policy_route_update = route_update;
1453 ctx.policy_event_start_flag = start_event;
1454 ctx.flags = 0;
1455
1456 update_group_walk (bgp, updgrp_policy_update_walkcb, &ctx);
1457}
1458
1459/*
1460 * update_subgroup_split_peer
1461 *
1462 * Ensure that the given peer is in a subgroup of its own in the
1463 * specified update group.
1464 */
1465void
1466update_subgroup_split_peer (struct peer_af *paf, struct update_group *updgrp)
1467{
1468 struct update_subgroup *old_subgrp, *subgrp;
1469 uint64_t old_id;
1470
1471
1472 old_subgrp = paf->subgroup;
1473
1474 if (!updgrp)
1475 updgrp = old_subgrp->update_group;
1476
1477 /*
1478 * If the peer is alone in its subgroup, reuse the existing
1479 * subgroup.
1480 */
1481 if (old_subgrp->peer_count == 1)
1482 {
1483 if (updgrp == old_subgrp->update_group)
1484 return;
1485
1486 subgrp = old_subgrp;
1487 old_id = old_subgrp->update_group->id;
1488
ffd0c037 1489 if (bgp_debug_peer_updout_enabled(paf->peer->host))
3f9c7369
DS
1490 {
1491 UPDGRP_PEER_DBG_DIS(old_subgrp->update_group);
1492 }
1493
1494 update_group_remove_subgroup (old_subgrp->update_group, old_subgrp);
1495 update_group_add_subgroup (updgrp, subgrp);
1496
ffd0c037 1497 if (bgp_debug_peer_updout_enabled(paf->peer->host))
3f9c7369
DS
1498 {
1499 UPDGRP_PEER_DBG_EN(updgrp);
1500 }
1501 if (BGP_DEBUG (update_groups, UPDATE_GROUPS))
ffd0c037 1502 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " peer %s moved to u%" PRIu64 ":s%" PRIu64,
3f9c7369
DS
1503 old_id, subgrp->id, paf->peer->host, updgrp->id, subgrp->id);
1504
1505 /*
1506 * The state of the subgroup (adj_out, advs, packet queue etc)
1507 * is consistent internally, but may not be identical to other
1508 * subgroups in the new update group even if the version number
1509 * matches up. Make sure a full refresh is done before the
1510 * subgroup is merged with another.
1511 */
1512 update_subgroup_set_needs_refresh (subgrp, 1);
1513
1514 SUBGRP_INCR_STAT (subgrp, updgrp_switch_events);
1515 return;
1516 }
1517
1518 /*
1519 * Create a new subgroup under the specified update group, and copy
1520 * over relevant state to it.
1521 */
1522 subgrp = update_subgroup_create (updgrp);
1523 update_subgroup_inherit_info (subgrp, old_subgrp);
1524
1525 subgrp->split_from.update_group_id = old_subgrp->update_group->id;
1526 subgrp->split_from.subgroup_id = old_subgrp->id;
1527
1528 /*
1529 * Copy out relevant state from the old subgroup.
1530 */
1531 update_subgroup_copy_adj_out (paf->subgroup, subgrp);
1532 update_subgroup_copy_packets (subgrp, paf->next_pkt_to_send);
1533
1534 if (BGP_DEBUG (update_groups, UPDATE_GROUPS))
ffd0c037 1535 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " peer %s split and moved into u%" PRIu64 ":s%" PRIu64,
3f9c7369
DS
1536 paf->subgroup->update_group->id, paf->subgroup->id,
1537 paf->peer->host, updgrp->id, subgrp->id);
1538
1539 SUBGRP_INCR_STAT (paf->subgroup, split_events);
1540
1541 /*
1542 * Since queued advs were left behind, this new subgroup needs a
1543 * refresh.
1544 */
1545 update_subgroup_set_needs_refresh (subgrp, 1);
1546
1547 /*
1548 * Remove peer from old subgroup, and add it to the new one.
1549 */
1550 update_subgroup_remove_peer (paf->subgroup, paf);
1551
1552 update_subgroup_add_peer (subgrp, paf, 1);
1553}
1554
1555void
3d68677e 1556update_bgp_group_init (struct bgp *bgp)
3f9c7369
DS
1557{
1558 int afid;
1559
1560 AF_FOREACH (afid)
1561 bgp->update_groups[afid] = hash_create (updgrp_hash_key_make,
dfd19ccc 1562 updgrp_hash_cmp, NULL);
3f9c7369
DS
1563}
1564
3d68677e
DS
1565void
1566update_bgp_group_free (struct bgp *bgp)
1567{
1568 int afid;
1569
1570 AF_FOREACH (afid)
1571 {
3ffe142a
DW
1572 if (bgp->update_groups[afid])
1573 {
1574 hash_free(bgp->update_groups[afid]);
1575 bgp->update_groups[afid] = NULL;
1576 }
3d68677e
DS
1577 }
1578}
1579
3f9c7369 1580void
8fe8a7f6 1581update_group_show (struct bgp *bgp, afi_t afi, safi_t safi, struct vty *vty,
f43e655e 1582 uint64_t subgrp_id)
3f9c7369 1583{
8fe8a7f6
DS
1584 struct updwalk_context ctx;
1585 memset (&ctx, 0, sizeof (ctx));
1586 ctx.vty = vty;
1587 ctx.subgrp_id = subgrp_id;
1588
1589 update_group_af_walk (bgp, afi, safi, update_group_show_walkcb, &ctx);
3f9c7369
DS
1590}
1591
1592/*
1593 * update_group_show_stats
1594 *
1595 * Show global statistics about update groups.
1596 */
1597void
1598update_group_show_stats (struct bgp *bgp, struct vty *vty)
1599{
96ade3ed
QY
1600 vty_outln (vty, "Update groups created: %u",
1601 bgp->update_group_stats.updgrps_created);
1602 vty_outln (vty, "Update groups deleted: %u",
1603 bgp->update_group_stats.updgrps_deleted);
1604 vty_outln (vty, "Update subgroups created: %u",
1605 bgp->update_group_stats.subgrps_created);
1606 vty_outln (vty, "Update subgroups deleted: %u",
1607 bgp->update_group_stats.subgrps_deleted);
1608 vty_outln (vty, "Join events: %u",
1609 bgp->update_group_stats.join_events);
1610 vty_outln (vty, "Prune events: %u",
1611 bgp->update_group_stats.prune_events);
1612 vty_outln (vty, "Merge events: %u",
1613 bgp->update_group_stats.merge_events);
1614 vty_outln (vty, "Split events: %u",
1615 bgp->update_group_stats.split_events);
1616 vty_outln (vty, "Update group switch events: %u",
1617 bgp->update_group_stats.updgrp_switch_events);
1618 vty_outln (vty, "Peer route refreshes combined: %u",
1619 bgp->update_group_stats.peer_refreshes_combined);
1620 vty_outln (vty, "Merge checks triggered: %u",
1621 bgp->update_group_stats.merge_checks_triggered);
3f9c7369
DS
1622}
1623
1624/*
1625 * update_group_adjust_peer
1626 */
1627void
1628update_group_adjust_peer (struct peer_af *paf)
1629{
1630 struct update_group *updgrp;
1631 struct update_subgroup *subgrp, *old_subgrp;
1632 struct peer *peer;
1633
1634 if (!paf)
1635 return;
1636
1637 peer = PAF_PEER (paf);
1638 if (!peer_established (peer))
1639 {
1640 return;
1641 }
1642
1643 if (!CHECK_FLAG (peer->flags, PEER_FLAG_CONFIG_NODE))
1644 {
1645 return;
1646 }
1647
1648 if (!peer->afc_nego[paf->afi][paf->safi])
1649 {
1650 return;
1651 }
1652
1653 updgrp = update_group_find (paf);
1654 if (!updgrp)
1655 {
1656 updgrp = update_group_create (paf);
1657 if (!updgrp)
1658 {
1659 zlog_err ("couldn't create update group for peer %s",
1660 paf->peer->host);
1661 return;
1662 }
1663 }
1664
1665 old_subgrp = paf->subgroup;
1666
1667 if (old_subgrp)
1668 {
1669
1670 /*
1671 * If the update group of the peer is unchanged, the peer can stay
1672 * in its existing subgroup and we're done.
1673 */
1674 if (old_subgrp->update_group == updgrp)
1675 return;
1676
1677 /*
1678 * The peer is switching between update groups. Put it in its
1679 * own subgroup under the new update group.
1680 */
1681 update_subgroup_split_peer (paf, updgrp);
1682 return;
1683 }
1684
1685 subgrp = update_subgroup_find (updgrp, paf);
1686 if (!subgrp)
1687 {
1688 subgrp = update_subgroup_create (updgrp);
1689 if (!subgrp)
1690 return;
1691 }
1692
1693 update_subgroup_add_peer (subgrp, paf, 1);
1694 if (BGP_DEBUG (update_groups, UPDATE_GROUPS))
ffd0c037 1695 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " add peer %s",
3f9c7369
DS
1696 updgrp->id, subgrp->id, paf->peer->host);
1697
1698 return;
1699}
1700
1701int
1702update_group_adjust_soloness (struct peer *peer, int set)
1703{
1704 struct peer_group *group;
1705 struct listnode *node, *nnode;
1706
3f9c7369
DS
1707 if (!CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
1708 {
1709 peer_lonesoul_or_not (peer, set);
1710 if (peer->status == Established)
1711 bgp_announce_route_all (peer);
1712 }
1713 else
1714 {
1715 group = peer->group;
1716 for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
1717 {
1718 peer_lonesoul_or_not (peer, set);
1719 if (peer->status == Established)
1720 bgp_announce_route_all (peer);
1721 }
1722 }
1723 return 0;
1724}
1725
1726/*
1727 * update_subgroup_rib
1728 */
1729struct bgp_table *
1730update_subgroup_rib (struct update_subgroup *subgrp)
1731{
1732 struct bgp *bgp;
1733
1734 bgp = SUBGRP_INST (subgrp);
1735 if (!bgp)
1736 return NULL;
1737
1738 return bgp->rib[SUBGRP_AFI (subgrp)][SUBGRP_SAFI (subgrp)];
1739}
1740
1741void
1742update_group_af_walk (struct bgp *bgp, afi_t afi, safi_t safi,
1743 updgrp_walkcb cb, void *ctx)
1744{
1745 struct updwalk_context wctx;
1746 int afid;
1747
1748 if (!bgp)
1749 return;
1750 afid = afindex (afi, safi);
1751 if (afid >= BGP_AF_MAX)
1752 return;
1753
1754 memset (&wctx, 0, sizeof (wctx));
1755 wctx.cb = cb;
1756 wctx.context = ctx;
0de4848d
DS
1757
1758 if (bgp->update_groups[afid])
1759 hash_walk (bgp->update_groups[afid], update_group_walkcb, &wctx);
3f9c7369
DS
1760}
1761
1762void
1763update_group_walk (struct bgp *bgp, updgrp_walkcb cb, void *ctx)
1764{
1765 afi_t afi;
1766 safi_t safi;
1767
1768 FOREACH_AFI_SAFI (afi, safi)
1769 {
1770 update_group_af_walk (bgp, afi, safi, cb, ctx);
1771 }
1772}
1773
1774void
1775update_group_periodic_merge (struct bgp *bgp)
1776{
1777 char reason[] = "periodic merge check";
1778
1779 update_group_walk (bgp, update_group_periodic_merge_walkcb,
1780 (void *) reason);
1781}
1782
0de4848d
DS
1783static int
1784update_group_default_originate_route_map_walkcb(struct update_group *updgrp,
1785 void *arg)
1786{
1787 struct update_subgroup *subgrp;
1788 struct peer *peer;
0de4848d
DS
1789 afi_t afi;
1790 safi_t safi;
1791
1792 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp)
1793 {
1794 peer = SUBGRP_PEER (subgrp);
1795 afi = SUBGRP_AFI (subgrp);
1796 safi = SUBGRP_SAFI (subgrp);
1797
1798 if (peer->default_rmap[afi][safi].name)
1799 {
1800 subgroup_default_originate (subgrp, 0);
1801 }
1802 }
1803
1804 return UPDWALK_CONTINUE;
1805}
1806
ffd0c037 1807int
0de4848d
DS
1808update_group_refresh_default_originate_route_map (struct thread *thread)
1809{
1810 struct bgp *bgp;
1811 char reason[] = "refresh default-originate route-map";
1812
1813 bgp = THREAD_ARG(thread);
1814 update_group_walk (bgp, update_group_default_originate_route_map_walkcb,
1815 reason);
1816 THREAD_TIMER_OFF (bgp->t_rmap_def_originate_eval);
1817 bgp_unlock(bgp);
ffd0c037
DS
1818
1819 return(0);
0de4848d
DS
1820}
1821
3f9c7369
DS
1822/*
1823 * peer_af_announce_route
1824 *
1825 * Refreshes routes out to a peer_af immediately.
1826 *
1827 * If the combine parameter is TRUE, then this function will try to
1828 * gather other peers in the subgroup for which a route announcement
1829 * is pending and efficently announce routes to all of them.
1830 *
1831 * For now, the 'combine' option has an effect only if all peers in
1832 * the subgroup have a route announcement pending.
1833 */
1834void
1835peer_af_announce_route (struct peer_af *paf, int combine)
1836{
1837 struct update_subgroup *subgrp;
1838 struct peer_af *cur_paf;
1839 int all_pending;
1840
1841 subgrp = paf->subgroup;
1842 all_pending = 0;
1843
1844 if (combine)
1845 {
3f9c7369
DS
1846 /*
1847 * If there are other peers in the old subgroup that also need
1848 * routes to be announced, pull them into the peer's new
1849 * subgroup.
1850 * Combine route announcement with other peers if possible.
1851 *
1852 * For now, we combine only if all peers in the subgroup have an
1853 * announcement pending.
1854 */
1855 all_pending = 1;
1856
1857 SUBGRP_FOREACH_PEER (subgrp, cur_paf)
1858 {
1859 if (cur_paf == paf)
1860 continue;
1861
1862 if (cur_paf->t_announce_route)
1863 continue;
1864
1865 all_pending = 0;
1866 break;
1867 }
1868 }
1869 /*
1870 * Announce to the peer alone if we were not asked to combine peers,
1871 * or if some peers don't have a route annoucement pending.
1872 */
1873 if (!combine || !all_pending)
1874 {
1875 update_subgroup_split_peer (paf, NULL);
1876 if (!paf->subgroup)
1877 return;
1878
1879 if (bgp_debug_update(paf->peer, NULL, subgrp->update_group, 0))
ffd0c037 1880 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " %s announcing routes",
3f9c7369
DS
1881 subgrp->update_group->id, subgrp->id, paf->peer->host);
1882
1883 subgroup_announce_route (paf->subgroup);
1884 return;
1885 }
1886
1887 /*
1888 * We will announce routes the entire subgroup.
1889 *
1890 * First stop refresh timers on all the other peers.
1891 */
1892 SUBGRP_FOREACH_PEER (subgrp, cur_paf)
1893 {
1894 if (cur_paf == paf)
1895 continue;
1896
1897 bgp_stop_announce_route_timer (cur_paf);
1898 }
1899
1900 if (bgp_debug_update(paf->peer, NULL, subgrp->update_group, 0))
ffd0c037 1901 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " announcing routes to %s, combined into %d peers",
3f9c7369
DS
1902 subgrp->update_group->id, subgrp->id,
1903 paf->peer->host, subgrp->peer_count);
1904
1905 subgroup_announce_route (subgrp);
1906
1907 SUBGRP_INCR_STAT_BY (subgrp, peer_refreshes_combined,
1908 subgrp->peer_count - 1);
1909}
1910
1911void
1912subgroup_trigger_write (struct update_subgroup *subgrp)
1913{
1914 struct peer_af *paf;
1915
1916#if 0
1917 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
1918 zlog_debug("u%llu:s%llu scheduling write thread for peers",
1919 subgrp->update_group->id, subgrp->id);
1920#endif
1921 SUBGRP_FOREACH_PEER (subgrp, paf)
1922 {
1923 if (paf->peer->status == Established)
1924 {
1925 BGP_PEER_WRITE_ON (paf->peer->t_write, bgp_write, paf->peer->fd,
1926 paf->peer);
1927 }
1928 }
1929}
1930
1931int
1932update_group_clear_update_dbg (struct update_group *updgrp, void *arg)
1933{
1934 UPDGRP_PEER_DBG_OFF(updgrp);
1935 return UPDWALK_CONTINUE;
1936}
adbac85e 1937
06370dac 1938/* Return true if we should addpath encode NLRI to this peer */
adbac85e
DW
1939int
1940bgp_addpath_encode_tx (struct peer *peer, afi_t afi, safi_t safi)
1941{
1942 return (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_TX_ADV) &&
1943 CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_RCV));
1944}
06370dac
DW
1945
1946/*
1947 * Return true if this is a path we should advertise due to a
1948 * configured addpath-tx knob
1949 */
1950int
1951bgp_addpath_tx_path (struct peer *peer, afi_t afi, safi_t safi,
1952 struct bgp_info *ri)
1953{
1954 if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ADDPATH_TX_ALL_PATHS))
1955 return 1;
1956
1957 if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ADDPATH_TX_BESTPATH_PER_AS) &&
1958 CHECK_FLAG (ri->flags, BGP_INFO_DMED_SELECTED))
1959 return 1;
1960
1961 return 0;
1962}