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 "lib/printfrr.h"
36 #include "bgpd/bgp_attr_evpn.h"
37 #include "bgpd/bgpd.h"
38 #include "bgpd/bgp_table.h"
39 #include "bgpd/bgp_route.h"
40 #include "bgpd/bgp_attr.h"
41 #include "bgpd/bgp_mplsvpn.h"
42 #include "bgpd/bgp_label.h"
43 #include "bgpd/bgp_evpn.h"
44 #include "bgpd/bgp_evpn_private.h"
45 #include "bgpd/bgp_evpn_mh.h"
46 #include "bgpd/bgp_ecommunity.h"
47 #include "bgpd/bgp_encap_types.h"
48 #include "bgpd/bgp_debug.h"
49 #include "bgpd/bgp_errors.h"
50 #include "bgpd/bgp_aspath.h"
51 #include "bgpd/bgp_zebra.h"
52 #include "bgpd/bgp_nexthop.h"
53 #include "bgpd/bgp_addpath.h"
54 #include "bgpd/bgp_mac.h"
55 #include "bgpd/bgp_vty.h"
58 * Definitions and external declarations.
60 DEFINE_QOBJ_TYPE(bgpevpn
);
61 DEFINE_QOBJ_TYPE(bgp_evpn_es
);
65 * Static function declarations
67 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
68 static struct in_addr zero_vtep_ip
;
77 static unsigned int vni_hash_key_make(const void *p
)
79 const struct bgpevpn
*vpn
= p
;
80 return (jhash_1word(vpn
->vni
, 0));
84 * Comparison function for vni hash
86 static bool vni_hash_cmp(const void *p1
, const void *p2
)
88 const struct bgpevpn
*vpn1
= p1
;
89 const struct bgpevpn
*vpn2
= p2
;
95 return (vpn1
->vni
== vpn2
->vni
);
98 int vni_list_cmp(void *p1
, void *p2
)
100 const struct bgpevpn
*vpn1
= p1
;
101 const struct bgpevpn
*vpn2
= p2
;
103 return vpn1
->vni
- vpn2
->vni
;
107 * Make vrf import route target hash key.
109 static unsigned int vrf_import_rt_hash_key_make(const void *p
)
111 const struct vrf_irt_node
*irt
= p
;
112 const char *pnt
= irt
->rt
.val
;
114 return jhash(pnt
, 8, 0x5abc1234);
118 * Comparison function for vrf import rt hash
120 static bool vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
122 const struct vrf_irt_node
*irt1
= p1
;
123 const struct vrf_irt_node
*irt2
= p2
;
125 if (irt1
== NULL
&& irt2
== NULL
)
128 if (irt1
== NULL
|| irt2
== NULL
)
131 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
135 * Create a new vrf import_rt in evpn instance
137 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
139 struct bgp
*bgp_evpn
= NULL
;
140 struct vrf_irt_node
*irt
;
142 bgp_evpn
= bgp_get_evpn();
144 flog_err(EC_BGP_NO_DFLT
,
145 "vrf import rt new - evpn instance not created yet");
149 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
150 sizeof(struct vrf_irt_node
));
153 irt
->vrfs
= list_new();
156 if (!hash_get(bgp_evpn
->vrf_import_rt_hash
, irt
, hash_alloc_intern
)) {
157 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
165 * Free the vrf import rt node
167 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
169 struct bgp
*bgp_evpn
= NULL
;
171 bgp_evpn
= bgp_get_evpn();
173 flog_err(EC_BGP_NO_DFLT
,
174 "vrf import rt free - evpn instance not created yet");
178 hash_release(bgp_evpn
->vrf_import_rt_hash
, irt
);
179 list_delete(&irt
->vrfs
);
180 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
184 * Function to lookup Import RT node - used to map a RT to set of
185 * VNIs importing routes with that RT.
187 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
189 struct bgp
*bgp_evpn
= NULL
;
190 struct vrf_irt_node
*irt
;
191 struct vrf_irt_node tmp
;
193 bgp_evpn
= bgp_get_evpn();
197 "vrf import rt lookup - evpn instance not created yet");
201 memset(&tmp
, 0, sizeof(struct vrf_irt_node
));
202 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
203 irt
= hash_lookup(bgp_evpn
->vrf_import_rt_hash
, &tmp
);
208 * Is specified VRF present on the RT's list of "importing" VRFs?
210 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
, struct bgp
*bgp_vrf
)
212 struct listnode
*node
= NULL
, *nnode
= NULL
;
213 struct bgp
*tmp_bgp_vrf
= NULL
;
215 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
216 if (tmp_bgp_vrf
== bgp_vrf
)
223 * Make import route target hash key.
225 static unsigned int import_rt_hash_key_make(const void *p
)
227 const struct irt_node
*irt
= p
;
228 const char *pnt
= irt
->rt
.val
;
230 return jhash(pnt
, 8, 0xdeadbeef);
234 * Comparison function for import rt hash
236 static bool import_rt_hash_cmp(const void *p1
, const void *p2
)
238 const struct irt_node
*irt1
= p1
;
239 const struct irt_node
*irt2
= p2
;
241 if (irt1
== NULL
&& irt2
== NULL
)
244 if (irt1
== NULL
|| irt2
== NULL
)
247 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
251 * Create a new import_rt
253 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
254 struct ecommunity_val
*rt
)
256 struct irt_node
*irt
;
261 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
264 irt
->vnis
= list_new();
267 if (!hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
)) {
268 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
276 * Free the import rt node
278 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
280 hash_release(bgp
->import_rt_hash
, irt
);
281 list_delete(&irt
->vnis
);
282 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
286 * Function to lookup Import RT node - used to map a RT to set of
287 * VNIs importing routes with that RT.
289 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
290 struct ecommunity_val
*rt
)
292 struct irt_node
*irt
;
295 memset(&tmp
, 0, sizeof(struct irt_node
));
296 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
297 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
302 * Is specified VNI present on the RT's list of "importing" VNIs?
304 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
306 struct listnode
*node
, *nnode
;
307 struct bgpevpn
*tmp_vpn
;
309 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
318 * Compare Route Targets.
320 static int evpn_route_target_cmp(struct ecommunity
*ecom1
,
321 struct ecommunity
*ecom2
)
329 if (!ecom1
&& !ecom2
)
332 if (ecom1
->str
&& !ecom2
->str
)
335 if (!ecom1
->str
&& ecom2
->str
)
338 if (!ecom1
->str
&& !ecom2
->str
)
341 return strcmp(ecom1
->str
, ecom2
->str
);
344 static void evpn_xxport_delete_ecomm(void *val
)
346 struct ecommunity
*ecomm
= val
;
347 ecommunity_free(&ecomm
);
351 * Mask off global-admin field of specified extended community (RT),
352 * just retain the local-admin field.
354 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
355 struct ecommunity_val
*src
)
361 if (type
== ECOMMUNITY_ENCODE_AS
) {
362 dst
->val
[2] = dst
->val
[3] = 0;
363 } else if (type
== ECOMMUNITY_ENCODE_AS4
364 || type
== ECOMMUNITY_ENCODE_IP
) {
365 dst
->val
[2] = dst
->val
[3] = 0;
366 dst
->val
[4] = dst
->val
[5] = 0;
371 * Map one RT to specified VRF.
372 * bgp_vrf = BGP vrf instance
374 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct ecommunity_val
*eval
)
376 struct vrf_irt_node
*irt
= NULL
;
377 struct ecommunity_val eval_tmp
;
379 /* If using "automatic" RT,
380 * we only care about the local-admin sub-field.
381 * This is to facilitate using L3VNI(VRF-VNI)
382 * as the RT for EBGP peering too.
384 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
385 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
386 mask_ecom_global_admin(&eval_tmp
, eval
);
388 irt
= lookup_vrf_import_rt(&eval_tmp
);
389 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
390 /* Already mapped. */
394 irt
= vrf_import_rt_new(&eval_tmp
);
396 /* Add VRF to the list for this RT. */
397 listnode_add(irt
->vrfs
, bgp_vrf
);
401 * Unmap specified VRF from specified RT. If there are no other
402 * VRFs for this RT, then the RT hash is deleted.
403 * bgp_vrf: BGP VRF specific instance
405 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
, struct vrf_irt_node
*irt
)
407 /* Delete VRF from list for this RT. */
408 listnode_delete(irt
->vrfs
, bgp_vrf
);
409 if (!listnode_head(irt
->vrfs
)) {
410 vrf_import_rt_free(irt
);
415 * Map one RT to specified VNI.
417 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
418 struct ecommunity_val
*eval
)
420 struct irt_node
*irt
;
421 struct ecommunity_val eval_tmp
;
423 /* If using "automatic" RT, we only care about the local-admin
425 * This is to facilitate using VNI as the RT for EBGP peering too.
427 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
428 if (!is_import_rt_configured(vpn
))
429 mask_ecom_global_admin(&eval_tmp
, eval
);
431 irt
= lookup_import_rt(bgp
, &eval_tmp
);
433 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
434 /* Already mapped. */
438 irt
= import_rt_new(bgp
, &eval_tmp
);
442 /* Add VNI to the hash list for this RT. */
443 listnode_add(irt
->vnis
, vpn
);
447 * Unmap specified VNI from specified RT. If there are no other
448 * VNIs for this RT, then the RT hash is deleted.
450 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
451 struct irt_node
*irt
)
453 /* Delete VNI from hash list for this RT. */
454 listnode_delete(irt
->vnis
, vpn
);
455 if (!listnode_head(irt
->vnis
)) {
456 import_rt_free(bgp
, irt
);
460 static void bgp_evpn_get_rmac_nexthop(struct bgpevpn
*vpn
,
461 const struct prefix_evpn
*p
,
462 struct attr
*attr
, uint8_t flags
)
464 struct bgp
*bgp_vrf
= vpn
->bgp_vrf
;
466 memset(&attr
->rmac
, 0, sizeof(struct ethaddr
));
470 if (p
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
473 /* Copy sys (pip) RMAC and PIP IP as nexthop
474 * in case of route is self MAC-IP,
475 * advertise-pip and advertise-svi-ip features
477 * Otherwise, for all host MAC-IP route's
480 if (CHECK_FLAG(flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
)
481 && bgp_vrf
->evpn_info
->advertise_pip
&&
482 bgp_vrf
->evpn_info
->is_anycast_mac
) {
484 memcpy(&attr
->rmac
, &bgp_vrf
->evpn_info
->pip_rmac
,
486 attr
->nexthop
= bgp_vrf
->evpn_info
->pip_ip
;
487 attr
->mp_nexthop_global_in
=
488 bgp_vrf
->evpn_info
->pip_ip
;
490 memcpy(&attr
->rmac
, &bgp_vrf
->rmac
, ETH_ALEN
);
494 * Create RT extended community automatically from passed information:
495 * of the form AS:VNI.
496 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
497 * the need is to get a RT value that will be unique across different
498 * VNIs but the same across routers (in the same AS) for a particular
501 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
503 struct ecommunity_val eval
;
504 struct ecommunity
*ecomadd
, *ecom
;
505 bool ecom_found
= false;
506 struct listnode
*node
;
508 if (bgp
->advertise_autort_rfc8365
)
509 vni
|= EVPN_AUTORT_VXLAN
;
510 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
512 ecomadd
= ecommunity_new();
513 ecommunity_add_val(ecomadd
, &eval
, false, false);
514 for (ALL_LIST_ELEMENTS_RO(rtl
, node
, ecom
))
515 if (ecommunity_cmp(ecomadd
, ecom
))
519 listnode_add_sort(rtl
, ecomadd
);
521 ecommunity_free(&ecomadd
);
525 * Derive RD and RT for a VNI automatically. Invoked at the time of
528 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
530 bgp_evpn_derive_auto_rd(bgp
, vpn
);
531 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
532 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
536 * Convert nexthop (remote VTEP IP) into an IPv6 address.
538 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
540 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
542 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
543 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
546 struct bgp_dest
*bgp_global_evpn_node_get(struct bgp_table
*table
, afi_t afi
,
548 const struct prefix_evpn
*evp
,
549 struct prefix_rd
*prd
)
551 struct prefix_evpn global_p
;
553 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
) {
554 /* prefix in the global table doesn't include the VTEP-IP so
555 * we need to create a different copy of the prefix
557 evpn_type1_prefix_global_copy(&global_p
, evp
);
560 return bgp_afi_node_get(table
, afi
, safi
, (struct prefix
*)evp
, prd
);
563 struct bgp_dest
*bgp_global_evpn_node_lookup(struct bgp_table
*table
, afi_t afi
,
565 const struct prefix_evpn
*evp
,
566 struct prefix_rd
*prd
)
568 struct prefix_evpn global_p
;
570 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
) {
571 /* prefix in the global table doesn't include the VTEP-IP so
572 * we need to create a different copy of the prefix
574 evpn_type1_prefix_global_copy(&global_p
, evp
);
577 return bgp_afi_node_lookup(table
, afi
, safi
, (struct prefix
*)evp
, prd
);
581 * Add (update) or delete MACIP from zebra.
583 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
584 const struct prefix_evpn
*p
,
585 struct in_addr remote_vtep_ip
, int add
,
586 uint8_t flags
, uint32_t seq
, esi_t
*esi
)
590 static struct in_addr zero_remote_vtep_ip
;
593 if (!zclient
|| zclient
->sock
< 0)
596 /* Don't try to register if Zebra doesn't know of this instance. */
597 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
598 if (BGP_DEBUG(zebra
, ZEBRA
))
600 "%s: No zebra instance to talk to, not installing remote macip",
610 zclient_create_header(
611 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
613 stream_putl(s
, vpn
->vni
);
614 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
615 /* IP address length and IP address, if any. */
616 if (is_evpn_prefix_ipaddr_none(p
))
619 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
621 stream_putw(s
, ipa_len
);
622 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
624 /* If the ESI is valid that becomes the nexthop; tape out the
625 * VTEP-IP for that case
627 if (bgp_evpn_is_esi_valid(esi
))
628 stream_put_in_addr(s
, &zero_remote_vtep_ip
);
630 stream_put_in_addr(s
, &remote_vtep_ip
);
632 /* TX flags - MAC sticky status and/or gateway mac */
633 /* Also TX the sequence number of the best route. */
635 stream_putc(s
, flags
);
637 stream_put(s
, esi
, sizeof(esi_t
));
640 stream_putw_at(s
, 0, stream_get_endp(s
));
642 if (bgp_debug_zebra(NULL
))
644 "Tx %s MACIP, VNI %u MAC %pEA IP %pIA flags 0x%x seq %u remote VTEP %pI4",
645 add
? "ADD" : "DEL", vpn
->vni
,
646 &p
->prefix
.macip_addr
.mac
, &p
->prefix
.macip_addr
.ip
,
647 flags
, seq
, &remote_vtep_ip
);
649 return zclient_send_message(zclient
);
653 * Add (update) or delete remote VTEP from zebra.
655 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
656 const struct prefix_evpn
*p
,
657 int flood_control
, int add
)
662 if (!zclient
|| zclient
->sock
< 0)
665 /* Don't try to register if Zebra doesn't know of this instance. */
666 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
667 if (BGP_DEBUG(zebra
, ZEBRA
))
669 "%s: No zebra instance to talk to, not installing remote vtep",
677 zclient_create_header(
678 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
680 stream_putl(s
, vpn
->vni
);
681 if (is_evpn_prefix_ipaddr_v4(p
))
682 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
683 else if (is_evpn_prefix_ipaddr_v6(p
)) {
686 "Bad remote IP when trying to %s remote VTEP for VNI %u",
687 add
? "ADD" : "DEL", vpn
->vni
);
690 stream_putl(s
, flood_control
);
692 stream_putw_at(s
, 0, stream_get_endp(s
));
694 if (bgp_debug_zebra(NULL
))
695 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %pI4",
696 add
? "ADD" : "DEL", vpn
->vni
,
697 &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
699 return zclient_send_message(zclient
);
703 * Build extended communities for EVPN prefix route.
705 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
708 struct ecommunity ecom_encap
;
709 struct ecommunity_val eval
;
710 struct ecommunity_val eval_rmac
;
711 bgp_encap_types tnl_type
;
712 struct listnode
*node
, *nnode
;
713 struct ecommunity
*ecom
;
714 struct ecommunity
*old_ecom
;
715 struct list
*vrf_export_rtl
= NULL
;
718 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
719 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
720 encode_encap_extcomm(tnl_type
, &eval
);
722 ecom_encap
.unit_size
= ECOMMUNITY_SIZE
;
723 ecom_encap
.val
= (uint8_t *)eval
.val
;
726 if (attr
->ecommunity
) {
727 old_ecom
= attr
->ecommunity
;
728 ecom
= ecommunity_merge(ecommunity_dup(old_ecom
), &ecom_encap
);
729 if (!old_ecom
->refcnt
)
730 ecommunity_free(&old_ecom
);
732 ecom
= ecommunity_dup(&ecom_encap
);
733 attr
->ecommunity
= ecom
;
734 attr
->encap_tunneltype
= tnl_type
;
736 /* Add the export RTs for L3VNI/VRF */
737 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
738 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
740 ecommunity_merge(attr
->ecommunity
, ecom
);
742 /* add the router mac extended community */
743 if (!is_zero_mac(&attr
->rmac
)) {
744 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
745 ecommunity_add_val(attr
->ecommunity
, &eval_rmac
, true, true);
748 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
752 * Build extended communities for EVPN route.
753 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
754 * and ENCAP extended communities are applicable for all routes.
755 * The default gateway extended community and MAC mobility (sticky) extended
756 * community are added as needed based on passed settings - only for type-2
757 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
758 * added, if present, based on passed settings - only for non-link-local
761 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
764 struct ecommunity ecom_encap
;
765 struct ecommunity ecom_sticky
;
766 struct ecommunity ecom_default_gw
;
767 struct ecommunity ecom_na
;
768 struct ecommunity_val eval
;
769 struct ecommunity_val eval_sticky
;
770 struct ecommunity_val eval_default_gw
;
771 struct ecommunity_val eval_rmac
;
772 struct ecommunity_val eval_na
;
775 bgp_encap_types tnl_type
;
776 struct listnode
*node
, *nnode
;
777 struct ecommunity
*ecom
;
779 struct list
*vrf_export_rtl
= NULL
;
782 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
783 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
784 encode_encap_extcomm(tnl_type
, &eval
);
786 ecom_encap
.unit_size
= ECOMMUNITY_SIZE
;
787 ecom_encap
.val
= (uint8_t *)eval
.val
;
790 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
791 attr
->encap_tunneltype
= tnl_type
;
793 /* Add the export RTs for L2VNI */
794 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
795 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
797 /* Add the export RTs for L3VNI if told to - caller determines
798 * when this should be done.
801 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
802 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
803 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
805 attr
->ecommunity
= ecommunity_merge(
806 attr
->ecommunity
, ecom
);
810 /* Add MAC mobility (sticky) if needed. */
813 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
814 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
815 ecom_sticky
.size
= 1;
816 ecom_sticky
.unit_size
= ECOMMUNITY_SIZE
;
817 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
819 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
822 /* Add RMAC, if told to. */
824 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
825 ecommunity_add_val(attr
->ecommunity
, &eval_rmac
, true, true);
828 /* Add default gateway, if needed. */
829 if (attr
->default_gw
) {
830 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
831 encode_default_gw_extcomm(&eval_default_gw
);
832 ecom_default_gw
.size
= 1;
833 ecom_default_gw
.unit_size
= ECOMMUNITY_SIZE
;
834 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
836 ecommunity_merge(attr
->ecommunity
, &ecom_default_gw
);
839 proxy
= !!(attr
->es_flags
& ATTR_ES_PROXY_ADVERT
);
840 if (attr
->router_flag
|| proxy
) {
841 memset(&ecom_na
, 0, sizeof(ecom_na
));
842 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
, proxy
);
844 ecom_na
.unit_size
= ECOMMUNITY_SIZE
;
845 ecom_na
.val
= (uint8_t *)eval_na
.val
;
846 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
850 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
854 * Add MAC mobility extended community to attribute.
856 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
858 struct ecommunity ecom_tmp
;
859 struct ecommunity_val eval
;
860 uint8_t *ecom_val_ptr
;
867 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
869 /* Find current MM ecommunity */
872 if (attr
->ecommunity
) {
873 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
874 pnt
= attr
->ecommunity
->val
+
875 (i
* attr
->ecommunity
->unit_size
);
879 if (type
== ECOMMUNITY_ENCODE_EVPN
881 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
883 (attr
->ecommunity
->val
+
884 (i
* attr
->ecommunity
->unit_size
));
890 /* Update the existing MM ecommunity */
892 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char)
893 * attr
->ecommunity
->unit_size
);
895 /* Add MM to existing */
897 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
899 ecom_tmp
.unit_size
= ECOMMUNITY_SIZE
;
900 ecom_tmp
.val
= (uint8_t *)eval
.val
;
902 if (attr
->ecommunity
)
904 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
906 attr
->ecommunity
= ecommunity_dup(&ecom_tmp
);
910 /* Install EVPN route into zebra. */
911 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
912 const struct prefix_evpn
*p
,
913 struct bgp_path_info
*pi
)
920 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
923 if (pi
->sub_type
== BGP_ROUTE_IMPORTED
) {
924 if (pi
->attr
->sticky
)
925 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
926 if (pi
->attr
->default_gw
)
927 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
928 if (is_evpn_prefix_ipaddr_v6(p
) &&
929 pi
->attr
->router_flag
)
930 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
932 seq
= mac_mobility_seqnum(pi
->attr
);
933 /* if local ES notify zebra that this is a sync path */
934 if (bgp_evpn_attr_is_local_es(pi
->attr
)) {
935 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_SYNC_PATH
);
936 if (bgp_evpn_attr_is_proxy(pi
->attr
))
938 ZEBRA_MACIP_TYPE_PROXY_ADVERT
);
941 if (!bgp_evpn_attr_is_sync(pi
->attr
))
944 /* if a local path is being turned around and sent
945 * to zebra it is because it is a sync path on
948 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_SYNC_PATH
);
949 /* supply the highest peer seq number to zebra
952 seq
= bgp_evpn_attr_get_sync_seq(pi
->attr
);
953 /* if any of the paths from the peer have the ROUTER
954 * flag set install the local entry as a router entry
956 if (is_evpn_prefix_ipaddr_v6(p
) &&
957 (pi
->attr
->es_flags
&
958 ATTR_ES_PEER_ROUTER
))
960 ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
962 if (!(pi
->attr
->es_flags
& ATTR_ES_PEER_ACTIVE
))
964 ZEBRA_MACIP_TYPE_PROXY_ADVERT
);
967 ret
= bgp_zebra_send_remote_macip(
968 bgp
, vpn
, p
, pi
->attr
->nexthop
, 1, flags
,
969 seq
, bgp_evpn_attr_get_esi(pi
->attr
));
970 } else if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
) {
971 ret
= bgp_evpn_remote_es_evi_add(bgp
, vpn
, p
);
973 switch (bgp_attr_get_pmsi_tnl_type(pi
->attr
)) {
974 case PMSI_TNLTYPE_INGR_REPL
:
975 flood_control
= VXLAN_FLOOD_HEAD_END_REPL
;
978 case PMSI_TNLTYPE_PIM_SM
:
979 flood_control
= VXLAN_FLOOD_PIM_SM
;
983 flood_control
= VXLAN_FLOOD_DISABLED
;
986 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, flood_control
, 1);
992 /* Uninstall EVPN route from zebra. */
993 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
994 const struct prefix_evpn
*p
,
995 struct in_addr remote_vtep_ip
)
999 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1000 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
1002 else if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
1003 ret
= bgp_evpn_remote_es_evi_del(bgp
, vpn
, p
);
1005 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
,
1006 VXLAN_FLOOD_DISABLED
, 0);
1012 * Due to MAC mobility, the prior "local" best route has been supplanted
1013 * by a "remote" best route. The prior route has to be deleted and withdrawn
1016 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1017 struct bgp_dest
*dest
,
1018 struct bgp_path_info
*old_local
,
1019 struct bgp_path_info
*new_select
)
1021 struct bgp_dest
*global_dest
;
1022 struct bgp_path_info
*pi
;
1023 afi_t afi
= AFI_L2VPN
;
1024 safi_t safi
= SAFI_EVPN
;
1026 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
1027 char esi_buf
[ESI_STR_LEN
];
1028 char esi_buf2
[ESI_STR_LEN
];
1029 struct prefix_evpn
*evp
=
1030 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1032 zlog_debug("local path deleted %pFX es %s; new-path-es %s", evp
,
1033 esi_to_str(&old_local
->attr
->esi
, esi_buf
,
1035 new_select
? esi_to_str(&new_select
->attr
->esi
,
1036 esi_buf2
, sizeof(esi_buf2
))
1040 /* Locate route node in the global EVPN routing table. Note that
1041 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1044 global_dest
= bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1045 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1048 /* Delete route entry in the global EVPN table. */
1049 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
1051 /* Schedule for processing - withdraws to peers happen from
1055 bgp_process(bgp
, global_dest
, afi
, safi
);
1056 bgp_dest_unlock_node(global_dest
);
1059 /* Delete route entry in the VNI route table, caller to remove. */
1060 bgp_path_info_delete(dest
, old_local
);
1064 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1066 * Note: vpn is NULL for local EAD-ES routes.
1068 int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1069 struct bgp_dest
*dest
)
1071 struct bgp_path_info
*old_select
, *new_select
;
1072 struct bgp_path_info_pair old_and_new
;
1073 afi_t afi
= AFI_L2VPN
;
1074 safi_t safi
= SAFI_EVPN
;
1077 /* Compute the best path. */
1078 bgp_best_selection(bgp
, dest
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1080 old_select
= old_and_new
.old
;
1081 new_select
= old_and_new
.new;
1083 /* If the best path hasn't changed - see if there is still something to
1084 * update to zebra RIB.
1085 * Remote routes and SYNC route (i.e. local routes with
1086 * SYNCED_FROM_PEER flag) need to updated to zebra on any attr
1089 if (old_select
&& old_select
== new_select
1090 && old_select
->type
== ZEBRA_ROUTE_BGP
1091 && (old_select
->sub_type
== BGP_ROUTE_IMPORTED
||
1092 bgp_evpn_attr_is_sync(old_select
->attr
))
1093 && !CHECK_FLAG(dest
->flags
, BGP_NODE_USER_CLEAR
)
1094 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1095 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1096 if (bgp_zebra_has_route_changed(old_select
))
1097 ret
= evpn_zebra_install(
1099 (const struct prefix_evpn
*)bgp_dest_get_prefix(
1102 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1103 UNSET_FLAG(old_select
->flags
, BGP_PATH_LINK_BW_CHG
);
1104 bgp_zebra_clear_route_change_flags(dest
);
1108 /* If the user did a "clear" this flag will be set */
1109 UNSET_FLAG(dest
->flags
, BGP_NODE_USER_CLEAR
);
1111 /* bestpath has changed; update relevant fields and install or uninstall
1112 * into the zebra RIB.
1114 if (old_select
|| new_select
)
1115 bgp_bump_version(dest
);
1118 bgp_path_info_unset_flag(dest
, old_select
, BGP_PATH_SELECTED
);
1120 bgp_path_info_set_flag(dest
, new_select
, BGP_PATH_SELECTED
);
1121 bgp_path_info_unset_flag(dest
, new_select
,
1122 BGP_PATH_ATTR_CHANGED
);
1123 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1124 UNSET_FLAG(new_select
->flags
, BGP_PATH_LINK_BW_CHG
);
1127 /* a local entry with the SYNC flag also results in a MAC-IP update
1130 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1131 && (new_select
->sub_type
== BGP_ROUTE_IMPORTED
||
1132 bgp_evpn_attr_is_sync(new_select
->attr
))) {
1133 ret
= evpn_zebra_install(
1135 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1138 /* If an old best existed and it was a "local" route, the only
1140 * it would be supplanted is due to MAC mobility procedures. So,
1142 * need to do an implicit delete and withdraw that route from
1145 if (new_select
->sub_type
== BGP_ROUTE_IMPORTED
&&
1146 old_select
&& old_select
->peer
== bgp
->peer_self
1147 && old_select
->type
== ZEBRA_ROUTE_BGP
1148 && old_select
->sub_type
== BGP_ROUTE_STATIC
1150 evpn_delete_old_local_route(bgp
, vpn
, dest
,
1151 old_select
, new_select
);
1153 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1154 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1155 ret
= evpn_zebra_uninstall(
1157 (const struct prefix_evpn
*)bgp_dest_get_prefix(
1159 old_select
->attr
->nexthop
);
1162 /* Clear any route change flags. */
1163 bgp_zebra_clear_route_change_flags(dest
);
1165 /* Reap old select bgp_path_info, if it has been removed */
1166 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1167 bgp_path_info_reap(dest
, old_select
);
1172 static struct bgp_path_info
*bgp_evpn_route_get_local_path(
1173 struct bgp
*bgp
, struct bgp_dest
*dest
)
1175 struct bgp_path_info
*tmp_pi
;
1176 struct bgp_path_info
*local_pi
= NULL
;
1178 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1179 tmp_pi
= tmp_pi
->next
) {
1180 if (bgp_evpn_is_path_local(bgp
, tmp_pi
)) {
1189 static int update_evpn_type5_route_entry(struct bgp
*bgp_evpn
,
1190 struct bgp
*bgp_vrf
, afi_t afi
,
1191 safi_t safi
, struct bgp_dest
*dest
,
1192 struct attr
*attr
, int *route_changed
)
1194 struct attr
*attr_new
= NULL
;
1195 struct bgp_path_info
*pi
= NULL
;
1196 mpls_label_t label
= MPLS_INVALID_LABEL
;
1197 struct bgp_path_info
*local_pi
= NULL
;
1198 struct bgp_path_info
*tmp_pi
= NULL
;
1201 /* locate the local route entry if any */
1202 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1203 tmp_pi
= tmp_pi
->next
) {
1204 if (tmp_pi
->peer
== bgp_evpn
->peer_self
1205 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1206 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1211 * create a new route entry if one doesn't exist.
1212 * Otherwise see if route attr has changed
1216 /* route has changed as this is the first entry */
1219 /* Add (or update) attribute to hash. */
1220 attr_new
= bgp_attr_intern(attr
);
1222 /* create the route info from attribute */
1223 pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1224 bgp_evpn
->peer_self
, attr_new
, dest
);
1225 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
1227 /* Type-5 routes advertise the L3-VNI */
1228 bgp_path_info_extra_get(pi
);
1229 vni2label(bgp_vrf
->l3vni
, &label
);
1230 memcpy(&pi
->extra
->label
, &label
, sizeof(label
));
1231 pi
->extra
->num_labels
= 1;
1233 /* add the route entry to route node*/
1234 bgp_path_info_add(dest
, pi
);
1238 if (!attrhash_cmp(tmp_pi
->attr
, attr
)) {
1240 /* attribute changed */
1243 /* The attribute has changed. */
1244 /* Add (or update) attribute to hash. */
1245 attr_new
= bgp_attr_intern(attr
);
1246 bgp_path_info_set_flag(dest
, tmp_pi
,
1247 BGP_PATH_ATTR_CHANGED
);
1249 /* Restore route, if needed. */
1250 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1251 bgp_path_info_restore(dest
, tmp_pi
);
1253 /* Unintern existing, set to new. */
1254 bgp_attr_unintern(&tmp_pi
->attr
);
1255 tmp_pi
->attr
= attr_new
;
1256 tmp_pi
->uptime
= bgp_clock();
1262 /* update evpn type-5 route entry */
1263 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1264 struct attr
*src_attr
, afi_t src_afi
,
1267 afi_t afi
= AFI_L2VPN
;
1268 safi_t safi
= SAFI_EVPN
;
1270 struct bgp_dest
*dest
= NULL
;
1271 struct bgp
*bgp_evpn
= NULL
;
1272 int route_changed
= 0;
1274 bgp_evpn
= bgp_get_evpn();
1278 /* Build path attribute for this route - use the source attr, if
1279 * present, else treat as locally originated.
1284 memset(&attr
, 0, sizeof(struct attr
));
1285 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1288 /* Advertise Primary IP (PIP) is enabled, send individual
1289 * IP (default instance router-id) as nexthop.
1290 * PIP is disabled or vrr interface is not present
1291 * use anycast-IP as nexthop and anycast RMAC.
1293 if (!bgp_vrf
->evpn_info
->advertise_pip
||
1294 (!bgp_vrf
->evpn_info
->is_anycast_mac
)) {
1295 attr
.nexthop
= bgp_vrf
->originator_ip
;
1296 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1297 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, ETH_ALEN
);
1300 memcpy(&attr
.rmac
, &bgp_vrf
->evpn_info
->pip_rmac
, ETH_ALEN
);
1301 if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
!= INADDR_ANY
) {
1302 attr
.nexthop
= bgp_vrf
->evpn_info
->pip_ip
;
1303 attr
.mp_nexthop_global_in
= bgp_vrf
->evpn_info
->pip_ip
;
1304 } else if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
== INADDR_ANY
)
1305 if (bgp_debug_zebra(NULL
))
1307 "VRF %s evp %pFX advertise-pip primary ip is not configured",
1308 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
);
1311 if (bgp_debug_zebra(NULL
))
1313 "VRF %s type-5 route evp %pFX RMAC %pEA nexthop %pI4",
1314 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
, &attr
.rmac
,
1317 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1319 if (src_afi
== AFI_IP6
&&
1320 CHECK_FLAG(bgp_vrf
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1321 BGP_L2VPN_EVPN_ADV_IPV6_UNICAST_GW_IP
)) {
1323 !IN6_IS_ADDR_UNSPECIFIED(&src_attr
->mp_nexthop_global
)) {
1324 attr
.evpn_overlay
.type
= OVERLAY_INDEX_GATEWAY_IP
;
1325 memcpy(&attr
.evpn_overlay
.gw_ip
.ipv6
,
1326 &src_attr
->mp_nexthop_global
,
1327 sizeof(struct in6_addr
));
1329 } else if (src_afi
== AFI_IP
&&
1330 CHECK_FLAG(bgp_vrf
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1331 BGP_L2VPN_EVPN_ADV_IPV4_UNICAST_GW_IP
)) {
1332 if (src_attr
&& src_attr
->nexthop
.s_addr
!= 0) {
1333 attr
.evpn_overlay
.type
= OVERLAY_INDEX_GATEWAY_IP
;
1334 memcpy(&attr
.evpn_overlay
.gw_ip
.ipv4
,
1335 &src_attr
->nexthop
, sizeof(struct in_addr
));
1339 /* Setup RT and encap extended community */
1340 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1342 /* get the route node in global table */
1343 dest
= bgp_global_evpn_node_get(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1344 (const struct prefix_evpn
*)evp
,
1348 /* create or update the route entry within the route node */
1349 update_evpn_type5_route_entry(bgp_evpn
, bgp_vrf
, afi
, safi
, dest
, &attr
,
1352 /* schedule for processing and unlock node */
1353 if (route_changed
) {
1354 bgp_process(bgp_evpn
, dest
, afi
, safi
);
1355 bgp_dest_unlock_node(dest
);
1358 /* uninten temporary */
1360 aspath_unintern(&attr
.aspath
);
1364 static void bgp_evpn_get_sync_info(struct bgp
*bgp
, esi_t
*esi
,
1365 struct bgp_dest
*dest
, uint32_t loc_seq
,
1366 uint32_t *max_sync_seq
, bool *active_on_peer
,
1367 bool *peer_router
, bool *proxy_from_peer
)
1369 struct bgp_path_info
*tmp_pi
;
1370 struct bgp_path_info
*second_best_path
= NULL
;
1371 uint32_t tmp_mm_seq
= 0;
1375 /* find the best non-local path. a local path can only be present
1378 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1379 tmp_pi
= tmp_pi
->next
) {
1380 if (tmp_pi
->sub_type
!= BGP_ROUTE_IMPORTED
||
1381 !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1384 if (bgp_evpn_path_info_cmp(bgp
, tmp_pi
,
1385 second_best_path
, &paths_eq
))
1386 second_best_path
= tmp_pi
;
1389 if (!second_best_path
)
1392 tmp_esi
= bgp_evpn_attr_get_esi(second_best_path
->attr
);
1393 /* if this has the same ES desination as the local path
1396 if (!memcmp(esi
, tmp_esi
, sizeof(esi_t
))) {
1397 tmp_mm_seq
= mac_mobility_seqnum(second_best_path
->attr
);
1398 if (tmp_mm_seq
< loc_seq
)
1401 /* we have a non-proxy path from the ES peer. */
1402 if (second_best_path
->attr
->es_flags
&
1403 ATTR_ES_PROXY_ADVERT
) {
1404 *proxy_from_peer
= true;
1406 *active_on_peer
= true;
1409 if (second_best_path
->attr
->router_flag
)
1410 *peer_router
= true;
1412 /* we use both proxy and non-proxy imports to
1413 * determine the max sync sequence
1415 if (tmp_mm_seq
> *max_sync_seq
)
1416 *max_sync_seq
= tmp_mm_seq
;
1420 /* Bubble up sync-info from all paths (non-best) to the local-path.
1421 * This is need for MM sequence number syncing and proxy advertisement.
1422 * Note: The local path can only exist as a best path in the
1423 * VPN route table. It will take precedence over all sync paths.
1425 static void update_evpn_route_entry_sync_info(struct bgp
*bgp
,
1426 struct bgp_dest
*dest
,
1428 uint32_t loc_seq
, bool setup_sync
)
1431 struct prefix_evpn
*evp
=
1432 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1434 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1437 esi
= bgp_evpn_attr_get_esi(attr
);
1438 if (bgp_evpn_is_esi_valid(esi
)) {
1440 uint32_t max_sync_seq
= 0;
1441 bool active_on_peer
= false;
1442 bool peer_router
= false;
1443 bool proxy_from_peer
= false;
1445 bgp_evpn_get_sync_info(bgp
, esi
, dest
, loc_seq
,
1446 &max_sync_seq
, &active_on_peer
,
1447 &peer_router
, &proxy_from_peer
);
1448 attr
->mm_sync_seqnum
= max_sync_seq
;
1450 attr
->es_flags
|= ATTR_ES_PEER_ACTIVE
;
1452 attr
->es_flags
&= ~ATTR_ES_PEER_ACTIVE
;
1453 if (proxy_from_peer
)
1454 attr
->es_flags
|= ATTR_ES_PEER_PROXY
;
1456 attr
->es_flags
&= ~ATTR_ES_PEER_PROXY
;
1458 attr
->es_flags
|= ATTR_ES_PEER_ROUTER
;
1460 attr
->es_flags
&= ~ATTR_ES_PEER_ROUTER
;
1462 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
1463 char esi_buf
[ESI_STR_LEN
];
1466 "setup sync info for %pFX es %s max_seq %d %s%s%s",
1468 esi_to_str(esi
, esi_buf
,
1471 (attr
->es_flags
& ATTR_ES_PEER_ACTIVE
)
1474 (attr
->es_flags
& ATTR_ES_PEER_PROXY
)
1477 (attr
->es_flags
& ATTR_ES_PEER_ROUTER
)
1483 attr
->mm_sync_seqnum
= 0;
1484 attr
->es_flags
&= ~ATTR_ES_PEER_ACTIVE
;
1485 attr
->es_flags
&= ~ATTR_ES_PEER_PROXY
;
1490 * Create or update EVPN route entry. This could be in the VNI route table
1491 * or the global route table.
1493 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1494 afi_t afi
, safi_t safi
,
1495 struct bgp_dest
*dest
, struct attr
*attr
,
1496 int add
, struct bgp_path_info
**pi
,
1497 uint8_t flags
, uint32_t seq
, bool vpn_rt
,
1500 struct bgp_path_info
*tmp_pi
;
1501 struct bgp_path_info
*local_pi
;
1502 struct attr
*attr_new
;
1503 mpls_label_t label
[BGP_MAX_LABELS
];
1504 uint32_t num_labels
= 1;
1505 int route_change
= 1;
1507 const struct prefix_evpn
*evp
;
1510 evp
= (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1511 memset(&label
, 0, sizeof(label
));
1513 /* See if this is an update of an existing route, or a new add. */
1514 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
1516 /* If route doesn't exist already, create a new one, if told to.
1517 * Otherwise act based on whether the attributes of the route have
1520 if (!local_pi
&& !add
)
1523 if (old_is_sync
&& local_pi
)
1524 *old_is_sync
= bgp_evpn_attr_is_sync(local_pi
->attr
);
1526 /* if a local path is being added with a non-zero esi look
1527 * for SYNC paths from ES peers and bubble up the sync-info
1529 update_evpn_route_entry_sync_info(bgp
, dest
, attr
, seq
, vpn_rt
);
1531 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1532 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1533 add_mac_mobility_to_attr(seq
, attr
);
1536 /* Add (or update) attribute to hash. */
1537 attr_new
= bgp_attr_intern(attr
);
1539 /* Extract MAC mobility sequence number, if any. */
1540 attr_new
->mm_seqnum
=
1541 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1542 attr_new
->sticky
= sticky
;
1544 /* Create new route with its attribute. */
1545 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1546 bgp
->peer_self
, attr_new
, dest
);
1547 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1548 bgp_path_info_extra_get(tmp_pi
);
1550 /* The VNI goes into the 'label' field of the route */
1551 vni2label(vpn
->vni
, &label
[0]);
1553 /* Type-2 routes may carry a second VNI - the L3-VNI.
1554 * Only attach second label if we are advertising two labels for
1557 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1558 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1561 l3vni
= bgpevpn_get_l3vni(vpn
);
1563 vni2label(l3vni
, &label
[1]);
1568 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1569 tmp_pi
->extra
->num_labels
= num_labels
;
1570 /* Mark route as self type-2 route */
1571 if (flags
&& CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1572 tmp_pi
->extra
->af_flags
= BGP_EVPN_MACIP_TYPE_SVI_IP
;
1573 bgp_path_info_add(dest
, tmp_pi
);
1576 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1577 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1581 * The attributes have changed, type-2 routes needs to
1582 * be advertised with right labels.
1584 vni2label(vpn
->vni
, &label
[0]);
1585 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1586 && CHECK_FLAG(vpn
->flags
,
1587 VNI_FLAG_USE_TWO_LABELS
)) {
1590 l3vni
= bgpevpn_get_l3vni(vpn
);
1592 vni2label(l3vni
, &label
[1]);
1596 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1597 tmp_pi
->extra
->num_labels
= num_labels
;
1599 /* The attribute has changed. */
1600 /* Add (or update) attribute to hash. */
1601 attr_new
= bgp_attr_intern(attr
);
1602 bgp_path_info_set_flag(dest
, tmp_pi
,
1603 BGP_PATH_ATTR_CHANGED
);
1605 /* Extract MAC mobility sequence number, if any. */
1606 attr_new
->mm_seqnum
=
1607 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1608 attr_new
->sticky
= sticky
;
1610 /* Restore route, if needed. */
1611 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1612 bgp_path_info_restore(dest
, tmp_pi
);
1614 /* Unintern existing, set to new. */
1615 bgp_attr_unintern(&tmp_pi
->attr
);
1616 tmp_pi
->attr
= attr_new
;
1617 tmp_pi
->uptime
= bgp_clock();
1621 /* local MAC-IP routes in the VNI table are linked to
1622 * the destination ES
1624 if (route_change
&& vpn_rt
1625 && (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
))
1626 bgp_evpn_path_es_link(tmp_pi
, vpn
->vni
,
1627 bgp_evpn_attr_get_esi(tmp_pi
->attr
));
1629 /* Return back the route entry. */
1631 return route_change
;
1634 static void evpn_zebra_reinstall_best_route(struct bgp
*bgp
,
1635 struct bgpevpn
*vpn
,
1636 struct bgp_dest
*dest
)
1638 struct bgp_path_info
*tmp_ri
;
1639 struct bgp_path_info
*curr_select
= NULL
;
1641 for (tmp_ri
= bgp_dest_get_bgp_path_info(dest
); tmp_ri
;
1642 tmp_ri
= tmp_ri
->next
) {
1643 if (CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_SELECTED
)) {
1644 curr_select
= tmp_ri
;
1649 if (curr_select
&& curr_select
->type
== ZEBRA_ROUTE_BGP
1650 && (curr_select
->sub_type
== BGP_ROUTE_IMPORTED
||
1651 bgp_evpn_attr_is_sync(curr_select
->attr
)))
1652 evpn_zebra_install(bgp
, vpn
,
1653 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1658 * If the local route was not selected evict it and tell zebra to re-add
1659 * the best remote dest.
1661 * Typically a local path added by zebra is expected to be selected as
1662 * best. In which case when a remote path wins as best (later)
1663 * evpn_route_select_install itself evicts the older-local-best path.
1665 * However if bgp's add and zebra's add cross paths (race condition) it
1666 * is possible that the local path is no longer the "older" best path.
1667 * It is a path that was never designated as best and hence requires
1668 * additional handling to prevent bgp from injecting and holding on to a
1669 * non-best local path.
1671 static void evpn_cleanup_local_non_best_route(struct bgp
*bgp
,
1672 struct bgpevpn
*vpn
,
1673 struct bgp_dest
*dest
,
1674 struct bgp_path_info
*local_pi
)
1676 /* local path was not picked as the winner; kick it out */
1677 if (bgp_debug_zebra(NULL
))
1678 zlog_debug("evicting local evpn prefix %pBD as remote won",
1681 evpn_delete_old_local_route(bgp
, vpn
, dest
, local_pi
, NULL
);
1682 bgp_path_info_reap(dest
, local_pi
);
1684 /* tell zebra to re-add the best remote path */
1685 evpn_zebra_reinstall_best_route(bgp
, vpn
, dest
);
1688 static inline bool bgp_evpn_route_add_l3_ecomm_ok(struct bgpevpn
*vpn
,
1689 const struct prefix_evpn
*p
,
1692 return p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1693 && (is_evpn_prefix_ipaddr_v4(p
)
1694 || (is_evpn_prefix_ipaddr_v6(p
)
1695 && !IN6_IS_ADDR_LINKLOCAL(
1696 &p
->prefix
.macip_addr
.ip
.ipaddr_v6
)))
1697 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)
1698 && bgpevpn_get_l3vni(vpn
) && bgp_evpn_es_add_l3_ecomm_ok(esi
);
1702 * Create or update EVPN route (of type based on prefix) for specified VNI
1703 * and schedule for processing.
1705 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1706 struct prefix_evpn
*p
, uint8_t flags
,
1707 uint32_t seq
, esi_t
*esi
)
1709 struct bgp_dest
*dest
;
1711 struct attr
*attr_new
;
1712 int add_l3_ecomm
= 0;
1713 struct bgp_path_info
*pi
;
1714 afi_t afi
= AFI_L2VPN
;
1715 safi_t safi
= SAFI_EVPN
;
1717 bool old_is_sync
= false;
1719 memset(&attr
, 0, sizeof(struct attr
));
1721 /* Build path-attribute for this route. */
1722 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1723 attr
.nexthop
= vpn
->originator_ip
;
1724 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1725 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1726 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1727 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1728 attr
.router_flag
= CHECK_FLAG(flags
,
1729 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1730 if (CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_PROXY_ADVERT
))
1731 attr
.es_flags
|= ATTR_ES_PROXY_ADVERT
;
1733 if (esi
&& bgp_evpn_is_esi_valid(esi
)) {
1734 memcpy(&attr
.esi
, esi
, sizeof(esi_t
));
1735 attr
.es_flags
|= ATTR_ES_IS_LOCAL
;
1738 /* PMSI is only needed for type-3 routes */
1739 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
1740 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1741 bgp_attr_set_pmsi_tnl_type(&attr
, PMSI_TNLTYPE_INGR_REPL
);
1744 if (bgp_debug_zebra(NULL
)) {
1745 char buf3
[ESI_STR_LEN
];
1748 "VRF %s vni %u type-2 route evp %pFX RMAC %pEA nexthop %pI4 esi %s",
1749 vpn
->bgp_vrf
? vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
)
1751 vpn
->vni
, p
, &attr
.rmac
, &attr
.mp_nexthop_global_in
,
1752 esi_to_str(esi
, buf3
, sizeof(buf3
)));
1754 /* router mac is only needed for type-2 routes here. */
1755 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
1756 uint8_t af_flags
= 0;
1758 if (CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1759 SET_FLAG(af_flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
);
1761 bgp_evpn_get_rmac_nexthop(vpn
, p
, &attr
, af_flags
);
1764 vni2label(vpn
->vni
, &(attr
.label
));
1766 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1767 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1770 add_l3_ecomm
= bgp_evpn_route_add_l3_ecomm_ok(
1771 vpn
, p
, (attr
.es_flags
& ATTR_ES_IS_LOCAL
) ? &attr
.esi
: NULL
);
1773 /* Set up extended community. */
1774 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1776 /* First, create (or fetch) route node within the VNI. */
1777 /* NOTE: There is no RD here. */
1778 dest
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1780 /* Create or update route entry. */
1781 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, dest
, &attr
,
1783 true /* setup_sync */, &old_is_sync
);
1785 attr_new
= pi
->attr
;
1787 /* lock ri to prevent freeing in evpn_route_select_install */
1788 bgp_path_info_lock(pi
);
1790 /* Perform route selection. Normally, the local route in the
1791 * VNI is expected to win and be the best route. However, if
1792 * there is a race condition where a host moved from local to
1793 * remote and the remote route was received in BGP just prior
1794 * to the local MACIP notification from zebra, the remote
1795 * route would win, and we should evict the defunct local route
1796 * and (re)install the remote route into zebra.
1798 evpn_route_select_install(bgp
, vpn
, dest
);
1800 * If the new local route was not selected evict it and tell zebra
1801 * to re-add the best remote dest. BGP doesn't retain non-best local
1804 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
1807 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1809 evpn_cleanup_local_non_best_route(bgp
, vpn
, dest
, pi
);
1813 /* If the local path already existed and is still the
1814 * best path we need to also check if it transitioned
1815 * from being a sync path to a non-sync path. If it
1816 * it did we need to notify zebra that the sync-path
1819 new_is_sync
= bgp_evpn_attr_is_sync(pi
->attr
);
1820 if (!new_is_sync
&& old_is_sync
)
1821 evpn_zebra_uninstall(bgp
, vpn
, p
, zero_vtep_ip
);
1824 bgp_path_info_unlock(pi
);
1826 bgp_dest_unlock_node(dest
);
1828 /* If this is a new route or some attribute has changed, export the
1829 * route to the global table. The route will be advertised to peers
1830 * from there. Note that this table is a 2-level tree (RD-level +
1831 * Prefix-level) similar to L3VPN routes.
1834 struct bgp_path_info
*global_pi
;
1836 dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1837 (const struct prefix_evpn
*)p
,
1839 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, dest
, attr_new
, 1,
1840 &global_pi
, flags
, seq
,
1841 false /* setup_sync */, NULL
/* old_is_sync */);
1843 /* Schedule for processing and unlock node. */
1844 bgp_process(bgp
, dest
, afi
, safi
);
1845 bgp_dest_unlock_node(dest
);
1848 /* Unintern temporary. */
1849 aspath_unintern(&attr
.aspath
);
1855 * Delete EVPN route entry.
1856 * The entry can be in ESI/VNI table or the global table.
1858 void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1859 struct bgp_dest
*dest
,
1860 struct bgp_path_info
**pi
)
1862 struct bgp_path_info
*tmp_pi
;
1866 /* Now, find matching route. */
1867 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1868 tmp_pi
= tmp_pi
->next
)
1869 if (tmp_pi
->peer
== bgp
->peer_self
1870 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1871 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1876 /* Mark route for delete. */
1878 bgp_path_info_delete(dest
, tmp_pi
);
1881 /* Delete EVPN type5 route */
1882 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1884 afi_t afi
= AFI_L2VPN
;
1885 safi_t safi
= SAFI_EVPN
;
1886 struct bgp_dest
*dest
= NULL
;
1887 struct bgp_path_info
*pi
= NULL
;
1888 struct bgp
*bgp_evpn
= NULL
; /* evpn bgp instance */
1890 bgp_evpn
= bgp_get_evpn();
1894 /* locate the global route entry for this type-5 prefix */
1895 dest
= bgp_global_evpn_node_lookup(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1896 (const struct prefix_evpn
*)evp
, &bgp_vrf
->vrf_prd
);
1900 delete_evpn_route_entry(bgp_evpn
, afi
, safi
, dest
, &pi
);
1902 bgp_process(bgp_evpn
, dest
, afi
, safi
);
1903 bgp_dest_unlock_node(dest
);
1908 * Delete EVPN route (of type based on prefix) for specified VNI and
1909 * schedule for processing.
1911 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1912 struct prefix_evpn
*p
)
1914 struct bgp_dest
*dest
, *global_dest
;
1915 struct bgp_path_info
*pi
;
1916 afi_t afi
= AFI_L2VPN
;
1917 safi_t safi
= SAFI_EVPN
;
1919 /* First, locate the route node within the VNI. If it doesn't exist,
1921 * is nothing further to do.
1923 /* NOTE: There is no RD here. */
1924 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1928 /* Next, locate route node in the global EVPN routing table. Note that
1929 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1932 global_dest
= bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1933 (const struct prefix_evpn
*)p
, &vpn
->prd
);
1935 /* Delete route entry in the global EVPN table. */
1936 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
1938 /* Schedule for processing - withdraws to peers happen from
1942 bgp_process(bgp
, global_dest
, afi
, safi
);
1943 bgp_dest_unlock_node(global_dest
);
1946 /* Delete route entry in the VNI route table. This can just be removed.
1948 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
1950 bgp_path_info_reap(dest
, pi
);
1951 evpn_route_select_install(bgp
, vpn
, dest
);
1953 bgp_dest_unlock_node(dest
);
1958 void bgp_evpn_update_type2_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1959 struct bgp_dest
*dest
,
1960 struct bgp_path_info
*local_pi
,
1963 afi_t afi
= AFI_L2VPN
;
1964 safi_t safi
= SAFI_EVPN
;
1965 struct bgp_path_info
*pi
;
1967 struct attr
*attr_new
;
1969 int add_l3_ecomm
= 0;
1970 struct bgp_dest
*global_dest
;
1971 struct bgp_path_info
*global_pi
;
1972 struct prefix_evpn
*evp
=
1973 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1975 bool old_is_sync
= false;
1977 if (CHECK_FLAG(local_pi
->flags
, BGP_PATH_REMOVED
))
1981 * Build attribute per local route as the MAC mobility and
1982 * some other values could differ for different routes. The
1983 * attributes will be shared in the hash table.
1985 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1986 attr
.nexthop
= vpn
->originator_ip
;
1987 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1988 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1989 attr
.sticky
= (local_pi
->attr
->sticky
) ? 1 : 0;
1990 attr
.router_flag
= (local_pi
->attr
->router_flag
) ? 1 : 0;
1991 attr
.es_flags
= local_pi
->attr
->es_flags
;
1992 if (local_pi
->attr
->default_gw
) {
1993 attr
.default_gw
= 1;
1994 if (is_evpn_prefix_ipaddr_v6(evp
))
1995 attr
.router_flag
= 1;
1997 memcpy(&attr
.esi
, &local_pi
->attr
->esi
, sizeof(esi_t
));
1998 bgp_evpn_get_rmac_nexthop(vpn
, evp
, &attr
,
1999 local_pi
->extra
->af_flags
);
2000 vni2label(vpn
->vni
, &(attr
.label
));
2001 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2002 * using L3 VNI for type-2 routes also.
2004 add_l3_ecomm
= bgp_evpn_route_add_l3_ecomm_ok(
2006 (attr
.es_flags
& ATTR_ES_IS_LOCAL
) ? &attr
.esi
: NULL
);
2008 /* Set up extended community. */
2009 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2010 seq
= mac_mobility_seqnum(local_pi
->attr
);
2012 if (bgp_debug_zebra(NULL
)) {
2013 char buf3
[ESI_STR_LEN
];
2016 "VRF %s vni %u evp %pFX RMAC %pEA nexthop %pI4 esi %s esf 0x%x from %s",
2017 vpn
->bgp_vrf
? vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
)
2019 vpn
->vni
, evp
, &attr
.rmac
, &attr
.mp_nexthop_global_in
,
2020 esi_to_str(&attr
.esi
, buf3
, sizeof(buf3
)),
2021 attr
.es_flags
, caller
);
2024 /* Update the route entry. */
2025 route_change
= update_evpn_route_entry(
2026 bgp
, vpn
, afi
, safi
, dest
, &attr
, 0, &pi
, 0, seq
,
2027 true /* setup_sync */, &old_is_sync
);
2030 attr_new
= pi
->attr
;
2031 /* lock ri to prevent freeing in evpn_route_select_install */
2032 bgp_path_info_lock(pi
);
2034 /* Perform route selection. Normally, the local route in the
2035 * VNI is expected to win and be the best route. However,
2036 * under peculiar situations (e.g., tunnel (next hop) IP change
2037 * that causes best selection to be based on next hop), a
2038 * remote route could win. If the local route is the best,
2039 * ensure it is updated in the global EVPN route table and
2040 * advertised to peers; otherwise, ensure it is evicted and
2041 * (re)install the remote route into zebra.
2043 evpn_route_select_install(bgp
, vpn
, dest
);
2045 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2048 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
2050 evpn_cleanup_local_non_best_route(bgp
, vpn
, dest
, pi
);
2054 /* If the local path already existed and is still the
2055 * best path we need to also check if it transitioned
2056 * from being a sync path to a non-sync path. If it
2057 * it did we need to notify zebra that the sync-path
2060 new_is_sync
= bgp_evpn_attr_is_sync(pi
->attr
);
2061 if (!new_is_sync
&& old_is_sync
)
2062 evpn_zebra_uninstall(bgp
, vpn
,
2069 bgp_path_info_unlock(pi
);
2072 /* Update route in global routing table. */
2073 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
,
2074 safi
, evp
, &vpn
->prd
);
2075 assert(global_dest
);
2076 update_evpn_route_entry(
2077 bgp
, vpn
, afi
, safi
, global_dest
, attr_new
, 0,
2078 &global_pi
, 0, mac_mobility_seqnum(attr_new
),
2079 false /* setup_sync */, NULL
/* old_is_sync */);
2081 /* Schedule for processing and unlock node. */
2082 bgp_process(bgp
, global_dest
, afi
, safi
);
2083 bgp_dest_unlock_node(global_dest
);
2086 /* Unintern temporary. */
2087 aspath_unintern(&attr
.aspath
);
2091 * Update all type-2 (MACIP) local routes for this VNI - these should also
2092 * be scheduled for advertise to peers.
2094 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2096 struct bgp_dest
*dest
;
2097 struct bgp_path_info
*tmp_pi
;
2099 /* Walk this VNI's route table and update local type-2 routes. For any
2100 * routes updated, update corresponding entry in the global table too.
2102 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2103 dest
= bgp_route_next(dest
)) {
2104 const struct prefix_evpn
*evp
=
2105 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
2107 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2110 /* Identify local route. */
2111 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
2112 tmp_pi
= tmp_pi
->next
) {
2113 if (tmp_pi
->peer
== bgp
->peer_self
2114 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2115 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2122 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, tmp_pi
,
2130 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2131 * global routing table. These are also scheduled for withdraw from peers.
2133 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2137 struct bgp_dest
*rddest
, *dest
;
2138 struct bgp_table
*table
;
2139 struct bgp_path_info
*pi
;
2144 rddest
= bgp_node_lookup(bgp
->rib
[afi
][safi
],
2145 (struct prefix
*)&vpn
->prd
);
2146 if (rddest
&& bgp_dest_has_bgp_path_info_data(rddest
)) {
2147 table
= bgp_dest_get_bgp_table_info(rddest
);
2148 for (dest
= bgp_table_top(table
); dest
;
2149 dest
= bgp_route_next(dest
)) {
2150 const struct prefix_evpn
*evp
=
2151 (const struct prefix_evpn
*)bgp_dest_get_prefix(
2154 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2157 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2159 bgp_process(bgp
, dest
, afi
, safi
);
2163 /* Unlock RD node. */
2165 bgp_dest_unlock_node(rddest
);
2171 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2172 * table as well as the per-VNI route table.
2174 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2178 struct bgp_dest
*dest
;
2179 struct bgp_path_info
*pi
;
2184 /* First, walk the global route table for this VNI's type-2 local
2186 * EVPN routes are a 2-level table, first get the RD table.
2188 delete_global_type2_routes(bgp
, vpn
);
2190 /* Next, walk this VNI's route table and delete local type-2 routes. */
2191 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2192 dest
= bgp_route_next(dest
)) {
2193 const struct prefix_evpn
*evp
=
2194 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
2196 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2199 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2201 /* Route entry in local table gets deleted immediately. */
2203 bgp_path_info_reap(dest
, pi
);
2210 * Delete all routes in the per-VNI route table.
2212 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2214 struct bgp_dest
*dest
;
2215 struct bgp_path_info
*pi
, *nextpi
;
2217 /* Walk this VNI's route table and delete all routes. */
2218 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2219 dest
= bgp_route_next(dest
)) {
2220 for (pi
= bgp_dest_get_bgp_path_info(dest
);
2221 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2222 bgp_path_info_delete(dest
, pi
);
2223 bgp_path_info_reap(dest
, pi
);
2230 /* BUM traffic flood mode per-l2-vni */
2231 static int bgp_evpn_vni_flood_mode_get(struct bgp
*bgp
,
2232 struct bgpevpn
*vpn
)
2234 /* if flooding has been globally disabled per-vni mode is
2237 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
2238 return VXLAN_FLOOD_DISABLED
;
2240 /* if mcast group ip has been specified we use a PIM-SM MDT */
2241 if (vpn
->mcast_grp
.s_addr
!= INADDR_ANY
)
2242 return VXLAN_FLOOD_PIM_SM
;
2244 /* default is ingress replication */
2245 return VXLAN_FLOOD_HEAD_END_REPL
;
2249 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2250 * export RT getting modified or change to tunnel IP. Note that these
2251 * situations need the route in the per-VNI table as well as the global
2252 * table to be updated (as attributes change).
2254 int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2257 struct prefix_evpn p
;
2259 update_type1_routes_for_evi(bgp
, vpn
);
2261 /* Update and advertise the type-3 route (only one) followed by the
2262 * locally learnt type-2 routes (MACIP) - for this VNI.
2264 * RT-3 only if doing head-end replication
2266 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
2267 == VXLAN_FLOOD_HEAD_END_REPL
) {
2268 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2269 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
);
2274 return update_all_type2_routes(bgp
, vpn
);
2278 * Delete (and withdraw) local routes for specified VNI from the global
2279 * table and per-VNI table. After this, remove all other routes from
2280 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2281 * (advertise-all-vni) being disabled.
2283 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2286 struct prefix_evpn p
;
2288 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2289 * followed by type-3 routes (only one) - for this VNI.
2291 ret
= delete_all_type2_routes(bgp
, vpn
);
2295 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2296 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2300 /* Delete all routes from the per-VNI table. */
2301 return delete_all_vni_routes(bgp
, vpn
);
2305 * There is a flood mcast IP address change. Update the mcast-grp and
2306 * remove the type-3 route if any. A new type-3 route will be generated
2307 * post tunnel_ip update if the new flood mode is head-end-replication.
2309 static int bgp_evpn_mcast_grp_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2310 struct in_addr mcast_grp
)
2312 struct prefix_evpn p
;
2314 vpn
->mcast_grp
= mcast_grp
;
2316 if (is_vni_live(vpn
)) {
2317 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2318 delete_evpn_route(bgp
, vpn
, &p
);
2325 * There is a tunnel endpoint IP address change for this VNI, delete
2326 * prior type-3 route (if needed) and update.
2327 * Note: Route re-advertisement happens elsewhere after other processing
2330 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2331 struct in_addr originator_ip
)
2333 struct prefix_evpn p
;
2335 /* If VNI is not live, we only need to update the originator ip */
2336 if (!is_vni_live(vpn
)) {
2337 vpn
->originator_ip
= originator_ip
;
2341 /* Update the tunnel-ip hash */
2342 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2343 bgp_tip_add(bgp
, &originator_ip
);
2345 /* filter routes as martian nexthop db has changed */
2346 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2348 /* Need to withdraw type-3 route as the originator IP is part
2351 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2352 delete_evpn_route(bgp
, vpn
, &p
);
2354 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2355 vpn
->originator_ip
= originator_ip
;
2359 static struct bgp_path_info
*
2360 bgp_create_evpn_bgp_path_info(struct bgp_path_info
*parent_pi
,
2361 struct bgp_dest
*dest
, struct attr
*attr
)
2363 struct attr
*attr_new
;
2364 struct bgp_path_info
*pi
;
2366 /* Add (or update) attribute to hash. */
2367 attr_new
= bgp_attr_intern(attr
);
2369 /* Create new route with its attribute. */
2370 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0, parent_pi
->peer
,
2372 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2373 bgp_path_info_extra_get(pi
);
2374 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2375 bgp_dest_lock_node((struct bgp_dest
*)parent_pi
->net
);
2376 if (parent_pi
->extra
) {
2377 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2378 sizeof(pi
->extra
->label
));
2379 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2381 bgp_path_info_add(dest
, pi
);
2387 * Install route entry into the VRF routing table and invoke route selection.
2389 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2390 const struct prefix_evpn
*evp
,
2391 struct bgp_path_info
*parent_pi
)
2393 struct bgp_dest
*dest
;
2394 struct bgp_path_info
*pi
;
2396 struct attr
*attr_new
;
2399 struct prefix
*pp
= &p
;
2402 bool new_pi
= false;
2403 bool use_l3nhg
= false;
2404 bool is_l3nhg_active
= false;
2406 memset(pp
, 0, sizeof(struct prefix
));
2407 ip_prefix_from_evpn_prefix(evp
, pp
);
2409 if (bgp_debug_zebra(NULL
))
2411 "vrf %s: import evpn prefix %pFX parent %p flags 0x%x",
2412 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
, parent_pi
,
2415 /* Create (or fetch) route within the VRF. */
2416 /* NOTE: There is no RD here. */
2417 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2419 safi
= SAFI_UNICAST
;
2420 dest
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2421 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2423 safi
= SAFI_UNICAST
;
2424 dest
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2428 /* EVPN routes currently only support a IPv4 next hop which corresponds
2429 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2430 * or prefix route, we have to convert the next hop to an IPv4-mapped
2431 * address for the rest of the code to flow through. In the case of IPv4,
2432 * make sure to set the flag for next hop attribute.
2434 attr
= *parent_pi
->attr
;
2436 evpn_convert_nexthop_to_ipv6(&attr
);
2438 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2440 bgp_evpn_es_vrf_use_nhg(bgp_vrf
, &parent_pi
->attr
->esi
, &use_l3nhg
,
2441 &is_l3nhg_active
, NULL
);
2443 attr
.es_flags
|= ATTR_ES_L3_NHG_USE
;
2444 if (is_l3nhg_active
)
2445 attr
.es_flags
|= ATTR_ES_L3_NHG_ACTIVE
;
2447 /* Check if route entry is already present. */
2448 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2450 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2454 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, dest
, &attr
);
2457 if (attrhash_cmp(pi
->attr
, &attr
)
2458 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2459 bgp_dest_unlock_node(dest
);
2462 /* The attribute has changed. */
2463 /* Add (or update) attribute to hash. */
2464 attr_new
= bgp_attr_intern(&attr
);
2466 /* Restore route, if needed. */
2467 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2468 bgp_path_info_restore(dest
, pi
);
2470 /* Mark if nexthop has changed. */
2472 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2474 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2475 &attr_new
->mp_nexthop_global
)))
2476 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2478 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ATTR_CHANGED
);
2479 /* Unintern existing, set to new. */
2480 bgp_attr_unintern(&pi
->attr
);
2481 pi
->attr
= attr_new
;
2482 pi
->uptime
= bgp_clock();
2484 /* as it is an importation, change nexthop */
2485 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ANNC_NH_SELF
);
2487 /* Link path to evpn nexthop */
2488 bgp_evpn_path_nh_add(bgp_vrf
, pi
);
2490 bgp_aggregate_increment(bgp_vrf
, bgp_dest_get_prefix(dest
), pi
, afi
,
2493 /* Perform route selection and update zebra, if required. */
2494 bgp_process(bgp_vrf
, dest
, afi
, safi
);
2496 /* Process for route leaking. */
2497 vpn_leak_from_vrf_update(bgp_get_default(), bgp_vrf
, pi
);
2499 bgp_dest_unlock_node(dest
);
2501 if (bgp_debug_zebra(NULL
))
2502 zlog_debug("... %s pi dest %p (l %d) pi %p (l %d, f 0x%x)",
2503 new_pi
? "new" : "update", dest
,
2504 bgp_dest_get_lock_count(dest
), pi
, pi
->lock
,
2511 * Install route entry into the VNI routing table and invoke route selection.
2513 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2514 const struct prefix_evpn
*p
,
2515 struct bgp_path_info
*parent_pi
)
2517 struct bgp_dest
*dest
;
2518 struct bgp_path_info
*pi
;
2519 struct bgp_path_info
*local_pi
;
2520 struct attr
*attr_new
;
2522 struct prefix_evpn ad_evp
;
2523 bool old_local_es
= false;
2526 /* EAD prefix in the global table doesn't include the VTEP-IP so
2527 * we need to create a different copy for the VNI
2529 if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
2530 p
= evpn_type1_prefix_vni_copy(&ad_evp
, p
,
2531 parent_pi
->attr
->nexthop
);
2533 /* Create (or fetch) route within the VNI. */
2534 /* NOTE: There is no RD here. */
2535 dest
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2537 /* Check if route entry is already present. */
2538 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2540 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2544 /* Create an info */
2545 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, dest
,
2547 new_local_es
= bgp_evpn_attr_is_local_es(pi
->attr
);
2549 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2550 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2551 bgp_dest_unlock_node(dest
);
2554 /* The attribute has changed. */
2555 /* Add (or update) attribute to hash. */
2556 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2558 /* Restore route, if needed. */
2559 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2560 bgp_path_info_restore(dest
, pi
);
2562 /* Mark if nexthop has changed. */
2563 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2564 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2566 old_local_es
= bgp_evpn_attr_is_local_es(pi
->attr
);
2567 new_local_es
= bgp_evpn_attr_is_local_es(attr_new
);
2568 /* If ESI is different or if its type has changed we
2569 * need to reinstall the path in zebra
2571 if ((old_local_es
!= new_local_es
)
2572 || memcmp(&pi
->attr
->esi
, &attr_new
->esi
,
2573 sizeof(attr_new
->esi
))) {
2575 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
))
2576 zlog_debug("VNI %d path %pFX chg to %s es",
2577 vpn
->vni
, &pi
->net
->p
,
2578 new_local_es
? "local"
2580 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ATTR_CHANGED
);
2583 /* Unintern existing, set to new. */
2584 bgp_attr_unintern(&pi
->attr
);
2585 pi
->attr
= attr_new
;
2586 pi
->uptime
= bgp_clock();
2589 /* Perform route selection and update zebra, if required. */
2590 ret
= evpn_route_select_install(bgp
, vpn
, dest
);
2592 /* if the best path is a local path with a non-zero ES
2593 * sync info against the local path may need to be updated
2594 * when a remote path is added/updated (including changes
2595 * from sync-path to remote-path)
2597 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
2598 if (local_pi
&& (old_local_es
|| new_local_es
))
2599 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, local_pi
,
2601 bgp_dest_unlock_node(dest
);
2607 * Uninstall route entry from the VRF routing table and send message
2608 * to zebra, if appropriate.
2610 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2611 const struct prefix_evpn
*evp
,
2612 struct bgp_path_info
*parent_pi
)
2614 struct bgp_dest
*dest
;
2615 struct bgp_path_info
*pi
;
2618 struct prefix
*pp
= &p
;
2622 memset(pp
, 0, sizeof(struct prefix
));
2623 ip_prefix_from_evpn_prefix(evp
, pp
);
2625 if (bgp_debug_zebra(NULL
))
2627 "vrf %s: unimport evpn prefix %pFX parent %p flags 0x%x",
2628 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
, parent_pi
,
2631 /* Locate route within the VRF. */
2632 /* NOTE: There is no RD here. */
2633 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2635 safi
= SAFI_UNICAST
;
2636 dest
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2639 safi
= SAFI_UNICAST
;
2640 dest
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2646 /* Find matching route entry. */
2647 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2649 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2655 if (bgp_debug_zebra(NULL
))
2656 zlog_debug("... delete dest %p (l %d) pi %p (l %d, f 0x%x)",
2657 dest
, bgp_dest_get_lock_count(dest
), pi
, pi
->lock
,
2660 /* Process for route leaking. */
2661 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp_vrf
, pi
);
2663 bgp_aggregate_decrement(bgp_vrf
, bgp_dest_get_prefix(dest
), pi
, afi
,
2666 /* Mark entry for deletion */
2667 bgp_path_info_delete(dest
, pi
);
2669 /* Unlink path to evpn nexthop */
2670 bgp_evpn_path_nh_del(bgp_vrf
, pi
);
2672 /* Perform route selection and update zebra, if required. */
2673 bgp_process(bgp_vrf
, dest
, afi
, safi
);
2675 /* Unlock route node. */
2676 bgp_dest_unlock_node(dest
);
2682 * Uninstall route entry from the VNI routing table and send message
2683 * to zebra, if appropriate.
2685 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2686 const struct prefix_evpn
*p
,
2687 struct bgp_path_info
*parent_pi
)
2689 struct bgp_dest
*dest
;
2690 struct bgp_path_info
*pi
;
2691 struct bgp_path_info
*local_pi
;
2693 struct prefix_evpn ad_evp
;
2695 /* EAD prefix in the global table doesn't include the VTEP-IP so
2696 * we need to create a different copy for the VNI
2698 if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
2699 p
= evpn_type1_prefix_vni_copy(&ad_evp
, p
,
2700 parent_pi
->attr
->nexthop
);
2702 /* Locate route within the VNI. */
2703 /* NOTE: There is no RD here. */
2704 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2708 /* Find matching route entry. */
2709 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2711 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2717 /* Mark entry for deletion */
2718 bgp_path_info_delete(dest
, pi
);
2720 /* Perform route selection and update zebra, if required. */
2721 ret
= evpn_route_select_install(bgp
, vpn
, dest
);
2723 /* if the best path is a local path with a non-zero ES
2724 * sync info against the local path may need to be updated
2725 * when a remote path is deleted
2727 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
2728 if (local_pi
&& bgp_evpn_attr_is_local_es(local_pi
->attr
))
2729 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, local_pi
,
2732 /* Unlock route node. */
2733 bgp_dest_unlock_node(dest
);
2739 * Given a route entry and a VRF, see if this route entry should be
2740 * imported into the VRF i.e., RTs match.
2742 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
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 vrf_irt_node
*irt
;
2768 /* Only deal with RTs */
2769 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
2770 eval
= (struct ecommunity_val
*)(ecom
->val
2771 + (i
* ecom
->unit_size
));
2774 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2777 /* See if this RT matches specified VNIs import RTs */
2778 irt
= lookup_vrf_import_rt(eval
);
2780 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
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
, ecom
->unit_size
);
2794 mask_ecom_global_admin(&eval_tmp
, eval
);
2795 irt
= lookup_vrf_import_rt(&eval_tmp
);
2798 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2806 * Given a route entry and a VNI, see if this route entry should be
2807 * imported into the VNI i.e., RTs match.
2809 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2810 struct bgp_path_info
*pi
)
2812 struct attr
*attr
= pi
->attr
;
2813 struct ecommunity
*ecom
;
2817 /* Route should have valid RT to be even considered. */
2818 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2821 ecom
= attr
->ecommunity
;
2822 if (!ecom
|| !ecom
->size
)
2825 /* For each extended community RT, see if it matches this VNI. If any RT
2826 * matches, we're done.
2828 for (i
= 0; i
< ecom
->size
; i
++) {
2830 uint8_t type
, sub_type
;
2831 struct ecommunity_val
*eval
;
2832 struct ecommunity_val eval_tmp
;
2833 struct irt_node
*irt
;
2835 /* Only deal with RTs */
2836 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
2837 eval
= (struct ecommunity_val
*)(ecom
->val
2838 + (i
* ecom
->unit_size
));
2841 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2844 /* See if this RT matches specified VNIs import RTs */
2845 irt
= lookup_import_rt(bgp
, eval
);
2847 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2850 /* Also check for non-exact match. In this, we mask out the AS
2852 * only check on the local-admin sub-field. This is to
2854 * VNI as the RT for EBGP peering too.
2857 if (type
== ECOMMUNITY_ENCODE_AS
2858 || type
== ECOMMUNITY_ENCODE_AS4
2859 || type
== ECOMMUNITY_ENCODE_IP
) {
2860 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
2861 mask_ecom_global_admin(&eval_tmp
, eval
);
2862 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2865 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2872 /* This API will scan evpn routes for checking attribute's rmac
2873 * macthes with bgp instance router mac. It avoid installing
2874 * route into bgp vrf table and remote rmac in bridge table.
2876 static int bgp_evpn_route_rmac_self_check(struct bgp
*bgp_vrf
,
2877 const struct prefix_evpn
*evp
,
2878 struct bgp_path_info
*pi
)
2880 /* evpn route could have learnt prior to L3vni has come up,
2881 * perform rmac check before installing route and
2882 * remote router mac.
2883 * The route will be removed from global bgp table once
2884 * SVI comes up with MAC and stored in hash, triggers
2885 * bgp_mac_rescan_all_evpn_tables.
2887 if (memcmp(&bgp_vrf
->rmac
, &pi
->attr
->rmac
, ETH_ALEN
) == 0) {
2888 if (bgp_debug_update(pi
->peer
, NULL
, NULL
, 1)) {
2889 char attr_str
[BUFSIZ
] = {0};
2891 bgp_dump_attr(pi
->attr
, attr_str
, sizeof(attr_str
));
2894 "%s: bgp %u prefix %pFX with attr %s - DENIED due to self mac",
2895 __func__
, bgp_vrf
->vrf_id
, evp
, attr_str
);
2904 /* don't import hosts that are locally attached */
2906 bgp_evpn_skip_vrf_import_of_local_es(struct bgp
*bgp_vrf
,
2907 const struct prefix_evpn
*evp
,
2908 struct bgp_path_info
*pi
, int install
)
2912 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
2913 esi
= bgp_evpn_attr_get_esi(pi
->attr
);
2915 /* Don't import routes that point to a local destination */
2916 if (bgp_evpn_attr_is_local_es(pi
->attr
)) {
2917 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
2918 char esi_buf
[ESI_STR_LEN
];
2921 "vrf %s of evpn prefix %pFX skipped, local es %s",
2922 install
? "import" : "unimport", evp
,
2923 esi_to_str(esi
, esi_buf
,
2933 * Install or uninstall a mac-ip route in the provided vrf if
2934 * there is a rt match
2936 int bgp_evpn_route_entry_install_if_vrf_match(struct bgp
*bgp_vrf
,
2937 struct bgp_path_info
*pi
,
2941 const struct prefix_evpn
*evp
=
2942 (const struct prefix_evpn
*)bgp_dest_get_prefix(pi
->net
);
2944 /* Consider "valid" remote routes applicable for
2947 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
2948 && pi
->type
== ZEBRA_ROUTE_BGP
2949 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
2952 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
2953 if (bgp_evpn_route_rmac_self_check(bgp_vrf
, evp
, pi
))
2956 /* don't import hosts that are locally attached */
2958 && !bgp_evpn_skip_vrf_import_of_local_es(bgp_vrf
, evp
, pi
,
2960 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
2962 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
2966 flog_err(EC_BGP_EVPN_FAIL
,
2967 "Failed to %s EVPN %pFX route in VRF %s",
2968 install
? "install" : "uninstall", evp
,
2969 vrf_id_to_name(bgp_vrf
->vrf_id
));
2976 * Install or uninstall mac-ip routes are appropriate for this
2979 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
2983 struct bgp_dest
*rd_dest
, *dest
;
2984 struct bgp_table
*table
;
2985 struct bgp_path_info
*pi
;
2987 struct bgp
*bgp_evpn
= NULL
;
2991 bgp_evpn
= bgp_get_evpn();
2995 /* Walk entire global routing table and evaluate routes which could be
2996 * imported into this VRF. Note that we need to loop through all global
2997 * routes to determine which route matches the import rt on vrf
2999 for (rd_dest
= bgp_table_top(bgp_evpn
->rib
[afi
][safi
]); rd_dest
;
3000 rd_dest
= bgp_route_next(rd_dest
)) {
3001 table
= bgp_dest_get_bgp_table_info(rd_dest
);
3005 for (dest
= bgp_table_top(table
); dest
;
3006 dest
= bgp_route_next(dest
)) {
3007 const struct prefix_evpn
*evp
=
3008 (const struct prefix_evpn
*)bgp_dest_get_prefix(
3011 /* if not mac-ip route skip this route */
3012 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3013 || evp
->prefix
.route_type
3014 == BGP_EVPN_IP_PREFIX_ROUTE
))
3017 /* if not a mac+ip route skip this route */
3018 if (!(is_evpn_prefix_ipaddr_v4(evp
)
3019 || is_evpn_prefix_ipaddr_v6(evp
)))
3022 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
3024 ret
= bgp_evpn_route_entry_install_if_vrf_match(
3025 bgp_vrf
, pi
, install
);
3036 * Install or uninstall routes of specified type that are appropriate for this
3039 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
3040 struct bgpevpn
*vpn
,
3041 bgp_evpn_route_type rtype
,
3046 struct bgp_dest
*rd_dest
, *dest
;
3047 struct bgp_table
*table
;
3048 struct bgp_path_info
*pi
;
3054 /* Walk entire global routing table and evaluate routes which could be
3055 * imported into this VPN. Note that we cannot just look at the routes
3057 * the VNI's RD - remote routes applicable for this VNI could have any
3060 /* EVPN routes are a 2-level table. */
3061 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
3062 rd_dest
= bgp_route_next(rd_dest
)) {
3063 table
= bgp_dest_get_bgp_table_info(rd_dest
);
3067 for (dest
= bgp_table_top(table
); dest
;
3068 dest
= bgp_route_next(dest
)) {
3069 const struct prefix_evpn
*evp
=
3070 (const struct prefix_evpn
*)bgp_dest_get_prefix(
3073 if (evp
->prefix
.route_type
!= rtype
)
3076 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
3078 /* Consider "valid" remote routes applicable for
3080 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3081 && pi
->type
== ZEBRA_ROUTE_BGP
3082 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3085 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3087 ret
= install_evpn_route_entry(
3090 ret
= uninstall_evpn_route_entry(
3096 "%u: Failed to %s EVPN %s route in VNI %u",
3100 rtype
== BGP_EVPN_MAC_IP_ROUTE
3105 bgp_dest_unlock_node(rd_dest
);
3106 bgp_dest_unlock_node(dest
);
3117 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3118 * is invoked upon l3vni-add or l3vni import rt change
3120 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3122 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3127 * Install any existing remote routes applicable for this VNI into its
3128 * routing table. This is invoked when a VNI becomes "live" or its Import
3131 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3135 /* Install type-3 routes followed by type-2 routes - the ones applicable
3138 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3143 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_AD_ROUTE
,
3148 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3152 /* uninstall routes from l3vni vrf. */
3153 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3155 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3160 * Uninstall any existing remote routes for this VNI. One scenario in which
3161 * this is invoked is upon an import RT change.
3163 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3167 /* Uninstall type-2 routes followed by type-3 routes - the ones
3171 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3176 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_AD_ROUTE
,
3182 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3187 * Install or uninstall route in matching VRFs (list).
3189 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3190 safi_t safi
, struct prefix_evpn
*evp
,
3191 struct bgp_path_info
*pi
,
3192 struct list
*vrfs
, int install
)
3194 struct bgp
*bgp_vrf
;
3195 struct listnode
*node
, *nnode
;
3197 /* Only type-2/type-5 routes go into a VRF */
3198 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3199 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3202 /* if it is type-2 route and not a mac+ip route skip this route */
3203 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3204 && !(is_evpn_prefix_ipaddr_v4(evp
)
3205 || is_evpn_prefix_ipaddr_v6(evp
)))
3208 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3211 /* don't import hosts that are locally attached */
3213 && !bgp_evpn_skip_vrf_import_of_local_es(bgp_vrf
, evp
, pi
,
3215 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3217 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3221 flog_err(EC_BGP_EVPN_FAIL
,
3222 "%u: Failed to %s prefix %pFX in VRF %s",
3224 install
? "install" : "uninstall", evp
,
3225 vrf_id_to_name(bgp_vrf
->vrf_id
));
3234 * Install or uninstall route in matching VNIs (list).
3236 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3237 safi_t safi
, struct prefix_evpn
*evp
,
3238 struct bgp_path_info
*pi
,
3239 struct list
*vnis
, int install
)
3241 struct bgpevpn
*vpn
;
3242 struct listnode
*node
, *nnode
;
3244 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3247 if (!is_vni_live(vpn
))
3251 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3253 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3256 flog_err(EC_BGP_EVPN_FAIL
,
3257 "%u: Failed to %s EVPN %s route in VNI %u",
3258 bgp
->vrf_id
, install
? "install" : "uninstall",
3259 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3271 * Install or uninstall route for appropriate VNIs/ESIs.
3273 static int bgp_evpn_install_uninstall_table(struct bgp
*bgp
, afi_t afi
,
3274 safi_t safi
, const struct prefix
*p
,
3275 struct bgp_path_info
*pi
,
3276 int import
, bool in_vni_rt
,
3279 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3280 struct attr
*attr
= pi
->attr
;
3281 struct ecommunity
*ecom
;
3283 struct prefix_evpn ad_evp
;
3287 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3288 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3289 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3290 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3291 || evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
3292 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3295 /* If we don't have Route Target, nothing much to do. */
3296 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3299 /* EAD prefix in the global table doesn't include the VTEP-IP so
3300 * we need to create a different copy for the VNI
3302 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
3303 evp
= evpn_type1_prefix_vni_copy(&ad_evp
, evp
, attr
->nexthop
);
3305 ecom
= attr
->ecommunity
;
3306 if (!ecom
|| !ecom
->size
)
3309 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3310 * attached to the route */
3311 for (i
= 0; i
< ecom
->size
; i
++) {
3313 uint8_t type
, sub_type
;
3314 struct ecommunity_val
*eval
;
3315 struct ecommunity_val eval_tmp
;
3316 struct irt_node
*irt
; /* import rt for l2vni */
3317 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3318 struct bgp_evpn_es
*es
;
3320 /* Only deal with RTs */
3321 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
3322 eval
= (struct ecommunity_val
*)(ecom
->val
3323 + (i
* ecom
->unit_size
));
3326 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3329 /* non-local MAC-IP routes in the global route table are linked
3330 * to the destination ES
3332 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3333 bgp_evpn_path_es_link(pi
, 0,
3334 bgp_evpn_attr_get_esi(pi
->attr
));
3337 * macip routes (type-2) are imported into VNI and VRF tables.
3338 * IMET route is imported into VNI table.
3339 * prefix routes are imported into VRF table.
3341 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3342 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3343 evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
||
3344 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3346 irt
= in_vni_rt
? lookup_import_rt(bgp
, eval
) : NULL
;
3348 install_uninstall_route_in_vnis(
3349 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3352 vrf_irt
= in_vrf_rt
? lookup_vrf_import_rt(eval
) : NULL
;
3354 install_uninstall_route_in_vrfs(
3355 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3358 /* Also check for non-exact match.
3359 * In this, we mask out the AS and
3360 * only check on the local-admin sub-field.
3361 * This is to facilitate using
3362 * VNI as the RT for EBGP peering too.
3366 if (type
== ECOMMUNITY_ENCODE_AS
3367 || type
== ECOMMUNITY_ENCODE_AS4
3368 || type
== ECOMMUNITY_ENCODE_IP
) {
3369 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
3370 mask_ecom_global_admin(&eval_tmp
, eval
);
3372 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3375 lookup_vrf_import_rt(&eval_tmp
);
3379 install_uninstall_route_in_vnis(
3380 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3383 install_uninstall_route_in_vrfs(
3384 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3388 /* es route is imported into the es table */
3389 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3391 /* we will match based on the entire esi to avoid
3392 * imoort of an es route for esi2 into esi1
3394 es
= bgp_evpn_es_find(&evp
->prefix
.es_addr
.esi
);
3395 if (es
&& bgp_evpn_is_es_local(es
))
3396 bgp_evpn_es_route_install_uninstall(
3397 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3405 * Install or uninstall route for appropriate VNIs/ESIs.
3407 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3408 const struct prefix
*p
,
3409 struct bgp_path_info
*pi
, int import
)
3411 return bgp_evpn_install_uninstall_table(bgp
, afi
, safi
, p
, pi
, import
,
3415 void bgp_evpn_import_type2_route(struct bgp_path_info
*pi
, int import
)
3417 struct bgp
*bgp_evpn
;
3419 bgp_evpn
= bgp_get_evpn();
3423 install_uninstall_evpn_route(bgp_evpn
, AFI_L2VPN
, SAFI_EVPN
,
3424 &pi
->net
->p
, pi
, import
);
3427 /* Import the pi into vrf routing tables */
3428 void bgp_evpn_import_route_in_vrfs(struct bgp_path_info
*pi
, int import
)
3430 struct bgp
*bgp_evpn
;
3432 bgp_evpn
= bgp_get_evpn();
3436 bgp_evpn_install_uninstall_table(bgp_evpn
, AFI_L2VPN
, SAFI_EVPN
,
3437 &pi
->net
->p
, pi
, import
, false /*vpn*/,
3442 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3445 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3447 /* Delete ipv4 default route and withdraw from peers */
3448 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3449 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3450 SAFI_UNICAST
, false);
3452 /* delete all ipv4 routes and withdraw from peers */
3453 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3454 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3456 /* Delete ipv6 default route and withdraw from peers */
3457 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3458 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3459 SAFI_UNICAST
, false);
3461 /* delete all ipv6 routes and withdraw from peers */
3462 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3463 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3467 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3470 void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3472 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
3474 bgp_evpn
= bgp_get_evpn();
3478 /* update all ipv4 routes */
3479 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3480 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3482 /* update ipv4 default route and withdraw from peers */
3483 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3484 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3485 SAFI_UNICAST
, true);
3487 /* update all ipv6 routes */
3488 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3489 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3491 /* update ipv6 default route and withdraw from peers */
3492 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3493 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3494 SAFI_UNICAST
, true);
3499 * update and advertise local routes for a VRF as type-5 routes.
3500 * This is invoked upon RD change for a VRF. Note taht the processing is only
3501 * done in the global route table using the routes which already exist in the
3504 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3506 /* skip if the RD is configured */
3507 if (is_vrf_rd_configured(bgp_vrf
))
3510 /* derive the RD for the VRF based on new router-id */
3511 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3513 /* update advertise ipv4|ipv6 routes as type-5 routes */
3514 update_advertise_vrf_routes(bgp_vrf
);
3518 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3519 * This is invoked upon VRF RD change. The processing is done only from global
3522 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3524 /* skip if the RD is configured */
3525 if (is_vrf_rd_configured(bgp_vrf
))
3528 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3529 delete_withdraw_vrf_routes(bgp_vrf
);
3533 * Update and advertise local routes for a VNI. Invoked upon router-id
3534 * change. Note that the processing is done only on the global route table
3535 * using routes that already exist in the per-VNI table.
3537 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3539 struct prefix_evpn p
;
3540 struct bgp_dest
*dest
, *global_dest
;
3541 struct bgp_path_info
*pi
, *global_pi
;
3543 afi_t afi
= AFI_L2VPN
;
3544 safi_t safi
= SAFI_EVPN
;
3546 /* Locate type-3 route for VNI in the per-VNI table and use its
3547 * attributes to create and advertise the type-3 route for this VNI
3548 * in the global table.
3550 * RT-3 only if doing head-end replication
3552 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3553 == VXLAN_FLOOD_HEAD_END_REPL
) {
3554 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3555 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3556 if (!dest
) /* unexpected */
3558 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
3559 if (pi
->peer
== bgp
->peer_self
&&
3560 pi
->type
== ZEBRA_ROUTE_BGP
3561 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3563 if (!pi
) /* unexpected */
3567 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
],
3568 afi
, safi
, &p
, &vpn
->prd
);
3569 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_dest
, attr
,
3570 1, &pi
, 0, mac_mobility_seqnum(attr
),
3571 false /* setup_sync */, NULL
/* old_is_sync */);
3573 /* Schedule for processing and unlock node. */
3574 bgp_process(bgp
, global_dest
, afi
, safi
);
3575 bgp_dest_unlock_node(global_dest
);
3578 /* Now, walk this VNI's route table and use the route and its attribute
3579 * to create and schedule route in global table.
3581 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
3582 dest
= bgp_route_next(dest
)) {
3583 const struct prefix_evpn
*evp
=
3584 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
3587 * We have already processed type-3 routes.
3588 * Process only type-1 and type-2 routes here.
3590 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
3591 && evp
->prefix
.route_type
!= BGP_EVPN_AD_ROUTE
)
3594 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
3595 if (pi
->peer
== bgp
->peer_self
3596 && pi
->type
== ZEBRA_ROUTE_BGP
3597 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3602 /* Create route in global routing table using this route entry's
3606 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3608 assert(global_dest
);
3610 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3612 update_evpn_route_entry(
3613 bgp
, vpn
, afi
, safi
, global_dest
, attr
, 1,
3614 &global_pi
, 0, mac_mobility_seqnum(attr
),
3615 false /* setup_sync */, NULL
/* old_is_sync */);
3618 struct bgp_evpn_es
*es
;
3619 int route_changed
= 0;
3621 es
= bgp_evpn_es_find(&evp
->prefix
.ead_addr
.esi
);
3622 bgp_evpn_mh_route_update(bgp
, es
, vpn
, afi
, safi
,
3623 global_dest
, attr
, 1,
3624 &global_pi
, &route_changed
);
3627 /* Schedule for processing and unlock node. */
3628 bgp_process(bgp
, global_dest
, afi
, safi
);
3629 bgp_dest_unlock_node(global_dest
);
3636 * Delete (and withdraw) local routes for a VNI - only from the global
3637 * table. Invoked upon router-id change.
3639 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3642 struct prefix_evpn p
;
3643 struct bgp_dest
*global_dest
;
3644 struct bgp_path_info
*pi
;
3645 afi_t afi
= AFI_L2VPN
;
3646 safi_t safi
= SAFI_EVPN
;
3648 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3649 * for this VNI - from the global table.
3651 ret
= delete_global_type2_routes(bgp
, vpn
);
3655 /* Remove type-3 route for this VNI from global table. */
3656 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3657 global_dest
= bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3658 (const struct prefix_evpn
*)&p
, &vpn
->prd
);
3660 /* Delete route entry in the global EVPN table. */
3661 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
3663 /* Schedule for processing - withdraws to peers happen from
3667 bgp_process(bgp
, global_dest
, afi
, safi
);
3668 bgp_dest_unlock_node(global_dest
);
3672 delete_global_ead_evi_routes(bgp
, vpn
);
3677 * Handle router-id change. Update and advertise local routes corresponding
3678 * to this VNI from peers. Note that this is invoked after updating the
3679 * router-id. The routes in the per-VNI table are used to create routes in
3680 * the global table and schedule them.
3682 static void update_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3684 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3686 /* Skip VNIs with configured RD. */
3687 if (is_rd_configured(vpn
))
3690 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3691 update_advertise_vni_routes(bgp
, vpn
);
3695 * Handle router-id change. Delete and withdraw local routes corresponding
3696 * to this VNI from peers. Note that this is invoked prior to updating
3697 * the router-id and is done only on the global route table, the routes
3698 * are needed in the per-VNI table to re-advertise with new router id.
3700 static void withdraw_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3702 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3704 /* Skip VNIs with configured RD. */
3705 if (is_rd_configured(vpn
))
3708 delete_withdraw_vni_routes(bgp
, vpn
);
3712 * Create RT-3 for a VNI and schedule for processing and advertisement.
3713 * This is invoked upon flooding mode changing to head-end replication.
3715 static void create_advertise_type3(struct hash_bucket
*bucket
, void *data
)
3717 struct bgpevpn
*vpn
= bucket
->data
;
3718 struct bgp
*bgp
= data
;
3719 struct prefix_evpn p
;
3721 if (!vpn
|| !is_vni_live(vpn
) ||
3722 bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3723 != VXLAN_FLOOD_HEAD_END_REPL
)
3726 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3727 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
))
3728 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3729 "Type3 route creation failure for VNI %u", vpn
->vni
);
3733 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3734 * This is invoked upon flooding mode changing to drop BUM packets.
3736 static void delete_withdraw_type3(struct hash_bucket
*bucket
, void *data
)
3738 struct bgpevpn
*vpn
= bucket
->data
;
3739 struct bgp
*bgp
= data
;
3740 struct prefix_evpn p
;
3742 if (!vpn
|| !is_vni_live(vpn
))
3745 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3746 delete_evpn_route(bgp
, vpn
, &p
);
3750 * Process received EVPN type-2 route (advertise or withdraw).
3752 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3753 struct attr
*attr
, uint8_t *pfx
, int psize
,
3754 uint32_t addpath_id
)
3756 struct prefix_rd prd
;
3757 struct prefix_evpn p
= {};
3758 struct bgp_route_evpn evpn
= {};
3760 uint8_t macaddr_len
;
3761 /* holds the VNI(s) as in packet */
3762 mpls_label_t label
[BGP_MAX_LABELS
] = {};
3763 uint32_t num_labels
= 0;
3767 /* Type-2 route should be either 33, 37 or 49 bytes or an
3768 * additional 3 bytes if there is a second label (VNI):
3769 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3770 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3771 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3773 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3774 && psize
!= 40 && psize
!= 52) {
3775 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3776 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3777 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3781 struct stream
*pkt
= stream_new(psize
);
3782 stream_put(pkt
, pfx
, psize
);
3784 /* Make prefix_rd */
3785 prd
.family
= AF_UNSPEC
;
3788 STREAM_GET(&prd
.val
, pkt
, 8);
3790 /* Make EVPN prefix. */
3792 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3793 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3795 /* Copy Ethernet Seg Identifier */
3797 STREAM_GET(&attr
->esi
, pkt
, sizeof(esi_t
));
3799 if (bgp_evpn_is_esi_local_and_non_bypass(&attr
->esi
))
3800 attr
->es_flags
|= ATTR_ES_IS_LOCAL
;
3802 attr
->es_flags
&= ~ATTR_ES_IS_LOCAL
;
3804 STREAM_FORWARD_GETP(pkt
, sizeof(esi_t
));
3807 /* Copy Ethernet Tag */
3808 STREAM_GET(ð_tag
, pkt
, 4);
3809 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3811 /* Get the MAC Addr len */
3812 STREAM_GETC(pkt
, macaddr_len
);
3814 /* Get the MAC Addr */
3815 if (macaddr_len
== (ETH_ALEN
* 8)) {
3816 STREAM_GET(&p
.prefix
.macip_addr
.mac
.octet
, pkt
, ETH_ALEN
);
3819 EC_BGP_EVPN_ROUTE_INVALID
,
3820 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3821 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3827 STREAM_GETC(pkt
, ipaddr_len
);
3829 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3830 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3832 EC_BGP_EVPN_ROUTE_INVALID
,
3833 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3834 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3839 ipaddr_len
/= 8; /* Convert to bytes. */
3840 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3843 STREAM_GET(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pkt
, ipaddr_len
);
3846 /* Get the VNI(s). Stored as bytes here. */
3847 STREAM_GET(&label
[0], pkt
, BGP_LABEL_BYTES
);
3850 /* Do we have a second VNI? */
3851 if (STREAM_READABLE(pkt
)) {
3853 STREAM_GET(&label
[1], pkt
, BGP_LABEL_BYTES
);
3856 /* Process the route. */
3858 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3859 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3860 &prd
, &label
[0], num_labels
, 0, &evpn
);
3862 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3863 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3864 &prd
, &label
[0], num_labels
, &evpn
);
3869 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3870 "%u:%s - Rx EVPN Type-2 NLRI - corrupt, discarding",
3871 peer
->bgp
->vrf_id
, peer
->host
);
3879 * Process received EVPN type-3 route (advertise or withdraw).
3881 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3882 struct attr
*attr
, uint8_t *pfx
, int psize
,
3883 uint32_t addpath_id
)
3885 struct prefix_rd prd
;
3886 struct prefix_evpn p
;
3891 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3892 * IP len (1) and IP (4 or 16).
3894 if (psize
!= 17 && psize
!= 29) {
3895 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3896 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3897 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3901 /* If PMSI is present, log if it is anything other than IR.
3902 * Note: We just simply ignore the values as it is not clear if
3903 * doing anything else is better.
3906 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3907 enum pta_type pmsi_tnl_type
= bgp_attr_get_pmsi_tnl_type(attr
);
3909 if (pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
3910 && pmsi_tnl_type
!= PMSI_TNLTYPE_PIM_SM
) {
3912 EC_BGP_EVPN_PMSI_PRESENT
,
3913 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3914 peer
->bgp
->vrf_id
, peer
->host
, pmsi_tnl_type
);
3918 /* Make prefix_rd */
3919 prd
.family
= AF_UNSPEC
;
3921 memcpy(&prd
.val
, pfx
, 8);
3924 /* Make EVPN prefix. */
3925 memset(&p
, 0, sizeof(struct prefix_evpn
));
3927 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3928 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3930 /* Copy Ethernet Tag */
3931 memcpy(ð_tag
, pfx
, 4);
3932 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
3936 ipaddr_len
= *pfx
++;
3937 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3938 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
3939 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
3942 EC_BGP_EVPN_ROUTE_INVALID
,
3943 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3944 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3948 /* Process the route. */
3950 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3951 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3952 &prd
, NULL
, 0, 0, NULL
);
3954 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3955 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3956 &prd
, NULL
, 0, NULL
);
3961 * Process received EVPN type-5 route (advertise or withdraw).
3963 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3964 struct attr
*attr
, uint8_t *pfx
, int psize
,
3965 uint32_t addpath_id
)
3967 struct prefix_rd prd
;
3968 struct prefix_evpn p
;
3969 struct bgp_route_evpn evpn
;
3972 mpls_label_t label
; /* holds the VNI as in the packet */
3975 bool is_valid_update
= true;
3977 /* Type-5 route should be 34 or 58 bytes:
3978 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3979 * GW (4 or 16) and VNI (3).
3980 * Note that the IP and GW should both be IPv4 or both IPv6.
3982 if (psize
!= 34 && psize
!= 58) {
3983 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3984 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3985 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3989 /* Make prefix_rd */
3990 prd
.family
= AF_UNSPEC
;
3992 memcpy(&prd
.val
, pfx
, 8);
3995 /* Make EVPN prefix. */
3996 memset(&p
, 0, sizeof(struct prefix_evpn
));
3998 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3999 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
4001 /* Additional information outside of prefix - ESI and GW IP */
4002 memset(&evpn
, 0, sizeof(evpn
));
4004 /* Fetch ESI overlay index */
4006 memcpy(&evpn
.eth_s_id
, pfx
, sizeof(esi_t
));
4009 /* Fetch Ethernet Tag. */
4010 memcpy(ð_tag
, pfx
, 4);
4011 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
4014 /* Fetch IP prefix length. */
4016 if (ippfx_len
> IPV6_MAX_BITLEN
) {
4018 EC_BGP_EVPN_ROUTE_INVALID
,
4019 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4020 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
4023 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
4025 /* Determine IPv4 or IPv6 prefix */
4026 /* Since the address and GW are from the same family, this just becomes
4027 * a simple check on the total size.
4030 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
4031 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
4033 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
4037 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
4038 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
4040 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
4045 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4046 memset(&label
, 0, sizeof(label
));
4047 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
4050 * If in future, we are required to access additional fields,
4051 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4056 * An update containing a non-zero gateway IP and a non-zero ESI
4057 * at the same time is should be treated as withdraw
4059 if (bgp_evpn_is_esi_valid(&evpn
.eth_s_id
)
4060 && !is_zero_gw_ip(&evpn
.gw_ip
, gw_afi
)) {
4061 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4062 "%s - Rx EVPN Type-5 ESI and gateway-IP both non-zero.",
4064 is_valid_update
= false;
4065 } else if (bgp_evpn_is_esi_valid(&evpn
.eth_s_id
))
4066 evpn
.type
= OVERLAY_INDEX_ESI
;
4067 else if (!is_zero_gw_ip(&evpn
.gw_ip
, gw_afi
))
4068 evpn
.type
= OVERLAY_INDEX_GATEWAY_IP
;
4070 if (is_zero_mac(&attr
->rmac
)
4071 && !bgp_evpn_is_esi_valid(&evpn
.eth_s_id
)
4072 && is_zero_gw_ip(&evpn
.gw_ip
, gw_afi
) && label
== 0) {
4073 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4074 "%s - Rx EVPN Type-5 ESI, gateway-IP, RMAC and label all zero",
4076 is_valid_update
= false;
4079 if (is_mcast_mac(&attr
->rmac
) || is_bcast_mac(&attr
->rmac
))
4080 is_valid_update
= false;
4083 /* Process the route. */
4084 if (attr
&& is_valid_update
)
4085 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4086 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4087 &prd
, &label
, 1, 0, &evpn
);
4089 if (!is_valid_update
) {
4090 char attr_str
[BUFSIZ
] = {0};
4092 bgp_dump_attr(attr
, attr_str
, BUFSIZ
);
4094 "Invalid update from peer %s vrf %u prefix %pFX attr %s - treat as withdraw",
4095 peer
->hostname
, peer
->bgp
->vrf_id
, &p
,
4098 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4099 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4100 &prd
, &label
, 1, &evpn
);
4106 static void evpn_mpattr_encode_type5(struct stream
*s
, const struct prefix
*p
,
4107 const struct prefix_rd
*prd
,
4108 mpls_label_t
*label
, uint32_t num_labels
,
4113 const struct evpn_addr
*p_evpn_p
;
4115 memset(&temp
, 0, 16);
4116 if (p
->family
!= AF_EVPN
)
4118 p_evpn_p
= &(p
->u
.prefix_evpn
);
4120 /* len denites the total len of IP and GW-IP in the route
4121 IP and GW-IP have to be both ipv4 or ipv6
4123 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4124 len
= 8; /* IP and GWIP are both ipv4 */
4126 len
= 32; /* IP and GWIP are both ipv6 */
4127 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4128 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4129 stream_put(s
, prd
->val
, 8);
4130 if (attr
&& attr
->evpn_overlay
.type
== OVERLAY_INDEX_ESI
)
4131 stream_put(s
, &attr
->esi
, sizeof(esi_t
));
4133 stream_put(s
, 0, sizeof(esi_t
));
4134 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4135 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4136 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4137 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4139 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4140 if (attr
&& attr
->evpn_overlay
.type
== OVERLAY_INDEX_GATEWAY_IP
) {
4141 const struct bgp_route_evpn
*evpn_overlay
=
4142 bgp_attr_get_evpn_overlay(attr
);
4144 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4145 stream_put_ipv4(s
, evpn_overlay
->gw_ip
.ipv4
.s_addr
);
4147 stream_put(s
, &(evpn_overlay
->gw_ip
.ipv6
), 16);
4149 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4150 stream_put_ipv4(s
, 0);
4152 stream_put(s
, &temp
, 16);
4156 stream_put(s
, label
, 3);
4162 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4164 static void cleanup_vni_on_disable(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4166 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4168 /* Remove EVPN routes and schedule for processing. */
4169 delete_routes_for_vni(bgp
, vpn
);
4171 /* Clear "live" flag and see if hash needs to be freed. */
4172 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4173 if (!is_vni_configured(vpn
))
4174 bgp_evpn_free(bgp
, vpn
);
4178 * Free a VNI entry; iterator function called during cleanup.
4180 static void free_vni_entry(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4182 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4184 delete_all_vni_routes(bgp
, vpn
);
4185 bgp_evpn_free(bgp
, vpn
);
4189 * Derive AUTO import RT for BGP VRF - L3VNI
4191 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4193 struct bgp
*bgp_evpn
= NULL
;
4195 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4196 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4199 bgp_evpn
= bgp_get_evpn();
4202 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4206 * Delete AUTO import RT from BGP VRF - L3VNI
4208 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4210 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4214 * Derive AUTO export RT for BGP VRF - L3VNI
4216 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4218 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4219 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4223 * Delete AUTO export RT from BGP VRF - L3VNI
4225 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4227 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4230 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4232 struct bgp
*bgp_evpn
= NULL
;
4233 struct listnode
*node
= NULL
;
4234 struct bgpevpn
*vpn
= NULL
;
4236 bgp_evpn
= bgp_get_evpn();
4240 /* update all type-5 routes */
4241 update_advertise_vrf_routes(bgp_vrf
);
4243 /* update all type-2 routes */
4244 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4245 update_routes_for_vni(bgp_evpn
, vpn
);
4249 * Handle autort change for a given VNI.
4251 static void update_autort_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4253 struct bgpevpn
*vpn
= bucket
->data
;
4255 if (!is_import_rt_configured(vpn
)) {
4256 if (is_vni_live(vpn
))
4257 bgp_evpn_uninstall_routes(bgp
, vpn
);
4258 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4259 list_delete_all_node(vpn
->import_rtl
);
4260 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4261 if (is_vni_live(vpn
))
4262 bgp_evpn_install_routes(bgp
, vpn
);
4264 if (!is_export_rt_configured(vpn
)) {
4265 list_delete_all_node(vpn
->export_rtl
);
4266 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4267 if (is_vni_live(vpn
))
4268 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4276 /* withdraw type-5 route corresponding to ip prefix */
4277 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4278 afi_t afi
, safi_t safi
)
4281 struct prefix_evpn evp
;
4283 build_type5_prefix_from_ip_prefix(&evp
, p
);
4284 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4287 EC_BGP_EVPN_ROUTE_DELETE
,
4288 "%u failed to delete type-5 route for prefix %pFX in vrf %s",
4289 bgp_vrf
->vrf_id
, p
, vrf_id_to_name(bgp_vrf
->vrf_id
));
4292 /* withdraw all type-5 routes for an address family */
4293 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4295 struct bgp_table
*table
= NULL
;
4296 struct bgp_dest
*dest
= NULL
;
4297 struct bgp_path_info
*pi
;
4299 table
= bgp_vrf
->rib
[afi
][safi
];
4300 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
4301 /* Only care about "selected" routes. Also ensure that
4302 * these are routes that are injectable into EVPN.
4304 /* TODO: Support for AddPath for EVPN. */
4305 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
4306 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4307 && is_route_injectable_into_evpn(pi
)) {
4308 bgp_evpn_withdraw_type5_route(
4309 bgp_vrf
, bgp_dest_get_prefix(dest
), afi
,
4318 * evpn - enable advertisement of default g/w
4320 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
4321 safi_t safi
, bool add
)
4323 struct prefix ip_prefix
;
4325 /* form the default prefix 0.0.0.0/0 */
4326 memset(&ip_prefix
, 0, sizeof(struct prefix
));
4327 ip_prefix
.family
= afi2family(afi
);
4330 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
4333 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
4340 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4341 * to this function correspond to those of the source IP prefix (best
4342 * path in the case of the attr. In the case of a local prefix (when we
4343 * are advertising local subnets), the src_attr will be NULL.
4345 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4346 struct attr
*src_attr
, afi_t afi
,
4350 struct prefix_evpn evp
;
4352 build_type5_prefix_from_ip_prefix(&evp
, p
);
4353 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
, afi
, safi
);
4355 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4356 "%u: Failed to create type-5 route for prefix %pFX",
4357 bgp_vrf
->vrf_id
, p
);
4360 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4361 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4362 * advertisement is enabled.
4364 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4367 struct bgp_table
*table
= NULL
;
4368 struct bgp_dest
*dest
= NULL
;
4369 struct bgp_path_info
*pi
;
4371 table
= bgp_vrf
->rib
[afi
][safi
];
4372 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
4373 /* Need to identify the "selected" route entry to use its
4374 * attribute. Also, ensure that the route is injectable
4376 * TODO: Support for AddPath for EVPN.
4378 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
4379 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4380 && is_route_injectable_into_evpn(pi
)) {
4382 /* apply the route-map */
4383 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4384 route_map_result_t ret
;
4385 struct bgp_path_info tmp_pi
;
4386 struct bgp_path_info_extra tmp_pie
;
4387 struct attr tmp_attr
;
4389 tmp_attr
= *pi
->attr
;
4391 /* Fill temp path_info */
4392 prep_for_rmap_apply(&tmp_pi
, &tmp_pie
,
4396 RESET_FLAG(tmp_attr
.rmap_change_flags
);
4398 ret
= route_map_apply(
4399 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4401 bgp_dest_get_prefix(dest
),
4403 if (ret
== RMAP_DENYMATCH
) {
4404 bgp_attr_flush(&tmp_attr
);
4407 bgp_evpn_advertise_type5_route(
4409 bgp_dest_get_prefix(dest
),
4410 &tmp_attr
, afi
, safi
);
4412 bgp_evpn_advertise_type5_route(
4414 bgp_dest_get_prefix(dest
),
4415 pi
->attr
, afi
, safi
);
4422 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4424 struct listnode
*node
, *nnode
, *node_to_del
;
4425 struct ecommunity
*ecom
, *ecom_auto
;
4426 struct ecommunity_val eval
;
4428 if (bgp
->advertise_autort_rfc8365
)
4429 vni
|= EVPN_AUTORT_VXLAN
;
4430 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4432 ecom_auto
= ecommunity_new();
4433 ecommunity_add_val(ecom_auto
, &eval
, false, false);
4436 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4437 if (ecommunity_match(ecom
, ecom_auto
)) {
4438 ecommunity_free(&ecom
);
4444 list_delete_node(rtl
, node_to_del
);
4446 ecommunity_free(&ecom_auto
);
4449 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4450 struct ecommunity
*ecomadd
)
4452 /* uninstall routes from vrf */
4453 if (is_l3vni_live(bgp_vrf
))
4454 uninstall_routes_for_vrf(bgp_vrf
);
4456 /* Cleanup the RT to VRF mapping */
4457 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4459 /* Remove auto generated RT */
4460 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4462 /* Add the newly configured RT to RT list */
4463 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4464 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4466 /* map VRF to its RTs and install routes matching the new RTs */
4467 if (is_l3vni_live(bgp_vrf
)) {
4468 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4469 install_routes_for_vrf(bgp_vrf
);
4473 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4474 struct ecommunity
*ecomdel
)
4476 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4477 struct ecommunity
*ecom
= NULL
;
4479 /* uninstall routes from vrf */
4480 if (is_l3vni_live(bgp_vrf
))
4481 uninstall_routes_for_vrf(bgp_vrf
);
4483 /* Cleanup the RT to VRF mapping */
4484 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4486 /* remove the RT from the RT list */
4487 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4488 if (ecommunity_match(ecom
, ecomdel
)) {
4489 ecommunity_free(&ecom
);
4496 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4498 assert(bgp_vrf
->vrf_import_rtl
);
4499 /* fallback to auto import rt, if this was the last RT */
4500 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4501 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4502 if (is_l3vni_live(bgp_vrf
))
4503 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4506 /* map VRFs to its RTs and install routes matching this new RT */
4507 if (is_l3vni_live(bgp_vrf
)) {
4508 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4509 install_routes_for_vrf(bgp_vrf
);
4513 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4514 struct ecommunity
*ecomadd
)
4516 /* remove auto-generated RT */
4517 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4519 /* Add the new RT to the RT list */
4520 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4521 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4523 if (is_l3vni_live(bgp_vrf
))
4524 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4527 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4528 struct ecommunity
*ecomdel
)
4530 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4531 struct ecommunity
*ecom
= NULL
;
4533 /* Remove the RT from the RT list */
4534 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4535 if (ecommunity_match(ecom
, ecomdel
)) {
4536 ecommunity_free(&ecom
);
4543 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4546 * Temporary assert to make SA happy.
4547 * The ALL_LIST_ELEMENTS macro above has a NULL check
4548 * which means that SA is going to complain about
4549 * the list_isempty call, which doesn't NULL check.
4550 * So until we get this situation cleaned up, here
4553 assert(bgp_vrf
->vrf_export_rtl
);
4555 /* fall back to auto-generated RT if this was the last RT */
4556 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4557 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4558 if (is_l3vni_live(bgp_vrf
))
4559 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4562 if (is_l3vni_live(bgp_vrf
))
4563 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4567 * Handle change to BGP router id. This is invoked twice by the change
4568 * handler, first before the router id has been changed and then after
4569 * the router id has been changed. The first invocation will result in
4570 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4571 * will result in the routes being re-advertised.
4573 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4575 struct listnode
*node
;
4576 struct bgp
*bgp_vrf
;
4580 /* delete and withdraw all the type-5 routes
4581 stored in the global table for this vrf
4583 withdraw_router_id_vrf(bgp
);
4585 /* delete all the VNI routes (type-2/type-3) routes for all the
4588 hash_iterate(bgp
->vnihash
,
4589 (void (*)(struct hash_bucket
*,
4590 void *))withdraw_router_id_vni
,
4593 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4594 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4595 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4596 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4598 bgp_vrf
->evpn_info
->pip_ip
.s_addr
4604 /* Assign new default instance router-id */
4605 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4606 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4607 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4608 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4610 bgp_vrf
->evpn_info
->pip_ip
=
4612 /* advertise type-5 routes with
4615 update_advertise_vrf_routes(bgp_vrf
);
4620 /* advertise all routes in the vrf as type-5 routes with the new
4623 update_router_id_vrf(bgp
);
4625 /* advertise all the VNI routes (type-2/type-3) routes with the
4628 hash_iterate(bgp
->vnihash
,
4629 (void (*)(struct hash_bucket
*,
4630 void *))update_router_id_vni
,
4636 * Handle change to auto-RT algorithm - update and advertise local routes.
4638 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4640 hash_iterate(bgp
->vnihash
,
4641 (void (*)(struct hash_bucket
*,
4642 void*))update_autort_vni
,
4647 * Handle change to export RT - update and advertise local routes.
4649 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4651 return update_routes_for_vni(bgp
, vpn
);
4654 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4657 delete_withdraw_vrf_routes(bgp_vrf
);
4659 update_advertise_vrf_routes(bgp_vrf
);
4663 * Handle change to RD. This is invoked twice by the change handler,
4664 * first before the RD has been changed and then after the RD has
4665 * been changed. The first invocation will result in local routes
4666 * of this VNI being deleted and withdrawn and the next will result
4667 * in the routes being re-advertised.
4669 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4673 delete_withdraw_vni_routes(bgp
, vpn
);
4675 update_advertise_vni_routes(bgp
, vpn
);
4679 * Install routes for this VNI. Invoked upon change to Import RT.
4681 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4683 return install_routes_for_vni(bgp
, vpn
);
4687 * Uninstall all routes installed for this VNI. Invoked upon change
4690 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4692 return uninstall_routes_for_vni(bgp
, vpn
);
4696 * TODO: Hardcoded for a maximum of 2 VNIs right now
4698 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4703 vni1
= label2vni(label
);
4704 if (num_labels
== 2) {
4705 vni2
= label2vni(label
+ 1);
4706 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4708 snprintf(buf
, len
, "%u", vni1
);
4713 * Function to convert evpn route to json format.
4714 * NOTE: We don't use prefix2str as the output here is a bit different.
4716 void bgp_evpn_route2json(const struct prefix_evpn
*p
, json_object
*json
)
4718 char buf1
[ETHER_ADDR_STRLEN
];
4719 char buf2
[PREFIX2STR_BUFFER
];
4726 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4728 switch (p
->prefix
.route_type
) {
4729 case BGP_EVPN_MAC_IP_ROUTE
:
4730 json_object_int_add(json
, "ethTag",
4731 p
->prefix
.macip_addr
.eth_tag
);
4732 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4733 json_object_string_add(json
, "mac",
4734 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4737 if (!is_evpn_prefix_ipaddr_none(p
)) {
4738 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
:
4740 prefixlen
= (family
== AF_INET
) ?
4741 IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
;
4742 inet_ntop(family
, &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4743 buf2
, PREFIX2STR_BUFFER
);
4744 json_object_int_add(json
, "ipLen", prefixlen
);
4745 json_object_string_add(json
, "ip", buf2
);
4749 case BGP_EVPN_IMET_ROUTE
:
4750 json_object_int_add(json
, "ethTag",
4751 p
->prefix
.imet_addr
.eth_tag
);
4752 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4753 prefixlen
= (family
== AF_INET
) ? IPV4_MAX_BITLEN
:
4755 inet_ntop(family
, &p
->prefix
.imet_addr
.ip
.ip
.addr
, buf2
,
4757 json_object_int_add(json
, "ipLen", prefixlen
);
4758 json_object_string_add(json
, "ip", buf2
);
4761 case BGP_EVPN_IP_PREFIX_ROUTE
:
4762 json_object_int_add(json
, "ethTag",
4763 p
->prefix
.prefix_addr
.eth_tag
);
4764 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4765 inet_ntop(family
, &p
->prefix
.prefix_addr
.ip
.ip
.addr
,
4766 buf2
, sizeof(buf2
));
4767 json_object_int_add(json
, "ipLen",
4768 p
->prefix
.prefix_addr
.ip_prefix_length
);
4769 json_object_string_add(json
, "ip", buf2
);
4778 * Encode EVPN prefix in Update (MP_REACH)
4780 void bgp_evpn_encode_prefix(struct stream
*s
, const struct prefix
*p
,
4781 const struct prefix_rd
*prd
, mpls_label_t
*label
,
4782 uint32_t num_labels
, struct attr
*attr
,
4783 int addpath_encode
, uint32_t addpath_tx_id
)
4785 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4786 int len
, ipa_len
= 0;
4789 stream_putl(s
, addpath_tx_id
);
4792 stream_putc(s
, evp
->prefix
.route_type
);
4794 switch (evp
->prefix
.route_type
) {
4795 case BGP_EVPN_MAC_IP_ROUTE
:
4796 if (is_evpn_prefix_ipaddr_v4(evp
))
4797 ipa_len
= IPV4_MAX_BYTELEN
;
4798 else if (is_evpn_prefix_ipaddr_v6(evp
))
4799 ipa_len
= IPV6_MAX_BYTELEN
;
4800 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4801 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4802 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4804 stream_putc(s
, len
);
4805 stream_put(s
, prd
->val
, 8); /* RD */
4807 stream_put(s
, &attr
->esi
, ESI_BYTES
);
4809 stream_put(s
, 0, 10);
4810 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4811 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4812 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4813 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4814 if (ipa_len
) /* IP */
4815 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4817 /* 1st label is the L2 VNI */
4818 stream_put(s
, label
, BGP_LABEL_BYTES
);
4819 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4820 if (ipa_len
&& num_labels
> 1)
4821 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4824 case BGP_EVPN_IMET_ROUTE
:
4825 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4826 stream_put(s
, prd
->val
, 8); /* RD */
4827 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4828 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4829 /* Originating Router's IP Addr */
4830 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4833 case BGP_EVPN_ES_ROUTE
:
4834 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4835 stream_put(s
, prd
->val
, 8); /* RD */
4836 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4837 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4839 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4842 case BGP_EVPN_AD_ROUTE
:
4843 /* RD, ESI, EthTag, 1 VNI */
4844 len
= RD_BYTES
+ ESI_BYTES
+ EVPN_ETH_TAG_BYTES
+ BGP_LABEL_BYTES
;
4845 stream_putc(s
, len
);
4846 stream_put(s
, prd
->val
, RD_BYTES
); /* RD */
4847 stream_put(s
, evp
->prefix
.ead_addr
.esi
.val
, ESI_BYTES
); /* ESI */
4848 stream_putl(s
, evp
->prefix
.ead_addr
.eth_tag
); /* Ethernet Tag */
4849 stream_put(s
, label
, BGP_LABEL_BYTES
);
4852 case BGP_EVPN_IP_PREFIX_ROUTE
:
4853 /* TODO: AddPath support. */
4854 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4862 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4863 struct bgp_nlri
*packet
, int withdraw
)
4869 uint32_t addpath_id
;
4870 int addpath_encoded
;
4875 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4877 lim
= pnt
+ packet
->length
;
4879 safi
= packet
->safi
;
4883 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
4884 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
4885 PEER_CAP_ADDPATH_AF_TX_RCV
));
4887 for (; pnt
< lim
; pnt
+= psize
) {
4888 /* Clear prefix structure. */
4889 memset(&p
, 0, sizeof(struct prefix
));
4891 /* Deal with path-id if AddPath is supported. */
4892 if (addpath_encoded
) {
4893 /* When packet overflow occurs return immediately. */
4894 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4895 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
4897 memcpy(&addpath_id
, pnt
, BGP_ADDPATH_ID_LEN
);
4898 addpath_id
= ntohl(addpath_id
);
4899 pnt
+= BGP_ADDPATH_ID_LEN
;
4902 /* All EVPN NLRI types start with type and length. */
4904 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE
;
4909 /* When packet overflow occur return immediately. */
4910 if (pnt
+ psize
> lim
)
4911 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
4914 case BGP_EVPN_MAC_IP_ROUTE
:
4915 if (process_type2_route(peer
, afi
, safi
,
4916 withdraw
? NULL
: attr
, pnt
,
4917 psize
, addpath_id
)) {
4920 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4921 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4922 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE
;
4926 case BGP_EVPN_IMET_ROUTE
:
4927 if (process_type3_route(peer
, afi
, safi
,
4928 withdraw
? NULL
: attr
, pnt
,
4929 psize
, addpath_id
)) {
4932 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4933 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4934 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE
;
4938 case BGP_EVPN_ES_ROUTE
:
4939 if (bgp_evpn_type4_route_process(peer
, afi
, safi
,
4940 withdraw
? NULL
: attr
, pnt
,
4941 psize
, addpath_id
)) {
4944 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4945 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4946 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE
;
4950 case BGP_EVPN_AD_ROUTE
:
4951 if (bgp_evpn_type1_route_process(peer
, afi
, safi
,
4952 withdraw
? NULL
: attr
, pnt
,
4953 psize
, addpath_id
)) {
4956 "%u:%s - Error in processing EVPN type-1 NLRI size %d",
4957 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4958 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE1_SIZE
;
4962 case BGP_EVPN_IP_PREFIX_ROUTE
:
4963 if (process_type5_route(peer
, afi
, safi
,
4964 withdraw
? NULL
: attr
, pnt
,
4965 psize
, addpath_id
)) {
4968 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4969 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4970 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE
;
4979 /* Packet length consistency check. */
4981 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH
;
4983 return BGP_NLRI_PARSE_OK
;
4987 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
4988 * The mapping will be used during route processing.
4989 * bgp_def: default bgp instance
4990 * bgp_vrf: specific bgp vrf instance on which RT is configured
4992 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
4995 struct ecommunity_val
*eval
= NULL
;
4996 struct listnode
*node
= NULL
, *nnode
= NULL
;
4997 struct ecommunity
*ecom
= NULL
;
4999 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5000 for (i
= 0; i
< ecom
->size
; i
++) {
5001 eval
= (struct ecommunity_val
*)(ecom
->val
5003 * ECOMMUNITY_SIZE
));
5004 map_vrf_to_rt(bgp_vrf
, eval
);
5010 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
5012 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
5015 struct ecommunity_val
*eval
;
5016 struct listnode
*node
, *nnode
;
5017 struct ecommunity
*ecom
;
5019 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5020 for (i
= 0; i
< ecom
->size
; i
++) {
5021 struct vrf_irt_node
*irt
;
5022 struct ecommunity_val eval_tmp
;
5024 eval
= (struct ecommunity_val
*)(ecom
->val
5026 * ECOMMUNITY_SIZE
));
5027 /* If using "automatic" RT, we only care about the
5028 * local-admin sub-field.
5029 * This is to facilitate using VNI as the RT for EBGP
5032 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5033 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
5034 BGP_VRF_IMPORT_RT_CFGD
))
5035 mask_ecom_global_admin(&eval_tmp
, eval
);
5037 irt
= lookup_vrf_import_rt(&eval_tmp
);
5039 unmap_vrf_from_rt(bgp_vrf
, irt
);
5046 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
5047 * The mapping will be used during route processing.
5049 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5052 struct ecommunity_val
*eval
;
5053 struct listnode
*node
, *nnode
;
5054 struct ecommunity
*ecom
;
5056 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5057 for (i
= 0; i
< ecom
->size
; i
++) {
5058 eval
= (struct ecommunity_val
*)(ecom
->val
5060 * ECOMMUNITY_SIZE
));
5061 map_vni_to_rt(bgp
, vpn
, eval
);
5067 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
5069 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5072 struct ecommunity_val
*eval
;
5073 struct listnode
*node
, *nnode
;
5074 struct ecommunity
*ecom
;
5076 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5077 for (i
= 0; i
< ecom
->size
; i
++) {
5078 struct irt_node
*irt
;
5079 struct ecommunity_val eval_tmp
;
5081 eval
= (struct ecommunity_val
*)(ecom
->val
5083 * ECOMMUNITY_SIZE
));
5084 /* If using "automatic" RT, we only care about the
5085 * local-admin sub-field.
5086 * This is to facilitate using VNI as the RT for EBGP
5089 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5090 if (!is_import_rt_configured(vpn
))
5091 mask_ecom_global_admin(&eval_tmp
, eval
);
5093 irt
= lookup_import_rt(bgp
, &eval_tmp
);
5095 unmap_vni_from_rt(bgp
, vpn
, irt
);
5101 * Derive Import RT automatically for VNI and map VNI to RT.
5102 * The mapping will be used during route processing.
5104 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5106 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
5107 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
5110 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
5114 * Derive Export RT automatically for VNI.
5116 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5118 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
5119 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5123 * Derive RD automatically for VNI using passed information - it
5124 * is of the form RouterId:unique-id-for-vni.
5126 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5128 if (is_vrf_rd_configured(bgp
))
5131 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5135 * Derive RD automatically for VNI using passed information - it
5136 * is of the form RouterId:unique-id-for-vni.
5138 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5140 char buf
[BGP_EVPN_PREFIX_RD_LEN
];
5142 vpn
->prd
.family
= AF_UNSPEC
;
5143 vpn
->prd
.prefixlen
= 64;
5144 snprintfrr(buf
, sizeof(buf
), "%pI4:%hu", &bgp
->router_id
, vpn
->rd_id
);
5145 (void)str2prefix_rd(buf
, &vpn
->prd
);
5146 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5152 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5154 struct list
*inst
= bm
->bgp
;
5155 struct listnode
*node
;
5156 struct bgp
*bgp_vrf
;
5158 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5159 if (bgp_vrf
->l3vni
== vni
)
5169 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5171 struct bgpevpn
*vpn
;
5174 memset(&tmp
, 0, sizeof(struct bgpevpn
));
5176 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5181 * Create a new vpn - invoked upon configuration or zebra notification.
5183 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5184 struct in_addr originator_ip
,
5185 vrf_id_t tenant_vrf_id
,
5186 struct in_addr mcast_grp
)
5188 struct bgpevpn
*vpn
;
5193 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5195 /* Set values - RD and RT set to defaults. */
5197 vpn
->originator_ip
= originator_ip
;
5198 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5199 vpn
->mcast_grp
= mcast_grp
;
5201 /* Initialize route-target import and export lists */
5202 vpn
->import_rtl
= list_new();
5203 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5204 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
5205 vpn
->export_rtl
= list_new();
5206 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5207 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
5208 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5209 derive_rd_rt_for_vni(bgp
, vpn
);
5211 /* Initialize EVPN route table. */
5212 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5215 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
5216 XFREE(MTYPE_BGP_EVPN
, vpn
);
5220 /* add to l2vni list on corresponding vrf */
5221 bgpevpn_link_to_l3vni(vpn
);
5223 bgp_evpn_vni_es_init(vpn
);
5225 QOBJ_REG(vpn
, bgpevpn
);
5230 * Free a given VPN - called in multiple scenarios such as zebra
5231 * notification, configuration being deleted, advertise-all-vni disabled etc.
5232 * This just frees appropriate memory, caller should have taken other
5235 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5237 bgp_evpn_vni_es_cleanup(vpn
);
5238 bgpevpn_unlink_from_l3vni(vpn
);
5239 bgp_table_unlock(vpn
->route_table
);
5240 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5241 list_delete(&vpn
->import_rtl
);
5242 list_delete(&vpn
->export_rtl
);
5243 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5244 hash_release(bgp
->vnihash
, vpn
);
5246 XFREE(MTYPE_BGP_EVPN
, vpn
);
5250 * Import evpn route from global table to VNI/VRF/ESI.
5252 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5253 const struct prefix
*p
, struct bgp_path_info
*pi
)
5255 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5259 * Unimport evpn route from VNI/VRF/ESI.
5261 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5262 const struct prefix
*p
, struct bgp_path_info
*pi
)
5264 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5267 /* filter routes which have martian next hops */
5268 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5272 struct bgp_dest
*rd_dest
, *dest
;
5273 struct bgp_table
*table
;
5274 struct bgp_path_info
*pi
;
5279 /* Walk entire global routing table and evaluate routes which could be
5280 * imported into this VPN. Note that we cannot just look at the routes
5281 * for the VNI's RD -
5282 * remote routes applicable for this VNI could have any RD.
5284 /* EVPN routes are a 2-level table. */
5285 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
5286 rd_dest
= bgp_route_next(rd_dest
)) {
5287 table
= bgp_dest_get_bgp_table_info(rd_dest
);
5291 for (dest
= bgp_table_top(table
); dest
;
5292 dest
= bgp_route_next(dest
)) {
5294 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
5297 /* Consider "valid" remote routes applicable for
5299 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5300 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5302 if (bgp_nexthop_self(bgp
, afi
, pi
->type
,
5303 pi
->sub_type
, pi
->attr
,
5305 const struct prefix
*p
=
5306 bgp_dest_get_prefix(dest
);
5308 if (bgp_debug_update(pi
->peer
, p
, NULL
,
5310 char attr_str
[BUFSIZ
] = {0};
5312 bgp_dump_attr(pi
->attr
,
5317 "%u: prefix %pBD with attr %s - DENIED due to martian or self nexthop",
5321 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5324 bgp_rib_remove(dest
, pi
, pi
->peer
, afi
,
5335 * Handle del of a local MACIP.
5337 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5338 struct ipaddr
*ip
, int state
)
5340 struct bgpevpn
*vpn
;
5341 struct prefix_evpn p
;
5342 struct bgp_dest
*dest
;
5344 /* Lookup VNI hash - should exist. */
5345 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5346 if (!vpn
|| !is_vni_live(vpn
)) {
5347 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5348 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5349 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5353 build_evpn_type2_prefix(&p
, mac
, ip
);
5354 if (state
== ZEBRA_NEIGH_ACTIVE
) {
5355 /* Remove EVPN type-2 route and schedule for processing. */
5356 delete_evpn_route(bgp
, vpn
, &p
);
5358 /* Re-instate the current remote best path if any */
5359 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
5361 evpn_zebra_reinstall_best_route(bgp
, vpn
, dest
);
5368 * Handle add of a local MACIP.
5370 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5371 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
, esi_t
*esi
)
5373 struct bgpevpn
*vpn
;
5374 struct prefix_evpn p
;
5376 /* Lookup VNI hash - should exist. */
5377 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5378 if (!vpn
|| !is_vni_live(vpn
)) {
5379 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5380 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5381 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5385 /* Create EVPN type-2 route and schedule for processing. */
5386 build_evpn_type2_prefix(&p
, mac
, ip
);
5387 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
, esi
)) {
5389 EC_BGP_EVPN_ROUTE_CREATE
,
5390 "%u:Failed to create Type-2 route, VNI %u %s MAC %pEA IP %pIA (flags: 0x%x)",
5391 bgp
->vrf_id
, vpn
->vni
,
5392 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5402 static void link_l2vni_hash_to_l3vni(struct hash_bucket
*bucket
,
5403 struct bgp
*bgp_vrf
)
5405 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
5406 struct bgp
*bgp_evpn
= NULL
;
5408 bgp_evpn
= bgp_get_evpn();
5411 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5412 bgpevpn_link_to_l3vni(vpn
);
5415 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
,
5416 struct ethaddr
*svi_rmac
,
5417 struct ethaddr
*vrr_rmac
,
5418 struct in_addr originator_ip
, int filter
,
5419 ifindex_t svi_ifindex
,
5420 bool is_anycast_mac
)
5422 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5423 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5424 struct listnode
*node
= NULL
;
5425 struct bgpevpn
*vpn
= NULL
;
5428 /* get the EVPN instance - required to get the AS number for VRF
5431 bgp_evpn
= bgp_get_evpn();
5435 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
5441 /* if the BGP vrf instance doesn't exist - create one */
5442 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5447 ret
= bgp_get_vty(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5448 vrf_id
== VRF_DEFAULT
5449 ? BGP_INSTANCE_TYPE_DEFAULT
5450 : BGP_INSTANCE_TYPE_VRF
);
5452 case BGP_ERR_AS_MISMATCH
:
5453 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5454 "BGP instance is already running; AS is %u",
5457 case BGP_ERR_INSTANCE_MISMATCH
:
5458 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5459 "BGP instance type mismatch");
5463 /* mark as auto created */
5464 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5467 /* associate the vrf with l3vni and related parameters */
5468 bgp_vrf
->l3vni
= l3vni
;
5469 bgp_vrf
->originator_ip
= originator_ip
;
5470 bgp_vrf
->l3vni_svi_ifindex
= svi_ifindex
;
5471 bgp_vrf
->evpn_info
->is_anycast_mac
= is_anycast_mac
;
5473 /* copy anycast MAC from VRR MAC */
5474 memcpy(&bgp_vrf
->rmac
, vrr_rmac
, ETH_ALEN
);
5475 /* copy sys RMAC from SVI MAC */
5476 memcpy(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, svi_rmac
, ETH_ALEN
);
5477 /* PIP user configured mac is not present use svi mac as sys mac */
5478 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
))
5479 memcpy(&bgp_vrf
->evpn_info
->pip_rmac
, svi_rmac
, ETH_ALEN
);
5481 if (bgp_debug_zebra(NULL
))
5483 "VRF %s vni %u pip %s RMAC %pEA sys RMAC %pEA static RMAC %pEA is_anycast_mac %s",
5484 vrf_id_to_name(bgp_vrf
->vrf_id
), bgp_vrf
->l3vni
,
5485 bgp_vrf
->evpn_info
->advertise_pip
? "enable"
5487 &bgp_vrf
->rmac
, &bgp_vrf
->evpn_info
->pip_rmac
,
5488 &bgp_vrf
->evpn_info
->pip_rmac_static
,
5489 is_anycast_mac
? "Enable" : "Disable");
5491 /* set the right filter - are we using l3vni only for prefix routes? */
5493 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5496 * VNI_FLAG_USE_TWO_LABELS flag for linked L2VNIs should not be
5497 * set before linking vrf to L3VNI. Thus, no need to clear
5501 UNSET_FLAG(bgp_vrf
->vrf_flags
,
5502 BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5504 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5505 if (!CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
5508 * If we are flapping VNI_FLAG_USE_TWO_LABELS
5509 * flag, update all MACIP routes in this VNI
5511 SET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5512 update_all_type2_routes(bgp_evpn
, vpn
);
5517 /* Map auto derive or configured RTs */
5518 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5519 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5521 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
5523 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5524 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5526 /* auto derive RD */
5527 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5529 /* link all corresponding l2vnis */
5530 hash_iterate(bgp_evpn
->vnihash
,
5531 (void (*)(struct hash_bucket
*,
5532 void *))link_l2vni_hash_to_l3vni
,
5535 /* Only update all corresponding type-2 routes if we are advertising two
5536 * labels along with type-2 routes
5539 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5540 update_routes_for_vni(bgp_evpn
, vpn
);
5542 /* advertise type-5 routes if needed */
5543 update_advertise_vrf_routes(bgp_vrf
);
5545 /* install all remote routes belonging to this l3vni into correspondng
5547 install_routes_for_vrf(bgp_vrf
);
5552 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5554 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5555 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5556 struct listnode
*node
= NULL
;
5557 struct listnode
*next
= NULL
;
5558 struct bgpevpn
*vpn
= NULL
;
5560 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5564 "Cannot process L3VNI %u Del - Could not find BGP instance",
5569 bgp_evpn
= bgp_get_evpn();
5573 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
5578 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5579 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5580 * routes. This will uninstalling the routes from zebra and decremnt the
5583 uninstall_routes_for_vrf(bgp_vrf
);
5585 /* delete/withdraw all type-5 routes */
5586 delete_withdraw_vrf_routes(bgp_vrf
);
5588 /* remove the l3vni from vrf instance */
5591 /* remove the Rmac from the BGP vrf */
5592 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5593 memset(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, 0, ETH_ALEN
);
5594 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
) &&
5595 !is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac
))
5596 memset(&bgp_vrf
->evpn_info
->pip_rmac
, 0, ETH_ALEN
);
5598 /* remove default import RT or Unmap non-default import RT */
5599 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5600 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5601 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5602 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5605 /* remove default export RT */
5606 if (!list_isempty(bgp_vrf
->vrf_export_rtl
) &&
5607 !CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
5608 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5611 /* update all corresponding local mac-ip routes */
5612 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5613 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5614 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5615 update_routes_for_vni(bgp_evpn
, vpn
);
5619 /* If any L2VNIs point to this instance, unlink them. */
5620 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5621 bgpevpn_unlink_from_l3vni(vpn
);
5623 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5625 /* Delete the instance if it was autocreated */
5626 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5627 bgp_delete(bgp_vrf
);
5633 * Handle del of a local VNI.
5635 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5637 struct bgpevpn
*vpn
;
5639 /* Locate VNI hash */
5640 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5642 if (bgp_debug_zebra(NULL
))
5644 EC_BGP_EVPN_VPN_VNI
,
5645 "%u: VNI hash entry for VNI %u not found at DEL",
5650 /* Remove all local EVPN routes and schedule for processing (to
5651 * withdraw from peers).
5653 delete_routes_for_vni(bgp
, vpn
);
5656 * tunnel is no longer active, del tunnel ip address from tip_hash
5658 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5660 /* Clear "live" flag and see if hash needs to be freed. */
5661 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5662 if (!is_vni_configured(vpn
))
5663 bgp_evpn_free(bgp
, vpn
);
5669 * Handle add (or update) of a local VNI. The VNI changes we care
5670 * about are for the local-tunnel-ip and the (tenant) VRF.
5672 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5673 struct in_addr originator_ip
,
5674 vrf_id_t tenant_vrf_id
,
5675 struct in_addr mcast_grp
)
5678 struct bgpevpn
*vpn
;
5679 struct prefix_evpn p
;
5681 /* Lookup VNI. If present and no change, exit. */
5682 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5685 if (is_vni_live(vpn
)
5686 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5687 && IPV4_ADDR_SAME(&vpn
->mcast_grp
, &mcast_grp
)
5688 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5689 /* Probably some other param has changed that we don't
5693 bgp_evpn_mcast_grp_change(bgp
, vpn
, mcast_grp
);
5695 /* Update tenant_vrf_id if it has changed. */
5696 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5697 bgpevpn_unlink_from_l3vni(vpn
);
5698 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5699 bgpevpn_link_to_l3vni(vpn
);
5702 /* If tunnel endpoint IP has changed, update (and delete prior
5703 * type-3 route, if needed.)
5705 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5706 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5708 /* Update all routes with new endpoint IP and/or export RT
5711 if (is_vni_live(vpn
))
5712 update_routes_for_vni(bgp
, vpn
);
5715 /* Create or update as appropriate. */
5717 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
,
5722 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5728 /* if the VNI is live already, there is nothing more to do */
5729 if (is_vni_live(vpn
))
5732 /* Mark as "live" */
5733 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5735 /* tunnel is now active, add tunnel-ip to db */
5736 bgp_tip_add(bgp
, &originator_ip
);
5738 /* filter routes as nexthop database has changed */
5739 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5742 * Create EVPN type-3 route and schedule for processing.
5744 * RT-3 only if doing head-end replication
5746 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
5747 == VXLAN_FLOOD_HEAD_END_REPL
) {
5748 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5749 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
)) {
5750 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5751 "%u: Type3 route creation failure for VNI %u",
5757 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5761 install_routes_for_vni(bgp
, vpn
);
5763 /* If we are advertising gateway mac-ip
5764 It needs to be conveyed again to zebra */
5765 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5767 /* advertise svi mac-ip knob to zebra */
5768 bgp_zebra_advertise_svi_macip(bgp
, vpn
->advertise_svi_macip
, vpn
->vni
);
5774 * Handle change in setting for BUM handling. The supported values
5775 * are head-end replication and dropping all BUM packets. Any change
5776 * should be registered with zebra. Also, if doing head-end replication,
5777 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5778 * to be dropped, the RT-3s must be withdrawn.
5780 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
5782 zlog_info("L2VPN EVPN BUM handling is %s",
5783 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
5784 "Flooding" : "Flooding Disabled");
5786 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
5787 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
5788 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
5789 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
5790 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
5794 * Cleanup EVPN information on disable - Need to delete and withdraw
5795 * EVPN routes from peers.
5797 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5799 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_bucket
*,
5800 void *))cleanup_vni_on_disable
,
5805 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5806 * BGP instance (default) is being freed.
5808 void bgp_evpn_cleanup(struct bgp
*bgp
)
5810 hash_iterate(bgp
->vnihash
,
5811 (void (*)(struct hash_bucket
*, void *))free_vni_entry
,
5814 hash_free(bgp
->import_rt_hash
);
5815 bgp
->import_rt_hash
= NULL
;
5817 hash_free(bgp
->vrf_import_rt_hash
);
5818 bgp
->vrf_import_rt_hash
= NULL
;
5820 hash_free(bgp
->vnihash
);
5821 bgp
->vnihash
= NULL
;
5823 list_delete(&bgp
->vrf_import_rtl
);
5824 list_delete(&bgp
->vrf_export_rtl
);
5825 list_delete(&bgp
->l2vnis
);
5829 * Initialization for EVPN
5832 * hash for RT to VNI
5834 void bgp_evpn_init(struct bgp
*bgp
)
5837 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
5838 bgp
->import_rt_hash
=
5839 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
5840 "BGP Import RT Hash");
5841 bgp
->vrf_import_rt_hash
=
5842 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
5843 "BGP VRF Import RT Hash");
5844 bgp
->vrf_import_rtl
= list_new();
5845 bgp
->vrf_import_rtl
->cmp
=
5846 (int (*)(void *, void *))evpn_route_target_cmp
;
5847 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
5848 bgp
->vrf_export_rtl
= list_new();
5849 bgp
->vrf_export_rtl
->cmp
=
5850 (int (*)(void *, void *))evpn_route_target_cmp
;
5851 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
5852 bgp
->l2vnis
= list_new();
5853 bgp
->l2vnis
->cmp
= vni_list_cmp
;
5854 /* By default Duplicate Address Dection is enabled.
5855 * Max-moves (N) 5, detection time (M) 180
5856 * default action is warning-only
5857 * freeze action permanently freezes address,
5858 * and freeze time (auto-recovery) is disabled.
5860 if (bgp
->evpn_info
) {
5861 bgp
->evpn_info
->dup_addr_detect
= true;
5862 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
5863 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
5864 bgp
->evpn_info
->dad_freeze
= false;
5865 bgp
->evpn_info
->dad_freeze_time
= 0;
5866 /* Initialize zebra vxlan */
5867 bgp_zebra_dup_addr_detection(bgp
);
5868 /* Enable PIP feature by default for bgp vrf instance */
5869 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
5870 struct bgp
*bgp_default
;
5872 bgp
->evpn_info
->advertise_pip
= true;
5873 bgp_default
= bgp_get_default();
5875 bgp
->evpn_info
->pip_ip
= bgp_default
->router_id
;
5879 /* Default BUM handling is to do head-end replication. */
5880 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
5882 bgp_evpn_nh_init(bgp
);
5885 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
5887 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5888 bgp_evpn_nh_finish(bgp_vrf
);
5892 * Get the prefixlen of the ip prefix carried within the type5 evpn route.
5894 int bgp_evpn_get_type5_prefixlen(const struct prefix
*pfx
)
5896 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
5898 if (!pfx
|| pfx
->family
!= AF_EVPN
)
5901 if (evp
->prefix
.route_type
!= BGP_EVPN_IP_PREFIX_ROUTE
)
5904 return evp
->prefix
.prefix_addr
.ip_prefix_length
;
5908 * Should we register nexthop for this EVPN prefix for nexthop tracking?
5910 bool bgp_evpn_is_prefix_nht_supported(const struct prefix
*pfx
)
5912 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
5915 * EVPN routes should be marked as valid only if the nexthop is
5916 * reachable. Only if this happens, the route should be imported
5917 * (into VNI or VRF routing tables) and/or advertised.
5918 * Note: This is currently applied for EVPN type-2, type-3 and
5919 * type-5 routes. It may be tweaked later on for other routes, or
5920 * even removed completely when all routes are handled.
5922 if (pfx
&& pfx
->family
== AF_EVPN
&&
5923 (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
5924 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
5925 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))