-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathbgp_evpn.c
5813 lines (4987 loc) · 153 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
/* Ethernet-VPN Packet and vty Processing File
* Copyright (C) 2016 6WIND
* Copyright (C) 2017 Cumulus Networks, Inc.
*
* This file is part of FRR.
*
* FRRouting is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* FRRouting is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#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 "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_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"
/*
* Definitions and external declarations.
*/
extern struct zclient *zclient;
DEFINE_QOBJ_TYPE(bgpevpn)
DEFINE_QOBJ_TYPE(evpnes)
/*
* Static function declarations
*/
static void delete_evpn_route_entry(struct bgp *bgp, afi_t afi, safi_t safi,
struct bgp_node *rn,
struct bgp_path_info **pi);
static int delete_all_vni_routes(struct bgp *bgp, struct bgpevpn *vpn);
/*
* Private functions.
*/
/* compare two IPV4 VTEP IPs */
static int evpn_vtep_ip_cmp(void *p1, void *p2)
{
const struct in_addr *ip1 = p1;
const struct in_addr *ip2 = p2;
return ip1->s_addr - ip2->s_addr;
}
/*
* Make hash key for ESI.
*/
static unsigned int esi_hash_keymake(void *p)
{
struct evpnes *pes = p;
const void *pnt = (void *)pes->esi.val;
return jhash(pnt, ESI_BYTES, 0xa5a5a55a);
}
/*
* Compare two ESIs.
*/
static bool esi_cmp(const void *p1, const void *p2)
{
const struct evpnes *pes1 = p1;
const struct evpnes *pes2 = p2;
if (pes1 == NULL && pes2 == NULL)
return true;
if (pes1 == NULL || pes2 == NULL)
return false;
return (memcmp(pes1->esi.val, pes2->esi.val, ESI_BYTES) == 0);
}
/*
* Make vni hash key.
*/
static unsigned int vni_hash_key_make(void *p)
{
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;
if (!vpn1 && !vpn2)
return true;
if (!vpn1 || !vpn2)
return false;
return (vpn1->vni == vpn2->vni);
}
static 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(void *p)
{
struct vrf_irt_node *irt = p;
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;
if (irt1 == NULL && irt2 == NULL)
return true;
if (irt1 == NULL || irt2 == NULL)
return false;
return (memcmp(irt1->rt.val, irt2->rt.val, ECOMMUNITY_SIZE) == 0);
}
/*
* Create a new vrf import_rt in default instance
*/
static struct vrf_irt_node *vrf_import_rt_new(struct ecommunity_val *rt)
{
struct bgp *bgp_def = NULL;
struct vrf_irt_node *irt;
bgp_def = bgp_get_default();
if (!bgp_def) {
flog_err(EC_BGP_NO_DFLT,
"vrf import rt new - def instance not created yet");
return NULL;
}
irt = XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT,
sizeof(struct vrf_irt_node));
if (!irt)
return NULL;
irt->rt = *rt;
irt->vrfs = list_new();
/* Add to hash */
if (!hash_get(bgp_def->vrf_import_rt_hash, irt, hash_alloc_intern)) {
XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT, irt);
return NULL;
}
return irt;
}
/*
* Free the vrf import rt node
*/
static void vrf_import_rt_free(struct vrf_irt_node *irt)
{
struct bgp *bgp_def = NULL;
bgp_def = bgp_get_default();
if (!bgp_def) {
flog_err(EC_BGP_NO_DFLT,
"vrf import rt free - def instance not created yet");
return;
}
hash_release(bgp_def->vrf_import_rt_hash, irt);
list_delete(&irt->vrfs);
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_def = NULL;
struct vrf_irt_node *irt;
struct vrf_irt_node tmp;
bgp_def = bgp_get_default();
if (!bgp_def) {
flog_err(EC_BGP_NO_DFLT,
"vrf import rt lookup - def instance not created yet");
return NULL;
}
memset(&tmp, 0, sizeof(struct vrf_irt_node));
memcpy(&tmp.rt, rt, ECOMMUNITY_SIZE);
irt = hash_lookup(bgp_def->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(void *p)
{
struct irt_node *irt = p;
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;
if (irt1 == NULL && irt2 == NULL)
return true;
if (irt1 == NULL || irt2 == NULL)
return false;
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;
if (!bgp)
return NULL;
irt = XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT, sizeof(struct irt_node));
if (!irt)
return NULL;
irt->rt = *rt;
irt->vnis = list_new();
/* Add to hash */
if (!hash_get(bgp->import_rt_hash, irt, hash_alloc_intern)) {
XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt);
return NULL;
}
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);
}
/*
* 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(struct irt_node));
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;
}
/*
* Compare Route Targets.
*/
static int 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);
}
static void evpn_xxport_delete_ecomm(void *val)
{
struct ecommunity *ecomm = val;
ecommunity_free(&ecomm);
}
/*
* 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,
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;
}
}
/*
* Map one RT to specified VRF.
* bgp_vrf = BGP vrf instance
*/
static void map_vrf_to_rt(struct bgp *bgp_vrf, struct ecommunity_val *eval)
{
struct vrf_irt_node *irt = NULL;
struct ecommunity_val eval_tmp;
/* If using "automatic" 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.
*/
memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
mask_ecom_global_admin(&eval_tmp, eval);
irt = lookup_vrf_import_rt(&eval_tmp);
if (irt && is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
/* Already mapped. */
return;
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_irt_node *irt)
{
/* 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);
assert(irt);
}
/* 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);
}
}
/*
* 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)
{
struct ecommunity_val eval;
struct ecommunity *ecomadd;
if (bgp->advertise_autort_rfc8365)
vni |= EVPN_AUTORT_VXLAN;
encode_route_target_as((bgp->as & 0xFFFF), vni, &eval);
ecomadd = ecommunity_new();
ecommunity_add_val(ecomadd, &eval);
listnode_add_sort(rtl, 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;
}
/*
* Add (update) or delete MACIP from zebra.
*/
static int bgp_zebra_send_remote_macip(struct bgp *bgp, struct bgpevpn *vpn,
struct prefix_evpn *p,
struct in_addr remote_vtep_ip, int add,
uint8_t flags, uint32_t seq)
{
struct stream *s;
int ipa_len;
char buf1[ETHER_ADDR_STRLEN];
char buf2[INET6_ADDRSTRLEN];
char buf3[INET6_ADDRSTRLEN];
/* Check socket. */
if (!zclient || zclient->sock < 0)
return 0;
/* 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",
__PRETTY_FUNCTION__);
return 0;
}
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->vni);
stream_put(s, &p->prefix.macip_addr.mac.octet, ETH_ALEN); /* Mac Addr */
/* IP address length and IP address, if any. */
if (is_evpn_prefix_ipaddr_none(p))
stream_putl(s, 0);
else {
ipa_len = is_evpn_prefix_ipaddr_v4(p) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN;
stream_putl(s, ipa_len);
stream_put(s, &p->prefix.macip_addr.ip.ip.addr, ipa_len);
}
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_putw_at(s, 0, stream_get_endp(s));
if (bgp_debug_zebra(NULL))
zlog_debug(
"Tx %s MACIP, VNI %u MAC %s IP %s flags 0x%x seq %u remote VTEP %s",
add ? "ADD" : "DEL", vpn->vni,
prefix_mac2str(&p->prefix.macip_addr.mac,
buf1, sizeof(buf1)),
ipaddr2str(&p->prefix.macip_addr.ip,
buf3, sizeof(buf3)), flags, seq,
inet_ntop(AF_INET, &remote_vtep_ip, buf2,
sizeof(buf2)));
return zclient_send_message(zclient);
}
/*
* Add (update) or delete remote VTEP from zebra.
*/
static int bgp_zebra_send_remote_vtep(struct bgp *bgp, struct bgpevpn *vpn,
struct prefix_evpn *p, int add)
{
struct stream *s;
/* Check socket. */
if (!zclient || zclient->sock < 0)
return 0;
/* 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 vtep",
__PRETTY_FUNCTION__);
return 0;
}
s = zclient->obuf;
stream_reset(s);
zclient_create_header(
s, add ? ZEBRA_REMOTE_VTEP_ADD : ZEBRA_REMOTE_VTEP_DEL,
bgp->vrf_id);
stream_putl(s, vpn->vni);
if (is_evpn_prefix_ipaddr_v4(p))
stream_put_in_addr(s, &p->prefix.imet_addr.ip.ipaddr_v4);
else if (is_evpn_prefix_ipaddr_v6(p)) {
flog_err(
EC_BGP_VTEP_INVALID,
"Bad remote IP when trying to %s remote VTEP for VNI %u",
add ? "ADD" : "DEL", vpn->vni);
return -1;
}
stream_putw_at(s, 0, stream_get_endp(s));
if (bgp_debug_zebra(NULL))
zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
add ? "ADD" : "DEL", vpn->vni,
inet_ntoa(p->prefix.imet_addr.ip.ipaddr_v4));
return zclient_send_message(zclient);
}
/*
* Build extended community for EVPN ES (type-4) route
*/
static void build_evpn_type4_route_extcomm(struct evpnes *es,
struct attr *attr)
{
struct ecommunity ecom_encap;
struct ecommunity ecom_es_rt;
struct ecommunity_val eval;
struct ecommunity_val eval_es_rt;
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.val = (uint8_t *)eval.val;
attr->ecommunity = ecommunity_dup(&ecom_encap);
/* ES import RT */
memset(&mac, 0, sizeof(struct ethaddr));
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.val = (uint8_t *)eval_es_rt.val;
attr->ecommunity =
ecommunity_merge(attr->ecommunity, &ecom_es_rt);
attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES);
}
/*
* Build extended communities for EVPN prefix route.
*/
static void build_evpn_type5_route_extcomm(struct bgp *bgp_vrf,
struct attr *attr)
{
struct ecommunity ecom_encap;
struct ecommunity ecom_rmac;
struct ecommunity_val eval;
struct ecommunity_val eval_rmac;
bgp_encap_types tnl_type;
struct listnode *node, *nnode;
struct ecommunity *ecom;
struct list *vrf_export_rtl = NULL;
/* 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.val = (uint8_t *)eval.val;
/* Add Encap */
attr->ecommunity = ecommunity_dup(&ecom_encap);
/* Add the export RTs for L3VNI/VRF */
vrf_export_rtl = bgp_vrf->vrf_export_rtl;
for (ALL_LIST_ELEMENTS(vrf_export_rtl, node, nnode, ecom))
attr->ecommunity =
ecommunity_merge(attr->ecommunity, ecom);
/* add the router mac extended community */
if (!is_zero_mac(&attr->rmac)) {
memset(&ecom_rmac, 0, sizeof(ecom_rmac));
encode_rmac_extcomm(&eval_rmac, &attr->rmac);
ecom_rmac.size = 1;
ecom_rmac.val = (uint8_t *)eval_rmac.val;
attr->ecommunity =
ecommunity_merge(attr->ecommunity, &ecom_rmac);
}
attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES);
}
/*
* Build extended communities for EVPN route.
* This function is applicable for type-2 and type-3 routes. The layer-2 RT
* and ENCAP extended communities are applicable for all routes.
* The default gateway extended community and MAC mobility (sticky) extended
* community are added as needed based on passed settings - only for type-2
* routes. Likewise, the layer-3 RT and Router MAC extended communities are
* added, if present, based on passed settings - only for non-link-local
* type-2 routes.
*/
static void build_evpn_route_extcomm(struct bgpevpn *vpn, struct attr *attr,
int add_l3_ecomm)
{
struct ecommunity ecom_encap;
struct ecommunity ecom_sticky;
struct ecommunity ecom_default_gw;
struct ecommunity ecom_rmac;
struct ecommunity ecom_na;
struct ecommunity_val eval;
struct ecommunity_val eval_sticky;
struct ecommunity_val eval_default_gw;
struct ecommunity_val eval_rmac;
struct ecommunity_val eval_na;
bgp_encap_types tnl_type;
struct listnode *node, *nnode;
struct ecommunity *ecom;
uint32_t seqnum;
struct list *vrf_export_rtl = NULL;
/* 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.val = (uint8_t *)eval.val;
/* Add Encap */
attr->ecommunity = ecommunity_dup(&ecom_encap);
/* Add the export RTs for L2VNI */
for (ALL_LIST_ELEMENTS(vpn->export_rtl, node, nnode, ecom))
attr->ecommunity = ecommunity_merge(attr->ecommunity, ecom);
/* Add the export RTs for L3VNI if told to - caller determines
* when this should be done.
*/
if (add_l3_ecomm) {
vrf_export_rtl = bgpevpn_get_vrf_export_rtl(vpn);
if (vrf_export_rtl && !list_isempty(vrf_export_rtl)) {
for (ALL_LIST_ELEMENTS(vrf_export_rtl, node, nnode,
ecom))
attr->ecommunity = ecommunity_merge(
attr->ecommunity, ecom);
}
}
/* Add MAC mobility (sticky) if needed. */
if (attr->sticky) {
seqnum = 0;
memset(&ecom_sticky, 0, sizeof(ecom_sticky));
encode_mac_mobility_extcomm(1, seqnum, &eval_sticky);
ecom_sticky.size = 1;
ecom_sticky.val = (uint8_t *)eval_sticky.val;
attr->ecommunity =
ecommunity_merge(attr->ecommunity, &ecom_sticky);
}
/* Add RMAC, if told to. */
if (add_l3_ecomm) {
memset(&ecom_rmac, 0, sizeof(ecom_rmac));
encode_rmac_extcomm(&eval_rmac, &attr->rmac);
ecom_rmac.size = 1;
ecom_rmac.val = (uint8_t *)eval_rmac.val;
attr->ecommunity =
ecommunity_merge(attr->ecommunity, &ecom_rmac);
}
/* Add default gateway, if needed. */
if (attr->default_gw) {
memset(&ecom_default_gw, 0, sizeof(ecom_default_gw));
encode_default_gw_extcomm(&eval_default_gw);
ecom_default_gw.size = 1;
ecom_default_gw.val = (uint8_t *)eval_default_gw.val;
attr->ecommunity =
ecommunity_merge(attr->ecommunity, &ecom_default_gw);
}
if (attr->router_flag) {
memset(&ecom_na, 0, sizeof(ecom_na));
encode_na_flag_extcomm(&eval_na, attr->router_flag);
ecom_na.size = 1;
ecom_na.val = (uint8_t *)eval_na.val;
attr->ecommunity = ecommunity_merge(attr->ecommunity,
&ecom_na);
}
attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES);
}
/*
* Add MAC mobility extended community to attribute.
*/
static void add_mac_mobility_to_attr(uint32_t seq_num, struct attr *attr)
{
struct ecommunity ecom_tmp;
struct ecommunity_val eval;
uint8_t *ecom_val_ptr;
int i;
uint8_t *pnt;
int type = 0;
int sub_type = 0;
/* Build MM */
encode_mac_mobility_extcomm(0, seq_num, &eval);
/* Find current MM ecommunity */
ecom_val_ptr = NULL;
if (attr->ecommunity) {
for (i = 0; i < attr->ecommunity->size; i++) {
pnt = attr->ecommunity->val + (i * 8);
type = *pnt++;
sub_type = *pnt++;
if (type == ECOMMUNITY_ENCODE_EVPN
&& sub_type
== ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY) {
ecom_val_ptr = (uint8_t *)(attr->ecommunity->val
+ (i * 8));
break;
}
}
}
/* Update the existing MM ecommunity */
if (ecom_val_ptr) {
memcpy(ecom_val_ptr, eval.val, sizeof(char) * ECOMMUNITY_SIZE);
}
/* Add MM to existing */
else {
memset(&ecom_tmp, 0, sizeof(ecom_tmp));
ecom_tmp.size = 1;
ecom_tmp.val = (uint8_t *)eval.val;
if (attr->ecommunity)
attr->ecommunity =
ecommunity_merge(attr->ecommunity, &ecom_tmp);
else
attr->ecommunity = ecommunity_dup(&ecom_tmp);
}
}
/* Install EVPN route into zebra. */
static int evpn_zebra_install(struct bgp *bgp, struct bgpevpn *vpn,
struct prefix_evpn *p,
struct in_addr remote_vtep_ip, uint8_t flags,
uint32_t seq)
{
int ret;
if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
ret = bgp_zebra_send_remote_macip(bgp, vpn, p, remote_vtep_ip,
1, flags, seq);
else
ret = bgp_zebra_send_remote_vtep(bgp, vpn, p, 1);
return ret;
}
/* Uninstall EVPN route from zebra. */
static int evpn_zebra_uninstall(struct bgp *bgp, struct bgpevpn *vpn,
struct prefix_evpn *p,
struct in_addr remote_vtep_ip)
{
int ret;
if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
ret = bgp_zebra_send_remote_macip(bgp, vpn, p, remote_vtep_ip,
0, 0, 0);
else
ret = bgp_zebra_send_remote_vtep(bgp, vpn, p, 0);
return ret;
}
/*
* Due to MAC mobility, the prior "local" best route has been supplanted
* by a "remote" best route. The prior route has to be deleted and withdrawn
* from peers.
*/
static void evpn_delete_old_local_route(struct bgp *bgp, struct bgpevpn *vpn,
struct bgp_node *rn,
struct bgp_path_info *old_local)
{
struct bgp_node *global_rn;
struct bgp_path_info *pi;
afi_t afi = AFI_L2VPN;
safi_t safi = SAFI_EVPN;
/* Locate route node in the global EVPN routing table. Note that
* this table is a 2-level tree (RD-level + Prefix-level) similar to
* L3VPN routes.
*/
global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
(struct prefix *)&rn->p, &vpn->prd);
if (global_rn) {
/* Delete route entry in the global EVPN table. */
delete_evpn_route_entry(bgp, afi, safi, global_rn, &pi);
/* Schedule for processing - withdraws to peers happen from
* this table.
*/
if (pi)
bgp_process(bgp, global_rn, afi, safi);
bgp_unlock_node(global_rn);
}
/* Delete route entry in the VNI route table, caller to remove. */
bgp_path_info_delete(rn, old_local);
}
static struct in_addr *es_vtep_new(struct in_addr vtep)
{
struct in_addr *ip;
ip = XCALLOC(MTYPE_BGP_EVPN_ES_VTEP, sizeof(struct in_addr));
if (!ip)
return NULL;
ip->s_addr = vtep.s_addr;
return ip;
}
static void es_vtep_free(struct in_addr *ip)
{
XFREE(MTYPE_BGP_EVPN_ES_VTEP, ip);
}
/* check if VTEP is already part of the list */
static int is_vtep_present_in_list(struct list *list,
struct in_addr vtep)
{
struct listnode *node = NULL;
struct in_addr *tmp;
for (ALL_LIST_ELEMENTS_RO(list, node, tmp)) {
if (tmp->s_addr == vtep.s_addr)
return 1;
}
return 0;
}
/*
* Best path for ES route was changed,
* update the list of VTEPs for this ES
*/
static int evpn_es_install_vtep(struct bgp *bgp,
struct evpnes *es,
struct prefix_evpn *p,
struct in_addr rvtep)
{
struct in_addr *vtep_ip;
if (is_vtep_present_in_list(es->vtep_list, rvtep))
return 0;