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"
56 #include "bgpd/bgp_nht.h"
57 #include "bgpd/bgp_trace.h"
60 * Definitions and external declarations.
62 DEFINE_QOBJ_TYPE(bgpevpn
);
63 DEFINE_QOBJ_TYPE(bgp_evpn_es
);
67 * Static function declarations
69 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
70 static void bgp_evpn_remote_ip_hash_init(struct bgpevpn
*evpn
);
71 static void bgp_evpn_remote_ip_hash_destroy(struct bgpevpn
*evpn
);
72 static void bgp_evpn_remote_ip_hash_add(struct bgpevpn
*vpn
,
73 struct bgp_path_info
*pi
);
74 static void bgp_evpn_remote_ip_hash_del(struct bgpevpn
*vpn
,
75 struct bgp_path_info
*pi
);
76 static void bgp_evpn_remote_ip_hash_iterate(struct bgpevpn
*vpn
,
77 void (*func
)(struct hash_bucket
*,
80 static void bgp_evpn_link_to_vni_svi_hash(struct bgp
*bgp
, struct bgpevpn
*vpn
);
81 static void bgp_evpn_unlink_from_vni_svi_hash(struct bgp
*bgp
,
83 static unsigned int vni_svi_hash_key_make(const void *p
);
84 static bool vni_svi_hash_cmp(const void *p1
, const void *p2
);
85 static void bgp_evpn_remote_ip_process_nexthops(struct bgpevpn
*vpn
,
88 static void bgp_evpn_remote_ip_hash_link_nexthop(struct hash_bucket
*bucket
,
90 static void bgp_evpn_remote_ip_hash_unlink_nexthop(struct hash_bucket
*bucket
,
92 static struct in_addr zero_vtep_ip
;
101 static unsigned int vni_hash_key_make(const void *p
)
103 const struct bgpevpn
*vpn
= p
;
104 return (jhash_1word(vpn
->vni
, 0));
108 * Comparison function for vni hash
110 static bool vni_hash_cmp(const void *p1
, const void *p2
)
112 const struct bgpevpn
*vpn1
= p1
;
113 const struct bgpevpn
*vpn2
= p2
;
115 return vpn1
->vni
== vpn2
->vni
;
118 int vni_list_cmp(void *p1
, void *p2
)
120 const struct bgpevpn
*vpn1
= p1
;
121 const struct bgpevpn
*vpn2
= p2
;
123 return vpn1
->vni
- vpn2
->vni
;
127 * Make vrf import route target hash key.
129 static unsigned int vrf_import_rt_hash_key_make(const void *p
)
131 const struct vrf_irt_node
*irt
= p
;
132 const char *pnt
= irt
->rt
.val
;
134 return jhash(pnt
, 8, 0x5abc1234);
138 * Comparison function for vrf import rt hash
140 static bool vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
142 const struct vrf_irt_node
*irt1
= p1
;
143 const struct vrf_irt_node
*irt2
= p2
;
145 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
149 * Create a new vrf import_rt in evpn instance
151 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
153 struct bgp
*bgp_evpn
= NULL
;
154 struct vrf_irt_node
*irt
;
156 bgp_evpn
= bgp_get_evpn();
158 flog_err(EC_BGP_NO_DFLT
,
159 "vrf import rt new - evpn instance not created yet");
163 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
164 sizeof(struct vrf_irt_node
));
167 irt
->vrfs
= list_new();
170 (void)hash_get(bgp_evpn
->vrf_import_rt_hash
, irt
, hash_alloc_intern
);
176 * Free the vrf import rt node
178 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
180 struct bgp
*bgp_evpn
= NULL
;
182 bgp_evpn
= bgp_get_evpn();
184 flog_err(EC_BGP_NO_DFLT
,
185 "vrf import rt free - evpn instance not created yet");
189 hash_release(bgp_evpn
->vrf_import_rt_hash
, irt
);
190 list_delete(&irt
->vrfs
);
191 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
194 static void hash_vrf_import_rt_free(struct vrf_irt_node
*irt
)
196 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
200 * Function to lookup Import RT node - used to map a RT to set of
201 * VNIs importing routes with that RT.
203 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
205 struct bgp
*bgp_evpn
= NULL
;
206 struct vrf_irt_node
*irt
;
207 struct vrf_irt_node tmp
;
209 bgp_evpn
= bgp_get_evpn();
213 "vrf import rt lookup - evpn instance not created yet");
217 memset(&tmp
, 0, sizeof(tmp
));
218 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
219 irt
= hash_lookup(bgp_evpn
->vrf_import_rt_hash
, &tmp
);
224 * Is specified VRF present on the RT's list of "importing" VRFs?
226 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
, struct bgp
*bgp_vrf
)
228 struct listnode
*node
= NULL
, *nnode
= NULL
;
229 struct bgp
*tmp_bgp_vrf
= NULL
;
231 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
232 if (tmp_bgp_vrf
== bgp_vrf
)
239 * Make import route target hash key.
241 static unsigned int import_rt_hash_key_make(const void *p
)
243 const struct irt_node
*irt
= p
;
244 const char *pnt
= irt
->rt
.val
;
246 return jhash(pnt
, 8, 0xdeadbeef);
250 * Comparison function for import rt hash
252 static bool import_rt_hash_cmp(const void *p1
, const void *p2
)
254 const struct irt_node
*irt1
= p1
;
255 const struct irt_node
*irt2
= p2
;
257 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
261 * Create a new import_rt
263 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
264 struct ecommunity_val
*rt
)
266 struct irt_node
*irt
;
268 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
271 irt
->vnis
= list_new();
274 (void)hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
);
280 * Free the import rt node
282 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
284 hash_release(bgp
->import_rt_hash
, irt
);
285 list_delete(&irt
->vnis
);
286 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
289 static void hash_import_rt_free(struct irt_node
*irt
)
291 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
295 * Function to lookup Import RT node - used to map a RT to set of
296 * VNIs importing routes with that RT.
298 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
299 struct ecommunity_val
*rt
)
301 struct irt_node
*irt
;
304 memset(&tmp
, 0, sizeof(tmp
));
305 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
306 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
311 * Is specified VNI present on the RT's list of "importing" VNIs?
313 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
315 struct listnode
*node
, *nnode
;
316 struct bgpevpn
*tmp_vpn
;
318 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
327 * Compare Route Targets.
329 int bgp_evpn_route_target_cmp(struct ecommunity
*ecom1
,
330 struct ecommunity
*ecom2
)
338 if (!ecom1
&& !ecom2
)
341 if (ecom1
->str
&& !ecom2
->str
)
344 if (!ecom1
->str
&& ecom2
->str
)
347 if (!ecom1
->str
&& !ecom2
->str
)
350 return strcmp(ecom1
->str
, ecom2
->str
);
353 void bgp_evpn_xxport_delete_ecomm(void *val
)
355 struct ecommunity
*ecomm
= val
;
356 ecommunity_free(&ecomm
);
360 * Mask off global-admin field of specified extended community (RT),
361 * just retain the local-admin field.
363 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
364 struct ecommunity_val
*src
)
370 if (type
== ECOMMUNITY_ENCODE_AS
) {
371 dst
->val
[2] = dst
->val
[3] = 0;
372 } else if (type
== ECOMMUNITY_ENCODE_AS4
373 || type
== ECOMMUNITY_ENCODE_IP
) {
374 dst
->val
[2] = dst
->val
[3] = 0;
375 dst
->val
[4] = dst
->val
[5] = 0;
380 * Map one RT to specified VRF.
381 * bgp_vrf = BGP vrf instance
383 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct ecommunity_val
*eval
)
385 struct vrf_irt_node
*irt
= NULL
;
386 struct ecommunity_val eval_tmp
;
388 /* If using "automatic" RT,
389 * we only care about the local-admin sub-field.
390 * This is to facilitate using L3VNI(VRF-VNI)
391 * as the RT for EBGP peering too.
393 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
394 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
395 mask_ecom_global_admin(&eval_tmp
, eval
);
397 irt
= lookup_vrf_import_rt(&eval_tmp
);
398 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
399 /* Already mapped. */
403 irt
= vrf_import_rt_new(&eval_tmp
);
405 /* Add VRF to the list for this RT. */
406 listnode_add(irt
->vrfs
, bgp_vrf
);
410 * Unmap specified VRF from specified RT. If there are no other
411 * VRFs for this RT, then the RT hash is deleted.
412 * bgp_vrf: BGP VRF specific instance
414 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
, struct vrf_irt_node
*irt
)
416 /* Delete VRF from list for this RT. */
417 listnode_delete(irt
->vrfs
, bgp_vrf
);
418 if (!listnode_head(irt
->vrfs
)) {
419 vrf_import_rt_free(irt
);
424 * Map one RT to specified VNI.
426 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
427 struct ecommunity_val
*eval
)
429 struct irt_node
*irt
;
430 struct ecommunity_val eval_tmp
;
432 /* If using "automatic" RT, we only care about the local-admin
434 * This is to facilitate using VNI as the RT for EBGP peering too.
436 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
437 if (!is_import_rt_configured(vpn
))
438 mask_ecom_global_admin(&eval_tmp
, eval
);
440 irt
= lookup_import_rt(bgp
, &eval_tmp
);
442 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
443 /* Already mapped. */
447 irt
= import_rt_new(bgp
, &eval_tmp
);
449 /* Add VNI to the hash list for this RT. */
450 listnode_add(irt
->vnis
, vpn
);
454 * Unmap specified VNI from specified RT. If there are no other
455 * VNIs for this RT, then the RT hash is deleted.
457 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
458 struct irt_node
*irt
)
460 /* Delete VNI from hash list for this RT. */
461 listnode_delete(irt
->vnis
, vpn
);
462 if (!listnode_head(irt
->vnis
)) {
463 import_rt_free(bgp
, irt
);
467 static void bgp_evpn_get_rmac_nexthop(struct bgpevpn
*vpn
,
468 const struct prefix_evpn
*p
,
469 struct attr
*attr
, uint8_t flags
)
471 struct bgp
*bgp_vrf
= vpn
->bgp_vrf
;
473 memset(&attr
->rmac
, 0, sizeof(struct ethaddr
));
477 if (p
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
480 /* Copy sys (pip) RMAC and PIP IP as nexthop
481 * in case of route is self MAC-IP,
482 * advertise-pip and advertise-svi-ip features
484 * Otherwise, for all host MAC-IP route's
487 if (CHECK_FLAG(flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
)
488 && bgp_vrf
->evpn_info
->advertise_pip
&&
489 bgp_vrf
->evpn_info
->is_anycast_mac
) {
491 memcpy(&attr
->rmac
, &bgp_vrf
->evpn_info
->pip_rmac
,
493 attr
->nexthop
= bgp_vrf
->evpn_info
->pip_ip
;
494 attr
->mp_nexthop_global_in
=
495 bgp_vrf
->evpn_info
->pip_ip
;
497 memcpy(&attr
->rmac
, &bgp_vrf
->rmac
, ETH_ALEN
);
501 * Create RT extended community automatically from passed information:
502 * of the form AS:VNI.
503 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
504 * the need is to get a RT value that will be unique across different
505 * VNIs but the same across routers (in the same AS) for a particular
508 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
510 struct ecommunity_val eval
;
511 struct ecommunity
*ecomadd
, *ecom
;
512 bool ecom_found
= false;
513 struct listnode
*node
;
515 if (bgp
->advertise_autort_rfc8365
)
516 vni
|= EVPN_AUTORT_VXLAN
;
517 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
519 ecomadd
= ecommunity_new();
520 ecommunity_add_val(ecomadd
, &eval
, false, false);
521 for (ALL_LIST_ELEMENTS_RO(rtl
, node
, ecom
))
522 if (ecommunity_cmp(ecomadd
, ecom
)) {
528 listnode_add_sort(rtl
, ecomadd
);
530 ecommunity_free(&ecomadd
);
534 * Derive RD and RT for a VNI automatically. Invoked at the time of
537 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
539 bgp_evpn_derive_auto_rd(bgp
, vpn
);
540 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
541 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
545 * Convert nexthop (remote VTEP IP) into an IPv6 address.
547 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
549 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
551 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
552 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
555 struct bgp_dest
*bgp_global_evpn_node_get(struct bgp_table
*table
, afi_t afi
,
557 const struct prefix_evpn
*evp
,
558 struct prefix_rd
*prd
)
560 struct prefix_evpn global_p
;
562 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
) {
563 /* prefix in the global table doesn't include the VTEP-IP so
564 * we need to create a different copy of the prefix
566 evpn_type1_prefix_global_copy(&global_p
, evp
);
569 return bgp_afi_node_get(table
, afi
, safi
, (struct prefix
*)evp
, prd
);
572 struct bgp_dest
*bgp_global_evpn_node_lookup(struct bgp_table
*table
, afi_t afi
,
574 const struct prefix_evpn
*evp
,
575 struct prefix_rd
*prd
)
577 struct prefix_evpn global_p
;
579 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
) {
580 /* prefix in the global table doesn't include the VTEP-IP so
581 * we need to create a different copy of the prefix
583 evpn_type1_prefix_global_copy(&global_p
, evp
);
586 return bgp_afi_node_lookup(table
, afi
, safi
, (struct prefix
*)evp
, prd
);
590 * Add (update) or delete MACIP from zebra.
592 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
593 const struct prefix_evpn
*p
,
594 struct in_addr remote_vtep_ip
, int add
,
595 uint8_t flags
, uint32_t seq
, esi_t
*esi
)
599 static struct in_addr zero_remote_vtep_ip
;
602 if (!zclient
|| zclient
->sock
< 0)
605 /* Don't try to register if Zebra doesn't know of this instance. */
606 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
607 if (BGP_DEBUG(zebra
, ZEBRA
))
609 "%s: No zebra instance to talk to, not installing remote macip",
619 zclient_create_header(
620 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
622 stream_putl(s
, vpn
->vni
);
623 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
624 /* IP address length and IP address, if any. */
625 if (is_evpn_prefix_ipaddr_none(p
))
628 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
630 stream_putw(s
, ipa_len
);
631 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
633 /* If the ESI is valid that becomes the nexthop; tape out the
634 * VTEP-IP for that case
636 if (bgp_evpn_is_esi_valid(esi
))
637 stream_put_in_addr(s
, &zero_remote_vtep_ip
);
639 stream_put_in_addr(s
, &remote_vtep_ip
);
641 /* TX flags - MAC sticky status and/or gateway mac */
642 /* Also TX the sequence number of the best route. */
644 stream_putc(s
, flags
);
646 stream_put(s
, esi
, sizeof(esi_t
));
649 stream_putw_at(s
, 0, stream_get_endp(s
));
651 if (bgp_debug_zebra(NULL
))
653 "Tx %s MACIP, VNI %u MAC %pEA IP %pIA flags 0x%x seq %u remote VTEP %pI4",
654 add
? "ADD" : "DEL", vpn
->vni
,
655 &p
->prefix
.macip_addr
.mac
, &p
->prefix
.macip_addr
.ip
,
656 flags
, seq
, &remote_vtep_ip
);
658 frrtrace(5, frr_bgp
, evpn_mac_ip_zsend
, add
, vpn
, p
, remote_vtep_ip
,
661 return zclient_send_message(zclient
);
665 * Add (update) or delete remote VTEP from zebra.
667 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
668 const struct prefix_evpn
*p
,
669 int flood_control
, int add
)
674 if (!zclient
|| zclient
->sock
< 0)
677 /* Don't try to register if Zebra doesn't know of this instance. */
678 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
679 if (BGP_DEBUG(zebra
, ZEBRA
))
681 "%s: No zebra instance to talk to, not installing remote vtep",
689 zclient_create_header(
690 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
692 stream_putl(s
, vpn
->vni
);
693 if (is_evpn_prefix_ipaddr_v4(p
))
694 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
695 else if (is_evpn_prefix_ipaddr_v6(p
)) {
698 "Bad remote IP when trying to %s remote VTEP for VNI %u",
699 add
? "ADD" : "DEL", vpn
->vni
);
702 stream_putl(s
, flood_control
);
704 stream_putw_at(s
, 0, stream_get_endp(s
));
706 if (bgp_debug_zebra(NULL
))
707 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %pI4",
708 add
? "ADD" : "DEL", vpn
->vni
,
709 &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
711 frrtrace(3, frr_bgp
, evpn_bum_vtep_zsend
, add
, vpn
, p
);
713 return zclient_send_message(zclient
);
717 * Build extended communities for EVPN prefix route.
719 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
722 struct ecommunity ecom_encap
;
723 struct ecommunity_val eval
;
724 struct ecommunity_val eval_rmac
;
725 bgp_encap_types tnl_type
;
726 struct listnode
*node
, *nnode
;
727 struct ecommunity
*ecom
;
728 struct ecommunity
*old_ecom
;
729 struct list
*vrf_export_rtl
= NULL
;
732 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
733 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
734 encode_encap_extcomm(tnl_type
, &eval
);
736 ecom_encap
.unit_size
= ECOMMUNITY_SIZE
;
737 ecom_encap
.val
= (uint8_t *)eval
.val
;
740 if (bgp_attr_get_ecommunity(attr
)) {
741 old_ecom
= bgp_attr_get_ecommunity(attr
);
742 ecom
= ecommunity_merge(ecommunity_dup(old_ecom
), &ecom_encap
);
743 if (!old_ecom
->refcnt
)
744 ecommunity_free(&old_ecom
);
746 ecom
= ecommunity_dup(&ecom_encap
);
747 bgp_attr_set_ecommunity(attr
, ecom
);
748 attr
->encap_tunneltype
= tnl_type
;
750 /* Add the export RTs for L3VNI/VRF */
751 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
752 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
753 bgp_attr_set_ecommunity(
755 ecommunity_merge(bgp_attr_get_ecommunity(attr
), ecom
));
757 /* add the router mac extended community */
758 if (!is_zero_mac(&attr
->rmac
)) {
759 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
760 ecommunity_add_val(bgp_attr_get_ecommunity(attr
), &eval_rmac
,
766 * Build extended communities for EVPN route.
767 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
768 * and ENCAP extended communities are applicable for all routes.
769 * The default gateway extended community and MAC mobility (sticky) extended
770 * community are added as needed based on passed settings - only for type-2
771 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
772 * added, if present, based on passed settings - only for non-link-local
775 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
778 struct ecommunity ecom_encap
;
779 struct ecommunity ecom_sticky
;
780 struct ecommunity ecom_default_gw
;
781 struct ecommunity ecom_na
;
782 struct ecommunity_val eval
;
783 struct ecommunity_val eval_sticky
;
784 struct ecommunity_val eval_default_gw
;
785 struct ecommunity_val eval_rmac
;
786 struct ecommunity_val eval_na
;
789 bgp_encap_types tnl_type
;
790 struct listnode
*node
, *nnode
;
791 struct ecommunity
*ecom
;
793 struct list
*vrf_export_rtl
= NULL
;
796 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
797 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
798 encode_encap_extcomm(tnl_type
, &eval
);
800 ecom_encap
.unit_size
= ECOMMUNITY_SIZE
;
801 ecom_encap
.val
= (uint8_t *)eval
.val
;
804 bgp_attr_set_ecommunity(attr
, ecommunity_dup(&ecom_encap
));
805 attr
->encap_tunneltype
= tnl_type
;
807 /* Add the export RTs for L2VNI */
808 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
809 bgp_attr_set_ecommunity(
811 ecommunity_merge(bgp_attr_get_ecommunity(attr
), ecom
));
813 /* Add the export RTs for L3VNI if told to - caller determines
814 * when this should be done.
817 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
818 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
819 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
821 bgp_attr_set_ecommunity(
824 bgp_attr_get_ecommunity(attr
),
829 /* Add MAC mobility (sticky) if needed. */
832 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
833 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
834 ecom_sticky
.size
= 1;
835 ecom_sticky
.unit_size
= ECOMMUNITY_SIZE
;
836 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
837 bgp_attr_set_ecommunity(
838 attr
, ecommunity_merge(bgp_attr_get_ecommunity(attr
),
842 /* Add RMAC, if told to. */
844 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
845 ecommunity_add_val(bgp_attr_get_ecommunity(attr
), &eval_rmac
,
849 /* Add default gateway, if needed. */
850 if (attr
->default_gw
) {
851 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
852 encode_default_gw_extcomm(&eval_default_gw
);
853 ecom_default_gw
.size
= 1;
854 ecom_default_gw
.unit_size
= ECOMMUNITY_SIZE
;
855 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
856 bgp_attr_set_ecommunity(
857 attr
, ecommunity_merge(bgp_attr_get_ecommunity(attr
),
861 proxy
= !!(attr
->es_flags
& ATTR_ES_PROXY_ADVERT
);
862 if (attr
->router_flag
|| proxy
) {
863 memset(&ecom_na
, 0, sizeof(ecom_na
));
864 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
, proxy
);
866 ecom_na
.unit_size
= ECOMMUNITY_SIZE
;
867 ecom_na
.val
= (uint8_t *)eval_na
.val
;
868 bgp_attr_set_ecommunity(
869 attr
, ecommunity_merge(bgp_attr_get_ecommunity(attr
),
875 * Add MAC mobility extended community to attribute.
877 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
879 struct ecommunity ecom_tmp
;
880 struct ecommunity_val eval
;
881 uint8_t *ecom_val_ptr
;
886 struct ecommunity
*ecomm
= bgp_attr_get_ecommunity(attr
);
889 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
891 /* Find current MM ecommunity */
895 for (i
= 0; i
< ecomm
->size
; i
++) {
896 pnt
= ecomm
->val
+ (i
* ecomm
->unit_size
);
900 if (type
== ECOMMUNITY_ENCODE_EVPN
902 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
904 (ecomm
->val
+ (i
* ecomm
->unit_size
));
910 /* Update the existing MM ecommunity */
912 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ecomm
->unit_size
);
914 /* Add MM to existing */
916 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
918 ecom_tmp
.unit_size
= ECOMMUNITY_SIZE
;
919 ecom_tmp
.val
= (uint8_t *)eval
.val
;
922 bgp_attr_set_ecommunity(
923 attr
, ecommunity_merge(ecomm
, &ecom_tmp
));
925 bgp_attr_set_ecommunity(attr
,
926 ecommunity_dup(&ecom_tmp
));
930 /* Install EVPN route into zebra. */
931 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
932 const struct prefix_evpn
*p
,
933 struct bgp_path_info
*pi
)
940 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
943 if (pi
->sub_type
== BGP_ROUTE_IMPORTED
) {
944 if (pi
->attr
->sticky
)
945 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
946 if (pi
->attr
->default_gw
)
947 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
948 if (is_evpn_prefix_ipaddr_v6(p
) &&
949 pi
->attr
->router_flag
)
950 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
952 seq
= mac_mobility_seqnum(pi
->attr
);
953 /* if local ES notify zebra that this is a sync path */
954 if (bgp_evpn_attr_is_local_es(pi
->attr
)) {
955 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_SYNC_PATH
);
956 if (bgp_evpn_attr_is_proxy(pi
->attr
))
958 ZEBRA_MACIP_TYPE_PROXY_ADVERT
);
961 if (!bgp_evpn_attr_is_sync(pi
->attr
))
964 /* if a local path is being turned around and sent
965 * to zebra it is because it is a sync path on
968 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_SYNC_PATH
);
969 /* supply the highest peer seq number to zebra
972 seq
= bgp_evpn_attr_get_sync_seq(pi
->attr
);
973 /* if any of the paths from the peer have the ROUTER
974 * flag set install the local entry as a router entry
976 if (is_evpn_prefix_ipaddr_v6(p
) &&
977 (pi
->attr
->es_flags
&
978 ATTR_ES_PEER_ROUTER
))
980 ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
982 if (!(pi
->attr
->es_flags
& ATTR_ES_PEER_ACTIVE
))
984 ZEBRA_MACIP_TYPE_PROXY_ADVERT
);
987 ret
= bgp_zebra_send_remote_macip(
988 bgp
, vpn
, p
, pi
->attr
->nexthop
, 1, flags
,
989 seq
, bgp_evpn_attr_get_esi(pi
->attr
));
990 } else if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
) {
991 ret
= bgp_evpn_remote_es_evi_add(bgp
, vpn
, p
);
993 switch (bgp_attr_get_pmsi_tnl_type(pi
->attr
)) {
994 case PMSI_TNLTYPE_INGR_REPL
:
995 flood_control
= VXLAN_FLOOD_HEAD_END_REPL
;
998 case PMSI_TNLTYPE_PIM_SM
:
999 flood_control
= VXLAN_FLOOD_PIM_SM
;
1003 flood_control
= VXLAN_FLOOD_DISABLED
;
1006 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, flood_control
, 1);
1012 /* Uninstall EVPN route from zebra. */
1013 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1014 const struct prefix_evpn
*p
,
1015 struct in_addr remote_vtep_ip
)
1019 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1020 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
1022 else if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
1023 ret
= bgp_evpn_remote_es_evi_del(bgp
, vpn
, p
);
1025 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
,
1026 VXLAN_FLOOD_DISABLED
, 0);
1032 * Due to MAC mobility, the prior "local" best route has been supplanted
1033 * by a "remote" best route. The prior route has to be deleted and withdrawn
1036 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1037 struct bgp_dest
*dest
,
1038 struct bgp_path_info
*old_local
,
1039 struct bgp_path_info
*new_select
)
1041 struct bgp_dest
*global_dest
;
1042 struct bgp_path_info
*pi
;
1043 afi_t afi
= AFI_L2VPN
;
1044 safi_t safi
= SAFI_EVPN
;
1046 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
1047 char esi_buf
[ESI_STR_LEN
];
1048 char esi_buf2
[ESI_STR_LEN
];
1049 struct prefix_evpn
*evp
=
1050 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1052 zlog_debug("local path deleted %pFX es %s; new-path-es %s", evp
,
1053 esi_to_str(&old_local
->attr
->esi
, esi_buf
,
1055 new_select
? esi_to_str(&new_select
->attr
->esi
,
1056 esi_buf2
, sizeof(esi_buf2
))
1060 /* Locate route node in the global EVPN routing table. Note that
1061 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1064 global_dest
= bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1065 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1068 /* Delete route entry in the global EVPN table. */
1069 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
1071 /* Schedule for processing - withdraws to peers happen from
1075 bgp_process(bgp
, global_dest
, afi
, safi
);
1076 bgp_dest_unlock_node(global_dest
);
1079 /* Delete route entry in the VNI route table, caller to remove. */
1080 bgp_path_info_delete(dest
, old_local
);
1084 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1086 * Note: vpn is NULL for local EAD-ES routes.
1088 int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1089 struct bgp_dest
*dest
)
1091 struct bgp_path_info
*old_select
, *new_select
;
1092 struct bgp_path_info_pair old_and_new
;
1093 afi_t afi
= AFI_L2VPN
;
1094 safi_t safi
= SAFI_EVPN
;
1097 /* Compute the best path. */
1098 bgp_best_selection(bgp
, dest
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1100 old_select
= old_and_new
.old
;
1101 new_select
= old_and_new
.new;
1103 /* If the best path hasn't changed - see if there is still something to
1104 * update to zebra RIB.
1105 * Remote routes and SYNC route (i.e. local routes with
1106 * SYNCED_FROM_PEER flag) need to updated to zebra on any attr
1109 if (old_select
&& old_select
== new_select
1110 && old_select
->type
== ZEBRA_ROUTE_BGP
1111 && (old_select
->sub_type
== BGP_ROUTE_IMPORTED
||
1112 bgp_evpn_attr_is_sync(old_select
->attr
))
1113 && !CHECK_FLAG(dest
->flags
, BGP_NODE_USER_CLEAR
)
1114 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1115 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1116 if (bgp_zebra_has_route_changed(old_select
))
1117 ret
= evpn_zebra_install(
1119 (const struct prefix_evpn
*)bgp_dest_get_prefix(
1122 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1123 UNSET_FLAG(old_select
->flags
, BGP_PATH_LINK_BW_CHG
);
1124 bgp_zebra_clear_route_change_flags(dest
);
1128 /* If the user did a "clear" this flag will be set */
1129 UNSET_FLAG(dest
->flags
, BGP_NODE_USER_CLEAR
);
1131 /* bestpath has changed; update relevant fields and install or uninstall
1132 * into the zebra RIB.
1134 if (old_select
|| new_select
)
1135 bgp_bump_version(dest
);
1138 bgp_path_info_unset_flag(dest
, old_select
, BGP_PATH_SELECTED
);
1140 bgp_path_info_set_flag(dest
, new_select
, BGP_PATH_SELECTED
);
1141 bgp_path_info_unset_flag(dest
, new_select
,
1142 BGP_PATH_ATTR_CHANGED
);
1143 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1144 UNSET_FLAG(new_select
->flags
, BGP_PATH_LINK_BW_CHG
);
1147 /* a local entry with the SYNC flag also results in a MAC-IP update
1150 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1151 && (new_select
->sub_type
== BGP_ROUTE_IMPORTED
||
1152 bgp_evpn_attr_is_sync(new_select
->attr
))) {
1153 ret
= evpn_zebra_install(
1155 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1158 /* If an old best existed and it was a "local" route, the only
1160 * it would be supplanted is due to MAC mobility procedures. So,
1162 * need to do an implicit delete and withdraw that route from
1165 if (new_select
->sub_type
== BGP_ROUTE_IMPORTED
&&
1166 old_select
&& old_select
->peer
== bgp
->peer_self
1167 && old_select
->type
== ZEBRA_ROUTE_BGP
1168 && old_select
->sub_type
== BGP_ROUTE_STATIC
1170 evpn_delete_old_local_route(bgp
, vpn
, dest
,
1171 old_select
, new_select
);
1173 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1174 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1175 ret
= evpn_zebra_uninstall(
1177 (const struct prefix_evpn
*)bgp_dest_get_prefix(
1179 old_select
->attr
->nexthop
);
1182 /* Clear any route change flags. */
1183 bgp_zebra_clear_route_change_flags(dest
);
1185 /* Reap old select bgp_path_info, if it has been removed */
1186 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1187 bgp_path_info_reap(dest
, old_select
);
1192 static struct bgp_path_info
*bgp_evpn_route_get_local_path(
1193 struct bgp
*bgp
, struct bgp_dest
*dest
)
1195 struct bgp_path_info
*tmp_pi
;
1196 struct bgp_path_info
*local_pi
= NULL
;
1198 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1199 tmp_pi
= tmp_pi
->next
) {
1200 if (bgp_evpn_is_path_local(bgp
, tmp_pi
)) {
1209 static int update_evpn_type5_route_entry(struct bgp
*bgp_evpn
,
1210 struct bgp
*bgp_vrf
, afi_t afi
,
1211 safi_t safi
, struct bgp_dest
*dest
,
1212 struct attr
*attr
, int *route_changed
)
1214 struct attr
*attr_new
= NULL
;
1215 struct bgp_path_info
*pi
= NULL
;
1216 mpls_label_t label
= MPLS_INVALID_LABEL
;
1217 struct bgp_path_info
*local_pi
= NULL
;
1218 struct bgp_path_info
*tmp_pi
= NULL
;
1221 /* locate the local route entry if any */
1222 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1223 tmp_pi
= tmp_pi
->next
) {
1224 if (tmp_pi
->peer
== bgp_evpn
->peer_self
1225 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1226 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1231 * create a new route entry if one doesn't exist.
1232 * Otherwise see if route attr has changed
1236 /* route has changed as this is the first entry */
1239 /* Add (or update) attribute to hash. */
1240 attr_new
= bgp_attr_intern(attr
);
1242 /* create the route info from attribute */
1243 pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1244 bgp_evpn
->peer_self
, attr_new
, dest
);
1245 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
1247 /* Type-5 routes advertise the L3-VNI */
1248 bgp_path_info_extra_get(pi
);
1249 vni2label(bgp_vrf
->l3vni
, &label
);
1250 memcpy(&pi
->extra
->label
, &label
, sizeof(label
));
1251 pi
->extra
->num_labels
= 1;
1253 /* add the route entry to route node*/
1254 bgp_path_info_add(dest
, pi
);
1258 if (!attrhash_cmp(tmp_pi
->attr
, attr
)) {
1260 /* attribute changed */
1263 /* The attribute has changed. */
1264 /* Add (or update) attribute to hash. */
1265 attr_new
= bgp_attr_intern(attr
);
1266 bgp_path_info_set_flag(dest
, tmp_pi
,
1267 BGP_PATH_ATTR_CHANGED
);
1269 /* Restore route, if needed. */
1270 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1271 bgp_path_info_restore(dest
, tmp_pi
);
1273 /* Unintern existing, set to new. */
1274 bgp_attr_unintern(&tmp_pi
->attr
);
1275 tmp_pi
->attr
= attr_new
;
1276 tmp_pi
->uptime
= monotime(NULL
);
1282 /* update evpn type-5 route entry */
1283 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1284 struct attr
*src_attr
, afi_t src_afi
,
1287 afi_t afi
= AFI_L2VPN
;
1288 safi_t safi
= SAFI_EVPN
;
1290 struct bgp_dest
*dest
= NULL
;
1291 struct bgp
*bgp_evpn
= NULL
;
1292 int route_changed
= 0;
1294 bgp_evpn
= bgp_get_evpn();
1298 /* Build path attribute for this route - use the source attr, if
1299 * present, else treat as locally originated.
1304 memset(&attr
, 0, sizeof(attr
));
1305 bgp_attr_default_set(&attr
, bgp_vrf
, BGP_ORIGIN_IGP
);
1308 /* Advertise Primary IP (PIP) is enabled, send individual
1309 * IP (default instance router-id) as nexthop.
1310 * PIP is disabled or vrr interface is not present
1311 * use anycast-IP as nexthop and anycast RMAC.
1313 if (!bgp_vrf
->evpn_info
->advertise_pip
||
1314 (!bgp_vrf
->evpn_info
->is_anycast_mac
)) {
1315 attr
.nexthop
= bgp_vrf
->originator_ip
;
1316 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1317 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, ETH_ALEN
);
1320 memcpy(&attr
.rmac
, &bgp_vrf
->evpn_info
->pip_rmac
, ETH_ALEN
);
1321 if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
!= INADDR_ANY
) {
1322 attr
.nexthop
= bgp_vrf
->evpn_info
->pip_ip
;
1323 attr
.mp_nexthop_global_in
= bgp_vrf
->evpn_info
->pip_ip
;
1324 } else if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
== INADDR_ANY
)
1325 if (bgp_debug_zebra(NULL
))
1327 "VRF %s evp %pFX advertise-pip primary ip is not configured",
1328 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
);
1331 if (bgp_debug_zebra(NULL
))
1333 "VRF %s type-5 route evp %pFX RMAC %pEA nexthop %pI4",
1334 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
, &attr
.rmac
,
1337 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1339 if (src_afi
== AFI_IP6
&&
1340 CHECK_FLAG(bgp_vrf
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1341 BGP_L2VPN_EVPN_ADV_IPV6_UNICAST_GW_IP
)) {
1343 !IN6_IS_ADDR_UNSPECIFIED(&src_attr
->mp_nexthop_global
)) {
1344 attr
.evpn_overlay
.type
= OVERLAY_INDEX_GATEWAY_IP
;
1345 SET_IPADDR_V6(&attr
.evpn_overlay
.gw_ip
);
1346 memcpy(&attr
.evpn_overlay
.gw_ip
.ipaddr_v6
,
1347 &src_attr
->mp_nexthop_global
,
1348 sizeof(struct in6_addr
));
1350 } else if (src_afi
== AFI_IP
&&
1351 CHECK_FLAG(bgp_vrf
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1352 BGP_L2VPN_EVPN_ADV_IPV4_UNICAST_GW_IP
)) {
1353 if (src_attr
&& src_attr
->nexthop
.s_addr
!= 0) {
1354 attr
.evpn_overlay
.type
= OVERLAY_INDEX_GATEWAY_IP
;
1355 SET_IPADDR_V4(&attr
.evpn_overlay
.gw_ip
);
1356 memcpy(&attr
.evpn_overlay
.gw_ip
.ipaddr_v4
,
1357 &src_attr
->nexthop
, sizeof(struct in_addr
));
1361 /* Setup RT and encap extended community */
1362 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1364 /* get the route node in global table */
1365 dest
= bgp_global_evpn_node_get(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1366 (const struct prefix_evpn
*)evp
,
1370 /* create or update the route entry within the route node */
1371 update_evpn_type5_route_entry(bgp_evpn
, bgp_vrf
, afi
, safi
, dest
, &attr
,
1374 /* schedule for processing and unlock node */
1375 if (route_changed
) {
1376 bgp_process(bgp_evpn
, dest
, afi
, safi
);
1377 bgp_dest_unlock_node(dest
);
1380 /* uninten temporary */
1382 aspath_unintern(&attr
.aspath
);
1386 static void bgp_evpn_get_sync_info(struct bgp
*bgp
, esi_t
*esi
,
1387 struct bgp_dest
*dest
, uint32_t loc_seq
,
1388 uint32_t *max_sync_seq
, bool *active_on_peer
,
1389 bool *peer_router
, bool *proxy_from_peer
)
1391 struct bgp_path_info
*tmp_pi
;
1392 struct bgp_path_info
*second_best_path
= NULL
;
1393 uint32_t tmp_mm_seq
= 0;
1397 /* find the best non-local path. a local path can only be present
1400 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1401 tmp_pi
= tmp_pi
->next
) {
1402 if (tmp_pi
->sub_type
!= BGP_ROUTE_IMPORTED
||
1403 !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1406 if (bgp_evpn_path_info_cmp(bgp
, tmp_pi
,
1407 second_best_path
, &paths_eq
))
1408 second_best_path
= tmp_pi
;
1411 if (!second_best_path
)
1414 tmp_esi
= bgp_evpn_attr_get_esi(second_best_path
->attr
);
1415 /* if this has the same ES desination as the local path
1418 if (!memcmp(esi
, tmp_esi
, sizeof(esi_t
))) {
1419 tmp_mm_seq
= mac_mobility_seqnum(second_best_path
->attr
);
1420 if (tmp_mm_seq
< loc_seq
)
1423 /* we have a non-proxy path from the ES peer. */
1424 if (second_best_path
->attr
->es_flags
&
1425 ATTR_ES_PROXY_ADVERT
) {
1426 *proxy_from_peer
= true;
1428 *active_on_peer
= true;
1431 if (second_best_path
->attr
->router_flag
)
1432 *peer_router
= true;
1434 /* we use both proxy and non-proxy imports to
1435 * determine the max sync sequence
1437 if (tmp_mm_seq
> *max_sync_seq
)
1438 *max_sync_seq
= tmp_mm_seq
;
1442 /* Bubble up sync-info from all paths (non-best) to the local-path.
1443 * This is need for MM sequence number syncing and proxy advertisement.
1444 * Note: The local path can only exist as a best path in the
1445 * VPN route table. It will take precedence over all sync paths.
1447 static void update_evpn_route_entry_sync_info(struct bgp
*bgp
,
1448 struct bgp_dest
*dest
,
1450 uint32_t loc_seq
, bool setup_sync
)
1453 struct prefix_evpn
*evp
=
1454 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1456 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1459 esi
= bgp_evpn_attr_get_esi(attr
);
1460 if (bgp_evpn_is_esi_valid(esi
)) {
1462 uint32_t max_sync_seq
= 0;
1463 bool active_on_peer
= false;
1464 bool peer_router
= false;
1465 bool proxy_from_peer
= false;
1467 bgp_evpn_get_sync_info(bgp
, esi
, dest
, loc_seq
,
1468 &max_sync_seq
, &active_on_peer
,
1469 &peer_router
, &proxy_from_peer
);
1470 attr
->mm_sync_seqnum
= max_sync_seq
;
1472 attr
->es_flags
|= ATTR_ES_PEER_ACTIVE
;
1474 attr
->es_flags
&= ~ATTR_ES_PEER_ACTIVE
;
1475 if (proxy_from_peer
)
1476 attr
->es_flags
|= ATTR_ES_PEER_PROXY
;
1478 attr
->es_flags
&= ~ATTR_ES_PEER_PROXY
;
1480 attr
->es_flags
|= ATTR_ES_PEER_ROUTER
;
1482 attr
->es_flags
&= ~ATTR_ES_PEER_ROUTER
;
1484 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
1485 char esi_buf
[ESI_STR_LEN
];
1488 "setup sync info for %pFX es %s max_seq %d %s%s%s",
1490 esi_to_str(esi
, esi_buf
,
1493 (attr
->es_flags
& ATTR_ES_PEER_ACTIVE
)
1496 (attr
->es_flags
& ATTR_ES_PEER_PROXY
)
1499 (attr
->es_flags
& ATTR_ES_PEER_ROUTER
)
1505 attr
->mm_sync_seqnum
= 0;
1506 attr
->es_flags
&= ~ATTR_ES_PEER_ACTIVE
;
1507 attr
->es_flags
&= ~ATTR_ES_PEER_PROXY
;
1512 * Create or update EVPN route entry. This could be in the VNI route table
1513 * or the global route table.
1515 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1516 afi_t afi
, safi_t safi
,
1517 struct bgp_dest
*dest
, struct attr
*attr
,
1518 int add
, struct bgp_path_info
**pi
,
1519 uint8_t flags
, uint32_t seq
, bool vpn_rt
,
1522 struct bgp_path_info
*tmp_pi
;
1523 struct bgp_path_info
*local_pi
;
1524 struct attr
*attr_new
;
1525 mpls_label_t label
[BGP_MAX_LABELS
];
1526 uint32_t num_labels
= 1;
1527 int route_change
= 1;
1529 const struct prefix_evpn
*evp
;
1532 evp
= (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1533 memset(&label
, 0, sizeof(label
));
1535 /* See if this is an update of an existing route, or a new add. */
1536 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
1538 /* If route doesn't exist already, create a new one, if told to.
1539 * Otherwise act based on whether the attributes of the route have
1542 if (!local_pi
&& !add
)
1545 if (old_is_sync
&& local_pi
)
1546 *old_is_sync
= bgp_evpn_attr_is_sync(local_pi
->attr
);
1548 /* if a local path is being added with a non-zero esi look
1549 * for SYNC paths from ES peers and bubble up the sync-info
1551 update_evpn_route_entry_sync_info(bgp
, dest
, attr
, seq
, vpn_rt
);
1553 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1554 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1555 add_mac_mobility_to_attr(seq
, attr
);
1558 /* Add (or update) attribute to hash. */
1559 attr_new
= bgp_attr_intern(attr
);
1561 /* Extract MAC mobility sequence number, if any. */
1562 attr_new
->mm_seqnum
=
1563 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1564 attr_new
->sticky
= sticky
;
1566 /* Create new route with its attribute. */
1567 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1568 bgp
->peer_self
, attr_new
, dest
);
1569 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1570 bgp_path_info_extra_get(tmp_pi
);
1572 /* The VNI goes into the 'label' field of the route */
1573 vni2label(vpn
->vni
, &label
[0]);
1575 /* Type-2 routes may carry a second VNI - the L3-VNI.
1576 * Only attach second label if we are advertising two labels for
1579 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1580 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1583 l3vni
= bgpevpn_get_l3vni(vpn
);
1585 vni2label(l3vni
, &label
[1]);
1590 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1591 tmp_pi
->extra
->num_labels
= num_labels
;
1592 /* Mark route as self type-2 route */
1593 if (flags
&& CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1594 tmp_pi
->extra
->af_flags
= BGP_EVPN_MACIP_TYPE_SVI_IP
;
1595 bgp_path_info_add(dest
, tmp_pi
);
1598 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1599 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1603 * The attributes have changed, type-2 routes needs to
1604 * be advertised with right labels.
1606 vni2label(vpn
->vni
, &label
[0]);
1607 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1608 && CHECK_FLAG(vpn
->flags
,
1609 VNI_FLAG_USE_TWO_LABELS
)) {
1612 l3vni
= bgpevpn_get_l3vni(vpn
);
1614 vni2label(l3vni
, &label
[1]);
1618 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1619 tmp_pi
->extra
->num_labels
= num_labels
;
1621 /* The attribute has changed. */
1622 /* Add (or update) attribute to hash. */
1623 attr_new
= bgp_attr_intern(attr
);
1624 bgp_path_info_set_flag(dest
, tmp_pi
,
1625 BGP_PATH_ATTR_CHANGED
);
1627 /* Extract MAC mobility sequence number, if any. */
1628 attr_new
->mm_seqnum
=
1629 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1630 attr_new
->sticky
= sticky
;
1632 /* Restore route, if needed. */
1633 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1634 bgp_path_info_restore(dest
, tmp_pi
);
1636 /* Unintern existing, set to new. */
1637 bgp_attr_unintern(&tmp_pi
->attr
);
1638 tmp_pi
->attr
= attr_new
;
1639 tmp_pi
->uptime
= monotime(NULL
);
1643 /* local MAC-IP routes in the VNI table are linked to
1644 * the destination ES
1646 if (route_change
&& vpn_rt
1647 && (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
))
1648 bgp_evpn_path_es_link(tmp_pi
, vpn
->vni
,
1649 bgp_evpn_attr_get_esi(tmp_pi
->attr
));
1651 /* Return back the route entry. */
1653 return route_change
;
1656 static void evpn_zebra_reinstall_best_route(struct bgp
*bgp
,
1657 struct bgpevpn
*vpn
,
1658 struct bgp_dest
*dest
)
1660 struct bgp_path_info
*tmp_ri
;
1661 struct bgp_path_info
*curr_select
= NULL
;
1663 for (tmp_ri
= bgp_dest_get_bgp_path_info(dest
); tmp_ri
;
1664 tmp_ri
= tmp_ri
->next
) {
1665 if (CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_SELECTED
)) {
1666 curr_select
= tmp_ri
;
1671 if (curr_select
&& curr_select
->type
== ZEBRA_ROUTE_BGP
1672 && (curr_select
->sub_type
== BGP_ROUTE_IMPORTED
||
1673 bgp_evpn_attr_is_sync(curr_select
->attr
)))
1674 evpn_zebra_install(bgp
, vpn
,
1675 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1680 * If the local route was not selected evict it and tell zebra to re-add
1681 * the best remote dest.
1683 * Typically a local path added by zebra is expected to be selected as
1684 * best. In which case when a remote path wins as best (later)
1685 * evpn_route_select_install itself evicts the older-local-best path.
1687 * However if bgp's add and zebra's add cross paths (race condition) it
1688 * is possible that the local path is no longer the "older" best path.
1689 * It is a path that was never designated as best and hence requires
1690 * additional handling to prevent bgp from injecting and holding on to a
1691 * non-best local path.
1693 static void evpn_cleanup_local_non_best_route(struct bgp
*bgp
,
1694 struct bgpevpn
*vpn
,
1695 struct bgp_dest
*dest
,
1696 struct bgp_path_info
*local_pi
)
1698 /* local path was not picked as the winner; kick it out */
1699 if (bgp_debug_zebra(NULL
))
1700 zlog_debug("evicting local evpn prefix %pBD as remote won",
1703 evpn_delete_old_local_route(bgp
, vpn
, dest
, local_pi
, NULL
);
1704 bgp_path_info_reap(dest
, local_pi
);
1706 /* tell zebra to re-add the best remote path */
1707 evpn_zebra_reinstall_best_route(bgp
, vpn
, dest
);
1710 static inline bool bgp_evpn_route_add_l3_ecomm_ok(struct bgpevpn
*vpn
,
1711 const struct prefix_evpn
*p
,
1714 return p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1715 && (is_evpn_prefix_ipaddr_v4(p
)
1716 || (is_evpn_prefix_ipaddr_v6(p
)
1717 && !IN6_IS_ADDR_LINKLOCAL(
1718 &p
->prefix
.macip_addr
.ip
.ipaddr_v6
)))
1719 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)
1720 && bgpevpn_get_l3vni(vpn
) && bgp_evpn_es_add_l3_ecomm_ok(esi
);
1724 * Create or update EVPN route (of type based on prefix) for specified VNI
1725 * and schedule for processing.
1727 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1728 struct prefix_evpn
*p
, uint8_t flags
,
1729 uint32_t seq
, esi_t
*esi
)
1731 struct bgp_dest
*dest
;
1733 struct attr
*attr_new
;
1734 int add_l3_ecomm
= 0;
1735 struct bgp_path_info
*pi
;
1736 afi_t afi
= AFI_L2VPN
;
1737 safi_t safi
= SAFI_EVPN
;
1739 bool old_is_sync
= false;
1741 memset(&attr
, 0, sizeof(attr
));
1743 /* Build path-attribute for this route. */
1744 bgp_attr_default_set(&attr
, bgp
, BGP_ORIGIN_IGP
);
1745 attr
.nexthop
= vpn
->originator_ip
;
1746 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1747 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1748 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1749 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1750 attr
.router_flag
= CHECK_FLAG(flags
,
1751 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1752 if (CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_PROXY_ADVERT
))
1753 attr
.es_flags
|= ATTR_ES_PROXY_ADVERT
;
1755 if (esi
&& bgp_evpn_is_esi_valid(esi
)) {
1756 memcpy(&attr
.esi
, esi
, sizeof(esi_t
));
1757 attr
.es_flags
|= ATTR_ES_IS_LOCAL
;
1760 /* PMSI is only needed for type-3 routes */
1761 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
1762 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1763 bgp_attr_set_pmsi_tnl_type(&attr
, PMSI_TNLTYPE_INGR_REPL
);
1766 /* router mac is only needed for type-2 routes here. */
1767 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
1768 uint8_t af_flags
= 0;
1770 if (CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1771 SET_FLAG(af_flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
);
1773 bgp_evpn_get_rmac_nexthop(vpn
, p
, &attr
, af_flags
);
1776 if (bgp_debug_zebra(NULL
)) {
1777 char buf3
[ESI_STR_LEN
];
1780 "VRF %s vni %u type-2 route evp %pFX RMAC %pEA nexthop %pI4 esi %s",
1781 vpn
->bgp_vrf
? vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
)
1783 vpn
->vni
, p
, &attr
.rmac
, &attr
.mp_nexthop_global_in
,
1784 esi_to_str(esi
, buf3
, sizeof(buf3
)));
1787 vni2label(vpn
->vni
, &(attr
.label
));
1789 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1790 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1793 add_l3_ecomm
= bgp_evpn_route_add_l3_ecomm_ok(
1794 vpn
, p
, (attr
.es_flags
& ATTR_ES_IS_LOCAL
) ? &attr
.esi
: NULL
);
1796 /* Set up extended community. */
1797 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1799 /* First, create (or fetch) route node within the VNI. */
1800 /* NOTE: There is no RD here. */
1801 dest
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1803 /* Create or update route entry. */
1804 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, dest
, &attr
,
1806 true /* setup_sync */, &old_is_sync
);
1808 attr_new
= pi
->attr
;
1810 /* lock ri to prevent freeing in evpn_route_select_install */
1811 bgp_path_info_lock(pi
);
1813 /* Perform route selection. Normally, the local route in the
1814 * VNI is expected to win and be the best route. However, if
1815 * there is a race condition where a host moved from local to
1816 * remote and the remote route was received in BGP just prior
1817 * to the local MACIP notification from zebra, the remote
1818 * route would win, and we should evict the defunct local route
1819 * and (re)install the remote route into zebra.
1821 evpn_route_select_install(bgp
, vpn
, dest
);
1823 * If the new local route was not selected evict it and tell zebra
1824 * to re-add the best remote dest. BGP doesn't retain non-best local
1827 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
1830 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1832 evpn_cleanup_local_non_best_route(bgp
, vpn
, dest
, pi
);
1836 /* If the local path already existed and is still the
1837 * best path we need to also check if it transitioned
1838 * from being a sync path to a non-sync path. If it
1839 * it did we need to notify zebra that the sync-path
1842 new_is_sync
= bgp_evpn_attr_is_sync(pi
->attr
);
1843 if (!new_is_sync
&& old_is_sync
)
1844 evpn_zebra_uninstall(bgp
, vpn
, p
, zero_vtep_ip
);
1847 bgp_path_info_unlock(pi
);
1849 bgp_dest_unlock_node(dest
);
1851 /* If this is a new route or some attribute has changed, export the
1852 * route to the global table. The route will be advertised to peers
1853 * from there. Note that this table is a 2-level tree (RD-level +
1854 * Prefix-level) similar to L3VPN routes.
1857 struct bgp_path_info
*global_pi
;
1859 dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1860 (const struct prefix_evpn
*)p
,
1862 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, dest
, attr_new
, 1,
1863 &global_pi
, flags
, seq
,
1864 false /* setup_sync */, NULL
/* old_is_sync */);
1866 /* Schedule for processing and unlock node. */
1867 bgp_process(bgp
, dest
, afi
, safi
);
1868 bgp_dest_unlock_node(dest
);
1871 /* Unintern temporary. */
1872 aspath_unintern(&attr
.aspath
);
1878 * Delete EVPN route entry.
1879 * The entry can be in ESI/VNI table or the global table.
1881 void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1882 struct bgp_dest
*dest
,
1883 struct bgp_path_info
**pi
)
1885 struct bgp_path_info
*tmp_pi
;
1889 /* Now, find matching route. */
1890 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1891 tmp_pi
= tmp_pi
->next
)
1892 if (tmp_pi
->peer
== bgp
->peer_self
1893 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1894 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1899 /* Mark route for delete. */
1901 bgp_path_info_delete(dest
, tmp_pi
);
1904 /* Delete EVPN type5 route */
1905 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1907 afi_t afi
= AFI_L2VPN
;
1908 safi_t safi
= SAFI_EVPN
;
1909 struct bgp_dest
*dest
= NULL
;
1910 struct bgp_path_info
*pi
= NULL
;
1911 struct bgp
*bgp_evpn
= NULL
; /* evpn bgp instance */
1913 bgp_evpn
= bgp_get_evpn();
1917 /* locate the global route entry for this type-5 prefix */
1918 dest
= bgp_global_evpn_node_lookup(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1919 (const struct prefix_evpn
*)evp
, &bgp_vrf
->vrf_prd
);
1923 delete_evpn_route_entry(bgp_evpn
, afi
, safi
, dest
, &pi
);
1925 bgp_process(bgp_evpn
, dest
, afi
, safi
);
1926 bgp_dest_unlock_node(dest
);
1931 * Delete EVPN route (of type based on prefix) for specified VNI and
1932 * schedule for processing.
1934 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1935 struct prefix_evpn
*p
)
1937 struct bgp_dest
*dest
, *global_dest
;
1938 struct bgp_path_info
*pi
;
1939 afi_t afi
= AFI_L2VPN
;
1940 safi_t safi
= SAFI_EVPN
;
1942 /* First, locate the route node within the VNI. If it doesn't exist,
1944 * is nothing further to do.
1946 /* NOTE: There is no RD here. */
1947 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1951 /* Next, locate route node in the global EVPN routing table. Note that
1952 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1955 global_dest
= bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1956 (const struct prefix_evpn
*)p
, &vpn
->prd
);
1958 /* Delete route entry in the global EVPN table. */
1959 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
1961 /* Schedule for processing - withdraws to peers happen from
1965 bgp_process(bgp
, global_dest
, afi
, safi
);
1966 bgp_dest_unlock_node(global_dest
);
1969 /* Delete route entry in the VNI route table. This can just be removed.
1971 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
1973 bgp_path_info_reap(dest
, pi
);
1974 evpn_route_select_install(bgp
, vpn
, dest
);
1976 bgp_dest_unlock_node(dest
);
1981 void bgp_evpn_update_type2_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1982 struct bgp_dest
*dest
,
1983 struct bgp_path_info
*local_pi
,
1986 afi_t afi
= AFI_L2VPN
;
1987 safi_t safi
= SAFI_EVPN
;
1988 struct bgp_path_info
*pi
;
1990 struct attr
*attr_new
;
1992 int add_l3_ecomm
= 0;
1993 struct bgp_dest
*global_dest
;
1994 struct bgp_path_info
*global_pi
;
1995 struct prefix_evpn
*evp
=
1996 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1998 bool old_is_sync
= false;
2000 if (CHECK_FLAG(local_pi
->flags
, BGP_PATH_REMOVED
))
2004 * Build attribute per local route as the MAC mobility and
2005 * some other values could differ for different routes. The
2006 * attributes will be shared in the hash table.
2008 bgp_attr_default_set(&attr
, bgp
, BGP_ORIGIN_IGP
);
2009 attr
.nexthop
= vpn
->originator_ip
;
2010 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
2011 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
2012 attr
.sticky
= (local_pi
->attr
->sticky
) ? 1 : 0;
2013 attr
.router_flag
= (local_pi
->attr
->router_flag
) ? 1 : 0;
2014 attr
.es_flags
= local_pi
->attr
->es_flags
;
2015 if (local_pi
->attr
->default_gw
) {
2016 attr
.default_gw
= 1;
2017 if (is_evpn_prefix_ipaddr_v6(evp
))
2018 attr
.router_flag
= 1;
2020 memcpy(&attr
.esi
, &local_pi
->attr
->esi
, sizeof(esi_t
));
2021 bgp_evpn_get_rmac_nexthop(vpn
, evp
, &attr
,
2022 local_pi
->extra
->af_flags
);
2023 vni2label(vpn
->vni
, &(attr
.label
));
2024 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2025 * using L3 VNI for type-2 routes also.
2027 add_l3_ecomm
= bgp_evpn_route_add_l3_ecomm_ok(
2029 (attr
.es_flags
& ATTR_ES_IS_LOCAL
) ? &attr
.esi
: NULL
);
2031 /* Set up extended community. */
2032 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2033 seq
= mac_mobility_seqnum(local_pi
->attr
);
2035 if (bgp_debug_zebra(NULL
)) {
2036 char buf3
[ESI_STR_LEN
];
2039 "VRF %s vni %u evp %pFX RMAC %pEA nexthop %pI4 esi %s esf 0x%x from %s",
2040 vpn
->bgp_vrf
? vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
)
2042 vpn
->vni
, evp
, &attr
.rmac
, &attr
.mp_nexthop_global_in
,
2043 esi_to_str(&attr
.esi
, buf3
, sizeof(buf3
)),
2044 attr
.es_flags
, caller
);
2047 /* Update the route entry. */
2048 route_change
= update_evpn_route_entry(
2049 bgp
, vpn
, afi
, safi
, dest
, &attr
, 0, &pi
, 0, seq
,
2050 true /* setup_sync */, &old_is_sync
);
2053 attr_new
= pi
->attr
;
2054 /* lock ri to prevent freeing in evpn_route_select_install */
2055 bgp_path_info_lock(pi
);
2057 /* Perform route selection. Normally, the local route in the
2058 * VNI is expected to win and be the best route. However,
2059 * under peculiar situations (e.g., tunnel (next hop) IP change
2060 * that causes best selection to be based on next hop), a
2061 * remote route could win. If the local route is the best,
2062 * ensure it is updated in the global EVPN route table and
2063 * advertised to peers; otherwise, ensure it is evicted and
2064 * (re)install the remote route into zebra.
2066 evpn_route_select_install(bgp
, vpn
, dest
);
2068 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2071 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
2073 evpn_cleanup_local_non_best_route(bgp
, vpn
, dest
, pi
);
2077 /* If the local path already existed and is still the
2078 * best path we need to also check if it transitioned
2079 * from being a sync path to a non-sync path. If it
2080 * it did we need to notify zebra that the sync-path
2083 new_is_sync
= bgp_evpn_attr_is_sync(pi
->attr
);
2084 if (!new_is_sync
&& old_is_sync
)
2085 evpn_zebra_uninstall(bgp
, vpn
,
2092 bgp_path_info_unlock(pi
);
2095 /* Update route in global routing table. */
2096 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
,
2097 safi
, evp
, &vpn
->prd
);
2098 assert(global_dest
);
2099 update_evpn_route_entry(
2100 bgp
, vpn
, afi
, safi
, global_dest
, attr_new
, 0,
2101 &global_pi
, 0, mac_mobility_seqnum(attr_new
),
2102 false /* setup_sync */, NULL
/* old_is_sync */);
2104 /* Schedule for processing and unlock node. */
2105 bgp_process(bgp
, global_dest
, afi
, safi
);
2106 bgp_dest_unlock_node(global_dest
);
2109 /* Unintern temporary. */
2110 aspath_unintern(&attr
.aspath
);
2114 * Update all type-2 (MACIP) local routes for this VNI - these should also
2115 * be scheduled for advertise to peers.
2117 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2119 struct bgp_dest
*dest
;
2120 struct bgp_path_info
*tmp_pi
;
2122 /* Walk this VNI's route table and update local type-2 routes. For any
2123 * routes updated, update corresponding entry in the global table too.
2125 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2126 dest
= bgp_route_next(dest
)) {
2127 const struct prefix_evpn
*evp
=
2128 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
2130 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2133 /* Identify local route. */
2134 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
2135 tmp_pi
= tmp_pi
->next
) {
2136 if (tmp_pi
->peer
== bgp
->peer_self
2137 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2138 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2145 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, tmp_pi
,
2153 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2154 * global routing table. These are also scheduled for withdraw from peers.
2156 static void delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2160 struct bgp_dest
*rddest
, *dest
;
2161 struct bgp_table
*table
;
2162 struct bgp_path_info
*pi
;
2167 rddest
= bgp_node_lookup(bgp
->rib
[afi
][safi
],
2168 (struct prefix
*)&vpn
->prd
);
2170 table
= bgp_dest_get_bgp_table_info(rddest
);
2171 for (dest
= bgp_table_top(table
); dest
;
2172 dest
= bgp_route_next(dest
)) {
2173 const struct prefix_evpn
*evp
=
2174 (const struct prefix_evpn
*)bgp_dest_get_prefix(
2177 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2180 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2182 bgp_process(bgp
, dest
, afi
, safi
);
2185 /* Unlock RD node. */
2186 bgp_dest_unlock_node(rddest
);
2191 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2192 * table as well as the per-VNI route table.
2194 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2198 struct bgp_dest
*dest
;
2199 struct bgp_path_info
*pi
;
2204 /* First, walk the global route table for this VNI's type-2 local
2206 * EVPN routes are a 2-level table, first get the RD table.
2208 delete_global_type2_routes(bgp
, vpn
);
2210 /* Next, walk this VNI's route table and delete local type-2 routes. */
2211 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2212 dest
= bgp_route_next(dest
)) {
2213 const struct prefix_evpn
*evp
=
2214 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
2216 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2219 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2221 /* Route entry in local table gets deleted immediately. */
2223 bgp_path_info_reap(dest
, pi
);
2230 * Delete all routes in the per-VNI route table.
2232 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2234 struct bgp_dest
*dest
;
2235 struct bgp_path_info
*pi
, *nextpi
;
2237 /* Walk this VNI's route table and delete all routes. */
2238 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2239 dest
= bgp_route_next(dest
)) {
2240 for (pi
= bgp_dest_get_bgp_path_info(dest
);
2241 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2242 bgp_evpn_remote_ip_hash_del(vpn
, pi
);
2243 bgp_path_info_delete(dest
, pi
);
2244 bgp_path_info_reap(dest
, pi
);
2251 /* BUM traffic flood mode per-l2-vni */
2252 static int bgp_evpn_vni_flood_mode_get(struct bgp
*bgp
,
2253 struct bgpevpn
*vpn
)
2255 /* if flooding has been globally disabled per-vni mode is
2258 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
2259 return VXLAN_FLOOD_DISABLED
;
2261 /* if mcast group ip has been specified we use a PIM-SM MDT */
2262 if (vpn
->mcast_grp
.s_addr
!= INADDR_ANY
)
2263 return VXLAN_FLOOD_PIM_SM
;
2265 /* default is ingress replication */
2266 return VXLAN_FLOOD_HEAD_END_REPL
;
2270 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2271 * export RT getting modified or change to tunnel IP. Note that these
2272 * situations need the route in the per-VNI table as well as the global
2273 * table to be updated (as attributes change).
2275 int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2278 struct prefix_evpn p
;
2280 update_type1_routes_for_evi(bgp
, vpn
);
2282 /* Update and advertise the type-3 route (only one) followed by the
2283 * locally learnt type-2 routes (MACIP) - for this VNI.
2285 * RT-3 only if doing head-end replication
2287 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
2288 == VXLAN_FLOOD_HEAD_END_REPL
) {
2289 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2290 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
);
2295 return update_all_type2_routes(bgp
, vpn
);
2299 * Delete (and withdraw) local routes for specified VNI from the global
2300 * table and per-VNI table. After this, remove all other routes from
2301 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2302 * (advertise-all-vni) being disabled.
2304 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2307 struct prefix_evpn p
;
2309 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2310 * followed by type-3 routes (only one) - for this VNI.
2312 ret
= delete_all_type2_routes(bgp
, vpn
);
2316 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2317 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2321 /* Delete all routes from the per-VNI table. */
2322 return delete_all_vni_routes(bgp
, vpn
);
2326 * There is a flood mcast IP address change. Update the mcast-grp and
2327 * remove the type-3 route if any. A new type-3 route will be generated
2328 * post tunnel_ip update if the new flood mode is head-end-replication.
2330 static int bgp_evpn_mcast_grp_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2331 struct in_addr mcast_grp
)
2333 struct prefix_evpn p
;
2335 vpn
->mcast_grp
= mcast_grp
;
2337 if (is_vni_live(vpn
)) {
2338 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2339 delete_evpn_route(bgp
, vpn
, &p
);
2346 * There is a tunnel endpoint IP address change for this VNI, delete
2347 * prior type-3 route (if needed) and update.
2348 * Note: Route re-advertisement happens elsewhere after other processing
2351 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2352 struct in_addr originator_ip
)
2354 struct prefix_evpn p
;
2356 /* If VNI is not live, we only need to update the originator ip */
2357 if (!is_vni_live(vpn
)) {
2358 vpn
->originator_ip
= originator_ip
;
2362 /* Update the tunnel-ip hash */
2363 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2364 bgp_tip_add(bgp
, &originator_ip
);
2366 /* filter routes as martian nexthop db has changed */
2367 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2369 /* Need to withdraw type-3 route as the originator IP is part
2372 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2373 delete_evpn_route(bgp
, vpn
, &p
);
2375 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2376 vpn
->originator_ip
= originator_ip
;
2380 static struct bgp_path_info
*
2381 bgp_create_evpn_bgp_path_info(struct bgp_path_info
*parent_pi
,
2382 struct bgp_dest
*dest
, struct attr
*attr
)
2384 struct attr
*attr_new
;
2385 struct bgp_path_info
*pi
;
2387 /* Add (or update) attribute to hash. */
2388 attr_new
= bgp_attr_intern(attr
);
2390 /* Create new route with its attribute. */
2391 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0, parent_pi
->peer
,
2393 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2394 bgp_path_info_extra_get(pi
);
2395 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2396 bgp_dest_lock_node((struct bgp_dest
*)parent_pi
->net
);
2397 if (parent_pi
->extra
) {
2398 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2399 sizeof(pi
->extra
->label
));
2400 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2401 pi
->extra
->igpmetric
= parent_pi
->extra
->igpmetric
;
2403 bgp_path_info_add(dest
, pi
);
2409 * Install route entry into the VRF routing table and invoke route selection.
2411 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2412 const struct prefix_evpn
*evp
,
2413 struct bgp_path_info
*parent_pi
)
2415 struct bgp_dest
*dest
;
2416 struct bgp_path_info
*pi
;
2418 struct attr
*attr_new
;
2421 struct prefix
*pp
= &p
;
2424 bool new_pi
= false;
2425 bool use_l3nhg
= false;
2426 bool is_l3nhg_active
= false;
2427 char buf1
[INET6_ADDRSTRLEN
];
2429 memset(pp
, 0, sizeof(struct prefix
));
2430 ip_prefix_from_evpn_prefix(evp
, pp
);
2432 if (bgp_debug_zebra(NULL
))
2434 "vrf %s: import evpn prefix %pFX parent %p flags 0x%x",
2435 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
, parent_pi
,
2438 /* Create (or fetch) route within the VRF. */
2439 /* NOTE: There is no RD here. */
2440 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2442 safi
= SAFI_UNICAST
;
2443 dest
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2444 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2446 safi
= SAFI_UNICAST
;
2447 dest
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2451 /* EVPN routes currently only support a IPv4 next hop which corresponds
2452 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2453 * or prefix route, we have to convert the next hop to an IPv4-mapped
2454 * address for the rest of the code to flow through. In the case of IPv4,
2455 * make sure to set the flag for next hop attribute.
2457 attr
= *parent_pi
->attr
;
2458 if (attr
.evpn_overlay
.type
!= OVERLAY_INDEX_GATEWAY_IP
) {
2460 evpn_convert_nexthop_to_ipv6(&attr
);
2462 attr
.nexthop
= attr
.mp_nexthop_global_in
;
2463 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2468 * If gateway IP overlay index is specified in the NLRI of
2469 * EVPN RT-5, this gateway IP should be used as the nexthop
2470 * for the prefix in the VRF
2472 if (bgp_debug_zebra(NULL
)) {
2474 "Install gateway IP %s as nexthop for prefix %pFX in vrf %s",
2475 inet_ntop(pp
->family
, &attr
.evpn_overlay
.gw_ip
,
2476 buf1
, sizeof(buf1
)), pp
,
2477 vrf_id_to_name(bgp_vrf
->vrf_id
));
2480 if (afi
== AFI_IP6
) {
2481 memcpy(&attr
.mp_nexthop_global
,
2482 &attr
.evpn_overlay
.gw_ip
.ipaddr_v6
,
2483 sizeof(struct in6_addr
));
2484 attr
.mp_nexthop_len
= IPV6_MAX_BYTELEN
;
2486 attr
.nexthop
= attr
.evpn_overlay
.gw_ip
.ipaddr_v4
;
2487 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2491 bgp_evpn_es_vrf_use_nhg(bgp_vrf
, &parent_pi
->attr
->esi
, &use_l3nhg
,
2492 &is_l3nhg_active
, NULL
);
2494 attr
.es_flags
|= ATTR_ES_L3_NHG_USE
;
2495 if (is_l3nhg_active
)
2496 attr
.es_flags
|= ATTR_ES_L3_NHG_ACTIVE
;
2498 /* Check if route entry is already present. */
2499 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2501 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2505 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, dest
, &attr
);
2508 if (attrhash_cmp(pi
->attr
, &attr
)
2509 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2510 bgp_dest_unlock_node(dest
);
2513 /* The attribute has changed. */
2514 /* Add (or update) attribute to hash. */
2515 attr_new
= bgp_attr_intern(&attr
);
2517 /* Restore route, if needed. */
2518 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2519 bgp_path_info_restore(dest
, pi
);
2521 /* Mark if nexthop has changed. */
2523 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2525 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2526 &attr_new
->mp_nexthop_global
)))
2527 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2529 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ATTR_CHANGED
);
2530 /* Unintern existing, set to new. */
2531 bgp_attr_unintern(&pi
->attr
);
2532 pi
->attr
= attr_new
;
2533 pi
->uptime
= monotime(NULL
);
2536 /* Gateway IP nexthop should be resolved */
2537 if (attr
.evpn_overlay
.type
== OVERLAY_INDEX_GATEWAY_IP
) {
2538 if (bgp_find_or_add_nexthop(bgp_vrf
, bgp_vrf
, afi
, safi
, pi
,
2540 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_VALID
);
2542 if (BGP_DEBUG(nht
, NHT
)) {
2543 inet_ntop(pp
->family
,
2544 &attr
.evpn_overlay
.gw_ip
,
2545 buf1
, sizeof(buf1
));
2546 zlog_debug("%s: gateway IP NH unresolved",
2549 bgp_path_info_unset_flag(dest
, pi
, BGP_PATH_VALID
);
2553 /* as it is an importation, change nexthop */
2554 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ANNC_NH_SELF
);
2557 /* Link path to evpn nexthop */
2558 bgp_evpn_path_nh_add(bgp_vrf
, pi
);
2560 bgp_aggregate_increment(bgp_vrf
, bgp_dest_get_prefix(dest
), pi
, afi
,
2563 /* Perform route selection and update zebra, if required. */
2564 bgp_process(bgp_vrf
, dest
, afi
, safi
);
2566 /* Process for route leaking. */
2567 vpn_leak_from_vrf_update(bgp_get_default(), bgp_vrf
, pi
);
2569 bgp_dest_unlock_node(dest
);
2571 if (bgp_debug_zebra(NULL
))
2572 zlog_debug("... %s pi dest %p (l %d) pi %p (l %d, f 0x%x)",
2573 new_pi
? "new" : "update", dest
,
2574 bgp_dest_get_lock_count(dest
), pi
, pi
->lock
,
2581 * Install route entry into the VNI routing table and invoke route selection.
2583 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2584 const struct prefix_evpn
*p
,
2585 struct bgp_path_info
*parent_pi
)
2587 struct bgp_dest
*dest
;
2588 struct bgp_path_info
*pi
;
2589 struct bgp_path_info
*local_pi
;
2590 struct attr
*attr_new
;
2592 struct prefix_evpn ad_evp
;
2593 bool old_local_es
= false;
2596 /* EAD prefix in the global table doesn't include the VTEP-IP so
2597 * we need to create a different copy for the VNI
2599 if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
2600 p
= evpn_type1_prefix_vni_copy(&ad_evp
, p
,
2601 parent_pi
->attr
->nexthop
);
2603 /* Create (or fetch) route within the VNI. */
2604 /* NOTE: There is no RD here. */
2605 dest
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2607 /* Check if route entry is already present. */
2608 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2610 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2614 /* Create an info */
2615 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, dest
,
2617 new_local_es
= bgp_evpn_attr_is_local_es(pi
->attr
);
2619 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2620 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2621 bgp_dest_unlock_node(dest
);
2624 /* The attribute has changed. */
2625 /* Add (or update) attribute to hash. */
2626 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2628 /* Restore route, if needed. */
2629 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2630 bgp_path_info_restore(dest
, pi
);
2632 /* Mark if nexthop has changed. */
2633 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2634 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2636 old_local_es
= bgp_evpn_attr_is_local_es(pi
->attr
);
2637 new_local_es
= bgp_evpn_attr_is_local_es(attr_new
);
2638 /* If ESI is different or if its type has changed we
2639 * need to reinstall the path in zebra
2641 if ((old_local_es
!= new_local_es
)
2642 || memcmp(&pi
->attr
->esi
, &attr_new
->esi
,
2643 sizeof(attr_new
->esi
))) {
2645 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
))
2646 zlog_debug("VNI %d path %pFX chg to %s es",
2647 vpn
->vni
, &pi
->net
->p
,
2648 new_local_es
? "local"
2650 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ATTR_CHANGED
);
2653 /* Unintern existing, set to new. */
2654 bgp_attr_unintern(&pi
->attr
);
2655 pi
->attr
= attr_new
;
2656 pi
->uptime
= monotime(NULL
);
2659 /* Add this route to remote IP hashtable */
2660 bgp_evpn_remote_ip_hash_add(vpn
, pi
);
2662 /* Perform route selection and update zebra, if required. */
2663 ret
= evpn_route_select_install(bgp
, vpn
, dest
);
2665 /* if the best path is a local path with a non-zero ES
2666 * sync info against the local path may need to be updated
2667 * when a remote path is added/updated (including changes
2668 * from sync-path to remote-path)
2670 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
2671 if (local_pi
&& (old_local_es
|| new_local_es
))
2672 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, local_pi
,
2674 bgp_dest_unlock_node(dest
);
2680 * Uninstall route entry from the VRF routing table and send message
2681 * to zebra, if appropriate.
2683 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2684 const struct prefix_evpn
*evp
,
2685 struct bgp_path_info
*parent_pi
)
2687 struct bgp_dest
*dest
;
2688 struct bgp_path_info
*pi
;
2691 struct prefix
*pp
= &p
;
2695 memset(pp
, 0, sizeof(struct prefix
));
2696 ip_prefix_from_evpn_prefix(evp
, pp
);
2698 if (bgp_debug_zebra(NULL
))
2700 "vrf %s: unimport evpn prefix %pFX parent %p flags 0x%x",
2701 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
, parent_pi
,
2704 /* Locate route within the VRF. */
2705 /* NOTE: There is no RD here. */
2706 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2708 safi
= SAFI_UNICAST
;
2709 dest
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2712 safi
= SAFI_UNICAST
;
2713 dest
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2719 /* Find matching route entry. */
2720 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2722 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2726 bgp_dest_unlock_node(dest
);
2730 if (bgp_debug_zebra(NULL
))
2731 zlog_debug("... delete dest %p (l %d) pi %p (l %d, f 0x%x)",
2732 dest
, bgp_dest_get_lock_count(dest
), pi
, pi
->lock
,
2735 /* Process for route leaking. */
2736 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp_vrf
, pi
);
2738 bgp_aggregate_decrement(bgp_vrf
, bgp_dest_get_prefix(dest
), pi
, afi
,
2741 /* Mark entry for deletion */
2742 bgp_path_info_delete(dest
, pi
);
2744 /* Unlink path to evpn nexthop */
2745 bgp_evpn_path_nh_del(bgp_vrf
, pi
);
2747 /* Perform route selection and update zebra, if required. */
2748 bgp_process(bgp_vrf
, dest
, afi
, safi
);
2750 /* Unlock route node. */
2751 bgp_dest_unlock_node(dest
);
2757 * Uninstall route entry from the VNI routing table and send message
2758 * to zebra, if appropriate.
2760 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2761 const struct prefix_evpn
*p
,
2762 struct bgp_path_info
*parent_pi
)
2764 struct bgp_dest
*dest
;
2765 struct bgp_path_info
*pi
;
2766 struct bgp_path_info
*local_pi
;
2768 struct prefix_evpn ad_evp
;
2770 /* EAD prefix in the global table doesn't include the VTEP-IP so
2771 * we need to create a different copy for the VNI
2773 if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
2774 p
= evpn_type1_prefix_vni_copy(&ad_evp
, p
,
2775 parent_pi
->attr
->nexthop
);
2777 /* Locate route within the VNI. */
2778 /* NOTE: There is no RD here. */
2779 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2783 /* Find matching route entry. */
2784 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2786 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2790 bgp_dest_unlock_node(dest
);
2794 bgp_evpn_remote_ip_hash_del(vpn
, pi
);
2796 /* Mark entry for deletion */
2797 bgp_path_info_delete(dest
, pi
);
2799 /* Perform route selection and update zebra, if required. */
2800 ret
= evpn_route_select_install(bgp
, vpn
, dest
);
2802 /* if the best path is a local path with a non-zero ES
2803 * sync info against the local path may need to be updated
2804 * when a remote path is deleted
2806 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
2807 if (local_pi
&& bgp_evpn_attr_is_local_es(local_pi
->attr
))
2808 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, local_pi
,
2811 /* Unlock route node. */
2812 bgp_dest_unlock_node(dest
);
2818 * Given a route entry and a VRF, see if this route entry should be
2819 * imported into the VRF i.e., RTs match.
2821 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2822 struct bgp_path_info
*pi
)
2824 struct attr
*attr
= pi
->attr
;
2825 struct ecommunity
*ecom
;
2829 /* Route should have valid RT to be even considered. */
2830 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2833 ecom
= bgp_attr_get_ecommunity(attr
);
2834 if (!ecom
|| !ecom
->size
)
2837 /* For each extended community RT, see if it matches this VNI. If any RT
2838 * matches, we're done.
2840 for (i
= 0; i
< ecom
->size
; i
++) {
2842 uint8_t type
, sub_type
;
2843 struct ecommunity_val
*eval
;
2844 struct ecommunity_val eval_tmp
;
2845 struct vrf_irt_node
*irt
;
2847 /* Only deal with RTs */
2848 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
2849 eval
= (struct ecommunity_val
*)(ecom
->val
2850 + (i
* ecom
->unit_size
));
2853 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2856 /* See if this RT matches specified VNIs import RTs */
2857 irt
= lookup_vrf_import_rt(eval
);
2859 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2862 /* Also check for non-exact match. In this, we mask out the AS
2864 * only check on the local-admin sub-field. This is to
2866 * VNI as the RT for EBGP peering too.
2869 if (type
== ECOMMUNITY_ENCODE_AS
2870 || type
== ECOMMUNITY_ENCODE_AS4
2871 || type
== ECOMMUNITY_ENCODE_IP
) {
2872 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
2873 mask_ecom_global_admin(&eval_tmp
, eval
);
2874 irt
= lookup_vrf_import_rt(&eval_tmp
);
2877 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2885 * Given a route entry and a VNI, see if this route entry should be
2886 * imported into the VNI i.e., RTs match.
2888 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2889 struct bgp_path_info
*pi
)
2891 struct attr
*attr
= pi
->attr
;
2892 struct ecommunity
*ecom
;
2896 /* Route should have valid RT to be even considered. */
2897 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2900 ecom
= bgp_attr_get_ecommunity(attr
);
2901 if (!ecom
|| !ecom
->size
)
2904 /* For each extended community RT, see if it matches this VNI. If any RT
2905 * matches, we're done.
2907 for (i
= 0; i
< ecom
->size
; i
++) {
2909 uint8_t type
, sub_type
;
2910 struct ecommunity_val
*eval
;
2911 struct ecommunity_val eval_tmp
;
2912 struct irt_node
*irt
;
2914 /* Only deal with RTs */
2915 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
2916 eval
= (struct ecommunity_val
*)(ecom
->val
2917 + (i
* ecom
->unit_size
));
2920 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2923 /* See if this RT matches specified VNIs import RTs */
2924 irt
= lookup_import_rt(bgp
, eval
);
2926 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2929 /* Also check for non-exact match. In this, we mask out the AS
2931 * only check on the local-admin sub-field. This is to
2933 * VNI as the RT for EBGP peering too.
2936 if (type
== ECOMMUNITY_ENCODE_AS
2937 || type
== ECOMMUNITY_ENCODE_AS4
2938 || type
== ECOMMUNITY_ENCODE_IP
) {
2939 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
2940 mask_ecom_global_admin(&eval_tmp
, eval
);
2941 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2944 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2951 /* This API will scan evpn routes for checking attribute's rmac
2952 * macthes with bgp instance router mac. It avoid installing
2953 * route into bgp vrf table and remote rmac in bridge table.
2955 static int bgp_evpn_route_rmac_self_check(struct bgp
*bgp_vrf
,
2956 const struct prefix_evpn
*evp
,
2957 struct bgp_path_info
*pi
)
2959 /* evpn route could have learnt prior to L3vni has come up,
2960 * perform rmac check before installing route and
2961 * remote router mac.
2962 * The route will be removed from global bgp table once
2963 * SVI comes up with MAC and stored in hash, triggers
2964 * bgp_mac_rescan_all_evpn_tables.
2966 if (memcmp(&bgp_vrf
->rmac
, &pi
->attr
->rmac
, ETH_ALEN
) == 0) {
2967 if (bgp_debug_update(pi
->peer
, NULL
, NULL
, 1)) {
2968 char attr_str
[BUFSIZ
] = {0};
2970 bgp_dump_attr(pi
->attr
, attr_str
, sizeof(attr_str
));
2973 "%s: bgp %u prefix %pFX with attr %s - DENIED due to self mac",
2974 __func__
, bgp_vrf
->vrf_id
, evp
, attr_str
);
2983 /* don't import hosts that are locally attached */
2985 bgp_evpn_skip_vrf_import_of_local_es(struct bgp
*bgp_vrf
,
2986 const struct prefix_evpn
*evp
,
2987 struct bgp_path_info
*pi
, int install
)
2991 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
2992 esi
= bgp_evpn_attr_get_esi(pi
->attr
);
2994 /* Don't import routes that point to a local destination */
2995 if (bgp_evpn_attr_is_local_es(pi
->attr
)) {
2996 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
2997 char esi_buf
[ESI_STR_LEN
];
3000 "vrf %s of evpn prefix %pFX skipped, local es %s",
3001 install
? "import" : "unimport", evp
,
3002 esi_to_str(esi
, esi_buf
,
3012 * Install or uninstall a mac-ip route in the provided vrf if
3013 * there is a rt match
3015 int bgp_evpn_route_entry_install_if_vrf_match(struct bgp
*bgp_vrf
,
3016 struct bgp_path_info
*pi
,
3020 const struct prefix_evpn
*evp
=
3021 (const struct prefix_evpn
*)bgp_dest_get_prefix(pi
->net
);
3023 /* Consider "valid" remote routes applicable for
3026 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3027 && pi
->type
== ZEBRA_ROUTE_BGP
3028 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3031 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
3032 if (bgp_evpn_route_rmac_self_check(bgp_vrf
, evp
, pi
))
3035 /* don't import hosts that are locally attached */
3036 if (install
&& bgp_evpn_skip_vrf_import_of_local_es(
3037 bgp_vrf
, evp
, pi
, install
))
3041 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3043 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3047 flog_err(EC_BGP_EVPN_FAIL
,
3048 "Failed to %s EVPN %pFX route in VRF %s",
3049 install
? "install" : "uninstall", evp
,
3050 vrf_id_to_name(bgp_vrf
->vrf_id
));
3057 * Install or uninstall mac-ip routes are appropriate for this
3060 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
3064 struct bgp_dest
*rd_dest
, *dest
;
3065 struct bgp_table
*table
;
3066 struct bgp_path_info
*pi
;
3068 struct bgp
*bgp_evpn
= NULL
;
3072 bgp_evpn
= bgp_get_evpn();
3076 /* Walk entire global routing table and evaluate routes which could be
3077 * imported into this VRF. Note that we need to loop through all global
3078 * routes to determine which route matches the import rt on vrf
3080 for (rd_dest
= bgp_table_top(bgp_evpn
->rib
[afi
][safi
]); rd_dest
;
3081 rd_dest
= bgp_route_next(rd_dest
)) {
3082 table
= bgp_dest_get_bgp_table_info(rd_dest
);
3086 for (dest
= bgp_table_top(table
); dest
;
3087 dest
= bgp_route_next(dest
)) {
3088 const struct prefix_evpn
*evp
=
3089 (const struct prefix_evpn
*)bgp_dest_get_prefix(
3092 /* if not mac-ip route skip this route */
3093 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3094 || evp
->prefix
.route_type
3095 == BGP_EVPN_IP_PREFIX_ROUTE
))
3098 /* if not a mac+ip route skip this route */
3099 if (!(is_evpn_prefix_ipaddr_v4(evp
)
3100 || is_evpn_prefix_ipaddr_v6(evp
)))
3103 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
3105 ret
= bgp_evpn_route_entry_install_if_vrf_match(
3106 bgp_vrf
, pi
, install
);
3117 * Install or uninstall routes of specified type that are appropriate for this
3120 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
3121 struct bgpevpn
*vpn
,
3122 bgp_evpn_route_type rtype
,
3127 struct bgp_dest
*rd_dest
, *dest
;
3128 struct bgp_table
*table
;
3129 struct bgp_path_info
*pi
;
3135 /* Walk entire global routing table and evaluate routes which could be
3136 * imported into this VPN. Note that we cannot just look at the routes
3138 * the VNI's RD - remote routes applicable for this VNI could have any
3141 /* EVPN routes are a 2-level table. */
3142 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
3143 rd_dest
= bgp_route_next(rd_dest
)) {
3144 table
= bgp_dest_get_bgp_table_info(rd_dest
);
3148 for (dest
= bgp_table_top(table
); dest
;
3149 dest
= bgp_route_next(dest
)) {
3150 const struct prefix_evpn
*evp
=
3151 (const struct prefix_evpn
*)bgp_dest_get_prefix(
3154 if (evp
->prefix
.route_type
!= rtype
)
3157 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
3159 /* Consider "valid" remote routes applicable for
3161 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3162 && pi
->type
== ZEBRA_ROUTE_BGP
3163 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3166 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3168 ret
= install_evpn_route_entry(
3171 ret
= uninstall_evpn_route_entry(
3177 "%u: Failed to %s EVPN %s route in VNI %u",
3181 rtype
== BGP_EVPN_MAC_IP_ROUTE
3186 bgp_dest_unlock_node(rd_dest
);
3187 bgp_dest_unlock_node(dest
);
3198 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3199 * is invoked upon l3vni-add or l3vni import rt change
3201 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3203 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3208 * Install any existing remote routes applicable for this VNI into its
3209 * routing table. This is invoked when a VNI becomes "live" or its Import
3212 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3216 /* Install type-3 routes followed by type-2 routes - the ones applicable
3219 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3224 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_AD_ROUTE
,
3229 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3233 /* uninstall routes from l3vni vrf. */
3234 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3236 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3241 * Uninstall any existing remote routes for this VNI. One scenario in which
3242 * this is invoked is upon an import RT change.
3244 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3248 /* Uninstall type-2 routes followed by type-3 routes - the ones
3252 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3257 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_AD_ROUTE
,
3263 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3268 * Install or uninstall route in matching VRFs (list).
3270 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3271 safi_t safi
, struct prefix_evpn
*evp
,
3272 struct bgp_path_info
*pi
,
3273 struct list
*vrfs
, int install
)
3275 struct bgp
*bgp_vrf
;
3276 struct listnode
*node
, *nnode
;
3278 /* Only type-2/type-5 routes go into a VRF */
3279 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3280 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3283 /* if it is type-2 route and not a mac+ip route skip this route */
3284 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3285 && !(is_evpn_prefix_ipaddr_v4(evp
)
3286 || is_evpn_prefix_ipaddr_v6(evp
)))
3289 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3292 /* don't import hosts that are locally attached */
3293 if (install
&& bgp_evpn_skip_vrf_import_of_local_es(
3294 bgp_vrf
, evp
, pi
, install
))
3298 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3300 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3304 flog_err(EC_BGP_EVPN_FAIL
,
3305 "%u: Failed to %s prefix %pFX in VRF %s",
3307 install
? "install" : "uninstall", evp
,
3308 vrf_id_to_name(bgp_vrf
->vrf_id
));
3317 * Install or uninstall route in matching VNIs (list).
3319 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3320 safi_t safi
, struct prefix_evpn
*evp
,
3321 struct bgp_path_info
*pi
,
3322 struct list
*vnis
, int install
)
3324 struct bgpevpn
*vpn
;
3325 struct listnode
*node
, *nnode
;
3327 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3330 if (!is_vni_live(vpn
))
3334 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3336 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3339 flog_err(EC_BGP_EVPN_FAIL
,
3340 "%u: Failed to %s EVPN %s route in VNI %u",
3341 bgp
->vrf_id
, install
? "install" : "uninstall",
3342 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3354 * Install or uninstall route for appropriate VNIs/ESIs.
3356 static int bgp_evpn_install_uninstall_table(struct bgp
*bgp
, afi_t afi
,
3357 safi_t safi
, const struct prefix
*p
,
3358 struct bgp_path_info
*pi
,
3359 int import
, bool in_vni_rt
,
3362 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3363 struct attr
*attr
= pi
->attr
;
3364 struct ecommunity
*ecom
;
3366 struct prefix_evpn ad_evp
;
3370 /* Only type-1, type-2, type-3, type-4 and type-5
3371 * are supported currently
3373 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3374 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3375 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3376 || evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
3377 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3380 /* If we don't have Route Target, nothing much to do. */
3381 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3384 /* EAD prefix in the global table doesn't include the VTEP-IP so
3385 * we need to create a different copy for the VNI
3387 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
3388 evp
= evpn_type1_prefix_vni_copy(&ad_evp
, evp
, attr
->nexthop
);
3390 ecom
= bgp_attr_get_ecommunity(attr
);
3391 if (!ecom
|| !ecom
->size
)
3394 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3395 * attached to the route */
3396 for (i
= 0; i
< ecom
->size
; i
++) {
3398 uint8_t type
, sub_type
;
3399 struct ecommunity_val
*eval
;
3400 struct ecommunity_val eval_tmp
;
3401 struct irt_node
*irt
; /* import rt for l2vni */
3402 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3403 struct bgp_evpn_es
*es
;
3405 /* Only deal with RTs */
3406 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
3407 eval
= (struct ecommunity_val
*)(ecom
->val
3408 + (i
* ecom
->unit_size
));
3411 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3414 /* non-local MAC-IP routes in the global route table are linked
3415 * to the destination ES
3417 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3418 bgp_evpn_path_es_link(pi
, 0,
3419 bgp_evpn_attr_get_esi(pi
->attr
));
3422 * macip routes (type-2) are imported into VNI and VRF tables.
3423 * IMET route is imported into VNI table.
3424 * prefix routes are imported into VRF table.
3426 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3427 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3428 evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
||
3429 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3431 irt
= in_vni_rt
? lookup_import_rt(bgp
, eval
) : NULL
;
3433 install_uninstall_route_in_vnis(
3434 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3437 vrf_irt
= in_vrf_rt
? lookup_vrf_import_rt(eval
) : NULL
;
3439 install_uninstall_route_in_vrfs(
3440 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3443 /* Also check for non-exact match.
3444 * In this, we mask out the AS and
3445 * only check on the local-admin sub-field.
3446 * This is to facilitate using
3447 * VNI as the RT for EBGP peering too.
3451 if (type
== ECOMMUNITY_ENCODE_AS
3452 || type
== ECOMMUNITY_ENCODE_AS4
3453 || type
== ECOMMUNITY_ENCODE_IP
) {
3454 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
3455 mask_ecom_global_admin(&eval_tmp
, eval
);
3457 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3460 lookup_vrf_import_rt(&eval_tmp
);
3464 install_uninstall_route_in_vnis(
3465 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3468 install_uninstall_route_in_vrfs(
3469 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3473 /* es route is imported into the es table */
3474 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3476 /* we will match based on the entire esi to avoid
3477 * import of an es route for esi2 into esi1
3479 es
= bgp_evpn_es_find(&evp
->prefix
.es_addr
.esi
);
3480 if (es
&& bgp_evpn_is_es_local(es
))
3481 bgp_evpn_es_route_install_uninstall(
3482 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3490 * Install or uninstall route for appropriate VNIs/ESIs.
3492 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3493 const struct prefix
*p
,
3494 struct bgp_path_info
*pi
, int import
)
3496 return bgp_evpn_install_uninstall_table(bgp
, afi
, safi
, p
, pi
, import
,
3500 void bgp_evpn_import_type2_route(struct bgp_path_info
*pi
, int import
)
3502 struct bgp
*bgp_evpn
;
3504 bgp_evpn
= bgp_get_evpn();
3508 install_uninstall_evpn_route(bgp_evpn
, AFI_L2VPN
, SAFI_EVPN
,
3509 &pi
->net
->p
, pi
, import
);
3513 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3516 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3518 /* Delete ipv4 default route and withdraw from peers */
3519 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3520 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3521 SAFI_UNICAST
, false);
3523 /* delete all ipv4 routes and withdraw from peers */
3524 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3525 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3527 /* Delete ipv6 default route and withdraw from peers */
3528 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3529 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3530 SAFI_UNICAST
, false);
3532 /* delete all ipv6 routes and withdraw from peers */
3533 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3534 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3538 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3541 void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3543 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
3545 bgp_evpn
= bgp_get_evpn();
3549 /* update all ipv4 routes */
3550 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3551 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3553 /* update ipv4 default route and withdraw from peers */
3554 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3555 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3556 SAFI_UNICAST
, true);
3558 /* update all ipv6 routes */
3559 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3560 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3562 /* update ipv6 default route and withdraw from peers */
3563 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3564 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3565 SAFI_UNICAST
, true);
3570 * update and advertise local routes for a VRF as type-5 routes.
3571 * This is invoked upon RD change for a VRF. Note taht the processing is only
3572 * done in the global route table using the routes which already exist in the
3575 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3577 /* skip if the RD is configured */
3578 if (is_vrf_rd_configured(bgp_vrf
))
3581 /* derive the RD for the VRF based on new router-id */
3582 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3584 /* update advertise ipv4|ipv6 routes as type-5 routes */
3585 update_advertise_vrf_routes(bgp_vrf
);
3589 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3590 * This is invoked upon VRF RD change. The processing is done only from global
3593 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3595 /* skip if the RD is configured */
3596 if (is_vrf_rd_configured(bgp_vrf
))
3599 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3600 delete_withdraw_vrf_routes(bgp_vrf
);
3604 * Update and advertise local routes for a VNI. Invoked upon router-id
3605 * change. Note that the processing is done only on the global route table
3606 * using routes that already exist in the per-VNI table.
3608 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3610 struct prefix_evpn p
;
3611 struct bgp_dest
*dest
, *global_dest
;
3612 struct bgp_path_info
*pi
, *global_pi
;
3614 afi_t afi
= AFI_L2VPN
;
3615 safi_t safi
= SAFI_EVPN
;
3617 /* Locate type-3 route for VNI in the per-VNI table and use its
3618 * attributes to create and advertise the type-3 route for this VNI
3619 * in the global table.
3621 * RT-3 only if doing head-end replication
3623 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3624 == VXLAN_FLOOD_HEAD_END_REPL
) {
3625 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3626 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3627 if (!dest
) /* unexpected */
3629 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
3630 if (pi
->peer
== bgp
->peer_self
&&
3631 pi
->type
== ZEBRA_ROUTE_BGP
3632 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3634 if (!pi
) /* unexpected */
3638 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
],
3639 afi
, safi
, &p
, &vpn
->prd
);
3640 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_dest
, attr
,
3641 1, &pi
, 0, mac_mobility_seqnum(attr
),
3642 false /* setup_sync */, NULL
/* old_is_sync */);
3644 /* Schedule for processing and unlock node. */
3645 bgp_process(bgp
, global_dest
, afi
, safi
);
3646 bgp_dest_unlock_node(global_dest
);
3649 /* Now, walk this VNI's route table and use the route and its attribute
3650 * to create and schedule route in global table.
3652 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
3653 dest
= bgp_route_next(dest
)) {
3654 const struct prefix_evpn
*evp
=
3655 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
3658 * We have already processed type-3 routes.
3659 * Process only type-1 and type-2 routes here.
3661 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
3662 && evp
->prefix
.route_type
!= BGP_EVPN_AD_ROUTE
)
3665 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
3666 if (pi
->peer
== bgp
->peer_self
3667 && pi
->type
== ZEBRA_ROUTE_BGP
3668 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3673 /* Create route in global routing table using this route entry's
3677 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3679 assert(global_dest
);
3681 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3683 update_evpn_route_entry(
3684 bgp
, vpn
, afi
, safi
, global_dest
, attr
, 1,
3685 &global_pi
, 0, mac_mobility_seqnum(attr
),
3686 false /* setup_sync */, NULL
/* old_is_sync */);
3689 struct bgp_evpn_es
*es
;
3690 int route_changed
= 0;
3692 es
= bgp_evpn_es_find(&evp
->prefix
.ead_addr
.esi
);
3693 bgp_evpn_mh_route_update(bgp
, es
, vpn
, afi
, safi
,
3694 global_dest
, attr
, &global_pi
,
3698 /* Schedule for processing and unlock node. */
3699 bgp_process(bgp
, global_dest
, afi
, safi
);
3700 bgp_dest_unlock_node(global_dest
);
3707 * Delete (and withdraw) local routes for a VNI - only from the global
3708 * table. Invoked upon router-id change.
3710 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3712 struct prefix_evpn p
;
3713 struct bgp_dest
*global_dest
;
3714 struct bgp_path_info
*pi
;
3715 afi_t afi
= AFI_L2VPN
;
3716 safi_t safi
= SAFI_EVPN
;
3718 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3719 * for this VNI - from the global table.
3721 delete_global_type2_routes(bgp
, vpn
);
3723 /* Remove type-3 route for this VNI from global table. */
3724 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3725 global_dest
= bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3726 (const struct prefix_evpn
*)&p
, &vpn
->prd
);
3728 /* Delete route entry in the global EVPN table. */
3729 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
3731 /* Schedule for processing - withdraws to peers happen from
3735 bgp_process(bgp
, global_dest
, afi
, safi
);
3736 bgp_dest_unlock_node(global_dest
);
3740 delete_global_ead_evi_routes(bgp
, vpn
);
3745 * Handle router-id change. Update and advertise local routes corresponding
3746 * to this VNI from peers. Note that this is invoked after updating the
3747 * router-id. The routes in the per-VNI table are used to create routes in
3748 * the global table and schedule them.
3750 static void update_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3752 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3754 /* Skip VNIs with configured RD. */
3755 if (is_rd_configured(vpn
))
3758 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3759 update_advertise_vni_routes(bgp
, vpn
);
3763 * Handle router-id change. Delete and withdraw local routes corresponding
3764 * to this VNI from peers. Note that this is invoked prior to updating
3765 * the router-id and is done only on the global route table, the routes
3766 * are needed in the per-VNI table to re-advertise with new router id.
3768 static void withdraw_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3770 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3772 /* Skip VNIs with configured RD. */
3773 if (is_rd_configured(vpn
))
3776 delete_withdraw_vni_routes(bgp
, vpn
);
3780 * Create RT-3 for a VNI and schedule for processing and advertisement.
3781 * This is invoked upon flooding mode changing to head-end replication.
3783 static void create_advertise_type3(struct hash_bucket
*bucket
, void *data
)
3785 struct bgpevpn
*vpn
= bucket
->data
;
3786 struct bgp
*bgp
= data
;
3787 struct prefix_evpn p
;
3789 if (!vpn
|| !is_vni_live(vpn
) ||
3790 bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3791 != VXLAN_FLOOD_HEAD_END_REPL
)
3794 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3795 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
))
3796 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3797 "Type3 route creation failure for VNI %u", vpn
->vni
);
3801 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3802 * This is invoked upon flooding mode changing to drop BUM packets.
3804 static void delete_withdraw_type3(struct hash_bucket
*bucket
, void *data
)
3806 struct bgpevpn
*vpn
= bucket
->data
;
3807 struct bgp
*bgp
= data
;
3808 struct prefix_evpn p
;
3810 if (!vpn
|| !is_vni_live(vpn
))
3813 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3814 delete_evpn_route(bgp
, vpn
, &p
);
3818 * Process received EVPN type-2 route (advertise or withdraw).
3820 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3821 struct attr
*attr
, uint8_t *pfx
, int psize
,
3822 uint32_t addpath_id
)
3824 struct prefix_rd prd
;
3825 struct prefix_evpn p
= {};
3826 struct bgp_route_evpn evpn
= {};
3828 uint8_t macaddr_len
;
3829 /* holds the VNI(s) as in packet */
3830 mpls_label_t label
[BGP_MAX_LABELS
] = {};
3831 uint32_t num_labels
= 0;
3835 /* Type-2 route should be either 33, 37 or 49 bytes or an
3836 * additional 3 bytes if there is a second label (VNI):
3837 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3838 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3839 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3841 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3842 && psize
!= 40 && psize
!= 52) {
3843 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3844 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3845 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3849 struct stream
*pkt
= stream_new(psize
);
3850 stream_put(pkt
, pfx
, psize
);
3852 /* Make prefix_rd */
3853 prd
.family
= AF_UNSPEC
;
3856 STREAM_GET(&prd
.val
, pkt
, 8);
3858 /* Make EVPN prefix. */
3860 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3861 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3863 /* Copy Ethernet Seg Identifier */
3865 STREAM_GET(&attr
->esi
, pkt
, sizeof(esi_t
));
3867 if (bgp_evpn_is_esi_local_and_non_bypass(&attr
->esi
))
3868 attr
->es_flags
|= ATTR_ES_IS_LOCAL
;
3870 attr
->es_flags
&= ~ATTR_ES_IS_LOCAL
;
3872 STREAM_FORWARD_GETP(pkt
, sizeof(esi_t
));
3875 /* Copy Ethernet Tag */
3876 STREAM_GET(ð_tag
, pkt
, 4);
3877 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3879 /* Get the MAC Addr len */
3880 STREAM_GETC(pkt
, macaddr_len
);
3882 /* Get the MAC Addr */
3883 if (macaddr_len
== (ETH_ALEN
* 8)) {
3884 STREAM_GET(&p
.prefix
.macip_addr
.mac
.octet
, pkt
, ETH_ALEN
);
3887 EC_BGP_EVPN_ROUTE_INVALID
,
3888 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3889 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3895 STREAM_GETC(pkt
, ipaddr_len
);
3897 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3898 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3900 EC_BGP_EVPN_ROUTE_INVALID
,
3901 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3902 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3907 ipaddr_len
/= 8; /* Convert to bytes. */
3908 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3911 STREAM_GET(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pkt
, ipaddr_len
);
3914 /* Get the VNI(s). Stored as bytes here. */
3915 STREAM_GET(&label
[0], pkt
, BGP_LABEL_BYTES
);
3918 /* Do we have a second VNI? */
3919 if (STREAM_READABLE(pkt
)) {
3921 STREAM_GET(&label
[1], pkt
, BGP_LABEL_BYTES
);
3924 /* Process the route. */
3926 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3927 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3928 &prd
, &label
[0], num_labels
, 0, &evpn
);
3930 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3931 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3932 &prd
, &label
[0], num_labels
, &evpn
);
3937 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3938 "%u:%s - Rx EVPN Type-2 NLRI - corrupt, discarding",
3939 peer
->bgp
->vrf_id
, peer
->host
);
3947 * Process received EVPN type-3 route (advertise or withdraw).
3949 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3950 struct attr
*attr
, uint8_t *pfx
, int psize
,
3951 uint32_t addpath_id
)
3953 struct prefix_rd prd
;
3954 struct prefix_evpn p
;
3959 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3960 * IP len (1) and IP (4 or 16).
3962 if (psize
!= 17 && psize
!= 29) {
3963 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3964 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3965 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3969 /* If PMSI is present, log if it is anything other than IR.
3970 * Note: We just simply ignore the values as it is not clear if
3971 * doing anything else is better.
3974 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3975 enum pta_type pmsi_tnl_type
= bgp_attr_get_pmsi_tnl_type(attr
);
3977 if (pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
3978 && pmsi_tnl_type
!= PMSI_TNLTYPE_PIM_SM
) {
3980 EC_BGP_EVPN_PMSI_PRESENT
,
3981 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3982 peer
->bgp
->vrf_id
, peer
->host
, pmsi_tnl_type
);
3986 /* Make prefix_rd */
3987 prd
.family
= AF_UNSPEC
;
3989 memcpy(&prd
.val
, pfx
, 8);
3992 /* Make EVPN prefix. */
3993 memset(&p
, 0, sizeof(p
));
3995 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3996 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3998 /* Copy Ethernet Tag */
3999 memcpy(ð_tag
, pfx
, 4);
4000 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
4004 ipaddr_len
= *pfx
++;
4005 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
4006 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
4007 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
4010 EC_BGP_EVPN_ROUTE_INVALID
,
4011 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
4012 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4016 /* Process the route. */
4018 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4019 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4020 &prd
, NULL
, 0, 0, NULL
);
4022 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4023 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4024 &prd
, NULL
, 0, NULL
);
4029 * Process received EVPN type-5 route (advertise or withdraw).
4031 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4032 struct attr
*attr
, uint8_t *pfx
, int psize
,
4033 uint32_t addpath_id
)
4035 struct prefix_rd prd
;
4036 struct prefix_evpn p
;
4037 struct bgp_route_evpn evpn
;
4040 mpls_label_t label
; /* holds the VNI as in the packet */
4042 bool is_valid_update
= true;
4044 /* Type-5 route should be 34 or 58 bytes:
4045 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
4046 * GW (4 or 16) and VNI (3).
4047 * Note that the IP and GW should both be IPv4 or both IPv6.
4049 if (psize
!= 34 && psize
!= 58) {
4050 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4051 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
4052 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4056 /* Make prefix_rd */
4057 prd
.family
= AF_UNSPEC
;
4059 memcpy(&prd
.val
, pfx
, 8);
4062 /* Make EVPN prefix. */
4063 memset(&p
, 0, sizeof(p
));
4065 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4066 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
4068 /* Additional information outside of prefix - ESI and GW IP */
4069 memset(&evpn
, 0, sizeof(evpn
));
4071 /* Fetch ESI overlay index */
4073 memcpy(&evpn
.eth_s_id
, pfx
, sizeof(esi_t
));
4076 /* Fetch Ethernet Tag. */
4077 memcpy(ð_tag
, pfx
, 4);
4078 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
4081 /* Fetch IP prefix length. */
4083 if (ippfx_len
> IPV6_MAX_BITLEN
) {
4085 EC_BGP_EVPN_ROUTE_INVALID
,
4086 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4087 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
4090 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
4092 /* Determine IPv4 or IPv6 prefix */
4093 /* Since the address and GW are from the same family, this just becomes
4094 * a simple check on the total size.
4097 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
4098 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
4100 SET_IPADDR_V4(&evpn
.gw_ip
);
4101 memcpy(&evpn
.gw_ip
.ipaddr_v4
, pfx
, 4);
4104 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
4105 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
,
4107 pfx
+= IPV6_MAX_BYTELEN
;
4108 SET_IPADDR_V6(&evpn
.gw_ip
);
4109 memcpy(&evpn
.gw_ip
.ipaddr_v6
, pfx
, IPV6_MAX_BYTELEN
);
4110 pfx
+= IPV6_MAX_BYTELEN
;
4113 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4114 memset(&label
, 0, sizeof(label
));
4115 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
4118 * If in future, we are required to access additional fields,
4119 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4124 * An update containing a non-zero gateway IP and a non-zero ESI
4125 * at the same time is should be treated as withdraw
4127 if (bgp_evpn_is_esi_valid(&evpn
.eth_s_id
) &&
4128 !ipaddr_is_zero(&evpn
.gw_ip
)) {
4129 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4130 "%s - Rx EVPN Type-5 ESI and gateway-IP both non-zero.",
4132 is_valid_update
= false;
4133 } else if (bgp_evpn_is_esi_valid(&evpn
.eth_s_id
))
4134 evpn
.type
= OVERLAY_INDEX_ESI
;
4135 else if (!ipaddr_is_zero(&evpn
.gw_ip
))
4136 evpn
.type
= OVERLAY_INDEX_GATEWAY_IP
;
4138 if (is_zero_mac(&attr
->rmac
) &&
4139 !bgp_evpn_is_esi_valid(&evpn
.eth_s_id
) &&
4140 ipaddr_is_zero(&evpn
.gw_ip
) && label
== 0) {
4141 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4142 "%s - Rx EVPN Type-5 ESI, gateway-IP, RMAC and label all zero",
4144 is_valid_update
= false;
4147 if (is_mcast_mac(&attr
->rmac
) || is_bcast_mac(&attr
->rmac
))
4148 is_valid_update
= false;
4151 /* Process the route. */
4152 if (attr
&& is_valid_update
)
4153 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4154 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4155 &prd
, &label
, 1, 0, &evpn
);
4157 if (!is_valid_update
) {
4158 char attr_str
[BUFSIZ
] = {0};
4160 bgp_dump_attr(attr
, attr_str
, BUFSIZ
);
4162 "Invalid update from peer %s vrf %u prefix %pFX attr %s - treat as withdraw",
4163 peer
->hostname
, peer
->bgp
->vrf_id
, &p
,
4166 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4167 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4168 &prd
, &label
, 1, &evpn
);
4174 static void evpn_mpattr_encode_type5(struct stream
*s
, const struct prefix
*p
,
4175 const struct prefix_rd
*prd
,
4176 mpls_label_t
*label
, uint32_t num_labels
,
4181 const struct evpn_addr
*p_evpn_p
;
4183 memset(&temp
, 0, sizeof(temp
));
4184 if (p
->family
!= AF_EVPN
)
4186 p_evpn_p
= &(p
->u
.prefix_evpn
);
4188 /* len denites the total len of IP and GW-IP in the route
4189 IP and GW-IP have to be both ipv4 or ipv6
4191 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4192 len
= 8; /* IP and GWIP are both ipv4 */
4194 len
= 32; /* IP and GWIP are both ipv6 */
4195 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4196 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4197 stream_put(s
, prd
->val
, 8);
4198 if (attr
&& attr
->evpn_overlay
.type
== OVERLAY_INDEX_ESI
)
4199 stream_put(s
, &attr
->esi
, sizeof(esi_t
));
4201 stream_put(s
, 0, sizeof(esi_t
));
4202 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4203 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4204 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4205 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4207 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4208 if (attr
&& attr
->evpn_overlay
.type
== OVERLAY_INDEX_GATEWAY_IP
) {
4209 const struct bgp_route_evpn
*evpn_overlay
=
4210 bgp_attr_get_evpn_overlay(attr
);
4212 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4214 evpn_overlay
->gw_ip
.ipaddr_v4
.s_addr
);
4216 stream_put(s
, &(evpn_overlay
->gw_ip
.ipaddr_v6
), 16);
4218 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4219 stream_put_ipv4(s
, 0);
4221 stream_put(s
, &temp
, 16);
4225 stream_put(s
, label
, 3);
4231 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4233 static void cleanup_vni_on_disable(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4235 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4237 /* Remove EVPN routes and schedule for processing. */
4238 delete_routes_for_vni(bgp
, vpn
);
4240 /* Clear "live" flag and see if hash needs to be freed. */
4241 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4242 if (!is_vni_configured(vpn
))
4243 bgp_evpn_free(bgp
, vpn
);
4247 * Free a VNI entry; iterator function called during cleanup.
4249 static void free_vni_entry(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4251 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4253 delete_all_vni_routes(bgp
, vpn
);
4254 bgp_evpn_free(bgp
, vpn
);
4258 * Derive AUTO import RT for BGP VRF - L3VNI
4260 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4262 struct bgp
*bgp_evpn
= NULL
;
4264 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4265 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4268 bgp_evpn
= bgp_get_evpn();
4271 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4275 * Delete AUTO import RT from BGP VRF - L3VNI
4277 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4279 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4283 * Derive AUTO export RT for BGP VRF - L3VNI
4285 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4287 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4288 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4292 * Delete AUTO export RT from BGP VRF - L3VNI
4294 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4296 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4299 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4301 struct bgp
*bgp_evpn
= NULL
;
4302 struct listnode
*node
= NULL
;
4303 struct bgpevpn
*vpn
= NULL
;
4305 bgp_evpn
= bgp_get_evpn();
4309 /* update all type-5 routes */
4310 update_advertise_vrf_routes(bgp_vrf
);
4312 /* update all type-2 routes */
4313 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4314 update_routes_for_vni(bgp_evpn
, vpn
);
4318 * Handle autort change for a given VNI.
4320 static void update_autort_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4322 struct bgpevpn
*vpn
= bucket
->data
;
4324 if (!is_import_rt_configured(vpn
)) {
4325 if (is_vni_live(vpn
))
4326 bgp_evpn_uninstall_routes(bgp
, vpn
);
4327 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4328 list_delete_all_node(vpn
->import_rtl
);
4329 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4330 if (is_vni_live(vpn
))
4331 bgp_evpn_install_routes(bgp
, vpn
);
4333 if (!is_export_rt_configured(vpn
)) {
4334 list_delete_all_node(vpn
->export_rtl
);
4335 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4336 if (is_vni_live(vpn
))
4337 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4342 * Handle autort change for L3VNI.
4344 static void update_autort_l3vni(struct bgp
*bgp
)
4346 if ((CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
4347 && (CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)))
4350 if (!CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
)) {
4351 if (is_l3vni_live(bgp
))
4352 uninstall_routes_for_vrf(bgp
);
4354 /* Cleanup the RT to VRF mapping */
4355 bgp_evpn_unmap_vrf_from_its_rts(bgp
);
4357 /* Remove auto generated RT */
4358 evpn_auto_rt_import_delete_for_vrf(bgp
);
4360 list_delete_all_node(bgp
->vrf_import_rtl
);
4362 /* Map auto derive or configured RTs */
4363 evpn_auto_rt_import_add_for_vrf(bgp
);
4366 if (!CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
4367 list_delete_all_node(bgp
->vrf_export_rtl
);
4369 evpn_auto_rt_export_delete_for_vrf(bgp
);
4371 evpn_auto_rt_export_add_for_vrf(bgp
);
4373 if (is_l3vni_live(bgp
))
4374 bgp_evpn_map_vrf_to_its_rts(bgp
);
4377 if (!is_l3vni_live(bgp
))
4380 /* advertise type-5 routes if needed */
4381 update_advertise_vrf_routes(bgp
);
4383 /* install all remote routes belonging to this l3vni
4384 * into corresponding vrf
4386 install_routes_for_vrf(bgp
);
4393 /* withdraw type-5 route corresponding to ip prefix */
4394 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4395 afi_t afi
, safi_t safi
)
4398 struct prefix_evpn evp
;
4400 build_type5_prefix_from_ip_prefix(&evp
, p
);
4401 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4404 EC_BGP_EVPN_ROUTE_DELETE
,
4405 "%u failed to delete type-5 route for prefix %pFX in vrf %s",
4406 bgp_vrf
->vrf_id
, p
, vrf_id_to_name(bgp_vrf
->vrf_id
));
4409 /* withdraw all type-5 routes for an address family */
4410 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4412 struct bgp_table
*table
= NULL
;
4413 struct bgp_dest
*dest
= NULL
;
4414 struct bgp_path_info
*pi
;
4416 table
= bgp_vrf
->rib
[afi
][safi
];
4417 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
4418 /* Only care about "selected" routes. Also ensure that
4419 * these are routes that are injectable into EVPN.
4421 /* TODO: Support for AddPath for EVPN. */
4422 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
4423 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4424 && is_route_injectable_into_evpn(pi
)) {
4425 bgp_evpn_withdraw_type5_route(
4426 bgp_vrf
, bgp_dest_get_prefix(dest
), afi
,
4435 * evpn - enable advertisement of default g/w
4437 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
4438 safi_t safi
, bool add
)
4440 struct prefix ip_prefix
;
4442 /* form the default prefix 0.0.0.0/0 */
4443 memset(&ip_prefix
, 0, sizeof(ip_prefix
));
4444 ip_prefix
.family
= afi2family(afi
);
4447 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
4450 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
4457 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4458 * to this function correspond to those of the source IP prefix (best
4459 * path in the case of the attr. In the case of a local prefix (when we
4460 * are advertising local subnets), the src_attr will be NULL.
4462 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4463 struct attr
*src_attr
, afi_t afi
,
4467 struct prefix_evpn evp
;
4469 build_type5_prefix_from_ip_prefix(&evp
, p
);
4470 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
, afi
, safi
);
4472 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4473 "%u: Failed to create type-5 route for prefix %pFX",
4474 bgp_vrf
->vrf_id
, p
);
4477 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4478 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4479 * advertisement is enabled.
4481 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4484 struct bgp_table
*table
= NULL
;
4485 struct bgp_dest
*dest
= NULL
;
4486 struct bgp_path_info
*pi
;
4488 table
= bgp_vrf
->rib
[afi
][safi
];
4489 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
4490 /* Need to identify the "selected" route entry to use its
4491 * attribute. Also, ensure that the route is injectable
4493 * TODO: Support for AddPath for EVPN.
4495 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
4496 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4497 && is_route_injectable_into_evpn(pi
)) {
4499 /* apply the route-map */
4500 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4501 route_map_result_t ret
;
4502 struct bgp_path_info tmp_pi
;
4503 struct bgp_path_info_extra tmp_pie
;
4504 struct attr tmp_attr
;
4506 tmp_attr
= *pi
->attr
;
4508 /* Fill temp path_info */
4509 prep_for_rmap_apply(&tmp_pi
, &tmp_pie
,
4513 RESET_FLAG(tmp_attr
.rmap_change_flags
);
4515 ret
= route_map_apply(
4516 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4518 bgp_dest_get_prefix(dest
),
4520 if (ret
== RMAP_DENYMATCH
) {
4521 bgp_attr_flush(&tmp_attr
);
4524 bgp_evpn_advertise_type5_route(
4526 bgp_dest_get_prefix(dest
),
4527 &tmp_attr
, afi
, safi
);
4529 bgp_evpn_advertise_type5_route(
4531 bgp_dest_get_prefix(dest
),
4532 pi
->attr
, afi
, safi
);
4539 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4541 struct listnode
*node
, *nnode
, *node_to_del
;
4542 struct ecommunity
*ecom
, *ecom_auto
;
4543 struct ecommunity_val eval
;
4545 if (bgp
->advertise_autort_rfc8365
)
4546 vni
|= EVPN_AUTORT_VXLAN
;
4547 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4549 ecom_auto
= ecommunity_new();
4550 ecommunity_add_val(ecom_auto
, &eval
, false, false);
4553 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4554 if (ecommunity_match(ecom
, ecom_auto
)) {
4555 ecommunity_free(&ecom
);
4562 list_delete_node(rtl
, node_to_del
);
4564 ecommunity_free(&ecom_auto
);
4567 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4568 struct ecommunity
*ecomadd
)
4570 /* uninstall routes from vrf */
4571 if (is_l3vni_live(bgp_vrf
))
4572 uninstall_routes_for_vrf(bgp_vrf
);
4574 /* Cleanup the RT to VRF mapping */
4575 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4577 /* Remove auto generated RT */
4578 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4580 /* Add the newly configured RT to RT list */
4581 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4582 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4584 /* map VRF to its RTs and install routes matching the new RTs */
4585 if (is_l3vni_live(bgp_vrf
)) {
4586 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4587 install_routes_for_vrf(bgp_vrf
);
4591 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4592 struct ecommunity
*ecomdel
)
4594 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4595 struct ecommunity
*ecom
= NULL
;
4597 /* uninstall routes from vrf */
4598 if (is_l3vni_live(bgp_vrf
))
4599 uninstall_routes_for_vrf(bgp_vrf
);
4601 /* Cleanup the RT to VRF mapping */
4602 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4604 /* remove the RT from the RT list */
4605 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4606 if (ecommunity_match(ecom
, ecomdel
)) {
4607 ecommunity_free(&ecom
);
4614 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4616 assert(bgp_vrf
->vrf_import_rtl
);
4617 /* fallback to auto import rt, if this was the last RT */
4618 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4619 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4620 if (is_l3vni_live(bgp_vrf
))
4621 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4624 /* map VRFs to its RTs and install routes matching this new RT */
4625 if (is_l3vni_live(bgp_vrf
)) {
4626 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4627 install_routes_for_vrf(bgp_vrf
);
4631 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4632 struct ecommunity
*ecomadd
)
4634 /* remove auto-generated RT */
4635 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4637 /* Add the new RT to the RT list */
4638 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4639 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4641 if (is_l3vni_live(bgp_vrf
))
4642 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4645 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4646 struct ecommunity
*ecomdel
)
4648 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4649 struct ecommunity
*ecom
= NULL
;
4651 /* Remove the RT from the RT list */
4652 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4653 if (ecommunity_match(ecom
, ecomdel
)) {
4654 ecommunity_free(&ecom
);
4661 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4664 * Temporary assert to make SA happy.
4665 * The ALL_LIST_ELEMENTS macro above has a NULL check
4666 * which means that SA is going to complain about
4667 * the list_isempty call, which doesn't NULL check.
4668 * So until we get this situation cleaned up, here
4671 assert(bgp_vrf
->vrf_export_rtl
);
4673 /* fall back to auto-generated RT if this was the last RT */
4674 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4675 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4676 if (is_l3vni_live(bgp_vrf
))
4677 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4680 if (is_l3vni_live(bgp_vrf
))
4681 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4685 * Handle change to BGP router id. This is invoked twice by the change
4686 * handler, first before the router id has been changed and then after
4687 * the router id has been changed. The first invocation will result in
4688 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4689 * will result in the routes being re-advertised.
4691 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4693 struct listnode
*node
;
4694 struct bgp
*bgp_vrf
;
4698 /* delete and withdraw all the type-5 routes
4699 stored in the global table for this vrf
4701 withdraw_router_id_vrf(bgp
);
4703 /* delete all the VNI routes (type-2/type-3) routes for all the
4706 hash_iterate(bgp
->vnihash
,
4707 (void (*)(struct hash_bucket
*,
4708 void *))withdraw_router_id_vni
,
4711 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4712 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4713 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4714 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4716 bgp_vrf
->evpn_info
->pip_ip
.s_addr
4722 /* Assign new default instance router-id */
4723 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4724 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4725 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4726 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4728 bgp_vrf
->evpn_info
->pip_ip
=
4730 /* advertise type-5 routes with
4733 update_advertise_vrf_routes(bgp_vrf
);
4738 /* advertise all routes in the vrf as type-5 routes with the new
4741 update_router_id_vrf(bgp
);
4743 /* advertise all the VNI routes (type-2/type-3) routes with the
4746 hash_iterate(bgp
->vnihash
,
4747 (void (*)(struct hash_bucket
*,
4748 void *))update_router_id_vni
,
4754 * Handle change to auto-RT algorithm - update and advertise local routes.
4756 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4758 hash_iterate(bgp
->vnihash
,
4759 (void (*)(struct hash_bucket
*,
4760 void*))update_autort_vni
,
4763 update_autort_l3vni(bgp
);
4767 * Handle change to export RT - update and advertise local routes.
4769 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4771 return update_routes_for_vni(bgp
, vpn
);
4774 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4777 delete_withdraw_vrf_routes(bgp_vrf
);
4779 update_advertise_vrf_routes(bgp_vrf
);
4783 * Handle change to RD. This is invoked twice by the change handler,
4784 * first before the RD has been changed and then after the RD has
4785 * been changed. The first invocation will result in local routes
4786 * of this VNI being deleted and withdrawn and the next will result
4787 * in the routes being re-advertised.
4789 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4793 delete_withdraw_vni_routes(bgp
, vpn
);
4795 update_advertise_vni_routes(bgp
, vpn
);
4799 * Install routes for this VNI. Invoked upon change to Import RT.
4801 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4803 return install_routes_for_vni(bgp
, vpn
);
4807 * Uninstall all routes installed for this VNI. Invoked upon change
4810 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4812 return uninstall_routes_for_vni(bgp
, vpn
);
4816 * TODO: Hardcoded for a maximum of 2 VNIs right now
4818 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4823 vni1
= label2vni(label
);
4824 if (num_labels
== 2) {
4825 vni2
= label2vni(label
+ 1);
4826 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4828 snprintf(buf
, len
, "%u", vni1
);
4833 * Function to convert evpn route to json format.
4834 * NOTE: We don't use prefix2str as the output here is a bit different.
4836 void bgp_evpn_route2json(const struct prefix_evpn
*p
, json_object
*json
)
4838 char buf1
[ETHER_ADDR_STRLEN
];
4839 char buf2
[PREFIX2STR_BUFFER
];
4846 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4848 switch (p
->prefix
.route_type
) {
4849 case BGP_EVPN_MAC_IP_ROUTE
:
4850 json_object_int_add(json
, "ethTag",
4851 p
->prefix
.macip_addr
.eth_tag
);
4852 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4853 json_object_string_add(json
, "mac",
4854 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4857 if (!is_evpn_prefix_ipaddr_none(p
)) {
4858 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
:
4860 prefixlen
= (family
== AF_INET
) ?
4861 IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
;
4862 inet_ntop(family
, &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4863 buf2
, PREFIX2STR_BUFFER
);
4864 json_object_int_add(json
, "ipLen", prefixlen
);
4865 json_object_string_add(json
, "ip", buf2
);
4869 case BGP_EVPN_IMET_ROUTE
:
4870 json_object_int_add(json
, "ethTag",
4871 p
->prefix
.imet_addr
.eth_tag
);
4872 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4873 prefixlen
= (family
== AF_INET
) ? IPV4_MAX_BITLEN
:
4875 inet_ntop(family
, &p
->prefix
.imet_addr
.ip
.ip
.addr
, buf2
,
4877 json_object_int_add(json
, "ipLen", prefixlen
);
4878 json_object_string_add(json
, "ip", buf2
);
4881 case BGP_EVPN_IP_PREFIX_ROUTE
:
4882 json_object_int_add(json
, "ethTag",
4883 p
->prefix
.prefix_addr
.eth_tag
);
4884 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4885 inet_ntop(family
, &p
->prefix
.prefix_addr
.ip
.ip
.addr
,
4886 buf2
, sizeof(buf2
));
4887 json_object_int_add(json
, "ipLen",
4888 p
->prefix
.prefix_addr
.ip_prefix_length
);
4889 json_object_string_add(json
, "ip", buf2
);
4898 * Encode EVPN prefix in Update (MP_REACH)
4900 void bgp_evpn_encode_prefix(struct stream
*s
, const struct prefix
*p
,
4901 const struct prefix_rd
*prd
, mpls_label_t
*label
,
4902 uint32_t num_labels
, struct attr
*attr
,
4903 bool addpath_capable
, uint32_t addpath_tx_id
)
4905 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4906 int len
, ipa_len
= 0;
4908 if (addpath_capable
)
4909 stream_putl(s
, addpath_tx_id
);
4912 stream_putc(s
, evp
->prefix
.route_type
);
4914 switch (evp
->prefix
.route_type
) {
4915 case BGP_EVPN_MAC_IP_ROUTE
:
4916 if (is_evpn_prefix_ipaddr_v4(evp
))
4917 ipa_len
= IPV4_MAX_BYTELEN
;
4918 else if (is_evpn_prefix_ipaddr_v6(evp
))
4919 ipa_len
= IPV6_MAX_BYTELEN
;
4920 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4921 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4922 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4924 stream_putc(s
, len
);
4925 stream_put(s
, prd
->val
, 8); /* RD */
4927 stream_put(s
, &attr
->esi
, ESI_BYTES
);
4929 stream_put(s
, 0, 10);
4930 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4931 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4932 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4933 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4934 if (ipa_len
) /* IP */
4935 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4937 /* 1st label is the L2 VNI */
4938 stream_put(s
, label
, BGP_LABEL_BYTES
);
4939 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4940 if (ipa_len
&& num_labels
> 1)
4941 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4944 case BGP_EVPN_IMET_ROUTE
:
4945 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4946 stream_put(s
, prd
->val
, 8); /* RD */
4947 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4948 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4949 /* Originating Router's IP Addr */
4950 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4953 case BGP_EVPN_ES_ROUTE
:
4954 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4955 stream_put(s
, prd
->val
, 8); /* RD */
4956 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4957 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4959 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4962 case BGP_EVPN_AD_ROUTE
:
4963 /* RD, ESI, EthTag, 1 VNI */
4964 len
= RD_BYTES
+ ESI_BYTES
+ EVPN_ETH_TAG_BYTES
+ BGP_LABEL_BYTES
;
4965 stream_putc(s
, len
);
4966 stream_put(s
, prd
->val
, RD_BYTES
); /* RD */
4967 stream_put(s
, evp
->prefix
.ead_addr
.esi
.val
, ESI_BYTES
); /* ESI */
4968 stream_putl(s
, evp
->prefix
.ead_addr
.eth_tag
); /* Ethernet Tag */
4969 stream_put(s
, label
, BGP_LABEL_BYTES
);
4972 case BGP_EVPN_IP_PREFIX_ROUTE
:
4973 /* TODO: AddPath support. */
4974 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4982 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4983 struct bgp_nlri
*packet
, int withdraw
)
4989 uint32_t addpath_id
;
4990 bool addpath_capable
;
4995 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4997 lim
= pnt
+ packet
->length
;
4999 safi
= packet
->safi
;
5002 addpath_capable
= bgp_addpath_encode_rx(peer
, afi
, safi
);
5004 for (; pnt
< lim
; pnt
+= psize
) {
5005 /* Clear prefix structure. */
5006 memset(&p
, 0, sizeof(p
));
5008 /* Deal with path-id if AddPath is supported. */
5009 if (addpath_capable
) {
5010 /* When packet overflow occurs return immediately. */
5011 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
5012 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5014 memcpy(&addpath_id
, pnt
, BGP_ADDPATH_ID_LEN
);
5015 addpath_id
= ntohl(addpath_id
);
5016 pnt
+= BGP_ADDPATH_ID_LEN
;
5019 /* All EVPN NLRI types start with type and length. */
5021 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE
;
5026 /* When packet overflow occur return immediately. */
5027 if (pnt
+ psize
> lim
)
5028 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5031 case BGP_EVPN_MAC_IP_ROUTE
:
5032 if (process_type2_route(peer
, afi
, safi
,
5033 withdraw
? NULL
: attr
, pnt
,
5034 psize
, addpath_id
)) {
5037 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
5038 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5039 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE
;
5043 case BGP_EVPN_IMET_ROUTE
:
5044 if (process_type3_route(peer
, afi
, safi
,
5045 withdraw
? NULL
: attr
, pnt
,
5046 psize
, addpath_id
)) {
5049 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
5050 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5051 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE
;
5055 case BGP_EVPN_ES_ROUTE
:
5056 if (bgp_evpn_type4_route_process(peer
, afi
, safi
,
5057 withdraw
? NULL
: attr
, pnt
,
5058 psize
, addpath_id
)) {
5061 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
5062 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5063 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE
;
5067 case BGP_EVPN_AD_ROUTE
:
5068 if (bgp_evpn_type1_route_process(peer
, afi
, safi
,
5069 withdraw
? NULL
: attr
, pnt
,
5070 psize
, addpath_id
)) {
5073 "%u:%s - Error in processing EVPN type-1 NLRI size %d",
5074 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5075 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE1_SIZE
;
5079 case BGP_EVPN_IP_PREFIX_ROUTE
:
5080 if (process_type5_route(peer
, afi
, safi
,
5081 withdraw
? NULL
: attr
, pnt
,
5082 psize
, addpath_id
)) {
5085 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
5086 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5087 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE
;
5096 /* Packet length consistency check. */
5098 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH
;
5100 return BGP_NLRI_PARSE_OK
;
5104 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
5105 * The mapping will be used during route processing.
5106 * bgp_vrf: specific bgp vrf instance on which RT is configured
5108 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
5111 struct ecommunity_val
*eval
= NULL
;
5112 struct listnode
*node
= NULL
, *nnode
= NULL
;
5113 struct ecommunity
*ecom
= NULL
;
5115 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5116 for (i
= 0; i
< ecom
->size
; i
++) {
5117 eval
= (struct ecommunity_val
*)(ecom
->val
5119 * ECOMMUNITY_SIZE
));
5120 map_vrf_to_rt(bgp_vrf
, eval
);
5126 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
5128 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
5131 struct ecommunity_val
*eval
;
5132 struct listnode
*node
, *nnode
;
5133 struct ecommunity
*ecom
;
5135 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5136 for (i
= 0; i
< ecom
->size
; i
++) {
5137 struct vrf_irt_node
*irt
;
5138 struct ecommunity_val eval_tmp
;
5140 eval
= (struct ecommunity_val
*)(ecom
->val
5142 * ECOMMUNITY_SIZE
));
5143 /* If using "automatic" RT, we only care about the
5144 * local-admin sub-field.
5145 * This is to facilitate using VNI as the RT for EBGP
5148 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5149 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
5150 BGP_VRF_IMPORT_RT_CFGD
))
5151 mask_ecom_global_admin(&eval_tmp
, eval
);
5153 irt
= lookup_vrf_import_rt(&eval_tmp
);
5155 unmap_vrf_from_rt(bgp_vrf
, irt
);
5162 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
5163 * The mapping will be used during route processing.
5165 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5168 struct ecommunity_val
*eval
;
5169 struct listnode
*node
, *nnode
;
5170 struct ecommunity
*ecom
;
5172 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5173 for (i
= 0; i
< ecom
->size
; i
++) {
5174 eval
= (struct ecommunity_val
*)(ecom
->val
5176 * ECOMMUNITY_SIZE
));
5177 map_vni_to_rt(bgp
, vpn
, eval
);
5183 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
5185 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5188 struct ecommunity_val
*eval
;
5189 struct listnode
*node
, *nnode
;
5190 struct ecommunity
*ecom
;
5192 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5193 for (i
= 0; i
< ecom
->size
; i
++) {
5194 struct irt_node
*irt
;
5195 struct ecommunity_val eval_tmp
;
5197 eval
= (struct ecommunity_val
*)(ecom
->val
5199 * ECOMMUNITY_SIZE
));
5200 /* If using "automatic" RT, we only care about the
5201 * local-admin sub-field.
5202 * This is to facilitate using VNI as the RT for EBGP
5205 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5206 if (!is_import_rt_configured(vpn
))
5207 mask_ecom_global_admin(&eval_tmp
, eval
);
5209 irt
= lookup_import_rt(bgp
, &eval_tmp
);
5211 unmap_vni_from_rt(bgp
, vpn
, irt
);
5217 * Derive Import RT automatically for VNI and map VNI to RT.
5218 * The mapping will be used during route processing.
5220 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5222 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
5223 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
5226 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
5230 * Derive Export RT automatically for VNI.
5232 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5234 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
5235 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5239 * Derive RD automatically for VNI using passed information - it
5240 * is of the form RouterId:unique-id-for-vni.
5242 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5244 if (is_vrf_rd_configured(bgp
))
5247 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5251 * Derive RD automatically for VNI using passed information - it
5252 * is of the form RouterId:unique-id-for-vni.
5254 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5256 char buf
[BGP_EVPN_PREFIX_RD_LEN
];
5258 vpn
->prd
.family
= AF_UNSPEC
;
5259 vpn
->prd
.prefixlen
= 64;
5260 snprintfrr(buf
, sizeof(buf
), "%pI4:%hu", &bgp
->router_id
, vpn
->rd_id
);
5261 (void)str2prefix_rd(buf
, &vpn
->prd
);
5262 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5268 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5270 struct list
*inst
= bm
->bgp
;
5271 struct listnode
*node
;
5272 struct bgp
*bgp_vrf
;
5274 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5275 if (bgp_vrf
->l3vni
== vni
)
5285 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5287 struct bgpevpn
*vpn
;
5290 memset(&tmp
, 0, sizeof(tmp
));
5292 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5297 * Create a new vpn - invoked upon configuration or zebra notification.
5299 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5300 struct in_addr originator_ip
,
5301 vrf_id_t tenant_vrf_id
,
5302 struct in_addr mcast_grp
,
5303 ifindex_t svi_ifindex
)
5305 struct bgpevpn
*vpn
;
5307 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5309 /* Set values - RD and RT set to defaults. */
5311 vpn
->originator_ip
= originator_ip
;
5312 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5313 vpn
->mcast_grp
= mcast_grp
;
5314 vpn
->svi_ifindex
= svi_ifindex
;
5316 /* Initialize route-target import and export lists */
5317 vpn
->import_rtl
= list_new();
5318 vpn
->import_rtl
->cmp
=
5319 (int (*)(void *, void *))bgp_evpn_route_target_cmp
;
5320 vpn
->import_rtl
->del
= bgp_evpn_xxport_delete_ecomm
;
5321 vpn
->export_rtl
= list_new();
5322 vpn
->export_rtl
->cmp
=
5323 (int (*)(void *, void *))bgp_evpn_route_target_cmp
;
5324 vpn
->export_rtl
->del
= bgp_evpn_xxport_delete_ecomm
;
5325 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5326 derive_rd_rt_for_vni(bgp
, vpn
);
5328 /* Initialize EVPN route table. */
5329 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5332 (void)hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
);
5334 bgp_evpn_remote_ip_hash_init(vpn
);
5335 bgp_evpn_link_to_vni_svi_hash(bgp
, vpn
);
5337 /* add to l2vni list on corresponding vrf */
5338 bgpevpn_link_to_l3vni(vpn
);
5340 bgp_evpn_vni_es_init(vpn
);
5342 QOBJ_REG(vpn
, bgpevpn
);
5347 * Free a given VPN - called in multiple scenarios such as zebra
5348 * notification, configuration being deleted, advertise-all-vni disabled etc.
5349 * This just frees appropriate memory, caller should have taken other
5352 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5354 bgp_evpn_remote_ip_hash_destroy(vpn
);
5355 bgp_evpn_vni_es_cleanup(vpn
);
5356 bgpevpn_unlink_from_l3vni(vpn
);
5357 bgp_table_unlock(vpn
->route_table
);
5358 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5359 list_delete(&vpn
->import_rtl
);
5360 list_delete(&vpn
->export_rtl
);
5361 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5362 hash_release(bgp
->vni_svi_hash
, vpn
);
5363 hash_release(bgp
->vnihash
, vpn
);
5365 XFREE(MTYPE_BGP_EVPN
, vpn
);
5368 static void hash_evpn_free(struct bgpevpn
*vpn
)
5370 XFREE(MTYPE_BGP_EVPN
, vpn
);
5374 * Import evpn route from global table to VNI/VRF/ESI.
5376 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5377 const struct prefix
*p
, struct bgp_path_info
*pi
)
5379 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5383 * Unimport evpn route from VNI/VRF/ESI.
5385 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5386 const struct prefix
*p
, struct bgp_path_info
*pi
)
5388 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5391 /* filter routes which have martian next hops */
5392 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5396 struct bgp_dest
*rd_dest
, *dest
;
5397 struct bgp_table
*table
;
5398 struct bgp_path_info
*pi
;
5403 /* Walk entire global routing table and evaluate routes which could be
5404 * imported into this VPN. Note that we cannot just look at the routes
5405 * for the VNI's RD -
5406 * remote routes applicable for this VNI could have any RD.
5408 /* EVPN routes are a 2-level table. */
5409 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
5410 rd_dest
= bgp_route_next(rd_dest
)) {
5411 table
= bgp_dest_get_bgp_table_info(rd_dest
);
5415 for (dest
= bgp_table_top(table
); dest
;
5416 dest
= bgp_route_next(dest
)) {
5418 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
5421 /* Consider "valid" remote routes applicable for
5423 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5424 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5426 if (bgp_nexthop_self(bgp
, afi
, pi
->type
,
5427 pi
->sub_type
, pi
->attr
,
5429 const struct prefix
*p
=
5430 bgp_dest_get_prefix(dest
);
5432 if (bgp_debug_update(pi
->peer
, p
, NULL
,
5434 char attr_str
[BUFSIZ
] = {0};
5436 bgp_dump_attr(pi
->attr
,
5441 "%u: prefix %pBD with attr %s - DENIED due to martian or self nexthop",
5445 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5448 bgp_rib_remove(dest
, pi
, pi
->peer
, afi
,
5459 * Handle del of a local MACIP.
5461 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5462 struct ipaddr
*ip
, int state
)
5464 struct bgpevpn
*vpn
;
5465 struct prefix_evpn p
;
5466 struct bgp_dest
*dest
;
5468 /* Lookup VNI hash - should exist. */
5469 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5470 if (!vpn
|| !is_vni_live(vpn
)) {
5471 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5472 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5473 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5477 build_evpn_type2_prefix(&p
, mac
, ip
);
5478 if (state
== ZEBRA_NEIGH_ACTIVE
) {
5479 /* Remove EVPN type-2 route and schedule for processing. */
5480 delete_evpn_route(bgp
, vpn
, &p
);
5482 /* Re-instate the current remote best path if any */
5483 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
5485 evpn_zebra_reinstall_best_route(bgp
, vpn
, dest
);
5486 bgp_dest_unlock_node(dest
);
5494 * Handle add of a local MACIP.
5496 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5497 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
, esi_t
*esi
)
5499 struct bgpevpn
*vpn
;
5500 struct prefix_evpn p
;
5502 /* Lookup VNI hash - should exist. */
5503 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5504 if (!vpn
|| !is_vni_live(vpn
)) {
5505 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5506 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5507 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5511 /* Create EVPN type-2 route and schedule for processing. */
5512 build_evpn_type2_prefix(&p
, mac
, ip
);
5513 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
, esi
)) {
5515 EC_BGP_EVPN_ROUTE_CREATE
,
5516 "%u:Failed to create Type-2 route, VNI %u %s MAC %pEA IP %pIA (flags: 0x%x)",
5517 bgp
->vrf_id
, vpn
->vni
,
5518 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5528 static void link_l2vni_hash_to_l3vni(struct hash_bucket
*bucket
,
5529 struct bgp
*bgp_vrf
)
5531 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
5532 struct bgp
*bgp_evpn
= NULL
;
5534 bgp_evpn
= bgp_get_evpn();
5537 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5538 bgpevpn_link_to_l3vni(vpn
);
5541 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
,
5542 struct ethaddr
*svi_rmac
,
5543 struct ethaddr
*vrr_rmac
,
5544 struct in_addr originator_ip
, int filter
,
5545 ifindex_t svi_ifindex
,
5546 bool is_anycast_mac
)
5548 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5549 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5550 struct listnode
*node
= NULL
;
5551 struct bgpevpn
*vpn
= NULL
;
5554 /* get the EVPN instance - required to get the AS number for VRF
5557 bgp_evpn
= bgp_get_evpn();
5561 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
5567 /* if the BGP vrf instance doesn't exist - create one */
5568 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5573 ret
= bgp_get_vty(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5574 vrf_id
== VRF_DEFAULT
5575 ? BGP_INSTANCE_TYPE_DEFAULT
5576 : BGP_INSTANCE_TYPE_VRF
);
5578 case BGP_ERR_AS_MISMATCH
:
5579 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5580 "BGP instance is already running; AS is %u",
5583 case BGP_ERR_INSTANCE_MISMATCH
:
5584 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5585 "BGP instance type mismatch");
5589 /* mark as auto created */
5590 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5593 /* associate the vrf with l3vni and related parameters */
5594 bgp_vrf
->l3vni
= l3vni
;
5595 bgp_vrf
->originator_ip
= originator_ip
;
5596 bgp_vrf
->l3vni_svi_ifindex
= svi_ifindex
;
5597 bgp_vrf
->evpn_info
->is_anycast_mac
= is_anycast_mac
;
5599 /* copy anycast MAC from VRR MAC */
5600 memcpy(&bgp_vrf
->rmac
, vrr_rmac
, ETH_ALEN
);
5601 /* copy sys RMAC from SVI MAC */
5602 memcpy(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, svi_rmac
, ETH_ALEN
);
5603 /* PIP user configured mac is not present use svi mac as sys mac */
5604 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
))
5605 memcpy(&bgp_vrf
->evpn_info
->pip_rmac
, svi_rmac
, ETH_ALEN
);
5607 if (bgp_debug_zebra(NULL
))
5609 "VRF %s vni %u pip %s RMAC %pEA sys RMAC %pEA static RMAC %pEA is_anycast_mac %s",
5610 vrf_id_to_name(bgp_vrf
->vrf_id
), bgp_vrf
->l3vni
,
5611 bgp_vrf
->evpn_info
->advertise_pip
? "enable"
5613 &bgp_vrf
->rmac
, &bgp_vrf
->evpn_info
->pip_rmac
,
5614 &bgp_vrf
->evpn_info
->pip_rmac_static
,
5615 is_anycast_mac
? "Enable" : "Disable");
5617 /* set the right filter - are we using l3vni only for prefix routes? */
5619 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5622 * VNI_FLAG_USE_TWO_LABELS flag for linked L2VNIs should not be
5623 * set before linking vrf to L3VNI. Thus, no need to clear
5627 UNSET_FLAG(bgp_vrf
->vrf_flags
,
5628 BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5630 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5631 if (!CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
5634 * If we are flapping VNI_FLAG_USE_TWO_LABELS
5635 * flag, update all MACIP routes in this VNI
5637 SET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5638 update_all_type2_routes(bgp_evpn
, vpn
);
5643 /* Map auto derive or configured RTs */
5644 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5645 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5647 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
5649 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5650 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5652 /* auto derive RD */
5653 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5655 /* link all corresponding l2vnis */
5656 hash_iterate(bgp_evpn
->vnihash
,
5657 (void (*)(struct hash_bucket
*,
5658 void *))link_l2vni_hash_to_l3vni
,
5661 /* Only update all corresponding type-2 routes if we are advertising two
5662 * labels along with type-2 routes
5665 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5666 update_routes_for_vni(bgp_evpn
, vpn
);
5668 /* advertise type-5 routes if needed */
5669 update_advertise_vrf_routes(bgp_vrf
);
5671 /* install all remote routes belonging to this l3vni into correspondng
5673 install_routes_for_vrf(bgp_vrf
);
5678 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5680 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5681 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5682 struct listnode
*node
= NULL
;
5683 struct listnode
*next
= NULL
;
5684 struct bgpevpn
*vpn
= NULL
;
5686 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5690 "Cannot process L3VNI %u Del - Could not find BGP instance",
5695 bgp_evpn
= bgp_get_evpn();
5699 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
5704 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5705 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5706 * routes. This will uninstalling the routes from zebra and decremnt the
5709 uninstall_routes_for_vrf(bgp_vrf
);
5711 /* delete/withdraw all type-5 routes */
5712 delete_withdraw_vrf_routes(bgp_vrf
);
5714 /* remove the l3vni from vrf instance */
5717 /* remove the Rmac from the BGP vrf */
5718 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5719 memset(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, 0, ETH_ALEN
);
5720 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
) &&
5721 !is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac
))
5722 memset(&bgp_vrf
->evpn_info
->pip_rmac
, 0, ETH_ALEN
);
5724 /* remove default import RT or Unmap non-default import RT */
5725 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5726 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5727 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5728 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5731 /* remove default export RT */
5732 if (!list_isempty(bgp_vrf
->vrf_export_rtl
) &&
5733 !CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
5734 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5737 /* update all corresponding local mac-ip routes */
5738 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5739 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5740 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5741 update_routes_for_vni(bgp_evpn
, vpn
);
5745 /* If any L2VNIs point to this instance, unlink them. */
5746 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5747 bgpevpn_unlink_from_l3vni(vpn
);
5749 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5751 /* Delete the instance if it was autocreated */
5752 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5753 bgp_delete(bgp_vrf
);
5759 * Handle del of a local VNI.
5761 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5763 struct bgpevpn
*vpn
;
5765 /* Locate VNI hash */
5766 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5768 if (bgp_debug_zebra(NULL
))
5770 EC_BGP_EVPN_VPN_VNI
,
5771 "%u: VNI hash entry for VNI %u not found at DEL",
5776 /* Remove all local EVPN routes and schedule for processing (to
5777 * withdraw from peers).
5779 delete_routes_for_vni(bgp
, vpn
);
5781 bgp_evpn_unlink_from_vni_svi_hash(bgp
, vpn
);
5783 vpn
->svi_ifindex
= 0;
5785 * tunnel is no longer active, del tunnel ip address from tip_hash
5787 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5789 /* Clear "live" flag and see if hash needs to be freed. */
5790 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5791 if (!is_vni_configured(vpn
))
5792 bgp_evpn_free(bgp
, vpn
);
5798 * Handle add (or update) of a local VNI. The VNI changes we care
5799 * about are for the local-tunnel-ip and the (tenant) VRF.
5801 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5802 struct in_addr originator_ip
,
5803 vrf_id_t tenant_vrf_id
,
5804 struct in_addr mcast_grp
,
5805 ifindex_t svi_ifindex
)
5807 struct bgpevpn
*vpn
;
5808 struct prefix_evpn p
;
5810 /* Lookup VNI. If present and no change, exit. */
5811 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5814 if (is_vni_live(vpn
)
5815 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5816 && IPV4_ADDR_SAME(&vpn
->mcast_grp
, &mcast_grp
)
5817 && vpn
->tenant_vrf_id
== tenant_vrf_id
5818 && vpn
->svi_ifindex
== svi_ifindex
)
5819 /* Probably some other param has changed that we don't
5823 bgp_evpn_mcast_grp_change(bgp
, vpn
, mcast_grp
);
5825 if (vpn
->svi_ifindex
!= svi_ifindex
) {
5828 * Unresolve all the gateway IP nexthops for this VNI
5831 bgp_evpn_remote_ip_hash_iterate(
5833 (void (*)(struct hash_bucket
*, void *))
5834 bgp_evpn_remote_ip_hash_unlink_nexthop
,
5836 bgp_evpn_unlink_from_vni_svi_hash(bgp
, vpn
);
5837 vpn
->svi_ifindex
= svi_ifindex
;
5838 bgp_evpn_link_to_vni_svi_hash(bgp
, vpn
);
5841 * Resolve all the gateway IP nexthops for this VNI
5844 bgp_evpn_remote_ip_hash_iterate(
5846 (void (*)(struct hash_bucket
*, void *))
5847 bgp_evpn_remote_ip_hash_link_nexthop
,
5851 /* Update tenant_vrf_id if it has changed. */
5852 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5855 * Unresolve all the gateway IP nexthops for this VNI
5858 bgp_evpn_remote_ip_hash_iterate(
5860 (void (*)(struct hash_bucket
*, void *))
5861 bgp_evpn_remote_ip_hash_unlink_nexthop
,
5863 bgpevpn_unlink_from_l3vni(vpn
);
5864 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5865 bgpevpn_link_to_l3vni(vpn
);
5868 * Resolve all the gateway IP nexthops for this VNI
5871 bgp_evpn_remote_ip_hash_iterate(
5873 (void (*)(struct hash_bucket
*, void *))
5874 bgp_evpn_remote_ip_hash_link_nexthop
,
5878 /* If tunnel endpoint IP has changed, update (and delete prior
5879 * type-3 route, if needed.)
5881 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5882 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5884 /* Update all routes with new endpoint IP and/or export RT
5887 if (is_vni_live(vpn
))
5888 update_routes_for_vni(bgp
, vpn
);
5890 /* Create or update as appropriate. */
5891 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
,
5892 mcast_grp
, svi_ifindex
);
5895 /* if the VNI is live already, there is nothing more to do */
5896 if (is_vni_live(vpn
))
5899 /* Mark as "live" */
5900 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5902 /* tunnel is now active, add tunnel-ip to db */
5903 bgp_tip_add(bgp
, &originator_ip
);
5905 /* filter routes as nexthop database has changed */
5906 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5909 * Create EVPN type-3 route and schedule for processing.
5911 * RT-3 only if doing head-end replication
5913 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
5914 == VXLAN_FLOOD_HEAD_END_REPL
) {
5915 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5916 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
)) {
5917 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5918 "%u: Type3 route creation failure for VNI %u",
5924 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5928 install_routes_for_vni(bgp
, vpn
);
5930 /* If we are advertising gateway mac-ip
5931 It needs to be conveyed again to zebra */
5932 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5934 /* advertise svi mac-ip knob to zebra */
5935 bgp_zebra_advertise_svi_macip(bgp
, vpn
->advertise_svi_macip
, vpn
->vni
);
5941 * Handle change in setting for BUM handling. The supported values
5942 * are head-end replication and dropping all BUM packets. Any change
5943 * should be registered with zebra. Also, if doing head-end replication,
5944 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5945 * to be dropped, the RT-3s must be withdrawn.
5947 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
5949 zlog_info("L2VPN EVPN BUM handling is %s",
5950 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
5951 "Flooding" : "Flooding Disabled");
5953 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
5954 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
5955 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
5956 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
5957 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
5961 * Cleanup EVPN information on disable - Need to delete and withdraw
5962 * EVPN routes from peers.
5964 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5966 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_bucket
*,
5967 void *))cleanup_vni_on_disable
,
5972 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5973 * BGP instance (default) is being freed.
5975 void bgp_evpn_cleanup(struct bgp
*bgp
)
5977 hash_iterate(bgp
->vnihash
,
5978 (void (*)(struct hash_bucket
*, void *))free_vni_entry
,
5981 hash_clean(bgp
->import_rt_hash
, (void (*)(void *))hash_import_rt_free
);
5982 hash_free(bgp
->import_rt_hash
);
5983 bgp
->import_rt_hash
= NULL
;
5985 hash_clean(bgp
->vrf_import_rt_hash
,
5986 (void (*)(void *))hash_vrf_import_rt_free
);
5987 hash_free(bgp
->vrf_import_rt_hash
);
5988 bgp
->vrf_import_rt_hash
= NULL
;
5990 hash_clean(bgp
->vni_svi_hash
, (void (*)(void *))hash_evpn_free
);
5991 hash_free(bgp
->vni_svi_hash
);
5992 bgp
->vni_svi_hash
= NULL
;
5993 hash_free(bgp
->vnihash
);
5994 bgp
->vnihash
= NULL
;
5996 list_delete(&bgp
->vrf_import_rtl
);
5997 list_delete(&bgp
->vrf_export_rtl
);
5998 list_delete(&bgp
->l2vnis
);
6002 * Initialization for EVPN
6005 * hash for RT to VNI
6007 void bgp_evpn_init(struct bgp
*bgp
)
6010 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
6012 hash_create(vni_svi_hash_key_make
, vni_svi_hash_cmp
,
6013 "BGP VNI hash based on SVI ifindex");
6014 bgp
->import_rt_hash
=
6015 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
6016 "BGP Import RT Hash");
6017 bgp
->vrf_import_rt_hash
=
6018 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
6019 "BGP VRF Import RT Hash");
6020 bgp
->vrf_import_rtl
= list_new();
6021 bgp
->vrf_import_rtl
->cmp
=
6022 (int (*)(void *, void *))bgp_evpn_route_target_cmp
;
6023 bgp
->vrf_import_rtl
->del
= bgp_evpn_xxport_delete_ecomm
;
6024 bgp
->vrf_export_rtl
= list_new();
6025 bgp
->vrf_export_rtl
->cmp
=
6026 (int (*)(void *, void *))bgp_evpn_route_target_cmp
;
6027 bgp
->vrf_export_rtl
->del
= bgp_evpn_xxport_delete_ecomm
;
6028 bgp
->l2vnis
= list_new();
6029 bgp
->l2vnis
->cmp
= vni_list_cmp
;
6030 /* By default Duplicate Address Dection is enabled.
6031 * Max-moves (N) 5, detection time (M) 180
6032 * default action is warning-only
6033 * freeze action permanently freezes address,
6034 * and freeze time (auto-recovery) is disabled.
6036 if (bgp
->evpn_info
) {
6037 bgp
->evpn_info
->dup_addr_detect
= true;
6038 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
6039 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
6040 bgp
->evpn_info
->dad_freeze
= false;
6041 bgp
->evpn_info
->dad_freeze_time
= 0;
6042 /* Initialize zebra vxlan */
6043 bgp_zebra_dup_addr_detection(bgp
);
6044 /* Enable PIP feature by default for bgp vrf instance */
6045 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
6046 struct bgp
*bgp_default
;
6048 bgp
->evpn_info
->advertise_pip
= true;
6049 bgp_default
= bgp_get_default();
6051 bgp
->evpn_info
->pip_ip
= bgp_default
->router_id
;
6055 /* Default BUM handling is to do head-end replication. */
6056 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
6058 bgp_evpn_nh_init(bgp
);
6061 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
6063 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
6064 bgp_evpn_nh_finish(bgp_vrf
);
6068 * Get the prefixlen of the ip prefix carried within the type5 evpn route.
6070 int bgp_evpn_get_type5_prefixlen(const struct prefix
*pfx
)
6072 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6074 if (!pfx
|| pfx
->family
!= AF_EVPN
)
6077 if (evp
->prefix
.route_type
!= BGP_EVPN_IP_PREFIX_ROUTE
)
6080 return evp
->prefix
.prefix_addr
.ip_prefix_length
;
6084 * Should we register nexthop for this EVPN prefix for nexthop tracking?
6086 bool bgp_evpn_is_prefix_nht_supported(const struct prefix
*pfx
)
6088 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6091 * EVPN routes should be marked as valid only if the nexthop is
6092 * reachable. Only if this happens, the route should be imported
6093 * (into VNI or VRF routing tables) and/or advertised.
6094 * Note: This is currently applied for EVPN type-1, type-2,
6095 * type-3, type-4 and type-5 routes.
6096 * It may be tweaked later on for other routes, or
6097 * even removed completely when all routes are handled.
6099 if (pfx
&& pfx
->family
== AF_EVPN
6100 && (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
6101 || evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
6102 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
6103 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
6104 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
6110 static void *bgp_evpn_remote_ip_hash_alloc(void *p
)
6112 const struct evpn_remote_ip
*key
= (const struct evpn_remote_ip
*)p
;
6113 struct evpn_remote_ip
*ip
;
6115 ip
= XMALLOC(MTYPE_EVPN_REMOTE_IP
, sizeof(struct evpn_remote_ip
));
6117 ip
->macip_path_list
= list_new();
6122 static unsigned int bgp_evpn_remote_ip_hash_key_make(const void *p
)
6124 const struct evpn_remote_ip
*ip
= p
;
6125 const struct ipaddr
*addr
= &ip
->addr
;
6127 if (IS_IPADDR_V4(addr
))
6128 return jhash_1word(addr
->ipaddr_v4
.s_addr
, 0);
6130 return jhash2(addr
->ipaddr_v6
.s6_addr32
,
6131 array_size(addr
->ipaddr_v6
.s6_addr32
), 0);
6134 static bool bgp_evpn_remote_ip_hash_cmp(const void *p1
, const void *p2
)
6136 const struct evpn_remote_ip
*ip1
= p1
;
6137 const struct evpn_remote_ip
*ip2
= p2
;
6139 return !ipaddr_cmp(&ip1
->addr
, &ip2
->addr
);
6142 static void bgp_evpn_remote_ip_hash_init(struct bgpevpn
*vpn
)
6144 if (!evpn_resolve_overlay_index())
6147 vpn
->remote_ip_hash
= hash_create(bgp_evpn_remote_ip_hash_key_make
,
6148 bgp_evpn_remote_ip_hash_cmp
,
6149 "BGP EVPN remote IP hash");
6152 static void bgp_evpn_remote_ip_hash_free(struct hash_bucket
*bucket
, void *args
)
6154 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6155 struct bgpevpn
*vpn
= (struct bgpevpn
*)args
;
6157 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, false);
6159 list_delete(&ip
->macip_path_list
);
6161 hash_release(vpn
->remote_ip_hash
, ip
);
6162 XFREE(MTYPE_EVPN_REMOTE_IP
, ip
);
6165 static void bgp_evpn_remote_ip_hash_destroy(struct bgpevpn
*vpn
)
6167 if (!evpn_resolve_overlay_index() || vpn
->remote_ip_hash
== NULL
)
6170 hash_iterate(vpn
->remote_ip_hash
,
6171 (void (*)(struct hash_bucket
*, void *))bgp_evpn_remote_ip_hash_free
,
6174 hash_free(vpn
->remote_ip_hash
);
6175 vpn
->remote_ip_hash
= NULL
;
6178 /* Add a remote MAC/IP route to hash table */
6179 static void bgp_evpn_remote_ip_hash_add(struct bgpevpn
*vpn
,
6180 struct bgp_path_info
*pi
)
6182 struct evpn_remote_ip tmp
;
6183 struct evpn_remote_ip
*ip
;
6184 struct prefix_evpn
*evp
;
6186 if (!evpn_resolve_overlay_index())
6189 if (pi
->type
!= ZEBRA_ROUTE_BGP
|| pi
->sub_type
!= BGP_ROUTE_IMPORTED
6190 || !CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
))
6193 evp
= (struct prefix_evpn
*)&pi
->net
->p
;
6195 if (evp
->family
!= AF_EVPN
6196 || evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
6197 || is_evpn_prefix_ipaddr_none(evp
))
6200 tmp
.addr
= evp
->prefix
.macip_addr
.ip
;
6201 ip
= hash_lookup(vpn
->remote_ip_hash
, &tmp
);
6203 if (listnode_lookup(ip
->macip_path_list
, pi
) != NULL
)
6205 (void)listnode_add(ip
->macip_path_list
, pi
);
6209 ip
= hash_get(vpn
->remote_ip_hash
, &tmp
, bgp_evpn_remote_ip_hash_alloc
);
6210 (void)listnode_add(ip
->macip_path_list
, pi
);
6212 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, true);
6215 /* Delete a remote MAC/IP route from hash table */
6216 static void bgp_evpn_remote_ip_hash_del(struct bgpevpn
*vpn
,
6217 struct bgp_path_info
*pi
)
6219 struct evpn_remote_ip tmp
;
6220 struct evpn_remote_ip
*ip
;
6221 struct prefix_evpn
*evp
;
6223 if (!evpn_resolve_overlay_index())
6226 evp
= (struct prefix_evpn
*)&pi
->net
->p
;
6228 if (evp
->family
!= AF_EVPN
6229 || evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
6230 || is_evpn_prefix_ipaddr_none(evp
))
6233 tmp
.addr
= evp
->prefix
.macip_addr
.ip
;
6234 ip
= hash_lookup(vpn
->remote_ip_hash
, &tmp
);
6238 listnode_delete(ip
->macip_path_list
, pi
);
6240 if (ip
->macip_path_list
->count
== 0) {
6241 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, false);
6242 hash_release(vpn
->remote_ip_hash
, ip
);
6243 XFREE(MTYPE_EVPN_REMOTE_IP
, ip
);
6247 static void bgp_evpn_remote_ip_hash_iterate(struct bgpevpn
*vpn
,
6248 void (*func
)(struct hash_bucket
*,
6252 if (!evpn_resolve_overlay_index())
6255 hash_iterate(vpn
->remote_ip_hash
, func
, arg
);
6258 static void show_remote_ip_entry(struct hash_bucket
*bucket
, void *args
)
6260 char buf
[INET6_ADDRSTRLEN
];
6261 struct listnode
*node
= NULL
;
6262 struct bgp_path_info
*pi
= NULL
;
6263 struct vty
*vty
= (struct vty
*)args
;
6264 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6266 vty_out(vty
, " Remote IP: %s\n",
6267 ipaddr2str(&ip
->addr
, buf
, sizeof(buf
)));
6268 vty_out(vty
, " Linked MAC/IP routes:\n");
6269 for (ALL_LIST_ELEMENTS_RO(ip
->macip_path_list
, node
, pi
))
6270 vty_out(vty
, " %pFX\n", &pi
->net
->p
);
6273 void bgp_evpn_show_remote_ip_hash(struct hash_bucket
*bucket
, void *args
)
6275 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
6276 struct vty
*vty
= (struct vty
*)args
;
6278 vty_out(vty
, "VNI: %u\n", vpn
->vni
);
6279 bgp_evpn_remote_ip_hash_iterate(
6281 (void (*)(struct hash_bucket
*, void *))show_remote_ip_entry
,
6286 static void bgp_evpn_remote_ip_hash_link_nexthop(struct hash_bucket
*bucket
,
6289 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6290 struct bgpevpn
*vpn
= (struct bgpevpn
*)args
;
6292 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, true);
6295 static void bgp_evpn_remote_ip_hash_unlink_nexthop(struct hash_bucket
*bucket
,
6298 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6299 struct bgpevpn
*vpn
= (struct bgpevpn
*)args
;
6301 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, false);
6304 static unsigned int vni_svi_hash_key_make(const void *p
)
6306 const struct bgpevpn
*vpn
= p
;
6308 return jhash_1word(vpn
->svi_ifindex
, 0);
6311 static bool vni_svi_hash_cmp(const void *p1
, const void *p2
)
6313 const struct bgpevpn
*vpn1
= p1
;
6314 const struct bgpevpn
*vpn2
= p2
;
6316 return (vpn1
->svi_ifindex
== vpn2
->svi_ifindex
);
6319 static struct bgpevpn
*bgp_evpn_vni_svi_hash_lookup(struct bgp
*bgp
,
6322 struct bgpevpn
*vpn
;
6325 memset(&tmp
, 0, sizeof(tmp
));
6326 tmp
.svi_ifindex
= svi
;
6327 vpn
= hash_lookup(bgp
->vni_svi_hash
, &tmp
);
6331 static void bgp_evpn_link_to_vni_svi_hash(struct bgp
*bgp
, struct bgpevpn
*vpn
)
6333 if (vpn
->svi_ifindex
== 0)
6336 (void)hash_get(bgp
->vni_svi_hash
, vpn
, hash_alloc_intern
);
6339 static void bgp_evpn_unlink_from_vni_svi_hash(struct bgp
*bgp
,
6340 struct bgpevpn
*vpn
)
6342 if (vpn
->svi_ifindex
== 0)
6345 hash_release(bgp
->vni_svi_hash
, vpn
);
6348 void bgp_evpn_show_vni_svi_hash(struct hash_bucket
*bucket
, void *args
)
6350 struct bgpevpn
*evpn
= (struct bgpevpn
*)bucket
->data
;
6351 struct vty
*vty
= (struct vty
*)args
;
6353 vty_out(vty
, "SVI: %u VNI: %u\n", evpn
->svi_ifindex
, evpn
->vni
);
6357 * This function is called for a bgp_nexthop_cache entry when the nexthop is
6358 * gateway IP overlay index.
6359 * This function returns true if there is a remote MAC/IP route for the gateway
6360 * IP in the EVI of the nexthop SVI.
6362 bool bgp_evpn_is_gateway_ip_resolved(struct bgp_nexthop_cache
*bnc
)
6364 struct bgp
*bgp_evpn
= NULL
;
6365 struct bgpevpn
*vpn
= NULL
;
6366 struct evpn_remote_ip tmp
;
6369 if (!evpn_resolve_overlay_index())
6372 if (!bnc
->nexthop
|| bnc
->nexthop
->ifindex
== 0)
6375 bgp_evpn
= bgp_get_evpn();
6380 * Gateway IP is resolved by nht over SVI interface.
6381 * Use this SVI to find corresponding EVI(L2 context)
6383 vpn
= bgp_evpn_vni_svi_hash_lookup(bgp_evpn
, bnc
->nexthop
->ifindex
);
6387 if (vpn
->bgp_vrf
!= bnc
->bgp
)
6391 * Check if the gateway IP is present in the EVI remote_ip_hash table
6392 * which stores all the remote IP addresses received via MAC/IP routes
6395 memset(&tmp
, 0, sizeof(tmp
));
6398 if (p
->family
== AF_INET
) {
6399 tmp
.addr
.ipa_type
= IPADDR_V4
;
6400 memcpy(&(tmp
.addr
.ipaddr_v4
), &(p
->u
.prefix4
),
6401 sizeof(struct in_addr
));
6402 } else if (p
->family
== AF_INET6
) {
6403 tmp
.addr
.ipa_type
= IPADDR_V6
;
6404 memcpy(&(tmp
.addr
.ipaddr_v6
), &(p
->u
.prefix6
),
6405 sizeof(struct in6_addr
));
6409 if (hash_lookup(vpn
->remote_ip_hash
, &tmp
) == NULL
)
6415 /* Resolve/Unresolve nexthops when a MAC/IP route is added/deleted */
6416 static void bgp_evpn_remote_ip_process_nexthops(struct bgpevpn
*vpn
,
6417 struct ipaddr
*addr
,
6422 struct bgp_nexthop_cache
*bnc
;
6423 struct bgp_nexthop_cache_head
*tree
= NULL
;
6425 if (!vpn
->bgp_vrf
|| vpn
->svi_ifindex
== 0)
6428 memset(&p
, 0, sizeof(p
));
6430 if (addr
->ipa_type
== IPADDR_V4
) {
6433 memcpy(&(p
.u
.prefix4
), &(addr
->ipaddr_v4
),
6434 sizeof(struct in_addr
));
6435 p
.prefixlen
= IPV4_MAX_BITLEN
;
6436 } else if (addr
->ipa_type
== IPADDR_V6
) {
6438 p
.family
= AF_INET6
;
6439 memcpy(&(p
.u
.prefix6
), &(addr
->ipaddr_v6
),
6440 sizeof(struct in6_addr
));
6441 p
.prefixlen
= IPV6_MAX_BITLEN
;
6445 tree
= &vpn
->bgp_vrf
->nexthop_cache_table
[afi
];
6446 bnc
= bnc_find(tree
, &p
, 0, 0);
6448 if (!bnc
|| !bnc
->is_evpn_gwip_nexthop
)
6451 if (!bnc
->nexthop
|| bnc
->nexthop
->ifindex
!= vpn
->svi_ifindex
)
6454 if (BGP_DEBUG(nht
, NHT
))
6455 zlog_debug("%s(%u): vni %u mac/ip %s for NH %pFX",
6456 vpn
->bgp_vrf
->name_pretty
, vpn
->tenant_vrf_id
,
6457 vpn
->vni
, (resolve
? "add" : "delete"),
6461 * MAC/IP route or SVI or tenant vrf being added to EVI.
6462 * Set nexthop as valid only if it is already L3 reachable
6464 if (resolve
&& bnc
->flags
& BGP_NEXTHOP_EVPN_INCOMPLETE
) {
6465 bnc
->flags
&= ~BGP_NEXTHOP_EVPN_INCOMPLETE
;
6466 bnc
->flags
|= BGP_NEXTHOP_VALID
;
6467 bnc
->change_flags
|= BGP_NEXTHOP_MACIP_CHANGED
;
6468 evaluate_paths(bnc
);
6471 /* MAC/IP route or SVI or tenant vrf being deleted from EVI */
6472 if (!resolve
&& bnc
->flags
& BGP_NEXTHOP_VALID
) {
6473 bnc
->flags
&= ~BGP_NEXTHOP_VALID
;
6474 bnc
->flags
|= BGP_NEXTHOP_EVPN_INCOMPLETE
;
6475 bnc
->change_flags
|= BGP_NEXTHOP_MACIP_CHANGED
;
6476 evaluate_paths(bnc
);
6480 void bgp_evpn_handle_resolve_overlay_index_set(struct hash_bucket
*bucket
,
6483 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
6484 struct bgp_dest
*dest
;
6485 struct bgp_path_info
*pi
;
6487 bgp_evpn_remote_ip_hash_init(vpn
);
6489 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
6490 dest
= bgp_route_next(dest
))
6491 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
6492 bgp_evpn_remote_ip_hash_add(vpn
, pi
);
6495 void bgp_evpn_handle_resolve_overlay_index_unset(struct hash_bucket
*bucket
,
6498 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
6500 bgp_evpn_remote_ip_hash_destroy(vpn
);