forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbgp_evpn.c
7852 lines (6684 loc) · 213 KB
/
bgp_evpn.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
/* Ethernet-VPN Packet and vty Processing File
* Copyright (C) 2016 6WIND
* Copyright (C) 2017 Cumulus Networks, Inc.
*/
#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_label.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_nexthop.h"
#include "bgpd/bgp_addpath.h"
#include "bgpd/bgp_mac.h"
#include "bgpd/bgp_vty.h"
#include "bgpd/bgp_nht.h"
#include "bgpd/bgp_trace.h"
#include "bgpd/bgp_mpath.h"
#include "bgpd/bgp_packet.h"
/*
* Definitions and external declarations.
*/
DEFINE_QOBJ_TYPE(bgpevpn);
DEFINE_QOBJ_TYPE(bgp_evpn_es);
DEFINE_MTYPE_STATIC(BGPD, BGP_EVPN_INFO, "BGP EVPN instance information");
DEFINE_MTYPE_STATIC(BGPD, VRF_ROUTE_TARGET, "L3 Route Target");
/*
* Static function declarations
*/
static void bgp_evpn_remote_ip_hash_init(struct bgpevpn *evpn);
static void bgp_evpn_remote_ip_hash_destroy(struct bgpevpn *evpn);
static void bgp_evpn_remote_ip_hash_add(struct bgpevpn *vpn,
struct bgp_path_info *pi);
static void bgp_evpn_remote_ip_hash_del(struct bgpevpn *vpn,
struct bgp_path_info *pi);
static void bgp_evpn_remote_ip_hash_iterate(struct bgpevpn *vpn,
void (*func)(struct hash_bucket *,
void *),
void *arg);
static void bgp_evpn_link_to_vni_svi_hash(struct bgp *bgp, struct bgpevpn *vpn);
static void bgp_evpn_unlink_from_vni_svi_hash(struct bgp *bgp,
struct bgpevpn *vpn);
static unsigned int vni_svi_hash_key_make(const void *p);
static bool vni_svi_hash_cmp(const void *p1, const void *p2);
static void bgp_evpn_remote_ip_process_nexthops(struct bgpevpn *vpn,
struct ipaddr *addr,
bool resolve);
static void bgp_evpn_remote_ip_hash_link_nexthop(struct hash_bucket *bucket,
void *args);
static void bgp_evpn_remote_ip_hash_unlink_nexthop(struct hash_bucket *bucket,
void *args);
static struct in_addr zero_vtep_ip;
/*
* Private functions.
*/
/*
* Make vni hash key.
*/
static unsigned int vni_hash_key_make(const void *p)
{
const struct bgpevpn *vpn = p;
return (jhash_1word(vpn->vni, 0));
}
/*
* Comparison function for vni hash
*/
static bool vni_hash_cmp(const void *p1, const void *p2)
{
const struct bgpevpn *vpn1 = p1;
const struct bgpevpn *vpn2 = p2;
return vpn1->vni == vpn2->vni;
}
int vni_list_cmp(void *p1, void *p2)
{
const struct bgpevpn *vpn1 = p1;
const struct bgpevpn *vpn2 = p2;
return vpn1->vni - vpn2->vni;
}
/*
* Make vrf import route target hash key.
*/
static unsigned int vrf_import_rt_hash_key_make(const void *p)
{
const struct vrf_irt_node *irt = p;
const char *pnt = irt->rt.val;
return jhash(pnt, 8, 0x5abc1234);
}
/*
* Comparison function for vrf import rt hash
*/
static bool vrf_import_rt_hash_cmp(const void *p1, const void *p2)
{
const struct vrf_irt_node *irt1 = p1;
const struct vrf_irt_node *irt2 = p2;
return (memcmp(irt1->rt.val, irt2->rt.val, ECOMMUNITY_SIZE) == 0);
}
/*
* Create a new vrf import_rt in evpn instance
*/
static struct vrf_irt_node *vrf_import_rt_new(struct ecommunity_val *rt)
{
struct bgp *bgp_evpn = NULL;
struct vrf_irt_node *irt;
bgp_evpn = bgp_get_evpn();
if (!bgp_evpn) {
flog_err(EC_BGP_NO_DFLT,
"vrf import rt new - evpn instance not created yet");
return NULL;
}
irt = XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT,
sizeof(struct vrf_irt_node));
irt->rt = *rt;
irt->vrfs = list_new();
/* Add to hash */
(void)hash_get(bgp_evpn->vrf_import_rt_hash, irt, hash_alloc_intern);
return irt;
}
/*
* Free the vrf import rt node
*/
static void vrf_import_rt_free(struct vrf_irt_node *irt)
{
struct bgp *bgp_evpn = NULL;
bgp_evpn = bgp_get_evpn();
if (!bgp_evpn) {
flog_err(EC_BGP_NO_DFLT,
"vrf import rt free - evpn instance not created yet");
return;
}
hash_release(bgp_evpn->vrf_import_rt_hash, irt);
list_delete(&irt->vrfs);
XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT, irt);
}
static void hash_vrf_import_rt_free(struct vrf_irt_node *irt)
{
XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT, irt);
}
/*
* Function to lookup Import RT node - used to map a RT to set of
* VNIs importing routes with that RT.
*/
static struct vrf_irt_node *lookup_vrf_import_rt(struct ecommunity_val *rt)
{
struct bgp *bgp_evpn = NULL;
struct vrf_irt_node *irt;
struct vrf_irt_node tmp;
bgp_evpn = bgp_get_evpn();
if (!bgp_evpn) {
flog_err(
EC_BGP_NO_DFLT,
"vrf import rt lookup - evpn instance not created yet");
return NULL;
}
memset(&tmp, 0, sizeof(tmp));
memcpy(&tmp.rt, rt, ECOMMUNITY_SIZE);
irt = hash_lookup(bgp_evpn->vrf_import_rt_hash, &tmp);
return irt;
}
/*
* Is specified VRF present on the RT's list of "importing" VRFs?
*/
static int is_vrf_present_in_irt_vrfs(struct list *vrfs, struct bgp *bgp_vrf)
{
struct listnode *node = NULL, *nnode = NULL;
struct bgp *tmp_bgp_vrf = NULL;
for (ALL_LIST_ELEMENTS(vrfs, node, nnode, tmp_bgp_vrf)) {
if (tmp_bgp_vrf == bgp_vrf)
return 1;
}
return 0;
}
/*
* Make import route target hash key.
*/
static unsigned int import_rt_hash_key_make(const void *p)
{
const struct irt_node *irt = p;
const char *pnt = irt->rt.val;
return jhash(pnt, 8, 0xdeadbeef);
}
/*
* Comparison function for import rt hash
*/
static bool import_rt_hash_cmp(const void *p1, const void *p2)
{
const struct irt_node *irt1 = p1;
const struct irt_node *irt2 = p2;
return (memcmp(irt1->rt.val, irt2->rt.val, ECOMMUNITY_SIZE) == 0);
}
/*
* Create a new import_rt
*/
static struct irt_node *import_rt_new(struct bgp *bgp,
struct ecommunity_val *rt)
{
struct irt_node *irt;
irt = XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT, sizeof(struct irt_node));
irt->rt = *rt;
irt->vnis = list_new();
/* Add to hash */
(void)hash_get(bgp->import_rt_hash, irt, hash_alloc_intern);
return irt;
}
/*
* Free the import rt node
*/
static void import_rt_free(struct bgp *bgp, struct irt_node *irt)
{
hash_release(bgp->import_rt_hash, irt);
list_delete(&irt->vnis);
XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt);
}
static void hash_import_rt_free(struct irt_node *irt)
{
XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt);
}
/*
* Function to lookup Import RT node - used to map a RT to set of
* VNIs importing routes with that RT.
*/
static struct irt_node *lookup_import_rt(struct bgp *bgp,
struct ecommunity_val *rt)
{
struct irt_node *irt;
struct irt_node tmp;
memset(&tmp, 0, sizeof(tmp));
memcpy(&tmp.rt, rt, ECOMMUNITY_SIZE);
irt = hash_lookup(bgp->import_rt_hash, &tmp);
return irt;
}
/*
* Is specified VNI present on the RT's list of "importing" VNIs?
*/
static int is_vni_present_in_irt_vnis(struct list *vnis, struct bgpevpn *vpn)
{
struct listnode *node, *nnode;
struct bgpevpn *tmp_vpn;
for (ALL_LIST_ELEMENTS(vnis, node, nnode, tmp_vpn)) {
if (tmp_vpn == vpn)
return 1;
}
return 0;
}
/* Flag if the route is injectable into EVPN.
* This would be following category:
* Non-imported route,
* Non-EVPN imported route,
*/
bool is_route_injectable_into_evpn_non_supp(struct bgp_path_info *pi)
{
struct bgp_path_info *parent_pi;
struct bgp_table *table;
struct bgp_dest *dest;
if (pi->sub_type != BGP_ROUTE_IMPORTED || !pi->extra ||
!pi->extra->vrfleak || !pi->extra->vrfleak->parent)
return true;
parent_pi = (struct bgp_path_info *)pi->extra->vrfleak->parent;
dest = parent_pi->net;
if (!dest)
return true;
table = bgp_dest_table(dest);
if (table &&
table->afi == AFI_L2VPN &&
table->safi == SAFI_EVPN)
return false;
return true;
}
/* Flag if the route is injectable into EVPN.
* This would be following category:
* Non-imported route,
* Non-EVPN imported route,
* Non Aggregate suppressed route.
*/
bool is_route_injectable_into_evpn(struct bgp_path_info *pi)
{
/* do not import aggr suppressed routes */
if (bgp_path_suppressed(pi))
return false;
return is_route_injectable_into_evpn_non_supp(pi);
}
/*
* Compare Route Targets.
*/
int bgp_evpn_route_target_cmp(struct ecommunity *ecom1,
struct ecommunity *ecom2)
{
if (ecom1 && !ecom2)
return -1;
if (!ecom1 && ecom2)
return 1;
if (!ecom1 && !ecom2)
return 0;
if (ecom1->str && !ecom2->str)
return -1;
if (!ecom1->str && ecom2->str)
return 1;
if (!ecom1->str && !ecom2->str)
return 0;
return strcmp(ecom1->str, ecom2->str);
}
/*
* Compare L3 Route Targets.
*/
static int evpn_vrf_route_target_cmp(struct vrf_route_target *rt1,
struct vrf_route_target *rt2)
{
return bgp_evpn_route_target_cmp(rt1->ecom, rt2->ecom);
}
void bgp_evpn_xxport_delete_ecomm(void *val)
{
struct ecommunity *ecomm = val;
ecommunity_free(&ecomm);
}
/*
* Delete l3 Route Target.
*/
static void evpn_vrf_rt_del(void *val)
{
struct vrf_route_target *l3rt = val;
ecommunity_free(&l3rt->ecom);
XFREE(MTYPE_VRF_ROUTE_TARGET, l3rt);
}
/*
* Allocate a new l3 Route Target.
*/
static struct vrf_route_target *evpn_vrf_rt_new(struct ecommunity *ecom)
{
struct vrf_route_target *l3rt;
l3rt = XCALLOC(MTYPE_VRF_ROUTE_TARGET, sizeof(struct vrf_route_target));
l3rt->ecom = ecom;
return l3rt;
}
/*
* Mask off global-admin field of specified extended community (RT),
* just retain the local-admin field.
*/
static inline void mask_ecom_global_admin(struct ecommunity_val *dst,
const struct ecommunity_val *src)
{
uint8_t type;
type = src->val[0];
dst->val[0] = 0;
if (type == ECOMMUNITY_ENCODE_AS) {
dst->val[2] = dst->val[3] = 0;
} else if (type == ECOMMUNITY_ENCODE_AS4
|| type == ECOMMUNITY_ENCODE_IP) {
dst->val[2] = dst->val[3] = 0;
dst->val[4] = dst->val[5] = 0;
}
}
/*
* Converts the RT to Ecommunity Value and adjusts masking based
* on flags set for RT.
*/
static void vrf_rt2ecom_val(struct ecommunity_val *to_eval,
const struct vrf_route_target *l3rt, int iter)
{
const struct ecommunity_val *eval;
eval = (const struct ecommunity_val *)(l3rt->ecom->val +
(iter * ECOMMUNITY_SIZE));
/* If using "automatic" or "wildcard *" RT,
* we only care about the local-admin sub-field.
* This is to facilitate using L3VNI(VRF-VNI)
* as the RT for EBGP peering too and simplify
* configurations by allowing any ASN via '*'.
*/
memcpy(to_eval, eval, ECOMMUNITY_SIZE);
if (CHECK_FLAG(l3rt->flags, BGP_VRF_RT_AUTO) ||
CHECK_FLAG(l3rt->flags, BGP_VRF_RT_WILD))
mask_ecom_global_admin(to_eval, eval);
}
/*
* Map one RT to specified VRF.
* bgp_vrf = BGP vrf instance
*/
static void map_vrf_to_rt(struct bgp *bgp_vrf, struct vrf_route_target *l3rt)
{
uint32_t i = 0;
for (i = 0; i < l3rt->ecom->size; i++) {
struct vrf_irt_node *irt = NULL;
struct ecommunity_val eval_tmp;
/* Adjust masking for value */
vrf_rt2ecom_val(&eval_tmp, l3rt, i);
irt = lookup_vrf_import_rt(&eval_tmp);
if (irt && is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
return; /* Already mapped. */
if (!irt)
irt = vrf_import_rt_new(&eval_tmp);
/* Add VRF to the list for this RT. */
listnode_add(irt->vrfs, bgp_vrf);
}
}
/*
* Unmap specified VRF from specified RT. If there are no other
* VRFs for this RT, then the RT hash is deleted.
* bgp_vrf: BGP VRF specific instance
*/
static void unmap_vrf_from_rt(struct bgp *bgp_vrf,
struct vrf_route_target *l3rt)
{
uint32_t i;
for (i = 0; i < l3rt->ecom->size; i++) {
struct vrf_irt_node *irt;
struct ecommunity_val eval_tmp;
/* Adjust masking for value */
vrf_rt2ecom_val(&eval_tmp, l3rt, i);
irt = lookup_vrf_import_rt(&eval_tmp);
if (!irt)
return; /* Not mapped */
/* Delete VRF from list for this RT. */
listnode_delete(irt->vrfs, bgp_vrf);
if (!listnode_head(irt->vrfs))
vrf_import_rt_free(irt);
}
}
/*
* Map one RT to specified VNI.
*/
static void map_vni_to_rt(struct bgp *bgp, struct bgpevpn *vpn,
struct ecommunity_val *eval)
{
struct irt_node *irt;
struct ecommunity_val eval_tmp;
/* If using "automatic" RT, we only care about the local-admin
* sub-field.
* This is to facilitate using VNI as the RT for EBGP peering too.
*/
memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
if (!is_import_rt_configured(vpn))
mask_ecom_global_admin(&eval_tmp, eval);
irt = lookup_import_rt(bgp, &eval_tmp);
if (irt)
if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
/* Already mapped. */
return;
if (!irt)
irt = import_rt_new(bgp, &eval_tmp);
/* Add VNI to the hash list for this RT. */
listnode_add(irt->vnis, vpn);
}
/*
* Unmap specified VNI from specified RT. If there are no other
* VNIs for this RT, then the RT hash is deleted.
*/
static void unmap_vni_from_rt(struct bgp *bgp, struct bgpevpn *vpn,
struct irt_node *irt)
{
/* Delete VNI from hash list for this RT. */
listnode_delete(irt->vnis, vpn);
if (!listnode_head(irt->vnis)) {
import_rt_free(bgp, irt);
}
}
static void bgp_evpn_get_rmac_nexthop(struct bgpevpn *vpn,
const struct prefix_evpn *p,
struct attr *attr, uint8_t flags)
{
struct bgp *bgp_vrf = vpn->bgp_vrf;
memset(&attr->rmac, 0, sizeof(struct ethaddr));
if (!bgp_vrf)
return;
if (p->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
return;
/* Copy sys (pip) RMAC and PIP IP as nexthop
* in case of route is self MAC-IP,
* advertise-pip and advertise-svi-ip features
* are enabled.
* Otherwise, for all host MAC-IP route's
* copy anycast RMAC.
*/
if (CHECK_FLAG(flags, BGP_EVPN_MACIP_TYPE_SVI_IP)
&& bgp_vrf->evpn_info->advertise_pip &&
bgp_vrf->evpn_info->is_anycast_mac) {
/* copy sys rmac */
memcpy(&attr->rmac, &bgp_vrf->evpn_info->pip_rmac,
ETH_ALEN);
attr->nexthop = bgp_vrf->evpn_info->pip_ip;
attr->mp_nexthop_global_in =
bgp_vrf->evpn_info->pip_ip;
} else
memcpy(&attr->rmac, &bgp_vrf->rmac, ETH_ALEN);
}
/*
* Create RT extended community automatically from passed information:
* of the form AS:VNI.
* NOTE: We use only the lower 16 bits of the AS. This is sufficient as
* the need is to get a RT value that will be unique across different
* VNIs but the same across routers (in the same AS) for a particular
* VNI.
*/
static void form_auto_rt(struct bgp *bgp, vni_t vni, struct list *rtl,
bool is_l3)
{
struct ecommunity_val eval;
struct ecommunity *ecomadd;
struct ecommunity *ecom;
struct vrf_route_target *l3rt;
struct vrf_route_target *newrt;
bool ecom_found = false;
struct listnode *node;
if (bgp->advertise_autort_rfc8365)
vni |= EVPN_AUTORT_VXLAN;
encode_route_target_as((bgp->as & 0xFFFF), vni, &eval, true);
ecomadd = ecommunity_new();
ecommunity_add_val(ecomadd, &eval, false, false);
if (is_l3) {
for (ALL_LIST_ELEMENTS_RO(rtl, node, l3rt))
if (ecommunity_cmp(ecomadd, l3rt->ecom)) {
ecom_found = true;
break;
}
} else {
for (ALL_LIST_ELEMENTS_RO(rtl, node, ecom))
if (ecommunity_cmp(ecomadd, ecom)) {
ecom_found = true;
break;
}
}
if (!ecom_found) {
if (is_l3) {
newrt = evpn_vrf_rt_new(ecomadd);
/* Label it as autoderived */
SET_FLAG(newrt->flags, BGP_VRF_RT_AUTO);
listnode_add_sort(rtl, newrt);
} else
listnode_add_sort(rtl, ecomadd);
} else
ecommunity_free(&ecomadd);
}
/*
* Derive RD and RT for a VNI automatically. Invoked at the time of
* creation of a VNI.
*/
static void derive_rd_rt_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
{
bgp_evpn_derive_auto_rd(bgp, vpn);
bgp_evpn_derive_auto_rt_import(bgp, vpn);
bgp_evpn_derive_auto_rt_export(bgp, vpn);
}
/*
* Convert nexthop (remote VTEP IP) into an IPv6 address.
*/
static void evpn_convert_nexthop_to_ipv6(struct attr *attr)
{
if (BGP_ATTR_NEXTHOP_AFI_IP6(attr))
return;
ipv4_to_ipv4_mapped_ipv6(&attr->mp_nexthop_global, attr->nexthop);
attr->mp_nexthop_len = IPV6_MAX_BYTELEN;
}
/*
* Wrapper for node get in global table.
*/
struct bgp_dest *bgp_evpn_global_node_get(struct bgp_table *table, afi_t afi,
safi_t safi,
const struct prefix_evpn *evp,
struct prefix_rd *prd,
const struct bgp_path_info *local_pi)
{
struct prefix_evpn global_p;
if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE) {
/* prefix in the global table doesn't include the VTEP-IP so
* we need to create a different copy of the prefix
*/
evpn_type1_prefix_global_copy(&global_p, evp);
evp = &global_p;
} else if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE &&
local_pi) {
/*
* prefix in the global table needs MAC/IP, ensure they are
* present, using one's from local table's path_info.
*/
if (is_evpn_prefix_ipaddr_none(evp)) {
/* VNI MAC -> Global */
evpn_type2_prefix_global_copy(
&global_p, evp, NULL /* mac */,
evpn_type2_path_info_get_ip(local_pi));
} else {
/* VNI IP -> Global */
evpn_type2_prefix_global_copy(
&global_p, evp,
evpn_type2_path_info_get_mac(local_pi),
NULL /* ip */);
}
evp = &global_p;
}
return bgp_afi_node_get(table, afi, safi, (struct prefix *)evp, prd);
}
/*
* Wrapper for node lookup in global table.
*/
struct bgp_dest *bgp_evpn_global_node_lookup(
struct bgp_table *table, safi_t safi, const struct prefix_evpn *evp,
struct prefix_rd *prd, const struct bgp_path_info *local_pi)
{
struct prefix_evpn global_p;
if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE) {
/* prefix in the global table doesn't include the VTEP-IP so
* we need to create a different copy of the prefix
*/
evpn_type1_prefix_global_copy(&global_p, evp);
evp = &global_p;
} else if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE &&
local_pi) {
/*
* prefix in the global table needs MAC/IP, ensure they are
* present, using one's from local table's path_info.
*/
if (is_evpn_prefix_ipaddr_none(evp)) {
/* VNI MAC -> Global */
evpn_type2_prefix_global_copy(
&global_p, evp, NULL /* mac */,
evpn_type2_path_info_get_ip(local_pi));
} else {
/* VNI IP -> Global */
evpn_type2_prefix_global_copy(
&global_p, evp,
evpn_type2_path_info_get_mac(local_pi),
NULL /* ip */);
}
evp = &global_p;
}
return bgp_safi_node_lookup(table, safi, (struct prefix *)evp, prd);
}
/*
* Wrapper for node get in VNI IP table.
*/
struct bgp_dest *bgp_evpn_vni_ip_node_get(struct bgp_table *const table,
const struct prefix_evpn *evp,
const struct bgp_path_info *parent_pi)
{
struct prefix_evpn vni_p;
if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE && parent_pi) {
/* prefix in the global table doesn't include the VTEP-IP so
* we need to create a different copy for the VNI
*/
evpn_type1_prefix_vni_ip_copy(&vni_p, evp,
parent_pi->attr->nexthop);
evp = &vni_p;
} else if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
/* Only MAC-IP should go into this table, not mac-only */
assert(is_evpn_prefix_ipaddr_none(evp) == false);
/*
* prefix in the vni IP table doesn't include MAC so
* we need to create a different copy of the prefix.
*/
evpn_type2_prefix_vni_ip_copy(&vni_p, evp);
evp = &vni_p;
}
return bgp_node_get(table, (struct prefix *)evp);
}
/*
* Wrapper for node lookup in VNI IP table.
*/
struct bgp_dest *
bgp_evpn_vni_ip_node_lookup(const struct bgp_table *const table,
const struct prefix_evpn *evp,
const struct bgp_path_info *parent_pi)
{
struct prefix_evpn vni_p;
if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE && parent_pi) {
/* prefix in the global table doesn't include the VTEP-IP so
* we need to create a different copy for the VNI
*/
evpn_type1_prefix_vni_ip_copy(&vni_p, evp,
parent_pi->attr->nexthop);
evp = &vni_p;
} else if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
/* Only MAC-IP should go into this table, not mac-only */
assert(is_evpn_prefix_ipaddr_none(evp) == false);
/*
* prefix in the vni IP table doesn't include MAC so
* we need to create a different copy of the prefix.
*/
evpn_type2_prefix_vni_ip_copy(&vni_p, evp);
evp = &vni_p;
}
return bgp_node_lookup(table, (struct prefix *)evp);
}
/*
* Wrapper for node get in VNI MAC table.
*/
struct bgp_dest *
bgp_evpn_vni_mac_node_get(struct bgp_table *const table,
const struct prefix_evpn *evp,
const struct bgp_path_info *parent_pi)
{
struct prefix_evpn vni_p;
/* Only type-2 should ever go into this table */
assert(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE);
/*
* prefix in the vni MAC table doesn't include IP so
* we need to create a different copy of the prefix.
*/
evpn_type2_prefix_vni_mac_copy(&vni_p, evp);
evp = &vni_p;
return bgp_node_get(table, (struct prefix *)evp);
}
/*
* Wrapper for node lookup in VNI MAC table.
*/
struct bgp_dest *
bgp_evpn_vni_mac_node_lookup(const struct bgp_table *const table,
const struct prefix_evpn *evp,
const struct bgp_path_info *parent_pi)
{
struct prefix_evpn vni_p;
/* Only type-2 should ever go into this table */
assert(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE);
/*
* prefix in the vni MAC table doesn't include IP so
* we need to create a different copy of the prefix.
*/
evpn_type2_prefix_vni_mac_copy(&vni_p, evp);
evp = &vni_p;
return bgp_node_lookup(table, (struct prefix *)evp);
}
/*
* Wrapper for node get in both VNI tables.
*/
struct bgp_dest *bgp_evpn_vni_node_get(struct bgpevpn *vpn,
const struct prefix_evpn *p,
const struct bgp_path_info *parent_pi)
{
if ((p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) &&
(is_evpn_prefix_ipaddr_none(p) == true))
return bgp_evpn_vni_mac_node_get(vpn->mac_table, p, parent_pi);
return bgp_evpn_vni_ip_node_get(vpn->ip_table, p, parent_pi);
}
/*
* Wrapper for node lookup in both VNI tables.
*/
struct bgp_dest *bgp_evpn_vni_node_lookup(const struct bgpevpn *vpn,
const struct prefix_evpn *p,
const struct bgp_path_info *parent_pi)
{
if ((p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) &&
(is_evpn_prefix_ipaddr_none(p) == true))
return bgp_evpn_vni_mac_node_lookup(vpn->mac_table, p,
parent_pi);
return bgp_evpn_vni_ip_node_lookup(vpn->ip_table, p, parent_pi);
}
/*
* Add (update) or delete MACIP from zebra.
*/
static enum zclient_send_status bgp_zebra_send_remote_macip(
struct bgp *bgp, struct bgpevpn *vpn, const struct prefix_evpn *p,
const struct ethaddr *mac, struct in_addr remote_vtep_ip, int add,
uint8_t flags, uint32_t seq, esi_t *esi)
{
struct stream *s;
uint16_t ipa_len;
static struct in_addr zero_remote_vtep_ip;
bool esi_valid;
/* Check socket. */
if (!zclient || zclient->sock < 0) {
if (BGP_DEBUG(zebra, ZEBRA))
zlog_debug("%s: No zclient or zclient->sock exists",
__func__);
return ZCLIENT_SEND_SUCCESS;
}
/* Don't try to register if Zebra doesn't know of this instance. */
if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
if (BGP_DEBUG(zebra, ZEBRA))
zlog_debug(
"%s: No zebra instance to talk to, not installing remote macip",
__func__);
return ZCLIENT_SEND_SUCCESS;
}
if (!esi)
esi = zero_esi;
s = zclient->obuf;
stream_reset(s);
zclient_create_header(
s, add ? ZEBRA_REMOTE_MACIP_ADD : ZEBRA_REMOTE_MACIP_DEL,
bgp->vrf_id);
stream_putl(s, vpn ? vpn->vni : 0);
if (mac) /* Mac Addr */
stream_put(s, &mac->octet, ETH_ALEN);
else
stream_put(s, &p->prefix.macip_addr.mac.octet, ETH_ALEN);
/* IP address length and IP address, if any. */
if (is_evpn_prefix_ipaddr_none(p))
stream_putw(s, 0);
else {
ipa_len = is_evpn_prefix_ipaddr_v4(p) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN;
stream_putw(s, ipa_len);
stream_put(s, &p->prefix.macip_addr.ip.ip.addr, ipa_len);
}
/* If the ESI is valid that becomes the nexthop; tape out the
* VTEP-IP for that case
*/
if (bgp_evpn_is_esi_valid(esi)) {
esi_valid = true;
stream_put_in_addr(s, &zero_remote_vtep_ip);
} else {
esi_valid = false;
stream_put_in_addr(s, &remote_vtep_ip);
}
/* TX flags - MAC sticky status and/or gateway mac */
/* Also TX the sequence number of the best route. */
if (add) {
stream_putc(s, flags);
stream_putl(s, seq);
stream_put(s, esi, sizeof(esi_t));
}
stream_putw_at(s, 0, stream_get_endp(s));
if (bgp_debug_zebra(NULL)) {
char esi_buf[ESI_STR_LEN];
if (esi_valid)
esi_to_str(esi, esi_buf, sizeof(esi_buf));
else
snprintf(esi_buf, sizeof(esi_buf), "-");
zlog_debug(
"Tx %s MACIP, VNI %u MAC %pEA IP %pIA flags 0x%x seq %u remote VTEP %pI4 esi %s",
add ? "ADD" : "DEL", (vpn ? vpn->vni : 0),
(mac ? mac : &p->prefix.macip_addr.mac),
&p->prefix.macip_addr.ip, flags, seq, &remote_vtep_ip,
esi_buf);
}
frrtrace(5, frr_bgp, evpn_mac_ip_zsend, add, vpn, p, remote_vtep_ip,
esi);
return zclient_send_message(zclient);
}
/*
* Add (update) or delete remote VTEP from zebra.
*/
static enum zclient_send_status
bgp_zebra_send_remote_vtep(struct bgp *bgp, struct bgpevpn *vpn,
const struct prefix_evpn *p, int flood_control,
int add)
{
struct stream *s;
/* Check socket. */
if (!zclient || zclient->sock < 0) {
if (BGP_DEBUG(zebra, ZEBRA))