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