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
35 #include "bgpd/bgpd.h"
36 #include "bgpd/bgp_table.h"
37 #include "bgpd/bgp_route.h"
38 #include "bgpd/bgp_attr.h"
39 #include "bgpd/bgp_nexthop.h"
40 #include "bgpd/bgp_debug.h"
41 #include "bgpd/bgp_errors.h"
42 #include "bgpd/bgp_nht.h"
43 #include "bgpd/bgp_fsm.h"
44 #include "bgpd/bgp_zebra.h"
45 #include "bgpd/bgp_flowspec_util.h"
46 #include "bgpd/bgp_evpn.h"
48 extern struct zclient
*zclient
;
50 static void register_zebra_rnh(struct bgp_nexthop_cache
*bnc
,
51 int is_bgp_static_route
);
52 static void unregister_zebra_rnh(struct bgp_nexthop_cache
*bnc
,
53 int is_bgp_static_route
);
54 static void evaluate_paths(struct bgp_nexthop_cache
*bnc
);
55 static int make_prefix(int afi
, struct bgp_path_info
*pi
, struct prefix
*p
);
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
)));
63 static int bgp_isvalid_labeled_nexthop(struct bgp_nexthop_cache
*bnc
)
65 return (bgp_zebra_num_connects() == 0
66 || (bnc
&& CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_LABELED_VALID
)));
69 static void bgp_unlink_nexthop_check(struct bgp_nexthop_cache
*bnc
)
71 if (LIST_EMPTY(&(bnc
->paths
)) && !bnc
->nht_info
) {
72 if (BGP_DEBUG(nht
, NHT
)) {
73 char buf
[PREFIX2STR_BUFFER
];
74 zlog_debug("bgp_unlink_nexthop: freeing bnc %s(%s)",
75 bnc_str(bnc
, buf
, PREFIX2STR_BUFFER
),
76 bnc
->bgp
->name_pretty
);
78 unregister_zebra_rnh(bnc
,
79 CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE
));
80 bgp_node_set_bgp_nexthop_info(bnc
->node
, NULL
);
81 bgp_unlock_node(bnc
->node
);
87 void bgp_unlink_nexthop(struct bgp_path_info
*path
)
89 struct bgp_nexthop_cache
*bnc
= path
->nexthop
;
94 path_nh_map(path
, NULL
, false);
96 bgp_unlink_nexthop_check(bnc
);
99 void bgp_unlink_nexthop_by_peer(struct peer
*peer
)
103 struct bgp_nexthop_cache
*bnc
;
104 afi_t afi
= family2afi(peer
->su
.sa
.sa_family
);
106 if (!sockunion2hostprefix(&peer
->su
, &p
))
109 rn
= bgp_node_get(peer
->bgp
->nexthop_cache_table
[afi
], &p
);
111 bnc
= bgp_node_get_bgp_nexthop_info(rn
);
115 /* cleanup the peer reference */
116 bnc
->nht_info
= NULL
;
118 bgp_unlink_nexthop_check(bnc
);
122 * A route and its nexthop might belong to different VRFs. Therefore,
123 * we need both the bgp_route and bgp_nexthop pointers.
125 int bgp_find_or_add_nexthop(struct bgp
*bgp_route
, struct bgp
*bgp_nexthop
,
126 afi_t afi
, struct bgp_path_info
*pi
,
127 struct peer
*peer
, int connected
)
130 struct bgp_nexthop_cache
*bnc
;
132 int is_bgp_static_route
= 0;
133 const struct prefix
*bnc_p
;
136 is_bgp_static_route
= ((pi
->type
== ZEBRA_ROUTE_BGP
)
137 && (pi
->sub_type
== BGP_ROUTE_STATIC
))
141 /* Since Extended Next-hop Encoding (RFC5549) support, we want
143 address-family from the next-hop. */
144 if (!is_bgp_static_route
)
145 afi
= BGP_ATTR_NEXTHOP_AFI_IP6(pi
->attr
) ? AFI_IP6
148 /* This will return true if the global IPv6 NH is a link local
150 if (make_prefix(afi
, pi
, &p
) < 0)
153 if (!sockunion2hostprefix(&peer
->su
, &p
)) {
154 if (BGP_DEBUG(nht
, NHT
)) {
156 "%s: Attempting to register with unknown AFI %d (not %d or %d)",
157 __func__
, afi
, AFI_IP
, AFI_IP6
);
164 if (is_bgp_static_route
)
165 rn
= bgp_node_get(bgp_nexthop
->import_check_table
[afi
], &p
);
167 rn
= bgp_node_get(bgp_nexthop
->nexthop_cache_table
[afi
], &p
);
169 bnc
= bgp_node_get_bgp_nexthop_info(rn
);
172 bgp_node_set_bgp_nexthop_info(rn
, bnc
);
174 bnc
->bgp
= bgp_nexthop
;
176 if (BGP_DEBUG(nht
, NHT
)) {
177 char buf
[PREFIX2STR_BUFFER
];
179 zlog_debug("Allocated bnc %s(%s) peer %p",
180 bnc_str(bnc
, buf
, PREFIX2STR_BUFFER
),
181 bnc
->bgp
->name_pretty
, peer
);
185 bnc_p
= bgp_node_get_prefix(bnc
->node
);
188 if (is_bgp_static_route
) {
189 SET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE
);
191 /* If we're toggling the type, re-register */
192 if ((CHECK_FLAG(bgp_route
->flags
, BGP_FLAG_IMPORT_CHECK
))
193 && !CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
)) {
194 SET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
);
195 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
196 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
197 } else if ((!CHECK_FLAG(bgp_route
->flags
,
198 BGP_FLAG_IMPORT_CHECK
))
199 && CHECK_FLAG(bnc
->flags
,
200 BGP_STATIC_ROUTE_EXACT_MATCH
)) {
201 UNSET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
);
202 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
203 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
206 /* When nexthop is already known, but now requires 'connected'
208 * re-register it. The reverse scenario where the nexthop currently
210 * 'connected' resolution does not need a re-register (i.e., we treat
211 * 'connected-required' as an override) except in the scenario where
213 * is actually a case of tracking a peer for connectivity (e.g., after
214 * disable connected-check).
215 * NOTE: We don't track the number of paths separately for 'connected-
216 * required' vs 'connected-not-required' as this change is not a common
219 else if (connected
&& !CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
)) {
220 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
);
221 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
222 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
223 } else if (peer
&& !connected
224 && CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
)) {
225 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
);
226 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
227 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
229 if (bgp_route
->inst_type
== BGP_INSTANCE_TYPE_VIEW
) {
230 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
231 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
232 } else if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
)
233 && !is_default_host_route(bnc_p
))
234 register_zebra_rnh(bnc
, is_bgp_static_route
);
236 if (pi
&& pi
->nexthop
!= bnc
) {
237 /* Unlink from existing nexthop cache, if any. This will also
239 * the nexthop cache entry, if appropriate.
241 bgp_unlink_nexthop(pi
);
243 /* updates NHT pi list reference */
244 path_nh_map(pi
, bnc
, true);
246 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
) && bnc
->metric
)
247 (bgp_path_info_extra_get(pi
))->igpmetric
= bnc
->metric
;
249 pi
->extra
->igpmetric
= 0;
251 bnc
->nht_info
= (void *)peer
; /* NHT peer reference */
254 * We are cheating here. Views have no associated underlying
255 * ability to detect nexthops. So when we have a view
256 * just tell everyone the nexthop is valid
258 if (bgp_route
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
261 return (bgp_isvalid_nexthop(bnc
));
264 void bgp_delete_connected_nexthop(afi_t afi
, struct peer
*peer
)
267 struct bgp_nexthop_cache
*bnc
;
273 if (!sockunion2hostprefix(&peer
->su
, &p
))
276 rn
= bgp_node_lookup(
277 peer
->bgp
->nexthop_cache_table
[family2afi(p
.family
)], &p
);
279 if (BGP_DEBUG(nht
, NHT
))
281 "Cannot find connected NHT node for peer %s(%s)",
282 peer
->host
, peer
->bgp
->name_pretty
);
286 bnc
= bgp_node_get_bgp_nexthop_info(rn
);
288 if (BGP_DEBUG(nht
, NHT
))
290 "Cannot find connected NHT node for peer %s(%s) on route_node as expected",
291 peer
->host
, peer
->bgp
->name_pretty
);
297 if (bnc
->nht_info
!= peer
) {
298 if (BGP_DEBUG(nht
, NHT
))
300 "Connected NHT %p node for peer %s(%s) points to %p",
301 bnc
, peer
->host
, bnc
->bgp
->name_pretty
,
306 bnc
->nht_info
= NULL
;
308 if (LIST_EMPTY(&(bnc
->paths
))) {
309 if (BGP_DEBUG(nht
, NHT
))
311 "Freeing connected NHT node %p for peer %s(%s)",
312 bnc
, peer
->host
, bnc
->bgp
->name_pretty
);
313 unregister_zebra_rnh(bnc
, 0);
314 bgp_node_set_bgp_nexthop_info(bnc
->node
, NULL
);
315 bgp_unlock_node(bnc
->node
);
320 void bgp_parse_nexthop_update(int command
, vrf_id_t vrf_id
)
322 struct bgp_node
*rn
= NULL
;
323 struct bgp_nexthop_cache
*bnc
;
324 struct nexthop
*nexthop
;
325 struct nexthop
*oldnh
;
326 struct nexthop
*nhlist_head
= NULL
;
327 struct nexthop
*nhlist_tail
= NULL
;
330 struct zapi_route nhr
;
332 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
336 "parse nexthop update: instance not found for vrf_id %u",
341 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &nhr
)) {
342 if (BGP_DEBUG(nht
, NHT
))
343 zlog_debug("%s[%s]: Failure to decode nexthop update",
344 __func__
, bgp
->name_pretty
);
348 if (command
== ZEBRA_NEXTHOP_UPDATE
)
349 rn
= bgp_node_lookup(
350 bgp
->nexthop_cache_table
[family2afi(nhr
.prefix
.family
)],
352 else if (command
== ZEBRA_IMPORT_CHECK_UPDATE
)
353 rn
= bgp_node_lookup(
354 bgp
->import_check_table
[family2afi(nhr
.prefix
.family
)],
358 if (BGP_DEBUG(nht
, NHT
)) {
359 char buf
[PREFIX2STR_BUFFER
];
360 prefix2str(&nhr
.prefix
, buf
, sizeof(buf
));
361 zlog_debug("parse nexthop update(%s(%s)): rn not found",
362 buf
, bgp
->name_pretty
);
367 bnc
= bgp_node_get_bgp_nexthop_info(rn
);
369 if (BGP_DEBUG(nht
, NHT
)) {
370 char buf
[PREFIX2STR_BUFFER
];
372 prefix2str(&nhr
.prefix
, buf
, sizeof(buf
));
374 "parse nexthop update(%s(%s)): bnc node info not found",
375 buf
, bgp
->name_pretty
);
382 bnc
->last_update
= bgp_clock();
383 bnc
->change_flags
= 0;
385 /* debug print the input */
386 if (BGP_DEBUG(nht
, NHT
)) {
387 char buf
[PREFIX2STR_BUFFER
];
388 prefix2str(&nhr
.prefix
, buf
, sizeof(buf
));
390 "%s(%u): Rcvd NH update %s - metric %d/%d #nhops %d/%d flags 0x%x",
391 bnc
->bgp
->name_pretty
, vrf_id
, buf
, nhr
.metric
,
392 bnc
->metric
, nhr
.nexthop_num
, bnc
->nexthop_num
,
396 if (nhr
.metric
!= bnc
->metric
)
397 bnc
->change_flags
|= BGP_NEXTHOP_METRIC_CHANGED
;
399 if (nhr
.nexthop_num
!= bnc
->nexthop_num
)
400 bnc
->change_flags
|= BGP_NEXTHOP_CHANGED
;
402 if (nhr
.nexthop_num
) {
403 struct peer
*peer
= bnc
->nht_info
;
405 /* notify bgp fsm if nbr ip goes from invalid->valid */
406 if (!bnc
->nexthop_num
)
407 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
409 bnc
->flags
|= BGP_NEXTHOP_VALID
;
410 bnc
->metric
= nhr
.metric
;
411 bnc
->nexthop_num
= nhr
.nexthop_num
;
413 bnc
->flags
&= ~BGP_NEXTHOP_LABELED_VALID
; /* check below */
415 for (i
= 0; i
< nhr
.nexthop_num
; i
++) {
418 nexthop
= nexthop_from_zapi_nexthop(&nhr
.nexthops
[i
]);
421 * Turn on RA for the v6 nexthops
422 * we receive from bgp. This is to allow us
423 * to work with v4 routing over v6 nexthops
425 if (peer
&& !peer
->ifp
426 && CHECK_FLAG(peer
->flags
,
427 PEER_FLAG_CAPABILITY_ENHE
)
428 && nhr
.prefix
.family
== AF_INET6
429 && nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
430 struct interface
*ifp
;
432 ifp
= if_lookup_by_index(nexthop
->ifindex
,
435 zclient_send_interface_radv_req(
436 zclient
, nexthop
->vrf_id
, ifp
,
438 BGP_UNNUM_DEFAULT_RA_INTERVAL
);
440 /* There is at least one label-switched path */
441 if (nexthop
->nh_label
&&
442 nexthop
->nh_label
->num_labels
) {
444 bnc
->flags
|= BGP_NEXTHOP_LABELED_VALID
;
445 num_labels
= nexthop
->nh_label
->num_labels
;
448 if (BGP_DEBUG(nht
, NHT
)) {
449 char buf
[NEXTHOP_STRLEN
];
451 " nhop via %s (%d labels)",
452 nexthop2str(nexthop
, buf
, sizeof(buf
)),
457 nhlist_tail
->next
= nexthop
;
458 nhlist_tail
= nexthop
;
460 nhlist_tail
= nexthop
;
461 nhlist_head
= nexthop
;
464 /* No need to evaluate the nexthop if we have already
466 * that there has been a change.
468 if (bnc
->change_flags
& BGP_NEXTHOP_CHANGED
)
471 for (oldnh
= bnc
->nexthop
; oldnh
; oldnh
= oldnh
->next
)
472 if (nexthop_same(oldnh
, nexthop
))
476 bnc
->change_flags
|= BGP_NEXTHOP_CHANGED
;
478 bnc_nexthop_free(bnc
);
479 bnc
->nexthop
= nhlist_head
;
481 bnc
->flags
&= ~BGP_NEXTHOP_VALID
;
482 bnc
->nexthop_num
= nhr
.nexthop_num
;
484 /* notify bgp fsm if nbr ip goes from valid->invalid */
485 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
487 bnc_nexthop_free(bnc
);
495 * Cleanup nexthop registration and status information for BGP nexthops
496 * pertaining to this VRF. This is invoked upon VRF deletion.
498 void bgp_cleanup_nexthops(struct bgp
*bgp
)
502 struct bgp_nexthop_cache
*bnc
;
504 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
505 if (!bgp
->nexthop_cache_table
[afi
])
508 for (rn
= bgp_table_top(bgp
->nexthop_cache_table
[afi
]); rn
;
509 rn
= bgp_route_next(rn
)) {
510 bnc
= bgp_node_get_bgp_nexthop_info(rn
);
514 /* Clear relevant flags. */
515 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
516 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
517 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
523 * make_prefix - make a prefix structure from the path (essentially
526 static int make_prefix(int afi
, struct bgp_path_info
*pi
, struct prefix
*p
)
529 int is_bgp_static
= ((pi
->type
== ZEBRA_ROUTE_BGP
)
530 && (pi
->sub_type
== BGP_ROUTE_STATIC
))
533 struct bgp_node
*net
= pi
->net
;
534 const struct prefix
*p_orig
= bgp_node_get_prefix(net
);
536 if (p_orig
->family
== AF_FLOWSPEC
) {
539 return bgp_flowspec_get_first_nh(pi
->peer
->bgp
,
542 memset(p
, 0, sizeof(struct prefix
));
547 p
->u
.prefix4
= p_orig
->u
.prefix4
;
548 p
->prefixlen
= p_orig
->prefixlen
;
550 p
->u
.prefix4
= pi
->attr
->nexthop
;
551 p
->prefixlen
= IPV4_MAX_BITLEN
;
555 p
->family
= AF_INET6
;
558 p
->u
.prefix6
= p_orig
->u
.prefix6
;
559 p
->prefixlen
= p_orig
->prefixlen
;
561 p
->u
.prefix6
= pi
->attr
->mp_nexthop_global
;
562 p
->prefixlen
= IPV6_MAX_BITLEN
;
566 if (BGP_DEBUG(nht
, NHT
)) {
568 "%s: Attempting to make prefix with unknown AFI %d (not %d or %d)",
569 __func__
, afi
, AFI_IP
, AFI_IP6
);
577 * sendmsg_zebra_rnh -- Format and send a nexthop register/Unregister
580 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
581 * int command -- command to send to zebra
585 static void sendmsg_zebra_rnh(struct bgp_nexthop_cache
*bnc
, int command
)
587 const struct prefix
*p
;
588 bool exact_match
= false;
594 /* Don't try to register if Zebra doesn't know of this instance. */
595 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bnc
->bgp
)) {
596 if (BGP_DEBUG(zebra
, ZEBRA
))
598 "%s: No zebra instance to talk to, not installing NHT entry",
603 if (!bgp_zebra_num_connects()) {
604 if (BGP_DEBUG(zebra
, ZEBRA
))
606 "%s: We have not connected yet, cannot send nexthops",
609 p
= bgp_node_get_prefix(bnc
->node
);
610 if ((command
== ZEBRA_NEXTHOP_REGISTER
611 || command
== ZEBRA_IMPORT_ROUTE_REGISTER
)
612 && (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
)
613 || CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
)))
616 if (BGP_DEBUG(zebra
, ZEBRA
)) {
617 char buf
[PREFIX2STR_BUFFER
];
619 prefix2str(p
, buf
, PREFIX2STR_BUFFER
);
620 zlog_debug("%s: sending cmd %s for %s (vrf %s)",
621 __func__
, zserv_command_string(command
), buf
,
622 bnc
->bgp
->name_pretty
);
625 ret
= zclient_send_rnh(zclient
, command
, p
, exact_match
,
627 /* TBD: handle the failure */
629 flog_warn(EC_BGP_ZEBRA_SEND
,
630 "sendmsg_nexthop: zclient_send_message() failed");
632 if ((command
== ZEBRA_NEXTHOP_REGISTER
)
633 || (command
== ZEBRA_IMPORT_ROUTE_REGISTER
))
634 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
635 else if ((command
== ZEBRA_NEXTHOP_UNREGISTER
)
636 || (command
== ZEBRA_IMPORT_ROUTE_UNREGISTER
))
637 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
642 * register_zebra_rnh - register a NH/route with Zebra for notification
643 * when the route or the route to the nexthop changes.
645 * struct bgp_nexthop_cache *bnc
649 static void register_zebra_rnh(struct bgp_nexthop_cache
*bnc
,
650 int is_bgp_import_route
)
652 /* Check if we have already registered */
653 if (bnc
->flags
& BGP_NEXTHOP_REGISTERED
)
655 if (is_bgp_import_route
)
656 sendmsg_zebra_rnh(bnc
, ZEBRA_IMPORT_ROUTE_REGISTER
);
658 sendmsg_zebra_rnh(bnc
, ZEBRA_NEXTHOP_REGISTER
);
662 * unregister_zebra_rnh -- Unregister the route/nexthop from Zebra.
664 * struct bgp_nexthop_cache *bnc
668 static void unregister_zebra_rnh(struct bgp_nexthop_cache
*bnc
,
669 int is_bgp_import_route
)
671 /* Check if we have already registered */
672 if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
))
675 if (is_bgp_import_route
)
676 sendmsg_zebra_rnh(bnc
, ZEBRA_IMPORT_ROUTE_UNREGISTER
);
678 sendmsg_zebra_rnh(bnc
, ZEBRA_NEXTHOP_UNREGISTER
);
682 * evaluate_paths - Evaluate the paths/nets associated with a nexthop.
684 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
688 static void evaluate_paths(struct bgp_nexthop_cache
*bnc
)
691 struct bgp_path_info
*path
;
693 struct peer
*peer
= (struct peer
*)bnc
->nht_info
;
694 struct bgp_table
*table
;
696 struct bgp
*bgp_path
;
697 const struct prefix
*p
;
699 if (BGP_DEBUG(nht
, NHT
)) {
700 char buf
[PREFIX2STR_BUFFER
];
701 bnc_str(bnc
, buf
, PREFIX2STR_BUFFER
);
703 "NH update for %s(%s) - flags 0x%x chgflags 0x%x - evaluate paths",
704 buf
, bnc
->bgp
->name_pretty
, bnc
->flags
,
708 LIST_FOREACH (path
, &(bnc
->paths
), nh_thread
) {
709 if (!(path
->type
== ZEBRA_ROUTE_BGP
710 && ((path
->sub_type
== BGP_ROUTE_NORMAL
)
711 || (path
->sub_type
== BGP_ROUTE_STATIC
)
712 || (path
->sub_type
== BGP_ROUTE_IMPORTED
))))
716 assert(rn
&& bgp_node_table(rn
));
717 p
= bgp_node_get_prefix(rn
);
718 afi
= family2afi(p
->family
);
719 table
= bgp_node_table(rn
);
723 * handle routes from other VRFs (they can have a
724 * nexthop in THIS VRF). bgp_path is the bgp instance
725 * that owns the route referencing this nexthop.
727 bgp_path
= table
->bgp
;
730 * Path becomes valid/invalid depending on whether the nexthop
731 * reachable/unreachable.
733 * In case of unicast routes that were imported from vpn
734 * and that have labels, they are valid only if there are
735 * nexthops with labels
738 int bnc_is_valid_nexthop
= 0;
740 if (safi
== SAFI_UNICAST
&&
741 path
->sub_type
== BGP_ROUTE_IMPORTED
&&
743 path
->extra
->num_labels
) {
745 bnc_is_valid_nexthop
=
746 bgp_isvalid_labeled_nexthop(bnc
) ? 1 : 0;
748 bnc_is_valid_nexthop
=
749 bgp_isvalid_nexthop(bnc
) ? 1 : 0;
752 if (BGP_DEBUG(nht
, NHT
))
753 zlog_debug("%s: prefix %pRN (vrf %s) %svalid", __func__
,
755 (bnc_is_valid_nexthop
? "" : "not "));
757 if ((CHECK_FLAG(path
->flags
, BGP_PATH_VALID
) ? 1 : 0)
758 != bnc_is_valid_nexthop
) {
759 if (CHECK_FLAG(path
->flags
, BGP_PATH_VALID
)) {
760 bgp_aggregate_decrement(bgp_path
, p
, path
, afi
,
762 bgp_path_info_unset_flag(rn
, path
,
765 bgp_path_info_set_flag(rn
, path
,
767 bgp_aggregate_increment(bgp_path
, p
, path
, afi
,
772 /* Copy the metric to the path. Will be used for bestpath
774 if (bgp_isvalid_nexthop(bnc
) && bnc
->metric
)
775 (bgp_path_info_extra_get(path
))->igpmetric
=
777 else if (path
->extra
)
778 path
->extra
->igpmetric
= 0;
780 if (CHECK_FLAG(bnc
->change_flags
, BGP_NEXTHOP_METRIC_CHANGED
)
781 || CHECK_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
))
782 SET_FLAG(path
->flags
, BGP_PATH_IGP_CHANGED
);
784 if (safi
== SAFI_EVPN
&& bgp_evpn_is_prefix_nht_supported(p
)) {
785 if (CHECK_FLAG(path
->flags
, BGP_PATH_VALID
))
786 bgp_evpn_import_route(bgp_path
, afi
, safi
, p
,
789 bgp_evpn_unimport_route(bgp_path
, afi
, safi
, p
,
793 bgp_process(bgp_path
, rn
, afi
, safi
);
797 int valid_nexthops
= bgp_isvalid_nexthop(bnc
);
800 peer
->last_reset
= PEER_DOWN_WAITING_OPEN
;
802 peer
->last_reset
= PEER_DOWN_WAITING_NHT
;
804 if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
)) {
805 if (BGP_DEBUG(nht
, NHT
))
807 "%s: Updating peer (%s(%s)) status with NHT",
808 __func__
, peer
->host
,
809 peer
->bgp
->name_pretty
);
810 bgp_fsm_event_update(peer
, valid_nexthops
);
811 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
815 RESET_FLAG(bnc
->change_flags
);
819 * path_nh_map - make or break path-to-nexthop association.
821 * path - pointer to the path structure
822 * bnc - pointer to the nexthop structure
823 * make - if set, make the association. if unset, just break the existing
826 void path_nh_map(struct bgp_path_info
*path
, struct bgp_nexthop_cache
*bnc
,
830 LIST_REMOVE(path
, nh_thread
);
831 path
->nexthop
->path_count
--;
832 path
->nexthop
= NULL
;
835 LIST_INSERT_HEAD(&(bnc
->paths
), path
, nh_thread
);
837 path
->nexthop
->path_count
++;
842 * This function is called to register nexthops to zebra
843 * as that we may have tried to install the nexthops
844 * before we actually have a zebra connection
846 void bgp_nht_register_nexthops(struct bgp
*bgp
)
849 struct bgp_nexthop_cache
*bnc
;
852 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
853 if (!bgp
->nexthop_cache_table
[afi
])
856 for (rn
= bgp_table_top(bgp
->nexthop_cache_table
[afi
]); rn
;
857 rn
= bgp_route_next(rn
)) {
858 bnc
= bgp_node_get_bgp_nexthop_info(rn
);
863 register_zebra_rnh(bnc
, 0);
868 void bgp_nht_register_enhe_capability_interfaces(struct peer
*peer
)
872 struct bgp_nexthop_cache
*bnc
;
873 struct nexthop
*nhop
;
874 struct interface
*ifp
;
882 if (!bgp
->nexthop_cache_table
[AFI_IP6
])
885 if (!sockunion2hostprefix(&peer
->su
, &p
)) {
886 if (BGP_DEBUG(nht
, NHT
))
887 zlog_debug("%s: Unable to convert prefix to sockunion",
892 if (p
.family
!= AF_INET6
)
894 rn
= bgp_node_lookup(bgp
->nexthop_cache_table
[AFI_IP6
], &p
);
898 bnc
= bgp_node_get_bgp_nexthop_info(rn
);
902 if (peer
!= bnc
->nht_info
)
905 for (nhop
= bnc
->nexthop
; nhop
; nhop
= nhop
->next
) {
906 ifp
= if_lookup_by_index(nhop
->ifindex
, nhop
->vrf_id
);
911 zclient_send_interface_radv_req(zclient
,
914 BGP_UNNUM_DEFAULT_RA_INTERVAL
);