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 int is_bgp_static_route
);
54 static void unregister_zebra_rnh(struct bgp_nexthop_cache
*bnc
,
55 int is_bgp_static_route
);
56 static int make_prefix(int afi
, struct bgp_path_info
*pi
, struct prefix
*p
);
57 static int bgp_nht_ifp_initial(struct thread
*thread
);
59 static int bgp_isvalid_nexthop(struct bgp_nexthop_cache
*bnc
)
61 return (bgp_zebra_num_connects() == 0
62 || (bnc
&& CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
)
63 && bnc
->nexthop_num
> 0));
66 static int bgp_isvalid_labeled_nexthop(struct bgp_nexthop_cache
*bnc
)
69 * In the case of MPLS-VPN, the label is learned from LDP or other
70 * protocols, and nexthop tracking is enabled for the label.
71 * The value is recorded as BGP_NEXTHOP_LABELED_VALID.
72 * In the case of SRv6-VPN, we need to track the reachability to the
73 * SID (in other words, IPv6 address). As in MPLS, we need to record
74 * the value as BGP_NEXTHOP_SID_VALID. However, this function is
75 * currently not implemented, and this function assumes that all
76 * Transit routes for SRv6-VPN are valid.
78 return (bgp_zebra_num_connects() == 0
79 || (bnc
&& bnc
->nexthop_num
> 0
80 && (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_LABELED_VALID
)
81 || bnc
->bgp
->srv6_enabled
)));
84 static void bgp_unlink_nexthop_check(struct bgp_nexthop_cache
*bnc
)
86 if (LIST_EMPTY(&(bnc
->paths
)) && !bnc
->nht_info
) {
87 if (BGP_DEBUG(nht
, NHT
)) {
88 char buf
[PREFIX2STR_BUFFER
];
89 zlog_debug("%s: freeing bnc %s(%u)(%s)", __func__
,
90 bnc_str(bnc
, buf
, PREFIX2STR_BUFFER
),
91 bnc
->srte_color
, bnc
->bgp
->name_pretty
);
93 /* only unregister if this is the last nh for this prefix*/
94 if (!bnc_existing_for_prefix(bnc
))
96 bnc
, CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE
));
101 void bgp_unlink_nexthop(struct bgp_path_info
*path
)
103 struct bgp_nexthop_cache
*bnc
= path
->nexthop
;
108 path_nh_map(path
, NULL
, false);
110 bgp_unlink_nexthop_check(bnc
);
113 void bgp_replace_nexthop_by_peer(struct peer
*from
, struct peer
*to
)
117 struct bgp_nexthop_cache
*bncp
, *bnct
;
120 if (!sockunion2hostprefix(&from
->su
, &pp
))
123 afi
= family2afi(pp
.family
);
124 bncp
= bnc_find(&from
->bgp
->nexthop_cache_table
[afi
], &pp
, 0);
126 if (!sockunion2hostprefix(&to
->su
, &pt
))
129 bnct
= bnc_find(&to
->bgp
->nexthop_cache_table
[afi
], &pt
, 0);
138 void bgp_unlink_nexthop_by_peer(struct peer
*peer
)
141 struct bgp_nexthop_cache
*bnc
;
142 afi_t afi
= family2afi(peer
->su
.sa
.sa_family
);
144 if (!sockunion2hostprefix(&peer
->su
, &p
))
147 bnc
= bnc_find(&peer
->bgp
->nexthop_cache_table
[afi
], &p
, 0);
151 /* cleanup the peer reference */
152 bnc
->nht_info
= NULL
;
154 bgp_unlink_nexthop_check(bnc
);
158 * A route and its nexthop might belong to different VRFs. Therefore,
159 * we need both the bgp_route and bgp_nexthop pointers.
161 int bgp_find_or_add_nexthop(struct bgp
*bgp_route
, struct bgp
*bgp_nexthop
,
162 afi_t afi
, safi_t safi
, struct bgp_path_info
*pi
,
163 struct peer
*peer
, int connected
,
164 const struct prefix
*orig_prefix
)
166 struct bgp_nexthop_cache_head
*tree
= NULL
;
167 struct bgp_nexthop_cache
*bnc
;
169 uint32_t srte_color
= 0;
170 int is_bgp_static_route
= 0;
171 ifindex_t ifindex
= 0;
174 is_bgp_static_route
= ((pi
->type
== ZEBRA_ROUTE_BGP
)
175 && (pi
->sub_type
== BGP_ROUTE_STATIC
))
179 /* Since Extended Next-hop Encoding (RFC5549) support, we want
181 address-family from the next-hop. */
182 if (!is_bgp_static_route
)
183 afi
= BGP_ATTR_NEXTHOP_AFI_IP6(pi
->attr
) ? AFI_IP6
186 /* Validation for the ipv4 mapped ipv6 nexthop. */
187 if (IS_MAPPED_IPV6(&pi
->attr
->mp_nexthop_global
)) {
191 /* This will return true if the global IPv6 NH is a link local
193 if (make_prefix(afi
, pi
, &p
) < 0)
196 if (!is_bgp_static_route
&& orig_prefix
197 && prefix_same(&p
, orig_prefix
)) {
198 if (BGP_DEBUG(nht
, NHT
)) {
200 "%s(%pFX): prefix loops through itself",
206 srte_color
= pi
->attr
->srte_color
;
209 * Gather the ifindex for if up/down events to be
210 * tagged into this fun
213 && IN6_IS_ADDR_LINKLOCAL(&peer
->su
.sin6
.sin6_addr
))
214 ifindex
= peer
->su
.sin6
.sin6_scope_id
;
216 if (!sockunion2hostprefix(&peer
->su
, &p
)) {
217 if (BGP_DEBUG(nht
, NHT
)) {
219 "%s: Attempting to register with unknown AFI %d (not %d or %d)",
220 __func__
, afi
, AFI_IP
, AFI_IP6
);
227 if (is_bgp_static_route
)
228 tree
= &bgp_nexthop
->import_check_table
[afi
];
230 tree
= &bgp_nexthop
->nexthop_cache_table
[afi
];
232 bnc
= bnc_find(tree
, &p
, srte_color
);
234 bnc
= bnc_new(tree
, &p
, srte_color
);
235 bnc
->bgp
= bgp_nexthop
;
236 bnc
->ifindex
= ifindex
;
237 if (BGP_DEBUG(nht
, NHT
)) {
238 char buf
[PREFIX2STR_BUFFER
];
240 zlog_debug("Allocated bnc %s(%u)(%s) peer %p",
241 bnc_str(bnc
, buf
, PREFIX2STR_BUFFER
),
242 bnc
->srte_color
, bnc
->bgp
->name_pretty
,
246 if (BGP_DEBUG(nht
, NHT
)) {
247 char buf
[PREFIX2STR_BUFFER
];
250 "Found existing bnc %s(%s) flags 0x%x ifindex %d #paths %d peer %p",
251 bnc_str(bnc
, buf
, PREFIX2STR_BUFFER
),
252 bnc
->bgp
->name_pretty
, bnc
->flags
, bnc
->ifindex
,
253 bnc
->path_count
, bnc
->nht_info
);
257 if (pi
&& is_route_parent_evpn(pi
))
258 bnc
->is_evpn_gwip_nexthop
= true;
260 if (is_bgp_static_route
) {
261 SET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE
);
263 /* If we're toggling the type, re-register */
264 if ((CHECK_FLAG(bgp_route
->flags
, BGP_FLAG_IMPORT_CHECK
))
265 && !CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
)) {
266 SET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
);
267 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
268 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
269 } else if ((!CHECK_FLAG(bgp_route
->flags
,
270 BGP_FLAG_IMPORT_CHECK
))
271 && CHECK_FLAG(bnc
->flags
,
272 BGP_STATIC_ROUTE_EXACT_MATCH
)) {
273 UNSET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
);
274 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
275 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
278 /* When nexthop is already known, but now requires 'connected'
280 * re-register it. The reverse scenario where the nexthop currently
282 * 'connected' resolution does not need a re-register (i.e., we treat
283 * 'connected-required' as an override) except in the scenario where
285 * is actually a case of tracking a peer for connectivity (e.g., after
286 * disable connected-check).
287 * NOTE: We don't track the number of paths separately for 'connected-
288 * required' vs 'connected-not-required' as this change is not a common
291 else if (connected
&& !CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
)) {
292 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
);
293 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
294 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
295 } else if (peer
&& !connected
296 && CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
)) {
297 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
);
298 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
299 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
301 if (peer
&& (bnc
->ifindex
!= ifindex
)) {
302 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
303 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
304 bnc
->ifindex
= ifindex
;
306 if (bgp_route
->inst_type
== BGP_INSTANCE_TYPE_VIEW
) {
307 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
308 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
309 } else if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
)
310 && !is_default_host_route(&bnc
->prefix
))
311 register_zebra_rnh(bnc
, is_bgp_static_route
);
313 if (pi
&& pi
->nexthop
!= bnc
) {
314 /* Unlink from existing nexthop cache, if any. This will also
316 * the nexthop cache entry, if appropriate.
318 bgp_unlink_nexthop(pi
);
320 /* updates NHT pi list reference */
321 path_nh_map(pi
, bnc
, true);
323 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
) && bnc
->metric
)
324 (bgp_path_info_extra_get(pi
))->igpmetric
= bnc
->metric
;
326 pi
->extra
->igpmetric
= 0;
329 * Let's not accidently save the peer data for a peer
330 * we are going to throw away in a second or so.
331 * When we come back around we'll fix up this
332 * data properly in replace_nexthop_by_peer
334 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
335 bnc
->nht_info
= (void *)peer
; /* NHT peer reference */
339 * We are cheating here. Views have no associated underlying
340 * ability to detect nexthops. So when we have a view
341 * just tell everyone the nexthop is valid
343 if (bgp_route
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
345 else if (safi
== SAFI_UNICAST
&& pi
346 && pi
->sub_type
== BGP_ROUTE_IMPORTED
&& pi
->extra
347 && pi
->extra
->num_labels
&& !bnc
->is_evpn_gwip_nexthop
) {
348 return bgp_isvalid_labeled_nexthop(bnc
);
350 return (bgp_isvalid_nexthop(bnc
));
353 void bgp_delete_connected_nexthop(afi_t afi
, struct peer
*peer
)
355 struct bgp_nexthop_cache
*bnc
;
361 if (!sockunion2hostprefix(&peer
->su
, &p
))
364 bnc
= bnc_find(&peer
->bgp
->nexthop_cache_table
[family2afi(p
.family
)],
367 if (BGP_DEBUG(nht
, NHT
))
369 "Cannot find connected NHT node for peer %s(%s)",
370 peer
->host
, peer
->bgp
->name_pretty
);
374 if (bnc
->nht_info
!= peer
) {
375 if (BGP_DEBUG(nht
, NHT
))
377 "Connected NHT %p node for peer %s(%s) points to %p",
378 bnc
, peer
->host
, bnc
->bgp
->name_pretty
,
383 bnc
->nht_info
= NULL
;
385 if (LIST_EMPTY(&(bnc
->paths
))) {
386 if (BGP_DEBUG(nht
, NHT
))
388 "Freeing connected NHT node %p for peer %s(%s)",
389 bnc
, peer
->host
, bnc
->bgp
->name_pretty
);
390 unregister_zebra_rnh(bnc
, 0);
395 static void bgp_process_nexthop_update(struct bgp_nexthop_cache
*bnc
,
396 struct zapi_route
*nhr
)
398 struct nexthop
*nexthop
;
399 struct nexthop
*oldnh
;
400 struct nexthop
*nhlist_head
= NULL
;
401 struct nexthop
*nhlist_tail
= NULL
;
403 bool evpn_resolved
= false;
405 bnc
->last_update
= bgp_clock();
406 bnc
->change_flags
= 0;
408 /* debug print the input */
409 if (BGP_DEBUG(nht
, NHT
)) {
410 char bnc_buf
[BNC_FLAG_DUMP_SIZE
];
413 "%s(%u): Rcvd NH update %pFX(%u) - metric %d/%d #nhops %d/%d flags %s",
414 bnc
->bgp
->name_pretty
, bnc
->bgp
->vrf_id
, &nhr
->prefix
,
415 bnc
->srte_color
, nhr
->metric
, bnc
->metric
,
416 nhr
->nexthop_num
, bnc
->nexthop_num
,
417 bgp_nexthop_dump_bnc_flags(bnc
, bnc_buf
,
421 if (nhr
->metric
!= bnc
->metric
)
422 bnc
->change_flags
|= BGP_NEXTHOP_METRIC_CHANGED
;
424 if (nhr
->nexthop_num
!= bnc
->nexthop_num
)
425 bnc
->change_flags
|= BGP_NEXTHOP_CHANGED
;
427 if (nhr
->nexthop_num
) {
428 struct peer
*peer
= bnc
->nht_info
;
430 /* notify bgp fsm if nbr ip goes from invalid->valid */
431 if (!bnc
->nexthop_num
)
432 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
434 if (!bnc
->is_evpn_gwip_nexthop
)
435 bnc
->flags
|= BGP_NEXTHOP_VALID
;
436 bnc
->metric
= nhr
->metric
;
437 bnc
->nexthop_num
= nhr
->nexthop_num
;
439 bnc
->flags
&= ~BGP_NEXTHOP_LABELED_VALID
; /* check below */
441 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
444 nexthop
= nexthop_from_zapi_nexthop(&nhr
->nexthops
[i
]);
447 * Turn on RA for the v6 nexthops
448 * we receive from bgp. This is to allow us
449 * to work with v4 routing over v6 nexthops
451 if (peer
&& !peer
->ifp
452 && CHECK_FLAG(peer
->flags
,
453 PEER_FLAG_CAPABILITY_ENHE
)
454 && nhr
->prefix
.family
== AF_INET6
455 && nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
456 struct interface
*ifp
;
458 ifp
= if_lookup_by_index(nexthop
->ifindex
,
461 zclient_send_interface_radv_req(
462 zclient
, nexthop
->vrf_id
, ifp
,
464 BGP_UNNUM_DEFAULT_RA_INTERVAL
);
466 /* There is at least one label-switched path */
467 if (nexthop
->nh_label
&&
468 nexthop
->nh_label
->num_labels
) {
470 bnc
->flags
|= BGP_NEXTHOP_LABELED_VALID
;
471 num_labels
= nexthop
->nh_label
->num_labels
;
474 if (BGP_DEBUG(nht
, NHT
)) {
475 char buf
[NEXTHOP_STRLEN
];
477 " nhop via %s (%d labels)",
478 nexthop2str(nexthop
, buf
, sizeof(buf
)),
483 nhlist_tail
->next
= nexthop
;
484 nhlist_tail
= nexthop
;
486 nhlist_tail
= nexthop
;
487 nhlist_head
= nexthop
;
490 /* No need to evaluate the nexthop if we have already
492 * that there has been a change.
494 if (bnc
->change_flags
& BGP_NEXTHOP_CHANGED
)
497 for (oldnh
= bnc
->nexthop
; oldnh
; oldnh
= oldnh
->next
)
498 if (nexthop_same(oldnh
, nexthop
))
502 bnc
->change_flags
|= BGP_NEXTHOP_CHANGED
;
504 bnc_nexthop_free(bnc
);
505 bnc
->nexthop
= nhlist_head
;
508 * Gateway IP nexthop is L3 reachable. Mark it as
509 * BGP_NEXTHOP_VALID only if it is recursively resolved with a
511 * Else, mark it as BGP_NEXTHOP_EVPN_INCOMPLETE.
512 * When its mapping with EVPN RT-2 is established, unset
513 * BGP_NEXTHOP_EVPN_INCOMPLETE and set BGP_NEXTHOP_VALID.
515 if (bnc
->is_evpn_gwip_nexthop
) {
516 evpn_resolved
= bgp_evpn_is_gateway_ip_resolved(bnc
);
518 if (BGP_DEBUG(nht
, NHT
)) {
519 char buf2
[PREFIX2STR_BUFFER
];
521 prefix2str(&bnc
->prefix
, buf2
, sizeof(buf2
));
523 "EVPN gateway IP %s recursive MAC/IP lookup %s",
525 (evpn_resolved
? "successful"
530 bnc
->flags
|= BGP_NEXTHOP_VALID
;
531 bnc
->flags
&= ~BGP_NEXTHOP_EVPN_INCOMPLETE
;
532 bnc
->change_flags
|= BGP_NEXTHOP_MACIP_CHANGED
;
534 bnc
->flags
|= BGP_NEXTHOP_EVPN_INCOMPLETE
;
535 bnc
->flags
&= ~BGP_NEXTHOP_VALID
;
539 bnc
->flags
&= ~BGP_NEXTHOP_EVPN_INCOMPLETE
;
540 bnc
->flags
&= ~BGP_NEXTHOP_VALID
;
541 bnc
->flags
&= ~BGP_NEXTHOP_LABELED_VALID
;
542 bnc
->nexthop_num
= nhr
->nexthop_num
;
544 /* notify bgp fsm if nbr ip goes from valid->invalid */
545 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
547 bnc_nexthop_free(bnc
);
554 static void bgp_nht_ifp_table_handle(struct bgp
*bgp
,
555 struct bgp_nexthop_cache_head
*table
,
556 struct interface
*ifp
, bool up
)
558 struct bgp_nexthop_cache
*bnc
;
560 frr_each (bgp_nexthop_cache
, table
, bnc
) {
561 if (bnc
->ifindex
!= ifp
->ifindex
)
564 bnc
->last_update
= bgp_clock();
565 bnc
->change_flags
= 0;
568 * For interface based routes ( ala the v6 LL routes
569 * that this was written for ) the metric received
570 * for the connected route is 0 not 1.
574 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
575 SET_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
);
576 bnc
->nexthop_num
= 1;
578 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
579 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
580 SET_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
);
581 bnc
->nexthop_num
= 0;
587 static void bgp_nht_ifp_handle(struct interface
*ifp
, bool up
)
591 bgp
= ifp
->vrf
->info
;
595 bgp_nht_ifp_table_handle(bgp
, &bgp
->nexthop_cache_table
[AFI_IP6
], ifp
,
597 bgp_nht_ifp_table_handle(bgp
, &bgp
->import_check_table
[AFI_IP6
], ifp
,
601 void bgp_nht_ifp_up(struct interface
*ifp
)
603 bgp_nht_ifp_handle(ifp
, true);
606 void bgp_nht_ifp_down(struct interface
*ifp
)
608 bgp_nht_ifp_handle(ifp
, false);
611 static int bgp_nht_ifp_initial(struct thread
*thread
)
613 ifindex_t ifindex
= THREAD_VAL(thread
);
614 struct bgp
*bgp
= THREAD_ARG(thread
);
615 struct interface
*ifp
= if_lookup_by_index(ifindex
, bgp
->vrf_id
);
620 if (BGP_DEBUG(nht
, NHT
))
622 "Handle NHT initial update for Intf %s(%d) status %s",
623 ifp
->name
, ifp
->ifindex
, if_is_up(ifp
) ? "up" : "down");
628 bgp_nht_ifp_down(ifp
);
634 * So the bnc code has the ability to handle interface up/down
635 * events to properly handle v6 LL peering.
636 * What is happening here:
637 * The event system for peering expects the nht code to
638 * report on the tracking events after we move to active
639 * So let's give the system a chance to report on that event
640 * in a manner that is expected.
642 void bgp_nht_interface_events(struct peer
*peer
)
644 struct bgp
*bgp
= peer
->bgp
;
645 struct bgp_nexthop_cache_head
*table
;
646 struct bgp_nexthop_cache
*bnc
;
649 if (!IN6_IS_ADDR_LINKLOCAL(&peer
->su
.sin6
.sin6_addr
))
652 if (!sockunion2hostprefix(&peer
->su
, &p
))
655 table
= &bgp
->nexthop_cache_table
[AFI_IP6
];
656 bnc
= bnc_find(table
, &p
, 0);
661 thread_add_event(bm
->master
, bgp_nht_ifp_initial
, bnc
->bgp
,
665 void bgp_parse_nexthop_update(int command
, vrf_id_t vrf_id
)
667 struct bgp_nexthop_cache_head
*tree
= NULL
;
668 struct bgp_nexthop_cache
*bnc_nhc
, *bnc_import
;
670 struct zapi_route nhr
;
673 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
677 "parse nexthop update: instance not found for vrf_id %u",
682 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &nhr
)) {
683 zlog_err("%s[%s]: Failure to decode nexthop update", __func__
,
688 afi
= family2afi(nhr
.prefix
.family
);
689 tree
= &bgp
->nexthop_cache_table
[afi
];
691 bnc_nhc
= bnc_find(tree
, &nhr
.prefix
, nhr
.srte_color
);
693 if (BGP_DEBUG(nht
, NHT
))
695 "parse nexthop update(%pFX(%u)(%s)): bnc info not found for nexthop cache",
696 &nhr
.prefix
, nhr
.srte_color
, bgp
->name_pretty
);
698 bgp_process_nexthop_update(bnc_nhc
, &nhr
);
700 tree
= &bgp
->import_check_table
[afi
];
702 bnc_import
= bnc_find(tree
, &nhr
.prefix
, nhr
.srte_color
);
704 if (BGP_DEBUG(nht
, NHT
))
706 "parse nexthop update(%pFX(%u)(%s)): bnc info not found for import check",
707 &nhr
.prefix
, nhr
.srte_color
, bgp
->name_pretty
);
710 if (nhr
.type
== ZEBRA_ROUTE_BGP
711 || !prefix_same(&bnc_import
->prefix
, &nhr
.prefix
)) {
712 if (BGP_DEBUG(nht
, NHT
))
714 "%s: Import Check does not resolve to the same prefix for %pFX received %pFX",
715 __func__
, &bnc_import
->prefix
, &nhr
.prefix
);
718 bgp_process_nexthop_update(bnc_import
, &nhr
);
722 * HACK: if any BGP route is dependant on an SR-policy that doesn't
723 * exist, zebra will never send NH updates relative to that policy. In
724 * that case, whenever we receive an update about a colorless NH, update
725 * the corresponding colorful NHs that share the same endpoint but that
726 * are inactive. This ugly hack should work around the problem at the
727 * cost of a performance pernalty. Long term, what should be done is to
728 * make zebra's RNH subsystem aware of SR-TE colors (like bgpd is),
729 * which should provide a better infrastructure to solve this issue in
730 * a more efficient and elegant way.
732 if (nhr
.srte_color
== 0 && bnc_nhc
) {
733 struct bgp_nexthop_cache
*bnc_iter
;
735 frr_each (bgp_nexthop_cache
, &bgp
->nexthop_cache_table
[afi
],
737 if (!prefix_same(&bnc_import
->prefix
, &bnc_iter
->prefix
)
738 || bnc_iter
->srte_color
== 0
739 || CHECK_FLAG(bnc_iter
->flags
, BGP_NEXTHOP_VALID
))
742 bgp_process_nexthop_update(bnc_iter
, &nhr
);
748 * Cleanup nexthop registration and status information for BGP nexthops
749 * pertaining to this VRF. This is invoked upon VRF deletion.
751 void bgp_cleanup_nexthops(struct bgp
*bgp
)
753 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
754 struct bgp_nexthop_cache
*bnc
;
756 frr_each (bgp_nexthop_cache
, &bgp
->nexthop_cache_table
[afi
],
758 /* Clear relevant flags. */
759 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
760 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
761 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
762 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_EVPN_INCOMPLETE
);
768 * make_prefix - make a prefix structure from the path (essentially
771 static int make_prefix(int afi
, struct bgp_path_info
*pi
, struct prefix
*p
)
774 int is_bgp_static
= ((pi
->type
== ZEBRA_ROUTE_BGP
)
775 && (pi
->sub_type
== BGP_ROUTE_STATIC
))
778 struct bgp_dest
*net
= pi
->net
;
779 const struct prefix
*p_orig
= bgp_dest_get_prefix(net
);
782 if (p_orig
->family
== AF_FLOWSPEC
) {
785 return bgp_flowspec_get_first_nh(pi
->peer
->bgp
,
788 memset(p
, 0, sizeof(struct prefix
));
793 p
->u
.prefix4
= p_orig
->u
.prefix4
;
794 p
->prefixlen
= p_orig
->prefixlen
;
796 if (IS_MAPPED_IPV6(&pi
->attr
->mp_nexthop_global
)) {
797 ipv4_mapped_ipv6_to_ipv4(
798 &pi
->attr
->mp_nexthop_global
, &ipv4
);
800 p
->prefixlen
= IPV4_MAX_BITLEN
;
802 p
->u
.prefix4
= pi
->attr
->nexthop
;
803 p
->prefixlen
= IPV4_MAX_BITLEN
;
808 p
->family
= AF_INET6
;
811 p
->u
.prefix6
= p_orig
->u
.prefix6
;
812 p
->prefixlen
= p_orig
->prefixlen
;
814 /* If we receive MP_REACH nexthop with ::(LL)
815 * or LL(LL), use LL address as nexthop cache.
817 if (pi
->attr
->mp_nexthop_len
818 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
819 && (IN6_IS_ADDR_UNSPECIFIED(
820 &pi
->attr
->mp_nexthop_global
)
821 || IN6_IS_ADDR_LINKLOCAL(
822 &pi
->attr
->mp_nexthop_global
)))
823 p
->u
.prefix6
= pi
->attr
->mp_nexthop_local
;
824 /* If we receive MR_REACH with (GA)::(LL)
825 * then check for route-map to choose GA or LL
827 else if (pi
->attr
->mp_nexthop_len
828 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
829 if (pi
->attr
->mp_nexthop_prefer_global
)
831 pi
->attr
->mp_nexthop_global
;
834 pi
->attr
->mp_nexthop_local
;
836 p
->u
.prefix6
= pi
->attr
->mp_nexthop_global
;
837 p
->prefixlen
= IPV6_MAX_BITLEN
;
841 if (BGP_DEBUG(nht
, NHT
)) {
843 "%s: Attempting to make prefix with unknown AFI %d (not %d or %d)",
844 __func__
, afi
, AFI_IP
, AFI_IP6
);
852 * sendmsg_zebra_rnh -- Format and send a nexthop register/Unregister
855 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
856 * int command -- command to send to zebra
860 static void sendmsg_zebra_rnh(struct bgp_nexthop_cache
*bnc
, int command
)
862 bool exact_match
= false;
863 bool resolve_via_default
= false;
869 /* Don't try to register if Zebra doesn't know of this instance. */
870 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bnc
->bgp
)) {
871 if (BGP_DEBUG(zebra
, ZEBRA
))
873 "%s: No zebra instance to talk to, not installing NHT entry",
878 if (!bgp_zebra_num_connects()) {
879 if (BGP_DEBUG(zebra
, ZEBRA
))
881 "%s: We have not connected yet, cannot send nexthops",
884 if (command
== ZEBRA_NEXTHOP_REGISTER
) {
885 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
))
887 if (CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
))
888 resolve_via_default
= true;
891 if (BGP_DEBUG(zebra
, ZEBRA
))
892 zlog_debug("%s: sending cmd %s for %pFX (vrf %s)", __func__
,
893 zserv_command_string(command
), &bnc
->prefix
,
894 bnc
->bgp
->name_pretty
);
896 ret
= zclient_send_rnh(zclient
, command
, &bnc
->prefix
, exact_match
,
897 resolve_via_default
, bnc
->bgp
->vrf_id
);
898 if (ret
== ZCLIENT_SEND_FAILURE
) {
899 flog_warn(EC_BGP_ZEBRA_SEND
,
900 "sendmsg_nexthop: zclient_send_message() failed");
904 if (command
== ZEBRA_NEXTHOP_REGISTER
)
905 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
906 else if (command
== ZEBRA_NEXTHOP_UNREGISTER
)
907 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
912 * register_zebra_rnh - register a NH/route with Zebra for notification
913 * when the route or the route to the nexthop changes.
915 * struct bgp_nexthop_cache *bnc
919 static void register_zebra_rnh(struct bgp_nexthop_cache
*bnc
,
920 int is_bgp_import_route
)
922 /* Check if we have already registered */
923 if (bnc
->flags
& BGP_NEXTHOP_REGISTERED
)
927 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
931 sendmsg_zebra_rnh(bnc
, ZEBRA_NEXTHOP_REGISTER
);
935 * unregister_zebra_rnh -- Unregister the route/nexthop from Zebra.
937 * struct bgp_nexthop_cache *bnc
941 static void unregister_zebra_rnh(struct bgp_nexthop_cache
*bnc
,
942 int is_bgp_import_route
)
944 /* Check if we have already registered */
945 if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
))
949 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
953 sendmsg_zebra_rnh(bnc
, ZEBRA_NEXTHOP_UNREGISTER
);
957 * evaluate_paths - Evaluate the paths/nets associated with a nexthop.
959 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
963 void evaluate_paths(struct bgp_nexthop_cache
*bnc
)
965 struct bgp_dest
*dest
;
966 struct bgp_path_info
*path
;
968 struct peer
*peer
= (struct peer
*)bnc
->nht_info
;
969 struct bgp_table
*table
;
971 struct bgp
*bgp_path
;
972 const struct prefix
*p
;
974 if (BGP_DEBUG(nht
, NHT
)) {
975 char buf
[PREFIX2STR_BUFFER
];
976 char bnc_buf
[BNC_FLAG_DUMP_SIZE
];
977 char chg_buf
[BNC_FLAG_DUMP_SIZE
];
979 bnc_str(bnc
, buf
, PREFIX2STR_BUFFER
);
981 "NH update for %s(%u)(%s) - flags %s chgflags %s- evaluate paths",
982 buf
, bnc
->srte_color
, bnc
->bgp
->name_pretty
,
983 bgp_nexthop_dump_bnc_flags(bnc
, bnc_buf
,
985 bgp_nexthop_dump_bnc_change_flags(bnc
, chg_buf
,
989 LIST_FOREACH (path
, &(bnc
->paths
), nh_thread
) {
990 if (!(path
->type
== ZEBRA_ROUTE_BGP
991 && ((path
->sub_type
== BGP_ROUTE_NORMAL
)
992 || (path
->sub_type
== BGP_ROUTE_STATIC
)
993 || (path
->sub_type
== BGP_ROUTE_IMPORTED
))))
997 assert(dest
&& bgp_dest_table(dest
));
998 p
= bgp_dest_get_prefix(dest
);
999 afi
= family2afi(p
->family
);
1000 table
= bgp_dest_table(dest
);
1004 * handle routes from other VRFs (they can have a
1005 * nexthop in THIS VRF). bgp_path is the bgp instance
1006 * that owns the route referencing this nexthop.
1008 bgp_path
= table
->bgp
;
1011 * Path becomes valid/invalid depending on whether the nexthop
1012 * reachable/unreachable.
1014 * In case of unicast routes that were imported from vpn
1015 * and that have labels, they are valid only if there are
1016 * nexthops with labels
1018 * If the nexthop is EVPN gateway-IP,
1019 * do not check for a valid label.
1022 bool bnc_is_valid_nexthop
= false;
1023 bool path_valid
= false;
1025 if (safi
== SAFI_UNICAST
&& path
->sub_type
== BGP_ROUTE_IMPORTED
1026 && path
->extra
&& path
->extra
->num_labels
1027 && (path
->attr
->evpn_overlay
.type
1028 != OVERLAY_INDEX_GATEWAY_IP
)) {
1029 bnc_is_valid_nexthop
=
1030 bgp_isvalid_labeled_nexthop(bnc
) ? true : false;
1032 if (bgp_update_martian_nexthop(
1033 bnc
->bgp
, afi
, safi
, path
->type
,
1034 path
->sub_type
, path
->attr
, dest
)) {
1035 if (BGP_DEBUG(nht
, NHT
))
1037 "%s: prefix %pBD (vrf %s), ignoring path due to martian or self-next-hop",
1038 __func__
, dest
, bgp_path
->name
);
1040 bnc_is_valid_nexthop
=
1041 bgp_isvalid_nexthop(bnc
) ? true : false;
1044 if (BGP_DEBUG(nht
, NHT
)) {
1045 char buf1
[RD_ADDRSTRLEN
];
1048 prefix_rd2str((struct prefix_rd
*)bgp_dest_get_prefix(dest
->pdest
),
1049 buf1
, sizeof(buf1
));
1051 "... eval path %d/%d %pBD RD %s %s flags 0x%x",
1052 afi
, safi
, dest
, buf1
,
1053 bgp_path
->name_pretty
, path
->flags
);
1056 "... eval path %d/%d %pBD %s flags 0x%x",
1057 afi
, safi
, dest
, bgp_path
->name_pretty
,
1061 /* Skip paths marked for removal or as history. */
1062 if (CHECK_FLAG(path
->flags
, BGP_PATH_REMOVED
)
1063 || CHECK_FLAG(path
->flags
, BGP_PATH_HISTORY
))
1066 /* Copy the metric to the path. Will be used for bestpath
1068 if (bgp_isvalid_nexthop(bnc
) && bnc
->metric
)
1069 (bgp_path_info_extra_get(path
))->igpmetric
=
1071 else if (path
->extra
)
1072 path
->extra
->igpmetric
= 0;
1074 if (CHECK_FLAG(bnc
->change_flags
, BGP_NEXTHOP_METRIC_CHANGED
)
1075 || CHECK_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
)
1076 || path
->attr
->srte_color
!= 0)
1077 SET_FLAG(path
->flags
, BGP_PATH_IGP_CHANGED
);
1079 path_valid
= CHECK_FLAG(path
->flags
, BGP_PATH_VALID
);
1080 if (path_valid
!= bnc_is_valid_nexthop
) {
1082 /* No longer valid, clear flag; also for EVPN
1083 * routes, unimport from VRFs if needed.
1085 bgp_aggregate_decrement(bgp_path
, p
, path
, afi
,
1087 bgp_path_info_unset_flag(dest
, path
,
1089 if (safi
== SAFI_EVPN
&&
1090 bgp_evpn_is_prefix_nht_supported(bgp_dest_get_prefix(dest
)))
1091 bgp_evpn_unimport_route(bgp_path
,
1092 afi
, safi
, bgp_dest_get_prefix(dest
), path
);
1094 /* Path becomes valid, set flag; also for EVPN
1095 * routes, import from VRFs if needed.
1097 bgp_path_info_set_flag(dest
, path
,
1099 bgp_aggregate_increment(bgp_path
, p
, path
, afi
,
1101 if (safi
== SAFI_EVPN
&&
1102 bgp_evpn_is_prefix_nht_supported(bgp_dest_get_prefix(dest
)))
1103 bgp_evpn_import_route(bgp_path
,
1104 afi
, safi
, bgp_dest_get_prefix(dest
), path
);
1108 bgp_process(bgp_path
, dest
, afi
, safi
);
1112 int valid_nexthops
= bgp_isvalid_nexthop(bnc
);
1114 if (valid_nexthops
) {
1116 * Peering cannot occur across a blackhole nexthop
1118 if (bnc
->nexthop_num
== 1 && bnc
->nexthop
1119 && bnc
->nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1120 peer
->last_reset
= PEER_DOWN_WAITING_NHT
;
1123 peer
->last_reset
= PEER_DOWN_WAITING_OPEN
;
1125 peer
->last_reset
= PEER_DOWN_WAITING_NHT
;
1127 if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
)) {
1128 if (BGP_DEBUG(nht
, NHT
))
1130 "%s: Updating peer (%s(%s)) status with NHT nexthops %d",
1131 __func__
, peer
->host
,
1132 peer
->bgp
->name_pretty
,
1134 bgp_fsm_nht_update(peer
, !!valid_nexthops
);
1135 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
1139 RESET_FLAG(bnc
->change_flags
);
1143 * path_nh_map - make or break path-to-nexthop association.
1145 * path - pointer to the path structure
1146 * bnc - pointer to the nexthop structure
1147 * make - if set, make the association. if unset, just break the existing
1150 void path_nh_map(struct bgp_path_info
*path
, struct bgp_nexthop_cache
*bnc
,
1153 if (path
->nexthop
) {
1154 LIST_REMOVE(path
, nh_thread
);
1155 path
->nexthop
->path_count
--;
1156 path
->nexthop
= NULL
;
1159 LIST_INSERT_HEAD(&(bnc
->paths
), path
, nh_thread
);
1160 path
->nexthop
= bnc
;
1161 path
->nexthop
->path_count
++;
1166 * This function is called to register nexthops to zebra
1167 * as that we may have tried to install the nexthops
1168 * before we actually have a zebra connection
1170 void bgp_nht_register_nexthops(struct bgp
*bgp
)
1172 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
1173 struct bgp_nexthop_cache
*bnc
;
1175 frr_each (bgp_nexthop_cache
, &bgp
->nexthop_cache_table
[afi
],
1177 register_zebra_rnh(bnc
, 0);
1182 void bgp_nht_reg_enhe_cap_intfs(struct peer
*peer
)
1185 struct bgp_nexthop_cache
*bnc
;
1186 struct nexthop
*nhop
;
1187 struct interface
*ifp
;
1194 if (!sockunion2hostprefix(&peer
->su
, &p
)) {
1195 zlog_warn("%s: Unable to convert sockunion to prefix for %s",
1196 __func__
, peer
->host
);
1200 if (p
.family
!= AF_INET6
)
1203 bnc
= bnc_find(&bgp
->nexthop_cache_table
[AFI_IP6
], &p
, 0);
1207 if (peer
!= bnc
->nht_info
)
1210 for (nhop
= bnc
->nexthop
; nhop
; nhop
= nhop
->next
) {
1211 ifp
= if_lookup_by_index(nhop
->ifindex
, nhop
->vrf_id
);
1216 zclient_send_interface_radv_req(zclient
,
1219 BGP_UNNUM_DEFAULT_RA_INTERVAL
);
1223 void bgp_nht_dereg_enhe_cap_intfs(struct peer
*peer
)
1226 struct bgp_nexthop_cache
*bnc
;
1227 struct nexthop
*nhop
;
1228 struct interface
*ifp
;
1236 if (!sockunion2hostprefix(&peer
->su
, &p
)) {
1237 zlog_warn("%s: Unable to convert sockunion to prefix for %s",
1238 __func__
, peer
->host
);
1242 if (p
.family
!= AF_INET6
)
1245 bnc
= bnc_find(&bgp
->nexthop_cache_table
[AFI_IP6
], &p
, 0);
1249 if (peer
!= bnc
->nht_info
)
1252 for (nhop
= bnc
->nexthop
; nhop
; nhop
= nhop
->next
) {
1253 ifp
= if_lookup_by_index(nhop
->ifindex
, nhop
->vrf_id
);
1258 zclient_send_interface_radv_req(zclient
, nhop
->vrf_id
, ifp
, 0,
1263 /****************************************************************************
1264 * L3 NHGs are used for fast failover of nexthops in the dplane. These are
1265 * the APIs for allocating L3 NHG ids. Management of the L3 NHG itself is
1266 * left to the application using it.
1267 * PS: Currently EVPN host routes is the only app using L3 NHG for fast
1268 * failover of remote ES links.
1269 ***************************************************************************/
1270 static bitfield_t bgp_nh_id_bitmap
;
1271 static uint32_t bgp_l3nhg_start
;
1273 /* XXX - currently we do nothing on the callbacks */
1274 static void bgp_l3nhg_add_cb(const char *name
)
1277 static void bgp_l3nhg_add_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
1278 const struct nexthop
*nhop
)
1281 static void bgp_l3nhg_del_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
1282 const struct nexthop
*nhop
)
1285 static void bgp_l3nhg_del_cb(const char *name
)
1289 static void bgp_l3nhg_zebra_init(void)
1291 static bool bgp_l3nhg_zebra_inited
;
1292 if (bgp_l3nhg_zebra_inited
)
1295 bgp_l3nhg_zebra_inited
= true;
1296 bgp_l3nhg_start
= zclient_get_nhg_start(ZEBRA_ROUTE_BGP
);
1297 nexthop_group_init(bgp_l3nhg_add_cb
, bgp_l3nhg_add_nexthop_cb
,
1298 bgp_l3nhg_del_nexthop_cb
, bgp_l3nhg_del_cb
);
1302 void bgp_l3nhg_init(void)
1306 id_max
= MIN(ZEBRA_NHG_PROTO_SPACING
- 1, 16 * 1024);
1307 bf_init(bgp_nh_id_bitmap
, id_max
);
1308 bf_assign_zero_index(bgp_nh_id_bitmap
);
1310 if (BGP_DEBUG(nht
, NHT
) || BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
1311 zlog_debug("bgp l3_nhg range %u - %u", bgp_l3nhg_start
+ 1,
1312 bgp_l3nhg_start
+ id_max
);
1315 void bgp_l3nhg_finish(void)
1317 bf_free(bgp_nh_id_bitmap
);
1320 uint32_t bgp_l3nhg_id_alloc(void)
1322 uint32_t nhg_id
= 0;
1324 bgp_l3nhg_zebra_init();
1325 bf_assign_index(bgp_nh_id_bitmap
, nhg_id
);
1327 nhg_id
+= bgp_l3nhg_start
;
1332 void bgp_l3nhg_id_free(uint32_t nhg_id
)
1334 if (!nhg_id
|| (nhg_id
<= bgp_l3nhg_start
))
1337 nhg_id
-= bgp_l3nhg_start
;
1339 bf_release_index(bgp_nh_id_bitmap
, nhg_id
);