1 /* Ethernet-VPN Packet and vty Processing File
2 * Copyright (C) 2016 6WIND
3 * Copyright (C) 2017 Cumulus Networks, Inc.
5 * This file is part of FRR.
7 * FRRouting is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * FRRouting is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "lib/printfrr.h"
36 #include "bgpd/bgp_attr_evpn.h"
37 #include "bgpd/bgpd.h"
38 #include "bgpd/bgp_table.h"
39 #include "bgpd/bgp_route.h"
40 #include "bgpd/bgp_attr.h"
41 #include "bgpd/bgp_mplsvpn.h"
42 #include "bgpd/bgp_label.h"
43 #include "bgpd/bgp_evpn.h"
44 #include "bgpd/bgp_evpn_private.h"
45 #include "bgpd/bgp_evpn_mh.h"
46 #include "bgpd/bgp_ecommunity.h"
47 #include "bgpd/bgp_encap_types.h"
48 #include "bgpd/bgp_debug.h"
49 #include "bgpd/bgp_errors.h"
50 #include "bgpd/bgp_aspath.h"
51 #include "bgpd/bgp_zebra.h"
52 #include "bgpd/bgp_nexthop.h"
53 #include "bgpd/bgp_addpath.h"
54 #include "bgpd/bgp_mac.h"
55 #include "bgpd/bgp_vty.h"
56 #include "bgpd/bgp_nht.h"
57 #include "bgpd/bgp_trace.h"
60 * Definitions and external declarations.
62 DEFINE_QOBJ_TYPE(bgpevpn
);
63 DEFINE_QOBJ_TYPE(bgp_evpn_es
);
67 * Static function declarations
69 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
70 static void bgp_evpn_remote_ip_hash_init(struct bgpevpn
*evpn
);
71 static void bgp_evpn_remote_ip_hash_destroy(struct bgpevpn
*evpn
);
72 static void bgp_evpn_remote_ip_hash_add(struct bgpevpn
*vpn
,
73 struct bgp_path_info
*pi
);
74 static void bgp_evpn_remote_ip_hash_del(struct bgpevpn
*vpn
,
75 struct bgp_path_info
*pi
);
76 static void bgp_evpn_remote_ip_hash_iterate(struct bgpevpn
*vpn
,
77 void (*func
)(struct hash_bucket
*,
80 static void bgp_evpn_link_to_vni_svi_hash(struct bgp
*bgp
, struct bgpevpn
*vpn
);
81 static void bgp_evpn_unlink_from_vni_svi_hash(struct bgp
*bgp
,
83 static unsigned int vni_svi_hash_key_make(const void *p
);
84 static bool vni_svi_hash_cmp(const void *p1
, const void *p2
);
85 static void bgp_evpn_remote_ip_process_nexthops(struct bgpevpn
*vpn
,
88 static void bgp_evpn_remote_ip_hash_link_nexthop(struct hash_bucket
*bucket
,
90 static void bgp_evpn_remote_ip_hash_unlink_nexthop(struct hash_bucket
*bucket
,
92 static struct in_addr zero_vtep_ip
;
101 static unsigned int vni_hash_key_make(const void *p
)
103 const struct bgpevpn
*vpn
= p
;
104 return (jhash_1word(vpn
->vni
, 0));
108 * Comparison function for vni hash
110 static bool vni_hash_cmp(const void *p1
, const void *p2
)
112 const struct bgpevpn
*vpn1
= p1
;
113 const struct bgpevpn
*vpn2
= p2
;
115 return vpn1
->vni
== vpn2
->vni
;
118 int vni_list_cmp(void *p1
, void *p2
)
120 const struct bgpevpn
*vpn1
= p1
;
121 const struct bgpevpn
*vpn2
= p2
;
123 return vpn1
->vni
- vpn2
->vni
;
127 * Make vrf import route target hash key.
129 static unsigned int vrf_import_rt_hash_key_make(const void *p
)
131 const struct vrf_irt_node
*irt
= p
;
132 const char *pnt
= irt
->rt
.val
;
134 return jhash(pnt
, 8, 0x5abc1234);
138 * Comparison function for vrf import rt hash
140 static bool vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
142 const struct vrf_irt_node
*irt1
= p1
;
143 const struct vrf_irt_node
*irt2
= p2
;
145 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
149 * Create a new vrf import_rt in evpn instance
151 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
153 struct bgp
*bgp_evpn
= NULL
;
154 struct vrf_irt_node
*irt
;
156 bgp_evpn
= bgp_get_evpn();
158 flog_err(EC_BGP_NO_DFLT
,
159 "vrf import rt new - evpn instance not created yet");
163 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
164 sizeof(struct vrf_irt_node
));
167 irt
->vrfs
= list_new();
170 (void)hash_get(bgp_evpn
->vrf_import_rt_hash
, irt
, hash_alloc_intern
);
176 * Free the vrf import rt node
178 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
180 struct bgp
*bgp_evpn
= NULL
;
182 bgp_evpn
= bgp_get_evpn();
184 flog_err(EC_BGP_NO_DFLT
,
185 "vrf import rt free - evpn instance not created yet");
189 hash_release(bgp_evpn
->vrf_import_rt_hash
, irt
);
190 list_delete(&irt
->vrfs
);
191 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
195 * Function to lookup Import RT node - used to map a RT to set of
196 * VNIs importing routes with that RT.
198 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
200 struct bgp
*bgp_evpn
= NULL
;
201 struct vrf_irt_node
*irt
;
202 struct vrf_irt_node tmp
;
204 bgp_evpn
= bgp_get_evpn();
208 "vrf import rt lookup - evpn instance not created yet");
212 memset(&tmp
, 0, sizeof(tmp
));
213 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
214 irt
= hash_lookup(bgp_evpn
->vrf_import_rt_hash
, &tmp
);
219 * Is specified VRF present on the RT's list of "importing" VRFs?
221 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
, struct bgp
*bgp_vrf
)
223 struct listnode
*node
= NULL
, *nnode
= NULL
;
224 struct bgp
*tmp_bgp_vrf
= NULL
;
226 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
227 if (tmp_bgp_vrf
== bgp_vrf
)
234 * Make import route target hash key.
236 static unsigned int import_rt_hash_key_make(const void *p
)
238 const struct irt_node
*irt
= p
;
239 const char *pnt
= irt
->rt
.val
;
241 return jhash(pnt
, 8, 0xdeadbeef);
245 * Comparison function for import rt hash
247 static bool import_rt_hash_cmp(const void *p1
, const void *p2
)
249 const struct irt_node
*irt1
= p1
;
250 const struct irt_node
*irt2
= p2
;
252 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
256 * Create a new import_rt
258 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
259 struct ecommunity_val
*rt
)
261 struct irt_node
*irt
;
263 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
266 irt
->vnis
= list_new();
269 (void)hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
);
275 * Free the import rt node
277 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
279 hash_release(bgp
->import_rt_hash
, irt
);
280 list_delete(&irt
->vnis
);
281 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
285 * Function to lookup Import RT node - used to map a RT to set of
286 * VNIs importing routes with that RT.
288 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
289 struct ecommunity_val
*rt
)
291 struct irt_node
*irt
;
294 memset(&tmp
, 0, sizeof(tmp
));
295 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
296 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
301 * Is specified VNI present on the RT's list of "importing" VNIs?
303 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
305 struct listnode
*node
, *nnode
;
306 struct bgpevpn
*tmp_vpn
;
308 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
317 * Compare Route Targets.
319 int bgp_evpn_route_target_cmp(struct ecommunity
*ecom1
,
320 struct ecommunity
*ecom2
)
328 if (!ecom1
&& !ecom2
)
331 if (ecom1
->str
&& !ecom2
->str
)
334 if (!ecom1
->str
&& ecom2
->str
)
337 if (!ecom1
->str
&& !ecom2
->str
)
340 return strcmp(ecom1
->str
, ecom2
->str
);
343 void bgp_evpn_xxport_delete_ecomm(void *val
)
345 struct ecommunity
*ecomm
= val
;
346 ecommunity_free(&ecomm
);
350 * Mask off global-admin field of specified extended community (RT),
351 * just retain the local-admin field.
353 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
354 struct ecommunity_val
*src
)
360 if (type
== ECOMMUNITY_ENCODE_AS
) {
361 dst
->val
[2] = dst
->val
[3] = 0;
362 } else if (type
== ECOMMUNITY_ENCODE_AS4
363 || type
== ECOMMUNITY_ENCODE_IP
) {
364 dst
->val
[2] = dst
->val
[3] = 0;
365 dst
->val
[4] = dst
->val
[5] = 0;
370 * Map one RT to specified VRF.
371 * bgp_vrf = BGP vrf instance
373 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct ecommunity_val
*eval
)
375 struct vrf_irt_node
*irt
= NULL
;
376 struct ecommunity_val eval_tmp
;
378 /* If using "automatic" RT,
379 * we only care about the local-admin sub-field.
380 * This is to facilitate using L3VNI(VRF-VNI)
381 * as the RT for EBGP peering too.
383 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
384 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
385 mask_ecom_global_admin(&eval_tmp
, eval
);
387 irt
= lookup_vrf_import_rt(&eval_tmp
);
388 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
389 /* Already mapped. */
393 irt
= vrf_import_rt_new(&eval_tmp
);
395 /* Add VRF to the list for this RT. */
396 listnode_add(irt
->vrfs
, bgp_vrf
);
400 * Unmap specified VRF from specified RT. If there are no other
401 * VRFs for this RT, then the RT hash is deleted.
402 * bgp_vrf: BGP VRF specific instance
404 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
, struct vrf_irt_node
*irt
)
406 /* Delete VRF from list for this RT. */
407 listnode_delete(irt
->vrfs
, bgp_vrf
);
408 if (!listnode_head(irt
->vrfs
)) {
409 vrf_import_rt_free(irt
);
414 * Map one RT to specified VNI.
416 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
417 struct ecommunity_val
*eval
)
419 struct irt_node
*irt
;
420 struct ecommunity_val eval_tmp
;
422 /* If using "automatic" RT, we only care about the local-admin
424 * This is to facilitate using VNI as the RT for EBGP peering too.
426 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
427 if (!is_import_rt_configured(vpn
))
428 mask_ecom_global_admin(&eval_tmp
, eval
);
430 irt
= lookup_import_rt(bgp
, &eval_tmp
);
432 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
433 /* Already mapped. */
437 irt
= import_rt_new(bgp
, &eval_tmp
);
439 /* Add VNI to the hash list for this RT. */
440 listnode_add(irt
->vnis
, vpn
);
444 * Unmap specified VNI from specified RT. If there are no other
445 * VNIs for this RT, then the RT hash is deleted.
447 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
448 struct irt_node
*irt
)
450 /* Delete VNI from hash list for this RT. */
451 listnode_delete(irt
->vnis
, vpn
);
452 if (!listnode_head(irt
->vnis
)) {
453 import_rt_free(bgp
, irt
);
457 static void bgp_evpn_get_rmac_nexthop(struct bgpevpn
*vpn
,
458 const struct prefix_evpn
*p
,
459 struct attr
*attr
, uint8_t flags
)
461 struct bgp
*bgp_vrf
= vpn
->bgp_vrf
;
463 memset(&attr
->rmac
, 0, sizeof(struct ethaddr
));
467 if (p
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
470 /* Copy sys (pip) RMAC and PIP IP as nexthop
471 * in case of route is self MAC-IP,
472 * advertise-pip and advertise-svi-ip features
474 * Otherwise, for all host MAC-IP route's
477 if (CHECK_FLAG(flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
)
478 && bgp_vrf
->evpn_info
->advertise_pip
&&
479 bgp_vrf
->evpn_info
->is_anycast_mac
) {
481 memcpy(&attr
->rmac
, &bgp_vrf
->evpn_info
->pip_rmac
,
483 attr
->nexthop
= bgp_vrf
->evpn_info
->pip_ip
;
484 attr
->mp_nexthop_global_in
=
485 bgp_vrf
->evpn_info
->pip_ip
;
487 memcpy(&attr
->rmac
, &bgp_vrf
->rmac
, ETH_ALEN
);
491 * Create RT extended community automatically from passed information:
492 * of the form AS:VNI.
493 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
494 * the need is to get a RT value that will be unique across different
495 * VNIs but the same across routers (in the same AS) for a particular
498 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
500 struct ecommunity_val eval
;
501 struct ecommunity
*ecomadd
, *ecom
;
502 bool ecom_found
= false;
503 struct listnode
*node
;
505 if (bgp
->advertise_autort_rfc8365
)
506 vni
|= EVPN_AUTORT_VXLAN
;
507 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
509 ecomadd
= ecommunity_new();
510 ecommunity_add_val(ecomadd
, &eval
, false, false);
511 for (ALL_LIST_ELEMENTS_RO(rtl
, node
, ecom
))
512 if (ecommunity_cmp(ecomadd
, ecom
)) {
518 listnode_add_sort(rtl
, ecomadd
);
520 ecommunity_free(&ecomadd
);
524 * Derive RD and RT for a VNI automatically. Invoked at the time of
527 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
529 bgp_evpn_derive_auto_rd(bgp
, vpn
);
530 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
531 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
535 * Convert nexthop (remote VTEP IP) into an IPv6 address.
537 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
539 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
541 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
542 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
545 struct bgp_dest
*bgp_global_evpn_node_get(struct bgp_table
*table
, afi_t afi
,
547 const struct prefix_evpn
*evp
,
548 struct prefix_rd
*prd
)
550 struct prefix_evpn global_p
;
552 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
) {
553 /* prefix in the global table doesn't include the VTEP-IP so
554 * we need to create a different copy of the prefix
556 evpn_type1_prefix_global_copy(&global_p
, evp
);
559 return bgp_afi_node_get(table
, afi
, safi
, (struct prefix
*)evp
, prd
);
562 struct bgp_dest
*bgp_global_evpn_node_lookup(struct bgp_table
*table
, afi_t afi
,
564 const struct prefix_evpn
*evp
,
565 struct prefix_rd
*prd
)
567 struct prefix_evpn global_p
;
569 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
) {
570 /* prefix in the global table doesn't include the VTEP-IP so
571 * we need to create a different copy of the prefix
573 evpn_type1_prefix_global_copy(&global_p
, evp
);
576 return bgp_afi_node_lookup(table
, afi
, safi
, (struct prefix
*)evp
, prd
);
580 * Add (update) or delete MACIP from zebra.
582 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
583 const struct prefix_evpn
*p
,
584 struct in_addr remote_vtep_ip
, int add
,
585 uint8_t flags
, uint32_t seq
, esi_t
*esi
)
589 static struct in_addr zero_remote_vtep_ip
;
592 if (!zclient
|| zclient
->sock
< 0)
595 /* Don't try to register if Zebra doesn't know of this instance. */
596 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
597 if (BGP_DEBUG(zebra
, ZEBRA
))
599 "%s: No zebra instance to talk to, not installing remote macip",
609 zclient_create_header(
610 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
612 stream_putl(s
, vpn
->vni
);
613 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
614 /* IP address length and IP address, if any. */
615 if (is_evpn_prefix_ipaddr_none(p
))
618 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
620 stream_putw(s
, ipa_len
);
621 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
623 /* If the ESI is valid that becomes the nexthop; tape out the
624 * VTEP-IP for that case
626 if (bgp_evpn_is_esi_valid(esi
))
627 stream_put_in_addr(s
, &zero_remote_vtep_ip
);
629 stream_put_in_addr(s
, &remote_vtep_ip
);
631 /* TX flags - MAC sticky status and/or gateway mac */
632 /* Also TX the sequence number of the best route. */
634 stream_putc(s
, flags
);
636 stream_put(s
, esi
, sizeof(esi_t
));
639 stream_putw_at(s
, 0, stream_get_endp(s
));
641 if (bgp_debug_zebra(NULL
))
643 "Tx %s MACIP, VNI %u MAC %pEA IP %pIA flags 0x%x seq %u remote VTEP %pI4",
644 add
? "ADD" : "DEL", vpn
->vni
,
645 &p
->prefix
.macip_addr
.mac
, &p
->prefix
.macip_addr
.ip
,
646 flags
, seq
, &remote_vtep_ip
);
648 frrtrace(5, frr_bgp
, evpn_mac_ip_zsend
, add
, vpn
, p
, remote_vtep_ip
,
651 return zclient_send_message(zclient
);
655 * Add (update) or delete remote VTEP from zebra.
657 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
658 const struct prefix_evpn
*p
,
659 int flood_control
, int add
)
664 if (!zclient
|| zclient
->sock
< 0)
667 /* Don't try to register if Zebra doesn't know of this instance. */
668 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
669 if (BGP_DEBUG(zebra
, ZEBRA
))
671 "%s: No zebra instance to talk to, not installing remote vtep",
679 zclient_create_header(
680 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
682 stream_putl(s
, vpn
->vni
);
683 if (is_evpn_prefix_ipaddr_v4(p
))
684 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
685 else if (is_evpn_prefix_ipaddr_v6(p
)) {
688 "Bad remote IP when trying to %s remote VTEP for VNI %u",
689 add
? "ADD" : "DEL", vpn
->vni
);
692 stream_putl(s
, flood_control
);
694 stream_putw_at(s
, 0, stream_get_endp(s
));
696 if (bgp_debug_zebra(NULL
))
697 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %pI4",
698 add
? "ADD" : "DEL", vpn
->vni
,
699 &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
701 frrtrace(3, frr_bgp
, evpn_bum_vtep_zsend
, add
, vpn
, p
);
703 return zclient_send_message(zclient
);
707 * Build extended communities for EVPN prefix route.
709 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
712 struct ecommunity ecom_encap
;
713 struct ecommunity_val eval
;
714 struct ecommunity_val eval_rmac
;
715 bgp_encap_types tnl_type
;
716 struct listnode
*node
, *nnode
;
717 struct ecommunity
*ecom
;
718 struct ecommunity
*old_ecom
;
719 struct list
*vrf_export_rtl
= NULL
;
722 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
723 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
724 encode_encap_extcomm(tnl_type
, &eval
);
726 ecom_encap
.unit_size
= ECOMMUNITY_SIZE
;
727 ecom_encap
.val
= (uint8_t *)eval
.val
;
730 if (bgp_attr_get_ecommunity(attr
)) {
731 old_ecom
= bgp_attr_get_ecommunity(attr
);
732 ecom
= ecommunity_merge(ecommunity_dup(old_ecom
), &ecom_encap
);
733 if (!old_ecom
->refcnt
)
734 ecommunity_free(&old_ecom
);
736 ecom
= ecommunity_dup(&ecom_encap
);
737 bgp_attr_set_ecommunity(attr
, ecom
);
738 attr
->encap_tunneltype
= tnl_type
;
740 /* Add the export RTs for L3VNI/VRF */
741 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
742 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
743 bgp_attr_set_ecommunity(
745 ecommunity_merge(bgp_attr_get_ecommunity(attr
), ecom
));
747 /* add the router mac extended community */
748 if (!is_zero_mac(&attr
->rmac
)) {
749 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
750 ecommunity_add_val(bgp_attr_get_ecommunity(attr
), &eval_rmac
,
756 * Build extended communities for EVPN route.
757 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
758 * and ENCAP extended communities are applicable for all routes.
759 * The default gateway extended community and MAC mobility (sticky) extended
760 * community are added as needed based on passed settings - only for type-2
761 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
762 * added, if present, based on passed settings - only for non-link-local
765 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
768 struct ecommunity ecom_encap
;
769 struct ecommunity ecom_sticky
;
770 struct ecommunity ecom_default_gw
;
771 struct ecommunity ecom_na
;
772 struct ecommunity_val eval
;
773 struct ecommunity_val eval_sticky
;
774 struct ecommunity_val eval_default_gw
;
775 struct ecommunity_val eval_rmac
;
776 struct ecommunity_val eval_na
;
779 bgp_encap_types tnl_type
;
780 struct listnode
*node
, *nnode
;
781 struct ecommunity
*ecom
;
783 struct list
*vrf_export_rtl
= NULL
;
786 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
787 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
788 encode_encap_extcomm(tnl_type
, &eval
);
790 ecom_encap
.unit_size
= ECOMMUNITY_SIZE
;
791 ecom_encap
.val
= (uint8_t *)eval
.val
;
794 bgp_attr_set_ecommunity(attr
, ecommunity_dup(&ecom_encap
));
795 attr
->encap_tunneltype
= tnl_type
;
797 /* Add the export RTs for L2VNI */
798 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
799 bgp_attr_set_ecommunity(
801 ecommunity_merge(bgp_attr_get_ecommunity(attr
), ecom
));
803 /* Add the export RTs for L3VNI if told to - caller determines
804 * when this should be done.
807 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
808 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
809 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
811 bgp_attr_set_ecommunity(
814 bgp_attr_get_ecommunity(attr
),
819 /* Add MAC mobility (sticky) if needed. */
822 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
823 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
824 ecom_sticky
.size
= 1;
825 ecom_sticky
.unit_size
= ECOMMUNITY_SIZE
;
826 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
827 bgp_attr_set_ecommunity(
828 attr
, ecommunity_merge(bgp_attr_get_ecommunity(attr
),
832 /* Add RMAC, if told to. */
834 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
835 ecommunity_add_val(bgp_attr_get_ecommunity(attr
), &eval_rmac
,
839 /* Add default gateway, if needed. */
840 if (attr
->default_gw
) {
841 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
842 encode_default_gw_extcomm(&eval_default_gw
);
843 ecom_default_gw
.size
= 1;
844 ecom_default_gw
.unit_size
= ECOMMUNITY_SIZE
;
845 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
846 bgp_attr_set_ecommunity(
847 attr
, ecommunity_merge(bgp_attr_get_ecommunity(attr
),
851 proxy
= !!(attr
->es_flags
& ATTR_ES_PROXY_ADVERT
);
852 if (attr
->router_flag
|| proxy
) {
853 memset(&ecom_na
, 0, sizeof(ecom_na
));
854 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
, proxy
);
856 ecom_na
.unit_size
= ECOMMUNITY_SIZE
;
857 ecom_na
.val
= (uint8_t *)eval_na
.val
;
858 bgp_attr_set_ecommunity(
859 attr
, ecommunity_merge(bgp_attr_get_ecommunity(attr
),
865 * Add MAC mobility extended community to attribute.
867 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
869 struct ecommunity ecom_tmp
;
870 struct ecommunity_val eval
;
871 uint8_t *ecom_val_ptr
;
876 struct ecommunity
*ecomm
= bgp_attr_get_ecommunity(attr
);
879 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
881 /* Find current MM ecommunity */
885 for (i
= 0; i
< ecomm
->size
; i
++) {
886 pnt
= ecomm
->val
+ (i
* ecomm
->unit_size
);
890 if (type
== ECOMMUNITY_ENCODE_EVPN
892 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
894 (ecomm
->val
+ (i
* ecomm
->unit_size
));
900 /* Update the existing MM ecommunity */
902 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ecomm
->unit_size
);
904 /* Add MM to existing */
906 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
908 ecom_tmp
.unit_size
= ECOMMUNITY_SIZE
;
909 ecom_tmp
.val
= (uint8_t *)eval
.val
;
912 bgp_attr_set_ecommunity(
913 attr
, ecommunity_merge(ecomm
, &ecom_tmp
));
915 bgp_attr_set_ecommunity(attr
,
916 ecommunity_dup(&ecom_tmp
));
920 /* Install EVPN route into zebra. */
921 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
922 const struct prefix_evpn
*p
,
923 struct bgp_path_info
*pi
)
930 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
933 if (pi
->sub_type
== BGP_ROUTE_IMPORTED
) {
934 if (pi
->attr
->sticky
)
935 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
936 if (pi
->attr
->default_gw
)
937 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
938 if (is_evpn_prefix_ipaddr_v6(p
) &&
939 pi
->attr
->router_flag
)
940 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
942 seq
= mac_mobility_seqnum(pi
->attr
);
943 /* if local ES notify zebra that this is a sync path */
944 if (bgp_evpn_attr_is_local_es(pi
->attr
)) {
945 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_SYNC_PATH
);
946 if (bgp_evpn_attr_is_proxy(pi
->attr
))
948 ZEBRA_MACIP_TYPE_PROXY_ADVERT
);
951 if (!bgp_evpn_attr_is_sync(pi
->attr
))
954 /* if a local path is being turned around and sent
955 * to zebra it is because it is a sync path on
958 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_SYNC_PATH
);
959 /* supply the highest peer seq number to zebra
962 seq
= bgp_evpn_attr_get_sync_seq(pi
->attr
);
963 /* if any of the paths from the peer have the ROUTER
964 * flag set install the local entry as a router entry
966 if (is_evpn_prefix_ipaddr_v6(p
) &&
967 (pi
->attr
->es_flags
&
968 ATTR_ES_PEER_ROUTER
))
970 ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
972 if (!(pi
->attr
->es_flags
& ATTR_ES_PEER_ACTIVE
))
974 ZEBRA_MACIP_TYPE_PROXY_ADVERT
);
977 ret
= bgp_zebra_send_remote_macip(
978 bgp
, vpn
, p
, pi
->attr
->nexthop
, 1, flags
,
979 seq
, bgp_evpn_attr_get_esi(pi
->attr
));
980 } else if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
) {
981 ret
= bgp_evpn_remote_es_evi_add(bgp
, vpn
, p
);
983 switch (bgp_attr_get_pmsi_tnl_type(pi
->attr
)) {
984 case PMSI_TNLTYPE_INGR_REPL
:
985 flood_control
= VXLAN_FLOOD_HEAD_END_REPL
;
988 case PMSI_TNLTYPE_PIM_SM
:
989 flood_control
= VXLAN_FLOOD_PIM_SM
;
993 flood_control
= VXLAN_FLOOD_DISABLED
;
996 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, flood_control
, 1);
1002 /* Uninstall EVPN route from zebra. */
1003 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1004 const struct prefix_evpn
*p
,
1005 struct in_addr remote_vtep_ip
)
1009 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1010 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
1012 else if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
1013 ret
= bgp_evpn_remote_es_evi_del(bgp
, vpn
, p
);
1015 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
,
1016 VXLAN_FLOOD_DISABLED
, 0);
1022 * Due to MAC mobility, the prior "local" best route has been supplanted
1023 * by a "remote" best route. The prior route has to be deleted and withdrawn
1026 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1027 struct bgp_dest
*dest
,
1028 struct bgp_path_info
*old_local
,
1029 struct bgp_path_info
*new_select
)
1031 struct bgp_dest
*global_dest
;
1032 struct bgp_path_info
*pi
;
1033 afi_t afi
= AFI_L2VPN
;
1034 safi_t safi
= SAFI_EVPN
;
1036 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
1037 char esi_buf
[ESI_STR_LEN
];
1038 char esi_buf2
[ESI_STR_LEN
];
1039 struct prefix_evpn
*evp
=
1040 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1042 zlog_debug("local path deleted %pFX es %s; new-path-es %s", evp
,
1043 esi_to_str(&old_local
->attr
->esi
, esi_buf
,
1045 new_select
? esi_to_str(&new_select
->attr
->esi
,
1046 esi_buf2
, sizeof(esi_buf2
))
1050 /* Locate route node in the global EVPN routing table. Note that
1051 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1054 global_dest
= bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1055 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1058 /* Delete route entry in the global EVPN table. */
1059 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
1061 /* Schedule for processing - withdraws to peers happen from
1065 bgp_process(bgp
, global_dest
, afi
, safi
);
1066 bgp_dest_unlock_node(global_dest
);
1069 /* Delete route entry in the VNI route table, caller to remove. */
1070 bgp_path_info_delete(dest
, old_local
);
1074 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1076 * Note: vpn is NULL for local EAD-ES routes.
1078 int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1079 struct bgp_dest
*dest
)
1081 struct bgp_path_info
*old_select
, *new_select
;
1082 struct bgp_path_info_pair old_and_new
;
1083 afi_t afi
= AFI_L2VPN
;
1084 safi_t safi
= SAFI_EVPN
;
1087 /* Compute the best path. */
1088 bgp_best_selection(bgp
, dest
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1090 old_select
= old_and_new
.old
;
1091 new_select
= old_and_new
.new;
1093 /* If the best path hasn't changed - see if there is still something to
1094 * update to zebra RIB.
1095 * Remote routes and SYNC route (i.e. local routes with
1096 * SYNCED_FROM_PEER flag) need to updated to zebra on any attr
1099 if (old_select
&& old_select
== new_select
1100 && old_select
->type
== ZEBRA_ROUTE_BGP
1101 && (old_select
->sub_type
== BGP_ROUTE_IMPORTED
||
1102 bgp_evpn_attr_is_sync(old_select
->attr
))
1103 && !CHECK_FLAG(dest
->flags
, BGP_NODE_USER_CLEAR
)
1104 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1105 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1106 if (bgp_zebra_has_route_changed(old_select
))
1107 ret
= evpn_zebra_install(
1109 (const struct prefix_evpn
*)bgp_dest_get_prefix(
1112 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1113 UNSET_FLAG(old_select
->flags
, BGP_PATH_LINK_BW_CHG
);
1114 bgp_zebra_clear_route_change_flags(dest
);
1118 /* If the user did a "clear" this flag will be set */
1119 UNSET_FLAG(dest
->flags
, BGP_NODE_USER_CLEAR
);
1121 /* bestpath has changed; update relevant fields and install or uninstall
1122 * into the zebra RIB.
1124 if (old_select
|| new_select
)
1125 bgp_bump_version(dest
);
1128 bgp_path_info_unset_flag(dest
, old_select
, BGP_PATH_SELECTED
);
1130 bgp_path_info_set_flag(dest
, new_select
, BGP_PATH_SELECTED
);
1131 bgp_path_info_unset_flag(dest
, new_select
,
1132 BGP_PATH_ATTR_CHANGED
);
1133 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1134 UNSET_FLAG(new_select
->flags
, BGP_PATH_LINK_BW_CHG
);
1137 /* a local entry with the SYNC flag also results in a MAC-IP update
1140 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1141 && (new_select
->sub_type
== BGP_ROUTE_IMPORTED
||
1142 bgp_evpn_attr_is_sync(new_select
->attr
))) {
1143 ret
= evpn_zebra_install(
1145 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1148 /* If an old best existed and it was a "local" route, the only
1150 * it would be supplanted is due to MAC mobility procedures. So,
1152 * need to do an implicit delete and withdraw that route from
1155 if (new_select
->sub_type
== BGP_ROUTE_IMPORTED
&&
1156 old_select
&& old_select
->peer
== bgp
->peer_self
1157 && old_select
->type
== ZEBRA_ROUTE_BGP
1158 && old_select
->sub_type
== BGP_ROUTE_STATIC
1160 evpn_delete_old_local_route(bgp
, vpn
, dest
,
1161 old_select
, new_select
);
1163 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1164 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1165 ret
= evpn_zebra_uninstall(
1167 (const struct prefix_evpn
*)bgp_dest_get_prefix(
1169 old_select
->attr
->nexthop
);
1172 /* Clear any route change flags. */
1173 bgp_zebra_clear_route_change_flags(dest
);
1175 /* Reap old select bgp_path_info, if it has been removed */
1176 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1177 bgp_path_info_reap(dest
, old_select
);
1182 static struct bgp_path_info
*bgp_evpn_route_get_local_path(
1183 struct bgp
*bgp
, struct bgp_dest
*dest
)
1185 struct bgp_path_info
*tmp_pi
;
1186 struct bgp_path_info
*local_pi
= NULL
;
1188 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1189 tmp_pi
= tmp_pi
->next
) {
1190 if (bgp_evpn_is_path_local(bgp
, tmp_pi
)) {
1199 static int update_evpn_type5_route_entry(struct bgp
*bgp_evpn
,
1200 struct bgp
*bgp_vrf
, afi_t afi
,
1201 safi_t safi
, struct bgp_dest
*dest
,
1202 struct attr
*attr
, int *route_changed
)
1204 struct attr
*attr_new
= NULL
;
1205 struct bgp_path_info
*pi
= NULL
;
1206 mpls_label_t label
= MPLS_INVALID_LABEL
;
1207 struct bgp_path_info
*local_pi
= NULL
;
1208 struct bgp_path_info
*tmp_pi
= NULL
;
1211 /* locate the local route entry if any */
1212 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1213 tmp_pi
= tmp_pi
->next
) {
1214 if (tmp_pi
->peer
== bgp_evpn
->peer_self
1215 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1216 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1221 * create a new route entry if one doesn't exist.
1222 * Otherwise see if route attr has changed
1226 /* route has changed as this is the first entry */
1229 /* Add (or update) attribute to hash. */
1230 attr_new
= bgp_attr_intern(attr
);
1232 /* create the route info from attribute */
1233 pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1234 bgp_evpn
->peer_self
, attr_new
, dest
);
1235 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
1237 /* Type-5 routes advertise the L3-VNI */
1238 bgp_path_info_extra_get(pi
);
1239 vni2label(bgp_vrf
->l3vni
, &label
);
1240 memcpy(&pi
->extra
->label
, &label
, sizeof(label
));
1241 pi
->extra
->num_labels
= 1;
1243 /* add the route entry to route node*/
1244 bgp_path_info_add(dest
, pi
);
1248 if (!attrhash_cmp(tmp_pi
->attr
, attr
)) {
1250 /* attribute changed */
1253 /* The attribute has changed. */
1254 /* Add (or update) attribute to hash. */
1255 attr_new
= bgp_attr_intern(attr
);
1256 bgp_path_info_set_flag(dest
, tmp_pi
,
1257 BGP_PATH_ATTR_CHANGED
);
1259 /* Restore route, if needed. */
1260 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1261 bgp_path_info_restore(dest
, tmp_pi
);
1263 /* Unintern existing, set to new. */
1264 bgp_attr_unintern(&tmp_pi
->attr
);
1265 tmp_pi
->attr
= attr_new
;
1266 tmp_pi
->uptime
= monotime(NULL
);
1272 /* update evpn type-5 route entry */
1273 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1274 struct attr
*src_attr
, afi_t src_afi
,
1277 afi_t afi
= AFI_L2VPN
;
1278 safi_t safi
= SAFI_EVPN
;
1280 struct bgp_dest
*dest
= NULL
;
1281 struct bgp
*bgp_evpn
= NULL
;
1282 int route_changed
= 0;
1284 bgp_evpn
= bgp_get_evpn();
1288 /* Build path attribute for this route - use the source attr, if
1289 * present, else treat as locally originated.
1294 memset(&attr
, 0, sizeof(attr
));
1295 bgp_attr_default_set(&attr
, bgp_vrf
, BGP_ORIGIN_IGP
);
1298 /* Advertise Primary IP (PIP) is enabled, send individual
1299 * IP (default instance router-id) as nexthop.
1300 * PIP is disabled or vrr interface is not present
1301 * use anycast-IP as nexthop and anycast RMAC.
1303 if (!bgp_vrf
->evpn_info
->advertise_pip
||
1304 (!bgp_vrf
->evpn_info
->is_anycast_mac
)) {
1305 attr
.nexthop
= bgp_vrf
->originator_ip
;
1306 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1307 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, ETH_ALEN
);
1310 memcpy(&attr
.rmac
, &bgp_vrf
->evpn_info
->pip_rmac
, ETH_ALEN
);
1311 if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
!= INADDR_ANY
) {
1312 attr
.nexthop
= bgp_vrf
->evpn_info
->pip_ip
;
1313 attr
.mp_nexthop_global_in
= bgp_vrf
->evpn_info
->pip_ip
;
1314 } else if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
== INADDR_ANY
)
1315 if (bgp_debug_zebra(NULL
))
1317 "VRF %s evp %pFX advertise-pip primary ip is not configured",
1318 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
);
1321 if (bgp_debug_zebra(NULL
))
1323 "VRF %s type-5 route evp %pFX RMAC %pEA nexthop %pI4",
1324 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
, &attr
.rmac
,
1327 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1329 if (src_afi
== AFI_IP6
&&
1330 CHECK_FLAG(bgp_vrf
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1331 BGP_L2VPN_EVPN_ADV_IPV6_UNICAST_GW_IP
)) {
1333 !IN6_IS_ADDR_UNSPECIFIED(&src_attr
->mp_nexthop_global
)) {
1334 attr
.evpn_overlay
.type
= OVERLAY_INDEX_GATEWAY_IP
;
1335 SET_IPADDR_V6(&attr
.evpn_overlay
.gw_ip
);
1336 memcpy(&attr
.evpn_overlay
.gw_ip
.ipaddr_v6
,
1337 &src_attr
->mp_nexthop_global
,
1338 sizeof(struct in6_addr
));
1340 } else if (src_afi
== AFI_IP
&&
1341 CHECK_FLAG(bgp_vrf
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1342 BGP_L2VPN_EVPN_ADV_IPV4_UNICAST_GW_IP
)) {
1343 if (src_attr
&& src_attr
->nexthop
.s_addr
!= 0) {
1344 attr
.evpn_overlay
.type
= OVERLAY_INDEX_GATEWAY_IP
;
1345 SET_IPADDR_V4(&attr
.evpn_overlay
.gw_ip
);
1346 memcpy(&attr
.evpn_overlay
.gw_ip
.ipaddr_v4
,
1347 &src_attr
->nexthop
, sizeof(struct in_addr
));
1351 /* Setup RT and encap extended community */
1352 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1354 /* get the route node in global table */
1355 dest
= bgp_global_evpn_node_get(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1356 (const struct prefix_evpn
*)evp
,
1360 /* create or update the route entry within the route node */
1361 update_evpn_type5_route_entry(bgp_evpn
, bgp_vrf
, afi
, safi
, dest
, &attr
,
1364 /* schedule for processing and unlock node */
1365 if (route_changed
) {
1366 bgp_process(bgp_evpn
, dest
, afi
, safi
);
1367 bgp_dest_unlock_node(dest
);
1370 /* uninten temporary */
1372 aspath_unintern(&attr
.aspath
);
1376 static void bgp_evpn_get_sync_info(struct bgp
*bgp
, esi_t
*esi
,
1377 struct bgp_dest
*dest
, uint32_t loc_seq
,
1378 uint32_t *max_sync_seq
, bool *active_on_peer
,
1379 bool *peer_router
, bool *proxy_from_peer
)
1381 struct bgp_path_info
*tmp_pi
;
1382 struct bgp_path_info
*second_best_path
= NULL
;
1383 uint32_t tmp_mm_seq
= 0;
1387 /* find the best non-local path. a local path can only be present
1390 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1391 tmp_pi
= tmp_pi
->next
) {
1392 if (tmp_pi
->sub_type
!= BGP_ROUTE_IMPORTED
||
1393 !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1396 if (bgp_evpn_path_info_cmp(bgp
, tmp_pi
,
1397 second_best_path
, &paths_eq
))
1398 second_best_path
= tmp_pi
;
1401 if (!second_best_path
)
1404 tmp_esi
= bgp_evpn_attr_get_esi(second_best_path
->attr
);
1405 /* if this has the same ES desination as the local path
1408 if (!memcmp(esi
, tmp_esi
, sizeof(esi_t
))) {
1409 tmp_mm_seq
= mac_mobility_seqnum(second_best_path
->attr
);
1410 if (tmp_mm_seq
< loc_seq
)
1413 /* we have a non-proxy path from the ES peer. */
1414 if (second_best_path
->attr
->es_flags
&
1415 ATTR_ES_PROXY_ADVERT
) {
1416 *proxy_from_peer
= true;
1418 *active_on_peer
= true;
1421 if (second_best_path
->attr
->router_flag
)
1422 *peer_router
= true;
1424 /* we use both proxy and non-proxy imports to
1425 * determine the max sync sequence
1427 if (tmp_mm_seq
> *max_sync_seq
)
1428 *max_sync_seq
= tmp_mm_seq
;
1432 /* Bubble up sync-info from all paths (non-best) to the local-path.
1433 * This is need for MM sequence number syncing and proxy advertisement.
1434 * Note: The local path can only exist as a best path in the
1435 * VPN route table. It will take precedence over all sync paths.
1437 static void update_evpn_route_entry_sync_info(struct bgp
*bgp
,
1438 struct bgp_dest
*dest
,
1440 uint32_t loc_seq
, bool setup_sync
)
1443 struct prefix_evpn
*evp
=
1444 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1446 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1449 esi
= bgp_evpn_attr_get_esi(attr
);
1450 if (bgp_evpn_is_esi_valid(esi
)) {
1452 uint32_t max_sync_seq
= 0;
1453 bool active_on_peer
= false;
1454 bool peer_router
= false;
1455 bool proxy_from_peer
= false;
1457 bgp_evpn_get_sync_info(bgp
, esi
, dest
, loc_seq
,
1458 &max_sync_seq
, &active_on_peer
,
1459 &peer_router
, &proxy_from_peer
);
1460 attr
->mm_sync_seqnum
= max_sync_seq
;
1462 attr
->es_flags
|= ATTR_ES_PEER_ACTIVE
;
1464 attr
->es_flags
&= ~ATTR_ES_PEER_ACTIVE
;
1465 if (proxy_from_peer
)
1466 attr
->es_flags
|= ATTR_ES_PEER_PROXY
;
1468 attr
->es_flags
&= ~ATTR_ES_PEER_PROXY
;
1470 attr
->es_flags
|= ATTR_ES_PEER_ROUTER
;
1472 attr
->es_flags
&= ~ATTR_ES_PEER_ROUTER
;
1474 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
1475 char esi_buf
[ESI_STR_LEN
];
1478 "setup sync info for %pFX es %s max_seq %d %s%s%s",
1480 esi_to_str(esi
, esi_buf
,
1483 (attr
->es_flags
& ATTR_ES_PEER_ACTIVE
)
1486 (attr
->es_flags
& ATTR_ES_PEER_PROXY
)
1489 (attr
->es_flags
& ATTR_ES_PEER_ROUTER
)
1495 attr
->mm_sync_seqnum
= 0;
1496 attr
->es_flags
&= ~ATTR_ES_PEER_ACTIVE
;
1497 attr
->es_flags
&= ~ATTR_ES_PEER_PROXY
;
1502 * Create or update EVPN route entry. This could be in the VNI route table
1503 * or the global route table.
1505 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1506 afi_t afi
, safi_t safi
,
1507 struct bgp_dest
*dest
, struct attr
*attr
,
1508 int add
, struct bgp_path_info
**pi
,
1509 uint8_t flags
, uint32_t seq
, bool vpn_rt
,
1512 struct bgp_path_info
*tmp_pi
;
1513 struct bgp_path_info
*local_pi
;
1514 struct attr
*attr_new
;
1515 mpls_label_t label
[BGP_MAX_LABELS
];
1516 uint32_t num_labels
= 1;
1517 int route_change
= 1;
1519 const struct prefix_evpn
*evp
;
1522 evp
= (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1523 memset(&label
, 0, sizeof(label
));
1525 /* See if this is an update of an existing route, or a new add. */
1526 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
1528 /* If route doesn't exist already, create a new one, if told to.
1529 * Otherwise act based on whether the attributes of the route have
1532 if (!local_pi
&& !add
)
1535 if (old_is_sync
&& local_pi
)
1536 *old_is_sync
= bgp_evpn_attr_is_sync(local_pi
->attr
);
1538 /* if a local path is being added with a non-zero esi look
1539 * for SYNC paths from ES peers and bubble up the sync-info
1541 update_evpn_route_entry_sync_info(bgp
, dest
, attr
, seq
, vpn_rt
);
1543 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1544 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1545 add_mac_mobility_to_attr(seq
, attr
);
1548 /* Add (or update) attribute to hash. */
1549 attr_new
= bgp_attr_intern(attr
);
1551 /* Extract MAC mobility sequence number, if any. */
1552 attr_new
->mm_seqnum
=
1553 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1554 attr_new
->sticky
= sticky
;
1556 /* Create new route with its attribute. */
1557 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1558 bgp
->peer_self
, attr_new
, dest
);
1559 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1560 bgp_path_info_extra_get(tmp_pi
);
1562 /* The VNI goes into the 'label' field of the route */
1563 vni2label(vpn
->vni
, &label
[0]);
1565 /* Type-2 routes may carry a second VNI - the L3-VNI.
1566 * Only attach second label if we are advertising two labels for
1569 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1570 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1573 l3vni
= bgpevpn_get_l3vni(vpn
);
1575 vni2label(l3vni
, &label
[1]);
1580 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1581 tmp_pi
->extra
->num_labels
= num_labels
;
1582 /* Mark route as self type-2 route */
1583 if (flags
&& CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1584 tmp_pi
->extra
->af_flags
= BGP_EVPN_MACIP_TYPE_SVI_IP
;
1585 bgp_path_info_add(dest
, tmp_pi
);
1588 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1589 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1593 * The attributes have changed, type-2 routes needs to
1594 * be advertised with right labels.
1596 vni2label(vpn
->vni
, &label
[0]);
1597 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1598 && CHECK_FLAG(vpn
->flags
,
1599 VNI_FLAG_USE_TWO_LABELS
)) {
1602 l3vni
= bgpevpn_get_l3vni(vpn
);
1604 vni2label(l3vni
, &label
[1]);
1608 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1609 tmp_pi
->extra
->num_labels
= num_labels
;
1611 /* The attribute has changed. */
1612 /* Add (or update) attribute to hash. */
1613 attr_new
= bgp_attr_intern(attr
);
1614 bgp_path_info_set_flag(dest
, tmp_pi
,
1615 BGP_PATH_ATTR_CHANGED
);
1617 /* Extract MAC mobility sequence number, if any. */
1618 attr_new
->mm_seqnum
=
1619 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1620 attr_new
->sticky
= sticky
;
1622 /* Restore route, if needed. */
1623 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1624 bgp_path_info_restore(dest
, tmp_pi
);
1626 /* Unintern existing, set to new. */
1627 bgp_attr_unintern(&tmp_pi
->attr
);
1628 tmp_pi
->attr
= attr_new
;
1629 tmp_pi
->uptime
= monotime(NULL
);
1633 /* local MAC-IP routes in the VNI table are linked to
1634 * the destination ES
1636 if (route_change
&& vpn_rt
1637 && (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
))
1638 bgp_evpn_path_es_link(tmp_pi
, vpn
->vni
,
1639 bgp_evpn_attr_get_esi(tmp_pi
->attr
));
1641 /* Return back the route entry. */
1643 return route_change
;
1646 static void evpn_zebra_reinstall_best_route(struct bgp
*bgp
,
1647 struct bgpevpn
*vpn
,
1648 struct bgp_dest
*dest
)
1650 struct bgp_path_info
*tmp_ri
;
1651 struct bgp_path_info
*curr_select
= NULL
;
1653 for (tmp_ri
= bgp_dest_get_bgp_path_info(dest
); tmp_ri
;
1654 tmp_ri
= tmp_ri
->next
) {
1655 if (CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_SELECTED
)) {
1656 curr_select
= tmp_ri
;
1661 if (curr_select
&& curr_select
->type
== ZEBRA_ROUTE_BGP
1662 && (curr_select
->sub_type
== BGP_ROUTE_IMPORTED
||
1663 bgp_evpn_attr_is_sync(curr_select
->attr
)))
1664 evpn_zebra_install(bgp
, vpn
,
1665 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1670 * If the local route was not selected evict it and tell zebra to re-add
1671 * the best remote dest.
1673 * Typically a local path added by zebra is expected to be selected as
1674 * best. In which case when a remote path wins as best (later)
1675 * evpn_route_select_install itself evicts the older-local-best path.
1677 * However if bgp's add and zebra's add cross paths (race condition) it
1678 * is possible that the local path is no longer the "older" best path.
1679 * It is a path that was never designated as best and hence requires
1680 * additional handling to prevent bgp from injecting and holding on to a
1681 * non-best local path.
1683 static void evpn_cleanup_local_non_best_route(struct bgp
*bgp
,
1684 struct bgpevpn
*vpn
,
1685 struct bgp_dest
*dest
,
1686 struct bgp_path_info
*local_pi
)
1688 /* local path was not picked as the winner; kick it out */
1689 if (bgp_debug_zebra(NULL
))
1690 zlog_debug("evicting local evpn prefix %pBD as remote won",
1693 evpn_delete_old_local_route(bgp
, vpn
, dest
, local_pi
, NULL
);
1694 bgp_path_info_reap(dest
, local_pi
);
1696 /* tell zebra to re-add the best remote path */
1697 evpn_zebra_reinstall_best_route(bgp
, vpn
, dest
);
1700 static inline bool bgp_evpn_route_add_l3_ecomm_ok(struct bgpevpn
*vpn
,
1701 const struct prefix_evpn
*p
,
1704 return p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1705 && (is_evpn_prefix_ipaddr_v4(p
)
1706 || (is_evpn_prefix_ipaddr_v6(p
)
1707 && !IN6_IS_ADDR_LINKLOCAL(
1708 &p
->prefix
.macip_addr
.ip
.ipaddr_v6
)))
1709 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)
1710 && bgpevpn_get_l3vni(vpn
) && bgp_evpn_es_add_l3_ecomm_ok(esi
);
1714 * Create or update EVPN route (of type based on prefix) for specified VNI
1715 * and schedule for processing.
1717 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1718 struct prefix_evpn
*p
, uint8_t flags
,
1719 uint32_t seq
, esi_t
*esi
)
1721 struct bgp_dest
*dest
;
1723 struct attr
*attr_new
;
1724 int add_l3_ecomm
= 0;
1725 struct bgp_path_info
*pi
;
1726 afi_t afi
= AFI_L2VPN
;
1727 safi_t safi
= SAFI_EVPN
;
1729 bool old_is_sync
= false;
1731 memset(&attr
, 0, sizeof(attr
));
1733 /* Build path-attribute for this route. */
1734 bgp_attr_default_set(&attr
, bgp
, BGP_ORIGIN_IGP
);
1735 attr
.nexthop
= vpn
->originator_ip
;
1736 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1737 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1738 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1739 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1740 attr
.router_flag
= CHECK_FLAG(flags
,
1741 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1742 if (CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_PROXY_ADVERT
))
1743 attr
.es_flags
|= ATTR_ES_PROXY_ADVERT
;
1745 if (esi
&& bgp_evpn_is_esi_valid(esi
)) {
1746 memcpy(&attr
.esi
, esi
, sizeof(esi_t
));
1747 attr
.es_flags
|= ATTR_ES_IS_LOCAL
;
1750 /* PMSI is only needed for type-3 routes */
1751 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
1752 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1753 bgp_attr_set_pmsi_tnl_type(&attr
, PMSI_TNLTYPE_INGR_REPL
);
1756 /* router mac is only needed for type-2 routes here. */
1757 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
1758 uint8_t af_flags
= 0;
1760 if (CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1761 SET_FLAG(af_flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
);
1763 bgp_evpn_get_rmac_nexthop(vpn
, p
, &attr
, af_flags
);
1766 if (bgp_debug_zebra(NULL
)) {
1767 char buf3
[ESI_STR_LEN
];
1770 "VRF %s vni %u type-2 route evp %pFX RMAC %pEA nexthop %pI4 esi %s",
1771 vpn
->bgp_vrf
? vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
)
1773 vpn
->vni
, p
, &attr
.rmac
, &attr
.mp_nexthop_global_in
,
1774 esi_to_str(esi
, buf3
, sizeof(buf3
)));
1777 vni2label(vpn
->vni
, &(attr
.label
));
1779 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1780 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1783 add_l3_ecomm
= bgp_evpn_route_add_l3_ecomm_ok(
1784 vpn
, p
, (attr
.es_flags
& ATTR_ES_IS_LOCAL
) ? &attr
.esi
: NULL
);
1786 /* Set up extended community. */
1787 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1789 /* First, create (or fetch) route node within the VNI. */
1790 /* NOTE: There is no RD here. */
1791 dest
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1793 /* Create or update route entry. */
1794 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, dest
, &attr
,
1796 true /* setup_sync */, &old_is_sync
);
1798 attr_new
= pi
->attr
;
1800 /* lock ri to prevent freeing in evpn_route_select_install */
1801 bgp_path_info_lock(pi
);
1803 /* Perform route selection. Normally, the local route in the
1804 * VNI is expected to win and be the best route. However, if
1805 * there is a race condition where a host moved from local to
1806 * remote and the remote route was received in BGP just prior
1807 * to the local MACIP notification from zebra, the remote
1808 * route would win, and we should evict the defunct local route
1809 * and (re)install the remote route into zebra.
1811 evpn_route_select_install(bgp
, vpn
, dest
);
1813 * If the new local route was not selected evict it and tell zebra
1814 * to re-add the best remote dest. BGP doesn't retain non-best local
1817 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
1820 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1822 evpn_cleanup_local_non_best_route(bgp
, vpn
, dest
, pi
);
1826 /* If the local path already existed and is still the
1827 * best path we need to also check if it transitioned
1828 * from being a sync path to a non-sync path. If it
1829 * it did we need to notify zebra that the sync-path
1832 new_is_sync
= bgp_evpn_attr_is_sync(pi
->attr
);
1833 if (!new_is_sync
&& old_is_sync
)
1834 evpn_zebra_uninstall(bgp
, vpn
, p
, zero_vtep_ip
);
1837 bgp_path_info_unlock(pi
);
1839 bgp_dest_unlock_node(dest
);
1841 /* If this is a new route or some attribute has changed, export the
1842 * route to the global table. The route will be advertised to peers
1843 * from there. Note that this table is a 2-level tree (RD-level +
1844 * Prefix-level) similar to L3VPN routes.
1847 struct bgp_path_info
*global_pi
;
1849 dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1850 (const struct prefix_evpn
*)p
,
1852 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, dest
, attr_new
, 1,
1853 &global_pi
, flags
, seq
,
1854 false /* setup_sync */, NULL
/* old_is_sync */);
1856 /* Schedule for processing and unlock node. */
1857 bgp_process(bgp
, dest
, afi
, safi
);
1858 bgp_dest_unlock_node(dest
);
1861 /* Unintern temporary. */
1862 aspath_unintern(&attr
.aspath
);
1868 * Delete EVPN route entry.
1869 * The entry can be in ESI/VNI table or the global table.
1871 void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1872 struct bgp_dest
*dest
,
1873 struct bgp_path_info
**pi
)
1875 struct bgp_path_info
*tmp_pi
;
1879 /* Now, find matching route. */
1880 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1881 tmp_pi
= tmp_pi
->next
)
1882 if (tmp_pi
->peer
== bgp
->peer_self
1883 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1884 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1889 /* Mark route for delete. */
1891 bgp_path_info_delete(dest
, tmp_pi
);
1894 /* Delete EVPN type5 route */
1895 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1897 afi_t afi
= AFI_L2VPN
;
1898 safi_t safi
= SAFI_EVPN
;
1899 struct bgp_dest
*dest
= NULL
;
1900 struct bgp_path_info
*pi
= NULL
;
1901 struct bgp
*bgp_evpn
= NULL
; /* evpn bgp instance */
1903 bgp_evpn
= bgp_get_evpn();
1907 /* locate the global route entry for this type-5 prefix */
1908 dest
= bgp_global_evpn_node_lookup(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1909 (const struct prefix_evpn
*)evp
, &bgp_vrf
->vrf_prd
);
1913 delete_evpn_route_entry(bgp_evpn
, afi
, safi
, dest
, &pi
);
1915 bgp_process(bgp_evpn
, dest
, afi
, safi
);
1916 bgp_dest_unlock_node(dest
);
1921 * Delete EVPN route (of type based on prefix) for specified VNI and
1922 * schedule for processing.
1924 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1925 struct prefix_evpn
*p
)
1927 struct bgp_dest
*dest
, *global_dest
;
1928 struct bgp_path_info
*pi
;
1929 afi_t afi
= AFI_L2VPN
;
1930 safi_t safi
= SAFI_EVPN
;
1932 /* First, locate the route node within the VNI. If it doesn't exist,
1934 * is nothing further to do.
1936 /* NOTE: There is no RD here. */
1937 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1941 /* Next, locate route node in the global EVPN routing table. Note that
1942 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1945 global_dest
= bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1946 (const struct prefix_evpn
*)p
, &vpn
->prd
);
1948 /* Delete route entry in the global EVPN table. */
1949 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
1951 /* Schedule for processing - withdraws to peers happen from
1955 bgp_process(bgp
, global_dest
, afi
, safi
);
1956 bgp_dest_unlock_node(global_dest
);
1959 /* Delete route entry in the VNI route table. This can just be removed.
1961 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
1963 bgp_path_info_reap(dest
, pi
);
1964 evpn_route_select_install(bgp
, vpn
, dest
);
1966 bgp_dest_unlock_node(dest
);
1971 void bgp_evpn_update_type2_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1972 struct bgp_dest
*dest
,
1973 struct bgp_path_info
*local_pi
,
1976 afi_t afi
= AFI_L2VPN
;
1977 safi_t safi
= SAFI_EVPN
;
1978 struct bgp_path_info
*pi
;
1980 struct attr
*attr_new
;
1982 int add_l3_ecomm
= 0;
1983 struct bgp_dest
*global_dest
;
1984 struct bgp_path_info
*global_pi
;
1985 struct prefix_evpn
*evp
=
1986 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1988 bool old_is_sync
= false;
1990 if (CHECK_FLAG(local_pi
->flags
, BGP_PATH_REMOVED
))
1994 * Build attribute per local route as the MAC mobility and
1995 * some other values could differ for different routes. The
1996 * attributes will be shared in the hash table.
1998 bgp_attr_default_set(&attr
, bgp
, BGP_ORIGIN_IGP
);
1999 attr
.nexthop
= vpn
->originator_ip
;
2000 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
2001 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
2002 attr
.sticky
= (local_pi
->attr
->sticky
) ? 1 : 0;
2003 attr
.router_flag
= (local_pi
->attr
->router_flag
) ? 1 : 0;
2004 attr
.es_flags
= local_pi
->attr
->es_flags
;
2005 if (local_pi
->attr
->default_gw
) {
2006 attr
.default_gw
= 1;
2007 if (is_evpn_prefix_ipaddr_v6(evp
))
2008 attr
.router_flag
= 1;
2010 memcpy(&attr
.esi
, &local_pi
->attr
->esi
, sizeof(esi_t
));
2011 bgp_evpn_get_rmac_nexthop(vpn
, evp
, &attr
,
2012 local_pi
->extra
->af_flags
);
2013 vni2label(vpn
->vni
, &(attr
.label
));
2014 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2015 * using L3 VNI for type-2 routes also.
2017 add_l3_ecomm
= bgp_evpn_route_add_l3_ecomm_ok(
2019 (attr
.es_flags
& ATTR_ES_IS_LOCAL
) ? &attr
.esi
: NULL
);
2021 /* Set up extended community. */
2022 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2023 seq
= mac_mobility_seqnum(local_pi
->attr
);
2025 if (bgp_debug_zebra(NULL
)) {
2026 char buf3
[ESI_STR_LEN
];
2029 "VRF %s vni %u evp %pFX RMAC %pEA nexthop %pI4 esi %s esf 0x%x from %s",
2030 vpn
->bgp_vrf
? vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
)
2032 vpn
->vni
, evp
, &attr
.rmac
, &attr
.mp_nexthop_global_in
,
2033 esi_to_str(&attr
.esi
, buf3
, sizeof(buf3
)),
2034 attr
.es_flags
, caller
);
2037 /* Update the route entry. */
2038 route_change
= update_evpn_route_entry(
2039 bgp
, vpn
, afi
, safi
, dest
, &attr
, 0, &pi
, 0, seq
,
2040 true /* setup_sync */, &old_is_sync
);
2043 attr_new
= pi
->attr
;
2044 /* lock ri to prevent freeing in evpn_route_select_install */
2045 bgp_path_info_lock(pi
);
2047 /* Perform route selection. Normally, the local route in the
2048 * VNI is expected to win and be the best route. However,
2049 * under peculiar situations (e.g., tunnel (next hop) IP change
2050 * that causes best selection to be based on next hop), a
2051 * remote route could win. If the local route is the best,
2052 * ensure it is updated in the global EVPN route table and
2053 * advertised to peers; otherwise, ensure it is evicted and
2054 * (re)install the remote route into zebra.
2056 evpn_route_select_install(bgp
, vpn
, dest
);
2058 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2061 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
2063 evpn_cleanup_local_non_best_route(bgp
, vpn
, dest
, pi
);
2067 /* If the local path already existed and is still the
2068 * best path we need to also check if it transitioned
2069 * from being a sync path to a non-sync path. If it
2070 * it did we need to notify zebra that the sync-path
2073 new_is_sync
= bgp_evpn_attr_is_sync(pi
->attr
);
2074 if (!new_is_sync
&& old_is_sync
)
2075 evpn_zebra_uninstall(bgp
, vpn
,
2082 bgp_path_info_unlock(pi
);
2085 /* Update route in global routing table. */
2086 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
,
2087 safi
, evp
, &vpn
->prd
);
2088 assert(global_dest
);
2089 update_evpn_route_entry(
2090 bgp
, vpn
, afi
, safi
, global_dest
, attr_new
, 0,
2091 &global_pi
, 0, mac_mobility_seqnum(attr_new
),
2092 false /* setup_sync */, NULL
/* old_is_sync */);
2094 /* Schedule for processing and unlock node. */
2095 bgp_process(bgp
, global_dest
, afi
, safi
);
2096 bgp_dest_unlock_node(global_dest
);
2099 /* Unintern temporary. */
2100 aspath_unintern(&attr
.aspath
);
2104 * Update all type-2 (MACIP) local routes for this VNI - these should also
2105 * be scheduled for advertise to peers.
2107 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2109 struct bgp_dest
*dest
;
2110 struct bgp_path_info
*tmp_pi
;
2112 /* Walk this VNI's route table and update local type-2 routes. For any
2113 * routes updated, update corresponding entry in the global table too.
2115 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2116 dest
= bgp_route_next(dest
)) {
2117 const struct prefix_evpn
*evp
=
2118 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
2120 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2123 /* Identify local route. */
2124 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
2125 tmp_pi
= tmp_pi
->next
) {
2126 if (tmp_pi
->peer
== bgp
->peer_self
2127 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2128 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2135 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, tmp_pi
,
2143 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2144 * global routing table. These are also scheduled for withdraw from peers.
2146 static void delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2150 struct bgp_dest
*rddest
, *dest
;
2151 struct bgp_table
*table
;
2152 struct bgp_path_info
*pi
;
2157 rddest
= bgp_node_lookup(bgp
->rib
[afi
][safi
],
2158 (struct prefix
*)&vpn
->prd
);
2160 table
= bgp_dest_get_bgp_table_info(rddest
);
2161 for (dest
= bgp_table_top(table
); dest
;
2162 dest
= bgp_route_next(dest
)) {
2163 const struct prefix_evpn
*evp
=
2164 (const struct prefix_evpn
*)bgp_dest_get_prefix(
2167 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2170 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2172 bgp_process(bgp
, dest
, afi
, safi
);
2175 /* Unlock RD node. */
2176 bgp_dest_unlock_node(rddest
);
2181 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2182 * table as well as the per-VNI route table.
2184 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2188 struct bgp_dest
*dest
;
2189 struct bgp_path_info
*pi
;
2194 /* First, walk the global route table for this VNI's type-2 local
2196 * EVPN routes are a 2-level table, first get the RD table.
2198 delete_global_type2_routes(bgp
, vpn
);
2200 /* Next, walk this VNI's route table and delete local type-2 routes. */
2201 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2202 dest
= bgp_route_next(dest
)) {
2203 const struct prefix_evpn
*evp
=
2204 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
2206 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2209 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2211 /* Route entry in local table gets deleted immediately. */
2213 bgp_path_info_reap(dest
, pi
);
2220 * Delete all routes in the per-VNI route table.
2222 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2224 struct bgp_dest
*dest
;
2225 struct bgp_path_info
*pi
, *nextpi
;
2227 /* Walk this VNI's route table and delete all routes. */
2228 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2229 dest
= bgp_route_next(dest
)) {
2230 for (pi
= bgp_dest_get_bgp_path_info(dest
);
2231 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2232 bgp_evpn_remote_ip_hash_del(vpn
, pi
);
2233 bgp_path_info_delete(dest
, pi
);
2234 bgp_path_info_reap(dest
, pi
);
2241 /* BUM traffic flood mode per-l2-vni */
2242 static int bgp_evpn_vni_flood_mode_get(struct bgp
*bgp
,
2243 struct bgpevpn
*vpn
)
2245 /* if flooding has been globally disabled per-vni mode is
2248 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
2249 return VXLAN_FLOOD_DISABLED
;
2251 /* if mcast group ip has been specified we use a PIM-SM MDT */
2252 if (vpn
->mcast_grp
.s_addr
!= INADDR_ANY
)
2253 return VXLAN_FLOOD_PIM_SM
;
2255 /* default is ingress replication */
2256 return VXLAN_FLOOD_HEAD_END_REPL
;
2260 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2261 * export RT getting modified or change to tunnel IP. Note that these
2262 * situations need the route in the per-VNI table as well as the global
2263 * table to be updated (as attributes change).
2265 int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2268 struct prefix_evpn p
;
2270 update_type1_routes_for_evi(bgp
, vpn
);
2272 /* Update and advertise the type-3 route (only one) followed by the
2273 * locally learnt type-2 routes (MACIP) - for this VNI.
2275 * RT-3 only if doing head-end replication
2277 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
2278 == VXLAN_FLOOD_HEAD_END_REPL
) {
2279 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2280 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
);
2285 return update_all_type2_routes(bgp
, vpn
);
2289 * Delete (and withdraw) local routes for specified VNI from the global
2290 * table and per-VNI table. After this, remove all other routes from
2291 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2292 * (advertise-all-vni) being disabled.
2294 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2297 struct prefix_evpn p
;
2299 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2300 * followed by type-3 routes (only one) - for this VNI.
2302 ret
= delete_all_type2_routes(bgp
, vpn
);
2306 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2307 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2311 /* Delete all routes from the per-VNI table. */
2312 return delete_all_vni_routes(bgp
, vpn
);
2316 * There is a flood mcast IP address change. Update the mcast-grp and
2317 * remove the type-3 route if any. A new type-3 route will be generated
2318 * post tunnel_ip update if the new flood mode is head-end-replication.
2320 static int bgp_evpn_mcast_grp_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2321 struct in_addr mcast_grp
)
2323 struct prefix_evpn p
;
2325 vpn
->mcast_grp
= mcast_grp
;
2327 if (is_vni_live(vpn
)) {
2328 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2329 delete_evpn_route(bgp
, vpn
, &p
);
2336 * There is a tunnel endpoint IP address change for this VNI, delete
2337 * prior type-3 route (if needed) and update.
2338 * Note: Route re-advertisement happens elsewhere after other processing
2341 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2342 struct in_addr originator_ip
)
2344 struct prefix_evpn p
;
2346 /* If VNI is not live, we only need to update the originator ip */
2347 if (!is_vni_live(vpn
)) {
2348 vpn
->originator_ip
= originator_ip
;
2352 /* Update the tunnel-ip hash */
2353 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2354 bgp_tip_add(bgp
, &originator_ip
);
2356 /* filter routes as martian nexthop db has changed */
2357 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2359 /* Need to withdraw type-3 route as the originator IP is part
2362 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2363 delete_evpn_route(bgp
, vpn
, &p
);
2365 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2366 vpn
->originator_ip
= originator_ip
;
2370 static struct bgp_path_info
*
2371 bgp_create_evpn_bgp_path_info(struct bgp_path_info
*parent_pi
,
2372 struct bgp_dest
*dest
, struct attr
*attr
)
2374 struct attr
*attr_new
;
2375 struct bgp_path_info
*pi
;
2377 /* Add (or update) attribute to hash. */
2378 attr_new
= bgp_attr_intern(attr
);
2380 /* Create new route with its attribute. */
2381 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0, parent_pi
->peer
,
2383 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2384 bgp_path_info_extra_get(pi
);
2385 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2386 bgp_dest_lock_node((struct bgp_dest
*)parent_pi
->net
);
2387 if (parent_pi
->extra
) {
2388 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2389 sizeof(pi
->extra
->label
));
2390 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2391 pi
->extra
->igpmetric
= parent_pi
->extra
->igpmetric
;
2393 bgp_path_info_add(dest
, pi
);
2399 * Install route entry into the VRF routing table and invoke route selection.
2401 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2402 const struct prefix_evpn
*evp
,
2403 struct bgp_path_info
*parent_pi
)
2405 struct bgp_dest
*dest
;
2406 struct bgp_path_info
*pi
;
2408 struct attr
*attr_new
;
2411 struct prefix
*pp
= &p
;
2414 bool new_pi
= false;
2415 bool use_l3nhg
= false;
2416 bool is_l3nhg_active
= false;
2417 char buf1
[INET6_ADDRSTRLEN
];
2419 memset(pp
, 0, sizeof(struct prefix
));
2420 ip_prefix_from_evpn_prefix(evp
, pp
);
2422 if (bgp_debug_zebra(NULL
))
2424 "vrf %s: import evpn prefix %pFX parent %p flags 0x%x",
2425 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
, parent_pi
,
2428 /* Create (or fetch) route within the VRF. */
2429 /* NOTE: There is no RD here. */
2430 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2432 safi
= SAFI_UNICAST
;
2433 dest
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2434 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2436 safi
= SAFI_UNICAST
;
2437 dest
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2441 /* EVPN routes currently only support a IPv4 next hop which corresponds
2442 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2443 * or prefix route, we have to convert the next hop to an IPv4-mapped
2444 * address for the rest of the code to flow through. In the case of IPv4,
2445 * make sure to set the flag for next hop attribute.
2447 attr
= *parent_pi
->attr
;
2448 if (attr
.evpn_overlay
.type
!= OVERLAY_INDEX_GATEWAY_IP
) {
2450 evpn_convert_nexthop_to_ipv6(&attr
);
2452 attr
.nexthop
= attr
.mp_nexthop_global_in
;
2453 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2458 * If gateway IP overlay index is specified in the NLRI of
2459 * EVPN RT-5, this gateway IP should be used as the nexthop
2460 * for the prefix in the VRF
2462 if (bgp_debug_zebra(NULL
)) {
2464 "Install gateway IP %s as nexthop for prefix %pFX in vrf %s",
2465 inet_ntop(pp
->family
, &attr
.evpn_overlay
.gw_ip
,
2466 buf1
, sizeof(buf1
)), pp
,
2467 vrf_id_to_name(bgp_vrf
->vrf_id
));
2470 if (afi
== AFI_IP6
) {
2471 memcpy(&attr
.mp_nexthop_global
,
2472 &attr
.evpn_overlay
.gw_ip
.ipaddr_v6
,
2473 sizeof(struct in6_addr
));
2474 attr
.mp_nexthop_len
= IPV6_MAX_BYTELEN
;
2476 attr
.nexthop
= attr
.evpn_overlay
.gw_ip
.ipaddr_v4
;
2477 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2481 bgp_evpn_es_vrf_use_nhg(bgp_vrf
, &parent_pi
->attr
->esi
, &use_l3nhg
,
2482 &is_l3nhg_active
, NULL
);
2484 attr
.es_flags
|= ATTR_ES_L3_NHG_USE
;
2485 if (is_l3nhg_active
)
2486 attr
.es_flags
|= ATTR_ES_L3_NHG_ACTIVE
;
2488 /* Check if route entry is already present. */
2489 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2491 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2495 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, dest
, &attr
);
2498 if (attrhash_cmp(pi
->attr
, &attr
)
2499 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2500 bgp_dest_unlock_node(dest
);
2503 /* The attribute has changed. */
2504 /* Add (or update) attribute to hash. */
2505 attr_new
= bgp_attr_intern(&attr
);
2507 /* Restore route, if needed. */
2508 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2509 bgp_path_info_restore(dest
, pi
);
2511 /* Mark if nexthop has changed. */
2513 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2515 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2516 &attr_new
->mp_nexthop_global
)))
2517 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2519 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ATTR_CHANGED
);
2520 /* Unintern existing, set to new. */
2521 bgp_attr_unintern(&pi
->attr
);
2522 pi
->attr
= attr_new
;
2523 pi
->uptime
= monotime(NULL
);
2526 /* Gateway IP nexthop should be resolved */
2527 if (attr
.evpn_overlay
.type
== OVERLAY_INDEX_GATEWAY_IP
) {
2528 if (bgp_find_or_add_nexthop(bgp_vrf
, bgp_vrf
, afi
, safi
, pi
,
2530 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_VALID
);
2532 if (BGP_DEBUG(nht
, NHT
)) {
2533 inet_ntop(pp
->family
,
2534 &attr
.evpn_overlay
.gw_ip
,
2535 buf1
, sizeof(buf1
));
2536 zlog_debug("%s: gateway IP NH unresolved",
2539 bgp_path_info_unset_flag(dest
, pi
, BGP_PATH_VALID
);
2543 /* as it is an importation, change nexthop */
2544 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ANNC_NH_SELF
);
2547 /* Link path to evpn nexthop */
2548 bgp_evpn_path_nh_add(bgp_vrf
, pi
);
2550 bgp_aggregate_increment(bgp_vrf
, bgp_dest_get_prefix(dest
), pi
, afi
,
2553 /* Perform route selection and update zebra, if required. */
2554 bgp_process(bgp_vrf
, dest
, afi
, safi
);
2556 /* Process for route leaking. */
2557 vpn_leak_from_vrf_update(bgp_get_default(), bgp_vrf
, pi
);
2559 bgp_dest_unlock_node(dest
);
2561 if (bgp_debug_zebra(NULL
))
2562 zlog_debug("... %s pi dest %p (l %d) pi %p (l %d, f 0x%x)",
2563 new_pi
? "new" : "update", dest
,
2564 bgp_dest_get_lock_count(dest
), pi
, pi
->lock
,
2571 * Install route entry into the VNI routing table and invoke route selection.
2573 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2574 const struct prefix_evpn
*p
,
2575 struct bgp_path_info
*parent_pi
)
2577 struct bgp_dest
*dest
;
2578 struct bgp_path_info
*pi
;
2579 struct bgp_path_info
*local_pi
;
2580 struct attr
*attr_new
;
2582 struct prefix_evpn ad_evp
;
2583 bool old_local_es
= false;
2586 /* EAD prefix in the global table doesn't include the VTEP-IP so
2587 * we need to create a different copy for the VNI
2589 if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
2590 p
= evpn_type1_prefix_vni_copy(&ad_evp
, p
,
2591 parent_pi
->attr
->nexthop
);
2593 /* Create (or fetch) route within the VNI. */
2594 /* NOTE: There is no RD here. */
2595 dest
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2597 /* Check if route entry is already present. */
2598 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2600 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2604 /* Create an info */
2605 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, dest
,
2607 new_local_es
= bgp_evpn_attr_is_local_es(pi
->attr
);
2609 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2610 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2611 bgp_dest_unlock_node(dest
);
2614 /* The attribute has changed. */
2615 /* Add (or update) attribute to hash. */
2616 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2618 /* Restore route, if needed. */
2619 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2620 bgp_path_info_restore(dest
, pi
);
2622 /* Mark if nexthop has changed. */
2623 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2624 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2626 old_local_es
= bgp_evpn_attr_is_local_es(pi
->attr
);
2627 new_local_es
= bgp_evpn_attr_is_local_es(attr_new
);
2628 /* If ESI is different or if its type has changed we
2629 * need to reinstall the path in zebra
2631 if ((old_local_es
!= new_local_es
)
2632 || memcmp(&pi
->attr
->esi
, &attr_new
->esi
,
2633 sizeof(attr_new
->esi
))) {
2635 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
))
2636 zlog_debug("VNI %d path %pFX chg to %s es",
2637 vpn
->vni
, &pi
->net
->p
,
2638 new_local_es
? "local"
2640 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ATTR_CHANGED
);
2643 /* Unintern existing, set to new. */
2644 bgp_attr_unintern(&pi
->attr
);
2645 pi
->attr
= attr_new
;
2646 pi
->uptime
= monotime(NULL
);
2649 /* Add this route to remote IP hashtable */
2650 bgp_evpn_remote_ip_hash_add(vpn
, pi
);
2652 /* Perform route selection and update zebra, if required. */
2653 ret
= evpn_route_select_install(bgp
, vpn
, dest
);
2655 /* if the best path is a local path with a non-zero ES
2656 * sync info against the local path may need to be updated
2657 * when a remote path is added/updated (including changes
2658 * from sync-path to remote-path)
2660 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
2661 if (local_pi
&& (old_local_es
|| new_local_es
))
2662 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, local_pi
,
2664 bgp_dest_unlock_node(dest
);
2670 * Uninstall route entry from the VRF routing table and send message
2671 * to zebra, if appropriate.
2673 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2674 const struct prefix_evpn
*evp
,
2675 struct bgp_path_info
*parent_pi
)
2677 struct bgp_dest
*dest
;
2678 struct bgp_path_info
*pi
;
2681 struct prefix
*pp
= &p
;
2685 memset(pp
, 0, sizeof(struct prefix
));
2686 ip_prefix_from_evpn_prefix(evp
, pp
);
2688 if (bgp_debug_zebra(NULL
))
2690 "vrf %s: unimport evpn prefix %pFX parent %p flags 0x%x",
2691 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
, parent_pi
,
2694 /* Locate route within the VRF. */
2695 /* NOTE: There is no RD here. */
2696 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2698 safi
= SAFI_UNICAST
;
2699 dest
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2702 safi
= SAFI_UNICAST
;
2703 dest
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2709 /* Find matching route entry. */
2710 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2712 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2716 bgp_dest_unlock_node(dest
);
2720 if (bgp_debug_zebra(NULL
))
2721 zlog_debug("... delete dest %p (l %d) pi %p (l %d, f 0x%x)",
2722 dest
, bgp_dest_get_lock_count(dest
), pi
, pi
->lock
,
2725 /* Process for route leaking. */
2726 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp_vrf
, pi
);
2728 bgp_aggregate_decrement(bgp_vrf
, bgp_dest_get_prefix(dest
), pi
, afi
,
2731 /* Mark entry for deletion */
2732 bgp_path_info_delete(dest
, pi
);
2734 /* Unlink path to evpn nexthop */
2735 bgp_evpn_path_nh_del(bgp_vrf
, pi
);
2737 /* Perform route selection and update zebra, if required. */
2738 bgp_process(bgp_vrf
, dest
, afi
, safi
);
2740 /* Unlock route node. */
2741 bgp_dest_unlock_node(dest
);
2747 * Uninstall route entry from the VNI routing table and send message
2748 * to zebra, if appropriate.
2750 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2751 const struct prefix_evpn
*p
,
2752 struct bgp_path_info
*parent_pi
)
2754 struct bgp_dest
*dest
;
2755 struct bgp_path_info
*pi
;
2756 struct bgp_path_info
*local_pi
;
2758 struct prefix_evpn ad_evp
;
2760 /* EAD prefix in the global table doesn't include the VTEP-IP so
2761 * we need to create a different copy for the VNI
2763 if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
2764 p
= evpn_type1_prefix_vni_copy(&ad_evp
, p
,
2765 parent_pi
->attr
->nexthop
);
2767 /* Locate route within the VNI. */
2768 /* NOTE: There is no RD here. */
2769 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2773 /* Find matching route entry. */
2774 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2776 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2780 bgp_dest_unlock_node(dest
);
2784 bgp_evpn_remote_ip_hash_del(vpn
, pi
);
2786 /* Mark entry for deletion */
2787 bgp_path_info_delete(dest
, pi
);
2789 /* Perform route selection and update zebra, if required. */
2790 ret
= evpn_route_select_install(bgp
, vpn
, dest
);
2792 /* if the best path is a local path with a non-zero ES
2793 * sync info against the local path may need to be updated
2794 * when a remote path is deleted
2796 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
2797 if (local_pi
&& bgp_evpn_attr_is_local_es(local_pi
->attr
))
2798 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, local_pi
,
2801 /* Unlock route node. */
2802 bgp_dest_unlock_node(dest
);
2808 * Given a route entry and a VRF, see if this route entry should be
2809 * imported into the VRF i.e., RTs match.
2811 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2812 struct bgp_path_info
*pi
)
2814 struct attr
*attr
= pi
->attr
;
2815 struct ecommunity
*ecom
;
2819 /* Route should have valid RT to be even considered. */
2820 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2823 ecom
= bgp_attr_get_ecommunity(attr
);
2824 if (!ecom
|| !ecom
->size
)
2827 /* For each extended community RT, see if it matches this VNI. If any RT
2828 * matches, we're done.
2830 for (i
= 0; i
< ecom
->size
; i
++) {
2832 uint8_t type
, sub_type
;
2833 struct ecommunity_val
*eval
;
2834 struct ecommunity_val eval_tmp
;
2835 struct vrf_irt_node
*irt
;
2837 /* Only deal with RTs */
2838 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
2839 eval
= (struct ecommunity_val
*)(ecom
->val
2840 + (i
* ecom
->unit_size
));
2843 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2846 /* See if this RT matches specified VNIs import RTs */
2847 irt
= lookup_vrf_import_rt(eval
);
2849 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2852 /* Also check for non-exact match. In this, we mask out the AS
2854 * only check on the local-admin sub-field. This is to
2856 * VNI as the RT for EBGP peering too.
2859 if (type
== ECOMMUNITY_ENCODE_AS
2860 || type
== ECOMMUNITY_ENCODE_AS4
2861 || type
== ECOMMUNITY_ENCODE_IP
) {
2862 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
2863 mask_ecom_global_admin(&eval_tmp
, eval
);
2864 irt
= lookup_vrf_import_rt(&eval_tmp
);
2867 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2875 * Given a route entry and a VNI, see if this route entry should be
2876 * imported into the VNI i.e., RTs match.
2878 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2879 struct bgp_path_info
*pi
)
2881 struct attr
*attr
= pi
->attr
;
2882 struct ecommunity
*ecom
;
2886 /* Route should have valid RT to be even considered. */
2887 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2890 ecom
= bgp_attr_get_ecommunity(attr
);
2891 if (!ecom
|| !ecom
->size
)
2894 /* For each extended community RT, see if it matches this VNI. If any RT
2895 * matches, we're done.
2897 for (i
= 0; i
< ecom
->size
; i
++) {
2899 uint8_t type
, sub_type
;
2900 struct ecommunity_val
*eval
;
2901 struct ecommunity_val eval_tmp
;
2902 struct irt_node
*irt
;
2904 /* Only deal with RTs */
2905 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
2906 eval
= (struct ecommunity_val
*)(ecom
->val
2907 + (i
* ecom
->unit_size
));
2910 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2913 /* See if this RT matches specified VNIs import RTs */
2914 irt
= lookup_import_rt(bgp
, eval
);
2916 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2919 /* Also check for non-exact match. In this, we mask out the AS
2921 * only check on the local-admin sub-field. This is to
2923 * VNI as the RT for EBGP peering too.
2926 if (type
== ECOMMUNITY_ENCODE_AS
2927 || type
== ECOMMUNITY_ENCODE_AS4
2928 || type
== ECOMMUNITY_ENCODE_IP
) {
2929 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
2930 mask_ecom_global_admin(&eval_tmp
, eval
);
2931 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2934 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2941 /* This API will scan evpn routes for checking attribute's rmac
2942 * macthes with bgp instance router mac. It avoid installing
2943 * route into bgp vrf table and remote rmac in bridge table.
2945 static int bgp_evpn_route_rmac_self_check(struct bgp
*bgp_vrf
,
2946 const struct prefix_evpn
*evp
,
2947 struct bgp_path_info
*pi
)
2949 /* evpn route could have learnt prior to L3vni has come up,
2950 * perform rmac check before installing route and
2951 * remote router mac.
2952 * The route will be removed from global bgp table once
2953 * SVI comes up with MAC and stored in hash, triggers
2954 * bgp_mac_rescan_all_evpn_tables.
2956 if (memcmp(&bgp_vrf
->rmac
, &pi
->attr
->rmac
, ETH_ALEN
) == 0) {
2957 if (bgp_debug_update(pi
->peer
, NULL
, NULL
, 1)) {
2958 char attr_str
[BUFSIZ
] = {0};
2960 bgp_dump_attr(pi
->attr
, attr_str
, sizeof(attr_str
));
2963 "%s: bgp %u prefix %pFX with attr %s - DENIED due to self mac",
2964 __func__
, bgp_vrf
->vrf_id
, evp
, attr_str
);
2973 /* don't import hosts that are locally attached */
2975 bgp_evpn_skip_vrf_import_of_local_es(struct bgp
*bgp_vrf
,
2976 const struct prefix_evpn
*evp
,
2977 struct bgp_path_info
*pi
, int install
)
2981 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
2982 esi
= bgp_evpn_attr_get_esi(pi
->attr
);
2984 /* Don't import routes that point to a local destination */
2985 if (bgp_evpn_attr_is_local_es(pi
->attr
)) {
2986 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
2987 char esi_buf
[ESI_STR_LEN
];
2990 "vrf %s of evpn prefix %pFX skipped, local es %s",
2991 install
? "import" : "unimport", evp
,
2992 esi_to_str(esi
, esi_buf
,
3002 * Install or uninstall a mac-ip route in the provided vrf if
3003 * there is a rt match
3005 int bgp_evpn_route_entry_install_if_vrf_match(struct bgp
*bgp_vrf
,
3006 struct bgp_path_info
*pi
,
3010 const struct prefix_evpn
*evp
=
3011 (const struct prefix_evpn
*)bgp_dest_get_prefix(pi
->net
);
3013 /* Consider "valid" remote routes applicable for
3016 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3017 && pi
->type
== ZEBRA_ROUTE_BGP
3018 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3021 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
3022 if (bgp_evpn_route_rmac_self_check(bgp_vrf
, evp
, pi
))
3025 /* don't import hosts that are locally attached */
3026 if (install
&& bgp_evpn_skip_vrf_import_of_local_es(
3027 bgp_vrf
, evp
, pi
, install
))
3031 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3033 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3037 flog_err(EC_BGP_EVPN_FAIL
,
3038 "Failed to %s EVPN %pFX route in VRF %s",
3039 install
? "install" : "uninstall", evp
,
3040 vrf_id_to_name(bgp_vrf
->vrf_id
));
3047 * Install or uninstall mac-ip routes are appropriate for this
3050 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
3054 struct bgp_dest
*rd_dest
, *dest
;
3055 struct bgp_table
*table
;
3056 struct bgp_path_info
*pi
;
3058 struct bgp
*bgp_evpn
= NULL
;
3062 bgp_evpn
= bgp_get_evpn();
3066 /* Walk entire global routing table and evaluate routes which could be
3067 * imported into this VRF. Note that we need to loop through all global
3068 * routes to determine which route matches the import rt on vrf
3070 for (rd_dest
= bgp_table_top(bgp_evpn
->rib
[afi
][safi
]); rd_dest
;
3071 rd_dest
= bgp_route_next(rd_dest
)) {
3072 table
= bgp_dest_get_bgp_table_info(rd_dest
);
3076 for (dest
= bgp_table_top(table
); dest
;
3077 dest
= bgp_route_next(dest
)) {
3078 const struct prefix_evpn
*evp
=
3079 (const struct prefix_evpn
*)bgp_dest_get_prefix(
3082 /* if not mac-ip route skip this route */
3083 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3084 || evp
->prefix
.route_type
3085 == BGP_EVPN_IP_PREFIX_ROUTE
))
3088 /* if not a mac+ip route skip this route */
3089 if (!(is_evpn_prefix_ipaddr_v4(evp
)
3090 || is_evpn_prefix_ipaddr_v6(evp
)))
3093 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
3095 ret
= bgp_evpn_route_entry_install_if_vrf_match(
3096 bgp_vrf
, pi
, install
);
3107 * Install or uninstall routes of specified type that are appropriate for this
3110 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
3111 struct bgpevpn
*vpn
,
3112 bgp_evpn_route_type rtype
,
3117 struct bgp_dest
*rd_dest
, *dest
;
3118 struct bgp_table
*table
;
3119 struct bgp_path_info
*pi
;
3125 /* Walk entire global routing table and evaluate routes which could be
3126 * imported into this VPN. Note that we cannot just look at the routes
3128 * the VNI's RD - remote routes applicable for this VNI could have any
3131 /* EVPN routes are a 2-level table. */
3132 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
3133 rd_dest
= bgp_route_next(rd_dest
)) {
3134 table
= bgp_dest_get_bgp_table_info(rd_dest
);
3138 for (dest
= bgp_table_top(table
); dest
;
3139 dest
= bgp_route_next(dest
)) {
3140 const struct prefix_evpn
*evp
=
3141 (const struct prefix_evpn
*)bgp_dest_get_prefix(
3144 if (evp
->prefix
.route_type
!= rtype
)
3147 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
3149 /* Consider "valid" remote routes applicable for
3151 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3152 && pi
->type
== ZEBRA_ROUTE_BGP
3153 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3156 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3158 ret
= install_evpn_route_entry(
3161 ret
= uninstall_evpn_route_entry(
3167 "%u: Failed to %s EVPN %s route in VNI %u",
3171 rtype
== BGP_EVPN_MAC_IP_ROUTE
3176 bgp_dest_unlock_node(rd_dest
);
3177 bgp_dest_unlock_node(dest
);
3188 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3189 * is invoked upon l3vni-add or l3vni import rt change
3191 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3193 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3198 * Install any existing remote routes applicable for this VNI into its
3199 * routing table. This is invoked when a VNI becomes "live" or its Import
3202 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3206 /* Install type-3 routes followed by type-2 routes - the ones applicable
3209 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3214 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_AD_ROUTE
,
3219 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3223 /* uninstall routes from l3vni vrf. */
3224 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3226 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3231 * Uninstall any existing remote routes for this VNI. One scenario in which
3232 * this is invoked is upon an import RT change.
3234 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3238 /* Uninstall type-2 routes followed by type-3 routes - the ones
3242 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3247 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_AD_ROUTE
,
3253 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3258 * Install or uninstall route in matching VRFs (list).
3260 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3261 safi_t safi
, struct prefix_evpn
*evp
,
3262 struct bgp_path_info
*pi
,
3263 struct list
*vrfs
, int install
)
3265 struct bgp
*bgp_vrf
;
3266 struct listnode
*node
, *nnode
;
3268 /* Only type-2/type-5 routes go into a VRF */
3269 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3270 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3273 /* if it is type-2 route and not a mac+ip route skip this route */
3274 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3275 && !(is_evpn_prefix_ipaddr_v4(evp
)
3276 || is_evpn_prefix_ipaddr_v6(evp
)))
3279 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3282 /* don't import hosts that are locally attached */
3283 if (install
&& bgp_evpn_skip_vrf_import_of_local_es(
3284 bgp_vrf
, evp
, pi
, install
))
3288 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3290 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3294 flog_err(EC_BGP_EVPN_FAIL
,
3295 "%u: Failed to %s prefix %pFX in VRF %s",
3297 install
? "install" : "uninstall", evp
,
3298 vrf_id_to_name(bgp_vrf
->vrf_id
));
3307 * Install or uninstall route in matching VNIs (list).
3309 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3310 safi_t safi
, struct prefix_evpn
*evp
,
3311 struct bgp_path_info
*pi
,
3312 struct list
*vnis
, int install
)
3314 struct bgpevpn
*vpn
;
3315 struct listnode
*node
, *nnode
;
3317 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3320 if (!is_vni_live(vpn
))
3324 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3326 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3329 flog_err(EC_BGP_EVPN_FAIL
,
3330 "%u: Failed to %s EVPN %s route in VNI %u",
3331 bgp
->vrf_id
, install
? "install" : "uninstall",
3332 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3344 * Install or uninstall route for appropriate VNIs/ESIs.
3346 static int bgp_evpn_install_uninstall_table(struct bgp
*bgp
, afi_t afi
,
3347 safi_t safi
, const struct prefix
*p
,
3348 struct bgp_path_info
*pi
,
3349 int import
, bool in_vni_rt
,
3352 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3353 struct attr
*attr
= pi
->attr
;
3354 struct ecommunity
*ecom
;
3356 struct prefix_evpn ad_evp
;
3360 /* Only type-1, type-2, type-3, type-4 and type-5
3361 * are supported currently
3363 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3364 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3365 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3366 || evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
3367 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3370 /* If we don't have Route Target, nothing much to do. */
3371 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3374 /* EAD prefix in the global table doesn't include the VTEP-IP so
3375 * we need to create a different copy for the VNI
3377 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
3378 evp
= evpn_type1_prefix_vni_copy(&ad_evp
, evp
, attr
->nexthop
);
3380 ecom
= bgp_attr_get_ecommunity(attr
);
3381 if (!ecom
|| !ecom
->size
)
3384 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3385 * attached to the route */
3386 for (i
= 0; i
< ecom
->size
; i
++) {
3388 uint8_t type
, sub_type
;
3389 struct ecommunity_val
*eval
;
3390 struct ecommunity_val eval_tmp
;
3391 struct irt_node
*irt
; /* import rt for l2vni */
3392 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3393 struct bgp_evpn_es
*es
;
3395 /* Only deal with RTs */
3396 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
3397 eval
= (struct ecommunity_val
*)(ecom
->val
3398 + (i
* ecom
->unit_size
));
3401 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3404 /* non-local MAC-IP routes in the global route table are linked
3405 * to the destination ES
3407 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3408 bgp_evpn_path_es_link(pi
, 0,
3409 bgp_evpn_attr_get_esi(pi
->attr
));
3412 * macip routes (type-2) are imported into VNI and VRF tables.
3413 * IMET route is imported into VNI table.
3414 * prefix routes are imported into VRF table.
3416 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3417 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3418 evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
||
3419 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3421 irt
= in_vni_rt
? lookup_import_rt(bgp
, eval
) : NULL
;
3423 install_uninstall_route_in_vnis(
3424 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3427 vrf_irt
= in_vrf_rt
? lookup_vrf_import_rt(eval
) : NULL
;
3429 install_uninstall_route_in_vrfs(
3430 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3433 /* Also check for non-exact match.
3434 * In this, we mask out the AS and
3435 * only check on the local-admin sub-field.
3436 * This is to facilitate using
3437 * VNI as the RT for EBGP peering too.
3441 if (type
== ECOMMUNITY_ENCODE_AS
3442 || type
== ECOMMUNITY_ENCODE_AS4
3443 || type
== ECOMMUNITY_ENCODE_IP
) {
3444 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
3445 mask_ecom_global_admin(&eval_tmp
, eval
);
3447 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3450 lookup_vrf_import_rt(&eval_tmp
);
3454 install_uninstall_route_in_vnis(
3455 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3458 install_uninstall_route_in_vrfs(
3459 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3463 /* es route is imported into the es table */
3464 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3466 /* we will match based on the entire esi to avoid
3467 * import of an es route for esi2 into esi1
3469 es
= bgp_evpn_es_find(&evp
->prefix
.es_addr
.esi
);
3470 if (es
&& bgp_evpn_is_es_local(es
))
3471 bgp_evpn_es_route_install_uninstall(
3472 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3480 * Install or uninstall route for appropriate VNIs/ESIs.
3482 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3483 const struct prefix
*p
,
3484 struct bgp_path_info
*pi
, int import
)
3486 return bgp_evpn_install_uninstall_table(bgp
, afi
, safi
, p
, pi
, import
,
3490 void bgp_evpn_import_type2_route(struct bgp_path_info
*pi
, int import
)
3492 struct bgp
*bgp_evpn
;
3494 bgp_evpn
= bgp_get_evpn();
3498 install_uninstall_evpn_route(bgp_evpn
, AFI_L2VPN
, SAFI_EVPN
,
3499 &pi
->net
->p
, pi
, import
);
3503 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3506 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3508 /* Delete ipv4 default route and withdraw from peers */
3509 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3510 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3511 SAFI_UNICAST
, false);
3513 /* delete all ipv4 routes and withdraw from peers */
3514 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3515 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3517 /* Delete ipv6 default route and withdraw from peers */
3518 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3519 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3520 SAFI_UNICAST
, false);
3522 /* delete all ipv6 routes and withdraw from peers */
3523 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3524 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3528 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3531 void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3533 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
3535 bgp_evpn
= bgp_get_evpn();
3539 /* update all ipv4 routes */
3540 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3541 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3543 /* update ipv4 default route and withdraw from peers */
3544 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3545 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3546 SAFI_UNICAST
, true);
3548 /* update all ipv6 routes */
3549 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3550 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3552 /* update ipv6 default route and withdraw from peers */
3553 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3554 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3555 SAFI_UNICAST
, true);
3560 * update and advertise local routes for a VRF as type-5 routes.
3561 * This is invoked upon RD change for a VRF. Note taht the processing is only
3562 * done in the global route table using the routes which already exist in the
3565 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3567 /* skip if the RD is configured */
3568 if (is_vrf_rd_configured(bgp_vrf
))
3571 /* derive the RD for the VRF based on new router-id */
3572 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3574 /* update advertise ipv4|ipv6 routes as type-5 routes */
3575 update_advertise_vrf_routes(bgp_vrf
);
3579 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3580 * This is invoked upon VRF RD change. The processing is done only from global
3583 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3585 /* skip if the RD is configured */
3586 if (is_vrf_rd_configured(bgp_vrf
))
3589 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3590 delete_withdraw_vrf_routes(bgp_vrf
);
3594 * Update and advertise local routes for a VNI. Invoked upon router-id
3595 * change. Note that the processing is done only on the global route table
3596 * using routes that already exist in the per-VNI table.
3598 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3600 struct prefix_evpn p
;
3601 struct bgp_dest
*dest
, *global_dest
;
3602 struct bgp_path_info
*pi
, *global_pi
;
3604 afi_t afi
= AFI_L2VPN
;
3605 safi_t safi
= SAFI_EVPN
;
3607 /* Locate type-3 route for VNI in the per-VNI table and use its
3608 * attributes to create and advertise the type-3 route for this VNI
3609 * in the global table.
3611 * RT-3 only if doing head-end replication
3613 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3614 == VXLAN_FLOOD_HEAD_END_REPL
) {
3615 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3616 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3617 if (!dest
) /* unexpected */
3619 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
3620 if (pi
->peer
== bgp
->peer_self
&&
3621 pi
->type
== ZEBRA_ROUTE_BGP
3622 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3624 if (!pi
) /* unexpected */
3628 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
],
3629 afi
, safi
, &p
, &vpn
->prd
);
3630 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_dest
, attr
,
3631 1, &pi
, 0, mac_mobility_seqnum(attr
),
3632 false /* setup_sync */, NULL
/* old_is_sync */);
3634 /* Schedule for processing and unlock node. */
3635 bgp_process(bgp
, global_dest
, afi
, safi
);
3636 bgp_dest_unlock_node(global_dest
);
3639 /* Now, walk this VNI's route table and use the route and its attribute
3640 * to create and schedule route in global table.
3642 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
3643 dest
= bgp_route_next(dest
)) {
3644 const struct prefix_evpn
*evp
=
3645 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
3648 * We have already processed type-3 routes.
3649 * Process only type-1 and type-2 routes here.
3651 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
3652 && evp
->prefix
.route_type
!= BGP_EVPN_AD_ROUTE
)
3655 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
3656 if (pi
->peer
== bgp
->peer_self
3657 && pi
->type
== ZEBRA_ROUTE_BGP
3658 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3663 /* Create route in global routing table using this route entry's
3667 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3669 assert(global_dest
);
3671 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3673 update_evpn_route_entry(
3674 bgp
, vpn
, afi
, safi
, global_dest
, attr
, 1,
3675 &global_pi
, 0, mac_mobility_seqnum(attr
),
3676 false /* setup_sync */, NULL
/* old_is_sync */);
3679 struct bgp_evpn_es
*es
;
3680 int route_changed
= 0;
3682 es
= bgp_evpn_es_find(&evp
->prefix
.ead_addr
.esi
);
3683 bgp_evpn_mh_route_update(bgp
, es
, vpn
, afi
, safi
,
3684 global_dest
, attr
, &global_pi
,
3688 /* Schedule for processing and unlock node. */
3689 bgp_process(bgp
, global_dest
, afi
, safi
);
3690 bgp_dest_unlock_node(global_dest
);
3697 * Delete (and withdraw) local routes for a VNI - only from the global
3698 * table. Invoked upon router-id change.
3700 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3702 struct prefix_evpn p
;
3703 struct bgp_dest
*global_dest
;
3704 struct bgp_path_info
*pi
;
3705 afi_t afi
= AFI_L2VPN
;
3706 safi_t safi
= SAFI_EVPN
;
3708 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3709 * for this VNI - from the global table.
3711 delete_global_type2_routes(bgp
, vpn
);
3713 /* Remove type-3 route for this VNI from global table. */
3714 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3715 global_dest
= bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3716 (const struct prefix_evpn
*)&p
, &vpn
->prd
);
3718 /* Delete route entry in the global EVPN table. */
3719 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
3721 /* Schedule for processing - withdraws to peers happen from
3725 bgp_process(bgp
, global_dest
, afi
, safi
);
3726 bgp_dest_unlock_node(global_dest
);
3730 delete_global_ead_evi_routes(bgp
, vpn
);
3735 * Handle router-id change. Update and advertise local routes corresponding
3736 * to this VNI from peers. Note that this is invoked after updating the
3737 * router-id. The routes in the per-VNI table are used to create routes in
3738 * the global table and schedule them.
3740 static void update_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3742 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3744 /* Skip VNIs with configured RD. */
3745 if (is_rd_configured(vpn
))
3748 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3749 update_advertise_vni_routes(bgp
, vpn
);
3753 * Handle router-id change. Delete and withdraw local routes corresponding
3754 * to this VNI from peers. Note that this is invoked prior to updating
3755 * the router-id and is done only on the global route table, the routes
3756 * are needed in the per-VNI table to re-advertise with new router id.
3758 static void withdraw_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3760 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3762 /* Skip VNIs with configured RD. */
3763 if (is_rd_configured(vpn
))
3766 delete_withdraw_vni_routes(bgp
, vpn
);
3770 * Create RT-3 for a VNI and schedule for processing and advertisement.
3771 * This is invoked upon flooding mode changing to head-end replication.
3773 static void create_advertise_type3(struct hash_bucket
*bucket
, void *data
)
3775 struct bgpevpn
*vpn
= bucket
->data
;
3776 struct bgp
*bgp
= data
;
3777 struct prefix_evpn p
;
3779 if (!vpn
|| !is_vni_live(vpn
) ||
3780 bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3781 != VXLAN_FLOOD_HEAD_END_REPL
)
3784 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3785 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
))
3786 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3787 "Type3 route creation failure for VNI %u", vpn
->vni
);
3791 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3792 * This is invoked upon flooding mode changing to drop BUM packets.
3794 static void delete_withdraw_type3(struct hash_bucket
*bucket
, void *data
)
3796 struct bgpevpn
*vpn
= bucket
->data
;
3797 struct bgp
*bgp
= data
;
3798 struct prefix_evpn p
;
3800 if (!vpn
|| !is_vni_live(vpn
))
3803 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3804 delete_evpn_route(bgp
, vpn
, &p
);
3808 * Process received EVPN type-2 route (advertise or withdraw).
3810 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3811 struct attr
*attr
, uint8_t *pfx
, int psize
,
3812 uint32_t addpath_id
)
3814 struct prefix_rd prd
;
3815 struct prefix_evpn p
= {};
3816 struct bgp_route_evpn evpn
= {};
3818 uint8_t macaddr_len
;
3819 /* holds the VNI(s) as in packet */
3820 mpls_label_t label
[BGP_MAX_LABELS
] = {};
3821 uint32_t num_labels
= 0;
3825 /* Type-2 route should be either 33, 37 or 49 bytes or an
3826 * additional 3 bytes if there is a second label (VNI):
3827 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3828 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3829 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3831 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3832 && psize
!= 40 && psize
!= 52) {
3833 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3834 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3835 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3839 struct stream
*pkt
= stream_new(psize
);
3840 stream_put(pkt
, pfx
, psize
);
3842 /* Make prefix_rd */
3843 prd
.family
= AF_UNSPEC
;
3846 STREAM_GET(&prd
.val
, pkt
, 8);
3848 /* Make EVPN prefix. */
3850 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3851 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3853 /* Copy Ethernet Seg Identifier */
3855 STREAM_GET(&attr
->esi
, pkt
, sizeof(esi_t
));
3857 if (bgp_evpn_is_esi_local_and_non_bypass(&attr
->esi
))
3858 attr
->es_flags
|= ATTR_ES_IS_LOCAL
;
3860 attr
->es_flags
&= ~ATTR_ES_IS_LOCAL
;
3862 STREAM_FORWARD_GETP(pkt
, sizeof(esi_t
));
3865 /* Copy Ethernet Tag */
3866 STREAM_GET(ð_tag
, pkt
, 4);
3867 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3869 /* Get the MAC Addr len */
3870 STREAM_GETC(pkt
, macaddr_len
);
3872 /* Get the MAC Addr */
3873 if (macaddr_len
== (ETH_ALEN
* 8)) {
3874 STREAM_GET(&p
.prefix
.macip_addr
.mac
.octet
, pkt
, ETH_ALEN
);
3877 EC_BGP_EVPN_ROUTE_INVALID
,
3878 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3879 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3885 STREAM_GETC(pkt
, ipaddr_len
);
3887 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3888 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3890 EC_BGP_EVPN_ROUTE_INVALID
,
3891 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3892 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3897 ipaddr_len
/= 8; /* Convert to bytes. */
3898 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3901 STREAM_GET(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pkt
, ipaddr_len
);
3904 /* Get the VNI(s). Stored as bytes here. */
3905 STREAM_GET(&label
[0], pkt
, BGP_LABEL_BYTES
);
3908 /* Do we have a second VNI? */
3909 if (STREAM_READABLE(pkt
)) {
3911 STREAM_GET(&label
[1], pkt
, BGP_LABEL_BYTES
);
3914 /* Process the route. */
3916 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3917 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3918 &prd
, &label
[0], num_labels
, 0, &evpn
);
3920 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3921 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3922 &prd
, &label
[0], num_labels
, &evpn
);
3927 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3928 "%u:%s - Rx EVPN Type-2 NLRI - corrupt, discarding",
3929 peer
->bgp
->vrf_id
, peer
->host
);
3937 * Process received EVPN type-3 route (advertise or withdraw).
3939 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3940 struct attr
*attr
, uint8_t *pfx
, int psize
,
3941 uint32_t addpath_id
)
3943 struct prefix_rd prd
;
3944 struct prefix_evpn p
;
3949 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3950 * IP len (1) and IP (4 or 16).
3952 if (psize
!= 17 && psize
!= 29) {
3953 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3954 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3955 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3959 /* If PMSI is present, log if it is anything other than IR.
3960 * Note: We just simply ignore the values as it is not clear if
3961 * doing anything else is better.
3964 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3965 enum pta_type pmsi_tnl_type
= bgp_attr_get_pmsi_tnl_type(attr
);
3967 if (pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
3968 && pmsi_tnl_type
!= PMSI_TNLTYPE_PIM_SM
) {
3970 EC_BGP_EVPN_PMSI_PRESENT
,
3971 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3972 peer
->bgp
->vrf_id
, peer
->host
, pmsi_tnl_type
);
3976 /* Make prefix_rd */
3977 prd
.family
= AF_UNSPEC
;
3979 memcpy(&prd
.val
, pfx
, 8);
3982 /* Make EVPN prefix. */
3983 memset(&p
, 0, sizeof(p
));
3985 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3986 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3988 /* Copy Ethernet Tag */
3989 memcpy(ð_tag
, pfx
, 4);
3990 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
3994 ipaddr_len
= *pfx
++;
3995 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3996 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
3997 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
4000 EC_BGP_EVPN_ROUTE_INVALID
,
4001 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
4002 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4006 /* Process the route. */
4008 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4009 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4010 &prd
, NULL
, 0, 0, NULL
);
4012 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4013 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4014 &prd
, NULL
, 0, NULL
);
4019 * Process received EVPN type-5 route (advertise or withdraw).
4021 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4022 struct attr
*attr
, uint8_t *pfx
, int psize
,
4023 uint32_t addpath_id
)
4025 struct prefix_rd prd
;
4026 struct prefix_evpn p
;
4027 struct bgp_route_evpn evpn
;
4030 mpls_label_t label
; /* holds the VNI as in the packet */
4032 bool is_valid_update
= true;
4034 /* Type-5 route should be 34 or 58 bytes:
4035 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
4036 * GW (4 or 16) and VNI (3).
4037 * Note that the IP and GW should both be IPv4 or both IPv6.
4039 if (psize
!= 34 && psize
!= 58) {
4040 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4041 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
4042 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4046 /* Make prefix_rd */
4047 prd
.family
= AF_UNSPEC
;
4049 memcpy(&prd
.val
, pfx
, 8);
4052 /* Make EVPN prefix. */
4053 memset(&p
, 0, sizeof(p
));
4055 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4056 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
4058 /* Additional information outside of prefix - ESI and GW IP */
4059 memset(&evpn
, 0, sizeof(evpn
));
4061 /* Fetch ESI overlay index */
4063 memcpy(&evpn
.eth_s_id
, pfx
, sizeof(esi_t
));
4066 /* Fetch Ethernet Tag. */
4067 memcpy(ð_tag
, pfx
, 4);
4068 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
4071 /* Fetch IP prefix length. */
4073 if (ippfx_len
> IPV6_MAX_BITLEN
) {
4075 EC_BGP_EVPN_ROUTE_INVALID
,
4076 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4077 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
4080 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
4082 /* Determine IPv4 or IPv6 prefix */
4083 /* Since the address and GW are from the same family, this just becomes
4084 * a simple check on the total size.
4087 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
4088 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
4090 SET_IPADDR_V4(&evpn
.gw_ip
);
4091 memcpy(&evpn
.gw_ip
.ipaddr_v4
, pfx
, 4);
4094 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
4095 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
,
4097 pfx
+= IPV6_MAX_BYTELEN
;
4098 SET_IPADDR_V6(&evpn
.gw_ip
);
4099 memcpy(&evpn
.gw_ip
.ipaddr_v6
, pfx
, IPV6_MAX_BYTELEN
);
4100 pfx
+= IPV6_MAX_BYTELEN
;
4103 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4104 memset(&label
, 0, sizeof(label
));
4105 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
4108 * If in future, we are required to access additional fields,
4109 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4114 * An update containing a non-zero gateway IP and a non-zero ESI
4115 * at the same time is should be treated as withdraw
4117 if (bgp_evpn_is_esi_valid(&evpn
.eth_s_id
) &&
4118 !ipaddr_is_zero(&evpn
.gw_ip
)) {
4119 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4120 "%s - Rx EVPN Type-5 ESI and gateway-IP both non-zero.",
4122 is_valid_update
= false;
4123 } else if (bgp_evpn_is_esi_valid(&evpn
.eth_s_id
))
4124 evpn
.type
= OVERLAY_INDEX_ESI
;
4125 else if (!ipaddr_is_zero(&evpn
.gw_ip
))
4126 evpn
.type
= OVERLAY_INDEX_GATEWAY_IP
;
4128 if (is_zero_mac(&attr
->rmac
) &&
4129 !bgp_evpn_is_esi_valid(&evpn
.eth_s_id
) &&
4130 ipaddr_is_zero(&evpn
.gw_ip
) && label
== 0) {
4131 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4132 "%s - Rx EVPN Type-5 ESI, gateway-IP, RMAC and label all zero",
4134 is_valid_update
= false;
4137 if (is_mcast_mac(&attr
->rmac
) || is_bcast_mac(&attr
->rmac
))
4138 is_valid_update
= false;
4141 /* Process the route. */
4142 if (attr
&& is_valid_update
)
4143 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4144 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4145 &prd
, &label
, 1, 0, &evpn
);
4147 if (!is_valid_update
) {
4148 char attr_str
[BUFSIZ
] = {0};
4150 bgp_dump_attr(attr
, attr_str
, BUFSIZ
);
4152 "Invalid update from peer %s vrf %u prefix %pFX attr %s - treat as withdraw",
4153 peer
->hostname
, peer
->bgp
->vrf_id
, &p
,
4156 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4157 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4158 &prd
, &label
, 1, &evpn
);
4164 static void evpn_mpattr_encode_type5(struct stream
*s
, const struct prefix
*p
,
4165 const struct prefix_rd
*prd
,
4166 mpls_label_t
*label
, uint32_t num_labels
,
4171 const struct evpn_addr
*p_evpn_p
;
4173 memset(&temp
, 0, sizeof(temp
));
4174 if (p
->family
!= AF_EVPN
)
4176 p_evpn_p
= &(p
->u
.prefix_evpn
);
4178 /* len denites the total len of IP and GW-IP in the route
4179 IP and GW-IP have to be both ipv4 or ipv6
4181 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4182 len
= 8; /* IP and GWIP are both ipv4 */
4184 len
= 32; /* IP and GWIP are both ipv6 */
4185 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4186 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4187 stream_put(s
, prd
->val
, 8);
4188 if (attr
&& attr
->evpn_overlay
.type
== OVERLAY_INDEX_ESI
)
4189 stream_put(s
, &attr
->esi
, sizeof(esi_t
));
4191 stream_put(s
, 0, sizeof(esi_t
));
4192 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4193 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4194 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4195 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4197 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4198 if (attr
&& attr
->evpn_overlay
.type
== OVERLAY_INDEX_GATEWAY_IP
) {
4199 const struct bgp_route_evpn
*evpn_overlay
=
4200 bgp_attr_get_evpn_overlay(attr
);
4202 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4204 evpn_overlay
->gw_ip
.ipaddr_v4
.s_addr
);
4206 stream_put(s
, &(evpn_overlay
->gw_ip
.ipaddr_v6
), 16);
4208 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4209 stream_put_ipv4(s
, 0);
4211 stream_put(s
, &temp
, 16);
4215 stream_put(s
, label
, 3);
4221 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4223 static void cleanup_vni_on_disable(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4225 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4227 /* Remove EVPN routes and schedule for processing. */
4228 delete_routes_for_vni(bgp
, vpn
);
4230 /* Clear "live" flag and see if hash needs to be freed. */
4231 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4232 if (!is_vni_configured(vpn
))
4233 bgp_evpn_free(bgp
, vpn
);
4237 * Free a VNI entry; iterator function called during cleanup.
4239 static void free_vni_entry(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4241 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4243 delete_all_vni_routes(bgp
, vpn
);
4244 bgp_evpn_free(bgp
, vpn
);
4248 * Derive AUTO import RT for BGP VRF - L3VNI
4250 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4252 struct bgp
*bgp_evpn
= NULL
;
4254 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4255 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4258 bgp_evpn
= bgp_get_evpn();
4261 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4265 * Delete AUTO import RT from BGP VRF - L3VNI
4267 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4269 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4273 * Derive AUTO export RT for BGP VRF - L3VNI
4275 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4277 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4278 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4282 * Delete AUTO export RT from BGP VRF - L3VNI
4284 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4286 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4289 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4291 struct bgp
*bgp_evpn
= NULL
;
4292 struct listnode
*node
= NULL
;
4293 struct bgpevpn
*vpn
= NULL
;
4295 bgp_evpn
= bgp_get_evpn();
4299 /* update all type-5 routes */
4300 update_advertise_vrf_routes(bgp_vrf
);
4302 /* update all type-2 routes */
4303 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4304 update_routes_for_vni(bgp_evpn
, vpn
);
4308 * Handle autort change for a given VNI.
4310 static void update_autort_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4312 struct bgpevpn
*vpn
= bucket
->data
;
4314 if (!is_import_rt_configured(vpn
)) {
4315 if (is_vni_live(vpn
))
4316 bgp_evpn_uninstall_routes(bgp
, vpn
);
4317 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4318 list_delete_all_node(vpn
->import_rtl
);
4319 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4320 if (is_vni_live(vpn
))
4321 bgp_evpn_install_routes(bgp
, vpn
);
4323 if (!is_export_rt_configured(vpn
)) {
4324 list_delete_all_node(vpn
->export_rtl
);
4325 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4326 if (is_vni_live(vpn
))
4327 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4332 * Handle autort change for L3VNI.
4334 static void update_autort_l3vni(struct bgp
*bgp
)
4336 if ((CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
4337 && (CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)))
4340 if (!CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
)) {
4341 if (is_l3vni_live(bgp
))
4342 uninstall_routes_for_vrf(bgp
);
4344 /* Cleanup the RT to VRF mapping */
4345 bgp_evpn_unmap_vrf_from_its_rts(bgp
);
4347 /* Remove auto generated RT */
4348 evpn_auto_rt_import_delete_for_vrf(bgp
);
4350 list_delete_all_node(bgp
->vrf_import_rtl
);
4352 /* Map auto derive or configured RTs */
4353 evpn_auto_rt_import_add_for_vrf(bgp
);
4356 if (!CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
4357 list_delete_all_node(bgp
->vrf_export_rtl
);
4359 evpn_auto_rt_export_delete_for_vrf(bgp
);
4361 evpn_auto_rt_export_add_for_vrf(bgp
);
4363 if (is_l3vni_live(bgp
))
4364 bgp_evpn_map_vrf_to_its_rts(bgp
);
4367 if (!is_l3vni_live(bgp
))
4370 /* advertise type-5 routes if needed */
4371 update_advertise_vrf_routes(bgp
);
4373 /* install all remote routes belonging to this l3vni
4374 * into corresponding vrf
4376 install_routes_for_vrf(bgp
);
4383 /* withdraw type-5 route corresponding to ip prefix */
4384 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4385 afi_t afi
, safi_t safi
)
4388 struct prefix_evpn evp
;
4390 build_type5_prefix_from_ip_prefix(&evp
, p
);
4391 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4394 EC_BGP_EVPN_ROUTE_DELETE
,
4395 "%u failed to delete type-5 route for prefix %pFX in vrf %s",
4396 bgp_vrf
->vrf_id
, p
, vrf_id_to_name(bgp_vrf
->vrf_id
));
4399 /* withdraw all type-5 routes for an address family */
4400 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4402 struct bgp_table
*table
= NULL
;
4403 struct bgp_dest
*dest
= NULL
;
4404 struct bgp_path_info
*pi
;
4406 table
= bgp_vrf
->rib
[afi
][safi
];
4407 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
4408 /* Only care about "selected" routes. Also ensure that
4409 * these are routes that are injectable into EVPN.
4411 /* TODO: Support for AddPath for EVPN. */
4412 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
4413 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4414 && is_route_injectable_into_evpn(pi
)) {
4415 bgp_evpn_withdraw_type5_route(
4416 bgp_vrf
, bgp_dest_get_prefix(dest
), afi
,
4425 * evpn - enable advertisement of default g/w
4427 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
4428 safi_t safi
, bool add
)
4430 struct prefix ip_prefix
;
4432 /* form the default prefix 0.0.0.0/0 */
4433 memset(&ip_prefix
, 0, sizeof(ip_prefix
));
4434 ip_prefix
.family
= afi2family(afi
);
4437 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
4440 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
4447 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4448 * to this function correspond to those of the source IP prefix (best
4449 * path in the case of the attr. In the case of a local prefix (when we
4450 * are advertising local subnets), the src_attr will be NULL.
4452 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4453 struct attr
*src_attr
, afi_t afi
,
4457 struct prefix_evpn evp
;
4459 build_type5_prefix_from_ip_prefix(&evp
, p
);
4460 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
, afi
, safi
);
4462 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4463 "%u: Failed to create type-5 route for prefix %pFX",
4464 bgp_vrf
->vrf_id
, p
);
4467 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4468 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4469 * advertisement is enabled.
4471 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4474 struct bgp_table
*table
= NULL
;
4475 struct bgp_dest
*dest
= NULL
;
4476 struct bgp_path_info
*pi
;
4478 table
= bgp_vrf
->rib
[afi
][safi
];
4479 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
4480 /* Need to identify the "selected" route entry to use its
4481 * attribute. Also, ensure that the route is injectable
4483 * TODO: Support for AddPath for EVPN.
4485 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
4486 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4487 && is_route_injectable_into_evpn(pi
)) {
4489 /* apply the route-map */
4490 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4491 route_map_result_t ret
;
4492 struct bgp_path_info tmp_pi
;
4493 struct bgp_path_info_extra tmp_pie
;
4494 struct attr tmp_attr
;
4496 tmp_attr
= *pi
->attr
;
4498 /* Fill temp path_info */
4499 prep_for_rmap_apply(&tmp_pi
, &tmp_pie
,
4503 RESET_FLAG(tmp_attr
.rmap_change_flags
);
4505 ret
= route_map_apply(
4506 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4508 bgp_dest_get_prefix(dest
),
4510 if (ret
== RMAP_DENYMATCH
) {
4511 bgp_attr_flush(&tmp_attr
);
4514 bgp_evpn_advertise_type5_route(
4516 bgp_dest_get_prefix(dest
),
4517 &tmp_attr
, afi
, safi
);
4519 bgp_evpn_advertise_type5_route(
4521 bgp_dest_get_prefix(dest
),
4522 pi
->attr
, afi
, safi
);
4529 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4531 struct listnode
*node
, *nnode
, *node_to_del
;
4532 struct ecommunity
*ecom
, *ecom_auto
;
4533 struct ecommunity_val eval
;
4535 if (bgp
->advertise_autort_rfc8365
)
4536 vni
|= EVPN_AUTORT_VXLAN
;
4537 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4539 ecom_auto
= ecommunity_new();
4540 ecommunity_add_val(ecom_auto
, &eval
, false, false);
4543 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4544 if (ecommunity_match(ecom
, ecom_auto
)) {
4545 ecommunity_free(&ecom
);
4552 list_delete_node(rtl
, node_to_del
);
4554 ecommunity_free(&ecom_auto
);
4557 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4558 struct ecommunity
*ecomadd
)
4560 /* uninstall routes from vrf */
4561 if (is_l3vni_live(bgp_vrf
))
4562 uninstall_routes_for_vrf(bgp_vrf
);
4564 /* Cleanup the RT to VRF mapping */
4565 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4567 /* Remove auto generated RT */
4568 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4570 /* Add the newly configured RT to RT list */
4571 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4572 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4574 /* map VRF to its RTs and install routes matching the new RTs */
4575 if (is_l3vni_live(bgp_vrf
)) {
4576 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4577 install_routes_for_vrf(bgp_vrf
);
4581 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4582 struct ecommunity
*ecomdel
)
4584 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4585 struct ecommunity
*ecom
= NULL
;
4587 /* uninstall routes from vrf */
4588 if (is_l3vni_live(bgp_vrf
))
4589 uninstall_routes_for_vrf(bgp_vrf
);
4591 /* Cleanup the RT to VRF mapping */
4592 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4594 /* remove the RT from the RT list */
4595 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4596 if (ecommunity_match(ecom
, ecomdel
)) {
4597 ecommunity_free(&ecom
);
4604 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4606 assert(bgp_vrf
->vrf_import_rtl
);
4607 /* fallback to auto import rt, if this was the last RT */
4608 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4609 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4610 if (is_l3vni_live(bgp_vrf
))
4611 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4614 /* map VRFs to its RTs and install routes matching this new RT */
4615 if (is_l3vni_live(bgp_vrf
)) {
4616 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4617 install_routes_for_vrf(bgp_vrf
);
4621 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4622 struct ecommunity
*ecomadd
)
4624 /* remove auto-generated RT */
4625 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4627 /* Add the new RT to the RT list */
4628 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4629 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4631 if (is_l3vni_live(bgp_vrf
))
4632 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4635 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4636 struct ecommunity
*ecomdel
)
4638 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4639 struct ecommunity
*ecom
= NULL
;
4641 /* Remove the RT from the RT list */
4642 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4643 if (ecommunity_match(ecom
, ecomdel
)) {
4644 ecommunity_free(&ecom
);
4651 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4654 * Temporary assert to make SA happy.
4655 * The ALL_LIST_ELEMENTS macro above has a NULL check
4656 * which means that SA is going to complain about
4657 * the list_isempty call, which doesn't NULL check.
4658 * So until we get this situation cleaned up, here
4661 assert(bgp_vrf
->vrf_export_rtl
);
4663 /* fall back to auto-generated RT if this was the last RT */
4664 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4665 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4666 if (is_l3vni_live(bgp_vrf
))
4667 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4670 if (is_l3vni_live(bgp_vrf
))
4671 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4675 * Handle change to BGP router id. This is invoked twice by the change
4676 * handler, first before the router id has been changed and then after
4677 * the router id has been changed. The first invocation will result in
4678 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4679 * will result in the routes being re-advertised.
4681 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4683 struct listnode
*node
;
4684 struct bgp
*bgp_vrf
;
4688 /* delete and withdraw all the type-5 routes
4689 stored in the global table for this vrf
4691 withdraw_router_id_vrf(bgp
);
4693 /* delete all the VNI routes (type-2/type-3) routes for all the
4696 hash_iterate(bgp
->vnihash
,
4697 (void (*)(struct hash_bucket
*,
4698 void *))withdraw_router_id_vni
,
4701 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4702 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4703 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4704 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4706 bgp_vrf
->evpn_info
->pip_ip
.s_addr
4712 /* Assign new default instance router-id */
4713 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4714 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4715 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4716 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4718 bgp_vrf
->evpn_info
->pip_ip
=
4720 /* advertise type-5 routes with
4723 update_advertise_vrf_routes(bgp_vrf
);
4728 /* advertise all routes in the vrf as type-5 routes with the new
4731 update_router_id_vrf(bgp
);
4733 /* advertise all the VNI routes (type-2/type-3) routes with the
4736 hash_iterate(bgp
->vnihash
,
4737 (void (*)(struct hash_bucket
*,
4738 void *))update_router_id_vni
,
4744 * Handle change to auto-RT algorithm - update and advertise local routes.
4746 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4748 hash_iterate(bgp
->vnihash
,
4749 (void (*)(struct hash_bucket
*,
4750 void*))update_autort_vni
,
4753 update_autort_l3vni(bgp
);
4757 * Handle change to export RT - update and advertise local routes.
4759 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4761 return update_routes_for_vni(bgp
, vpn
);
4764 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4767 delete_withdraw_vrf_routes(bgp_vrf
);
4769 update_advertise_vrf_routes(bgp_vrf
);
4773 * Handle change to RD. This is invoked twice by the change handler,
4774 * first before the RD has been changed and then after the RD has
4775 * been changed. The first invocation will result in local routes
4776 * of this VNI being deleted and withdrawn and the next will result
4777 * in the routes being re-advertised.
4779 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4783 delete_withdraw_vni_routes(bgp
, vpn
);
4785 update_advertise_vni_routes(bgp
, vpn
);
4789 * Install routes for this VNI. Invoked upon change to Import RT.
4791 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4793 return install_routes_for_vni(bgp
, vpn
);
4797 * Uninstall all routes installed for this VNI. Invoked upon change
4800 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4802 return uninstall_routes_for_vni(bgp
, vpn
);
4806 * TODO: Hardcoded for a maximum of 2 VNIs right now
4808 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4813 vni1
= label2vni(label
);
4814 if (num_labels
== 2) {
4815 vni2
= label2vni(label
+ 1);
4816 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4818 snprintf(buf
, len
, "%u", vni1
);
4823 * Function to convert evpn route to json format.
4824 * NOTE: We don't use prefix2str as the output here is a bit different.
4826 void bgp_evpn_route2json(const struct prefix_evpn
*p
, json_object
*json
)
4828 char buf1
[ETHER_ADDR_STRLEN
];
4829 char buf2
[PREFIX2STR_BUFFER
];
4836 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4838 switch (p
->prefix
.route_type
) {
4839 case BGP_EVPN_MAC_IP_ROUTE
:
4840 json_object_int_add(json
, "ethTag",
4841 p
->prefix
.macip_addr
.eth_tag
);
4842 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4843 json_object_string_add(json
, "mac",
4844 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4847 if (!is_evpn_prefix_ipaddr_none(p
)) {
4848 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
:
4850 prefixlen
= (family
== AF_INET
) ?
4851 IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
;
4852 inet_ntop(family
, &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4853 buf2
, PREFIX2STR_BUFFER
);
4854 json_object_int_add(json
, "ipLen", prefixlen
);
4855 json_object_string_add(json
, "ip", buf2
);
4859 case BGP_EVPN_IMET_ROUTE
:
4860 json_object_int_add(json
, "ethTag",
4861 p
->prefix
.imet_addr
.eth_tag
);
4862 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4863 prefixlen
= (family
== AF_INET
) ? IPV4_MAX_BITLEN
:
4865 inet_ntop(family
, &p
->prefix
.imet_addr
.ip
.ip
.addr
, buf2
,
4867 json_object_int_add(json
, "ipLen", prefixlen
);
4868 json_object_string_add(json
, "ip", buf2
);
4871 case BGP_EVPN_IP_PREFIX_ROUTE
:
4872 json_object_int_add(json
, "ethTag",
4873 p
->prefix
.prefix_addr
.eth_tag
);
4874 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4875 inet_ntop(family
, &p
->prefix
.prefix_addr
.ip
.ip
.addr
,
4876 buf2
, sizeof(buf2
));
4877 json_object_int_add(json
, "ipLen",
4878 p
->prefix
.prefix_addr
.ip_prefix_length
);
4879 json_object_string_add(json
, "ip", buf2
);
4888 * Encode EVPN prefix in Update (MP_REACH)
4890 void bgp_evpn_encode_prefix(struct stream
*s
, const struct prefix
*p
,
4891 const struct prefix_rd
*prd
, mpls_label_t
*label
,
4892 uint32_t num_labels
, struct attr
*attr
,
4893 bool addpath_capable
, uint32_t addpath_tx_id
)
4895 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4896 int len
, ipa_len
= 0;
4898 if (addpath_capable
)
4899 stream_putl(s
, addpath_tx_id
);
4902 stream_putc(s
, evp
->prefix
.route_type
);
4904 switch (evp
->prefix
.route_type
) {
4905 case BGP_EVPN_MAC_IP_ROUTE
:
4906 if (is_evpn_prefix_ipaddr_v4(evp
))
4907 ipa_len
= IPV4_MAX_BYTELEN
;
4908 else if (is_evpn_prefix_ipaddr_v6(evp
))
4909 ipa_len
= IPV6_MAX_BYTELEN
;
4910 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4911 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4912 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4914 stream_putc(s
, len
);
4915 stream_put(s
, prd
->val
, 8); /* RD */
4917 stream_put(s
, &attr
->esi
, ESI_BYTES
);
4919 stream_put(s
, 0, 10);
4920 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4921 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4922 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4923 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4924 if (ipa_len
) /* IP */
4925 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4927 /* 1st label is the L2 VNI */
4928 stream_put(s
, label
, BGP_LABEL_BYTES
);
4929 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4930 if (ipa_len
&& num_labels
> 1)
4931 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4934 case BGP_EVPN_IMET_ROUTE
:
4935 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4936 stream_put(s
, prd
->val
, 8); /* RD */
4937 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4938 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4939 /* Originating Router's IP Addr */
4940 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4943 case BGP_EVPN_ES_ROUTE
:
4944 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4945 stream_put(s
, prd
->val
, 8); /* RD */
4946 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4947 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4949 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4952 case BGP_EVPN_AD_ROUTE
:
4953 /* RD, ESI, EthTag, 1 VNI */
4954 len
= RD_BYTES
+ ESI_BYTES
+ EVPN_ETH_TAG_BYTES
+ BGP_LABEL_BYTES
;
4955 stream_putc(s
, len
);
4956 stream_put(s
, prd
->val
, RD_BYTES
); /* RD */
4957 stream_put(s
, evp
->prefix
.ead_addr
.esi
.val
, ESI_BYTES
); /* ESI */
4958 stream_putl(s
, evp
->prefix
.ead_addr
.eth_tag
); /* Ethernet Tag */
4959 stream_put(s
, label
, BGP_LABEL_BYTES
);
4962 case BGP_EVPN_IP_PREFIX_ROUTE
:
4963 /* TODO: AddPath support. */
4964 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4972 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4973 struct bgp_nlri
*packet
, int withdraw
)
4979 uint32_t addpath_id
;
4980 bool addpath_capable
;
4985 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4987 lim
= pnt
+ packet
->length
;
4989 safi
= packet
->safi
;
4992 addpath_capable
= bgp_addpath_encode_rx(peer
, afi
, safi
);
4994 for (; pnt
< lim
; pnt
+= psize
) {
4995 /* Clear prefix structure. */
4996 memset(&p
, 0, sizeof(p
));
4998 /* Deal with path-id if AddPath is supported. */
4999 if (addpath_capable
) {
5000 /* When packet overflow occurs return immediately. */
5001 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
5002 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5004 memcpy(&addpath_id
, pnt
, BGP_ADDPATH_ID_LEN
);
5005 addpath_id
= ntohl(addpath_id
);
5006 pnt
+= BGP_ADDPATH_ID_LEN
;
5009 /* All EVPN NLRI types start with type and length. */
5011 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE
;
5016 /* When packet overflow occur return immediately. */
5017 if (pnt
+ psize
> lim
)
5018 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5021 case BGP_EVPN_MAC_IP_ROUTE
:
5022 if (process_type2_route(peer
, afi
, safi
,
5023 withdraw
? NULL
: attr
, pnt
,
5024 psize
, addpath_id
)) {
5027 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
5028 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5029 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE
;
5033 case BGP_EVPN_IMET_ROUTE
:
5034 if (process_type3_route(peer
, afi
, safi
,
5035 withdraw
? NULL
: attr
, pnt
,
5036 psize
, addpath_id
)) {
5039 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
5040 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5041 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE
;
5045 case BGP_EVPN_ES_ROUTE
:
5046 if (bgp_evpn_type4_route_process(peer
, afi
, safi
,
5047 withdraw
? NULL
: attr
, pnt
,
5048 psize
, addpath_id
)) {
5051 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
5052 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5053 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE
;
5057 case BGP_EVPN_AD_ROUTE
:
5058 if (bgp_evpn_type1_route_process(peer
, afi
, safi
,
5059 withdraw
? NULL
: attr
, pnt
,
5060 psize
, addpath_id
)) {
5063 "%u:%s - Error in processing EVPN type-1 NLRI size %d",
5064 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5065 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE1_SIZE
;
5069 case BGP_EVPN_IP_PREFIX_ROUTE
:
5070 if (process_type5_route(peer
, afi
, safi
,
5071 withdraw
? NULL
: attr
, pnt
,
5072 psize
, addpath_id
)) {
5075 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
5076 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5077 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE
;
5086 /* Packet length consistency check. */
5088 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH
;
5090 return BGP_NLRI_PARSE_OK
;
5094 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
5095 * The mapping will be used during route processing.
5096 * bgp_vrf: specific bgp vrf instance on which RT is configured
5098 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
5101 struct ecommunity_val
*eval
= NULL
;
5102 struct listnode
*node
= NULL
, *nnode
= NULL
;
5103 struct ecommunity
*ecom
= NULL
;
5105 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5106 for (i
= 0; i
< ecom
->size
; i
++) {
5107 eval
= (struct ecommunity_val
*)(ecom
->val
5109 * ECOMMUNITY_SIZE
));
5110 map_vrf_to_rt(bgp_vrf
, eval
);
5116 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
5118 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
5121 struct ecommunity_val
*eval
;
5122 struct listnode
*node
, *nnode
;
5123 struct ecommunity
*ecom
;
5125 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5126 for (i
= 0; i
< ecom
->size
; i
++) {
5127 struct vrf_irt_node
*irt
;
5128 struct ecommunity_val eval_tmp
;
5130 eval
= (struct ecommunity_val
*)(ecom
->val
5132 * ECOMMUNITY_SIZE
));
5133 /* If using "automatic" RT, we only care about the
5134 * local-admin sub-field.
5135 * This is to facilitate using VNI as the RT for EBGP
5138 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5139 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
5140 BGP_VRF_IMPORT_RT_CFGD
))
5141 mask_ecom_global_admin(&eval_tmp
, eval
);
5143 irt
= lookup_vrf_import_rt(&eval_tmp
);
5145 unmap_vrf_from_rt(bgp_vrf
, irt
);
5152 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
5153 * The mapping will be used during route processing.
5155 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5158 struct ecommunity_val
*eval
;
5159 struct listnode
*node
, *nnode
;
5160 struct ecommunity
*ecom
;
5162 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5163 for (i
= 0; i
< ecom
->size
; i
++) {
5164 eval
= (struct ecommunity_val
*)(ecom
->val
5166 * ECOMMUNITY_SIZE
));
5167 map_vni_to_rt(bgp
, vpn
, eval
);
5173 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
5175 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5178 struct ecommunity_val
*eval
;
5179 struct listnode
*node
, *nnode
;
5180 struct ecommunity
*ecom
;
5182 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5183 for (i
= 0; i
< ecom
->size
; i
++) {
5184 struct irt_node
*irt
;
5185 struct ecommunity_val eval_tmp
;
5187 eval
= (struct ecommunity_val
*)(ecom
->val
5189 * ECOMMUNITY_SIZE
));
5190 /* If using "automatic" RT, we only care about the
5191 * local-admin sub-field.
5192 * This is to facilitate using VNI as the RT for EBGP
5195 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5196 if (!is_import_rt_configured(vpn
))
5197 mask_ecom_global_admin(&eval_tmp
, eval
);
5199 irt
= lookup_import_rt(bgp
, &eval_tmp
);
5201 unmap_vni_from_rt(bgp
, vpn
, irt
);
5207 * Derive Import RT automatically for VNI and map VNI to RT.
5208 * The mapping will be used during route processing.
5210 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5212 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
5213 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
5216 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
5220 * Derive Export RT automatically for VNI.
5222 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5224 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
5225 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5229 * Derive RD automatically for VNI using passed information - it
5230 * is of the form RouterId:unique-id-for-vni.
5232 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5234 if (is_vrf_rd_configured(bgp
))
5237 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5241 * Derive RD automatically for VNI using passed information - it
5242 * is of the form RouterId:unique-id-for-vni.
5244 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5246 char buf
[BGP_EVPN_PREFIX_RD_LEN
];
5248 vpn
->prd
.family
= AF_UNSPEC
;
5249 vpn
->prd
.prefixlen
= 64;
5250 snprintfrr(buf
, sizeof(buf
), "%pI4:%hu", &bgp
->router_id
, vpn
->rd_id
);
5251 (void)str2prefix_rd(buf
, &vpn
->prd
);
5252 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5258 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5260 struct list
*inst
= bm
->bgp
;
5261 struct listnode
*node
;
5262 struct bgp
*bgp_vrf
;
5264 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5265 if (bgp_vrf
->l3vni
== vni
)
5275 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5277 struct bgpevpn
*vpn
;
5280 memset(&tmp
, 0, sizeof(tmp
));
5282 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5287 * Create a new vpn - invoked upon configuration or zebra notification.
5289 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5290 struct in_addr originator_ip
,
5291 vrf_id_t tenant_vrf_id
,
5292 struct in_addr mcast_grp
,
5293 ifindex_t svi_ifindex
)
5295 struct bgpevpn
*vpn
;
5297 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5299 /* Set values - RD and RT set to defaults. */
5301 vpn
->originator_ip
= originator_ip
;
5302 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5303 vpn
->mcast_grp
= mcast_grp
;
5304 vpn
->svi_ifindex
= svi_ifindex
;
5306 /* Initialize route-target import and export lists */
5307 vpn
->import_rtl
= list_new();
5308 vpn
->import_rtl
->cmp
=
5309 (int (*)(void *, void *))bgp_evpn_route_target_cmp
;
5310 vpn
->import_rtl
->del
= bgp_evpn_xxport_delete_ecomm
;
5311 vpn
->export_rtl
= list_new();
5312 vpn
->export_rtl
->cmp
=
5313 (int (*)(void *, void *))bgp_evpn_route_target_cmp
;
5314 vpn
->export_rtl
->del
= bgp_evpn_xxport_delete_ecomm
;
5315 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5316 derive_rd_rt_for_vni(bgp
, vpn
);
5318 /* Initialize EVPN route table. */
5319 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5322 (void)hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
);
5324 bgp_evpn_remote_ip_hash_init(vpn
);
5325 bgp_evpn_link_to_vni_svi_hash(bgp
, vpn
);
5327 /* add to l2vni list on corresponding vrf */
5328 bgpevpn_link_to_l3vni(vpn
);
5330 bgp_evpn_vni_es_init(vpn
);
5332 QOBJ_REG(vpn
, bgpevpn
);
5337 * Free a given VPN - called in multiple scenarios such as zebra
5338 * notification, configuration being deleted, advertise-all-vni disabled etc.
5339 * This just frees appropriate memory, caller should have taken other
5342 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5344 bgp_evpn_remote_ip_hash_destroy(vpn
);
5345 bgp_evpn_vni_es_cleanup(vpn
);
5346 bgpevpn_unlink_from_l3vni(vpn
);
5347 bgp_table_unlock(vpn
->route_table
);
5348 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5349 list_delete(&vpn
->import_rtl
);
5350 list_delete(&vpn
->export_rtl
);
5351 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5352 hash_release(bgp
->vni_svi_hash
, vpn
);
5353 hash_release(bgp
->vnihash
, vpn
);
5355 XFREE(MTYPE_BGP_EVPN
, vpn
);
5359 * Import evpn route from global table to VNI/VRF/ESI.
5361 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5362 const struct prefix
*p
, struct bgp_path_info
*pi
)
5364 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5368 * Unimport evpn route from VNI/VRF/ESI.
5370 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5371 const struct prefix
*p
, struct bgp_path_info
*pi
)
5373 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5376 /* filter routes which have martian next hops */
5377 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5381 struct bgp_dest
*rd_dest
, *dest
;
5382 struct bgp_table
*table
;
5383 struct bgp_path_info
*pi
;
5388 /* Walk entire global routing table and evaluate routes which could be
5389 * imported into this VPN. Note that we cannot just look at the routes
5390 * for the VNI's RD -
5391 * remote routes applicable for this VNI could have any RD.
5393 /* EVPN routes are a 2-level table. */
5394 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
5395 rd_dest
= bgp_route_next(rd_dest
)) {
5396 table
= bgp_dest_get_bgp_table_info(rd_dest
);
5400 for (dest
= bgp_table_top(table
); dest
;
5401 dest
= bgp_route_next(dest
)) {
5403 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
5406 /* Consider "valid" remote routes applicable for
5408 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5409 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5411 if (bgp_nexthop_self(bgp
, afi
, pi
->type
,
5412 pi
->sub_type
, pi
->attr
,
5414 const struct prefix
*p
=
5415 bgp_dest_get_prefix(dest
);
5417 if (bgp_debug_update(pi
->peer
, p
, NULL
,
5419 char attr_str
[BUFSIZ
] = {0};
5421 bgp_dump_attr(pi
->attr
,
5426 "%u: prefix %pBD with attr %s - DENIED due to martian or self nexthop",
5430 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5433 bgp_rib_remove(dest
, pi
, pi
->peer
, afi
,
5444 * Handle del of a local MACIP.
5446 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5447 struct ipaddr
*ip
, int state
)
5449 struct bgpevpn
*vpn
;
5450 struct prefix_evpn p
;
5451 struct bgp_dest
*dest
;
5453 /* Lookup VNI hash - should exist. */
5454 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5455 if (!vpn
|| !is_vni_live(vpn
)) {
5456 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5457 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5458 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5462 build_evpn_type2_prefix(&p
, mac
, ip
);
5463 if (state
== ZEBRA_NEIGH_ACTIVE
) {
5464 /* Remove EVPN type-2 route and schedule for processing. */
5465 delete_evpn_route(bgp
, vpn
, &p
);
5467 /* Re-instate the current remote best path if any */
5468 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
5470 evpn_zebra_reinstall_best_route(bgp
, vpn
, dest
);
5471 bgp_dest_unlock_node(dest
);
5479 * Handle add of a local MACIP.
5481 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5482 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
, esi_t
*esi
)
5484 struct bgpevpn
*vpn
;
5485 struct prefix_evpn p
;
5487 /* Lookup VNI hash - should exist. */
5488 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5489 if (!vpn
|| !is_vni_live(vpn
)) {
5490 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5491 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5492 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5496 /* Create EVPN type-2 route and schedule for processing. */
5497 build_evpn_type2_prefix(&p
, mac
, ip
);
5498 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
, esi
)) {
5500 EC_BGP_EVPN_ROUTE_CREATE
,
5501 "%u:Failed to create Type-2 route, VNI %u %s MAC %pEA IP %pIA (flags: 0x%x)",
5502 bgp
->vrf_id
, vpn
->vni
,
5503 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5513 static void link_l2vni_hash_to_l3vni(struct hash_bucket
*bucket
,
5514 struct bgp
*bgp_vrf
)
5516 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
5517 struct bgp
*bgp_evpn
= NULL
;
5519 bgp_evpn
= bgp_get_evpn();
5522 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5523 bgpevpn_link_to_l3vni(vpn
);
5526 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
,
5527 struct ethaddr
*svi_rmac
,
5528 struct ethaddr
*vrr_rmac
,
5529 struct in_addr originator_ip
, int filter
,
5530 ifindex_t svi_ifindex
,
5531 bool is_anycast_mac
)
5533 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5534 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5535 struct listnode
*node
= NULL
;
5536 struct bgpevpn
*vpn
= NULL
;
5539 /* get the EVPN instance - required to get the AS number for VRF
5542 bgp_evpn
= bgp_get_evpn();
5546 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
5552 /* if the BGP vrf instance doesn't exist - create one */
5553 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5558 ret
= bgp_get_vty(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5559 vrf_id
== VRF_DEFAULT
5560 ? BGP_INSTANCE_TYPE_DEFAULT
5561 : BGP_INSTANCE_TYPE_VRF
);
5563 case BGP_ERR_AS_MISMATCH
:
5564 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5565 "BGP instance is already running; AS is %u",
5568 case BGP_ERR_INSTANCE_MISMATCH
:
5569 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5570 "BGP instance type mismatch");
5574 /* mark as auto created */
5575 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5578 /* associate the vrf with l3vni and related parameters */
5579 bgp_vrf
->l3vni
= l3vni
;
5580 bgp_vrf
->originator_ip
= originator_ip
;
5581 bgp_vrf
->l3vni_svi_ifindex
= svi_ifindex
;
5582 bgp_vrf
->evpn_info
->is_anycast_mac
= is_anycast_mac
;
5584 /* copy anycast MAC from VRR MAC */
5585 memcpy(&bgp_vrf
->rmac
, vrr_rmac
, ETH_ALEN
);
5586 /* copy sys RMAC from SVI MAC */
5587 memcpy(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, svi_rmac
, ETH_ALEN
);
5588 /* PIP user configured mac is not present use svi mac as sys mac */
5589 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
))
5590 memcpy(&bgp_vrf
->evpn_info
->pip_rmac
, svi_rmac
, ETH_ALEN
);
5592 if (bgp_debug_zebra(NULL
))
5594 "VRF %s vni %u pip %s RMAC %pEA sys RMAC %pEA static RMAC %pEA is_anycast_mac %s",
5595 vrf_id_to_name(bgp_vrf
->vrf_id
), bgp_vrf
->l3vni
,
5596 bgp_vrf
->evpn_info
->advertise_pip
? "enable"
5598 &bgp_vrf
->rmac
, &bgp_vrf
->evpn_info
->pip_rmac
,
5599 &bgp_vrf
->evpn_info
->pip_rmac_static
,
5600 is_anycast_mac
? "Enable" : "Disable");
5602 /* set the right filter - are we using l3vni only for prefix routes? */
5604 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5607 * VNI_FLAG_USE_TWO_LABELS flag for linked L2VNIs should not be
5608 * set before linking vrf to L3VNI. Thus, no need to clear
5612 UNSET_FLAG(bgp_vrf
->vrf_flags
,
5613 BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5615 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5616 if (!CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
5619 * If we are flapping VNI_FLAG_USE_TWO_LABELS
5620 * flag, update all MACIP routes in this VNI
5622 SET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5623 update_all_type2_routes(bgp_evpn
, vpn
);
5628 /* Map auto derive or configured RTs */
5629 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5630 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5632 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
5634 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5635 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5637 /* auto derive RD */
5638 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5640 /* link all corresponding l2vnis */
5641 hash_iterate(bgp_evpn
->vnihash
,
5642 (void (*)(struct hash_bucket
*,
5643 void *))link_l2vni_hash_to_l3vni
,
5646 /* Only update all corresponding type-2 routes if we are advertising two
5647 * labels along with type-2 routes
5650 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5651 update_routes_for_vni(bgp_evpn
, vpn
);
5653 /* advertise type-5 routes if needed */
5654 update_advertise_vrf_routes(bgp_vrf
);
5656 /* install all remote routes belonging to this l3vni into correspondng
5658 install_routes_for_vrf(bgp_vrf
);
5663 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5665 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5666 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5667 struct listnode
*node
= NULL
;
5668 struct listnode
*next
= NULL
;
5669 struct bgpevpn
*vpn
= NULL
;
5671 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5675 "Cannot process L3VNI %u Del - Could not find BGP instance",
5680 bgp_evpn
= bgp_get_evpn();
5684 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
5689 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5690 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5691 * routes. This will uninstalling the routes from zebra and decremnt the
5694 uninstall_routes_for_vrf(bgp_vrf
);
5696 /* delete/withdraw all type-5 routes */
5697 delete_withdraw_vrf_routes(bgp_vrf
);
5699 /* remove the l3vni from vrf instance */
5702 /* remove the Rmac from the BGP vrf */
5703 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5704 memset(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, 0, ETH_ALEN
);
5705 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
) &&
5706 !is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac
))
5707 memset(&bgp_vrf
->evpn_info
->pip_rmac
, 0, ETH_ALEN
);
5709 /* remove default import RT or Unmap non-default import RT */
5710 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5711 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5712 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5713 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5716 /* remove default export RT */
5717 if (!list_isempty(bgp_vrf
->vrf_export_rtl
) &&
5718 !CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
5719 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5722 /* update all corresponding local mac-ip routes */
5723 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5724 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5725 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5726 update_routes_for_vni(bgp_evpn
, vpn
);
5730 /* If any L2VNIs point to this instance, unlink them. */
5731 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5732 bgpevpn_unlink_from_l3vni(vpn
);
5734 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5736 /* Delete the instance if it was autocreated */
5737 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5738 bgp_delete(bgp_vrf
);
5744 * Handle del of a local VNI.
5746 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5748 struct bgpevpn
*vpn
;
5750 /* Locate VNI hash */
5751 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5753 if (bgp_debug_zebra(NULL
))
5755 EC_BGP_EVPN_VPN_VNI
,
5756 "%u: VNI hash entry for VNI %u not found at DEL",
5761 /* Remove all local EVPN routes and schedule for processing (to
5762 * withdraw from peers).
5764 delete_routes_for_vni(bgp
, vpn
);
5766 bgp_evpn_unlink_from_vni_svi_hash(bgp
, vpn
);
5768 vpn
->svi_ifindex
= 0;
5770 * tunnel is no longer active, del tunnel ip address from tip_hash
5772 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5774 /* Clear "live" flag and see if hash needs to be freed. */
5775 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5776 if (!is_vni_configured(vpn
))
5777 bgp_evpn_free(bgp
, vpn
);
5783 * Handle add (or update) of a local VNI. The VNI changes we care
5784 * about are for the local-tunnel-ip and the (tenant) VRF.
5786 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5787 struct in_addr originator_ip
,
5788 vrf_id_t tenant_vrf_id
,
5789 struct in_addr mcast_grp
,
5790 ifindex_t svi_ifindex
)
5792 struct bgpevpn
*vpn
;
5793 struct prefix_evpn p
;
5795 /* Lookup VNI. If present and no change, exit. */
5796 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5799 if (is_vni_live(vpn
)
5800 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5801 && IPV4_ADDR_SAME(&vpn
->mcast_grp
, &mcast_grp
)
5802 && vpn
->tenant_vrf_id
== tenant_vrf_id
5803 && vpn
->svi_ifindex
== svi_ifindex
)
5804 /* Probably some other param has changed that we don't
5808 bgp_evpn_mcast_grp_change(bgp
, vpn
, mcast_grp
);
5810 if (vpn
->svi_ifindex
!= svi_ifindex
) {
5813 * Unresolve all the gateway IP nexthops for this VNI
5816 bgp_evpn_remote_ip_hash_iterate(
5818 (void (*)(struct hash_bucket
*, void *))
5819 bgp_evpn_remote_ip_hash_unlink_nexthop
,
5821 bgp_evpn_unlink_from_vni_svi_hash(bgp
, vpn
);
5822 vpn
->svi_ifindex
= svi_ifindex
;
5823 bgp_evpn_link_to_vni_svi_hash(bgp
, vpn
);
5826 * Resolve all the gateway IP nexthops for this VNI
5829 bgp_evpn_remote_ip_hash_iterate(
5831 (void (*)(struct hash_bucket
*, void *))
5832 bgp_evpn_remote_ip_hash_link_nexthop
,
5836 /* Update tenant_vrf_id if it has changed. */
5837 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5840 * Unresolve all the gateway IP nexthops for this VNI
5843 bgp_evpn_remote_ip_hash_iterate(
5845 (void (*)(struct hash_bucket
*, void *))
5846 bgp_evpn_remote_ip_hash_unlink_nexthop
,
5848 bgpevpn_unlink_from_l3vni(vpn
);
5849 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5850 bgpevpn_link_to_l3vni(vpn
);
5853 * Resolve all the gateway IP nexthops for this VNI
5856 bgp_evpn_remote_ip_hash_iterate(
5858 (void (*)(struct hash_bucket
*, void *))
5859 bgp_evpn_remote_ip_hash_link_nexthop
,
5863 /* If tunnel endpoint IP has changed, update (and delete prior
5864 * type-3 route, if needed.)
5866 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5867 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5869 /* Update all routes with new endpoint IP and/or export RT
5872 if (is_vni_live(vpn
))
5873 update_routes_for_vni(bgp
, vpn
);
5875 /* Create or update as appropriate. */
5876 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
,
5877 mcast_grp
, svi_ifindex
);
5880 /* if the VNI is live already, there is nothing more to do */
5881 if (is_vni_live(vpn
))
5884 /* Mark as "live" */
5885 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5887 /* tunnel is now active, add tunnel-ip to db */
5888 bgp_tip_add(bgp
, &originator_ip
);
5890 /* filter routes as nexthop database has changed */
5891 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5894 * Create EVPN type-3 route and schedule for processing.
5896 * RT-3 only if doing head-end replication
5898 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
5899 == VXLAN_FLOOD_HEAD_END_REPL
) {
5900 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5901 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
)) {
5902 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5903 "%u: Type3 route creation failure for VNI %u",
5909 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5913 install_routes_for_vni(bgp
, vpn
);
5915 /* If we are advertising gateway mac-ip
5916 It needs to be conveyed again to zebra */
5917 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5919 /* advertise svi mac-ip knob to zebra */
5920 bgp_zebra_advertise_svi_macip(bgp
, vpn
->advertise_svi_macip
, vpn
->vni
);
5926 * Handle change in setting for BUM handling. The supported values
5927 * are head-end replication and dropping all BUM packets. Any change
5928 * should be registered with zebra. Also, if doing head-end replication,
5929 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5930 * to be dropped, the RT-3s must be withdrawn.
5932 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
5934 zlog_info("L2VPN EVPN BUM handling is %s",
5935 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
5936 "Flooding" : "Flooding Disabled");
5938 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
5939 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
5940 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
5941 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
5942 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
5946 * Cleanup EVPN information on disable - Need to delete and withdraw
5947 * EVPN routes from peers.
5949 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5951 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_bucket
*,
5952 void *))cleanup_vni_on_disable
,
5957 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5958 * BGP instance (default) is being freed.
5960 void bgp_evpn_cleanup(struct bgp
*bgp
)
5962 hash_iterate(bgp
->vnihash
,
5963 (void (*)(struct hash_bucket
*, void *))free_vni_entry
,
5966 hash_free(bgp
->import_rt_hash
);
5967 bgp
->import_rt_hash
= NULL
;
5969 hash_free(bgp
->vrf_import_rt_hash
);
5970 bgp
->vrf_import_rt_hash
= NULL
;
5972 hash_free(bgp
->vni_svi_hash
);
5973 bgp
->vni_svi_hash
= NULL
;
5974 hash_free(bgp
->vnihash
);
5975 bgp
->vnihash
= NULL
;
5977 list_delete(&bgp
->vrf_import_rtl
);
5978 list_delete(&bgp
->vrf_export_rtl
);
5979 list_delete(&bgp
->l2vnis
);
5983 * Initialization for EVPN
5986 * hash for RT to VNI
5988 void bgp_evpn_init(struct bgp
*bgp
)
5991 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
5993 hash_create(vni_svi_hash_key_make
, vni_svi_hash_cmp
,
5994 "BGP VNI hash based on SVI ifindex");
5995 bgp
->import_rt_hash
=
5996 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
5997 "BGP Import RT Hash");
5998 bgp
->vrf_import_rt_hash
=
5999 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
6000 "BGP VRF Import RT Hash");
6001 bgp
->vrf_import_rtl
= list_new();
6002 bgp
->vrf_import_rtl
->cmp
=
6003 (int (*)(void *, void *))bgp_evpn_route_target_cmp
;
6004 bgp
->vrf_import_rtl
->del
= bgp_evpn_xxport_delete_ecomm
;
6005 bgp
->vrf_export_rtl
= list_new();
6006 bgp
->vrf_export_rtl
->cmp
=
6007 (int (*)(void *, void *))bgp_evpn_route_target_cmp
;
6008 bgp
->vrf_export_rtl
->del
= bgp_evpn_xxport_delete_ecomm
;
6009 bgp
->l2vnis
= list_new();
6010 bgp
->l2vnis
->cmp
= vni_list_cmp
;
6011 /* By default Duplicate Address Dection is enabled.
6012 * Max-moves (N) 5, detection time (M) 180
6013 * default action is warning-only
6014 * freeze action permanently freezes address,
6015 * and freeze time (auto-recovery) is disabled.
6017 if (bgp
->evpn_info
) {
6018 bgp
->evpn_info
->dup_addr_detect
= true;
6019 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
6020 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
6021 bgp
->evpn_info
->dad_freeze
= false;
6022 bgp
->evpn_info
->dad_freeze_time
= 0;
6023 /* Initialize zebra vxlan */
6024 bgp_zebra_dup_addr_detection(bgp
);
6025 /* Enable PIP feature by default for bgp vrf instance */
6026 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
6027 struct bgp
*bgp_default
;
6029 bgp
->evpn_info
->advertise_pip
= true;
6030 bgp_default
= bgp_get_default();
6032 bgp
->evpn_info
->pip_ip
= bgp_default
->router_id
;
6036 /* Default BUM handling is to do head-end replication. */
6037 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
6039 bgp_evpn_nh_init(bgp
);
6042 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
6044 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
6045 bgp_evpn_nh_finish(bgp_vrf
);
6049 * Get the prefixlen of the ip prefix carried within the type5 evpn route.
6051 int bgp_evpn_get_type5_prefixlen(const struct prefix
*pfx
)
6053 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6055 if (!pfx
|| pfx
->family
!= AF_EVPN
)
6058 if (evp
->prefix
.route_type
!= BGP_EVPN_IP_PREFIX_ROUTE
)
6061 return evp
->prefix
.prefix_addr
.ip_prefix_length
;
6065 * Should we register nexthop for this EVPN prefix for nexthop tracking?
6067 bool bgp_evpn_is_prefix_nht_supported(const struct prefix
*pfx
)
6069 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6072 * EVPN routes should be marked as valid only if the nexthop is
6073 * reachable. Only if this happens, the route should be imported
6074 * (into VNI or VRF routing tables) and/or advertised.
6075 * Note: This is currently applied for EVPN type-1, type-2,
6076 * type-3, type-4 and type-5 routes.
6077 * It may be tweaked later on for other routes, or
6078 * even removed completely when all routes are handled.
6080 if (pfx
&& pfx
->family
== AF_EVPN
6081 && (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
6082 || evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
6083 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
6084 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
6085 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
6091 static void *bgp_evpn_remote_ip_hash_alloc(void *p
)
6093 const struct evpn_remote_ip
*key
= (const struct evpn_remote_ip
*)p
;
6094 struct evpn_remote_ip
*ip
;
6096 ip
= XMALLOC(MTYPE_EVPN_REMOTE_IP
, sizeof(struct evpn_remote_ip
));
6098 ip
->macip_path_list
= list_new();
6103 static unsigned int bgp_evpn_remote_ip_hash_key_make(const void *p
)
6105 const struct evpn_remote_ip
*ip
= p
;
6106 const struct ipaddr
*addr
= &ip
->addr
;
6108 if (IS_IPADDR_V4(addr
))
6109 return jhash_1word(addr
->ipaddr_v4
.s_addr
, 0);
6111 return jhash2(addr
->ipaddr_v6
.s6_addr32
,
6112 array_size(addr
->ipaddr_v6
.s6_addr32
), 0);
6115 static bool bgp_evpn_remote_ip_hash_cmp(const void *p1
, const void *p2
)
6117 const struct evpn_remote_ip
*ip1
= p1
;
6118 const struct evpn_remote_ip
*ip2
= p2
;
6120 return !ipaddr_cmp(&ip1
->addr
, &ip2
->addr
);
6123 static void bgp_evpn_remote_ip_hash_init(struct bgpevpn
*vpn
)
6125 if (!evpn_resolve_overlay_index())
6128 vpn
->remote_ip_hash
= hash_create(bgp_evpn_remote_ip_hash_key_make
,
6129 bgp_evpn_remote_ip_hash_cmp
,
6130 "BGP EVPN remote IP hash");
6133 static void bgp_evpn_remote_ip_hash_free(struct hash_bucket
*bucket
, void *args
)
6135 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6136 struct bgpevpn
*vpn
= (struct bgpevpn
*)args
;
6138 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, false);
6140 list_delete(&ip
->macip_path_list
);
6142 hash_release(vpn
->remote_ip_hash
, ip
);
6143 XFREE(MTYPE_EVPN_REMOTE_IP
, ip
);
6146 static void bgp_evpn_remote_ip_hash_destroy(struct bgpevpn
*vpn
)
6148 if (!evpn_resolve_overlay_index() || vpn
->remote_ip_hash
== NULL
)
6151 hash_iterate(vpn
->remote_ip_hash
,
6152 (void (*)(struct hash_bucket
*, void *))bgp_evpn_remote_ip_hash_free
,
6155 hash_free(vpn
->remote_ip_hash
);
6156 vpn
->remote_ip_hash
= NULL
;
6159 /* Add a remote MAC/IP route to hash table */
6160 static void bgp_evpn_remote_ip_hash_add(struct bgpevpn
*vpn
,
6161 struct bgp_path_info
*pi
)
6163 struct evpn_remote_ip tmp
;
6164 struct evpn_remote_ip
*ip
;
6165 struct prefix_evpn
*evp
;
6167 if (!evpn_resolve_overlay_index())
6170 if (pi
->type
!= ZEBRA_ROUTE_BGP
|| pi
->sub_type
!= BGP_ROUTE_IMPORTED
6171 || !CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
))
6174 evp
= (struct prefix_evpn
*)&pi
->net
->p
;
6176 if (evp
->family
!= AF_EVPN
6177 || evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
6178 || is_evpn_prefix_ipaddr_none(evp
))
6181 tmp
.addr
= evp
->prefix
.macip_addr
.ip
;
6182 ip
= hash_lookup(vpn
->remote_ip_hash
, &tmp
);
6184 if (listnode_lookup(ip
->macip_path_list
, pi
) != NULL
)
6186 (void)listnode_add(ip
->macip_path_list
, pi
);
6190 ip
= hash_get(vpn
->remote_ip_hash
, &tmp
, bgp_evpn_remote_ip_hash_alloc
);
6191 (void)listnode_add(ip
->macip_path_list
, pi
);
6193 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, true);
6196 /* Delete a remote MAC/IP route from hash table */
6197 static void bgp_evpn_remote_ip_hash_del(struct bgpevpn
*vpn
,
6198 struct bgp_path_info
*pi
)
6200 struct evpn_remote_ip tmp
;
6201 struct evpn_remote_ip
*ip
;
6202 struct prefix_evpn
*evp
;
6204 if (!evpn_resolve_overlay_index())
6207 evp
= (struct prefix_evpn
*)&pi
->net
->p
;
6209 if (evp
->family
!= AF_EVPN
6210 || evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
6211 || is_evpn_prefix_ipaddr_none(evp
))
6214 tmp
.addr
= evp
->prefix
.macip_addr
.ip
;
6215 ip
= hash_lookup(vpn
->remote_ip_hash
, &tmp
);
6219 listnode_delete(ip
->macip_path_list
, pi
);
6221 if (ip
->macip_path_list
->count
== 0) {
6222 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, false);
6223 hash_release(vpn
->remote_ip_hash
, ip
);
6224 XFREE(MTYPE_EVPN_REMOTE_IP
, ip
);
6228 static void bgp_evpn_remote_ip_hash_iterate(struct bgpevpn
*vpn
,
6229 void (*func
)(struct hash_bucket
*,
6233 if (!evpn_resolve_overlay_index())
6236 hash_iterate(vpn
->remote_ip_hash
, func
, arg
);
6239 static void show_remote_ip_entry(struct hash_bucket
*bucket
, void *args
)
6241 char buf
[INET6_ADDRSTRLEN
];
6242 struct listnode
*node
= NULL
;
6243 struct bgp_path_info
*pi
= NULL
;
6244 struct vty
*vty
= (struct vty
*)args
;
6245 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6247 vty_out(vty
, " Remote IP: %s\n",
6248 ipaddr2str(&ip
->addr
, buf
, sizeof(buf
)));
6249 vty_out(vty
, " Linked MAC/IP routes:\n");
6250 for (ALL_LIST_ELEMENTS_RO(ip
->macip_path_list
, node
, pi
))
6251 vty_out(vty
, " %pFX\n", &pi
->net
->p
);
6254 void bgp_evpn_show_remote_ip_hash(struct hash_bucket
*bucket
, void *args
)
6256 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
6257 struct vty
*vty
= (struct vty
*)args
;
6259 vty_out(vty
, "VNI: %u\n", vpn
->vni
);
6260 bgp_evpn_remote_ip_hash_iterate(
6262 (void (*)(struct hash_bucket
*, void *))show_remote_ip_entry
,
6267 static void bgp_evpn_remote_ip_hash_link_nexthop(struct hash_bucket
*bucket
,
6270 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6271 struct bgpevpn
*vpn
= (struct bgpevpn
*)args
;
6273 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, true);
6276 static void bgp_evpn_remote_ip_hash_unlink_nexthop(struct hash_bucket
*bucket
,
6279 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6280 struct bgpevpn
*vpn
= (struct bgpevpn
*)args
;
6282 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, false);
6285 static unsigned int vni_svi_hash_key_make(const void *p
)
6287 const struct bgpevpn
*vpn
= p
;
6289 return jhash_1word(vpn
->svi_ifindex
, 0);
6292 static bool vni_svi_hash_cmp(const void *p1
, const void *p2
)
6294 const struct bgpevpn
*vpn1
= p1
;
6295 const struct bgpevpn
*vpn2
= p2
;
6297 return (vpn1
->svi_ifindex
== vpn2
->svi_ifindex
);
6300 static struct bgpevpn
*bgp_evpn_vni_svi_hash_lookup(struct bgp
*bgp
,
6303 struct bgpevpn
*vpn
;
6306 memset(&tmp
, 0, sizeof(tmp
));
6307 tmp
.svi_ifindex
= svi
;
6308 vpn
= hash_lookup(bgp
->vni_svi_hash
, &tmp
);
6312 static void bgp_evpn_link_to_vni_svi_hash(struct bgp
*bgp
, struct bgpevpn
*vpn
)
6314 if (vpn
->svi_ifindex
== 0)
6317 (void)hash_get(bgp
->vni_svi_hash
, vpn
, hash_alloc_intern
);
6320 static void bgp_evpn_unlink_from_vni_svi_hash(struct bgp
*bgp
,
6321 struct bgpevpn
*vpn
)
6323 if (vpn
->svi_ifindex
== 0)
6326 hash_release(bgp
->vni_svi_hash
, vpn
);
6329 void bgp_evpn_show_vni_svi_hash(struct hash_bucket
*bucket
, void *args
)
6331 struct bgpevpn
*evpn
= (struct bgpevpn
*)bucket
->data
;
6332 struct vty
*vty
= (struct vty
*)args
;
6334 vty_out(vty
, "SVI: %u VNI: %u\n", evpn
->svi_ifindex
, evpn
->vni
);
6338 * This function is called for a bgp_nexthop_cache entry when the nexthop is
6339 * gateway IP overlay index.
6340 * This function returns true if there is a remote MAC/IP route for the gateway
6341 * IP in the EVI of the nexthop SVI.
6343 bool bgp_evpn_is_gateway_ip_resolved(struct bgp_nexthop_cache
*bnc
)
6345 struct bgp
*bgp_evpn
= NULL
;
6346 struct bgpevpn
*vpn
= NULL
;
6347 struct evpn_remote_ip tmp
;
6350 if (!evpn_resolve_overlay_index())
6353 if (!bnc
->nexthop
|| bnc
->nexthop
->ifindex
== 0)
6356 bgp_evpn
= bgp_get_evpn();
6361 * Gateway IP is resolved by nht over SVI interface.
6362 * Use this SVI to find corresponding EVI(L2 context)
6364 vpn
= bgp_evpn_vni_svi_hash_lookup(bgp_evpn
, bnc
->nexthop
->ifindex
);
6368 if (vpn
->bgp_vrf
!= bnc
->bgp
)
6372 * Check if the gateway IP is present in the EVI remote_ip_hash table
6373 * which stores all the remote IP addresses received via MAC/IP routes
6376 memset(&tmp
, 0, sizeof(tmp
));
6379 if (p
->family
== AF_INET
) {
6380 tmp
.addr
.ipa_type
= IPADDR_V4
;
6381 memcpy(&(tmp
.addr
.ipaddr_v4
), &(p
->u
.prefix4
),
6382 sizeof(struct in_addr
));
6383 } else if (p
->family
== AF_INET6
) {
6384 tmp
.addr
.ipa_type
= IPADDR_V6
;
6385 memcpy(&(tmp
.addr
.ipaddr_v6
), &(p
->u
.prefix6
),
6386 sizeof(struct in6_addr
));
6390 if (hash_lookup(vpn
->remote_ip_hash
, &tmp
) == NULL
)
6396 /* Resolve/Unresolve nexthops when a MAC/IP route is added/deleted */
6397 static void bgp_evpn_remote_ip_process_nexthops(struct bgpevpn
*vpn
,
6398 struct ipaddr
*addr
,
6403 struct bgp_nexthop_cache
*bnc
;
6404 struct bgp_nexthop_cache_head
*tree
= NULL
;
6406 if (!vpn
->bgp_vrf
|| vpn
->svi_ifindex
== 0)
6409 memset(&p
, 0, sizeof(p
));
6411 if (addr
->ipa_type
== IPADDR_V4
) {
6414 memcpy(&(p
.u
.prefix4
), &(addr
->ipaddr_v4
),
6415 sizeof(struct in_addr
));
6416 p
.prefixlen
= IPV4_MAX_BITLEN
;
6417 } else if (addr
->ipa_type
== IPADDR_V6
) {
6419 p
.family
= AF_INET6
;
6420 memcpy(&(p
.u
.prefix6
), &(addr
->ipaddr_v6
),
6421 sizeof(struct in6_addr
));
6422 p
.prefixlen
= IPV6_MAX_BITLEN
;
6426 tree
= &vpn
->bgp_vrf
->nexthop_cache_table
[afi
];
6427 bnc
= bnc_find(tree
, &p
, 0, 0);
6429 if (!bnc
|| !bnc
->is_evpn_gwip_nexthop
)
6432 if (!bnc
->nexthop
|| bnc
->nexthop
->ifindex
!= vpn
->svi_ifindex
)
6435 if (BGP_DEBUG(nht
, NHT
))
6436 zlog_debug("%s(%u): vni %u mac/ip %s for NH %pFX",
6437 vpn
->bgp_vrf
->name_pretty
, vpn
->tenant_vrf_id
,
6438 vpn
->vni
, (resolve
? "add" : "delete"),
6442 * MAC/IP route or SVI or tenant vrf being added to EVI.
6443 * Set nexthop as valid only if it is already L3 reachable
6445 if (resolve
&& bnc
->flags
& BGP_NEXTHOP_EVPN_INCOMPLETE
) {
6446 bnc
->flags
&= ~BGP_NEXTHOP_EVPN_INCOMPLETE
;
6447 bnc
->flags
|= BGP_NEXTHOP_VALID
;
6448 bnc
->change_flags
|= BGP_NEXTHOP_MACIP_CHANGED
;
6449 evaluate_paths(bnc
);
6452 /* MAC/IP route or SVI or tenant vrf being deleted from EVI */
6453 if (!resolve
&& bnc
->flags
& BGP_NEXTHOP_VALID
) {
6454 bnc
->flags
&= ~BGP_NEXTHOP_VALID
;
6455 bnc
->flags
|= BGP_NEXTHOP_EVPN_INCOMPLETE
;
6456 bnc
->change_flags
|= BGP_NEXTHOP_MACIP_CHANGED
;
6457 evaluate_paths(bnc
);
6461 void bgp_evpn_handle_resolve_overlay_index_set(struct hash_bucket
*bucket
,
6464 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
6465 struct bgp_dest
*dest
;
6466 struct bgp_path_info
*pi
;
6468 bgp_evpn_remote_ip_hash_init(vpn
);
6470 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
6471 dest
= bgp_route_next(dest
))
6472 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
6473 bgp_evpn_remote_ip_hash_add(vpn
, pi
);
6476 void bgp_evpn_handle_resolve_overlay_index_unset(struct hash_bucket
*bucket
,
6479 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
6481 bgp_evpn_remote_ip_hash_destroy(vpn
);