1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Zebra Nexthop Group Code.
3 * Copyright (C) 2019 Cumulus Networks, Inc.
9 #include "lib/nexthop.h"
10 #include "lib/nexthop_group_private.h"
11 #include "lib/routemap.h"
13 #include "lib/jhash.h"
14 #include "lib/debug.h"
15 #include "lib/lib_errors.h"
17 #include "zebra/connected.h"
18 #include "zebra/debug.h"
19 #include "zebra/zebra_router.h"
20 #include "zebra/zebra_nhg_private.h"
21 #include "zebra/zebra_rnh.h"
22 #include "zebra/zebra_routemap.h"
23 #include "zebra/zebra_srte.h"
24 #include "zebra/zserv.h"
26 #include "zebra_errors.h"
27 #include "zebra_dplane.h"
28 #include "zebra/interface.h"
29 #include "zebra/zapi_msg.h"
30 #include "zebra/rib.h"
31 #include "zebra/zebra_vxlan.h"
33 DEFINE_MTYPE_STATIC(ZEBRA
, NHG
, "Nexthop Group Entry");
34 DEFINE_MTYPE_STATIC(ZEBRA
, NHG_CONNECTED
, "Nexthop Group Connected");
35 DEFINE_MTYPE_STATIC(ZEBRA
, NHG_CTX
, "Nexthop Group Context");
37 /* Map backup nexthop indices between two nhes */
38 struct backup_nh_map_s
{
47 /* id counter to keep in sync with kernel */
50 /* Controlled through ui */
51 static bool g_nexthops_enabled
= true;
52 static bool proto_nexthops_only
;
53 static bool use_recursive_backups
= true;
55 static struct nhg_hash_entry
*depends_find(const struct nexthop
*nh
, afi_t afi
,
56 int type
, bool from_dplane
);
57 static void depends_add(struct nhg_connected_tree_head
*head
,
58 struct nhg_hash_entry
*depend
);
59 static struct nhg_hash_entry
*
60 depends_find_add(struct nhg_connected_tree_head
*head
, struct nexthop
*nh
,
61 afi_t afi
, int type
, bool from_dplane
);
62 static struct nhg_hash_entry
*
63 depends_find_id_add(struct nhg_connected_tree_head
*head
, uint32_t id
);
64 static void depends_decrement_free(struct nhg_connected_tree_head
*head
);
66 static struct nhg_backup_info
*
67 nhg_backup_copy(const struct nhg_backup_info
*orig
);
69 /* Helper function for getting the next allocatable ID */
70 static uint32_t nhg_get_next_id(void)
75 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
76 zlog_debug("%s: ID %u checking", __func__
, id_counter
);
78 if (id_counter
== ZEBRA_NHG_PROTO_LOWER
) {
79 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
80 zlog_debug("%s: ID counter wrapped", __func__
);
86 if (zebra_nhg_lookup_id(id_counter
)) {
87 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
88 zlog_debug("%s: ID already exists", __func__
);
99 static void nhg_connected_free(struct nhg_connected
*dep
)
101 XFREE(MTYPE_NHG_CONNECTED
, dep
);
104 static struct nhg_connected
*nhg_connected_new(struct nhg_hash_entry
*nhe
)
106 struct nhg_connected
*new = NULL
;
108 new = XCALLOC(MTYPE_NHG_CONNECTED
, sizeof(struct nhg_connected
));
114 void nhg_connected_tree_free(struct nhg_connected_tree_head
*head
)
116 struct nhg_connected
*rb_node_dep
= NULL
;
118 if (!nhg_connected_tree_is_empty(head
)) {
119 frr_each_safe(nhg_connected_tree
, head
, rb_node_dep
) {
120 nhg_connected_tree_del(head
, rb_node_dep
);
121 nhg_connected_free(rb_node_dep
);
126 bool nhg_connected_tree_is_empty(const struct nhg_connected_tree_head
*head
)
128 return nhg_connected_tree_count(head
) ? false : true;
131 struct nhg_connected
*
132 nhg_connected_tree_root(struct nhg_connected_tree_head
*head
)
134 return nhg_connected_tree_first(head
);
137 struct nhg_hash_entry
*
138 nhg_connected_tree_del_nhe(struct nhg_connected_tree_head
*head
,
139 struct nhg_hash_entry
*depend
)
141 struct nhg_connected lookup
= {};
142 struct nhg_connected
*remove
= NULL
;
143 struct nhg_hash_entry
*removed_nhe
;
147 /* Lookup to find the element, then remove it */
148 remove
= nhg_connected_tree_find(head
, &lookup
);
150 /* Re-returning here just in case this API changes..
151 * the _del list api's are a bit undefined at the moment.
153 * So hopefully returning here will make it fail if the api
154 * changes to something different than currently expected.
156 remove
= nhg_connected_tree_del(head
, remove
);
158 /* If the entry was sucessfully removed, free the 'connected` struct */
160 removed_nhe
= remove
->nhe
;
161 nhg_connected_free(remove
);
168 /* Assuming UNIQUE RB tree. If this changes, assumptions here about
169 * insertion need to change.
171 struct nhg_hash_entry
*
172 nhg_connected_tree_add_nhe(struct nhg_connected_tree_head
*head
,
173 struct nhg_hash_entry
*depend
)
175 struct nhg_connected
*new = NULL
;
177 new = nhg_connected_new(depend
);
179 /* On success, NULL will be returned from the
182 if (new && (nhg_connected_tree_add(head
, new) == NULL
))
185 /* If it wasn't successful, it must be a duplicate. We enforce the
186 * unique property for the `nhg_connected` tree.
188 nhg_connected_free(new);
194 nhg_connected_tree_decrement_ref(struct nhg_connected_tree_head
*head
)
196 struct nhg_connected
*rb_node_dep
= NULL
;
198 frr_each_safe(nhg_connected_tree
, head
, rb_node_dep
) {
199 zebra_nhg_decrement_ref(rb_node_dep
->nhe
);
204 nhg_connected_tree_increment_ref(struct nhg_connected_tree_head
*head
)
206 struct nhg_connected
*rb_node_dep
= NULL
;
208 frr_each(nhg_connected_tree
, head
, rb_node_dep
) {
209 zebra_nhg_increment_ref(rb_node_dep
->nhe
);
213 struct nhg_hash_entry
*zebra_nhg_resolve(struct nhg_hash_entry
*nhe
)
215 if (CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_RECURSIVE
)
216 && !zebra_nhg_depends_is_empty(nhe
)) {
217 nhe
= nhg_connected_tree_root(&nhe
->nhg_depends
)->nhe
;
218 return zebra_nhg_resolve(nhe
);
224 unsigned int zebra_nhg_depends_count(const struct nhg_hash_entry
*nhe
)
226 return nhg_connected_tree_count(&nhe
->nhg_depends
);
229 bool zebra_nhg_depends_is_empty(const struct nhg_hash_entry
*nhe
)
231 return nhg_connected_tree_is_empty(&nhe
->nhg_depends
);
234 static void zebra_nhg_depends_del(struct nhg_hash_entry
*from
,
235 struct nhg_hash_entry
*depend
)
237 nhg_connected_tree_del_nhe(&from
->nhg_depends
, depend
);
240 static void zebra_nhg_depends_init(struct nhg_hash_entry
*nhe
)
242 nhg_connected_tree_init(&nhe
->nhg_depends
);
245 unsigned int zebra_nhg_dependents_count(const struct nhg_hash_entry
*nhe
)
247 return nhg_connected_tree_count(&nhe
->nhg_dependents
);
251 bool zebra_nhg_dependents_is_empty(const struct nhg_hash_entry
*nhe
)
253 return nhg_connected_tree_is_empty(&nhe
->nhg_dependents
);
256 static void zebra_nhg_dependents_del(struct nhg_hash_entry
*from
,
257 struct nhg_hash_entry
*dependent
)
259 nhg_connected_tree_del_nhe(&from
->nhg_dependents
, dependent
);
262 static void zebra_nhg_dependents_add(struct nhg_hash_entry
*to
,
263 struct nhg_hash_entry
*dependent
)
265 nhg_connected_tree_add_nhe(&to
->nhg_dependents
, dependent
);
268 static void zebra_nhg_dependents_init(struct nhg_hash_entry
*nhe
)
270 nhg_connected_tree_init(&nhe
->nhg_dependents
);
273 /* Release this nhe from anything depending on it */
274 static void zebra_nhg_dependents_release(struct nhg_hash_entry
*nhe
)
276 struct nhg_connected
*rb_node_dep
= NULL
;
278 frr_each_safe(nhg_connected_tree
, &nhe
->nhg_dependents
, rb_node_dep
) {
279 zebra_nhg_depends_del(rb_node_dep
->nhe
, nhe
);
280 /* recheck validity of the dependent */
281 zebra_nhg_check_valid(rb_node_dep
->nhe
);
285 /* Release this nhe from anything that it depends on */
286 static void zebra_nhg_depends_release(struct nhg_hash_entry
*nhe
)
288 if (!zebra_nhg_depends_is_empty(nhe
)) {
289 struct nhg_connected
*rb_node_dep
= NULL
;
291 frr_each_safe(nhg_connected_tree
, &nhe
->nhg_depends
,
293 zebra_nhg_dependents_del(rb_node_dep
->nhe
, nhe
);
299 struct nhg_hash_entry
*zebra_nhg_lookup_id(uint32_t id
)
301 struct nhg_hash_entry lookup
= {};
304 return hash_lookup(zrouter
.nhgs_id
, &lookup
);
307 static int zebra_nhg_insert_id(struct nhg_hash_entry
*nhe
)
309 if (hash_lookup(zrouter
.nhgs_id
, nhe
)) {
311 EC_ZEBRA_NHG_TABLE_INSERT_FAILED
,
312 "Failed inserting NHG %pNG into the ID hash table, entry already exists",
317 (void)hash_get(zrouter
.nhgs_id
, nhe
, hash_alloc_intern
);
322 static void zebra_nhg_set_if(struct nhg_hash_entry
*nhe
, struct interface
*ifp
)
325 if_nhg_dependents_add(ifp
, nhe
);
329 zebra_nhg_connect_depends(struct nhg_hash_entry
*nhe
,
330 struct nhg_connected_tree_head
*nhg_depends
)
332 struct nhg_connected
*rb_node_dep
= NULL
;
334 /* This has been allocated higher above in the stack. Could probably
335 * re-allocate and free the old stuff but just using the same memory
336 * for now. Otherwise, their might be a time trade-off for repeated
337 * alloc/frees as startup.
339 nhe
->nhg_depends
= *nhg_depends
;
341 /* Attach backpointer to anything that it depends on */
342 zebra_nhg_dependents_init(nhe
);
343 if (!zebra_nhg_depends_is_empty(nhe
)) {
344 frr_each(nhg_connected_tree
, &nhe
->nhg_depends
, rb_node_dep
) {
345 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
346 zlog_debug("%s: nhe %p (%pNG), dep %p (%pNG)",
347 __func__
, nhe
, nhe
, rb_node_dep
->nhe
,
350 zebra_nhg_dependents_add(rb_node_dep
->nhe
, nhe
);
355 /* Init an nhe, for use in a hash lookup for example */
356 void zebra_nhe_init(struct nhg_hash_entry
*nhe
, afi_t afi
,
357 const struct nexthop
*nh
)
359 memset(nhe
, 0, sizeof(struct nhg_hash_entry
));
360 nhe
->vrf_id
= VRF_DEFAULT
;
361 nhe
->type
= ZEBRA_ROUTE_NHG
;
362 nhe
->afi
= AFI_UNSPEC
;
364 /* There are some special rules that apply to groups representing
367 if (nh
&& (nh
->next
== NULL
)) {
369 case NEXTHOP_TYPE_IFINDEX
:
370 case NEXTHOP_TYPE_BLACKHOLE
:
372 * This switch case handles setting the afi different
373 * for ipv4/v6 routes. Ifindex/blackhole nexthop
374 * objects cannot be ambiguous, they must be Address
375 * Family specific. If we get here, we will either use
376 * the AF of the route, or the one we got passed from
377 * here from the kernel.
381 case NEXTHOP_TYPE_IPV4_IFINDEX
:
382 case NEXTHOP_TYPE_IPV4
:
385 case NEXTHOP_TYPE_IPV6_IFINDEX
:
386 case NEXTHOP_TYPE_IPV6
:
393 struct nhg_hash_entry
*zebra_nhg_alloc(void)
395 struct nhg_hash_entry
*nhe
;
397 nhe
= XCALLOC(MTYPE_NHG
, sizeof(struct nhg_hash_entry
));
403 * Allocate new nhe and make shallow copy of 'orig'; no
404 * recursive info is copied.
406 struct nhg_hash_entry
*zebra_nhe_copy(const struct nhg_hash_entry
*orig
,
409 struct nhg_hash_entry
*nhe
;
411 nhe
= zebra_nhg_alloc();
415 nexthop_group_copy(&(nhe
->nhg
), &(orig
->nhg
));
417 nhe
->vrf_id
= orig
->vrf_id
;
418 nhe
->afi
= orig
->afi
;
419 nhe
->type
= orig
->type
? orig
->type
: ZEBRA_ROUTE_NHG
;
421 nhe
->dplane_ref
= zebra_router_get_next_sequence();
423 /* Copy backup info also, if present */
424 if (orig
->backup_info
)
425 nhe
->backup_info
= nhg_backup_copy(orig
->backup_info
);
430 /* Allocation via hash handler */
431 static void *zebra_nhg_hash_alloc(void *arg
)
433 struct nhg_hash_entry
*nhe
= NULL
;
434 struct nhg_hash_entry
*copy
= arg
;
436 nhe
= zebra_nhe_copy(copy
, copy
->id
);
438 /* Mark duplicate nexthops in a group at creation time. */
439 nexthop_group_mark_duplicates(&(nhe
->nhg
));
442 * Add the ifp now if it's not a group or recursive and has ifindex.
444 * A proto-owned ID is always a group.
446 if (!PROTO_OWNED(nhe
) && nhe
->nhg
.nexthop
&& !nhe
->nhg
.nexthop
->next
447 && !nhe
->nhg
.nexthop
->resolved
&& nhe
->nhg
.nexthop
->ifindex
) {
448 struct interface
*ifp
= NULL
;
450 ifp
= if_lookup_by_index(nhe
->nhg
.nexthop
->ifindex
,
451 nhe
->nhg
.nexthop
->vrf_id
);
453 zebra_nhg_set_if(nhe
, ifp
);
455 if (IS_ZEBRA_DEBUG_NHG
)
457 "Failed to lookup an interface with ifindex=%d in vrf=%u for NHE %pNG",
458 nhe
->nhg
.nexthop
->ifindex
,
459 nhe
->nhg
.nexthop
->vrf_id
, nhe
);
466 uint32_t zebra_nhg_hash_key(const void *arg
)
468 const struct nhg_hash_entry
*nhe
= arg
;
469 uint32_t key
= 0x5a351234;
470 uint32_t primary
= 0;
473 primary
= nexthop_group_hash(&(nhe
->nhg
));
474 if (nhe
->backup_info
)
475 backup
= nexthop_group_hash(&(nhe
->backup_info
->nhe
->nhg
));
477 key
= jhash_3words(primary
, backup
, nhe
->type
, key
);
479 key
= jhash_2words(nhe
->vrf_id
, nhe
->afi
, key
);
484 uint32_t zebra_nhg_id_key(const void *arg
)
486 const struct nhg_hash_entry
*nhe
= arg
;
491 /* Helper with common nhg/nhe nexthop comparison logic */
492 static bool nhg_compare_nexthops(const struct nexthop
*nh1
,
493 const struct nexthop
*nh2
)
495 assert(nh1
!= NULL
&& nh2
!= NULL
);
498 * We have to check the active flag of each individual one,
499 * not just the overall active_num. This solves the special case
500 * issue of a route with a nexthop group with one nexthop
501 * resolving to itself and thus marking it inactive. If we
502 * have two different routes each wanting to mark a different
503 * nexthop inactive, they need to hash to two different groups.
505 * If we just hashed on num_active, they would hash the same
506 * which is incorrect.
510 * -> 1.1.1.1 dummy1 (inactive)
515 * -> 1.1.2.1 dummy2 (inactive)
517 * Without checking each individual one, they would hash to
518 * the same group and both have 1.1.1.1 dummy1 marked inactive.
521 if (CHECK_FLAG(nh1
->flags
, NEXTHOP_FLAG_ACTIVE
)
522 != CHECK_FLAG(nh2
->flags
, NEXTHOP_FLAG_ACTIVE
))
525 if (!nexthop_same(nh1
, nh2
))
531 bool zebra_nhg_hash_equal(const void *arg1
, const void *arg2
)
533 const struct nhg_hash_entry
*nhe1
= arg1
;
534 const struct nhg_hash_entry
*nhe2
= arg2
;
535 struct nexthop
*nexthop1
;
536 struct nexthop
*nexthop2
;
538 /* No matter what if they equal IDs, assume equal */
539 if (nhe1
->id
&& nhe2
->id
&& (nhe1
->id
== nhe2
->id
))
542 if (nhe1
->type
!= nhe2
->type
)
545 if (nhe1
->vrf_id
!= nhe2
->vrf_id
)
548 if (nhe1
->afi
!= nhe2
->afi
)
551 if (nhe1
->nhg
.nhgr
.buckets
!= nhe2
->nhg
.nhgr
.buckets
)
554 if (nhe1
->nhg
.nhgr
.idle_timer
!= nhe2
->nhg
.nhgr
.idle_timer
)
557 if (nhe1
->nhg
.nhgr
.unbalanced_timer
!= nhe2
->nhg
.nhgr
.unbalanced_timer
)
560 /* Nexthops should be in-order, so we simply compare them in-place */
561 for (nexthop1
= nhe1
->nhg
.nexthop
, nexthop2
= nhe2
->nhg
.nexthop
;
562 nexthop1
&& nexthop2
;
563 nexthop1
= nexthop1
->next
, nexthop2
= nexthop2
->next
) {
565 if (!nhg_compare_nexthops(nexthop1
, nexthop2
))
569 /* Check for unequal list lengths */
570 if (nexthop1
|| nexthop2
)
573 /* If there's no backup info, comparison is done. */
574 if ((nhe1
->backup_info
== NULL
) && (nhe2
->backup_info
== NULL
))
577 /* Compare backup info also - test the easy things first */
578 if (nhe1
->backup_info
&& (nhe2
->backup_info
== NULL
))
580 if (nhe2
->backup_info
&& (nhe1
->backup_info
== NULL
))
583 /* Compare number of backups before actually comparing any */
584 for (nexthop1
= nhe1
->backup_info
->nhe
->nhg
.nexthop
,
585 nexthop2
= nhe2
->backup_info
->nhe
->nhg
.nexthop
;
586 nexthop1
&& nexthop2
;
587 nexthop1
= nexthop1
->next
, nexthop2
= nexthop2
->next
) {
591 /* Did we find the end of one list before the other? */
592 if (nexthop1
|| nexthop2
)
595 /* Have to compare the backup nexthops */
596 for (nexthop1
= nhe1
->backup_info
->nhe
->nhg
.nexthop
,
597 nexthop2
= nhe2
->backup_info
->nhe
->nhg
.nexthop
;
598 nexthop1
&& nexthop2
;
599 nexthop1
= nexthop1
->next
, nexthop2
= nexthop2
->next
) {
601 if (!nhg_compare_nexthops(nexthop1
, nexthop2
))
608 bool zebra_nhg_hash_id_equal(const void *arg1
, const void *arg2
)
610 const struct nhg_hash_entry
*nhe1
= arg1
;
611 const struct nhg_hash_entry
*nhe2
= arg2
;
613 return nhe1
->id
== nhe2
->id
;
616 static int zebra_nhg_process_grp(struct nexthop_group
*nhg
,
617 struct nhg_connected_tree_head
*depends
,
618 struct nh_grp
*grp
, uint8_t count
,
619 struct nhg_resilience
*resilience
)
621 nhg_connected_tree_init(depends
);
623 for (int i
= 0; i
< count
; i
++) {
624 struct nhg_hash_entry
*depend
= NULL
;
625 /* We do not care about nexthop_grp.weight at
626 * this time. But we should figure out
627 * how to adapt this to our code in
630 depend
= depends_find_id_add(depends
, grp
[i
].id
);
635 "Received Nexthop Group from the kernel with a dependent Nexthop ID (%u) which we do not have in our table",
641 * If this is a nexthop with its own group
642 * dependencies, add them as well. Not sure its
643 * even possible to have a group within a group
647 copy_nexthops(&nhg
->nexthop
, depend
->nhg
.nexthop
, NULL
);
651 nhg
->nhgr
= *resilience
;
656 static void handle_recursive_depend(struct nhg_connected_tree_head
*nhg_depends
,
657 struct nexthop
*nh
, afi_t afi
, int type
)
659 struct nhg_hash_entry
*depend
= NULL
;
660 struct nexthop_group resolved_ng
= {};
662 resolved_ng
.nexthop
= nh
;
664 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
665 zlog_debug("%s: head %p, nh %pNHv",
666 __func__
, nhg_depends
, nh
);
668 depend
= zebra_nhg_rib_find(0, &resolved_ng
, afi
, type
);
670 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
671 zlog_debug("%s: nh %pNHv => %p (%u)",
672 __func__
, nh
, depend
,
673 depend
? depend
->id
: 0);
676 depends_add(nhg_depends
, depend
);
680 * Lookup an nhe in the global hash, using data from another nhe. If 'lookup'
681 * has an id value, that's used. Create a new global/shared nhe if not found.
683 static bool zebra_nhe_find(struct nhg_hash_entry
**nhe
, /* return value */
684 struct nhg_hash_entry
*lookup
,
685 struct nhg_connected_tree_head
*nhg_depends
,
686 afi_t afi
, bool from_dplane
)
688 bool created
= false;
689 bool recursive
= false;
690 struct nhg_hash_entry
*newnhe
, *backup_nhe
;
691 struct nexthop
*nh
= NULL
;
693 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
695 "%s: id %u, lookup %p, vrf %d, type %d, depends %p%s",
696 __func__
, lookup
->id
, lookup
, lookup
->vrf_id
,
697 lookup
->type
, nhg_depends
,
698 (from_dplane
? " (from dplane)" : ""));
701 (*nhe
) = zebra_nhg_lookup_id(lookup
->id
);
703 (*nhe
) = hash_lookup(zrouter
.nhgs
, lookup
);
705 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
706 zlog_debug("%s: lookup => %p (%pNG)", __func__
, *nhe
, *nhe
);
708 /* If we found an existing object, we're done */
712 /* We're going to create/insert a new nhe:
713 * assign the next global id value if necessary.
716 lookup
->id
= nhg_get_next_id();
718 if (!from_dplane
&& lookup
->id
< ZEBRA_NHG_PROTO_LOWER
) {
720 * This is a zebra hashed/owned NHG.
722 * It goes in HASH and ID table.
724 newnhe
= hash_get(zrouter
.nhgs
, lookup
, zebra_nhg_hash_alloc
);
725 zebra_nhg_insert_id(newnhe
);
728 * This is upperproto owned NHG or one we read in from dataplane
729 * and should not be hashed to.
731 * It goes in ID table.
734 hash_get(zrouter
.nhgs_id
, lookup
, zebra_nhg_hash_alloc
);
739 /* Mail back the new object */
742 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
743 zlog_debug("%s: => created %p (%pNG)", __func__
, newnhe
,
746 /* Only hash/lookup the depends if the first lookup
747 * fails to find something. This should hopefully save a
748 * lot of cycles for larger ecmp sizes.
751 /* If you don't want to hash on each nexthop in the
752 * nexthop group struct you can pass the depends
753 * directly. Kernel-side we do this since it just looks
756 zebra_nhg_connect_depends(newnhe
, nhg_depends
);
760 /* Prepare dependency relationships if this is not a
761 * singleton nexthop. There are two cases: a single
762 * recursive nexthop, where we need a relationship to the
763 * resolving nexthop; or a group of nexthops, where we need
764 * relationships with the corresponding singletons.
766 zebra_nhg_depends_init(newnhe
);
768 nh
= newnhe
->nhg
.nexthop
;
770 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ACTIVE
))
771 SET_FLAG(newnhe
->flags
, NEXTHOP_GROUP_VALID
);
773 if (nh
->next
== NULL
&& newnhe
->id
< ZEBRA_NHG_PROTO_LOWER
) {
774 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
)) {
775 /* Single recursive nexthop */
776 handle_recursive_depend(&newnhe
->nhg_depends
,
782 /* Proto-owned are groups by default */
783 /* List of nexthops */
784 for (nh
= newnhe
->nhg
.nexthop
; nh
; nh
= nh
->next
) {
785 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
786 zlog_debug("%s: depends NH %pNHv %s",
788 CHECK_FLAG(nh
->flags
,
789 NEXTHOP_FLAG_RECURSIVE
) ?
792 depends_find_add(&newnhe
->nhg_depends
, nh
, afi
,
793 newnhe
->type
, from_dplane
);
798 SET_FLAG(newnhe
->flags
, NEXTHOP_GROUP_RECURSIVE
);
800 /* Attach dependent backpointers to singletons */
801 zebra_nhg_connect_depends(newnhe
, &newnhe
->nhg_depends
);
807 if (zebra_nhg_get_backup_nhg(newnhe
) == NULL
||
808 zebra_nhg_get_backup_nhg(newnhe
)->nexthop
== NULL
)
811 /* If there are backup nexthops, add them to the backup
812 * depends tree. The rules here are a little different.
815 backup_nhe
= newnhe
->backup_info
->nhe
;
817 nh
= backup_nhe
->nhg
.nexthop
;
819 /* Singleton recursive NH */
820 if (nh
->next
== NULL
&&
821 CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
)) {
822 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
823 zlog_debug("%s: backup depend NH %pNHv (R)",
826 /* Single recursive nexthop */
827 handle_recursive_depend(&backup_nhe
->nhg_depends
, nh
->resolved
,
828 afi
, backup_nhe
->type
);
831 /* One or more backup NHs */
832 for (; nh
; nh
= nh
->next
) {
833 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
834 zlog_debug("%s: backup depend NH %pNHv %s",
836 CHECK_FLAG(nh
->flags
,
837 NEXTHOP_FLAG_RECURSIVE
) ?
840 depends_find_add(&backup_nhe
->nhg_depends
, nh
, afi
,
841 backup_nhe
->type
, from_dplane
);
846 SET_FLAG(backup_nhe
->flags
, NEXTHOP_GROUP_RECURSIVE
);
849 /* Reset time since last update */
850 (*nhe
)->uptime
= monotime(NULL
);
856 * Lookup or create an nhe, based on an nhg or an nhe id.
858 static bool zebra_nhg_find(struct nhg_hash_entry
**nhe
, uint32_t id
,
859 struct nexthop_group
*nhg
,
860 struct nhg_connected_tree_head
*nhg_depends
,
861 vrf_id_t vrf_id
, afi_t afi
, int type
,
864 struct nhg_hash_entry lookup
= {};
865 bool created
= false;
867 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
868 zlog_debug("%s: id %u, nhg %p, vrf %d, type %d, depends %p",
869 __func__
, id
, nhg
, vrf_id
, type
,
872 /* Use a temporary nhe and call into the superset/common code */
874 lookup
.type
= type
? type
: ZEBRA_ROUTE_NHG
;
877 lookup
.vrf_id
= vrf_id
;
878 if (nhg_depends
|| lookup
.nhg
.nexthop
->next
) {
879 /* Groups can have all vrfs and AF's in them */
880 lookup
.afi
= AFI_UNSPEC
;
882 switch (lookup
.nhg
.nexthop
->type
) {
883 case (NEXTHOP_TYPE_IFINDEX
):
884 case (NEXTHOP_TYPE_BLACKHOLE
):
886 * This switch case handles setting the afi different
887 * for ipv4/v6 routes. Ifindex/blackhole nexthop
888 * objects cannot be ambiguous, they must be Address
889 * Family specific. If we get here, we will either use
890 * the AF of the route, or the one we got passed from
891 * here from the kernel.
895 case (NEXTHOP_TYPE_IPV4_IFINDEX
):
896 case (NEXTHOP_TYPE_IPV4
):
899 case (NEXTHOP_TYPE_IPV6_IFINDEX
):
900 case (NEXTHOP_TYPE_IPV6
):
901 lookup
.afi
= AFI_IP6
;
906 created
= zebra_nhe_find(nhe
, &lookup
, nhg_depends
, afi
, from_dplane
);
911 /* Find/create a single nexthop */
912 static struct nhg_hash_entry
*zebra_nhg_find_nexthop(uint32_t id
,
917 struct nhg_hash_entry
*nhe
= NULL
;
918 struct nexthop_group nhg
= {};
919 vrf_id_t vrf_id
= !vrf_is_backend_netns() ? VRF_DEFAULT
: nh
->vrf_id
;
921 nexthop_group_add_sorted(&nhg
, nh
);
923 zebra_nhg_find(&nhe
, id
, &nhg
, NULL
, vrf_id
, afi
, type
, from_dplane
);
925 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
926 zlog_debug("%s: nh %pNHv => %p (%pNG)", __func__
, nh
, nhe
, nhe
);
931 static uint32_t nhg_ctx_get_id(const struct nhg_ctx
*ctx
)
936 static void nhg_ctx_set_status(struct nhg_ctx
*ctx
, enum nhg_ctx_status status
)
938 ctx
->status
= status
;
941 static enum nhg_ctx_status
nhg_ctx_get_status(const struct nhg_ctx
*ctx
)
946 static void nhg_ctx_set_op(struct nhg_ctx
*ctx
, enum nhg_ctx_op_e op
)
951 static enum nhg_ctx_op_e
nhg_ctx_get_op(const struct nhg_ctx
*ctx
)
956 static vrf_id_t
nhg_ctx_get_vrf_id(const struct nhg_ctx
*ctx
)
961 static int nhg_ctx_get_type(const struct nhg_ctx
*ctx
)
966 static int nhg_ctx_get_afi(const struct nhg_ctx
*ctx
)
971 static struct nexthop
*nhg_ctx_get_nh(struct nhg_ctx
*ctx
)
976 static uint8_t nhg_ctx_get_count(const struct nhg_ctx
*ctx
)
981 static struct nh_grp
*nhg_ctx_get_grp(struct nhg_ctx
*ctx
)
986 static struct nhg_resilience
*nhg_ctx_get_resilience(struct nhg_ctx
*ctx
)
988 return &ctx
->resilience
;
991 static struct nhg_ctx
*nhg_ctx_new(void)
995 new = XCALLOC(MTYPE_NHG_CTX
, sizeof(struct nhg_ctx
));
1000 void nhg_ctx_free(struct nhg_ctx
**ctx
)
1007 assert((*ctx
) != NULL
);
1009 if (nhg_ctx_get_count(*ctx
))
1012 nh
= nhg_ctx_get_nh(*ctx
);
1014 nexthop_del_labels(nh
);
1015 nexthop_del_srv6_seg6local(nh
);
1016 nexthop_del_srv6_seg6(nh
);
1019 XFREE(MTYPE_NHG_CTX
, *ctx
);
1022 static struct nhg_ctx
*nhg_ctx_init(uint32_t id
, struct nexthop
*nh
,
1023 struct nh_grp
*grp
, vrf_id_t vrf_id
,
1024 afi_t afi
, int type
, uint8_t count
,
1025 struct nhg_resilience
*resilience
)
1027 struct nhg_ctx
*ctx
= NULL
;
1029 ctx
= nhg_ctx_new();
1032 ctx
->vrf_id
= vrf_id
;
1038 ctx
->resilience
= *resilience
;
1041 /* Copy over the array */
1042 memcpy(&ctx
->u
.grp
, grp
, count
* sizeof(struct nh_grp
));
1049 static void zebra_nhg_set_valid(struct nhg_hash_entry
*nhe
)
1051 struct nhg_connected
*rb_node_dep
;
1053 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
1055 frr_each(nhg_connected_tree
, &nhe
->nhg_dependents
, rb_node_dep
)
1056 zebra_nhg_set_valid(rb_node_dep
->nhe
);
1059 static void zebra_nhg_set_invalid(struct nhg_hash_entry
*nhe
)
1061 struct nhg_connected
*rb_node_dep
;
1063 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
1065 /* If we're in shutdown, this interface event needs to clean
1066 * up installed NHGs, so don't clear that flag directly.
1068 if (!zebra_router_in_shutdown())
1069 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
1071 /* Update validity of nexthops depending on it */
1072 frr_each(nhg_connected_tree
, &nhe
->nhg_dependents
, rb_node_dep
)
1073 zebra_nhg_check_valid(rb_node_dep
->nhe
);
1076 void zebra_nhg_check_valid(struct nhg_hash_entry
*nhe
)
1078 struct nhg_connected
*rb_node_dep
= NULL
;
1081 /* If anthing else in the group is valid, the group is valid */
1082 frr_each(nhg_connected_tree
, &nhe
->nhg_depends
, rb_node_dep
) {
1083 if (CHECK_FLAG(rb_node_dep
->nhe
->flags
, NEXTHOP_GROUP_VALID
)) {
1091 zebra_nhg_set_valid(nhe
);
1093 zebra_nhg_set_invalid(nhe
);
1096 static void zebra_nhg_release_all_deps(struct nhg_hash_entry
*nhe
)
1098 /* Remove it from any lists it may be on */
1099 zebra_nhg_depends_release(nhe
);
1100 zebra_nhg_dependents_release(nhe
);
1102 if_nhg_dependents_del(nhe
->ifp
, nhe
);
1105 static void zebra_nhg_release(struct nhg_hash_entry
*nhe
)
1107 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1108 zlog_debug("%s: nhe %p (%pNG)", __func__
, nhe
, nhe
);
1110 zebra_nhg_release_all_deps(nhe
);
1113 * If its not zebra owned, we didn't store it here and have to be
1114 * sure we don't clear one thats actually being used.
1116 if (nhe
->id
< ZEBRA_NHG_PROTO_LOWER
)
1117 hash_release(zrouter
.nhgs
, nhe
);
1119 hash_release(zrouter
.nhgs_id
, nhe
);
1122 static void zebra_nhg_handle_uninstall(struct nhg_hash_entry
*nhe
)
1124 zebra_nhg_release(nhe
);
1125 zebra_nhg_free(nhe
);
1128 static void zebra_nhg_handle_install(struct nhg_hash_entry
*nhe
)
1130 /* Update validity of groups depending on it */
1131 struct nhg_connected
*rb_node_dep
;
1133 frr_each_safe(nhg_connected_tree
, &nhe
->nhg_dependents
, rb_node_dep
)
1134 zebra_nhg_set_valid(rb_node_dep
->nhe
);
1138 * The kernel/other program has changed the state of a nexthop object we are
1141 static void zebra_nhg_handle_kernel_state_change(struct nhg_hash_entry
*nhe
,
1147 "Kernel %s a nexthop group with ID (%pNG) that we are still using for a route, sending it back down",
1148 (is_delete
? "deleted" : "updated"), nhe
);
1150 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
1151 zebra_nhg_install_kernel(nhe
);
1153 zebra_nhg_handle_uninstall(nhe
);
1156 static int nhg_ctx_process_new(struct nhg_ctx
*ctx
)
1158 struct nexthop_group
*nhg
= NULL
;
1159 struct nhg_connected_tree_head nhg_depends
= {};
1160 struct nhg_hash_entry
*lookup
= NULL
;
1161 struct nhg_hash_entry
*nhe
= NULL
;
1163 uint32_t id
= nhg_ctx_get_id(ctx
);
1164 uint8_t count
= nhg_ctx_get_count(ctx
);
1165 vrf_id_t vrf_id
= nhg_ctx_get_vrf_id(ctx
);
1166 int type
= nhg_ctx_get_type(ctx
);
1167 afi_t afi
= nhg_ctx_get_afi(ctx
);
1169 lookup
= zebra_nhg_lookup_id(id
);
1171 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1172 zlog_debug("%s: id %u, count %d, lookup => %p",
1173 __func__
, id
, count
, lookup
);
1176 /* This is already present in our table, hence an update
1177 * that we did not initate.
1179 zebra_nhg_handle_kernel_state_change(lookup
, false);
1183 if (nhg_ctx_get_count(ctx
)) {
1184 nhg
= nexthop_group_new();
1185 if (zebra_nhg_process_grp(nhg
, &nhg_depends
,
1186 nhg_ctx_get_grp(ctx
), count
,
1187 nhg_ctx_get_resilience(ctx
))) {
1188 depends_decrement_free(&nhg_depends
);
1189 nexthop_group_delete(&nhg
);
1193 if (!zebra_nhg_find(&nhe
, id
, nhg
, &nhg_depends
, vrf_id
, afi
,
1195 depends_decrement_free(&nhg_depends
);
1197 /* These got copied over in zebra_nhg_alloc() */
1198 nexthop_group_delete(&nhg
);
1200 nhe
= zebra_nhg_find_nexthop(id
, nhg_ctx_get_nh(ctx
), afi
, type
,
1205 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
1206 "Zebra failed to find or create a nexthop hash entry for ID (%u)",
1211 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1212 zlog_debug("%s: nhe %p (%pNG) is new", __func__
, nhe
, nhe
);
1215 * If daemon nhg from the kernel, add a refcnt here to indicate the
1218 if (PROTO_OWNED(nhe
))
1219 zebra_nhg_increment_ref(nhe
);
1221 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
1222 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
1227 static int nhg_ctx_process_del(struct nhg_ctx
*ctx
)
1229 struct nhg_hash_entry
*nhe
= NULL
;
1230 uint32_t id
= nhg_ctx_get_id(ctx
);
1232 nhe
= zebra_nhg_lookup_id(id
);
1236 EC_ZEBRA_BAD_NHG_MESSAGE
,
1237 "Kernel delete message received for nexthop group ID (%u) that we do not have in our ID table",
1242 zebra_nhg_handle_kernel_state_change(nhe
, true);
1247 static void nhg_ctx_fini(struct nhg_ctx
**ctx
)
1250 * Just freeing for now, maybe do something more in the future
1257 static int queue_add(struct nhg_ctx
*ctx
)
1259 /* If its queued or already processed do nothing */
1260 if (nhg_ctx_get_status(ctx
) == NHG_CTX_QUEUED
)
1263 if (rib_queue_nhg_ctx_add(ctx
)) {
1264 nhg_ctx_set_status(ctx
, NHG_CTX_FAILURE
);
1268 nhg_ctx_set_status(ctx
, NHG_CTX_QUEUED
);
1273 int nhg_ctx_process(struct nhg_ctx
*ctx
)
1277 switch (nhg_ctx_get_op(ctx
)) {
1278 case NHG_CTX_OP_NEW
:
1279 ret
= nhg_ctx_process_new(ctx
);
1280 if (nhg_ctx_get_count(ctx
) && ret
== -ENOENT
1281 && nhg_ctx_get_status(ctx
) != NHG_CTX_REQUEUED
) {
1283 * We have entered a situation where we are
1284 * processing a group from the kernel
1285 * that has a contained nexthop which
1286 * we have not yet processed.
1288 * Re-enqueue this ctx to be handled exactly one
1289 * more time (indicated by the flag).
1291 * By the time we get back to it, we
1292 * should have processed its depends.
1294 nhg_ctx_set_status(ctx
, NHG_CTX_NONE
);
1295 if (queue_add(ctx
) == 0) {
1296 nhg_ctx_set_status(ctx
, NHG_CTX_REQUEUED
);
1301 case NHG_CTX_OP_DEL
:
1302 ret
= nhg_ctx_process_del(ctx
);
1303 case NHG_CTX_OP_NONE
:
1307 nhg_ctx_set_status(ctx
, (ret
? NHG_CTX_FAILURE
: NHG_CTX_SUCCESS
));
1314 /* Kernel-side, you either get a single new nexthop or a array of ID's */
1315 int zebra_nhg_kernel_find(uint32_t id
, struct nexthop
*nh
, struct nh_grp
*grp
,
1316 uint8_t count
, vrf_id_t vrf_id
, afi_t afi
, int type
,
1317 int startup
, struct nhg_resilience
*nhgr
)
1319 struct nhg_ctx
*ctx
= NULL
;
1321 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1322 zlog_debug("%s: nh %pNHv, id %u, count %d",
1323 __func__
, nh
, id
, (int)count
);
1325 if (id
> id_counter
&& id
< ZEBRA_NHG_PROTO_LOWER
)
1326 /* Increase our counter so we don't try to create
1327 * an ID that already exists
1331 ctx
= nhg_ctx_init(id
, nh
, grp
, vrf_id
, afi
, type
, count
, nhgr
);
1332 nhg_ctx_set_op(ctx
, NHG_CTX_OP_NEW
);
1334 /* Under statup conditions, we need to handle them immediately
1335 * like we do for routes. Otherwise, we are going to get a route
1336 * with a nhe_id that we have not handled.
1339 return nhg_ctx_process(ctx
);
1341 if (queue_add(ctx
)) {
1349 /* Kernel-side, received delete message */
1350 int zebra_nhg_kernel_del(uint32_t id
, vrf_id_t vrf_id
)
1352 struct nhg_ctx
*ctx
= NULL
;
1354 ctx
= nhg_ctx_init(id
, NULL
, NULL
, vrf_id
, 0, 0, 0, NULL
);
1356 nhg_ctx_set_op(ctx
, NHG_CTX_OP_DEL
);
1358 if (queue_add(ctx
)) {
1366 /* Some dependency helper functions */
1367 static struct nhg_hash_entry
*depends_find_recursive(const struct nexthop
*nh
,
1368 afi_t afi
, int type
)
1370 struct nhg_hash_entry
*nhe
;
1371 struct nexthop
*lookup
= NULL
;
1373 lookup
= nexthop_dup(nh
, NULL
);
1375 nhe
= zebra_nhg_find_nexthop(0, lookup
, afi
, type
, false);
1377 nexthops_free(lookup
);
1382 static struct nhg_hash_entry
*depends_find_singleton(const struct nexthop
*nh
,
1383 afi_t afi
, int type
,
1386 struct nhg_hash_entry
*nhe
;
1387 struct nexthop lookup
= {};
1389 /* Capture a snapshot of this single nh; it might be part of a list,
1390 * so we need to make a standalone copy.
1392 nexthop_copy_no_recurse(&lookup
, nh
, NULL
);
1394 nhe
= zebra_nhg_find_nexthop(0, &lookup
, afi
, type
, from_dplane
);
1396 /* The copy may have allocated labels; free them if necessary. */
1397 nexthop_del_labels(&lookup
);
1398 nexthop_del_srv6_seg6local(&lookup
);
1399 nexthop_del_srv6_seg6(&lookup
);
1401 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1402 zlog_debug("%s: nh %pNHv => %p (%pNG)", __func__
, nh
, nhe
, nhe
);
1407 static struct nhg_hash_entry
*depends_find(const struct nexthop
*nh
, afi_t afi
,
1408 int type
, bool from_dplane
)
1410 struct nhg_hash_entry
*nhe
= NULL
;
1415 /* We are separating these functions out to increase handling speed
1416 * in the non-recursive case (by not alloc/freeing)
1418 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1419 nhe
= depends_find_recursive(nh
, afi
, type
);
1421 nhe
= depends_find_singleton(nh
, afi
, type
, from_dplane
);
1424 if (IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1425 zlog_debug("%s: nh %pNHv %s => %p (%pNG)", __func__
, nh
,
1426 CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
) ? "(R)"
1435 static void depends_add(struct nhg_connected_tree_head
*head
,
1436 struct nhg_hash_entry
*depend
)
1438 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1439 zlog_debug("%s: head %p nh %pNHv",
1440 __func__
, head
, depend
->nhg
.nexthop
);
1442 /* If NULL is returned, it was successfully added and
1443 * needs to have its refcnt incremented.
1445 * Else the NHE is already present in the tree and doesn't
1446 * need to increment the refcnt.
1448 if (nhg_connected_tree_add_nhe(head
, depend
) == NULL
)
1449 zebra_nhg_increment_ref(depend
);
1452 static struct nhg_hash_entry
*
1453 depends_find_add(struct nhg_connected_tree_head
*head
, struct nexthop
*nh
,
1454 afi_t afi
, int type
, bool from_dplane
)
1456 struct nhg_hash_entry
*depend
= NULL
;
1458 depend
= depends_find(nh
, afi
, type
, from_dplane
);
1460 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1461 zlog_debug("%s: nh %pNHv => %p",
1462 __func__
, nh
, depend
);
1465 depends_add(head
, depend
);
1470 static struct nhg_hash_entry
*
1471 depends_find_id_add(struct nhg_connected_tree_head
*head
, uint32_t id
)
1473 struct nhg_hash_entry
*depend
= NULL
;
1475 depend
= zebra_nhg_lookup_id(id
);
1478 depends_add(head
, depend
);
1483 static void depends_decrement_free(struct nhg_connected_tree_head
*head
)
1485 nhg_connected_tree_decrement_ref(head
);
1486 nhg_connected_tree_free(head
);
1489 /* Find an nhe based on a list of nexthops */
1490 struct nhg_hash_entry
*zebra_nhg_rib_find(uint32_t id
,
1491 struct nexthop_group
*nhg
,
1492 afi_t rt_afi
, int type
)
1494 struct nhg_hash_entry
*nhe
= NULL
;
1498 * CLANG SA is complaining that nexthop may be NULL
1499 * Make it happy but this is ridonc
1501 assert(nhg
->nexthop
);
1502 vrf_id
= !vrf_is_backend_netns() ? VRF_DEFAULT
: nhg
->nexthop
->vrf_id
;
1504 zebra_nhg_find(&nhe
, id
, nhg
, NULL
, vrf_id
, rt_afi
, type
, false);
1506 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1507 zlog_debug("%s: => nhe %p (%pNG)", __func__
, nhe
, nhe
);
1512 /* Find an nhe based on a route's nhe */
1513 struct nhg_hash_entry
*
1514 zebra_nhg_rib_find_nhe(struct nhg_hash_entry
*rt_nhe
, afi_t rt_afi
)
1516 struct nhg_hash_entry
*nhe
= NULL
;
1518 if (!(rt_nhe
&& rt_nhe
->nhg
.nexthop
)) {
1519 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
1520 "No nexthop passed to %s", __func__
);
1524 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1525 zlog_debug("%s: rt_nhe %p (%pNG)", __func__
, rt_nhe
, rt_nhe
);
1527 zebra_nhe_find(&nhe
, rt_nhe
, NULL
, rt_afi
, false);
1529 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1530 zlog_debug("%s: => nhe %p (%pNG)", __func__
, nhe
, nhe
);
1536 * Allocate backup nexthop info object. Typically these are embedded in
1537 * nhg_hash_entry objects.
1539 struct nhg_backup_info
*zebra_nhg_backup_alloc(void)
1541 struct nhg_backup_info
*p
;
1543 p
= XCALLOC(MTYPE_NHG
, sizeof(struct nhg_backup_info
));
1545 p
->nhe
= zebra_nhg_alloc();
1547 /* Identify the embedded group used to hold the list of backups */
1548 SET_FLAG(p
->nhe
->flags
, NEXTHOP_GROUP_BACKUP
);
1554 * Free backup nexthop info object, deal with any embedded allocations
1556 void zebra_nhg_backup_free(struct nhg_backup_info
**p
)
1560 zebra_nhg_free((*p
)->nhe
);
1562 XFREE(MTYPE_NHG
, (*p
));
1566 /* Accessor for backup nexthop group */
1567 struct nexthop_group
*zebra_nhg_get_backup_nhg(struct nhg_hash_entry
*nhe
)
1569 struct nexthop_group
*p
= NULL
;
1572 if (nhe
->backup_info
&& nhe
->backup_info
->nhe
)
1573 p
= &(nhe
->backup_info
->nhe
->nhg
);
1580 * Helper to return a copy of a backup_info - note that this is a shallow
1581 * copy, meant to be used when creating a new nhe from info passed in with
1584 static struct nhg_backup_info
*
1585 nhg_backup_copy(const struct nhg_backup_info
*orig
)
1587 struct nhg_backup_info
*b
;
1589 b
= zebra_nhg_backup_alloc();
1591 /* Copy list of nexthops */
1592 nexthop_group_copy(&(b
->nhe
->nhg
), &(orig
->nhe
->nhg
));
1597 static void zebra_nhg_free_members(struct nhg_hash_entry
*nhe
)
1599 nexthops_free(nhe
->nhg
.nexthop
);
1601 zebra_nhg_backup_free(&nhe
->backup_info
);
1603 /* Decrement to remove connection ref */
1604 nhg_connected_tree_decrement_ref(&nhe
->nhg_depends
);
1605 nhg_connected_tree_free(&nhe
->nhg_depends
);
1606 nhg_connected_tree_free(&nhe
->nhg_dependents
);
1609 void zebra_nhg_free(struct nhg_hash_entry
*nhe
)
1611 if (IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1612 /* Group or singleton? */
1613 if (nhe
->nhg
.nexthop
&& nhe
->nhg
.nexthop
->next
)
1614 zlog_debug("%s: nhe %p (%pNG), refcnt %d", __func__
,
1615 nhe
, nhe
, nhe
->refcnt
);
1617 zlog_debug("%s: nhe %p (%pNG), refcnt %d, NH %pNHv",
1618 __func__
, nhe
, nhe
, nhe
->refcnt
,
1622 THREAD_OFF(nhe
->timer
);
1624 zebra_nhg_free_members(nhe
);
1626 XFREE(MTYPE_NHG
, nhe
);
1630 * Let's just drop the memory associated with each item
1632 void zebra_nhg_hash_free(void *p
)
1634 struct nhg_hash_entry
*nhe
= p
;
1636 if (IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1637 /* Group or singleton? */
1638 if (nhe
->nhg
.nexthop
&& nhe
->nhg
.nexthop
->next
)
1639 zlog_debug("%s: nhe %p (%u), refcnt %d", __func__
, nhe
,
1640 nhe
->id
, nhe
->refcnt
);
1642 zlog_debug("%s: nhe %p (%pNG), refcnt %d, NH %pNHv",
1643 __func__
, nhe
, nhe
, nhe
->refcnt
,
1647 THREAD_OFF(nhe
->timer
);
1649 nexthops_free(nhe
->nhg
.nexthop
);
1651 XFREE(MTYPE_NHG
, nhe
);
1655 * On cleanup there are nexthop groups that have not
1656 * been resolved at all( a nhe->id of 0 ). As such
1657 * zebra needs to clean up the memory associated with
1660 void zebra_nhg_hash_free_zero_id(struct hash_bucket
*b
, void *arg
)
1662 struct nhg_hash_entry
*nhe
= b
->data
;
1663 struct nhg_connected
*dep
;
1665 while ((dep
= nhg_connected_tree_pop(&nhe
->nhg_depends
))) {
1666 if (dep
->nhe
->id
== 0)
1667 zebra_nhg_hash_free(dep
->nhe
);
1669 nhg_connected_free(dep
);
1672 while ((dep
= nhg_connected_tree_pop(&nhe
->nhg_dependents
)))
1673 nhg_connected_free(dep
);
1675 if (nhe
->backup_info
&& nhe
->backup_info
->nhe
->id
== 0) {
1676 while ((dep
= nhg_connected_tree_pop(
1677 &nhe
->backup_info
->nhe
->nhg_depends
)))
1678 nhg_connected_free(dep
);
1680 zebra_nhg_hash_free(nhe
->backup_info
->nhe
);
1682 XFREE(MTYPE_NHG
, nhe
->backup_info
);
1686 static void zebra_nhg_timer(struct thread
*thread
)
1688 struct nhg_hash_entry
*nhe
= THREAD_ARG(thread
);
1690 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1691 zlog_debug("Nexthop Timer for nhe: %pNG", nhe
);
1693 if (nhe
->refcnt
== 1)
1694 zebra_nhg_decrement_ref(nhe
);
1697 void zebra_nhg_decrement_ref(struct nhg_hash_entry
*nhe
)
1699 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1700 zlog_debug("%s: nhe %p (%pNG) %d => %d", __func__
, nhe
, nhe
,
1701 nhe
->refcnt
, nhe
->refcnt
- 1);
1705 if (!zebra_router_in_shutdown() && nhe
->refcnt
<= 0 &&
1706 CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
) &&
1707 !CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_KEEP_AROUND
)) {
1709 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_KEEP_AROUND
);
1710 thread_add_timer(zrouter
.master
, zebra_nhg_timer
, nhe
,
1711 zrouter
.nhg_keep
, &nhe
->timer
);
1715 if (!zebra_nhg_depends_is_empty(nhe
))
1716 nhg_connected_tree_decrement_ref(&nhe
->nhg_depends
);
1718 if (ZEBRA_NHG_CREATED(nhe
) && nhe
->refcnt
<= 0)
1719 zebra_nhg_uninstall_kernel(nhe
);
1722 void zebra_nhg_increment_ref(struct nhg_hash_entry
*nhe
)
1724 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1725 zlog_debug("%s: nhe %p (%pNG) %d => %d", __func__
, nhe
, nhe
,
1726 nhe
->refcnt
, nhe
->refcnt
+ 1);
1730 if (thread_is_scheduled(nhe
->timer
)) {
1731 THREAD_OFF(nhe
->timer
);
1733 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_KEEP_AROUND
);
1736 if (!zebra_nhg_depends_is_empty(nhe
))
1737 nhg_connected_tree_increment_ref(&nhe
->nhg_depends
);
1740 static struct nexthop
*nexthop_set_resolved(afi_t afi
,
1741 const struct nexthop
*newhop
,
1742 struct nexthop
*nexthop
,
1743 struct zebra_sr_policy
*policy
)
1745 struct nexthop
*resolved_hop
;
1746 uint8_t num_labels
= 0;
1747 mpls_label_t labels
[MPLS_MAX_LABELS
];
1748 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1751 resolved_hop
= nexthop_new();
1752 SET_FLAG(resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1754 resolved_hop
->vrf_id
= nexthop
->vrf_id
;
1755 switch (newhop
->type
) {
1756 case NEXTHOP_TYPE_IPV4
:
1757 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1758 /* If the resolving route specifies a gateway, use it */
1759 resolved_hop
->type
= newhop
->type
;
1760 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
1762 if (newhop
->ifindex
) {
1763 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1764 resolved_hop
->ifindex
= newhop
->ifindex
;
1767 case NEXTHOP_TYPE_IPV6
:
1768 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1769 resolved_hop
->type
= newhop
->type
;
1770 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
1772 if (newhop
->ifindex
) {
1773 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1774 resolved_hop
->ifindex
= newhop
->ifindex
;
1777 case NEXTHOP_TYPE_IFINDEX
:
1778 /* If the resolving route is an interface route,
1779 * it means the gateway we are looking up is connected
1780 * to that interface. (The actual network is _not_ onlink).
1781 * Therefore, the resolved route should have the original
1782 * gateway as nexthop as it is directly connected.
1784 * On Linux, we have to set the onlink netlink flag because
1785 * otherwise, the kernel won't accept the route.
1787 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
1788 if (afi
== AFI_IP
) {
1789 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1790 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
1791 } else if (afi
== AFI_IP6
) {
1792 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1793 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
1795 resolved_hop
->ifindex
= newhop
->ifindex
;
1797 case NEXTHOP_TYPE_BLACKHOLE
:
1798 resolved_hop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
1799 resolved_hop
->bh_type
= newhop
->bh_type
;
1803 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
1804 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
1806 /* Copy labels of the resolved route and the parent resolving to it */
1811 * Don't push the first SID if the corresponding action in the
1814 if (!newhop
->nh_label
|| !newhop
->nh_label
->num_labels
1815 || newhop
->nh_label
->label
[0] == MPLS_LABEL_IMPLICIT_NULL
)
1818 for (; label_num
< policy
->segment_list
.label_num
; label_num
++)
1819 labels
[num_labels
++] =
1820 policy
->segment_list
.labels
[label_num
];
1821 label_type
= policy
->segment_list
.type
;
1822 } else if (newhop
->nh_label
) {
1823 for (i
= 0; i
< newhop
->nh_label
->num_labels
; i
++) {
1824 /* Be a bit picky about overrunning the local array */
1825 if (num_labels
>= MPLS_MAX_LABELS
) {
1826 if (IS_ZEBRA_DEBUG_NHG
|| IS_ZEBRA_DEBUG_RIB
)
1827 zlog_debug("%s: too many labels in newhop %pNHv",
1831 labels
[num_labels
++] = newhop
->nh_label
->label
[i
];
1833 /* Use the "outer" type */
1834 label_type
= newhop
->nh_label_type
;
1837 if (nexthop
->nh_label
) {
1838 for (i
= 0; i
< nexthop
->nh_label
->num_labels
; i
++) {
1839 /* Be a bit picky about overrunning the local array */
1840 if (num_labels
>= MPLS_MAX_LABELS
) {
1841 if (IS_ZEBRA_DEBUG_NHG
|| IS_ZEBRA_DEBUG_RIB
)
1842 zlog_debug("%s: too many labels in nexthop %pNHv",
1846 labels
[num_labels
++] = nexthop
->nh_label
->label
[i
];
1849 /* If the parent has labels, use its type if
1850 * we don't already have one.
1852 if (label_type
== ZEBRA_LSP_NONE
)
1853 label_type
= nexthop
->nh_label_type
;
1857 nexthop_add_labels(resolved_hop
, label_type
, num_labels
,
1860 if (nexthop
->nh_srv6
) {
1861 nexthop_add_srv6_seg6local(resolved_hop
,
1862 nexthop
->nh_srv6
->seg6local_action
,
1863 &nexthop
->nh_srv6
->seg6local_ctx
);
1864 nexthop_add_srv6_seg6(resolved_hop
,
1865 &nexthop
->nh_srv6
->seg6_segs
);
1868 resolved_hop
->rparent
= nexthop
;
1869 _nexthop_add(&nexthop
->resolved
, resolved_hop
);
1871 return resolved_hop
;
1874 /* Checks if nexthop we are trying to resolve to is valid */
1875 static bool nexthop_valid_resolve(const struct nexthop
*nexthop
,
1876 const struct nexthop
*resolved
)
1878 /* Can't resolve to a recursive nexthop */
1879 if (CHECK_FLAG(resolved
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1882 /* Must be ACTIVE */
1883 if (!CHECK_FLAG(resolved
->flags
, NEXTHOP_FLAG_ACTIVE
))
1886 /* Must not be duplicate */
1887 if (CHECK_FLAG(resolved
->flags
, NEXTHOP_FLAG_DUPLICATE
))
1890 switch (nexthop
->type
) {
1891 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1892 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1893 /* If the nexthop we are resolving to does not match the
1894 * ifindex for the nexthop the route wanted, its not valid.
1896 if (nexthop
->ifindex
!= resolved
->ifindex
)
1899 case NEXTHOP_TYPE_IPV4
:
1900 case NEXTHOP_TYPE_IPV6
:
1901 case NEXTHOP_TYPE_IFINDEX
:
1902 case NEXTHOP_TYPE_BLACKHOLE
:
1910 * Downstream VNI and Single VXlan device check.
1912 * If it has nexthop VNI labels at this point it must be D-VNI allocated
1913 * and all the nexthops have to be on an SVD.
1915 * If SVD is not available, mark as inactive.
1917 static bool nexthop_set_evpn_dvni_svd(vrf_id_t re_vrf_id
,
1918 struct nexthop
*nexthop
)
1920 if (!is_vrf_l3vni_svd_backed(re_vrf_id
)) {
1921 if (IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1922 struct vrf
*vrf
= vrf_lookup_by_id(re_vrf_id
);
1925 "nexthop %pNHv D-VNI but route's vrf %s(%u) doesn't use SVD",
1926 nexthop
, VRF_LOGNAME(vrf
), re_vrf_id
);
1932 nexthop
->ifindex
= get_l3vni_vxlan_ifindex(re_vrf_id
);
1933 nexthop
->vrf_id
= 0;
1935 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1936 zlog_debug("nexthop %pNHv using SVD", nexthop
);
1942 * Given a nexthop we need to properly recursively resolve
1943 * the route. As such, do a table lookup to find and match
1944 * if at all possible. Set the nexthop->ifindex and resolved_id
1947 static int resolve_backup_nexthops(const struct nexthop
*nexthop
,
1948 const struct nhg_hash_entry
*nhe
,
1949 struct nexthop
*resolved
,
1950 struct nhg_hash_entry
*resolve_nhe
,
1951 struct backup_nh_map_s
*map
)
1954 const struct nexthop
*bnh
;
1955 struct nexthop
*nh
, *newnh
;
1956 mpls_label_t labels
[MPLS_MAX_LABELS
];
1959 assert(nexthop
->backup_num
<= NEXTHOP_MAX_BACKUPS
);
1961 /* Locate backups from the original nexthop's backup index and nhe */
1962 for (i
= 0; i
< nexthop
->backup_num
; i
++) {
1963 idx
= nexthop
->backup_idx
[i
];
1965 /* Do we already know about this particular backup? */
1966 for (j
= 0; j
< map
->map_count
; j
++) {
1967 if (map
->map
[j
].orig_idx
== idx
)
1971 if (j
< map
->map_count
) {
1972 resolved
->backup_idx
[resolved
->backup_num
] =
1973 map
->map
[j
].new_idx
;
1974 resolved
->backup_num
++;
1976 SET_FLAG(resolved
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1978 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1979 zlog_debug("%s: found map idx orig %d, new %d",
1980 __func__
, map
->map
[j
].orig_idx
,
1981 map
->map
[j
].new_idx
);
1986 /* We can't handle any new map entries at this point. */
1987 if (map
->map_count
== MULTIPATH_NUM
)
1990 /* Need to create/copy a new backup */
1991 bnh
= nhe
->backup_info
->nhe
->nhg
.nexthop
;
1992 for (j
= 0; j
< idx
; j
++) {
1998 /* Whoops - bad index in the nexthop? */
2002 if (resolve_nhe
->backup_info
== NULL
)
2003 resolve_nhe
->backup_info
= zebra_nhg_backup_alloc();
2005 /* Update backup info in the resolving nexthop and its nhe */
2006 newnh
= nexthop_dup_no_recurse(bnh
, NULL
);
2008 /* We may need some special handling for mpls labels: the new
2009 * backup needs to carry the recursive nexthop's labels,
2010 * if any: they may be vrf labels e.g.
2011 * The original/inner labels are in the stack of 'resolve_nhe',
2012 * if that is longer than the stack in 'nexthop'.
2014 if (newnh
->nh_label
&& resolved
->nh_label
&&
2015 nexthop
->nh_label
) {
2016 if (resolved
->nh_label
->num_labels
>
2017 nexthop
->nh_label
->num_labels
) {
2018 /* Prepare new label stack */
2020 for (j
= 0; j
< newnh
->nh_label
->num_labels
;
2022 labels
[j
] = newnh
->nh_label
->label
[j
];
2026 /* Include inner labels */
2027 for (j
= nexthop
->nh_label
->num_labels
;
2028 j
< resolved
->nh_label
->num_labels
;
2030 labels
[num_labels
] =
2031 resolved
->nh_label
->label
[j
];
2035 /* Replace existing label stack in the backup */
2036 nexthop_del_labels(newnh
);
2037 nexthop_add_labels(newnh
, bnh
->nh_label_type
,
2038 num_labels
, labels
);
2042 /* Need to compute the new backup index in the new
2043 * backup list, and add to map struct.
2046 nh
= resolve_nhe
->backup_info
->nhe
->nhg
.nexthop
;
2056 } else /* First one */
2057 resolve_nhe
->backup_info
->nhe
->nhg
.nexthop
= newnh
;
2060 resolved
->backup_idx
[resolved
->backup_num
] = j
;
2061 resolved
->backup_num
++;
2063 SET_FLAG(resolved
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
2065 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2066 zlog_debug("%s: added idx orig %d, new %d",
2069 /* Update map/cache */
2070 map
->map
[map
->map_count
].orig_idx
= idx
;
2071 map
->map
[map
->map_count
].new_idx
= j
;
2079 * So this nexthop resolution has decided that a connected route
2080 * is the correct choice. At this point in time if FRR has multiple
2081 * connected routes that all point to the same prefix one will be
2082 * selected, *but* the particular interface may not be the one
2083 * that the nexthop points at. Let's look at all the available
2084 * connected routes on this node and if any of them auto match
2085 * the routes nexthops ifindex that is good enough for a match
2087 * This code is depending on the fact that a nexthop->ifindex is 0
2088 * if it is not known, if this assumption changes, yummy!
2089 * Additionally a ifindx of 0 means figure it out for us.
2091 static struct route_entry
*
2092 zebra_nhg_connected_ifindex(struct route_node
*rn
, struct route_entry
*match
,
2093 int32_t curr_ifindex
)
2095 struct nexthop
*newhop
= match
->nhe
->nhg
.nexthop
;
2096 struct route_entry
*re
;
2098 assert(newhop
); /* What a kick in the patooey */
2100 if (curr_ifindex
== 0)
2103 if (curr_ifindex
== newhop
->ifindex
)
2107 * At this point we know that this route is matching a connected
2108 * but there are possibly a bunch of connected routes that are
2109 * alive that should be considered as well. So let's iterate over
2110 * all the re's and see if they are connected as well and maybe one
2111 * of those ifindexes match as well.
2113 RNODE_FOREACH_RE (rn
, re
) {
2114 if (re
->type
!= ZEBRA_ROUTE_CONNECT
)
2117 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2121 * zebra has a connected route that is not removed
2122 * let's test if it is good
2124 newhop
= re
->nhe
->nhg
.nexthop
;
2126 if (curr_ifindex
== newhop
->ifindex
)
2134 * Given a nexthop we need to properly recursively resolve,
2135 * do a table lookup to find and match if at all possible.
2136 * Set the nexthop->ifindex and resolution info as appropriate.
2138 static int nexthop_active(struct nexthop
*nexthop
, struct nhg_hash_entry
*nhe
,
2139 const struct prefix
*top
, int type
, uint32_t flags
,
2140 uint32_t *pmtu
, vrf_id_t vrf_id
)
2143 struct route_table
*table
;
2144 struct route_node
*rn
;
2145 struct route_entry
*match
= NULL
;
2147 struct zebra_nhlfe
*nhlfe
;
2148 struct nexthop
*newhop
;
2149 struct interface
*ifp
;
2151 struct zebra_vrf
*zvrf
;
2152 struct in_addr local_ipv4
;
2153 struct in_addr
*ipv4
;
2156 /* Reset some nexthop attributes that we'll recompute if necessary */
2157 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
)
2158 || (nexthop
->type
== NEXTHOP_TYPE_IPV6
))
2159 nexthop
->ifindex
= 0;
2161 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
2162 nexthops_free(nexthop
->resolved
);
2163 nexthop
->resolved
= NULL
;
2166 * Set afi based on nexthop type.
2167 * Some nexthop types get special handling, possibly skipping
2168 * the normal processing.
2170 switch (nexthop
->type
) {
2171 case NEXTHOP_TYPE_IFINDEX
:
2173 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
2175 * If the interface exists and its operative or its a kernel
2176 * route and interface is up, its active. We trust kernel routes
2179 if (ifp
&& (if_is_operative(ifp
)))
2185 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2188 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
2189 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2191 if (ifp
&& if_is_operative(ifp
))
2198 case NEXTHOP_TYPE_IPV4
:
2199 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2202 case NEXTHOP_TYPE_IPV6
:
2206 case NEXTHOP_TYPE_BLACKHOLE
:
2211 * If the nexthop has been marked as 'onlink' we just need to make
2212 * sure the nexthop's interface is known and is operational.
2214 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)) {
2215 /* DVNI/SVD Checks for EVPN routes */
2216 if (nexthop
->nh_label
&&
2217 nexthop
->nh_label_type
== ZEBRA_LSP_EVPN
&&
2218 !nexthop_set_evpn_dvni_svd(vrf_id
, nexthop
))
2221 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
2223 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2224 zlog_debug("nexthop %pNHv marked onlink but nhif %u doesn't exist",
2225 nexthop
, nexthop
->ifindex
);
2228 if (!if_is_operative(ifp
)) {
2229 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2230 zlog_debug("nexthop %pNHv marked onlink but nhif %s is not operational",
2231 nexthop
, ifp
->name
);
2238 ((top
->family
== AF_INET
&& top
->prefixlen
== IPV4_MAX_BITLEN
&&
2239 nexthop
->gate
.ipv4
.s_addr
== top
->u
.prefix4
.s_addr
) ||
2240 (top
->family
== AF_INET6
&& top
->prefixlen
== IPV6_MAX_BITLEN
&&
2241 memcmp(&nexthop
->gate
.ipv6
, &top
->u
.prefix6
, IPV6_MAX_BYTELEN
) ==
2243 nexthop
->vrf_id
== vrf_id
) {
2244 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2246 " :%s: Attempting to install a max prefixlength route through itself",
2251 /* Validation for ipv4 mapped ipv6 nexthop. */
2252 if (IS_MAPPED_IPV6(&nexthop
->gate
.ipv6
)) {
2255 ipv4_mapped_ipv6_to_ipv4(&nexthop
->gate
.ipv6
, ipv4
);
2257 ipv4
= &nexthop
->gate
.ipv4
;
2260 /* Processing for nexthops with SR 'color' attribute, using
2261 * the corresponding SR policy object.
2263 if (nexthop
->srte_color
) {
2264 struct ipaddr endpoint
= {0};
2265 struct zebra_sr_policy
*policy
;
2269 endpoint
.ipa_type
= IPADDR_V4
;
2270 endpoint
.ipaddr_v4
= *ipv4
;
2273 endpoint
.ipa_type
= IPADDR_V6
;
2274 endpoint
.ipaddr_v6
= nexthop
->gate
.ipv6
;
2279 flog_err(EC_LIB_DEVELOPMENT
,
2280 "%s: unknown address-family: %u", __func__
,
2285 policy
= zebra_sr_policy_find(nexthop
->srte_color
, &endpoint
);
2286 if (policy
&& policy
->status
== ZEBRA_SR_POLICY_UP
) {
2288 frr_each_safe (nhlfe_list
, &policy
->lsp
->nhlfe_list
,
2290 if (!CHECK_FLAG(nhlfe
->flags
,
2291 NHLFE_FLAG_SELECTED
)
2292 || CHECK_FLAG(nhlfe
->flags
,
2293 NHLFE_FLAG_DELETED
))
2295 SET_FLAG(nexthop
->flags
,
2296 NEXTHOP_FLAG_RECURSIVE
);
2297 nexthop_set_resolved(afi
, nhlfe
->nexthop
,
2306 /* Make lookup prefix. */
2307 memset(&p
, 0, sizeof(struct prefix
));
2311 p
.prefixlen
= IPV4_MAX_BITLEN
;
2312 p
.u
.prefix4
= *ipv4
;
2315 p
.family
= AF_INET6
;
2316 p
.prefixlen
= IPV6_MAX_BITLEN
;
2317 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
2322 assert(afi
!= AFI_IP
&& afi
!= AFI_IP6
);
2326 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, nexthop
->vrf_id
);
2328 zvrf
= zebra_vrf_lookup_by_id(nexthop
->vrf_id
);
2329 if (!table
|| !zvrf
) {
2330 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2331 zlog_debug(" %s: Table not found", __func__
);
2335 rn
= route_node_match(table
, (struct prefix
*)&p
);
2337 route_unlock_node(rn
);
2339 /* Lookup should halt if we've matched against ourselves ('top',
2340 * if specified) - i.e., we cannot have a nexthop NH1 is
2341 * resolved by a route NH1. The exception is if the route is a
2344 if (prefix_same(&rn
->p
, top
))
2345 if (((afi
== AFI_IP
)
2346 && (rn
->p
.prefixlen
!= IPV4_MAX_BITLEN
))
2347 || ((afi
== AFI_IP6
)
2348 && (rn
->p
.prefixlen
!= IPV6_MAX_BITLEN
))) {
2349 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2351 " %s: Matched against ourself and prefix length is not max bit length",
2356 /* Pick up selected route. */
2357 /* However, do not resolve over default route unless explicitly
2360 if (is_default_prefix(&rn
->p
)
2361 && !rnh_resolve_via_default(zvrf
, p
.family
)) {
2362 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2364 " :%s: Resolved against default route",
2369 dest
= rib_dest_from_rnode(rn
);
2370 if (dest
&& dest
->selected_fib
2371 && !CHECK_FLAG(dest
->selected_fib
->status
,
2372 ROUTE_ENTRY_REMOVED
)
2373 && dest
->selected_fib
->type
!= ZEBRA_ROUTE_TABLE
)
2374 match
= dest
->selected_fib
;
2376 /* If there is no selected route or matched route is EGP, go up
2382 } while (rn
&& rn
->info
== NULL
);
2384 route_lock_node(rn
);
2389 if ((match
->type
== ZEBRA_ROUTE_CONNECT
) ||
2390 (RIB_SYSTEM_ROUTE(match
) && RSYSTEM_ROUTE(type
))) {
2391 match
= zebra_nhg_connected_ifindex(rn
, match
,
2394 newhop
= match
->nhe
->nhg
.nexthop
;
2395 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
||
2396 nexthop
->type
== NEXTHOP_TYPE_IPV6
)
2397 nexthop
->ifindex
= newhop
->ifindex
;
2398 else if (nexthop
->ifindex
!= newhop
->ifindex
) {
2399 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2401 "%s: %pNHv given ifindex does not match nexthops ifindex found: %pNHv",
2402 __func__
, nexthop
, newhop
);
2404 * NEXTHOP_TYPE_*_IFINDEX but ifindex
2405 * doesn't match what we found.
2410 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2412 "%s: CONNECT match %p (%pNG), newhop %pNHv",
2413 __func__
, match
, match
->nhe
, newhop
);
2416 } else if (CHECK_FLAG(flags
, ZEBRA_FLAG_ALLOW_RECURSION
)) {
2417 struct nexthop_group
*nhg
;
2418 struct nexthop
*resolver
;
2419 struct backup_nh_map_s map
= {};
2424 * Only useful if installed or being Route Replacing
2425 * Why Being Route Replaced as well?
2426 * Imagine a route A and route B( that depends on A )
2427 * for recursive resolution and A already exists in the
2428 * zebra rib. If zebra receives the routes
2429 * for resolution at aproximately the same time in the [
2430 * B, A ] order on the workQ. If this happens then
2431 * normal route resolution will happen and B will be
2432 * resolved successfully and then A will be resolved
2433 * successfully. Now imagine the reversed order [A, B].
2434 * A will be resolved and then scheduled for installed
2435 * (Thus not having the ROUTE_ENTRY_INSTALLED flag ). B
2436 * will then get resolved and fail to be installed
2437 * because the original below test. Let's `loosen` this
2438 * up a tiny bit and allow the
2439 * ROUTE_ENTRY_ROUTE_REPLACING flag ( that is set when a
2440 * Route Replace operation is being initiated on A now )
2441 * to now satisfy this situation. This will allow
2442 * either order in the workQ to work properly.
2444 if (!CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
) &&
2445 !CHECK_FLAG(match
->status
,
2446 ROUTE_ENTRY_ROUTE_REPLACING
)) {
2447 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2449 "%s: match %p (%pNG) not installed or being Route Replaced",
2450 __func__
, match
, match
->nhe
);
2452 goto done_with_match
;
2455 /* Examine installed nexthops; note that there
2456 * may not be any installed primary nexthops if
2457 * only backups are installed.
2459 nhg
= rib_get_fib_nhg(match
);
2460 for (ALL_NEXTHOPS_PTR(nhg
, newhop
)) {
2461 if (!nexthop_valid_resolve(nexthop
, newhop
))
2464 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2466 "%s: RECURSIVE match %p (%pNG), newhop %pNHv",
2467 __func__
, match
, match
->nhe
,
2470 SET_FLAG(nexthop
->flags
,
2471 NEXTHOP_FLAG_RECURSIVE
);
2472 resolver
= nexthop_set_resolved(afi
, newhop
,
2476 /* If there are backup nexthops, capture
2477 * that info with the resolving nexthop.
2479 if (resolver
&& newhop
->backup_num
> 0) {
2480 resolve_backup_nexthops(newhop
,
2487 /* Examine installed backup nexthops, if any. There
2488 * are only installed backups *if* there is a
2489 * dedicated fib list. The UI can also control use
2490 * of backups for resolution.
2492 nhg
= rib_get_fib_backup_nhg(match
);
2493 if (!use_recursive_backups
||
2494 nhg
== NULL
|| nhg
->nexthop
== NULL
)
2495 goto done_with_match
;
2497 for (ALL_NEXTHOPS_PTR(nhg
, newhop
)) {
2498 if (!nexthop_valid_resolve(nexthop
, newhop
))
2501 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2503 "%s: RECURSIVE match backup %p (%pNG), newhop %pNHv",
2504 __func__
, match
, match
->nhe
,
2507 SET_FLAG(nexthop
->flags
,
2508 NEXTHOP_FLAG_RECURSIVE
);
2509 nexthop_set_resolved(afi
, newhop
, nexthop
,
2515 /* Capture resolving mtu */
2520 } else if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2522 " %s: Recursion failed to find",
2527 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2529 " %s: Route Type %s has not turned on recursion",
2530 __func__
, zebra_route_string(type
));
2531 if (type
== ZEBRA_ROUTE_BGP
2532 && !CHECK_FLAG(flags
, ZEBRA_FLAG_IBGP
))
2534 " EBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\"");
2539 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2540 zlog_debug(" %s: Nexthop did not lookup in table",
2545 /* This function verifies reachability of one given nexthop, which can be
2546 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
2547 * in nexthop->flags field. The nexthop->ifindex will be updated
2548 * appropriately as well.
2550 * An existing route map can turn an otherwise active nexthop into inactive,
2551 * but not vice versa.
2553 * The return value is the final value of 'ACTIVE' flag.
2555 static unsigned nexthop_active_check(struct route_node
*rn
,
2556 struct route_entry
*re
,
2557 struct nexthop
*nexthop
,
2558 struct nhg_hash_entry
*nhe
)
2560 route_map_result_t ret
= RMAP_PERMITMATCH
;
2562 const struct prefix
*p
, *src_p
;
2563 struct zebra_vrf
*zvrf
;
2567 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
2569 if (rn
->p
.family
== AF_INET
)
2571 else if (rn
->p
.family
== AF_INET6
)
2576 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2577 zlog_debug("%s: re %p, nexthop %pNHv", __func__
, re
, nexthop
);
2580 * If this is a kernel route, then if the interface is *up* then
2581 * by golly gee whiz it's a good route.
2583 if (re
->type
== ZEBRA_ROUTE_KERNEL
|| re
->type
== ZEBRA_ROUTE_SYSTEM
) {
2584 struct interface
*ifp
;
2586 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
2588 if (ifp
&& (if_is_operative(ifp
) || if_is_up(ifp
))) {
2589 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2594 vrf_id
= zvrf_id(rib_dest_vrf(rib_dest_from_rnode(rn
)));
2595 switch (nexthop
->type
) {
2596 case NEXTHOP_TYPE_IFINDEX
:
2597 if (nexthop_active(nexthop
, nhe
, &rn
->p
, re
->type
, re
->flags
,
2599 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2601 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2603 case NEXTHOP_TYPE_IPV4
:
2604 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2606 if (nexthop_active(nexthop
, nhe
, &rn
->p
, re
->type
, re
->flags
,
2608 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2610 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2612 case NEXTHOP_TYPE_IPV6
:
2614 if (nexthop_active(nexthop
, nhe
, &rn
->p
, re
->type
, re
->flags
,
2616 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2618 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2620 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2621 /* RFC 5549, v4 prefix with v6 NH */
2622 if (rn
->p
.family
!= AF_INET
)
2625 if (nexthop_active(nexthop
, nhe
, &rn
->p
, re
->type
, re
->flags
,
2627 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2629 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2631 case NEXTHOP_TYPE_BLACKHOLE
:
2632 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2640 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
2641 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2642 zlog_debug(" %s: Unable to find active nexthop",
2647 /* Capture recursive nexthop mtu.
2648 * TODO -- the code used to just reset the re's value to zero
2649 * for each nexthop, and then jam any resolving route's mtu value in,
2650 * whether or not that was zero, or lt/gt any existing value? The
2651 * way this is used appears to be as a floor value, so let's try
2652 * using it that way here.
2655 if (re
->nexthop_mtu
== 0 || re
->nexthop_mtu
> mtu
)
2656 re
->nexthop_mtu
= mtu
;
2659 /* XXX: What exactly do those checks do? Do we support
2660 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
2662 if (RIB_SYSTEM_ROUTE(re
) || (family
== AFI_IP
&& p
->family
!= AF_INET
)
2663 || (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
2664 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2666 /* The original code didn't determine the family correctly
2667 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
2668 * from the rib_table_info in those cases.
2669 * Possibly it may be better to use only the rib_table_info
2673 struct rib_table_info
*info
;
2675 info
= srcdest_rnode_table_info(rn
);
2679 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
2681 zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2683 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2684 zlog_debug(" %s: zvrf is NULL", __func__
);
2685 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2688 /* It'll get set if required inside */
2689 ret
= zebra_route_map_check(family
, re
->type
, re
->instance
, p
, nexthop
,
2691 if (ret
== RMAP_DENYMATCH
) {
2692 if (IS_ZEBRA_DEBUG_RIB
) {
2694 "%u:%pRN: Filtering out with NH %pNHv due to route map",
2695 re
->vrf_id
, rn
, nexthop
);
2697 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2699 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2702 /* Helper function called after resolution to walk nhg rb trees
2703 * and toggle the NEXTHOP_GROUP_VALID flag if the nexthop
2704 * is active on singleton NHEs.
2706 static bool zebra_nhg_set_valid_if_active(struct nhg_hash_entry
*nhe
)
2708 struct nhg_connected
*rb_node_dep
= NULL
;
2711 if (!zebra_nhg_depends_is_empty(nhe
)) {
2712 /* Is at least one depend valid? */
2713 frr_each(nhg_connected_tree
, &nhe
->nhg_depends
, rb_node_dep
) {
2714 if (zebra_nhg_set_valid_if_active(rb_node_dep
->nhe
))
2721 /* should be fully resolved singleton at this point */
2722 if (CHECK_FLAG(nhe
->nhg
.nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2727 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
2732 /* Checks if the first nexthop is EVPN. If not, early return.
2734 * This is used to determine if there is a mismatch between l3VNI
2735 * of the route's vrf and the nexthops in use's VNI labels.
2737 * If there is a mismatch, we keep the labels as these MUST be DVNI nexthops.
2739 * IF there is no mismatch, we remove the labels and handle the routes as
2740 * we have traditionally with evpn.
2742 static bool nexthop_list_set_evpn_dvni(struct route_entry
*re
,
2743 struct nexthop_group
*nhg
)
2745 struct nexthop
*nexthop
;
2748 bool use_dvni
= false;
2750 nexthop
= nhg
->nexthop
;
2752 if (!nexthop
->nh_label
|| nexthop
->nh_label_type
!= ZEBRA_LSP_EVPN
)
2755 re_vrf_vni
= get_l3vni_vni(re
->vrf_id
);
2757 for (; nexthop
; nexthop
= nexthop
->next
) {
2758 if (!nexthop
->nh_label
||
2759 nexthop
->nh_label_type
!= ZEBRA_LSP_EVPN
)
2762 nh_vni
= label2vni(&nexthop
->nh_label
->label
[0]);
2764 if (nh_vni
!= re_vrf_vni
)
2768 /* Using traditional way, no VNI encap - remove labels */
2770 for (nexthop
= nhg
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2771 nexthop_del_labels(nexthop
);
2778 * Process a list of nexthops, given an nhe, determining
2779 * whether each one is ACTIVE/installable at this time.
2781 static uint32_t nexthop_list_active_update(struct route_node
*rn
,
2782 struct route_entry
*re
,
2783 struct nhg_hash_entry
*nhe
,
2786 union g_addr prev_src
;
2787 unsigned int prev_active
, new_active
;
2788 ifindex_t prev_index
;
2789 uint32_t counter
= 0;
2790 struct nexthop
*nexthop
;
2791 struct nexthop_group
*nhg
= &nhe
->nhg
;
2792 bool vni_removed
= false;
2794 nexthop
= nhg
->nexthop
;
2796 /* Init recursive nh mtu */
2797 re
->nexthop_mtu
= 0;
2799 /* Handler for dvni evpn nexthops. Has to be done at nhg level */
2800 vni_removed
= !nexthop_list_set_evpn_dvni(re
, nhg
);
2802 /* Process nexthops one-by-one */
2803 for ( ; nexthop
; nexthop
= nexthop
->next
) {
2805 /* No protocol daemon provides src and so we're skipping
2808 prev_src
= nexthop
->rmap_src
;
2809 prev_active
= CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2810 prev_index
= nexthop
->ifindex
;
2812 /* Include the containing nhe for primary nexthops: if there's
2813 * recursive resolution, we capture the backup info also.
2816 nexthop_active_check(rn
, re
, nexthop
,
2817 (is_backup
? NULL
: nhe
));
2820 * We need to respect the multipath_num here
2821 * as that what we should be able to install from
2822 * a multipath perspective should not be a data plane
2825 if (new_active
&& counter
>= zrouter
.multipath_num
) {
2828 /* Set it and its resolved nexthop as inactive. */
2829 for (nh
= nexthop
; nh
; nh
= nh
->resolved
)
2830 UNSET_FLAG(nh
->flags
, NEXTHOP_FLAG_ACTIVE
);
2838 /* Check for changes to the nexthop - set ROUTE_ENTRY_CHANGED */
2839 if (prev_active
!= new_active
||
2840 prev_index
!= nexthop
->ifindex
||
2841 ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
&&
2842 nexthop
->type
< NEXTHOP_TYPE_IPV6
) &&
2843 prev_src
.ipv4
.s_addr
!= nexthop
->rmap_src
.ipv4
.s_addr
) ||
2844 ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
&&
2845 nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
) &&
2846 !(IPV6_ADDR_SAME(&prev_src
.ipv6
,
2847 &nexthop
->rmap_src
.ipv6
))) ||
2848 CHECK_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
) ||
2850 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2857 static uint32_t proto_nhg_nexthop_active_update(struct nexthop_group
*nhg
)
2860 uint32_t curr_active
= 0;
2862 /* Assume all active for now */
2864 for (nh
= nhg
->nexthop
; nh
; nh
= nh
->next
) {
2865 SET_FLAG(nh
->flags
, NEXTHOP_FLAG_ACTIVE
);
2873 * Iterate over all nexthops of the given RIB entry and refresh their
2874 * ACTIVE flag. If any nexthop is found to toggle the ACTIVE flag,
2875 * the whole re structure is flagged with ROUTE_ENTRY_CHANGED.
2877 * Return value is the new number of active nexthops.
2879 int nexthop_active_update(struct route_node
*rn
, struct route_entry
*re
)
2881 struct nhg_hash_entry
*curr_nhe
;
2882 uint32_t curr_active
= 0, backup_active
= 0;
2884 if (PROTO_OWNED(re
->nhe
))
2885 return proto_nhg_nexthop_active_update(&re
->nhe
->nhg
);
2887 afi_t rt_afi
= family2afi(rn
->p
.family
);
2889 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2891 /* Make a local copy of the existing nhe, so we don't work on/modify
2894 curr_nhe
= zebra_nhe_copy(re
->nhe
, re
->nhe
->id
);
2896 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2897 zlog_debug("%s: re %p nhe %p (%pNG), curr_nhe %p", __func__
, re
,
2898 re
->nhe
, re
->nhe
, curr_nhe
);
2900 /* Clear the existing id, if any: this will avoid any confusion
2901 * if the id exists, and will also force the creation
2902 * of a new nhe reflecting the changes we may make in this local copy.
2906 /* Process nexthops */
2907 curr_active
= nexthop_list_active_update(rn
, re
, curr_nhe
, false);
2909 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2910 zlog_debug("%s: re %p curr_active %u", __func__
, re
,
2913 /* If there are no backup nexthops, we are done */
2914 if (zebra_nhg_get_backup_nhg(curr_nhe
) == NULL
)
2917 backup_active
= nexthop_list_active_update(
2918 rn
, re
, curr_nhe
->backup_info
->nhe
, true /*is_backup*/);
2920 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2921 zlog_debug("%s: re %p backup_active %u", __func__
, re
,
2927 * Ref or create an nhe that matches the current state of the
2930 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
2931 struct nhg_hash_entry
*new_nhe
= NULL
;
2933 new_nhe
= zebra_nhg_rib_find_nhe(curr_nhe
, rt_afi
);
2935 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2937 "%s: re %p CHANGED: nhe %p (%pNG) => new_nhe %p (%pNG)",
2938 __func__
, re
, re
->nhe
, re
->nhe
, new_nhe
,
2941 route_entry_update_nhe(re
, new_nhe
);
2945 /* Walk the NHE depends tree and toggle NEXTHOP_GROUP_VALID
2946 * flag where appropriate.
2949 zebra_nhg_set_valid_if_active(re
->nhe
);
2952 * Do not need the old / copied nhe anymore since it
2953 * was either copied over into a new nhe or not
2956 zebra_nhg_free(curr_nhe
);
2960 /* Recursively construct a grp array of fully resolved IDs.
2962 * This function allows us to account for groups within groups,
2963 * by converting them into a flat array of IDs.
2965 * nh_grp is modified at every level of recursion to append
2966 * to it the next unique, fully resolved ID from the entire tree.
2970 * I'm pretty sure we only allow ONE level of group within group currently.
2971 * But making this recursive just in case that ever changes.
2973 static uint8_t zebra_nhg_nhe2grp_internal(struct nh_grp
*grp
,
2975 struct nhg_hash_entry
*nhe
,
2978 struct nhg_connected
*rb_node_dep
= NULL
;
2979 struct nhg_hash_entry
*depend
= NULL
;
2980 uint8_t i
= curr_index
;
2982 frr_each(nhg_connected_tree
, &nhe
->nhg_depends
, rb_node_dep
) {
2983 bool duplicate
= false;
2988 depend
= rb_node_dep
->nhe
;
2991 * If its recursive, use its resolved nhe in the group
2993 if (CHECK_FLAG(depend
->flags
, NEXTHOP_GROUP_RECURSIVE
)) {
2994 depend
= zebra_nhg_resolve(depend
);
2997 EC_ZEBRA_NHG_FIB_UPDATE
,
2998 "Failed to recursively resolve Nexthop Hash Entry in the group id=%pNG",
3004 if (!zebra_nhg_depends_is_empty(depend
)) {
3005 /* This is a group within a group */
3006 i
= zebra_nhg_nhe2grp_internal(grp
, i
, depend
, max_num
);
3008 if (!CHECK_FLAG(depend
->flags
, NEXTHOP_GROUP_VALID
)) {
3009 if (IS_ZEBRA_DEBUG_RIB_DETAILED
3010 || IS_ZEBRA_DEBUG_NHG
)
3012 "%s: Nexthop ID (%u) not valid, not appending to dataplane install group",
3013 __func__
, depend
->id
);
3017 /* If the nexthop not installed/queued for install don't
3018 * put in the ID array.
3020 if (!(CHECK_FLAG(depend
->flags
, NEXTHOP_GROUP_INSTALLED
)
3021 || CHECK_FLAG(depend
->flags
,
3022 NEXTHOP_GROUP_QUEUED
))) {
3023 if (IS_ZEBRA_DEBUG_RIB_DETAILED
3024 || IS_ZEBRA_DEBUG_NHG
)
3026 "%s: Nexthop ID (%u) not installed or queued for install, not appending to dataplane install group",
3027 __func__
, depend
->id
);
3031 /* Check for duplicate IDs, ignore if found. */
3032 for (int j
= 0; j
< i
; j
++) {
3033 if (depend
->id
== grp
[j
].id
) {
3040 if (IS_ZEBRA_DEBUG_RIB_DETAILED
3041 || IS_ZEBRA_DEBUG_NHG
)
3043 "%s: Nexthop ID (%u) is duplicate, not appending to dataplane install group",
3044 __func__
, depend
->id
);
3048 grp
[i
].id
= depend
->id
;
3049 grp
[i
].weight
= depend
->nhg
.nexthop
->weight
;
3054 if (nhe
->backup_info
== NULL
|| nhe
->backup_info
->nhe
== NULL
)
3057 /* TODO -- For now, we are not trying to use or install any
3058 * backup info in this nexthop-id path: we aren't prepared
3059 * to use the backups here yet. We're just debugging what we find.
3061 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
3062 zlog_debug("%s: skipping backup nhe", __func__
);
3068 /* Convert a nhe into a group array */
3069 uint8_t zebra_nhg_nhe2grp(struct nh_grp
*grp
, struct nhg_hash_entry
*nhe
,
3072 /* Call into the recursive function */
3073 return zebra_nhg_nhe2grp_internal(grp
, 0, nhe
, max_num
);
3076 void zebra_nhg_install_kernel(struct nhg_hash_entry
*nhe
)
3078 struct nhg_connected
*rb_node_dep
= NULL
;
3080 /* Resolve it first */
3081 nhe
= zebra_nhg_resolve(nhe
);
3083 /* Make sure all depends are installed/queued */
3084 frr_each(nhg_connected_tree
, &nhe
->nhg_depends
, rb_node_dep
) {
3085 zebra_nhg_install_kernel(rb_node_dep
->nhe
);
3088 if (CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
)
3089 && !CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
)
3090 && !CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_QUEUED
)) {
3091 /* Change its type to us since we are installing it */
3092 if (!ZEBRA_NHG_CREATED(nhe
))
3093 nhe
->type
= ZEBRA_ROUTE_NHG
;
3095 int ret
= dplane_nexthop_add(nhe
);
3098 case ZEBRA_DPLANE_REQUEST_QUEUED
:
3099 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_QUEUED
);
3101 case ZEBRA_DPLANE_REQUEST_FAILURE
:
3103 EC_ZEBRA_DP_INSTALL_FAIL
,
3104 "Failed to install Nexthop ID (%pNG) into the kernel",
3107 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
3108 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
3109 zebra_nhg_handle_install(nhe
);
3115 void zebra_nhg_uninstall_kernel(struct nhg_hash_entry
*nhe
)
3117 if (CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
)) {
3118 int ret
= dplane_nexthop_delete(nhe
);
3121 case ZEBRA_DPLANE_REQUEST_QUEUED
:
3122 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_QUEUED
);
3124 case ZEBRA_DPLANE_REQUEST_FAILURE
:
3126 EC_ZEBRA_DP_DELETE_FAIL
,
3127 "Failed to uninstall Nexthop ID (%pNG) from the kernel",
3130 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
3131 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
3136 zebra_nhg_handle_uninstall(nhe
);
3139 void zebra_nhg_dplane_result(struct zebra_dplane_ctx
*ctx
)
3141 enum dplane_op_e op
;
3142 enum zebra_dplane_result status
;
3144 struct nhg_hash_entry
*nhe
= NULL
;
3146 op
= dplane_ctx_get_op(ctx
);
3147 status
= dplane_ctx_get_status(ctx
);
3149 id
= dplane_ctx_get_nhe_id(ctx
);
3151 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
|| IS_ZEBRA_DEBUG_NHG_DETAIL
)
3153 "Nexthop dplane ctx %p, op %s, nexthop ID (%u), result %s",
3154 ctx
, dplane_op2str(op
), id
, dplane_res2str(status
));
3157 case DPLANE_OP_NH_DELETE
:
3158 if (status
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
3160 EC_ZEBRA_DP_DELETE_FAIL
,
3161 "Failed to uninstall Nexthop ID (%u) from the kernel",
3164 /* We already free'd the data, nothing to do */
3166 case DPLANE_OP_NH_INSTALL
:
3167 case DPLANE_OP_NH_UPDATE
:
3168 nhe
= zebra_nhg_lookup_id(id
);
3171 if (IS_ZEBRA_DEBUG_NHG
)
3173 "%s operation preformed on Nexthop ID (%u) in the kernel, that we no longer have in our table",
3174 dplane_op2str(op
), id
);
3179 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_QUEUED
);
3180 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3181 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
3182 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
3183 zebra_nhg_handle_install(nhe
);
3185 /* If daemon nhg, send it an update */
3186 if (PROTO_OWNED(nhe
))
3187 zsend_nhg_notify(nhe
->type
, nhe
->zapi_instance
,
3188 nhe
->zapi_session
, nhe
->id
,
3189 ZAPI_NHG_INSTALLED
);
3191 /* If daemon nhg, send it an update */
3192 if (PROTO_OWNED(nhe
))
3193 zsend_nhg_notify(nhe
->type
, nhe
->zapi_instance
,
3194 nhe
->zapi_session
, nhe
->id
,
3195 ZAPI_NHG_FAIL_INSTALL
);
3197 if (!(zebra_nhg_proto_nexthops_only() &&
3200 EC_ZEBRA_DP_INSTALL_FAIL
,
3201 "Failed to install Nexthop (%pNG) into the kernel",
3206 case DPLANE_OP_ROUTE_INSTALL
:
3207 case DPLANE_OP_ROUTE_UPDATE
:
3208 case DPLANE_OP_ROUTE_DELETE
:
3209 case DPLANE_OP_ROUTE_NOTIFY
:
3210 case DPLANE_OP_LSP_INSTALL
:
3211 case DPLANE_OP_LSP_UPDATE
:
3212 case DPLANE_OP_LSP_DELETE
:
3213 case DPLANE_OP_LSP_NOTIFY
:
3214 case DPLANE_OP_PW_INSTALL
:
3215 case DPLANE_OP_PW_UNINSTALL
:
3216 case DPLANE_OP_SYS_ROUTE_ADD
:
3217 case DPLANE_OP_SYS_ROUTE_DELETE
:
3218 case DPLANE_OP_ADDR_INSTALL
:
3219 case DPLANE_OP_ADDR_UNINSTALL
:
3220 case DPLANE_OP_MAC_INSTALL
:
3221 case DPLANE_OP_MAC_DELETE
:
3222 case DPLANE_OP_NEIGH_INSTALL
:
3223 case DPLANE_OP_NEIGH_UPDATE
:
3224 case DPLANE_OP_NEIGH_DELETE
:
3225 case DPLANE_OP_NEIGH_IP_INSTALL
:
3226 case DPLANE_OP_NEIGH_IP_DELETE
:
3227 case DPLANE_OP_VTEP_ADD
:
3228 case DPLANE_OP_VTEP_DELETE
:
3229 case DPLANE_OP_RULE_ADD
:
3230 case DPLANE_OP_RULE_DELETE
:
3231 case DPLANE_OP_RULE_UPDATE
:
3232 case DPLANE_OP_NEIGH_DISCOVER
:
3233 case DPLANE_OP_BR_PORT_UPDATE
:
3234 case DPLANE_OP_NONE
:
3235 case DPLANE_OP_IPTABLE_ADD
:
3236 case DPLANE_OP_IPTABLE_DELETE
:
3237 case DPLANE_OP_IPSET_ADD
:
3238 case DPLANE_OP_IPSET_DELETE
:
3239 case DPLANE_OP_IPSET_ENTRY_ADD
:
3240 case DPLANE_OP_IPSET_ENTRY_DELETE
:
3241 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
3242 case DPLANE_OP_GRE_SET
:
3243 case DPLANE_OP_INTF_ADDR_ADD
:
3244 case DPLANE_OP_INTF_ADDR_DEL
:
3245 case DPLANE_OP_INTF_NETCONFIG
:
3246 case DPLANE_OP_INTF_INSTALL
:
3247 case DPLANE_OP_INTF_UPDATE
:
3248 case DPLANE_OP_INTF_DELETE
:
3249 case DPLANE_OP_TC_QDISC_INSTALL
:
3250 case DPLANE_OP_TC_QDISC_UNINSTALL
:
3251 case DPLANE_OP_TC_CLASS_ADD
:
3252 case DPLANE_OP_TC_CLASS_DELETE
:
3253 case DPLANE_OP_TC_CLASS_UPDATE
:
3254 case DPLANE_OP_TC_FILTER_ADD
:
3255 case DPLANE_OP_TC_FILTER_DELETE
:
3256 case DPLANE_OP_TC_FILTER_UPDATE
:
3261 static int zebra_nhg_sweep_entry(struct hash_bucket
*bucket
, void *arg
)
3263 struct nhg_hash_entry
*nhe
= NULL
;
3265 nhe
= (struct nhg_hash_entry
*)bucket
->data
;
3268 * same logic as with routes.
3270 * If older than startup time, we know we read them in from the
3271 * kernel and have not gotten and update for them since startup
3272 * from an upper level proto.
3274 if (zrouter
.startup_time
< nhe
->uptime
)
3275 return HASHWALK_CONTINUE
;
3278 * If it's proto-owned and not being used by a route, remove it since
3279 * we haven't gotten an update about it from the proto since startup.
3280 * This means that either the config for it was removed or the daemon
3281 * didn't get started. This handles graceful restart & retain scenario.
3283 if (PROTO_OWNED(nhe
) && nhe
->refcnt
== 1) {
3284 zebra_nhg_decrement_ref(nhe
);
3285 return HASHWALK_ABORT
;
3289 * If its being ref'd by routes, just let it be uninstalled via a route
3292 if (ZEBRA_NHG_CREATED(nhe
) && nhe
->refcnt
<= 0) {
3293 zebra_nhg_uninstall_kernel(nhe
);
3294 return HASHWALK_ABORT
;
3297 return HASHWALK_CONTINUE
;
3300 void zebra_nhg_sweep_table(struct hash
*hash
)
3305 * Yes this is extremely odd. Effectively nhg's have
3306 * other nexthop groups that depend on them and when you
3307 * remove them, you can have other entries blown up.
3308 * our hash code does not work with deleting multiple
3309 * entries at a time and will possibly cause crashes
3310 * So what to do? Whenever zebra_nhg_sweep_entry
3311 * deletes an entry it will return HASHWALK_ABORT,
3312 * cause that deletion might have triggered more.
3313 * then we can just keep sweeping this table
3314 * until nothing more is found to do.
3317 count
= hashcount(hash
);
3318 hash_walk(hash
, zebra_nhg_sweep_entry
, NULL
);
3319 } while (count
!= hashcount(hash
));
3322 static void zebra_nhg_mark_keep_entry(struct hash_bucket
*bucket
, void *arg
)
3324 struct nhg_hash_entry
*nhe
= bucket
->data
;
3326 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
3330 * When we are shutting down and we have retain mode enabled
3331 * in zebra the process is to mark each vrf that it's
3332 * routes should not be deleted. The problem with that
3333 * is that shutdown actually free's up memory which
3334 * causes the nexthop group's ref counts to go to zero
3335 * we need a way to subtly tell the system to not remove
3336 * the nexthop groups from the kernel at the same time.
3337 * The easiest just looks like that we should not mark
3338 * the nhg's as installed any more and when the ref count
3339 * goes to zero we'll attempt to delete and do nothing
3341 void zebra_nhg_mark_keep(void)
3343 hash_iterate(zrouter
.nhgs_id
, zebra_nhg_mark_keep_entry
, NULL
);
3346 /* Global control to disable use of kernel nexthops, if available. We can't
3347 * force the kernel to support nexthop ids, of course, but we can disable
3348 * zebra's use of them, for testing e.g. By default, if the kernel supports
3349 * nexthop ids, zebra uses them.
3351 void zebra_nhg_enable_kernel_nexthops(bool set
)
3353 g_nexthops_enabled
= set
;
3356 bool zebra_nhg_kernel_nexthops_enabled(void)
3358 return g_nexthops_enabled
;
3361 /* Global control for use of activated backups for recursive resolution. */
3362 void zebra_nhg_set_recursive_use_backups(bool set
)
3364 use_recursive_backups
= set
;
3367 bool zebra_nhg_recursive_use_backups(void)
3369 return use_recursive_backups
;
3373 * Global control to only use kernel nexthops for protocol created NHGs.
3374 * There are some use cases where you may not want zebra to implicitly
3375 * create kernel nexthops for all routes and only create them for NHGs
3376 * passed down by upper level protos.
3380 void zebra_nhg_set_proto_nexthops_only(bool set
)
3382 proto_nexthops_only
= set
;
3385 bool zebra_nhg_proto_nexthops_only(void)
3387 return proto_nexthops_only
;
3390 /* Add NHE from upper level proto */
3391 struct nhg_hash_entry
*zebra_nhg_proto_add(uint32_t id
, int type
,
3392 uint16_t instance
, uint32_t session
,
3393 struct nexthop_group
*nhg
, afi_t afi
)
3395 struct nhg_hash_entry lookup
;
3396 struct nhg_hash_entry
*new, *old
;
3397 struct nhg_connected
*rb_node_dep
= NULL
;
3398 struct nexthop
*newhop
;
3399 bool replace
= false;
3401 if (!nhg
->nexthop
) {
3402 if (IS_ZEBRA_DEBUG_NHG
)
3403 zlog_debug("%s: id %u, no nexthops passed to add",
3409 /* Set nexthop list as active, since they wont go through rib
3412 * Assuming valid/onlink for now.
3414 * Once resolution is figured out, we won't need this!
3416 for (ALL_NEXTHOPS_PTR(nhg
, newhop
)) {
3417 if (CHECK_FLAG(newhop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
3418 if (IS_ZEBRA_DEBUG_NHG
)
3420 "%s: id %u, backup nexthops not supported",
3425 if (newhop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
3426 if (IS_ZEBRA_DEBUG_NHG
)
3428 "%s: id %u, blackhole nexthop not supported",
3433 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
) {
3434 if (IS_ZEBRA_DEBUG_NHG
)
3436 "%s: id %u, nexthop without gateway not supported",
3441 if (!newhop
->ifindex
) {
3442 if (IS_ZEBRA_DEBUG_NHG
)
3444 "%s: id %u, nexthop without ifindex is not supported",
3448 SET_FLAG(newhop
->flags
, NEXTHOP_FLAG_ACTIVE
);
3451 zebra_nhe_init(&lookup
, afi
, nhg
->nexthop
);
3452 lookup
.nhg
.nexthop
= nhg
->nexthop
;
3453 lookup
.nhg
.nhgr
= nhg
->nhgr
;
3457 old
= zebra_nhg_lookup_id(id
);
3461 * This is a replace, just release NHE from ID for now, The
3462 * depends/dependents may still be used in the replacement so
3463 * we don't touch them other than to remove their refs to their
3467 hash_release(zrouter
.nhgs_id
, old
);
3469 /* Free all the things */
3470 zebra_nhg_release_all_deps(old
);
3473 new = zebra_nhg_rib_find_nhe(&lookup
, afi
);
3475 zebra_nhg_increment_ref(new);
3477 /* Capture zapi client info */
3478 new->zapi_instance
= instance
;
3479 new->zapi_session
= session
;
3481 zebra_nhg_set_valid_if_active(new);
3483 zebra_nhg_install_kernel(new);
3487 * Check to handle recving DEL while routes still in use then
3490 * In this case we would have decremented the refcnt already
3491 * but set the FLAG here. Go ahead and increment once to fix
3492 * the misordering we have been sent.
3494 if (CHECK_FLAG(old
->flags
, NEXTHOP_GROUP_PROTO_RELEASED
))
3495 zebra_nhg_increment_ref(old
);
3497 rib_handle_nhg_replace(old
, new);
3499 /* We have to decrement its singletons
3500 * because some might not exist in NEW.
3502 if (!zebra_nhg_depends_is_empty(old
)) {
3503 frr_each (nhg_connected_tree
, &old
->nhg_depends
,
3505 zebra_nhg_decrement_ref(rb_node_dep
->nhe
);
3508 /* Dont call the dec API, we dont want to uninstall the ID */
3510 THREAD_OFF(old
->timer
);
3511 zebra_nhg_free(old
);
3515 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
3516 zlog_debug("%s: %s nhe %p (%u), vrf %d, type %s", __func__
,
3517 (replace
? "replaced" : "added"), new, new->id
,
3518 new->vrf_id
, zebra_route_string(new->type
));
3523 /* Delete NHE from upper level proto, caller must decrement ref */
3524 struct nhg_hash_entry
*zebra_nhg_proto_del(uint32_t id
, int type
)
3526 struct nhg_hash_entry
*nhe
;
3528 nhe
= zebra_nhg_lookup_id(id
);
3531 if (IS_ZEBRA_DEBUG_NHG
)
3532 zlog_debug("%s: id %u, lookup failed", __func__
, id
);
3537 if (type
!= nhe
->type
) {
3538 if (IS_ZEBRA_DEBUG_NHG
)
3540 "%s: id %u, type %s mismatch, sent by %s, ignoring",
3541 __func__
, id
, zebra_route_string(nhe
->type
),
3542 zebra_route_string(type
));
3546 if (CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_PROTO_RELEASED
)) {
3547 if (IS_ZEBRA_DEBUG_NHG
)
3548 zlog_debug("%s: id %u, already released", __func__
, id
);
3553 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_PROTO_RELEASED
);
3555 if (nhe
->refcnt
> 1) {
3556 if (IS_ZEBRA_DEBUG_NHG
)
3558 "%s: %pNG, still being used by routes refcnt %u",
3559 __func__
, nhe
, nhe
->refcnt
);
3563 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
3564 zlog_debug("%s: deleted nhe %p (%pNG), vrf %d, type %s",
3565 __func__
, nhe
, nhe
, nhe
->vrf_id
,
3566 zebra_route_string(nhe
->type
));
3571 struct nhg_score_proto_iter
{
3576 static void zebra_nhg_score_proto_entry(struct hash_bucket
*bucket
, void *arg
)
3578 struct nhg_hash_entry
*nhe
;
3579 struct nhg_score_proto_iter
*iter
;
3581 nhe
= (struct nhg_hash_entry
*)bucket
->data
;
3584 /* Needs to match type and outside zebra ID space */
3585 if (nhe
->type
== iter
->type
&& PROTO_OWNED(nhe
)) {
3586 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
3588 "%s: found nhe %p (%pNG), vrf %d, type %s after client disconnect",
3589 __func__
, nhe
, nhe
, nhe
->vrf_id
,
3590 zebra_route_string(nhe
->type
));
3592 /* Add to removal list */
3593 listnode_add(iter
->found
, nhe
);
3597 /* Remove specific by proto NHGs */
3598 unsigned long zebra_nhg_score_proto(int type
)
3600 struct nhg_hash_entry
*nhe
;
3601 struct nhg_score_proto_iter iter
= {};
3602 struct listnode
*ln
;
3603 unsigned long count
;
3606 iter
.found
= list_new();
3608 /* Find matching entries to remove */
3609 hash_iterate(zrouter
.nhgs_id
, zebra_nhg_score_proto_entry
, &iter
);
3611 /* Now remove them */
3612 for (ALL_LIST_ELEMENTS_RO(iter
.found
, ln
, nhe
)) {
3614 * This should be the last ref if we remove client routes too,
3615 * and thus should remove and free them.
3617 zebra_nhg_decrement_ref(nhe
);
3620 count
= iter
.found
->count
;
3621 list_delete(&iter
.found
);
3626 printfrr_ext_autoreg_p("NG", printfrr_nhghe
);
3627 static ssize_t
printfrr_nhghe(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
3630 const struct nhg_hash_entry
*nhe
= ptr
;
3631 const struct nhg_connected
*dep
;
3635 return bputs(buf
, "[NULL]");
3637 ret
+= bprintfrr(buf
, "%u[", nhe
->id
);
3639 ret
+= printfrr_nhs(buf
, nhe
->nhg
.nexthop
);
3641 int count
= zebra_nhg_depends_count(nhe
);
3643 frr_each (nhg_connected_tree_const
, &nhe
->nhg_depends
, dep
) {
3644 ret
+= bprintfrr(buf
, "%u", dep
->nhe
->id
);
3646 ret
+= bputs(buf
, "/");
3651 ret
+= bputs(buf
, "]");