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_labeled_nexthop(struct bgp_nexthop_cache
*bnc
)
67 * In the case of MPLS-VPN, the label is learned from LDP or other
68 * protocols, and nexthop tracking is enabled for the label.
69 * The value is recorded as BGP_NEXTHOP_LABELED_VALID.
70 * In the case of SRv6-VPN, we need to track the reachability to the
71 * SID (in other words, IPv6 address). As in MPLS, we need to record
72 * the value as BGP_NEXTHOP_SID_VALID. However, this function is
73 * currently not implemented, and this function assumes that all
74 * Transit routes for SRv6-VPN are valid.
76 return (bgp_zebra_num_connects() == 0
77 || (bnc
&& bnc
->nexthop_num
> 0
78 && (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_LABELED_VALID
)
79 || bnc
->bgp
->srv6_enabled
)));
82 static void bgp_unlink_nexthop_check(struct bgp_nexthop_cache
*bnc
)
84 if (LIST_EMPTY(&(bnc
->paths
)) && !bnc
->nht_info
) {
85 if (BGP_DEBUG(nht
, NHT
)) {
86 char buf
[PREFIX2STR_BUFFER
];
87 zlog_debug("%s: freeing bnc %s(%u)(%s)", __func__
,
88 bnc_str(bnc
, buf
, PREFIX2STR_BUFFER
),
89 bnc
->srte_color
, bnc
->bgp
->name_pretty
);
91 /* only unregister if this is the last nh for this prefix*/
92 if (!bnc_existing_for_prefix(bnc
))
93 unregister_zebra_rnh(bnc
);
98 void bgp_unlink_nexthop(struct bgp_path_info
*path
)
100 struct bgp_nexthop_cache
*bnc
= path
->nexthop
;
105 path_nh_map(path
, NULL
, false);
107 bgp_unlink_nexthop_check(bnc
);
110 void bgp_replace_nexthop_by_peer(struct peer
*from
, struct peer
*to
)
114 struct bgp_nexthop_cache
*bncp
, *bnct
;
117 if (!sockunion2hostprefix(&from
->su
, &pp
))
120 afi
= family2afi(pp
.family
);
121 bncp
= bnc_find(&from
->bgp
->nexthop_cache_table
[afi
], &pp
, 0);
123 if (!sockunion2hostprefix(&to
->su
, &pt
))
126 bnct
= bnc_find(&to
->bgp
->nexthop_cache_table
[afi
], &pt
, 0);
135 void bgp_unlink_nexthop_by_peer(struct peer
*peer
)
138 struct bgp_nexthop_cache
*bnc
;
139 afi_t afi
= family2afi(peer
->su
.sa
.sa_family
);
141 if (!sockunion2hostprefix(&peer
->su
, &p
))
144 bnc
= bnc_find(&peer
->bgp
->nexthop_cache_table
[afi
], &p
, 0);
148 /* cleanup the peer reference */
149 bnc
->nht_info
= NULL
;
151 bgp_unlink_nexthop_check(bnc
);
155 * A route and its nexthop might belong to different VRFs. Therefore,
156 * we need both the bgp_route and bgp_nexthop pointers.
158 int bgp_find_or_add_nexthop(struct bgp
*bgp_route
, struct bgp
*bgp_nexthop
,
159 afi_t afi
, safi_t safi
, struct bgp_path_info
*pi
,
160 struct peer
*peer
, int connected
,
161 const struct prefix
*orig_prefix
)
163 struct bgp_nexthop_cache_head
*tree
= NULL
;
164 struct bgp_nexthop_cache
*bnc
;
166 uint32_t srte_color
= 0;
167 int is_bgp_static_route
= 0;
168 ifindex_t ifindex
= 0;
171 is_bgp_static_route
= ((pi
->type
== ZEBRA_ROUTE_BGP
)
172 && (pi
->sub_type
== BGP_ROUTE_STATIC
))
176 /* Since Extended Next-hop Encoding (RFC5549) support, we want
178 address-family from the next-hop. */
179 if (!is_bgp_static_route
)
180 afi
= BGP_ATTR_NEXTHOP_AFI_IP6(pi
->attr
) ? AFI_IP6
183 /* Validation for the ipv4 mapped ipv6 nexthop. */
184 if (IS_MAPPED_IPV6(&pi
->attr
->mp_nexthop_global
)) {
188 /* This will return true if the global IPv6 NH is a link local
190 if (make_prefix(afi
, pi
, &p
) < 0)
193 if (!is_bgp_static_route
&& orig_prefix
194 && prefix_same(&p
, orig_prefix
)) {
195 if (BGP_DEBUG(nht
, NHT
)) {
197 "%s(%pFX): prefix loops through itself",
203 srte_color
= pi
->attr
->srte_color
;
206 * Gather the ifindex for if up/down events to be
207 * tagged into this fun
210 && IN6_IS_ADDR_LINKLOCAL(&peer
->su
.sin6
.sin6_addr
))
211 ifindex
= peer
->su
.sin6
.sin6_scope_id
;
213 if (!sockunion2hostprefix(&peer
->su
, &p
)) {
214 if (BGP_DEBUG(nht
, NHT
)) {
216 "%s: Attempting to register with unknown AFI %d (not %d or %d)",
217 __func__
, afi
, AFI_IP
, AFI_IP6
);
224 if (is_bgp_static_route
)
225 tree
= &bgp_nexthop
->import_check_table
[afi
];
227 tree
= &bgp_nexthop
->nexthop_cache_table
[afi
];
229 bnc
= bnc_find(tree
, &p
, srte_color
);
231 bnc
= bnc_new(tree
, &p
, srte_color
);
232 bnc
->bgp
= bgp_nexthop
;
233 bnc
->ifindex
= ifindex
;
234 if (BGP_DEBUG(nht
, NHT
)) {
235 char buf
[PREFIX2STR_BUFFER
];
237 zlog_debug("Allocated bnc %s(%u)(%s) peer %p",
238 bnc_str(bnc
, buf
, PREFIX2STR_BUFFER
),
239 bnc
->srte_color
, bnc
->bgp
->name_pretty
,
243 if (BGP_DEBUG(nht
, NHT
)) {
244 char buf
[PREFIX2STR_BUFFER
];
247 "Found existing bnc %s(%s) flags 0x%x ifindex %d #paths %d peer %p",
248 bnc_str(bnc
, buf
, PREFIX2STR_BUFFER
),
249 bnc
->bgp
->name_pretty
, bnc
->flags
, bnc
->ifindex
,
250 bnc
->path_count
, bnc
->nht_info
);
254 if (pi
&& is_route_parent_evpn(pi
))
255 bnc
->is_evpn_gwip_nexthop
= true;
257 if (is_bgp_static_route
) {
258 SET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE
);
260 /* If we're toggling the type, re-register */
261 if ((CHECK_FLAG(bgp_route
->flags
, BGP_FLAG_IMPORT_CHECK
))
262 && !CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
)) {
263 SET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
);
264 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
265 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
266 } else if ((!CHECK_FLAG(bgp_route
->flags
,
267 BGP_FLAG_IMPORT_CHECK
))
268 && CHECK_FLAG(bnc
->flags
,
269 BGP_STATIC_ROUTE_EXACT_MATCH
)) {
270 UNSET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
);
271 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
272 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
275 /* When nexthop is already known, but now requires 'connected'
277 * re-register it. The reverse scenario where the nexthop currently
279 * 'connected' resolution does not need a re-register (i.e., we treat
280 * 'connected-required' as an override) except in the scenario where
282 * is actually a case of tracking a peer for connectivity (e.g., after
283 * disable connected-check).
284 * NOTE: We don't track the number of paths separately for 'connected-
285 * required' vs 'connected-not-required' as this change is not a common
288 else if (connected
&& !CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
)) {
289 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
);
290 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
291 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
292 } else if (peer
&& !connected
293 && CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
)) {
294 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
);
295 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
296 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
298 if (peer
&& (bnc
->ifindex
!= ifindex
)) {
299 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
300 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
301 bnc
->ifindex
= ifindex
;
303 if (bgp_route
->inst_type
== BGP_INSTANCE_TYPE_VIEW
) {
304 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
305 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
306 } else if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
)
307 && !is_default_host_route(&bnc
->prefix
))
308 register_zebra_rnh(bnc
);
310 if (pi
&& pi
->nexthop
!= bnc
) {
311 /* Unlink from existing nexthop cache, if any. This will also
313 * the nexthop cache entry, if appropriate.
315 bgp_unlink_nexthop(pi
);
317 /* updates NHT pi list reference */
318 path_nh_map(pi
, bnc
, true);
320 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
) && bnc
->metric
)
321 (bgp_path_info_extra_get(pi
))->igpmetric
= bnc
->metric
;
323 pi
->extra
->igpmetric
= 0;
326 * Let's not accidently save the peer data for a peer
327 * we are going to throw away in a second or so.
328 * When we come back around we'll fix up this
329 * data properly in replace_nexthop_by_peer
331 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
332 bnc
->nht_info
= (void *)peer
; /* NHT peer reference */
336 * We are cheating here. Views have no associated underlying
337 * ability to detect nexthops. So when we have a view
338 * just tell everyone the nexthop is valid
340 if (bgp_route
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
342 else if (safi
== SAFI_UNICAST
&& pi
343 && pi
->sub_type
== BGP_ROUTE_IMPORTED
&& pi
->extra
344 && pi
->extra
->num_labels
&& !bnc
->is_evpn_gwip_nexthop
) {
345 return bgp_isvalid_labeled_nexthop(bnc
);
347 return (bgp_isvalid_nexthop(bnc
));
350 void bgp_delete_connected_nexthop(afi_t afi
, struct peer
*peer
)
352 struct bgp_nexthop_cache
*bnc
;
358 if (!sockunion2hostprefix(&peer
->su
, &p
))
361 bnc
= bnc_find(&peer
->bgp
->nexthop_cache_table
[family2afi(p
.family
)],
364 if (BGP_DEBUG(nht
, NHT
))
366 "Cannot find connected NHT node for peer %s(%s)",
367 peer
->host
, peer
->bgp
->name_pretty
);
371 if (bnc
->nht_info
!= peer
) {
372 if (BGP_DEBUG(nht
, NHT
))
374 "Connected NHT %p node for peer %s(%s) points to %p",
375 bnc
, peer
->host
, bnc
->bgp
->name_pretty
,
380 bnc
->nht_info
= NULL
;
382 if (LIST_EMPTY(&(bnc
->paths
))) {
383 if (BGP_DEBUG(nht
, NHT
))
385 "Freeing connected NHT node %p for peer %s(%s)",
386 bnc
, peer
->host
, bnc
->bgp
->name_pretty
);
387 unregister_zebra_rnh(bnc
);
392 static void bgp_process_nexthop_update(struct bgp_nexthop_cache
*bnc
,
393 struct zapi_route
*nhr
)
395 struct nexthop
*nexthop
;
396 struct nexthop
*oldnh
;
397 struct nexthop
*nhlist_head
= NULL
;
398 struct nexthop
*nhlist_tail
= NULL
;
400 bool evpn_resolved
= false;
402 bnc
->last_update
= bgp_clock();
403 bnc
->change_flags
= 0;
405 /* debug print the input */
406 if (BGP_DEBUG(nht
, NHT
)) {
407 char bnc_buf
[BNC_FLAG_DUMP_SIZE
];
410 "%s(%u): Rcvd NH update %pFX(%u) - metric %d/%d #nhops %d/%d flags %s",
411 bnc
->bgp
->name_pretty
, bnc
->bgp
->vrf_id
, &nhr
->prefix
,
412 bnc
->srte_color
, nhr
->metric
, bnc
->metric
,
413 nhr
->nexthop_num
, bnc
->nexthop_num
,
414 bgp_nexthop_dump_bnc_flags(bnc
, bnc_buf
,
418 if (nhr
->metric
!= bnc
->metric
)
419 bnc
->change_flags
|= BGP_NEXTHOP_METRIC_CHANGED
;
421 if (nhr
->nexthop_num
!= bnc
->nexthop_num
)
422 bnc
->change_flags
|= BGP_NEXTHOP_CHANGED
;
424 if (nhr
->nexthop_num
) {
425 struct peer
*peer
= bnc
->nht_info
;
427 /* notify bgp fsm if nbr ip goes from invalid->valid */
428 if (!bnc
->nexthop_num
)
429 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
431 if (!bnc
->is_evpn_gwip_nexthop
)
432 bnc
->flags
|= BGP_NEXTHOP_VALID
;
433 bnc
->metric
= nhr
->metric
;
434 bnc
->nexthop_num
= nhr
->nexthop_num
;
436 bnc
->flags
&= ~BGP_NEXTHOP_LABELED_VALID
; /* check below */
438 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
441 nexthop
= nexthop_from_zapi_nexthop(&nhr
->nexthops
[i
]);
444 * Turn on RA for the v6 nexthops
445 * we receive from bgp. This is to allow us
446 * to work with v4 routing over v6 nexthops
448 if (peer
&& !peer
->ifp
449 && CHECK_FLAG(peer
->flags
,
450 PEER_FLAG_CAPABILITY_ENHE
)
451 && nhr
->prefix
.family
== AF_INET6
452 && nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
453 struct interface
*ifp
;
455 ifp
= if_lookup_by_index(nexthop
->ifindex
,
458 zclient_send_interface_radv_req(
459 zclient
, nexthop
->vrf_id
, ifp
,
461 BGP_UNNUM_DEFAULT_RA_INTERVAL
);
463 /* There is at least one label-switched path */
464 if (nexthop
->nh_label
&&
465 nexthop
->nh_label
->num_labels
) {
467 bnc
->flags
|= BGP_NEXTHOP_LABELED_VALID
;
468 num_labels
= nexthop
->nh_label
->num_labels
;
471 if (BGP_DEBUG(nht
, NHT
)) {
472 char buf
[NEXTHOP_STRLEN
];
474 " nhop via %s (%d labels)",
475 nexthop2str(nexthop
, buf
, sizeof(buf
)),
480 nhlist_tail
->next
= nexthop
;
481 nhlist_tail
= nexthop
;
483 nhlist_tail
= nexthop
;
484 nhlist_head
= nexthop
;
487 /* No need to evaluate the nexthop if we have already
489 * that there has been a change.
491 if (bnc
->change_flags
& BGP_NEXTHOP_CHANGED
)
494 for (oldnh
= bnc
->nexthop
; oldnh
; oldnh
= oldnh
->next
)
495 if (nexthop_same(oldnh
, nexthop
))
499 bnc
->change_flags
|= BGP_NEXTHOP_CHANGED
;
501 bnc_nexthop_free(bnc
);
502 bnc
->nexthop
= nhlist_head
;
505 * Gateway IP nexthop is L3 reachable. Mark it as
506 * BGP_NEXTHOP_VALID only if it is recursively resolved with a
508 * Else, mark it as BGP_NEXTHOP_EVPN_INCOMPLETE.
509 * When its mapping with EVPN RT-2 is established, unset
510 * BGP_NEXTHOP_EVPN_INCOMPLETE and set BGP_NEXTHOP_VALID.
512 if (bnc
->is_evpn_gwip_nexthop
) {
513 evpn_resolved
= bgp_evpn_is_gateway_ip_resolved(bnc
);
515 if (BGP_DEBUG(nht
, NHT
)) {
516 char buf2
[PREFIX2STR_BUFFER
];
518 prefix2str(&bnc
->prefix
, buf2
, sizeof(buf2
));
520 "EVPN gateway IP %s recursive MAC/IP lookup %s",
522 (evpn_resolved
? "successful"
527 bnc
->flags
|= BGP_NEXTHOP_VALID
;
528 bnc
->flags
&= ~BGP_NEXTHOP_EVPN_INCOMPLETE
;
529 bnc
->change_flags
|= BGP_NEXTHOP_MACIP_CHANGED
;
531 bnc
->flags
|= BGP_NEXTHOP_EVPN_INCOMPLETE
;
532 bnc
->flags
&= ~BGP_NEXTHOP_VALID
;
536 bnc
->flags
&= ~BGP_NEXTHOP_EVPN_INCOMPLETE
;
537 bnc
->flags
&= ~BGP_NEXTHOP_VALID
;
538 bnc
->flags
&= ~BGP_NEXTHOP_LABELED_VALID
;
539 bnc
->nexthop_num
= nhr
->nexthop_num
;
541 /* notify bgp fsm if nbr ip goes from valid->invalid */
542 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
544 bnc_nexthop_free(bnc
);
551 static void bgp_nht_ifp_table_handle(struct bgp
*bgp
,
552 struct bgp_nexthop_cache_head
*table
,
553 struct interface
*ifp
, bool up
)
555 struct bgp_nexthop_cache
*bnc
;
557 frr_each (bgp_nexthop_cache
, table
, bnc
) {
558 if (bnc
->ifindex
!= ifp
->ifindex
)
561 bnc
->last_update
= bgp_clock();
562 bnc
->change_flags
= 0;
565 * For interface based routes ( ala the v6 LL routes
566 * that this was written for ) the metric received
567 * for the connected route is 0 not 1.
571 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
572 SET_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
);
573 bnc
->nexthop_num
= 1;
575 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
576 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
577 SET_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
);
578 bnc
->nexthop_num
= 0;
584 static void bgp_nht_ifp_handle(struct interface
*ifp
, bool up
)
588 bgp
= ifp
->vrf
->info
;
592 bgp_nht_ifp_table_handle(bgp
, &bgp
->nexthop_cache_table
[AFI_IP6
], ifp
,
594 bgp_nht_ifp_table_handle(bgp
, &bgp
->import_check_table
[AFI_IP6
], ifp
,
598 void bgp_nht_ifp_up(struct interface
*ifp
)
600 bgp_nht_ifp_handle(ifp
, true);
603 void bgp_nht_ifp_down(struct interface
*ifp
)
605 bgp_nht_ifp_handle(ifp
, false);
608 static void bgp_nht_ifp_initial(struct thread
*thread
)
610 ifindex_t ifindex
= THREAD_VAL(thread
);
611 struct bgp
*bgp
= THREAD_ARG(thread
);
612 struct interface
*ifp
= if_lookup_by_index(ifindex
, bgp
->vrf_id
);
617 if (BGP_DEBUG(nht
, NHT
))
619 "Handle NHT initial update for Intf %s(%d) status %s",
620 ifp
->name
, ifp
->ifindex
, if_is_up(ifp
) ? "up" : "down");
625 bgp_nht_ifp_down(ifp
);
629 * So the bnc code has the ability to handle interface up/down
630 * events to properly handle v6 LL peering.
631 * What is happening here:
632 * The event system for peering expects the nht code to
633 * report on the tracking events after we move to active
634 * So let's give the system a chance to report on that event
635 * in a manner that is expected.
637 void bgp_nht_interface_events(struct peer
*peer
)
639 struct bgp
*bgp
= peer
->bgp
;
640 struct bgp_nexthop_cache_head
*table
;
641 struct bgp_nexthop_cache
*bnc
;
644 if (!IN6_IS_ADDR_LINKLOCAL(&peer
->su
.sin6
.sin6_addr
))
647 if (!sockunion2hostprefix(&peer
->su
, &p
))
650 table
= &bgp
->nexthop_cache_table
[AFI_IP6
];
651 bnc
= bnc_find(table
, &p
, 0);
656 thread_add_event(bm
->master
, bgp_nht_ifp_initial
, bnc
->bgp
,
660 void bgp_parse_nexthop_update(int command
, vrf_id_t vrf_id
)
662 struct bgp_nexthop_cache_head
*tree
= NULL
;
663 struct bgp_nexthop_cache
*bnc_nhc
, *bnc_import
;
666 struct zapi_route nhr
;
669 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
673 "parse nexthop update: instance not found for vrf_id %u",
678 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &match
, &nhr
)) {
679 zlog_err("%s[%s]: Failure to decode nexthop update", __func__
,
684 afi
= family2afi(match
.family
);
685 tree
= &bgp
->nexthop_cache_table
[afi
];
687 bnc_nhc
= bnc_find(tree
, &match
, nhr
.srte_color
);
689 if (BGP_DEBUG(nht
, NHT
))
691 "parse nexthop update(%pFX(%u)(%s)): bnc info not found for nexthop cache",
692 &nhr
.prefix
, nhr
.srte_color
, bgp
->name_pretty
);
694 bgp_process_nexthop_update(bnc_nhc
, &nhr
);
696 tree
= &bgp
->import_check_table
[afi
];
698 bnc_import
= bnc_find(tree
, &match
, nhr
.srte_color
);
700 if (BGP_DEBUG(nht
, NHT
))
702 "parse nexthop update(%pFX(%u)(%s)): bnc info not found for import check",
703 &nhr
.prefix
, nhr
.srte_color
, bgp
->name_pretty
);
706 if (nhr
.type
== ZEBRA_ROUTE_BGP
707 || !prefix_same(&bnc_import
->prefix
, &nhr
.prefix
)) {
708 if (BGP_DEBUG(nht
, NHT
))
710 "%s: Import Check does not resolve to the same prefix for %pFX received %pFX",
711 __func__
, &bnc_import
->prefix
, &nhr
.prefix
);
714 bgp_process_nexthop_update(bnc_import
, &nhr
);
718 * HACK: if any BGP route is dependant on an SR-policy that doesn't
719 * exist, zebra will never send NH updates relative to that policy. In
720 * that case, whenever we receive an update about a colorless NH, update
721 * the corresponding colorful NHs that share the same endpoint but that
722 * are inactive. This ugly hack should work around the problem at the
723 * cost of a performance pernalty. Long term, what should be done is to
724 * make zebra's RNH subsystem aware of SR-TE colors (like bgpd is),
725 * which should provide a better infrastructure to solve this issue in
726 * a more efficient and elegant way.
728 if (nhr
.srte_color
== 0 && bnc_nhc
) {
729 struct bgp_nexthop_cache
*bnc_iter
;
731 frr_each (bgp_nexthop_cache
, &bgp
->nexthop_cache_table
[afi
],
733 if (!prefix_same(&bnc_import
->prefix
, &bnc_iter
->prefix
)
734 || bnc_iter
->srte_color
== 0
735 || CHECK_FLAG(bnc_iter
->flags
, BGP_NEXTHOP_VALID
))
738 bgp_process_nexthop_update(bnc_iter
, &nhr
);
744 * Cleanup nexthop registration and status information for BGP nexthops
745 * pertaining to this VRF. This is invoked upon VRF deletion.
747 void bgp_cleanup_nexthops(struct bgp
*bgp
)
749 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
750 struct bgp_nexthop_cache
*bnc
;
752 frr_each (bgp_nexthop_cache
, &bgp
->nexthop_cache_table
[afi
],
754 /* Clear relevant flags. */
755 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
756 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
757 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
758 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_EVPN_INCOMPLETE
);
764 * make_prefix - make a prefix structure from the path (essentially
767 static int make_prefix(int afi
, struct bgp_path_info
*pi
, struct prefix
*p
)
770 int is_bgp_static
= ((pi
->type
== ZEBRA_ROUTE_BGP
)
771 && (pi
->sub_type
== BGP_ROUTE_STATIC
))
774 struct bgp_dest
*net
= pi
->net
;
775 const struct prefix
*p_orig
= bgp_dest_get_prefix(net
);
778 if (p_orig
->family
== AF_FLOWSPEC
) {
781 return bgp_flowspec_get_first_nh(pi
->peer
->bgp
,
784 memset(p
, 0, sizeof(struct prefix
));
789 p
->u
.prefix4
= p_orig
->u
.prefix4
;
790 p
->prefixlen
= p_orig
->prefixlen
;
792 if (IS_MAPPED_IPV6(&pi
->attr
->mp_nexthop_global
)) {
793 ipv4_mapped_ipv6_to_ipv4(
794 &pi
->attr
->mp_nexthop_global
, &ipv4
);
796 p
->prefixlen
= IPV4_MAX_BITLEN
;
798 p
->u
.prefix4
= pi
->attr
->nexthop
;
799 p
->prefixlen
= IPV4_MAX_BITLEN
;
804 p
->family
= AF_INET6
;
807 p
->u
.prefix6
= p_orig
->u
.prefix6
;
808 p
->prefixlen
= p_orig
->prefixlen
;
810 /* If we receive MP_REACH nexthop with ::(LL)
811 * or LL(LL), use LL address as nexthop cache.
813 if (pi
->attr
->mp_nexthop_len
814 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
815 && (IN6_IS_ADDR_UNSPECIFIED(
816 &pi
->attr
->mp_nexthop_global
)
817 || IN6_IS_ADDR_LINKLOCAL(
818 &pi
->attr
->mp_nexthop_global
)))
819 p
->u
.prefix6
= pi
->attr
->mp_nexthop_local
;
820 /* If we receive MR_REACH with (GA)::(LL)
821 * then check for route-map to choose GA or LL
823 else if (pi
->attr
->mp_nexthop_len
824 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
825 if (pi
->attr
->mp_nexthop_prefer_global
)
827 pi
->attr
->mp_nexthop_global
;
830 pi
->attr
->mp_nexthop_local
;
832 p
->u
.prefix6
= pi
->attr
->mp_nexthop_global
;
833 p
->prefixlen
= IPV6_MAX_BITLEN
;
837 if (BGP_DEBUG(nht
, NHT
)) {
839 "%s: Attempting to make prefix with unknown AFI %d (not %d or %d)",
840 __func__
, afi
, AFI_IP
, AFI_IP6
);
848 * sendmsg_zebra_rnh -- Format and send a nexthop register/Unregister
851 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
852 * int command -- command to send to zebra
856 static void sendmsg_zebra_rnh(struct bgp_nexthop_cache
*bnc
, int command
)
858 bool exact_match
= false;
859 bool resolve_via_default
= false;
865 /* Don't try to register if Zebra doesn't know of this instance. */
866 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bnc
->bgp
)) {
867 if (BGP_DEBUG(zebra
, ZEBRA
))
869 "%s: No zebra instance to talk to, not installing NHT entry",
874 if (!bgp_zebra_num_connects()) {
875 if (BGP_DEBUG(zebra
, ZEBRA
))
877 "%s: We have not connected yet, cannot send nexthops",
880 if (command
== ZEBRA_NEXTHOP_REGISTER
) {
881 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
))
883 if (CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
))
884 resolve_via_default
= true;
887 if (BGP_DEBUG(zebra
, ZEBRA
))
888 zlog_debug("%s: sending cmd %s for %pFX (vrf %s)", __func__
,
889 zserv_command_string(command
), &bnc
->prefix
,
890 bnc
->bgp
->name_pretty
);
892 ret
= zclient_send_rnh(zclient
, command
, &bnc
->prefix
, exact_match
,
893 resolve_via_default
, bnc
->bgp
->vrf_id
);
894 if (ret
== ZCLIENT_SEND_FAILURE
) {
895 flog_warn(EC_BGP_ZEBRA_SEND
,
896 "sendmsg_nexthop: zclient_send_message() failed");
900 if (command
== ZEBRA_NEXTHOP_REGISTER
)
901 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
902 else if (command
== ZEBRA_NEXTHOP_UNREGISTER
)
903 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
908 * register_zebra_rnh - register a NH/route with Zebra for notification
909 * when the route or the route to the nexthop changes.
911 * struct bgp_nexthop_cache *bnc
915 static void register_zebra_rnh(struct bgp_nexthop_cache
*bnc
)
917 /* Check if we have already registered */
918 if (bnc
->flags
& BGP_NEXTHOP_REGISTERED
)
922 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
926 sendmsg_zebra_rnh(bnc
, ZEBRA_NEXTHOP_REGISTER
);
930 * unregister_zebra_rnh -- Unregister the route/nexthop from Zebra.
932 * struct bgp_nexthop_cache *bnc
936 static void unregister_zebra_rnh(struct bgp_nexthop_cache
*bnc
)
938 /* Check if we have already registered */
939 if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
))
943 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
947 sendmsg_zebra_rnh(bnc
, ZEBRA_NEXTHOP_UNREGISTER
);
951 * evaluate_paths - Evaluate the paths/nets associated with a nexthop.
953 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
957 void evaluate_paths(struct bgp_nexthop_cache
*bnc
)
959 struct bgp_dest
*dest
;
960 struct bgp_path_info
*path
;
962 struct peer
*peer
= (struct peer
*)bnc
->nht_info
;
963 struct bgp_table
*table
;
965 struct bgp
*bgp_path
;
966 const struct prefix
*p
;
968 if (BGP_DEBUG(nht
, NHT
)) {
969 char buf
[PREFIX2STR_BUFFER
];
970 char bnc_buf
[BNC_FLAG_DUMP_SIZE
];
971 char chg_buf
[BNC_FLAG_DUMP_SIZE
];
973 bnc_str(bnc
, buf
, PREFIX2STR_BUFFER
);
975 "NH update for %s(%u)(%s) - flags %s chgflags %s- evaluate paths",
976 buf
, bnc
->srte_color
, bnc
->bgp
->name_pretty
,
977 bgp_nexthop_dump_bnc_flags(bnc
, bnc_buf
,
979 bgp_nexthop_dump_bnc_change_flags(bnc
, chg_buf
,
983 LIST_FOREACH (path
, &(bnc
->paths
), nh_thread
) {
984 if (!(path
->type
== ZEBRA_ROUTE_BGP
985 && ((path
->sub_type
== BGP_ROUTE_NORMAL
)
986 || (path
->sub_type
== BGP_ROUTE_STATIC
)
987 || (path
->sub_type
== BGP_ROUTE_IMPORTED
))))
991 assert(dest
&& bgp_dest_table(dest
));
992 p
= bgp_dest_get_prefix(dest
);
993 afi
= family2afi(p
->family
);
994 table
= bgp_dest_table(dest
);
998 * handle routes from other VRFs (they can have a
999 * nexthop in THIS VRF). bgp_path is the bgp instance
1000 * that owns the route referencing this nexthop.
1002 bgp_path
= table
->bgp
;
1005 * Path becomes valid/invalid depending on whether the nexthop
1006 * reachable/unreachable.
1008 * In case of unicast routes that were imported from vpn
1009 * and that have labels, they are valid only if there are
1010 * nexthops with labels
1012 * If the nexthop is EVPN gateway-IP,
1013 * do not check for a valid label.
1016 bool bnc_is_valid_nexthop
= false;
1017 bool path_valid
= false;
1019 if (safi
== SAFI_UNICAST
&& path
->sub_type
== BGP_ROUTE_IMPORTED
1020 && path
->extra
&& path
->extra
->num_labels
1021 && (path
->attr
->evpn_overlay
.type
1022 != OVERLAY_INDEX_GATEWAY_IP
)) {
1023 bnc_is_valid_nexthop
=
1024 bgp_isvalid_labeled_nexthop(bnc
) ? true : false;
1026 if (bgp_update_martian_nexthop(
1027 bnc
->bgp
, afi
, safi
, path
->type
,
1028 path
->sub_type
, path
->attr
, dest
)) {
1029 if (BGP_DEBUG(nht
, NHT
))
1031 "%s: prefix %pBD (vrf %s), ignoring path due to martian or self-next-hop",
1032 __func__
, dest
, bgp_path
->name
);
1034 bnc_is_valid_nexthop
=
1035 bgp_isvalid_nexthop(bnc
) ? true : false;
1038 if (BGP_DEBUG(nht
, NHT
)) {
1039 char buf1
[RD_ADDRSTRLEN
];
1042 prefix_rd2str((struct prefix_rd
*)bgp_dest_get_prefix(dest
->pdest
),
1043 buf1
, sizeof(buf1
));
1045 "... eval path %d/%d %pBD RD %s %s flags 0x%x",
1046 afi
, safi
, dest
, buf1
,
1047 bgp_path
->name_pretty
, path
->flags
);
1050 "... eval path %d/%d %pBD %s flags 0x%x",
1051 afi
, safi
, dest
, bgp_path
->name_pretty
,
1055 /* Skip paths marked for removal or as history. */
1056 if (CHECK_FLAG(path
->flags
, BGP_PATH_REMOVED
)
1057 || CHECK_FLAG(path
->flags
, BGP_PATH_HISTORY
))
1060 /* Copy the metric to the path. Will be used for bestpath
1062 if (bgp_isvalid_nexthop(bnc
) && bnc
->metric
)
1063 (bgp_path_info_extra_get(path
))->igpmetric
=
1065 else if (path
->extra
)
1066 path
->extra
->igpmetric
= 0;
1068 if (CHECK_FLAG(bnc
->change_flags
, BGP_NEXTHOP_METRIC_CHANGED
)
1069 || CHECK_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
)
1070 || path
->attr
->srte_color
!= 0)
1071 SET_FLAG(path
->flags
, BGP_PATH_IGP_CHANGED
);
1073 path_valid
= CHECK_FLAG(path
->flags
, BGP_PATH_VALID
);
1074 if (path_valid
!= bnc_is_valid_nexthop
) {
1076 /* No longer valid, clear flag; also for EVPN
1077 * routes, unimport from VRFs if needed.
1079 bgp_aggregate_decrement(bgp_path
, p
, path
, afi
,
1081 bgp_path_info_unset_flag(dest
, path
,
1083 if (safi
== SAFI_EVPN
&&
1084 bgp_evpn_is_prefix_nht_supported(bgp_dest_get_prefix(dest
)))
1085 bgp_evpn_unimport_route(bgp_path
,
1086 afi
, safi
, bgp_dest_get_prefix(dest
), path
);
1088 /* Path becomes valid, set flag; also for EVPN
1089 * routes, import from VRFs if needed.
1091 bgp_path_info_set_flag(dest
, path
,
1093 bgp_aggregate_increment(bgp_path
, p
, path
, afi
,
1095 if (safi
== SAFI_EVPN
&&
1096 bgp_evpn_is_prefix_nht_supported(bgp_dest_get_prefix(dest
)))
1097 bgp_evpn_import_route(bgp_path
,
1098 afi
, safi
, bgp_dest_get_prefix(dest
), path
);
1102 bgp_process(bgp_path
, dest
, afi
, safi
);
1106 int valid_nexthops
= bgp_isvalid_nexthop(bnc
);
1108 if (valid_nexthops
) {
1110 * Peering cannot occur across a blackhole nexthop
1112 if (bnc
->nexthop_num
== 1 && bnc
->nexthop
1113 && bnc
->nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1114 peer
->last_reset
= PEER_DOWN_WAITING_NHT
;
1117 peer
->last_reset
= PEER_DOWN_WAITING_OPEN
;
1119 peer
->last_reset
= PEER_DOWN_WAITING_NHT
;
1121 if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
)) {
1122 if (BGP_DEBUG(nht
, NHT
))
1124 "%s: Updating peer (%s(%s)) status with NHT nexthops %d",
1125 __func__
, peer
->host
,
1126 peer
->bgp
->name_pretty
,
1128 bgp_fsm_nht_update(peer
, !!valid_nexthops
);
1129 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
1133 RESET_FLAG(bnc
->change_flags
);
1137 * path_nh_map - make or break path-to-nexthop association.
1139 * path - pointer to the path structure
1140 * bnc - pointer to the nexthop structure
1141 * make - if set, make the association. if unset, just break the existing
1144 void path_nh_map(struct bgp_path_info
*path
, struct bgp_nexthop_cache
*bnc
,
1147 if (path
->nexthop
) {
1148 LIST_REMOVE(path
, nh_thread
);
1149 path
->nexthop
->path_count
--;
1150 path
->nexthop
= NULL
;
1153 LIST_INSERT_HEAD(&(bnc
->paths
), path
, nh_thread
);
1154 path
->nexthop
= bnc
;
1155 path
->nexthop
->path_count
++;
1160 * This function is called to register nexthops to zebra
1161 * as that we may have tried to install the nexthops
1162 * before we actually have a zebra connection
1164 void bgp_nht_register_nexthops(struct bgp
*bgp
)
1166 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
1167 struct bgp_nexthop_cache
*bnc
;
1169 frr_each (bgp_nexthop_cache
, &bgp
->nexthop_cache_table
[afi
],
1171 register_zebra_rnh(bnc
);
1176 void bgp_nht_reg_enhe_cap_intfs(struct peer
*peer
)
1179 struct bgp_nexthop_cache
*bnc
;
1180 struct nexthop
*nhop
;
1181 struct interface
*ifp
;
1188 if (!sockunion2hostprefix(&peer
->su
, &p
)) {
1189 zlog_warn("%s: Unable to convert sockunion to prefix for %s",
1190 __func__
, peer
->host
);
1194 if (p
.family
!= AF_INET6
)
1197 bnc
= bnc_find(&bgp
->nexthop_cache_table
[AFI_IP6
], &p
, 0);
1201 if (peer
!= bnc
->nht_info
)
1204 for (nhop
= bnc
->nexthop
; nhop
; nhop
= nhop
->next
) {
1205 ifp
= if_lookup_by_index(nhop
->ifindex
, nhop
->vrf_id
);
1210 zclient_send_interface_radv_req(zclient
,
1213 BGP_UNNUM_DEFAULT_RA_INTERVAL
);
1217 void bgp_nht_dereg_enhe_cap_intfs(struct peer
*peer
)
1220 struct bgp_nexthop_cache
*bnc
;
1221 struct nexthop
*nhop
;
1222 struct interface
*ifp
;
1230 if (!sockunion2hostprefix(&peer
->su
, &p
)) {
1231 zlog_warn("%s: Unable to convert sockunion to prefix for %s",
1232 __func__
, peer
->host
);
1236 if (p
.family
!= AF_INET6
)
1239 bnc
= bnc_find(&bgp
->nexthop_cache_table
[AFI_IP6
], &p
, 0);
1243 if (peer
!= bnc
->nht_info
)
1246 for (nhop
= bnc
->nexthop
; nhop
; nhop
= nhop
->next
) {
1247 ifp
= if_lookup_by_index(nhop
->ifindex
, nhop
->vrf_id
);
1252 zclient_send_interface_radv_req(zclient
, nhop
->vrf_id
, ifp
, 0,
1257 /****************************************************************************
1258 * L3 NHGs are used for fast failover of nexthops in the dplane. These are
1259 * the APIs for allocating L3 NHG ids. Management of the L3 NHG itself is
1260 * left to the application using it.
1261 * PS: Currently EVPN host routes is the only app using L3 NHG for fast
1262 * failover of remote ES links.
1263 ***************************************************************************/
1264 static bitfield_t bgp_nh_id_bitmap
;
1265 static uint32_t bgp_l3nhg_start
;
1267 /* XXX - currently we do nothing on the callbacks */
1268 static void bgp_l3nhg_add_cb(const char *name
)
1271 static void bgp_l3nhg_add_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
1272 const struct nexthop
*nhop
)
1275 static void bgp_l3nhg_del_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
1276 const struct nexthop
*nhop
)
1279 static void bgp_l3nhg_del_cb(const char *name
)
1283 static void bgp_l3nhg_zebra_init(void)
1285 static bool bgp_l3nhg_zebra_inited
;
1286 if (bgp_l3nhg_zebra_inited
)
1289 bgp_l3nhg_zebra_inited
= true;
1290 bgp_l3nhg_start
= zclient_get_nhg_start(ZEBRA_ROUTE_BGP
);
1291 nexthop_group_init(bgp_l3nhg_add_cb
, bgp_l3nhg_add_nexthop_cb
,
1292 bgp_l3nhg_del_nexthop_cb
, bgp_l3nhg_del_cb
);
1296 void bgp_l3nhg_init(void)
1300 id_max
= MIN(ZEBRA_NHG_PROTO_SPACING
- 1, 16 * 1024);
1301 bf_init(bgp_nh_id_bitmap
, id_max
);
1302 bf_assign_zero_index(bgp_nh_id_bitmap
);
1304 if (BGP_DEBUG(nht
, NHT
) || BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
1305 zlog_debug("bgp l3_nhg range %u - %u", bgp_l3nhg_start
+ 1,
1306 bgp_l3nhg_start
+ id_max
);
1309 void bgp_l3nhg_finish(void)
1311 bf_free(bgp_nh_id_bitmap
);
1314 uint32_t bgp_l3nhg_id_alloc(void)
1316 uint32_t nhg_id
= 0;
1318 bgp_l3nhg_zebra_init();
1319 bf_assign_index(bgp_nh_id_bitmap
, nhg_id
);
1321 nhg_id
+= bgp_l3nhg_start
;
1326 void bgp_l3nhg_id_free(uint32_t nhg_id
)
1328 if (!nhg_id
|| (nhg_id
<= bgp_l3nhg_start
))
1331 nhg_id
-= bgp_l3nhg_start
;
1333 bf_release_index(bgp_nh_id_bitmap
, nhg_id
);