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
,
67 struct bgp_dest
*dest
,
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 const 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
, false, false);
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 const 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
))
604 "%s: No zebra instance to talk to, not installing remote macip",
611 zclient_create_header(
612 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
614 stream_putl(s
, vpn
->vni
);
615 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
616 /* IP address length and IP address, if any. */
617 if (is_evpn_prefix_ipaddr_none(p
))
620 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
622 stream_putl(s
, ipa_len
);
623 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
625 stream_put_in_addr(s
, &remote_vtep_ip
);
627 /* TX flags - MAC sticky status and/or gateway mac */
628 /* Also TX the sequence number of the best route. */
630 stream_putc(s
, flags
);
634 stream_putw_at(s
, 0, stream_get_endp(s
));
636 if (bgp_debug_zebra(NULL
))
638 "Tx %s MACIP, VNI %u MAC %s IP %s flags 0x%x seq %u remote VTEP %s",
639 add
? "ADD" : "DEL", vpn
->vni
,
640 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
642 ipaddr2str(&p
->prefix
.macip_addr
.ip
,
643 buf3
, sizeof(buf3
)), flags
, seq
,
644 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
647 return zclient_send_message(zclient
);
651 * Add (update) or delete remote VTEP from zebra.
653 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
654 const struct prefix_evpn
*p
,
655 int flood_control
, int add
)
660 if (!zclient
|| zclient
->sock
< 0)
663 /* Don't try to register if Zebra doesn't know of this instance. */
664 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
665 if (BGP_DEBUG(zebra
, ZEBRA
))
667 "%s: No zebra instance to talk to, not installing remote vtep",
675 zclient_create_header(
676 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
678 stream_putl(s
, vpn
->vni
);
679 if (is_evpn_prefix_ipaddr_v4(p
))
680 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
681 else if (is_evpn_prefix_ipaddr_v6(p
)) {
684 "Bad remote IP when trying to %s remote VTEP for VNI %u",
685 add
? "ADD" : "DEL", vpn
->vni
);
688 stream_putl(s
, flood_control
);
690 stream_putw_at(s
, 0, stream_get_endp(s
));
692 if (bgp_debug_zebra(NULL
))
693 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
694 add
? "ADD" : "DEL", vpn
->vni
,
695 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
697 return zclient_send_message(zclient
);
701 * Build extended community for EVPN ES (type-4) route
703 static void build_evpn_type4_route_extcomm(struct evpnes
*es
,
706 struct ecommunity ecom_encap
;
707 struct ecommunity ecom_es_rt
;
708 struct ecommunity_val eval
;
709 struct ecommunity_val eval_es_rt
;
710 bgp_encap_types tnl_type
;
714 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
715 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
716 encode_encap_extcomm(tnl_type
, &eval
);
718 ecom_encap
.val
= (uint8_t *)eval
.val
;
719 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
722 memset(&mac
, 0, sizeof(struct ethaddr
));
723 memset(&ecom_es_rt
, 0, sizeof(ecom_es_rt
));
724 es_get_system_mac(&es
->esi
, &mac
);
725 encode_es_rt_extcomm(&eval_es_rt
, &mac
);
727 ecom_es_rt
.val
= (uint8_t *)eval_es_rt
.val
;
729 ecommunity_merge(attr
->ecommunity
, &ecom_es_rt
);
731 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
735 * Build extended communities for EVPN prefix route.
737 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
740 struct ecommunity ecom_encap
;
741 struct ecommunity_val eval
;
742 struct ecommunity_val eval_rmac
;
743 bgp_encap_types tnl_type
;
744 struct listnode
*node
, *nnode
;
745 struct ecommunity
*ecom
;
746 struct ecommunity
*old_ecom
;
747 struct list
*vrf_export_rtl
= NULL
;
750 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
751 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
752 encode_encap_extcomm(tnl_type
, &eval
);
754 ecom_encap
.val
= (uint8_t *)eval
.val
;
757 if (attr
->ecommunity
) {
758 old_ecom
= attr
->ecommunity
;
759 ecom
= ecommunity_merge(ecommunity_dup(old_ecom
), &ecom_encap
);
760 if (!old_ecom
->refcnt
)
761 ecommunity_free(&old_ecom
);
763 ecom
= ecommunity_dup(&ecom_encap
);
764 attr
->ecommunity
= ecom
;
766 /* Add the export RTs for L3VNI/VRF */
767 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
768 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
770 ecommunity_merge(attr
->ecommunity
, ecom
);
772 /* add the router mac extended community */
773 if (!is_zero_mac(&attr
->rmac
)) {
774 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
775 ecommunity_add_val(attr
->ecommunity
, &eval_rmac
, true, true);
778 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
782 * Build extended communities for EVPN route.
783 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
784 * and ENCAP extended communities are applicable for all routes.
785 * The default gateway extended community and MAC mobility (sticky) extended
786 * community are added as needed based on passed settings - only for type-2
787 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
788 * added, if present, based on passed settings - only for non-link-local
791 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
794 struct ecommunity ecom_encap
;
795 struct ecommunity ecom_sticky
;
796 struct ecommunity ecom_default_gw
;
797 struct ecommunity ecom_na
;
798 struct ecommunity_val eval
;
799 struct ecommunity_val eval_sticky
;
800 struct ecommunity_val eval_default_gw
;
801 struct ecommunity_val eval_rmac
;
802 struct ecommunity_val eval_na
;
804 bgp_encap_types tnl_type
;
805 struct listnode
*node
, *nnode
;
806 struct ecommunity
*ecom
;
808 struct list
*vrf_export_rtl
= NULL
;
811 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
812 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
813 encode_encap_extcomm(tnl_type
, &eval
);
815 ecom_encap
.val
= (uint8_t *)eval
.val
;
818 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
820 /* Add the export RTs for L2VNI */
821 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
822 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
824 /* Add the export RTs for L3VNI if told to - caller determines
825 * when this should be done.
828 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
829 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
830 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
832 attr
->ecommunity
= ecommunity_merge(
833 attr
->ecommunity
, ecom
);
837 /* Add MAC mobility (sticky) if needed. */
840 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
841 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
842 ecom_sticky
.size
= 1;
843 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
845 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
848 /* Add RMAC, if told to. */
850 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
851 ecommunity_add_val(attr
->ecommunity
, &eval_rmac
, true, true);
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
) {
905 (attr
->ecommunity
->val
+ (i
* 8));
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 const struct prefix_evpn
*p
,
932 struct bgp_path_info
*pi
)
938 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
940 if (pi
->attr
->sticky
)
941 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
942 if (pi
->attr
->default_gw
)
943 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
944 if (is_evpn_prefix_ipaddr_v6(p
) &&
945 pi
->attr
->router_flag
)
946 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
947 ret
= bgp_zebra_send_remote_macip(
948 bgp
, vpn
, p
, pi
->attr
->nexthop
, 1, flags
,
949 mac_mobility_seqnum(pi
->attr
));
951 switch (pi
->attr
->pmsi_tnl_type
) {
952 case PMSI_TNLTYPE_INGR_REPL
:
953 flood_control
= VXLAN_FLOOD_HEAD_END_REPL
;
956 case PMSI_TNLTYPE_PIM_SM
:
957 flood_control
= VXLAN_FLOOD_PIM_SM
;
961 flood_control
= VXLAN_FLOOD_DISABLED
;
964 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, flood_control
, 1);
970 /* Uninstall EVPN route from zebra. */
971 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
972 const struct prefix_evpn
*p
,
973 struct in_addr remote_vtep_ip
)
977 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
978 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
981 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
,
982 VXLAN_FLOOD_DISABLED
, 0);
988 * Due to MAC mobility, the prior "local" best route has been supplanted
989 * by a "remote" best route. The prior route has to be deleted and withdrawn
992 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
993 struct bgp_dest
*dest
,
994 struct bgp_path_info
*old_local
)
996 struct bgp_dest
*global_dest
;
997 struct bgp_path_info
*pi
;
998 afi_t afi
= AFI_L2VPN
;
999 safi_t safi
= SAFI_EVPN
;
1001 /* Locate route node in the global EVPN routing table. Note that
1002 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1005 global_dest
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1006 bgp_dest_get_prefix(dest
), &vpn
->prd
);
1008 /* Delete route entry in the global EVPN table. */
1009 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
1011 /* Schedule for processing - withdraws to peers happen from
1015 bgp_process(bgp
, global_dest
, afi
, safi
);
1016 bgp_dest_unlock_node(global_dest
);
1019 /* Delete route entry in the VNI route table, caller to remove. */
1020 bgp_path_info_delete(dest
, old_local
);
1023 static struct in_addr
*es_vtep_new(struct in_addr vtep
)
1027 ip
= XCALLOC(MTYPE_BGP_EVPN_ES_VTEP
, sizeof(struct in_addr
));
1029 ip
->s_addr
= vtep
.s_addr
;
1033 static void es_vtep_free(struct in_addr
*ip
)
1035 XFREE(MTYPE_BGP_EVPN_ES_VTEP
, ip
);
1038 /* check if VTEP is already part of the list */
1039 static int is_vtep_present_in_list(struct list
*list
,
1040 struct in_addr vtep
)
1042 struct listnode
*node
= NULL
;
1043 struct in_addr
*tmp
;
1045 for (ALL_LIST_ELEMENTS_RO(list
, node
, tmp
)) {
1046 if (tmp
->s_addr
== vtep
.s_addr
)
1053 * Best path for ES route was changed,
1054 * update the list of VTEPs for this ES
1056 static int evpn_es_install_vtep(struct bgp
*bgp
, struct evpnes
*es
,
1057 const 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
, struct evpnes
*es
,
1101 struct bgp_dest
*dest
)
1104 afi_t afi
= AFI_L2VPN
;
1105 safi_t safi
= SAFI_EVPN
;
1106 struct bgp_path_info
*old_select
; /* old best */
1107 struct bgp_path_info
*new_select
; /* new best */
1108 struct bgp_path_info_pair old_and_new
;
1110 /* Compute the best path. */
1111 bgp_best_selection(bgp
, dest
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1113 old_select
= old_and_new
.old
;
1114 new_select
= old_and_new
.new;
1117 * If the best path hasn't changed - see if something needs to be
1120 if (old_select
&& old_select
== new_select
1121 && old_select
->type
== ZEBRA_ROUTE_BGP
1122 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1123 && !CHECK_FLAG(dest
->flags
, BGP_NODE_USER_CLEAR
)
1124 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1125 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1126 if (bgp_zebra_has_route_changed(old_select
)) {
1127 ret
= evpn_es_install_vtep(
1129 (const struct prefix_evpn
*)bgp_dest_get_prefix(
1131 old_select
->attr
->nexthop
);
1133 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1134 UNSET_FLAG(old_select
->flags
, BGP_PATH_LINK_BW_CHG
);
1135 bgp_zebra_clear_route_change_flags(dest
);
1139 /* If the user did a "clear" this flag will be set */
1140 UNSET_FLAG(dest
->flags
, BGP_NODE_USER_CLEAR
);
1143 * bestpath has changed; update relevant fields and install or uninstall
1144 * into the zebra RIB.
1146 if (old_select
|| new_select
)
1147 bgp_bump_version(dest
);
1150 bgp_path_info_unset_flag(dest
, old_select
, BGP_PATH_SELECTED
);
1152 bgp_path_info_set_flag(dest
, new_select
, BGP_PATH_SELECTED
);
1153 bgp_path_info_unset_flag(dest
, new_select
,
1154 BGP_PATH_ATTR_CHANGED
);
1155 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1156 UNSET_FLAG(new_select
->flags
, BGP_PATH_LINK_BW_CHG
);
1159 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1160 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1161 ret
= evpn_es_install_vtep(
1163 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1164 new_select
->attr
->nexthop
);
1166 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1167 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1168 ret
= evpn_es_uninstall_vtep(
1170 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1171 old_select
->attr
->nexthop
);
1174 /* Clear any route change flags. */
1175 bgp_zebra_clear_route_change_flags(dest
);
1177 /* Reap old select bgp_path_info, if it has been removed */
1178 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1179 bgp_path_info_reap(dest
, old_select
);
1185 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1188 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1189 struct bgp_dest
*dest
)
1191 struct bgp_path_info
*old_select
, *new_select
;
1192 struct bgp_path_info_pair old_and_new
;
1193 afi_t afi
= AFI_L2VPN
;
1194 safi_t safi
= SAFI_EVPN
;
1197 /* Compute the best path. */
1198 bgp_best_selection(bgp
, dest
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1200 old_select
= old_and_new
.old
;
1201 new_select
= old_and_new
.new;
1203 /* If the best path hasn't changed - see if there is still something to
1207 if (old_select
&& old_select
== new_select
1208 && old_select
->type
== ZEBRA_ROUTE_BGP
1209 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1210 && !CHECK_FLAG(dest
->flags
, BGP_NODE_USER_CLEAR
)
1211 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1212 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1213 if (bgp_zebra_has_route_changed(old_select
))
1214 ret
= evpn_zebra_install(
1216 (const struct prefix_evpn
*)bgp_dest_get_prefix(
1219 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1220 UNSET_FLAG(old_select
->flags
, BGP_PATH_LINK_BW_CHG
);
1221 bgp_zebra_clear_route_change_flags(dest
);
1225 /* If the user did a "clear" this flag will be set */
1226 UNSET_FLAG(dest
->flags
, BGP_NODE_USER_CLEAR
);
1228 /* bestpath has changed; update relevant fields and install or uninstall
1229 * into the zebra RIB.
1231 if (old_select
|| new_select
)
1232 bgp_bump_version(dest
);
1235 bgp_path_info_unset_flag(dest
, old_select
, BGP_PATH_SELECTED
);
1237 bgp_path_info_set_flag(dest
, new_select
, BGP_PATH_SELECTED
);
1238 bgp_path_info_unset_flag(dest
, new_select
,
1239 BGP_PATH_ATTR_CHANGED
);
1240 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1241 UNSET_FLAG(new_select
->flags
, BGP_PATH_LINK_BW_CHG
);
1244 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1245 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1246 ret
= evpn_zebra_install(
1248 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1251 /* If an old best existed and it was a "local" route, the only
1253 * it would be supplanted is due to MAC mobility procedures. So,
1255 * need to do an implicit delete and withdraw that route from
1258 if (old_select
&& old_select
->peer
== bgp
->peer_self
1259 && old_select
->type
== ZEBRA_ROUTE_BGP
1260 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
1261 evpn_delete_old_local_route(bgp
, vpn
, dest
, old_select
);
1263 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1264 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1265 ret
= evpn_zebra_uninstall(
1267 (const struct prefix_evpn
*)bgp_dest_get_prefix(
1269 old_select
->attr
->nexthop
);
1272 /* Clear any route change flags. */
1273 bgp_zebra_clear_route_change_flags(dest
);
1275 /* Reap old select bgp_path_info, if it has been removed */
1276 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1277 bgp_path_info_reap(dest
, old_select
);
1283 * Return true if the local ri for this rn is of type gateway mac
1285 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_dest
*dest
)
1287 struct bgp_path_info
*tmp_pi
= NULL
;
1288 struct bgp_path_info
*local_pi
= NULL
;
1291 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1292 tmp_pi
= tmp_pi
->next
) {
1293 if (tmp_pi
->peer
== bgp
->peer_self
1294 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1295 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1302 return local_pi
->attr
->default_gw
;
1307 * Return true if the local ri for this rn has sticky set
1309 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_dest
*dest
)
1311 struct bgp_path_info
*tmp_pi
;
1312 struct bgp_path_info
*local_pi
;
1315 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1316 tmp_pi
= tmp_pi
->next
) {
1317 if (tmp_pi
->peer
== bgp
->peer_self
1318 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1319 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1326 return local_pi
->attr
->sticky
;
1330 * create or update EVPN type4 route entry.
1331 * This could be in the ES table or the global table.
1332 * TODO: handle remote ES (type4) routes as well
1334 static int update_evpn_type4_route_entry(struct bgp
*bgp
, struct evpnes
*es
,
1335 afi_t afi
, safi_t safi
,
1336 struct bgp_dest
*dest
,
1337 struct attr
*attr
, int add
,
1338 struct bgp_path_info
**ri
,
1341 char buf
[ESI_STR_LEN
];
1342 char buf1
[INET6_ADDRSTRLEN
];
1343 struct bgp_path_info
*tmp_pi
= NULL
;
1344 struct bgp_path_info
*local_pi
= NULL
; /* local route entry if any */
1345 struct bgp_path_info
*remote_pi
= NULL
; /* remote route entry if any */
1346 struct attr
*attr_new
= NULL
;
1347 const struct prefix_evpn
*evp
= NULL
;
1351 evp
= (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1353 /* locate the local and remote entries if any */
1354 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1355 tmp_pi
= tmp_pi
->next
) {
1356 if (tmp_pi
->peer
== bgp
->peer_self
1357 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1358 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1360 if (tmp_pi
->type
== ZEBRA_ROUTE_BGP
1361 && tmp_pi
->sub_type
== BGP_ROUTE_IMPORTED
1362 && CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1366 /* we don't expect to see a remote_ri at this point.
1367 * An ES route has esi + vtep_ip as the key,
1368 * We shouldn't see the same route from any other vtep.
1373 "%u ERROR: local es route for ESI: %s Vtep %s also learnt from remote",
1375 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1376 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1380 if (!local_pi
&& !add
)
1383 /* create or update the entry */
1386 /* Add or update attribute to hash */
1387 attr_new
= bgp_attr_intern(attr
);
1389 /* Create new route with its attribute. */
1390 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1391 bgp
->peer_self
, attr_new
, dest
);
1392 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1394 /* add the newly created path to the route-node */
1395 bgp_path_info_add(dest
, tmp_pi
);
1398 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1399 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1402 /* The attribute has changed.
1403 * Add (or update) attribute to hash. */
1404 attr_new
= bgp_attr_intern(attr
);
1405 bgp_path_info_set_flag(dest
, tmp_pi
,
1406 BGP_PATH_ATTR_CHANGED
);
1408 /* Restore route, if needed. */
1409 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1410 bgp_path_info_restore(dest
, tmp_pi
);
1412 /* Unintern existing, set to new. */
1413 bgp_attr_unintern(&tmp_pi
->attr
);
1414 tmp_pi
->attr
= attr_new
;
1415 tmp_pi
->uptime
= bgp_clock();
1419 /* Return back the route entry. */
1424 /* update evpn es (type-4) route */
1425 static int update_evpn_type4_route(struct bgp
*bgp
,
1427 struct prefix_evpn
*p
)
1430 int route_changed
= 0;
1431 char buf
[ESI_STR_LEN
];
1432 char buf1
[INET6_ADDRSTRLEN
];
1433 afi_t afi
= AFI_L2VPN
;
1434 safi_t safi
= SAFI_EVPN
;
1436 struct attr
*attr_new
= NULL
;
1437 struct bgp_dest
*dest
= NULL
;
1438 struct bgp_path_info
*pi
= NULL
;
1440 memset(&attr
, 0, sizeof(struct attr
));
1442 /* Build path-attribute for this route. */
1443 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1444 attr
.nexthop
= es
->originator_ip
.ipaddr_v4
;
1445 attr
.mp_nexthop_global_in
= es
->originator_ip
.ipaddr_v4
;
1446 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1448 /* Set up extended community. */
1449 build_evpn_type4_route_extcomm(es
, &attr
);
1451 /* First, create (or fetch) route node within the ESI. */
1452 /* NOTE: There is no RD here. */
1453 dest
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
1455 /* Create or update route entry. */
1456 ret
= update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, dest
, &attr
, 1,
1457 &pi
, &route_changed
);
1459 flog_err(EC_BGP_ES_INVALID
,
1460 "%u ERROR: Failed to updated ES route ESI: %s VTEP %s",
1462 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1463 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1467 attr_new
= pi
->attr
;
1469 /* Perform route selection;
1470 * this is just to set the flags correctly
1471 * as local route in the ES always wins.
1473 evpn_es_route_select_install(bgp
, es
, dest
);
1474 bgp_dest_unlock_node(dest
);
1476 /* If this is a new route or some attribute has changed, export the
1477 * route to the global table. The route will be advertised to peers
1478 * from there. Note that this table is a 2-level tree (RD-level +
1479 * Prefix-level) similar to L3VPN routes.
1481 if (route_changed
) {
1482 struct bgp_path_info
*global_pi
;
1484 dest
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1485 (struct prefix
*)p
, &es
->prd
);
1486 update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, dest
,
1487 attr_new
, 1, &global_pi
,
1490 /* Schedule for processing and unlock node. */
1491 bgp_process(bgp
, dest
, afi
, safi
);
1492 bgp_dest_unlock_node(dest
);
1495 /* Unintern temporary. */
1496 aspath_unintern(&attr
.aspath
);
1500 static int update_evpn_type5_route_entry(struct bgp
*bgp_evpn
,
1501 struct bgp
*bgp_vrf
, afi_t afi
,
1502 safi_t safi
, struct bgp_dest
*dest
,
1503 struct attr
*attr
, int *route_changed
)
1505 struct attr
*attr_new
= NULL
;
1506 struct bgp_path_info
*pi
= NULL
;
1507 mpls_label_t label
= MPLS_INVALID_LABEL
;
1508 struct bgp_path_info
*local_pi
= NULL
;
1509 struct bgp_path_info
*tmp_pi
= NULL
;
1512 /* locate the local route entry if any */
1513 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1514 tmp_pi
= tmp_pi
->next
) {
1515 if (tmp_pi
->peer
== bgp_evpn
->peer_self
1516 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1517 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1522 * create a new route entry if one doesn't exist.
1523 * Otherwise see if route attr has changed
1527 /* route has changed as this is the first entry */
1530 /* Add (or update) attribute to hash. */
1531 attr_new
= bgp_attr_intern(attr
);
1533 /* create the route info from attribute */
1534 pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1535 bgp_evpn
->peer_self
, attr_new
, dest
);
1536 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
1538 /* Type-5 routes advertise the L3-VNI */
1539 bgp_path_info_extra_get(pi
);
1540 vni2label(bgp_vrf
->l3vni
, &label
);
1541 memcpy(&pi
->extra
->label
, &label
, sizeof(label
));
1542 pi
->extra
->num_labels
= 1;
1544 /* add the route entry to route node*/
1545 bgp_path_info_add(dest
, pi
);
1549 if (!attrhash_cmp(tmp_pi
->attr
, attr
)) {
1551 /* attribute changed */
1554 /* The attribute has changed. */
1555 /* Add (or update) attribute to hash. */
1556 attr_new
= bgp_attr_intern(attr
);
1557 bgp_path_info_set_flag(dest
, tmp_pi
,
1558 BGP_PATH_ATTR_CHANGED
);
1560 /* Restore route, if needed. */
1561 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1562 bgp_path_info_restore(dest
, tmp_pi
);
1564 /* Unintern existing, set to new. */
1565 bgp_attr_unintern(&tmp_pi
->attr
);
1566 tmp_pi
->attr
= attr_new
;
1567 tmp_pi
->uptime
= bgp_clock();
1573 /* update evpn type-5 route entry */
1574 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1575 struct attr
*src_attr
)
1577 afi_t afi
= AFI_L2VPN
;
1578 safi_t safi
= SAFI_EVPN
;
1580 struct bgp_dest
*dest
= NULL
;
1581 struct bgp
*bgp_evpn
= NULL
;
1582 int route_changed
= 0;
1584 bgp_evpn
= bgp_get_evpn();
1588 /* Build path attribute for this route - use the source attr, if
1589 * present, else treat as locally originated.
1594 memset(&attr
, 0, sizeof(struct attr
));
1595 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1598 /* Advertise Primary IP (PIP) is enabled, send individual
1599 * IP (default instance router-id) as nexthop.
1600 * PIP is disabled or vrr interface is not present
1601 * use anycast-IP as nexthop and anycast RMAC.
1603 if (!bgp_vrf
->evpn_info
->advertise_pip
||
1604 (!bgp_vrf
->evpn_info
->is_anycast_mac
)) {
1605 attr
.nexthop
= bgp_vrf
->originator_ip
;
1606 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1607 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, ETH_ALEN
);
1610 memcpy(&attr
.rmac
, &bgp_vrf
->evpn_info
->pip_rmac
, ETH_ALEN
);
1611 if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
!= INADDR_ANY
) {
1612 attr
.nexthop
= bgp_vrf
->evpn_info
->pip_ip
;
1613 attr
.mp_nexthop_global_in
= bgp_vrf
->evpn_info
->pip_ip
;
1614 } else if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
== INADDR_ANY
)
1615 if (bgp_debug_zebra(NULL
)) {
1616 char buf1
[PREFIX_STRLEN
];
1618 zlog_debug("VRF %s evp %s advertise-pip primary ip is not configured",
1619 vrf_id_to_name(bgp_vrf
->vrf_id
),
1620 prefix2str(evp
, buf1
, sizeof(buf1
)));
1624 if (bgp_debug_zebra(NULL
)) {
1625 char buf
[ETHER_ADDR_STRLEN
];
1626 char buf1
[PREFIX_STRLEN
];
1627 char buf2
[INET6_ADDRSTRLEN
];
1629 zlog_debug("VRF %s type-5 route evp %s RMAC %s nexthop %s",
1630 vrf_id_to_name(bgp_vrf
->vrf_id
),
1631 prefix2str(evp
, buf1
, sizeof(buf1
)),
1632 prefix_mac2str(&attr
.rmac
, buf
, sizeof(buf
)),
1633 inet_ntop(AF_INET
, &attr
.nexthop
, buf2
,
1637 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1639 /* Setup RT and encap extended community */
1640 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1642 /* get the route node in global table */
1643 dest
= bgp_afi_node_get(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1644 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1647 /* create or update the route entry within the route node */
1648 update_evpn_type5_route_entry(bgp_evpn
, bgp_vrf
, afi
, safi
, dest
, &attr
,
1651 /* schedule for processing and unlock node */
1652 if (route_changed
) {
1653 bgp_process(bgp_evpn
, dest
, afi
, safi
);
1654 bgp_dest_unlock_node(dest
);
1657 /* uninten temporary */
1659 aspath_unintern(&attr
.aspath
);
1664 * Create or update EVPN route entry. This could be in the VNI route table
1665 * or the global route table.
1667 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1668 afi_t afi
, safi_t safi
,
1669 struct bgp_dest
*dest
, struct attr
*attr
,
1670 int add
, struct bgp_path_info
**pi
,
1671 uint8_t flags
, uint32_t seq
)
1673 struct bgp_path_info
*tmp_pi
;
1674 struct bgp_path_info
*local_pi
;
1675 struct attr
*attr_new
;
1676 mpls_label_t label
[BGP_MAX_LABELS
];
1677 uint32_t num_labels
= 1;
1678 int route_change
= 1;
1680 const struct prefix_evpn
*evp
;
1683 evp
= (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1684 memset(&label
, 0, sizeof(label
));
1686 /* See if this is an update of an existing route, or a new add. */
1688 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1689 tmp_pi
= tmp_pi
->next
) {
1690 if (tmp_pi
->peer
== bgp
->peer_self
1691 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1692 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1696 /* If route doesn't exist already, create a new one, if told to.
1697 * Otherwise act based on whether the attributes of the route have
1700 if (!local_pi
&& !add
)
1703 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1704 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1705 add_mac_mobility_to_attr(seq
, attr
);
1708 /* Add (or update) attribute to hash. */
1709 attr_new
= bgp_attr_intern(attr
);
1711 /* Extract MAC mobility sequence number, if any. */
1712 attr_new
->mm_seqnum
=
1713 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1714 attr_new
->sticky
= sticky
;
1716 /* Create new route with its attribute. */
1717 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1718 bgp
->peer_self
, attr_new
, dest
);
1719 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1720 bgp_path_info_extra_get(tmp_pi
);
1722 /* The VNI goes into the 'label' field of the route */
1723 vni2label(vpn
->vni
, &label
[0]);
1725 /* Type-2 routes may carry a second VNI - the L3-VNI.
1726 * Only attach second label if we are advertising two labels for
1729 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1730 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1733 l3vni
= bgpevpn_get_l3vni(vpn
);
1735 vni2label(l3vni
, &label
[1]);
1740 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1741 tmp_pi
->extra
->num_labels
= num_labels
;
1742 /* Mark route as self type-2 route */
1743 if (flags
&& CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1744 tmp_pi
->extra
->af_flags
= BGP_EVPN_MACIP_TYPE_SVI_IP
;
1745 bgp_path_info_add(dest
, tmp_pi
);
1748 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1749 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1753 * The attributes have changed, type-2 routes needs to
1754 * be advertised with right labels.
1756 vni2label(vpn
->vni
, &label
[0]);
1757 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1758 && CHECK_FLAG(vpn
->flags
,
1759 VNI_FLAG_USE_TWO_LABELS
)) {
1762 l3vni
= bgpevpn_get_l3vni(vpn
);
1764 vni2label(l3vni
, &label
[1]);
1768 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1769 tmp_pi
->extra
->num_labels
= num_labels
;
1771 /* The attribute has changed. */
1772 /* Add (or update) attribute to hash. */
1773 attr_new
= bgp_attr_intern(attr
);
1774 bgp_path_info_set_flag(dest
, tmp_pi
,
1775 BGP_PATH_ATTR_CHANGED
);
1777 /* Extract MAC mobility sequence number, if any. */
1778 attr_new
->mm_seqnum
=
1779 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1780 attr_new
->sticky
= sticky
;
1782 /* Restore route, if needed. */
1783 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1784 bgp_path_info_restore(dest
, tmp_pi
);
1786 /* Unintern existing, set to new. */
1787 bgp_attr_unintern(&tmp_pi
->attr
);
1788 tmp_pi
->attr
= attr_new
;
1789 tmp_pi
->uptime
= bgp_clock();
1793 /* Return back the route entry. */
1795 return route_change
;
1798 static void evpn_zebra_reinstall_best_route(struct bgp
*bgp
,
1799 struct bgpevpn
*vpn
,
1800 struct bgp_dest
*dest
)
1802 struct bgp_path_info
*tmp_ri
;
1803 struct bgp_path_info
*curr_select
= NULL
;
1805 for (tmp_ri
= bgp_dest_get_bgp_path_info(dest
); tmp_ri
;
1806 tmp_ri
= tmp_ri
->next
) {
1807 if (CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_SELECTED
)) {
1808 curr_select
= tmp_ri
;
1813 if (curr_select
&& curr_select
->type
== ZEBRA_ROUTE_BGP
1814 && curr_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1817 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1822 * If the local route was not selected evict it and tell zebra to re-add
1823 * the best remote dest.
1825 * Typically a local path added by zebra is expected to be selected as
1826 * best. In which case when a remote path wins as best (later)
1827 * evpn_route_select_install itself evicts the older-local-best path.
1829 * However if bgp's add and zebra's add cross paths (race condition) it
1830 * is possible that the local path is no longer the "older" best path.
1831 * It is a path that was never designated as best and hence requires
1832 * additional handling to prevent bgp from injecting and holding on to a
1833 * non-best local path.
1835 static void evpn_cleanup_local_non_best_route(struct bgp
*bgp
,
1836 struct bgpevpn
*vpn
,
1837 struct bgp_dest
*dest
,
1838 struct bgp_path_info
*local_pi
)
1840 /* local path was not picked as the winner; kick it out */
1841 if (bgp_debug_zebra(NULL
))
1842 zlog_debug("evicting local evpn prefix %pRN as remote won",
1845 evpn_delete_old_local_route(bgp
, vpn
, dest
, local_pi
);
1846 bgp_path_info_reap(dest
, local_pi
);
1848 /* tell zebra to re-add the best remote path */
1849 evpn_zebra_reinstall_best_route(bgp
, vpn
, dest
);
1853 * Create or update EVPN route (of type based on prefix) for specified VNI
1854 * and schedule for processing.
1856 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1857 struct prefix_evpn
*p
, uint8_t flags
,
1860 struct bgp_dest
*dest
;
1862 struct attr
*attr_new
;
1863 int add_l3_ecomm
= 0;
1864 struct bgp_path_info
*pi
;
1865 afi_t afi
= AFI_L2VPN
;
1866 safi_t safi
= SAFI_EVPN
;
1869 memset(&attr
, 0, sizeof(struct attr
));
1871 /* Build path-attribute for this route. */
1872 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1873 attr
.nexthop
= vpn
->originator_ip
;
1874 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1875 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1876 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1877 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1878 attr
.router_flag
= CHECK_FLAG(flags
,
1879 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1881 /* PMSI is only needed for type-3 routes */
1882 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
1883 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1884 attr
.pmsi_tnl_type
= PMSI_TNLTYPE_INGR_REPL
;
1887 /* router mac is only needed for type-2 routes here. */
1888 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
1889 uint8_t af_flags
= 0;
1891 if (CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1892 SET_FLAG(af_flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
);
1894 bgp_evpn_get_rmac_nexthop(vpn
, p
, &attr
, af_flags
);
1896 if (bgp_debug_zebra(NULL
)) {
1897 char buf
[ETHER_ADDR_STRLEN
];
1898 char buf1
[PREFIX_STRLEN
];
1900 zlog_debug("VRF %s vni %u type-2 route evp %s RMAC %s nexthop %s",
1902 vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
) : " ",
1904 prefix2str(p
, buf1
, sizeof(buf1
)),
1905 prefix_mac2str(&attr
.rmac
, buf
,
1907 inet_ntoa(attr
.mp_nexthop_global_in
));
1911 vni2label(vpn
->vni
, &(attr
.label
));
1913 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1914 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1917 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1918 (is_evpn_prefix_ipaddr_v4(p
) ||
1919 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1920 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1921 bgpevpn_get_l3vni(vpn
))
1924 /* Set up extended community. */
1925 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1927 /* First, create (or fetch) route node within the VNI. */
1928 /* NOTE: There is no RD here. */
1929 dest
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1931 /* Create or update route entry. */
1932 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, dest
, &attr
,
1933 1, &pi
, flags
, seq
);
1935 attr_new
= pi
->attr
;
1937 /* lock ri to prevent freeing in evpn_route_select_install */
1938 bgp_path_info_lock(pi
);
1940 /* Perform route selection. Normally, the local route in the
1941 * VNI is expected to win and be the best route. However, if
1942 * there is a race condition where a host moved from local to
1943 * remote and the remote route was received in BGP just prior
1944 * to the local MACIP notification from zebra, the remote
1945 * route would win, and we should evict the defunct local route
1946 * and (re)install the remote route into zebra.
1948 evpn_route_select_install(bgp
, vpn
, dest
);
1950 * If the new local route was not selected evict it and tell zebra
1951 * to re-add the best remote dest. BGP doesn't retain non-best local
1954 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1956 evpn_cleanup_local_non_best_route(bgp
, vpn
, dest
, pi
);
1958 bgp_path_info_unlock(pi
);
1960 bgp_dest_unlock_node(dest
);
1962 /* If this is a new route or some attribute has changed, export the
1963 * route to the global table. The route will be advertised to peers
1964 * from there. Note that this table is a 2-level tree (RD-level +
1965 * Prefix-level) similar to L3VPN routes.
1968 struct bgp_path_info
*global_pi
;
1970 dest
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1971 (struct prefix
*)p
, &vpn
->prd
);
1972 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, dest
, attr_new
, 1,
1973 &global_pi
, flags
, seq
);
1975 /* Schedule for processing and unlock node. */
1976 bgp_process(bgp
, dest
, afi
, safi
);
1977 bgp_dest_unlock_node(dest
);
1980 /* Unintern temporary. */
1981 aspath_unintern(&attr
.aspath
);
1987 * Delete EVPN route entry.
1988 * The entry can be in ESI/VNI table or the global table.
1990 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1991 struct bgp_dest
*dest
,
1992 struct bgp_path_info
**pi
)
1994 struct bgp_path_info
*tmp_pi
;
1998 /* Now, find matching route. */
1999 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
2000 tmp_pi
= tmp_pi
->next
)
2001 if (tmp_pi
->peer
== bgp
->peer_self
2002 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2003 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2008 /* Mark route for delete. */
2010 bgp_path_info_delete(dest
, tmp_pi
);
2015 /* Delete EVPN ES (type-4) route */
2016 static int delete_evpn_type4_route(struct bgp
*bgp
,
2018 struct prefix_evpn
*p
)
2020 afi_t afi
= AFI_L2VPN
;
2021 safi_t safi
= SAFI_EVPN
;
2022 struct bgp_path_info
*pi
;
2023 struct bgp_dest
*dest
= NULL
; /* dest in esi table */
2024 struct bgp_dest
*global_dest
= NULL
; /* dest in global table */
2026 /* First, locate the route node within the ESI.
2027 * If it doesn't exist, ther is nothing to do.
2028 * Note: there is no RD here.
2030 dest
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2034 /* Next, locate route node in the global EVPN routing table.
2035 * Note that this table is a 2-level tree (RD-level + Prefix-level)
2037 global_dest
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
2038 (struct prefix
*)p
, &es
->prd
);
2041 /* Delete route entry in the global EVPN table. */
2042 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
2044 /* Schedule for processing - withdraws to peers happen from
2048 bgp_process(bgp
, global_dest
, afi
, safi
);
2049 bgp_dest_unlock_node(global_dest
);
2053 * Delete route entry in the ESI route table.
2054 * This can just be removed.
2056 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2058 bgp_path_info_reap(dest
, pi
);
2059 bgp_dest_unlock_node(dest
);
2063 /* Delete EVPN type5 route */
2064 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
2066 afi_t afi
= AFI_L2VPN
;
2067 safi_t safi
= SAFI_EVPN
;
2068 struct bgp_dest
*dest
= NULL
;
2069 struct bgp_path_info
*pi
= NULL
;
2070 struct bgp
*bgp_evpn
= NULL
; /* evpn bgp instance */
2072 bgp_evpn
= bgp_get_evpn();
2076 /* locate the global route entry for this type-5 prefix */
2077 dest
= bgp_afi_node_lookup(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
2078 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
2082 delete_evpn_route_entry(bgp_evpn
, afi
, safi
, dest
, &pi
);
2084 bgp_process(bgp_evpn
, dest
, afi
, safi
);
2085 bgp_dest_unlock_node(dest
);
2090 * Delete EVPN route (of type based on prefix) for specified VNI and
2091 * schedule for processing.
2093 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2094 struct prefix_evpn
*p
)
2096 struct bgp_dest
*dest
, *global_dest
;
2097 struct bgp_path_info
*pi
;
2098 afi_t afi
= AFI_L2VPN
;
2099 safi_t safi
= SAFI_EVPN
;
2101 /* First, locate the route node within the VNI. If it doesn't exist,
2103 * is nothing further to do.
2105 /* NOTE: There is no RD here. */
2106 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2110 /* Next, locate route node in the global EVPN routing table. Note that
2111 * this table is a 2-level tree (RD-level + Prefix-level) similar to
2114 global_dest
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
2115 (struct prefix
*)p
, &vpn
->prd
);
2117 /* Delete route entry in the global EVPN table. */
2118 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
2120 /* Schedule for processing - withdraws to peers happen from
2124 bgp_process(bgp
, global_dest
, afi
, safi
);
2125 bgp_dest_unlock_node(global_dest
);
2128 /* Delete route entry in the VNI route table. This can just be removed.
2130 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2132 bgp_path_info_reap(dest
, pi
);
2133 evpn_route_select_install(bgp
, vpn
, dest
);
2135 bgp_dest_unlock_node(dest
);
2141 * Update all type-2 (MACIP) local routes for this VNI - these should also
2142 * be scheduled for advertise to peers.
2144 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2148 struct bgp_dest
*dest
;
2149 struct bgp_path_info
*pi
, *tmp_pi
;
2151 struct attr
*attr_new
;
2153 int add_l3_ecomm
= 0;
2158 /* Walk this VNI's route table and update local type-2 routes. For any
2159 * routes updated, update corresponding entry in the global table too.
2161 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2162 dest
= bgp_route_next(dest
)) {
2163 const struct prefix_evpn
*evp
=
2164 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
2165 struct bgp_dest
*rd_dest
;
2166 struct bgp_path_info
*global_pi
;
2168 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2171 /* Identify local route. */
2172 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
2173 tmp_pi
= tmp_pi
->next
) {
2174 if (tmp_pi
->peer
== bgp
->peer_self
2175 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2176 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2184 * Build attribute per local route as the MAC mobility and
2185 * some other values could differ for different routes. The
2186 * attributes will be shared in the hash table.
2188 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
2189 attr
.nexthop
= vpn
->originator_ip
;
2190 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
2191 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
2192 bgp_evpn_get_rmac_nexthop(vpn
, evp
, &attr
,
2193 tmp_pi
->extra
->af_flags
);
2195 if (evpn_route_is_sticky(bgp
, dest
))
2197 else if (evpn_route_is_def_gw(bgp
, dest
)) {
2198 attr
.default_gw
= 1;
2199 if (is_evpn_prefix_ipaddr_v6(evp
))
2200 attr
.router_flag
= 1;
2203 if (bgp_debug_zebra(NULL
)) {
2204 char buf
[ETHER_ADDR_STRLEN
];
2205 char buf1
[PREFIX_STRLEN
];
2207 zlog_debug("VRF %s vni %u evp %s RMAC %s nexthop %s",
2209 vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
) : " ",
2211 prefix2str(evp
, buf1
, sizeof(buf1
)),
2212 prefix_mac2str(&attr
.rmac
, buf
, sizeof(buf
)),
2213 inet_ntoa(attr
.mp_nexthop_global_in
));
2216 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2217 * using L3 VNI for type-2 routes also.
2219 if ((is_evpn_prefix_ipaddr_v4(evp
) ||
2220 !IN6_IS_ADDR_LINKLOCAL(
2221 &evp
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
2222 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
2223 bgpevpn_get_l3vni(vpn
))
2226 /* Set up extended community. */
2227 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2229 seq
= mac_mobility_seqnum(tmp_pi
->attr
);
2231 /* Update the route entry. */
2232 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, dest
, &attr
, 0,
2235 /* lock ri to prevent freeing in evpn_route_select_install */
2236 bgp_path_info_lock(pi
);
2238 /* Perform route selection. Normally, the local route in the
2239 * VNI is expected to win and be the best route. However,
2240 * under peculiar situations (e.g., tunnel (next hop) IP change
2241 * that causes best selection to be based on next hop), a
2242 * remote route could win. If the local route is the best,
2243 * ensure it is updated in the global EVPN route table and
2244 * advertised to peers; otherwise, ensure it is evicted and
2245 * (re)install the remote route into zebra.
2247 evpn_route_select_install(bgp
, vpn
, dest
);
2248 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
2249 evpn_cleanup_local_non_best_route(bgp
, vpn
, dest
, pi
);
2251 bgp_path_info_unlock(pi
);
2253 attr_new
= pi
->attr
;
2255 bgp_path_info_unlock(pi
);
2257 /* Update route in global routing table. */
2258 rd_dest
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
,
2259 safi
, (struct prefix
*)evp
,
2262 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_dest
,
2263 attr_new
, 0, &global_pi
, 0,
2264 mac_mobility_seqnum(attr_new
));
2266 /* Schedule for processing and unlock node. */
2267 bgp_process(bgp
, rd_dest
, afi
, safi
);
2268 bgp_dest_unlock_node(rd_dest
);
2271 /* Unintern temporary. */
2272 aspath_unintern(&attr
.aspath
);
2279 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2280 * global routing table. These are also scheduled for withdraw from peers.
2282 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2286 struct bgp_dest
*rddest
, *dest
;
2287 struct bgp_table
*table
;
2288 struct bgp_path_info
*pi
;
2293 rddest
= bgp_node_lookup(bgp
->rib
[afi
][safi
],
2294 (struct prefix
*)&vpn
->prd
);
2295 if (rddest
&& bgp_dest_has_bgp_path_info_data(rddest
)) {
2296 table
= bgp_dest_get_bgp_table_info(rddest
);
2297 for (dest
= bgp_table_top(table
); dest
;
2298 dest
= bgp_route_next(dest
)) {
2299 const struct prefix_evpn
*evp
=
2300 (const struct prefix_evpn
*)bgp_dest_get_prefix(
2303 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2306 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2308 bgp_process(bgp
, dest
, afi
, safi
);
2312 /* Unlock RD node. */
2314 bgp_dest_unlock_node(rddest
);
2320 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2321 * table as well as the per-VNI route table.
2323 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2327 struct bgp_dest
*dest
;
2328 struct bgp_path_info
*pi
;
2333 /* First, walk the global route table for this VNI's type-2 local
2335 * EVPN routes are a 2-level table, first get the RD table.
2337 delete_global_type2_routes(bgp
, vpn
);
2339 /* Next, walk this VNI's route table and delete local type-2 routes. */
2340 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2341 dest
= bgp_route_next(dest
)) {
2342 const struct prefix_evpn
*evp
=
2343 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
2345 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2348 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2350 /* Route entry in local table gets deleted immediately. */
2352 bgp_path_info_reap(dest
, pi
);
2359 * Delete all routes in per ES route-table
2361 static int delete_all_es_routes(struct bgp
*bgp
, struct evpnes
*es
)
2363 struct bgp_dest
*dest
;
2364 struct bgp_path_info
*pi
, *nextpi
;
2366 /* Walk this ES's route table and delete all routes. */
2367 for (dest
= bgp_table_top(es
->route_table
); dest
;
2368 dest
= bgp_route_next(dest
)) {
2369 for (pi
= bgp_dest_get_bgp_path_info(dest
);
2370 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2371 bgp_path_info_delete(dest
, pi
);
2372 bgp_path_info_reap(dest
, pi
);
2380 * Delete all routes in the per-VNI route table.
2382 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2384 struct bgp_dest
*dest
;
2385 struct bgp_path_info
*pi
, *nextpi
;
2387 /* Walk this VNI's route table and delete all routes. */
2388 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2389 dest
= bgp_route_next(dest
)) {
2390 for (pi
= bgp_dest_get_bgp_path_info(dest
);
2391 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2392 bgp_path_info_delete(dest
, pi
);
2393 bgp_path_info_reap(dest
, pi
);
2400 /* BUM traffic flood mode per-l2-vni */
2401 static int bgp_evpn_vni_flood_mode_get(struct bgp
*bgp
,
2402 struct bgpevpn
*vpn
)
2404 /* if flooding has been globally disabled per-vni mode is
2407 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
2408 return VXLAN_FLOOD_DISABLED
;
2410 /* if mcast group ip has been specified we use a PIM-SM MDT */
2411 if (vpn
->mcast_grp
.s_addr
!= INADDR_ANY
)
2412 return VXLAN_FLOOD_PIM_SM
;
2414 /* default is ingress replication */
2415 return VXLAN_FLOOD_HEAD_END_REPL
;
2419 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2420 * export RT getting modified or change to tunnel IP. Note that these
2421 * situations need the route in the per-VNI table as well as the global
2422 * table to be updated (as attributes change).
2424 int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2427 struct prefix_evpn p
;
2429 /* Update and advertise the type-3 route (only one) followed by the
2430 * locally learnt type-2 routes (MACIP) - for this VNI.
2432 * RT-3 only if doing head-end replication
2434 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
2435 == VXLAN_FLOOD_HEAD_END_REPL
) {
2436 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2437 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0);
2442 return update_all_type2_routes(bgp
, vpn
);
2445 /* Delete (and withdraw) local routes for specified ES from global and ES table.
2446 * Also remove all other routes from the per ES table.
2447 * Invoked when ES is deleted.
2449 static int delete_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
2452 char buf
[ESI_STR_LEN
];
2453 struct prefix_evpn p
;
2455 /* Delete and withdraw locally learnt ES route */
2456 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
.ipaddr_v4
);
2457 ret
= delete_evpn_type4_route(bgp
, es
, &p
);
2459 flog_err(EC_BGP_EVPN_ROUTE_DELETE
,
2460 "%u failed to delete type-4 route for ESI %s",
2461 bgp
->vrf_id
, esi_to_str(&es
->esi
, buf
, sizeof(buf
)));
2464 /* Delete all routes from per ES table */
2465 return delete_all_es_routes(bgp
, es
);
2469 * Delete (and withdraw) local routes for specified VNI from the global
2470 * table and per-VNI table. After this, remove all other routes from
2471 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2472 * (advertise-all-vni) being disabled.
2474 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2477 struct prefix_evpn p
;
2479 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2480 * followed by type-3 routes (only one) - for this VNI.
2482 ret
= delete_all_type2_routes(bgp
, vpn
);
2486 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2487 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2491 /* Delete all routes from the per-VNI table. */
2492 return delete_all_vni_routes(bgp
, vpn
);
2496 * There is a flood mcast IP address change. Update the mcast-grp and
2497 * remove the type-3 route if any. A new type-3 route will be generated
2498 * post tunnel_ip update if the new flood mode is head-end-replication.
2500 static int bgp_evpn_mcast_grp_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2501 struct in_addr mcast_grp
)
2503 struct prefix_evpn p
;
2505 vpn
->mcast_grp
= mcast_grp
;
2507 if (is_vni_live(vpn
)) {
2508 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2509 delete_evpn_route(bgp
, vpn
, &p
);
2516 * There is a tunnel endpoint IP address change for this VNI, delete
2517 * prior type-3 route (if needed) and update.
2518 * Note: Route re-advertisement happens elsewhere after other processing
2521 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2522 struct in_addr originator_ip
)
2524 struct prefix_evpn p
;
2526 /* If VNI is not live, we only need to update the originator ip */
2527 if (!is_vni_live(vpn
)) {
2528 vpn
->originator_ip
= originator_ip
;
2532 /* Update the tunnel-ip hash */
2533 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2534 bgp_tip_add(bgp
, &originator_ip
);
2536 /* filter routes as martian nexthop db has changed */
2537 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2539 /* Need to withdraw type-3 route as the originator IP is part
2542 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2543 delete_evpn_route(bgp
, vpn
, &p
);
2545 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2546 vpn
->originator_ip
= originator_ip
;
2550 static struct bgp_path_info
*
2551 bgp_create_evpn_bgp_path_info(struct bgp_path_info
*parent_pi
,
2552 struct bgp_dest
*dest
, struct attr
*attr
)
2554 struct attr
*attr_new
;
2555 struct bgp_path_info
*pi
;
2557 /* Add (or update) attribute to hash. */
2558 attr_new
= bgp_attr_intern(attr
);
2560 /* Create new route with its attribute. */
2561 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0, parent_pi
->peer
,
2563 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2564 bgp_path_info_extra_get(pi
);
2565 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2566 bgp_dest_lock_node((struct bgp_dest
*)parent_pi
->net
);
2567 if (parent_pi
->extra
) {
2568 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2569 sizeof(pi
->extra
->label
));
2570 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2572 bgp_path_info_add(dest
, pi
);
2577 /* Install EVPN route entry in ES */
2578 static int install_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2579 const struct prefix_evpn
*p
,
2580 struct bgp_path_info
*parent_pi
)
2583 struct bgp_dest
*dest
= NULL
;
2584 struct bgp_path_info
*pi
= NULL
;
2585 struct attr
*attr_new
= NULL
;
2587 /* Create (or fetch) route within the VNI.
2588 * NOTE: There is no RD here.
2590 dest
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
2592 /* Check if route entry is already present. */
2593 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2595 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2599 /* Add (or update) attribute to hash. */
2600 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2602 /* Create new route with its attribute. */
2603 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2604 parent_pi
->peer
, attr_new
, dest
);
2605 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2606 bgp_path_info_extra_get(pi
);
2607 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2608 bgp_dest_lock_node((struct bgp_dest
*)parent_pi
->net
);
2609 bgp_path_info_add(dest
, pi
);
2611 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2612 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2613 bgp_dest_unlock_node(dest
);
2616 /* The attribute has changed. */
2617 /* Add (or update) attribute to hash. */
2618 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2620 /* Restore route, if needed. */
2621 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2622 bgp_path_info_restore(dest
, pi
);
2624 /* Mark if nexthop has changed. */
2625 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2626 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2628 /* Unintern existing, set to new. */
2629 bgp_attr_unintern(&pi
->attr
);
2630 pi
->attr
= attr_new
;
2631 pi
->uptime
= bgp_clock();
2634 /* Perform route selection and update zebra, if required. */
2635 ret
= evpn_es_route_select_install(bgp
, es
, dest
);
2640 * Install route entry into the VRF routing table and invoke route selection.
2642 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2643 const struct prefix_evpn
*evp
,
2644 struct bgp_path_info
*parent_pi
)
2646 struct bgp_dest
*dest
;
2647 struct bgp_path_info
*pi
;
2649 struct attr
*attr_new
;
2652 struct prefix
*pp
= &p
;
2655 char buf
[PREFIX_STRLEN
];
2656 bool new_pi
= false;
2658 memset(pp
, 0, sizeof(struct prefix
));
2659 ip_prefix_from_evpn_prefix(evp
, pp
);
2661 if (bgp_debug_zebra(NULL
)) {
2663 "vrf %s: import evpn prefix %s parent %p flags 0x%x",
2664 vrf_id_to_name(bgp_vrf
->vrf_id
),
2665 prefix2str(evp
, buf
, sizeof(buf
)),
2666 parent_pi
, parent_pi
->flags
);
2669 /* Create (or fetch) route within the VRF. */
2670 /* NOTE: There is no RD here. */
2671 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2673 safi
= SAFI_UNICAST
;
2674 dest
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2675 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2677 safi
= SAFI_UNICAST
;
2678 dest
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2682 /* EVPN routes currently only support a IPv4 next hop which corresponds
2683 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2684 * or prefix route, we have to convert the next hop to an IPv4-mapped
2685 * address for the rest of the code to flow through. In the case of IPv4,
2686 * make sure to set the flag for next hop attribute.
2688 attr
= *parent_pi
->attr
;
2690 evpn_convert_nexthop_to_ipv6(&attr
);
2692 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2694 /* Check if route entry is already present. */
2695 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2697 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2701 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, dest
, &attr
);
2704 if (attrhash_cmp(pi
->attr
, &attr
)
2705 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2706 bgp_dest_unlock_node(dest
);
2709 /* The attribute has changed. */
2710 /* Add (or update) attribute to hash. */
2711 attr_new
= bgp_attr_intern(&attr
);
2713 /* Restore route, if needed. */
2714 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2715 bgp_path_info_restore(dest
, pi
);
2717 /* Mark if nexthop has changed. */
2719 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2721 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2722 &attr_new
->mp_nexthop_global
)))
2723 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2725 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ATTR_CHANGED
);
2726 /* Unintern existing, set to new. */
2727 bgp_attr_unintern(&pi
->attr
);
2728 pi
->attr
= attr_new
;
2729 pi
->uptime
= bgp_clock();
2731 /* as it is an importation, change nexthop */
2732 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ANNC_NH_SELF
);
2734 bgp_aggregate_increment(bgp_vrf
, bgp_dest_get_prefix(dest
), pi
, afi
,
2737 /* Perform route selection and update zebra, if required. */
2738 bgp_process(bgp_vrf
, dest
, afi
, safi
);
2740 /* Process for route leaking. */
2741 vpn_leak_from_vrf_update(bgp_get_default(), bgp_vrf
, pi
);
2743 bgp_dest_unlock_node(dest
);
2745 if (bgp_debug_zebra(NULL
))
2747 "... %s pi dest %p (l %d) pi %p (l %d, f 0x%x)",
2748 new_pi
? "new" : "update",
2749 dest
, bgp_dest_to_rnode(dest
)->lock
,
2750 pi
, pi
->lock
, pi
->flags
);
2756 * Install route entry into the VNI routing table and invoke route selection.
2758 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2759 const struct prefix_evpn
*p
,
2760 struct bgp_path_info
*parent_pi
)
2762 struct bgp_dest
*dest
;
2763 struct bgp_path_info
*pi
;
2764 struct attr
*attr_new
;
2767 /* Create (or fetch) route within the VNI. */
2768 /* NOTE: There is no RD here. */
2769 dest
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2771 /* Check if route entry is already present. */
2772 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2774 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2778 /* Create an info */
2779 (void)bgp_create_evpn_bgp_path_info(parent_pi
, dest
,
2782 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2783 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2784 bgp_dest_unlock_node(dest
);
2787 /* The attribute has changed. */
2788 /* Add (or update) attribute to hash. */
2789 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2791 /* Restore route, if needed. */
2792 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2793 bgp_path_info_restore(dest
, pi
);
2795 /* Mark if nexthop has changed. */
2796 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2797 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2799 /* Unintern existing, set to new. */
2800 bgp_attr_unintern(&pi
->attr
);
2801 pi
->attr
= attr_new
;
2802 pi
->uptime
= bgp_clock();
2805 /* Perform route selection and update zebra, if required. */
2806 ret
= evpn_route_select_install(bgp
, vpn
, dest
);
2808 bgp_dest_unlock_node(dest
);
2813 /* Uninstall EVPN route entry from ES route table */
2814 static int uninstall_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2815 const struct prefix_evpn
*p
,
2816 struct bgp_path_info
*parent_pi
)
2819 struct bgp_dest
*dest
;
2820 struct bgp_path_info
*pi
;
2822 if (!es
->route_table
)
2825 /* Locate route within the ESI.
2826 * NOTE: There is no RD here.
2828 dest
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2832 /* Find matching route entry. */
2833 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2835 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2841 /* Mark entry for deletion */
2842 bgp_path_info_delete(dest
, pi
);
2844 /* Perform route selection and update zebra, if required. */
2845 ret
= evpn_es_route_select_install(bgp
, es
, dest
);
2847 /* Unlock route node. */
2848 bgp_dest_unlock_node(dest
);
2854 * Uninstall route entry from the VRF routing table and send message
2855 * to zebra, if appropriate.
2857 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2858 const struct prefix_evpn
*evp
,
2859 struct bgp_path_info
*parent_pi
)
2861 struct bgp_dest
*dest
;
2862 struct bgp_path_info
*pi
;
2865 struct prefix
*pp
= &p
;
2868 char buf
[PREFIX_STRLEN
];
2870 memset(pp
, 0, sizeof(struct prefix
));
2871 ip_prefix_from_evpn_prefix(evp
, pp
);
2873 if (bgp_debug_zebra(NULL
)) {
2875 "vrf %s: unimport evpn prefix %s parent %p flags 0x%x",
2876 vrf_id_to_name(bgp_vrf
->vrf_id
),
2877 prefix2str(evp
, buf
, sizeof(buf
)),
2878 parent_pi
, parent_pi
->flags
);
2881 /* Locate route within the VRF. */
2882 /* NOTE: There is no RD here. */
2883 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2885 safi
= SAFI_UNICAST
;
2886 dest
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2889 safi
= SAFI_UNICAST
;
2890 dest
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2896 /* Find matching route entry. */
2897 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2899 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2905 if (bgp_debug_zebra(NULL
))
2907 "... delete dest %p (l %d) pi %p (l %d, f 0x%x)",
2908 dest
, bgp_dest_to_rnode(dest
)->lock
,
2909 pi
, pi
->lock
, pi
->flags
);
2911 /* Process for route leaking. */
2912 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp_vrf
, pi
);
2914 bgp_aggregate_decrement(bgp_vrf
, bgp_dest_get_prefix(dest
), pi
, afi
,
2917 /* Mark entry for deletion */
2918 bgp_path_info_delete(dest
, pi
);
2920 /* Perform route selection and update zebra, if required. */
2921 bgp_process(bgp_vrf
, dest
, afi
, safi
);
2923 /* Unlock route node. */
2924 bgp_dest_unlock_node(dest
);
2930 * Uninstall route entry from the VNI routing table and send message
2931 * to zebra, if appropriate.
2933 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2934 const struct prefix_evpn
*p
,
2935 struct bgp_path_info
*parent_pi
)
2937 struct bgp_dest
*dest
;
2938 struct bgp_path_info
*pi
;
2941 /* Locate route within the VNI. */
2942 /* NOTE: There is no RD here. */
2943 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2947 /* Find matching route entry. */
2948 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2950 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2956 /* Mark entry for deletion */
2957 bgp_path_info_delete(dest
, pi
);
2959 /* Perform route selection and update zebra, if required. */
2960 ret
= evpn_route_select_install(bgp
, vpn
, dest
);
2962 /* Unlock route node. */
2963 bgp_dest_unlock_node(dest
);
2969 * Given a prefix, see if it belongs to ES.
2971 static int is_prefix_matching_for_es(const struct prefix_evpn
*p
,
2974 /* if not an ES route return false */
2975 if (p
->prefix
.route_type
!= BGP_EVPN_ES_ROUTE
)
2978 if (memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
)) == 0)
2985 * Given a route entry and a VRF, see if this route entry should be
2986 * imported into the VRF i.e., RTs match.
2988 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2989 struct bgp_path_info
*pi
)
2991 struct attr
*attr
= pi
->attr
;
2992 struct ecommunity
*ecom
;
2996 /* Route should have valid RT to be even considered. */
2997 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3000 ecom
= attr
->ecommunity
;
3001 if (!ecom
|| !ecom
->size
)
3004 /* For each extended community RT, see if it matches this VNI. If any RT
3005 * matches, we're done.
3007 for (i
= 0; i
< ecom
->size
; i
++) {
3009 uint8_t type
, sub_type
;
3010 struct ecommunity_val
*eval
;
3011 struct ecommunity_val eval_tmp
;
3012 struct vrf_irt_node
*irt
;
3014 /* Only deal with RTs */
3015 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3016 eval
= (struct ecommunity_val
*)(ecom
->val
3017 + (i
* ECOMMUNITY_SIZE
));
3020 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3023 /* See if this RT matches specified VNIs import RTs */
3024 irt
= lookup_vrf_import_rt(eval
);
3026 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
3029 /* Also check for non-exact match. In this, we mask out the AS
3031 * only check on the local-admin sub-field. This is to
3033 * VNI as the RT for EBGP peering too.
3036 if (type
== ECOMMUNITY_ENCODE_AS
3037 || type
== ECOMMUNITY_ENCODE_AS4
3038 || type
== ECOMMUNITY_ENCODE_IP
) {
3039 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3040 mask_ecom_global_admin(&eval_tmp
, eval
);
3041 irt
= lookup_vrf_import_rt(&eval_tmp
);
3044 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
3052 * Given a route entry and a VNI, see if this route entry should be
3053 * imported into the VNI i.e., RTs match.
3055 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
3056 struct bgp_path_info
*pi
)
3058 struct attr
*attr
= pi
->attr
;
3059 struct ecommunity
*ecom
;
3063 /* Route should have valid RT to be even considered. */
3064 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3067 ecom
= attr
->ecommunity
;
3068 if (!ecom
|| !ecom
->size
)
3071 /* For each extended community RT, see if it matches this VNI. If any RT
3072 * matches, we're done.
3074 for (i
= 0; i
< ecom
->size
; i
++) {
3076 uint8_t type
, sub_type
;
3077 struct ecommunity_val
*eval
;
3078 struct ecommunity_val eval_tmp
;
3079 struct irt_node
*irt
;
3081 /* Only deal with RTs */
3082 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3083 eval
= (struct ecommunity_val
*)(ecom
->val
3084 + (i
* ECOMMUNITY_SIZE
));
3087 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3090 /* See if this RT matches specified VNIs import RTs */
3091 irt
= lookup_import_rt(bgp
, eval
);
3093 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
3096 /* Also check for non-exact match. In this, we mask out the AS
3098 * only check on the local-admin sub-field. This is to
3100 * VNI as the RT for EBGP peering too.
3103 if (type
== ECOMMUNITY_ENCODE_AS
3104 || type
== ECOMMUNITY_ENCODE_AS4
3105 || type
== ECOMMUNITY_ENCODE_IP
) {
3106 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3107 mask_ecom_global_admin(&eval_tmp
, eval
);
3108 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3111 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
3118 static int install_uninstall_routes_for_es(struct bgp
*bgp
,
3125 char buf
[PREFIX_STRLEN
];
3126 char buf1
[ESI_STR_LEN
];
3127 struct bgp_dest
*rd_dest
, *dest
;
3128 struct bgp_table
*table
;
3129 struct bgp_path_info
*pi
;
3135 * Walk entire global routing table and evaluate routes which could be
3136 * imported into this VRF. Note that we need to loop through all global
3137 * routes to determine which route matches the import rt on vrf
3139 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
3140 rd_dest
= bgp_route_next(rd_dest
)) {
3141 table
= bgp_dest_get_bgp_table_info(rd_dest
);
3145 for (dest
= bgp_table_top(table
); dest
;
3146 dest
= bgp_route_next(dest
)) {
3147 const struct prefix_evpn
*evp
=
3148 (const struct prefix_evpn
*)bgp_dest_get_prefix(
3151 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
3154 * Consider "valid" remote routes applicable for
3157 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3158 && pi
->type
== ZEBRA_ROUTE_BGP
3159 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3162 if (!is_prefix_matching_for_es(evp
, es
))
3166 ret
= install_evpn_route_entry_in_es(
3169 ret
= uninstall_evpn_route_entry_in_es(
3175 "Failed to %s EVPN %s route in ESI %s",
3178 prefix2str(evp
, buf
,
3180 esi_to_str(&es
->esi
, buf1
,
3190 /* This API will scan evpn routes for checking attribute's rmac
3191 * macthes with bgp instance router mac. It avoid installing
3192 * route into bgp vrf table and remote rmac in bridge table.
3194 static int bgp_evpn_route_rmac_self_check(struct bgp
*bgp_vrf
,
3195 const struct prefix_evpn
*evp
,
3196 struct bgp_path_info
*pi
)
3198 /* evpn route could have learnt prior to L3vni has come up,
3199 * perform rmac check before installing route and
3200 * remote router mac.
3201 * The route will be removed from global bgp table once
3202 * SVI comes up with MAC and stored in hash, triggers
3203 * bgp_mac_rescan_all_evpn_tables.
3205 if (memcmp(&bgp_vrf
->rmac
, &pi
->attr
->rmac
, ETH_ALEN
) == 0) {
3206 if (bgp_debug_update(pi
->peer
, NULL
, NULL
, 1)) {
3207 char buf1
[PREFIX_STRLEN
];
3208 char attr_str
[BUFSIZ
] = {0};
3210 bgp_dump_attr(pi
->attr
, attr_str
, BUFSIZ
);
3212 zlog_debug("%s: bgp %u prefix %s with attr %s - DENIED due to self mac",
3213 __func__
, bgp_vrf
->vrf_id
,
3214 prefix2str(evp
, buf1
, sizeof(buf1
)),
3225 * Install or uninstall mac-ip routes are appropriate for this
3228 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
3232 struct bgp_dest
*rd_dest
, *dest
;
3233 struct bgp_table
*table
;
3234 struct bgp_path_info
*pi
;
3236 char buf
[PREFIX_STRLEN
];
3237 struct bgp
*bgp_evpn
= NULL
;
3241 bgp_evpn
= bgp_get_evpn();
3245 /* Walk entire global routing table and evaluate routes which could be
3246 * imported into this VRF. Note that we need to loop through all global
3247 * routes to determine which route matches the import rt on vrf
3249 for (rd_dest
= bgp_table_top(bgp_evpn
->rib
[afi
][safi
]); rd_dest
;
3250 rd_dest
= bgp_route_next(rd_dest
)) {
3251 table
= bgp_dest_get_bgp_table_info(rd_dest
);
3255 for (dest
= bgp_table_top(table
); dest
;
3256 dest
= bgp_route_next(dest
)) {
3257 const struct prefix_evpn
*evp
=
3258 (const struct prefix_evpn
*)bgp_dest_get_prefix(
3261 /* if not mac-ip route skip this route */
3262 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3263 || evp
->prefix
.route_type
3264 == BGP_EVPN_IP_PREFIX_ROUTE
))
3267 /* if not a mac+ip route skip this route */
3268 if (!(is_evpn_prefix_ipaddr_v4(evp
)
3269 || is_evpn_prefix_ipaddr_v6(evp
)))
3272 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
3274 /* Consider "valid" remote routes applicable for
3277 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3278 && pi
->type
== ZEBRA_ROUTE_BGP
3279 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3282 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
3283 if (bgp_evpn_route_rmac_self_check(
3288 ret
= install_evpn_route_entry_in_vrf(
3291 ret
= uninstall_evpn_route_entry_in_vrf(
3297 "Failed to %s EVPN %s route in VRF %s",
3300 prefix2str(evp
, buf
,
3315 * Install or uninstall routes of specified type that are appropriate for this
3318 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
3319 struct bgpevpn
*vpn
,
3320 bgp_evpn_route_type rtype
,
3325 struct bgp_dest
*rd_dest
, *dest
;
3326 struct bgp_table
*table
;
3327 struct bgp_path_info
*pi
;
3333 /* Walk entire global routing table and evaluate routes which could be
3334 * imported into this VPN. Note that we cannot just look at the routes
3336 * the VNI's RD - remote routes applicable for this VNI could have any
3339 /* EVPN routes are a 2-level table. */
3340 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
3341 rd_dest
= bgp_route_next(rd_dest
)) {
3342 table
= bgp_dest_get_bgp_table_info(rd_dest
);
3346 for (dest
= bgp_table_top(table
); dest
;
3347 dest
= bgp_route_next(dest
)) {
3348 const struct prefix_evpn
*evp
=
3349 (const struct prefix_evpn
*)bgp_dest_get_prefix(
3352 if (evp
->prefix
.route_type
!= rtype
)
3355 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
3357 /* Consider "valid" remote routes applicable for
3359 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3360 && pi
->type
== ZEBRA_ROUTE_BGP
3361 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3364 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3366 ret
= install_evpn_route_entry(
3369 ret
= uninstall_evpn_route_entry(
3375 "%u: Failed to %s EVPN %s route in VNI %u",
3379 rtype
== BGP_EVPN_MAC_IP_ROUTE
3393 /* Install any existing remote ES routes applicable for this ES into its routing
3394 * table. This is invoked when ES comes up.
3396 static int install_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
3398 return install_uninstall_routes_for_es(bgp
, es
, 1);
3402 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3403 * is invoked upon l3vni-add or l3vni import rt change
3405 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3407 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3412 * Install any existing remote routes applicable for this VNI into its
3413 * routing table. This is invoked when a VNI becomes "live" or its Import
3416 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3420 /* Install type-3 routes followed by type-2 routes - the ones applicable
3423 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3428 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3432 /* uninstall routes from l3vni vrf. */
3433 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3435 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3440 * Uninstall any existing remote routes for this VNI. One scenario in which
3441 * this is invoked is upon an import RT change.
3443 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3447 /* Uninstall type-2 routes followed by type-3 routes - the ones
3451 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3456 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3460 /* Install or unistall route in ES */
3461 static int install_uninstall_route_in_es(struct bgp
*bgp
, struct evpnes
*es
,
3462 afi_t afi
, safi_t safi
,
3463 struct prefix_evpn
*evp
,
3464 struct bgp_path_info
*pi
, int install
)
3467 char buf
[ESI_STR_LEN
];
3470 ret
= install_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3472 ret
= uninstall_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3477 "%u: Failed to %s EVPN %s route in ESI %s", bgp
->vrf_id
,
3478 install
? "install" : "uninstall", "ES",
3479 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)));
3486 * Install or uninstall route in matching VRFs (list).
3488 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3489 safi_t safi
, struct prefix_evpn
*evp
,
3490 struct bgp_path_info
*pi
,
3491 struct list
*vrfs
, int install
)
3493 char buf
[PREFIX2STR_BUFFER
];
3494 struct bgp
*bgp_vrf
;
3495 struct listnode
*node
, *nnode
;
3497 /* Only type-2/type-5 routes go into a VRF */
3498 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3499 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3502 /* if it is type-2 route and not a mac+ip route skip this route */
3503 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3504 && !(is_evpn_prefix_ipaddr_v4(evp
)
3505 || is_evpn_prefix_ipaddr_v6(evp
)))
3508 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3512 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3514 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3518 flog_err(EC_BGP_EVPN_FAIL
,
3519 "%u: Failed to %s prefix %s in VRF %s",
3521 install
? "install" : "uninstall",
3522 prefix2str(evp
, buf
, sizeof(buf
)),
3523 vrf_id_to_name(bgp_vrf
->vrf_id
));
3532 * Install or uninstall route in matching VNIs (list).
3534 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3535 safi_t safi
, struct prefix_evpn
*evp
,
3536 struct bgp_path_info
*pi
,
3537 struct list
*vnis
, int install
)
3539 struct bgpevpn
*vpn
;
3540 struct listnode
*node
, *nnode
;
3542 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3545 if (!is_vni_live(vpn
))
3549 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3551 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3554 flog_err(EC_BGP_EVPN_FAIL
,
3555 "%u: Failed to %s EVPN %s route in VNI %u",
3556 bgp
->vrf_id
, install
? "install" : "uninstall",
3557 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3569 * Install or uninstall route for appropriate VNIs/ESIs.
3571 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3572 const struct prefix
*p
,
3573 struct bgp_path_info
*pi
, int import
)
3575 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3576 struct attr
*attr
= pi
->attr
;
3577 struct ecommunity
*ecom
;
3582 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3583 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3584 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3585 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3586 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3589 /* If we don't have Route Target, nothing much to do. */
3590 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3593 ecom
= attr
->ecommunity
;
3594 if (!ecom
|| !ecom
->size
)
3597 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3598 * attached to the route */
3599 for (i
= 0; i
< ecom
->size
; i
++) {
3601 uint8_t type
, sub_type
;
3602 struct ecommunity_val
*eval
;
3603 struct ecommunity_val eval_tmp
;
3604 struct irt_node
*irt
; /* import rt for l2vni */
3605 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3608 /* Only deal with RTs */
3609 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3610 eval
= (struct ecommunity_val
*)(ecom
->val
3611 + (i
* ECOMMUNITY_SIZE
));
3614 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3618 * macip routes (type-2) are imported into VNI and VRF tables.
3619 * IMET route is imported into VNI table.
3620 * prefix routes are imported into VRF table.
3622 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3623 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3624 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3626 irt
= lookup_import_rt(bgp
, eval
);
3628 install_uninstall_route_in_vnis(
3629 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3632 vrf_irt
= lookup_vrf_import_rt(eval
);
3634 install_uninstall_route_in_vrfs(
3635 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3638 /* Also check for non-exact match.
3639 * In this, we mask out the AS and
3640 * only check on the local-admin sub-field.
3641 * This is to facilitate using
3642 * VNI as the RT for EBGP peering too.
3646 if (type
== ECOMMUNITY_ENCODE_AS
3647 || type
== ECOMMUNITY_ENCODE_AS4
3648 || type
== ECOMMUNITY_ENCODE_IP
) {
3649 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3650 mask_ecom_global_admin(&eval_tmp
, eval
);
3651 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3652 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3656 install_uninstall_route_in_vnis(
3657 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3660 install_uninstall_route_in_vrfs(
3661 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3665 /* es route is imported into the es table */
3666 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3668 /* we will match based on the entire esi to avoid
3669 * imoort of an es route for esi2 into esi1
3671 es
= bgp_evpn_lookup_es(bgp
, &evp
->prefix
.es_addr
.esi
);
3672 if (es
&& is_es_local(es
))
3673 install_uninstall_route_in_es(
3674 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3682 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3685 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3687 /* Delete ipv4 default route and withdraw from peers */
3688 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3689 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3690 SAFI_UNICAST
, false);
3692 /* delete all ipv4 routes and withdraw from peers */
3693 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3694 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3696 /* Delete ipv6 default route and withdraw from peers */
3697 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3698 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3699 SAFI_UNICAST
, false);
3701 /* delete all ipv6 routes and withdraw from peers */
3702 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3703 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3707 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3710 void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3712 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
3714 bgp_evpn
= bgp_get_evpn();
3718 /* update all ipv4 routes */
3719 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3720 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3722 /* update ipv4 default route and withdraw from peers */
3723 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3724 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3725 SAFI_UNICAST
, true);
3727 /* update all ipv6 routes */
3728 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3729 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3731 /* update ipv6 default route and withdraw from peers */
3732 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3733 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3734 SAFI_UNICAST
, true);
3739 * update and advertise local routes for a VRF as type-5 routes.
3740 * This is invoked upon RD change for a VRF. Note taht the processing is only
3741 * done in the global route table using the routes which already exist in the
3744 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3746 /* skip if the RD is configured */
3747 if (is_vrf_rd_configured(bgp_vrf
))
3750 /* derive the RD for the VRF based on new router-id */
3751 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3753 /* update advertise ipv4|ipv6 routes as type-5 routes */
3754 update_advertise_vrf_routes(bgp_vrf
);
3758 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3759 * This is invoked upon VRF RD change. The processing is done only from global
3762 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3764 /* skip if the RD is configured */
3765 if (is_vrf_rd_configured(bgp_vrf
))
3768 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3769 delete_withdraw_vrf_routes(bgp_vrf
);
3773 * Update and advertise local routes for a VNI. Invoked upon router-id
3774 * change. Note that the processing is done only on the global route table
3775 * using routes that already exist in the per-VNI table.
3777 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3779 struct prefix_evpn p
;
3780 struct bgp_dest
*dest
, *global_dest
;
3781 struct bgp_path_info
*pi
, *global_pi
;
3783 afi_t afi
= AFI_L2VPN
;
3784 safi_t safi
= SAFI_EVPN
;
3786 /* Locate type-3 route for VNI in the per-VNI table and use its
3787 * attributes to create and advertise the type-3 route for this VNI
3788 * in the global table.
3790 * RT-3 only if doing head-end replication
3792 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3793 == VXLAN_FLOOD_HEAD_END_REPL
) {
3794 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3795 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3796 if (!dest
) /* unexpected */
3798 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
3799 if (pi
->peer
== bgp
->peer_self
&&
3800 pi
->type
== ZEBRA_ROUTE_BGP
3801 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3803 if (!pi
) /* unexpected */
3807 global_dest
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3808 (struct prefix
*)&p
, &vpn
->prd
);
3809 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_dest
, attr
,
3810 1, &pi
, 0, mac_mobility_seqnum(attr
));
3812 /* Schedule for processing and unlock node. */
3813 bgp_process(bgp
, global_dest
, afi
, safi
);
3814 bgp_dest_unlock_node(global_dest
);
3817 /* Now, walk this VNI's route table and use the route and its attribute
3818 * to create and schedule route in global table.
3820 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
3821 dest
= bgp_route_next(dest
)) {
3822 const struct prefix_evpn
*evp
=
3823 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
3825 /* Identify MAC-IP local routes. */
3826 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3829 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
3830 if (pi
->peer
== bgp
->peer_self
3831 && pi
->type
== ZEBRA_ROUTE_BGP
3832 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3837 /* Create route in global routing table using this route entry's
3841 global_dest
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3842 (struct prefix
*)evp
, &vpn
->prd
);
3843 assert(global_dest
);
3844 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_dest
, attr
,
3846 mac_mobility_seqnum(attr
));
3848 /* Schedule for processing and unlock node. */
3849 bgp_process(bgp
, global_dest
, afi
, safi
);
3850 bgp_dest_unlock_node(global_dest
);
3857 * Delete (and withdraw) local routes for a VNI - only from the global
3858 * table. Invoked upon router-id change.
3860 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3863 struct prefix_evpn p
;
3864 struct bgp_dest
*global_dest
;
3865 struct bgp_path_info
*pi
;
3866 afi_t afi
= AFI_L2VPN
;
3867 safi_t safi
= SAFI_EVPN
;
3869 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3870 * for this VNI - from the global table.
3872 ret
= delete_global_type2_routes(bgp
, vpn
);
3876 /* Remove type-3 route for this VNI from global table. */
3877 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3878 global_dest
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3879 (struct prefix
*)&p
, &vpn
->prd
);
3881 /* Delete route entry in the global EVPN table. */
3882 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
3884 /* Schedule for processing - withdraws to peers happen from
3888 bgp_process(bgp
, global_dest
, afi
, safi
);
3889 bgp_dest_unlock_node(global_dest
);
3896 * Handle router-id change. Update and advertise local routes corresponding
3897 * to this VNI from peers. Note that this is invoked after updating the
3898 * router-id. The routes in the per-VNI table are used to create routes in
3899 * the global table and schedule them.
3901 static void update_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3903 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3905 /* Skip VNIs with configured RD. */
3906 if (is_rd_configured(vpn
))
3909 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3910 update_advertise_vni_routes(bgp
, vpn
);
3914 * Handle router-id change. Delete and withdraw local routes corresponding
3915 * to this VNI from peers. Note that this is invoked prior to updating
3916 * the router-id and is done only on the global route table, the routes
3917 * are needed in the per-VNI table to re-advertise with new router id.
3919 static void withdraw_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3921 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3923 /* Skip VNIs with configured RD. */
3924 if (is_rd_configured(vpn
))
3927 delete_withdraw_vni_routes(bgp
, vpn
);
3931 * Create RT-3 for a VNI and schedule for processing and advertisement.
3932 * This is invoked upon flooding mode changing to head-end replication.
3934 static void create_advertise_type3(struct hash_bucket
*bucket
, void *data
)
3936 struct bgpevpn
*vpn
= bucket
->data
;
3937 struct bgp
*bgp
= data
;
3938 struct prefix_evpn p
;
3940 if (!vpn
|| !is_vni_live(vpn
) ||
3941 bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3942 != VXLAN_FLOOD_HEAD_END_REPL
)
3945 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3946 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0))
3947 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3948 "Type3 route creation failure for VNI %u", vpn
->vni
);
3952 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3953 * This is invoked upon flooding mode changing to drop BUM packets.
3955 static void delete_withdraw_type3(struct hash_bucket
*bucket
, void *data
)
3957 struct bgpevpn
*vpn
= bucket
->data
;
3958 struct bgp
*bgp
= data
;
3959 struct prefix_evpn p
;
3961 if (!vpn
|| !is_vni_live(vpn
))
3964 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3965 delete_evpn_route(bgp
, vpn
, &p
);
3969 * Process received EVPN type-2 route (advertise or withdraw).
3971 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3972 struct attr
*attr
, uint8_t *pfx
, int psize
,
3973 uint32_t addpath_id
)
3975 struct prefix_rd prd
;
3976 struct prefix_evpn p
;
3977 struct bgp_route_evpn evpn
;
3979 uint8_t macaddr_len
;
3980 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
3981 uint32_t num_labels
= 0;
3985 /* Type-2 route should be either 33, 37 or 49 bytes or an
3986 * additional 3 bytes if there is a second label (VNI):
3987 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3988 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3989 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3991 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3992 && psize
!= 40 && psize
!= 52) {
3993 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3994 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3995 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3999 memset(&evpn
, 0, sizeof(evpn
));
4001 /* Make prefix_rd */
4002 prd
.family
= AF_UNSPEC
;
4004 memcpy(&prd
.val
, pfx
, 8);
4007 /* Make EVPN prefix. */
4008 memset(&p
, 0, sizeof(struct prefix_evpn
));
4010 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4011 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
4013 /* Copy Ethernet Seg Identifier */
4014 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
4017 /* Copy Ethernet Tag */
4018 memcpy(ð_tag
, pfx
, 4);
4019 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
4022 /* Get the MAC Addr len */
4023 macaddr_len
= *pfx
++;
4025 /* Get the MAC Addr */
4026 if (macaddr_len
== (ETH_ALEN
* 8)) {
4027 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
4031 EC_BGP_EVPN_ROUTE_INVALID
,
4032 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
4033 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
4039 ipaddr_len
= *pfx
++;
4040 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
4041 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
4043 EC_BGP_EVPN_ROUTE_INVALID
,
4044 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
4045 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4050 ipaddr_len
/= 8; /* Convert to bytes. */
4051 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
4054 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
4058 /* Get the VNI(s). Stored as bytes here. */
4060 memset(label
, 0, sizeof(label
));
4061 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
4062 pfx
+= BGP_LABEL_BYTES
;
4063 psize
-= (33 + ipaddr_len
);
4064 /* Do we have a second VNI? */
4067 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
4069 * If in future, we are required to access additional fields,
4070 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
4075 /* Process the route. */
4077 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4078 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4079 &prd
, &label
[0], num_labels
, 0, &evpn
);
4081 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4082 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4083 &prd
, &label
[0], num_labels
, &evpn
);
4088 * Process received EVPN type-3 route (advertise or withdraw).
4090 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4091 struct attr
*attr
, uint8_t *pfx
, int psize
,
4092 uint32_t addpath_id
)
4094 struct prefix_rd prd
;
4095 struct prefix_evpn p
;
4100 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
4101 * IP len (1) and IP (4 or 16).
4103 if (psize
!= 17 && psize
!= 29) {
4104 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4105 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
4106 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4110 /* If PMSI is present, log if it is anything other than IR.
4111 * Note: We just simply ignore the values as it is not clear if
4112 * doing anything else is better.
4115 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
4116 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
&&
4117 attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_PIM_SM
) {
4118 flog_warn(EC_BGP_EVPN_PMSI_PRESENT
,
4119 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
4120 peer
->bgp
->vrf_id
, peer
->host
,
4121 attr
->pmsi_tnl_type
);
4125 /* Make prefix_rd */
4126 prd
.family
= AF_UNSPEC
;
4128 memcpy(&prd
.val
, pfx
, 8);
4131 /* Make EVPN prefix. */
4132 memset(&p
, 0, sizeof(struct prefix_evpn
));
4134 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4135 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
4137 /* Copy Ethernet Tag */
4138 memcpy(ð_tag
, pfx
, 4);
4139 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
4143 ipaddr_len
= *pfx
++;
4144 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
4145 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
4146 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
4149 EC_BGP_EVPN_ROUTE_INVALID
,
4150 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
4151 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4155 /* Process the route. */
4157 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4158 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4159 &prd
, NULL
, 0, 0, NULL
);
4161 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4162 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4163 &prd
, NULL
, 0, NULL
);
4168 * Process received EVPN type-4 route (advertise or withdraw).
4170 static int process_type4_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4171 struct attr
*attr
, uint8_t *pfx
, int psize
,
4172 uint32_t addpath_id
)
4177 struct in_addr vtep_ip
;
4178 struct prefix_rd prd
;
4179 struct prefix_evpn p
;
4181 /* Type-4 route should be either 23 or 35 bytes
4182 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
4184 if (psize
!= 23 && psize
!= 35) {
4185 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4186 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
4187 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4191 /* Make prefix_rd */
4192 prd
.family
= AF_UNSPEC
;
4194 memcpy(&prd
.val
, pfx
, 8);
4198 memcpy(&esi
, pfx
, ESI_BYTES
);
4203 ipaddr_len
= *pfx
++;
4204 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
4205 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
4208 EC_BGP_EVPN_ROUTE_INVALID
,
4209 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
4210 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4214 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
4215 /* Process the route. */
4217 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4218 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4219 &prd
, NULL
, 0, 0, NULL
);
4221 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4222 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4223 &prd
, NULL
, 0, NULL
);
4230 * Process received EVPN type-5 route (advertise or withdraw).
4232 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4233 struct attr
*attr
, uint8_t *pfx
, int psize
,
4234 uint32_t addpath_id
)
4236 struct prefix_rd prd
;
4237 struct prefix_evpn p
;
4238 struct bgp_route_evpn evpn
;
4241 mpls_label_t label
; /* holds the VNI as in the packet */
4244 bool is_valid_update
= false;
4246 /* Type-5 route should be 34 or 58 bytes:
4247 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
4248 * GW (4 or 16) and VNI (3).
4249 * Note that the IP and GW should both be IPv4 or both IPv6.
4251 if (psize
!= 34 && psize
!= 58) {
4252 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4253 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
4254 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4258 /* Make prefix_rd */
4259 prd
.family
= AF_UNSPEC
;
4261 memcpy(&prd
.val
, pfx
, 8);
4264 /* Make EVPN prefix. */
4265 memset(&p
, 0, sizeof(struct prefix_evpn
));
4267 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4268 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
4270 /* Additional information outside of prefix - ESI and GW IP */
4271 memset(&evpn
, 0, sizeof(evpn
));
4274 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
4277 /* Fetch Ethernet Tag. */
4278 memcpy(ð_tag
, pfx
, 4);
4279 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
4282 /* Fetch IP prefix length. */
4284 if (ippfx_len
> IPV6_MAX_BITLEN
) {
4286 EC_BGP_EVPN_ROUTE_INVALID
,
4287 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4288 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
4291 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
4293 /* Determine IPv4 or IPv6 prefix */
4294 /* Since the address and GW are from the same family, this just becomes
4295 * a simple check on the total size.
4298 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
4299 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
4301 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
4305 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
4306 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
4308 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
4313 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4314 memset(&label
, 0, sizeof(label
));
4315 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
4318 * If in future, we are required to access additional fields,
4319 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4324 is_valid_update
= true;
4325 if (is_zero_mac(&attr
->rmac
) && is_zero_esi(&evpn
.eth_s_id
) &&
4326 is_zero_gw_ip(&evpn
.gw_ip
, gw_afi
))
4327 is_valid_update
= false;
4329 if (is_mcast_mac(&attr
->rmac
) || is_bcast_mac(&attr
->rmac
))
4330 is_valid_update
= false;
4333 /* Process the route. */
4334 if (is_valid_update
)
4335 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4336 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4337 &prd
, &label
, 1, 0, &evpn
);
4339 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4340 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4341 &prd
, &label
, 1, &evpn
);
4346 static void evpn_mpattr_encode_type5(struct stream
*s
, const struct prefix
*p
,
4347 const struct prefix_rd
*prd
,
4348 mpls_label_t
*label
, uint32_t num_labels
,
4353 const struct evpn_addr
*p_evpn_p
;
4355 memset(&temp
, 0, 16);
4356 if (p
->family
!= AF_EVPN
)
4358 p_evpn_p
= &(p
->u
.prefix_evpn
);
4360 /* len denites the total len of IP and GW-IP in the route
4361 IP and GW-IP have to be both ipv4 or ipv6
4363 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4364 len
= 8; /* IP and GWIP are both ipv4 */
4366 len
= 32; /* IP and GWIP are both ipv6 */
4367 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4368 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4369 stream_put(s
, prd
->val
, 8);
4371 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
4373 stream_put(s
, &temp
, 10);
4374 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4375 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4376 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4377 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4379 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4381 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4383 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
4385 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
4387 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4388 stream_put_ipv4(s
, 0);
4390 stream_put(s
, &temp
, 16);
4394 stream_put(s
, label
, 3);
4400 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4402 static void cleanup_vni_on_disable(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4404 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4406 /* Remove EVPN routes and schedule for processing. */
4407 delete_routes_for_vni(bgp
, vpn
);
4409 /* Clear "live" flag and see if hash needs to be freed. */
4410 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4411 if (!is_vni_configured(vpn
))
4412 bgp_evpn_free(bgp
, vpn
);
4416 * Free a VNI entry; iterator function called during cleanup.
4418 static void free_vni_entry(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4420 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4422 delete_all_vni_routes(bgp
, vpn
);
4423 bgp_evpn_free(bgp
, vpn
);
4427 * Derive AUTO import RT for BGP VRF - L3VNI
4429 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4431 struct bgp
*bgp_evpn
= NULL
;
4433 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4434 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4437 bgp_evpn
= bgp_get_evpn();
4440 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4444 * Delete AUTO import RT from BGP VRF - L3VNI
4446 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4448 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4452 * Derive AUTO export RT for BGP VRF - L3VNI
4454 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4456 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4457 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4461 * Delete AUTO export RT from BGP VRF - L3VNI
4463 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4465 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4468 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4470 struct bgp
*bgp_evpn
= NULL
;
4471 struct listnode
*node
= NULL
;
4472 struct bgpevpn
*vpn
= NULL
;
4474 bgp_evpn
= bgp_get_evpn();
4478 /* update all type-5 routes */
4479 update_advertise_vrf_routes(bgp_vrf
);
4481 /* update all type-2 routes */
4482 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4483 update_routes_for_vni(bgp_evpn
, vpn
);
4487 * Handle autort change for a given VNI.
4489 static void update_autort_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4491 struct bgpevpn
*vpn
= bucket
->data
;
4493 if (!is_import_rt_configured(vpn
)) {
4494 if (is_vni_live(vpn
))
4495 bgp_evpn_uninstall_routes(bgp
, vpn
);
4496 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4497 list_delete_all_node(vpn
->import_rtl
);
4498 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4499 if (is_vni_live(vpn
))
4500 bgp_evpn_install_routes(bgp
, vpn
);
4502 if (!is_export_rt_configured(vpn
)) {
4503 list_delete_all_node(vpn
->export_rtl
);
4504 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4505 if (is_vni_live(vpn
))
4506 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4514 /* withdraw type-5 route corresponding to ip prefix */
4515 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4516 afi_t afi
, safi_t safi
)
4519 struct prefix_evpn evp
;
4520 char buf
[PREFIX_STRLEN
];
4522 build_type5_prefix_from_ip_prefix(&evp
, p
);
4523 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4526 EC_BGP_EVPN_ROUTE_DELETE
,
4527 "%u failed to delete type-5 route for prefix %s in vrf %s",
4528 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4529 vrf_id_to_name(bgp_vrf
->vrf_id
));
4533 /* withdraw all type-5 routes for an address family */
4534 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4536 struct bgp_table
*table
= NULL
;
4537 struct bgp_dest
*dest
= NULL
;
4538 struct bgp_path_info
*pi
;
4540 table
= bgp_vrf
->rib
[afi
][safi
];
4541 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
4542 /* Only care about "selected" routes. Also ensure that
4543 * these are routes that are injectable into EVPN.
4545 /* TODO: Support for AddPath for EVPN. */
4546 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
4547 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4548 && is_route_injectable_into_evpn(pi
)) {
4549 bgp_evpn_withdraw_type5_route(
4550 bgp_vrf
, bgp_dest_get_prefix(dest
), afi
,
4559 * evpn - enable advertisement of default g/w
4561 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
4562 safi_t safi
, bool add
)
4564 struct prefix ip_prefix
;
4566 /* form the default prefix 0.0.0.0/0 */
4567 memset(&ip_prefix
, 0, sizeof(struct prefix
));
4568 ip_prefix
.family
= afi2family(afi
);
4571 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
4574 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
4581 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4582 * to this function correspond to those of the source IP prefix (best
4583 * path in the case of the attr. In the case of a local prefix (when we
4584 * are advertising local subnets), the src_attr will be NULL.
4586 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4587 struct attr
*src_attr
, afi_t afi
,
4591 struct prefix_evpn evp
;
4592 char buf
[PREFIX_STRLEN
];
4594 build_type5_prefix_from_ip_prefix(&evp
, p
);
4595 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4597 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4598 "%u: Failed to create type-5 route for prefix %s",
4599 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4602 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4603 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4604 * advertisement is enabled.
4606 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4609 struct bgp_table
*table
= NULL
;
4610 struct bgp_dest
*dest
= NULL
;
4611 struct bgp_path_info
*pi
;
4613 table
= bgp_vrf
->rib
[afi
][safi
];
4614 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
4615 /* Need to identify the "selected" route entry to use its
4616 * attribute. Also, ensure that the route is injectable
4618 * TODO: Support for AddPath for EVPN.
4620 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
4621 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4622 && is_route_injectable_into_evpn(pi
)) {
4624 /* apply the route-map */
4625 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4626 route_map_result_t ret
;
4627 struct bgp_path_info tmp_pi
;
4628 struct bgp_path_info_extra tmp_pie
;
4629 struct attr tmp_attr
;
4631 tmp_attr
= *pi
->attr
;
4633 /* Fill temp path_info */
4634 prep_for_rmap_apply(&tmp_pi
, &tmp_pie
,
4638 RESET_FLAG(tmp_attr
.rmap_change_flags
);
4640 ret
= route_map_apply(
4641 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4643 bgp_dest_get_prefix(dest
),
4645 if (ret
== RMAP_DENYMATCH
) {
4646 bgp_attr_flush(&tmp_attr
);
4649 bgp_evpn_advertise_type5_route(
4651 bgp_dest_get_prefix(dest
),
4652 &tmp_attr
, afi
, safi
);
4654 bgp_evpn_advertise_type5_route(
4656 bgp_dest_get_prefix(dest
),
4657 pi
->attr
, afi
, safi
);
4664 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4666 struct listnode
*node
, *nnode
, *node_to_del
;
4667 struct ecommunity
*ecom
, *ecom_auto
;
4668 struct ecommunity_val eval
;
4670 if (bgp
->advertise_autort_rfc8365
)
4671 vni
|= EVPN_AUTORT_VXLAN
;
4672 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4674 ecom_auto
= ecommunity_new();
4675 ecommunity_add_val(ecom_auto
, &eval
, false, false);
4678 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4679 if (ecommunity_match(ecom
, ecom_auto
)) {
4680 ecommunity_free(&ecom
);
4686 list_delete_node(rtl
, node_to_del
);
4688 ecommunity_free(&ecom_auto
);
4691 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4692 struct ecommunity
*ecomadd
)
4694 /* uninstall routes from vrf */
4695 if (is_l3vni_live(bgp_vrf
))
4696 uninstall_routes_for_vrf(bgp_vrf
);
4698 /* Cleanup the RT to VRF mapping */
4699 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4701 /* Remove auto generated RT */
4702 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4704 /* Add the newly configured RT to RT list */
4705 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4706 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4708 /* map VRF to its RTs and install routes matching the new RTs */
4709 if (is_l3vni_live(bgp_vrf
)) {
4710 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4711 install_routes_for_vrf(bgp_vrf
);
4715 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4716 struct ecommunity
*ecomdel
)
4718 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4719 struct ecommunity
*ecom
= NULL
;
4721 /* uninstall routes from vrf */
4722 if (is_l3vni_live(bgp_vrf
))
4723 uninstall_routes_for_vrf(bgp_vrf
);
4725 /* Cleanup the RT to VRF mapping */
4726 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4728 /* remove the RT from the RT list */
4729 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4730 if (ecommunity_match(ecom
, ecomdel
)) {
4731 ecommunity_free(&ecom
);
4738 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4740 assert(bgp_vrf
->vrf_import_rtl
);
4741 /* fallback to auto import rt, if this was the last RT */
4742 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4743 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4744 if (is_l3vni_live(bgp_vrf
))
4745 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4748 /* map VRFs to its RTs and install routes matching this new RT */
4749 if (is_l3vni_live(bgp_vrf
)) {
4750 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4751 install_routes_for_vrf(bgp_vrf
);
4755 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4756 struct ecommunity
*ecomadd
)
4758 /* remove auto-generated RT */
4759 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4761 /* Add the new RT to the RT list */
4762 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4763 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4765 if (is_l3vni_live(bgp_vrf
))
4766 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4769 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4770 struct ecommunity
*ecomdel
)
4772 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4773 struct ecommunity
*ecom
= NULL
;
4775 /* Remove the RT from the RT list */
4776 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4777 if (ecommunity_match(ecom
, ecomdel
)) {
4778 ecommunity_free(&ecom
);
4785 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4788 * Temporary assert to make SA happy.
4789 * The ALL_LIST_ELEMENTS macro above has a NULL check
4790 * which means that SA is going to complain about
4791 * the list_isempty call, which doesn't NULL check.
4792 * So until we get this situation cleaned up, here
4795 assert(bgp_vrf
->vrf_export_rtl
);
4797 /* fall back to auto-generated RT if this was the last RT */
4798 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4799 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4800 if (is_l3vni_live(bgp_vrf
))
4801 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4804 if (is_l3vni_live(bgp_vrf
))
4805 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4809 * Handle change to BGP router id. This is invoked twice by the change
4810 * handler, first before the router id has been changed and then after
4811 * the router id has been changed. The first invocation will result in
4812 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4813 * will result in the routes being re-advertised.
4815 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4817 struct listnode
*node
;
4818 struct bgp
*bgp_vrf
;
4822 /* delete and withdraw all the type-5 routes
4823 stored in the global table for this vrf
4825 withdraw_router_id_vrf(bgp
);
4827 /* delete all the VNI routes (type-2/type-3) routes for all the
4830 hash_iterate(bgp
->vnihash
,
4831 (void (*)(struct hash_bucket
*,
4832 void *))withdraw_router_id_vni
,
4835 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4836 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4837 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4838 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4840 bgp_vrf
->evpn_info
->pip_ip
.s_addr
4846 /* Assign new default instance router-id */
4847 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4848 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4849 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4850 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4852 bgp_vrf
->evpn_info
->pip_ip
=
4854 /* advertise type-5 routes with
4857 update_advertise_vrf_routes(bgp_vrf
);
4862 /* advertise all routes in the vrf as type-5 routes with the new
4865 update_router_id_vrf(bgp
);
4867 /* advertise all the VNI routes (type-2/type-3) routes with the
4870 hash_iterate(bgp
->vnihash
,
4871 (void (*)(struct hash_bucket
*,
4872 void *))update_router_id_vni
,
4878 * Handle change to auto-RT algorithm - update and advertise local routes.
4880 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4882 hash_iterate(bgp
->vnihash
,
4883 (void (*)(struct hash_bucket
*,
4884 void*))update_autort_vni
,
4889 * Handle change to export RT - update and advertise local routes.
4891 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4893 return update_routes_for_vni(bgp
, vpn
);
4896 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4899 delete_withdraw_vrf_routes(bgp_vrf
);
4901 update_advertise_vrf_routes(bgp_vrf
);
4905 * Handle change to RD. This is invoked twice by the change handler,
4906 * first before the RD has been changed and then after the RD has
4907 * been changed. The first invocation will result in local routes
4908 * of this VNI being deleted and withdrawn and the next will result
4909 * in the routes being re-advertised.
4911 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4915 delete_withdraw_vni_routes(bgp
, vpn
);
4917 update_advertise_vni_routes(bgp
, vpn
);
4921 * Install routes for this VNI. Invoked upon change to Import RT.
4923 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4925 return install_routes_for_vni(bgp
, vpn
);
4929 * Uninstall all routes installed for this VNI. Invoked upon change
4932 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4934 return uninstall_routes_for_vni(bgp
, vpn
);
4938 * TODO: Hardcoded for a maximum of 2 VNIs right now
4940 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4945 vni1
= label2vni(label
);
4946 if (num_labels
== 2) {
4947 vni2
= label2vni(label
+ 1);
4948 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4950 snprintf(buf
, len
, "%u", vni1
);
4955 * Function to convert evpn route to json format.
4956 * NOTE: We don't use prefix2str as the output here is a bit different.
4958 void bgp_evpn_route2json(const struct prefix_evpn
*p
, json_object
*json
)
4960 char buf1
[ETHER_ADDR_STRLEN
];
4961 char buf2
[PREFIX2STR_BUFFER
];
4968 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4970 switch (p
->prefix
.route_type
) {
4971 case BGP_EVPN_MAC_IP_ROUTE
:
4972 json_object_int_add(json
, "ethTag",
4973 p
->prefix
.macip_addr
.eth_tag
);
4974 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4975 json_object_string_add(json
, "mac",
4976 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4979 if (!is_evpn_prefix_ipaddr_none(p
)) {
4980 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
:
4982 prefixlen
= (family
== AF_INET
) ?
4983 IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
;
4984 inet_ntop(family
, &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4985 buf2
, PREFIX2STR_BUFFER
);
4986 json_object_int_add(json
, "ipLen", prefixlen
);
4987 json_object_string_add(json
, "ip", buf2
);
4991 case BGP_EVPN_IMET_ROUTE
:
4992 json_object_int_add(json
, "ethTag",
4993 p
->prefix
.imet_addr
.eth_tag
);
4994 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4995 prefixlen
= (family
== AF_INET
) ? IPV4_MAX_BITLEN
:
4997 inet_ntop(family
, &p
->prefix
.imet_addr
.ip
.ip
.addr
, buf2
,
4999 json_object_int_add(json
, "ipLen", prefixlen
);
5000 json_object_string_add(json
, "ip", buf2
);
5003 case BGP_EVPN_IP_PREFIX_ROUTE
:
5004 json_object_int_add(json
, "ethTag",
5005 p
->prefix
.prefix_addr
.eth_tag
);
5006 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
5007 inet_ntop(family
, &p
->prefix
.prefix_addr
.ip
.ip
.addr
,
5008 buf2
, sizeof(buf2
));
5009 json_object_int_add(json
, "ipLen",
5010 p
->prefix
.prefix_addr
.ip_prefix_length
);
5011 json_object_string_add(json
, "ip", buf2
);
5020 * Function to convert evpn route to string.
5021 * NOTE: We don't use prefix2str as the output here is a bit different.
5023 char *bgp_evpn_route2str(const struct prefix_evpn
*p
, char *buf
, int len
)
5025 char buf1
[ETHER_ADDR_STRLEN
];
5026 char buf2
[PREFIX2STR_BUFFER
];
5027 char buf3
[ESI_STR_LEN
];
5029 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
5030 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
5031 p
->prefix
.imet_addr
.eth_tag
,
5032 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
5034 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
5035 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
5036 if (is_evpn_prefix_ipaddr_none(p
))
5037 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
5038 p
->prefix
.route_type
,
5039 p
->prefix
.macip_addr
.eth_tag
,
5041 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
5046 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
5048 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
5049 p
->prefix
.route_type
,
5050 p
->prefix
.macip_addr
.eth_tag
,
5052 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
5054 family
== AF_INET
? IPV4_MAX_BITLEN
5057 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
5059 PREFIX2STR_BUFFER
));
5061 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
5062 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
5063 p
->prefix
.route_type
,
5064 p
->prefix
.prefix_addr
.eth_tag
,
5065 p
->prefix
.prefix_addr
.ip_prefix_length
,
5066 is_evpn_prefix_ipaddr_v4(p
)
5067 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
5068 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
5069 } else if (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
5070 snprintf(buf
, len
, "[%d]:[%s]:[%d]:[%s]",
5071 p
->prefix
.route_type
,
5072 esi_to_str(&p
->prefix
.es_addr
.esi
, buf3
, sizeof(buf3
)),
5073 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
5075 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
));
5077 /* For EVPN route types not supported yet. */
5078 snprintf(buf
, len
, "(unsupported route type %d)",
5079 p
->prefix
.route_type
);
5086 * Encode EVPN prefix in Update (MP_REACH)
5088 void bgp_evpn_encode_prefix(struct stream
*s
, const struct prefix
*p
,
5089 const struct prefix_rd
*prd
, mpls_label_t
*label
,
5090 uint32_t num_labels
, struct attr
*attr
,
5091 int addpath_encode
, uint32_t addpath_tx_id
)
5093 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
5094 int len
, ipa_len
= 0;
5097 stream_putl(s
, addpath_tx_id
);
5100 stream_putc(s
, evp
->prefix
.route_type
);
5102 switch (evp
->prefix
.route_type
) {
5103 case BGP_EVPN_MAC_IP_ROUTE
:
5104 if (is_evpn_prefix_ipaddr_v4(evp
))
5105 ipa_len
= IPV4_MAX_BYTELEN
;
5106 else if (is_evpn_prefix_ipaddr_v6(evp
))
5107 ipa_len
= IPV6_MAX_BYTELEN
;
5108 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
5109 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
5110 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
5112 stream_putc(s
, len
);
5113 stream_put(s
, prd
->val
, 8); /* RD */
5115 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
5117 stream_put(s
, 0, 10);
5118 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
5119 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
5120 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
5121 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
5122 if (ipa_len
) /* IP */
5123 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
5125 /* 1st label is the L2 VNI */
5126 stream_put(s
, label
, BGP_LABEL_BYTES
);
5127 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
5128 if (ipa_len
&& num_labels
> 1)
5129 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
5132 case BGP_EVPN_IMET_ROUTE
:
5133 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
5134 stream_put(s
, prd
->val
, 8); /* RD */
5135 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
5136 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
5137 /* Originating Router's IP Addr */
5138 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
5141 case BGP_EVPN_ES_ROUTE
:
5142 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
5143 stream_put(s
, prd
->val
, 8); /* RD */
5144 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
5145 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
5147 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
5150 case BGP_EVPN_IP_PREFIX_ROUTE
:
5151 /* TODO: AddPath support. */
5152 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
5160 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
5161 struct bgp_nlri
*packet
, int withdraw
)
5167 uint32_t addpath_id
;
5168 int addpath_encoded
;
5173 /* Start processing the NLRI - there may be multiple in the MP_REACH */
5175 lim
= pnt
+ packet
->length
;
5177 safi
= packet
->safi
;
5181 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
5182 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5183 PEER_CAP_ADDPATH_AF_TX_RCV
));
5185 for (; pnt
< lim
; pnt
+= psize
) {
5186 /* Clear prefix structure. */
5187 memset(&p
, 0, sizeof(struct prefix
));
5189 /* Deal with path-id if AddPath is supported. */
5190 if (addpath_encoded
) {
5191 /* When packet overflow occurs return immediately. */
5192 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
5193 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5195 memcpy(&addpath_id
, pnt
, BGP_ADDPATH_ID_LEN
);
5196 addpath_id
= ntohl(addpath_id
);
5197 pnt
+= BGP_ADDPATH_ID_LEN
;
5200 /* All EVPN NLRI types start with type and length. */
5202 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE
;
5207 /* When packet overflow occur return immediately. */
5208 if (pnt
+ psize
> lim
)
5209 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5212 case BGP_EVPN_MAC_IP_ROUTE
:
5213 if (process_type2_route(peer
, afi
, safi
,
5214 withdraw
? NULL
: attr
, pnt
,
5215 psize
, addpath_id
)) {
5218 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
5219 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5220 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE
;
5224 case BGP_EVPN_IMET_ROUTE
:
5225 if (process_type3_route(peer
, afi
, safi
,
5226 withdraw
? NULL
: attr
, pnt
,
5227 psize
, addpath_id
)) {
5230 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
5231 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5232 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE
;
5236 case BGP_EVPN_ES_ROUTE
:
5237 if (process_type4_route(peer
, afi
, safi
,
5238 withdraw
? NULL
: attr
, pnt
,
5239 psize
, addpath_id
)) {
5242 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
5243 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5244 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE
;
5248 case BGP_EVPN_IP_PREFIX_ROUTE
:
5249 if (process_type5_route(peer
, afi
, safi
,
5250 withdraw
? NULL
: attr
, pnt
,
5251 psize
, addpath_id
)) {
5254 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
5255 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5256 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE
;
5265 /* Packet length consistency check. */
5267 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH
;
5269 return BGP_NLRI_PARSE_OK
;
5273 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
5274 * The mapping will be used during route processing.
5275 * bgp_def: default bgp instance
5276 * bgp_vrf: specific bgp vrf instance on which RT is configured
5278 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
5281 struct ecommunity_val
*eval
= NULL
;
5282 struct listnode
*node
= NULL
, *nnode
= NULL
;
5283 struct ecommunity
*ecom
= NULL
;
5285 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5286 for (i
= 0; i
< ecom
->size
; i
++) {
5287 eval
= (struct ecommunity_val
*)(ecom
->val
5289 * ECOMMUNITY_SIZE
));
5290 map_vrf_to_rt(bgp_vrf
, eval
);
5296 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
5298 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
5301 struct ecommunity_val
*eval
;
5302 struct listnode
*node
, *nnode
;
5303 struct ecommunity
*ecom
;
5305 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5306 for (i
= 0; i
< ecom
->size
; i
++) {
5307 struct vrf_irt_node
*irt
;
5308 struct ecommunity_val eval_tmp
;
5310 eval
= (struct ecommunity_val
*)(ecom
->val
5312 * ECOMMUNITY_SIZE
));
5313 /* If using "automatic" RT, we only care about the
5314 * local-admin sub-field.
5315 * This is to facilitate using VNI as the RT for EBGP
5318 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5319 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
5320 BGP_VRF_IMPORT_RT_CFGD
))
5321 mask_ecom_global_admin(&eval_tmp
, eval
);
5323 irt
= lookup_vrf_import_rt(&eval_tmp
);
5325 unmap_vrf_from_rt(bgp_vrf
, irt
);
5332 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
5333 * The mapping will be used during route processing.
5335 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5338 struct ecommunity_val
*eval
;
5339 struct listnode
*node
, *nnode
;
5340 struct ecommunity
*ecom
;
5342 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5343 for (i
= 0; i
< ecom
->size
; i
++) {
5344 eval
= (struct ecommunity_val
*)(ecom
->val
5346 * ECOMMUNITY_SIZE
));
5347 map_vni_to_rt(bgp
, vpn
, eval
);
5353 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
5355 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5358 struct ecommunity_val
*eval
;
5359 struct listnode
*node
, *nnode
;
5360 struct ecommunity
*ecom
;
5362 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5363 for (i
= 0; i
< ecom
->size
; i
++) {
5364 struct irt_node
*irt
;
5365 struct ecommunity_val eval_tmp
;
5367 eval
= (struct ecommunity_val
*)(ecom
->val
5369 * ECOMMUNITY_SIZE
));
5370 /* If using "automatic" RT, we only care about the
5371 * local-admin sub-field.
5372 * This is to facilitate using VNI as the RT for EBGP
5375 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5376 if (!is_import_rt_configured(vpn
))
5377 mask_ecom_global_admin(&eval_tmp
, eval
);
5379 irt
= lookup_import_rt(bgp
, &eval_tmp
);
5381 unmap_vni_from_rt(bgp
, vpn
, irt
);
5387 * Derive Import RT automatically for VNI and map VNI to RT.
5388 * The mapping will be used during route processing.
5390 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5392 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
5393 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
5396 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
5400 * Derive Export RT automatically for VNI.
5402 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5404 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
5405 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5409 * Derive RD automatically for VNI using passed information - it
5410 * is of the form RouterId:unique-id-for-vni.
5412 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5414 if (is_vrf_rd_configured(bgp
))
5417 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5421 * Derive RD automatically for VNI using passed information - it
5422 * is of the form RouterId:unique-id-for-vni.
5424 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5428 vpn
->prd
.family
= AF_UNSPEC
;
5429 vpn
->prd
.prefixlen
= 64;
5430 snprintf(buf
, sizeof(buf
), "%s:%hu", inet_ntoa(bgp
->router_id
),
5432 (void)str2prefix_rd(buf
, &vpn
->prd
);
5433 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5439 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5441 struct list
*inst
= bm
->bgp
;
5442 struct listnode
*node
;
5443 struct bgp
*bgp_vrf
;
5445 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5446 if (bgp_vrf
->l3vni
== vni
)
5456 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5458 struct bgpevpn
*vpn
;
5461 memset(&tmp
, 0, sizeof(struct bgpevpn
));
5463 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5468 * Create a new vpn - invoked upon configuration or zebra notification.
5470 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5471 struct in_addr originator_ip
,
5472 vrf_id_t tenant_vrf_id
,
5473 struct in_addr mcast_grp
)
5475 struct bgpevpn
*vpn
;
5480 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5482 /* Set values - RD and RT set to defaults. */
5484 vpn
->originator_ip
= originator_ip
;
5485 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5486 vpn
->mcast_grp
= mcast_grp
;
5488 /* Initialize route-target import and export lists */
5489 vpn
->import_rtl
= list_new();
5490 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5491 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
5492 vpn
->export_rtl
= list_new();
5493 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5494 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
5495 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5496 derive_rd_rt_for_vni(bgp
, vpn
);
5498 /* Initialize EVPN route table. */
5499 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5502 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
5503 XFREE(MTYPE_BGP_EVPN
, vpn
);
5507 /* add to l2vni list on corresponding vrf */
5508 bgpevpn_link_to_l3vni(vpn
);
5510 QOBJ_REG(vpn
, bgpevpn
);
5515 * Free a given VPN - called in multiple scenarios such as zebra
5516 * notification, configuration being deleted, advertise-all-vni disabled etc.
5517 * This just frees appropriate memory, caller should have taken other
5520 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5522 bgpevpn_unlink_from_l3vni(vpn
);
5523 bgp_table_unlock(vpn
->route_table
);
5524 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5525 list_delete(&vpn
->import_rtl
);
5526 list_delete(&vpn
->export_rtl
);
5527 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5528 hash_release(bgp
->vnihash
, vpn
);
5530 XFREE(MTYPE_BGP_EVPN
, vpn
);
5536 struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
)
5541 memset(&tmp
, 0, sizeof(struct evpnes
));
5542 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
5543 es
= hash_lookup(bgp
->esihash
, &tmp
);
5548 * Create a new local es - invoked upon zebra notification.
5550 struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
,
5552 struct ipaddr
*originator_ip
)
5560 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct evpnes
));
5562 /* set the ESI and originator_ip */
5563 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
5564 memcpy(&es
->originator_ip
, originator_ip
, sizeof(struct ipaddr
));
5566 /* Initialise the VTEP list */
5567 es
->vtep_list
= list_new();
5568 es
->vtep_list
->cmp
= evpn_vtep_ip_cmp
;
5570 /* auto derive RD for this es */
5571 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
5572 es
->prd
.family
= AF_UNSPEC
;
5573 es
->prd
.prefixlen
= 64;
5574 snprintf(buf
, sizeof(buf
), "%s:%hu", inet_ntoa(bgp
->router_id
),
5576 (void)str2prefix_rd(buf
, &es
->prd
);
5578 /* Initialize the ES route table */
5579 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5582 if (!hash_get(bgp
->esihash
, es
, hash_alloc_intern
)) {
5583 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5587 QOBJ_REG(es
, evpnes
);
5593 * This just frees appropriate memory, caller should have taken other
5596 void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
)
5598 list_delete(&es
->vtep_list
);
5599 bgp_table_unlock(es
->route_table
);
5600 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
5601 hash_release(bgp
->esihash
, es
);
5603 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5607 * Import evpn route from global table to VNI/VRF/ESI.
5609 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5610 const struct prefix
*p
, struct bgp_path_info
*pi
)
5612 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5616 * Unimport evpn route from VNI/VRF/ESI.
5618 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5619 const struct prefix
*p
, struct bgp_path_info
*pi
)
5621 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5624 /* filter routes which have martian next hops */
5625 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5629 struct bgp_dest
*rd_dest
, *dest
;
5630 struct bgp_table
*table
;
5631 struct bgp_path_info
*pi
;
5636 /* Walk entire global routing table and evaluate routes which could be
5637 * imported into this VPN. Note that we cannot just look at the routes
5638 * for the VNI's RD -
5639 * remote routes applicable for this VNI could have any RD.
5641 /* EVPN routes are a 2-level table. */
5642 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
5643 rd_dest
= bgp_route_next(rd_dest
)) {
5644 table
= bgp_dest_get_bgp_table_info(rd_dest
);
5648 for (dest
= bgp_table_top(table
); dest
;
5649 dest
= bgp_route_next(dest
)) {
5651 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
5654 /* Consider "valid" remote routes applicable for
5656 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5657 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5659 if (bgp_nexthop_self(bgp
, afi
, pi
->type
,
5660 pi
->sub_type
, pi
->attr
,
5662 const struct prefix
*p
=
5663 bgp_dest_get_prefix(dest
);
5665 if (bgp_debug_update(pi
->peer
, p
, NULL
,
5667 char attr_str
[BUFSIZ
] = {0};
5669 bgp_dump_attr(pi
->attr
,
5673 "%u: prefix %pRN with attr %s - DENIED due to martian or self nexthop",
5677 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5680 bgp_rib_remove(dest
, pi
, pi
->peer
, afi
,
5691 * Handle del of a local MACIP.
5693 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5694 struct ipaddr
*ip
, int state
)
5696 struct bgpevpn
*vpn
;
5697 struct prefix_evpn p
;
5698 struct bgp_dest
*dest
;
5700 /* Lookup VNI hash - should exist. */
5701 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5702 if (!vpn
|| !is_vni_live(vpn
)) {
5703 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5704 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5705 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5709 build_evpn_type2_prefix(&p
, mac
, ip
);
5710 if (state
== ZEBRA_NEIGH_ACTIVE
) {
5711 /* Remove EVPN type-2 route and schedule for processing. */
5712 delete_evpn_route(bgp
, vpn
, &p
);
5714 /* Re-instate the current remote best path if any */
5715 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
5717 evpn_zebra_reinstall_best_route(bgp
, vpn
, dest
);
5724 * Handle add of a local MACIP.
5726 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5727 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
)
5729 struct bgpevpn
*vpn
;
5730 struct prefix_evpn p
;
5732 /* Lookup VNI hash - should exist. */
5733 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5734 if (!vpn
|| !is_vni_live(vpn
)) {
5735 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5736 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5737 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5741 /* Create EVPN type-2 route and schedule for processing. */
5742 build_evpn_type2_prefix(&p
, mac
, ip
);
5743 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
)) {
5744 char buf
[ETHER_ADDR_STRLEN
];
5745 char buf2
[INET6_ADDRSTRLEN
];
5748 EC_BGP_EVPN_ROUTE_CREATE
,
5749 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5750 bgp
->vrf_id
, vpn
->vni
,
5751 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5754 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5755 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5762 static void link_l2vni_hash_to_l3vni(struct hash_bucket
*bucket
,
5763 struct bgp
*bgp_vrf
)
5765 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
5766 struct bgp
*bgp_evpn
= NULL
;
5768 bgp_evpn
= bgp_get_evpn();
5771 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5772 bgpevpn_link_to_l3vni(vpn
);
5775 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
,
5776 struct ethaddr
*svi_rmac
,
5777 struct ethaddr
*vrr_rmac
,
5778 struct in_addr originator_ip
, int filter
,
5779 ifindex_t svi_ifindex
,
5780 bool is_anycast_mac
)
5782 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5783 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5784 struct listnode
*node
= NULL
;
5785 struct bgpevpn
*vpn
= NULL
;
5788 /* get the EVPN instance - required to get the AS number for VRF
5791 bgp_evpn
= bgp_get_evpn();
5795 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
5801 /* if the BGP vrf instance doesn't exist - create one */
5802 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5807 ret
= bgp_get_vty(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5808 vrf_id
== VRF_DEFAULT
5809 ? BGP_INSTANCE_TYPE_DEFAULT
5810 : BGP_INSTANCE_TYPE_VRF
);
5812 case BGP_ERR_AS_MISMATCH
:
5813 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5814 "BGP is already running; AS is %u\n", as
);
5816 case BGP_ERR_INSTANCE_MISMATCH
:
5817 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5818 "BGP instance name and AS number mismatch\n");
5822 /* mark as auto created */
5823 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5826 /* associate the vrf with l3vni and related parameters */
5827 bgp_vrf
->l3vni
= l3vni
;
5828 bgp_vrf
->originator_ip
= originator_ip
;
5829 bgp_vrf
->l3vni_svi_ifindex
= svi_ifindex
;
5830 bgp_vrf
->evpn_info
->is_anycast_mac
= is_anycast_mac
;
5832 /* copy anycast MAC from VRR MAC */
5833 memcpy(&bgp_vrf
->rmac
, vrr_rmac
, ETH_ALEN
);
5834 /* copy sys RMAC from SVI MAC */
5835 memcpy(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, svi_rmac
, ETH_ALEN
);
5836 /* PIP user configured mac is not present use svi mac as sys mac */
5837 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
))
5838 memcpy(&bgp_vrf
->evpn_info
->pip_rmac
, svi_rmac
, ETH_ALEN
);
5840 if (bgp_debug_zebra(NULL
)) {
5841 char buf
[ETHER_ADDR_STRLEN
];
5842 char buf1
[ETHER_ADDR_STRLEN
];
5843 char buf2
[ETHER_ADDR_STRLEN
];
5845 zlog_debug("VRF %s vni %u pip %s RMAC %s sys RMAC %s static RMAC %s is_anycast_mac %s",
5846 vrf_id_to_name(bgp_vrf
->vrf_id
),
5848 bgp_vrf
->evpn_info
->advertise_pip
? "enable"
5850 prefix_mac2str(&bgp_vrf
->rmac
, buf
, sizeof(buf
)),
5851 prefix_mac2str(&bgp_vrf
->evpn_info
->pip_rmac
,
5852 buf1
, sizeof(buf1
)),
5853 prefix_mac2str(&bgp_vrf
->evpn_info
->pip_rmac_static
,
5854 buf2
, sizeof(buf2
)),
5855 is_anycast_mac
? "Enable" : "Disable");
5857 /* set the right filter - are we using l3vni only for prefix routes? */
5859 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5862 * VNI_FLAG_USE_TWO_LABELS flag for linked L2VNIs should not be
5863 * set before linking vrf to L3VNI. Thus, no need to clear
5867 UNSET_FLAG(bgp_vrf
->vrf_flags
,
5868 BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5870 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5871 if (!CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
5874 * If we are flapping VNI_FLAG_USE_TWO_LABELS
5875 * flag, update all MACIP routes in this VNI
5877 SET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5878 update_all_type2_routes(bgp_evpn
, vpn
);
5883 /* Map auto derive or configured RTs */
5884 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5885 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5887 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
5889 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5890 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5892 /* auto derive RD */
5893 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5895 /* link all corresponding l2vnis */
5896 hash_iterate(bgp_evpn
->vnihash
,
5897 (void (*)(struct hash_bucket
*,
5898 void *))link_l2vni_hash_to_l3vni
,
5901 /* Only update all corresponding type-2 routes if we are advertising two
5902 * labels along with type-2 routes
5905 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5906 update_routes_for_vni(bgp_evpn
, vpn
);
5908 /* advertise type-5 routes if needed */
5909 update_advertise_vrf_routes(bgp_vrf
);
5911 /* install all remote routes belonging to this l3vni into correspondng
5913 install_routes_for_vrf(bgp_vrf
);
5918 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5920 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5921 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5922 struct listnode
*node
= NULL
;
5923 struct listnode
*next
= NULL
;
5924 struct bgpevpn
*vpn
= NULL
;
5926 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5930 "Cannot process L3VNI %u Del - Could not find BGP instance",
5935 bgp_evpn
= bgp_get_evpn();
5939 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
5944 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5945 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5946 * routes. This will uninstalling the routes from zebra and decremnt the
5949 uninstall_routes_for_vrf(bgp_vrf
);
5951 /* delete/withdraw all type-5 routes */
5952 delete_withdraw_vrf_routes(bgp_vrf
);
5954 /* remove the l3vni from vrf instance */
5957 /* remove the Rmac from the BGP vrf */
5958 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5959 memset(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, 0, ETH_ALEN
);
5960 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
) &&
5961 !is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac
))
5962 memset(&bgp_vrf
->evpn_info
->pip_rmac
, 0, ETH_ALEN
);
5964 /* remove default import RT or Unmap non-default import RT */
5965 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5966 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5967 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5968 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5971 /* remove default export RT */
5972 if (!list_isempty(bgp_vrf
->vrf_export_rtl
) &&
5973 !CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
5974 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5977 /* update all corresponding local mac-ip routes */
5978 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5979 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5980 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5981 update_routes_for_vni(bgp_evpn
, vpn
);
5985 /* If any L2VNIs point to this instance, unlink them. */
5986 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5987 bgpevpn_unlink_from_l3vni(vpn
);
5989 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5991 /* Delete the instance if it was autocreated */
5992 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5993 bgp_delete(bgp_vrf
);
5999 * Handle del of a local VNI.
6001 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
6003 struct bgpevpn
*vpn
;
6005 /* Locate VNI hash */
6006 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
6008 if (bgp_debug_zebra(NULL
))
6010 EC_BGP_EVPN_VPN_VNI
,
6011 "%u: VNI hash entry for VNI %u not found at DEL",
6016 /* Remove all local EVPN routes and schedule for processing (to
6017 * withdraw from peers).
6019 delete_routes_for_vni(bgp
, vpn
);
6022 * tunnel is no longer active, del tunnel ip address from tip_hash
6024 bgp_tip_del(bgp
, &vpn
->originator_ip
);
6026 /* Clear "live" flag and see if hash needs to be freed. */
6027 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
6028 if (!is_vni_configured(vpn
))
6029 bgp_evpn_free(bgp
, vpn
);
6035 * Handle add (or update) of a local VNI. The VNI changes we care
6036 * about are for the local-tunnel-ip and the (tenant) VRF.
6038 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
6039 struct in_addr originator_ip
,
6040 vrf_id_t tenant_vrf_id
,
6041 struct in_addr mcast_grp
)
6044 struct bgpevpn
*vpn
;
6045 struct prefix_evpn p
;
6047 /* Lookup VNI. If present and no change, exit. */
6048 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
6051 if (is_vni_live(vpn
)
6052 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
6053 && IPV4_ADDR_SAME(&vpn
->mcast_grp
, &mcast_grp
)
6054 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
6055 /* Probably some other param has changed that we don't
6059 bgp_evpn_mcast_grp_change(bgp
, vpn
, mcast_grp
);
6061 /* Update tenant_vrf_id if it has changed. */
6062 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
6063 bgpevpn_unlink_from_l3vni(vpn
);
6064 vpn
->tenant_vrf_id
= tenant_vrf_id
;
6065 bgpevpn_link_to_l3vni(vpn
);
6068 /* If tunnel endpoint IP has changed, update (and delete prior
6069 * type-3 route, if needed.)
6071 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
6072 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
6074 /* Update all routes with new endpoint IP and/or export RT
6077 if (is_vni_live(vpn
))
6078 update_routes_for_vni(bgp
, vpn
);
6081 /* Create or update as appropriate. */
6083 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
,
6088 "%u: Failed to allocate VNI entry for VNI %u - at Add",
6094 /* if the VNI is live already, there is nothing more to do */
6095 if (is_vni_live(vpn
))
6098 /* Mark as "live" */
6099 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
6101 /* tunnel is now active, add tunnel-ip to db */
6102 bgp_tip_add(bgp
, &originator_ip
);
6104 /* filter routes as nexthop database has changed */
6105 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
6108 * Create EVPN type-3 route and schedule for processing.
6110 * RT-3 only if doing head-end replication
6112 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
6113 == VXLAN_FLOOD_HEAD_END_REPL
) {
6114 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
6115 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0)) {
6116 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
6117 "%u: Type3 route creation failure for VNI %u",
6123 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
6127 install_routes_for_vni(bgp
, vpn
);
6129 /* If we are advertising gateway mac-ip
6130 It needs to be conveyed again to zebra */
6131 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
6133 /* advertise svi mac-ip knob to zebra */
6134 bgp_zebra_advertise_svi_macip(bgp
, vpn
->advertise_svi_macip
, vpn
->vni
);
6140 * bgp_evpn_local_es_del
6142 int bgp_evpn_local_es_del(struct bgp
*bgp
,
6144 struct ipaddr
*originator_ip
)
6146 char buf
[ESI_STR_LEN
];
6147 struct evpnes
*es
= NULL
;
6149 if (!bgp
->esihash
) {
6150 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
6155 /* Lookup ESI hash - should exist. */
6156 es
= bgp_evpn_lookup_es(bgp
, esi
);
6158 flog_warn(EC_BGP_EVPN_ESI
,
6159 "%u: ESI hash entry for ESI %s at Local ES DEL",
6160 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
6164 /* Delete all local EVPN ES routes from ESI table
6165 * and schedule for processing (to withdraw from peers))
6167 delete_routes_for_es(bgp
, es
);
6169 /* free the hash entry */
6170 bgp_evpn_es_free(bgp
, es
);
6176 * bgp_evpn_local_es_add
6178 int bgp_evpn_local_es_add(struct bgp
*bgp
,
6180 struct ipaddr
*originator_ip
)
6182 char buf
[ESI_STR_LEN
];
6183 struct evpnes
*es
= NULL
;
6184 struct prefix_evpn p
;
6186 if (!bgp
->esihash
) {
6187 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
6192 /* create the new es */
6193 es
= bgp_evpn_lookup_es(bgp
, esi
);
6195 es
= bgp_evpn_es_new(bgp
, esi
, originator_ip
);
6199 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
6200 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
6204 UNSET_FLAG(es
->flags
, EVPNES_REMOTE
);
6205 SET_FLAG(es
->flags
, EVPNES_LOCAL
);
6207 build_evpn_type4_prefix(&p
, esi
, originator_ip
->ipaddr_v4
);
6208 if (update_evpn_type4_route(bgp
, es
, &p
)) {
6209 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
6210 "%u: Type4 route creation failure for ESI %s",
6211 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
6215 /* import all remote ES routes in th ES table */
6216 install_routes_for_es(bgp
, es
);
6222 * Handle change in setting for BUM handling. The supported values
6223 * are head-end replication and dropping all BUM packets. Any change
6224 * should be registered with zebra. Also, if doing head-end replication,
6225 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
6226 * to be dropped, the RT-3s must be withdrawn.
6228 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
6230 zlog_info("L2VPN EVPN BUM handling is %s",
6231 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
6232 "Flooding" : "Flooding Disabled");
6234 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
6235 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
6236 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
6237 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
6238 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
6242 * Cleanup EVPN information on disable - Need to delete and withdraw
6243 * EVPN routes from peers.
6245 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
6247 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_bucket
*,
6248 void *))cleanup_vni_on_disable
,
6253 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
6254 * BGP instance (default) is being freed.
6256 void bgp_evpn_cleanup(struct bgp
*bgp
)
6258 hash_iterate(bgp
->vnihash
,
6259 (void (*)(struct hash_bucket
*, void *))free_vni_entry
,
6262 hash_free(bgp
->import_rt_hash
);
6263 bgp
->import_rt_hash
= NULL
;
6265 hash_free(bgp
->vrf_import_rt_hash
);
6266 bgp
->vrf_import_rt_hash
= NULL
;
6268 hash_free(bgp
->vnihash
);
6269 bgp
->vnihash
= NULL
;
6271 hash_free(bgp
->esihash
);
6272 bgp
->esihash
= NULL
;
6274 list_delete(&bgp
->vrf_import_rtl
);
6275 list_delete(&bgp
->vrf_export_rtl
);
6276 list_delete(&bgp
->l2vnis
);
6280 * Initialization for EVPN
6283 * hash for RT to VNI
6285 void bgp_evpn_init(struct bgp
*bgp
)
6288 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
6290 hash_create(esi_hash_keymake
, esi_cmp
,
6291 "BGP EVPN Local ESI Hash");
6292 bgp
->import_rt_hash
=
6293 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
6294 "BGP Import RT Hash");
6295 bgp
->vrf_import_rt_hash
=
6296 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
6297 "BGP VRF Import RT Hash");
6298 bgp
->vrf_import_rtl
= list_new();
6299 bgp
->vrf_import_rtl
->cmp
=
6300 (int (*)(void *, void *))evpn_route_target_cmp
;
6301 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
6302 bgp
->vrf_export_rtl
= list_new();
6303 bgp
->vrf_export_rtl
->cmp
=
6304 (int (*)(void *, void *))evpn_route_target_cmp
;
6305 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
6306 bgp
->l2vnis
= list_new();
6307 bgp
->l2vnis
->cmp
= vni_list_cmp
;
6308 /* By default Duplicate Address Dection is enabled.
6309 * Max-moves (N) 5, detection time (M) 180
6310 * default action is warning-only
6311 * freeze action permanently freezes address,
6312 * and freeze time (auto-recovery) is disabled.
6314 if (bgp
->evpn_info
) {
6315 bgp
->evpn_info
->dup_addr_detect
= true;
6316 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
6317 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
6318 bgp
->evpn_info
->dad_freeze
= false;
6319 bgp
->evpn_info
->dad_freeze_time
= 0;
6320 /* Initialize zebra vxlan */
6321 bgp_zebra_dup_addr_detection(bgp
);
6322 /* Enable PIP feature by default for bgp vrf instance */
6323 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
6324 struct bgp
*bgp_default
;
6326 bgp
->evpn_info
->advertise_pip
= true;
6327 bgp_default
= bgp_get_default();
6329 bgp
->evpn_info
->pip_ip
= bgp_default
->router_id
;
6333 /* Default BUM handling is to do head-end replication. */
6334 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
6337 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
6339 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
6343 * Get the prefixlen of the ip prefix carried within the type5 evpn route.
6345 int bgp_evpn_get_type5_prefixlen(const struct prefix
*pfx
)
6347 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6349 if (!pfx
|| pfx
->family
!= AF_EVPN
)
6352 if (evp
->prefix
.route_type
!= BGP_EVPN_IP_PREFIX_ROUTE
)
6355 return evp
->prefix
.prefix_addr
.ip_prefix_length
;
6359 * Should we register nexthop for this EVPN prefix for nexthop tracking?
6361 bool bgp_evpn_is_prefix_nht_supported(const struct prefix
*pfx
)
6363 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6366 * EVPN routes should be marked as valid only if the nexthop is
6367 * reachable. Only if this happens, the route should be imported
6368 * (into VNI or VRF routing tables) and/or advertised.
6369 * Note: This is currently applied for EVPN type-2, type-3 and
6370 * type-5 routes. It may be tweaked later on for other routes, or
6371 * even removed completely when all routes are handled.
6373 if (pfx
&& pfx
->family
== AF_EVPN
&&
6374 (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
6375 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
6376 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))