]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_nhg.c
Merge pull request #5637 from sworleys/NHG-Zapi-Fuzz
[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
32 #include "zebra/connected.h"
33 #include "zebra/debug.h"
34 #include "zebra/zebra_router.h"
35 #include "zebra/zebra_nhg_private.h"
36 #include "zebra/zebra_rnh.h"
37 #include "zebra/zebra_routemap.h"
38 #include "zebra/zebra_memory.h"
39 #include "zebra/zserv.h"
40 #include "zebra/rt.h"
41 #include "zebra_errors.h"
42 #include "zebra_dplane.h"
43 #include "zebra/interface.h"
44
45 DEFINE_MTYPE_STATIC(ZEBRA, NHG, "Nexthop Group Entry");
46 DEFINE_MTYPE_STATIC(ZEBRA, NHG_CONNECTED, "Nexthop Group Connected");
47 DEFINE_MTYPE_STATIC(ZEBRA, NHG_CTX, "Nexthop Group Context");
48
49 /* id counter to keep in sync with kernel */
50 uint32_t id_counter;
51
52 static struct nhg_hash_entry *depends_find(const struct nexthop *nh,
53 afi_t afi);
54 static void depends_add(struct nhg_connected_tree_head *head,
55 struct nhg_hash_entry *depend);
56 static struct nhg_hash_entry *
57 depends_find_add(struct nhg_connected_tree_head *head, struct nexthop *nh,
58 afi_t afi);
59 static struct nhg_hash_entry *
60 depends_find_id_add(struct nhg_connected_tree_head *head, uint32_t id);
61 static void depends_decrement_free(struct nhg_connected_tree_head *head);
62
63
64 static void nhg_connected_free(struct nhg_connected *dep)
65 {
66 XFREE(MTYPE_NHG_CONNECTED, dep);
67 }
68
69 static struct nhg_connected *nhg_connected_new(struct nhg_hash_entry *nhe)
70 {
71 struct nhg_connected *new = NULL;
72
73 new = XCALLOC(MTYPE_NHG_CONNECTED, sizeof(struct nhg_connected));
74 new->nhe = nhe;
75
76 return new;
77 }
78
79 void nhg_connected_tree_free(struct nhg_connected_tree_head *head)
80 {
81 struct nhg_connected *rb_node_dep = NULL;
82
83 if (!nhg_connected_tree_is_empty(head)) {
84 frr_each_safe(nhg_connected_tree, head, rb_node_dep) {
85 nhg_connected_tree_del(head, rb_node_dep);
86 nhg_connected_free(rb_node_dep);
87 }
88 }
89 }
90
91 bool nhg_connected_tree_is_empty(const struct nhg_connected_tree_head *head)
92 {
93 return nhg_connected_tree_count(head) ? false : true;
94 }
95
96 struct nhg_connected *
97 nhg_connected_tree_root(struct nhg_connected_tree_head *head)
98 {
99 return nhg_connected_tree_first(head);
100 }
101
102 struct nhg_hash_entry *
103 nhg_connected_tree_del_nhe(struct nhg_connected_tree_head *head,
104 struct nhg_hash_entry *depend)
105 {
106 struct nhg_connected lookup = {};
107 struct nhg_connected *remove = NULL;
108 struct nhg_hash_entry *removed_nhe;
109
110 lookup.nhe = depend;
111
112 /* Lookup to find the element, then remove it */
113 remove = nhg_connected_tree_find(head, &lookup);
114 if (remove)
115 /* Re-returning here just in case this API changes..
116 * the _del list api's are a bit undefined at the moment.
117 *
118 * So hopefully returning here will make it fail if the api
119 * changes to something different than currently expected.
120 */
121 remove = nhg_connected_tree_del(head, remove);
122
123 /* If the entry was sucessfully removed, free the 'connected` struct */
124 if (remove) {
125 removed_nhe = remove->nhe;
126 nhg_connected_free(remove);
127 return removed_nhe;
128 }
129
130 return NULL;
131 }
132
133 /* Assuming UNIQUE RB tree. If this changes, assumptions here about
134 * insertion need to change.
135 */
136 struct nhg_hash_entry *
137 nhg_connected_tree_add_nhe(struct nhg_connected_tree_head *head,
138 struct nhg_hash_entry *depend)
139 {
140 struct nhg_connected *new = NULL;
141
142 new = nhg_connected_new(depend);
143
144 /* On success, NULL will be returned from the
145 * RB code.
146 */
147 if (new && (nhg_connected_tree_add(head, new) == NULL))
148 return NULL;
149
150 /* If it wasn't successful, it must be a duplicate. We enforce the
151 * unique property for the `nhg_connected` tree.
152 */
153 nhg_connected_free(new);
154
155 return depend;
156 }
157
158 static void
159 nhg_connected_tree_decrement_ref(struct nhg_connected_tree_head *head)
160 {
161 struct nhg_connected *rb_node_dep = NULL;
162
163 frr_each_safe(nhg_connected_tree, head, rb_node_dep) {
164 zebra_nhg_decrement_ref(rb_node_dep->nhe);
165 }
166 }
167
168 static void
169 nhg_connected_tree_increment_ref(struct nhg_connected_tree_head *head)
170 {
171 struct nhg_connected *rb_node_dep = NULL;
172
173 frr_each(nhg_connected_tree, head, rb_node_dep) {
174 zebra_nhg_increment_ref(rb_node_dep->nhe);
175 }
176 }
177
178 struct nhg_hash_entry *zebra_nhg_resolve(struct nhg_hash_entry *nhe)
179 {
180 if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_RECURSIVE)
181 && !zebra_nhg_depends_is_empty(nhe)) {
182 nhe = nhg_connected_tree_root(&nhe->nhg_depends)->nhe;
183 return zebra_nhg_resolve(nhe);
184 }
185
186 return nhe;
187 }
188
189 unsigned int zebra_nhg_depends_count(const struct nhg_hash_entry *nhe)
190 {
191 return nhg_connected_tree_count(&nhe->nhg_depends);
192 }
193
194 bool zebra_nhg_depends_is_empty(const struct nhg_hash_entry *nhe)
195 {
196 return nhg_connected_tree_is_empty(&nhe->nhg_depends);
197 }
198
199 static void zebra_nhg_depends_del(struct nhg_hash_entry *from,
200 struct nhg_hash_entry *depend)
201 {
202 nhg_connected_tree_del_nhe(&from->nhg_depends, depend);
203 }
204
205 static void zebra_nhg_depends_init(struct nhg_hash_entry *nhe)
206 {
207 nhg_connected_tree_init(&nhe->nhg_depends);
208 }
209
210 unsigned int zebra_nhg_dependents_count(const struct nhg_hash_entry *nhe)
211 {
212 return nhg_connected_tree_count(&nhe->nhg_dependents);
213 }
214
215
216 bool zebra_nhg_dependents_is_empty(const struct nhg_hash_entry *nhe)
217 {
218 return nhg_connected_tree_is_empty(&nhe->nhg_dependents);
219 }
220
221 static void zebra_nhg_dependents_del(struct nhg_hash_entry *from,
222 struct nhg_hash_entry *dependent)
223 {
224 nhg_connected_tree_del_nhe(&from->nhg_dependents, dependent);
225 }
226
227 static void zebra_nhg_dependents_add(struct nhg_hash_entry *to,
228 struct nhg_hash_entry *dependent)
229 {
230 nhg_connected_tree_add_nhe(&to->nhg_dependents, dependent);
231 }
232
233 static void zebra_nhg_dependents_init(struct nhg_hash_entry *nhe)
234 {
235 nhg_connected_tree_init(&nhe->nhg_dependents);
236 }
237
238 /* Release this nhe from anything depending on it */
239 static void zebra_nhg_dependents_release(struct nhg_hash_entry *nhe)
240 {
241 struct nhg_connected *rb_node_dep = NULL;
242
243 frr_each_safe(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep) {
244 zebra_nhg_depends_del(rb_node_dep->nhe, nhe);
245 /* recheck validity of the dependent */
246 zebra_nhg_check_valid(rb_node_dep->nhe);
247 }
248 }
249
250 /* Release this nhe from anything that it depends on */
251 static void zebra_nhg_depends_release(struct nhg_hash_entry *nhe)
252 {
253 if (!zebra_nhg_depends_is_empty(nhe)) {
254 struct nhg_connected *rb_node_dep = NULL;
255
256 frr_each_safe(nhg_connected_tree, &nhe->nhg_depends,
257 rb_node_dep) {
258 zebra_nhg_dependents_del(rb_node_dep->nhe, nhe);
259 }
260 }
261 }
262
263
264 struct nhg_hash_entry *zebra_nhg_lookup_id(uint32_t id)
265 {
266 struct nhg_hash_entry lookup = {};
267
268 lookup.id = id;
269 return hash_lookup(zrouter.nhgs_id, &lookup);
270 }
271
272 static int zebra_nhg_insert_id(struct nhg_hash_entry *nhe)
273 {
274 if (hash_lookup(zrouter.nhgs_id, nhe)) {
275 flog_err(
276 EC_ZEBRA_NHG_TABLE_INSERT_FAILED,
277 "Failed inserting NHG id=%u into the ID hash table, entry already exists",
278 nhe->id);
279 return -1;
280 }
281
282 hash_get(zrouter.nhgs_id, nhe, hash_alloc_intern);
283
284 return 0;
285 }
286
287 static void zebra_nhg_set_if(struct nhg_hash_entry *nhe, struct interface *ifp)
288 {
289 nhe->ifp = ifp;
290 if_nhg_dependents_add(ifp, nhe);
291 }
292
293 static void
294 zebra_nhg_connect_depends(struct nhg_hash_entry *nhe,
295 struct nhg_connected_tree_head nhg_depends)
296 {
297 struct nhg_connected *rb_node_dep = NULL;
298
299 /* This has been allocated higher above in the stack. Could probably
300 * re-allocate and free the old stuff but just using the same memory
301 * for now. Otherwise, their might be a time trade-off for repeated
302 * alloc/frees as startup.
303 */
304 nhe->nhg_depends = nhg_depends;
305
306 /* Attach backpointer to anything that it depends on */
307 zebra_nhg_dependents_init(nhe);
308 if (!zebra_nhg_depends_is_empty(nhe)) {
309 frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) {
310 zebra_nhg_dependents_add(rb_node_dep->nhe, nhe);
311 }
312 }
313
314 /* Add the ifp now if its not a group or recursive and has ifindex */
315 if (zebra_nhg_depends_is_empty(nhe) && nhe->nhg->nexthop
316 && nhe->nhg->nexthop->ifindex) {
317 struct interface *ifp = NULL;
318
319 ifp = if_lookup_by_index(nhe->nhg->nexthop->ifindex,
320 nhe->vrf_id);
321 if (ifp)
322 zebra_nhg_set_if(nhe, ifp);
323 else
324 flog_err(
325 EC_ZEBRA_IF_LOOKUP_FAILED,
326 "Zebra failed to lookup an interface with ifindex=%d in vrf=%u for NHE id=%u",
327 nhe->nhg->nexthop->ifindex, nhe->vrf_id,
328 nhe->id);
329 }
330 }
331
332 struct nhg_hash_entry *zebra_nhg_alloc(void)
333 {
334 struct nhg_hash_entry *nhe;
335
336 nhe = XCALLOC(MTYPE_NHG, sizeof(struct nhg_hash_entry));
337
338 return nhe;
339 }
340
341 static struct nhg_hash_entry *zebra_nhg_copy(const struct nhg_hash_entry *copy,
342 uint32_t id)
343 {
344 struct nhg_hash_entry *nhe;
345
346 nhe = zebra_nhg_alloc();
347
348 nhe->id = id;
349
350 nhe->nhg = nexthop_group_new();
351 nexthop_group_copy(nhe->nhg, copy->nhg);
352
353 nhe->vrf_id = copy->vrf_id;
354 nhe->afi = copy->afi;
355 nhe->type = copy->type ? copy->type : ZEBRA_ROUTE_NHG;
356 nhe->refcnt = 0;
357 nhe->dplane_ref = zebra_router_get_next_sequence();
358
359 return nhe;
360 }
361
362 /* Allocation via hash handler */
363 static void *zebra_nhg_hash_alloc(void *arg)
364 {
365 struct nhg_hash_entry *nhe = NULL;
366 struct nhg_hash_entry *copy = arg;
367
368 nhe = zebra_nhg_copy(copy, copy->id);
369
370 /* Mark duplicate nexthops in a group at creation time. */
371 nexthop_group_mark_duplicates(nhe->nhg);
372
373 zebra_nhg_connect_depends(nhe, copy->nhg_depends);
374 zebra_nhg_insert_id(nhe);
375
376 return nhe;
377 }
378
379 uint32_t zebra_nhg_hash_key(const void *arg)
380 {
381 const struct nhg_hash_entry *nhe = arg;
382
383 uint32_t key = 0x5a351234;
384
385 key = jhash_3words(nhe->vrf_id, nhe->afi, nexthop_group_hash(nhe->nhg),
386 key);
387
388 return key;
389 }
390
391 uint32_t zebra_nhg_id_key(const void *arg)
392 {
393 const struct nhg_hash_entry *nhe = arg;
394
395 return nhe->id;
396 }
397
398 bool zebra_nhg_hash_equal(const void *arg1, const void *arg2)
399 {
400 const struct nhg_hash_entry *nhe1 = arg1;
401 const struct nhg_hash_entry *nhe2 = arg2;
402 struct nexthop *nexthop1;
403 struct nexthop *nexthop2;
404
405 /* No matter what if they equal IDs, assume equal */
406 if (nhe1->id && nhe2->id && (nhe1->id == nhe2->id))
407 return true;
408
409 if (nhe1->vrf_id != nhe2->vrf_id)
410 return false;
411
412 if (nhe1->afi != nhe2->afi)
413 return false;
414
415 /* Nexthops should be sorted */
416 for (nexthop1 = nhe1->nhg->nexthop, nexthop2 = nhe2->nhg->nexthop;
417 nexthop1 || nexthop2;
418 nexthop1 = nexthop1->next, nexthop2 = nexthop2->next) {
419 if (nexthop1 && !nexthop2)
420 return false;
421
422 if (!nexthop1 && nexthop2)
423 return false;
424
425 /*
426 * We have to check the active flag of each individual one,
427 * not just the overall active_num. This solves the special case
428 * issue of a route with a nexthop group with one nexthop
429 * resolving to itself and thus marking it inactive. If we
430 * have two different routes each wanting to mark a different
431 * nexthop inactive, they need to hash to two different groups.
432 *
433 * If we just hashed on num_active, they would hash the same
434 * which is incorrect.
435 *
436 * ex)
437 * 1.1.1.0/24
438 * -> 1.1.1.1 dummy1 (inactive)
439 * -> 1.1.2.1 dummy2
440 *
441 * 1.1.2.0/24
442 * -> 1.1.1.1 dummy1
443 * -> 1.1.2.1 dummy2 (inactive)
444 *
445 * Without checking each individual one, they would hash to
446 * the same group and both have 1.1.1.1 dummy1 marked inactive.
447 *
448 */
449 if (CHECK_FLAG(nexthop1->flags, NEXTHOP_FLAG_ACTIVE)
450 != CHECK_FLAG(nexthop2->flags, NEXTHOP_FLAG_ACTIVE))
451 return false;
452
453 if (!nexthop_same(nexthop1, nexthop2))
454 return false;
455 }
456
457 return true;
458 }
459
460 bool zebra_nhg_hash_id_equal(const void *arg1, const void *arg2)
461 {
462 const struct nhg_hash_entry *nhe1 = arg1;
463 const struct nhg_hash_entry *nhe2 = arg2;
464
465 return nhe1->id == nhe2->id;
466 }
467
468 static int zebra_nhg_process_grp(struct nexthop_group *nhg,
469 struct nhg_connected_tree_head *depends,
470 struct nh_grp *grp, uint8_t count)
471 {
472 nhg_connected_tree_init(depends);
473
474 for (int i = 0; i < count; i++) {
475 struct nhg_hash_entry *depend = NULL;
476 /* We do not care about nexthop_grp.weight at
477 * this time. But we should figure out
478 * how to adapt this to our code in
479 * the future.
480 */
481 depend = depends_find_id_add(depends, grp[i].id);
482
483 if (!depend) {
484 flog_err(
485 EC_ZEBRA_NHG_SYNC,
486 "Received Nexthop Group from the kernel with a dependent Nexthop ID (%u) which we do not have in our table",
487 grp[i].id);
488 return -1;
489 }
490
491 /*
492 * If this is a nexthop with its own group
493 * dependencies, add them as well. Not sure its
494 * even possible to have a group within a group
495 * in the kernel.
496 */
497
498 copy_nexthops(&nhg->nexthop, depend->nhg->nexthop, NULL);
499 }
500
501 return 0;
502 }
503
504 static void handle_recursive_depend(struct nhg_connected_tree_head *nhg_depends,
505 struct nexthop *nh, afi_t afi)
506 {
507 struct nhg_hash_entry *depend = NULL;
508 struct nexthop_group resolved_ng = {};
509
510 resolved_ng.nexthop = nh;
511
512 depend = zebra_nhg_rib_find(0, &resolved_ng, afi);
513
514 if (depend)
515 depends_add(nhg_depends, depend);
516 }
517
518 static bool zebra_nhg_find(struct nhg_hash_entry **nhe, uint32_t id,
519 struct nexthop_group *nhg,
520 struct nhg_connected_tree_head *nhg_depends,
521 vrf_id_t vrf_id, afi_t afi, int type)
522 {
523 struct nhg_hash_entry lookup = {};
524
525 uint32_t old_id_counter = id_counter;
526
527 bool created = false;
528 bool recursive = false;
529
530 /*
531 * If it has an id at this point, we must have gotten it from the kernel
532 */
533 lookup.id = id ? id : ++id_counter;
534
535 lookup.type = type ? type : ZEBRA_ROUTE_NHG;
536 lookup.nhg = nhg;
537
538 if (lookup.nhg->nexthop->next) {
539 /* Groups can have all vrfs and AF's in them */
540 lookup.afi = AFI_UNSPEC;
541 lookup.vrf_id = VRF_DEFAULT;
542 } else {
543 switch (lookup.nhg->nexthop->type) {
544 case (NEXTHOP_TYPE_IFINDEX):
545 case (NEXTHOP_TYPE_BLACKHOLE):
546 /*
547 * This switch case handles setting the afi different
548 * for ipv4/v6 routes. Ifindex/blackhole nexthop
549 * objects cannot be ambiguous, they must be Address
550 * Family specific. If we get here, we will either use
551 * the AF of the route, or the one we got passed from
552 * here from the kernel.
553 */
554 lookup.afi = afi;
555 break;
556 case (NEXTHOP_TYPE_IPV4_IFINDEX):
557 case (NEXTHOP_TYPE_IPV4):
558 lookup.afi = AFI_IP;
559 break;
560 case (NEXTHOP_TYPE_IPV6_IFINDEX):
561 case (NEXTHOP_TYPE_IPV6):
562 lookup.afi = AFI_IP6;
563 break;
564 }
565
566 lookup.vrf_id = vrf_id;
567 }
568
569 if (id)
570 (*nhe) = zebra_nhg_lookup_id(id);
571 else
572 (*nhe) = hash_lookup(zrouter.nhgs, &lookup);
573
574 /* If it found an nhe in our tables, this new ID is unused */
575 if (*nhe)
576 id_counter = old_id_counter;
577
578 if (!(*nhe)) {
579 /* Only hash/lookup the depends if the first lookup
580 * fails to find something. This should hopefully save a
581 * lot of cycles for larger ecmp sizes.
582 */
583 if (nhg_depends)
584 /* If you don't want to hash on each nexthop in the
585 * nexthop group struct you can pass the depends
586 * directly. Kernel-side we do this since it just looks
587 * them up via IDs.
588 */
589 lookup.nhg_depends = *nhg_depends;
590 else {
591 if (nhg->nexthop->next) {
592 zebra_nhg_depends_init(&lookup);
593
594 /* If its a group, create a dependency tree */
595 struct nexthop *nh = NULL;
596
597 for (nh = nhg->nexthop; nh; nh = nh->next)
598 depends_find_add(&lookup.nhg_depends,
599 nh, afi);
600 } else if (CHECK_FLAG(nhg->nexthop->flags,
601 NEXTHOP_FLAG_RECURSIVE)) {
602 zebra_nhg_depends_init(&lookup);
603 handle_recursive_depend(&lookup.nhg_depends,
604 nhg->nexthop->resolved,
605 afi);
606 recursive = true;
607 }
608 }
609
610 (*nhe) = hash_get(zrouter.nhgs, &lookup, zebra_nhg_hash_alloc);
611 created = true;
612
613 if (recursive)
614 SET_FLAG((*nhe)->flags, NEXTHOP_GROUP_RECURSIVE);
615 }
616 return created;
617 }
618
619 /* Find/create a single nexthop */
620 static struct nhg_hash_entry *
621 zebra_nhg_find_nexthop(uint32_t id, struct nexthop *nh, afi_t afi, int type)
622 {
623 struct nhg_hash_entry *nhe = NULL;
624 struct nexthop_group nhg = {};
625
626 nexthop_group_add_sorted(&nhg, nh);
627
628 zebra_nhg_find(&nhe, id, &nhg, NULL, nh->vrf_id, afi, type);
629
630 return nhe;
631 }
632
633 static uint32_t nhg_ctx_get_id(const struct nhg_ctx *ctx)
634 {
635 return ctx->id;
636 }
637
638 static void nhg_ctx_set_status(struct nhg_ctx *ctx, enum nhg_ctx_status status)
639 {
640 ctx->status = status;
641 }
642
643 static enum nhg_ctx_status nhg_ctx_get_status(const struct nhg_ctx *ctx)
644 {
645 return ctx->status;
646 }
647
648 static void nhg_ctx_set_op(struct nhg_ctx *ctx, enum nhg_ctx_op_e op)
649 {
650 ctx->op = op;
651 }
652
653 static enum nhg_ctx_op_e nhg_ctx_get_op(const struct nhg_ctx *ctx)
654 {
655 return ctx->op;
656 }
657
658 static vrf_id_t nhg_ctx_get_vrf_id(const struct nhg_ctx *ctx)
659 {
660 return ctx->vrf_id;
661 }
662
663 static int nhg_ctx_get_type(const struct nhg_ctx *ctx)
664 {
665 return ctx->type;
666 }
667
668 static int nhg_ctx_get_afi(const struct nhg_ctx *ctx)
669 {
670 return ctx->afi;
671 }
672
673 static struct nexthop *nhg_ctx_get_nh(struct nhg_ctx *ctx)
674 {
675 return &ctx->u.nh;
676 }
677
678 static uint8_t nhg_ctx_get_count(const struct nhg_ctx *ctx)
679 {
680 return ctx->count;
681 }
682
683 static struct nh_grp *nhg_ctx_get_grp(struct nhg_ctx *ctx)
684 {
685 return ctx->u.grp;
686 }
687
688 static struct nhg_ctx *nhg_ctx_new()
689 {
690 struct nhg_ctx *new = NULL;
691
692 new = XCALLOC(MTYPE_NHG_CTX, sizeof(struct nhg_ctx));
693
694 return new;
695 }
696
697 static void nhg_ctx_free(struct nhg_ctx **ctx)
698 {
699 struct nexthop *nh;
700
701 if (ctx == NULL)
702 return;
703
704 assert((*ctx) != NULL);
705
706 if (nhg_ctx_get_count(*ctx))
707 goto done;
708
709 nh = nhg_ctx_get_nh(*ctx);
710
711 nexthop_del_labels(nh);
712
713 done:
714 XFREE(MTYPE_NHG_CTX, *ctx);
715 *ctx = NULL;
716 }
717
718 static struct nhg_ctx *nhg_ctx_init(uint32_t id, struct nexthop *nh,
719 struct nh_grp *grp, vrf_id_t vrf_id,
720 afi_t afi, int type, uint8_t count)
721 {
722 struct nhg_ctx *ctx = NULL;
723
724 ctx = nhg_ctx_new();
725
726 ctx->id = id;
727 ctx->vrf_id = vrf_id;
728 ctx->afi = afi;
729 ctx->type = type;
730 ctx->count = count;
731
732 if (count)
733 /* Copy over the array */
734 memcpy(&ctx->u.grp, grp, count * sizeof(struct nh_grp));
735 else if (nh)
736 ctx->u.nh = *nh;
737
738 return ctx;
739 }
740
741 static bool zebra_nhg_contains_unhashable(struct nhg_hash_entry *nhe)
742 {
743 struct nhg_connected *rb_node_dep = NULL;
744
745 frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) {
746 if (CHECK_FLAG(rb_node_dep->nhe->flags,
747 NEXTHOP_GROUP_UNHASHABLE))
748 return true;
749 }
750
751 return false;
752 }
753
754 static void zebra_nhg_set_unhashable(struct nhg_hash_entry *nhe)
755 {
756 SET_FLAG(nhe->flags, NEXTHOP_GROUP_UNHASHABLE);
757 SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
758
759 flog_warn(
760 EC_ZEBRA_DUPLICATE_NHG_MESSAGE,
761 "Nexthop Group with ID (%d) is a duplicate, therefore unhashable, ignoring",
762 nhe->id);
763 }
764
765 static void zebra_nhg_set_valid(struct nhg_hash_entry *nhe)
766 {
767 struct nhg_connected *rb_node_dep;
768
769 SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID);
770
771 frr_each(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep)
772 zebra_nhg_set_valid(rb_node_dep->nhe);
773 }
774
775 static void zebra_nhg_set_invalid(struct nhg_hash_entry *nhe)
776 {
777 struct nhg_connected *rb_node_dep;
778
779 UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID);
780
781 /* Update validity of nexthops depending on it */
782 frr_each(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep)
783 zebra_nhg_check_valid(rb_node_dep->nhe);
784 }
785
786 void zebra_nhg_check_valid(struct nhg_hash_entry *nhe)
787 {
788 struct nhg_connected *rb_node_dep = NULL;
789 bool valid = false;
790
791 /* If anthing else in the group is valid, the group is valid */
792 frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) {
793 if (CHECK_FLAG(rb_node_dep->nhe->flags, NEXTHOP_GROUP_VALID)) {
794 valid = true;
795 goto done;
796 }
797 }
798
799 done:
800 if (valid)
801 zebra_nhg_set_valid(nhe);
802 else
803 zebra_nhg_set_invalid(nhe);
804 }
805
806
807 static void zebra_nhg_release(struct nhg_hash_entry *nhe)
808 {
809 /* Remove it from any lists it may be on */
810 zebra_nhg_depends_release(nhe);
811 zebra_nhg_dependents_release(nhe);
812 if (nhe->ifp)
813 if_nhg_dependents_del(nhe->ifp, nhe);
814
815 /*
816 * If its unhashable, we didn't store it here and have to be
817 * sure we don't clear one thats actually being used.
818 */
819 if (!CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_UNHASHABLE))
820 hash_release(zrouter.nhgs, nhe);
821
822 hash_release(zrouter.nhgs_id, nhe);
823 }
824
825 static void zebra_nhg_handle_uninstall(struct nhg_hash_entry *nhe)
826 {
827 zebra_nhg_release(nhe);
828 zebra_nhg_free(nhe);
829 }
830
831 static void zebra_nhg_handle_install(struct nhg_hash_entry *nhe)
832 {
833 /* Update validity of groups depending on it */
834 struct nhg_connected *rb_node_dep;
835
836 frr_each_safe(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep)
837 zebra_nhg_set_valid(rb_node_dep->nhe);
838 }
839
840 /*
841 * The kernel/other program has changed the state of a nexthop object we are
842 * using.
843 */
844 static void zebra_nhg_handle_kernel_state_change(struct nhg_hash_entry *nhe,
845 bool is_delete)
846 {
847 if (nhe->refcnt) {
848 flog_err(
849 EC_ZEBRA_NHG_SYNC,
850 "Kernel %s a nexthop group with ID (%u) that we are still using for a route, sending it back down",
851 (is_delete ? "deleted" : "updated"), nhe->id);
852
853 UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
854 zebra_nhg_install_kernel(nhe);
855 } else
856 zebra_nhg_handle_uninstall(nhe);
857 }
858
859 static int nhg_ctx_process_new(struct nhg_ctx *ctx)
860 {
861 struct nexthop_group *nhg = NULL;
862 struct nhg_connected_tree_head nhg_depends = {};
863 struct nhg_hash_entry *lookup = NULL;
864 struct nhg_hash_entry *nhe = NULL;
865
866 uint32_t id = nhg_ctx_get_id(ctx);
867 uint8_t count = nhg_ctx_get_count(ctx);
868 vrf_id_t vrf_id = nhg_ctx_get_vrf_id(ctx);
869 int type = nhg_ctx_get_type(ctx);
870 afi_t afi = nhg_ctx_get_afi(ctx);
871
872 lookup = zebra_nhg_lookup_id(id);
873
874 if (lookup) {
875 /* This is already present in our table, hence an update
876 * that we did not initate.
877 */
878 zebra_nhg_handle_kernel_state_change(lookup, false);
879 return 0;
880 }
881
882 if (nhg_ctx_get_count(ctx)) {
883 nhg = nexthop_group_new();
884 if (zebra_nhg_process_grp(nhg, &nhg_depends,
885 nhg_ctx_get_grp(ctx), count)) {
886 depends_decrement_free(&nhg_depends);
887 nexthop_group_delete(&nhg);
888 return -ENOENT;
889 }
890
891 if (!zebra_nhg_find(&nhe, id, nhg, &nhg_depends, vrf_id, type,
892 afi))
893 depends_decrement_free(&nhg_depends);
894
895 /* These got copied over in zebra_nhg_alloc() */
896 nexthop_group_delete(&nhg);
897 } else
898 nhe = zebra_nhg_find_nexthop(id, nhg_ctx_get_nh(ctx), afi,
899 type);
900
901 if (nhe) {
902 if (id != nhe->id) {
903 struct nhg_hash_entry *kernel_nhe = NULL;
904
905 /* Duplicate but with different ID from
906 * the kernel
907 */
908
909 /* The kernel allows duplicate nexthops
910 * as long as they have different IDs.
911 * We are ignoring those to prevent
912 * syncing problems with the kernel
913 * changes.
914 *
915 * We maintain them *ONLY* in the ID hash table to
916 * track them and set the flag to indicated
917 * their attributes are unhashable.
918 */
919
920 kernel_nhe = zebra_nhg_copy(nhe, id);
921 zebra_nhg_insert_id(kernel_nhe);
922 zebra_nhg_set_unhashable(kernel_nhe);
923 } else if (zebra_nhg_contains_unhashable(nhe)) {
924 /* The group we got contains an unhashable/duplicated
925 * depend, so lets mark this group as unhashable as well
926 * and release it from the non-ID hash.
927 */
928 hash_release(zrouter.nhgs, nhe);
929 zebra_nhg_set_unhashable(nhe);
930 } else {
931 /* It actually created a new nhe */
932 SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID);
933 SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
934 }
935 } else {
936 flog_err(
937 EC_ZEBRA_TABLE_LOOKUP_FAILED,
938 "Zebra failed to find or create a nexthop hash entry for ID (%u)",
939 id);
940 return -1;
941 }
942
943 return 0;
944 }
945
946 static int nhg_ctx_process_del(struct nhg_ctx *ctx)
947 {
948 struct nhg_hash_entry *nhe = NULL;
949 uint32_t id = nhg_ctx_get_id(ctx);
950
951 nhe = zebra_nhg_lookup_id(id);
952
953 if (!nhe) {
954 flog_warn(
955 EC_ZEBRA_BAD_NHG_MESSAGE,
956 "Kernel delete message received for nexthop group ID (%u) that we do not have in our ID table",
957 id);
958 return -1;
959 }
960
961 zebra_nhg_handle_kernel_state_change(nhe, true);
962
963 return 0;
964 }
965
966 static void nhg_ctx_fini(struct nhg_ctx **ctx)
967 {
968 /*
969 * Just freeing for now, maybe do something more in the future
970 * based on flag.
971 */
972
973 nhg_ctx_free(ctx);
974 }
975
976 static int queue_add(struct nhg_ctx *ctx)
977 {
978 /* If its queued or already processed do nothing */
979 if (nhg_ctx_get_status(ctx) == NHG_CTX_QUEUED)
980 return 0;
981
982 if (rib_queue_nhg_add(ctx)) {
983 nhg_ctx_set_status(ctx, NHG_CTX_FAILURE);
984 return -1;
985 }
986
987 nhg_ctx_set_status(ctx, NHG_CTX_QUEUED);
988
989 return 0;
990 }
991
992 int nhg_ctx_process(struct nhg_ctx *ctx)
993 {
994 int ret = 0;
995
996 switch (nhg_ctx_get_op(ctx)) {
997 case NHG_CTX_OP_NEW:
998 ret = nhg_ctx_process_new(ctx);
999 if (nhg_ctx_get_count(ctx) && ret == -ENOENT
1000 && nhg_ctx_get_status(ctx) != NHG_CTX_REQUEUED) {
1001 /**
1002 * We have entered a situation where we are
1003 * processing a group from the kernel
1004 * that has a contained nexthop which
1005 * we have not yet processed.
1006 *
1007 * Re-enqueue this ctx to be handled exactly one
1008 * more time (indicated by the flag).
1009 *
1010 * By the time we get back to it, we
1011 * should have processed its depends.
1012 */
1013 nhg_ctx_set_status(ctx, NHG_CTX_NONE);
1014 if (queue_add(ctx) == 0) {
1015 nhg_ctx_set_status(ctx, NHG_CTX_REQUEUED);
1016 return 0;
1017 }
1018 }
1019 break;
1020 case NHG_CTX_OP_DEL:
1021 ret = nhg_ctx_process_del(ctx);
1022 case NHG_CTX_OP_NONE:
1023 break;
1024 }
1025
1026 nhg_ctx_set_status(ctx, (ret ? NHG_CTX_FAILURE : NHG_CTX_SUCCESS));
1027
1028 nhg_ctx_fini(&ctx);
1029
1030 return ret;
1031 }
1032
1033 /* Kernel-side, you either get a single new nexthop or a array of ID's */
1034 int zebra_nhg_kernel_find(uint32_t id, struct nexthop *nh, struct nh_grp *grp,
1035 uint8_t count, vrf_id_t vrf_id, afi_t afi, int type,
1036 int startup)
1037 {
1038 struct nhg_ctx *ctx = NULL;
1039
1040 if (id > id_counter)
1041 /* Increase our counter so we don't try to create
1042 * an ID that already exists
1043 */
1044 id_counter = id;
1045
1046 ctx = nhg_ctx_init(id, nh, grp, vrf_id, afi, type, count);
1047 nhg_ctx_set_op(ctx, NHG_CTX_OP_NEW);
1048
1049 /* Under statup conditions, we need to handle them immediately
1050 * like we do for routes. Otherwise, we are going to get a route
1051 * with a nhe_id that we have not handled.
1052 */
1053 if (startup)
1054 return nhg_ctx_process(ctx);
1055
1056 if (queue_add(ctx)) {
1057 nhg_ctx_fini(&ctx);
1058 return -1;
1059 }
1060
1061 return 0;
1062 }
1063
1064 /* Kernel-side, received delete message */
1065 int zebra_nhg_kernel_del(uint32_t id)
1066 {
1067 struct nhg_ctx *ctx = NULL;
1068
1069 ctx = nhg_ctx_init(id, NULL, NULL, 0, 0, 0, 0);
1070
1071 nhg_ctx_set_op(ctx, NHG_CTX_OP_DEL);
1072
1073 if (queue_add(ctx)) {
1074 nhg_ctx_fini(&ctx);
1075 return -1;
1076 }
1077
1078 return 0;
1079 }
1080
1081 /* Some dependency helper functions */
1082 static struct nhg_hash_entry *depends_find_recursive(const struct nexthop *nh,
1083 afi_t afi)
1084 {
1085 struct nhg_hash_entry *nhe;
1086 struct nexthop *lookup = NULL;
1087
1088 lookup = nexthop_dup(nh, NULL);
1089
1090 nhe = zebra_nhg_find_nexthop(0, lookup, afi, 0);
1091
1092 nexthops_free(lookup);
1093
1094 return nhe;
1095 }
1096
1097 static struct nhg_hash_entry *depends_find_singleton(const struct nexthop *nh,
1098 afi_t afi)
1099 {
1100 struct nhg_hash_entry *nhe;
1101 struct nexthop lookup = {};
1102
1103 /* Capture a snapshot of this single nh; it might be part of a list,
1104 * so we need to make a standalone copy.
1105 */
1106 nexthop_copy_no_recurse(&lookup, nh, NULL);
1107
1108 nhe = zebra_nhg_find_nexthop(0, &lookup, afi, 0);
1109
1110 /* The copy may have allocated labels; free them if necessary. */
1111 nexthop_del_labels(&lookup);
1112
1113 return nhe;
1114 }
1115
1116 static struct nhg_hash_entry *depends_find(const struct nexthop *nh, afi_t afi)
1117 {
1118 struct nhg_hash_entry *nhe = NULL;
1119
1120 if (!nh)
1121 goto done;
1122
1123 /* We are separating these functions out to increase handling speed
1124 * in the non-recursive case (by not alloc/freeing)
1125 */
1126 if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE))
1127 nhe = depends_find_recursive(nh, afi);
1128 else
1129 nhe = depends_find_singleton(nh, afi);
1130
1131 done:
1132 return nhe;
1133 }
1134
1135 static void depends_add(struct nhg_connected_tree_head *head,
1136 struct nhg_hash_entry *depend)
1137 {
1138 /* If NULL is returned, it was successfully added and
1139 * needs to have its refcnt incremented.
1140 *
1141 * Else the NHE is already present in the tree and doesn't
1142 * need to increment the refcnt.
1143 */
1144 if (nhg_connected_tree_add_nhe(head, depend) == NULL)
1145 zebra_nhg_increment_ref(depend);
1146 }
1147
1148 static struct nhg_hash_entry *
1149 depends_find_add(struct nhg_connected_tree_head *head, struct nexthop *nh,
1150 afi_t afi)
1151 {
1152 struct nhg_hash_entry *depend = NULL;
1153
1154 depend = depends_find(nh, afi);
1155
1156 if (depend)
1157 depends_add(head, depend);
1158
1159 return depend;
1160 }
1161
1162 static struct nhg_hash_entry *
1163 depends_find_id_add(struct nhg_connected_tree_head *head, uint32_t id)
1164 {
1165 struct nhg_hash_entry *depend = NULL;
1166
1167 depend = zebra_nhg_lookup_id(id);
1168
1169 if (depend)
1170 depends_add(head, depend);
1171
1172 return depend;
1173 }
1174
1175 static void depends_decrement_free(struct nhg_connected_tree_head *head)
1176 {
1177 nhg_connected_tree_decrement_ref(head);
1178 nhg_connected_tree_free(head);
1179 }
1180
1181 /* Rib-side, you get a nexthop group struct */
1182 struct nhg_hash_entry *
1183 zebra_nhg_rib_find(uint32_t id, struct nexthop_group *nhg, afi_t rt_afi)
1184 {
1185 struct nhg_hash_entry *nhe = NULL;
1186
1187 if (!(nhg && nhg->nexthop)) {
1188 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED,
1189 "No nexthop passed to %s", __func__);
1190 return NULL;
1191 }
1192
1193 zebra_nhg_find(&nhe, id, nhg, NULL, nhg->nexthop->vrf_id, rt_afi, 0);
1194
1195 return nhe;
1196 }
1197
1198 static void zebra_nhg_free_members(struct nhg_hash_entry *nhe)
1199 {
1200 nexthop_group_delete(&nhe->nhg);
1201 /* Decrement to remove connection ref */
1202 nhg_connected_tree_decrement_ref(&nhe->nhg_depends);
1203 nhg_connected_tree_free(&nhe->nhg_depends);
1204 nhg_connected_tree_free(&nhe->nhg_dependents);
1205 }
1206
1207 void zebra_nhg_free(struct nhg_hash_entry *nhe)
1208 {
1209 if (nhe->refcnt)
1210 zlog_debug("nhe_id=%u hash refcnt=%d", nhe->id, nhe->refcnt);
1211
1212 zebra_nhg_free_members(nhe);
1213
1214 XFREE(MTYPE_NHG, nhe);
1215 }
1216
1217 void zebra_nhg_hash_free(void *p)
1218 {
1219 zebra_nhg_free((struct nhg_hash_entry *)p);
1220 }
1221
1222 void zebra_nhg_decrement_ref(struct nhg_hash_entry *nhe)
1223 {
1224 nhe->refcnt--;
1225
1226 if (!zebra_nhg_depends_is_empty(nhe))
1227 nhg_connected_tree_decrement_ref(&nhe->nhg_depends);
1228
1229 if (ZEBRA_NHG_CREATED(nhe) && nhe->refcnt <= 0)
1230 zebra_nhg_uninstall_kernel(nhe);
1231 }
1232
1233 void zebra_nhg_increment_ref(struct nhg_hash_entry *nhe)
1234 {
1235 nhe->refcnt++;
1236
1237 if (!zebra_nhg_depends_is_empty(nhe))
1238 nhg_connected_tree_increment_ref(&nhe->nhg_depends);
1239 }
1240
1241 static void nexthop_set_resolved(afi_t afi, const struct nexthop *newhop,
1242 struct nexthop *nexthop)
1243 {
1244 struct nexthop *resolved_hop;
1245 uint8_t num_labels = 0;
1246 mpls_label_t labels[MPLS_MAX_LABELS];
1247 enum lsp_types_t label_type = ZEBRA_LSP_NONE;
1248 int i = 0;
1249
1250 resolved_hop = nexthop_new();
1251 SET_FLAG(resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
1252
1253 resolved_hop->vrf_id = nexthop->vrf_id;
1254 switch (newhop->type) {
1255 case NEXTHOP_TYPE_IPV4:
1256 case NEXTHOP_TYPE_IPV4_IFINDEX:
1257 /* If the resolving route specifies a gateway, use it */
1258 resolved_hop->type = newhop->type;
1259 resolved_hop->gate.ipv4 = newhop->gate.ipv4;
1260
1261 if (newhop->ifindex) {
1262 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
1263 resolved_hop->ifindex = newhop->ifindex;
1264 }
1265 break;
1266 case NEXTHOP_TYPE_IPV6:
1267 case NEXTHOP_TYPE_IPV6_IFINDEX:
1268 resolved_hop->type = newhop->type;
1269 resolved_hop->gate.ipv6 = newhop->gate.ipv6;
1270
1271 if (newhop->ifindex) {
1272 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1273 resolved_hop->ifindex = newhop->ifindex;
1274 }
1275 break;
1276 case NEXTHOP_TYPE_IFINDEX:
1277 /* If the resolving route is an interface route,
1278 * it means the gateway we are looking up is connected
1279 * to that interface. (The actual network is _not_ onlink).
1280 * Therefore, the resolved route should have the original
1281 * gateway as nexthop as it is directly connected.
1282 *
1283 * On Linux, we have to set the onlink netlink flag because
1284 * otherwise, the kernel won't accept the route.
1285 */
1286 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
1287 if (afi == AFI_IP) {
1288 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
1289 resolved_hop->gate.ipv4 = nexthop->gate.ipv4;
1290 } else if (afi == AFI_IP6) {
1291 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1292 resolved_hop->gate.ipv6 = nexthop->gate.ipv6;
1293 }
1294 resolved_hop->ifindex = newhop->ifindex;
1295 break;
1296 case NEXTHOP_TYPE_BLACKHOLE:
1297 resolved_hop->type = NEXTHOP_TYPE_BLACKHOLE;
1298 resolved_hop->bh_type = newhop->bh_type;
1299 break;
1300 }
1301
1302 if (newhop->flags & NEXTHOP_FLAG_ONLINK)
1303 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
1304
1305 /* Copy labels of the resolved route and the parent resolving to it */
1306 if (newhop->nh_label) {
1307 for (i = 0; i < newhop->nh_label->num_labels; i++)
1308 labels[num_labels++] = newhop->nh_label->label[i];
1309 label_type = newhop->nh_label_type;
1310 }
1311
1312 if (nexthop->nh_label) {
1313 for (i = 0; i < nexthop->nh_label->num_labels; i++)
1314 labels[num_labels++] = nexthop->nh_label->label[i];
1315
1316 /* If the parent has labels, use its type */
1317 label_type = nexthop->nh_label_type;
1318 }
1319
1320 if (num_labels)
1321 nexthop_add_labels(resolved_hop, label_type, num_labels,
1322 labels);
1323
1324 resolved_hop->rparent = nexthop;
1325 _nexthop_add(&nexthop->resolved, resolved_hop);
1326 }
1327
1328 /* Checks if nexthop we are trying to resolve to is valid */
1329 static bool nexthop_valid_resolve(const struct nexthop *nexthop,
1330 const struct nexthop *resolved)
1331 {
1332 /* Can't resolve to a recursive nexthop */
1333 if (CHECK_FLAG(resolved->flags, NEXTHOP_FLAG_RECURSIVE))
1334 return false;
1335
1336 switch (nexthop->type) {
1337 case NEXTHOP_TYPE_IPV4_IFINDEX:
1338 case NEXTHOP_TYPE_IPV6_IFINDEX:
1339 /* If the nexthop we are resolving to does not match the
1340 * ifindex for the nexthop the route wanted, its not valid.
1341 */
1342 if (nexthop->ifindex != resolved->ifindex)
1343 return false;
1344 break;
1345 case NEXTHOP_TYPE_IPV4:
1346 case NEXTHOP_TYPE_IPV6:
1347 case NEXTHOP_TYPE_IFINDEX:
1348 case NEXTHOP_TYPE_BLACKHOLE:
1349 break;
1350 }
1351
1352 return true;
1353 }
1354
1355 /*
1356 * Given a nexthop we need to properly recursively resolve
1357 * the route. As such, do a table lookup to find and match
1358 * if at all possible. Set the nexthop->ifindex and resolved_id
1359 * as appropriate
1360 */
1361 static int nexthop_active(afi_t afi, struct route_entry *re,
1362 struct nexthop *nexthop, struct route_node *top)
1363 {
1364 struct prefix p;
1365 struct route_table *table;
1366 struct route_node *rn;
1367 struct route_entry *match = NULL;
1368 int resolved;
1369 struct nexthop *newhop;
1370 struct interface *ifp;
1371 rib_dest_t *dest;
1372 struct zebra_vrf *zvrf;
1373
1374 if ((nexthop->type == NEXTHOP_TYPE_IPV4)
1375 || nexthop->type == NEXTHOP_TYPE_IPV6)
1376 nexthop->ifindex = 0;
1377
1378
1379 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
1380 nexthops_free(nexthop->resolved);
1381 nexthop->resolved = NULL;
1382 re->nexthop_mtu = 0;
1383
1384 /*
1385 * If the kernel has sent us a NEW route, then
1386 * by golly gee whiz it's a good route.
1387 *
1388 * If its an already INSTALLED route we have already handled, then the
1389 * kernel route's nexthop might have became unreachable
1390 * and we have to handle that.
1391 */
1392 if (!CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED)
1393 && (re->type == ZEBRA_ROUTE_KERNEL
1394 || re->type == ZEBRA_ROUTE_SYSTEM))
1395 return 1;
1396
1397 /*
1398 * Check to see if we should trust the passed in information
1399 * for UNNUMBERED interfaces as that we won't find the GW
1400 * address in the routing table.
1401 * This check should suffice to handle IPv4 or IPv6 routes
1402 * sourced from EVPN routes which are installed with the
1403 * next hop as the remote VTEP IP.
1404 */
1405 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)) {
1406 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
1407 if (!ifp) {
1408 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1409 zlog_debug(
1410 "\t%s: Onlink and interface: %u[%u] does not exist",
1411 __PRETTY_FUNCTION__, nexthop->ifindex,
1412 nexthop->vrf_id);
1413 return 0;
1414 }
1415 if (connected_is_unnumbered(ifp)) {
1416 if (if_is_operative(ifp))
1417 return 1;
1418
1419 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1420 zlog_debug(
1421 "\t%s: Onlink and interface %s is not operative",
1422 __PRETTY_FUNCTION__, ifp->name);
1423 return 0;
1424 }
1425 if (!if_is_operative(ifp)) {
1426 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1427 zlog_debug(
1428 "\t%s: Interface %s is not unnumbered",
1429 __PRETTY_FUNCTION__, ifp->name);
1430 return 0;
1431 }
1432 }
1433
1434 if ((top->p.family == AF_INET && top->p.prefixlen == 32
1435 && nexthop->gate.ipv4.s_addr == top->p.u.prefix4.s_addr)
1436 || (top->p.family == AF_INET6 && top->p.prefixlen == 128
1437 && memcmp(&nexthop->gate.ipv6, &top->p.u.prefix6, 16) == 0)) {
1438 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1439 zlog_debug(
1440 "\t:%s: Attempting to install a max prefixlength route through itself",
1441 __PRETTY_FUNCTION__);
1442 return 0;
1443 }
1444
1445 /* Make lookup prefix. */
1446 memset(&p, 0, sizeof(struct prefix));
1447 switch (afi) {
1448 case AFI_IP:
1449 p.family = AF_INET;
1450 p.prefixlen = IPV4_MAX_PREFIXLEN;
1451 p.u.prefix4 = nexthop->gate.ipv4;
1452 break;
1453 case AFI_IP6:
1454 p.family = AF_INET6;
1455 p.prefixlen = IPV6_MAX_PREFIXLEN;
1456 p.u.prefix6 = nexthop->gate.ipv6;
1457 break;
1458 default:
1459 assert(afi != AFI_IP && afi != AFI_IP6);
1460 break;
1461 }
1462 /* Lookup table. */
1463 table = zebra_vrf_table(afi, SAFI_UNICAST, nexthop->vrf_id);
1464 /* get zvrf */
1465 zvrf = zebra_vrf_lookup_by_id(nexthop->vrf_id);
1466 if (!table || !zvrf) {
1467 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1468 zlog_debug("\t%s: Table not found",
1469 __PRETTY_FUNCTION__);
1470 return 0;
1471 }
1472
1473 rn = route_node_match(table, (struct prefix *)&p);
1474 while (rn) {
1475 route_unlock_node(rn);
1476
1477 /* Lookup should halt if we've matched against ourselves ('top',
1478 * if specified) - i.e., we cannot have a nexthop NH1 is
1479 * resolved by a route NH1. The exception is if the route is a
1480 * host route.
1481 */
1482 if (top && rn == top)
1483 if (((afi == AFI_IP) && (rn->p.prefixlen != 32))
1484 || ((afi == AFI_IP6) && (rn->p.prefixlen != 128))) {
1485 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1486 zlog_debug(
1487 "\t%s: Matched against ourself and prefix length is not max bit length",
1488 __PRETTY_FUNCTION__);
1489 return 0;
1490 }
1491
1492 /* Pick up selected route. */
1493 /* However, do not resolve over default route unless explicitly
1494 * allowed.
1495 */
1496 if (is_default_prefix(&rn->p)
1497 && !rnh_resolve_via_default(zvrf, p.family)) {
1498 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1499 zlog_debug(
1500 "\t:%s: Resolved against default route",
1501 __PRETTY_FUNCTION__);
1502 return 0;
1503 }
1504
1505 dest = rib_dest_from_rnode(rn);
1506 if (dest && dest->selected_fib
1507 && !CHECK_FLAG(dest->selected_fib->status,
1508 ROUTE_ENTRY_REMOVED)
1509 && dest->selected_fib->type != ZEBRA_ROUTE_TABLE)
1510 match = dest->selected_fib;
1511
1512 /* If there is no selected route or matched route is EGP, go up
1513 * tree.
1514 */
1515 if (!match) {
1516 do {
1517 rn = rn->parent;
1518 } while (rn && rn->info == NULL);
1519 if (rn)
1520 route_lock_node(rn);
1521
1522 continue;
1523 }
1524
1525 if (match->type == ZEBRA_ROUTE_CONNECT) {
1526 /* Directly point connected route. */
1527 newhop = match->nhe->nhg->nexthop;
1528 if (newhop) {
1529 if (nexthop->type == NEXTHOP_TYPE_IPV4
1530 || nexthop->type == NEXTHOP_TYPE_IPV6)
1531 nexthop->ifindex = newhop->ifindex;
1532 }
1533 return 1;
1534 } else if (CHECK_FLAG(re->flags, ZEBRA_FLAG_ALLOW_RECURSION)) {
1535 resolved = 0;
1536 for (ALL_NEXTHOPS_PTR(match->nhe->nhg, newhop)) {
1537 if (!CHECK_FLAG(match->status,
1538 ROUTE_ENTRY_INSTALLED))
1539 continue;
1540 if (!nexthop_valid_resolve(nexthop, newhop))
1541 continue;
1542
1543 SET_FLAG(nexthop->flags,
1544 NEXTHOP_FLAG_RECURSIVE);
1545 nexthop_set_resolved(afi, newhop, nexthop);
1546 resolved = 1;
1547 }
1548 if (resolved)
1549 re->nexthop_mtu = match->mtu;
1550
1551 if (!resolved && IS_ZEBRA_DEBUG_RIB_DETAILED)
1552 zlog_debug("\t%s: Recursion failed to find",
1553 __PRETTY_FUNCTION__);
1554 return resolved;
1555 } else if (re->type == ZEBRA_ROUTE_STATIC) {
1556 resolved = 0;
1557 for (ALL_NEXTHOPS_PTR(match->nhe->nhg, newhop)) {
1558 if (!CHECK_FLAG(match->status,
1559 ROUTE_ENTRY_INSTALLED))
1560 continue;
1561 if (!nexthop_valid_resolve(nexthop, newhop))
1562 continue;
1563
1564 SET_FLAG(nexthop->flags,
1565 NEXTHOP_FLAG_RECURSIVE);
1566 nexthop_set_resolved(afi, newhop, nexthop);
1567 resolved = 1;
1568 }
1569 if (resolved)
1570 re->nexthop_mtu = match->mtu;
1571
1572 if (!resolved && IS_ZEBRA_DEBUG_RIB_DETAILED)
1573 zlog_debug(
1574 "\t%s: Static route unable to resolve",
1575 __PRETTY_FUNCTION__);
1576 return resolved;
1577 } else {
1578 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1579 zlog_debug(
1580 "\t%s: Route Type %s has not turned on recursion",
1581 __PRETTY_FUNCTION__,
1582 zebra_route_string(re->type));
1583 if (re->type == ZEBRA_ROUTE_BGP
1584 && !CHECK_FLAG(re->flags, ZEBRA_FLAG_IBGP))
1585 zlog_debug(
1586 "\tEBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\"");
1587 }
1588 return 0;
1589 }
1590 }
1591 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1592 zlog_debug("\t%s: Nexthop did not lookup in table",
1593 __PRETTY_FUNCTION__);
1594 return 0;
1595 }
1596
1597 /* This function verifies reachability of one given nexthop, which can be
1598 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
1599 * in nexthop->flags field. The nexthop->ifindex will be updated
1600 * appropriately as well. An existing route map can turn
1601 * (otherwise active) nexthop into inactive, but not vice versa.
1602 *
1603 * If it finds a nexthop recursivedly, set the resolved_id
1604 * to match that nexthop's nhg_hash_entry ID;
1605 *
1606 * The return value is the final value of 'ACTIVE' flag.
1607 */
1608 static unsigned nexthop_active_check(struct route_node *rn,
1609 struct route_entry *re,
1610 struct nexthop *nexthop)
1611 {
1612 struct interface *ifp;
1613 route_map_result_t ret = RMAP_PERMITMATCH;
1614 int family;
1615 char buf[SRCDEST2STR_BUFFER];
1616 const struct prefix *p, *src_p;
1617 struct zebra_vrf *zvrf;
1618
1619 srcdest_rnode_prefixes(rn, &p, &src_p);
1620
1621 if (rn->p.family == AF_INET)
1622 family = AFI_IP;
1623 else if (rn->p.family == AF_INET6)
1624 family = AFI_IP6;
1625 else
1626 family = 0;
1627 switch (nexthop->type) {
1628 case NEXTHOP_TYPE_IFINDEX:
1629 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
1630 if (ifp && if_is_operative(ifp))
1631 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1632 else
1633 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1634 break;
1635 case NEXTHOP_TYPE_IPV4:
1636 case NEXTHOP_TYPE_IPV4_IFINDEX:
1637 family = AFI_IP;
1638 if (nexthop_active(AFI_IP, re, nexthop, rn))
1639 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1640 else
1641 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1642 break;
1643 case NEXTHOP_TYPE_IPV6:
1644 family = AFI_IP6;
1645 if (nexthop_active(AFI_IP6, re, nexthop, rn))
1646 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1647 else
1648 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1649 break;
1650 case NEXTHOP_TYPE_IPV6_IFINDEX:
1651 /* RFC 5549, v4 prefix with v6 NH */
1652 if (rn->p.family != AF_INET)
1653 family = AFI_IP6;
1654 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->gate.ipv6)) {
1655 ifp = if_lookup_by_index(nexthop->ifindex,
1656 nexthop->vrf_id);
1657 if (ifp && if_is_operative(ifp))
1658 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1659 else
1660 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1661 } else {
1662 if (nexthop_active(AFI_IP6, re, nexthop, rn))
1663 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1664 else
1665 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1666 }
1667 break;
1668 case NEXTHOP_TYPE_BLACKHOLE:
1669 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1670 break;
1671 default:
1672 break;
1673 }
1674 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) {
1675 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1676 zlog_debug("\t%s: Unable to find a active nexthop",
1677 __PRETTY_FUNCTION__);
1678 return 0;
1679 }
1680
1681 /* XXX: What exactly do those checks do? Do we support
1682 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
1683 */
1684 if (RIB_SYSTEM_ROUTE(re) || (family == AFI_IP && p->family != AF_INET)
1685 || (family == AFI_IP6 && p->family != AF_INET6))
1686 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1687
1688 /* The original code didn't determine the family correctly
1689 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
1690 * from the rib_table_info in those cases.
1691 * Possibly it may be better to use only the rib_table_info
1692 * in every case.
1693 */
1694 if (!family) {
1695 rib_table_info_t *info;
1696
1697 info = srcdest_rnode_table_info(rn);
1698 family = info->afi;
1699 }
1700
1701 memset(&nexthop->rmap_src.ipv6, 0, sizeof(union g_addr));
1702
1703 zvrf = zebra_vrf_lookup_by_id(nexthop->vrf_id);
1704 if (!zvrf) {
1705 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1706 zlog_debug("\t%s: zvrf is NULL", __PRETTY_FUNCTION__);
1707 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1708 }
1709
1710 /* It'll get set if required inside */
1711 ret = zebra_route_map_check(family, re->type, re->instance, p, nexthop,
1712 zvrf, re->tag);
1713 if (ret == RMAP_DENYMATCH) {
1714 if (IS_ZEBRA_DEBUG_RIB) {
1715 srcdest_rnode2str(rn, buf, sizeof(buf));
1716 zlog_debug(
1717 "%u:%s: Filtering out with NH out %s due to route map",
1718 re->vrf_id, buf,
1719 ifindex2ifname(nexthop->ifindex,
1720 nexthop->vrf_id));
1721 }
1722 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1723 }
1724 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1725 }
1726
1727 /*
1728 * Iterate over all nexthops of the given RIB entry and refresh their
1729 * ACTIVE flag. If any nexthop is found to toggle the ACTIVE flag,
1730 * the whole re structure is flagged with ROUTE_ENTRY_CHANGED.
1731 *
1732 * Return value is the new number of active nexthops.
1733 */
1734 int nexthop_active_update(struct route_node *rn, struct route_entry *re)
1735 {
1736 struct nexthop_group new_grp = {};
1737 struct nexthop *nexthop;
1738 union g_addr prev_src;
1739 unsigned int prev_active, new_active;
1740 ifindex_t prev_index;
1741 uint8_t curr_active = 0;
1742
1743 afi_t rt_afi = family2afi(rn->p.family);
1744
1745 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1746
1747 /* Copy over the nexthops in current state */
1748 nexthop_group_copy(&new_grp, re->nhe->nhg);
1749
1750 for (nexthop = new_grp.nexthop; nexthop; nexthop = nexthop->next) {
1751
1752 /* No protocol daemon provides src and so we're skipping
1753 * tracking it */
1754 prev_src = nexthop->rmap_src;
1755 prev_active = CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1756 prev_index = nexthop->ifindex;
1757 /*
1758 * We need to respect the multipath_num here
1759 * as that what we should be able to install from
1760 * a multipath perpsective should not be a data plane
1761 * decision point.
1762 */
1763 new_active =
1764 nexthop_active_check(rn, re, nexthop);
1765
1766 if (new_active && curr_active >= zrouter.multipath_num) {
1767 struct nexthop *nh;
1768
1769 /* Set it and its resolved nexthop as inactive. */
1770 for (nh = nexthop; nh; nh = nh->resolved)
1771 UNSET_FLAG(nh->flags, NEXTHOP_FLAG_ACTIVE);
1772
1773 new_active = 0;
1774 }
1775
1776 if (new_active)
1777 curr_active++;
1778
1779 /* Don't allow src setting on IPv6 addr for now */
1780 if (prev_active != new_active || prev_index != nexthop->ifindex
1781 || ((nexthop->type >= NEXTHOP_TYPE_IFINDEX
1782 && nexthop->type < NEXTHOP_TYPE_IPV6)
1783 && prev_src.ipv4.s_addr
1784 != nexthop->rmap_src.ipv4.s_addr)
1785 || ((nexthop->type >= NEXTHOP_TYPE_IPV6
1786 && nexthop->type < NEXTHOP_TYPE_BLACKHOLE)
1787 && !(IPV6_ADDR_SAME(&prev_src.ipv6,
1788 &nexthop->rmap_src.ipv6)))
1789 || CHECK_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED))
1790 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1791 }
1792
1793 if (CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)) {
1794 struct nhg_hash_entry *new_nhe = NULL;
1795
1796 new_nhe = zebra_nhg_rib_find(0, &new_grp, rt_afi);
1797
1798 route_entry_update_nhe(re, new_nhe);
1799 }
1800
1801 if (curr_active) {
1802 struct nhg_hash_entry *nhe = NULL;
1803
1804 nhe = zebra_nhg_lookup_id(re->nhe_id);
1805
1806 if (nhe)
1807 SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID);
1808 else
1809 flog_err(
1810 EC_ZEBRA_TABLE_LOOKUP_FAILED,
1811 "Active update on NHE id=%u that we do not have in our tables",
1812 re->nhe_id);
1813 }
1814
1815 /*
1816 * Do not need these nexthops anymore since they
1817 * were either copied over into an nhe or not
1818 * used at all.
1819 */
1820 nexthops_free(new_grp.nexthop);
1821 return curr_active;
1822 }
1823
1824 /* Convert a nhe into a group array */
1825 uint8_t zebra_nhg_nhe2grp(struct nh_grp *grp, struct nhg_hash_entry *nhe,
1826 int max_num)
1827 {
1828 struct nhg_connected *rb_node_dep = NULL;
1829 struct nhg_hash_entry *depend = NULL;
1830 uint8_t i = 0;
1831
1832 frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) {
1833 bool duplicate = false;
1834
1835 depend = rb_node_dep->nhe;
1836
1837 /*
1838 * If its recursive, use its resolved nhe in the group
1839 */
1840 if (CHECK_FLAG(depend->flags, NEXTHOP_GROUP_RECURSIVE)) {
1841 depend = zebra_nhg_resolve(depend);
1842 if (!depend) {
1843 flog_err(
1844 EC_ZEBRA_NHG_FIB_UPDATE,
1845 "Failed to recursively resolve Nexthop Hash Entry in the group id=%u",
1846 nhe->id);
1847 continue;
1848 }
1849 }
1850
1851 /* Check for duplicate IDs, kernel doesn't like that */
1852 for (int j = 0; j < i; j++) {
1853 if (depend->id == grp[j].id)
1854 duplicate = true;
1855 }
1856
1857 if (!duplicate) {
1858 grp[i].id = depend->id;
1859 /* We aren't using weights for anything right now */
1860 grp[i].weight = depend->nhg->nexthop->weight;
1861 i++;
1862 }
1863
1864 if (i >= max_num)
1865 goto done;
1866 }
1867
1868 done:
1869 return i;
1870 }
1871
1872 void zebra_nhg_install_kernel(struct nhg_hash_entry *nhe)
1873 {
1874 struct nhg_connected *rb_node_dep = NULL;
1875
1876 /* Resolve it first */
1877 nhe = zebra_nhg_resolve(nhe);
1878
1879 /* Make sure all depends are installed/queued */
1880 frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) {
1881 zebra_nhg_install_kernel(rb_node_dep->nhe);
1882 }
1883
1884 if (!CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED)
1885 && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED)) {
1886 /* Change its type to us since we are installing it */
1887 nhe->type = ZEBRA_ROUTE_NHG;
1888
1889 int ret = dplane_nexthop_add(nhe);
1890
1891 switch (ret) {
1892 case ZEBRA_DPLANE_REQUEST_QUEUED:
1893 SET_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED);
1894 break;
1895 case ZEBRA_DPLANE_REQUEST_FAILURE:
1896 flog_err(
1897 EC_ZEBRA_DP_INSTALL_FAIL,
1898 "Failed to install Nexthop ID (%u) into the kernel",
1899 nhe->id);
1900 break;
1901 case ZEBRA_DPLANE_REQUEST_SUCCESS:
1902 SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
1903 zebra_nhg_handle_install(nhe);
1904 break;
1905 }
1906 }
1907 }
1908
1909 void zebra_nhg_uninstall_kernel(struct nhg_hash_entry *nhe)
1910 {
1911 if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED)) {
1912 int ret = dplane_nexthop_delete(nhe);
1913
1914 switch (ret) {
1915 case ZEBRA_DPLANE_REQUEST_QUEUED:
1916 SET_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED);
1917 break;
1918 case ZEBRA_DPLANE_REQUEST_FAILURE:
1919 flog_err(
1920 EC_ZEBRA_DP_DELETE_FAIL,
1921 "Failed to uninstall Nexthop ID (%u) from the kernel",
1922 nhe->id);
1923 break;
1924 case ZEBRA_DPLANE_REQUEST_SUCCESS:
1925 UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
1926 break;
1927 }
1928 }
1929
1930 zebra_nhg_handle_uninstall(nhe);
1931 }
1932
1933 void zebra_nhg_dplane_result(struct zebra_dplane_ctx *ctx)
1934 {
1935 enum dplane_op_e op;
1936 enum zebra_dplane_result status;
1937 uint32_t id = 0;
1938 struct nhg_hash_entry *nhe = NULL;
1939
1940 op = dplane_ctx_get_op(ctx);
1941 status = dplane_ctx_get_status(ctx);
1942
1943 id = dplane_ctx_get_nhe_id(ctx);
1944
1945 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1946 zlog_debug(
1947 "Nexthop dplane ctx %p, op %s, nexthop ID (%u), result %s",
1948 ctx, dplane_op2str(op), id, dplane_res2str(status));
1949
1950 switch (op) {
1951 case DPLANE_OP_NH_DELETE:
1952 if (status != ZEBRA_DPLANE_REQUEST_SUCCESS)
1953 flog_err(
1954 EC_ZEBRA_DP_DELETE_FAIL,
1955 "Failed to uninstall Nexthop ID (%u) from the kernel",
1956 id);
1957 /* We already free'd the data, nothing to do */
1958 break;
1959 case DPLANE_OP_NH_INSTALL:
1960 case DPLANE_OP_NH_UPDATE:
1961 nhe = zebra_nhg_lookup_id(id);
1962
1963 if (!nhe) {
1964 flog_err(
1965 EC_ZEBRA_NHG_SYNC,
1966 "%s operation preformed on Nexthop ID (%u) in the kernel, that we no longer have in our table",
1967 dplane_op2str(op), id);
1968 break;
1969 }
1970
1971 UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED);
1972 if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
1973 SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID);
1974 SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
1975 zebra_nhg_handle_install(nhe);
1976 } else
1977 flog_err(
1978 EC_ZEBRA_DP_INSTALL_FAIL,
1979 "Failed to install Nexthop ID (%u) into the kernel",
1980 nhe->id);
1981 break;
1982 case DPLANE_OP_ROUTE_INSTALL:
1983 case DPLANE_OP_ROUTE_UPDATE:
1984 case DPLANE_OP_ROUTE_DELETE:
1985 case DPLANE_OP_ROUTE_NOTIFY:
1986 case DPLANE_OP_LSP_INSTALL:
1987 case DPLANE_OP_LSP_UPDATE:
1988 case DPLANE_OP_LSP_DELETE:
1989 case DPLANE_OP_LSP_NOTIFY:
1990 case DPLANE_OP_PW_INSTALL:
1991 case DPLANE_OP_PW_UNINSTALL:
1992 case DPLANE_OP_SYS_ROUTE_ADD:
1993 case DPLANE_OP_SYS_ROUTE_DELETE:
1994 case DPLANE_OP_ADDR_INSTALL:
1995 case DPLANE_OP_ADDR_UNINSTALL:
1996 case DPLANE_OP_MAC_INSTALL:
1997 case DPLANE_OP_MAC_DELETE:
1998 case DPLANE_OP_NEIGH_INSTALL:
1999 case DPLANE_OP_NEIGH_UPDATE:
2000 case DPLANE_OP_NEIGH_DELETE:
2001 case DPLANE_OP_VTEP_ADD:
2002 case DPLANE_OP_VTEP_DELETE:
2003 case DPLANE_OP_NONE:
2004 break;
2005 }
2006
2007 dplane_ctx_fini(&ctx);
2008 }
2009
2010 static void zebra_nhg_sweep_entry(struct hash_bucket *bucket, void *arg)
2011 {
2012 struct nhg_hash_entry *nhe = NULL;
2013
2014 nhe = (struct nhg_hash_entry *)bucket->data;
2015
2016 /* If its being ref'd, just let it be uninstalled via a route removal */
2017 if (ZEBRA_NHG_CREATED(nhe) && nhe->refcnt <= 0)
2018 zebra_nhg_uninstall_kernel(nhe);
2019 }
2020
2021 void zebra_nhg_sweep_table(struct hash *hash)
2022 {
2023 hash_iterate(hash, zebra_nhg_sweep_entry, NULL);
2024 }