forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzebra_evpn_mh.c
4138 lines (3428 loc) · 110 KB
/
zebra_evpn_mh.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 EVPN multihoming code
*
* Copyright (C) 2019 Cumulus Networks, Inc.
* Anuradha Karuppiah
*/
#include <zebra.h>
#include "command.h"
#include "hash.h"
#include "if.h"
#include "jhash.h"
#include "linklist.h"
#include "log.h"
#include "memory.h"
#include "prefix.h"
#include "stream.h"
#include "table.h"
#include "vlan.h"
#include "vxlan.h"
#include "zebra/zebra_router.h"
#include "zebra/debug.h"
#include "zebra/interface.h"
#include "zebra/rib.h"
#include "zebra/rt.h"
#include "zebra/rt_netlink.h"
#include "zebra/if_netlink.h"
#include "zebra/zebra_errors.h"
#include "zebra/zebra_l2.h"
#include "zebra/zebra_l2_bridge_if.h"
#include "zebra/zebra_ns.h"
#include "zebra/zebra_vrf.h"
#include "zebra/zebra_vxlan.h"
#include "zebra/zebra_vxlan_private.h"
#include "zebra/zebra_evpn.h"
#include "zebra/zebra_evpn_mac.h"
#include "zebra/zebra_router.h"
#include "zebra/zebra_evpn_mh.h"
#include "zebra/zebra_nhg.h"
DEFINE_MTYPE_STATIC(ZEBRA, ZACC_BD, "Access Broadcast Domain");
DEFINE_MTYPE_STATIC(ZEBRA, ZES, "Ethernet Segment");
DEFINE_MTYPE_STATIC(ZEBRA, ZES_EVI, "ES info per-EVI");
DEFINE_MTYPE_STATIC(ZEBRA, ZMH_INFO, "MH global info");
DEFINE_MTYPE_STATIC(ZEBRA, ZES_VTEP, "VTEP attached to the ES");
DEFINE_MTYPE_STATIC(ZEBRA, L2_NH, "L2 nexthop");
static void zebra_evpn_es_get_one_base_evpn(void);
static int zebra_evpn_es_evi_send_to_client(struct zebra_evpn_es *es,
struct zebra_evpn *zevpn, bool add);
static void zebra_evpn_local_es_del(struct zebra_evpn_es **esp);
static int zebra_evpn_local_es_update(struct zebra_if *zif, esi_t *esi);
static bool zebra_evpn_es_br_port_dplane_update(struct zebra_evpn_es *es,
const char *caller);
static void zebra_evpn_mh_uplink_cfg_update(struct zebra_if *zif, bool set);
static void zebra_evpn_mh_update_protodown_es(struct zebra_evpn_es *es,
bool resync_dplane);
static void zebra_evpn_mh_clear_protodown_es(struct zebra_evpn_es *es);
static void zebra_evpn_mh_startup_delay_timer_start(const char *rc);
esi_t zero_esi_buf, *zero_esi = &zero_esi_buf;
/*****************************************************************************/
/* Ethernet Segment to EVI association -
* 1. The ES-EVI entry is maintained as a RB tree per L2-VNI
* (struct zebra_evpn.es_evi_rb_tree).
* 2. Each local ES-EVI entry is sent to BGP which advertises it as an
* EAD-EVI (Type-1 EVPN) route
* 3. Local ES-EVI setup is re-evaluated on the following triggers -
* a. When an ESI is set or cleared on an access port.
* b. When an access port associated with an ESI is deleted.
* c. When VLAN member ship changes on an access port.
* d. When a VXLAN_IF is set or cleared on an access broadcast domain.
* e. When a L2-VNI is added or deleted for a VxLAN_IF.
* 4. Currently zebra doesn't remote ES-EVIs. Those are managed and maintained
* entirely in BGP which consolidates them into a remote ES. The remote ES
* is then sent to zebra which allocates a NHG for it.
*/
/* compare ES-IDs for the ES-EVI RB tree maintained per-EVPN */
static int zebra_es_evi_rb_cmp(const struct zebra_evpn_es_evi *es_evi1,
const struct zebra_evpn_es_evi *es_evi2)
{
return memcmp(&es_evi1->es->esi, &es_evi2->es->esi, ESI_BYTES);
}
RB_GENERATE(zebra_es_evi_rb_head, zebra_evpn_es_evi,
rb_node, zebra_es_evi_rb_cmp);
/* allocate a new ES-EVI and insert it into the per-L2-VNI and per-ES
* tables.
*/
static struct zebra_evpn_es_evi *zebra_evpn_es_evi_new(struct zebra_evpn_es *es,
struct zebra_evpn *zevpn)
{
struct zebra_evpn_es_evi *es_evi;
es_evi = XCALLOC(MTYPE_ZES_EVI, sizeof(struct zebra_evpn_es_evi));
es_evi->es = es;
es_evi->zevpn = zevpn;
/* insert into the EVPN-ESI rb tree */
RB_INSERT(zebra_es_evi_rb_head, &zevpn->es_evi_rb_tree, es_evi);
/* add to the ES's VNI list */
listnode_init(&es_evi->es_listnode, es_evi);
listnode_add(es->es_evi_list, &es_evi->es_listnode);
if (IS_ZEBRA_DEBUG_EVPN_MH_ES)
zlog_debug("es %s evi %d new",
es_evi->es->esi_str, es_evi->zevpn->vni);
return es_evi;
}
/* Evaluate if the es_evi is ready to be sent BGP -
* 1. If it is ready an add is sent to BGP
* 2. If it is not ready a del is sent (if the ES had been previously added
* to BGP).
*/
static void zebra_evpn_es_evi_re_eval_send_to_client(
struct zebra_evpn_es_evi *es_evi)
{
bool old_ready;
bool new_ready;
old_ready = !!(es_evi->flags & ZEBRA_EVPNES_EVI_READY_FOR_BGP);
/* ES and L2-VNI have to be individually ready for BGP */
if ((es_evi->flags & ZEBRA_EVPNES_EVI_LOCAL) &&
(es_evi->es->flags & ZEBRA_EVPNES_READY_FOR_BGP) &&
zebra_evpn_send_to_client_ok(es_evi->zevpn))
es_evi->flags |= ZEBRA_EVPNES_EVI_READY_FOR_BGP;
else
es_evi->flags &= ~ZEBRA_EVPNES_EVI_READY_FOR_BGP;
new_ready = !!(es_evi->flags & ZEBRA_EVPNES_EVI_READY_FOR_BGP);
if (old_ready == new_ready)
return;
if (new_ready)
zebra_evpn_es_evi_send_to_client(es_evi->es, es_evi->zevpn,
true /* add */);
else
zebra_evpn_es_evi_send_to_client(es_evi->es, es_evi->zevpn,
false /* add */);
}
/* remove the ES-EVI from the per-L2-VNI and per-ES tables and free
* up the memory.
*/
static void zebra_evpn_es_evi_free(struct zebra_evpn_es_evi *es_evi)
{
struct zebra_evpn_es *es = es_evi->es;
struct zebra_evpn *zevpn = es_evi->zevpn;
if (IS_ZEBRA_DEBUG_EVPN_MH_ES)
zlog_debug("es %s evi %d free",
es_evi->es->esi_str, es_evi->zevpn->vni);
/* remove from the ES's VNI list */
list_delete_node(es->es_evi_list, &es_evi->es_listnode);
/* remove from the VNI-ESI rb tree */
RB_REMOVE(zebra_es_evi_rb_head, &zevpn->es_evi_rb_tree, es_evi);
/* remove from the VNI-ESI rb tree */
XFREE(MTYPE_ZES_EVI, es_evi);
}
/* find the ES-EVI in the per-L2-VNI RB tree */
struct zebra_evpn_es_evi *zebra_evpn_es_evi_find(struct zebra_evpn_es *es,
struct zebra_evpn *zevpn)
{
struct zebra_evpn_es_evi es_evi;
es_evi.es = es;
return RB_FIND(zebra_es_evi_rb_head, &zevpn->es_evi_rb_tree, &es_evi);
}
/* Tell BGP about an ES-EVI deletion and then delete it */
static void zebra_evpn_local_es_evi_do_del(struct zebra_evpn_es_evi *es_evi)
{
if (!(es_evi->flags & ZEBRA_EVPNES_EVI_LOCAL))
return;
if (IS_ZEBRA_DEBUG_EVPN_MH_ES)
zlog_debug("local es %s evi %d del",
es_evi->es->esi_str, es_evi->zevpn->vni);
if (es_evi->flags & ZEBRA_EVPNES_EVI_READY_FOR_BGP) {
/* send a del only if add was sent for it earlier */
zebra_evpn_es_evi_send_to_client(es_evi->es,
es_evi->zevpn, false /* add */);
}
/* delete it from the EVPN's local list */
list_delete_node(es_evi->zevpn->local_es_evi_list,
&es_evi->l2vni_listnode);
es_evi->flags &= ~ZEBRA_EVPNES_EVI_LOCAL;
zebra_evpn_es_evi_free(es_evi);
}
static void zebra_evpn_local_es_evi_del(struct zebra_evpn_es *es,
struct zebra_evpn *zevpn)
{
struct zebra_evpn_es_evi *es_evi;
es_evi = zebra_evpn_es_evi_find(es, zevpn);
if (es_evi)
zebra_evpn_local_es_evi_do_del(es_evi);
}
/* If there are any existing MAC entries for this es/zevpn we need
* to install it in the dataplane.
*
* Note: primary purpose of this is to handle es del/re-add windows where
* sync MAC entries may be added by bgpd before the es-evi membership is
* created in the dataplane and in zebra
*/
static void zebra_evpn_es_evi_mac_install(struct zebra_evpn_es_evi *es_evi)
{
struct zebra_mac *mac;
struct listnode *node;
struct zebra_evpn_es *es = es_evi->es;
if (listcount(es->mac_list) && IS_ZEBRA_DEBUG_EVPN_MH_ES)
zlog_debug("dp-mac install on es %s evi %d add", es->esi_str,
es_evi->zevpn->vni);
for (ALL_LIST_ELEMENTS_RO(es->mac_list, node, mac)) {
if (mac->zevpn != es_evi->zevpn)
continue;
if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL))
continue;
zebra_evpn_sync_mac_dp_install(mac, false, false, __func__);
}
}
/* Create an ES-EVI if it doesn't already exist and tell BGP */
static void zebra_evpn_local_es_evi_add(struct zebra_evpn_es *es,
struct zebra_evpn *zevpn)
{
struct zebra_evpn_es_evi *es_evi;
es_evi = zebra_evpn_es_evi_find(es, zevpn);
if (!es_evi) {
es_evi = zebra_evpn_es_evi_new(es, zevpn);
if (!es_evi)
return;
if (IS_ZEBRA_DEBUG_EVPN_MH_ES)
zlog_debug("local es %s evi %d add",
es_evi->es->esi_str, es_evi->zevpn->vni);
es_evi->flags |= ZEBRA_EVPNES_EVI_LOCAL;
/* add to the EVPN's local list */
listnode_init(&es_evi->l2vni_listnode, es_evi);
listnode_add(zevpn->local_es_evi_list, &es_evi->l2vni_listnode);
zebra_evpn_es_evi_re_eval_send_to_client(es_evi);
zebra_evpn_es_evi_mac_install(es_evi);
}
}
static void zebra_evpn_es_evi_show_entry(struct vty *vty,
struct zebra_evpn_es_evi *es_evi,
json_object *json_array)
{
char type_str[4];
if (json_array) {
json_object *json;
json_object *json_types;
/* Separate JSON object for each es-evi entry */
json = json_object_new_object();
json_object_string_add(json, "esi", es_evi->es->esi_str);
json_object_int_add(json, "vni", es_evi->zevpn->vni);
if (es_evi->flags & ZEBRA_EVPNES_EVI_LOCAL) {
json_types = json_object_new_array();
if (es_evi->flags & ZEBRA_EVPNES_EVI_LOCAL)
json_array_string_add(json_types, "local");
json_object_object_add(json, "type", json_types);
}
/* Add es-evi entry to json array */
json_object_array_add(json_array, json);
} else {
type_str[0] = '\0';
if (es_evi->flags & ZEBRA_EVPNES_EVI_LOCAL)
strlcat(type_str, "L", sizeof(type_str));
vty_out(vty, "%-8d %-30s %-4s\n",
es_evi->zevpn->vni, es_evi->es->esi_str,
type_str);
}
}
static void
zebra_evpn_es_evi_show_entry_detail(struct vty *vty,
struct zebra_evpn_es_evi *es_evi,
json_object *json_array)
{
char type_str[4];
if (json_array) {
json_object *json;
json_object *json_flags;
/* Separate JSON object for each es-evi entry */
json = json_object_new_object();
json_object_string_add(json, "esi", es_evi->es->esi_str);
json_object_int_add(json, "vni", es_evi->zevpn->vni);
if (es_evi->flags
& (ZEBRA_EVPNES_EVI_LOCAL
| ZEBRA_EVPNES_EVI_READY_FOR_BGP)) {
json_flags = json_object_new_array();
if (es_evi->flags & ZEBRA_EVPNES_EVI_LOCAL)
json_array_string_add(json_flags, "local");
if (es_evi->flags & ZEBRA_EVPNES_EVI_READY_FOR_BGP)
json_array_string_add(json_flags,
"readyForBgp");
json_object_object_add(json, "flags", json_flags);
}
/* Add es-evi entry to json array */
json_object_array_add(json_array, json);
} else {
type_str[0] = '\0';
if (es_evi->flags & ZEBRA_EVPNES_EVI_LOCAL)
strlcat(type_str, "L", sizeof(type_str));
vty_out(vty, "VNI %d ESI: %s\n",
es_evi->zevpn->vni, es_evi->es->esi_str);
vty_out(vty, " Type: %s\n", type_str);
vty_out(vty, " Ready for BGP: %s\n",
(es_evi->flags &
ZEBRA_EVPNES_EVI_READY_FOR_BGP) ?
"yes" : "no");
vty_out(vty, "\n");
}
}
static void zebra_evpn_es_evi_show_one_evpn(struct zebra_evpn *zevpn,
struct vty *vty,
json_object *json_array, int detail)
{
struct zebra_evpn_es_evi *es_evi;
RB_FOREACH(es_evi, zebra_es_evi_rb_head, &zevpn->es_evi_rb_tree) {
if (detail)
zebra_evpn_es_evi_show_entry_detail(vty, es_evi,
json_array);
else
zebra_evpn_es_evi_show_entry(vty, es_evi, json_array);
}
}
struct evpn_mh_show_ctx {
struct vty *vty;
json_object *json;
int detail;
};
static void zebra_evpn_es_evi_show_one_evpn_hash_cb(struct hash_bucket *bucket,
void *ctxt)
{
struct zebra_evpn *zevpn = (struct zebra_evpn *)bucket->data;
struct evpn_mh_show_ctx *wctx = (struct evpn_mh_show_ctx *)ctxt;
zebra_evpn_es_evi_show_one_evpn(zevpn, wctx->vty,
wctx->json, wctx->detail);
}
void zebra_evpn_es_evi_show(struct vty *vty, bool uj, int detail)
{
json_object *json_array = NULL;
struct zebra_vrf *zvrf;
struct evpn_mh_show_ctx wctx;
zvrf = zebra_vrf_get_evpn();
if (uj)
json_array = json_object_new_array();
memset(&wctx, 0, sizeof(wctx));
wctx.vty = vty;
wctx.json = json_array;
wctx.detail = detail;
if (!detail && !json_array) {
vty_out(vty, "Type: L local, R remote\n");
vty_out(vty, "%-8s %-30s %-4s\n", "VNI", "ESI", "Type");
}
/* Display all L2-VNIs */
hash_iterate(zvrf->evpn_table, zebra_evpn_es_evi_show_one_evpn_hash_cb,
&wctx);
if (uj)
vty_json(vty, json_array);
}
void zebra_evpn_es_evi_show_vni(struct vty *vty, bool uj, vni_t vni, int detail)
{
json_object *json_array = NULL;
struct zebra_evpn *zevpn;
zevpn = zebra_evpn_lookup(vni);
if (uj)
json_array = json_object_new_array();
if (zevpn) {
if (!detail && !json_array) {
vty_out(vty, "Type: L local, R remote\n");
vty_out(vty, "%-8s %-30s %-4s\n", "VNI", "ESI", "Type");
}
zebra_evpn_es_evi_show_one_evpn(zevpn, vty, json_array, detail);
} else {
if (!uj)
vty_out(vty, "VNI %d doesn't exist\n", vni);
}
if (uj)
vty_json(vty, json_array);
}
/* Initialize the ES tables maintained per-L2_VNI */
void zebra_evpn_es_evi_init(struct zebra_evpn *zevpn)
{
/* Initialize the ES-EVI RB tree */
RB_INIT(zebra_es_evi_rb_head, &zevpn->es_evi_rb_tree);
/* Initialize the local and remote ES lists maintained for quick
* walks by type
*/
zevpn->local_es_evi_list = list_new();
listset_app_node_mem(zevpn->local_es_evi_list);
}
/* Cleanup the ES info maintained per- EVPN */
void zebra_evpn_es_evi_cleanup(struct zebra_evpn *zevpn)
{
struct zebra_evpn_es_evi *es_evi;
struct zebra_evpn_es_evi *es_evi_next;
RB_FOREACH_SAFE(es_evi, zebra_es_evi_rb_head,
&zevpn->es_evi_rb_tree, es_evi_next) {
zebra_evpn_local_es_evi_do_del(es_evi);
}
list_delete(&zevpn->local_es_evi_list);
zebra_evpn_es_clear_base_evpn(zevpn);
}
/* called when the oper state or bridge membership changes for the
* vxlan device
*/
void zebra_evpn_update_all_es(struct zebra_evpn *zevpn)
{
struct zebra_evpn_es_evi *es_evi;
struct listnode *node;
struct interface *vlan_if;
struct interface *vxlan_if;
struct zebra_if *vxlan_zif;
struct zebra_vxlan_vni *vni;
/* the EVPN is now elgible as a base for EVPN-MH */
if (zebra_evpn_send_to_client_ok(zevpn))
zebra_evpn_es_set_base_evpn(zevpn);
else
zebra_evpn_es_clear_base_evpn(zevpn);
for (ALL_LIST_ELEMENTS_RO(zevpn->local_es_evi_list, node, es_evi))
zebra_evpn_es_evi_re_eval_send_to_client(es_evi);
/* reinstall SVI MAC */
vxlan_if = zevpn->vxlan_if;
if (vxlan_if) {
vxlan_zif = vxlan_if->info;
if (if_is_operative(vxlan_if)
&& vxlan_zif->brslave_info.br_if) {
vni = zebra_vxlan_if_vni_find(vxlan_zif, zevpn->vni);
/* VLAN-VNI mappings may not exist */
if (vni) {
vlan_if = zvni_map_to_svi(
vni->access_vlan,
vxlan_zif->brslave_info.br_if);
if (vlan_if)
zebra_evpn_acc_bd_svi_mac_add(vlan_if);
}
}
}
}
/*****************************************************************************/
/* Access broadcast domains (BD)
* 1. These broadcast domains can be VLAN aware (in which case
* the key is VID) or VLAN unaware (in which case the key is
* 2. A VID-BD is created when a VLAN is associated with an access port or
* when the VLAN is associated with VXLAN_IF
* 3. A BD is translated into ES-EVI entries when a VNI is associated
* with the broadcast domain
*/
/* Hash key for VLAN based broadcast domains */
static unsigned int zebra_evpn_acc_vl_hash_keymake(const void *p)
{
const struct zebra_evpn_access_bd *acc_bd = p;
return jhash_2words(acc_bd->vid, acc_bd->bridge_ifindex, 0);
}
/* Compare two VLAN based broadcast domains */
static bool zebra_evpn_acc_vl_cmp(const void *p1, const void *p2)
{
const struct zebra_evpn_access_bd *acc_bd1 = p1;
const struct zebra_evpn_access_bd *acc_bd2 = p2;
if (acc_bd1 == NULL && acc_bd2 == NULL)
return true;
if (acc_bd1 == NULL || acc_bd2 == NULL)
return false;
return ((acc_bd1->vid == acc_bd2->vid) &&
(acc_bd1->bridge_ifindex == acc_bd2->bridge_ifindex));
}
/* Lookup VLAN based broadcast domain */
struct zebra_evpn_access_bd *
zebra_evpn_acc_vl_find_index(vlanid_t vid, ifindex_t bridge_ifindex)
{
struct zebra_evpn_access_bd *acc_bd;
struct zebra_evpn_access_bd tmp;
tmp.vid = vid;
tmp.bridge_ifindex = bridge_ifindex;
acc_bd = hash_lookup(zmh_info->evpn_vlan_table, &tmp);
return acc_bd;
}
/* Lookup VLAN based broadcast domain */
struct zebra_evpn_access_bd *zebra_evpn_acc_vl_find(vlanid_t vid,
struct interface *br_if)
{
return zebra_evpn_acc_vl_find_index(vid, br_if->ifindex);
}
/* A new broadcast domain can be created when a VLAN member or VLAN<=>VxLAN_IF
* mapping is added.
*/
static struct zebra_evpn_access_bd *
zebra_evpn_acc_vl_new(vlanid_t vid, struct interface *br_if)
{
struct zebra_evpn_access_bd *acc_bd;
struct interface *vlan_if;
if (IS_ZEBRA_DEBUG_EVPN_MH_ES)
zlog_debug("access vlan %d bridge %s add", vid, br_if->name);
acc_bd = XCALLOC(MTYPE_ZACC_BD, sizeof(struct zebra_evpn_access_bd));
acc_bd->vid = vid;
acc_bd->bridge_ifindex = br_if->ifindex;
acc_bd->bridge_zif = (struct zebra_if *)br_if->info;
/* Initialize the mbr list */
acc_bd->mbr_zifs = list_new();
/* Add to hash */
(void)hash_get(zmh_info->evpn_vlan_table, acc_bd, hash_alloc_intern);
/* check if an svi exists for the vlan */
vlan_if = zvni_map_to_svi(vid, br_if);
if (vlan_if) {
if (IS_ZEBRA_DEBUG_EVPN_MH_ES)
zlog_debug("vlan %d bridge %s SVI %s set", vid,
br_if->name, vlan_if->name);
acc_bd->vlan_zif = vlan_if->info;
}
return acc_bd;
}
/* Free VLAN based broadcast domain -
* This just frees appropriate memory, caller should have taken other
* needed actions.
*/
static void zebra_evpn_acc_vl_free(struct zebra_evpn_access_bd *acc_bd)
{
if (IS_ZEBRA_DEBUG_EVPN_MH_ES)
zlog_debug("access vlan %d del", acc_bd->vid);
if (acc_bd->vlan_zif && acc_bd->zevpn && acc_bd->zevpn->mac_table)
zebra_evpn_mac_svi_del(acc_bd->vlan_zif->ifp, acc_bd->zevpn);
/* cleanup resources maintained against the ES */
list_delete(&acc_bd->mbr_zifs);
/* remove EVI from various tables */
hash_release(zmh_info->evpn_vlan_table, acc_bd);
XFREE(MTYPE_ZACC_BD, acc_bd);
}
static void zebra_evpn_acc_vl_cleanup_all(struct hash_bucket *bucket, void *arg)
{
struct zebra_evpn_access_bd *acc_bd = bucket->data;
zebra_evpn_acc_vl_free(acc_bd);
}
/* called when a bd mbr is removed or VxLAN_IF is diassociated from the access
* VLAN
*/
static void zebra_evpn_acc_bd_free_on_deref(struct zebra_evpn_access_bd *acc_bd)
{
if (!list_isempty(acc_bd->mbr_zifs) || acc_bd->vxlan_zif)
return;
/* Remove this access_bd from bridge hash table */
zebra_l2_bridge_if_vlan_access_bd_deref(acc_bd);
/* if there are no references free the EVI */
zebra_evpn_acc_vl_free(acc_bd);
}
static struct zebra_evpn_access_bd *
zebra_evpn_acc_bd_alloc_on_ref(vlanid_t vid, struct interface *br_if)
{
struct zebra_evpn_access_bd *acc_bd = NULL;
assert(br_if && br_if->info);
acc_bd = zebra_evpn_acc_vl_new(vid, br_if);
if (acc_bd)
/* Add this access_bd to bridge hash table */
zebra_l2_bridge_if_vlan_access_bd_ref(acc_bd);
return acc_bd;
}
/* called when a SVI is goes up/down */
void zebra_evpn_acc_bd_svi_set(struct zebra_if *vlan_zif,
struct zebra_if *br_zif, bool is_up)
{
struct zebra_evpn_access_bd *acc_bd;
uint16_t vid;
struct zebra_if *tmp_br_zif = br_zif;
if (!tmp_br_zif) {
if (!vlan_zif->link || !vlan_zif->link->info)
return;
tmp_br_zif = vlan_zif->link->info;
}
/* ignore vlan unaware bridges */
if (!IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(tmp_br_zif))
return;
vid = vlan_zif->l2info.vl.vid;
acc_bd = zebra_evpn_acc_vl_find(vid, tmp_br_zif->ifp);
if (!acc_bd)
return;
if (is_up) {
if (IS_ZEBRA_DEBUG_EVPN_MH_ES)
zlog_debug("vlan %d bridge %s SVI %s set", vid,
tmp_br_zif->ifp->name, vlan_zif->ifp->name);
acc_bd->vlan_zif = vlan_zif;
if (acc_bd->zevpn)
zebra_evpn_mac_svi_add(acc_bd->vlan_zif->ifp,
acc_bd->zevpn);
} else if (acc_bd->vlan_zif) {
if (IS_ZEBRA_DEBUG_EVPN_MH_ES)
zlog_debug("vlan %d bridge %s SVI clear", vid,
tmp_br_zif->ifp->name);
acc_bd->vlan_zif = NULL;
if (acc_bd->zevpn && acc_bd->zevpn->mac_table)
zebra_evpn_mac_svi_del(vlan_zif->ifp, acc_bd->zevpn);
}
}
/* On some events macs are force-flushed. This api can be used to reinstate
* the svi-mac after such cleanup-events.
*/
void zebra_evpn_acc_bd_svi_mac_add(struct interface *vlan_if)
{
zebra_evpn_acc_bd_svi_set(vlan_if->info, NULL,
if_is_operative(vlan_if));
}
/* called when a EVPN-L2VNI is set or cleared against a BD */
static void zebra_evpn_acc_bd_evpn_set(struct zebra_evpn_access_bd *acc_bd,
struct zebra_evpn *zevpn,
struct zebra_evpn *old_zevpn)
{
struct zebra_if *zif;
struct listnode *node;
if (IS_ZEBRA_DEBUG_EVPN_MH_ES)
zlog_debug("access vlan %d bridge %s l2-vni %u set",
acc_bd->vid, acc_bd->bridge_zif->ifp->name,
zevpn ? zevpn->vni : 0);
for (ALL_LIST_ELEMENTS_RO(acc_bd->mbr_zifs, node, zif)) {
if (!zif->es_info.es)
continue;
if (zevpn)
zebra_evpn_local_es_evi_add(zif->es_info.es, zevpn);
else if (old_zevpn)
zebra_evpn_local_es_evi_del(zif->es_info.es, old_zevpn);
}
if (acc_bd->vlan_zif) {
if (zevpn)
zebra_evpn_mac_svi_add(acc_bd->vlan_zif->ifp,
acc_bd->zevpn);
else if (old_zevpn && old_zevpn->mac_table)
zebra_evpn_mac_svi_del(acc_bd->vlan_zif->ifp,
old_zevpn);
}
}
/* handle VLAN->VxLAN_IF association */
void zebra_evpn_vl_vxl_ref(uint16_t vid, vni_t vni_id,
struct zebra_if *vxlan_zif)
{
vni_t old_vni;
struct zebra_evpn_access_bd *acc_bd;
struct zebra_evpn *old_zevpn;
struct interface *br_if;
if (!vid)
return;
if (!vni_id)
return;
br_if = vxlan_zif->brslave_info.br_if;
if (!br_if)
return;
acc_bd = zebra_evpn_acc_vl_find(vid, br_if);
if (!acc_bd)
acc_bd = zebra_evpn_acc_bd_alloc_on_ref(vid, br_if);
old_vni = acc_bd->vni;
if (vni_id == old_vni)
return;
acc_bd->vni = vni_id;
acc_bd->vxlan_zif = vxlan_zif;
old_zevpn = acc_bd->zevpn;
acc_bd->zevpn = zebra_evpn_lookup(vni_id);
if (acc_bd->zevpn == old_zevpn)
return;
if (IS_ZEBRA_DEBUG_EVPN_MH_ES)
zlog_debug("access vlan %d vni %u ref", acc_bd->vid, vni_id);
if (old_zevpn)
zebra_evpn_acc_bd_evpn_set(acc_bd, NULL, old_zevpn);
if (acc_bd->zevpn)
zebra_evpn_acc_bd_evpn_set(acc_bd, acc_bd->zevpn, NULL);
}
/* handle VLAN->VxLAN_IF deref */
void zebra_evpn_vl_vxl_deref(uint16_t vid, vni_t vni_id,
struct zebra_if *vxlan_zif)
{
struct interface *br_if;
struct zebra_evpn_access_bd *acc_bd;
if (!vid)
return;
if (!vni_id)
return;
br_if = vxlan_zif->brslave_info.br_if;
if (!br_if)
return;
acc_bd = zebra_evpn_acc_vl_find(vid, br_if);
if (!acc_bd)
return;
/* clear vxlan_if only if it matches */
if (acc_bd->vni != vni_id)
return;
if (IS_ZEBRA_DEBUG_EVPN_MH_ES)
zlog_debug("access vlan %d bridge %s vni %u deref", acc_bd->vid,
br_if->name, vni_id);
if (acc_bd->zevpn)
zebra_evpn_acc_bd_evpn_set(acc_bd, NULL, acc_bd->zevpn);
acc_bd->zevpn = NULL;
acc_bd->vxlan_zif = NULL;
acc_bd->vni = 0;
/* if there are no other references the access_bd can be freed */
zebra_evpn_acc_bd_free_on_deref(acc_bd);
}
/* handle BridgeIf<->AccessBD cleanup */
void zebra_evpn_access_bd_bridge_cleanup(vlanid_t vid, struct interface *br_if,
struct zebra_evpn_access_bd *acc_bd)
{
struct zebra_evpn *zevpn;
if (IS_ZEBRA_DEBUG_EVPN_MH_ES)
zlog_debug("access bd vlan %d bridge %s cleanup", acc_bd->vid,
br_if->name);
zevpn = acc_bd->zevpn;
if (zevpn)
zebra_evpn_acc_bd_evpn_set(acc_bd, NULL, zevpn);
/* cleanup resources maintained against the ES */
list_delete_all_node(acc_bd->mbr_zifs);
acc_bd->zevpn = NULL;
acc_bd->vxlan_zif = NULL;
acc_bd->vni = 0;
acc_bd->bridge_zif = NULL;
/* if there are no other references the access_bd can be freed */
zebra_evpn_acc_bd_free_on_deref(acc_bd);
}
/* handle EVPN add/del */
void zebra_evpn_vxl_evpn_set(struct zebra_if *zif, struct zebra_evpn *zevpn,
bool set)
{
struct zebra_vxlan_vni *vni;
struct zebra_evpn_access_bd *acc_bd;
ifindex_t br_ifindex;
if (!zif)
return;
/* locate access_bd associated with the vxlan device */
vni = zebra_vxlan_if_vni_find(zif, zevpn->vni);
if (!vni)
return;
/* Use the index as the pointer can be stale (deleted) */
br_ifindex = zif->brslave_info.bridge_ifindex;
if (!zif->brslave_info.br_if || br_ifindex == IFINDEX_INTERNAL)
return;
acc_bd = zebra_evpn_acc_vl_find_index(vni->access_vlan, br_ifindex);
if (!acc_bd)
return;
if (set) {
zebra_evpn_es_set_base_evpn(zevpn);
if (acc_bd->zevpn != zevpn) {
acc_bd->zevpn = zevpn;
zebra_evpn_acc_bd_evpn_set(acc_bd, zevpn, NULL);
}
} else {
if (acc_bd->zevpn) {
struct zebra_evpn *old_zevpn = acc_bd->zevpn;
acc_bd->zevpn = NULL;
zebra_evpn_acc_bd_evpn_set(acc_bd, NULL, old_zevpn);
}
}
}
/* handle addition of new VLAN members */
void zebra_evpn_vl_mbr_ref(uint16_t vid, struct zebra_if *zif)
{
struct interface *br_if;
struct zebra_evpn_access_bd *acc_bd;
if (!vid)
return;
br_if = zif->brslave_info.br_if;
if (!br_if)
return;
acc_bd = zebra_evpn_acc_vl_find(vid, br_if);
if (!acc_bd)
acc_bd = zebra_evpn_acc_bd_alloc_on_ref(vid, br_if);
if (listnode_lookup(acc_bd->mbr_zifs, zif))
return;
if (IS_ZEBRA_DEBUG_EVPN_MH_ES)
zlog_debug("access vlan %d bridge %s mbr %s ref", vid,
br_if->name, zif->ifp->name);
listnode_add(acc_bd->mbr_zifs, zif);
if (acc_bd->zevpn && zif->es_info.es)
zebra_evpn_local_es_evi_add(zif->es_info.es, acc_bd->zevpn);
}
/* handle deletion of VLAN members */
void zebra_evpn_vl_mbr_deref(uint16_t vid, struct zebra_if *zif)
{
struct interface *br_if;
struct zebra_evpn_access_bd *acc_bd;
struct listnode *node;
if (!vid)
return;
br_if = zif->brslave_info.br_if;
if (!br_if)
return;
acc_bd = zebra_evpn_acc_vl_find(vid, br_if);
if (!acc_bd)
return;
node = listnode_lookup(acc_bd->mbr_zifs, zif);
if (!node)
return;
if (IS_ZEBRA_DEBUG_EVPN_MH_ES)
zlog_debug("access vlan %d bridge %s mbr %s deref", vid,
br_if->name, zif->ifp->name);
list_delete_node(acc_bd->mbr_zifs, node);
if (acc_bd->zevpn && zif->es_info.es)
zebra_evpn_local_es_evi_del(zif->es_info.es, acc_bd->zevpn);
/* if there are no other references the access_bd can be freed */
zebra_evpn_acc_bd_free_on_deref(acc_bd);
}
static void zebra_evpn_acc_vl_adv_svi_mac_cb(struct hash_bucket *bucket,
void *ctxt)
{
struct zebra_evpn_access_bd *acc_bd = bucket->data;
if (acc_bd->vlan_zif && acc_bd->zevpn)
zebra_evpn_mac_svi_add(acc_bd->vlan_zif->ifp, acc_bd->zevpn);
}
/* called when advertise SVI MAC is enabled on the switch */
static void zebra_evpn_acc_vl_adv_svi_mac_all(void)
{
hash_iterate(zmh_info->evpn_vlan_table,
zebra_evpn_acc_vl_adv_svi_mac_cb, NULL);
}
static void zebra_evpn_acc_vl_json_fill(struct zebra_evpn_access_bd *acc_bd,
json_object *json, bool detail)
{
json_object_int_add(json, "vlan", acc_bd->vid);
if (acc_bd->vxlan_zif)
json_object_string_add(json, "vxlanIf",
acc_bd->vxlan_zif->ifp->name);
if (acc_bd->zevpn)
json_object_int_add(json, "vni", acc_bd->zevpn->vni);
if (acc_bd->mbr_zifs)
json_object_int_add(json, "memberIfCount",
listcount(acc_bd->mbr_zifs));
if (detail) {
json_object *json_mbrs;
json_object *json_mbr;
struct zebra_if *zif;
struct listnode *node;
json_mbrs = json_object_new_array();
for (ALL_LIST_ELEMENTS_RO(acc_bd->mbr_zifs, node, zif)) {
json_mbr = json_object_new_object();
json_object_string_add(json_mbr, "ifName",
zif->ifp->name);
json_object_array_add(json_mbrs, json_mbr);
}
json_object_object_add(json, "members", json_mbrs);
}
}
static void zebra_evpn_acc_vl_show_entry_detail(struct vty *vty,
struct zebra_evpn_access_bd *acc_bd, json_object *json)