forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbgp_evpn_mh.c
5095 lines (4267 loc) · 142 KB
/
bgp_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
/* EVPN Multihoming procedures
*
* Copyright (C) 2019 Cumulus Networks, Inc.
* Anuradha Karuppiah
*
*/
#include <zebra.h>
#include "command.h"
#include "filter.h"
#include "prefix.h"
#include "log.h"
#include "memory.h"
#include "stream.h"
#include "hash.h"
#include "jhash.h"
#include "zclient.h"
#include "lib/printfrr.h"
#include "bgpd/bgp_attr_evpn.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_table.h"
#include "bgpd/bgp_route.h"
#include "bgpd/bgp_attr.h"
#include "bgpd/bgp_mplsvpn.h"
#include "bgpd/bgp_evpn.h"
#include "bgpd/bgp_evpn_private.h"
#include "bgpd/bgp_evpn_mh.h"
#include "bgpd/bgp_ecommunity.h"
#include "bgpd/bgp_encap_types.h"
#include "bgpd/bgp_debug.h"
#include "bgpd/bgp_errors.h"
#include "bgpd/bgp_aspath.h"
#include "bgpd/bgp_zebra.h"
#include "bgpd/bgp_addpath.h"
#include "bgpd/bgp_label.h"
#include "bgpd/bgp_nhg.h"
#include "bgpd/bgp_mpath.h"
#include "bgpd/bgp_trace.h"
static void bgp_evpn_local_es_down(struct bgp *bgp,
struct bgp_evpn_es *es);
static void bgp_evpn_local_type1_evi_route_del(struct bgp *bgp,
struct bgp_evpn_es *es);
static struct bgp_evpn_es_vtep *
bgp_evpn_es_vtep_add(struct bgp *bgp, struct bgp_evpn_es *es,
struct in_addr vtep_ip, bool esr, uint8_t df_alg,
uint16_t df_pref, int *zret);
static enum zclient_send_status bgp_evpn_es_vtep_del(struct bgp *bgp,
struct bgp_evpn_es *es,
struct in_addr vtep_ip,
bool esr);
static void bgp_evpn_es_cons_checks_pend_add(struct bgp_evpn_es *es);
static void bgp_evpn_es_cons_checks_pend_del(struct bgp_evpn_es *es);
static struct bgp_evpn_es_evi *
bgp_evpn_local_es_evi_do_del(struct bgp_evpn_es_evi *es_evi);
static uint32_t bgp_evpn_es_get_active_vtep_cnt(struct bgp_evpn_es *es);
static void bgp_evpn_l3nhg_update_on_vtep_chg(struct bgp_evpn_es *es);
static struct bgp_evpn_es *bgp_evpn_es_new(struct bgp *bgp, const esi_t *esi);
static void bgp_evpn_es_free(struct bgp_evpn_es *es, const char *caller);
static void bgp_evpn_path_es_unlink(struct bgp_path_es_info *es_info);
static void bgp_evpn_mac_update_on_es_local_chg(struct bgp_evpn_es *es,
bool is_local);
esi_t zero_esi_buf, *zero_esi = &zero_esi_buf;
static void bgp_evpn_run_consistency_checks(struct event *t);
static void bgp_evpn_path_nh_info_free(struct bgp_path_evpn_nh_info *nh_info);
static void bgp_evpn_path_nh_unlink(struct bgp_path_evpn_nh_info *nh_info);
/******************************************************************************
* per-ES (Ethernet Segment) routing table
*
* Following routes are added to the ES's routing table -
* 1. Local and remote ESR (Type-4)
* 2. Local EAD-per-ES (Type-1).
*
* Key for these routes is {ESI, VTEP-IP} so the path selection is practically
* a no-op i.e. all paths lead to same VTEP-IP (i.e. result in the same VTEP
* being added to same ES).
*
* Note the following routes go into the VNI routing table (instead of the
* ES routing table) -
* 1. Remote EAD-per-ES
* 2. Local and remote EAD-per-EVI
*/
/* Calculate the best path for a multi-homing (Type-1 or Type-4) route
* installed in the ES's routing table.
*/
static int bgp_evpn_es_route_select_install(struct bgp *bgp,
struct bgp_evpn_es *es,
struct bgp_dest *dest,
struct bgp_path_info *pi)
{
int ret = 0;
int zret = 0;
afi_t afi = AFI_L2VPN;
safi_t safi = SAFI_EVPN;
struct bgp_path_info *old_select; /* old best */
struct bgp_path_info *new_select; /* new best */
struct bgp_path_info_pair old_and_new;
SET_FLAG(pi->flags, BGP_PATH_UNSORTED);
/* Compute the best path. */
bgp_best_selection(bgp, dest, &bgp->maxpaths[afi][safi], &old_and_new,
afi, safi);
old_select = old_and_new.old;
new_select = old_and_new.new;
/*
* If the best path hasn't changed - see if something needs to be
* updated
*/
if (old_select && old_select == new_select
&& old_select->type == ZEBRA_ROUTE_BGP
&& old_select->sub_type == BGP_ROUTE_IMPORTED
&& !CHECK_FLAG(dest->flags, BGP_NODE_USER_CLEAR)
&& !CHECK_FLAG(old_select->flags, BGP_PATH_ATTR_CHANGED)
&& !bgp_addpath_is_addpath_used(&bgp->tx_addpath, afi, safi)) {
if (bgp_zebra_has_route_changed(old_select)) {
bgp_evpn_es_vtep_add(bgp, es, old_select->attr->nexthop,
true /*esr*/,
old_select->attr->df_alg,
old_select->attr->df_pref, &zret);
}
UNSET_FLAG(old_select->flags, BGP_PATH_MULTIPATH_CHG);
bgp_zebra_clear_route_change_flags(dest);
return ret;
}
/* If the user did a "clear" this flag will be set */
UNSET_FLAG(dest->flags, BGP_NODE_USER_CLEAR);
/* bestpath has changed; update relevant fields and install or uninstall
* into the zebra RIB.
*/
if (old_select || new_select)
bgp_bump_version(dest);
if (old_select)
bgp_path_info_unset_flag(dest, old_select, BGP_PATH_SELECTED);
if (new_select) {
bgp_path_info_set_flag(dest, new_select, BGP_PATH_SELECTED);
bgp_path_info_unset_flag(dest, new_select,
BGP_PATH_ATTR_CHANGED);
UNSET_FLAG(new_select->flags, BGP_PATH_MULTIPATH_CHG);
}
if (new_select && new_select->type == ZEBRA_ROUTE_BGP
&& new_select->sub_type == BGP_ROUTE_IMPORTED) {
bgp_evpn_es_vtep_add(bgp, es, new_select->attr->nexthop,
true /*esr */, new_select->attr->df_alg,
new_select->attr->df_pref, &zret);
} else {
if (old_select && old_select->type == ZEBRA_ROUTE_BGP
&& old_select->sub_type == BGP_ROUTE_IMPORTED)
bgp_evpn_es_vtep_del(
bgp, es, old_select->attr->nexthop,
true /*esr*/);
}
/* Clear any route change flags. */
bgp_zebra_clear_route_change_flags(dest);
/* Reap old select bgp_path_info, if it has been removed */
if (old_select && CHECK_FLAG(old_select->flags, BGP_PATH_REMOVED))
bgp_path_info_reap(dest, old_select);
return ret;
}
/* Install Type-1/Type-4 route entry in the per-ES routing table */
static int bgp_evpn_es_route_install(struct bgp *bgp,
struct bgp_evpn_es *es, struct prefix_evpn *p,
struct bgp_path_info *parent_pi)
{
int ret = 0;
struct bgp_dest *dest = NULL;
struct bgp_path_info *pi = NULL;
struct attr *attr_new = NULL;
/* Create (or fetch) route within the VNI.
* NOTE: There is no RD here.
*/
dest = bgp_node_get(es->route_table, (struct prefix *)p);
/* Check if route entry is already present. */
for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
if (pi->extra && pi->extra->vrfleak &&
(struct bgp_path_info *)pi->extra->vrfleak->parent ==
parent_pi)
break;
if (!pi) {
/* Add (or update) attribute to hash. */
attr_new = bgp_attr_intern(parent_pi->attr);
/* Create new route with its attribute. */
pi = info_make(parent_pi->type, BGP_ROUTE_IMPORTED, 0,
parent_pi->peer, attr_new, dest);
SET_FLAG(pi->flags, BGP_PATH_VALID);
bgp_path_info_extra_get(pi);
if (!pi->extra->vrfleak)
pi->extra->vrfleak =
XCALLOC(MTYPE_BGP_ROUTE_EXTRA_VRFLEAK,
sizeof(struct bgp_path_info_extra_vrfleak));
pi->extra->vrfleak->parent = bgp_path_info_lock(parent_pi);
bgp_dest_lock_node((struct bgp_dest *)parent_pi->net);
bgp_path_info_add(dest, pi);
} else {
if (attrhash_cmp(pi->attr, parent_pi->attr)
&& !CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
bgp_dest_unlock_node(dest);
return 0;
}
/* The attribute has changed. */
/* Add (or update) attribute to hash. */
attr_new = bgp_attr_intern(parent_pi->attr);
/* Restore route, if needed. */
if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
bgp_path_info_restore(dest, pi);
/* Mark if nexthop has changed. */
if (!IPV4_ADDR_SAME(&pi->attr->nexthop, &attr_new->nexthop))
SET_FLAG(pi->flags, BGP_PATH_IGP_CHANGED);
/* Unintern existing, set to new. */
bgp_attr_unintern(&pi->attr);
pi->attr = attr_new;
pi->uptime = monotime(NULL);
}
/* Perform route selection and update zebra, if required. */
ret = bgp_evpn_es_route_select_install(bgp, es, dest, pi);
bgp_dest_unlock_node(dest);
return ret;
}
/* Uninstall Type-1/Type-4 route entry from the ES routing table */
static int bgp_evpn_es_route_uninstall(struct bgp *bgp, struct bgp_evpn_es *es,
struct prefix_evpn *p, struct bgp_path_info *parent_pi)
{
int ret;
struct bgp_dest *dest;
struct bgp_path_info *pi;
if (!es->route_table)
return 0;
/* Locate route within the ESI.
* NOTE: There is no RD here.
*/
dest = bgp_node_lookup(es->route_table, (struct prefix *)p);
if (!dest)
return 0;
/* Find matching route entry. */
for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
if (pi->extra && pi->extra->vrfleak &&
(struct bgp_path_info *)pi->extra->vrfleak->parent ==
parent_pi)
break;
if (!pi) {
bgp_dest_unlock_node(dest);
return 0;
}
/* Mark entry for deletion */
bgp_path_info_delete(dest, pi);
/* Perform route selection and update zebra, if required. */
ret = bgp_evpn_es_route_select_install(bgp, es, dest, pi);
/* Unlock route node. */
bgp_dest_unlock_node(dest);
return ret;
}
/* Install or unistall a Type-4 route in the per-ES routing table */
int bgp_evpn_es_route_install_uninstall(struct bgp *bgp, struct bgp_evpn_es *es,
afi_t afi, safi_t safi, struct prefix_evpn *evp,
struct bgp_path_info *pi, int install)
{
int ret = 0;
if (install)
ret = bgp_evpn_es_route_install(bgp, es, evp, pi);
else
ret = bgp_evpn_es_route_uninstall(bgp, es, evp, pi);
if (ret) {
flog_err(
EC_BGP_EVPN_FAIL,
"%u: Failed to %s EVPN %s route in ESI %s",
bgp->vrf_id,
install ? "install" : "uninstall",
"ES", es->esi_str);
return ret;
}
return 0;
}
/* Delete (and withdraw) local routes for specified ES from global and ES table.
* Also remove all remote routes from the per ES table. Invoked when ES
* is deleted.
*/
static void bgp_evpn_es_route_del_all(struct bgp *bgp, struct bgp_evpn_es *es)
{
struct bgp_dest *dest;
struct bgp_path_info *pi, *nextpi;
/* de-activate the ES */
bgp_evpn_local_es_down(bgp, es);
bgp_evpn_local_type1_evi_route_del(bgp, es);
/* Walk this ES's routing table and delete all routes. */
for (dest = bgp_table_top(es->route_table); dest;
dest = bgp_route_next(dest)) {
for (pi = bgp_dest_get_bgp_path_info(dest);
(pi != NULL) && (nextpi = pi->next, 1); pi = nextpi) {
bgp_path_info_delete(dest, pi);
dest = bgp_path_info_reap(dest, pi);
assert(dest);
}
}
}
/*****************************************************************************
* Base APIs for creating MH routes (Type-1 or Type-4) on local ethernet
* segment updates.
*/
/* create or update local EVPN type1/type4 route entry.
*
* This could be in -
* the ES table if ESR/EAD-ES (or)
* the VNI table if EAD-EVI (or)
* the global table if ESR/EAD-ES/EAD-EVI
*
* Note: vpn is applicable only to EAD-EVI routes (NULL for EAD-ES and
* ESR).
*/
int bgp_evpn_mh_route_update(struct bgp *bgp, struct bgp_evpn_es *es,
struct bgpevpn *vpn, afi_t afi, safi_t safi,
struct bgp_dest *dest, struct attr *attr,
struct bgp_path_info **ri, int *route_changed)
{
struct bgp_path_info *tmp_pi = NULL;
struct bgp_path_info *local_pi = NULL; /* local route entry if any */
struct bgp_path_info *remote_pi = NULL; /* remote route entry if any */
struct bgp_labels bgp_labels = {};
struct attr *attr_new = NULL;
struct prefix_evpn *evp;
*ri = NULL;
evp = (struct prefix_evpn *)bgp_dest_get_prefix(dest);
*route_changed = 1;
/* locate the local and remote entries if any */
for (tmp_pi = bgp_dest_get_bgp_path_info(dest); tmp_pi;
tmp_pi = tmp_pi->next) {
if (tmp_pi->peer == bgp->peer_self
&& tmp_pi->type == ZEBRA_ROUTE_BGP
&& tmp_pi->sub_type == BGP_ROUTE_STATIC)
local_pi = tmp_pi;
if (tmp_pi->type == ZEBRA_ROUTE_BGP
&& tmp_pi->sub_type == BGP_ROUTE_IMPORTED
&& CHECK_FLAG(tmp_pi->flags, BGP_PATH_VALID))
remote_pi = tmp_pi;
}
/* we don't expect to see a remote_pi at this point as
* an ES route has {esi, vtep_ip} as the key in the ES-rt-table
* in the VNI-rt-table.
*/
if (remote_pi) {
flog_err(
EC_BGP_ES_INVALID,
"%u ERROR: local es route for ESI: %s vtep %pI4 also learnt from remote",
bgp->vrf_id, es ? es->esi_str : "Null",
es ? &es->originator_ip : NULL);
return -1;
}
/* create or update the entry */
if (!local_pi) {
/* Add or update attribute to hash */
attr_new = bgp_attr_intern(attr);
/* Create new route with its attribute. */
tmp_pi = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0,
bgp->peer_self, attr_new, dest);
SET_FLAG(tmp_pi->flags, BGP_PATH_VALID);
if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE) {
bgp_path_info_extra_get(tmp_pi);
bgp_labels.num_labels = 1;
if (vpn)
vni2label(vpn->vni, &bgp_labels.label[0]);
if (!bgp_path_info_labels_same(tmp_pi,
&bgp_labels.label[0],
bgp_labels.num_labels)) {
bgp_labels_unintern(&tmp_pi->extra->labels);
tmp_pi->extra->labels =
bgp_labels_intern(&bgp_labels);
}
}
/* add the newly created path to the route-node */
bgp_path_info_add(dest, tmp_pi);
} else {
tmp_pi = local_pi;
if (attrhash_cmp(tmp_pi->attr, attr)
&& !CHECK_FLAG(tmp_pi->flags, BGP_PATH_REMOVED))
*route_changed = 0;
else {
/* The attribute has changed.
* Add (or update) attribute to hash.
*/
attr_new = bgp_attr_intern(attr);
bgp_path_info_set_flag(dest, tmp_pi,
BGP_PATH_ATTR_CHANGED);
/* Restore route, if needed. */
if (CHECK_FLAG(tmp_pi->flags, BGP_PATH_REMOVED))
bgp_path_info_restore(dest, tmp_pi);
/* Unintern existing, set to new. */
bgp_attr_unintern(&tmp_pi->attr);
tmp_pi->attr = attr_new;
tmp_pi->uptime = monotime(NULL);
}
}
if (*route_changed) {
if (BGP_DEBUG(evpn_mh, EVPN_MH_RT))
zlog_debug(
"local ES %s vni %u route-type %s nexthop %pI4 updated",
es ? es->esi_str : "Null", vpn ? vpn->vni : 0,
evp->prefix.route_type == BGP_EVPN_ES_ROUTE
? "esr"
: (vpn ? "ead-evi" : "ead-es"),
&attr->mp_nexthop_global_in);
frrtrace(4, frr_bgp, evpn_mh_local_ead_es_evi_route_upd,
&es->esi, (vpn ? vpn->vni : 0), evp->prefix.route_type,
attr->mp_nexthop_global_in);
}
/* Return back th*e route entry. */
*ri = tmp_pi;
return 0;
}
/* Delete local EVPN ESR (type-4) and EAD (type-1) route
*
* Note: vpn is applicable only to EAD-EVI routes (NULL for EAD-ES and
* ESR).
*/
static int bgp_evpn_mh_route_delete(struct bgp *bgp, struct bgp_evpn_es *es,
struct bgpevpn *vpn,
struct bgp_evpn_es_frag *es_frag,
struct prefix_evpn *p)
{
afi_t afi = AFI_L2VPN;
safi_t safi = SAFI_EVPN;
struct bgp_path_info *pi;
struct bgp_dest *dest = NULL; /* dest in esi table */
struct bgp_dest *global_dest = NULL; /* dest in global table */
struct bgp_table *rt_table;
struct prefix_rd *prd;
if (vpn) {
rt_table = vpn->ip_table;
prd = &vpn->prd;
} else {
rt_table = es->route_table;
prd = &es_frag->prd;
}
/* First, locate the route node within the ESI or VNI.
* If it doesn't exist, ther is nothing to do.
* Note: there is no RD here.
*/
dest = bgp_node_lookup(rt_table, (struct prefix *)p);
if (!dest)
return 0;
if (BGP_DEBUG(evpn_mh, EVPN_MH_RT))
zlog_debug(
"local ES %s vni %u route-type %s nexthop %pI4 delete",
es->esi_str, vpn ? vpn->vni : 0,
p->prefix.route_type == BGP_EVPN_ES_ROUTE
? "esr"
: (vpn ? "ead-evi" : "ead-es"),
&es->originator_ip);
frrtrace(4, frr_bgp, evpn_mh_local_ead_es_evi_route_del, &es->esi,
(vpn ? vpn->vni : 0), p->prefix.route_type, es->originator_ip);
/* Next, locate route node in the global EVPN routing table.
* Note that this table is a 2-level tree (RD-level + Prefix-level)
*/
global_dest = bgp_evpn_global_node_lookup(bgp->rib[afi][safi], safi, p,
prd, NULL);
if (global_dest) {
/* Delete route entry in the global EVPN table. */
delete_evpn_route_entry(bgp, afi, safi, global_dest, &pi);
/* Schedule for processing - withdraws to peers happen from
* this table.
*/
if (pi)
bgp_process(bgp, global_dest, pi, afi, safi);
bgp_dest_unlock_node(global_dest);
}
/*
* Delete route entry in the ESI or VNI routing table.
* This can just be removed.
*/
delete_evpn_route_entry(bgp, afi, safi, dest, &pi);
if (pi)
dest = bgp_path_info_reap(dest, pi);
assert(dest);
bgp_dest_unlock_node(dest);
return 0;
}
/*
* This function is called when the VNI RD changes.
* Delete all EAD/EVI local routes for this VNI from the global routing table.
* These routes are scheduled for withdraw from peers.
*/
int delete_global_ead_evi_routes(struct bgp *bgp, struct bgpevpn *vpn)
{
afi_t afi;
safi_t safi;
struct bgp_dest *rdrn, *bd;
struct bgp_table *table;
struct bgp_path_info *pi;
afi = AFI_L2VPN;
safi = SAFI_EVPN;
/* Find the RD node for the VNI in the global table */
rdrn = bgp_node_lookup(bgp->rib[afi][safi], (struct prefix *)&vpn->prd);
if (rdrn && bgp_dest_has_bgp_path_info_data(rdrn)) {
table = bgp_dest_get_bgp_table_info(rdrn);
/*
* Iterate over all the routes in this table and delete EAD/EVI
* routes
*/
for (bd = bgp_table_top(table); bd; bd = bgp_route_next(bd)) {
struct prefix_evpn *evp = (struct prefix_evpn *)&bd->rn->p;
if (evp->prefix.route_type != BGP_EVPN_AD_ROUTE)
continue;
delete_evpn_route_entry(bgp, afi, safi, bd, &pi);
if (pi)
bgp_process(bgp, bd, pi, afi, safi);
}
}
/* Unlock RD node. */
if (rdrn)
bgp_dest_unlock_node(rdrn);
return 0;
}
/*****************************************************************************
* Ethernet Segment (Type-4) Routes
* ESRs are used for DF election. Currently service-carving described in
* RFC 7432 is NOT supported. Instead preference based DF election is
* used by default.
* Reference: draft-ietf-bess-evpn-pref-df
*/
/* Build extended community for EVPN ES (type-4) route */
static void bgp_evpn_type4_route_extcomm_build(struct bgp_evpn_es *es,
struct attr *attr)
{
struct ecommunity ecom_encap;
struct ecommunity ecom_es_rt;
struct ecommunity ecom_df;
struct ecommunity_val eval;
struct ecommunity_val eval_es_rt;
struct ecommunity_val eval_df;
bgp_encap_types tnl_type;
struct ethaddr mac;
/* Encap */
tnl_type = BGP_ENCAP_TYPE_VXLAN;
memset(&ecom_encap, 0, sizeof(ecom_encap));
encode_encap_extcomm(tnl_type, &eval);
ecom_encap.size = 1;
ecom_encap.unit_size = ECOMMUNITY_SIZE;
ecom_encap.val = (uint8_t *)eval.val;
bgp_attr_set_ecommunity(attr, ecommunity_dup(&ecom_encap));
/* ES import RT */
memset(&mac, 0, sizeof(mac));
memset(&ecom_es_rt, 0, sizeof(ecom_es_rt));
es_get_system_mac(&es->esi, &mac);
encode_es_rt_extcomm(&eval_es_rt, &mac);
ecom_es_rt.size = 1;
ecom_es_rt.unit_size = ECOMMUNITY_SIZE;
ecom_es_rt.val = (uint8_t *)eval_es_rt.val;
bgp_attr_set_ecommunity(
attr,
ecommunity_merge(bgp_attr_get_ecommunity(attr), &ecom_es_rt));
/* DF election extended community */
memset(&ecom_df, 0, sizeof(ecom_df));
encode_df_elect_extcomm(&eval_df, es->df_pref);
ecom_df.size = 1;
ecom_df.val = (uint8_t *)eval_df.val;
bgp_attr_set_ecommunity(
attr,
ecommunity_merge(bgp_attr_get_ecommunity(attr), &ecom_df));
}
/* Create or update local type-4 route */
static int bgp_evpn_type4_route_update(struct bgp *bgp,
struct bgp_evpn_es *es, struct prefix_evpn *p)
{
int ret = 0;
int route_changed = 0;
afi_t afi = AFI_L2VPN;
safi_t safi = SAFI_EVPN;
struct attr attr;
struct attr *attr_new = NULL;
struct bgp_dest *dest = NULL;
struct bgp_path_info *pi = NULL;
memset(&attr, 0, sizeof(attr));
/* Build path-attribute for this route. */
bgp_attr_default_set(&attr, bgp, BGP_ORIGIN_IGP);
attr.nexthop = es->originator_ip;
attr.mp_nexthop_global_in = es->originator_ip;
attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
/* Set up extended community. */
bgp_evpn_type4_route_extcomm_build(es, &attr);
/* First, create (or fetch) route node within the ESI. */
/* NOTE: There is no RD here. */
dest = bgp_node_get(es->route_table, (struct prefix *)p);
/* Create or update route entry. */
ret = bgp_evpn_mh_route_update(bgp, es, NULL, afi, safi, dest, &attr,
&pi, &route_changed);
if (ret != 0)
flog_err(
EC_BGP_ES_INVALID,
"%u ERROR: Failed to updated ES route ESI: %s VTEP %pI4",
bgp->vrf_id, es->esi_str, &es->originator_ip);
assert(pi);
attr_new = pi->attr;
/* Perform route selection;
* this is just to set the flags correctly
* as local route in the ES always wins.
*/
bgp_evpn_es_route_select_install(bgp, es, dest, pi);
bgp_dest_unlock_node(dest);
/* If this is a new route or some attribute has changed, export the
* route to the global table. The route will be advertised to peers
* from there. Note that this table is a 2-level tree (RD-level +
* Prefix-level) similar to L3VPN routes.
*/
if (route_changed) {
struct bgp_path_info *global_pi;
dest = bgp_evpn_global_node_get(bgp->rib[afi][safi], afi, safi,
p, &es->es_base_frag->prd,
NULL);
bgp_evpn_mh_route_update(bgp, es, NULL, afi, safi, dest,
attr_new, &global_pi, &route_changed);
/* Schedule for processing and unlock node. */
bgp_process(bgp, dest, global_pi, afi, safi);
bgp_dest_unlock_node(dest);
}
/* Unintern temporary. */
aspath_unintern(&attr.aspath);
return 0;
}
/* Delete local type-4 route */
static int bgp_evpn_type4_route_delete(struct bgp *bgp,
struct bgp_evpn_es *es, struct prefix_evpn *p)
{
if (!es->es_base_frag)
return -1;
return bgp_evpn_mh_route_delete(bgp, es, NULL /* l2vni */,
es->es_base_frag, p);
}
/* Process remote/received EVPN type-4 route (advertise or withdraw) */
int bgp_evpn_type4_route_process(struct peer *peer, afi_t afi, safi_t safi,
struct attr *attr, uint8_t *pfx, int psize,
uint32_t addpath_id)
{
esi_t esi;
uint8_t ipaddr_len;
struct in_addr vtep_ip;
struct prefix_rd prd;
struct prefix_evpn p;
/* Type-4 route should be either 23 or 35 bytes
* RD (8), ESI (10), ip-len (1), ip (4 or 16)
*/
if (psize != BGP_EVPN_TYPE4_V4_PSIZE &&
psize != BGP_EVPN_TYPE4_V6_PSIZE) {
flog_err(EC_BGP_EVPN_ROUTE_INVALID,
"%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
peer->bgp->vrf_id, peer->host, psize);
return -1;
}
/* Make prefix_rd */
prd.family = AF_UNSPEC;
prd.prefixlen = 64;
memcpy(&prd.val, pfx, RD_BYTES);
pfx += RD_BYTES;
/* get the ESI */
memcpy(&esi, pfx, ESI_BYTES);
pfx += ESI_BYTES;
/* Get the IP. */
ipaddr_len = *pfx++;
if (ipaddr_len == IPV4_MAX_BITLEN) {
memcpy(&vtep_ip, pfx, IPV4_MAX_BYTELEN);
} else {
flog_err(
EC_BGP_EVPN_ROUTE_INVALID,
"%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
peer->bgp->vrf_id, peer->host, ipaddr_len);
return -1;
}
build_evpn_type4_prefix(&p, &esi, vtep_ip);
/* Process the route. */
if (attr) {
bgp_update(peer, (struct prefix *)&p, addpath_id, attr, afi,
safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, &prd, NULL,
0, 0, NULL);
} else {
bgp_withdraw(peer, (struct prefix *)&p, addpath_id, afi, safi,
ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, &prd, NULL, 0);
}
return 0;
}
/* Check if a prefix belongs to the local ES */
static bool bgp_evpn_type4_prefix_match(struct prefix_evpn *p,
struct bgp_evpn_es *es)
{
return (p->prefix.route_type == BGP_EVPN_ES_ROUTE) &&
!memcmp(&p->prefix.es_addr.esi, &es->esi, sizeof(esi_t));
}
/* Import remote ESRs on local ethernet segment add */
static int bgp_evpn_type4_remote_routes_import(struct bgp *bgp,
struct bgp_evpn_es *es, bool install)
{
int ret;
afi_t afi;
safi_t safi;
struct bgp_dest *rd_dest, *dest;
struct bgp_table *table;
struct bgp_path_info *pi;
afi = AFI_L2VPN;
safi = SAFI_EVPN;
/* Walk entire global routing table and evaluate routes which could be
* imported into this Ethernet Segment.
*/
for (rd_dest = bgp_table_top(bgp->rib[afi][safi]); rd_dest;
rd_dest = bgp_route_next(rd_dest)) {
table = bgp_dest_get_bgp_table_info(rd_dest);
if (!table)
continue;
for (dest = bgp_table_top(table); dest;
dest = bgp_route_next(dest)) {
struct prefix_evpn *evp =
(struct prefix_evpn *)bgp_dest_get_prefix(dest);
for (pi = bgp_dest_get_bgp_path_info(dest); pi;
pi = pi->next) {
/*
* Consider "valid" remote routes applicable for
* this ES.
*/
if (!(CHECK_FLAG(pi->flags, BGP_PATH_VALID)
&& pi->type == ZEBRA_ROUTE_BGP
&& pi->sub_type == BGP_ROUTE_NORMAL))
continue;
if (!bgp_evpn_type4_prefix_match(evp, es))
continue;
if (install)
ret = bgp_evpn_es_route_install(
bgp, es, evp, pi);
else
ret = bgp_evpn_es_route_uninstall(
bgp, es, evp, pi);
if (ret) {
flog_err(
EC_BGP_EVPN_FAIL,
"Failed to %s EVPN %pFX route in ESI %s",
install ? "install"
: "uninstall",
evp, es->esi_str);
bgp_dest_unlock_node(rd_dest);
bgp_dest_unlock_node(dest);
return ret;
}
}
}
}
return 0;
}
/*****************************************************************************
* Ethernet Auto Discovery (EAD/Type-1) route handling
* There are two types of EAD routes -
* 1. EAD-per-ES - Key: {ESI, ET=0xffffffff}
* 2. EAD-per-EVI - Key: {ESI, ET=0}
*/
/* Extended communities associated with EAD-per-ES */
static void
bgp_evpn_type1_es_route_extcomm_build(struct bgp_evpn_es_frag *es_frag,
struct attr *attr)
{
struct ecommunity ecom_encap;
struct ecommunity ecom_esi_label;
struct ecommunity_val eval;
struct ecommunity_val eval_esi_label;
bgp_encap_types tnl_type;
struct listnode *evi_node, *rt_node;
struct ecommunity *ecom;
struct bgp_evpn_es_evi *es_evi;
/* Encap */
tnl_type = BGP_ENCAP_TYPE_VXLAN;
memset(&ecom_encap, 0, sizeof(ecom_encap));
encode_encap_extcomm(tnl_type, &eval);
ecom_encap.size = 1;
ecom_encap.unit_size = ECOMMUNITY_SIZE;
ecom_encap.val = (uint8_t *)eval.val;
bgp_attr_set_ecommunity(attr, ecommunity_dup(&ecom_encap));
/* ESI label */
encode_esi_label_extcomm(&eval_esi_label,
false /*single_active*/);
ecom_esi_label.size = 1;
ecom_esi_label.unit_size = ECOMMUNITY_SIZE;
ecom_esi_label.val = (uint8_t *)eval_esi_label.val;
bgp_attr_set_ecommunity(attr,
ecommunity_merge(bgp_attr_get_ecommunity(attr),
&ecom_esi_label));
/* Add export RTs for all L2-VNIs associated with this ES */
/* XXX - suppress EAD-ES advertisment if there are no EVIs associated
* with it.
*/
if (listcount(bgp_mh_info->ead_es_export_rtl)) {
for (ALL_LIST_ELEMENTS_RO(bgp_mh_info->ead_es_export_rtl,
rt_node, ecom))
bgp_attr_set_ecommunity(
attr, ecommunity_merge(attr->ecommunity, ecom));
} else {
for (ALL_LIST_ELEMENTS_RO(es_frag->es_evi_frag_list, evi_node,
es_evi)) {
if (!CHECK_FLAG(es_evi->flags, BGP_EVPNES_EVI_LOCAL))
continue;
for (ALL_LIST_ELEMENTS_RO(es_evi->vpn->export_rtl,
rt_node, ecom))
bgp_attr_set_ecommunity(
attr, ecommunity_merge(attr->ecommunity,
ecom));
}
}
}
/* Extended communities associated with EAD-per-EVI */
static void bgp_evpn_type1_evi_route_extcomm_build(struct bgp_evpn_es *es,
struct bgpevpn *vpn, struct attr *attr)
{
struct ecommunity ecom_encap;
struct ecommunity_val eval;
bgp_encap_types tnl_type;
struct listnode *rt_node;
struct ecommunity *ecom;
/* Encap */
tnl_type = BGP_ENCAP_TYPE_VXLAN;
memset(&ecom_encap, 0, sizeof(ecom_encap));
encode_encap_extcomm(tnl_type, &eval);
ecom_encap.size = 1;
ecom_encap.unit_size = ECOMMUNITY_SIZE;
ecom_encap.val = (uint8_t *)eval.val;
bgp_attr_set_ecommunity(attr, ecommunity_dup(&ecom_encap));
/* Add export RTs for the L2-VNI */
for (ALL_LIST_ELEMENTS_RO(vpn->export_rtl, rt_node, ecom))
bgp_attr_set_ecommunity(
attr,
ecommunity_merge(bgp_attr_get_ecommunity(attr), ecom));
}
/* Update EVPN EAD (type-1) route -
* vpn - valid for EAD-EVI routes and NULL for EAD-ES routes
*/
static int bgp_evpn_type1_route_update(struct bgp *bgp, struct bgp_evpn_es *es,
struct bgpevpn *vpn,
struct bgp_evpn_es_frag *es_frag,
struct prefix_evpn *p)
{
int ret = 0;
afi_t afi = AFI_L2VPN;
safi_t safi = SAFI_EVPN;
struct attr attr;
struct attr *attr_new = NULL;
struct bgp_dest *dest = NULL;
struct bgp_path_info *pi = NULL;
int route_changed = 0;
struct prefix_rd *global_rd;
memset(&attr, 0, sizeof(attr));
/* Build path-attribute for this route. */
bgp_attr_default_set(&attr, bgp, BGP_ORIGIN_IGP);
attr.nexthop = es->originator_ip;
attr.mp_nexthop_global_in = es->originator_ip;
attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
if (vpn) {
/* EAD-EVI route update */
/* MPLS label */
vni2label(vpn->vni, &(attr.label));
/* Set up extended community */
bgp_evpn_type1_evi_route_extcomm_build(es, vpn, &attr);
/* First, create (or fetch) route node within the VNI. */
dest = bgp_node_get(vpn->ip_table, (struct prefix *)p);
/* Create or update route entry. */
ret = bgp_evpn_mh_route_update(bgp, es, vpn, afi, safi, dest,
&attr, &pi, &route_changed);
if (ret != 0)
flog_err(
EC_BGP_ES_INVALID,
"%u Failed to update EAD-EVI route ESI: %s VNI %u VTEP %pI4",
bgp->vrf_id, es->esi_str, vpn->vni,
&es->originator_ip);
global_rd = &vpn->prd;
} else {
/* EAD-ES route update */
/* MPLS label is 0 for EAD-ES route */
/* Set up extended community */
bgp_evpn_type1_es_route_extcomm_build(es_frag, &attr);
/* First, create (or fetch) route node within the ES. */
/* NOTE: There is no RD here. */
/* XXX: fragment ID must be included as a part of the prefix. */
dest = bgp_node_get(es->route_table, (struct prefix *)p);