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
);
65 DEFINE_MTYPE_STATIC(BGPD
, VRF_ROUTE_TARGET
, "L3 Route Target");
68 * Static function declarations
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
);
354 * Compare L3 Route Targets.
356 static int evpn_vrf_route_target_cmp(struct vrf_route_target
*rt1
,
357 struct vrf_route_target
*rt2
)
359 return bgp_evpn_route_target_cmp(rt1
->ecom
, rt2
->ecom
);
362 void bgp_evpn_xxport_delete_ecomm(void *val
)
364 struct ecommunity
*ecomm
= val
;
365 ecommunity_free(&ecomm
);
369 * Delete l3 Route Target.
371 static void evpn_vrf_rt_del(void *val
)
373 struct vrf_route_target
*l3rt
= val
;
375 ecommunity_free(&l3rt
->ecom
);
377 XFREE(MTYPE_VRF_ROUTE_TARGET
, l3rt
);
381 * Allocate a new l3 Route Target.
383 static struct vrf_route_target
*evpn_vrf_rt_new(struct ecommunity
*ecom
)
385 struct vrf_route_target
*l3rt
;
387 l3rt
= XCALLOC(MTYPE_VRF_ROUTE_TARGET
, sizeof(struct vrf_route_target
));
395 * Mask off global-admin field of specified extended community (RT),
396 * just retain the local-admin field.
398 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
399 const struct ecommunity_val
*src
)
405 if (type
== ECOMMUNITY_ENCODE_AS
) {
406 dst
->val
[2] = dst
->val
[3] = 0;
407 } else if (type
== ECOMMUNITY_ENCODE_AS4
408 || type
== ECOMMUNITY_ENCODE_IP
) {
409 dst
->val
[2] = dst
->val
[3] = 0;
410 dst
->val
[4] = dst
->val
[5] = 0;
415 * Converts the RT to Ecommunity Value and adjusts masking based
416 * on flags set for RT.
418 static void vrf_rt2ecom_val(struct ecommunity_val
*to_eval
,
419 const struct vrf_route_target
*l3rt
, int iter
)
421 const struct ecommunity_val
*eval
;
423 eval
= (const struct ecommunity_val
*)(l3rt
->ecom
->val
+
424 (iter
* ECOMMUNITY_SIZE
));
425 /* If using "automatic" or "wildcard *" RT,
426 * we only care about the local-admin sub-field.
427 * This is to facilitate using L3VNI(VRF-VNI)
428 * as the RT for EBGP peering too and simplify
429 * configurations by allowing any ASN via '*'.
431 memcpy(to_eval
, eval
, ECOMMUNITY_SIZE
);
433 if (CHECK_FLAG(l3rt
->flags
, BGP_VRF_RT_AUTO
) ||
434 CHECK_FLAG(l3rt
->flags
, BGP_VRF_RT_WILD
))
435 mask_ecom_global_admin(to_eval
, eval
);
439 * Map one RT to specified VRF.
440 * bgp_vrf = BGP vrf instance
442 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct vrf_route_target
*l3rt
)
446 for (i
= 0; i
< l3rt
->ecom
->size
; i
++) {
447 struct vrf_irt_node
*irt
= NULL
;
448 struct ecommunity_val eval_tmp
;
450 /* Adjust masking for value */
451 vrf_rt2ecom_val(&eval_tmp
, l3rt
, i
);
453 irt
= lookup_vrf_import_rt(&eval_tmp
);
455 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
456 return; /* Already mapped. */
459 irt
= vrf_import_rt_new(&eval_tmp
);
461 /* Add VRF to the list for this RT. */
462 listnode_add(irt
->vrfs
, bgp_vrf
);
467 * Unmap specified VRF from specified RT. If there are no other
468 * VRFs for this RT, then the RT hash is deleted.
469 * bgp_vrf: BGP VRF specific instance
471 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
,
472 struct vrf_route_target
*l3rt
)
476 for (i
= 0; i
< l3rt
->ecom
->size
; i
++) {
477 struct vrf_irt_node
*irt
;
478 struct ecommunity_val eval_tmp
;
480 /* Adjust masking for value */
481 vrf_rt2ecom_val(&eval_tmp
, l3rt
, i
);
483 irt
= lookup_vrf_import_rt(&eval_tmp
);
486 return; /* Not mapped */
488 /* Delete VRF from list for this RT. */
489 listnode_delete(irt
->vrfs
, bgp_vrf
);
491 if (!listnode_head(irt
->vrfs
))
492 vrf_import_rt_free(irt
);
497 * Map one RT to specified VNI.
499 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
500 struct ecommunity_val
*eval
)
502 struct irt_node
*irt
;
503 struct ecommunity_val eval_tmp
;
505 /* If using "automatic" RT, we only care about the local-admin
507 * This is to facilitate using VNI as the RT for EBGP peering too.
509 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
510 if (!is_import_rt_configured(vpn
))
511 mask_ecom_global_admin(&eval_tmp
, eval
);
513 irt
= lookup_import_rt(bgp
, &eval_tmp
);
515 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
516 /* Already mapped. */
520 irt
= import_rt_new(bgp
, &eval_tmp
);
522 /* Add VNI to the hash list for this RT. */
523 listnode_add(irt
->vnis
, vpn
);
527 * Unmap specified VNI from specified RT. If there are no other
528 * VNIs for this RT, then the RT hash is deleted.
530 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
531 struct irt_node
*irt
)
533 /* Delete VNI from hash list for this RT. */
534 listnode_delete(irt
->vnis
, vpn
);
535 if (!listnode_head(irt
->vnis
)) {
536 import_rt_free(bgp
, irt
);
540 static void bgp_evpn_get_rmac_nexthop(struct bgpevpn
*vpn
,
541 const struct prefix_evpn
*p
,
542 struct attr
*attr
, uint8_t flags
)
544 struct bgp
*bgp_vrf
= vpn
->bgp_vrf
;
546 memset(&attr
->rmac
, 0, sizeof(struct ethaddr
));
550 if (p
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
553 /* Copy sys (pip) RMAC and PIP IP as nexthop
554 * in case of route is self MAC-IP,
555 * advertise-pip and advertise-svi-ip features
557 * Otherwise, for all host MAC-IP route's
560 if (CHECK_FLAG(flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
)
561 && bgp_vrf
->evpn_info
->advertise_pip
&&
562 bgp_vrf
->evpn_info
->is_anycast_mac
) {
564 memcpy(&attr
->rmac
, &bgp_vrf
->evpn_info
->pip_rmac
,
566 attr
->nexthop
= bgp_vrf
->evpn_info
->pip_ip
;
567 attr
->mp_nexthop_global_in
=
568 bgp_vrf
->evpn_info
->pip_ip
;
570 memcpy(&attr
->rmac
, &bgp_vrf
->rmac
, ETH_ALEN
);
574 * Create RT extended community automatically from passed information:
575 * of the form AS:VNI.
576 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
577 * the need is to get a RT value that will be unique across different
578 * VNIs but the same across routers (in the same AS) for a particular
581 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
,
584 struct ecommunity_val eval
;
585 struct ecommunity
*ecomadd
;
586 struct ecommunity
*ecom
;
587 struct vrf_route_target
*l3rt
;
588 struct vrf_route_target
*newrt
;
589 bool ecom_found
= false;
590 struct listnode
*node
;
592 if (bgp
->advertise_autort_rfc8365
)
593 vni
|= EVPN_AUTORT_VXLAN
;
594 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
596 ecomadd
= ecommunity_new();
597 ecommunity_add_val(ecomadd
, &eval
, false, false);
600 for (ALL_LIST_ELEMENTS_RO(rtl
, node
, l3rt
))
601 if (ecommunity_cmp(ecomadd
, l3rt
->ecom
)) {
606 for (ALL_LIST_ELEMENTS_RO(rtl
, node
, ecom
))
607 if (ecommunity_cmp(ecomadd
, ecom
)) {
615 newrt
= evpn_vrf_rt_new(ecomadd
);
616 /* Label it as autoderived */
617 SET_FLAG(newrt
->flags
, BGP_VRF_RT_AUTO
);
618 listnode_add_sort(rtl
, newrt
);
620 listnode_add_sort(rtl
, ecomadd
);
622 ecommunity_free(&ecomadd
);
626 * Derive RD and RT for a VNI automatically. Invoked at the time of
629 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
631 bgp_evpn_derive_auto_rd(bgp
, vpn
);
632 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
633 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
637 * Convert nexthop (remote VTEP IP) into an IPv6 address.
639 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
641 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
643 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
644 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
648 * Wrapper for node get in global table.
650 struct bgp_dest
*bgp_evpn_global_node_get(struct bgp_table
*table
, afi_t afi
,
652 const struct prefix_evpn
*evp
,
653 struct prefix_rd
*prd
,
654 const struct bgp_path_info
*local_pi
)
656 struct prefix_evpn global_p
;
658 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
) {
659 /* prefix in the global table doesn't include the VTEP-IP so
660 * we need to create a different copy of the prefix
662 evpn_type1_prefix_global_copy(&global_p
, evp
);
664 } else if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
667 * prefix in the global table needs MAC/IP, ensure they are
668 * present, using one's from local table's path_info.
670 if (is_evpn_prefix_ipaddr_none(evp
)) {
671 /* VNI MAC -> Global */
672 evpn_type2_prefix_global_copy(
673 &global_p
, evp
, NULL
/* mac */,
674 evpn_type2_path_info_get_ip(local_pi
));
676 /* VNI IP -> Global */
677 evpn_type2_prefix_global_copy(
679 evpn_type2_path_info_get_mac(local_pi
),
685 return bgp_afi_node_get(table
, afi
, safi
, (struct prefix
*)evp
, prd
);
689 * Wrapper for node lookup in global table.
692 bgp_evpn_global_node_lookup(struct bgp_table
*table
, afi_t afi
, safi_t safi
,
693 const struct prefix_evpn
*evp
,
694 struct prefix_rd
*prd
,
695 const struct bgp_path_info
*local_pi
)
697 struct prefix_evpn global_p
;
699 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
) {
700 /* prefix in the global table doesn't include the VTEP-IP so
701 * we need to create a different copy of the prefix
703 evpn_type1_prefix_global_copy(&global_p
, evp
);
705 } else if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
708 * prefix in the global table needs MAC/IP, ensure they are
709 * present, using one's from local table's path_info.
711 if (is_evpn_prefix_ipaddr_none(evp
)) {
712 /* VNI MAC -> Global */
713 evpn_type2_prefix_global_copy(
714 &global_p
, evp
, NULL
/* mac */,
715 evpn_type2_path_info_get_ip(local_pi
));
717 /* VNI IP -> Global */
718 evpn_type2_prefix_global_copy(
720 evpn_type2_path_info_get_mac(local_pi
),
726 return bgp_afi_node_lookup(table
, afi
, safi
, (struct prefix
*)evp
, prd
);
730 * Wrapper for node get in VNI IP table.
732 struct bgp_dest
*bgp_evpn_vni_ip_node_get(struct bgp_table
*const table
,
733 const struct prefix_evpn
*evp
,
734 const struct bgp_path_info
*parent_pi
)
736 struct prefix_evpn vni_p
;
738 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
&& parent_pi
) {
739 /* prefix in the global table doesn't include the VTEP-IP so
740 * we need to create a different copy for the VNI
742 evpn_type1_prefix_vni_ip_copy(&vni_p
, evp
,
743 parent_pi
->attr
->nexthop
);
745 } else if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
746 /* Only MAC-IP should go into this table, not mac-only */
747 assert(is_evpn_prefix_ipaddr_none(evp
) == false);
750 * prefix in the vni IP table doesn't include MAC so
751 * we need to create a different copy of the prefix.
753 evpn_type2_prefix_vni_ip_copy(&vni_p
, evp
);
756 return bgp_node_get(table
, (struct prefix
*)evp
);
760 * Wrapper for node lookup in VNI IP table.
763 bgp_evpn_vni_ip_node_lookup(const struct bgp_table
*const table
,
764 const struct prefix_evpn
*evp
,
765 const struct bgp_path_info
*parent_pi
)
767 struct prefix_evpn vni_p
;
769 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
&& parent_pi
) {
770 /* prefix in the global table doesn't include the VTEP-IP so
771 * we need to create a different copy for the VNI
773 evpn_type1_prefix_vni_ip_copy(&vni_p
, evp
,
774 parent_pi
->attr
->nexthop
);
776 } else if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
777 /* Only MAC-IP should go into this table, not mac-only */
778 assert(is_evpn_prefix_ipaddr_none(evp
) == false);
781 * prefix in the vni IP table doesn't include MAC so
782 * we need to create a different copy of the prefix.
784 evpn_type2_prefix_vni_ip_copy(&vni_p
, evp
);
787 return bgp_node_lookup(table
, (struct prefix
*)evp
);
791 * Wrapper for node get in VNI MAC table.
794 bgp_evpn_vni_mac_node_get(struct bgp_table
*const table
,
795 const struct prefix_evpn
*evp
,
796 const struct bgp_path_info
*parent_pi
)
798 struct prefix_evpn vni_p
;
800 /* Only type-2 should ever go into this table */
801 assert(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
);
804 * prefix in the vni MAC table doesn't include IP so
805 * we need to create a different copy of the prefix.
807 evpn_type2_prefix_vni_mac_copy(&vni_p
, evp
);
809 return bgp_node_get(table
, (struct prefix
*)evp
);
813 * Wrapper for node lookup in VNI MAC table.
816 bgp_evpn_vni_mac_node_lookup(const struct bgp_table
*const table
,
817 const struct prefix_evpn
*evp
,
818 const struct bgp_path_info
*parent_pi
)
820 struct prefix_evpn vni_p
;
822 /* Only type-2 should ever go into this table */
823 assert(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
);
826 * prefix in the vni MAC table doesn't include IP so
827 * we need to create a different copy of the prefix.
829 evpn_type2_prefix_vni_mac_copy(&vni_p
, evp
);
831 return bgp_node_lookup(table
, (struct prefix
*)evp
);
835 * Wrapper for node get in both VNI tables.
837 struct bgp_dest
*bgp_evpn_vni_node_get(struct bgpevpn
*vpn
,
838 const struct prefix_evpn
*p
,
839 const struct bgp_path_info
*parent_pi
)
841 if ((p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) &&
842 (is_evpn_prefix_ipaddr_none(p
) == true))
843 return bgp_evpn_vni_mac_node_get(vpn
->mac_table
, p
, parent_pi
);
845 return bgp_evpn_vni_ip_node_get(vpn
->ip_table
, p
, parent_pi
);
849 * Wrapper for node lookup in both VNI tables.
851 struct bgp_dest
*bgp_evpn_vni_node_lookup(const struct bgpevpn
*vpn
,
852 const struct prefix_evpn
*p
,
853 const struct bgp_path_info
*parent_pi
)
855 if ((p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) &&
856 (is_evpn_prefix_ipaddr_none(p
) == true))
857 return bgp_evpn_vni_mac_node_lookup(vpn
->mac_table
, p
,
860 return bgp_evpn_vni_ip_node_lookup(vpn
->ip_table
, p
, parent_pi
);
864 * Add (update) or delete MACIP from zebra.
866 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
867 const struct prefix_evpn
*p
,
868 const struct ethaddr
*mac
,
869 struct in_addr remote_vtep_ip
, int add
,
870 uint8_t flags
, uint32_t seq
, esi_t
*esi
)
874 static struct in_addr zero_remote_vtep_ip
;
878 if (!zclient
|| zclient
->sock
< 0)
881 /* Don't try to register if Zebra doesn't know of this instance. */
882 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
883 if (BGP_DEBUG(zebra
, ZEBRA
))
885 "%s: No zebra instance to talk to, not installing remote macip",
895 zclient_create_header(
896 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
898 stream_putl(s
, vpn
->vni
);
900 if (mac
) /* Mac Addr */
901 stream_put(s
, &mac
->octet
, ETH_ALEN
);
903 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
);
905 /* IP address length and IP address, if any. */
906 if (is_evpn_prefix_ipaddr_none(p
))
909 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
911 stream_putw(s
, ipa_len
);
912 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
914 /* If the ESI is valid that becomes the nexthop; tape out the
915 * VTEP-IP for that case
917 if (bgp_evpn_is_esi_valid(esi
)) {
919 stream_put_in_addr(s
, &zero_remote_vtep_ip
);
922 stream_put_in_addr(s
, &remote_vtep_ip
);
925 /* TX flags - MAC sticky status and/or gateway mac */
926 /* Also TX the sequence number of the best route. */
928 stream_putc(s
, flags
);
930 stream_put(s
, esi
, sizeof(esi_t
));
933 stream_putw_at(s
, 0, stream_get_endp(s
));
935 if (bgp_debug_zebra(NULL
)) {
936 char esi_buf
[ESI_STR_LEN
];
939 esi_to_str(esi
, esi_buf
, sizeof(esi_buf
));
941 snprintf(esi_buf
, sizeof(esi_buf
), "-");
943 "Tx %s MACIP, VNI %u MAC %pEA IP %pIA flags 0x%x seq %u remote VTEP %pI4 esi %s",
944 add
? "ADD" : "DEL", vpn
->vni
,
945 (mac
? mac
: &p
->prefix
.macip_addr
.mac
),
946 &p
->prefix
.macip_addr
.ip
, flags
, seq
, &remote_vtep_ip
,
950 frrtrace(5, frr_bgp
, evpn_mac_ip_zsend
, add
, vpn
, p
, remote_vtep_ip
,
953 return zclient_send_message(zclient
);
957 * Add (update) or delete remote VTEP from zebra.
959 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
960 const struct prefix_evpn
*p
,
961 int flood_control
, int add
)
966 if (!zclient
|| zclient
->sock
< 0)
969 /* Don't try to register if Zebra doesn't know of this instance. */
970 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
971 if (BGP_DEBUG(zebra
, ZEBRA
))
973 "%s: No zebra instance to talk to, not installing remote vtep",
981 zclient_create_header(
982 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
984 stream_putl(s
, vpn
->vni
);
985 if (is_evpn_prefix_ipaddr_v4(p
))
986 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
987 else if (is_evpn_prefix_ipaddr_v6(p
)) {
990 "Bad remote IP when trying to %s remote VTEP for VNI %u",
991 add
? "ADD" : "DEL", vpn
->vni
);
994 stream_putl(s
, flood_control
);
996 stream_putw_at(s
, 0, stream_get_endp(s
));
998 if (bgp_debug_zebra(NULL
))
999 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %pI4",
1000 add
? "ADD" : "DEL", vpn
->vni
,
1001 &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
1003 frrtrace(3, frr_bgp
, evpn_bum_vtep_zsend
, add
, vpn
, p
);
1005 return zclient_send_message(zclient
);
1009 * Build extended communities for EVPN prefix route.
1011 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
1014 struct ecommunity ecom_encap
;
1015 struct ecommunity_val eval
;
1016 struct ecommunity_val eval_rmac
;
1017 bgp_encap_types tnl_type
;
1018 struct listnode
*node
, *nnode
;
1019 struct vrf_route_target
*l3rt
;
1020 struct ecommunity
*old_ecom
;
1021 struct ecommunity
*ecom
;
1022 struct list
*vrf_export_rtl
= NULL
;
1025 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
1026 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
1027 encode_encap_extcomm(tnl_type
, &eval
);
1028 ecom_encap
.size
= 1;
1029 ecom_encap
.unit_size
= ECOMMUNITY_SIZE
;
1030 ecom_encap
.val
= (uint8_t *)eval
.val
;
1033 if (bgp_attr_get_ecommunity(attr
)) {
1034 old_ecom
= bgp_attr_get_ecommunity(attr
);
1035 ecom
= ecommunity_merge(ecommunity_dup(old_ecom
), &ecom_encap
);
1036 if (!old_ecom
->refcnt
)
1037 ecommunity_free(&old_ecom
);
1039 ecom
= ecommunity_dup(&ecom_encap
);
1040 bgp_attr_set_ecommunity(attr
, ecom
);
1041 attr
->encap_tunneltype
= tnl_type
;
1043 /* Add the export RTs for L3VNI/VRF */
1044 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
1045 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, l3rt
))
1046 bgp_attr_set_ecommunity(
1047 attr
, ecommunity_merge(bgp_attr_get_ecommunity(attr
),
1050 /* add the router mac extended community */
1051 if (!is_zero_mac(&attr
->rmac
)) {
1052 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
1053 ecommunity_add_val(bgp_attr_get_ecommunity(attr
), &eval_rmac
,
1059 * Build extended communities for EVPN route.
1060 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
1061 * and ENCAP extended communities are applicable for all routes.
1062 * The default gateway extended community and MAC mobility (sticky) extended
1063 * community are added as needed based on passed settings - only for type-2
1064 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
1065 * added, if present, based on passed settings - only for non-link-local
1068 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
1071 struct ecommunity ecom_encap
;
1072 struct ecommunity ecom_sticky
;
1073 struct ecommunity ecom_default_gw
;
1074 struct ecommunity ecom_na
;
1075 struct ecommunity_val eval
;
1076 struct ecommunity_val eval_sticky
;
1077 struct ecommunity_val eval_default_gw
;
1078 struct ecommunity_val eval_rmac
;
1079 struct ecommunity_val eval_na
;
1082 bgp_encap_types tnl_type
;
1083 struct listnode
*node
, *nnode
;
1084 struct ecommunity
*ecom
;
1085 struct vrf_route_target
*l3rt
;
1087 struct list
*vrf_export_rtl
= NULL
;
1090 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
1091 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
1092 encode_encap_extcomm(tnl_type
, &eval
);
1093 ecom_encap
.size
= 1;
1094 ecom_encap
.unit_size
= ECOMMUNITY_SIZE
;
1095 ecom_encap
.val
= (uint8_t *)eval
.val
;
1098 bgp_attr_set_ecommunity(attr
, ecommunity_dup(&ecom_encap
));
1099 attr
->encap_tunneltype
= tnl_type
;
1101 /* Add the export RTs for L2VNI */
1102 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
1103 bgp_attr_set_ecommunity(
1105 ecommunity_merge(bgp_attr_get_ecommunity(attr
), ecom
));
1107 /* Add the export RTs for L3VNI if told to - caller determines
1108 * when this should be done.
1111 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
1112 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
1113 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
1115 bgp_attr_set_ecommunity(
1118 bgp_attr_get_ecommunity(attr
),
1123 /* Add MAC mobility (sticky) if needed. */
1126 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
1127 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
1128 ecom_sticky
.size
= 1;
1129 ecom_sticky
.unit_size
= ECOMMUNITY_SIZE
;
1130 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
1131 bgp_attr_set_ecommunity(
1132 attr
, ecommunity_merge(bgp_attr_get_ecommunity(attr
),
1136 /* Add RMAC, if told to. */
1138 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
1139 ecommunity_add_val(bgp_attr_get_ecommunity(attr
), &eval_rmac
,
1143 /* Add default gateway, if needed. */
1144 if (attr
->default_gw
) {
1145 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
1146 encode_default_gw_extcomm(&eval_default_gw
);
1147 ecom_default_gw
.size
= 1;
1148 ecom_default_gw
.unit_size
= ECOMMUNITY_SIZE
;
1149 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
1150 bgp_attr_set_ecommunity(
1151 attr
, ecommunity_merge(bgp_attr_get_ecommunity(attr
),
1155 proxy
= !!(attr
->es_flags
& ATTR_ES_PROXY_ADVERT
);
1156 if (attr
->router_flag
|| proxy
) {
1157 memset(&ecom_na
, 0, sizeof(ecom_na
));
1158 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
, proxy
);
1160 ecom_na
.unit_size
= ECOMMUNITY_SIZE
;
1161 ecom_na
.val
= (uint8_t *)eval_na
.val
;
1162 bgp_attr_set_ecommunity(
1163 attr
, ecommunity_merge(bgp_attr_get_ecommunity(attr
),
1169 * Add MAC mobility extended community to attribute.
1171 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
1173 struct ecommunity ecom_tmp
;
1174 struct ecommunity_val eval
;
1175 uint8_t *ecom_val_ptr
;
1180 struct ecommunity
*ecomm
= bgp_attr_get_ecommunity(attr
);
1183 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
1185 /* Find current MM ecommunity */
1186 ecom_val_ptr
= NULL
;
1189 for (i
= 0; i
< ecomm
->size
; i
++) {
1190 pnt
= ecomm
->val
+ (i
* ecomm
->unit_size
);
1194 if (type
== ECOMMUNITY_ENCODE_EVPN
1196 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
1198 (ecomm
->val
+ (i
* ecomm
->unit_size
));
1204 /* Update the existing MM ecommunity */
1206 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ecomm
->unit_size
);
1208 /* Add MM to existing */
1210 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
1212 ecom_tmp
.unit_size
= ECOMMUNITY_SIZE
;
1213 ecom_tmp
.val
= (uint8_t *)eval
.val
;
1216 bgp_attr_set_ecommunity(
1217 attr
, ecommunity_merge(ecomm
, &ecom_tmp
));
1219 bgp_attr_set_ecommunity(attr
,
1220 ecommunity_dup(&ecom_tmp
));
1224 /* Install EVPN route into zebra. */
1225 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1226 const struct prefix_evpn
*p
,
1227 struct bgp_path_info
*pi
)
1234 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
1237 if (pi
->sub_type
== BGP_ROUTE_IMPORTED
) {
1238 if (pi
->attr
->sticky
)
1239 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
1240 if (pi
->attr
->default_gw
)
1241 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
1242 if (is_evpn_prefix_ipaddr_v6(p
) &&
1243 pi
->attr
->router_flag
)
1244 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
1246 seq
= mac_mobility_seqnum(pi
->attr
);
1247 /* if local ES notify zebra that this is a sync path */
1248 if (bgp_evpn_attr_is_local_es(pi
->attr
)) {
1249 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_SYNC_PATH
);
1250 if (bgp_evpn_attr_is_proxy(pi
->attr
))
1252 ZEBRA_MACIP_TYPE_PROXY_ADVERT
);
1255 if (!bgp_evpn_attr_is_sync(pi
->attr
))
1258 /* if a local path is being turned around and sent
1259 * to zebra it is because it is a sync path on
1262 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_SYNC_PATH
);
1263 /* supply the highest peer seq number to zebra
1264 * for MM seq syncing
1266 seq
= bgp_evpn_attr_get_sync_seq(pi
->attr
);
1267 /* if any of the paths from the peer have the ROUTER
1268 * flag set install the local entry as a router entry
1270 if (is_evpn_prefix_ipaddr_v6(p
) &&
1271 (pi
->attr
->es_flags
&
1272 ATTR_ES_PEER_ROUTER
))
1274 ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
1276 if (!(pi
->attr
->es_flags
& ATTR_ES_PEER_ACTIVE
))
1278 ZEBRA_MACIP_TYPE_PROXY_ADVERT
);
1281 ret
= bgp_zebra_send_remote_macip(
1283 (is_evpn_prefix_ipaddr_none(p
)
1284 ? NULL
/* MAC update */
1285 : evpn_type2_path_info_get_mac(
1286 pi
) /* MAC-IP update */),
1287 pi
->attr
->nexthop
, 1, flags
, seq
,
1288 bgp_evpn_attr_get_esi(pi
->attr
));
1289 } else if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
) {
1290 ret
= bgp_evpn_remote_es_evi_add(bgp
, vpn
, p
);
1292 switch (bgp_attr_get_pmsi_tnl_type(pi
->attr
)) {
1293 case PMSI_TNLTYPE_INGR_REPL
:
1294 flood_control
= VXLAN_FLOOD_HEAD_END_REPL
;
1297 case PMSI_TNLTYPE_PIM_SM
:
1298 flood_control
= VXLAN_FLOOD_PIM_SM
;
1302 flood_control
= VXLAN_FLOOD_DISABLED
;
1305 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, flood_control
, 1);
1311 /* Uninstall EVPN route from zebra. */
1312 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1313 const struct prefix_evpn
*p
,
1314 struct bgp_path_info
*pi
, bool is_sync
)
1318 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1319 ret
= bgp_zebra_send_remote_macip(
1321 (is_evpn_prefix_ipaddr_none(p
)
1322 ? NULL
/* MAC update */
1323 : evpn_type2_path_info_get_mac(
1324 pi
) /* MAC-IP update */),
1325 (is_sync
? zero_vtep_ip
: pi
->attr
->nexthop
), 0, 0, 0,
1327 else if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
1328 ret
= bgp_evpn_remote_es_evi_del(bgp
, vpn
, p
);
1330 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
,
1331 VXLAN_FLOOD_DISABLED
, 0);
1337 * Due to MAC mobility, the prior "local" best route has been supplanted
1338 * by a "remote" best route. The prior route has to be deleted and withdrawn
1341 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1342 struct bgp_dest
*dest
,
1343 struct bgp_path_info
*old_local
,
1344 struct bgp_path_info
*new_select
)
1346 struct bgp_dest
*global_dest
;
1347 struct bgp_path_info
*pi
;
1348 afi_t afi
= AFI_L2VPN
;
1349 safi_t safi
= SAFI_EVPN
;
1351 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
1352 char esi_buf
[ESI_STR_LEN
];
1353 char esi_buf2
[ESI_STR_LEN
];
1354 struct prefix_evpn
*evp
=
1355 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1357 zlog_debug("local path deleted %pFX es %s; new-path-es %s", evp
,
1358 esi_to_str(&old_local
->attr
->esi
, esi_buf
,
1360 new_select
? esi_to_str(&new_select
->attr
->esi
,
1361 esi_buf2
, sizeof(esi_buf2
))
1365 /* Locate route node in the global EVPN routing table. Note that
1366 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1369 global_dest
= bgp_evpn_global_node_lookup(
1370 bgp
->rib
[afi
][safi
], afi
, safi
,
1371 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1372 &vpn
->prd
, old_local
);
1374 /* Delete route entry in the global EVPN table. */
1375 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
1377 /* Schedule for processing - withdraws to peers happen from
1381 bgp_process(bgp
, global_dest
, afi
, safi
);
1382 bgp_dest_unlock_node(global_dest
);
1385 /* Delete route entry in the VNI route table, caller to remove. */
1386 bgp_path_info_delete(dest
, old_local
);
1390 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1392 * Note: vpn is NULL for local EAD-ES routes.
1394 int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1395 struct bgp_dest
*dest
)
1397 struct bgp_path_info
*old_select
, *new_select
;
1398 struct bgp_path_info_pair old_and_new
;
1399 afi_t afi
= AFI_L2VPN
;
1400 safi_t safi
= SAFI_EVPN
;
1403 /* Compute the best path. */
1404 bgp_best_selection(bgp
, dest
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1406 old_select
= old_and_new
.old
;
1407 new_select
= old_and_new
.new;
1409 /* If the best path hasn't changed - see if there is still something to
1410 * update to zebra RIB.
1411 * Remote routes and SYNC route (i.e. local routes with
1412 * SYNCED_FROM_PEER flag) need to updated to zebra on any attr
1415 if (old_select
&& old_select
== new_select
1416 && old_select
->type
== ZEBRA_ROUTE_BGP
1417 && (old_select
->sub_type
== BGP_ROUTE_IMPORTED
||
1418 bgp_evpn_attr_is_sync(old_select
->attr
))
1419 && !CHECK_FLAG(dest
->flags
, BGP_NODE_USER_CLEAR
)
1420 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1421 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1422 if (bgp_zebra_has_route_changed(old_select
))
1423 ret
= evpn_zebra_install(
1425 (const struct prefix_evpn
*)bgp_dest_get_prefix(
1428 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1429 UNSET_FLAG(old_select
->flags
, BGP_PATH_LINK_BW_CHG
);
1430 bgp_zebra_clear_route_change_flags(dest
);
1434 /* If the user did a "clear" this flag will be set */
1435 UNSET_FLAG(dest
->flags
, BGP_NODE_USER_CLEAR
);
1437 /* bestpath has changed; update relevant fields and install or uninstall
1438 * into the zebra RIB.
1440 if (old_select
|| new_select
)
1441 bgp_bump_version(dest
);
1444 bgp_path_info_unset_flag(dest
, old_select
, BGP_PATH_SELECTED
);
1446 bgp_path_info_set_flag(dest
, new_select
, BGP_PATH_SELECTED
);
1447 bgp_path_info_unset_flag(dest
, new_select
,
1448 BGP_PATH_ATTR_CHANGED
);
1449 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1450 UNSET_FLAG(new_select
->flags
, BGP_PATH_LINK_BW_CHG
);
1453 /* a local entry with the SYNC flag also results in a MAC-IP update
1456 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1457 && (new_select
->sub_type
== BGP_ROUTE_IMPORTED
||
1458 bgp_evpn_attr_is_sync(new_select
->attr
))) {
1459 ret
= evpn_zebra_install(
1461 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1464 /* If an old best existed and it was a "local" route, the only
1466 * it would be supplanted is due to MAC mobility procedures. So,
1468 * need to do an implicit delete and withdraw that route from
1471 if (new_select
->sub_type
== BGP_ROUTE_IMPORTED
&&
1472 old_select
&& old_select
->peer
== bgp
->peer_self
1473 && old_select
->type
== ZEBRA_ROUTE_BGP
1474 && old_select
->sub_type
== BGP_ROUTE_STATIC
1476 evpn_delete_old_local_route(bgp
, vpn
, dest
,
1477 old_select
, new_select
);
1479 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1480 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1481 ret
= evpn_zebra_uninstall(
1483 (const struct prefix_evpn
*)bgp_dest_get_prefix(
1488 /* Clear any route change flags. */
1489 bgp_zebra_clear_route_change_flags(dest
);
1491 /* Reap old select bgp_path_info, if it has been removed */
1492 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1493 bgp_path_info_reap(dest
, old_select
);
1498 static struct bgp_path_info
*bgp_evpn_route_get_local_path(
1499 struct bgp
*bgp
, struct bgp_dest
*dest
)
1501 struct bgp_path_info
*tmp_pi
;
1502 struct bgp_path_info
*local_pi
= NULL
;
1504 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1505 tmp_pi
= tmp_pi
->next
) {
1506 if (bgp_evpn_is_path_local(bgp
, tmp_pi
)) {
1515 static int update_evpn_type5_route_entry(struct bgp
*bgp_evpn
,
1516 struct bgp
*bgp_vrf
, afi_t afi
,
1517 safi_t safi
, struct bgp_dest
*dest
,
1518 struct attr
*attr
, int *route_changed
)
1520 struct attr
*attr_new
= NULL
;
1521 struct bgp_path_info
*pi
= NULL
;
1522 mpls_label_t label
= MPLS_INVALID_LABEL
;
1523 struct bgp_path_info
*local_pi
= NULL
;
1524 struct bgp_path_info
*tmp_pi
= NULL
;
1527 /* locate the local route entry if any */
1528 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1529 tmp_pi
= tmp_pi
->next
) {
1530 if (tmp_pi
->peer
== bgp_evpn
->peer_self
1531 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1532 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1537 * create a new route entry if one doesn't exist.
1538 * Otherwise see if route attr has changed
1542 /* route has changed as this is the first entry */
1545 /* Add (or update) attribute to hash. */
1546 attr_new
= bgp_attr_intern(attr
);
1548 /* create the route info from attribute */
1549 pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1550 bgp_evpn
->peer_self
, attr_new
, dest
);
1551 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
1553 /* Type-5 routes advertise the L3-VNI */
1554 bgp_path_info_extra_get(pi
);
1555 vni2label(bgp_vrf
->l3vni
, &label
);
1556 memcpy(&pi
->extra
->label
, &label
, sizeof(label
));
1557 pi
->extra
->num_labels
= 1;
1559 /* add the route entry to route node*/
1560 bgp_path_info_add(dest
, pi
);
1564 if (!attrhash_cmp(tmp_pi
->attr
, attr
)) {
1566 /* attribute changed */
1569 /* The attribute has changed. */
1570 /* Add (or update) attribute to hash. */
1571 attr_new
= bgp_attr_intern(attr
);
1572 bgp_path_info_set_flag(dest
, tmp_pi
,
1573 BGP_PATH_ATTR_CHANGED
);
1575 /* Restore route, if needed. */
1576 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1577 bgp_path_info_restore(dest
, tmp_pi
);
1579 /* Unintern existing, set to new. */
1580 bgp_attr_unintern(&tmp_pi
->attr
);
1581 tmp_pi
->attr
= attr_new
;
1582 tmp_pi
->uptime
= monotime(NULL
);
1588 /* update evpn type-5 route entry */
1589 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1590 struct attr
*src_attr
, afi_t src_afi
,
1593 afi_t afi
= AFI_L2VPN
;
1594 safi_t safi
= SAFI_EVPN
;
1596 struct bgp_dest
*dest
= NULL
;
1597 struct bgp
*bgp_evpn
= NULL
;
1598 int route_changed
= 0;
1600 bgp_evpn
= bgp_get_evpn();
1604 /* Build path attribute for this route - use the source attr, if
1605 * present, else treat as locally originated.
1610 memset(&attr
, 0, sizeof(attr
));
1611 bgp_attr_default_set(&attr
, bgp_vrf
, BGP_ORIGIN_IGP
);
1614 /* Advertise Primary IP (PIP) is enabled, send individual
1615 * IP (default instance router-id) as nexthop.
1616 * PIP is disabled or vrr interface is not present
1617 * use anycast-IP as nexthop and anycast RMAC.
1619 if (!bgp_vrf
->evpn_info
->advertise_pip
||
1620 (!bgp_vrf
->evpn_info
->is_anycast_mac
)) {
1621 attr
.nexthop
= bgp_vrf
->originator_ip
;
1622 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1623 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, ETH_ALEN
);
1626 memcpy(&attr
.rmac
, &bgp_vrf
->evpn_info
->pip_rmac
, ETH_ALEN
);
1627 if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
!= INADDR_ANY
) {
1628 attr
.nexthop
= bgp_vrf
->evpn_info
->pip_ip
;
1629 attr
.mp_nexthop_global_in
= bgp_vrf
->evpn_info
->pip_ip
;
1630 } else if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
== INADDR_ANY
)
1631 if (bgp_debug_zebra(NULL
))
1633 "VRF %s evp %pFX advertise-pip primary ip is not configured",
1634 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
);
1637 if (bgp_debug_zebra(NULL
))
1639 "VRF %s type-5 route evp %pFX RMAC %pEA nexthop %pI4",
1640 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
, &attr
.rmac
,
1643 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1645 if (src_afi
== AFI_IP6
&&
1646 CHECK_FLAG(bgp_vrf
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1647 BGP_L2VPN_EVPN_ADV_IPV6_UNICAST_GW_IP
)) {
1649 !IN6_IS_ADDR_UNSPECIFIED(&src_attr
->mp_nexthop_global
)) {
1650 attr
.evpn_overlay
.type
= OVERLAY_INDEX_GATEWAY_IP
;
1651 SET_IPADDR_V6(&attr
.evpn_overlay
.gw_ip
);
1652 memcpy(&attr
.evpn_overlay
.gw_ip
.ipaddr_v6
,
1653 &src_attr
->mp_nexthop_global
,
1654 sizeof(struct in6_addr
));
1656 } else if (src_afi
== AFI_IP
&&
1657 CHECK_FLAG(bgp_vrf
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1658 BGP_L2VPN_EVPN_ADV_IPV4_UNICAST_GW_IP
)) {
1659 if (src_attr
&& src_attr
->nexthop
.s_addr
!= 0) {
1660 attr
.evpn_overlay
.type
= OVERLAY_INDEX_GATEWAY_IP
;
1661 SET_IPADDR_V4(&attr
.evpn_overlay
.gw_ip
);
1662 memcpy(&attr
.evpn_overlay
.gw_ip
.ipaddr_v4
,
1663 &src_attr
->nexthop
, sizeof(struct in_addr
));
1667 /* Setup RT and encap extended community */
1668 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1670 /* get the route node in global table */
1671 dest
= bgp_evpn_global_node_get(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1672 evp
, &bgp_vrf
->vrf_prd
, NULL
);
1675 /* create or update the route entry within the route node */
1676 update_evpn_type5_route_entry(bgp_evpn
, bgp_vrf
, afi
, safi
, dest
, &attr
,
1679 /* schedule for processing and unlock node */
1680 if (route_changed
) {
1681 bgp_process(bgp_evpn
, dest
, afi
, safi
);
1682 bgp_dest_unlock_node(dest
);
1685 /* uninten temporary */
1687 aspath_unintern(&attr
.aspath
);
1691 static void bgp_evpn_get_sync_info(struct bgp
*bgp
, esi_t
*esi
,
1692 struct bgp_dest
*dest
, uint32_t loc_seq
,
1693 uint32_t *max_sync_seq
, bool *active_on_peer
,
1694 bool *peer_router
, bool *proxy_from_peer
,
1695 const struct ethaddr
*mac
)
1697 struct bgp_path_info
*tmp_pi
;
1698 struct bgp_path_info
*second_best_path
= NULL
;
1699 uint32_t tmp_mm_seq
= 0;
1702 struct ethaddr
*tmp_mac
;
1703 bool mac_cmp
= false;
1704 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&dest
->p
;
1707 /* mac comparison is not needed for MAC-only routes */
1708 if (mac
&& !is_evpn_prefix_ipaddr_none(evp
))
1711 /* find the best non-local path. a local path can only be present
1714 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1715 tmp_pi
= tmp_pi
->next
) {
1716 if (tmp_pi
->sub_type
!= BGP_ROUTE_IMPORTED
||
1717 !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1720 /* ignore paths that have a different mac */
1722 tmp_mac
= evpn_type2_path_info_get_mac(tmp_pi
);
1723 if (memcmp(mac
, tmp_mac
, sizeof(*mac
)))
1727 if (bgp_evpn_path_info_cmp(bgp
, tmp_pi
,
1728 second_best_path
, &paths_eq
))
1729 second_best_path
= tmp_pi
;
1732 if (!second_best_path
)
1735 tmp_esi
= bgp_evpn_attr_get_esi(second_best_path
->attr
);
1736 /* if this has the same ES desination as the local path
1739 if (!memcmp(esi
, tmp_esi
, sizeof(esi_t
))) {
1740 tmp_mm_seq
= mac_mobility_seqnum(second_best_path
->attr
);
1741 if (tmp_mm_seq
< loc_seq
)
1744 /* we have a non-proxy path from the ES peer. */
1745 if (second_best_path
->attr
->es_flags
&
1746 ATTR_ES_PROXY_ADVERT
) {
1747 *proxy_from_peer
= true;
1749 *active_on_peer
= true;
1752 if (second_best_path
->attr
->router_flag
)
1753 *peer_router
= true;
1755 /* we use both proxy and non-proxy imports to
1756 * determine the max sync sequence
1758 if (tmp_mm_seq
> *max_sync_seq
)
1759 *max_sync_seq
= tmp_mm_seq
;
1763 /* Bubble up sync-info from all paths (non-best) to the local-path.
1764 * This is need for MM sequence number syncing and proxy advertisement.
1765 * Note: The local path can only exist as a best path in the
1766 * VPN route table. It will take precedence over all sync paths.
1768 static void update_evpn_route_entry_sync_info(struct bgp
*bgp
,
1769 struct bgp_dest
*dest
,
1771 uint32_t loc_seq
, bool setup_sync
,
1772 const struct ethaddr
*mac
)
1775 struct prefix_evpn
*evp
=
1776 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1778 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1781 esi
= bgp_evpn_attr_get_esi(attr
);
1782 if (bgp_evpn_is_esi_valid(esi
)) {
1784 uint32_t max_sync_seq
= 0;
1785 bool active_on_peer
= false;
1786 bool peer_router
= false;
1787 bool proxy_from_peer
= false;
1789 bgp_evpn_get_sync_info(bgp
, esi
, dest
, loc_seq
,
1790 &max_sync_seq
, &active_on_peer
,
1791 &peer_router
, &proxy_from_peer
,
1793 attr
->mm_sync_seqnum
= max_sync_seq
;
1795 attr
->es_flags
|= ATTR_ES_PEER_ACTIVE
;
1797 attr
->es_flags
&= ~ATTR_ES_PEER_ACTIVE
;
1798 if (proxy_from_peer
)
1799 attr
->es_flags
|= ATTR_ES_PEER_PROXY
;
1801 attr
->es_flags
&= ~ATTR_ES_PEER_PROXY
;
1803 attr
->es_flags
|= ATTR_ES_PEER_ROUTER
;
1805 attr
->es_flags
&= ~ATTR_ES_PEER_ROUTER
;
1807 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
1808 char esi_buf
[ESI_STR_LEN
];
1811 "setup sync info for %pFX es %s max_seq %d %s%s%s",
1813 esi_to_str(esi
, esi_buf
,
1816 (attr
->es_flags
& ATTR_ES_PEER_ACTIVE
)
1819 (attr
->es_flags
& ATTR_ES_PEER_PROXY
)
1822 (attr
->es_flags
& ATTR_ES_PEER_ROUTER
)
1828 attr
->mm_sync_seqnum
= 0;
1829 attr
->es_flags
&= ~ATTR_ES_PEER_ACTIVE
;
1830 attr
->es_flags
&= ~ATTR_ES_PEER_PROXY
;
1835 * Create or update EVPN route entry. This could be in the VNI route tables
1836 * or the global route table.
1838 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1839 afi_t afi
, safi_t safi
,
1840 struct bgp_dest
*dest
, struct attr
*attr
,
1841 const struct ethaddr
*mac
,
1842 const struct ipaddr
*ip
, int add
,
1843 struct bgp_path_info
**pi
, uint8_t flags
,
1844 uint32_t seq
, bool vpn_rt
, bool *old_is_sync
)
1846 struct bgp_path_info
*tmp_pi
;
1847 struct bgp_path_info
*local_pi
;
1848 struct attr
*attr_new
;
1849 mpls_label_t label
[BGP_MAX_LABELS
];
1850 uint32_t num_labels
= 1;
1851 int route_change
= 1;
1853 const struct prefix_evpn
*evp
;
1856 evp
= (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1857 memset(&label
, 0, sizeof(label
));
1859 /* See if this is an update of an existing route, or a new add. */
1860 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
1862 /* If route doesn't exist already, create a new one, if told to.
1863 * Otherwise act based on whether the attributes of the route have
1866 if (!local_pi
&& !add
)
1869 if (old_is_sync
&& local_pi
)
1870 *old_is_sync
= bgp_evpn_attr_is_sync(local_pi
->attr
);
1872 /* if a local path is being added with a non-zero esi look
1873 * for SYNC paths from ES peers and bubble up the sync-info
1875 update_evpn_route_entry_sync_info(bgp
, dest
, attr
, seq
, vpn_rt
, mac
);
1877 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1878 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1879 add_mac_mobility_to_attr(seq
, attr
);
1882 /* Add (or update) attribute to hash. */
1883 attr_new
= bgp_attr_intern(attr
);
1885 /* Extract MAC mobility sequence number, if any. */
1886 attr_new
->mm_seqnum
=
1887 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1888 attr_new
->sticky
= sticky
;
1890 /* Create new route with its attribute. */
1891 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1892 bgp
->peer_self
, attr_new
, dest
);
1893 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1894 bgp_path_info_extra_get(tmp_pi
);
1896 /* The VNI goes into the 'label' field of the route */
1897 vni2label(vpn
->vni
, &label
[0]);
1899 /* Type-2 routes may carry a second VNI - the L3-VNI.
1900 * Only attach second label if we are advertising two labels for
1903 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1904 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1907 l3vni
= bgpevpn_get_l3vni(vpn
);
1909 vni2label(l3vni
, &label
[1]);
1914 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1915 tmp_pi
->extra
->num_labels
= num_labels
;
1917 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
1919 evpn_type2_path_info_set_mac(tmp_pi
, *mac
);
1921 evpn_type2_path_info_set_ip(tmp_pi
, *ip
);
1924 /* Mark route as self type-2 route */
1925 if (flags
&& CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1926 tmp_pi
->extra
->af_flags
= BGP_EVPN_MACIP_TYPE_SVI_IP
;
1927 bgp_path_info_add(dest
, tmp_pi
);
1930 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1931 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1935 * The attributes have changed, type-2 routes needs to
1936 * be advertised with right labels.
1938 vni2label(vpn
->vni
, &label
[0]);
1939 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1940 && CHECK_FLAG(vpn
->flags
,
1941 VNI_FLAG_USE_TWO_LABELS
)) {
1944 l3vni
= bgpevpn_get_l3vni(vpn
);
1946 vni2label(l3vni
, &label
[1]);
1950 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1951 tmp_pi
->extra
->num_labels
= num_labels
;
1953 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
1955 evpn_type2_path_info_set_mac(tmp_pi
,
1958 evpn_type2_path_info_set_ip(tmp_pi
,
1962 /* The attribute has changed. */
1963 /* Add (or update) attribute to hash. */
1964 attr_new
= bgp_attr_intern(attr
);
1965 bgp_path_info_set_flag(dest
, tmp_pi
,
1966 BGP_PATH_ATTR_CHANGED
);
1968 /* Extract MAC mobility sequence number, if any. */
1969 attr_new
->mm_seqnum
=
1970 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1971 attr_new
->sticky
= sticky
;
1973 /* Restore route, if needed. */
1974 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1975 bgp_path_info_restore(dest
, tmp_pi
);
1977 /* Unintern existing, set to new. */
1978 bgp_attr_unintern(&tmp_pi
->attr
);
1979 tmp_pi
->attr
= attr_new
;
1980 tmp_pi
->uptime
= monotime(NULL
);
1984 /* local MAC-IP routes in the VNI table are linked to
1985 * the destination ES
1987 if (route_change
&& vpn_rt
1988 && (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
))
1989 bgp_evpn_path_es_link(tmp_pi
, vpn
->vni
,
1990 bgp_evpn_attr_get_esi(tmp_pi
->attr
));
1992 /* Return back the route entry. */
1994 return route_change
;
1997 static void evpn_zebra_reinstall_best_route(struct bgp
*bgp
,
1998 struct bgpevpn
*vpn
,
1999 struct bgp_dest
*dest
)
2001 struct bgp_path_info
*tmp_ri
;
2002 struct bgp_path_info
*curr_select
= NULL
;
2004 for (tmp_ri
= bgp_dest_get_bgp_path_info(dest
); tmp_ri
;
2005 tmp_ri
= tmp_ri
->next
) {
2006 if (CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_SELECTED
)) {
2007 curr_select
= tmp_ri
;
2012 if (curr_select
&& curr_select
->type
== ZEBRA_ROUTE_BGP
2013 && (curr_select
->sub_type
== BGP_ROUTE_IMPORTED
||
2014 bgp_evpn_attr_is_sync(curr_select
->attr
)))
2015 evpn_zebra_install(bgp
, vpn
,
2016 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
2021 * If the local route was not selected evict it and tell zebra to re-add
2022 * the best remote dest.
2024 * Typically a local path added by zebra is expected to be selected as
2025 * best. In which case when a remote path wins as best (later)
2026 * evpn_route_select_install itself evicts the older-local-best path.
2028 * However if bgp's add and zebra's add cross paths (race condition) it
2029 * is possible that the local path is no longer the "older" best path.
2030 * It is a path that was never designated as best and hence requires
2031 * additional handling to prevent bgp from injecting and holding on to a
2032 * non-best local path.
2034 static void evpn_cleanup_local_non_best_route(struct bgp
*bgp
,
2035 struct bgpevpn
*vpn
,
2036 struct bgp_dest
*dest
,
2037 struct bgp_path_info
*local_pi
)
2039 /* local path was not picked as the winner; kick it out */
2040 if (bgp_debug_zebra(NULL
))
2041 zlog_debug("evicting local evpn prefix %pBD as remote won",
2044 evpn_delete_old_local_route(bgp
, vpn
, dest
, local_pi
, NULL
);
2045 bgp_path_info_reap(dest
, local_pi
);
2047 /* tell zebra to re-add the best remote path */
2048 evpn_zebra_reinstall_best_route(bgp
, vpn
, dest
);
2051 static inline bool bgp_evpn_route_add_l3_ecomm_ok(struct bgpevpn
*vpn
,
2052 const struct prefix_evpn
*p
,
2055 return p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2056 && (is_evpn_prefix_ipaddr_v4(p
)
2057 || (is_evpn_prefix_ipaddr_v6(p
)
2058 && !IN6_IS_ADDR_LINKLOCAL(
2059 &p
->prefix
.macip_addr
.ip
.ipaddr_v6
)))
2060 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)
2061 && bgpevpn_get_l3vni(vpn
) && bgp_evpn_es_add_l3_ecomm_ok(esi
);
2065 * Create or update EVPN route (of type based on prefix) for specified VNI
2066 * and schedule for processing.
2068 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2069 struct prefix_evpn
*p
, uint8_t flags
,
2070 uint32_t seq
, esi_t
*esi
)
2072 struct bgp_dest
*dest
;
2074 struct attr
*attr_new
;
2075 int add_l3_ecomm
= 0;
2076 struct bgp_path_info
*pi
;
2077 afi_t afi
= AFI_L2VPN
;
2078 safi_t safi
= SAFI_EVPN
;
2080 bool old_is_sync
= false;
2081 bool mac_only
= false;
2083 memset(&attr
, 0, sizeof(attr
));
2085 /* Build path-attribute for this route. */
2086 bgp_attr_default_set(&attr
, bgp
, BGP_ORIGIN_IGP
);
2087 attr
.nexthop
= vpn
->originator_ip
;
2088 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
2089 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
2090 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
2091 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
2092 attr
.router_flag
= CHECK_FLAG(flags
,
2093 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
2094 if (CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_PROXY_ADVERT
))
2095 attr
.es_flags
|= ATTR_ES_PROXY_ADVERT
;
2097 if (esi
&& bgp_evpn_is_esi_valid(esi
)) {
2098 memcpy(&attr
.esi
, esi
, sizeof(esi_t
));
2099 attr
.es_flags
|= ATTR_ES_IS_LOCAL
;
2102 /* PMSI is only needed for type-3 routes */
2103 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
2104 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
2105 bgp_attr_set_pmsi_tnl_type(&attr
, PMSI_TNLTYPE_INGR_REPL
);
2108 /* router mac is only needed for type-2 routes here. */
2109 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
2110 uint8_t af_flags
= 0;
2112 if (CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
2113 SET_FLAG(af_flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
);
2115 bgp_evpn_get_rmac_nexthop(vpn
, p
, &attr
, af_flags
);
2118 if (bgp_debug_zebra(NULL
)) {
2119 char buf3
[ESI_STR_LEN
];
2122 "VRF %s vni %u type-%u route evp %pFX RMAC %pEA nexthop %pI4 esi %s",
2123 vpn
->bgp_vrf
? vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
)
2125 vpn
->vni
, p
->prefix
.route_type
, p
, &attr
.rmac
,
2126 &attr
.mp_nexthop_global_in
,
2127 esi_to_str(esi
, buf3
, sizeof(buf3
)));
2130 vni2label(vpn
->vni
, &(attr
.label
));
2132 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
2133 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
2136 add_l3_ecomm
= bgp_evpn_route_add_l3_ecomm_ok(
2137 vpn
, p
, (attr
.es_flags
& ATTR_ES_IS_LOCAL
) ? &attr
.esi
: NULL
);
2139 /* Set up extended community. */
2140 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2142 /* First, create (or fetch) route node within the VNI.
2143 * NOTE: There is no RD here.
2145 dest
= bgp_evpn_vni_node_get(vpn
, p
, NULL
);
2147 if ((p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) &&
2148 (is_evpn_prefix_ipaddr_none(p
) == true))
2151 /* Create or update route entry. */
2152 route_change
= update_evpn_route_entry(
2153 bgp
, vpn
, afi
, safi
, dest
, &attr
,
2154 (mac_only
? NULL
: &p
->prefix
.macip_addr
.mac
), NULL
/* ip */, 1,
2155 &pi
, flags
, seq
, true /* setup_sync */, &old_is_sync
);
2157 attr_new
= pi
->attr
;
2159 /* lock ri to prevent freeing in evpn_route_select_install */
2160 bgp_path_info_lock(pi
);
2162 /* Perform route selection. Normally, the local route in the
2163 * VNI is expected to win and be the best route. However, if
2164 * there is a race condition where a host moved from local to
2165 * remote and the remote route was received in BGP just prior
2166 * to the local MACIP notification from zebra, the remote
2167 * route would win, and we should evict the defunct local route
2168 * and (re)install the remote route into zebra.
2170 evpn_route_select_install(bgp
, vpn
, dest
);
2172 * If the new local route was not selected evict it and tell zebra
2173 * to re-add the best remote dest. BGP doesn't retain non-best local
2176 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2179 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
2181 evpn_cleanup_local_non_best_route(bgp
, vpn
, dest
, pi
);
2185 /* If the local path already existed and is still the
2186 * best path we need to also check if it transitioned
2187 * from being a sync path to a non-sync path. If it
2188 * it did we need to notify zebra that the sync-path
2191 new_is_sync
= bgp_evpn_attr_is_sync(pi
->attr
);
2192 if (!new_is_sync
&& old_is_sync
)
2193 evpn_zebra_uninstall(bgp
, vpn
, p
, pi
, true);
2196 bgp_path_info_unlock(pi
);
2198 bgp_dest_unlock_node(dest
);
2200 /* If this is a new route or some attribute has changed, export the
2201 * route to the global table. The route will be advertised to peers
2202 * from there. Note that this table is a 2-level tree (RD-level +
2203 * Prefix-level) similar to L3VPN routes.
2206 struct bgp_path_info
*global_pi
;
2208 dest
= bgp_evpn_global_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2209 p
, &vpn
->prd
, NULL
);
2210 update_evpn_route_entry(
2211 bgp
, vpn
, afi
, safi
, dest
, attr_new
, NULL
/* mac */,
2212 NULL
/* ip */, 1, &global_pi
, flags
, seq
,
2213 false /* setup_sync */, NULL
/* old_is_sync */);
2215 /* Schedule for processing and unlock node. */
2216 bgp_process(bgp
, dest
, afi
, safi
);
2217 bgp_dest_unlock_node(dest
);
2220 /* Unintern temporary. */
2221 aspath_unintern(&attr
.aspath
);
2227 * Delete EVPN route entry.
2228 * The entry can be in ESI/VNI table or the global table.
2230 void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
2231 struct bgp_dest
*dest
,
2232 struct bgp_path_info
**pi
)
2234 struct bgp_path_info
*tmp_pi
;
2238 /* Now, find matching route. */
2239 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
2240 tmp_pi
= tmp_pi
->next
)
2241 if (tmp_pi
->peer
== bgp
->peer_self
2242 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2243 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2248 /* Mark route for delete. */
2250 bgp_path_info_delete(dest
, tmp_pi
);
2253 /* Delete EVPN type5 route */
2254 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
2256 afi_t afi
= AFI_L2VPN
;
2257 safi_t safi
= SAFI_EVPN
;
2258 struct bgp_dest
*dest
= NULL
;
2259 struct bgp_path_info
*pi
= NULL
;
2260 struct bgp
*bgp_evpn
= NULL
; /* evpn bgp instance */
2262 bgp_evpn
= bgp_get_evpn();
2266 /* locate the global route entry for this type-5 prefix */
2267 dest
= bgp_evpn_global_node_lookup(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
2268 evp
, &bgp_vrf
->vrf_prd
, NULL
);
2272 delete_evpn_route_entry(bgp_evpn
, afi
, safi
, dest
, &pi
);
2274 bgp_process(bgp_evpn
, dest
, afi
, safi
);
2275 bgp_dest_unlock_node(dest
);
2280 * Delete EVPN route (of type based on prefix) for specified VNI and
2281 * schedule for processing.
2283 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2284 struct prefix_evpn
*p
)
2286 struct bgp_dest
*dest
, *global_dest
;
2287 struct bgp_path_info
*pi
;
2288 afi_t afi
= AFI_L2VPN
;
2289 safi_t safi
= SAFI_EVPN
;
2291 /* First, locate the route node within the VNI. If it doesn't exist,
2293 * is nothing further to do.
2294 * NOTE: There is no RD here.
2296 dest
= bgp_evpn_vni_node_lookup(vpn
, p
, NULL
);
2300 /* Next, locate route node in the global EVPN routing table. Note that
2301 * this table is a 2-level tree (RD-level + Prefix-level) similar to
2304 global_dest
= bgp_evpn_global_node_lookup(bgp
->rib
[afi
][safi
], afi
,
2305 safi
, p
, &vpn
->prd
, NULL
);
2307 /* Delete route entry in the global EVPN table. */
2308 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
2310 /* Schedule for processing - withdraws to peers happen from
2314 bgp_process(bgp
, global_dest
, afi
, safi
);
2315 bgp_dest_unlock_node(global_dest
);
2318 /* Delete route entry in the VNI route table. This can just be removed.
2320 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2322 bgp_path_info_reap(dest
, pi
);
2323 evpn_route_select_install(bgp
, vpn
, dest
);
2325 bgp_dest_unlock_node(dest
);
2330 void bgp_evpn_update_type2_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2331 struct bgp_dest
*dest
,
2332 struct bgp_path_info
*local_pi
,
2335 afi_t afi
= AFI_L2VPN
;
2336 safi_t safi
= SAFI_EVPN
;
2337 struct bgp_path_info
*pi
;
2339 struct attr
*attr_new
;
2341 int add_l3_ecomm
= 0;
2342 struct bgp_dest
*global_dest
;
2343 struct bgp_path_info
*global_pi
;
2344 struct prefix_evpn evp
;
2346 bool old_is_sync
= false;
2348 if (CHECK_FLAG(local_pi
->flags
, BGP_PATH_REMOVED
))
2352 * VNI table MAC-IP prefixes don't have MAC so make sure it's set from
2355 if (is_evpn_prefix_ipaddr_none((struct prefix_evpn
*)&dest
->p
)) {
2356 /* VNI MAC -> Global */
2357 evpn_type2_prefix_global_copy(
2358 &evp
, (struct prefix_evpn
*)&dest
->p
, NULL
/* mac */,
2359 evpn_type2_path_info_get_ip(local_pi
));
2361 /* VNI IP -> Global */
2362 evpn_type2_prefix_global_copy(
2363 &evp
, (struct prefix_evpn
*)&dest
->p
,
2364 evpn_type2_path_info_get_mac(local_pi
), NULL
/* ip */);
2368 * Build attribute per local route as the MAC mobility and
2369 * some other values could differ for different routes. The
2370 * attributes will be shared in the hash table.
2372 bgp_attr_default_set(&attr
, bgp
, BGP_ORIGIN_IGP
);
2373 attr
.nexthop
= vpn
->originator_ip
;
2374 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
2375 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
2376 attr
.sticky
= (local_pi
->attr
->sticky
) ? 1 : 0;
2377 attr
.router_flag
= (local_pi
->attr
->router_flag
) ? 1 : 0;
2378 attr
.es_flags
= local_pi
->attr
->es_flags
;
2379 if (local_pi
->attr
->default_gw
) {
2380 attr
.default_gw
= 1;
2381 if (is_evpn_prefix_ipaddr_v6(&evp
))
2382 attr
.router_flag
= 1;
2384 memcpy(&attr
.esi
, &local_pi
->attr
->esi
, sizeof(esi_t
));
2385 bgp_evpn_get_rmac_nexthop(vpn
, &evp
, &attr
, local_pi
->extra
->af_flags
);
2386 vni2label(vpn
->vni
, &(attr
.label
));
2387 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2388 * using L3 VNI for type-2 routes also.
2390 add_l3_ecomm
= bgp_evpn_route_add_l3_ecomm_ok(
2392 (attr
.es_flags
& ATTR_ES_IS_LOCAL
) ? &attr
.esi
: NULL
);
2394 /* Set up extended community. */
2395 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2396 seq
= mac_mobility_seqnum(local_pi
->attr
);
2398 if (bgp_debug_zebra(NULL
)) {
2399 char buf3
[ESI_STR_LEN
];
2402 "VRF %s vni %u evp %pFX RMAC %pEA nexthop %pI4 esi %s esf 0x%x from %s",
2403 vpn
->bgp_vrf
? vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
)
2405 vpn
->vni
, &evp
, &attr
.rmac
, &attr
.mp_nexthop_global_in
,
2406 esi_to_str(&attr
.esi
, buf3
, sizeof(buf3
)),
2407 attr
.es_flags
, caller
);
2410 /* Update the route entry. */
2411 route_change
= update_evpn_route_entry(
2412 bgp
, vpn
, afi
, safi
, dest
, &attr
, NULL
/* mac */, NULL
/* ip */,
2413 0, &pi
, 0, seq
, true /* setup_sync */, &old_is_sync
);
2416 attr_new
= pi
->attr
;
2417 /* lock ri to prevent freeing in evpn_route_select_install */
2418 bgp_path_info_lock(pi
);
2420 /* Perform route selection. Normally, the local route in the
2421 * VNI is expected to win and be the best route. However,
2422 * under peculiar situations (e.g., tunnel (next hop) IP change
2423 * that causes best selection to be based on next hop), a
2424 * remote route could win. If the local route is the best,
2425 * ensure it is updated in the global EVPN route table and
2426 * advertised to peers; otherwise, ensure it is evicted and
2427 * (re)install the remote route into zebra.
2429 evpn_route_select_install(bgp
, vpn
, dest
);
2431 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2434 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
2436 evpn_cleanup_local_non_best_route(bgp
, vpn
, dest
, pi
);
2440 /* If the local path already existed and is still the
2441 * best path we need to also check if it transitioned
2442 * from being a sync path to a non-sync path. If it
2443 * it did we need to notify zebra that the sync-path
2446 new_is_sync
= bgp_evpn_attr_is_sync(pi
->attr
);
2447 if (!new_is_sync
&& old_is_sync
)
2448 evpn_zebra_uninstall(bgp
, vpn
, &evp
, pi
, true);
2454 bgp_path_info_unlock(pi
);
2457 /* Update route in global routing table. */
2458 global_dest
= bgp_evpn_global_node_get(
2459 bgp
->rib
[afi
][safi
], afi
, safi
, &evp
, &vpn
->prd
, NULL
);
2460 assert(global_dest
);
2461 update_evpn_route_entry(
2462 bgp
, vpn
, afi
, safi
, global_dest
, attr_new
,
2463 NULL
/* mac */, NULL
/* ip */, 0, &global_pi
, 0,
2464 mac_mobility_seqnum(attr_new
), false /* setup_sync */,
2465 NULL
/* old_is_sync */);
2467 /* Schedule for processing and unlock node. */
2468 bgp_process(bgp
, global_dest
, afi
, safi
);
2469 bgp_dest_unlock_node(global_dest
);
2472 /* Unintern temporary. */
2473 aspath_unintern(&attr
.aspath
);
2476 static void update_type2_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2477 struct bgp_dest
*dest
)
2479 struct bgp_path_info
*tmp_pi
;
2481 const struct prefix_evpn
*evp
=
2482 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
2484 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2487 /* Identify local route. */
2488 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
2489 tmp_pi
= tmp_pi
->next
) {
2490 if (tmp_pi
->peer
== bgp
->peer_self
&&
2491 tmp_pi
->type
== ZEBRA_ROUTE_BGP
&&
2492 tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2499 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, tmp_pi
, __func__
);
2503 * Update all type-2 (MACIP) local routes for this VNI - these should also
2504 * be scheduled for advertise to peers.
2506 static void update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2508 struct bgp_dest
*dest
;
2510 /* Walk this VNI's route MAC & IP table and update local type-2
2511 * routes. For any routes updated, update corresponding entry in the
2514 for (dest
= bgp_table_top(vpn
->mac_table
); dest
;
2515 dest
= bgp_route_next(dest
))
2516 update_type2_route(bgp
, vpn
, dest
);
2518 for (dest
= bgp_table_top(vpn
->ip_table
); dest
;
2519 dest
= bgp_route_next(dest
))
2520 update_type2_route(bgp
, vpn
, dest
);
2524 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2525 * global routing table. These are also scheduled for withdraw from peers.
2527 static void delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2531 struct bgp_dest
*rddest
, *dest
;
2532 struct bgp_table
*table
;
2533 struct bgp_path_info
*pi
;
2538 rddest
= bgp_node_lookup(bgp
->rib
[afi
][safi
],
2539 (struct prefix
*)&vpn
->prd
);
2541 table
= bgp_dest_get_bgp_table_info(rddest
);
2542 for (dest
= bgp_table_top(table
); dest
;
2543 dest
= bgp_route_next(dest
)) {
2544 const struct prefix_evpn
*evp
=
2545 (const struct prefix_evpn
*)bgp_dest_get_prefix(
2548 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2551 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2553 bgp_process(bgp
, dest
, afi
, safi
);
2556 /* Unlock RD node. */
2557 bgp_dest_unlock_node(rddest
);
2561 static void delete_vni_type2_route(struct bgp
*bgp
, struct bgp_dest
*dest
)
2563 struct bgp_path_info
*pi
;
2564 afi_t afi
= AFI_L2VPN
;
2565 safi_t safi
= SAFI_EVPN
;
2567 const struct prefix_evpn
*evp
=
2568 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
2570 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2573 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2575 /* Route entry in local table gets deleted immediately. */
2577 bgp_path_info_reap(dest
, pi
);
2580 static void delete_vni_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2582 struct bgp_dest
*dest
;
2584 /* Next, walk this VNI's MAC & IP route table and delete local type-2
2587 for (dest
= bgp_table_top(vpn
->mac_table
); dest
;
2588 dest
= bgp_route_next(dest
))
2589 delete_vni_type2_route(bgp
, dest
);
2591 for (dest
= bgp_table_top(vpn
->ip_table
); dest
;
2592 dest
= bgp_route_next(dest
))
2593 delete_vni_type2_route(bgp
, dest
);
2597 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2598 * table as well as the per-VNI route table.
2600 static void delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2602 /* First, walk the global route table for this VNI's type-2 local
2604 * EVPN routes are a 2-level table, first get the RD table.
2606 delete_global_type2_routes(bgp
, vpn
);
2607 delete_vni_type2_routes(bgp
, vpn
);
2611 * Delete all routes in the per-VNI route table.
2613 static void delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2615 struct bgp_dest
*dest
;
2616 struct bgp_path_info
*pi
, *nextpi
;
2618 /* Walk this VNI's MAC & IP route table and delete all routes. */
2619 for (dest
= bgp_table_top(vpn
->mac_table
); dest
;
2620 dest
= bgp_route_next(dest
)) {
2621 for (pi
= bgp_dest_get_bgp_path_info(dest
);
2622 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2623 bgp_evpn_remote_ip_hash_del(vpn
, pi
);
2624 bgp_path_info_delete(dest
, pi
);
2625 bgp_path_info_reap(dest
, pi
);
2629 for (dest
= bgp_table_top(vpn
->ip_table
); dest
;
2630 dest
= bgp_route_next(dest
)) {
2631 for (pi
= bgp_dest_get_bgp_path_info(dest
);
2632 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2633 bgp_path_info_delete(dest
, pi
);
2634 bgp_path_info_reap(dest
, pi
);
2639 /* BUM traffic flood mode per-l2-vni */
2640 static int bgp_evpn_vni_flood_mode_get(struct bgp
*bgp
,
2641 struct bgpevpn
*vpn
)
2643 /* if flooding has been globally disabled per-vni mode is
2646 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
2647 return VXLAN_FLOOD_DISABLED
;
2649 /* if mcast group ip has been specified we use a PIM-SM MDT */
2650 if (vpn
->mcast_grp
.s_addr
!= INADDR_ANY
)
2651 return VXLAN_FLOOD_PIM_SM
;
2653 /* default is ingress replication */
2654 return VXLAN_FLOOD_HEAD_END_REPL
;
2658 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2659 * export RT getting modified or change to tunnel IP. Note that these
2660 * situations need the route in the per-VNI table as well as the global
2661 * table to be updated (as attributes change).
2663 int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2666 struct prefix_evpn p
;
2668 update_type1_routes_for_evi(bgp
, vpn
);
2670 /* Update and advertise the type-3 route (only one) followed by the
2671 * locally learnt type-2 routes (MACIP) - for this VNI.
2673 * RT-3 only if doing head-end replication
2675 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
2676 == VXLAN_FLOOD_HEAD_END_REPL
) {
2677 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2678 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
);
2683 update_all_type2_routes(bgp
, vpn
);
2688 * Delete (and withdraw) local routes for specified VNI from the global
2689 * table and per-VNI table. After this, remove all other routes from
2690 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2691 * (advertise-all-vni) being disabled.
2693 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2696 struct prefix_evpn p
;
2698 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2699 * followed by type-3 routes (only one) - for this VNI.
2701 delete_all_type2_routes(bgp
, vpn
);
2703 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2704 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2708 /* Delete all routes from the per-VNI table. */
2709 delete_all_vni_routes(bgp
, vpn
);
2714 * There is a flood mcast IP address change. Update the mcast-grp and
2715 * remove the type-3 route if any. A new type-3 route will be generated
2716 * post tunnel_ip update if the new flood mode is head-end-replication.
2718 static int bgp_evpn_mcast_grp_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2719 struct in_addr mcast_grp
)
2721 struct prefix_evpn p
;
2723 vpn
->mcast_grp
= mcast_grp
;
2725 if (is_vni_live(vpn
)) {
2726 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2727 delete_evpn_route(bgp
, vpn
, &p
);
2734 * There is a tunnel endpoint IP address change for this VNI, delete
2735 * prior type-3 route (if needed) and update.
2736 * Note: Route re-advertisement happens elsewhere after other processing
2739 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2740 struct in_addr originator_ip
)
2742 struct prefix_evpn p
;
2744 /* If VNI is not live, we only need to update the originator ip */
2745 if (!is_vni_live(vpn
)) {
2746 vpn
->originator_ip
= originator_ip
;
2750 /* Update the tunnel-ip hash */
2751 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2752 bgp_tip_add(bgp
, &originator_ip
);
2754 /* filter routes as martian nexthop db has changed */
2755 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2757 /* Need to withdraw type-3 route as the originator IP is part
2760 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2761 delete_evpn_route(bgp
, vpn
, &p
);
2763 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2764 vpn
->originator_ip
= originator_ip
;
2768 static struct bgp_path_info
*
2769 bgp_create_evpn_bgp_path_info(struct bgp_path_info
*parent_pi
,
2770 struct bgp_dest
*dest
, struct attr
*attr
)
2772 struct attr
*attr_new
;
2773 struct bgp_path_info
*pi
;
2775 /* Add (or update) attribute to hash. */
2776 attr_new
= bgp_attr_intern(attr
);
2778 /* Create new route with its attribute. */
2779 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0, parent_pi
->peer
,
2781 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2782 bgp_path_info_extra_get(pi
);
2783 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2784 bgp_dest_lock_node((struct bgp_dest
*)parent_pi
->net
);
2785 if (parent_pi
->extra
) {
2786 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2787 sizeof(pi
->extra
->label
));
2788 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2789 pi
->extra
->igpmetric
= parent_pi
->extra
->igpmetric
;
2792 bgp_path_info_add(dest
, pi
);
2798 * Install route entry into the VRF routing table and invoke route selection.
2800 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2801 const struct prefix_evpn
*evp
,
2802 struct bgp_path_info
*parent_pi
)
2804 struct bgp_dest
*dest
;
2805 struct bgp_path_info
*pi
;
2807 struct attr
*attr_new
;
2810 struct prefix
*pp
= &p
;
2813 bool new_pi
= false;
2814 bool use_l3nhg
= false;
2815 bool is_l3nhg_active
= false;
2816 char buf1
[INET6_ADDRSTRLEN
];
2818 memset(pp
, 0, sizeof(struct prefix
));
2819 ip_prefix_from_evpn_prefix(evp
, pp
);
2821 if (bgp_debug_zebra(NULL
))
2823 "vrf %s: import evpn prefix %pFX parent %p flags 0x%x",
2824 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
, parent_pi
,
2827 /* Create (or fetch) route within the VRF. */
2828 /* NOTE: There is no RD here. */
2829 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2831 safi
= SAFI_UNICAST
;
2832 dest
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2833 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2835 safi
= SAFI_UNICAST
;
2836 dest
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2840 /* EVPN routes currently only support a IPv4 next hop which corresponds
2841 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2842 * or prefix route, we have to convert the next hop to an IPv4-mapped
2843 * address for the rest of the code to flow through. In the case of IPv4,
2844 * make sure to set the flag for next hop attribute.
2846 attr
= *parent_pi
->attr
;
2847 if (attr
.evpn_overlay
.type
!= OVERLAY_INDEX_GATEWAY_IP
) {
2849 evpn_convert_nexthop_to_ipv6(&attr
);
2851 attr
.nexthop
= attr
.mp_nexthop_global_in
;
2852 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2857 * If gateway IP overlay index is specified in the NLRI of
2858 * EVPN RT-5, this gateway IP should be used as the nexthop
2859 * for the prefix in the VRF
2861 if (bgp_debug_zebra(NULL
)) {
2863 "Install gateway IP %s as nexthop for prefix %pFX in vrf %s",
2864 inet_ntop(pp
->family
, &attr
.evpn_overlay
.gw_ip
,
2865 buf1
, sizeof(buf1
)), pp
,
2866 vrf_id_to_name(bgp_vrf
->vrf_id
));
2869 if (afi
== AFI_IP6
) {
2870 memcpy(&attr
.mp_nexthop_global
,
2871 &attr
.evpn_overlay
.gw_ip
.ipaddr_v6
,
2872 sizeof(struct in6_addr
));
2873 attr
.mp_nexthop_len
= IPV6_MAX_BYTELEN
;
2875 attr
.nexthop
= attr
.evpn_overlay
.gw_ip
.ipaddr_v4
;
2876 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2880 bgp_evpn_es_vrf_use_nhg(bgp_vrf
, &parent_pi
->attr
->esi
, &use_l3nhg
,
2881 &is_l3nhg_active
, NULL
);
2883 attr
.es_flags
|= ATTR_ES_L3_NHG_USE
;
2884 if (is_l3nhg_active
)
2885 attr
.es_flags
|= ATTR_ES_L3_NHG_ACTIVE
;
2887 /* Check if route entry is already present. */
2888 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2890 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2894 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, dest
, &attr
);
2897 if (attrhash_cmp(pi
->attr
, &attr
)
2898 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2899 bgp_dest_unlock_node(dest
);
2902 /* The attribute has changed. */
2903 /* Add (or update) attribute to hash. */
2904 attr_new
= bgp_attr_intern(&attr
);
2906 /* Restore route, if needed. */
2907 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2908 bgp_path_info_restore(dest
, pi
);
2910 /* Mark if nexthop has changed. */
2912 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2914 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2915 &attr_new
->mp_nexthop_global
)))
2916 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2918 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ATTR_CHANGED
);
2919 /* Unintern existing, set to new. */
2920 bgp_attr_unintern(&pi
->attr
);
2921 pi
->attr
= attr_new
;
2922 pi
->uptime
= monotime(NULL
);
2925 /* Gateway IP nexthop should be resolved */
2926 if (attr
.evpn_overlay
.type
== OVERLAY_INDEX_GATEWAY_IP
) {
2927 if (bgp_find_or_add_nexthop(bgp_vrf
, bgp_vrf
, afi
, safi
, pi
,
2929 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_VALID
);
2931 if (BGP_DEBUG(nht
, NHT
)) {
2932 inet_ntop(pp
->family
,
2933 &attr
.evpn_overlay
.gw_ip
,
2934 buf1
, sizeof(buf1
));
2935 zlog_debug("%s: gateway IP NH unresolved",
2938 bgp_path_info_unset_flag(dest
, pi
, BGP_PATH_VALID
);
2942 /* as it is an importation, change nexthop */
2943 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ANNC_NH_SELF
);
2946 /* Link path to evpn nexthop */
2947 bgp_evpn_path_nh_add(bgp_vrf
, pi
);
2949 bgp_aggregate_increment(bgp_vrf
, bgp_dest_get_prefix(dest
), pi
, afi
,
2952 /* Perform route selection and update zebra, if required. */
2953 bgp_process(bgp_vrf
, dest
, afi
, safi
);
2955 /* Process for route leaking. */
2956 vpn_leak_from_vrf_update(bgp_get_default(), bgp_vrf
, pi
);
2958 bgp_dest_unlock_node(dest
);
2960 if (bgp_debug_zebra(NULL
))
2961 zlog_debug("... %s pi dest %p (l %d) pi %p (l %d, f 0x%x)",
2962 new_pi
? "new" : "update", dest
,
2963 bgp_dest_get_lock_count(dest
), pi
, pi
->lock
,
2970 * Common handling for vni route tables install/selection.
2972 static int install_evpn_route_entry_in_vni_common(
2973 struct bgp
*bgp
, struct bgpevpn
*vpn
, const struct prefix_evpn
*p
,
2974 struct bgp_dest
*dest
, struct bgp_path_info
*parent_pi
)
2976 struct bgp_path_info
*pi
;
2977 struct bgp_path_info
*local_pi
;
2978 struct attr
*attr_new
;
2980 bool old_local_es
= false;
2983 /* Check if route entry is already present. */
2984 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2986 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2990 /* Create an info */
2991 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, dest
,
2994 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
2995 if (is_evpn_type2_dest_ipaddr_none(dest
))
2996 evpn_type2_path_info_set_ip(
2997 pi
, p
->prefix
.macip_addr
.ip
);
2999 evpn_type2_path_info_set_mac(
3000 pi
, p
->prefix
.macip_addr
.mac
);
3003 new_local_es
= bgp_evpn_attr_is_local_es(pi
->attr
);
3005 /* Return early if attributes haven't changed
3006 * and dest isn't flagged for removal.
3007 * dest will be unlocked by either
3008 * install_evpn_route_entry_in_vni_mac() or
3009 * install_evpn_route_entry_in_vni_ip()
3011 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
) &&
3012 !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
3014 /* The attribute has changed. */
3015 /* Add (or update) attribute to hash. */
3016 attr_new
= bgp_attr_intern(parent_pi
->attr
);
3018 /* Restore route, if needed. */
3019 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
3020 bgp_path_info_restore(dest
, pi
);
3022 /* Mark if nexthop has changed. */
3023 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
3024 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
3026 old_local_es
= bgp_evpn_attr_is_local_es(pi
->attr
);
3027 new_local_es
= bgp_evpn_attr_is_local_es(attr_new
);
3028 /* If ESI is different or if its type has changed we
3029 * need to reinstall the path in zebra
3031 if ((old_local_es
!= new_local_es
)
3032 || memcmp(&pi
->attr
->esi
, &attr_new
->esi
,
3033 sizeof(attr_new
->esi
))) {
3035 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
))
3036 zlog_debug("VNI %d path %pFX chg to %s es",
3037 vpn
->vni
, &pi
->net
->p
,
3038 new_local_es
? "local"
3040 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ATTR_CHANGED
);
3043 /* Unintern existing, set to new. */
3044 bgp_attr_unintern(&pi
->attr
);
3045 pi
->attr
= attr_new
;
3046 pi
->uptime
= monotime(NULL
);
3049 /* Add this route to remote IP hashtable */
3050 bgp_evpn_remote_ip_hash_add(vpn
, pi
);
3052 /* Perform route selection and update zebra, if required. */
3053 ret
= evpn_route_select_install(bgp
, vpn
, dest
);
3055 /* if the best path is a local path with a non-zero ES
3056 * sync info against the local path may need to be updated
3057 * when a remote path is added/updated (including changes
3058 * from sync-path to remote-path)
3060 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
3061 if (local_pi
&& (old_local_es
|| new_local_es
))
3062 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, local_pi
,
3069 * Common handling for vni route tables uninstall/selection.
3071 static int uninstall_evpn_route_entry_in_vni_common(
3072 struct bgp
*bgp
, struct bgpevpn
*vpn
, const struct prefix_evpn
*p
,
3073 struct bgp_dest
*dest
, struct bgp_path_info
*parent_pi
)
3075 struct bgp_path_info
*pi
;
3076 struct bgp_path_info
*local_pi
;
3079 /* Find matching route entry. */
3080 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
3082 (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
3088 bgp_evpn_remote_ip_hash_del(vpn
, pi
);
3090 /* Mark entry for deletion */
3091 bgp_path_info_delete(dest
, pi
);
3093 /* Perform route selection and update zebra, if required. */
3094 ret
= evpn_route_select_install(bgp
, vpn
, dest
);
3096 /* if the best path is a local path with a non-zero ES
3097 * sync info against the local path may need to be updated
3098 * when a remote path is deleted
3100 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
3101 if (local_pi
&& bgp_evpn_attr_is_local_es(local_pi
->attr
))
3102 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, local_pi
,
3109 * Install route entry into VNI IP table and invoke route selection.
3111 static int install_evpn_route_entry_in_vni_ip(struct bgp
*bgp
,
3112 struct bgpevpn
*vpn
,
3113 const struct prefix_evpn
*p
,
3114 struct bgp_path_info
*parent_pi
)
3117 struct bgp_dest
*dest
;
3119 /* Ignore MAC Only Type-2 */
3120 if ((p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) &&
3121 (is_evpn_prefix_ipaddr_none(p
) == true))
3124 /* Create (or fetch) route within the VNI IP table. */
3125 dest
= bgp_evpn_vni_ip_node_get(vpn
->ip_table
, p
, parent_pi
);
3127 ret
= install_evpn_route_entry_in_vni_common(bgp
, vpn
, p
, dest
,
3130 bgp_dest_unlock_node(dest
);
3136 * Install route entry into VNI MAC table and invoke route selection.
3138 static int install_evpn_route_entry_in_vni_mac(struct bgp
*bgp
,
3139 struct bgpevpn
*vpn
,
3140 const struct prefix_evpn
*p
,
3141 struct bgp_path_info
*parent_pi
)
3144 struct bgp_dest
*dest
;
3146 /* Only type-2 routes go into this table */
3147 if (p
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3150 /* Create (or fetch) route within the VNI MAC table. */
3151 dest
= bgp_evpn_vni_mac_node_get(vpn
->mac_table
, p
, parent_pi
);
3153 ret
= install_evpn_route_entry_in_vni_common(bgp
, vpn
, p
, dest
,
3156 bgp_dest_unlock_node(dest
);
3162 * Uninstall route entry from VNI IP table and invoke route selection.
3164 static int uninstall_evpn_route_entry_in_vni_ip(struct bgp
*bgp
,
3165 struct bgpevpn
*vpn
,
3166 const struct prefix_evpn
*p
,
3167 struct bgp_path_info
*parent_pi
)
3170 struct bgp_dest
*dest
;
3172 /* Ignore MAC Only Type-2 */
3173 if ((p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) &&
3174 (is_evpn_prefix_ipaddr_none(p
) == true))
3177 /* Locate route within the VNI IP table. */
3178 dest
= bgp_evpn_vni_ip_node_lookup(vpn
->ip_table
, p
, parent_pi
);
3182 ret
= uninstall_evpn_route_entry_in_vni_common(bgp
, vpn
, p
, dest
,
3185 bgp_dest_unlock_node(dest
);
3191 * Uninstall route entry from VNI IP table and invoke route selection.
3194 uninstall_evpn_route_entry_in_vni_mac(struct bgp
*bgp
, struct bgpevpn
*vpn
,
3195 const struct prefix_evpn
*p
,
3196 struct bgp_path_info
*parent_pi
)
3199 struct bgp_dest
*dest
;
3201 /* Only type-2 routes go into this table */
3202 if (p
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3205 /* Locate route within the VNI MAC table. */
3206 dest
= bgp_evpn_vni_mac_node_lookup(vpn
->mac_table
, p
, parent_pi
);
3210 ret
= uninstall_evpn_route_entry_in_vni_common(bgp
, vpn
, p
, dest
,
3213 bgp_dest_unlock_node(dest
);
3218 * Uninstall route entry from the VRF routing table and send message
3219 * to zebra, if appropriate.
3221 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
3222 const struct prefix_evpn
*evp
,
3223 struct bgp_path_info
*parent_pi
)
3225 struct bgp_dest
*dest
;
3226 struct bgp_path_info
*pi
;
3229 struct prefix
*pp
= &p
;
3233 memset(pp
, 0, sizeof(struct prefix
));
3234 ip_prefix_from_evpn_prefix(evp
, pp
);
3236 if (bgp_debug_zebra(NULL
))
3238 "vrf %s: unimport evpn prefix %pFX parent %p flags 0x%x",
3239 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
, parent_pi
,
3242 /* Locate route within the VRF. */
3243 /* NOTE: There is no RD here. */
3244 if (is_evpn_prefix_ipaddr_v4(evp
)) {
3246 safi
= SAFI_UNICAST
;
3247 dest
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
3250 safi
= SAFI_UNICAST
;
3251 dest
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
3257 /* Find matching route entry. */
3258 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
3260 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
3264 bgp_dest_unlock_node(dest
);
3268 if (bgp_debug_zebra(NULL
))
3269 zlog_debug("... delete dest %p (l %d) pi %p (l %d, f 0x%x)",
3270 dest
, bgp_dest_get_lock_count(dest
), pi
, pi
->lock
,
3273 /* Process for route leaking. */
3274 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp_vrf
, pi
);
3276 bgp_aggregate_decrement(bgp_vrf
, bgp_dest_get_prefix(dest
), pi
, afi
,
3279 /* Mark entry for deletion */
3280 bgp_path_info_delete(dest
, pi
);
3282 /* Unlink path to evpn nexthop */
3283 bgp_evpn_path_nh_del(bgp_vrf
, pi
);
3285 /* Perform route selection and update zebra, if required. */
3286 bgp_process(bgp_vrf
, dest
, afi
, safi
);
3288 /* Unlock route node. */
3289 bgp_dest_unlock_node(dest
);
3295 * Install route entry into the VNI routing tables.
3297 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
3298 const struct prefix_evpn
*p
,
3299 struct bgp_path_info
*parent_pi
)
3303 if (bgp_debug_update(parent_pi
->peer
, NULL
, NULL
, 1))
3305 "%s (%u): Installing EVPN %pFX route in VNI %u IP/MAC table",
3306 vrf_id_to_name(bgp
->vrf_id
), bgp
->vrf_id
, p
, vpn
->vni
);
3308 ret
= install_evpn_route_entry_in_vni_mac(bgp
, vpn
, p
, parent_pi
);
3313 "%s (%u): Failed to install EVPN %pFX route in VNI %u MAC table",
3314 vrf_id_to_name(bgp
->vrf_id
), bgp
->vrf_id
, p
, vpn
->vni
);
3319 ret
= install_evpn_route_entry_in_vni_ip(bgp
, vpn
, p
, parent_pi
);
3324 "%s (%u): Failed to install EVPN %pFX route in VNI %u IP table",
3325 vrf_id_to_name(bgp
->vrf_id
), bgp
->vrf_id
, p
, vpn
->vni
);
3334 * Uninstall route entry from the VNI routing tables.
3336 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
3337 const struct prefix_evpn
*p
,
3338 struct bgp_path_info
*parent_pi
)
3342 if (bgp_debug_update(parent_pi
->peer
, NULL
, NULL
, 1))
3344 "%s (%u): Uninstalling EVPN %pFX route from VNI %u IP/MAC table",
3345 vrf_id_to_name(bgp
->vrf_id
), bgp
->vrf_id
, p
, vpn
->vni
);
3347 ret
= uninstall_evpn_route_entry_in_vni_ip(bgp
, vpn
, p
, parent_pi
);
3352 "%s (%u): Failed to uninstall EVPN %pFX route from VNI %u IP table",
3353 vrf_id_to_name(bgp
->vrf_id
), bgp
->vrf_id
, p
, vpn
->vni
);
3358 ret
= uninstall_evpn_route_entry_in_vni_mac(bgp
, vpn
, p
, parent_pi
);
3363 "%s (%u): Failed to uninstall EVPN %pFX route from VNI %u MAC table",
3364 vrf_id_to_name(bgp
->vrf_id
), bgp
->vrf_id
, p
, vpn
->vni
);
3373 * Given a route entry and a VRF, see if this route entry should be
3374 * imported into the VRF i.e., RTs match.
3376 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
3377 struct bgp_path_info
*pi
)
3379 struct attr
*attr
= pi
->attr
;
3380 struct ecommunity
*ecom
;
3384 /* Route should have valid RT to be even considered. */
3385 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3388 ecom
= bgp_attr_get_ecommunity(attr
);
3389 if (!ecom
|| !ecom
->size
)
3392 /* For each extended community RT, see if it matches this VNI. If any RT
3393 * matches, we're done.
3395 for (i
= 0; i
< ecom
->size
; i
++) {
3397 uint8_t type
, sub_type
;
3398 struct ecommunity_val
*eval
;
3399 struct ecommunity_val eval_tmp
;
3400 struct vrf_irt_node
*irt
;
3402 /* Only deal with RTs */
3403 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
3404 eval
= (struct ecommunity_val
*)(ecom
->val
3405 + (i
* ecom
->unit_size
));
3408 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3411 /* See if this RT matches specified VNIs import RTs */
3412 irt
= lookup_vrf_import_rt(eval
);
3414 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
3417 /* Also check for non-exact match. In this, we mask out the AS
3419 * only check on the local-admin sub-field. This is to
3421 * VNI as the RT for EBGP peering too.
3424 if (type
== ECOMMUNITY_ENCODE_AS
3425 || type
== ECOMMUNITY_ENCODE_AS4
3426 || type
== ECOMMUNITY_ENCODE_IP
) {
3427 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
3428 mask_ecom_global_admin(&eval_tmp
, eval
);
3429 irt
= lookup_vrf_import_rt(&eval_tmp
);
3432 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
3440 * Given a route entry and a VNI, see if this route entry should be
3441 * imported into the VNI i.e., RTs match.
3443 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
3444 struct bgp_path_info
*pi
)
3446 struct attr
*attr
= pi
->attr
;
3447 struct ecommunity
*ecom
;
3451 /* Route should have valid RT to be even considered. */
3452 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3455 ecom
= bgp_attr_get_ecommunity(attr
);
3456 if (!ecom
|| !ecom
->size
)
3459 /* For each extended community RT, see if it matches this VNI. If any RT
3460 * matches, we're done.
3462 for (i
= 0; i
< ecom
->size
; i
++) {
3464 uint8_t type
, sub_type
;
3465 struct ecommunity_val
*eval
;
3466 struct ecommunity_val eval_tmp
;
3467 struct irt_node
*irt
;
3469 /* Only deal with RTs */
3470 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
3471 eval
= (struct ecommunity_val
*)(ecom
->val
3472 + (i
* ecom
->unit_size
));
3475 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3478 /* See if this RT matches specified VNIs import RTs */
3479 irt
= lookup_import_rt(bgp
, eval
);
3481 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
3484 /* Also check for non-exact match. In this, we mask out the AS
3486 * only check on the local-admin sub-field. This is to
3488 * VNI as the RT for EBGP peering too.
3491 if (type
== ECOMMUNITY_ENCODE_AS
3492 || type
== ECOMMUNITY_ENCODE_AS4
3493 || type
== ECOMMUNITY_ENCODE_IP
) {
3494 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
3495 mask_ecom_global_admin(&eval_tmp
, eval
);
3496 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3499 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
3506 /* This API will scan evpn routes for checking attribute's rmac
3507 * macthes with bgp instance router mac. It avoid installing
3508 * route into bgp vrf table and remote rmac in bridge table.
3510 static int bgp_evpn_route_rmac_self_check(struct bgp
*bgp_vrf
,
3511 const struct prefix_evpn
*evp
,
3512 struct bgp_path_info
*pi
)
3514 /* evpn route could have learnt prior to L3vni has come up,
3515 * perform rmac check before installing route and
3516 * remote router mac.
3517 * The route will be removed from global bgp table once
3518 * SVI comes up with MAC and stored in hash, triggers
3519 * bgp_mac_rescan_all_evpn_tables.
3521 if (memcmp(&bgp_vrf
->rmac
, &pi
->attr
->rmac
, ETH_ALEN
) == 0) {
3522 if (bgp_debug_update(pi
->peer
, NULL
, NULL
, 1)) {
3523 char attr_str
[BUFSIZ
] = {0};
3525 bgp_dump_attr(pi
->attr
, attr_str
, sizeof(attr_str
));
3528 "%s: bgp %u prefix %pFX with attr %s - DENIED due to self mac",
3529 __func__
, bgp_vrf
->vrf_id
, evp
, attr_str
);
3538 /* don't import hosts that are locally attached */
3540 bgp_evpn_skip_vrf_import_of_local_es(struct bgp
*bgp_vrf
,
3541 const struct prefix_evpn
*evp
,
3542 struct bgp_path_info
*pi
, int install
)
3546 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3547 esi
= bgp_evpn_attr_get_esi(pi
->attr
);
3549 /* Don't import routes that point to a local destination */
3550 if (bgp_evpn_attr_is_local_es(pi
->attr
)) {
3551 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
3552 char esi_buf
[ESI_STR_LEN
];
3555 "vrf %s of evpn prefix %pFX skipped, local es %s",
3556 install
? "import" : "unimport", evp
,
3557 esi_to_str(esi
, esi_buf
,
3567 * Install or uninstall a mac-ip route in the provided vrf if
3568 * there is a rt match
3570 int bgp_evpn_route_entry_install_if_vrf_match(struct bgp
*bgp_vrf
,
3571 struct bgp_path_info
*pi
,
3575 const struct prefix_evpn
*evp
=
3576 (const struct prefix_evpn
*)bgp_dest_get_prefix(pi
->net
);
3578 /* Consider "valid" remote routes applicable for
3581 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3582 && pi
->type
== ZEBRA_ROUTE_BGP
3583 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3586 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
3587 if (bgp_evpn_route_rmac_self_check(bgp_vrf
, evp
, pi
))
3590 /* don't import hosts that are locally attached */
3591 if (install
&& bgp_evpn_skip_vrf_import_of_local_es(
3592 bgp_vrf
, evp
, pi
, install
))
3596 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3598 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3602 flog_err(EC_BGP_EVPN_FAIL
,
3603 "Failed to %s EVPN %pFX route in VRF %s",
3604 install
? "install" : "uninstall", evp
,
3605 vrf_id_to_name(bgp_vrf
->vrf_id
));
3612 * Install or uninstall mac-ip routes are appropriate for this
3615 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
3619 struct bgp_dest
*rd_dest
, *dest
;
3620 struct bgp_table
*table
;
3621 struct bgp_path_info
*pi
;
3623 struct bgp
*bgp_evpn
= NULL
;
3627 bgp_evpn
= bgp_get_evpn();
3631 /* Walk entire global routing table and evaluate routes which could be
3632 * imported into this VRF. Note that we need to loop through all global
3633 * routes to determine which route matches the import rt on vrf
3635 for (rd_dest
= bgp_table_top(bgp_evpn
->rib
[afi
][safi
]); rd_dest
;
3636 rd_dest
= bgp_route_next(rd_dest
)) {
3637 table
= bgp_dest_get_bgp_table_info(rd_dest
);
3641 for (dest
= bgp_table_top(table
); dest
;
3642 dest
= bgp_route_next(dest
)) {
3643 const struct prefix_evpn
*evp
=
3644 (const struct prefix_evpn
*)bgp_dest_get_prefix(
3647 /* if not mac-ip route skip this route */
3648 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3649 || evp
->prefix
.route_type
3650 == BGP_EVPN_IP_PREFIX_ROUTE
))
3653 /* if not a mac+ip route skip this route */
3654 if (!(is_evpn_prefix_ipaddr_v4(evp
)
3655 || is_evpn_prefix_ipaddr_v6(evp
)))
3658 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
3660 ret
= bgp_evpn_route_entry_install_if_vrf_match(
3661 bgp_vrf
, pi
, install
);
3672 * Install or uninstall routes of specified type that are appropriate for this
3675 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
3676 struct bgpevpn
*vpn
,
3677 bgp_evpn_route_type rtype
,
3682 struct bgp_dest
*rd_dest
, *dest
;
3683 struct bgp_table
*table
;
3684 struct bgp_path_info
*pi
;
3690 /* Walk entire global routing table and evaluate routes which could be
3691 * imported into this VPN. Note that we cannot just look at the routes
3693 * the VNI's RD - remote routes applicable for this VNI could have any
3696 /* EVPN routes are a 2-level table. */
3697 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
3698 rd_dest
= bgp_route_next(rd_dest
)) {
3699 table
= bgp_dest_get_bgp_table_info(rd_dest
);
3703 for (dest
= bgp_table_top(table
); dest
;
3704 dest
= bgp_route_next(dest
)) {
3705 const struct prefix_evpn
*evp
=
3706 (const struct prefix_evpn
*)bgp_dest_get_prefix(
3709 if (evp
->prefix
.route_type
!= rtype
)
3712 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
3714 /* Consider "valid" remote routes applicable for
3716 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3717 && pi
->type
== ZEBRA_ROUTE_BGP
3718 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3721 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3723 ret
= install_evpn_route_entry(
3726 ret
= uninstall_evpn_route_entry(
3732 "%u: Failed to %s EVPN %s route in VNI %u",
3736 rtype
== BGP_EVPN_MAC_IP_ROUTE
3741 bgp_dest_unlock_node(rd_dest
);
3742 bgp_dest_unlock_node(dest
);
3753 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3754 * is invoked upon l3vni-add or l3vni import rt change
3756 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3758 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3763 * Install any existing remote routes applicable for this VNI into its
3764 * routing table. This is invoked when a VNI becomes "live" or its Import
3767 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3771 /* Install type-3 routes followed by type-2 routes - the ones applicable
3774 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3779 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_AD_ROUTE
,
3784 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3788 /* uninstall routes from l3vni vrf. */
3789 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3791 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3796 * Uninstall any existing remote routes for this VNI. One scenario in which
3797 * this is invoked is upon an import RT change.
3799 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3803 /* Uninstall type-2 routes followed by type-3 routes - the ones
3807 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3812 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_AD_ROUTE
,
3818 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3823 * Install or uninstall route in matching VRFs (list).
3825 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3826 safi_t safi
, struct prefix_evpn
*evp
,
3827 struct bgp_path_info
*pi
,
3828 struct list
*vrfs
, int install
)
3830 struct bgp
*bgp_vrf
;
3831 struct listnode
*node
, *nnode
;
3833 /* Only type-2/type-5 routes go into a VRF */
3834 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3835 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3838 /* if it is type-2 route and not a mac+ip route skip this route */
3839 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3840 && !(is_evpn_prefix_ipaddr_v4(evp
)
3841 || is_evpn_prefix_ipaddr_v6(evp
)))
3844 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3847 /* don't import hosts that are locally attached */
3848 if (install
&& bgp_evpn_skip_vrf_import_of_local_es(
3849 bgp_vrf
, evp
, pi
, install
))
3853 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3855 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3859 flog_err(EC_BGP_EVPN_FAIL
,
3860 "%u: Failed to %s prefix %pFX in VRF %s",
3862 install
? "install" : "uninstall", evp
,
3863 vrf_id_to_name(bgp_vrf
->vrf_id
));
3872 * Install or uninstall route in matching VNIs (list).
3874 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3875 safi_t safi
, struct prefix_evpn
*evp
,
3876 struct bgp_path_info
*pi
,
3877 struct list
*vnis
, int install
)
3879 struct bgpevpn
*vpn
;
3880 struct listnode
*node
, *nnode
;
3882 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3885 if (!is_vni_live(vpn
))
3889 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3891 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3894 flog_err(EC_BGP_EVPN_FAIL
,
3895 "%u: Failed to %s EVPN %s route in VNI %u",
3896 bgp
->vrf_id
, install
? "install" : "uninstall",
3897 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3909 * Install or uninstall route for appropriate VNIs/ESIs.
3911 static int bgp_evpn_install_uninstall_table(struct bgp
*bgp
, afi_t afi
,
3912 safi_t safi
, const struct prefix
*p
,
3913 struct bgp_path_info
*pi
,
3914 int import
, bool in_vni_rt
,
3917 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3918 struct attr
*attr
= pi
->attr
;
3919 struct ecommunity
*ecom
;
3921 struct prefix_evpn ad_evp
;
3925 /* Only type-1, type-2, type-3, type-4 and type-5
3926 * are supported currently
3928 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3929 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3930 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3931 || evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
3932 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3935 /* If we don't have Route Target, nothing much to do. */
3936 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3939 /* EAD prefix in the global table doesn't include the VTEP-IP so
3940 * we need to create a different copy for the VNI
3942 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
3943 evp
= evpn_type1_prefix_vni_ip_copy(&ad_evp
, evp
,
3946 ecom
= bgp_attr_get_ecommunity(attr
);
3947 if (!ecom
|| !ecom
->size
)
3950 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3951 * attached to the route */
3952 for (i
= 0; i
< ecom
->size
; i
++) {
3954 uint8_t type
, sub_type
;
3955 struct ecommunity_val
*eval
;
3956 struct ecommunity_val eval_tmp
;
3957 struct irt_node
*irt
; /* import rt for l2vni */
3958 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3959 struct bgp_evpn_es
*es
;
3961 /* Only deal with RTs */
3962 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
3963 eval
= (struct ecommunity_val
*)(ecom
->val
3964 + (i
* ecom
->unit_size
));
3967 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3970 /* non-local MAC-IP routes in the global route table are linked
3971 * to the destination ES
3973 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3974 bgp_evpn_path_es_link(pi
, 0,
3975 bgp_evpn_attr_get_esi(pi
->attr
));
3978 * macip routes (type-2) are imported into VNI and VRF tables.
3979 * IMET route is imported into VNI table.
3980 * prefix routes are imported into VRF table.
3982 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3983 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3984 evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
||
3985 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3987 irt
= in_vni_rt
? lookup_import_rt(bgp
, eval
) : NULL
;
3989 install_uninstall_route_in_vnis(
3990 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3993 vrf_irt
= in_vrf_rt
? lookup_vrf_import_rt(eval
) : NULL
;
3995 install_uninstall_route_in_vrfs(
3996 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3999 /* Also check for non-exact match.
4000 * In this, we mask out the AS and
4001 * only check on the local-admin sub-field.
4002 * This is to facilitate using
4003 * VNI as the RT for EBGP peering too.
4007 if (type
== ECOMMUNITY_ENCODE_AS
4008 || type
== ECOMMUNITY_ENCODE_AS4
4009 || type
== ECOMMUNITY_ENCODE_IP
) {
4010 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
4011 mask_ecom_global_admin(&eval_tmp
, eval
);
4013 irt
= lookup_import_rt(bgp
, &eval_tmp
);
4016 lookup_vrf_import_rt(&eval_tmp
);
4020 install_uninstall_route_in_vnis(
4021 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
4024 install_uninstall_route_in_vrfs(
4025 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
4029 /* es route is imported into the es table */
4030 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
4032 /* we will match based on the entire esi to avoid
4033 * import of an es route for esi2 into esi1
4035 es
= bgp_evpn_es_find(&evp
->prefix
.es_addr
.esi
);
4036 if (es
&& bgp_evpn_is_es_local(es
))
4037 bgp_evpn_es_route_install_uninstall(
4038 bgp
, es
, afi
, safi
, evp
, pi
, import
);
4046 * Install or uninstall route for appropriate VNIs/ESIs.
4048 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
4049 const struct prefix
*p
,
4050 struct bgp_path_info
*pi
, int import
)
4052 return bgp_evpn_install_uninstall_table(bgp
, afi
, safi
, p
, pi
, import
,
4056 void bgp_evpn_import_type2_route(struct bgp_path_info
*pi
, int import
)
4058 struct bgp
*bgp_evpn
;
4060 bgp_evpn
= bgp_get_evpn();
4064 install_uninstall_evpn_route(bgp_evpn
, AFI_L2VPN
, SAFI_EVPN
,
4065 &pi
->net
->p
, pi
, import
);
4069 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
4072 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
4074 /* Delete ipv4 default route and withdraw from peers */
4075 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
4076 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
4077 SAFI_UNICAST
, false);
4079 /* delete all ipv4 routes and withdraw from peers */
4080 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
4081 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
4083 /* Delete ipv6 default route and withdraw from peers */
4084 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
4085 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
4086 SAFI_UNICAST
, false);
4088 /* delete all ipv6 routes and withdraw from peers */
4089 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
4090 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
4094 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
4097 void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
4099 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
4101 bgp_evpn
= bgp_get_evpn();
4105 /* update all ipv4 routes */
4106 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
4107 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
4109 /* update ipv4 default route and withdraw from peers */
4110 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
4111 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
4112 SAFI_UNICAST
, true);
4114 /* update all ipv6 routes */
4115 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
4116 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
4118 /* update ipv6 default route and withdraw from peers */
4119 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
4120 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
4121 SAFI_UNICAST
, true);
4126 * update and advertise local routes for a VRF as type-5 routes.
4127 * This is invoked upon RD change for a VRF. Note taht the processing is only
4128 * done in the global route table using the routes which already exist in the
4131 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
4133 /* skip if the RD is configured */
4134 if (is_vrf_rd_configured(bgp_vrf
))
4137 /* derive the RD for the VRF based on new router-id */
4138 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
4140 /* update advertise ipv4|ipv6 routes as type-5 routes */
4141 update_advertise_vrf_routes(bgp_vrf
);
4145 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
4146 * This is invoked upon VRF RD change. The processing is done only from global
4149 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
4151 /* skip if the RD is configured */
4152 if (is_vrf_rd_configured(bgp_vrf
))
4155 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
4156 delete_withdraw_vrf_routes(bgp_vrf
);
4159 static void update_advertise_vni_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4160 struct bgp_dest
*dest
)
4162 struct bgp_dest
*global_dest
;
4163 struct bgp_path_info
*pi
, *global_pi
;
4165 afi_t afi
= AFI_L2VPN
;
4166 safi_t safi
= SAFI_EVPN
;
4168 struct prefix_evpn tmp_evp
;
4169 const struct prefix_evpn
*evp
=
4170 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
4173 * We have already processed type-3 routes.
4174 * Process only type-1 and type-2 routes here.
4176 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
&&
4177 evp
->prefix
.route_type
!= BGP_EVPN_AD_ROUTE
)
4180 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
4181 if (pi
->peer
== bgp
->peer_self
&& pi
->type
== ZEBRA_ROUTE_BGP
&&
4182 pi
->sub_type
== BGP_ROUTE_STATIC
)
4188 * VNI table MAC-IP prefixes don't have MAC so make sure it's
4189 * set from path info here.
4191 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4192 if (is_evpn_prefix_ipaddr_none(evp
)) {
4193 /* VNI MAC -> Global */
4194 evpn_type2_prefix_global_copy(
4195 &tmp_evp
, evp
, NULL
/* mac */,
4196 evpn_type2_path_info_get_ip(pi
));
4198 /* VNI IP -> Global */
4199 evpn_type2_prefix_global_copy(
4200 &tmp_evp
, evp
, evpn_type2_path_info_get_mac(pi
),
4204 memcpy(&tmp_evp
, evp
, sizeof(tmp_evp
));
4207 /* Create route in global routing table using this route entry's
4211 global_dest
= bgp_evpn_global_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
4212 &tmp_evp
, &vpn
->prd
, NULL
);
4213 assert(global_dest
);
4215 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4217 update_evpn_route_entry(
4218 bgp
, vpn
, afi
, safi
, global_dest
, attr
, NULL
/* mac */,
4219 NULL
/* ip */, 1, &global_pi
, 0,
4220 mac_mobility_seqnum(attr
), false /* setup_sync */,
4221 NULL
/* old_is_sync */);
4224 struct bgp_evpn_es
*es
;
4225 int route_changed
= 0;
4227 es
= bgp_evpn_es_find(&evp
->prefix
.ead_addr
.esi
);
4228 bgp_evpn_mh_route_update(bgp
, es
, vpn
, afi
, safi
, global_dest
,
4229 attr
, &global_pi
, &route_changed
);
4232 /* Schedule for processing and unlock node. */
4233 bgp_process(bgp
, global_dest
, afi
, safi
);
4234 bgp_dest_unlock_node(global_dest
);
4238 * Update and advertise local routes for a VNI. Invoked upon router-id
4239 * change. Note that the processing is done only on the global route table
4240 * using routes that already exist in the per-VNI table.
4242 static void update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4244 struct prefix_evpn p
;
4245 struct bgp_dest
*dest
, *global_dest
;
4246 struct bgp_path_info
*pi
;
4248 afi_t afi
= AFI_L2VPN
;
4249 safi_t safi
= SAFI_EVPN
;
4251 /* Locate type-3 route for VNI in the per-VNI table and use its
4252 * attributes to create and advertise the type-3 route for this VNI
4253 * in the global table.
4255 * RT-3 only if doing head-end replication
4257 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
4258 == VXLAN_FLOOD_HEAD_END_REPL
) {
4259 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
4260 dest
= bgp_evpn_vni_node_lookup(vpn
, &p
, NULL
);
4261 if (!dest
) /* unexpected */
4263 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
4264 if (pi
->peer
== bgp
->peer_self
&&
4265 pi
->type
== ZEBRA_ROUTE_BGP
4266 && pi
->sub_type
== BGP_ROUTE_STATIC
)
4269 bgp_dest_unlock_node(dest
);
4275 global_dest
= bgp_evpn_global_node_get(
4276 bgp
->rib
[afi
][safi
], afi
, safi
, &p
, &vpn
->prd
, NULL
);
4277 update_evpn_route_entry(
4278 bgp
, vpn
, afi
, safi
, global_dest
, attr
, NULL
/* mac */,
4279 NULL
/* ip */, 1, &pi
, 0, mac_mobility_seqnum(attr
),
4280 false /* setup_sync */, NULL
/* old_is_sync */);
4282 /* Schedule for processing and unlock node. */
4283 bgp_process(bgp
, global_dest
, afi
, safi
);
4284 bgp_dest_unlock_node(global_dest
);
4287 /* Now, walk this VNI's MAC & IP route table and use the route and its
4288 * attribute to create and schedule route in global table.
4290 for (dest
= bgp_table_top(vpn
->mac_table
); dest
;
4291 dest
= bgp_route_next(dest
))
4292 update_advertise_vni_route(bgp
, vpn
, dest
);
4294 for (dest
= bgp_table_top(vpn
->ip_table
); dest
;
4295 dest
= bgp_route_next(dest
))
4296 update_advertise_vni_route(bgp
, vpn
, dest
);
4300 * Delete (and withdraw) local routes for a VNI - only from the global
4301 * table. Invoked upon router-id change.
4303 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4305 struct prefix_evpn p
;
4306 struct bgp_dest
*global_dest
;
4307 struct bgp_path_info
*pi
;
4308 afi_t afi
= AFI_L2VPN
;
4309 safi_t safi
= SAFI_EVPN
;
4311 /* Delete and withdraw locally learnt type-2 routes (MACIP)
4312 * for this VNI - from the global table.
4314 delete_global_type2_routes(bgp
, vpn
);
4316 /* Remove type-3 route for this VNI from global table. */
4317 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
4318 global_dest
= bgp_evpn_global_node_lookup(bgp
->rib
[afi
][safi
], afi
,
4319 safi
, &p
, &vpn
->prd
, NULL
);
4321 /* Delete route entry in the global EVPN table. */
4322 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
4324 /* Schedule for processing - withdraws to peers happen from
4328 bgp_process(bgp
, global_dest
, afi
, safi
);
4329 bgp_dest_unlock_node(global_dest
);
4333 delete_global_ead_evi_routes(bgp
, vpn
);
4338 * Handle router-id change. Update and advertise local routes corresponding
4339 * to this VNI from peers. Note that this is invoked after updating the
4340 * router-id. The routes in the per-VNI table are used to create routes in
4341 * the global table and schedule them.
4343 static void update_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4345 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4347 /* Skip VNIs with configured RD. */
4348 if (is_rd_configured(vpn
))
4351 bgp_evpn_derive_auto_rd(bgp
, vpn
);
4352 update_advertise_vni_routes(bgp
, vpn
);
4356 * Handle router-id change. Delete and withdraw local routes corresponding
4357 * to this VNI from peers. Note that this is invoked prior to updating
4358 * the router-id and is done only on the global route table, the routes
4359 * are needed in the per-VNI table to re-advertise with new router id.
4361 static void withdraw_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4363 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4365 /* Skip VNIs with configured RD. */
4366 if (is_rd_configured(vpn
))
4369 delete_withdraw_vni_routes(bgp
, vpn
);
4373 * Create RT-3 for a VNI and schedule for processing and advertisement.
4374 * This is invoked upon flooding mode changing to head-end replication.
4376 static void create_advertise_type3(struct hash_bucket
*bucket
, void *data
)
4378 struct bgpevpn
*vpn
= bucket
->data
;
4379 struct bgp
*bgp
= data
;
4380 struct prefix_evpn p
;
4382 if (!vpn
|| !is_vni_live(vpn
) ||
4383 bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
4384 != VXLAN_FLOOD_HEAD_END_REPL
)
4387 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
4388 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
))
4389 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4390 "Type3 route creation failure for VNI %u", vpn
->vni
);
4394 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
4395 * This is invoked upon flooding mode changing to drop BUM packets.
4397 static void delete_withdraw_type3(struct hash_bucket
*bucket
, void *data
)
4399 struct bgpevpn
*vpn
= bucket
->data
;
4400 struct bgp
*bgp
= data
;
4401 struct prefix_evpn p
;
4403 if (!vpn
|| !is_vni_live(vpn
))
4406 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
4407 delete_evpn_route(bgp
, vpn
, &p
);
4411 * Process received EVPN type-2 route (advertise or withdraw).
4413 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4414 struct attr
*attr
, uint8_t *pfx
, int psize
,
4415 uint32_t addpath_id
)
4417 struct prefix_rd prd
;
4418 struct prefix_evpn p
= {};
4419 struct bgp_route_evpn evpn
= {};
4421 uint8_t macaddr_len
;
4422 /* holds the VNI(s) as in packet */
4423 mpls_label_t label
[BGP_MAX_LABELS
] = {};
4424 uint32_t num_labels
= 0;
4428 /* Type-2 route should be either 33, 37 or 49 bytes or an
4429 * additional 3 bytes if there is a second label (VNI):
4430 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
4431 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
4432 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
4434 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
4435 && psize
!= 40 && psize
!= 52) {
4436 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4437 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
4438 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4442 struct stream
*pkt
= stream_new(psize
);
4443 stream_put(pkt
, pfx
, psize
);
4445 /* Make prefix_rd */
4446 prd
.family
= AF_UNSPEC
;
4449 STREAM_GET(&prd
.val
, pkt
, 8);
4451 /* Make EVPN prefix. */
4453 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4454 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
4456 /* Copy Ethernet Seg Identifier */
4458 STREAM_GET(&attr
->esi
, pkt
, sizeof(esi_t
));
4460 if (bgp_evpn_is_esi_local_and_non_bypass(&attr
->esi
))
4461 attr
->es_flags
|= ATTR_ES_IS_LOCAL
;
4463 attr
->es_flags
&= ~ATTR_ES_IS_LOCAL
;
4465 STREAM_FORWARD_GETP(pkt
, sizeof(esi_t
));
4468 /* Copy Ethernet Tag */
4469 STREAM_GET(ð_tag
, pkt
, 4);
4470 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
4472 /* Get the MAC Addr len */
4473 STREAM_GETC(pkt
, macaddr_len
);
4475 /* Get the MAC Addr */
4476 if (macaddr_len
== (ETH_ALEN
* 8)) {
4477 STREAM_GET(&p
.prefix
.macip_addr
.mac
.octet
, pkt
, ETH_ALEN
);
4480 EC_BGP_EVPN_ROUTE_INVALID
,
4481 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
4482 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
4488 STREAM_GETC(pkt
, ipaddr_len
);
4490 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
4491 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
4493 EC_BGP_EVPN_ROUTE_INVALID
,
4494 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
4495 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4500 ipaddr_len
/= 8; /* Convert to bytes. */
4501 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
4504 STREAM_GET(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pkt
, ipaddr_len
);
4507 /* Get the VNI(s). Stored as bytes here. */
4508 STREAM_GET(&label
[0], pkt
, BGP_LABEL_BYTES
);
4511 /* Do we have a second VNI? */
4512 if (STREAM_READABLE(pkt
)) {
4514 STREAM_GET(&label
[1], pkt
, BGP_LABEL_BYTES
);
4517 /* Process the route. */
4519 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4520 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4521 &prd
, &label
[0], num_labels
, 0, &evpn
);
4523 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4524 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4525 &prd
, &label
[0], num_labels
, &evpn
);
4530 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4531 "%u:%s - Rx EVPN Type-2 NLRI - corrupt, discarding",
4532 peer
->bgp
->vrf_id
, peer
->host
);
4540 * Process received EVPN type-3 route (advertise or withdraw).
4542 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4543 struct attr
*attr
, uint8_t *pfx
, int psize
,
4544 uint32_t addpath_id
)
4546 struct prefix_rd prd
;
4547 struct prefix_evpn p
;
4552 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
4553 * IP len (1) and IP (4 or 16).
4555 if (psize
!= 17 && psize
!= 29) {
4556 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4557 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
4558 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4562 /* If PMSI is present, log if it is anything other than IR.
4563 * Note: We just simply ignore the values as it is not clear if
4564 * doing anything else is better.
4567 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
4568 enum pta_type pmsi_tnl_type
= bgp_attr_get_pmsi_tnl_type(attr
);
4570 if (pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
4571 && pmsi_tnl_type
!= PMSI_TNLTYPE_PIM_SM
) {
4573 EC_BGP_EVPN_PMSI_PRESENT
,
4574 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
4575 peer
->bgp
->vrf_id
, peer
->host
, pmsi_tnl_type
);
4579 /* Make prefix_rd */
4580 prd
.family
= AF_UNSPEC
;
4582 memcpy(&prd
.val
, pfx
, 8);
4585 /* Make EVPN prefix. */
4586 memset(&p
, 0, sizeof(p
));
4588 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4589 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
4591 /* Copy Ethernet Tag */
4592 memcpy(ð_tag
, pfx
, 4);
4593 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
4597 ipaddr_len
= *pfx
++;
4598 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
4599 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
4600 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
4603 EC_BGP_EVPN_ROUTE_INVALID
,
4604 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
4605 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4609 /* Process the route. */
4611 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4612 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4613 &prd
, NULL
, 0, 0, NULL
);
4615 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4616 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4617 &prd
, NULL
, 0, NULL
);
4622 * Process received EVPN type-5 route (advertise or withdraw).
4624 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4625 struct attr
*attr
, uint8_t *pfx
, int psize
,
4626 uint32_t addpath_id
)
4628 struct prefix_rd prd
;
4629 struct prefix_evpn p
;
4630 struct bgp_route_evpn evpn
;
4633 mpls_label_t label
; /* holds the VNI as in the packet */
4635 bool is_valid_update
= true;
4637 /* Type-5 route should be 34 or 58 bytes:
4638 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
4639 * GW (4 or 16) and VNI (3).
4640 * Note that the IP and GW should both be IPv4 or both IPv6.
4642 if (psize
!= 34 && psize
!= 58) {
4643 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4644 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
4645 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4649 /* Make prefix_rd */
4650 prd
.family
= AF_UNSPEC
;
4652 memcpy(&prd
.val
, pfx
, 8);
4655 /* Make EVPN prefix. */
4656 memset(&p
, 0, sizeof(p
));
4658 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4659 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
4661 /* Additional information outside of prefix - ESI and GW IP */
4662 memset(&evpn
, 0, sizeof(evpn
));
4664 /* Fetch ESI overlay index */
4666 memcpy(&evpn
.eth_s_id
, pfx
, sizeof(esi_t
));
4669 /* Fetch Ethernet Tag. */
4670 memcpy(ð_tag
, pfx
, 4);
4671 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
4674 /* Fetch IP prefix length. */
4676 if (ippfx_len
> IPV6_MAX_BITLEN
) {
4678 EC_BGP_EVPN_ROUTE_INVALID
,
4679 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4680 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
4683 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
4685 /* Determine IPv4 or IPv6 prefix */
4686 /* Since the address and GW are from the same family, this just becomes
4687 * a simple check on the total size.
4690 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
4691 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
4693 SET_IPADDR_V4(&evpn
.gw_ip
);
4694 memcpy(&evpn
.gw_ip
.ipaddr_v4
, pfx
, 4);
4697 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
4698 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
,
4700 pfx
+= IPV6_MAX_BYTELEN
;
4701 SET_IPADDR_V6(&evpn
.gw_ip
);
4702 memcpy(&evpn
.gw_ip
.ipaddr_v6
, pfx
, IPV6_MAX_BYTELEN
);
4703 pfx
+= IPV6_MAX_BYTELEN
;
4706 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4707 memset(&label
, 0, sizeof(label
));
4708 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
4711 * If in future, we are required to access additional fields,
4712 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4717 * An update containing a non-zero gateway IP and a non-zero ESI
4718 * at the same time is should be treated as withdraw
4720 if (bgp_evpn_is_esi_valid(&evpn
.eth_s_id
) &&
4721 !ipaddr_is_zero(&evpn
.gw_ip
)) {
4722 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4723 "%s - Rx EVPN Type-5 ESI and gateway-IP both non-zero.",
4725 is_valid_update
= false;
4726 } else if (bgp_evpn_is_esi_valid(&evpn
.eth_s_id
))
4727 evpn
.type
= OVERLAY_INDEX_ESI
;
4728 else if (!ipaddr_is_zero(&evpn
.gw_ip
))
4729 evpn
.type
= OVERLAY_INDEX_GATEWAY_IP
;
4731 if (is_zero_mac(&attr
->rmac
) &&
4732 !bgp_evpn_is_esi_valid(&evpn
.eth_s_id
) &&
4733 ipaddr_is_zero(&evpn
.gw_ip
) && label
== 0) {
4734 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4735 "%s - Rx EVPN Type-5 ESI, gateway-IP, RMAC and label all zero",
4737 is_valid_update
= false;
4740 if (is_mcast_mac(&attr
->rmac
) || is_bcast_mac(&attr
->rmac
))
4741 is_valid_update
= false;
4744 /* Process the route. */
4745 if (attr
&& is_valid_update
)
4746 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4747 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4748 &prd
, &label
, 1, 0, &evpn
);
4750 if (!is_valid_update
) {
4751 char attr_str
[BUFSIZ
] = {0};
4753 bgp_dump_attr(attr
, attr_str
, BUFSIZ
);
4755 "Invalid update from peer %s vrf %u prefix %pFX attr %s - treat as withdraw",
4756 peer
->hostname
, peer
->bgp
->vrf_id
, &p
,
4759 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4760 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4761 &prd
, &label
, 1, &evpn
);
4767 static void evpn_mpattr_encode_type5(struct stream
*s
, const struct prefix
*p
,
4768 const struct prefix_rd
*prd
,
4769 mpls_label_t
*label
, uint32_t num_labels
,
4774 const struct evpn_addr
*p_evpn_p
;
4776 memset(&temp
, 0, sizeof(temp
));
4777 if (p
->family
!= AF_EVPN
)
4779 p_evpn_p
= &(p
->u
.prefix_evpn
);
4781 /* len denites the total len of IP and GW-IP in the route
4782 IP and GW-IP have to be both ipv4 or ipv6
4784 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4785 len
= 8; /* IP and GWIP are both ipv4 */
4787 len
= 32; /* IP and GWIP are both ipv6 */
4788 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4789 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4790 stream_put(s
, prd
->val
, 8);
4791 if (attr
&& attr
->evpn_overlay
.type
== OVERLAY_INDEX_ESI
)
4792 stream_put(s
, &attr
->esi
, sizeof(esi_t
));
4794 stream_put(s
, 0, sizeof(esi_t
));
4795 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4796 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4797 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4798 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4800 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4801 if (attr
&& attr
->evpn_overlay
.type
== OVERLAY_INDEX_GATEWAY_IP
) {
4802 const struct bgp_route_evpn
*evpn_overlay
=
4803 bgp_attr_get_evpn_overlay(attr
);
4805 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4807 evpn_overlay
->gw_ip
.ipaddr_v4
.s_addr
);
4809 stream_put(s
, &(evpn_overlay
->gw_ip
.ipaddr_v6
), 16);
4811 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4812 stream_put_ipv4(s
, 0);
4814 stream_put(s
, &temp
, 16);
4818 stream_put(s
, label
, 3);
4824 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4826 static void cleanup_vni_on_disable(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4828 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4830 /* Remove EVPN routes and schedule for processing. */
4831 delete_routes_for_vni(bgp
, vpn
);
4833 /* Clear "live" flag and see if hash needs to be freed. */
4834 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4835 if (!is_vni_configured(vpn
))
4836 bgp_evpn_free(bgp
, vpn
);
4840 * Free a VNI entry; iterator function called during cleanup.
4842 static void free_vni_entry(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4844 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4846 delete_all_vni_routes(bgp
, vpn
);
4847 bgp_evpn_free(bgp
, vpn
);
4851 * Derive AUTO import RT for BGP VRF - L3VNI
4853 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4855 struct bgp
*bgp_evpn
= NULL
;
4857 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
, true);
4860 bgp_evpn
= bgp_get_evpn();
4865 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4869 * Delete AUTO import RT from BGP VRF - L3VNI
4871 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4873 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
,
4878 * Derive AUTO export RT for BGP VRF - L3VNI
4880 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4882 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
, true);
4886 * Delete AUTO export RT from BGP VRF - L3VNI
4888 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4890 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
,
4894 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4896 struct bgp
*bgp_evpn
= NULL
;
4897 struct listnode
*node
= NULL
;
4898 struct bgpevpn
*vpn
= NULL
;
4900 bgp_evpn
= bgp_get_evpn();
4904 /* update all type-5 routes */
4905 update_advertise_vrf_routes(bgp_vrf
);
4907 /* update all type-2 routes */
4908 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4909 update_routes_for_vni(bgp_evpn
, vpn
);
4913 * Handle autort change for a given VNI.
4915 static void update_autort_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4917 struct bgpevpn
*vpn
= bucket
->data
;
4919 if (!is_import_rt_configured(vpn
)) {
4920 if (is_vni_live(vpn
))
4921 bgp_evpn_uninstall_routes(bgp
, vpn
);
4922 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4923 list_delete_all_node(vpn
->import_rtl
);
4924 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4925 if (is_vni_live(vpn
))
4926 bgp_evpn_install_routes(bgp
, vpn
);
4928 if (!is_export_rt_configured(vpn
)) {
4929 list_delete_all_node(vpn
->export_rtl
);
4930 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4931 if (is_vni_live(vpn
))
4932 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4937 * Handle autort change for L3VNI.
4939 static void update_autort_l3vni(struct bgp
*bgp
)
4941 if ((CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
4942 && (CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)))
4945 if (!CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
)) {
4946 if (is_l3vni_live(bgp
))
4947 uninstall_routes_for_vrf(bgp
);
4949 /* Cleanup the RT to VRF mapping */
4950 bgp_evpn_unmap_vrf_from_its_rts(bgp
);
4952 /* Remove auto generated RT */
4953 evpn_auto_rt_import_delete_for_vrf(bgp
);
4955 list_delete_all_node(bgp
->vrf_import_rtl
);
4957 /* Map auto derive or configured RTs */
4958 evpn_auto_rt_import_add_for_vrf(bgp
);
4961 if (!CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
4962 list_delete_all_node(bgp
->vrf_export_rtl
);
4964 evpn_auto_rt_export_delete_for_vrf(bgp
);
4966 evpn_auto_rt_export_add_for_vrf(bgp
);
4968 if (is_l3vni_live(bgp
))
4969 bgp_evpn_map_vrf_to_its_rts(bgp
);
4972 if (!is_l3vni_live(bgp
))
4975 /* advertise type-5 routes if needed */
4976 update_advertise_vrf_routes(bgp
);
4978 /* install all remote routes belonging to this l3vni
4979 * into corresponding vrf
4981 install_routes_for_vrf(bgp
);
4988 /* withdraw type-5 route corresponding to ip prefix */
4989 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4990 afi_t afi
, safi_t safi
)
4993 struct prefix_evpn evp
;
4995 build_type5_prefix_from_ip_prefix(&evp
, p
);
4996 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4999 EC_BGP_EVPN_ROUTE_DELETE
,
5000 "%u failed to delete type-5 route for prefix %pFX in vrf %s",
5001 bgp_vrf
->vrf_id
, p
, vrf_id_to_name(bgp_vrf
->vrf_id
));
5004 /* withdraw all type-5 routes for an address family */
5005 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
5007 struct bgp_table
*table
= NULL
;
5008 struct bgp_dest
*dest
= NULL
;
5009 struct bgp_path_info
*pi
;
5011 table
= bgp_vrf
->rib
[afi
][safi
];
5012 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
5013 /* Only care about "selected" routes. Also ensure that
5014 * these are routes that are injectable into EVPN.
5016 /* TODO: Support for AddPath for EVPN. */
5017 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
5018 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
5019 && is_route_injectable_into_evpn(pi
)) {
5020 bgp_evpn_withdraw_type5_route(
5021 bgp_vrf
, bgp_dest_get_prefix(dest
), afi
,
5030 * evpn - enable advertisement of default g/w
5032 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
5033 safi_t safi
, bool add
)
5035 struct prefix ip_prefix
;
5037 /* form the default prefix 0.0.0.0/0 */
5038 memset(&ip_prefix
, 0, sizeof(ip_prefix
));
5039 ip_prefix
.family
= afi2family(afi
);
5042 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
5045 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
5052 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
5053 * to this function correspond to those of the source IP prefix (best
5054 * path in the case of the attr. In the case of a local prefix (when we
5055 * are advertising local subnets), the src_attr will be NULL.
5057 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
5058 struct attr
*src_attr
, afi_t afi
,
5062 struct prefix_evpn evp
;
5064 build_type5_prefix_from_ip_prefix(&evp
, p
);
5065 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
, afi
, safi
);
5067 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5068 "%u: Failed to create type-5 route for prefix %pFX",
5069 bgp_vrf
->vrf_id
, p
);
5072 /* Inject all prefixes of a particular address-family (currently, IPv4 or
5073 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
5074 * advertisement is enabled.
5076 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
5079 struct bgp_table
*table
= NULL
;
5080 struct bgp_dest
*dest
= NULL
;
5081 struct bgp_path_info
*pi
;
5083 table
= bgp_vrf
->rib
[afi
][safi
];
5084 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
5085 /* Need to identify the "selected" route entry to use its
5086 * attribute. Also, ensure that the route is injectable
5088 * TODO: Support for AddPath for EVPN.
5090 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
5091 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
5092 && is_route_injectable_into_evpn(pi
)) {
5094 /* apply the route-map */
5095 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
5096 route_map_result_t ret
;
5097 struct bgp_path_info tmp_pi
;
5098 struct bgp_path_info_extra tmp_pie
;
5099 struct attr tmp_attr
;
5101 tmp_attr
= *pi
->attr
;
5103 /* Fill temp path_info */
5104 prep_for_rmap_apply(&tmp_pi
, &tmp_pie
,
5108 RESET_FLAG(tmp_attr
.rmap_change_flags
);
5110 ret
= route_map_apply(
5111 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
5113 bgp_dest_get_prefix(dest
),
5115 if (ret
== RMAP_DENYMATCH
) {
5116 bgp_attr_flush(&tmp_attr
);
5119 bgp_evpn_advertise_type5_route(
5121 bgp_dest_get_prefix(dest
),
5122 &tmp_attr
, afi
, safi
);
5124 bgp_evpn_advertise_type5_route(
5126 bgp_dest_get_prefix(dest
),
5127 pi
->attr
, afi
, safi
);
5134 static void rt_list_remove_node(struct list
*rt_list
,
5135 struct ecommunity
*ecomdel
, bool is_l3
)
5137 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
5138 struct vrf_route_target
*l3rt
= NULL
;
5139 struct ecommunity
*ecom
= NULL
;
5142 for (ALL_LIST_ELEMENTS(rt_list
, node
, nnode
, l3rt
)) {
5143 if (ecommunity_match(l3rt
->ecom
, ecomdel
)) {
5144 evpn_vrf_rt_del(l3rt
);
5150 for (ALL_LIST_ELEMENTS(rt_list
, node
, nnode
, ecom
)) {
5151 if (ecommunity_match(ecom
, ecomdel
)) {
5152 ecommunity_free(&ecom
);
5161 list_delete_node(rt_list
, node_to_del
);
5164 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
,
5167 struct ecommunity
*ecom_auto
;
5168 struct ecommunity_val eval
;
5170 if (bgp
->advertise_autort_rfc8365
)
5171 vni
|= EVPN_AUTORT_VXLAN
;
5173 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
5175 ecom_auto
= ecommunity_new();
5176 ecommunity_add_val(ecom_auto
, &eval
, false, false);
5178 rt_list_remove_node(rtl
, ecom_auto
, is_l3
);
5180 ecommunity_free(&ecom_auto
);
5183 static void evpn_vrf_rt_routes_map(struct bgp
*bgp_vrf
)
5185 /* map VRFs to its RTs and install routes matching this new RT */
5186 if (is_l3vni_live(bgp_vrf
)) {
5187 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
5188 install_routes_for_vrf(bgp_vrf
);
5192 static void evpn_vrf_rt_routes_unmap(struct bgp
*bgp_vrf
)
5194 /* uninstall routes from vrf */
5195 if (is_l3vni_live(bgp_vrf
))
5196 uninstall_routes_for_vrf(bgp_vrf
);
5198 /* Cleanup the RT to VRF mapping */
5199 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5202 static bool rt_list_has_cfgd_rt(struct list
*rt_list
)
5204 struct listnode
*node
= NULL
, *nnode
= NULL
;
5205 struct vrf_route_target
*l3rt
= NULL
;
5207 for (ALL_LIST_ELEMENTS(rt_list
, node
, nnode
, l3rt
)) {
5208 if (!CHECK_FLAG(l3rt
->flags
, BGP_VRF_RT_AUTO
))
5215 static void unconfigure_import_rt_for_vrf_fini(struct bgp
*bgp_vrf
)
5217 if (!bgp_vrf
->vrf_import_rtl
)
5218 return; /* this should never fail */
5220 if (!is_l3vni_live(bgp_vrf
))
5221 return; /* Nothing to do if no vni */
5223 /* fall back to auto-generated RT if this was the last RT */
5224 if (list_isempty(bgp_vrf
->vrf_import_rtl
))
5225 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5228 static void unconfigure_export_rt_for_vrf_fini(struct bgp
*bgp_vrf
)
5231 if (!bgp_vrf
->vrf_export_rtl
)
5232 return; /* this should never fail */
5234 if (!is_l3vni_live(bgp_vrf
))
5235 return; /* Nothing to do if no vni */
5237 /* fall back to auto-generated RT if this was the last RT */
5238 if (list_isempty(bgp_vrf
->vrf_export_rtl
))
5239 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5241 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
5244 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
5245 struct ecommunity
*ecomadd
,
5248 struct vrf_route_target
*newrt
;
5250 newrt
= evpn_vrf_rt_new(ecomadd
);
5253 SET_FLAG(newrt
->flags
, BGP_VRF_RT_WILD
);
5255 evpn_vrf_rt_routes_unmap(bgp_vrf
);
5257 /* Remove auto generated RT if not configured */
5258 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_AUTO_RT_CFGD
))
5259 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
5261 /* Add the newly configured RT to RT list */
5262 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, newrt
);
5264 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
5266 evpn_vrf_rt_routes_map(bgp_vrf
);
5269 void bgp_evpn_configure_import_auto_rt_for_vrf(struct bgp
*bgp_vrf
)
5271 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_AUTO_RT_CFGD
))
5272 return; /* Already configured */
5274 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_AUTO_RT_CFGD
);
5276 if (!is_l3vni_live(bgp_vrf
))
5277 return; /* Wait for VNI before adding rts */
5279 evpn_vrf_rt_routes_unmap(bgp_vrf
);
5281 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5283 evpn_vrf_rt_routes_map(bgp_vrf
);
5286 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
5287 struct ecommunity
*ecomdel
)
5289 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5290 return; /* Already un-configured */
5292 evpn_vrf_rt_routes_unmap(bgp_vrf
);
5295 rt_list_remove_node(bgp_vrf
->vrf_import_rtl
, ecomdel
, true);
5297 if (!rt_list_has_cfgd_rt(bgp_vrf
->vrf_import_rtl
))
5298 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
5300 unconfigure_import_rt_for_vrf_fini(bgp_vrf
);
5302 evpn_vrf_rt_routes_map(bgp_vrf
);
5305 void bgp_evpn_unconfigure_import_auto_rt_for_vrf(struct bgp
*bgp_vrf
)
5307 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_AUTO_RT_CFGD
))
5308 return; /* Already un-configured */
5310 evpn_vrf_rt_routes_unmap(bgp_vrf
);
5312 /* remove auto-generated RT */
5313 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
5315 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_AUTO_RT_CFGD
);
5317 unconfigure_import_rt_for_vrf_fini(bgp_vrf
);
5319 evpn_vrf_rt_routes_map(bgp_vrf
);
5322 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
5323 struct ecommunity
*ecomadd
)
5325 struct vrf_route_target
*newrt
;
5327 newrt
= evpn_vrf_rt_new(ecomadd
);
5329 /* Remove auto generated RT if not configured */
5330 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_AUTO_RT_CFGD
))
5331 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
5333 /* Add the new RT to the RT list */
5334 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, newrt
);
5336 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
5338 if (is_l3vni_live(bgp_vrf
))
5339 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
5342 void bgp_evpn_configure_export_auto_rt_for_vrf(struct bgp
*bgp_vrf
)
5344 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_AUTO_RT_CFGD
))
5345 return; /* Already configured */
5347 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_AUTO_RT_CFGD
);
5349 if (!is_l3vni_live(bgp_vrf
))
5350 return; /* Wait for VNI before adding rts */
5352 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5354 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
5357 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
5358 struct ecommunity
*ecomdel
)
5360 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5361 return; /* Already un-configured */
5364 rt_list_remove_node(bgp_vrf
->vrf_export_rtl
, ecomdel
, true);
5366 if (!rt_list_has_cfgd_rt(bgp_vrf
->vrf_export_rtl
))
5367 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
5369 unconfigure_export_rt_for_vrf_fini(bgp_vrf
);
5372 void bgp_evpn_unconfigure_export_auto_rt_for_vrf(struct bgp
*bgp_vrf
)
5374 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_AUTO_RT_CFGD
))
5375 return; /* Already un-configured */
5377 /* remove auto-generated RT */
5378 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
5380 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_AUTO_RT_CFGD
);
5382 unconfigure_export_rt_for_vrf_fini(bgp_vrf
);
5386 * Handle change to BGP router id. This is invoked twice by the change
5387 * handler, first before the router id has been changed and then after
5388 * the router id has been changed. The first invocation will result in
5389 * local routes for all VNIs/VRF being deleted and withdrawn and the next
5390 * will result in the routes being re-advertised.
5392 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
5394 struct listnode
*node
;
5395 struct bgp
*bgp_vrf
;
5399 /* delete and withdraw all the type-5 routes
5400 stored in the global table for this vrf
5402 withdraw_router_id_vrf(bgp
);
5404 /* delete all the VNI routes (type-2/type-3) routes for all the
5407 hash_iterate(bgp
->vnihash
,
5408 (void (*)(struct hash_bucket
*,
5409 void *))withdraw_router_id_vni
,
5412 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
5413 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
5414 if (bgp_vrf
->evpn_info
->advertise_pip
&&
5415 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
5417 bgp_vrf
->evpn_info
->pip_ip
.s_addr
5423 /* Assign new default instance router-id */
5424 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
5425 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
5426 if (bgp_vrf
->evpn_info
->advertise_pip
&&
5427 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
5429 bgp_vrf
->evpn_info
->pip_ip
=
5431 /* advertise type-5 routes with
5434 update_advertise_vrf_routes(bgp_vrf
);
5439 /* advertise all routes in the vrf as type-5 routes with the new
5442 update_router_id_vrf(bgp
);
5444 /* advertise all the VNI routes (type-2/type-3) routes with the
5447 hash_iterate(bgp
->vnihash
,
5448 (void (*)(struct hash_bucket
*,
5449 void *))update_router_id_vni
,
5455 * Handle change to auto-RT algorithm - update and advertise local routes.
5457 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
5459 hash_iterate(bgp
->vnihash
,
5460 (void (*)(struct hash_bucket
*,
5461 void*))update_autort_vni
,
5464 update_autort_l3vni(bgp
);
5468 * Handle change to export RT - update and advertise local routes.
5470 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5472 return update_routes_for_vni(bgp
, vpn
);
5475 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
5478 delete_withdraw_vrf_routes(bgp_vrf
);
5480 update_advertise_vrf_routes(bgp_vrf
);
5484 * Handle change to RD. This is invoked twice by the change handler,
5485 * first before the RD has been changed and then after the RD has
5486 * been changed. The first invocation will result in local routes
5487 * of this VNI being deleted and withdrawn and the next will result
5488 * in the routes being re-advertised.
5490 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
5494 delete_withdraw_vni_routes(bgp
, vpn
);
5496 update_advertise_vni_routes(bgp
, vpn
);
5500 * Install routes for this VNI. Invoked upon change to Import RT.
5502 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5504 return install_routes_for_vni(bgp
, vpn
);
5508 * Uninstall all routes installed for this VNI. Invoked upon change
5511 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5513 return uninstall_routes_for_vni(bgp
, vpn
);
5517 * TODO: Hardcoded for a maximum of 2 VNIs right now
5519 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
5524 vni1
= label2vni(label
);
5525 if (num_labels
== 2) {
5526 vni2
= label2vni(label
+ 1);
5527 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
5529 snprintf(buf
, len
, "%u", vni1
);
5534 * Function to convert evpn route to json format.
5535 * NOTE: We don't use prefix2str as the output here is a bit different.
5537 void bgp_evpn_route2json(const struct prefix_evpn
*p
, json_object
*json
)
5539 char buf1
[ETHER_ADDR_STRLEN
];
5540 char buf2
[PREFIX2STR_BUFFER
];
5547 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
5549 switch (p
->prefix
.route_type
) {
5550 case BGP_EVPN_MAC_IP_ROUTE
:
5551 json_object_int_add(json
, "ethTag",
5552 p
->prefix
.macip_addr
.eth_tag
);
5553 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
5554 json_object_string_add(json
, "mac",
5555 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
5558 if (!is_evpn_prefix_ipaddr_none(p
)) {
5559 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
:
5561 prefixlen
= (family
== AF_INET
) ?
5562 IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
;
5563 inet_ntop(family
, &p
->prefix
.macip_addr
.ip
.ip
.addr
,
5564 buf2
, PREFIX2STR_BUFFER
);
5565 json_object_int_add(json
, "ipLen", prefixlen
);
5566 json_object_string_add(json
, "ip", buf2
);
5570 case BGP_EVPN_IMET_ROUTE
:
5571 json_object_int_add(json
, "ethTag",
5572 p
->prefix
.imet_addr
.eth_tag
);
5573 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
5574 prefixlen
= (family
== AF_INET
) ? IPV4_MAX_BITLEN
:
5576 inet_ntop(family
, &p
->prefix
.imet_addr
.ip
.ip
.addr
, buf2
,
5578 json_object_int_add(json
, "ipLen", prefixlen
);
5579 json_object_string_add(json
, "ip", buf2
);
5582 case BGP_EVPN_IP_PREFIX_ROUTE
:
5583 json_object_int_add(json
, "ethTag",
5584 p
->prefix
.prefix_addr
.eth_tag
);
5585 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
5586 inet_ntop(family
, &p
->prefix
.prefix_addr
.ip
.ip
.addr
,
5587 buf2
, sizeof(buf2
));
5588 json_object_int_add(json
, "ipLen",
5589 p
->prefix
.prefix_addr
.ip_prefix_length
);
5590 json_object_string_add(json
, "ip", buf2
);
5599 * Encode EVPN prefix in Update (MP_REACH)
5601 void bgp_evpn_encode_prefix(struct stream
*s
, const struct prefix
*p
,
5602 const struct prefix_rd
*prd
, mpls_label_t
*label
,
5603 uint32_t num_labels
, struct attr
*attr
,
5604 bool addpath_capable
, uint32_t addpath_tx_id
)
5606 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
5607 int len
, ipa_len
= 0;
5609 if (addpath_capable
)
5610 stream_putl(s
, addpath_tx_id
);
5613 stream_putc(s
, evp
->prefix
.route_type
);
5615 switch (evp
->prefix
.route_type
) {
5616 case BGP_EVPN_MAC_IP_ROUTE
:
5617 if (is_evpn_prefix_ipaddr_v4(evp
))
5618 ipa_len
= IPV4_MAX_BYTELEN
;
5619 else if (is_evpn_prefix_ipaddr_v6(evp
))
5620 ipa_len
= IPV6_MAX_BYTELEN
;
5621 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
5622 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
5623 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
5625 stream_putc(s
, len
);
5626 stream_put(s
, prd
->val
, 8); /* RD */
5628 stream_put(s
, &attr
->esi
, ESI_BYTES
);
5630 stream_put(s
, 0, 10);
5631 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
5632 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
5633 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
5634 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
5635 if (ipa_len
) /* IP */
5636 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
5638 /* 1st label is the L2 VNI */
5639 stream_put(s
, label
, BGP_LABEL_BYTES
);
5640 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
5641 if (ipa_len
&& num_labels
> 1)
5642 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
5645 case BGP_EVPN_IMET_ROUTE
:
5646 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
5647 stream_put(s
, prd
->val
, 8); /* RD */
5648 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
5649 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
5650 /* Originating Router's IP Addr */
5651 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
5654 case BGP_EVPN_ES_ROUTE
:
5655 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
5656 stream_put(s
, prd
->val
, 8); /* RD */
5657 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
5658 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
5660 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
5663 case BGP_EVPN_AD_ROUTE
:
5664 /* RD, ESI, EthTag, 1 VNI */
5665 len
= RD_BYTES
+ ESI_BYTES
+ EVPN_ETH_TAG_BYTES
+ BGP_LABEL_BYTES
;
5666 stream_putc(s
, len
);
5667 stream_put(s
, prd
->val
, RD_BYTES
); /* RD */
5668 stream_put(s
, evp
->prefix
.ead_addr
.esi
.val
, ESI_BYTES
); /* ESI */
5669 stream_putl(s
, evp
->prefix
.ead_addr
.eth_tag
); /* Ethernet Tag */
5670 stream_put(s
, label
, BGP_LABEL_BYTES
);
5673 case BGP_EVPN_IP_PREFIX_ROUTE
:
5674 /* TODO: AddPath support. */
5675 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
5683 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
5684 struct bgp_nlri
*packet
, int withdraw
)
5690 uint32_t addpath_id
;
5691 bool addpath_capable
;
5696 /* Start processing the NLRI - there may be multiple in the MP_REACH */
5698 lim
= pnt
+ packet
->length
;
5700 safi
= packet
->safi
;
5703 addpath_capable
= bgp_addpath_encode_rx(peer
, afi
, safi
);
5705 for (; pnt
< lim
; pnt
+= psize
) {
5706 /* Clear prefix structure. */
5707 memset(&p
, 0, sizeof(p
));
5709 /* Deal with path-id if AddPath is supported. */
5710 if (addpath_capable
) {
5711 /* When packet overflow occurs return immediately. */
5712 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
5713 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5715 memcpy(&addpath_id
, pnt
, BGP_ADDPATH_ID_LEN
);
5716 addpath_id
= ntohl(addpath_id
);
5717 pnt
+= BGP_ADDPATH_ID_LEN
;
5720 /* All EVPN NLRI types start with type and length. */
5722 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE
;
5727 /* When packet overflow occur return immediately. */
5728 if (pnt
+ psize
> lim
)
5729 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5732 case BGP_EVPN_MAC_IP_ROUTE
:
5733 if (process_type2_route(peer
, afi
, safi
,
5734 withdraw
? NULL
: attr
, pnt
,
5735 psize
, addpath_id
)) {
5738 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
5739 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5740 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE
;
5744 case BGP_EVPN_IMET_ROUTE
:
5745 if (process_type3_route(peer
, afi
, safi
,
5746 withdraw
? NULL
: attr
, pnt
,
5747 psize
, addpath_id
)) {
5750 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
5751 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5752 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE
;
5756 case BGP_EVPN_ES_ROUTE
:
5757 if (bgp_evpn_type4_route_process(peer
, afi
, safi
,
5758 withdraw
? NULL
: attr
, pnt
,
5759 psize
, addpath_id
)) {
5762 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
5763 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5764 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE
;
5768 case BGP_EVPN_AD_ROUTE
:
5769 if (bgp_evpn_type1_route_process(peer
, afi
, safi
,
5770 withdraw
? NULL
: attr
, pnt
,
5771 psize
, addpath_id
)) {
5774 "%u:%s - Error in processing EVPN type-1 NLRI size %d",
5775 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5776 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE1_SIZE
;
5780 case BGP_EVPN_IP_PREFIX_ROUTE
:
5781 if (process_type5_route(peer
, afi
, safi
,
5782 withdraw
? NULL
: attr
, pnt
,
5783 psize
, addpath_id
)) {
5786 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
5787 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5788 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE
;
5797 /* Packet length consistency check. */
5799 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH
;
5801 return BGP_NLRI_PARSE_OK
;
5805 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
5806 * The mapping will be used during route processing.
5807 * bgp_vrf: specific bgp vrf instance on which RT is configured
5809 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
5811 struct listnode
*node
, *nnode
;
5812 struct vrf_route_target
*l3rt
;
5814 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, l3rt
))
5815 map_vrf_to_rt(bgp_vrf
, l3rt
);
5819 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
5821 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
5823 struct listnode
*node
, *nnode
;
5824 struct vrf_route_target
*l3rt
;
5826 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, l3rt
))
5827 unmap_vrf_from_rt(bgp_vrf
, l3rt
);
5831 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
5832 * The mapping will be used during route processing.
5834 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5837 struct ecommunity_val
*eval
;
5838 struct listnode
*node
, *nnode
;
5839 struct ecommunity
*ecom
;
5841 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5842 for (i
= 0; i
< ecom
->size
; i
++) {
5843 eval
= (struct ecommunity_val
*)(ecom
->val
5845 * ECOMMUNITY_SIZE
));
5846 map_vni_to_rt(bgp
, vpn
, eval
);
5852 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
5854 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5857 struct ecommunity_val
*eval
;
5858 struct listnode
*node
, *nnode
;
5859 struct ecommunity
*ecom
;
5861 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5862 for (i
= 0; i
< ecom
->size
; i
++) {
5863 struct irt_node
*irt
;
5864 struct ecommunity_val eval_tmp
;
5866 eval
= (struct ecommunity_val
*)(ecom
->val
5868 * ECOMMUNITY_SIZE
));
5869 /* If using "automatic" RT, we only care about the
5870 * local-admin sub-field.
5871 * This is to facilitate using VNI as the RT for EBGP
5874 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5875 if (!is_import_rt_configured(vpn
))
5876 mask_ecom_global_admin(&eval_tmp
, eval
);
5878 irt
= lookup_import_rt(bgp
, &eval_tmp
);
5880 unmap_vni_from_rt(bgp
, vpn
, irt
);
5886 * Derive Import RT automatically for VNI and map VNI to RT.
5887 * The mapping will be used during route processing.
5889 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5891 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
, false);
5892 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
5895 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
5899 * Derive Export RT automatically for VNI.
5901 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5903 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
, false);
5904 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5908 * Derive RD automatically for VNI using passed information - it
5909 * is of the form RouterId:unique-id-for-vni.
5911 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5913 if (is_vrf_rd_configured(bgp
))
5916 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5920 * Derive RD automatically for VNI using passed information - it
5921 * is of the form RouterId:unique-id-for-vni.
5923 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5925 char buf
[BGP_EVPN_PREFIX_RD_LEN
];
5927 vpn
->prd
.family
= AF_UNSPEC
;
5928 vpn
->prd
.prefixlen
= 64;
5929 snprintfrr(buf
, sizeof(buf
), "%pI4:%hu", &bgp
->router_id
, vpn
->rd_id
);
5930 (void)str2prefix_rd(buf
, &vpn
->prd
);
5931 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5937 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5939 struct list
*inst
= bm
->bgp
;
5940 struct listnode
*node
;
5941 struct bgp
*bgp_vrf
;
5943 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5944 if (bgp_vrf
->l3vni
== vni
)
5954 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5956 struct bgpevpn
*vpn
;
5959 memset(&tmp
, 0, sizeof(tmp
));
5961 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5966 * Create a new vpn - invoked upon configuration or zebra notification.
5968 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5969 struct in_addr originator_ip
,
5970 vrf_id_t tenant_vrf_id
,
5971 struct in_addr mcast_grp
,
5972 ifindex_t svi_ifindex
)
5974 struct bgpevpn
*vpn
;
5976 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5978 /* Set values - RD and RT set to defaults. */
5980 vpn
->originator_ip
= originator_ip
;
5981 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5982 vpn
->mcast_grp
= mcast_grp
;
5983 vpn
->svi_ifindex
= svi_ifindex
;
5985 /* Initialize route-target import and export lists */
5986 vpn
->import_rtl
= list_new();
5987 vpn
->import_rtl
->cmp
=
5988 (int (*)(void *, void *))bgp_evpn_route_target_cmp
;
5989 vpn
->import_rtl
->del
= bgp_evpn_xxport_delete_ecomm
;
5990 vpn
->export_rtl
= list_new();
5991 vpn
->export_rtl
->cmp
=
5992 (int (*)(void *, void *))bgp_evpn_route_target_cmp
;
5993 vpn
->export_rtl
->del
= bgp_evpn_xxport_delete_ecomm
;
5994 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5995 derive_rd_rt_for_vni(bgp
, vpn
);
5997 /* Initialize EVPN route tables. */
5998 vpn
->ip_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5999 vpn
->mac_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
6002 (void)hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
);
6004 bgp_evpn_remote_ip_hash_init(vpn
);
6005 bgp_evpn_link_to_vni_svi_hash(bgp
, vpn
);
6007 /* add to l2vni list on corresponding vrf */
6008 bgpevpn_link_to_l3vni(vpn
);
6010 bgp_evpn_vni_es_init(vpn
);
6012 QOBJ_REG(vpn
, bgpevpn
);
6017 * Free a given VPN - called in multiple scenarios such as zebra
6018 * notification, configuration being deleted, advertise-all-vni disabled etc.
6019 * This just frees appropriate memory, caller should have taken other
6022 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
6024 bgp_evpn_remote_ip_hash_destroy(vpn
);
6025 bgp_evpn_vni_es_cleanup(vpn
);
6026 bgpevpn_unlink_from_l3vni(vpn
);
6027 bgp_table_unlock(vpn
->ip_table
);
6028 bgp_table_unlock(vpn
->mac_table
);
6029 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
6030 list_delete(&vpn
->import_rtl
);
6031 list_delete(&vpn
->export_rtl
);
6032 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
6033 hash_release(bgp
->vni_svi_hash
, vpn
);
6034 hash_release(bgp
->vnihash
, vpn
);
6036 XFREE(MTYPE_BGP_EVPN
, vpn
);
6039 static void hash_evpn_free(struct bgpevpn
*vpn
)
6041 XFREE(MTYPE_BGP_EVPN
, vpn
);
6045 * Import evpn route from global table to VNI/VRF/ESI.
6047 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
6048 const struct prefix
*p
, struct bgp_path_info
*pi
)
6050 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
6054 * Unimport evpn route from VNI/VRF/ESI.
6056 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
6057 const struct prefix
*p
, struct bgp_path_info
*pi
)
6059 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
6062 /* filter routes which have martian next hops */
6063 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
6067 struct bgp_dest
*rd_dest
, *dest
;
6068 struct bgp_table
*table
;
6069 struct bgp_path_info
*pi
;
6074 /* Walk entire global routing table and evaluate routes which could be
6075 * imported into this VPN. Note that we cannot just look at the routes
6076 * for the VNI's RD -
6077 * remote routes applicable for this VNI could have any RD.
6079 /* EVPN routes are a 2-level table. */
6080 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
6081 rd_dest
= bgp_route_next(rd_dest
)) {
6082 table
= bgp_dest_get_bgp_table_info(rd_dest
);
6086 for (dest
= bgp_table_top(table
); dest
;
6087 dest
= bgp_route_next(dest
)) {
6089 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
6092 /* Consider "valid" remote routes applicable for
6094 if (!(pi
->type
== ZEBRA_ROUTE_BGP
6095 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
6097 if (bgp_nexthop_self(bgp
, afi
, pi
->type
,
6098 pi
->sub_type
, pi
->attr
,
6100 const struct prefix
*p
=
6101 bgp_dest_get_prefix(dest
);
6103 if (bgp_debug_update(pi
->peer
, p
, NULL
,
6105 char attr_str
[BUFSIZ
] = {0};
6107 bgp_dump_attr(pi
->attr
,
6112 "%u: prefix %pBD with attr %s - DENIED due to martian or self nexthop",
6116 bgp_evpn_unimport_route(bgp
, afi
, safi
,
6119 bgp_rib_remove(dest
, pi
, pi
->peer
, afi
,
6130 * Handle del of a local MACIP.
6132 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
6133 struct ipaddr
*ip
, int state
)
6135 struct bgpevpn
*vpn
;
6136 struct prefix_evpn p
;
6137 struct bgp_dest
*dest
;
6139 /* Lookup VNI hash - should exist. */
6140 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
6141 if (!vpn
|| !is_vni_live(vpn
)) {
6142 flog_warn(EC_BGP_EVPN_VPN_VNI
,
6143 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
6144 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
6148 build_evpn_type2_prefix(&p
, mac
, ip
);
6149 if (state
== ZEBRA_NEIGH_ACTIVE
) {
6150 /* Remove EVPN type-2 route and schedule for processing. */
6151 delete_evpn_route(bgp
, vpn
, &p
);
6153 /* Re-instate the current remote best path if any */
6154 dest
= bgp_evpn_vni_node_lookup(vpn
, &p
, NULL
);
6156 evpn_zebra_reinstall_best_route(bgp
, vpn
, dest
);
6157 bgp_dest_unlock_node(dest
);
6165 * Handle add of a local MACIP.
6167 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
6168 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
, esi_t
*esi
)
6170 struct bgpevpn
*vpn
;
6171 struct prefix_evpn p
;
6173 /* Lookup VNI hash - should exist. */
6174 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
6175 if (!vpn
|| !is_vni_live(vpn
)) {
6176 flog_warn(EC_BGP_EVPN_VPN_VNI
,
6177 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
6178 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
6182 /* Create EVPN type-2 route and schedule for processing. */
6183 build_evpn_type2_prefix(&p
, mac
, ip
);
6184 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
, esi
)) {
6186 EC_BGP_EVPN_ROUTE_CREATE
,
6187 "%u:Failed to create Type-2 route, VNI %u %s MAC %pEA IP %pIA (flags: 0x%x)",
6188 bgp
->vrf_id
, vpn
->vni
,
6189 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
6199 static void link_l2vni_hash_to_l3vni(struct hash_bucket
*bucket
,
6200 struct bgp
*bgp_vrf
)
6202 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
6203 struct bgp
*bgp_evpn
= NULL
;
6205 bgp_evpn
= bgp_get_evpn();
6208 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
6209 bgpevpn_link_to_l3vni(vpn
);
6212 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
,
6213 struct ethaddr
*svi_rmac
,
6214 struct ethaddr
*vrr_rmac
,
6215 struct in_addr originator_ip
, int filter
,
6216 ifindex_t svi_ifindex
,
6217 bool is_anycast_mac
)
6219 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
6220 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
6221 struct listnode
*node
= NULL
;
6222 struct bgpevpn
*vpn
= NULL
;
6225 /* get the EVPN instance - required to get the AS number for VRF
6228 bgp_evpn
= bgp_get_evpn();
6232 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
6238 /* if the BGP vrf instance doesn't exist - create one */
6239 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
6244 ret
= bgp_get_vty(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
6245 vrf_id
== VRF_DEFAULT
6246 ? BGP_INSTANCE_TYPE_DEFAULT
6247 : BGP_INSTANCE_TYPE_VRF
);
6249 case BGP_ERR_AS_MISMATCH
:
6250 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
6251 "BGP instance is already running; AS is %u",
6254 case BGP_ERR_INSTANCE_MISMATCH
:
6255 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
6256 "BGP instance type mismatch");
6260 /* mark as auto created */
6261 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
6264 /* associate the vrf with l3vni and related parameters */
6265 bgp_vrf
->l3vni
= l3vni
;
6266 bgp_vrf
->originator_ip
= originator_ip
;
6267 bgp_vrf
->l3vni_svi_ifindex
= svi_ifindex
;
6268 bgp_vrf
->evpn_info
->is_anycast_mac
= is_anycast_mac
;
6270 /* copy anycast MAC from VRR MAC */
6271 memcpy(&bgp_vrf
->rmac
, vrr_rmac
, ETH_ALEN
);
6272 /* copy sys RMAC from SVI MAC */
6273 memcpy(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, svi_rmac
, ETH_ALEN
);
6274 /* PIP user configured mac is not present use svi mac as sys mac */
6275 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
))
6276 memcpy(&bgp_vrf
->evpn_info
->pip_rmac
, svi_rmac
, ETH_ALEN
);
6278 if (bgp_debug_zebra(NULL
))
6280 "VRF %s vni %u pip %s RMAC %pEA sys RMAC %pEA static RMAC %pEA is_anycast_mac %s",
6281 vrf_id_to_name(bgp_vrf
->vrf_id
), bgp_vrf
->l3vni
,
6282 bgp_vrf
->evpn_info
->advertise_pip
? "enable"
6284 &bgp_vrf
->rmac
, &bgp_vrf
->evpn_info
->pip_rmac
,
6285 &bgp_vrf
->evpn_info
->pip_rmac_static
,
6286 is_anycast_mac
? "Enable" : "Disable");
6288 /* set the right filter - are we using l3vni only for prefix routes? */
6290 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
6293 * VNI_FLAG_USE_TWO_LABELS flag for linked L2VNIs should not be
6294 * set before linking vrf to L3VNI. Thus, no need to clear
6298 UNSET_FLAG(bgp_vrf
->vrf_flags
,
6299 BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
6301 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
6302 if (!CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
6305 * If we are flapping VNI_FLAG_USE_TWO_LABELS
6306 * flag, update all MACIP routes in this VNI
6308 SET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
6309 update_all_type2_routes(bgp_evpn
, vpn
);
6314 /* Map auto derive or configured RTs */
6315 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
) ||
6316 CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_AUTO_RT_CFGD
))
6317 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
6319 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
6321 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
) ||
6322 CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_AUTO_RT_CFGD
))
6323 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
6325 /* auto derive RD */
6326 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
6328 /* link all corresponding l2vnis */
6329 hash_iterate(bgp_evpn
->vnihash
,
6330 (void (*)(struct hash_bucket
*,
6331 void *))link_l2vni_hash_to_l3vni
,
6334 /* Only update all corresponding type-2 routes if we are advertising two
6335 * labels along with type-2 routes
6338 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
6339 update_routes_for_vni(bgp_evpn
, vpn
);
6341 /* advertise type-5 routes if needed */
6342 update_advertise_vrf_routes(bgp_vrf
);
6344 /* install all remote routes belonging to this l3vni into correspondng
6346 install_routes_for_vrf(bgp_vrf
);
6351 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
6353 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
6354 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
6355 struct listnode
*node
= NULL
;
6356 struct listnode
*next
= NULL
;
6357 struct bgpevpn
*vpn
= NULL
;
6359 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
6363 "Cannot process L3VNI %u Del - Could not find BGP instance",
6368 bgp_evpn
= bgp_get_evpn();
6372 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
6377 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
6378 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
6379 * routes. This will uninstalling the routes from zebra and decremnt the
6382 uninstall_routes_for_vrf(bgp_vrf
);
6384 /* delete/withdraw all type-5 routes */
6385 delete_withdraw_vrf_routes(bgp_vrf
);
6387 /* remove the l3vni from vrf instance */
6390 /* remove the Rmac from the BGP vrf */
6391 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
6392 memset(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, 0, ETH_ALEN
);
6393 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
) &&
6394 !is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac
))
6395 memset(&bgp_vrf
->evpn_info
->pip_rmac
, 0, ETH_ALEN
);
6397 /* remove default import RT or Unmap non-default import RT */
6398 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
6399 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
6400 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
6401 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
6404 /* remove default export RT */
6405 if (!list_isempty(bgp_vrf
->vrf_export_rtl
) &&
6406 !CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
6407 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
6410 /* update all corresponding local mac-ip routes */
6411 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
6412 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
6413 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
6414 update_routes_for_vni(bgp_evpn
, vpn
);
6418 /* If any L2VNIs point to this instance, unlink them. */
6419 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
6420 bgpevpn_unlink_from_l3vni(vpn
);
6422 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
6424 /* Delete the instance if it was autocreated */
6425 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
6426 bgp_delete(bgp_vrf
);
6432 * Handle del of a local VNI.
6434 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
6436 struct bgpevpn
*vpn
;
6438 /* Locate VNI hash */
6439 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
6443 /* Remove all local EVPN routes and schedule for processing (to
6444 * withdraw from peers).
6446 delete_routes_for_vni(bgp
, vpn
);
6448 bgp_evpn_unlink_from_vni_svi_hash(bgp
, vpn
);
6450 vpn
->svi_ifindex
= 0;
6452 * tunnel is no longer active, del tunnel ip address from tip_hash
6454 bgp_tip_del(bgp
, &vpn
->originator_ip
);
6456 /* Clear "live" flag and see if hash needs to be freed. */
6457 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
6458 if (!is_vni_configured(vpn
))
6459 bgp_evpn_free(bgp
, vpn
);
6465 * Handle add (or update) of a local VNI. The VNI changes we care
6466 * about are for the local-tunnel-ip and the (tenant) VRF.
6468 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
6469 struct in_addr originator_ip
,
6470 vrf_id_t tenant_vrf_id
,
6471 struct in_addr mcast_grp
,
6472 ifindex_t svi_ifindex
)
6474 struct bgpevpn
*vpn
;
6475 struct prefix_evpn p
;
6477 /* Lookup VNI. If present and no change, exit. */
6478 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
6481 if (is_vni_live(vpn
)
6482 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
6483 && IPV4_ADDR_SAME(&vpn
->mcast_grp
, &mcast_grp
)
6484 && vpn
->tenant_vrf_id
== tenant_vrf_id
6485 && vpn
->svi_ifindex
== svi_ifindex
)
6486 /* Probably some other param has changed that we don't
6490 bgp_evpn_mcast_grp_change(bgp
, vpn
, mcast_grp
);
6492 if (vpn
->svi_ifindex
!= svi_ifindex
) {
6495 * Unresolve all the gateway IP nexthops for this VNI
6498 bgp_evpn_remote_ip_hash_iterate(
6500 (void (*)(struct hash_bucket
*, void *))
6501 bgp_evpn_remote_ip_hash_unlink_nexthop
,
6503 bgp_evpn_unlink_from_vni_svi_hash(bgp
, vpn
);
6504 vpn
->svi_ifindex
= svi_ifindex
;
6505 bgp_evpn_link_to_vni_svi_hash(bgp
, vpn
);
6508 * Resolve all the gateway IP nexthops for this VNI
6511 bgp_evpn_remote_ip_hash_iterate(
6513 (void (*)(struct hash_bucket
*, void *))
6514 bgp_evpn_remote_ip_hash_link_nexthop
,
6518 /* Update tenant_vrf_id if it has changed. */
6519 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
6522 * Unresolve all the gateway IP nexthops for this VNI
6525 bgp_evpn_remote_ip_hash_iterate(
6527 (void (*)(struct hash_bucket
*, void *))
6528 bgp_evpn_remote_ip_hash_unlink_nexthop
,
6530 bgpevpn_unlink_from_l3vni(vpn
);
6531 vpn
->tenant_vrf_id
= tenant_vrf_id
;
6532 bgpevpn_link_to_l3vni(vpn
);
6535 * Resolve all the gateway IP nexthops for this VNI
6538 bgp_evpn_remote_ip_hash_iterate(
6540 (void (*)(struct hash_bucket
*, void *))
6541 bgp_evpn_remote_ip_hash_link_nexthop
,
6545 /* If tunnel endpoint IP has changed, update (and delete prior
6546 * type-3 route, if needed.)
6548 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
6549 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
6551 /* Update all routes with new endpoint IP and/or export RT
6554 if (is_vni_live(vpn
))
6555 update_routes_for_vni(bgp
, vpn
);
6557 /* Create or update as appropriate. */
6558 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
,
6559 mcast_grp
, svi_ifindex
);
6562 /* if the VNI is live already, there is nothing more to do */
6563 if (is_vni_live(vpn
))
6566 /* Mark as "live" */
6567 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
6569 /* tunnel is now active, add tunnel-ip to db */
6570 bgp_tip_add(bgp
, &originator_ip
);
6572 /* filter routes as nexthop database has changed */
6573 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
6576 * Create EVPN type-3 route and schedule for processing.
6578 * RT-3 only if doing head-end replication
6580 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
6581 == VXLAN_FLOOD_HEAD_END_REPL
) {
6582 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
6583 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
)) {
6584 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
6585 "%u: Type3 route creation failure for VNI %u",
6591 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
6595 install_routes_for_vni(bgp
, vpn
);
6597 /* If we are advertising gateway mac-ip
6598 It needs to be conveyed again to zebra */
6599 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
6601 /* advertise svi mac-ip knob to zebra */
6602 bgp_zebra_advertise_svi_macip(bgp
, vpn
->advertise_svi_macip
, vpn
->vni
);
6608 * Handle change in setting for BUM handling. The supported values
6609 * are head-end replication and dropping all BUM packets. Any change
6610 * should be registered with zebra. Also, if doing head-end replication,
6611 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
6612 * to be dropped, the RT-3s must be withdrawn.
6614 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
6616 zlog_info("L2VPN EVPN BUM handling is %s",
6617 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
6618 "Flooding" : "Flooding Disabled");
6620 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
6621 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
6622 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
6623 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
6624 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
6628 * Cleanup EVPN information on disable - Need to delete and withdraw
6629 * EVPN routes from peers.
6631 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
6633 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_bucket
*,
6634 void *))cleanup_vni_on_disable
,
6639 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
6640 * BGP instance (default) is being freed.
6642 void bgp_evpn_cleanup(struct bgp
*bgp
)
6644 hash_iterate(bgp
->vnihash
,
6645 (void (*)(struct hash_bucket
*, void *))free_vni_entry
,
6648 hash_clean(bgp
->import_rt_hash
, (void (*)(void *))hash_import_rt_free
);
6649 hash_free(bgp
->import_rt_hash
);
6650 bgp
->import_rt_hash
= NULL
;
6652 hash_clean(bgp
->vrf_import_rt_hash
,
6653 (void (*)(void *))hash_vrf_import_rt_free
);
6654 hash_free(bgp
->vrf_import_rt_hash
);
6655 bgp
->vrf_import_rt_hash
= NULL
;
6657 hash_clean(bgp
->vni_svi_hash
, (void (*)(void *))hash_evpn_free
);
6658 hash_free(bgp
->vni_svi_hash
);
6659 bgp
->vni_svi_hash
= NULL
;
6660 hash_free(bgp
->vnihash
);
6661 bgp
->vnihash
= NULL
;
6663 list_delete(&bgp
->vrf_import_rtl
);
6664 list_delete(&bgp
->vrf_export_rtl
);
6665 list_delete(&bgp
->l2vnis
);
6669 * Initialization for EVPN
6672 * hash for RT to VNI
6674 void bgp_evpn_init(struct bgp
*bgp
)
6677 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
6679 hash_create(vni_svi_hash_key_make
, vni_svi_hash_cmp
,
6680 "BGP VNI hash based on SVI ifindex");
6681 bgp
->import_rt_hash
=
6682 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
6683 "BGP Import RT Hash");
6684 bgp
->vrf_import_rt_hash
=
6685 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
6686 "BGP VRF Import RT Hash");
6687 bgp
->vrf_import_rtl
= list_new();
6688 bgp
->vrf_import_rtl
->cmp
=
6689 (int (*)(void *, void *))evpn_vrf_route_target_cmp
;
6690 bgp
->vrf_import_rtl
->del
= evpn_vrf_rt_del
;
6691 bgp
->vrf_export_rtl
= list_new();
6692 bgp
->vrf_export_rtl
->cmp
=
6693 (int (*)(void *, void *))evpn_vrf_route_target_cmp
;
6694 bgp
->vrf_export_rtl
->del
= evpn_vrf_rt_del
;
6695 bgp
->l2vnis
= list_new();
6696 bgp
->l2vnis
->cmp
= vni_list_cmp
;
6697 /* By default Duplicate Address Dection is enabled.
6698 * Max-moves (N) 5, detection time (M) 180
6699 * default action is warning-only
6700 * freeze action permanently freezes address,
6701 * and freeze time (auto-recovery) is disabled.
6703 if (bgp
->evpn_info
) {
6704 bgp
->evpn_info
->dup_addr_detect
= true;
6705 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
6706 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
6707 bgp
->evpn_info
->dad_freeze
= false;
6708 bgp
->evpn_info
->dad_freeze_time
= 0;
6709 /* Initialize zebra vxlan */
6710 bgp_zebra_dup_addr_detection(bgp
);
6711 /* Enable PIP feature by default for bgp vrf instance */
6712 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
6713 struct bgp
*bgp_default
;
6715 bgp
->evpn_info
->advertise_pip
= true;
6716 bgp_default
= bgp_get_default();
6718 bgp
->evpn_info
->pip_ip
= bgp_default
->router_id
;
6722 /* Default BUM handling is to do head-end replication. */
6723 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
6725 bgp_evpn_nh_init(bgp
);
6728 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
6730 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
6731 bgp_evpn_nh_finish(bgp_vrf
);
6735 * Get the prefixlen of the ip prefix carried within the type5 evpn route.
6737 int bgp_evpn_get_type5_prefixlen(const struct prefix
*pfx
)
6739 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6741 if (!pfx
|| pfx
->family
!= AF_EVPN
)
6744 if (evp
->prefix
.route_type
!= BGP_EVPN_IP_PREFIX_ROUTE
)
6747 return evp
->prefix
.prefix_addr
.ip_prefix_length
;
6751 * Should we register nexthop for this EVPN prefix for nexthop tracking?
6753 bool bgp_evpn_is_prefix_nht_supported(const struct prefix
*pfx
)
6755 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6758 * EVPN routes should be marked as valid only if the nexthop is
6759 * reachable. Only if this happens, the route should be imported
6760 * (into VNI or VRF routing tables) and/or advertised.
6761 * Note: This is currently applied for EVPN type-1, type-2,
6762 * type-3, type-4 and type-5 routes.
6763 * It may be tweaked later on for other routes, or
6764 * even removed completely when all routes are handled.
6766 if (pfx
&& pfx
->family
== AF_EVPN
6767 && (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
6768 || evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
6769 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
6770 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
6771 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
6777 static void *bgp_evpn_remote_ip_hash_alloc(void *p
)
6779 const struct evpn_remote_ip
*key
= (const struct evpn_remote_ip
*)p
;
6780 struct evpn_remote_ip
*ip
;
6782 ip
= XMALLOC(MTYPE_EVPN_REMOTE_IP
, sizeof(struct evpn_remote_ip
));
6784 ip
->macip_path_list
= list_new();
6789 static unsigned int bgp_evpn_remote_ip_hash_key_make(const void *p
)
6791 const struct evpn_remote_ip
*ip
= p
;
6792 const struct ipaddr
*addr
= &ip
->addr
;
6794 if (IS_IPADDR_V4(addr
))
6795 return jhash_1word(addr
->ipaddr_v4
.s_addr
, 0);
6797 return jhash2(addr
->ipaddr_v6
.s6_addr32
,
6798 array_size(addr
->ipaddr_v6
.s6_addr32
), 0);
6801 static bool bgp_evpn_remote_ip_hash_cmp(const void *p1
, const void *p2
)
6803 const struct evpn_remote_ip
*ip1
= p1
;
6804 const struct evpn_remote_ip
*ip2
= p2
;
6806 return !ipaddr_cmp(&ip1
->addr
, &ip2
->addr
);
6809 static void bgp_evpn_remote_ip_hash_init(struct bgpevpn
*vpn
)
6811 if (!evpn_resolve_overlay_index())
6814 vpn
->remote_ip_hash
= hash_create(bgp_evpn_remote_ip_hash_key_make
,
6815 bgp_evpn_remote_ip_hash_cmp
,
6816 "BGP EVPN remote IP hash");
6819 static void bgp_evpn_remote_ip_hash_free(struct hash_bucket
*bucket
, void *args
)
6821 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6822 struct bgpevpn
*vpn
= (struct bgpevpn
*)args
;
6824 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, false);
6826 list_delete(&ip
->macip_path_list
);
6828 hash_release(vpn
->remote_ip_hash
, ip
);
6829 XFREE(MTYPE_EVPN_REMOTE_IP
, ip
);
6832 static void bgp_evpn_remote_ip_hash_destroy(struct bgpevpn
*vpn
)
6834 if (!evpn_resolve_overlay_index() || vpn
->remote_ip_hash
== NULL
)
6837 hash_iterate(vpn
->remote_ip_hash
,
6838 (void (*)(struct hash_bucket
*, void *))bgp_evpn_remote_ip_hash_free
,
6841 hash_free(vpn
->remote_ip_hash
);
6842 vpn
->remote_ip_hash
= NULL
;
6845 /* Add a remote MAC/IP route to hash table */
6846 static void bgp_evpn_remote_ip_hash_add(struct bgpevpn
*vpn
,
6847 struct bgp_path_info
*pi
)
6849 struct evpn_remote_ip tmp
;
6850 struct evpn_remote_ip
*ip
;
6851 struct prefix_evpn
*evp
;
6853 if (!evpn_resolve_overlay_index())
6856 if (pi
->type
!= ZEBRA_ROUTE_BGP
|| pi
->sub_type
!= BGP_ROUTE_IMPORTED
6857 || !CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
))
6860 evp
= (struct prefix_evpn
*)&pi
->net
->p
;
6862 if (evp
->family
!= AF_EVPN
6863 || evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
6864 || is_evpn_prefix_ipaddr_none(evp
))
6867 tmp
.addr
= evp
->prefix
.macip_addr
.ip
;
6868 ip
= hash_lookup(vpn
->remote_ip_hash
, &tmp
);
6870 if (listnode_lookup(ip
->macip_path_list
, pi
) != NULL
)
6872 (void)listnode_add(ip
->macip_path_list
, pi
);
6876 ip
= hash_get(vpn
->remote_ip_hash
, &tmp
, bgp_evpn_remote_ip_hash_alloc
);
6877 (void)listnode_add(ip
->macip_path_list
, pi
);
6879 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, true);
6882 /* Delete a remote MAC/IP route from hash table */
6883 static void bgp_evpn_remote_ip_hash_del(struct bgpevpn
*vpn
,
6884 struct bgp_path_info
*pi
)
6886 struct evpn_remote_ip tmp
;
6887 struct evpn_remote_ip
*ip
;
6888 struct prefix_evpn
*evp
;
6890 if (!evpn_resolve_overlay_index())
6893 evp
= (struct prefix_evpn
*)&pi
->net
->p
;
6895 if (evp
->family
!= AF_EVPN
6896 || evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
6897 || is_evpn_prefix_ipaddr_none(evp
))
6900 tmp
.addr
= evp
->prefix
.macip_addr
.ip
;
6901 ip
= hash_lookup(vpn
->remote_ip_hash
, &tmp
);
6905 listnode_delete(ip
->macip_path_list
, pi
);
6907 if (ip
->macip_path_list
->count
== 0) {
6908 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, false);
6909 hash_release(vpn
->remote_ip_hash
, ip
);
6910 list_delete(&ip
->macip_path_list
);
6911 XFREE(MTYPE_EVPN_REMOTE_IP
, ip
);
6915 static void bgp_evpn_remote_ip_hash_iterate(struct bgpevpn
*vpn
,
6916 void (*func
)(struct hash_bucket
*,
6920 if (!evpn_resolve_overlay_index())
6923 hash_iterate(vpn
->remote_ip_hash
, func
, arg
);
6926 static void show_remote_ip_entry(struct hash_bucket
*bucket
, void *args
)
6928 char buf
[INET6_ADDRSTRLEN
];
6929 struct listnode
*node
= NULL
;
6930 struct bgp_path_info
*pi
= NULL
;
6931 struct vty
*vty
= (struct vty
*)args
;
6932 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6934 vty_out(vty
, " Remote IP: %s\n",
6935 ipaddr2str(&ip
->addr
, buf
, sizeof(buf
)));
6936 vty_out(vty
, " Linked MAC/IP routes:\n");
6937 for (ALL_LIST_ELEMENTS_RO(ip
->macip_path_list
, node
, pi
))
6938 vty_out(vty
, " %pFX\n", &pi
->net
->p
);
6941 void bgp_evpn_show_remote_ip_hash(struct hash_bucket
*bucket
, void *args
)
6943 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
6944 struct vty
*vty
= (struct vty
*)args
;
6946 vty_out(vty
, "VNI: %u\n", vpn
->vni
);
6947 bgp_evpn_remote_ip_hash_iterate(
6949 (void (*)(struct hash_bucket
*, void *))show_remote_ip_entry
,
6954 static void bgp_evpn_remote_ip_hash_link_nexthop(struct hash_bucket
*bucket
,
6957 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6958 struct bgpevpn
*vpn
= (struct bgpevpn
*)args
;
6960 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, true);
6963 static void bgp_evpn_remote_ip_hash_unlink_nexthop(struct hash_bucket
*bucket
,
6966 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6967 struct bgpevpn
*vpn
= (struct bgpevpn
*)args
;
6969 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, false);
6972 static unsigned int vni_svi_hash_key_make(const void *p
)
6974 const struct bgpevpn
*vpn
= p
;
6976 return jhash_1word(vpn
->svi_ifindex
, 0);
6979 static bool vni_svi_hash_cmp(const void *p1
, const void *p2
)
6981 const struct bgpevpn
*vpn1
= p1
;
6982 const struct bgpevpn
*vpn2
= p2
;
6984 return (vpn1
->svi_ifindex
== vpn2
->svi_ifindex
);
6987 static struct bgpevpn
*bgp_evpn_vni_svi_hash_lookup(struct bgp
*bgp
,
6990 struct bgpevpn
*vpn
;
6993 memset(&tmp
, 0, sizeof(tmp
));
6994 tmp
.svi_ifindex
= svi
;
6995 vpn
= hash_lookup(bgp
->vni_svi_hash
, &tmp
);
6999 static void bgp_evpn_link_to_vni_svi_hash(struct bgp
*bgp
, struct bgpevpn
*vpn
)
7001 if (vpn
->svi_ifindex
== 0)
7004 (void)hash_get(bgp
->vni_svi_hash
, vpn
, hash_alloc_intern
);
7007 static void bgp_evpn_unlink_from_vni_svi_hash(struct bgp
*bgp
,
7008 struct bgpevpn
*vpn
)
7010 if (vpn
->svi_ifindex
== 0)
7013 hash_release(bgp
->vni_svi_hash
, vpn
);
7016 void bgp_evpn_show_vni_svi_hash(struct hash_bucket
*bucket
, void *args
)
7018 struct bgpevpn
*evpn
= (struct bgpevpn
*)bucket
->data
;
7019 struct vty
*vty
= (struct vty
*)args
;
7021 vty_out(vty
, "SVI: %u VNI: %u\n", evpn
->svi_ifindex
, evpn
->vni
);
7025 * This function is called for a bgp_nexthop_cache entry when the nexthop is
7026 * gateway IP overlay index.
7027 * This function returns true if there is a remote MAC/IP route for the gateway
7028 * IP in the EVI of the nexthop SVI.
7030 bool bgp_evpn_is_gateway_ip_resolved(struct bgp_nexthop_cache
*bnc
)
7032 struct bgp
*bgp_evpn
= NULL
;
7033 struct bgpevpn
*vpn
= NULL
;
7034 struct evpn_remote_ip tmp
;
7037 if (!evpn_resolve_overlay_index())
7040 if (!bnc
->nexthop
|| bnc
->nexthop
->ifindex
== 0)
7043 bgp_evpn
= bgp_get_evpn();
7048 * Gateway IP is resolved by nht over SVI interface.
7049 * Use this SVI to find corresponding EVI(L2 context)
7051 vpn
= bgp_evpn_vni_svi_hash_lookup(bgp_evpn
, bnc
->nexthop
->ifindex
);
7055 if (vpn
->bgp_vrf
!= bnc
->bgp
)
7059 * Check if the gateway IP is present in the EVI remote_ip_hash table
7060 * which stores all the remote IP addresses received via MAC/IP routes
7063 memset(&tmp
, 0, sizeof(tmp
));
7066 if (p
->family
== AF_INET
) {
7067 tmp
.addr
.ipa_type
= IPADDR_V4
;
7068 memcpy(&(tmp
.addr
.ipaddr_v4
), &(p
->u
.prefix4
),
7069 sizeof(struct in_addr
));
7070 } else if (p
->family
== AF_INET6
) {
7071 tmp
.addr
.ipa_type
= IPADDR_V6
;
7072 memcpy(&(tmp
.addr
.ipaddr_v6
), &(p
->u
.prefix6
),
7073 sizeof(struct in6_addr
));
7077 if (hash_lookup(vpn
->remote_ip_hash
, &tmp
) == NULL
)
7083 /* Resolve/Unresolve nexthops when a MAC/IP route is added/deleted */
7084 static void bgp_evpn_remote_ip_process_nexthops(struct bgpevpn
*vpn
,
7085 struct ipaddr
*addr
,
7090 struct bgp_nexthop_cache
*bnc
;
7091 struct bgp_nexthop_cache_head
*tree
= NULL
;
7093 if (!vpn
->bgp_vrf
|| vpn
->svi_ifindex
== 0)
7096 memset(&p
, 0, sizeof(p
));
7098 if (addr
->ipa_type
== IPADDR_V4
) {
7101 memcpy(&(p
.u
.prefix4
), &(addr
->ipaddr_v4
),
7102 sizeof(struct in_addr
));
7103 p
.prefixlen
= IPV4_MAX_BITLEN
;
7104 } else if (addr
->ipa_type
== IPADDR_V6
) {
7106 p
.family
= AF_INET6
;
7107 memcpy(&(p
.u
.prefix6
), &(addr
->ipaddr_v6
),
7108 sizeof(struct in6_addr
));
7109 p
.prefixlen
= IPV6_MAX_BITLEN
;
7113 tree
= &vpn
->bgp_vrf
->nexthop_cache_table
[afi
];
7114 bnc
= bnc_find(tree
, &p
, 0, 0);
7116 if (!bnc
|| !bnc
->is_evpn_gwip_nexthop
)
7119 if (!bnc
->nexthop
|| bnc
->nexthop
->ifindex
!= vpn
->svi_ifindex
)
7122 if (BGP_DEBUG(nht
, NHT
))
7123 zlog_debug("%s(%u): vni %u mac/ip %s for NH %pFX",
7124 vpn
->bgp_vrf
->name_pretty
, vpn
->tenant_vrf_id
,
7125 vpn
->vni
, (resolve
? "add" : "delete"),
7129 * MAC/IP route or SVI or tenant vrf being added to EVI.
7130 * Set nexthop as valid only if it is already L3 reachable
7132 if (resolve
&& bnc
->flags
& BGP_NEXTHOP_EVPN_INCOMPLETE
) {
7133 bnc
->flags
&= ~BGP_NEXTHOP_EVPN_INCOMPLETE
;
7134 bnc
->flags
|= BGP_NEXTHOP_VALID
;
7135 bnc
->change_flags
|= BGP_NEXTHOP_MACIP_CHANGED
;
7136 evaluate_paths(bnc
);
7139 /* MAC/IP route or SVI or tenant vrf being deleted from EVI */
7140 if (!resolve
&& bnc
->flags
& BGP_NEXTHOP_VALID
) {
7141 bnc
->flags
&= ~BGP_NEXTHOP_VALID
;
7142 bnc
->flags
|= BGP_NEXTHOP_EVPN_INCOMPLETE
;
7143 bnc
->change_flags
|= BGP_NEXTHOP_MACIP_CHANGED
;
7144 evaluate_paths(bnc
);
7148 void bgp_evpn_handle_resolve_overlay_index_set(struct hash_bucket
*bucket
,
7151 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
7152 struct bgp_dest
*dest
;
7153 struct bgp_path_info
*pi
;
7155 bgp_evpn_remote_ip_hash_init(vpn
);
7157 for (dest
= bgp_table_top(vpn
->ip_table
); dest
;
7158 dest
= bgp_route_next(dest
))
7159 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
7160 bgp_evpn_remote_ip_hash_add(vpn
, pi
);
7163 void bgp_evpn_handle_resolve_overlay_index_unset(struct hash_bucket
*bucket
,
7166 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
7168 bgp_evpn_remote_ip_hash_destroy(vpn
);