]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_nhg.c
zebra: warn if zapi NHG add has no nexthops
[mirror_frr.git] / zebra / zebra_nhg.c
CommitLineData
ad28e79a
SW
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"
50d89650 26#include "lib/nexthop_group_private.h"
ad28e79a 27#include "lib/routemap.h"
b43434ad 28#include "lib/mpls.h"
69171da2 29#include "lib/jhash.h"
51d80884 30#include "lib/debug.h"
31f937fb 31#include "lib/lib_errors.h"
ad28e79a
SW
32
33#include "zebra/connected.h"
34#include "zebra/debug.h"
35#include "zebra/zebra_router.h"
5948f013 36#include "zebra/zebra_nhg_private.h"
ad28e79a
SW
37#include "zebra/zebra_rnh.h"
38#include "zebra/zebra_routemap.h"
51d80884 39#include "zebra/zebra_memory.h"
31f937fb 40#include "zebra/zebra_srte.h"
51d80884 41#include "zebra/zserv.h"
ad28e79a 42#include "zebra/rt.h"
d9f5b2f5 43#include "zebra_errors.h"
0c8215cb 44#include "zebra_dplane.h"
fe593b78 45#include "zebra/interface.h"
d9f5b2f5 46
51d80884 47DEFINE_MTYPE_STATIC(ZEBRA, NHG, "Nexthop Group Entry");
a15d4c00 48DEFINE_MTYPE_STATIC(ZEBRA, NHG_CONNECTED, "Nexthop Group Connected");
e22e8001 49DEFINE_MTYPE_STATIC(ZEBRA, NHG_CTX, "Nexthop Group Context");
0c8215cb 50
38e40db1
SW
51/* id counter to keep in sync with kernel */
52uint32_t id_counter;
53
7c99d51b
MS
54/* */
55static bool g_nexthops_enabled = true;
6c67f41f 56static bool proto_nexthops_only = false;
7c99d51b 57
0885b1e3
SW
58static struct nhg_hash_entry *depends_find(const struct nexthop *nh, afi_t afi,
59 int type);
37c6708b 60static void depends_add(struct nhg_connected_tree_head *head,
5657e7e9 61 struct nhg_hash_entry *depend);
38e40db1
SW
62static struct nhg_hash_entry *
63depends_find_add(struct nhg_connected_tree_head *head, struct nexthop *nh,
0885b1e3 64 afi_t afi, int type);
38e40db1
SW
65static struct nhg_hash_entry *
66depends_find_id_add(struct nhg_connected_tree_head *head, uint32_t id);
37c6708b 67static void depends_decrement_free(struct nhg_connected_tree_head *head);
0c8215cb 68
1d48702e
MS
69static struct nhg_backup_info *
70nhg_backup_copy(const struct nhg_backup_info *orig);
71
ac5d1091
SW
72/* Helper function for getting the next allocatable ID */
73static uint32_t nhg_get_next_id()
74{
75 while (1) {
76 id_counter++;
77
78 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
79 zlog_debug("%s: ID %u checking", __func__, id_counter);
80
81 if (id_counter == ZEBRA_NHG_PROTO_LOWER) {
82 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
83 zlog_debug("%s: ID counter wrapped", __func__);
84
85 id_counter = 0;
86 continue;
87 }
88
89 if (zebra_nhg_lookup_id(id_counter)) {
90 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
91 zlog_debug("%s: ID already exists", __func__);
92
93 continue;
94 }
95
96 break;
97 }
98
99 return id_counter;
100}
e22e8001 101
5948f013 102static void nhg_connected_free(struct nhg_connected *dep)
0c8215cb 103{
a15d4c00 104 XFREE(MTYPE_NHG_CONNECTED, dep);
0c8215cb
SW
105}
106
5948f013 107static struct nhg_connected *nhg_connected_new(struct nhg_hash_entry *nhe)
0c8215cb 108{
a15d4c00 109 struct nhg_connected *new = NULL;
0c8215cb 110
a15d4c00 111 new = XCALLOC(MTYPE_NHG_CONNECTED, sizeof(struct nhg_connected));
0c8215cb
SW
112 new->nhe = nhe;
113
114 return new;
115}
116
37c6708b 117void nhg_connected_tree_free(struct nhg_connected_tree_head *head)
0c8215cb 118{
a15d4c00 119 struct nhg_connected *rb_node_dep = NULL;
0c8215cb 120
37c6708b 121 if (!nhg_connected_tree_is_empty(head)) {
fec211ad 122 frr_each_safe(nhg_connected_tree, head, rb_node_dep) {
37c6708b 123 nhg_connected_tree_del(head, rb_node_dep);
fe593b78
SW
124 nhg_connected_free(rb_node_dep);
125 }
0c8215cb 126 }
0c8215cb
SW
127}
128
37c6708b 129bool nhg_connected_tree_is_empty(const struct nhg_connected_tree_head *head)
0c8215cb 130{
fec211ad 131 return nhg_connected_tree_count(head) ? false : true;
0c8215cb
SW
132}
133
98cda54a 134struct nhg_connected *
37c6708b 135nhg_connected_tree_root(struct nhg_connected_tree_head *head)
98cda54a 136{
37c6708b 137 return nhg_connected_tree_first(head);
98cda54a
SW
138}
139
5bf15faa
SW
140struct nhg_hash_entry *
141nhg_connected_tree_del_nhe(struct nhg_connected_tree_head *head,
142 struct nhg_hash_entry *depend)
0c8215cb 143{
a15d4c00 144 struct nhg_connected lookup = {};
085304dc 145 struct nhg_connected *remove = NULL;
5bf15faa 146 struct nhg_hash_entry *removed_nhe;
0c8215cb
SW
147
148 lookup.nhe = depend;
3119f6a1 149
085304dc 150 /* Lookup to find the element, then remove it */
37c6708b 151 remove = nhg_connected_tree_find(head, &lookup);
085304dc 152 if (remove)
5bf15faa
SW
153 /* Re-returning here just in case this API changes..
154 * the _del list api's are a bit undefined at the moment.
155 *
156 * So hopefully returning here will make it fail if the api
157 * changes to something different than currently expected.
158 */
159 remove = nhg_connected_tree_del(head, remove);
160
161 /* If the entry was sucessfully removed, free the 'connected` struct */
162 if (remove) {
163 removed_nhe = remove->nhe;
085304dc 164 nhg_connected_free(remove);
5bf15faa
SW
165 return removed_nhe;
166 }
167
168 return NULL;
3119f6a1
SW
169}
170
5bf15faa
SW
171/* Assuming UNIQUE RB tree. If this changes, assumptions here about
172 * insertion need to change.
173 */
174struct nhg_hash_entry *
175nhg_connected_tree_add_nhe(struct nhg_connected_tree_head *head,
176 struct nhg_hash_entry *depend)
3119f6a1 177{
a15d4c00 178 struct nhg_connected *new = NULL;
0c8215cb 179
a15d4c00 180 new = nhg_connected_new(depend);
0c8215cb 181
5bf15faa
SW
182 /* On success, NULL will be returned from the
183 * RB code.
184 */
185 if (new && (nhg_connected_tree_add(head, new) == NULL))
186 return NULL;
187
188 /* If it wasn't successful, it must be a duplicate. We enforce the
189 * unique property for the `nhg_connected` tree.
190 */
191 nhg_connected_free(new);
192
193 return depend;
3119f6a1
SW
194}
195
37c6708b
SW
196static void
197nhg_connected_tree_decrement_ref(struct nhg_connected_tree_head *head)
32e29e79
SW
198{
199 struct nhg_connected *rb_node_dep = NULL;
32e29e79 200
fec211ad 201 frr_each_safe(nhg_connected_tree, head, rb_node_dep) {
32e29e79
SW
202 zebra_nhg_decrement_ref(rb_node_dep->nhe);
203 }
204}
205
37c6708b
SW
206static void
207nhg_connected_tree_increment_ref(struct nhg_connected_tree_head *head)
32e29e79
SW
208{
209 struct nhg_connected *rb_node_dep = NULL;
210
fec211ad 211 frr_each(nhg_connected_tree, head, rb_node_dep) {
32e29e79
SW
212 zebra_nhg_increment_ref(rb_node_dep->nhe);
213 }
214}
215
98cda54a
SW
216struct nhg_hash_entry *zebra_nhg_resolve(struct nhg_hash_entry *nhe)
217{
218 if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_RECURSIVE)
219 && !zebra_nhg_depends_is_empty(nhe)) {
37c6708b 220 nhe = nhg_connected_tree_root(&nhe->nhg_depends)->nhe;
98cda54a
SW
221 return zebra_nhg_resolve(nhe);
222 }
223
224 return nhe;
225}
226
fe593b78 227unsigned int zebra_nhg_depends_count(const struct nhg_hash_entry *nhe)
a15d4c00 228{
37c6708b 229 return nhg_connected_tree_count(&nhe->nhg_depends);
a15d4c00
SW
230}
231
fe593b78 232bool zebra_nhg_depends_is_empty(const struct nhg_hash_entry *nhe)
a15d4c00 233{
37c6708b 234 return nhg_connected_tree_is_empty(&nhe->nhg_depends);
a15d4c00
SW
235}
236
5948f013
SW
237static void zebra_nhg_depends_del(struct nhg_hash_entry *from,
238 struct nhg_hash_entry *depend)
3119f6a1 239{
37c6708b 240 nhg_connected_tree_del_nhe(&from->nhg_depends, depend);
3119f6a1
SW
241}
242
5948f013 243static void zebra_nhg_depends_init(struct nhg_hash_entry *nhe)
148a0103 244{
37c6708b 245 nhg_connected_tree_init(&nhe->nhg_depends);
148a0103
SW
246}
247
fe593b78
SW
248unsigned int zebra_nhg_dependents_count(const struct nhg_hash_entry *nhe)
249{
37c6708b 250 return nhg_connected_tree_count(&nhe->nhg_dependents);
fe593b78
SW
251}
252
5948f013 253
fe593b78
SW
254bool zebra_nhg_dependents_is_empty(const struct nhg_hash_entry *nhe)
255{
37c6708b 256 return nhg_connected_tree_is_empty(&nhe->nhg_dependents);
fe593b78
SW
257}
258
5948f013
SW
259static void zebra_nhg_dependents_del(struct nhg_hash_entry *from,
260 struct nhg_hash_entry *dependent)
fe593b78 261{
37c6708b 262 nhg_connected_tree_del_nhe(&from->nhg_dependents, dependent);
fe593b78
SW
263}
264
5948f013
SW
265static void zebra_nhg_dependents_add(struct nhg_hash_entry *to,
266 struct nhg_hash_entry *dependent)
fe593b78 267{
37c6708b 268 nhg_connected_tree_add_nhe(&to->nhg_dependents, dependent);
fe593b78
SW
269}
270
5948f013 271static void zebra_nhg_dependents_init(struct nhg_hash_entry *nhe)
fe593b78 272{
37c6708b 273 nhg_connected_tree_init(&nhe->nhg_dependents);
fe593b78
SW
274}
275
21615102
SW
276/* Release this nhe from anything depending on it */
277static void zebra_nhg_dependents_release(struct nhg_hash_entry *nhe)
278{
80286aa5 279 struct nhg_connected *rb_node_dep = NULL;
21615102 280
80286aa5
SW
281 frr_each_safe(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep) {
282 zebra_nhg_depends_del(rb_node_dep->nhe, nhe);
283 /* recheck validity of the dependent */
284 zebra_nhg_check_valid(rb_node_dep->nhe);
21615102
SW
285 }
286}
287
5948f013
SW
288/* Release this nhe from anything that it depends on */
289static void zebra_nhg_depends_release(struct nhg_hash_entry *nhe)
290{
291 if (!zebra_nhg_depends_is_empty(nhe)) {
292 struct nhg_connected *rb_node_dep = NULL;
293
294 frr_each_safe(nhg_connected_tree, &nhe->nhg_depends,
295 rb_node_dep) {
296 zebra_nhg_dependents_del(rb_node_dep->nhe, nhe);
297 }
298 }
299}
300
301
d9f5b2f5
SW
302struct nhg_hash_entry *zebra_nhg_lookup_id(uint32_t id)
303{
0c8215cb 304 struct nhg_hash_entry lookup = {};
d9f5b2f5
SW
305
306 lookup.id = id;
307 return hash_lookup(zrouter.nhgs_id, &lookup);
308}
309
5948f013 310static int zebra_nhg_insert_id(struct nhg_hash_entry *nhe)
d9f5b2f5
SW
311{
312 if (hash_lookup(zrouter.nhgs_id, nhe)) {
313 flog_err(
314 EC_ZEBRA_NHG_TABLE_INSERT_FAILED,
315 "Failed inserting NHG id=%u into the ID hash table, entry already exists",
316 nhe->id);
317 return -1;
318 }
319
320 hash_get(zrouter.nhgs_id, nhe, hash_alloc_intern);
321
322 return 0;
323}
ad28e79a 324
5948f013
SW
325static void zebra_nhg_set_if(struct nhg_hash_entry *nhe, struct interface *ifp)
326{
327 nhe->ifp = ifp;
328 if_nhg_dependents_add(ifp, nhe);
329}
330
38e40db1
SW
331static void
332zebra_nhg_connect_depends(struct nhg_hash_entry *nhe,
377e29f7 333 struct nhg_connected_tree_head *nhg_depends)
4e49c8b8 334{
a15d4c00
SW
335 struct nhg_connected *rb_node_dep = NULL;
336
38e40db1
SW
337 /* This has been allocated higher above in the stack. Could probably
338 * re-allocate and free the old stuff but just using the same memory
339 * for now. Otherwise, their might be a time trade-off for repeated
340 * alloc/frees as startup.
341 */
377e29f7 342 nhe->nhg_depends = *nhg_depends;
4e49c8b8 343
a15d4c00 344 /* Attach backpointer to anything that it depends on */
fe593b78 345 zebra_nhg_dependents_init(nhe);
a15d4c00 346 if (!zebra_nhg_depends_is_empty(nhe)) {
fec211ad 347 frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) {
377e29f7
MS
348 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
349 zlog_debug("%s: nhe %p (%u), dep %p (%u)",
350 __func__, nhe, nhe->id,
351 rb_node_dep->nhe,
352 rb_node_dep->nhe->id);
353
a15d4c00
SW
354 zebra_nhg_dependents_add(rb_node_dep->nhe, nhe);
355 }
356 }
377e29f7 357}
4e49c8b8 358
377e29f7
MS
359/* Init an nhe, for use in a hash lookup for example */
360void zebra_nhe_init(struct nhg_hash_entry *nhe, afi_t afi,
361 const struct nexthop *nh)
362{
363 memset(nhe, 0, sizeof(struct nhg_hash_entry));
364 nhe->vrf_id = VRF_DEFAULT;
365 nhe->type = ZEBRA_ROUTE_NHG;
366 nhe->afi = AFI_UNSPEC;
7b683a96 367
377e29f7
MS
368 /* There are some special rules that apply to groups representing
369 * a single nexthop.
370 */
371 if (nh && (nh->next == NULL)) {
372 switch (nh->type) {
373 case (NEXTHOP_TYPE_IFINDEX):
374 case (NEXTHOP_TYPE_BLACKHOLE):
375 /*
376 * This switch case handles setting the afi different
377 * for ipv4/v6 routes. Ifindex/blackhole nexthop
378 * objects cannot be ambiguous, they must be Address
379 * Family specific. If we get here, we will either use
380 * the AF of the route, or the one we got passed from
381 * here from the kernel.
382 */
383 nhe->afi = afi;
384 break;
385 case (NEXTHOP_TYPE_IPV4_IFINDEX):
386 case (NEXTHOP_TYPE_IPV4):
387 nhe->afi = AFI_IP;
388 break;
389 case (NEXTHOP_TYPE_IPV6_IFINDEX):
390 case (NEXTHOP_TYPE_IPV6):
391 nhe->afi = AFI_IP6;
392 break;
393 }
7b683a96 394 }
38e40db1
SW
395}
396
0eb97b86 397struct nhg_hash_entry *zebra_nhg_alloc(void)
38e40db1
SW
398{
399 struct nhg_hash_entry *nhe;
400
401 nhe = XCALLOC(MTYPE_NHG, sizeof(struct nhg_hash_entry));
402
0eb97b86
MS
403 return nhe;
404}
405
f727646a
MS
406/*
407 * Allocate new nhe and make shallow copy of 'orig'; no
408 * recursive info is copied.
409 */
410struct nhg_hash_entry *zebra_nhe_copy(const struct nhg_hash_entry *orig,
411 uint32_t id)
0eb97b86
MS
412{
413 struct nhg_hash_entry *nhe;
414
415 nhe = zebra_nhg_alloc();
416
38e40db1
SW
417 nhe->id = id;
418
1d48702e 419 nexthop_group_copy(&(nhe->nhg), &(orig->nhg));
38e40db1 420
1d48702e
MS
421 nhe->vrf_id = orig->vrf_id;
422 nhe->afi = orig->afi;
423 nhe->type = orig->type ? orig->type : ZEBRA_ROUTE_NHG;
38e40db1
SW
424 nhe->refcnt = 0;
425 nhe->dplane_ref = zebra_router_get_next_sequence();
426
1d48702e
MS
427 /* Copy backup info also, if present */
428 if (orig->backup_info)
429 nhe->backup_info = nhg_backup_copy(orig->backup_info);
430
38e40db1
SW
431 return nhe;
432}
433
434/* Allocation via hash handler */
435static void *zebra_nhg_hash_alloc(void *arg)
436{
437 struct nhg_hash_entry *nhe = NULL;
438 struct nhg_hash_entry *copy = arg;
7b683a96 439
f727646a 440 nhe = zebra_nhe_copy(copy, copy->id);
9ef49038
SW
441
442 /* Mark duplicate nexthops in a group at creation time. */
c415d895 443 nexthop_group_mark_duplicates(&(nhe->nhg));
9ef49038 444
377e29f7
MS
445 zebra_nhg_connect_depends(nhe, &(copy->nhg_depends));
446
447 /* Add the ifp now if it's not a group or recursive and has ifindex */
448 if (zebra_nhg_depends_is_empty(nhe) && nhe->nhg.nexthop
449 && nhe->nhg.nexthop->ifindex) {
450 struct interface *ifp = NULL;
451
452 ifp = if_lookup_by_index(nhe->nhg.nexthop->ifindex,
453 nhe->nhg.nexthop->vrf_id);
454 if (ifp)
455 zebra_nhg_set_if(nhe, ifp);
456 else
457 flog_err(
458 EC_ZEBRA_IF_LOOKUP_FAILED,
459 "Zebra failed to lookup an interface with ifindex=%d in vrf=%u for NHE id=%u",
460 nhe->nhg.nexthop->ifindex,
461 nhe->nhg.nexthop->vrf_id, nhe->id);
462 }
463
d9f5b2f5 464
4e49c8b8
DS
465 return nhe;
466}
467
4e49c8b8
DS
468uint32_t zebra_nhg_hash_key(const void *arg)
469{
470 const struct nhg_hash_entry *nhe = arg;
0885b1e3
SW
471 uint32_t key = 0x5a351234;
472 uint32_t primary = 0;
473 uint32_t backup = 0;
474
475 primary = nexthop_group_hash(&(nhe->nhg));
476 if (nhe->backup_info)
477 backup = nexthop_group_hash(&(nhe->backup_info->nhe->nhg));
d9f5b2f5 478
0885b1e3
SW
479 key = jhash_3words(primary, backup, nhe->type, key);
480
481 key = jhash_2words(nhe->vrf_id, nhe->afi, key);
d9f5b2f5 482
d9f5b2f5 483 return key;
4e49c8b8
DS
484}
485
a95b8020
SW
486uint32_t zebra_nhg_id_key(const void *arg)
487{
488 const struct nhg_hash_entry *nhe = arg;
489
490 return nhe->id;
491}
492
1d48702e
MS
493/* Helper with common nhg/nhe nexthop comparison logic */
494static bool nhg_compare_nexthops(const struct nexthop *nh1,
495 const struct nexthop *nh2)
496{
f924db49 497 assert(nh1 != NULL && nh2 != NULL);
1d48702e
MS
498
499 /*
500 * We have to check the active flag of each individual one,
501 * not just the overall active_num. This solves the special case
502 * issue of a route with a nexthop group with one nexthop
503 * resolving to itself and thus marking it inactive. If we
504 * have two different routes each wanting to mark a different
505 * nexthop inactive, they need to hash to two different groups.
506 *
507 * If we just hashed on num_active, they would hash the same
508 * which is incorrect.
509 *
510 * ex)
511 * 1.1.1.0/24
512 * -> 1.1.1.1 dummy1 (inactive)
513 * -> 1.1.2.1 dummy2
514 *
515 * 1.1.2.0/24
516 * -> 1.1.1.1 dummy1
517 * -> 1.1.2.1 dummy2 (inactive)
518 *
519 * Without checking each individual one, they would hash to
520 * the same group and both have 1.1.1.1 dummy1 marked inactive.
521 *
522 */
523 if (CHECK_FLAG(nh1->flags, NEXTHOP_FLAG_ACTIVE)
524 != CHECK_FLAG(nh2->flags, NEXTHOP_FLAG_ACTIVE))
525 return false;
526
527 if (!nexthop_same(nh1, nh2))
528 return false;
529
530 return true;
531}
532
4e49c8b8
DS
533bool zebra_nhg_hash_equal(const void *arg1, const void *arg2)
534{
535 const struct nhg_hash_entry *nhe1 = arg1;
536 const struct nhg_hash_entry *nhe2 = arg2;
148813c2
SW
537 struct nexthop *nexthop1;
538 struct nexthop *nexthop2;
4e49c8b8 539
98cda54a
SW
540 /* No matter what if they equal IDs, assume equal */
541 if (nhe1->id && nhe2->id && (nhe1->id == nhe2->id))
542 return true;
543
0885b1e3
SW
544 if (nhe1->type != nhe2->type)
545 return false;
546
4e49c8b8
DS
547 if (nhe1->vrf_id != nhe2->vrf_id)
548 return false;
549
77b76fc9
SW
550 if (nhe1->afi != nhe2->afi)
551 return false;
552
1d48702e 553 /* Nexthops should be in-order, so we simply compare them in-place */
c415d895 554 for (nexthop1 = nhe1->nhg.nexthop, nexthop2 = nhe2->nhg.nexthop;
f924db49 555 nexthop1 && nexthop2;
148813c2 556 nexthop1 = nexthop1->next, nexthop2 = nexthop2->next) {
148813c2 557
1d48702e 558 if (!nhg_compare_nexthops(nexthop1, nexthop2))
148813c2 559 return false;
1d48702e 560 }
148813c2 561
f924db49
MS
562 /* Check for unequal list lengths */
563 if (nexthop1 || nexthop2)
564 return false;
565
1d48702e
MS
566 /* If there's no backup info, comparison is done. */
567 if ((nhe1->backup_info == NULL) && (nhe2->backup_info == NULL))
568 return true;
569
570 /* Compare backup info also - test the easy things first */
571 if (nhe1->backup_info && (nhe2->backup_info == NULL))
572 return false;
573 if (nhe2->backup_info && (nhe1->backup_info == NULL))
574 return false;
575
576 /* Compare number of backups before actually comparing any */
577 for (nexthop1 = nhe1->backup_info->nhe->nhg.nexthop,
578 nexthop2 = nhe2->backup_info->nhe->nhg.nexthop;
579 nexthop1 && nexthop2;
580 nexthop1 = nexthop1->next, nexthop2 = nexthop2->next) {
581 ;
582 }
583
584 /* Did we find the end of one list before the other? */
585 if (nexthop1 || nexthop2)
586 return false;
587
588 /* Have to compare the backup nexthops */
589 for (nexthop1 = nhe1->backup_info->nhe->nhg.nexthop,
590 nexthop2 = nhe2->backup_info->nhe->nhg.nexthop;
f924db49 591 nexthop1 && nexthop2;
1d48702e 592 nexthop1 = nexthop1->next, nexthop2 = nexthop2->next) {
148813c2 593
1d48702e 594 if (!nhg_compare_nexthops(nexthop1, nexthop2))
148813c2
SW
595 return false;
596 }
e4ac313b 597
4e49c8b8
DS
598 return true;
599}
600
d9f5b2f5 601bool zebra_nhg_hash_id_equal(const void *arg1, const void *arg2)
4e49c8b8 602{
d9f5b2f5
SW
603 const struct nhg_hash_entry *nhe1 = arg1;
604 const struct nhg_hash_entry *nhe2 = arg2;
4e49c8b8 605
d9f5b2f5
SW
606 return nhe1->id == nhe2->id;
607}
4e49c8b8 608
1b366e63
SW
609static int zebra_nhg_process_grp(struct nexthop_group *nhg,
610 struct nhg_connected_tree_head *depends,
611 struct nh_grp *grp, uint8_t count)
e22e8001 612{
37c6708b 613 nhg_connected_tree_init(depends);
e22e8001
SW
614
615 for (int i = 0; i < count; i++) {
616 struct nhg_hash_entry *depend = NULL;
617 /* We do not care about nexthop_grp.weight at
618 * this time. But we should figure out
619 * how to adapt this to our code in
620 * the future.
621 */
38e40db1 622 depend = depends_find_id_add(depends, grp[i].id);
e22e8001 623
38e40db1 624 if (!depend) {
e22e8001
SW
625 flog_err(
626 EC_ZEBRA_NHG_SYNC,
627 "Received Nexthop Group from the kernel with a dependent Nexthop ID (%u) which we do not have in our table",
628 grp[i].id);
1b366e63 629 return -1;
e22e8001 630 }
38e40db1
SW
631
632 /*
633 * If this is a nexthop with its own group
634 * dependencies, add them as well. Not sure its
635 * even possible to have a group within a group
636 * in the kernel.
637 */
638
c415d895 639 copy_nexthops(&nhg->nexthop, depend->nhg.nexthop, NULL);
e22e8001 640 }
1b366e63
SW
641
642 return 0;
e22e8001
SW
643}
644
6384cbcb 645static void handle_recursive_depend(struct nhg_connected_tree_head *nhg_depends,
0885b1e3 646 struct nexthop *nh, afi_t afi, int type)
6384cbcb
SW
647{
648 struct nhg_hash_entry *depend = NULL;
649 struct nexthop_group resolved_ng = {};
650
1d049aba 651 resolved_ng.nexthop = nh;
6384cbcb 652
377e29f7
MS
653 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
654 zlog_debug("%s: head %p, nh %pNHv",
655 __func__, nhg_depends, nh);
656
0885b1e3 657 depend = zebra_nhg_rib_find(0, &resolved_ng, afi, type);
a7e1b02d 658
377e29f7
MS
659 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
660 zlog_debug("%s: nh %pNHv => %p (%u)",
661 __func__, nh, depend,
662 depend ? depend->id : 0);
663
a7e1b02d
SW
664 if (depend)
665 depends_add(nhg_depends, depend);
6384cbcb 666}
e22e8001 667
377e29f7
MS
668/*
669 * Lookup an nhe in the global hash, using data from another nhe. If 'lookup'
670 * has an id value, that's used. Create a new global/shared nhe if not found.
671 */
672static bool zebra_nhe_find(struct nhg_hash_entry **nhe, /* return value */
673 struct nhg_hash_entry *lookup,
674 struct nhg_connected_tree_head *nhg_depends,
675 afi_t afi)
676{
677 bool created = false;
678 bool recursive = false;
0328a5bd 679 struct nhg_hash_entry *newnhe, *backup_nhe;
377e29f7
MS
680 struct nexthop *nh = NULL;
681
682 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
683 zlog_debug("%s: id %u, lookup %p, vrf %d, type %d, depends %p",
684 __func__, lookup->id, lookup,
685 lookup->vrf_id, lookup->type,
686 nhg_depends);
687
688 if (lookup->id)
689 (*nhe) = zebra_nhg_lookup_id(lookup->id);
690 else
691 (*nhe) = hash_lookup(zrouter.nhgs, lookup);
692
693 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
694 zlog_debug("%s: lookup => %p (%u)",
695 __func__, (*nhe),
696 (*nhe) ? (*nhe)->id : 0);
697
698 /* If we found an existing object, we're done */
699 if (*nhe)
700 goto done;
701
702 /* We're going to create/insert a new nhe:
703 * assign the next global id value if necessary.
704 */
705 if (lookup->id == 0)
ac5d1091 706 lookup->id = nhg_get_next_id();
0885b1e3 707
54c89c93 708 if (lookup->id < ZEBRA_NHG_PROTO_LOWER) {
0885b1e3
SW
709 /*
710 * This is a zebra hashed/owned NHG.
711 *
712 * It goes in HASH and ID table.
713 */
714 newnhe = hash_get(zrouter.nhgs, lookup, zebra_nhg_hash_alloc);
715 zebra_nhg_insert_id(newnhe);
716 } else {
717 /*
718 * This is upperproto owned NHG and should not be hashed to.
719 *
720 * It goes in ID table.
721 */
722 newnhe =
723 hash_get(zrouter.nhgs_id, lookup, zebra_nhg_hash_alloc);
724 }
725
377e29f7
MS
726 created = true;
727
728 /* Mail back the new object */
729 *nhe = newnhe;
730
731 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
732 zlog_debug("%s: => created %p (%u)", __func__, newnhe,
733 newnhe->id);
734
735 /* Only hash/lookup the depends if the first lookup
736 * fails to find something. This should hopefully save a
737 * lot of cycles for larger ecmp sizes.
738 */
739 if (nhg_depends) {
740 /* If you don't want to hash on each nexthop in the
741 * nexthop group struct you can pass the depends
742 * directly. Kernel-side we do this since it just looks
743 * them up via IDs.
744 */
745 zebra_nhg_connect_depends(newnhe, nhg_depends);
746 goto done;
747 }
748
749 /* Prepare dependency relationships if this is not a
750 * singleton nexthop. There are two cases: a single
751 * recursive nexthop, where we need a relationship to the
752 * resolving nexthop; or a group of nexthops, where we need
753 * relationships with the corresponding singletons.
754 */
755 zebra_nhg_depends_init(lookup);
756
757 nh = newnhe->nhg.nexthop;
758
759 if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_ACTIVE))
760 SET_FLAG(newnhe->flags, NEXTHOP_GROUP_VALID);
761
54c89c93 762 if (nh->next == NULL && newnhe->id < ZEBRA_NHG_PROTO_LOWER) {
377e29f7
MS
763 if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE)) {
764 /* Single recursive nexthop */
765 handle_recursive_depend(&newnhe->nhg_depends,
0885b1e3
SW
766 nh->resolved, afi,
767 newnhe->type);
377e29f7
MS
768 recursive = true;
769 }
770 } else {
dd1e105f 771 /* Proto-owned are groups by default */
377e29f7
MS
772 /* List of nexthops */
773 for (nh = newnhe->nhg.nexthop; nh; nh = nh->next) {
774 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
775 zlog_debug("%s: depends NH %pNHv %s",
776 __func__, nh,
777 CHECK_FLAG(nh->flags,
778 NEXTHOP_FLAG_RECURSIVE) ?
779 "(R)" : "");
780
0885b1e3
SW
781 depends_find_add(&newnhe->nhg_depends, nh, afi,
782 newnhe->type);
377e29f7
MS
783 }
784 }
785
0328a5bd
MS
786 if (recursive)
787 SET_FLAG((*nhe)->flags, NEXTHOP_GROUP_RECURSIVE);
788
377e29f7
MS
789 if (zebra_nhg_get_backup_nhg(newnhe) == NULL ||
790 zebra_nhg_get_backup_nhg(newnhe)->nexthop == NULL)
0328a5bd
MS
791 goto done;
792
793 /* If there are backup nexthops, add them to the backup
794 * depends tree. The rules here are a little different.
795 */
796 recursive = false;
797 backup_nhe = newnhe->backup_info->nhe;
377e29f7 798
0328a5bd 799 nh = backup_nhe->nhg.nexthop;
377e29f7
MS
800
801 /* Singleton recursive NH */
802 if (nh->next == NULL &&
803 CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE)) {
804 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
805 zlog_debug("%s: backup depend NH %pNHv (R)",
806 __func__, nh);
807
808 /* Single recursive nexthop */
0885b1e3
SW
809 handle_recursive_depend(&backup_nhe->nhg_depends, nh->resolved,
810 afi, backup_nhe->type);
377e29f7
MS
811 recursive = true;
812 } else {
813 /* One or more backup NHs */
814 for (; nh; nh = nh->next) {
815 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
816 zlog_debug("%s: backup depend NH %pNHv %s",
817 __func__, nh,
818 CHECK_FLAG(nh->flags,
819 NEXTHOP_FLAG_RECURSIVE) ?
820 "(R)" : "");
821
0885b1e3
SW
822 depends_find_add(&backup_nhe->nhg_depends, nh, afi,
823 backup_nhe->type);
377e29f7
MS
824 }
825 }
826
377e29f7 827 if (recursive)
0328a5bd 828 SET_FLAG(backup_nhe->flags, NEXTHOP_GROUP_RECURSIVE);
377e29f7
MS
829
830done:
831
832 return created;
833}
834
835/*
836 * Lookup or create an nhe, based on an nhg or an nhe id.
837 */
4505578b
SW
838static bool zebra_nhg_find(struct nhg_hash_entry **nhe, uint32_t id,
839 struct nexthop_group *nhg,
37c6708b 840 struct nhg_connected_tree_head *nhg_depends,
38e40db1 841 vrf_id_t vrf_id, afi_t afi, int type)
a95b8020 842{
0c8215cb 843 struct nhg_hash_entry lookup = {};
4505578b
SW
844 bool created = false;
845
1d48702e
MS
846 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
847 zlog_debug("%s: id %u, nhg %p, vrf %d, type %d, depends %p",
848 __func__, id, nhg, vrf_id, type,
849 nhg_depends);
850
377e29f7
MS
851 /* Use a temporary nhe and call into the superset/common code */
852 lookup.id = id;
9a1588c4 853 lookup.type = type ? type : ZEBRA_ROUTE_NHG;
c415d895 854 lookup.nhg = *nhg;
e22e8001 855
88cafda7 856 lookup.vrf_id = vrf_id;
c415d895 857 if (lookup.nhg.nexthop->next) {
6384cbcb
SW
858 /* Groups can have all vrfs and AF's in them */
859 lookup.afi = AFI_UNSPEC;
6384cbcb 860 } else {
c415d895 861 switch (lookup.nhg.nexthop->type) {
4d21c7c0
SW
862 case (NEXTHOP_TYPE_IFINDEX):
863 case (NEXTHOP_TYPE_BLACKHOLE):
864 /*
865 * This switch case handles setting the afi different
866 * for ipv4/v6 routes. Ifindex/blackhole nexthop
867 * objects cannot be ambiguous, they must be Address
868 * Family specific. If we get here, we will either use
869 * the AF of the route, or the one we got passed from
870 * here from the kernel.
871 */
872 lookup.afi = afi;
873 break;
874 case (NEXTHOP_TYPE_IPV4_IFINDEX):
875 case (NEXTHOP_TYPE_IPV4):
876 lookup.afi = AFI_IP;
877 break;
878 case (NEXTHOP_TYPE_IPV6_IFINDEX):
879 case (NEXTHOP_TYPE_IPV6):
880 lookup.afi = AFI_IP6;
881 break;
882 }
6384cbcb 883 }
a95b8020 884
377e29f7 885 created = zebra_nhe_find(nhe, &lookup, nhg_depends, afi);
d9f5b2f5 886
4505578b 887 return created;
a95b8020
SW
888}
889
e22e8001 890/* Find/create a single nexthop */
6384cbcb
SW
891static struct nhg_hash_entry *
892zebra_nhg_find_nexthop(uint32_t id, struct nexthop *nh, afi_t afi, int type)
3057df51 893{
6384cbcb 894 struct nhg_hash_entry *nhe = NULL;
e22e8001 895 struct nexthop_group nhg = {};
88cafda7 896 vrf_id_t vrf_id = !vrf_is_backend_netns() ? VRF_DEFAULT : nh->vrf_id;
e22e8001 897
0eb97b86 898 nexthop_group_add_sorted(&nhg, nh);
e22e8001 899
88cafda7 900 zebra_nhg_find(&nhe, id, &nhg, NULL, vrf_id, afi, type);
8a507796 901
377e29f7
MS
902 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
903 zlog_debug("%s: nh %pNHv => %p (%u)",
904 __func__, nh, nhe, nhe ? nhe->id : 0);
905
6384cbcb 906 return nhe;
e22e8001
SW
907}
908
10200d40
SW
909static uint32_t nhg_ctx_get_id(const struct nhg_ctx *ctx)
910{
911 return ctx->id;
912}
913
1b366e63 914static void nhg_ctx_set_status(struct nhg_ctx *ctx, enum nhg_ctx_status status)
e22e8001
SW
915{
916 ctx->status = status;
917}
918
1b366e63 919static enum nhg_ctx_status nhg_ctx_get_status(const struct nhg_ctx *ctx)
e22e8001
SW
920{
921 return ctx->status;
922}
923
924static void nhg_ctx_set_op(struct nhg_ctx *ctx, enum nhg_ctx_op_e op)
925{
926 ctx->op = op;
927}
928
929static enum nhg_ctx_op_e nhg_ctx_get_op(const struct nhg_ctx *ctx)
930{
931 return ctx->op;
932}
933
10200d40
SW
934static vrf_id_t nhg_ctx_get_vrf_id(const struct nhg_ctx *ctx)
935{
936 return ctx->vrf_id;
937}
938
939static int nhg_ctx_get_type(const struct nhg_ctx *ctx)
940{
941 return ctx->type;
942}
943
944static int nhg_ctx_get_afi(const struct nhg_ctx *ctx)
945{
946 return ctx->afi;
947}
948
949static struct nexthop *nhg_ctx_get_nh(struct nhg_ctx *ctx)
950{
951 return &ctx->u.nh;
952}
953
954static uint8_t nhg_ctx_get_count(const struct nhg_ctx *ctx)
955{
956 return ctx->count;
957}
958
959static struct nh_grp *nhg_ctx_get_grp(struct nhg_ctx *ctx)
960{
961 return ctx->u.grp;
962}
963
99e7ab12 964static struct nhg_ctx *nhg_ctx_new(void)
7c6d5f25 965{
99e7ab12 966 struct nhg_ctx *new;
7c6d5f25
SW
967
968 new = XCALLOC(MTYPE_NHG_CTX, sizeof(struct nhg_ctx));
969
970 return new;
971}
972
973static void nhg_ctx_free(struct nhg_ctx **ctx)
974{
975 struct nexthop *nh;
976
977 if (ctx == NULL)
978 return;
979
980 assert((*ctx) != NULL);
981
982 if (nhg_ctx_get_count(*ctx))
983 goto done;
984
985 nh = nhg_ctx_get_nh(*ctx);
986
987 nexthop_del_labels(nh);
988
989done:
990 XFREE(MTYPE_NHG_CTX, *ctx);
7c6d5f25
SW
991}
992
81505946
SW
993static struct nhg_ctx *nhg_ctx_init(uint32_t id, struct nexthop *nh,
994 struct nh_grp *grp, vrf_id_t vrf_id,
995 afi_t afi, int type, uint8_t count)
996{
997 struct nhg_ctx *ctx = NULL;
998
999 ctx = nhg_ctx_new();
1000
1001 ctx->id = id;
1002 ctx->vrf_id = vrf_id;
1003 ctx->afi = afi;
1004 ctx->type = type;
1005 ctx->count = count;
1006
1007 if (count)
1008 /* Copy over the array */
1009 memcpy(&ctx->u.grp, grp, count * sizeof(struct nh_grp));
1010 else if (nh)
1011 ctx->u.nh = *nh;
1012
1013 return ctx;
1014}
1015
80286aa5
SW
1016static void zebra_nhg_set_valid(struct nhg_hash_entry *nhe)
1017{
1018 struct nhg_connected *rb_node_dep;
1019
1020 SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID);
1021
1022 frr_each(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep)
1023 zebra_nhg_set_valid(rb_node_dep->nhe);
1024}
1025
1026static void zebra_nhg_set_invalid(struct nhg_hash_entry *nhe)
1027{
1028 struct nhg_connected *rb_node_dep;
1029
1030 UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID);
1031
1032 /* Update validity of nexthops depending on it */
1033 frr_each(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep)
1034 zebra_nhg_check_valid(rb_node_dep->nhe);
1035}
1036
1037void zebra_nhg_check_valid(struct nhg_hash_entry *nhe)
1038{
1039 struct nhg_connected *rb_node_dep = NULL;
1040 bool valid = false;
1041
1042 /* If anthing else in the group is valid, the group is valid */
1043 frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) {
1044 if (CHECK_FLAG(rb_node_dep->nhe->flags, NEXTHOP_GROUP_VALID)) {
1045 valid = true;
1046 goto done;
1047 }
1048 }
1049
1050done:
1051 if (valid)
1052 zebra_nhg_set_valid(nhe);
1053 else
1054 zebra_nhg_set_invalid(nhe);
1055}
1056
dd1e105f 1057static void zebra_nhg_release_all_deps(struct nhg_hash_entry *nhe)
9a1588c4
SW
1058{
1059 /* Remove it from any lists it may be on */
1060 zebra_nhg_depends_release(nhe);
1061 zebra_nhg_dependents_release(nhe);
1062 if (nhe->ifp)
1063 if_nhg_dependents_del(nhe->ifp, nhe);
dd1e105f
SW
1064}
1065
1066static void zebra_nhg_release(struct nhg_hash_entry *nhe)
1067{
1068 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1069 zlog_debug("%s: nhe %p (%u)", __func__, nhe, nhe->id);
1070
1071 zebra_nhg_release_all_deps(nhe);
9a1588c4
SW
1072
1073 /*
0885b1e3 1074 * If its not zebra owned, we didn't store it here and have to be
9a1588c4
SW
1075 * sure we don't clear one thats actually being used.
1076 */
0885b1e3 1077 if (ZEBRA_OWNED(nhe))
9a1588c4 1078 hash_release(zrouter.nhgs, nhe);
9a1588c4 1079
9a1588c4
SW
1080 hash_release(zrouter.nhgs_id, nhe);
1081}
1082
9a1588c4
SW
1083static void zebra_nhg_handle_uninstall(struct nhg_hash_entry *nhe)
1084{
177e711d 1085 zebra_nhg_release(nhe);
9a1588c4
SW
1086 zebra_nhg_free(nhe);
1087}
1088
80286aa5
SW
1089static void zebra_nhg_handle_install(struct nhg_hash_entry *nhe)
1090{
1091 /* Update validity of groups depending on it */
1092 struct nhg_connected *rb_node_dep;
1093
1094 frr_each_safe(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep)
1095 zebra_nhg_set_valid(rb_node_dep->nhe);
1096}
1097
9a1588c4
SW
1098/*
1099 * The kernel/other program has changed the state of a nexthop object we are
1100 * using.
1101 */
1102static void zebra_nhg_handle_kernel_state_change(struct nhg_hash_entry *nhe,
1103 bool is_delete)
1104{
1105 if (nhe->refcnt) {
1106 flog_err(
1107 EC_ZEBRA_NHG_SYNC,
1108 "Kernel %s a nexthop group with ID (%u) that we are still using for a route, sending it back down",
1109 (is_delete ? "deleted" : "updated"), nhe->id);
1110
1111 UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
1112 zebra_nhg_install_kernel(nhe);
177e711d 1113 } else
9a1588c4 1114 zebra_nhg_handle_uninstall(nhe);
9a1588c4
SW
1115}
1116
e22e8001
SW
1117static int nhg_ctx_process_new(struct nhg_ctx *ctx)
1118{
1119 struct nexthop_group *nhg = NULL;
37c6708b 1120 struct nhg_connected_tree_head nhg_depends = {};
9a1588c4 1121 struct nhg_hash_entry *lookup = NULL;
3057df51
SW
1122 struct nhg_hash_entry *nhe = NULL;
1123
10200d40
SW
1124 uint32_t id = nhg_ctx_get_id(ctx);
1125 uint8_t count = nhg_ctx_get_count(ctx);
1126 vrf_id_t vrf_id = nhg_ctx_get_vrf_id(ctx);
1127 int type = nhg_ctx_get_type(ctx);
1128 afi_t afi = nhg_ctx_get_afi(ctx);
1129
1130 lookup = zebra_nhg_lookup_id(id);
9a1588c4 1131
377e29f7
MS
1132 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1133 zlog_debug("%s: id %u, count %d, lookup => %p",
1134 __func__, id, count, lookup);
1135
9a1588c4
SW
1136 if (lookup) {
1137 /* This is already present in our table, hence an update
1138 * that we did not initate.
1139 */
1140 zebra_nhg_handle_kernel_state_change(lookup, false);
1141 return 0;
1142 }
1143
10200d40 1144 if (nhg_ctx_get_count(ctx)) {
e22e8001 1145 nhg = nexthop_group_new();
1b366e63
SW
1146 if (zebra_nhg_process_grp(nhg, &nhg_depends,
1147 nhg_ctx_get_grp(ctx), count)) {
1148 depends_decrement_free(&nhg_depends);
d3a35138 1149 nexthop_group_delete(&nhg);
fec211ad 1150 return -ENOENT;
1b366e63
SW
1151 }
1152
0885b1e3
SW
1153 if (!zebra_nhg_find(&nhe, id, nhg, &nhg_depends, vrf_id, afi,
1154 type))
38e40db1 1155 depends_decrement_free(&nhg_depends);
4505578b 1156
e22e8001 1157 /* These got copied over in zebra_nhg_alloc() */
d3a35138 1158 nexthop_group_delete(&nhg);
38e40db1 1159 } else
10200d40
SW
1160 nhe = zebra_nhg_find_nexthop(id, nhg_ctx_get_nh(ctx), afi,
1161 type);
e22e8001 1162
5b27c09d 1163 if (!nhe) {
e22e8001
SW
1164 flog_err(
1165 EC_ZEBRA_TABLE_LOOKUP_FAILED,
1166 "Zebra failed to find or create a nexthop hash entry for ID (%u)",
10200d40 1167 id);
e22e8001
SW
1168 return -1;
1169 }
1170
5b27c09d
SW
1171 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1172 zlog_debug("%s: nhe %p (%u) is new", __func__, nhe, nhe->id);
1173
1174 SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID);
1175 SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
1176
e22e8001
SW
1177 return 0;
1178}
1179
9a1588c4
SW
1180static int nhg_ctx_process_del(struct nhg_ctx *ctx)
1181{
1182 struct nhg_hash_entry *nhe = NULL;
10200d40 1183 uint32_t id = nhg_ctx_get_id(ctx);
9a1588c4 1184
10200d40 1185 nhe = zebra_nhg_lookup_id(id);
9a1588c4
SW
1186
1187 if (!nhe) {
1188 flog_warn(
1189 EC_ZEBRA_BAD_NHG_MESSAGE,
1190 "Kernel delete message received for nexthop group ID (%u) that we do not have in our ID table",
10200d40 1191 id);
81505946 1192 return -1;
9a1588c4
SW
1193 }
1194
1195 zebra_nhg_handle_kernel_state_change(nhe, true);
1196
1197 return 0;
1198}
1199
7c6d5f25 1200static void nhg_ctx_fini(struct nhg_ctx **ctx)
e22e8001
SW
1201{
1202 /*
1203 * Just freeing for now, maybe do something more in the future
1204 * based on flag.
1205 */
1206
7134ba70 1207 nhg_ctx_free(ctx);
e22e8001
SW
1208}
1209
1b366e63
SW
1210static int queue_add(struct nhg_ctx *ctx)
1211{
1212 /* If its queued or already processed do nothing */
1213 if (nhg_ctx_get_status(ctx) == NHG_CTX_QUEUED)
1214 return 0;
1215
1216 if (rib_queue_nhg_add(ctx)) {
1217 nhg_ctx_set_status(ctx, NHG_CTX_FAILURE);
1218 return -1;
1219 }
1220
1221 nhg_ctx_set_status(ctx, NHG_CTX_QUEUED);
1222
1223 return 0;
1224}
1225
e22e8001
SW
1226int nhg_ctx_process(struct nhg_ctx *ctx)
1227{
1228 int ret = 0;
1229
1230 switch (nhg_ctx_get_op(ctx)) {
1231 case NHG_CTX_OP_NEW:
1232 ret = nhg_ctx_process_new(ctx);
fec211ad 1233 if (nhg_ctx_get_count(ctx) && ret == -ENOENT
1b366e63 1234 && nhg_ctx_get_status(ctx) != NHG_CTX_REQUEUED) {
e1292378
SW
1235 /**
1236 * We have entered a situation where we are
1237 * processing a group from the kernel
1238 * that has a contained nexthop which
1239 * we have not yet processed.
1b366e63 1240 *
e1292378
SW
1241 * Re-enqueue this ctx to be handled exactly one
1242 * more time (indicated by the flag).
1243 *
1244 * By the time we get back to it, we
1245 * should have processed its depends.
1b366e63
SW
1246 */
1247 nhg_ctx_set_status(ctx, NHG_CTX_NONE);
1248 if (queue_add(ctx) == 0) {
1249 nhg_ctx_set_status(ctx, NHG_CTX_REQUEUED);
1250 return 0;
1251 }
1252 }
e22e8001
SW
1253 break;
1254 case NHG_CTX_OP_DEL:
9a1588c4 1255 ret = nhg_ctx_process_del(ctx);
e22e8001
SW
1256 case NHG_CTX_OP_NONE:
1257 break;
1258 }
1259
1260 nhg_ctx_set_status(ctx, (ret ? NHG_CTX_FAILURE : NHG_CTX_SUCCESS));
1261
7c6d5f25 1262 nhg_ctx_fini(&ctx);
e22e8001
SW
1263
1264 return ret;
1265}
3057df51 1266
e22e8001
SW
1267/* Kernel-side, you either get a single new nexthop or a array of ID's */
1268int zebra_nhg_kernel_find(uint32_t id, struct nexthop *nh, struct nh_grp *grp,
38e40db1
SW
1269 uint8_t count, vrf_id_t vrf_id, afi_t afi, int type,
1270 int startup)
e22e8001 1271{
e22e8001
SW
1272 struct nhg_ctx *ctx = NULL;
1273
377e29f7
MS
1274 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1275 zlog_debug("%s: nh %pNHv, id %u, count %d",
1276 __func__, nh, id, (int)count);
1277
54c89c93 1278 if (id > id_counter && id < ZEBRA_NHG_PROTO_LOWER)
38e40db1
SW
1279 /* Increase our counter so we don't try to create
1280 * an ID that already exists
1281 */
1282 id_counter = id;
1283
81505946 1284 ctx = nhg_ctx_init(id, nh, grp, vrf_id, afi, type, count);
e22e8001
SW
1285 nhg_ctx_set_op(ctx, NHG_CTX_OP_NEW);
1286
38e40db1
SW
1287 /* Under statup conditions, we need to handle them immediately
1288 * like we do for routes. Otherwise, we are going to get a route
1289 * with a nhe_id that we have not handled.
1290 */
1291 if (startup)
1292 return nhg_ctx_process(ctx);
1293
e22e8001 1294 if (queue_add(ctx)) {
7c6d5f25 1295 nhg_ctx_fini(&ctx);
e22e8001
SW
1296 return -1;
1297 }
1298
1299 return 0;
1300}
1301
9a1588c4 1302/* Kernel-side, received delete message */
88cafda7 1303int zebra_nhg_kernel_del(uint32_t id, vrf_id_t vrf_id)
9a1588c4
SW
1304{
1305 struct nhg_ctx *ctx = NULL;
1306
88cafda7 1307 ctx = nhg_ctx_init(id, NULL, NULL, vrf_id, 0, 0, 0);
9a1588c4
SW
1308
1309 nhg_ctx_set_op(ctx, NHG_CTX_OP_DEL);
1310
1311 if (queue_add(ctx)) {
7c6d5f25 1312 nhg_ctx_fini(&ctx);
9a1588c4
SW
1313 return -1;
1314 }
1315
1316 return 0;
1317}
1318
5657e7e9 1319/* Some dependency helper functions */
0fff714e 1320static struct nhg_hash_entry *depends_find_recursive(const struct nexthop *nh,
0885b1e3 1321 afi_t afi, int type)
98cda54a 1322{
0fff714e
SW
1323 struct nhg_hash_entry *nhe;
1324 struct nexthop *lookup = NULL;
98cda54a 1325
77bf9504 1326 lookup = nexthop_dup(nh, NULL);
0fff714e 1327
0885b1e3 1328 nhe = zebra_nhg_find_nexthop(0, lookup, afi, type);
0fff714e
SW
1329
1330 nexthops_free(lookup);
1331
1332 return nhe;
1333}
1334
1335static struct nhg_hash_entry *depends_find_singleton(const struct nexthop *nh,
0885b1e3 1336 afi_t afi, int type)
0fff714e
SW
1337{
1338 struct nhg_hash_entry *nhe;
1339 struct nexthop lookup = {};
606fa9e5 1340
cb86eba3
MS
1341 /* Capture a snapshot of this single nh; it might be part of a list,
1342 * so we need to make a standalone copy.
1343 */
77bf9504 1344 nexthop_copy_no_recurse(&lookup, nh, NULL);
8a507796 1345
0885b1e3 1346 nhe = zebra_nhg_find_nexthop(0, &lookup, afi, type);
8a507796 1347
cb86eba3
MS
1348 /* The copy may have allocated labels; free them if necessary. */
1349 nexthop_del_labels(&lookup);
4505578b 1350
377e29f7
MS
1351 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1352 zlog_debug("%s: nh %pNHv => %p (%u)",
1353 __func__, nh, nhe, nhe ? nhe->id : 0);
1354
0fff714e
SW
1355 return nhe;
1356}
1357
0885b1e3
SW
1358static struct nhg_hash_entry *depends_find(const struct nexthop *nh, afi_t afi,
1359 int type)
0fff714e
SW
1360{
1361 struct nhg_hash_entry *nhe = NULL;
1362
1363 if (!nh)
1364 goto done;
1365
1366 /* We are separating these functions out to increase handling speed
1367 * in the non-recursive case (by not alloc/freeing)
1368 */
bed74d17 1369 if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE))
0885b1e3 1370 nhe = depends_find_recursive(nh, afi, type);
bed74d17 1371 else
0885b1e3 1372 nhe = depends_find_singleton(nh, afi, type);
377e29f7 1373
bed74d17
DS
1374
1375 if (IS_ZEBRA_DEBUG_NHG_DETAIL) {
1376 zlog_debug("%s: nh %pNHv %s => %p (%u)", __func__, nh,
1377 CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE) ? "(R)"
1378 : "",
377e29f7 1379 nhe, nhe ? nhe->id : 0);
bed74d17 1380 }
0fff714e 1381
606fa9e5 1382done:
4505578b 1383 return nhe;
98cda54a
SW
1384}
1385
37c6708b 1386static void depends_add(struct nhg_connected_tree_head *head,
5657e7e9
SW
1387 struct nhg_hash_entry *depend)
1388{
377e29f7
MS
1389 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1390 zlog_debug("%s: head %p nh %pNHv",
1391 __func__, head, depend->nhg.nexthop);
1392
5bf15faa
SW
1393 /* If NULL is returned, it was successfully added and
1394 * needs to have its refcnt incremented.
1395 *
1396 * Else the NHE is already present in the tree and doesn't
1397 * need to increment the refcnt.
1398 */
1399 if (nhg_connected_tree_add_nhe(head, depend) == NULL)
1400 zebra_nhg_increment_ref(depend);
5657e7e9
SW
1401}
1402
38e40db1
SW
1403static struct nhg_hash_entry *
1404depends_find_add(struct nhg_connected_tree_head *head, struct nexthop *nh,
0885b1e3 1405 afi_t afi, int type)
5657e7e9
SW
1406{
1407 struct nhg_hash_entry *depend = NULL;
1408
0885b1e3 1409 depend = depends_find(nh, afi, type);
1b366e63 1410
1d48702e
MS
1411 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1412 zlog_debug("%s: nh %pNHv => %p",
1413 __func__, nh, depend);
1414
1b366e63
SW
1415 if (depend)
1416 depends_add(head, depend);
38e40db1
SW
1417
1418 return depend;
1419}
1420
1421static struct nhg_hash_entry *
1422depends_find_id_add(struct nhg_connected_tree_head *head, uint32_t id)
1423{
1424 struct nhg_hash_entry *depend = NULL;
1425
1426 depend = zebra_nhg_lookup_id(id);
1b366e63
SW
1427
1428 if (depend)
1429 depends_add(head, depend);
38e40db1
SW
1430
1431 return depend;
5657e7e9
SW
1432}
1433
37c6708b 1434static void depends_decrement_free(struct nhg_connected_tree_head *head)
5657e7e9 1435{
37c6708b
SW
1436 nhg_connected_tree_decrement_ref(head);
1437 nhg_connected_tree_free(head);
5657e7e9
SW
1438}
1439
377e29f7 1440/* Find an nhe based on a list of nexthops */
0885b1e3
SW
1441struct nhg_hash_entry *zebra_nhg_rib_find(uint32_t id,
1442 struct nexthop_group *nhg,
1443 afi_t rt_afi, int type)
e22e8001
SW
1444{
1445 struct nhg_hash_entry *nhe = NULL;
88cafda7
DS
1446 vrf_id_t vrf_id;
1447
1448 /*
1449 * CLANG SA is complaining that nexthop may be NULL
1450 * Make it happy but this is ridonc
1451 */
1452 assert(nhg->nexthop);
1453 vrf_id = !vrf_is_backend_netns() ? VRF_DEFAULT : nhg->nexthop->vrf_id;
98cda54a 1454
0885b1e3 1455 zebra_nhg_find(&nhe, id, nhg, NULL, vrf_id, rt_afi, type);
4505578b 1456
377e29f7
MS
1457 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1458 zlog_debug("%s: => nhe %p (%u)",
1459 __func__, nhe, nhe ? nhe->id : 0);
1460
1461 return nhe;
1462}
1463
1464/* Find an nhe based on a route's nhe */
1465struct nhg_hash_entry *
1466zebra_nhg_rib_find_nhe(struct nhg_hash_entry *rt_nhe, afi_t rt_afi)
1467{
1468 struct nhg_hash_entry *nhe = NULL;
1469
1470 if (!(rt_nhe && rt_nhe->nhg.nexthop)) {
1471 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED,
1472 "No nexthop passed to %s", __func__);
1473 return NULL;
1474 }
1475
1476 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
f924db49 1477 zlog_debug("%s: rt_nhe %p (%u)", __func__, rt_nhe, rt_nhe->id);
377e29f7
MS
1478
1479 zebra_nhe_find(&nhe, rt_nhe, NULL, rt_afi);
1480
1481 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1482 zlog_debug("%s: => nhe %p (%u)",
1483 __func__, nhe, nhe ? nhe->id : 0);
1484
3057df51
SW
1485 return nhe;
1486}
1487
1d48702e
MS
1488/*
1489 * Allocate backup nexthop info object. Typically these are embedded in
1490 * nhg_hash_entry objects.
1491 */
1492struct nhg_backup_info *zebra_nhg_backup_alloc(void)
1493{
1494 struct nhg_backup_info *p;
1495
1496 p = XCALLOC(MTYPE_NHG, sizeof(struct nhg_backup_info));
1497
1498 p->nhe = zebra_nhg_alloc();
1499
1500 /* Identify the embedded group used to hold the list of backups */
1501 SET_FLAG(p->nhe->flags, NEXTHOP_GROUP_BACKUP);
1502
1503 return p;
1504}
1505
1506/*
1507 * Free backup nexthop info object, deal with any embedded allocations
1508 */
1509void zebra_nhg_backup_free(struct nhg_backup_info **p)
1510{
1511 if (p && *p) {
1512 if ((*p)->nhe)
1513 zebra_nhg_free((*p)->nhe);
1514
1515 XFREE(MTYPE_NHG, (*p));
1516 }
1517}
1518
1d48702e
MS
1519/* Accessor for backup nexthop group */
1520struct nexthop_group *zebra_nhg_get_backup_nhg(struct nhg_hash_entry *nhe)
1521{
1522 struct nexthop_group *p = NULL;
1523
1524 if (nhe) {
1525 if (nhe->backup_info && nhe->backup_info->nhe)
1526 p = &(nhe->backup_info->nhe->nhg);
1527 }
1528
1529 return p;
1530}
1531
1532/*
1533 * Helper to return a copy of a backup_info - note that this is a shallow
1534 * copy, meant to be used when creating a new nhe from info passed in with
1535 * a route e.g.
1536 */
1537static struct nhg_backup_info *
1538nhg_backup_copy(const struct nhg_backup_info *orig)
1539{
1540 struct nhg_backup_info *b;
1541
1542 b = zebra_nhg_backup_alloc();
1543
1544 /* Copy list of nexthops */
1545 nexthop_group_copy(&(b->nhe->nhg), &(orig->nhe->nhg));
1546
1547 return b;
1548}
1549
5948f013 1550static void zebra_nhg_free_members(struct nhg_hash_entry *nhe)
b599cd2a 1551{
c415d895
MS
1552 nexthops_free(nhe->nhg.nexthop);
1553
1d48702e
MS
1554 zebra_nhg_backup_free(&nhe->backup_info);
1555
58396544 1556 /* Decrement to remove connection ref */
37c6708b
SW
1557 nhg_connected_tree_decrement_ref(&nhe->nhg_depends);
1558 nhg_connected_tree_free(&nhe->nhg_depends);
1559 nhg_connected_tree_free(&nhe->nhg_dependents);
b599cd2a
SW
1560}
1561
0eb97b86 1562void zebra_nhg_free(struct nhg_hash_entry *nhe)
a95b8020 1563{
377e29f7
MS
1564 if (IS_ZEBRA_DEBUG_NHG_DETAIL) {
1565 /* Group or singleton? */
1566 if (nhe->nhg.nexthop && nhe->nhg.nexthop->next)
1567 zlog_debug("%s: nhe %p (%u), refcnt %d",
f924db49 1568 __func__, nhe, nhe->id, nhe->refcnt);
377e29f7
MS
1569 else
1570 zlog_debug("%s: nhe %p (%u), refcnt %d, NH %pNHv",
f924db49 1571 __func__, nhe, nhe->id, nhe->refcnt,
377e29f7
MS
1572 nhe->nhg.nexthop);
1573 }
1574
38e40db1
SW
1575 if (nhe->refcnt)
1576 zlog_debug("nhe_id=%u hash refcnt=%d", nhe->id, nhe->refcnt);
1577
8e401b25 1578 zebra_nhg_free_members(nhe);
51d80884
SW
1579
1580 XFREE(MTYPE_NHG, nhe);
a95b8020
SW
1581}
1582
0eb97b86
MS
1583void zebra_nhg_hash_free(void *p)
1584{
1585 zebra_nhg_free((struct nhg_hash_entry *)p);
1586}
1587
d9f5b2f5
SW
1588void zebra_nhg_decrement_ref(struct nhg_hash_entry *nhe)
1589{
377e29f7
MS
1590 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1591 zlog_debug("%s: nhe %p (%u) %d => %d",
1592 __func__, nhe, nhe->id, nhe->refcnt,
1593 nhe->refcnt - 1);
1594
e22e8001
SW
1595 nhe->refcnt--;
1596
32e29e79 1597 if (!zebra_nhg_depends_is_empty(nhe))
37c6708b 1598 nhg_connected_tree_decrement_ref(&nhe->nhg_depends);
f54ef6a5 1599
38e40db1 1600 if (ZEBRA_NHG_CREATED(nhe) && nhe->refcnt <= 0)
cb50cbc9 1601 zebra_nhg_uninstall_kernel(nhe);
7fd392cc
SW
1602}
1603
7fd392cc
SW
1604void zebra_nhg_increment_ref(struct nhg_hash_entry *nhe)
1605{
377e29f7
MS
1606 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1607 zlog_debug("%s: nhe %p (%u) %d => %d",
1608 __func__, nhe, nhe->id, nhe->refcnt,
1609 nhe->refcnt + 1);
1610
e22e8001
SW
1611 nhe->refcnt++;
1612
32e29e79 1613 if (!zebra_nhg_depends_is_empty(nhe))
37c6708b 1614 nhg_connected_tree_increment_ref(&nhe->nhg_depends);
e22e8001 1615}
d9f5b2f5 1616
ad28e79a 1617static void nexthop_set_resolved(afi_t afi, const struct nexthop *newhop,
31f937fb
SM
1618 struct nexthop *nexthop,
1619 struct zebra_sr_policy *policy)
ad28e79a
SW
1620{
1621 struct nexthop *resolved_hop;
b43434ad
SW
1622 uint8_t num_labels = 0;
1623 mpls_label_t labels[MPLS_MAX_LABELS];
1624 enum lsp_types_t label_type = ZEBRA_LSP_NONE;
1625 int i = 0;
ad28e79a
SW
1626
1627 resolved_hop = nexthop_new();
1628 SET_FLAG(resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
1629
1630 resolved_hop->vrf_id = nexthop->vrf_id;
1631 switch (newhop->type) {
1632 case NEXTHOP_TYPE_IPV4:
1633 case NEXTHOP_TYPE_IPV4_IFINDEX:
1634 /* If the resolving route specifies a gateway, use it */
1635 resolved_hop->type = newhop->type;
1636 resolved_hop->gate.ipv4 = newhop->gate.ipv4;
1637
1638 if (newhop->ifindex) {
1639 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
1640 resolved_hop->ifindex = newhop->ifindex;
1641 }
1642 break;
1643 case NEXTHOP_TYPE_IPV6:
1644 case NEXTHOP_TYPE_IPV6_IFINDEX:
1645 resolved_hop->type = newhop->type;
1646 resolved_hop->gate.ipv6 = newhop->gate.ipv6;
1647
1648 if (newhop->ifindex) {
1649 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1650 resolved_hop->ifindex = newhop->ifindex;
1651 }
1652 break;
1653 case NEXTHOP_TYPE_IFINDEX:
1654 /* If the resolving route is an interface route,
1655 * it means the gateway we are looking up is connected
1656 * to that interface. (The actual network is _not_ onlink).
1657 * Therefore, the resolved route should have the original
1658 * gateway as nexthop as it is directly connected.
1659 *
1660 * On Linux, we have to set the onlink netlink flag because
1661 * otherwise, the kernel won't accept the route.
1662 */
1663 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
1664 if (afi == AFI_IP) {
1665 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
1666 resolved_hop->gate.ipv4 = nexthop->gate.ipv4;
1667 } else if (afi == AFI_IP6) {
1668 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1669 resolved_hop->gate.ipv6 = nexthop->gate.ipv6;
1670 }
1671 resolved_hop->ifindex = newhop->ifindex;
1672 break;
1673 case NEXTHOP_TYPE_BLACKHOLE:
1674 resolved_hop->type = NEXTHOP_TYPE_BLACKHOLE;
2dc359a6 1675 resolved_hop->bh_type = newhop->bh_type;
ad28e79a
SW
1676 break;
1677 }
1678
1679 if (newhop->flags & NEXTHOP_FLAG_ONLINK)
1680 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
1681
b43434ad 1682 /* Copy labels of the resolved route and the parent resolving to it */
31f937fb
SM
1683 if (policy) {
1684 int i = 0;
1685
1686 /*
1687 * Don't push the first SID if the corresponding action in the
1688 * LFIB is POP.
1689 */
1690 if (!newhop->nh_label || !newhop->nh_label->num_labels
1691 || newhop->nh_label->label[0] == MPLS_LABEL_IMPLICIT_NULL)
1692 i = 1;
1693
1694 for (; i < policy->segment_list.label_num; i++)
1695 labels[num_labels++] = policy->segment_list.labels[i];
1696 label_type = policy->segment_list.type;
1697 } else if (newhop->nh_label) {
6bc5d977
MS
1698 for (i = 0; i < newhop->nh_label->num_labels; i++) {
1699 /* Be a bit picky about overrunning the local array */
1700 if (num_labels >= MPLS_MAX_LABELS) {
1701 if (IS_ZEBRA_DEBUG_NHG || IS_ZEBRA_DEBUG_RIB)
1702 zlog_debug("%s: too many labels in newhop %pNHv",
1703 __func__, newhop);
1704 break;
1705 }
b43434ad 1706 labels[num_labels++] = newhop->nh_label->label[i];
6bc5d977
MS
1707 }
1708 /* Use the "outer" type */
b43434ad
SW
1709 label_type = newhop->nh_label_type;
1710 }
1711
1712 if (nexthop->nh_label) {
6bc5d977
MS
1713 for (i = 0; i < nexthop->nh_label->num_labels; i++) {
1714 /* Be a bit picky about overrunning the local array */
1715 if (num_labels >= MPLS_MAX_LABELS) {
1716 if (IS_ZEBRA_DEBUG_NHG || IS_ZEBRA_DEBUG_RIB)
1717 zlog_debug("%s: too many labels in nexthop %pNHv",
1718 __func__, nexthop);
1719 break;
1720 }
b43434ad 1721 labels[num_labels++] = nexthop->nh_label->label[i];
6bc5d977 1722 }
b43434ad 1723
6bc5d977
MS
1724 /* If the parent has labels, use its type if
1725 * we don't already have one.
1726 */
1727 if (label_type == ZEBRA_LSP_NONE)
1728 label_type = nexthop->nh_label_type;
b43434ad
SW
1729 }
1730
1731 if (num_labels)
1732 nexthop_add_labels(resolved_hop, label_type, num_labels,
1733 labels);
ad28e79a
SW
1734
1735 resolved_hop->rparent = nexthop;
50d89650 1736 _nexthop_add(&nexthop->resolved, resolved_hop);
ad28e79a
SW
1737}
1738
6913cb1b
SW
1739/* Checks if nexthop we are trying to resolve to is valid */
1740static bool nexthop_valid_resolve(const struct nexthop *nexthop,
1741 const struct nexthop *resolved)
1742{
1743 /* Can't resolve to a recursive nexthop */
1744 if (CHECK_FLAG(resolved->flags, NEXTHOP_FLAG_RECURSIVE))
1745 return false;
1746
9d43854d
MS
1747 /* Must be ACTIVE */
1748 if (!CHECK_FLAG(resolved->flags, NEXTHOP_FLAG_ACTIVE))
1749 return false;
1750
6913cb1b
SW
1751 switch (nexthop->type) {
1752 case NEXTHOP_TYPE_IPV4_IFINDEX:
1753 case NEXTHOP_TYPE_IPV6_IFINDEX:
1754 /* If the nexthop we are resolving to does not match the
1755 * ifindex for the nexthop the route wanted, its not valid.
1756 */
1757 if (nexthop->ifindex != resolved->ifindex)
1758 return false;
1759 break;
1760 case NEXTHOP_TYPE_IPV4:
1761 case NEXTHOP_TYPE_IPV6:
1762 case NEXTHOP_TYPE_IFINDEX:
1763 case NEXTHOP_TYPE_BLACKHOLE:
1764 break;
1765 }
1766
1767 return true;
1768}
1769
ad28e79a
SW
1770/*
1771 * Given a nexthop we need to properly recursively resolve
1772 * the route. As such, do a table lookup to find and match
98cda54a
SW
1773 * if at all possible. Set the nexthop->ifindex and resolved_id
1774 * as appropriate
ad28e79a
SW
1775 */
1776static int nexthop_active(afi_t afi, struct route_entry *re,
8a507796 1777 struct nexthop *nexthop, struct route_node *top)
ad28e79a
SW
1778{
1779 struct prefix p;
1780 struct route_table *table;
1781 struct route_node *rn;
1782 struct route_entry *match = NULL;
1783 int resolved;
31f937fb 1784 zebra_nhlfe_t *nhlfe;
ad28e79a
SW
1785 struct nexthop *newhop;
1786 struct interface *ifp;
1787 rib_dest_t *dest;
5a0bdc78 1788 struct zebra_vrf *zvrf;
31f937fb
SM
1789 struct in_addr local_ipv4;
1790 struct in_addr *ipv4;
ad28e79a
SW
1791
1792 if ((nexthop->type == NEXTHOP_TYPE_IPV4)
1793 || nexthop->type == NEXTHOP_TYPE_IPV6)
1794 nexthop->ifindex = 0;
1795
8a507796 1796
ad28e79a
SW
1797 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
1798 nexthops_free(nexthop->resolved);
1799 nexthop->resolved = NULL;
1800 re->nexthop_mtu = 0;
1801
377e29f7
MS
1802 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1803 zlog_debug("%s: re %p, nexthop %pNHv",
1804 __func__, re, nexthop);
1805
ad28e79a 1806 /*
a8c427ee 1807 * If the kernel has sent us a NEW route, then
ad28e79a 1808 * by golly gee whiz it's a good route.
a8c427ee
SW
1809 *
1810 * If its an already INSTALLED route we have already handled, then the
1811 * kernel route's nexthop might have became unreachable
1812 * and we have to handle that.
ad28e79a 1813 */
a8c427ee
SW
1814 if (!CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED)
1815 && (re->type == ZEBRA_ROUTE_KERNEL
1816 || re->type == ZEBRA_ROUTE_SYSTEM))
ad28e79a
SW
1817 return 1;
1818
1819 /*
12b4d77b 1820 * If the nexthop has been marked as 'onlink' we just need to make
1821 * sure the nexthop's interface is known and is operational.
ad28e79a
SW
1822 */
1823 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)) {
1824 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
1825 if (!ifp) {
12b4d77b 1826 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1827 zlog_debug("nexthop %pNHv marked onlink but nhif %u doesn't exist",
1828 nexthop, nexthop->ifindex);
ad28e79a
SW
1829 return 0;
1830 }
12b4d77b 1831 if (!if_is_operative(ifp)) {
1832 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1833 zlog_debug("nexthop %pNHv marked onlink but nhif %s is not operational",
1834 nexthop, ifp->name);
2d3c57e6 1835 return 0;
ad28e79a 1836 }
12b4d77b 1837 return 1;
ad28e79a
SW
1838 }
1839
4dcc2276
DS
1840 if ((top->p.family == AF_INET && top->p.prefixlen == 32
1841 && nexthop->gate.ipv4.s_addr == top->p.u.prefix4.s_addr)
1842 || (top->p.family == AF_INET6 && top->p.prefixlen == 128
1843 && memcmp(&nexthop->gate.ipv6, &top->p.u.prefix6, 16) == 0)) {
1844 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1845 zlog_debug(
d6951e5e 1846 " :%s: Attempting to install a max prefixlength route through itself",
5e81f5dd 1847 __func__);
4dcc2276
DS
1848 return 0;
1849 }
1850
92d6f769
K
1851 /* Validation for ipv4 mapped ipv6 nexthop. */
1852 if (IS_MAPPED_IPV6(&nexthop->gate.ipv6)) {
1853 afi = AFI_IP;
31f937fb
SM
1854 ipv4 = &local_ipv4;
1855 ipv4_mapped_ipv6_to_ipv4(&nexthop->gate.ipv6, ipv4);
1856 } else {
1857 ipv4 = &nexthop->gate.ipv4;
1858 }
1859
1860 if (nexthop->srte_color) {
1861 struct ipaddr endpoint = {0};
1862 struct zebra_sr_policy *policy;
1863
1864 switch (afi) {
1865 case AFI_IP:
1866 endpoint.ipa_type = IPADDR_V4;
1867 endpoint.ipaddr_v4 = *ipv4;
1868 break;
1869 case AFI_IP6:
1870 endpoint.ipa_type = IPADDR_V6;
1871 endpoint.ipaddr_v6 = nexthop->gate.ipv6;
1872 break;
1873 default:
1874 flog_err(EC_LIB_DEVELOPMENT,
1875 "%s: unknown address-family: %u", __func__,
1876 afi);
1877 exit(1);
1878 }
1879
1880 policy = zebra_sr_policy_find(nexthop->srte_color, &endpoint);
1881 if (policy && policy->status == ZEBRA_SR_POLICY_UP) {
1882 resolved = 0;
1883 frr_each_safe (nhlfe_list, &policy->lsp->nhlfe_list,
1884 nhlfe) {
1885 if (!CHECK_FLAG(nhlfe->flags,
1886 NHLFE_FLAG_SELECTED)
1887 || CHECK_FLAG(nhlfe->flags,
1888 NHLFE_FLAG_DELETED))
1889 continue;
1890 SET_FLAG(nexthop->flags,
1891 NEXTHOP_FLAG_RECURSIVE);
1892 nexthop_set_resolved(afi, nhlfe->nexthop,
1893 nexthop, policy);
1894 resolved = 1;
1895 }
1896 if (resolved)
1897 return 1;
1898 }
92d6f769
K
1899 }
1900
ad28e79a
SW
1901 /* Make lookup prefix. */
1902 memset(&p, 0, sizeof(struct prefix));
1903 switch (afi) {
1904 case AFI_IP:
1905 p.family = AF_INET;
1906 p.prefixlen = IPV4_MAX_PREFIXLEN;
31f937fb 1907 p.u.prefix4 = *ipv4;
ad28e79a
SW
1908 break;
1909 case AFI_IP6:
1910 p.family = AF_INET6;
1911 p.prefixlen = IPV6_MAX_PREFIXLEN;
1912 p.u.prefix6 = nexthop->gate.ipv6;
1913 break;
1914 default:
1915 assert(afi != AFI_IP && afi != AFI_IP6);
1916 break;
1917 }
1918 /* Lookup table. */
1919 table = zebra_vrf_table(afi, SAFI_UNICAST, nexthop->vrf_id);
5a0bdc78
PG
1920 /* get zvrf */
1921 zvrf = zebra_vrf_lookup_by_id(nexthop->vrf_id);
1922 if (!table || !zvrf) {
ad28e79a 1923 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
d6951e5e 1924 zlog_debug(" %s: Table not found", __func__);
ad28e79a
SW
1925 return 0;
1926 }
1927
1928 rn = route_node_match(table, (struct prefix *)&p);
1929 while (rn) {
1930 route_unlock_node(rn);
1931
1932 /* Lookup should halt if we've matched against ourselves ('top',
1933 * if specified) - i.e., we cannot have a nexthop NH1 is
1934 * resolved by a route NH1. The exception is if the route is a
1935 * host route.
1936 */
92756825 1937 if (rn == top)
ad28e79a
SW
1938 if (((afi == AFI_IP) && (rn->p.prefixlen != 32))
1939 || ((afi == AFI_IP6) && (rn->p.prefixlen != 128))) {
1940 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1941 zlog_debug(
d6951e5e 1942 " %s: Matched against ourself and prefix length is not max bit length",
5e81f5dd 1943 __func__);
ad28e79a
SW
1944 return 0;
1945 }
1946
1947 /* Pick up selected route. */
1948 /* However, do not resolve over default route unless explicitly
2d3c57e6
SW
1949 * allowed.
1950 */
ad28e79a 1951 if (is_default_prefix(&rn->p)
5a0bdc78 1952 && !rnh_resolve_via_default(zvrf, p.family)) {
ad28e79a
SW
1953 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1954 zlog_debug(
d6951e5e 1955 " :%s: Resolved against default route",
5e81f5dd 1956 __func__);
ad28e79a
SW
1957 return 0;
1958 }
1959
1960 dest = rib_dest_from_rnode(rn);
1961 if (dest && dest->selected_fib
1962 && !CHECK_FLAG(dest->selected_fib->status,
1963 ROUTE_ENTRY_REMOVED)
1964 && dest->selected_fib->type != ZEBRA_ROUTE_TABLE)
1965 match = dest->selected_fib;
1966
1967 /* If there is no selected route or matched route is EGP, go up
2d3c57e6
SW
1968 * tree.
1969 */
ad28e79a
SW
1970 if (!match) {
1971 do {
1972 rn = rn->parent;
1973 } while (rn && rn->info == NULL);
1974 if (rn)
1975 route_lock_node(rn);
1976
1977 continue;
1978 }
1979
1980 if (match->type == ZEBRA_ROUTE_CONNECT) {
1981 /* Directly point connected route. */
c415d895 1982 newhop = match->nhe->nhg.nexthop;
ad28e79a
SW
1983 if (newhop) {
1984 if (nexthop->type == NEXTHOP_TYPE_IPV4
1985 || nexthop->type == NEXTHOP_TYPE_IPV6)
1986 nexthop->ifindex = newhop->ifindex;
c479909b
SW
1987 else if (nexthop->ifindex != newhop->ifindex) {
1988 /*
1989 * NEXTHOP_TYPE_*_IFINDEX but ifindex
1990 * doesn't match what we found.
1991 */
1992 return 0;
1993 }
ad28e79a 1994 }
377e29f7
MS
1995
1996 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1997 zlog_debug("%s: CONNECT match %p (%u), newhop %pNHv",
1998 __func__, match,
1999 match->nhe->id, newhop);
2000
ad28e79a
SW
2001 return 1;
2002 } else if (CHECK_FLAG(re->flags, ZEBRA_FLAG_ALLOW_RECURSION)) {
f2646720
MS
2003 struct nexthop_group *nhg;
2004
ad28e79a 2005 resolved = 0;
f2646720 2006
92ad0c55
MS
2007 /* Only useful if installed */
2008 if (!CHECK_FLAG(match->status, ROUTE_ENTRY_INSTALLED)) {
2009 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
2010 zlog_debug("%s: match %p (%u) not installed",
2011 __func__, match,
2012 match->nhe->id);
2013
2014 goto done_with_match;
2015 }
2016
3c0e1622
MS
2017 /* Examine installed nexthops; note that there
2018 * may not be any installed primary nexthops if
2019 * only backups are installed.
2020 */
2021 nhg = rib_get_fib_nhg(match);
f2646720 2022 for (ALL_NEXTHOPS_PTR(nhg, newhop)) {
6913cb1b 2023 if (!nexthop_valid_resolve(nexthop, newhop))
ad28e79a
SW
2024 continue;
2025
377e29f7
MS
2026 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
2027 zlog_debug("%s: RECURSIVE match %p (%u), newhop %pNHv",
2028 __func__, match,
2029 match->nhe->id, newhop);
2030
ad28e79a
SW
2031 SET_FLAG(nexthop->flags,
2032 NEXTHOP_FLAG_RECURSIVE);
31f937fb
SM
2033 nexthop_set_resolved(afi, newhop, nexthop,
2034 NULL);
ad28e79a
SW
2035 resolved = 1;
2036 }
f924db49 2037
9959f1da
MS
2038 /* Examine installed backup nexthops, if any. There
2039 * are only installed backups *if* there is a
2040 * dedicated fib list.
2041 */
2042 nhg = rib_get_fib_backup_nhg(match);
3c0e1622 2043 if (nhg == NULL || nhg->nexthop == NULL)
f2646720
MS
2044 goto done_with_match;
2045
2046 for (ALL_NEXTHOPS_PTR(nhg, newhop)) {
f2646720
MS
2047 if (!nexthop_valid_resolve(nexthop, newhop))
2048 continue;
2049
2050 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
2051 zlog_debug("%s: RECURSIVE match backup %p (%u), newhop %pNHv",
2052 __func__, match,
2053 match->nhe->id, newhop);
2054
2055 SET_FLAG(nexthop->flags,
2056 NEXTHOP_FLAG_RECURSIVE);
31f937fb
SM
2057 nexthop_set_resolved(afi, newhop, nexthop,
2058 NULL);
f2646720
MS
2059 resolved = 1;
2060 }
2061done_with_match:
8a507796 2062 if (resolved)
ad28e79a 2063 re->nexthop_mtu = match->mtu;
f924db49 2064 else if (IS_ZEBRA_DEBUG_RIB_DETAILED)
d6951e5e
DL
2065 zlog_debug(
2066 " %s: Recursion failed to find",
2067 __func__);
f924db49 2068
ad28e79a 2069 return resolved;
ad28e79a
SW
2070 } else {
2071 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
2072 zlog_debug(
d6951e5e 2073 " %s: Route Type %s has not turned on recursion",
5e81f5dd 2074 __func__, zebra_route_string(re->type));
ad28e79a
SW
2075 if (re->type == ZEBRA_ROUTE_BGP
2076 && !CHECK_FLAG(re->flags, ZEBRA_FLAG_IBGP))
2077 zlog_debug(
d6951e5e 2078 " EBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\"");
ad28e79a
SW
2079 }
2080 return 0;
2081 }
2082 }
2083 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
d6951e5e
DL
2084 zlog_debug(" %s: Nexthop did not lookup in table",
2085 __func__);
ad28e79a
SW
2086 return 0;
2087}
2088
2089/* This function verifies reachability of one given nexthop, which can be
2090 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
2091 * in nexthop->flags field. The nexthop->ifindex will be updated
2092 * appropriately as well. An existing route map can turn
2093 * (otherwise active) nexthop into inactive, but not vice versa.
2094 *
98cda54a
SW
2095 * If it finds a nexthop recursivedly, set the resolved_id
2096 * to match that nexthop's nhg_hash_entry ID;
2097 *
ad28e79a
SW
2098 * The return value is the final value of 'ACTIVE' flag.
2099 */
2100static unsigned nexthop_active_check(struct route_node *rn,
2101 struct route_entry *re,
8a507796 2102 struct nexthop *nexthop)
ad28e79a
SW
2103{
2104 struct interface *ifp;
b68885f9 2105 route_map_result_t ret = RMAP_PERMITMATCH;
ad28e79a
SW
2106 int family;
2107 char buf[SRCDEST2STR_BUFFER];
2108 const struct prefix *p, *src_p;
2109 struct zebra_vrf *zvrf;
2110
2111 srcdest_rnode_prefixes(rn, &p, &src_p);
2112
2113 if (rn->p.family == AF_INET)
2114 family = AFI_IP;
2115 else if (rn->p.family == AF_INET6)
2116 family = AFI_IP6;
2117 else
2118 family = 0;
2119 switch (nexthop->type) {
2120 case NEXTHOP_TYPE_IFINDEX:
2121 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
fc8a02c4
SW
2122 /*
2123 * If the interface exists and its operative or its a kernel
2124 * route and interface is up, its active. We trust kernel routes
2125 * to be good.
2126 */
2127 if (ifp
2128 && (if_is_operative(ifp)
2129 || (if_is_up(ifp)
2130 && (re->type == ZEBRA_ROUTE_KERNEL
2131 || re->type == ZEBRA_ROUTE_SYSTEM))))
ad28e79a
SW
2132 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2133 else
2134 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2135 break;
2136 case NEXTHOP_TYPE_IPV4:
2137 case NEXTHOP_TYPE_IPV4_IFINDEX:
2138 family = AFI_IP;
8a507796 2139 if (nexthop_active(AFI_IP, re, nexthop, rn))
ad28e79a
SW
2140 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2141 else
2142 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2143 break;
2144 case NEXTHOP_TYPE_IPV6:
2145 family = AFI_IP6;
8a507796 2146 if (nexthop_active(AFI_IP6, re, nexthop, rn))
ad28e79a
SW
2147 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2148 else
2149 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2150 break;
2151 case NEXTHOP_TYPE_IPV6_IFINDEX:
2152 /* RFC 5549, v4 prefix with v6 NH */
2153 if (rn->p.family != AF_INET)
2154 family = AFI_IP6;
2155 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->gate.ipv6)) {
2156 ifp = if_lookup_by_index(nexthop->ifindex,
2157 nexthop->vrf_id);
2158 if (ifp && if_is_operative(ifp))
2159 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2160 else
2161 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2162 } else {
8a507796 2163 if (nexthop_active(AFI_IP6, re, nexthop, rn))
ad28e79a
SW
2164 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2165 else
2166 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2167 }
2168 break;
2169 case NEXTHOP_TYPE_BLACKHOLE:
2170 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2171 break;
2172 default:
2173 break;
2174 }
377e29f7 2175
ad28e79a
SW
2176 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) {
2177 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
377e29f7
MS
2178 zlog_debug(" %s: Unable to find active nexthop",
2179 __func__);
ad28e79a
SW
2180 return 0;
2181 }
2182
2183 /* XXX: What exactly do those checks do? Do we support
2184 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
2185 */
2186 if (RIB_SYSTEM_ROUTE(re) || (family == AFI_IP && p->family != AF_INET)
2187 || (family == AFI_IP6 && p->family != AF_INET6))
2188 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2189
2190 /* The original code didn't determine the family correctly
2191 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
2192 * from the rib_table_info in those cases.
2193 * Possibly it may be better to use only the rib_table_info
2194 * in every case.
2195 */
2196 if (!family) {
630d5962 2197 struct rib_table_info *info;
ad28e79a
SW
2198
2199 info = srcdest_rnode_table_info(rn);
2200 family = info->afi;
2201 }
2202
2203 memset(&nexthop->rmap_src.ipv6, 0, sizeof(union g_addr));
2204
2205 zvrf = zebra_vrf_lookup_by_id(nexthop->vrf_id);
2206 if (!zvrf) {
2207 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
d6951e5e 2208 zlog_debug(" %s: zvrf is NULL", __func__);
ad28e79a
SW
2209 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2210 }
2211
2212 /* It'll get set if required inside */
2213 ret = zebra_route_map_check(family, re->type, re->instance, p, nexthop,
2214 zvrf, re->tag);
2215 if (ret == RMAP_DENYMATCH) {
2216 if (IS_ZEBRA_DEBUG_RIB) {
2217 srcdest_rnode2str(rn, buf, sizeof(buf));
2218 zlog_debug(
2219 "%u:%s: Filtering out with NH out %s due to route map",
2220 re->vrf_id, buf,
2221 ifindex2ifname(nexthop->ifindex,
2222 nexthop->vrf_id));
2223 }
2224 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2225 }
2226 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2227}
2228
086e4e02
SW
2229/* Helper function called after resolution to walk nhg rb trees
2230 * and toggle the NEXTHOP_GROUP_VALID flag if the nexthop
2231 * is active on singleton NHEs.
2232 */
2233static bool zebra_nhg_set_valid_if_active(struct nhg_hash_entry *nhe)
2234{
2235 struct nhg_connected *rb_node_dep = NULL;
2236 bool valid = false;
2237
2238 if (!zebra_nhg_depends_is_empty(nhe)) {
2239 /* Is at least one depend valid? */
2240 frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) {
2241 if (zebra_nhg_set_valid_if_active(rb_node_dep->nhe))
2242 valid = true;
2243 }
2244
2245 goto done;
2246 }
2247
2248 /* should be fully resolved singleton at this point */
2249 if (CHECK_FLAG(nhe->nhg.nexthop->flags, NEXTHOP_FLAG_ACTIVE))
2250 valid = true;
2251
2252done:
2253 if (valid)
2254 SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID);
2255
2256 return valid;
2257}
2258
ad28e79a 2259/*
f2646720 2260 * Process a list of nexthops, given an nhg, determining
377e29f7 2261 * whether each one is ACTIVE/installable at this time.
ad28e79a 2262 */
377e29f7
MS
2263static uint32_t nexthop_list_active_update(struct route_node *rn,
2264 struct route_entry *re,
f2646720 2265 struct nexthop_group *nhg)
ad28e79a 2266{
ad28e79a
SW
2267 union g_addr prev_src;
2268 unsigned int prev_active, new_active;
2269 ifindex_t prev_index;
377e29f7 2270 uint32_t counter = 0;
f2646720
MS
2271 struct nexthop *nexthop;
2272
2273 nexthop = nhg->nexthop;
e22e8001 2274
377e29f7
MS
2275 /* Process nexthops one-by-one */
2276 for ( ; nexthop; nexthop = nexthop->next) {
ad28e79a 2277
ad28e79a 2278 /* No protocol daemon provides src and so we're skipping
377e29f7
MS
2279 * tracking it
2280 */
ad28e79a
SW
2281 prev_src = nexthop->rmap_src;
2282 prev_active = CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2283 prev_index = nexthop->ifindex;
2284 /*
2285 * We need to respect the multipath_num here
2286 * as that what we should be able to install from
377e29f7 2287 * a multipath perspective should not be a data plane
ad28e79a
SW
2288 * decision point.
2289 */
98cda54a 2290 new_active =
8a507796 2291 nexthop_active_check(rn, re, nexthop);
98cda54a 2292
377e29f7 2293 if (new_active && counter >= zrouter.multipath_num) {
4c55b5ff
SW
2294 struct nexthop *nh;
2295
2296 /* Set it and its resolved nexthop as inactive. */
2297 for (nh = nexthop; nh; nh = nh->resolved)
2298 UNSET_FLAG(nh->flags, NEXTHOP_FLAG_ACTIVE);
2299
ad28e79a
SW
2300 new_active = 0;
2301 }
9a0d4dd3 2302
df31a989 2303 if (new_active)
377e29f7 2304 counter++;
9a0d4dd3 2305
ad28e79a
SW
2306 /* Don't allow src setting on IPv6 addr for now */
2307 if (prev_active != new_active || prev_index != nexthop->ifindex
2308 || ((nexthop->type >= NEXTHOP_TYPE_IFINDEX
2309 && nexthop->type < NEXTHOP_TYPE_IPV6)
2310 && prev_src.ipv4.s_addr
2311 != nexthop->rmap_src.ipv4.s_addr)
2312 || ((nexthop->type >= NEXTHOP_TYPE_IPV6
2313 && nexthop->type < NEXTHOP_TYPE_BLACKHOLE)
2314 && !(IPV6_ADDR_SAME(&prev_src.ipv6,
2315 &nexthop->rmap_src.ipv6)))
8a507796 2316 || CHECK_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED))
ad28e79a 2317 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
ad28e79a
SW
2318 }
2319
377e29f7
MS
2320 return counter;
2321}
2322
2c41ef8c
SW
2323
2324static uint32_t proto_nhg_nexthop_active_update(struct nexthop_group *nhg)
2325{
2326 struct nexthop *nh;
2327 uint32_t curr_active = 0;
2328
2329 /* Assume all active for now */
2330
2331 for (nh = nhg->nexthop; nh; nh = nh->next) {
2332 SET_FLAG(nh->flags, NEXTHOP_FLAG_ACTIVE);
2333 curr_active++;
2334 }
2335
2336 return curr_active;
2337}
2338
377e29f7
MS
2339/*
2340 * Iterate over all nexthops of the given RIB entry and refresh their
2341 * ACTIVE flag. If any nexthop is found to toggle the ACTIVE flag,
2342 * the whole re structure is flagged with ROUTE_ENTRY_CHANGED.
2343 *
2344 * Return value is the new number of active nexthops.
2345 */
2346int nexthop_active_update(struct route_node *rn, struct route_entry *re)
2347{
2348 struct nhg_hash_entry *curr_nhe;
2349 uint32_t curr_active = 0, backup_active = 0;
2350
54c89c93 2351 if (re->nhe->id >= ZEBRA_NHG_PROTO_LOWER)
2c41ef8c
SW
2352 return proto_nhg_nexthop_active_update(&re->nhe->nhg);
2353
377e29f7
MS
2354 afi_t rt_afi = family2afi(rn->p.family);
2355
2356 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
2357
2358 /* Make a local copy of the existing nhe, so we don't work on/modify
2359 * the shared nhe.
2360 */
f727646a 2361 curr_nhe = zebra_nhe_copy(re->nhe, re->nhe->id);
377e29f7
MS
2362
2363 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
2364 zlog_debug("%s: re %p nhe %p (%u), curr_nhe %p",
2365 __func__, re, re->nhe, re->nhe->id,
2366 curr_nhe);
2367
2368 /* Clear the existing id, if any: this will avoid any confusion
2369 * if the id exists, and will also force the creation
2370 * of a new nhe reflecting the changes we may make in this local copy.
2371 */
2372 curr_nhe->id = 0;
2373
2374 /* Process nexthops */
f2646720 2375 curr_active = nexthop_list_active_update(rn, re, &curr_nhe->nhg);
377e29f7
MS
2376
2377 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
2378 zlog_debug("%s: re %p curr_active %u", __func__, re,
2379 curr_active);
2380
2381 /* If there are no backup nexthops, we are done */
2382 if (zebra_nhg_get_backup_nhg(curr_nhe) == NULL)
2383 goto backups_done;
2384
2385 backup_active = nexthop_list_active_update(
f2646720 2386 rn, re, zebra_nhg_get_backup_nhg(curr_nhe));
377e29f7
MS
2387
2388 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
2389 zlog_debug("%s: re %p backup_active %u", __func__, re,
2390 backup_active);
2391
2392backups_done:
2393
2394 /*
2395 * Ref or create an nhe that matches the current state of the
2396 * nexthop(s).
2397 */
8a507796 2398 if (CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)) {
98cda54a 2399 struct nhg_hash_entry *new_nhe = NULL;
98cda54a 2400
377e29f7
MS
2401 new_nhe = zebra_nhg_rib_find_nhe(curr_nhe, rt_afi);
2402
2403 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
2404 zlog_debug("%s: re %p CHANGED: nhe %p (%u) => new_nhe %p (%u)",
2405 __func__, re, re->nhe,
2406 re->nhe->id, new_nhe, new_nhe->id);
98cda54a 2407
5463ce26 2408 route_entry_update_nhe(re, new_nhe);
e22e8001
SW
2409 }
2410
377e29f7 2411
086e4e02
SW
2412 /* Walk the NHE depends tree and toggle NEXTHOP_GROUP_VALID
2413 * flag where appropriate.
2414 */
715e5c70 2415 if (curr_active)
086e4e02 2416 zebra_nhg_set_valid_if_active(re->nhe);
98cda54a
SW
2417
2418 /*
377e29f7
MS
2419 * Do not need the old / copied nhe anymore since it
2420 * was either copied over into a new nhe or not
98cda54a
SW
2421 * used at all.
2422 */
377e29f7 2423 zebra_nhg_free(curr_nhe);
9a0d4dd3 2424 return curr_active;
ad28e79a 2425}
5be96a2d 2426
497ff579
SW
2427/* Recursively construct a grp array of fully resolved IDs.
2428 *
2429 * This function allows us to account for groups within groups,
2430 * by converting them into a flat array of IDs.
2431 *
2432 * nh_grp is modified at every level of recursion to append
2433 * to it the next unique, fully resolved ID from the entire tree.
2434 *
2435 *
2436 * Note:
2437 * I'm pretty sure we only allow ONE level of group within group currently.
2438 * But making this recursive just in case that ever changes.
2439 */
2440static uint8_t zebra_nhg_nhe2grp_internal(struct nh_grp *grp,
2441 uint8_t curr_index,
2442 struct nhg_hash_entry *nhe,
2443 int max_num)
98cda54a
SW
2444{
2445 struct nhg_connected *rb_node_dep = NULL;
2446 struct nhg_hash_entry *depend = NULL;
497ff579 2447 uint8_t i = curr_index;
98cda54a 2448
fec211ad 2449 frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) {
8dbc800f
SW
2450 bool duplicate = false;
2451
497ff579
SW
2452 if (i >= max_num)
2453 goto done;
2454
98cda54a
SW
2455 depend = rb_node_dep->nhe;
2456
2457 /*
2458 * If its recursive, use its resolved nhe in the group
2459 */
2460 if (CHECK_FLAG(depend->flags, NEXTHOP_GROUP_RECURSIVE)) {
2461 depend = zebra_nhg_resolve(depend);
2462 if (!depend) {
2463 flog_err(
2464 EC_ZEBRA_NHG_FIB_UPDATE,
df31a989
SW
2465 "Failed to recursively resolve Nexthop Hash Entry in the group id=%u",
2466 nhe->id);
98cda54a
SW
2467 continue;
2468 }
2469 }
2470
497ff579
SW
2471 if (!zebra_nhg_depends_is_empty(depend)) {
2472 /* This is a group within a group */
2473 i = zebra_nhg_nhe2grp_internal(grp, i, depend, max_num);
2474 } else {
086e4e02
SW
2475 if (!CHECK_FLAG(depend->flags, NEXTHOP_GROUP_VALID)) {
2476 if (IS_ZEBRA_DEBUG_RIB_DETAILED
2477 || IS_ZEBRA_DEBUG_NHG)
2478 zlog_debug(
2479 "%s: Nexthop ID (%u) not valid, not appending to dataplane install group",
2480 __func__, depend->id);
2481 continue;
2482 }
2483
1866b3af
SW
2484 /* If the nexthop not installed/queued for install don't
2485 * put in the ID array.
2486 */
2487 if (!(CHECK_FLAG(depend->flags, NEXTHOP_GROUP_INSTALLED)
2488 || CHECK_FLAG(depend->flags,
2489 NEXTHOP_GROUP_QUEUED))) {
2490 if (IS_ZEBRA_DEBUG_RIB_DETAILED
2491 || IS_ZEBRA_DEBUG_NHG)
2492 zlog_debug(
2493 "%s: Nexthop ID (%u) not installed or queued for install, not appending to dataplane install group",
2494 __func__, depend->id);
2495 continue;
2496 }
2497
b1c3f7ef 2498 /* Check for duplicate IDs, ignore if found. */
497ff579 2499 for (int j = 0; j < i; j++) {
d43122b5 2500 if (depend->id == grp[j].id) {
497ff579 2501 duplicate = true;
d43122b5
SW
2502 break;
2503 }
497ff579 2504 }
8dbc800f 2505
b1c3f7ef
SW
2506 if (duplicate) {
2507 if (IS_ZEBRA_DEBUG_RIB_DETAILED
2508 || IS_ZEBRA_DEBUG_NHG)
2509 zlog_debug(
2510 "%s: Nexthop ID (%u) is duplicate, not appending to dataplane install group",
2511 __func__, depend->id);
2512 continue;
497ff579 2513 }
b1c3f7ef
SW
2514
2515 grp[i].id = depend->id;
2516 grp[i].weight = depend->nhg.nexthop->weight;
2517 i++;
8dbc800f 2518 }
98cda54a 2519 }
8dbc800f 2520
0328a5bd
MS
2521 if (nhe->backup_info == NULL || nhe->backup_info->nhe == NULL)
2522 goto done;
2523
2524 /* TODO -- For now, we are not trying to use or install any
2525 * backup info in this nexthop-id path: we aren't prepared
2526 * to use the backups here yet. We're just debugging what we find.
2527 */
2528 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
2529 zlog_debug("%s: skipping backup nhe", __func__);
2530
8dbc800f 2531done:
98cda54a
SW
2532 return i;
2533}
2534
497ff579
SW
2535/* Convert a nhe into a group array */
2536uint8_t zebra_nhg_nhe2grp(struct nh_grp *grp, struct nhg_hash_entry *nhe,
2537 int max_num)
2538{
2539 /* Call into the recursive function */
2540 return zebra_nhg_nhe2grp_internal(grp, 0, nhe, max_num);
2541}
2542
5be96a2d
SW
2543void zebra_nhg_install_kernel(struct nhg_hash_entry *nhe)
2544{
f429bd1b
SW
2545 struct nhg_connected *rb_node_dep = NULL;
2546
2547 /* Resolve it first */
2548 nhe = zebra_nhg_resolve(nhe);
2549
2550 /* Make sure all depends are installed/queued */
fec211ad 2551 frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) {
f429bd1b
SW
2552 zebra_nhg_install_kernel(rb_node_dep->nhe);
2553 }
2554
086e4e02 2555 if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_VALID)
dd1e105f 2556 && (!CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED)
54c89c93 2557 || nhe->id >= ZEBRA_NHG_PROTO_LOWER)
e22e8001 2558 && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED)) {
724583ed 2559 /* Change its type to us since we are installing it */
0885b1e3
SW
2560 if (!ZEBRA_NHG_CREATED(nhe))
2561 nhe->type = ZEBRA_ROUTE_NHG;
724583ed 2562
147bad16 2563 int ret = dplane_nexthop_add(nhe);
2d3c57e6 2564
147bad16
SW
2565 switch (ret) {
2566 case ZEBRA_DPLANE_REQUEST_QUEUED:
2567 SET_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED);
2568 break;
2569 case ZEBRA_DPLANE_REQUEST_FAILURE:
2570 flog_err(
2571 EC_ZEBRA_DP_INSTALL_FAIL,
2572 "Failed to install Nexthop ID (%u) into the kernel",
2573 nhe->id);
2574 break;
2575 case ZEBRA_DPLANE_REQUEST_SUCCESS:
2576 SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
80286aa5 2577 zebra_nhg_handle_install(nhe);
147bad16
SW
2578 break;
2579 }
2580 }
2581}
2582
147bad16
SW
2583void zebra_nhg_uninstall_kernel(struct nhg_hash_entry *nhe)
2584{
2585 if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED)) {
2586 int ret = dplane_nexthop_delete(nhe);
2d3c57e6 2587
147bad16
SW
2588 switch (ret) {
2589 case ZEBRA_DPLANE_REQUEST_QUEUED:
2590 SET_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED);
2591 break;
2592 case ZEBRA_DPLANE_REQUEST_FAILURE:
2593 flog_err(
2594 EC_ZEBRA_DP_DELETE_FAIL,
2595 "Failed to uninstall Nexthop ID (%u) from the kernel",
2596 nhe->id);
2597 break;
2598 case ZEBRA_DPLANE_REQUEST_SUCCESS:
2599 UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
2600 break;
2601 }
177e711d
SW
2602 }
2603
2604 zebra_nhg_handle_uninstall(nhe);
147bad16
SW
2605}
2606
5f3c9e52
SW
2607void zebra_nhg_dplane_result(struct zebra_dplane_ctx *ctx)
2608{
2609 enum dplane_op_e op;
2610 enum zebra_dplane_result status;
2611 uint32_t id = 0;
2612 struct nhg_hash_entry *nhe = NULL;
2613
2614 op = dplane_ctx_get_op(ctx);
2615 status = dplane_ctx_get_status(ctx);
2616
0c8215cb 2617 id = dplane_ctx_get_nhe_id(ctx);
e22e8001 2618
377e29f7 2619 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL || IS_ZEBRA_DEBUG_NHG_DETAIL)
177e711d
SW
2620 zlog_debug(
2621 "Nexthop dplane ctx %p, op %s, nexthop ID (%u), result %s",
2622 ctx, dplane_op2str(op), id, dplane_res2str(status));
5f3c9e52 2623
177e711d
SW
2624 switch (op) {
2625 case DPLANE_OP_NH_DELETE:
2626 if (status != ZEBRA_DPLANE_REQUEST_SUCCESS)
2627 flog_err(
2628 EC_ZEBRA_DP_DELETE_FAIL,
2629 "Failed to uninstall Nexthop ID (%u) from the kernel",
2630 id);
2631 /* We already free'd the data, nothing to do */
2632 break;
2633 case DPLANE_OP_NH_INSTALL:
2634 case DPLANE_OP_NH_UPDATE:
2635 nhe = zebra_nhg_lookup_id(id);
2636
2637 if (!nhe) {
2638 flog_err(
2639 EC_ZEBRA_NHG_SYNC,
2640 "%s operation preformed on Nexthop ID (%u) in the kernel, that we no longer have in our table",
2641 dplane_op2str(op), id);
5f3c9e52
SW
2642 break;
2643 }
177e711d
SW
2644
2645 UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED);
2646 if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
2647 SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID);
2648 SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
80286aa5 2649 zebra_nhg_handle_install(nhe);
177e711d
SW
2650 } else
2651 flog_err(
2652 EC_ZEBRA_DP_INSTALL_FAIL,
2653 "Failed to install Nexthop ID (%u) into the kernel",
2654 nhe->id);
2655 break;
2656 case DPLANE_OP_ROUTE_INSTALL:
2657 case DPLANE_OP_ROUTE_UPDATE:
2658 case DPLANE_OP_ROUTE_DELETE:
2659 case DPLANE_OP_ROUTE_NOTIFY:
2660 case DPLANE_OP_LSP_INSTALL:
2661 case DPLANE_OP_LSP_UPDATE:
2662 case DPLANE_OP_LSP_DELETE:
2663 case DPLANE_OP_LSP_NOTIFY:
2664 case DPLANE_OP_PW_INSTALL:
2665 case DPLANE_OP_PW_UNINSTALL:
2666 case DPLANE_OP_SYS_ROUTE_ADD:
2667 case DPLANE_OP_SYS_ROUTE_DELETE:
2668 case DPLANE_OP_ADDR_INSTALL:
2669 case DPLANE_OP_ADDR_UNINSTALL:
2670 case DPLANE_OP_MAC_INSTALL:
2671 case DPLANE_OP_MAC_DELETE:
2672 case DPLANE_OP_NEIGH_INSTALL:
2673 case DPLANE_OP_NEIGH_UPDATE:
2674 case DPLANE_OP_NEIGH_DELETE:
2675 case DPLANE_OP_VTEP_ADD:
2676 case DPLANE_OP_VTEP_DELETE:
60d8d43b
JU
2677 case DPLANE_OP_RULE_ADD:
2678 case DPLANE_OP_RULE_DELETE:
2679 case DPLANE_OP_RULE_UPDATE:
d68e74b4 2680 case DPLANE_OP_NEIGH_DISCOVER:
177e711d
SW
2681 case DPLANE_OP_NONE:
2682 break;
2683 }
71593b3f
SW
2684
2685 dplane_ctx_fini(&ctx);
5be96a2d
SW
2686}
2687
38e40db1
SW
2688static void zebra_nhg_sweep_entry(struct hash_bucket *bucket, void *arg)
2689{
2690 struct nhg_hash_entry *nhe = NULL;
2691
2692 nhe = (struct nhg_hash_entry *)bucket->data;
2693
2694 /* If its being ref'd, just let it be uninstalled via a route removal */
2695 if (ZEBRA_NHG_CREATED(nhe) && nhe->refcnt <= 0)
2696 zebra_nhg_uninstall_kernel(nhe);
2697}
2698
2699void zebra_nhg_sweep_table(struct hash *hash)
2700{
2701 hash_iterate(hash, zebra_nhg_sweep_entry, NULL);
2702}
7c99d51b
MS
2703
2704/* Global control to disable use of kernel nexthops, if available. We can't
2705 * force the kernel to support nexthop ids, of course, but we can disable
2706 * zebra's use of them, for testing e.g. By default, if the kernel supports
2707 * nexthop ids, zebra uses them.
2708 */
2709void zebra_nhg_enable_kernel_nexthops(bool set)
2710{
2711 g_nexthops_enabled = set;
2712}
2713
2714bool zebra_nhg_kernel_nexthops_enabled(void)
2715{
2716 return g_nexthops_enabled;
2717}
0885b1e3 2718
6c67f41f
SW
2719/*
2720 * Global control to only use kernel nexthops for protocol created NHGs.
2721 * There are some use cases where you may not want zebra to implicitly
2722 * create kernel nexthops for all routes and only create them for NHGs
2723 * passed down by upper level protos.
2724 *
2725 * Default is off.
2726 */
2727void zebra_nhg_set_proto_nexthops_only(bool set)
2728{
2729 proto_nexthops_only = set;
2730}
2731
2732bool zebra_nhg_proto_nexthops_only(void)
2733{
2734 return proto_nexthops_only;
2735}
2736
0885b1e3
SW
2737/* Add NHE from upper level proto */
2738struct nhg_hash_entry *zebra_nhg_proto_add(uint32_t id, int type,
2739 struct nexthop_group *nhg, afi_t afi)
2740{
2741 struct nhg_hash_entry lookup;
dd1e105f 2742 struct nhg_hash_entry *new, *old;
0885b1e3
SW
2743 struct nhg_connected *rb_node_dep = NULL;
2744
2745 zebra_nhe_init(&lookup, afi, nhg->nexthop);
2746 lookup.nhg.nexthop = nhg->nexthop;
2747 lookup.id = id;
2748 lookup.type = type;
2749
dd1e105f
SW
2750 old = zebra_nhg_lookup_id(id);
2751
2752 if (old) {
2753 /*
2754 * This is a replace, just release NHE from ID for now, The
2755 * depends/dependents may still be used in the replacement.
2756 */
2757 hash_release(zrouter.nhgs_id, old);
2758 }
2759
0885b1e3
SW
2760 new = zebra_nhg_rib_find_nhe(&lookup, afi);
2761
dd1e105f
SW
2762 if (old) {
2763 /* Now release depends/dependents in old one */
2764 zebra_nhg_release_all_deps(old);
2765 }
2766
0885b1e3
SW
2767 if (!new)
2768 return NULL;
2769
2770 /* TODO: Assuming valid/onlink for now */
2771 SET_FLAG(new->flags, NEXTHOP_GROUP_VALID);
2772
2773 if (!zebra_nhg_depends_is_empty(new)) {
2774 frr_each (nhg_connected_tree, &new->nhg_depends, rb_node_dep)
2775 SET_FLAG(rb_node_dep->nhe->flags, NEXTHOP_GROUP_VALID);
2776 }
2777
2778 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
dd1e105f
SW
2779 zlog_debug("%s: %s nhe %p (%u), vrf %d, type %s", __func__,
2780 (old ? "replaced" : "added"), new, new->id,
2781 new->vrf_id, zebra_route_string(new->type));
0885b1e3
SW
2782
2783 return new;
2784}
2785
2786/* Delete NHE from upper level proto */
2787struct nhg_hash_entry *zebra_nhg_proto_del(uint32_t id)
2788{
2789 struct nhg_hash_entry *nhe;
2790
2791 nhe = zebra_nhg_lookup_id(id);
2792
2793 if (!nhe) {
2794 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
2795 zlog_debug("%s: id %u, lookup failed", __func__, id);
2796
2797 return NULL;
2798 }
2799
2800 if (nhe->refcnt) {
2801 /* TODO: should be warn? */
2802 if (IS_ZEBRA_DEBUG_NHG)
2803 zlog_debug("%s: id %u, still being used refcnt %u",
2804 __func__, nhe->id, nhe->refcnt);
2805 return NULL;
2806 }
2807
2808 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
2809 zlog_debug("%s: deleted nhe %p (%u), vrf %d, type %s", __func__,
2810 nhe, nhe->id, nhe->vrf_id,
2811 zebra_route_string(nhe->type));
2812
2813 return nhe;
2814}
2815
24db1a7b
SW
2816struct nhg_score_proto_iter {
2817 int type;
2818 unsigned long found;
2819};
2820
2821static void zebra_nhg_score_proto_entry(struct hash_bucket *bucket, void *arg)
2822{
2823 struct nhg_hash_entry *nhe;
2824 struct nhg_score_proto_iter *iter;
2825
2826 nhe = (struct nhg_hash_entry *)bucket->data;
2827 iter = arg;
2828
2829 /* Needs to match type and outside zebra ID space */
2830 if (nhe->type == iter->type && nhe->id >= ZEBRA_NHG_PROTO_LOWER) {
2831 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
2832 zlog_debug(
2833 "%s: found nhe %p (%u), vrf %d, type %s after client disconnect",
2834 __func__, nhe, nhe->id, nhe->vrf_id,
2835 zebra_route_string(nhe->type));
2836
2837 /* This should be the last ref if we remove client routes too */
2838 zebra_nhg_decrement_ref(nhe);
2839 }
2840}
2841
2842/* Remove specific by proto NHGs */
2843unsigned long zebra_nhg_score_proto(int type)
2844{
2845 struct nhg_score_proto_iter iter = {};
2846
2847 iter.type = type;
2848
2849 hash_iterate(zrouter.nhgs_id, zebra_nhg_score_proto_entry, &iter);
2850
2851 return iter.found;
2852}