2 * bgp_updgrp.c: BGP update group structures
4 * @copyright Copyright (C) 2014 Cumulus Networks, Inc.
6 * @author Avneesh Sachdev <avneesh@sproute.net>
7 * @author Rajesh Varadarajan <rajesh@sproute.net>
8 * @author Pradosh Mohapatra <pradosh@sproute.net>
10 * This file is part of GNU Zebra.
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
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.
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
34 #include "sockunion.h"
42 #include "workqueue.h"
47 #include "bgpd/bgpd.h"
48 #include "bgpd/bgp_table.h"
49 #include "bgpd/bgp_debug.h"
50 #include "bgpd/bgp_errors.h"
51 #include "bgpd/bgp_fsm.h"
52 #include "bgpd/bgp_advertise.h"
53 #include "bgpd/bgp_packet.h"
54 #include "bgpd/bgp_updgrp.h"
55 #include "bgpd/bgp_route.h"
56 #include "bgpd/bgp_filter.h"
57 #include "bgpd/bgp_io.h"
64 * assign a unique ID to update group and subgroup. Mostly for display/
65 * debugging purposes. It's a 64-bit space - used leisurely without a
66 * worry about its wrapping and about filling gaps. While at it, timestamp
69 static void update_group_checkin(struct update_group
*updgrp
)
71 updgrp
->id
= ++bm
->updgrp_idspace
;
72 updgrp
->uptime
= bgp_clock();
75 static void update_subgroup_checkin(struct update_subgroup
*subgrp
,
76 struct update_group
*updgrp
)
78 subgrp
->id
= ++bm
->subgrp_idspace
;
79 subgrp
->uptime
= bgp_clock();
82 static void sync_init(struct update_subgroup
*subgrp
,
83 struct update_group
*updgrp
)
85 struct peer
*peer
= UPDGRP_PEER(updgrp
);
88 XCALLOC(MTYPE_BGP_SYNCHRONISE
, sizeof(struct bgp_synchronize
));
89 bgp_adv_fifo_init(&subgrp
->sync
->update
);
90 bgp_adv_fifo_init(&subgrp
->sync
->withdraw
);
91 bgp_adv_fifo_init(&subgrp
->sync
->withdraw_low
);
93 hash_create(baa_hash_key
, baa_hash_cmp
, "BGP SubGroup Hash");
95 /* We use a larger buffer for subgrp->work in the event that:
96 * - We RX a BGP_UPDATE where the attributes alone are just
97 * under 4096 or 65535 (if Extended Message capability negotiated).
98 * - The user configures an outbound route-map that does many as-path
99 * prepends or adds many communities. At most they can have
101 * args in a route-map so there is a finite limit on how large they
103 * make the attributes.
105 * Having a buffer with BGP_MAX_PACKET_SIZE_OVERFLOW allows us to avoid
107 * checking for every single attribute as we construct an UPDATE.
109 subgrp
->work
= stream_new(peer
->max_packet_size
110 + BGP_MAX_PACKET_SIZE_OVERFLOW
);
111 subgrp
->scratch
= stream_new(peer
->max_packet_size
);
114 static void sync_delete(struct update_subgroup
*subgrp
)
116 XFREE(MTYPE_BGP_SYNCHRONISE
, subgrp
->sync
);
118 hash_free(subgrp
->hash
);
121 stream_free(subgrp
->work
);
124 stream_free(subgrp
->scratch
);
125 subgrp
->scratch
= NULL
;
131 * copy only those fields that are relevant to update group match
133 static void conf_copy(struct peer
*dst
, struct peer
*src
, afi_t afi
,
136 struct bgp_filter
*srcfilter
;
137 struct bgp_filter
*dstfilter
;
139 srcfilter
= &src
->filter
[afi
][safi
];
140 dstfilter
= &dst
->filter
[afi
][safi
];
143 dst
->sort
= src
->sort
;
145 dst
->v_routeadv
= src
->v_routeadv
;
146 dst
->flags
= src
->flags
;
147 dst
->af_flags
[afi
][safi
] = src
->af_flags
[afi
][safi
];
148 dst
->pmax_out
[afi
][safi
] = src
->pmax_out
[afi
][safi
];
149 dst
->max_packet_size
= src
->max_packet_size
;
150 XFREE(MTYPE_BGP_PEER_HOST
, dst
->host
);
152 dst
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, src
->host
);
154 dst
->af_cap
[afi
][safi
] = src
->af_cap
[afi
][safi
];
155 dst
->afc_nego
[afi
][safi
] = src
->afc_nego
[afi
][safi
];
156 dst
->orf_plist
[afi
][safi
] = src
->orf_plist
[afi
][safi
];
157 dst
->addpath_type
[afi
][safi
] = src
->addpath_type
[afi
][safi
];
158 dst
->local_as
= src
->local_as
;
159 dst
->change_local_as
= src
->change_local_as
;
160 dst
->shared_network
= src
->shared_network
;
161 memcpy(&(dst
->nexthop
), &(src
->nexthop
), sizeof(struct bgp_nexthop
));
163 dst
->group
= src
->group
;
165 if (src
->default_rmap
[afi
][safi
].name
) {
166 dst
->default_rmap
[afi
][safi
].name
=
167 XSTRDUP(MTYPE_ROUTE_MAP_NAME
,
168 src
->default_rmap
[afi
][safi
].name
);
169 dst
->default_rmap
[afi
][safi
].map
=
170 src
->default_rmap
[afi
][safi
].map
;
173 if (DISTRIBUTE_OUT_NAME(srcfilter
)) {
174 DISTRIBUTE_OUT_NAME(dstfilter
) = XSTRDUP(
175 MTYPE_BGP_FILTER_NAME
, DISTRIBUTE_OUT_NAME(srcfilter
));
176 DISTRIBUTE_OUT(dstfilter
) = DISTRIBUTE_OUT(srcfilter
);
179 if (PREFIX_LIST_OUT_NAME(srcfilter
)) {
180 PREFIX_LIST_OUT_NAME(dstfilter
) = XSTRDUP(
181 MTYPE_BGP_FILTER_NAME
, PREFIX_LIST_OUT_NAME(srcfilter
));
182 PREFIX_LIST_OUT(dstfilter
) = PREFIX_LIST_OUT(srcfilter
);
185 if (FILTER_LIST_OUT_NAME(srcfilter
)) {
186 FILTER_LIST_OUT_NAME(dstfilter
) = XSTRDUP(
187 MTYPE_BGP_FILTER_NAME
, FILTER_LIST_OUT_NAME(srcfilter
));
188 FILTER_LIST_OUT(dstfilter
) = FILTER_LIST_OUT(srcfilter
);
191 if (ROUTE_MAP_OUT_NAME(srcfilter
)) {
192 ROUTE_MAP_OUT_NAME(dstfilter
) = XSTRDUP(
193 MTYPE_BGP_FILTER_NAME
, ROUTE_MAP_OUT_NAME(srcfilter
));
194 ROUTE_MAP_OUT(dstfilter
) = ROUTE_MAP_OUT(srcfilter
);
197 if (UNSUPPRESS_MAP_NAME(srcfilter
)) {
198 UNSUPPRESS_MAP_NAME(dstfilter
) = XSTRDUP(
199 MTYPE_BGP_FILTER_NAME
, UNSUPPRESS_MAP_NAME(srcfilter
));
200 UNSUPPRESS_MAP(dstfilter
) = UNSUPPRESS_MAP(srcfilter
);
203 if (ADVERTISE_MAP_NAME(srcfilter
)) {
204 ADVERTISE_MAP_NAME(dstfilter
) = XSTRDUP(
205 MTYPE_BGP_FILTER_NAME
, ADVERTISE_MAP_NAME(srcfilter
));
206 ADVERTISE_MAP(dstfilter
) = ADVERTISE_MAP(srcfilter
);
207 ADVERTISE_CONDITION(dstfilter
) = ADVERTISE_CONDITION(srcfilter
);
210 if (CONDITION_MAP_NAME(srcfilter
)) {
211 CONDITION_MAP_NAME(dstfilter
) = XSTRDUP(
212 MTYPE_BGP_FILTER_NAME
, CONDITION_MAP_NAME(srcfilter
));
213 CONDITION_MAP(dstfilter
) = CONDITION_MAP(srcfilter
);
218 * since we did a bunch of XSTRDUP's in conf_copy, time to free them up
220 static void conf_release(struct peer
*src
, afi_t afi
, safi_t safi
)
222 struct bgp_filter
*srcfilter
;
224 srcfilter
= &src
->filter
[afi
][safi
];
226 XFREE(MTYPE_ROUTE_MAP_NAME
, src
->default_rmap
[afi
][safi
].name
);
228 XFREE(MTYPE_BGP_FILTER_NAME
, srcfilter
->dlist
[FILTER_OUT
].name
);
230 XFREE(MTYPE_BGP_FILTER_NAME
, srcfilter
->plist
[FILTER_OUT
].name
);
232 XFREE(MTYPE_BGP_FILTER_NAME
, srcfilter
->aslist
[FILTER_OUT
].name
);
234 XFREE(MTYPE_BGP_FILTER_NAME
, srcfilter
->map
[RMAP_OUT
].name
);
236 XFREE(MTYPE_BGP_FILTER_NAME
, srcfilter
->usmap
.name
);
238 XFREE(MTYPE_BGP_FILTER_NAME
, srcfilter
->advmap
.aname
);
240 XFREE(MTYPE_BGP_FILTER_NAME
, srcfilter
->advmap
.cname
);
242 XFREE(MTYPE_BGP_PEER_HOST
, src
->host
);
245 static void peer2_updgrp_copy(struct update_group
*updgrp
, struct peer_af
*paf
)
258 updgrp
->afi
= paf
->afi
;
259 updgrp
->safi
= paf
->safi
;
260 updgrp
->afid
= paf
->afid
;
261 updgrp
->bgp
= src
->bgp
;
263 conf_copy(dst
, src
, paf
->afi
, paf
->safi
);
267 * auxiliary functions to maintain the hash table.
268 * - updgrp_hash_alloc - to create a new entry, passed to hash_get
269 * - updgrp_hash_key_make - makes the key for update group search
270 * - updgrp_hash_cmp - compare two update groups.
272 static void *updgrp_hash_alloc(void *p
)
274 struct update_group
*updgrp
;
275 const struct update_group
*in
;
277 in
= (const struct update_group
*)p
;
278 updgrp
= XCALLOC(MTYPE_BGP_UPDGRP
, sizeof(struct update_group
));
279 memcpy(updgrp
, in
, sizeof(struct update_group
));
280 updgrp
->conf
= XCALLOC(MTYPE_BGP_PEER
, sizeof(struct peer
));
281 conf_copy(updgrp
->conf
, in
->conf
, in
->afi
, in
->safi
);
286 * The hash value for a peer is computed from the following variables:
288 * 1. IBGP (1) or EBGP (2)
289 * 2. FLAGS based on configuration:
290 * LOCAL_AS_NO_PREPEND
291 * LOCAL_AS_REPLACE_AS
292 * 3. AF_FLAGS based on configuration:
293 * Refer to definition in bgp_updgrp.h
294 * 4. (AF-independent) Capability flags:
296 * 5. (AF-dependent) Capability flags:
297 * ORF_PREFIX_SM_RCV (peer can send prefix ORF)
300 * 8. Outbound route-map name (neighbor route-map <> out)
301 * 9. Outbound distribute-list name (neighbor distribute-list <> out)
302 * 10. Outbound prefix-list name (neighbor prefix-list <> out)
303 * 11. Outbound as-list name (neighbor filter-list <> out)
304 * 12. Unsuppress map name (neighbor unsuppress-map <>)
305 * 13. default rmap name (neighbor default-originate route-map <>)
306 * 14. encoding both global and link-local nexthop?
307 * 15. If peer is configured to be a lonesoul, peer ip address
308 * 16. Local-as should match, if configured.
311 static unsigned int updgrp_hash_key_make(const void *p
)
313 const struct update_group
*updgrp
;
314 const struct peer
*peer
;
315 const struct bgp_filter
*filter
;
322 #define SEED2 2147483647
328 flags
= peer
->af_flags
[afi
][safi
];
329 filter
= &peer
->filter
[afi
][safi
];
333 key
= jhash_1word(peer
->sort
, key
); /* EBGP or IBGP */
334 key
= jhash_1word((peer
->flags
& PEER_UPDGRP_FLAGS
), key
);
335 key
= jhash_1word((flags
& PEER_UPDGRP_AF_FLAGS
), key
);
336 key
= jhash_1word((uint32_t)peer
->addpath_type
[afi
][safi
], key
);
337 key
= jhash_1word((peer
->cap
& PEER_UPDGRP_CAP_FLAGS
), key
);
338 key
= jhash_1word((peer
->af_cap
[afi
][safi
] & PEER_UPDGRP_AF_CAP_FLAGS
),
340 key
= jhash_1word(peer
->v_routeadv
, key
);
341 key
= jhash_1word(peer
->change_local_as
, key
);
342 key
= jhash_1word(peer
->max_packet_size
, key
);
345 key
= jhash_1word(jhash(peer
->group
->name
,
346 strlen(peer
->group
->name
), SEED1
),
349 if (filter
->map
[RMAP_OUT
].name
)
350 key
= jhash_1word(jhash(filter
->map
[RMAP_OUT
].name
,
351 strlen(filter
->map
[RMAP_OUT
].name
),
355 if (filter
->dlist
[FILTER_OUT
].name
)
356 key
= jhash_1word(jhash(filter
->dlist
[FILTER_OUT
].name
,
357 strlen(filter
->dlist
[FILTER_OUT
].name
),
361 if (filter
->plist
[FILTER_OUT
].name
)
362 key
= jhash_1word(jhash(filter
->plist
[FILTER_OUT
].name
,
363 strlen(filter
->plist
[FILTER_OUT
].name
),
367 if (filter
->aslist
[FILTER_OUT
].name
)
368 key
= jhash_1word(jhash(filter
->aslist
[FILTER_OUT
].name
,
369 strlen(filter
->aslist
[FILTER_OUT
].name
),
373 if (filter
->usmap
.name
)
374 key
= jhash_1word(jhash(filter
->usmap
.name
,
375 strlen(filter
->usmap
.name
), SEED1
),
378 if (filter
->advmap
.aname
)
379 key
= jhash_1word(jhash(filter
->advmap
.aname
,
380 strlen(filter
->advmap
.aname
), SEED1
),
383 if (peer
->default_rmap
[afi
][safi
].name
)
385 jhash(peer
->default_rmap
[afi
][safi
].name
,
386 strlen(peer
->default_rmap
[afi
][safi
].name
),
390 /* If peer is on a shared network and is exchanging IPv6 prefixes,
391 * it needs to include link-local address. That's different from
392 * non-shared-network peers (nexthop encoded with 32 bytes vs 16
393 * bytes). We create different update groups to take care of that.
396 (peer
->shared_network
&& peer_afi_active_nego(peer
, AFI_IP6
)),
400 * There are certain peers that must get their own update-group:
402 * - peers that negotiated ORF
403 * - maximum-prefix-out is set
405 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_LONESOUL
)
406 || CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_RCV
)
407 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
408 PEER_CAP_ORF_PREFIX_SM_OLD_RCV
)
409 || CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_MAX_PREFIX_OUT
))
410 key
= jhash_1word(jhash(peer
->host
, strlen(peer
->host
), SEED2
),
416 static bool updgrp_hash_cmp(const void *p1
, const void *p2
)
418 const struct update_group
*grp1
;
419 const struct update_group
*grp2
;
420 const struct peer
*pe1
;
421 const struct peer
*pe2
;
424 const struct bgp_filter
*fl1
;
425 const struct bgp_filter
*fl2
;
438 flags1
= pe1
->af_flags
[afi
][safi
];
439 flags2
= pe2
->af_flags
[afi
][safi
];
440 fl1
= &pe1
->filter
[afi
][safi
];
441 fl2
= &pe2
->filter
[afi
][safi
];
443 /* put EBGP and IBGP peers in different update groups */
444 if (pe1
->sort
!= pe2
->sort
)
447 /* check peer flags */
448 if ((pe1
->flags
& PEER_UPDGRP_FLAGS
)
449 != (pe2
->flags
& PEER_UPDGRP_FLAGS
))
452 /* If there is 'local-as' configured, it should match. */
453 if (pe1
->change_local_as
!= pe2
->change_local_as
)
456 /* flags like route reflector client */
457 if ((flags1
& PEER_UPDGRP_AF_FLAGS
) != (flags2
& PEER_UPDGRP_AF_FLAGS
))
460 if (pe1
->addpath_type
[afi
][safi
] != pe2
->addpath_type
[afi
][safi
])
463 if ((pe1
->cap
& PEER_UPDGRP_CAP_FLAGS
)
464 != (pe2
->cap
& PEER_UPDGRP_CAP_FLAGS
))
467 if ((pe1
->af_cap
[afi
][safi
] & PEER_UPDGRP_AF_CAP_FLAGS
)
468 != (pe2
->af_cap
[afi
][safi
] & PEER_UPDGRP_AF_CAP_FLAGS
))
471 if (pe1
->v_routeadv
!= pe2
->v_routeadv
)
474 if (pe1
->group
!= pe2
->group
)
477 /* route-map names should be the same */
478 if ((fl1
->map
[RMAP_OUT
].name
&& !fl2
->map
[RMAP_OUT
].name
)
479 || (!fl1
->map
[RMAP_OUT
].name
&& fl2
->map
[RMAP_OUT
].name
)
480 || (fl1
->map
[RMAP_OUT
].name
&& fl2
->map
[RMAP_OUT
].name
481 && strcmp(fl1
->map
[RMAP_OUT
].name
, fl2
->map
[RMAP_OUT
].name
)))
484 if ((fl1
->dlist
[FILTER_OUT
].name
&& !fl2
->dlist
[FILTER_OUT
].name
)
485 || (!fl1
->dlist
[FILTER_OUT
].name
&& fl2
->dlist
[FILTER_OUT
].name
)
486 || (fl1
->dlist
[FILTER_OUT
].name
&& fl2
->dlist
[FILTER_OUT
].name
487 && strcmp(fl1
->dlist
[FILTER_OUT
].name
,
488 fl2
->dlist
[FILTER_OUT
].name
)))
491 if ((fl1
->plist
[FILTER_OUT
].name
&& !fl2
->plist
[FILTER_OUT
].name
)
492 || (!fl1
->plist
[FILTER_OUT
].name
&& fl2
->plist
[FILTER_OUT
].name
)
493 || (fl1
->plist
[FILTER_OUT
].name
&& fl2
->plist
[FILTER_OUT
].name
494 && strcmp(fl1
->plist
[FILTER_OUT
].name
,
495 fl2
->plist
[FILTER_OUT
].name
)))
498 if ((fl1
->aslist
[FILTER_OUT
].name
&& !fl2
->aslist
[FILTER_OUT
].name
)
499 || (!fl1
->aslist
[FILTER_OUT
].name
&& fl2
->aslist
[FILTER_OUT
].name
)
500 || (fl1
->aslist
[FILTER_OUT
].name
&& fl2
->aslist
[FILTER_OUT
].name
501 && strcmp(fl1
->aslist
[FILTER_OUT
].name
,
502 fl2
->aslist
[FILTER_OUT
].name
)))
505 if ((fl1
->usmap
.name
&& !fl2
->usmap
.name
)
506 || (!fl1
->usmap
.name
&& fl2
->usmap
.name
)
507 || (fl1
->usmap
.name
&& fl2
->usmap
.name
508 && strcmp(fl1
->usmap
.name
, fl2
->usmap
.name
)))
511 if ((fl1
->advmap
.aname
&& !fl2
->advmap
.aname
)
512 || (!fl1
->advmap
.aname
&& fl2
->advmap
.aname
)
513 || (fl1
->advmap
.aname
&& fl2
->advmap
.aname
514 && strcmp(fl1
->advmap
.aname
, fl2
->advmap
.aname
)))
517 if ((pe1
->default_rmap
[afi
][safi
].name
518 && !pe2
->default_rmap
[afi
][safi
].name
)
519 || (!pe1
->default_rmap
[afi
][safi
].name
520 && pe2
->default_rmap
[afi
][safi
].name
)
521 || (pe1
->default_rmap
[afi
][safi
].name
522 && pe2
->default_rmap
[afi
][safi
].name
523 && strcmp(pe1
->default_rmap
[afi
][safi
].name
,
524 pe2
->default_rmap
[afi
][safi
].name
)))
527 if ((afi
== AFI_IP6
) && (pe1
->shared_network
!= pe2
->shared_network
))
530 if ((CHECK_FLAG(pe1
->flags
, PEER_FLAG_LONESOUL
)
531 || CHECK_FLAG(pe1
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_RCV
)
532 || CHECK_FLAG(pe1
->af_cap
[afi
][safi
],
533 PEER_CAP_ORF_PREFIX_SM_OLD_RCV
))
534 && !sockunion_same(&pe1
->su
, &pe2
->su
))
540 static void peer_lonesoul_or_not(struct peer
*peer
, int set
)
542 /* no change in status? */
543 if (set
== (CHECK_FLAG(peer
->flags
, PEER_FLAG_LONESOUL
) > 0))
547 SET_FLAG(peer
->flags
, PEER_FLAG_LONESOUL
);
549 UNSET_FLAG(peer
->flags
, PEER_FLAG_LONESOUL
);
551 update_group_adjust_peer_afs(peer
);
555 * subgroup_total_packets_enqueued
557 * Returns the total number of packets enqueued to a subgroup.
560 subgroup_total_packets_enqueued(struct update_subgroup
*subgrp
)
564 pkt
= bpacket_queue_last(SUBGRP_PKTQ(subgrp
));
569 static int update_group_show_walkcb(struct update_group
*updgrp
, void *arg
)
571 struct updwalk_context
*ctx
= arg
;
573 struct update_subgroup
*subgrp
;
575 struct bgp_filter
*filter
;
576 struct peer
*peer
= UPDGRP_PEER(updgrp
);
582 if (ctx
->subgrp_id
) {
583 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
) {
584 if (ctx
->subgrp_id
&& (ctx
->subgrp_id
!= subgrp
->id
))
596 /* Since this routine is invoked from a walk, we cannot signal
598 /* error here, can only return. */
604 vty_out(vty
, "Update-group %" PRIu64
":\n", updgrp
->id
);
605 vty_out(vty
, " Created: %s", timestamp_string(updgrp
->uptime
));
606 filter
= &updgrp
->conf
->filter
[updgrp
->afi
][updgrp
->safi
];
607 if (filter
->map
[RMAP_OUT
].name
)
608 vty_out(vty
, " Outgoing route map: %s\n",
609 filter
->map
[RMAP_OUT
].name
);
610 vty_out(vty
, " MRAI value (seconds): %d\n", updgrp
->conf
->v_routeadv
);
611 if (updgrp
->conf
->change_local_as
)
612 vty_out(vty
, " Local AS %u%s%s\n",
613 updgrp
->conf
->change_local_as
,
614 CHECK_FLAG(updgrp
->conf
->flags
,
615 PEER_FLAG_LOCAL_AS_NO_PREPEND
)
618 CHECK_FLAG(updgrp
->conf
->flags
,
619 PEER_FLAG_LOCAL_AS_REPLACE_AS
)
623 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
) {
624 if (ctx
->subgrp_id
&& (ctx
->subgrp_id
!= subgrp
->id
))
627 vty_out(vty
, " Update-subgroup %" PRIu64
":\n", subgrp
->id
);
628 vty_out(vty
, " Created: %s",
629 timestamp_string(subgrp
->uptime
));
631 if (subgrp
->split_from
.update_group_id
632 || subgrp
->split_from
.subgroup_id
) {
633 vty_out(vty
, " Split from group id: %" PRIu64
"\n",
634 subgrp
->split_from
.update_group_id
);
636 " Split from subgroup id: %" PRIu64
"\n",
637 subgrp
->split_from
.subgroup_id
);
640 vty_out(vty
, " Join events: %u\n", subgrp
->join_events
);
641 vty_out(vty
, " Prune events: %u\n", subgrp
->prune_events
);
642 vty_out(vty
, " Merge events: %u\n", subgrp
->merge_events
);
643 vty_out(vty
, " Split events: %u\n", subgrp
->split_events
);
644 vty_out(vty
, " Update group switch events: %u\n",
645 subgrp
->updgrp_switch_events
);
646 vty_out(vty
, " Peer refreshes combined: %u\n",
647 subgrp
->peer_refreshes_combined
);
648 vty_out(vty
, " Merge checks triggered: %u\n",
649 subgrp
->merge_checks_triggered
);
650 vty_out(vty
, " Coalesce Time: %u%s\n",
651 (UPDGRP_INST(subgrp
->update_group
))->coalesce_time
,
652 subgrp
->t_coalesce
? "(Running)" : "");
653 vty_out(vty
, " Version: %" PRIu64
"\n", subgrp
->version
);
654 vty_out(vty
, " Packet queue length: %d\n",
655 bpacket_queue_length(SUBGRP_PKTQ(subgrp
)));
656 vty_out(vty
, " Total packets enqueued: %u\n",
657 subgroup_total_packets_enqueued(subgrp
));
658 vty_out(vty
, " Packet queue high watermark: %d\n",
659 bpacket_queue_hwm_length(SUBGRP_PKTQ(subgrp
)));
660 vty_out(vty
, " Adj-out list count: %u\n", subgrp
->adj_count
);
661 vty_out(vty
, " Advertise list: %s\n",
662 advertise_list_is_empty(subgrp
) ? "empty"
664 vty_out(vty
, " Flags: %s\n",
665 CHECK_FLAG(subgrp
->flags
, SUBGRP_FLAG_NEEDS_REFRESH
)
669 vty_out(vty
, " Max packet size: %d\n",
670 peer
->max_packet_size
);
671 if (subgrp
->peer_count
> 0) {
672 vty_out(vty
, " Peers:\n");
673 SUBGRP_FOREACH_PEER (subgrp
, paf
)
674 vty_out(vty
, " - %s\n", paf
->peer
->host
);
677 return UPDWALK_CONTINUE
;
681 * Helper function to show the packet queue for each subgroup of update group.
682 * Will be constrained to a particular subgroup id if id !=0
684 static int updgrp_show_packet_queue_walkcb(struct update_group
*updgrp
,
687 struct updwalk_context
*ctx
= arg
;
688 struct update_subgroup
*subgrp
;
692 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
) {
693 if (ctx
->subgrp_id
&& (ctx
->subgrp_id
!= subgrp
->id
))
695 vty_out(vty
, "update group %" PRIu64
", subgroup %" PRIu64
"\n",
696 updgrp
->id
, subgrp
->id
);
697 bpacket_queue_show_vty(SUBGRP_PKTQ(subgrp
), vty
);
699 return UPDWALK_CONTINUE
;
703 * Show the packet queue for each subgroup of update group. Will be
704 * constrained to a particular subgroup id if id !=0
706 void update_group_show_packet_queue(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
707 struct vty
*vty
, uint64_t id
)
709 struct updwalk_context ctx
;
711 memset(&ctx
, 0, sizeof(ctx
));
715 update_group_af_walk(bgp
, afi
, safi
, updgrp_show_packet_queue_walkcb
,
719 static struct update_group
*update_group_find(struct peer_af
*paf
)
721 struct update_group
*updgrp
;
722 struct update_group tmp
;
723 struct peer tmp_conf
;
725 if (!peer_established(PAF_PEER(paf
)))
728 memset(&tmp
, 0, sizeof(tmp
));
729 memset(&tmp_conf
, 0, sizeof(tmp_conf
));
730 tmp
.conf
= &tmp_conf
;
731 peer2_updgrp_copy(&tmp
, paf
);
733 updgrp
= hash_lookup(paf
->peer
->bgp
->update_groups
[paf
->afid
], &tmp
);
734 conf_release(&tmp_conf
, paf
->afi
, paf
->safi
);
738 static struct update_group
*update_group_create(struct peer_af
*paf
)
740 struct update_group
*updgrp
;
741 struct update_group tmp
;
742 struct peer tmp_conf
;
744 memset(&tmp
, 0, sizeof(tmp
));
745 memset(&tmp_conf
, 0, sizeof(tmp_conf
));
746 tmp
.conf
= &tmp_conf
;
747 peer2_updgrp_copy(&tmp
, paf
);
749 updgrp
= hash_get(paf
->peer
->bgp
->update_groups
[paf
->afid
], &tmp
,
753 update_group_checkin(updgrp
);
755 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
756 zlog_debug("create update group %" PRIu64
, updgrp
->id
);
758 UPDGRP_GLOBAL_STAT(updgrp
, updgrps_created
) += 1;
760 conf_release(&tmp_conf
, paf
->afi
, paf
->safi
);
764 static void update_group_delete(struct update_group
*updgrp
)
766 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
767 zlog_debug("delete update group %" PRIu64
, updgrp
->id
);
769 UPDGRP_GLOBAL_STAT(updgrp
, updgrps_deleted
) += 1;
771 hash_release(updgrp
->bgp
->update_groups
[updgrp
->afid
], updgrp
);
772 conf_release(updgrp
->conf
, updgrp
->afi
, updgrp
->safi
);
774 XFREE(MTYPE_BGP_PEER_HOST
, updgrp
->conf
->host
);
776 XFREE(MTYPE_BGP_PEER_IFNAME
, updgrp
->conf
->ifname
);
778 XFREE(MTYPE_BGP_PEER
, updgrp
->conf
);
779 XFREE(MTYPE_BGP_UPDGRP
, updgrp
);
782 static void update_group_add_subgroup(struct update_group
*updgrp
,
783 struct update_subgroup
*subgrp
)
785 if (!updgrp
|| !subgrp
)
788 LIST_INSERT_HEAD(&(updgrp
->subgrps
), subgrp
, updgrp_train
);
789 subgrp
->update_group
= updgrp
;
792 static void update_group_remove_subgroup(struct update_group
*updgrp
,
793 struct update_subgroup
*subgrp
)
795 if (!updgrp
|| !subgrp
)
798 LIST_REMOVE(subgrp
, updgrp_train
);
799 subgrp
->update_group
= NULL
;
800 if (LIST_EMPTY(&(updgrp
->subgrps
)))
801 update_group_delete(updgrp
);
804 static struct update_subgroup
*
805 update_subgroup_create(struct update_group
*updgrp
)
807 struct update_subgroup
*subgrp
;
809 subgrp
= XCALLOC(MTYPE_BGP_UPD_SUBGRP
, sizeof(struct update_subgroup
));
810 update_subgroup_checkin(subgrp
, updgrp
);
811 subgrp
->v_coalesce
= (UPDGRP_INST(updgrp
))->coalesce_time
;
812 sync_init(subgrp
, updgrp
);
813 bpacket_queue_init(SUBGRP_PKTQ(subgrp
));
814 bpacket_queue_add(SUBGRP_PKTQ(subgrp
), NULL
, NULL
);
815 TAILQ_INIT(&(subgrp
->adjq
));
816 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
817 zlog_debug("create subgroup u%" PRIu64
":s%" PRIu64
, updgrp
->id
,
820 update_group_add_subgroup(updgrp
, subgrp
);
822 UPDGRP_INCR_STAT(updgrp
, subgrps_created
);
827 static void update_subgroup_delete(struct update_subgroup
*subgrp
)
832 if (subgrp
->update_group
)
833 UPDGRP_INCR_STAT(subgrp
->update_group
, subgrps_deleted
);
835 THREAD_OFF(subgrp
->t_merge_check
);
836 THREAD_OFF(subgrp
->t_coalesce
);
838 bpacket_queue_cleanup(SUBGRP_PKTQ(subgrp
));
839 subgroup_clear_table(subgrp
);
843 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
) && subgrp
->update_group
)
844 zlog_debug("delete subgroup u%" PRIu64
":s%" PRIu64
,
845 subgrp
->update_group
->id
, subgrp
->id
);
847 update_group_remove_subgroup(subgrp
->update_group
, subgrp
);
849 XFREE(MTYPE_BGP_UPD_SUBGRP
, subgrp
);
852 void update_subgroup_inherit_info(struct update_subgroup
*to
,
853 struct update_subgroup
*from
)
858 to
->sflags
= from
->sflags
;
862 * update_subgroup_check_delete
864 * Delete a subgroup if it is ready to be deleted.
866 * Returns true if the subgroup was deleted.
868 static bool update_subgroup_check_delete(struct update_subgroup
*subgrp
)
873 if (!LIST_EMPTY(&(subgrp
->peers
)))
876 update_subgroup_delete(subgrp
);
882 * update_subgroup_add_peer
884 * @param send_enqueued_packets If true all currently enqueued packets will
885 * also be sent to the peer.
887 static void update_subgroup_add_peer(struct update_subgroup
*subgrp
,
889 int send_enqueued_pkts
)
896 LIST_INSERT_HEAD(&(subgrp
->peers
), paf
, subgrp_train
);
897 paf
->subgroup
= subgrp
;
898 subgrp
->peer_count
++;
900 if (bgp_debug_peer_updout_enabled(paf
->peer
->host
)) {
901 UPDGRP_PEER_DBG_EN(subgrp
->update_group
);
904 SUBGRP_INCR_STAT(subgrp
, join_events
);
906 if (send_enqueued_pkts
) {
907 pkt
= bpacket_queue_first(SUBGRP_PKTQ(subgrp
));
911 * Hang the peer off of the last, placeholder, packet in the
912 * queue. This means it won't see any of the packets that are
913 * currently the queue.
915 pkt
= bpacket_queue_last(SUBGRP_PKTQ(subgrp
));
916 assert(pkt
->buffer
== NULL
);
919 bpacket_add_peer(pkt
, paf
);
921 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
922 zlog_debug("peer %s added to subgroup s%" PRIu64
,
923 paf
->peer
->host
, subgrp
->id
);
927 * update_subgroup_remove_peer_internal
929 * Internal function that removes a peer from a subgroup, but does not
930 * delete the subgroup. A call to this function must almost always be
931 * followed by a call to update_subgroup_check_delete().
933 * @see update_subgroup_remove_peer
935 static void update_subgroup_remove_peer_internal(struct update_subgroup
*subgrp
,
938 assert(subgrp
&& paf
&& subgrp
->update_group
);
940 if (bgp_debug_peer_updout_enabled(paf
->peer
->host
)) {
941 UPDGRP_PEER_DBG_DIS(subgrp
->update_group
);
944 bpacket_queue_remove_peer(paf
);
945 LIST_REMOVE(paf
, subgrp_train
);
946 paf
->subgroup
= NULL
;
947 subgrp
->peer_count
--;
949 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
950 zlog_debug("peer %s deleted from subgroup s%"
951 PRIu64
" peer cnt %d",
952 paf
->peer
->host
, subgrp
->id
, subgrp
->peer_count
);
953 SUBGRP_INCR_STAT(subgrp
, prune_events
);
957 * update_subgroup_remove_peer
959 void update_subgroup_remove_peer(struct update_subgroup
*subgrp
,
965 update_subgroup_remove_peer_internal(subgrp
, paf
);
967 if (update_subgroup_check_delete(subgrp
))
971 * The deletion of the peer may have caused some packets to be
972 * deleted from the subgroup packet queue. Check if the subgroup can
975 update_subgroup_check_merge(subgrp
, "removed peer from subgroup");
978 static struct update_subgroup
*update_subgroup_find(struct update_group
*updgrp
,
981 struct update_subgroup
*subgrp
= NULL
;
990 if (!peer_established(PAF_PEER(paf
)))
993 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
) {
994 if (subgrp
->version
!= version
995 || CHECK_FLAG(subgrp
->sflags
,
996 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
1000 * The version number is not meaningful on a subgroup that needs
1003 if (update_subgroup_needs_refresh(subgrp
))
1013 * update_subgroup_ready_for_merge
1015 * Returns true if this subgroup is in a state that allows it to be
1016 * merged into another subgroup.
1018 static bool update_subgroup_ready_for_merge(struct update_subgroup
*subgrp
)
1022 * Not ready if there are any encoded packets waiting to be written
1025 if (!bpacket_queue_is_empty(SUBGRP_PKTQ(subgrp
)))
1029 * Not ready if there enqueued updates waiting to be encoded.
1031 if (!advertise_list_is_empty(subgrp
))
1035 * Don't attempt to merge a subgroup that needs a refresh. For one,
1036 * we can't determine if the adj_out of such a group matches that of
1039 if (update_subgroup_needs_refresh(subgrp
))
1046 * update_subgrp_can_merge_into
1048 * Returns true if the first subgroup can merge into the second
1051 static int update_subgroup_can_merge_into(struct update_subgroup
*subgrp
,
1052 struct update_subgroup
*target
)
1055 if (subgrp
== target
)
1059 * Both must have processed the BRIB to the same point in order to
1062 if (subgrp
->version
!= target
->version
)
1065 if (CHECK_FLAG(subgrp
->sflags
, SUBGRP_STATUS_DEFAULT_ORIGINATE
)
1066 != CHECK_FLAG(target
->sflags
, SUBGRP_STATUS_DEFAULT_ORIGINATE
))
1069 if (subgrp
->adj_count
!= target
->adj_count
)
1072 return update_subgroup_ready_for_merge(target
);
1076 * update_subgroup_merge
1078 * Merge the first subgroup into the second one.
1080 static void update_subgroup_merge(struct update_subgroup
*subgrp
,
1081 struct update_subgroup
*target
,
1084 struct peer_af
*paf
;
1088 assert(subgrp
->adj_count
== target
->adj_count
);
1090 peer_count
= subgrp
->peer_count
;
1093 paf
= LIST_FIRST(&subgrp
->peers
);
1097 update_subgroup_remove_peer_internal(subgrp
, paf
);
1100 * Add the peer to the target subgroup, while making sure that
1101 * any currently enqueued packets won't be sent to it. Enqueued
1102 * packets could, for example, result in an unnecessary withdraw
1103 * followed by an advertise.
1105 update_subgroup_add_peer(target
, paf
, 0);
1108 SUBGRP_INCR_STAT(target
, merge_events
);
1110 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
1111 zlog_debug("u%" PRIu64
":s%" PRIu64
" (%d peers) merged into u%" PRIu64
":s%" PRIu64
", trigger: %s",
1112 subgrp
->update_group
->id
, subgrp
->id
, peer_count
,
1113 target
->update_group
->id
, target
->id
,
1114 reason
? reason
: "unknown");
1116 result
= update_subgroup_check_delete(subgrp
);
1121 * update_subgroup_check_merge
1123 * Merge this subgroup into another subgroup if possible.
1125 * Returns true if the subgroup has been merged. The subgroup pointer
1126 * should not be accessed in this case.
1128 bool update_subgroup_check_merge(struct update_subgroup
*subgrp
,
1131 struct update_subgroup
*target
;
1133 if (!update_subgroup_ready_for_merge(subgrp
))
1137 * Look for a subgroup to merge into.
1139 UPDGRP_FOREACH_SUBGRP (subgrp
->update_group
, target
) {
1140 if (update_subgroup_can_merge_into(subgrp
, target
))
1147 update_subgroup_merge(subgrp
, target
, reason
);
1152 * update_subgroup_merge_check_thread_cb
1154 static int update_subgroup_merge_check_thread_cb(struct thread
*thread
)
1156 struct update_subgroup
*subgrp
;
1158 subgrp
= THREAD_ARG(thread
);
1160 subgrp
->t_merge_check
= NULL
;
1162 update_subgroup_check_merge(subgrp
, "triggered merge check");
1167 * update_subgroup_trigger_merge_check
1169 * Triggers a call to update_subgroup_check_merge() on a clean context.
1171 * @param force If true, the merge check will be triggered even if the
1172 * subgroup doesn't currently look ready for a merge.
1174 * Returns true if a merge check will be performed shortly.
1176 bool update_subgroup_trigger_merge_check(struct update_subgroup
*subgrp
,
1179 if (subgrp
->t_merge_check
)
1182 if (!force
&& !update_subgroup_ready_for_merge(subgrp
))
1185 subgrp
->t_merge_check
= NULL
;
1186 thread_add_timer_msec(bm
->master
, update_subgroup_merge_check_thread_cb
,
1187 subgrp
, 0, &subgrp
->t_merge_check
);
1189 SUBGRP_INCR_STAT(subgrp
, merge_checks_triggered
);
1195 * update_subgroup_copy_adj_out
1197 * Helper function that clones the adj out (state about advertised
1198 * routes) from one subgroup to another. It assumes that the adj out
1199 * of the target subgroup is empty.
1201 static void update_subgroup_copy_adj_out(struct update_subgroup
*source
,
1202 struct update_subgroup
*dest
)
1204 struct bgp_adj_out
*aout
, *aout_copy
;
1206 SUBGRP_FOREACH_ADJ (source
, aout
) {
1210 aout_copy
= bgp_adj_out_alloc(dest
, aout
->dest
,
1211 aout
->addpath_tx_id
);
1213 aout
->attr
? bgp_attr_intern(aout
->attr
) : NULL
;
1216 dest
->scount
= source
->scount
;
1220 * update_subgroup_copy_packets
1222 * Copy packets after and including the given packet to the subgroup
1225 * Returns the number of packets copied.
1227 static int update_subgroup_copy_packets(struct update_subgroup
*dest
,
1228 struct bpacket
*pkt
)
1233 while (pkt
&& pkt
->buffer
) {
1234 bpacket_queue_add(SUBGRP_PKTQ(dest
), stream_dup(pkt
->buffer
),
1237 pkt
= bpacket_next(pkt
);
1243 static bool updgrp_prefix_list_update(struct update_group
*updgrp
,
1247 struct bgp_filter
*filter
;
1249 peer
= UPDGRP_PEER(updgrp
);
1250 filter
= &peer
->filter
[UPDGRP_AFI(updgrp
)][UPDGRP_SAFI(updgrp
)];
1252 if (PREFIX_LIST_OUT_NAME(filter
)
1253 && (strcmp(name
, PREFIX_LIST_OUT_NAME(filter
)) == 0)) {
1254 PREFIX_LIST_OUT(filter
) = prefix_list_lookup(
1255 UPDGRP_AFI(updgrp
), PREFIX_LIST_OUT_NAME(filter
));
1261 static bool updgrp_filter_list_update(struct update_group
*updgrp
,
1265 struct bgp_filter
*filter
;
1267 peer
= UPDGRP_PEER(updgrp
);
1268 filter
= &peer
->filter
[UPDGRP_AFI(updgrp
)][UPDGRP_SAFI(updgrp
)];
1270 if (FILTER_LIST_OUT_NAME(filter
)
1271 && (strcmp(name
, FILTER_LIST_OUT_NAME(filter
)) == 0)) {
1272 FILTER_LIST_OUT(filter
) =
1273 as_list_lookup(FILTER_LIST_OUT_NAME(filter
));
1279 static bool updgrp_distribute_list_update(struct update_group
*updgrp
,
1283 struct bgp_filter
*filter
;
1285 peer
= UPDGRP_PEER(updgrp
);
1286 filter
= &peer
->filter
[UPDGRP_AFI(updgrp
)][UPDGRP_SAFI(updgrp
)];
1288 if (DISTRIBUTE_OUT_NAME(filter
)
1289 && (strcmp(name
, DISTRIBUTE_OUT_NAME(filter
)) == 0)) {
1290 DISTRIBUTE_OUT(filter
) = access_list_lookup(
1291 UPDGRP_AFI(updgrp
), DISTRIBUTE_OUT_NAME(filter
));
1297 static int updgrp_route_map_update(struct update_group
*updgrp
,
1298 const char *name
, int *def_rmap_changed
)
1301 struct bgp_filter
*filter
;
1306 peer
= UPDGRP_PEER(updgrp
);
1307 afi
= UPDGRP_AFI(updgrp
);
1308 safi
= UPDGRP_SAFI(updgrp
);
1309 filter
= &peer
->filter
[afi
][safi
];
1311 if (ROUTE_MAP_OUT_NAME(filter
)
1312 && (strcmp(name
, ROUTE_MAP_OUT_NAME(filter
)) == 0)) {
1313 ROUTE_MAP_OUT(filter
) = route_map_lookup_by_name(name
);
1318 if (UNSUPPRESS_MAP_NAME(filter
)
1319 && (strcmp(name
, UNSUPPRESS_MAP_NAME(filter
)) == 0)) {
1320 UNSUPPRESS_MAP(filter
) = route_map_lookup_by_name(name
);
1324 /* process default-originate route-map */
1325 if (peer
->default_rmap
[afi
][safi
].name
1326 && (strcmp(name
, peer
->default_rmap
[afi
][safi
].name
) == 0)) {
1327 peer
->default_rmap
[afi
][safi
].map
=
1328 route_map_lookup_by_name(name
);
1329 if (def_rmap_changed
)
1330 *def_rmap_changed
= 1;
1336 * hash iteration callback function to process a policy change for an
1337 * update group. Check if the changed policy matches the updgrp's
1338 * outbound route-map or unsuppress-map or default-originate map or
1339 * filter-list or prefix-list or distribute-list.
1340 * Trigger update generation accordingly.
1342 static int updgrp_policy_update_walkcb(struct update_group
*updgrp
, void *arg
)
1344 struct updwalk_context
*ctx
= arg
;
1345 struct update_subgroup
*subgrp
;
1347 int def_changed
= 0;
1349 if (!updgrp
|| !ctx
|| !ctx
->policy_name
)
1350 return UPDWALK_CONTINUE
;
1352 switch (ctx
->policy_type
) {
1353 case BGP_POLICY_ROUTE_MAP
:
1354 changed
= updgrp_route_map_update(updgrp
, ctx
->policy_name
,
1357 case BGP_POLICY_FILTER_LIST
:
1358 changed
= updgrp_filter_list_update(updgrp
, ctx
->policy_name
);
1360 case BGP_POLICY_PREFIX_LIST
:
1361 changed
= updgrp_prefix_list_update(updgrp
, ctx
->policy_name
);
1363 case BGP_POLICY_DISTRIBUTE_LIST
:
1365 updgrp_distribute_list_update(updgrp
, ctx
->policy_name
);
1371 /* If not doing route update, return after updating "config" */
1372 if (!ctx
->policy_route_update
)
1373 return UPDWALK_CONTINUE
;
1375 /* If nothing has changed, return after updating "config" */
1376 if (!changed
&& !def_changed
)
1377 return UPDWALK_CONTINUE
;
1380 * If something has changed, at the beginning of a route-map
1382 * event, mark each subgroup's needs-refresh bit. For one, it signals to
1383 * whoever that the subgroup needs a refresh. Second, it prevents
1385 * merge of this subgroup with another before a complete (outbound)
1388 if (ctx
->policy_event_start_flag
) {
1389 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
) {
1390 update_subgroup_set_needs_refresh(subgrp
, 1);
1392 return UPDWALK_CONTINUE
;
1395 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
) {
1396 /* Avoid supressing duplicate routes later
1397 * when processing in subgroup_announce_table().
1399 SET_FLAG(subgrp
->sflags
, SUBGRP_STATUS_FORCE_UPDATES
);
1402 if (bgp_debug_update(NULL
, NULL
, updgrp
, 0))
1404 "u%" PRIu64
":s%" PRIu64
" announcing routes upon policy %s (type %d) change",
1405 updgrp
->id
, subgrp
->id
,
1406 ctx
->policy_name
, ctx
->policy_type
);
1407 subgroup_announce_route(subgrp
);
1410 if (bgp_debug_update(NULL
, NULL
, updgrp
, 0))
1412 "u%" PRIu64
":s%" PRIu64
" announcing default upon default routemap %s change",
1413 updgrp
->id
, subgrp
->id
,
1415 subgroup_default_originate(subgrp
, 0);
1417 update_subgroup_set_needs_refresh(subgrp
, 0);
1419 return UPDWALK_CONTINUE
;
1422 static int update_group_walkcb(struct hash_bucket
*bucket
, void *arg
)
1424 struct update_group
*updgrp
= bucket
->data
;
1425 struct updwalk_context
*wctx
= arg
;
1426 int ret
= (*wctx
->cb
)(updgrp
, wctx
->context
);
1430 static int update_group_periodic_merge_walkcb(struct update_group
*updgrp
,
1433 struct update_subgroup
*subgrp
;
1434 struct update_subgroup
*tmp_subgrp
;
1435 const char *reason
= arg
;
1437 UPDGRP_FOREACH_SUBGRP_SAFE (updgrp
, subgrp
, tmp_subgrp
)
1438 update_subgroup_check_merge(subgrp
, reason
);
1439 return UPDWALK_CONTINUE
;
1442 /********************
1444 ********************/
1447 * trigger function when a policy (route-map/filter-list/prefix-list/
1448 * distribute-list etc.) content changes. Go through all the
1449 * update groups and process the change.
1451 * bgp: the bgp instance
1452 * ptype: the type of policy that got modified, see bgpd.h
1453 * pname: name of the policy
1454 * route_update: flag to control if an automatic update generation should
1456 * start_event: flag that indicates if it's the beginning of the change.
1457 * Esp. when the user is changing the content interactively
1458 * over multiple statements. Useful to set dirty flag on
1461 void update_group_policy_update(struct bgp
*bgp
, bgp_policy_type_e ptype
,
1462 const char *pname
, int route_update
,
1465 struct updwalk_context ctx
;
1467 memset(&ctx
, 0, sizeof(ctx
));
1468 ctx
.policy_type
= ptype
;
1469 ctx
.policy_name
= pname
;
1470 ctx
.policy_route_update
= route_update
;
1471 ctx
.policy_event_start_flag
= start_event
;
1474 update_group_walk(bgp
, updgrp_policy_update_walkcb
, &ctx
);
1478 * update_subgroup_split_peer
1480 * Ensure that the given peer is in a subgroup of its own in the
1481 * specified update group.
1483 void update_subgroup_split_peer(struct peer_af
*paf
,
1484 struct update_group
*updgrp
)
1486 struct update_subgroup
*old_subgrp
, *subgrp
;
1490 old_subgrp
= paf
->subgroup
;
1493 updgrp
= old_subgrp
->update_group
;
1496 * If the peer is alone in its subgroup, reuse the existing
1499 if (old_subgrp
->peer_count
== 1) {
1500 if (updgrp
== old_subgrp
->update_group
)
1503 subgrp
= old_subgrp
;
1504 old_id
= old_subgrp
->update_group
->id
;
1506 if (bgp_debug_peer_updout_enabled(paf
->peer
->host
)) {
1507 UPDGRP_PEER_DBG_DIS(old_subgrp
->update_group
);
1510 update_group_remove_subgroup(old_subgrp
->update_group
,
1512 update_group_add_subgroup(updgrp
, subgrp
);
1514 if (bgp_debug_peer_updout_enabled(paf
->peer
->host
)) {
1515 UPDGRP_PEER_DBG_EN(updgrp
);
1517 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
1518 zlog_debug("u%" PRIu64
":s%" PRIu64
" peer %s moved to u%" PRIu64
":s%" PRIu64
,
1519 old_id
, subgrp
->id
, paf
->peer
->host
,
1520 updgrp
->id
, subgrp
->id
);
1523 * The state of the subgroup (adj_out, advs, packet queue etc)
1524 * is consistent internally, but may not be identical to other
1525 * subgroups in the new update group even if the version number
1526 * matches up. Make sure a full refresh is done before the
1527 * subgroup is merged with another.
1529 update_subgroup_set_needs_refresh(subgrp
, 1);
1531 SUBGRP_INCR_STAT(subgrp
, updgrp_switch_events
);
1536 * Create a new subgroup under the specified update group, and copy
1537 * over relevant state to it.
1539 subgrp
= update_subgroup_create(updgrp
);
1540 update_subgroup_inherit_info(subgrp
, old_subgrp
);
1542 subgrp
->split_from
.update_group_id
= old_subgrp
->update_group
->id
;
1543 subgrp
->split_from
.subgroup_id
= old_subgrp
->id
;
1546 * Copy out relevant state from the old subgroup.
1548 update_subgroup_copy_adj_out(paf
->subgroup
, subgrp
);
1549 update_subgroup_copy_packets(subgrp
, paf
->next_pkt_to_send
);
1551 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
1552 zlog_debug("u%" PRIu64
":s%" PRIu64
" peer %s split and moved into u%" PRIu64
":s%" PRIu64
,
1553 paf
->subgroup
->update_group
->id
, paf
->subgroup
->id
,
1554 paf
->peer
->host
, updgrp
->id
, subgrp
->id
);
1556 SUBGRP_INCR_STAT(paf
->subgroup
, split_events
);
1559 * Since queued advs were left behind, this new subgroup needs a
1562 update_subgroup_set_needs_refresh(subgrp
, 1);
1565 * Remove peer from old subgroup, and add it to the new one.
1567 update_subgroup_remove_peer(paf
->subgroup
, paf
);
1569 update_subgroup_add_peer(subgrp
, paf
, 1);
1572 void update_bgp_group_init(struct bgp
*bgp
)
1577 bgp
->update_groups
[afid
] =
1578 hash_create(updgrp_hash_key_make
, updgrp_hash_cmp
,
1579 "BGP Update Group Hash");
1582 void update_bgp_group_free(struct bgp
*bgp
)
1587 if (bgp
->update_groups
[afid
]) {
1588 hash_free(bgp
->update_groups
[afid
]);
1589 bgp
->update_groups
[afid
] = NULL
;
1594 void update_group_show(struct bgp
*bgp
, afi_t afi
, safi_t safi
, struct vty
*vty
,
1597 struct updwalk_context ctx
;
1598 memset(&ctx
, 0, sizeof(ctx
));
1600 ctx
.subgrp_id
= subgrp_id
;
1602 update_group_af_walk(bgp
, afi
, safi
, update_group_show_walkcb
, &ctx
);
1606 * update_group_show_stats
1608 * Show global statistics about update groups.
1610 void update_group_show_stats(struct bgp
*bgp
, struct vty
*vty
)
1612 vty_out(vty
, "Update groups created: %u\n",
1613 bgp
->update_group_stats
.updgrps_created
);
1614 vty_out(vty
, "Update groups deleted: %u\n",
1615 bgp
->update_group_stats
.updgrps_deleted
);
1616 vty_out(vty
, "Update subgroups created: %u\n",
1617 bgp
->update_group_stats
.subgrps_created
);
1618 vty_out(vty
, "Update subgroups deleted: %u\n",
1619 bgp
->update_group_stats
.subgrps_deleted
);
1620 vty_out(vty
, "Join events: %u\n", bgp
->update_group_stats
.join_events
);
1621 vty_out(vty
, "Prune events: %u\n",
1622 bgp
->update_group_stats
.prune_events
);
1623 vty_out(vty
, "Merge events: %u\n",
1624 bgp
->update_group_stats
.merge_events
);
1625 vty_out(vty
, "Split events: %u\n",
1626 bgp
->update_group_stats
.split_events
);
1627 vty_out(vty
, "Update group switch events: %u\n",
1628 bgp
->update_group_stats
.updgrp_switch_events
);
1629 vty_out(vty
, "Peer route refreshes combined: %u\n",
1630 bgp
->update_group_stats
.peer_refreshes_combined
);
1631 vty_out(vty
, "Merge checks triggered: %u\n",
1632 bgp
->update_group_stats
.merge_checks_triggered
);
1636 * update_group_adjust_peer
1638 void update_group_adjust_peer(struct peer_af
*paf
)
1640 struct update_group
*updgrp
;
1641 struct update_subgroup
*subgrp
, *old_subgrp
;
1647 peer
= PAF_PEER(paf
);
1648 if (!peer_established(peer
)) {
1652 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)) {
1656 if (!peer
->afc_nego
[paf
->afi
][paf
->safi
]) {
1660 updgrp
= update_group_find(paf
);
1662 updgrp
= update_group_create(paf
);
1664 flog_err(EC_BGP_UPDGRP_CREATE
,
1665 "couldn't create update group for peer %s",
1671 old_subgrp
= paf
->subgroup
;
1676 * If the update group of the peer is unchanged, the peer can
1678 * in its existing subgroup and we're done.
1680 if (old_subgrp
->update_group
== updgrp
)
1684 * The peer is switching between update groups. Put it in its
1685 * own subgroup under the new update group.
1687 update_subgroup_split_peer(paf
, updgrp
);
1691 subgrp
= update_subgroup_find(updgrp
, paf
);
1693 subgrp
= update_subgroup_create(updgrp
);
1698 update_subgroup_add_peer(subgrp
, paf
, 1);
1699 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
1700 zlog_debug("u%" PRIu64
":s%" PRIu64
" add peer %s", updgrp
->id
,
1701 subgrp
->id
, paf
->peer
->host
);
1706 int update_group_adjust_soloness(struct peer
*peer
, int set
)
1708 struct peer_group
*group
;
1709 struct listnode
*node
, *nnode
;
1711 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1712 peer_lonesoul_or_not(peer
, set
);
1713 if (peer_established(peer
))
1714 bgp_announce_route_all(peer
);
1716 group
= peer
->group
;
1717 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
1718 peer_lonesoul_or_not(peer
, set
);
1719 if (peer_established(peer
))
1720 bgp_announce_route_all(peer
);
1727 * update_subgroup_rib
1729 struct bgp_table
*update_subgroup_rib(struct update_subgroup
*subgrp
)
1733 bgp
= SUBGRP_INST(subgrp
);
1737 return bgp
->rib
[SUBGRP_AFI(subgrp
)][SUBGRP_SAFI(subgrp
)];
1740 void update_group_af_walk(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1741 updgrp_walkcb cb
, void *ctx
)
1743 struct updwalk_context wctx
;
1748 afid
= afindex(afi
, safi
);
1749 if (afid
>= BGP_AF_MAX
)
1752 memset(&wctx
, 0, sizeof(wctx
));
1756 if (bgp
->update_groups
[afid
])
1757 hash_walk(bgp
->update_groups
[afid
], update_group_walkcb
, &wctx
);
1760 void update_group_walk(struct bgp
*bgp
, updgrp_walkcb cb
, void *ctx
)
1765 FOREACH_AFI_SAFI (afi
, safi
) {
1766 update_group_af_walk(bgp
, afi
, safi
, cb
, ctx
);
1770 void update_group_periodic_merge(struct bgp
*bgp
)
1772 char reason
[] = "periodic merge check";
1774 update_group_walk(bgp
, update_group_periodic_merge_walkcb
,
1779 update_group_default_originate_route_map_walkcb(struct update_group
*updgrp
,
1782 struct update_subgroup
*subgrp
;
1787 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
) {
1788 peer
= SUBGRP_PEER(subgrp
);
1789 afi
= SUBGRP_AFI(subgrp
);
1790 safi
= SUBGRP_SAFI(subgrp
);
1792 if (peer
->default_rmap
[afi
][safi
].name
) {
1793 subgroup_default_originate(subgrp
, 0);
1797 return UPDWALK_CONTINUE
;
1800 int update_group_refresh_default_originate_route_map(struct thread
*thread
)
1803 char reason
[] = "refresh default-originate route-map";
1805 bgp
= THREAD_ARG(thread
);
1806 update_group_walk(bgp
, update_group_default_originate_route_map_walkcb
,
1808 thread_cancel(&bgp
->t_rmap_def_originate_eval
);
1815 * peer_af_announce_route
1817 * Refreshes routes out to a peer_af immediately.
1819 * If the combine parameter is true, then this function will try to
1820 * gather other peers in the subgroup for which a route announcement
1821 * is pending and efficently announce routes to all of them.
1823 * For now, the 'combine' option has an effect only if all peers in
1824 * the subgroup have a route announcement pending.
1826 void peer_af_announce_route(struct peer_af
*paf
, int combine
)
1828 struct update_subgroup
*subgrp
;
1829 struct peer_af
*cur_paf
;
1832 subgrp
= paf
->subgroup
;
1837 * If there are other peers in the old subgroup that also need
1838 * routes to be announced, pull them into the peer's new
1840 * Combine route announcement with other peers if possible.
1842 * For now, we combine only if all peers in the subgroup have an
1843 * announcement pending.
1847 SUBGRP_FOREACH_PEER (subgrp
, cur_paf
) {
1851 if (cur_paf
->t_announce_route
)
1859 * Announce to the peer alone if we were not asked to combine peers,
1860 * or if some peers don't have a route annoucement pending.
1862 if (!combine
|| !all_pending
) {
1863 update_subgroup_split_peer(paf
, NULL
);
1864 subgrp
= paf
->subgroup
;
1866 assert(subgrp
&& subgrp
->update_group
);
1867 if (bgp_debug_update(paf
->peer
, NULL
, subgrp
->update_group
, 0))
1868 zlog_debug("u%" PRIu64
":s%" PRIu64
" %s announcing routes",
1869 subgrp
->update_group
->id
, subgrp
->id
,
1872 subgroup_announce_route(paf
->subgroup
);
1877 * We will announce routes the entire subgroup.
1879 * First stop refresh timers on all the other peers.
1881 SUBGRP_FOREACH_PEER (subgrp
, cur_paf
) {
1885 bgp_stop_announce_route_timer(cur_paf
);
1888 if (bgp_debug_update(paf
->peer
, NULL
, subgrp
->update_group
, 0))
1889 zlog_debug("u%" PRIu64
":s%" PRIu64
" announcing routes to %s, combined into %d peers",
1890 subgrp
->update_group
->id
, subgrp
->id
,
1891 paf
->peer
->host
, subgrp
->peer_count
);
1893 subgroup_announce_route(subgrp
);
1895 SUBGRP_INCR_STAT_BY(subgrp
, peer_refreshes_combined
,
1896 subgrp
->peer_count
- 1);
1899 void subgroup_trigger_write(struct update_subgroup
*subgrp
)
1901 struct peer_af
*paf
;
1904 * For each peer in the subgroup, schedule a job to pull packets from
1905 * the subgroup output queue into their own output queue. This action
1906 * will trigger a write job on the I/O thread.
1908 SUBGRP_FOREACH_PEER (subgrp
, paf
)
1909 if (peer_established(paf
->peer
))
1910 thread_add_timer_msec(
1911 bm
->master
, bgp_generate_updgrp_packets
,
1913 &paf
->peer
->t_generate_updgrp_packets
);
1916 int update_group_clear_update_dbg(struct update_group
*updgrp
, void *arg
)
1918 UPDGRP_PEER_DBG_OFF(updgrp
);
1919 return UPDWALK_CONTINUE
;
1922 /* Return true if we should addpath encode NLRI to this peer */
1923 int bgp_addpath_encode_tx(struct peer
*peer
, afi_t afi
, safi_t safi
)
1925 return (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_TX_ADV
)
1926 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1927 PEER_CAP_ADDPATH_AF_RX_RCV
));