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 static void free_state(vrf_id_t vrf_id
, struct route_entry
*re
,
56 struct route_node
*rn
);
57 static void copy_state(struct rnh
*rnh
, const struct route_entry
*re
,
58 struct route_node
*rn
);
59 static int compare_state(struct route_entry
*r1
, struct route_entry
*r2
);
60 static void print_rnh(struct route_node
*rn
, struct vty
*vty
);
61 static int zebra_client_cleanup_rnh(struct zserv
*client
);
63 void zebra_rnh_init(void)
65 hook_register(zserv_client_close
, zebra_client_cleanup_rnh
);
68 static inline struct route_table
*get_rnh_table(vrf_id_t vrfid
, afi_t afi
,
71 struct zebra_vrf
*zvrf
;
72 struct route_table
*t
= NULL
;
74 zvrf
= zebra_vrf_lookup_by_id(vrfid
);
77 case RNH_NEXTHOP_TYPE
:
78 t
= zvrf
->rnh_table
[afi
];
80 case RNH_IMPORT_CHECK_TYPE
:
81 t
= zvrf
->import_check_table
[afi
];
88 static void zebra_rnh_remove_from_routing_table(struct rnh
*rnh
)
90 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(rnh
->vrf_id
);
91 struct route_table
*table
= zvrf
->table
[rnh
->afi
][SAFI_UNICAST
];
92 struct route_node
*rn
;
98 rn
= route_node_match(table
, &rnh
->resolved_route
);
102 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
103 zlog_debug("%s: %s(%u):%pRN removed from tracking on %pRN",
104 __func__
, VRF_LOGNAME(zvrf
->vrf
), rnh
->vrf_id
,
107 dest
= rib_dest_from_rnode(rn
);
108 rnh_list_del(&dest
->nht
, rnh
);
109 route_unlock_node(rn
);
112 static void zebra_rnh_store_in_routing_table(struct rnh
*rnh
)
114 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(rnh
->vrf_id
);
115 struct route_table
*table
= zvrf
->table
[rnh
->afi
][SAFI_UNICAST
];
116 struct route_node
*rn
;
119 rn
= route_node_match(table
, &rnh
->resolved_route
);
123 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
124 zlog_debug("%s: %s(%u):%pRN added for tracking on %pRN",
125 __func__
, VRF_LOGNAME(zvrf
->vrf
), rnh
->vrf_id
,
128 dest
= rib_dest_from_rnode(rn
);
129 rnh_list_add_tail(&dest
->nht
, rnh
);
130 route_unlock_node(rn
);
133 struct rnh
*zebra_add_rnh(struct prefix
*p
, vrf_id_t vrfid
, enum rnh_type type
,
136 struct route_table
*table
;
137 struct route_node
*rn
;
138 struct rnh
*rnh
= NULL
;
139 afi_t afi
= family2afi(p
->family
);
141 if (IS_ZEBRA_DEBUG_NHT
) {
142 struct vrf
*vrf
= vrf_lookup_by_id(vrfid
);
144 zlog_debug("%s(%u): Add RNH %pFX type %s", VRF_LOGNAME(vrf
),
145 vrfid
, p
, rnh_type2str(type
));
147 table
= get_rnh_table(vrfid
, afi
, type
);
149 struct vrf
*vrf
= vrf_lookup_by_id(vrfid
);
151 flog_warn(EC_ZEBRA_RNH_NO_TABLE
,
152 "%s(%u): Add RNH %pFX type %s - table not found",
153 VRF_LOGNAME(vrf
), vrfid
, p
, rnh_type2str(type
));
158 /* Make it sure prefixlen is applied to the prefix. */
161 /* Lookup (or add) route node.*/
162 rn
= route_node_get(table
, p
);
165 rnh
= XCALLOC(MTYPE_RNH
, sizeof(struct rnh
));
168 * The resolved route is already 0.0.0.0/0 or
169 * 0::0/0 due to the calloc right above, but
170 * we should set the family so that future
171 * comparisons can just be done
173 rnh
->resolved_route
.family
= p
->family
;
174 rnh
->client_list
= list_new();
179 rnh
->zebra_pseudowire_list
= list_new();
185 zebra_rnh_store_in_routing_table(rnh
);
189 route_unlock_node(rn
);
193 struct rnh
*zebra_lookup_rnh(struct prefix
*p
, vrf_id_t vrfid
,
196 struct route_table
*table
;
197 struct route_node
*rn
;
199 table
= get_rnh_table(vrfid
, family2afi(PREFIX_FAMILY(p
)), type
);
203 /* Make it sure prefixlen is applied to the prefix. */
206 /* Lookup route node.*/
207 rn
= route_node_lookup(table
, p
);
211 route_unlock_node(rn
);
215 void zebra_free_rnh(struct rnh
*rnh
)
217 struct zebra_vrf
*zvrf
;
218 struct route_table
*table
;
220 zebra_rnh_remove_from_routing_table(rnh
);
221 rnh
->flags
|= ZEBRA_NHT_DELETED
;
222 list_delete(&rnh
->client_list
);
223 list_delete(&rnh
->zebra_pseudowire_list
);
225 zvrf
= zebra_vrf_lookup_by_id(rnh
->vrf_id
);
226 table
= zvrf
->table
[family2afi(rnh
->resolved_route
.family
)][SAFI_UNICAST
];
229 struct route_node
*rern
;
231 rern
= route_node_match(table
, &rnh
->resolved_route
);
235 route_unlock_node(rern
);
237 dest
= rib_dest_from_rnode(rern
);
238 rnh_list_del(&dest
->nht
, rnh
);
241 free_state(rnh
->vrf_id
, rnh
->state
, rnh
->node
);
242 XFREE(MTYPE_RNH
, rnh
);
245 static void zebra_delete_rnh(struct rnh
*rnh
, enum rnh_type type
)
247 struct route_node
*rn
;
249 if (!list_isempty(rnh
->client_list
)
250 || !list_isempty(rnh
->zebra_pseudowire_list
))
253 if ((rnh
->flags
& ZEBRA_NHT_DELETED
) || !(rn
= rnh
->node
))
256 if (IS_ZEBRA_DEBUG_NHT
) {
257 struct vrf
*vrf
= vrf_lookup_by_id(rnh
->vrf_id
);
259 zlog_debug("%s(%u): Del RNH %pRN type %s", VRF_LOGNAME(vrf
),
260 rnh
->vrf_id
, rnh
->node
, rnh_type2str(type
));
265 route_unlock_node(rn
);
269 * This code will send to the registering client
271 * For a rnh that was created, there is no data
272 * so it will send an empty nexthop group
273 * If rnh exists then we know it has been evaluated
274 * and as such it will have a resolved rnh.
276 void zebra_add_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
277 enum rnh_type type
, vrf_id_t vrf_id
)
279 if (IS_ZEBRA_DEBUG_NHT
) {
280 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
282 zlog_debug("%s(%u): Client %s registers for RNH %pRN type %s",
283 VRF_LOGNAME(vrf
), vrf_id
,
284 zebra_route_string(client
->proto
), rnh
->node
,
287 if (!listnode_lookup(rnh
->client_list
, client
))
288 listnode_add(rnh
->client_list
, client
);
291 * We always need to respond with known information,
292 * currently multiple daemons expect this behavior
294 zebra_send_rnh_update(rnh
, client
, type
, vrf_id
, 0);
297 void zebra_remove_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
300 if (IS_ZEBRA_DEBUG_NHT
) {
301 struct vrf
*vrf
= vrf_lookup_by_id(rnh
->vrf_id
);
303 zlog_debug("Client %s unregisters for RNH %s(%u)%pRN type %s",
304 zebra_route_string(client
->proto
), VRF_LOGNAME(vrf
),
305 vrf
->vrf_id
, rnh
->node
, rnh_type2str(type
));
307 listnode_delete(rnh
->client_list
, client
);
308 zebra_delete_rnh(rnh
, type
);
311 /* XXX move this utility function elsewhere? */
312 static void addr2hostprefix(int af
, const union g_addr
*addr
,
313 struct prefix
*prefix
)
317 prefix
->family
= AF_INET
;
318 prefix
->prefixlen
= IPV4_MAX_BITLEN
;
319 prefix
->u
.prefix4
= addr
->ipv4
;
322 prefix
->family
= AF_INET6
;
323 prefix
->prefixlen
= IPV6_MAX_BITLEN
;
324 prefix
->u
.prefix6
= addr
->ipv6
;
327 memset(prefix
, 0, sizeof(*prefix
));
328 zlog_warn("%s: unknown address family %d", __func__
, af
);
333 void zebra_register_rnh_pseudowire(vrf_id_t vrf_id
, struct zebra_pw
*pw
,
339 struct zebra_vrf
*zvrf
;
343 zvrf
= vrf_info_lookup(vrf_id
);
347 addr2hostprefix(pw
->af
, &pw
->nexthop
, &nh
);
348 rnh
= zebra_add_rnh(&nh
, vrf_id
, RNH_NEXTHOP_TYPE
, &exists
);
352 if (!listnode_lookup(rnh
->zebra_pseudowire_list
, pw
)) {
353 listnode_add(rnh
->zebra_pseudowire_list
, pw
);
355 zebra_evaluate_rnh(zvrf
, family2afi(pw
->af
), 1,
356 RNH_NEXTHOP_TYPE
, &nh
);
361 void zebra_deregister_rnh_pseudowire(vrf_id_t vrf_id
, struct zebra_pw
*pw
)
369 listnode_delete(rnh
->zebra_pseudowire_list
, pw
);
372 zebra_delete_rnh(rnh
, RNH_NEXTHOP_TYPE
);
375 /* Clear the NEXTHOP_FLAG_RNH_FILTERED flags on all nexthops
377 static void zebra_rnh_clear_nexthop_rnh_filters(struct route_entry
*re
)
379 struct nexthop
*nexthop
;
382 for (nexthop
= re
->nhe
->nhg
.nexthop
; nexthop
;
383 nexthop
= nexthop
->next
) {
384 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RNH_FILTERED
);
389 /* Apply the NHT route-map for a client to the route (and nexthops)
392 static int zebra_rnh_apply_nht_rmap(afi_t afi
, struct zebra_vrf
*zvrf
,
393 struct route_node
*prn
,
394 struct route_entry
*re
, int proto
)
396 int at_least_one
= 0;
397 struct nexthop
*nexthop
;
398 route_map_result_t ret
;
401 for (nexthop
= re
->nhe
->nhg
.nexthop
; nexthop
;
402 nexthop
= nexthop
->next
) {
403 ret
= zebra_nht_route_map_check(
404 afi
, proto
, &prn
->p
, zvrf
, re
, nexthop
);
405 if (ret
!= RMAP_DENYMATCH
)
406 at_least_one
++; /* at least one valid NH */
408 SET_FLAG(nexthop
->flags
,
409 NEXTHOP_FLAG_RNH_FILTERED
);
413 return (at_least_one
);
417 * Determine appropriate route (RE entry) resolving a tracked BGP route
418 * for BGP route for import.
420 static struct route_entry
*
421 zebra_rnh_resolve_import_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
422 struct route_node
*nrn
, struct rnh
*rnh
,
423 struct route_node
**prn
)
425 struct route_table
*route_table
;
426 struct route_node
*rn
;
427 struct route_entry
*re
;
431 route_table
= zvrf
->table
[afi
][SAFI_UNICAST
];
432 if (!route_table
) // unexpected
435 rn
= route_node_match(route_table
, &nrn
->p
);
439 /* Unlock route node - we don't need to lock when walking the tree. */
440 route_unlock_node(rn
);
442 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
)
443 && !prefix_same(&nrn
->p
, &rn
->p
))
446 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
447 zlog_debug("%s: %s(%u):%pRN Resolved Import Entry to %pRN",
448 __func__
, VRF_LOGNAME(zvrf
->vrf
), rnh
->vrf_id
,
452 /* Identify appropriate route entry. */
453 RNODE_FOREACH_RE (rn
, re
) {
454 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
455 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
456 && !CHECK_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
)
457 && (re
->type
!= ZEBRA_ROUTE_BGP
))
464 if (!re
&& IS_ZEBRA_DEBUG_NHT_DETAILED
)
465 zlog_debug(" Rejected due to removed or is a bgp route");
471 * See if a tracked route entry for import (by BGP) has undergone any
472 * change, and if so, notify the client.
474 static void zebra_rnh_eval_import_check_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
475 int force
, struct route_node
*nrn
,
477 struct route_node
*prn
,
478 struct route_entry
*re
)
480 int state_changed
= 0;
481 struct zserv
*client
;
482 struct listnode
*node
;
484 zebra_rnh_remove_from_routing_table(rnh
);
486 prefix_copy(&rnh
->resolved_route
, &prn
->p
);
488 int family
= rnh
->resolved_route
.family
;
490 memset(&rnh
->resolved_route
.family
, 0, sizeof(struct prefix
));
491 rnh
->resolved_route
.family
= family
;
493 zebra_rnh_store_in_routing_table(rnh
);
495 if (re
&& (rnh
->state
== NULL
)) {
496 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
498 } else if (!re
&& (rnh
->state
!= NULL
))
501 if (compare_state(re
, rnh
->state
)) {
502 copy_state(rnh
, re
, nrn
);
506 if (state_changed
|| force
) {
507 if (IS_ZEBRA_DEBUG_NHT
)
508 zlog_debug("%s(%u):%pRN: Route import check %s %s",
509 VRF_LOGNAME(zvrf
->vrf
), zvrf
->vrf
->vrf_id
,
510 nrn
, rnh
->state
? "passed" : "failed",
511 state_changed
? "(state changed)" : "");
512 /* state changed, notify clients */
513 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
514 zebra_send_rnh_update(rnh
, client
,
515 RNH_IMPORT_CHECK_TYPE
,
516 zvrf
->vrf
->vrf_id
, 0);
522 * Notify clients registered for this nexthop about a change.
524 static void zebra_rnh_notify_protocol_clients(struct zebra_vrf
*zvrf
, afi_t afi
,
525 struct route_node
*nrn
,
527 struct route_node
*prn
,
528 struct route_entry
*re
)
530 struct listnode
*node
;
531 struct zserv
*client
;
532 int num_resolving_nh
;
534 if (IS_ZEBRA_DEBUG_NHT
) {
536 zlog_debug("%s(%u):%pRN: NH resolved over route %pRN",
537 VRF_LOGNAME(zvrf
->vrf
), zvrf
->vrf
->vrf_id
,
540 zlog_debug("%s(%u):%pRN: NH has become unresolved",
541 VRF_LOGNAME(zvrf
->vrf
), zvrf
->vrf
->vrf_id
,
545 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
547 /* Apply route-map for this client to route resolving
549 * nexthop to see if it is filtered or not.
551 zebra_rnh_clear_nexthop_rnh_filters(re
);
552 num_resolving_nh
= zebra_rnh_apply_nht_rmap(
553 afi
, zvrf
, prn
, re
, client
->proto
);
554 if (num_resolving_nh
)
555 rnh
->filtered
[client
->proto
] = 0;
557 rnh
->filtered
[client
->proto
] = 1;
559 if (IS_ZEBRA_DEBUG_NHT
)
561 "%s(%u):%pRN: Notifying client %s about NH %s",
562 VRF_LOGNAME(zvrf
->vrf
),
563 zvrf
->vrf
->vrf_id
, nrn
,
564 zebra_route_string(client
->proto
),
567 : "(filtered by route-map)");
569 rnh
->filtered
[client
->proto
] = 0;
570 if (IS_ZEBRA_DEBUG_NHT
)
572 "%s(%u):%pRN: Notifying client %s about NH (unreachable)",
573 VRF_LOGNAME(zvrf
->vrf
),
574 zvrf
->vrf
->vrf_id
, nrn
,
575 zebra_route_string(client
->proto
));
578 zebra_send_rnh_update(rnh
, client
, RNH_NEXTHOP_TYPE
,
579 zvrf
->vrf
->vrf_id
, 0);
583 zebra_rnh_clear_nexthop_rnh_filters(re
);
587 * Utility to determine whether a candidate nexthop is useable. We make this
588 * check in a couple of places, so this is a single home for the logic we
591 static bool rnh_nexthop_valid(const struct route_entry
*re
,
592 const struct nexthop
*nh
)
594 return (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
595 && CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ACTIVE
)
596 && !CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
)
597 && !CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_DUPLICATE
)
598 && !CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RNH_FILTERED
));
602 * Determine appropriate route (route entry) resolving a tracked
605 static struct route_entry
*
606 zebra_rnh_resolve_nexthop_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
607 struct route_node
*nrn
, struct rnh
*rnh
,
608 struct route_node
**prn
)
610 struct route_table
*route_table
;
611 struct route_node
*rn
;
612 struct route_entry
*re
;
613 struct nexthop
*nexthop
;
617 route_table
= zvrf
->table
[afi
][SAFI_UNICAST
];
621 rn
= route_node_match(route_table
, &nrn
->p
);
625 /* Unlock route node - we don't need to lock when walking the tree. */
626 route_unlock_node(rn
);
628 /* While resolving nexthops, we may need to walk up the tree from the
629 * most-specific match. Do similar logic as in zebra_rib.c
632 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
633 zlog_debug("%s: %s(%u):%pRN Possible Match to %pRN",
634 __func__
, VRF_LOGNAME(zvrf
->vrf
),
635 rnh
->vrf_id
, rnh
->node
, rn
);
637 /* Do not resolve over default route unless allowed &&
638 * match route to be exact if so specified
640 if (is_default_prefix(&rn
->p
)
641 && !rnh_resolve_via_default(zvrf
, rn
->p
.family
)) {
642 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
644 " Not allowed to resolve through default prefix");
648 /* Identify appropriate route entry. */
649 RNODE_FOREACH_RE (rn
, re
) {
650 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
651 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
653 " Route Entry %s removed",
654 zebra_route_string(re
->type
));
657 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
) &&
658 !CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
659 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
661 " Route Entry %s !selected",
662 zebra_route_string(re
->type
));
666 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
)) {
667 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
669 " Route Entry %s queued",
670 zebra_route_string(re
->type
));
674 /* Just being SELECTED isn't quite enough - must
675 * have an installed nexthop to be useful.
677 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
)) {
678 if (rnh_nexthop_valid(re
, nexthop
))
682 if (nexthop
== NULL
) {
683 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
685 " Route Entry %s no nexthops",
686 zebra_route_string(re
->type
));
690 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)) {
691 if ((re
->type
== ZEBRA_ROUTE_CONNECT
)
692 || (re
->type
== ZEBRA_ROUTE_STATIC
))
694 if (re
->type
== ZEBRA_ROUTE_NHRP
) {
696 for (nexthop
= re
->nhe
->nhg
.nexthop
;
698 nexthop
= nexthop
->next
)
700 == NEXTHOP_TYPE_IFINDEX
)
709 /* Route entry found, we're done; else, walk up the tree. */
715 if (!CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
718 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
720 " Nexthop must be connected, cannot recurse up");
728 static void zebra_rnh_process_pseudowires(vrf_id_t vrfid
, struct rnh
*rnh
)
731 struct listnode
*node
;
733 for (ALL_LIST_ELEMENTS_RO(rnh
->zebra_pseudowire_list
, node
, pw
))
738 * See if a tracked nexthop entry has undergone any change, and if so,
739 * take appropriate action; this involves notifying any clients and/or
740 * scheduling dependent static routes for processing.
742 static void zebra_rnh_eval_nexthop_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
743 int force
, struct route_node
*nrn
,
745 struct route_node
*prn
,
746 struct route_entry
*re
)
748 int state_changed
= 0;
750 /* If we're resolving over a different route, resolution has changed or
751 * the resolving route has some change (e.g., metric), there is a state
754 zebra_rnh_remove_from_routing_table(rnh
);
755 if (!prefix_same(&rnh
->resolved_route
, prn
? &prn
->p
: NULL
)) {
757 prefix_copy(&rnh
->resolved_route
, &prn
->p
);
760 * Just quickly store the family of the resolved
761 * route so that we can reset it in a second here
763 int family
= rnh
->resolved_route
.family
;
765 memset(&rnh
->resolved_route
, 0, sizeof(struct prefix
));
766 rnh
->resolved_route
.family
= family
;
769 copy_state(rnh
, re
, nrn
);
771 } else if (compare_state(re
, rnh
->state
)) {
772 copy_state(rnh
, re
, nrn
);
775 zebra_rnh_store_in_routing_table(rnh
);
777 if (state_changed
|| force
) {
778 /* NOTE: Use the "copy" of resolving route stored in 'rnh' i.e.,
781 /* Notify registered protocol clients. */
782 zebra_rnh_notify_protocol_clients(zvrf
, afi
, nrn
, rnh
, prn
,
785 /* Process pseudowires attached to this nexthop */
786 zebra_rnh_process_pseudowires(zvrf
->vrf
->vrf_id
, rnh
);
790 /* Evaluate one tracked entry */
791 static void zebra_rnh_evaluate_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
792 int force
, enum rnh_type type
,
793 struct route_node
*nrn
)
796 struct route_entry
*re
;
797 struct route_node
*prn
;
799 if (IS_ZEBRA_DEBUG_NHT
) {
800 zlog_debug("%s(%u):%pRN: Evaluate RNH, type %s %s",
801 VRF_LOGNAME(zvrf
->vrf
), zvrf
->vrf
->vrf_id
, nrn
,
802 rnh_type2str(type
), force
? "(force)" : "");
807 /* Identify route entry (RE) resolving this tracked entry. */
808 if (type
== RNH_IMPORT_CHECK_TYPE
)
809 re
= zebra_rnh_resolve_import_entry(zvrf
, afi
, nrn
, rnh
, &prn
);
811 re
= zebra_rnh_resolve_nexthop_entry(zvrf
, afi
, nrn
, rnh
, &prn
);
813 /* If the entry cannot be resolved and that is also the existing state,
814 * there is nothing further to do.
816 if (!re
&& rnh
->state
== NULL
&& !force
)
819 /* Process based on type of entry. */
820 if (type
== RNH_IMPORT_CHECK_TYPE
)
821 zebra_rnh_eval_import_check_entry(zvrf
, afi
, force
, nrn
, rnh
,
824 zebra_rnh_eval_nexthop_entry(zvrf
, afi
, force
, nrn
, rnh
, prn
,
829 * Clear the ROUTE_ENTRY_NEXTHOPS_CHANGED flag
830 * from the re entries.
832 * Please note we are doing this *after* we have
833 * notified the world about each nexthop as that
834 * we can have a situation where one re entry
835 * covers multiple nexthops we are interested in.
837 static void zebra_rnh_clear_nhc_flag(struct zebra_vrf
*zvrf
, afi_t afi
,
838 enum rnh_type type
, struct route_node
*nrn
)
841 struct route_entry
*re
;
842 struct route_node
*prn
;
846 /* Identify route entry (RIB) resolving this tracked entry. */
847 if (type
== RNH_IMPORT_CHECK_TYPE
)
848 re
= zebra_rnh_resolve_import_entry(zvrf
, afi
, nrn
, rnh
,
851 re
= zebra_rnh_resolve_nexthop_entry(zvrf
, afi
, nrn
, rnh
,
855 UNSET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
858 /* Evaluate all tracked entries (nexthops or routes for import into BGP)
859 * of a particular VRF and address-family or a specific prefix.
861 void zebra_evaluate_rnh(struct zebra_vrf
*zvrf
, afi_t afi
, int force
,
862 enum rnh_type type
, struct prefix
*p
)
864 struct route_table
*rnh_table
;
865 struct route_node
*nrn
;
867 rnh_table
= get_rnh_table(zvrf
->vrf
->vrf_id
, afi
, type
);
868 if (!rnh_table
) // unexpected
872 /* Evaluating a specific entry, make sure it exists. */
873 nrn
= route_node_lookup(rnh_table
, p
);
874 if (nrn
&& nrn
->info
)
875 zebra_rnh_evaluate_entry(zvrf
, afi
, force
, type
, nrn
);
878 route_unlock_node(nrn
);
880 /* Evaluate entire table. */
881 nrn
= route_top(rnh_table
);
884 zebra_rnh_evaluate_entry(zvrf
, afi
, force
, type
,
886 nrn
= route_next(nrn
); /* this will also unlock nrn */
888 nrn
= route_top(rnh_table
);
891 zebra_rnh_clear_nhc_flag(zvrf
, afi
, type
, nrn
);
892 nrn
= route_next(nrn
); /* this will also unlock nrn */
897 void zebra_print_rnh_table(vrf_id_t vrfid
, afi_t afi
, struct vty
*vty
,
898 enum rnh_type type
, struct prefix
*p
)
900 struct route_table
*table
;
901 struct route_node
*rn
;
903 table
= get_rnh_table(vrfid
, afi
, type
);
905 if (IS_ZEBRA_DEBUG_NHT
)
906 zlog_debug("print_rnhs: rnh table not found");
910 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
911 if (p
&& !prefix_match(&rn
->p
, p
))
920 * free_state - free up the re structure associated with the rnh.
922 static void free_state(vrf_id_t vrf_id
, struct route_entry
*re
,
923 struct route_node
*rn
)
928 /* free RE and nexthops */
929 zebra_nhg_free(re
->nhe
);
933 static void copy_state(struct rnh
*rnh
, const struct route_entry
*re
,
934 struct route_node
*rn
)
936 struct route_entry
*state
;
939 free_state(rnh
->vrf_id
, rnh
->state
, rn
);
946 state
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
947 state
->type
= re
->type
;
948 state
->distance
= re
->distance
;
949 state
->metric
= re
->metric
;
950 state
->vrf_id
= re
->vrf_id
;
951 state
->status
= re
->status
;
953 state
->nhe
= zebra_nhe_copy(re
->nhe
, 0);
955 /* Copy the 'fib' nexthops also, if present - we want to capture
956 * the true installed nexthops.
958 if (re
->fib_ng
.nexthop
)
959 nexthop_group_copy(&state
->fib_ng
, &re
->fib_ng
);
960 if (re
->fib_backup_ng
.nexthop
)
961 nexthop_group_copy(&state
->fib_backup_ng
, &re
->fib_backup_ng
);
967 * Compare two route_entries' nexthops.
969 static bool compare_valid_nexthops(struct route_entry
*r1
,
970 struct route_entry
*r2
)
972 bool matched_p
= false;
973 struct nexthop_group
*nhg1
, *nhg2
;
974 struct nexthop
*nh1
, *nh2
;
976 /* Account for backup nexthops and for the 'fib' nexthop lists,
979 nhg1
= rib_get_fib_nhg(r1
);
980 nhg2
= rib_get_fib_nhg(r2
);
986 /* Find each list's next valid nexthop */
987 while ((nh1
!= NULL
) && !rnh_nexthop_valid(r1
, nh1
))
988 nh1
= nexthop_next(nh1
);
990 while ((nh2
!= NULL
) && !rnh_nexthop_valid(r2
, nh2
))
991 nh2
= nexthop_next(nh2
);
994 /* Any difference is a no-match */
995 if (nexthop_cmp(nh1
, nh2
) != 0) {
996 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
997 zlog_debug("%s: nh1, nh2 differ",
1002 nh1
= nexthop_next(nh1
);
1003 nh2
= nexthop_next(nh2
);
1004 } else if (nh1
|| nh2
) {
1005 /* One list has more valid nexthops than the other */
1006 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1007 zlog_debug("%s: nh1 %s, nh2 %s", __func__
,
1008 nh1
? "non-NULL" : "NULL",
1009 nh2
? "non-NULL" : "NULL");
1012 break; /* Done with both lists */
1015 /* The test for the backups is slightly different: the only installed
1016 * backups will be in the 'fib' list.
1018 nhg1
= rib_get_fib_backup_nhg(r1
);
1019 nhg2
= rib_get_fib_backup_nhg(r2
);
1021 nh1
= nhg1
->nexthop
;
1022 nh2
= nhg2
->nexthop
;
1025 /* Find each backup list's next valid nexthop */
1026 while ((nh1
!= NULL
) && !rnh_nexthop_valid(r1
, nh1
))
1027 nh1
= nexthop_next(nh1
);
1029 while ((nh2
!= NULL
) && !rnh_nexthop_valid(r2
, nh2
))
1030 nh2
= nexthop_next(nh2
);
1033 /* Any difference is a no-match */
1034 if (nexthop_cmp(nh1
, nh2
) != 0) {
1035 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1036 zlog_debug("%s: backup nh1, nh2 differ",
1041 nh1
= nexthop_next(nh1
);
1042 nh2
= nexthop_next(nh2
);
1043 } else if (nh1
|| nh2
) {
1044 /* One list has more valid nexthops than the other */
1045 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1046 zlog_debug("%s: backup nh1 %s, nh2 %s",
1048 nh1
? "non-NULL" : "NULL",
1049 nh2
? "non-NULL" : "NULL");
1052 break; /* Done with both lists */
1055 /* Well, it's a match */
1056 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1057 zlog_debug("%s: matched", __func__
);
1066 static int compare_state(struct route_entry
*r1
, struct route_entry
*r2
)
1071 if ((!r1
&& r2
) || (r1
&& !r2
))
1074 if (r1
->distance
!= r2
->distance
)
1077 if (r1
->metric
!= r2
->metric
)
1080 if (!compare_valid_nexthops(r1
, r2
))
1086 int zebra_send_rnh_update(struct rnh
*rnh
, struct zserv
*client
,
1087 enum rnh_type type
, vrf_id_t vrf_id
,
1088 uint32_t srte_color
)
1090 struct stream
*s
= NULL
;
1091 struct route_entry
*re
;
1095 struct route_node
*rn
;
1097 uint32_t message
= 0;
1098 int cmd
= (type
== RNH_IMPORT_CHECK_TYPE
) ? ZEBRA_IMPORT_CHECK_UPDATE
1099 : ZEBRA_NEXTHOP_UPDATE
;
1104 /* Get output stream. */
1105 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1107 zclient_create_header(s
, cmd
, vrf_id
);
1109 /* Message flags. */
1111 SET_FLAG(message
, ZAPI_MESSAGE_SRTE
);
1112 stream_putl(s
, message
);
1114 stream_putw(s
, rn
->p
.family
);
1115 switch (rn
->p
.family
) {
1117 stream_putc(s
, rn
->p
.prefixlen
);
1118 stream_put_in_addr(s
, &rn
->p
.u
.prefix4
);
1121 stream_putc(s
, rn
->p
.prefixlen
);
1122 stream_put(s
, &rn
->p
.u
.prefix6
, IPV6_MAX_BYTELEN
);
1125 flog_err(EC_ZEBRA_RNH_UNKNOWN_FAMILY
,
1126 "%s: Unknown family (%d) notification attempted",
1127 __func__
, rn
->p
.family
);
1131 stream_putl(s
, srte_color
);
1134 struct zapi_nexthop znh
;
1135 struct nexthop_group
*nhg
;
1137 stream_putc(s
, re
->type
);
1138 stream_putw(s
, re
->instance
);
1139 stream_putc(s
, re
->distance
);
1140 stream_putl(s
, re
->metric
);
1142 nump
= stream_get_endp(s
);
1145 nhg
= rib_get_fib_nhg(re
);
1146 for (ALL_NEXTHOPS_PTR(nhg
, nh
))
1147 if (rnh_nexthop_valid(re
, nh
)) {
1148 zapi_nexthop_from_nexthop(&znh
, nh
);
1149 ret
= zapi_nexthop_encode(s
, &znh
, 0, message
);
1156 nhg
= rib_get_fib_backup_nhg(re
);
1158 for (ALL_NEXTHOPS_PTR(nhg
, nh
))
1159 if (rnh_nexthop_valid(re
, nh
)) {
1160 zapi_nexthop_from_nexthop(&znh
, nh
);
1161 ret
= zapi_nexthop_encode(
1162 s
, &znh
, 0 /* flags */,
1171 stream_putc_at(s
, nump
, num
);
1173 stream_putc(s
, 0); // type
1174 stream_putw(s
, 0); // instance
1175 stream_putc(s
, 0); // distance
1176 stream_putl(s
, 0); // metric
1177 stream_putc(s
, 0); // nexthops
1179 stream_putw_at(s
, 0, stream_get_endp(s
));
1181 client
->nh_last_upd_time
= monotime(NULL
);
1182 client
->last_write_cmd
= cmd
;
1183 return zserv_send_message(client
, s
);
1191 static void print_nh(struct nexthop
*nexthop
, struct vty
*vty
)
1194 struct zebra_ns
*zns
= zebra_ns_lookup(nexthop
->vrf_id
);
1196 switch (nexthop
->type
) {
1197 case NEXTHOP_TYPE_IPV4
:
1198 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1199 vty_out(vty
, " via %pI4", &nexthop
->gate
.ipv4
);
1200 if (nexthop
->ifindex
)
1201 vty_out(vty
, ", %s",
1202 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
1204 case NEXTHOP_TYPE_IPV6
:
1205 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1207 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1208 if (nexthop
->ifindex
)
1209 vty_out(vty
, ", via %s",
1210 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
1212 case NEXTHOP_TYPE_IFINDEX
:
1213 vty_out(vty
, " is directly connected, %s",
1214 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
1216 case NEXTHOP_TYPE_BLACKHOLE
:
1217 vty_out(vty
, " is directly connected, Null0");
1225 static void print_rnh(struct route_node
*rn
, struct vty
*vty
)
1228 struct nexthop
*nexthop
;
1229 struct listnode
*node
;
1230 struct zserv
*client
;
1234 vty_out(vty
, "%s%s\n",
1235 inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
1236 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
) ? "(Connected)"
1239 vty_out(vty
, " resolved via %s\n",
1240 zebra_route_string(rnh
->state
->type
));
1241 for (nexthop
= rnh
->state
->nhe
->nhg
.nexthop
; nexthop
;
1242 nexthop
= nexthop
->next
)
1243 print_nh(nexthop
, vty
);
1245 vty_out(vty
, " unresolved%s\n",
1246 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)
1250 vty_out(vty
, " Client list:");
1251 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
))
1252 vty_out(vty
, " %s(fd %d)%s", zebra_route_string(client
->proto
),
1254 rnh
->filtered
[client
->proto
] ? "(filtered)" : "");
1255 if (!list_isempty(rnh
->zebra_pseudowire_list
))
1256 vty_out(vty
, " zebra[pseudowires]");
1260 static int zebra_cleanup_rnh_client(vrf_id_t vrf_id
, afi_t afi
,
1261 struct zserv
*client
, enum rnh_type type
)
1263 struct route_table
*ntable
;
1264 struct route_node
*nrn
;
1267 if (IS_ZEBRA_DEBUG_NHT
) {
1268 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
1271 "%s(%u): Client %s RNH cleanup for family %s type %s",
1272 VRF_LOGNAME(vrf
), vrf_id
,
1273 zebra_route_string(client
->proto
), afi2str(afi
),
1274 rnh_type2str(type
));
1277 ntable
= get_rnh_table(vrf_id
, afi
, type
);
1279 zlog_debug("cleanup_rnh_client: rnh table not found");
1283 for (nrn
= route_top(ntable
); nrn
; nrn
= route_next(nrn
)) {
1288 zebra_remove_rnh_client(rnh
, client
, type
);
1293 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
1294 static int zebra_client_cleanup_rnh(struct zserv
*client
)
1297 struct zebra_vrf
*zvrf
;
1299 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1302 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AFI_IP
, client
,
1304 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AFI_IP6
, client
,
1306 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AFI_IP
, client
,
1307 RNH_IMPORT_CHECK_TYPE
);
1308 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AFI_IP6
, client
,
1309 RNH_IMPORT_CHECK_TYPE
);
1316 int rnh_resolve_via_default(struct zebra_vrf
*zvrf
, int family
)
1318 if (((family
== AF_INET
) && zvrf
->zebra_rnh_ip_default_route
)
1319 || ((family
== AF_INET6
) && zvrf
->zebra_rnh_ipv6_default_route
))