-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
zebra_nhg.c
3732 lines (3114 loc) · 96.4 KB
/
zebra_nhg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/* Zebra Nexthop Group Code.
* Copyright (C) 2019 Cumulus Networks, Inc.
* Donald Sharp
* Stephen Worley
*/
#include <zebra.h>
#include "lib/nexthop.h"
#include "lib/nexthop_group_private.h"
#include "lib/routemap.h"
#include "lib/mpls.h"
#include "lib/jhash.h"
#include "lib/debug.h"
#include "lib/lib_errors.h"
#include "zebra/connected.h"
#include "zebra/debug.h"
#include "zebra/zebra_router.h"
#include "zebra/zebra_nhg_private.h"
#include "zebra/zebra_rnh.h"
#include "zebra/zebra_routemap.h"
#include "zebra/zebra_srte.h"
#include "zebra/zserv.h"
#include "zebra/rt.h"
#include "zebra_errors.h"
#include "zebra_dplane.h"
#include "zebra/interface.h"
#include "zebra/zapi_msg.h"
#include "zebra/rib.h"
#include "zebra/zebra_vxlan.h"
DEFINE_MTYPE_STATIC(ZEBRA, NHG, "Nexthop Group Entry");
DEFINE_MTYPE_STATIC(ZEBRA, NHG_CONNECTED, "Nexthop Group Connected");
DEFINE_MTYPE_STATIC(ZEBRA, NHG_CTX, "Nexthop Group Context");
/* Map backup nexthop indices between two nhes */
struct backup_nh_map_s {
int map_count;
struct {
uint8_t orig_idx;
uint8_t new_idx;
} map[MULTIPATH_NUM];
};
/* id counter to keep in sync with kernel */
uint32_t id_counter;
/* Controlled through ui */
static bool g_nexthops_enabled = true;
static bool proto_nexthops_only;
static bool use_recursive_backups = true;
static struct nhg_hash_entry *depends_find(const struct nexthop *nh, afi_t afi,
int type, bool from_dplane);
static void depends_add(struct nhg_connected_tree_head *head,
struct nhg_hash_entry *depend);
static struct nhg_hash_entry *
depends_find_add(struct nhg_connected_tree_head *head, struct nexthop *nh,
afi_t afi, int type, bool from_dplane);
static struct nhg_hash_entry *
depends_find_id_add(struct nhg_connected_tree_head *head, uint32_t id);
static void depends_decrement_free(struct nhg_connected_tree_head *head);
static struct nhg_backup_info *
nhg_backup_copy(const struct nhg_backup_info *orig);
/* Helper function for getting the next allocatable ID */
static uint32_t nhg_get_next_id(void)
{
while (1) {
id_counter++;
if (IS_ZEBRA_DEBUG_NHG_DETAIL)
zlog_debug("%s: ID %u checking", __func__, id_counter);
if (id_counter == ZEBRA_NHG_PROTO_LOWER) {
if (IS_ZEBRA_DEBUG_NHG_DETAIL)
zlog_debug("%s: ID counter wrapped", __func__);
id_counter = 0;
continue;
}
if (zebra_nhg_lookup_id(id_counter)) {
if (IS_ZEBRA_DEBUG_NHG_DETAIL)
zlog_debug("%s: ID already exists", __func__);
continue;
}
break;
}
return id_counter;
}
static void nhg_connected_free(struct nhg_connected *dep)
{
XFREE(MTYPE_NHG_CONNECTED, dep);
}
static struct nhg_connected *nhg_connected_new(struct nhg_hash_entry *nhe)
{
struct nhg_connected *new = NULL;
new = XCALLOC(MTYPE_NHG_CONNECTED, sizeof(struct nhg_connected));
new->nhe = nhe;
return new;
}
void nhg_connected_tree_free(struct nhg_connected_tree_head *head)
{
struct nhg_connected *rb_node_dep = NULL;
if (!nhg_connected_tree_is_empty(head)) {
frr_each_safe(nhg_connected_tree, head, rb_node_dep) {
nhg_connected_tree_del(head, rb_node_dep);
nhg_connected_free(rb_node_dep);
}
}
}
bool nhg_connected_tree_is_empty(const struct nhg_connected_tree_head *head)
{
return nhg_connected_tree_count(head) ? false : true;
}
struct nhg_connected *
nhg_connected_tree_root(struct nhg_connected_tree_head *head)
{
return nhg_connected_tree_first(head);
}
struct nhg_hash_entry *
nhg_connected_tree_del_nhe(struct nhg_connected_tree_head *head,
struct nhg_hash_entry *depend)
{
struct nhg_connected lookup = {};
struct nhg_connected *remove = NULL;
struct nhg_hash_entry *removed_nhe;
lookup.nhe = depend;
/* Lookup to find the element, then remove it */
remove = nhg_connected_tree_find(head, &lookup);
if (remove)
/* Re-returning here just in case this API changes..
* the _del list api's are a bit undefined at the moment.
*
* So hopefully returning here will make it fail if the api
* changes to something different than currently expected.
*/
remove = nhg_connected_tree_del(head, remove);
/* If the entry was sucessfully removed, free the 'connected` struct */
if (remove) {
removed_nhe = remove->nhe;
nhg_connected_free(remove);
return removed_nhe;
}
return NULL;
}
/* Assuming UNIQUE RB tree. If this changes, assumptions here about
* insertion need to change.
*/
struct nhg_hash_entry *
nhg_connected_tree_add_nhe(struct nhg_connected_tree_head *head,
struct nhg_hash_entry *depend)
{
struct nhg_connected *new = NULL;
new = nhg_connected_new(depend);
/* On success, NULL will be returned from the
* RB code.
*/
if (new && (nhg_connected_tree_add(head, new) == NULL))
return NULL;
/* If it wasn't successful, it must be a duplicate. We enforce the
* unique property for the `nhg_connected` tree.
*/
nhg_connected_free(new);
return depend;
}
static void
nhg_connected_tree_decrement_ref(struct nhg_connected_tree_head *head)
{
struct nhg_connected *rb_node_dep = NULL;
frr_each_safe(nhg_connected_tree, head, rb_node_dep) {
zebra_nhg_decrement_ref(rb_node_dep->nhe);
}
}
static void
nhg_connected_tree_increment_ref(struct nhg_connected_tree_head *head)
{
struct nhg_connected *rb_node_dep = NULL;
frr_each(nhg_connected_tree, head, rb_node_dep) {
zebra_nhg_increment_ref(rb_node_dep->nhe);
}
}
struct nhg_hash_entry *zebra_nhg_resolve(struct nhg_hash_entry *nhe)
{
if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_RECURSIVE)
&& !zebra_nhg_depends_is_empty(nhe)) {
nhe = nhg_connected_tree_root(&nhe->nhg_depends)->nhe;
return zebra_nhg_resolve(nhe);
}
return nhe;
}
unsigned int zebra_nhg_depends_count(const struct nhg_hash_entry *nhe)
{
return nhg_connected_tree_count(&nhe->nhg_depends);
}
bool zebra_nhg_depends_is_empty(const struct nhg_hash_entry *nhe)
{
return nhg_connected_tree_is_empty(&nhe->nhg_depends);
}
static void zebra_nhg_depends_del(struct nhg_hash_entry *from,
struct nhg_hash_entry *depend)
{
nhg_connected_tree_del_nhe(&from->nhg_depends, depend);
}
static void zebra_nhg_depends_init(struct nhg_hash_entry *nhe)
{
nhg_connected_tree_init(&nhe->nhg_depends);
}
unsigned int zebra_nhg_dependents_count(const struct nhg_hash_entry *nhe)
{
return nhg_connected_tree_count(&nhe->nhg_dependents);
}
bool zebra_nhg_dependents_is_empty(const struct nhg_hash_entry *nhe)
{
return nhg_connected_tree_is_empty(&nhe->nhg_dependents);
}
static void zebra_nhg_dependents_del(struct nhg_hash_entry *from,
struct nhg_hash_entry *dependent)
{
nhg_connected_tree_del_nhe(&from->nhg_dependents, dependent);
}
static void zebra_nhg_dependents_add(struct nhg_hash_entry *to,
struct nhg_hash_entry *dependent)
{
nhg_connected_tree_add_nhe(&to->nhg_dependents, dependent);
}
static void zebra_nhg_dependents_init(struct nhg_hash_entry *nhe)
{
nhg_connected_tree_init(&nhe->nhg_dependents);
}
/* Release this nhe from anything depending on it */
static void zebra_nhg_dependents_release(struct nhg_hash_entry *nhe)
{
struct nhg_connected *rb_node_dep = NULL;
frr_each_safe(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep) {
zebra_nhg_depends_del(rb_node_dep->nhe, nhe);
/* recheck validity of the dependent */
zebra_nhg_check_valid(rb_node_dep->nhe);
}
}
/* Release this nhe from anything that it depends on */
static void zebra_nhg_depends_release(struct nhg_hash_entry *nhe)
{
if (!zebra_nhg_depends_is_empty(nhe)) {
struct nhg_connected *rb_node_dep = NULL;
frr_each_safe(nhg_connected_tree, &nhe->nhg_depends,
rb_node_dep) {
zebra_nhg_dependents_del(rb_node_dep->nhe, nhe);
}
}
}
struct nhg_hash_entry *zebra_nhg_lookup_id(uint32_t id)
{
struct nhg_hash_entry lookup = {};
lookup.id = id;
return hash_lookup(zrouter.nhgs_id, &lookup);
}
static int zebra_nhg_insert_id(struct nhg_hash_entry *nhe)
{
if (hash_lookup(zrouter.nhgs_id, nhe)) {
flog_err(
EC_ZEBRA_NHG_TABLE_INSERT_FAILED,
"Failed inserting NHG %pNG into the ID hash table, entry already exists",
nhe);
return -1;
}
(void)hash_get(zrouter.nhgs_id, nhe, hash_alloc_intern);
return 0;
}
static void zebra_nhg_set_if(struct nhg_hash_entry *nhe, struct interface *ifp)
{
nhe->ifp = ifp;
if_nhg_dependents_add(ifp, nhe);
}
static void
zebra_nhg_connect_depends(struct nhg_hash_entry *nhe,
struct nhg_connected_tree_head *nhg_depends)
{
struct nhg_connected *rb_node_dep = NULL;
/* This has been allocated higher above in the stack. Could probably
* re-allocate and free the old stuff but just using the same memory
* for now. Otherwise, their might be a time trade-off for repeated
* alloc/frees as startup.
*/
nhe->nhg_depends = *nhg_depends;
/* Attach backpointer to anything that it depends on */
zebra_nhg_dependents_init(nhe);
if (!zebra_nhg_depends_is_empty(nhe)) {
frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) {
if (IS_ZEBRA_DEBUG_NHG_DETAIL)
zlog_debug("%s: nhe %p (%pNG), dep %p (%pNG)",
__func__, nhe, nhe, rb_node_dep->nhe,
rb_node_dep->nhe);
zebra_nhg_dependents_add(rb_node_dep->nhe, nhe);
}
}
}
/* Init an nhe, for use in a hash lookup for example */
void zebra_nhe_init(struct nhg_hash_entry *nhe, afi_t afi,
const struct nexthop *nh)
{
memset(nhe, 0, sizeof(struct nhg_hash_entry));
nhe->vrf_id = VRF_DEFAULT;
nhe->type = ZEBRA_ROUTE_NHG;
nhe->afi = AFI_UNSPEC;
/* There are some special rules that apply to groups representing
* a single nexthop.
*/
if (nh && (nh->next == NULL)) {
switch (nh->type) {
case NEXTHOP_TYPE_IFINDEX:
case NEXTHOP_TYPE_BLACKHOLE:
/*
* This switch case handles setting the afi different
* for ipv4/v6 routes. Ifindex/blackhole nexthop
* objects cannot be ambiguous, they must be Address
* Family specific. If we get here, we will either use
* the AF of the route, or the one we got passed from
* here from the kernel.
*/
nhe->afi = afi;
break;
case NEXTHOP_TYPE_IPV4_IFINDEX:
case NEXTHOP_TYPE_IPV4:
nhe->afi = AFI_IP;
break;
case NEXTHOP_TYPE_IPV6_IFINDEX:
case NEXTHOP_TYPE_IPV6:
nhe->afi = AFI_IP6;
break;
}
}
}
struct nhg_hash_entry *zebra_nhg_alloc(void)
{
struct nhg_hash_entry *nhe;
nhe = XCALLOC(MTYPE_NHG, sizeof(struct nhg_hash_entry));
return nhe;
}
/*
* Allocate new nhe and make shallow copy of 'orig'; no
* recursive info is copied.
*/
struct nhg_hash_entry *zebra_nhe_copy(const struct nhg_hash_entry *orig,
uint32_t id)
{
struct nhg_hash_entry *nhe;
nhe = zebra_nhg_alloc();
nhe->id = id;
nexthop_group_copy(&(nhe->nhg), &(orig->nhg));
nhe->vrf_id = orig->vrf_id;
nhe->afi = orig->afi;
nhe->type = orig->type ? orig->type : ZEBRA_ROUTE_NHG;
nhe->refcnt = 0;
nhe->dplane_ref = zebra_router_get_next_sequence();
/* Copy backup info also, if present */
if (orig->backup_info)
nhe->backup_info = nhg_backup_copy(orig->backup_info);
return nhe;
}
/* Allocation via hash handler */
static void *zebra_nhg_hash_alloc(void *arg)
{
struct nhg_hash_entry *nhe = NULL;
struct nhg_hash_entry *copy = arg;
nhe = zebra_nhe_copy(copy, copy->id);
/* Mark duplicate nexthops in a group at creation time. */
nexthop_group_mark_duplicates(&(nhe->nhg));
/*
* Add the ifp now if it's not a group or recursive and has ifindex.
*
* A proto-owned ID is always a group.
*/
if (!PROTO_OWNED(nhe) && nhe->nhg.nexthop && !nhe->nhg.nexthop->next
&& !nhe->nhg.nexthop->resolved && nhe->nhg.nexthop->ifindex) {
struct interface *ifp = NULL;
ifp = if_lookup_by_index(nhe->nhg.nexthop->ifindex,
nhe->nhg.nexthop->vrf_id);
if (ifp)
zebra_nhg_set_if(nhe, ifp);
else {
if (IS_ZEBRA_DEBUG_NHG)
zlog_debug(
"Failed to lookup an interface with ifindex=%d in vrf=%u for NHE %pNG",
nhe->nhg.nexthop->ifindex,
nhe->nhg.nexthop->vrf_id, nhe);
}
}
return nhe;
}
uint32_t zebra_nhg_hash_key(const void *arg)
{
const struct nhg_hash_entry *nhe = arg;
uint32_t key = 0x5a351234;
uint32_t primary = 0;
uint32_t backup = 0;
primary = nexthop_group_hash(&(nhe->nhg));
if (nhe->backup_info)
backup = nexthop_group_hash(&(nhe->backup_info->nhe->nhg));
key = jhash_3words(primary, backup, nhe->type, key);
key = jhash_2words(nhe->vrf_id, nhe->afi, key);
return key;
}
uint32_t zebra_nhg_id_key(const void *arg)
{
const struct nhg_hash_entry *nhe = arg;
return nhe->id;
}
/* Helper with common nhg/nhe nexthop comparison logic */
static bool nhg_compare_nexthops(const struct nexthop *nh1,
const struct nexthop *nh2)
{
assert(nh1 != NULL && nh2 != NULL);
/*
* We have to check the active flag of each individual one,
* not just the overall active_num. This solves the special case
* issue of a route with a nexthop group with one nexthop
* resolving to itself and thus marking it inactive. If we
* have two different routes each wanting to mark a different
* nexthop inactive, they need to hash to two different groups.
*
* If we just hashed on num_active, they would hash the same
* which is incorrect.
*
* ex)
* 1.1.1.0/24
* -> 1.1.1.1 dummy1 (inactive)
* -> 1.1.2.1 dummy2
*
* 1.1.2.0/24
* -> 1.1.1.1 dummy1
* -> 1.1.2.1 dummy2 (inactive)
*
* Without checking each individual one, they would hash to
* the same group and both have 1.1.1.1 dummy1 marked inactive.
*
*/
if (CHECK_FLAG(nh1->flags, NEXTHOP_FLAG_ACTIVE)
!= CHECK_FLAG(nh2->flags, NEXTHOP_FLAG_ACTIVE))
return false;
if (!nexthop_same(nh1, nh2))
return false;
return true;
}
bool zebra_nhg_hash_equal(const void *arg1, const void *arg2)
{
const struct nhg_hash_entry *nhe1 = arg1;
const struct nhg_hash_entry *nhe2 = arg2;
struct nexthop *nexthop1;
struct nexthop *nexthop2;
/* No matter what if they equal IDs, assume equal */
if (nhe1->id && nhe2->id && (nhe1->id == nhe2->id))
return true;
if (nhe1->type != nhe2->type)
return false;
if (nhe1->vrf_id != nhe2->vrf_id)
return false;
if (nhe1->afi != nhe2->afi)
return false;
if (nhe1->nhg.nhgr.buckets != nhe2->nhg.nhgr.buckets)
return false;
if (nhe1->nhg.nhgr.idle_timer != nhe2->nhg.nhgr.idle_timer)
return false;
if (nhe1->nhg.nhgr.unbalanced_timer != nhe2->nhg.nhgr.unbalanced_timer)
return false;
/* Nexthops should be in-order, so we simply compare them in-place */
for (nexthop1 = nhe1->nhg.nexthop, nexthop2 = nhe2->nhg.nexthop;
nexthop1 && nexthop2;
nexthop1 = nexthop1->next, nexthop2 = nexthop2->next) {
if (!nhg_compare_nexthops(nexthop1, nexthop2))
return false;
}
/* Check for unequal list lengths */
if (nexthop1 || nexthop2)
return false;
/* If there's no backup info, comparison is done. */
if ((nhe1->backup_info == NULL) && (nhe2->backup_info == NULL))
return true;
/* Compare backup info also - test the easy things first */
if (nhe1->backup_info && (nhe2->backup_info == NULL))
return false;
if (nhe2->backup_info && (nhe1->backup_info == NULL))
return false;
/* Compare number of backups before actually comparing any */
for (nexthop1 = nhe1->backup_info->nhe->nhg.nexthop,
nexthop2 = nhe2->backup_info->nhe->nhg.nexthop;
nexthop1 && nexthop2;
nexthop1 = nexthop1->next, nexthop2 = nexthop2->next) {
;
}
/* Did we find the end of one list before the other? */
if (nexthop1 || nexthop2)
return false;
/* Have to compare the backup nexthops */
for (nexthop1 = nhe1->backup_info->nhe->nhg.nexthop,
nexthop2 = nhe2->backup_info->nhe->nhg.nexthop;
nexthop1 && nexthop2;
nexthop1 = nexthop1->next, nexthop2 = nexthop2->next) {
if (!nhg_compare_nexthops(nexthop1, nexthop2))
return false;
}
return true;
}
bool zebra_nhg_hash_id_equal(const void *arg1, const void *arg2)
{
const struct nhg_hash_entry *nhe1 = arg1;
const struct nhg_hash_entry *nhe2 = arg2;
return nhe1->id == nhe2->id;
}
static int zebra_nhg_process_grp(struct nexthop_group *nhg,
struct nhg_connected_tree_head *depends,
struct nh_grp *grp, uint8_t count,
struct nhg_resilience *resilience)
{
nhg_connected_tree_init(depends);
for (int i = 0; i < count; i++) {
struct nhg_hash_entry *depend = NULL;
/* We do not care about nexthop_grp.weight at
* this time. But we should figure out
* how to adapt this to our code in
* the future.
*/
depend = depends_find_id_add(depends, grp[i].id);
if (!depend) {
flog_err(
EC_ZEBRA_NHG_SYNC,
"Received Nexthop Group from the kernel with a dependent Nexthop ID (%u) which we do not have in our table",
grp[i].id);
return -1;
}
/*
* If this is a nexthop with its own group
* dependencies, add them as well. Not sure its
* even possible to have a group within a group
* in the kernel.
*/
copy_nexthops(&nhg->nexthop, depend->nhg.nexthop, NULL);
}
if (resilience)
nhg->nhgr = *resilience;
return 0;
}
static void handle_recursive_depend(struct nhg_connected_tree_head *nhg_depends,
struct nexthop *nh, afi_t afi, int type)
{
struct nhg_hash_entry *depend = NULL;
struct nexthop_group resolved_ng = {};
resolved_ng.nexthop = nh;
if (IS_ZEBRA_DEBUG_NHG_DETAIL)
zlog_debug("%s: head %p, nh %pNHv",
__func__, nhg_depends, nh);
depend = zebra_nhg_rib_find(0, &resolved_ng, afi, type);
if (IS_ZEBRA_DEBUG_NHG_DETAIL)
zlog_debug("%s: nh %pNHv => %p (%u)",
__func__, nh, depend,
depend ? depend->id : 0);
if (depend)
depends_add(nhg_depends, depend);
}
/*
* Lookup an nhe in the global hash, using data from another nhe. If 'lookup'
* has an id value, that's used. Create a new global/shared nhe if not found.
*/
static bool zebra_nhe_find(struct nhg_hash_entry **nhe, /* return value */
struct nhg_hash_entry *lookup,
struct nhg_connected_tree_head *nhg_depends,
afi_t afi, bool from_dplane)
{
bool created = false;
bool recursive = false;
struct nhg_hash_entry *newnhe, *backup_nhe;
struct nexthop *nh = NULL;
if (IS_ZEBRA_DEBUG_NHG_DETAIL)
zlog_debug(
"%s: id %u, lookup %p, vrf %d, type %d, depends %p%s",
__func__, lookup->id, lookup, lookup->vrf_id,
lookup->type, nhg_depends,
(from_dplane ? " (from dplane)" : ""));
if (lookup->id)
(*nhe) = zebra_nhg_lookup_id(lookup->id);
else
(*nhe) = hash_lookup(zrouter.nhgs, lookup);
if (IS_ZEBRA_DEBUG_NHG_DETAIL)
zlog_debug("%s: lookup => %p (%pNG)", __func__, *nhe, *nhe);
/* If we found an existing object, we're done */
if (*nhe)
goto done;
/* We're going to create/insert a new nhe:
* assign the next global id value if necessary.
*/
if (lookup->id == 0)
lookup->id = nhg_get_next_id();
if (!from_dplane && lookup->id < ZEBRA_NHG_PROTO_LOWER) {
/*
* This is a zebra hashed/owned NHG.
*
* It goes in HASH and ID table.
*/
newnhe = hash_get(zrouter.nhgs, lookup, zebra_nhg_hash_alloc);
zebra_nhg_insert_id(newnhe);
} else {
/*
* This is upperproto owned NHG or one we read in from dataplane
* and should not be hashed to.
*
* It goes in ID table.
*/
newnhe =
hash_get(zrouter.nhgs_id, lookup, zebra_nhg_hash_alloc);
}
created = true;
/* Mail back the new object */
*nhe = newnhe;
if (IS_ZEBRA_DEBUG_NHG_DETAIL)
zlog_debug("%s: => created %p (%pNG)", __func__, newnhe,
newnhe);
/* Only hash/lookup the depends if the first lookup
* fails to find something. This should hopefully save a
* lot of cycles for larger ecmp sizes.
*/
if (nhg_depends) {
/* If you don't want to hash on each nexthop in the
* nexthop group struct you can pass the depends
* directly. Kernel-side we do this since it just looks
* them up via IDs.
*/
zebra_nhg_connect_depends(newnhe, nhg_depends);
goto done;
}
/* Prepare dependency relationships if this is not a
* singleton nexthop. There are two cases: a single
* recursive nexthop, where we need a relationship to the
* resolving nexthop; or a group of nexthops, where we need
* relationships with the corresponding singletons.
*/
zebra_nhg_depends_init(newnhe);
nh = newnhe->nhg.nexthop;
if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_ACTIVE))
SET_FLAG(newnhe->flags, NEXTHOP_GROUP_VALID);
if (nh->next == NULL && newnhe->id < ZEBRA_NHG_PROTO_LOWER) {
if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE)) {
/* Single recursive nexthop */
handle_recursive_depend(&newnhe->nhg_depends,
nh->resolved, afi,
newnhe->type);
recursive = true;
}
} else {
/* Proto-owned are groups by default */
/* List of nexthops */
for (nh = newnhe->nhg.nexthop; nh; nh = nh->next) {
if (IS_ZEBRA_DEBUG_NHG_DETAIL)
zlog_debug("%s: depends NH %pNHv %s",
__func__, nh,
CHECK_FLAG(nh->flags,
NEXTHOP_FLAG_RECURSIVE) ?
"(R)" : "");
depends_find_add(&newnhe->nhg_depends, nh, afi,
newnhe->type, from_dplane);
}
}
if (recursive)
SET_FLAG(newnhe->flags, NEXTHOP_GROUP_RECURSIVE);
/* Attach dependent backpointers to singletons */
zebra_nhg_connect_depends(newnhe, &newnhe->nhg_depends);
/**
* Backup Nexthops
*/
if (zebra_nhg_get_backup_nhg(newnhe) == NULL ||
zebra_nhg_get_backup_nhg(newnhe)->nexthop == NULL)
goto done;
/* If there are backup nexthops, add them to the backup
* depends tree. The rules here are a little different.
*/
recursive = false;
backup_nhe = newnhe->backup_info->nhe;
nh = backup_nhe->nhg.nexthop;
/* Singleton recursive NH */
if (nh->next == NULL &&
CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE)) {
if (IS_ZEBRA_DEBUG_NHG_DETAIL)
zlog_debug("%s: backup depend NH %pNHv (R)",
__func__, nh);
/* Single recursive nexthop */
handle_recursive_depend(&backup_nhe->nhg_depends, nh->resolved,
afi, backup_nhe->type);
recursive = true;
} else {
/* One or more backup NHs */
for (; nh; nh = nh->next) {
if (IS_ZEBRA_DEBUG_NHG_DETAIL)
zlog_debug("%s: backup depend NH %pNHv %s",
__func__, nh,
CHECK_FLAG(nh->flags,
NEXTHOP_FLAG_RECURSIVE) ?
"(R)" : "");
depends_find_add(&backup_nhe->nhg_depends, nh, afi,
backup_nhe->type, from_dplane);
}
}
if (recursive)
SET_FLAG(backup_nhe->flags, NEXTHOP_GROUP_RECURSIVE);
done:
/* Reset time since last update */
(*nhe)->uptime = monotime(NULL);
return created;
}
/*
* Lookup or create an nhe, based on an nhg or an nhe id.
*/
static bool zebra_nhg_find(struct nhg_hash_entry **nhe, uint32_t id,
struct nexthop_group *nhg,
struct nhg_connected_tree_head *nhg_depends,
vrf_id_t vrf_id, afi_t afi, int type,
bool from_dplane)
{
struct nhg_hash_entry lookup = {};
bool created = false;
if (IS_ZEBRA_DEBUG_NHG_DETAIL)
zlog_debug("%s: id %u, nhg %p, vrf %d, type %d, depends %p",
__func__, id, nhg, vrf_id, type,
nhg_depends);
/* Use a temporary nhe and call into the superset/common code */
lookup.id = id;
lookup.type = type ? type : ZEBRA_ROUTE_NHG;
lookup.nhg = *nhg;
lookup.vrf_id = vrf_id;
if (nhg_depends || lookup.nhg.nexthop->next) {
/* Groups can have all vrfs and AF's in them */
lookup.afi = AFI_UNSPEC;
} else {
switch (lookup.nhg.nexthop->type) {
case (NEXTHOP_TYPE_IFINDEX):
case (NEXTHOP_TYPE_BLACKHOLE):
/*
* This switch case handles setting the afi different
* for ipv4/v6 routes. Ifindex/blackhole nexthop
* objects cannot be ambiguous, they must be Address
* Family specific. If we get here, we will either use
* the AF of the route, or the one we got passed from
* here from the kernel.
*/
lookup.afi = afi;
break;
case (NEXTHOP_TYPE_IPV4_IFINDEX):
case (NEXTHOP_TYPE_IPV4):
lookup.afi = AFI_IP;
break;
case (NEXTHOP_TYPE_IPV6_IFINDEX):
case (NEXTHOP_TYPE_IPV6):
lookup.afi = AFI_IP6;
break;
}
}
created = zebra_nhe_find(nhe, &lookup, nhg_depends, afi, from_dplane);
return created;
}
/* Find/create a single nexthop */
static struct nhg_hash_entry *zebra_nhg_find_nexthop(uint32_t id,
struct nexthop *nh,
afi_t afi, int type,
bool from_dplane)
{
struct nhg_hash_entry *nhe = NULL;
struct nexthop_group nhg = {};
vrf_id_t vrf_id = !vrf_is_backend_netns() ? VRF_DEFAULT : nh->vrf_id;
nexthop_group_add_sorted(&nhg, nh);
zebra_nhg_find(&nhe, id, &nhg, NULL, vrf_id, afi, type, from_dplane);
if (IS_ZEBRA_DEBUG_NHG_DETAIL)
zlog_debug("%s: nh %pNHv => %p (%pNG)", __func__, nh, nhe, nhe);
return nhe;
}
static uint32_t nhg_ctx_get_id(const struct nhg_ctx *ctx)
{
return ctx->id;
}
static void nhg_ctx_set_status(struct nhg_ctx *ctx, enum nhg_ctx_status status)
{
ctx->status = status;
}
static enum nhg_ctx_status nhg_ctx_get_status(const struct nhg_ctx *ctx)
{
return ctx->status;
}
static void nhg_ctx_set_op(struct nhg_ctx *ctx, enum nhg_ctx_op_e op)
{
ctx->op = op;
}
static enum nhg_ctx_op_e nhg_ctx_get_op(const struct nhg_ctx *ctx)
{
return ctx->op;
}
static vrf_id_t nhg_ctx_get_vrf_id(const struct nhg_ctx *ctx)
{
return ctx->vrf_id;
}
static int nhg_ctx_get_type(const struct nhg_ctx *ctx)
{
return ctx->type;
}
static int nhg_ctx_get_afi(const struct nhg_ctx *ctx)
{
return ctx->afi;
}
static struct nexthop *nhg_ctx_get_nh(struct nhg_ctx *ctx)
{
return &ctx->u.nh;
}
static uint8_t nhg_ctx_get_count(const struct nhg_ctx *ctx)
{
return ctx->count;
}
static struct nh_grp *nhg_ctx_get_grp(struct nhg_ctx *ctx)
{
return ctx->u.grp;
}
static struct nhg_resilience *nhg_ctx_get_resilience(struct nhg_ctx *ctx)
{
return &ctx->resilience;
}
static struct nhg_ctx *nhg_ctx_new(void)
{
struct nhg_ctx *new;
new = XCALLOC(MTYPE_NHG_CTX, sizeof(struct nhg_ctx));
return new;
}
void nhg_ctx_free(struct nhg_ctx **ctx)