1 /* Zebra Nexthop Group Code.
2 * Copyright (C) 2019 Cumulus Networks, Inc.
6 * This file is part of FRR.
8 * FRR is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
13 * FRR is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with FRR; see the file COPYING. If not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 #include "lib/nexthop.h"
26 #include "lib/nexthop_group_private.h"
27 #include "lib/routemap.h"
29 #include "lib/jhash.h"
30 #include "lib/debug.h"
31 #include "lib/lib_errors.h"
33 #include "zebra/connected.h"
34 #include "zebra/debug.h"
35 #include "zebra/zebra_router.h"
36 #include "zebra/zebra_nhg_private.h"
37 #include "zebra/zebra_rnh.h"
38 #include "zebra/zebra_routemap.h"
39 #include "zebra/zebra_srte.h"
40 #include "zebra/zserv.h"
42 #include "zebra_errors.h"
43 #include "zebra_dplane.h"
44 #include "zebra/interface.h"
45 #include "zebra/zapi_msg.h"
46 #include "zebra/rib.h"
47 #include "zebra/zebra_vxlan.h"
49 DEFINE_MTYPE_STATIC(ZEBRA
, NHG
, "Nexthop Group Entry");
50 DEFINE_MTYPE_STATIC(ZEBRA
, NHG_CONNECTED
, "Nexthop Group Connected");
51 DEFINE_MTYPE_STATIC(ZEBRA
, NHG_CTX
, "Nexthop Group Context");
53 /* Map backup nexthop indices between two nhes */
54 struct backup_nh_map_s
{
63 /* id counter to keep in sync with kernel */
66 /* Controlled through ui */
67 static bool g_nexthops_enabled
= true;
68 static bool proto_nexthops_only
;
69 static bool use_recursive_backups
= true;
71 static struct nhg_hash_entry
*depends_find(const struct nexthop
*nh
, afi_t afi
,
72 int type
, bool from_dplane
);
73 static void depends_add(struct nhg_connected_tree_head
*head
,
74 struct nhg_hash_entry
*depend
);
75 static struct nhg_hash_entry
*
76 depends_find_add(struct nhg_connected_tree_head
*head
, struct nexthop
*nh
,
77 afi_t afi
, int type
, bool from_dplane
);
78 static struct nhg_hash_entry
*
79 depends_find_id_add(struct nhg_connected_tree_head
*head
, uint32_t id
);
80 static void depends_decrement_free(struct nhg_connected_tree_head
*head
);
82 static struct nhg_backup_info
*
83 nhg_backup_copy(const struct nhg_backup_info
*orig
);
85 /* Helper function for getting the next allocatable ID */
86 static uint32_t nhg_get_next_id(void)
91 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
92 zlog_debug("%s: ID %u checking", __func__
, id_counter
);
94 if (id_counter
== ZEBRA_NHG_PROTO_LOWER
) {
95 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
96 zlog_debug("%s: ID counter wrapped", __func__
);
102 if (zebra_nhg_lookup_id(id_counter
)) {
103 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
104 zlog_debug("%s: ID already exists", __func__
);
115 static void nhg_connected_free(struct nhg_connected
*dep
)
117 XFREE(MTYPE_NHG_CONNECTED
, dep
);
120 static struct nhg_connected
*nhg_connected_new(struct nhg_hash_entry
*nhe
)
122 struct nhg_connected
*new = NULL
;
124 new = XCALLOC(MTYPE_NHG_CONNECTED
, sizeof(struct nhg_connected
));
130 void nhg_connected_tree_free(struct nhg_connected_tree_head
*head
)
132 struct nhg_connected
*rb_node_dep
= NULL
;
134 if (!nhg_connected_tree_is_empty(head
)) {
135 frr_each_safe(nhg_connected_tree
, head
, rb_node_dep
) {
136 nhg_connected_tree_del(head
, rb_node_dep
);
137 nhg_connected_free(rb_node_dep
);
142 bool nhg_connected_tree_is_empty(const struct nhg_connected_tree_head
*head
)
144 return nhg_connected_tree_count(head
) ? false : true;
147 struct nhg_connected
*
148 nhg_connected_tree_root(struct nhg_connected_tree_head
*head
)
150 return nhg_connected_tree_first(head
);
153 struct nhg_hash_entry
*
154 nhg_connected_tree_del_nhe(struct nhg_connected_tree_head
*head
,
155 struct nhg_hash_entry
*depend
)
157 struct nhg_connected lookup
= {};
158 struct nhg_connected
*remove
= NULL
;
159 struct nhg_hash_entry
*removed_nhe
;
163 /* Lookup to find the element, then remove it */
164 remove
= nhg_connected_tree_find(head
, &lookup
);
166 /* Re-returning here just in case this API changes..
167 * the _del list api's are a bit undefined at the moment.
169 * So hopefully returning here will make it fail if the api
170 * changes to something different than currently expected.
172 remove
= nhg_connected_tree_del(head
, remove
);
174 /* If the entry was sucessfully removed, free the 'connected` struct */
176 removed_nhe
= remove
->nhe
;
177 nhg_connected_free(remove
);
184 /* Assuming UNIQUE RB tree. If this changes, assumptions here about
185 * insertion need to change.
187 struct nhg_hash_entry
*
188 nhg_connected_tree_add_nhe(struct nhg_connected_tree_head
*head
,
189 struct nhg_hash_entry
*depend
)
191 struct nhg_connected
*new = NULL
;
193 new = nhg_connected_new(depend
);
195 /* On success, NULL will be returned from the
198 if (new && (nhg_connected_tree_add(head
, new) == NULL
))
201 /* If it wasn't successful, it must be a duplicate. We enforce the
202 * unique property for the `nhg_connected` tree.
204 nhg_connected_free(new);
210 nhg_connected_tree_decrement_ref(struct nhg_connected_tree_head
*head
)
212 struct nhg_connected
*rb_node_dep
= NULL
;
214 frr_each_safe(nhg_connected_tree
, head
, rb_node_dep
) {
215 zebra_nhg_decrement_ref(rb_node_dep
->nhe
);
220 nhg_connected_tree_increment_ref(struct nhg_connected_tree_head
*head
)
222 struct nhg_connected
*rb_node_dep
= NULL
;
224 frr_each(nhg_connected_tree
, head
, rb_node_dep
) {
225 zebra_nhg_increment_ref(rb_node_dep
->nhe
);
229 struct nhg_hash_entry
*zebra_nhg_resolve(struct nhg_hash_entry
*nhe
)
231 if (CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_RECURSIVE
)
232 && !zebra_nhg_depends_is_empty(nhe
)) {
233 nhe
= nhg_connected_tree_root(&nhe
->nhg_depends
)->nhe
;
234 return zebra_nhg_resolve(nhe
);
240 unsigned int zebra_nhg_depends_count(const struct nhg_hash_entry
*nhe
)
242 return nhg_connected_tree_count(&nhe
->nhg_depends
);
245 bool zebra_nhg_depends_is_empty(const struct nhg_hash_entry
*nhe
)
247 return nhg_connected_tree_is_empty(&nhe
->nhg_depends
);
250 static void zebra_nhg_depends_del(struct nhg_hash_entry
*from
,
251 struct nhg_hash_entry
*depend
)
253 nhg_connected_tree_del_nhe(&from
->nhg_depends
, depend
);
256 static void zebra_nhg_depends_init(struct nhg_hash_entry
*nhe
)
258 nhg_connected_tree_init(&nhe
->nhg_depends
);
261 unsigned int zebra_nhg_dependents_count(const struct nhg_hash_entry
*nhe
)
263 return nhg_connected_tree_count(&nhe
->nhg_dependents
);
267 bool zebra_nhg_dependents_is_empty(const struct nhg_hash_entry
*nhe
)
269 return nhg_connected_tree_is_empty(&nhe
->nhg_dependents
);
272 static void zebra_nhg_dependents_del(struct nhg_hash_entry
*from
,
273 struct nhg_hash_entry
*dependent
)
275 nhg_connected_tree_del_nhe(&from
->nhg_dependents
, dependent
);
278 static void zebra_nhg_dependents_add(struct nhg_hash_entry
*to
,
279 struct nhg_hash_entry
*dependent
)
281 nhg_connected_tree_add_nhe(&to
->nhg_dependents
, dependent
);
284 static void zebra_nhg_dependents_init(struct nhg_hash_entry
*nhe
)
286 nhg_connected_tree_init(&nhe
->nhg_dependents
);
289 /* Release this nhe from anything depending on it */
290 static void zebra_nhg_dependents_release(struct nhg_hash_entry
*nhe
)
292 struct nhg_connected
*rb_node_dep
= NULL
;
294 frr_each_safe(nhg_connected_tree
, &nhe
->nhg_dependents
, rb_node_dep
) {
295 zebra_nhg_depends_del(rb_node_dep
->nhe
, nhe
);
296 /* recheck validity of the dependent */
297 zebra_nhg_check_valid(rb_node_dep
->nhe
);
301 /* Release this nhe from anything that it depends on */
302 static void zebra_nhg_depends_release(struct nhg_hash_entry
*nhe
)
304 if (!zebra_nhg_depends_is_empty(nhe
)) {
305 struct nhg_connected
*rb_node_dep
= NULL
;
307 frr_each_safe(nhg_connected_tree
, &nhe
->nhg_depends
,
309 zebra_nhg_dependents_del(rb_node_dep
->nhe
, nhe
);
315 struct nhg_hash_entry
*zebra_nhg_lookup_id(uint32_t id
)
317 struct nhg_hash_entry lookup
= {};
320 return hash_lookup(zrouter
.nhgs_id
, &lookup
);
323 static int zebra_nhg_insert_id(struct nhg_hash_entry
*nhe
)
325 if (hash_lookup(zrouter
.nhgs_id
, nhe
)) {
327 EC_ZEBRA_NHG_TABLE_INSERT_FAILED
,
328 "Failed inserting NHG %pNG into the ID hash table, entry already exists",
333 (void)hash_get(zrouter
.nhgs_id
, nhe
, hash_alloc_intern
);
338 static void zebra_nhg_set_if(struct nhg_hash_entry
*nhe
, struct interface
*ifp
)
341 if_nhg_dependents_add(ifp
, nhe
);
345 zebra_nhg_connect_depends(struct nhg_hash_entry
*nhe
,
346 struct nhg_connected_tree_head
*nhg_depends
)
348 struct nhg_connected
*rb_node_dep
= NULL
;
350 /* This has been allocated higher above in the stack. Could probably
351 * re-allocate and free the old stuff but just using the same memory
352 * for now. Otherwise, their might be a time trade-off for repeated
353 * alloc/frees as startup.
355 nhe
->nhg_depends
= *nhg_depends
;
357 /* Attach backpointer to anything that it depends on */
358 zebra_nhg_dependents_init(nhe
);
359 if (!zebra_nhg_depends_is_empty(nhe
)) {
360 frr_each(nhg_connected_tree
, &nhe
->nhg_depends
, rb_node_dep
) {
361 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
362 zlog_debug("%s: nhe %p (%pNG), dep %p (%pNG)",
363 __func__
, nhe
, nhe
, rb_node_dep
->nhe
,
366 zebra_nhg_dependents_add(rb_node_dep
->nhe
, nhe
);
371 /* Init an nhe, for use in a hash lookup for example */
372 void zebra_nhe_init(struct nhg_hash_entry
*nhe
, afi_t afi
,
373 const struct nexthop
*nh
)
375 memset(nhe
, 0, sizeof(struct nhg_hash_entry
));
376 nhe
->vrf_id
= VRF_DEFAULT
;
377 nhe
->type
= ZEBRA_ROUTE_NHG
;
378 nhe
->afi
= AFI_UNSPEC
;
380 /* There are some special rules that apply to groups representing
383 if (nh
&& (nh
->next
== NULL
)) {
385 case NEXTHOP_TYPE_IFINDEX
:
386 case NEXTHOP_TYPE_BLACKHOLE
:
388 * This switch case handles setting the afi different
389 * for ipv4/v6 routes. Ifindex/blackhole nexthop
390 * objects cannot be ambiguous, they must be Address
391 * Family specific. If we get here, we will either use
392 * the AF of the route, or the one we got passed from
393 * here from the kernel.
397 case NEXTHOP_TYPE_IPV4_IFINDEX
:
398 case NEXTHOP_TYPE_IPV4
:
401 case NEXTHOP_TYPE_IPV6_IFINDEX
:
402 case NEXTHOP_TYPE_IPV6
:
409 struct nhg_hash_entry
*zebra_nhg_alloc(void)
411 struct nhg_hash_entry
*nhe
;
413 nhe
= XCALLOC(MTYPE_NHG
, sizeof(struct nhg_hash_entry
));
419 * Allocate new nhe and make shallow copy of 'orig'; no
420 * recursive info is copied.
422 struct nhg_hash_entry
*zebra_nhe_copy(const struct nhg_hash_entry
*orig
,
425 struct nhg_hash_entry
*nhe
;
427 nhe
= zebra_nhg_alloc();
431 nexthop_group_copy(&(nhe
->nhg
), &(orig
->nhg
));
433 nhe
->vrf_id
= orig
->vrf_id
;
434 nhe
->afi
= orig
->afi
;
435 nhe
->type
= orig
->type
? orig
->type
: ZEBRA_ROUTE_NHG
;
437 nhe
->dplane_ref
= zebra_router_get_next_sequence();
439 /* Copy backup info also, if present */
440 if (orig
->backup_info
)
441 nhe
->backup_info
= nhg_backup_copy(orig
->backup_info
);
446 /* Allocation via hash handler */
447 static void *zebra_nhg_hash_alloc(void *arg
)
449 struct nhg_hash_entry
*nhe
= NULL
;
450 struct nhg_hash_entry
*copy
= arg
;
452 nhe
= zebra_nhe_copy(copy
, copy
->id
);
454 /* Mark duplicate nexthops in a group at creation time. */
455 nexthop_group_mark_duplicates(&(nhe
->nhg
));
458 * Add the ifp now if it's not a group or recursive and has ifindex.
460 * A proto-owned ID is always a group.
462 if (!PROTO_OWNED(nhe
) && nhe
->nhg
.nexthop
&& !nhe
->nhg
.nexthop
->next
463 && !nhe
->nhg
.nexthop
->resolved
&& nhe
->nhg
.nexthop
->ifindex
) {
464 struct interface
*ifp
= NULL
;
466 ifp
= if_lookup_by_index(nhe
->nhg
.nexthop
->ifindex
,
467 nhe
->nhg
.nexthop
->vrf_id
);
469 zebra_nhg_set_if(nhe
, ifp
);
471 if (IS_ZEBRA_DEBUG_NHG
)
473 "Failed to lookup an interface with ifindex=%d in vrf=%u for NHE %pNG",
474 nhe
->nhg
.nexthop
->ifindex
,
475 nhe
->nhg
.nexthop
->vrf_id
, nhe
);
482 uint32_t zebra_nhg_hash_key(const void *arg
)
484 const struct nhg_hash_entry
*nhe
= arg
;
485 uint32_t key
= 0x5a351234;
486 uint32_t primary
= 0;
489 primary
= nexthop_group_hash(&(nhe
->nhg
));
490 if (nhe
->backup_info
)
491 backup
= nexthop_group_hash(&(nhe
->backup_info
->nhe
->nhg
));
493 key
= jhash_3words(primary
, backup
, nhe
->type
, key
);
495 key
= jhash_2words(nhe
->vrf_id
, nhe
->afi
, key
);
500 uint32_t zebra_nhg_id_key(const void *arg
)
502 const struct nhg_hash_entry
*nhe
= arg
;
507 /* Helper with common nhg/nhe nexthop comparison logic */
508 static bool nhg_compare_nexthops(const struct nexthop
*nh1
,
509 const struct nexthop
*nh2
)
511 assert(nh1
!= NULL
&& nh2
!= NULL
);
514 * We have to check the active flag of each individual one,
515 * not just the overall active_num. This solves the special case
516 * issue of a route with a nexthop group with one nexthop
517 * resolving to itself and thus marking it inactive. If we
518 * have two different routes each wanting to mark a different
519 * nexthop inactive, they need to hash to two different groups.
521 * If we just hashed on num_active, they would hash the same
522 * which is incorrect.
526 * -> 1.1.1.1 dummy1 (inactive)
531 * -> 1.1.2.1 dummy2 (inactive)
533 * Without checking each individual one, they would hash to
534 * the same group and both have 1.1.1.1 dummy1 marked inactive.
537 if (CHECK_FLAG(nh1
->flags
, NEXTHOP_FLAG_ACTIVE
)
538 != CHECK_FLAG(nh2
->flags
, NEXTHOP_FLAG_ACTIVE
))
541 if (!nexthop_same(nh1
, nh2
))
547 bool zebra_nhg_hash_equal(const void *arg1
, const void *arg2
)
549 const struct nhg_hash_entry
*nhe1
= arg1
;
550 const struct nhg_hash_entry
*nhe2
= arg2
;
551 struct nexthop
*nexthop1
;
552 struct nexthop
*nexthop2
;
554 /* No matter what if they equal IDs, assume equal */
555 if (nhe1
->id
&& nhe2
->id
&& (nhe1
->id
== nhe2
->id
))
558 if (nhe1
->type
!= nhe2
->type
)
561 if (nhe1
->vrf_id
!= nhe2
->vrf_id
)
564 if (nhe1
->afi
!= nhe2
->afi
)
567 if (nhe1
->nhg
.nhgr
.buckets
!= nhe2
->nhg
.nhgr
.buckets
)
570 if (nhe1
->nhg
.nhgr
.idle_timer
!= nhe2
->nhg
.nhgr
.idle_timer
)
573 if (nhe1
->nhg
.nhgr
.unbalanced_timer
!= nhe2
->nhg
.nhgr
.unbalanced_timer
)
576 /* Nexthops should be in-order, so we simply compare them in-place */
577 for (nexthop1
= nhe1
->nhg
.nexthop
, nexthop2
= nhe2
->nhg
.nexthop
;
578 nexthop1
&& nexthop2
;
579 nexthop1
= nexthop1
->next
, nexthop2
= nexthop2
->next
) {
581 if (!nhg_compare_nexthops(nexthop1
, nexthop2
))
585 /* Check for unequal list lengths */
586 if (nexthop1
|| nexthop2
)
589 /* If there's no backup info, comparison is done. */
590 if ((nhe1
->backup_info
== NULL
) && (nhe2
->backup_info
== NULL
))
593 /* Compare backup info also - test the easy things first */
594 if (nhe1
->backup_info
&& (nhe2
->backup_info
== NULL
))
596 if (nhe2
->backup_info
&& (nhe1
->backup_info
== NULL
))
599 /* Compare number of backups before actually comparing any */
600 for (nexthop1
= nhe1
->backup_info
->nhe
->nhg
.nexthop
,
601 nexthop2
= nhe2
->backup_info
->nhe
->nhg
.nexthop
;
602 nexthop1
&& nexthop2
;
603 nexthop1
= nexthop1
->next
, nexthop2
= nexthop2
->next
) {
607 /* Did we find the end of one list before the other? */
608 if (nexthop1
|| nexthop2
)
611 /* Have to compare the backup nexthops */
612 for (nexthop1
= nhe1
->backup_info
->nhe
->nhg
.nexthop
,
613 nexthop2
= nhe2
->backup_info
->nhe
->nhg
.nexthop
;
614 nexthop1
&& nexthop2
;
615 nexthop1
= nexthop1
->next
, nexthop2
= nexthop2
->next
) {
617 if (!nhg_compare_nexthops(nexthop1
, nexthop2
))
624 bool zebra_nhg_hash_id_equal(const void *arg1
, const void *arg2
)
626 const struct nhg_hash_entry
*nhe1
= arg1
;
627 const struct nhg_hash_entry
*nhe2
= arg2
;
629 return nhe1
->id
== nhe2
->id
;
632 static int zebra_nhg_process_grp(struct nexthop_group
*nhg
,
633 struct nhg_connected_tree_head
*depends
,
634 struct nh_grp
*grp
, uint8_t count
,
635 struct nhg_resilience
*resilience
)
637 nhg_connected_tree_init(depends
);
639 for (int i
= 0; i
< count
; i
++) {
640 struct nhg_hash_entry
*depend
= NULL
;
641 /* We do not care about nexthop_grp.weight at
642 * this time. But we should figure out
643 * how to adapt this to our code in
646 depend
= depends_find_id_add(depends
, grp
[i
].id
);
651 "Received Nexthop Group from the kernel with a dependent Nexthop ID (%u) which we do not have in our table",
657 * If this is a nexthop with its own group
658 * dependencies, add them as well. Not sure its
659 * even possible to have a group within a group
663 copy_nexthops(&nhg
->nexthop
, depend
->nhg
.nexthop
, NULL
);
667 nhg
->nhgr
= *resilience
;
672 static void handle_recursive_depend(struct nhg_connected_tree_head
*nhg_depends
,
673 struct nexthop
*nh
, afi_t afi
, int type
)
675 struct nhg_hash_entry
*depend
= NULL
;
676 struct nexthop_group resolved_ng
= {};
678 resolved_ng
.nexthop
= nh
;
680 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
681 zlog_debug("%s: head %p, nh %pNHv",
682 __func__
, nhg_depends
, nh
);
684 depend
= zebra_nhg_rib_find(0, &resolved_ng
, afi
, type
);
686 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
687 zlog_debug("%s: nh %pNHv => %p (%u)",
688 __func__
, nh
, depend
,
689 depend
? depend
->id
: 0);
692 depends_add(nhg_depends
, depend
);
696 * Lookup an nhe in the global hash, using data from another nhe. If 'lookup'
697 * has an id value, that's used. Create a new global/shared nhe if not found.
699 static bool zebra_nhe_find(struct nhg_hash_entry
**nhe
, /* return value */
700 struct nhg_hash_entry
*lookup
,
701 struct nhg_connected_tree_head
*nhg_depends
,
702 afi_t afi
, bool from_dplane
)
704 bool created
= false;
705 bool recursive
= false;
706 struct nhg_hash_entry
*newnhe
, *backup_nhe
;
707 struct nexthop
*nh
= NULL
;
709 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
711 "%s: id %u, lookup %p, vrf %d, type %d, depends %p%s",
712 __func__
, lookup
->id
, lookup
, lookup
->vrf_id
,
713 lookup
->type
, nhg_depends
,
714 (from_dplane
? " (from dplane)" : ""));
717 (*nhe
) = zebra_nhg_lookup_id(lookup
->id
);
719 (*nhe
) = hash_lookup(zrouter
.nhgs
, lookup
);
721 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
722 zlog_debug("%s: lookup => %p (%pNG)", __func__
, *nhe
, *nhe
);
724 /* If we found an existing object, we're done */
728 /* We're going to create/insert a new nhe:
729 * assign the next global id value if necessary.
732 lookup
->id
= nhg_get_next_id();
734 if (!from_dplane
&& lookup
->id
< ZEBRA_NHG_PROTO_LOWER
) {
736 * This is a zebra hashed/owned NHG.
738 * It goes in HASH and ID table.
740 newnhe
= hash_get(zrouter
.nhgs
, lookup
, zebra_nhg_hash_alloc
);
741 zebra_nhg_insert_id(newnhe
);
744 * This is upperproto owned NHG or one we read in from dataplane
745 * and should not be hashed to.
747 * It goes in ID table.
750 hash_get(zrouter
.nhgs_id
, lookup
, zebra_nhg_hash_alloc
);
755 /* Mail back the new object */
758 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
759 zlog_debug("%s: => created %p (%pNG)", __func__
, newnhe
,
762 /* Only hash/lookup the depends if the first lookup
763 * fails to find something. This should hopefully save a
764 * lot of cycles for larger ecmp sizes.
767 /* If you don't want to hash on each nexthop in the
768 * nexthop group struct you can pass the depends
769 * directly. Kernel-side we do this since it just looks
772 zebra_nhg_connect_depends(newnhe
, nhg_depends
);
776 /* Prepare dependency relationships if this is not a
777 * singleton nexthop. There are two cases: a single
778 * recursive nexthop, where we need a relationship to the
779 * resolving nexthop; or a group of nexthops, where we need
780 * relationships with the corresponding singletons.
782 zebra_nhg_depends_init(newnhe
);
784 nh
= newnhe
->nhg
.nexthop
;
786 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ACTIVE
))
787 SET_FLAG(newnhe
->flags
, NEXTHOP_GROUP_VALID
);
789 if (nh
->next
== NULL
&& newnhe
->id
< ZEBRA_NHG_PROTO_LOWER
) {
790 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
)) {
791 /* Single recursive nexthop */
792 handle_recursive_depend(&newnhe
->nhg_depends
,
798 /* Proto-owned are groups by default */
799 /* List of nexthops */
800 for (nh
= newnhe
->nhg
.nexthop
; nh
; nh
= nh
->next
) {
801 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
802 zlog_debug("%s: depends NH %pNHv %s",
804 CHECK_FLAG(nh
->flags
,
805 NEXTHOP_FLAG_RECURSIVE
) ?
808 depends_find_add(&newnhe
->nhg_depends
, nh
, afi
,
809 newnhe
->type
, from_dplane
);
814 SET_FLAG(newnhe
->flags
, NEXTHOP_GROUP_RECURSIVE
);
816 /* Attach dependent backpointers to singletons */
817 zebra_nhg_connect_depends(newnhe
, &newnhe
->nhg_depends
);
823 if (zebra_nhg_get_backup_nhg(newnhe
) == NULL
||
824 zebra_nhg_get_backup_nhg(newnhe
)->nexthop
== NULL
)
827 /* If there are backup nexthops, add them to the backup
828 * depends tree. The rules here are a little different.
831 backup_nhe
= newnhe
->backup_info
->nhe
;
833 nh
= backup_nhe
->nhg
.nexthop
;
835 /* Singleton recursive NH */
836 if (nh
->next
== NULL
&&
837 CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
)) {
838 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
839 zlog_debug("%s: backup depend NH %pNHv (R)",
842 /* Single recursive nexthop */
843 handle_recursive_depend(&backup_nhe
->nhg_depends
, nh
->resolved
,
844 afi
, backup_nhe
->type
);
847 /* One or more backup NHs */
848 for (; nh
; nh
= nh
->next
) {
849 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
850 zlog_debug("%s: backup depend NH %pNHv %s",
852 CHECK_FLAG(nh
->flags
,
853 NEXTHOP_FLAG_RECURSIVE
) ?
856 depends_find_add(&backup_nhe
->nhg_depends
, nh
, afi
,
857 backup_nhe
->type
, from_dplane
);
862 SET_FLAG(backup_nhe
->flags
, NEXTHOP_GROUP_RECURSIVE
);
865 /* Reset time since last update */
866 (*nhe
)->uptime
= monotime(NULL
);
872 * Lookup or create an nhe, based on an nhg or an nhe id.
874 static bool zebra_nhg_find(struct nhg_hash_entry
**nhe
, uint32_t id
,
875 struct nexthop_group
*nhg
,
876 struct nhg_connected_tree_head
*nhg_depends
,
877 vrf_id_t vrf_id
, afi_t afi
, int type
,
880 struct nhg_hash_entry lookup
= {};
881 bool created
= false;
883 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
884 zlog_debug("%s: id %u, nhg %p, vrf %d, type %d, depends %p",
885 __func__
, id
, nhg
, vrf_id
, type
,
888 /* Use a temporary nhe and call into the superset/common code */
890 lookup
.type
= type
? type
: ZEBRA_ROUTE_NHG
;
893 lookup
.vrf_id
= vrf_id
;
894 if (nhg_depends
|| lookup
.nhg
.nexthop
->next
) {
895 /* Groups can have all vrfs and AF's in them */
896 lookup
.afi
= AFI_UNSPEC
;
898 switch (lookup
.nhg
.nexthop
->type
) {
899 case (NEXTHOP_TYPE_IFINDEX
):
900 case (NEXTHOP_TYPE_BLACKHOLE
):
902 * This switch case handles setting the afi different
903 * for ipv4/v6 routes. Ifindex/blackhole nexthop
904 * objects cannot be ambiguous, they must be Address
905 * Family specific. If we get here, we will either use
906 * the AF of the route, or the one we got passed from
907 * here from the kernel.
911 case (NEXTHOP_TYPE_IPV4_IFINDEX
):
912 case (NEXTHOP_TYPE_IPV4
):
915 case (NEXTHOP_TYPE_IPV6_IFINDEX
):
916 case (NEXTHOP_TYPE_IPV6
):
917 lookup
.afi
= AFI_IP6
;
922 created
= zebra_nhe_find(nhe
, &lookup
, nhg_depends
, afi
, from_dplane
);
927 /* Find/create a single nexthop */
928 static struct nhg_hash_entry
*zebra_nhg_find_nexthop(uint32_t id
,
933 struct nhg_hash_entry
*nhe
= NULL
;
934 struct nexthop_group nhg
= {};
935 vrf_id_t vrf_id
= !vrf_is_backend_netns() ? VRF_DEFAULT
: nh
->vrf_id
;
937 nexthop_group_add_sorted(&nhg
, nh
);
939 zebra_nhg_find(&nhe
, id
, &nhg
, NULL
, vrf_id
, afi
, type
, from_dplane
);
941 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
942 zlog_debug("%s: nh %pNHv => %p (%pNG)", __func__
, nh
, nhe
, nhe
);
947 static uint32_t nhg_ctx_get_id(const struct nhg_ctx
*ctx
)
952 static void nhg_ctx_set_status(struct nhg_ctx
*ctx
, enum nhg_ctx_status status
)
954 ctx
->status
= status
;
957 static enum nhg_ctx_status
nhg_ctx_get_status(const struct nhg_ctx
*ctx
)
962 static void nhg_ctx_set_op(struct nhg_ctx
*ctx
, enum nhg_ctx_op_e op
)
967 static enum nhg_ctx_op_e
nhg_ctx_get_op(const struct nhg_ctx
*ctx
)
972 static vrf_id_t
nhg_ctx_get_vrf_id(const struct nhg_ctx
*ctx
)
977 static int nhg_ctx_get_type(const struct nhg_ctx
*ctx
)
982 static int nhg_ctx_get_afi(const struct nhg_ctx
*ctx
)
987 static struct nexthop
*nhg_ctx_get_nh(struct nhg_ctx
*ctx
)
992 static uint8_t nhg_ctx_get_count(const struct nhg_ctx
*ctx
)
997 static struct nh_grp
*nhg_ctx_get_grp(struct nhg_ctx
*ctx
)
1002 static struct nhg_resilience
*nhg_ctx_get_resilience(struct nhg_ctx
*ctx
)
1004 return &ctx
->resilience
;
1007 static struct nhg_ctx
*nhg_ctx_new(void)
1009 struct nhg_ctx
*new;
1011 new = XCALLOC(MTYPE_NHG_CTX
, sizeof(struct nhg_ctx
));
1016 void nhg_ctx_free(struct nhg_ctx
**ctx
)
1023 assert((*ctx
) != NULL
);
1025 if (nhg_ctx_get_count(*ctx
))
1028 nh
= nhg_ctx_get_nh(*ctx
);
1030 nexthop_del_labels(nh
);
1031 nexthop_del_srv6_seg6local(nh
);
1032 nexthop_del_srv6_seg6(nh
);
1035 XFREE(MTYPE_NHG_CTX
, *ctx
);
1038 static struct nhg_ctx
*nhg_ctx_init(uint32_t id
, struct nexthop
*nh
,
1039 struct nh_grp
*grp
, vrf_id_t vrf_id
,
1040 afi_t afi
, int type
, uint8_t count
,
1041 struct nhg_resilience
*resilience
)
1043 struct nhg_ctx
*ctx
= NULL
;
1045 ctx
= nhg_ctx_new();
1048 ctx
->vrf_id
= vrf_id
;
1054 ctx
->resilience
= *resilience
;
1057 /* Copy over the array */
1058 memcpy(&ctx
->u
.grp
, grp
, count
* sizeof(struct nh_grp
));
1065 static void zebra_nhg_set_valid(struct nhg_hash_entry
*nhe
)
1067 struct nhg_connected
*rb_node_dep
;
1069 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
1071 frr_each(nhg_connected_tree
, &nhe
->nhg_dependents
, rb_node_dep
)
1072 zebra_nhg_set_valid(rb_node_dep
->nhe
);
1075 static void zebra_nhg_set_invalid(struct nhg_hash_entry
*nhe
)
1077 struct nhg_connected
*rb_node_dep
;
1079 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
1081 /* If we're in shutdown, this interface event needs to clean
1082 * up installed NHGs, so don't clear that flag directly.
1084 if (!zebra_router_in_shutdown())
1085 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
1087 /* Update validity of nexthops depending on it */
1088 frr_each(nhg_connected_tree
, &nhe
->nhg_dependents
, rb_node_dep
)
1089 zebra_nhg_check_valid(rb_node_dep
->nhe
);
1092 void zebra_nhg_check_valid(struct nhg_hash_entry
*nhe
)
1094 struct nhg_connected
*rb_node_dep
= NULL
;
1097 /* If anthing else in the group is valid, the group is valid */
1098 frr_each(nhg_connected_tree
, &nhe
->nhg_depends
, rb_node_dep
) {
1099 if (CHECK_FLAG(rb_node_dep
->nhe
->flags
, NEXTHOP_GROUP_VALID
)) {
1107 zebra_nhg_set_valid(nhe
);
1109 zebra_nhg_set_invalid(nhe
);
1112 static void zebra_nhg_release_all_deps(struct nhg_hash_entry
*nhe
)
1114 /* Remove it from any lists it may be on */
1115 zebra_nhg_depends_release(nhe
);
1116 zebra_nhg_dependents_release(nhe
);
1118 if_nhg_dependents_del(nhe
->ifp
, nhe
);
1121 static void zebra_nhg_release(struct nhg_hash_entry
*nhe
)
1123 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1124 zlog_debug("%s: nhe %p (%pNG)", __func__
, nhe
, nhe
);
1126 zebra_nhg_release_all_deps(nhe
);
1129 * If its not zebra owned, we didn't store it here and have to be
1130 * sure we don't clear one thats actually being used.
1132 if (nhe
->id
< ZEBRA_NHG_PROTO_LOWER
)
1133 hash_release(zrouter
.nhgs
, nhe
);
1135 hash_release(zrouter
.nhgs_id
, nhe
);
1138 static void zebra_nhg_handle_uninstall(struct nhg_hash_entry
*nhe
)
1140 zebra_nhg_release(nhe
);
1141 zebra_nhg_free(nhe
);
1144 static void zebra_nhg_handle_install(struct nhg_hash_entry
*nhe
)
1146 /* Update validity of groups depending on it */
1147 struct nhg_connected
*rb_node_dep
;
1149 frr_each_safe(nhg_connected_tree
, &nhe
->nhg_dependents
, rb_node_dep
)
1150 zebra_nhg_set_valid(rb_node_dep
->nhe
);
1154 * The kernel/other program has changed the state of a nexthop object we are
1157 static void zebra_nhg_handle_kernel_state_change(struct nhg_hash_entry
*nhe
,
1163 "Kernel %s a nexthop group with ID (%pNG) that we are still using for a route, sending it back down",
1164 (is_delete
? "deleted" : "updated"), nhe
);
1166 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
1167 zebra_nhg_install_kernel(nhe
);
1169 zebra_nhg_handle_uninstall(nhe
);
1172 static int nhg_ctx_process_new(struct nhg_ctx
*ctx
)
1174 struct nexthop_group
*nhg
= NULL
;
1175 struct nhg_connected_tree_head nhg_depends
= {};
1176 struct nhg_hash_entry
*lookup
= NULL
;
1177 struct nhg_hash_entry
*nhe
= NULL
;
1179 uint32_t id
= nhg_ctx_get_id(ctx
);
1180 uint8_t count
= nhg_ctx_get_count(ctx
);
1181 vrf_id_t vrf_id
= nhg_ctx_get_vrf_id(ctx
);
1182 int type
= nhg_ctx_get_type(ctx
);
1183 afi_t afi
= nhg_ctx_get_afi(ctx
);
1185 lookup
= zebra_nhg_lookup_id(id
);
1187 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1188 zlog_debug("%s: id %u, count %d, lookup => %p",
1189 __func__
, id
, count
, lookup
);
1192 /* This is already present in our table, hence an update
1193 * that we did not initate.
1195 zebra_nhg_handle_kernel_state_change(lookup
, false);
1199 if (nhg_ctx_get_count(ctx
)) {
1200 nhg
= nexthop_group_new();
1201 if (zebra_nhg_process_grp(nhg
, &nhg_depends
,
1202 nhg_ctx_get_grp(ctx
), count
,
1203 nhg_ctx_get_resilience(ctx
))) {
1204 depends_decrement_free(&nhg_depends
);
1205 nexthop_group_delete(&nhg
);
1209 if (!zebra_nhg_find(&nhe
, id
, nhg
, &nhg_depends
, vrf_id
, afi
,
1211 depends_decrement_free(&nhg_depends
);
1213 /* These got copied over in zebra_nhg_alloc() */
1214 nexthop_group_delete(&nhg
);
1216 nhe
= zebra_nhg_find_nexthop(id
, nhg_ctx_get_nh(ctx
), afi
, type
,
1221 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
1222 "Zebra failed to find or create a nexthop hash entry for ID (%u)",
1227 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1228 zlog_debug("%s: nhe %p (%pNG) is new", __func__
, nhe
, nhe
);
1231 * If daemon nhg from the kernel, add a refcnt here to indicate the
1234 if (PROTO_OWNED(nhe
))
1235 zebra_nhg_increment_ref(nhe
);
1237 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
1238 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
1243 static int nhg_ctx_process_del(struct nhg_ctx
*ctx
)
1245 struct nhg_hash_entry
*nhe
= NULL
;
1246 uint32_t id
= nhg_ctx_get_id(ctx
);
1248 nhe
= zebra_nhg_lookup_id(id
);
1252 EC_ZEBRA_BAD_NHG_MESSAGE
,
1253 "Kernel delete message received for nexthop group ID (%u) that we do not have in our ID table",
1258 zebra_nhg_handle_kernel_state_change(nhe
, true);
1263 static void nhg_ctx_fini(struct nhg_ctx
**ctx
)
1266 * Just freeing for now, maybe do something more in the future
1273 static int queue_add(struct nhg_ctx
*ctx
)
1275 /* If its queued or already processed do nothing */
1276 if (nhg_ctx_get_status(ctx
) == NHG_CTX_QUEUED
)
1279 if (rib_queue_nhg_ctx_add(ctx
)) {
1280 nhg_ctx_set_status(ctx
, NHG_CTX_FAILURE
);
1284 nhg_ctx_set_status(ctx
, NHG_CTX_QUEUED
);
1289 int nhg_ctx_process(struct nhg_ctx
*ctx
)
1293 switch (nhg_ctx_get_op(ctx
)) {
1294 case NHG_CTX_OP_NEW
:
1295 ret
= nhg_ctx_process_new(ctx
);
1296 if (nhg_ctx_get_count(ctx
) && ret
== -ENOENT
1297 && nhg_ctx_get_status(ctx
) != NHG_CTX_REQUEUED
) {
1299 * We have entered a situation where we are
1300 * processing a group from the kernel
1301 * that has a contained nexthop which
1302 * we have not yet processed.
1304 * Re-enqueue this ctx to be handled exactly one
1305 * more time (indicated by the flag).
1307 * By the time we get back to it, we
1308 * should have processed its depends.
1310 nhg_ctx_set_status(ctx
, NHG_CTX_NONE
);
1311 if (queue_add(ctx
) == 0) {
1312 nhg_ctx_set_status(ctx
, NHG_CTX_REQUEUED
);
1317 case NHG_CTX_OP_DEL
:
1318 ret
= nhg_ctx_process_del(ctx
);
1319 case NHG_CTX_OP_NONE
:
1323 nhg_ctx_set_status(ctx
, (ret
? NHG_CTX_FAILURE
: NHG_CTX_SUCCESS
));
1330 /* Kernel-side, you either get a single new nexthop or a array of ID's */
1331 int zebra_nhg_kernel_find(uint32_t id
, struct nexthop
*nh
, struct nh_grp
*grp
,
1332 uint8_t count
, vrf_id_t vrf_id
, afi_t afi
, int type
,
1333 int startup
, struct nhg_resilience
*nhgr
)
1335 struct nhg_ctx
*ctx
= NULL
;
1337 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1338 zlog_debug("%s: nh %pNHv, id %u, count %d",
1339 __func__
, nh
, id
, (int)count
);
1341 if (id
> id_counter
&& id
< ZEBRA_NHG_PROTO_LOWER
)
1342 /* Increase our counter so we don't try to create
1343 * an ID that already exists
1347 ctx
= nhg_ctx_init(id
, nh
, grp
, vrf_id
, afi
, type
, count
, nhgr
);
1348 nhg_ctx_set_op(ctx
, NHG_CTX_OP_NEW
);
1350 /* Under statup conditions, we need to handle them immediately
1351 * like we do for routes. Otherwise, we are going to get a route
1352 * with a nhe_id that we have not handled.
1355 return nhg_ctx_process(ctx
);
1357 if (queue_add(ctx
)) {
1365 /* Kernel-side, received delete message */
1366 int zebra_nhg_kernel_del(uint32_t id
, vrf_id_t vrf_id
)
1368 struct nhg_ctx
*ctx
= NULL
;
1370 ctx
= nhg_ctx_init(id
, NULL
, NULL
, vrf_id
, 0, 0, 0, NULL
);
1372 nhg_ctx_set_op(ctx
, NHG_CTX_OP_DEL
);
1374 if (queue_add(ctx
)) {
1382 /* Some dependency helper functions */
1383 static struct nhg_hash_entry
*depends_find_recursive(const struct nexthop
*nh
,
1384 afi_t afi
, int type
)
1386 struct nhg_hash_entry
*nhe
;
1387 struct nexthop
*lookup
= NULL
;
1389 lookup
= nexthop_dup(nh
, NULL
);
1391 nhe
= zebra_nhg_find_nexthop(0, lookup
, afi
, type
, false);
1393 nexthops_free(lookup
);
1398 static struct nhg_hash_entry
*depends_find_singleton(const struct nexthop
*nh
,
1399 afi_t afi
, int type
,
1402 struct nhg_hash_entry
*nhe
;
1403 struct nexthop lookup
= {};
1405 /* Capture a snapshot of this single nh; it might be part of a list,
1406 * so we need to make a standalone copy.
1408 nexthop_copy_no_recurse(&lookup
, nh
, NULL
);
1410 nhe
= zebra_nhg_find_nexthop(0, &lookup
, afi
, type
, from_dplane
);
1412 /* The copy may have allocated labels; free them if necessary. */
1413 nexthop_del_labels(&lookup
);
1414 nexthop_del_srv6_seg6local(&lookup
);
1415 nexthop_del_srv6_seg6(&lookup
);
1417 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1418 zlog_debug("%s: nh %pNHv => %p (%pNG)", __func__
, nh
, nhe
, nhe
);
1423 static struct nhg_hash_entry
*depends_find(const struct nexthop
*nh
, afi_t afi
,
1424 int type
, bool from_dplane
)
1426 struct nhg_hash_entry
*nhe
= NULL
;
1431 /* We are separating these functions out to increase handling speed
1432 * in the non-recursive case (by not alloc/freeing)
1434 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1435 nhe
= depends_find_recursive(nh
, afi
, type
);
1437 nhe
= depends_find_singleton(nh
, afi
, type
, from_dplane
);
1440 if (IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1441 zlog_debug("%s: nh %pNHv %s => %p (%pNG)", __func__
, nh
,
1442 CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
) ? "(R)"
1451 static void depends_add(struct nhg_connected_tree_head
*head
,
1452 struct nhg_hash_entry
*depend
)
1454 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1455 zlog_debug("%s: head %p nh %pNHv",
1456 __func__
, head
, depend
->nhg
.nexthop
);
1458 /* If NULL is returned, it was successfully added and
1459 * needs to have its refcnt incremented.
1461 * Else the NHE is already present in the tree and doesn't
1462 * need to increment the refcnt.
1464 if (nhg_connected_tree_add_nhe(head
, depend
) == NULL
)
1465 zebra_nhg_increment_ref(depend
);
1468 static struct nhg_hash_entry
*
1469 depends_find_add(struct nhg_connected_tree_head
*head
, struct nexthop
*nh
,
1470 afi_t afi
, int type
, bool from_dplane
)
1472 struct nhg_hash_entry
*depend
= NULL
;
1474 depend
= depends_find(nh
, afi
, type
, from_dplane
);
1476 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1477 zlog_debug("%s: nh %pNHv => %p",
1478 __func__
, nh
, depend
);
1481 depends_add(head
, depend
);
1486 static struct nhg_hash_entry
*
1487 depends_find_id_add(struct nhg_connected_tree_head
*head
, uint32_t id
)
1489 struct nhg_hash_entry
*depend
= NULL
;
1491 depend
= zebra_nhg_lookup_id(id
);
1494 depends_add(head
, depend
);
1499 static void depends_decrement_free(struct nhg_connected_tree_head
*head
)
1501 nhg_connected_tree_decrement_ref(head
);
1502 nhg_connected_tree_free(head
);
1505 /* Find an nhe based on a list of nexthops */
1506 struct nhg_hash_entry
*zebra_nhg_rib_find(uint32_t id
,
1507 struct nexthop_group
*nhg
,
1508 afi_t rt_afi
, int type
)
1510 struct nhg_hash_entry
*nhe
= NULL
;
1514 * CLANG SA is complaining that nexthop may be NULL
1515 * Make it happy but this is ridonc
1517 assert(nhg
->nexthop
);
1518 vrf_id
= !vrf_is_backend_netns() ? VRF_DEFAULT
: nhg
->nexthop
->vrf_id
;
1520 zebra_nhg_find(&nhe
, id
, nhg
, NULL
, vrf_id
, rt_afi
, type
, false);
1522 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1523 zlog_debug("%s: => nhe %p (%pNG)", __func__
, nhe
, nhe
);
1528 /* Find an nhe based on a route's nhe */
1529 struct nhg_hash_entry
*
1530 zebra_nhg_rib_find_nhe(struct nhg_hash_entry
*rt_nhe
, afi_t rt_afi
)
1532 struct nhg_hash_entry
*nhe
= NULL
;
1534 if (!(rt_nhe
&& rt_nhe
->nhg
.nexthop
)) {
1535 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
1536 "No nexthop passed to %s", __func__
);
1540 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1541 zlog_debug("%s: rt_nhe %p (%pNG)", __func__
, rt_nhe
, rt_nhe
);
1543 zebra_nhe_find(&nhe
, rt_nhe
, NULL
, rt_afi
, false);
1545 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1546 zlog_debug("%s: => nhe %p (%pNG)", __func__
, nhe
, nhe
);
1552 * Allocate backup nexthop info object. Typically these are embedded in
1553 * nhg_hash_entry objects.
1555 struct nhg_backup_info
*zebra_nhg_backup_alloc(void)
1557 struct nhg_backup_info
*p
;
1559 p
= XCALLOC(MTYPE_NHG
, sizeof(struct nhg_backup_info
));
1561 p
->nhe
= zebra_nhg_alloc();
1563 /* Identify the embedded group used to hold the list of backups */
1564 SET_FLAG(p
->nhe
->flags
, NEXTHOP_GROUP_BACKUP
);
1570 * Free backup nexthop info object, deal with any embedded allocations
1572 void zebra_nhg_backup_free(struct nhg_backup_info
**p
)
1576 zebra_nhg_free((*p
)->nhe
);
1578 XFREE(MTYPE_NHG
, (*p
));
1582 /* Accessor for backup nexthop group */
1583 struct nexthop_group
*zebra_nhg_get_backup_nhg(struct nhg_hash_entry
*nhe
)
1585 struct nexthop_group
*p
= NULL
;
1588 if (nhe
->backup_info
&& nhe
->backup_info
->nhe
)
1589 p
= &(nhe
->backup_info
->nhe
->nhg
);
1596 * Helper to return a copy of a backup_info - note that this is a shallow
1597 * copy, meant to be used when creating a new nhe from info passed in with
1600 static struct nhg_backup_info
*
1601 nhg_backup_copy(const struct nhg_backup_info
*orig
)
1603 struct nhg_backup_info
*b
;
1605 b
= zebra_nhg_backup_alloc();
1607 /* Copy list of nexthops */
1608 nexthop_group_copy(&(b
->nhe
->nhg
), &(orig
->nhe
->nhg
));
1613 static void zebra_nhg_free_members(struct nhg_hash_entry
*nhe
)
1615 nexthops_free(nhe
->nhg
.nexthop
);
1617 zebra_nhg_backup_free(&nhe
->backup_info
);
1619 /* Decrement to remove connection ref */
1620 nhg_connected_tree_decrement_ref(&nhe
->nhg_depends
);
1621 nhg_connected_tree_free(&nhe
->nhg_depends
);
1622 nhg_connected_tree_free(&nhe
->nhg_dependents
);
1625 void zebra_nhg_free(struct nhg_hash_entry
*nhe
)
1627 if (IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1628 /* Group or singleton? */
1629 if (nhe
->nhg
.nexthop
&& nhe
->nhg
.nexthop
->next
)
1630 zlog_debug("%s: nhe %p (%pNG), refcnt %d", __func__
,
1631 nhe
, nhe
, nhe
->refcnt
);
1633 zlog_debug("%s: nhe %p (%pNG), refcnt %d, NH %pNHv",
1634 __func__
, nhe
, nhe
, nhe
->refcnt
,
1638 THREAD_OFF(nhe
->timer
);
1640 zebra_nhg_free_members(nhe
);
1642 XFREE(MTYPE_NHG
, nhe
);
1646 * Let's just drop the memory associated with each item
1648 void zebra_nhg_hash_free(void *p
)
1650 struct nhg_hash_entry
*nhe
= p
;
1652 if (IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1653 /* Group or singleton? */
1654 if (nhe
->nhg
.nexthop
&& nhe
->nhg
.nexthop
->next
)
1655 zlog_debug("%s: nhe %p (%u), refcnt %d", __func__
, nhe
,
1656 nhe
->id
, nhe
->refcnt
);
1658 zlog_debug("%s: nhe %p (%pNG), refcnt %d, NH %pNHv",
1659 __func__
, nhe
, nhe
, nhe
->refcnt
,
1663 THREAD_OFF(nhe
->timer
);
1665 nexthops_free(nhe
->nhg
.nexthop
);
1667 XFREE(MTYPE_NHG
, nhe
);
1671 * On cleanup there are nexthop groups that have not
1672 * been resolved at all( a nhe->id of 0 ). As such
1673 * zebra needs to clean up the memory associated with
1676 void zebra_nhg_hash_free_zero_id(struct hash_bucket
*b
, void *arg
)
1678 struct nhg_hash_entry
*nhe
= b
->data
;
1679 struct nhg_connected
*dep
;
1681 while ((dep
= nhg_connected_tree_pop(&nhe
->nhg_depends
))) {
1682 if (dep
->nhe
->id
== 0)
1683 zebra_nhg_hash_free(dep
->nhe
);
1685 nhg_connected_free(dep
);
1688 while ((dep
= nhg_connected_tree_pop(&nhe
->nhg_dependents
)))
1689 nhg_connected_free(dep
);
1691 if (nhe
->backup_info
&& nhe
->backup_info
->nhe
->id
== 0) {
1692 while ((dep
= nhg_connected_tree_pop(
1693 &nhe
->backup_info
->nhe
->nhg_depends
)))
1694 nhg_connected_free(dep
);
1696 zebra_nhg_hash_free(nhe
->backup_info
->nhe
);
1698 XFREE(MTYPE_NHG
, nhe
->backup_info
);
1702 static void zebra_nhg_timer(struct thread
*thread
)
1704 struct nhg_hash_entry
*nhe
= THREAD_ARG(thread
);
1706 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1707 zlog_debug("Nexthop Timer for nhe: %pNG", nhe
);
1709 if (nhe
->refcnt
== 1)
1710 zebra_nhg_decrement_ref(nhe
);
1713 void zebra_nhg_decrement_ref(struct nhg_hash_entry
*nhe
)
1715 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1716 zlog_debug("%s: nhe %p (%pNG) %d => %d", __func__
, nhe
, nhe
,
1717 nhe
->refcnt
, nhe
->refcnt
- 1);
1721 if (!zebra_router_in_shutdown() && nhe
->refcnt
<= 0 &&
1722 CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
) &&
1723 !CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_KEEP_AROUND
)) {
1725 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_KEEP_AROUND
);
1726 thread_add_timer(zrouter
.master
, zebra_nhg_timer
, nhe
,
1727 zrouter
.nhg_keep
, &nhe
->timer
);
1731 if (!zebra_nhg_depends_is_empty(nhe
))
1732 nhg_connected_tree_decrement_ref(&nhe
->nhg_depends
);
1734 if (ZEBRA_NHG_CREATED(nhe
) && nhe
->refcnt
<= 0)
1735 zebra_nhg_uninstall_kernel(nhe
);
1738 void zebra_nhg_increment_ref(struct nhg_hash_entry
*nhe
)
1740 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1741 zlog_debug("%s: nhe %p (%pNG) %d => %d", __func__
, nhe
, nhe
,
1742 nhe
->refcnt
, nhe
->refcnt
+ 1);
1746 if (thread_is_scheduled(nhe
->timer
)) {
1747 THREAD_OFF(nhe
->timer
);
1749 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_KEEP_AROUND
);
1752 if (!zebra_nhg_depends_is_empty(nhe
))
1753 nhg_connected_tree_increment_ref(&nhe
->nhg_depends
);
1756 static struct nexthop
*nexthop_set_resolved(afi_t afi
,
1757 const struct nexthop
*newhop
,
1758 struct nexthop
*nexthop
,
1759 struct zebra_sr_policy
*policy
)
1761 struct nexthop
*resolved_hop
;
1762 uint8_t num_labels
= 0;
1763 mpls_label_t labels
[MPLS_MAX_LABELS
];
1764 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1767 resolved_hop
= nexthop_new();
1768 SET_FLAG(resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1770 resolved_hop
->vrf_id
= nexthop
->vrf_id
;
1771 switch (newhop
->type
) {
1772 case NEXTHOP_TYPE_IPV4
:
1773 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1774 /* If the resolving route specifies a gateway, use it */
1775 resolved_hop
->type
= newhop
->type
;
1776 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
1778 if (newhop
->ifindex
) {
1779 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1780 resolved_hop
->ifindex
= newhop
->ifindex
;
1783 case NEXTHOP_TYPE_IPV6
:
1784 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1785 resolved_hop
->type
= newhop
->type
;
1786 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
1788 if (newhop
->ifindex
) {
1789 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1790 resolved_hop
->ifindex
= newhop
->ifindex
;
1793 case NEXTHOP_TYPE_IFINDEX
:
1794 /* If the resolving route is an interface route,
1795 * it means the gateway we are looking up is connected
1796 * to that interface. (The actual network is _not_ onlink).
1797 * Therefore, the resolved route should have the original
1798 * gateway as nexthop as it is directly connected.
1800 * On Linux, we have to set the onlink netlink flag because
1801 * otherwise, the kernel won't accept the route.
1803 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
1804 if (afi
== AFI_IP
) {
1805 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1806 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
1807 } else if (afi
== AFI_IP6
) {
1808 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1809 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
1811 resolved_hop
->ifindex
= newhop
->ifindex
;
1813 case NEXTHOP_TYPE_BLACKHOLE
:
1814 resolved_hop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
1815 resolved_hop
->bh_type
= newhop
->bh_type
;
1819 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
1820 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
1822 /* Copy labels of the resolved route and the parent resolving to it */
1827 * Don't push the first SID if the corresponding action in the
1830 if (!newhop
->nh_label
|| !newhop
->nh_label
->num_labels
1831 || newhop
->nh_label
->label
[0] == MPLS_LABEL_IMPLICIT_NULL
)
1834 for (; label_num
< policy
->segment_list
.label_num
; label_num
++)
1835 labels
[num_labels
++] =
1836 policy
->segment_list
.labels
[label_num
];
1837 label_type
= policy
->segment_list
.type
;
1838 } else if (newhop
->nh_label
) {
1839 for (i
= 0; i
< newhop
->nh_label
->num_labels
; i
++) {
1840 /* Be a bit picky about overrunning the local array */
1841 if (num_labels
>= MPLS_MAX_LABELS
) {
1842 if (IS_ZEBRA_DEBUG_NHG
|| IS_ZEBRA_DEBUG_RIB
)
1843 zlog_debug("%s: too many labels in newhop %pNHv",
1847 labels
[num_labels
++] = newhop
->nh_label
->label
[i
];
1849 /* Use the "outer" type */
1850 label_type
= newhop
->nh_label_type
;
1853 if (nexthop
->nh_label
) {
1854 for (i
= 0; i
< nexthop
->nh_label
->num_labels
; i
++) {
1855 /* Be a bit picky about overrunning the local array */
1856 if (num_labels
>= MPLS_MAX_LABELS
) {
1857 if (IS_ZEBRA_DEBUG_NHG
|| IS_ZEBRA_DEBUG_RIB
)
1858 zlog_debug("%s: too many labels in nexthop %pNHv",
1862 labels
[num_labels
++] = nexthop
->nh_label
->label
[i
];
1865 /* If the parent has labels, use its type if
1866 * we don't already have one.
1868 if (label_type
== ZEBRA_LSP_NONE
)
1869 label_type
= nexthop
->nh_label_type
;
1873 nexthop_add_labels(resolved_hop
, label_type
, num_labels
,
1876 if (nexthop
->nh_srv6
) {
1877 nexthop_add_srv6_seg6local(resolved_hop
,
1878 nexthop
->nh_srv6
->seg6local_action
,
1879 &nexthop
->nh_srv6
->seg6local_ctx
);
1880 nexthop_add_srv6_seg6(resolved_hop
,
1881 &nexthop
->nh_srv6
->seg6_segs
);
1884 resolved_hop
->rparent
= nexthop
;
1885 _nexthop_add(&nexthop
->resolved
, resolved_hop
);
1887 return resolved_hop
;
1890 /* Checks if nexthop we are trying to resolve to is valid */
1891 static bool nexthop_valid_resolve(const struct nexthop
*nexthop
,
1892 const struct nexthop
*resolved
)
1894 /* Can't resolve to a recursive nexthop */
1895 if (CHECK_FLAG(resolved
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1898 /* Must be ACTIVE */
1899 if (!CHECK_FLAG(resolved
->flags
, NEXTHOP_FLAG_ACTIVE
))
1902 /* Must not be duplicate */
1903 if (CHECK_FLAG(resolved
->flags
, NEXTHOP_FLAG_DUPLICATE
))
1906 switch (nexthop
->type
) {
1907 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1908 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1909 /* If the nexthop we are resolving to does not match the
1910 * ifindex for the nexthop the route wanted, its not valid.
1912 if (nexthop
->ifindex
!= resolved
->ifindex
)
1915 case NEXTHOP_TYPE_IPV4
:
1916 case NEXTHOP_TYPE_IPV6
:
1917 case NEXTHOP_TYPE_IFINDEX
:
1918 case NEXTHOP_TYPE_BLACKHOLE
:
1926 * Downstream VNI and Single VXlan device check.
1928 * If it has nexthop VNI labels at this point it must be D-VNI allocated
1929 * and all the nexthops have to be on an SVD.
1931 * If SVD is not available, mark as inactive.
1933 static bool nexthop_set_evpn_dvni_svd(vrf_id_t re_vrf_id
,
1934 struct nexthop
*nexthop
)
1936 if (!is_vrf_l3vni_svd_backed(re_vrf_id
)) {
1937 if (IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1938 struct vrf
*vrf
= vrf_lookup_by_id(re_vrf_id
);
1941 "nexthop %pNHv D-VNI but route's vrf %s(%u) doesn't use SVD",
1942 nexthop
, VRF_LOGNAME(vrf
), re_vrf_id
);
1948 nexthop
->ifindex
= get_l3vni_vxlan_ifindex(re_vrf_id
);
1949 nexthop
->vrf_id
= 0;
1951 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1952 zlog_debug("nexthop %pNHv using SVD", nexthop
);
1958 * Given a nexthop we need to properly recursively resolve
1959 * the route. As such, do a table lookup to find and match
1960 * if at all possible. Set the nexthop->ifindex and resolved_id
1963 static int resolve_backup_nexthops(const struct nexthop
*nexthop
,
1964 const struct nhg_hash_entry
*nhe
,
1965 struct nexthop
*resolved
,
1966 struct nhg_hash_entry
*resolve_nhe
,
1967 struct backup_nh_map_s
*map
)
1970 const struct nexthop
*bnh
;
1971 struct nexthop
*nh
, *newnh
;
1972 mpls_label_t labels
[MPLS_MAX_LABELS
];
1975 assert(nexthop
->backup_num
<= NEXTHOP_MAX_BACKUPS
);
1977 /* Locate backups from the original nexthop's backup index and nhe */
1978 for (i
= 0; i
< nexthop
->backup_num
; i
++) {
1979 idx
= nexthop
->backup_idx
[i
];
1981 /* Do we already know about this particular backup? */
1982 for (j
= 0; j
< map
->map_count
; j
++) {
1983 if (map
->map
[j
].orig_idx
== idx
)
1987 if (j
< map
->map_count
) {
1988 resolved
->backup_idx
[resolved
->backup_num
] =
1989 map
->map
[j
].new_idx
;
1990 resolved
->backup_num
++;
1992 SET_FLAG(resolved
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1994 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1995 zlog_debug("%s: found map idx orig %d, new %d",
1996 __func__
, map
->map
[j
].orig_idx
,
1997 map
->map
[j
].new_idx
);
2002 /* We can't handle any new map entries at this point. */
2003 if (map
->map_count
== MULTIPATH_NUM
)
2006 /* Need to create/copy a new backup */
2007 bnh
= nhe
->backup_info
->nhe
->nhg
.nexthop
;
2008 for (j
= 0; j
< idx
; j
++) {
2014 /* Whoops - bad index in the nexthop? */
2018 if (resolve_nhe
->backup_info
== NULL
)
2019 resolve_nhe
->backup_info
= zebra_nhg_backup_alloc();
2021 /* Update backup info in the resolving nexthop and its nhe */
2022 newnh
= nexthop_dup_no_recurse(bnh
, NULL
);
2024 /* We may need some special handling for mpls labels: the new
2025 * backup needs to carry the recursive nexthop's labels,
2026 * if any: they may be vrf labels e.g.
2027 * The original/inner labels are in the stack of 'resolve_nhe',
2028 * if that is longer than the stack in 'nexthop'.
2030 if (newnh
->nh_label
&& resolved
->nh_label
&&
2031 nexthop
->nh_label
) {
2032 if (resolved
->nh_label
->num_labels
>
2033 nexthop
->nh_label
->num_labels
) {
2034 /* Prepare new label stack */
2036 for (j
= 0; j
< newnh
->nh_label
->num_labels
;
2038 labels
[j
] = newnh
->nh_label
->label
[j
];
2042 /* Include inner labels */
2043 for (j
= nexthop
->nh_label
->num_labels
;
2044 j
< resolved
->nh_label
->num_labels
;
2046 labels
[num_labels
] =
2047 resolved
->nh_label
->label
[j
];
2051 /* Replace existing label stack in the backup */
2052 nexthop_del_labels(newnh
);
2053 nexthop_add_labels(newnh
, bnh
->nh_label_type
,
2054 num_labels
, labels
);
2058 /* Need to compute the new backup index in the new
2059 * backup list, and add to map struct.
2062 nh
= resolve_nhe
->backup_info
->nhe
->nhg
.nexthop
;
2072 } else /* First one */
2073 resolve_nhe
->backup_info
->nhe
->nhg
.nexthop
= newnh
;
2076 resolved
->backup_idx
[resolved
->backup_num
] = j
;
2077 resolved
->backup_num
++;
2079 SET_FLAG(resolved
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
2081 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2082 zlog_debug("%s: added idx orig %d, new %d",
2085 /* Update map/cache */
2086 map
->map
[map
->map_count
].orig_idx
= idx
;
2087 map
->map
[map
->map_count
].new_idx
= j
;
2095 * So this nexthop resolution has decided that a connected route
2096 * is the correct choice. At this point in time if FRR has multiple
2097 * connected routes that all point to the same prefix one will be
2098 * selected, *but* the particular interface may not be the one
2099 * that the nexthop points at. Let's look at all the available
2100 * connected routes on this node and if any of them auto match
2101 * the routes nexthops ifindex that is good enough for a match
2103 * This code is depending on the fact that a nexthop->ifindex is 0
2104 * if it is not known, if this assumption changes, yummy!
2105 * Additionally a ifindx of 0 means figure it out for us.
2107 static struct route_entry
*
2108 zebra_nhg_connected_ifindex(struct route_node
*rn
, struct route_entry
*match
,
2109 int32_t curr_ifindex
)
2111 struct nexthop
*newhop
= match
->nhe
->nhg
.nexthop
;
2112 struct route_entry
*re
;
2114 assert(newhop
); /* What a kick in the patooey */
2116 if (curr_ifindex
== 0)
2119 if (curr_ifindex
== newhop
->ifindex
)
2123 * At this point we know that this route is matching a connected
2124 * but there are possibly a bunch of connected routes that are
2125 * alive that should be considered as well. So let's iterate over
2126 * all the re's and see if they are connected as well and maybe one
2127 * of those ifindexes match as well.
2129 RNODE_FOREACH_RE (rn
, re
) {
2130 if (re
->type
!= ZEBRA_ROUTE_CONNECT
)
2133 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2137 * zebra has a connected route that is not removed
2138 * let's test if it is good
2140 newhop
= re
->nhe
->nhg
.nexthop
;
2142 if (curr_ifindex
== newhop
->ifindex
)
2150 * Given a nexthop we need to properly recursively resolve,
2151 * do a table lookup to find and match if at all possible.
2152 * Set the nexthop->ifindex and resolution info as appropriate.
2154 static int nexthop_active(struct nexthop
*nexthop
, struct nhg_hash_entry
*nhe
,
2155 const struct prefix
*top
, int type
, uint32_t flags
,
2156 uint32_t *pmtu
, vrf_id_t vrf_id
)
2159 struct route_table
*table
;
2160 struct route_node
*rn
;
2161 struct route_entry
*match
= NULL
;
2163 struct zebra_nhlfe
*nhlfe
;
2164 struct nexthop
*newhop
;
2165 struct interface
*ifp
;
2167 struct zebra_vrf
*zvrf
;
2168 struct in_addr local_ipv4
;
2169 struct in_addr
*ipv4
;
2172 /* Reset some nexthop attributes that we'll recompute if necessary */
2173 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
)
2174 || (nexthop
->type
== NEXTHOP_TYPE_IPV6
))
2175 nexthop
->ifindex
= 0;
2177 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
2178 nexthops_free(nexthop
->resolved
);
2179 nexthop
->resolved
= NULL
;
2182 * Set afi based on nexthop type.
2183 * Some nexthop types get special handling, possibly skipping
2184 * the normal processing.
2186 switch (nexthop
->type
) {
2187 case NEXTHOP_TYPE_IFINDEX
:
2189 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
2191 * If the interface exists and its operative or its a kernel
2192 * route and interface is up, its active. We trust kernel routes
2195 if (ifp
&& (if_is_operative(ifp
)))
2201 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2204 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
2205 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2207 if (ifp
&& if_is_operative(ifp
))
2214 case NEXTHOP_TYPE_IPV4
:
2215 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2218 case NEXTHOP_TYPE_IPV6
:
2222 case NEXTHOP_TYPE_BLACKHOLE
:
2227 * If the nexthop has been marked as 'onlink' we just need to make
2228 * sure the nexthop's interface is known and is operational.
2230 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)) {
2231 /* DVNI/SVD Checks for EVPN routes */
2232 if (nexthop
->nh_label
&&
2233 nexthop
->nh_label_type
== ZEBRA_LSP_EVPN
&&
2234 !nexthop_set_evpn_dvni_svd(vrf_id
, nexthop
))
2237 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
2239 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2240 zlog_debug("nexthop %pNHv marked onlink but nhif %u doesn't exist",
2241 nexthop
, nexthop
->ifindex
);
2244 if (!if_is_operative(ifp
)) {
2245 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2246 zlog_debug("nexthop %pNHv marked onlink but nhif %s is not operational",
2247 nexthop
, ifp
->name
);
2254 ((top
->family
== AF_INET
&& top
->prefixlen
== IPV4_MAX_BITLEN
&&
2255 nexthop
->gate
.ipv4
.s_addr
== top
->u
.prefix4
.s_addr
) ||
2256 (top
->family
== AF_INET6
&& top
->prefixlen
== IPV6_MAX_BITLEN
&&
2257 memcmp(&nexthop
->gate
.ipv6
, &top
->u
.prefix6
, IPV6_MAX_BYTELEN
) ==
2259 nexthop
->vrf_id
== vrf_id
) {
2260 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2262 " :%s: Attempting to install a max prefixlength route through itself",
2267 /* Validation for ipv4 mapped ipv6 nexthop. */
2268 if (IS_MAPPED_IPV6(&nexthop
->gate
.ipv6
)) {
2271 ipv4_mapped_ipv6_to_ipv4(&nexthop
->gate
.ipv6
, ipv4
);
2273 ipv4
= &nexthop
->gate
.ipv4
;
2276 /* Processing for nexthops with SR 'color' attribute, using
2277 * the corresponding SR policy object.
2279 if (nexthop
->srte_color
) {
2280 struct ipaddr endpoint
= {0};
2281 struct zebra_sr_policy
*policy
;
2285 endpoint
.ipa_type
= IPADDR_V4
;
2286 endpoint
.ipaddr_v4
= *ipv4
;
2289 endpoint
.ipa_type
= IPADDR_V6
;
2290 endpoint
.ipaddr_v6
= nexthop
->gate
.ipv6
;
2295 flog_err(EC_LIB_DEVELOPMENT
,
2296 "%s: unknown address-family: %u", __func__
,
2301 policy
= zebra_sr_policy_find(nexthop
->srte_color
, &endpoint
);
2302 if (policy
&& policy
->status
== ZEBRA_SR_POLICY_UP
) {
2304 frr_each_safe (nhlfe_list
, &policy
->lsp
->nhlfe_list
,
2306 if (!CHECK_FLAG(nhlfe
->flags
,
2307 NHLFE_FLAG_SELECTED
)
2308 || CHECK_FLAG(nhlfe
->flags
,
2309 NHLFE_FLAG_DELETED
))
2311 SET_FLAG(nexthop
->flags
,
2312 NEXTHOP_FLAG_RECURSIVE
);
2313 nexthop_set_resolved(afi
, nhlfe
->nexthop
,
2322 /* Make lookup prefix. */
2323 memset(&p
, 0, sizeof(struct prefix
));
2327 p
.prefixlen
= IPV4_MAX_BITLEN
;
2328 p
.u
.prefix4
= *ipv4
;
2331 p
.family
= AF_INET6
;
2332 p
.prefixlen
= IPV6_MAX_BITLEN
;
2333 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
2338 assert(afi
!= AFI_IP
&& afi
!= AFI_IP6
);
2342 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, nexthop
->vrf_id
);
2344 zvrf
= zebra_vrf_lookup_by_id(nexthop
->vrf_id
);
2345 if (!table
|| !zvrf
) {
2346 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2347 zlog_debug(" %s: Table not found", __func__
);
2351 rn
= route_node_match(table
, (struct prefix
*)&p
);
2353 route_unlock_node(rn
);
2355 /* Lookup should halt if we've matched against ourselves ('top',
2356 * if specified) - i.e., we cannot have a nexthop NH1 is
2357 * resolved by a route NH1. The exception is if the route is a
2360 if (prefix_same(&rn
->p
, top
))
2361 if (((afi
== AFI_IP
)
2362 && (rn
->p
.prefixlen
!= IPV4_MAX_BITLEN
))
2363 || ((afi
== AFI_IP6
)
2364 && (rn
->p
.prefixlen
!= IPV6_MAX_BITLEN
))) {
2365 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2367 " %s: Matched against ourself and prefix length is not max bit length",
2372 /* Pick up selected route. */
2373 /* However, do not resolve over default route unless explicitly
2376 if (is_default_prefix(&rn
->p
)
2377 && !rnh_resolve_via_default(zvrf
, p
.family
)) {
2378 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2380 " :%s: Resolved against default route",
2385 dest
= rib_dest_from_rnode(rn
);
2386 if (dest
&& dest
->selected_fib
2387 && !CHECK_FLAG(dest
->selected_fib
->status
,
2388 ROUTE_ENTRY_REMOVED
)
2389 && dest
->selected_fib
->type
!= ZEBRA_ROUTE_TABLE
)
2390 match
= dest
->selected_fib
;
2392 /* If there is no selected route or matched route is EGP, go up
2398 } while (rn
&& rn
->info
== NULL
);
2400 route_lock_node(rn
);
2405 if ((match
->type
== ZEBRA_ROUTE_CONNECT
) ||
2406 (RIB_SYSTEM_ROUTE(match
) && RSYSTEM_ROUTE(type
))) {
2407 match
= zebra_nhg_connected_ifindex(rn
, match
,
2410 newhop
= match
->nhe
->nhg
.nexthop
;
2411 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
||
2412 nexthop
->type
== NEXTHOP_TYPE_IPV6
)
2413 nexthop
->ifindex
= newhop
->ifindex
;
2414 else if (nexthop
->ifindex
!= newhop
->ifindex
) {
2415 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2417 "%s: %pNHv given ifindex does not match nexthops ifindex found: %pNHv",
2418 __func__
, nexthop
, newhop
);
2420 * NEXTHOP_TYPE_*_IFINDEX but ifindex
2421 * doesn't match what we found.
2426 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2428 "%s: CONNECT match %p (%pNG), newhop %pNHv",
2429 __func__
, match
, match
->nhe
, newhop
);
2432 } else if (CHECK_FLAG(flags
, ZEBRA_FLAG_ALLOW_RECURSION
)) {
2433 struct nexthop_group
*nhg
;
2434 struct nexthop
*resolver
;
2435 struct backup_nh_map_s map
= {};
2440 * Only useful if installed or being Route Replacing
2441 * Why Being Route Replaced as well?
2442 * Imagine a route A and route B( that depends on A )
2443 * for recursive resolution and A already exists in the
2444 * zebra rib. If zebra receives the routes
2445 * for resolution at aproximately the same time in the [
2446 * B, A ] order on the workQ. If this happens then
2447 * normal route resolution will happen and B will be
2448 * resolved successfully and then A will be resolved
2449 * successfully. Now imagine the reversed order [A, B].
2450 * A will be resolved and then scheduled for installed
2451 * (Thus not having the ROUTE_ENTRY_INSTALLED flag ). B
2452 * will then get resolved and fail to be installed
2453 * because the original below test. Let's `loosen` this
2454 * up a tiny bit and allow the
2455 * ROUTE_ENTRY_ROUTE_REPLACING flag ( that is set when a
2456 * Route Replace operation is being initiated on A now )
2457 * to now satisfy this situation. This will allow
2458 * either order in the workQ to work properly.
2460 if (!CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
) &&
2461 !CHECK_FLAG(match
->status
,
2462 ROUTE_ENTRY_ROUTE_REPLACING
)) {
2463 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2465 "%s: match %p (%pNG) not installed or being Route Replaced",
2466 __func__
, match
, match
->nhe
);
2468 goto done_with_match
;
2471 /* Examine installed nexthops; note that there
2472 * may not be any installed primary nexthops if
2473 * only backups are installed.
2475 nhg
= rib_get_fib_nhg(match
);
2476 for (ALL_NEXTHOPS_PTR(nhg
, newhop
)) {
2477 if (!nexthop_valid_resolve(nexthop
, newhop
))
2480 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2482 "%s: RECURSIVE match %p (%pNG), newhop %pNHv",
2483 __func__
, match
, match
->nhe
,
2486 SET_FLAG(nexthop
->flags
,
2487 NEXTHOP_FLAG_RECURSIVE
);
2488 resolver
= nexthop_set_resolved(afi
, newhop
,
2492 /* If there are backup nexthops, capture
2493 * that info with the resolving nexthop.
2495 if (resolver
&& newhop
->backup_num
> 0) {
2496 resolve_backup_nexthops(newhop
,
2503 /* Examine installed backup nexthops, if any. There
2504 * are only installed backups *if* there is a
2505 * dedicated fib list. The UI can also control use
2506 * of backups for resolution.
2508 nhg
= rib_get_fib_backup_nhg(match
);
2509 if (!use_recursive_backups
||
2510 nhg
== NULL
|| nhg
->nexthop
== NULL
)
2511 goto done_with_match
;
2513 for (ALL_NEXTHOPS_PTR(nhg
, newhop
)) {
2514 if (!nexthop_valid_resolve(nexthop
, newhop
))
2517 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2519 "%s: RECURSIVE match backup %p (%pNG), newhop %pNHv",
2520 __func__
, match
, match
->nhe
,
2523 SET_FLAG(nexthop
->flags
,
2524 NEXTHOP_FLAG_RECURSIVE
);
2525 nexthop_set_resolved(afi
, newhop
, nexthop
,
2531 /* Capture resolving mtu */
2536 } else if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2538 " %s: Recursion failed to find",
2543 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2545 " %s: Route Type %s has not turned on recursion",
2546 __func__
, zebra_route_string(type
));
2547 if (type
== ZEBRA_ROUTE_BGP
2548 && !CHECK_FLAG(flags
, ZEBRA_FLAG_IBGP
))
2550 " EBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\"");
2555 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2556 zlog_debug(" %s: Nexthop did not lookup in table",
2561 /* This function verifies reachability of one given nexthop, which can be
2562 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
2563 * in nexthop->flags field. The nexthop->ifindex will be updated
2564 * appropriately as well.
2566 * An existing route map can turn an otherwise active nexthop into inactive,
2567 * but not vice versa.
2569 * The return value is the final value of 'ACTIVE' flag.
2571 static unsigned nexthop_active_check(struct route_node
*rn
,
2572 struct route_entry
*re
,
2573 struct nexthop
*nexthop
,
2574 struct nhg_hash_entry
*nhe
)
2576 route_map_result_t ret
= RMAP_PERMITMATCH
;
2578 const struct prefix
*p
, *src_p
;
2579 struct zebra_vrf
*zvrf
;
2583 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
2585 if (rn
->p
.family
== AF_INET
)
2587 else if (rn
->p
.family
== AF_INET6
)
2592 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2593 zlog_debug("%s: re %p, nexthop %pNHv", __func__
, re
, nexthop
);
2596 * If this is a kernel route, then if the interface is *up* then
2597 * by golly gee whiz it's a good route.
2599 if (re
->type
== ZEBRA_ROUTE_KERNEL
|| re
->type
== ZEBRA_ROUTE_SYSTEM
) {
2600 struct interface
*ifp
;
2602 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
2604 if (ifp
&& (if_is_operative(ifp
) || if_is_up(ifp
))) {
2605 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2610 vrf_id
= zvrf_id(rib_dest_vrf(rib_dest_from_rnode(rn
)));
2611 switch (nexthop
->type
) {
2612 case NEXTHOP_TYPE_IFINDEX
:
2613 if (nexthop_active(nexthop
, nhe
, &rn
->p
, re
->type
, re
->flags
,
2615 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2617 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2619 case NEXTHOP_TYPE_IPV4
:
2620 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2622 if (nexthop_active(nexthop
, nhe
, &rn
->p
, re
->type
, re
->flags
,
2624 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2626 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2628 case NEXTHOP_TYPE_IPV6
:
2630 if (nexthop_active(nexthop
, nhe
, &rn
->p
, re
->type
, re
->flags
,
2632 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2634 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2636 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2637 /* RFC 5549, v4 prefix with v6 NH */
2638 if (rn
->p
.family
!= AF_INET
)
2641 if (nexthop_active(nexthop
, nhe
, &rn
->p
, re
->type
, re
->flags
,
2643 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2645 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2647 case NEXTHOP_TYPE_BLACKHOLE
:
2648 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2656 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
2657 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2658 zlog_debug(" %s: Unable to find active nexthop",
2663 /* Capture recursive nexthop mtu.
2664 * TODO -- the code used to just reset the re's value to zero
2665 * for each nexthop, and then jam any resolving route's mtu value in,
2666 * whether or not that was zero, or lt/gt any existing value? The
2667 * way this is used appears to be as a floor value, so let's try
2668 * using it that way here.
2671 if (re
->nexthop_mtu
== 0 || re
->nexthop_mtu
> mtu
)
2672 re
->nexthop_mtu
= mtu
;
2675 /* XXX: What exactly do those checks do? Do we support
2676 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
2678 if (RIB_SYSTEM_ROUTE(re
) || (family
== AFI_IP
&& p
->family
!= AF_INET
)
2679 || (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
2680 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2682 /* The original code didn't determine the family correctly
2683 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
2684 * from the rib_table_info in those cases.
2685 * Possibly it may be better to use only the rib_table_info
2689 struct rib_table_info
*info
;
2691 info
= srcdest_rnode_table_info(rn
);
2695 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
2697 zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2699 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2700 zlog_debug(" %s: zvrf is NULL", __func__
);
2701 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2704 /* It'll get set if required inside */
2705 ret
= zebra_route_map_check(family
, re
->type
, re
->instance
, p
, nexthop
,
2707 if (ret
== RMAP_DENYMATCH
) {
2708 if (IS_ZEBRA_DEBUG_RIB
) {
2710 "%u:%pRN: Filtering out with NH %pNHv due to route map",
2711 re
->vrf_id
, rn
, nexthop
);
2713 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2715 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2718 /* Helper function called after resolution to walk nhg rb trees
2719 * and toggle the NEXTHOP_GROUP_VALID flag if the nexthop
2720 * is active on singleton NHEs.
2722 static bool zebra_nhg_set_valid_if_active(struct nhg_hash_entry
*nhe
)
2724 struct nhg_connected
*rb_node_dep
= NULL
;
2727 if (!zebra_nhg_depends_is_empty(nhe
)) {
2728 /* Is at least one depend valid? */
2729 frr_each(nhg_connected_tree
, &nhe
->nhg_depends
, rb_node_dep
) {
2730 if (zebra_nhg_set_valid_if_active(rb_node_dep
->nhe
))
2737 /* should be fully resolved singleton at this point */
2738 if (CHECK_FLAG(nhe
->nhg
.nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2743 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
2748 /* Checks if the first nexthop is EVPN. If not, early return.
2750 * This is used to determine if there is a mismatch between l3VNI
2751 * of the route's vrf and the nexthops in use's VNI labels.
2753 * If there is a mismatch, we keep the labels as these MUST be DVNI nexthops.
2755 * IF there is no mismatch, we remove the labels and handle the routes as
2756 * we have traditionally with evpn.
2758 static bool nexthop_list_set_evpn_dvni(struct route_entry
*re
,
2759 struct nexthop_group
*nhg
)
2761 struct nexthop
*nexthop
;
2764 bool use_dvni
= false;
2766 nexthop
= nhg
->nexthop
;
2768 if (!nexthop
->nh_label
|| nexthop
->nh_label_type
!= ZEBRA_LSP_EVPN
)
2771 re_vrf_vni
= get_l3vni_vni(re
->vrf_id
);
2773 for (; nexthop
; nexthop
= nexthop
->next
) {
2774 if (!nexthop
->nh_label
||
2775 nexthop
->nh_label_type
!= ZEBRA_LSP_EVPN
)
2778 nh_vni
= label2vni(&nexthop
->nh_label
->label
[0]);
2780 if (nh_vni
!= re_vrf_vni
)
2784 /* Using traditional way, no VNI encap - remove labels */
2786 for (nexthop
= nhg
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2787 nexthop_del_labels(nexthop
);
2794 * Process a list of nexthops, given an nhe, determining
2795 * whether each one is ACTIVE/installable at this time.
2797 static uint32_t nexthop_list_active_update(struct route_node
*rn
,
2798 struct route_entry
*re
,
2799 struct nhg_hash_entry
*nhe
,
2802 union g_addr prev_src
;
2803 unsigned int prev_active
, new_active
;
2804 ifindex_t prev_index
;
2805 uint32_t counter
= 0;
2806 struct nexthop
*nexthop
;
2807 struct nexthop_group
*nhg
= &nhe
->nhg
;
2808 bool vni_removed
= false;
2810 nexthop
= nhg
->nexthop
;
2812 /* Init recursive nh mtu */
2813 re
->nexthop_mtu
= 0;
2815 /* Handler for dvni evpn nexthops. Has to be done at nhg level */
2816 vni_removed
= !nexthop_list_set_evpn_dvni(re
, nhg
);
2818 /* Process nexthops one-by-one */
2819 for ( ; nexthop
; nexthop
= nexthop
->next
) {
2821 /* No protocol daemon provides src and so we're skipping
2824 prev_src
= nexthop
->rmap_src
;
2825 prev_active
= CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2826 prev_index
= nexthop
->ifindex
;
2828 /* Include the containing nhe for primary nexthops: if there's
2829 * recursive resolution, we capture the backup info also.
2832 nexthop_active_check(rn
, re
, nexthop
,
2833 (is_backup
? NULL
: nhe
));
2836 * We need to respect the multipath_num here
2837 * as that what we should be able to install from
2838 * a multipath perspective should not be a data plane
2841 if (new_active
&& counter
>= zrouter
.multipath_num
) {
2844 /* Set it and its resolved nexthop as inactive. */
2845 for (nh
= nexthop
; nh
; nh
= nh
->resolved
)
2846 UNSET_FLAG(nh
->flags
, NEXTHOP_FLAG_ACTIVE
);
2854 /* Check for changes to the nexthop - set ROUTE_ENTRY_CHANGED */
2855 if (prev_active
!= new_active
|| prev_index
!= nexthop
->ifindex
2856 || ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
2857 && nexthop
->type
< NEXTHOP_TYPE_IPV6
)
2858 && prev_src
.ipv4
.s_addr
2859 != nexthop
->rmap_src
.ipv4
.s_addr
)
2860 || ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
2861 && nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
)
2862 && !(IPV6_ADDR_SAME(&prev_src
.ipv6
,
2863 &nexthop
->rmap_src
.ipv6
)))
2864 || CHECK_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
)
2866 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2873 static uint32_t proto_nhg_nexthop_active_update(struct nexthop_group
*nhg
)
2876 uint32_t curr_active
= 0;
2878 /* Assume all active for now */
2880 for (nh
= nhg
->nexthop
; nh
; nh
= nh
->next
) {
2881 SET_FLAG(nh
->flags
, NEXTHOP_FLAG_ACTIVE
);
2889 * Iterate over all nexthops of the given RIB entry and refresh their
2890 * ACTIVE flag. If any nexthop is found to toggle the ACTIVE flag,
2891 * the whole re structure is flagged with ROUTE_ENTRY_CHANGED.
2893 * Return value is the new number of active nexthops.
2895 int nexthop_active_update(struct route_node
*rn
, struct route_entry
*re
)
2897 struct nhg_hash_entry
*curr_nhe
;
2898 uint32_t curr_active
= 0, backup_active
= 0;
2900 if (PROTO_OWNED(re
->nhe
))
2901 return proto_nhg_nexthop_active_update(&re
->nhe
->nhg
);
2903 afi_t rt_afi
= family2afi(rn
->p
.family
);
2905 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2907 /* Make a local copy of the existing nhe, so we don't work on/modify
2910 curr_nhe
= zebra_nhe_copy(re
->nhe
, re
->nhe
->id
);
2912 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2913 zlog_debug("%s: re %p nhe %p (%pNG), curr_nhe %p", __func__
, re
,
2914 re
->nhe
, re
->nhe
, curr_nhe
);
2916 /* Clear the existing id, if any: this will avoid any confusion
2917 * if the id exists, and will also force the creation
2918 * of a new nhe reflecting the changes we may make in this local copy.
2922 /* Process nexthops */
2923 curr_active
= nexthop_list_active_update(rn
, re
, curr_nhe
, false);
2925 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2926 zlog_debug("%s: re %p curr_active %u", __func__
, re
,
2929 /* If there are no backup nexthops, we are done */
2930 if (zebra_nhg_get_backup_nhg(curr_nhe
) == NULL
)
2933 backup_active
= nexthop_list_active_update(
2934 rn
, re
, curr_nhe
->backup_info
->nhe
, true /*is_backup*/);
2936 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2937 zlog_debug("%s: re %p backup_active %u", __func__
, re
,
2943 * Ref or create an nhe that matches the current state of the
2946 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
2947 struct nhg_hash_entry
*new_nhe
= NULL
;
2949 new_nhe
= zebra_nhg_rib_find_nhe(curr_nhe
, rt_afi
);
2951 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2953 "%s: re %p CHANGED: nhe %p (%pNG) => new_nhe %p (%pNG)",
2954 __func__
, re
, re
->nhe
, re
->nhe
, new_nhe
,
2957 route_entry_update_nhe(re
, new_nhe
);
2961 /* Walk the NHE depends tree and toggle NEXTHOP_GROUP_VALID
2962 * flag where appropriate.
2965 zebra_nhg_set_valid_if_active(re
->nhe
);
2968 * Do not need the old / copied nhe anymore since it
2969 * was either copied over into a new nhe or not
2972 zebra_nhg_free(curr_nhe
);
2976 /* Recursively construct a grp array of fully resolved IDs.
2978 * This function allows us to account for groups within groups,
2979 * by converting them into a flat array of IDs.
2981 * nh_grp is modified at every level of recursion to append
2982 * to it the next unique, fully resolved ID from the entire tree.
2986 * I'm pretty sure we only allow ONE level of group within group currently.
2987 * But making this recursive just in case that ever changes.
2989 static uint8_t zebra_nhg_nhe2grp_internal(struct nh_grp
*grp
,
2991 struct nhg_hash_entry
*nhe
,
2994 struct nhg_connected
*rb_node_dep
= NULL
;
2995 struct nhg_hash_entry
*depend
= NULL
;
2996 uint8_t i
= curr_index
;
2998 frr_each(nhg_connected_tree
, &nhe
->nhg_depends
, rb_node_dep
) {
2999 bool duplicate
= false;
3004 depend
= rb_node_dep
->nhe
;
3007 * If its recursive, use its resolved nhe in the group
3009 if (CHECK_FLAG(depend
->flags
, NEXTHOP_GROUP_RECURSIVE
)) {
3010 depend
= zebra_nhg_resolve(depend
);
3013 EC_ZEBRA_NHG_FIB_UPDATE
,
3014 "Failed to recursively resolve Nexthop Hash Entry in the group id=%pNG",
3020 if (!zebra_nhg_depends_is_empty(depend
)) {
3021 /* This is a group within a group */
3022 i
= zebra_nhg_nhe2grp_internal(grp
, i
, depend
, max_num
);
3024 if (!CHECK_FLAG(depend
->flags
, NEXTHOP_GROUP_VALID
)) {
3025 if (IS_ZEBRA_DEBUG_RIB_DETAILED
3026 || IS_ZEBRA_DEBUG_NHG
)
3028 "%s: Nexthop ID (%u) not valid, not appending to dataplane install group",
3029 __func__
, depend
->id
);
3033 /* If the nexthop not installed/queued for install don't
3034 * put in the ID array.
3036 if (!(CHECK_FLAG(depend
->flags
, NEXTHOP_GROUP_INSTALLED
)
3037 || CHECK_FLAG(depend
->flags
,
3038 NEXTHOP_GROUP_QUEUED
))) {
3039 if (IS_ZEBRA_DEBUG_RIB_DETAILED
3040 || IS_ZEBRA_DEBUG_NHG
)
3042 "%s: Nexthop ID (%u) not installed or queued for install, not appending to dataplane install group",
3043 __func__
, depend
->id
);
3047 /* Check for duplicate IDs, ignore if found. */
3048 for (int j
= 0; j
< i
; j
++) {
3049 if (depend
->id
== grp
[j
].id
) {
3056 if (IS_ZEBRA_DEBUG_RIB_DETAILED
3057 || IS_ZEBRA_DEBUG_NHG
)
3059 "%s: Nexthop ID (%u) is duplicate, not appending to dataplane install group",
3060 __func__
, depend
->id
);
3064 grp
[i
].id
= depend
->id
;
3065 grp
[i
].weight
= depend
->nhg
.nexthop
->weight
;
3070 if (nhe
->backup_info
== NULL
|| nhe
->backup_info
->nhe
== NULL
)
3073 /* TODO -- For now, we are not trying to use or install any
3074 * backup info in this nexthop-id path: we aren't prepared
3075 * to use the backups here yet. We're just debugging what we find.
3077 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
3078 zlog_debug("%s: skipping backup nhe", __func__
);
3084 /* Convert a nhe into a group array */
3085 uint8_t zebra_nhg_nhe2grp(struct nh_grp
*grp
, struct nhg_hash_entry
*nhe
,
3088 /* Call into the recursive function */
3089 return zebra_nhg_nhe2grp_internal(grp
, 0, nhe
, max_num
);
3092 void zebra_nhg_install_kernel(struct nhg_hash_entry
*nhe
)
3094 struct nhg_connected
*rb_node_dep
= NULL
;
3096 /* Resolve it first */
3097 nhe
= zebra_nhg_resolve(nhe
);
3099 /* Make sure all depends are installed/queued */
3100 frr_each(nhg_connected_tree
, &nhe
->nhg_depends
, rb_node_dep
) {
3101 zebra_nhg_install_kernel(rb_node_dep
->nhe
);
3104 if (CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
)
3105 && !CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
)
3106 && !CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_QUEUED
)) {
3107 /* Change its type to us since we are installing it */
3108 if (!ZEBRA_NHG_CREATED(nhe
))
3109 nhe
->type
= ZEBRA_ROUTE_NHG
;
3111 int ret
= dplane_nexthop_add(nhe
);
3114 case ZEBRA_DPLANE_REQUEST_QUEUED
:
3115 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_QUEUED
);
3117 case ZEBRA_DPLANE_REQUEST_FAILURE
:
3119 EC_ZEBRA_DP_INSTALL_FAIL
,
3120 "Failed to install Nexthop ID (%pNG) into the kernel",
3123 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
3124 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
3125 zebra_nhg_handle_install(nhe
);
3131 void zebra_nhg_uninstall_kernel(struct nhg_hash_entry
*nhe
)
3133 if (CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
)) {
3134 int ret
= dplane_nexthop_delete(nhe
);
3137 case ZEBRA_DPLANE_REQUEST_QUEUED
:
3138 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_QUEUED
);
3140 case ZEBRA_DPLANE_REQUEST_FAILURE
:
3142 EC_ZEBRA_DP_DELETE_FAIL
,
3143 "Failed to uninstall Nexthop ID (%pNG) from the kernel",
3146 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
3147 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
3152 zebra_nhg_handle_uninstall(nhe
);
3155 void zebra_nhg_dplane_result(struct zebra_dplane_ctx
*ctx
)
3157 enum dplane_op_e op
;
3158 enum zebra_dplane_result status
;
3160 struct nhg_hash_entry
*nhe
= NULL
;
3162 op
= dplane_ctx_get_op(ctx
);
3163 status
= dplane_ctx_get_status(ctx
);
3165 id
= dplane_ctx_get_nhe_id(ctx
);
3167 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
|| IS_ZEBRA_DEBUG_NHG_DETAIL
)
3169 "Nexthop dplane ctx %p, op %s, nexthop ID (%u), result %s",
3170 ctx
, dplane_op2str(op
), id
, dplane_res2str(status
));
3173 case DPLANE_OP_NH_DELETE
:
3174 if (status
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
3176 EC_ZEBRA_DP_DELETE_FAIL
,
3177 "Failed to uninstall Nexthop ID (%u) from the kernel",
3180 /* We already free'd the data, nothing to do */
3182 case DPLANE_OP_NH_INSTALL
:
3183 case DPLANE_OP_NH_UPDATE
:
3184 nhe
= zebra_nhg_lookup_id(id
);
3187 if (IS_ZEBRA_DEBUG_NHG
)
3189 "%s operation preformed on Nexthop ID (%u) in the kernel, that we no longer have in our table",
3190 dplane_op2str(op
), id
);
3195 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_QUEUED
);
3196 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3197 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
3198 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
3199 zebra_nhg_handle_install(nhe
);
3201 /* If daemon nhg, send it an update */
3202 if (PROTO_OWNED(nhe
))
3203 zsend_nhg_notify(nhe
->type
, nhe
->zapi_instance
,
3204 nhe
->zapi_session
, nhe
->id
,
3205 ZAPI_NHG_INSTALLED
);
3207 /* If daemon nhg, send it an update */
3208 if (PROTO_OWNED(nhe
))
3209 zsend_nhg_notify(nhe
->type
, nhe
->zapi_instance
,
3210 nhe
->zapi_session
, nhe
->id
,
3211 ZAPI_NHG_FAIL_INSTALL
);
3213 if (!(zebra_nhg_proto_nexthops_only() &&
3216 EC_ZEBRA_DP_INSTALL_FAIL
,
3217 "Failed to install Nexthop (%pNG) into the kernel",
3222 case DPLANE_OP_ROUTE_INSTALL
:
3223 case DPLANE_OP_ROUTE_UPDATE
:
3224 case DPLANE_OP_ROUTE_DELETE
:
3225 case DPLANE_OP_ROUTE_NOTIFY
:
3226 case DPLANE_OP_LSP_INSTALL
:
3227 case DPLANE_OP_LSP_UPDATE
:
3228 case DPLANE_OP_LSP_DELETE
:
3229 case DPLANE_OP_LSP_NOTIFY
:
3230 case DPLANE_OP_PW_INSTALL
:
3231 case DPLANE_OP_PW_UNINSTALL
:
3232 case DPLANE_OP_SYS_ROUTE_ADD
:
3233 case DPLANE_OP_SYS_ROUTE_DELETE
:
3234 case DPLANE_OP_ADDR_INSTALL
:
3235 case DPLANE_OP_ADDR_UNINSTALL
:
3236 case DPLANE_OP_MAC_INSTALL
:
3237 case DPLANE_OP_MAC_DELETE
:
3238 case DPLANE_OP_NEIGH_INSTALL
:
3239 case DPLANE_OP_NEIGH_UPDATE
:
3240 case DPLANE_OP_NEIGH_DELETE
:
3241 case DPLANE_OP_NEIGH_IP_INSTALL
:
3242 case DPLANE_OP_NEIGH_IP_DELETE
:
3243 case DPLANE_OP_VTEP_ADD
:
3244 case DPLANE_OP_VTEP_DELETE
:
3245 case DPLANE_OP_RULE_ADD
:
3246 case DPLANE_OP_RULE_DELETE
:
3247 case DPLANE_OP_RULE_UPDATE
:
3248 case DPLANE_OP_NEIGH_DISCOVER
:
3249 case DPLANE_OP_BR_PORT_UPDATE
:
3250 case DPLANE_OP_NONE
:
3251 case DPLANE_OP_IPTABLE_ADD
:
3252 case DPLANE_OP_IPTABLE_DELETE
:
3253 case DPLANE_OP_IPSET_ADD
:
3254 case DPLANE_OP_IPSET_DELETE
:
3255 case DPLANE_OP_IPSET_ENTRY_ADD
:
3256 case DPLANE_OP_IPSET_ENTRY_DELETE
:
3257 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
3258 case DPLANE_OP_GRE_SET
:
3259 case DPLANE_OP_INTF_ADDR_ADD
:
3260 case DPLANE_OP_INTF_ADDR_DEL
:
3261 case DPLANE_OP_INTF_NETCONFIG
:
3262 case DPLANE_OP_INTF_INSTALL
:
3263 case DPLANE_OP_INTF_UPDATE
:
3264 case DPLANE_OP_INTF_DELETE
:
3265 case DPLANE_OP_TC_QDISC_INSTALL
:
3266 case DPLANE_OP_TC_QDISC_UNINSTALL
:
3267 case DPLANE_OP_TC_CLASS_ADD
:
3268 case DPLANE_OP_TC_CLASS_DELETE
:
3269 case DPLANE_OP_TC_CLASS_UPDATE
:
3270 case DPLANE_OP_TC_FILTER_ADD
:
3271 case DPLANE_OP_TC_FILTER_DELETE
:
3272 case DPLANE_OP_TC_FILTER_UPDATE
:
3277 static int zebra_nhg_sweep_entry(struct hash_bucket
*bucket
, void *arg
)
3279 struct nhg_hash_entry
*nhe
= NULL
;
3281 nhe
= (struct nhg_hash_entry
*)bucket
->data
;
3284 * same logic as with routes.
3286 * If older than startup time, we know we read them in from the
3287 * kernel and have not gotten and update for them since startup
3288 * from an upper level proto.
3290 if (zrouter
.startup_time
< nhe
->uptime
)
3291 return HASHWALK_CONTINUE
;
3294 * If it's proto-owned and not being used by a route, remove it since
3295 * we haven't gotten an update about it from the proto since startup.
3296 * This means that either the config for it was removed or the daemon
3297 * didn't get started. This handles graceful restart & retain scenario.
3299 if (PROTO_OWNED(nhe
) && nhe
->refcnt
== 1) {
3300 zebra_nhg_decrement_ref(nhe
);
3301 return HASHWALK_ABORT
;
3305 * If its being ref'd by routes, just let it be uninstalled via a route
3308 if (ZEBRA_NHG_CREATED(nhe
) && nhe
->refcnt
<= 0) {
3309 zebra_nhg_uninstall_kernel(nhe
);
3310 return HASHWALK_ABORT
;
3313 return HASHWALK_CONTINUE
;
3316 void zebra_nhg_sweep_table(struct hash
*hash
)
3321 * Yes this is extremely odd. Effectively nhg's have
3322 * other nexthop groups that depend on them and when you
3323 * remove them, you can have other entries blown up.
3324 * our hash code does not work with deleting multiple
3325 * entries at a time and will possibly cause crashes
3326 * So what to do? Whenever zebra_nhg_sweep_entry
3327 * deletes an entry it will return HASHWALK_ABORT,
3328 * cause that deletion might have triggered more.
3329 * then we can just keep sweeping this table
3330 * until nothing more is found to do.
3333 count
= hashcount(hash
);
3334 hash_walk(hash
, zebra_nhg_sweep_entry
, NULL
);
3335 } while (count
!= hashcount(hash
));
3338 static void zebra_nhg_mark_keep_entry(struct hash_bucket
*bucket
, void *arg
)
3340 struct nhg_hash_entry
*nhe
= bucket
->data
;
3342 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
3346 * When we are shutting down and we have retain mode enabled
3347 * in zebra the process is to mark each vrf that it's
3348 * routes should not be deleted. The problem with that
3349 * is that shutdown actually free's up memory which
3350 * causes the nexthop group's ref counts to go to zero
3351 * we need a way to subtly tell the system to not remove
3352 * the nexthop groups from the kernel at the same time.
3353 * The easiest just looks like that we should not mark
3354 * the nhg's as installed any more and when the ref count
3355 * goes to zero we'll attempt to delete and do nothing
3357 void zebra_nhg_mark_keep(void)
3359 hash_iterate(zrouter
.nhgs_id
, zebra_nhg_mark_keep_entry
, NULL
);
3362 /* Global control to disable use of kernel nexthops, if available. We can't
3363 * force the kernel to support nexthop ids, of course, but we can disable
3364 * zebra's use of them, for testing e.g. By default, if the kernel supports
3365 * nexthop ids, zebra uses them.
3367 void zebra_nhg_enable_kernel_nexthops(bool set
)
3369 g_nexthops_enabled
= set
;
3372 bool zebra_nhg_kernel_nexthops_enabled(void)
3374 return g_nexthops_enabled
;
3377 /* Global control for use of activated backups for recursive resolution. */
3378 void zebra_nhg_set_recursive_use_backups(bool set
)
3380 use_recursive_backups
= set
;
3383 bool zebra_nhg_recursive_use_backups(void)
3385 return use_recursive_backups
;
3389 * Global control to only use kernel nexthops for protocol created NHGs.
3390 * There are some use cases where you may not want zebra to implicitly
3391 * create kernel nexthops for all routes and only create them for NHGs
3392 * passed down by upper level protos.
3396 void zebra_nhg_set_proto_nexthops_only(bool set
)
3398 proto_nexthops_only
= set
;
3401 bool zebra_nhg_proto_nexthops_only(void)
3403 return proto_nexthops_only
;
3406 /* Add NHE from upper level proto */
3407 struct nhg_hash_entry
*zebra_nhg_proto_add(uint32_t id
, int type
,
3408 uint16_t instance
, uint32_t session
,
3409 struct nexthop_group
*nhg
, afi_t afi
)
3411 struct nhg_hash_entry lookup
;
3412 struct nhg_hash_entry
*new, *old
;
3413 struct nhg_connected
*rb_node_dep
= NULL
;
3414 struct nexthop
*newhop
;
3415 bool replace
= false;
3417 if (!nhg
->nexthop
) {
3418 if (IS_ZEBRA_DEBUG_NHG
)
3419 zlog_debug("%s: id %u, no nexthops passed to add",
3425 /* Set nexthop list as active, since they wont go through rib
3428 * Assuming valid/onlink for now.
3430 * Once resolution is figured out, we won't need this!
3432 for (ALL_NEXTHOPS_PTR(nhg
, newhop
)) {
3433 if (CHECK_FLAG(newhop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
3434 if (IS_ZEBRA_DEBUG_NHG
)
3436 "%s: id %u, backup nexthops not supported",
3441 if (newhop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
3442 if (IS_ZEBRA_DEBUG_NHG
)
3444 "%s: id %u, blackhole nexthop not supported",
3449 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
) {
3450 if (IS_ZEBRA_DEBUG_NHG
)
3452 "%s: id %u, nexthop without gateway not supported",
3457 if (!newhop
->ifindex
) {
3458 if (IS_ZEBRA_DEBUG_NHG
)
3460 "%s: id %u, nexthop without ifindex is not supported",
3464 SET_FLAG(newhop
->flags
, NEXTHOP_FLAG_ACTIVE
);
3467 zebra_nhe_init(&lookup
, afi
, nhg
->nexthop
);
3468 lookup
.nhg
.nexthop
= nhg
->nexthop
;
3469 lookup
.nhg
.nhgr
= nhg
->nhgr
;
3473 old
= zebra_nhg_lookup_id(id
);
3477 * This is a replace, just release NHE from ID for now, The
3478 * depends/dependents may still be used in the replacement so
3479 * we don't touch them other than to remove their refs to their
3483 hash_release(zrouter
.nhgs_id
, old
);
3485 /* Free all the things */
3486 zebra_nhg_release_all_deps(old
);
3489 new = zebra_nhg_rib_find_nhe(&lookup
, afi
);
3491 zebra_nhg_increment_ref(new);
3493 /* Capture zapi client info */
3494 new->zapi_instance
= instance
;
3495 new->zapi_session
= session
;
3497 zebra_nhg_set_valid_if_active(new);
3499 zebra_nhg_install_kernel(new);
3503 * Check to handle recving DEL while routes still in use then
3506 * In this case we would have decremented the refcnt already
3507 * but set the FLAG here. Go ahead and increment once to fix
3508 * the misordering we have been sent.
3510 if (CHECK_FLAG(old
->flags
, NEXTHOP_GROUP_PROTO_RELEASED
))
3511 zebra_nhg_increment_ref(old
);
3513 rib_handle_nhg_replace(old
, new);
3515 /* We have to decrement its singletons
3516 * because some might not exist in NEW.
3518 if (!zebra_nhg_depends_is_empty(old
)) {
3519 frr_each (nhg_connected_tree
, &old
->nhg_depends
,
3521 zebra_nhg_decrement_ref(rb_node_dep
->nhe
);
3524 /* Dont call the dec API, we dont want to uninstall the ID */
3526 THREAD_OFF(old
->timer
);
3527 zebra_nhg_free(old
);
3531 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
3532 zlog_debug("%s: %s nhe %p (%u), vrf %d, type %s", __func__
,
3533 (replace
? "replaced" : "added"), new, new->id
,
3534 new->vrf_id
, zebra_route_string(new->type
));
3539 /* Delete NHE from upper level proto, caller must decrement ref */
3540 struct nhg_hash_entry
*zebra_nhg_proto_del(uint32_t id
, int type
)
3542 struct nhg_hash_entry
*nhe
;
3544 nhe
= zebra_nhg_lookup_id(id
);
3547 if (IS_ZEBRA_DEBUG_NHG
)
3548 zlog_debug("%s: id %u, lookup failed", __func__
, id
);
3553 if (type
!= nhe
->type
) {
3554 if (IS_ZEBRA_DEBUG_NHG
)
3556 "%s: id %u, type %s mismatch, sent by %s, ignoring",
3557 __func__
, id
, zebra_route_string(nhe
->type
),
3558 zebra_route_string(type
));
3562 if (CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_PROTO_RELEASED
)) {
3563 if (IS_ZEBRA_DEBUG_NHG
)
3564 zlog_debug("%s: id %u, already released", __func__
, id
);
3569 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_PROTO_RELEASED
);
3571 if (nhe
->refcnt
> 1) {
3572 if (IS_ZEBRA_DEBUG_NHG
)
3574 "%s: %pNG, still being used by routes refcnt %u",
3575 __func__
, nhe
, nhe
->refcnt
);
3579 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
3580 zlog_debug("%s: deleted nhe %p (%pNG), vrf %d, type %s",
3581 __func__
, nhe
, nhe
, nhe
->vrf_id
,
3582 zebra_route_string(nhe
->type
));
3587 struct nhg_score_proto_iter
{
3592 static void zebra_nhg_score_proto_entry(struct hash_bucket
*bucket
, void *arg
)
3594 struct nhg_hash_entry
*nhe
;
3595 struct nhg_score_proto_iter
*iter
;
3597 nhe
= (struct nhg_hash_entry
*)bucket
->data
;
3600 /* Needs to match type and outside zebra ID space */
3601 if (nhe
->type
== iter
->type
&& PROTO_OWNED(nhe
)) {
3602 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
3604 "%s: found nhe %p (%pNG), vrf %d, type %s after client disconnect",
3605 __func__
, nhe
, nhe
, nhe
->vrf_id
,
3606 zebra_route_string(nhe
->type
));
3608 /* Add to removal list */
3609 listnode_add(iter
->found
, nhe
);
3613 /* Remove specific by proto NHGs */
3614 unsigned long zebra_nhg_score_proto(int type
)
3616 struct nhg_hash_entry
*nhe
;
3617 struct nhg_score_proto_iter iter
= {};
3618 struct listnode
*ln
;
3619 unsigned long count
;
3622 iter
.found
= list_new();
3624 /* Find matching entries to remove */
3625 hash_iterate(zrouter
.nhgs_id
, zebra_nhg_score_proto_entry
, &iter
);
3627 /* Now remove them */
3628 for (ALL_LIST_ELEMENTS_RO(iter
.found
, ln
, nhe
)) {
3630 * This should be the last ref if we remove client routes too,
3631 * and thus should remove and free them.
3633 zebra_nhg_decrement_ref(nhe
);
3636 count
= iter
.found
->count
;
3637 list_delete(&iter
.found
);
3642 printfrr_ext_autoreg_p("NG", printfrr_nhghe
);
3643 static ssize_t
printfrr_nhghe(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
3646 const struct nhg_hash_entry
*nhe
= ptr
;
3647 const struct nhg_connected
*dep
;
3651 return bputs(buf
, "[NULL]");
3653 ret
+= bprintfrr(buf
, "%u[", nhe
->id
);
3655 ret
+= printfrr_nhs(buf
, nhe
->nhg
.nexthop
);
3657 int count
= zebra_nhg_depends_count(nhe
);
3659 frr_each (nhg_connected_tree_const
, &nhe
->nhg_depends
, dep
) {
3660 ret
+= bprintfrr(buf
, "%u", dep
->nhe
->id
);
3662 ret
+= bputs(buf
, "/");
3667 ret
+= bputs(buf
, "]");