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_mplsovergre(struct bgp_nexthop_cache
*bnc
,
65 struct bgp_path_info
*path
)
67 struct interface
*ifp
= NULL
;
68 struct nexthop
*nexthop
;
70 for (nexthop
= bnc
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
71 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
72 ifp
= if_lookup_by_index(
73 bnc
->ifindex
? bnc
->ifindex
: nexthop
->ifindex
,
75 if (ifp
&& (ifp
->ll_type
== ZEBRA_LLT_IPGRE
||
76 ifp
->ll_type
== ZEBRA_LLT_IP6GRE
))
83 if (CHECK_FLAG(path
->attr
->rmap_change_flags
,
84 BATTR_RMAP_L3VPN_ACCEPT_GRE
))
90 static int bgp_isvalid_nexthop_for_mpls(struct bgp_nexthop_cache
*bnc
,
91 struct bgp_path_info
*path
)
94 * - In the case of MPLS-VPN, the label is learned from LDP or other
95 * protocols, and nexthop tracking is enabled for the label.
96 * The value is recorded as BGP_NEXTHOP_LABELED_VALID.
97 * - In the case of SRv6-VPN, we need to track the reachability to the
98 * SID (in other words, IPv6 address). As in MPLS, we need to record
99 * the value as BGP_NEXTHOP_SID_VALID. However, this function is
100 * currently not implemented, and this function assumes that all
101 * Transit routes for SRv6-VPN are valid.
102 * - Otherwise check for mpls-gre acceptance
104 return (bgp_zebra_num_connects() == 0 ||
105 (bnc
&& (bnc
->nexthop_num
> 0 &&
106 (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_LABELED_VALID
) ||
107 bnc
->bgp
->srv6_enabled
||
108 bgp_isvalid_nexthop_for_mplsovergre(bnc
, path
)))));
111 static void bgp_unlink_nexthop_check(struct bgp_nexthop_cache
*bnc
)
113 if (LIST_EMPTY(&(bnc
->paths
)) && !bnc
->nht_info
) {
114 if (BGP_DEBUG(nht
, NHT
))
115 zlog_debug("%s: freeing bnc %pFX(%d)(%u)(%s)", __func__
,
116 &bnc
->prefix
, bnc
->ifindex
, bnc
->srte_color
,
117 bnc
->bgp
->name_pretty
);
118 /* only unregister if this is the last nh for this prefix*/
119 if (!bnc_existing_for_prefix(bnc
))
120 unregister_zebra_rnh(bnc
);
125 void bgp_unlink_nexthop(struct bgp_path_info
*path
)
127 struct bgp_nexthop_cache
*bnc
= path
->nexthop
;
132 path_nh_map(path
, NULL
, false);
134 bgp_unlink_nexthop_check(bnc
);
137 void bgp_replace_nexthop_by_peer(struct peer
*from
, struct peer
*to
)
141 struct bgp_nexthop_cache
*bncp
, *bnct
;
143 ifindex_t ifindex
= 0;
145 if (!sockunion2hostprefix(&from
->su
, &pp
))
149 * Gather the ifindex for if up/down events to be
150 * tagged into this fun
152 if (from
->conf_if
&& IN6_IS_ADDR_LINKLOCAL(&from
->su
.sin6
.sin6_addr
))
153 ifindex
= from
->su
.sin6
.sin6_scope_id
;
155 afi
= family2afi(pp
.family
);
156 bncp
= bnc_find(&from
->bgp
->nexthop_cache_table
[afi
], &pp
, 0, ifindex
);
158 if (!sockunion2hostprefix(&to
->su
, &pt
))
162 * Gather the ifindex for if up/down events to be
163 * tagged into this fun
166 if (to
->conf_if
&& IN6_IS_ADDR_LINKLOCAL(&to
->su
.sin6
.sin6_addr
))
167 ifindex
= to
->su
.sin6
.sin6_scope_id
;
168 bnct
= bnc_find(&to
->bgp
->nexthop_cache_table
[afi
], &pt
, 0, ifindex
);
177 void bgp_unlink_nexthop_by_peer(struct peer
*peer
)
180 struct bgp_nexthop_cache
*bnc
;
181 afi_t afi
= family2afi(peer
->su
.sa
.sa_family
);
182 ifindex_t ifindex
= 0;
184 if (!sockunion2hostprefix(&peer
->su
, &p
))
187 * Gather the ifindex for if up/down events to be
188 * tagged into this fun
190 if (afi
== AFI_IP6
&& IN6_IS_ADDR_LINKLOCAL(&peer
->su
.sin6
.sin6_addr
))
191 ifindex
= peer
->su
.sin6
.sin6_scope_id
;
192 bnc
= bnc_find(&peer
->bgp
->nexthop_cache_table
[afi
], &p
, 0, ifindex
);
196 /* cleanup the peer reference */
197 bnc
->nht_info
= NULL
;
199 bgp_unlink_nexthop_check(bnc
);
203 * A route and its nexthop might belong to different VRFs. Therefore,
204 * we need both the bgp_route and bgp_nexthop pointers.
206 int bgp_find_or_add_nexthop(struct bgp
*bgp_route
, struct bgp
*bgp_nexthop
,
207 afi_t afi
, safi_t safi
, struct bgp_path_info
*pi
,
208 struct peer
*peer
, int connected
,
209 const struct prefix
*orig_prefix
)
211 struct bgp_nexthop_cache_head
*tree
= NULL
;
212 struct bgp_nexthop_cache
*bnc
;
214 uint32_t srte_color
= 0;
215 int is_bgp_static_route
= 0;
216 ifindex_t ifindex
= 0;
219 is_bgp_static_route
= ((pi
->type
== ZEBRA_ROUTE_BGP
)
220 && (pi
->sub_type
== BGP_ROUTE_STATIC
))
224 /* Since Extended Next-hop Encoding (RFC5549) support, we want
226 address-family from the next-hop. */
227 if (!is_bgp_static_route
)
228 afi
= BGP_ATTR_MP_NEXTHOP_LEN_IP6(pi
->attr
) ? AFI_IP6
231 /* Validation for the ipv4 mapped ipv6 nexthop. */
232 if (IS_MAPPED_IPV6(&pi
->attr
->mp_nexthop_global
)) {
236 /* This will return true if the global IPv6 NH is a link local
238 if (make_prefix(afi
, pi
, &p
) < 0)
241 if (!is_bgp_static_route
&& orig_prefix
242 && prefix_same(&p
, orig_prefix
)) {
243 if (BGP_DEBUG(nht
, NHT
)) {
245 "%s(%pFX): prefix loops through itself",
251 srte_color
= pi
->attr
->srte_color
;
254 * Gather the ifindex for if up/down events to be
255 * tagged into this fun
257 if (afi
== AFI_IP6
&&
258 IN6_IS_ADDR_LINKLOCAL(&peer
->su
.sin6
.sin6_addr
)) {
259 ifindex
= peer
->su
.sin6
.sin6_scope_id
;
261 if (BGP_DEBUG(nht
, NHT
)) {
263 "%s: Unable to locate ifindex, waiting till we have one",
270 if (!sockunion2hostprefix(&peer
->su
, &p
)) {
271 if (BGP_DEBUG(nht
, NHT
)) {
273 "%s: Attempting to register with unknown AFI %d (not %d or %d)",
274 __func__
, afi
, AFI_IP
, AFI_IP6
);
281 if (is_bgp_static_route
)
282 tree
= &bgp_nexthop
->import_check_table
[afi
];
284 tree
= &bgp_nexthop
->nexthop_cache_table
[afi
];
286 bnc
= bnc_find(tree
, &p
, srte_color
, ifindex
);
288 bnc
= bnc_new(tree
, &p
, srte_color
, ifindex
);
289 bnc
->bgp
= bgp_nexthop
;
290 if (BGP_DEBUG(nht
, NHT
))
291 zlog_debug("Allocated bnc %pFX(%d)(%u)(%s) peer %p",
292 &bnc
->prefix
, bnc
->ifindex
, bnc
->srte_color
,
293 bnc
->bgp
->name_pretty
, peer
);
295 if (BGP_DEBUG(nht
, NHT
))
297 "Found existing bnc %pFX(%d)(%s) flags 0x%x ifindex %d #paths %d peer %p",
298 &bnc
->prefix
, bnc
->ifindex
,
299 bnc
->bgp
->name_pretty
, bnc
->flags
, bnc
->ifindex
,
300 bnc
->path_count
, bnc
->nht_info
);
303 if (pi
&& is_route_parent_evpn(pi
))
304 bnc
->is_evpn_gwip_nexthop
= true;
306 if (is_bgp_static_route
) {
307 SET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE
);
309 /* If we're toggling the type, re-register */
310 if ((CHECK_FLAG(bgp_route
->flags
, BGP_FLAG_IMPORT_CHECK
))
311 && !CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
)) {
312 SET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
);
313 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
314 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
315 } else if ((!CHECK_FLAG(bgp_route
->flags
,
316 BGP_FLAG_IMPORT_CHECK
))
317 && CHECK_FLAG(bnc
->flags
,
318 BGP_STATIC_ROUTE_EXACT_MATCH
)) {
319 UNSET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
);
320 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
321 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
324 /* When nexthop is already known, but now requires 'connected'
326 * re-register it. The reverse scenario where the nexthop currently
328 * 'connected' resolution does not need a re-register (i.e., we treat
329 * 'connected-required' as an override) except in the scenario where
331 * is actually a case of tracking a peer for connectivity (e.g., after
332 * disable connected-check).
333 * NOTE: We don't track the number of paths separately for 'connected-
334 * required' vs 'connected-not-required' as this change is not a common
337 else if (connected
&& !CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
)) {
338 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
);
339 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
340 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
341 } else if (peer
&& !connected
342 && CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
)) {
343 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
);
344 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
345 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
347 if (peer
&& (bnc
->ifindex
!= ifindex
)) {
348 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
349 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
350 bnc
->ifindex
= ifindex
;
352 if (bgp_route
->inst_type
== BGP_INSTANCE_TYPE_VIEW
) {
353 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
354 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
355 } else if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
)
356 && !is_default_host_route(&bnc
->prefix
))
357 register_zebra_rnh(bnc
);
359 if (pi
&& pi
->nexthop
!= bnc
) {
360 /* Unlink from existing nexthop cache, if any. This will also
362 * the nexthop cache entry, if appropriate.
364 bgp_unlink_nexthop(pi
);
366 /* updates NHT pi list reference */
367 path_nh_map(pi
, bnc
, true);
369 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
) && bnc
->metric
)
370 (bgp_path_info_extra_get(pi
))->igpmetric
= bnc
->metric
;
372 pi
->extra
->igpmetric
= 0;
375 * Let's not accidentally save the peer data for a peer
376 * we are going to throw away in a second or so.
377 * When we come back around we'll fix up this
378 * data properly in replace_nexthop_by_peer
380 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
381 bnc
->nht_info
= (void *)peer
; /* NHT peer reference */
385 * We are cheating here. Views have no associated underlying
386 * ability to detect nexthops. So when we have a view
387 * just tell everyone the nexthop is valid
389 if (bgp_route
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
391 else if (safi
== SAFI_UNICAST
&& pi
&&
392 pi
->sub_type
== BGP_ROUTE_IMPORTED
&& pi
->extra
&&
393 pi
->extra
->num_labels
&& !bnc
->is_evpn_gwip_nexthop
)
394 return bgp_isvalid_nexthop_for_mpls(bnc
, pi
);
396 return (bgp_isvalid_nexthop(bnc
));
399 void bgp_delete_connected_nexthop(afi_t afi
, struct peer
*peer
)
401 struct bgp_nexthop_cache
*bnc
;
403 ifindex_t ifindex
= 0;
408 if (!sockunion2hostprefix(&peer
->su
, &p
))
411 * Gather the ifindex for if up/down events to be
412 * tagged into this fun
414 if (afi
== AFI_IP6
&& IN6_IS_ADDR_LINKLOCAL(&peer
->su
.sin6
.sin6_addr
))
415 ifindex
= peer
->su
.sin6
.sin6_scope_id
;
416 bnc
= bnc_find(&peer
->bgp
->nexthop_cache_table
[family2afi(p
.family
)],
419 if (BGP_DEBUG(nht
, NHT
))
421 "Cannot find connected NHT node for peer %s(%s)",
422 peer
->host
, peer
->bgp
->name_pretty
);
426 if (bnc
->nht_info
!= peer
) {
427 if (BGP_DEBUG(nht
, NHT
))
429 "Connected NHT %p node for peer %s(%s) points to %p",
430 bnc
, peer
->host
, bnc
->bgp
->name_pretty
,
435 bnc
->nht_info
= NULL
;
437 if (LIST_EMPTY(&(bnc
->paths
))) {
438 if (BGP_DEBUG(nht
, NHT
))
440 "Freeing connected NHT node %p for peer %s(%s)",
441 bnc
, peer
->host
, bnc
->bgp
->name_pretty
);
442 unregister_zebra_rnh(bnc
);
447 static void bgp_process_nexthop_update(struct bgp_nexthop_cache
*bnc
,
448 struct zapi_route
*nhr
,
451 struct nexthop
*nexthop
;
452 struct nexthop
*oldnh
;
453 struct nexthop
*nhlist_head
= NULL
;
454 struct nexthop
*nhlist_tail
= NULL
;
456 bool evpn_resolved
= false;
458 bnc
->last_update
= monotime(NULL
);
459 bnc
->change_flags
= 0;
461 /* debug print the input */
462 if (BGP_DEBUG(nht
, NHT
)) {
463 char bnc_buf
[BNC_FLAG_DUMP_SIZE
];
466 "%s(%u): Rcvd NH update %pFX(%u)%u) - metric %d/%d #nhops %d/%d flags %s",
467 bnc
->bgp
->name_pretty
, bnc
->bgp
->vrf_id
, &nhr
->prefix
,
468 bnc
->ifindex
, bnc
->srte_color
, nhr
->metric
, bnc
->metric
,
469 nhr
->nexthop_num
, bnc
->nexthop_num
,
470 bgp_nexthop_dump_bnc_flags(bnc
, bnc_buf
,
474 if (nhr
->metric
!= bnc
->metric
)
475 bnc
->change_flags
|= BGP_NEXTHOP_METRIC_CHANGED
;
477 if (nhr
->nexthop_num
!= bnc
->nexthop_num
)
478 bnc
->change_flags
|= BGP_NEXTHOP_CHANGED
;
480 if (import_check
&& (nhr
->type
== ZEBRA_ROUTE_BGP
||
481 !prefix_same(&bnc
->prefix
, &nhr
->prefix
))) {
482 SET_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
);
483 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
484 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_LABELED_VALID
);
485 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_EVPN_INCOMPLETE
);
487 bnc_nexthop_free(bnc
);
490 if (BGP_DEBUG(nht
, NHT
))
492 "%s: Import Check does not resolve to the same prefix for %pFX received %pFX or matching route is BGP",
493 __func__
, &bnc
->prefix
, &nhr
->prefix
);
494 } else if (nhr
->nexthop_num
) {
495 struct peer
*peer
= bnc
->nht_info
;
497 /* notify bgp fsm if nbr ip goes from invalid->valid */
498 if (!bnc
->nexthop_num
)
499 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
501 if (!bnc
->is_evpn_gwip_nexthop
)
502 bnc
->flags
|= BGP_NEXTHOP_VALID
;
503 bnc
->metric
= nhr
->metric
;
504 bnc
->nexthop_num
= nhr
->nexthop_num
;
506 bnc
->flags
&= ~BGP_NEXTHOP_LABELED_VALID
; /* check below */
508 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
511 nexthop
= nexthop_from_zapi_nexthop(&nhr
->nexthops
[i
]);
514 * Turn on RA for the v6 nexthops
515 * we receive from bgp. This is to allow us
516 * to work with v4 routing over v6 nexthops
518 if (peer
&& !peer
->ifp
519 && CHECK_FLAG(peer
->flags
,
520 PEER_FLAG_CAPABILITY_ENHE
)
521 && nhr
->prefix
.family
== AF_INET6
522 && nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
523 struct interface
*ifp
;
525 ifp
= if_lookup_by_index(nexthop
->ifindex
,
528 zclient_send_interface_radv_req(
529 zclient
, nexthop
->vrf_id
, ifp
,
531 BGP_UNNUM_DEFAULT_RA_INTERVAL
);
533 /* There is at least one label-switched path */
534 if (nexthop
->nh_label
&&
535 nexthop
->nh_label
->num_labels
) {
537 bnc
->flags
|= BGP_NEXTHOP_LABELED_VALID
;
538 num_labels
= nexthop
->nh_label
->num_labels
;
541 if (BGP_DEBUG(nht
, NHT
)) {
542 char buf
[NEXTHOP_STRLEN
];
544 " nhop via %s (%d labels)",
545 nexthop2str(nexthop
, buf
, sizeof(buf
)),
550 nhlist_tail
->next
= nexthop
;
551 nhlist_tail
= nexthop
;
553 nhlist_tail
= nexthop
;
554 nhlist_head
= nexthop
;
557 /* No need to evaluate the nexthop if we have already
559 * that there has been a change.
561 if (bnc
->change_flags
& BGP_NEXTHOP_CHANGED
)
564 for (oldnh
= bnc
->nexthop
; oldnh
; oldnh
= oldnh
->next
)
565 if (nexthop_same(oldnh
, nexthop
))
569 bnc
->change_flags
|= BGP_NEXTHOP_CHANGED
;
571 bnc_nexthop_free(bnc
);
572 bnc
->nexthop
= nhlist_head
;
575 * Gateway IP nexthop is L3 reachable. Mark it as
576 * BGP_NEXTHOP_VALID only if it is recursively resolved with a
578 * Else, mark it as BGP_NEXTHOP_EVPN_INCOMPLETE.
579 * When its mapping with EVPN RT-2 is established, unset
580 * BGP_NEXTHOP_EVPN_INCOMPLETE and set BGP_NEXTHOP_VALID.
582 if (bnc
->is_evpn_gwip_nexthop
) {
583 evpn_resolved
= bgp_evpn_is_gateway_ip_resolved(bnc
);
585 if (BGP_DEBUG(nht
, NHT
))
587 "EVPN gateway IP %pFX recursive MAC/IP lookup %s",
589 (evpn_resolved
? "successful"
593 bnc
->flags
|= BGP_NEXTHOP_VALID
;
594 bnc
->flags
&= ~BGP_NEXTHOP_EVPN_INCOMPLETE
;
595 bnc
->change_flags
|= BGP_NEXTHOP_MACIP_CHANGED
;
597 bnc
->flags
|= BGP_NEXTHOP_EVPN_INCOMPLETE
;
598 bnc
->flags
&= ~BGP_NEXTHOP_VALID
;
602 bnc
->flags
&= ~BGP_NEXTHOP_EVPN_INCOMPLETE
;
603 bnc
->flags
&= ~BGP_NEXTHOP_VALID
;
604 bnc
->flags
&= ~BGP_NEXTHOP_LABELED_VALID
;
605 bnc
->nexthop_num
= nhr
->nexthop_num
;
607 /* notify bgp fsm if nbr ip goes from valid->invalid */
608 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
610 bnc_nexthop_free(bnc
);
617 static void bgp_nht_ifp_table_handle(struct bgp
*bgp
,
618 struct bgp_nexthop_cache_head
*table
,
619 struct interface
*ifp
, bool up
)
621 struct bgp_nexthop_cache
*bnc
;
623 frr_each (bgp_nexthop_cache
, table
, bnc
) {
624 if (bnc
->ifindex
!= ifp
->ifindex
)
627 bnc
->last_update
= monotime(NULL
);
628 bnc
->change_flags
= 0;
631 * For interface based routes ( ala the v6 LL routes
632 * that this was written for ) the metric received
633 * for the connected route is 0 not 1.
637 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
638 SET_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
);
639 bnc
->nexthop_num
= 1;
641 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
642 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
643 SET_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
);
644 bnc
->nexthop_num
= 0;
650 static void bgp_nht_ifp_handle(struct interface
*ifp
, bool up
)
654 bgp
= ifp
->vrf
->info
;
658 bgp_nht_ifp_table_handle(bgp
, &bgp
->nexthop_cache_table
[AFI_IP
], ifp
,
660 bgp_nht_ifp_table_handle(bgp
, &bgp
->import_check_table
[AFI_IP
], ifp
,
662 bgp_nht_ifp_table_handle(bgp
, &bgp
->nexthop_cache_table
[AFI_IP6
], ifp
,
664 bgp_nht_ifp_table_handle(bgp
, &bgp
->import_check_table
[AFI_IP6
], ifp
,
668 void bgp_nht_ifp_up(struct interface
*ifp
)
670 bgp_nht_ifp_handle(ifp
, true);
673 void bgp_nht_ifp_down(struct interface
*ifp
)
675 bgp_nht_ifp_handle(ifp
, false);
678 static void bgp_nht_ifp_initial(struct thread
*thread
)
680 ifindex_t ifindex
= THREAD_VAL(thread
);
681 struct bgp
*bgp
= THREAD_ARG(thread
);
682 struct interface
*ifp
= if_lookup_by_index(ifindex
, bgp
->vrf_id
);
687 if (BGP_DEBUG(nht
, NHT
))
689 "Handle NHT initial update for Intf %s(%d) status %s",
690 ifp
->name
, ifp
->ifindex
, if_is_up(ifp
) ? "up" : "down");
695 bgp_nht_ifp_down(ifp
);
699 * So the bnc code has the ability to handle interface up/down
700 * events to properly handle v6 LL peering.
701 * What is happening here:
702 * The event system for peering expects the nht code to
703 * report on the tracking events after we move to active
704 * So let's give the system a chance to report on that event
705 * in a manner that is expected.
707 void bgp_nht_interface_events(struct peer
*peer
)
709 struct bgp
*bgp
= peer
->bgp
;
710 struct bgp_nexthop_cache_head
*table
;
711 struct bgp_nexthop_cache
*bnc
;
713 ifindex_t ifindex
= 0;
715 if (!IN6_IS_ADDR_LINKLOCAL(&peer
->su
.sin6
.sin6_addr
))
718 if (!sockunion2hostprefix(&peer
->su
, &p
))
721 * Gather the ifindex for if up/down events to be
722 * tagged into this fun
724 if (peer
->conf_if
&& IN6_IS_ADDR_LINKLOCAL(&peer
->su
.sin6
.sin6_addr
))
725 ifindex
= peer
->su
.sin6
.sin6_scope_id
;
727 table
= &bgp
->nexthop_cache_table
[AFI_IP6
];
728 bnc
= bnc_find(table
, &p
, 0, ifindex
);
733 thread_add_event(bm
->master
, bgp_nht_ifp_initial
, bnc
->bgp
,
737 void bgp_parse_nexthop_update(int command
, vrf_id_t vrf_id
)
739 struct bgp_nexthop_cache_head
*tree
= NULL
;
740 struct bgp_nexthop_cache
*bnc_nhc
, *bnc_import
;
743 struct zapi_route nhr
;
746 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
750 "parse nexthop update: instance not found for vrf_id %u",
755 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &match
, &nhr
)) {
756 zlog_err("%s[%s]: Failure to decode nexthop update", __func__
,
761 afi
= family2afi(match
.family
);
762 tree
= &bgp
->nexthop_cache_table
[afi
];
764 bnc_nhc
= bnc_find(tree
, &match
, nhr
.srte_color
, 0);
766 if (BGP_DEBUG(nht
, NHT
))
768 "parse nexthop update(%pFX(%u)(%s)): bnc info not found for nexthop cache",
769 &nhr
.prefix
, nhr
.srte_color
, bgp
->name_pretty
);
771 bgp_process_nexthop_update(bnc_nhc
, &nhr
, false);
773 tree
= &bgp
->import_check_table
[afi
];
775 bnc_import
= bnc_find(tree
, &match
, nhr
.srte_color
, 0);
777 if (BGP_DEBUG(nht
, NHT
))
779 "parse nexthop update(%pFX(%u)(%s)): bnc info not found for import check",
780 &nhr
.prefix
, nhr
.srte_color
, bgp
->name_pretty
);
782 bgp_process_nexthop_update(bnc_import
, &nhr
, true);
785 * HACK: if any BGP route is dependant on an SR-policy that doesn't
786 * exist, zebra will never send NH updates relative to that policy. In
787 * that case, whenever we receive an update about a colorless NH, update
788 * the corresponding colorful NHs that share the same endpoint but that
789 * are inactive. This ugly hack should work around the problem at the
790 * cost of a performance pernalty. Long term, what should be done is to
791 * make zebra's RNH subsystem aware of SR-TE colors (like bgpd is),
792 * which should provide a better infrastructure to solve this issue in
793 * a more efficient and elegant way.
795 if (nhr
.srte_color
== 0 && bnc_nhc
) {
796 struct bgp_nexthop_cache
*bnc_iter
;
798 frr_each (bgp_nexthop_cache
, &bgp
->nexthop_cache_table
[afi
],
800 if (!prefix_same(&bnc_nhc
->prefix
, &bnc_iter
->prefix
) ||
801 bnc_iter
->srte_color
== 0 ||
802 CHECK_FLAG(bnc_iter
->flags
, BGP_NEXTHOP_VALID
))
805 bgp_process_nexthop_update(bnc_iter
, &nhr
, false);
811 * Cleanup nexthop registration and status information for BGP nexthops
812 * pertaining to this VRF. This is invoked upon VRF deletion.
814 void bgp_cleanup_nexthops(struct bgp
*bgp
)
816 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
817 struct bgp_nexthop_cache
*bnc
;
819 frr_each (bgp_nexthop_cache
, &bgp
->nexthop_cache_table
[afi
],
821 /* Clear relevant flags. */
822 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
823 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
824 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
825 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_EVPN_INCOMPLETE
);
831 * make_prefix - make a prefix structure from the path (essentially
834 static int make_prefix(int afi
, struct bgp_path_info
*pi
, struct prefix
*p
)
837 int is_bgp_static
= ((pi
->type
== ZEBRA_ROUTE_BGP
)
838 && (pi
->sub_type
== BGP_ROUTE_STATIC
))
841 struct bgp_dest
*net
= pi
->net
;
842 const struct prefix
*p_orig
= bgp_dest_get_prefix(net
);
845 if (p_orig
->family
== AF_FLOWSPEC
) {
848 return bgp_flowspec_get_first_nh(pi
->peer
->bgp
,
851 memset(p
, 0, sizeof(struct prefix
));
856 p
->u
.prefix4
= p_orig
->u
.prefix4
;
857 p
->prefixlen
= p_orig
->prefixlen
;
859 if (IS_MAPPED_IPV6(&pi
->attr
->mp_nexthop_global
)) {
860 ipv4_mapped_ipv6_to_ipv4(
861 &pi
->attr
->mp_nexthop_global
, &ipv4
);
863 p
->prefixlen
= IPV4_MAX_BITLEN
;
865 if (p_orig
->family
== AF_EVPN
)
867 pi
->attr
->mp_nexthop_global_in
;
869 p
->u
.prefix4
= pi
->attr
->nexthop
;
870 p
->prefixlen
= IPV4_MAX_BITLEN
;
875 p
->family
= AF_INET6
;
878 p
->u
.prefix6
= p_orig
->u
.prefix6
;
879 p
->prefixlen
= p_orig
->prefixlen
;
881 /* If we receive MP_REACH nexthop with ::(LL)
882 * or LL(LL), use LL address as nexthop cache.
884 if (pi
->attr
->mp_nexthop_len
885 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
886 && (IN6_IS_ADDR_UNSPECIFIED(
887 &pi
->attr
->mp_nexthop_global
)
888 || IN6_IS_ADDR_LINKLOCAL(
889 &pi
->attr
->mp_nexthop_global
)))
890 p
->u
.prefix6
= pi
->attr
->mp_nexthop_local
;
891 /* If we receive MR_REACH with (GA)::(LL)
892 * then check for route-map to choose GA or LL
894 else if (pi
->attr
->mp_nexthop_len
895 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
896 if (pi
->attr
->mp_nexthop_prefer_global
)
898 pi
->attr
->mp_nexthop_global
;
901 pi
->attr
->mp_nexthop_local
;
903 p
->u
.prefix6
= pi
->attr
->mp_nexthop_global
;
904 p
->prefixlen
= IPV6_MAX_BITLEN
;
908 if (BGP_DEBUG(nht
, NHT
)) {
910 "%s: Attempting to make prefix with unknown AFI %d (not %d or %d)",
911 __func__
, afi
, AFI_IP
, AFI_IP6
);
919 * sendmsg_zebra_rnh -- Format and send a nexthop register/Unregister
922 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
923 * int command -- command to send to zebra
927 static void sendmsg_zebra_rnh(struct bgp_nexthop_cache
*bnc
, int command
)
929 bool exact_match
= false;
930 bool resolve_via_default
= false;
936 /* Don't try to register if Zebra doesn't know of this instance. */
937 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bnc
->bgp
)) {
938 if (BGP_DEBUG(zebra
, ZEBRA
))
940 "%s: No zebra instance to talk to, not installing NHT entry",
945 if (!bgp_zebra_num_connects()) {
946 if (BGP_DEBUG(zebra
, ZEBRA
))
948 "%s: We have not connected yet, cannot send nexthops",
951 if (command
== ZEBRA_NEXTHOP_REGISTER
) {
952 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
))
954 if (CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
))
955 resolve_via_default
= true;
958 if (BGP_DEBUG(zebra
, ZEBRA
))
959 zlog_debug("%s: sending cmd %s for %pFX (vrf %s)", __func__
,
960 zserv_command_string(command
), &bnc
->prefix
,
961 bnc
->bgp
->name_pretty
);
963 ret
= zclient_send_rnh(zclient
, command
, &bnc
->prefix
, SAFI_UNICAST
,
964 exact_match
, resolve_via_default
,
966 if (ret
== ZCLIENT_SEND_FAILURE
) {
967 flog_warn(EC_BGP_ZEBRA_SEND
,
968 "sendmsg_nexthop: zclient_send_message() failed");
972 if (command
== ZEBRA_NEXTHOP_REGISTER
)
973 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
974 else if (command
== ZEBRA_NEXTHOP_UNREGISTER
)
975 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
980 * register_zebra_rnh - register a NH/route with Zebra for notification
981 * when the route or the route to the nexthop changes.
983 * struct bgp_nexthop_cache *bnc
987 static void register_zebra_rnh(struct bgp_nexthop_cache
*bnc
)
989 /* Check if we have already registered */
990 if (bnc
->flags
& BGP_NEXTHOP_REGISTERED
)
994 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
998 sendmsg_zebra_rnh(bnc
, ZEBRA_NEXTHOP_REGISTER
);
1002 * unregister_zebra_rnh -- Unregister the route/nexthop from Zebra.
1004 * struct bgp_nexthop_cache *bnc
1008 static void unregister_zebra_rnh(struct bgp_nexthop_cache
*bnc
)
1010 /* Check if we have already registered */
1011 if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
))
1015 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
1019 sendmsg_zebra_rnh(bnc
, ZEBRA_NEXTHOP_UNREGISTER
);
1023 * evaluate_paths - Evaluate the paths/nets associated with a nexthop.
1025 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
1029 void evaluate_paths(struct bgp_nexthop_cache
*bnc
)
1031 struct bgp_dest
*dest
;
1032 struct bgp_path_info
*path
;
1034 struct peer
*peer
= (struct peer
*)bnc
->nht_info
;
1035 struct bgp_table
*table
;
1037 struct bgp
*bgp_path
;
1038 const struct prefix
*p
;
1040 if (BGP_DEBUG(nht
, NHT
)) {
1041 char bnc_buf
[BNC_FLAG_DUMP_SIZE
];
1042 char chg_buf
[BNC_FLAG_DUMP_SIZE
];
1045 "NH update for %pFX(%d)(%u)(%s) - flags %s chgflags %s- evaluate paths",
1046 &bnc
->prefix
, bnc
->ifindex
, bnc
->srte_color
,
1047 bnc
->bgp
->name_pretty
,
1048 bgp_nexthop_dump_bnc_flags(bnc
, bnc_buf
,
1050 bgp_nexthop_dump_bnc_change_flags(bnc
, chg_buf
,
1054 LIST_FOREACH (path
, &(bnc
->paths
), nh_thread
) {
1055 if (!(path
->type
== ZEBRA_ROUTE_BGP
1056 && ((path
->sub_type
== BGP_ROUTE_NORMAL
)
1057 || (path
->sub_type
== BGP_ROUTE_STATIC
)
1058 || (path
->sub_type
== BGP_ROUTE_IMPORTED
))))
1062 assert(dest
&& bgp_dest_table(dest
));
1063 p
= bgp_dest_get_prefix(dest
);
1064 afi
= family2afi(p
->family
);
1065 table
= bgp_dest_table(dest
);
1069 * handle routes from other VRFs (they can have a
1070 * nexthop in THIS VRF). bgp_path is the bgp instance
1071 * that owns the route referencing this nexthop.
1073 bgp_path
= table
->bgp
;
1076 * Path becomes valid/invalid depending on whether the nexthop
1077 * reachable/unreachable.
1079 * In case of unicast routes that were imported from vpn
1080 * and that have labels, they are valid only if there are
1081 * nexthops with labels
1083 * If the nexthop is EVPN gateway-IP,
1084 * do not check for a valid label.
1087 bool bnc_is_valid_nexthop
= false;
1088 bool path_valid
= false;
1090 if (safi
== SAFI_UNICAST
&& path
->sub_type
== BGP_ROUTE_IMPORTED
1091 && path
->extra
&& path
->extra
->num_labels
1092 && (path
->attr
->evpn_overlay
.type
1093 != OVERLAY_INDEX_GATEWAY_IP
)) {
1094 bnc_is_valid_nexthop
=
1095 bgp_isvalid_nexthop_for_mpls(bnc
, path
) ? true
1098 if (bgp_update_martian_nexthop(
1099 bnc
->bgp
, afi
, safi
, path
->type
,
1100 path
->sub_type
, path
->attr
, dest
)) {
1101 if (BGP_DEBUG(nht
, NHT
))
1103 "%s: prefix %pBD (vrf %s), ignoring path due to martian or self-next-hop",
1104 __func__
, dest
, bgp_path
->name
);
1106 bnc_is_valid_nexthop
=
1107 bgp_isvalid_nexthop(bnc
) ? true : false;
1110 if (BGP_DEBUG(nht
, NHT
)) {
1111 char buf1
[RD_ADDRSTRLEN
];
1114 prefix_rd2str((struct prefix_rd
*)bgp_dest_get_prefix(dest
->pdest
),
1115 buf1
, sizeof(buf1
));
1117 "... eval path %d/%d %pBD RD %s %s flags 0x%x",
1118 afi
, safi
, dest
, buf1
,
1119 bgp_path
->name_pretty
, path
->flags
);
1122 "... eval path %d/%d %pBD %s flags 0x%x",
1123 afi
, safi
, dest
, bgp_path
->name_pretty
,
1127 /* Skip paths marked for removal or as history. */
1128 if (CHECK_FLAG(path
->flags
, BGP_PATH_REMOVED
)
1129 || CHECK_FLAG(path
->flags
, BGP_PATH_HISTORY
))
1132 /* Copy the metric to the path. Will be used for bestpath
1134 if (bgp_isvalid_nexthop(bnc
) && bnc
->metric
)
1135 (bgp_path_info_extra_get(path
))->igpmetric
=
1137 else if (path
->extra
)
1138 path
->extra
->igpmetric
= 0;
1140 if (CHECK_FLAG(bnc
->change_flags
, BGP_NEXTHOP_METRIC_CHANGED
)
1141 || CHECK_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
)
1142 || path
->attr
->srte_color
!= 0)
1143 SET_FLAG(path
->flags
, BGP_PATH_IGP_CHANGED
);
1145 path_valid
= CHECK_FLAG(path
->flags
, BGP_PATH_VALID
);
1146 if (path_valid
!= bnc_is_valid_nexthop
) {
1148 /* No longer valid, clear flag; also for EVPN
1149 * routes, unimport from VRFs if needed.
1151 bgp_aggregate_decrement(bgp_path
, p
, path
, afi
,
1153 bgp_path_info_unset_flag(dest
, path
,
1155 if (safi
== SAFI_EVPN
&&
1156 bgp_evpn_is_prefix_nht_supported(bgp_dest_get_prefix(dest
)))
1157 bgp_evpn_unimport_route(bgp_path
,
1158 afi
, safi
, bgp_dest_get_prefix(dest
), path
);
1160 /* Path becomes valid, set flag; also for EVPN
1161 * routes, import from VRFs if needed.
1163 bgp_path_info_set_flag(dest
, path
,
1165 bgp_aggregate_increment(bgp_path
, p
, path
, afi
,
1167 if (safi
== SAFI_EVPN
&&
1168 bgp_evpn_is_prefix_nht_supported(bgp_dest_get_prefix(dest
)))
1169 bgp_evpn_import_route(bgp_path
,
1170 afi
, safi
, bgp_dest_get_prefix(dest
), path
);
1174 bgp_process(bgp_path
, dest
, afi
, safi
);
1178 int valid_nexthops
= bgp_isvalid_nexthop(bnc
);
1180 if (valid_nexthops
) {
1182 * Peering cannot occur across a blackhole nexthop
1184 if (bnc
->nexthop_num
== 1 && bnc
->nexthop
1185 && bnc
->nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1186 peer
->last_reset
= PEER_DOWN_WAITING_NHT
;
1189 peer
->last_reset
= PEER_DOWN_WAITING_OPEN
;
1191 peer
->last_reset
= PEER_DOWN_WAITING_NHT
;
1193 if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
)) {
1194 if (BGP_DEBUG(nht
, NHT
))
1196 "%s: Updating peer (%s(%s)) status with NHT nexthops %d",
1197 __func__
, peer
->host
,
1198 peer
->bgp
->name_pretty
,
1200 bgp_fsm_nht_update(peer
, !!valid_nexthops
);
1201 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
1205 RESET_FLAG(bnc
->change_flags
);
1209 * path_nh_map - make or break path-to-nexthop association.
1211 * path - pointer to the path structure
1212 * bnc - pointer to the nexthop structure
1213 * make - if set, make the association. if unset, just break the existing
1216 void path_nh_map(struct bgp_path_info
*path
, struct bgp_nexthop_cache
*bnc
,
1219 if (path
->nexthop
) {
1220 LIST_REMOVE(path
, nh_thread
);
1221 path
->nexthop
->path_count
--;
1222 path
->nexthop
= NULL
;
1225 LIST_INSERT_HEAD(&(bnc
->paths
), path
, nh_thread
);
1226 path
->nexthop
= bnc
;
1227 path
->nexthop
->path_count
++;
1232 * This function is called to register nexthops to zebra
1233 * as that we may have tried to install the nexthops
1234 * before we actually have a zebra connection
1236 void bgp_nht_register_nexthops(struct bgp
*bgp
)
1238 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
1239 struct bgp_nexthop_cache
*bnc
;
1241 frr_each (bgp_nexthop_cache
, &bgp
->nexthop_cache_table
[afi
],
1243 register_zebra_rnh(bnc
);
1248 void bgp_nht_reg_enhe_cap_intfs(struct peer
*peer
)
1251 struct bgp_nexthop_cache
*bnc
;
1252 struct nexthop
*nhop
;
1253 struct interface
*ifp
;
1255 ifindex_t ifindex
= 0;
1261 if (!sockunion2hostprefix(&peer
->su
, &p
)) {
1262 zlog_warn("%s: Unable to convert sockunion to prefix for %s",
1263 __func__
, peer
->host
);
1267 if (p
.family
!= AF_INET6
)
1270 * Gather the ifindex for if up/down events to be
1271 * tagged into this fun
1273 if (peer
->conf_if
&& IN6_IS_ADDR_LINKLOCAL(&peer
->su
.sin6
.sin6_addr
))
1274 ifindex
= peer
->su
.sin6
.sin6_scope_id
;
1276 bnc
= bnc_find(&bgp
->nexthop_cache_table
[AFI_IP6
], &p
, 0, ifindex
);
1280 if (peer
!= bnc
->nht_info
)
1283 for (nhop
= bnc
->nexthop
; nhop
; nhop
= nhop
->next
) {
1284 ifp
= if_lookup_by_index(nhop
->ifindex
, nhop
->vrf_id
);
1289 zclient_send_interface_radv_req(zclient
,
1292 BGP_UNNUM_DEFAULT_RA_INTERVAL
);
1296 void bgp_nht_dereg_enhe_cap_intfs(struct peer
*peer
)
1299 struct bgp_nexthop_cache
*bnc
;
1300 struct nexthop
*nhop
;
1301 struct interface
*ifp
;
1303 ifindex_t ifindex
= 0;
1310 if (!sockunion2hostprefix(&peer
->su
, &p
)) {
1311 zlog_warn("%s: Unable to convert sockunion to prefix for %s",
1312 __func__
, peer
->host
);
1316 if (p
.family
!= AF_INET6
)
1319 * Gather the ifindex for if up/down events to be
1320 * tagged into this fun
1322 if (peer
->conf_if
&& IN6_IS_ADDR_LINKLOCAL(&peer
->su
.sin6
.sin6_addr
))
1323 ifindex
= peer
->su
.sin6
.sin6_scope_id
;
1325 bnc
= bnc_find(&bgp
->nexthop_cache_table
[AFI_IP6
], &p
, 0, ifindex
);
1329 if (peer
!= bnc
->nht_info
)
1332 for (nhop
= bnc
->nexthop
; nhop
; nhop
= nhop
->next
) {
1333 ifp
= if_lookup_by_index(nhop
->ifindex
, nhop
->vrf_id
);
1338 zclient_send_interface_radv_req(zclient
, nhop
->vrf_id
, ifp
, 0,
1343 /****************************************************************************
1344 * L3 NHGs are used for fast failover of nexthops in the dplane. These are
1345 * the APIs for allocating L3 NHG ids. Management of the L3 NHG itself is
1346 * left to the application using it.
1347 * PS: Currently EVPN host routes is the only app using L3 NHG for fast
1348 * failover of remote ES links.
1349 ***************************************************************************/
1350 static bitfield_t bgp_nh_id_bitmap
;
1351 static uint32_t bgp_l3nhg_start
;
1353 /* XXX - currently we do nothing on the callbacks */
1354 static void bgp_l3nhg_add_cb(const char *name
)
1357 static void bgp_l3nhg_add_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
1358 const struct nexthop
*nhop
)
1361 static void bgp_l3nhg_del_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
1362 const struct nexthop
*nhop
)
1365 static void bgp_l3nhg_del_cb(const char *name
)
1369 static void bgp_l3nhg_zebra_init(void)
1371 static bool bgp_l3nhg_zebra_inited
;
1372 if (bgp_l3nhg_zebra_inited
)
1375 bgp_l3nhg_zebra_inited
= true;
1376 bgp_l3nhg_start
= zclient_get_nhg_start(ZEBRA_ROUTE_BGP
);
1377 nexthop_group_init(bgp_l3nhg_add_cb
, bgp_l3nhg_add_nexthop_cb
,
1378 bgp_l3nhg_del_nexthop_cb
, bgp_l3nhg_del_cb
);
1382 void bgp_l3nhg_init(void)
1386 id_max
= MIN(ZEBRA_NHG_PROTO_SPACING
- 1, 16 * 1024);
1387 bf_init(bgp_nh_id_bitmap
, id_max
);
1388 bf_assign_zero_index(bgp_nh_id_bitmap
);
1390 if (BGP_DEBUG(nht
, NHT
) || BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
1391 zlog_debug("bgp l3_nhg range %u - %u", bgp_l3nhg_start
+ 1,
1392 bgp_l3nhg_start
+ id_max
);
1395 void bgp_l3nhg_finish(void)
1397 bf_free(bgp_nh_id_bitmap
);
1400 uint32_t bgp_l3nhg_id_alloc(void)
1402 uint32_t nhg_id
= 0;
1404 bgp_l3nhg_zebra_init();
1405 bf_assign_index(bgp_nh_id_bitmap
, nhg_id
);
1407 nhg_id
+= bgp_l3nhg_start
;
1412 void bgp_l3nhg_id_free(uint32_t nhg_id
)
1414 if (!nhg_id
|| (nhg_id
<= bgp_l3nhg_start
))
1417 nhg_id
-= bgp_l3nhg_start
;
1419 bf_release_index(bgp_nh_id_bitmap
, nhg_id
);