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"
47 extern struct zclient
*zclient
;
49 static void register_zebra_rnh(struct bgp_nexthop_cache
*bnc
,
50 int is_bgp_static_route
);
51 static void unregister_zebra_rnh(struct bgp_nexthop_cache
*bnc
,
52 int is_bgp_static_route
);
53 static void evaluate_paths(struct bgp_nexthop_cache
*bnc
);
54 static int make_prefix(int afi
, struct bgp_path_info
*pi
, struct prefix
*p
);
56 static int bgp_isvalid_nexthop(struct bgp_nexthop_cache
*bnc
)
58 return (bgp_zebra_num_connects() == 0
59 || (bnc
&& CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
)));
62 static int bgp_isvalid_labeled_nexthop(struct bgp_nexthop_cache
*bnc
)
64 return (bgp_zebra_num_connects() == 0
65 || (bnc
&& CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_LABELED_VALID
)));
68 int bgp_find_nexthop(struct bgp_path_info
*path
, int connected
)
70 struct bgp_nexthop_cache
*bnc
= path
->nexthop
;
76 * We are cheating here. Views have no associated underlying
77 * ability to detect nexthops. So when we have a view
78 * just tell everyone the nexthop is valid
80 if (path
->peer
&& path
->peer
->bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
83 if (connected
&& !(CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
)))
86 return (bgp_isvalid_nexthop(bnc
));
89 static void bgp_unlink_nexthop_check(struct bgp_nexthop_cache
*bnc
)
91 if (LIST_EMPTY(&(bnc
->paths
)) && !bnc
->nht_info
) {
92 if (BGP_DEBUG(nht
, NHT
)) {
93 char buf
[PREFIX2STR_BUFFER
];
94 zlog_debug("bgp_unlink_nexthop: freeing bnc %s",
95 bnc_str(bnc
, buf
, PREFIX2STR_BUFFER
));
97 unregister_zebra_rnh(bnc
,
98 CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE
));
99 bgp_node_set_bgp_nexthop_info(bnc
->node
, NULL
);
100 bgp_unlock_node(bnc
->node
);
106 void bgp_unlink_nexthop(struct bgp_path_info
*path
)
108 struct bgp_nexthop_cache
*bnc
= path
->nexthop
;
113 path_nh_map(path
, NULL
, false);
115 bgp_unlink_nexthop_check(bnc
);
118 void bgp_unlink_nexthop_by_peer(struct peer
*peer
)
122 struct bgp_nexthop_cache
*bnc
;
123 afi_t afi
= family2afi(peer
->su
.sa
.sa_family
);
125 if (!sockunion2hostprefix(&peer
->su
, &p
))
128 rn
= bgp_node_get(peer
->bgp
->nexthop_cache_table
[afi
], &p
);
130 bnc
= bgp_node_get_bgp_nexthop_info(rn
);
134 /* cleanup the peer reference */
135 bnc
->nht_info
= NULL
;
137 bgp_unlink_nexthop_check(bnc
);
141 * A route and its nexthop might belong to different VRFs. Therefore,
142 * we need both the bgp_route and bgp_nexthop pointers.
144 int bgp_find_or_add_nexthop(struct bgp
*bgp_route
, struct bgp
*bgp_nexthop
,
145 afi_t afi
, struct bgp_path_info
*pi
,
146 struct peer
*peer
, int connected
)
149 struct bgp_nexthop_cache
*bnc
;
151 int is_bgp_static_route
= 0;
154 is_bgp_static_route
= ((pi
->type
== ZEBRA_ROUTE_BGP
)
155 && (pi
->sub_type
== BGP_ROUTE_STATIC
))
159 /* Since Extended Next-hop Encoding (RFC5549) support, we want
161 address-family from the next-hop. */
162 if (!is_bgp_static_route
)
163 afi
= BGP_ATTR_NEXTHOP_AFI_IP6(pi
->attr
) ? AFI_IP6
166 /* This will return TRUE if the global IPv6 NH is a link local
168 if (make_prefix(afi
, pi
, &p
) < 0)
171 if (!sockunion2hostprefix(&peer
->su
, &p
)) {
172 if (BGP_DEBUG(nht
, NHT
)) {
174 "%s: Attempting to register with unknown AFI %d (not %d or %d)",
175 __FUNCTION__
, afi
, AFI_IP
, AFI_IP6
);
182 if (is_bgp_static_route
)
183 rn
= bgp_node_get(bgp_nexthop
->import_check_table
[afi
], &p
);
185 rn
= bgp_node_get(bgp_nexthop
->nexthop_cache_table
[afi
], &p
);
187 bnc
= bgp_node_get_bgp_nexthop_info(rn
);
190 bgp_node_set_bgp_nexthop_info(rn
, bnc
);
192 bnc
->bgp
= bgp_nexthop
;
194 if (BGP_DEBUG(nht
, NHT
)) {
195 char buf
[PREFIX2STR_BUFFER
];
197 zlog_debug("Allocated bnc %s peer %p",
198 bnc_str(bnc
, buf
, PREFIX2STR_BUFFER
), peer
);
203 if (is_bgp_static_route
) {
204 SET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE
);
206 /* If we're toggling the type, re-register */
207 if ((bgp_flag_check(bgp_route
, BGP_FLAG_IMPORT_CHECK
))
208 && !CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
)) {
209 SET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
);
210 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
211 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
212 } else if ((!bgp_flag_check(bgp_route
, BGP_FLAG_IMPORT_CHECK
))
213 && CHECK_FLAG(bnc
->flags
,
214 BGP_STATIC_ROUTE_EXACT_MATCH
)) {
215 UNSET_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
);
216 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
217 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
220 /* When nexthop is already known, but now requires 'connected'
222 * re-register it. The reverse scenario where the nexthop currently
224 * 'connected' resolution does not need a re-register (i.e., we treat
225 * 'connected-required' as an override) except in the scenario where
227 * is actually a case of tracking a peer for connectivity (e.g., after
228 * disable connected-check).
229 * NOTE: We don't track the number of paths separately for 'connected-
230 * required' vs 'connected-not-required' as this change is not a common
233 else if (connected
&& !CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
)) {
234 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
);
235 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
236 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
237 } else if (peer
&& !connected
238 && CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
)) {
239 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
);
240 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
241 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
243 if (bgp_route
->inst_type
== BGP_INSTANCE_TYPE_VIEW
) {
244 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
245 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
246 } else if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
) &&
247 !is_default_host_route(&bnc
->node
->p
))
248 register_zebra_rnh(bnc
, is_bgp_static_route
);
250 if (pi
&& pi
->nexthop
!= bnc
) {
251 /* Unlink from existing nexthop cache, if any. This will also
253 * the nexthop cache entry, if appropriate.
255 bgp_unlink_nexthop(pi
);
257 /* updates NHT pi list reference */
258 path_nh_map(pi
, bnc
, true);
260 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
) && bnc
->metric
)
261 (bgp_path_info_extra_get(pi
))->igpmetric
= bnc
->metric
;
263 pi
->extra
->igpmetric
= 0;
265 bnc
->nht_info
= (void *)peer
; /* NHT peer reference */
268 * We are cheating here. Views have no associated underlying
269 * ability to detect nexthops. So when we have a view
270 * just tell everyone the nexthop is valid
272 if (bgp_route
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
275 return (bgp_isvalid_nexthop(bnc
));
278 void bgp_delete_connected_nexthop(afi_t afi
, struct peer
*peer
)
281 struct bgp_nexthop_cache
*bnc
;
287 if (!sockunion2hostprefix(&peer
->su
, &p
))
290 rn
= bgp_node_lookup(
291 peer
->bgp
->nexthop_cache_table
[family2afi(p
.family
)], &p
);
293 if (BGP_DEBUG(nht
, NHT
))
294 zlog_debug("Cannot find connected NHT node for peer %s",
299 bnc
= bgp_node_get_bgp_nexthop_info(rn
);
301 if (BGP_DEBUG(nht
, NHT
))
302 zlog_debug("Cannot find connected NHT node for peer %s on route_node as expected",
309 if (bnc
->nht_info
!= peer
) {
310 if (BGP_DEBUG(nht
, NHT
))
312 "Connected NHT %p node for peer %s points to %p",
313 bnc
, peer
->host
, bnc
->nht_info
);
317 bnc
->nht_info
= NULL
;
319 if (LIST_EMPTY(&(bnc
->paths
))) {
320 if (BGP_DEBUG(nht
, NHT
))
321 zlog_debug("Freeing connected NHT node %p for peer %s",
323 unregister_zebra_rnh(bnc
, 0);
324 bgp_node_set_bgp_nexthop_info(bnc
->node
, NULL
);
325 bgp_unlock_node(bnc
->node
);
330 void bgp_parse_nexthop_update(int command
, vrf_id_t vrf_id
)
332 struct bgp_node
*rn
= NULL
;
333 struct bgp_nexthop_cache
*bnc
;
334 struct nexthop
*nexthop
;
335 struct nexthop
*oldnh
;
336 struct nexthop
*nhlist_head
= NULL
;
337 struct nexthop
*nhlist_tail
= NULL
;
340 struct zapi_route nhr
;
342 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
346 "parse nexthop update: instance not found for vrf_id %u",
351 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &nhr
)) {
352 if (BGP_DEBUG(nht
, NHT
))
353 zlog_debug("%s: Failure to decode nexthop update",
354 __PRETTY_FUNCTION__
);
358 if (command
== ZEBRA_NEXTHOP_UPDATE
)
359 rn
= bgp_node_lookup(
360 bgp
->nexthop_cache_table
[family2afi(nhr
.prefix
.family
)],
362 else if (command
== ZEBRA_IMPORT_CHECK_UPDATE
)
363 rn
= bgp_node_lookup(
364 bgp
->import_check_table
[family2afi(nhr
.prefix
.family
)],
368 if (BGP_DEBUG(nht
, NHT
)) {
369 char buf
[PREFIX2STR_BUFFER
];
370 prefix2str(&nhr
.prefix
, buf
, sizeof(buf
));
371 zlog_debug("parse nexthop update(%s): rn not found",
377 bnc
= bgp_node_get_bgp_nexthop_info(rn
);
379 if (BGP_DEBUG(nht
, NHT
)) {
380 char buf
[PREFIX2STR_BUFFER
];
382 prefix2str(&nhr
.prefix
, buf
, sizeof(buf
));
383 zlog_debug("parse nexthop update(%s): bnc node info not found",
391 bnc
->last_update
= bgp_clock();
392 bnc
->change_flags
= 0;
394 /* debug print the input */
395 if (BGP_DEBUG(nht
, NHT
)) {
396 char buf
[PREFIX2STR_BUFFER
];
397 prefix2str(&nhr
.prefix
, buf
, sizeof(buf
));
399 "%u: Rcvd NH update %s - metric %d/%d #nhops %d/%d flags 0x%x",
400 vrf_id
, buf
, nhr
.metric
, bnc
->metric
, nhr
.nexthop_num
,
401 bnc
->nexthop_num
, bnc
->flags
);
404 if (nhr
.metric
!= bnc
->metric
)
405 bnc
->change_flags
|= BGP_NEXTHOP_METRIC_CHANGED
;
407 if (nhr
.nexthop_num
!= bnc
->nexthop_num
)
408 bnc
->change_flags
|= BGP_NEXTHOP_CHANGED
;
410 if (nhr
.nexthop_num
) {
411 struct peer
*peer
= bnc
->nht_info
;
413 /* notify bgp fsm if nbr ip goes from invalid->valid */
414 if (!bnc
->nexthop_num
)
415 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
417 bnc
->flags
|= BGP_NEXTHOP_VALID
;
418 bnc
->metric
= nhr
.metric
;
419 bnc
->nexthop_num
= nhr
.nexthop_num
;
421 bnc
->flags
&= ~BGP_NEXTHOP_LABELED_VALID
; /* check below */
423 for (i
= 0; i
< nhr
.nexthop_num
; i
++) {
426 nexthop
= nexthop_from_zapi_nexthop(&nhr
.nexthops
[i
]);
429 * Turn on RA for the v6 nexthops
430 * we receive from bgp. This is to allow us
431 * to work with v4 routing over v6 nexthops
433 if (peer
&& !peer
->ifp
434 && CHECK_FLAG(peer
->flags
,
435 PEER_FLAG_CAPABILITY_ENHE
)
436 && nhr
.prefix
.family
== AF_INET6
) {
437 struct interface
*ifp
;
439 ifp
= if_lookup_by_index(nexthop
->ifindex
,
441 zclient_send_interface_radv_req(
442 zclient
, nexthop
->vrf_id
, ifp
, true,
443 BGP_UNNUM_DEFAULT_RA_INTERVAL
);
445 /* There is at least one label-switched path */
446 if (nexthop
->nh_label
&&
447 nexthop
->nh_label
->num_labels
) {
449 bnc
->flags
|= BGP_NEXTHOP_LABELED_VALID
;
450 num_labels
= nexthop
->nh_label
->num_labels
;
453 if (BGP_DEBUG(nht
, NHT
)) {
454 char buf
[NEXTHOP_STRLEN
];
456 " nhop via %s (%d labels)",
457 nexthop2str(nexthop
, buf
, sizeof(buf
)),
462 nhlist_tail
->next
= nexthop
;
463 nhlist_tail
= nexthop
;
465 nhlist_tail
= nexthop
;
466 nhlist_head
= nexthop
;
469 /* No need to evaluate the nexthop if we have already
471 * that there has been a change.
473 if (bnc
->change_flags
& BGP_NEXTHOP_CHANGED
)
476 for (oldnh
= bnc
->nexthop
; oldnh
; oldnh
= oldnh
->next
)
477 if (nexthop_same_no_recurse(oldnh
, nexthop
) &&
478 nexthop_labels_match(oldnh
, nexthop
))
482 bnc
->change_flags
|= BGP_NEXTHOP_CHANGED
;
484 bnc_nexthop_free(bnc
);
485 bnc
->nexthop
= nhlist_head
;
487 bnc
->flags
&= ~BGP_NEXTHOP_VALID
;
488 bnc
->nexthop_num
= nhr
.nexthop_num
;
490 /* notify bgp fsm if nbr ip goes from valid->invalid */
491 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
493 bnc_nexthop_free(bnc
);
501 * Cleanup nexthop registration and status information for BGP nexthops
502 * pertaining to this VRF. This is invoked upon VRF deletion.
504 void bgp_cleanup_nexthops(struct bgp
*bgp
)
508 struct bgp_nexthop_cache
*bnc
;
510 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
511 if (!bgp
->nexthop_cache_table
[afi
])
514 for (rn
= bgp_table_top(bgp
->nexthop_cache_table
[afi
]); rn
;
515 rn
= bgp_route_next(rn
)) {
516 bnc
= bgp_node_get_bgp_nexthop_info(rn
);
520 /* Clear relevant flags. */
521 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
);
522 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
523 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
529 * make_prefix - make a prefix structure from the path (essentially
532 static int make_prefix(int afi
, struct bgp_path_info
*pi
, struct prefix
*p
)
535 int is_bgp_static
= ((pi
->type
== ZEBRA_ROUTE_BGP
)
536 && (pi
->sub_type
== BGP_ROUTE_STATIC
))
539 struct bgp_node
*net
= pi
->net
;
540 struct prefix
*p_orig
= &net
->p
;
542 if (p_orig
->family
== AF_FLOWSPEC
) {
545 return bgp_flowspec_get_first_nh(pi
->peer
->bgp
,
548 memset(p
, 0, sizeof(struct prefix
));
553 p
->u
.prefix4
= pi
->net
->p
.u
.prefix4
;
554 p
->prefixlen
= pi
->net
->p
.prefixlen
;
556 p
->u
.prefix4
= pi
->attr
->nexthop
;
557 p
->prefixlen
= IPV4_MAX_BITLEN
;
561 p
->family
= AF_INET6
;
564 p
->u
.prefix6
= pi
->net
->p
.u
.prefix6
;
565 p
->prefixlen
= pi
->net
->p
.prefixlen
;
567 p
->u
.prefix6
= pi
->attr
->mp_nexthop_global
;
568 p
->prefixlen
= IPV6_MAX_BITLEN
;
572 if (BGP_DEBUG(nht
, NHT
)) {
574 "%s: Attempting to make prefix with unknown AFI %d (not %d or %d)",
575 __FUNCTION__
, afi
, AFI_IP
, AFI_IP6
);
583 * sendmsg_zebra_rnh -- Format and send a nexthop register/Unregister
586 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
587 * int command -- command to send to zebra
591 static void sendmsg_zebra_rnh(struct bgp_nexthop_cache
*bnc
, int command
)
594 bool exact_match
= false;
600 /* Don't try to register if Zebra doesn't know of this instance. */
601 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bnc
->bgp
)) {
602 if (BGP_DEBUG(zebra
, ZEBRA
))
603 zlog_debug("%s: No zebra instance to talk to, not installing NHT entry",
604 __PRETTY_FUNCTION__
);
608 if (!bgp_zebra_num_connects()) {
609 if (BGP_DEBUG(zebra
, ZEBRA
))
610 zlog_debug("%s: We have not connected yet, cannot send nexthops",
611 __PRETTY_FUNCTION__
);
614 if ((command
== ZEBRA_NEXTHOP_REGISTER
615 || command
== ZEBRA_IMPORT_ROUTE_REGISTER
)
616 && (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
)
617 || CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
)))
620 if (BGP_DEBUG(zebra
, ZEBRA
)) {
621 char buf
[PREFIX2STR_BUFFER
];
623 prefix2str(p
, buf
, PREFIX2STR_BUFFER
);
624 zlog_debug("%s: sending cmd %s for %s (vrf %s)",
625 __func__
, zserv_command_string(command
), buf
,
629 ret
= zclient_send_rnh(zclient
, command
, p
, exact_match
,
631 /* TBD: handle the failure */
633 flog_warn(EC_BGP_ZEBRA_SEND
,
634 "sendmsg_nexthop: zclient_send_message() failed");
636 if ((command
== ZEBRA_NEXTHOP_REGISTER
)
637 || (command
== ZEBRA_IMPORT_ROUTE_REGISTER
))
638 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
639 else if ((command
== ZEBRA_NEXTHOP_UNREGISTER
)
640 || (command
== ZEBRA_IMPORT_ROUTE_UNREGISTER
))
641 UNSET_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
);
646 * register_zebra_rnh - register a NH/route with Zebra for notification
647 * when the route or the route to the nexthop changes.
649 * struct bgp_nexthop_cache *bnc
653 static void register_zebra_rnh(struct bgp_nexthop_cache
*bnc
,
654 int is_bgp_import_route
)
656 /* Check if we have already registered */
657 if (bnc
->flags
& BGP_NEXTHOP_REGISTERED
)
659 if (is_bgp_import_route
)
660 sendmsg_zebra_rnh(bnc
, ZEBRA_IMPORT_ROUTE_REGISTER
);
662 sendmsg_zebra_rnh(bnc
, ZEBRA_NEXTHOP_REGISTER
);
666 * unregister_zebra_rnh -- Unregister the route/nexthop from Zebra.
668 * struct bgp_nexthop_cache *bnc
672 static void unregister_zebra_rnh(struct bgp_nexthop_cache
*bnc
,
673 int is_bgp_import_route
)
675 /* Check if we have already registered */
676 if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
))
679 if (is_bgp_import_route
)
680 sendmsg_zebra_rnh(bnc
, ZEBRA_IMPORT_ROUTE_UNREGISTER
);
682 sendmsg_zebra_rnh(bnc
, ZEBRA_NEXTHOP_UNREGISTER
);
686 * evaluate_paths - Evaluate the paths/nets associated with a nexthop.
688 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
692 static void evaluate_paths(struct bgp_nexthop_cache
*bnc
)
695 struct bgp_path_info
*path
;
697 struct peer
*peer
= (struct peer
*)bnc
->nht_info
;
698 struct bgp_table
*table
;
700 struct bgp
*bgp_path
;
702 if (BGP_DEBUG(nht
, NHT
)) {
703 char buf
[PREFIX2STR_BUFFER
];
704 bnc_str(bnc
, buf
, PREFIX2STR_BUFFER
);
706 "NH update for %s - flags 0x%x chgflags 0x%x - evaluate paths",
707 buf
, bnc
->flags
, bnc
->change_flags
);
710 LIST_FOREACH (path
, &(bnc
->paths
), nh_thread
) {
711 if (!(path
->type
== ZEBRA_ROUTE_BGP
712 && ((path
->sub_type
== BGP_ROUTE_NORMAL
)
713 || (path
->sub_type
== BGP_ROUTE_STATIC
)
714 || (path
->sub_type
== BGP_ROUTE_IMPORTED
))))
718 assert(rn
&& bgp_node_table(rn
));
719 afi
= family2afi(rn
->p
.family
);
720 table
= bgp_node_table(rn
);
724 * handle routes from other VRFs (they can have a
725 * nexthop in THIS VRF). bgp_path is the bgp instance
726 * that owns the route referencing this nexthop.
728 bgp_path
= table
->bgp
;
731 * Path becomes valid/invalid depending on whether the nexthop
732 * reachable/unreachable.
734 * In case of unicast routes that were imported from vpn
735 * and that have labels, they are valid only if there are
736 * nexthops with labels
739 int bnc_is_valid_nexthop
= 0;
741 if (safi
== SAFI_UNICAST
&&
742 path
->sub_type
== BGP_ROUTE_IMPORTED
&&
744 path
->extra
->num_labels
) {
746 bnc_is_valid_nexthop
=
747 bgp_isvalid_labeled_nexthop(bnc
) ? 1 : 0;
749 bnc_is_valid_nexthop
=
750 bgp_isvalid_nexthop(bnc
) ? 1 : 0;
753 if (BGP_DEBUG(nht
, NHT
)) {
754 char buf
[PREFIX_STRLEN
];
756 prefix2str(&rn
->p
, buf
, PREFIX_STRLEN
);
757 zlog_debug("%s: prefix %s (vrf %s) %svalid",
758 __func__
, buf
, bgp_path
->name
,
759 (bnc_is_valid_nexthop
? "" : "not "));
762 if ((CHECK_FLAG(path
->flags
, BGP_PATH_VALID
) ? 1 : 0)
763 != bnc_is_valid_nexthop
) {
764 if (CHECK_FLAG(path
->flags
, BGP_PATH_VALID
)) {
765 bgp_aggregate_decrement(bgp_path
, &rn
->p
,
767 bgp_path_info_unset_flag(rn
, path
,
770 bgp_path_info_set_flag(rn
, path
,
772 bgp_aggregate_increment(bgp_path
, &rn
->p
,
777 /* Copy the metric to the path. Will be used for bestpath
779 if (bgp_isvalid_nexthop(bnc
) && bnc
->metric
)
780 (bgp_path_info_extra_get(path
))->igpmetric
=
782 else if (path
->extra
)
783 path
->extra
->igpmetric
= 0;
785 if (CHECK_FLAG(bnc
->change_flags
, BGP_NEXTHOP_METRIC_CHANGED
)
786 || CHECK_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
))
787 SET_FLAG(path
->flags
, BGP_PATH_IGP_CHANGED
);
789 bgp_process(bgp_path
, rn
, afi
, safi
);
792 if (peer
&& !CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
)) {
793 if (BGP_DEBUG(nht
, NHT
))
794 zlog_debug("%s: Updating peer (%s) status with NHT",
795 __FUNCTION__
, peer
->host
);
796 bgp_fsm_event_update(peer
, bgp_isvalid_nexthop(bnc
));
797 SET_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
);
800 RESET_FLAG(bnc
->change_flags
);
804 * path_nh_map - make or break path-to-nexthop association.
806 * path - pointer to the path structure
807 * bnc - pointer to the nexthop structure
808 * make - if set, make the association. if unset, just break the existing
811 void path_nh_map(struct bgp_path_info
*path
, struct bgp_nexthop_cache
*bnc
,
815 LIST_REMOVE(path
, nh_thread
);
816 path
->nexthop
->path_count
--;
817 path
->nexthop
= NULL
;
820 LIST_INSERT_HEAD(&(bnc
->paths
), path
, nh_thread
);
822 path
->nexthop
->path_count
++;
827 * This function is called to register nexthops to zebra
828 * as that we may have tried to install the nexthops
829 * before we actually have a zebra connection
831 void bgp_nht_register_nexthops(struct bgp
*bgp
)
834 struct bgp_nexthop_cache
*bnc
;
837 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
838 if (!bgp
->nexthop_cache_table
[afi
])
841 for (rn
= bgp_table_top(bgp
->nexthop_cache_table
[afi
]); rn
;
842 rn
= bgp_route_next(rn
)) {
843 bnc
= bgp_node_get_bgp_nexthop_info(rn
);
848 register_zebra_rnh(bnc
, 0);
853 void bgp_nht_register_enhe_capability_interfaces(struct peer
*peer
)
857 struct bgp_nexthop_cache
*bnc
;
858 struct nexthop
*nhop
;
859 struct interface
*ifp
;
867 if (!bgp
->nexthop_cache_table
[AFI_IP6
])
870 if (!sockunion2hostprefix(&peer
->su
, &p
)) {
871 if (BGP_DEBUG(nht
, NHT
))
872 zlog_debug("%s: Unable to convert prefix to sockunion",
873 __PRETTY_FUNCTION__
);
877 if (p
.family
!= AF_INET6
)
879 rn
= bgp_node_lookup(bgp
->nexthop_cache_table
[AFI_IP6
], &p
);
883 bnc
= bgp_node_get_bgp_nexthop_info(rn
);
887 if (peer
!= bnc
->nht_info
)
890 for (nhop
= bnc
->nexthop
; nhop
; nhop
= nhop
->next
) {
891 ifp
= if_lookup_by_index(nhop
->ifindex
,
893 zclient_send_interface_radv_req(zclient
,
896 BGP_UNNUM_DEFAULT_RA_INTERVAL
);