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 void evaluate_paths(struct bgp_nexthop_cache
*bnc
);
57 static int make_prefix(int afi
, struct bgp_path_info
*pi
, struct prefix
*p
);
58 static int bgp_nht_ifp_initial(struct thread
*thread
);
60 static int bgp_isvalid_nexthop(struct bgp_nexthop_cache
*bnc
)
62 return (bgp_zebra_num_connects() == 0
63 || (bnc
&& CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
)
64 && bnc
->nexthop_num
> 0));
67 static int bgp_isvalid_labeled_nexthop(struct bgp_nexthop_cache
*bnc
)
70 * In the case of MPLS-VPN, the label is learned from LDP or other
71 * protocols, and nexthop tracking is enabled for the label.
72 * The value is recorded as BGP_NEXTHOP_LABELED_VALID.
73 * In the case of SRv6-VPN, we need to track the reachability to the
74 * SID (in other words, IPv6 address). As in MPLS, we need to record
75 * the value as BGP_NEXTHOP_SID_VALID. However, this function is
76 * currently not implemented, and this function assumes that all
77 * Transit routes for SRv6-VPN are valid.
79 return (bgp_zebra_num_connects() == 0
80 || (bnc
&& bnc
->nexthop_num
> 0
81 && (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_LABELED_VALID
)
82 || bnc
->bgp
->srv6_enabled
)));
85 static void bgp_unlink_nexthop_check(struct bgp_nexthop_cache
*bnc
)
87 if (LIST_EMPTY(&(bnc
->paths
)) && !bnc
->nht_info
) {
88 if (BGP_DEBUG(nht
, NHT
)) {
89 char buf
[PREFIX2STR_BUFFER
];
90 zlog_debug("%s: freeing bnc %s(%u)(%s)", __func__
,
91 bnc_str(bnc
, buf
, PREFIX2STR_BUFFER
),
92 bnc
->srte_color
, bnc
->bgp
->name_pretty
);
94 /* only unregister if this is the last nh for this prefix*/
95 if (!bnc_existing_for_prefix(bnc
))
97 bnc
, CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE
));
102 void bgp_unlink_nexthop(struct bgp_path_info
*path
)
104 struct bgp_nexthop_cache
*bnc
= path
->nexthop
;
109 path_nh_map(path
, NULL
, false);
111 bgp_unlink_nexthop_check(bnc
);
114 void bgp_replace_nexthop_by_peer(struct peer
*from
, struct peer
*to
)
118 struct bgp_nexthop_cache
*bncp
, *bnct
;
121 if (!sockunion2hostprefix(&from
->su
, &pp
))
124 afi
= family2afi(pp
.family
);
125 bncp
= bnc_find(&from
->bgp
->nexthop_cache_table
[afi
], &pp
, 0);
127 if (!sockunion2hostprefix(&to
->su
, &pt
))
130 bnct
= bnc_find(&to
->bgp
->nexthop_cache_table
[afi
], &pt
, 0);
139 void bgp_unlink_nexthop_by_peer(struct peer
*peer
)
142 struct bgp_nexthop_cache
*bnc
;
143 afi_t afi
= family2afi(peer
->su
.sa
.sa_family
);
145 if (!sockunion2hostprefix(&peer
->su
, &p
))
148 bnc
= bnc_find(&peer
->bgp
->nexthop_cache_table
[afi
], &p
, 0);
152 /* cleanup the peer reference */
153 bnc
->nht_info
= NULL
;
155 bgp_unlink_nexthop_check(bnc
);
159 * A route and its nexthop might belong to different VRFs. Therefore,
160 * we need both the bgp_route and bgp_nexthop pointers.
162 int bgp_find_or_add_nexthop(struct bgp
*bgp_route
, struct bgp
*bgp_nexthop
,
163 afi_t afi
, safi_t safi
, struct bgp_path_info
*pi
,
164 struct peer
*peer
, int connected
)
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 srte_color
= pi
->attr
->srte_color
;
199 * Gather the ifindex for if up/down events to be
200 * tagged into this fun
203 && IN6_IS_ADDR_LINKLOCAL(&peer
->su
.sin6
.sin6_addr
))
204 ifindex
= peer
->su
.sin6
.sin6_scope_id
;
206 if (!sockunion2hostprefix(&peer
->su
, &p
)) {
207 if (BGP_DEBUG(nht
, NHT
)) {
209 "%s: Attempting to register with unknown AFI %d (not %d or %d)",
210 __func__
, afi
, AFI_IP
, AFI_IP6
);
217 if (is_bgp_static_route
)
218 tree
= &bgp_nexthop
->import_check_table
[afi
];
220 tree
= &bgp_nexthop
->nexthop_cache_table
[afi
];
222 bnc
= bnc_find(tree
, &p
, srte_color
);
224 bnc
= bnc_new(tree
, &p
, srte_color
);
225 bnc
->bgp
= bgp_nexthop
;
226 bnc
->ifindex
= ifindex
;
227 if (BGP_DEBUG(nht
, NHT
)) {
228 char buf
[PREFIX2STR_BUFFER
];
230 zlog_debug("Allocated bnc %s(%u)(%s) peer %p",
231 bnc_str(bnc
, buf
, PREFIX2STR_BUFFER
),
232 bnc
->srte_color
, bnc
->bgp
->name_pretty
,
236 if (BGP_DEBUG(nht
, NHT
)) {
237 char buf
[PREFIX2STR_BUFFER
];
240 "Found existing bnc %s(%s) flags 0x%x ifindex %d #paths %d peer %p",
241 bnc_str(bnc
, buf
, PREFIX2STR_BUFFER
),
242 bnc
->bgp
->name_pretty
, bnc
->flags
, bnc
->ifindex
,
243 bnc
->path_count
, bnc
->nht_info
);
247 if (is_bgp_static_route
) {
248 SET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE
);
250 /* If we're toggling the type, re-register */
251 if ((CHECK_FLAG(bgp_route
->flags
, BGP_FLAG_IMPORT_CHECK
))
252 && !CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
)) {
253 SET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
);
254 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
255 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
256 } else if ((!CHECK_FLAG(bgp_route
->flags
,
257 BGP_FLAG_IMPORT_CHECK
))
258 && CHECK_FLAG(bnc
->flags
,
259 BGP_STATIC_ROUTE_EXACT_MATCH
)) {
260 UNSET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
);
261 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
262 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
265 /* When nexthop is already known, but now requires 'connected'
267 * re-register it. The reverse scenario where the nexthop currently
269 * 'connected' resolution does not need a re-register (i.e., we treat
270 * 'connected-required' as an override) except in the scenario where
272 * is actually a case of tracking a peer for connectivity (e.g., after
273 * disable connected-check).
274 * NOTE: We don't track the number of paths separately for 'connected-
275 * required' vs 'connected-not-required' as this change is not a common
278 else if (connected
&& !CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
)) {
279 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
);
280 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
281 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
282 } else if (peer
&& !connected
283 && CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
)) {
284 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
);
285 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
286 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
288 if (peer
&& (bnc
->ifindex
!= ifindex
)) {
289 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
290 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
291 bnc
->ifindex
= ifindex
;
293 if (bgp_route
->inst_type
== BGP_INSTANCE_TYPE_VIEW
) {
294 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
295 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
296 } else if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
)
297 && !is_default_host_route(&bnc
->prefix
))
298 register_zebra_rnh(bnc
, is_bgp_static_route
);
300 if (pi
&& pi
->nexthop
!= bnc
) {
301 /* Unlink from existing nexthop cache, if any. This will also
303 * the nexthop cache entry, if appropriate.
305 bgp_unlink_nexthop(pi
);
307 /* updates NHT pi list reference */
308 path_nh_map(pi
, bnc
, true);
310 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
) && bnc
->metric
)
311 (bgp_path_info_extra_get(pi
))->igpmetric
= bnc
->metric
;
313 pi
->extra
->igpmetric
= 0;
316 * Let's not accidently save the peer data for a peer
317 * we are going to throw away in a second or so.
318 * When we come back around we'll fix up this
319 * data properly in replace_nexthop_by_peer
321 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
322 bnc
->nht_info
= (void *)peer
; /* NHT peer reference */
326 * We are cheating here. Views have no associated underlying
327 * ability to detect nexthops. So when we have a view
328 * just tell everyone the nexthop is valid
330 if (bgp_route
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
332 else if (safi
== SAFI_UNICAST
&& pi
333 && pi
->sub_type
== BGP_ROUTE_IMPORTED
&& pi
->extra
334 && pi
->extra
->num_labels
) {
335 return bgp_isvalid_labeled_nexthop(bnc
);
337 return (bgp_isvalid_nexthop(bnc
));
340 void bgp_delete_connected_nexthop(afi_t afi
, struct peer
*peer
)
342 struct bgp_nexthop_cache
*bnc
;
348 if (!sockunion2hostprefix(&peer
->su
, &p
))
351 bnc
= bnc_find(&peer
->bgp
->nexthop_cache_table
[family2afi(p
.family
)],
354 if (BGP_DEBUG(nht
, NHT
))
356 "Cannot find connected NHT node for peer %s(%s)",
357 peer
->host
, peer
->bgp
->name_pretty
);
361 if (bnc
->nht_info
!= peer
) {
362 if (BGP_DEBUG(nht
, NHT
))
364 "Connected NHT %p node for peer %s(%s) points to %p",
365 bnc
, peer
->host
, bnc
->bgp
->name_pretty
,
370 bnc
->nht_info
= NULL
;
372 if (LIST_EMPTY(&(bnc
->paths
))) {
373 if (BGP_DEBUG(nht
, NHT
))
375 "Freeing connected NHT node %p for peer %s(%s)",
376 bnc
, peer
->host
, bnc
->bgp
->name_pretty
);
377 unregister_zebra_rnh(bnc
, 0);
382 static void bgp_process_nexthop_update(struct bgp_nexthop_cache
*bnc
,
383 struct zapi_route
*nhr
)
385 struct nexthop
*nexthop
;
386 struct nexthop
*oldnh
;
387 struct nexthop
*nhlist_head
= NULL
;
388 struct nexthop
*nhlist_tail
= NULL
;
391 bnc
->last_update
= bgp_clock();
392 bnc
->change_flags
= 0;
394 /* debug print the input */
395 if (BGP_DEBUG(nht
, NHT
)) {
396 char bnc_buf
[BNC_FLAG_DUMP_SIZE
];
399 "%s(%u): Rcvd NH update %pFX(%u) - metric %d/%d #nhops %d/%d flags %s",
400 bnc
->bgp
->name_pretty
, bnc
->bgp
->vrf_id
, &nhr
->prefix
,
401 bnc
->srte_color
, nhr
->metric
, bnc
->metric
,
402 nhr
->nexthop_num
, bnc
->nexthop_num
,
403 bgp_nexthop_dump_bnc_flags(bnc
, bnc_buf
,
407 if (nhr
->metric
!= bnc
->metric
)
408 bnc
->change_flags
|= BGP_NEXTHOP_METRIC_CHANGED
;
410 if (nhr
->nexthop_num
!= bnc
->nexthop_num
)
411 bnc
->change_flags
|= BGP_NEXTHOP_CHANGED
;
413 if (nhr
->nexthop_num
) {
414 struct peer
*peer
= bnc
->nht_info
;
416 /* notify bgp fsm if nbr ip goes from invalid->valid */
417 if (!bnc
->nexthop_num
)
418 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
420 bnc
->flags
|= BGP_NEXTHOP_VALID
;
421 bnc
->metric
= nhr
->metric
;
422 bnc
->nexthop_num
= nhr
->nexthop_num
;
424 bnc
->flags
&= ~BGP_NEXTHOP_LABELED_VALID
; /* check below */
426 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
429 nexthop
= nexthop_from_zapi_nexthop(&nhr
->nexthops
[i
]);
432 * Turn on RA for the v6 nexthops
433 * we receive from bgp. This is to allow us
434 * to work with v4 routing over v6 nexthops
436 if (peer
&& !peer
->ifp
437 && CHECK_FLAG(peer
->flags
,
438 PEER_FLAG_CAPABILITY_ENHE
)
439 && nhr
->prefix
.family
== AF_INET6
440 && nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
441 struct interface
*ifp
;
443 ifp
= if_lookup_by_index(nexthop
->ifindex
,
446 zclient_send_interface_radv_req(
447 zclient
, nexthop
->vrf_id
, ifp
,
449 BGP_UNNUM_DEFAULT_RA_INTERVAL
);
451 /* There is at least one label-switched path */
452 if (nexthop
->nh_label
&&
453 nexthop
->nh_label
->num_labels
) {
455 bnc
->flags
|= BGP_NEXTHOP_LABELED_VALID
;
456 num_labels
= nexthop
->nh_label
->num_labels
;
459 if (BGP_DEBUG(nht
, NHT
)) {
460 char buf
[NEXTHOP_STRLEN
];
462 " nhop via %s (%d labels)",
463 nexthop2str(nexthop
, buf
, sizeof(buf
)),
468 nhlist_tail
->next
= nexthop
;
469 nhlist_tail
= nexthop
;
471 nhlist_tail
= nexthop
;
472 nhlist_head
= nexthop
;
475 /* No need to evaluate the nexthop if we have already
477 * that there has been a change.
479 if (bnc
->change_flags
& BGP_NEXTHOP_CHANGED
)
482 for (oldnh
= bnc
->nexthop
; oldnh
; oldnh
= oldnh
->next
)
483 if (nexthop_same(oldnh
, nexthop
))
487 bnc
->change_flags
|= BGP_NEXTHOP_CHANGED
;
489 bnc_nexthop_free(bnc
);
490 bnc
->nexthop
= nhlist_head
;
492 bnc
->flags
&= ~BGP_NEXTHOP_VALID
;
493 bnc
->flags
&= ~BGP_NEXTHOP_LABELED_VALID
;
494 bnc
->nexthop_num
= nhr
->nexthop_num
;
496 /* notify bgp fsm if nbr ip goes from valid->invalid */
497 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
499 bnc_nexthop_free(bnc
);
506 static void bgp_nht_ifp_table_handle(struct bgp
*bgp
,
507 struct bgp_nexthop_cache_head
*table
,
508 struct interface
*ifp
, bool up
)
510 struct bgp_nexthop_cache
*bnc
;
512 frr_each (bgp_nexthop_cache
, table
, bnc
) {
513 if (bnc
->ifindex
!= ifp
->ifindex
)
516 bnc
->last_update
= bgp_clock();
517 bnc
->change_flags
= 0;
520 * For interface based routes ( ala the v6 LL routes
521 * that this was written for ) the metric received
522 * for the connected route is 0 not 1.
526 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
527 SET_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
);
528 bnc
->nexthop_num
= 1;
530 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
531 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
532 SET_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
);
533 bnc
->nexthop_num
= 0;
539 static void bgp_nht_ifp_handle(struct interface
*ifp
, bool up
)
543 bgp
= bgp_lookup_by_vrf_id(ifp
->vrf_id
);
547 bgp_nht_ifp_table_handle(bgp
, &bgp
->nexthop_cache_table
[AFI_IP6
], ifp
,
549 bgp_nht_ifp_table_handle(bgp
, &bgp
->import_check_table
[AFI_IP6
], ifp
,
553 void bgp_nht_ifp_up(struct interface
*ifp
)
555 bgp_nht_ifp_handle(ifp
, true);
558 void bgp_nht_ifp_down(struct interface
*ifp
)
560 bgp_nht_ifp_handle(ifp
, false);
563 static int bgp_nht_ifp_initial(struct thread
*thread
)
565 ifindex_t ifindex
= THREAD_VAL(thread
);
566 struct interface
*ifp
= if_lookup_by_index_all_vrf(ifindex
);
571 if (BGP_DEBUG(nht
, NHT
))
573 "Handle NHT initial update for Intf %s(%d) status %s",
574 ifp
->name
, ifp
->ifindex
, if_is_up(ifp
) ? "up" : "down");
579 bgp_nht_ifp_down(ifp
);
585 * So the bnc code has the ability to handle interface up/down
586 * events to properly handle v6 LL peering.
587 * What is happening here:
588 * The event system for peering expects the nht code to
589 * report on the tracking events after we move to active
590 * So let's give the system a chance to report on that event
591 * in a manner that is expected.
593 void bgp_nht_interface_events(struct peer
*peer
)
595 struct bgp
*bgp
= peer
->bgp
;
596 struct bgp_nexthop_cache_head
*table
;
597 struct bgp_nexthop_cache
*bnc
;
600 if (!IN6_IS_ADDR_LINKLOCAL(&peer
->su
.sin6
.sin6_addr
))
603 if (!sockunion2hostprefix(&peer
->su
, &p
))
606 table
= &bgp
->nexthop_cache_table
[AFI_IP6
];
607 bnc
= bnc_find(table
, &p
, 0);
612 thread_add_event(bm
->master
, bgp_nht_ifp_initial
, NULL
,
616 void bgp_parse_nexthop_update(int command
, vrf_id_t vrf_id
)
618 struct bgp_nexthop_cache_head
*tree
= NULL
;
619 struct bgp_nexthop_cache
*bnc
;
621 struct zapi_route nhr
;
624 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
628 "parse nexthop update: instance not found for vrf_id %u",
633 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &nhr
)) {
634 zlog_err("%s[%s]: Failure to decode nexthop update", __func__
,
639 afi
= family2afi(nhr
.prefix
.family
);
640 if (command
== ZEBRA_NEXTHOP_UPDATE
)
641 tree
= &bgp
->nexthop_cache_table
[afi
];
642 else if (command
== ZEBRA_IMPORT_CHECK_UPDATE
)
643 tree
= &bgp
->import_check_table
[afi
];
645 bnc
= bnc_find(tree
, &nhr
.prefix
, nhr
.srte_color
);
647 if (BGP_DEBUG(nht
, NHT
))
649 "parse nexthop update(%pFX(%u)(%s)): bnc info not found",
650 &nhr
.prefix
, nhr
.srte_color
, bgp
->name_pretty
);
654 bgp_process_nexthop_update(bnc
, &nhr
);
657 * HACK: if any BGP route is dependant on an SR-policy that doesn't
658 * exist, zebra will never send NH updates relative to that policy. In
659 * that case, whenever we receive an update about a colorless NH, update
660 * the corresponding colorful NHs that share the same endpoint but that
661 * are inactive. This ugly hack should work around the problem at the
662 * cost of a performance pernalty. Long term, what should be done is to
663 * make zebra's RNH subsystem aware of SR-TE colors (like bgpd is),
664 * which should provide a better infrastructure to solve this issue in
665 * a more efficient and elegant way.
667 if (nhr
.srte_color
== 0) {
668 struct bgp_nexthop_cache
*bnc_iter
;
670 frr_each (bgp_nexthop_cache
, &bgp
->nexthop_cache_table
[afi
],
672 if (!prefix_same(&bnc
->prefix
, &bnc_iter
->prefix
)
673 || bnc_iter
->srte_color
== 0
674 || CHECK_FLAG(bnc_iter
->flags
, BGP_NEXTHOP_VALID
))
677 bgp_process_nexthop_update(bnc_iter
, &nhr
);
683 * Cleanup nexthop registration and status information for BGP nexthops
684 * pertaining to this VRF. This is invoked upon VRF deletion.
686 void bgp_cleanup_nexthops(struct bgp
*bgp
)
688 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
689 struct bgp_nexthop_cache
*bnc
;
691 frr_each (bgp_nexthop_cache
, &bgp
->nexthop_cache_table
[afi
],
693 /* Clear relevant flags. */
694 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
695 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
696 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
702 * make_prefix - make a prefix structure from the path (essentially
705 static int make_prefix(int afi
, struct bgp_path_info
*pi
, struct prefix
*p
)
708 int is_bgp_static
= ((pi
->type
== ZEBRA_ROUTE_BGP
)
709 && (pi
->sub_type
== BGP_ROUTE_STATIC
))
712 struct bgp_dest
*net
= pi
->net
;
713 const struct prefix
*p_orig
= bgp_dest_get_prefix(net
);
716 if (p_orig
->family
== AF_FLOWSPEC
) {
719 return bgp_flowspec_get_first_nh(pi
->peer
->bgp
,
722 memset(p
, 0, sizeof(struct prefix
));
727 p
->u
.prefix4
= p_orig
->u
.prefix4
;
728 p
->prefixlen
= p_orig
->prefixlen
;
730 if (IS_MAPPED_IPV6(&pi
->attr
->mp_nexthop_global
)) {
731 ipv4_mapped_ipv6_to_ipv4(
732 &pi
->attr
->mp_nexthop_global
, &ipv4
);
734 p
->prefixlen
= IPV4_MAX_BITLEN
;
736 p
->u
.prefix4
= pi
->attr
->nexthop
;
737 p
->prefixlen
= IPV4_MAX_BITLEN
;
742 p
->family
= AF_INET6
;
745 p
->u
.prefix6
= p_orig
->u
.prefix6
;
746 p
->prefixlen
= p_orig
->prefixlen
;
748 /* If we receive MP_REACH nexthop with ::(LL)
749 * or LL(LL), use LL address as nexthop cache.
751 if (pi
->attr
->mp_nexthop_len
752 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
753 && (IN6_IS_ADDR_UNSPECIFIED(
754 &pi
->attr
->mp_nexthop_global
)
755 || IN6_IS_ADDR_LINKLOCAL(
756 &pi
->attr
->mp_nexthop_global
)))
757 p
->u
.prefix6
= pi
->attr
->mp_nexthop_local
;
759 p
->u
.prefix6
= pi
->attr
->mp_nexthop_global
;
760 p
->prefixlen
= IPV6_MAX_BITLEN
;
764 if (BGP_DEBUG(nht
, NHT
)) {
766 "%s: Attempting to make prefix with unknown AFI %d (not %d or %d)",
767 __func__
, afi
, AFI_IP
, AFI_IP6
);
775 * sendmsg_zebra_rnh -- Format and send a nexthop register/Unregister
778 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
779 * int command -- command to send to zebra
783 static void sendmsg_zebra_rnh(struct bgp_nexthop_cache
*bnc
, int command
)
785 bool exact_match
= false;
791 /* Don't try to register if Zebra doesn't know of this instance. */
792 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bnc
->bgp
)) {
793 if (BGP_DEBUG(zebra
, ZEBRA
))
795 "%s: No zebra instance to talk to, not installing NHT entry",
800 if (!bgp_zebra_num_connects()) {
801 if (BGP_DEBUG(zebra
, ZEBRA
))
803 "%s: We have not connected yet, cannot send nexthops",
806 if ((command
== ZEBRA_NEXTHOP_REGISTER
807 || command
== ZEBRA_IMPORT_ROUTE_REGISTER
)
808 && (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
)
809 || CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
)))
812 if (BGP_DEBUG(zebra
, ZEBRA
))
813 zlog_debug("%s: sending cmd %s for %pFX (vrf %s)", __func__
,
814 zserv_command_string(command
), &bnc
->prefix
,
815 bnc
->bgp
->name_pretty
);
817 ret
= zclient_send_rnh(zclient
, command
, &bnc
->prefix
, exact_match
,
819 /* TBD: handle the failure */
820 if (ret
== ZCLIENT_SEND_FAILURE
)
821 flog_warn(EC_BGP_ZEBRA_SEND
,
822 "sendmsg_nexthop: zclient_send_message() failed");
824 if ((command
== ZEBRA_NEXTHOP_REGISTER
)
825 || (command
== ZEBRA_IMPORT_ROUTE_REGISTER
))
826 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
827 else if ((command
== ZEBRA_NEXTHOP_UNREGISTER
)
828 || (command
== ZEBRA_IMPORT_ROUTE_UNREGISTER
))
829 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
834 * register_zebra_rnh - register a NH/route with Zebra for notification
835 * when the route or the route to the nexthop changes.
837 * struct bgp_nexthop_cache *bnc
841 static void register_zebra_rnh(struct bgp_nexthop_cache
*bnc
,
842 int is_bgp_import_route
)
844 /* Check if we have already registered */
845 if (bnc
->flags
& BGP_NEXTHOP_REGISTERED
)
849 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
853 if (is_bgp_import_route
)
854 sendmsg_zebra_rnh(bnc
, ZEBRA_IMPORT_ROUTE_REGISTER
);
856 sendmsg_zebra_rnh(bnc
, ZEBRA_NEXTHOP_REGISTER
);
860 * unregister_zebra_rnh -- Unregister the route/nexthop from Zebra.
862 * struct bgp_nexthop_cache *bnc
866 static void unregister_zebra_rnh(struct bgp_nexthop_cache
*bnc
,
867 int is_bgp_import_route
)
869 /* Check if we have already registered */
870 if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
))
874 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
878 if (is_bgp_import_route
)
879 sendmsg_zebra_rnh(bnc
, ZEBRA_IMPORT_ROUTE_UNREGISTER
);
881 sendmsg_zebra_rnh(bnc
, ZEBRA_NEXTHOP_UNREGISTER
);
885 * evaluate_paths - Evaluate the paths/nets associated with a nexthop.
887 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
891 static void evaluate_paths(struct bgp_nexthop_cache
*bnc
)
893 struct bgp_dest
*dest
;
894 struct bgp_path_info
*path
;
896 struct peer
*peer
= (struct peer
*)bnc
->nht_info
;
897 struct bgp_table
*table
;
899 struct bgp
*bgp_path
;
900 const struct prefix
*p
;
902 if (BGP_DEBUG(nht
, NHT
)) {
903 char buf
[PREFIX2STR_BUFFER
];
904 char bnc_buf
[BNC_FLAG_DUMP_SIZE
];
905 char chg_buf
[BNC_FLAG_DUMP_SIZE
];
907 bnc_str(bnc
, buf
, PREFIX2STR_BUFFER
);
909 "NH update for %s(%u)(%s) - flags %s chgflags %s- evaluate paths",
910 buf
, bnc
->srte_color
, bnc
->bgp
->name_pretty
,
911 bgp_nexthop_dump_bnc_flags(bnc
, bnc_buf
,
913 bgp_nexthop_dump_bnc_change_flags(bnc
, chg_buf
,
917 LIST_FOREACH (path
, &(bnc
->paths
), nh_thread
) {
918 if (!(path
->type
== ZEBRA_ROUTE_BGP
919 && ((path
->sub_type
== BGP_ROUTE_NORMAL
)
920 || (path
->sub_type
== BGP_ROUTE_STATIC
)
921 || (path
->sub_type
== BGP_ROUTE_IMPORTED
))))
925 assert(dest
&& bgp_dest_table(dest
));
926 p
= bgp_dest_get_prefix(dest
);
927 afi
= family2afi(p
->family
);
928 table
= bgp_dest_table(dest
);
932 * handle routes from other VRFs (they can have a
933 * nexthop in THIS VRF). bgp_path is the bgp instance
934 * that owns the route referencing this nexthop.
936 bgp_path
= table
->bgp
;
939 * Path becomes valid/invalid depending on whether the nexthop
940 * reachable/unreachable.
942 * In case of unicast routes that were imported from vpn
943 * and that have labels, they are valid only if there are
944 * nexthops with labels
947 bool bnc_is_valid_nexthop
= false;
948 bool path_valid
= false;
950 if (safi
== SAFI_UNICAST
&&
951 path
->sub_type
== BGP_ROUTE_IMPORTED
&&
953 path
->extra
->num_labels
) {
955 bnc_is_valid_nexthop
=
956 bgp_isvalid_labeled_nexthop(bnc
) ? true : false;
958 if (bgp_update_martian_nexthop(
959 bnc
->bgp
, afi
, safi
, path
->type
,
960 path
->sub_type
, path
->attr
, dest
)) {
961 if (BGP_DEBUG(nht
, NHT
))
963 "%s: prefix %pBD (vrf %s), ignoring path due to martian or self-next-hop",
964 __func__
, dest
, bgp_path
->name
);
966 bnc_is_valid_nexthop
=
967 bgp_isvalid_nexthop(bnc
) ? true : false;
970 if (BGP_DEBUG(nht
, NHT
)) {
971 char buf1
[RD_ADDRSTRLEN
];
974 prefix_rd2str((struct prefix_rd
*)bgp_dest_get_prefix(dest
->pdest
),
977 "... eval path %d/%d %pBD RD %s %s flags 0x%x",
978 afi
, safi
, dest
, buf1
,
979 bgp_path
->name_pretty
, path
->flags
);
982 "... eval path %d/%d %pBD %s flags 0x%x",
983 afi
, safi
, dest
, bgp_path
->name_pretty
,
987 /* Skip paths marked for removal or as history. */
988 if (CHECK_FLAG(path
->flags
, BGP_PATH_REMOVED
)
989 || CHECK_FLAG(path
->flags
, BGP_PATH_HISTORY
))
992 /* Copy the metric to the path. Will be used for bestpath
994 if (bgp_isvalid_nexthop(bnc
) && bnc
->metric
)
995 (bgp_path_info_extra_get(path
))->igpmetric
=
997 else if (path
->extra
)
998 path
->extra
->igpmetric
= 0;
1000 if (CHECK_FLAG(bnc
->change_flags
, BGP_NEXTHOP_METRIC_CHANGED
)
1001 || CHECK_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
)
1002 || path
->attr
->srte_color
!= 0)
1003 SET_FLAG(path
->flags
, BGP_PATH_IGP_CHANGED
);
1005 path_valid
= !!CHECK_FLAG(path
->flags
, BGP_PATH_VALID
);
1006 if (path_valid
!= bnc_is_valid_nexthop
) {
1008 /* No longer valid, clear flag; also for EVPN
1009 * routes, unimport from VRFs if needed.
1011 bgp_aggregate_decrement(bgp_path
, p
, path
, afi
,
1013 bgp_path_info_unset_flag(dest
, path
,
1015 if (safi
== SAFI_EVPN
&&
1016 bgp_evpn_is_prefix_nht_supported(bgp_dest_get_prefix(dest
)))
1017 bgp_evpn_unimport_route(bgp_path
,
1018 afi
, safi
, bgp_dest_get_prefix(dest
), path
);
1020 /* Path becomes valid, set flag; also for EVPN
1021 * routes, import from VRFs if needed.
1023 bgp_path_info_set_flag(dest
, path
,
1025 bgp_aggregate_increment(bgp_path
, p
, path
, afi
,
1027 if (safi
== SAFI_EVPN
&&
1028 bgp_evpn_is_prefix_nht_supported(bgp_dest_get_prefix(dest
)))
1029 bgp_evpn_import_route(bgp_path
,
1030 afi
, safi
, bgp_dest_get_prefix(dest
), path
);
1034 bgp_process(bgp_path
, dest
, afi
, safi
);
1038 int valid_nexthops
= bgp_isvalid_nexthop(bnc
);
1040 if (valid_nexthops
) {
1042 * Peering cannot occur across a blackhole nexthop
1044 if (bnc
->nexthop_num
== 1 && bnc
->nexthop
1045 && bnc
->nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1046 peer
->last_reset
= PEER_DOWN_WAITING_NHT
;
1049 peer
->last_reset
= PEER_DOWN_WAITING_OPEN
;
1051 peer
->last_reset
= PEER_DOWN_WAITING_NHT
;
1053 if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
)) {
1054 if (BGP_DEBUG(nht
, NHT
))
1056 "%s: Updating peer (%s(%s)) status with NHT nexthops %d",
1057 __func__
, peer
->host
,
1058 peer
->bgp
->name_pretty
,
1060 bgp_fsm_nht_update(peer
, !!valid_nexthops
);
1061 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
1065 RESET_FLAG(bnc
->change_flags
);
1069 * path_nh_map - make or break path-to-nexthop association.
1071 * path - pointer to the path structure
1072 * bnc - pointer to the nexthop structure
1073 * make - if set, make the association. if unset, just break the existing
1076 void path_nh_map(struct bgp_path_info
*path
, struct bgp_nexthop_cache
*bnc
,
1079 if (path
->nexthop
) {
1080 LIST_REMOVE(path
, nh_thread
);
1081 path
->nexthop
->path_count
--;
1082 path
->nexthop
= NULL
;
1085 LIST_INSERT_HEAD(&(bnc
->paths
), path
, nh_thread
);
1086 path
->nexthop
= bnc
;
1087 path
->nexthop
->path_count
++;
1092 * This function is called to register nexthops to zebra
1093 * as that we may have tried to install the nexthops
1094 * before we actually have a zebra connection
1096 void bgp_nht_register_nexthops(struct bgp
*bgp
)
1098 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
1099 struct bgp_nexthop_cache
*bnc
;
1101 frr_each (bgp_nexthop_cache
, &bgp
->nexthop_cache_table
[afi
],
1103 register_zebra_rnh(bnc
, 0);
1108 void bgp_nht_reg_enhe_cap_intfs(struct peer
*peer
)
1111 struct bgp_nexthop_cache
*bnc
;
1112 struct nexthop
*nhop
;
1113 struct interface
*ifp
;
1120 if (!sockunion2hostprefix(&peer
->su
, &p
)) {
1121 zlog_warn("%s: Unable to convert sockunion to prefix for %s",
1122 __func__
, peer
->host
);
1126 if (p
.family
!= AF_INET6
)
1129 bnc
= bnc_find(&bgp
->nexthop_cache_table
[AFI_IP6
], &p
, 0);
1133 if (peer
!= bnc
->nht_info
)
1136 for (nhop
= bnc
->nexthop
; nhop
; nhop
= nhop
->next
) {
1137 ifp
= if_lookup_by_index(nhop
->ifindex
, nhop
->vrf_id
);
1142 zclient_send_interface_radv_req(zclient
,
1145 BGP_UNNUM_DEFAULT_RA_INTERVAL
);
1149 void bgp_nht_dereg_enhe_cap_intfs(struct peer
*peer
)
1152 struct bgp_nexthop_cache
*bnc
;
1153 struct nexthop
*nhop
;
1154 struct interface
*ifp
;
1162 if (!sockunion2hostprefix(&peer
->su
, &p
)) {
1163 zlog_warn("%s: Unable to convert sockunion to prefix for %s",
1164 __func__
, peer
->host
);
1168 if (p
.family
!= AF_INET6
)
1171 bnc
= bnc_find(&bgp
->nexthop_cache_table
[AFI_IP6
], &p
, 0);
1175 if (peer
!= bnc
->nht_info
)
1178 for (nhop
= bnc
->nexthop
; nhop
; nhop
= nhop
->next
) {
1179 ifp
= if_lookup_by_index(nhop
->ifindex
, nhop
->vrf_id
);
1184 zclient_send_interface_radv_req(zclient
, nhop
->vrf_id
, ifp
, 0,
1189 /****************************************************************************
1190 * L3 NHGs are used for fast failover of nexthops in the dplane. These are
1191 * the APIs for allocating L3 NHG ids. Management of the L3 NHG itself is
1192 * left to the application using it.
1193 * PS: Currently EVPN host routes is the only app using L3 NHG for fast
1194 * failover of remote ES links.
1195 ***************************************************************************/
1196 static bitfield_t bgp_nh_id_bitmap
;
1197 static uint32_t bgp_l3nhg_start
;
1199 /* XXX - currently we do nothing on the callbacks */
1200 static void bgp_l3nhg_add_cb(const char *name
)
1203 static void bgp_l3nhg_add_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
1204 const struct nexthop
*nhop
)
1207 static void bgp_l3nhg_del_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
1208 const struct nexthop
*nhop
)
1211 static void bgp_l3nhg_del_cb(const char *name
)
1215 static void bgp_l3nhg_zebra_init(void)
1217 static bool bgp_l3nhg_zebra_inited
;
1218 if (bgp_l3nhg_zebra_inited
)
1221 bgp_l3nhg_zebra_inited
= true;
1222 bgp_l3nhg_start
= zclient_get_nhg_start(ZEBRA_ROUTE_BGP
);
1223 nexthop_group_init(bgp_l3nhg_add_cb
, bgp_l3nhg_add_nexthop_cb
,
1224 bgp_l3nhg_del_nexthop_cb
, bgp_l3nhg_del_cb
);
1228 #define min(A, B) ((A) < (B) ? (A) : (B))
1229 void bgp_l3nhg_init(void)
1233 id_max
= min(ZEBRA_NHG_PROTO_SPACING
- 1, 16 * 1024);
1234 bf_init(bgp_nh_id_bitmap
, id_max
);
1235 bf_assign_zero_index(bgp_nh_id_bitmap
);
1237 if (BGP_DEBUG(nht
, NHT
) || BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
1238 zlog_debug("bgp l3_nhg range %u - %u", bgp_l3nhg_start
+ 1,
1239 bgp_l3nhg_start
+ id_max
);
1242 void bgp_l3nhg_finish(void)
1244 bf_free(bgp_nh_id_bitmap
);
1247 uint32_t bgp_l3nhg_id_alloc(void)
1249 uint32_t nhg_id
= 0;
1251 bgp_l3nhg_zebra_init();
1252 bf_assign_index(bgp_nh_id_bitmap
, nhg_id
);
1254 nhg_id
+= bgp_l3nhg_start
;
1259 void bgp_l3nhg_id_free(uint32_t nhg_id
)
1261 if (!nhg_id
|| (nhg_id
<= bgp_l3nhg_start
))
1264 nhg_id
-= bgp_l3nhg_start
;
1266 bf_release_index(bgp_nh_id_bitmap
, nhg_id
);