]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_nhg.c
Merge pull request #11408 from donaldsharp/common_config
[mirror_frr.git] / zebra / zebra_nhg.c
1 /* Zebra Nexthop Group Code.
2 * Copyright (C) 2019 Cumulus Networks, Inc.
3 * Donald Sharp
4 * Stephen Worley
5 *
6 * This file is part of FRR.
7 *
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
11 * later version.
12 *
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.
17 *
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
21 * 02111-1307, USA.
22 */
23 #include <zebra.h>
24
25 #include "lib/nexthop.h"
26 #include "lib/nexthop_group_private.h"
27 #include "lib/routemap.h"
28 #include "lib/mpls.h"
29 #include "lib/jhash.h"
30 #include "lib/debug.h"
31 #include "lib/lib_errors.h"
32
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"
41 #include "zebra/rt.h"
42 #include "zebra_errors.h"
43 #include "zebra_dplane.h"
44 #include "zebra/interface.h"
45 #include "zebra/zapi_msg.h"
46 #include "zebra/rib.h"
47
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");
51
52 /* Map backup nexthop indices between two nhes */
53 struct backup_nh_map_s {
54 int map_count;
55
56 struct {
57 uint8_t orig_idx;
58 uint8_t new_idx;
59 } map[MULTIPATH_NUM];
60 };
61
62 /* id counter to keep in sync with kernel */
63 uint32_t id_counter;
64
65 /* Controlled through ui */
66 static bool g_nexthops_enabled = true;
67 static bool proto_nexthops_only;
68 static bool use_recursive_backups = true;
69
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);
80
81 static struct nhg_backup_info *
82 nhg_backup_copy(const struct nhg_backup_info *orig);
83
84 /* Helper function for getting the next allocatable ID */
85 static uint32_t nhg_get_next_id(void)
86 {
87 while (1) {
88 id_counter++;
89
90 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
91 zlog_debug("%s: ID %u checking", __func__, id_counter);
92
93 if (id_counter == ZEBRA_NHG_PROTO_LOWER) {
94 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
95 zlog_debug("%s: ID counter wrapped", __func__);
96
97 id_counter = 0;
98 continue;
99 }
100
101 if (zebra_nhg_lookup_id(id_counter)) {
102 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
103 zlog_debug("%s: ID already exists", __func__);
104
105 continue;
106 }
107
108 break;
109 }
110
111 return id_counter;
112 }
113
114 static void nhg_connected_free(struct nhg_connected *dep)
115 {
116 XFREE(MTYPE_NHG_CONNECTED, dep);
117 }
118
119 static struct nhg_connected *nhg_connected_new(struct nhg_hash_entry *nhe)
120 {
121 struct nhg_connected *new = NULL;
122
123 new = XCALLOC(MTYPE_NHG_CONNECTED, sizeof(struct nhg_connected));
124 new->nhe = nhe;
125
126 return new;
127 }
128
129 void nhg_connected_tree_free(struct nhg_connected_tree_head *head)
130 {
131 struct nhg_connected *rb_node_dep = NULL;
132
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);
137 }
138 }
139 }
140
141 bool nhg_connected_tree_is_empty(const struct nhg_connected_tree_head *head)
142 {
143 return nhg_connected_tree_count(head) ? false : true;
144 }
145
146 struct nhg_connected *
147 nhg_connected_tree_root(struct nhg_connected_tree_head *head)
148 {
149 return nhg_connected_tree_first(head);
150 }
151
152 struct nhg_hash_entry *
153 nhg_connected_tree_del_nhe(struct nhg_connected_tree_head *head,
154 struct nhg_hash_entry *depend)
155 {
156 struct nhg_connected lookup = {};
157 struct nhg_connected *remove = NULL;
158 struct nhg_hash_entry *removed_nhe;
159
160 lookup.nhe = depend;
161
162 /* Lookup to find the element, then remove it */
163 remove = nhg_connected_tree_find(head, &lookup);
164 if (remove)
165 /* Re-returning here just in case this API changes..
166 * the _del list api's are a bit undefined at the moment.
167 *
168 * So hopefully returning here will make it fail if the api
169 * changes to something different than currently expected.
170 */
171 remove = nhg_connected_tree_del(head, remove);
172
173 /* If the entry was sucessfully removed, free the 'connected` struct */
174 if (remove) {
175 removed_nhe = remove->nhe;
176 nhg_connected_free(remove);
177 return removed_nhe;
178 }
179
180 return NULL;
181 }
182
183 /* Assuming UNIQUE RB tree. If this changes, assumptions here about
184 * insertion need to change.
185 */
186 struct nhg_hash_entry *
187 nhg_connected_tree_add_nhe(struct nhg_connected_tree_head *head,
188 struct nhg_hash_entry *depend)
189 {
190 struct nhg_connected *new = NULL;
191
192 new = nhg_connected_new(depend);
193
194 /* On success, NULL will be returned from the
195 * RB code.
196 */
197 if (new && (nhg_connected_tree_add(head, new) == NULL))
198 return NULL;
199
200 /* If it wasn't successful, it must be a duplicate. We enforce the
201 * unique property for the `nhg_connected` tree.
202 */
203 nhg_connected_free(new);
204
205 return depend;
206 }
207
208 static void
209 nhg_connected_tree_decrement_ref(struct nhg_connected_tree_head *head)
210 {
211 struct nhg_connected *rb_node_dep = NULL;
212
213 frr_each_safe(nhg_connected_tree, head, rb_node_dep) {
214 zebra_nhg_decrement_ref(rb_node_dep->nhe);
215 }
216 }
217
218 static void
219 nhg_connected_tree_increment_ref(struct nhg_connected_tree_head *head)
220 {
221 struct nhg_connected *rb_node_dep = NULL;
222
223 frr_each(nhg_connected_tree, head, rb_node_dep) {
224 zebra_nhg_increment_ref(rb_node_dep->nhe);
225 }
226 }
227
228 struct nhg_hash_entry *zebra_nhg_resolve(struct nhg_hash_entry *nhe)
229 {
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);
234 }
235
236 return nhe;
237 }
238
239 unsigned int zebra_nhg_depends_count(const struct nhg_hash_entry *nhe)
240 {
241 return nhg_connected_tree_count(&nhe->nhg_depends);
242 }
243
244 bool zebra_nhg_depends_is_empty(const struct nhg_hash_entry *nhe)
245 {
246 return nhg_connected_tree_is_empty(&nhe->nhg_depends);
247 }
248
249 static void zebra_nhg_depends_del(struct nhg_hash_entry *from,
250 struct nhg_hash_entry *depend)
251 {
252 nhg_connected_tree_del_nhe(&from->nhg_depends, depend);
253 }
254
255 static void zebra_nhg_depends_init(struct nhg_hash_entry *nhe)
256 {
257 nhg_connected_tree_init(&nhe->nhg_depends);
258 }
259
260 unsigned int zebra_nhg_dependents_count(const struct nhg_hash_entry *nhe)
261 {
262 return nhg_connected_tree_count(&nhe->nhg_dependents);
263 }
264
265
266 bool zebra_nhg_dependents_is_empty(const struct nhg_hash_entry *nhe)
267 {
268 return nhg_connected_tree_is_empty(&nhe->nhg_dependents);
269 }
270
271 static void zebra_nhg_dependents_del(struct nhg_hash_entry *from,
272 struct nhg_hash_entry *dependent)
273 {
274 nhg_connected_tree_del_nhe(&from->nhg_dependents, dependent);
275 }
276
277 static void zebra_nhg_dependents_add(struct nhg_hash_entry *to,
278 struct nhg_hash_entry *dependent)
279 {
280 nhg_connected_tree_add_nhe(&to->nhg_dependents, dependent);
281 }
282
283 static void zebra_nhg_dependents_init(struct nhg_hash_entry *nhe)
284 {
285 nhg_connected_tree_init(&nhe->nhg_dependents);
286 }
287
288 /* Release this nhe from anything depending on it */
289 static void zebra_nhg_dependents_release(struct nhg_hash_entry *nhe)
290 {
291 struct nhg_connected *rb_node_dep = NULL;
292
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);
297 }
298 }
299
300 /* Release this nhe from anything that it depends on */
301 static void zebra_nhg_depends_release(struct nhg_hash_entry *nhe)
302 {
303 if (!zebra_nhg_depends_is_empty(nhe)) {
304 struct nhg_connected *rb_node_dep = NULL;
305
306 frr_each_safe(nhg_connected_tree, &nhe->nhg_depends,
307 rb_node_dep) {
308 zebra_nhg_dependents_del(rb_node_dep->nhe, nhe);
309 }
310 }
311 }
312
313
314 struct nhg_hash_entry *zebra_nhg_lookup_id(uint32_t id)
315 {
316 struct nhg_hash_entry lookup = {};
317
318 lookup.id = id;
319 return hash_lookup(zrouter.nhgs_id, &lookup);
320 }
321
322 static int zebra_nhg_insert_id(struct nhg_hash_entry *nhe)
323 {
324 if (hash_lookup(zrouter.nhgs_id, nhe)) {
325 flog_err(
326 EC_ZEBRA_NHG_TABLE_INSERT_FAILED,
327 "Failed inserting NHG %pNG into the ID hash table, entry already exists",
328 nhe);
329 return -1;
330 }
331
332 (void)hash_get(zrouter.nhgs_id, nhe, hash_alloc_intern);
333
334 return 0;
335 }
336
337 static void zebra_nhg_set_if(struct nhg_hash_entry *nhe, struct interface *ifp)
338 {
339 nhe->ifp = ifp;
340 if_nhg_dependents_add(ifp, nhe);
341 }
342
343 static void
344 zebra_nhg_connect_depends(struct nhg_hash_entry *nhe,
345 struct nhg_connected_tree_head *nhg_depends)
346 {
347 struct nhg_connected *rb_node_dep = NULL;
348
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.
353 */
354 nhe->nhg_depends = *nhg_depends;
355
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 (%pNG), dep %p (%pNG)",
362 __func__, nhe, nhe, rb_node_dep->nhe,
363 rb_node_dep->nhe);
364
365 zebra_nhg_dependents_add(rb_node_dep->nhe, nhe);
366 }
367 }
368 }
369
370 /* Init an nhe, for use in a hash lookup for example */
371 void zebra_nhe_init(struct nhg_hash_entry *nhe, afi_t afi,
372 const struct nexthop *nh)
373 {
374 memset(nhe, 0, sizeof(struct nhg_hash_entry));
375 nhe->vrf_id = VRF_DEFAULT;
376 nhe->type = ZEBRA_ROUTE_NHG;
377 nhe->afi = AFI_UNSPEC;
378
379 /* There are some special rules that apply to groups representing
380 * a single nexthop.
381 */
382 if (nh && (nh->next == NULL)) {
383 switch (nh->type) {
384 case NEXTHOP_TYPE_IFINDEX:
385 case NEXTHOP_TYPE_BLACKHOLE:
386 /*
387 * This switch case handles setting the afi different
388 * for ipv4/v6 routes. Ifindex/blackhole nexthop
389 * objects cannot be ambiguous, they must be Address
390 * Family specific. If we get here, we will either use
391 * the AF of the route, or the one we got passed from
392 * here from the kernel.
393 */
394 nhe->afi = afi;
395 break;
396 case NEXTHOP_TYPE_IPV4_IFINDEX:
397 case NEXTHOP_TYPE_IPV4:
398 nhe->afi = AFI_IP;
399 break;
400 case NEXTHOP_TYPE_IPV6_IFINDEX:
401 case NEXTHOP_TYPE_IPV6:
402 nhe->afi = AFI_IP6;
403 break;
404 }
405 }
406 }
407
408 struct nhg_hash_entry *zebra_nhg_alloc(void)
409 {
410 struct nhg_hash_entry *nhe;
411
412 nhe = XCALLOC(MTYPE_NHG, sizeof(struct nhg_hash_entry));
413
414 return nhe;
415 }
416
417 /*
418 * Allocate new nhe and make shallow copy of 'orig'; no
419 * recursive info is copied.
420 */
421 struct nhg_hash_entry *zebra_nhe_copy(const struct nhg_hash_entry *orig,
422 uint32_t id)
423 {
424 struct nhg_hash_entry *nhe;
425
426 nhe = zebra_nhg_alloc();
427
428 nhe->id = id;
429
430 nexthop_group_copy(&(nhe->nhg), &(orig->nhg));
431
432 nhe->vrf_id = orig->vrf_id;
433 nhe->afi = orig->afi;
434 nhe->type = orig->type ? orig->type : ZEBRA_ROUTE_NHG;
435 nhe->refcnt = 0;
436 nhe->dplane_ref = zebra_router_get_next_sequence();
437
438 /* Copy backup info also, if present */
439 if (orig->backup_info)
440 nhe->backup_info = nhg_backup_copy(orig->backup_info);
441
442 return nhe;
443 }
444
445 /* Allocation via hash handler */
446 static void *zebra_nhg_hash_alloc(void *arg)
447 {
448 struct nhg_hash_entry *nhe = NULL;
449 struct nhg_hash_entry *copy = arg;
450
451 nhe = zebra_nhe_copy(copy, copy->id);
452
453 /* Mark duplicate nexthops in a group at creation time. */
454 nexthop_group_mark_duplicates(&(nhe->nhg));
455
456 /*
457 * Add the ifp now if it's not a group or recursive and has ifindex.
458 *
459 * A proto-owned ID is always a group.
460 */
461 if (!PROTO_OWNED(nhe) && nhe->nhg.nexthop && !nhe->nhg.nexthop->next
462 && !nhe->nhg.nexthop->resolved && nhe->nhg.nexthop->ifindex) {
463 struct interface *ifp = NULL;
464
465 ifp = if_lookup_by_index(nhe->nhg.nexthop->ifindex,
466 nhe->nhg.nexthop->vrf_id);
467 if (ifp)
468 zebra_nhg_set_if(nhe, ifp);
469 else {
470 if (IS_ZEBRA_DEBUG_NHG)
471 zlog_debug(
472 "Failed to lookup an interface with ifindex=%d in vrf=%u for NHE %pNG",
473 nhe->nhg.nexthop->ifindex,
474 nhe->nhg.nexthop->vrf_id, nhe);
475 }
476 }
477
478 return nhe;
479 }
480
481 uint32_t zebra_nhg_hash_key(const void *arg)
482 {
483 const struct nhg_hash_entry *nhe = arg;
484 uint32_t key = 0x5a351234;
485 uint32_t primary = 0;
486 uint32_t backup = 0;
487
488 primary = nexthop_group_hash(&(nhe->nhg));
489 if (nhe->backup_info)
490 backup = nexthop_group_hash(&(nhe->backup_info->nhe->nhg));
491
492 key = jhash_3words(primary, backup, nhe->type, key);
493
494 key = jhash_2words(nhe->vrf_id, nhe->afi, key);
495
496 return key;
497 }
498
499 uint32_t zebra_nhg_id_key(const void *arg)
500 {
501 const struct nhg_hash_entry *nhe = arg;
502
503 return nhe->id;
504 }
505
506 /* Helper with common nhg/nhe nexthop comparison logic */
507 static bool nhg_compare_nexthops(const struct nexthop *nh1,
508 const struct nexthop *nh2)
509 {
510 assert(nh1 != NULL && nh2 != NULL);
511
512 /*
513 * We have to check the active flag of each individual one,
514 * not just the overall active_num. This solves the special case
515 * issue of a route with a nexthop group with one nexthop
516 * resolving to itself and thus marking it inactive. If we
517 * have two different routes each wanting to mark a different
518 * nexthop inactive, they need to hash to two different groups.
519 *
520 * If we just hashed on num_active, they would hash the same
521 * which is incorrect.
522 *
523 * ex)
524 * 1.1.1.0/24
525 * -> 1.1.1.1 dummy1 (inactive)
526 * -> 1.1.2.1 dummy2
527 *
528 * 1.1.2.0/24
529 * -> 1.1.1.1 dummy1
530 * -> 1.1.2.1 dummy2 (inactive)
531 *
532 * Without checking each individual one, they would hash to
533 * the same group and both have 1.1.1.1 dummy1 marked inactive.
534 *
535 */
536 if (CHECK_FLAG(nh1->flags, NEXTHOP_FLAG_ACTIVE)
537 != CHECK_FLAG(nh2->flags, NEXTHOP_FLAG_ACTIVE))
538 return false;
539
540 if (!nexthop_same(nh1, nh2))
541 return false;
542
543 return true;
544 }
545
546 bool zebra_nhg_hash_equal(const void *arg1, const void *arg2)
547 {
548 const struct nhg_hash_entry *nhe1 = arg1;
549 const struct nhg_hash_entry *nhe2 = arg2;
550 struct nexthop *nexthop1;
551 struct nexthop *nexthop2;
552
553 /* No matter what if they equal IDs, assume equal */
554 if (nhe1->id && nhe2->id && (nhe1->id == nhe2->id))
555 return true;
556
557 if (nhe1->type != nhe2->type)
558 return false;
559
560 if (nhe1->vrf_id != nhe2->vrf_id)
561 return false;
562
563 if (nhe1->afi != nhe2->afi)
564 return false;
565
566 /* Nexthops should be in-order, so we simply compare them in-place */
567 for (nexthop1 = nhe1->nhg.nexthop, nexthop2 = nhe2->nhg.nexthop;
568 nexthop1 && nexthop2;
569 nexthop1 = nexthop1->next, nexthop2 = nexthop2->next) {
570
571 if (!nhg_compare_nexthops(nexthop1, nexthop2))
572 return false;
573 }
574
575 /* Check for unequal list lengths */
576 if (nexthop1 || nexthop2)
577 return false;
578
579 /* If there's no backup info, comparison is done. */
580 if ((nhe1->backup_info == NULL) && (nhe2->backup_info == NULL))
581 return true;
582
583 /* Compare backup info also - test the easy things first */
584 if (nhe1->backup_info && (nhe2->backup_info == NULL))
585 return false;
586 if (nhe2->backup_info && (nhe1->backup_info == NULL))
587 return false;
588
589 /* Compare number of backups before actually comparing any */
590 for (nexthop1 = nhe1->backup_info->nhe->nhg.nexthop,
591 nexthop2 = nhe2->backup_info->nhe->nhg.nexthop;
592 nexthop1 && nexthop2;
593 nexthop1 = nexthop1->next, nexthop2 = nexthop2->next) {
594 ;
595 }
596
597 /* Did we find the end of one list before the other? */
598 if (nexthop1 || nexthop2)
599 return false;
600
601 /* Have to compare the backup nexthops */
602 for (nexthop1 = nhe1->backup_info->nhe->nhg.nexthop,
603 nexthop2 = nhe2->backup_info->nhe->nhg.nexthop;
604 nexthop1 && nexthop2;
605 nexthop1 = nexthop1->next, nexthop2 = nexthop2->next) {
606
607 if (!nhg_compare_nexthops(nexthop1, nexthop2))
608 return false;
609 }
610
611 return true;
612 }
613
614 bool zebra_nhg_hash_id_equal(const void *arg1, const void *arg2)
615 {
616 const struct nhg_hash_entry *nhe1 = arg1;
617 const struct nhg_hash_entry *nhe2 = arg2;
618
619 return nhe1->id == nhe2->id;
620 }
621
622 static int zebra_nhg_process_grp(struct nexthop_group *nhg,
623 struct nhg_connected_tree_head *depends,
624 struct nh_grp *grp, uint8_t count)
625 {
626 nhg_connected_tree_init(depends);
627
628 for (int i = 0; i < count; i++) {
629 struct nhg_hash_entry *depend = NULL;
630 /* We do not care about nexthop_grp.weight at
631 * this time. But we should figure out
632 * how to adapt this to our code in
633 * the future.
634 */
635 depend = depends_find_id_add(depends, grp[i].id);
636
637 if (!depend) {
638 flog_err(
639 EC_ZEBRA_NHG_SYNC,
640 "Received Nexthop Group from the kernel with a dependent Nexthop ID (%u) which we do not have in our table",
641 grp[i].id);
642 return -1;
643 }
644
645 /*
646 * If this is a nexthop with its own group
647 * dependencies, add them as well. Not sure its
648 * even possible to have a group within a group
649 * in the kernel.
650 */
651
652 copy_nexthops(&nhg->nexthop, depend->nhg.nexthop, NULL);
653 }
654
655 return 0;
656 }
657
658 static void handle_recursive_depend(struct nhg_connected_tree_head *nhg_depends,
659 struct nexthop *nh, afi_t afi, int type)
660 {
661 struct nhg_hash_entry *depend = NULL;
662 struct nexthop_group resolved_ng = {};
663
664 resolved_ng.nexthop = nh;
665
666 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
667 zlog_debug("%s: head %p, nh %pNHv",
668 __func__, nhg_depends, nh);
669
670 depend = zebra_nhg_rib_find(0, &resolved_ng, afi, type);
671
672 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
673 zlog_debug("%s: nh %pNHv => %p (%u)",
674 __func__, nh, depend,
675 depend ? depend->id : 0);
676
677 if (depend)
678 depends_add(nhg_depends, depend);
679 }
680
681 /*
682 * Lookup an nhe in the global hash, using data from another nhe. If 'lookup'
683 * has an id value, that's used. Create a new global/shared nhe if not found.
684 */
685 static bool zebra_nhe_find(struct nhg_hash_entry **nhe, /* return value */
686 struct nhg_hash_entry *lookup,
687 struct nhg_connected_tree_head *nhg_depends,
688 afi_t afi, bool from_dplane)
689 {
690 bool created = false;
691 bool recursive = false;
692 struct nhg_hash_entry *newnhe, *backup_nhe;
693 struct nexthop *nh = NULL;
694
695 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
696 zlog_debug(
697 "%s: id %u, lookup %p, vrf %d, type %d, depends %p%s",
698 __func__, lookup->id, lookup, lookup->vrf_id,
699 lookup->type, nhg_depends,
700 (from_dplane ? " (from dplane)" : ""));
701
702 if (lookup->id)
703 (*nhe) = zebra_nhg_lookup_id(lookup->id);
704 else
705 (*nhe) = hash_lookup(zrouter.nhgs, lookup);
706
707 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
708 zlog_debug("%s: lookup => %p (%pNG)", __func__, *nhe, *nhe);
709
710 /* If we found an existing object, we're done */
711 if (*nhe)
712 goto done;
713
714 /* We're going to create/insert a new nhe:
715 * assign the next global id value if necessary.
716 */
717 if (lookup->id == 0)
718 lookup->id = nhg_get_next_id();
719
720 if (!from_dplane && lookup->id < ZEBRA_NHG_PROTO_LOWER) {
721 /*
722 * This is a zebra hashed/owned NHG.
723 *
724 * It goes in HASH and ID table.
725 */
726 newnhe = hash_get(zrouter.nhgs, lookup, zebra_nhg_hash_alloc);
727 zebra_nhg_insert_id(newnhe);
728 } else {
729 /*
730 * This is upperproto owned NHG or one we read in from dataplane
731 * and should not be hashed to.
732 *
733 * It goes in ID table.
734 */
735 newnhe =
736 hash_get(zrouter.nhgs_id, lookup, zebra_nhg_hash_alloc);
737 }
738
739 created = true;
740
741 /* Mail back the new object */
742 *nhe = newnhe;
743
744 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
745 zlog_debug("%s: => created %p (%pNG)", __func__, newnhe,
746 newnhe);
747
748 /* Only hash/lookup the depends if the first lookup
749 * fails to find something. This should hopefully save a
750 * lot of cycles for larger ecmp sizes.
751 */
752 if (nhg_depends) {
753 /* If you don't want to hash on each nexthop in the
754 * nexthop group struct you can pass the depends
755 * directly. Kernel-side we do this since it just looks
756 * them up via IDs.
757 */
758 zebra_nhg_connect_depends(newnhe, nhg_depends);
759 goto done;
760 }
761
762 /* Prepare dependency relationships if this is not a
763 * singleton nexthop. There are two cases: a single
764 * recursive nexthop, where we need a relationship to the
765 * resolving nexthop; or a group of nexthops, where we need
766 * relationships with the corresponding singletons.
767 */
768 zebra_nhg_depends_init(newnhe);
769
770 nh = newnhe->nhg.nexthop;
771
772 if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_ACTIVE))
773 SET_FLAG(newnhe->flags, NEXTHOP_GROUP_VALID);
774
775 if (nh->next == NULL && newnhe->id < ZEBRA_NHG_PROTO_LOWER) {
776 if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE)) {
777 /* Single recursive nexthop */
778 handle_recursive_depend(&newnhe->nhg_depends,
779 nh->resolved, afi,
780 newnhe->type);
781 recursive = true;
782 }
783 } else {
784 /* Proto-owned are groups by default */
785 /* List of nexthops */
786 for (nh = newnhe->nhg.nexthop; nh; nh = nh->next) {
787 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
788 zlog_debug("%s: depends NH %pNHv %s",
789 __func__, nh,
790 CHECK_FLAG(nh->flags,
791 NEXTHOP_FLAG_RECURSIVE) ?
792 "(R)" : "");
793
794 depends_find_add(&newnhe->nhg_depends, nh, afi,
795 newnhe->type, from_dplane);
796 }
797 }
798
799 if (recursive)
800 SET_FLAG(newnhe->flags, NEXTHOP_GROUP_RECURSIVE);
801
802 /* Attach dependent backpointers to singletons */
803 zebra_nhg_connect_depends(newnhe, &newnhe->nhg_depends);
804
805 /**
806 * Backup Nexthops
807 */
808
809 if (zebra_nhg_get_backup_nhg(newnhe) == NULL ||
810 zebra_nhg_get_backup_nhg(newnhe)->nexthop == NULL)
811 goto done;
812
813 /* If there are backup nexthops, add them to the backup
814 * depends tree. The rules here are a little different.
815 */
816 recursive = false;
817 backup_nhe = newnhe->backup_info->nhe;
818
819 nh = backup_nhe->nhg.nexthop;
820
821 /* Singleton recursive NH */
822 if (nh->next == NULL &&
823 CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE)) {
824 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
825 zlog_debug("%s: backup depend NH %pNHv (R)",
826 __func__, nh);
827
828 /* Single recursive nexthop */
829 handle_recursive_depend(&backup_nhe->nhg_depends, nh->resolved,
830 afi, backup_nhe->type);
831 recursive = true;
832 } else {
833 /* One or more backup NHs */
834 for (; nh; nh = nh->next) {
835 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
836 zlog_debug("%s: backup depend NH %pNHv %s",
837 __func__, nh,
838 CHECK_FLAG(nh->flags,
839 NEXTHOP_FLAG_RECURSIVE) ?
840 "(R)" : "");
841
842 depends_find_add(&backup_nhe->nhg_depends, nh, afi,
843 backup_nhe->type, from_dplane);
844 }
845 }
846
847 if (recursive)
848 SET_FLAG(backup_nhe->flags, NEXTHOP_GROUP_RECURSIVE);
849
850 done:
851 /* Reset time since last update */
852 (*nhe)->uptime = monotime(NULL);
853
854 return created;
855 }
856
857 /*
858 * Lookup or create an nhe, based on an nhg or an nhe id.
859 */
860 static bool zebra_nhg_find(struct nhg_hash_entry **nhe, uint32_t id,
861 struct nexthop_group *nhg,
862 struct nhg_connected_tree_head *nhg_depends,
863 vrf_id_t vrf_id, afi_t afi, int type,
864 bool from_dplane)
865 {
866 struct nhg_hash_entry lookup = {};
867 bool created = false;
868
869 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
870 zlog_debug("%s: id %u, nhg %p, vrf %d, type %d, depends %p",
871 __func__, id, nhg, vrf_id, type,
872 nhg_depends);
873
874 /* Use a temporary nhe and call into the superset/common code */
875 lookup.id = id;
876 lookup.type = type ? type : ZEBRA_ROUTE_NHG;
877 lookup.nhg = *nhg;
878
879 lookup.vrf_id = vrf_id;
880 if (nhg_depends || lookup.nhg.nexthop->next) {
881 /* Groups can have all vrfs and AF's in them */
882 lookup.afi = AFI_UNSPEC;
883 } else {
884 switch (lookup.nhg.nexthop->type) {
885 case (NEXTHOP_TYPE_IFINDEX):
886 case (NEXTHOP_TYPE_BLACKHOLE):
887 /*
888 * This switch case handles setting the afi different
889 * for ipv4/v6 routes. Ifindex/blackhole nexthop
890 * objects cannot be ambiguous, they must be Address
891 * Family specific. If we get here, we will either use
892 * the AF of the route, or the one we got passed from
893 * here from the kernel.
894 */
895 lookup.afi = afi;
896 break;
897 case (NEXTHOP_TYPE_IPV4_IFINDEX):
898 case (NEXTHOP_TYPE_IPV4):
899 lookup.afi = AFI_IP;
900 break;
901 case (NEXTHOP_TYPE_IPV6_IFINDEX):
902 case (NEXTHOP_TYPE_IPV6):
903 lookup.afi = AFI_IP6;
904 break;
905 }
906 }
907
908 created = zebra_nhe_find(nhe, &lookup, nhg_depends, afi, from_dplane);
909
910 return created;
911 }
912
913 /* Find/create a single nexthop */
914 static struct nhg_hash_entry *zebra_nhg_find_nexthop(uint32_t id,
915 struct nexthop *nh,
916 afi_t afi, int type,
917 bool from_dplane)
918 {
919 struct nhg_hash_entry *nhe = NULL;
920 struct nexthop_group nhg = {};
921 vrf_id_t vrf_id = !vrf_is_backend_netns() ? VRF_DEFAULT : nh->vrf_id;
922
923 nexthop_group_add_sorted(&nhg, nh);
924
925 zebra_nhg_find(&nhe, id, &nhg, NULL, vrf_id, afi, type, from_dplane);
926
927 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
928 zlog_debug("%s: nh %pNHv => %p (%pNG)", __func__, nh, nhe, nhe);
929
930 return nhe;
931 }
932
933 static uint32_t nhg_ctx_get_id(const struct nhg_ctx *ctx)
934 {
935 return ctx->id;
936 }
937
938 static void nhg_ctx_set_status(struct nhg_ctx *ctx, enum nhg_ctx_status status)
939 {
940 ctx->status = status;
941 }
942
943 static enum nhg_ctx_status nhg_ctx_get_status(const struct nhg_ctx *ctx)
944 {
945 return ctx->status;
946 }
947
948 static void nhg_ctx_set_op(struct nhg_ctx *ctx, enum nhg_ctx_op_e op)
949 {
950 ctx->op = op;
951 }
952
953 static enum nhg_ctx_op_e nhg_ctx_get_op(const struct nhg_ctx *ctx)
954 {
955 return ctx->op;
956 }
957
958 static vrf_id_t nhg_ctx_get_vrf_id(const struct nhg_ctx *ctx)
959 {
960 return ctx->vrf_id;
961 }
962
963 static int nhg_ctx_get_type(const struct nhg_ctx *ctx)
964 {
965 return ctx->type;
966 }
967
968 static int nhg_ctx_get_afi(const struct nhg_ctx *ctx)
969 {
970 return ctx->afi;
971 }
972
973 static struct nexthop *nhg_ctx_get_nh(struct nhg_ctx *ctx)
974 {
975 return &ctx->u.nh;
976 }
977
978 static uint8_t nhg_ctx_get_count(const struct nhg_ctx *ctx)
979 {
980 return ctx->count;
981 }
982
983 static struct nh_grp *nhg_ctx_get_grp(struct nhg_ctx *ctx)
984 {
985 return ctx->u.grp;
986 }
987
988 static struct nhg_ctx *nhg_ctx_new(void)
989 {
990 struct nhg_ctx *new;
991
992 new = XCALLOC(MTYPE_NHG_CTX, sizeof(struct nhg_ctx));
993
994 return new;
995 }
996
997 void nhg_ctx_free(struct nhg_ctx **ctx)
998 {
999 struct nexthop *nh;
1000
1001 if (ctx == NULL)
1002 return;
1003
1004 assert((*ctx) != NULL);
1005
1006 if (nhg_ctx_get_count(*ctx))
1007 goto done;
1008
1009 nh = nhg_ctx_get_nh(*ctx);
1010
1011 nexthop_del_labels(nh);
1012 nexthop_del_srv6_seg6local(nh);
1013 nexthop_del_srv6_seg6(nh);
1014
1015 done:
1016 XFREE(MTYPE_NHG_CTX, *ctx);
1017 }
1018
1019 static struct nhg_ctx *nhg_ctx_init(uint32_t id, struct nexthop *nh,
1020 struct nh_grp *grp, vrf_id_t vrf_id,
1021 afi_t afi, int type, uint8_t count)
1022 {
1023 struct nhg_ctx *ctx = NULL;
1024
1025 ctx = nhg_ctx_new();
1026
1027 ctx->id = id;
1028 ctx->vrf_id = vrf_id;
1029 ctx->afi = afi;
1030 ctx->type = type;
1031 ctx->count = count;
1032
1033 if (count)
1034 /* Copy over the array */
1035 memcpy(&ctx->u.grp, grp, count * sizeof(struct nh_grp));
1036 else if (nh)
1037 ctx->u.nh = *nh;
1038
1039 return ctx;
1040 }
1041
1042 static void zebra_nhg_set_valid(struct nhg_hash_entry *nhe)
1043 {
1044 struct nhg_connected *rb_node_dep;
1045
1046 SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID);
1047
1048 frr_each(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep)
1049 zebra_nhg_set_valid(rb_node_dep->nhe);
1050 }
1051
1052 static void zebra_nhg_set_invalid(struct nhg_hash_entry *nhe)
1053 {
1054 struct nhg_connected *rb_node_dep;
1055
1056 UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID);
1057
1058 /* Update validity of nexthops depending on it */
1059 frr_each(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep)
1060 zebra_nhg_check_valid(rb_node_dep->nhe);
1061 }
1062
1063 void zebra_nhg_check_valid(struct nhg_hash_entry *nhe)
1064 {
1065 struct nhg_connected *rb_node_dep = NULL;
1066 bool valid = false;
1067
1068 /* If anthing else in the group is valid, the group is valid */
1069 frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) {
1070 if (CHECK_FLAG(rb_node_dep->nhe->flags, NEXTHOP_GROUP_VALID)) {
1071 valid = true;
1072 goto done;
1073 }
1074 }
1075
1076 done:
1077 if (valid)
1078 zebra_nhg_set_valid(nhe);
1079 else
1080 zebra_nhg_set_invalid(nhe);
1081 }
1082
1083 static void zebra_nhg_release_all_deps(struct nhg_hash_entry *nhe)
1084 {
1085 /* Remove it from any lists it may be on */
1086 zebra_nhg_depends_release(nhe);
1087 zebra_nhg_dependents_release(nhe);
1088 if (nhe->ifp)
1089 if_nhg_dependents_del(nhe->ifp, nhe);
1090 }
1091
1092 static void zebra_nhg_release(struct nhg_hash_entry *nhe)
1093 {
1094 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1095 zlog_debug("%s: nhe %p (%pNG)", __func__, nhe, nhe);
1096
1097 zebra_nhg_release_all_deps(nhe);
1098
1099 /*
1100 * If its not zebra owned, we didn't store it here and have to be
1101 * sure we don't clear one thats actually being used.
1102 */
1103 if (nhe->id < ZEBRA_NHG_PROTO_LOWER)
1104 hash_release(zrouter.nhgs, nhe);
1105
1106 hash_release(zrouter.nhgs_id, nhe);
1107 }
1108
1109 static void zebra_nhg_handle_uninstall(struct nhg_hash_entry *nhe)
1110 {
1111 zebra_nhg_release(nhe);
1112 zebra_nhg_free(nhe);
1113 }
1114
1115 static void zebra_nhg_handle_install(struct nhg_hash_entry *nhe)
1116 {
1117 /* Update validity of groups depending on it */
1118 struct nhg_connected *rb_node_dep;
1119
1120 frr_each_safe(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep)
1121 zebra_nhg_set_valid(rb_node_dep->nhe);
1122 }
1123
1124 /*
1125 * The kernel/other program has changed the state of a nexthop object we are
1126 * using.
1127 */
1128 static void zebra_nhg_handle_kernel_state_change(struct nhg_hash_entry *nhe,
1129 bool is_delete)
1130 {
1131 if (nhe->refcnt) {
1132 flog_err(
1133 EC_ZEBRA_NHG_SYNC,
1134 "Kernel %s a nexthop group with ID (%pNG) that we are still using for a route, sending it back down",
1135 (is_delete ? "deleted" : "updated"), nhe);
1136
1137 UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
1138 zebra_nhg_install_kernel(nhe);
1139 } else
1140 zebra_nhg_handle_uninstall(nhe);
1141 }
1142
1143 static int nhg_ctx_process_new(struct nhg_ctx *ctx)
1144 {
1145 struct nexthop_group *nhg = NULL;
1146 struct nhg_connected_tree_head nhg_depends = {};
1147 struct nhg_hash_entry *lookup = NULL;
1148 struct nhg_hash_entry *nhe = NULL;
1149
1150 uint32_t id = nhg_ctx_get_id(ctx);
1151 uint8_t count = nhg_ctx_get_count(ctx);
1152 vrf_id_t vrf_id = nhg_ctx_get_vrf_id(ctx);
1153 int type = nhg_ctx_get_type(ctx);
1154 afi_t afi = nhg_ctx_get_afi(ctx);
1155
1156 lookup = zebra_nhg_lookup_id(id);
1157
1158 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1159 zlog_debug("%s: id %u, count %d, lookup => %p",
1160 __func__, id, count, lookup);
1161
1162 if (lookup) {
1163 /* This is already present in our table, hence an update
1164 * that we did not initate.
1165 */
1166 zebra_nhg_handle_kernel_state_change(lookup, false);
1167 return 0;
1168 }
1169
1170 if (nhg_ctx_get_count(ctx)) {
1171 nhg = nexthop_group_new();
1172 if (zebra_nhg_process_grp(nhg, &nhg_depends,
1173 nhg_ctx_get_grp(ctx), count)) {
1174 depends_decrement_free(&nhg_depends);
1175 nexthop_group_delete(&nhg);
1176 return -ENOENT;
1177 }
1178
1179 if (!zebra_nhg_find(&nhe, id, nhg, &nhg_depends, vrf_id, afi,
1180 type, true))
1181 depends_decrement_free(&nhg_depends);
1182
1183 /* These got copied over in zebra_nhg_alloc() */
1184 nexthop_group_delete(&nhg);
1185 } else
1186 nhe = zebra_nhg_find_nexthop(id, nhg_ctx_get_nh(ctx), afi, type,
1187 true);
1188
1189 if (!nhe) {
1190 flog_err(
1191 EC_ZEBRA_TABLE_LOOKUP_FAILED,
1192 "Zebra failed to find or create a nexthop hash entry for ID (%u)",
1193 id);
1194 return -1;
1195 }
1196
1197 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1198 zlog_debug("%s: nhe %p (%pNG) is new", __func__, nhe, nhe);
1199
1200 /*
1201 * If daemon nhg from the kernel, add a refcnt here to indicate the
1202 * daemon owns it.
1203 */
1204 if (PROTO_OWNED(nhe))
1205 zebra_nhg_increment_ref(nhe);
1206
1207 SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID);
1208 SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
1209
1210 return 0;
1211 }
1212
1213 static int nhg_ctx_process_del(struct nhg_ctx *ctx)
1214 {
1215 struct nhg_hash_entry *nhe = NULL;
1216 uint32_t id = nhg_ctx_get_id(ctx);
1217
1218 nhe = zebra_nhg_lookup_id(id);
1219
1220 if (!nhe) {
1221 flog_warn(
1222 EC_ZEBRA_BAD_NHG_MESSAGE,
1223 "Kernel delete message received for nexthop group ID (%u) that we do not have in our ID table",
1224 id);
1225 return -1;
1226 }
1227
1228 zebra_nhg_handle_kernel_state_change(nhe, true);
1229
1230 return 0;
1231 }
1232
1233 static void nhg_ctx_fini(struct nhg_ctx **ctx)
1234 {
1235 /*
1236 * Just freeing for now, maybe do something more in the future
1237 * based on flag.
1238 */
1239
1240 nhg_ctx_free(ctx);
1241 }
1242
1243 static int queue_add(struct nhg_ctx *ctx)
1244 {
1245 /* If its queued or already processed do nothing */
1246 if (nhg_ctx_get_status(ctx) == NHG_CTX_QUEUED)
1247 return 0;
1248
1249 if (rib_queue_nhg_ctx_add(ctx)) {
1250 nhg_ctx_set_status(ctx, NHG_CTX_FAILURE);
1251 return -1;
1252 }
1253
1254 nhg_ctx_set_status(ctx, NHG_CTX_QUEUED);
1255
1256 return 0;
1257 }
1258
1259 int nhg_ctx_process(struct nhg_ctx *ctx)
1260 {
1261 int ret = 0;
1262
1263 switch (nhg_ctx_get_op(ctx)) {
1264 case NHG_CTX_OP_NEW:
1265 ret = nhg_ctx_process_new(ctx);
1266 if (nhg_ctx_get_count(ctx) && ret == -ENOENT
1267 && nhg_ctx_get_status(ctx) != NHG_CTX_REQUEUED) {
1268 /**
1269 * We have entered a situation where we are
1270 * processing a group from the kernel
1271 * that has a contained nexthop which
1272 * we have not yet processed.
1273 *
1274 * Re-enqueue this ctx to be handled exactly one
1275 * more time (indicated by the flag).
1276 *
1277 * By the time we get back to it, we
1278 * should have processed its depends.
1279 */
1280 nhg_ctx_set_status(ctx, NHG_CTX_NONE);
1281 if (queue_add(ctx) == 0) {
1282 nhg_ctx_set_status(ctx, NHG_CTX_REQUEUED);
1283 return 0;
1284 }
1285 }
1286 break;
1287 case NHG_CTX_OP_DEL:
1288 ret = nhg_ctx_process_del(ctx);
1289 case NHG_CTX_OP_NONE:
1290 break;
1291 }
1292
1293 nhg_ctx_set_status(ctx, (ret ? NHG_CTX_FAILURE : NHG_CTX_SUCCESS));
1294
1295 nhg_ctx_fini(&ctx);
1296
1297 return ret;
1298 }
1299
1300 /* Kernel-side, you either get a single new nexthop or a array of ID's */
1301 int zebra_nhg_kernel_find(uint32_t id, struct nexthop *nh, struct nh_grp *grp,
1302 uint8_t count, vrf_id_t vrf_id, afi_t afi, int type,
1303 int startup)
1304 {
1305 struct nhg_ctx *ctx = NULL;
1306
1307 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1308 zlog_debug("%s: nh %pNHv, id %u, count %d",
1309 __func__, nh, id, (int)count);
1310
1311 if (id > id_counter && id < ZEBRA_NHG_PROTO_LOWER)
1312 /* Increase our counter so we don't try to create
1313 * an ID that already exists
1314 */
1315 id_counter = id;
1316
1317 ctx = nhg_ctx_init(id, nh, grp, vrf_id, afi, type, count);
1318 nhg_ctx_set_op(ctx, NHG_CTX_OP_NEW);
1319
1320 /* Under statup conditions, we need to handle them immediately
1321 * like we do for routes. Otherwise, we are going to get a route
1322 * with a nhe_id that we have not handled.
1323 */
1324 if (startup)
1325 return nhg_ctx_process(ctx);
1326
1327 if (queue_add(ctx)) {
1328 nhg_ctx_fini(&ctx);
1329 return -1;
1330 }
1331
1332 return 0;
1333 }
1334
1335 /* Kernel-side, received delete message */
1336 int zebra_nhg_kernel_del(uint32_t id, vrf_id_t vrf_id)
1337 {
1338 struct nhg_ctx *ctx = NULL;
1339
1340 ctx = nhg_ctx_init(id, NULL, NULL, vrf_id, 0, 0, 0);
1341
1342 nhg_ctx_set_op(ctx, NHG_CTX_OP_DEL);
1343
1344 if (queue_add(ctx)) {
1345 nhg_ctx_fini(&ctx);
1346 return -1;
1347 }
1348
1349 return 0;
1350 }
1351
1352 /* Some dependency helper functions */
1353 static struct nhg_hash_entry *depends_find_recursive(const struct nexthop *nh,
1354 afi_t afi, int type)
1355 {
1356 struct nhg_hash_entry *nhe;
1357 struct nexthop *lookup = NULL;
1358
1359 lookup = nexthop_dup(nh, NULL);
1360
1361 nhe = zebra_nhg_find_nexthop(0, lookup, afi, type, false);
1362
1363 nexthops_free(lookup);
1364
1365 return nhe;
1366 }
1367
1368 static struct nhg_hash_entry *depends_find_singleton(const struct nexthop *nh,
1369 afi_t afi, int type,
1370 bool from_dplane)
1371 {
1372 struct nhg_hash_entry *nhe;
1373 struct nexthop lookup = {};
1374
1375 /* Capture a snapshot of this single nh; it might be part of a list,
1376 * so we need to make a standalone copy.
1377 */
1378 nexthop_copy_no_recurse(&lookup, nh, NULL);
1379
1380 nhe = zebra_nhg_find_nexthop(0, &lookup, afi, type, from_dplane);
1381
1382 /* The copy may have allocated labels; free them if necessary. */
1383 nexthop_del_labels(&lookup);
1384 nexthop_del_srv6_seg6local(&lookup);
1385 nexthop_del_srv6_seg6(&lookup);
1386
1387 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1388 zlog_debug("%s: nh %pNHv => %p (%pNG)", __func__, nh, nhe, nhe);
1389
1390 return nhe;
1391 }
1392
1393 static struct nhg_hash_entry *depends_find(const struct nexthop *nh, afi_t afi,
1394 int type, bool from_dplane)
1395 {
1396 struct nhg_hash_entry *nhe = NULL;
1397
1398 if (!nh)
1399 goto done;
1400
1401 /* We are separating these functions out to increase handling speed
1402 * in the non-recursive case (by not alloc/freeing)
1403 */
1404 if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE))
1405 nhe = depends_find_recursive(nh, afi, type);
1406 else
1407 nhe = depends_find_singleton(nh, afi, type, from_dplane);
1408
1409
1410 if (IS_ZEBRA_DEBUG_NHG_DETAIL) {
1411 zlog_debug("%s: nh %pNHv %s => %p (%pNG)", __func__, nh,
1412 CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE) ? "(R)"
1413 : "",
1414 nhe, nhe);
1415 }
1416
1417 done:
1418 return nhe;
1419 }
1420
1421 static void depends_add(struct nhg_connected_tree_head *head,
1422 struct nhg_hash_entry *depend)
1423 {
1424 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1425 zlog_debug("%s: head %p nh %pNHv",
1426 __func__, head, depend->nhg.nexthop);
1427
1428 /* If NULL is returned, it was successfully added and
1429 * needs to have its refcnt incremented.
1430 *
1431 * Else the NHE is already present in the tree and doesn't
1432 * need to increment the refcnt.
1433 */
1434 if (nhg_connected_tree_add_nhe(head, depend) == NULL)
1435 zebra_nhg_increment_ref(depend);
1436 }
1437
1438 static struct nhg_hash_entry *
1439 depends_find_add(struct nhg_connected_tree_head *head, struct nexthop *nh,
1440 afi_t afi, int type, bool from_dplane)
1441 {
1442 struct nhg_hash_entry *depend = NULL;
1443
1444 depend = depends_find(nh, afi, type, from_dplane);
1445
1446 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1447 zlog_debug("%s: nh %pNHv => %p",
1448 __func__, nh, depend);
1449
1450 if (depend)
1451 depends_add(head, depend);
1452
1453 return depend;
1454 }
1455
1456 static struct nhg_hash_entry *
1457 depends_find_id_add(struct nhg_connected_tree_head *head, uint32_t id)
1458 {
1459 struct nhg_hash_entry *depend = NULL;
1460
1461 depend = zebra_nhg_lookup_id(id);
1462
1463 if (depend)
1464 depends_add(head, depend);
1465
1466 return depend;
1467 }
1468
1469 static void depends_decrement_free(struct nhg_connected_tree_head *head)
1470 {
1471 nhg_connected_tree_decrement_ref(head);
1472 nhg_connected_tree_free(head);
1473 }
1474
1475 /* Find an nhe based on a list of nexthops */
1476 struct nhg_hash_entry *zebra_nhg_rib_find(uint32_t id,
1477 struct nexthop_group *nhg,
1478 afi_t rt_afi, int type)
1479 {
1480 struct nhg_hash_entry *nhe = NULL;
1481 vrf_id_t vrf_id;
1482
1483 /*
1484 * CLANG SA is complaining that nexthop may be NULL
1485 * Make it happy but this is ridonc
1486 */
1487 assert(nhg->nexthop);
1488 vrf_id = !vrf_is_backend_netns() ? VRF_DEFAULT : nhg->nexthop->vrf_id;
1489
1490 zebra_nhg_find(&nhe, id, nhg, NULL, vrf_id, rt_afi, type, false);
1491
1492 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1493 zlog_debug("%s: => nhe %p (%pNG)", __func__, nhe, nhe);
1494
1495 return nhe;
1496 }
1497
1498 /* Find an nhe based on a route's nhe */
1499 struct nhg_hash_entry *
1500 zebra_nhg_rib_find_nhe(struct nhg_hash_entry *rt_nhe, afi_t rt_afi)
1501 {
1502 struct nhg_hash_entry *nhe = NULL;
1503
1504 if (!(rt_nhe && rt_nhe->nhg.nexthop)) {
1505 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED,
1506 "No nexthop passed to %s", __func__);
1507 return NULL;
1508 }
1509
1510 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1511 zlog_debug("%s: rt_nhe %p (%pNG)", __func__, rt_nhe, rt_nhe);
1512
1513 zebra_nhe_find(&nhe, rt_nhe, NULL, rt_afi, false);
1514
1515 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1516 zlog_debug("%s: => nhe %p (%pNG)", __func__, nhe, nhe);
1517
1518 return nhe;
1519 }
1520
1521 /*
1522 * Allocate backup nexthop info object. Typically these are embedded in
1523 * nhg_hash_entry objects.
1524 */
1525 struct nhg_backup_info *zebra_nhg_backup_alloc(void)
1526 {
1527 struct nhg_backup_info *p;
1528
1529 p = XCALLOC(MTYPE_NHG, sizeof(struct nhg_backup_info));
1530
1531 p->nhe = zebra_nhg_alloc();
1532
1533 /* Identify the embedded group used to hold the list of backups */
1534 SET_FLAG(p->nhe->flags, NEXTHOP_GROUP_BACKUP);
1535
1536 return p;
1537 }
1538
1539 /*
1540 * Free backup nexthop info object, deal with any embedded allocations
1541 */
1542 void zebra_nhg_backup_free(struct nhg_backup_info **p)
1543 {
1544 if (p && *p) {
1545 if ((*p)->nhe)
1546 zebra_nhg_free((*p)->nhe);
1547
1548 XFREE(MTYPE_NHG, (*p));
1549 }
1550 }
1551
1552 /* Accessor for backup nexthop group */
1553 struct nexthop_group *zebra_nhg_get_backup_nhg(struct nhg_hash_entry *nhe)
1554 {
1555 struct nexthop_group *p = NULL;
1556
1557 if (nhe) {
1558 if (nhe->backup_info && nhe->backup_info->nhe)
1559 p = &(nhe->backup_info->nhe->nhg);
1560 }
1561
1562 return p;
1563 }
1564
1565 /*
1566 * Helper to return a copy of a backup_info - note that this is a shallow
1567 * copy, meant to be used when creating a new nhe from info passed in with
1568 * a route e.g.
1569 */
1570 static struct nhg_backup_info *
1571 nhg_backup_copy(const struct nhg_backup_info *orig)
1572 {
1573 struct nhg_backup_info *b;
1574
1575 b = zebra_nhg_backup_alloc();
1576
1577 /* Copy list of nexthops */
1578 nexthop_group_copy(&(b->nhe->nhg), &(orig->nhe->nhg));
1579
1580 return b;
1581 }
1582
1583 static void zebra_nhg_free_members(struct nhg_hash_entry *nhe)
1584 {
1585 nexthops_free(nhe->nhg.nexthop);
1586
1587 zebra_nhg_backup_free(&nhe->backup_info);
1588
1589 /* Decrement to remove connection ref */
1590 nhg_connected_tree_decrement_ref(&nhe->nhg_depends);
1591 nhg_connected_tree_free(&nhe->nhg_depends);
1592 nhg_connected_tree_free(&nhe->nhg_dependents);
1593 }
1594
1595 void zebra_nhg_free(struct nhg_hash_entry *nhe)
1596 {
1597 if (IS_ZEBRA_DEBUG_NHG_DETAIL) {
1598 /* Group or singleton? */
1599 if (nhe->nhg.nexthop && nhe->nhg.nexthop->next)
1600 zlog_debug("%s: nhe %p (%pNG), refcnt %d", __func__,
1601 nhe, nhe, nhe->refcnt);
1602 else
1603 zlog_debug("%s: nhe %p (%pNG), refcnt %d, NH %pNHv",
1604 __func__, nhe, nhe, nhe->refcnt,
1605 nhe->nhg.nexthop);
1606 }
1607
1608 if (nhe->refcnt)
1609 zlog_debug("nhe_id=%pNG hash refcnt=%d", nhe, nhe->refcnt);
1610
1611 zebra_nhg_free_members(nhe);
1612
1613 XFREE(MTYPE_NHG, nhe);
1614 }
1615
1616 void zebra_nhg_hash_free(void *p)
1617 {
1618 zebra_nhg_release_all_deps((struct nhg_hash_entry *)p);
1619 zebra_nhg_free((struct nhg_hash_entry *)p);
1620 }
1621
1622 void zebra_nhg_decrement_ref(struct nhg_hash_entry *nhe)
1623 {
1624 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1625 zlog_debug("%s: nhe %p (%pNG) %d => %d", __func__, nhe, nhe,
1626 nhe->refcnt, nhe->refcnt - 1);
1627
1628 nhe->refcnt--;
1629
1630 if (!zebra_nhg_depends_is_empty(nhe))
1631 nhg_connected_tree_decrement_ref(&nhe->nhg_depends);
1632
1633 if (ZEBRA_NHG_CREATED(nhe) && nhe->refcnt <= 0)
1634 zebra_nhg_uninstall_kernel(nhe);
1635 }
1636
1637 void zebra_nhg_increment_ref(struct nhg_hash_entry *nhe)
1638 {
1639 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1640 zlog_debug("%s: nhe %p (%pNG) %d => %d", __func__, nhe, nhe,
1641 nhe->refcnt, nhe->refcnt + 1);
1642
1643 nhe->refcnt++;
1644
1645 if (!zebra_nhg_depends_is_empty(nhe))
1646 nhg_connected_tree_increment_ref(&nhe->nhg_depends);
1647 }
1648
1649 static struct nexthop *nexthop_set_resolved(afi_t afi,
1650 const struct nexthop *newhop,
1651 struct nexthop *nexthop,
1652 struct zebra_sr_policy *policy)
1653 {
1654 struct nexthop *resolved_hop;
1655 uint8_t num_labels = 0;
1656 mpls_label_t labels[MPLS_MAX_LABELS];
1657 enum lsp_types_t label_type = ZEBRA_LSP_NONE;
1658 int i = 0;
1659
1660 resolved_hop = nexthop_new();
1661 SET_FLAG(resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
1662
1663 resolved_hop->vrf_id = nexthop->vrf_id;
1664 switch (newhop->type) {
1665 case NEXTHOP_TYPE_IPV4:
1666 case NEXTHOP_TYPE_IPV4_IFINDEX:
1667 /* If the resolving route specifies a gateway, use it */
1668 resolved_hop->type = newhop->type;
1669 resolved_hop->gate.ipv4 = newhop->gate.ipv4;
1670
1671 if (newhop->ifindex) {
1672 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
1673 resolved_hop->ifindex = newhop->ifindex;
1674 }
1675 break;
1676 case NEXTHOP_TYPE_IPV6:
1677 case NEXTHOP_TYPE_IPV6_IFINDEX:
1678 resolved_hop->type = newhop->type;
1679 resolved_hop->gate.ipv6 = newhop->gate.ipv6;
1680
1681 if (newhop->ifindex) {
1682 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1683 resolved_hop->ifindex = newhop->ifindex;
1684 }
1685 break;
1686 case NEXTHOP_TYPE_IFINDEX:
1687 /* If the resolving route is an interface route,
1688 * it means the gateway we are looking up is connected
1689 * to that interface. (The actual network is _not_ onlink).
1690 * Therefore, the resolved route should have the original
1691 * gateway as nexthop as it is directly connected.
1692 *
1693 * On Linux, we have to set the onlink netlink flag because
1694 * otherwise, the kernel won't accept the route.
1695 */
1696 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
1697 if (afi == AFI_IP) {
1698 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
1699 resolved_hop->gate.ipv4 = nexthop->gate.ipv4;
1700 } else if (afi == AFI_IP6) {
1701 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1702 resolved_hop->gate.ipv6 = nexthop->gate.ipv6;
1703 }
1704 resolved_hop->ifindex = newhop->ifindex;
1705 break;
1706 case NEXTHOP_TYPE_BLACKHOLE:
1707 resolved_hop->type = NEXTHOP_TYPE_BLACKHOLE;
1708 resolved_hop->bh_type = newhop->bh_type;
1709 break;
1710 }
1711
1712 if (newhop->flags & NEXTHOP_FLAG_ONLINK)
1713 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
1714
1715 /* Copy labels of the resolved route and the parent resolving to it */
1716 if (policy) {
1717 int i = 0;
1718
1719 /*
1720 * Don't push the first SID if the corresponding action in the
1721 * LFIB is POP.
1722 */
1723 if (!newhop->nh_label || !newhop->nh_label->num_labels
1724 || newhop->nh_label->label[0] == MPLS_LABEL_IMPLICIT_NULL)
1725 i = 1;
1726
1727 for (; i < policy->segment_list.label_num; i++)
1728 labels[num_labels++] = policy->segment_list.labels[i];
1729 label_type = policy->segment_list.type;
1730 } else if (newhop->nh_label) {
1731 for (i = 0; i < newhop->nh_label->num_labels; i++) {
1732 /* Be a bit picky about overrunning the local array */
1733 if (num_labels >= MPLS_MAX_LABELS) {
1734 if (IS_ZEBRA_DEBUG_NHG || IS_ZEBRA_DEBUG_RIB)
1735 zlog_debug("%s: too many labels in newhop %pNHv",
1736 __func__, newhop);
1737 break;
1738 }
1739 labels[num_labels++] = newhop->nh_label->label[i];
1740 }
1741 /* Use the "outer" type */
1742 label_type = newhop->nh_label_type;
1743 }
1744
1745 if (nexthop->nh_label) {
1746 for (i = 0; i < nexthop->nh_label->num_labels; i++) {
1747 /* Be a bit picky about overrunning the local array */
1748 if (num_labels >= MPLS_MAX_LABELS) {
1749 if (IS_ZEBRA_DEBUG_NHG || IS_ZEBRA_DEBUG_RIB)
1750 zlog_debug("%s: too many labels in nexthop %pNHv",
1751 __func__, nexthop);
1752 break;
1753 }
1754 labels[num_labels++] = nexthop->nh_label->label[i];
1755 }
1756
1757 /* If the parent has labels, use its type if
1758 * we don't already have one.
1759 */
1760 if (label_type == ZEBRA_LSP_NONE)
1761 label_type = nexthop->nh_label_type;
1762 }
1763
1764 if (num_labels)
1765 nexthop_add_labels(resolved_hop, label_type, num_labels,
1766 labels);
1767
1768 if (nexthop->nh_srv6) {
1769 nexthop_add_srv6_seg6local(resolved_hop,
1770 nexthop->nh_srv6->seg6local_action,
1771 &nexthop->nh_srv6->seg6local_ctx);
1772 nexthop_add_srv6_seg6(resolved_hop,
1773 &nexthop->nh_srv6->seg6_segs);
1774 }
1775
1776 resolved_hop->rparent = nexthop;
1777 _nexthop_add(&nexthop->resolved, resolved_hop);
1778
1779 return resolved_hop;
1780 }
1781
1782 /* Checks if nexthop we are trying to resolve to is valid */
1783 static bool nexthop_valid_resolve(const struct nexthop *nexthop,
1784 const struct nexthop *resolved)
1785 {
1786 /* Can't resolve to a recursive nexthop */
1787 if (CHECK_FLAG(resolved->flags, NEXTHOP_FLAG_RECURSIVE))
1788 return false;
1789
1790 /* Must be ACTIVE */
1791 if (!CHECK_FLAG(resolved->flags, NEXTHOP_FLAG_ACTIVE))
1792 return false;
1793
1794 /* Must not be duplicate */
1795 if (CHECK_FLAG(resolved->flags, NEXTHOP_FLAG_DUPLICATE))
1796 return false;
1797
1798 switch (nexthop->type) {
1799 case NEXTHOP_TYPE_IPV4_IFINDEX:
1800 case NEXTHOP_TYPE_IPV6_IFINDEX:
1801 /* If the nexthop we are resolving to does not match the
1802 * ifindex for the nexthop the route wanted, its not valid.
1803 */
1804 if (nexthop->ifindex != resolved->ifindex)
1805 return false;
1806 break;
1807 case NEXTHOP_TYPE_IPV4:
1808 case NEXTHOP_TYPE_IPV6:
1809 case NEXTHOP_TYPE_IFINDEX:
1810 case NEXTHOP_TYPE_BLACKHOLE:
1811 break;
1812 }
1813
1814 return true;
1815 }
1816
1817 /*
1818 * When resolving a recursive nexthop, capture backup nexthop(s) also
1819 * so they can be conveyed through the dataplane to the FIB. We'll look
1820 * at the backups in the resolving nh 'nexthop' and its nhe, and copy them
1821 * into the route's resolved nh 'resolved' and its nhe 'nhe'.
1822 */
1823 static int resolve_backup_nexthops(const struct nexthop *nexthop,
1824 const struct nhg_hash_entry *nhe,
1825 struct nexthop *resolved,
1826 struct nhg_hash_entry *resolve_nhe,
1827 struct backup_nh_map_s *map)
1828 {
1829 int i, j, idx;
1830 const struct nexthop *bnh;
1831 struct nexthop *nh, *newnh;
1832 mpls_label_t labels[MPLS_MAX_LABELS];
1833 uint8_t num_labels;
1834
1835 assert(nexthop->backup_num <= NEXTHOP_MAX_BACKUPS);
1836
1837 /* Locate backups from the original nexthop's backup index and nhe */
1838 for (i = 0; i < nexthop->backup_num; i++) {
1839 idx = nexthop->backup_idx[i];
1840
1841 /* Do we already know about this particular backup? */
1842 for (j = 0; j < map->map_count; j++) {
1843 if (map->map[j].orig_idx == idx)
1844 break;
1845 }
1846
1847 if (j < map->map_count) {
1848 resolved->backup_idx[resolved->backup_num] =
1849 map->map[j].new_idx;
1850 resolved->backup_num++;
1851
1852 SET_FLAG(resolved->flags, NEXTHOP_FLAG_HAS_BACKUP);
1853
1854 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1855 zlog_debug("%s: found map idx orig %d, new %d",
1856 __func__, map->map[j].orig_idx,
1857 map->map[j].new_idx);
1858
1859 continue;
1860 }
1861
1862 /* We can't handle any new map entries at this point. */
1863 if (map->map_count == MULTIPATH_NUM)
1864 break;
1865
1866 /* Need to create/copy a new backup */
1867 bnh = nhe->backup_info->nhe->nhg.nexthop;
1868 for (j = 0; j < idx; j++) {
1869 if (bnh == NULL)
1870 break;
1871 bnh = bnh->next;
1872 }
1873
1874 /* Whoops - bad index in the nexthop? */
1875 if (bnh == NULL)
1876 continue;
1877
1878 if (resolve_nhe->backup_info == NULL)
1879 resolve_nhe->backup_info = zebra_nhg_backup_alloc();
1880
1881 /* Update backup info in the resolving nexthop and its nhe */
1882 newnh = nexthop_dup_no_recurse(bnh, NULL);
1883
1884 /* We may need some special handling for mpls labels: the new
1885 * backup needs to carry the recursive nexthop's labels,
1886 * if any: they may be vrf labels e.g.
1887 * The original/inner labels are in the stack of 'resolve_nhe',
1888 * if that is longer than the stack in 'nexthop'.
1889 */
1890 if (newnh->nh_label && resolved->nh_label &&
1891 nexthop->nh_label) {
1892 if (resolved->nh_label->num_labels >
1893 nexthop->nh_label->num_labels) {
1894 /* Prepare new label stack */
1895 num_labels = 0;
1896 for (j = 0; j < newnh->nh_label->num_labels;
1897 j++) {
1898 labels[j] = newnh->nh_label->label[j];
1899 num_labels++;
1900 }
1901
1902 /* Include inner labels */
1903 for (j = nexthop->nh_label->num_labels;
1904 j < resolved->nh_label->num_labels;
1905 j++) {
1906 labels[num_labels] =
1907 resolved->nh_label->label[j];
1908 num_labels++;
1909 }
1910
1911 /* Replace existing label stack in the backup */
1912 nexthop_del_labels(newnh);
1913 nexthop_add_labels(newnh, bnh->nh_label_type,
1914 num_labels, labels);
1915 }
1916 }
1917
1918 /* Need to compute the new backup index in the new
1919 * backup list, and add to map struct.
1920 */
1921 j = 0;
1922 nh = resolve_nhe->backup_info->nhe->nhg.nexthop;
1923 if (nh) {
1924 while (nh->next) {
1925 nh = nh->next;
1926 j++;
1927 }
1928
1929 nh->next = newnh;
1930 j++;
1931
1932 } else /* First one */
1933 resolve_nhe->backup_info->nhe->nhg.nexthop = newnh;
1934
1935 /* Capture index */
1936 resolved->backup_idx[resolved->backup_num] = j;
1937 resolved->backup_num++;
1938
1939 SET_FLAG(resolved->flags, NEXTHOP_FLAG_HAS_BACKUP);
1940
1941 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1942 zlog_debug("%s: added idx orig %d, new %d",
1943 __func__, idx, j);
1944
1945 /* Update map/cache */
1946 map->map[map->map_count].orig_idx = idx;
1947 map->map[map->map_count].new_idx = j;
1948 map->map_count++;
1949 }
1950
1951 return 0;
1952 }
1953
1954 /*
1955 * So this nexthop resolution has decided that a connected route
1956 * is the correct choice. At this point in time if FRR has multiple
1957 * connected routes that all point to the same prefix one will be
1958 * selected, *but* the particular interface may not be the one
1959 * that the nexthop points at. Let's look at all the available
1960 * connected routes on this node and if any of them auto match
1961 * the routes nexthops ifindex that is good enough for a match
1962 *
1963 * This code is depending on the fact that a nexthop->ifindex is 0
1964 * if it is not known, if this assumption changes, yummy!
1965 * Additionally a ifindx of 0 means figure it out for us.
1966 */
1967 static struct route_entry *
1968 zebra_nhg_connected_ifindex(struct route_node *rn, struct route_entry *match,
1969 int32_t curr_ifindex)
1970 {
1971 struct nexthop *newhop = match->nhe->nhg.nexthop;
1972 struct route_entry *re;
1973
1974 assert(newhop); /* What a kick in the patooey */
1975
1976 if (curr_ifindex == 0)
1977 return match;
1978
1979 if (curr_ifindex == newhop->ifindex)
1980 return match;
1981
1982 /*
1983 * At this point we know that this route is matching a connected
1984 * but there are possibly a bunch of connected routes that are
1985 * alive that should be considered as well. So let's iterate over
1986 * all the re's and see if they are connected as well and maybe one
1987 * of those ifindexes match as well.
1988 */
1989 RNODE_FOREACH_RE (rn, re) {
1990 if (re->type != ZEBRA_ROUTE_CONNECT)
1991 continue;
1992
1993 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
1994 continue;
1995
1996 /*
1997 * zebra has a connected route that is not removed
1998 * let's test if it is good
1999 */
2000 newhop = re->nhe->nhg.nexthop;
2001 assert(newhop);
2002 if (curr_ifindex == newhop->ifindex)
2003 return re;
2004 }
2005
2006 return match;
2007 }
2008
2009 /*
2010 * Given a nexthop we need to properly recursively resolve,
2011 * do a table lookup to find and match if at all possible.
2012 * Set the nexthop->ifindex and resolution info as appropriate.
2013 */
2014 static int nexthop_active(struct nexthop *nexthop, struct nhg_hash_entry *nhe,
2015 const struct prefix *top, int type, uint32_t flags,
2016 uint32_t *pmtu, vrf_id_t vrf_id)
2017 {
2018 struct prefix p;
2019 struct route_table *table;
2020 struct route_node *rn;
2021 struct route_entry *match = NULL;
2022 int resolved;
2023 struct zebra_nhlfe *nhlfe;
2024 struct nexthop *newhop;
2025 struct interface *ifp;
2026 rib_dest_t *dest;
2027 struct zebra_vrf *zvrf;
2028 struct in_addr local_ipv4;
2029 struct in_addr *ipv4;
2030 afi_t afi = AFI_IP;
2031
2032 /* Reset some nexthop attributes that we'll recompute if necessary */
2033 if ((nexthop->type == NEXTHOP_TYPE_IPV4)
2034 || (nexthop->type == NEXTHOP_TYPE_IPV6))
2035 nexthop->ifindex = 0;
2036
2037 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
2038 nexthops_free(nexthop->resolved);
2039 nexthop->resolved = NULL;
2040
2041 /*
2042 * Set afi based on nexthop type.
2043 * Some nexthop types get special handling, possibly skipping
2044 * the normal processing.
2045 */
2046 switch (nexthop->type) {
2047 case NEXTHOP_TYPE_IFINDEX:
2048
2049 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
2050 /*
2051 * If the interface exists and its operative or its a kernel
2052 * route and interface is up, its active. We trust kernel routes
2053 * to be good.
2054 */
2055 if (ifp
2056 && (if_is_operative(ifp)
2057 || (if_is_up(ifp)
2058 && (type == ZEBRA_ROUTE_KERNEL
2059 || type == ZEBRA_ROUTE_SYSTEM))))
2060 return 1;
2061 else
2062 return 0;
2063 break;
2064
2065 case NEXTHOP_TYPE_IPV6_IFINDEX:
2066 afi = AFI_IP6;
2067
2068 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->gate.ipv6)) {
2069 ifp = if_lookup_by_index(nexthop->ifindex,
2070 nexthop->vrf_id);
2071 if (ifp && if_is_operative(ifp))
2072 return 1;
2073 else
2074 return 0;
2075 }
2076 break;
2077
2078 case NEXTHOP_TYPE_IPV4:
2079 case NEXTHOP_TYPE_IPV4_IFINDEX:
2080 afi = AFI_IP;
2081 break;
2082 case NEXTHOP_TYPE_IPV6:
2083 afi = AFI_IP6;
2084 break;
2085
2086 case NEXTHOP_TYPE_BLACKHOLE:
2087 return 1;
2088 }
2089
2090 /*
2091 * If the nexthop has been marked as 'onlink' we just need to make
2092 * sure the nexthop's interface is known and is operational.
2093 */
2094 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)) {
2095 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
2096 if (!ifp) {
2097 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2098 zlog_debug("nexthop %pNHv marked onlink but nhif %u doesn't exist",
2099 nexthop, nexthop->ifindex);
2100 return 0;
2101 }
2102 if (!if_is_operative(ifp)) {
2103 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2104 zlog_debug("nexthop %pNHv marked onlink but nhif %s is not operational",
2105 nexthop, ifp->name);
2106 return 0;
2107 }
2108 return 1;
2109 }
2110
2111 if (top &&
2112 ((top->family == AF_INET && top->prefixlen == IPV4_MAX_BITLEN &&
2113 nexthop->gate.ipv4.s_addr == top->u.prefix4.s_addr) ||
2114 (top->family == AF_INET6 && top->prefixlen == IPV6_MAX_BITLEN &&
2115 memcmp(&nexthop->gate.ipv6, &top->u.prefix6, IPV6_MAX_BYTELEN) ==
2116 0)) &&
2117 nexthop->vrf_id == vrf_id) {
2118 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2119 zlog_debug(
2120 " :%s: Attempting to install a max prefixlength route through itself",
2121 __func__);
2122 return 0;
2123 }
2124
2125 /* Validation for ipv4 mapped ipv6 nexthop. */
2126 if (IS_MAPPED_IPV6(&nexthop->gate.ipv6)) {
2127 afi = AFI_IP;
2128 ipv4 = &local_ipv4;
2129 ipv4_mapped_ipv6_to_ipv4(&nexthop->gate.ipv6, ipv4);
2130 } else {
2131 ipv4 = &nexthop->gate.ipv4;
2132 }
2133
2134 /* Processing for nexthops with SR 'color' attribute, using
2135 * the corresponding SR policy object.
2136 */
2137 if (nexthop->srte_color) {
2138 struct ipaddr endpoint = {0};
2139 struct zebra_sr_policy *policy;
2140
2141 switch (afi) {
2142 case AFI_IP:
2143 endpoint.ipa_type = IPADDR_V4;
2144 endpoint.ipaddr_v4 = *ipv4;
2145 break;
2146 case AFI_IP6:
2147 endpoint.ipa_type = IPADDR_V6;
2148 endpoint.ipaddr_v6 = nexthop->gate.ipv6;
2149 break;
2150 default:
2151 flog_err(EC_LIB_DEVELOPMENT,
2152 "%s: unknown address-family: %u", __func__,
2153 afi);
2154 exit(1);
2155 }
2156
2157 policy = zebra_sr_policy_find(nexthop->srte_color, &endpoint);
2158 if (policy && policy->status == ZEBRA_SR_POLICY_UP) {
2159 resolved = 0;
2160 frr_each_safe (nhlfe_list, &policy->lsp->nhlfe_list,
2161 nhlfe) {
2162 if (!CHECK_FLAG(nhlfe->flags,
2163 NHLFE_FLAG_SELECTED)
2164 || CHECK_FLAG(nhlfe->flags,
2165 NHLFE_FLAG_DELETED))
2166 continue;
2167 SET_FLAG(nexthop->flags,
2168 NEXTHOP_FLAG_RECURSIVE);
2169 nexthop_set_resolved(afi, nhlfe->nexthop,
2170 nexthop, policy);
2171 resolved = 1;
2172 }
2173 if (resolved)
2174 return 1;
2175 }
2176 }
2177
2178 /* Make lookup prefix. */
2179 memset(&p, 0, sizeof(struct prefix));
2180 switch (afi) {
2181 case AFI_IP:
2182 p.family = AF_INET;
2183 p.prefixlen = IPV4_MAX_BITLEN;
2184 p.u.prefix4 = *ipv4;
2185 break;
2186 case AFI_IP6:
2187 p.family = AF_INET6;
2188 p.prefixlen = IPV6_MAX_BITLEN;
2189 p.u.prefix6 = nexthop->gate.ipv6;
2190 break;
2191 default:
2192 assert(afi != AFI_IP && afi != AFI_IP6);
2193 break;
2194 }
2195 /* Lookup table. */
2196 table = zebra_vrf_table(afi, SAFI_UNICAST, nexthop->vrf_id);
2197 /* get zvrf */
2198 zvrf = zebra_vrf_lookup_by_id(nexthop->vrf_id);
2199 if (!table || !zvrf) {
2200 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2201 zlog_debug(" %s: Table not found", __func__);
2202 return 0;
2203 }
2204
2205 rn = route_node_match(table, (struct prefix *)&p);
2206 while (rn) {
2207 route_unlock_node(rn);
2208
2209 /* Lookup should halt if we've matched against ourselves ('top',
2210 * if specified) - i.e., we cannot have a nexthop NH1 is
2211 * resolved by a route NH1. The exception is if the route is a
2212 * host route.
2213 */
2214 if (prefix_same(&rn->p, top))
2215 if (((afi == AFI_IP)
2216 && (rn->p.prefixlen != IPV4_MAX_BITLEN))
2217 || ((afi == AFI_IP6)
2218 && (rn->p.prefixlen != IPV6_MAX_BITLEN))) {
2219 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2220 zlog_debug(
2221 " %s: Matched against ourself and prefix length is not max bit length",
2222 __func__);
2223 return 0;
2224 }
2225
2226 /* Pick up selected route. */
2227 /* However, do not resolve over default route unless explicitly
2228 * allowed.
2229 */
2230 if (is_default_prefix(&rn->p)
2231 && !rnh_resolve_via_default(zvrf, p.family)) {
2232 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2233 zlog_debug(
2234 " :%s: Resolved against default route",
2235 __func__);
2236 return 0;
2237 }
2238
2239 dest = rib_dest_from_rnode(rn);
2240 if (dest && dest->selected_fib
2241 && !CHECK_FLAG(dest->selected_fib->status,
2242 ROUTE_ENTRY_REMOVED)
2243 && dest->selected_fib->type != ZEBRA_ROUTE_TABLE)
2244 match = dest->selected_fib;
2245
2246 /* If there is no selected route or matched route is EGP, go up
2247 * tree.
2248 */
2249 if (!match) {
2250 do {
2251 rn = rn->parent;
2252 } while (rn && rn->info == NULL);
2253 if (rn)
2254 route_lock_node(rn);
2255
2256 continue;
2257 }
2258
2259 if ((match->type == ZEBRA_ROUTE_CONNECT) ||
2260 (RIB_SYSTEM_ROUTE(match) && RSYSTEM_ROUTE(type))) {
2261 match = zebra_nhg_connected_ifindex(rn, match,
2262 nexthop->ifindex);
2263
2264 newhop = match->nhe->nhg.nexthop;
2265 if (nexthop->type == NEXTHOP_TYPE_IPV4 ||
2266 nexthop->type == NEXTHOP_TYPE_IPV6)
2267 nexthop->ifindex = newhop->ifindex;
2268 else if (nexthop->ifindex != newhop->ifindex) {
2269 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2270 zlog_debug(
2271 "%s: %pNHv given ifindex does not match nexthops ifindex found: %pNHv",
2272 __func__, nexthop, newhop);
2273 /*
2274 * NEXTHOP_TYPE_*_IFINDEX but ifindex
2275 * doesn't match what we found.
2276 */
2277 return 0;
2278 }
2279
2280 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
2281 zlog_debug(
2282 "%s: CONNECT match %p (%pNG), newhop %pNHv",
2283 __func__, match, match->nhe, newhop);
2284
2285 return 1;
2286 } else if (CHECK_FLAG(flags, ZEBRA_FLAG_ALLOW_RECURSION)) {
2287 struct nexthop_group *nhg;
2288 struct nexthop *resolver;
2289 struct backup_nh_map_s map = {};
2290
2291 resolved = 0;
2292
2293 /* Only useful if installed */
2294 if (!CHECK_FLAG(match->status, ROUTE_ENTRY_INSTALLED)) {
2295 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2296 zlog_debug(
2297 "%s: match %p (%pNG) not installed",
2298 __func__, match, match->nhe);
2299
2300 goto done_with_match;
2301 }
2302
2303 /* Examine installed nexthops; note that there
2304 * may not be any installed primary nexthops if
2305 * only backups are installed.
2306 */
2307 nhg = rib_get_fib_nhg(match);
2308 for (ALL_NEXTHOPS_PTR(nhg, newhop)) {
2309 if (!nexthop_valid_resolve(nexthop, newhop))
2310 continue;
2311
2312 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
2313 zlog_debug(
2314 "%s: RECURSIVE match %p (%pNG), newhop %pNHv",
2315 __func__, match, match->nhe,
2316 newhop);
2317
2318 SET_FLAG(nexthop->flags,
2319 NEXTHOP_FLAG_RECURSIVE);
2320 resolver = nexthop_set_resolved(afi, newhop,
2321 nexthop, NULL);
2322 resolved = 1;
2323
2324 /* If there are backup nexthops, capture
2325 * that info with the resolving nexthop.
2326 */
2327 if (resolver && newhop->backup_num > 0) {
2328 resolve_backup_nexthops(newhop,
2329 match->nhe,
2330 resolver, nhe,
2331 &map);
2332 }
2333 }
2334
2335 /* Examine installed backup nexthops, if any. There
2336 * are only installed backups *if* there is a
2337 * dedicated fib list. The UI can also control use
2338 * of backups for resolution.
2339 */
2340 nhg = rib_get_fib_backup_nhg(match);
2341 if (!use_recursive_backups ||
2342 nhg == NULL || nhg->nexthop == NULL)
2343 goto done_with_match;
2344
2345 for (ALL_NEXTHOPS_PTR(nhg, newhop)) {
2346 if (!nexthop_valid_resolve(nexthop, newhop))
2347 continue;
2348
2349 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
2350 zlog_debug(
2351 "%s: RECURSIVE match backup %p (%pNG), newhop %pNHv",
2352 __func__, match, match->nhe,
2353 newhop);
2354
2355 SET_FLAG(nexthop->flags,
2356 NEXTHOP_FLAG_RECURSIVE);
2357 nexthop_set_resolved(afi, newhop, nexthop,
2358 NULL);
2359 resolved = 1;
2360 }
2361
2362 done_with_match:
2363 /* Capture resolving mtu */
2364 if (resolved) {
2365 if (pmtu)
2366 *pmtu = match->mtu;
2367
2368 } else if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2369 zlog_debug(
2370 " %s: Recursion failed to find",
2371 __func__);
2372
2373 return resolved;
2374 } else {
2375 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
2376 zlog_debug(
2377 " %s: Route Type %s has not turned on recursion",
2378 __func__, zebra_route_string(type));
2379 if (type == ZEBRA_ROUTE_BGP
2380 && !CHECK_FLAG(flags, ZEBRA_FLAG_IBGP))
2381 zlog_debug(
2382 " EBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\"");
2383 }
2384 return 0;
2385 }
2386 }
2387 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2388 zlog_debug(" %s: Nexthop did not lookup in table",
2389 __func__);
2390 return 0;
2391 }
2392
2393 /* This function verifies reachability of one given nexthop, which can be
2394 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
2395 * in nexthop->flags field. The nexthop->ifindex will be updated
2396 * appropriately as well.
2397 *
2398 * An existing route map can turn an otherwise active nexthop into inactive,
2399 * but not vice versa.
2400 *
2401 * The return value is the final value of 'ACTIVE' flag.
2402 */
2403 static unsigned nexthop_active_check(struct route_node *rn,
2404 struct route_entry *re,
2405 struct nexthop *nexthop,
2406 struct nhg_hash_entry *nhe)
2407 {
2408 route_map_result_t ret = RMAP_PERMITMATCH;
2409 afi_t family;
2410 const struct prefix *p, *src_p;
2411 struct zebra_vrf *zvrf;
2412 uint32_t mtu = 0;
2413 vrf_id_t vrf_id;
2414
2415 srcdest_rnode_prefixes(rn, &p, &src_p);
2416
2417 if (rn->p.family == AF_INET)
2418 family = AFI_IP;
2419 else if (rn->p.family == AF_INET6)
2420 family = AFI_IP6;
2421 else
2422 family = AF_UNSPEC;
2423
2424 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
2425 zlog_debug("%s: re %p, nexthop %pNHv", __func__, re, nexthop);
2426
2427 /*
2428 * If the kernel has sent us a NEW route, then
2429 * by golly gee whiz it's a good route.
2430 *
2431 * If its an already INSTALLED route we have already handled, then the
2432 * kernel route's nexthop might have became unreachable
2433 * and we have to handle that.
2434 */
2435 if (!CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED) &&
2436 (re->type == ZEBRA_ROUTE_KERNEL ||
2437 re->type == ZEBRA_ROUTE_SYSTEM)) {
2438 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2439 goto skip_check;
2440 }
2441
2442
2443 vrf_id = zvrf_id(rib_dest_vrf(rib_dest_from_rnode(rn)));
2444 switch (nexthop->type) {
2445 case NEXTHOP_TYPE_IFINDEX:
2446 if (nexthop_active(nexthop, nhe, &rn->p, re->type, re->flags,
2447 &mtu, vrf_id))
2448 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2449 else
2450 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2451 break;
2452 case NEXTHOP_TYPE_IPV4:
2453 case NEXTHOP_TYPE_IPV4_IFINDEX:
2454 family = AFI_IP;
2455 if (nexthop_active(nexthop, nhe, &rn->p, re->type, re->flags,
2456 &mtu, vrf_id))
2457 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2458 else
2459 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2460 break;
2461 case NEXTHOP_TYPE_IPV6:
2462 family = AFI_IP6;
2463 if (nexthop_active(nexthop, nhe, &rn->p, re->type, re->flags,
2464 &mtu, vrf_id))
2465 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2466 else
2467 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2468 break;
2469 case NEXTHOP_TYPE_IPV6_IFINDEX:
2470 /* RFC 5549, v4 prefix with v6 NH */
2471 if (rn->p.family != AF_INET)
2472 family = AFI_IP6;
2473
2474 if (nexthop_active(nexthop, nhe, &rn->p, re->type, re->flags,
2475 &mtu, vrf_id))
2476 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2477 else
2478 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2479 break;
2480 case NEXTHOP_TYPE_BLACKHOLE:
2481 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2482 break;
2483 default:
2484 break;
2485 }
2486
2487 skip_check:
2488
2489 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) {
2490 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2491 zlog_debug(" %s: Unable to find active nexthop",
2492 __func__);
2493 return 0;
2494 }
2495
2496 /* Capture recursive nexthop mtu.
2497 * TODO -- the code used to just reset the re's value to zero
2498 * for each nexthop, and then jam any resolving route's mtu value in,
2499 * whether or not that was zero, or lt/gt any existing value? The
2500 * way this is used appears to be as a floor value, so let's try
2501 * using it that way here.
2502 */
2503 if (mtu > 0) {
2504 if (re->nexthop_mtu == 0 || re->nexthop_mtu > mtu)
2505 re->nexthop_mtu = mtu;
2506 }
2507
2508 /* XXX: What exactly do those checks do? Do we support
2509 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
2510 */
2511 if (RIB_SYSTEM_ROUTE(re) || (family == AFI_IP && p->family != AF_INET)
2512 || (family == AFI_IP6 && p->family != AF_INET6))
2513 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2514
2515 /* The original code didn't determine the family correctly
2516 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
2517 * from the rib_table_info in those cases.
2518 * Possibly it may be better to use only the rib_table_info
2519 * in every case.
2520 */
2521 if (family == 0) {
2522 struct rib_table_info *info;
2523
2524 info = srcdest_rnode_table_info(rn);
2525 family = info->afi;
2526 }
2527
2528 memset(&nexthop->rmap_src.ipv6, 0, sizeof(union g_addr));
2529
2530 zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
2531 if (!zvrf) {
2532 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2533 zlog_debug(" %s: zvrf is NULL", __func__);
2534 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2535 }
2536
2537 /* It'll get set if required inside */
2538 ret = zebra_route_map_check(family, re->type, re->instance, p, nexthop,
2539 zvrf, re->tag);
2540 if (ret == RMAP_DENYMATCH) {
2541 if (IS_ZEBRA_DEBUG_RIB) {
2542 zlog_debug(
2543 "%u:%pRN: Filtering out with NH out %s due to route map",
2544 re->vrf_id, rn,
2545 ifindex2ifname(nexthop->ifindex,
2546 nexthop->vrf_id));
2547 }
2548 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2549 }
2550 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2551 }
2552
2553 /* Helper function called after resolution to walk nhg rb trees
2554 * and toggle the NEXTHOP_GROUP_VALID flag if the nexthop
2555 * is active on singleton NHEs.
2556 */
2557 static bool zebra_nhg_set_valid_if_active(struct nhg_hash_entry *nhe)
2558 {
2559 struct nhg_connected *rb_node_dep = NULL;
2560 bool valid = false;
2561
2562 if (!zebra_nhg_depends_is_empty(nhe)) {
2563 /* Is at least one depend valid? */
2564 frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) {
2565 if (zebra_nhg_set_valid_if_active(rb_node_dep->nhe))
2566 valid = true;
2567 }
2568
2569 goto done;
2570 }
2571
2572 /* should be fully resolved singleton at this point */
2573 if (CHECK_FLAG(nhe->nhg.nexthop->flags, NEXTHOP_FLAG_ACTIVE))
2574 valid = true;
2575
2576 done:
2577 if (valid)
2578 SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID);
2579
2580 return valid;
2581 }
2582
2583 /*
2584 * Process a list of nexthops, given an nhe, determining
2585 * whether each one is ACTIVE/installable at this time.
2586 */
2587 static uint32_t nexthop_list_active_update(struct route_node *rn,
2588 struct route_entry *re,
2589 struct nhg_hash_entry *nhe,
2590 bool is_backup)
2591 {
2592 union g_addr prev_src;
2593 unsigned int prev_active, new_active;
2594 ifindex_t prev_index;
2595 uint32_t counter = 0;
2596 struct nexthop *nexthop;
2597 struct nexthop_group *nhg = &nhe->nhg;
2598
2599 nexthop = nhg->nexthop;
2600
2601 /* Init recursive nh mtu */
2602 re->nexthop_mtu = 0;
2603
2604 /* Process nexthops one-by-one */
2605 for ( ; nexthop; nexthop = nexthop->next) {
2606
2607 /* No protocol daemon provides src and so we're skipping
2608 * tracking it
2609 */
2610 prev_src = nexthop->rmap_src;
2611 prev_active = CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2612 prev_index = nexthop->ifindex;
2613
2614 /* Include the containing nhe for primary nexthops: if there's
2615 * recursive resolution, we capture the backup info also.
2616 */
2617 new_active =
2618 nexthop_active_check(rn, re, nexthop,
2619 (is_backup ? NULL : nhe));
2620
2621 /*
2622 * We need to respect the multipath_num here
2623 * as that what we should be able to install from
2624 * a multipath perspective should not be a data plane
2625 * decision point.
2626 */
2627 if (new_active && counter >= zrouter.multipath_num) {
2628 struct nexthop *nh;
2629
2630 /* Set it and its resolved nexthop as inactive. */
2631 for (nh = nexthop; nh; nh = nh->resolved)
2632 UNSET_FLAG(nh->flags, NEXTHOP_FLAG_ACTIVE);
2633
2634 new_active = 0;
2635 }
2636
2637 if (new_active)
2638 counter++;
2639
2640 /* Check for changes to the nexthop - set ROUTE_ENTRY_CHANGED */
2641 if (prev_active != new_active || prev_index != nexthop->ifindex
2642 || ((nexthop->type >= NEXTHOP_TYPE_IFINDEX
2643 && nexthop->type < NEXTHOP_TYPE_IPV6)
2644 && prev_src.ipv4.s_addr
2645 != nexthop->rmap_src.ipv4.s_addr)
2646 || ((nexthop->type >= NEXTHOP_TYPE_IPV6
2647 && nexthop->type < NEXTHOP_TYPE_BLACKHOLE)
2648 && !(IPV6_ADDR_SAME(&prev_src.ipv6,
2649 &nexthop->rmap_src.ipv6)))
2650 || CHECK_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED))
2651 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
2652 }
2653
2654 return counter;
2655 }
2656
2657
2658 static uint32_t proto_nhg_nexthop_active_update(struct nexthop_group *nhg)
2659 {
2660 struct nexthop *nh;
2661 uint32_t curr_active = 0;
2662
2663 /* Assume all active for now */
2664
2665 for (nh = nhg->nexthop; nh; nh = nh->next) {
2666 SET_FLAG(nh->flags, NEXTHOP_FLAG_ACTIVE);
2667 curr_active++;
2668 }
2669
2670 return curr_active;
2671 }
2672
2673 /*
2674 * Iterate over all nexthops of the given RIB entry and refresh their
2675 * ACTIVE flag. If any nexthop is found to toggle the ACTIVE flag,
2676 * the whole re structure is flagged with ROUTE_ENTRY_CHANGED.
2677 *
2678 * Return value is the new number of active nexthops.
2679 */
2680 int nexthop_active_update(struct route_node *rn, struct route_entry *re)
2681 {
2682 struct nhg_hash_entry *curr_nhe;
2683 uint32_t curr_active = 0, backup_active = 0;
2684
2685 if (PROTO_OWNED(re->nhe))
2686 return proto_nhg_nexthop_active_update(&re->nhe->nhg);
2687
2688 afi_t rt_afi = family2afi(rn->p.family);
2689
2690 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
2691
2692 /* Make a local copy of the existing nhe, so we don't work on/modify
2693 * the shared nhe.
2694 */
2695 curr_nhe = zebra_nhe_copy(re->nhe, re->nhe->id);
2696
2697 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
2698 zlog_debug("%s: re %p nhe %p (%pNG), curr_nhe %p", __func__, re,
2699 re->nhe, re->nhe, curr_nhe);
2700
2701 /* Clear the existing id, if any: this will avoid any confusion
2702 * if the id exists, and will also force the creation
2703 * of a new nhe reflecting the changes we may make in this local copy.
2704 */
2705 curr_nhe->id = 0;
2706
2707 /* Process nexthops */
2708 curr_active = nexthop_list_active_update(rn, re, curr_nhe, false);
2709
2710 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
2711 zlog_debug("%s: re %p curr_active %u", __func__, re,
2712 curr_active);
2713
2714 /* If there are no backup nexthops, we are done */
2715 if (zebra_nhg_get_backup_nhg(curr_nhe) == NULL)
2716 goto backups_done;
2717
2718 backup_active = nexthop_list_active_update(
2719 rn, re, curr_nhe->backup_info->nhe, true /*is_backup*/);
2720
2721 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
2722 zlog_debug("%s: re %p backup_active %u", __func__, re,
2723 backup_active);
2724
2725 backups_done:
2726
2727 /*
2728 * Ref or create an nhe that matches the current state of the
2729 * nexthop(s).
2730 */
2731 if (CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)) {
2732 struct nhg_hash_entry *new_nhe = NULL;
2733
2734 new_nhe = zebra_nhg_rib_find_nhe(curr_nhe, rt_afi);
2735
2736 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
2737 zlog_debug(
2738 "%s: re %p CHANGED: nhe %p (%pNG) => new_nhe %p (%pNG)",
2739 __func__, re, re->nhe, re->nhe, new_nhe,
2740 new_nhe);
2741
2742 route_entry_update_nhe(re, new_nhe);
2743 }
2744
2745
2746 /* Walk the NHE depends tree and toggle NEXTHOP_GROUP_VALID
2747 * flag where appropriate.
2748 */
2749 if (curr_active)
2750 zebra_nhg_set_valid_if_active(re->nhe);
2751
2752 /*
2753 * Do not need the old / copied nhe anymore since it
2754 * was either copied over into a new nhe or not
2755 * used at all.
2756 */
2757 zebra_nhg_free(curr_nhe);
2758 return curr_active;
2759 }
2760
2761 /* Recursively construct a grp array of fully resolved IDs.
2762 *
2763 * This function allows us to account for groups within groups,
2764 * by converting them into a flat array of IDs.
2765 *
2766 * nh_grp is modified at every level of recursion to append
2767 * to it the next unique, fully resolved ID from the entire tree.
2768 *
2769 *
2770 * Note:
2771 * I'm pretty sure we only allow ONE level of group within group currently.
2772 * But making this recursive just in case that ever changes.
2773 */
2774 static uint8_t zebra_nhg_nhe2grp_internal(struct nh_grp *grp,
2775 uint8_t curr_index,
2776 struct nhg_hash_entry *nhe,
2777 int max_num)
2778 {
2779 struct nhg_connected *rb_node_dep = NULL;
2780 struct nhg_hash_entry *depend = NULL;
2781 uint8_t i = curr_index;
2782
2783 frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) {
2784 bool duplicate = false;
2785
2786 if (i >= max_num)
2787 goto done;
2788
2789 depend = rb_node_dep->nhe;
2790
2791 /*
2792 * If its recursive, use its resolved nhe in the group
2793 */
2794 if (CHECK_FLAG(depend->flags, NEXTHOP_GROUP_RECURSIVE)) {
2795 depend = zebra_nhg_resolve(depend);
2796 if (!depend) {
2797 flog_err(
2798 EC_ZEBRA_NHG_FIB_UPDATE,
2799 "Failed to recursively resolve Nexthop Hash Entry in the group id=%pNG",
2800 nhe);
2801 continue;
2802 }
2803 }
2804
2805 if (!zebra_nhg_depends_is_empty(depend)) {
2806 /* This is a group within a group */
2807 i = zebra_nhg_nhe2grp_internal(grp, i, depend, max_num);
2808 } else {
2809 if (!CHECK_FLAG(depend->flags, NEXTHOP_GROUP_VALID)) {
2810 if (IS_ZEBRA_DEBUG_RIB_DETAILED
2811 || IS_ZEBRA_DEBUG_NHG)
2812 zlog_debug(
2813 "%s: Nexthop ID (%u) not valid, not appending to dataplane install group",
2814 __func__, depend->id);
2815 continue;
2816 }
2817
2818 /* If the nexthop not installed/queued for install don't
2819 * put in the ID array.
2820 */
2821 if (!(CHECK_FLAG(depend->flags, NEXTHOP_GROUP_INSTALLED)
2822 || CHECK_FLAG(depend->flags,
2823 NEXTHOP_GROUP_QUEUED))) {
2824 if (IS_ZEBRA_DEBUG_RIB_DETAILED
2825 || IS_ZEBRA_DEBUG_NHG)
2826 zlog_debug(
2827 "%s: Nexthop ID (%u) not installed or queued for install, not appending to dataplane install group",
2828 __func__, depend->id);
2829 continue;
2830 }
2831
2832 /* Check for duplicate IDs, ignore if found. */
2833 for (int j = 0; j < i; j++) {
2834 if (depend->id == grp[j].id) {
2835 duplicate = true;
2836 break;
2837 }
2838 }
2839
2840 if (duplicate) {
2841 if (IS_ZEBRA_DEBUG_RIB_DETAILED
2842 || IS_ZEBRA_DEBUG_NHG)
2843 zlog_debug(
2844 "%s: Nexthop ID (%u) is duplicate, not appending to dataplane install group",
2845 __func__, depend->id);
2846 continue;
2847 }
2848
2849 grp[i].id = depend->id;
2850 grp[i].weight = depend->nhg.nexthop->weight;
2851 i++;
2852 }
2853 }
2854
2855 if (nhe->backup_info == NULL || nhe->backup_info->nhe == NULL)
2856 goto done;
2857
2858 /* TODO -- For now, we are not trying to use or install any
2859 * backup info in this nexthop-id path: we aren't prepared
2860 * to use the backups here yet. We're just debugging what we find.
2861 */
2862 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
2863 zlog_debug("%s: skipping backup nhe", __func__);
2864
2865 done:
2866 return i;
2867 }
2868
2869 /* Convert a nhe into a group array */
2870 uint8_t zebra_nhg_nhe2grp(struct nh_grp *grp, struct nhg_hash_entry *nhe,
2871 int max_num)
2872 {
2873 /* Call into the recursive function */
2874 return zebra_nhg_nhe2grp_internal(grp, 0, nhe, max_num);
2875 }
2876
2877 void zebra_nhg_install_kernel(struct nhg_hash_entry *nhe)
2878 {
2879 struct nhg_connected *rb_node_dep = NULL;
2880
2881 /* Resolve it first */
2882 nhe = zebra_nhg_resolve(nhe);
2883
2884 /* Make sure all depends are installed/queued */
2885 frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) {
2886 zebra_nhg_install_kernel(rb_node_dep->nhe);
2887 }
2888
2889 if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_VALID)
2890 && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED)
2891 && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED)) {
2892 /* Change its type to us since we are installing it */
2893 if (!ZEBRA_NHG_CREATED(nhe))
2894 nhe->type = ZEBRA_ROUTE_NHG;
2895
2896 int ret = dplane_nexthop_add(nhe);
2897
2898 switch (ret) {
2899 case ZEBRA_DPLANE_REQUEST_QUEUED:
2900 SET_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED);
2901 break;
2902 case ZEBRA_DPLANE_REQUEST_FAILURE:
2903 flog_err(
2904 EC_ZEBRA_DP_INSTALL_FAIL,
2905 "Failed to install Nexthop ID (%pNG) into the kernel",
2906 nhe);
2907 break;
2908 case ZEBRA_DPLANE_REQUEST_SUCCESS:
2909 SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
2910 zebra_nhg_handle_install(nhe);
2911 break;
2912 }
2913 }
2914 }
2915
2916 void zebra_nhg_uninstall_kernel(struct nhg_hash_entry *nhe)
2917 {
2918 if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED)) {
2919 int ret = dplane_nexthop_delete(nhe);
2920
2921 switch (ret) {
2922 case ZEBRA_DPLANE_REQUEST_QUEUED:
2923 SET_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED);
2924 break;
2925 case ZEBRA_DPLANE_REQUEST_FAILURE:
2926 flog_err(
2927 EC_ZEBRA_DP_DELETE_FAIL,
2928 "Failed to uninstall Nexthop ID (%pNG) from the kernel",
2929 nhe);
2930 break;
2931 case ZEBRA_DPLANE_REQUEST_SUCCESS:
2932 UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
2933 break;
2934 }
2935 }
2936
2937 zebra_nhg_handle_uninstall(nhe);
2938 }
2939
2940 void zebra_nhg_dplane_result(struct zebra_dplane_ctx *ctx)
2941 {
2942 enum dplane_op_e op;
2943 enum zebra_dplane_result status;
2944 uint32_t id = 0;
2945 struct nhg_hash_entry *nhe = NULL;
2946
2947 op = dplane_ctx_get_op(ctx);
2948 status = dplane_ctx_get_status(ctx);
2949
2950 id = dplane_ctx_get_nhe_id(ctx);
2951
2952 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL || IS_ZEBRA_DEBUG_NHG_DETAIL)
2953 zlog_debug(
2954 "Nexthop dplane ctx %p, op %s, nexthop ID (%u), result %s",
2955 ctx, dplane_op2str(op), id, dplane_res2str(status));
2956
2957 switch (op) {
2958 case DPLANE_OP_NH_DELETE:
2959 if (status != ZEBRA_DPLANE_REQUEST_SUCCESS)
2960 flog_err(
2961 EC_ZEBRA_DP_DELETE_FAIL,
2962 "Failed to uninstall Nexthop ID (%u) from the kernel",
2963 id);
2964
2965 /* We already free'd the data, nothing to do */
2966 break;
2967 case DPLANE_OP_NH_INSTALL:
2968 case DPLANE_OP_NH_UPDATE:
2969 nhe = zebra_nhg_lookup_id(id);
2970
2971 if (!nhe) {
2972 if (IS_ZEBRA_DEBUG_NHG)
2973 zlog_debug(
2974 "%s operation preformed on Nexthop ID (%u) in the kernel, that we no longer have in our table",
2975 dplane_op2str(op), id);
2976
2977 break;
2978 }
2979
2980 UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED);
2981 if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
2982 SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID);
2983 SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
2984 zebra_nhg_handle_install(nhe);
2985
2986 /* If daemon nhg, send it an update */
2987 if (PROTO_OWNED(nhe))
2988 zsend_nhg_notify(nhe->type, nhe->zapi_instance,
2989 nhe->zapi_session, nhe->id,
2990 ZAPI_NHG_INSTALLED);
2991 } else {
2992 /* If daemon nhg, send it an update */
2993 if (PROTO_OWNED(nhe))
2994 zsend_nhg_notify(nhe->type, nhe->zapi_instance,
2995 nhe->zapi_session, nhe->id,
2996 ZAPI_NHG_FAIL_INSTALL);
2997
2998 if (!(zebra_nhg_proto_nexthops_only() &&
2999 !PROTO_OWNED(nhe)))
3000 flog_err(
3001 EC_ZEBRA_DP_INSTALL_FAIL,
3002 "Failed to install Nexthop (%pNG) into the kernel",
3003 nhe);
3004 }
3005 break;
3006
3007 case DPLANE_OP_ROUTE_INSTALL:
3008 case DPLANE_OP_ROUTE_UPDATE:
3009 case DPLANE_OP_ROUTE_DELETE:
3010 case DPLANE_OP_ROUTE_NOTIFY:
3011 case DPLANE_OP_LSP_INSTALL:
3012 case DPLANE_OP_LSP_UPDATE:
3013 case DPLANE_OP_LSP_DELETE:
3014 case DPLANE_OP_LSP_NOTIFY:
3015 case DPLANE_OP_PW_INSTALL:
3016 case DPLANE_OP_PW_UNINSTALL:
3017 case DPLANE_OP_SYS_ROUTE_ADD:
3018 case DPLANE_OP_SYS_ROUTE_DELETE:
3019 case DPLANE_OP_ADDR_INSTALL:
3020 case DPLANE_OP_ADDR_UNINSTALL:
3021 case DPLANE_OP_MAC_INSTALL:
3022 case DPLANE_OP_MAC_DELETE:
3023 case DPLANE_OP_NEIGH_INSTALL:
3024 case DPLANE_OP_NEIGH_UPDATE:
3025 case DPLANE_OP_NEIGH_DELETE:
3026 case DPLANE_OP_NEIGH_IP_INSTALL:
3027 case DPLANE_OP_NEIGH_IP_DELETE:
3028 case DPLANE_OP_VTEP_ADD:
3029 case DPLANE_OP_VTEP_DELETE:
3030 case DPLANE_OP_RULE_ADD:
3031 case DPLANE_OP_RULE_DELETE:
3032 case DPLANE_OP_RULE_UPDATE:
3033 case DPLANE_OP_NEIGH_DISCOVER:
3034 case DPLANE_OP_BR_PORT_UPDATE:
3035 case DPLANE_OP_NONE:
3036 case DPLANE_OP_IPTABLE_ADD:
3037 case DPLANE_OP_IPTABLE_DELETE:
3038 case DPLANE_OP_IPSET_ADD:
3039 case DPLANE_OP_IPSET_DELETE:
3040 case DPLANE_OP_IPSET_ENTRY_ADD:
3041 case DPLANE_OP_IPSET_ENTRY_DELETE:
3042 case DPLANE_OP_NEIGH_TABLE_UPDATE:
3043 case DPLANE_OP_GRE_SET:
3044 case DPLANE_OP_INTF_ADDR_ADD:
3045 case DPLANE_OP_INTF_ADDR_DEL:
3046 case DPLANE_OP_INTF_NETCONFIG:
3047 case DPLANE_OP_INTF_INSTALL:
3048 case DPLANE_OP_INTF_UPDATE:
3049 case DPLANE_OP_INTF_DELETE:
3050 break;
3051 }
3052
3053 dplane_ctx_fini(&ctx);
3054 }
3055
3056 static int zebra_nhg_sweep_entry(struct hash_bucket *bucket, void *arg)
3057 {
3058 struct nhg_hash_entry *nhe = NULL;
3059
3060 nhe = (struct nhg_hash_entry *)bucket->data;
3061
3062 /*
3063 * same logic as with routes.
3064 *
3065 * If older than startup time, we know we read them in from the
3066 * kernel and have not gotten and update for them since startup
3067 * from an upper level proto.
3068 */
3069 if (zrouter.startup_time < nhe->uptime)
3070 return HASHWALK_CONTINUE;
3071
3072 /*
3073 * If it's proto-owned and not being used by a route, remove it since
3074 * we haven't gotten an update about it from the proto since startup.
3075 * This means that either the config for it was removed or the daemon
3076 * didn't get started. This handles graceful restart & retain scenario.
3077 */
3078 if (PROTO_OWNED(nhe) && nhe->refcnt == 1) {
3079 zebra_nhg_decrement_ref(nhe);
3080 return HASHWALK_ABORT;
3081 }
3082
3083 /*
3084 * If its being ref'd by routes, just let it be uninstalled via a route
3085 * removal.
3086 */
3087 if (ZEBRA_NHG_CREATED(nhe) && nhe->refcnt <= 0) {
3088 zebra_nhg_uninstall_kernel(nhe);
3089 return HASHWALK_ABORT;
3090 }
3091
3092 return HASHWALK_CONTINUE;
3093 }
3094
3095 void zebra_nhg_sweep_table(struct hash *hash)
3096 {
3097 uint32_t count;
3098
3099 /*
3100 * Yes this is extremely odd. Effectively nhg's have
3101 * other nexthop groups that depend on them and when you
3102 * remove them, you can have other entries blown up.
3103 * our hash code does not work with deleting multiple
3104 * entries at a time and will possibly cause crashes
3105 * So what to do? Whenever zebra_nhg_sweep_entry
3106 * deletes an entry it will return HASHWALK_ABORT,
3107 * cause that deletion might have triggered more.
3108 * then we can just keep sweeping this table
3109 * until nothing more is found to do.
3110 */
3111 do {
3112 count = hashcount(hash);
3113 hash_walk(hash, zebra_nhg_sweep_entry, NULL);
3114 } while (count != hashcount(hash));
3115 }
3116
3117 static void zebra_nhg_mark_keep_entry(struct hash_bucket *bucket, void *arg)
3118 {
3119 struct nhg_hash_entry *nhe = bucket->data;
3120
3121 UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
3122 }
3123
3124 /*
3125 * When we are shutting down and we have retain mode enabled
3126 * in zebra the process is to mark each vrf that it's
3127 * routes should not be deleted. The problem with that
3128 * is that shutdown actually free's up memory which
3129 * causes the nexthop group's ref counts to go to zero
3130 * we need a way to subtly tell the system to not remove
3131 * the nexthop groups from the kernel at the same time.
3132 * The easiest just looks like that we should not mark
3133 * the nhg's as installed any more and when the ref count
3134 * goes to zero we'll attempt to delete and do nothing
3135 */
3136 void zebra_nhg_mark_keep(void)
3137 {
3138 hash_iterate(zrouter.nhgs_id, zebra_nhg_mark_keep_entry, NULL);
3139 }
3140
3141 /* Global control to disable use of kernel nexthops, if available. We can't
3142 * force the kernel to support nexthop ids, of course, but we can disable
3143 * zebra's use of them, for testing e.g. By default, if the kernel supports
3144 * nexthop ids, zebra uses them.
3145 */
3146 void zebra_nhg_enable_kernel_nexthops(bool set)
3147 {
3148 g_nexthops_enabled = set;
3149 }
3150
3151 bool zebra_nhg_kernel_nexthops_enabled(void)
3152 {
3153 return g_nexthops_enabled;
3154 }
3155
3156 /* Global control for use of activated backups for recursive resolution. */
3157 void zebra_nhg_set_recursive_use_backups(bool set)
3158 {
3159 use_recursive_backups = set;
3160 }
3161
3162 bool zebra_nhg_recursive_use_backups(void)
3163 {
3164 return use_recursive_backups;
3165 }
3166
3167 /*
3168 * Global control to only use kernel nexthops for protocol created NHGs.
3169 * There are some use cases where you may not want zebra to implicitly
3170 * create kernel nexthops for all routes and only create them for NHGs
3171 * passed down by upper level protos.
3172 *
3173 * Default is off.
3174 */
3175 void zebra_nhg_set_proto_nexthops_only(bool set)
3176 {
3177 proto_nexthops_only = set;
3178 }
3179
3180 bool zebra_nhg_proto_nexthops_only(void)
3181 {
3182 return proto_nexthops_only;
3183 }
3184
3185 /* Add NHE from upper level proto */
3186 struct nhg_hash_entry *zebra_nhg_proto_add(uint32_t id, int type,
3187 uint16_t instance, uint32_t session,
3188 struct nexthop_group *nhg, afi_t afi)
3189 {
3190 struct nhg_hash_entry lookup;
3191 struct nhg_hash_entry *new, *old;
3192 struct nhg_connected *rb_node_dep = NULL;
3193 struct nexthop *newhop;
3194 bool replace = false;
3195
3196 if (!nhg->nexthop) {
3197 if (IS_ZEBRA_DEBUG_NHG)
3198 zlog_debug("%s: id %u, no nexthops passed to add",
3199 __func__, id);
3200 return NULL;
3201 }
3202
3203
3204 /* Set nexthop list as active, since they wont go through rib
3205 * processing.
3206 *
3207 * Assuming valid/onlink for now.
3208 *
3209 * Once resolution is figured out, we won't need this!
3210 */
3211 for (ALL_NEXTHOPS_PTR(nhg, newhop)) {
3212 if (CHECK_FLAG(newhop->flags, NEXTHOP_FLAG_HAS_BACKUP)) {
3213 if (IS_ZEBRA_DEBUG_NHG)
3214 zlog_debug(
3215 "%s: id %u, backup nexthops not supported",
3216 __func__, id);
3217 return NULL;
3218 }
3219
3220 if (newhop->type == NEXTHOP_TYPE_BLACKHOLE) {
3221 if (IS_ZEBRA_DEBUG_NHG)
3222 zlog_debug(
3223 "%s: id %u, blackhole nexthop not supported",
3224 __func__, id);
3225 return NULL;
3226 }
3227
3228 if (newhop->type == NEXTHOP_TYPE_IFINDEX) {
3229 if (IS_ZEBRA_DEBUG_NHG)
3230 zlog_debug(
3231 "%s: id %u, nexthop without gateway not supported",
3232 __func__, id);
3233 return NULL;
3234 }
3235
3236 if (!newhop->ifindex) {
3237 if (IS_ZEBRA_DEBUG_NHG)
3238 zlog_debug(
3239 "%s: id %u, nexthop without ifindex is not supported",
3240 __func__, id);
3241 return NULL;
3242 }
3243 SET_FLAG(newhop->flags, NEXTHOP_FLAG_ACTIVE);
3244 }
3245
3246 zebra_nhe_init(&lookup, afi, nhg->nexthop);
3247 lookup.nhg.nexthop = nhg->nexthop;
3248 lookup.id = id;
3249 lookup.type = type;
3250
3251 old = zebra_nhg_lookup_id(id);
3252
3253 if (old) {
3254 /*
3255 * This is a replace, just release NHE from ID for now, The
3256 * depends/dependents may still be used in the replacement so
3257 * we don't touch them other than to remove their refs to their
3258 * old parent.
3259 */
3260 replace = true;
3261 hash_release(zrouter.nhgs_id, old);
3262
3263 /* Free all the things */
3264 zebra_nhg_release_all_deps(old);
3265 }
3266
3267 new = zebra_nhg_rib_find_nhe(&lookup, afi);
3268
3269 zebra_nhg_increment_ref(new);
3270
3271 /* Capture zapi client info */
3272 new->zapi_instance = instance;
3273 new->zapi_session = session;
3274
3275 zebra_nhg_set_valid_if_active(new);
3276
3277 zebra_nhg_install_kernel(new);
3278
3279 if (old) {
3280 /*
3281 * Check to handle recving DEL while routes still in use then
3282 * a replace.
3283 *
3284 * In this case we would have decremented the refcnt already
3285 * but set the FLAG here. Go ahead and increment once to fix
3286 * the misordering we have been sent.
3287 */
3288 if (CHECK_FLAG(old->flags, NEXTHOP_GROUP_PROTO_RELEASED))
3289 zebra_nhg_increment_ref(old);
3290
3291 rib_handle_nhg_replace(old, new);
3292
3293 /* if this != 1 at this point, we have a bug */
3294 assert(old->refcnt == 1);
3295
3296 /* We have to decrement its singletons
3297 * because some might not exist in NEW.
3298 */
3299 if (!zebra_nhg_depends_is_empty(old)) {
3300 frr_each (nhg_connected_tree, &old->nhg_depends,
3301 rb_node_dep)
3302 zebra_nhg_decrement_ref(rb_node_dep->nhe);
3303 }
3304
3305 /* Dont call the dec API, we dont want to uninstall the ID */
3306 old->refcnt = 0;
3307 zebra_nhg_free(old);
3308 old = NULL;
3309 }
3310
3311 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
3312 zlog_debug("%s: %s nhe %p (%u), vrf %d, type %s", __func__,
3313 (replace ? "replaced" : "added"), new, new->id,
3314 new->vrf_id, zebra_route_string(new->type));
3315
3316 return new;
3317 }
3318
3319 /* Delete NHE from upper level proto, caller must decrement ref */
3320 struct nhg_hash_entry *zebra_nhg_proto_del(uint32_t id, int type)
3321 {
3322 struct nhg_hash_entry *nhe;
3323
3324 nhe = zebra_nhg_lookup_id(id);
3325
3326 if (!nhe) {
3327 if (IS_ZEBRA_DEBUG_NHG)
3328 zlog_debug("%s: id %u, lookup failed", __func__, id);
3329
3330 return NULL;
3331 }
3332
3333 if (type != nhe->type) {
3334 if (IS_ZEBRA_DEBUG_NHG)
3335 zlog_debug(
3336 "%s: id %u, type %s mismatch, sent by %s, ignoring",
3337 __func__, id, zebra_route_string(nhe->type),
3338 zebra_route_string(type));
3339 return NULL;
3340 }
3341
3342 if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_PROTO_RELEASED)) {
3343 if (IS_ZEBRA_DEBUG_NHG)
3344 zlog_debug("%s: id %u, already released", __func__, id);
3345
3346 return NULL;
3347 }
3348
3349 SET_FLAG(nhe->flags, NEXTHOP_GROUP_PROTO_RELEASED);
3350
3351 if (nhe->refcnt > 1) {
3352 if (IS_ZEBRA_DEBUG_NHG)
3353 zlog_debug(
3354 "%s: %pNG, still being used by routes refcnt %u",
3355 __func__, nhe, nhe->refcnt);
3356 return nhe;
3357 }
3358
3359 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
3360 zlog_debug("%s: deleted nhe %p (%pNG), vrf %d, type %s",
3361 __func__, nhe, nhe, nhe->vrf_id,
3362 zebra_route_string(nhe->type));
3363
3364 return nhe;
3365 }
3366
3367 struct nhg_score_proto_iter {
3368 int type;
3369 struct list *found;
3370 };
3371
3372 static void zebra_nhg_score_proto_entry(struct hash_bucket *bucket, void *arg)
3373 {
3374 struct nhg_hash_entry *nhe;
3375 struct nhg_score_proto_iter *iter;
3376
3377 nhe = (struct nhg_hash_entry *)bucket->data;
3378 iter = arg;
3379
3380 /* Needs to match type and outside zebra ID space */
3381 if (nhe->type == iter->type && PROTO_OWNED(nhe)) {
3382 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
3383 zlog_debug(
3384 "%s: found nhe %p (%pNG), vrf %d, type %s after client disconnect",
3385 __func__, nhe, nhe, nhe->vrf_id,
3386 zebra_route_string(nhe->type));
3387
3388 /* Add to removal list */
3389 listnode_add(iter->found, nhe);
3390 }
3391 }
3392
3393 /* Remove specific by proto NHGs */
3394 unsigned long zebra_nhg_score_proto(int type)
3395 {
3396 struct nhg_hash_entry *nhe;
3397 struct nhg_score_proto_iter iter = {};
3398 struct listnode *ln;
3399 unsigned long count;
3400
3401 iter.type = type;
3402 iter.found = list_new();
3403
3404 /* Find matching entries to remove */
3405 hash_iterate(zrouter.nhgs_id, zebra_nhg_score_proto_entry, &iter);
3406
3407 /* Now remove them */
3408 for (ALL_LIST_ELEMENTS_RO(iter.found, ln, nhe)) {
3409 /*
3410 * This should be the last ref if we remove client routes too,
3411 * and thus should remove and free them.
3412 */
3413 zebra_nhg_decrement_ref(nhe);
3414 }
3415
3416 count = iter.found->count;
3417 list_delete(&iter.found);
3418
3419 return count;
3420 }
3421
3422 printfrr_ext_autoreg_p("NG", printfrr_nhghe);
3423 static ssize_t printfrr_nhghe(struct fbuf *buf, struct printfrr_eargs *ea,
3424 const void *ptr)
3425 {
3426 const struct nhg_hash_entry *nhe = ptr;
3427 const struct nhg_connected *dep;
3428 ssize_t ret = 0;
3429
3430 if (!nhe)
3431 return bputs(buf, "[NULL]");
3432
3433 ret += bprintfrr(buf, "%u[", nhe->id);
3434 if (nhe->ifp)
3435 ret += printfrr_nhs(buf, nhe->nhg.nexthop);
3436 else {
3437 int count = zebra_nhg_depends_count(nhe);
3438
3439 frr_each (nhg_connected_tree_const, &nhe->nhg_depends, dep) {
3440 ret += bprintfrr(buf, "%u", dep->nhe->id);
3441 if (count > 1)
3442 ret += bputs(buf, "/");
3443 count--;
3444 }
3445 }
3446
3447 ret += bputs(buf, "]");
3448 return ret;
3449 }