]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_nhg.c
*: don't null after XFREE; XFREE does this itself
[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->nhg->nexthop->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,
328 nhe->nhg->nexthop->vrf_id, 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 lookup.vrf_id = vrf_id;
539 if (lookup.nhg->nexthop->next) {
540 /* Groups can have all vrfs and AF's in them */
541 lookup.afi = AFI_UNSPEC;
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
567 if (id)
568 (*nhe) = zebra_nhg_lookup_id(id);
569 else
570 (*nhe) = hash_lookup(zrouter.nhgs, &lookup);
571
572 /* If it found an nhe in our tables, this new ID is unused */
573 if (*nhe)
574 id_counter = old_id_counter;
575
576 if (!(*nhe)) {
577 /* Only hash/lookup the depends if the first lookup
578 * fails to find something. This should hopefully save a
579 * lot of cycles for larger ecmp sizes.
580 */
581 if (nhg_depends)
582 /* If you don't want to hash on each nexthop in the
583 * nexthop group struct you can pass the depends
584 * directly. Kernel-side we do this since it just looks
585 * them up via IDs.
586 */
587 lookup.nhg_depends = *nhg_depends;
588 else {
589 if (nhg->nexthop->next) {
590 zebra_nhg_depends_init(&lookup);
591
592 /* If its a group, create a dependency tree */
593 struct nexthop *nh = NULL;
594
595 for (nh = nhg->nexthop; nh; nh = nh->next)
596 depends_find_add(&lookup.nhg_depends,
597 nh, afi);
598 } else if (CHECK_FLAG(nhg->nexthop->flags,
599 NEXTHOP_FLAG_RECURSIVE)) {
600 zebra_nhg_depends_init(&lookup);
601 handle_recursive_depend(&lookup.nhg_depends,
602 nhg->nexthop->resolved,
603 afi);
604 recursive = true;
605 }
606 }
607
608 (*nhe) = hash_get(zrouter.nhgs, &lookup, zebra_nhg_hash_alloc);
609 created = true;
610
611 if (recursive)
612 SET_FLAG((*nhe)->flags, NEXTHOP_GROUP_RECURSIVE);
613 }
614 return created;
615 }
616
617 /* Find/create a single nexthop */
618 static struct nhg_hash_entry *
619 zebra_nhg_find_nexthop(uint32_t id, struct nexthop *nh, afi_t afi, int type)
620 {
621 struct nhg_hash_entry *nhe = NULL;
622 struct nexthop_group nhg = {};
623 vrf_id_t vrf_id = !vrf_is_backend_netns() ? VRF_DEFAULT : nh->vrf_id;
624
625 nexthop_group_add_sorted(&nhg, nh);
626
627 zebra_nhg_find(&nhe, id, &nhg, NULL, vrf_id, afi, type);
628
629 return nhe;
630 }
631
632 static uint32_t nhg_ctx_get_id(const struct nhg_ctx *ctx)
633 {
634 return ctx->id;
635 }
636
637 static void nhg_ctx_set_status(struct nhg_ctx *ctx, enum nhg_ctx_status status)
638 {
639 ctx->status = status;
640 }
641
642 static enum nhg_ctx_status nhg_ctx_get_status(const struct nhg_ctx *ctx)
643 {
644 return ctx->status;
645 }
646
647 static void nhg_ctx_set_op(struct nhg_ctx *ctx, enum nhg_ctx_op_e op)
648 {
649 ctx->op = op;
650 }
651
652 static enum nhg_ctx_op_e nhg_ctx_get_op(const struct nhg_ctx *ctx)
653 {
654 return ctx->op;
655 }
656
657 static vrf_id_t nhg_ctx_get_vrf_id(const struct nhg_ctx *ctx)
658 {
659 return ctx->vrf_id;
660 }
661
662 static int nhg_ctx_get_type(const struct nhg_ctx *ctx)
663 {
664 return ctx->type;
665 }
666
667 static int nhg_ctx_get_afi(const struct nhg_ctx *ctx)
668 {
669 return ctx->afi;
670 }
671
672 static struct nexthop *nhg_ctx_get_nh(struct nhg_ctx *ctx)
673 {
674 return &ctx->u.nh;
675 }
676
677 static uint8_t nhg_ctx_get_count(const struct nhg_ctx *ctx)
678 {
679 return ctx->count;
680 }
681
682 static struct nh_grp *nhg_ctx_get_grp(struct nhg_ctx *ctx)
683 {
684 return ctx->u.grp;
685 }
686
687 static struct nhg_ctx *nhg_ctx_new()
688 {
689 struct nhg_ctx *new = NULL;
690
691 new = XCALLOC(MTYPE_NHG_CTX, sizeof(struct nhg_ctx));
692
693 return new;
694 }
695
696 static void nhg_ctx_free(struct nhg_ctx **ctx)
697 {
698 struct nexthop *nh;
699
700 if (ctx == NULL)
701 return;
702
703 assert((*ctx) != NULL);
704
705 if (nhg_ctx_get_count(*ctx))
706 goto done;
707
708 nh = nhg_ctx_get_nh(*ctx);
709
710 nexthop_del_labels(nh);
711
712 done:
713 XFREE(MTYPE_NHG_CTX, *ctx);
714 }
715
716 static struct nhg_ctx *nhg_ctx_init(uint32_t id, struct nexthop *nh,
717 struct nh_grp *grp, vrf_id_t vrf_id,
718 afi_t afi, int type, uint8_t count)
719 {
720 struct nhg_ctx *ctx = NULL;
721
722 ctx = nhg_ctx_new();
723
724 ctx->id = id;
725 ctx->vrf_id = vrf_id;
726 ctx->afi = afi;
727 ctx->type = type;
728 ctx->count = count;
729
730 if (count)
731 /* Copy over the array */
732 memcpy(&ctx->u.grp, grp, count * sizeof(struct nh_grp));
733 else if (nh)
734 ctx->u.nh = *nh;
735
736 return ctx;
737 }
738
739 static bool zebra_nhg_contains_unhashable(struct nhg_hash_entry *nhe)
740 {
741 struct nhg_connected *rb_node_dep = NULL;
742
743 frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) {
744 if (CHECK_FLAG(rb_node_dep->nhe->flags,
745 NEXTHOP_GROUP_UNHASHABLE))
746 return true;
747 }
748
749 return false;
750 }
751
752 static void zebra_nhg_set_unhashable(struct nhg_hash_entry *nhe)
753 {
754 SET_FLAG(nhe->flags, NEXTHOP_GROUP_UNHASHABLE);
755 SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
756
757 flog_warn(
758 EC_ZEBRA_DUPLICATE_NHG_MESSAGE,
759 "Nexthop Group with ID (%d) is a duplicate, therefore unhashable, ignoring",
760 nhe->id);
761 }
762
763 static void zebra_nhg_set_valid(struct nhg_hash_entry *nhe)
764 {
765 struct nhg_connected *rb_node_dep;
766
767 SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID);
768
769 frr_each(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep)
770 zebra_nhg_set_valid(rb_node_dep->nhe);
771 }
772
773 static void zebra_nhg_set_invalid(struct nhg_hash_entry *nhe)
774 {
775 struct nhg_connected *rb_node_dep;
776
777 UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID);
778
779 /* Update validity of nexthops depending on it */
780 frr_each(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep)
781 zebra_nhg_check_valid(rb_node_dep->nhe);
782 }
783
784 void zebra_nhg_check_valid(struct nhg_hash_entry *nhe)
785 {
786 struct nhg_connected *rb_node_dep = NULL;
787 bool valid = false;
788
789 /* If anthing else in the group is valid, the group is valid */
790 frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) {
791 if (CHECK_FLAG(rb_node_dep->nhe->flags, NEXTHOP_GROUP_VALID)) {
792 valid = true;
793 goto done;
794 }
795 }
796
797 done:
798 if (valid)
799 zebra_nhg_set_valid(nhe);
800 else
801 zebra_nhg_set_invalid(nhe);
802 }
803
804
805 static void zebra_nhg_release(struct nhg_hash_entry *nhe)
806 {
807 /* Remove it from any lists it may be on */
808 zebra_nhg_depends_release(nhe);
809 zebra_nhg_dependents_release(nhe);
810 if (nhe->ifp)
811 if_nhg_dependents_del(nhe->ifp, nhe);
812
813 /*
814 * If its unhashable, we didn't store it here and have to be
815 * sure we don't clear one thats actually being used.
816 */
817 if (!CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_UNHASHABLE))
818 hash_release(zrouter.nhgs, nhe);
819
820 hash_release(zrouter.nhgs_id, nhe);
821 }
822
823 static void zebra_nhg_handle_uninstall(struct nhg_hash_entry *nhe)
824 {
825 zebra_nhg_release(nhe);
826 zebra_nhg_free(nhe);
827 }
828
829 static void zebra_nhg_handle_install(struct nhg_hash_entry *nhe)
830 {
831 /* Update validity of groups depending on it */
832 struct nhg_connected *rb_node_dep;
833
834 frr_each_safe(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep)
835 zebra_nhg_set_valid(rb_node_dep->nhe);
836 }
837
838 /*
839 * The kernel/other program has changed the state of a nexthop object we are
840 * using.
841 */
842 static void zebra_nhg_handle_kernel_state_change(struct nhg_hash_entry *nhe,
843 bool is_delete)
844 {
845 if (nhe->refcnt) {
846 flog_err(
847 EC_ZEBRA_NHG_SYNC,
848 "Kernel %s a nexthop group with ID (%u) that we are still using for a route, sending it back down",
849 (is_delete ? "deleted" : "updated"), nhe->id);
850
851 UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
852 zebra_nhg_install_kernel(nhe);
853 } else
854 zebra_nhg_handle_uninstall(nhe);
855 }
856
857 static int nhg_ctx_process_new(struct nhg_ctx *ctx)
858 {
859 struct nexthop_group *nhg = NULL;
860 struct nhg_connected_tree_head nhg_depends = {};
861 struct nhg_hash_entry *lookup = NULL;
862 struct nhg_hash_entry *nhe = NULL;
863
864 uint32_t id = nhg_ctx_get_id(ctx);
865 uint8_t count = nhg_ctx_get_count(ctx);
866 vrf_id_t vrf_id = nhg_ctx_get_vrf_id(ctx);
867 int type = nhg_ctx_get_type(ctx);
868 afi_t afi = nhg_ctx_get_afi(ctx);
869
870 lookup = zebra_nhg_lookup_id(id);
871
872 if (lookup) {
873 /* This is already present in our table, hence an update
874 * that we did not initate.
875 */
876 zebra_nhg_handle_kernel_state_change(lookup, false);
877 return 0;
878 }
879
880 if (nhg_ctx_get_count(ctx)) {
881 nhg = nexthop_group_new();
882 if (zebra_nhg_process_grp(nhg, &nhg_depends,
883 nhg_ctx_get_grp(ctx), count)) {
884 depends_decrement_free(&nhg_depends);
885 nexthop_group_delete(&nhg);
886 return -ENOENT;
887 }
888
889 if (!zebra_nhg_find(&nhe, id, nhg, &nhg_depends, vrf_id, type,
890 afi))
891 depends_decrement_free(&nhg_depends);
892
893 /* These got copied over in zebra_nhg_alloc() */
894 nexthop_group_delete(&nhg);
895 } else
896 nhe = zebra_nhg_find_nexthop(id, nhg_ctx_get_nh(ctx), afi,
897 type);
898
899 if (nhe) {
900 if (id != nhe->id) {
901 struct nhg_hash_entry *kernel_nhe = NULL;
902
903 /* Duplicate but with different ID from
904 * the kernel
905 */
906
907 /* The kernel allows duplicate nexthops
908 * as long as they have different IDs.
909 * We are ignoring those to prevent
910 * syncing problems with the kernel
911 * changes.
912 *
913 * We maintain them *ONLY* in the ID hash table to
914 * track them and set the flag to indicated
915 * their attributes are unhashable.
916 */
917
918 kernel_nhe = zebra_nhg_copy(nhe, id);
919 zebra_nhg_insert_id(kernel_nhe);
920 zebra_nhg_set_unhashable(kernel_nhe);
921 } else if (zebra_nhg_contains_unhashable(nhe)) {
922 /* The group we got contains an unhashable/duplicated
923 * depend, so lets mark this group as unhashable as well
924 * and release it from the non-ID hash.
925 */
926 hash_release(zrouter.nhgs, nhe);
927 zebra_nhg_set_unhashable(nhe);
928 } else {
929 /* It actually created a new nhe */
930 SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID);
931 SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
932 }
933 } else {
934 flog_err(
935 EC_ZEBRA_TABLE_LOOKUP_FAILED,
936 "Zebra failed to find or create a nexthop hash entry for ID (%u)",
937 id);
938 return -1;
939 }
940
941 return 0;
942 }
943
944 static int nhg_ctx_process_del(struct nhg_ctx *ctx)
945 {
946 struct nhg_hash_entry *nhe = NULL;
947 uint32_t id = nhg_ctx_get_id(ctx);
948
949 nhe = zebra_nhg_lookup_id(id);
950
951 if (!nhe) {
952 flog_warn(
953 EC_ZEBRA_BAD_NHG_MESSAGE,
954 "Kernel delete message received for nexthop group ID (%u) that we do not have in our ID table",
955 id);
956 return -1;
957 }
958
959 zebra_nhg_handle_kernel_state_change(nhe, true);
960
961 return 0;
962 }
963
964 static void nhg_ctx_fini(struct nhg_ctx **ctx)
965 {
966 /*
967 * Just freeing for now, maybe do something more in the future
968 * based on flag.
969 */
970
971 nhg_ctx_free(ctx);
972 }
973
974 static int queue_add(struct nhg_ctx *ctx)
975 {
976 /* If its queued or already processed do nothing */
977 if (nhg_ctx_get_status(ctx) == NHG_CTX_QUEUED)
978 return 0;
979
980 if (rib_queue_nhg_add(ctx)) {
981 nhg_ctx_set_status(ctx, NHG_CTX_FAILURE);
982 return -1;
983 }
984
985 nhg_ctx_set_status(ctx, NHG_CTX_QUEUED);
986
987 return 0;
988 }
989
990 int nhg_ctx_process(struct nhg_ctx *ctx)
991 {
992 int ret = 0;
993
994 switch (nhg_ctx_get_op(ctx)) {
995 case NHG_CTX_OP_NEW:
996 ret = nhg_ctx_process_new(ctx);
997 if (nhg_ctx_get_count(ctx) && ret == -ENOENT
998 && nhg_ctx_get_status(ctx) != NHG_CTX_REQUEUED) {
999 /**
1000 * We have entered a situation where we are
1001 * processing a group from the kernel
1002 * that has a contained nexthop which
1003 * we have not yet processed.
1004 *
1005 * Re-enqueue this ctx to be handled exactly one
1006 * more time (indicated by the flag).
1007 *
1008 * By the time we get back to it, we
1009 * should have processed its depends.
1010 */
1011 nhg_ctx_set_status(ctx, NHG_CTX_NONE);
1012 if (queue_add(ctx) == 0) {
1013 nhg_ctx_set_status(ctx, NHG_CTX_REQUEUED);
1014 return 0;
1015 }
1016 }
1017 break;
1018 case NHG_CTX_OP_DEL:
1019 ret = nhg_ctx_process_del(ctx);
1020 case NHG_CTX_OP_NONE:
1021 break;
1022 }
1023
1024 nhg_ctx_set_status(ctx, (ret ? NHG_CTX_FAILURE : NHG_CTX_SUCCESS));
1025
1026 nhg_ctx_fini(&ctx);
1027
1028 return ret;
1029 }
1030
1031 /* Kernel-side, you either get a single new nexthop or a array of ID's */
1032 int zebra_nhg_kernel_find(uint32_t id, struct nexthop *nh, struct nh_grp *grp,
1033 uint8_t count, vrf_id_t vrf_id, afi_t afi, int type,
1034 int startup)
1035 {
1036 struct nhg_ctx *ctx = NULL;
1037
1038 if (id > id_counter)
1039 /* Increase our counter so we don't try to create
1040 * an ID that already exists
1041 */
1042 id_counter = id;
1043
1044 ctx = nhg_ctx_init(id, nh, grp, vrf_id, afi, type, count);
1045 nhg_ctx_set_op(ctx, NHG_CTX_OP_NEW);
1046
1047 /* Under statup conditions, we need to handle them immediately
1048 * like we do for routes. Otherwise, we are going to get a route
1049 * with a nhe_id that we have not handled.
1050 */
1051 if (startup)
1052 return nhg_ctx_process(ctx);
1053
1054 if (queue_add(ctx)) {
1055 nhg_ctx_fini(&ctx);
1056 return -1;
1057 }
1058
1059 return 0;
1060 }
1061
1062 /* Kernel-side, received delete message */
1063 int zebra_nhg_kernel_del(uint32_t id, vrf_id_t vrf_id)
1064 {
1065 struct nhg_ctx *ctx = NULL;
1066
1067 ctx = nhg_ctx_init(id, NULL, NULL, vrf_id, 0, 0, 0);
1068
1069 nhg_ctx_set_op(ctx, NHG_CTX_OP_DEL);
1070
1071 if (queue_add(ctx)) {
1072 nhg_ctx_fini(&ctx);
1073 return -1;
1074 }
1075
1076 return 0;
1077 }
1078
1079 /* Some dependency helper functions */
1080 static struct nhg_hash_entry *depends_find_recursive(const struct nexthop *nh,
1081 afi_t afi)
1082 {
1083 struct nhg_hash_entry *nhe;
1084 struct nexthop *lookup = NULL;
1085
1086 lookup = nexthop_dup(nh, NULL);
1087
1088 nhe = zebra_nhg_find_nexthop(0, lookup, afi, 0);
1089
1090 nexthops_free(lookup);
1091
1092 return nhe;
1093 }
1094
1095 static struct nhg_hash_entry *depends_find_singleton(const struct nexthop *nh,
1096 afi_t afi)
1097 {
1098 struct nhg_hash_entry *nhe;
1099 struct nexthop lookup = {};
1100
1101 /* Capture a snapshot of this single nh; it might be part of a list,
1102 * so we need to make a standalone copy.
1103 */
1104 nexthop_copy_no_recurse(&lookup, nh, NULL);
1105
1106 nhe = zebra_nhg_find_nexthop(0, &lookup, afi, 0);
1107
1108 /* The copy may have allocated labels; free them if necessary. */
1109 nexthop_del_labels(&lookup);
1110
1111 return nhe;
1112 }
1113
1114 static struct nhg_hash_entry *depends_find(const struct nexthop *nh, afi_t afi)
1115 {
1116 struct nhg_hash_entry *nhe = NULL;
1117
1118 if (!nh)
1119 goto done;
1120
1121 /* We are separating these functions out to increase handling speed
1122 * in the non-recursive case (by not alloc/freeing)
1123 */
1124 if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE))
1125 nhe = depends_find_recursive(nh, afi);
1126 else
1127 nhe = depends_find_singleton(nh, afi);
1128
1129 done:
1130 return nhe;
1131 }
1132
1133 static void depends_add(struct nhg_connected_tree_head *head,
1134 struct nhg_hash_entry *depend)
1135 {
1136 /* If NULL is returned, it was successfully added and
1137 * needs to have its refcnt incremented.
1138 *
1139 * Else the NHE is already present in the tree and doesn't
1140 * need to increment the refcnt.
1141 */
1142 if (nhg_connected_tree_add_nhe(head, depend) == NULL)
1143 zebra_nhg_increment_ref(depend);
1144 }
1145
1146 static struct nhg_hash_entry *
1147 depends_find_add(struct nhg_connected_tree_head *head, struct nexthop *nh,
1148 afi_t afi)
1149 {
1150 struct nhg_hash_entry *depend = NULL;
1151
1152 depend = depends_find(nh, afi);
1153
1154 if (depend)
1155 depends_add(head, depend);
1156
1157 return depend;
1158 }
1159
1160 static struct nhg_hash_entry *
1161 depends_find_id_add(struct nhg_connected_tree_head *head, uint32_t id)
1162 {
1163 struct nhg_hash_entry *depend = NULL;
1164
1165 depend = zebra_nhg_lookup_id(id);
1166
1167 if (depend)
1168 depends_add(head, depend);
1169
1170 return depend;
1171 }
1172
1173 static void depends_decrement_free(struct nhg_connected_tree_head *head)
1174 {
1175 nhg_connected_tree_decrement_ref(head);
1176 nhg_connected_tree_free(head);
1177 }
1178
1179 /* Rib-side, you get a nexthop group struct */
1180 struct nhg_hash_entry *
1181 zebra_nhg_rib_find(uint32_t id, struct nexthop_group *nhg, afi_t rt_afi)
1182 {
1183 struct nhg_hash_entry *nhe = NULL;
1184 vrf_id_t vrf_id;
1185
1186 /*
1187 * CLANG SA is complaining that nexthop may be NULL
1188 * Make it happy but this is ridonc
1189 */
1190 assert(nhg->nexthop);
1191 vrf_id = !vrf_is_backend_netns() ? VRF_DEFAULT : nhg->nexthop->vrf_id;
1192
1193 if (!(nhg && nhg->nexthop)) {
1194 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED,
1195 "No nexthop passed to %s", __func__);
1196 return NULL;
1197 }
1198
1199 zebra_nhg_find(&nhe, id, nhg, NULL, vrf_id, rt_afi, 0);
1200
1201 return nhe;
1202 }
1203
1204 static void zebra_nhg_free_members(struct nhg_hash_entry *nhe)
1205 {
1206 nexthop_group_delete(&nhe->nhg);
1207 /* Decrement to remove connection ref */
1208 nhg_connected_tree_decrement_ref(&nhe->nhg_depends);
1209 nhg_connected_tree_free(&nhe->nhg_depends);
1210 nhg_connected_tree_free(&nhe->nhg_dependents);
1211 }
1212
1213 void zebra_nhg_free(struct nhg_hash_entry *nhe)
1214 {
1215 if (nhe->refcnt)
1216 zlog_debug("nhe_id=%u hash refcnt=%d", nhe->id, nhe->refcnt);
1217
1218 zebra_nhg_free_members(nhe);
1219
1220 XFREE(MTYPE_NHG, nhe);
1221 }
1222
1223 void zebra_nhg_hash_free(void *p)
1224 {
1225 zebra_nhg_free((struct nhg_hash_entry *)p);
1226 }
1227
1228 void zebra_nhg_decrement_ref(struct nhg_hash_entry *nhe)
1229 {
1230 nhe->refcnt--;
1231
1232 if (!zebra_nhg_depends_is_empty(nhe))
1233 nhg_connected_tree_decrement_ref(&nhe->nhg_depends);
1234
1235 if (ZEBRA_NHG_CREATED(nhe) && nhe->refcnt <= 0)
1236 zebra_nhg_uninstall_kernel(nhe);
1237 }
1238
1239 void zebra_nhg_increment_ref(struct nhg_hash_entry *nhe)
1240 {
1241 nhe->refcnt++;
1242
1243 if (!zebra_nhg_depends_is_empty(nhe))
1244 nhg_connected_tree_increment_ref(&nhe->nhg_depends);
1245 }
1246
1247 static void nexthop_set_resolved(afi_t afi, const struct nexthop *newhop,
1248 struct nexthop *nexthop)
1249 {
1250 struct nexthop *resolved_hop;
1251 uint8_t num_labels = 0;
1252 mpls_label_t labels[MPLS_MAX_LABELS];
1253 enum lsp_types_t label_type = ZEBRA_LSP_NONE;
1254 int i = 0;
1255
1256 resolved_hop = nexthop_new();
1257 SET_FLAG(resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
1258
1259 resolved_hop->vrf_id = nexthop->vrf_id;
1260 switch (newhop->type) {
1261 case NEXTHOP_TYPE_IPV4:
1262 case NEXTHOP_TYPE_IPV4_IFINDEX:
1263 /* If the resolving route specifies a gateway, use it */
1264 resolved_hop->type = newhop->type;
1265 resolved_hop->gate.ipv4 = newhop->gate.ipv4;
1266
1267 if (newhop->ifindex) {
1268 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
1269 resolved_hop->ifindex = newhop->ifindex;
1270 }
1271 break;
1272 case NEXTHOP_TYPE_IPV6:
1273 case NEXTHOP_TYPE_IPV6_IFINDEX:
1274 resolved_hop->type = newhop->type;
1275 resolved_hop->gate.ipv6 = newhop->gate.ipv6;
1276
1277 if (newhop->ifindex) {
1278 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1279 resolved_hop->ifindex = newhop->ifindex;
1280 }
1281 break;
1282 case NEXTHOP_TYPE_IFINDEX:
1283 /* If the resolving route is an interface route,
1284 * it means the gateway we are looking up is connected
1285 * to that interface. (The actual network is _not_ onlink).
1286 * Therefore, the resolved route should have the original
1287 * gateway as nexthop as it is directly connected.
1288 *
1289 * On Linux, we have to set the onlink netlink flag because
1290 * otherwise, the kernel won't accept the route.
1291 */
1292 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
1293 if (afi == AFI_IP) {
1294 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
1295 resolved_hop->gate.ipv4 = nexthop->gate.ipv4;
1296 } else if (afi == AFI_IP6) {
1297 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1298 resolved_hop->gate.ipv6 = nexthop->gate.ipv6;
1299 }
1300 resolved_hop->ifindex = newhop->ifindex;
1301 break;
1302 case NEXTHOP_TYPE_BLACKHOLE:
1303 resolved_hop->type = NEXTHOP_TYPE_BLACKHOLE;
1304 resolved_hop->bh_type = newhop->bh_type;
1305 break;
1306 }
1307
1308 if (newhop->flags & NEXTHOP_FLAG_ONLINK)
1309 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
1310
1311 /* Copy labels of the resolved route and the parent resolving to it */
1312 if (newhop->nh_label) {
1313 for (i = 0; i < newhop->nh_label->num_labels; i++)
1314 labels[num_labels++] = newhop->nh_label->label[i];
1315 label_type = newhop->nh_label_type;
1316 }
1317
1318 if (nexthop->nh_label) {
1319 for (i = 0; i < nexthop->nh_label->num_labels; i++)
1320 labels[num_labels++] = nexthop->nh_label->label[i];
1321
1322 /* If the parent has labels, use its type */
1323 label_type = nexthop->nh_label_type;
1324 }
1325
1326 if (num_labels)
1327 nexthop_add_labels(resolved_hop, label_type, num_labels,
1328 labels);
1329
1330 resolved_hop->rparent = nexthop;
1331 _nexthop_add(&nexthop->resolved, resolved_hop);
1332 }
1333
1334 /* Checks if nexthop we are trying to resolve to is valid */
1335 static bool nexthop_valid_resolve(const struct nexthop *nexthop,
1336 const struct nexthop *resolved)
1337 {
1338 /* Can't resolve to a recursive nexthop */
1339 if (CHECK_FLAG(resolved->flags, NEXTHOP_FLAG_RECURSIVE))
1340 return false;
1341
1342 switch (nexthop->type) {
1343 case NEXTHOP_TYPE_IPV4_IFINDEX:
1344 case NEXTHOP_TYPE_IPV6_IFINDEX:
1345 /* If the nexthop we are resolving to does not match the
1346 * ifindex for the nexthop the route wanted, its not valid.
1347 */
1348 if (nexthop->ifindex != resolved->ifindex)
1349 return false;
1350 break;
1351 case NEXTHOP_TYPE_IPV4:
1352 case NEXTHOP_TYPE_IPV6:
1353 case NEXTHOP_TYPE_IFINDEX:
1354 case NEXTHOP_TYPE_BLACKHOLE:
1355 break;
1356 }
1357
1358 return true;
1359 }
1360
1361 /*
1362 * Given a nexthop we need to properly recursively resolve
1363 * the route. As such, do a table lookup to find and match
1364 * if at all possible. Set the nexthop->ifindex and resolved_id
1365 * as appropriate
1366 */
1367 static int nexthop_active(afi_t afi, struct route_entry *re,
1368 struct nexthop *nexthop, struct route_node *top)
1369 {
1370 struct prefix p;
1371 struct route_table *table;
1372 struct route_node *rn;
1373 struct route_entry *match = NULL;
1374 int resolved;
1375 struct nexthop *newhop;
1376 struct interface *ifp;
1377 rib_dest_t *dest;
1378 struct zebra_vrf *zvrf;
1379
1380 if ((nexthop->type == NEXTHOP_TYPE_IPV4)
1381 || nexthop->type == NEXTHOP_TYPE_IPV6)
1382 nexthop->ifindex = 0;
1383
1384
1385 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
1386 nexthops_free(nexthop->resolved);
1387 nexthop->resolved = NULL;
1388 re->nexthop_mtu = 0;
1389
1390 /*
1391 * If the kernel has sent us a NEW route, then
1392 * by golly gee whiz it's a good route.
1393 *
1394 * If its an already INSTALLED route we have already handled, then the
1395 * kernel route's nexthop might have became unreachable
1396 * and we have to handle that.
1397 */
1398 if (!CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED)
1399 && (re->type == ZEBRA_ROUTE_KERNEL
1400 || re->type == ZEBRA_ROUTE_SYSTEM))
1401 return 1;
1402
1403 /*
1404 * Check to see if we should trust the passed in information
1405 * for UNNUMBERED interfaces as that we won't find the GW
1406 * address in the routing table.
1407 * This check should suffice to handle IPv4 or IPv6 routes
1408 * sourced from EVPN routes which are installed with the
1409 * next hop as the remote VTEP IP.
1410 */
1411 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)) {
1412 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
1413 if (!ifp) {
1414 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1415 zlog_debug(
1416 "\t%s: Onlink and interface: %u[%u] does not exist",
1417 __PRETTY_FUNCTION__, nexthop->ifindex,
1418 nexthop->vrf_id);
1419 return 0;
1420 }
1421 if (connected_is_unnumbered(ifp)) {
1422 if (if_is_operative(ifp))
1423 return 1;
1424
1425 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1426 zlog_debug(
1427 "\t%s: Onlink and interface %s is not operative",
1428 __PRETTY_FUNCTION__, ifp->name);
1429 return 0;
1430 }
1431 if (!if_is_operative(ifp)) {
1432 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1433 zlog_debug(
1434 "\t%s: Interface %s is not unnumbered",
1435 __PRETTY_FUNCTION__, ifp->name);
1436 return 0;
1437 }
1438 }
1439
1440 if ((top->p.family == AF_INET && top->p.prefixlen == 32
1441 && nexthop->gate.ipv4.s_addr == top->p.u.prefix4.s_addr)
1442 || (top->p.family == AF_INET6 && top->p.prefixlen == 128
1443 && memcmp(&nexthop->gate.ipv6, &top->p.u.prefix6, 16) == 0)) {
1444 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1445 zlog_debug(
1446 "\t:%s: Attempting to install a max prefixlength route through itself",
1447 __PRETTY_FUNCTION__);
1448 return 0;
1449 }
1450
1451 /* Make lookup prefix. */
1452 memset(&p, 0, sizeof(struct prefix));
1453 switch (afi) {
1454 case AFI_IP:
1455 p.family = AF_INET;
1456 p.prefixlen = IPV4_MAX_PREFIXLEN;
1457 p.u.prefix4 = nexthop->gate.ipv4;
1458 break;
1459 case AFI_IP6:
1460 p.family = AF_INET6;
1461 p.prefixlen = IPV6_MAX_PREFIXLEN;
1462 p.u.prefix6 = nexthop->gate.ipv6;
1463 break;
1464 default:
1465 assert(afi != AFI_IP && afi != AFI_IP6);
1466 break;
1467 }
1468 /* Lookup table. */
1469 table = zebra_vrf_table(afi, SAFI_UNICAST, nexthop->vrf_id);
1470 /* get zvrf */
1471 zvrf = zebra_vrf_lookup_by_id(nexthop->vrf_id);
1472 if (!table || !zvrf) {
1473 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1474 zlog_debug("\t%s: Table not found",
1475 __PRETTY_FUNCTION__);
1476 return 0;
1477 }
1478
1479 rn = route_node_match(table, (struct prefix *)&p);
1480 while (rn) {
1481 route_unlock_node(rn);
1482
1483 /* Lookup should halt if we've matched against ourselves ('top',
1484 * if specified) - i.e., we cannot have a nexthop NH1 is
1485 * resolved by a route NH1. The exception is if the route is a
1486 * host route.
1487 */
1488 if (top && rn == top)
1489 if (((afi == AFI_IP) && (rn->p.prefixlen != 32))
1490 || ((afi == AFI_IP6) && (rn->p.prefixlen != 128))) {
1491 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1492 zlog_debug(
1493 "\t%s: Matched against ourself and prefix length is not max bit length",
1494 __PRETTY_FUNCTION__);
1495 return 0;
1496 }
1497
1498 /* Pick up selected route. */
1499 /* However, do not resolve over default route unless explicitly
1500 * allowed.
1501 */
1502 if (is_default_prefix(&rn->p)
1503 && !rnh_resolve_via_default(zvrf, p.family)) {
1504 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1505 zlog_debug(
1506 "\t:%s: Resolved against default route",
1507 __PRETTY_FUNCTION__);
1508 return 0;
1509 }
1510
1511 dest = rib_dest_from_rnode(rn);
1512 if (dest && dest->selected_fib
1513 && !CHECK_FLAG(dest->selected_fib->status,
1514 ROUTE_ENTRY_REMOVED)
1515 && dest->selected_fib->type != ZEBRA_ROUTE_TABLE)
1516 match = dest->selected_fib;
1517
1518 /* If there is no selected route or matched route is EGP, go up
1519 * tree.
1520 */
1521 if (!match) {
1522 do {
1523 rn = rn->parent;
1524 } while (rn && rn->info == NULL);
1525 if (rn)
1526 route_lock_node(rn);
1527
1528 continue;
1529 }
1530
1531 if (match->type == ZEBRA_ROUTE_CONNECT) {
1532 /* Directly point connected route. */
1533 newhop = match->nhe->nhg->nexthop;
1534 if (newhop) {
1535 if (nexthop->type == NEXTHOP_TYPE_IPV4
1536 || nexthop->type == NEXTHOP_TYPE_IPV6)
1537 nexthop->ifindex = newhop->ifindex;
1538 }
1539 return 1;
1540 } else if (CHECK_FLAG(re->flags, ZEBRA_FLAG_ALLOW_RECURSION)) {
1541 resolved = 0;
1542 for (ALL_NEXTHOPS_PTR(match->nhe->nhg, newhop)) {
1543 if (!CHECK_FLAG(match->status,
1544 ROUTE_ENTRY_INSTALLED))
1545 continue;
1546 if (!nexthop_valid_resolve(nexthop, newhop))
1547 continue;
1548
1549 SET_FLAG(nexthop->flags,
1550 NEXTHOP_FLAG_RECURSIVE);
1551 nexthop_set_resolved(afi, newhop, nexthop);
1552 resolved = 1;
1553 }
1554 if (resolved)
1555 re->nexthop_mtu = match->mtu;
1556
1557 if (!resolved && IS_ZEBRA_DEBUG_RIB_DETAILED)
1558 zlog_debug("\t%s: Recursion failed to find",
1559 __PRETTY_FUNCTION__);
1560 return resolved;
1561 } else if (re->type == ZEBRA_ROUTE_STATIC) {
1562 resolved = 0;
1563 for (ALL_NEXTHOPS_PTR(match->nhe->nhg, newhop)) {
1564 if (!CHECK_FLAG(match->status,
1565 ROUTE_ENTRY_INSTALLED))
1566 continue;
1567 if (!nexthop_valid_resolve(nexthop, newhop))
1568 continue;
1569
1570 SET_FLAG(nexthop->flags,
1571 NEXTHOP_FLAG_RECURSIVE);
1572 nexthop_set_resolved(afi, newhop, nexthop);
1573 resolved = 1;
1574 }
1575 if (resolved)
1576 re->nexthop_mtu = match->mtu;
1577
1578 if (!resolved && IS_ZEBRA_DEBUG_RIB_DETAILED)
1579 zlog_debug(
1580 "\t%s: Static route unable to resolve",
1581 __PRETTY_FUNCTION__);
1582 return resolved;
1583 } else {
1584 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1585 zlog_debug(
1586 "\t%s: Route Type %s has not turned on recursion",
1587 __PRETTY_FUNCTION__,
1588 zebra_route_string(re->type));
1589 if (re->type == ZEBRA_ROUTE_BGP
1590 && !CHECK_FLAG(re->flags, ZEBRA_FLAG_IBGP))
1591 zlog_debug(
1592 "\tEBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\"");
1593 }
1594 return 0;
1595 }
1596 }
1597 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1598 zlog_debug("\t%s: Nexthop did not lookup in table",
1599 __PRETTY_FUNCTION__);
1600 return 0;
1601 }
1602
1603 /* This function verifies reachability of one given nexthop, which can be
1604 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
1605 * in nexthop->flags field. The nexthop->ifindex will be updated
1606 * appropriately as well. An existing route map can turn
1607 * (otherwise active) nexthop into inactive, but not vice versa.
1608 *
1609 * If it finds a nexthop recursivedly, set the resolved_id
1610 * to match that nexthop's nhg_hash_entry ID;
1611 *
1612 * The return value is the final value of 'ACTIVE' flag.
1613 */
1614 static unsigned nexthop_active_check(struct route_node *rn,
1615 struct route_entry *re,
1616 struct nexthop *nexthop)
1617 {
1618 struct interface *ifp;
1619 route_map_result_t ret = RMAP_PERMITMATCH;
1620 int family;
1621 char buf[SRCDEST2STR_BUFFER];
1622 const struct prefix *p, *src_p;
1623 struct zebra_vrf *zvrf;
1624
1625 srcdest_rnode_prefixes(rn, &p, &src_p);
1626
1627 if (rn->p.family == AF_INET)
1628 family = AFI_IP;
1629 else if (rn->p.family == AF_INET6)
1630 family = AFI_IP6;
1631 else
1632 family = 0;
1633 switch (nexthop->type) {
1634 case NEXTHOP_TYPE_IFINDEX:
1635 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
1636 if (ifp && if_is_operative(ifp))
1637 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1638 else
1639 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1640 break;
1641 case NEXTHOP_TYPE_IPV4:
1642 case NEXTHOP_TYPE_IPV4_IFINDEX:
1643 family = AFI_IP;
1644 if (nexthop_active(AFI_IP, re, nexthop, rn))
1645 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1646 else
1647 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1648 break;
1649 case NEXTHOP_TYPE_IPV6:
1650 family = AFI_IP6;
1651 if (nexthop_active(AFI_IP6, re, nexthop, rn))
1652 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1653 else
1654 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1655 break;
1656 case NEXTHOP_TYPE_IPV6_IFINDEX:
1657 /* RFC 5549, v4 prefix with v6 NH */
1658 if (rn->p.family != AF_INET)
1659 family = AFI_IP6;
1660 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->gate.ipv6)) {
1661 ifp = if_lookup_by_index(nexthop->ifindex,
1662 nexthop->vrf_id);
1663 if (ifp && if_is_operative(ifp))
1664 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1665 else
1666 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1667 } else {
1668 if (nexthop_active(AFI_IP6, re, nexthop, rn))
1669 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1670 else
1671 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1672 }
1673 break;
1674 case NEXTHOP_TYPE_BLACKHOLE:
1675 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1676 break;
1677 default:
1678 break;
1679 }
1680 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) {
1681 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1682 zlog_debug("\t%s: Unable to find a active nexthop",
1683 __PRETTY_FUNCTION__);
1684 return 0;
1685 }
1686
1687 /* XXX: What exactly do those checks do? Do we support
1688 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
1689 */
1690 if (RIB_SYSTEM_ROUTE(re) || (family == AFI_IP && p->family != AF_INET)
1691 || (family == AFI_IP6 && p->family != AF_INET6))
1692 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1693
1694 /* The original code didn't determine the family correctly
1695 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
1696 * from the rib_table_info in those cases.
1697 * Possibly it may be better to use only the rib_table_info
1698 * in every case.
1699 */
1700 if (!family) {
1701 rib_table_info_t *info;
1702
1703 info = srcdest_rnode_table_info(rn);
1704 family = info->afi;
1705 }
1706
1707 memset(&nexthop->rmap_src.ipv6, 0, sizeof(union g_addr));
1708
1709 zvrf = zebra_vrf_lookup_by_id(nexthop->vrf_id);
1710 if (!zvrf) {
1711 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1712 zlog_debug("\t%s: zvrf is NULL", __PRETTY_FUNCTION__);
1713 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1714 }
1715
1716 /* It'll get set if required inside */
1717 ret = zebra_route_map_check(family, re->type, re->instance, p, nexthop,
1718 zvrf, re->tag);
1719 if (ret == RMAP_DENYMATCH) {
1720 if (IS_ZEBRA_DEBUG_RIB) {
1721 srcdest_rnode2str(rn, buf, sizeof(buf));
1722 zlog_debug(
1723 "%u:%s: Filtering out with NH out %s due to route map",
1724 re->vrf_id, buf,
1725 ifindex2ifname(nexthop->ifindex,
1726 nexthop->vrf_id));
1727 }
1728 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1729 }
1730 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1731 }
1732
1733 /*
1734 * Iterate over all nexthops of the given RIB entry and refresh their
1735 * ACTIVE flag. If any nexthop is found to toggle the ACTIVE flag,
1736 * the whole re structure is flagged with ROUTE_ENTRY_CHANGED.
1737 *
1738 * Return value is the new number of active nexthops.
1739 */
1740 int nexthop_active_update(struct route_node *rn, struct route_entry *re)
1741 {
1742 struct nexthop_group new_grp = {};
1743 struct nexthop *nexthop;
1744 union g_addr prev_src;
1745 unsigned int prev_active, new_active;
1746 ifindex_t prev_index;
1747 uint8_t curr_active = 0;
1748
1749 afi_t rt_afi = family2afi(rn->p.family);
1750
1751 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1752
1753 /* Copy over the nexthops in current state */
1754 nexthop_group_copy(&new_grp, re->nhe->nhg);
1755
1756 for (nexthop = new_grp.nexthop; nexthop; nexthop = nexthop->next) {
1757
1758 /* No protocol daemon provides src and so we're skipping
1759 * tracking it */
1760 prev_src = nexthop->rmap_src;
1761 prev_active = CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1762 prev_index = nexthop->ifindex;
1763 /*
1764 * We need to respect the multipath_num here
1765 * as that what we should be able to install from
1766 * a multipath perpsective should not be a data plane
1767 * decision point.
1768 */
1769 new_active =
1770 nexthop_active_check(rn, re, nexthop);
1771
1772 if (new_active && curr_active >= zrouter.multipath_num) {
1773 struct nexthop *nh;
1774
1775 /* Set it and its resolved nexthop as inactive. */
1776 for (nh = nexthop; nh; nh = nh->resolved)
1777 UNSET_FLAG(nh->flags, NEXTHOP_FLAG_ACTIVE);
1778
1779 new_active = 0;
1780 }
1781
1782 if (new_active)
1783 curr_active++;
1784
1785 /* Don't allow src setting on IPv6 addr for now */
1786 if (prev_active != new_active || prev_index != nexthop->ifindex
1787 || ((nexthop->type >= NEXTHOP_TYPE_IFINDEX
1788 && nexthop->type < NEXTHOP_TYPE_IPV6)
1789 && prev_src.ipv4.s_addr
1790 != nexthop->rmap_src.ipv4.s_addr)
1791 || ((nexthop->type >= NEXTHOP_TYPE_IPV6
1792 && nexthop->type < NEXTHOP_TYPE_BLACKHOLE)
1793 && !(IPV6_ADDR_SAME(&prev_src.ipv6,
1794 &nexthop->rmap_src.ipv6)))
1795 || CHECK_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED))
1796 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1797 }
1798
1799 if (CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)) {
1800 struct nhg_hash_entry *new_nhe = NULL;
1801
1802 new_nhe = zebra_nhg_rib_find(0, &new_grp, rt_afi);
1803
1804 route_entry_update_nhe(re, new_nhe);
1805 }
1806
1807 if (curr_active) {
1808 struct nhg_hash_entry *nhe = NULL;
1809
1810 nhe = zebra_nhg_lookup_id(re->nhe_id);
1811
1812 if (nhe)
1813 SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID);
1814 else
1815 flog_err(
1816 EC_ZEBRA_TABLE_LOOKUP_FAILED,
1817 "Active update on NHE id=%u that we do not have in our tables",
1818 re->nhe_id);
1819 }
1820
1821 /*
1822 * Do not need these nexthops anymore since they
1823 * were either copied over into an nhe or not
1824 * used at all.
1825 */
1826 nexthops_free(new_grp.nexthop);
1827 return curr_active;
1828 }
1829
1830 /* Convert a nhe into a group array */
1831 uint8_t zebra_nhg_nhe2grp(struct nh_grp *grp, struct nhg_hash_entry *nhe,
1832 int max_num)
1833 {
1834 struct nhg_connected *rb_node_dep = NULL;
1835 struct nhg_hash_entry *depend = NULL;
1836 uint8_t i = 0;
1837
1838 frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) {
1839 bool duplicate = false;
1840
1841 depend = rb_node_dep->nhe;
1842
1843 /*
1844 * If its recursive, use its resolved nhe in the group
1845 */
1846 if (CHECK_FLAG(depend->flags, NEXTHOP_GROUP_RECURSIVE)) {
1847 depend = zebra_nhg_resolve(depend);
1848 if (!depend) {
1849 flog_err(
1850 EC_ZEBRA_NHG_FIB_UPDATE,
1851 "Failed to recursively resolve Nexthop Hash Entry in the group id=%u",
1852 nhe->id);
1853 continue;
1854 }
1855 }
1856
1857 /* Check for duplicate IDs, kernel doesn't like that */
1858 for (int j = 0; j < i; j++) {
1859 if (depend->id == grp[j].id)
1860 duplicate = true;
1861 }
1862
1863 if (!duplicate) {
1864 grp[i].id = depend->id;
1865 /* We aren't using weights for anything right now */
1866 grp[i].weight = depend->nhg->nexthop->weight;
1867 i++;
1868 }
1869
1870 if (i >= max_num)
1871 goto done;
1872 }
1873
1874 done:
1875 return i;
1876 }
1877
1878 void zebra_nhg_install_kernel(struct nhg_hash_entry *nhe)
1879 {
1880 struct nhg_connected *rb_node_dep = NULL;
1881
1882 /* Resolve it first */
1883 nhe = zebra_nhg_resolve(nhe);
1884
1885 /* Make sure all depends are installed/queued */
1886 frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) {
1887 zebra_nhg_install_kernel(rb_node_dep->nhe);
1888 }
1889
1890 if (!CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED)
1891 && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED)) {
1892 /* Change its type to us since we are installing it */
1893 nhe->type = ZEBRA_ROUTE_NHG;
1894
1895 int ret = dplane_nexthop_add(nhe);
1896
1897 switch (ret) {
1898 case ZEBRA_DPLANE_REQUEST_QUEUED:
1899 SET_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED);
1900 break;
1901 case ZEBRA_DPLANE_REQUEST_FAILURE:
1902 flog_err(
1903 EC_ZEBRA_DP_INSTALL_FAIL,
1904 "Failed to install Nexthop ID (%u) into the kernel",
1905 nhe->id);
1906 break;
1907 case ZEBRA_DPLANE_REQUEST_SUCCESS:
1908 SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
1909 zebra_nhg_handle_install(nhe);
1910 break;
1911 }
1912 }
1913 }
1914
1915 void zebra_nhg_uninstall_kernel(struct nhg_hash_entry *nhe)
1916 {
1917 if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED)) {
1918 int ret = dplane_nexthop_delete(nhe);
1919
1920 switch (ret) {
1921 case ZEBRA_DPLANE_REQUEST_QUEUED:
1922 SET_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED);
1923 break;
1924 case ZEBRA_DPLANE_REQUEST_FAILURE:
1925 flog_err(
1926 EC_ZEBRA_DP_DELETE_FAIL,
1927 "Failed to uninstall Nexthop ID (%u) from the kernel",
1928 nhe->id);
1929 break;
1930 case ZEBRA_DPLANE_REQUEST_SUCCESS:
1931 UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
1932 break;
1933 }
1934 }
1935
1936 zebra_nhg_handle_uninstall(nhe);
1937 }
1938
1939 void zebra_nhg_dplane_result(struct zebra_dplane_ctx *ctx)
1940 {
1941 enum dplane_op_e op;
1942 enum zebra_dplane_result status;
1943 uint32_t id = 0;
1944 struct nhg_hash_entry *nhe = NULL;
1945
1946 op = dplane_ctx_get_op(ctx);
1947 status = dplane_ctx_get_status(ctx);
1948
1949 id = dplane_ctx_get_nhe_id(ctx);
1950
1951 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1952 zlog_debug(
1953 "Nexthop dplane ctx %p, op %s, nexthop ID (%u), result %s",
1954 ctx, dplane_op2str(op), id, dplane_res2str(status));
1955
1956 switch (op) {
1957 case DPLANE_OP_NH_DELETE:
1958 if (status != ZEBRA_DPLANE_REQUEST_SUCCESS)
1959 flog_err(
1960 EC_ZEBRA_DP_DELETE_FAIL,
1961 "Failed to uninstall Nexthop ID (%u) from the kernel",
1962 id);
1963 /* We already free'd the data, nothing to do */
1964 break;
1965 case DPLANE_OP_NH_INSTALL:
1966 case DPLANE_OP_NH_UPDATE:
1967 nhe = zebra_nhg_lookup_id(id);
1968
1969 if (!nhe) {
1970 flog_err(
1971 EC_ZEBRA_NHG_SYNC,
1972 "%s operation preformed on Nexthop ID (%u) in the kernel, that we no longer have in our table",
1973 dplane_op2str(op), id);
1974 break;
1975 }
1976
1977 UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED);
1978 if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
1979 SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID);
1980 SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
1981 zebra_nhg_handle_install(nhe);
1982 } else
1983 flog_err(
1984 EC_ZEBRA_DP_INSTALL_FAIL,
1985 "Failed to install Nexthop ID (%u) into the kernel",
1986 nhe->id);
1987 break;
1988 case DPLANE_OP_ROUTE_INSTALL:
1989 case DPLANE_OP_ROUTE_UPDATE:
1990 case DPLANE_OP_ROUTE_DELETE:
1991 case DPLANE_OP_ROUTE_NOTIFY:
1992 case DPLANE_OP_LSP_INSTALL:
1993 case DPLANE_OP_LSP_UPDATE:
1994 case DPLANE_OP_LSP_DELETE:
1995 case DPLANE_OP_LSP_NOTIFY:
1996 case DPLANE_OP_PW_INSTALL:
1997 case DPLANE_OP_PW_UNINSTALL:
1998 case DPLANE_OP_SYS_ROUTE_ADD:
1999 case DPLANE_OP_SYS_ROUTE_DELETE:
2000 case DPLANE_OP_ADDR_INSTALL:
2001 case DPLANE_OP_ADDR_UNINSTALL:
2002 case DPLANE_OP_MAC_INSTALL:
2003 case DPLANE_OP_MAC_DELETE:
2004 case DPLANE_OP_NEIGH_INSTALL:
2005 case DPLANE_OP_NEIGH_UPDATE:
2006 case DPLANE_OP_NEIGH_DELETE:
2007 case DPLANE_OP_VTEP_ADD:
2008 case DPLANE_OP_VTEP_DELETE:
2009 case DPLANE_OP_NONE:
2010 break;
2011 }
2012
2013 dplane_ctx_fini(&ctx);
2014 }
2015
2016 static void zebra_nhg_sweep_entry(struct hash_bucket *bucket, void *arg)
2017 {
2018 struct nhg_hash_entry *nhe = NULL;
2019
2020 nhe = (struct nhg_hash_entry *)bucket->data;
2021
2022 /* If its being ref'd, just let it be uninstalled via a route removal */
2023 if (ZEBRA_NHG_CREATED(nhe) && nhe->refcnt <= 0)
2024 zebra_nhg_uninstall_kernel(nhe);
2025 }
2026
2027 void zebra_nhg_sweep_table(struct hash *hash)
2028 {
2029 hash_iterate(hash, zebra_nhg_sweep_entry, NULL);
2030 }