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
)
3635 bgp_dest_unlock_node(dest
);
3640 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
],
3641 afi
, safi
, &p
, &vpn
->prd
);
3642 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_dest
, attr
,
3643 1, &pi
, 0, mac_mobility_seqnum(attr
),
3644 false /* setup_sync */, NULL
/* old_is_sync */);
3646 /* Schedule for processing and unlock node. */
3647 bgp_process(bgp
, global_dest
, afi
, safi
);
3648 bgp_dest_unlock_node(global_dest
);
3651 /* Now, walk this VNI's route table and use the route and its attribute
3652 * to create and schedule route in global table.
3654 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
3655 dest
= bgp_route_next(dest
)) {
3656 const struct prefix_evpn
*evp
=
3657 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
3660 * We have already processed type-3 routes.
3661 * Process only type-1 and type-2 routes here.
3663 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
3664 && evp
->prefix
.route_type
!= BGP_EVPN_AD_ROUTE
)
3667 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
3668 if (pi
->peer
== bgp
->peer_self
3669 && pi
->type
== ZEBRA_ROUTE_BGP
3670 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3675 /* Create route in global routing table using this route entry's
3679 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3681 assert(global_dest
);
3683 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3685 update_evpn_route_entry(
3686 bgp
, vpn
, afi
, safi
, global_dest
, attr
, 1,
3687 &global_pi
, 0, mac_mobility_seqnum(attr
),
3688 false /* setup_sync */, NULL
/* old_is_sync */);
3691 struct bgp_evpn_es
*es
;
3692 int route_changed
= 0;
3694 es
= bgp_evpn_es_find(&evp
->prefix
.ead_addr
.esi
);
3695 bgp_evpn_mh_route_update(bgp
, es
, vpn
, afi
, safi
,
3696 global_dest
, attr
, &global_pi
,
3700 /* Schedule for processing and unlock node. */
3701 bgp_process(bgp
, global_dest
, afi
, safi
);
3702 bgp_dest_unlock_node(global_dest
);
3709 * Delete (and withdraw) local routes for a VNI - only from the global
3710 * table. Invoked upon router-id change.
3712 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3714 struct prefix_evpn p
;
3715 struct bgp_dest
*global_dest
;
3716 struct bgp_path_info
*pi
;
3717 afi_t afi
= AFI_L2VPN
;
3718 safi_t safi
= SAFI_EVPN
;
3720 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3721 * for this VNI - from the global table.
3723 delete_global_type2_routes(bgp
, vpn
);
3725 /* Remove type-3 route for this VNI from global table. */
3726 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3727 global_dest
= bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3728 (const struct prefix_evpn
*)&p
, &vpn
->prd
);
3730 /* Delete route entry in the global EVPN table. */
3731 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
3733 /* Schedule for processing - withdraws to peers happen from
3737 bgp_process(bgp
, global_dest
, afi
, safi
);
3738 bgp_dest_unlock_node(global_dest
);
3742 delete_global_ead_evi_routes(bgp
, vpn
);
3747 * Handle router-id change. Update and advertise local routes corresponding
3748 * to this VNI from peers. Note that this is invoked after updating the
3749 * router-id. The routes in the per-VNI table are used to create routes in
3750 * the global table and schedule them.
3752 static void update_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3754 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3756 /* Skip VNIs with configured RD. */
3757 if (is_rd_configured(vpn
))
3760 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3761 update_advertise_vni_routes(bgp
, vpn
);
3765 * Handle router-id change. Delete and withdraw local routes corresponding
3766 * to this VNI from peers. Note that this is invoked prior to updating
3767 * the router-id and is done only on the global route table, the routes
3768 * are needed in the per-VNI table to re-advertise with new router id.
3770 static void withdraw_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3772 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3774 /* Skip VNIs with configured RD. */
3775 if (is_rd_configured(vpn
))
3778 delete_withdraw_vni_routes(bgp
, vpn
);
3782 * Create RT-3 for a VNI and schedule for processing and advertisement.
3783 * This is invoked upon flooding mode changing to head-end replication.
3785 static void create_advertise_type3(struct hash_bucket
*bucket
, void *data
)
3787 struct bgpevpn
*vpn
= bucket
->data
;
3788 struct bgp
*bgp
= data
;
3789 struct prefix_evpn p
;
3791 if (!vpn
|| !is_vni_live(vpn
) ||
3792 bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3793 != VXLAN_FLOOD_HEAD_END_REPL
)
3796 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3797 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
))
3798 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3799 "Type3 route creation failure for VNI %u", vpn
->vni
);
3803 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3804 * This is invoked upon flooding mode changing to drop BUM packets.
3806 static void delete_withdraw_type3(struct hash_bucket
*bucket
, void *data
)
3808 struct bgpevpn
*vpn
= bucket
->data
;
3809 struct bgp
*bgp
= data
;
3810 struct prefix_evpn p
;
3812 if (!vpn
|| !is_vni_live(vpn
))
3815 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3816 delete_evpn_route(bgp
, vpn
, &p
);
3820 * Process received EVPN type-2 route (advertise or withdraw).
3822 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3823 struct attr
*attr
, uint8_t *pfx
, int psize
,
3824 uint32_t addpath_id
)
3826 struct prefix_rd prd
;
3827 struct prefix_evpn p
= {};
3828 struct bgp_route_evpn evpn
= {};
3830 uint8_t macaddr_len
;
3831 /* holds the VNI(s) as in packet */
3832 mpls_label_t label
[BGP_MAX_LABELS
] = {};
3833 uint32_t num_labels
= 0;
3837 /* Type-2 route should be either 33, 37 or 49 bytes or an
3838 * additional 3 bytes if there is a second label (VNI):
3839 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3840 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3841 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3843 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3844 && psize
!= 40 && psize
!= 52) {
3845 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3846 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3847 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3851 struct stream
*pkt
= stream_new(psize
);
3852 stream_put(pkt
, pfx
, psize
);
3854 /* Make prefix_rd */
3855 prd
.family
= AF_UNSPEC
;
3858 STREAM_GET(&prd
.val
, pkt
, 8);
3860 /* Make EVPN prefix. */
3862 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3863 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3865 /* Copy Ethernet Seg Identifier */
3867 STREAM_GET(&attr
->esi
, pkt
, sizeof(esi_t
));
3869 if (bgp_evpn_is_esi_local_and_non_bypass(&attr
->esi
))
3870 attr
->es_flags
|= ATTR_ES_IS_LOCAL
;
3872 attr
->es_flags
&= ~ATTR_ES_IS_LOCAL
;
3874 STREAM_FORWARD_GETP(pkt
, sizeof(esi_t
));
3877 /* Copy Ethernet Tag */
3878 STREAM_GET(ð_tag
, pkt
, 4);
3879 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3881 /* Get the MAC Addr len */
3882 STREAM_GETC(pkt
, macaddr_len
);
3884 /* Get the MAC Addr */
3885 if (macaddr_len
== (ETH_ALEN
* 8)) {
3886 STREAM_GET(&p
.prefix
.macip_addr
.mac
.octet
, pkt
, ETH_ALEN
);
3889 EC_BGP_EVPN_ROUTE_INVALID
,
3890 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3891 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3897 STREAM_GETC(pkt
, ipaddr_len
);
3899 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3900 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3902 EC_BGP_EVPN_ROUTE_INVALID
,
3903 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3904 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3909 ipaddr_len
/= 8; /* Convert to bytes. */
3910 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3913 STREAM_GET(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pkt
, ipaddr_len
);
3916 /* Get the VNI(s). Stored as bytes here. */
3917 STREAM_GET(&label
[0], pkt
, BGP_LABEL_BYTES
);
3920 /* Do we have a second VNI? */
3921 if (STREAM_READABLE(pkt
)) {
3923 STREAM_GET(&label
[1], pkt
, BGP_LABEL_BYTES
);
3926 /* Process the route. */
3928 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3929 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3930 &prd
, &label
[0], num_labels
, 0, &evpn
);
3932 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3933 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3934 &prd
, &label
[0], num_labels
, &evpn
);
3939 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3940 "%u:%s - Rx EVPN Type-2 NLRI - corrupt, discarding",
3941 peer
->bgp
->vrf_id
, peer
->host
);
3949 * Process received EVPN type-3 route (advertise or withdraw).
3951 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3952 struct attr
*attr
, uint8_t *pfx
, int psize
,
3953 uint32_t addpath_id
)
3955 struct prefix_rd prd
;
3956 struct prefix_evpn p
;
3961 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3962 * IP len (1) and IP (4 or 16).
3964 if (psize
!= 17 && psize
!= 29) {
3965 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3966 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3967 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3971 /* If PMSI is present, log if it is anything other than IR.
3972 * Note: We just simply ignore the values as it is not clear if
3973 * doing anything else is better.
3976 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3977 enum pta_type pmsi_tnl_type
= bgp_attr_get_pmsi_tnl_type(attr
);
3979 if (pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
3980 && pmsi_tnl_type
!= PMSI_TNLTYPE_PIM_SM
) {
3982 EC_BGP_EVPN_PMSI_PRESENT
,
3983 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3984 peer
->bgp
->vrf_id
, peer
->host
, pmsi_tnl_type
);
3988 /* Make prefix_rd */
3989 prd
.family
= AF_UNSPEC
;
3991 memcpy(&prd
.val
, pfx
, 8);
3994 /* Make EVPN prefix. */
3995 memset(&p
, 0, sizeof(p
));
3997 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3998 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
4000 /* Copy Ethernet Tag */
4001 memcpy(ð_tag
, pfx
, 4);
4002 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
4006 ipaddr_len
= *pfx
++;
4007 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
4008 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
4009 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
4012 EC_BGP_EVPN_ROUTE_INVALID
,
4013 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
4014 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4018 /* Process the route. */
4020 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4021 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4022 &prd
, NULL
, 0, 0, NULL
);
4024 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4025 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4026 &prd
, NULL
, 0, NULL
);
4031 * Process received EVPN type-5 route (advertise or withdraw).
4033 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4034 struct attr
*attr
, uint8_t *pfx
, int psize
,
4035 uint32_t addpath_id
)
4037 struct prefix_rd prd
;
4038 struct prefix_evpn p
;
4039 struct bgp_route_evpn evpn
;
4042 mpls_label_t label
; /* holds the VNI as in the packet */
4044 bool is_valid_update
= true;
4046 /* Type-5 route should be 34 or 58 bytes:
4047 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
4048 * GW (4 or 16) and VNI (3).
4049 * Note that the IP and GW should both be IPv4 or both IPv6.
4051 if (psize
!= 34 && psize
!= 58) {
4052 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4053 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
4054 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4058 /* Make prefix_rd */
4059 prd
.family
= AF_UNSPEC
;
4061 memcpy(&prd
.val
, pfx
, 8);
4064 /* Make EVPN prefix. */
4065 memset(&p
, 0, sizeof(p
));
4067 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4068 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
4070 /* Additional information outside of prefix - ESI and GW IP */
4071 memset(&evpn
, 0, sizeof(evpn
));
4073 /* Fetch ESI overlay index */
4075 memcpy(&evpn
.eth_s_id
, pfx
, sizeof(esi_t
));
4078 /* Fetch Ethernet Tag. */
4079 memcpy(ð_tag
, pfx
, 4);
4080 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
4083 /* Fetch IP prefix length. */
4085 if (ippfx_len
> IPV6_MAX_BITLEN
) {
4087 EC_BGP_EVPN_ROUTE_INVALID
,
4088 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4089 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
4092 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
4094 /* Determine IPv4 or IPv6 prefix */
4095 /* Since the address and GW are from the same family, this just becomes
4096 * a simple check on the total size.
4099 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
4100 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
4102 SET_IPADDR_V4(&evpn
.gw_ip
);
4103 memcpy(&evpn
.gw_ip
.ipaddr_v4
, pfx
, 4);
4106 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
4107 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
,
4109 pfx
+= IPV6_MAX_BYTELEN
;
4110 SET_IPADDR_V6(&evpn
.gw_ip
);
4111 memcpy(&evpn
.gw_ip
.ipaddr_v6
, pfx
, IPV6_MAX_BYTELEN
);
4112 pfx
+= IPV6_MAX_BYTELEN
;
4115 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4116 memset(&label
, 0, sizeof(label
));
4117 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
4120 * If in future, we are required to access additional fields,
4121 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4126 * An update containing a non-zero gateway IP and a non-zero ESI
4127 * at the same time is should be treated as withdraw
4129 if (bgp_evpn_is_esi_valid(&evpn
.eth_s_id
) &&
4130 !ipaddr_is_zero(&evpn
.gw_ip
)) {
4131 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4132 "%s - Rx EVPN Type-5 ESI and gateway-IP both non-zero.",
4134 is_valid_update
= false;
4135 } else if (bgp_evpn_is_esi_valid(&evpn
.eth_s_id
))
4136 evpn
.type
= OVERLAY_INDEX_ESI
;
4137 else if (!ipaddr_is_zero(&evpn
.gw_ip
))
4138 evpn
.type
= OVERLAY_INDEX_GATEWAY_IP
;
4140 if (is_zero_mac(&attr
->rmac
) &&
4141 !bgp_evpn_is_esi_valid(&evpn
.eth_s_id
) &&
4142 ipaddr_is_zero(&evpn
.gw_ip
) && label
== 0) {
4143 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4144 "%s - Rx EVPN Type-5 ESI, gateway-IP, RMAC and label all zero",
4146 is_valid_update
= false;
4149 if (is_mcast_mac(&attr
->rmac
) || is_bcast_mac(&attr
->rmac
))
4150 is_valid_update
= false;
4153 /* Process the route. */
4154 if (attr
&& is_valid_update
)
4155 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4156 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4157 &prd
, &label
, 1, 0, &evpn
);
4159 if (!is_valid_update
) {
4160 char attr_str
[BUFSIZ
] = {0};
4162 bgp_dump_attr(attr
, attr_str
, BUFSIZ
);
4164 "Invalid update from peer %s vrf %u prefix %pFX attr %s - treat as withdraw",
4165 peer
->hostname
, peer
->bgp
->vrf_id
, &p
,
4168 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4169 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4170 &prd
, &label
, 1, &evpn
);
4176 static void evpn_mpattr_encode_type5(struct stream
*s
, const struct prefix
*p
,
4177 const struct prefix_rd
*prd
,
4178 mpls_label_t
*label
, uint32_t num_labels
,
4183 const struct evpn_addr
*p_evpn_p
;
4185 memset(&temp
, 0, sizeof(temp
));
4186 if (p
->family
!= AF_EVPN
)
4188 p_evpn_p
= &(p
->u
.prefix_evpn
);
4190 /* len denites the total len of IP and GW-IP in the route
4191 IP and GW-IP have to be both ipv4 or ipv6
4193 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4194 len
= 8; /* IP and GWIP are both ipv4 */
4196 len
= 32; /* IP and GWIP are both ipv6 */
4197 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4198 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4199 stream_put(s
, prd
->val
, 8);
4200 if (attr
&& attr
->evpn_overlay
.type
== OVERLAY_INDEX_ESI
)
4201 stream_put(s
, &attr
->esi
, sizeof(esi_t
));
4203 stream_put(s
, 0, sizeof(esi_t
));
4204 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4205 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4206 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4207 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4209 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4210 if (attr
&& attr
->evpn_overlay
.type
== OVERLAY_INDEX_GATEWAY_IP
) {
4211 const struct bgp_route_evpn
*evpn_overlay
=
4212 bgp_attr_get_evpn_overlay(attr
);
4214 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4216 evpn_overlay
->gw_ip
.ipaddr_v4
.s_addr
);
4218 stream_put(s
, &(evpn_overlay
->gw_ip
.ipaddr_v6
), 16);
4220 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4221 stream_put_ipv4(s
, 0);
4223 stream_put(s
, &temp
, 16);
4227 stream_put(s
, label
, 3);
4233 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4235 static void cleanup_vni_on_disable(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4237 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4239 /* Remove EVPN routes and schedule for processing. */
4240 delete_routes_for_vni(bgp
, vpn
);
4242 /* Clear "live" flag and see if hash needs to be freed. */
4243 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4244 if (!is_vni_configured(vpn
))
4245 bgp_evpn_free(bgp
, vpn
);
4249 * Free a VNI entry; iterator function called during cleanup.
4251 static void free_vni_entry(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4253 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4255 delete_all_vni_routes(bgp
, vpn
);
4256 bgp_evpn_free(bgp
, vpn
);
4260 * Derive AUTO import RT for BGP VRF - L3VNI
4262 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4264 struct bgp
*bgp_evpn
= NULL
;
4266 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4267 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4270 bgp_evpn
= bgp_get_evpn();
4273 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4277 * Delete AUTO import RT from BGP VRF - L3VNI
4279 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4281 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4285 * Derive AUTO export RT for BGP VRF - L3VNI
4287 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4289 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4290 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4294 * Delete AUTO export RT from BGP VRF - L3VNI
4296 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4298 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4301 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4303 struct bgp
*bgp_evpn
= NULL
;
4304 struct listnode
*node
= NULL
;
4305 struct bgpevpn
*vpn
= NULL
;
4307 bgp_evpn
= bgp_get_evpn();
4311 /* update all type-5 routes */
4312 update_advertise_vrf_routes(bgp_vrf
);
4314 /* update all type-2 routes */
4315 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4316 update_routes_for_vni(bgp_evpn
, vpn
);
4320 * Handle autort change for a given VNI.
4322 static void update_autort_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4324 struct bgpevpn
*vpn
= bucket
->data
;
4326 if (!is_import_rt_configured(vpn
)) {
4327 if (is_vni_live(vpn
))
4328 bgp_evpn_uninstall_routes(bgp
, vpn
);
4329 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4330 list_delete_all_node(vpn
->import_rtl
);
4331 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4332 if (is_vni_live(vpn
))
4333 bgp_evpn_install_routes(bgp
, vpn
);
4335 if (!is_export_rt_configured(vpn
)) {
4336 list_delete_all_node(vpn
->export_rtl
);
4337 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4338 if (is_vni_live(vpn
))
4339 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4344 * Handle autort change for L3VNI.
4346 static void update_autort_l3vni(struct bgp
*bgp
)
4348 if ((CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
4349 && (CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)))
4352 if (!CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
)) {
4353 if (is_l3vni_live(bgp
))
4354 uninstall_routes_for_vrf(bgp
);
4356 /* Cleanup the RT to VRF mapping */
4357 bgp_evpn_unmap_vrf_from_its_rts(bgp
);
4359 /* Remove auto generated RT */
4360 evpn_auto_rt_import_delete_for_vrf(bgp
);
4362 list_delete_all_node(bgp
->vrf_import_rtl
);
4364 /* Map auto derive or configured RTs */
4365 evpn_auto_rt_import_add_for_vrf(bgp
);
4368 if (!CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
4369 list_delete_all_node(bgp
->vrf_export_rtl
);
4371 evpn_auto_rt_export_delete_for_vrf(bgp
);
4373 evpn_auto_rt_export_add_for_vrf(bgp
);
4375 if (is_l3vni_live(bgp
))
4376 bgp_evpn_map_vrf_to_its_rts(bgp
);
4379 if (!is_l3vni_live(bgp
))
4382 /* advertise type-5 routes if needed */
4383 update_advertise_vrf_routes(bgp
);
4385 /* install all remote routes belonging to this l3vni
4386 * into corresponding vrf
4388 install_routes_for_vrf(bgp
);
4395 /* withdraw type-5 route corresponding to ip prefix */
4396 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4397 afi_t afi
, safi_t safi
)
4400 struct prefix_evpn evp
;
4402 build_type5_prefix_from_ip_prefix(&evp
, p
);
4403 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4406 EC_BGP_EVPN_ROUTE_DELETE
,
4407 "%u failed to delete type-5 route for prefix %pFX in vrf %s",
4408 bgp_vrf
->vrf_id
, p
, vrf_id_to_name(bgp_vrf
->vrf_id
));
4411 /* withdraw all type-5 routes for an address family */
4412 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4414 struct bgp_table
*table
= NULL
;
4415 struct bgp_dest
*dest
= NULL
;
4416 struct bgp_path_info
*pi
;
4418 table
= bgp_vrf
->rib
[afi
][safi
];
4419 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
4420 /* Only care about "selected" routes. Also ensure that
4421 * these are routes that are injectable into EVPN.
4423 /* TODO: Support for AddPath for EVPN. */
4424 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
4425 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4426 && is_route_injectable_into_evpn(pi
)) {
4427 bgp_evpn_withdraw_type5_route(
4428 bgp_vrf
, bgp_dest_get_prefix(dest
), afi
,
4437 * evpn - enable advertisement of default g/w
4439 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
4440 safi_t safi
, bool add
)
4442 struct prefix ip_prefix
;
4444 /* form the default prefix 0.0.0.0/0 */
4445 memset(&ip_prefix
, 0, sizeof(ip_prefix
));
4446 ip_prefix
.family
= afi2family(afi
);
4449 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
4452 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
4459 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4460 * to this function correspond to those of the source IP prefix (best
4461 * path in the case of the attr. In the case of a local prefix (when we
4462 * are advertising local subnets), the src_attr will be NULL.
4464 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4465 struct attr
*src_attr
, afi_t afi
,
4469 struct prefix_evpn evp
;
4471 build_type5_prefix_from_ip_prefix(&evp
, p
);
4472 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
, afi
, safi
);
4474 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4475 "%u: Failed to create type-5 route for prefix %pFX",
4476 bgp_vrf
->vrf_id
, p
);
4479 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4480 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4481 * advertisement is enabled.
4483 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4486 struct bgp_table
*table
= NULL
;
4487 struct bgp_dest
*dest
= NULL
;
4488 struct bgp_path_info
*pi
;
4490 table
= bgp_vrf
->rib
[afi
][safi
];
4491 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
4492 /* Need to identify the "selected" route entry to use its
4493 * attribute. Also, ensure that the route is injectable
4495 * TODO: Support for AddPath for EVPN.
4497 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
4498 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4499 && is_route_injectable_into_evpn(pi
)) {
4501 /* apply the route-map */
4502 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4503 route_map_result_t ret
;
4504 struct bgp_path_info tmp_pi
;
4505 struct bgp_path_info_extra tmp_pie
;
4506 struct attr tmp_attr
;
4508 tmp_attr
= *pi
->attr
;
4510 /* Fill temp path_info */
4511 prep_for_rmap_apply(&tmp_pi
, &tmp_pie
,
4515 RESET_FLAG(tmp_attr
.rmap_change_flags
);
4517 ret
= route_map_apply(
4518 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4520 bgp_dest_get_prefix(dest
),
4522 if (ret
== RMAP_DENYMATCH
) {
4523 bgp_attr_flush(&tmp_attr
);
4526 bgp_evpn_advertise_type5_route(
4528 bgp_dest_get_prefix(dest
),
4529 &tmp_attr
, afi
, safi
);
4531 bgp_evpn_advertise_type5_route(
4533 bgp_dest_get_prefix(dest
),
4534 pi
->attr
, afi
, safi
);
4541 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4543 struct listnode
*node
, *nnode
, *node_to_del
;
4544 struct ecommunity
*ecom
, *ecom_auto
;
4545 struct ecommunity_val eval
;
4547 if (bgp
->advertise_autort_rfc8365
)
4548 vni
|= EVPN_AUTORT_VXLAN
;
4549 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4551 ecom_auto
= ecommunity_new();
4552 ecommunity_add_val(ecom_auto
, &eval
, false, false);
4555 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4556 if (ecommunity_match(ecom
, ecom_auto
)) {
4557 ecommunity_free(&ecom
);
4564 list_delete_node(rtl
, node_to_del
);
4566 ecommunity_free(&ecom_auto
);
4569 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4570 struct ecommunity
*ecomadd
)
4572 /* uninstall routes from vrf */
4573 if (is_l3vni_live(bgp_vrf
))
4574 uninstall_routes_for_vrf(bgp_vrf
);
4576 /* Cleanup the RT to VRF mapping */
4577 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4579 /* Remove auto generated RT */
4580 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4582 /* Add the newly configured RT to RT list */
4583 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4584 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4586 /* map VRF to its RTs and install routes matching the new RTs */
4587 if (is_l3vni_live(bgp_vrf
)) {
4588 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4589 install_routes_for_vrf(bgp_vrf
);
4593 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4594 struct ecommunity
*ecomdel
)
4596 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4597 struct ecommunity
*ecom
= NULL
;
4599 /* uninstall routes from vrf */
4600 if (is_l3vni_live(bgp_vrf
))
4601 uninstall_routes_for_vrf(bgp_vrf
);
4603 /* Cleanup the RT to VRF mapping */
4604 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4606 /* remove the RT from the RT list */
4607 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4608 if (ecommunity_match(ecom
, ecomdel
)) {
4609 ecommunity_free(&ecom
);
4616 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4618 assert(bgp_vrf
->vrf_import_rtl
);
4619 /* fallback to auto import rt, if this was the last RT */
4620 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4621 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4622 if (is_l3vni_live(bgp_vrf
))
4623 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4626 /* map VRFs to its RTs and install routes matching this new RT */
4627 if (is_l3vni_live(bgp_vrf
)) {
4628 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4629 install_routes_for_vrf(bgp_vrf
);
4633 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4634 struct ecommunity
*ecomadd
)
4636 /* remove auto-generated RT */
4637 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4639 /* Add the new RT to the RT list */
4640 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4641 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4643 if (is_l3vni_live(bgp_vrf
))
4644 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4647 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4648 struct ecommunity
*ecomdel
)
4650 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4651 struct ecommunity
*ecom
= NULL
;
4653 /* Remove the RT from the RT list */
4654 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4655 if (ecommunity_match(ecom
, ecomdel
)) {
4656 ecommunity_free(&ecom
);
4663 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4666 * Temporary assert to make SA happy.
4667 * The ALL_LIST_ELEMENTS macro above has a NULL check
4668 * which means that SA is going to complain about
4669 * the list_isempty call, which doesn't NULL check.
4670 * So until we get this situation cleaned up, here
4673 assert(bgp_vrf
->vrf_export_rtl
);
4675 /* fall back to auto-generated RT if this was the last RT */
4676 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4677 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4678 if (is_l3vni_live(bgp_vrf
))
4679 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4682 if (is_l3vni_live(bgp_vrf
))
4683 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4687 * Handle change to BGP router id. This is invoked twice by the change
4688 * handler, first before the router id has been changed and then after
4689 * the router id has been changed. The first invocation will result in
4690 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4691 * will result in the routes being re-advertised.
4693 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4695 struct listnode
*node
;
4696 struct bgp
*bgp_vrf
;
4700 /* delete and withdraw all the type-5 routes
4701 stored in the global table for this vrf
4703 withdraw_router_id_vrf(bgp
);
4705 /* delete all the VNI routes (type-2/type-3) routes for all the
4708 hash_iterate(bgp
->vnihash
,
4709 (void (*)(struct hash_bucket
*,
4710 void *))withdraw_router_id_vni
,
4713 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4714 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4715 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4716 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4718 bgp_vrf
->evpn_info
->pip_ip
.s_addr
4724 /* Assign new default instance router-id */
4725 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4726 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4727 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4728 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4730 bgp_vrf
->evpn_info
->pip_ip
=
4732 /* advertise type-5 routes with
4735 update_advertise_vrf_routes(bgp_vrf
);
4740 /* advertise all routes in the vrf as type-5 routes with the new
4743 update_router_id_vrf(bgp
);
4745 /* advertise all the VNI routes (type-2/type-3) routes with the
4748 hash_iterate(bgp
->vnihash
,
4749 (void (*)(struct hash_bucket
*,
4750 void *))update_router_id_vni
,
4756 * Handle change to auto-RT algorithm - update and advertise local routes.
4758 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4760 hash_iterate(bgp
->vnihash
,
4761 (void (*)(struct hash_bucket
*,
4762 void*))update_autort_vni
,
4765 update_autort_l3vni(bgp
);
4769 * Handle change to export RT - update and advertise local routes.
4771 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4773 return update_routes_for_vni(bgp
, vpn
);
4776 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4779 delete_withdraw_vrf_routes(bgp_vrf
);
4781 update_advertise_vrf_routes(bgp_vrf
);
4785 * Handle change to RD. This is invoked twice by the change handler,
4786 * first before the RD has been changed and then after the RD has
4787 * been changed. The first invocation will result in local routes
4788 * of this VNI being deleted and withdrawn and the next will result
4789 * in the routes being re-advertised.
4791 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4795 delete_withdraw_vni_routes(bgp
, vpn
);
4797 update_advertise_vni_routes(bgp
, vpn
);
4801 * Install routes for this VNI. Invoked upon change to Import RT.
4803 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4805 return install_routes_for_vni(bgp
, vpn
);
4809 * Uninstall all routes installed for this VNI. Invoked upon change
4812 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4814 return uninstall_routes_for_vni(bgp
, vpn
);
4818 * TODO: Hardcoded for a maximum of 2 VNIs right now
4820 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4825 vni1
= label2vni(label
);
4826 if (num_labels
== 2) {
4827 vni2
= label2vni(label
+ 1);
4828 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4830 snprintf(buf
, len
, "%u", vni1
);
4835 * Function to convert evpn route to json format.
4836 * NOTE: We don't use prefix2str as the output here is a bit different.
4838 void bgp_evpn_route2json(const struct prefix_evpn
*p
, json_object
*json
)
4840 char buf1
[ETHER_ADDR_STRLEN
];
4841 char buf2
[PREFIX2STR_BUFFER
];
4848 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4850 switch (p
->prefix
.route_type
) {
4851 case BGP_EVPN_MAC_IP_ROUTE
:
4852 json_object_int_add(json
, "ethTag",
4853 p
->prefix
.macip_addr
.eth_tag
);
4854 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4855 json_object_string_add(json
, "mac",
4856 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4859 if (!is_evpn_prefix_ipaddr_none(p
)) {
4860 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
:
4862 prefixlen
= (family
== AF_INET
) ?
4863 IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
;
4864 inet_ntop(family
, &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4865 buf2
, PREFIX2STR_BUFFER
);
4866 json_object_int_add(json
, "ipLen", prefixlen
);
4867 json_object_string_add(json
, "ip", buf2
);
4871 case BGP_EVPN_IMET_ROUTE
:
4872 json_object_int_add(json
, "ethTag",
4873 p
->prefix
.imet_addr
.eth_tag
);
4874 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4875 prefixlen
= (family
== AF_INET
) ? IPV4_MAX_BITLEN
:
4877 inet_ntop(family
, &p
->prefix
.imet_addr
.ip
.ip
.addr
, buf2
,
4879 json_object_int_add(json
, "ipLen", prefixlen
);
4880 json_object_string_add(json
, "ip", buf2
);
4883 case BGP_EVPN_IP_PREFIX_ROUTE
:
4884 json_object_int_add(json
, "ethTag",
4885 p
->prefix
.prefix_addr
.eth_tag
);
4886 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4887 inet_ntop(family
, &p
->prefix
.prefix_addr
.ip
.ip
.addr
,
4888 buf2
, sizeof(buf2
));
4889 json_object_int_add(json
, "ipLen",
4890 p
->prefix
.prefix_addr
.ip_prefix_length
);
4891 json_object_string_add(json
, "ip", buf2
);
4900 * Encode EVPN prefix in Update (MP_REACH)
4902 void bgp_evpn_encode_prefix(struct stream
*s
, const struct prefix
*p
,
4903 const struct prefix_rd
*prd
, mpls_label_t
*label
,
4904 uint32_t num_labels
, struct attr
*attr
,
4905 bool addpath_capable
, uint32_t addpath_tx_id
)
4907 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4908 int len
, ipa_len
= 0;
4910 if (addpath_capable
)
4911 stream_putl(s
, addpath_tx_id
);
4914 stream_putc(s
, evp
->prefix
.route_type
);
4916 switch (evp
->prefix
.route_type
) {
4917 case BGP_EVPN_MAC_IP_ROUTE
:
4918 if (is_evpn_prefix_ipaddr_v4(evp
))
4919 ipa_len
= IPV4_MAX_BYTELEN
;
4920 else if (is_evpn_prefix_ipaddr_v6(evp
))
4921 ipa_len
= IPV6_MAX_BYTELEN
;
4922 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4923 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4924 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4926 stream_putc(s
, len
);
4927 stream_put(s
, prd
->val
, 8); /* RD */
4929 stream_put(s
, &attr
->esi
, ESI_BYTES
);
4931 stream_put(s
, 0, 10);
4932 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4933 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4934 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4935 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4936 if (ipa_len
) /* IP */
4937 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4939 /* 1st label is the L2 VNI */
4940 stream_put(s
, label
, BGP_LABEL_BYTES
);
4941 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4942 if (ipa_len
&& num_labels
> 1)
4943 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4946 case BGP_EVPN_IMET_ROUTE
:
4947 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4948 stream_put(s
, prd
->val
, 8); /* RD */
4949 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4950 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4951 /* Originating Router's IP Addr */
4952 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4955 case BGP_EVPN_ES_ROUTE
:
4956 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4957 stream_put(s
, prd
->val
, 8); /* RD */
4958 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4959 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4961 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4964 case BGP_EVPN_AD_ROUTE
:
4965 /* RD, ESI, EthTag, 1 VNI */
4966 len
= RD_BYTES
+ ESI_BYTES
+ EVPN_ETH_TAG_BYTES
+ BGP_LABEL_BYTES
;
4967 stream_putc(s
, len
);
4968 stream_put(s
, prd
->val
, RD_BYTES
); /* RD */
4969 stream_put(s
, evp
->prefix
.ead_addr
.esi
.val
, ESI_BYTES
); /* ESI */
4970 stream_putl(s
, evp
->prefix
.ead_addr
.eth_tag
); /* Ethernet Tag */
4971 stream_put(s
, label
, BGP_LABEL_BYTES
);
4974 case BGP_EVPN_IP_PREFIX_ROUTE
:
4975 /* TODO: AddPath support. */
4976 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4984 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4985 struct bgp_nlri
*packet
, int withdraw
)
4991 uint32_t addpath_id
;
4992 bool addpath_capable
;
4997 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4999 lim
= pnt
+ packet
->length
;
5001 safi
= packet
->safi
;
5004 addpath_capable
= bgp_addpath_encode_rx(peer
, afi
, safi
);
5006 for (; pnt
< lim
; pnt
+= psize
) {
5007 /* Clear prefix structure. */
5008 memset(&p
, 0, sizeof(p
));
5010 /* Deal with path-id if AddPath is supported. */
5011 if (addpath_capable
) {
5012 /* When packet overflow occurs return immediately. */
5013 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
5014 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5016 memcpy(&addpath_id
, pnt
, BGP_ADDPATH_ID_LEN
);
5017 addpath_id
= ntohl(addpath_id
);
5018 pnt
+= BGP_ADDPATH_ID_LEN
;
5021 /* All EVPN NLRI types start with type and length. */
5023 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE
;
5028 /* When packet overflow occur return immediately. */
5029 if (pnt
+ psize
> lim
)
5030 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5033 case BGP_EVPN_MAC_IP_ROUTE
:
5034 if (process_type2_route(peer
, afi
, safi
,
5035 withdraw
? NULL
: attr
, pnt
,
5036 psize
, addpath_id
)) {
5039 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
5040 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5041 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE
;
5045 case BGP_EVPN_IMET_ROUTE
:
5046 if (process_type3_route(peer
, afi
, safi
,
5047 withdraw
? NULL
: attr
, pnt
,
5048 psize
, addpath_id
)) {
5051 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
5052 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5053 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE
;
5057 case BGP_EVPN_ES_ROUTE
:
5058 if (bgp_evpn_type4_route_process(peer
, afi
, safi
,
5059 withdraw
? NULL
: attr
, pnt
,
5060 psize
, addpath_id
)) {
5063 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
5064 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5065 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE
;
5069 case BGP_EVPN_AD_ROUTE
:
5070 if (bgp_evpn_type1_route_process(peer
, afi
, safi
,
5071 withdraw
? NULL
: attr
, pnt
,
5072 psize
, addpath_id
)) {
5075 "%u:%s - Error in processing EVPN type-1 NLRI size %d",
5076 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5077 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE1_SIZE
;
5081 case BGP_EVPN_IP_PREFIX_ROUTE
:
5082 if (process_type5_route(peer
, afi
, safi
,
5083 withdraw
? NULL
: attr
, pnt
,
5084 psize
, addpath_id
)) {
5087 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
5088 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5089 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE
;
5098 /* Packet length consistency check. */
5100 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH
;
5102 return BGP_NLRI_PARSE_OK
;
5106 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
5107 * The mapping will be used during route processing.
5108 * bgp_vrf: specific bgp vrf instance on which RT is configured
5110 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
5113 struct ecommunity_val
*eval
= NULL
;
5114 struct listnode
*node
= NULL
, *nnode
= NULL
;
5115 struct ecommunity
*ecom
= NULL
;
5117 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5118 for (i
= 0; i
< ecom
->size
; i
++) {
5119 eval
= (struct ecommunity_val
*)(ecom
->val
5121 * ECOMMUNITY_SIZE
));
5122 map_vrf_to_rt(bgp_vrf
, eval
);
5128 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
5130 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
5133 struct ecommunity_val
*eval
;
5134 struct listnode
*node
, *nnode
;
5135 struct ecommunity
*ecom
;
5137 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5138 for (i
= 0; i
< ecom
->size
; i
++) {
5139 struct vrf_irt_node
*irt
;
5140 struct ecommunity_val eval_tmp
;
5142 eval
= (struct ecommunity_val
*)(ecom
->val
5144 * ECOMMUNITY_SIZE
));
5145 /* If using "automatic" RT, we only care about the
5146 * local-admin sub-field.
5147 * This is to facilitate using VNI as the RT for EBGP
5150 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5151 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
5152 BGP_VRF_IMPORT_RT_CFGD
))
5153 mask_ecom_global_admin(&eval_tmp
, eval
);
5155 irt
= lookup_vrf_import_rt(&eval_tmp
);
5157 unmap_vrf_from_rt(bgp_vrf
, irt
);
5164 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
5165 * The mapping will be used during route processing.
5167 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5170 struct ecommunity_val
*eval
;
5171 struct listnode
*node
, *nnode
;
5172 struct ecommunity
*ecom
;
5174 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5175 for (i
= 0; i
< ecom
->size
; i
++) {
5176 eval
= (struct ecommunity_val
*)(ecom
->val
5178 * ECOMMUNITY_SIZE
));
5179 map_vni_to_rt(bgp
, vpn
, eval
);
5185 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
5187 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5190 struct ecommunity_val
*eval
;
5191 struct listnode
*node
, *nnode
;
5192 struct ecommunity
*ecom
;
5194 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5195 for (i
= 0; i
< ecom
->size
; i
++) {
5196 struct irt_node
*irt
;
5197 struct ecommunity_val eval_tmp
;
5199 eval
= (struct ecommunity_val
*)(ecom
->val
5201 * ECOMMUNITY_SIZE
));
5202 /* If using "automatic" RT, we only care about the
5203 * local-admin sub-field.
5204 * This is to facilitate using VNI as the RT for EBGP
5207 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5208 if (!is_import_rt_configured(vpn
))
5209 mask_ecom_global_admin(&eval_tmp
, eval
);
5211 irt
= lookup_import_rt(bgp
, &eval_tmp
);
5213 unmap_vni_from_rt(bgp
, vpn
, irt
);
5219 * Derive Import RT automatically for VNI and map VNI to RT.
5220 * The mapping will be used during route processing.
5222 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5224 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
5225 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
5228 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
5232 * Derive Export RT automatically for VNI.
5234 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5236 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
5237 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5241 * Derive RD automatically for VNI using passed information - it
5242 * is of the form RouterId:unique-id-for-vni.
5244 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5246 if (is_vrf_rd_configured(bgp
))
5249 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5253 * Derive RD automatically for VNI using passed information - it
5254 * is of the form RouterId:unique-id-for-vni.
5256 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5258 char buf
[BGP_EVPN_PREFIX_RD_LEN
];
5260 vpn
->prd
.family
= AF_UNSPEC
;
5261 vpn
->prd
.prefixlen
= 64;
5262 snprintfrr(buf
, sizeof(buf
), "%pI4:%hu", &bgp
->router_id
, vpn
->rd_id
);
5263 (void)str2prefix_rd(buf
, &vpn
->prd
);
5264 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5270 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5272 struct list
*inst
= bm
->bgp
;
5273 struct listnode
*node
;
5274 struct bgp
*bgp_vrf
;
5276 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5277 if (bgp_vrf
->l3vni
== vni
)
5287 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5289 struct bgpevpn
*vpn
;
5292 memset(&tmp
, 0, sizeof(tmp
));
5294 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5299 * Create a new vpn - invoked upon configuration or zebra notification.
5301 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5302 struct in_addr originator_ip
,
5303 vrf_id_t tenant_vrf_id
,
5304 struct in_addr mcast_grp
,
5305 ifindex_t svi_ifindex
)
5307 struct bgpevpn
*vpn
;
5309 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5311 /* Set values - RD and RT set to defaults. */
5313 vpn
->originator_ip
= originator_ip
;
5314 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5315 vpn
->mcast_grp
= mcast_grp
;
5316 vpn
->svi_ifindex
= svi_ifindex
;
5318 /* Initialize route-target import and export lists */
5319 vpn
->import_rtl
= list_new();
5320 vpn
->import_rtl
->cmp
=
5321 (int (*)(void *, void *))bgp_evpn_route_target_cmp
;
5322 vpn
->import_rtl
->del
= bgp_evpn_xxport_delete_ecomm
;
5323 vpn
->export_rtl
= list_new();
5324 vpn
->export_rtl
->cmp
=
5325 (int (*)(void *, void *))bgp_evpn_route_target_cmp
;
5326 vpn
->export_rtl
->del
= bgp_evpn_xxport_delete_ecomm
;
5327 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5328 derive_rd_rt_for_vni(bgp
, vpn
);
5330 /* Initialize EVPN route table. */
5331 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5334 (void)hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
);
5336 bgp_evpn_remote_ip_hash_init(vpn
);
5337 bgp_evpn_link_to_vni_svi_hash(bgp
, vpn
);
5339 /* add to l2vni list on corresponding vrf */
5340 bgpevpn_link_to_l3vni(vpn
);
5342 bgp_evpn_vni_es_init(vpn
);
5344 QOBJ_REG(vpn
, bgpevpn
);
5349 * Free a given VPN - called in multiple scenarios such as zebra
5350 * notification, configuration being deleted, advertise-all-vni disabled etc.
5351 * This just frees appropriate memory, caller should have taken other
5354 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5356 bgp_evpn_remote_ip_hash_destroy(vpn
);
5357 bgp_evpn_vni_es_cleanup(vpn
);
5358 bgpevpn_unlink_from_l3vni(vpn
);
5359 bgp_table_unlock(vpn
->route_table
);
5360 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5361 list_delete(&vpn
->import_rtl
);
5362 list_delete(&vpn
->export_rtl
);
5363 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5364 hash_release(bgp
->vni_svi_hash
, vpn
);
5365 hash_release(bgp
->vnihash
, vpn
);
5367 XFREE(MTYPE_BGP_EVPN
, vpn
);
5370 static void hash_evpn_free(struct bgpevpn
*vpn
)
5372 XFREE(MTYPE_BGP_EVPN
, vpn
);
5376 * Import evpn route from global table to VNI/VRF/ESI.
5378 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5379 const struct prefix
*p
, struct bgp_path_info
*pi
)
5381 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5385 * Unimport evpn route from VNI/VRF/ESI.
5387 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5388 const struct prefix
*p
, struct bgp_path_info
*pi
)
5390 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5393 /* filter routes which have martian next hops */
5394 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5398 struct bgp_dest
*rd_dest
, *dest
;
5399 struct bgp_table
*table
;
5400 struct bgp_path_info
*pi
;
5405 /* Walk entire global routing table and evaluate routes which could be
5406 * imported into this VPN. Note that we cannot just look at the routes
5407 * for the VNI's RD -
5408 * remote routes applicable for this VNI could have any RD.
5410 /* EVPN routes are a 2-level table. */
5411 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
5412 rd_dest
= bgp_route_next(rd_dest
)) {
5413 table
= bgp_dest_get_bgp_table_info(rd_dest
);
5417 for (dest
= bgp_table_top(table
); dest
;
5418 dest
= bgp_route_next(dest
)) {
5420 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
5423 /* Consider "valid" remote routes applicable for
5425 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5426 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5428 if (bgp_nexthop_self(bgp
, afi
, pi
->type
,
5429 pi
->sub_type
, pi
->attr
,
5431 const struct prefix
*p
=
5432 bgp_dest_get_prefix(dest
);
5434 if (bgp_debug_update(pi
->peer
, p
, NULL
,
5436 char attr_str
[BUFSIZ
] = {0};
5438 bgp_dump_attr(pi
->attr
,
5443 "%u: prefix %pBD with attr %s - DENIED due to martian or self nexthop",
5447 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5450 bgp_rib_remove(dest
, pi
, pi
->peer
, afi
,
5461 * Handle del of a local MACIP.
5463 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5464 struct ipaddr
*ip
, int state
)
5466 struct bgpevpn
*vpn
;
5467 struct prefix_evpn p
;
5468 struct bgp_dest
*dest
;
5470 /* Lookup VNI hash - should exist. */
5471 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5472 if (!vpn
|| !is_vni_live(vpn
)) {
5473 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5474 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5475 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5479 build_evpn_type2_prefix(&p
, mac
, ip
);
5480 if (state
== ZEBRA_NEIGH_ACTIVE
) {
5481 /* Remove EVPN type-2 route and schedule for processing. */
5482 delete_evpn_route(bgp
, vpn
, &p
);
5484 /* Re-instate the current remote best path if any */
5485 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
5487 evpn_zebra_reinstall_best_route(bgp
, vpn
, dest
);
5488 bgp_dest_unlock_node(dest
);
5496 * Handle add of a local MACIP.
5498 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5499 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
, esi_t
*esi
)
5501 struct bgpevpn
*vpn
;
5502 struct prefix_evpn p
;
5504 /* Lookup VNI hash - should exist. */
5505 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5506 if (!vpn
|| !is_vni_live(vpn
)) {
5507 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5508 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5509 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5513 /* Create EVPN type-2 route and schedule for processing. */
5514 build_evpn_type2_prefix(&p
, mac
, ip
);
5515 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
, esi
)) {
5517 EC_BGP_EVPN_ROUTE_CREATE
,
5518 "%u:Failed to create Type-2 route, VNI %u %s MAC %pEA IP %pIA (flags: 0x%x)",
5519 bgp
->vrf_id
, vpn
->vni
,
5520 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5530 static void link_l2vni_hash_to_l3vni(struct hash_bucket
*bucket
,
5531 struct bgp
*bgp_vrf
)
5533 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
5534 struct bgp
*bgp_evpn
= NULL
;
5536 bgp_evpn
= bgp_get_evpn();
5539 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5540 bgpevpn_link_to_l3vni(vpn
);
5543 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
,
5544 struct ethaddr
*svi_rmac
,
5545 struct ethaddr
*vrr_rmac
,
5546 struct in_addr originator_ip
, int filter
,
5547 ifindex_t svi_ifindex
,
5548 bool is_anycast_mac
)
5550 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5551 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5552 struct listnode
*node
= NULL
;
5553 struct bgpevpn
*vpn
= NULL
;
5556 /* get the EVPN instance - required to get the AS number for VRF
5559 bgp_evpn
= bgp_get_evpn();
5563 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
5569 /* if the BGP vrf instance doesn't exist - create one */
5570 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5575 ret
= bgp_get_vty(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5576 vrf_id
== VRF_DEFAULT
5577 ? BGP_INSTANCE_TYPE_DEFAULT
5578 : BGP_INSTANCE_TYPE_VRF
);
5580 case BGP_ERR_AS_MISMATCH
:
5581 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5582 "BGP instance is already running; AS is %u",
5585 case BGP_ERR_INSTANCE_MISMATCH
:
5586 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5587 "BGP instance type mismatch");
5591 /* mark as auto created */
5592 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5595 /* associate the vrf with l3vni and related parameters */
5596 bgp_vrf
->l3vni
= l3vni
;
5597 bgp_vrf
->originator_ip
= originator_ip
;
5598 bgp_vrf
->l3vni_svi_ifindex
= svi_ifindex
;
5599 bgp_vrf
->evpn_info
->is_anycast_mac
= is_anycast_mac
;
5601 /* copy anycast MAC from VRR MAC */
5602 memcpy(&bgp_vrf
->rmac
, vrr_rmac
, ETH_ALEN
);
5603 /* copy sys RMAC from SVI MAC */
5604 memcpy(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, svi_rmac
, ETH_ALEN
);
5605 /* PIP user configured mac is not present use svi mac as sys mac */
5606 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
))
5607 memcpy(&bgp_vrf
->evpn_info
->pip_rmac
, svi_rmac
, ETH_ALEN
);
5609 if (bgp_debug_zebra(NULL
))
5611 "VRF %s vni %u pip %s RMAC %pEA sys RMAC %pEA static RMAC %pEA is_anycast_mac %s",
5612 vrf_id_to_name(bgp_vrf
->vrf_id
), bgp_vrf
->l3vni
,
5613 bgp_vrf
->evpn_info
->advertise_pip
? "enable"
5615 &bgp_vrf
->rmac
, &bgp_vrf
->evpn_info
->pip_rmac
,
5616 &bgp_vrf
->evpn_info
->pip_rmac_static
,
5617 is_anycast_mac
? "Enable" : "Disable");
5619 /* set the right filter - are we using l3vni only for prefix routes? */
5621 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5624 * VNI_FLAG_USE_TWO_LABELS flag for linked L2VNIs should not be
5625 * set before linking vrf to L3VNI. Thus, no need to clear
5629 UNSET_FLAG(bgp_vrf
->vrf_flags
,
5630 BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5632 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5633 if (!CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
5636 * If we are flapping VNI_FLAG_USE_TWO_LABELS
5637 * flag, update all MACIP routes in this VNI
5639 SET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5640 update_all_type2_routes(bgp_evpn
, vpn
);
5645 /* Map auto derive or configured RTs */
5646 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5647 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5649 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
5651 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5652 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5654 /* auto derive RD */
5655 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5657 /* link all corresponding l2vnis */
5658 hash_iterate(bgp_evpn
->vnihash
,
5659 (void (*)(struct hash_bucket
*,
5660 void *))link_l2vni_hash_to_l3vni
,
5663 /* Only update all corresponding type-2 routes if we are advertising two
5664 * labels along with type-2 routes
5667 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5668 update_routes_for_vni(bgp_evpn
, vpn
);
5670 /* advertise type-5 routes if needed */
5671 update_advertise_vrf_routes(bgp_vrf
);
5673 /* install all remote routes belonging to this l3vni into correspondng
5675 install_routes_for_vrf(bgp_vrf
);
5680 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5682 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5683 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5684 struct listnode
*node
= NULL
;
5685 struct listnode
*next
= NULL
;
5686 struct bgpevpn
*vpn
= NULL
;
5688 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5692 "Cannot process L3VNI %u Del - Could not find BGP instance",
5697 bgp_evpn
= bgp_get_evpn();
5701 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
5706 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5707 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5708 * routes. This will uninstalling the routes from zebra and decremnt the
5711 uninstall_routes_for_vrf(bgp_vrf
);
5713 /* delete/withdraw all type-5 routes */
5714 delete_withdraw_vrf_routes(bgp_vrf
);
5716 /* remove the l3vni from vrf instance */
5719 /* remove the Rmac from the BGP vrf */
5720 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5721 memset(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, 0, ETH_ALEN
);
5722 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
) &&
5723 !is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac
))
5724 memset(&bgp_vrf
->evpn_info
->pip_rmac
, 0, ETH_ALEN
);
5726 /* remove default import RT or Unmap non-default import RT */
5727 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5728 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5729 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5730 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5733 /* remove default export RT */
5734 if (!list_isempty(bgp_vrf
->vrf_export_rtl
) &&
5735 !CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
5736 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5739 /* update all corresponding local mac-ip routes */
5740 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5741 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5742 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5743 update_routes_for_vni(bgp_evpn
, vpn
);
5747 /* If any L2VNIs point to this instance, unlink them. */
5748 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5749 bgpevpn_unlink_from_l3vni(vpn
);
5751 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5753 /* Delete the instance if it was autocreated */
5754 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5755 bgp_delete(bgp_vrf
);
5761 * Handle del of a local VNI.
5763 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5765 struct bgpevpn
*vpn
;
5767 /* Locate VNI hash */
5768 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5770 if (bgp_debug_zebra(NULL
))
5772 EC_BGP_EVPN_VPN_VNI
,
5773 "%u: VNI hash entry for VNI %u not found at DEL",
5778 /* Remove all local EVPN routes and schedule for processing (to
5779 * withdraw from peers).
5781 delete_routes_for_vni(bgp
, vpn
);
5783 bgp_evpn_unlink_from_vni_svi_hash(bgp
, vpn
);
5785 vpn
->svi_ifindex
= 0;
5787 * tunnel is no longer active, del tunnel ip address from tip_hash
5789 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5791 /* Clear "live" flag and see if hash needs to be freed. */
5792 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5793 if (!is_vni_configured(vpn
))
5794 bgp_evpn_free(bgp
, vpn
);
5800 * Handle add (or update) of a local VNI. The VNI changes we care
5801 * about are for the local-tunnel-ip and the (tenant) VRF.
5803 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5804 struct in_addr originator_ip
,
5805 vrf_id_t tenant_vrf_id
,
5806 struct in_addr mcast_grp
,
5807 ifindex_t svi_ifindex
)
5809 struct bgpevpn
*vpn
;
5810 struct prefix_evpn p
;
5812 /* Lookup VNI. If present and no change, exit. */
5813 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5816 if (is_vni_live(vpn
)
5817 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5818 && IPV4_ADDR_SAME(&vpn
->mcast_grp
, &mcast_grp
)
5819 && vpn
->tenant_vrf_id
== tenant_vrf_id
5820 && vpn
->svi_ifindex
== svi_ifindex
)
5821 /* Probably some other param has changed that we don't
5825 bgp_evpn_mcast_grp_change(bgp
, vpn
, mcast_grp
);
5827 if (vpn
->svi_ifindex
!= svi_ifindex
) {
5830 * Unresolve all the gateway IP nexthops for this VNI
5833 bgp_evpn_remote_ip_hash_iterate(
5835 (void (*)(struct hash_bucket
*, void *))
5836 bgp_evpn_remote_ip_hash_unlink_nexthop
,
5838 bgp_evpn_unlink_from_vni_svi_hash(bgp
, vpn
);
5839 vpn
->svi_ifindex
= svi_ifindex
;
5840 bgp_evpn_link_to_vni_svi_hash(bgp
, vpn
);
5843 * Resolve all the gateway IP nexthops for this VNI
5846 bgp_evpn_remote_ip_hash_iterate(
5848 (void (*)(struct hash_bucket
*, void *))
5849 bgp_evpn_remote_ip_hash_link_nexthop
,
5853 /* Update tenant_vrf_id if it has changed. */
5854 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5857 * Unresolve all the gateway IP nexthops for this VNI
5860 bgp_evpn_remote_ip_hash_iterate(
5862 (void (*)(struct hash_bucket
*, void *))
5863 bgp_evpn_remote_ip_hash_unlink_nexthop
,
5865 bgpevpn_unlink_from_l3vni(vpn
);
5866 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5867 bgpevpn_link_to_l3vni(vpn
);
5870 * Resolve all the gateway IP nexthops for this VNI
5873 bgp_evpn_remote_ip_hash_iterate(
5875 (void (*)(struct hash_bucket
*, void *))
5876 bgp_evpn_remote_ip_hash_link_nexthop
,
5880 /* If tunnel endpoint IP has changed, update (and delete prior
5881 * type-3 route, if needed.)
5883 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5884 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5886 /* Update all routes with new endpoint IP and/or export RT
5889 if (is_vni_live(vpn
))
5890 update_routes_for_vni(bgp
, vpn
);
5892 /* Create or update as appropriate. */
5893 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
,
5894 mcast_grp
, svi_ifindex
);
5897 /* if the VNI is live already, there is nothing more to do */
5898 if (is_vni_live(vpn
))
5901 /* Mark as "live" */
5902 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5904 /* tunnel is now active, add tunnel-ip to db */
5905 bgp_tip_add(bgp
, &originator_ip
);
5907 /* filter routes as nexthop database has changed */
5908 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5911 * Create EVPN type-3 route and schedule for processing.
5913 * RT-3 only if doing head-end replication
5915 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
5916 == VXLAN_FLOOD_HEAD_END_REPL
) {
5917 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5918 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
)) {
5919 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5920 "%u: Type3 route creation failure for VNI %u",
5926 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5930 install_routes_for_vni(bgp
, vpn
);
5932 /* If we are advertising gateway mac-ip
5933 It needs to be conveyed again to zebra */
5934 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5936 /* advertise svi mac-ip knob to zebra */
5937 bgp_zebra_advertise_svi_macip(bgp
, vpn
->advertise_svi_macip
, vpn
->vni
);
5943 * Handle change in setting for BUM handling. The supported values
5944 * are head-end replication and dropping all BUM packets. Any change
5945 * should be registered with zebra. Also, if doing head-end replication,
5946 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5947 * to be dropped, the RT-3s must be withdrawn.
5949 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
5951 zlog_info("L2VPN EVPN BUM handling is %s",
5952 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
5953 "Flooding" : "Flooding Disabled");
5955 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
5956 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
5957 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
5958 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
5959 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
5963 * Cleanup EVPN information on disable - Need to delete and withdraw
5964 * EVPN routes from peers.
5966 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5968 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_bucket
*,
5969 void *))cleanup_vni_on_disable
,
5974 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5975 * BGP instance (default) is being freed.
5977 void bgp_evpn_cleanup(struct bgp
*bgp
)
5979 hash_iterate(bgp
->vnihash
,
5980 (void (*)(struct hash_bucket
*, void *))free_vni_entry
,
5983 hash_clean(bgp
->import_rt_hash
, (void (*)(void *))hash_import_rt_free
);
5984 hash_free(bgp
->import_rt_hash
);
5985 bgp
->import_rt_hash
= NULL
;
5987 hash_clean(bgp
->vrf_import_rt_hash
,
5988 (void (*)(void *))hash_vrf_import_rt_free
);
5989 hash_free(bgp
->vrf_import_rt_hash
);
5990 bgp
->vrf_import_rt_hash
= NULL
;
5992 hash_clean(bgp
->vni_svi_hash
, (void (*)(void *))hash_evpn_free
);
5993 hash_free(bgp
->vni_svi_hash
);
5994 bgp
->vni_svi_hash
= NULL
;
5995 hash_free(bgp
->vnihash
);
5996 bgp
->vnihash
= NULL
;
5998 list_delete(&bgp
->vrf_import_rtl
);
5999 list_delete(&bgp
->vrf_export_rtl
);
6000 list_delete(&bgp
->l2vnis
);
6004 * Initialization for EVPN
6007 * hash for RT to VNI
6009 void bgp_evpn_init(struct bgp
*bgp
)
6012 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
6014 hash_create(vni_svi_hash_key_make
, vni_svi_hash_cmp
,
6015 "BGP VNI hash based on SVI ifindex");
6016 bgp
->import_rt_hash
=
6017 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
6018 "BGP Import RT Hash");
6019 bgp
->vrf_import_rt_hash
=
6020 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
6021 "BGP VRF Import RT Hash");
6022 bgp
->vrf_import_rtl
= list_new();
6023 bgp
->vrf_import_rtl
->cmp
=
6024 (int (*)(void *, void *))bgp_evpn_route_target_cmp
;
6025 bgp
->vrf_import_rtl
->del
= bgp_evpn_xxport_delete_ecomm
;
6026 bgp
->vrf_export_rtl
= list_new();
6027 bgp
->vrf_export_rtl
->cmp
=
6028 (int (*)(void *, void *))bgp_evpn_route_target_cmp
;
6029 bgp
->vrf_export_rtl
->del
= bgp_evpn_xxport_delete_ecomm
;
6030 bgp
->l2vnis
= list_new();
6031 bgp
->l2vnis
->cmp
= vni_list_cmp
;
6032 /* By default Duplicate Address Dection is enabled.
6033 * Max-moves (N) 5, detection time (M) 180
6034 * default action is warning-only
6035 * freeze action permanently freezes address,
6036 * and freeze time (auto-recovery) is disabled.
6038 if (bgp
->evpn_info
) {
6039 bgp
->evpn_info
->dup_addr_detect
= true;
6040 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
6041 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
6042 bgp
->evpn_info
->dad_freeze
= false;
6043 bgp
->evpn_info
->dad_freeze_time
= 0;
6044 /* Initialize zebra vxlan */
6045 bgp_zebra_dup_addr_detection(bgp
);
6046 /* Enable PIP feature by default for bgp vrf instance */
6047 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
6048 struct bgp
*bgp_default
;
6050 bgp
->evpn_info
->advertise_pip
= true;
6051 bgp_default
= bgp_get_default();
6053 bgp
->evpn_info
->pip_ip
= bgp_default
->router_id
;
6057 /* Default BUM handling is to do head-end replication. */
6058 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
6060 bgp_evpn_nh_init(bgp
);
6063 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
6065 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
6066 bgp_evpn_nh_finish(bgp_vrf
);
6070 * Get the prefixlen of the ip prefix carried within the type5 evpn route.
6072 int bgp_evpn_get_type5_prefixlen(const struct prefix
*pfx
)
6074 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6076 if (!pfx
|| pfx
->family
!= AF_EVPN
)
6079 if (evp
->prefix
.route_type
!= BGP_EVPN_IP_PREFIX_ROUTE
)
6082 return evp
->prefix
.prefix_addr
.ip_prefix_length
;
6086 * Should we register nexthop for this EVPN prefix for nexthop tracking?
6088 bool bgp_evpn_is_prefix_nht_supported(const struct prefix
*pfx
)
6090 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6093 * EVPN routes should be marked as valid only if the nexthop is
6094 * reachable. Only if this happens, the route should be imported
6095 * (into VNI or VRF routing tables) and/or advertised.
6096 * Note: This is currently applied for EVPN type-1, type-2,
6097 * type-3, type-4 and type-5 routes.
6098 * It may be tweaked later on for other routes, or
6099 * even removed completely when all routes are handled.
6101 if (pfx
&& pfx
->family
== AF_EVPN
6102 && (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
6103 || evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
6104 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
6105 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
6106 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
6112 static void *bgp_evpn_remote_ip_hash_alloc(void *p
)
6114 const struct evpn_remote_ip
*key
= (const struct evpn_remote_ip
*)p
;
6115 struct evpn_remote_ip
*ip
;
6117 ip
= XMALLOC(MTYPE_EVPN_REMOTE_IP
, sizeof(struct evpn_remote_ip
));
6119 ip
->macip_path_list
= list_new();
6124 static unsigned int bgp_evpn_remote_ip_hash_key_make(const void *p
)
6126 const struct evpn_remote_ip
*ip
= p
;
6127 const struct ipaddr
*addr
= &ip
->addr
;
6129 if (IS_IPADDR_V4(addr
))
6130 return jhash_1word(addr
->ipaddr_v4
.s_addr
, 0);
6132 return jhash2(addr
->ipaddr_v6
.s6_addr32
,
6133 array_size(addr
->ipaddr_v6
.s6_addr32
), 0);
6136 static bool bgp_evpn_remote_ip_hash_cmp(const void *p1
, const void *p2
)
6138 const struct evpn_remote_ip
*ip1
= p1
;
6139 const struct evpn_remote_ip
*ip2
= p2
;
6141 return !ipaddr_cmp(&ip1
->addr
, &ip2
->addr
);
6144 static void bgp_evpn_remote_ip_hash_init(struct bgpevpn
*vpn
)
6146 if (!evpn_resolve_overlay_index())
6149 vpn
->remote_ip_hash
= hash_create(bgp_evpn_remote_ip_hash_key_make
,
6150 bgp_evpn_remote_ip_hash_cmp
,
6151 "BGP EVPN remote IP hash");
6154 static void bgp_evpn_remote_ip_hash_free(struct hash_bucket
*bucket
, void *args
)
6156 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6157 struct bgpevpn
*vpn
= (struct bgpevpn
*)args
;
6159 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, false);
6161 list_delete(&ip
->macip_path_list
);
6163 hash_release(vpn
->remote_ip_hash
, ip
);
6164 XFREE(MTYPE_EVPN_REMOTE_IP
, ip
);
6167 static void bgp_evpn_remote_ip_hash_destroy(struct bgpevpn
*vpn
)
6169 if (!evpn_resolve_overlay_index() || vpn
->remote_ip_hash
== NULL
)
6172 hash_iterate(vpn
->remote_ip_hash
,
6173 (void (*)(struct hash_bucket
*, void *))bgp_evpn_remote_ip_hash_free
,
6176 hash_free(vpn
->remote_ip_hash
);
6177 vpn
->remote_ip_hash
= NULL
;
6180 /* Add a remote MAC/IP route to hash table */
6181 static void bgp_evpn_remote_ip_hash_add(struct bgpevpn
*vpn
,
6182 struct bgp_path_info
*pi
)
6184 struct evpn_remote_ip tmp
;
6185 struct evpn_remote_ip
*ip
;
6186 struct prefix_evpn
*evp
;
6188 if (!evpn_resolve_overlay_index())
6191 if (pi
->type
!= ZEBRA_ROUTE_BGP
|| pi
->sub_type
!= BGP_ROUTE_IMPORTED
6192 || !CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
))
6195 evp
= (struct prefix_evpn
*)&pi
->net
->p
;
6197 if (evp
->family
!= AF_EVPN
6198 || evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
6199 || is_evpn_prefix_ipaddr_none(evp
))
6202 tmp
.addr
= evp
->prefix
.macip_addr
.ip
;
6203 ip
= hash_lookup(vpn
->remote_ip_hash
, &tmp
);
6205 if (listnode_lookup(ip
->macip_path_list
, pi
) != NULL
)
6207 (void)listnode_add(ip
->macip_path_list
, pi
);
6211 ip
= hash_get(vpn
->remote_ip_hash
, &tmp
, bgp_evpn_remote_ip_hash_alloc
);
6212 (void)listnode_add(ip
->macip_path_list
, pi
);
6214 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, true);
6217 /* Delete a remote MAC/IP route from hash table */
6218 static void bgp_evpn_remote_ip_hash_del(struct bgpevpn
*vpn
,
6219 struct bgp_path_info
*pi
)
6221 struct evpn_remote_ip tmp
;
6222 struct evpn_remote_ip
*ip
;
6223 struct prefix_evpn
*evp
;
6225 if (!evpn_resolve_overlay_index())
6228 evp
= (struct prefix_evpn
*)&pi
->net
->p
;
6230 if (evp
->family
!= AF_EVPN
6231 || evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
6232 || is_evpn_prefix_ipaddr_none(evp
))
6235 tmp
.addr
= evp
->prefix
.macip_addr
.ip
;
6236 ip
= hash_lookup(vpn
->remote_ip_hash
, &tmp
);
6240 listnode_delete(ip
->macip_path_list
, pi
);
6242 if (ip
->macip_path_list
->count
== 0) {
6243 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, false);
6244 hash_release(vpn
->remote_ip_hash
, ip
);
6245 XFREE(MTYPE_EVPN_REMOTE_IP
, ip
);
6249 static void bgp_evpn_remote_ip_hash_iterate(struct bgpevpn
*vpn
,
6250 void (*func
)(struct hash_bucket
*,
6254 if (!evpn_resolve_overlay_index())
6257 hash_iterate(vpn
->remote_ip_hash
, func
, arg
);
6260 static void show_remote_ip_entry(struct hash_bucket
*bucket
, void *args
)
6262 char buf
[INET6_ADDRSTRLEN
];
6263 struct listnode
*node
= NULL
;
6264 struct bgp_path_info
*pi
= NULL
;
6265 struct vty
*vty
= (struct vty
*)args
;
6266 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6268 vty_out(vty
, " Remote IP: %s\n",
6269 ipaddr2str(&ip
->addr
, buf
, sizeof(buf
)));
6270 vty_out(vty
, " Linked MAC/IP routes:\n");
6271 for (ALL_LIST_ELEMENTS_RO(ip
->macip_path_list
, node
, pi
))
6272 vty_out(vty
, " %pFX\n", &pi
->net
->p
);
6275 void bgp_evpn_show_remote_ip_hash(struct hash_bucket
*bucket
, void *args
)
6277 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
6278 struct vty
*vty
= (struct vty
*)args
;
6280 vty_out(vty
, "VNI: %u\n", vpn
->vni
);
6281 bgp_evpn_remote_ip_hash_iterate(
6283 (void (*)(struct hash_bucket
*, void *))show_remote_ip_entry
,
6288 static void bgp_evpn_remote_ip_hash_link_nexthop(struct hash_bucket
*bucket
,
6291 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6292 struct bgpevpn
*vpn
= (struct bgpevpn
*)args
;
6294 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, true);
6297 static void bgp_evpn_remote_ip_hash_unlink_nexthop(struct hash_bucket
*bucket
,
6300 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6301 struct bgpevpn
*vpn
= (struct bgpevpn
*)args
;
6303 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, false);
6306 static unsigned int vni_svi_hash_key_make(const void *p
)
6308 const struct bgpevpn
*vpn
= p
;
6310 return jhash_1word(vpn
->svi_ifindex
, 0);
6313 static bool vni_svi_hash_cmp(const void *p1
, const void *p2
)
6315 const struct bgpevpn
*vpn1
= p1
;
6316 const struct bgpevpn
*vpn2
= p2
;
6318 return (vpn1
->svi_ifindex
== vpn2
->svi_ifindex
);
6321 static struct bgpevpn
*bgp_evpn_vni_svi_hash_lookup(struct bgp
*bgp
,
6324 struct bgpevpn
*vpn
;
6327 memset(&tmp
, 0, sizeof(tmp
));
6328 tmp
.svi_ifindex
= svi
;
6329 vpn
= hash_lookup(bgp
->vni_svi_hash
, &tmp
);
6333 static void bgp_evpn_link_to_vni_svi_hash(struct bgp
*bgp
, struct bgpevpn
*vpn
)
6335 if (vpn
->svi_ifindex
== 0)
6338 (void)hash_get(bgp
->vni_svi_hash
, vpn
, hash_alloc_intern
);
6341 static void bgp_evpn_unlink_from_vni_svi_hash(struct bgp
*bgp
,
6342 struct bgpevpn
*vpn
)
6344 if (vpn
->svi_ifindex
== 0)
6347 hash_release(bgp
->vni_svi_hash
, vpn
);
6350 void bgp_evpn_show_vni_svi_hash(struct hash_bucket
*bucket
, void *args
)
6352 struct bgpevpn
*evpn
= (struct bgpevpn
*)bucket
->data
;
6353 struct vty
*vty
= (struct vty
*)args
;
6355 vty_out(vty
, "SVI: %u VNI: %u\n", evpn
->svi_ifindex
, evpn
->vni
);
6359 * This function is called for a bgp_nexthop_cache entry when the nexthop is
6360 * gateway IP overlay index.
6361 * This function returns true if there is a remote MAC/IP route for the gateway
6362 * IP in the EVI of the nexthop SVI.
6364 bool bgp_evpn_is_gateway_ip_resolved(struct bgp_nexthop_cache
*bnc
)
6366 struct bgp
*bgp_evpn
= NULL
;
6367 struct bgpevpn
*vpn
= NULL
;
6368 struct evpn_remote_ip tmp
;
6371 if (!evpn_resolve_overlay_index())
6374 if (!bnc
->nexthop
|| bnc
->nexthop
->ifindex
== 0)
6377 bgp_evpn
= bgp_get_evpn();
6382 * Gateway IP is resolved by nht over SVI interface.
6383 * Use this SVI to find corresponding EVI(L2 context)
6385 vpn
= bgp_evpn_vni_svi_hash_lookup(bgp_evpn
, bnc
->nexthop
->ifindex
);
6389 if (vpn
->bgp_vrf
!= bnc
->bgp
)
6393 * Check if the gateway IP is present in the EVI remote_ip_hash table
6394 * which stores all the remote IP addresses received via MAC/IP routes
6397 memset(&tmp
, 0, sizeof(tmp
));
6400 if (p
->family
== AF_INET
) {
6401 tmp
.addr
.ipa_type
= IPADDR_V4
;
6402 memcpy(&(tmp
.addr
.ipaddr_v4
), &(p
->u
.prefix4
),
6403 sizeof(struct in_addr
));
6404 } else if (p
->family
== AF_INET6
) {
6405 tmp
.addr
.ipa_type
= IPADDR_V6
;
6406 memcpy(&(tmp
.addr
.ipaddr_v6
), &(p
->u
.prefix6
),
6407 sizeof(struct in6_addr
));
6411 if (hash_lookup(vpn
->remote_ip_hash
, &tmp
) == NULL
)
6417 /* Resolve/Unresolve nexthops when a MAC/IP route is added/deleted */
6418 static void bgp_evpn_remote_ip_process_nexthops(struct bgpevpn
*vpn
,
6419 struct ipaddr
*addr
,
6424 struct bgp_nexthop_cache
*bnc
;
6425 struct bgp_nexthop_cache_head
*tree
= NULL
;
6427 if (!vpn
->bgp_vrf
|| vpn
->svi_ifindex
== 0)
6430 memset(&p
, 0, sizeof(p
));
6432 if (addr
->ipa_type
== IPADDR_V4
) {
6435 memcpy(&(p
.u
.prefix4
), &(addr
->ipaddr_v4
),
6436 sizeof(struct in_addr
));
6437 p
.prefixlen
= IPV4_MAX_BITLEN
;
6438 } else if (addr
->ipa_type
== IPADDR_V6
) {
6440 p
.family
= AF_INET6
;
6441 memcpy(&(p
.u
.prefix6
), &(addr
->ipaddr_v6
),
6442 sizeof(struct in6_addr
));
6443 p
.prefixlen
= IPV6_MAX_BITLEN
;
6447 tree
= &vpn
->bgp_vrf
->nexthop_cache_table
[afi
];
6448 bnc
= bnc_find(tree
, &p
, 0, 0);
6450 if (!bnc
|| !bnc
->is_evpn_gwip_nexthop
)
6453 if (!bnc
->nexthop
|| bnc
->nexthop
->ifindex
!= vpn
->svi_ifindex
)
6456 if (BGP_DEBUG(nht
, NHT
))
6457 zlog_debug("%s(%u): vni %u mac/ip %s for NH %pFX",
6458 vpn
->bgp_vrf
->name_pretty
, vpn
->tenant_vrf_id
,
6459 vpn
->vni
, (resolve
? "add" : "delete"),
6463 * MAC/IP route or SVI or tenant vrf being added to EVI.
6464 * Set nexthop as valid only if it is already L3 reachable
6466 if (resolve
&& bnc
->flags
& BGP_NEXTHOP_EVPN_INCOMPLETE
) {
6467 bnc
->flags
&= ~BGP_NEXTHOP_EVPN_INCOMPLETE
;
6468 bnc
->flags
|= BGP_NEXTHOP_VALID
;
6469 bnc
->change_flags
|= BGP_NEXTHOP_MACIP_CHANGED
;
6470 evaluate_paths(bnc
);
6473 /* MAC/IP route or SVI or tenant vrf being deleted from EVI */
6474 if (!resolve
&& bnc
->flags
& BGP_NEXTHOP_VALID
) {
6475 bnc
->flags
&= ~BGP_NEXTHOP_VALID
;
6476 bnc
->flags
|= BGP_NEXTHOP_EVPN_INCOMPLETE
;
6477 bnc
->change_flags
|= BGP_NEXTHOP_MACIP_CHANGED
;
6478 evaluate_paths(bnc
);
6482 void bgp_evpn_handle_resolve_overlay_index_set(struct hash_bucket
*bucket
,
6485 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
6486 struct bgp_dest
*dest
;
6487 struct bgp_path_info
*pi
;
6489 bgp_evpn_remote_ip_hash_init(vpn
);
6491 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
6492 dest
= bgp_route_next(dest
))
6493 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
6494 bgp_evpn_remote_ip_hash_add(vpn
, pi
);
6497 void bgp_evpn_handle_resolve_overlay_index_unset(struct hash_bucket
*bucket
,
6500 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
6502 bgp_evpn_remote_ip_hash_destroy(vpn
);