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