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"
32 #include "zebra/connected.h"
33 #include "zebra/debug.h"
34 #include "zebra/zebra_router.h"
35 #include "zebra/zebra_nhg.h"
36 #include "zebra/zebra_rnh.h"
37 #include "zebra/zebra_routemap.h"
38 #include "zebra/zebra_memory.h"
39 #include "zebra/zserv.h"
41 #include "zebra_errors.h"
42 #include "zebra_dplane.h"
43 #include "zebra/interface.h"
45 DEFINE_MTYPE_STATIC(ZEBRA
, NHG
, "Nexthop Group Entry");
46 DEFINE_MTYPE_STATIC(ZEBRA
, NHG_CONNECTED
, "Nexthop Group Connected");
47 DEFINE_MTYPE_STATIC(ZEBRA
, NHG_CTX
, "Nexthop Group Context");
49 /* id counter to keep in sync with kernel */
52 static struct nhg_hash_entry
*depends_find(struct nexthop
*nh
, afi_t afi
);
53 static void depends_add(struct nhg_connected_tree_head
*head
,
54 struct nhg_hash_entry
*depend
);
55 static struct nhg_hash_entry
*
56 depends_find_add(struct nhg_connected_tree_head
*head
, struct nexthop
*nh
,
58 static struct nhg_hash_entry
*
59 depends_find_id_add(struct nhg_connected_tree_head
*head
, uint32_t id
);
60 static void depends_decrement_free(struct nhg_connected_tree_head
*head
);
63 void nhg_connected_free(struct nhg_connected
*dep
)
65 XFREE(MTYPE_NHG_CONNECTED
, dep
);
68 struct nhg_connected
*nhg_connected_new(struct nhg_hash_entry
*nhe
)
70 struct nhg_connected
*new = NULL
;
72 new = XCALLOC(MTYPE_NHG_CONNECTED
, sizeof(struct nhg_connected
));
78 void nhg_connected_tree_free(struct nhg_connected_tree_head
*head
)
80 struct nhg_connected
*rb_node_dep
= NULL
;
82 if (!nhg_connected_tree_is_empty(head
)) {
83 frr_each_safe(nhg_connected_tree
, head
, rb_node_dep
) {
84 nhg_connected_tree_del(head
, rb_node_dep
);
85 nhg_connected_free(rb_node_dep
);
90 bool nhg_connected_tree_is_empty(const struct nhg_connected_tree_head
*head
)
92 return nhg_connected_tree_count(head
) ? false : true;
95 struct nhg_connected
*
96 nhg_connected_tree_root(struct nhg_connected_tree_head
*head
)
98 return nhg_connected_tree_first(head
);
101 void nhg_connected_tree_del_nhe(struct nhg_connected_tree_head
*head
,
102 struct nhg_hash_entry
*depend
)
104 struct nhg_connected lookup
= {};
105 struct nhg_connected
*remove
= NULL
;
109 /* Lookup to find the element, then remove it */
110 remove
= nhg_connected_tree_find(head
, &lookup
);
111 remove
= nhg_connected_tree_del(head
, remove
);
114 nhg_connected_free(remove
);
117 void nhg_connected_tree_add_nhe(struct nhg_connected_tree_head
*head
,
118 struct nhg_hash_entry
*depend
)
120 struct nhg_connected
*new = NULL
;
122 new = nhg_connected_new(depend
);
125 nhg_connected_tree_add(head
, new);
129 nhg_connected_tree_decrement_ref(struct nhg_connected_tree_head
*head
)
131 struct nhg_connected
*rb_node_dep
= NULL
;
133 frr_each_safe(nhg_connected_tree
, head
, rb_node_dep
) {
134 zebra_nhg_decrement_ref(rb_node_dep
->nhe
);
139 nhg_connected_tree_increment_ref(struct nhg_connected_tree_head
*head
)
141 struct nhg_connected
*rb_node_dep
= NULL
;
143 frr_each(nhg_connected_tree
, head
, rb_node_dep
) {
144 zebra_nhg_increment_ref(rb_node_dep
->nhe
);
148 struct nhg_hash_entry
*zebra_nhg_resolve(struct nhg_hash_entry
*nhe
)
150 if (CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_RECURSIVE
)
151 && !zebra_nhg_depends_is_empty(nhe
)) {
152 nhe
= nhg_connected_tree_root(&nhe
->nhg_depends
)->nhe
;
153 return zebra_nhg_resolve(nhe
);
159 uint32_t zebra_nhg_get_resolved_id(uint32_t id
)
161 struct nhg_hash_entry
*nhe
= NULL
;
163 nhe
= zebra_nhg_lookup_id(id
);
167 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
168 "Zebra failed to lookup a resolved nexthop hash entry id=%u",
173 if (CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_RECURSIVE
))
174 nhe
= zebra_nhg_resolve(nhe
);
179 unsigned int zebra_nhg_depends_count(const struct nhg_hash_entry
*nhe
)
181 return nhg_connected_tree_count(&nhe
->nhg_depends
);
184 bool zebra_nhg_depends_is_empty(const struct nhg_hash_entry
*nhe
)
186 return nhg_connected_tree_is_empty(&nhe
->nhg_depends
);
189 void zebra_nhg_depends_del(struct nhg_hash_entry
*from
,
190 struct nhg_hash_entry
*depend
)
192 nhg_connected_tree_del_nhe(&from
->nhg_depends
, depend
);
195 void zebra_nhg_depends_add(struct nhg_hash_entry
*to
,
196 struct nhg_hash_entry
*depend
)
198 nhg_connected_tree_add_nhe(&to
->nhg_depends
, depend
);
201 void zebra_nhg_depends_init(struct nhg_hash_entry
*nhe
)
203 nhg_connected_tree_init(&nhe
->nhg_depends
);
206 /* Release this nhe from anything that it depends on */
207 static void zebra_nhg_depends_release(struct nhg_hash_entry
*nhe
)
209 if (!zebra_nhg_depends_is_empty(nhe
)) {
210 struct nhg_connected
*rb_node_dep
= NULL
;
212 frr_each_safe(nhg_connected_tree
, &nhe
->nhg_depends
,
214 zebra_nhg_dependents_del(rb_node_dep
->nhe
, nhe
);
219 unsigned int zebra_nhg_dependents_count(const struct nhg_hash_entry
*nhe
)
221 return nhg_connected_tree_count(&nhe
->nhg_dependents
);
224 bool zebra_nhg_dependents_is_empty(const struct nhg_hash_entry
*nhe
)
226 return nhg_connected_tree_is_empty(&nhe
->nhg_dependents
);
229 void zebra_nhg_dependents_del(struct nhg_hash_entry
*from
,
230 struct nhg_hash_entry
*dependent
)
232 nhg_connected_tree_del_nhe(&from
->nhg_dependents
, dependent
);
235 void zebra_nhg_dependents_add(struct nhg_hash_entry
*to
,
236 struct nhg_hash_entry
*dependent
)
238 nhg_connected_tree_add_nhe(&to
->nhg_dependents
, dependent
);
241 void zebra_nhg_dependents_init(struct nhg_hash_entry
*nhe
)
243 nhg_connected_tree_init(&nhe
->nhg_dependents
);
246 /* Release this nhe from anything depending on it */
247 static void zebra_nhg_dependents_release(struct nhg_hash_entry
*nhe
)
249 struct nhg_connected
*rb_node_dep
= NULL
;
251 frr_each_safe(nhg_connected_tree
, &nhe
->nhg_dependents
, rb_node_dep
) {
252 zebra_nhg_depends_del(rb_node_dep
->nhe
, nhe
);
253 /* recheck validity of the dependent */
254 zebra_nhg_check_valid(rb_node_dep
->nhe
);
258 struct nhg_hash_entry
*zebra_nhg_lookup_id(uint32_t id
)
260 struct nhg_hash_entry lookup
= {};
263 return hash_lookup(zrouter
.nhgs_id
, &lookup
);
266 int zebra_nhg_insert_id(struct nhg_hash_entry
*nhe
)
268 if (hash_lookup(zrouter
.nhgs_id
, nhe
)) {
270 EC_ZEBRA_NHG_TABLE_INSERT_FAILED
,
271 "Failed inserting NHG id=%u into the ID hash table, entry already exists",
276 hash_get(zrouter
.nhgs_id
, nhe
, hash_alloc_intern
);
282 zebra_nhg_connect_depends(struct nhg_hash_entry
*nhe
,
283 struct nhg_connected_tree_head nhg_depends
)
285 struct nhg_connected
*rb_node_dep
= NULL
;
287 /* This has been allocated higher above in the stack. Could probably
288 * re-allocate and free the old stuff but just using the same memory
289 * for now. Otherwise, their might be a time trade-off for repeated
290 * alloc/frees as startup.
292 nhe
->nhg_depends
= nhg_depends
;
294 /* Attach backpointer to anything that it depends on */
295 zebra_nhg_dependents_init(nhe
);
296 if (!zebra_nhg_depends_is_empty(nhe
)) {
297 frr_each(nhg_connected_tree
, &nhe
->nhg_depends
, rb_node_dep
) {
298 zebra_nhg_dependents_add(rb_node_dep
->nhe
, nhe
);
302 /* Add the ifp now if its not a group or recursive and has ifindex */
303 if (zebra_nhg_depends_is_empty(nhe
) && nhe
->nhg
->nexthop
304 && nhe
->nhg
->nexthop
->ifindex
) {
305 struct interface
*ifp
= NULL
;
307 ifp
= if_lookup_by_index(nhe
->nhg
->nexthop
->ifindex
,
310 zebra_nhg_set_if(nhe
, ifp
);
313 EC_ZEBRA_IF_LOOKUP_FAILED
,
314 "Zebra failed to lookup an interface with ifindex=%d in vrf=%u for NHE id=%u",
315 nhe
->nhg
->nexthop
->ifindex
, nhe
->vrf_id
,
320 static struct nhg_hash_entry
*zebra_nhg_copy(struct nhg_hash_entry
*copy
,
323 struct nhg_hash_entry
*nhe
;
325 nhe
= XCALLOC(MTYPE_NHG
, sizeof(struct nhg_hash_entry
));
329 nhe
->nhg
= nexthop_group_new();
330 nexthop_group_copy(nhe
->nhg
, copy
->nhg
);
332 nhe
->vrf_id
= copy
->vrf_id
;
333 nhe
->afi
= copy
->afi
;
334 nhe
->type
= copy
->type
? copy
->type
: ZEBRA_ROUTE_NHG
;
336 nhe
->dplane_ref
= zebra_router_get_next_sequence();
341 /* Allocation via hash handler */
342 static void *zebra_nhg_hash_alloc(void *arg
)
344 struct nhg_hash_entry
*nhe
= NULL
;
345 struct nhg_hash_entry
*copy
= arg
;
347 nhe
= zebra_nhg_copy(copy
, copy
->id
);
349 /* Mark duplicate nexthops in a group at creation time. */
350 nexthop_group_mark_duplicates(nhe
->nhg
);
352 zebra_nhg_connect_depends(nhe
, copy
->nhg_depends
);
353 zebra_nhg_insert_id(nhe
);
358 uint32_t zebra_nhg_hash_key(const void *arg
)
360 const struct nhg_hash_entry
*nhe
= arg
;
362 uint32_t key
= 0x5a351234;
364 key
= jhash_3words(nhe
->vrf_id
, nhe
->afi
, nexthop_group_hash(nhe
->nhg
),
370 uint32_t zebra_nhg_id_key(const void *arg
)
372 const struct nhg_hash_entry
*nhe
= arg
;
377 bool zebra_nhg_hash_equal(const void *arg1
, const void *arg2
)
379 const struct nhg_hash_entry
*nhe1
= arg1
;
380 const struct nhg_hash_entry
*nhe2
= arg2
;
382 /* No matter what if they equal IDs, assume equal */
383 if (nhe1
->id
&& nhe2
->id
&& (nhe1
->id
== nhe2
->id
))
386 if (nhe1
->vrf_id
!= nhe2
->vrf_id
)
389 if (nhe1
->afi
!= nhe2
->afi
)
392 if (nexthop_group_active_nexthop_num_no_recurse(nhe1
->nhg
)
393 != nexthop_group_active_nexthop_num_no_recurse(nhe2
->nhg
))
396 if (!nexthop_group_equal_no_recurse(nhe1
->nhg
, nhe2
->nhg
))
402 bool zebra_nhg_hash_id_equal(const void *arg1
, const void *arg2
)
404 const struct nhg_hash_entry
*nhe1
= arg1
;
405 const struct nhg_hash_entry
*nhe2
= arg2
;
407 return nhe1
->id
== nhe2
->id
;
410 static int zebra_nhg_process_grp(struct nexthop_group
*nhg
,
411 struct nhg_connected_tree_head
*depends
,
412 struct nh_grp
*grp
, uint8_t count
)
414 nhg_connected_tree_init(depends
);
416 for (int i
= 0; i
< count
; i
++) {
417 struct nhg_hash_entry
*depend
= NULL
;
418 /* We do not care about nexthop_grp.weight at
419 * this time. But we should figure out
420 * how to adapt this to our code in
423 depend
= depends_find_id_add(depends
, grp
[i
].id
);
428 "Received Nexthop Group from the kernel with a dependent Nexthop ID (%u) which we do not have in our table",
434 * If this is a nexthop with its own group
435 * dependencies, add them as well. Not sure its
436 * even possible to have a group within a group
440 copy_nexthops(&nhg
->nexthop
, depend
->nhg
->nexthop
, NULL
);
446 static void handle_recursive_depend(struct nhg_connected_tree_head
*nhg_depends
,
447 struct nexthop
*nh
, afi_t afi
)
449 struct nhg_hash_entry
*depend
= NULL
;
450 struct nexthop_group resolved_ng
= {};
452 _nexthop_group_add_sorted(&resolved_ng
, nh
);
454 depend
= zebra_nhg_rib_find(0, &resolved_ng
, afi
);
455 depends_add(nhg_depends
, depend
);
458 static bool zebra_nhg_find(struct nhg_hash_entry
**nhe
, uint32_t id
,
459 struct nexthop_group
*nhg
,
460 struct nhg_connected_tree_head
*nhg_depends
,
461 vrf_id_t vrf_id
, afi_t afi
, int type
)
463 struct nhg_hash_entry lookup
= {};
465 uint32_t old_id_counter
= id_counter
;
467 bool created
= false;
468 bool recursive
= false;
471 * If it has an id at this point, we must have gotten it from the kernel
473 lookup
.id
= id
? id
: ++id_counter
;
475 lookup
.type
= type
? type
: ZEBRA_ROUTE_NHG
;
478 if (lookup
.nhg
->nexthop
->next
) {
479 /* Groups can have all vrfs and AF's in them */
480 lookup
.afi
= AFI_UNSPEC
;
483 switch (lookup
.nhg
->nexthop
->type
) {
484 case (NEXTHOP_TYPE_IFINDEX
):
485 case (NEXTHOP_TYPE_BLACKHOLE
):
487 * This switch case handles setting the afi different
488 * for ipv4/v6 routes. Ifindex/blackhole nexthop
489 * objects cannot be ambiguous, they must be Address
490 * Family specific. If we get here, we will either use
491 * the AF of the route, or the one we got passed from
492 * here from the kernel.
496 case (NEXTHOP_TYPE_IPV4_IFINDEX
):
497 case (NEXTHOP_TYPE_IPV4
):
500 case (NEXTHOP_TYPE_IPV6_IFINDEX
):
501 case (NEXTHOP_TYPE_IPV6
):
502 lookup
.afi
= AFI_IP6
;
506 lookup
.vrf_id
= vrf_id
;
510 (*nhe
) = zebra_nhg_lookup_id(id
);
512 (*nhe
) = hash_lookup(zrouter
.nhgs
, &lookup
);
514 /* If it found an nhe in our tables, this new ID is unused */
516 id_counter
= old_id_counter
;
519 /* Only hash/lookup the depends if the first lookup
520 * fails to find something. This should hopefully save a
521 * lot of cycles for larger ecmp sizes.
524 /* If you don't want to hash on each nexthop in the
525 * nexthop group struct you can pass the depends
526 * directly. Kernel-side we do this since it just looks
529 lookup
.nhg_depends
= *nhg_depends
;
531 if (nhg
->nexthop
->next
) {
532 nhg_connected_tree_init(&lookup
.nhg_depends
);
534 /* If its a group, create a dependency tree */
535 struct nexthop
*nh
= NULL
;
537 for (nh
= nhg
->nexthop
; nh
; nh
= nh
->next
)
538 depends_find_add(&lookup
.nhg_depends
,
540 } else if (CHECK_FLAG(nhg
->nexthop
->flags
,
541 NEXTHOP_FLAG_RECURSIVE
)) {
542 nhg_connected_tree_init(&lookup
.nhg_depends
);
543 handle_recursive_depend(&lookup
.nhg_depends
,
544 nhg
->nexthop
->resolved
,
550 (*nhe
) = hash_get(zrouter
.nhgs
, &lookup
, zebra_nhg_hash_alloc
);
554 SET_FLAG((*nhe
)->flags
, NEXTHOP_GROUP_RECURSIVE
);
559 /* Find/create a single nexthop */
560 static struct nhg_hash_entry
*
561 zebra_nhg_find_nexthop(uint32_t id
, struct nexthop
*nh
, afi_t afi
, int type
)
563 struct nhg_hash_entry
*nhe
= NULL
;
564 struct nexthop_group nhg
= {};
566 _nexthop_group_add_sorted(&nhg
, nh
);
568 zebra_nhg_find(&nhe
, id
, &nhg
, NULL
, nh
->vrf_id
, afi
, 0);
573 static struct nhg_ctx
*nhg_ctx_new()
575 struct nhg_ctx
*new = NULL
;
577 new = XCALLOC(MTYPE_NHG_CTX
, sizeof(struct nhg_ctx
));
582 static void nhg_ctx_free(struct nhg_ctx
*ctx
)
584 XFREE(MTYPE_NHG_CTX
, ctx
);
587 static uint32_t nhg_ctx_get_id(const struct nhg_ctx
*ctx
)
592 static void nhg_ctx_set_status(struct nhg_ctx
*ctx
, enum nhg_ctx_status status
)
594 ctx
->status
= status
;
597 static enum nhg_ctx_status
nhg_ctx_get_status(const struct nhg_ctx
*ctx
)
602 static void nhg_ctx_set_op(struct nhg_ctx
*ctx
, enum nhg_ctx_op_e op
)
607 static enum nhg_ctx_op_e
nhg_ctx_get_op(const struct nhg_ctx
*ctx
)
612 static vrf_id_t
nhg_ctx_get_vrf_id(const struct nhg_ctx
*ctx
)
617 static int nhg_ctx_get_type(const struct nhg_ctx
*ctx
)
622 static int nhg_ctx_get_afi(const struct nhg_ctx
*ctx
)
627 static struct nexthop
*nhg_ctx_get_nh(struct nhg_ctx
*ctx
)
632 static uint8_t nhg_ctx_get_count(const struct nhg_ctx
*ctx
)
637 static struct nh_grp
*nhg_ctx_get_grp(struct nhg_ctx
*ctx
)
642 static struct nhg_ctx
*nhg_ctx_init(uint32_t id
, struct nexthop
*nh
,
643 struct nh_grp
*grp
, vrf_id_t vrf_id
,
644 afi_t afi
, int type
, uint8_t count
)
646 struct nhg_ctx
*ctx
= NULL
;
651 ctx
->vrf_id
= vrf_id
;
657 /* Copy over the array */
658 memcpy(&ctx
->u
.grp
, grp
, count
* sizeof(struct nh_grp
));
665 static bool zebra_nhg_contains_unhashable(struct nhg_hash_entry
*nhe
)
667 struct nhg_connected
*rb_node_dep
= NULL
;
669 frr_each(nhg_connected_tree
, &nhe
->nhg_depends
, rb_node_dep
) {
670 if (CHECK_FLAG(rb_node_dep
->nhe
->flags
,
671 NEXTHOP_GROUP_UNHASHABLE
))
678 static void zebra_nhg_set_unhashable(struct nhg_hash_entry
*nhe
)
680 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_UNHASHABLE
);
681 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
684 EC_ZEBRA_DUPLICATE_NHG_MESSAGE
,
685 "Nexthop Group with ID (%d) is a duplicate, therefore unhashable, ignoring",
689 static void zebra_nhg_set_valid(struct nhg_hash_entry
*nhe
)
691 struct nhg_connected
*rb_node_dep
;
693 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
695 frr_each(nhg_connected_tree
, &nhe
->nhg_dependents
, rb_node_dep
)
696 zebra_nhg_set_valid(rb_node_dep
->nhe
);
699 static void zebra_nhg_set_invalid(struct nhg_hash_entry
*nhe
)
701 struct nhg_connected
*rb_node_dep
;
703 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
705 /* Update validity of nexthops depending on it */
706 frr_each(nhg_connected_tree
, &nhe
->nhg_dependents
, rb_node_dep
)
707 zebra_nhg_check_valid(rb_node_dep
->nhe
);
710 void zebra_nhg_check_valid(struct nhg_hash_entry
*nhe
)
712 struct nhg_connected
*rb_node_dep
= NULL
;
715 /* If anthing else in the group is valid, the group is valid */
716 frr_each(nhg_connected_tree
, &nhe
->nhg_depends
, rb_node_dep
) {
717 if (CHECK_FLAG(rb_node_dep
->nhe
->flags
, NEXTHOP_GROUP_VALID
)) {
725 zebra_nhg_set_valid(nhe
);
727 zebra_nhg_set_invalid(nhe
);
731 static void zebra_nhg_release(struct nhg_hash_entry
*nhe
)
733 /* Remove it from any lists it may be on */
734 zebra_nhg_depends_release(nhe
);
735 zebra_nhg_dependents_release(nhe
);
737 if_nhg_dependents_del(nhe
->ifp
, nhe
);
740 * If its unhashable, we didn't store it here and have to be
741 * sure we don't clear one thats actually being used.
743 if (!CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_UNHASHABLE
))
744 hash_release(zrouter
.nhgs
, nhe
);
746 hash_release(zrouter
.nhgs_id
, nhe
);
749 static void zebra_nhg_handle_uninstall(struct nhg_hash_entry
*nhe
)
751 zebra_nhg_release(nhe
);
755 static void zebra_nhg_handle_install(struct nhg_hash_entry
*nhe
)
757 /* Update validity of groups depending on it */
758 struct nhg_connected
*rb_node_dep
;
760 frr_each_safe(nhg_connected_tree
, &nhe
->nhg_dependents
, rb_node_dep
)
761 zebra_nhg_set_valid(rb_node_dep
->nhe
);
765 * The kernel/other program has changed the state of a nexthop object we are
768 static void zebra_nhg_handle_kernel_state_change(struct nhg_hash_entry
*nhe
,
774 "Kernel %s a nexthop group with ID (%u) that we are still using for a route, sending it back down",
775 (is_delete
? "deleted" : "updated"), nhe
->id
);
777 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
778 zebra_nhg_install_kernel(nhe
);
780 zebra_nhg_handle_uninstall(nhe
);
783 static int nhg_ctx_process_new(struct nhg_ctx
*ctx
)
785 struct nexthop_group
*nhg
= NULL
;
786 struct nhg_connected_tree_head nhg_depends
= {};
787 struct nhg_hash_entry
*lookup
= NULL
;
788 struct nhg_hash_entry
*nhe
= NULL
;
790 uint32_t id
= nhg_ctx_get_id(ctx
);
791 uint8_t count
= nhg_ctx_get_count(ctx
);
792 vrf_id_t vrf_id
= nhg_ctx_get_vrf_id(ctx
);
793 int type
= nhg_ctx_get_type(ctx
);
794 afi_t afi
= nhg_ctx_get_afi(ctx
);
796 lookup
= zebra_nhg_lookup_id(id
);
799 /* This is already present in our table, hence an update
800 * that we did not initate.
802 zebra_nhg_handle_kernel_state_change(lookup
, false);
806 if (nhg_ctx_get_count(ctx
)) {
807 nhg
= nexthop_group_new();
808 if (zebra_nhg_process_grp(nhg
, &nhg_depends
,
809 nhg_ctx_get_grp(ctx
), count
)) {
810 depends_decrement_free(&nhg_depends
);
811 nexthop_group_delete(&nhg
);
815 if (!zebra_nhg_find(&nhe
, id
, nhg
, &nhg_depends
, vrf_id
, type
,
817 depends_decrement_free(&nhg_depends
);
819 /* These got copied over in zebra_nhg_alloc() */
820 nexthop_group_delete(&nhg
);
822 nhe
= zebra_nhg_find_nexthop(id
, nhg_ctx_get_nh(ctx
), afi
,
827 struct nhg_hash_entry
*kernel_nhe
= NULL
;
829 /* Duplicate but with different ID from
833 /* The kernel allows duplicate nexthops
834 * as long as they have different IDs.
835 * We are ignoring those to prevent
836 * syncing problems with the kernel
839 * We maintain them *ONLY* in the ID hash table to
840 * track them and set the flag to indicated
841 * their attributes are unhashable.
844 kernel_nhe
= zebra_nhg_copy(nhe
, id
);
845 zebra_nhg_insert_id(kernel_nhe
);
846 zebra_nhg_set_unhashable(kernel_nhe
);
847 } else if (zebra_nhg_contains_unhashable(nhe
)) {
848 /* The group we got contains an unhashable/duplicated
849 * depend, so lets mark this group as unhashable as well
850 * and release it from the non-ID hash.
852 hash_release(zrouter
.nhgs
, nhe
);
853 zebra_nhg_set_unhashable(nhe
);
855 /* It actually created a new nhe */
856 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
857 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
861 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
862 "Zebra failed to find or create a nexthop hash entry for ID (%u)",
870 static int nhg_ctx_process_del(struct nhg_ctx
*ctx
)
872 struct nhg_hash_entry
*nhe
= NULL
;
873 uint32_t id
= nhg_ctx_get_id(ctx
);
875 nhe
= zebra_nhg_lookup_id(id
);
879 EC_ZEBRA_BAD_NHG_MESSAGE
,
880 "Kernel delete message received for nexthop group ID (%u) that we do not have in our ID table",
885 zebra_nhg_handle_kernel_state_change(nhe
, true);
890 static void nhg_ctx_process_finish(struct nhg_ctx
*ctx
)
895 * Just freeing for now, maybe do something more in the future
899 if (nhg_ctx_get_count(ctx
))
902 nh
= nhg_ctx_get_nh(ctx
);
904 nexthop_del_labels(nh
);
911 static int queue_add(struct nhg_ctx
*ctx
)
913 /* If its queued or already processed do nothing */
914 if (nhg_ctx_get_status(ctx
) == NHG_CTX_QUEUED
)
917 if (rib_queue_nhg_add(ctx
)) {
918 nhg_ctx_set_status(ctx
, NHG_CTX_FAILURE
);
922 nhg_ctx_set_status(ctx
, NHG_CTX_QUEUED
);
927 int nhg_ctx_process(struct nhg_ctx
*ctx
)
931 switch (nhg_ctx_get_op(ctx
)) {
933 ret
= nhg_ctx_process_new(ctx
);
934 if (nhg_ctx_get_count(ctx
) && ret
== -ENOENT
935 && nhg_ctx_get_status(ctx
) != NHG_CTX_REQUEUED
) {
937 * We have entered a situation where we are
938 * processing a group from the kernel
939 * that has a contained nexthop which
940 * we have not yet processed.
942 * Re-enqueue this ctx to be handled exactly one
943 * more time (indicated by the flag).
945 * By the time we get back to it, we
946 * should have processed its depends.
948 nhg_ctx_set_status(ctx
, NHG_CTX_NONE
);
949 if (queue_add(ctx
) == 0) {
950 nhg_ctx_set_status(ctx
, NHG_CTX_REQUEUED
);
956 ret
= nhg_ctx_process_del(ctx
);
957 case NHG_CTX_OP_NONE
:
961 nhg_ctx_set_status(ctx
, (ret
? NHG_CTX_FAILURE
: NHG_CTX_SUCCESS
));
963 nhg_ctx_process_finish(ctx
);
968 /* Kernel-side, you either get a single new nexthop or a array of ID's */
969 int zebra_nhg_kernel_find(uint32_t id
, struct nexthop
*nh
, struct nh_grp
*grp
,
970 uint8_t count
, vrf_id_t vrf_id
, afi_t afi
, int type
,
973 struct nhg_ctx
*ctx
= NULL
;
976 /* Increase our counter so we don't try to create
977 * an ID that already exists
981 ctx
= nhg_ctx_init(id
, nh
, grp
, vrf_id
, afi
, type
, count
);
982 nhg_ctx_set_op(ctx
, NHG_CTX_OP_NEW
);
984 /* Under statup conditions, we need to handle them immediately
985 * like we do for routes. Otherwise, we are going to get a route
986 * with a nhe_id that we have not handled.
989 return nhg_ctx_process(ctx
);
991 if (queue_add(ctx
)) {
992 nhg_ctx_process_finish(ctx
);
999 /* Kernel-side, received delete message */
1000 int zebra_nhg_kernel_del(uint32_t id
)
1002 struct nhg_ctx
*ctx
= NULL
;
1004 ctx
= nhg_ctx_init(id
, NULL
, NULL
, 0, 0, 0, 0);
1006 nhg_ctx_set_op(ctx
, NHG_CTX_OP_DEL
);
1008 if (queue_add(ctx
)) {
1009 nhg_ctx_process_finish(ctx
);
1016 /* Some dependency helper functions */
1017 static struct nhg_hash_entry
*depends_find(struct nexthop
*nh
, afi_t afi
)
1019 struct nexthop
*lookup
= NULL
;
1020 struct nhg_hash_entry
*nhe
= NULL
;
1022 copy_nexthops(&lookup
, nh
, NULL
);
1024 /* Clear it, in case its a group */
1025 nexthops_free(lookup
->next
);
1026 nexthops_free(lookup
->prev
);
1027 lookup
->next
= NULL
;
1028 lookup
->prev
= NULL
;
1030 nhe
= zebra_nhg_find_nexthop(0, lookup
, afi
, 0);
1032 nexthops_free(lookup
);
1037 static void depends_add(struct nhg_connected_tree_head
*head
,
1038 struct nhg_hash_entry
*depend
)
1040 nhg_connected_tree_add_nhe(head
, depend
);
1041 zebra_nhg_increment_ref(depend
);
1044 static struct nhg_hash_entry
*
1045 depends_find_add(struct nhg_connected_tree_head
*head
, struct nexthop
*nh
,
1048 struct nhg_hash_entry
*depend
= NULL
;
1050 depend
= depends_find(nh
, afi
);
1053 depends_add(head
, depend
);
1058 static struct nhg_hash_entry
*
1059 depends_find_id_add(struct nhg_connected_tree_head
*head
, uint32_t id
)
1061 struct nhg_hash_entry
*depend
= NULL
;
1063 depend
= zebra_nhg_lookup_id(id
);
1066 depends_add(head
, depend
);
1071 static void depends_decrement_free(struct nhg_connected_tree_head
*head
)
1073 nhg_connected_tree_decrement_ref(head
);
1074 nhg_connected_tree_free(head
);
1077 /* Rib-side, you get a nexthop group struct */
1078 struct nhg_hash_entry
*
1079 zebra_nhg_rib_find(uint32_t id
, struct nexthop_group
*nhg
, afi_t rt_afi
)
1081 struct nhg_hash_entry
*nhe
= NULL
;
1083 if (!(nhg
&& nhg
->nexthop
)) {
1084 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
1085 "No nexthop passed to %s", __func__
);
1089 zebra_nhg_find(&nhe
, id
, nhg
, NULL
, nhg
->nexthop
->vrf_id
, rt_afi
, 0);
1094 void zebra_nhg_free_members(struct nhg_hash_entry
*nhe
)
1096 nexthop_group_delete(&nhe
->nhg
);
1097 /* Decrement to remove connection ref */
1098 nhg_connected_tree_decrement_ref(&nhe
->nhg_depends
);
1099 nhg_connected_tree_free(&nhe
->nhg_depends
);
1100 nhg_connected_tree_free(&nhe
->nhg_dependents
);
1103 void zebra_nhg_free(void *arg
)
1105 struct nhg_hash_entry
*nhe
= NULL
;
1107 nhe
= (struct nhg_hash_entry
*)arg
;
1110 zlog_debug("nhe_id=%u hash refcnt=%d", nhe
->id
, nhe
->refcnt
);
1112 zebra_nhg_free_members(nhe
);
1114 XFREE(MTYPE_NHG
, nhe
);
1117 void zebra_nhg_decrement_ref(struct nhg_hash_entry
*nhe
)
1121 if (!zebra_nhg_depends_is_empty(nhe
))
1122 nhg_connected_tree_decrement_ref(&nhe
->nhg_depends
);
1124 if (ZEBRA_NHG_CREATED(nhe
) && nhe
->refcnt
<= 0)
1125 zebra_nhg_uninstall_kernel(nhe
);
1128 void zebra_nhg_increment_ref(struct nhg_hash_entry
*nhe
)
1132 if (!zebra_nhg_depends_is_empty(nhe
))
1133 nhg_connected_tree_increment_ref(&nhe
->nhg_depends
);
1136 void zebra_nhg_set_if(struct nhg_hash_entry
*nhe
, struct interface
*ifp
)
1139 if_nhg_dependents_add(ifp
, nhe
);
1142 static void nexthop_set_resolved(afi_t afi
, const struct nexthop
*newhop
,
1143 struct nexthop
*nexthop
)
1145 struct nexthop
*resolved_hop
;
1146 uint8_t num_labels
= 0;
1147 mpls_label_t labels
[MPLS_MAX_LABELS
];
1148 enum lsp_types_t label_type
= ZEBRA_LSP_NONE
;
1151 resolved_hop
= nexthop_new();
1152 SET_FLAG(resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1154 resolved_hop
->vrf_id
= nexthop
->vrf_id
;
1155 switch (newhop
->type
) {
1156 case NEXTHOP_TYPE_IPV4
:
1157 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1158 /* If the resolving route specifies a gateway, use it */
1159 resolved_hop
->type
= newhop
->type
;
1160 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
1162 if (newhop
->ifindex
) {
1163 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1164 resolved_hop
->ifindex
= newhop
->ifindex
;
1167 case NEXTHOP_TYPE_IPV6
:
1168 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1169 resolved_hop
->type
= newhop
->type
;
1170 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
1172 if (newhop
->ifindex
) {
1173 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1174 resolved_hop
->ifindex
= newhop
->ifindex
;
1177 case NEXTHOP_TYPE_IFINDEX
:
1178 /* If the resolving route is an interface route,
1179 * it means the gateway we are looking up is connected
1180 * to that interface. (The actual network is _not_ onlink).
1181 * Therefore, the resolved route should have the original
1182 * gateway as nexthop as it is directly connected.
1184 * On Linux, we have to set the onlink netlink flag because
1185 * otherwise, the kernel won't accept the route.
1187 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
1188 if (afi
== AFI_IP
) {
1189 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1190 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
1191 } else if (afi
== AFI_IP6
) {
1192 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1193 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
1195 resolved_hop
->ifindex
= newhop
->ifindex
;
1197 case NEXTHOP_TYPE_BLACKHOLE
:
1198 resolved_hop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
1199 resolved_hop
->bh_type
= newhop
->bh_type
;
1203 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
1204 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
1206 /* Copy labels of the resolved route and the parent resolving to it */
1207 if (newhop
->nh_label
) {
1208 for (i
= 0; i
< newhop
->nh_label
->num_labels
; i
++)
1209 labels
[num_labels
++] = newhop
->nh_label
->label
[i
];
1210 label_type
= newhop
->nh_label_type
;
1213 if (nexthop
->nh_label
) {
1214 for (i
= 0; i
< nexthop
->nh_label
->num_labels
; i
++)
1215 labels
[num_labels
++] = nexthop
->nh_label
->label
[i
];
1217 /* If the parent has labels, use its type */
1218 label_type
= nexthop
->nh_label_type
;
1222 nexthop_add_labels(resolved_hop
, label_type
, num_labels
,
1225 resolved_hop
->rparent
= nexthop
;
1226 _nexthop_add(&nexthop
->resolved
, resolved_hop
);
1229 /* Checks if nexthop we are trying to resolve to is valid */
1230 static bool nexthop_valid_resolve(const struct nexthop
*nexthop
,
1231 const struct nexthop
*resolved
)
1233 /* Can't resolve to a recursive nexthop */
1234 if (CHECK_FLAG(resolved
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1237 switch (nexthop
->type
) {
1238 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1239 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1240 /* If the nexthop we are resolving to does not match the
1241 * ifindex for the nexthop the route wanted, its not valid.
1243 if (nexthop
->ifindex
!= resolved
->ifindex
)
1246 case NEXTHOP_TYPE_IPV4
:
1247 case NEXTHOP_TYPE_IPV6
:
1248 case NEXTHOP_TYPE_IFINDEX
:
1249 case NEXTHOP_TYPE_BLACKHOLE
:
1257 * Given a nexthop we need to properly recursively resolve
1258 * the route. As such, do a table lookup to find and match
1259 * if at all possible. Set the nexthop->ifindex and resolved_id
1262 static int nexthop_active(afi_t afi
, struct route_entry
*re
,
1263 struct nexthop
*nexthop
, struct route_node
*top
)
1266 struct route_table
*table
;
1267 struct route_node
*rn
;
1268 struct route_entry
*match
= NULL
;
1270 struct nexthop
*newhop
;
1271 struct interface
*ifp
;
1273 struct zebra_vrf
*zvrf
;
1275 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
)
1276 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
1277 nexthop
->ifindex
= 0;
1280 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
1281 nexthops_free(nexthop
->resolved
);
1282 nexthop
->resolved
= NULL
;
1283 re
->nexthop_mtu
= 0;
1286 * If the kernel has sent us a NEW route, then
1287 * by golly gee whiz it's a good route.
1289 * If its an already INSTALLED route we have already handled, then the
1290 * kernel route's nexthop might have became unreachable
1291 * and we have to handle that.
1293 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
1294 && (re
->type
== ZEBRA_ROUTE_KERNEL
1295 || re
->type
== ZEBRA_ROUTE_SYSTEM
))
1299 * Check to see if we should trust the passed in information
1300 * for UNNUMBERED interfaces as that we won't find the GW
1301 * address in the routing table.
1302 * This check should suffice to handle IPv4 or IPv6 routes
1303 * sourced from EVPN routes which are installed with the
1304 * next hop as the remote VTEP IP.
1306 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)) {
1307 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
1309 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1311 "\t%s: Onlink and interface: %u[%u] does not exist",
1312 __PRETTY_FUNCTION__
, nexthop
->ifindex
,
1316 if (connected_is_unnumbered(ifp
)) {
1317 if (if_is_operative(ifp
))
1320 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1322 "\t%s: Onlink and interface %s is not operative",
1323 __PRETTY_FUNCTION__
, ifp
->name
);
1326 if (!if_is_operative(ifp
)) {
1327 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1329 "\t%s: Interface %s is not unnumbered",
1330 __PRETTY_FUNCTION__
, ifp
->name
);
1335 /* Make lookup prefix. */
1336 memset(&p
, 0, sizeof(struct prefix
));
1340 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1341 p
.u
.prefix4
= nexthop
->gate
.ipv4
;
1344 p
.family
= AF_INET6
;
1345 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
1346 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
1349 assert(afi
!= AFI_IP
&& afi
!= AFI_IP6
);
1353 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, nexthop
->vrf_id
);
1355 zvrf
= zebra_vrf_lookup_by_id(nexthop
->vrf_id
);
1356 if (!table
|| !zvrf
) {
1357 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1358 zlog_debug("\t%s: Table not found",
1359 __PRETTY_FUNCTION__
);
1363 rn
= route_node_match(table
, (struct prefix
*)&p
);
1365 route_unlock_node(rn
);
1367 /* Lookup should halt if we've matched against ourselves ('top',
1368 * if specified) - i.e., we cannot have a nexthop NH1 is
1369 * resolved by a route NH1. The exception is if the route is a
1372 if (top
&& rn
== top
)
1373 if (((afi
== AFI_IP
) && (rn
->p
.prefixlen
!= 32))
1374 || ((afi
== AFI_IP6
) && (rn
->p
.prefixlen
!= 128))) {
1375 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1377 "\t%s: Matched against ourself and prefix length is not max bit length",
1378 __PRETTY_FUNCTION__
);
1382 /* Pick up selected route. */
1383 /* However, do not resolve over default route unless explicitly
1386 if (is_default_prefix(&rn
->p
)
1387 && !rnh_resolve_via_default(zvrf
, p
.family
)) {
1388 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1390 "\t:%s: Resolved against default route",
1391 __PRETTY_FUNCTION__
);
1395 dest
= rib_dest_from_rnode(rn
);
1396 if (dest
&& dest
->selected_fib
1397 && !CHECK_FLAG(dest
->selected_fib
->status
,
1398 ROUTE_ENTRY_REMOVED
)
1399 && dest
->selected_fib
->type
!= ZEBRA_ROUTE_TABLE
)
1400 match
= dest
->selected_fib
;
1402 /* If there is no selected route or matched route is EGP, go up
1408 } while (rn
&& rn
->info
== NULL
);
1410 route_lock_node(rn
);
1415 if (match
->type
== ZEBRA_ROUTE_CONNECT
) {
1416 /* Directly point connected route. */
1417 newhop
= match
->ng
->nexthop
;
1419 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1420 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
1421 nexthop
->ifindex
= newhop
->ifindex
;
1424 } else if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_ALLOW_RECURSION
)) {
1426 for (ALL_NEXTHOPS_PTR(match
->ng
, newhop
)) {
1427 if (!CHECK_FLAG(match
->status
,
1428 ROUTE_ENTRY_INSTALLED
))
1430 if (!nexthop_valid_resolve(nexthop
, newhop
))
1433 SET_FLAG(nexthop
->flags
,
1434 NEXTHOP_FLAG_RECURSIVE
);
1435 nexthop_set_resolved(afi
, newhop
, nexthop
);
1439 re
->nexthop_mtu
= match
->mtu
;
1441 if (!resolved
&& IS_ZEBRA_DEBUG_RIB_DETAILED
)
1442 zlog_debug("\t%s: Recursion failed to find",
1443 __PRETTY_FUNCTION__
);
1445 } else if (re
->type
== ZEBRA_ROUTE_STATIC
) {
1447 for (ALL_NEXTHOPS_PTR(match
->ng
, newhop
)) {
1448 if (!CHECK_FLAG(match
->status
,
1449 ROUTE_ENTRY_INSTALLED
))
1451 if (!nexthop_valid_resolve(nexthop
, newhop
))
1454 SET_FLAG(nexthop
->flags
,
1455 NEXTHOP_FLAG_RECURSIVE
);
1456 nexthop_set_resolved(afi
, newhop
, nexthop
);
1460 re
->nexthop_mtu
= match
->mtu
;
1462 if (!resolved
&& IS_ZEBRA_DEBUG_RIB_DETAILED
)
1464 "\t%s: Static route unable to resolve",
1465 __PRETTY_FUNCTION__
);
1468 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1470 "\t%s: Route Type %s has not turned on recursion",
1471 __PRETTY_FUNCTION__
,
1472 zebra_route_string(re
->type
));
1473 if (re
->type
== ZEBRA_ROUTE_BGP
1474 && !CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
1476 "\tEBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\"");
1481 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1482 zlog_debug("\t%s: Nexthop did not lookup in table",
1483 __PRETTY_FUNCTION__
);
1487 /* This function verifies reachability of one given nexthop, which can be
1488 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
1489 * in nexthop->flags field. The nexthop->ifindex will be updated
1490 * appropriately as well. An existing route map can turn
1491 * (otherwise active) nexthop into inactive, but not vice versa.
1493 * If it finds a nexthop recursivedly, set the resolved_id
1494 * to match that nexthop's nhg_hash_entry ID;
1496 * The return value is the final value of 'ACTIVE' flag.
1498 static unsigned nexthop_active_check(struct route_node
*rn
,
1499 struct route_entry
*re
,
1500 struct nexthop
*nexthop
)
1502 struct interface
*ifp
;
1503 route_map_result_t ret
= RMAP_PERMITMATCH
;
1505 char buf
[SRCDEST2STR_BUFFER
];
1506 const struct prefix
*p
, *src_p
;
1507 struct zebra_vrf
*zvrf
;
1509 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1511 if (rn
->p
.family
== AF_INET
)
1513 else if (rn
->p
.family
== AF_INET6
)
1517 switch (nexthop
->type
) {
1518 case NEXTHOP_TYPE_IFINDEX
:
1519 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
1520 if (ifp
&& if_is_operative(ifp
))
1521 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1523 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1525 case NEXTHOP_TYPE_IPV4
:
1526 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1528 if (nexthop_active(AFI_IP
, re
, nexthop
, rn
))
1529 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1531 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1533 case NEXTHOP_TYPE_IPV6
:
1535 if (nexthop_active(AFI_IP6
, re
, nexthop
, rn
))
1536 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1538 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1540 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1541 /* RFC 5549, v4 prefix with v6 NH */
1542 if (rn
->p
.family
!= AF_INET
)
1544 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
1545 ifp
= if_lookup_by_index(nexthop
->ifindex
,
1547 if (ifp
&& if_is_operative(ifp
))
1548 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1550 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1552 if (nexthop_active(AFI_IP6
, re
, nexthop
, rn
))
1553 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1555 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1558 case NEXTHOP_TYPE_BLACKHOLE
:
1559 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1564 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
1565 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1566 zlog_debug("\t%s: Unable to find a active nexthop",
1567 __PRETTY_FUNCTION__
);
1571 /* XXX: What exactly do those checks do? Do we support
1572 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
1574 if (RIB_SYSTEM_ROUTE(re
) || (family
== AFI_IP
&& p
->family
!= AF_INET
)
1575 || (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
1576 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1578 /* The original code didn't determine the family correctly
1579 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
1580 * from the rib_table_info in those cases.
1581 * Possibly it may be better to use only the rib_table_info
1585 rib_table_info_t
*info
;
1587 info
= srcdest_rnode_table_info(rn
);
1591 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
1593 zvrf
= zebra_vrf_lookup_by_id(nexthop
->vrf_id
);
1595 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1596 zlog_debug("\t%s: zvrf is NULL", __PRETTY_FUNCTION__
);
1597 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1600 /* It'll get set if required inside */
1601 ret
= zebra_route_map_check(family
, re
->type
, re
->instance
, p
, nexthop
,
1603 if (ret
== RMAP_DENYMATCH
) {
1604 if (IS_ZEBRA_DEBUG_RIB
) {
1605 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1607 "%u:%s: Filtering out with NH out %s due to route map",
1609 ifindex2ifname(nexthop
->ifindex
,
1612 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1614 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1618 * Iterate over all nexthops of the given RIB entry and refresh their
1619 * ACTIVE flag. If any nexthop is found to toggle the ACTIVE flag,
1620 * the whole re structure is flagged with ROUTE_ENTRY_CHANGED.
1622 * Return value is the new number of active nexthops.
1624 int nexthop_active_update(struct route_node
*rn
, struct route_entry
*re
)
1626 struct nexthop_group new_grp
= {};
1627 struct nexthop
*nexthop
;
1628 union g_addr prev_src
;
1629 unsigned int prev_active
, new_active
;
1630 ifindex_t prev_index
;
1631 uint8_t curr_active
= 0;
1633 afi_t rt_afi
= family2afi(rn
->p
.family
);
1635 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1637 /* Copy over the nexthops in current state */
1638 nexthop_group_copy(&new_grp
, re
->ng
);
1640 for (nexthop
= new_grp
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
1642 /* No protocol daemon provides src and so we're skipping
1644 prev_src
= nexthop
->rmap_src
;
1645 prev_active
= CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1646 prev_index
= nexthop
->ifindex
;
1648 * We need to respect the multipath_num here
1649 * as that what we should be able to install from
1650 * a multipath perpsective should not be a data plane
1654 nexthop_active_check(rn
, re
, nexthop
);
1657 && nexthop_group_active_nexthop_num(&new_grp
)
1658 >= zrouter
.multipath_num
) {
1659 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1666 /* Don't allow src setting on IPv6 addr for now */
1667 if (prev_active
!= new_active
|| prev_index
!= nexthop
->ifindex
1668 || ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
1669 && nexthop
->type
< NEXTHOP_TYPE_IPV6
)
1670 && prev_src
.ipv4
.s_addr
1671 != nexthop
->rmap_src
.ipv4
.s_addr
)
1672 || ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
1673 && nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
)
1674 && !(IPV6_ADDR_SAME(&prev_src
.ipv6
,
1675 &nexthop
->rmap_src
.ipv6
)))
1676 || CHECK_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
))
1677 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1680 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
1681 struct nhg_hash_entry
*new_nhe
= NULL
;
1683 new_nhe
= zebra_nhg_rib_find(0, &new_grp
, rt_afi
);
1685 zebra_nhg_re_update_ref(re
, new_nhe
);
1689 struct nhg_hash_entry
*nhe
= NULL
;
1691 nhe
= zebra_nhg_lookup_id(re
->nhe_id
);
1694 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
1697 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
1698 "Active update on NHE id=%u that we do not have in our tables",
1703 * Do not need these nexthops anymore since they
1704 * were either copied over into an nhe or not
1707 nexthops_free(new_grp
.nexthop
);
1711 static void zebra_nhg_re_attach_ref(struct route_entry
*re
,
1712 struct nhg_hash_entry
*new)
1715 re
->nhe_id
= new->id
;
1717 zebra_nhg_increment_ref(new);
1720 int zebra_nhg_re_update_ref(struct route_entry
*re
, struct nhg_hash_entry
*new)
1722 struct nhg_hash_entry
*old
= NULL
;
1730 if (re
->nhe_id
!= new->id
) {
1731 old
= zebra_nhg_lookup_id(re
->nhe_id
);
1733 zebra_nhg_re_attach_ref(re
, new);
1736 zebra_nhg_decrement_ref(old
);
1738 /* This is the first time it's being attached */
1739 zebra_nhg_re_attach_ref(re
, new);
1745 /* Convert a nhe into a group array */
1746 uint8_t zebra_nhg_nhe2grp(struct nh_grp
*grp
, struct nhg_hash_entry
*nhe
,
1749 struct nhg_connected
*rb_node_dep
= NULL
;
1750 struct nhg_hash_entry
*depend
= NULL
;
1753 frr_each(nhg_connected_tree
, &nhe
->nhg_depends
, rb_node_dep
) {
1754 bool duplicate
= false;
1756 depend
= rb_node_dep
->nhe
;
1759 * If its recursive, use its resolved nhe in the group
1761 if (CHECK_FLAG(depend
->flags
, NEXTHOP_GROUP_RECURSIVE
)) {
1762 depend
= zebra_nhg_resolve(depend
);
1765 EC_ZEBRA_NHG_FIB_UPDATE
,
1766 "Failed to recursively resolve Nexthop Hash Entry in the group id=%u",
1772 /* Check for duplicate IDs, kernel doesn't like that */
1773 for (int j
= 0; j
< i
; j
++) {
1774 if (depend
->id
== grp
[j
].id
)
1779 grp
[i
].id
= depend
->id
;
1780 /* We aren't using weights for anything right now */
1793 void zebra_nhg_install_kernel(struct nhg_hash_entry
*nhe
)
1795 struct nhg_connected
*rb_node_dep
= NULL
;
1797 /* Resolve it first */
1798 nhe
= zebra_nhg_resolve(nhe
);
1800 /* Make sure all depends are installed/queued */
1801 frr_each(nhg_connected_tree
, &nhe
->nhg_depends
, rb_node_dep
) {
1802 zebra_nhg_install_kernel(rb_node_dep
->nhe
);
1805 if (!CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
)
1806 && !CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_QUEUED
)) {
1807 /* Change its type to us since we are installing it */
1808 nhe
->type
= ZEBRA_ROUTE_NHG
;
1810 int ret
= dplane_nexthop_add(nhe
);
1813 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1814 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_QUEUED
);
1816 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1818 EC_ZEBRA_DP_INSTALL_FAIL
,
1819 "Failed to install Nexthop ID (%u) into the kernel",
1822 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1823 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
1824 zebra_nhg_handle_install(nhe
);
1830 void zebra_nhg_uninstall_kernel(struct nhg_hash_entry
*nhe
)
1832 if (CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
)) {
1833 int ret
= dplane_nexthop_delete(nhe
);
1836 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1837 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_QUEUED
);
1839 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1841 EC_ZEBRA_DP_DELETE_FAIL
,
1842 "Failed to uninstall Nexthop ID (%u) from the kernel",
1845 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1846 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
1851 zebra_nhg_handle_uninstall(nhe
);
1854 void zebra_nhg_dplane_result(struct zebra_dplane_ctx
*ctx
)
1856 enum dplane_op_e op
;
1857 enum zebra_dplane_result status
;
1859 struct nhg_hash_entry
*nhe
= NULL
;
1861 op
= dplane_ctx_get_op(ctx
);
1862 status
= dplane_ctx_get_status(ctx
);
1864 id
= dplane_ctx_get_nhe_id(ctx
);
1866 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1868 "Nexthop dplane ctx %p, op %s, nexthop ID (%u), result %s",
1869 ctx
, dplane_op2str(op
), id
, dplane_res2str(status
));
1872 case DPLANE_OP_NH_DELETE
:
1873 if (status
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
1875 EC_ZEBRA_DP_DELETE_FAIL
,
1876 "Failed to uninstall Nexthop ID (%u) from the kernel",
1878 /* We already free'd the data, nothing to do */
1880 case DPLANE_OP_NH_INSTALL
:
1881 case DPLANE_OP_NH_UPDATE
:
1882 nhe
= zebra_nhg_lookup_id(id
);
1887 "%s operation preformed on Nexthop ID (%u) in the kernel, that we no longer have in our table",
1888 dplane_op2str(op
), id
);
1892 UNSET_FLAG(nhe
->flags
, NEXTHOP_GROUP_QUEUED
);
1893 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1894 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_VALID
);
1895 SET_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
);
1896 zebra_nhg_handle_install(nhe
);
1899 EC_ZEBRA_DP_INSTALL_FAIL
,
1900 "Failed to install Nexthop ID (%u) into the kernel",
1903 case DPLANE_OP_ROUTE_INSTALL
:
1904 case DPLANE_OP_ROUTE_UPDATE
:
1905 case DPLANE_OP_ROUTE_DELETE
:
1906 case DPLANE_OP_ROUTE_NOTIFY
:
1907 case DPLANE_OP_LSP_INSTALL
:
1908 case DPLANE_OP_LSP_UPDATE
:
1909 case DPLANE_OP_LSP_DELETE
:
1910 case DPLANE_OP_LSP_NOTIFY
:
1911 case DPLANE_OP_PW_INSTALL
:
1912 case DPLANE_OP_PW_UNINSTALL
:
1913 case DPLANE_OP_SYS_ROUTE_ADD
:
1914 case DPLANE_OP_SYS_ROUTE_DELETE
:
1915 case DPLANE_OP_ADDR_INSTALL
:
1916 case DPLANE_OP_ADDR_UNINSTALL
:
1917 case DPLANE_OP_MAC_INSTALL
:
1918 case DPLANE_OP_MAC_DELETE
:
1919 case DPLANE_OP_NEIGH_INSTALL
:
1920 case DPLANE_OP_NEIGH_UPDATE
:
1921 case DPLANE_OP_NEIGH_DELETE
:
1922 case DPLANE_OP_VTEP_ADD
:
1923 case DPLANE_OP_VTEP_DELETE
:
1924 case DPLANE_OP_NONE
:
1928 dplane_ctx_fini(&ctx
);
1931 static void zebra_nhg_sweep_entry(struct hash_bucket
*bucket
, void *arg
)
1933 struct nhg_hash_entry
*nhe
= NULL
;
1935 nhe
= (struct nhg_hash_entry
*)bucket
->data
;
1937 /* If its being ref'd, just let it be uninstalled via a route removal */
1938 if (ZEBRA_NHG_CREATED(nhe
) && nhe
->refcnt
<= 0)
1939 zebra_nhg_uninstall_kernel(nhe
);
1942 void zebra_nhg_sweep_table(struct hash
*hash
)
1944 hash_iterate(hash
, zebra_nhg_sweep_entry
, NULL
);