1 /* Zebra next hop tracking code
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
29 #include "sockunion.h"
32 #include "workqueue.h"
39 #include "zebra/zebra_router.h"
40 #include "zebra/rib.h"
42 #include "zebra/zserv.h"
43 #include "zebra/zebra_ns.h"
44 #include "zebra/zebra_vrf.h"
45 #include "zebra/redistribute.h"
46 #include "zebra/debug.h"
47 #include "zebra/zebra_rnh.h"
48 #include "zebra/zebra_routemap.h"
49 #include "zebra/zebra_srte.h"
50 #include "zebra/interface.h"
51 #include "zebra/zebra_errors.h"
53 DEFINE_MTYPE_STATIC(ZEBRA
, RNH
, "Nexthop tracking object");
55 /* UI controls whether to notify about changes that only involve backup
56 * nexthops. Default is to notify all changes.
58 static bool rnh_hide_backups
;
60 static void free_state(vrf_id_t vrf_id
, struct route_entry
*re
,
61 struct route_node
*rn
);
62 static void copy_state(struct rnh
*rnh
, const struct route_entry
*re
,
63 struct route_node
*rn
);
64 static bool compare_state(struct route_entry
*r1
, struct route_entry
*r2
);
65 static void print_rnh(struct route_node
*rn
, struct vty
*vty
);
66 static int zebra_client_cleanup_rnh(struct zserv
*client
);
68 void zebra_rnh_init(void)
70 hook_register(zserv_client_close
, zebra_client_cleanup_rnh
);
73 static inline struct route_table
*get_rnh_table(vrf_id_t vrfid
, afi_t afi
,
76 struct zebra_vrf
*zvrf
;
77 struct route_table
*t
= NULL
;
79 zvrf
= zebra_vrf_lookup_by_id(vrfid
);
82 case RNH_NEXTHOP_TYPE
:
83 t
= zvrf
->rnh_table
[afi
];
85 case RNH_IMPORT_CHECK_TYPE
:
86 t
= zvrf
->import_check_table
[afi
];
93 static void zebra_rnh_remove_from_routing_table(struct rnh
*rnh
)
95 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(rnh
->vrf_id
);
96 struct route_table
*table
= zvrf
->table
[rnh
->afi
][SAFI_UNICAST
];
97 struct route_node
*rn
;
103 rn
= route_node_match(table
, &rnh
->resolved_route
);
107 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
108 zlog_debug("%s: %s(%u):%pRN removed from tracking on %pRN",
109 __func__
, VRF_LOGNAME(zvrf
->vrf
), rnh
->vrf_id
,
112 dest
= rib_dest_from_rnode(rn
);
113 rnh_list_del(&dest
->nht
, rnh
);
114 route_unlock_node(rn
);
117 static void zebra_rnh_store_in_routing_table(struct rnh
*rnh
)
119 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(rnh
->vrf_id
);
120 struct route_table
*table
= zvrf
->table
[rnh
->afi
][SAFI_UNICAST
];
121 struct route_node
*rn
;
124 rn
= route_node_match(table
, &rnh
->resolved_route
);
128 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
129 zlog_debug("%s: %s(%u):%pRN added for tracking on %pRN",
130 __func__
, VRF_LOGNAME(zvrf
->vrf
), rnh
->vrf_id
,
133 dest
= rib_dest_from_rnode(rn
);
134 rnh_list_add_tail(&dest
->nht
, rnh
);
135 route_unlock_node(rn
);
138 struct rnh
*zebra_add_rnh(struct prefix
*p
, vrf_id_t vrfid
, enum rnh_type type
,
141 struct route_table
*table
;
142 struct route_node
*rn
;
143 struct rnh
*rnh
= NULL
;
144 afi_t afi
= family2afi(p
->family
);
146 if (IS_ZEBRA_DEBUG_NHT
) {
147 struct vrf
*vrf
= vrf_lookup_by_id(vrfid
);
149 zlog_debug("%s(%u): Add RNH %pFX type %s", VRF_LOGNAME(vrf
),
150 vrfid
, p
, rnh_type2str(type
));
152 table
= get_rnh_table(vrfid
, afi
, type
);
154 struct vrf
*vrf
= vrf_lookup_by_id(vrfid
);
156 flog_warn(EC_ZEBRA_RNH_NO_TABLE
,
157 "%s(%u): Add RNH %pFX type %s - table not found",
158 VRF_LOGNAME(vrf
), vrfid
, p
, rnh_type2str(type
));
163 /* Make it sure prefixlen is applied to the prefix. */
166 /* Lookup (or add) route node.*/
167 rn
= route_node_get(table
, p
);
170 rnh
= XCALLOC(MTYPE_RNH
, sizeof(struct rnh
));
173 * The resolved route is already 0.0.0.0/0 or
174 * 0::0/0 due to the calloc right above, but
175 * we should set the family so that future
176 * comparisons can just be done
178 rnh
->resolved_route
.family
= p
->family
;
179 rnh
->client_list
= list_new();
184 rnh
->zebra_pseudowire_list
= list_new();
190 zebra_rnh_store_in_routing_table(rnh
);
194 route_unlock_node(rn
);
198 struct rnh
*zebra_lookup_rnh(struct prefix
*p
, vrf_id_t vrfid
,
201 struct route_table
*table
;
202 struct route_node
*rn
;
204 table
= get_rnh_table(vrfid
, family2afi(PREFIX_FAMILY(p
)), type
);
208 /* Make it sure prefixlen is applied to the prefix. */
211 /* Lookup route node.*/
212 rn
= route_node_lookup(table
, p
);
216 route_unlock_node(rn
);
220 void zebra_free_rnh(struct rnh
*rnh
)
222 struct zebra_vrf
*zvrf
;
223 struct route_table
*table
;
225 zebra_rnh_remove_from_routing_table(rnh
);
226 rnh
->flags
|= ZEBRA_NHT_DELETED
;
227 list_delete(&rnh
->client_list
);
228 list_delete(&rnh
->zebra_pseudowire_list
);
230 zvrf
= zebra_vrf_lookup_by_id(rnh
->vrf_id
);
231 table
= zvrf
->table
[family2afi(rnh
->resolved_route
.family
)][SAFI_UNICAST
];
234 struct route_node
*rern
;
236 rern
= route_node_match(table
, &rnh
->resolved_route
);
240 route_unlock_node(rern
);
242 dest
= rib_dest_from_rnode(rern
);
243 rnh_list_del(&dest
->nht
, rnh
);
246 free_state(rnh
->vrf_id
, rnh
->state
, rnh
->node
);
247 XFREE(MTYPE_RNH
, rnh
);
250 static void zebra_delete_rnh(struct rnh
*rnh
, enum rnh_type type
)
252 struct route_node
*rn
;
254 if (!list_isempty(rnh
->client_list
)
255 || !list_isempty(rnh
->zebra_pseudowire_list
))
258 if ((rnh
->flags
& ZEBRA_NHT_DELETED
) || !(rn
= rnh
->node
))
261 if (IS_ZEBRA_DEBUG_NHT
) {
262 struct vrf
*vrf
= vrf_lookup_by_id(rnh
->vrf_id
);
264 zlog_debug("%s(%u): Del RNH %pRN type %s", VRF_LOGNAME(vrf
),
265 rnh
->vrf_id
, rnh
->node
, rnh_type2str(type
));
270 route_unlock_node(rn
);
274 * This code will send to the registering client
276 * For a rnh that was created, there is no data
277 * so it will send an empty nexthop group
278 * If rnh exists then we know it has been evaluated
279 * and as such it will have a resolved rnh.
281 void zebra_add_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
282 enum rnh_type type
, vrf_id_t vrf_id
)
284 if (IS_ZEBRA_DEBUG_NHT
) {
285 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
287 zlog_debug("%s(%u): Client %s registers for RNH %pRN type %s",
288 VRF_LOGNAME(vrf
), vrf_id
,
289 zebra_route_string(client
->proto
), rnh
->node
,
292 if (!listnode_lookup(rnh
->client_list
, client
))
293 listnode_add(rnh
->client_list
, client
);
296 * We always need to respond with known information,
297 * currently multiple daemons expect this behavior
299 zebra_send_rnh_update(rnh
, client
, type
, vrf_id
, 0);
302 void zebra_remove_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
305 if (IS_ZEBRA_DEBUG_NHT
) {
306 struct vrf
*vrf
= vrf_lookup_by_id(rnh
->vrf_id
);
308 zlog_debug("Client %s unregisters for RNH %s(%u)%pRN type %s",
309 zebra_route_string(client
->proto
), VRF_LOGNAME(vrf
),
310 vrf
->vrf_id
, rnh
->node
, rnh_type2str(type
));
312 listnode_delete(rnh
->client_list
, client
);
313 zebra_delete_rnh(rnh
, type
);
316 /* XXX move this utility function elsewhere? */
317 static void addr2hostprefix(int af
, const union g_addr
*addr
,
318 struct prefix
*prefix
)
322 prefix
->family
= AF_INET
;
323 prefix
->prefixlen
= IPV4_MAX_BITLEN
;
324 prefix
->u
.prefix4
= addr
->ipv4
;
327 prefix
->family
= AF_INET6
;
328 prefix
->prefixlen
= IPV6_MAX_BITLEN
;
329 prefix
->u
.prefix6
= addr
->ipv6
;
332 memset(prefix
, 0, sizeof(*prefix
));
333 zlog_warn("%s: unknown address family %d", __func__
, af
);
338 void zebra_register_rnh_pseudowire(vrf_id_t vrf_id
, struct zebra_pw
*pw
,
344 struct zebra_vrf
*zvrf
;
348 zvrf
= vrf_info_lookup(vrf_id
);
352 addr2hostprefix(pw
->af
, &pw
->nexthop
, &nh
);
353 rnh
= zebra_add_rnh(&nh
, vrf_id
, RNH_NEXTHOP_TYPE
, &exists
);
357 if (!listnode_lookup(rnh
->zebra_pseudowire_list
, pw
)) {
358 listnode_add(rnh
->zebra_pseudowire_list
, pw
);
360 zebra_evaluate_rnh(zvrf
, family2afi(pw
->af
), 1,
361 RNH_NEXTHOP_TYPE
, &nh
);
366 void zebra_deregister_rnh_pseudowire(vrf_id_t vrf_id
, struct zebra_pw
*pw
)
374 listnode_delete(rnh
->zebra_pseudowire_list
, pw
);
377 zebra_delete_rnh(rnh
, RNH_NEXTHOP_TYPE
);
380 /* Clear the NEXTHOP_FLAG_RNH_FILTERED flags on all nexthops
382 static void zebra_rnh_clear_nexthop_rnh_filters(struct route_entry
*re
)
384 struct nexthop
*nexthop
;
387 for (nexthop
= re
->nhe
->nhg
.nexthop
; nexthop
;
388 nexthop
= nexthop
->next
) {
389 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RNH_FILTERED
);
394 /* Apply the NHT route-map for a client to the route (and nexthops)
397 static int zebra_rnh_apply_nht_rmap(afi_t afi
, struct zebra_vrf
*zvrf
,
398 struct route_node
*prn
,
399 struct route_entry
*re
, int proto
)
401 int at_least_one
= 0;
402 struct nexthop
*nexthop
;
403 route_map_result_t ret
;
406 for (nexthop
= re
->nhe
->nhg
.nexthop
; nexthop
;
407 nexthop
= nexthop
->next
) {
408 ret
= zebra_nht_route_map_check(
409 afi
, proto
, &prn
->p
, zvrf
, re
, nexthop
);
410 if (ret
!= RMAP_DENYMATCH
)
411 at_least_one
++; /* at least one valid NH */
413 SET_FLAG(nexthop
->flags
,
414 NEXTHOP_FLAG_RNH_FILTERED
);
418 return (at_least_one
);
422 * Determine appropriate route (RE entry) resolving a tracked BGP route
423 * for BGP route for import.
425 static struct route_entry
*
426 zebra_rnh_resolve_import_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
427 struct route_node
*nrn
, struct rnh
*rnh
,
428 struct route_node
**prn
)
430 struct route_table
*route_table
;
431 struct route_node
*rn
;
432 struct route_entry
*re
;
436 route_table
= zvrf
->table
[afi
][SAFI_UNICAST
];
437 if (!route_table
) // unexpected
440 rn
= route_node_match(route_table
, &nrn
->p
);
444 /* Unlock route node - we don't need to lock when walking the tree. */
445 route_unlock_node(rn
);
447 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
)
448 && !prefix_same(&nrn
->p
, &rn
->p
))
451 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
452 zlog_debug("%s: %s(%u):%pRN Resolved Import Entry to %pRN",
453 __func__
, VRF_LOGNAME(zvrf
->vrf
), rnh
->vrf_id
,
457 /* Identify appropriate route entry. */
458 RNODE_FOREACH_RE (rn
, re
) {
459 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
460 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
461 && !CHECK_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
)
462 && (re
->type
!= ZEBRA_ROUTE_BGP
))
469 if (!re
&& IS_ZEBRA_DEBUG_NHT_DETAILED
)
470 zlog_debug(" Rejected due to removed or is a bgp route");
476 * See if a tracked route entry for import (by BGP) has undergone any
477 * change, and if so, notify the client.
479 static void zebra_rnh_eval_import_check_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
480 int force
, struct route_node
*nrn
,
482 struct route_node
*prn
,
483 struct route_entry
*re
)
485 int state_changed
= 0;
486 struct zserv
*client
;
487 struct listnode
*node
;
489 zebra_rnh_remove_from_routing_table(rnh
);
491 prefix_copy(&rnh
->resolved_route
, &prn
->p
);
493 int family
= rnh
->resolved_route
.family
;
495 memset(&rnh
->resolved_route
.family
, 0, sizeof(struct prefix
));
496 rnh
->resolved_route
.family
= family
;
498 zebra_rnh_store_in_routing_table(rnh
);
500 if (re
&& (rnh
->state
== NULL
)) {
501 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
503 } else if (!re
&& (rnh
->state
!= NULL
))
506 if (compare_state(re
, rnh
->state
)) {
507 copy_state(rnh
, re
, nrn
);
511 if (state_changed
|| force
) {
512 if (IS_ZEBRA_DEBUG_NHT
)
513 zlog_debug("%s(%u):%pRN: Route import check %s %s",
514 VRF_LOGNAME(zvrf
->vrf
), zvrf
->vrf
->vrf_id
,
515 nrn
, rnh
->state
? "passed" : "failed",
516 state_changed
? "(state changed)" : "");
517 /* state changed, notify clients */
518 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
519 zebra_send_rnh_update(rnh
, client
,
520 RNH_IMPORT_CHECK_TYPE
,
521 zvrf
->vrf
->vrf_id
, 0);
527 * Notify clients registered for this nexthop about a change.
529 static void zebra_rnh_notify_protocol_clients(struct zebra_vrf
*zvrf
, afi_t afi
,
530 struct route_node
*nrn
,
532 struct route_node
*prn
,
533 struct route_entry
*re
)
535 struct listnode
*node
;
536 struct zserv
*client
;
537 int num_resolving_nh
;
539 if (IS_ZEBRA_DEBUG_NHT
) {
541 zlog_debug("%s(%u):%pRN: NH resolved over route %pRN",
542 VRF_LOGNAME(zvrf
->vrf
), zvrf
->vrf
->vrf_id
,
545 zlog_debug("%s(%u):%pRN: NH has become unresolved",
546 VRF_LOGNAME(zvrf
->vrf
), zvrf
->vrf
->vrf_id
,
550 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
552 /* Apply route-map for this client to route resolving
554 * nexthop to see if it is filtered or not.
556 zebra_rnh_clear_nexthop_rnh_filters(re
);
557 num_resolving_nh
= zebra_rnh_apply_nht_rmap(
558 afi
, zvrf
, prn
, re
, client
->proto
);
559 if (num_resolving_nh
)
560 rnh
->filtered
[client
->proto
] = 0;
562 rnh
->filtered
[client
->proto
] = 1;
564 if (IS_ZEBRA_DEBUG_NHT
)
566 "%s(%u):%pRN: Notifying client %s about NH %s",
567 VRF_LOGNAME(zvrf
->vrf
),
568 zvrf
->vrf
->vrf_id
, nrn
,
569 zebra_route_string(client
->proto
),
572 : "(filtered by route-map)");
574 rnh
->filtered
[client
->proto
] = 0;
575 if (IS_ZEBRA_DEBUG_NHT
)
577 "%s(%u):%pRN: Notifying client %s about NH (unreachable)",
578 VRF_LOGNAME(zvrf
->vrf
),
579 zvrf
->vrf
->vrf_id
, nrn
,
580 zebra_route_string(client
->proto
));
583 zebra_send_rnh_update(rnh
, client
, RNH_NEXTHOP_TYPE
,
584 zvrf
->vrf
->vrf_id
, 0);
588 zebra_rnh_clear_nexthop_rnh_filters(re
);
592 * Utility to determine whether a candidate nexthop is useable. We make this
593 * check in a couple of places, so this is a single home for the logic we
597 static const int RNH_INVALID_NH_FLAGS
= (NEXTHOP_FLAG_RECURSIVE
|
598 NEXTHOP_FLAG_DUPLICATE
|
599 NEXTHOP_FLAG_RNH_FILTERED
);
601 bool rnh_nexthop_valid(const struct route_entry
*re
, const struct nexthop
*nh
)
603 return (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
604 && CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ACTIVE
)
605 && !CHECK_FLAG(nh
->flags
, RNH_INVALID_NH_FLAGS
));
609 * Determine whether an re's nexthops are valid for tracking.
611 static bool rnh_check_re_nexthops(const struct route_entry
*re
,
612 const struct rnh
*rnh
)
615 const struct nexthop
*nexthop
= NULL
;
617 /* Check route's nexthops */
618 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
)) {
619 if (rnh_nexthop_valid(re
, nexthop
))
623 /* Check backup nexthops, if any. */
624 if (nexthop
== NULL
&& re
->nhe
->backup_info
&&
625 re
->nhe
->backup_info
->nhe
) {
626 for (ALL_NEXTHOPS(re
->nhe
->backup_info
->nhe
->nhg
, nexthop
)) {
627 if (rnh_nexthop_valid(re
, nexthop
))
632 if (nexthop
== NULL
) {
633 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
635 " Route Entry %s no nexthops",
636 zebra_route_string(re
->type
));
641 /* Some special checks if registration asked for them. */
642 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)) {
643 if ((re
->type
== ZEBRA_ROUTE_CONNECT
)
644 || (re
->type
== ZEBRA_ROUTE_STATIC
))
646 if (re
->type
== ZEBRA_ROUTE_NHRP
) {
648 for (nexthop
= re
->nhe
->nhg
.nexthop
;
650 nexthop
= nexthop
->next
)
651 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
665 * Determine appropriate route (route entry) resolving a tracked
668 static struct route_entry
*
669 zebra_rnh_resolve_nexthop_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
670 struct route_node
*nrn
, const struct rnh
*rnh
,
671 struct route_node
**prn
)
673 struct route_table
*route_table
;
674 struct route_node
*rn
;
675 struct route_entry
*re
;
679 route_table
= zvrf
->table
[afi
][SAFI_UNICAST
];
683 rn
= route_node_match(route_table
, &nrn
->p
);
687 /* Unlock route node - we don't need to lock when walking the tree. */
688 route_unlock_node(rn
);
690 /* While resolving nexthops, we may need to walk up the tree from the
691 * most-specific match. Do similar logic as in zebra_rib.c
694 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
695 zlog_debug("%s: %s(%u):%pRN Possible Match to %pRN",
696 __func__
, VRF_LOGNAME(zvrf
->vrf
),
697 rnh
->vrf_id
, rnh
->node
, rn
);
699 /* Do not resolve over default route unless allowed &&
700 * match route to be exact if so specified
702 if (is_default_prefix(&rn
->p
)
703 && !rnh_resolve_via_default(zvrf
, rn
->p
.family
)) {
704 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
706 " Not allowed to resolve through default prefix");
710 /* Identify appropriate route entry. */
711 RNODE_FOREACH_RE (rn
, re
) {
712 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
713 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
715 " Route Entry %s removed",
716 zebra_route_string(re
->type
));
719 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
) &&
720 !CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
721 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
723 " Route Entry %s !selected",
724 zebra_route_string(re
->type
));
728 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
)) {
729 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
731 " Route Entry %s queued",
732 zebra_route_string(re
->type
));
736 /* Just being SELECTED isn't quite enough - must
737 * have an installed nexthop to be useful.
739 if (rnh_check_re_nexthops(re
, rnh
))
743 /* Route entry found, we're done; else, walk up the tree. */
749 if (!CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
752 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
754 " Nexthop must be connected, cannot recurse up");
762 static void zebra_rnh_process_pseudowires(vrf_id_t vrfid
, struct rnh
*rnh
)
765 struct listnode
*node
;
767 for (ALL_LIST_ELEMENTS_RO(rnh
->zebra_pseudowire_list
, node
, pw
))
772 * See if a tracked nexthop entry has undergone any change, and if so,
773 * take appropriate action; this involves notifying any clients and/or
774 * scheduling dependent static routes for processing.
776 static void zebra_rnh_eval_nexthop_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
777 int force
, struct route_node
*nrn
,
779 struct route_node
*prn
,
780 struct route_entry
*re
)
782 int state_changed
= 0;
784 /* If we're resolving over a different route, resolution has changed or
785 * the resolving route has some change (e.g., metric), there is a state
788 zebra_rnh_remove_from_routing_table(rnh
);
789 if (!prefix_same(&rnh
->resolved_route
, prn
? &prn
->p
: NULL
)) {
791 prefix_copy(&rnh
->resolved_route
, &prn
->p
);
794 * Just quickly store the family of the resolved
795 * route so that we can reset it in a second here
797 int family
= rnh
->resolved_route
.family
;
799 memset(&rnh
->resolved_route
, 0, sizeof(struct prefix
));
800 rnh
->resolved_route
.family
= family
;
803 copy_state(rnh
, re
, nrn
);
805 } else if (compare_state(re
, rnh
->state
)) {
806 copy_state(rnh
, re
, nrn
);
809 zebra_rnh_store_in_routing_table(rnh
);
811 if (state_changed
|| force
) {
812 /* NOTE: Use the "copy" of resolving route stored in 'rnh' i.e.,
815 /* Notify registered protocol clients. */
816 zebra_rnh_notify_protocol_clients(zvrf
, afi
, nrn
, rnh
, prn
,
819 /* Process pseudowires attached to this nexthop */
820 zebra_rnh_process_pseudowires(zvrf
->vrf
->vrf_id
, rnh
);
824 /* Evaluate one tracked entry */
825 static void zebra_rnh_evaluate_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
826 int force
, enum rnh_type type
,
827 struct route_node
*nrn
)
830 struct route_entry
*re
;
831 struct route_node
*prn
;
833 if (IS_ZEBRA_DEBUG_NHT
) {
834 zlog_debug("%s(%u):%pRN: Evaluate RNH, type %s %s",
835 VRF_LOGNAME(zvrf
->vrf
), zvrf
->vrf
->vrf_id
, nrn
,
836 rnh_type2str(type
), force
? "(force)" : "");
841 /* Identify route entry (RE) resolving this tracked entry. */
842 if (type
== RNH_IMPORT_CHECK_TYPE
)
843 re
= zebra_rnh_resolve_import_entry(zvrf
, afi
, nrn
, rnh
, &prn
);
845 re
= zebra_rnh_resolve_nexthop_entry(zvrf
, afi
, nrn
, rnh
, &prn
);
847 /* If the entry cannot be resolved and that is also the existing state,
848 * there is nothing further to do.
850 if (!re
&& rnh
->state
== NULL
&& !force
)
853 /* Process based on type of entry. */
854 if (type
== RNH_IMPORT_CHECK_TYPE
)
855 zebra_rnh_eval_import_check_entry(zvrf
, afi
, force
, nrn
, rnh
,
858 zebra_rnh_eval_nexthop_entry(zvrf
, afi
, force
, nrn
, rnh
, prn
,
863 * Clear the ROUTE_ENTRY_NEXTHOPS_CHANGED flag
864 * from the re entries.
866 * Please note we are doing this *after* we have
867 * notified the world about each nexthop as that
868 * we can have a situation where one re entry
869 * covers multiple nexthops we are interested in.
871 static void zebra_rnh_clear_nhc_flag(struct zebra_vrf
*zvrf
, afi_t afi
,
872 enum rnh_type type
, struct route_node
*nrn
)
875 struct route_entry
*re
;
876 struct route_node
*prn
;
880 /* Identify route entry (RIB) resolving this tracked entry. */
881 if (type
== RNH_IMPORT_CHECK_TYPE
)
882 re
= zebra_rnh_resolve_import_entry(zvrf
, afi
, nrn
, rnh
,
885 re
= zebra_rnh_resolve_nexthop_entry(zvrf
, afi
, nrn
, rnh
,
889 UNSET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
892 /* Evaluate all tracked entries (nexthops or routes for import into BGP)
893 * of a particular VRF and address-family or a specific prefix.
895 void zebra_evaluate_rnh(struct zebra_vrf
*zvrf
, afi_t afi
, int force
,
896 enum rnh_type type
, struct prefix
*p
)
898 struct route_table
*rnh_table
;
899 struct route_node
*nrn
;
901 rnh_table
= get_rnh_table(zvrf
->vrf
->vrf_id
, afi
, type
);
902 if (!rnh_table
) // unexpected
906 /* Evaluating a specific entry, make sure it exists. */
907 nrn
= route_node_lookup(rnh_table
, p
);
908 if (nrn
&& nrn
->info
)
909 zebra_rnh_evaluate_entry(zvrf
, afi
, force
, type
, nrn
);
912 route_unlock_node(nrn
);
914 /* Evaluate entire table. */
915 nrn
= route_top(rnh_table
);
918 zebra_rnh_evaluate_entry(zvrf
, afi
, force
, type
,
920 nrn
= route_next(nrn
); /* this will also unlock nrn */
922 nrn
= route_top(rnh_table
);
925 zebra_rnh_clear_nhc_flag(zvrf
, afi
, type
, nrn
);
926 nrn
= route_next(nrn
); /* this will also unlock nrn */
931 void zebra_print_rnh_table(vrf_id_t vrfid
, afi_t afi
, struct vty
*vty
,
932 enum rnh_type type
, struct prefix
*p
)
934 struct route_table
*table
;
935 struct route_node
*rn
;
937 table
= get_rnh_table(vrfid
, afi
, type
);
939 if (IS_ZEBRA_DEBUG_NHT
)
940 zlog_debug("print_rnhs: rnh table not found");
944 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
945 if (p
&& !prefix_match(&rn
->p
, p
))
954 * free_state - free up the re structure associated with the rnh.
956 static void free_state(vrf_id_t vrf_id
, struct route_entry
*re
,
957 struct route_node
*rn
)
962 /* free RE and nexthops */
963 zebra_nhg_free(re
->nhe
);
967 static void copy_state(struct rnh
*rnh
, const struct route_entry
*re
,
968 struct route_node
*rn
)
970 struct route_entry
*state
;
973 free_state(rnh
->vrf_id
, rnh
->state
, rn
);
980 state
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
981 state
->type
= re
->type
;
982 state
->distance
= re
->distance
;
983 state
->metric
= re
->metric
;
984 state
->vrf_id
= re
->vrf_id
;
985 state
->status
= re
->status
;
987 state
->nhe
= zebra_nhe_copy(re
->nhe
, 0);
989 /* Copy the 'fib' nexthops also, if present - we want to capture
990 * the true installed nexthops.
992 if (re
->fib_ng
.nexthop
)
993 nexthop_group_copy(&state
->fib_ng
, &re
->fib_ng
);
994 if (re
->fib_backup_ng
.nexthop
)
995 nexthop_group_copy(&state
->fib_backup_ng
, &re
->fib_backup_ng
);
1001 * Locate the next primary nexthop, used when comparing current rnh info with
1004 static struct nexthop
*next_valid_primary_nh(struct route_entry
*re
,
1007 struct nexthop_group
*nhg
;
1008 struct nexthop
*bnh
;
1010 bool default_path
= true;
1012 /* Fib backup ng present: some backups are installed,
1013 * and we're configured for special handling if there are backups.
1015 if (rnh_hide_backups
&& (re
->fib_backup_ng
.nexthop
!= NULL
))
1016 default_path
= false;
1018 /* Default path: no special handling, just using the 'installed'
1019 * primary nexthops and the common validity test.
1023 nhg
= rib_get_fib_nhg(re
);
1026 nh
= nexthop_next(nh
);
1029 if (rnh_nexthop_valid(re
, nh
))
1032 nh
= nexthop_next(nh
);
1038 /* Hide backup activation/switchover events.
1040 * If we've had a switchover, an inactive primary won't be in
1041 * the fib list at all - the 'fib' list could even be empty
1042 * in the case where no primary is installed. But we want to consider
1043 * those primaries "valid" if they have an activated backup nh.
1045 * The logic is something like:
1047 * // then all primaries are installed
1049 * for each primary in re nhg
1051 * primary is installed
1052 * else if a backup is installed
1053 * primary counts as installed
1055 * primary !installed
1058 /* Start with the first primary */
1060 nh
= re
->nhe
->nhg
.nexthop
;
1062 nh
= nexthop_next(nh
);
1066 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1067 zlog_debug("%s: checking primary NH %pNHv",
1070 /* If this nexthop is in the fib list, it's installed */
1071 nhg
= rib_get_fib_nhg(re
);
1073 for (bnh
= nhg
->nexthop
; bnh
; bnh
= nexthop_next(bnh
)) {
1074 if (nexthop_cmp(nh
, bnh
) == 0)
1079 /* Found the match */
1080 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1081 zlog_debug("%s: NH in fib list", __func__
);
1085 /* Else if this nexthop's backup is installed, it counts */
1086 nhg
= rib_get_fib_backup_nhg(re
);
1089 for (idx
= 0; bnh
!= NULL
; idx
++) {
1090 /* If we find an active backup nh for this
1091 * primary, we're done;
1093 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1094 zlog_debug("%s: checking backup %pNHv [%d]",
1095 __func__
, bnh
, idx
);
1097 if (!CHECK_FLAG(bnh
->flags
, NEXTHOP_FLAG_ACTIVE
))
1100 for (i
= 0; i
< nh
->backup_num
; i
++) {
1101 /* Found a matching activated backup nh */
1102 if (nh
->backup_idx
[i
] == idx
) {
1103 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1104 zlog_debug("%s: backup %d activated",
1111 /* Note that we're not recursing here if the
1112 * backups are recursive: the primary's index is
1113 * only valid in the top-level backup list.
1118 /* Try the next primary nexthop */
1119 nh
= nexthop_next(nh
);
1128 * Compare two route_entries' nexthops. Account for backup nexthops
1129 * and for the 'fib' nexthop lists, if present.
1131 static bool compare_valid_nexthops(struct route_entry
*r1
,
1132 struct route_entry
*r2
)
1134 bool matched_p
= false;
1135 struct nexthop_group
*nhg1
, *nhg2
;
1136 struct nexthop
*nh1
, *nh2
;
1138 /* Start with the primary nexthops */
1140 nh1
= next_valid_primary_nh(r1
, NULL
);
1141 nh2
= next_valid_primary_nh(r2
, NULL
);
1144 /* Find any differences in the nexthop lists */
1147 /* Any difference is a no-match */
1148 if (nexthop_cmp(nh1
, nh2
) != 0) {
1149 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1150 zlog_debug("%s: nh1: %pNHv, nh2: %pNHv differ",
1151 __func__
, nh1
, nh2
);
1155 } else if (nh1
|| nh2
) {
1156 /* One list has more valid nexthops than the other */
1157 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1158 zlog_debug("%s: nh1 %s, nh2 %s", __func__
,
1159 nh1
? "non-NULL" : "NULL",
1160 nh2
? "non-NULL" : "NULL");
1163 break; /* Done with both lists */
1165 nh1
= next_valid_primary_nh(r1
, nh1
);
1166 nh2
= next_valid_primary_nh(r2
, nh2
);
1169 /* If configured, don't compare installed backup state - we've
1170 * accounted for that with the primaries above.
1172 * But we do want to compare the routes' backup info,
1173 * in case the owning route has changed the backups -
1174 * that change we do want to report.
1176 if (rnh_hide_backups
) {
1177 uint32_t hash1
= 0, hash2
= 0;
1179 if (r1
->nhe
->backup_info
)
1180 hash1
= nexthop_group_hash(
1181 &r1
->nhe
->backup_info
->nhe
->nhg
);
1183 if (r2
->nhe
->backup_info
)
1184 hash2
= nexthop_group_hash(
1185 &r2
->nhe
->backup_info
->nhe
->nhg
);
1187 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1188 zlog_debug("%s: backup hash1 %#x, hash2 %#x",
1189 __func__
, hash1
, hash2
);
1197 /* The test for the backups is slightly different: the only installed
1198 * backups will be in the 'fib' list.
1200 nhg1
= rib_get_fib_backup_nhg(r1
);
1201 nhg2
= rib_get_fib_backup_nhg(r2
);
1203 nh1
= nhg1
->nexthop
;
1204 nh2
= nhg2
->nexthop
;
1207 /* Find each backup list's next valid nexthop */
1208 while ((nh1
!= NULL
) && !rnh_nexthop_valid(r1
, nh1
))
1209 nh1
= nexthop_next(nh1
);
1211 while ((nh2
!= NULL
) && !rnh_nexthop_valid(r2
, nh2
))
1212 nh2
= nexthop_next(nh2
);
1215 /* Any difference is a no-match */
1216 if (nexthop_cmp(nh1
, nh2
) != 0) {
1217 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1218 zlog_debug("%s: backup nh1: %pNHv, nh2: %pNHv differ",
1219 __func__
, nh1
, nh2
);
1223 nh1
= nexthop_next(nh1
);
1224 nh2
= nexthop_next(nh2
);
1225 } else if (nh1
|| nh2
) {
1226 /* One list has more valid nexthops than the other */
1227 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1228 zlog_debug("%s: backup nh1 %s, nh2 %s",
1230 nh1
? "non-NULL" : "NULL",
1231 nh2
? "non-NULL" : "NULL");
1234 break; /* Done with both lists */
1239 /* Well, it's a match */
1244 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1245 zlog_debug("%s: %smatched",
1246 __func__
, (matched_p
? "" : "NOT "));
1251 /* Returns 'false' if no difference. */
1252 static bool compare_state(struct route_entry
*r1
,
1253 struct route_entry
*r2
)
1258 if ((!r1
&& r2
) || (r1
&& !r2
))
1261 if (r1
->distance
!= r2
->distance
)
1264 if (r1
->metric
!= r2
->metric
)
1267 if (!compare_valid_nexthops(r1
, r2
))
1273 int zebra_send_rnh_update(struct rnh
*rnh
, struct zserv
*client
,
1274 enum rnh_type type
, vrf_id_t vrf_id
,
1275 uint32_t srte_color
)
1277 struct stream
*s
= NULL
;
1278 struct route_entry
*re
;
1282 struct route_node
*rn
;
1284 uint32_t message
= 0;
1285 int cmd
= (type
== RNH_IMPORT_CHECK_TYPE
) ? ZEBRA_IMPORT_CHECK_UPDATE
1286 : ZEBRA_NEXTHOP_UPDATE
;
1291 /* Get output stream. */
1292 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1294 zclient_create_header(s
, cmd
, vrf_id
);
1296 /* Message flags. */
1298 SET_FLAG(message
, ZAPI_MESSAGE_SRTE
);
1299 stream_putl(s
, message
);
1301 stream_putw(s
, rn
->p
.family
);
1302 switch (rn
->p
.family
) {
1304 stream_putc(s
, rn
->p
.prefixlen
);
1305 stream_put_in_addr(s
, &rn
->p
.u
.prefix4
);
1308 stream_putc(s
, rn
->p
.prefixlen
);
1309 stream_put(s
, &rn
->p
.u
.prefix6
, IPV6_MAX_BYTELEN
);
1312 flog_err(EC_ZEBRA_RNH_UNKNOWN_FAMILY
,
1313 "%s: Unknown family (%d) notification attempted",
1314 __func__
, rn
->p
.family
);
1318 stream_putl(s
, srte_color
);
1321 struct zapi_nexthop znh
;
1322 struct nexthop_group
*nhg
;
1324 stream_putc(s
, re
->type
);
1325 stream_putw(s
, re
->instance
);
1326 stream_putc(s
, re
->distance
);
1327 stream_putl(s
, re
->metric
);
1329 nump
= stream_get_endp(s
);
1332 nhg
= rib_get_fib_nhg(re
);
1333 for (ALL_NEXTHOPS_PTR(nhg
, nh
))
1334 if (rnh_nexthop_valid(re
, nh
)) {
1335 zapi_nexthop_from_nexthop(&znh
, nh
);
1336 ret
= zapi_nexthop_encode(s
, &znh
, 0, message
);
1343 nhg
= rib_get_fib_backup_nhg(re
);
1345 for (ALL_NEXTHOPS_PTR(nhg
, nh
))
1346 if (rnh_nexthop_valid(re
, nh
)) {
1347 zapi_nexthop_from_nexthop(&znh
, nh
);
1348 ret
= zapi_nexthop_encode(
1349 s
, &znh
, 0 /* flags */,
1358 stream_putc_at(s
, nump
, num
);
1360 stream_putc(s
, 0); // type
1361 stream_putw(s
, 0); // instance
1362 stream_putc(s
, 0); // distance
1363 stream_putl(s
, 0); // metric
1364 stream_putc(s
, 0); // nexthops
1366 stream_putw_at(s
, 0, stream_get_endp(s
));
1368 client
->nh_last_upd_time
= monotime(NULL
);
1369 return zserv_send_message(client
, s
);
1377 static void print_nh(struct nexthop
*nexthop
, struct vty
*vty
)
1380 struct zebra_ns
*zns
= zebra_ns_lookup(nexthop
->vrf_id
);
1382 switch (nexthop
->type
) {
1383 case NEXTHOP_TYPE_IPV4
:
1384 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1385 vty_out(vty
, " via %pI4", &nexthop
->gate
.ipv4
);
1386 if (nexthop
->ifindex
)
1387 vty_out(vty
, ", %s",
1388 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
1390 case NEXTHOP_TYPE_IPV6
:
1391 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1393 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1394 if (nexthop
->ifindex
)
1395 vty_out(vty
, ", via %s",
1396 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
1398 case NEXTHOP_TYPE_IFINDEX
:
1399 vty_out(vty
, " is directly connected, %s",
1400 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
1402 case NEXTHOP_TYPE_BLACKHOLE
:
1403 vty_out(vty
, " is directly connected, Null0");
1411 static void print_rnh(struct route_node
*rn
, struct vty
*vty
)
1414 struct nexthop
*nexthop
;
1415 struct listnode
*node
;
1416 struct zserv
*client
;
1420 vty_out(vty
, "%s%s\n",
1421 inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
1422 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
) ? "(Connected)"
1425 vty_out(vty
, " resolved via %s\n",
1426 zebra_route_string(rnh
->state
->type
));
1427 for (nexthop
= rnh
->state
->nhe
->nhg
.nexthop
; nexthop
;
1428 nexthop
= nexthop
->next
)
1429 print_nh(nexthop
, vty
);
1431 vty_out(vty
, " unresolved%s\n",
1432 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)
1436 vty_out(vty
, " Client list:");
1437 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
))
1438 vty_out(vty
, " %s(fd %d)%s", zebra_route_string(client
->proto
),
1440 rnh
->filtered
[client
->proto
] ? "(filtered)" : "");
1441 if (!list_isempty(rnh
->zebra_pseudowire_list
))
1442 vty_out(vty
, " zebra[pseudowires]");
1446 static int zebra_cleanup_rnh_client(vrf_id_t vrf_id
, afi_t afi
,
1447 struct zserv
*client
, enum rnh_type type
)
1449 struct route_table
*ntable
;
1450 struct route_node
*nrn
;
1453 if (IS_ZEBRA_DEBUG_NHT
) {
1454 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
1457 "%s(%u): Client %s RNH cleanup for family %s type %s",
1458 VRF_LOGNAME(vrf
), vrf_id
,
1459 zebra_route_string(client
->proto
), afi2str(afi
),
1460 rnh_type2str(type
));
1463 ntable
= get_rnh_table(vrf_id
, afi
, type
);
1465 zlog_debug("cleanup_rnh_client: rnh table not found");
1469 for (nrn
= route_top(ntable
); nrn
; nrn
= route_next(nrn
)) {
1474 zebra_remove_rnh_client(rnh
, client
, type
);
1479 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
1480 static int zebra_client_cleanup_rnh(struct zserv
*client
)
1483 struct zebra_vrf
*zvrf
;
1485 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1488 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AFI_IP
, client
,
1490 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AFI_IP6
, client
,
1492 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AFI_IP
, client
,
1493 RNH_IMPORT_CHECK_TYPE
);
1494 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AFI_IP6
, client
,
1495 RNH_IMPORT_CHECK_TYPE
);
1502 int rnh_resolve_via_default(struct zebra_vrf
*zvrf
, int family
)
1504 if (((family
== AF_INET
) && zvrf
->zebra_rnh_ip_default_route
)
1505 || ((family
== AF_INET6
) && zvrf
->zebra_rnh_ipv6_default_route
))
1512 * UI control to avoid notifications if backup nexthop status changes
1514 void rnh_set_hide_backups(bool hide_p
)
1516 rnh_hide_backups
= hide_p
;
1519 bool rnh_get_hide_backups(void)
1521 return rnh_hide_backups
;