1 /* Ethernet-VPN Packet and vty Processing File
2 * Copyright (C) 2016 6WIND
3 * Copyright (C) 2017 Cumulus Networks, Inc.
5 * This file is part of FRR.
7 * FRRouting is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * FRRouting is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "bgpd/bgp_attr_evpn.h"
35 #include "bgpd/bgpd.h"
36 #include "bgpd/bgp_table.h"
37 #include "bgpd/bgp_route.h"
38 #include "bgpd/bgp_attr.h"
39 #include "bgpd/bgp_mplsvpn.h"
40 #include "bgpd/bgp_label.h"
41 #include "bgpd/bgp_evpn.h"
42 #include "bgpd/bgp_evpn_private.h"
43 #include "bgpd/bgp_ecommunity.h"
44 #include "bgpd/bgp_encap_types.h"
45 #include "bgpd/bgp_debug.h"
46 #include "bgpd/bgp_errors.h"
47 #include "bgpd/bgp_aspath.h"
48 #include "bgpd/bgp_zebra.h"
49 #include "bgpd/bgp_nexthop.h"
50 #include "bgpd/bgp_addpath.h"
51 #include "bgpd/bgp_mac.h"
52 #include "bgpd/bgp_vty.h"
55 * Definitions and external declarations.
57 extern struct zclient
*zclient
;
59 DEFINE_QOBJ_TYPE(bgpevpn
)
60 DEFINE_QOBJ_TYPE(evpnes
)
64 * Static function declarations
66 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
68 struct bgp_path_info
**pi
);
69 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
75 /* compare two IPV4 VTEP IPs */
76 static int evpn_vtep_ip_cmp(void *p1
, void *p2
)
78 const struct in_addr
*ip1
= p1
;
79 const struct in_addr
*ip2
= p2
;
81 return ip1
->s_addr
- ip2
->s_addr
;
85 * Make hash key for ESI.
87 static unsigned int esi_hash_keymake(const void *p
)
89 const struct evpnes
*pes
= p
;
90 const void *pnt
= (void *)pes
->esi
.val
;
92 return jhash(pnt
, ESI_BYTES
, 0xa5a5a55a);
98 static bool esi_cmp(const void *p1
, const void *p2
)
100 const struct evpnes
*pes1
= p1
;
101 const struct evpnes
*pes2
= p2
;
103 if (pes1
== NULL
&& pes2
== NULL
)
106 if (pes1
== NULL
|| pes2
== NULL
)
109 return (memcmp(pes1
->esi
.val
, pes2
->esi
.val
, ESI_BYTES
) == 0);
115 static unsigned int vni_hash_key_make(const void *p
)
117 const struct bgpevpn
*vpn
= p
;
118 return (jhash_1word(vpn
->vni
, 0));
122 * Comparison function for vni hash
124 static bool vni_hash_cmp(const void *p1
, const void *p2
)
126 const struct bgpevpn
*vpn1
= p1
;
127 const struct bgpevpn
*vpn2
= p2
;
133 return (vpn1
->vni
== vpn2
->vni
);
136 static int vni_list_cmp(void *p1
, void *p2
)
138 const struct bgpevpn
*vpn1
= p1
;
139 const struct bgpevpn
*vpn2
= p2
;
141 return vpn1
->vni
- vpn2
->vni
;
145 * Make vrf import route target hash key.
147 static unsigned int vrf_import_rt_hash_key_make(const void *p
)
149 const struct vrf_irt_node
*irt
= p
;
150 const char *pnt
= irt
->rt
.val
;
152 return jhash(pnt
, 8, 0x5abc1234);
156 * Comparison function for vrf import rt hash
158 static bool vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
160 const struct vrf_irt_node
*irt1
= p1
;
161 const struct vrf_irt_node
*irt2
= p2
;
163 if (irt1
== NULL
&& irt2
== NULL
)
166 if (irt1
== NULL
|| irt2
== NULL
)
169 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
173 * Create a new vrf import_rt in evpn instance
175 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
177 struct bgp
*bgp_evpn
= NULL
;
178 struct vrf_irt_node
*irt
;
180 bgp_evpn
= bgp_get_evpn();
182 flog_err(EC_BGP_NO_DFLT
,
183 "vrf import rt new - evpn instance not created yet");
187 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
188 sizeof(struct vrf_irt_node
));
191 irt
->vrfs
= list_new();
194 if (!hash_get(bgp_evpn
->vrf_import_rt_hash
, irt
, hash_alloc_intern
)) {
195 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
203 * Free the vrf import rt node
205 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
207 struct bgp
*bgp_evpn
= NULL
;
209 bgp_evpn
= bgp_get_evpn();
211 flog_err(EC_BGP_NO_DFLT
,
212 "vrf import rt free - evpn instance not created yet");
216 hash_release(bgp_evpn
->vrf_import_rt_hash
, irt
);
217 list_delete(&irt
->vrfs
);
218 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
222 * Function to lookup Import RT node - used to map a RT to set of
223 * VNIs importing routes with that RT.
225 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
227 struct bgp
*bgp_evpn
= NULL
;
228 struct vrf_irt_node
*irt
;
229 struct vrf_irt_node tmp
;
231 bgp_evpn
= bgp_get_evpn();
235 "vrf import rt lookup - evpn instance not created yet");
239 memset(&tmp
, 0, sizeof(struct vrf_irt_node
));
240 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
241 irt
= hash_lookup(bgp_evpn
->vrf_import_rt_hash
, &tmp
);
246 * Is specified VRF present on the RT's list of "importing" VRFs?
248 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
, struct bgp
*bgp_vrf
)
250 struct listnode
*node
= NULL
, *nnode
= NULL
;
251 struct bgp
*tmp_bgp_vrf
= NULL
;
253 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
254 if (tmp_bgp_vrf
== bgp_vrf
)
261 * Make import route target hash key.
263 static unsigned int import_rt_hash_key_make(const void *p
)
265 const struct irt_node
*irt
= p
;
266 const char *pnt
= irt
->rt
.val
;
268 return jhash(pnt
, 8, 0xdeadbeef);
272 * Comparison function for import rt hash
274 static bool import_rt_hash_cmp(const void *p1
, const void *p2
)
276 const struct irt_node
*irt1
= p1
;
277 const struct irt_node
*irt2
= p2
;
279 if (irt1
== NULL
&& irt2
== NULL
)
282 if (irt1
== NULL
|| irt2
== NULL
)
285 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
289 * Create a new import_rt
291 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
292 struct ecommunity_val
*rt
)
294 struct irt_node
*irt
;
299 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
302 irt
->vnis
= list_new();
305 if (!hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
)) {
306 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
314 * Free the import rt node
316 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
318 hash_release(bgp
->import_rt_hash
, irt
);
319 list_delete(&irt
->vnis
);
320 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
324 * Function to lookup Import RT node - used to map a RT to set of
325 * VNIs importing routes with that RT.
327 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
328 struct ecommunity_val
*rt
)
330 struct irt_node
*irt
;
333 memset(&tmp
, 0, sizeof(struct irt_node
));
334 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
335 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
340 * Is specified VNI present on the RT's list of "importing" VNIs?
342 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
344 struct listnode
*node
, *nnode
;
345 struct bgpevpn
*tmp_vpn
;
347 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
356 * Compare Route Targets.
358 static int evpn_route_target_cmp(struct ecommunity
*ecom1
,
359 struct ecommunity
*ecom2
)
367 if (!ecom1
&& !ecom2
)
370 if (ecom1
->str
&& !ecom2
->str
)
373 if (!ecom1
->str
&& ecom2
->str
)
376 if (!ecom1
->str
&& !ecom2
->str
)
379 return strcmp(ecom1
->str
, ecom2
->str
);
382 static void evpn_xxport_delete_ecomm(void *val
)
384 struct ecommunity
*ecomm
= val
;
385 ecommunity_free(&ecomm
);
389 * Mask off global-admin field of specified extended community (RT),
390 * just retain the local-admin field.
392 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
393 struct ecommunity_val
*src
)
399 if (type
== ECOMMUNITY_ENCODE_AS
) {
400 dst
->val
[2] = dst
->val
[3] = 0;
401 } else if (type
== ECOMMUNITY_ENCODE_AS4
402 || type
== ECOMMUNITY_ENCODE_IP
) {
403 dst
->val
[2] = dst
->val
[3] = 0;
404 dst
->val
[4] = dst
->val
[5] = 0;
409 * Map one RT to specified VRF.
410 * bgp_vrf = BGP vrf instance
412 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct ecommunity_val
*eval
)
414 struct vrf_irt_node
*irt
= NULL
;
415 struct ecommunity_val eval_tmp
;
417 /* If using "automatic" RT,
418 * we only care about the local-admin sub-field.
419 * This is to facilitate using L3VNI(VRF-VNI)
420 * as the RT for EBGP peering too.
422 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
423 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
424 mask_ecom_global_admin(&eval_tmp
, eval
);
426 irt
= lookup_vrf_import_rt(&eval_tmp
);
427 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
428 /* Already mapped. */
432 irt
= vrf_import_rt_new(&eval_tmp
);
434 /* Add VRF to the list for this RT. */
435 listnode_add(irt
->vrfs
, bgp_vrf
);
439 * Unmap specified VRF from specified RT. If there are no other
440 * VRFs for this RT, then the RT hash is deleted.
441 * bgp_vrf: BGP VRF specific instance
443 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
, struct vrf_irt_node
*irt
)
445 /* Delete VRF from list for this RT. */
446 listnode_delete(irt
->vrfs
, bgp_vrf
);
447 if (!listnode_head(irt
->vrfs
)) {
448 vrf_import_rt_free(irt
);
453 * Map one RT to specified VNI.
455 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
456 struct ecommunity_val
*eval
)
458 struct irt_node
*irt
;
459 struct ecommunity_val eval_tmp
;
461 /* If using "automatic" RT, we only care about the local-admin
463 * This is to facilitate using VNI as the RT for EBGP peering too.
465 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
466 if (!is_import_rt_configured(vpn
))
467 mask_ecom_global_admin(&eval_tmp
, eval
);
469 irt
= lookup_import_rt(bgp
, &eval_tmp
);
471 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
472 /* Already mapped. */
476 irt
= import_rt_new(bgp
, &eval_tmp
);
480 /* Add VNI to the hash list for this RT. */
481 listnode_add(irt
->vnis
, vpn
);
485 * Unmap specified VNI from specified RT. If there are no other
486 * VNIs for this RT, then the RT hash is deleted.
488 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
489 struct irt_node
*irt
)
491 /* Delete VNI from hash list for this RT. */
492 listnode_delete(irt
->vnis
, vpn
);
493 if (!listnode_head(irt
->vnis
)) {
494 import_rt_free(bgp
, irt
);
498 static void bgp_evpn_get_rmac_nexthop(struct bgpevpn
*vpn
,
499 struct prefix_evpn
*p
,
500 struct attr
*attr
, uint8_t flags
)
502 struct bgp
*bgp_vrf
= vpn
->bgp_vrf
;
504 memset(&attr
->rmac
, 0, sizeof(struct ethaddr
));
508 if (p
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
511 /* Copy sys (pip) RMAC and PIP IP as nexthop
512 * in case of route is self MAC-IP,
513 * advertise-pip and advertise-svi-ip features
515 * Otherwise, for all host MAC-IP route's
518 if (CHECK_FLAG(flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
)
519 && bgp_vrf
->evpn_info
->advertise_pip
&&
520 bgp_vrf
->evpn_info
->is_anycast_mac
) {
522 memcpy(&attr
->rmac
, &bgp_vrf
->evpn_info
->pip_rmac
,
524 attr
->nexthop
= bgp_vrf
->evpn_info
->pip_ip
;
525 attr
->mp_nexthop_global_in
=
526 bgp_vrf
->evpn_info
->pip_ip
;
528 memcpy(&attr
->rmac
, &bgp_vrf
->rmac
, ETH_ALEN
);
532 * Create RT extended community automatically from passed information:
533 * of the form AS:VNI.
534 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
535 * the need is to get a RT value that will be unique across different
536 * VNIs but the same across routers (in the same AS) for a particular
539 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
541 struct ecommunity_val eval
;
542 struct ecommunity
*ecomadd
, *ecom
;
543 bool ecom_found
= false;
544 struct listnode
*node
;
546 if (bgp
->advertise_autort_rfc8365
)
547 vni
|= EVPN_AUTORT_VXLAN
;
548 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
550 ecomadd
= ecommunity_new();
551 ecommunity_add_val(ecomadd
, &eval
);
552 for (ALL_LIST_ELEMENTS_RO(rtl
, node
, ecom
))
553 if (ecommunity_cmp(ecomadd
, ecom
))
557 listnode_add_sort(rtl
, ecomadd
);
561 * Derive RD and RT for a VNI automatically. Invoked at the time of
564 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
566 bgp_evpn_derive_auto_rd(bgp
, vpn
);
567 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
568 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
572 * Convert nexthop (remote VTEP IP) into an IPv6 address.
574 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
576 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
578 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
579 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
583 * Add (update) or delete MACIP from zebra.
585 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
586 struct prefix_evpn
*p
,
587 struct in_addr remote_vtep_ip
, int add
,
588 uint8_t flags
, uint32_t seq
)
592 char buf1
[ETHER_ADDR_STRLEN
];
593 char buf2
[INET6_ADDRSTRLEN
];
594 char buf3
[INET6_ADDRSTRLEN
];
597 if (!zclient
|| zclient
->sock
< 0)
600 /* Don't try to register if Zebra doesn't know of this instance. */
601 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
602 if (BGP_DEBUG(zebra
, ZEBRA
))
603 zlog_debug("%s: No zebra instance to talk to, not installing remote macip",
604 __PRETTY_FUNCTION__
);
610 zclient_create_header(
611 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
613 stream_putl(s
, vpn
->vni
);
614 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
615 /* IP address length and IP address, if any. */
616 if (is_evpn_prefix_ipaddr_none(p
))
619 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
621 stream_putl(s
, ipa_len
);
622 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
624 stream_put_in_addr(s
, &remote_vtep_ip
);
626 /* TX flags - MAC sticky status and/or gateway mac */
627 /* Also TX the sequence number of the best route. */
629 stream_putc(s
, flags
);
633 stream_putw_at(s
, 0, stream_get_endp(s
));
635 if (bgp_debug_zebra(NULL
))
637 "Tx %s MACIP, VNI %u MAC %s IP %s flags 0x%x seq %u remote VTEP %s",
638 add
? "ADD" : "DEL", vpn
->vni
,
639 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
641 ipaddr2str(&p
->prefix
.macip_addr
.ip
,
642 buf3
, sizeof(buf3
)), flags
, seq
,
643 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
646 return zclient_send_message(zclient
);
650 * Add (update) or delete remote VTEP from zebra.
652 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
653 struct prefix_evpn
*p
,
654 int flood_control
, int add
)
659 if (!zclient
|| zclient
->sock
< 0)
662 /* Don't try to register if Zebra doesn't know of this instance. */
663 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
664 if (BGP_DEBUG(zebra
, ZEBRA
))
665 zlog_debug("%s: No zebra instance to talk to, not installing remote vtep",
666 __PRETTY_FUNCTION__
);
673 zclient_create_header(
674 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
676 stream_putl(s
, vpn
->vni
);
677 if (is_evpn_prefix_ipaddr_v4(p
))
678 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
679 else if (is_evpn_prefix_ipaddr_v6(p
)) {
682 "Bad remote IP when trying to %s remote VTEP for VNI %u",
683 add
? "ADD" : "DEL", vpn
->vni
);
686 stream_putl(s
, flood_control
);
688 stream_putw_at(s
, 0, stream_get_endp(s
));
690 if (bgp_debug_zebra(NULL
))
691 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
692 add
? "ADD" : "DEL", vpn
->vni
,
693 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
695 return zclient_send_message(zclient
);
699 * Build extended community for EVPN ES (type-4) route
701 static void build_evpn_type4_route_extcomm(struct evpnes
*es
,
704 struct ecommunity ecom_encap
;
705 struct ecommunity ecom_es_rt
;
706 struct ecommunity_val eval
;
707 struct ecommunity_val eval_es_rt
;
708 bgp_encap_types tnl_type
;
712 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
713 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
714 encode_encap_extcomm(tnl_type
, &eval
);
716 ecom_encap
.val
= (uint8_t *)eval
.val
;
717 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
720 memset(&mac
, 0, sizeof(struct ethaddr
));
721 memset(&ecom_es_rt
, 0, sizeof(ecom_es_rt
));
722 es_get_system_mac(&es
->esi
, &mac
);
723 encode_es_rt_extcomm(&eval_es_rt
, &mac
);
725 ecom_es_rt
.val
= (uint8_t *)eval_es_rt
.val
;
727 ecommunity_merge(attr
->ecommunity
, &ecom_es_rt
);
729 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
733 * Build extended communities for EVPN prefix route.
735 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
738 struct ecommunity ecom_encap
;
739 struct ecommunity ecom_rmac
;
740 struct ecommunity_val eval
;
741 struct ecommunity_val eval_rmac
;
742 bgp_encap_types tnl_type
;
743 struct listnode
*node
, *nnode
;
744 struct ecommunity
*ecom
;
745 struct list
*vrf_export_rtl
= NULL
;
748 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
749 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
750 encode_encap_extcomm(tnl_type
, &eval
);
752 ecom_encap
.val
= (uint8_t *)eval
.val
;
755 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
757 /* Add the export RTs for L3VNI/VRF */
758 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
759 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
761 ecommunity_merge(attr
->ecommunity
, ecom
);
763 /* add the router mac extended community */
764 if (!is_zero_mac(&attr
->rmac
)) {
765 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
766 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
768 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
770 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
773 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
777 * Build extended communities for EVPN route.
778 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
779 * and ENCAP extended communities are applicable for all routes.
780 * The default gateway extended community and MAC mobility (sticky) extended
781 * community are added as needed based on passed settings - only for type-2
782 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
783 * added, if present, based on passed settings - only for non-link-local
786 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
789 struct ecommunity ecom_encap
;
790 struct ecommunity ecom_sticky
;
791 struct ecommunity ecom_default_gw
;
792 struct ecommunity ecom_rmac
;
793 struct ecommunity ecom_na
;
794 struct ecommunity_val eval
;
795 struct ecommunity_val eval_sticky
;
796 struct ecommunity_val eval_default_gw
;
797 struct ecommunity_val eval_rmac
;
798 struct ecommunity_val eval_na
;
800 bgp_encap_types tnl_type
;
801 struct listnode
*node
, *nnode
;
802 struct ecommunity
*ecom
;
804 struct list
*vrf_export_rtl
= NULL
;
807 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
808 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
809 encode_encap_extcomm(tnl_type
, &eval
);
811 ecom_encap
.val
= (uint8_t *)eval
.val
;
814 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
816 /* Add the export RTs for L2VNI */
817 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
818 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
820 /* Add the export RTs for L3VNI if told to - caller determines
821 * when this should be done.
824 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
825 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
826 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
828 attr
->ecommunity
= ecommunity_merge(
829 attr
->ecommunity
, ecom
);
833 /* Add MAC mobility (sticky) if needed. */
836 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
837 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
838 ecom_sticky
.size
= 1;
839 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
841 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
844 /* Add RMAC, if told to. */
846 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
847 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
849 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
851 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
854 /* Add default gateway, if needed. */
855 if (attr
->default_gw
) {
856 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
857 encode_default_gw_extcomm(&eval_default_gw
);
858 ecom_default_gw
.size
= 1;
859 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
861 ecommunity_merge(attr
->ecommunity
, &ecom_default_gw
);
864 if (attr
->router_flag
) {
865 memset(&ecom_na
, 0, sizeof(ecom_na
));
866 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
);
868 ecom_na
.val
= (uint8_t *)eval_na
.val
;
869 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
873 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
877 * Add MAC mobility extended community to attribute.
879 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
881 struct ecommunity ecom_tmp
;
882 struct ecommunity_val eval
;
883 uint8_t *ecom_val_ptr
;
890 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
892 /* Find current MM ecommunity */
895 if (attr
->ecommunity
) {
896 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
897 pnt
= attr
->ecommunity
->val
+ (i
* 8);
901 if (type
== ECOMMUNITY_ENCODE_EVPN
903 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
904 ecom_val_ptr
= (uint8_t *)(attr
->ecommunity
->val
911 /* Update the existing MM ecommunity */
913 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
915 /* Add MM to existing */
917 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
919 ecom_tmp
.val
= (uint8_t *)eval
.val
;
921 if (attr
->ecommunity
)
923 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
925 attr
->ecommunity
= ecommunity_dup(&ecom_tmp
);
929 /* Install EVPN route into zebra. */
930 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
931 struct prefix_evpn
*p
, struct bgp_path_info
*pi
)
937 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
939 if (pi
->attr
->sticky
)
940 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
941 if (pi
->attr
->default_gw
)
942 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
943 if (is_evpn_prefix_ipaddr_v6(p
) &&
944 pi
->attr
->router_flag
)
945 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
946 ret
= bgp_zebra_send_remote_macip(
947 bgp
, vpn
, p
, pi
->attr
->nexthop
, 1, flags
,
948 mac_mobility_seqnum(pi
->attr
));
950 switch (pi
->attr
->pmsi_tnl_type
) {
951 case PMSI_TNLTYPE_INGR_REPL
:
952 flood_control
= VXLAN_FLOOD_HEAD_END_REPL
;
955 case PMSI_TNLTYPE_PIM_SM
:
956 flood_control
= VXLAN_FLOOD_PIM_SM
;
960 flood_control
= VXLAN_FLOOD_DISABLED
;
963 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, flood_control
, 1);
969 /* Uninstall EVPN route from zebra. */
970 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
971 struct prefix_evpn
*p
,
972 struct in_addr remote_vtep_ip
)
976 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
977 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
980 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
,
981 VXLAN_FLOOD_DISABLED
, 0);
987 * Due to MAC mobility, the prior "local" best route has been supplanted
988 * by a "remote" best route. The prior route has to be deleted and withdrawn
991 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
993 struct bgp_path_info
*old_local
)
995 struct bgp_node
*global_rn
;
996 struct bgp_path_info
*pi
;
997 afi_t afi
= AFI_L2VPN
;
998 safi_t safi
= SAFI_EVPN
;
1000 /* Locate route node in the global EVPN routing table. Note that
1001 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1004 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1005 (struct prefix
*)&rn
->p
, &vpn
->prd
);
1007 /* Delete route entry in the global EVPN table. */
1008 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1010 /* Schedule for processing - withdraws to peers happen from
1014 bgp_process(bgp
, global_rn
, afi
, safi
);
1015 bgp_unlock_node(global_rn
);
1018 /* Delete route entry in the VNI route table, caller to remove. */
1019 bgp_path_info_delete(rn
, old_local
);
1022 static struct in_addr
*es_vtep_new(struct in_addr vtep
)
1026 ip
= XCALLOC(MTYPE_BGP_EVPN_ES_VTEP
, sizeof(struct in_addr
));
1028 ip
->s_addr
= vtep
.s_addr
;
1032 static void es_vtep_free(struct in_addr
*ip
)
1034 XFREE(MTYPE_BGP_EVPN_ES_VTEP
, ip
);
1037 /* check if VTEP is already part of the list */
1038 static int is_vtep_present_in_list(struct list
*list
,
1039 struct in_addr vtep
)
1041 struct listnode
*node
= NULL
;
1042 struct in_addr
*tmp
;
1044 for (ALL_LIST_ELEMENTS_RO(list
, node
, tmp
)) {
1045 if (tmp
->s_addr
== vtep
.s_addr
)
1052 * Best path for ES route was changed,
1053 * update the list of VTEPs for this ES
1055 static int evpn_es_install_vtep(struct bgp
*bgp
,
1057 struct prefix_evpn
*p
,
1058 struct in_addr rvtep
)
1060 struct in_addr
*vtep_ip
;
1062 if (is_vtep_present_in_list(es
->vtep_list
, rvtep
))
1066 vtep_ip
= es_vtep_new(rvtep
);
1068 listnode_add_sort(es
->vtep_list
, vtep_ip
);
1073 * Best path for ES route was changed,
1074 * update the list of VTEPs for this ES
1076 static int evpn_es_uninstall_vtep(struct bgp
*bgp
,
1078 struct prefix_evpn
*p
,
1079 struct in_addr rvtep
)
1081 struct listnode
*node
, *nnode
, *node_to_del
= NULL
;
1082 struct in_addr
*tmp
;
1084 for (ALL_LIST_ELEMENTS(es
->vtep_list
, node
, nnode
, tmp
)) {
1085 if (tmp
->s_addr
== rvtep
.s_addr
) {
1092 list_delete_node(es
->vtep_list
, node_to_del
);
1098 * Calculate the best path for a ES(type-4) route.
1100 static int evpn_es_route_select_install(struct bgp
*bgp
,
1102 struct bgp_node
*rn
)
1105 afi_t afi
= AFI_L2VPN
;
1106 safi_t safi
= SAFI_EVPN
;
1107 struct bgp_path_info
*old_select
; /* old best */
1108 struct bgp_path_info
*new_select
; /* new best */
1109 struct bgp_path_info_pair old_and_new
;
1111 /* Compute the best path. */
1112 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
],
1113 &old_and_new
, afi
, safi
);
1114 old_select
= old_and_new
.old
;
1115 new_select
= old_and_new
.new;
1118 * If the best path hasn't changed - see if something needs to be
1121 if (old_select
&& old_select
== new_select
1122 && old_select
->type
== ZEBRA_ROUTE_BGP
1123 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1124 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1125 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1126 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1127 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1128 ret
= evpn_es_install_vtep(bgp
, es
,
1129 (struct prefix_evpn
*)&rn
->p
,
1130 old_select
->attr
->nexthop
);
1132 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1133 bgp_zebra_clear_route_change_flags(rn
);
1137 /* If the user did a "clear" this flag will be set */
1138 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1141 * bestpath has changed; update relevant fields and install or uninstall
1142 * into the zebra RIB.
1144 if (old_select
|| new_select
)
1145 bgp_bump_version(rn
);
1148 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1150 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1151 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1152 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1155 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1156 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1157 ret
= evpn_es_install_vtep(bgp
, es
,
1158 (struct prefix_evpn
*)&rn
->p
,
1159 new_select
->attr
->nexthop
);
1161 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1162 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1163 ret
= evpn_es_uninstall_vtep(
1164 bgp
, es
, (struct prefix_evpn
*)&rn
->p
,
1165 old_select
->attr
->nexthop
);
1168 /* Clear any route change flags. */
1169 bgp_zebra_clear_route_change_flags(rn
);
1171 /* Reap old select bgp_path_info, if it has been removed */
1172 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1173 bgp_path_info_reap(rn
, old_select
);
1179 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1182 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1183 struct bgp_node
*rn
)
1185 struct bgp_path_info
*old_select
, *new_select
;
1186 struct bgp_path_info_pair old_and_new
;
1187 afi_t afi
= AFI_L2VPN
;
1188 safi_t safi
= SAFI_EVPN
;
1191 /* Compute the best path. */
1192 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1194 old_select
= old_and_new
.old
;
1195 new_select
= old_and_new
.new;
1197 /* If the best path hasn't changed - see if there is still something to
1201 if (old_select
&& old_select
== new_select
1202 && old_select
->type
== ZEBRA_ROUTE_BGP
1203 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1204 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1205 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1206 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1207 if (bgp_zebra_has_route_changed(rn
, old_select
))
1208 ret
= evpn_zebra_install(
1209 bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1211 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1212 bgp_zebra_clear_route_change_flags(rn
);
1216 /* If the user did a "clear" this flag will be set */
1217 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1219 /* bestpath has changed; update relevant fields and install or uninstall
1220 * into the zebra RIB.
1222 if (old_select
|| new_select
)
1223 bgp_bump_version(rn
);
1226 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1228 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1229 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1230 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1233 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1234 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1235 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1238 /* If an old best existed and it was a "local" route, the only
1240 * it would be supplanted is due to MAC mobility procedures. So,
1242 * need to do an implicit delete and withdraw that route from
1245 if (old_select
&& old_select
->peer
== bgp
->peer_self
1246 && old_select
->type
== ZEBRA_ROUTE_BGP
1247 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
1248 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
1250 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1251 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1252 ret
= evpn_zebra_uninstall(bgp
, vpn
,
1253 (struct prefix_evpn
*)&rn
->p
,
1254 old_select
->attr
->nexthop
);
1257 /* Clear any route change flags. */
1258 bgp_zebra_clear_route_change_flags(rn
);
1260 /* Reap old select bgp_path_info, if it has been removed */
1261 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1262 bgp_path_info_reap(rn
, old_select
);
1268 * Return true if the local ri for this rn is of type gateway mac
1270 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
1272 struct bgp_path_info
*tmp_pi
= NULL
;
1273 struct bgp_path_info
*local_pi
= NULL
;
1276 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1277 tmp_pi
= tmp_pi
->next
) {
1278 if (tmp_pi
->peer
== bgp
->peer_self
1279 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1280 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1287 return local_pi
->attr
->default_gw
;
1292 * Return true if the local ri for this rn has sticky set
1294 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
1296 struct bgp_path_info
*tmp_pi
;
1297 struct bgp_path_info
*local_pi
;
1300 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1301 tmp_pi
= tmp_pi
->next
) {
1302 if (tmp_pi
->peer
== bgp
->peer_self
1303 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1304 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1311 return local_pi
->attr
->sticky
;
1315 * create or update EVPN type4 route entry.
1316 * This could be in the ES table or the global table.
1317 * TODO: handle remote ES (type4) routes as well
1319 static int update_evpn_type4_route_entry(struct bgp
*bgp
, struct evpnes
*es
,
1320 afi_t afi
, safi_t safi
,
1321 struct bgp_node
*rn
, struct attr
*attr
,
1322 int add
, struct bgp_path_info
**ri
,
1325 char buf
[ESI_STR_LEN
];
1326 char buf1
[INET6_ADDRSTRLEN
];
1327 struct bgp_path_info
*tmp_pi
= NULL
;
1328 struct bgp_path_info
*local_pi
= NULL
; /* local route entry if any */
1329 struct bgp_path_info
*remote_pi
= NULL
; /* remote route entry if any */
1330 struct attr
*attr_new
= NULL
;
1331 struct prefix_evpn
*evp
= NULL
;
1335 evp
= (struct prefix_evpn
*)&rn
->p
;
1337 /* locate the local and remote entries if any */
1338 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1339 tmp_pi
= tmp_pi
->next
) {
1340 if (tmp_pi
->peer
== bgp
->peer_self
1341 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1342 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1344 if (tmp_pi
->type
== ZEBRA_ROUTE_BGP
1345 && tmp_pi
->sub_type
== BGP_ROUTE_IMPORTED
1346 && CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1350 /* we don't expect to see a remote_ri at this point.
1351 * An ES route has esi + vtep_ip as the key,
1352 * We shouldn't see the same route from any other vtep.
1357 "%u ERROR: local es route for ESI: %s Vtep %s also learnt from remote",
1359 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1360 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1364 if (!local_pi
&& !add
)
1367 /* create or update the entry */
1370 /* Add or update attribute to hash */
1371 attr_new
= bgp_attr_intern(attr
);
1373 /* Create new route with its attribute. */
1374 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1375 bgp
->peer_self
, attr_new
, rn
);
1376 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1378 /* add the newly created path to the route-node */
1379 bgp_path_info_add(rn
, tmp_pi
);
1382 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1383 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1386 /* The attribute has changed.
1387 * Add (or update) attribute to hash. */
1388 attr_new
= bgp_attr_intern(attr
);
1389 bgp_path_info_set_flag(rn
, tmp_pi
,
1390 BGP_PATH_ATTR_CHANGED
);
1392 /* Restore route, if needed. */
1393 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1394 bgp_path_info_restore(rn
, tmp_pi
);
1396 /* Unintern existing, set to new. */
1397 bgp_attr_unintern(&tmp_pi
->attr
);
1398 tmp_pi
->attr
= attr_new
;
1399 tmp_pi
->uptime
= bgp_clock();
1403 /* Return back the route entry. */
1408 /* update evpn es (type-4) route */
1409 static int update_evpn_type4_route(struct bgp
*bgp
,
1411 struct prefix_evpn
*p
)
1414 int route_changed
= 0;
1415 char buf
[ESI_STR_LEN
];
1416 char buf1
[INET6_ADDRSTRLEN
];
1417 afi_t afi
= AFI_L2VPN
;
1418 safi_t safi
= SAFI_EVPN
;
1420 struct attr
*attr_new
= NULL
;
1421 struct bgp_node
*rn
= NULL
;
1422 struct bgp_path_info
*pi
= NULL
;
1424 memset(&attr
, 0, sizeof(struct attr
));
1426 /* Build path-attribute for this route. */
1427 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1428 attr
.nexthop
= es
->originator_ip
.ipaddr_v4
;
1429 attr
.mp_nexthop_global_in
= es
->originator_ip
.ipaddr_v4
;
1430 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1432 /* Set up extended community. */
1433 build_evpn_type4_route_extcomm(es
, &attr
);
1435 /* First, create (or fetch) route node within the ESI. */
1436 /* NOTE: There is no RD here. */
1437 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
1439 /* Create or update route entry. */
1440 ret
= update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, &attr
, 1,
1441 &pi
, &route_changed
);
1443 flog_err(EC_BGP_ES_INVALID
,
1444 "%u ERROR: Failed to updated ES route ESI: %s VTEP %s",
1446 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1447 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1451 attr_new
= pi
->attr
;
1453 /* Perform route selection;
1454 * this is just to set the flags correctly
1455 * as local route in the ES always wins.
1457 evpn_es_route_select_install(bgp
, es
, rn
);
1458 bgp_unlock_node(rn
);
1460 /* If this is a new route or some attribute has changed, export the
1461 * route to the global table. The route will be advertised to peers
1462 * from there. Note that this table is a 2-level tree (RD-level +
1463 * Prefix-level) similar to L3VPN routes.
1465 if (route_changed
) {
1466 struct bgp_path_info
*global_pi
;
1468 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1469 (struct prefix
*)p
, &es
->prd
);
1470 update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, attr_new
,
1471 1, &global_pi
, &route_changed
);
1473 /* Schedule for processing and unlock node. */
1474 bgp_process(bgp
, rn
, afi
, safi
);
1475 bgp_unlock_node(rn
);
1478 /* Unintern temporary. */
1479 aspath_unintern(&attr
.aspath
);
1483 static int update_evpn_type5_route_entry(struct bgp
*bgp_evpn
,
1484 struct bgp
*bgp_vrf
, afi_t afi
,
1485 safi_t safi
, struct bgp_node
*rn
,
1486 struct attr
*attr
, int *route_changed
)
1488 struct attr
*attr_new
= NULL
;
1489 struct bgp_path_info
*pi
= NULL
;
1490 mpls_label_t label
= MPLS_INVALID_LABEL
;
1491 struct bgp_path_info
*local_pi
= NULL
;
1492 struct bgp_path_info
*tmp_pi
= NULL
;
1495 /* locate the local route entry if any */
1496 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1497 tmp_pi
= tmp_pi
->next
) {
1498 if (tmp_pi
->peer
== bgp_evpn
->peer_self
1499 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1500 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1505 * create a new route entry if one doesn't exist.
1506 * Otherwise see if route attr has changed
1510 /* route has changed as this is the first entry */
1513 /* Add (or update) attribute to hash. */
1514 attr_new
= bgp_attr_intern(attr
);
1516 /* create the route info from attribute */
1517 pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1518 bgp_evpn
->peer_self
, attr_new
, rn
);
1519 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
1521 /* Type-5 routes advertise the L3-VNI */
1522 bgp_path_info_extra_get(pi
);
1523 vni2label(bgp_vrf
->l3vni
, &label
);
1524 memcpy(&pi
->extra
->label
, &label
, sizeof(label
));
1525 pi
->extra
->num_labels
= 1;
1527 /* add the route entry to route node*/
1528 bgp_path_info_add(rn
, pi
);
1532 if (!attrhash_cmp(tmp_pi
->attr
, attr
)) {
1534 /* attribute changed */
1537 /* The attribute has changed. */
1538 /* Add (or update) attribute to hash. */
1539 attr_new
= bgp_attr_intern(attr
);
1540 bgp_path_info_set_flag(rn
, tmp_pi
,
1541 BGP_PATH_ATTR_CHANGED
);
1543 /* Restore route, if needed. */
1544 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1545 bgp_path_info_restore(rn
, tmp_pi
);
1547 /* Unintern existing, set to new. */
1548 bgp_attr_unintern(&tmp_pi
->attr
);
1549 tmp_pi
->attr
= attr_new
;
1550 tmp_pi
->uptime
= bgp_clock();
1556 /* update evpn type-5 route entry */
1557 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1558 struct attr
*src_attr
)
1560 afi_t afi
= AFI_L2VPN
;
1561 safi_t safi
= SAFI_EVPN
;
1563 struct bgp_node
*rn
= NULL
;
1564 struct bgp
*bgp_evpn
= NULL
;
1565 int route_changed
= 0;
1567 bgp_evpn
= bgp_get_evpn();
1571 /* Build path attribute for this route - use the source attr, if
1572 * present, else treat as locally originated.
1577 memset(&attr
, 0, sizeof(struct attr
));
1578 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1581 /* Advertise Primary IP (PIP) is enabled, send individual
1582 * IP (default instance router-id) as nexthop.
1583 * PIP is disabled or vrr interface is not present
1584 * use anycast-IP as nexthop and anycast RMAC.
1586 if (!bgp_vrf
->evpn_info
->advertise_pip
||
1587 (!bgp_vrf
->evpn_info
->is_anycast_mac
)) {
1588 attr
.nexthop
= bgp_vrf
->originator_ip
;
1589 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1590 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, ETH_ALEN
);
1593 memcpy(&attr
.rmac
, &bgp_vrf
->evpn_info
->pip_rmac
, ETH_ALEN
);
1594 if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
!= INADDR_ANY
) {
1595 attr
.nexthop
= bgp_vrf
->evpn_info
->pip_ip
;
1596 attr
.mp_nexthop_global_in
= bgp_vrf
->evpn_info
->pip_ip
;
1597 } else if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
== INADDR_ANY
)
1598 if (bgp_debug_zebra(NULL
)) {
1599 char buf1
[PREFIX_STRLEN
];
1601 zlog_debug("VRF %s evp %s advertise-pip primary ip is not configured",
1602 vrf_id_to_name(bgp_vrf
->vrf_id
),
1603 prefix2str(evp
, buf1
, sizeof(buf1
)));
1607 if (bgp_debug_zebra(NULL
)) {
1608 char buf
[ETHER_ADDR_STRLEN
];
1609 char buf1
[PREFIX_STRLEN
];
1610 char buf2
[INET6_ADDRSTRLEN
];
1612 zlog_debug("VRF %s type-5 route evp %s RMAC %s nexthop %s",
1613 vrf_id_to_name(bgp_vrf
->vrf_id
),
1614 prefix2str(evp
, buf1
, sizeof(buf1
)),
1615 prefix_mac2str(&attr
.rmac
, buf
, sizeof(buf
)),
1616 inet_ntop(AF_INET
, &attr
.nexthop
, buf2
,
1620 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1622 /* Setup RT and encap extended community */
1623 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1625 /* get the route node in global table */
1626 rn
= bgp_afi_node_get(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1627 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1630 /* create or update the route entry within the route node */
1631 update_evpn_type5_route_entry(bgp_evpn
, bgp_vrf
, afi
, safi
, rn
, &attr
,
1634 /* schedule for processing and unlock node */
1635 if (route_changed
) {
1636 bgp_process(bgp_evpn
, rn
, afi
, safi
);
1637 bgp_unlock_node(rn
);
1640 /* uninten temporary */
1642 aspath_unintern(&attr
.aspath
);
1647 * Create or update EVPN route entry. This could be in the VNI route table
1648 * or the global route table.
1650 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1651 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1652 struct attr
*attr
, int add
,
1653 struct bgp_path_info
**pi
, uint8_t flags
,
1656 struct bgp_path_info
*tmp_pi
;
1657 struct bgp_path_info
*local_pi
;
1658 struct attr
*attr_new
;
1659 mpls_label_t label
[BGP_MAX_LABELS
];
1660 uint32_t num_labels
= 1;
1661 int route_change
= 1;
1663 struct prefix_evpn
*evp
;
1666 evp
= (struct prefix_evpn
*)&rn
->p
;
1667 memset(&label
, 0, sizeof(label
));
1669 /* See if this is an update of an existing route, or a new add. */
1671 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1672 tmp_pi
= tmp_pi
->next
) {
1673 if (tmp_pi
->peer
== bgp
->peer_self
1674 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1675 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1679 /* If route doesn't exist already, create a new one, if told to.
1680 * Otherwise act based on whether the attributes of the route have
1683 if (!local_pi
&& !add
)
1686 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1687 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1688 add_mac_mobility_to_attr(seq
, attr
);
1691 /* Add (or update) attribute to hash. */
1692 attr_new
= bgp_attr_intern(attr
);
1694 /* Extract MAC mobility sequence number, if any. */
1695 attr_new
->mm_seqnum
=
1696 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1697 attr_new
->sticky
= sticky
;
1699 /* Create new route with its attribute. */
1700 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1701 bgp
->peer_self
, attr_new
, rn
);
1702 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1703 bgp_path_info_extra_get(tmp_pi
);
1705 /* The VNI goes into the 'label' field of the route */
1706 vni2label(vpn
->vni
, &label
[0]);
1708 /* Type-2 routes may carry a second VNI - the L3-VNI.
1709 * Only attach second label if we are advertising two labels for
1712 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1713 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1716 l3vni
= bgpevpn_get_l3vni(vpn
);
1718 vni2label(l3vni
, &label
[1]);
1723 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1724 tmp_pi
->extra
->num_labels
= num_labels
;
1725 /* Mark route as self type-2 route */
1726 if (flags
&& CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1727 tmp_pi
->extra
->af_flags
= BGP_EVPN_MACIP_TYPE_SVI_IP
;
1728 bgp_path_info_add(rn
, tmp_pi
);
1731 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1732 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1736 * The attributes have changed, type-2 routes needs to
1737 * be advertised with right labels.
1739 vni2label(vpn
->vni
, &label
[0]);
1740 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1741 && CHECK_FLAG(vpn
->flags
,
1742 VNI_FLAG_USE_TWO_LABELS
)) {
1745 l3vni
= bgpevpn_get_l3vni(vpn
);
1747 vni2label(l3vni
, &label
[1]);
1751 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1752 tmp_pi
->extra
->num_labels
= num_labels
;
1754 /* The attribute has changed. */
1755 /* Add (or update) attribute to hash. */
1756 attr_new
= bgp_attr_intern(attr
);
1757 bgp_path_info_set_flag(rn
, tmp_pi
,
1758 BGP_PATH_ATTR_CHANGED
);
1760 /* Extract MAC mobility sequence number, if any. */
1761 attr_new
->mm_seqnum
=
1762 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1763 attr_new
->sticky
= sticky
;
1765 /* Restore route, if needed. */
1766 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1767 bgp_path_info_restore(rn
, tmp_pi
);
1769 /* Unintern existing, set to new. */
1770 bgp_attr_unintern(&tmp_pi
->attr
);
1771 tmp_pi
->attr
= attr_new
;
1772 tmp_pi
->uptime
= bgp_clock();
1776 /* Return back the route entry. */
1778 return route_change
;
1781 static void evpn_zebra_reinstall_best_route(struct bgp
*bgp
,
1782 struct bgpevpn
*vpn
, struct bgp_node
*rn
)
1784 struct bgp_path_info
*tmp_ri
;
1785 struct bgp_path_info
*curr_select
= NULL
;
1787 for (tmp_ri
= bgp_node_get_bgp_path_info(rn
);
1788 tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1789 if (CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_SELECTED
)) {
1790 curr_select
= tmp_ri
;
1795 if (curr_select
&& curr_select
->type
== ZEBRA_ROUTE_BGP
1796 && curr_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1797 evpn_zebra_install(bgp
, vpn
,
1798 (struct prefix_evpn
*)&rn
->p
,
1803 * If the local route was not selected evict it and tell zebra to re-add
1804 * the best remote dest.
1806 * Typically a local path added by zebra is expected to be selected as
1807 * best. In which case when a remote path wins as best (later)
1808 * evpn_route_select_install itself evicts the older-local-best path.
1810 * However if bgp's add and zebra's add cross paths (race condition) it
1811 * is possible that the local path is no longer the "older" best path.
1812 * It is a path that was never designated as best and hence requires
1813 * additional handling to prevent bgp from injecting and holding on to a
1814 * non-best local path.
1816 static void evpn_cleanup_local_non_best_route(struct bgp
*bgp
,
1817 struct bgpevpn
*vpn
,
1818 struct bgp_node
*rn
,
1819 struct bgp_path_info
*local_pi
)
1821 char buf
[PREFIX_STRLEN
];
1823 /* local path was not picked as the winner; kick it out */
1824 if (bgp_debug_zebra(NULL
)) {
1825 zlog_debug("evicting local evpn prefix %s as remote won",
1826 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
1828 evpn_delete_old_local_route(bgp
, vpn
, rn
, local_pi
);
1829 bgp_path_info_reap(rn
, local_pi
);
1831 /* tell zebra to re-add the best remote path */
1832 evpn_zebra_reinstall_best_route(bgp
, vpn
, rn
);
1836 * Create or update EVPN route (of type based on prefix) for specified VNI
1837 * and schedule for processing.
1839 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1840 struct prefix_evpn
*p
, uint8_t flags
,
1843 struct bgp_node
*rn
;
1845 struct attr
*attr_new
;
1846 int add_l3_ecomm
= 0;
1847 struct bgp_path_info
*pi
;
1848 afi_t afi
= AFI_L2VPN
;
1849 safi_t safi
= SAFI_EVPN
;
1852 memset(&attr
, 0, sizeof(struct attr
));
1854 /* Build path-attribute for this route. */
1855 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1856 attr
.nexthop
= vpn
->originator_ip
;
1857 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1858 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1859 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1860 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1861 attr
.router_flag
= CHECK_FLAG(flags
,
1862 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1864 /* PMSI is only needed for type-3 routes */
1865 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
1866 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1867 attr
.pmsi_tnl_type
= PMSI_TNLTYPE_INGR_REPL
;
1870 /* router mac is only needed for type-2 routes here. */
1871 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
1872 uint8_t af_flags
= 0;
1874 if (CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1875 SET_FLAG(af_flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
);
1877 bgp_evpn_get_rmac_nexthop(vpn
, p
, &attr
, af_flags
);
1879 if (bgp_debug_zebra(NULL
)) {
1880 char buf
[ETHER_ADDR_STRLEN
];
1881 char buf1
[PREFIX_STRLEN
];
1883 zlog_debug("VRF %s vni %u type-2 route evp %s RMAC %s nexthop %s",
1885 vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
) : " ",
1887 prefix2str(p
, buf1
, sizeof(buf1
)),
1888 prefix_mac2str(&attr
.rmac
, buf
,
1890 inet_ntoa(attr
.mp_nexthop_global_in
));
1894 vni2label(vpn
->vni
, &(attr
.label
));
1896 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1897 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1900 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1901 (is_evpn_prefix_ipaddr_v4(p
) ||
1902 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1903 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1904 bgpevpn_get_l3vni(vpn
))
1907 /* Set up extended community. */
1908 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1910 /* First, create (or fetch) route node within the VNI. */
1911 /* NOTE: There is no RD here. */
1912 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1914 /* Create or update route entry. */
1915 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1916 1, &pi
, flags
, seq
);
1918 attr_new
= pi
->attr
;
1920 /* lock ri to prevent freeing in evpn_route_select_install */
1921 bgp_path_info_lock(pi
);
1923 /* Perform route selection. Normally, the local route in the
1924 * VNI is expected to win and be the best route. However, if
1925 * there is a race condition where a host moved from local to
1926 * remote and the remote route was received in BGP just prior
1927 * to the local MACIP notification from zebra, the remote
1928 * route would win, and we should evict the defunct local route
1929 * and (re)install the remote route into zebra.
1931 evpn_route_select_install(bgp
, vpn
, rn
);
1933 * If the new local route was not selected evict it and tell zebra
1934 * to re-add the best remote dest. BGP doesn't retain non-best local
1937 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1939 evpn_cleanup_local_non_best_route(bgp
, vpn
, rn
, pi
);
1941 bgp_path_info_unlock(pi
);
1943 bgp_unlock_node(rn
);
1945 /* If this is a new route or some attribute has changed, export the
1946 * route to the global table. The route will be advertised to peers
1947 * from there. Note that this table is a 2-level tree (RD-level +
1948 * Prefix-level) similar to L3VPN routes.
1951 struct bgp_path_info
*global_pi
;
1953 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1954 (struct prefix
*)p
, &vpn
->prd
);
1955 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1,
1956 &global_pi
, flags
, seq
);
1958 /* Schedule for processing and unlock node. */
1959 bgp_process(bgp
, rn
, afi
, safi
);
1960 bgp_unlock_node(rn
);
1963 /* Unintern temporary. */
1964 aspath_unintern(&attr
.aspath
);
1970 * Delete EVPN route entry.
1971 * The entry can be in ESI/VNI table or the global table.
1973 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1974 struct bgp_node
*rn
,
1975 struct bgp_path_info
**pi
)
1977 struct bgp_path_info
*tmp_pi
;
1981 /* Now, find matching route. */
1982 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1983 tmp_pi
= tmp_pi
->next
)
1984 if (tmp_pi
->peer
== bgp
->peer_self
1985 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1986 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1991 /* Mark route for delete. */
1993 bgp_path_info_delete(rn
, tmp_pi
);
1998 /* Delete EVPN ES (type-4) route */
1999 static int delete_evpn_type4_route(struct bgp
*bgp
,
2001 struct prefix_evpn
*p
)
2003 afi_t afi
= AFI_L2VPN
;
2004 safi_t safi
= SAFI_EVPN
;
2005 struct bgp_path_info
*pi
;
2006 struct bgp_node
*rn
= NULL
; /* rn in esi table */
2007 struct bgp_node
*global_rn
= NULL
; /* rn in global table */
2009 /* First, locate the route node within the ESI.
2010 * If it doesn't exist, ther is nothing to do.
2011 * Note: there is no RD here.
2013 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2017 /* Next, locate route node in the global EVPN routing table.
2018 * Note that this table is a 2-level tree (RD-level + Prefix-level)
2020 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
2021 (struct prefix
*)p
, &es
->prd
);
2024 /* Delete route entry in the global EVPN table. */
2025 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
2027 /* Schedule for processing - withdraws to peers happen from
2031 bgp_process(bgp
, global_rn
, afi
, safi
);
2032 bgp_unlock_node(global_rn
);
2036 * Delete route entry in the ESI route table.
2037 * This can just be removed.
2039 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2041 bgp_path_info_reap(rn
, pi
);
2042 bgp_unlock_node(rn
);
2046 /* Delete EVPN type5 route */
2047 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
2049 afi_t afi
= AFI_L2VPN
;
2050 safi_t safi
= SAFI_EVPN
;
2051 struct bgp_node
*rn
= NULL
;
2052 struct bgp_path_info
*pi
= NULL
;
2053 struct bgp
*bgp_evpn
= NULL
; /* evpn bgp instance */
2055 bgp_evpn
= bgp_get_evpn();
2059 /* locate the global route entry for this type-5 prefix */
2060 rn
= bgp_afi_node_lookup(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
2061 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
2065 delete_evpn_route_entry(bgp_evpn
, afi
, safi
, rn
, &pi
);
2067 bgp_process(bgp_evpn
, rn
, afi
, safi
);
2068 bgp_unlock_node(rn
);
2073 * Delete EVPN route (of type based on prefix) for specified VNI and
2074 * schedule for processing.
2076 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2077 struct prefix_evpn
*p
)
2079 struct bgp_node
*rn
, *global_rn
;
2080 struct bgp_path_info
*pi
;
2081 afi_t afi
= AFI_L2VPN
;
2082 safi_t safi
= SAFI_EVPN
;
2084 /* First, locate the route node within the VNI. If it doesn't exist,
2086 * is nothing further to do.
2088 /* NOTE: There is no RD here. */
2089 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2093 /* Next, locate route node in the global EVPN routing table. Note that
2094 * this table is a 2-level tree (RD-level + Prefix-level) similar to
2097 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
2098 (struct prefix
*)p
, &vpn
->prd
);
2100 /* Delete route entry in the global EVPN table. */
2101 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
2103 /* Schedule for processing - withdraws to peers happen from
2107 bgp_process(bgp
, global_rn
, afi
, safi
);
2108 bgp_unlock_node(global_rn
);
2111 /* Delete route entry in the VNI route table. This can just be removed.
2113 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2115 bgp_path_info_reap(rn
, pi
);
2116 evpn_route_select_install(bgp
, vpn
, rn
);
2118 bgp_unlock_node(rn
);
2124 * Update all type-2 (MACIP) local routes for this VNI - these should also
2125 * be scheduled for advertise to peers.
2127 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2131 struct bgp_node
*rn
;
2132 struct bgp_path_info
*pi
, *tmp_pi
;
2134 struct attr
*attr_new
;
2136 int add_l3_ecomm
= 0;
2141 /* Walk this VNI's route table and update local type-2 routes. For any
2142 * routes updated, update corresponding entry in the global table too.
2144 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2145 rn
= bgp_route_next(rn
)) {
2146 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2147 struct bgp_node
*rd_rn
;
2148 struct bgp_path_info
*global_pi
;
2150 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2153 /* Identify local route. */
2154 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
2155 tmp_pi
= tmp_pi
->next
) {
2156 if (tmp_pi
->peer
== bgp
->peer_self
2157 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2158 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2166 * Build attribute per local route as the MAC mobility and
2167 * some other values could differ for different routes. The
2168 * attributes will be shared in the hash table.
2170 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
2171 attr
.nexthop
= vpn
->originator_ip
;
2172 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
2173 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
2174 bgp_evpn_get_rmac_nexthop(vpn
, evp
, &attr
,
2175 tmp_pi
->extra
->af_flags
);
2177 if (evpn_route_is_sticky(bgp
, rn
))
2179 else if (evpn_route_is_def_gw(bgp
, rn
)) {
2180 attr
.default_gw
= 1;
2181 if (is_evpn_prefix_ipaddr_v6(evp
))
2182 attr
.router_flag
= 1;
2185 if (bgp_debug_zebra(NULL
)) {
2186 char buf
[ETHER_ADDR_STRLEN
];
2187 char buf1
[PREFIX_STRLEN
];
2189 zlog_debug("VRF %s vni %u evp %s RMAC %s nexthop %s",
2191 vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
) : " ",
2193 prefix2str(evp
, buf1
, sizeof(buf1
)),
2194 prefix_mac2str(&attr
.rmac
, buf
, sizeof(buf
)),
2195 inet_ntoa(attr
.mp_nexthop_global_in
));
2198 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2199 * using L3 VNI for type-2 routes also.
2201 if ((is_evpn_prefix_ipaddr_v4(evp
) ||
2202 !IN6_IS_ADDR_LINKLOCAL(
2203 &evp
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
2204 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
2205 bgpevpn_get_l3vni(vpn
))
2208 /* Set up extended community. */
2209 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2211 seq
= mac_mobility_seqnum(tmp_pi
->attr
);
2213 /* Update the route entry. */
2214 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
, 0, &pi
,
2217 /* lock ri to prevent freeing in evpn_route_select_install */
2218 bgp_path_info_lock(pi
);
2220 /* Perform route selection. Normally, the local route in the
2221 * VNI is expected to win and be the best route. However,
2222 * under peculiar situations (e.g., tunnel (next hop) IP change
2223 * that causes best selection to be based on next hop), a
2224 * remote route could win. If the local route is the best,
2225 * ensure it is updated in the global EVPN route table and
2226 * advertised to peers; otherwise, ensure it is evicted and
2227 * (re)install the remote route into zebra.
2229 evpn_route_select_install(bgp
, vpn
, rn
);
2230 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
2231 evpn_cleanup_local_non_best_route(bgp
, vpn
, rn
, pi
);
2233 bgp_path_info_unlock(pi
);
2235 attr_new
= pi
->attr
;
2237 bgp_path_info_unlock(pi
);
2239 /* Update route in global routing table. */
2240 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2241 (struct prefix
*)evp
, &vpn
->prd
);
2243 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
,
2244 attr_new
, 0, &global_pi
, 0,
2245 mac_mobility_seqnum(attr_new
));
2247 /* Schedule for processing and unlock node. */
2248 bgp_process(bgp
, rd_rn
, afi
, safi
);
2249 bgp_unlock_node(rd_rn
);
2252 /* Unintern temporary. */
2253 aspath_unintern(&attr
.aspath
);
2261 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2262 * global routing table. These are also scheduled for withdraw from peers.
2264 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2268 struct bgp_node
*rdrn
, *rn
;
2269 struct bgp_table
*table
;
2270 struct bgp_path_info
*pi
;
2275 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
2276 if (rdrn
&& bgp_node_has_bgp_path_info_data(rdrn
)) {
2277 table
= bgp_node_get_bgp_table_info(rdrn
);
2278 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2279 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2281 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2284 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2286 bgp_process(bgp
, rn
, afi
, safi
);
2290 /* Unlock RD node. */
2292 bgp_unlock_node(rdrn
);
2298 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2299 * table as well as the per-VNI route table.
2301 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2305 struct bgp_node
*rn
;
2306 struct bgp_path_info
*pi
;
2311 /* First, walk the global route table for this VNI's type-2 local
2313 * EVPN routes are a 2-level table, first get the RD table.
2315 delete_global_type2_routes(bgp
, vpn
);
2317 /* Next, walk this VNI's route table and delete local type-2 routes. */
2318 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2319 rn
= bgp_route_next(rn
)) {
2320 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2322 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2325 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2327 /* Route entry in local table gets deleted immediately. */
2329 bgp_path_info_reap(rn
, pi
);
2336 * Delete all routes in per ES route-table
2338 static int delete_all_es_routes(struct bgp
*bgp
, struct evpnes
*es
)
2340 struct bgp_node
*rn
;
2341 struct bgp_path_info
*pi
, *nextpi
;
2343 /* Walk this ES's route table and delete all routes. */
2344 for (rn
= bgp_table_top(es
->route_table
); rn
;
2345 rn
= bgp_route_next(rn
)) {
2346 for (pi
= bgp_node_get_bgp_path_info(rn
);
2347 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2348 bgp_path_info_delete(rn
, pi
);
2349 bgp_path_info_reap(rn
, pi
);
2357 * Delete all routes in the per-VNI route table.
2359 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2361 struct bgp_node
*rn
;
2362 struct bgp_path_info
*pi
, *nextpi
;
2364 /* Walk this VNI's route table and delete all routes. */
2365 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2366 rn
= bgp_route_next(rn
)) {
2367 for (pi
= bgp_node_get_bgp_path_info(rn
);
2368 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2369 bgp_path_info_delete(rn
, pi
);
2370 bgp_path_info_reap(rn
, pi
);
2377 /* BUM traffic flood mode per-l2-vni */
2378 static int bgp_evpn_vni_flood_mode_get(struct bgp
*bgp
,
2379 struct bgpevpn
*vpn
)
2381 /* if flooding has been globally disabled per-vni mode is
2384 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
2385 return VXLAN_FLOOD_DISABLED
;
2387 /* if mcast group ip has been specified we use a PIM-SM MDT */
2388 if (vpn
->mcast_grp
.s_addr
!= INADDR_ANY
)
2389 return VXLAN_FLOOD_PIM_SM
;
2391 /* default is ingress replication */
2392 return VXLAN_FLOOD_HEAD_END_REPL
;
2396 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2397 * export RT getting modified or change to tunnel IP. Note that these
2398 * situations need the route in the per-VNI table as well as the global
2399 * table to be updated (as attributes change).
2401 int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2404 struct prefix_evpn p
;
2406 /* Update and advertise the type-3 route (only one) followed by the
2407 * locally learnt type-2 routes (MACIP) - for this VNI.
2409 * RT-3 only if doing head-end replication
2411 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
2412 == VXLAN_FLOOD_HEAD_END_REPL
) {
2413 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2414 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0);
2419 return update_all_type2_routes(bgp
, vpn
);
2422 /* Delete (and withdraw) local routes for specified ES from global and ES table.
2423 * Also remove all other routes from the per ES table.
2424 * Invoked when ES is deleted.
2426 static int delete_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
2429 char buf
[ESI_STR_LEN
];
2430 struct prefix_evpn p
;
2432 /* Delete and withdraw locally learnt ES route */
2433 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
.ipaddr_v4
);
2434 ret
= delete_evpn_type4_route(bgp
, es
, &p
);
2436 flog_err(EC_BGP_EVPN_ROUTE_DELETE
,
2437 "%u failed to delete type-4 route for ESI %s",
2438 bgp
->vrf_id
, esi_to_str(&es
->esi
, buf
, sizeof(buf
)));
2441 /* Delete all routes from per ES table */
2442 return delete_all_es_routes(bgp
, es
);
2446 * Delete (and withdraw) local routes for specified VNI from the global
2447 * table and per-VNI table. After this, remove all other routes from
2448 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2449 * (advertise-all-vni) being disabled.
2451 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2454 struct prefix_evpn p
;
2456 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2457 * followed by type-3 routes (only one) - for this VNI.
2459 ret
= delete_all_type2_routes(bgp
, vpn
);
2463 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2464 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2468 /* Delete all routes from the per-VNI table. */
2469 return delete_all_vni_routes(bgp
, vpn
);
2473 * There is a flood mcast IP address change. Update the mcast-grp and
2474 * remove the type-3 route if any. A new type-3 route will be generated
2475 * post tunnel_ip update if the new flood mode is head-end-replication.
2477 static int bgp_evpn_mcast_grp_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2478 struct in_addr mcast_grp
)
2480 struct prefix_evpn p
;
2482 vpn
->mcast_grp
= mcast_grp
;
2484 if (is_vni_live(vpn
)) {
2485 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2486 delete_evpn_route(bgp
, vpn
, &p
);
2493 * There is a tunnel endpoint IP address change for this VNI, delete
2494 * prior type-3 route (if needed) and update.
2495 * Note: Route re-advertisement happens elsewhere after other processing
2498 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2499 struct in_addr originator_ip
)
2501 struct prefix_evpn p
;
2503 /* If VNI is not live, we only need to update the originator ip */
2504 if (!is_vni_live(vpn
)) {
2505 vpn
->originator_ip
= originator_ip
;
2509 /* Update the tunnel-ip hash */
2510 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2511 bgp_tip_add(bgp
, &originator_ip
);
2513 /* filter routes as martian nexthop db has changed */
2514 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2516 /* Need to withdraw type-3 route as the originator IP is part
2519 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2520 delete_evpn_route(bgp
, vpn
, &p
);
2522 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2523 vpn
->originator_ip
= originator_ip
;
2527 static struct bgp_path_info
*
2528 bgp_create_evpn_bgp_path_info(struct bgp_path_info
*parent_pi
,
2529 struct bgp_node
*rn
)
2531 struct attr
*attr_new
;
2532 struct bgp_path_info
*pi
;
2534 /* Add (or update) attribute to hash. */
2535 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2537 /* Create new route with its attribute. */
2538 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0, parent_pi
->peer
,
2540 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2541 bgp_path_info_extra_get(pi
);
2542 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2543 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2544 if (parent_pi
->extra
) {
2545 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2546 sizeof(pi
->extra
->label
));
2547 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2549 bgp_path_info_add(rn
, pi
);
2554 /* Install EVPN route entry in ES */
2555 static int install_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2556 struct prefix_evpn
*p
,
2557 struct bgp_path_info
*parent_pi
)
2560 struct bgp_node
*rn
= NULL
;
2561 struct bgp_path_info
*pi
= NULL
;
2562 struct attr
*attr_new
= NULL
;
2564 /* Create (or fetch) route within the VNI.
2565 * NOTE: There is no RD here.
2567 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
2569 /* Check if route entry is already present. */
2570 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2572 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2576 /* Add (or update) attribute to hash. */
2577 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2579 /* Create new route with its attribute. */
2580 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2581 parent_pi
->peer
, attr_new
, rn
);
2582 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2583 bgp_path_info_extra_get(pi
);
2584 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2585 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2586 bgp_path_info_add(rn
, pi
);
2588 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2589 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2590 bgp_unlock_node(rn
);
2593 /* The attribute has changed. */
2594 /* Add (or update) attribute to hash. */
2595 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2597 /* Restore route, if needed. */
2598 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2599 bgp_path_info_restore(rn
, pi
);
2601 /* Mark if nexthop has changed. */
2602 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2603 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2605 /* Unintern existing, set to new. */
2606 bgp_attr_unintern(&pi
->attr
);
2607 pi
->attr
= attr_new
;
2608 pi
->uptime
= bgp_clock();
2611 /* Perform route selection and update zebra, if required. */
2612 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2617 * Install route entry into the VRF routing table and invoke route selection.
2619 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2620 struct prefix_evpn
*evp
,
2621 struct bgp_path_info
*parent_pi
)
2623 struct bgp_node
*rn
;
2624 struct bgp_path_info
*pi
;
2626 struct attr
*attr_new
;
2629 struct prefix
*pp
= &p
;
2632 char buf
[PREFIX_STRLEN
];
2633 char buf1
[PREFIX_STRLEN
];
2635 memset(pp
, 0, sizeof(struct prefix
));
2636 ip_prefix_from_evpn_prefix(evp
, pp
);
2638 if (bgp_debug_zebra(NULL
)) {
2640 "import evpn prefix %s as ip prefix %s in vrf %s",
2641 prefix2str(evp
, buf
, sizeof(buf
)),
2642 prefix2str(pp
, buf1
, sizeof(buf
)),
2643 vrf_id_to_name(bgp_vrf
->vrf_id
));
2646 /* Create (or fetch) route within the VRF. */
2647 /* NOTE: There is no RD here. */
2648 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2650 safi
= SAFI_UNICAST
;
2651 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2652 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2654 safi
= SAFI_UNICAST
;
2655 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2659 /* EVPN routes currently only support a IPv4 next hop which corresponds
2660 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2661 * or prefix route, we have to convert the next hop to an IPv4-mapped
2662 * address for the rest of the code to flow through. In the case of IPv4,
2663 * make sure to set the flag for next hop attribute.
2665 attr
= *parent_pi
->attr
;
2667 evpn_convert_nexthop_to_ipv6(&attr
);
2669 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2671 /* Check if route entry is already present. */
2672 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2674 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2678 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, rn
);
2680 if (attrhash_cmp(pi
->attr
, &attr
)
2681 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2682 bgp_unlock_node(rn
);
2685 /* The attribute has changed. */
2686 /* Add (or update) attribute to hash. */
2687 attr_new
= bgp_attr_intern(&attr
);
2689 /* Restore route, if needed. */
2690 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2691 bgp_path_info_restore(rn
, pi
);
2693 /* Mark if nexthop has changed. */
2695 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2697 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2698 &attr_new
->mp_nexthop_global
)))
2699 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2701 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_ATTR_CHANGED
);
2702 /* Unintern existing, set to new. */
2703 bgp_attr_unintern(&pi
->attr
);
2704 pi
->attr
= attr_new
;
2705 pi
->uptime
= bgp_clock();
2708 bgp_aggregate_increment(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2710 /* Perform route selection and update zebra, if required. */
2711 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2713 /* Process for route leaking. */
2714 vpn_leak_from_vrf_update(bgp_get_default(), bgp_vrf
, pi
);
2716 bgp_unlock_node(rn
);
2722 * Install route entry into the VNI routing table and invoke route selection.
2724 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2725 struct prefix_evpn
*p
,
2726 struct bgp_path_info
*parent_pi
)
2728 struct bgp_node
*rn
;
2729 struct bgp_path_info
*pi
;
2730 struct attr
*attr_new
;
2733 /* Create (or fetch) route within the VNI. */
2734 /* NOTE: There is no RD here. */
2735 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2737 /* Check if route entry is already present. */
2738 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2740 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2744 /* Create an info */
2745 (void)bgp_create_evpn_bgp_path_info(parent_pi
, rn
);
2747 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2748 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2749 bgp_unlock_node(rn
);
2752 /* The attribute has changed. */
2753 /* Add (or update) attribute to hash. */
2754 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2756 /* Restore route, if needed. */
2757 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2758 bgp_path_info_restore(rn
, pi
);
2760 /* Mark if nexthop has changed. */
2761 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2762 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2764 /* Unintern existing, set to new. */
2765 bgp_attr_unintern(&pi
->attr
);
2766 pi
->attr
= attr_new
;
2767 pi
->uptime
= bgp_clock();
2770 /* Perform route selection and update zebra, if required. */
2771 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2773 bgp_unlock_node(rn
);
2778 /* Uninstall EVPN route entry from ES route table */
2779 static int uninstall_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2780 struct prefix_evpn
*p
,
2781 struct bgp_path_info
*parent_pi
)
2784 struct bgp_node
*rn
;
2785 struct bgp_path_info
*pi
;
2787 if (!es
->route_table
)
2790 /* Locate route within the ESI.
2791 * NOTE: There is no RD here.
2793 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2797 /* Find matching route entry. */
2798 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2800 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2806 /* Mark entry for deletion */
2807 bgp_path_info_delete(rn
, pi
);
2809 /* Perform route selection and update zebra, if required. */
2810 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2812 /* Unlock route node. */
2813 bgp_unlock_node(rn
);
2819 * Uninstall route entry from the VRF routing table and send message
2820 * to zebra, if appropriate.
2822 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2823 struct prefix_evpn
*evp
,
2824 struct bgp_path_info
*parent_pi
)
2826 struct bgp_node
*rn
;
2827 struct bgp_path_info
*pi
;
2830 struct prefix
*pp
= &p
;
2833 char buf
[PREFIX_STRLEN
];
2834 char buf1
[PREFIX_STRLEN
];
2836 memset(pp
, 0, sizeof(struct prefix
));
2837 ip_prefix_from_evpn_prefix(evp
, pp
);
2839 if (bgp_debug_zebra(NULL
)) {
2841 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2842 prefix2str(evp
, buf
, sizeof(buf
)),
2843 prefix2str(pp
, buf1
, sizeof(buf
)),
2844 vrf_id_to_name(bgp_vrf
->vrf_id
));
2847 /* Locate route within the VRF. */
2848 /* NOTE: There is no RD here. */
2849 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2851 safi
= SAFI_UNICAST
;
2852 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2855 safi
= SAFI_UNICAST
;
2856 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2862 /* Find matching route entry. */
2863 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2865 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2871 /* Process for route leaking. */
2872 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp_vrf
, pi
);
2874 bgp_aggregate_decrement(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2876 /* Mark entry for deletion */
2877 bgp_path_info_delete(rn
, pi
);
2879 /* Perform route selection and update zebra, if required. */
2880 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2882 /* Unlock route node. */
2883 bgp_unlock_node(rn
);
2889 * Uninstall route entry from the VNI routing table and send message
2890 * to zebra, if appropriate.
2892 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2893 struct prefix_evpn
*p
,
2894 struct bgp_path_info
*parent_pi
)
2896 struct bgp_node
*rn
;
2897 struct bgp_path_info
*pi
;
2900 /* Locate route within the VNI. */
2901 /* NOTE: There is no RD here. */
2902 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2906 /* Find matching route entry. */
2907 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2909 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2915 /* Mark entry for deletion */
2916 bgp_path_info_delete(rn
, pi
);
2918 /* Perform route selection and update zebra, if required. */
2919 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2921 /* Unlock route node. */
2922 bgp_unlock_node(rn
);
2928 * Given a prefix, see if it belongs to ES.
2930 static int is_prefix_matching_for_es(struct prefix_evpn
*p
,
2933 /* if not an ES route return false */
2934 if (p
->prefix
.route_type
!= BGP_EVPN_ES_ROUTE
)
2937 if (memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
)) == 0)
2944 * Given a route entry and a VRF, see if this route entry should be
2945 * imported into the VRF i.e., RTs match.
2947 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2948 struct bgp_path_info
*pi
)
2950 struct attr
*attr
= pi
->attr
;
2951 struct ecommunity
*ecom
;
2955 /* Route should have valid RT to be even considered. */
2956 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2959 ecom
= attr
->ecommunity
;
2960 if (!ecom
|| !ecom
->size
)
2963 /* For each extended community RT, see if it matches this VNI. If any RT
2964 * matches, we're done.
2966 for (i
= 0; i
< ecom
->size
; i
++) {
2968 uint8_t type
, sub_type
;
2969 struct ecommunity_val
*eval
;
2970 struct ecommunity_val eval_tmp
;
2971 struct vrf_irt_node
*irt
;
2973 /* Only deal with RTs */
2974 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2975 eval
= (struct ecommunity_val
*)(ecom
->val
2976 + (i
* ECOMMUNITY_SIZE
));
2979 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2982 /* See if this RT matches specified VNIs import RTs */
2983 irt
= lookup_vrf_import_rt(eval
);
2985 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2988 /* Also check for non-exact match. In this, we mask out the AS
2990 * only check on the local-admin sub-field. This is to
2992 * VNI as the RT for EBGP peering too.
2995 if (type
== ECOMMUNITY_ENCODE_AS
2996 || type
== ECOMMUNITY_ENCODE_AS4
2997 || type
== ECOMMUNITY_ENCODE_IP
) {
2998 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2999 mask_ecom_global_admin(&eval_tmp
, eval
);
3000 irt
= lookup_vrf_import_rt(&eval_tmp
);
3003 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
3011 * Given a route entry and a VNI, see if this route entry should be
3012 * imported into the VNI i.e., RTs match.
3014 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
3015 struct bgp_path_info
*pi
)
3017 struct attr
*attr
= pi
->attr
;
3018 struct ecommunity
*ecom
;
3022 /* Route should have valid RT to be even considered. */
3023 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3026 ecom
= attr
->ecommunity
;
3027 if (!ecom
|| !ecom
->size
)
3030 /* For each extended community RT, see if it matches this VNI. If any RT
3031 * matches, we're done.
3033 for (i
= 0; i
< ecom
->size
; i
++) {
3035 uint8_t type
, sub_type
;
3036 struct ecommunity_val
*eval
;
3037 struct ecommunity_val eval_tmp
;
3038 struct irt_node
*irt
;
3040 /* Only deal with RTs */
3041 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3042 eval
= (struct ecommunity_val
*)(ecom
->val
3043 + (i
* ECOMMUNITY_SIZE
));
3046 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3049 /* See if this RT matches specified VNIs import RTs */
3050 irt
= lookup_import_rt(bgp
, eval
);
3052 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
3055 /* Also check for non-exact match. In this, we mask out the AS
3057 * only check on the local-admin sub-field. This is to
3059 * VNI as the RT for EBGP peering too.
3062 if (type
== ECOMMUNITY_ENCODE_AS
3063 || type
== ECOMMUNITY_ENCODE_AS4
3064 || type
== ECOMMUNITY_ENCODE_IP
) {
3065 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3066 mask_ecom_global_admin(&eval_tmp
, eval
);
3067 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3070 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
3077 static int install_uninstall_routes_for_es(struct bgp
*bgp
,
3084 char buf
[PREFIX_STRLEN
];
3085 char buf1
[ESI_STR_LEN
];
3086 struct bgp_node
*rd_rn
, *rn
;
3087 struct bgp_table
*table
;
3088 struct bgp_path_info
*pi
;
3094 * Walk entire global routing table and evaluate routes which could be
3095 * imported into this VRF. Note that we need to loop through all global
3096 * routes to determine which route matches the import rt on vrf
3098 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
3099 rd_rn
= bgp_route_next(rd_rn
)) {
3100 table
= bgp_node_get_bgp_table_info(rd_rn
);
3104 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3105 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3107 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3110 * Consider "valid" remote routes applicable for
3113 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3114 && pi
->type
== ZEBRA_ROUTE_BGP
3115 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3118 if (!is_prefix_matching_for_es(evp
, es
))
3122 ret
= install_evpn_route_entry_in_es(
3125 ret
= uninstall_evpn_route_entry_in_es(
3131 "Failed to %s EVPN %s route in ESI %s",
3134 prefix2str(evp
, buf
,
3136 esi_to_str(&es
->esi
, buf1
,
3146 /* This API will scan evpn routes for checking attribute's rmac
3147 * macthes with bgp instance router mac. It avoid installing
3148 * route into bgp vrf table and remote rmac in bridge table.
3150 static int bgp_evpn_route_rmac_self_check(struct bgp
*bgp_vrf
,
3151 struct prefix_evpn
*evp
,
3152 struct bgp_path_info
*pi
)
3154 /* evpn route could have learnt prior to L3vni has come up,
3155 * perform rmac check before installing route and
3156 * remote router mac.
3157 * The route will be removed from global bgp table once
3158 * SVI comes up with MAC and stored in hash, triggers
3159 * bgp_mac_rescan_all_evpn_tables.
3161 if (memcmp(&bgp_vrf
->rmac
, &pi
->attr
->rmac
, ETH_ALEN
) == 0) {
3162 if (bgp_debug_update(pi
->peer
, NULL
, NULL
, 1)) {
3163 char buf1
[PREFIX_STRLEN
];
3164 char attr_str
[BUFSIZ
] = {0};
3166 bgp_dump_attr(pi
->attr
, attr_str
, BUFSIZ
);
3168 zlog_debug("%s: bgp %u prefix %s with attr %s - DENIED due to self mac",
3169 __func__
, bgp_vrf
->vrf_id
,
3170 prefix2str(evp
, buf1
, sizeof(buf1
)),
3181 * Install or uninstall mac-ip routes are appropriate for this
3184 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
3188 struct bgp_node
*rd_rn
, *rn
;
3189 struct bgp_table
*table
;
3190 struct bgp_path_info
*pi
;
3192 char buf
[PREFIX_STRLEN
];
3193 struct bgp
*bgp_evpn
= NULL
;
3197 bgp_evpn
= bgp_get_evpn();
3201 /* Walk entire global routing table and evaluate routes which could be
3202 * imported into this VRF. Note that we need to loop through all global
3203 * routes to determine which route matches the import rt on vrf
3205 for (rd_rn
= bgp_table_top(bgp_evpn
->rib
[afi
][safi
]); rd_rn
;
3206 rd_rn
= bgp_route_next(rd_rn
)) {
3207 table
= bgp_node_get_bgp_table_info(rd_rn
);
3211 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3212 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3214 /* if not mac-ip route skip this route */
3215 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3216 || evp
->prefix
.route_type
3217 == BGP_EVPN_IP_PREFIX_ROUTE
))
3220 /* if not a mac+ip route skip this route */
3221 if (!(is_evpn_prefix_ipaddr_v4(evp
)
3222 || is_evpn_prefix_ipaddr_v6(evp
)))
3225 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3227 /* Consider "valid" remote routes applicable for
3230 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3231 && pi
->type
== ZEBRA_ROUTE_BGP
3232 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3235 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
3236 if (bgp_evpn_route_rmac_self_check(
3241 ret
= install_evpn_route_entry_in_vrf(
3244 ret
= uninstall_evpn_route_entry_in_vrf(
3250 "Failed to %s EVPN %s route in VRF %s",
3253 prefix2str(evp
, buf
,
3268 * Install or uninstall routes of specified type that are appropriate for this
3271 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
3272 struct bgpevpn
*vpn
,
3273 bgp_evpn_route_type rtype
,
3278 struct bgp_node
*rd_rn
, *rn
;
3279 struct bgp_table
*table
;
3280 struct bgp_path_info
*pi
;
3286 /* Walk entire global routing table and evaluate routes which could be
3287 * imported into this VPN. Note that we cannot just look at the routes
3289 * the VNI's RD - remote routes applicable for this VNI could have any
3292 /* EVPN routes are a 2-level table. */
3293 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
3294 rd_rn
= bgp_route_next(rd_rn
)) {
3295 table
= bgp_node_get_bgp_table_info(rd_rn
);
3299 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3300 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3302 if (evp
->prefix
.route_type
!= rtype
)
3305 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3307 /* Consider "valid" remote routes applicable for
3309 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3310 && pi
->type
== ZEBRA_ROUTE_BGP
3311 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3314 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3316 ret
= install_evpn_route_entry(
3319 ret
= uninstall_evpn_route_entry(
3325 "%u: Failed to %s EVPN %s route in VNI %u",
3329 rtype
== BGP_EVPN_MAC_IP_ROUTE
3343 /* Install any existing remote ES routes applicable for this ES into its routing
3344 * table. This is invoked when ES comes up.
3346 static int install_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
3348 return install_uninstall_routes_for_es(bgp
, es
, 1);
3352 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3353 * is invoked upon l3vni-add or l3vni import rt change
3355 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3357 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3362 * Install any existing remote routes applicable for this VNI into its
3363 * routing table. This is invoked when a VNI becomes "live" or its Import
3366 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3370 /* Install type-3 routes followed by type-2 routes - the ones applicable
3373 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3378 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3382 /* uninstall routes from l3vni vrf. */
3383 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3385 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3390 * Uninstall any existing remote routes for this VNI. One scenario in which
3391 * this is invoked is upon an import RT change.
3393 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3397 /* Uninstall type-2 routes followed by type-3 routes - the ones
3401 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3406 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3410 /* Install or unistall route in ES */
3411 static int install_uninstall_route_in_es(struct bgp
*bgp
, struct evpnes
*es
,
3412 afi_t afi
, safi_t safi
,
3413 struct prefix_evpn
*evp
,
3414 struct bgp_path_info
*pi
, int install
)
3417 char buf
[ESI_STR_LEN
];
3420 ret
= install_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3422 ret
= uninstall_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3427 "%u: Failed to %s EVPN %s route in ESI %s", bgp
->vrf_id
,
3428 install
? "install" : "uninstall", "ES",
3429 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)));
3436 * Install or uninstall route in matching VRFs (list).
3438 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3439 safi_t safi
, struct prefix_evpn
*evp
,
3440 struct bgp_path_info
*pi
,
3441 struct list
*vrfs
, int install
)
3443 char buf
[PREFIX2STR_BUFFER
];
3444 struct bgp
*bgp_vrf
;
3445 struct listnode
*node
, *nnode
;
3447 /* Only type-2/type-5 routes go into a VRF */
3448 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3449 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3452 /* if it is type-2 route and not a mac+ip route skip this route */
3453 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3454 && !(is_evpn_prefix_ipaddr_v4(evp
)
3455 || is_evpn_prefix_ipaddr_v6(evp
)))
3458 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3462 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3464 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3468 flog_err(EC_BGP_EVPN_FAIL
,
3469 "%u: Failed to %s prefix %s in VRF %s",
3471 install
? "install" : "uninstall",
3472 prefix2str(evp
, buf
, sizeof(buf
)),
3473 vrf_id_to_name(bgp_vrf
->vrf_id
));
3482 * Install or uninstall route in matching VNIs (list).
3484 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3485 safi_t safi
, struct prefix_evpn
*evp
,
3486 struct bgp_path_info
*pi
,
3487 struct list
*vnis
, int install
)
3489 struct bgpevpn
*vpn
;
3490 struct listnode
*node
, *nnode
;
3492 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3495 if (!is_vni_live(vpn
))
3499 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3501 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3504 flog_err(EC_BGP_EVPN_FAIL
,
3505 "%u: Failed to %s EVPN %s route in VNI %u",
3506 bgp
->vrf_id
, install
? "install" : "uninstall",
3507 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3519 * Install or uninstall route for appropriate VNIs/ESIs.
3521 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3523 struct bgp_path_info
*pi
, int import
)
3525 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3526 struct attr
*attr
= pi
->attr
;
3527 struct ecommunity
*ecom
;
3532 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3533 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3534 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3535 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3536 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3539 /* If we don't have Route Target, nothing much to do. */
3540 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3543 ecom
= attr
->ecommunity
;
3544 if (!ecom
|| !ecom
->size
)
3547 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3548 * attached to the route */
3549 for (i
= 0; i
< ecom
->size
; i
++) {
3551 uint8_t type
, sub_type
;
3552 struct ecommunity_val
*eval
;
3553 struct ecommunity_val eval_tmp
;
3554 struct irt_node
*irt
; /* import rt for l2vni */
3555 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3558 /* Only deal with RTs */
3559 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3560 eval
= (struct ecommunity_val
*)(ecom
->val
3561 + (i
* ECOMMUNITY_SIZE
));
3564 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3568 * macip routes (type-2) are imported into VNI and VRF tables.
3569 * IMET route is imported into VNI table.
3570 * prefix routes are imported into VRF table.
3572 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3573 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3574 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3576 irt
= lookup_import_rt(bgp
, eval
);
3578 install_uninstall_route_in_vnis(
3579 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3582 vrf_irt
= lookup_vrf_import_rt(eval
);
3584 install_uninstall_route_in_vrfs(
3585 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3588 /* Also check for non-exact match.
3589 * In this, we mask out the AS and
3590 * only check on the local-admin sub-field.
3591 * This is to facilitate using
3592 * VNI as the RT for EBGP peering too.
3596 if (type
== ECOMMUNITY_ENCODE_AS
3597 || type
== ECOMMUNITY_ENCODE_AS4
3598 || type
== ECOMMUNITY_ENCODE_IP
) {
3599 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3600 mask_ecom_global_admin(&eval_tmp
, eval
);
3601 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3602 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3606 install_uninstall_route_in_vnis(
3607 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3610 install_uninstall_route_in_vrfs(
3611 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3615 /* es route is imported into the es table */
3616 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3618 /* we will match based on the entire esi to avoid
3619 * imoort of an es route for esi2 into esi1
3621 es
= bgp_evpn_lookup_es(bgp
, &evp
->prefix
.es_addr
.esi
);
3622 if (es
&& is_es_local(es
))
3623 install_uninstall_route_in_es(
3624 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3632 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3635 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3637 /* Delete ipv4 default route and withdraw from peers */
3638 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3639 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3640 SAFI_UNICAST
, false);
3642 /* delete all ipv4 routes and withdraw from peers */
3643 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3644 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3646 /* Delete ipv6 default route and withdraw from peers */
3647 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3648 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3649 SAFI_UNICAST
, false);
3651 /* delete all ipv6 routes and withdraw from peers */
3652 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3653 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3657 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3660 void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3662 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
3664 bgp_evpn
= bgp_get_evpn();
3668 /* update all ipv4 routes */
3669 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3670 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3672 /* update ipv4 default route and withdraw from peers */
3673 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3674 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3675 SAFI_UNICAST
, true);
3677 /* update all ipv6 routes */
3678 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3679 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3681 /* update ipv6 default route and withdraw from peers */
3682 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3683 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3684 SAFI_UNICAST
, true);
3689 * update and advertise local routes for a VRF as type-5 routes.
3690 * This is invoked upon RD change for a VRF. Note taht the processing is only
3691 * done in the global route table using the routes which already exist in the
3694 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3696 /* skip if the RD is configured */
3697 if (is_vrf_rd_configured(bgp_vrf
))
3700 /* derive the RD for the VRF based on new router-id */
3701 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3703 /* update advertise ipv4|ipv6 routes as type-5 routes */
3704 update_advertise_vrf_routes(bgp_vrf
);
3708 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3709 * This is invoked upon VRF RD change. The processing is done only from global
3712 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3714 /* skip if the RD is configured */
3715 if (is_vrf_rd_configured(bgp_vrf
))
3718 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3719 delete_withdraw_vrf_routes(bgp_vrf
);
3723 * Update and advertise local routes for a VNI. Invoked upon router-id
3724 * change. Note that the processing is done only on the global route table
3725 * using routes that already exist in the per-VNI table.
3727 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3729 struct prefix_evpn p
;
3730 struct bgp_node
*rn
, *global_rn
;
3731 struct bgp_path_info
*pi
, *global_pi
;
3733 afi_t afi
= AFI_L2VPN
;
3734 safi_t safi
= SAFI_EVPN
;
3736 /* Locate type-3 route for VNI in the per-VNI table and use its
3737 * attributes to create and advertise the type-3 route for this VNI
3738 * in the global table.
3740 * RT-3 only if doing head-end replication
3742 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3743 == VXLAN_FLOOD_HEAD_END_REPL
) {
3744 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3745 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3746 if (!rn
) /* unexpected */
3748 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3749 if (pi
->peer
== bgp
->peer_self
&&
3750 pi
->type
== ZEBRA_ROUTE_BGP
3751 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3753 if (!pi
) /* unexpected */
3757 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3758 (struct prefix
*)&p
, &vpn
->prd
);
3759 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
,
3760 1, &pi
, 0, mac_mobility_seqnum(attr
));
3762 /* Schedule for processing and unlock node. */
3763 bgp_process(bgp
, global_rn
, afi
, safi
);
3764 bgp_unlock_node(global_rn
);
3767 /* Now, walk this VNI's route table and use the route and its attribute
3768 * to create and schedule route in global table.
3770 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
3771 rn
= bgp_route_next(rn
)) {
3772 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3774 /* Identify MAC-IP local routes. */
3775 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3778 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3779 if (pi
->peer
== bgp
->peer_self
3780 && pi
->type
== ZEBRA_ROUTE_BGP
3781 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3786 /* Create route in global routing table using this route entry's
3790 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3791 (struct prefix
*)evp
, &vpn
->prd
);
3793 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
3795 mac_mobility_seqnum(attr
));
3797 /* Schedule for processing and unlock node. */
3798 bgp_process(bgp
, global_rn
, afi
, safi
);
3799 bgp_unlock_node(global_rn
);
3806 * Delete (and withdraw) local routes for a VNI - only from the global
3807 * table. Invoked upon router-id change.
3809 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3812 struct prefix_evpn p
;
3813 struct bgp_node
*global_rn
;
3814 struct bgp_path_info
*pi
;
3815 afi_t afi
= AFI_L2VPN
;
3816 safi_t safi
= SAFI_EVPN
;
3818 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3819 * for this VNI - from the global table.
3821 ret
= delete_global_type2_routes(bgp
, vpn
);
3825 /* Remove type-3 route for this VNI from global table. */
3826 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3827 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3828 (struct prefix
*)&p
, &vpn
->prd
);
3830 /* Delete route entry in the global EVPN table. */
3831 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
3833 /* Schedule for processing - withdraws to peers happen from
3837 bgp_process(bgp
, global_rn
, afi
, safi
);
3838 bgp_unlock_node(global_rn
);
3845 * Handle router-id change. Update and advertise local routes corresponding
3846 * to this VNI from peers. Note that this is invoked after updating the
3847 * router-id. The routes in the per-VNI table are used to create routes in
3848 * the global table and schedule them.
3850 static void update_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3852 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3854 /* Skip VNIs with configured RD. */
3855 if (is_rd_configured(vpn
))
3858 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3859 update_advertise_vni_routes(bgp
, vpn
);
3863 * Handle router-id change. Delete and withdraw local routes corresponding
3864 * to this VNI from peers. Note that this is invoked prior to updating
3865 * the router-id and is done only on the global route table, the routes
3866 * are needed in the per-VNI table to re-advertise with new router id.
3868 static void withdraw_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3870 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3872 /* Skip VNIs with configured RD. */
3873 if (is_rd_configured(vpn
))
3876 delete_withdraw_vni_routes(bgp
, vpn
);
3880 * Create RT-3 for a VNI and schedule for processing and advertisement.
3881 * This is invoked upon flooding mode changing to head-end replication.
3883 static void create_advertise_type3(struct hash_bucket
*bucket
, void *data
)
3885 struct bgpevpn
*vpn
= bucket
->data
;
3886 struct bgp
*bgp
= data
;
3887 struct prefix_evpn p
;
3889 if (!vpn
|| !is_vni_live(vpn
) ||
3890 bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3891 != VXLAN_FLOOD_HEAD_END_REPL
)
3894 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3895 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0))
3896 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3897 "Type3 route creation failure for VNI %u", vpn
->vni
);
3901 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3902 * This is invoked upon flooding mode changing to drop BUM packets.
3904 static void delete_withdraw_type3(struct hash_bucket
*bucket
, void *data
)
3906 struct bgpevpn
*vpn
= bucket
->data
;
3907 struct bgp
*bgp
= data
;
3908 struct prefix_evpn p
;
3910 if (!vpn
|| !is_vni_live(vpn
))
3913 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3914 delete_evpn_route(bgp
, vpn
, &p
);
3918 * Process received EVPN type-2 route (advertise or withdraw).
3920 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3921 struct attr
*attr
, uint8_t *pfx
, int psize
,
3922 uint32_t addpath_id
)
3924 struct prefix_rd prd
;
3925 struct prefix_evpn p
;
3926 struct bgp_route_evpn evpn
;
3928 uint8_t macaddr_len
;
3929 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
3930 uint32_t num_labels
= 0;
3934 /* Type-2 route should be either 33, 37 or 49 bytes or an
3935 * additional 3 bytes if there is a second label (VNI):
3936 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3937 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3938 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3940 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3941 && psize
!= 40 && psize
!= 52) {
3942 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3943 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3944 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3948 memset(&evpn
, 0, sizeof(evpn
));
3950 /* Make prefix_rd */
3951 prd
.family
= AF_UNSPEC
;
3953 memcpy(&prd
.val
, pfx
, 8);
3956 /* Make EVPN prefix. */
3957 memset(&p
, 0, sizeof(struct prefix_evpn
));
3959 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3960 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3962 /* Copy Ethernet Seg Identifier */
3963 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
3966 /* Copy Ethernet Tag */
3967 memcpy(ð_tag
, pfx
, 4);
3968 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3971 /* Get the MAC Addr len */
3972 macaddr_len
= *pfx
++;
3974 /* Get the MAC Addr */
3975 if (macaddr_len
== (ETH_ALEN
* 8)) {
3976 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
3980 EC_BGP_EVPN_ROUTE_INVALID
,
3981 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3982 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3988 ipaddr_len
= *pfx
++;
3989 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3990 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3992 EC_BGP_EVPN_ROUTE_INVALID
,
3993 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3994 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3999 ipaddr_len
/= 8; /* Convert to bytes. */
4000 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
4003 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
4007 /* Get the VNI(s). Stored as bytes here. */
4009 memset(label
, 0, sizeof(label
));
4010 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
4011 pfx
+= BGP_LABEL_BYTES
;
4012 psize
-= (33 + ipaddr_len
);
4013 /* Do we have a second VNI? */
4016 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
4018 * If in future, we are required to access additional fields,
4019 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
4024 /* Process the route. */
4026 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4027 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4028 &prd
, &label
[0], num_labels
, 0, &evpn
);
4030 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4031 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4032 &prd
, &label
[0], num_labels
, &evpn
);
4037 * Process received EVPN type-3 route (advertise or withdraw).
4039 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4040 struct attr
*attr
, uint8_t *pfx
, int psize
,
4041 uint32_t addpath_id
)
4043 struct prefix_rd prd
;
4044 struct prefix_evpn p
;
4049 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
4050 * IP len (1) and IP (4 or 16).
4052 if (psize
!= 17 && psize
!= 29) {
4053 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4054 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
4055 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4059 /* If PMSI is present, log if it is anything other than IR.
4060 * Note: We just simply ignore the values as it is not clear if
4061 * doing anything else is better.
4064 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
4065 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
&&
4066 attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_PIM_SM
) {
4067 flog_warn(EC_BGP_EVPN_PMSI_PRESENT
,
4068 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
4069 peer
->bgp
->vrf_id
, peer
->host
,
4070 attr
->pmsi_tnl_type
);
4074 /* Make prefix_rd */
4075 prd
.family
= AF_UNSPEC
;
4077 memcpy(&prd
.val
, pfx
, 8);
4080 /* Make EVPN prefix. */
4081 memset(&p
, 0, sizeof(struct prefix_evpn
));
4083 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4084 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
4086 /* Copy Ethernet Tag */
4087 memcpy(ð_tag
, pfx
, 4);
4088 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
4092 ipaddr_len
= *pfx
++;
4093 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
4094 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
4095 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
4098 EC_BGP_EVPN_ROUTE_INVALID
,
4099 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
4100 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4104 /* Process the route. */
4106 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4107 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4108 &prd
, NULL
, 0, 0, NULL
);
4110 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4111 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4112 &prd
, NULL
, 0, NULL
);
4117 * Process received EVPN type-4 route (advertise or withdraw).
4119 static int process_type4_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4120 struct attr
*attr
, uint8_t *pfx
, int psize
,
4121 uint32_t addpath_id
)
4126 struct in_addr vtep_ip
;
4127 struct prefix_rd prd
;
4128 struct prefix_evpn p
;
4130 /* Type-4 route should be either 23 or 35 bytes
4131 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
4133 if (psize
!= 23 && psize
!= 35) {
4134 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4135 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
4136 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4140 /* Make prefix_rd */
4141 prd
.family
= AF_UNSPEC
;
4143 memcpy(&prd
.val
, pfx
, 8);
4147 memcpy(&esi
, pfx
, ESI_BYTES
);
4152 ipaddr_len
= *pfx
++;
4153 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
4154 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
4157 EC_BGP_EVPN_ROUTE_INVALID
,
4158 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
4159 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4163 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
4164 /* Process the route. */
4166 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4167 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4168 &prd
, NULL
, 0, 0, NULL
);
4170 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4171 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4172 &prd
, NULL
, 0, NULL
);
4179 * Process received EVPN type-5 route (advertise or withdraw).
4181 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4182 struct attr
*attr
, uint8_t *pfx
, int psize
,
4183 uint32_t addpath_id
)
4185 struct prefix_rd prd
;
4186 struct prefix_evpn p
;
4187 struct bgp_route_evpn evpn
;
4190 mpls_label_t label
; /* holds the VNI as in the packet */
4193 /* Type-5 route should be 34 or 58 bytes:
4194 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
4195 * GW (4 or 16) and VNI (3).
4196 * Note that the IP and GW should both be IPv4 or both IPv6.
4198 if (psize
!= 34 && psize
!= 58) {
4199 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4200 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
4201 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4205 /* Make prefix_rd */
4206 prd
.family
= AF_UNSPEC
;
4208 memcpy(&prd
.val
, pfx
, 8);
4211 /* Make EVPN prefix. */
4212 memset(&p
, 0, sizeof(struct prefix_evpn
));
4214 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4215 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
4217 /* Additional information outside of prefix - ESI and GW IP */
4218 memset(&evpn
, 0, sizeof(evpn
));
4221 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
4224 /* Fetch Ethernet Tag. */
4225 memcpy(ð_tag
, pfx
, 4);
4226 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
4229 /* Fetch IP prefix length. */
4231 if (ippfx_len
> IPV6_MAX_BITLEN
) {
4233 EC_BGP_EVPN_ROUTE_INVALID
,
4234 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4235 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
4238 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
4240 /* Determine IPv4 or IPv6 prefix */
4241 /* Since the address and GW are from the same family, this just becomes
4242 * a simple check on the total size.
4245 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
4246 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
4248 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
4251 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
4252 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
4254 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
4258 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4259 memset(&label
, 0, sizeof(label
));
4260 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
4263 * If in future, we are required to access additional fields,
4264 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4268 /* Process the route. */
4270 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4271 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4272 &prd
, &label
, 1, 0, &evpn
);
4274 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4275 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4276 &prd
, &label
, 1, &evpn
);
4281 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
4282 struct prefix_rd
*prd
, mpls_label_t
*label
,
4283 uint32_t num_labels
, struct attr
*attr
)
4287 struct evpn_addr
*p_evpn_p
;
4289 memset(&temp
, 0, 16);
4290 if (p
->family
!= AF_EVPN
)
4292 p_evpn_p
= &(p
->u
.prefix_evpn
);
4294 /* len denites the total len of IP and GW-IP in the route
4295 IP and GW-IP have to be both ipv4 or ipv6
4297 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4298 len
= 8; /* IP and GWIP are both ipv4 */
4300 len
= 32; /* IP and GWIP are both ipv6 */
4301 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4302 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4303 stream_put(s
, prd
->val
, 8);
4305 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
4307 stream_put(s
, &temp
, 10);
4308 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4309 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4310 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4311 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4313 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4315 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4317 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
4319 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
4321 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4322 stream_put_ipv4(s
, 0);
4324 stream_put(s
, &temp
, 16);
4328 stream_put(s
, label
, 3);
4334 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4336 static void cleanup_vni_on_disable(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4338 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4340 /* Remove EVPN routes and schedule for processing. */
4341 delete_routes_for_vni(bgp
, vpn
);
4343 /* Clear "live" flag and see if hash needs to be freed. */
4344 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4345 if (!is_vni_configured(vpn
))
4346 bgp_evpn_free(bgp
, vpn
);
4350 * Free a VNI entry; iterator function called during cleanup.
4352 static void free_vni_entry(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4354 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4356 delete_all_vni_routes(bgp
, vpn
);
4357 bgp_evpn_free(bgp
, vpn
);
4361 * Derive AUTO import RT for BGP VRF - L3VNI
4363 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4365 struct bgp
*bgp_evpn
= NULL
;
4367 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4368 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4371 bgp_evpn
= bgp_get_evpn();
4374 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4378 * Delete AUTO import RT from BGP VRF - L3VNI
4380 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4382 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4386 * Derive AUTO export RT for BGP VRF - L3VNI
4388 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4390 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4391 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4395 * Delete AUTO export RT from BGP VRF - L3VNI
4397 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4399 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4402 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4404 struct bgp
*bgp_evpn
= NULL
;
4405 struct listnode
*node
= NULL
;
4406 struct bgpevpn
*vpn
= NULL
;
4408 bgp_evpn
= bgp_get_evpn();
4412 /* update all type-5 routes */
4413 update_advertise_vrf_routes(bgp_vrf
);
4415 /* update all type-2 routes */
4416 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4417 update_routes_for_vni(bgp_evpn
, vpn
);
4421 * Handle autort change for a given VNI.
4423 static void update_autort_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4425 struct bgpevpn
*vpn
= bucket
->data
;
4427 if (!is_import_rt_configured(vpn
)) {
4428 if (is_vni_live(vpn
))
4429 bgp_evpn_uninstall_routes(bgp
, vpn
);
4430 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4431 list_delete_all_node(vpn
->import_rtl
);
4432 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4433 if (is_vni_live(vpn
))
4434 bgp_evpn_install_routes(bgp
, vpn
);
4436 if (!is_export_rt_configured(vpn
)) {
4437 list_delete_all_node(vpn
->export_rtl
);
4438 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4439 if (is_vni_live(vpn
))
4440 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4448 /* withdraw type-5 route corresponding to ip prefix */
4449 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4450 afi_t afi
, safi_t safi
)
4453 struct prefix_evpn evp
;
4454 char buf
[PREFIX_STRLEN
];
4456 build_type5_prefix_from_ip_prefix(&evp
, p
);
4457 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4460 EC_BGP_EVPN_ROUTE_DELETE
,
4461 "%u failed to delete type-5 route for prefix %s in vrf %s",
4462 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4463 vrf_id_to_name(bgp_vrf
->vrf_id
));
4467 /* withdraw all type-5 routes for an address family */
4468 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4470 struct bgp_table
*table
= NULL
;
4471 struct bgp_node
*rn
= NULL
;
4472 struct bgp_path_info
*pi
;
4474 table
= bgp_vrf
->rib
[afi
][safi
];
4475 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4476 /* Only care about "selected" routes. Also ensure that
4477 * these are routes that are injectable into EVPN.
4479 /* TODO: Support for AddPath for EVPN. */
4480 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4481 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4482 && is_route_injectable_into_evpn(pi
)) {
4483 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
4492 * evpn - enable advertisement of default g/w
4494 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
4495 safi_t safi
, bool add
)
4497 struct prefix ip_prefix
;
4499 /* form the default prefix 0.0.0.0/0 */
4500 memset(&ip_prefix
, 0, sizeof(struct prefix
));
4501 ip_prefix
.family
= afi2family(afi
);
4504 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
4507 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
4514 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4515 * to this function correspond to those of the source IP prefix (best
4516 * path in the case of the attr. In the case of a local prefix (when we
4517 * are advertising local subnets), the src_attr will be NULL.
4519 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4520 struct attr
*src_attr
, afi_t afi
,
4524 struct prefix_evpn evp
;
4525 char buf
[PREFIX_STRLEN
];
4527 build_type5_prefix_from_ip_prefix(&evp
, p
);
4528 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4530 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4531 "%u: Failed to create type-5 route for prefix %s",
4532 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4535 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4536 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4537 * advertisement is enabled.
4539 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4542 struct bgp_table
*table
= NULL
;
4543 struct bgp_node
*rn
= NULL
;
4544 struct bgp_path_info
*pi
;
4546 table
= bgp_vrf
->rib
[afi
][safi
];
4547 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4548 /* Need to identify the "selected" route entry to use its
4549 * attribute. Also, ensure that the route is injectable
4551 * TODO: Support for AddPath for EVPN.
4553 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4554 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4555 && is_route_injectable_into_evpn(pi
)) {
4557 /* apply the route-map */
4558 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4559 route_map_result_t ret
;
4561 ret
= route_map_apply(
4562 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4564 &rn
->p
, RMAP_BGP
, pi
);
4565 if (ret
== RMAP_DENYMATCH
)
4568 bgp_evpn_advertise_type5_route(
4569 bgp_vrf
, &rn
->p
, pi
->attr
, afi
, safi
);
4576 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4578 struct listnode
*node
, *nnode
, *node_to_del
;
4579 struct ecommunity
*ecom
, *ecom_auto
;
4580 struct ecommunity_val eval
;
4582 if (bgp
->advertise_autort_rfc8365
)
4583 vni
|= EVPN_AUTORT_VXLAN
;
4584 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4586 ecom_auto
= ecommunity_new();
4587 ecommunity_add_val(ecom_auto
, &eval
);
4590 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4591 if (ecommunity_match(ecom
, ecom_auto
)) {
4592 ecommunity_free(&ecom
);
4598 list_delete_node(rtl
, node_to_del
);
4600 ecommunity_free(&ecom_auto
);
4603 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4604 struct ecommunity
*ecomadd
)
4606 /* uninstall routes from vrf */
4607 if (is_l3vni_live(bgp_vrf
))
4608 uninstall_routes_for_vrf(bgp_vrf
);
4610 /* Cleanup the RT to VRF mapping */
4611 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4613 /* Remove auto generated RT */
4614 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4616 /* Add the newly configured RT to RT list */
4617 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4618 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4620 /* map VRF to its RTs and install routes matching the new RTs */
4621 if (is_l3vni_live(bgp_vrf
)) {
4622 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4623 install_routes_for_vrf(bgp_vrf
);
4627 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4628 struct ecommunity
*ecomdel
)
4630 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4631 struct ecommunity
*ecom
= NULL
;
4633 /* uninstall routes from vrf */
4634 if (is_l3vni_live(bgp_vrf
))
4635 uninstall_routes_for_vrf(bgp_vrf
);
4637 /* Cleanup the RT to VRF mapping */
4638 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4640 /* remove the RT from the RT list */
4641 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4642 if (ecommunity_match(ecom
, ecomdel
)) {
4643 ecommunity_free(&ecom
);
4650 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4652 assert(bgp_vrf
->vrf_import_rtl
);
4653 /* fallback to auto import rt, if this was the last RT */
4654 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4655 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4656 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4659 /* map VRFs to its RTs and install routes matching this new RT */
4660 if (is_l3vni_live(bgp_vrf
)) {
4661 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4662 install_routes_for_vrf(bgp_vrf
);
4666 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4667 struct ecommunity
*ecomadd
)
4669 /* remove auto-generated RT */
4670 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4672 /* Add the new RT to the RT list */
4673 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4674 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4676 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4679 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4680 struct ecommunity
*ecomdel
)
4682 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4683 struct ecommunity
*ecom
= NULL
;
4685 /* Remove the RT from the RT list */
4686 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4687 if (ecommunity_match(ecom
, ecomdel
)) {
4688 ecommunity_free(&ecom
);
4695 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4698 * Temporary assert to make SA happy.
4699 * The ALL_LIST_ELEMENTS macro above has a NULL check
4700 * which means that SA is going to complain about
4701 * the list_isempty call, which doesn't NULL check.
4702 * So until we get this situation cleaned up, here
4705 assert(bgp_vrf
->vrf_export_rtl
);
4707 /* fall back to auto-generated RT if this was the last RT */
4708 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4709 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4710 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4713 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4717 * Handle change to BGP router id. This is invoked twice by the change
4718 * handler, first before the router id has been changed and then after
4719 * the router id has been changed. The first invocation will result in
4720 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4721 * will result in the routes being re-advertised.
4723 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4725 struct listnode
*node
;
4726 struct bgp
*bgp_vrf
;
4730 /* delete and withdraw all the type-5 routes
4731 stored in the global table for this vrf
4733 withdraw_router_id_vrf(bgp
);
4735 /* delete all the VNI routes (type-2/type-3) routes for all the
4738 hash_iterate(bgp
->vnihash
,
4739 (void (*)(struct hash_bucket
*,
4740 void *))withdraw_router_id_vni
,
4743 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4744 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4745 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4746 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4748 bgp_vrf
->evpn_info
->pip_ip
.s_addr
4754 /* Assign new default instance router-id */
4755 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4756 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4757 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4758 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4760 bgp_vrf
->evpn_info
->pip_ip
=
4762 /* advertise type-5 routes with
4765 update_advertise_vrf_routes(bgp_vrf
);
4770 /* advertise all routes in the vrf as type-5 routes with the new
4773 update_router_id_vrf(bgp
);
4775 /* advertise all the VNI routes (type-2/type-3) routes with the
4778 hash_iterate(bgp
->vnihash
,
4779 (void (*)(struct hash_bucket
*,
4780 void *))update_router_id_vni
,
4786 * Handle change to auto-RT algorithm - update and advertise local routes.
4788 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4790 hash_iterate(bgp
->vnihash
,
4791 (void (*)(struct hash_bucket
*,
4792 void*))update_autort_vni
,
4797 * Handle change to export RT - update and advertise local routes.
4799 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4801 return update_routes_for_vni(bgp
, vpn
);
4804 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4807 delete_withdraw_vrf_routes(bgp_vrf
);
4809 update_advertise_vrf_routes(bgp_vrf
);
4813 * Handle change to RD. This is invoked twice by the change handler,
4814 * first before the RD has been changed and then after the RD has
4815 * been changed. The first invocation will result in local routes
4816 * of this VNI being deleted and withdrawn and the next will result
4817 * in the routes being re-advertised.
4819 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4823 delete_withdraw_vni_routes(bgp
, vpn
);
4825 update_advertise_vni_routes(bgp
, vpn
);
4829 * Install routes for this VNI. Invoked upon change to Import RT.
4831 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4833 return install_routes_for_vni(bgp
, vpn
);
4837 * Uninstall all routes installed for this VNI. Invoked upon change
4840 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4842 return uninstall_routes_for_vni(bgp
, vpn
);
4846 * TODO: Hardcoded for a maximum of 2 VNIs right now
4848 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4853 vni1
= label2vni(label
);
4854 if (num_labels
== 2) {
4855 vni2
= label2vni(label
+ 1);
4856 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4858 snprintf(buf
, len
, "%u", vni1
);
4863 * Function to convert evpn route to json format.
4864 * NOTE: We don't use prefix2str as the output here is a bit different.
4866 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
4868 char buf1
[ETHER_ADDR_STRLEN
];
4869 char buf2
[PREFIX2STR_BUFFER
];
4876 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4878 switch (p
->prefix
.route_type
) {
4879 case BGP_EVPN_MAC_IP_ROUTE
:
4880 json_object_int_add(json
, "ethTag",
4881 p
->prefix
.macip_addr
.eth_tag
);
4882 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4883 json_object_string_add(json
, "mac",
4884 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4887 if (!is_evpn_prefix_ipaddr_none(p
)) {
4888 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
:
4890 prefixlen
= (family
== AF_INET
) ?
4891 IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
;
4892 inet_ntop(family
, &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4893 buf2
, PREFIX2STR_BUFFER
);
4894 json_object_int_add(json
, "ipLen", prefixlen
);
4895 json_object_string_add(json
, "ip", buf2
);
4899 case BGP_EVPN_IMET_ROUTE
:
4900 json_object_int_add(json
, "ethTag",
4901 p
->prefix
.imet_addr
.eth_tag
);
4902 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4903 prefixlen
= (family
== AF_INET
) ? IPV4_MAX_BITLEN
:
4905 inet_ntop(family
, &p
->prefix
.imet_addr
.ip
.ip
.addr
, buf2
,
4907 json_object_int_add(json
, "ipLen", prefixlen
);
4908 json_object_string_add(json
, "ip", buf2
);
4911 case BGP_EVPN_IP_PREFIX_ROUTE
:
4912 json_object_int_add(json
, "ethTag",
4913 p
->prefix
.prefix_addr
.eth_tag
);
4914 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4915 inet_ntop(family
, &p
->prefix
.prefix_addr
.ip
.ip
.addr
,
4916 buf2
, sizeof(buf2
));
4917 json_object_int_add(json
, "ipLen",
4918 p
->prefix
.prefix_addr
.ip_prefix_length
);
4919 json_object_string_add(json
, "ip", buf2
);
4928 * Function to convert evpn route to string.
4929 * NOTE: We don't use prefix2str as the output here is a bit different.
4931 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
4933 char buf1
[ETHER_ADDR_STRLEN
];
4934 char buf2
[PREFIX2STR_BUFFER
];
4935 char buf3
[ESI_STR_LEN
];
4937 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4938 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
4939 p
->prefix
.imet_addr
.eth_tag
,
4940 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4942 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4943 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4944 if (is_evpn_prefix_ipaddr_none(p
))
4945 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4946 p
->prefix
.route_type
,
4947 p
->prefix
.macip_addr
.eth_tag
,
4949 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4954 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4956 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
4957 p
->prefix
.route_type
,
4958 p
->prefix
.macip_addr
.eth_tag
,
4960 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4962 family
== AF_INET
? IPV4_MAX_BITLEN
4965 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4967 PREFIX2STR_BUFFER
));
4969 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
4970 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4971 p
->prefix
.route_type
,
4972 p
->prefix
.prefix_addr
.eth_tag
,
4973 p
->prefix
.prefix_addr
.ip_prefix_length
,
4974 is_evpn_prefix_ipaddr_v4(p
)
4975 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
4976 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
4977 } else if (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
4978 snprintf(buf
, len
, "[%d]:[%s]:[%d]:[%s]",
4979 p
->prefix
.route_type
,
4980 esi_to_str(&p
->prefix
.es_addr
.esi
, buf3
, sizeof(buf3
)),
4981 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4983 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
));
4985 /* For EVPN route types not supported yet. */
4986 snprintf(buf
, len
, "(unsupported route type %d)",
4987 p
->prefix
.route_type
);
4994 * Encode EVPN prefix in Update (MP_REACH)
4996 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
4997 struct prefix_rd
*prd
, mpls_label_t
*label
,
4998 uint32_t num_labels
, struct attr
*attr
,
4999 int addpath_encode
, uint32_t addpath_tx_id
)
5001 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
5002 int len
, ipa_len
= 0;
5005 stream_putl(s
, addpath_tx_id
);
5008 stream_putc(s
, evp
->prefix
.route_type
);
5010 switch (evp
->prefix
.route_type
) {
5011 case BGP_EVPN_MAC_IP_ROUTE
:
5012 if (is_evpn_prefix_ipaddr_v4(evp
))
5013 ipa_len
= IPV4_MAX_BYTELEN
;
5014 else if (is_evpn_prefix_ipaddr_v6(evp
))
5015 ipa_len
= IPV6_MAX_BYTELEN
;
5016 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
5017 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
5018 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
5020 stream_putc(s
, len
);
5021 stream_put(s
, prd
->val
, 8); /* RD */
5023 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
5025 stream_put(s
, 0, 10);
5026 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
5027 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
5028 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
5029 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
5030 if (ipa_len
) /* IP */
5031 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
5033 /* 1st label is the L2 VNI */
5034 stream_put(s
, label
, BGP_LABEL_BYTES
);
5035 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
5036 if (ipa_len
&& num_labels
> 1)
5037 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
5040 case BGP_EVPN_IMET_ROUTE
:
5041 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
5042 stream_put(s
, prd
->val
, 8); /* RD */
5043 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
5044 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
5045 /* Originating Router's IP Addr */
5046 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
5049 case BGP_EVPN_ES_ROUTE
:
5050 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
5051 stream_put(s
, prd
->val
, 8); /* RD */
5052 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
5053 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
5055 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
5058 case BGP_EVPN_IP_PREFIX_ROUTE
:
5059 /* TODO: AddPath support. */
5060 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
5068 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
5069 struct bgp_nlri
*packet
, int withdraw
)
5075 uint32_t addpath_id
;
5076 int addpath_encoded
;
5081 /* Start processing the NLRI - there may be multiple in the MP_REACH */
5083 lim
= pnt
+ packet
->length
;
5085 safi
= packet
->safi
;
5089 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
5090 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5091 PEER_CAP_ADDPATH_AF_TX_RCV
));
5093 for (; pnt
< lim
; pnt
+= psize
) {
5094 /* Clear prefix structure. */
5095 memset(&p
, 0, sizeof(struct prefix
));
5097 /* Deal with path-id if AddPath is supported. */
5098 if (addpath_encoded
) {
5099 /* When packet overflow occurs return immediately. */
5100 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
5101 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5103 addpath_id
= ntohl(*((uint32_t *)pnt
));
5104 pnt
+= BGP_ADDPATH_ID_LEN
;
5107 /* All EVPN NLRI types start with type and length. */
5109 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE
;
5114 /* When packet overflow occur return immediately. */
5115 if (pnt
+ psize
> lim
)
5116 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5119 case BGP_EVPN_MAC_IP_ROUTE
:
5120 if (process_type2_route(peer
, afi
, safi
,
5121 withdraw
? NULL
: attr
, pnt
,
5122 psize
, addpath_id
)) {
5125 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
5126 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5127 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE
;
5131 case BGP_EVPN_IMET_ROUTE
:
5132 if (process_type3_route(peer
, afi
, safi
,
5133 withdraw
? NULL
: attr
, pnt
,
5134 psize
, addpath_id
)) {
5137 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
5138 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5139 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE
;
5143 case BGP_EVPN_ES_ROUTE
:
5144 if (process_type4_route(peer
, afi
, safi
,
5145 withdraw
? NULL
: attr
, pnt
,
5146 psize
, addpath_id
)) {
5149 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
5150 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5151 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE
;
5155 case BGP_EVPN_IP_PREFIX_ROUTE
:
5156 if (process_type5_route(peer
, afi
, safi
,
5157 withdraw
? NULL
: attr
, pnt
,
5158 psize
, addpath_id
)) {
5161 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
5162 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5163 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE
;
5172 /* Packet length consistency check. */
5174 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH
;
5176 return BGP_NLRI_PARSE_OK
;
5180 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
5181 * The mapping will be used during route processing.
5182 * bgp_def: default bgp instance
5183 * bgp_vrf: specific bgp vrf instance on which RT is configured
5185 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
5188 struct ecommunity_val
*eval
= NULL
;
5189 struct listnode
*node
= NULL
, *nnode
= NULL
;
5190 struct ecommunity
*ecom
= NULL
;
5192 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5193 for (i
= 0; i
< ecom
->size
; i
++) {
5194 eval
= (struct ecommunity_val
*)(ecom
->val
5196 * ECOMMUNITY_SIZE
));
5197 map_vrf_to_rt(bgp_vrf
, eval
);
5203 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
5205 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
5208 struct ecommunity_val
*eval
;
5209 struct listnode
*node
, *nnode
;
5210 struct ecommunity
*ecom
;
5212 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5213 for (i
= 0; i
< ecom
->size
; i
++) {
5214 struct vrf_irt_node
*irt
;
5215 struct ecommunity_val eval_tmp
;
5217 eval
= (struct ecommunity_val
*)(ecom
->val
5219 * ECOMMUNITY_SIZE
));
5220 /* If using "automatic" RT, we only care about the
5221 * local-admin sub-field.
5222 * This is to facilitate using VNI as the RT for EBGP
5225 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5226 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
5227 BGP_VRF_IMPORT_RT_CFGD
))
5228 mask_ecom_global_admin(&eval_tmp
, eval
);
5230 irt
= lookup_vrf_import_rt(&eval_tmp
);
5232 unmap_vrf_from_rt(bgp_vrf
, irt
);
5239 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
5240 * The mapping will be used during route processing.
5242 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5245 struct ecommunity_val
*eval
;
5246 struct listnode
*node
, *nnode
;
5247 struct ecommunity
*ecom
;
5249 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5250 for (i
= 0; i
< ecom
->size
; i
++) {
5251 eval
= (struct ecommunity_val
*)(ecom
->val
5253 * ECOMMUNITY_SIZE
));
5254 map_vni_to_rt(bgp
, vpn
, eval
);
5260 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
5262 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5265 struct ecommunity_val
*eval
;
5266 struct listnode
*node
, *nnode
;
5267 struct ecommunity
*ecom
;
5269 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5270 for (i
= 0; i
< ecom
->size
; i
++) {
5271 struct irt_node
*irt
;
5272 struct ecommunity_val eval_tmp
;
5274 eval
= (struct ecommunity_val
*)(ecom
->val
5276 * ECOMMUNITY_SIZE
));
5277 /* If using "automatic" RT, we only care about the
5278 * local-admin sub-field.
5279 * This is to facilitate using VNI as the RT for EBGP
5282 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5283 if (!is_import_rt_configured(vpn
))
5284 mask_ecom_global_admin(&eval_tmp
, eval
);
5286 irt
= lookup_import_rt(bgp
, &eval_tmp
);
5288 unmap_vni_from_rt(bgp
, vpn
, irt
);
5294 * Derive Import RT automatically for VNI and map VNI to RT.
5295 * The mapping will be used during route processing.
5297 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5299 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
5300 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
5303 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
5307 * Derive Export RT automatically for VNI.
5309 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5311 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
5312 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5316 * Derive RD automatically for VNI using passed information - it
5317 * is of the form RouterId:unique-id-for-vni.
5319 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5321 if (is_vrf_rd_configured(bgp
))
5324 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5328 * Derive RD automatically for VNI using passed information - it
5329 * is of the form RouterId:unique-id-for-vni.
5331 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5335 vpn
->prd
.family
= AF_UNSPEC
;
5336 vpn
->prd
.prefixlen
= 64;
5337 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
5338 (void)str2prefix_rd(buf
, &vpn
->prd
);
5339 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5345 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5347 struct list
*inst
= bm
->bgp
;
5348 struct listnode
*node
;
5349 struct bgp
*bgp_vrf
;
5351 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5352 if (bgp_vrf
->l3vni
== vni
)
5362 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5364 struct bgpevpn
*vpn
;
5367 memset(&tmp
, 0, sizeof(struct bgpevpn
));
5369 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5374 * Create a new vpn - invoked upon configuration or zebra notification.
5376 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5377 struct in_addr originator_ip
,
5378 vrf_id_t tenant_vrf_id
,
5379 struct in_addr mcast_grp
)
5381 struct bgpevpn
*vpn
;
5386 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5388 /* Set values - RD and RT set to defaults. */
5390 vpn
->originator_ip
= originator_ip
;
5391 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5392 vpn
->mcast_grp
= mcast_grp
;
5394 /* Initialize route-target import and export lists */
5395 vpn
->import_rtl
= list_new();
5396 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5397 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
5398 vpn
->export_rtl
= list_new();
5399 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5400 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
5401 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5402 derive_rd_rt_for_vni(bgp
, vpn
);
5404 /* Initialize EVPN route table. */
5405 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5408 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
5409 XFREE(MTYPE_BGP_EVPN
, vpn
);
5413 /* add to l2vni list on corresponding vrf */
5414 bgpevpn_link_to_l3vni(vpn
);
5416 QOBJ_REG(vpn
, bgpevpn
);
5421 * Free a given VPN - called in multiple scenarios such as zebra
5422 * notification, configuration being deleted, advertise-all-vni disabled etc.
5423 * This just frees appropriate memory, caller should have taken other
5426 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5428 bgpevpn_unlink_from_l3vni(vpn
);
5429 bgp_table_unlock(vpn
->route_table
);
5430 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5431 list_delete(&vpn
->import_rtl
);
5432 list_delete(&vpn
->export_rtl
);
5433 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5434 hash_release(bgp
->vnihash
, vpn
);
5436 XFREE(MTYPE_BGP_EVPN
, vpn
);
5442 struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
)
5447 memset(&tmp
, 0, sizeof(struct evpnes
));
5448 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
5449 es
= hash_lookup(bgp
->esihash
, &tmp
);
5454 * Create a new local es - invoked upon zebra notification.
5456 struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
,
5458 struct ipaddr
*originator_ip
)
5466 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct evpnes
));
5468 /* set the ESI and originator_ip */
5469 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
5470 memcpy(&es
->originator_ip
, originator_ip
, sizeof(struct ipaddr
));
5472 /* Initialise the VTEP list */
5473 es
->vtep_list
= list_new();
5474 es
->vtep_list
->cmp
= evpn_vtep_ip_cmp
;
5476 /* auto derive RD for this es */
5477 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
5478 es
->prd
.family
= AF_UNSPEC
;
5479 es
->prd
.prefixlen
= 64;
5480 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), es
->rd_id
);
5481 (void)str2prefix_rd(buf
, &es
->prd
);
5483 /* Initialize the ES route table */
5484 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5487 if (!hash_get(bgp
->esihash
, es
, hash_alloc_intern
)) {
5488 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5492 QOBJ_REG(es
, evpnes
);
5498 * This just frees appropriate memory, caller should have taken other
5501 void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
)
5503 list_delete(&es
->vtep_list
);
5504 bgp_table_unlock(es
->route_table
);
5505 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
5506 hash_release(bgp
->esihash
, es
);
5508 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5512 * Import evpn route from global table to VNI/VRF/ESI.
5514 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5515 struct prefix
*p
, struct bgp_path_info
*pi
)
5517 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5521 * Unimport evpn route from VNI/VRF/ESI.
5523 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5524 struct prefix
*p
, struct bgp_path_info
*pi
)
5526 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5529 /* filter routes which have martian next hops */
5530 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5534 struct bgp_node
*rd_rn
, *rn
;
5535 struct bgp_table
*table
;
5536 struct bgp_path_info
*pi
;
5541 /* Walk entire global routing table and evaluate routes which could be
5542 * imported into this VPN. Note that we cannot just look at the routes
5543 * for the VNI's RD -
5544 * remote routes applicable for this VNI could have any RD.
5546 /* EVPN routes are a 2-level table. */
5547 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
5548 rd_rn
= bgp_route_next(rd_rn
)) {
5549 table
= bgp_node_get_bgp_table_info(rd_rn
);
5553 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5555 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
5558 /* Consider "valid" remote routes applicable for
5560 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5561 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5563 if (bgp_nexthop_self(bgp
, afi
,
5564 pi
->type
, pi
->sub_type
,
5567 char attr_str
[BUFSIZ
] = {0};
5568 char pbuf
[PREFIX_STRLEN
];
5570 bgp_dump_attr(pi
->attr
, attr_str
,
5573 if (bgp_debug_update(pi
->peer
, &rn
->p
,
5576 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
5583 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5586 bgp_rib_remove(rn
, pi
, pi
->peer
, afi
,
5597 * Handle del of a local MACIP.
5599 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5600 struct ipaddr
*ip
, int state
)
5602 struct bgpevpn
*vpn
;
5603 struct prefix_evpn p
;
5604 struct bgp_node
*rn
;
5606 /* Lookup VNI hash - should exist. */
5607 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5608 if (!vpn
|| !is_vni_live(vpn
)) {
5609 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5610 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5611 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5615 build_evpn_type2_prefix(&p
, mac
, ip
);
5616 if (state
== ZEBRA_NEIGH_ACTIVE
) {
5617 /* Remove EVPN type-2 route and schedule for processing. */
5618 delete_evpn_route(bgp
, vpn
, &p
);
5620 /* Re-instate the current remote best path if any */
5621 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
5623 evpn_zebra_reinstall_best_route(bgp
, vpn
, rn
);
5630 * Handle add of a local MACIP.
5632 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5633 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
)
5635 struct bgpevpn
*vpn
;
5636 struct prefix_evpn p
;
5638 /* Lookup VNI hash - should exist. */
5639 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5640 if (!vpn
|| !is_vni_live(vpn
)) {
5641 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5642 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5643 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5647 /* Create EVPN type-2 route and schedule for processing. */
5648 build_evpn_type2_prefix(&p
, mac
, ip
);
5649 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
)) {
5650 char buf
[ETHER_ADDR_STRLEN
];
5651 char buf2
[INET6_ADDRSTRLEN
];
5654 EC_BGP_EVPN_ROUTE_CREATE
,
5655 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5656 bgp
->vrf_id
, vpn
->vni
,
5657 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5660 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5661 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5668 static void link_l2vni_hash_to_l3vni(struct hash_bucket
*bucket
,
5669 struct bgp
*bgp_vrf
)
5671 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
5672 struct bgp
*bgp_evpn
= NULL
;
5674 bgp_evpn
= bgp_get_evpn();
5677 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5678 bgpevpn_link_to_l3vni(vpn
);
5681 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
,
5682 struct ethaddr
*svi_rmac
,
5683 struct ethaddr
*vrr_rmac
,
5684 struct in_addr originator_ip
, int filter
,
5685 ifindex_t svi_ifindex
,
5686 bool is_anycast_mac
)
5688 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5689 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5690 struct listnode
*node
= NULL
;
5691 struct bgpevpn
*vpn
= NULL
;
5694 /* get the EVPN instance - required to get the AS number for VRF
5697 bgp_evpn
= bgp_get_evpn();
5701 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
5707 /* if the BGP vrf instance doesn't exist - create one */
5708 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5713 ret
= bgp_get_vty(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5714 vrf_id
== VRF_DEFAULT
5715 ? BGP_INSTANCE_TYPE_DEFAULT
5716 : BGP_INSTANCE_TYPE_VRF
);
5718 case BGP_ERR_AS_MISMATCH
:
5719 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5720 "BGP is already running; AS is %u\n", as
);
5722 case BGP_ERR_INSTANCE_MISMATCH
:
5723 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5724 "BGP instance name and AS number mismatch\n");
5728 /* mark as auto created */
5729 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5732 /* associate the vrf with l3vni and related parameters */
5733 bgp_vrf
->l3vni
= l3vni
;
5734 bgp_vrf
->originator_ip
= originator_ip
;
5735 bgp_vrf
->l3vni_svi_ifindex
= svi_ifindex
;
5736 bgp_vrf
->evpn_info
->is_anycast_mac
= is_anycast_mac
;
5738 /* copy anycast MAC from VRR MAC */
5739 memcpy(&bgp_vrf
->rmac
, vrr_rmac
, ETH_ALEN
);
5740 /* copy sys RMAC from SVI MAC */
5741 memcpy(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, svi_rmac
, ETH_ALEN
);
5742 /* PIP user configured mac is not present use svi mac as sys mac */
5743 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
))
5744 memcpy(&bgp_vrf
->evpn_info
->pip_rmac
, svi_rmac
, ETH_ALEN
);
5746 if (bgp_debug_zebra(NULL
)) {
5747 char buf
[ETHER_ADDR_STRLEN
];
5748 char buf1
[ETHER_ADDR_STRLEN
];
5749 char buf2
[ETHER_ADDR_STRLEN
];
5751 zlog_debug("VRF %s vni %u pip %s RMAC %s sys RMAC %s static RMAC %s is_anycast_mac %s",
5752 vrf_id_to_name(bgp_vrf
->vrf_id
),
5754 bgp_vrf
->evpn_info
->advertise_pip
? "enable"
5756 prefix_mac2str(&bgp_vrf
->rmac
, buf
, sizeof(buf
)),
5757 prefix_mac2str(&bgp_vrf
->evpn_info
->pip_rmac
,
5758 buf1
, sizeof(buf1
)),
5759 prefix_mac2str(&bgp_vrf
->evpn_info
->pip_rmac_static
,
5760 buf2
, sizeof(buf2
)),
5761 is_anycast_mac
? "Enable" : "Disable");
5763 /* set the right filter - are we using l3vni only for prefix routes? */
5765 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5767 /* Map auto derive or configured RTs */
5768 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5769 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5771 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
5773 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5774 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5776 /* auto derive RD */
5777 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5779 /* link all corresponding l2vnis */
5780 hash_iterate(bgp_evpn
->vnihash
,
5781 (void (*)(struct hash_bucket
*,
5782 void *))link_l2vni_hash_to_l3vni
,
5785 /* Only update all corresponding type-2 routes if we are advertising two
5786 * labels along with type-2 routes
5789 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5790 update_routes_for_vni(bgp_evpn
, vpn
);
5792 /* advertise type-5 routes if needed */
5793 update_advertise_vrf_routes(bgp_vrf
);
5795 /* install all remote routes belonging to this l3vni into correspondng
5797 install_routes_for_vrf(bgp_vrf
);
5802 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5804 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5805 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5806 struct listnode
*node
= NULL
;
5807 struct listnode
*next
= NULL
;
5808 struct bgpevpn
*vpn
= NULL
;
5810 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5814 "Cannot process L3VNI %u Del - Could not find BGP instance",
5819 bgp_evpn
= bgp_get_evpn();
5823 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
5828 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5829 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5830 * routes. This will uninstalling the routes from zebra and decremnt the
5833 uninstall_routes_for_vrf(bgp_vrf
);
5835 /* delete/withdraw all type-5 routes */
5836 delete_withdraw_vrf_routes(bgp_vrf
);
5838 /* remove the l3vni from vrf instance */
5841 /* remove the Rmac from the BGP vrf */
5842 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5843 memset(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, 0, ETH_ALEN
);
5844 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
) &&
5845 !is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac
))
5846 memset(&bgp_vrf
->evpn_info
->pip_rmac
, 0, ETH_ALEN
);
5848 /* remove default import RT or Unmap non-default import RT */
5849 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5850 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5851 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5852 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5855 /* remove default export RT */
5856 if (!list_isempty(bgp_vrf
->vrf_export_rtl
) &&
5857 !CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
5858 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5861 /* update all corresponding local mac-ip routes */
5862 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5863 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5864 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5865 update_routes_for_vni(bgp_evpn
, vpn
);
5869 /* If any L2VNIs point to this instance, unlink them. */
5870 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5871 bgpevpn_unlink_from_l3vni(vpn
);
5873 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5875 /* Delete the instance if it was autocreated */
5876 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5877 bgp_delete(bgp_vrf
);
5883 * Handle del of a local VNI.
5885 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5887 struct bgpevpn
*vpn
;
5889 /* Locate VNI hash */
5890 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5892 if (bgp_debug_zebra(NULL
))
5894 EC_BGP_EVPN_VPN_VNI
,
5895 "%u: VNI hash entry for VNI %u not found at DEL",
5900 /* Remove all local EVPN routes and schedule for processing (to
5901 * withdraw from peers).
5903 delete_routes_for_vni(bgp
, vpn
);
5906 * tunnel is no longer active, del tunnel ip address from tip_hash
5908 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5910 /* Clear "live" flag and see if hash needs to be freed. */
5911 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5912 if (!is_vni_configured(vpn
))
5913 bgp_evpn_free(bgp
, vpn
);
5919 * Handle add (or update) of a local VNI. The VNI changes we care
5920 * about are for the local-tunnel-ip and the (tenant) VRF.
5922 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5923 struct in_addr originator_ip
,
5924 vrf_id_t tenant_vrf_id
,
5925 struct in_addr mcast_grp
)
5928 struct bgpevpn
*vpn
;
5929 struct prefix_evpn p
;
5931 /* Lookup VNI. If present and no change, exit. */
5932 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5935 if (is_vni_live(vpn
)
5936 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5937 && IPV4_ADDR_SAME(&vpn
->mcast_grp
, &mcast_grp
)
5938 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5939 /* Probably some other param has changed that we don't
5943 bgp_evpn_mcast_grp_change(bgp
, vpn
, mcast_grp
);
5945 /* Update tenant_vrf_id if it has changed. */
5946 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5947 bgpevpn_unlink_from_l3vni(vpn
);
5948 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5949 bgpevpn_link_to_l3vni(vpn
);
5952 /* If tunnel endpoint IP has changed, update (and delete prior
5953 * type-3 route, if needed.)
5955 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5956 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5958 /* Update all routes with new endpoint IP and/or export RT
5961 if (is_vni_live(vpn
))
5962 update_routes_for_vni(bgp
, vpn
);
5965 /* Create or update as appropriate. */
5967 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
,
5972 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5978 /* if the VNI is live already, there is nothing more to do */
5979 if (is_vni_live(vpn
))
5982 /* Mark as "live" */
5983 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5985 /* tunnel is now active, add tunnel-ip to db */
5986 bgp_tip_add(bgp
, &originator_ip
);
5988 /* filter routes as nexthop database has changed */
5989 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5992 * Create EVPN type-3 route and schedule for processing.
5994 * RT-3 only if doing head-end replication
5996 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
5997 == VXLAN_FLOOD_HEAD_END_REPL
) {
5998 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5999 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0)) {
6000 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
6001 "%u: Type3 route creation failure for VNI %u",
6007 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
6011 install_routes_for_vni(bgp
, vpn
);
6013 /* If we are advertising gateway mac-ip
6014 It needs to be conveyed again to zebra */
6015 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
6017 /* advertise svi mac-ip knob to zebra */
6018 bgp_zebra_advertise_svi_macip(bgp
, vpn
->advertise_svi_macip
, vpn
->vni
);
6024 * bgp_evpn_local_es_del
6026 int bgp_evpn_local_es_del(struct bgp
*bgp
,
6028 struct ipaddr
*originator_ip
)
6030 char buf
[ESI_STR_LEN
];
6031 struct evpnes
*es
= NULL
;
6033 if (!bgp
->esihash
) {
6034 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
6039 /* Lookup ESI hash - should exist. */
6040 es
= bgp_evpn_lookup_es(bgp
, esi
);
6042 flog_warn(EC_BGP_EVPN_ESI
,
6043 "%u: ESI hash entry for ESI %s at Local ES DEL",
6044 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
6048 /* Delete all local EVPN ES routes from ESI table
6049 * and schedule for processing (to withdraw from peers))
6051 delete_routes_for_es(bgp
, es
);
6053 /* free the hash entry */
6054 bgp_evpn_es_free(bgp
, es
);
6060 * bgp_evpn_local_es_add
6062 int bgp_evpn_local_es_add(struct bgp
*bgp
,
6064 struct ipaddr
*originator_ip
)
6066 char buf
[ESI_STR_LEN
];
6067 struct evpnes
*es
= NULL
;
6068 struct prefix_evpn p
;
6070 if (!bgp
->esihash
) {
6071 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
6076 /* create the new es */
6077 es
= bgp_evpn_lookup_es(bgp
, esi
);
6079 es
= bgp_evpn_es_new(bgp
, esi
, originator_ip
);
6083 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
6084 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
6088 UNSET_FLAG(es
->flags
, EVPNES_REMOTE
);
6089 SET_FLAG(es
->flags
, EVPNES_LOCAL
);
6091 build_evpn_type4_prefix(&p
, esi
, originator_ip
->ipaddr_v4
);
6092 if (update_evpn_type4_route(bgp
, es
, &p
)) {
6093 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
6094 "%u: Type4 route creation failure for ESI %s",
6095 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
6099 /* import all remote ES routes in th ES table */
6100 install_routes_for_es(bgp
, es
);
6106 * Handle change in setting for BUM handling. The supported values
6107 * are head-end replication and dropping all BUM packets. Any change
6108 * should be registered with zebra. Also, if doing head-end replication,
6109 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
6110 * to be dropped, the RT-3s must be withdrawn.
6112 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
6114 zlog_info("L2VPN EVPN BUM handling is %s",
6115 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
6116 "Flooding" : "Flooding Disabled");
6118 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
6119 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
6120 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
6121 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
6122 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
6126 * Cleanup EVPN information on disable - Need to delete and withdraw
6127 * EVPN routes from peers.
6129 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
6131 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_bucket
*,
6132 void *))cleanup_vni_on_disable
,
6137 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
6138 * BGP instance (default) is being freed.
6140 void bgp_evpn_cleanup(struct bgp
*bgp
)
6142 hash_iterate(bgp
->vnihash
,
6143 (void (*)(struct hash_bucket
*, void *))free_vni_entry
,
6146 hash_free(bgp
->import_rt_hash
);
6147 bgp
->import_rt_hash
= NULL
;
6149 hash_free(bgp
->vrf_import_rt_hash
);
6150 bgp
->vrf_import_rt_hash
= NULL
;
6152 hash_free(bgp
->vnihash
);
6153 bgp
->vnihash
= NULL
;
6155 hash_free(bgp
->esihash
);
6156 bgp
->esihash
= NULL
;
6158 list_delete(&bgp
->vrf_import_rtl
);
6159 list_delete(&bgp
->vrf_export_rtl
);
6160 list_delete(&bgp
->l2vnis
);
6164 * Initialization for EVPN
6167 * hash for RT to VNI
6169 void bgp_evpn_init(struct bgp
*bgp
)
6172 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
6174 hash_create(esi_hash_keymake
, esi_cmp
,
6175 "BGP EVPN Local ESI Hash");
6176 bgp
->import_rt_hash
=
6177 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
6178 "BGP Import RT Hash");
6179 bgp
->vrf_import_rt_hash
=
6180 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
6181 "BGP VRF Import RT Hash");
6182 bgp
->vrf_import_rtl
= list_new();
6183 bgp
->vrf_import_rtl
->cmp
=
6184 (int (*)(void *, void *))evpn_route_target_cmp
;
6185 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
6186 bgp
->vrf_export_rtl
= list_new();
6187 bgp
->vrf_export_rtl
->cmp
=
6188 (int (*)(void *, void *))evpn_route_target_cmp
;
6189 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
6190 bgp
->l2vnis
= list_new();
6191 bgp
->l2vnis
->cmp
= vni_list_cmp
;
6192 /* By default Duplicate Address Dection is enabled.
6193 * Max-moves (N) 5, detection time (M) 180
6194 * default action is warning-only
6195 * freeze action permanently freezes address,
6196 * and freeze time (auto-recovery) is disabled.
6198 if (bgp
->evpn_info
) {
6199 bgp
->evpn_info
->dup_addr_detect
= true;
6200 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
6201 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
6202 bgp
->evpn_info
->dad_freeze
= false;
6203 bgp
->evpn_info
->dad_freeze_time
= 0;
6204 /* Initialize zebra vxlan */
6205 bgp_zebra_dup_addr_detection(bgp
);
6206 /* Enable PIP feature by default for bgp vrf instance */
6207 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
6208 struct bgp
*bgp_default
;
6210 bgp
->evpn_info
->advertise_pip
= true;
6211 bgp_default
= bgp_get_default();
6213 bgp
->evpn_info
->pip_ip
= bgp_default
->router_id
;
6217 /* Default BUM handling is to do head-end replication. */
6218 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
6221 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
6223 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
6227 * Get the prefixlen of the ip prefix carried within the type5 evpn route.
6229 int bgp_evpn_get_type5_prefixlen(struct prefix
*pfx
)
6231 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6233 if (!pfx
|| pfx
->family
!= AF_EVPN
)
6236 if (evp
->prefix
.route_type
!= BGP_EVPN_IP_PREFIX_ROUTE
)
6239 return evp
->prefix
.prefix_addr
.ip_prefix_length
;
6243 * Should we register nexthop for this EVPN prefix for nexthop tracking?
6245 bool bgp_evpn_is_prefix_nht_supported(struct prefix
*pfx
)
6247 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6250 * EVPN RT-5 should not be marked as valid and imported to vrfs if the
6251 * BGP nexthop is not reachable. To check for the nexthop reachability,
6252 * Add nexthop for EVPN RT-5 for nexthop tracking.
6254 * Ideally, a BGP route should be marked as valid only if the
6255 * nexthop is reachable. Thus, other EVPN route types also should be
6256 * added here after testing is performed for them.
6258 if (pfx
&& pfx
->family
== AF_EVPN
&&
6259 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
)