1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * bgp_updgrp_adv.c: BGP update group advertisement and adjacency
7 * @copyright Copyright (C) 2014 Cumulus Networks, Inc.
9 * @author Avneesh Sachdev <avneesh@sproute.net>
10 * @author Rajesh Varadarajan <rajesh@sproute.net>
11 * @author Pradosh Mohapatra <pradosh@sproute.net>
25 #include "bgpd/bgpd.h"
26 #include "bgpd/bgp_table.h"
27 #include "bgpd/bgp_debug.h"
28 #include "bgpd/bgp_route.h"
29 #include "bgpd/bgp_advertise.h"
30 #include "bgpd/bgp_attr.h"
31 #include "bgpd/bgp_aspath.h"
32 #include "bgpd/bgp_packet.h"
33 #include "bgpd/bgp_fsm.h"
34 #include "bgpd/bgp_mplsvpn.h"
35 #include "bgpd/bgp_updgrp.h"
36 #include "bgpd/bgp_advertise.h"
37 #include "bgpd/bgp_addpath.h"
43 static int bgp_adj_out_compare(const struct bgp_adj_out
*o1
,
44 const struct bgp_adj_out
*o2
)
46 if (o1
->subgroup
< o2
->subgroup
)
49 if (o1
->subgroup
> o2
->subgroup
)
52 if (o1
->addpath_tx_id
< o2
->addpath_tx_id
)
55 if (o1
->addpath_tx_id
> o2
->addpath_tx_id
)
60 RB_GENERATE(bgp_adj_out_rb
, bgp_adj_out
, adj_entry
, bgp_adj_out_compare
);
62 static inline struct bgp_adj_out
*adj_lookup(struct bgp_dest
*dest
,
63 struct update_subgroup
*subgrp
,
64 uint32_t addpath_tx_id
)
66 struct bgp_adj_out lookup
;
71 /* update-groups that do not support addpath will pass 0 for
73 lookup
.subgroup
= subgrp
;
74 lookup
.addpath_tx_id
= addpath_tx_id
;
76 return RB_FIND(bgp_adj_out_rb
, &dest
->adj_out
, &lookup
);
79 static void adj_free(struct bgp_adj_out
*adj
)
81 TAILQ_REMOVE(&(adj
->subgroup
->adjq
), adj
, subgrp_adj_train
);
82 SUBGRP_DECR_STAT(adj
->subgroup
, adj_count
);
84 RB_REMOVE(bgp_adj_out_rb
, &adj
->dest
->adj_out
, adj
);
85 bgp_dest_unlock_node(adj
->dest
);
87 XFREE(MTYPE_BGP_ADJ_OUT
, adj
);
90 static void subgrp_withdraw_stale_addpath(struct updwalk_context
*ctx
,
91 struct update_subgroup
*subgrp
)
93 struct bgp_adj_out
*adj
, *adj_next
;
95 struct bgp_path_info
*pi
;
96 afi_t afi
= SUBGRP_AFI(subgrp
);
97 safi_t safi
= SUBGRP_SAFI(subgrp
);
98 struct peer
*peer
= SUBGRP_PEER(subgrp
);
100 /* Look through all of the paths we have advertised for this rn and send
101 * a withdraw for the ones that are no longer present */
102 RB_FOREACH_SAFE (adj
, bgp_adj_out_rb
, &ctx
->dest
->adj_out
, adj_next
) {
103 if (adj
->subgroup
!= subgrp
)
106 for (pi
= bgp_dest_get_bgp_path_info(ctx
->dest
); pi
;
108 id
= bgp_addpath_id_for_peer(peer
, afi
, safi
,
111 if (id
== adj
->addpath_tx_id
) {
117 subgroup_process_announce_selected(subgrp
, NULL
,
118 ctx
->dest
, afi
, safi
,
124 static int group_announce_route_walkcb(struct update_group
*updgrp
, void *arg
)
126 struct updwalk_context
*ctx
= arg
;
127 struct update_subgroup
*subgrp
;
128 struct bgp_path_info
*pi
;
132 struct bgp_adj_out
*adj
, *adj_next
;
133 bool addpath_capable
;
135 afi
= UPDGRP_AFI(updgrp
);
136 safi
= UPDGRP_SAFI(updgrp
);
137 peer
= UPDGRP_PEER(updgrp
);
138 addpath_capable
= bgp_addpath_encode_tx(peer
, afi
, safi
);
140 if (BGP_DEBUG(update
, UPDATE_OUT
))
141 zlog_debug("%s: afi=%s, safi=%s, p=%pRN", __func__
,
142 afi2str(afi
), safi2str(safi
),
143 bgp_dest_to_rnode(ctx
->dest
));
145 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
) {
148 * Skip the subgroups that have coalesce timer running. We will
149 * walk the entire prefix table for those subgroups when the
150 * coalesce timer fires.
152 if (!subgrp
->t_coalesce
) {
154 /* An update-group that uses addpath */
155 if (addpath_capable
) {
156 subgrp_withdraw_stale_addpath(ctx
, subgrp
);
158 for (pi
= bgp_dest_get_bgp_path_info(ctx
->dest
);
160 /* Skip the bestpath for now */
164 subgroup_process_announce_selected(
165 subgrp
, pi
, ctx
->dest
, afi
,
167 bgp_addpath_id_for_peer(
172 /* Process the bestpath last so the "show [ip]
173 * bgp neighbor x.x.x.x advertised"
174 * output shows the attributes from the bestpath
177 subgroup_process_announce_selected(
178 subgrp
, ctx
->pi
, ctx
->dest
, afi
,
180 bgp_addpath_id_for_peer(
182 &ctx
->pi
->tx_addpath
));
184 /* An update-group that does not use addpath */
187 subgroup_process_announce_selected(
188 subgrp
, ctx
->pi
, ctx
->dest
, afi
,
190 bgp_addpath_id_for_peer(
192 &ctx
->pi
->tx_addpath
));
194 /* Find the addpath_tx_id of the path we
197 RB_FOREACH_SAFE (adj
, bgp_adj_out_rb
,
200 if (adj
->subgroup
== subgrp
) {
201 subgroup_process_announce_selected(
212 /* Notify BGP Conditional advertisement */
213 bgp_notify_conditional_adv_scanner(subgrp
);
216 return UPDWALK_CONTINUE
;
219 static void subgrp_show_adjq_vty(struct update_subgroup
*subgrp
,
220 struct vty
*vty
, uint8_t flags
)
222 struct bgp_table
*table
;
223 struct bgp_adj_out
*adj
;
224 unsigned long output_count
;
225 struct bgp_dest
*dest
;
230 bgp
= SUBGRP_INST(subgrp
);
234 table
= bgp
->rib
[SUBGRP_AFI(subgrp
)][SUBGRP_SAFI(subgrp
)];
238 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
239 const struct prefix
*dest_p
= bgp_dest_get_prefix(dest
);
241 RB_FOREACH (adj
, bgp_adj_out_rb
, &dest
->adj_out
) {
242 if (adj
->subgroup
!= subgrp
)
247 "BGP table version is %" PRIu64
248 ", local router ID is %pI4\n",
249 table
->version
, &bgp
->router_id
);
250 vty_out(vty
, BGP_SHOW_SCODE_HEADER
);
251 vty_out(vty
, BGP_SHOW_OCODE_HEADER
);
255 vty_out(vty
, BGP_SHOW_HEADER
);
258 if ((flags
& UPDWALK_FLAGS_ADVQUEUE
) && adj
->adv
&&
261 vty
, dest
, dest_p
, adj
->adv
->baa
->attr
,
262 SUBGRP_SAFI(subgrp
), 0, NULL
, false);
265 if ((flags
& UPDWALK_FLAGS_ADVERTISED
) && adj
->attr
) {
266 route_vty_out_tmp(vty
, dest
, dest_p
, adj
->attr
,
267 SUBGRP_SAFI(subgrp
), 0, NULL
,
273 if (output_count
!= 0)
274 vty_out(vty
, "\nTotal number of prefixes %ld\n", output_count
);
277 static int updgrp_show_adj_walkcb(struct update_group
*updgrp
, void *arg
)
279 struct updwalk_context
*ctx
= arg
;
280 struct update_subgroup
*subgrp
;
284 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
) {
285 if (ctx
->subgrp_id
&& (ctx
->subgrp_id
!= subgrp
->id
))
287 vty_out(vty
, "update group %" PRIu64
", subgroup %" PRIu64
"\n",
288 updgrp
->id
, subgrp
->id
);
289 subgrp_show_adjq_vty(subgrp
, vty
, ctx
->flags
);
291 return UPDWALK_CONTINUE
;
294 static void updgrp_show_adj(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
295 struct vty
*vty
, uint64_t id
, uint8_t flags
)
297 struct updwalk_context ctx
;
298 memset(&ctx
, 0, sizeof(ctx
));
303 update_group_af_walk(bgp
, afi
, safi
, updgrp_show_adj_walkcb
, &ctx
);
306 static void subgroup_coalesce_timer(struct event
*thread
)
308 struct update_subgroup
*subgrp
;
311 subgrp
= EVENT_ARG(thread
);
312 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
313 zlog_debug("u%" PRIu64
":s%" PRIu64
" announcing routes upon coalesce timer expiry(%u ms)",
314 (SUBGRP_UPDGRP(subgrp
))->id
, subgrp
->id
,
316 subgrp
->t_coalesce
= NULL
;
317 subgrp
->v_coalesce
= 0;
318 bgp
= SUBGRP_INST(subgrp
);
319 subgroup_announce_route(subgrp
);
322 /* While the announce_route() may kick off the route advertisement timer
324 * the members of the subgroup, we'd like to send the initial updates
326 * faster (i.e., without enforcing MRAI). Also, if there were no routes
328 * announce, this is the method currently employed to trigger the EOR.
330 if (!bgp_update_delay_active(SUBGRP_INST(subgrp
)) &&
331 !(BGP_SUPPRESS_FIB_ENABLED(bgp
))) {
335 SUBGRP_FOREACH_PEER (subgrp
, paf
) {
336 peer
= PAF_PEER(paf
);
337 EVENT_OFF(peer
->t_routeadv
);
338 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
343 static int update_group_announce_walkcb(struct update_group
*updgrp
, void *arg
)
345 struct update_subgroup
*subgrp
;
347 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
) {
348 /* Avoid supressing duplicate routes later
349 * when processing in subgroup_announce_table().
351 SET_FLAG(subgrp
->sflags
, SUBGRP_STATUS_FORCE_UPDATES
);
353 subgroup_announce_all(subgrp
);
356 return UPDWALK_CONTINUE
;
359 static int update_group_announce_rrc_walkcb(struct update_group
*updgrp
,
362 struct update_subgroup
*subgrp
;
367 afi
= UPDGRP_AFI(updgrp
);
368 safi
= UPDGRP_SAFI(updgrp
);
369 peer
= UPDGRP_PEER(updgrp
);
371 /* Only announce if this is a group of route-reflector-clients */
372 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_REFLECTOR_CLIENT
)) {
373 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
) {
374 subgroup_announce_all(subgrp
);
378 return UPDWALK_CONTINUE
;
381 /********************
383 ********************/
386 * Allocate an adj-out object. Do proper initialization of its fields,
387 * primarily its association with the subgroup and the prefix.
389 struct bgp_adj_out
*bgp_adj_out_alloc(struct update_subgroup
*subgrp
,
390 struct bgp_dest
*dest
,
391 uint32_t addpath_tx_id
)
393 struct bgp_adj_out
*adj
;
395 adj
= XCALLOC(MTYPE_BGP_ADJ_OUT
, sizeof(struct bgp_adj_out
));
396 adj
->subgroup
= subgrp
;
397 adj
->addpath_tx_id
= addpath_tx_id
;
399 RB_INSERT(bgp_adj_out_rb
, &dest
->adj_out
, adj
);
400 bgp_dest_lock_node(dest
);
403 TAILQ_INSERT_TAIL(&(subgrp
->adjq
), adj
, subgrp_adj_train
);
404 SUBGRP_INCR_STAT(subgrp
, adj_count
);
409 struct bgp_advertise
*
410 bgp_advertise_clean_subgroup(struct update_subgroup
*subgrp
,
411 struct bgp_adj_out
*adj
)
413 struct bgp_advertise
*adv
;
414 struct bgp_advertise_attr
*baa
;
415 struct bgp_advertise
*next
;
416 struct bgp_adv_fifo_head
*fhead
;
423 fhead
= &subgrp
->sync
->update
;
425 /* Unlink myself from advertise attribute FIFO. */
426 bgp_advertise_delete(baa
, adv
);
428 /* Fetch next advertise candidate. */
431 /* Unintern BGP advertise attribute. */
432 bgp_advertise_attr_unintern(subgrp
->hash
, baa
);
434 fhead
= &subgrp
->sync
->withdraw
;
437 /* Unlink myself from advertisement FIFO. */
438 bgp_adv_fifo_del(fhead
, adv
);
441 bgp_advertise_free(adj
->adv
);
447 void bgp_adj_out_set_subgroup(struct bgp_dest
*dest
,
448 struct update_subgroup
*subgrp
, struct attr
*attr
,
449 struct bgp_path_info
*path
)
451 struct bgp_adj_out
*adj
= NULL
;
452 struct bgp_advertise
*adv
;
456 struct peer
*adv_peer
;
459 uint32_t attr_hash
= attrhash_key_make(attr
);
461 peer
= SUBGRP_PEER(subgrp
);
462 afi
= SUBGRP_AFI(subgrp
);
463 safi
= SUBGRP_SAFI(subgrp
);
464 bgp
= SUBGRP_INST(subgrp
);
466 if (DISABLE_BGP_ANNOUNCE
)
469 /* Look for adjacency information. */
472 bgp_addpath_id_for_peer(peer
, afi
, safi
, &path
->tx_addpath
));
475 if (CHECK_FLAG(subgrp
->sflags
, SUBGRP_STATUS_TABLE_REPARSING
))
478 adj
= bgp_adj_out_alloc(
480 bgp_addpath_id_for_peer(peer
, afi
, safi
,
488 /* Check if we are sending the same route. This is needed to
489 * avoid duplicate UPDATES. For instance, filtering communities
490 * at egress, neighbors will see duplicate UPDATES despite
491 * the route wasn't changed actually.
492 * Do not suppress BGP UPDATES for route-refresh.
494 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_SUPPRESS_DUPLICATES
)
495 && !CHECK_FLAG(subgrp
->sflags
, SUBGRP_STATUS_FORCE_UPDATES
)
496 && adj
->attr_hash
== attr_hash
) {
497 if (BGP_DEBUG(update
, UPDATE_OUT
)) {
498 char attr_str
[BUFSIZ
] = {0};
500 bgp_dump_attr(attr
, attr_str
, sizeof(attr_str
));
502 zlog_debug("%s suppress UPDATE w/ attr: %s", peer
->host
,
507 * If BGP is skipping sending this value to it's peers
508 * the version number should be updated just like it
509 * would if it sent the data. Why? Because update
510 * groups will not be coalesced until such time that
511 * the version numbers are the same.
513 * Imagine a scenario with say 2 peers and they come
514 * up and are placed in the same update group. Then
515 * a new peer comes up a bit later. Then a prefix is
516 * flapped that we decide for the first 2 peers are
517 * mapped to and we decide not to send the data to
518 * it. Then unless more network changes happen we
519 * will never be able to coalesce the 3rd peer down
521 subgrp
->version
= MAX(subgrp
->version
, dest
->version
);
526 bgp_advertise_clean_subgroup(subgrp
, adj
);
527 adj
->adv
= bgp_advertise_new();
531 assert(adv
->pathi
== NULL
);
532 /* bgp_path_info adj_out reference */
533 adv
->pathi
= bgp_path_info_lock(path
);
535 adv
->baa
= bgp_advertise_attr_intern(subgrp
->hash
, attr
);
537 adj
->attr_hash
= attr_hash
;
539 /* Add new advertisement to advertisement attribute list. */
540 bgp_advertise_add(adv
->baa
, adv
);
543 * If the update adv list is empty, trigger the member peers'
544 * mrai timers so the socket writes can happen.
546 if (!bgp_adv_fifo_count(&subgrp
->sync
->update
)) {
547 SUBGRP_FOREACH_PEER (subgrp
, paf
) {
548 /* If there are no routes in the withdraw list, set
549 * the flag PEER_STATUS_ADV_DELAY which will allow
550 * more routes to be sent in the update message
552 if (BGP_SUPPRESS_FIB_ENABLED(bgp
)) {
553 adv_peer
= PAF_PEER(paf
);
554 if (!bgp_adv_fifo_count(
555 &subgrp
->sync
->withdraw
))
556 SET_FLAG(adv_peer
->thread_flags
,
557 PEER_THREAD_SUBGRP_ADV_DELAY
);
559 UNSET_FLAG(adv_peer
->thread_flags
,
560 PEER_THREAD_SUBGRP_ADV_DELAY
);
562 bgp_adjust_routeadv(PAF_PEER(paf
));
566 bgp_adv_fifo_add_tail(&subgrp
->sync
->update
, adv
);
568 subgrp
->version
= MAX(subgrp
->version
, dest
->version
);
571 /* The only time 'withdraw' will be false is if we are sending
572 * the "neighbor x.x.x.x default-originate" default and need to clear
573 * bgp_adj_out for the 0.0.0.0/0 route in the BGP table.
575 void bgp_adj_out_unset_subgroup(struct bgp_dest
*dest
,
576 struct update_subgroup
*subgrp
, char withdraw
,
577 uint32_t addpath_tx_id
)
579 struct bgp_adj_out
*adj
;
580 struct bgp_advertise
*adv
;
583 if (DISABLE_BGP_ANNOUNCE
)
586 /* Lookup existing adjacency */
587 adj
= adj_lookup(dest
, subgrp
, addpath_tx_id
);
589 /* Clean up previous advertisement. */
591 bgp_advertise_clean_subgroup(subgrp
, adj
);
593 /* If default originate is enabled and the route is default
594 * route, do not send withdraw. This will prevent deletion of
595 * the default route at the peer.
597 if (CHECK_FLAG(subgrp
->sflags
, SUBGRP_STATUS_DEFAULT_ORIGINATE
)
598 && is_default_prefix(bgp_dest_get_prefix(dest
)))
601 if (adj
->attr
&& withdraw
) {
602 /* We need advertisement structure. */
603 adj
->adv
= bgp_advertise_new();
608 /* Note if we need to trigger a packet write */
610 !bgp_adv_fifo_count(&subgrp
->sync
->withdraw
);
612 /* Add to synchronization entry for withdraw
614 bgp_adv_fifo_add_tail(&subgrp
->sync
->withdraw
, adv
);
617 subgroup_trigger_write(subgrp
);
619 /* Free allocated information. */
622 if (!CHECK_FLAG(subgrp
->sflags
, SUBGRP_STATUS_TABLE_REPARSING
))
626 subgrp
->version
= MAX(subgrp
->version
, dest
->version
);
629 void bgp_adj_out_remove_subgroup(struct bgp_dest
*dest
, struct bgp_adj_out
*adj
,
630 struct update_subgroup
*subgrp
)
633 bgp_attr_unintern(&adj
->attr
);
636 bgp_advertise_clean_subgroup(subgrp
, adj
);
642 * Go through all the routes and clean up the adj/adv structures corresponding
645 void subgroup_clear_table(struct update_subgroup
*subgrp
)
647 struct bgp_adj_out
*aout
, *taout
;
649 SUBGRP_FOREACH_ADJ_SAFE (subgrp
, aout
, taout
)
650 bgp_adj_out_remove_subgroup(aout
->dest
, aout
, subgrp
);
654 * subgroup_announce_table
656 void subgroup_announce_table(struct update_subgroup
*subgrp
,
657 struct bgp_table
*table
)
659 struct bgp_dest
*dest
;
660 struct bgp_path_info
*ri
;
665 bool addpath_capable
;
667 peer
= SUBGRP_PEER(subgrp
);
668 afi
= SUBGRP_AFI(subgrp
);
669 safi
= SUBGRP_SAFI(subgrp
);
670 addpath_capable
= bgp_addpath_encode_tx(peer
, afi
, safi
);
672 if (safi
== SAFI_LABELED_UNICAST
)
673 safi_rib
= SAFI_UNICAST
;
678 table
= peer
->bgp
->rib
[afi
][safi_rib
];
680 if (safi
!= SAFI_MPLS_VPN
&& safi
!= SAFI_ENCAP
&& safi
!= SAFI_EVPN
681 && CHECK_FLAG(peer
->af_flags
[afi
][safi
],
682 PEER_FLAG_DEFAULT_ORIGINATE
))
683 subgroup_default_originate(subgrp
, 0);
686 SET_FLAG(subgrp
->sflags
, SUBGRP_STATUS_TABLE_REPARSING
);
688 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
689 for (ri
= bgp_dest_get_bgp_path_info(dest
); ri
; ri
= ri
->next
) {
691 if (!bgp_check_selected(ri
, peer
, addpath_capable
, afi
,
695 /* If default originate is enabled for
696 * the peer, do not send explicit
697 * withdraw. This will prevent deletion
698 * of default route advertised through
701 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
702 PEER_FLAG_DEFAULT_ORIGINATE
) &&
703 is_default_prefix(bgp_dest_get_prefix(dest
)))
706 subgroup_process_announce_selected(
707 subgrp
, ri
, dest
, afi
, safi_rib
,
708 bgp_addpath_id_for_peer(peer
, afi
, safi_rib
,
712 UNSET_FLAG(subgrp
->sflags
, SUBGRP_STATUS_TABLE_REPARSING
);
715 * We walked through the whole table -- make sure our version number
716 * is consistent with the one on the table. This should allow
717 * subgroups to merge sooner if a peer comes up when the route node
718 * with the largest version is no longer in the table. This also
719 * covers the pathological case where all routes in the table have
722 subgrp
->version
= MAX(subgrp
->version
, table
->version
);
725 * Start a task to merge the subgroup if necessary.
727 update_subgroup_trigger_merge_check(subgrp
, 0);
731 * subgroup_announce_route
733 * Refresh all routes out to a subgroup.
735 void subgroup_announce_route(struct update_subgroup
*subgrp
)
737 struct bgp_dest
*dest
;
738 struct bgp_table
*table
;
739 struct peer
*onlypeer
;
741 if (update_subgroup_needs_refresh(subgrp
)) {
742 update_subgroup_set_needs_refresh(subgrp
, 0);
746 * First update is deferred until ORF or ROUTE-REFRESH is received
748 onlypeer
= ((SUBGRP_PCOUNT(subgrp
) == 1) ? (SUBGRP_PFIRST(subgrp
))->peer
750 if (onlypeer
&& CHECK_FLAG(onlypeer
->af_sflags
[SUBGRP_AFI(subgrp
)]
751 [SUBGRP_SAFI(subgrp
)],
752 PEER_STATUS_ORF_WAIT_REFRESH
))
755 if (SUBGRP_SAFI(subgrp
) != SAFI_MPLS_VPN
756 && SUBGRP_SAFI(subgrp
) != SAFI_ENCAP
757 && SUBGRP_SAFI(subgrp
) != SAFI_EVPN
)
758 subgroup_announce_table(subgrp
, NULL
);
760 for (dest
= bgp_table_top(update_subgroup_rib(subgrp
)); dest
;
761 dest
= bgp_route_next(dest
)) {
762 table
= bgp_dest_get_bgp_table_info(dest
);
765 subgroup_announce_table(subgrp
, table
);
769 void subgroup_default_originate(struct update_subgroup
*subgrp
, int withdraw
)
773 struct attr
*new_attr
= &attr
;
776 struct bgp_dest
*dest
;
777 struct bgp_path_info
*pi
;
779 struct bgp_adj_out
*adj
;
780 route_map_result_t ret
= RMAP_DENYMATCH
;
781 route_map_result_t new_ret
= RMAP_DENYMATCH
;
791 peer
= SUBGRP_PEER(subgrp
);
792 afi
= SUBGRP_AFI(subgrp
);
793 safi
= SUBGRP_SAFI(subgrp
);
795 if (!(afi
== AFI_IP
|| afi
== AFI_IP6
))
798 if (safi
== SAFI_LABELED_UNICAST
)
799 safi_rib
= SAFI_UNICAST
;
804 from
= bgp
->peer_self
;
806 bgp_attr_default_set(&attr
, bgp
, BGP_ORIGIN_IGP
);
808 /* make coverity happy */
812 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
);
814 if ((afi
== AFI_IP6
) || peer_cap_enhe(peer
, afi
, safi
)) {
815 /* IPv6 global nexthop must be included. */
816 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV6_GLOBAL
;
818 /* If the peer is on shared nextwork and we have link-local
820 if (peer
->shared_network
821 && !IN6_IS_ADDR_UNSPECIFIED(&peer
->nexthop
.v6_local
))
822 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
;
825 if (peer
->default_rmap
[afi
][safi
].name
) {
826 struct bgp_path_info tmp_pi
= {0};
828 tmp_pi
.peer
= bgp
->peer_self
;
830 SET_FLAG(bgp
->peer_self
->rmap_type
, PEER_RMAP_TYPE_DEFAULT
);
832 /* Iterate over the RIB to see if we can announce
833 * the default route. We announce the default
834 * route only if route-map has a match.
836 for (dest
= bgp_table_top(bgp
->rib
[afi
][safi_rib
]); dest
;
837 dest
= bgp_route_next(dest
)) {
838 if (!bgp_dest_has_bgp_path_info_data(dest
))
841 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
843 struct attr tmp_attr
= attr
;
845 tmp_pi
.attr
= &tmp_attr
;
847 new_ret
= route_map_apply_ext(
848 peer
->default_rmap
[afi
][safi
].map
,
849 bgp_dest_get_prefix(dest
), pi
, &tmp_pi
,
852 if (new_ret
== RMAP_PERMITMATCH
) {
853 if (new_pref
< pref
) {
855 bgp_attr_flush(new_attr
);
856 new_attr
= bgp_attr_intern(
858 bgp_attr_flush(tmp_pi
.attr
);
860 subgroup_announce_reset_nhop(
861 (peer_cap_enhe(peer
, afi
, safi
)
867 bgp_attr_flush(&tmp_attr
);
870 bgp
->peer_self
->rmap_type
= 0;
872 if (ret
== RMAP_DENYMATCH
) {
874 * If its a implicit withdraw due to routemap
875 * deny operation need to set the flag back.
876 * This is a convertion of update flow to
880 (!CHECK_FLAG(subgrp
->sflags
,
881 SUBGRP_STATUS_DEFAULT_ORIGINATE
)))
882 SET_FLAG(subgrp
->sflags
,
883 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
888 /* Check if the default route is in local BGP RIB which is
889 * installed through redistribute or network command
891 memset(&p
, 0, sizeof(p
));
892 p
.family
= afi2family(afi
);
894 dest
= bgp_safi_node_lookup(bgp
->rib
[afi
][safi_rib
], safi_rib
, &p
,
898 /* Withdraw the default route advertised using default
901 if (CHECK_FLAG(subgrp
->sflags
, SUBGRP_STATUS_DEFAULT_ORIGINATE
))
902 subgroup_default_withdraw_packet(subgrp
);
903 UNSET_FLAG(subgrp
->sflags
, SUBGRP_STATUS_DEFAULT_ORIGINATE
);
905 /* If default route is present in the local RIB, advertise the
909 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
911 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
))
912 if (subgroup_announce_check(
914 bgp_dest_get_prefix(dest
),
916 struct attr
*default_attr
=
917 bgp_attr_intern(&attr
);
919 bgp_adj_out_set_subgroup(
924 bgp_dest_unlock_node(dest
);
927 if (!CHECK_FLAG(subgrp
->sflags
,
928 SUBGRP_STATUS_DEFAULT_ORIGINATE
)) {
930 /* The 'neighbor x.x.x.x default-originate' default will
932 * implicit withdraw for any previous UPDATEs sent for
934 * clear adj_out for the 0.0.0.0/0 prefix in the BGP
938 /* Remove the adjacency for the previously
939 * advertised default route
943 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
945 /* Clean up previous advertisement. */
947 bgp_advertise_clean_subgroup(
950 /* Free allocated information. */
953 bgp_dest_unlock_node(dest
);
956 /* Advertise the default route */
957 if (bgp_in_graceful_shutdown(bgp
))
958 bgp_attr_add_gshut_community(new_attr
);
960 SET_FLAG(subgrp
->sflags
,
961 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
962 subgroup_default_update_packet(subgrp
, new_attr
, from
);
966 aspath_unintern(&attr
.aspath
);
970 * Announce the BGP table to a subgroup.
972 * At startup, we try to optimize route announcement by coalescing the
973 * peer-up events. This is done only the first time - from then on,
974 * subgrp->v_coalesce will be set to zero and the normal logic
977 void subgroup_announce_all(struct update_subgroup
*subgrp
)
983 * If coalesce timer value is not set, announce routes immediately.
985 if (!subgrp
->v_coalesce
) {
986 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
987 zlog_debug("u%" PRIu64
":s%" PRIu64
" announcing all routes",
988 subgrp
->update_group
->id
, subgrp
->id
);
989 subgroup_announce_route(subgrp
);
994 * We should wait for the coalesce timer. Arm the timer if not done.
996 if (!subgrp
->t_coalesce
) {
997 event_add_timer_msec(bm
->master
, subgroup_coalesce_timer
,
998 subgrp
, subgrp
->v_coalesce
,
999 &subgrp
->t_coalesce
);
1004 * Go through all update subgroups and set up the adv queue for the
1007 void group_announce_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1008 struct bgp_dest
*dest
, struct bgp_path_info
*pi
)
1010 struct updwalk_context ctx
;
1014 /* If suppress fib is enabled, the route will be advertised when
1015 * FIB status is received
1017 if (!bgp_check_advertise(bgp
, dest
))
1020 update_group_af_walk(bgp
, afi
, safi
, group_announce_route_walkcb
, &ctx
);
1023 void update_group_show_adj_queue(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1024 struct vty
*vty
, uint64_t id
)
1026 updgrp_show_adj(bgp
, afi
, safi
, vty
, id
, UPDWALK_FLAGS_ADVQUEUE
);
1029 void update_group_show_advertised(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1030 struct vty
*vty
, uint64_t id
)
1032 updgrp_show_adj(bgp
, afi
, safi
, vty
, id
, UPDWALK_FLAGS_ADVERTISED
);
1035 void update_group_announce(struct bgp
*bgp
)
1037 update_group_walk(bgp
, update_group_announce_walkcb
, NULL
);
1040 void update_group_announce_rrclients(struct bgp
*bgp
)
1042 update_group_walk(bgp
, update_group_announce_rrc_walkcb
, NULL
);