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"
48 DEFINE_MTYPE_STATIC(ZEBRA
, NHG
, "Nexthop Group Entry");
49 DEFINE_MTYPE_STATIC(ZEBRA
, NHG_CONNECTED
, "Nexthop Group Connected");
50 DEFINE_MTYPE_STATIC(ZEBRA
, NHG_CTX
, "Nexthop Group Context");
52 /* Map backup nexthop indices between two nhes */
53 struct backup_nh_map_s
{
62 /* id counter to keep in sync with kernel */
65 /* Controlled through ui */
66 static bool g_nexthops_enabled
= true;
67 static bool proto_nexthops_only
;
68 static bool use_recursive_backups
= true;
70 static struct nhg_hash_entry
*depends_find(const struct nexthop
*nh
, afi_t afi
,
71 int type
, bool from_dplane
);
72 static void depends_add(struct nhg_connected_tree_head
*head
,
73 struct nhg_hash_entry
*depend
);
74 static struct nhg_hash_entry
*
75 depends_find_add(struct nhg_connected_tree_head
*head
, struct nexthop
*nh
,
76 afi_t afi
, int type
, bool from_dplane
);
77 static struct nhg_hash_entry
*
78 depends_find_id_add(struct nhg_connected_tree_head
*head
, uint32_t id
);
79 static void depends_decrement_free(struct nhg_connected_tree_head
*head
);
81 static struct nhg_backup_info
*
82 nhg_backup_copy(const struct nhg_backup_info
*orig
);
84 /* Helper function for getting the next allocatable ID */
85 static uint32_t nhg_get_next_id(void)
90 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
91 zlog_debug("%s: ID %u checking", __func__
, id_counter
);
93 if (id_counter
== ZEBRA_NHG_PROTO_LOWER
) {
94 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
95 zlog_debug("%s: ID counter wrapped", __func__
);
101 if (zebra_nhg_lookup_id(id_counter
)) {
102 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
103 zlog_debug("%s: ID already exists", __func__
);
114 static void nhg_connected_free(struct nhg_connected
*dep
)
116 XFREE(MTYPE_NHG_CONNECTED
, dep
);
119 static struct nhg_connected
*nhg_connected_new(struct nhg_hash_entry
*nhe
)
121 struct nhg_connected
*new = NULL
;
123 new = XCALLOC(MTYPE_NHG_CONNECTED
, sizeof(struct nhg_connected
));
129 void nhg_connected_tree_free(struct nhg_connected_tree_head
*head
)
131 struct nhg_connected
*rb_node_dep
= NULL
;
133 if (!nhg_connected_tree_is_empty(head
)) {
134 frr_each_safe(nhg_connected_tree
, head
, rb_node_dep
) {
135 nhg_connected_tree_del(head
, rb_node_dep
);
136 nhg_connected_free(rb_node_dep
);
141 bool nhg_connected_tree_is_empty(const struct nhg_connected_tree_head
*head
)
143 return nhg_connected_tree_count(head
) ? false : true;
146 struct nhg_connected
*
147 nhg_connected_tree_root(struct nhg_connected_tree_head
*head
)
149 return nhg_connected_tree_first(head
);
152 struct nhg_hash_entry
*
153 nhg_connected_tree_del_nhe(struct nhg_connected_tree_head
*head
,
154 struct nhg_hash_entry
*depend
)
156 struct nhg_connected lookup
= {};
157 struct nhg_connected
*remove
= NULL
;
158 struct nhg_hash_entry
*removed_nhe
;
162 /* Lookup to find the element, then remove it */
163 remove
= nhg_connected_tree_find(head
, &lookup
);
165 /* Re-returning here just in case this API changes..
166 * the _del list api's are a bit undefined at the moment.
168 * So hopefully returning here will make it fail if the api
169 * changes to something different than currently expected.
171 remove
= nhg_connected_tree_del(head
, remove
);
173 /* If the entry was sucessfully removed, free the 'connected` struct */
175 removed_nhe
= remove
->nhe
;
176 nhg_connected_free(remove
);
183 /* Assuming UNIQUE RB tree. If this changes, assumptions here about
184 * insertion need to change.
186 struct nhg_hash_entry
*
187 nhg_connected_tree_add_nhe(struct nhg_connected_tree_head
*head
,
188 struct nhg_hash_entry
*depend
)
190 struct nhg_connected
*new = NULL
;
192 new = nhg_connected_new(depend
);
194 /* On success, NULL will be returned from the
197 if (new && (nhg_connected_tree_add(head
, new) == NULL
))
200 /* If it wasn't successful, it must be a duplicate. We enforce the
201 * unique property for the `nhg_connected` tree.
203 nhg_connected_free(new);
209 nhg_connected_tree_decrement_ref(struct nhg_connected_tree_head
*head
)
211 struct nhg_connected
*rb_node_dep
= NULL
;
213 frr_each_safe(nhg_connected_tree
, head
, rb_node_dep
) {
214 zebra_nhg_decrement_ref(rb_node_dep
->nhe
);
219 nhg_connected_tree_increment_ref(struct nhg_connected_tree_head
*head
)
221 struct nhg_connected
*rb_node_dep
= NULL
;
223 frr_each(nhg_connected_tree
, head
, rb_node_dep
) {
224 zebra_nhg_increment_ref(rb_node_dep
->nhe
);
228 struct nhg_hash_entry
*zebra_nhg_resolve(struct nhg_hash_entry
*nhe
)
230 if (CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_RECURSIVE
)
231 && !zebra_nhg_depends_is_empty(nhe
)) {
232 nhe
= nhg_connected_tree_root(&nhe
->nhg_depends
)->nhe
;
233 return zebra_nhg_resolve(nhe
);
239 unsigned int zebra_nhg_depends_count(const struct nhg_hash_entry
*nhe
)
241 return nhg_connected_tree_count(&nhe
->nhg_depends
);
244 bool zebra_nhg_depends_is_empty(const struct nhg_hash_entry
*nhe
)
246 return nhg_connected_tree_is_empty(&nhe
->nhg_depends
);
249 static void zebra_nhg_depends_del(struct nhg_hash_entry
*from
,
250 struct nhg_hash_entry
*depend
)
252 nhg_connected_tree_del_nhe(&from
->nhg_depends
, depend
);
255 static void zebra_nhg_depends_init(struct nhg_hash_entry
*nhe
)
257 nhg_connected_tree_init(&nhe
->nhg_depends
);
260 unsigned int zebra_nhg_dependents_count(const struct nhg_hash_entry
*nhe
)
262 return nhg_connected_tree_count(&nhe
->nhg_dependents
);
266 bool zebra_nhg_dependents_is_empty(const struct nhg_hash_entry
*nhe
)
268 return nhg_connected_tree_is_empty(&nhe
->nhg_dependents
);
271 static void zebra_nhg_dependents_del(struct nhg_hash_entry
*from
,
272 struct nhg_hash_entry
*dependent
)
274 nhg_connected_tree_del_nhe(&from
->nhg_dependents
, dependent
);
277 static void zebra_nhg_dependents_add(struct nhg_hash_entry
*to
,
278 struct nhg_hash_entry
*dependent
)
280 nhg_connected_tree_add_nhe(&to
->nhg_dependents
, dependent
);
283 static void zebra_nhg_dependents_init(struct nhg_hash_entry
*nhe
)
285 nhg_connected_tree_init(&nhe
->nhg_dependents
);
288 /* Release this nhe from anything depending on it */
289 static void zebra_nhg_dependents_release(struct nhg_hash_entry
*nhe
)
291 struct nhg_connected
*rb_node_dep
= NULL
;
293 frr_each_safe(nhg_connected_tree
, &nhe
->nhg_dependents
, rb_node_dep
) {
294 zebra_nhg_depends_del(rb_node_dep
->nhe
, nhe
);
295 /* recheck validity of the dependent */
296 zebra_nhg_check_valid(rb_node_dep
->nhe
);
300 /* Release this nhe from anything that it depends on */
301 static void zebra_nhg_depends_release(struct nhg_hash_entry
*nhe
)
303 if (!zebra_nhg_depends_is_empty(nhe
)) {
304 struct nhg_connected
*rb_node_dep
= NULL
;
306 frr_each_safe(nhg_connected_tree
, &nhe
->nhg_depends
,
308 zebra_nhg_dependents_del(rb_node_dep
->nhe
, nhe
);
314 struct nhg_hash_entry
*zebra_nhg_lookup_id(uint32_t id
)
316 struct nhg_hash_entry lookup
= {};
319 return hash_lookup(zrouter
.nhgs_id
, &lookup
);
322 static int zebra_nhg_insert_id(struct nhg_hash_entry
*nhe
)
324 if (hash_lookup(zrouter
.nhgs_id
, nhe
)) {
326 EC_ZEBRA_NHG_TABLE_INSERT_FAILED
,
327 "Failed inserting NHG id=%u into the ID hash table, entry already exists",
332 (void)hash_get(zrouter
.nhgs_id
, nhe
, hash_alloc_intern
);
337 static void zebra_nhg_set_if(struct nhg_hash_entry
*nhe
, struct interface
*ifp
)
340 if_nhg_dependents_add(ifp
, nhe
);
344 zebra_nhg_connect_depends(struct nhg_hash_entry
*nhe
,
345 struct nhg_connected_tree_head
*nhg_depends
)
347 struct nhg_connected
*rb_node_dep
= NULL
;
349 /* This has been allocated higher above in the stack. Could probably
350 * re-allocate and free the old stuff but just using the same memory
351 * for now. Otherwise, their might be a time trade-off for repeated
352 * alloc/frees as startup.
354 nhe
->nhg_depends
= *nhg_depends
;
356 /* Attach backpointer to anything that it depends on */
357 zebra_nhg_dependents_init(nhe
);
358 if (!zebra_nhg_depends_is_empty(nhe
)) {
359 frr_each(nhg_connected_tree
, &nhe
->nhg_depends
, rb_node_dep
) {
360 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
361 zlog_debug("%s: nhe %p (%u), dep %p (%u)",
362 __func__
, nhe
, nhe
->id
,
364 rb_node_dep
->nhe
->id
);
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 id=%u",
474 nhe
->nhg
.nexthop
->ifindex
,
475 nhe
->nhg
.nexthop
->vrf_id
, nhe
->id
);
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 /* Nexthops should be in-order, so we simply compare them in-place */
568 for (nexthop1
= nhe1
->nhg
.nexthop
, nexthop2
= nhe2
->nhg
.nexthop
;
569 nexthop1
&& nexthop2
;
570 nexthop1
= nexthop1
->next
, nexthop2
= nexthop2
->next
) {
572 if (!nhg_compare_nexthops(nexthop1
, nexthop2
))
576 /* Check for unequal list lengths */
577 if (nexthop1
|| nexthop2
)
580 /* If there's no backup info, comparison is done. */
581 if ((nhe1
->backup_info
== NULL
) && (nhe2
->backup_info
== NULL
))
584 /* Compare backup info also - test the easy things first */
585 if (nhe1
->backup_info
&& (nhe2
->backup_info
== NULL
))
587 if (nhe2
->backup_info
&& (nhe1
->backup_info
== NULL
))
590 /* Compare number of backups before actually comparing any */
591 for (nexthop1
= nhe1
->backup_info
->nhe
->nhg
.nexthop
,
592 nexthop2
= nhe2
->backup_info
->nhe
->nhg
.nexthop
;
593 nexthop1
&& nexthop2
;
594 nexthop1
= nexthop1
->next
, nexthop2
= nexthop2
->next
) {
598 /* Did we find the end of one list before the other? */
599 if (nexthop1
|| nexthop2
)
602 /* Have to compare the backup nexthops */
603 for (nexthop1
= nhe1
->backup_info
->nhe
->nhg
.nexthop
,
604 nexthop2
= nhe2
->backup_info
->nhe
->nhg
.nexthop
;
605 nexthop1
&& nexthop2
;
606 nexthop1
= nexthop1
->next
, nexthop2
= nexthop2
->next
) {
608 if (!nhg_compare_nexthops(nexthop1
, nexthop2
))
615 bool zebra_nhg_hash_id_equal(const void *arg1
, const void *arg2
)
617 const struct nhg_hash_entry
*nhe1
= arg1
;
618 const struct nhg_hash_entry
*nhe2
= arg2
;
620 return nhe1
->id
== nhe2
->id
;
623 static int zebra_nhg_process_grp(struct nexthop_group
*nhg
,
624 struct nhg_connected_tree_head
*depends
,
625 struct nh_grp
*grp
, uint8_t count
)
627 nhg_connected_tree_init(depends
);
629 for (int i
= 0; i
< count
; i
++) {
630 struct nhg_hash_entry
*depend
= NULL
;
631 /* We do not care about nexthop_grp.weight at
632 * this time. But we should figure out
633 * how to adapt this to our code in
636 depend
= depends_find_id_add(depends
, grp
[i
].id
);
641 "Received Nexthop Group from the kernel with a dependent Nexthop ID (%u) which we do not have in our table",
647 * If this is a nexthop with its own group
648 * dependencies, add them as well. Not sure its
649 * even possible to have a group within a group
653 copy_nexthops(&nhg
->nexthop
, depend
->nhg
.nexthop
, NULL
);
659 static void handle_recursive_depend(struct nhg_connected_tree_head
*nhg_depends
,
660 struct nexthop
*nh
, afi_t afi
, int type
)
662 struct nhg_hash_entry
*depend
= NULL
;
663 struct nexthop_group resolved_ng
= {};
665 resolved_ng
.nexthop
= nh
;
667 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
668 zlog_debug("%s: head %p, nh %pNHv",
669 __func__
, nhg_depends
, nh
);
671 depend
= zebra_nhg_rib_find(0, &resolved_ng
, afi
, type
);
673 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
674 zlog_debug("%s: nh %pNHv => %p (%u)",
675 __func__
, nh
, depend
,
676 depend
? depend
->id
: 0);
679 depends_add(nhg_depends
, depend
);
683 * Lookup an nhe in the global hash, using data from another nhe. If 'lookup'
684 * has an id value, that's used. Create a new global/shared nhe if not found.
686 static bool zebra_nhe_find(struct nhg_hash_entry
**nhe
, /* return value */
687 struct nhg_hash_entry
*lookup
,
688 struct nhg_connected_tree_head
*nhg_depends
,
689 afi_t afi
, bool from_dplane
)
691 bool created
= false;
692 bool recursive
= false;
693 struct nhg_hash_entry
*newnhe
, *backup_nhe
;
694 struct nexthop
*nh
= NULL
;
696 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
698 "%s: id %u, lookup %p, vrf %d, type %d, depends %p%s",
699 __func__
, lookup
->id
, lookup
, lookup
->vrf_id
,
700 lookup
->type
, nhg_depends
,
701 (from_dplane
? " (from dplane)" : ""));
704 (*nhe
) = zebra_nhg_lookup_id(lookup
->id
);
706 (*nhe
) = hash_lookup(zrouter
.nhgs
, lookup
);
708 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
709 zlog_debug("%s: lookup => %p (%u)",
711 (*nhe
) ? (*nhe
)->id
: 0);
713 /* If we found an existing object, we're done */
717 /* We're going to create/insert a new nhe:
718 * assign the next global id value if necessary.
721 lookup
->id
= nhg_get_next_id();
723 if (!from_dplane
&& lookup
->id
< ZEBRA_NHG_PROTO_LOWER
) {
725 * This is a zebra hashed/owned NHG.
727 * It goes in HASH and ID table.
729 newnhe
= hash_get(zrouter
.nhgs
, lookup
, zebra_nhg_hash_alloc
);
730 zebra_nhg_insert_id(newnhe
);
733 * This is upperproto owned NHG or one we read in from dataplane
734 * and should not be hashed to.
736 * It goes in ID table.
739 hash_get(zrouter
.nhgs_id
, lookup
, zebra_nhg_hash_alloc
);
744 /* Mail back the new object */
747 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
748 zlog_debug("%s: => created %p (%u)", __func__
, newnhe
,
751 /* Only hash/lookup the depends if the first lookup
752 * fails to find something. This should hopefully save a
753 * lot of cycles for larger ecmp sizes.
756 /* If you don't want to hash on each nexthop in the
757 * nexthop group struct you can pass the depends
758 * directly. Kernel-side we do this since it just looks
761 zebra_nhg_connect_depends(newnhe
, nhg_depends
);
765 /* Prepare dependency relationships if this is not a
766 * singleton nexthop. There are two cases: a single
767 * recursive nexthop, where we need a relationship to the
768 * resolving nexthop; or a group of nexthops, where we need
769 * relationships with the corresponding singletons.
771 zebra_nhg_depends_init(newnhe
);
773 nh
= newnhe
->nhg
.nexthop
;
775 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ACTIVE
))
776 SET_FLAG(newnhe
->flags
, NEXTHOP_GROUP_VALID
);
778 if (nh
->next
== NULL
&& newnhe
->id
< ZEBRA_NHG_PROTO_LOWER
) {
779 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
)) {
780 /* Single recursive nexthop */
781 handle_recursive_depend(&newnhe
->nhg_depends
,
787 /* Proto-owned are groups by default */
788 /* List of nexthops */
789 for (nh
= newnhe
->nhg
.nexthop
; nh
; nh
= nh
->next
) {
790 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
791 zlog_debug("%s: depends NH %pNHv %s",
793 CHECK_FLAG(nh
->flags
,
794 NEXTHOP_FLAG_RECURSIVE
) ?
797 depends_find_add(&newnhe
->nhg_depends
, nh
, afi
,
798 newnhe
->type
, from_dplane
);
803 SET_FLAG(newnhe
->flags
, NEXTHOP_GROUP_RECURSIVE
);
805 /* Attach dependent backpointers to singletons */
806 zebra_nhg_connect_depends(newnhe
, &newnhe
->nhg_depends
);
812 if (zebra_nhg_get_backup_nhg(newnhe
) == NULL
||
813 zebra_nhg_get_backup_nhg(newnhe
)->nexthop
== NULL
)
816 /* If there are backup nexthops, add them to the backup
817 * depends tree. The rules here are a little different.
820 backup_nhe
= newnhe
->backup_info
->nhe
;
822 nh
= backup_nhe
->nhg
.nexthop
;
824 /* Singleton recursive NH */
825 if (nh
->next
== NULL
&&
826 CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
)) {
827 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
828 zlog_debug("%s: backup depend NH %pNHv (R)",
831 /* Single recursive nexthop */
832 handle_recursive_depend(&backup_nhe
->nhg_depends
, nh
->resolved
,
833 afi
, backup_nhe
->type
);
836 /* One or more backup NHs */
837 for (; nh
; nh
= nh
->next
) {
838 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
839 zlog_debug("%s: backup depend NH %pNHv %s",
841 CHECK_FLAG(nh
->flags
,
842 NEXTHOP_FLAG_RECURSIVE
) ?
845 depends_find_add(&backup_nhe
->nhg_depends
, nh
, afi
,
846 backup_nhe
->type
, from_dplane
);
851 SET_FLAG(backup_nhe
->flags
, NEXTHOP_GROUP_RECURSIVE
);
854 /* Reset time since last update */
855 (*nhe
)->uptime
= monotime(NULL
);
861 * Lookup or create an nhe, based on an nhg or an nhe id.
863 static bool zebra_nhg_find(struct nhg_hash_entry
**nhe
, uint32_t id
,
864 struct nexthop_group
*nhg
,
865 struct nhg_connected_tree_head
*nhg_depends
,
866 vrf_id_t vrf_id
, afi_t afi
, int type
,
869 struct nhg_hash_entry lookup
= {};
870 bool created
= false;
872 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
873 zlog_debug("%s: id %u, nhg %p, vrf %d, type %d, depends %p",
874 __func__
, id
, nhg
, vrf_id
, type
,
877 /* Use a temporary nhe and call into the superset/common code */
879 lookup
.type
= type
? type
: ZEBRA_ROUTE_NHG
;
882 lookup
.vrf_id
= vrf_id
;
883 if (nhg_depends
|| lookup
.nhg
.nexthop
->next
) {
884 /* Groups can have all vrfs and AF's in them */
885 lookup
.afi
= AFI_UNSPEC
;
887 switch (lookup
.nhg
.nexthop
->type
) {
888 case (NEXTHOP_TYPE_IFINDEX
):
889 case (NEXTHOP_TYPE_BLACKHOLE
):
891 * This switch case handles setting the afi different
892 * for ipv4/v6 routes. Ifindex/blackhole nexthop
893 * objects cannot be ambiguous, they must be Address
894 * Family specific. If we get here, we will either use
895 * the AF of the route, or the one we got passed from
896 * here from the kernel.
900 case (NEXTHOP_TYPE_IPV4_IFINDEX
):
901 case (NEXTHOP_TYPE_IPV4
):
904 case (NEXTHOP_TYPE_IPV6_IFINDEX
):
905 case (NEXTHOP_TYPE_IPV6
):
906 lookup
.afi
= AFI_IP6
;
911 created
= zebra_nhe_find(nhe
, &lookup
, nhg_depends
, afi
, from_dplane
);
916 /* Find/create a single nexthop */
917 static struct nhg_hash_entry
*zebra_nhg_find_nexthop(uint32_t id
,
922 struct nhg_hash_entry
*nhe
= NULL
;
923 struct nexthop_group nhg
= {};
924 vrf_id_t vrf_id
= !vrf_is_backend_netns() ? VRF_DEFAULT
: nh
->vrf_id
;
926 nexthop_group_add_sorted(&nhg
, nh
);
928 zebra_nhg_find(&nhe
, id
, &nhg
, NULL
, vrf_id
, afi
, type
, from_dplane
);
930 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
931 zlog_debug("%s: nh %pNHv => %p (%u)",
932 __func__
, nh
, nhe
, nhe
? nhe
->id
: 0);
937 static uint32_t nhg_ctx_get_id(const struct nhg_ctx
*ctx
)
942 static void nhg_ctx_set_status(struct nhg_ctx
*ctx
, enum nhg_ctx_status status
)
944 ctx
->status
= status
;
947 static enum nhg_ctx_status
nhg_ctx_get_status(const struct nhg_ctx
*ctx
)
952 static void nhg_ctx_set_op(struct nhg_ctx
*ctx
, enum nhg_ctx_op_e op
)
957 static enum nhg_ctx_op_e
nhg_ctx_get_op(const struct nhg_ctx
*ctx
)
962 static vrf_id_t
nhg_ctx_get_vrf_id(const struct nhg_ctx
*ctx
)
967 static int nhg_ctx_get_type(const struct nhg_ctx
*ctx
)
972 static int nhg_ctx_get_afi(const struct nhg_ctx
*ctx
)
977 static struct nexthop
*nhg_ctx_get_nh(struct nhg_ctx
*ctx
)
982 static uint8_t nhg_ctx_get_count(const struct nhg_ctx
*ctx
)
987 static struct nh_grp
*nhg_ctx_get_grp(struct nhg_ctx
*ctx
)
992 static struct nhg_ctx
*nhg_ctx_new(void)
996 new = XCALLOC(MTYPE_NHG_CTX
, sizeof(struct nhg_ctx
));
1001 void nhg_ctx_free(struct nhg_ctx
**ctx
)
1008 assert((*ctx
) != NULL
);
1010 if (nhg_ctx_get_count(*ctx
))
1013 nh
= nhg_ctx_get_nh(*ctx
);
1015 nexthop_del_labels(nh
);
1016 nexthop_del_srv6_seg6local(nh
);
1017 nexthop_del_srv6_seg6(nh
);
1020 XFREE(MTYPE_NHG_CTX
, *ctx
);
1023 static struct nhg_ctx
*nhg_ctx_init(uint32_t id
, struct nexthop
*nh
,
1024 struct nh_grp
*grp
, vrf_id_t vrf_id
,
1025 afi_t afi
, int type
, uint8_t count
)
1027 struct nhg_ctx
*ctx
= NULL
;
1029 ctx
= nhg_ctx_new();
1032 ctx
->vrf_id
= vrf_id
;
1038 /* Copy over the array */
1039 memcpy(&ctx
->u
.grp
, grp
, count
* sizeof(struct nh_grp
));
1046 static void zebra_nhg_set_valid(struct nhg_hash_entry
*nhe
)
1048 struct nhg_connected
*rb_node_dep
;
1050 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
1052 frr_each(nhg_connected_tree
, &nhe
->nhg_dependents
, rb_node_dep
)
1053 zebra_nhg_set_valid(rb_node_dep
->nhe
);
1056 static void zebra_nhg_set_invalid(struct nhg_hash_entry
*nhe
)
1058 struct nhg_connected
*rb_node_dep
;
1060 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
1062 /* Update validity of nexthops depending on it */
1063 frr_each(nhg_connected_tree
, &nhe
->nhg_dependents
, rb_node_dep
)
1064 zebra_nhg_check_valid(rb_node_dep
->nhe
);
1067 void zebra_nhg_check_valid(struct nhg_hash_entry
*nhe
)
1069 struct nhg_connected
*rb_node_dep
= NULL
;
1072 /* If anthing else in the group is valid, the group is valid */
1073 frr_each(nhg_connected_tree
, &nhe
->nhg_depends
, rb_node_dep
) {
1074 if (CHECK_FLAG(rb_node_dep
->nhe
->flags
, NEXTHOP_GROUP_VALID
)) {
1082 zebra_nhg_set_valid(nhe
);
1084 zebra_nhg_set_invalid(nhe
);
1087 static void zebra_nhg_release_all_deps(struct nhg_hash_entry
*nhe
)
1089 /* Remove it from any lists it may be on */
1090 zebra_nhg_depends_release(nhe
);
1091 zebra_nhg_dependents_release(nhe
);
1093 if_nhg_dependents_del(nhe
->ifp
, nhe
);
1096 static void zebra_nhg_release(struct nhg_hash_entry
*nhe
)
1098 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1099 zlog_debug("%s: nhe %p (%u)", __func__
, nhe
, nhe
->id
);
1101 zebra_nhg_release_all_deps(nhe
);
1104 * If its not zebra owned, we didn't store it here and have to be
1105 * sure we don't clear one thats actually being used.
1107 if (nhe
->id
< ZEBRA_NHG_PROTO_LOWER
)
1108 hash_release(zrouter
.nhgs
, nhe
);
1110 hash_release(zrouter
.nhgs_id
, nhe
);
1113 static void zebra_nhg_handle_uninstall(struct nhg_hash_entry
*nhe
)
1115 zebra_nhg_release(nhe
);
1116 zebra_nhg_free(nhe
);
1119 static void zebra_nhg_handle_install(struct nhg_hash_entry
*nhe
)
1121 /* Update validity of groups depending on it */
1122 struct nhg_connected
*rb_node_dep
;
1124 frr_each_safe(nhg_connected_tree
, &nhe
->nhg_dependents
, rb_node_dep
)
1125 zebra_nhg_set_valid(rb_node_dep
->nhe
);
1129 * The kernel/other program has changed the state of a nexthop object we are
1132 static void zebra_nhg_handle_kernel_state_change(struct nhg_hash_entry
*nhe
,
1138 "Kernel %s a nexthop group with ID (%u) that we are still using for a route, sending it back down",
1139 (is_delete
? "deleted" : "updated"), nhe
->id
);
1141 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
1142 zebra_nhg_install_kernel(nhe
);
1144 zebra_nhg_handle_uninstall(nhe
);
1147 static int nhg_ctx_process_new(struct nhg_ctx
*ctx
)
1149 struct nexthop_group
*nhg
= NULL
;
1150 struct nhg_connected_tree_head nhg_depends
= {};
1151 struct nhg_hash_entry
*lookup
= NULL
;
1152 struct nhg_hash_entry
*nhe
= NULL
;
1154 uint32_t id
= nhg_ctx_get_id(ctx
);
1155 uint8_t count
= nhg_ctx_get_count(ctx
);
1156 vrf_id_t vrf_id
= nhg_ctx_get_vrf_id(ctx
);
1157 int type
= nhg_ctx_get_type(ctx
);
1158 afi_t afi
= nhg_ctx_get_afi(ctx
);
1160 lookup
= zebra_nhg_lookup_id(id
);
1162 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1163 zlog_debug("%s: id %u, count %d, lookup => %p",
1164 __func__
, id
, count
, lookup
);
1167 /* This is already present in our table, hence an update
1168 * that we did not initate.
1170 zebra_nhg_handle_kernel_state_change(lookup
, false);
1174 if (nhg_ctx_get_count(ctx
)) {
1175 nhg
= nexthop_group_new();
1176 if (zebra_nhg_process_grp(nhg
, &nhg_depends
,
1177 nhg_ctx_get_grp(ctx
), count
)) {
1178 depends_decrement_free(&nhg_depends
);
1179 nexthop_group_delete(&nhg
);
1183 if (!zebra_nhg_find(&nhe
, id
, nhg
, &nhg_depends
, vrf_id
, afi
,
1185 depends_decrement_free(&nhg_depends
);
1187 /* These got copied over in zebra_nhg_alloc() */
1188 nexthop_group_delete(&nhg
);
1190 nhe
= zebra_nhg_find_nexthop(id
, nhg_ctx_get_nh(ctx
), afi
, type
,
1195 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
1196 "Zebra failed to find or create a nexthop hash entry for ID (%u)",
1201 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1202 zlog_debug("%s: nhe %p (%u) is new", __func__
, nhe
, nhe
->id
);
1205 * If daemon nhg from the kernel, add a refcnt here to indicate the
1208 if (PROTO_OWNED(nhe
))
1209 zebra_nhg_increment_ref(nhe
);
1211 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
1212 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
1217 static int nhg_ctx_process_del(struct nhg_ctx
*ctx
)
1219 struct nhg_hash_entry
*nhe
= NULL
;
1220 uint32_t id
= nhg_ctx_get_id(ctx
);
1222 nhe
= zebra_nhg_lookup_id(id
);
1226 EC_ZEBRA_BAD_NHG_MESSAGE
,
1227 "Kernel delete message received for nexthop group ID (%u) that we do not have in our ID table",
1232 zebra_nhg_handle_kernel_state_change(nhe
, true);
1237 static void nhg_ctx_fini(struct nhg_ctx
**ctx
)
1240 * Just freeing for now, maybe do something more in the future
1247 static int queue_add(struct nhg_ctx
*ctx
)
1249 /* If its queued or already processed do nothing */
1250 if (nhg_ctx_get_status(ctx
) == NHG_CTX_QUEUED
)
1253 if (rib_queue_nhg_ctx_add(ctx
)) {
1254 nhg_ctx_set_status(ctx
, NHG_CTX_FAILURE
);
1258 nhg_ctx_set_status(ctx
, NHG_CTX_QUEUED
);
1263 int nhg_ctx_process(struct nhg_ctx
*ctx
)
1267 switch (nhg_ctx_get_op(ctx
)) {
1268 case NHG_CTX_OP_NEW
:
1269 ret
= nhg_ctx_process_new(ctx
);
1270 if (nhg_ctx_get_count(ctx
) && ret
== -ENOENT
1271 && nhg_ctx_get_status(ctx
) != NHG_CTX_REQUEUED
) {
1273 * We have entered a situation where we are
1274 * processing a group from the kernel
1275 * that has a contained nexthop which
1276 * we have not yet processed.
1278 * Re-enqueue this ctx to be handled exactly one
1279 * more time (indicated by the flag).
1281 * By the time we get back to it, we
1282 * should have processed its depends.
1284 nhg_ctx_set_status(ctx
, NHG_CTX_NONE
);
1285 if (queue_add(ctx
) == 0) {
1286 nhg_ctx_set_status(ctx
, NHG_CTX_REQUEUED
);
1291 case NHG_CTX_OP_DEL
:
1292 ret
= nhg_ctx_process_del(ctx
);
1293 case NHG_CTX_OP_NONE
:
1297 nhg_ctx_set_status(ctx
, (ret
? NHG_CTX_FAILURE
: NHG_CTX_SUCCESS
));
1304 /* Kernel-side, you either get a single new nexthop or a array of ID's */
1305 int zebra_nhg_kernel_find(uint32_t id
, struct nexthop
*nh
, struct nh_grp
*grp
,
1306 uint8_t count
, vrf_id_t vrf_id
, afi_t afi
, int type
,
1309 struct nhg_ctx
*ctx
= NULL
;
1311 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1312 zlog_debug("%s: nh %pNHv, id %u, count %d",
1313 __func__
, nh
, id
, (int)count
);
1315 if (id
> id_counter
&& id
< ZEBRA_NHG_PROTO_LOWER
)
1316 /* Increase our counter so we don't try to create
1317 * an ID that already exists
1321 ctx
= nhg_ctx_init(id
, nh
, grp
, vrf_id
, afi
, type
, count
);
1322 nhg_ctx_set_op(ctx
, NHG_CTX_OP_NEW
);
1324 /* Under statup conditions, we need to handle them immediately
1325 * like we do for routes. Otherwise, we are going to get a route
1326 * with a nhe_id that we have not handled.
1329 return nhg_ctx_process(ctx
);
1331 if (queue_add(ctx
)) {
1339 /* Kernel-side, received delete message */
1340 int zebra_nhg_kernel_del(uint32_t id
, vrf_id_t vrf_id
)
1342 struct nhg_ctx
*ctx
= NULL
;
1344 ctx
= nhg_ctx_init(id
, NULL
, NULL
, vrf_id
, 0, 0, 0);
1346 nhg_ctx_set_op(ctx
, NHG_CTX_OP_DEL
);
1348 if (queue_add(ctx
)) {
1356 /* Some dependency helper functions */
1357 static struct nhg_hash_entry
*depends_find_recursive(const struct nexthop
*nh
,
1358 afi_t afi
, int type
)
1360 struct nhg_hash_entry
*nhe
;
1361 struct nexthop
*lookup
= NULL
;
1363 lookup
= nexthop_dup(nh
, NULL
);
1365 nhe
= zebra_nhg_find_nexthop(0, lookup
, afi
, type
, false);
1367 nexthops_free(lookup
);
1372 static struct nhg_hash_entry
*depends_find_singleton(const struct nexthop
*nh
,
1373 afi_t afi
, int type
,
1376 struct nhg_hash_entry
*nhe
;
1377 struct nexthop lookup
= {};
1379 /* Capture a snapshot of this single nh; it might be part of a list,
1380 * so we need to make a standalone copy.
1382 nexthop_copy_no_recurse(&lookup
, nh
, NULL
);
1384 nhe
= zebra_nhg_find_nexthop(0, &lookup
, afi
, type
, from_dplane
);
1386 /* The copy may have allocated labels; free them if necessary. */
1387 nexthop_del_labels(&lookup
);
1388 nexthop_del_srv6_seg6local(&lookup
);
1389 nexthop_del_srv6_seg6(&lookup
);
1391 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1392 zlog_debug("%s: nh %pNHv => %p (%u)",
1393 __func__
, nh
, nhe
, nhe
? nhe
->id
: 0);
1398 static struct nhg_hash_entry
*depends_find(const struct nexthop
*nh
, afi_t afi
,
1399 int type
, bool from_dplane
)
1401 struct nhg_hash_entry
*nhe
= NULL
;
1406 /* We are separating these functions out to increase handling speed
1407 * in the non-recursive case (by not alloc/freeing)
1409 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1410 nhe
= depends_find_recursive(nh
, afi
, type
);
1412 nhe
= depends_find_singleton(nh
, afi
, type
, from_dplane
);
1415 if (IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1416 zlog_debug("%s: nh %pNHv %s => %p (%u)", __func__
, nh
,
1417 CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
) ? "(R)"
1419 nhe
, nhe
? nhe
->id
: 0);
1426 static void depends_add(struct nhg_connected_tree_head
*head
,
1427 struct nhg_hash_entry
*depend
)
1429 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1430 zlog_debug("%s: head %p nh %pNHv",
1431 __func__
, head
, depend
->nhg
.nexthop
);
1433 /* If NULL is returned, it was successfully added and
1434 * needs to have its refcnt incremented.
1436 * Else the NHE is already present in the tree and doesn't
1437 * need to increment the refcnt.
1439 if (nhg_connected_tree_add_nhe(head
, depend
) == NULL
)
1440 zebra_nhg_increment_ref(depend
);
1443 static struct nhg_hash_entry
*
1444 depends_find_add(struct nhg_connected_tree_head
*head
, struct nexthop
*nh
,
1445 afi_t afi
, int type
, bool from_dplane
)
1447 struct nhg_hash_entry
*depend
= NULL
;
1449 depend
= depends_find(nh
, afi
, type
, from_dplane
);
1451 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1452 zlog_debug("%s: nh %pNHv => %p",
1453 __func__
, nh
, depend
);
1456 depends_add(head
, depend
);
1461 static struct nhg_hash_entry
*
1462 depends_find_id_add(struct nhg_connected_tree_head
*head
, uint32_t id
)
1464 struct nhg_hash_entry
*depend
= NULL
;
1466 depend
= zebra_nhg_lookup_id(id
);
1469 depends_add(head
, depend
);
1474 static void depends_decrement_free(struct nhg_connected_tree_head
*head
)
1476 nhg_connected_tree_decrement_ref(head
);
1477 nhg_connected_tree_free(head
);
1480 /* Find an nhe based on a list of nexthops */
1481 struct nhg_hash_entry
*zebra_nhg_rib_find(uint32_t id
,
1482 struct nexthop_group
*nhg
,
1483 afi_t rt_afi
, int type
)
1485 struct nhg_hash_entry
*nhe
= NULL
;
1489 * CLANG SA is complaining that nexthop may be NULL
1490 * Make it happy but this is ridonc
1492 assert(nhg
->nexthop
);
1493 vrf_id
= !vrf_is_backend_netns() ? VRF_DEFAULT
: nhg
->nexthop
->vrf_id
;
1495 zebra_nhg_find(&nhe
, id
, nhg
, NULL
, vrf_id
, rt_afi
, type
, false);
1497 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1498 zlog_debug("%s: => nhe %p (%u)",
1499 __func__
, nhe
, nhe
? nhe
->id
: 0);
1504 /* Find an nhe based on a route's nhe */
1505 struct nhg_hash_entry
*
1506 zebra_nhg_rib_find_nhe(struct nhg_hash_entry
*rt_nhe
, afi_t rt_afi
)
1508 struct nhg_hash_entry
*nhe
= NULL
;
1510 if (!(rt_nhe
&& rt_nhe
->nhg
.nexthop
)) {
1511 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
1512 "No nexthop passed to %s", __func__
);
1516 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1517 zlog_debug("%s: rt_nhe %p (%u)", __func__
, rt_nhe
, rt_nhe
->id
);
1519 zebra_nhe_find(&nhe
, rt_nhe
, NULL
, rt_afi
, false);
1521 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1522 zlog_debug("%s: => nhe %p (%u)",
1523 __func__
, nhe
, nhe
? nhe
->id
: 0);
1529 * Allocate backup nexthop info object. Typically these are embedded in
1530 * nhg_hash_entry objects.
1532 struct nhg_backup_info
*zebra_nhg_backup_alloc(void)
1534 struct nhg_backup_info
*p
;
1536 p
= XCALLOC(MTYPE_NHG
, sizeof(struct nhg_backup_info
));
1538 p
->nhe
= zebra_nhg_alloc();
1540 /* Identify the embedded group used to hold the list of backups */
1541 SET_FLAG(p
->nhe
->flags
, NEXTHOP_GROUP_BACKUP
);
1547 * Free backup nexthop info object, deal with any embedded allocations
1549 void zebra_nhg_backup_free(struct nhg_backup_info
**p
)
1553 zebra_nhg_free((*p
)->nhe
);
1555 XFREE(MTYPE_NHG
, (*p
));
1559 /* Accessor for backup nexthop group */
1560 struct nexthop_group
*zebra_nhg_get_backup_nhg(struct nhg_hash_entry
*nhe
)
1562 struct nexthop_group
*p
= NULL
;
1565 if (nhe
->backup_info
&& nhe
->backup_info
->nhe
)
1566 p
= &(nhe
->backup_info
->nhe
->nhg
);
1573 * Helper to return a copy of a backup_info - note that this is a shallow
1574 * copy, meant to be used when creating a new nhe from info passed in with
1577 static struct nhg_backup_info
*
1578 nhg_backup_copy(const struct nhg_backup_info
*orig
)
1580 struct nhg_backup_info
*b
;
1582 b
= zebra_nhg_backup_alloc();
1584 /* Copy list of nexthops */
1585 nexthop_group_copy(&(b
->nhe
->nhg
), &(orig
->nhe
->nhg
));
1590 static void zebra_nhg_free_members(struct nhg_hash_entry
*nhe
)
1592 nexthops_free(nhe
->nhg
.nexthop
);
1594 zebra_nhg_backup_free(&nhe
->backup_info
);
1596 /* Decrement to remove connection ref */
1597 nhg_connected_tree_decrement_ref(&nhe
->nhg_depends
);
1598 nhg_connected_tree_free(&nhe
->nhg_depends
);
1599 nhg_connected_tree_free(&nhe
->nhg_dependents
);
1602 void zebra_nhg_free(struct nhg_hash_entry
*nhe
)
1604 if (IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1605 /* Group or singleton? */
1606 if (nhe
->nhg
.nexthop
&& nhe
->nhg
.nexthop
->next
)
1607 zlog_debug("%s: nhe %p (%u), refcnt %d",
1608 __func__
, nhe
, nhe
->id
, nhe
->refcnt
);
1610 zlog_debug("%s: nhe %p (%u), refcnt %d, NH %pNHv",
1611 __func__
, nhe
, nhe
->id
, nhe
->refcnt
,
1616 zlog_debug("nhe_id=%u hash refcnt=%d", nhe
->id
, nhe
->refcnt
);
1618 zebra_nhg_free_members(nhe
);
1620 XFREE(MTYPE_NHG
, nhe
);
1623 void zebra_nhg_hash_free(void *p
)
1625 zebra_nhg_release_all_deps((struct nhg_hash_entry
*)p
);
1626 zebra_nhg_free((struct nhg_hash_entry
*)p
);
1629 void zebra_nhg_decrement_ref(struct nhg_hash_entry
*nhe
)
1631 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1632 zlog_debug("%s: nhe %p (%u) %d => %d",
1633 __func__
, nhe
, nhe
->id
, nhe
->refcnt
,
1638 if (!zebra_nhg_depends_is_empty(nhe
))
1639 nhg_connected_tree_decrement_ref(&nhe
->nhg_depends
);
1641 if (ZEBRA_NHG_CREATED(nhe
) && nhe
->refcnt
<= 0)
1642 zebra_nhg_uninstall_kernel(nhe
);
1645 void zebra_nhg_increment_ref(struct nhg_hash_entry
*nhe
)
1647 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1648 zlog_debug("%s: nhe %p (%u) %d => %d",
1649 __func__
, nhe
, nhe
->id
, nhe
->refcnt
,
1654 if (!zebra_nhg_depends_is_empty(nhe
))
1655 nhg_connected_tree_increment_ref(&nhe
->nhg_depends
);
1658 static struct nexthop
*nexthop_set_resolved(afi_t afi
,
1659 const struct nexthop
*newhop
,
1660 struct nexthop
*nexthop
,
1661 struct zebra_sr_policy
*policy
)
1663 struct nexthop
*resolved_hop
;
1664 uint8_t num_labels
= 0;
1665 mpls_label_t labels
[MPLS_MAX_LABELS
];
1666 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1669 resolved_hop
= nexthop_new();
1670 SET_FLAG(resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1672 resolved_hop
->vrf_id
= nexthop
->vrf_id
;
1673 switch (newhop
->type
) {
1674 case NEXTHOP_TYPE_IPV4
:
1675 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1676 /* If the resolving route specifies a gateway, use it */
1677 resolved_hop
->type
= newhop
->type
;
1678 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
1680 if (newhop
->ifindex
) {
1681 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1682 resolved_hop
->ifindex
= newhop
->ifindex
;
1685 case NEXTHOP_TYPE_IPV6
:
1686 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1687 resolved_hop
->type
= newhop
->type
;
1688 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
1690 if (newhop
->ifindex
) {
1691 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1692 resolved_hop
->ifindex
= newhop
->ifindex
;
1695 case NEXTHOP_TYPE_IFINDEX
:
1696 /* If the resolving route is an interface route,
1697 * it means the gateway we are looking up is connected
1698 * to that interface. (The actual network is _not_ onlink).
1699 * Therefore, the resolved route should have the original
1700 * gateway as nexthop as it is directly connected.
1702 * On Linux, we have to set the onlink netlink flag because
1703 * otherwise, the kernel won't accept the route.
1705 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
1706 if (afi
== AFI_IP
) {
1707 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1708 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
1709 } else if (afi
== AFI_IP6
) {
1710 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1711 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
1713 resolved_hop
->ifindex
= newhop
->ifindex
;
1715 case NEXTHOP_TYPE_BLACKHOLE
:
1716 resolved_hop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
1717 resolved_hop
->bh_type
= newhop
->bh_type
;
1721 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
1722 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
1724 /* Copy labels of the resolved route and the parent resolving to it */
1729 * Don't push the first SID if the corresponding action in the
1732 if (!newhop
->nh_label
|| !newhop
->nh_label
->num_labels
1733 || newhop
->nh_label
->label
[0] == MPLS_LABEL_IMPLICIT_NULL
)
1736 for (; i
< policy
->segment_list
.label_num
; i
++)
1737 labels
[num_labels
++] = policy
->segment_list
.labels
[i
];
1738 label_type
= policy
->segment_list
.type
;
1739 } else if (newhop
->nh_label
) {
1740 for (i
= 0; i
< newhop
->nh_label
->num_labels
; i
++) {
1741 /* Be a bit picky about overrunning the local array */
1742 if (num_labels
>= MPLS_MAX_LABELS
) {
1743 if (IS_ZEBRA_DEBUG_NHG
|| IS_ZEBRA_DEBUG_RIB
)
1744 zlog_debug("%s: too many labels in newhop %pNHv",
1748 labels
[num_labels
++] = newhop
->nh_label
->label
[i
];
1750 /* Use the "outer" type */
1751 label_type
= newhop
->nh_label_type
;
1754 if (nexthop
->nh_label
) {
1755 for (i
= 0; i
< nexthop
->nh_label
->num_labels
; i
++) {
1756 /* Be a bit picky about overrunning the local array */
1757 if (num_labels
>= MPLS_MAX_LABELS
) {
1758 if (IS_ZEBRA_DEBUG_NHG
|| IS_ZEBRA_DEBUG_RIB
)
1759 zlog_debug("%s: too many labels in nexthop %pNHv",
1763 labels
[num_labels
++] = nexthop
->nh_label
->label
[i
];
1766 /* If the parent has labels, use its type if
1767 * we don't already have one.
1769 if (label_type
== ZEBRA_LSP_NONE
)
1770 label_type
= nexthop
->nh_label_type
;
1774 nexthop_add_labels(resolved_hop
, label_type
, num_labels
,
1777 if (nexthop
->nh_srv6
) {
1778 nexthop_add_srv6_seg6local(resolved_hop
,
1779 nexthop
->nh_srv6
->seg6local_action
,
1780 &nexthop
->nh_srv6
->seg6local_ctx
);
1781 nexthop_add_srv6_seg6(resolved_hop
,
1782 &nexthop
->nh_srv6
->seg6_segs
);
1785 resolved_hop
->rparent
= nexthop
;
1786 _nexthop_add(&nexthop
->resolved
, resolved_hop
);
1788 return resolved_hop
;
1791 /* Checks if nexthop we are trying to resolve to is valid */
1792 static bool nexthop_valid_resolve(const struct nexthop
*nexthop
,
1793 const struct nexthop
*resolved
)
1795 /* Can't resolve to a recursive nexthop */
1796 if (CHECK_FLAG(resolved
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1799 /* Must be ACTIVE */
1800 if (!CHECK_FLAG(resolved
->flags
, NEXTHOP_FLAG_ACTIVE
))
1803 /* Must not be duplicate */
1804 if (CHECK_FLAG(resolved
->flags
, NEXTHOP_FLAG_DUPLICATE
))
1807 switch (nexthop
->type
) {
1808 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1809 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1810 /* If the nexthop we are resolving to does not match the
1811 * ifindex for the nexthop the route wanted, its not valid.
1813 if (nexthop
->ifindex
!= resolved
->ifindex
)
1816 case NEXTHOP_TYPE_IPV4
:
1817 case NEXTHOP_TYPE_IPV6
:
1818 case NEXTHOP_TYPE_IFINDEX
:
1819 case NEXTHOP_TYPE_BLACKHOLE
:
1827 * When resolving a recursive nexthop, capture backup nexthop(s) also
1828 * so they can be conveyed through the dataplane to the FIB. We'll look
1829 * at the backups in the resolving nh 'nexthop' and its nhe, and copy them
1830 * into the route's resolved nh 'resolved' and its nhe 'nhe'.
1832 static int resolve_backup_nexthops(const struct nexthop
*nexthop
,
1833 const struct nhg_hash_entry
*nhe
,
1834 struct nexthop
*resolved
,
1835 struct nhg_hash_entry
*resolve_nhe
,
1836 struct backup_nh_map_s
*map
)
1839 const struct nexthop
*bnh
;
1840 struct nexthop
*nh
, *newnh
;
1841 mpls_label_t labels
[MPLS_MAX_LABELS
];
1844 assert(nexthop
->backup_num
<= NEXTHOP_MAX_BACKUPS
);
1846 /* Locate backups from the original nexthop's backup index and nhe */
1847 for (i
= 0; i
< nexthop
->backup_num
; i
++) {
1848 idx
= nexthop
->backup_idx
[i
];
1850 /* Do we already know about this particular backup? */
1851 for (j
= 0; j
< map
->map_count
; j
++) {
1852 if (map
->map
[j
].orig_idx
== idx
)
1856 if (j
< map
->map_count
) {
1857 resolved
->backup_idx
[resolved
->backup_num
] =
1858 map
->map
[j
].new_idx
;
1859 resolved
->backup_num
++;
1861 SET_FLAG(resolved
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1863 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1864 zlog_debug("%s: found map idx orig %d, new %d",
1865 __func__
, map
->map
[j
].orig_idx
,
1866 map
->map
[j
].new_idx
);
1871 /* We can't handle any new map entries at this point. */
1872 if (map
->map_count
== MULTIPATH_NUM
)
1875 /* Need to create/copy a new backup */
1876 bnh
= nhe
->backup_info
->nhe
->nhg
.nexthop
;
1877 for (j
= 0; j
< idx
; j
++) {
1883 /* Whoops - bad index in the nexthop? */
1887 if (resolve_nhe
->backup_info
== NULL
)
1888 resolve_nhe
->backup_info
= zebra_nhg_backup_alloc();
1890 /* Update backup info in the resolving nexthop and its nhe */
1891 newnh
= nexthop_dup_no_recurse(bnh
, NULL
);
1893 /* We may need some special handling for mpls labels: the new
1894 * backup needs to carry the recursive nexthop's labels,
1895 * if any: they may be vrf labels e.g.
1896 * The original/inner labels are in the stack of 'resolve_nhe',
1897 * if that is longer than the stack in 'nexthop'.
1899 if (newnh
->nh_label
&& resolved
->nh_label
&&
1900 nexthop
->nh_label
) {
1901 if (resolved
->nh_label
->num_labels
>
1902 nexthop
->nh_label
->num_labels
) {
1903 /* Prepare new label stack */
1905 for (j
= 0; j
< newnh
->nh_label
->num_labels
;
1907 labels
[j
] = newnh
->nh_label
->label
[j
];
1911 /* Include inner labels */
1912 for (j
= nexthop
->nh_label
->num_labels
;
1913 j
< resolved
->nh_label
->num_labels
;
1915 labels
[num_labels
] =
1916 resolved
->nh_label
->label
[j
];
1920 /* Replace existing label stack in the backup */
1921 nexthop_del_labels(newnh
);
1922 nexthop_add_labels(newnh
, bnh
->nh_label_type
,
1923 num_labels
, labels
);
1927 /* Need to compute the new backup index in the new
1928 * backup list, and add to map struct.
1931 nh
= resolve_nhe
->backup_info
->nhe
->nhg
.nexthop
;
1941 } else /* First one */
1942 resolve_nhe
->backup_info
->nhe
->nhg
.nexthop
= newnh
;
1945 resolved
->backup_idx
[resolved
->backup_num
] = j
;
1946 resolved
->backup_num
++;
1948 SET_FLAG(resolved
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1950 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1951 zlog_debug("%s: added idx orig %d, new %d",
1954 /* Update map/cache */
1955 map
->map
[map
->map_count
].orig_idx
= idx
;
1956 map
->map
[map
->map_count
].new_idx
= j
;
1964 * So this nexthop resolution has decided that a connected route
1965 * is the correct choice. At this point in time if FRR has multiple
1966 * connected routes that all point to the same prefix one will be
1967 * selected, *but* the particular interface may not be the one
1968 * that the nexthop points at. Let's look at all the available
1969 * connected routes on this node and if any of them auto match
1970 * the routes nexthops ifindex that is good enough for a match
1972 * This code is depending on the fact that a nexthop->ifindex is 0
1973 * if it is not known, if this assumption changes, yummy!
1974 * Additionally a ifindx of 0 means figure it out for us.
1976 static struct route_entry
*
1977 zebra_nhg_connected_ifindex(struct route_node
*rn
, struct route_entry
*match
,
1978 int32_t curr_ifindex
)
1980 struct nexthop
*newhop
= match
->nhe
->nhg
.nexthop
;
1981 struct route_entry
*re
;
1983 assert(newhop
); /* What a kick in the patooey */
1985 if (curr_ifindex
== 0)
1988 if (curr_ifindex
== newhop
->ifindex
)
1992 * At this point we know that this route is matching a connected
1993 * but there are possibly a bunch of connected routes that are
1994 * alive that should be considered as well. So let's iterate over
1995 * all the re's and see if they are connected as well and maybe one
1996 * of those ifindexes match as well.
1998 RNODE_FOREACH_RE (rn
, re
) {
1999 if (re
->type
!= ZEBRA_ROUTE_CONNECT
)
2002 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2006 * zebra has a connected route that is not removed
2007 * let's test if it is good
2009 newhop
= re
->nhe
->nhg
.nexthop
;
2011 if (curr_ifindex
== newhop
->ifindex
)
2019 * Given a nexthop we need to properly recursively resolve,
2020 * do a table lookup to find and match if at all possible.
2021 * Set the nexthop->ifindex and resolution info as appropriate.
2023 static int nexthop_active(struct nexthop
*nexthop
, struct nhg_hash_entry
*nhe
,
2024 const struct prefix
*top
, int type
, uint32_t flags
,
2025 uint32_t *pmtu
, vrf_id_t vrf_id
)
2028 struct route_table
*table
;
2029 struct route_node
*rn
;
2030 struct route_entry
*match
= NULL
;
2032 struct zebra_nhlfe
*nhlfe
;
2033 struct nexthop
*newhop
;
2034 struct interface
*ifp
;
2036 struct zebra_vrf
*zvrf
;
2037 struct in_addr local_ipv4
;
2038 struct in_addr
*ipv4
;
2041 /* Reset some nexthop attributes that we'll recompute if necessary */
2042 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
)
2043 || (nexthop
->type
== NEXTHOP_TYPE_IPV6
))
2044 nexthop
->ifindex
= 0;
2046 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
2047 nexthops_free(nexthop
->resolved
);
2048 nexthop
->resolved
= NULL
;
2051 * Set afi based on nexthop type.
2052 * Some nexthop types get special handling, possibly skipping
2053 * the normal processing.
2055 switch (nexthop
->type
) {
2056 case NEXTHOP_TYPE_IFINDEX
:
2058 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
2060 * If the interface exists and its operative or its a kernel
2061 * route and interface is up, its active. We trust kernel routes
2065 && (if_is_operative(ifp
)
2067 && (type
== ZEBRA_ROUTE_KERNEL
2068 || type
== ZEBRA_ROUTE_SYSTEM
))))
2074 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2077 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
2078 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2080 if (ifp
&& if_is_operative(ifp
))
2087 case NEXTHOP_TYPE_IPV4
:
2088 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2091 case NEXTHOP_TYPE_IPV6
:
2095 case NEXTHOP_TYPE_BLACKHOLE
:
2100 * If the nexthop has been marked as 'onlink' we just need to make
2101 * sure the nexthop's interface is known and is operational.
2103 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)) {
2104 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
2106 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2107 zlog_debug("nexthop %pNHv marked onlink but nhif %u doesn't exist",
2108 nexthop
, nexthop
->ifindex
);
2111 if (!if_is_operative(ifp
)) {
2112 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2113 zlog_debug("nexthop %pNHv marked onlink but nhif %s is not operational",
2114 nexthop
, ifp
->name
);
2121 ((top
->family
== AF_INET
&& top
->prefixlen
== IPV4_MAX_BITLEN
&&
2122 nexthop
->gate
.ipv4
.s_addr
== top
->u
.prefix4
.s_addr
) ||
2123 (top
->family
== AF_INET6
&& top
->prefixlen
== IPV6_MAX_BITLEN
&&
2124 memcmp(&nexthop
->gate
.ipv6
, &top
->u
.prefix6
, IPV6_MAX_BYTELEN
) ==
2126 nexthop
->vrf_id
== vrf_id
) {
2127 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2129 " :%s: Attempting to install a max prefixlength route through itself",
2134 /* Validation for ipv4 mapped ipv6 nexthop. */
2135 if (IS_MAPPED_IPV6(&nexthop
->gate
.ipv6
)) {
2138 ipv4_mapped_ipv6_to_ipv4(&nexthop
->gate
.ipv6
, ipv4
);
2140 ipv4
= &nexthop
->gate
.ipv4
;
2143 /* Processing for nexthops with SR 'color' attribute, using
2144 * the corresponding SR policy object.
2146 if (nexthop
->srte_color
) {
2147 struct ipaddr endpoint
= {0};
2148 struct zebra_sr_policy
*policy
;
2152 endpoint
.ipa_type
= IPADDR_V4
;
2153 endpoint
.ipaddr_v4
= *ipv4
;
2156 endpoint
.ipa_type
= IPADDR_V6
;
2157 endpoint
.ipaddr_v6
= nexthop
->gate
.ipv6
;
2160 flog_err(EC_LIB_DEVELOPMENT
,
2161 "%s: unknown address-family: %u", __func__
,
2166 policy
= zebra_sr_policy_find(nexthop
->srte_color
, &endpoint
);
2167 if (policy
&& policy
->status
== ZEBRA_SR_POLICY_UP
) {
2169 frr_each_safe (nhlfe_list
, &policy
->lsp
->nhlfe_list
,
2171 if (!CHECK_FLAG(nhlfe
->flags
,
2172 NHLFE_FLAG_SELECTED
)
2173 || CHECK_FLAG(nhlfe
->flags
,
2174 NHLFE_FLAG_DELETED
))
2176 SET_FLAG(nexthop
->flags
,
2177 NEXTHOP_FLAG_RECURSIVE
);
2178 nexthop_set_resolved(afi
, nhlfe
->nexthop
,
2187 /* Make lookup prefix. */
2188 memset(&p
, 0, sizeof(struct prefix
));
2192 p
.prefixlen
= IPV4_MAX_BITLEN
;
2193 p
.u
.prefix4
= *ipv4
;
2196 p
.family
= AF_INET6
;
2197 p
.prefixlen
= IPV6_MAX_BITLEN
;
2198 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
2201 assert(afi
!= AFI_IP
&& afi
!= AFI_IP6
);
2205 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, nexthop
->vrf_id
);
2207 zvrf
= zebra_vrf_lookup_by_id(nexthop
->vrf_id
);
2208 if (!table
|| !zvrf
) {
2209 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2210 zlog_debug(" %s: Table not found", __func__
);
2214 rn
= route_node_match(table
, (struct prefix
*)&p
);
2216 route_unlock_node(rn
);
2218 /* Lookup should halt if we've matched against ourselves ('top',
2219 * if specified) - i.e., we cannot have a nexthop NH1 is
2220 * resolved by a route NH1. The exception is if the route is a
2223 if (prefix_same(&rn
->p
, top
))
2224 if (((afi
== AFI_IP
)
2225 && (rn
->p
.prefixlen
!= IPV4_MAX_BITLEN
))
2226 || ((afi
== AFI_IP6
)
2227 && (rn
->p
.prefixlen
!= IPV6_MAX_BITLEN
))) {
2228 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2230 " %s: Matched against ourself and prefix length is not max bit length",
2235 /* Pick up selected route. */
2236 /* However, do not resolve over default route unless explicitly
2239 if (is_default_prefix(&rn
->p
)
2240 && !rnh_resolve_via_default(zvrf
, p
.family
)) {
2241 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2243 " :%s: Resolved against default route",
2248 dest
= rib_dest_from_rnode(rn
);
2249 if (dest
&& dest
->selected_fib
2250 && !CHECK_FLAG(dest
->selected_fib
->status
,
2251 ROUTE_ENTRY_REMOVED
)
2252 && dest
->selected_fib
->type
!= ZEBRA_ROUTE_TABLE
)
2253 match
= dest
->selected_fib
;
2255 /* If there is no selected route or matched route is EGP, go up
2261 } while (rn
&& rn
->info
== NULL
);
2263 route_lock_node(rn
);
2268 if ((match
->type
== ZEBRA_ROUTE_CONNECT
) ||
2269 (RIB_SYSTEM_ROUTE(match
) && RSYSTEM_ROUTE(type
))) {
2270 match
= zebra_nhg_connected_ifindex(rn
, match
,
2273 newhop
= match
->nhe
->nhg
.nexthop
;
2274 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
||
2275 nexthop
->type
== NEXTHOP_TYPE_IPV6
)
2276 nexthop
->ifindex
= newhop
->ifindex
;
2277 else if (nexthop
->ifindex
!= newhop
->ifindex
) {
2278 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2280 "%s: %pNHv given ifindex does not match nexthops ifindex found: %pNHv",
2281 __func__
, nexthop
, newhop
);
2283 * NEXTHOP_TYPE_*_IFINDEX but ifindex
2284 * doesn't match what we found.
2289 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2290 zlog_debug("%s: CONNECT match %p (%u), newhop %pNHv",
2292 match
->nhe
->id
, newhop
);
2295 } else if (CHECK_FLAG(flags
, ZEBRA_FLAG_ALLOW_RECURSION
)) {
2296 struct nexthop_group
*nhg
;
2297 struct nexthop
*resolver
;
2298 struct backup_nh_map_s map
= {};
2302 /* Only useful if installed */
2303 if (!CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
)) {
2304 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2305 zlog_debug("%s: match %p (%u) not installed",
2309 goto done_with_match
;
2312 /* Examine installed nexthops; note that there
2313 * may not be any installed primary nexthops if
2314 * only backups are installed.
2316 nhg
= rib_get_fib_nhg(match
);
2317 for (ALL_NEXTHOPS_PTR(nhg
, newhop
)) {
2318 if (!nexthop_valid_resolve(nexthop
, newhop
))
2321 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2322 zlog_debug("%s: RECURSIVE match %p (%u), newhop %pNHv",
2324 match
->nhe
->id
, newhop
);
2326 SET_FLAG(nexthop
->flags
,
2327 NEXTHOP_FLAG_RECURSIVE
);
2328 resolver
= nexthop_set_resolved(afi
, newhop
,
2332 /* If there are backup nexthops, capture
2333 * that info with the resolving nexthop.
2335 if (resolver
&& newhop
->backup_num
> 0) {
2336 resolve_backup_nexthops(newhop
,
2343 /* Examine installed backup nexthops, if any. There
2344 * are only installed backups *if* there is a
2345 * dedicated fib list. The UI can also control use
2346 * of backups for resolution.
2348 nhg
= rib_get_fib_backup_nhg(match
);
2349 if (!use_recursive_backups
||
2350 nhg
== NULL
|| nhg
->nexthop
== NULL
)
2351 goto done_with_match
;
2353 for (ALL_NEXTHOPS_PTR(nhg
, newhop
)) {
2354 if (!nexthop_valid_resolve(nexthop
, newhop
))
2357 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2358 zlog_debug("%s: RECURSIVE match backup %p (%u), newhop %pNHv",
2360 match
->nhe
->id
, newhop
);
2362 SET_FLAG(nexthop
->flags
,
2363 NEXTHOP_FLAG_RECURSIVE
);
2364 nexthop_set_resolved(afi
, newhop
, nexthop
,
2370 /* Capture resolving mtu */
2375 } else if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2377 " %s: Recursion failed to find",
2382 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2384 " %s: Route Type %s has not turned on recursion",
2385 __func__
, zebra_route_string(type
));
2386 if (type
== ZEBRA_ROUTE_BGP
2387 && !CHECK_FLAG(flags
, ZEBRA_FLAG_IBGP
))
2389 " EBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\"");
2394 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2395 zlog_debug(" %s: Nexthop did not lookup in table",
2400 /* This function verifies reachability of one given nexthop, which can be
2401 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
2402 * in nexthop->flags field. The nexthop->ifindex will be updated
2403 * appropriately as well.
2405 * An existing route map can turn an otherwise active nexthop into inactive,
2406 * but not vice versa.
2408 * The return value is the final value of 'ACTIVE' flag.
2410 static unsigned nexthop_active_check(struct route_node
*rn
,
2411 struct route_entry
*re
,
2412 struct nexthop
*nexthop
,
2413 struct nhg_hash_entry
*nhe
)
2415 route_map_result_t ret
= RMAP_PERMITMATCH
;
2417 const struct prefix
*p
, *src_p
;
2418 struct zebra_vrf
*zvrf
;
2422 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
2424 if (rn
->p
.family
== AF_INET
)
2426 else if (rn
->p
.family
== AF_INET6
)
2431 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2432 zlog_debug("%s: re %p, nexthop %pNHv", __func__
, re
, nexthop
);
2435 * If the kernel has sent us a NEW route, then
2436 * by golly gee whiz it's a good route.
2438 * If its an already INSTALLED route we have already handled, then the
2439 * kernel route's nexthop might have became unreachable
2440 * and we have to handle that.
2442 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
) &&
2443 (re
->type
== ZEBRA_ROUTE_KERNEL
||
2444 re
->type
== ZEBRA_ROUTE_SYSTEM
)) {
2445 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2450 vrf_id
= zvrf_id(rib_dest_vrf(rib_dest_from_rnode(rn
)));
2451 switch (nexthop
->type
) {
2452 case NEXTHOP_TYPE_IFINDEX
:
2453 if (nexthop_active(nexthop
, nhe
, &rn
->p
, re
->type
, re
->flags
,
2455 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2457 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2459 case NEXTHOP_TYPE_IPV4
:
2460 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2462 if (nexthop_active(nexthop
, nhe
, &rn
->p
, re
->type
, re
->flags
,
2464 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2466 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2468 case NEXTHOP_TYPE_IPV6
:
2470 if (nexthop_active(nexthop
, nhe
, &rn
->p
, re
->type
, re
->flags
,
2472 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2474 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2476 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2477 /* RFC 5549, v4 prefix with v6 NH */
2478 if (rn
->p
.family
!= AF_INET
)
2481 if (nexthop_active(nexthop
, nhe
, &rn
->p
, re
->type
, re
->flags
,
2483 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2485 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2487 case NEXTHOP_TYPE_BLACKHOLE
:
2488 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2496 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
2497 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2498 zlog_debug(" %s: Unable to find active nexthop",
2503 /* Capture recursive nexthop mtu.
2504 * TODO -- the code used to just reset the re's value to zero
2505 * for each nexthop, and then jam any resolving route's mtu value in,
2506 * whether or not that was zero, or lt/gt any existing value? The
2507 * way this is used appears to be as a floor value, so let's try
2508 * using it that way here.
2511 if (re
->nexthop_mtu
== 0 || re
->nexthop_mtu
> mtu
)
2512 re
->nexthop_mtu
= mtu
;
2515 /* XXX: What exactly do those checks do? Do we support
2516 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
2518 if (RIB_SYSTEM_ROUTE(re
) || (family
== AFI_IP
&& p
->family
!= AF_INET
)
2519 || (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
2520 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2522 /* The original code didn't determine the family correctly
2523 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
2524 * from the rib_table_info in those cases.
2525 * Possibly it may be better to use only the rib_table_info
2529 struct rib_table_info
*info
;
2531 info
= srcdest_rnode_table_info(rn
);
2535 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
2537 zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2539 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2540 zlog_debug(" %s: zvrf is NULL", __func__
);
2541 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2544 /* It'll get set if required inside */
2545 ret
= zebra_route_map_check(family
, re
->type
, re
->instance
, p
, nexthop
,
2547 if (ret
== RMAP_DENYMATCH
) {
2548 if (IS_ZEBRA_DEBUG_RIB
) {
2550 "%u:%pRN: Filtering out with NH out %s due to route map",
2552 ifindex2ifname(nexthop
->ifindex
,
2555 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2557 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2560 /* Helper function called after resolution to walk nhg rb trees
2561 * and toggle the NEXTHOP_GROUP_VALID flag if the nexthop
2562 * is active on singleton NHEs.
2564 static bool zebra_nhg_set_valid_if_active(struct nhg_hash_entry
*nhe
)
2566 struct nhg_connected
*rb_node_dep
= NULL
;
2569 if (!zebra_nhg_depends_is_empty(nhe
)) {
2570 /* Is at least one depend valid? */
2571 frr_each(nhg_connected_tree
, &nhe
->nhg_depends
, rb_node_dep
) {
2572 if (zebra_nhg_set_valid_if_active(rb_node_dep
->nhe
))
2579 /* should be fully resolved singleton at this point */
2580 if (CHECK_FLAG(nhe
->nhg
.nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2585 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
2591 * Process a list of nexthops, given an nhe, determining
2592 * whether each one is ACTIVE/installable at this time.
2594 static uint32_t nexthop_list_active_update(struct route_node
*rn
,
2595 struct route_entry
*re
,
2596 struct nhg_hash_entry
*nhe
,
2599 union g_addr prev_src
;
2600 unsigned int prev_active
, new_active
;
2601 ifindex_t prev_index
;
2602 uint32_t counter
= 0;
2603 struct nexthop
*nexthop
;
2604 struct nexthop_group
*nhg
= &nhe
->nhg
;
2606 nexthop
= nhg
->nexthop
;
2608 /* Init recursive nh mtu */
2609 re
->nexthop_mtu
= 0;
2611 /* Process nexthops one-by-one */
2612 for ( ; nexthop
; nexthop
= nexthop
->next
) {
2614 /* No protocol daemon provides src and so we're skipping
2617 prev_src
= nexthop
->rmap_src
;
2618 prev_active
= CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2619 prev_index
= nexthop
->ifindex
;
2621 /* Include the containing nhe for primary nexthops: if there's
2622 * recursive resolution, we capture the backup info also.
2625 nexthop_active_check(rn
, re
, nexthop
,
2626 (is_backup
? NULL
: nhe
));
2629 * We need to respect the multipath_num here
2630 * as that what we should be able to install from
2631 * a multipath perspective should not be a data plane
2634 if (new_active
&& counter
>= zrouter
.multipath_num
) {
2637 /* Set it and its resolved nexthop as inactive. */
2638 for (nh
= nexthop
; nh
; nh
= nh
->resolved
)
2639 UNSET_FLAG(nh
->flags
, NEXTHOP_FLAG_ACTIVE
);
2647 /* Check for changes to the nexthop - set ROUTE_ENTRY_CHANGED */
2648 if (prev_active
!= new_active
|| prev_index
!= nexthop
->ifindex
2649 || ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
2650 && nexthop
->type
< NEXTHOP_TYPE_IPV6
)
2651 && prev_src
.ipv4
.s_addr
2652 != nexthop
->rmap_src
.ipv4
.s_addr
)
2653 || ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
2654 && nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
)
2655 && !(IPV6_ADDR_SAME(&prev_src
.ipv6
,
2656 &nexthop
->rmap_src
.ipv6
)))
2657 || CHECK_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
))
2658 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2665 static uint32_t proto_nhg_nexthop_active_update(struct nexthop_group
*nhg
)
2668 uint32_t curr_active
= 0;
2670 /* Assume all active for now */
2672 for (nh
= nhg
->nexthop
; nh
; nh
= nh
->next
) {
2673 SET_FLAG(nh
->flags
, NEXTHOP_FLAG_ACTIVE
);
2681 * Iterate over all nexthops of the given RIB entry and refresh their
2682 * ACTIVE flag. If any nexthop is found to toggle the ACTIVE flag,
2683 * the whole re structure is flagged with ROUTE_ENTRY_CHANGED.
2685 * Return value is the new number of active nexthops.
2687 int nexthop_active_update(struct route_node
*rn
, struct route_entry
*re
)
2689 struct nhg_hash_entry
*curr_nhe
;
2690 uint32_t curr_active
= 0, backup_active
= 0;
2692 if (PROTO_OWNED(re
->nhe
))
2693 return proto_nhg_nexthop_active_update(&re
->nhe
->nhg
);
2695 afi_t rt_afi
= family2afi(rn
->p
.family
);
2697 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2699 /* Make a local copy of the existing nhe, so we don't work on/modify
2702 curr_nhe
= zebra_nhe_copy(re
->nhe
, re
->nhe
->id
);
2704 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2705 zlog_debug("%s: re %p nhe %p (%u), curr_nhe %p",
2706 __func__
, re
, re
->nhe
, re
->nhe
->id
,
2709 /* Clear the existing id, if any: this will avoid any confusion
2710 * if the id exists, and will also force the creation
2711 * of a new nhe reflecting the changes we may make in this local copy.
2715 /* Process nexthops */
2716 curr_active
= nexthop_list_active_update(rn
, re
, curr_nhe
, false);
2718 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2719 zlog_debug("%s: re %p curr_active %u", __func__
, re
,
2722 /* If there are no backup nexthops, we are done */
2723 if (zebra_nhg_get_backup_nhg(curr_nhe
) == NULL
)
2726 backup_active
= nexthop_list_active_update(
2727 rn
, re
, curr_nhe
->backup_info
->nhe
, true /*is_backup*/);
2729 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2730 zlog_debug("%s: re %p backup_active %u", __func__
, re
,
2736 * Ref or create an nhe that matches the current state of the
2739 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
2740 struct nhg_hash_entry
*new_nhe
= NULL
;
2742 new_nhe
= zebra_nhg_rib_find_nhe(curr_nhe
, rt_afi
);
2744 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2745 zlog_debug("%s: re %p CHANGED: nhe %p (%u) => new_nhe %p (%u)",
2746 __func__
, re
, re
->nhe
,
2747 re
->nhe
->id
, new_nhe
, new_nhe
->id
);
2749 route_entry_update_nhe(re
, new_nhe
);
2753 /* Walk the NHE depends tree and toggle NEXTHOP_GROUP_VALID
2754 * flag where appropriate.
2757 zebra_nhg_set_valid_if_active(re
->nhe
);
2760 * Do not need the old / copied nhe anymore since it
2761 * was either copied over into a new nhe or not
2764 zebra_nhg_free(curr_nhe
);
2768 /* Recursively construct a grp array of fully resolved IDs.
2770 * This function allows us to account for groups within groups,
2771 * by converting them into a flat array of IDs.
2773 * nh_grp is modified at every level of recursion to append
2774 * to it the next unique, fully resolved ID from the entire tree.
2778 * I'm pretty sure we only allow ONE level of group within group currently.
2779 * But making this recursive just in case that ever changes.
2781 static uint8_t zebra_nhg_nhe2grp_internal(struct nh_grp
*grp
,
2783 struct nhg_hash_entry
*nhe
,
2786 struct nhg_connected
*rb_node_dep
= NULL
;
2787 struct nhg_hash_entry
*depend
= NULL
;
2788 uint8_t i
= curr_index
;
2790 frr_each(nhg_connected_tree
, &nhe
->nhg_depends
, rb_node_dep
) {
2791 bool duplicate
= false;
2796 depend
= rb_node_dep
->nhe
;
2799 * If its recursive, use its resolved nhe in the group
2801 if (CHECK_FLAG(depend
->flags
, NEXTHOP_GROUP_RECURSIVE
)) {
2802 depend
= zebra_nhg_resolve(depend
);
2805 EC_ZEBRA_NHG_FIB_UPDATE
,
2806 "Failed to recursively resolve Nexthop Hash Entry in the group id=%u",
2812 if (!zebra_nhg_depends_is_empty(depend
)) {
2813 /* This is a group within a group */
2814 i
= zebra_nhg_nhe2grp_internal(grp
, i
, depend
, max_num
);
2816 if (!CHECK_FLAG(depend
->flags
, NEXTHOP_GROUP_VALID
)) {
2817 if (IS_ZEBRA_DEBUG_RIB_DETAILED
2818 || IS_ZEBRA_DEBUG_NHG
)
2820 "%s: Nexthop ID (%u) not valid, not appending to dataplane install group",
2821 __func__
, depend
->id
);
2825 /* If the nexthop not installed/queued for install don't
2826 * put in the ID array.
2828 if (!(CHECK_FLAG(depend
->flags
, NEXTHOP_GROUP_INSTALLED
)
2829 || CHECK_FLAG(depend
->flags
,
2830 NEXTHOP_GROUP_QUEUED
))) {
2831 if (IS_ZEBRA_DEBUG_RIB_DETAILED
2832 || IS_ZEBRA_DEBUG_NHG
)
2834 "%s: Nexthop ID (%u) not installed or queued for install, not appending to dataplane install group",
2835 __func__
, depend
->id
);
2839 /* Check for duplicate IDs, ignore if found. */
2840 for (int j
= 0; j
< i
; j
++) {
2841 if (depend
->id
== grp
[j
].id
) {
2848 if (IS_ZEBRA_DEBUG_RIB_DETAILED
2849 || IS_ZEBRA_DEBUG_NHG
)
2851 "%s: Nexthop ID (%u) is duplicate, not appending to dataplane install group",
2852 __func__
, depend
->id
);
2856 grp
[i
].id
= depend
->id
;
2857 grp
[i
].weight
= depend
->nhg
.nexthop
->weight
;
2862 if (nhe
->backup_info
== NULL
|| nhe
->backup_info
->nhe
== NULL
)
2865 /* TODO -- For now, we are not trying to use or install any
2866 * backup info in this nexthop-id path: we aren't prepared
2867 * to use the backups here yet. We're just debugging what we find.
2869 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
2870 zlog_debug("%s: skipping backup nhe", __func__
);
2876 /* Convert a nhe into a group array */
2877 uint8_t zebra_nhg_nhe2grp(struct nh_grp
*grp
, struct nhg_hash_entry
*nhe
,
2880 /* Call into the recursive function */
2881 return zebra_nhg_nhe2grp_internal(grp
, 0, nhe
, max_num
);
2884 void zebra_nhg_install_kernel(struct nhg_hash_entry
*nhe
)
2886 struct nhg_connected
*rb_node_dep
= NULL
;
2888 /* Resolve it first */
2889 nhe
= zebra_nhg_resolve(nhe
);
2891 /* Make sure all depends are installed/queued */
2892 frr_each(nhg_connected_tree
, &nhe
->nhg_depends
, rb_node_dep
) {
2893 zebra_nhg_install_kernel(rb_node_dep
->nhe
);
2896 if (CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
)
2897 && !CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
)
2898 && !CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_QUEUED
)) {
2899 /* Change its type to us since we are installing it */
2900 if (!ZEBRA_NHG_CREATED(nhe
))
2901 nhe
->type
= ZEBRA_ROUTE_NHG
;
2903 int ret
= dplane_nexthop_add(nhe
);
2906 case ZEBRA_DPLANE_REQUEST_QUEUED
:
2907 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_QUEUED
);
2909 case ZEBRA_DPLANE_REQUEST_FAILURE
:
2911 EC_ZEBRA_DP_INSTALL_FAIL
,
2912 "Failed to install Nexthop ID (%u) into the kernel",
2915 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
2916 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
2917 zebra_nhg_handle_install(nhe
);
2923 void zebra_nhg_uninstall_kernel(struct nhg_hash_entry
*nhe
)
2925 if (CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
)) {
2926 int ret
= dplane_nexthop_delete(nhe
);
2929 case ZEBRA_DPLANE_REQUEST_QUEUED
:
2930 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_QUEUED
);
2932 case ZEBRA_DPLANE_REQUEST_FAILURE
:
2934 EC_ZEBRA_DP_DELETE_FAIL
,
2935 "Failed to uninstall Nexthop ID (%u) from the kernel",
2938 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
2939 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
2944 zebra_nhg_handle_uninstall(nhe
);
2947 void zebra_nhg_dplane_result(struct zebra_dplane_ctx
*ctx
)
2949 enum dplane_op_e op
;
2950 enum zebra_dplane_result status
;
2952 struct nhg_hash_entry
*nhe
= NULL
;
2954 op
= dplane_ctx_get_op(ctx
);
2955 status
= dplane_ctx_get_status(ctx
);
2957 id
= dplane_ctx_get_nhe_id(ctx
);
2959 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
|| IS_ZEBRA_DEBUG_NHG_DETAIL
)
2961 "Nexthop dplane ctx %p, op %s, nexthop ID (%u), result %s",
2962 ctx
, dplane_op2str(op
), id
, dplane_res2str(status
));
2965 case DPLANE_OP_NH_DELETE
:
2966 if (status
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
2968 EC_ZEBRA_DP_DELETE_FAIL
,
2969 "Failed to uninstall Nexthop ID (%u) from the kernel",
2972 /* We already free'd the data, nothing to do */
2974 case DPLANE_OP_NH_INSTALL
:
2975 case DPLANE_OP_NH_UPDATE
:
2976 nhe
= zebra_nhg_lookup_id(id
);
2979 if (IS_ZEBRA_DEBUG_NHG
)
2981 "%s operation preformed on Nexthop ID (%u) in the kernel, that we no longer have in our table",
2982 dplane_op2str(op
), id
);
2987 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_QUEUED
);
2988 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2989 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
2990 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
2991 zebra_nhg_handle_install(nhe
);
2993 /* If daemon nhg, send it an update */
2994 if (PROTO_OWNED(nhe
))
2995 zsend_nhg_notify(nhe
->type
, nhe
->zapi_instance
,
2996 nhe
->zapi_session
, nhe
->id
,
2997 ZAPI_NHG_INSTALLED
);
2999 /* If daemon nhg, send it an update */
3000 if (PROTO_OWNED(nhe
))
3001 zsend_nhg_notify(nhe
->type
, nhe
->zapi_instance
,
3002 nhe
->zapi_session
, nhe
->id
,
3003 ZAPI_NHG_FAIL_INSTALL
);
3005 if (!(zebra_nhg_proto_nexthops_only() &&
3008 EC_ZEBRA_DP_INSTALL_FAIL
,
3009 "Failed to install Nexthop ID (%u) into the kernel",
3014 case DPLANE_OP_ROUTE_INSTALL
:
3015 case DPLANE_OP_ROUTE_UPDATE
:
3016 case DPLANE_OP_ROUTE_DELETE
:
3017 case DPLANE_OP_ROUTE_NOTIFY
:
3018 case DPLANE_OP_LSP_INSTALL
:
3019 case DPLANE_OP_LSP_UPDATE
:
3020 case DPLANE_OP_LSP_DELETE
:
3021 case DPLANE_OP_LSP_NOTIFY
:
3022 case DPLANE_OP_PW_INSTALL
:
3023 case DPLANE_OP_PW_UNINSTALL
:
3024 case DPLANE_OP_SYS_ROUTE_ADD
:
3025 case DPLANE_OP_SYS_ROUTE_DELETE
:
3026 case DPLANE_OP_ADDR_INSTALL
:
3027 case DPLANE_OP_ADDR_UNINSTALL
:
3028 case DPLANE_OP_MAC_INSTALL
:
3029 case DPLANE_OP_MAC_DELETE
:
3030 case DPLANE_OP_NEIGH_INSTALL
:
3031 case DPLANE_OP_NEIGH_UPDATE
:
3032 case DPLANE_OP_NEIGH_DELETE
:
3033 case DPLANE_OP_NEIGH_IP_INSTALL
:
3034 case DPLANE_OP_NEIGH_IP_DELETE
:
3035 case DPLANE_OP_VTEP_ADD
:
3036 case DPLANE_OP_VTEP_DELETE
:
3037 case DPLANE_OP_RULE_ADD
:
3038 case DPLANE_OP_RULE_DELETE
:
3039 case DPLANE_OP_RULE_UPDATE
:
3040 case DPLANE_OP_NEIGH_DISCOVER
:
3041 case DPLANE_OP_BR_PORT_UPDATE
:
3042 case DPLANE_OP_NONE
:
3043 case DPLANE_OP_IPTABLE_ADD
:
3044 case DPLANE_OP_IPTABLE_DELETE
:
3045 case DPLANE_OP_IPSET_ADD
:
3046 case DPLANE_OP_IPSET_DELETE
:
3047 case DPLANE_OP_IPSET_ENTRY_ADD
:
3048 case DPLANE_OP_IPSET_ENTRY_DELETE
:
3049 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
3050 case DPLANE_OP_GRE_SET
:
3051 case DPLANE_OP_INTF_ADDR_ADD
:
3052 case DPLANE_OP_INTF_ADDR_DEL
:
3053 case DPLANE_OP_INTF_NETCONFIG
:
3054 case DPLANE_OP_INTF_INSTALL
:
3055 case DPLANE_OP_INTF_UPDATE
:
3056 case DPLANE_OP_INTF_DELETE
:
3060 dplane_ctx_fini(&ctx
);
3063 static int zebra_nhg_sweep_entry(struct hash_bucket
*bucket
, void *arg
)
3065 struct nhg_hash_entry
*nhe
= NULL
;
3067 nhe
= (struct nhg_hash_entry
*)bucket
->data
;
3070 * same logic as with routes.
3072 * If older than startup time, we know we read them in from the
3073 * kernel and have not gotten and update for them since startup
3074 * from an upper level proto.
3076 if (zrouter
.startup_time
< nhe
->uptime
)
3077 return HASHWALK_CONTINUE
;
3080 * If it's proto-owned and not being used by a route, remove it since
3081 * we haven't gotten an update about it from the proto since startup.
3082 * This means that either the config for it was removed or the daemon
3083 * didn't get started. This handles graceful restart & retain scenario.
3085 if (PROTO_OWNED(nhe
) && nhe
->refcnt
== 1) {
3086 zebra_nhg_decrement_ref(nhe
);
3087 return HASHWALK_ABORT
;
3091 * If its being ref'd by routes, just let it be uninstalled via a route
3094 if (ZEBRA_NHG_CREATED(nhe
) && nhe
->refcnt
<= 0) {
3095 zebra_nhg_uninstall_kernel(nhe
);
3096 return HASHWALK_ABORT
;
3099 return HASHWALK_CONTINUE
;
3102 void zebra_nhg_sweep_table(struct hash
*hash
)
3107 * Yes this is extremely odd. Effectively nhg's have
3108 * other nexthop groups that depend on them and when you
3109 * remove them, you can have other entries blown up.
3110 * our hash code does not work with deleting multiple
3111 * entries at a time and will possibly cause crashes
3112 * So what to do? Whenever zebra_nhg_sweep_entry
3113 * deletes an entry it will return HASHWALK_ABORT,
3114 * cause that deletion might have triggered more.
3115 * then we can just keep sweeping this table
3116 * until nothing more is found to do.
3119 count
= hashcount(hash
);
3120 hash_walk(hash
, zebra_nhg_sweep_entry
, NULL
);
3121 } while (count
!= hashcount(hash
));
3124 static void zebra_nhg_mark_keep_entry(struct hash_bucket
*bucket
, void *arg
)
3126 struct nhg_hash_entry
*nhe
= bucket
->data
;
3128 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
3132 * When we are shutting down and we have retain mode enabled
3133 * in zebra the process is to mark each vrf that it's
3134 * routes should not be deleted. The problem with that
3135 * is that shutdown actually free's up memory which
3136 * causes the nexthop group's ref counts to go to zero
3137 * we need a way to subtly tell the system to not remove
3138 * the nexthop groups from the kernel at the same time.
3139 * The easiest just looks like that we should not mark
3140 * the nhg's as installed any more and when the ref count
3141 * goes to zero we'll attempt to delete and do nothing
3143 void zebra_nhg_mark_keep(void)
3145 hash_iterate(zrouter
.nhgs_id
, zebra_nhg_mark_keep_entry
, NULL
);
3148 /* Global control to disable use of kernel nexthops, if available. We can't
3149 * force the kernel to support nexthop ids, of course, but we can disable
3150 * zebra's use of them, for testing e.g. By default, if the kernel supports
3151 * nexthop ids, zebra uses them.
3153 void zebra_nhg_enable_kernel_nexthops(bool set
)
3155 g_nexthops_enabled
= set
;
3158 bool zebra_nhg_kernel_nexthops_enabled(void)
3160 return g_nexthops_enabled
;
3163 /* Global control for use of activated backups for recursive resolution. */
3164 void zebra_nhg_set_recursive_use_backups(bool set
)
3166 use_recursive_backups
= set
;
3169 bool zebra_nhg_recursive_use_backups(void)
3171 return use_recursive_backups
;
3175 * Global control to only use kernel nexthops for protocol created NHGs.
3176 * There are some use cases where you may not want zebra to implicitly
3177 * create kernel nexthops for all routes and only create them for NHGs
3178 * passed down by upper level protos.
3182 void zebra_nhg_set_proto_nexthops_only(bool set
)
3184 proto_nexthops_only
= set
;
3187 bool zebra_nhg_proto_nexthops_only(void)
3189 return proto_nexthops_only
;
3192 /* Add NHE from upper level proto */
3193 struct nhg_hash_entry
*zebra_nhg_proto_add(uint32_t id
, int type
,
3194 uint16_t instance
, uint32_t session
,
3195 struct nexthop_group
*nhg
, afi_t afi
)
3197 struct nhg_hash_entry lookup
;
3198 struct nhg_hash_entry
*new, *old
;
3199 struct nhg_connected
*rb_node_dep
= NULL
;
3200 struct nexthop
*newhop
;
3201 bool replace
= false;
3203 if (!nhg
->nexthop
) {
3204 if (IS_ZEBRA_DEBUG_NHG
)
3205 zlog_debug("%s: id %u, no nexthops passed to add",
3211 /* Set nexthop list as active, since they wont go through rib
3214 * Assuming valid/onlink for now.
3216 * Once resolution is figured out, we won't need this!
3218 for (ALL_NEXTHOPS_PTR(nhg
, newhop
)) {
3219 if (CHECK_FLAG(newhop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
3220 if (IS_ZEBRA_DEBUG_NHG
)
3222 "%s: id %u, backup nexthops not supported",
3227 if (newhop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
3228 if (IS_ZEBRA_DEBUG_NHG
)
3230 "%s: id %u, blackhole nexthop not supported",
3235 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
) {
3236 if (IS_ZEBRA_DEBUG_NHG
)
3238 "%s: id %u, nexthop without gateway not supported",
3243 if (!newhop
->ifindex
) {
3244 if (IS_ZEBRA_DEBUG_NHG
)
3246 "%s: id %u, nexthop without ifindex is not supported",
3250 SET_FLAG(newhop
->flags
, NEXTHOP_FLAG_ACTIVE
);
3253 zebra_nhe_init(&lookup
, afi
, nhg
->nexthop
);
3254 lookup
.nhg
.nexthop
= nhg
->nexthop
;
3258 old
= zebra_nhg_lookup_id(id
);
3262 * This is a replace, just release NHE from ID for now, The
3263 * depends/dependents may still be used in the replacement so
3264 * we don't touch them other than to remove their refs to their
3268 hash_release(zrouter
.nhgs_id
, old
);
3270 /* Free all the things */
3271 zebra_nhg_release_all_deps(old
);
3274 new = zebra_nhg_rib_find_nhe(&lookup
, afi
);
3276 zebra_nhg_increment_ref(new);
3278 /* Capture zapi client info */
3279 new->zapi_instance
= instance
;
3280 new->zapi_session
= session
;
3282 zebra_nhg_set_valid_if_active(new);
3284 zebra_nhg_install_kernel(new);
3288 * Check to handle recving DEL while routes still in use then
3291 * In this case we would have decremented the refcnt already
3292 * but set the FLAG here. Go ahead and increment once to fix
3293 * the misordering we have been sent.
3295 if (CHECK_FLAG(old
->flags
, NEXTHOP_GROUP_PROTO_RELEASED
))
3296 zebra_nhg_increment_ref(old
);
3298 rib_handle_nhg_replace(old
, new);
3300 /* if this != 1 at this point, we have a bug */
3301 assert(old
->refcnt
== 1);
3303 /* We have to decrement its singletons
3304 * because some might not exist in NEW.
3306 if (!zebra_nhg_depends_is_empty(old
)) {
3307 frr_each (nhg_connected_tree
, &old
->nhg_depends
,
3309 zebra_nhg_decrement_ref(rb_node_dep
->nhe
);
3312 /* Dont call the dec API, we dont want to uninstall the ID */
3314 zebra_nhg_free(old
);
3318 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
3319 zlog_debug("%s: %s nhe %p (%u), vrf %d, type %s", __func__
,
3320 (replace
? "replaced" : "added"), new, new->id
,
3321 new->vrf_id
, zebra_route_string(new->type
));
3326 /* Delete NHE from upper level proto, caller must decrement ref */
3327 struct nhg_hash_entry
*zebra_nhg_proto_del(uint32_t id
, int type
)
3329 struct nhg_hash_entry
*nhe
;
3331 nhe
= zebra_nhg_lookup_id(id
);
3334 if (IS_ZEBRA_DEBUG_NHG
)
3335 zlog_debug("%s: id %u, lookup failed", __func__
, id
);
3340 if (type
!= nhe
->type
) {
3341 if (IS_ZEBRA_DEBUG_NHG
)
3343 "%s: id %u, type %s mismatch, sent by %s, ignoring",
3344 __func__
, id
, zebra_route_string(nhe
->type
),
3345 zebra_route_string(type
));
3349 if (CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_PROTO_RELEASED
)) {
3350 if (IS_ZEBRA_DEBUG_NHG
)
3351 zlog_debug("%s: id %u, already released", __func__
, id
);
3356 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_PROTO_RELEASED
);
3358 if (nhe
->refcnt
> 1) {
3359 if (IS_ZEBRA_DEBUG_NHG
)
3361 "%s: id %u, still being used by routes refcnt %u",
3362 __func__
, nhe
->id
, nhe
->refcnt
);
3366 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
3367 zlog_debug("%s: deleted nhe %p (%u), vrf %d, type %s", __func__
,
3368 nhe
, nhe
->id
, nhe
->vrf_id
,
3369 zebra_route_string(nhe
->type
));
3374 struct nhg_score_proto_iter
{
3379 static void zebra_nhg_score_proto_entry(struct hash_bucket
*bucket
, void *arg
)
3381 struct nhg_hash_entry
*nhe
;
3382 struct nhg_score_proto_iter
*iter
;
3384 nhe
= (struct nhg_hash_entry
*)bucket
->data
;
3387 /* Needs to match type and outside zebra ID space */
3388 if (nhe
->type
== iter
->type
&& PROTO_OWNED(nhe
)) {
3389 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
3391 "%s: found nhe %p (%u), vrf %d, type %s after client disconnect",
3392 __func__
, nhe
, nhe
->id
, nhe
->vrf_id
,
3393 zebra_route_string(nhe
->type
));
3395 /* Add to removal list */
3396 listnode_add(iter
->found
, nhe
);
3400 /* Remove specific by proto NHGs */
3401 unsigned long zebra_nhg_score_proto(int type
)
3403 struct nhg_hash_entry
*nhe
;
3404 struct nhg_score_proto_iter iter
= {};
3405 struct listnode
*ln
;
3406 unsigned long count
;
3409 iter
.found
= list_new();
3411 /* Find matching entries to remove */
3412 hash_iterate(zrouter
.nhgs_id
, zebra_nhg_score_proto_entry
, &iter
);
3414 /* Now remove them */
3415 for (ALL_LIST_ELEMENTS_RO(iter
.found
, ln
, nhe
)) {
3417 * This should be the last ref if we remove client routes too,
3418 * and thus should remove and free them.
3420 zebra_nhg_decrement_ref(nhe
);
3423 count
= iter
.found
->count
;
3424 list_delete(&iter
.found
);