1 /* BGP Nexthop tracking
2 * Copyright (C) 2013 Cumulus Networks, Inc.
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "nexthop_group.h"
36 #include "bgpd/bgpd.h"
37 #include "bgpd/bgp_table.h"
38 #include "bgpd/bgp_route.h"
39 #include "bgpd/bgp_attr.h"
40 #include "bgpd/bgp_nexthop.h"
41 #include "bgpd/bgp_debug.h"
42 #include "bgpd/bgp_errors.h"
43 #include "bgpd/bgp_nht.h"
44 #include "bgpd/bgp_fsm.h"
45 #include "bgpd/bgp_zebra.h"
46 #include "bgpd/bgp_flowspec_util.h"
47 #include "bgpd/bgp_evpn.h"
48 #include "bgpd/bgp_rd.h"
50 extern struct zclient
*zclient
;
52 static void register_zebra_rnh(struct bgp_nexthop_cache
*bnc
);
53 static void unregister_zebra_rnh(struct bgp_nexthop_cache
*bnc
);
54 static int make_prefix(int afi
, struct bgp_path_info
*pi
, struct prefix
*p
);
55 static void bgp_nht_ifp_initial(struct thread
*thread
);
57 static int bgp_isvalid_nexthop(struct bgp_nexthop_cache
*bnc
)
59 return (bgp_zebra_num_connects() == 0
60 || (bnc
&& CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
)
61 && bnc
->nexthop_num
> 0));
64 static int bgp_isvalid_nexthop_for_ebgp(struct bgp_nexthop_cache
*bnc
,
65 struct bgp_path_info
*path
)
67 struct interface
*ifp
= NULL
;
68 struct nexthop
*nexthop
;
69 struct bgp_interface
*iifp
;
72 if (!path
->extra
|| !path
->extra
->peer_orig
)
75 peer
= path
->extra
->peer_orig
;
77 /* only connected ebgp peers are valid */
78 if (peer
->sort
!= BGP_PEER_EBGP
|| peer
->ttl
!= BGP_DEFAULT_TTL
||
79 CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
) ||
80 CHECK_FLAG(peer
->bgp
->flags
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
83 for (nexthop
= bnc
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
84 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
||
85 nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
||
86 nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
87 ifp
= if_lookup_by_index(
88 bnc
->ifindex
? bnc
->ifindex
: nexthop
->ifindex
,
94 if (CHECK_FLAG(iifp
->flags
, BGP_INTERFACE_MPLS_BGP_FORWARDING
))
100 static int bgp_isvalid_nexthop_for_mplsovergre(struct bgp_nexthop_cache
*bnc
,
101 struct bgp_path_info
*path
)
103 struct interface
*ifp
= NULL
;
104 struct nexthop
*nexthop
;
106 for (nexthop
= bnc
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
107 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
108 ifp
= if_lookup_by_index(
109 bnc
->ifindex
? bnc
->ifindex
: nexthop
->ifindex
,
111 if (ifp
&& (ifp
->ll_type
== ZEBRA_LLT_IPGRE
||
112 ifp
->ll_type
== ZEBRA_LLT_IP6GRE
))
119 if (CHECK_FLAG(path
->attr
->rmap_change_flags
,
120 BATTR_RMAP_L3VPN_ACCEPT_GRE
))
126 static int bgp_isvalid_nexthop_for_mpls(struct bgp_nexthop_cache
*bnc
,
127 struct bgp_path_info
*path
)
130 * - In the case of MPLS-VPN, the label is learned from LDP or other
131 * protocols, and nexthop tracking is enabled for the label.
132 * The value is recorded as BGP_NEXTHOP_LABELED_VALID.
133 * - In the case of SRv6-VPN, we need to track the reachability to the
134 * SID (in other words, IPv6 address). As in MPLS, we need to record
135 * the value as BGP_NEXTHOP_SID_VALID. However, this function is
136 * currently not implemented, and this function assumes that all
137 * Transit routes for SRv6-VPN are valid.
138 * - Otherwise check for mpls-gre acceptance
140 return (bgp_zebra_num_connects() == 0 ||
141 (bnc
&& (bnc
->nexthop_num
> 0 &&
142 (CHECK_FLAG(path
->flags
, BGP_PATH_ACCEPT_OWN
) ||
143 CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_LABELED_VALID
) ||
144 bnc
->bgp
->srv6_enabled
||
145 bgp_isvalid_nexthop_for_ebgp(bnc
, path
) ||
146 bgp_isvalid_nexthop_for_mplsovergre(bnc
, path
)))));
149 static void bgp_unlink_nexthop_check(struct bgp_nexthop_cache
*bnc
)
151 if (LIST_EMPTY(&(bnc
->paths
)) && !bnc
->nht_info
) {
152 if (BGP_DEBUG(nht
, NHT
))
153 zlog_debug("%s: freeing bnc %pFX(%d)(%u)(%s)", __func__
,
154 &bnc
->prefix
, bnc
->ifindex
, bnc
->srte_color
,
155 bnc
->bgp
->name_pretty
);
156 /* only unregister if this is the last nh for this prefix*/
157 if (!bnc_existing_for_prefix(bnc
))
158 unregister_zebra_rnh(bnc
);
163 void bgp_unlink_nexthop(struct bgp_path_info
*path
)
165 struct bgp_nexthop_cache
*bnc
= path
->nexthop
;
170 path_nh_map(path
, NULL
, false);
172 bgp_unlink_nexthop_check(bnc
);
175 void bgp_replace_nexthop_by_peer(struct peer
*from
, struct peer
*to
)
179 struct bgp_nexthop_cache
*bncp
, *bnct
;
181 ifindex_t ifindex
= 0;
183 if (!sockunion2hostprefix(&from
->su
, &pp
))
187 * Gather the ifindex for if up/down events to be
188 * tagged into this fun
190 if (from
->conf_if
&& IN6_IS_ADDR_LINKLOCAL(&from
->su
.sin6
.sin6_addr
))
191 ifindex
= from
->su
.sin6
.sin6_scope_id
;
193 afi
= family2afi(pp
.family
);
194 bncp
= bnc_find(&from
->bgp
->nexthop_cache_table
[afi
], &pp
, 0, ifindex
);
196 if (!sockunion2hostprefix(&to
->su
, &pt
))
200 * Gather the ifindex for if up/down events to be
201 * tagged into this fun
204 if (to
->conf_if
&& IN6_IS_ADDR_LINKLOCAL(&to
->su
.sin6
.sin6_addr
))
205 ifindex
= to
->su
.sin6
.sin6_scope_id
;
206 bnct
= bnc_find(&to
->bgp
->nexthop_cache_table
[afi
], &pt
, 0, ifindex
);
216 * Returns the bnc whose bnc->nht_info matches the LL peer by
217 * looping through the IPv6 nexthop table
219 static struct bgp_nexthop_cache
*
220 bgp_find_ipv6_nexthop_matching_peer(struct peer
*peer
)
222 struct bgp_nexthop_cache
*bnc
;
224 frr_each (bgp_nexthop_cache
, &peer
->bgp
->nexthop_cache_table
[AFI_IP6
],
226 if (bnc
->nht_info
== peer
) {
227 if (BGP_DEBUG(nht
, NHT
)) {
229 "Found bnc: %pFX(%u)(%u)(%p) for peer: %s(%s) %p",
230 &bnc
->prefix
, bnc
->ifindex
,
231 bnc
->srte_color
, bnc
, peer
->host
,
232 peer
->bgp
->name_pretty
, peer
);
238 if (BGP_DEBUG(nht
, NHT
))
240 "Could not find bnc for peer %s(%s) %p in v6 nexthop table",
241 peer
->host
, peer
->bgp
->name_pretty
, peer
);
246 void bgp_unlink_nexthop_by_peer(struct peer
*peer
)
249 struct bgp_nexthop_cache
*bnc
;
250 afi_t afi
= family2afi(peer
->su
.sa
.sa_family
);
251 ifindex_t ifindex
= 0;
253 if (!sockunion2hostprefix(&peer
->su
, &p
)) {
255 * In scenarios where unnumbered BGP session is brought
256 * down by shutting down the interface before unconfiguring
257 * the BGP neighbor, neighbor information in peer->su.sa
258 * will be cleared when the interface is shutdown. So
259 * during the deletion of unnumbered bgp peer, above check
260 * will return true. Therefore, in this case,BGP needs to
261 * find the bnc whose bnc->nht_info matches the
262 * peer being deleted and free it.
264 bnc
= bgp_find_ipv6_nexthop_matching_peer(peer
);
267 * Gather the ifindex for if up/down events to be
268 * tagged into this fun
270 if (afi
== AFI_IP6
&&
271 IN6_IS_ADDR_LINKLOCAL(&peer
->su
.sin6
.sin6_addr
))
272 ifindex
= peer
->su
.sin6
.sin6_scope_id
;
273 bnc
= bnc_find(&peer
->bgp
->nexthop_cache_table
[afi
], &p
, 0,
280 /* cleanup the peer reference */
281 bnc
->nht_info
= NULL
;
283 bgp_unlink_nexthop_check(bnc
);
287 * A route and its nexthop might belong to different VRFs. Therefore,
288 * we need both the bgp_route and bgp_nexthop pointers.
290 int bgp_find_or_add_nexthop(struct bgp
*bgp_route
, struct bgp
*bgp_nexthop
,
291 afi_t afi
, safi_t safi
, struct bgp_path_info
*pi
,
292 struct peer
*peer
, int connected
,
293 const struct prefix
*orig_prefix
)
295 struct bgp_nexthop_cache_head
*tree
= NULL
;
296 struct bgp_nexthop_cache
*bnc
;
297 struct bgp_path_info
*bpi_ultimate
;
299 uint32_t srte_color
= 0;
300 int is_bgp_static_route
= 0;
301 ifindex_t ifindex
= 0;
304 is_bgp_static_route
= ((pi
->type
== ZEBRA_ROUTE_BGP
)
305 && (pi
->sub_type
== BGP_ROUTE_STATIC
))
309 /* Since Extended Next-hop Encoding (RFC5549) support, we want
311 address-family from the next-hop. */
312 if (!is_bgp_static_route
)
313 afi
= BGP_ATTR_MP_NEXTHOP_LEN_IP6(pi
->attr
) ? AFI_IP6
316 /* Validation for the ipv4 mapped ipv6 nexthop. */
317 if (IS_MAPPED_IPV6(&pi
->attr
->mp_nexthop_global
)) {
321 /* This will return true if the global IPv6 NH is a link local
323 if (make_prefix(afi
, pi
, &p
) < 0)
326 if (!is_bgp_static_route
&& orig_prefix
327 && prefix_same(&p
, orig_prefix
)) {
328 if (BGP_DEBUG(nht
, NHT
)) {
330 "%s(%pFX): prefix loops through itself",
336 srte_color
= pi
->attr
->srte_color
;
339 * Gather the ifindex for if up/down events to be
340 * tagged into this fun
342 if (afi
== AFI_IP6
&&
343 IN6_IS_ADDR_LINKLOCAL(&peer
->su
.sin6
.sin6_addr
)) {
344 ifindex
= peer
->su
.sin6
.sin6_scope_id
;
346 if (BGP_DEBUG(nht
, NHT
)) {
348 "%s: Unable to locate ifindex, waiting till we have one",
355 if (!sockunion2hostprefix(&peer
->su
, &p
)) {
356 if (BGP_DEBUG(nht
, NHT
)) {
358 "%s: Attempting to register with unknown AFI %d (not %d or %d)",
359 __func__
, afi
, AFI_IP
, AFI_IP6
);
366 if (is_bgp_static_route
)
367 tree
= &bgp_nexthop
->import_check_table
[afi
];
369 tree
= &bgp_nexthop
->nexthop_cache_table
[afi
];
371 bnc
= bnc_find(tree
, &p
, srte_color
, ifindex
);
373 bnc
= bnc_new(tree
, &p
, srte_color
, ifindex
);
374 bnc
->bgp
= bgp_nexthop
;
375 if (BGP_DEBUG(nht
, NHT
))
376 zlog_debug("Allocated bnc %pFX(%d)(%u)(%s) peer %p",
377 &bnc
->prefix
, bnc
->ifindex
, bnc
->srte_color
,
378 bnc
->bgp
->name_pretty
, peer
);
380 if (BGP_DEBUG(nht
, NHT
))
382 "Found existing bnc %pFX(%d)(%s) flags 0x%x ifindex %d #paths %d peer %p",
383 &bnc
->prefix
, bnc
->ifindex
,
384 bnc
->bgp
->name_pretty
, bnc
->flags
, bnc
->ifindex
,
385 bnc
->path_count
, bnc
->nht_info
);
388 if (pi
&& is_route_parent_evpn(pi
))
389 bnc
->is_evpn_gwip_nexthop
= true;
391 if (is_bgp_static_route
) {
392 SET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE
);
394 /* If we're toggling the type, re-register */
395 if ((CHECK_FLAG(bgp_route
->flags
, BGP_FLAG_IMPORT_CHECK
))
396 && !CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
)) {
397 SET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
);
398 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
399 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
400 } else if ((!CHECK_FLAG(bgp_route
->flags
,
401 BGP_FLAG_IMPORT_CHECK
))
402 && CHECK_FLAG(bnc
->flags
,
403 BGP_STATIC_ROUTE_EXACT_MATCH
)) {
404 UNSET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
);
405 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
406 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
409 /* When nexthop is already known, but now requires 'connected'
411 * re-register it. The reverse scenario where the nexthop currently
413 * 'connected' resolution does not need a re-register (i.e., we treat
414 * 'connected-required' as an override) except in the scenario where
416 * is actually a case of tracking a peer for connectivity (e.g., after
417 * disable connected-check).
418 * NOTE: We don't track the number of paths separately for 'connected-
419 * required' vs 'connected-not-required' as this change is not a common
422 else if (connected
&& !CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
)) {
423 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
);
424 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
425 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
426 } else if (peer
&& !connected
427 && CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
)) {
428 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
);
429 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
430 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
432 if (peer
&& (bnc
->ifindex
!= ifindex
)) {
433 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
434 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
435 bnc
->ifindex
= ifindex
;
437 if (bgp_route
->inst_type
== BGP_INSTANCE_TYPE_VIEW
) {
438 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
439 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
440 } else if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
)
441 && !is_default_host_route(&bnc
->prefix
))
442 register_zebra_rnh(bnc
);
444 if (pi
&& pi
->nexthop
!= bnc
) {
445 /* Unlink from existing nexthop cache, if any. This will also
447 * the nexthop cache entry, if appropriate.
449 bgp_unlink_nexthop(pi
);
451 /* updates NHT pi list reference */
452 path_nh_map(pi
, bnc
, true);
454 bpi_ultimate
= bgp_get_imported_bpi_ultimate(pi
);
455 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
) && bnc
->metric
)
456 (bgp_path_info_extra_get(bpi_ultimate
))->igpmetric
=
458 else if (bpi_ultimate
->extra
)
459 bpi_ultimate
->extra
->igpmetric
= 0;
462 * Let's not accidentally save the peer data for a peer
463 * we are going to throw away in a second or so.
464 * When we come back around we'll fix up this
465 * data properly in replace_nexthop_by_peer
467 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
468 bnc
->nht_info
= (void *)peer
; /* NHT peer reference */
472 * We are cheating here. Views have no associated underlying
473 * ability to detect nexthops. So when we have a view
474 * just tell everyone the nexthop is valid
476 if (bgp_route
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
478 else if (safi
== SAFI_UNICAST
&& pi
&&
479 pi
->sub_type
== BGP_ROUTE_IMPORTED
&& pi
->extra
&&
480 pi
->extra
->num_labels
&& !bnc
->is_evpn_gwip_nexthop
)
481 return bgp_isvalid_nexthop_for_mpls(bnc
, pi
);
483 return (bgp_isvalid_nexthop(bnc
));
486 void bgp_delete_connected_nexthop(afi_t afi
, struct peer
*peer
)
488 struct bgp_nexthop_cache
*bnc
;
490 ifindex_t ifindex
= 0;
496 * In case the below check evaluates true and if
497 * the bnc has not been freed at this point, then
498 * we might have to do something similar to what's
499 * done in bgp_unlink_nexthop_by_peer(). Since
500 * bgp_unlink_nexthop_by_peer() loops through the
501 * nodes of V6 nexthop cache to find the bnc, it is
502 * currently not being called here.
504 if (!sockunion2hostprefix(&peer
->su
, &p
))
507 * Gather the ifindex for if up/down events to be
508 * tagged into this fun
510 if (afi
== AFI_IP6
&& IN6_IS_ADDR_LINKLOCAL(&peer
->su
.sin6
.sin6_addr
))
511 ifindex
= peer
->su
.sin6
.sin6_scope_id
;
512 bnc
= bnc_find(&peer
->bgp
->nexthop_cache_table
[family2afi(p
.family
)],
515 if (BGP_DEBUG(nht
, NHT
))
517 "Cannot find connected NHT node for peer %s(%s)",
518 peer
->host
, peer
->bgp
->name_pretty
);
522 if (bnc
->nht_info
!= peer
) {
523 if (BGP_DEBUG(nht
, NHT
))
525 "Connected NHT %p node for peer %s(%s) points to %p",
526 bnc
, peer
->host
, bnc
->bgp
->name_pretty
,
531 bnc
->nht_info
= NULL
;
533 if (LIST_EMPTY(&(bnc
->paths
))) {
534 if (BGP_DEBUG(nht
, NHT
))
536 "Freeing connected NHT node %p for peer %s(%s)",
537 bnc
, peer
->host
, bnc
->bgp
->name_pretty
);
538 unregister_zebra_rnh(bnc
);
543 static void bgp_process_nexthop_update(struct bgp_nexthop_cache
*bnc
,
544 struct zapi_route
*nhr
,
547 struct nexthop
*nexthop
;
548 struct nexthop
*oldnh
;
549 struct nexthop
*nhlist_head
= NULL
;
550 struct nexthop
*nhlist_tail
= NULL
;
552 bool evpn_resolved
= false;
554 bnc
->last_update
= monotime(NULL
);
555 bnc
->change_flags
= 0;
557 /* debug print the input */
558 if (BGP_DEBUG(nht
, NHT
)) {
559 char bnc_buf
[BNC_FLAG_DUMP_SIZE
];
562 "%s(%u): Rcvd NH update %pFX(%u)%u) - metric %d/%d #nhops %d/%d flags %s",
563 bnc
->bgp
->name_pretty
, bnc
->bgp
->vrf_id
, &nhr
->prefix
,
564 bnc
->ifindex
, bnc
->srte_color
, nhr
->metric
, bnc
->metric
,
565 nhr
->nexthop_num
, bnc
->nexthop_num
,
566 bgp_nexthop_dump_bnc_flags(bnc
, bnc_buf
,
570 if (nhr
->metric
!= bnc
->metric
)
571 bnc
->change_flags
|= BGP_NEXTHOP_METRIC_CHANGED
;
573 if (nhr
->nexthop_num
!= bnc
->nexthop_num
)
574 bnc
->change_flags
|= BGP_NEXTHOP_CHANGED
;
576 if (import_check
&& (nhr
->type
== ZEBRA_ROUTE_BGP
||
577 !prefix_same(&bnc
->prefix
, &nhr
->prefix
))) {
578 SET_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
);
579 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
580 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_LABELED_VALID
);
581 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_EVPN_INCOMPLETE
);
583 bnc_nexthop_free(bnc
);
586 if (BGP_DEBUG(nht
, NHT
))
588 "%s: Import Check does not resolve to the same prefix for %pFX received %pFX or matching route is BGP",
589 __func__
, &bnc
->prefix
, &nhr
->prefix
);
590 } else if (nhr
->nexthop_num
) {
591 struct peer
*peer
= bnc
->nht_info
;
593 /* notify bgp fsm if nbr ip goes from invalid->valid */
594 if (!bnc
->nexthop_num
)
595 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
597 if (!bnc
->is_evpn_gwip_nexthop
)
598 bnc
->flags
|= BGP_NEXTHOP_VALID
;
599 bnc
->metric
= nhr
->metric
;
600 bnc
->nexthop_num
= nhr
->nexthop_num
;
602 bnc
->flags
&= ~BGP_NEXTHOP_LABELED_VALID
; /* check below */
604 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
607 nexthop
= nexthop_from_zapi_nexthop(&nhr
->nexthops
[i
]);
610 * Turn on RA for the v6 nexthops
611 * we receive from bgp. This is to allow us
612 * to work with v4 routing over v6 nexthops
614 if (peer
&& !peer
->ifp
615 && CHECK_FLAG(peer
->flags
,
616 PEER_FLAG_CAPABILITY_ENHE
)
617 && nhr
->prefix
.family
== AF_INET6
618 && nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
619 struct interface
*ifp
;
621 ifp
= if_lookup_by_index(nexthop
->ifindex
,
624 zclient_send_interface_radv_req(
625 zclient
, nexthop
->vrf_id
, ifp
,
627 BGP_UNNUM_DEFAULT_RA_INTERVAL
);
629 /* There is at least one label-switched path */
630 if (nexthop
->nh_label
&&
631 nexthop
->nh_label
->num_labels
) {
633 bnc
->flags
|= BGP_NEXTHOP_LABELED_VALID
;
634 num_labels
= nexthop
->nh_label
->num_labels
;
637 if (BGP_DEBUG(nht
, NHT
)) {
638 char buf
[NEXTHOP_STRLEN
];
640 " nhop via %s (%d labels)",
641 nexthop2str(nexthop
, buf
, sizeof(buf
)),
646 nhlist_tail
->next
= nexthop
;
647 nhlist_tail
= nexthop
;
649 nhlist_tail
= nexthop
;
650 nhlist_head
= nexthop
;
653 /* No need to evaluate the nexthop if we have already
655 * that there has been a change.
657 if (bnc
->change_flags
& BGP_NEXTHOP_CHANGED
)
660 for (oldnh
= bnc
->nexthop
; oldnh
; oldnh
= oldnh
->next
)
661 if (nexthop_same(oldnh
, nexthop
))
665 bnc
->change_flags
|= BGP_NEXTHOP_CHANGED
;
667 bnc_nexthop_free(bnc
);
668 bnc
->nexthop
= nhlist_head
;
671 * Gateway IP nexthop is L3 reachable. Mark it as
672 * BGP_NEXTHOP_VALID only if it is recursively resolved with a
674 * Else, mark it as BGP_NEXTHOP_EVPN_INCOMPLETE.
675 * When its mapping with EVPN RT-2 is established, unset
676 * BGP_NEXTHOP_EVPN_INCOMPLETE and set BGP_NEXTHOP_VALID.
678 if (bnc
->is_evpn_gwip_nexthop
) {
679 evpn_resolved
= bgp_evpn_is_gateway_ip_resolved(bnc
);
681 if (BGP_DEBUG(nht
, NHT
))
683 "EVPN gateway IP %pFX recursive MAC/IP lookup %s",
685 (evpn_resolved
? "successful"
689 bnc
->flags
|= BGP_NEXTHOP_VALID
;
690 bnc
->flags
&= ~BGP_NEXTHOP_EVPN_INCOMPLETE
;
691 bnc
->change_flags
|= BGP_NEXTHOP_MACIP_CHANGED
;
693 bnc
->flags
|= BGP_NEXTHOP_EVPN_INCOMPLETE
;
694 bnc
->flags
&= ~BGP_NEXTHOP_VALID
;
698 bnc
->flags
&= ~BGP_NEXTHOP_EVPN_INCOMPLETE
;
699 bnc
->flags
&= ~BGP_NEXTHOP_VALID
;
700 bnc
->flags
&= ~BGP_NEXTHOP_LABELED_VALID
;
701 bnc
->nexthop_num
= nhr
->nexthop_num
;
703 /* notify bgp fsm if nbr ip goes from valid->invalid */
704 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
706 bnc_nexthop_free(bnc
);
713 static void bgp_nht_ifp_table_handle(struct bgp
*bgp
,
714 struct bgp_nexthop_cache_head
*table
,
715 struct interface
*ifp
, bool up
)
717 struct bgp_nexthop_cache
*bnc
;
719 frr_each (bgp_nexthop_cache
, table
, bnc
) {
720 if (bnc
->ifindex
!= ifp
->ifindex
)
723 bnc
->last_update
= monotime(NULL
);
724 bnc
->change_flags
= 0;
727 * For interface based routes ( ala the v6 LL routes
728 * that this was written for ) the metric received
729 * for the connected route is 0 not 1.
733 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
734 SET_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
);
735 bnc
->nexthop_num
= 1;
737 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
738 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
739 SET_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
);
740 bnc
->nexthop_num
= 0;
746 static void bgp_nht_ifp_handle(struct interface
*ifp
, bool up
)
750 bgp
= ifp
->vrf
->info
;
754 bgp_nht_ifp_table_handle(bgp
, &bgp
->nexthop_cache_table
[AFI_IP
], ifp
,
756 bgp_nht_ifp_table_handle(bgp
, &bgp
->import_check_table
[AFI_IP
], ifp
,
758 bgp_nht_ifp_table_handle(bgp
, &bgp
->nexthop_cache_table
[AFI_IP6
], ifp
,
760 bgp_nht_ifp_table_handle(bgp
, &bgp
->import_check_table
[AFI_IP6
], ifp
,
764 void bgp_nht_ifp_up(struct interface
*ifp
)
766 bgp_nht_ifp_handle(ifp
, true);
769 void bgp_nht_ifp_down(struct interface
*ifp
)
771 bgp_nht_ifp_handle(ifp
, false);
774 static void bgp_nht_ifp_initial(struct thread
*thread
)
776 ifindex_t ifindex
= THREAD_VAL(thread
);
777 struct bgp
*bgp
= THREAD_ARG(thread
);
778 struct interface
*ifp
= if_lookup_by_index(ifindex
, bgp
->vrf_id
);
783 if (BGP_DEBUG(nht
, NHT
))
785 "Handle NHT initial update for Intf %s(%d) status %s",
786 ifp
->name
, ifp
->ifindex
, if_is_up(ifp
) ? "up" : "down");
791 bgp_nht_ifp_down(ifp
);
795 * So the bnc code has the ability to handle interface up/down
796 * events to properly handle v6 LL peering.
797 * What is happening here:
798 * The event system for peering expects the nht code to
799 * report on the tracking events after we move to active
800 * So let's give the system a chance to report on that event
801 * in a manner that is expected.
803 void bgp_nht_interface_events(struct peer
*peer
)
805 struct bgp
*bgp
= peer
->bgp
;
806 struct bgp_nexthop_cache_head
*table
;
807 struct bgp_nexthop_cache
*bnc
;
809 ifindex_t ifindex
= 0;
811 if (!IN6_IS_ADDR_LINKLOCAL(&peer
->su
.sin6
.sin6_addr
))
814 if (!sockunion2hostprefix(&peer
->su
, &p
))
817 * Gather the ifindex for if up/down events to be
818 * tagged into this fun
820 if (peer
->conf_if
&& IN6_IS_ADDR_LINKLOCAL(&peer
->su
.sin6
.sin6_addr
))
821 ifindex
= peer
->su
.sin6
.sin6_scope_id
;
823 table
= &bgp
->nexthop_cache_table
[AFI_IP6
];
824 bnc
= bnc_find(table
, &p
, 0, ifindex
);
829 thread_add_event(bm
->master
, bgp_nht_ifp_initial
, bnc
->bgp
,
833 void bgp_parse_nexthop_update(int command
, vrf_id_t vrf_id
)
835 struct bgp_nexthop_cache_head
*tree
= NULL
;
836 struct bgp_nexthop_cache
*bnc_nhc
, *bnc_import
;
839 struct zapi_route nhr
;
842 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
846 "parse nexthop update: instance not found for vrf_id %u",
851 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &match
, &nhr
)) {
852 zlog_err("%s[%s]: Failure to decode nexthop update", __func__
,
857 afi
= family2afi(match
.family
);
858 tree
= &bgp
->nexthop_cache_table
[afi
];
860 bnc_nhc
= bnc_find(tree
, &match
, nhr
.srte_color
, 0);
862 if (BGP_DEBUG(nht
, NHT
))
864 "parse nexthop update(%pFX(%u)(%s)): bnc info not found for nexthop cache",
865 &nhr
.prefix
, nhr
.srte_color
, bgp
->name_pretty
);
867 bgp_process_nexthop_update(bnc_nhc
, &nhr
, false);
869 tree
= &bgp
->import_check_table
[afi
];
871 bnc_import
= bnc_find(tree
, &match
, nhr
.srte_color
, 0);
873 if (BGP_DEBUG(nht
, NHT
))
875 "parse nexthop update(%pFX(%u)(%s)): bnc info not found for import check",
876 &nhr
.prefix
, nhr
.srte_color
, bgp
->name_pretty
);
878 bgp_process_nexthop_update(bnc_import
, &nhr
, true);
881 * HACK: if any BGP route is dependant on an SR-policy that doesn't
882 * exist, zebra will never send NH updates relative to that policy. In
883 * that case, whenever we receive an update about a colorless NH, update
884 * the corresponding colorful NHs that share the same endpoint but that
885 * are inactive. This ugly hack should work around the problem at the
886 * cost of a performance pernalty. Long term, what should be done is to
887 * make zebra's RNH subsystem aware of SR-TE colors (like bgpd is),
888 * which should provide a better infrastructure to solve this issue in
889 * a more efficient and elegant way.
891 if (nhr
.srte_color
== 0 && bnc_nhc
) {
892 struct bgp_nexthop_cache
*bnc_iter
;
894 frr_each (bgp_nexthop_cache
, &bgp
->nexthop_cache_table
[afi
],
896 if (!prefix_same(&bnc_nhc
->prefix
, &bnc_iter
->prefix
) ||
897 bnc_iter
->srte_color
== 0 ||
898 CHECK_FLAG(bnc_iter
->flags
, BGP_NEXTHOP_VALID
))
901 bgp_process_nexthop_update(bnc_iter
, &nhr
, false);
907 * Cleanup nexthop registration and status information for BGP nexthops
908 * pertaining to this VRF. This is invoked upon VRF deletion.
910 void bgp_cleanup_nexthops(struct bgp
*bgp
)
912 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
913 struct bgp_nexthop_cache
*bnc
;
915 frr_each (bgp_nexthop_cache
, &bgp
->nexthop_cache_table
[afi
],
917 /* Clear relevant flags. */
918 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
919 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
920 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
921 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_EVPN_INCOMPLETE
);
927 * make_prefix - make a prefix structure from the path (essentially
930 static int make_prefix(int afi
, struct bgp_path_info
*pi
, struct prefix
*p
)
933 int is_bgp_static
= ((pi
->type
== ZEBRA_ROUTE_BGP
)
934 && (pi
->sub_type
== BGP_ROUTE_STATIC
))
937 struct bgp_dest
*net
= pi
->net
;
938 const struct prefix
*p_orig
= bgp_dest_get_prefix(net
);
941 if (p_orig
->family
== AF_FLOWSPEC
) {
944 return bgp_flowspec_get_first_nh(pi
->peer
->bgp
,
947 memset(p
, 0, sizeof(struct prefix
));
952 p
->u
.prefix4
= p_orig
->u
.prefix4
;
953 p
->prefixlen
= p_orig
->prefixlen
;
955 if (IS_MAPPED_IPV6(&pi
->attr
->mp_nexthop_global
)) {
956 ipv4_mapped_ipv6_to_ipv4(
957 &pi
->attr
->mp_nexthop_global
, &ipv4
);
959 p
->prefixlen
= IPV4_MAX_BITLEN
;
961 if (p_orig
->family
== AF_EVPN
)
963 pi
->attr
->mp_nexthop_global_in
;
965 p
->u
.prefix4
= pi
->attr
->nexthop
;
966 p
->prefixlen
= IPV4_MAX_BITLEN
;
971 p
->family
= AF_INET6
;
974 p
->u
.prefix6
= p_orig
->u
.prefix6
;
975 p
->prefixlen
= p_orig
->prefixlen
;
977 /* If we receive MP_REACH nexthop with ::(LL)
978 * or LL(LL), use LL address as nexthop cache.
980 if (pi
->attr
->mp_nexthop_len
981 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
982 && (IN6_IS_ADDR_UNSPECIFIED(
983 &pi
->attr
->mp_nexthop_global
)
984 || IN6_IS_ADDR_LINKLOCAL(
985 &pi
->attr
->mp_nexthop_global
)))
986 p
->u
.prefix6
= pi
->attr
->mp_nexthop_local
;
987 /* If we receive MR_REACH with (GA)::(LL)
988 * then check for route-map to choose GA or LL
990 else if (pi
->attr
->mp_nexthop_len
991 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
992 if (pi
->attr
->mp_nexthop_prefer_global
)
994 pi
->attr
->mp_nexthop_global
;
997 pi
->attr
->mp_nexthop_local
;
999 p
->u
.prefix6
= pi
->attr
->mp_nexthop_global
;
1000 p
->prefixlen
= IPV6_MAX_BITLEN
;
1004 if (BGP_DEBUG(nht
, NHT
)) {
1006 "%s: Attempting to make prefix with unknown AFI %d (not %d or %d)",
1007 __func__
, afi
, AFI_IP
, AFI_IP6
);
1015 * sendmsg_zebra_rnh -- Format and send a nexthop register/Unregister
1018 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
1019 * int command -- command to send to zebra
1023 static void sendmsg_zebra_rnh(struct bgp_nexthop_cache
*bnc
, int command
)
1025 bool exact_match
= false;
1026 bool resolve_via_default
= false;
1032 /* Don't try to register if Zebra doesn't know of this instance. */
1033 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bnc
->bgp
)) {
1034 if (BGP_DEBUG(zebra
, ZEBRA
))
1036 "%s: No zebra instance to talk to, not installing NHT entry",
1041 if (!bgp_zebra_num_connects()) {
1042 if (BGP_DEBUG(zebra
, ZEBRA
))
1044 "%s: We have not connected yet, cannot send nexthops",
1047 if (command
== ZEBRA_NEXTHOP_REGISTER
) {
1048 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
))
1050 if (CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
))
1051 resolve_via_default
= true;
1054 if (BGP_DEBUG(zebra
, ZEBRA
))
1055 zlog_debug("%s: sending cmd %s for %pFX (vrf %s)", __func__
,
1056 zserv_command_string(command
), &bnc
->prefix
,
1057 bnc
->bgp
->name_pretty
);
1059 ret
= zclient_send_rnh(zclient
, command
, &bnc
->prefix
, SAFI_UNICAST
,
1060 exact_match
, resolve_via_default
,
1062 if (ret
== ZCLIENT_SEND_FAILURE
) {
1063 flog_warn(EC_BGP_ZEBRA_SEND
,
1064 "sendmsg_nexthop: zclient_send_message() failed");
1068 if (command
== ZEBRA_NEXTHOP_REGISTER
)
1069 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
1070 else if (command
== ZEBRA_NEXTHOP_UNREGISTER
)
1071 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
1076 * register_zebra_rnh - register a NH/route with Zebra for notification
1077 * when the route or the route to the nexthop changes.
1079 * struct bgp_nexthop_cache *bnc
1083 static void register_zebra_rnh(struct bgp_nexthop_cache
*bnc
)
1085 /* Check if we have already registered */
1086 if (bnc
->flags
& BGP_NEXTHOP_REGISTERED
)
1090 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
1094 sendmsg_zebra_rnh(bnc
, ZEBRA_NEXTHOP_REGISTER
);
1098 * unregister_zebra_rnh -- Unregister the route/nexthop from Zebra.
1100 * struct bgp_nexthop_cache *bnc
1104 static void unregister_zebra_rnh(struct bgp_nexthop_cache
*bnc
)
1106 /* Check if we have already registered */
1107 if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
))
1111 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
1115 sendmsg_zebra_rnh(bnc
, ZEBRA_NEXTHOP_UNREGISTER
);
1119 * evaluate_paths - Evaluate the paths/nets associated with a nexthop.
1121 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
1125 void evaluate_paths(struct bgp_nexthop_cache
*bnc
)
1127 struct bgp_dest
*dest
;
1128 struct bgp_path_info
*path
;
1129 struct bgp_path_info
*bpi_ultimate
;
1131 struct peer
*peer
= (struct peer
*)bnc
->nht_info
;
1132 struct bgp_table
*table
;
1134 struct bgp
*bgp_path
;
1135 const struct prefix
*p
;
1137 if (BGP_DEBUG(nht
, NHT
)) {
1138 char bnc_buf
[BNC_FLAG_DUMP_SIZE
];
1139 char chg_buf
[BNC_FLAG_DUMP_SIZE
];
1142 "NH update for %pFX(%d)(%u)(%s) - flags %s chgflags %s- evaluate paths",
1143 &bnc
->prefix
, bnc
->ifindex
, bnc
->srte_color
,
1144 bnc
->bgp
->name_pretty
,
1145 bgp_nexthop_dump_bnc_flags(bnc
, bnc_buf
,
1147 bgp_nexthop_dump_bnc_change_flags(bnc
, chg_buf
,
1151 LIST_FOREACH (path
, &(bnc
->paths
), nh_thread
) {
1152 if (!(path
->type
== ZEBRA_ROUTE_BGP
1153 && ((path
->sub_type
== BGP_ROUTE_NORMAL
)
1154 || (path
->sub_type
== BGP_ROUTE_STATIC
)
1155 || (path
->sub_type
== BGP_ROUTE_IMPORTED
))))
1159 assert(dest
&& bgp_dest_table(dest
));
1160 p
= bgp_dest_get_prefix(dest
);
1161 afi
= family2afi(p
->family
);
1162 table
= bgp_dest_table(dest
);
1166 * handle routes from other VRFs (they can have a
1167 * nexthop in THIS VRF). bgp_path is the bgp instance
1168 * that owns the route referencing this nexthop.
1170 bgp_path
= table
->bgp
;
1173 * Path becomes valid/invalid depending on whether the nexthop
1174 * reachable/unreachable.
1176 * In case of unicast routes that were imported from vpn
1177 * and that have labels, they are valid only if there are
1178 * nexthops with labels
1180 * If the nexthop is EVPN gateway-IP,
1181 * do not check for a valid label.
1184 bool bnc_is_valid_nexthop
= false;
1185 bool path_valid
= false;
1187 if (safi
== SAFI_UNICAST
&& path
->sub_type
== BGP_ROUTE_IMPORTED
1188 && path
->extra
&& path
->extra
->num_labels
1189 && (path
->attr
->evpn_overlay
.type
1190 != OVERLAY_INDEX_GATEWAY_IP
)) {
1191 bnc_is_valid_nexthop
=
1192 bgp_isvalid_nexthop_for_mpls(bnc
, path
) ? true
1195 if (bgp_update_martian_nexthop(
1196 bnc
->bgp
, afi
, safi
, path
->type
,
1197 path
->sub_type
, path
->attr
, dest
)) {
1198 if (BGP_DEBUG(nht
, NHT
))
1200 "%s: prefix %pBD (vrf %s), ignoring path due to martian or self-next-hop",
1201 __func__
, dest
, bgp_path
->name
);
1203 bnc_is_valid_nexthop
=
1204 bgp_isvalid_nexthop(bnc
) ? true : false;
1207 if (BGP_DEBUG(nht
, NHT
)) {
1210 "... eval path %d/%d %pBD RD %pRD %s flags 0x%x",
1212 (struct prefix_rd
*)bgp_dest_get_prefix(
1214 bgp_path
->name_pretty
, path
->flags
);
1217 "... eval path %d/%d %pBD %s flags 0x%x",
1218 afi
, safi
, dest
, bgp_path
->name_pretty
,
1222 /* Skip paths marked for removal or as history. */
1223 if (CHECK_FLAG(path
->flags
, BGP_PATH_REMOVED
)
1224 || CHECK_FLAG(path
->flags
, BGP_PATH_HISTORY
))
1227 /* Copy the metric to the path. Will be used for bestpath
1229 bpi_ultimate
= bgp_get_imported_bpi_ultimate(path
);
1230 if (bgp_isvalid_nexthop(bnc
) && bnc
->metric
)
1231 (bgp_path_info_extra_get(bpi_ultimate
))->igpmetric
=
1233 else if (bpi_ultimate
->extra
)
1234 bpi_ultimate
->extra
->igpmetric
= 0;
1236 if (CHECK_FLAG(bnc
->change_flags
, BGP_NEXTHOP_METRIC_CHANGED
)
1237 || CHECK_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
)
1238 || path
->attr
->srte_color
!= 0)
1239 SET_FLAG(path
->flags
, BGP_PATH_IGP_CHANGED
);
1241 path_valid
= CHECK_FLAG(path
->flags
, BGP_PATH_VALID
);
1242 if (path_valid
!= bnc_is_valid_nexthop
) {
1244 /* No longer valid, clear flag; also for EVPN
1245 * routes, unimport from VRFs if needed.
1247 bgp_aggregate_decrement(bgp_path
, p
, path
, afi
,
1249 bgp_path_info_unset_flag(dest
, path
,
1251 if (safi
== SAFI_EVPN
&&
1252 bgp_evpn_is_prefix_nht_supported(bgp_dest_get_prefix(dest
)))
1253 bgp_evpn_unimport_route(bgp_path
,
1254 afi
, safi
, bgp_dest_get_prefix(dest
), path
);
1256 /* Path becomes valid, set flag; also for EVPN
1257 * routes, import from VRFs if needed.
1259 bgp_path_info_set_flag(dest
, path
,
1261 bgp_aggregate_increment(bgp_path
, p
, path
, afi
,
1263 if (safi
== SAFI_EVPN
&&
1264 bgp_evpn_is_prefix_nht_supported(bgp_dest_get_prefix(dest
)))
1265 bgp_evpn_import_route(bgp_path
,
1266 afi
, safi
, bgp_dest_get_prefix(dest
), path
);
1270 bgp_process(bgp_path
, dest
, afi
, safi
);
1274 int valid_nexthops
= bgp_isvalid_nexthop(bnc
);
1276 if (valid_nexthops
) {
1278 * Peering cannot occur across a blackhole nexthop
1280 if (bnc
->nexthop_num
== 1 && bnc
->nexthop
1281 && bnc
->nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1282 peer
->last_reset
= PEER_DOWN_WAITING_NHT
;
1285 peer
->last_reset
= PEER_DOWN_WAITING_OPEN
;
1287 peer
->last_reset
= PEER_DOWN_WAITING_NHT
;
1289 if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
)) {
1290 if (BGP_DEBUG(nht
, NHT
))
1292 "%s: Updating peer (%s(%s)) status with NHT nexthops %d",
1293 __func__
, peer
->host
,
1294 peer
->bgp
->name_pretty
,
1296 bgp_fsm_nht_update(peer
, !!valid_nexthops
);
1297 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
1301 RESET_FLAG(bnc
->change_flags
);
1305 * path_nh_map - make or break path-to-nexthop association.
1307 * path - pointer to the path structure
1308 * bnc - pointer to the nexthop structure
1309 * make - if set, make the association. if unset, just break the existing
1312 void path_nh_map(struct bgp_path_info
*path
, struct bgp_nexthop_cache
*bnc
,
1315 if (path
->nexthop
) {
1316 LIST_REMOVE(path
, nh_thread
);
1317 path
->nexthop
->path_count
--;
1318 path
->nexthop
= NULL
;
1321 LIST_INSERT_HEAD(&(bnc
->paths
), path
, nh_thread
);
1322 path
->nexthop
= bnc
;
1323 path
->nexthop
->path_count
++;
1328 * This function is called to register nexthops to zebra
1329 * as that we may have tried to install the nexthops
1330 * before we actually have a zebra connection
1332 void bgp_nht_register_nexthops(struct bgp
*bgp
)
1334 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
1335 struct bgp_nexthop_cache
*bnc
;
1337 frr_each (bgp_nexthop_cache
, &bgp
->nexthop_cache_table
[afi
],
1339 register_zebra_rnh(bnc
);
1344 void bgp_nht_reg_enhe_cap_intfs(struct peer
*peer
)
1347 struct bgp_nexthop_cache
*bnc
;
1348 struct nexthop
*nhop
;
1349 struct interface
*ifp
;
1351 ifindex_t ifindex
= 0;
1357 if (!sockunion2hostprefix(&peer
->su
, &p
)) {
1358 zlog_warn("%s: Unable to convert sockunion to prefix for %s",
1359 __func__
, peer
->host
);
1363 if (p
.family
!= AF_INET6
)
1366 * Gather the ifindex for if up/down events to be
1367 * tagged into this fun
1369 if (peer
->conf_if
&& IN6_IS_ADDR_LINKLOCAL(&peer
->su
.sin6
.sin6_addr
))
1370 ifindex
= peer
->su
.sin6
.sin6_scope_id
;
1372 bnc
= bnc_find(&bgp
->nexthop_cache_table
[AFI_IP6
], &p
, 0, ifindex
);
1376 if (peer
!= bnc
->nht_info
)
1379 for (nhop
= bnc
->nexthop
; nhop
; nhop
= nhop
->next
) {
1380 ifp
= if_lookup_by_index(nhop
->ifindex
, nhop
->vrf_id
);
1385 zclient_send_interface_radv_req(zclient
,
1388 BGP_UNNUM_DEFAULT_RA_INTERVAL
);
1392 void bgp_nht_dereg_enhe_cap_intfs(struct peer
*peer
)
1395 struct bgp_nexthop_cache
*bnc
;
1396 struct nexthop
*nhop
;
1397 struct interface
*ifp
;
1399 ifindex_t ifindex
= 0;
1406 if (!sockunion2hostprefix(&peer
->su
, &p
)) {
1407 zlog_warn("%s: Unable to convert sockunion to prefix for %s",
1408 __func__
, peer
->host
);
1412 if (p
.family
!= AF_INET6
)
1415 * Gather the ifindex for if up/down events to be
1416 * tagged into this fun
1418 if (peer
->conf_if
&& IN6_IS_ADDR_LINKLOCAL(&peer
->su
.sin6
.sin6_addr
))
1419 ifindex
= peer
->su
.sin6
.sin6_scope_id
;
1421 bnc
= bnc_find(&bgp
->nexthop_cache_table
[AFI_IP6
], &p
, 0, ifindex
);
1425 if (peer
!= bnc
->nht_info
)
1428 for (nhop
= bnc
->nexthop
; nhop
; nhop
= nhop
->next
) {
1429 ifp
= if_lookup_by_index(nhop
->ifindex
, nhop
->vrf_id
);
1434 zclient_send_interface_radv_req(zclient
, nhop
->vrf_id
, ifp
, 0,
1439 /****************************************************************************
1440 * L3 NHGs are used for fast failover of nexthops in the dplane. These are
1441 * the APIs for allocating L3 NHG ids. Management of the L3 NHG itself is
1442 * left to the application using it.
1443 * PS: Currently EVPN host routes is the only app using L3 NHG for fast
1444 * failover of remote ES links.
1445 ***************************************************************************/
1446 static bitfield_t bgp_nh_id_bitmap
;
1447 static uint32_t bgp_l3nhg_start
;
1449 /* XXX - currently we do nothing on the callbacks */
1450 static void bgp_l3nhg_add_cb(const char *name
)
1454 static void bgp_l3nhg_modify_cb(const struct nexthop_group_cmd
*nhgc
)
1458 static void bgp_l3nhg_add_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
1459 const struct nexthop
*nhop
)
1463 static void bgp_l3nhg_del_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
1464 const struct nexthop
*nhop
)
1468 static void bgp_l3nhg_del_cb(const char *name
)
1472 static void bgp_l3nhg_zebra_init(void)
1474 static bool bgp_l3nhg_zebra_inited
;
1475 if (bgp_l3nhg_zebra_inited
)
1478 bgp_l3nhg_zebra_inited
= true;
1479 bgp_l3nhg_start
= zclient_get_nhg_start(ZEBRA_ROUTE_BGP
);
1480 nexthop_group_init(bgp_l3nhg_add_cb
, bgp_l3nhg_modify_cb
,
1481 bgp_l3nhg_add_nexthop_cb
, bgp_l3nhg_del_nexthop_cb
,
1486 void bgp_l3nhg_init(void)
1490 id_max
= MIN(ZEBRA_NHG_PROTO_SPACING
- 1, 16 * 1024);
1491 bf_init(bgp_nh_id_bitmap
, id_max
);
1492 bf_assign_zero_index(bgp_nh_id_bitmap
);
1494 if (BGP_DEBUG(nht
, NHT
) || BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
1495 zlog_debug("bgp l3_nhg range %u - %u", bgp_l3nhg_start
+ 1,
1496 bgp_l3nhg_start
+ id_max
);
1499 void bgp_l3nhg_finish(void)
1501 bf_free(bgp_nh_id_bitmap
);
1504 uint32_t bgp_l3nhg_id_alloc(void)
1506 uint32_t nhg_id
= 0;
1508 bgp_l3nhg_zebra_init();
1509 bf_assign_index(bgp_nh_id_bitmap
, nhg_id
);
1511 nhg_id
+= bgp_l3nhg_start
;
1516 void bgp_l3nhg_id_free(uint32_t nhg_id
)
1518 if (!nhg_id
|| (nhg_id
<= bgp_l3nhg_start
))
1521 nhg_id
-= bgp_l3nhg_start
;
1523 bf_release_index(bgp_nh_id_bitmap
, nhg_id
);