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_memory.h"
52 #include "zebra/zebra_errors.h"
54 DEFINE_MTYPE_STATIC(ZEBRA
, RNH
, "Nexthop tracking object")
56 static void free_state(vrf_id_t vrf_id
, struct route_entry
*re
,
57 struct route_node
*rn
);
58 static void copy_state(struct rnh
*rnh
, const struct route_entry
*re
,
59 struct route_node
*rn
);
60 static int compare_state(struct route_entry
*r1
, struct route_entry
*r2
);
61 static void print_rnh(struct route_node
*rn
, struct vty
*vty
);
62 static int zebra_client_cleanup_rnh(struct zserv
*client
);
64 void zebra_rnh_init(void)
66 hook_register(zserv_client_close
, zebra_client_cleanup_rnh
);
69 static inline struct route_table
*get_rnh_table(vrf_id_t vrfid
, afi_t afi
,
72 struct zebra_vrf
*zvrf
;
73 struct route_table
*t
= NULL
;
75 zvrf
= zebra_vrf_lookup_by_id(vrfid
);
78 case RNH_NEXTHOP_TYPE
:
79 t
= zvrf
->rnh_table
[afi
];
81 case RNH_IMPORT_CHECK_TYPE
:
82 t
= zvrf
->import_check_table
[afi
];
89 static void zebra_rnh_remove_from_routing_table(struct rnh
*rnh
)
91 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(rnh
->vrf_id
);
92 struct route_table
*table
= zvrf
->table
[rnh
->afi
][SAFI_UNICAST
];
93 struct route_node
*rn
;
99 rn
= route_node_match(table
, &rnh
->resolved_route
);
103 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
104 zlog_debug("%s: %s(%u):%pRN removed from tracking on %pRN",
105 __func__
, VRF_LOGNAME(zvrf
->vrf
), rnh
->vrf_id
,
108 dest
= rib_dest_from_rnode(rn
);
109 rnh_list_del(&dest
->nht
, rnh
);
110 route_unlock_node(rn
);
113 static void zebra_rnh_store_in_routing_table(struct rnh
*rnh
)
115 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(rnh
->vrf_id
);
116 struct route_table
*table
= zvrf
->table
[rnh
->afi
][SAFI_UNICAST
];
117 struct route_node
*rn
;
120 rn
= route_node_match(table
, &rnh
->resolved_route
);
124 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
125 zlog_debug("%s: %s(%u):%pRN added for tracking on %pRN",
126 __func__
, VRF_LOGNAME(zvrf
->vrf
), rnh
->vrf_id
,
129 dest
= rib_dest_from_rnode(rn
);
130 rnh_list_add_tail(&dest
->nht
, rnh
);
131 route_unlock_node(rn
);
134 struct rnh
*zebra_add_rnh(struct prefix
*p
, vrf_id_t vrfid
, enum rnh_type type
,
137 struct route_table
*table
;
138 struct route_node
*rn
;
139 struct rnh
*rnh
= NULL
;
140 afi_t afi
= family2afi(p
->family
);
142 if (IS_ZEBRA_DEBUG_NHT
) {
143 struct vrf
*vrf
= vrf_lookup_by_id(vrfid
);
145 zlog_debug("%s(%u): Add RNH %pFX type %s", VRF_LOGNAME(vrf
),
146 vrfid
, p
, rnh_type2str(type
));
148 table
= get_rnh_table(vrfid
, afi
, type
);
150 struct vrf
*vrf
= vrf_lookup_by_id(vrfid
);
152 flog_warn(EC_ZEBRA_RNH_NO_TABLE
,
153 "%s(%u): Add RNH %pFX type %s - table not found",
154 VRF_LOGNAME(vrf
), vrfid
, p
, rnh_type2str(type
));
159 /* Make it sure prefixlen is applied to the prefix. */
162 /* Lookup (or add) route node.*/
163 rn
= route_node_get(table
, p
);
166 rnh
= XCALLOC(MTYPE_RNH
, sizeof(struct rnh
));
169 * The resolved route is already 0.0.0.0/0 or
170 * 0::0/0 due to the calloc right above, but
171 * we should set the family so that future
172 * comparisons can just be done
174 rnh
->resolved_route
.family
= p
->family
;
175 rnh
->client_list
= list_new();
180 rnh
->zebra_pseudowire_list
= list_new();
186 zebra_rnh_store_in_routing_table(rnh
);
190 route_unlock_node(rn
);
194 struct rnh
*zebra_lookup_rnh(struct prefix
*p
, vrf_id_t vrfid
,
197 struct route_table
*table
;
198 struct route_node
*rn
;
200 table
= get_rnh_table(vrfid
, family2afi(PREFIX_FAMILY(p
)), type
);
204 /* Make it sure prefixlen is applied to the prefix. */
207 /* Lookup route node.*/
208 rn
= route_node_lookup(table
, p
);
212 route_unlock_node(rn
);
216 void zebra_free_rnh(struct rnh
*rnh
)
218 struct zebra_vrf
*zvrf
;
219 struct route_table
*table
;
221 zebra_rnh_remove_from_routing_table(rnh
);
222 rnh
->flags
|= ZEBRA_NHT_DELETED
;
223 list_delete(&rnh
->client_list
);
224 list_delete(&rnh
->zebra_pseudowire_list
);
226 zvrf
= zebra_vrf_lookup_by_id(rnh
->vrf_id
);
227 table
= zvrf
->table
[family2afi(rnh
->resolved_route
.family
)][SAFI_UNICAST
];
230 struct route_node
*rern
;
232 rern
= route_node_match(table
, &rnh
->resolved_route
);
236 route_unlock_node(rern
);
238 dest
= rib_dest_from_rnode(rern
);
239 rnh_list_del(&dest
->nht
, rnh
);
242 free_state(rnh
->vrf_id
, rnh
->state
, rnh
->node
);
243 XFREE(MTYPE_RNH
, rnh
);
246 static void zebra_delete_rnh(struct rnh
*rnh
, enum rnh_type type
)
248 struct route_node
*rn
;
250 if (!list_isempty(rnh
->client_list
)
251 || !list_isempty(rnh
->zebra_pseudowire_list
))
254 if ((rnh
->flags
& ZEBRA_NHT_DELETED
) || !(rn
= rnh
->node
))
257 if (IS_ZEBRA_DEBUG_NHT
) {
258 struct vrf
*vrf
= vrf_lookup_by_id(rnh
->vrf_id
);
260 zlog_debug("%s(%u): Del RNH %pRN type %s", VRF_LOGNAME(vrf
),
261 rnh
->vrf_id
, rnh
->node
, rnh_type2str(type
));
266 route_unlock_node(rn
);
270 * This code will send to the registering client
272 * For a rnh that was created, there is no data
273 * so it will send an empty nexthop group
274 * If rnh exists then we know it has been evaluated
275 * and as such it will have a resolved rnh.
277 void zebra_add_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
278 enum rnh_type type
, vrf_id_t vrf_id
)
280 if (IS_ZEBRA_DEBUG_NHT
) {
281 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
283 zlog_debug("%s(%u): Client %s registers for RNH %pRN type %s",
284 VRF_LOGNAME(vrf
), vrf_id
,
285 zebra_route_string(client
->proto
), rnh
->node
,
288 if (!listnode_lookup(rnh
->client_list
, client
))
289 listnode_add(rnh
->client_list
, client
);
292 * We always need to respond with known information,
293 * currently multiple daemons expect this behavior
295 zebra_send_rnh_update(rnh
, client
, type
, vrf_id
, 0);
298 void zebra_remove_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
301 if (IS_ZEBRA_DEBUG_NHT
) {
302 struct vrf
*vrf
= vrf_lookup_by_id(rnh
->vrf_id
);
304 zlog_debug("Client %s unregisters for RNH %s(%u)%pRN type %s",
305 zebra_route_string(client
->proto
), VRF_LOGNAME(vrf
),
306 vrf
->vrf_id
, rnh
->node
, rnh_type2str(type
));
308 listnode_delete(rnh
->client_list
, client
);
309 zebra_delete_rnh(rnh
, type
);
312 /* XXX move this utility function elsewhere? */
313 static void addr2hostprefix(int af
, const union g_addr
*addr
,
314 struct prefix
*prefix
)
318 prefix
->family
= AF_INET
;
319 prefix
->prefixlen
= IPV4_MAX_BITLEN
;
320 prefix
->u
.prefix4
= addr
->ipv4
;
323 prefix
->family
= AF_INET6
;
324 prefix
->prefixlen
= IPV6_MAX_BITLEN
;
325 prefix
->u
.prefix6
= addr
->ipv6
;
328 memset(prefix
, 0, sizeof(*prefix
));
329 zlog_warn("%s: unknown address family %d", __func__
, af
);
334 void zebra_register_rnh_pseudowire(vrf_id_t vrf_id
, struct zebra_pw
*pw
,
340 struct zebra_vrf
*zvrf
;
344 zvrf
= vrf_info_lookup(vrf_id
);
348 addr2hostprefix(pw
->af
, &pw
->nexthop
, &nh
);
349 rnh
= zebra_add_rnh(&nh
, vrf_id
, RNH_NEXTHOP_TYPE
, &exists
);
353 if (!listnode_lookup(rnh
->zebra_pseudowire_list
, pw
)) {
354 listnode_add(rnh
->zebra_pseudowire_list
, pw
);
356 zebra_evaluate_rnh(zvrf
, family2afi(pw
->af
), 1,
357 RNH_NEXTHOP_TYPE
, &nh
);
362 void zebra_deregister_rnh_pseudowire(vrf_id_t vrf_id
, struct zebra_pw
*pw
)
370 listnode_delete(rnh
->zebra_pseudowire_list
, pw
);
373 zebra_delete_rnh(rnh
, RNH_NEXTHOP_TYPE
);
376 /* Clear the NEXTHOP_FLAG_RNH_FILTERED flags on all nexthops
378 static void zebra_rnh_clear_nexthop_rnh_filters(struct route_entry
*re
)
380 struct nexthop
*nexthop
;
383 for (nexthop
= re
->nhe
->nhg
.nexthop
; nexthop
;
384 nexthop
= nexthop
->next
) {
385 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RNH_FILTERED
);
390 /* Apply the NHT route-map for a client to the route (and nexthops)
393 static int zebra_rnh_apply_nht_rmap(afi_t afi
, struct zebra_vrf
*zvrf
,
394 struct route_node
*prn
,
395 struct route_entry
*re
, int proto
)
397 int at_least_one
= 0;
398 struct nexthop
*nexthop
;
399 route_map_result_t ret
;
402 for (nexthop
= re
->nhe
->nhg
.nexthop
; nexthop
;
403 nexthop
= nexthop
->next
) {
404 ret
= zebra_nht_route_map_check(
405 afi
, proto
, &prn
->p
, zvrf
, re
, nexthop
);
406 if (ret
!= RMAP_DENYMATCH
)
407 at_least_one
++; /* at least one valid NH */
409 SET_FLAG(nexthop
->flags
,
410 NEXTHOP_FLAG_RNH_FILTERED
);
414 return (at_least_one
);
418 * Determine appropriate route (RE entry) resolving a tracked BGP route
419 * for BGP route for import.
421 static struct route_entry
*
422 zebra_rnh_resolve_import_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
423 struct route_node
*nrn
, struct rnh
*rnh
,
424 struct route_node
**prn
)
426 struct route_table
*route_table
;
427 struct route_node
*rn
;
428 struct route_entry
*re
;
432 route_table
= zvrf
->table
[afi
][SAFI_UNICAST
];
433 if (!route_table
) // unexpected
436 rn
= route_node_match(route_table
, &nrn
->p
);
440 /* Unlock route node - we don't need to lock when walking the tree. */
441 route_unlock_node(rn
);
443 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
)
444 && !prefix_same(&nrn
->p
, &rn
->p
))
447 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
448 zlog_debug("%s: %s(%u):%pRN Resolved Import Entry to %pRN",
449 __func__
, VRF_LOGNAME(zvrf
->vrf
), rnh
->vrf_id
,
453 /* Identify appropriate route entry. */
454 RNODE_FOREACH_RE (rn
, re
) {
455 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
456 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
457 && !CHECK_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
)
458 && (re
->type
!= ZEBRA_ROUTE_BGP
))
465 if (!re
&& IS_ZEBRA_DEBUG_NHT_DETAILED
)
466 zlog_debug(" Rejected due to removed or is a bgp route");
472 * See if a tracked route entry for import (by BGP) has undergone any
473 * change, and if so, notify the client.
475 static void zebra_rnh_eval_import_check_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
476 int force
, struct route_node
*nrn
,
478 struct route_node
*prn
,
479 struct route_entry
*re
)
481 int state_changed
= 0;
482 struct zserv
*client
;
483 struct listnode
*node
;
485 zebra_rnh_remove_from_routing_table(rnh
);
487 prefix_copy(&rnh
->resolved_route
, &prn
->p
);
489 int family
= rnh
->resolved_route
.family
;
491 memset(&rnh
->resolved_route
.family
, 0, sizeof(struct prefix
));
492 rnh
->resolved_route
.family
= family
;
494 zebra_rnh_store_in_routing_table(rnh
);
496 if (re
&& (rnh
->state
== NULL
)) {
497 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
499 } else if (!re
&& (rnh
->state
!= NULL
))
502 if (compare_state(re
, rnh
->state
)) {
503 copy_state(rnh
, re
, nrn
);
507 if (state_changed
|| force
) {
508 if (IS_ZEBRA_DEBUG_NHT
)
509 zlog_debug("%s(%u):%pRN: Route import check %s %s",
510 VRF_LOGNAME(zvrf
->vrf
), zvrf
->vrf
->vrf_id
,
511 nrn
, rnh
->state
? "passed" : "failed",
512 state_changed
? "(state changed)" : "");
513 /* state changed, notify clients */
514 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
515 zebra_send_rnh_update(rnh
, client
,
516 RNH_IMPORT_CHECK_TYPE
,
517 zvrf
->vrf
->vrf_id
, 0);
523 * Notify clients registered for this nexthop about a change.
525 static void zebra_rnh_notify_protocol_clients(struct zebra_vrf
*zvrf
, afi_t afi
,
526 struct route_node
*nrn
,
528 struct route_node
*prn
,
529 struct route_entry
*re
)
531 struct listnode
*node
;
532 struct zserv
*client
;
533 int num_resolving_nh
;
535 if (IS_ZEBRA_DEBUG_NHT
) {
537 zlog_debug("%s(%u):%pRN: NH resolved over route %pRN",
538 VRF_LOGNAME(zvrf
->vrf
), zvrf
->vrf
->vrf_id
,
541 zlog_debug("%s(%u):%pRN: NH has become unresolved",
542 VRF_LOGNAME(zvrf
->vrf
), zvrf
->vrf
->vrf_id
,
546 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
548 /* Apply route-map for this client to route resolving
550 * nexthop to see if it is filtered or not.
552 zebra_rnh_clear_nexthop_rnh_filters(re
);
553 num_resolving_nh
= zebra_rnh_apply_nht_rmap(
554 afi
, zvrf
, prn
, re
, client
->proto
);
555 if (num_resolving_nh
)
556 rnh
->filtered
[client
->proto
] = 0;
558 rnh
->filtered
[client
->proto
] = 1;
560 if (IS_ZEBRA_DEBUG_NHT
)
562 "%s(%u):%pRN: Notifying client %s about NH %s",
563 VRF_LOGNAME(zvrf
->vrf
),
564 zvrf
->vrf
->vrf_id
, nrn
,
565 zebra_route_string(client
->proto
),
568 : "(filtered by route-map)");
570 rnh
->filtered
[client
->proto
] = 0;
571 if (IS_ZEBRA_DEBUG_NHT
)
573 "%s(%u):%pRN: Notifying client %s about NH (unreachable)",
574 VRF_LOGNAME(zvrf
->vrf
),
575 zvrf
->vrf
->vrf_id
, nrn
,
576 zebra_route_string(client
->proto
));
579 zebra_send_rnh_update(rnh
, client
, RNH_NEXTHOP_TYPE
,
580 zvrf
->vrf
->vrf_id
, 0);
584 zebra_rnh_clear_nexthop_rnh_filters(re
);
588 * Utility to determine whether a candidate nexthop is useable. We make this
589 * check in a couple of places, so this is a single home for the logic we
592 static bool rnh_nexthop_valid(const struct route_entry
*re
,
593 const struct nexthop
*nh
)
595 return (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
596 && CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ACTIVE
)
597 && !CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
)
598 && !CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_DUPLICATE
)
599 && !CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RNH_FILTERED
));
603 * Determine appropriate route (route entry) resolving a tracked
606 static struct route_entry
*
607 zebra_rnh_resolve_nexthop_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
608 struct route_node
*nrn
, struct rnh
*rnh
,
609 struct route_node
**prn
)
611 struct route_table
*route_table
;
612 struct route_node
*rn
;
613 struct route_entry
*re
;
614 struct nexthop
*nexthop
;
618 route_table
= zvrf
->table
[afi
][SAFI_UNICAST
];
622 rn
= route_node_match(route_table
, &nrn
->p
);
626 /* Unlock route node - we don't need to lock when walking the tree. */
627 route_unlock_node(rn
);
629 /* While resolving nexthops, we may need to walk up the tree from the
630 * most-specific match. Do similar logic as in zebra_rib.c
633 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
634 zlog_debug("%s: %s(%u):%pRN Possible Match to %pRN",
635 __func__
, VRF_LOGNAME(zvrf
->vrf
),
636 rnh
->vrf_id
, rnh
->node
, rn
);
638 /* Do not resolve over default route unless allowed &&
639 * match route to be exact if so specified
641 if (is_default_prefix(&rn
->p
)
642 && !rnh_resolve_via_default(zvrf
, rn
->p
.family
)) {
643 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
645 " Not allowed to resolve through default prefix");
649 /* Identify appropriate route entry. */
650 RNODE_FOREACH_RE (rn
, re
) {
651 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
652 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
654 " Route Entry %s removed",
655 zebra_route_string(re
->type
));
658 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
) &&
659 !CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
660 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
662 " Route Entry %s !selected",
663 zebra_route_string(re
->type
));
667 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
)) {
668 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
670 " Route Entry %s queued",
671 zebra_route_string(re
->type
));
675 /* Just being SELECTED isn't quite enough - must
676 * have an installed nexthop to be useful.
678 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
)) {
679 if (rnh_nexthop_valid(re
, nexthop
))
683 if (nexthop
== NULL
) {
684 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
686 " Route Entry %s no nexthops",
687 zebra_route_string(re
->type
));
691 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)) {
692 if ((re
->type
== ZEBRA_ROUTE_CONNECT
)
693 || (re
->type
== ZEBRA_ROUTE_STATIC
))
695 if (re
->type
== ZEBRA_ROUTE_NHRP
) {
697 for (nexthop
= re
->nhe
->nhg
.nexthop
;
699 nexthop
= nexthop
->next
)
701 == NEXTHOP_TYPE_IFINDEX
)
710 /* Route entry found, we're done; else, walk up the tree. */
716 if (!CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
719 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
721 " Nexthop must be connected, cannot recurse up");
729 static void zebra_rnh_process_pseudowires(vrf_id_t vrfid
, struct rnh
*rnh
)
732 struct listnode
*node
;
734 for (ALL_LIST_ELEMENTS_RO(rnh
->zebra_pseudowire_list
, node
, pw
))
739 * See if a tracked nexthop entry has undergone any change, and if so,
740 * take appropriate action; this involves notifying any clients and/or
741 * scheduling dependent static routes for processing.
743 static void zebra_rnh_eval_nexthop_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
744 int force
, struct route_node
*nrn
,
746 struct route_node
*prn
,
747 struct route_entry
*re
)
749 int state_changed
= 0;
751 /* If we're resolving over a different route, resolution has changed or
752 * the resolving route has some change (e.g., metric), there is a state
755 zebra_rnh_remove_from_routing_table(rnh
);
756 if (!prefix_same(&rnh
->resolved_route
, prn
? &prn
->p
: NULL
)) {
758 prefix_copy(&rnh
->resolved_route
, &prn
->p
);
761 * Just quickly store the family of the resolved
762 * route so that we can reset it in a second here
764 int family
= rnh
->resolved_route
.family
;
766 memset(&rnh
->resolved_route
, 0, sizeof(struct prefix
));
767 rnh
->resolved_route
.family
= family
;
770 copy_state(rnh
, re
, nrn
);
772 } else if (compare_state(re
, rnh
->state
)) {
773 copy_state(rnh
, re
, nrn
);
776 zebra_rnh_store_in_routing_table(rnh
);
778 if (state_changed
|| force
) {
779 /* NOTE: Use the "copy" of resolving route stored in 'rnh' i.e.,
782 /* Notify registered protocol clients. */
783 zebra_rnh_notify_protocol_clients(zvrf
, afi
, nrn
, rnh
, prn
,
786 /* Process pseudowires attached to this nexthop */
787 zebra_rnh_process_pseudowires(zvrf
->vrf
->vrf_id
, rnh
);
791 /* Evaluate one tracked entry */
792 static void zebra_rnh_evaluate_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
793 int force
, enum rnh_type type
,
794 struct route_node
*nrn
)
797 struct route_entry
*re
;
798 struct route_node
*prn
;
800 if (IS_ZEBRA_DEBUG_NHT
) {
801 zlog_debug("%s(%u):%pRN: Evaluate RNH, type %s %s",
802 VRF_LOGNAME(zvrf
->vrf
), zvrf
->vrf
->vrf_id
, nrn
,
803 rnh_type2str(type
), force
? "(force)" : "");
808 /* Identify route entry (RE) resolving this tracked entry. */
809 if (type
== RNH_IMPORT_CHECK_TYPE
)
810 re
= zebra_rnh_resolve_import_entry(zvrf
, afi
, nrn
, rnh
, &prn
);
812 re
= zebra_rnh_resolve_nexthop_entry(zvrf
, afi
, nrn
, rnh
, &prn
);
814 /* If the entry cannot be resolved and that is also the existing state,
815 * there is nothing further to do.
817 if (!re
&& rnh
->state
== NULL
&& !force
)
820 /* Process based on type of entry. */
821 if (type
== RNH_IMPORT_CHECK_TYPE
)
822 zebra_rnh_eval_import_check_entry(zvrf
, afi
, force
, nrn
, rnh
,
825 zebra_rnh_eval_nexthop_entry(zvrf
, afi
, force
, nrn
, rnh
, prn
,
830 * Clear the ROUTE_ENTRY_NEXTHOPS_CHANGED flag
831 * from the re entries.
833 * Please note we are doing this *after* we have
834 * notified the world about each nexthop as that
835 * we can have a situation where one re entry
836 * covers multiple nexthops we are interested in.
838 static void zebra_rnh_clear_nhc_flag(struct zebra_vrf
*zvrf
, afi_t afi
,
839 enum rnh_type type
, struct route_node
*nrn
)
842 struct route_entry
*re
;
843 struct route_node
*prn
;
847 /* Identify route entry (RIB) resolving this tracked entry. */
848 if (type
== RNH_IMPORT_CHECK_TYPE
)
849 re
= zebra_rnh_resolve_import_entry(zvrf
, afi
, nrn
, rnh
,
852 re
= zebra_rnh_resolve_nexthop_entry(zvrf
, afi
, nrn
, rnh
,
856 UNSET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
859 /* Evaluate all tracked entries (nexthops or routes for import into BGP)
860 * of a particular VRF and address-family or a specific prefix.
862 void zebra_evaluate_rnh(struct zebra_vrf
*zvrf
, afi_t afi
, int force
,
863 enum rnh_type type
, struct prefix
*p
)
865 struct route_table
*rnh_table
;
866 struct route_node
*nrn
;
868 rnh_table
= get_rnh_table(zvrf
->vrf
->vrf_id
, afi
, type
);
869 if (!rnh_table
) // unexpected
873 /* Evaluating a specific entry, make sure it exists. */
874 nrn
= route_node_lookup(rnh_table
, p
);
875 if (nrn
&& nrn
->info
)
876 zebra_rnh_evaluate_entry(zvrf
, afi
, force
, type
, nrn
);
879 route_unlock_node(nrn
);
881 /* Evaluate entire table. */
882 nrn
= route_top(rnh_table
);
885 zebra_rnh_evaluate_entry(zvrf
, afi
, force
, type
,
887 nrn
= route_next(nrn
); /* this will also unlock nrn */
889 nrn
= route_top(rnh_table
);
892 zebra_rnh_clear_nhc_flag(zvrf
, afi
, type
, nrn
);
893 nrn
= route_next(nrn
); /* this will also unlock nrn */
898 void zebra_print_rnh_table(vrf_id_t vrfid
, afi_t afi
, struct vty
*vty
,
899 enum rnh_type type
, struct prefix
*p
)
901 struct route_table
*table
;
902 struct route_node
*rn
;
904 table
= get_rnh_table(vrfid
, afi
, type
);
906 if (IS_ZEBRA_DEBUG_NHT
)
907 zlog_debug("print_rnhs: rnh table not found");
911 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
912 if (p
&& !prefix_match(&rn
->p
, p
))
921 * free_state - free up the re structure associated with the rnh.
923 static void free_state(vrf_id_t vrf_id
, struct route_entry
*re
,
924 struct route_node
*rn
)
929 /* free RE and nexthops */
930 zebra_nhg_free(re
->nhe
);
934 static void copy_state(struct rnh
*rnh
, const struct route_entry
*re
,
935 struct route_node
*rn
)
937 struct route_entry
*state
;
940 free_state(rnh
->vrf_id
, rnh
->state
, rn
);
947 state
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
948 state
->type
= re
->type
;
949 state
->distance
= re
->distance
;
950 state
->metric
= re
->metric
;
951 state
->vrf_id
= re
->vrf_id
;
952 state
->status
= re
->status
;
954 state
->nhe
= zebra_nhe_copy(re
->nhe
, 0);
956 /* Copy the 'fib' nexthops also, if present - we want to capture
957 * the true installed nexthops.
959 if (re
->fib_ng
.nexthop
)
960 nexthop_group_copy(&state
->fib_ng
, &re
->fib_ng
);
961 if (re
->fib_backup_ng
.nexthop
)
962 nexthop_group_copy(&state
->fib_backup_ng
, &re
->fib_backup_ng
);
968 * Compare two route_entries' nexthops.
970 static bool compare_valid_nexthops(struct route_entry
*r1
,
971 struct route_entry
*r2
)
973 bool matched_p
= false;
974 struct nexthop_group
*nhg1
, *nhg2
;
975 struct nexthop
*nh1
, *nh2
;
977 /* Account for backup nexthops and for the 'fib' nexthop lists,
980 nhg1
= rib_get_fib_nhg(r1
);
981 nhg2
= rib_get_fib_nhg(r2
);
987 /* Find each list's next valid nexthop */
988 while ((nh1
!= NULL
) && !rnh_nexthop_valid(r1
, nh1
))
989 nh1
= nexthop_next(nh1
);
991 while ((nh2
!= NULL
) && !rnh_nexthop_valid(r2
, nh2
))
992 nh2
= nexthop_next(nh2
);
995 /* Any difference is a no-match */
996 if (nexthop_cmp(nh1
, nh2
) != 0) {
997 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
998 zlog_debug("%s: nh1, nh2 differ",
1003 nh1
= nexthop_next(nh1
);
1004 nh2
= nexthop_next(nh2
);
1005 } else if (nh1
|| nh2
) {
1006 /* One list has more valid nexthops than the other */
1007 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1008 zlog_debug("%s: nh1 %s, nh2 %s", __func__
,
1009 nh1
? "non-NULL" : "NULL",
1010 nh2
? "non-NULL" : "NULL");
1013 break; /* Done with both lists */
1016 /* The test for the backups is slightly different: the only installed
1017 * backups will be in the 'fib' list.
1019 nhg1
= rib_get_fib_backup_nhg(r1
);
1020 nhg2
= rib_get_fib_backup_nhg(r2
);
1022 nh1
= nhg1
->nexthop
;
1023 nh2
= nhg2
->nexthop
;
1026 /* Find each backup list's next valid nexthop */
1027 while ((nh1
!= NULL
) && !rnh_nexthop_valid(r1
, nh1
))
1028 nh1
= nexthop_next(nh1
);
1030 while ((nh2
!= NULL
) && !rnh_nexthop_valid(r2
, nh2
))
1031 nh2
= nexthop_next(nh2
);
1034 /* Any difference is a no-match */
1035 if (nexthop_cmp(nh1
, nh2
) != 0) {
1036 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1037 zlog_debug("%s: backup nh1, nh2 differ",
1042 nh1
= nexthop_next(nh1
);
1043 nh2
= nexthop_next(nh2
);
1044 } else if (nh1
|| nh2
) {
1045 /* One list has more valid nexthops than the other */
1046 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1047 zlog_debug("%s: backup nh1 %s, nh2 %s",
1049 nh1
? "non-NULL" : "NULL",
1050 nh2
? "non-NULL" : "NULL");
1053 break; /* Done with both lists */
1056 /* Well, it's a match */
1057 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1058 zlog_debug("%s: matched", __func__
);
1067 static int compare_state(struct route_entry
*r1
, struct route_entry
*r2
)
1072 if ((!r1
&& r2
) || (r1
&& !r2
))
1075 if (r1
->distance
!= r2
->distance
)
1078 if (r1
->metric
!= r2
->metric
)
1081 if (!compare_valid_nexthops(r1
, r2
))
1087 int zebra_send_rnh_update(struct rnh
*rnh
, struct zserv
*client
,
1088 enum rnh_type type
, vrf_id_t vrf_id
,
1089 uint32_t srte_color
)
1091 struct stream
*s
= NULL
;
1092 struct route_entry
*re
;
1096 struct route_node
*rn
;
1098 uint32_t message
= 0;
1099 int cmd
= (type
== RNH_IMPORT_CHECK_TYPE
) ? ZEBRA_IMPORT_CHECK_UPDATE
1100 : ZEBRA_NEXTHOP_UPDATE
;
1105 /* Get output stream. */
1106 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1108 zclient_create_header(s
, cmd
, vrf_id
);
1110 /* Message flags. */
1112 SET_FLAG(message
, ZAPI_MESSAGE_SRTE
);
1113 stream_putl(s
, message
);
1115 stream_putw(s
, rn
->p
.family
);
1116 switch (rn
->p
.family
) {
1118 stream_putc(s
, rn
->p
.prefixlen
);
1119 stream_put_in_addr(s
, &rn
->p
.u
.prefix4
);
1122 stream_putc(s
, rn
->p
.prefixlen
);
1123 stream_put(s
, &rn
->p
.u
.prefix6
, IPV6_MAX_BYTELEN
);
1126 flog_err(EC_ZEBRA_RNH_UNKNOWN_FAMILY
,
1127 "%s: Unknown family (%d) notification attempted",
1128 __func__
, rn
->p
.family
);
1132 stream_putl(s
, srte_color
);
1135 struct zapi_nexthop znh
;
1136 struct nexthop_group
*nhg
;
1138 stream_putc(s
, re
->type
);
1139 stream_putw(s
, re
->instance
);
1140 stream_putc(s
, re
->distance
);
1141 stream_putl(s
, re
->metric
);
1143 nump
= stream_get_endp(s
);
1146 nhg
= rib_get_fib_nhg(re
);
1147 for (ALL_NEXTHOPS_PTR(nhg
, nh
))
1148 if (rnh_nexthop_valid(re
, nh
)) {
1149 zapi_nexthop_from_nexthop(&znh
, nh
);
1150 ret
= zapi_nexthop_encode(s
, &znh
, 0, message
);
1157 nhg
= rib_get_fib_backup_nhg(re
);
1159 for (ALL_NEXTHOPS_PTR(nhg
, nh
))
1160 if (rnh_nexthop_valid(re
, nh
)) {
1161 zapi_nexthop_from_nexthop(&znh
, nh
);
1162 ret
= zapi_nexthop_encode(
1163 s
, &znh
, 0 /* flags */,
1172 stream_putc_at(s
, nump
, num
);
1174 stream_putc(s
, 0); // type
1175 stream_putw(s
, 0); // instance
1176 stream_putc(s
, 0); // distance
1177 stream_putl(s
, 0); // metric
1178 stream_putc(s
, 0); // nexthops
1180 stream_putw_at(s
, 0, stream_get_endp(s
));
1182 client
->nh_last_upd_time
= monotime(NULL
);
1183 client
->last_write_cmd
= cmd
;
1184 return zserv_send_message(client
, s
);
1192 static void print_nh(struct nexthop
*nexthop
, struct vty
*vty
)
1195 struct zebra_ns
*zns
= zebra_ns_lookup(nexthop
->vrf_id
);
1197 switch (nexthop
->type
) {
1198 case NEXTHOP_TYPE_IPV4
:
1199 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1200 vty_out(vty
, " via %pI4", &nexthop
->gate
.ipv4
);
1201 if (nexthop
->ifindex
)
1202 vty_out(vty
, ", %s",
1203 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
1205 case NEXTHOP_TYPE_IPV6
:
1206 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1208 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1209 if (nexthop
->ifindex
)
1210 vty_out(vty
, ", via %s",
1211 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
1213 case NEXTHOP_TYPE_IFINDEX
:
1214 vty_out(vty
, " is directly connected, %s",
1215 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
1217 case NEXTHOP_TYPE_BLACKHOLE
:
1218 vty_out(vty
, " is directly connected, Null0");
1226 static void print_rnh(struct route_node
*rn
, struct vty
*vty
)
1229 struct nexthop
*nexthop
;
1230 struct listnode
*node
;
1231 struct zserv
*client
;
1235 vty_out(vty
, "%s%s\n",
1236 inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
1237 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
) ? "(Connected)"
1240 vty_out(vty
, " resolved via %s\n",
1241 zebra_route_string(rnh
->state
->type
));
1242 for (nexthop
= rnh
->state
->nhe
->nhg
.nexthop
; nexthop
;
1243 nexthop
= nexthop
->next
)
1244 print_nh(nexthop
, vty
);
1246 vty_out(vty
, " unresolved%s\n",
1247 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)
1251 vty_out(vty
, " Client list:");
1252 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
))
1253 vty_out(vty
, " %s(fd %d)%s", zebra_route_string(client
->proto
),
1255 rnh
->filtered
[client
->proto
] ? "(filtered)" : "");
1256 if (!list_isempty(rnh
->zebra_pseudowire_list
))
1257 vty_out(vty
, " zebra[pseudowires]");
1261 static int zebra_cleanup_rnh_client(vrf_id_t vrf_id
, afi_t afi
,
1262 struct zserv
*client
, enum rnh_type type
)
1264 struct route_table
*ntable
;
1265 struct route_node
*nrn
;
1268 if (IS_ZEBRA_DEBUG_NHT
) {
1269 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
1272 "%s(%u): Client %s RNH cleanup for family %s type %s",
1273 VRF_LOGNAME(vrf
), vrf_id
,
1274 zebra_route_string(client
->proto
), afi2str(afi
),
1275 rnh_type2str(type
));
1278 ntable
= get_rnh_table(vrf_id
, afi
, type
);
1280 zlog_debug("cleanup_rnh_client: rnh table not found");
1284 for (nrn
= route_top(ntable
); nrn
; nrn
= route_next(nrn
)) {
1289 zebra_remove_rnh_client(rnh
, client
, type
);
1294 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
1295 static int zebra_client_cleanup_rnh(struct zserv
*client
)
1298 struct zebra_vrf
*zvrf
;
1300 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1303 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AFI_IP
, client
,
1305 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AFI_IP6
, client
,
1307 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AFI_IP
, client
,
1308 RNH_IMPORT_CHECK_TYPE
);
1309 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AFI_IP6
, client
,
1310 RNH_IMPORT_CHECK_TYPE
);
1317 int rnh_resolve_via_default(struct zebra_vrf
*zvrf
, int family
)
1319 if (((family
== AF_INET
) && zvrf
->zebra_rnh_ip_default_route
)
1320 || ((family
== AF_INET6
) && zvrf
->zebra_rnh_ipv6_default_route
))