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 char *rnh_str(struct rnh
*rnh
, char *buf
, int size
)
91 prefix2str(&(rnh
->node
->p
), buf
, size
);
95 static void zebra_rnh_remove_from_routing_table(struct rnh
*rnh
)
97 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(rnh
->vrf_id
);
98 struct route_table
*table
= zvrf
->table
[rnh
->afi
][SAFI_UNICAST
];
99 struct route_node
*rn
;
105 rn
= route_node_match(table
, &rnh
->resolved_route
);
109 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
110 char buf
[PREFIX_STRLEN
];
111 char buf1
[PREFIX_STRLEN
];
113 zlog_debug("%s: %u:%s removed from tracking on %s", __func__
,
115 prefix2str(&rnh
->node
->p
, buf
, sizeof(buf
)),
116 srcdest_rnode2str(rn
, buf1
, sizeof(buf
)));
119 dest
= rib_dest_from_rnode(rn
);
120 rnh_list_del(&dest
->nht
, rnh
);
121 route_unlock_node(rn
);
124 static void zebra_rnh_store_in_routing_table(struct rnh
*rnh
)
126 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(rnh
->vrf_id
);
127 struct route_table
*table
= zvrf
->table
[rnh
->afi
][SAFI_UNICAST
];
128 struct route_node
*rn
;
131 rn
= route_node_match(table
, &rnh
->resolved_route
);
135 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
136 char buf
[PREFIX_STRLEN
];
137 char buf1
[PREFIX_STRLEN
];
139 zlog_debug("%s: %u:%s added for tracking on %s", __func__
,
141 prefix2str(&rnh
->node
->p
, buf
, sizeof(buf
)),
142 srcdest_rnode2str(rn
, buf1
, sizeof(buf
)));
145 dest
= rib_dest_from_rnode(rn
);
146 rnh_list_add_tail(&dest
->nht
, rnh
);
147 route_unlock_node(rn
);
150 struct rnh
*zebra_add_rnh(struct prefix
*p
, vrf_id_t vrfid
, enum rnh_type type
,
153 struct route_table
*table
;
154 struct route_node
*rn
;
155 struct rnh
*rnh
= NULL
;
156 char buf
[PREFIX2STR_BUFFER
];
157 afi_t afi
= family2afi(p
->family
);
159 if (IS_ZEBRA_DEBUG_NHT
) {
160 prefix2str(p
, buf
, sizeof(buf
));
161 zlog_debug("%u: Add RNH %s type %s", vrfid
, buf
,
164 table
= get_rnh_table(vrfid
, afi
, type
);
166 prefix2str(p
, buf
, sizeof(buf
));
167 flog_warn(EC_ZEBRA_RNH_NO_TABLE
,
168 "%u: Add RNH %s type %s - table not found", vrfid
,
169 buf
, rnh_type2str(type
));
174 /* Make it sure prefixlen is applied to the prefix. */
177 /* Lookup (or add) route node.*/
178 rn
= route_node_get(table
, p
);
181 rnh
= XCALLOC(MTYPE_RNH
, sizeof(struct rnh
));
184 * The resolved route is already 0.0.0.0/0 or
185 * 0::0/0 due to the calloc right above, but
186 * we should set the family so that future
187 * comparisons can just be done
189 rnh
->resolved_route
.family
= p
->family
;
190 rnh
->client_list
= list_new();
195 rnh
->zebra_pseudowire_list
= list_new();
201 zebra_rnh_store_in_routing_table(rnh
);
205 route_unlock_node(rn
);
209 struct rnh
*zebra_lookup_rnh(struct prefix
*p
, vrf_id_t vrfid
,
212 struct route_table
*table
;
213 struct route_node
*rn
;
215 table
= get_rnh_table(vrfid
, family2afi(PREFIX_FAMILY(p
)), type
);
219 /* Make it sure prefixlen is applied to the prefix. */
222 /* Lookup route node.*/
223 rn
= route_node_lookup(table
, p
);
227 route_unlock_node(rn
);
231 void zebra_free_rnh(struct rnh
*rnh
)
233 struct zebra_vrf
*zvrf
;
234 struct route_table
*table
;
236 zebra_rnh_remove_from_routing_table(rnh
);
237 rnh
->flags
|= ZEBRA_NHT_DELETED
;
238 list_delete(&rnh
->client_list
);
239 list_delete(&rnh
->zebra_pseudowire_list
);
241 zvrf
= zebra_vrf_lookup_by_id(rnh
->vrf_id
);
242 table
= zvrf
->table
[family2afi(rnh
->resolved_route
.family
)][SAFI_UNICAST
];
245 struct route_node
*rern
;
247 rern
= route_node_match(table
, &rnh
->resolved_route
);
251 route_unlock_node(rern
);
253 dest
= rib_dest_from_rnode(rern
);
254 rnh_list_del(&dest
->nht
, rnh
);
257 free_state(rnh
->vrf_id
, rnh
->state
, rnh
->node
);
258 XFREE(MTYPE_RNH
, rnh
);
261 static void zebra_delete_rnh(struct rnh
*rnh
, enum rnh_type type
)
263 struct route_node
*rn
;
265 if (!list_isempty(rnh
->client_list
)
266 || !list_isempty(rnh
->zebra_pseudowire_list
))
269 if ((rnh
->flags
& ZEBRA_NHT_DELETED
) || !(rn
= rnh
->node
))
272 if (IS_ZEBRA_DEBUG_NHT
) {
273 char buf
[PREFIX2STR_BUFFER
];
274 zlog_debug("%u: Del RNH %s type %s", rnh
->vrf_id
,
275 rnh_str(rnh
, buf
, sizeof(buf
)), rnh_type2str(type
));
280 route_unlock_node(rn
);
284 * This code will send to the registering client
286 * For a rnh that was created, there is no data
287 * so it will send an empty nexthop group
288 * If rnh exists then we know it has been evaluated
289 * and as such it will have a resolved rnh.
291 void zebra_add_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
292 enum rnh_type type
, vrf_id_t vrf_id
)
294 if (IS_ZEBRA_DEBUG_NHT
) {
295 char buf
[PREFIX2STR_BUFFER
];
296 zlog_debug("%u: Client %s registers for RNH %s type %s", vrf_id
,
297 zebra_route_string(client
->proto
),
298 rnh_str(rnh
, buf
, sizeof(buf
)), rnh_type2str(type
));
300 if (!listnode_lookup(rnh
->client_list
, client
))
301 listnode_add(rnh
->client_list
, client
);
304 * We always need to respond with known information,
305 * currently multiple daemons expect this behavior
307 zebra_send_rnh_update(rnh
, client
, type
, vrf_id
, 0);
310 void zebra_remove_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
313 if (IS_ZEBRA_DEBUG_NHT
) {
314 char buf
[PREFIX2STR_BUFFER
];
315 zlog_debug("Client %s unregisters for RNH %s type %s",
316 zebra_route_string(client
->proto
),
317 rnh_str(rnh
, buf
, sizeof(buf
)), rnh_type2str(type
));
319 listnode_delete(rnh
->client_list
, client
);
320 zebra_delete_rnh(rnh
, type
);
323 /* XXX move this utility function elsewhere? */
324 static void addr2hostprefix(int af
, const union g_addr
*addr
,
325 struct prefix
*prefix
)
329 prefix
->family
= AF_INET
;
330 prefix
->prefixlen
= IPV4_MAX_BITLEN
;
331 prefix
->u
.prefix4
= addr
->ipv4
;
334 prefix
->family
= AF_INET6
;
335 prefix
->prefixlen
= IPV6_MAX_BITLEN
;
336 prefix
->u
.prefix6
= addr
->ipv6
;
339 memset(prefix
, 0, sizeof(*prefix
));
340 zlog_warn("%s: unknown address family %d", __func__
, af
);
345 void zebra_register_rnh_pseudowire(vrf_id_t vrf_id
, struct zebra_pw
*pw
,
351 struct zebra_vrf
*zvrf
;
355 zvrf
= vrf_info_lookup(vrf_id
);
359 addr2hostprefix(pw
->af
, &pw
->nexthop
, &nh
);
360 rnh
= zebra_add_rnh(&nh
, vrf_id
, RNH_NEXTHOP_TYPE
, &exists
);
364 if (!listnode_lookup(rnh
->zebra_pseudowire_list
, pw
)) {
365 listnode_add(rnh
->zebra_pseudowire_list
, pw
);
367 zebra_evaluate_rnh(zvrf
, family2afi(pw
->af
), 1,
368 RNH_NEXTHOP_TYPE
, &nh
);
373 void zebra_deregister_rnh_pseudowire(vrf_id_t vrf_id
, struct zebra_pw
*pw
)
381 listnode_delete(rnh
->zebra_pseudowire_list
, pw
);
384 zebra_delete_rnh(rnh
, RNH_NEXTHOP_TYPE
);
387 /* Clear the NEXTHOP_FLAG_RNH_FILTERED flags on all nexthops
389 static void zebra_rnh_clear_nexthop_rnh_filters(struct route_entry
*re
)
391 struct nexthop
*nexthop
;
394 for (nexthop
= re
->nhe
->nhg
.nexthop
; nexthop
;
395 nexthop
= nexthop
->next
) {
396 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RNH_FILTERED
);
401 /* Apply the NHT route-map for a client to the route (and nexthops)
404 static int zebra_rnh_apply_nht_rmap(afi_t afi
, struct zebra_vrf
*zvrf
,
405 struct route_node
*prn
,
406 struct route_entry
*re
, int proto
)
408 int at_least_one
= 0;
409 struct nexthop
*nexthop
;
410 route_map_result_t ret
;
413 for (nexthop
= re
->nhe
->nhg
.nexthop
; nexthop
;
414 nexthop
= nexthop
->next
) {
415 ret
= zebra_nht_route_map_check(
416 afi
, proto
, &prn
->p
, zvrf
, re
, nexthop
);
417 if (ret
!= RMAP_DENYMATCH
)
418 at_least_one
++; /* at least one valid NH */
420 SET_FLAG(nexthop
->flags
,
421 NEXTHOP_FLAG_RNH_FILTERED
);
425 return (at_least_one
);
429 * Determine appropriate route (RE entry) resolving a tracked BGP route
430 * for BGP route for import.
432 static struct route_entry
*
433 zebra_rnh_resolve_import_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
434 struct route_node
*nrn
, struct rnh
*rnh
,
435 struct route_node
**prn
)
437 struct route_table
*route_table
;
438 struct route_node
*rn
;
439 struct route_entry
*re
;
443 route_table
= zvrf
->table
[afi
][SAFI_UNICAST
];
444 if (!route_table
) // unexpected
447 rn
= route_node_match(route_table
, &nrn
->p
);
451 /* Unlock route node - we don't need to lock when walking the tree. */
452 route_unlock_node(rn
);
454 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
)
455 && !prefix_same(&nrn
->p
, &rn
->p
))
458 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
459 char buf
[PREFIX_STRLEN
];
460 char buf1
[SRCDEST2STR_BUFFER
];
462 zlog_debug("%s: %u:%s Resolved Import Entry to %s", __func__
,
464 prefix2str(&rnh
->node
->p
, buf
, sizeof(buf
)),
465 srcdest_rnode2str(rn
, buf1
, sizeof(buf1
)));
468 /* Identify appropriate route entry. */
469 RNODE_FOREACH_RE (rn
, re
) {
470 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
471 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
472 && !CHECK_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
)
473 && (re
->type
!= ZEBRA_ROUTE_BGP
))
480 if (!re
&& IS_ZEBRA_DEBUG_NHT_DETAILED
)
481 zlog_debug(" Rejected due to removed or is a bgp route");
487 * See if a tracked route entry for import (by BGP) has undergone any
488 * change, and if so, notify the client.
490 static void zebra_rnh_eval_import_check_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
491 int force
, struct route_node
*nrn
,
493 struct route_node
*prn
,
494 struct route_entry
*re
)
496 int state_changed
= 0;
497 struct zserv
*client
;
498 char bufn
[INET6_ADDRSTRLEN
];
499 struct listnode
*node
;
501 zebra_rnh_remove_from_routing_table(rnh
);
503 prefix_copy(&rnh
->resolved_route
, &prn
->p
);
505 int family
= rnh
->resolved_route
.family
;
507 memset(&rnh
->resolved_route
.family
, 0, sizeof(struct prefix
));
508 rnh
->resolved_route
.family
= family
;
510 zebra_rnh_store_in_routing_table(rnh
);
512 if (re
&& (rnh
->state
== NULL
)) {
513 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
515 } else if (!re
&& (rnh
->state
!= NULL
))
518 if (compare_state(re
, rnh
->state
)) {
519 copy_state(rnh
, re
, nrn
);
523 if (state_changed
|| force
) {
524 if (IS_ZEBRA_DEBUG_NHT
) {
525 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
526 zlog_debug("%u:%s: Route import check %s %s",
528 bufn
, rnh
->state
? "passed" : "failed",
529 state_changed
? "(state changed)" : "");
531 /* state changed, notify clients */
532 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
533 zebra_send_rnh_update(rnh
, client
,
534 RNH_IMPORT_CHECK_TYPE
,
535 zvrf
->vrf
->vrf_id
, 0);
541 * Notify clients registered for this nexthop about a change.
543 static void zebra_rnh_notify_protocol_clients(struct zebra_vrf
*zvrf
, afi_t afi
,
544 struct route_node
*nrn
,
546 struct route_node
*prn
,
547 struct route_entry
*re
)
549 struct listnode
*node
;
550 struct zserv
*client
;
551 char bufn
[INET6_ADDRSTRLEN
];
552 char bufp
[INET6_ADDRSTRLEN
];
553 int num_resolving_nh
;
555 if (IS_ZEBRA_DEBUG_NHT
) {
556 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
558 srcdest_rnode2str(prn
, bufp
, INET6_ADDRSTRLEN
);
559 zlog_debug("%u:%s: NH resolved over route %s",
560 zvrf
->vrf
->vrf_id
, bufn
, bufp
);
562 zlog_debug("%u:%s: NH has become unresolved",
563 zvrf
->vrf
->vrf_id
, bufn
);
566 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
568 /* Apply route-map for this client to route resolving
570 * nexthop to see if it is filtered or not.
572 zebra_rnh_clear_nexthop_rnh_filters(re
);
573 num_resolving_nh
= zebra_rnh_apply_nht_rmap(
574 afi
, zvrf
, prn
, re
, client
->proto
);
575 if (num_resolving_nh
)
576 rnh
->filtered
[client
->proto
] = 0;
578 rnh
->filtered
[client
->proto
] = 1;
580 if (IS_ZEBRA_DEBUG_NHT
)
582 "%u:%s: Notifying client %s about NH %s",
583 zvrf
->vrf
->vrf_id
, bufn
,
584 zebra_route_string(client
->proto
),
587 : "(filtered by route-map)");
589 rnh
->filtered
[client
->proto
] = 0;
590 if (IS_ZEBRA_DEBUG_NHT
)
592 "%u:%s: Notifying client %s about NH (unreachable)",
593 zvrf
->vrf
->vrf_id
, bufn
,
594 zebra_route_string(client
->proto
));
597 zebra_send_rnh_update(rnh
, client
, RNH_NEXTHOP_TYPE
,
598 zvrf
->vrf
->vrf_id
, 0);
602 zebra_rnh_clear_nexthop_rnh_filters(re
);
606 * Utility to determine whether a candidate nexthop is useable. We make this
607 * check in a couple of places, so this is a single home for the logic we
610 static bool rnh_nexthop_valid(const struct route_entry
*re
,
611 const struct nexthop
*nh
)
613 return (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
614 && CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ACTIVE
)
615 && !CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
)
616 && !CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_DUPLICATE
)
617 && !CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RNH_FILTERED
));
621 * Determine appropriate route (route entry) resolving a tracked
624 static struct route_entry
*
625 zebra_rnh_resolve_nexthop_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
626 struct route_node
*nrn
, struct rnh
*rnh
,
627 struct route_node
**prn
)
629 struct route_table
*route_table
;
630 struct route_node
*rn
;
631 struct route_entry
*re
;
632 struct nexthop
*nexthop
;
636 route_table
= zvrf
->table
[afi
][SAFI_UNICAST
];
640 rn
= route_node_match(route_table
, &nrn
->p
);
644 /* Unlock route node - we don't need to lock when walking the tree. */
645 route_unlock_node(rn
);
647 /* While resolving nexthops, we may need to walk up the tree from the
648 * most-specific match. Do similar logic as in zebra_rib.c
651 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
652 char buf
[PREFIX_STRLEN
];
653 char buf1
[PREFIX_STRLEN
];
655 zlog_debug("%s: %u:%s Possible Match to %s", __func__
,
657 prefix2str(&rnh
->node
->p
, buf
, sizeof(buf
)),
658 srcdest_rnode2str(rn
, buf1
, sizeof(buf
)));
661 /* Do not resolve over default route unless allowed &&
662 * match route to be exact if so specified
664 if (is_default_prefix(&rn
->p
)
665 && !rnh_resolve_via_default(zvrf
, rn
->p
.family
)) {
666 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
668 " Not allowed to resolve through default prefix");
672 /* Identify appropriate route entry. */
673 RNODE_FOREACH_RE (rn
, re
) {
674 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
675 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
677 " Route Entry %s removed",
678 zebra_route_string(re
->type
));
681 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
) &&
682 !CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
683 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
685 " Route Entry %s !selected",
686 zebra_route_string(re
->type
));
690 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
)) {
691 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
693 " Route Entry %s queued",
694 zebra_route_string(re
->type
));
698 /* Just being SELECTED isn't quite enough - must
699 * have an installed nexthop to be useful.
701 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
)) {
702 if (rnh_nexthop_valid(re
, nexthop
))
706 if (nexthop
== NULL
) {
707 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
709 " Route Entry %s no nexthops",
710 zebra_route_string(re
->type
));
714 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)) {
715 if ((re
->type
== ZEBRA_ROUTE_CONNECT
)
716 || (re
->type
== ZEBRA_ROUTE_STATIC
))
718 if (re
->type
== ZEBRA_ROUTE_NHRP
) {
720 for (nexthop
= re
->nhe
->nhg
.nexthop
;
722 nexthop
= nexthop
->next
)
724 == NEXTHOP_TYPE_IFINDEX
)
733 /* Route entry found, we're done; else, walk up the tree. */
739 if (!CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
742 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
744 " Nexthop must be connected, cannot recurse up");
752 static void zebra_rnh_process_pseudowires(vrf_id_t vrfid
, struct rnh
*rnh
)
755 struct listnode
*node
;
757 for (ALL_LIST_ELEMENTS_RO(rnh
->zebra_pseudowire_list
, node
, pw
))
762 * See if a tracked nexthop entry has undergone any change, and if so,
763 * take appropriate action; this involves notifying any clients and/or
764 * scheduling dependent static routes for processing.
766 static void zebra_rnh_eval_nexthop_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
767 int force
, struct route_node
*nrn
,
769 struct route_node
*prn
,
770 struct route_entry
*re
)
772 int state_changed
= 0;
774 /* If we're resolving over a different route, resolution has changed or
775 * the resolving route has some change (e.g., metric), there is a state
778 zebra_rnh_remove_from_routing_table(rnh
);
779 if (!prefix_same(&rnh
->resolved_route
, prn
? &prn
->p
: NULL
)) {
781 prefix_copy(&rnh
->resolved_route
, &prn
->p
);
784 * Just quickly store the family of the resolved
785 * route so that we can reset it in a second here
787 int family
= rnh
->resolved_route
.family
;
789 memset(&rnh
->resolved_route
, 0, sizeof(struct prefix
));
790 rnh
->resolved_route
.family
= family
;
793 copy_state(rnh
, re
, nrn
);
795 } else if (compare_state(re
, rnh
->state
)) {
796 copy_state(rnh
, re
, nrn
);
799 zebra_rnh_store_in_routing_table(rnh
);
801 if (state_changed
|| force
) {
802 /* NOTE: Use the "copy" of resolving route stored in 'rnh' i.e.,
805 /* Notify registered protocol clients. */
806 zebra_rnh_notify_protocol_clients(zvrf
, afi
, nrn
, rnh
, prn
,
809 /* Process pseudowires attached to this nexthop */
810 zebra_rnh_process_pseudowires(zvrf
->vrf
->vrf_id
, rnh
);
814 /* Evaluate one tracked entry */
815 static void zebra_rnh_evaluate_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
816 int force
, enum rnh_type type
,
817 struct route_node
*nrn
)
820 struct route_entry
*re
;
821 struct route_node
*prn
;
822 char bufn
[INET6_ADDRSTRLEN
];
824 if (IS_ZEBRA_DEBUG_NHT
) {
825 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
826 zlog_debug("%u:%s: Evaluate RNH, type %s %s", zvrf
->vrf
->vrf_id
,
827 bufn
, rnh_type2str(type
), force
? "(force)" : "");
832 /* Identify route entry (RE) resolving this tracked entry. */
833 if (type
== RNH_IMPORT_CHECK_TYPE
)
834 re
= zebra_rnh_resolve_import_entry(zvrf
, afi
, nrn
, rnh
, &prn
);
836 re
= zebra_rnh_resolve_nexthop_entry(zvrf
, afi
, nrn
, rnh
, &prn
);
838 /* If the entry cannot be resolved and that is also the existing state,
839 * there is nothing further to do.
841 if (!re
&& rnh
->state
== NULL
&& !force
)
844 /* Process based on type of entry. */
845 if (type
== RNH_IMPORT_CHECK_TYPE
)
846 zebra_rnh_eval_import_check_entry(zvrf
, afi
, force
, nrn
, rnh
,
849 zebra_rnh_eval_nexthop_entry(zvrf
, afi
, force
, nrn
, rnh
, prn
,
854 * Clear the ROUTE_ENTRY_NEXTHOPS_CHANGED flag
855 * from the re entries.
857 * Please note we are doing this *after* we have
858 * notified the world about each nexthop as that
859 * we can have a situation where one re entry
860 * covers multiple nexthops we are interested in.
862 static void zebra_rnh_clear_nhc_flag(struct zebra_vrf
*zvrf
, afi_t afi
,
863 enum rnh_type type
, struct route_node
*nrn
)
866 struct route_entry
*re
;
867 struct route_node
*prn
;
871 /* Identify route entry (RIB) resolving this tracked entry. */
872 if (type
== RNH_IMPORT_CHECK_TYPE
)
873 re
= zebra_rnh_resolve_import_entry(zvrf
, afi
, nrn
, rnh
,
876 re
= zebra_rnh_resolve_nexthop_entry(zvrf
, afi
, nrn
, rnh
,
880 UNSET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
883 /* Evaluate all tracked entries (nexthops or routes for import into BGP)
884 * of a particular VRF and address-family or a specific prefix.
886 void zebra_evaluate_rnh(struct zebra_vrf
*zvrf
, afi_t afi
, int force
,
887 enum rnh_type type
, struct prefix
*p
)
889 struct route_table
*rnh_table
;
890 struct route_node
*nrn
;
892 rnh_table
= get_rnh_table(zvrf
->vrf
->vrf_id
, afi
, type
);
893 if (!rnh_table
) // unexpected
897 /* Evaluating a specific entry, make sure it exists. */
898 nrn
= route_node_lookup(rnh_table
, p
);
899 if (nrn
&& nrn
->info
)
900 zebra_rnh_evaluate_entry(zvrf
, afi
, force
, type
, nrn
);
903 route_unlock_node(nrn
);
905 /* Evaluate entire table. */
906 nrn
= route_top(rnh_table
);
909 zebra_rnh_evaluate_entry(zvrf
, afi
, force
, type
,
911 nrn
= route_next(nrn
); /* this will also unlock nrn */
913 nrn
= route_top(rnh_table
);
916 zebra_rnh_clear_nhc_flag(zvrf
, afi
, type
, nrn
);
917 nrn
= route_next(nrn
); /* this will also unlock nrn */
922 void zebra_print_rnh_table(vrf_id_t vrfid
, afi_t afi
, struct vty
*vty
,
923 enum rnh_type type
, struct prefix
*p
)
925 struct route_table
*table
;
926 struct route_node
*rn
;
928 table
= get_rnh_table(vrfid
, afi
, type
);
930 if (IS_ZEBRA_DEBUG_NHT
)
931 zlog_debug("print_rnhs: rnh table not found");
935 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
936 if (p
&& !prefix_match(&rn
->p
, p
))
945 * free_state - free up the re structure associated with the rnh.
947 static void free_state(vrf_id_t vrf_id
, struct route_entry
*re
,
948 struct route_node
*rn
)
953 /* free RE and nexthops */
954 zebra_nhg_free(re
->nhe
);
958 static void copy_state(struct rnh
*rnh
, const struct route_entry
*re
,
959 struct route_node
*rn
)
961 struct route_entry
*state
;
964 free_state(rnh
->vrf_id
, rnh
->state
, rn
);
971 state
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
972 state
->type
= re
->type
;
973 state
->distance
= re
->distance
;
974 state
->metric
= re
->metric
;
975 state
->vrf_id
= re
->vrf_id
;
976 state
->status
= re
->status
;
978 state
->nhe
= zebra_nhe_copy(re
->nhe
, 0);
980 /* Copy the 'fib' nexthops also, if present - we want to capture
981 * the true installed nexthops.
983 if (re
->fib_ng
.nexthop
)
984 nexthop_group_copy(&state
->fib_ng
, &re
->fib_ng
);
985 if (re
->fib_backup_ng
.nexthop
)
986 nexthop_group_copy(&state
->fib_backup_ng
, &re
->fib_backup_ng
);
992 * Compare two route_entries' nexthops.
994 static bool compare_valid_nexthops(struct route_entry
*r1
,
995 struct route_entry
*r2
)
997 bool matched_p
= false;
998 struct nexthop_group
*nhg1
, *nhg2
;
999 struct nexthop
*nh1
, *nh2
;
1001 /* Account for backup nexthops and for the 'fib' nexthop lists,
1004 nhg1
= rib_get_fib_nhg(r1
);
1005 nhg2
= rib_get_fib_nhg(r2
);
1007 nh1
= nhg1
->nexthop
;
1008 nh2
= nhg2
->nexthop
;
1011 /* Find each list's next valid nexthop */
1012 while ((nh1
!= NULL
) && !rnh_nexthop_valid(r1
, nh1
))
1013 nh1
= nexthop_next(nh1
);
1015 while ((nh2
!= NULL
) && !rnh_nexthop_valid(r2
, nh2
))
1016 nh2
= nexthop_next(nh2
);
1019 /* Any difference is a no-match */
1020 if (nexthop_cmp(nh1
, nh2
) != 0) {
1021 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1022 zlog_debug("%s: nh1, nh2 differ",
1027 nh1
= nexthop_next(nh1
);
1028 nh2
= nexthop_next(nh2
);
1029 } else if (nh1
|| nh2
) {
1030 /* One list has more valid nexthops than the other */
1031 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1032 zlog_debug("%s: nh1 %s, nh2 %s", __func__
,
1033 nh1
? "non-NULL" : "NULL",
1034 nh2
? "non-NULL" : "NULL");
1037 break; /* Done with both lists */
1040 /* The test for the backups is slightly different: the only installed
1041 * backups will be in the 'fib' list.
1043 nhg1
= rib_get_fib_backup_nhg(r1
);
1044 nhg2
= rib_get_fib_backup_nhg(r2
);
1046 nh1
= nhg1
->nexthop
;
1047 nh2
= nhg2
->nexthop
;
1050 /* Find each backup list's next valid nexthop */
1051 while ((nh1
!= NULL
) && !rnh_nexthop_valid(r1
, nh1
))
1052 nh1
= nexthop_next(nh1
);
1054 while ((nh2
!= NULL
) && !rnh_nexthop_valid(r2
, nh2
))
1055 nh2
= nexthop_next(nh2
);
1058 /* Any difference is a no-match */
1059 if (nexthop_cmp(nh1
, nh2
) != 0) {
1060 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1061 zlog_debug("%s: backup nh1, nh2 differ",
1066 nh1
= nexthop_next(nh1
);
1067 nh2
= nexthop_next(nh2
);
1068 } else if (nh1
|| nh2
) {
1069 /* One list has more valid nexthops than the other */
1070 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1071 zlog_debug("%s: backup nh1 %s, nh2 %s",
1073 nh1
? "non-NULL" : "NULL",
1074 nh2
? "non-NULL" : "NULL");
1077 break; /* Done with both lists */
1080 /* Well, it's a match */
1081 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1082 zlog_debug("%s: matched", __func__
);
1091 static int compare_state(struct route_entry
*r1
, struct route_entry
*r2
)
1096 if ((!r1
&& r2
) || (r1
&& !r2
))
1099 if (r1
->distance
!= r2
->distance
)
1102 if (r1
->metric
!= r2
->metric
)
1105 if (!compare_valid_nexthops(r1
, r2
))
1111 int zebra_send_rnh_update(struct rnh
*rnh
, struct zserv
*client
,
1112 enum rnh_type type
, vrf_id_t vrf_id
,
1113 uint32_t srte_color
)
1115 struct stream
*s
= NULL
;
1116 struct route_entry
*re
;
1120 struct route_node
*rn
;
1122 uint32_t message
= 0;
1123 int cmd
= (type
== RNH_IMPORT_CHECK_TYPE
) ? ZEBRA_IMPORT_CHECK_UPDATE
1124 : ZEBRA_NEXTHOP_UPDATE
;
1129 /* Get output stream. */
1130 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1132 zclient_create_header(s
, cmd
, vrf_id
);
1134 /* Message flags. */
1136 SET_FLAG(message
, ZAPI_MESSAGE_SRTE
);
1137 stream_putl(s
, message
);
1139 stream_putw(s
, rn
->p
.family
);
1140 switch (rn
->p
.family
) {
1142 stream_putc(s
, rn
->p
.prefixlen
);
1143 stream_put_in_addr(s
, &rn
->p
.u
.prefix4
);
1146 stream_putc(s
, rn
->p
.prefixlen
);
1147 stream_put(s
, &rn
->p
.u
.prefix6
, IPV6_MAX_BYTELEN
);
1150 flog_err(EC_ZEBRA_RNH_UNKNOWN_FAMILY
,
1151 "%s: Unknown family (%d) notification attempted\n",
1152 __func__
, rn
->p
.family
);
1156 stream_putl(s
, srte_color
);
1159 struct zapi_nexthop znh
;
1160 struct nexthop_group
*nhg
;
1162 stream_putc(s
, re
->type
);
1163 stream_putw(s
, re
->instance
);
1164 stream_putc(s
, re
->distance
);
1165 stream_putl(s
, re
->metric
);
1167 nump
= stream_get_endp(s
);
1170 nhg
= rib_get_fib_nhg(re
);
1171 for (ALL_NEXTHOPS_PTR(nhg
, nh
))
1172 if (rnh_nexthop_valid(re
, nh
)) {
1173 zapi_nexthop_from_nexthop(&znh
, nh
);
1174 ret
= zapi_nexthop_encode(s
, &znh
, 0, message
);
1181 nhg
= rib_get_fib_backup_nhg(re
);
1183 for (ALL_NEXTHOPS_PTR(nhg
, nh
))
1184 if (rnh_nexthop_valid(re
, nh
)) {
1185 zapi_nexthop_from_nexthop(&znh
, nh
);
1186 ret
= zapi_nexthop_encode(
1187 s
, &znh
, 0 /* flags */,
1196 stream_putc_at(s
, nump
, num
);
1198 stream_putc(s
, 0); // type
1199 stream_putw(s
, 0); // instance
1200 stream_putc(s
, 0); // distance
1201 stream_putl(s
, 0); // metric
1202 stream_putc(s
, 0); // nexthops
1204 stream_putw_at(s
, 0, stream_get_endp(s
));
1206 client
->nh_last_upd_time
= monotime(NULL
);
1207 client
->last_write_cmd
= cmd
;
1208 return zserv_send_message(client
, s
);
1216 static void print_nh(struct nexthop
*nexthop
, struct vty
*vty
)
1219 struct zebra_ns
*zns
= zebra_ns_lookup(nexthop
->vrf_id
);
1221 switch (nexthop
->type
) {
1222 case NEXTHOP_TYPE_IPV4
:
1223 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1224 vty_out(vty
, " via %s", inet_ntoa(nexthop
->gate
.ipv4
));
1225 if (nexthop
->ifindex
)
1226 vty_out(vty
, ", %s",
1227 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
1229 case NEXTHOP_TYPE_IPV6
:
1230 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1232 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1233 if (nexthop
->ifindex
)
1234 vty_out(vty
, ", via %s",
1235 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
1237 case NEXTHOP_TYPE_IFINDEX
:
1238 vty_out(vty
, " is directly connected, %s",
1239 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
1241 case NEXTHOP_TYPE_BLACKHOLE
:
1242 vty_out(vty
, " is directly connected, Null0");
1250 static void print_rnh(struct route_node
*rn
, struct vty
*vty
)
1253 struct nexthop
*nexthop
;
1254 struct listnode
*node
;
1255 struct zserv
*client
;
1259 vty_out(vty
, "%s%s\n",
1260 inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
1261 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
) ? "(Connected)"
1264 vty_out(vty
, " resolved via %s\n",
1265 zebra_route_string(rnh
->state
->type
));
1266 for (nexthop
= rnh
->state
->nhe
->nhg
.nexthop
; nexthop
;
1267 nexthop
= nexthop
->next
)
1268 print_nh(nexthop
, vty
);
1270 vty_out(vty
, " unresolved%s\n",
1271 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)
1275 vty_out(vty
, " Client list:");
1276 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
))
1277 vty_out(vty
, " %s(fd %d)%s", zebra_route_string(client
->proto
),
1279 rnh
->filtered
[client
->proto
] ? "(filtered)" : "");
1280 if (!list_isempty(rnh
->zebra_pseudowire_list
))
1281 vty_out(vty
, " zebra[pseudowires]");
1285 static int zebra_cleanup_rnh_client(vrf_id_t vrf_id
, afi_t afi
,
1286 struct zserv
*client
, enum rnh_type type
)
1288 struct route_table
*ntable
;
1289 struct route_node
*nrn
;
1292 if (IS_ZEBRA_DEBUG_NHT
)
1293 zlog_debug("%u: Client %s RNH cleanup for family %s type %s",
1294 vrf_id
, zebra_route_string(client
->proto
),
1295 afi2str(afi
), rnh_type2str(type
));
1297 ntable
= get_rnh_table(vrf_id
, afi
, type
);
1299 zlog_debug("cleanup_rnh_client: rnh table not found");
1303 for (nrn
= route_top(ntable
); nrn
; nrn
= route_next(nrn
)) {
1308 zebra_remove_rnh_client(rnh
, client
, type
);
1313 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
1314 static int zebra_client_cleanup_rnh(struct zserv
*client
)
1317 struct zebra_vrf
*zvrf
;
1319 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1322 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AFI_IP
, client
,
1324 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AFI_IP6
, client
,
1326 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AFI_IP
, client
,
1327 RNH_IMPORT_CHECK_TYPE
);
1328 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AFI_IP6
, client
,
1329 RNH_IMPORT_CHECK_TYPE
);
1336 int rnh_resolve_via_default(struct zebra_vrf
*zvrf
, int family
)
1338 if (((family
== AF_INET
) && zvrf
->zebra_rnh_ip_default_route
)
1339 || ((family
== AF_INET6
) && zvrf
->zebra_rnh_ipv6_default_route
))