1 /* Ethernet-VPN Packet and vty Processing File
2 * Copyright (C) 2016 6WIND
3 * Copyright (C) 2017 Cumulus Networks, Inc.
5 * This file is part of FRR.
7 * FRRouting is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * FRRouting is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "bgpd/bgp_attr_evpn.h"
35 #include "bgpd/bgpd.h"
36 #include "bgpd/bgp_table.h"
37 #include "bgpd/bgp_route.h"
38 #include "bgpd/bgp_attr.h"
39 #include "bgpd/bgp_mplsvpn.h"
40 #include "bgpd/bgp_label.h"
41 #include "bgpd/bgp_evpn.h"
42 #include "bgpd/bgp_evpn_private.h"
43 #include "bgpd/bgp_ecommunity.h"
44 #include "bgpd/bgp_encap_types.h"
45 #include "bgpd/bgp_debug.h"
46 #include "bgpd/bgp_errors.h"
47 #include "bgpd/bgp_aspath.h"
48 #include "bgpd/bgp_zebra.h"
49 #include "bgpd/bgp_nexthop.h"
50 #include "bgpd/bgp_addpath.h"
51 #include "bgpd/bgp_mac.h"
52 #include "bgpd/bgp_vty.h"
55 * Definitions and external declarations.
57 extern struct zclient
*zclient
;
59 DEFINE_QOBJ_TYPE(bgpevpn
)
60 DEFINE_QOBJ_TYPE(evpnes
)
64 * Static function declarations
66 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
68 struct bgp_path_info
**pi
);
69 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
75 /* compare two IPV4 VTEP IPs */
76 static int evpn_vtep_ip_cmp(void *p1
, void *p2
)
78 const struct in_addr
*ip1
= p1
;
79 const struct in_addr
*ip2
= p2
;
81 return ip1
->s_addr
- ip2
->s_addr
;
85 * Make hash key for ESI.
87 static unsigned int esi_hash_keymake(const void *p
)
89 const struct evpnes
*pes
= p
;
90 const void *pnt
= (void *)pes
->esi
.val
;
92 return jhash(pnt
, ESI_BYTES
, 0xa5a5a55a);
98 static bool esi_cmp(const void *p1
, const void *p2
)
100 const struct evpnes
*pes1
= p1
;
101 const struct evpnes
*pes2
= p2
;
103 if (pes1
== NULL
&& pes2
== NULL
)
106 if (pes1
== NULL
|| pes2
== NULL
)
109 return (memcmp(pes1
->esi
.val
, pes2
->esi
.val
, ESI_BYTES
) == 0);
115 static unsigned int vni_hash_key_make(const void *p
)
117 const struct bgpevpn
*vpn
= p
;
118 return (jhash_1word(vpn
->vni
, 0));
122 * Comparison function for vni hash
124 static bool vni_hash_cmp(const void *p1
, const void *p2
)
126 const struct bgpevpn
*vpn1
= p1
;
127 const struct bgpevpn
*vpn2
= p2
;
133 return (vpn1
->vni
== vpn2
->vni
);
136 static int vni_list_cmp(void *p1
, void *p2
)
138 const struct bgpevpn
*vpn1
= p1
;
139 const struct bgpevpn
*vpn2
= p2
;
141 return vpn1
->vni
- vpn2
->vni
;
145 * Make vrf import route target hash key.
147 static unsigned int vrf_import_rt_hash_key_make(const void *p
)
149 const struct vrf_irt_node
*irt
= p
;
150 const char *pnt
= irt
->rt
.val
;
152 return jhash(pnt
, 8, 0x5abc1234);
156 * Comparison function for vrf import rt hash
158 static bool vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
160 const struct vrf_irt_node
*irt1
= p1
;
161 const struct vrf_irt_node
*irt2
= p2
;
163 if (irt1
== NULL
&& irt2
== NULL
)
166 if (irt1
== NULL
|| irt2
== NULL
)
169 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
173 * Create a new vrf import_rt in evpn instance
175 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
177 struct bgp
*bgp_evpn
= NULL
;
178 struct vrf_irt_node
*irt
;
180 bgp_evpn
= bgp_get_evpn();
182 flog_err(EC_BGP_NO_DFLT
,
183 "vrf import rt new - evpn instance not created yet");
187 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
188 sizeof(struct vrf_irt_node
));
191 irt
->vrfs
= list_new();
194 if (!hash_get(bgp_evpn
->vrf_import_rt_hash
, irt
, hash_alloc_intern
)) {
195 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
203 * Free the vrf import rt node
205 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
207 struct bgp
*bgp_evpn
= NULL
;
209 bgp_evpn
= bgp_get_evpn();
211 flog_err(EC_BGP_NO_DFLT
,
212 "vrf import rt free - evpn instance not created yet");
216 hash_release(bgp_evpn
->vrf_import_rt_hash
, irt
);
217 list_delete(&irt
->vrfs
);
218 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
222 * Function to lookup Import RT node - used to map a RT to set of
223 * VNIs importing routes with that RT.
225 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
227 struct bgp
*bgp_evpn
= NULL
;
228 struct vrf_irt_node
*irt
;
229 struct vrf_irt_node tmp
;
231 bgp_evpn
= bgp_get_evpn();
235 "vrf import rt lookup - evpn instance not created yet");
239 memset(&tmp
, 0, sizeof(struct vrf_irt_node
));
240 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
241 irt
= hash_lookup(bgp_evpn
->vrf_import_rt_hash
, &tmp
);
246 * Is specified VRF present on the RT's list of "importing" VRFs?
248 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
, struct bgp
*bgp_vrf
)
250 struct listnode
*node
= NULL
, *nnode
= NULL
;
251 struct bgp
*tmp_bgp_vrf
= NULL
;
253 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
254 if (tmp_bgp_vrf
== bgp_vrf
)
261 * Make import route target hash key.
263 static unsigned int import_rt_hash_key_make(const void *p
)
265 const struct irt_node
*irt
= p
;
266 const char *pnt
= irt
->rt
.val
;
268 return jhash(pnt
, 8, 0xdeadbeef);
272 * Comparison function for import rt hash
274 static bool import_rt_hash_cmp(const void *p1
, const void *p2
)
276 const struct irt_node
*irt1
= p1
;
277 const struct irt_node
*irt2
= p2
;
279 if (irt1
== NULL
&& irt2
== NULL
)
282 if (irt1
== NULL
|| irt2
== NULL
)
285 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
289 * Create a new import_rt
291 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
292 struct ecommunity_val
*rt
)
294 struct irt_node
*irt
;
299 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
302 irt
->vnis
= list_new();
305 if (!hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
)) {
306 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
314 * Free the import rt node
316 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
318 hash_release(bgp
->import_rt_hash
, irt
);
319 list_delete(&irt
->vnis
);
320 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
324 * Function to lookup Import RT node - used to map a RT to set of
325 * VNIs importing routes with that RT.
327 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
328 struct ecommunity_val
*rt
)
330 struct irt_node
*irt
;
333 memset(&tmp
, 0, sizeof(struct irt_node
));
334 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
335 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
340 * Is specified VNI present on the RT's list of "importing" VNIs?
342 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
344 struct listnode
*node
, *nnode
;
345 struct bgpevpn
*tmp_vpn
;
347 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
356 * Compare Route Targets.
358 static int evpn_route_target_cmp(struct ecommunity
*ecom1
,
359 struct ecommunity
*ecom2
)
367 if (!ecom1
&& !ecom2
)
370 if (ecom1
->str
&& !ecom2
->str
)
373 if (!ecom1
->str
&& ecom2
->str
)
376 if (!ecom1
->str
&& !ecom2
->str
)
379 return strcmp(ecom1
->str
, ecom2
->str
);
382 static void evpn_xxport_delete_ecomm(void *val
)
384 struct ecommunity
*ecomm
= val
;
385 ecommunity_free(&ecomm
);
389 * Mask off global-admin field of specified extended community (RT),
390 * just retain the local-admin field.
392 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
393 struct ecommunity_val
*src
)
399 if (type
== ECOMMUNITY_ENCODE_AS
) {
400 dst
->val
[2] = dst
->val
[3] = 0;
401 } else if (type
== ECOMMUNITY_ENCODE_AS4
402 || type
== ECOMMUNITY_ENCODE_IP
) {
403 dst
->val
[2] = dst
->val
[3] = 0;
404 dst
->val
[4] = dst
->val
[5] = 0;
409 * Map one RT to specified VRF.
410 * bgp_vrf = BGP vrf instance
412 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct ecommunity_val
*eval
)
414 struct vrf_irt_node
*irt
= NULL
;
415 struct ecommunity_val eval_tmp
;
417 /* If using "automatic" RT,
418 * we only care about the local-admin sub-field.
419 * This is to facilitate using L3VNI(VRF-VNI)
420 * as the RT for EBGP peering too.
422 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
423 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
424 mask_ecom_global_admin(&eval_tmp
, eval
);
426 irt
= lookup_vrf_import_rt(&eval_tmp
);
427 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
428 /* Already mapped. */
432 irt
= vrf_import_rt_new(&eval_tmp
);
434 /* Add VRF to the list for this RT. */
435 listnode_add(irt
->vrfs
, bgp_vrf
);
439 * Unmap specified VRF from specified RT. If there are no other
440 * VRFs for this RT, then the RT hash is deleted.
441 * bgp_vrf: BGP VRF specific instance
443 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
, struct vrf_irt_node
*irt
)
445 /* Delete VRF from list for this RT. */
446 listnode_delete(irt
->vrfs
, bgp_vrf
);
447 if (!listnode_head(irt
->vrfs
)) {
448 vrf_import_rt_free(irt
);
453 * Map one RT to specified VNI.
455 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
456 struct ecommunity_val
*eval
)
458 struct irt_node
*irt
;
459 struct ecommunity_val eval_tmp
;
461 /* If using "automatic" RT, we only care about the local-admin
463 * This is to facilitate using VNI as the RT for EBGP peering too.
465 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
466 if (!is_import_rt_configured(vpn
))
467 mask_ecom_global_admin(&eval_tmp
, eval
);
469 irt
= lookup_import_rt(bgp
, &eval_tmp
);
471 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
472 /* Already mapped. */
476 irt
= import_rt_new(bgp
, &eval_tmp
);
480 /* Add VNI to the hash list for this RT. */
481 listnode_add(irt
->vnis
, vpn
);
485 * Unmap specified VNI from specified RT. If there are no other
486 * VNIs for this RT, then the RT hash is deleted.
488 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
489 struct irt_node
*irt
)
491 /* Delete VNI from hash list for this RT. */
492 listnode_delete(irt
->vnis
, vpn
);
493 if (!listnode_head(irt
->vnis
)) {
494 import_rt_free(bgp
, irt
);
498 static void bgp_evpn_get_rmac_nexthop(struct bgpevpn
*vpn
,
499 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
) {
904 ecom_val_ptr
= (uint8_t *)(attr
->ecommunity
->val
911 /* Update the existing MM ecommunity */
913 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
915 /* Add MM to existing */
917 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
919 ecom_tmp
.val
= (uint8_t *)eval
.val
;
921 if (attr
->ecommunity
)
923 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
925 attr
->ecommunity
= ecommunity_dup(&ecom_tmp
);
929 /* Install EVPN route into zebra. */
930 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
931 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
,
994 struct bgp_path_info
*old_local
)
996 struct bgp_node
*global_rn
;
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_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1006 bgp_node_get_prefix(rn
), &vpn
->prd
);
1008 /* Delete route entry in the global EVPN table. */
1009 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1011 /* Schedule for processing - withdraws to peers happen from
1015 bgp_process(bgp
, global_rn
, afi
, safi
);
1016 bgp_unlock_node(global_rn
);
1019 /* Delete route entry in the VNI route table, caller to remove. */
1020 bgp_path_info_delete(rn
, 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
,
1102 struct bgp_node
*rn
)
1105 afi_t afi
= AFI_L2VPN
;
1106 safi_t safi
= SAFI_EVPN
;
1107 struct bgp_path_info
*old_select
; /* old best */
1108 struct bgp_path_info
*new_select
; /* new best */
1109 struct bgp_path_info_pair old_and_new
;
1111 /* Compute the best path. */
1112 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
],
1113 &old_and_new
, afi
, safi
);
1114 old_select
= old_and_new
.old
;
1115 new_select
= old_and_new
.new;
1118 * If the best path hasn't changed - see if something needs to be
1121 if (old_select
&& old_select
== new_select
1122 && old_select
->type
== ZEBRA_ROUTE_BGP
1123 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1124 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1125 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1126 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1127 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1128 ret
= evpn_es_install_vtep(bgp
, es
,
1129 (const struct prefix_evpn
*)bgp_node_get_prefix(rn
),
1130 old_select
->attr
->nexthop
);
1132 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1133 UNSET_FLAG(old_select
->flags
, BGP_PATH_LINK_BW_CHG
);
1134 bgp_zebra_clear_route_change_flags(rn
);
1138 /* If the user did a "clear" this flag will be set */
1139 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1142 * bestpath has changed; update relevant fields and install or uninstall
1143 * into the zebra RIB.
1145 if (old_select
|| new_select
)
1146 bgp_bump_version(rn
);
1149 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1151 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1152 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1153 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1154 UNSET_FLAG(new_select
->flags
, BGP_PATH_LINK_BW_CHG
);
1157 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1158 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1159 ret
= evpn_es_install_vtep(bgp
, es
,
1160 (const struct prefix_evpn
*)bgp_node_get_prefix(rn
),
1161 new_select
->attr
->nexthop
);
1163 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1164 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1165 ret
= evpn_es_uninstall_vtep(
1166 bgp
, es
, (struct prefix_evpn
*)bgp_node_get_prefix(rn
),
1167 old_select
->attr
->nexthop
);
1170 /* Clear any route change flags. */
1171 bgp_zebra_clear_route_change_flags(rn
);
1173 /* Reap old select bgp_path_info, if it has been removed */
1174 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1175 bgp_path_info_reap(rn
, old_select
);
1181 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1184 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1185 struct bgp_node
*rn
)
1187 struct bgp_path_info
*old_select
, *new_select
;
1188 struct bgp_path_info_pair old_and_new
;
1189 afi_t afi
= AFI_L2VPN
;
1190 safi_t safi
= SAFI_EVPN
;
1193 /* Compute the best path. */
1194 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1196 old_select
= old_and_new
.old
;
1197 new_select
= old_and_new
.new;
1199 /* If the best path hasn't changed - see if there is still something to
1203 if (old_select
&& old_select
== new_select
1204 && old_select
->type
== ZEBRA_ROUTE_BGP
1205 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1206 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1207 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1208 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1209 if (bgp_zebra_has_route_changed(rn
, old_select
))
1210 ret
= evpn_zebra_install(
1211 bgp
, vpn
, (const struct prefix_evpn
*)bgp_node_get_prefix(rn
),
1213 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1214 UNSET_FLAG(old_select
->flags
, BGP_PATH_LINK_BW_CHG
);
1215 bgp_zebra_clear_route_change_flags(rn
);
1219 /* If the user did a "clear" this flag will be set */
1220 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1222 /* bestpath has changed; update relevant fields and install or uninstall
1223 * into the zebra RIB.
1225 if (old_select
|| new_select
)
1226 bgp_bump_version(rn
);
1229 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1231 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1232 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1233 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1234 UNSET_FLAG(new_select
->flags
, BGP_PATH_LINK_BW_CHG
);
1237 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1238 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1239 ret
= evpn_zebra_install(
1240 bgp
, vpn
, (struct prefix_evpn
*)bgp_node_get_prefix(rn
),
1243 /* If an old best existed and it was a "local" route, the only
1245 * it would be supplanted is due to MAC mobility procedures. So,
1247 * need to do an implicit delete and withdraw that route from
1250 if (old_select
&& old_select
->peer
== bgp
->peer_self
1251 && old_select
->type
== ZEBRA_ROUTE_BGP
1252 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
1253 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
1255 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1256 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1257 ret
= evpn_zebra_uninstall(
1259 (const struct prefix_evpn
*)bgp_node_get_prefix(
1261 old_select
->attr
->nexthop
);
1264 /* Clear any route change flags. */
1265 bgp_zebra_clear_route_change_flags(rn
);
1267 /* Reap old select bgp_path_info, if it has been removed */
1268 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1269 bgp_path_info_reap(rn
, old_select
);
1275 * Return true if the local ri for this rn is of type gateway mac
1277 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
1279 struct bgp_path_info
*tmp_pi
= NULL
;
1280 struct bgp_path_info
*local_pi
= NULL
;
1283 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1284 tmp_pi
= tmp_pi
->next
) {
1285 if (tmp_pi
->peer
== bgp
->peer_self
1286 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1287 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1294 return local_pi
->attr
->default_gw
;
1299 * Return true if the local ri for this rn has sticky set
1301 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
1303 struct bgp_path_info
*tmp_pi
;
1304 struct bgp_path_info
*local_pi
;
1307 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1308 tmp_pi
= tmp_pi
->next
) {
1309 if (tmp_pi
->peer
== bgp
->peer_self
1310 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1311 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1318 return local_pi
->attr
->sticky
;
1322 * create or update EVPN type4 route entry.
1323 * This could be in the ES table or the global table.
1324 * TODO: handle remote ES (type4) routes as well
1326 static int update_evpn_type4_route_entry(struct bgp
*bgp
, struct evpnes
*es
,
1327 afi_t afi
, safi_t safi
,
1328 struct bgp_node
*rn
, struct attr
*attr
,
1329 int add
, struct bgp_path_info
**ri
,
1332 char buf
[ESI_STR_LEN
];
1333 char buf1
[INET6_ADDRSTRLEN
];
1334 struct bgp_path_info
*tmp_pi
= NULL
;
1335 struct bgp_path_info
*local_pi
= NULL
; /* local route entry if any */
1336 struct bgp_path_info
*remote_pi
= NULL
; /* remote route entry if any */
1337 struct attr
*attr_new
= NULL
;
1338 const struct prefix_evpn
*evp
= NULL
;
1342 evp
= (const struct prefix_evpn
*)bgp_node_get_prefix(rn
);
1344 /* locate the local and remote entries if any */
1345 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1346 tmp_pi
= tmp_pi
->next
) {
1347 if (tmp_pi
->peer
== bgp
->peer_self
1348 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1349 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1351 if (tmp_pi
->type
== ZEBRA_ROUTE_BGP
1352 && tmp_pi
->sub_type
== BGP_ROUTE_IMPORTED
1353 && CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1357 /* we don't expect to see a remote_ri at this point.
1358 * An ES route has esi + vtep_ip as the key,
1359 * We shouldn't see the same route from any other vtep.
1364 "%u ERROR: local es route for ESI: %s Vtep %s also learnt from remote",
1366 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1367 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1371 if (!local_pi
&& !add
)
1374 /* create or update the entry */
1377 /* Add or update attribute to hash */
1378 attr_new
= bgp_attr_intern(attr
);
1380 /* Create new route with its attribute. */
1381 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1382 bgp
->peer_self
, attr_new
, rn
);
1383 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1385 /* add the newly created path to the route-node */
1386 bgp_path_info_add(rn
, tmp_pi
);
1389 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1390 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1393 /* The attribute has changed.
1394 * Add (or update) attribute to hash. */
1395 attr_new
= bgp_attr_intern(attr
);
1396 bgp_path_info_set_flag(rn
, tmp_pi
,
1397 BGP_PATH_ATTR_CHANGED
);
1399 /* Restore route, if needed. */
1400 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1401 bgp_path_info_restore(rn
, tmp_pi
);
1403 /* Unintern existing, set to new. */
1404 bgp_attr_unintern(&tmp_pi
->attr
);
1405 tmp_pi
->attr
= attr_new
;
1406 tmp_pi
->uptime
= bgp_clock();
1410 /* Return back the route entry. */
1415 /* update evpn es (type-4) route */
1416 static int update_evpn_type4_route(struct bgp
*bgp
,
1418 struct prefix_evpn
*p
)
1421 int route_changed
= 0;
1422 char buf
[ESI_STR_LEN
];
1423 char buf1
[INET6_ADDRSTRLEN
];
1424 afi_t afi
= AFI_L2VPN
;
1425 safi_t safi
= SAFI_EVPN
;
1427 struct attr
*attr_new
= NULL
;
1428 struct bgp_node
*rn
= NULL
;
1429 struct bgp_path_info
*pi
= NULL
;
1431 memset(&attr
, 0, sizeof(struct attr
));
1433 /* Build path-attribute for this route. */
1434 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1435 attr
.nexthop
= es
->originator_ip
.ipaddr_v4
;
1436 attr
.mp_nexthop_global_in
= es
->originator_ip
.ipaddr_v4
;
1437 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1439 /* Set up extended community. */
1440 build_evpn_type4_route_extcomm(es
, &attr
);
1442 /* First, create (or fetch) route node within the ESI. */
1443 /* NOTE: There is no RD here. */
1444 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
1446 /* Create or update route entry. */
1447 ret
= update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, &attr
, 1,
1448 &pi
, &route_changed
);
1450 flog_err(EC_BGP_ES_INVALID
,
1451 "%u ERROR: Failed to updated ES route ESI: %s VTEP %s",
1453 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1454 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1458 attr_new
= pi
->attr
;
1460 /* Perform route selection;
1461 * this is just to set the flags correctly
1462 * as local route in the ES always wins.
1464 evpn_es_route_select_install(bgp
, es
, rn
);
1465 bgp_unlock_node(rn
);
1467 /* If this is a new route or some attribute has changed, export the
1468 * route to the global table. The route will be advertised to peers
1469 * from there. Note that this table is a 2-level tree (RD-level +
1470 * Prefix-level) similar to L3VPN routes.
1472 if (route_changed
) {
1473 struct bgp_path_info
*global_pi
;
1475 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1476 (struct prefix
*)p
, &es
->prd
);
1477 update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, attr_new
,
1478 1, &global_pi
, &route_changed
);
1480 /* Schedule for processing and unlock node. */
1481 bgp_process(bgp
, rn
, afi
, safi
);
1482 bgp_unlock_node(rn
);
1485 /* Unintern temporary. */
1486 aspath_unintern(&attr
.aspath
);
1490 static int update_evpn_type5_route_entry(struct bgp
*bgp_evpn
,
1491 struct bgp
*bgp_vrf
, afi_t afi
,
1492 safi_t safi
, struct bgp_node
*rn
,
1493 struct attr
*attr
, int *route_changed
)
1495 struct attr
*attr_new
= NULL
;
1496 struct bgp_path_info
*pi
= NULL
;
1497 mpls_label_t label
= MPLS_INVALID_LABEL
;
1498 struct bgp_path_info
*local_pi
= NULL
;
1499 struct bgp_path_info
*tmp_pi
= NULL
;
1502 /* locate the local route entry if any */
1503 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1504 tmp_pi
= tmp_pi
->next
) {
1505 if (tmp_pi
->peer
== bgp_evpn
->peer_self
1506 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1507 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1512 * create a new route entry if one doesn't exist.
1513 * Otherwise see if route attr has changed
1517 /* route has changed as this is the first entry */
1520 /* Add (or update) attribute to hash. */
1521 attr_new
= bgp_attr_intern(attr
);
1523 /* create the route info from attribute */
1524 pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1525 bgp_evpn
->peer_self
, attr_new
, rn
);
1526 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
1528 /* Type-5 routes advertise the L3-VNI */
1529 bgp_path_info_extra_get(pi
);
1530 vni2label(bgp_vrf
->l3vni
, &label
);
1531 memcpy(&pi
->extra
->label
, &label
, sizeof(label
));
1532 pi
->extra
->num_labels
= 1;
1534 /* add the route entry to route node*/
1535 bgp_path_info_add(rn
, pi
);
1539 if (!attrhash_cmp(tmp_pi
->attr
, attr
)) {
1541 /* attribute changed */
1544 /* The attribute has changed. */
1545 /* Add (or update) attribute to hash. */
1546 attr_new
= bgp_attr_intern(attr
);
1547 bgp_path_info_set_flag(rn
, tmp_pi
,
1548 BGP_PATH_ATTR_CHANGED
);
1550 /* Restore route, if needed. */
1551 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1552 bgp_path_info_restore(rn
, tmp_pi
);
1554 /* Unintern existing, set to new. */
1555 bgp_attr_unintern(&tmp_pi
->attr
);
1556 tmp_pi
->attr
= attr_new
;
1557 tmp_pi
->uptime
= bgp_clock();
1563 /* update evpn type-5 route entry */
1564 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1565 struct attr
*src_attr
)
1567 afi_t afi
= AFI_L2VPN
;
1568 safi_t safi
= SAFI_EVPN
;
1570 struct bgp_node
*rn
= NULL
;
1571 struct bgp
*bgp_evpn
= NULL
;
1572 int route_changed
= 0;
1574 bgp_evpn
= bgp_get_evpn();
1578 /* Build path attribute for this route - use the source attr, if
1579 * present, else treat as locally originated.
1584 memset(&attr
, 0, sizeof(struct attr
));
1585 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1588 /* Advertise Primary IP (PIP) is enabled, send individual
1589 * IP (default instance router-id) as nexthop.
1590 * PIP is disabled or vrr interface is not present
1591 * use anycast-IP as nexthop and anycast RMAC.
1593 if (!bgp_vrf
->evpn_info
->advertise_pip
||
1594 (!bgp_vrf
->evpn_info
->is_anycast_mac
)) {
1595 attr
.nexthop
= bgp_vrf
->originator_ip
;
1596 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1597 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, ETH_ALEN
);
1600 memcpy(&attr
.rmac
, &bgp_vrf
->evpn_info
->pip_rmac
, ETH_ALEN
);
1601 if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
!= INADDR_ANY
) {
1602 attr
.nexthop
= bgp_vrf
->evpn_info
->pip_ip
;
1603 attr
.mp_nexthop_global_in
= bgp_vrf
->evpn_info
->pip_ip
;
1604 } else if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
== INADDR_ANY
)
1605 if (bgp_debug_zebra(NULL
)) {
1606 char buf1
[PREFIX_STRLEN
];
1608 zlog_debug("VRF %s evp %s advertise-pip primary ip is not configured",
1609 vrf_id_to_name(bgp_vrf
->vrf_id
),
1610 prefix2str(evp
, buf1
, sizeof(buf1
)));
1614 if (bgp_debug_zebra(NULL
)) {
1615 char buf
[ETHER_ADDR_STRLEN
];
1616 char buf1
[PREFIX_STRLEN
];
1617 char buf2
[INET6_ADDRSTRLEN
];
1619 zlog_debug("VRF %s type-5 route evp %s RMAC %s nexthop %s",
1620 vrf_id_to_name(bgp_vrf
->vrf_id
),
1621 prefix2str(evp
, buf1
, sizeof(buf1
)),
1622 prefix_mac2str(&attr
.rmac
, buf
, sizeof(buf
)),
1623 inet_ntop(AF_INET
, &attr
.nexthop
, buf2
,
1627 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1629 /* Setup RT and encap extended community */
1630 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1632 /* get the route node in global table */
1633 rn
= bgp_afi_node_get(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1634 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1637 /* create or update the route entry within the route node */
1638 update_evpn_type5_route_entry(bgp_evpn
, bgp_vrf
, afi
, safi
, rn
, &attr
,
1641 /* schedule for processing and unlock node */
1642 if (route_changed
) {
1643 bgp_process(bgp_evpn
, rn
, afi
, safi
);
1644 bgp_unlock_node(rn
);
1647 /* uninten temporary */
1649 aspath_unintern(&attr
.aspath
);
1654 * Create or update EVPN route entry. This could be in the VNI route table
1655 * or the global route table.
1657 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1658 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1659 struct attr
*attr
, int add
,
1660 struct bgp_path_info
**pi
, uint8_t flags
,
1663 struct bgp_path_info
*tmp_pi
;
1664 struct bgp_path_info
*local_pi
;
1665 struct attr
*attr_new
;
1666 mpls_label_t label
[BGP_MAX_LABELS
];
1667 uint32_t num_labels
= 1;
1668 int route_change
= 1;
1670 const struct prefix_evpn
*evp
;
1673 evp
= (const struct prefix_evpn
*)bgp_node_get_prefix(rn
);
1674 memset(&label
, 0, sizeof(label
));
1676 /* See if this is an update of an existing route, or a new add. */
1678 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1679 tmp_pi
= tmp_pi
->next
) {
1680 if (tmp_pi
->peer
== bgp
->peer_self
1681 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1682 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1686 /* If route doesn't exist already, create a new one, if told to.
1687 * Otherwise act based on whether the attributes of the route have
1690 if (!local_pi
&& !add
)
1693 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1694 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1695 add_mac_mobility_to_attr(seq
, attr
);
1698 /* Add (or update) attribute to hash. */
1699 attr_new
= bgp_attr_intern(attr
);
1701 /* Extract MAC mobility sequence number, if any. */
1702 attr_new
->mm_seqnum
=
1703 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1704 attr_new
->sticky
= sticky
;
1706 /* Create new route with its attribute. */
1707 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1708 bgp
->peer_self
, attr_new
, rn
);
1709 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1710 bgp_path_info_extra_get(tmp_pi
);
1712 /* The VNI goes into the 'label' field of the route */
1713 vni2label(vpn
->vni
, &label
[0]);
1715 /* Type-2 routes may carry a second VNI - the L3-VNI.
1716 * Only attach second label if we are advertising two labels for
1719 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1720 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1723 l3vni
= bgpevpn_get_l3vni(vpn
);
1725 vni2label(l3vni
, &label
[1]);
1730 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1731 tmp_pi
->extra
->num_labels
= num_labels
;
1732 /* Mark route as self type-2 route */
1733 if (flags
&& CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1734 tmp_pi
->extra
->af_flags
= BGP_EVPN_MACIP_TYPE_SVI_IP
;
1735 bgp_path_info_add(rn
, tmp_pi
);
1738 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1739 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1743 * The attributes have changed, type-2 routes needs to
1744 * be advertised with right labels.
1746 vni2label(vpn
->vni
, &label
[0]);
1747 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1748 && CHECK_FLAG(vpn
->flags
,
1749 VNI_FLAG_USE_TWO_LABELS
)) {
1752 l3vni
= bgpevpn_get_l3vni(vpn
);
1754 vni2label(l3vni
, &label
[1]);
1758 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1759 tmp_pi
->extra
->num_labels
= num_labels
;
1761 /* The attribute has changed. */
1762 /* Add (or update) attribute to hash. */
1763 attr_new
= bgp_attr_intern(attr
);
1764 bgp_path_info_set_flag(rn
, tmp_pi
,
1765 BGP_PATH_ATTR_CHANGED
);
1767 /* Extract MAC mobility sequence number, if any. */
1768 attr_new
->mm_seqnum
=
1769 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1770 attr_new
->sticky
= sticky
;
1772 /* Restore route, if needed. */
1773 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1774 bgp_path_info_restore(rn
, tmp_pi
);
1776 /* Unintern existing, set to new. */
1777 bgp_attr_unintern(&tmp_pi
->attr
);
1778 tmp_pi
->attr
= attr_new
;
1779 tmp_pi
->uptime
= bgp_clock();
1783 /* Return back the route entry. */
1785 return route_change
;
1788 static void evpn_zebra_reinstall_best_route(struct bgp
*bgp
,
1789 struct bgpevpn
*vpn
, struct bgp_node
*rn
)
1791 struct bgp_path_info
*tmp_ri
;
1792 struct bgp_path_info
*curr_select
= NULL
;
1794 for (tmp_ri
= bgp_node_get_bgp_path_info(rn
);
1795 tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1796 if (CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_SELECTED
)) {
1797 curr_select
= tmp_ri
;
1802 if (curr_select
&& curr_select
->type
== ZEBRA_ROUTE_BGP
1803 && curr_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1804 evpn_zebra_install(bgp
, vpn
,
1805 (const struct prefix_evpn
*)bgp_node_get_prefix(rn
),
1810 * If the local route was not selected evict it and tell zebra to re-add
1811 * the best remote dest.
1813 * Typically a local path added by zebra is expected to be selected as
1814 * best. In which case when a remote path wins as best (later)
1815 * evpn_route_select_install itself evicts the older-local-best path.
1817 * However if bgp's add and zebra's add cross paths (race condition) it
1818 * is possible that the local path is no longer the "older" best path.
1819 * It is a path that was never designated as best and hence requires
1820 * additional handling to prevent bgp from injecting and holding on to a
1821 * non-best local path.
1823 static void evpn_cleanup_local_non_best_route(struct bgp
*bgp
,
1824 struct bgpevpn
*vpn
,
1825 struct bgp_node
*rn
,
1826 struct bgp_path_info
*local_pi
)
1828 /* local path was not picked as the winner; kick it out */
1829 if (bgp_debug_zebra(NULL
))
1830 zlog_debug("evicting local evpn prefix %pRN as remote won", rn
);
1832 evpn_delete_old_local_route(bgp
, vpn
, rn
, local_pi
);
1833 bgp_path_info_reap(rn
, local_pi
);
1835 /* tell zebra to re-add the best remote path */
1836 evpn_zebra_reinstall_best_route(bgp
, vpn
, rn
);
1840 * Create or update EVPN route (of type based on prefix) for specified VNI
1841 * and schedule for processing.
1843 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1844 struct prefix_evpn
*p
, uint8_t flags
,
1847 struct bgp_node
*rn
;
1849 struct attr
*attr_new
;
1850 int add_l3_ecomm
= 0;
1851 struct bgp_path_info
*pi
;
1852 afi_t afi
= AFI_L2VPN
;
1853 safi_t safi
= SAFI_EVPN
;
1856 memset(&attr
, 0, sizeof(struct attr
));
1858 /* Build path-attribute for this route. */
1859 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1860 attr
.nexthop
= vpn
->originator_ip
;
1861 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1862 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1863 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1864 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1865 attr
.router_flag
= CHECK_FLAG(flags
,
1866 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1868 /* PMSI is only needed for type-3 routes */
1869 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
1870 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1871 attr
.pmsi_tnl_type
= PMSI_TNLTYPE_INGR_REPL
;
1874 /* router mac is only needed for type-2 routes here. */
1875 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
1876 uint8_t af_flags
= 0;
1878 if (CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1879 SET_FLAG(af_flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
);
1881 bgp_evpn_get_rmac_nexthop(vpn
, p
, &attr
, af_flags
);
1883 if (bgp_debug_zebra(NULL
)) {
1884 char buf
[ETHER_ADDR_STRLEN
];
1885 char buf1
[PREFIX_STRLEN
];
1887 zlog_debug("VRF %s vni %u type-2 route evp %s RMAC %s nexthop %s",
1889 vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
) : " ",
1891 prefix2str(p
, buf1
, sizeof(buf1
)),
1892 prefix_mac2str(&attr
.rmac
, buf
,
1894 inet_ntoa(attr
.mp_nexthop_global_in
));
1898 vni2label(vpn
->vni
, &(attr
.label
));
1900 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1901 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1904 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1905 (is_evpn_prefix_ipaddr_v4(p
) ||
1906 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1907 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1908 bgpevpn_get_l3vni(vpn
))
1911 /* Set up extended community. */
1912 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1914 /* First, create (or fetch) route node within the VNI. */
1915 /* NOTE: There is no RD here. */
1916 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1918 /* Create or update route entry. */
1919 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1920 1, &pi
, flags
, seq
);
1922 attr_new
= pi
->attr
;
1924 /* lock ri to prevent freeing in evpn_route_select_install */
1925 bgp_path_info_lock(pi
);
1927 /* Perform route selection. Normally, the local route in the
1928 * VNI is expected to win and be the best route. However, if
1929 * there is a race condition where a host moved from local to
1930 * remote and the remote route was received in BGP just prior
1931 * to the local MACIP notification from zebra, the remote
1932 * route would win, and we should evict the defunct local route
1933 * and (re)install the remote route into zebra.
1935 evpn_route_select_install(bgp
, vpn
, rn
);
1937 * If the new local route was not selected evict it and tell zebra
1938 * to re-add the best remote dest. BGP doesn't retain non-best local
1941 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1943 evpn_cleanup_local_non_best_route(bgp
, vpn
, rn
, pi
);
1945 bgp_path_info_unlock(pi
);
1947 bgp_unlock_node(rn
);
1949 /* If this is a new route or some attribute has changed, export the
1950 * route to the global table. The route will be advertised to peers
1951 * from there. Note that this table is a 2-level tree (RD-level +
1952 * Prefix-level) similar to L3VPN routes.
1955 struct bgp_path_info
*global_pi
;
1957 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1958 (struct prefix
*)p
, &vpn
->prd
);
1959 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1,
1960 &global_pi
, flags
, seq
);
1962 /* Schedule for processing and unlock node. */
1963 bgp_process(bgp
, rn
, afi
, safi
);
1964 bgp_unlock_node(rn
);
1967 /* Unintern temporary. */
1968 aspath_unintern(&attr
.aspath
);
1974 * Delete EVPN route entry.
1975 * The entry can be in ESI/VNI table or the global table.
1977 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1978 struct bgp_node
*rn
,
1979 struct bgp_path_info
**pi
)
1981 struct bgp_path_info
*tmp_pi
;
1985 /* Now, find matching route. */
1986 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1987 tmp_pi
= tmp_pi
->next
)
1988 if (tmp_pi
->peer
== bgp
->peer_self
1989 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1990 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1995 /* Mark route for delete. */
1997 bgp_path_info_delete(rn
, tmp_pi
);
2002 /* Delete EVPN ES (type-4) route */
2003 static int delete_evpn_type4_route(struct bgp
*bgp
,
2005 struct prefix_evpn
*p
)
2007 afi_t afi
= AFI_L2VPN
;
2008 safi_t safi
= SAFI_EVPN
;
2009 struct bgp_path_info
*pi
;
2010 struct bgp_node
*rn
= NULL
; /* rn in esi table */
2011 struct bgp_node
*global_rn
= NULL
; /* rn in global table */
2013 /* First, locate the route node within the ESI.
2014 * If it doesn't exist, ther is nothing to do.
2015 * Note: there is no RD here.
2017 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2021 /* Next, locate route node in the global EVPN routing table.
2022 * Note that this table is a 2-level tree (RD-level + Prefix-level)
2024 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
2025 (struct prefix
*)p
, &es
->prd
);
2028 /* Delete route entry in the global EVPN table. */
2029 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
2031 /* Schedule for processing - withdraws to peers happen from
2035 bgp_process(bgp
, global_rn
, afi
, safi
);
2036 bgp_unlock_node(global_rn
);
2040 * Delete route entry in the ESI route table.
2041 * This can just be removed.
2043 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2045 bgp_path_info_reap(rn
, pi
);
2046 bgp_unlock_node(rn
);
2050 /* Delete EVPN type5 route */
2051 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
2053 afi_t afi
= AFI_L2VPN
;
2054 safi_t safi
= SAFI_EVPN
;
2055 struct bgp_node
*rn
= NULL
;
2056 struct bgp_path_info
*pi
= NULL
;
2057 struct bgp
*bgp_evpn
= NULL
; /* evpn bgp instance */
2059 bgp_evpn
= bgp_get_evpn();
2063 /* locate the global route entry for this type-5 prefix */
2064 rn
= bgp_afi_node_lookup(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
2065 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
2069 delete_evpn_route_entry(bgp_evpn
, afi
, safi
, rn
, &pi
);
2071 bgp_process(bgp_evpn
, rn
, afi
, safi
);
2072 bgp_unlock_node(rn
);
2077 * Delete EVPN route (of type based on prefix) for specified VNI and
2078 * schedule for processing.
2080 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2081 struct prefix_evpn
*p
)
2083 struct bgp_node
*rn
, *global_rn
;
2084 struct bgp_path_info
*pi
;
2085 afi_t afi
= AFI_L2VPN
;
2086 safi_t safi
= SAFI_EVPN
;
2088 /* First, locate the route node within the VNI. If it doesn't exist,
2090 * is nothing further to do.
2092 /* NOTE: There is no RD here. */
2093 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2097 /* Next, locate route node in the global EVPN routing table. Note that
2098 * this table is a 2-level tree (RD-level + Prefix-level) similar to
2101 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
2102 (struct prefix
*)p
, &vpn
->prd
);
2104 /* Delete route entry in the global EVPN table. */
2105 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
2107 /* Schedule for processing - withdraws to peers happen from
2111 bgp_process(bgp
, global_rn
, afi
, safi
);
2112 bgp_unlock_node(global_rn
);
2115 /* Delete route entry in the VNI route table. This can just be removed.
2117 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2119 bgp_path_info_reap(rn
, pi
);
2120 evpn_route_select_install(bgp
, vpn
, rn
);
2122 bgp_unlock_node(rn
);
2128 * Update all type-2 (MACIP) local routes for this VNI - these should also
2129 * be scheduled for advertise to peers.
2131 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2135 struct bgp_node
*rn
;
2136 struct bgp_path_info
*pi
, *tmp_pi
;
2138 struct attr
*attr_new
;
2140 int add_l3_ecomm
= 0;
2145 /* Walk this VNI's route table and update local type-2 routes. For any
2146 * routes updated, update corresponding entry in the global table too.
2148 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2149 rn
= bgp_route_next(rn
)) {
2150 const struct prefix_evpn
*evp
= (const struct prefix_evpn
*)bgp_node_get_prefix(rn
);
2151 struct bgp_node
*rd_rn
;
2152 struct bgp_path_info
*global_pi
;
2154 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2157 /* Identify local route. */
2158 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
2159 tmp_pi
= tmp_pi
->next
) {
2160 if (tmp_pi
->peer
== bgp
->peer_self
2161 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2162 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2170 * Build attribute per local route as the MAC mobility and
2171 * some other values could differ for different routes. The
2172 * attributes will be shared in the hash table.
2174 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
2175 attr
.nexthop
= vpn
->originator_ip
;
2176 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
2177 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
2178 bgp_evpn_get_rmac_nexthop(vpn
, evp
, &attr
,
2179 tmp_pi
->extra
->af_flags
);
2181 if (evpn_route_is_sticky(bgp
, rn
))
2183 else if (evpn_route_is_def_gw(bgp
, rn
)) {
2184 attr
.default_gw
= 1;
2185 if (is_evpn_prefix_ipaddr_v6(evp
))
2186 attr
.router_flag
= 1;
2189 if (bgp_debug_zebra(NULL
)) {
2190 char buf
[ETHER_ADDR_STRLEN
];
2191 char buf1
[PREFIX_STRLEN
];
2193 zlog_debug("VRF %s vni %u evp %s RMAC %s nexthop %s",
2195 vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
) : " ",
2197 prefix2str(evp
, buf1
, sizeof(buf1
)),
2198 prefix_mac2str(&attr
.rmac
, buf
, sizeof(buf
)),
2199 inet_ntoa(attr
.mp_nexthop_global_in
));
2202 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2203 * using L3 VNI for type-2 routes also.
2205 if ((is_evpn_prefix_ipaddr_v4(evp
) ||
2206 !IN6_IS_ADDR_LINKLOCAL(
2207 &evp
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
2208 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
2209 bgpevpn_get_l3vni(vpn
))
2212 /* Set up extended community. */
2213 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2215 seq
= mac_mobility_seqnum(tmp_pi
->attr
);
2217 /* Update the route entry. */
2218 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
, 0, &pi
,
2221 /* lock ri to prevent freeing in evpn_route_select_install */
2222 bgp_path_info_lock(pi
);
2224 /* Perform route selection. Normally, the local route in the
2225 * VNI is expected to win and be the best route. However,
2226 * under peculiar situations (e.g., tunnel (next hop) IP change
2227 * that causes best selection to be based on next hop), a
2228 * remote route could win. If the local route is the best,
2229 * ensure it is updated in the global EVPN route table and
2230 * advertised to peers; otherwise, ensure it is evicted and
2231 * (re)install the remote route into zebra.
2233 evpn_route_select_install(bgp
, vpn
, rn
);
2234 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
2235 evpn_cleanup_local_non_best_route(bgp
, vpn
, rn
, pi
);
2237 bgp_path_info_unlock(pi
);
2239 attr_new
= pi
->attr
;
2241 bgp_path_info_unlock(pi
);
2243 /* Update route in global routing table. */
2244 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2245 (struct prefix
*)evp
, &vpn
->prd
);
2247 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
,
2248 attr_new
, 0, &global_pi
, 0,
2249 mac_mobility_seqnum(attr_new
));
2251 /* Schedule for processing and unlock node. */
2252 bgp_process(bgp
, rd_rn
, afi
, safi
);
2253 bgp_unlock_node(rd_rn
);
2256 /* Unintern temporary. */
2257 aspath_unintern(&attr
.aspath
);
2265 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2266 * global routing table. These are also scheduled for withdraw from peers.
2268 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2272 struct bgp_node
*rdrn
, *rn
;
2273 struct bgp_table
*table
;
2274 struct bgp_path_info
*pi
;
2279 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
2280 if (rdrn
&& bgp_node_has_bgp_path_info_data(rdrn
)) {
2281 table
= bgp_node_get_bgp_table_info(rdrn
);
2282 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2283 const struct prefix_evpn
*evp
= (const struct prefix_evpn
*)bgp_node_get_prefix(rn
);
2285 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2288 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2290 bgp_process(bgp
, rn
, afi
, safi
);
2294 /* Unlock RD node. */
2296 bgp_unlock_node(rdrn
);
2302 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2303 * table as well as the per-VNI route table.
2305 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2309 struct bgp_node
*rn
;
2310 struct bgp_path_info
*pi
;
2315 /* First, walk the global route table for this VNI's type-2 local
2317 * EVPN routes are a 2-level table, first get the RD table.
2319 delete_global_type2_routes(bgp
, vpn
);
2321 /* Next, walk this VNI's route table and delete local type-2 routes. */
2322 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2323 rn
= bgp_route_next(rn
)) {
2324 const struct prefix_evpn
*evp
= (const struct prefix_evpn
*)bgp_node_get_prefix(rn
);
2326 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2329 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2331 /* Route entry in local table gets deleted immediately. */
2333 bgp_path_info_reap(rn
, pi
);
2340 * Delete all routes in per ES route-table
2342 static int delete_all_es_routes(struct bgp
*bgp
, struct evpnes
*es
)
2344 struct bgp_node
*rn
;
2345 struct bgp_path_info
*pi
, *nextpi
;
2347 /* Walk this ES's route table and delete all routes. */
2348 for (rn
= bgp_table_top(es
->route_table
); rn
;
2349 rn
= bgp_route_next(rn
)) {
2350 for (pi
= bgp_node_get_bgp_path_info(rn
);
2351 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2352 bgp_path_info_delete(rn
, pi
);
2353 bgp_path_info_reap(rn
, pi
);
2361 * Delete all routes in the per-VNI route table.
2363 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2365 struct bgp_node
*rn
;
2366 struct bgp_path_info
*pi
, *nextpi
;
2368 /* Walk this VNI's route table and delete all routes. */
2369 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2370 rn
= bgp_route_next(rn
)) {
2371 for (pi
= bgp_node_get_bgp_path_info(rn
);
2372 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2373 bgp_path_info_delete(rn
, pi
);
2374 bgp_path_info_reap(rn
, pi
);
2381 /* BUM traffic flood mode per-l2-vni */
2382 static int bgp_evpn_vni_flood_mode_get(struct bgp
*bgp
,
2383 struct bgpevpn
*vpn
)
2385 /* if flooding has been globally disabled per-vni mode is
2388 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
2389 return VXLAN_FLOOD_DISABLED
;
2391 /* if mcast group ip has been specified we use a PIM-SM MDT */
2392 if (vpn
->mcast_grp
.s_addr
!= INADDR_ANY
)
2393 return VXLAN_FLOOD_PIM_SM
;
2395 /* default is ingress replication */
2396 return VXLAN_FLOOD_HEAD_END_REPL
;
2400 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2401 * export RT getting modified or change to tunnel IP. Note that these
2402 * situations need the route in the per-VNI table as well as the global
2403 * table to be updated (as attributes change).
2405 int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2408 struct prefix_evpn p
;
2410 /* Update and advertise the type-3 route (only one) followed by the
2411 * locally learnt type-2 routes (MACIP) - for this VNI.
2413 * RT-3 only if doing head-end replication
2415 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
2416 == VXLAN_FLOOD_HEAD_END_REPL
) {
2417 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2418 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0);
2423 return update_all_type2_routes(bgp
, vpn
);
2426 /* Delete (and withdraw) local routes for specified ES from global and ES table.
2427 * Also remove all other routes from the per ES table.
2428 * Invoked when ES is deleted.
2430 static int delete_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
2433 char buf
[ESI_STR_LEN
];
2434 struct prefix_evpn p
;
2436 /* Delete and withdraw locally learnt ES route */
2437 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
.ipaddr_v4
);
2438 ret
= delete_evpn_type4_route(bgp
, es
, &p
);
2440 flog_err(EC_BGP_EVPN_ROUTE_DELETE
,
2441 "%u failed to delete type-4 route for ESI %s",
2442 bgp
->vrf_id
, esi_to_str(&es
->esi
, buf
, sizeof(buf
)));
2445 /* Delete all routes from per ES table */
2446 return delete_all_es_routes(bgp
, es
);
2450 * Delete (and withdraw) local routes for specified VNI from the global
2451 * table and per-VNI table. After this, remove all other routes from
2452 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2453 * (advertise-all-vni) being disabled.
2455 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2458 struct prefix_evpn p
;
2460 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2461 * followed by type-3 routes (only one) - for this VNI.
2463 ret
= delete_all_type2_routes(bgp
, vpn
);
2467 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2468 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2472 /* Delete all routes from the per-VNI table. */
2473 return delete_all_vni_routes(bgp
, vpn
);
2477 * There is a flood mcast IP address change. Update the mcast-grp and
2478 * remove the type-3 route if any. A new type-3 route will be generated
2479 * post tunnel_ip update if the new flood mode is head-end-replication.
2481 static int bgp_evpn_mcast_grp_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2482 struct in_addr mcast_grp
)
2484 struct prefix_evpn p
;
2486 vpn
->mcast_grp
= mcast_grp
;
2488 if (is_vni_live(vpn
)) {
2489 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2490 delete_evpn_route(bgp
, vpn
, &p
);
2497 * There is a tunnel endpoint IP address change for this VNI, delete
2498 * prior type-3 route (if needed) and update.
2499 * Note: Route re-advertisement happens elsewhere after other processing
2502 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2503 struct in_addr originator_ip
)
2505 struct prefix_evpn p
;
2507 /* If VNI is not live, we only need to update the originator ip */
2508 if (!is_vni_live(vpn
)) {
2509 vpn
->originator_ip
= originator_ip
;
2513 /* Update the tunnel-ip hash */
2514 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2515 bgp_tip_add(bgp
, &originator_ip
);
2517 /* filter routes as martian nexthop db has changed */
2518 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2520 /* Need to withdraw type-3 route as the originator IP is part
2523 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2524 delete_evpn_route(bgp
, vpn
, &p
);
2526 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2527 vpn
->originator_ip
= originator_ip
;
2531 static struct bgp_path_info
*
2532 bgp_create_evpn_bgp_path_info(struct bgp_path_info
*parent_pi
,
2533 struct bgp_node
*rn
, struct attr
*attr
)
2535 struct attr
*attr_new
;
2536 struct bgp_path_info
*pi
;
2538 /* Add (or update) attribute to hash. */
2539 attr_new
= bgp_attr_intern(attr
);
2541 /* Create new route with its attribute. */
2542 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0, parent_pi
->peer
,
2544 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2545 bgp_path_info_extra_get(pi
);
2546 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2547 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2548 if (parent_pi
->extra
) {
2549 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2550 sizeof(pi
->extra
->label
));
2551 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2553 bgp_path_info_add(rn
, pi
);
2558 /* Install EVPN route entry in ES */
2559 static int install_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2560 const struct prefix_evpn
*p
,
2561 struct bgp_path_info
*parent_pi
)
2564 struct bgp_node
*rn
= NULL
;
2565 struct bgp_path_info
*pi
= NULL
;
2566 struct attr
*attr_new
= NULL
;
2568 /* Create (or fetch) route within the VNI.
2569 * NOTE: There is no RD here.
2571 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
2573 /* Check if route entry is already present. */
2574 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2576 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2580 /* Add (or update) attribute to hash. */
2581 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2583 /* Create new route with its attribute. */
2584 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2585 parent_pi
->peer
, attr_new
, rn
);
2586 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2587 bgp_path_info_extra_get(pi
);
2588 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2589 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2590 bgp_path_info_add(rn
, pi
);
2592 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2593 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2594 bgp_unlock_node(rn
);
2597 /* The attribute has changed. */
2598 /* Add (or update) attribute to hash. */
2599 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2601 /* Restore route, if needed. */
2602 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2603 bgp_path_info_restore(rn
, pi
);
2605 /* Mark if nexthop has changed. */
2606 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2607 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2609 /* Unintern existing, set to new. */
2610 bgp_attr_unintern(&pi
->attr
);
2611 pi
->attr
= attr_new
;
2612 pi
->uptime
= bgp_clock();
2615 /* Perform route selection and update zebra, if required. */
2616 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2621 * Install route entry into the VRF routing table and invoke route selection.
2623 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2624 const struct prefix_evpn
*evp
,
2625 struct bgp_path_info
*parent_pi
)
2627 struct bgp_node
*rn
;
2628 struct bgp_path_info
*pi
;
2630 struct attr
*attr_new
;
2633 struct prefix
*pp
= &p
;
2636 char buf
[PREFIX_STRLEN
];
2637 char buf1
[PREFIX_STRLEN
];
2639 memset(pp
, 0, sizeof(struct prefix
));
2640 ip_prefix_from_evpn_prefix(evp
, pp
);
2642 if (bgp_debug_zebra(NULL
)) {
2644 "import evpn prefix %s as ip prefix %s in vrf %s",
2645 prefix2str(evp
, buf
, sizeof(buf
)),
2646 prefix2str(pp
, buf1
, sizeof(buf
)),
2647 vrf_id_to_name(bgp_vrf
->vrf_id
));
2650 /* Create (or fetch) route within the VRF. */
2651 /* NOTE: There is no RD here. */
2652 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2654 safi
= SAFI_UNICAST
;
2655 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2656 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2658 safi
= SAFI_UNICAST
;
2659 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2663 /* EVPN routes currently only support a IPv4 next hop which corresponds
2664 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2665 * or prefix route, we have to convert the next hop to an IPv4-mapped
2666 * address for the rest of the code to flow through. In the case of IPv4,
2667 * make sure to set the flag for next hop attribute.
2669 attr
= *parent_pi
->attr
;
2671 evpn_convert_nexthop_to_ipv6(&attr
);
2673 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2675 /* Check if route entry is already present. */
2676 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2678 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2682 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, rn
, &attr
);
2684 if (attrhash_cmp(pi
->attr
, &attr
)
2685 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2686 bgp_unlock_node(rn
);
2689 /* The attribute has changed. */
2690 /* Add (or update) attribute to hash. */
2691 attr_new
= bgp_attr_intern(&attr
);
2693 /* Restore route, if needed. */
2694 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2695 bgp_path_info_restore(rn
, pi
);
2697 /* Mark if nexthop has changed. */
2699 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2701 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2702 &attr_new
->mp_nexthop_global
)))
2703 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2705 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_ATTR_CHANGED
);
2706 /* Unintern existing, set to new. */
2707 bgp_attr_unintern(&pi
->attr
);
2708 pi
->attr
= attr_new
;
2709 pi
->uptime
= bgp_clock();
2711 /* as it is an importation, change nexthop */
2712 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_ANNC_NH_SELF
);
2714 bgp_aggregate_increment(bgp_vrf
, bgp_node_get_prefix(rn
), pi
, afi
,
2717 /* Perform route selection and update zebra, if required. */
2718 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2720 /* Process for route leaking. */
2721 vpn_leak_from_vrf_update(bgp_get_default(), bgp_vrf
, pi
);
2723 bgp_unlock_node(rn
);
2729 * Install route entry into the VNI routing table and invoke route selection.
2731 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2732 const struct prefix_evpn
*p
,
2733 struct bgp_path_info
*parent_pi
)
2735 struct bgp_node
*rn
;
2736 struct bgp_path_info
*pi
;
2737 struct attr
*attr_new
;
2740 /* Create (or fetch) route within the VNI. */
2741 /* NOTE: There is no RD here. */
2742 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2744 /* Check if route entry is already present. */
2745 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2747 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2751 /* Create an info */
2752 (void)bgp_create_evpn_bgp_path_info(parent_pi
, rn
,
2755 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2756 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2757 bgp_unlock_node(rn
);
2760 /* The attribute has changed. */
2761 /* Add (or update) attribute to hash. */
2762 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2764 /* Restore route, if needed. */
2765 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2766 bgp_path_info_restore(rn
, pi
);
2768 /* Mark if nexthop has changed. */
2769 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2770 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2772 /* Unintern existing, set to new. */
2773 bgp_attr_unintern(&pi
->attr
);
2774 pi
->attr
= attr_new
;
2775 pi
->uptime
= bgp_clock();
2778 /* Perform route selection and update zebra, if required. */
2779 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2781 bgp_unlock_node(rn
);
2786 /* Uninstall EVPN route entry from ES route table */
2787 static int uninstall_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2788 const struct prefix_evpn
*p
,
2789 struct bgp_path_info
*parent_pi
)
2792 struct bgp_node
*rn
;
2793 struct bgp_path_info
*pi
;
2795 if (!es
->route_table
)
2798 /* Locate route within the ESI.
2799 * NOTE: There is no RD here.
2801 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2805 /* Find matching route entry. */
2806 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2808 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2814 /* Mark entry for deletion */
2815 bgp_path_info_delete(rn
, pi
);
2817 /* Perform route selection and update zebra, if required. */
2818 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2820 /* Unlock route node. */
2821 bgp_unlock_node(rn
);
2827 * Uninstall route entry from the VRF routing table and send message
2828 * to zebra, if appropriate.
2830 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2831 const struct prefix_evpn
*evp
,
2832 struct bgp_path_info
*parent_pi
)
2834 struct bgp_node
*rn
;
2835 struct bgp_path_info
*pi
;
2838 struct prefix
*pp
= &p
;
2841 char buf
[PREFIX_STRLEN
];
2842 char buf1
[PREFIX_STRLEN
];
2844 memset(pp
, 0, sizeof(struct prefix
));
2845 ip_prefix_from_evpn_prefix(evp
, pp
);
2847 if (bgp_debug_zebra(NULL
)) {
2849 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2850 prefix2str(evp
, buf
, sizeof(buf
)),
2851 prefix2str(pp
, buf1
, sizeof(buf
)),
2852 vrf_id_to_name(bgp_vrf
->vrf_id
));
2855 /* Locate route within the VRF. */
2856 /* NOTE: There is no RD here. */
2857 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2859 safi
= SAFI_UNICAST
;
2860 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2863 safi
= SAFI_UNICAST
;
2864 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2870 /* Find matching route entry. */
2871 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2873 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2879 /* Process for route leaking. */
2880 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp_vrf
, pi
);
2882 bgp_aggregate_decrement(bgp_vrf
, bgp_node_get_prefix(rn
), pi
, afi
,
2885 /* Mark entry for deletion */
2886 bgp_path_info_delete(rn
, pi
);
2888 /* Perform route selection and update zebra, if required. */
2889 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2891 /* Unlock route node. */
2892 bgp_unlock_node(rn
);
2898 * Uninstall route entry from the VNI routing table and send message
2899 * to zebra, if appropriate.
2901 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2902 const struct prefix_evpn
*p
,
2903 struct bgp_path_info
*parent_pi
)
2905 struct bgp_node
*rn
;
2906 struct bgp_path_info
*pi
;
2909 /* Locate route within the VNI. */
2910 /* NOTE: There is no RD here. */
2911 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2915 /* Find matching route entry. */
2916 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2918 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2924 /* Mark entry for deletion */
2925 bgp_path_info_delete(rn
, pi
);
2927 /* Perform route selection and update zebra, if required. */
2928 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2930 /* Unlock route node. */
2931 bgp_unlock_node(rn
);
2937 * Given a prefix, see if it belongs to ES.
2939 static int is_prefix_matching_for_es(const struct prefix_evpn
*p
,
2942 /* if not an ES route return false */
2943 if (p
->prefix
.route_type
!= BGP_EVPN_ES_ROUTE
)
2946 if (memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
)) == 0)
2953 * Given a route entry and a VRF, see if this route entry should be
2954 * imported into the VRF i.e., RTs match.
2956 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2957 struct bgp_path_info
*pi
)
2959 struct attr
*attr
= pi
->attr
;
2960 struct ecommunity
*ecom
;
2964 /* Route should have valid RT to be even considered. */
2965 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2968 ecom
= attr
->ecommunity
;
2969 if (!ecom
|| !ecom
->size
)
2972 /* For each extended community RT, see if it matches this VNI. If any RT
2973 * matches, we're done.
2975 for (i
= 0; i
< ecom
->size
; i
++) {
2977 uint8_t type
, sub_type
;
2978 struct ecommunity_val
*eval
;
2979 struct ecommunity_val eval_tmp
;
2980 struct vrf_irt_node
*irt
;
2982 /* Only deal with RTs */
2983 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2984 eval
= (struct ecommunity_val
*)(ecom
->val
2985 + (i
* ECOMMUNITY_SIZE
));
2988 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2991 /* See if this RT matches specified VNIs import RTs */
2992 irt
= lookup_vrf_import_rt(eval
);
2994 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2997 /* Also check for non-exact match. In this, we mask out the AS
2999 * only check on the local-admin sub-field. This is to
3001 * VNI as the RT for EBGP peering too.
3004 if (type
== ECOMMUNITY_ENCODE_AS
3005 || type
== ECOMMUNITY_ENCODE_AS4
3006 || type
== ECOMMUNITY_ENCODE_IP
) {
3007 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3008 mask_ecom_global_admin(&eval_tmp
, eval
);
3009 irt
= lookup_vrf_import_rt(&eval_tmp
);
3012 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
3020 * Given a route entry and a VNI, see if this route entry should be
3021 * imported into the VNI i.e., RTs match.
3023 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
3024 struct bgp_path_info
*pi
)
3026 struct attr
*attr
= pi
->attr
;
3027 struct ecommunity
*ecom
;
3031 /* Route should have valid RT to be even considered. */
3032 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3035 ecom
= attr
->ecommunity
;
3036 if (!ecom
|| !ecom
->size
)
3039 /* For each extended community RT, see if it matches this VNI. If any RT
3040 * matches, we're done.
3042 for (i
= 0; i
< ecom
->size
; i
++) {
3044 uint8_t type
, sub_type
;
3045 struct ecommunity_val
*eval
;
3046 struct ecommunity_val eval_tmp
;
3047 struct irt_node
*irt
;
3049 /* Only deal with RTs */
3050 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3051 eval
= (struct ecommunity_val
*)(ecom
->val
3052 + (i
* ECOMMUNITY_SIZE
));
3055 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3058 /* See if this RT matches specified VNIs import RTs */
3059 irt
= lookup_import_rt(bgp
, eval
);
3061 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
3064 /* Also check for non-exact match. In this, we mask out the AS
3066 * only check on the local-admin sub-field. This is to
3068 * VNI as the RT for EBGP peering too.
3071 if (type
== ECOMMUNITY_ENCODE_AS
3072 || type
== ECOMMUNITY_ENCODE_AS4
3073 || type
== ECOMMUNITY_ENCODE_IP
) {
3074 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3075 mask_ecom_global_admin(&eval_tmp
, eval
);
3076 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3079 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
3086 static int install_uninstall_routes_for_es(struct bgp
*bgp
,
3093 char buf
[PREFIX_STRLEN
];
3094 char buf1
[ESI_STR_LEN
];
3095 struct bgp_node
*rd_rn
, *rn
;
3096 struct bgp_table
*table
;
3097 struct bgp_path_info
*pi
;
3103 * Walk entire global routing table and evaluate routes which could be
3104 * imported into this VRF. Note that we need to loop through all global
3105 * routes to determine which route matches the import rt on vrf
3107 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
3108 rd_rn
= bgp_route_next(rd_rn
)) {
3109 table
= bgp_node_get_bgp_table_info(rd_rn
);
3113 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3114 const struct prefix_evpn
*evp
=
3115 (const struct prefix_evpn
*)bgp_node_get_prefix(
3118 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3121 * Consider "valid" remote routes applicable for
3124 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3125 && pi
->type
== ZEBRA_ROUTE_BGP
3126 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3129 if (!is_prefix_matching_for_es(evp
, es
))
3133 ret
= install_evpn_route_entry_in_es(
3136 ret
= uninstall_evpn_route_entry_in_es(
3142 "Failed to %s EVPN %s route in ESI %s",
3145 prefix2str(evp
, buf
,
3147 esi_to_str(&es
->esi
, buf1
,
3157 /* This API will scan evpn routes for checking attribute's rmac
3158 * macthes with bgp instance router mac. It avoid installing
3159 * route into bgp vrf table and remote rmac in bridge table.
3161 static int bgp_evpn_route_rmac_self_check(struct bgp
*bgp_vrf
,
3162 const struct prefix_evpn
*evp
,
3163 struct bgp_path_info
*pi
)
3165 /* evpn route could have learnt prior to L3vni has come up,
3166 * perform rmac check before installing route and
3167 * remote router mac.
3168 * The route will be removed from global bgp table once
3169 * SVI comes up with MAC and stored in hash, triggers
3170 * bgp_mac_rescan_all_evpn_tables.
3172 if (memcmp(&bgp_vrf
->rmac
, &pi
->attr
->rmac
, ETH_ALEN
) == 0) {
3173 if (bgp_debug_update(pi
->peer
, NULL
, NULL
, 1)) {
3174 char buf1
[PREFIX_STRLEN
];
3175 char attr_str
[BUFSIZ
] = {0};
3177 bgp_dump_attr(pi
->attr
, attr_str
, BUFSIZ
);
3179 zlog_debug("%s: bgp %u prefix %s with attr %s - DENIED due to self mac",
3180 __func__
, bgp_vrf
->vrf_id
,
3181 prefix2str(evp
, buf1
, sizeof(buf1
)),
3192 * Install or uninstall mac-ip routes are appropriate for this
3195 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
3199 struct bgp_node
*rd_rn
, *rn
;
3200 struct bgp_table
*table
;
3201 struct bgp_path_info
*pi
;
3203 char buf
[PREFIX_STRLEN
];
3204 struct bgp
*bgp_evpn
= NULL
;
3208 bgp_evpn
= bgp_get_evpn();
3212 /* Walk entire global routing table and evaluate routes which could be
3213 * imported into this VRF. Note that we need to loop through all global
3214 * routes to determine which route matches the import rt on vrf
3216 for (rd_rn
= bgp_table_top(bgp_evpn
->rib
[afi
][safi
]); rd_rn
;
3217 rd_rn
= bgp_route_next(rd_rn
)) {
3218 table
= bgp_node_get_bgp_table_info(rd_rn
);
3222 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3223 const struct prefix_evpn
*evp
= (const struct prefix_evpn
*)bgp_node_get_prefix(rn
);
3225 /* if not mac-ip route skip this route */
3226 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3227 || evp
->prefix
.route_type
3228 == BGP_EVPN_IP_PREFIX_ROUTE
))
3231 /* if not a mac+ip route skip this route */
3232 if (!(is_evpn_prefix_ipaddr_v4(evp
)
3233 || is_evpn_prefix_ipaddr_v6(evp
)))
3236 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3238 /* Consider "valid" remote routes applicable for
3241 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3242 && pi
->type
== ZEBRA_ROUTE_BGP
3243 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3246 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
3247 if (bgp_evpn_route_rmac_self_check(
3252 ret
= install_evpn_route_entry_in_vrf(
3255 ret
= uninstall_evpn_route_entry_in_vrf(
3261 "Failed to %s EVPN %s route in VRF %s",
3264 prefix2str(evp
, buf
,
3279 * Install or uninstall routes of specified type that are appropriate for this
3282 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
3283 struct bgpevpn
*vpn
,
3284 bgp_evpn_route_type rtype
,
3289 struct bgp_node
*rd_rn
, *rn
;
3290 struct bgp_table
*table
;
3291 struct bgp_path_info
*pi
;
3297 /* Walk entire global routing table and evaluate routes which could be
3298 * imported into this VPN. Note that we cannot just look at the routes
3300 * the VNI's RD - remote routes applicable for this VNI could have any
3303 /* EVPN routes are a 2-level table. */
3304 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
3305 rd_rn
= bgp_route_next(rd_rn
)) {
3306 table
= bgp_node_get_bgp_table_info(rd_rn
);
3310 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3311 const struct prefix_evpn
*evp
=
3312 (const struct prefix_evpn
*)bgp_node_get_prefix(
3315 if (evp
->prefix
.route_type
!= rtype
)
3318 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3320 /* Consider "valid" remote routes applicable for
3322 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3323 && pi
->type
== ZEBRA_ROUTE_BGP
3324 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3327 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3329 ret
= install_evpn_route_entry(
3332 ret
= uninstall_evpn_route_entry(
3338 "%u: Failed to %s EVPN %s route in VNI %u",
3342 rtype
== BGP_EVPN_MAC_IP_ROUTE
3356 /* Install any existing remote ES routes applicable for this ES into its routing
3357 * table. This is invoked when ES comes up.
3359 static int install_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
3361 return install_uninstall_routes_for_es(bgp
, es
, 1);
3365 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3366 * is invoked upon l3vni-add or l3vni import rt change
3368 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3370 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3375 * Install any existing remote routes applicable for this VNI into its
3376 * routing table. This is invoked when a VNI becomes "live" or its Import
3379 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3383 /* Install type-3 routes followed by type-2 routes - the ones applicable
3386 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3391 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3395 /* uninstall routes from l3vni vrf. */
3396 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3398 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3403 * Uninstall any existing remote routes for this VNI. One scenario in which
3404 * this is invoked is upon an import RT change.
3406 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3410 /* Uninstall type-2 routes followed by type-3 routes - the ones
3414 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3419 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3423 /* Install or unistall route in ES */
3424 static int install_uninstall_route_in_es(struct bgp
*bgp
, struct evpnes
*es
,
3425 afi_t afi
, safi_t safi
,
3426 struct prefix_evpn
*evp
,
3427 struct bgp_path_info
*pi
, int install
)
3430 char buf
[ESI_STR_LEN
];
3433 ret
= install_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3435 ret
= uninstall_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3440 "%u: Failed to %s EVPN %s route in ESI %s", bgp
->vrf_id
,
3441 install
? "install" : "uninstall", "ES",
3442 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)));
3449 * Install or uninstall route in matching VRFs (list).
3451 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3452 safi_t safi
, struct prefix_evpn
*evp
,
3453 struct bgp_path_info
*pi
,
3454 struct list
*vrfs
, int install
)
3456 char buf
[PREFIX2STR_BUFFER
];
3457 struct bgp
*bgp_vrf
;
3458 struct listnode
*node
, *nnode
;
3460 /* Only type-2/type-5 routes go into a VRF */
3461 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3462 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3465 /* if it is type-2 route and not a mac+ip route skip this route */
3466 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3467 && !(is_evpn_prefix_ipaddr_v4(evp
)
3468 || is_evpn_prefix_ipaddr_v6(evp
)))
3471 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3475 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3477 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3481 flog_err(EC_BGP_EVPN_FAIL
,
3482 "%u: Failed to %s prefix %s in VRF %s",
3484 install
? "install" : "uninstall",
3485 prefix2str(evp
, buf
, sizeof(buf
)),
3486 vrf_id_to_name(bgp_vrf
->vrf_id
));
3495 * Install or uninstall route in matching VNIs (list).
3497 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3498 safi_t safi
, struct prefix_evpn
*evp
,
3499 struct bgp_path_info
*pi
,
3500 struct list
*vnis
, int install
)
3502 struct bgpevpn
*vpn
;
3503 struct listnode
*node
, *nnode
;
3505 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3508 if (!is_vni_live(vpn
))
3512 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3514 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3517 flog_err(EC_BGP_EVPN_FAIL
,
3518 "%u: Failed to %s EVPN %s route in VNI %u",
3519 bgp
->vrf_id
, install
? "install" : "uninstall",
3520 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3532 * Install or uninstall route for appropriate VNIs/ESIs.
3534 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3535 const struct prefix
*p
,
3536 struct bgp_path_info
*pi
, int import
)
3538 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3539 struct attr
*attr
= pi
->attr
;
3540 struct ecommunity
*ecom
;
3545 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3546 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3547 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3548 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3549 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3552 /* If we don't have Route Target, nothing much to do. */
3553 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3556 ecom
= attr
->ecommunity
;
3557 if (!ecom
|| !ecom
->size
)
3560 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3561 * attached to the route */
3562 for (i
= 0; i
< ecom
->size
; i
++) {
3564 uint8_t type
, sub_type
;
3565 struct ecommunity_val
*eval
;
3566 struct ecommunity_val eval_tmp
;
3567 struct irt_node
*irt
; /* import rt for l2vni */
3568 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3571 /* Only deal with RTs */
3572 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3573 eval
= (struct ecommunity_val
*)(ecom
->val
3574 + (i
* ECOMMUNITY_SIZE
));
3577 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3581 * macip routes (type-2) are imported into VNI and VRF tables.
3582 * IMET route is imported into VNI table.
3583 * prefix routes are imported into VRF table.
3585 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3586 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3587 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3589 irt
= lookup_import_rt(bgp
, eval
);
3591 install_uninstall_route_in_vnis(
3592 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3595 vrf_irt
= lookup_vrf_import_rt(eval
);
3597 install_uninstall_route_in_vrfs(
3598 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3601 /* Also check for non-exact match.
3602 * In this, we mask out the AS and
3603 * only check on the local-admin sub-field.
3604 * This is to facilitate using
3605 * VNI as the RT for EBGP peering too.
3609 if (type
== ECOMMUNITY_ENCODE_AS
3610 || type
== ECOMMUNITY_ENCODE_AS4
3611 || type
== ECOMMUNITY_ENCODE_IP
) {
3612 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3613 mask_ecom_global_admin(&eval_tmp
, eval
);
3614 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3615 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3619 install_uninstall_route_in_vnis(
3620 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3623 install_uninstall_route_in_vrfs(
3624 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3628 /* es route is imported into the es table */
3629 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3631 /* we will match based on the entire esi to avoid
3632 * imoort of an es route for esi2 into esi1
3634 es
= bgp_evpn_lookup_es(bgp
, &evp
->prefix
.es_addr
.esi
);
3635 if (es
&& is_es_local(es
))
3636 install_uninstall_route_in_es(
3637 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3645 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3648 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3650 /* Delete ipv4 default route and withdraw from peers */
3651 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3652 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3653 SAFI_UNICAST
, false);
3655 /* delete all ipv4 routes and withdraw from peers */
3656 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3657 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3659 /* Delete ipv6 default route and withdraw from peers */
3660 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3661 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3662 SAFI_UNICAST
, false);
3664 /* delete all ipv6 routes and withdraw from peers */
3665 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3666 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3670 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3673 void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3675 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
3677 bgp_evpn
= bgp_get_evpn();
3681 /* update all ipv4 routes */
3682 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3683 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3685 /* update ipv4 default route and withdraw from peers */
3686 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3687 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3688 SAFI_UNICAST
, true);
3690 /* update all ipv6 routes */
3691 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3692 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3694 /* update ipv6 default route and withdraw from peers */
3695 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3696 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3697 SAFI_UNICAST
, true);
3702 * update and advertise local routes for a VRF as type-5 routes.
3703 * This is invoked upon RD change for a VRF. Note taht the processing is only
3704 * done in the global route table using the routes which already exist in the
3707 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3709 /* skip if the RD is configured */
3710 if (is_vrf_rd_configured(bgp_vrf
))
3713 /* derive the RD for the VRF based on new router-id */
3714 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3716 /* update advertise ipv4|ipv6 routes as type-5 routes */
3717 update_advertise_vrf_routes(bgp_vrf
);
3721 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3722 * This is invoked upon VRF RD change. The processing is done only from global
3725 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3727 /* skip if the RD is configured */
3728 if (is_vrf_rd_configured(bgp_vrf
))
3731 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3732 delete_withdraw_vrf_routes(bgp_vrf
);
3736 * Update and advertise local routes for a VNI. Invoked upon router-id
3737 * change. Note that the processing is done only on the global route table
3738 * using routes that already exist in the per-VNI table.
3740 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3742 struct prefix_evpn p
;
3743 struct bgp_node
*rn
, *global_rn
;
3744 struct bgp_path_info
*pi
, *global_pi
;
3746 afi_t afi
= AFI_L2VPN
;
3747 safi_t safi
= SAFI_EVPN
;
3749 /* Locate type-3 route for VNI in the per-VNI table and use its
3750 * attributes to create and advertise the type-3 route for this VNI
3751 * in the global table.
3753 * RT-3 only if doing head-end replication
3755 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3756 == VXLAN_FLOOD_HEAD_END_REPL
) {
3757 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3758 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3759 if (!rn
) /* unexpected */
3761 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3762 if (pi
->peer
== bgp
->peer_self
&&
3763 pi
->type
== ZEBRA_ROUTE_BGP
3764 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3766 if (!pi
) /* unexpected */
3770 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3771 (struct prefix
*)&p
, &vpn
->prd
);
3772 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
,
3773 1, &pi
, 0, mac_mobility_seqnum(attr
));
3775 /* Schedule for processing and unlock node. */
3776 bgp_process(bgp
, global_rn
, afi
, safi
);
3777 bgp_unlock_node(global_rn
);
3780 /* Now, walk this VNI's route table and use the route and its attribute
3781 * to create and schedule route in global table.
3783 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
3784 rn
= bgp_route_next(rn
)) {
3785 const struct prefix_evpn
*evp
=
3786 (const struct prefix_evpn
*)bgp_node_get_prefix(rn
);
3788 /* Identify MAC-IP local routes. */
3789 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3792 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3793 if (pi
->peer
== bgp
->peer_self
3794 && pi
->type
== ZEBRA_ROUTE_BGP
3795 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3800 /* Create route in global routing table using this route entry's
3804 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3805 (struct prefix
*)evp
, &vpn
->prd
);
3807 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
3809 mac_mobility_seqnum(attr
));
3811 /* Schedule for processing and unlock node. */
3812 bgp_process(bgp
, global_rn
, afi
, safi
);
3813 bgp_unlock_node(global_rn
);
3820 * Delete (and withdraw) local routes for a VNI - only from the global
3821 * table. Invoked upon router-id change.
3823 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3826 struct prefix_evpn p
;
3827 struct bgp_node
*global_rn
;
3828 struct bgp_path_info
*pi
;
3829 afi_t afi
= AFI_L2VPN
;
3830 safi_t safi
= SAFI_EVPN
;
3832 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3833 * for this VNI - from the global table.
3835 ret
= delete_global_type2_routes(bgp
, vpn
);
3839 /* Remove type-3 route for this VNI from global table. */
3840 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3841 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3842 (struct prefix
*)&p
, &vpn
->prd
);
3844 /* Delete route entry in the global EVPN table. */
3845 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
3847 /* Schedule for processing - withdraws to peers happen from
3851 bgp_process(bgp
, global_rn
, afi
, safi
);
3852 bgp_unlock_node(global_rn
);
3859 * Handle router-id change. Update and advertise local routes corresponding
3860 * to this VNI from peers. Note that this is invoked after updating the
3861 * router-id. The routes in the per-VNI table are used to create routes in
3862 * the global table and schedule them.
3864 static void update_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3866 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3868 /* Skip VNIs with configured RD. */
3869 if (is_rd_configured(vpn
))
3872 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3873 update_advertise_vni_routes(bgp
, vpn
);
3877 * Handle router-id change. Delete and withdraw local routes corresponding
3878 * to this VNI from peers. Note that this is invoked prior to updating
3879 * the router-id and is done only on the global route table, the routes
3880 * are needed in the per-VNI table to re-advertise with new router id.
3882 static void withdraw_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3884 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3886 /* Skip VNIs with configured RD. */
3887 if (is_rd_configured(vpn
))
3890 delete_withdraw_vni_routes(bgp
, vpn
);
3894 * Create RT-3 for a VNI and schedule for processing and advertisement.
3895 * This is invoked upon flooding mode changing to head-end replication.
3897 static void create_advertise_type3(struct hash_bucket
*bucket
, void *data
)
3899 struct bgpevpn
*vpn
= bucket
->data
;
3900 struct bgp
*bgp
= data
;
3901 struct prefix_evpn p
;
3903 if (!vpn
|| !is_vni_live(vpn
) ||
3904 bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3905 != VXLAN_FLOOD_HEAD_END_REPL
)
3908 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3909 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0))
3910 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3911 "Type3 route creation failure for VNI %u", vpn
->vni
);
3915 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3916 * This is invoked upon flooding mode changing to drop BUM packets.
3918 static void delete_withdraw_type3(struct hash_bucket
*bucket
, void *data
)
3920 struct bgpevpn
*vpn
= bucket
->data
;
3921 struct bgp
*bgp
= data
;
3922 struct prefix_evpn p
;
3924 if (!vpn
|| !is_vni_live(vpn
))
3927 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3928 delete_evpn_route(bgp
, vpn
, &p
);
3932 * Process received EVPN type-2 route (advertise or withdraw).
3934 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3935 struct attr
*attr
, uint8_t *pfx
, int psize
,
3936 uint32_t addpath_id
)
3938 struct prefix_rd prd
;
3939 struct prefix_evpn p
;
3940 struct bgp_route_evpn evpn
;
3942 uint8_t macaddr_len
;
3943 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
3944 uint32_t num_labels
= 0;
3948 /* Type-2 route should be either 33, 37 or 49 bytes or an
3949 * additional 3 bytes if there is a second label (VNI):
3950 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3951 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3952 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3954 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3955 && psize
!= 40 && psize
!= 52) {
3956 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3957 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3958 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3962 memset(&evpn
, 0, sizeof(evpn
));
3964 /* Make prefix_rd */
3965 prd
.family
= AF_UNSPEC
;
3967 memcpy(&prd
.val
, pfx
, 8);
3970 /* Make EVPN prefix. */
3971 memset(&p
, 0, sizeof(struct prefix_evpn
));
3973 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3974 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3976 /* Copy Ethernet Seg Identifier */
3977 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
3980 /* Copy Ethernet Tag */
3981 memcpy(ð_tag
, pfx
, 4);
3982 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3985 /* Get the MAC Addr len */
3986 macaddr_len
= *pfx
++;
3988 /* Get the MAC Addr */
3989 if (macaddr_len
== (ETH_ALEN
* 8)) {
3990 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
3994 EC_BGP_EVPN_ROUTE_INVALID
,
3995 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3996 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
4002 ipaddr_len
= *pfx
++;
4003 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
4004 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
4006 EC_BGP_EVPN_ROUTE_INVALID
,
4007 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
4008 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4013 ipaddr_len
/= 8; /* Convert to bytes. */
4014 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
4017 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
4021 /* Get the VNI(s). Stored as bytes here. */
4023 memset(label
, 0, sizeof(label
));
4024 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
4025 pfx
+= BGP_LABEL_BYTES
;
4026 psize
-= (33 + ipaddr_len
);
4027 /* Do we have a second VNI? */
4030 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
4032 * If in future, we are required to access additional fields,
4033 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
4038 /* Process the route. */
4040 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4041 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4042 &prd
, &label
[0], num_labels
, 0, &evpn
);
4044 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4045 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4046 &prd
, &label
[0], num_labels
, &evpn
);
4051 * Process received EVPN type-3 route (advertise or withdraw).
4053 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4054 struct attr
*attr
, uint8_t *pfx
, int psize
,
4055 uint32_t addpath_id
)
4057 struct prefix_rd prd
;
4058 struct prefix_evpn p
;
4063 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
4064 * IP len (1) and IP (4 or 16).
4066 if (psize
!= 17 && psize
!= 29) {
4067 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4068 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
4069 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4073 /* If PMSI is present, log if it is anything other than IR.
4074 * Note: We just simply ignore the values as it is not clear if
4075 * doing anything else is better.
4078 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
4079 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
&&
4080 attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_PIM_SM
) {
4081 flog_warn(EC_BGP_EVPN_PMSI_PRESENT
,
4082 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
4083 peer
->bgp
->vrf_id
, peer
->host
,
4084 attr
->pmsi_tnl_type
);
4088 /* Make prefix_rd */
4089 prd
.family
= AF_UNSPEC
;
4091 memcpy(&prd
.val
, pfx
, 8);
4094 /* Make EVPN prefix. */
4095 memset(&p
, 0, sizeof(struct prefix_evpn
));
4097 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4098 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
4100 /* Copy Ethernet Tag */
4101 memcpy(ð_tag
, pfx
, 4);
4102 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
4106 ipaddr_len
= *pfx
++;
4107 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
4108 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
4109 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
4112 EC_BGP_EVPN_ROUTE_INVALID
,
4113 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
4114 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4118 /* Process the route. */
4120 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4121 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4122 &prd
, NULL
, 0, 0, NULL
);
4124 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4125 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4126 &prd
, NULL
, 0, NULL
);
4131 * Process received EVPN type-4 route (advertise or withdraw).
4133 static int process_type4_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4134 struct attr
*attr
, uint8_t *pfx
, int psize
,
4135 uint32_t addpath_id
)
4140 struct in_addr vtep_ip
;
4141 struct prefix_rd prd
;
4142 struct prefix_evpn p
;
4144 /* Type-4 route should be either 23 or 35 bytes
4145 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
4147 if (psize
!= 23 && psize
!= 35) {
4148 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4149 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
4150 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4154 /* Make prefix_rd */
4155 prd
.family
= AF_UNSPEC
;
4157 memcpy(&prd
.val
, pfx
, 8);
4161 memcpy(&esi
, pfx
, ESI_BYTES
);
4166 ipaddr_len
= *pfx
++;
4167 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
4168 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
4171 EC_BGP_EVPN_ROUTE_INVALID
,
4172 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
4173 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4177 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
4178 /* Process the route. */
4180 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4181 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4182 &prd
, NULL
, 0, 0, NULL
);
4184 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4185 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4186 &prd
, NULL
, 0, NULL
);
4193 * Process received EVPN type-5 route (advertise or withdraw).
4195 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4196 struct attr
*attr
, uint8_t *pfx
, int psize
,
4197 uint32_t addpath_id
)
4199 struct prefix_rd prd
;
4200 struct prefix_evpn p
;
4201 struct bgp_route_evpn evpn
;
4204 mpls_label_t label
; /* holds the VNI as in the packet */
4207 bool is_valid_update
= false;
4209 /* Type-5 route should be 34 or 58 bytes:
4210 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
4211 * GW (4 or 16) and VNI (3).
4212 * Note that the IP and GW should both be IPv4 or both IPv6.
4214 if (psize
!= 34 && psize
!= 58) {
4215 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4216 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
4217 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4221 /* Make prefix_rd */
4222 prd
.family
= AF_UNSPEC
;
4224 memcpy(&prd
.val
, pfx
, 8);
4227 /* Make EVPN prefix. */
4228 memset(&p
, 0, sizeof(struct prefix_evpn
));
4230 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4231 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
4233 /* Additional information outside of prefix - ESI and GW IP */
4234 memset(&evpn
, 0, sizeof(evpn
));
4237 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
4240 /* Fetch Ethernet Tag. */
4241 memcpy(ð_tag
, pfx
, 4);
4242 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
4245 /* Fetch IP prefix length. */
4247 if (ippfx_len
> IPV6_MAX_BITLEN
) {
4249 EC_BGP_EVPN_ROUTE_INVALID
,
4250 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4251 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
4254 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
4256 /* Determine IPv4 or IPv6 prefix */
4257 /* Since the address and GW are from the same family, this just becomes
4258 * a simple check on the total size.
4261 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
4262 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
4264 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
4268 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
4269 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
4271 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
4276 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4277 memset(&label
, 0, sizeof(label
));
4278 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
4281 * If in future, we are required to access additional fields,
4282 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4287 is_valid_update
= true;
4288 if (is_zero_mac(&attr
->rmac
) && is_zero_esi(&evpn
.eth_s_id
) &&
4289 is_zero_gw_ip(&evpn
.gw_ip
, gw_afi
))
4290 is_valid_update
= false;
4292 if (is_mcast_mac(&attr
->rmac
) || is_bcast_mac(&attr
->rmac
))
4293 is_valid_update
= false;
4296 /* Process the route. */
4297 if (is_valid_update
)
4298 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4299 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4300 &prd
, &label
, 1, 0, &evpn
);
4302 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4303 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4304 &prd
, &label
, 1, &evpn
);
4309 static void evpn_mpattr_encode_type5(struct stream
*s
, const struct prefix
*p
,
4310 const struct prefix_rd
*prd
,
4311 mpls_label_t
*label
, uint32_t num_labels
,
4316 const struct evpn_addr
*p_evpn_p
;
4318 memset(&temp
, 0, 16);
4319 if (p
->family
!= AF_EVPN
)
4321 p_evpn_p
= &(p
->u
.prefix_evpn
);
4323 /* len denites the total len of IP and GW-IP in the route
4324 IP and GW-IP have to be both ipv4 or ipv6
4326 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4327 len
= 8; /* IP and GWIP are both ipv4 */
4329 len
= 32; /* IP and GWIP are both ipv6 */
4330 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4331 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4332 stream_put(s
, prd
->val
, 8);
4334 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
4336 stream_put(s
, &temp
, 10);
4337 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4338 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4339 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4340 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4342 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4344 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4346 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
4348 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
4350 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4351 stream_put_ipv4(s
, 0);
4353 stream_put(s
, &temp
, 16);
4357 stream_put(s
, label
, 3);
4363 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4365 static void cleanup_vni_on_disable(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4367 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4369 /* Remove EVPN routes and schedule for processing. */
4370 delete_routes_for_vni(bgp
, vpn
);
4372 /* Clear "live" flag and see if hash needs to be freed. */
4373 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4374 if (!is_vni_configured(vpn
))
4375 bgp_evpn_free(bgp
, vpn
);
4379 * Free a VNI entry; iterator function called during cleanup.
4381 static void free_vni_entry(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4383 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4385 delete_all_vni_routes(bgp
, vpn
);
4386 bgp_evpn_free(bgp
, vpn
);
4390 * Derive AUTO import RT for BGP VRF - L3VNI
4392 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4394 struct bgp
*bgp_evpn
= NULL
;
4396 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4397 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4400 bgp_evpn
= bgp_get_evpn();
4403 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4407 * Delete AUTO import RT from BGP VRF - L3VNI
4409 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4411 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4415 * Derive AUTO export RT for BGP VRF - L3VNI
4417 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4419 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4420 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4424 * Delete AUTO export RT from BGP VRF - L3VNI
4426 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4428 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4431 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4433 struct bgp
*bgp_evpn
= NULL
;
4434 struct listnode
*node
= NULL
;
4435 struct bgpevpn
*vpn
= NULL
;
4437 bgp_evpn
= bgp_get_evpn();
4441 /* update all type-5 routes */
4442 update_advertise_vrf_routes(bgp_vrf
);
4444 /* update all type-2 routes */
4445 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4446 update_routes_for_vni(bgp_evpn
, vpn
);
4450 * Handle autort change for a given VNI.
4452 static void update_autort_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4454 struct bgpevpn
*vpn
= bucket
->data
;
4456 if (!is_import_rt_configured(vpn
)) {
4457 if (is_vni_live(vpn
))
4458 bgp_evpn_uninstall_routes(bgp
, vpn
);
4459 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4460 list_delete_all_node(vpn
->import_rtl
);
4461 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4462 if (is_vni_live(vpn
))
4463 bgp_evpn_install_routes(bgp
, vpn
);
4465 if (!is_export_rt_configured(vpn
)) {
4466 list_delete_all_node(vpn
->export_rtl
);
4467 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4468 if (is_vni_live(vpn
))
4469 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4477 /* withdraw type-5 route corresponding to ip prefix */
4478 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4479 afi_t afi
, safi_t safi
)
4482 struct prefix_evpn evp
;
4483 char buf
[PREFIX_STRLEN
];
4485 build_type5_prefix_from_ip_prefix(&evp
, p
);
4486 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4489 EC_BGP_EVPN_ROUTE_DELETE
,
4490 "%u failed to delete type-5 route for prefix %s in vrf %s",
4491 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4492 vrf_id_to_name(bgp_vrf
->vrf_id
));
4496 /* withdraw all type-5 routes for an address family */
4497 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4499 struct bgp_table
*table
= NULL
;
4500 struct bgp_node
*rn
= NULL
;
4501 struct bgp_path_info
*pi
;
4503 table
= bgp_vrf
->rib
[afi
][safi
];
4504 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4505 /* Only care about "selected" routes. Also ensure that
4506 * these are routes that are injectable into EVPN.
4508 /* TODO: Support for AddPath for EVPN. */
4509 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4510 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4511 && is_route_injectable_into_evpn(pi
)) {
4512 bgp_evpn_withdraw_type5_route(
4513 bgp_vrf
, bgp_node_get_prefix(rn
), afi
,
4522 * evpn - enable advertisement of default g/w
4524 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
4525 safi_t safi
, bool add
)
4527 struct prefix ip_prefix
;
4529 /* form the default prefix 0.0.0.0/0 */
4530 memset(&ip_prefix
, 0, sizeof(struct prefix
));
4531 ip_prefix
.family
= afi2family(afi
);
4534 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
4537 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
4544 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4545 * to this function correspond to those of the source IP prefix (best
4546 * path in the case of the attr. In the case of a local prefix (when we
4547 * are advertising local subnets), the src_attr will be NULL.
4549 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4550 struct attr
*src_attr
, afi_t afi
,
4554 struct prefix_evpn evp
;
4555 char buf
[PREFIX_STRLEN
];
4557 build_type5_prefix_from_ip_prefix(&evp
, p
);
4558 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4560 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4561 "%u: Failed to create type-5 route for prefix %s",
4562 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4565 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4566 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4567 * advertisement is enabled.
4569 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4572 struct bgp_table
*table
= NULL
;
4573 struct bgp_node
*rn
= NULL
;
4574 struct bgp_path_info
*pi
;
4576 table
= bgp_vrf
->rib
[afi
][safi
];
4577 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4578 /* Need to identify the "selected" route entry to use its
4579 * attribute. Also, ensure that the route is injectable
4581 * TODO: Support for AddPath for EVPN.
4583 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4584 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4585 && is_route_injectable_into_evpn(pi
)) {
4587 /* apply the route-map */
4588 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4589 route_map_result_t ret
;
4590 struct bgp_path_info tmp_pi
;
4591 struct bgp_path_info_extra tmp_pie
;
4592 struct attr tmp_attr
;
4594 tmp_attr
= *pi
->attr
;
4596 /* Fill temp path_info */
4597 prep_for_rmap_apply(
4598 &tmp_pi
, &tmp_pie
, rn
, pi
,
4599 pi
->peer
, &tmp_attr
);
4601 RESET_FLAG(tmp_attr
.rmap_change_flags
);
4603 ret
= route_map_apply(
4604 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4606 bgp_node_get_prefix(rn
),
4608 if (ret
== RMAP_DENYMATCH
) {
4609 bgp_attr_flush(&tmp_attr
);
4612 bgp_evpn_advertise_type5_route(
4614 bgp_node_get_prefix(rn
),
4615 &tmp_attr
, afi
, safi
);
4617 bgp_evpn_advertise_type5_route(
4619 bgp_node_get_prefix(rn
),
4620 pi
->attr
, afi
, safi
);
4627 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4629 struct listnode
*node
, *nnode
, *node_to_del
;
4630 struct ecommunity
*ecom
, *ecom_auto
;
4631 struct ecommunity_val eval
;
4633 if (bgp
->advertise_autort_rfc8365
)
4634 vni
|= EVPN_AUTORT_VXLAN
;
4635 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4637 ecom_auto
= ecommunity_new();
4638 ecommunity_add_val(ecom_auto
, &eval
, false, false);
4641 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4642 if (ecommunity_match(ecom
, ecom_auto
)) {
4643 ecommunity_free(&ecom
);
4649 list_delete_node(rtl
, node_to_del
);
4651 ecommunity_free(&ecom_auto
);
4654 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4655 struct ecommunity
*ecomadd
)
4657 /* uninstall routes from vrf */
4658 if (is_l3vni_live(bgp_vrf
))
4659 uninstall_routes_for_vrf(bgp_vrf
);
4661 /* Cleanup the RT to VRF mapping */
4662 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4664 /* Remove auto generated RT */
4665 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4667 /* Add the newly configured RT to RT list */
4668 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4669 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4671 /* map VRF to its RTs and install routes matching the new RTs */
4672 if (is_l3vni_live(bgp_vrf
)) {
4673 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4674 install_routes_for_vrf(bgp_vrf
);
4678 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4679 struct ecommunity
*ecomdel
)
4681 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4682 struct ecommunity
*ecom
= NULL
;
4684 /* uninstall routes from vrf */
4685 if (is_l3vni_live(bgp_vrf
))
4686 uninstall_routes_for_vrf(bgp_vrf
);
4688 /* Cleanup the RT to VRF mapping */
4689 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4691 /* remove the RT from the RT list */
4692 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4693 if (ecommunity_match(ecom
, ecomdel
)) {
4694 ecommunity_free(&ecom
);
4701 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4703 assert(bgp_vrf
->vrf_import_rtl
);
4704 /* fallback to auto import rt, if this was the last RT */
4705 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4706 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4707 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4710 /* map VRFs to its RTs and install routes matching this new RT */
4711 if (is_l3vni_live(bgp_vrf
)) {
4712 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4713 install_routes_for_vrf(bgp_vrf
);
4717 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4718 struct ecommunity
*ecomadd
)
4720 /* remove auto-generated RT */
4721 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4723 /* Add the new RT to the RT list */
4724 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4725 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4727 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4730 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4731 struct ecommunity
*ecomdel
)
4733 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4734 struct ecommunity
*ecom
= NULL
;
4736 /* Remove the RT from the RT list */
4737 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4738 if (ecommunity_match(ecom
, ecomdel
)) {
4739 ecommunity_free(&ecom
);
4746 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4749 * Temporary assert to make SA happy.
4750 * The ALL_LIST_ELEMENTS macro above has a NULL check
4751 * which means that SA is going to complain about
4752 * the list_isempty call, which doesn't NULL check.
4753 * So until we get this situation cleaned up, here
4756 assert(bgp_vrf
->vrf_export_rtl
);
4758 /* fall back to auto-generated RT if this was the last RT */
4759 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4760 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4761 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4764 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4768 * Handle change to BGP router id. This is invoked twice by the change
4769 * handler, first before the router id has been changed and then after
4770 * the router id has been changed. The first invocation will result in
4771 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4772 * will result in the routes being re-advertised.
4774 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4776 struct listnode
*node
;
4777 struct bgp
*bgp_vrf
;
4781 /* delete and withdraw all the type-5 routes
4782 stored in the global table for this vrf
4784 withdraw_router_id_vrf(bgp
);
4786 /* delete all the VNI routes (type-2/type-3) routes for all the
4789 hash_iterate(bgp
->vnihash
,
4790 (void (*)(struct hash_bucket
*,
4791 void *))withdraw_router_id_vni
,
4794 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4795 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4796 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4797 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4799 bgp_vrf
->evpn_info
->pip_ip
.s_addr
4805 /* Assign new default instance router-id */
4806 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4807 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4808 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4809 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4811 bgp_vrf
->evpn_info
->pip_ip
=
4813 /* advertise type-5 routes with
4816 update_advertise_vrf_routes(bgp_vrf
);
4821 /* advertise all routes in the vrf as type-5 routes with the new
4824 update_router_id_vrf(bgp
);
4826 /* advertise all the VNI routes (type-2/type-3) routes with the
4829 hash_iterate(bgp
->vnihash
,
4830 (void (*)(struct hash_bucket
*,
4831 void *))update_router_id_vni
,
4837 * Handle change to auto-RT algorithm - update and advertise local routes.
4839 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4841 hash_iterate(bgp
->vnihash
,
4842 (void (*)(struct hash_bucket
*,
4843 void*))update_autort_vni
,
4848 * Handle change to export RT - update and advertise local routes.
4850 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4852 return update_routes_for_vni(bgp
, vpn
);
4855 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4858 delete_withdraw_vrf_routes(bgp_vrf
);
4860 update_advertise_vrf_routes(bgp_vrf
);
4864 * Handle change to RD. This is invoked twice by the change handler,
4865 * first before the RD has been changed and then after the RD has
4866 * been changed. The first invocation will result in local routes
4867 * of this VNI being deleted and withdrawn and the next will result
4868 * in the routes being re-advertised.
4870 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4874 delete_withdraw_vni_routes(bgp
, vpn
);
4876 update_advertise_vni_routes(bgp
, vpn
);
4880 * Install routes for this VNI. Invoked upon change to Import RT.
4882 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4884 return install_routes_for_vni(bgp
, vpn
);
4888 * Uninstall all routes installed for this VNI. Invoked upon change
4891 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4893 return uninstall_routes_for_vni(bgp
, vpn
);
4897 * TODO: Hardcoded for a maximum of 2 VNIs right now
4899 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4904 vni1
= label2vni(label
);
4905 if (num_labels
== 2) {
4906 vni2
= label2vni(label
+ 1);
4907 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4909 snprintf(buf
, len
, "%u", vni1
);
4914 * Function to convert evpn route to json format.
4915 * NOTE: We don't use prefix2str as the output here is a bit different.
4917 void bgp_evpn_route2json(const struct prefix_evpn
*p
, json_object
*json
)
4919 char buf1
[ETHER_ADDR_STRLEN
];
4920 char buf2
[PREFIX2STR_BUFFER
];
4927 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4929 switch (p
->prefix
.route_type
) {
4930 case BGP_EVPN_MAC_IP_ROUTE
:
4931 json_object_int_add(json
, "ethTag",
4932 p
->prefix
.macip_addr
.eth_tag
);
4933 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4934 json_object_string_add(json
, "mac",
4935 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4938 if (!is_evpn_prefix_ipaddr_none(p
)) {
4939 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
:
4941 prefixlen
= (family
== AF_INET
) ?
4942 IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
;
4943 inet_ntop(family
, &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4944 buf2
, PREFIX2STR_BUFFER
);
4945 json_object_int_add(json
, "ipLen", prefixlen
);
4946 json_object_string_add(json
, "ip", buf2
);
4950 case BGP_EVPN_IMET_ROUTE
:
4951 json_object_int_add(json
, "ethTag",
4952 p
->prefix
.imet_addr
.eth_tag
);
4953 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4954 prefixlen
= (family
== AF_INET
) ? IPV4_MAX_BITLEN
:
4956 inet_ntop(family
, &p
->prefix
.imet_addr
.ip
.ip
.addr
, buf2
,
4958 json_object_int_add(json
, "ipLen", prefixlen
);
4959 json_object_string_add(json
, "ip", buf2
);
4962 case BGP_EVPN_IP_PREFIX_ROUTE
:
4963 json_object_int_add(json
, "ethTag",
4964 p
->prefix
.prefix_addr
.eth_tag
);
4965 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4966 inet_ntop(family
, &p
->prefix
.prefix_addr
.ip
.ip
.addr
,
4967 buf2
, sizeof(buf2
));
4968 json_object_int_add(json
, "ipLen",
4969 p
->prefix
.prefix_addr
.ip_prefix_length
);
4970 json_object_string_add(json
, "ip", buf2
);
4979 * Function to convert evpn route to string.
4980 * NOTE: We don't use prefix2str as the output here is a bit different.
4982 char *bgp_evpn_route2str(const struct prefix_evpn
*p
, char *buf
, int len
)
4984 char buf1
[ETHER_ADDR_STRLEN
];
4985 char buf2
[PREFIX2STR_BUFFER
];
4986 char buf3
[ESI_STR_LEN
];
4988 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4989 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
4990 p
->prefix
.imet_addr
.eth_tag
,
4991 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4993 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4994 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4995 if (is_evpn_prefix_ipaddr_none(p
))
4996 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4997 p
->prefix
.route_type
,
4998 p
->prefix
.macip_addr
.eth_tag
,
5000 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
5005 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
5007 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
5008 p
->prefix
.route_type
,
5009 p
->prefix
.macip_addr
.eth_tag
,
5011 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
5013 family
== AF_INET
? IPV4_MAX_BITLEN
5016 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
5018 PREFIX2STR_BUFFER
));
5020 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
5021 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
5022 p
->prefix
.route_type
,
5023 p
->prefix
.prefix_addr
.eth_tag
,
5024 p
->prefix
.prefix_addr
.ip_prefix_length
,
5025 is_evpn_prefix_ipaddr_v4(p
)
5026 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
5027 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
5028 } else if (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
5029 snprintf(buf
, len
, "[%d]:[%s]:[%d]:[%s]",
5030 p
->prefix
.route_type
,
5031 esi_to_str(&p
->prefix
.es_addr
.esi
, buf3
, sizeof(buf3
)),
5032 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
5034 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
));
5036 /* For EVPN route types not supported yet. */
5037 snprintf(buf
, len
, "(unsupported route type %d)",
5038 p
->prefix
.route_type
);
5045 * Encode EVPN prefix in Update (MP_REACH)
5047 void bgp_evpn_encode_prefix(struct stream
*s
, const struct prefix
*p
,
5048 const struct prefix_rd
*prd
, mpls_label_t
*label
,
5049 uint32_t num_labels
, struct attr
*attr
,
5050 int addpath_encode
, uint32_t addpath_tx_id
)
5052 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
5053 int len
, ipa_len
= 0;
5056 stream_putl(s
, addpath_tx_id
);
5059 stream_putc(s
, evp
->prefix
.route_type
);
5061 switch (evp
->prefix
.route_type
) {
5062 case BGP_EVPN_MAC_IP_ROUTE
:
5063 if (is_evpn_prefix_ipaddr_v4(evp
))
5064 ipa_len
= IPV4_MAX_BYTELEN
;
5065 else if (is_evpn_prefix_ipaddr_v6(evp
))
5066 ipa_len
= IPV6_MAX_BYTELEN
;
5067 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
5068 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
5069 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
5071 stream_putc(s
, len
);
5072 stream_put(s
, prd
->val
, 8); /* RD */
5074 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
5076 stream_put(s
, 0, 10);
5077 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
5078 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
5079 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
5080 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
5081 if (ipa_len
) /* IP */
5082 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
5084 /* 1st label is the L2 VNI */
5085 stream_put(s
, label
, BGP_LABEL_BYTES
);
5086 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
5087 if (ipa_len
&& num_labels
> 1)
5088 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
5091 case BGP_EVPN_IMET_ROUTE
:
5092 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
5093 stream_put(s
, prd
->val
, 8); /* RD */
5094 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
5095 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
5096 /* Originating Router's IP Addr */
5097 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
5100 case BGP_EVPN_ES_ROUTE
:
5101 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
5102 stream_put(s
, prd
->val
, 8); /* RD */
5103 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
5104 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
5106 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
5109 case BGP_EVPN_IP_PREFIX_ROUTE
:
5110 /* TODO: AddPath support. */
5111 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
5119 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
5120 struct bgp_nlri
*packet
, int withdraw
)
5126 uint32_t addpath_id
;
5127 int addpath_encoded
;
5132 /* Start processing the NLRI - there may be multiple in the MP_REACH */
5134 lim
= pnt
+ packet
->length
;
5136 safi
= packet
->safi
;
5140 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
5141 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5142 PEER_CAP_ADDPATH_AF_TX_RCV
));
5144 for (; pnt
< lim
; pnt
+= psize
) {
5145 /* Clear prefix structure. */
5146 memset(&p
, 0, sizeof(struct prefix
));
5148 /* Deal with path-id if AddPath is supported. */
5149 if (addpath_encoded
) {
5150 /* When packet overflow occurs return immediately. */
5151 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
5152 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5154 memcpy(&addpath_id
, pnt
, BGP_ADDPATH_ID_LEN
);
5155 addpath_id
= ntohl(addpath_id
);
5156 pnt
+= BGP_ADDPATH_ID_LEN
;
5159 /* All EVPN NLRI types start with type and length. */
5161 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE
;
5166 /* When packet overflow occur return immediately. */
5167 if (pnt
+ psize
> lim
)
5168 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5171 case BGP_EVPN_MAC_IP_ROUTE
:
5172 if (process_type2_route(peer
, afi
, safi
,
5173 withdraw
? NULL
: attr
, pnt
,
5174 psize
, addpath_id
)) {
5177 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
5178 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5179 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE
;
5183 case BGP_EVPN_IMET_ROUTE
:
5184 if (process_type3_route(peer
, afi
, safi
,
5185 withdraw
? NULL
: attr
, pnt
,
5186 psize
, addpath_id
)) {
5189 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
5190 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5191 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE
;
5195 case BGP_EVPN_ES_ROUTE
:
5196 if (process_type4_route(peer
, afi
, safi
,
5197 withdraw
? NULL
: attr
, pnt
,
5198 psize
, addpath_id
)) {
5201 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
5202 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5203 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE
;
5207 case BGP_EVPN_IP_PREFIX_ROUTE
:
5208 if (process_type5_route(peer
, afi
, safi
,
5209 withdraw
? NULL
: attr
, pnt
,
5210 psize
, addpath_id
)) {
5213 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
5214 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5215 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE
;
5224 /* Packet length consistency check. */
5226 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH
;
5228 return BGP_NLRI_PARSE_OK
;
5232 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
5233 * The mapping will be used during route processing.
5234 * bgp_def: default bgp instance
5235 * bgp_vrf: specific bgp vrf instance on which RT is configured
5237 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
5240 struct ecommunity_val
*eval
= NULL
;
5241 struct listnode
*node
= NULL
, *nnode
= NULL
;
5242 struct ecommunity
*ecom
= NULL
;
5244 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5245 for (i
= 0; i
< ecom
->size
; i
++) {
5246 eval
= (struct ecommunity_val
*)(ecom
->val
5248 * ECOMMUNITY_SIZE
));
5249 map_vrf_to_rt(bgp_vrf
, eval
);
5255 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
5257 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
5260 struct ecommunity_val
*eval
;
5261 struct listnode
*node
, *nnode
;
5262 struct ecommunity
*ecom
;
5264 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5265 for (i
= 0; i
< ecom
->size
; i
++) {
5266 struct vrf_irt_node
*irt
;
5267 struct ecommunity_val eval_tmp
;
5269 eval
= (struct ecommunity_val
*)(ecom
->val
5271 * ECOMMUNITY_SIZE
));
5272 /* If using "automatic" RT, we only care about the
5273 * local-admin sub-field.
5274 * This is to facilitate using VNI as the RT for EBGP
5277 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5278 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
5279 BGP_VRF_IMPORT_RT_CFGD
))
5280 mask_ecom_global_admin(&eval_tmp
, eval
);
5282 irt
= lookup_vrf_import_rt(&eval_tmp
);
5284 unmap_vrf_from_rt(bgp_vrf
, irt
);
5291 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
5292 * The mapping will be used during route processing.
5294 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5297 struct ecommunity_val
*eval
;
5298 struct listnode
*node
, *nnode
;
5299 struct ecommunity
*ecom
;
5301 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5302 for (i
= 0; i
< ecom
->size
; i
++) {
5303 eval
= (struct ecommunity_val
*)(ecom
->val
5305 * ECOMMUNITY_SIZE
));
5306 map_vni_to_rt(bgp
, vpn
, eval
);
5312 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
5314 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5317 struct ecommunity_val
*eval
;
5318 struct listnode
*node
, *nnode
;
5319 struct ecommunity
*ecom
;
5321 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5322 for (i
= 0; i
< ecom
->size
; i
++) {
5323 struct irt_node
*irt
;
5324 struct ecommunity_val eval_tmp
;
5326 eval
= (struct ecommunity_val
*)(ecom
->val
5328 * ECOMMUNITY_SIZE
));
5329 /* If using "automatic" RT, we only care about the
5330 * local-admin sub-field.
5331 * This is to facilitate using VNI as the RT for EBGP
5334 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5335 if (!is_import_rt_configured(vpn
))
5336 mask_ecom_global_admin(&eval_tmp
, eval
);
5338 irt
= lookup_import_rt(bgp
, &eval_tmp
);
5340 unmap_vni_from_rt(bgp
, vpn
, irt
);
5346 * Derive Import RT automatically for VNI and map VNI to RT.
5347 * The mapping will be used during route processing.
5349 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5351 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
5352 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
5355 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
5359 * Derive Export RT automatically for VNI.
5361 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5363 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
5364 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5368 * Derive RD automatically for VNI using passed information - it
5369 * is of the form RouterId:unique-id-for-vni.
5371 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5373 if (is_vrf_rd_configured(bgp
))
5376 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5380 * Derive RD automatically for VNI using passed information - it
5381 * is of the form RouterId:unique-id-for-vni.
5383 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5387 vpn
->prd
.family
= AF_UNSPEC
;
5388 vpn
->prd
.prefixlen
= 64;
5389 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
5390 (void)str2prefix_rd(buf
, &vpn
->prd
);
5391 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5397 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5399 struct list
*inst
= bm
->bgp
;
5400 struct listnode
*node
;
5401 struct bgp
*bgp_vrf
;
5403 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5404 if (bgp_vrf
->l3vni
== vni
)
5414 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5416 struct bgpevpn
*vpn
;
5419 memset(&tmp
, 0, sizeof(struct bgpevpn
));
5421 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5426 * Create a new vpn - invoked upon configuration or zebra notification.
5428 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5429 struct in_addr originator_ip
,
5430 vrf_id_t tenant_vrf_id
,
5431 struct in_addr mcast_grp
)
5433 struct bgpevpn
*vpn
;
5438 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5440 /* Set values - RD and RT set to defaults. */
5442 vpn
->originator_ip
= originator_ip
;
5443 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5444 vpn
->mcast_grp
= mcast_grp
;
5446 /* Initialize route-target import and export lists */
5447 vpn
->import_rtl
= list_new();
5448 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5449 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
5450 vpn
->export_rtl
= list_new();
5451 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5452 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
5453 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5454 derive_rd_rt_for_vni(bgp
, vpn
);
5456 /* Initialize EVPN route table. */
5457 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5460 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
5461 XFREE(MTYPE_BGP_EVPN
, vpn
);
5465 /* add to l2vni list on corresponding vrf */
5466 bgpevpn_link_to_l3vni(vpn
);
5468 QOBJ_REG(vpn
, bgpevpn
);
5473 * Free a given VPN - called in multiple scenarios such as zebra
5474 * notification, configuration being deleted, advertise-all-vni disabled etc.
5475 * This just frees appropriate memory, caller should have taken other
5478 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5480 bgpevpn_unlink_from_l3vni(vpn
);
5481 bgp_table_unlock(vpn
->route_table
);
5482 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5483 list_delete(&vpn
->import_rtl
);
5484 list_delete(&vpn
->export_rtl
);
5485 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5486 hash_release(bgp
->vnihash
, vpn
);
5488 XFREE(MTYPE_BGP_EVPN
, vpn
);
5494 struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
)
5499 memset(&tmp
, 0, sizeof(struct evpnes
));
5500 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
5501 es
= hash_lookup(bgp
->esihash
, &tmp
);
5506 * Create a new local es - invoked upon zebra notification.
5508 struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
,
5510 struct ipaddr
*originator_ip
)
5518 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct evpnes
));
5520 /* set the ESI and originator_ip */
5521 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
5522 memcpy(&es
->originator_ip
, originator_ip
, sizeof(struct ipaddr
));
5524 /* Initialise the VTEP list */
5525 es
->vtep_list
= list_new();
5526 es
->vtep_list
->cmp
= evpn_vtep_ip_cmp
;
5528 /* auto derive RD for this es */
5529 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
5530 es
->prd
.family
= AF_UNSPEC
;
5531 es
->prd
.prefixlen
= 64;
5532 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), es
->rd_id
);
5533 (void)str2prefix_rd(buf
, &es
->prd
);
5535 /* Initialize the ES route table */
5536 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5539 if (!hash_get(bgp
->esihash
, es
, hash_alloc_intern
)) {
5540 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5544 QOBJ_REG(es
, evpnes
);
5550 * This just frees appropriate memory, caller should have taken other
5553 void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
)
5555 list_delete(&es
->vtep_list
);
5556 bgp_table_unlock(es
->route_table
);
5557 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
5558 hash_release(bgp
->esihash
, es
);
5560 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5564 * Import evpn route from global table to VNI/VRF/ESI.
5566 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5567 const struct prefix
*p
, struct bgp_path_info
*pi
)
5569 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5573 * Unimport evpn route from VNI/VRF/ESI.
5575 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5576 const struct prefix
*p
, struct bgp_path_info
*pi
)
5578 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5581 /* filter routes which have martian next hops */
5582 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5586 struct bgp_node
*rd_rn
, *rn
;
5587 struct bgp_table
*table
;
5588 struct bgp_path_info
*pi
;
5593 /* Walk entire global routing table and evaluate routes which could be
5594 * imported into this VPN. Note that we cannot just look at the routes
5595 * for the VNI's RD -
5596 * remote routes applicable for this VNI could have any RD.
5598 /* EVPN routes are a 2-level table. */
5599 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
5600 rd_rn
= bgp_route_next(rd_rn
)) {
5601 table
= bgp_node_get_bgp_table_info(rd_rn
);
5605 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5607 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
5610 /* Consider "valid" remote routes applicable for
5612 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5613 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5615 if (bgp_nexthop_self(bgp
, afi
,
5616 pi
->type
, pi
->sub_type
,
5618 const struct prefix
*p
=
5619 bgp_node_get_prefix(rn
);
5621 if (bgp_debug_update(pi
->peer
, p
, NULL
,
5623 char attr_str
[BUFSIZ
] = {0};
5625 bgp_dump_attr(pi
->attr
,
5629 "%u: prefix %pRN with attr %s - DENIED due to martian or self nexthop",
5633 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5636 bgp_rib_remove(rn
, pi
, pi
->peer
, afi
,
5647 * Handle del of a local MACIP.
5649 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5650 struct ipaddr
*ip
, int state
)
5652 struct bgpevpn
*vpn
;
5653 struct prefix_evpn p
;
5654 struct bgp_node
*rn
;
5656 /* Lookup VNI hash - should exist. */
5657 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5658 if (!vpn
|| !is_vni_live(vpn
)) {
5659 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5660 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5661 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5665 build_evpn_type2_prefix(&p
, mac
, ip
);
5666 if (state
== ZEBRA_NEIGH_ACTIVE
) {
5667 /* Remove EVPN type-2 route and schedule for processing. */
5668 delete_evpn_route(bgp
, vpn
, &p
);
5670 /* Re-instate the current remote best path if any */
5671 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
5673 evpn_zebra_reinstall_best_route(bgp
, vpn
, rn
);
5680 * Handle add of a local MACIP.
5682 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5683 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
)
5685 struct bgpevpn
*vpn
;
5686 struct prefix_evpn p
;
5688 /* Lookup VNI hash - should exist. */
5689 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5690 if (!vpn
|| !is_vni_live(vpn
)) {
5691 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5692 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5693 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5697 /* Create EVPN type-2 route and schedule for processing. */
5698 build_evpn_type2_prefix(&p
, mac
, ip
);
5699 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
)) {
5700 char buf
[ETHER_ADDR_STRLEN
];
5701 char buf2
[INET6_ADDRSTRLEN
];
5704 EC_BGP_EVPN_ROUTE_CREATE
,
5705 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5706 bgp
->vrf_id
, vpn
->vni
,
5707 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5710 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5711 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5718 static void link_l2vni_hash_to_l3vni(struct hash_bucket
*bucket
,
5719 struct bgp
*bgp_vrf
)
5721 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
5722 struct bgp
*bgp_evpn
= NULL
;
5724 bgp_evpn
= bgp_get_evpn();
5727 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5728 bgpevpn_link_to_l3vni(vpn
);
5731 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
,
5732 struct ethaddr
*svi_rmac
,
5733 struct ethaddr
*vrr_rmac
,
5734 struct in_addr originator_ip
, int filter
,
5735 ifindex_t svi_ifindex
,
5736 bool is_anycast_mac
)
5738 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5739 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5740 struct listnode
*node
= NULL
;
5741 struct bgpevpn
*vpn
= NULL
;
5744 /* get the EVPN instance - required to get the AS number for VRF
5747 bgp_evpn
= bgp_get_evpn();
5751 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
5757 /* if the BGP vrf instance doesn't exist - create one */
5758 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5763 ret
= bgp_get_vty(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5764 vrf_id
== VRF_DEFAULT
5765 ? BGP_INSTANCE_TYPE_DEFAULT
5766 : BGP_INSTANCE_TYPE_VRF
);
5768 case BGP_ERR_AS_MISMATCH
:
5769 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5770 "BGP is already running; AS is %u\n", as
);
5772 case BGP_ERR_INSTANCE_MISMATCH
:
5773 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5774 "BGP instance name and AS number mismatch\n");
5778 /* mark as auto created */
5779 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5782 /* associate the vrf with l3vni and related parameters */
5783 bgp_vrf
->l3vni
= l3vni
;
5784 bgp_vrf
->originator_ip
= originator_ip
;
5785 bgp_vrf
->l3vni_svi_ifindex
= svi_ifindex
;
5786 bgp_vrf
->evpn_info
->is_anycast_mac
= is_anycast_mac
;
5788 /* copy anycast MAC from VRR MAC */
5789 memcpy(&bgp_vrf
->rmac
, vrr_rmac
, ETH_ALEN
);
5790 /* copy sys RMAC from SVI MAC */
5791 memcpy(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, svi_rmac
, ETH_ALEN
);
5792 /* PIP user configured mac is not present use svi mac as sys mac */
5793 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
))
5794 memcpy(&bgp_vrf
->evpn_info
->pip_rmac
, svi_rmac
, ETH_ALEN
);
5796 if (bgp_debug_zebra(NULL
)) {
5797 char buf
[ETHER_ADDR_STRLEN
];
5798 char buf1
[ETHER_ADDR_STRLEN
];
5799 char buf2
[ETHER_ADDR_STRLEN
];
5801 zlog_debug("VRF %s vni %u pip %s RMAC %s sys RMAC %s static RMAC %s is_anycast_mac %s",
5802 vrf_id_to_name(bgp_vrf
->vrf_id
),
5804 bgp_vrf
->evpn_info
->advertise_pip
? "enable"
5806 prefix_mac2str(&bgp_vrf
->rmac
, buf
, sizeof(buf
)),
5807 prefix_mac2str(&bgp_vrf
->evpn_info
->pip_rmac
,
5808 buf1
, sizeof(buf1
)),
5809 prefix_mac2str(&bgp_vrf
->evpn_info
->pip_rmac_static
,
5810 buf2
, sizeof(buf2
)),
5811 is_anycast_mac
? "Enable" : "Disable");
5813 /* set the right filter - are we using l3vni only for prefix routes? */
5815 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5817 /* Map auto derive or configured RTs */
5818 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5819 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5821 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
5823 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5824 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5826 /* auto derive RD */
5827 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5829 /* link all corresponding l2vnis */
5830 hash_iterate(bgp_evpn
->vnihash
,
5831 (void (*)(struct hash_bucket
*,
5832 void *))link_l2vni_hash_to_l3vni
,
5835 /* Only update all corresponding type-2 routes if we are advertising two
5836 * labels along with type-2 routes
5839 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5840 update_routes_for_vni(bgp_evpn
, vpn
);
5842 /* advertise type-5 routes if needed */
5843 update_advertise_vrf_routes(bgp_vrf
);
5845 /* install all remote routes belonging to this l3vni into correspondng
5847 install_routes_for_vrf(bgp_vrf
);
5852 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5854 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5855 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5856 struct listnode
*node
= NULL
;
5857 struct listnode
*next
= NULL
;
5858 struct bgpevpn
*vpn
= NULL
;
5860 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5864 "Cannot process L3VNI %u Del - Could not find BGP instance",
5869 bgp_evpn
= bgp_get_evpn();
5873 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
5878 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5879 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5880 * routes. This will uninstalling the routes from zebra and decremnt the
5883 uninstall_routes_for_vrf(bgp_vrf
);
5885 /* delete/withdraw all type-5 routes */
5886 delete_withdraw_vrf_routes(bgp_vrf
);
5888 /* remove the l3vni from vrf instance */
5891 /* remove the Rmac from the BGP vrf */
5892 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5893 memset(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, 0, ETH_ALEN
);
5894 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
) &&
5895 !is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac
))
5896 memset(&bgp_vrf
->evpn_info
->pip_rmac
, 0, ETH_ALEN
);
5898 /* remove default import RT or Unmap non-default import RT */
5899 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5900 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5901 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5902 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5905 /* remove default export RT */
5906 if (!list_isempty(bgp_vrf
->vrf_export_rtl
) &&
5907 !CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
5908 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5911 /* update all corresponding local mac-ip routes */
5912 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5913 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5914 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5915 update_routes_for_vni(bgp_evpn
, vpn
);
5919 /* If any L2VNIs point to this instance, unlink them. */
5920 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5921 bgpevpn_unlink_from_l3vni(vpn
);
5923 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5925 /* Delete the instance if it was autocreated */
5926 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5927 bgp_delete(bgp_vrf
);
5933 * Handle del of a local VNI.
5935 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5937 struct bgpevpn
*vpn
;
5939 /* Locate VNI hash */
5940 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5942 if (bgp_debug_zebra(NULL
))
5944 EC_BGP_EVPN_VPN_VNI
,
5945 "%u: VNI hash entry for VNI %u not found at DEL",
5950 /* Remove all local EVPN routes and schedule for processing (to
5951 * withdraw from peers).
5953 delete_routes_for_vni(bgp
, vpn
);
5956 * tunnel is no longer active, del tunnel ip address from tip_hash
5958 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5960 /* Clear "live" flag and see if hash needs to be freed. */
5961 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5962 if (!is_vni_configured(vpn
))
5963 bgp_evpn_free(bgp
, vpn
);
5969 * Handle add (or update) of a local VNI. The VNI changes we care
5970 * about are for the local-tunnel-ip and the (tenant) VRF.
5972 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5973 struct in_addr originator_ip
,
5974 vrf_id_t tenant_vrf_id
,
5975 struct in_addr mcast_grp
)
5978 struct bgpevpn
*vpn
;
5979 struct prefix_evpn p
;
5981 /* Lookup VNI. If present and no change, exit. */
5982 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5985 if (is_vni_live(vpn
)
5986 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5987 && IPV4_ADDR_SAME(&vpn
->mcast_grp
, &mcast_grp
)
5988 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5989 /* Probably some other param has changed that we don't
5993 bgp_evpn_mcast_grp_change(bgp
, vpn
, mcast_grp
);
5995 /* Update tenant_vrf_id if it has changed. */
5996 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5997 bgpevpn_unlink_from_l3vni(vpn
);
5998 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5999 bgpevpn_link_to_l3vni(vpn
);
6002 /* If tunnel endpoint IP has changed, update (and delete prior
6003 * type-3 route, if needed.)
6005 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
6006 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
6008 /* Update all routes with new endpoint IP and/or export RT
6011 if (is_vni_live(vpn
))
6012 update_routes_for_vni(bgp
, vpn
);
6015 /* Create or update as appropriate. */
6017 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
,
6022 "%u: Failed to allocate VNI entry for VNI %u - at Add",
6028 /* if the VNI is live already, there is nothing more to do */
6029 if (is_vni_live(vpn
))
6032 /* Mark as "live" */
6033 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
6035 /* tunnel is now active, add tunnel-ip to db */
6036 bgp_tip_add(bgp
, &originator_ip
);
6038 /* filter routes as nexthop database has changed */
6039 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
6042 * Create EVPN type-3 route and schedule for processing.
6044 * RT-3 only if doing head-end replication
6046 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
6047 == VXLAN_FLOOD_HEAD_END_REPL
) {
6048 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
6049 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0)) {
6050 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
6051 "%u: Type3 route creation failure for VNI %u",
6057 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
6061 install_routes_for_vni(bgp
, vpn
);
6063 /* If we are advertising gateway mac-ip
6064 It needs to be conveyed again to zebra */
6065 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
6067 /* advertise svi mac-ip knob to zebra */
6068 bgp_zebra_advertise_svi_macip(bgp
, vpn
->advertise_svi_macip
, vpn
->vni
);
6074 * bgp_evpn_local_es_del
6076 int bgp_evpn_local_es_del(struct bgp
*bgp
,
6078 struct ipaddr
*originator_ip
)
6080 char buf
[ESI_STR_LEN
];
6081 struct evpnes
*es
= NULL
;
6083 if (!bgp
->esihash
) {
6084 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
6089 /* Lookup ESI hash - should exist. */
6090 es
= bgp_evpn_lookup_es(bgp
, esi
);
6092 flog_warn(EC_BGP_EVPN_ESI
,
6093 "%u: ESI hash entry for ESI %s at Local ES DEL",
6094 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
6098 /* Delete all local EVPN ES routes from ESI table
6099 * and schedule for processing (to withdraw from peers))
6101 delete_routes_for_es(bgp
, es
);
6103 /* free the hash entry */
6104 bgp_evpn_es_free(bgp
, es
);
6110 * bgp_evpn_local_es_add
6112 int bgp_evpn_local_es_add(struct bgp
*bgp
,
6114 struct ipaddr
*originator_ip
)
6116 char buf
[ESI_STR_LEN
];
6117 struct evpnes
*es
= NULL
;
6118 struct prefix_evpn p
;
6120 if (!bgp
->esihash
) {
6121 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
6126 /* create the new es */
6127 es
= bgp_evpn_lookup_es(bgp
, esi
);
6129 es
= bgp_evpn_es_new(bgp
, esi
, originator_ip
);
6133 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
6134 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
6138 UNSET_FLAG(es
->flags
, EVPNES_REMOTE
);
6139 SET_FLAG(es
->flags
, EVPNES_LOCAL
);
6141 build_evpn_type4_prefix(&p
, esi
, originator_ip
->ipaddr_v4
);
6142 if (update_evpn_type4_route(bgp
, es
, &p
)) {
6143 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
6144 "%u: Type4 route creation failure for ESI %s",
6145 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
6149 /* import all remote ES routes in th ES table */
6150 install_routes_for_es(bgp
, es
);
6156 * Handle change in setting for BUM handling. The supported values
6157 * are head-end replication and dropping all BUM packets. Any change
6158 * should be registered with zebra. Also, if doing head-end replication,
6159 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
6160 * to be dropped, the RT-3s must be withdrawn.
6162 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
6164 zlog_info("L2VPN EVPN BUM handling is %s",
6165 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
6166 "Flooding" : "Flooding Disabled");
6168 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
6169 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
6170 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
6171 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
6172 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
6176 * Cleanup EVPN information on disable - Need to delete and withdraw
6177 * EVPN routes from peers.
6179 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
6181 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_bucket
*,
6182 void *))cleanup_vni_on_disable
,
6187 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
6188 * BGP instance (default) is being freed.
6190 void bgp_evpn_cleanup(struct bgp
*bgp
)
6192 hash_iterate(bgp
->vnihash
,
6193 (void (*)(struct hash_bucket
*, void *))free_vni_entry
,
6196 hash_free(bgp
->import_rt_hash
);
6197 bgp
->import_rt_hash
= NULL
;
6199 hash_free(bgp
->vrf_import_rt_hash
);
6200 bgp
->vrf_import_rt_hash
= NULL
;
6202 hash_free(bgp
->vnihash
);
6203 bgp
->vnihash
= NULL
;
6205 hash_free(bgp
->esihash
);
6206 bgp
->esihash
= NULL
;
6208 list_delete(&bgp
->vrf_import_rtl
);
6209 list_delete(&bgp
->vrf_export_rtl
);
6210 list_delete(&bgp
->l2vnis
);
6214 * Initialization for EVPN
6217 * hash for RT to VNI
6219 void bgp_evpn_init(struct bgp
*bgp
)
6222 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
6224 hash_create(esi_hash_keymake
, esi_cmp
,
6225 "BGP EVPN Local ESI Hash");
6226 bgp
->import_rt_hash
=
6227 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
6228 "BGP Import RT Hash");
6229 bgp
->vrf_import_rt_hash
=
6230 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
6231 "BGP VRF Import RT Hash");
6232 bgp
->vrf_import_rtl
= list_new();
6233 bgp
->vrf_import_rtl
->cmp
=
6234 (int (*)(void *, void *))evpn_route_target_cmp
;
6235 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
6236 bgp
->vrf_export_rtl
= list_new();
6237 bgp
->vrf_export_rtl
->cmp
=
6238 (int (*)(void *, void *))evpn_route_target_cmp
;
6239 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
6240 bgp
->l2vnis
= list_new();
6241 bgp
->l2vnis
->cmp
= vni_list_cmp
;
6242 /* By default Duplicate Address Dection is enabled.
6243 * Max-moves (N) 5, detection time (M) 180
6244 * default action is warning-only
6245 * freeze action permanently freezes address,
6246 * and freeze time (auto-recovery) is disabled.
6248 if (bgp
->evpn_info
) {
6249 bgp
->evpn_info
->dup_addr_detect
= true;
6250 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
6251 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
6252 bgp
->evpn_info
->dad_freeze
= false;
6253 bgp
->evpn_info
->dad_freeze_time
= 0;
6254 /* Initialize zebra vxlan */
6255 bgp_zebra_dup_addr_detection(bgp
);
6256 /* Enable PIP feature by default for bgp vrf instance */
6257 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
6258 struct bgp
*bgp_default
;
6260 bgp
->evpn_info
->advertise_pip
= true;
6261 bgp_default
= bgp_get_default();
6263 bgp
->evpn_info
->pip_ip
= bgp_default
->router_id
;
6267 /* Default BUM handling is to do head-end replication. */
6268 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
6271 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
6273 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
6277 * Get the prefixlen of the ip prefix carried within the type5 evpn route.
6279 int bgp_evpn_get_type5_prefixlen(const struct prefix
*pfx
)
6281 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6283 if (!pfx
|| pfx
->family
!= AF_EVPN
)
6286 if (evp
->prefix
.route_type
!= BGP_EVPN_IP_PREFIX_ROUTE
)
6289 return evp
->prefix
.prefix_addr
.ip_prefix_length
;
6293 * Should we register nexthop for this EVPN prefix for nexthop tracking?
6295 bool bgp_evpn_is_prefix_nht_supported(const struct prefix
*pfx
)
6297 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6300 * EVPN RT-5 should not be marked as valid and imported to vrfs if the
6301 * BGP nexthop is not reachable. To check for the nexthop reachability,
6302 * Add nexthop for EVPN RT-5 for nexthop tracking.
6304 * Ideally, a BGP route should be marked as valid only if the
6305 * nexthop is reachable. Thus, other EVPN route types also should be
6306 * added here after testing is performed for them.
6308 if (pfx
&& pfx
->family
== AF_EVPN
&&
6309 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
)