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"
52 * Definitions and external declarations.
54 extern struct zclient
*zclient
;
56 DEFINE_QOBJ_TYPE(bgpevpn
)
57 DEFINE_QOBJ_TYPE(evpnes
)
61 * Static function declarations
63 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
65 struct bgp_path_info
**pi
);
66 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
72 /* compare two IPV4 VTEP IPs */
73 static int evpn_vtep_ip_cmp(void *p1
, void *p2
)
75 const struct in_addr
*ip1
= p1
;
76 const struct in_addr
*ip2
= p2
;
78 return ip1
->s_addr
- ip2
->s_addr
;
82 * Make hash key for ESI.
84 static unsigned int esi_hash_keymake(void *p
)
86 struct evpnes
*pes
= p
;
87 const void *pnt
= (void *)pes
->esi
.val
;
89 return jhash(pnt
, ESI_BYTES
, 0xa5a5a55a);
95 static bool esi_cmp(const void *p1
, const void *p2
)
97 const struct evpnes
*pes1
= p1
;
98 const struct evpnes
*pes2
= p2
;
100 if (pes1
== NULL
&& pes2
== NULL
)
103 if (pes1
== NULL
|| pes2
== NULL
)
106 return (memcmp(pes1
->esi
.val
, pes2
->esi
.val
, ESI_BYTES
) == 0);
112 static unsigned int vni_hash_key_make(void *p
)
114 struct bgpevpn
*vpn
= p
;
115 return (jhash_1word(vpn
->vni
, 0));
119 * Comparison function for vni hash
121 static bool vni_hash_cmp(const void *p1
, const void *p2
)
123 const struct bgpevpn
*vpn1
= p1
;
124 const struct bgpevpn
*vpn2
= p2
;
130 return (vpn1
->vni
== vpn2
->vni
);
133 static int vni_list_cmp(void *p1
, void *p2
)
135 const struct bgpevpn
*vpn1
= p1
;
136 const struct bgpevpn
*vpn2
= p2
;
138 return vpn1
->vni
- vpn2
->vni
;
142 * Make vrf import route target hash key.
144 static unsigned int vrf_import_rt_hash_key_make(void *p
)
146 struct vrf_irt_node
*irt
= p
;
147 char *pnt
= irt
->rt
.val
;
149 return jhash(pnt
, 8, 0x5abc1234);
153 * Comparison function for vrf import rt hash
155 static bool vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
157 const struct vrf_irt_node
*irt1
= p1
;
158 const struct vrf_irt_node
*irt2
= p2
;
160 if (irt1
== NULL
&& irt2
== NULL
)
163 if (irt1
== NULL
|| irt2
== NULL
)
166 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
170 * Create a new vrf import_rt in default instance
172 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
174 struct bgp
*bgp_def
= NULL
;
175 struct vrf_irt_node
*irt
;
177 bgp_def
= bgp_get_default();
179 flog_err(EC_BGP_NO_DFLT
,
180 "vrf import rt new - def instance not created yet");
184 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
185 sizeof(struct vrf_irt_node
));
190 irt
->vrfs
= list_new();
193 if (!hash_get(bgp_def
->vrf_import_rt_hash
, irt
, hash_alloc_intern
)) {
194 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
202 * Free the vrf import rt node
204 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
206 struct bgp
*bgp_def
= NULL
;
208 bgp_def
= bgp_get_default();
210 flog_err(EC_BGP_NO_DFLT
,
211 "vrf import rt free - def instance not created yet");
215 hash_release(bgp_def
->vrf_import_rt_hash
, irt
);
216 list_delete(&irt
->vrfs
);
217 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
221 * Function to lookup Import RT node - used to map a RT to set of
222 * VNIs importing routes with that RT.
224 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
226 struct bgp
*bgp_def
= NULL
;
227 struct vrf_irt_node
*irt
;
228 struct vrf_irt_node tmp
;
230 bgp_def
= bgp_get_default();
232 flog_err(EC_BGP_NO_DFLT
,
233 "vrf import rt lookup - def instance not created yet");
237 memset(&tmp
, 0, sizeof(struct vrf_irt_node
));
238 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
239 irt
= hash_lookup(bgp_def
->vrf_import_rt_hash
, &tmp
);
244 * Is specified VRF present on the RT's list of "importing" VRFs?
246 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
, struct bgp
*bgp_vrf
)
248 struct listnode
*node
= NULL
, *nnode
= NULL
;
249 struct bgp
*tmp_bgp_vrf
= NULL
;
251 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
252 if (tmp_bgp_vrf
== bgp_vrf
)
259 * Make import route target hash key.
261 static unsigned int import_rt_hash_key_make(void *p
)
263 struct irt_node
*irt
= p
;
264 char *pnt
= irt
->rt
.val
;
266 return jhash(pnt
, 8, 0xdeadbeef);
270 * Comparison function for import rt hash
272 static bool import_rt_hash_cmp(const void *p1
, const void *p2
)
274 const struct irt_node
*irt1
= p1
;
275 const struct irt_node
*irt2
= p2
;
277 if (irt1
== NULL
&& irt2
== NULL
)
280 if (irt1
== NULL
|| irt2
== NULL
)
283 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
287 * Create a new import_rt
289 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
290 struct ecommunity_val
*rt
)
292 struct irt_node
*irt
;
297 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
);
499 * Create RT extended community automatically from passed information:
500 * of the form AS:VNI.
501 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
502 * the need is to get a RT value that will be unique across different
503 * VNIs but the same across routers (in the same AS) for a particular
506 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
508 struct ecommunity_val eval
;
509 struct ecommunity
*ecomadd
;
511 if (bgp
->advertise_autort_rfc8365
)
512 vni
|= EVPN_AUTORT_VXLAN
;
513 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
515 ecomadd
= ecommunity_new();
516 ecommunity_add_val(ecomadd
, &eval
);
517 listnode_add_sort(rtl
, ecomadd
);
521 * Derive RD and RT for a VNI automatically. Invoked at the time of
524 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
526 bgp_evpn_derive_auto_rd(bgp
, vpn
);
527 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
528 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
532 * Convert nexthop (remote VTEP IP) into an IPv6 address.
534 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
536 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
538 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
539 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
543 * Add (update) or delete MACIP from zebra.
545 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
546 struct prefix_evpn
*p
,
547 struct in_addr remote_vtep_ip
, int add
,
548 uint8_t flags
, uint32_t seq
)
552 char buf1
[ETHER_ADDR_STRLEN
];
553 char buf2
[INET6_ADDRSTRLEN
];
554 char buf3
[INET6_ADDRSTRLEN
];
557 if (!zclient
|| zclient
->sock
< 0)
560 /* Don't try to register if Zebra doesn't know of this instance. */
561 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
567 zclient_create_header(
568 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
570 stream_putl(s
, vpn
->vni
);
571 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
572 /* IP address length and IP address, if any. */
573 if (is_evpn_prefix_ipaddr_none(p
))
576 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
578 stream_putl(s
, ipa_len
);
579 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
581 stream_put_in_addr(s
, &remote_vtep_ip
);
583 /* TX flags - MAC sticky status and/or gateway mac */
584 /* Also TX the sequence number of the best route. */
586 stream_putc(s
, flags
);
590 stream_putw_at(s
, 0, stream_get_endp(s
));
592 if (bgp_debug_zebra(NULL
))
594 "Tx %s MACIP, VNI %u MAC %s IP %s flags 0x%x seq %u remote VTEP %s",
595 add
? "ADD" : "DEL", vpn
->vni
,
596 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
598 ipaddr2str(&p
->prefix
.macip_addr
.ip
,
599 buf3
, sizeof(buf3
)), flags
, seq
,
600 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
603 return zclient_send_message(zclient
);
607 * Add (update) or delete remote VTEP from zebra.
609 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
610 struct prefix_evpn
*p
, int add
)
615 if (!zclient
|| zclient
->sock
< 0)
618 /* Don't try to register if Zebra doesn't know of this instance. */
619 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
625 zclient_create_header(
626 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
628 stream_putl(s
, vpn
->vni
);
629 if (is_evpn_prefix_ipaddr_v4(p
))
630 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
631 else if (is_evpn_prefix_ipaddr_v6(p
)) {
634 "Bad remote IP when trying to %s remote VTEP for VNI %u",
635 add
? "ADD" : "DEL", vpn
->vni
);
639 stream_putw_at(s
, 0, stream_get_endp(s
));
641 if (bgp_debug_zebra(NULL
))
642 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
643 add
? "ADD" : "DEL", vpn
->vni
,
644 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
646 return zclient_send_message(zclient
);
650 * Build extended community for EVPN ES (type-4) route
652 static void build_evpn_type4_route_extcomm(struct evpnes
*es
,
655 struct ecommunity ecom_encap
;
656 struct ecommunity ecom_es_rt
;
657 struct ecommunity_val eval
;
658 struct ecommunity_val eval_es_rt
;
659 bgp_encap_types tnl_type
;
663 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
664 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
665 encode_encap_extcomm(tnl_type
, &eval
);
667 ecom_encap
.val
= (uint8_t *)eval
.val
;
668 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
671 memset(&mac
, 0, sizeof(struct ethaddr
));
672 memset(&ecom_es_rt
, 0, sizeof(ecom_es_rt
));
673 es_get_system_mac(&es
->esi
, &mac
);
674 encode_es_rt_extcomm(&eval_es_rt
, &mac
);
676 ecom_es_rt
.val
= (uint8_t *)eval_es_rt
.val
;
678 ecommunity_merge(attr
->ecommunity
, &ecom_es_rt
);
680 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
684 * Build extended communities for EVPN prefix route.
686 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
689 struct ecommunity ecom_encap
;
690 struct ecommunity ecom_rmac
;
691 struct ecommunity_val eval
;
692 struct ecommunity_val eval_rmac
;
693 bgp_encap_types tnl_type
;
694 struct listnode
*node
, *nnode
;
695 struct ecommunity
*ecom
;
696 struct list
*vrf_export_rtl
= NULL
;
699 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
700 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
701 encode_encap_extcomm(tnl_type
, &eval
);
703 ecom_encap
.val
= (uint8_t *)eval
.val
;
706 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
708 /* Add the export RTs for L3VNI/VRF */
709 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
710 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
712 ecommunity_merge(attr
->ecommunity
, ecom
);
714 /* add the router mac extended community */
715 if (!is_zero_mac(&attr
->rmac
)) {
716 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
717 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
719 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
721 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
724 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
728 * Build extended communities for EVPN route.
729 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
730 * and ENCAP extended communities are applicable for all routes.
731 * The default gateway extended community and MAC mobility (sticky) extended
732 * community are added as needed based on passed settings - only for type-2
733 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
734 * added, if present, based on passed settings - only for non-link-local
737 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
740 struct ecommunity ecom_encap
;
741 struct ecommunity ecom_sticky
;
742 struct ecommunity ecom_default_gw
;
743 struct ecommunity ecom_rmac
;
744 struct ecommunity ecom_na
;
745 struct ecommunity_val eval
;
746 struct ecommunity_val eval_sticky
;
747 struct ecommunity_val eval_default_gw
;
748 struct ecommunity_val eval_rmac
;
749 struct ecommunity_val eval_na
;
751 bgp_encap_types tnl_type
;
752 struct listnode
*node
, *nnode
;
753 struct ecommunity
*ecom
;
755 struct list
*vrf_export_rtl
= NULL
;
758 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
759 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
760 encode_encap_extcomm(tnl_type
, &eval
);
762 ecom_encap
.val
= (uint8_t *)eval
.val
;
765 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
767 /* Add the export RTs for L2VNI */
768 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
769 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
771 /* Add the export RTs for L3VNI if told to - caller determines
772 * when this should be done.
775 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
776 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
777 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
779 attr
->ecommunity
= ecommunity_merge(
780 attr
->ecommunity
, ecom
);
784 /* Add MAC mobility (sticky) if needed. */
787 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
788 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
789 ecom_sticky
.size
= 1;
790 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
792 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
795 /* Add RMAC, if told to. */
797 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
798 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
800 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
802 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
805 /* Add default gateway, if needed. */
806 if (attr
->default_gw
) {
807 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
808 encode_default_gw_extcomm(&eval_default_gw
);
809 ecom_default_gw
.size
= 1;
810 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
812 ecommunity_merge(attr
->ecommunity
, &ecom_default_gw
);
815 if (attr
->router_flag
) {
816 memset(&ecom_na
, 0, sizeof(ecom_na
));
817 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
);
819 ecom_na
.val
= (uint8_t *)eval_na
.val
;
820 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
824 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
828 * Add MAC mobility extended community to attribute.
830 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
832 struct ecommunity ecom_tmp
;
833 struct ecommunity_val eval
;
834 uint8_t *ecom_val_ptr
;
841 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
843 /* Find current MM ecommunity */
846 if (attr
->ecommunity
) {
847 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
848 pnt
= attr
->ecommunity
->val
+ (i
* 8);
852 if (type
== ECOMMUNITY_ENCODE_EVPN
854 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
855 ecom_val_ptr
= (uint8_t *)(attr
->ecommunity
->val
862 /* Update the existing MM ecommunity */
864 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
866 /* Add MM to existing */
868 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
870 ecom_tmp
.val
= (uint8_t *)eval
.val
;
872 if (attr
->ecommunity
)
874 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
876 attr
->ecommunity
= ecommunity_dup(&ecom_tmp
);
880 /* Install EVPN route into zebra. */
881 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
882 struct prefix_evpn
*p
,
883 struct in_addr remote_vtep_ip
, uint8_t flags
,
888 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
889 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
892 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 1);
897 /* Uninstall EVPN route from zebra. */
898 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
899 struct prefix_evpn
*p
,
900 struct in_addr remote_vtep_ip
)
904 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
905 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
908 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 0);
914 * Due to MAC mobility, the prior "local" best route has been supplanted
915 * by a "remote" best route. The prior route has to be deleted and withdrawn
918 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
920 struct bgp_path_info
*old_local
)
922 struct bgp_node
*global_rn
;
923 struct bgp_path_info
*pi
;
924 afi_t afi
= AFI_L2VPN
;
925 safi_t safi
= SAFI_EVPN
;
927 /* Locate route node in the global EVPN routing table. Note that
928 * this table is a 2-level tree (RD-level + Prefix-level) similar to
931 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
932 (struct prefix
*)&rn
->p
, &vpn
->prd
);
934 /* Delete route entry in the global EVPN table. */
935 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
937 /* Schedule for processing - withdraws to peers happen from
941 bgp_process(bgp
, global_rn
, afi
, safi
);
942 bgp_unlock_node(global_rn
);
945 /* Delete route entry in the VNI route table, caller to remove. */
946 bgp_path_info_delete(rn
, old_local
);
949 static struct in_addr
*es_vtep_new(struct in_addr vtep
)
953 ip
= XCALLOC(MTYPE_BGP_EVPN_ES_VTEP
, sizeof(struct in_addr
));
957 ip
->s_addr
= vtep
.s_addr
;
961 static void es_vtep_free(struct in_addr
*ip
)
963 XFREE(MTYPE_BGP_EVPN_ES_VTEP
, ip
);
966 /* check if VTEP is already part of the list */
967 static int is_vtep_present_in_list(struct list
*list
,
970 struct listnode
*node
= NULL
;
973 for (ALL_LIST_ELEMENTS_RO(list
, node
, tmp
)) {
974 if (tmp
->s_addr
== vtep
.s_addr
)
981 * Best path for ES route was changed,
982 * update the list of VTEPs for this ES
984 static int evpn_es_install_vtep(struct bgp
*bgp
,
986 struct prefix_evpn
*p
,
987 struct in_addr rvtep
)
989 struct in_addr
*vtep_ip
;
991 if (is_vtep_present_in_list(es
->vtep_list
, rvtep
))
995 vtep_ip
= es_vtep_new(rvtep
);
997 listnode_add_sort(es
->vtep_list
, vtep_ip
);
1002 * Best path for ES route was changed,
1003 * update the list of VTEPs for this ES
1005 static int evpn_es_uninstall_vtep(struct bgp
*bgp
,
1007 struct prefix_evpn
*p
,
1008 struct in_addr rvtep
)
1010 struct listnode
*node
, *nnode
, *node_to_del
= NULL
;
1011 struct in_addr
*tmp
;
1013 for (ALL_LIST_ELEMENTS(es
->vtep_list
, node
, nnode
, tmp
)) {
1014 if (tmp
->s_addr
== rvtep
.s_addr
) {
1021 list_delete_node(es
->vtep_list
, node_to_del
);
1027 * Calculate the best path for a ES(type-4) route.
1029 static int evpn_es_route_select_install(struct bgp
*bgp
,
1031 struct bgp_node
*rn
)
1034 afi_t afi
= AFI_L2VPN
;
1035 safi_t safi
= SAFI_EVPN
;
1036 struct bgp_path_info
*old_select
; /* old best */
1037 struct bgp_path_info
*new_select
; /* new best */
1038 struct bgp_path_info_pair old_and_new
;
1040 /* Compute the best path. */
1041 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
],
1042 &old_and_new
, afi
, safi
);
1043 old_select
= old_and_new
.old
;
1044 new_select
= old_and_new
.new;
1047 * If the best path hasn't changed - see if something needs to be
1050 if (old_select
&& old_select
== new_select
1051 && old_select
->type
== ZEBRA_ROUTE_BGP
1052 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1053 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1054 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1055 && !bgp
->addpath_tx_used
[afi
][safi
]) {
1056 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1057 ret
= evpn_es_install_vtep(bgp
, es
,
1058 (struct prefix_evpn
*)&rn
->p
,
1059 old_select
->attr
->nexthop
);
1061 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1062 bgp_zebra_clear_route_change_flags(rn
);
1066 /* If the user did a "clear" this flag will be set */
1067 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1070 * bestpath has changed; update relevant fields and install or uninstall
1071 * into the zebra RIB.
1073 if (old_select
|| new_select
)
1074 bgp_bump_version(rn
);
1077 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1079 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1080 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1081 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1084 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1085 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1086 ret
= evpn_es_install_vtep(bgp
, es
,
1087 (struct prefix_evpn
*)&rn
->p
,
1088 new_select
->attr
->nexthop
);
1090 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1091 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1092 ret
= evpn_es_uninstall_vtep(
1093 bgp
, es
, (struct prefix_evpn
*)&rn
->p
,
1094 old_select
->attr
->nexthop
);
1097 /* Clear any route change flags. */
1098 bgp_zebra_clear_route_change_flags(rn
);
1100 /* Reap old select bgp_path_info, if it has been removed */
1101 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1102 bgp_path_info_reap(rn
, old_select
);
1108 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1111 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1112 struct bgp_node
*rn
)
1114 struct bgp_path_info
*old_select
, *new_select
;
1115 struct bgp_path_info_pair old_and_new
;
1116 struct prefix_evpn
*evp
;
1117 afi_t afi
= AFI_L2VPN
;
1118 safi_t safi
= SAFI_EVPN
;
1122 /* Compute the best path. */
1123 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1125 old_select
= old_and_new
.old
;
1126 new_select
= old_and_new
.new;
1128 evp
= (struct prefix_evpn
*)&rn
->p
;
1129 /* If the best path hasn't changed - see if there is still something to
1133 if (old_select
&& old_select
== new_select
1134 && old_select
->type
== ZEBRA_ROUTE_BGP
1135 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1136 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1137 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1138 && !bgp
->addpath_tx_used
[afi
][safi
]) {
1139 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1140 if (old_select
->attr
->sticky
)
1141 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
1142 if (old_select
->attr
->default_gw
)
1143 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
1144 if (is_evpn_prefix_ipaddr_v6(evp
) &&
1145 old_select
->attr
->router_flag
)
1146 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
1148 ret
= evpn_zebra_install(
1149 bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1150 old_select
->attr
->nexthop
, flags
,
1151 mac_mobility_seqnum(old_select
->attr
));
1153 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1154 bgp_zebra_clear_route_change_flags(rn
);
1158 /* If the user did a "clear" this flag will be set */
1159 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1161 /* bestpath has changed; update relevant fields and install or uninstall
1162 * into the zebra RIB.
1164 if (old_select
|| new_select
)
1165 bgp_bump_version(rn
);
1168 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1170 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1171 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1172 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1175 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1176 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1178 if (new_select
->attr
->sticky
)
1179 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
1180 if (new_select
->attr
->default_gw
)
1181 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
1182 if (is_evpn_prefix_ipaddr_v6(evp
) &&
1183 new_select
->attr
->router_flag
)
1184 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
1186 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1187 new_select
->attr
->nexthop
, flags
,
1188 mac_mobility_seqnum(new_select
->attr
));
1189 /* If an old best existed and it was a "local" route, the only
1191 * it would be supplanted is due to MAC mobility procedures. So,
1193 * need to do an implicit delete and withdraw that route from
1196 if (old_select
&& old_select
->peer
== bgp
->peer_self
1197 && old_select
->type
== ZEBRA_ROUTE_BGP
1198 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
1199 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
1201 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1202 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1203 ret
= evpn_zebra_uninstall(bgp
, vpn
,
1204 (struct prefix_evpn
*)&rn
->p
,
1205 old_select
->attr
->nexthop
);
1208 /* Clear any route change flags. */
1209 bgp_zebra_clear_route_change_flags(rn
);
1211 /* Reap old select bgp_path_info, if it has been removed */
1212 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1213 bgp_path_info_reap(rn
, old_select
);
1219 * Return true if the local ri for this rn is of type gateway mac
1221 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
1223 struct bgp_path_info
*tmp_pi
= NULL
;
1224 struct bgp_path_info
*local_pi
= NULL
;
1227 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
) {
1228 if (tmp_pi
->peer
== bgp
->peer_self
1229 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1230 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1237 return local_pi
->attr
->default_gw
;
1242 * Return true if the local ri for this rn has sticky set
1244 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
1246 struct bgp_path_info
*tmp_pi
;
1247 struct bgp_path_info
*local_pi
;
1250 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
) {
1251 if (tmp_pi
->peer
== bgp
->peer_self
1252 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1253 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1260 return local_pi
->attr
->sticky
;
1264 * create or update EVPN type4 route entry.
1265 * This could be in the ES table or the global table.
1266 * TODO: handle remote ES (type4) routes as well
1268 static int update_evpn_type4_route_entry(struct bgp
*bgp
, struct evpnes
*es
,
1269 afi_t afi
, safi_t safi
,
1270 struct bgp_node
*rn
, struct attr
*attr
,
1271 int add
, struct bgp_path_info
**ri
,
1274 char buf
[ESI_STR_LEN
];
1275 char buf1
[INET6_ADDRSTRLEN
];
1276 struct bgp_path_info
*tmp_pi
= NULL
;
1277 struct bgp_path_info
*local_pi
= NULL
; /* local route entry if any */
1278 struct bgp_path_info
*remote_pi
= NULL
; /* remote route entry if any */
1279 struct attr
*attr_new
= NULL
;
1280 struct prefix_evpn
*evp
= NULL
;
1284 evp
= (struct prefix_evpn
*)&rn
->p
;
1286 /* locate the local and remote entries if any */
1287 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
) {
1288 if (tmp_pi
->peer
== bgp
->peer_self
1289 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1290 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1292 if (tmp_pi
->type
== ZEBRA_ROUTE_BGP
1293 && tmp_pi
->sub_type
== BGP_ROUTE_IMPORTED
1294 && CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1298 /* we don't expect to see a remote_pi at this point.
1299 * An ES route has esi + vtep_ip as the key,
1300 * We shouldn't see the same route from any other vtep.
1305 "%u ERROR: local es route for ESI: %s Vtep %s also learnt from remote",
1307 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1308 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1312 if (!local_pi
&& !add
)
1315 /* create or update the entry */
1318 /* Add or update attribute to hash */
1319 attr_new
= bgp_attr_intern(attr
);
1321 /* Create new route with its attribute. */
1322 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1323 bgp
->peer_self
, attr_new
, rn
);
1324 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1326 /* add the newly created path to the route-node */
1327 bgp_path_info_add(rn
, tmp_pi
);
1330 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1331 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1334 /* The attribute has changed.
1335 * Add (or update) attribute to hash. */
1336 attr_new
= bgp_attr_intern(attr
);
1337 bgp_path_info_set_flag(rn
, tmp_pi
,
1338 BGP_PATH_ATTR_CHANGED
);
1340 /* Restore route, if needed. */
1341 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1342 bgp_path_info_restore(rn
, tmp_pi
);
1344 /* Unintern existing, set to new. */
1345 bgp_attr_unintern(&tmp_pi
->attr
);
1346 tmp_pi
->attr
= attr_new
;
1347 tmp_pi
->uptime
= bgp_clock();
1351 /* Return back the route entry. */
1356 /* update evpn es (type-4) route */
1357 static int update_evpn_type4_route(struct bgp
*bgp
,
1359 struct prefix_evpn
*p
)
1362 int route_changed
= 0;
1363 char buf
[ESI_STR_LEN
];
1364 char buf1
[INET6_ADDRSTRLEN
];
1365 afi_t afi
= AFI_L2VPN
;
1366 safi_t safi
= SAFI_EVPN
;
1368 struct attr
*attr_new
= NULL
;
1369 struct bgp_node
*rn
= NULL
;
1370 struct bgp_path_info
*pi
= NULL
;
1372 memset(&attr
, 0, sizeof(struct attr
));
1374 /* Build path-attribute for this route. */
1375 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1376 attr
.nexthop
= es
->originator_ip
.ipaddr_v4
;
1377 attr
.mp_nexthop_global_in
= es
->originator_ip
.ipaddr_v4
;
1378 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1380 /* Set up extended community. */
1381 build_evpn_type4_route_extcomm(es
, &attr
);
1383 /* First, create (or fetch) route node within the ESI. */
1384 /* NOTE: There is no RD here. */
1385 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
1387 /* Create or update route entry. */
1388 ret
= update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, &attr
, 1,
1389 &pi
, &route_changed
);
1391 flog_err(EC_BGP_ES_INVALID
,
1392 "%u ERROR: Failed to updated ES route ESI: %s VTEP %s",
1394 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1395 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1399 attr_new
= pi
->attr
;
1401 /* Perform route selection;
1402 * this is just to set the flags correctly
1403 * as local route in the ES always wins.
1405 evpn_es_route_select_install(bgp
, es
, rn
);
1406 bgp_unlock_node(rn
);
1408 /* If this is a new route or some attribute has changed, export the
1409 * route to the global table. The route will be advertised to peers
1410 * from there. Note that this table is a 2-level tree (RD-level +
1411 * Prefix-level) similar to L3VPN routes.
1413 if (route_changed
) {
1414 struct bgp_path_info
*global_pi
;
1416 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1417 (struct prefix
*)p
, &es
->prd
);
1418 update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, attr_new
,
1419 1, &global_pi
, &route_changed
);
1421 /* Schedule for processing and unlock node. */
1422 bgp_process(bgp
, rn
, afi
, safi
);
1423 bgp_unlock_node(rn
);
1426 /* Unintern temporary. */
1427 aspath_unintern(&attr
.aspath
);
1431 static int update_evpn_type5_route_entry(struct bgp
*bgp_def
,
1432 struct bgp
*bgp_vrf
, afi_t afi
,
1433 safi_t safi
, struct bgp_node
*rn
,
1434 struct attr
*attr
, int *route_changed
)
1436 struct attr
*attr_new
= NULL
;
1437 struct bgp_path_info
*pi
= NULL
;
1438 mpls_label_t label
= MPLS_INVALID_LABEL
;
1439 struct bgp_path_info
*local_pi
= NULL
;
1440 struct bgp_path_info
*tmp_pi
= NULL
;
1443 /* locate the local route entry if any */
1444 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
) {
1445 if (tmp_pi
->peer
== bgp_def
->peer_self
1446 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1447 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1452 * create a new route entry if one doesn't exist.
1453 * Otherwise see if route attr has changed
1457 /* route has changed as this is the first entry */
1460 /* Add (or update) attribute to hash. */
1461 attr_new
= bgp_attr_intern(attr
);
1463 /* create the route info from attribute */
1464 pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1465 bgp_def
->peer_self
, attr_new
, rn
);
1466 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
1468 /* Type-5 routes advertise the L3-VNI */
1469 bgp_path_info_extra_get(pi
);
1470 vni2label(bgp_vrf
->l3vni
, &label
);
1471 memcpy(&pi
->extra
->label
, &label
, sizeof(label
));
1472 pi
->extra
->num_labels
= 1;
1474 /* add the route entry to route node*/
1475 bgp_path_info_add(rn
, pi
);
1479 if (!attrhash_cmp(tmp_pi
->attr
, attr
)) {
1481 /* attribute changed */
1484 /* The attribute has changed. */
1485 /* Add (or update) attribute to hash. */
1486 attr_new
= bgp_attr_intern(attr
);
1487 bgp_path_info_set_flag(rn
, tmp_pi
,
1488 BGP_PATH_ATTR_CHANGED
);
1490 /* Restore route, if needed. */
1491 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1492 bgp_path_info_restore(rn
, tmp_pi
);
1494 /* Unintern existing, set to new. */
1495 bgp_attr_unintern(&tmp_pi
->attr
);
1496 tmp_pi
->attr
= attr_new
;
1497 tmp_pi
->uptime
= bgp_clock();
1503 /* update evpn type-5 route entry */
1504 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1505 struct attr
*src_attr
)
1507 afi_t afi
= AFI_L2VPN
;
1508 safi_t safi
= SAFI_EVPN
;
1510 struct bgp_node
*rn
= NULL
;
1511 struct bgp
*bgp_def
= NULL
;
1512 int route_changed
= 0;
1514 bgp_def
= bgp_get_default();
1518 /* Build path attribute for this route - use the source attr, if
1519 * present, else treat as locally originated.
1522 bgp_attr_dup(&attr
, src_attr
);
1524 memset(&attr
, 0, sizeof(struct attr
));
1525 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1527 /* Set nexthop to ourselves and fill in the Router MAC. */
1528 attr
.nexthop
= bgp_vrf
->originator_ip
;
1529 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1530 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1531 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
1533 /* Setup RT and encap extended community */
1534 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1536 /* get the route node in global table */
1537 rn
= bgp_afi_node_get(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1538 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1541 /* create or update the route entry within the route node */
1542 update_evpn_type5_route_entry(bgp_def
, bgp_vrf
, afi
, safi
, rn
, &attr
,
1545 /* schedule for processing and unlock node */
1546 if (route_changed
) {
1547 bgp_process(bgp_def
, rn
, afi
, safi
);
1548 bgp_unlock_node(rn
);
1551 /* uninten temporary */
1553 aspath_unintern(&attr
.aspath
);
1558 * Create or update EVPN route entry. This could be in the VNI route table
1559 * or the global route table.
1561 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1562 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1563 struct attr
*attr
, int add
,
1564 struct bgp_path_info
**pi
, uint8_t flags
,
1567 struct bgp_path_info
*tmp_pi
;
1568 struct bgp_path_info
*local_pi
;
1569 struct attr
*attr_new
;
1570 mpls_label_t label
[BGP_MAX_LABELS
];
1571 uint32_t num_labels
= 1;
1572 int route_change
= 1;
1574 struct prefix_evpn
*evp
;
1577 evp
= (struct prefix_evpn
*)&rn
->p
;
1578 memset(&label
, 0, sizeof(label
));
1580 /* See if this is an update of an existing route, or a new add. */
1582 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
) {
1583 if (tmp_pi
->peer
== bgp
->peer_self
1584 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1585 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1589 /* If route doesn't exist already, create a new one, if told to.
1590 * Otherwise act based on whether the attributes of the route have
1593 if (!local_pi
&& !add
)
1596 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1597 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1598 add_mac_mobility_to_attr(seq
, attr
);
1601 /* Add (or update) attribute to hash. */
1602 attr_new
= bgp_attr_intern(attr
);
1604 /* Extract MAC mobility sequence number, if any. */
1605 attr_new
->mm_seqnum
=
1606 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1607 attr_new
->sticky
= sticky
;
1609 /* Create new route with its attribute. */
1610 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1611 bgp
->peer_self
, attr_new
, rn
);
1612 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1613 bgp_path_info_extra_get(tmp_pi
);
1615 /* The VNI goes into the 'label' field of the route */
1616 vni2label(vpn
->vni
, &label
[0]);
1618 /* Type-2 routes may carry a second VNI - the L3-VNI.
1619 * Only attach second label if we are advertising two labels for
1622 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1623 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1626 l3vni
= bgpevpn_get_l3vni(vpn
);
1628 vni2label(l3vni
, &label
[1]);
1633 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1634 tmp_pi
->extra
->num_labels
= num_labels
;
1635 bgp_path_info_add(rn
, tmp_pi
);
1638 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1639 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1643 * The attributes have changed, type-2 routes needs to
1644 * be advertised with right labels.
1646 vni2label(vpn
->vni
, &label
[0]);
1647 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1648 && CHECK_FLAG(vpn
->flags
,
1649 VNI_FLAG_USE_TWO_LABELS
)) {
1652 l3vni
= bgpevpn_get_l3vni(vpn
);
1654 vni2label(l3vni
, &label
[1]);
1658 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1659 tmp_pi
->extra
->num_labels
= num_labels
;
1661 /* The attribute has changed. */
1662 /* Add (or update) attribute to hash. */
1663 attr_new
= bgp_attr_intern(attr
);
1664 bgp_path_info_set_flag(rn
, tmp_pi
,
1665 BGP_PATH_ATTR_CHANGED
);
1667 /* Extract MAC mobility sequence number, if any. */
1668 attr_new
->mm_seqnum
=
1669 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1670 attr_new
->sticky
= sticky
;
1672 /* Restore route, if needed. */
1673 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1674 bgp_path_info_restore(rn
, tmp_pi
);
1676 /* Unintern existing, set to new. */
1677 bgp_attr_unintern(&tmp_pi
->attr
);
1678 tmp_pi
->attr
= attr_new
;
1679 tmp_pi
->uptime
= bgp_clock();
1683 /* Return back the route entry. */
1685 return route_change
;
1689 * Create or update EVPN route (of type based on prefix) for specified VNI
1690 * and schedule for processing.
1692 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1693 struct prefix_evpn
*p
, uint8_t flags
,
1696 struct bgp_node
*rn
;
1698 struct attr
*attr_new
;
1699 int add_l3_ecomm
= 0;
1700 struct bgp_path_info
*pi
;
1701 afi_t afi
= AFI_L2VPN
;
1702 safi_t safi
= SAFI_EVPN
;
1705 memset(&attr
, 0, sizeof(struct attr
));
1707 /* Build path-attribute for this route. */
1708 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1709 attr
.nexthop
= vpn
->originator_ip
;
1710 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1711 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1712 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1713 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1714 attr
.router_flag
= CHECK_FLAG(flags
,
1715 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1717 /* PMSI is only needed for type-3 routes */
1718 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
)
1719 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1721 /* router mac is only needed for type-2 routes here. */
1722 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1723 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1724 vni2label(vpn
->vni
, &(attr
.label
));
1726 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1727 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1730 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1731 (is_evpn_prefix_ipaddr_v4(p
) ||
1732 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1733 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1734 bgpevpn_get_l3vni(vpn
))
1737 /* Set up extended community. */
1738 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1740 /* First, create (or fetch) route node within the VNI. */
1741 /* NOTE: There is no RD here. */
1742 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1744 /* Create or update route entry. */
1745 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1746 1, &pi
, flags
, seq
);
1748 attr_new
= pi
->attr
;
1750 /* Perform route selection; this is just to set the flags correctly
1751 * as local route in the VNI always wins.
1753 evpn_route_select_install(bgp
, vpn
, rn
);
1754 bgp_unlock_node(rn
);
1756 /* If this is a new route or some attribute has changed, export the
1757 * route to the global table. The route will be advertised to peers
1758 * from there. Note that this table is a 2-level tree (RD-level +
1759 * Prefix-level) similar to L3VPN routes.
1762 struct bgp_path_info
*global_pi
;
1764 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1765 (struct prefix
*)p
, &vpn
->prd
);
1766 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1,
1767 &global_pi
, flags
, seq
);
1769 /* Schedule for processing and unlock node. */
1770 bgp_process(bgp
, rn
, afi
, safi
);
1771 bgp_unlock_node(rn
);
1774 /* Unintern temporary. */
1775 aspath_unintern(&attr
.aspath
);
1781 * Delete EVPN route entry.
1782 * The entry can be in ESI/VNI table or the global table.
1784 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1785 struct bgp_node
*rn
,
1786 struct bgp_path_info
**pi
)
1788 struct bgp_path_info
*tmp_pi
;
1792 /* Now, find matching route. */
1793 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
)
1794 if (tmp_pi
->peer
== bgp
->peer_self
1795 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1796 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1801 /* Mark route for delete. */
1803 bgp_path_info_delete(rn
, tmp_pi
);
1808 /* Delete EVPN ES (type-4) route */
1809 static int delete_evpn_type4_route(struct bgp
*bgp
,
1811 struct prefix_evpn
*p
)
1813 afi_t afi
= AFI_L2VPN
;
1814 safi_t safi
= SAFI_EVPN
;
1815 struct bgp_path_info
*pi
;
1816 struct bgp_node
*rn
= NULL
; /* rn in esi table */
1817 struct bgp_node
*global_rn
= NULL
; /* rn in global table */
1819 /* First, locate the route node within the ESI.
1820 * If it doesn't exist, ther is nothing to do.
1821 * Note: there is no RD here.
1823 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
1827 /* Next, locate route node in the global EVPN routing table.
1828 * Note that this table is a 2-level tree (RD-level + Prefix-level)
1830 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1831 (struct prefix
*)p
, &es
->prd
);
1834 /* Delete route entry in the global EVPN table. */
1835 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1837 /* Schedule for processing - withdraws to peers happen from
1841 bgp_process(bgp
, global_rn
, afi
, safi
);
1842 bgp_unlock_node(global_rn
);
1846 * Delete route entry in the ESI route table.
1847 * This can just be removed.
1849 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
1851 bgp_path_info_reap(rn
, pi
);
1852 bgp_unlock_node(rn
);
1856 /* Delete EVPN type5 route */
1857 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1859 afi_t afi
= AFI_L2VPN
;
1860 safi_t safi
= SAFI_EVPN
;
1861 struct bgp_node
*rn
= NULL
;
1862 struct bgp_path_info
*pi
= NULL
;
1863 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
1865 bgp_def
= bgp_get_default();
1869 /* locate the global route entry for this type-5 prefix */
1870 rn
= bgp_afi_node_lookup(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1871 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1875 delete_evpn_route_entry(bgp_def
, afi
, safi
, rn
, &pi
);
1877 bgp_process(bgp_def
, rn
, afi
, safi
);
1878 bgp_unlock_node(rn
);
1883 * Delete EVPN route (of type based on prefix) for specified VNI and
1884 * schedule for processing.
1886 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1887 struct prefix_evpn
*p
)
1889 struct bgp_node
*rn
, *global_rn
;
1890 struct bgp_path_info
*pi
;
1891 afi_t afi
= AFI_L2VPN
;
1892 safi_t safi
= SAFI_EVPN
;
1894 /* First, locate the route node within the VNI. If it doesn't exist,
1896 * is nothing further to do.
1898 /* NOTE: There is no RD here. */
1899 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1903 /* Next, locate route node in the global EVPN routing table. Note that
1904 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1907 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1908 (struct prefix
*)p
, &vpn
->prd
);
1910 /* Delete route entry in the global EVPN table. */
1911 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1913 /* Schedule for processing - withdraws to peers happen from
1917 bgp_process(bgp
, global_rn
, afi
, safi
);
1918 bgp_unlock_node(global_rn
);
1921 /* Delete route entry in the VNI route table. This can just be removed.
1923 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
1925 bgp_path_info_reap(rn
, pi
);
1926 bgp_unlock_node(rn
);
1932 * Update all type-2 (MACIP) local routes for this VNI - these should also
1933 * be scheduled for advertise to peers.
1935 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1939 struct bgp_node
*rn
;
1940 struct bgp_path_info
*pi
, *tmp_pi
;
1942 struct attr
*attr_new
;
1944 int add_l3_ecomm
= 0;
1949 /* Walk this VNI's route table and update local type-2 routes. For any
1950 * routes updated, update corresponding entry in the global table too.
1952 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1953 rn
= bgp_route_next(rn
)) {
1954 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1955 struct bgp_node
*rd_rn
;
1956 struct bgp_path_info
*global_pi
;
1958 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1961 /* Identify local route. */
1962 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
) {
1963 if (tmp_pi
->peer
== bgp
->peer_self
1964 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1965 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1973 * Build attribute per local route as the MAC mobility and
1974 * some other values could differ for different routes. The
1975 * attributes will be shared in the hash table.
1977 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1978 attr
.nexthop
= vpn
->originator_ip
;
1979 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1980 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1981 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1983 if (evpn_route_is_sticky(bgp
, rn
))
1985 else if (evpn_route_is_def_gw(bgp
, rn
)) {
1986 attr
.default_gw
= 1;
1987 if (is_evpn_prefix_ipaddr_v6(evp
))
1988 attr
.router_flag
= 1;
1991 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
1992 * using L3 VNI for type-2 routes also.
1994 if ((is_evpn_prefix_ipaddr_v4(evp
) ||
1995 !IN6_IS_ADDR_LINKLOCAL(
1996 &evp
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1997 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1998 bgpevpn_get_l3vni(vpn
))
2001 /* Set up extended community. */
2002 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2004 seq
= mac_mobility_seqnum(tmp_pi
->attr
);
2006 /* Update the route entry. */
2007 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
, 0, &pi
,
2010 /* Perform route selection; this is just to set the flags
2011 * correctly as local route in the VNI always wins.
2013 evpn_route_select_install(bgp
, vpn
, rn
);
2015 attr_new
= pi
->attr
;
2017 /* Update route in global routing table. */
2018 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2019 (struct prefix
*)evp
, &vpn
->prd
);
2021 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
2023 mac_mobility_seqnum(attr_new
));
2025 /* Schedule for processing and unlock node. */
2026 bgp_process(bgp
, rd_rn
, afi
, safi
);
2027 bgp_unlock_node(rd_rn
);
2029 /* Unintern temporary. */
2030 aspath_unintern(&attr
.aspath
);
2038 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2039 * global routing table. These are also scheduled for withdraw from peers.
2041 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2045 struct bgp_node
*rdrn
, *rn
;
2046 struct bgp_table
*table
;
2047 struct bgp_path_info
*pi
;
2052 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
2053 if (rdrn
&& rdrn
->info
) {
2054 table
= (struct bgp_table
*)rdrn
->info
;
2055 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2056 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2058 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2061 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2063 bgp_process(bgp
, rn
, afi
, safi
);
2067 /* Unlock RD node. */
2069 bgp_unlock_node(rdrn
);
2075 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2076 * table as well as the per-VNI route table.
2078 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2082 struct bgp_node
*rn
;
2083 struct bgp_path_info
*pi
;
2088 /* First, walk the global route table for this VNI's type-2 local
2090 * EVPN routes are a 2-level table, first get the RD table.
2092 delete_global_type2_routes(bgp
, vpn
);
2094 /* Next, walk this VNI's route table and delete local type-2 routes. */
2095 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2096 rn
= bgp_route_next(rn
)) {
2097 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2099 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2102 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2104 /* Route entry in local table gets deleted immediately. */
2106 bgp_path_info_reap(rn
, pi
);
2113 * Delete all routes in per ES route-table
2115 static int delete_all_es_routes(struct bgp
*bgp
, struct evpnes
*es
)
2117 struct bgp_node
*rn
;
2118 struct bgp_path_info
*pi
, *nextpi
;
2120 /* Walk this ES's route table and delete all routes. */
2121 for (rn
= bgp_table_top(es
->route_table
); rn
;
2122 rn
= bgp_route_next(rn
)) {
2123 for (pi
= rn
->info
; (pi
!= NULL
) && (nextpi
= pi
->next
, 1);
2125 bgp_path_info_delete(rn
, pi
);
2126 bgp_path_info_reap(rn
, pi
);
2134 * Delete all routes in the per-VNI route table.
2136 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2138 struct bgp_node
*rn
;
2139 struct bgp_path_info
*pi
, *nextpi
;
2141 /* Walk this VNI's route table and delete all routes. */
2142 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2143 rn
= bgp_route_next(rn
)) {
2144 for (pi
= rn
->info
; (pi
!= NULL
) && (nextpi
= pi
->next
, 1);
2146 bgp_path_info_delete(rn
, pi
);
2147 bgp_path_info_reap(rn
, pi
);
2155 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2156 * export RT getting modified or change to tunnel IP. Note that these
2157 * situations need the route in the per-VNI table as well as the global
2158 * table to be updated (as attributes change).
2160 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2163 struct prefix_evpn p
;
2165 /* Update and advertise the type-3 route (only one) followed by the
2166 * locally learnt type-2 routes (MACIP) - for this VNI.
2168 * RT-3 only if doing head-end replication
2170 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
2171 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2172 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0);
2177 return update_all_type2_routes(bgp
, vpn
);
2180 /* Delete (and withdraw) local routes for specified ES from global and ES table.
2181 * Also remove all other routes from the per ES table.
2182 * Invoked when ES is deleted.
2184 static int delete_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
2187 char buf
[ESI_STR_LEN
];
2188 struct prefix_evpn p
;
2190 /* Delete and withdraw locally learnt ES route */
2191 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
.ipaddr_v4
);
2192 ret
= delete_evpn_type4_route(bgp
, es
, &p
);
2194 flog_err(EC_BGP_EVPN_ROUTE_DELETE
,
2195 "%u failed to delete type-4 route for ESI %s",
2196 bgp
->vrf_id
, esi_to_str(&es
->esi
, buf
, sizeof(buf
)));
2199 /* Delete all routes from per ES table */
2200 return delete_all_es_routes(bgp
, es
);
2204 * Delete (and withdraw) local routes for specified VNI from the global
2205 * table and per-VNI table. After this, remove all other routes from
2206 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2207 * (advertise-all-vni) being disabled.
2209 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2212 struct prefix_evpn p
;
2214 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2215 * followed by type-3 routes (only one) - for this VNI.
2217 ret
= delete_all_type2_routes(bgp
, vpn
);
2221 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2222 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2226 /* Delete all routes from the per-VNI table. */
2227 return delete_all_vni_routes(bgp
, vpn
);
2231 * There is a tunnel endpoint IP address change for this VNI, delete
2232 * prior type-3 route (if needed) and update.
2233 * Note: Route re-advertisement happens elsewhere after other processing
2236 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2237 struct in_addr originator_ip
)
2239 struct prefix_evpn p
;
2241 /* If VNI is not live, we only need to update the originator ip */
2242 if (!is_vni_live(vpn
)) {
2243 vpn
->originator_ip
= originator_ip
;
2247 /* Update the tunnel-ip hash */
2248 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2249 bgp_tip_add(bgp
, &originator_ip
);
2251 /* filter routes as martian nexthop db has changed */
2252 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2254 /* Need to withdraw type-3 route as the originator IP is part
2257 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2258 delete_evpn_route(bgp
, vpn
, &p
);
2260 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2261 vpn
->originator_ip
= originator_ip
;
2265 /* Install EVPN route entry in ES */
2266 static int install_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2267 struct prefix_evpn
*p
,
2268 struct bgp_path_info
*parent_pi
)
2271 struct bgp_node
*rn
= NULL
;
2272 struct bgp_path_info
*pi
= NULL
;
2273 struct attr
*attr_new
= NULL
;
2275 /* Create (or fetch) route within the VNI.
2276 * NOTE: There is no RD here.
2278 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
2280 /* Check if route entry is already present. */
2281 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
2283 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2287 /* Add (or update) attribute to hash. */
2288 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2290 /* Create new route with its attribute. */
2291 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2292 parent_pi
->peer
, attr_new
, rn
);
2293 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2294 bgp_path_info_extra_get(pi
);
2295 pi
->extra
->parent
= parent_pi
;
2296 bgp_path_info_add(rn
, pi
);
2298 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2299 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2300 bgp_unlock_node(rn
);
2303 /* The attribute has changed. */
2304 /* Add (or update) attribute to hash. */
2305 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2307 /* Restore route, if needed. */
2308 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2309 bgp_path_info_restore(rn
, pi
);
2311 /* Mark if nexthop has changed. */
2312 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2313 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2315 /* Unintern existing, set to new. */
2316 bgp_attr_unintern(&pi
->attr
);
2317 pi
->attr
= attr_new
;
2318 pi
->uptime
= bgp_clock();
2321 /* Perform route selection and update zebra, if required. */
2322 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2327 * Install route entry into the VRF routing table and invoke route selection.
2329 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2330 struct prefix_evpn
*evp
,
2331 struct bgp_path_info
*parent_pi
)
2333 struct bgp_node
*rn
;
2334 struct bgp_path_info
*pi
;
2336 struct attr
*attr_new
;
2339 struct prefix
*pp
= &p
;
2342 char buf
[PREFIX_STRLEN
];
2343 char buf1
[PREFIX_STRLEN
];
2345 memset(pp
, 0, sizeof(struct prefix
));
2346 ip_prefix_from_evpn_prefix(evp
, pp
);
2348 if (bgp_debug_zebra(NULL
)) {
2350 "installing evpn prefix %s as ip prefix %s in vrf %s",
2351 prefix2str(evp
, buf
, sizeof(buf
)),
2352 prefix2str(pp
, buf1
, sizeof(buf
)),
2353 vrf_id_to_name(bgp_vrf
->vrf_id
));
2356 /* Create (or fetch) route within the VRF. */
2357 /* NOTE: There is no RD here. */
2358 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2360 safi
= SAFI_UNICAST
;
2361 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2362 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2364 safi
= SAFI_UNICAST
;
2365 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2369 /* EVPN routes currently only support a IPv4 next hop which corresponds
2370 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2371 * or prefix route, we have to convert the next hop to an IPv4-mapped
2372 * address for the rest of the code to flow through. In the case of IPv4,
2373 * make sure to set the flag for next hop attribute.
2375 bgp_attr_dup(&attr
, parent_pi
->attr
);
2377 evpn_convert_nexthop_to_ipv6(&attr
);
2379 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2381 /* Check if route entry is already present. */
2382 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
2384 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2388 /* Add (or update) attribute to hash. */
2389 attr_new
= bgp_attr_intern(&attr
);
2391 /* Create new route with its attribute. */
2392 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2393 parent_pi
->peer
, attr_new
, rn
);
2394 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2395 bgp_path_info_extra_get(pi
);
2396 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2397 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2398 if (parent_pi
->extra
) {
2399 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2400 sizeof(pi
->extra
->label
));
2401 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2403 bgp_path_info_add(rn
, pi
);
2405 if (attrhash_cmp(pi
->attr
, &attr
)
2406 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2407 bgp_unlock_node(rn
);
2410 /* The attribute has changed. */
2411 /* Add (or update) attribute to hash. */
2412 attr_new
= bgp_attr_intern(&attr
);
2414 /* Restore route, if needed. */
2415 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2416 bgp_path_info_restore(rn
, pi
);
2418 /* Mark if nexthop has changed. */
2420 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2422 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2423 &attr_new
->mp_nexthop_global
)))
2424 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2426 /* Unintern existing, set to new. */
2427 bgp_attr_unintern(&pi
->attr
);
2428 pi
->attr
= attr_new
;
2429 pi
->uptime
= bgp_clock();
2432 bgp_aggregate_increment(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2434 /* Perform route selection and update zebra, if required. */
2435 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2441 * Install route entry into the VNI routing table and invoke route selection.
2443 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2444 struct prefix_evpn
*p
,
2445 struct bgp_path_info
*parent_pi
)
2447 struct bgp_node
*rn
;
2448 struct bgp_path_info
*pi
;
2449 struct attr
*attr_new
;
2452 /* Create (or fetch) route within the VNI. */
2453 /* NOTE: There is no RD here. */
2454 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2456 /* Check if route entry is already present. */
2457 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
2459 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2463 /* Add (or update) attribute to hash. */
2464 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2466 /* Create new route with its attribute. */
2467 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2468 parent_pi
->peer
, attr_new
, rn
);
2469 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2470 bgp_path_info_extra_get(pi
);
2471 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2472 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2473 if (parent_pi
->extra
) {
2474 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2475 sizeof(pi
->extra
->label
));
2476 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2478 bgp_path_info_add(rn
, pi
);
2480 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2481 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2482 bgp_unlock_node(rn
);
2485 /* The attribute has changed. */
2486 /* Add (or update) attribute to hash. */
2487 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2489 /* Restore route, if needed. */
2490 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2491 bgp_path_info_restore(rn
, pi
);
2493 /* Mark if nexthop has changed. */
2494 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2495 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2497 /* Unintern existing, set to new. */
2498 bgp_attr_unintern(&pi
->attr
);
2499 pi
->attr
= attr_new
;
2500 pi
->uptime
= bgp_clock();
2503 /* Perform route selection and update zebra, if required. */
2504 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2509 /* Uninstall EVPN route entry from ES route table */
2510 static int uninstall_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2511 struct prefix_evpn
*p
,
2512 struct bgp_path_info
*parent_pi
)
2515 struct bgp_node
*rn
;
2516 struct bgp_path_info
*pi
;
2518 if (!es
->route_table
)
2521 /* Locate route within the ESI.
2522 * NOTE: There is no RD here.
2524 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2528 /* Find matching route entry. */
2529 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
2531 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2537 /* Mark entry for deletion */
2538 bgp_path_info_delete(rn
, pi
);
2540 /* Perform route selection and update zebra, if required. */
2541 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2543 /* Unlock route node. */
2544 bgp_unlock_node(rn
);
2550 * Uninstall route entry from the VRF routing table and send message
2551 * to zebra, if appropriate.
2553 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2554 struct prefix_evpn
*evp
,
2555 struct bgp_path_info
*parent_pi
)
2557 struct bgp_node
*rn
;
2558 struct bgp_path_info
*pi
;
2561 struct prefix
*pp
= &p
;
2564 char buf
[PREFIX_STRLEN
];
2565 char buf1
[PREFIX_STRLEN
];
2567 memset(pp
, 0, sizeof(struct prefix
));
2568 ip_prefix_from_evpn_prefix(evp
, pp
);
2570 if (bgp_debug_zebra(NULL
)) {
2572 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2573 prefix2str(evp
, buf
, sizeof(buf
)),
2574 prefix2str(pp
, buf1
, sizeof(buf
)),
2575 vrf_id_to_name(bgp_vrf
->vrf_id
));
2578 /* Locate route within the VRF. */
2579 /* NOTE: There is no RD here. */
2580 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2582 safi
= SAFI_UNICAST
;
2583 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2586 safi
= SAFI_UNICAST
;
2587 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2593 /* Find matching route entry. */
2594 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
2596 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2602 bgp_aggregate_decrement(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2604 /* Mark entry for deletion */
2605 bgp_path_info_delete(rn
, pi
);
2607 /* Perform route selection and update zebra, if required. */
2608 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2610 /* Unlock route node. */
2611 bgp_unlock_node(rn
);
2617 * Uninstall route entry from the VNI routing table and send message
2618 * to zebra, if appropriate.
2620 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2621 struct prefix_evpn
*p
,
2622 struct bgp_path_info
*parent_pi
)
2624 struct bgp_node
*rn
;
2625 struct bgp_path_info
*pi
;
2628 /* Locate route within the VNI. */
2629 /* NOTE: There is no RD here. */
2630 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2634 /* Find matching route entry. */
2635 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
2637 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2643 /* Mark entry for deletion */
2644 bgp_path_info_delete(rn
, pi
);
2646 /* Perform route selection and update zebra, if required. */
2647 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2649 /* Unlock route node. */
2650 bgp_unlock_node(rn
);
2656 * Given a prefix, see if it belongs to ES.
2658 static int is_prefix_matching_for_es(struct prefix_evpn
*p
,
2661 /* if not an ES route return false */
2662 if (p
->prefix
.route_type
!= BGP_EVPN_ES_ROUTE
)
2665 if (memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
)) == 0)
2672 * Given a route entry and a VRF, see if this route entry should be
2673 * imported into the VRF i.e., RTs match.
2675 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2676 struct bgp_path_info
*pi
)
2678 struct attr
*attr
= pi
->attr
;
2679 struct ecommunity
*ecom
;
2683 /* Route should have valid RT to be even considered. */
2684 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2687 ecom
= attr
->ecommunity
;
2688 if (!ecom
|| !ecom
->size
)
2691 /* For each extended community RT, see if it matches this VNI. If any RT
2692 * matches, we're done.
2694 for (i
= 0; i
< ecom
->size
; i
++) {
2696 uint8_t type
, sub_type
;
2697 struct ecommunity_val
*eval
;
2698 struct ecommunity_val eval_tmp
;
2699 struct vrf_irt_node
*irt
;
2701 /* Only deal with RTs */
2702 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2703 eval
= (struct ecommunity_val
*)(ecom
->val
2704 + (i
* ECOMMUNITY_SIZE
));
2707 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2710 /* See if this RT matches specified VNIs import RTs */
2711 irt
= lookup_vrf_import_rt(eval
);
2713 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2716 /* Also check for non-exact match. In this, we mask out the AS
2718 * only check on the local-admin sub-field. This is to
2720 * VNI as the RT for EBGP peering too.
2723 if (type
== ECOMMUNITY_ENCODE_AS
2724 || type
== ECOMMUNITY_ENCODE_AS4
2725 || type
== ECOMMUNITY_ENCODE_IP
) {
2726 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2727 mask_ecom_global_admin(&eval_tmp
, eval
);
2728 irt
= lookup_vrf_import_rt(&eval_tmp
);
2731 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2739 * Given a route entry and a VNI, see if this route entry should be
2740 * imported into the VNI i.e., RTs match.
2742 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2743 struct bgp_path_info
*pi
)
2745 struct attr
*attr
= pi
->attr
;
2746 struct ecommunity
*ecom
;
2750 /* Route should have valid RT to be even considered. */
2751 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2754 ecom
= attr
->ecommunity
;
2755 if (!ecom
|| !ecom
->size
)
2758 /* For each extended community RT, see if it matches this VNI. If any RT
2759 * matches, we're done.
2761 for (i
= 0; i
< ecom
->size
; i
++) {
2763 uint8_t type
, sub_type
;
2764 struct ecommunity_val
*eval
;
2765 struct ecommunity_val eval_tmp
;
2766 struct irt_node
*irt
;
2768 /* Only deal with RTs */
2769 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2770 eval
= (struct ecommunity_val
*)(ecom
->val
2771 + (i
* ECOMMUNITY_SIZE
));
2774 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2777 /* See if this RT matches specified VNIs import RTs */
2778 irt
= lookup_import_rt(bgp
, eval
);
2780 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2783 /* Also check for non-exact match. In this, we mask out the AS
2785 * only check on the local-admin sub-field. This is to
2787 * VNI as the RT for EBGP peering too.
2790 if (type
== ECOMMUNITY_ENCODE_AS
2791 || type
== ECOMMUNITY_ENCODE_AS4
2792 || type
== ECOMMUNITY_ENCODE_IP
) {
2793 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2794 mask_ecom_global_admin(&eval_tmp
, eval
);
2795 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2798 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2805 static int install_uninstall_routes_for_es(struct bgp
*bgp
,
2812 char buf
[PREFIX_STRLEN
];
2813 char buf1
[ESI_STR_LEN
];
2814 struct bgp_node
*rd_rn
, *rn
;
2815 struct bgp_table
*table
;
2816 struct bgp_path_info
*pi
;
2822 * Walk entire global routing table and evaluate routes which could be
2823 * imported into this VRF. Note that we need to loop through all global
2824 * routes to determine which route matches the import rt on vrf
2826 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2827 rd_rn
= bgp_route_next(rd_rn
)) {
2828 table
= (struct bgp_table
*)(rd_rn
->info
);
2832 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2833 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2835 for (pi
= rn
->info
; pi
; pi
= pi
->next
) {
2837 * Consider "valid" remote routes applicable for
2840 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
2841 && pi
->type
== ZEBRA_ROUTE_BGP
2842 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
2845 if (!is_prefix_matching_for_es(evp
, es
))
2849 ret
= install_evpn_route_entry_in_es(
2852 ret
= uninstall_evpn_route_entry_in_es(
2858 "Failed to %s EVPN %s route in ESI %s",
2861 prefix2str(evp
, buf
,
2863 esi_to_str(&es
->esi
, buf1
,
2874 * Install or uninstall mac-ip routes are appropriate for this
2877 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
2881 struct bgp_node
*rd_rn
, *rn
;
2882 struct bgp_table
*table
;
2883 struct bgp_path_info
*pi
;
2885 char buf
[PREFIX_STRLEN
];
2886 struct bgp
*bgp_def
= NULL
;
2890 bgp_def
= bgp_get_default();
2894 /* Walk entire global routing table and evaluate routes which could be
2895 * imported into this VRF. Note that we need to loop through all global
2896 * routes to determine which route matches the import rt on vrf
2898 for (rd_rn
= bgp_table_top(bgp_def
->rib
[afi
][safi
]); rd_rn
;
2899 rd_rn
= bgp_route_next(rd_rn
)) {
2900 table
= (struct bgp_table
*)(rd_rn
->info
);
2904 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2905 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2907 /* if not mac-ip route skip this route */
2908 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2909 || evp
->prefix
.route_type
2910 == BGP_EVPN_IP_PREFIX_ROUTE
))
2913 /* if not a mac+ip route skip this route */
2914 if (!(is_evpn_prefix_ipaddr_v4(evp
)
2915 || is_evpn_prefix_ipaddr_v6(evp
)))
2918 for (pi
= rn
->info
; pi
; pi
= pi
->next
) {
2919 /* Consider "valid" remote routes applicable for
2922 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
2923 && pi
->type
== ZEBRA_ROUTE_BGP
2924 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
2927 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
2929 ret
= install_evpn_route_entry_in_vrf(
2932 ret
= uninstall_evpn_route_entry_in_vrf(
2938 "Failed to %s EVPN %s route in VRF %s",
2941 prefix2str(evp
, buf
,
2956 * Install or uninstall routes of specified type that are appropriate for this
2959 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
2960 struct bgpevpn
*vpn
,
2961 bgp_evpn_route_type rtype
,
2966 struct bgp_node
*rd_rn
, *rn
;
2967 struct bgp_table
*table
;
2968 struct bgp_path_info
*pi
;
2974 /* Walk entire global routing table and evaluate routes which could be
2975 * imported into this VPN. Note that we cannot just look at the routes
2977 * the VNI's RD - remote routes applicable for this VNI could have any
2980 /* EVPN routes are a 2-level table. */
2981 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2982 rd_rn
= bgp_route_next(rd_rn
)) {
2983 table
= (struct bgp_table
*)(rd_rn
->info
);
2987 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2988 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2990 if (evp
->prefix
.route_type
!= rtype
)
2993 for (pi
= rn
->info
; pi
; pi
= pi
->next
) {
2994 /* Consider "valid" remote routes applicable for
2996 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
2997 && pi
->type
== ZEBRA_ROUTE_BGP
2998 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3001 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3003 ret
= install_evpn_route_entry(
3006 ret
= uninstall_evpn_route_entry(
3012 "%u: Failed to %s EVPN %s route in VNI %u",
3016 rtype
== BGP_EVPN_MAC_IP_ROUTE
3030 /* Install any existing remote ES routes applicable for this ES into its routing
3031 * table. This is invoked when ES comes up.
3033 static int install_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
3035 return install_uninstall_routes_for_es(bgp
, es
, 1);
3039 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3040 * is invoked upon l3vni-add or l3vni import rt change
3042 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3044 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3049 * Install any existing remote routes applicable for this VNI into its
3050 * routing table. This is invoked when a VNI becomes "live" or its Import
3053 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3057 /* Install type-3 routes followed by type-2 routes - the ones applicable
3060 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3065 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3069 /* uninstall routes from l3vni vrf. */
3070 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3072 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3077 * Uninstall any existing remote routes for this VNI. One scenario in which
3078 * this is invoked is upon an import RT change.
3080 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3084 /* Uninstall type-2 routes followed by type-3 routes - the ones
3088 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3093 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3097 /* Install or unistall route in ES */
3098 static int install_uninstall_route_in_es(struct bgp
*bgp
, struct evpnes
*es
,
3099 afi_t afi
, safi_t safi
,
3100 struct prefix_evpn
*evp
,
3101 struct bgp_path_info
*pi
, int install
)
3104 char buf
[ESI_STR_LEN
];
3107 ret
= install_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3109 ret
= uninstall_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3114 "%u: Failed to %s EVPN %s route in ESI %s", bgp
->vrf_id
,
3115 install
? "install" : "uninstall", "ES",
3116 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)));
3123 * Install or uninstall route in matching VRFs (list).
3125 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3126 safi_t safi
, struct prefix_evpn
*evp
,
3127 struct bgp_path_info
*pi
,
3128 struct list
*vrfs
, int install
)
3130 char buf
[PREFIX2STR_BUFFER
];
3131 struct bgp
*bgp_vrf
;
3132 struct listnode
*node
, *nnode
;
3134 /* Only type-2/type-5 routes go into a VRF */
3135 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3136 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3139 /* if it is type-2 route and not a mac+ip route skip this route */
3140 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3141 && !(is_evpn_prefix_ipaddr_v4(evp
)
3142 || is_evpn_prefix_ipaddr_v6(evp
)))
3145 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3149 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3151 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3155 flog_err(EC_BGP_EVPN_FAIL
,
3156 "%u: Failed to %s prefix %s in VRF %s",
3158 install
? "install" : "uninstall",
3159 prefix2str(evp
, buf
, sizeof(buf
)),
3160 vrf_id_to_name(bgp_vrf
->vrf_id
));
3169 * Install or uninstall route in matching VNIs (list).
3171 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3172 safi_t safi
, struct prefix_evpn
*evp
,
3173 struct bgp_path_info
*pi
,
3174 struct list
*vnis
, int install
)
3176 struct bgpevpn
*vpn
;
3177 struct listnode
*node
, *nnode
;
3179 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3182 if (!is_vni_live(vpn
))
3186 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3188 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3191 flog_err(EC_BGP_EVPN_FAIL
,
3192 "%u: Failed to %s EVPN %s route in VNI %u",
3193 bgp
->vrf_id
, install
? "install" : "uninstall",
3194 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3206 * Install or uninstall route for appropriate VNIs/ESIs.
3208 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3210 struct bgp_path_info
*pi
, int import
)
3212 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3213 struct attr
*attr
= pi
->attr
;
3214 struct ecommunity
*ecom
;
3219 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3220 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3221 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3222 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3223 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3226 /* If we don't have Route Target, nothing much to do. */
3227 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3230 ecom
= attr
->ecommunity
;
3231 if (!ecom
|| !ecom
->size
)
3234 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3235 * attached to the route */
3236 for (i
= 0; i
< ecom
->size
; i
++) {
3238 uint8_t type
, sub_type
;
3239 struct ecommunity_val
*eval
;
3240 struct ecommunity_val eval_tmp
;
3241 struct irt_node
*irt
; /* import rt for l2vni */
3242 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3245 /* Only deal with RTs */
3246 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3247 eval
= (struct ecommunity_val
*)(ecom
->val
3248 + (i
* ECOMMUNITY_SIZE
));
3251 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3255 * macip routes (type-2) are imported into VNI and VRF tables.
3256 * IMET route is imported into VNI table.
3257 * prefix routes are imported into VRF table.
3259 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3260 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3261 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3263 irt
= lookup_import_rt(bgp
, eval
);
3265 install_uninstall_route_in_vnis(
3266 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3269 vrf_irt
= lookup_vrf_import_rt(eval
);
3271 install_uninstall_route_in_vrfs(
3272 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3275 /* Also check for non-exact match.
3276 * In this, we mask out the AS and
3277 * only check on the local-admin sub-field.
3278 * This is to facilitate using
3279 * VNI as the RT for EBGP peering too.
3283 if (type
== ECOMMUNITY_ENCODE_AS
3284 || type
== ECOMMUNITY_ENCODE_AS4
3285 || type
== ECOMMUNITY_ENCODE_IP
) {
3286 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3287 mask_ecom_global_admin(&eval_tmp
, eval
);
3288 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3289 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3293 install_uninstall_route_in_vnis(
3294 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3297 install_uninstall_route_in_vrfs(
3298 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3302 /* es route is imported into the es table */
3303 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3305 /* we will match based on the entire esi to avoid
3306 * imoort of an es route for esi2 into esi1
3308 es
= bgp_evpn_lookup_es(bgp
, &evp
->prefix
.es_addr
.esi
);
3309 if (es
&& is_es_local(es
))
3310 install_uninstall_route_in_es(
3311 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3319 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3322 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3324 /* delete all ipv4 routes and withdraw from peers */
3325 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3326 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3328 /* delete all ipv6 routes and withdraw from peers */
3329 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3330 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3334 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3337 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3339 /* update all ipv4 routes */
3340 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3341 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3343 /* update all ipv6 routes */
3344 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3345 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3349 * update and advertise local routes for a VRF as type-5 routes.
3350 * This is invoked upon RD change for a VRF. Note taht the processing is only
3351 * done in the global route table using the routes which already exist in the
3354 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3356 /* skip if the RD is configured */
3357 if (is_vrf_rd_configured(bgp_vrf
))
3360 /* derive the RD for the VRF based on new router-id */
3361 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3363 /* update advertise ipv4|ipv6 routes as type-5 routes */
3364 update_advertise_vrf_routes(bgp_vrf
);
3368 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3369 * This is invoked upon VRF RD change. The processing is done only from global
3372 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3374 /* skip if the RD is configured */
3375 if (is_vrf_rd_configured(bgp_vrf
))
3378 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3379 delete_withdraw_vrf_routes(bgp_vrf
);
3383 * Update and advertise local routes for a VNI. Invoked upon router-id
3384 * change. Note that the processing is done only on the global route table
3385 * using routes that already exist in the per-VNI table.
3387 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3389 struct prefix_evpn p
;
3390 struct bgp_node
*rn
, *global_rn
;
3391 struct bgp_path_info
*pi
, *global_pi
;
3393 afi_t afi
= AFI_L2VPN
;
3394 safi_t safi
= SAFI_EVPN
;
3396 /* Locate type-3 route for VNI in the per-VNI table and use its
3397 * attributes to create and advertise the type-3 route for this VNI
3398 * in the global table.
3400 * RT-3 only if doing head-end replication
3402 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
3403 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3404 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3405 if (!rn
) /* unexpected */
3407 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
3408 if (pi
->peer
== bgp
->peer_self
&&
3409 pi
->type
== ZEBRA_ROUTE_BGP
3410 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3412 if (!pi
) /* unexpected */
3416 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3417 (struct prefix
*)&p
, &vpn
->prd
);
3418 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
,
3419 1, &pi
, 0, mac_mobility_seqnum(attr
));
3421 /* Schedule for processing and unlock node. */
3422 bgp_process(bgp
, global_rn
, afi
, safi
);
3423 bgp_unlock_node(global_rn
);
3426 /* Now, walk this VNI's route table and use the route and its attribute
3427 * to create and schedule route in global table.
3429 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
3430 rn
= bgp_route_next(rn
)) {
3431 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3433 /* Identify MAC-IP local routes. */
3434 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3437 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
3438 if (pi
->peer
== bgp
->peer_self
3439 && pi
->type
== ZEBRA_ROUTE_BGP
3440 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3445 /* Create route in global routing table using this route entry's
3449 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3450 (struct prefix
*)evp
, &vpn
->prd
);
3452 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
3454 mac_mobility_seqnum(attr
));
3456 /* Schedule for processing and unlock node. */
3457 bgp_process(bgp
, global_rn
, afi
, safi
);
3458 bgp_unlock_node(global_rn
);
3465 * Delete (and withdraw) local routes for a VNI - only from the global
3466 * table. Invoked upon router-id change.
3468 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3471 struct prefix_evpn p
;
3472 struct bgp_node
*global_rn
;
3473 struct bgp_path_info
*pi
;
3474 afi_t afi
= AFI_L2VPN
;
3475 safi_t safi
= SAFI_EVPN
;
3477 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3478 * for this VNI - from the global table.
3480 ret
= delete_global_type2_routes(bgp
, vpn
);
3484 /* Remove type-3 route for this VNI from global table. */
3485 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3486 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3487 (struct prefix
*)&p
, &vpn
->prd
);
3489 /* Delete route entry in the global EVPN table. */
3490 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
3492 /* Schedule for processing - withdraws to peers happen from
3496 bgp_process(bgp
, global_rn
, afi
, safi
);
3497 bgp_unlock_node(global_rn
);
3504 * Handle router-id change. Update and advertise local routes corresponding
3505 * to this VNI from peers. Note that this is invoked after updating the
3506 * router-id. The routes in the per-VNI table are used to create routes in
3507 * the global table and schedule them.
3509 static void update_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3511 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3513 /* Skip VNIs with configured RD. */
3514 if (is_rd_configured(vpn
))
3517 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3518 update_advertise_vni_routes(bgp
, vpn
);
3522 * Handle router-id change. Delete and withdraw local routes corresponding
3523 * to this VNI from peers. Note that this is invoked prior to updating
3524 * the router-id and is done only on the global route table, the routes
3525 * are needed in the per-VNI table to re-advertise with new router id.
3527 static void withdraw_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3529 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3531 /* Skip VNIs with configured RD. */
3532 if (is_rd_configured(vpn
))
3535 delete_withdraw_vni_routes(bgp
, vpn
);
3539 * Create RT-3 for a VNI and schedule for processing and advertisement.
3540 * This is invoked upon flooding mode changing to head-end replication.
3542 static void create_advertise_type3(struct hash_backet
*backet
, void *data
)
3544 struct bgpevpn
*vpn
= backet
->data
;
3545 struct bgp
*bgp
= data
;
3546 struct prefix_evpn p
;
3548 if (!vpn
|| !is_vni_live(vpn
))
3551 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3552 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0))
3553 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3554 "Type3 route creation failure for VNI %u", vpn
->vni
);
3558 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3559 * This is invoked upon flooding mode changing to drop BUM packets.
3561 static void delete_withdraw_type3(struct hash_backet
*backet
, void *data
)
3563 struct bgpevpn
*vpn
= backet
->data
;
3564 struct bgp
*bgp
= data
;
3565 struct prefix_evpn p
;
3567 if (!vpn
|| !is_vni_live(vpn
))
3570 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3571 delete_evpn_route(bgp
, vpn
, &p
);
3575 * Process received EVPN type-2 route (advertise or withdraw).
3577 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3578 struct attr
*attr
, uint8_t *pfx
, int psize
,
3579 uint32_t addpath_id
)
3581 struct prefix_rd prd
;
3582 struct prefix_evpn p
;
3583 struct bgp_route_evpn evpn
;
3585 uint8_t macaddr_len
;
3586 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
3587 uint32_t num_labels
= 0;
3591 /* Type-2 route should be either 33, 37 or 49 bytes or an
3592 * additional 3 bytes if there is a second label (VNI):
3593 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3594 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3595 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3597 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3598 && psize
!= 40 && psize
!= 52) {
3599 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3600 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3601 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3605 memset(&evpn
, 0, sizeof(evpn
));
3607 /* Make prefix_rd */
3608 prd
.family
= AF_UNSPEC
;
3610 memcpy(&prd
.val
, pfx
, 8);
3613 /* Make EVPN prefix. */
3614 memset(&p
, 0, sizeof(struct prefix_evpn
));
3616 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3617 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3619 /* Copy Ethernet Seg Identifier */
3620 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
3623 /* Copy Ethernet Tag */
3624 memcpy(ð_tag
, pfx
, 4);
3625 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3628 /* Get the MAC Addr len */
3629 macaddr_len
= *pfx
++;
3631 /* Get the MAC Addr */
3632 if (macaddr_len
== (ETH_ALEN
* 8)) {
3633 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
3637 EC_BGP_EVPN_ROUTE_INVALID
,
3638 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3639 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3645 ipaddr_len
= *pfx
++;
3646 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3647 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3649 EC_BGP_EVPN_ROUTE_INVALID
,
3650 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3651 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3656 ipaddr_len
/= 8; /* Convert to bytes. */
3657 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3660 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
3664 /* Get the VNI(s). Stored as bytes here. */
3666 memset(label
, 0, sizeof(label
));
3667 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
3668 pfx
+= BGP_LABEL_BYTES
;
3669 psize
-= (33 + ipaddr_len
);
3670 /* Do we have a second VNI? */
3673 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
3675 * If in future, we are required to access additional fields,
3676 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
3681 /* Process the route. */
3683 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3684 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3685 &prd
, &label
[0], num_labels
, 0, &evpn
);
3687 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3688 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3689 &prd
, &label
[0], num_labels
, &evpn
);
3694 * Process received EVPN type-3 route (advertise or withdraw).
3696 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3697 struct attr
*attr
, uint8_t *pfx
, int psize
,
3698 uint32_t addpath_id
)
3700 struct prefix_rd prd
;
3701 struct prefix_evpn p
;
3706 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3707 * IP len (1) and IP (4 or 16).
3709 if (psize
!= 17 && psize
!= 29) {
3710 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3711 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3712 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3716 /* If PMSI is present, log if it is anything other than IR.
3717 * Note: We just simply ignore the values as it is not clear if
3718 * doing anything else is better.
3721 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3722 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
) {
3724 EC_BGP_EVPN_PMSI_PRESENT
,
3725 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3726 peer
->bgp
->vrf_id
, peer
->host
,
3727 attr
->pmsi_tnl_type
);
3731 /* Make prefix_rd */
3732 prd
.family
= AF_UNSPEC
;
3734 memcpy(&prd
.val
, pfx
, 8);
3737 /* Make EVPN prefix. */
3738 memset(&p
, 0, sizeof(struct prefix_evpn
));
3740 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3741 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3743 /* Copy Ethernet Tag */
3744 memcpy(ð_tag
, pfx
, 4);
3745 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
3749 ipaddr_len
= *pfx
++;
3750 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3751 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
3752 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
3755 EC_BGP_EVPN_ROUTE_INVALID
,
3756 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3757 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3761 /* Process the route. */
3763 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3764 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3765 &prd
, NULL
, 0, 0, NULL
);
3767 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3768 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3769 &prd
, NULL
, 0, NULL
);
3774 * Process received EVPN type-4 route (advertise or withdraw).
3776 static int process_type4_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3777 struct attr
*attr
, uint8_t *pfx
, int psize
,
3778 uint32_t addpath_id
)
3783 struct in_addr vtep_ip
;
3784 struct prefix_rd prd
;
3785 struct prefix_evpn p
;
3787 /* Type-4 route should be either 23 or 35 bytes
3788 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
3790 if (psize
!= 23 && psize
!= 35) {
3791 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3792 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
3793 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3797 /* Make prefix_rd */
3798 prd
.family
= AF_UNSPEC
;
3800 memcpy(&prd
.val
, pfx
, 8);
3804 memcpy(&esi
, pfx
, ESI_BYTES
);
3809 ipaddr_len
= *pfx
++;
3810 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3811 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
3814 EC_BGP_EVPN_ROUTE_INVALID
,
3815 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
3816 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3820 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
3821 /* Process the route. */
3823 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3824 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3825 &prd
, NULL
, 0, 0, NULL
);
3827 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3828 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3829 &prd
, NULL
, 0, NULL
);
3836 * Process received EVPN type-5 route (advertise or withdraw).
3838 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3839 struct attr
*attr
, uint8_t *pfx
, int psize
,
3840 uint32_t addpath_id
, int withdraw
)
3842 struct prefix_rd prd
;
3843 struct prefix_evpn p
;
3844 struct bgp_route_evpn evpn
;
3847 mpls_label_t label
; /* holds the VNI as in the packet */
3850 /* Type-5 route should be 34 or 58 bytes:
3851 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3852 * GW (4 or 16) and VNI (3).
3853 * Note that the IP and GW should both be IPv4 or both IPv6.
3855 if (psize
!= 34 && psize
!= 58) {
3856 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3857 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3858 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3862 /* Make prefix_rd */
3863 prd
.family
= AF_UNSPEC
;
3865 memcpy(&prd
.val
, pfx
, 8);
3868 /* Make EVPN prefix. */
3869 memset(&p
, 0, sizeof(struct prefix_evpn
));
3871 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3872 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
3874 /* Additional information outside of prefix - ESI and GW IP */
3875 memset(&evpn
, 0, sizeof(evpn
));
3878 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
3881 /* Fetch Ethernet Tag. */
3882 memcpy(ð_tag
, pfx
, 4);
3883 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
3886 /* Fetch IP prefix length. */
3888 if (ippfx_len
> IPV6_MAX_BITLEN
) {
3890 EC_BGP_EVPN_ROUTE_INVALID
,
3891 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3892 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
3895 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
3897 /* Determine IPv4 or IPv6 prefix */
3898 /* Since the address and GW are from the same family, this just becomes
3899 * a simple check on the total size.
3902 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
3903 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
3905 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
3908 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
3909 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
3911 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
3915 /* Get the VNI (in MPLS label field). Stored as bytes here. */
3916 memset(&label
, 0, sizeof(label
));
3917 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
3920 * If in future, we are required to access additional fields,
3921 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
3925 /* Process the route. */
3927 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3928 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3929 &prd
, &label
, 1, 0, &evpn
);
3931 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3932 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3933 &prd
, &label
, 1, &evpn
);
3938 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
3939 struct prefix_rd
*prd
, mpls_label_t
*label
,
3940 uint32_t num_labels
, struct attr
*attr
)
3944 struct evpn_addr
*p_evpn_p
;
3946 memset(&temp
, 0, 16);
3947 if (p
->family
!= AF_EVPN
)
3949 p_evpn_p
= &(p
->u
.prefix_evpn
);
3951 /* len denites the total len of IP and GW-IP in the route
3952 IP and GW-IP have to be both ipv4 or ipv6
3954 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3955 len
= 8; /* IP and GWIP are both ipv4 */
3957 len
= 32; /* IP and GWIP are both ipv6 */
3958 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
3959 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
3960 stream_put(s
, prd
->val
, 8);
3962 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
3964 stream_put(s
, &temp
, 10);
3965 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
3966 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
3967 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3968 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
3970 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
3972 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3974 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
3976 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
3978 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3979 stream_put_ipv4(s
, 0);
3981 stream_put(s
, &temp
, 16);
3985 stream_put(s
, label
, 3);
3991 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
3993 static void cleanup_vni_on_disable(struct hash_backet
*backet
, struct bgp
*bgp
)
3995 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3997 /* Remove EVPN routes and schedule for processing. */
3998 delete_routes_for_vni(bgp
, vpn
);
4000 /* Clear "live" flag and see if hash needs to be freed. */
4001 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4002 if (!is_vni_configured(vpn
))
4003 bgp_evpn_free(bgp
, vpn
);
4007 * Free a VNI entry; iterator function called during cleanup.
4009 static void free_vni_entry(struct hash_backet
*backet
, struct bgp
*bgp
)
4011 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
4013 delete_all_vni_routes(bgp
, vpn
);
4014 bgp_evpn_free(bgp
, vpn
);
4018 * Derive AUTO import RT for BGP VRF - L3VNI
4020 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4022 struct bgp
*bgp_def
= NULL
;
4024 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4025 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4028 bgp_def
= bgp_get_default();
4031 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4035 * Delete AUTO import RT from BGP VRF - L3VNI
4037 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4039 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4043 * Derive AUTO export RT for BGP VRF - L3VNI
4045 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4047 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4048 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4052 * Delete AUTO export RT from BGP VRF - L3VNI
4054 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4056 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4059 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4061 struct bgp
*bgp_def
= NULL
;
4062 struct listnode
*node
= NULL
;
4063 struct bgpevpn
*vpn
= NULL
;
4065 bgp_def
= bgp_get_default();
4069 /* update all type-5 routes */
4070 update_advertise_vrf_routes(bgp_vrf
);
4072 /* update all type-2 routes */
4073 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4074 update_routes_for_vni(bgp_def
, vpn
);
4078 * Handle autort change for a given VNI.
4080 static void update_autort_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
4082 struct bgpevpn
*vpn
= backet
->data
;
4084 if (!is_import_rt_configured(vpn
)) {
4085 if (is_vni_live(vpn
))
4086 bgp_evpn_uninstall_routes(bgp
, vpn
);
4087 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4088 list_delete_all_node(vpn
->import_rtl
);
4089 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4090 if (is_vni_live(vpn
))
4091 bgp_evpn_install_routes(bgp
, vpn
);
4093 if (!is_export_rt_configured(vpn
)) {
4094 list_delete_all_node(vpn
->export_rtl
);
4095 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4096 if (is_vni_live(vpn
))
4097 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4105 /* withdraw type-5 route corresponding to ip prefix */
4106 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4107 afi_t afi
, safi_t safi
)
4110 struct prefix_evpn evp
;
4111 char buf
[PREFIX_STRLEN
];
4113 build_type5_prefix_from_ip_prefix(&evp
, p
);
4114 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4117 EC_BGP_EVPN_ROUTE_DELETE
,
4118 "%u failed to delete type-5 route for prefix %s in vrf %s",
4119 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4120 vrf_id_to_name(bgp_vrf
->vrf_id
));
4124 /* withdraw all type-5 routes for an address family */
4125 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4127 struct bgp_table
*table
= NULL
;
4128 struct bgp_node
*rn
= NULL
;
4129 struct bgp_path_info
*pi
;
4131 table
= bgp_vrf
->rib
[afi
][safi
];
4132 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4133 /* Only care about "selected" routes - non-imported. */
4134 /* TODO: Support for AddPath for EVPN. */
4135 for (pi
= rn
->info
; pi
; pi
= pi
->next
) {
4136 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4137 && (!pi
->extra
|| !pi
->extra
->parent
)) {
4138 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
4147 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4148 * to this function correspond to those of the source IP prefix (best
4149 * path in the case of the attr. In the case of a local prefix (when we
4150 * are advertising local subnets), the src_attr will be NULL.
4152 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4153 struct attr
*src_attr
, afi_t afi
,
4157 struct prefix_evpn evp
;
4158 char buf
[PREFIX_STRLEN
];
4160 build_type5_prefix_from_ip_prefix(&evp
, p
);
4161 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4163 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4164 "%u: Failed to create type-5 route for prefix %s",
4165 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4168 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4169 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4170 * advertisement is enabled.
4172 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4175 struct bgp_table
*table
= NULL
;
4176 struct bgp_node
*rn
= NULL
;
4177 struct bgp_path_info
*pi
;
4179 table
= bgp_vrf
->rib
[afi
][safi
];
4180 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4181 /* Need to identify the "selected" route entry to use its
4182 * attribute. Also, we only consider "non-imported" routes.
4183 * TODO: Support for AddPath for EVPN.
4185 for (pi
= rn
->info
; pi
; pi
= pi
->next
) {
4186 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4187 && (!pi
->extra
|| !pi
->extra
->parent
)) {
4189 /* apply the route-map */
4190 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4193 ret
= route_map_apply(
4194 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4196 &rn
->p
, RMAP_BGP
, pi
);
4197 if (ret
== RMAP_DENYMATCH
)
4200 bgp_evpn_advertise_type5_route(
4201 bgp_vrf
, &rn
->p
, pi
->attr
, afi
, safi
);
4208 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4210 struct listnode
*node
, *nnode
, *node_to_del
;
4211 struct ecommunity
*ecom
, *ecom_auto
;
4212 struct ecommunity_val eval
;
4214 if (bgp
->advertise_autort_rfc8365
)
4215 vni
|= EVPN_AUTORT_VXLAN
;
4216 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4218 ecom_auto
= ecommunity_new();
4219 ecommunity_add_val(ecom_auto
, &eval
);
4222 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4223 if (ecommunity_match(ecom
, ecom_auto
)) {
4224 ecommunity_free(&ecom
);
4230 list_delete_node(rtl
, node_to_del
);
4232 ecommunity_free(&ecom_auto
);
4235 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4236 struct ecommunity
*ecomadd
)
4238 /* uninstall routes from vrf */
4239 uninstall_routes_for_vrf(bgp_vrf
);
4241 /* Cleanup the RT to VRF mapping */
4242 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4244 /* Remove auto generated RT */
4245 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4247 /* Add the newly configured RT to RT list */
4248 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4249 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4251 /* map VRF to its RTs */
4252 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4254 /* install routes matching the new VRF */
4255 install_routes_for_vrf(bgp_vrf
);
4258 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4259 struct ecommunity
*ecomdel
)
4261 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4262 struct ecommunity
*ecom
= NULL
;
4264 /* uninstall routes from vrf */
4265 uninstall_routes_for_vrf(bgp_vrf
);
4267 /* Cleanup the RT to VRF mapping */
4268 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4270 /* remove the RT from the RT list */
4271 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4272 if (ecommunity_match(ecom
, ecomdel
)) {
4273 ecommunity_free(&ecom
);
4280 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4282 assert(bgp_vrf
->vrf_import_rtl
);
4283 /* fallback to auto import rt, if this was the last RT */
4284 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4285 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4286 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4289 /* map VRFs to its RTs */
4290 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4292 /* install routes matching this new RT */
4293 install_routes_for_vrf(bgp_vrf
);
4296 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4297 struct ecommunity
*ecomadd
)
4299 /* remove auto-generated RT */
4300 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4302 /* Add the new RT to the RT list */
4303 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4304 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4306 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4309 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4310 struct ecommunity
*ecomdel
)
4312 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4313 struct ecommunity
*ecom
= NULL
;
4315 /* Remove the RT from the RT list */
4316 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4317 if (ecommunity_match(ecom
, ecomdel
)) {
4318 ecommunity_free(&ecom
);
4325 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4328 * Temporary assert to make SA happy.
4329 * The ALL_LIST_ELEMENTS macro above has a NULL check
4330 * which means that SA is going to complain about
4331 * the list_isempty call, which doesn't NULL check.
4332 * So until we get this situation cleaned up, here
4335 assert(bgp_vrf
->vrf_export_rtl
);
4337 /* fall back to auto-generated RT if this was the last RT */
4338 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4339 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4340 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4343 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4347 * Handle change to BGP router id. This is invoked twice by the change
4348 * handler, first before the router id has been changed and then after
4349 * the router id has been changed. The first invocation will result in
4350 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4351 * will result in the routes being re-advertised.
4353 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4357 /* delete and withdraw all the type-5 routes
4358 stored in the global table for this vrf
4360 withdraw_router_id_vrf(bgp
);
4362 /* delete all the VNI routes (type-2/type-3) routes for all the
4365 hash_iterate(bgp
->vnihash
,
4366 (void (*)(struct hash_backet
*,
4367 void *))withdraw_router_id_vni
,
4371 /* advertise all routes in the vrf as type-5 routes with the new
4374 update_router_id_vrf(bgp
);
4376 /* advertise all the VNI routes (type-2/type-3) routes with the
4379 hash_iterate(bgp
->vnihash
,
4380 (void (*)(struct hash_backet
*,
4381 void *))update_router_id_vni
,
4387 * Handle change to auto-RT algorithm - update and advertise local routes.
4389 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4391 hash_iterate(bgp
->vnihash
,
4392 (void (*)(struct hash_backet
*,
4393 void*))update_autort_vni
,
4398 * Handle change to export RT - update and advertise local routes.
4400 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4402 return update_routes_for_vni(bgp
, vpn
);
4405 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4408 delete_withdraw_vrf_routes(bgp_vrf
);
4410 update_advertise_vrf_routes(bgp_vrf
);
4414 * Handle change to RD. This is invoked twice by the change handler,
4415 * first before the RD has been changed and then after the RD has
4416 * been changed. The first invocation will result in local routes
4417 * of this VNI being deleted and withdrawn and the next will result
4418 * in the routes being re-advertised.
4420 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4424 delete_withdraw_vni_routes(bgp
, vpn
);
4426 update_advertise_vni_routes(bgp
, vpn
);
4430 * Install routes for this VNI. Invoked upon change to Import RT.
4432 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4434 return install_routes_for_vni(bgp
, vpn
);
4438 * Uninstall all routes installed for this VNI. Invoked upon change
4441 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4443 return uninstall_routes_for_vni(bgp
, vpn
);
4447 * TODO: Hardcoded for a maximum of 2 VNIs right now
4449 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4454 vni1
= label2vni(label
);
4455 if (num_labels
== 2) {
4456 vni2
= label2vni(label
+ 1);
4457 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4459 snprintf(buf
, len
, "%u", vni1
);
4464 * Function to convert evpn route to json format.
4465 * NOTE: We don't use prefix2str as the output here is a bit different.
4467 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
4469 char buf1
[ETHER_ADDR_STRLEN
];
4470 char buf2
[PREFIX2STR_BUFFER
];
4475 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4476 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4477 json_object_int_add(json
, "ethTag",
4478 p
->prefix
.imet_addr
.eth_tag
);
4479 json_object_int_add(json
, "ipLen",
4480 is_evpn_prefix_ipaddr_v4(p
)
4483 json_object_string_add(json
, "ip",
4484 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4485 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4486 if (is_evpn_prefix_ipaddr_none(p
)) {
4487 json_object_int_add(json
, "routeType",
4488 p
->prefix
.route_type
);
4489 json_object_int_add(json
, "ethTag",
4490 p
->prefix
.macip_addr
.eth_tag
);
4491 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4492 json_object_string_add(json
, "mac",
4493 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4499 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4502 json_object_int_add(json
, "routeType",
4503 p
->prefix
.route_type
);
4504 json_object_int_add(json
, "ethTag",
4505 p
->prefix
.macip_addr
.eth_tag
);
4506 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4507 json_object_string_add(json
, "mac",
4508 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4511 json_object_int_add(json
, "ipLen",
4512 is_evpn_prefix_ipaddr_v4(p
)
4515 json_object_string_add(
4518 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4520 PREFIX2STR_BUFFER
));
4523 /* Currently, this is to cater to other AF_ETHERNET code. */
4528 * Function to convert evpn route to string.
4529 * NOTE: We don't use prefix2str as the output here is a bit different.
4531 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
4533 char buf1
[ETHER_ADDR_STRLEN
];
4534 char buf2
[PREFIX2STR_BUFFER
];
4535 char buf3
[ESI_STR_LEN
];
4537 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4538 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
4539 p
->prefix
.imet_addr
.eth_tag
,
4540 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4542 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4543 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4544 if (is_evpn_prefix_ipaddr_none(p
))
4545 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4546 p
->prefix
.route_type
,
4547 p
->prefix
.macip_addr
.eth_tag
,
4549 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4554 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4556 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
4557 p
->prefix
.route_type
,
4558 p
->prefix
.macip_addr
.eth_tag
,
4560 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4562 family
== AF_INET
? IPV4_MAX_BITLEN
4565 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4567 PREFIX2STR_BUFFER
));
4569 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
4570 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4571 p
->prefix
.route_type
,
4572 p
->prefix
.prefix_addr
.eth_tag
,
4573 p
->prefix
.prefix_addr
.ip_prefix_length
,
4574 is_evpn_prefix_ipaddr_v4(p
)
4575 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
4576 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
4577 } else if (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
4578 snprintf(buf
, len
, "[%d]:[%s]:[%d]:[%s]",
4579 p
->prefix
.route_type
,
4580 esi_to_str(&p
->prefix
.es_addr
.esi
, buf3
, sizeof(buf3
)),
4581 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4583 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
));
4585 /* For EVPN route types not supported yet. */
4586 snprintf(buf
, len
, "(unsupported route type %d)",
4587 p
->prefix
.route_type
);
4594 * Encode EVPN prefix in Update (MP_REACH)
4596 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
4597 struct prefix_rd
*prd
, mpls_label_t
*label
,
4598 uint32_t num_labels
, struct attr
*attr
,
4599 int addpath_encode
, uint32_t addpath_tx_id
)
4601 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4602 int len
, ipa_len
= 0;
4605 stream_putl(s
, addpath_tx_id
);
4608 stream_putc(s
, evp
->prefix
.route_type
);
4610 switch (evp
->prefix
.route_type
) {
4611 case BGP_EVPN_MAC_IP_ROUTE
:
4612 if (is_evpn_prefix_ipaddr_v4(evp
))
4613 ipa_len
= IPV4_MAX_BYTELEN
;
4614 else if (is_evpn_prefix_ipaddr_v6(evp
))
4615 ipa_len
= IPV6_MAX_BYTELEN
;
4616 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4617 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4618 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4620 stream_putc(s
, len
);
4621 stream_put(s
, prd
->val
, 8); /* RD */
4623 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
4625 stream_put(s
, 0, 10);
4626 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4627 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4628 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4629 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4630 if (ipa_len
) /* IP */
4631 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4633 /* 1st label is the L2 VNI */
4634 stream_put(s
, label
, BGP_LABEL_BYTES
);
4635 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4636 if (ipa_len
&& num_labels
> 1)
4637 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4640 case BGP_EVPN_IMET_ROUTE
:
4641 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4642 stream_put(s
, prd
->val
, 8); /* RD */
4643 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4644 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4645 /* Originating Router's IP Addr */
4646 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4649 case BGP_EVPN_ES_ROUTE
:
4650 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4651 stream_put(s
, prd
->val
, 8); /* RD */
4652 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4653 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4655 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4658 case BGP_EVPN_IP_PREFIX_ROUTE
:
4659 /* TODO: AddPath support. */
4660 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4668 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4669 struct bgp_nlri
*packet
, int withdraw
)
4675 uint32_t addpath_id
;
4676 int addpath_encoded
;
4681 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4683 lim
= pnt
+ packet
->length
;
4685 safi
= packet
->safi
;
4689 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
4690 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
4691 PEER_CAP_ADDPATH_AF_TX_RCV
));
4693 for (; pnt
< lim
; pnt
+= psize
) {
4694 /* Clear prefix structure. */
4695 memset(&p
, 0, sizeof(struct prefix
));
4697 /* Deal with path-id if AddPath is supported. */
4698 if (addpath_encoded
) {
4699 /* When packet overflow occurs return immediately. */
4700 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4703 addpath_id
= ntohl(*((uint32_t *)pnt
));
4704 pnt
+= BGP_ADDPATH_ID_LEN
;
4707 /* All EVPN NLRI types start with type and length. */
4714 /* When packet overflow occur return immediately. */
4715 if (pnt
+ psize
> lim
)
4719 case BGP_EVPN_MAC_IP_ROUTE
:
4720 if (process_type2_route(peer
, afi
, safi
,
4721 withdraw
? NULL
: attr
, pnt
,
4722 psize
, addpath_id
)) {
4725 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4726 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4731 case BGP_EVPN_IMET_ROUTE
:
4732 if (process_type3_route(peer
, afi
, safi
,
4733 withdraw
? NULL
: attr
, pnt
,
4734 psize
, addpath_id
)) {
4737 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4738 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4743 case BGP_EVPN_ES_ROUTE
:
4744 if (process_type4_route(peer
, afi
, safi
,
4745 withdraw
? NULL
: attr
, pnt
,
4746 psize
, addpath_id
)) {
4749 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4750 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4755 case BGP_EVPN_IP_PREFIX_ROUTE
:
4756 if (process_type5_route(peer
, afi
, safi
, attr
, pnt
,
4757 psize
, addpath_id
, withdraw
)) {
4760 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4761 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4771 /* Packet length consistency check. */
4779 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
4780 * The mapping will be used during route processing.
4781 * bgp_def: default bgp instance
4782 * bgp_vrf: specific bgp vrf instance on which RT is configured
4784 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
4787 struct ecommunity_val
*eval
= NULL
;
4788 struct listnode
*node
= NULL
, *nnode
= NULL
;
4789 struct ecommunity
*ecom
= NULL
;
4791 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4792 for (i
= 0; i
< ecom
->size
; i
++) {
4793 eval
= (struct ecommunity_val
*)(ecom
->val
4795 * ECOMMUNITY_SIZE
));
4796 map_vrf_to_rt(bgp_vrf
, eval
);
4802 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
4804 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
4807 struct ecommunity_val
*eval
;
4808 struct listnode
*node
, *nnode
;
4809 struct ecommunity
*ecom
;
4811 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4812 for (i
= 0; i
< ecom
->size
; i
++) {
4813 struct vrf_irt_node
*irt
;
4814 struct ecommunity_val eval_tmp
;
4816 eval
= (struct ecommunity_val
*)(ecom
->val
4818 * ECOMMUNITY_SIZE
));
4819 /* If using "automatic" RT, we only care about the
4820 * local-admin sub-field.
4821 * This is to facilitate using VNI as the RT for EBGP
4824 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4825 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
4826 BGP_VRF_IMPORT_RT_CFGD
))
4827 mask_ecom_global_admin(&eval_tmp
, eval
);
4829 irt
= lookup_vrf_import_rt(&eval_tmp
);
4831 unmap_vrf_from_rt(bgp_vrf
, irt
);
4838 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
4839 * The mapping will be used during route processing.
4841 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4844 struct ecommunity_val
*eval
;
4845 struct listnode
*node
, *nnode
;
4846 struct ecommunity
*ecom
;
4848 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4849 for (i
= 0; i
< ecom
->size
; i
++) {
4850 eval
= (struct ecommunity_val
*)(ecom
->val
4852 * ECOMMUNITY_SIZE
));
4853 map_vni_to_rt(bgp
, vpn
, eval
);
4859 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
4861 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4864 struct ecommunity_val
*eval
;
4865 struct listnode
*node
, *nnode
;
4866 struct ecommunity
*ecom
;
4868 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4869 for (i
= 0; i
< ecom
->size
; i
++) {
4870 struct irt_node
*irt
;
4871 struct ecommunity_val eval_tmp
;
4873 eval
= (struct ecommunity_val
*)(ecom
->val
4875 * ECOMMUNITY_SIZE
));
4876 /* If using "automatic" RT, we only care about the
4877 * local-admin sub-field.
4878 * This is to facilitate using VNI as the RT for EBGP
4881 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4882 if (!is_import_rt_configured(vpn
))
4883 mask_ecom_global_admin(&eval_tmp
, eval
);
4885 irt
= lookup_import_rt(bgp
, &eval_tmp
);
4887 unmap_vni_from_rt(bgp
, vpn
, irt
);
4893 * Derive Import RT automatically for VNI and map VNI to RT.
4894 * The mapping will be used during route processing.
4896 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4898 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
4899 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
4902 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
4906 * Derive Export RT automatically for VNI.
4908 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4910 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
4911 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
4915 * Derive RD automatically for VNI using passed information - it
4916 * is of the form RouterId:unique-id-for-vni.
4918 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
4920 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
4924 * Derive RD automatically for VNI using passed information - it
4925 * is of the form RouterId:unique-id-for-vni.
4927 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4931 vpn
->prd
.family
= AF_UNSPEC
;
4932 vpn
->prd
.prefixlen
= 64;
4933 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
4934 (void)str2prefix_rd(buf
, &vpn
->prd
);
4935 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
4941 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
4943 struct list
*inst
= bm
->bgp
;
4944 struct listnode
*node
;
4945 struct bgp
*bgp_vrf
;
4947 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
4948 if (bgp_vrf
->l3vni
== vni
)
4958 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
4960 struct bgpevpn
*vpn
;
4963 memset(&tmp
, 0, sizeof(struct bgpevpn
));
4965 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
4970 * Create a new vpn - invoked upon configuration or zebra notification.
4972 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
4973 struct in_addr originator_ip
,
4974 vrf_id_t tenant_vrf_id
)
4976 struct bgpevpn
*vpn
;
4981 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
4985 /* Set values - RD and RT set to defaults. */
4987 vpn
->originator_ip
= originator_ip
;
4988 vpn
->tenant_vrf_id
= tenant_vrf_id
;
4990 /* Initialize route-target import and export lists */
4991 vpn
->import_rtl
= list_new();
4992 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4993 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
4994 vpn
->export_rtl
= list_new();
4995 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4996 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
4997 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
4998 derive_rd_rt_for_vni(bgp
, vpn
);
5000 /* Initialize EVPN route table. */
5001 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5004 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
5005 XFREE(MTYPE_BGP_EVPN
, vpn
);
5009 /* add to l2vni list on corresponding vrf */
5010 bgpevpn_link_to_l3vni(vpn
);
5012 QOBJ_REG(vpn
, bgpevpn
);
5017 * Free a given VPN - called in multiple scenarios such as zebra
5018 * notification, configuration being deleted, advertise-all-vni disabled etc.
5019 * This just frees appropriate memory, caller should have taken other
5022 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5024 bgpevpn_unlink_from_l3vni(vpn
);
5025 bgp_table_unlock(vpn
->route_table
);
5026 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5027 list_delete(&vpn
->import_rtl
);
5028 list_delete(&vpn
->export_rtl
);
5029 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5030 hash_release(bgp
->vnihash
, vpn
);
5032 XFREE(MTYPE_BGP_EVPN
, vpn
);
5038 struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
)
5043 memset(&tmp
, 0, sizeof(struct evpnes
));
5044 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
5045 es
= hash_lookup(bgp
->esihash
, &tmp
);
5050 * Create a new local es - invoked upon zebra notification.
5052 struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
,
5054 struct ipaddr
*originator_ip
)
5062 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct evpnes
));
5066 /* set the ESI and originator_ip */
5067 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
5068 memcpy(&es
->originator_ip
, originator_ip
, sizeof(struct ipaddr
));
5070 /* Initialise the VTEP list */
5071 es
->vtep_list
= list_new();
5072 es
->vtep_list
->cmp
= evpn_vtep_ip_cmp
;
5074 /* auto derive RD for this es */
5075 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
5076 es
->prd
.family
= AF_UNSPEC
;
5077 es
->prd
.prefixlen
= 64;
5078 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), es
->rd_id
);
5079 (void)str2prefix_rd(buf
, &es
->prd
);
5081 /* Initialize the ES route table */
5082 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5085 if (!hash_get(bgp
->esihash
, es
, hash_alloc_intern
)) {
5086 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5090 QOBJ_REG(es
, evpnes
);
5096 * This just frees appropriate memory, caller should have taken other
5099 void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
)
5101 list_delete(&es
->vtep_list
);
5102 bgp_table_unlock(es
->route_table
);
5103 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
5104 hash_release(bgp
->esihash
, es
);
5106 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5110 * Import evpn route from global table to VNI/VRF/ESI.
5112 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5113 struct prefix
*p
, struct bgp_path_info
*pi
)
5115 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5119 * Unimport evpn route from VNI/VRF/ESI.
5121 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5122 struct prefix
*p
, struct bgp_path_info
*pi
)
5124 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5127 /* filter routes which have martian next hops */
5128 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5132 struct bgp_node
*rd_rn
, *rn
;
5133 struct bgp_table
*table
;
5134 struct bgp_path_info
*pi
;
5139 /* Walk entire global routing table and evaluate routes which could be
5140 * imported into this VPN. Note that we cannot just look at the routes
5141 * for the VNI's RD -
5142 * remote routes applicable for this VNI could have any RD.
5144 /* EVPN routes are a 2-level table. */
5145 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
5146 rd_rn
= bgp_route_next(rd_rn
)) {
5147 table
= (struct bgp_table
*)(rd_rn
->info
);
5151 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5153 for (pi
= rn
->info
; pi
; pi
= pi
->next
) {
5155 /* Consider "valid" remote routes applicable for
5157 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5158 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5161 if (bgp_nexthop_self(bgp
, pi
->attr
->nexthop
)) {
5163 char attr_str
[BUFSIZ
];
5164 char pbuf
[PREFIX_STRLEN
];
5166 bgp_dump_attr(pi
->attr
, attr_str
,
5169 if (bgp_debug_update(pi
->peer
, &rn
->p
,
5172 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
5179 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5182 bgp_rib_remove(rn
, pi
, pi
->peer
, afi
,
5193 * Handle del of a local MACIP.
5195 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5198 struct bgpevpn
*vpn
;
5199 struct prefix_evpn p
;
5201 /* Lookup VNI hash - should exist. */
5202 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5203 if (!vpn
|| !is_vni_live(vpn
)) {
5204 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5205 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5206 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5210 /* Remove EVPN type-2 route and schedule for processing. */
5211 build_evpn_type2_prefix(&p
, mac
, ip
);
5212 delete_evpn_route(bgp
, vpn
, &p
);
5218 * Handle add of a local MACIP.
5220 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5221 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
)
5223 struct bgpevpn
*vpn
;
5224 struct prefix_evpn p
;
5226 /* Lookup VNI hash - should exist. */
5227 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5228 if (!vpn
|| !is_vni_live(vpn
)) {
5229 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5230 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5231 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5235 /* Create EVPN type-2 route and schedule for processing. */
5236 build_evpn_type2_prefix(&p
, mac
, ip
);
5237 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
)) {
5238 char buf
[ETHER_ADDR_STRLEN
];
5239 char buf2
[INET6_ADDRSTRLEN
];
5242 EC_BGP_EVPN_ROUTE_CREATE
,
5243 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5244 bgp
->vrf_id
, vpn
->vni
,
5245 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5248 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5249 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5256 static void link_l2vni_hash_to_l3vni(struct hash_backet
*backet
,
5257 struct bgp
*bgp_vrf
)
5259 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
5260 struct bgp
*bgp_def
= NULL
;
5262 bgp_def
= bgp_get_default();
5265 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5266 bgpevpn_link_to_l3vni(vpn
);
5269 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
, struct ethaddr
*rmac
,
5270 struct in_addr originator_ip
, int filter
)
5272 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5273 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5274 struct listnode
*node
= NULL
;
5275 struct bgpevpn
*vpn
= NULL
;
5278 /* get the default instance - required to get the AS number for VRF
5281 bgp_def
= bgp_get_default();
5285 "Cannot process L3VNI %u ADD - default BGP instance not yet created",
5291 /* if the BGP vrf instance doesn't exist - create one */
5292 bgp_vrf
= bgp_lookup_by_name(vrf_id_to_name(vrf_id
));
5297 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5298 BGP_INSTANCE_TYPE_VRF
);
5300 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
:
5301 flog_err(EC_BGP_MULTI_INSTANCE
,
5302 "'bgp multiple-instance' not present\n");
5304 case BGP_ERR_AS_MISMATCH
:
5305 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5306 "BGP is already running; AS is %u\n", as
);
5308 case BGP_ERR_INSTANCE_MISMATCH
:
5309 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5310 "BGP instance name and AS number mismatch\n");
5314 /* mark as auto created */
5315 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5318 /* associate with l3vni */
5319 bgp_vrf
->l3vni
= l3vni
;
5321 /* set the router mac - to be used in mac-ip routes for this vrf */
5322 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
5324 /* set the originator ip */
5325 bgp_vrf
->originator_ip
= originator_ip
;
5327 /* set the right filter - are we using l3vni only for prefix routes? */
5329 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5331 /* auto derive RD/RT */
5332 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5333 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5334 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5335 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5336 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5338 /* link all corresponding l2vnis */
5339 hash_iterate(bgp_def
->vnihash
,
5340 (void (*)(struct hash_backet
*,
5341 void *))link_l2vni_hash_to_l3vni
,
5344 /* Only update all corresponding type-2 routes if we are advertising two
5345 * labels along with type-2 routes
5348 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5349 update_routes_for_vni(bgp_def
, vpn
);
5351 /* advertise type-5 routes if needed */
5352 update_advertise_vrf_routes(bgp_vrf
);
5354 /* install all remote routes belonging to this l3vni into correspondng
5356 install_routes_for_vrf(bgp_vrf
);
5361 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5363 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5364 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5365 struct listnode
*node
= NULL
;
5366 struct listnode
*next
= NULL
;
5367 struct bgpevpn
*vpn
= NULL
;
5369 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5373 "Cannot process L3VNI %u Del - Could not find BGP instance",
5378 bgp_def
= bgp_get_default();
5382 "Cannot process L3VNI %u Del - Could not find default BGP instance",
5387 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5388 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5389 * routes. This will uninstalling the routes from zebra and decremnt the
5392 uninstall_routes_for_vrf(bgp_vrf
);
5394 /* delete/withdraw all type-5 routes */
5395 delete_withdraw_vrf_routes(bgp_vrf
);
5397 /* remove the l3vni from vrf instance */
5400 /* remove the Rmac from the BGP vrf */
5401 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5404 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5405 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5406 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5408 if (!list_isempty(bgp_vrf
->vrf_export_rtl
)) {
5409 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5412 /* update all corresponding local mac-ip routes */
5413 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5414 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5415 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5416 update_routes_for_vni(bgp_def
, vpn
);
5420 /* If any L2VNIs point to this instance, unlink them. */
5421 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5422 bgpevpn_unlink_from_l3vni(vpn
);
5424 /* Delete the instance if it was autocreated */
5425 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5426 bgp_delete(bgp_vrf
);
5432 * Handle del of a local VNI.
5434 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5436 struct bgpevpn
*vpn
;
5438 /* Locate VNI hash */
5439 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5441 if (bgp_debug_zebra(NULL
))
5443 EC_BGP_EVPN_VPN_VNI
,
5444 "%u: VNI hash entry for VNI %u not found at DEL",
5449 /* Remove all local EVPN routes and schedule for processing (to
5450 * withdraw from peers).
5452 delete_routes_for_vni(bgp
, vpn
);
5455 * tunnel is no longer active, del tunnel ip address from tip_hash
5457 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5459 /* Clear "live" flag and see if hash needs to be freed. */
5460 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5461 if (!is_vni_configured(vpn
))
5462 bgp_evpn_free(bgp
, vpn
);
5468 * Handle add (or update) of a local VNI. The VNI changes we care
5469 * about are for the local-tunnel-ip and the (tenant) VRF.
5471 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5472 struct in_addr originator_ip
, vrf_id_t tenant_vrf_id
)
5474 struct bgpevpn
*vpn
;
5475 struct prefix_evpn p
;
5477 /* Lookup VNI. If present and no change, exit. */
5478 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5481 if (is_vni_live(vpn
)
5482 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5483 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5484 /* Probably some other param has changed that we don't
5488 /* Update tenant_vrf_id if it has changed. */
5489 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5490 bgpevpn_unlink_from_l3vni(vpn
);
5491 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5492 bgpevpn_link_to_l3vni(vpn
);
5495 /* If tunnel endpoint IP has changed, update (and delete prior
5496 * type-3 route, if needed.)
5498 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5499 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5501 /* Update all routes with new endpoint IP and/or export RT
5504 if (is_vni_live(vpn
))
5505 update_routes_for_vni(bgp
, vpn
);
5508 /* Create or update as appropriate. */
5510 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
);
5514 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5520 /* if the VNI is live already, there is nothing more to do */
5521 if (is_vni_live(vpn
))
5524 /* Mark as "live" */
5525 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5527 /* tunnel is now active, add tunnel-ip to db */
5528 bgp_tip_add(bgp
, &originator_ip
);
5530 /* filter routes as nexthop database has changed */
5531 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5534 * Create EVPN type-3 route and schedule for processing.
5536 * RT-3 only if doing head-end replication
5538 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
5539 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5540 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0)) {
5541 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5542 "%u: Type3 route creation failure for VNI %u",
5548 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5552 install_routes_for_vni(bgp
, vpn
);
5554 /* If we are advertising gateway mac-ip
5555 It needs to be conveyed again to zebra */
5556 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5562 * bgp_evpn_local_es_del
5564 int bgp_evpn_local_es_del(struct bgp
*bgp
,
5566 struct ipaddr
*originator_ip
)
5568 char buf
[ESI_STR_LEN
];
5569 struct evpnes
*es
= NULL
;
5571 if (!bgp
->esihash
) {
5572 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5577 /* Lookup ESI hash - should exist. */
5578 es
= bgp_evpn_lookup_es(bgp
, esi
);
5580 flog_warn(EC_BGP_EVPN_ESI
,
5581 "%u: ESI hash entry for ESI %s at Local ES DEL",
5582 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5586 /* Delete all local EVPN ES routes from ESI table
5587 * and schedule for processing (to withdraw from peers))
5589 delete_routes_for_es(bgp
, es
);
5591 /* free the hash entry */
5592 bgp_evpn_es_free(bgp
, es
);
5598 * bgp_evpn_local_es_add
5600 int bgp_evpn_local_es_add(struct bgp
*bgp
,
5602 struct ipaddr
*originator_ip
)
5604 char buf
[ESI_STR_LEN
];
5605 struct evpnes
*es
= NULL
;
5606 struct prefix_evpn p
;
5608 if (!bgp
->esihash
) {
5609 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5614 /* create the new es */
5615 es
= bgp_evpn_lookup_es(bgp
, esi
);
5617 es
= bgp_evpn_es_new(bgp
, esi
, originator_ip
);
5621 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
5622 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5626 UNSET_FLAG(es
->flags
, EVPNES_REMOTE
);
5627 SET_FLAG(es
->flags
, EVPNES_LOCAL
);
5629 build_evpn_type4_prefix(&p
, esi
, originator_ip
->ipaddr_v4
);
5630 if (update_evpn_type4_route(bgp
, es
, &p
)) {
5631 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5632 "%u: Type4 route creation failure for ESI %s",
5633 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5637 /* import all remote ES routes in th ES table */
5638 install_routes_for_es(bgp
, es
);
5644 * Handle change in setting for BUM handling. The supported values
5645 * are head-end replication and dropping all BUM packets. Any change
5646 * should be registered with zebra. Also, if doing head-end replication,
5647 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5648 * to be dropped, the RT-3s must be withdrawn.
5650 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
5652 zlog_info("L2VPN EVPN BUM handling is %s",
5653 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
5654 "Flooding" : "Flooding Disabled");
5656 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
5657 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
5658 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
5659 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
5660 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
5664 * Cleanup EVPN information on disable - Need to delete and withdraw
5665 * EVPN routes from peers.
5667 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5669 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
5670 void *))cleanup_vni_on_disable
,
5675 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5676 * BGP instance (default) is being freed.
5678 void bgp_evpn_cleanup(struct bgp
*bgp
)
5680 hash_iterate(bgp
->vnihash
,
5681 (void (*)(struct hash_backet
*, void *))free_vni_entry
,
5684 hash_free(bgp
->import_rt_hash
);
5685 bgp
->import_rt_hash
= NULL
;
5687 hash_free(bgp
->vrf_import_rt_hash
);
5688 bgp
->vrf_import_rt_hash
= NULL
;
5690 hash_free(bgp
->vnihash
);
5691 bgp
->vnihash
= NULL
;
5693 hash_free(bgp
->esihash
);
5694 bgp
->esihash
= NULL
;
5696 list_delete(&bgp
->vrf_import_rtl
);
5697 list_delete(&bgp
->vrf_export_rtl
);
5698 list_delete(&bgp
->l2vnis
);
5702 * Initialization for EVPN
5705 * hash for RT to VNI
5707 void bgp_evpn_init(struct bgp
*bgp
)
5710 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
5712 hash_create(esi_hash_keymake
, esi_cmp
,
5713 "BGP EVPN Local ESI Hash");
5714 bgp
->import_rt_hash
=
5715 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
5716 "BGP Import RT Hash");
5717 bgp
->vrf_import_rt_hash
=
5718 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
5719 "BGP VRF Import RT Hash");
5720 bgp
->vrf_import_rtl
= list_new();
5721 bgp
->vrf_import_rtl
->cmp
=
5722 (int (*)(void *, void *))evpn_route_target_cmp
;
5723 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
5724 bgp
->vrf_export_rtl
= list_new();
5725 bgp
->vrf_export_rtl
->cmp
=
5726 (int (*)(void *, void *))evpn_route_target_cmp
;
5727 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
5728 bgp
->l2vnis
= list_new();
5729 bgp
->l2vnis
->cmp
= vni_list_cmp
;
5731 /* Default BUM handling is to do head-end replication. */
5732 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
5735 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
5737 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);