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
= bgp_clock();
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
= bgp_clock();
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 if (bgp_debug_zebra(NULL
)) {
1757 char buf3
[ESI_STR_LEN
];
1760 "VRF %s vni %u type-2 route evp %pFX RMAC %pEA nexthop %pI4 esi %s",
1761 vpn
->bgp_vrf
? vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
)
1763 vpn
->vni
, p
, &attr
.rmac
, &attr
.mp_nexthop_global_in
,
1764 esi_to_str(esi
, buf3
, sizeof(buf3
)));
1766 /* router mac is only needed for type-2 routes here. */
1767 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
1768 uint8_t af_flags
= 0;
1770 if (CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1771 SET_FLAG(af_flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
);
1773 bgp_evpn_get_rmac_nexthop(vpn
, p
, &attr
, af_flags
);
1776 vni2label(vpn
->vni
, &(attr
.label
));
1778 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1779 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1782 add_l3_ecomm
= bgp_evpn_route_add_l3_ecomm_ok(
1783 vpn
, p
, (attr
.es_flags
& ATTR_ES_IS_LOCAL
) ? &attr
.esi
: NULL
);
1785 /* Set up extended community. */
1786 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1788 /* First, create (or fetch) route node within the VNI. */
1789 /* NOTE: There is no RD here. */
1790 dest
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1792 /* Create or update route entry. */
1793 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, dest
, &attr
,
1795 true /* setup_sync */, &old_is_sync
);
1797 attr_new
= pi
->attr
;
1799 /* lock ri to prevent freeing in evpn_route_select_install */
1800 bgp_path_info_lock(pi
);
1802 /* Perform route selection. Normally, the local route in the
1803 * VNI is expected to win and be the best route. However, if
1804 * there is a race condition where a host moved from local to
1805 * remote and the remote route was received in BGP just prior
1806 * to the local MACIP notification from zebra, the remote
1807 * route would win, and we should evict the defunct local route
1808 * and (re)install the remote route into zebra.
1810 evpn_route_select_install(bgp
, vpn
, dest
);
1812 * If the new local route was not selected evict it and tell zebra
1813 * to re-add the best remote dest. BGP doesn't retain non-best local
1816 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
1819 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1821 evpn_cleanup_local_non_best_route(bgp
, vpn
, dest
, pi
);
1825 /* If the local path already existed and is still the
1826 * best path we need to also check if it transitioned
1827 * from being a sync path to a non-sync path. If it
1828 * it did we need to notify zebra that the sync-path
1831 new_is_sync
= bgp_evpn_attr_is_sync(pi
->attr
);
1832 if (!new_is_sync
&& old_is_sync
)
1833 evpn_zebra_uninstall(bgp
, vpn
, p
, zero_vtep_ip
);
1836 bgp_path_info_unlock(pi
);
1838 bgp_dest_unlock_node(dest
);
1840 /* If this is a new route or some attribute has changed, export the
1841 * route to the global table. The route will be advertised to peers
1842 * from there. Note that this table is a 2-level tree (RD-level +
1843 * Prefix-level) similar to L3VPN routes.
1846 struct bgp_path_info
*global_pi
;
1848 dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1849 (const struct prefix_evpn
*)p
,
1851 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, dest
, attr_new
, 1,
1852 &global_pi
, flags
, seq
,
1853 false /* setup_sync */, NULL
/* old_is_sync */);
1855 /* Schedule for processing and unlock node. */
1856 bgp_process(bgp
, dest
, afi
, safi
);
1857 bgp_dest_unlock_node(dest
);
1860 /* Unintern temporary. */
1861 aspath_unintern(&attr
.aspath
);
1867 * Delete EVPN route entry.
1868 * The entry can be in ESI/VNI table or the global table.
1870 void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1871 struct bgp_dest
*dest
,
1872 struct bgp_path_info
**pi
)
1874 struct bgp_path_info
*tmp_pi
;
1878 /* Now, find matching route. */
1879 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1880 tmp_pi
= tmp_pi
->next
)
1881 if (tmp_pi
->peer
== bgp
->peer_self
1882 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1883 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1888 /* Mark route for delete. */
1890 bgp_path_info_delete(dest
, tmp_pi
);
1893 /* Delete EVPN type5 route */
1894 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1896 afi_t afi
= AFI_L2VPN
;
1897 safi_t safi
= SAFI_EVPN
;
1898 struct bgp_dest
*dest
= NULL
;
1899 struct bgp_path_info
*pi
= NULL
;
1900 struct bgp
*bgp_evpn
= NULL
; /* evpn bgp instance */
1902 bgp_evpn
= bgp_get_evpn();
1906 /* locate the global route entry for this type-5 prefix */
1907 dest
= bgp_global_evpn_node_lookup(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1908 (const struct prefix_evpn
*)evp
, &bgp_vrf
->vrf_prd
);
1912 delete_evpn_route_entry(bgp_evpn
, afi
, safi
, dest
, &pi
);
1914 bgp_process(bgp_evpn
, dest
, afi
, safi
);
1915 bgp_dest_unlock_node(dest
);
1920 * Delete EVPN route (of type based on prefix) for specified VNI and
1921 * schedule for processing.
1923 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1924 struct prefix_evpn
*p
)
1926 struct bgp_dest
*dest
, *global_dest
;
1927 struct bgp_path_info
*pi
;
1928 afi_t afi
= AFI_L2VPN
;
1929 safi_t safi
= SAFI_EVPN
;
1931 /* First, locate the route node within the VNI. If it doesn't exist,
1933 * is nothing further to do.
1935 /* NOTE: There is no RD here. */
1936 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1940 /* Next, locate route node in the global EVPN routing table. Note that
1941 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1944 global_dest
= bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1945 (const struct prefix_evpn
*)p
, &vpn
->prd
);
1947 /* Delete route entry in the global EVPN table. */
1948 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
1950 /* Schedule for processing - withdraws to peers happen from
1954 bgp_process(bgp
, global_dest
, afi
, safi
);
1955 bgp_dest_unlock_node(global_dest
);
1958 /* Delete route entry in the VNI route table. This can just be removed.
1960 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
1962 bgp_path_info_reap(dest
, pi
);
1963 evpn_route_select_install(bgp
, vpn
, dest
);
1965 bgp_dest_unlock_node(dest
);
1970 void bgp_evpn_update_type2_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1971 struct bgp_dest
*dest
,
1972 struct bgp_path_info
*local_pi
,
1975 afi_t afi
= AFI_L2VPN
;
1976 safi_t safi
= SAFI_EVPN
;
1977 struct bgp_path_info
*pi
;
1979 struct attr
*attr_new
;
1981 int add_l3_ecomm
= 0;
1982 struct bgp_dest
*global_dest
;
1983 struct bgp_path_info
*global_pi
;
1984 struct prefix_evpn
*evp
=
1985 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1987 bool old_is_sync
= false;
1989 if (CHECK_FLAG(local_pi
->flags
, BGP_PATH_REMOVED
))
1993 * Build attribute per local route as the MAC mobility and
1994 * some other values could differ for different routes. The
1995 * attributes will be shared in the hash table.
1997 bgp_attr_default_set(&attr
, bgp
, BGP_ORIGIN_IGP
);
1998 attr
.nexthop
= vpn
->originator_ip
;
1999 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
2000 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
2001 attr
.sticky
= (local_pi
->attr
->sticky
) ? 1 : 0;
2002 attr
.router_flag
= (local_pi
->attr
->router_flag
) ? 1 : 0;
2003 attr
.es_flags
= local_pi
->attr
->es_flags
;
2004 if (local_pi
->attr
->default_gw
) {
2005 attr
.default_gw
= 1;
2006 if (is_evpn_prefix_ipaddr_v6(evp
))
2007 attr
.router_flag
= 1;
2009 memcpy(&attr
.esi
, &local_pi
->attr
->esi
, sizeof(esi_t
));
2010 bgp_evpn_get_rmac_nexthop(vpn
, evp
, &attr
,
2011 local_pi
->extra
->af_flags
);
2012 vni2label(vpn
->vni
, &(attr
.label
));
2013 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2014 * using L3 VNI for type-2 routes also.
2016 add_l3_ecomm
= bgp_evpn_route_add_l3_ecomm_ok(
2018 (attr
.es_flags
& ATTR_ES_IS_LOCAL
) ? &attr
.esi
: NULL
);
2020 /* Set up extended community. */
2021 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2022 seq
= mac_mobility_seqnum(local_pi
->attr
);
2024 if (bgp_debug_zebra(NULL
)) {
2025 char buf3
[ESI_STR_LEN
];
2028 "VRF %s vni %u evp %pFX RMAC %pEA nexthop %pI4 esi %s esf 0x%x from %s",
2029 vpn
->bgp_vrf
? vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
)
2031 vpn
->vni
, evp
, &attr
.rmac
, &attr
.mp_nexthop_global_in
,
2032 esi_to_str(&attr
.esi
, buf3
, sizeof(buf3
)),
2033 attr
.es_flags
, caller
);
2036 /* Update the route entry. */
2037 route_change
= update_evpn_route_entry(
2038 bgp
, vpn
, afi
, safi
, dest
, &attr
, 0, &pi
, 0, seq
,
2039 true /* setup_sync */, &old_is_sync
);
2042 attr_new
= pi
->attr
;
2043 /* lock ri to prevent freeing in evpn_route_select_install */
2044 bgp_path_info_lock(pi
);
2046 /* Perform route selection. Normally, the local route in the
2047 * VNI is expected to win and be the best route. However,
2048 * under peculiar situations (e.g., tunnel (next hop) IP change
2049 * that causes best selection to be based on next hop), a
2050 * remote route could win. If the local route is the best,
2051 * ensure it is updated in the global EVPN route table and
2052 * advertised to peers; otherwise, ensure it is evicted and
2053 * (re)install the remote route into zebra.
2055 evpn_route_select_install(bgp
, vpn
, dest
);
2057 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2060 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
2062 evpn_cleanup_local_non_best_route(bgp
, vpn
, dest
, pi
);
2066 /* If the local path already existed and is still the
2067 * best path we need to also check if it transitioned
2068 * from being a sync path to a non-sync path. If it
2069 * it did we need to notify zebra that the sync-path
2072 new_is_sync
= bgp_evpn_attr_is_sync(pi
->attr
);
2073 if (!new_is_sync
&& old_is_sync
)
2074 evpn_zebra_uninstall(bgp
, vpn
,
2081 bgp_path_info_unlock(pi
);
2084 /* Update route in global routing table. */
2085 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
,
2086 safi
, evp
, &vpn
->prd
);
2087 assert(global_dest
);
2088 update_evpn_route_entry(
2089 bgp
, vpn
, afi
, safi
, global_dest
, attr_new
, 0,
2090 &global_pi
, 0, mac_mobility_seqnum(attr_new
),
2091 false /* setup_sync */, NULL
/* old_is_sync */);
2093 /* Schedule for processing and unlock node. */
2094 bgp_process(bgp
, global_dest
, afi
, safi
);
2095 bgp_dest_unlock_node(global_dest
);
2098 /* Unintern temporary. */
2099 aspath_unintern(&attr
.aspath
);
2103 * Update all type-2 (MACIP) local routes for this VNI - these should also
2104 * be scheduled for advertise to peers.
2106 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2108 struct bgp_dest
*dest
;
2109 struct bgp_path_info
*tmp_pi
;
2111 /* Walk this VNI's route table and update local type-2 routes. For any
2112 * routes updated, update corresponding entry in the global table too.
2114 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2115 dest
= bgp_route_next(dest
)) {
2116 const struct prefix_evpn
*evp
=
2117 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
2119 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2122 /* Identify local route. */
2123 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
2124 tmp_pi
= tmp_pi
->next
) {
2125 if (tmp_pi
->peer
== bgp
->peer_self
2126 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2127 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2134 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, tmp_pi
,
2142 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2143 * global routing table. These are also scheduled for withdraw from peers.
2145 static void delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2149 struct bgp_dest
*rddest
, *dest
;
2150 struct bgp_table
*table
;
2151 struct bgp_path_info
*pi
;
2156 rddest
= bgp_node_lookup(bgp
->rib
[afi
][safi
],
2157 (struct prefix
*)&vpn
->prd
);
2159 table
= bgp_dest_get_bgp_table_info(rddest
);
2160 for (dest
= bgp_table_top(table
); dest
;
2161 dest
= bgp_route_next(dest
)) {
2162 const struct prefix_evpn
*evp
=
2163 (const struct prefix_evpn
*)bgp_dest_get_prefix(
2166 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2169 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2171 bgp_process(bgp
, dest
, afi
, safi
);
2174 /* Unlock RD node. */
2175 bgp_dest_unlock_node(rddest
);
2180 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2181 * table as well as the per-VNI route table.
2183 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2187 struct bgp_dest
*dest
;
2188 struct bgp_path_info
*pi
;
2193 /* First, walk the global route table for this VNI's type-2 local
2195 * EVPN routes are a 2-level table, first get the RD table.
2197 delete_global_type2_routes(bgp
, vpn
);
2199 /* Next, walk this VNI's route table and delete local type-2 routes. */
2200 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2201 dest
= bgp_route_next(dest
)) {
2202 const struct prefix_evpn
*evp
=
2203 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
2205 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2208 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2210 /* Route entry in local table gets deleted immediately. */
2212 bgp_path_info_reap(dest
, pi
);
2219 * Delete all routes in the per-VNI route table.
2221 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2223 struct bgp_dest
*dest
;
2224 struct bgp_path_info
*pi
, *nextpi
;
2226 /* Walk this VNI's route table and delete all routes. */
2227 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2228 dest
= bgp_route_next(dest
)) {
2229 for (pi
= bgp_dest_get_bgp_path_info(dest
);
2230 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2231 bgp_evpn_remote_ip_hash_del(vpn
, pi
);
2232 bgp_path_info_delete(dest
, pi
);
2233 bgp_path_info_reap(dest
, pi
);
2240 /* BUM traffic flood mode per-l2-vni */
2241 static int bgp_evpn_vni_flood_mode_get(struct bgp
*bgp
,
2242 struct bgpevpn
*vpn
)
2244 /* if flooding has been globally disabled per-vni mode is
2247 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
2248 return VXLAN_FLOOD_DISABLED
;
2250 /* if mcast group ip has been specified we use a PIM-SM MDT */
2251 if (vpn
->mcast_grp
.s_addr
!= INADDR_ANY
)
2252 return VXLAN_FLOOD_PIM_SM
;
2254 /* default is ingress replication */
2255 return VXLAN_FLOOD_HEAD_END_REPL
;
2259 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2260 * export RT getting modified or change to tunnel IP. Note that these
2261 * situations need the route in the per-VNI table as well as the global
2262 * table to be updated (as attributes change).
2264 int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2267 struct prefix_evpn p
;
2269 update_type1_routes_for_evi(bgp
, vpn
);
2271 /* Update and advertise the type-3 route (only one) followed by the
2272 * locally learnt type-2 routes (MACIP) - for this VNI.
2274 * RT-3 only if doing head-end replication
2276 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
2277 == VXLAN_FLOOD_HEAD_END_REPL
) {
2278 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2279 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
);
2284 return update_all_type2_routes(bgp
, vpn
);
2288 * Delete (and withdraw) local routes for specified VNI from the global
2289 * table and per-VNI table. After this, remove all other routes from
2290 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2291 * (advertise-all-vni) being disabled.
2293 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2296 struct prefix_evpn p
;
2298 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2299 * followed by type-3 routes (only one) - for this VNI.
2301 ret
= delete_all_type2_routes(bgp
, vpn
);
2305 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2306 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2310 /* Delete all routes from the per-VNI table. */
2311 return delete_all_vni_routes(bgp
, vpn
);
2315 * There is a flood mcast IP address change. Update the mcast-grp and
2316 * remove the type-3 route if any. A new type-3 route will be generated
2317 * post tunnel_ip update if the new flood mode is head-end-replication.
2319 static int bgp_evpn_mcast_grp_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2320 struct in_addr mcast_grp
)
2322 struct prefix_evpn p
;
2324 vpn
->mcast_grp
= mcast_grp
;
2326 if (is_vni_live(vpn
)) {
2327 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2328 delete_evpn_route(bgp
, vpn
, &p
);
2335 * There is a tunnel endpoint IP address change for this VNI, delete
2336 * prior type-3 route (if needed) and update.
2337 * Note: Route re-advertisement happens elsewhere after other processing
2340 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2341 struct in_addr originator_ip
)
2343 struct prefix_evpn p
;
2345 /* If VNI is not live, we only need to update the originator ip */
2346 if (!is_vni_live(vpn
)) {
2347 vpn
->originator_ip
= originator_ip
;
2351 /* Update the tunnel-ip hash */
2352 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2353 bgp_tip_add(bgp
, &originator_ip
);
2355 /* filter routes as martian nexthop db has changed */
2356 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2358 /* Need to withdraw type-3 route as the originator IP is part
2361 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2362 delete_evpn_route(bgp
, vpn
, &p
);
2364 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2365 vpn
->originator_ip
= originator_ip
;
2369 static struct bgp_path_info
*
2370 bgp_create_evpn_bgp_path_info(struct bgp_path_info
*parent_pi
,
2371 struct bgp_dest
*dest
, struct attr
*attr
)
2373 struct attr
*attr_new
;
2374 struct bgp_path_info
*pi
;
2376 /* Add (or update) attribute to hash. */
2377 attr_new
= bgp_attr_intern(attr
);
2379 /* Create new route with its attribute. */
2380 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0, parent_pi
->peer
,
2382 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2383 bgp_path_info_extra_get(pi
);
2384 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2385 bgp_dest_lock_node((struct bgp_dest
*)parent_pi
->net
);
2386 if (parent_pi
->extra
) {
2387 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2388 sizeof(pi
->extra
->label
));
2389 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2390 pi
->extra
->igpmetric
= parent_pi
->extra
->igpmetric
;
2392 bgp_path_info_add(dest
, pi
);
2398 * Install route entry into the VRF routing table and invoke route selection.
2400 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2401 const struct prefix_evpn
*evp
,
2402 struct bgp_path_info
*parent_pi
)
2404 struct bgp_dest
*dest
;
2405 struct bgp_path_info
*pi
;
2407 struct attr
*attr_new
;
2410 struct prefix
*pp
= &p
;
2413 bool new_pi
= false;
2414 bool use_l3nhg
= false;
2415 bool is_l3nhg_active
= false;
2416 char buf1
[INET6_ADDRSTRLEN
];
2418 memset(pp
, 0, sizeof(struct prefix
));
2419 ip_prefix_from_evpn_prefix(evp
, pp
);
2421 if (bgp_debug_zebra(NULL
))
2423 "vrf %s: import evpn prefix %pFX parent %p flags 0x%x",
2424 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
, parent_pi
,
2427 /* Create (or fetch) route within the VRF. */
2428 /* NOTE: There is no RD here. */
2429 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2431 safi
= SAFI_UNICAST
;
2432 dest
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2433 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2435 safi
= SAFI_UNICAST
;
2436 dest
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2440 /* EVPN routes currently only support a IPv4 next hop which corresponds
2441 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2442 * or prefix route, we have to convert the next hop to an IPv4-mapped
2443 * address for the rest of the code to flow through. In the case of IPv4,
2444 * make sure to set the flag for next hop attribute.
2446 attr
= *parent_pi
->attr
;
2447 if (attr
.evpn_overlay
.type
!= OVERLAY_INDEX_GATEWAY_IP
) {
2449 evpn_convert_nexthop_to_ipv6(&attr
);
2451 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2455 * If gateway IP overlay index is specified in the NLRI of
2456 * EVPN RT-5, this gateway IP should be used as the nexthop
2457 * for the prefix in the VRF
2459 if (bgp_debug_zebra(NULL
)) {
2461 "Install gateway IP %s as nexthop for prefix %pFX in vrf %s",
2462 inet_ntop(pp
->family
, &attr
.evpn_overlay
.gw_ip
,
2463 buf1
, sizeof(buf1
)), pp
,
2464 vrf_id_to_name(bgp_vrf
->vrf_id
));
2467 if (afi
== AFI_IP6
) {
2468 memcpy(&attr
.mp_nexthop_global
,
2469 &attr
.evpn_overlay
.gw_ip
.ipaddr_v6
,
2470 sizeof(struct in6_addr
));
2471 attr
.mp_nexthop_len
= IPV6_MAX_BYTELEN
;
2473 attr
.nexthop
= attr
.evpn_overlay
.gw_ip
.ipaddr_v4
;
2474 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2478 bgp_evpn_es_vrf_use_nhg(bgp_vrf
, &parent_pi
->attr
->esi
, &use_l3nhg
,
2479 &is_l3nhg_active
, NULL
);
2481 attr
.es_flags
|= ATTR_ES_L3_NHG_USE
;
2482 if (is_l3nhg_active
)
2483 attr
.es_flags
|= ATTR_ES_L3_NHG_ACTIVE
;
2485 /* Check if route entry is already present. */
2486 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2488 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2492 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, dest
, &attr
);
2495 if (attrhash_cmp(pi
->attr
, &attr
)
2496 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2497 bgp_dest_unlock_node(dest
);
2500 /* The attribute has changed. */
2501 /* Add (or update) attribute to hash. */
2502 attr_new
= bgp_attr_intern(&attr
);
2504 /* Restore route, if needed. */
2505 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2506 bgp_path_info_restore(dest
, pi
);
2508 /* Mark if nexthop has changed. */
2510 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2512 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2513 &attr_new
->mp_nexthop_global
)))
2514 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2516 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ATTR_CHANGED
);
2517 /* Unintern existing, set to new. */
2518 bgp_attr_unintern(&pi
->attr
);
2519 pi
->attr
= attr_new
;
2520 pi
->uptime
= bgp_clock();
2523 /* Gateway IP nexthop should be resolved */
2524 if (attr
.evpn_overlay
.type
== OVERLAY_INDEX_GATEWAY_IP
) {
2525 if (bgp_find_or_add_nexthop(bgp_vrf
, bgp_vrf
, afi
, safi
, pi
,
2527 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_VALID
);
2529 if (BGP_DEBUG(nht
, NHT
)) {
2530 inet_ntop(pp
->family
,
2531 &attr
.evpn_overlay
.gw_ip
,
2532 buf1
, sizeof(buf1
));
2533 zlog_debug("%s: gateway IP NH unresolved",
2536 bgp_path_info_unset_flag(dest
, pi
, BGP_PATH_VALID
);
2540 /* as it is an importation, change nexthop */
2541 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ANNC_NH_SELF
);
2544 /* Link path to evpn nexthop */
2545 bgp_evpn_path_nh_add(bgp_vrf
, pi
);
2547 bgp_aggregate_increment(bgp_vrf
, bgp_dest_get_prefix(dest
), pi
, afi
,
2550 /* Perform route selection and update zebra, if required. */
2551 bgp_process(bgp_vrf
, dest
, afi
, safi
);
2553 /* Process for route leaking. */
2554 vpn_leak_from_vrf_update(bgp_get_default(), bgp_vrf
, pi
);
2556 bgp_dest_unlock_node(dest
);
2558 if (bgp_debug_zebra(NULL
))
2559 zlog_debug("... %s pi dest %p (l %d) pi %p (l %d, f 0x%x)",
2560 new_pi
? "new" : "update", dest
,
2561 bgp_dest_get_lock_count(dest
), pi
, pi
->lock
,
2568 * Install route entry into the VNI routing table and invoke route selection.
2570 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2571 const struct prefix_evpn
*p
,
2572 struct bgp_path_info
*parent_pi
)
2574 struct bgp_dest
*dest
;
2575 struct bgp_path_info
*pi
;
2576 struct bgp_path_info
*local_pi
;
2577 struct attr
*attr_new
;
2579 struct prefix_evpn ad_evp
;
2580 bool old_local_es
= false;
2583 /* EAD prefix in the global table doesn't include the VTEP-IP so
2584 * we need to create a different copy for the VNI
2586 if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
2587 p
= evpn_type1_prefix_vni_copy(&ad_evp
, p
,
2588 parent_pi
->attr
->nexthop
);
2590 /* Create (or fetch) route within the VNI. */
2591 /* NOTE: There is no RD here. */
2592 dest
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2594 /* Check if route entry is already present. */
2595 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2597 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2601 /* Create an info */
2602 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, dest
,
2604 new_local_es
= bgp_evpn_attr_is_local_es(pi
->attr
);
2606 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2607 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2608 bgp_dest_unlock_node(dest
);
2611 /* The attribute has changed. */
2612 /* Add (or update) attribute to hash. */
2613 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2615 /* Restore route, if needed. */
2616 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2617 bgp_path_info_restore(dest
, pi
);
2619 /* Mark if nexthop has changed. */
2620 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2621 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2623 old_local_es
= bgp_evpn_attr_is_local_es(pi
->attr
);
2624 new_local_es
= bgp_evpn_attr_is_local_es(attr_new
);
2625 /* If ESI is different or if its type has changed we
2626 * need to reinstall the path in zebra
2628 if ((old_local_es
!= new_local_es
)
2629 || memcmp(&pi
->attr
->esi
, &attr_new
->esi
,
2630 sizeof(attr_new
->esi
))) {
2632 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
))
2633 zlog_debug("VNI %d path %pFX chg to %s es",
2634 vpn
->vni
, &pi
->net
->p
,
2635 new_local_es
? "local"
2637 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ATTR_CHANGED
);
2640 /* Unintern existing, set to new. */
2641 bgp_attr_unintern(&pi
->attr
);
2642 pi
->attr
= attr_new
;
2643 pi
->uptime
= bgp_clock();
2646 /* Add this route to remote IP hashtable */
2647 bgp_evpn_remote_ip_hash_add(vpn
, pi
);
2649 /* Perform route selection and update zebra, if required. */
2650 ret
= evpn_route_select_install(bgp
, vpn
, dest
);
2652 /* if the best path is a local path with a non-zero ES
2653 * sync info against the local path may need to be updated
2654 * when a remote path is added/updated (including changes
2655 * from sync-path to remote-path)
2657 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
2658 if (local_pi
&& (old_local_es
|| new_local_es
))
2659 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, local_pi
,
2661 bgp_dest_unlock_node(dest
);
2667 * Uninstall route entry from the VRF routing table and send message
2668 * to zebra, if appropriate.
2670 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2671 const struct prefix_evpn
*evp
,
2672 struct bgp_path_info
*parent_pi
)
2674 struct bgp_dest
*dest
;
2675 struct bgp_path_info
*pi
;
2678 struct prefix
*pp
= &p
;
2682 memset(pp
, 0, sizeof(struct prefix
));
2683 ip_prefix_from_evpn_prefix(evp
, pp
);
2685 if (bgp_debug_zebra(NULL
))
2687 "vrf %s: unimport evpn prefix %pFX parent %p flags 0x%x",
2688 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
, parent_pi
,
2691 /* Locate route within the VRF. */
2692 /* NOTE: There is no RD here. */
2693 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2695 safi
= SAFI_UNICAST
;
2696 dest
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2699 safi
= SAFI_UNICAST
;
2700 dest
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2706 /* Find matching route entry. */
2707 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2709 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2713 bgp_dest_unlock_node(dest
);
2717 if (bgp_debug_zebra(NULL
))
2718 zlog_debug("... delete dest %p (l %d) pi %p (l %d, f 0x%x)",
2719 dest
, bgp_dest_get_lock_count(dest
), pi
, pi
->lock
,
2722 /* Process for route leaking. */
2723 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp_vrf
, pi
);
2725 bgp_aggregate_decrement(bgp_vrf
, bgp_dest_get_prefix(dest
), pi
, afi
,
2728 /* Mark entry for deletion */
2729 bgp_path_info_delete(dest
, pi
);
2731 /* Unlink path to evpn nexthop */
2732 bgp_evpn_path_nh_del(bgp_vrf
, pi
);
2734 /* Perform route selection and update zebra, if required. */
2735 bgp_process(bgp_vrf
, dest
, afi
, safi
);
2737 /* Unlock route node. */
2738 bgp_dest_unlock_node(dest
);
2744 * Uninstall route entry from the VNI routing table and send message
2745 * to zebra, if appropriate.
2747 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2748 const struct prefix_evpn
*p
,
2749 struct bgp_path_info
*parent_pi
)
2751 struct bgp_dest
*dest
;
2752 struct bgp_path_info
*pi
;
2753 struct bgp_path_info
*local_pi
;
2755 struct prefix_evpn ad_evp
;
2757 /* EAD prefix in the global table doesn't include the VTEP-IP so
2758 * we need to create a different copy for the VNI
2760 if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
2761 p
= evpn_type1_prefix_vni_copy(&ad_evp
, p
,
2762 parent_pi
->attr
->nexthop
);
2764 /* Locate route within the VNI. */
2765 /* NOTE: There is no RD here. */
2766 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2770 /* Find matching route entry. */
2771 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2773 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2777 bgp_dest_unlock_node(dest
);
2781 bgp_evpn_remote_ip_hash_del(vpn
, pi
);
2783 /* Mark entry for deletion */
2784 bgp_path_info_delete(dest
, pi
);
2786 /* Perform route selection and update zebra, if required. */
2787 ret
= evpn_route_select_install(bgp
, vpn
, dest
);
2789 /* if the best path is a local path with a non-zero ES
2790 * sync info against the local path may need to be updated
2791 * when a remote path is deleted
2793 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
2794 if (local_pi
&& bgp_evpn_attr_is_local_es(local_pi
->attr
))
2795 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, local_pi
,
2798 /* Unlock route node. */
2799 bgp_dest_unlock_node(dest
);
2805 * Given a route entry and a VRF, see if this route entry should be
2806 * imported into the VRF i.e., RTs match.
2808 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2809 struct bgp_path_info
*pi
)
2811 struct attr
*attr
= pi
->attr
;
2812 struct ecommunity
*ecom
;
2816 /* Route should have valid RT to be even considered. */
2817 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2820 ecom
= bgp_attr_get_ecommunity(attr
);
2821 if (!ecom
|| !ecom
->size
)
2824 /* For each extended community RT, see if it matches this VNI. If any RT
2825 * matches, we're done.
2827 for (i
= 0; i
< ecom
->size
; i
++) {
2829 uint8_t type
, sub_type
;
2830 struct ecommunity_val
*eval
;
2831 struct ecommunity_val eval_tmp
;
2832 struct vrf_irt_node
*irt
;
2834 /* Only deal with RTs */
2835 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
2836 eval
= (struct ecommunity_val
*)(ecom
->val
2837 + (i
* ecom
->unit_size
));
2840 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2843 /* See if this RT matches specified VNIs import RTs */
2844 irt
= lookup_vrf_import_rt(eval
);
2846 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2849 /* Also check for non-exact match. In this, we mask out the AS
2851 * only check on the local-admin sub-field. This is to
2853 * VNI as the RT for EBGP peering too.
2856 if (type
== ECOMMUNITY_ENCODE_AS
2857 || type
== ECOMMUNITY_ENCODE_AS4
2858 || type
== ECOMMUNITY_ENCODE_IP
) {
2859 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
2860 mask_ecom_global_admin(&eval_tmp
, eval
);
2861 irt
= lookup_vrf_import_rt(&eval_tmp
);
2864 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2872 * Given a route entry and a VNI, see if this route entry should be
2873 * imported into the VNI i.e., RTs match.
2875 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2876 struct bgp_path_info
*pi
)
2878 struct attr
*attr
= pi
->attr
;
2879 struct ecommunity
*ecom
;
2883 /* Route should have valid RT to be even considered. */
2884 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2887 ecom
= bgp_attr_get_ecommunity(attr
);
2888 if (!ecom
|| !ecom
->size
)
2891 /* For each extended community RT, see if it matches this VNI. If any RT
2892 * matches, we're done.
2894 for (i
= 0; i
< ecom
->size
; i
++) {
2896 uint8_t type
, sub_type
;
2897 struct ecommunity_val
*eval
;
2898 struct ecommunity_val eval_tmp
;
2899 struct irt_node
*irt
;
2901 /* Only deal with RTs */
2902 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
2903 eval
= (struct ecommunity_val
*)(ecom
->val
2904 + (i
* ecom
->unit_size
));
2907 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2910 /* See if this RT matches specified VNIs import RTs */
2911 irt
= lookup_import_rt(bgp
, eval
);
2913 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2916 /* Also check for non-exact match. In this, we mask out the AS
2918 * only check on the local-admin sub-field. This is to
2920 * VNI as the RT for EBGP peering too.
2923 if (type
== ECOMMUNITY_ENCODE_AS
2924 || type
== ECOMMUNITY_ENCODE_AS4
2925 || type
== ECOMMUNITY_ENCODE_IP
) {
2926 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
2927 mask_ecom_global_admin(&eval_tmp
, eval
);
2928 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2931 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2938 /* This API will scan evpn routes for checking attribute's rmac
2939 * macthes with bgp instance router mac. It avoid installing
2940 * route into bgp vrf table and remote rmac in bridge table.
2942 static int bgp_evpn_route_rmac_self_check(struct bgp
*bgp_vrf
,
2943 const struct prefix_evpn
*evp
,
2944 struct bgp_path_info
*pi
)
2946 /* evpn route could have learnt prior to L3vni has come up,
2947 * perform rmac check before installing route and
2948 * remote router mac.
2949 * The route will be removed from global bgp table once
2950 * SVI comes up with MAC and stored in hash, triggers
2951 * bgp_mac_rescan_all_evpn_tables.
2953 if (memcmp(&bgp_vrf
->rmac
, &pi
->attr
->rmac
, ETH_ALEN
) == 0) {
2954 if (bgp_debug_update(pi
->peer
, NULL
, NULL
, 1)) {
2955 char attr_str
[BUFSIZ
] = {0};
2957 bgp_dump_attr(pi
->attr
, attr_str
, sizeof(attr_str
));
2960 "%s: bgp %u prefix %pFX with attr %s - DENIED due to self mac",
2961 __func__
, bgp_vrf
->vrf_id
, evp
, attr_str
);
2970 /* don't import hosts that are locally attached */
2972 bgp_evpn_skip_vrf_import_of_local_es(struct bgp
*bgp_vrf
,
2973 const struct prefix_evpn
*evp
,
2974 struct bgp_path_info
*pi
, int install
)
2978 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
2979 esi
= bgp_evpn_attr_get_esi(pi
->attr
);
2981 /* Don't import routes that point to a local destination */
2982 if (bgp_evpn_attr_is_local_es(pi
->attr
)) {
2983 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
2984 char esi_buf
[ESI_STR_LEN
];
2987 "vrf %s of evpn prefix %pFX skipped, local es %s",
2988 install
? "import" : "unimport", evp
,
2989 esi_to_str(esi
, esi_buf
,
2999 * Install or uninstall a mac-ip route in the provided vrf if
3000 * there is a rt match
3002 int bgp_evpn_route_entry_install_if_vrf_match(struct bgp
*bgp_vrf
,
3003 struct bgp_path_info
*pi
,
3007 const struct prefix_evpn
*evp
=
3008 (const struct prefix_evpn
*)bgp_dest_get_prefix(pi
->net
);
3010 /* Consider "valid" remote routes applicable for
3013 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3014 && pi
->type
== ZEBRA_ROUTE_BGP
3015 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3018 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
3019 if (bgp_evpn_route_rmac_self_check(bgp_vrf
, evp
, pi
))
3022 /* don't import hosts that are locally attached */
3023 if (install
&& bgp_evpn_skip_vrf_import_of_local_es(
3024 bgp_vrf
, evp
, pi
, install
))
3028 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3030 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3034 flog_err(EC_BGP_EVPN_FAIL
,
3035 "Failed to %s EVPN %pFX route in VRF %s",
3036 install
? "install" : "uninstall", evp
,
3037 vrf_id_to_name(bgp_vrf
->vrf_id
));
3044 * Install or uninstall mac-ip routes are appropriate for this
3047 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
3051 struct bgp_dest
*rd_dest
, *dest
;
3052 struct bgp_table
*table
;
3053 struct bgp_path_info
*pi
;
3055 struct bgp
*bgp_evpn
= NULL
;
3059 bgp_evpn
= bgp_get_evpn();
3063 /* Walk entire global routing table and evaluate routes which could be
3064 * imported into this VRF. Note that we need to loop through all global
3065 * routes to determine which route matches the import rt on vrf
3067 for (rd_dest
= bgp_table_top(bgp_evpn
->rib
[afi
][safi
]); rd_dest
;
3068 rd_dest
= bgp_route_next(rd_dest
)) {
3069 table
= bgp_dest_get_bgp_table_info(rd_dest
);
3073 for (dest
= bgp_table_top(table
); dest
;
3074 dest
= bgp_route_next(dest
)) {
3075 const struct prefix_evpn
*evp
=
3076 (const struct prefix_evpn
*)bgp_dest_get_prefix(
3079 /* if not mac-ip route skip this route */
3080 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3081 || evp
->prefix
.route_type
3082 == BGP_EVPN_IP_PREFIX_ROUTE
))
3085 /* if not a mac+ip route skip this route */
3086 if (!(is_evpn_prefix_ipaddr_v4(evp
)
3087 || is_evpn_prefix_ipaddr_v6(evp
)))
3090 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
3092 ret
= bgp_evpn_route_entry_install_if_vrf_match(
3093 bgp_vrf
, pi
, install
);
3104 * Install or uninstall routes of specified type that are appropriate for this
3107 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
3108 struct bgpevpn
*vpn
,
3109 bgp_evpn_route_type rtype
,
3114 struct bgp_dest
*rd_dest
, *dest
;
3115 struct bgp_table
*table
;
3116 struct bgp_path_info
*pi
;
3122 /* Walk entire global routing table and evaluate routes which could be
3123 * imported into this VPN. Note that we cannot just look at the routes
3125 * the VNI's RD - remote routes applicable for this VNI could have any
3128 /* EVPN routes are a 2-level table. */
3129 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
3130 rd_dest
= bgp_route_next(rd_dest
)) {
3131 table
= bgp_dest_get_bgp_table_info(rd_dest
);
3135 for (dest
= bgp_table_top(table
); dest
;
3136 dest
= bgp_route_next(dest
)) {
3137 const struct prefix_evpn
*evp
=
3138 (const struct prefix_evpn
*)bgp_dest_get_prefix(
3141 if (evp
->prefix
.route_type
!= rtype
)
3144 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
3146 /* Consider "valid" remote routes applicable for
3148 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3149 && pi
->type
== ZEBRA_ROUTE_BGP
3150 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3153 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3155 ret
= install_evpn_route_entry(
3158 ret
= uninstall_evpn_route_entry(
3164 "%u: Failed to %s EVPN %s route in VNI %u",
3168 rtype
== BGP_EVPN_MAC_IP_ROUTE
3173 bgp_dest_unlock_node(rd_dest
);
3174 bgp_dest_unlock_node(dest
);
3185 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3186 * is invoked upon l3vni-add or l3vni import rt change
3188 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3190 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3195 * Install any existing remote routes applicable for this VNI into its
3196 * routing table. This is invoked when a VNI becomes "live" or its Import
3199 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3203 /* Install type-3 routes followed by type-2 routes - the ones applicable
3206 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3211 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_AD_ROUTE
,
3216 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3220 /* uninstall routes from l3vni vrf. */
3221 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3223 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3228 * Uninstall any existing remote routes for this VNI. One scenario in which
3229 * this is invoked is upon an import RT change.
3231 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3235 /* Uninstall type-2 routes followed by type-3 routes - the ones
3239 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3244 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_AD_ROUTE
,
3250 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3255 * Install or uninstall route in matching VRFs (list).
3257 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3258 safi_t safi
, struct prefix_evpn
*evp
,
3259 struct bgp_path_info
*pi
,
3260 struct list
*vrfs
, int install
)
3262 struct bgp
*bgp_vrf
;
3263 struct listnode
*node
, *nnode
;
3265 /* Only type-2/type-5 routes go into a VRF */
3266 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3267 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3270 /* if it is type-2 route and not a mac+ip route skip this route */
3271 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3272 && !(is_evpn_prefix_ipaddr_v4(evp
)
3273 || is_evpn_prefix_ipaddr_v6(evp
)))
3276 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3279 /* don't import hosts that are locally attached */
3280 if (install
&& bgp_evpn_skip_vrf_import_of_local_es(
3281 bgp_vrf
, evp
, pi
, install
))
3285 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3287 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3291 flog_err(EC_BGP_EVPN_FAIL
,
3292 "%u: Failed to %s prefix %pFX in VRF %s",
3294 install
? "install" : "uninstall", evp
,
3295 vrf_id_to_name(bgp_vrf
->vrf_id
));
3304 * Install or uninstall route in matching VNIs (list).
3306 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3307 safi_t safi
, struct prefix_evpn
*evp
,
3308 struct bgp_path_info
*pi
,
3309 struct list
*vnis
, int install
)
3311 struct bgpevpn
*vpn
;
3312 struct listnode
*node
, *nnode
;
3314 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3317 if (!is_vni_live(vpn
))
3321 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3323 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3326 flog_err(EC_BGP_EVPN_FAIL
,
3327 "%u: Failed to %s EVPN %s route in VNI %u",
3328 bgp
->vrf_id
, install
? "install" : "uninstall",
3329 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3341 * Install or uninstall route for appropriate VNIs/ESIs.
3343 static int bgp_evpn_install_uninstall_table(struct bgp
*bgp
, afi_t afi
,
3344 safi_t safi
, const struct prefix
*p
,
3345 struct bgp_path_info
*pi
,
3346 int import
, bool in_vni_rt
,
3349 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3350 struct attr
*attr
= pi
->attr
;
3351 struct ecommunity
*ecom
;
3353 struct prefix_evpn ad_evp
;
3357 /* Only type-1, type-2, type-3, type-4 and type-5
3358 * are supported currently
3360 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3361 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3362 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3363 || evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
3364 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3367 /* If we don't have Route Target, nothing much to do. */
3368 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3371 /* EAD prefix in the global table doesn't include the VTEP-IP so
3372 * we need to create a different copy for the VNI
3374 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
3375 evp
= evpn_type1_prefix_vni_copy(&ad_evp
, evp
, attr
->nexthop
);
3377 ecom
= bgp_attr_get_ecommunity(attr
);
3378 if (!ecom
|| !ecom
->size
)
3381 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3382 * attached to the route */
3383 for (i
= 0; i
< ecom
->size
; i
++) {
3385 uint8_t type
, sub_type
;
3386 struct ecommunity_val
*eval
;
3387 struct ecommunity_val eval_tmp
;
3388 struct irt_node
*irt
; /* import rt for l2vni */
3389 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3390 struct bgp_evpn_es
*es
;
3392 /* Only deal with RTs */
3393 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
3394 eval
= (struct ecommunity_val
*)(ecom
->val
3395 + (i
* ecom
->unit_size
));
3398 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3401 /* non-local MAC-IP routes in the global route table are linked
3402 * to the destination ES
3404 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3405 bgp_evpn_path_es_link(pi
, 0,
3406 bgp_evpn_attr_get_esi(pi
->attr
));
3409 * macip routes (type-2) are imported into VNI and VRF tables.
3410 * IMET route is imported into VNI table.
3411 * prefix routes are imported into VRF table.
3413 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3414 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3415 evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
||
3416 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3418 irt
= in_vni_rt
? lookup_import_rt(bgp
, eval
) : NULL
;
3420 install_uninstall_route_in_vnis(
3421 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3424 vrf_irt
= in_vrf_rt
? lookup_vrf_import_rt(eval
) : NULL
;
3426 install_uninstall_route_in_vrfs(
3427 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3430 /* Also check for non-exact match.
3431 * In this, we mask out the AS and
3432 * only check on the local-admin sub-field.
3433 * This is to facilitate using
3434 * VNI as the RT for EBGP peering too.
3438 if (type
== ECOMMUNITY_ENCODE_AS
3439 || type
== ECOMMUNITY_ENCODE_AS4
3440 || type
== ECOMMUNITY_ENCODE_IP
) {
3441 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
3442 mask_ecom_global_admin(&eval_tmp
, eval
);
3444 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3447 lookup_vrf_import_rt(&eval_tmp
);
3451 install_uninstall_route_in_vnis(
3452 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3455 install_uninstall_route_in_vrfs(
3456 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3460 /* es route is imported into the es table */
3461 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3463 /* we will match based on the entire esi to avoid
3464 * import of an es route for esi2 into esi1
3466 es
= bgp_evpn_es_find(&evp
->prefix
.es_addr
.esi
);
3467 if (es
&& bgp_evpn_is_es_local(es
))
3468 bgp_evpn_es_route_install_uninstall(
3469 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3477 * Install or uninstall route for appropriate VNIs/ESIs.
3479 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3480 const struct prefix
*p
,
3481 struct bgp_path_info
*pi
, int import
)
3483 return bgp_evpn_install_uninstall_table(bgp
, afi
, safi
, p
, pi
, import
,
3487 void bgp_evpn_import_type2_route(struct bgp_path_info
*pi
, int import
)
3489 struct bgp
*bgp_evpn
;
3491 bgp_evpn
= bgp_get_evpn();
3495 install_uninstall_evpn_route(bgp_evpn
, AFI_L2VPN
, SAFI_EVPN
,
3496 &pi
->net
->p
, pi
, import
);
3500 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3503 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3505 /* Delete ipv4 default route and withdraw from peers */
3506 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3507 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3508 SAFI_UNICAST
, false);
3510 /* delete all ipv4 routes and withdraw from peers */
3511 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3512 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3514 /* Delete ipv6 default route and withdraw from peers */
3515 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3516 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3517 SAFI_UNICAST
, false);
3519 /* delete all ipv6 routes and withdraw from peers */
3520 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3521 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3525 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3528 void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3530 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
3532 bgp_evpn
= bgp_get_evpn();
3536 /* update all ipv4 routes */
3537 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3538 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3540 /* update ipv4 default route and withdraw from peers */
3541 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3542 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3543 SAFI_UNICAST
, true);
3545 /* update all ipv6 routes */
3546 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3547 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3549 /* update ipv6 default route and withdraw from peers */
3550 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3551 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3552 SAFI_UNICAST
, true);
3557 * update and advertise local routes for a VRF as type-5 routes.
3558 * This is invoked upon RD change for a VRF. Note taht the processing is only
3559 * done in the global route table using the routes which already exist in the
3562 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3564 /* skip if the RD is configured */
3565 if (is_vrf_rd_configured(bgp_vrf
))
3568 /* derive the RD for the VRF based on new router-id */
3569 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3571 /* update advertise ipv4|ipv6 routes as type-5 routes */
3572 update_advertise_vrf_routes(bgp_vrf
);
3576 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3577 * This is invoked upon VRF RD change. The processing is done only from global
3580 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3582 /* skip if the RD is configured */
3583 if (is_vrf_rd_configured(bgp_vrf
))
3586 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3587 delete_withdraw_vrf_routes(bgp_vrf
);
3591 * Update and advertise local routes for a VNI. Invoked upon router-id
3592 * change. Note that the processing is done only on the global route table
3593 * using routes that already exist in the per-VNI table.
3595 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3597 struct prefix_evpn p
;
3598 struct bgp_dest
*dest
, *global_dest
;
3599 struct bgp_path_info
*pi
, *global_pi
;
3601 afi_t afi
= AFI_L2VPN
;
3602 safi_t safi
= SAFI_EVPN
;
3604 /* Locate type-3 route for VNI in the per-VNI table and use its
3605 * attributes to create and advertise the type-3 route for this VNI
3606 * in the global table.
3608 * RT-3 only if doing head-end replication
3610 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3611 == VXLAN_FLOOD_HEAD_END_REPL
) {
3612 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3613 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3614 if (!dest
) /* unexpected */
3616 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
3617 if (pi
->peer
== bgp
->peer_self
&&
3618 pi
->type
== ZEBRA_ROUTE_BGP
3619 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3621 if (!pi
) /* unexpected */
3625 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
],
3626 afi
, safi
, &p
, &vpn
->prd
);
3627 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_dest
, attr
,
3628 1, &pi
, 0, mac_mobility_seqnum(attr
),
3629 false /* setup_sync */, NULL
/* old_is_sync */);
3631 /* Schedule for processing and unlock node. */
3632 bgp_process(bgp
, global_dest
, afi
, safi
);
3633 bgp_dest_unlock_node(global_dest
);
3636 /* Now, walk this VNI's route table and use the route and its attribute
3637 * to create and schedule route in global table.
3639 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
3640 dest
= bgp_route_next(dest
)) {
3641 const struct prefix_evpn
*evp
=
3642 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
3645 * We have already processed type-3 routes.
3646 * Process only type-1 and type-2 routes here.
3648 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
3649 && evp
->prefix
.route_type
!= BGP_EVPN_AD_ROUTE
)
3652 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
3653 if (pi
->peer
== bgp
->peer_self
3654 && pi
->type
== ZEBRA_ROUTE_BGP
3655 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3660 /* Create route in global routing table using this route entry's
3664 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3666 assert(global_dest
);
3668 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3670 update_evpn_route_entry(
3671 bgp
, vpn
, afi
, safi
, global_dest
, attr
, 1,
3672 &global_pi
, 0, mac_mobility_seqnum(attr
),
3673 false /* setup_sync */, NULL
/* old_is_sync */);
3676 struct bgp_evpn_es
*es
;
3677 int route_changed
= 0;
3679 es
= bgp_evpn_es_find(&evp
->prefix
.ead_addr
.esi
);
3680 bgp_evpn_mh_route_update(bgp
, es
, vpn
, afi
, safi
,
3681 global_dest
, attr
, &global_pi
,
3685 /* Schedule for processing and unlock node. */
3686 bgp_process(bgp
, global_dest
, afi
, safi
);
3687 bgp_dest_unlock_node(global_dest
);
3694 * Delete (and withdraw) local routes for a VNI - only from the global
3695 * table. Invoked upon router-id change.
3697 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3699 struct prefix_evpn p
;
3700 struct bgp_dest
*global_dest
;
3701 struct bgp_path_info
*pi
;
3702 afi_t afi
= AFI_L2VPN
;
3703 safi_t safi
= SAFI_EVPN
;
3705 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3706 * for this VNI - from the global table.
3708 delete_global_type2_routes(bgp
, vpn
);
3710 /* Remove type-3 route for this VNI from global table. */
3711 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3712 global_dest
= bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3713 (const struct prefix_evpn
*)&p
, &vpn
->prd
);
3715 /* Delete route entry in the global EVPN table. */
3716 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
3718 /* Schedule for processing - withdraws to peers happen from
3722 bgp_process(bgp
, global_dest
, afi
, safi
);
3723 bgp_dest_unlock_node(global_dest
);
3727 delete_global_ead_evi_routes(bgp
, vpn
);
3732 * Handle router-id change. Update and advertise local routes corresponding
3733 * to this VNI from peers. Note that this is invoked after updating the
3734 * router-id. The routes in the per-VNI table are used to create routes in
3735 * the global table and schedule them.
3737 static void update_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3739 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3741 /* Skip VNIs with configured RD. */
3742 if (is_rd_configured(vpn
))
3745 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3746 update_advertise_vni_routes(bgp
, vpn
);
3750 * Handle router-id change. Delete and withdraw local routes corresponding
3751 * to this VNI from peers. Note that this is invoked prior to updating
3752 * the router-id and is done only on the global route table, the routes
3753 * are needed in the per-VNI table to re-advertise with new router id.
3755 static void withdraw_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3757 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3759 /* Skip VNIs with configured RD. */
3760 if (is_rd_configured(vpn
))
3763 delete_withdraw_vni_routes(bgp
, vpn
);
3767 * Create RT-3 for a VNI and schedule for processing and advertisement.
3768 * This is invoked upon flooding mode changing to head-end replication.
3770 static void create_advertise_type3(struct hash_bucket
*bucket
, void *data
)
3772 struct bgpevpn
*vpn
= bucket
->data
;
3773 struct bgp
*bgp
= data
;
3774 struct prefix_evpn p
;
3776 if (!vpn
|| !is_vni_live(vpn
) ||
3777 bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3778 != VXLAN_FLOOD_HEAD_END_REPL
)
3781 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3782 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
))
3783 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3784 "Type3 route creation failure for VNI %u", vpn
->vni
);
3788 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3789 * This is invoked upon flooding mode changing to drop BUM packets.
3791 static void delete_withdraw_type3(struct hash_bucket
*bucket
, void *data
)
3793 struct bgpevpn
*vpn
= bucket
->data
;
3794 struct bgp
*bgp
= data
;
3795 struct prefix_evpn p
;
3797 if (!vpn
|| !is_vni_live(vpn
))
3800 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3801 delete_evpn_route(bgp
, vpn
, &p
);
3805 * Process received EVPN type-2 route (advertise or withdraw).
3807 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3808 struct attr
*attr
, uint8_t *pfx
, int psize
,
3809 uint32_t addpath_id
)
3811 struct prefix_rd prd
;
3812 struct prefix_evpn p
= {};
3813 struct bgp_route_evpn evpn
= {};
3815 uint8_t macaddr_len
;
3816 /* holds the VNI(s) as in packet */
3817 mpls_label_t label
[BGP_MAX_LABELS
] = {};
3818 uint32_t num_labels
= 0;
3822 /* Type-2 route should be either 33, 37 or 49 bytes or an
3823 * additional 3 bytes if there is a second label (VNI):
3824 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3825 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3826 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3828 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3829 && psize
!= 40 && psize
!= 52) {
3830 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3831 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3832 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3836 struct stream
*pkt
= stream_new(psize
);
3837 stream_put(pkt
, pfx
, psize
);
3839 /* Make prefix_rd */
3840 prd
.family
= AF_UNSPEC
;
3843 STREAM_GET(&prd
.val
, pkt
, 8);
3845 /* Make EVPN prefix. */
3847 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3848 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3850 /* Copy Ethernet Seg Identifier */
3852 STREAM_GET(&attr
->esi
, pkt
, sizeof(esi_t
));
3854 if (bgp_evpn_is_esi_local_and_non_bypass(&attr
->esi
))
3855 attr
->es_flags
|= ATTR_ES_IS_LOCAL
;
3857 attr
->es_flags
&= ~ATTR_ES_IS_LOCAL
;
3859 STREAM_FORWARD_GETP(pkt
, sizeof(esi_t
));
3862 /* Copy Ethernet Tag */
3863 STREAM_GET(ð_tag
, pkt
, 4);
3864 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3866 /* Get the MAC Addr len */
3867 STREAM_GETC(pkt
, macaddr_len
);
3869 /* Get the MAC Addr */
3870 if (macaddr_len
== (ETH_ALEN
* 8)) {
3871 STREAM_GET(&p
.prefix
.macip_addr
.mac
.octet
, pkt
, ETH_ALEN
);
3874 EC_BGP_EVPN_ROUTE_INVALID
,
3875 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3876 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3882 STREAM_GETC(pkt
, ipaddr_len
);
3884 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3885 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3887 EC_BGP_EVPN_ROUTE_INVALID
,
3888 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3889 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3894 ipaddr_len
/= 8; /* Convert to bytes. */
3895 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3898 STREAM_GET(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pkt
, ipaddr_len
);
3901 /* Get the VNI(s). Stored as bytes here. */
3902 STREAM_GET(&label
[0], pkt
, BGP_LABEL_BYTES
);
3905 /* Do we have a second VNI? */
3906 if (STREAM_READABLE(pkt
)) {
3908 STREAM_GET(&label
[1], pkt
, BGP_LABEL_BYTES
);
3911 /* Process the route. */
3913 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3914 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3915 &prd
, &label
[0], num_labels
, 0, &evpn
);
3917 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3918 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3919 &prd
, &label
[0], num_labels
, &evpn
);
3924 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3925 "%u:%s - Rx EVPN Type-2 NLRI - corrupt, discarding",
3926 peer
->bgp
->vrf_id
, peer
->host
);
3934 * Process received EVPN type-3 route (advertise or withdraw).
3936 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3937 struct attr
*attr
, uint8_t *pfx
, int psize
,
3938 uint32_t addpath_id
)
3940 struct prefix_rd prd
;
3941 struct prefix_evpn p
;
3946 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3947 * IP len (1) and IP (4 or 16).
3949 if (psize
!= 17 && psize
!= 29) {
3950 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3951 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3952 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3956 /* If PMSI is present, log if it is anything other than IR.
3957 * Note: We just simply ignore the values as it is not clear if
3958 * doing anything else is better.
3961 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3962 enum pta_type pmsi_tnl_type
= bgp_attr_get_pmsi_tnl_type(attr
);
3964 if (pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
3965 && pmsi_tnl_type
!= PMSI_TNLTYPE_PIM_SM
) {
3967 EC_BGP_EVPN_PMSI_PRESENT
,
3968 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3969 peer
->bgp
->vrf_id
, peer
->host
, pmsi_tnl_type
);
3973 /* Make prefix_rd */
3974 prd
.family
= AF_UNSPEC
;
3976 memcpy(&prd
.val
, pfx
, 8);
3979 /* Make EVPN prefix. */
3980 memset(&p
, 0, sizeof(p
));
3982 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3983 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3985 /* Copy Ethernet Tag */
3986 memcpy(ð_tag
, pfx
, 4);
3987 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
3991 ipaddr_len
= *pfx
++;
3992 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3993 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
3994 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
3997 EC_BGP_EVPN_ROUTE_INVALID
,
3998 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3999 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4003 /* Process the route. */
4005 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4006 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4007 &prd
, NULL
, 0, 0, NULL
);
4009 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4010 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4011 &prd
, NULL
, 0, NULL
);
4016 * Process received EVPN type-5 route (advertise or withdraw).
4018 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4019 struct attr
*attr
, uint8_t *pfx
, int psize
,
4020 uint32_t addpath_id
)
4022 struct prefix_rd prd
;
4023 struct prefix_evpn p
;
4024 struct bgp_route_evpn evpn
;
4027 mpls_label_t label
; /* holds the VNI as in the packet */
4029 bool is_valid_update
= true;
4031 /* Type-5 route should be 34 or 58 bytes:
4032 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
4033 * GW (4 or 16) and VNI (3).
4034 * Note that the IP and GW should both be IPv4 or both IPv6.
4036 if (psize
!= 34 && psize
!= 58) {
4037 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4038 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
4039 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4043 /* Make prefix_rd */
4044 prd
.family
= AF_UNSPEC
;
4046 memcpy(&prd
.val
, pfx
, 8);
4049 /* Make EVPN prefix. */
4050 memset(&p
, 0, sizeof(p
));
4052 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4053 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
4055 /* Additional information outside of prefix - ESI and GW IP */
4056 memset(&evpn
, 0, sizeof(evpn
));
4058 /* Fetch ESI overlay index */
4060 memcpy(&evpn
.eth_s_id
, pfx
, sizeof(esi_t
));
4063 /* Fetch Ethernet Tag. */
4064 memcpy(ð_tag
, pfx
, 4);
4065 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
4068 /* Fetch IP prefix length. */
4070 if (ippfx_len
> IPV6_MAX_BITLEN
) {
4072 EC_BGP_EVPN_ROUTE_INVALID
,
4073 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4074 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
4077 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
4079 /* Determine IPv4 or IPv6 prefix */
4080 /* Since the address and GW are from the same family, this just becomes
4081 * a simple check on the total size.
4084 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
4085 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
4087 SET_IPADDR_V4(&evpn
.gw_ip
);
4088 memcpy(&evpn
.gw_ip
.ipaddr_v4
, pfx
, 4);
4091 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
4092 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
,
4094 pfx
+= IPV6_MAX_BYTELEN
;
4095 SET_IPADDR_V6(&evpn
.gw_ip
);
4096 memcpy(&evpn
.gw_ip
.ipaddr_v6
, pfx
, IPV6_MAX_BYTELEN
);
4097 pfx
+= IPV6_MAX_BYTELEN
;
4100 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4101 memset(&label
, 0, sizeof(label
));
4102 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
4105 * If in future, we are required to access additional fields,
4106 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4111 * An update containing a non-zero gateway IP and a non-zero ESI
4112 * at the same time is should be treated as withdraw
4114 if (bgp_evpn_is_esi_valid(&evpn
.eth_s_id
) &&
4115 !ipaddr_is_zero(&evpn
.gw_ip
)) {
4116 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4117 "%s - Rx EVPN Type-5 ESI and gateway-IP both non-zero.",
4119 is_valid_update
= false;
4120 } else if (bgp_evpn_is_esi_valid(&evpn
.eth_s_id
))
4121 evpn
.type
= OVERLAY_INDEX_ESI
;
4122 else if (!ipaddr_is_zero(&evpn
.gw_ip
))
4123 evpn
.type
= OVERLAY_INDEX_GATEWAY_IP
;
4125 if (is_zero_mac(&attr
->rmac
) &&
4126 !bgp_evpn_is_esi_valid(&evpn
.eth_s_id
) &&
4127 ipaddr_is_zero(&evpn
.gw_ip
) && label
== 0) {
4128 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4129 "%s - Rx EVPN Type-5 ESI, gateway-IP, RMAC and label all zero",
4131 is_valid_update
= false;
4134 if (is_mcast_mac(&attr
->rmac
) || is_bcast_mac(&attr
->rmac
))
4135 is_valid_update
= false;
4138 /* Process the route. */
4139 if (attr
&& is_valid_update
)
4140 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4141 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4142 &prd
, &label
, 1, 0, &evpn
);
4144 if (!is_valid_update
) {
4145 char attr_str
[BUFSIZ
] = {0};
4147 bgp_dump_attr(attr
, attr_str
, BUFSIZ
);
4149 "Invalid update from peer %s vrf %u prefix %pFX attr %s - treat as withdraw",
4150 peer
->hostname
, peer
->bgp
->vrf_id
, &p
,
4153 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4154 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4155 &prd
, &label
, 1, &evpn
);
4161 static void evpn_mpattr_encode_type5(struct stream
*s
, const struct prefix
*p
,
4162 const struct prefix_rd
*prd
,
4163 mpls_label_t
*label
, uint32_t num_labels
,
4168 const struct evpn_addr
*p_evpn_p
;
4170 memset(&temp
, 0, sizeof(temp
));
4171 if (p
->family
!= AF_EVPN
)
4173 p_evpn_p
= &(p
->u
.prefix_evpn
);
4175 /* len denites the total len of IP and GW-IP in the route
4176 IP and GW-IP have to be both ipv4 or ipv6
4178 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4179 len
= 8; /* IP and GWIP are both ipv4 */
4181 len
= 32; /* IP and GWIP are both ipv6 */
4182 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4183 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4184 stream_put(s
, prd
->val
, 8);
4185 if (attr
&& attr
->evpn_overlay
.type
== OVERLAY_INDEX_ESI
)
4186 stream_put(s
, &attr
->esi
, sizeof(esi_t
));
4188 stream_put(s
, 0, sizeof(esi_t
));
4189 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4190 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4191 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4192 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4194 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4195 if (attr
&& attr
->evpn_overlay
.type
== OVERLAY_INDEX_GATEWAY_IP
) {
4196 const struct bgp_route_evpn
*evpn_overlay
=
4197 bgp_attr_get_evpn_overlay(attr
);
4199 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4201 evpn_overlay
->gw_ip
.ipaddr_v4
.s_addr
);
4203 stream_put(s
, &(evpn_overlay
->gw_ip
.ipaddr_v6
), 16);
4205 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4206 stream_put_ipv4(s
, 0);
4208 stream_put(s
, &temp
, 16);
4212 stream_put(s
, label
, 3);
4218 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4220 static void cleanup_vni_on_disable(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4222 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4224 /* Remove EVPN routes and schedule for processing. */
4225 delete_routes_for_vni(bgp
, vpn
);
4227 /* Clear "live" flag and see if hash needs to be freed. */
4228 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4229 if (!is_vni_configured(vpn
))
4230 bgp_evpn_free(bgp
, vpn
);
4234 * Free a VNI entry; iterator function called during cleanup.
4236 static void free_vni_entry(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4238 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4240 delete_all_vni_routes(bgp
, vpn
);
4241 bgp_evpn_free(bgp
, vpn
);
4245 * Derive AUTO import RT for BGP VRF - L3VNI
4247 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4249 struct bgp
*bgp_evpn
= NULL
;
4251 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4252 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4255 bgp_evpn
= bgp_get_evpn();
4258 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4262 * Delete AUTO import RT from BGP VRF - L3VNI
4264 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4266 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4270 * Derive AUTO export RT for BGP VRF - L3VNI
4272 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4274 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4275 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4279 * Delete AUTO export RT from BGP VRF - L3VNI
4281 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4283 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4286 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4288 struct bgp
*bgp_evpn
= NULL
;
4289 struct listnode
*node
= NULL
;
4290 struct bgpevpn
*vpn
= NULL
;
4292 bgp_evpn
= bgp_get_evpn();
4296 /* update all type-5 routes */
4297 update_advertise_vrf_routes(bgp_vrf
);
4299 /* update all type-2 routes */
4300 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4301 update_routes_for_vni(bgp_evpn
, vpn
);
4305 * Handle autort change for a given VNI.
4307 static void update_autort_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4309 struct bgpevpn
*vpn
= bucket
->data
;
4311 if (!is_import_rt_configured(vpn
)) {
4312 if (is_vni_live(vpn
))
4313 bgp_evpn_uninstall_routes(bgp
, vpn
);
4314 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4315 list_delete_all_node(vpn
->import_rtl
);
4316 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4317 if (is_vni_live(vpn
))
4318 bgp_evpn_install_routes(bgp
, vpn
);
4320 if (!is_export_rt_configured(vpn
)) {
4321 list_delete_all_node(vpn
->export_rtl
);
4322 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4323 if (is_vni_live(vpn
))
4324 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4329 * Handle autort change for L3VNI.
4331 static void update_autort_l3vni(struct bgp
*bgp
)
4333 if ((CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
4334 && (CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)))
4337 if (!CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
)) {
4338 if (is_l3vni_live(bgp
))
4339 uninstall_routes_for_vrf(bgp
);
4341 /* Cleanup the RT to VRF mapping */
4342 bgp_evpn_unmap_vrf_from_its_rts(bgp
);
4344 /* Remove auto generated RT */
4345 evpn_auto_rt_import_delete_for_vrf(bgp
);
4347 list_delete_all_node(bgp
->vrf_import_rtl
);
4349 /* Map auto derive or configured RTs */
4350 evpn_auto_rt_import_add_for_vrf(bgp
);
4353 if (!CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
4354 list_delete_all_node(bgp
->vrf_export_rtl
);
4356 evpn_auto_rt_export_delete_for_vrf(bgp
);
4358 evpn_auto_rt_export_add_for_vrf(bgp
);
4360 if (is_l3vni_live(bgp
))
4361 bgp_evpn_map_vrf_to_its_rts(bgp
);
4364 if (!is_l3vni_live(bgp
))
4367 /* advertise type-5 routes if needed */
4368 update_advertise_vrf_routes(bgp
);
4370 /* install all remote routes belonging to this l3vni
4371 * into corresponding vrf
4373 install_routes_for_vrf(bgp
);
4380 /* withdraw type-5 route corresponding to ip prefix */
4381 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4382 afi_t afi
, safi_t safi
)
4385 struct prefix_evpn evp
;
4387 build_type5_prefix_from_ip_prefix(&evp
, p
);
4388 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4391 EC_BGP_EVPN_ROUTE_DELETE
,
4392 "%u failed to delete type-5 route for prefix %pFX in vrf %s",
4393 bgp_vrf
->vrf_id
, p
, vrf_id_to_name(bgp_vrf
->vrf_id
));
4396 /* withdraw all type-5 routes for an address family */
4397 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4399 struct bgp_table
*table
= NULL
;
4400 struct bgp_dest
*dest
= NULL
;
4401 struct bgp_path_info
*pi
;
4403 table
= bgp_vrf
->rib
[afi
][safi
];
4404 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
4405 /* Only care about "selected" routes. Also ensure that
4406 * these are routes that are injectable into EVPN.
4408 /* TODO: Support for AddPath for EVPN. */
4409 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
4410 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4411 && is_route_injectable_into_evpn(pi
)) {
4412 bgp_evpn_withdraw_type5_route(
4413 bgp_vrf
, bgp_dest_get_prefix(dest
), afi
,
4422 * evpn - enable advertisement of default g/w
4424 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
4425 safi_t safi
, bool add
)
4427 struct prefix ip_prefix
;
4429 /* form the default prefix 0.0.0.0/0 */
4430 memset(&ip_prefix
, 0, sizeof(ip_prefix
));
4431 ip_prefix
.family
= afi2family(afi
);
4434 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
4437 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
4444 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4445 * to this function correspond to those of the source IP prefix (best
4446 * path in the case of the attr. In the case of a local prefix (when we
4447 * are advertising local subnets), the src_attr will be NULL.
4449 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4450 struct attr
*src_attr
, afi_t afi
,
4454 struct prefix_evpn evp
;
4456 build_type5_prefix_from_ip_prefix(&evp
, p
);
4457 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
, afi
, safi
);
4459 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4460 "%u: Failed to create type-5 route for prefix %pFX",
4461 bgp_vrf
->vrf_id
, p
);
4464 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4465 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4466 * advertisement is enabled.
4468 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4471 struct bgp_table
*table
= NULL
;
4472 struct bgp_dest
*dest
= NULL
;
4473 struct bgp_path_info
*pi
;
4475 table
= bgp_vrf
->rib
[afi
][safi
];
4476 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
4477 /* Need to identify the "selected" route entry to use its
4478 * attribute. Also, ensure that the route is injectable
4480 * TODO: Support for AddPath for EVPN.
4482 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
4483 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4484 && is_route_injectable_into_evpn(pi
)) {
4486 /* apply the route-map */
4487 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4488 route_map_result_t ret
;
4489 struct bgp_path_info tmp_pi
;
4490 struct bgp_path_info_extra tmp_pie
;
4491 struct attr tmp_attr
;
4493 tmp_attr
= *pi
->attr
;
4495 /* Fill temp path_info */
4496 prep_for_rmap_apply(&tmp_pi
, &tmp_pie
,
4500 RESET_FLAG(tmp_attr
.rmap_change_flags
);
4502 ret
= route_map_apply(
4503 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4505 bgp_dest_get_prefix(dest
),
4507 if (ret
== RMAP_DENYMATCH
) {
4508 bgp_attr_flush(&tmp_attr
);
4511 bgp_evpn_advertise_type5_route(
4513 bgp_dest_get_prefix(dest
),
4514 &tmp_attr
, afi
, safi
);
4516 bgp_evpn_advertise_type5_route(
4518 bgp_dest_get_prefix(dest
),
4519 pi
->attr
, afi
, safi
);
4526 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4528 struct listnode
*node
, *nnode
, *node_to_del
;
4529 struct ecommunity
*ecom
, *ecom_auto
;
4530 struct ecommunity_val eval
;
4532 if (bgp
->advertise_autort_rfc8365
)
4533 vni
|= EVPN_AUTORT_VXLAN
;
4534 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4536 ecom_auto
= ecommunity_new();
4537 ecommunity_add_val(ecom_auto
, &eval
, false, false);
4540 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4541 if (ecommunity_match(ecom
, ecom_auto
)) {
4542 ecommunity_free(&ecom
);
4549 list_delete_node(rtl
, node_to_del
);
4551 ecommunity_free(&ecom_auto
);
4554 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4555 struct ecommunity
*ecomadd
)
4557 /* uninstall routes from vrf */
4558 if (is_l3vni_live(bgp_vrf
))
4559 uninstall_routes_for_vrf(bgp_vrf
);
4561 /* Cleanup the RT to VRF mapping */
4562 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4564 /* Remove auto generated RT */
4565 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4567 /* Add the newly configured RT to RT list */
4568 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4569 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4571 /* map VRF to its RTs and install routes matching the new RTs */
4572 if (is_l3vni_live(bgp_vrf
)) {
4573 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4574 install_routes_for_vrf(bgp_vrf
);
4578 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4579 struct ecommunity
*ecomdel
)
4581 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4582 struct ecommunity
*ecom
= NULL
;
4584 /* uninstall routes from vrf */
4585 if (is_l3vni_live(bgp_vrf
))
4586 uninstall_routes_for_vrf(bgp_vrf
);
4588 /* Cleanup the RT to VRF mapping */
4589 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4591 /* remove the RT from the RT list */
4592 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4593 if (ecommunity_match(ecom
, ecomdel
)) {
4594 ecommunity_free(&ecom
);
4601 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4603 assert(bgp_vrf
->vrf_import_rtl
);
4604 /* fallback to auto import rt, if this was the last RT */
4605 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4606 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4607 if (is_l3vni_live(bgp_vrf
))
4608 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4611 /* map VRFs to its RTs and install routes matching this new RT */
4612 if (is_l3vni_live(bgp_vrf
)) {
4613 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4614 install_routes_for_vrf(bgp_vrf
);
4618 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4619 struct ecommunity
*ecomadd
)
4621 /* remove auto-generated RT */
4622 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4624 /* Add the new RT to the RT list */
4625 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4626 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4628 if (is_l3vni_live(bgp_vrf
))
4629 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4632 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4633 struct ecommunity
*ecomdel
)
4635 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4636 struct ecommunity
*ecom
= NULL
;
4638 /* Remove the RT from the RT list */
4639 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4640 if (ecommunity_match(ecom
, ecomdel
)) {
4641 ecommunity_free(&ecom
);
4648 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4651 * Temporary assert to make SA happy.
4652 * The ALL_LIST_ELEMENTS macro above has a NULL check
4653 * which means that SA is going to complain about
4654 * the list_isempty call, which doesn't NULL check.
4655 * So until we get this situation cleaned up, here
4658 assert(bgp_vrf
->vrf_export_rtl
);
4660 /* fall back to auto-generated RT if this was the last RT */
4661 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4662 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4663 if (is_l3vni_live(bgp_vrf
))
4664 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4667 if (is_l3vni_live(bgp_vrf
))
4668 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4672 * Handle change to BGP router id. This is invoked twice by the change
4673 * handler, first before the router id has been changed and then after
4674 * the router id has been changed. The first invocation will result in
4675 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4676 * will result in the routes being re-advertised.
4678 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4680 struct listnode
*node
;
4681 struct bgp
*bgp_vrf
;
4685 /* delete and withdraw all the type-5 routes
4686 stored in the global table for this vrf
4688 withdraw_router_id_vrf(bgp
);
4690 /* delete all the VNI routes (type-2/type-3) routes for all the
4693 hash_iterate(bgp
->vnihash
,
4694 (void (*)(struct hash_bucket
*,
4695 void *))withdraw_router_id_vni
,
4698 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4699 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4700 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4701 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4703 bgp_vrf
->evpn_info
->pip_ip
.s_addr
4709 /* Assign new default instance router-id */
4710 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4711 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4712 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4713 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4715 bgp_vrf
->evpn_info
->pip_ip
=
4717 /* advertise type-5 routes with
4720 update_advertise_vrf_routes(bgp_vrf
);
4725 /* advertise all routes in the vrf as type-5 routes with the new
4728 update_router_id_vrf(bgp
);
4730 /* advertise all the VNI routes (type-2/type-3) routes with the
4733 hash_iterate(bgp
->vnihash
,
4734 (void (*)(struct hash_bucket
*,
4735 void *))update_router_id_vni
,
4741 * Handle change to auto-RT algorithm - update and advertise local routes.
4743 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4745 hash_iterate(bgp
->vnihash
,
4746 (void (*)(struct hash_bucket
*,
4747 void*))update_autort_vni
,
4750 update_autort_l3vni(bgp
);
4754 * Handle change to export RT - update and advertise local routes.
4756 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4758 return update_routes_for_vni(bgp
, vpn
);
4761 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4764 delete_withdraw_vrf_routes(bgp_vrf
);
4766 update_advertise_vrf_routes(bgp_vrf
);
4770 * Handle change to RD. This is invoked twice by the change handler,
4771 * first before the RD has been changed and then after the RD has
4772 * been changed. The first invocation will result in local routes
4773 * of this VNI being deleted and withdrawn and the next will result
4774 * in the routes being re-advertised.
4776 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4780 delete_withdraw_vni_routes(bgp
, vpn
);
4782 update_advertise_vni_routes(bgp
, vpn
);
4786 * Install routes for this VNI. Invoked upon change to Import RT.
4788 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4790 return install_routes_for_vni(bgp
, vpn
);
4794 * Uninstall all routes installed for this VNI. Invoked upon change
4797 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4799 return uninstall_routes_for_vni(bgp
, vpn
);
4803 * TODO: Hardcoded for a maximum of 2 VNIs right now
4805 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4810 vni1
= label2vni(label
);
4811 if (num_labels
== 2) {
4812 vni2
= label2vni(label
+ 1);
4813 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4815 snprintf(buf
, len
, "%u", vni1
);
4820 * Function to convert evpn route to json format.
4821 * NOTE: We don't use prefix2str as the output here is a bit different.
4823 void bgp_evpn_route2json(const struct prefix_evpn
*p
, json_object
*json
)
4825 char buf1
[ETHER_ADDR_STRLEN
];
4826 char buf2
[PREFIX2STR_BUFFER
];
4833 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4835 switch (p
->prefix
.route_type
) {
4836 case BGP_EVPN_MAC_IP_ROUTE
:
4837 json_object_int_add(json
, "ethTag",
4838 p
->prefix
.macip_addr
.eth_tag
);
4839 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4840 json_object_string_add(json
, "mac",
4841 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4844 if (!is_evpn_prefix_ipaddr_none(p
)) {
4845 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
:
4847 prefixlen
= (family
== AF_INET
) ?
4848 IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
;
4849 inet_ntop(family
, &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4850 buf2
, PREFIX2STR_BUFFER
);
4851 json_object_int_add(json
, "ipLen", prefixlen
);
4852 json_object_string_add(json
, "ip", buf2
);
4856 case BGP_EVPN_IMET_ROUTE
:
4857 json_object_int_add(json
, "ethTag",
4858 p
->prefix
.imet_addr
.eth_tag
);
4859 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4860 prefixlen
= (family
== AF_INET
) ? IPV4_MAX_BITLEN
:
4862 inet_ntop(family
, &p
->prefix
.imet_addr
.ip
.ip
.addr
, buf2
,
4864 json_object_int_add(json
, "ipLen", prefixlen
);
4865 json_object_string_add(json
, "ip", buf2
);
4868 case BGP_EVPN_IP_PREFIX_ROUTE
:
4869 json_object_int_add(json
, "ethTag",
4870 p
->prefix
.prefix_addr
.eth_tag
);
4871 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4872 inet_ntop(family
, &p
->prefix
.prefix_addr
.ip
.ip
.addr
,
4873 buf2
, sizeof(buf2
));
4874 json_object_int_add(json
, "ipLen",
4875 p
->prefix
.prefix_addr
.ip_prefix_length
);
4876 json_object_string_add(json
, "ip", buf2
);
4885 * Encode EVPN prefix in Update (MP_REACH)
4887 void bgp_evpn_encode_prefix(struct stream
*s
, const struct prefix
*p
,
4888 const struct prefix_rd
*prd
, mpls_label_t
*label
,
4889 uint32_t num_labels
, struct attr
*attr
,
4890 bool addpath_capable
, uint32_t addpath_tx_id
)
4892 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4893 int len
, ipa_len
= 0;
4895 if (addpath_capable
)
4896 stream_putl(s
, addpath_tx_id
);
4899 stream_putc(s
, evp
->prefix
.route_type
);
4901 switch (evp
->prefix
.route_type
) {
4902 case BGP_EVPN_MAC_IP_ROUTE
:
4903 if (is_evpn_prefix_ipaddr_v4(evp
))
4904 ipa_len
= IPV4_MAX_BYTELEN
;
4905 else if (is_evpn_prefix_ipaddr_v6(evp
))
4906 ipa_len
= IPV6_MAX_BYTELEN
;
4907 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4908 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4909 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4911 stream_putc(s
, len
);
4912 stream_put(s
, prd
->val
, 8); /* RD */
4914 stream_put(s
, &attr
->esi
, ESI_BYTES
);
4916 stream_put(s
, 0, 10);
4917 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4918 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4919 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4920 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4921 if (ipa_len
) /* IP */
4922 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4924 /* 1st label is the L2 VNI */
4925 stream_put(s
, label
, BGP_LABEL_BYTES
);
4926 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4927 if (ipa_len
&& num_labels
> 1)
4928 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4931 case BGP_EVPN_IMET_ROUTE
:
4932 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4933 stream_put(s
, prd
->val
, 8); /* RD */
4934 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4935 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4936 /* Originating Router's IP Addr */
4937 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4940 case BGP_EVPN_ES_ROUTE
:
4941 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4942 stream_put(s
, prd
->val
, 8); /* RD */
4943 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4944 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4946 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4949 case BGP_EVPN_AD_ROUTE
:
4950 /* RD, ESI, EthTag, 1 VNI */
4951 len
= RD_BYTES
+ ESI_BYTES
+ EVPN_ETH_TAG_BYTES
+ BGP_LABEL_BYTES
;
4952 stream_putc(s
, len
);
4953 stream_put(s
, prd
->val
, RD_BYTES
); /* RD */
4954 stream_put(s
, evp
->prefix
.ead_addr
.esi
.val
, ESI_BYTES
); /* ESI */
4955 stream_putl(s
, evp
->prefix
.ead_addr
.eth_tag
); /* Ethernet Tag */
4956 stream_put(s
, label
, BGP_LABEL_BYTES
);
4959 case BGP_EVPN_IP_PREFIX_ROUTE
:
4960 /* TODO: AddPath support. */
4961 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4969 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4970 struct bgp_nlri
*packet
, int withdraw
)
4976 uint32_t addpath_id
;
4977 bool addpath_capable
;
4982 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4984 lim
= pnt
+ packet
->length
;
4986 safi
= packet
->safi
;
4989 addpath_capable
= bgp_addpath_encode_rx(peer
, afi
, safi
);
4991 for (; pnt
< lim
; pnt
+= psize
) {
4992 /* Clear prefix structure. */
4993 memset(&p
, 0, sizeof(p
));
4995 /* Deal with path-id if AddPath is supported. */
4996 if (addpath_capable
) {
4997 /* When packet overflow occurs return immediately. */
4998 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4999 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5001 memcpy(&addpath_id
, pnt
, BGP_ADDPATH_ID_LEN
);
5002 addpath_id
= ntohl(addpath_id
);
5003 pnt
+= BGP_ADDPATH_ID_LEN
;
5006 /* All EVPN NLRI types start with type and length. */
5008 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE
;
5013 /* When packet overflow occur return immediately. */
5014 if (pnt
+ psize
> lim
)
5015 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5018 case BGP_EVPN_MAC_IP_ROUTE
:
5019 if (process_type2_route(peer
, afi
, safi
,
5020 withdraw
? NULL
: attr
, pnt
,
5021 psize
, addpath_id
)) {
5024 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
5025 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5026 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE
;
5030 case BGP_EVPN_IMET_ROUTE
:
5031 if (process_type3_route(peer
, afi
, safi
,
5032 withdraw
? NULL
: attr
, pnt
,
5033 psize
, addpath_id
)) {
5036 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
5037 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5038 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE
;
5042 case BGP_EVPN_ES_ROUTE
:
5043 if (bgp_evpn_type4_route_process(peer
, afi
, safi
,
5044 withdraw
? NULL
: attr
, pnt
,
5045 psize
, addpath_id
)) {
5048 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
5049 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5050 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE
;
5054 case BGP_EVPN_AD_ROUTE
:
5055 if (bgp_evpn_type1_route_process(peer
, afi
, safi
,
5056 withdraw
? NULL
: attr
, pnt
,
5057 psize
, addpath_id
)) {
5060 "%u:%s - Error in processing EVPN type-1 NLRI size %d",
5061 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5062 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE1_SIZE
;
5066 case BGP_EVPN_IP_PREFIX_ROUTE
:
5067 if (process_type5_route(peer
, afi
, safi
,
5068 withdraw
? NULL
: attr
, pnt
,
5069 psize
, addpath_id
)) {
5072 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
5073 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5074 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE
;
5083 /* Packet length consistency check. */
5085 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH
;
5087 return BGP_NLRI_PARSE_OK
;
5091 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
5092 * The mapping will be used during route processing.
5093 * bgp_vrf: specific bgp vrf instance on which RT is configured
5095 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
5098 struct ecommunity_val
*eval
= NULL
;
5099 struct listnode
*node
= NULL
, *nnode
= NULL
;
5100 struct ecommunity
*ecom
= NULL
;
5102 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5103 for (i
= 0; i
< ecom
->size
; i
++) {
5104 eval
= (struct ecommunity_val
*)(ecom
->val
5106 * ECOMMUNITY_SIZE
));
5107 map_vrf_to_rt(bgp_vrf
, eval
);
5113 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
5115 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
5118 struct ecommunity_val
*eval
;
5119 struct listnode
*node
, *nnode
;
5120 struct ecommunity
*ecom
;
5122 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5123 for (i
= 0; i
< ecom
->size
; i
++) {
5124 struct vrf_irt_node
*irt
;
5125 struct ecommunity_val eval_tmp
;
5127 eval
= (struct ecommunity_val
*)(ecom
->val
5129 * ECOMMUNITY_SIZE
));
5130 /* If using "automatic" RT, we only care about the
5131 * local-admin sub-field.
5132 * This is to facilitate using VNI as the RT for EBGP
5135 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5136 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
5137 BGP_VRF_IMPORT_RT_CFGD
))
5138 mask_ecom_global_admin(&eval_tmp
, eval
);
5140 irt
= lookup_vrf_import_rt(&eval_tmp
);
5142 unmap_vrf_from_rt(bgp_vrf
, irt
);
5149 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
5150 * The mapping will be used during route processing.
5152 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5155 struct ecommunity_val
*eval
;
5156 struct listnode
*node
, *nnode
;
5157 struct ecommunity
*ecom
;
5159 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5160 for (i
= 0; i
< ecom
->size
; i
++) {
5161 eval
= (struct ecommunity_val
*)(ecom
->val
5163 * ECOMMUNITY_SIZE
));
5164 map_vni_to_rt(bgp
, vpn
, eval
);
5170 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
5172 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5175 struct ecommunity_val
*eval
;
5176 struct listnode
*node
, *nnode
;
5177 struct ecommunity
*ecom
;
5179 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5180 for (i
= 0; i
< ecom
->size
; i
++) {
5181 struct irt_node
*irt
;
5182 struct ecommunity_val eval_tmp
;
5184 eval
= (struct ecommunity_val
*)(ecom
->val
5186 * ECOMMUNITY_SIZE
));
5187 /* If using "automatic" RT, we only care about the
5188 * local-admin sub-field.
5189 * This is to facilitate using VNI as the RT for EBGP
5192 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5193 if (!is_import_rt_configured(vpn
))
5194 mask_ecom_global_admin(&eval_tmp
, eval
);
5196 irt
= lookup_import_rt(bgp
, &eval_tmp
);
5198 unmap_vni_from_rt(bgp
, vpn
, irt
);
5204 * Derive Import RT automatically for VNI and map VNI to RT.
5205 * The mapping will be used during route processing.
5207 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5209 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
5210 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
5213 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
5217 * Derive Export RT automatically for VNI.
5219 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5221 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
5222 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5226 * Derive RD automatically for VNI using passed information - it
5227 * is of the form RouterId:unique-id-for-vni.
5229 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5231 if (is_vrf_rd_configured(bgp
))
5234 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5238 * Derive RD automatically for VNI using passed information - it
5239 * is of the form RouterId:unique-id-for-vni.
5241 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5243 char buf
[BGP_EVPN_PREFIX_RD_LEN
];
5245 vpn
->prd
.family
= AF_UNSPEC
;
5246 vpn
->prd
.prefixlen
= 64;
5247 snprintfrr(buf
, sizeof(buf
), "%pI4:%hu", &bgp
->router_id
, vpn
->rd_id
);
5248 (void)str2prefix_rd(buf
, &vpn
->prd
);
5249 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5255 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5257 struct list
*inst
= bm
->bgp
;
5258 struct listnode
*node
;
5259 struct bgp
*bgp_vrf
;
5261 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5262 if (bgp_vrf
->l3vni
== vni
)
5272 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5274 struct bgpevpn
*vpn
;
5277 memset(&tmp
, 0, sizeof(tmp
));
5279 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5284 * Create a new vpn - invoked upon configuration or zebra notification.
5286 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5287 struct in_addr originator_ip
,
5288 vrf_id_t tenant_vrf_id
,
5289 struct in_addr mcast_grp
,
5290 ifindex_t svi_ifindex
)
5292 struct bgpevpn
*vpn
;
5294 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5296 /* Set values - RD and RT set to defaults. */
5298 vpn
->originator_ip
= originator_ip
;
5299 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5300 vpn
->mcast_grp
= mcast_grp
;
5301 vpn
->svi_ifindex
= svi_ifindex
;
5303 /* Initialize route-target import and export lists */
5304 vpn
->import_rtl
= list_new();
5305 vpn
->import_rtl
->cmp
=
5306 (int (*)(void *, void *))bgp_evpn_route_target_cmp
;
5307 vpn
->import_rtl
->del
= bgp_evpn_xxport_delete_ecomm
;
5308 vpn
->export_rtl
= list_new();
5309 vpn
->export_rtl
->cmp
=
5310 (int (*)(void *, void *))bgp_evpn_route_target_cmp
;
5311 vpn
->export_rtl
->del
= bgp_evpn_xxport_delete_ecomm
;
5312 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5313 derive_rd_rt_for_vni(bgp
, vpn
);
5315 /* Initialize EVPN route table. */
5316 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5319 (void)hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
);
5321 bgp_evpn_remote_ip_hash_init(vpn
);
5322 bgp_evpn_link_to_vni_svi_hash(bgp
, vpn
);
5324 /* add to l2vni list on corresponding vrf */
5325 bgpevpn_link_to_l3vni(vpn
);
5327 bgp_evpn_vni_es_init(vpn
);
5329 QOBJ_REG(vpn
, bgpevpn
);
5334 * Free a given VPN - called in multiple scenarios such as zebra
5335 * notification, configuration being deleted, advertise-all-vni disabled etc.
5336 * This just frees appropriate memory, caller should have taken other
5339 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5341 bgp_evpn_remote_ip_hash_destroy(vpn
);
5342 bgp_evpn_vni_es_cleanup(vpn
);
5343 bgpevpn_unlink_from_l3vni(vpn
);
5344 bgp_table_unlock(vpn
->route_table
);
5345 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5346 list_delete(&vpn
->import_rtl
);
5347 list_delete(&vpn
->export_rtl
);
5348 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5349 hash_release(bgp
->vni_svi_hash
, vpn
);
5350 hash_release(bgp
->vnihash
, vpn
);
5352 XFREE(MTYPE_BGP_EVPN
, vpn
);
5356 * Import evpn route from global table to VNI/VRF/ESI.
5358 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5359 const struct prefix
*p
, struct bgp_path_info
*pi
)
5361 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5365 * Unimport evpn route from VNI/VRF/ESI.
5367 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5368 const struct prefix
*p
, struct bgp_path_info
*pi
)
5370 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5373 /* filter routes which have martian next hops */
5374 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5378 struct bgp_dest
*rd_dest
, *dest
;
5379 struct bgp_table
*table
;
5380 struct bgp_path_info
*pi
;
5385 /* Walk entire global routing table and evaluate routes which could be
5386 * imported into this VPN. Note that we cannot just look at the routes
5387 * for the VNI's RD -
5388 * remote routes applicable for this VNI could have any RD.
5390 /* EVPN routes are a 2-level table. */
5391 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
5392 rd_dest
= bgp_route_next(rd_dest
)) {
5393 table
= bgp_dest_get_bgp_table_info(rd_dest
);
5397 for (dest
= bgp_table_top(table
); dest
;
5398 dest
= bgp_route_next(dest
)) {
5400 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
5403 /* Consider "valid" remote routes applicable for
5405 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5406 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5408 if (bgp_nexthop_self(bgp
, afi
, pi
->type
,
5409 pi
->sub_type
, pi
->attr
,
5411 const struct prefix
*p
=
5412 bgp_dest_get_prefix(dest
);
5414 if (bgp_debug_update(pi
->peer
, p
, NULL
,
5416 char attr_str
[BUFSIZ
] = {0};
5418 bgp_dump_attr(pi
->attr
,
5423 "%u: prefix %pBD with attr %s - DENIED due to martian or self nexthop",
5427 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5430 bgp_rib_remove(dest
, pi
, pi
->peer
, afi
,
5441 * Handle del of a local MACIP.
5443 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5444 struct ipaddr
*ip
, int state
)
5446 struct bgpevpn
*vpn
;
5447 struct prefix_evpn p
;
5448 struct bgp_dest
*dest
;
5450 /* Lookup VNI hash - should exist. */
5451 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5452 if (!vpn
|| !is_vni_live(vpn
)) {
5453 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5454 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5455 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5459 build_evpn_type2_prefix(&p
, mac
, ip
);
5460 if (state
== ZEBRA_NEIGH_ACTIVE
) {
5461 /* Remove EVPN type-2 route and schedule for processing. */
5462 delete_evpn_route(bgp
, vpn
, &p
);
5464 /* Re-instate the current remote best path if any */
5465 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
5467 evpn_zebra_reinstall_best_route(bgp
, vpn
, dest
);
5468 bgp_dest_unlock_node(dest
);
5476 * Handle add of a local MACIP.
5478 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5479 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
, esi_t
*esi
)
5481 struct bgpevpn
*vpn
;
5482 struct prefix_evpn p
;
5484 /* Lookup VNI hash - should exist. */
5485 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5486 if (!vpn
|| !is_vni_live(vpn
)) {
5487 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5488 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5489 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5493 /* Create EVPN type-2 route and schedule for processing. */
5494 build_evpn_type2_prefix(&p
, mac
, ip
);
5495 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
, esi
)) {
5497 EC_BGP_EVPN_ROUTE_CREATE
,
5498 "%u:Failed to create Type-2 route, VNI %u %s MAC %pEA IP %pIA (flags: 0x%x)",
5499 bgp
->vrf_id
, vpn
->vni
,
5500 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5510 static void link_l2vni_hash_to_l3vni(struct hash_bucket
*bucket
,
5511 struct bgp
*bgp_vrf
)
5513 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
5514 struct bgp
*bgp_evpn
= NULL
;
5516 bgp_evpn
= bgp_get_evpn();
5519 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5520 bgpevpn_link_to_l3vni(vpn
);
5523 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
,
5524 struct ethaddr
*svi_rmac
,
5525 struct ethaddr
*vrr_rmac
,
5526 struct in_addr originator_ip
, int filter
,
5527 ifindex_t svi_ifindex
,
5528 bool is_anycast_mac
)
5530 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5531 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5532 struct listnode
*node
= NULL
;
5533 struct bgpevpn
*vpn
= NULL
;
5536 /* get the EVPN instance - required to get the AS number for VRF
5539 bgp_evpn
= bgp_get_evpn();
5543 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
5549 /* if the BGP vrf instance doesn't exist - create one */
5550 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5555 ret
= bgp_get_vty(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5556 vrf_id
== VRF_DEFAULT
5557 ? BGP_INSTANCE_TYPE_DEFAULT
5558 : BGP_INSTANCE_TYPE_VRF
);
5560 case BGP_ERR_AS_MISMATCH
:
5561 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5562 "BGP instance is already running; AS is %u",
5565 case BGP_ERR_INSTANCE_MISMATCH
:
5566 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5567 "BGP instance type mismatch");
5571 /* mark as auto created */
5572 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5575 /* associate the vrf with l3vni and related parameters */
5576 bgp_vrf
->l3vni
= l3vni
;
5577 bgp_vrf
->originator_ip
= originator_ip
;
5578 bgp_vrf
->l3vni_svi_ifindex
= svi_ifindex
;
5579 bgp_vrf
->evpn_info
->is_anycast_mac
= is_anycast_mac
;
5581 /* copy anycast MAC from VRR MAC */
5582 memcpy(&bgp_vrf
->rmac
, vrr_rmac
, ETH_ALEN
);
5583 /* copy sys RMAC from SVI MAC */
5584 memcpy(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, svi_rmac
, ETH_ALEN
);
5585 /* PIP user configured mac is not present use svi mac as sys mac */
5586 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
))
5587 memcpy(&bgp_vrf
->evpn_info
->pip_rmac
, svi_rmac
, ETH_ALEN
);
5589 if (bgp_debug_zebra(NULL
))
5591 "VRF %s vni %u pip %s RMAC %pEA sys RMAC %pEA static RMAC %pEA is_anycast_mac %s",
5592 vrf_id_to_name(bgp_vrf
->vrf_id
), bgp_vrf
->l3vni
,
5593 bgp_vrf
->evpn_info
->advertise_pip
? "enable"
5595 &bgp_vrf
->rmac
, &bgp_vrf
->evpn_info
->pip_rmac
,
5596 &bgp_vrf
->evpn_info
->pip_rmac_static
,
5597 is_anycast_mac
? "Enable" : "Disable");
5599 /* set the right filter - are we using l3vni only for prefix routes? */
5601 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5604 * VNI_FLAG_USE_TWO_LABELS flag for linked L2VNIs should not be
5605 * set before linking vrf to L3VNI. Thus, no need to clear
5609 UNSET_FLAG(bgp_vrf
->vrf_flags
,
5610 BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5612 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5613 if (!CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
5616 * If we are flapping VNI_FLAG_USE_TWO_LABELS
5617 * flag, update all MACIP routes in this VNI
5619 SET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5620 update_all_type2_routes(bgp_evpn
, vpn
);
5625 /* Map auto derive or configured RTs */
5626 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5627 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5629 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
5631 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5632 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5634 /* auto derive RD */
5635 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5637 /* link all corresponding l2vnis */
5638 hash_iterate(bgp_evpn
->vnihash
,
5639 (void (*)(struct hash_bucket
*,
5640 void *))link_l2vni_hash_to_l3vni
,
5643 /* Only update all corresponding type-2 routes if we are advertising two
5644 * labels along with type-2 routes
5647 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5648 update_routes_for_vni(bgp_evpn
, vpn
);
5650 /* advertise type-5 routes if needed */
5651 update_advertise_vrf_routes(bgp_vrf
);
5653 /* install all remote routes belonging to this l3vni into correspondng
5655 install_routes_for_vrf(bgp_vrf
);
5660 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5662 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5663 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5664 struct listnode
*node
= NULL
;
5665 struct listnode
*next
= NULL
;
5666 struct bgpevpn
*vpn
= NULL
;
5668 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5672 "Cannot process L3VNI %u Del - Could not find BGP instance",
5677 bgp_evpn
= bgp_get_evpn();
5681 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
5686 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5687 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5688 * routes. This will uninstalling the routes from zebra and decremnt the
5691 uninstall_routes_for_vrf(bgp_vrf
);
5693 /* delete/withdraw all type-5 routes */
5694 delete_withdraw_vrf_routes(bgp_vrf
);
5696 /* remove the l3vni from vrf instance */
5699 /* remove the Rmac from the BGP vrf */
5700 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5701 memset(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, 0, ETH_ALEN
);
5702 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
) &&
5703 !is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac
))
5704 memset(&bgp_vrf
->evpn_info
->pip_rmac
, 0, ETH_ALEN
);
5706 /* remove default import RT or Unmap non-default import RT */
5707 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5708 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5709 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5710 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5713 /* remove default export RT */
5714 if (!list_isempty(bgp_vrf
->vrf_export_rtl
) &&
5715 !CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
5716 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5719 /* update all corresponding local mac-ip routes */
5720 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5721 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5722 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5723 update_routes_for_vni(bgp_evpn
, vpn
);
5727 /* If any L2VNIs point to this instance, unlink them. */
5728 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5729 bgpevpn_unlink_from_l3vni(vpn
);
5731 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5733 /* Delete the instance if it was autocreated */
5734 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5735 bgp_delete(bgp_vrf
);
5741 * Handle del of a local VNI.
5743 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5745 struct bgpevpn
*vpn
;
5747 /* Locate VNI hash */
5748 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5750 if (bgp_debug_zebra(NULL
))
5752 EC_BGP_EVPN_VPN_VNI
,
5753 "%u: VNI hash entry for VNI %u not found at DEL",
5758 /* Remove all local EVPN routes and schedule for processing (to
5759 * withdraw from peers).
5761 delete_routes_for_vni(bgp
, vpn
);
5763 bgp_evpn_unlink_from_vni_svi_hash(bgp
, vpn
);
5765 vpn
->svi_ifindex
= 0;
5767 * tunnel is no longer active, del tunnel ip address from tip_hash
5769 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5771 /* Clear "live" flag and see if hash needs to be freed. */
5772 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5773 if (!is_vni_configured(vpn
))
5774 bgp_evpn_free(bgp
, vpn
);
5780 * Handle add (or update) of a local VNI. The VNI changes we care
5781 * about are for the local-tunnel-ip and the (tenant) VRF.
5783 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5784 struct in_addr originator_ip
,
5785 vrf_id_t tenant_vrf_id
,
5786 struct in_addr mcast_grp
,
5787 ifindex_t svi_ifindex
)
5789 struct bgpevpn
*vpn
;
5790 struct prefix_evpn p
;
5792 /* Lookup VNI. If present and no change, exit. */
5793 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5796 if (is_vni_live(vpn
)
5797 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5798 && IPV4_ADDR_SAME(&vpn
->mcast_grp
, &mcast_grp
)
5799 && vpn
->tenant_vrf_id
== tenant_vrf_id
5800 && vpn
->svi_ifindex
== svi_ifindex
)
5801 /* Probably some other param has changed that we don't
5805 bgp_evpn_mcast_grp_change(bgp
, vpn
, mcast_grp
);
5807 if (vpn
->svi_ifindex
!= svi_ifindex
) {
5810 * Unresolve all the gateway IP nexthops for this VNI
5813 bgp_evpn_remote_ip_hash_iterate(
5815 (void (*)(struct hash_bucket
*, void *))
5816 bgp_evpn_remote_ip_hash_unlink_nexthop
,
5818 bgp_evpn_unlink_from_vni_svi_hash(bgp
, vpn
);
5819 vpn
->svi_ifindex
= svi_ifindex
;
5820 bgp_evpn_link_to_vni_svi_hash(bgp
, vpn
);
5823 * Resolve all the gateway IP nexthops for this VNI
5826 bgp_evpn_remote_ip_hash_iterate(
5828 (void (*)(struct hash_bucket
*, void *))
5829 bgp_evpn_remote_ip_hash_link_nexthop
,
5833 /* Update tenant_vrf_id if it has changed. */
5834 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5837 * Unresolve all the gateway IP nexthops for this VNI
5840 bgp_evpn_remote_ip_hash_iterate(
5842 (void (*)(struct hash_bucket
*, void *))
5843 bgp_evpn_remote_ip_hash_unlink_nexthop
,
5845 bgpevpn_unlink_from_l3vni(vpn
);
5846 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5847 bgpevpn_link_to_l3vni(vpn
);
5850 * Resolve all the gateway IP nexthops for this VNI
5853 bgp_evpn_remote_ip_hash_iterate(
5855 (void (*)(struct hash_bucket
*, void *))
5856 bgp_evpn_remote_ip_hash_link_nexthop
,
5860 /* If tunnel endpoint IP has changed, update (and delete prior
5861 * type-3 route, if needed.)
5863 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5864 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5866 /* Update all routes with new endpoint IP and/or export RT
5869 if (is_vni_live(vpn
))
5870 update_routes_for_vni(bgp
, vpn
);
5872 /* Create or update as appropriate. */
5873 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
,
5874 mcast_grp
, svi_ifindex
);
5877 /* if the VNI is live already, there is nothing more to do */
5878 if (is_vni_live(vpn
))
5881 /* Mark as "live" */
5882 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5884 /* tunnel is now active, add tunnel-ip to db */
5885 bgp_tip_add(bgp
, &originator_ip
);
5887 /* filter routes as nexthop database has changed */
5888 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5891 * Create EVPN type-3 route and schedule for processing.
5893 * RT-3 only if doing head-end replication
5895 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
5896 == VXLAN_FLOOD_HEAD_END_REPL
) {
5897 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5898 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
)) {
5899 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5900 "%u: Type3 route creation failure for VNI %u",
5906 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5910 install_routes_for_vni(bgp
, vpn
);
5912 /* If we are advertising gateway mac-ip
5913 It needs to be conveyed again to zebra */
5914 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5916 /* advertise svi mac-ip knob to zebra */
5917 bgp_zebra_advertise_svi_macip(bgp
, vpn
->advertise_svi_macip
, vpn
->vni
);
5923 * Handle change in setting for BUM handling. The supported values
5924 * are head-end replication and dropping all BUM packets. Any change
5925 * should be registered with zebra. Also, if doing head-end replication,
5926 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5927 * to be dropped, the RT-3s must be withdrawn.
5929 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
5931 zlog_info("L2VPN EVPN BUM handling is %s",
5932 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
5933 "Flooding" : "Flooding Disabled");
5935 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
5936 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
5937 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
5938 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
5939 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
5943 * Cleanup EVPN information on disable - Need to delete and withdraw
5944 * EVPN routes from peers.
5946 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5948 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_bucket
*,
5949 void *))cleanup_vni_on_disable
,
5954 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5955 * BGP instance (default) is being freed.
5957 void bgp_evpn_cleanup(struct bgp
*bgp
)
5959 hash_iterate(bgp
->vnihash
,
5960 (void (*)(struct hash_bucket
*, void *))free_vni_entry
,
5963 hash_free(bgp
->import_rt_hash
);
5964 bgp
->import_rt_hash
= NULL
;
5966 hash_free(bgp
->vrf_import_rt_hash
);
5967 bgp
->vrf_import_rt_hash
= NULL
;
5969 hash_free(bgp
->vni_svi_hash
);
5970 bgp
->vni_svi_hash
= NULL
;
5971 hash_free(bgp
->vnihash
);
5972 bgp
->vnihash
= NULL
;
5974 list_delete(&bgp
->vrf_import_rtl
);
5975 list_delete(&bgp
->vrf_export_rtl
);
5976 list_delete(&bgp
->l2vnis
);
5980 * Initialization for EVPN
5983 * hash for RT to VNI
5985 void bgp_evpn_init(struct bgp
*bgp
)
5988 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
5990 hash_create(vni_svi_hash_key_make
, vni_svi_hash_cmp
,
5991 "BGP VNI hash based on SVI ifindex");
5992 bgp
->import_rt_hash
=
5993 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
5994 "BGP Import RT Hash");
5995 bgp
->vrf_import_rt_hash
=
5996 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
5997 "BGP VRF Import RT Hash");
5998 bgp
->vrf_import_rtl
= list_new();
5999 bgp
->vrf_import_rtl
->cmp
=
6000 (int (*)(void *, void *))bgp_evpn_route_target_cmp
;
6001 bgp
->vrf_import_rtl
->del
= bgp_evpn_xxport_delete_ecomm
;
6002 bgp
->vrf_export_rtl
= list_new();
6003 bgp
->vrf_export_rtl
->cmp
=
6004 (int (*)(void *, void *))bgp_evpn_route_target_cmp
;
6005 bgp
->vrf_export_rtl
->del
= bgp_evpn_xxport_delete_ecomm
;
6006 bgp
->l2vnis
= list_new();
6007 bgp
->l2vnis
->cmp
= vni_list_cmp
;
6008 /* By default Duplicate Address Dection is enabled.
6009 * Max-moves (N) 5, detection time (M) 180
6010 * default action is warning-only
6011 * freeze action permanently freezes address,
6012 * and freeze time (auto-recovery) is disabled.
6014 if (bgp
->evpn_info
) {
6015 bgp
->evpn_info
->dup_addr_detect
= true;
6016 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
6017 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
6018 bgp
->evpn_info
->dad_freeze
= false;
6019 bgp
->evpn_info
->dad_freeze_time
= 0;
6020 /* Initialize zebra vxlan */
6021 bgp_zebra_dup_addr_detection(bgp
);
6022 /* Enable PIP feature by default for bgp vrf instance */
6023 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
6024 struct bgp
*bgp_default
;
6026 bgp
->evpn_info
->advertise_pip
= true;
6027 bgp_default
= bgp_get_default();
6029 bgp
->evpn_info
->pip_ip
= bgp_default
->router_id
;
6033 /* Default BUM handling is to do head-end replication. */
6034 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
6036 bgp_evpn_nh_init(bgp
);
6039 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
6041 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
6042 bgp_evpn_nh_finish(bgp_vrf
);
6046 * Get the prefixlen of the ip prefix carried within the type5 evpn route.
6048 int bgp_evpn_get_type5_prefixlen(const struct prefix
*pfx
)
6050 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6052 if (!pfx
|| pfx
->family
!= AF_EVPN
)
6055 if (evp
->prefix
.route_type
!= BGP_EVPN_IP_PREFIX_ROUTE
)
6058 return evp
->prefix
.prefix_addr
.ip_prefix_length
;
6062 * Should we register nexthop for this EVPN prefix for nexthop tracking?
6064 bool bgp_evpn_is_prefix_nht_supported(const struct prefix
*pfx
)
6066 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6069 * EVPN routes should be marked as valid only if the nexthop is
6070 * reachable. Only if this happens, the route should be imported
6071 * (into VNI or VRF routing tables) and/or advertised.
6072 * Note: This is currently applied for EVPN type-1, type-2,
6073 * type-3, type-4 and type-5 routes.
6074 * It may be tweaked later on for other routes, or
6075 * even removed completely when all routes are handled.
6077 if (pfx
&& pfx
->family
== AF_EVPN
6078 && (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
6079 || evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
6080 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
6081 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
6082 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
6088 static void *bgp_evpn_remote_ip_hash_alloc(void *p
)
6090 const struct evpn_remote_ip
*key
= (const struct evpn_remote_ip
*)p
;
6091 struct evpn_remote_ip
*ip
;
6093 ip
= XMALLOC(MTYPE_EVPN_REMOTE_IP
, sizeof(struct evpn_remote_ip
));
6095 ip
->macip_path_list
= list_new();
6100 static unsigned int bgp_evpn_remote_ip_hash_key_make(const void *p
)
6102 const struct evpn_remote_ip
*ip
= p
;
6103 const struct ipaddr
*addr
= &ip
->addr
;
6105 if (IS_IPADDR_V4(addr
))
6106 return jhash_1word(addr
->ipaddr_v4
.s_addr
, 0);
6108 return jhash2(addr
->ipaddr_v6
.s6_addr32
,
6109 array_size(addr
->ipaddr_v6
.s6_addr32
), 0);
6112 static bool bgp_evpn_remote_ip_hash_cmp(const void *p1
, const void *p2
)
6114 const struct evpn_remote_ip
*ip1
= p1
;
6115 const struct evpn_remote_ip
*ip2
= p2
;
6117 return !ipaddr_cmp(&ip1
->addr
, &ip2
->addr
);
6120 static void bgp_evpn_remote_ip_hash_init(struct bgpevpn
*vpn
)
6122 if (!evpn_resolve_overlay_index())
6125 vpn
->remote_ip_hash
= hash_create(bgp_evpn_remote_ip_hash_key_make
,
6126 bgp_evpn_remote_ip_hash_cmp
,
6127 "BGP EVPN remote IP hash");
6130 static void bgp_evpn_remote_ip_hash_free(struct hash_bucket
*bucket
, void *args
)
6132 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6133 struct bgpevpn
*vpn
= (struct bgpevpn
*)args
;
6135 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, false);
6137 list_delete(&ip
->macip_path_list
);
6139 hash_release(vpn
->remote_ip_hash
, ip
);
6140 XFREE(MTYPE_EVPN_REMOTE_IP
, ip
);
6143 static void bgp_evpn_remote_ip_hash_destroy(struct bgpevpn
*vpn
)
6145 if (!evpn_resolve_overlay_index() || vpn
->remote_ip_hash
== NULL
)
6148 hash_iterate(vpn
->remote_ip_hash
,
6149 (void (*)(struct hash_bucket
*, void *))bgp_evpn_remote_ip_hash_free
,
6152 hash_free(vpn
->remote_ip_hash
);
6153 vpn
->remote_ip_hash
= NULL
;
6156 /* Add a remote MAC/IP route to hash table */
6157 static void bgp_evpn_remote_ip_hash_add(struct bgpevpn
*vpn
,
6158 struct bgp_path_info
*pi
)
6160 struct evpn_remote_ip tmp
;
6161 struct evpn_remote_ip
*ip
;
6162 struct prefix_evpn
*evp
;
6164 if (!evpn_resolve_overlay_index())
6167 if (pi
->type
!= ZEBRA_ROUTE_BGP
|| pi
->sub_type
!= BGP_ROUTE_IMPORTED
6168 || !CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
))
6171 evp
= (struct prefix_evpn
*)&pi
->net
->p
;
6173 if (evp
->family
!= AF_EVPN
6174 || evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
6175 || is_evpn_prefix_ipaddr_none(evp
))
6178 tmp
.addr
= evp
->prefix
.macip_addr
.ip
;
6179 ip
= hash_lookup(vpn
->remote_ip_hash
, &tmp
);
6181 if (listnode_lookup(ip
->macip_path_list
, pi
) != NULL
)
6183 (void)listnode_add(ip
->macip_path_list
, pi
);
6187 ip
= hash_get(vpn
->remote_ip_hash
, &tmp
, bgp_evpn_remote_ip_hash_alloc
);
6188 (void)listnode_add(ip
->macip_path_list
, pi
);
6190 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, true);
6193 /* Delete a remote MAC/IP route from hash table */
6194 static void bgp_evpn_remote_ip_hash_del(struct bgpevpn
*vpn
,
6195 struct bgp_path_info
*pi
)
6197 struct evpn_remote_ip tmp
;
6198 struct evpn_remote_ip
*ip
;
6199 struct prefix_evpn
*evp
;
6201 if (!evpn_resolve_overlay_index())
6204 evp
= (struct prefix_evpn
*)&pi
->net
->p
;
6206 if (evp
->family
!= AF_EVPN
6207 || evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
6208 || is_evpn_prefix_ipaddr_none(evp
))
6211 tmp
.addr
= evp
->prefix
.macip_addr
.ip
;
6212 ip
= hash_lookup(vpn
->remote_ip_hash
, &tmp
);
6216 listnode_delete(ip
->macip_path_list
, pi
);
6218 if (ip
->macip_path_list
->count
== 0) {
6219 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, false);
6220 hash_release(vpn
->remote_ip_hash
, ip
);
6221 XFREE(MTYPE_EVPN_REMOTE_IP
, ip
);
6225 static void bgp_evpn_remote_ip_hash_iterate(struct bgpevpn
*vpn
,
6226 void (*func
)(struct hash_bucket
*,
6230 if (!evpn_resolve_overlay_index())
6233 hash_iterate(vpn
->remote_ip_hash
, func
, arg
);
6236 static void show_remote_ip_entry(struct hash_bucket
*bucket
, void *args
)
6238 char buf
[INET6_ADDRSTRLEN
];
6239 char buf2
[EVPN_ROUTE_STRLEN
];
6240 struct prefix_evpn
*evp
;
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 evp
= (struct prefix_evpn
*)&pi
->net
->p
;
6252 prefix2str(evp
, buf2
, sizeof(buf2
));
6253 vty_out(vty
, " %s\n", buf2
);
6257 void bgp_evpn_show_remote_ip_hash(struct hash_bucket
*bucket
, void *args
)
6259 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
6260 struct vty
*vty
= (struct vty
*)args
;
6262 vty_out(vty
, "VNI: %u\n", vpn
->vni
);
6263 bgp_evpn_remote_ip_hash_iterate(
6265 (void (*)(struct hash_bucket
*, void *))show_remote_ip_entry
,
6270 static void bgp_evpn_remote_ip_hash_link_nexthop(struct hash_bucket
*bucket
,
6273 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6274 struct bgpevpn
*vpn
= (struct bgpevpn
*)args
;
6276 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, true);
6279 static void bgp_evpn_remote_ip_hash_unlink_nexthop(struct hash_bucket
*bucket
,
6282 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6283 struct bgpevpn
*vpn
= (struct bgpevpn
*)args
;
6285 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, false);
6288 static unsigned int vni_svi_hash_key_make(const void *p
)
6290 const struct bgpevpn
*vpn
= p
;
6292 return jhash_1word(vpn
->svi_ifindex
, 0);
6295 static bool vni_svi_hash_cmp(const void *p1
, const void *p2
)
6297 const struct bgpevpn
*vpn1
= p1
;
6298 const struct bgpevpn
*vpn2
= p2
;
6300 return (vpn1
->svi_ifindex
== vpn2
->svi_ifindex
);
6303 static struct bgpevpn
*bgp_evpn_vni_svi_hash_lookup(struct bgp
*bgp
,
6306 struct bgpevpn
*vpn
;
6309 memset(&tmp
, 0, sizeof(tmp
));
6310 tmp
.svi_ifindex
= svi
;
6311 vpn
= hash_lookup(bgp
->vni_svi_hash
, &tmp
);
6315 static void bgp_evpn_link_to_vni_svi_hash(struct bgp
*bgp
, struct bgpevpn
*vpn
)
6317 if (vpn
->svi_ifindex
== 0)
6320 (void)hash_get(bgp
->vni_svi_hash
, vpn
, hash_alloc_intern
);
6323 static void bgp_evpn_unlink_from_vni_svi_hash(struct bgp
*bgp
,
6324 struct bgpevpn
*vpn
)
6326 if (vpn
->svi_ifindex
== 0)
6329 hash_release(bgp
->vni_svi_hash
, vpn
);
6332 void bgp_evpn_show_vni_svi_hash(struct hash_bucket
*bucket
, void *args
)
6334 struct bgpevpn
*evpn
= (struct bgpevpn
*)bucket
->data
;
6335 struct vty
*vty
= (struct vty
*)args
;
6337 vty_out(vty
, "SVI: %u VNI: %u\n", evpn
->svi_ifindex
, evpn
->vni
);
6341 * This function is called for a bgp_nexthop_cache entry when the nexthop is
6342 * gateway IP overlay index.
6343 * This function returns true if there is a remote MAC/IP route for the gateway
6344 * IP in the EVI of the nexthop SVI.
6346 bool bgp_evpn_is_gateway_ip_resolved(struct bgp_nexthop_cache
*bnc
)
6348 struct bgp
*bgp_evpn
= NULL
;
6349 struct bgpevpn
*vpn
= NULL
;
6350 struct evpn_remote_ip tmp
;
6353 if (!evpn_resolve_overlay_index())
6356 if (!bnc
->nexthop
|| bnc
->nexthop
->ifindex
== 0)
6359 bgp_evpn
= bgp_get_evpn();
6364 * Gateway IP is resolved by nht over SVI interface.
6365 * Use this SVI to find corresponding EVI(L2 context)
6367 vpn
= bgp_evpn_vni_svi_hash_lookup(bgp_evpn
, bnc
->nexthop
->ifindex
);
6371 if (vpn
->bgp_vrf
!= bnc
->bgp
)
6375 * Check if the gateway IP is present in the EVI remote_ip_hash table
6376 * which stores all the remote IP addresses received via MAC/IP routes
6379 memset(&tmp
, 0, sizeof(tmp
));
6382 if (p
->family
== AF_INET
) {
6383 tmp
.addr
.ipa_type
= IPADDR_V4
;
6384 memcpy(&(tmp
.addr
.ipaddr_v4
), &(p
->u
.prefix4
),
6385 sizeof(struct in_addr
));
6386 } else if (p
->family
== AF_INET6
) {
6387 tmp
.addr
.ipa_type
= IPADDR_V6
;
6388 memcpy(&(tmp
.addr
.ipaddr_v6
), &(p
->u
.prefix6
),
6389 sizeof(struct in6_addr
));
6393 if (hash_lookup(vpn
->remote_ip_hash
, &tmp
) == NULL
)
6399 /* Resolve/Unresolve nexthops when a MAC/IP route is added/deleted */
6400 static void bgp_evpn_remote_ip_process_nexthops(struct bgpevpn
*vpn
,
6401 struct ipaddr
*addr
,
6406 struct bgp_nexthop_cache
*bnc
;
6407 struct bgp_nexthop_cache_head
*tree
= NULL
;
6409 if (!vpn
->bgp_vrf
|| vpn
->svi_ifindex
== 0)
6412 memset(&p
, 0, sizeof(p
));
6414 if (addr
->ipa_type
== IPADDR_V4
) {
6417 memcpy(&(p
.u
.prefix4
), &(addr
->ipaddr_v4
),
6418 sizeof(struct in_addr
));
6419 p
.prefixlen
= IPV4_MAX_BITLEN
;
6420 } else if (addr
->ipa_type
== IPADDR_V6
) {
6422 p
.family
= AF_INET6
;
6423 memcpy(&(p
.u
.prefix6
), &(addr
->ipaddr_v6
),
6424 sizeof(struct in6_addr
));
6425 p
.prefixlen
= IPV6_MAX_BITLEN
;
6429 tree
= &vpn
->bgp_vrf
->nexthop_cache_table
[afi
];
6430 bnc
= bnc_find(tree
, &p
, 0);
6432 if (!bnc
|| !bnc
->is_evpn_gwip_nexthop
)
6435 if (!bnc
->nexthop
|| bnc
->nexthop
->ifindex
!= vpn
->svi_ifindex
)
6438 if (BGP_DEBUG(nht
, NHT
)) {
6439 char buf
[PREFIX2STR_BUFFER
];
6441 prefix2str(&bnc
->prefix
, buf
, sizeof(buf
));
6442 zlog_debug("%s(%u): vni %u mac/ip %s for NH %s",
6443 vpn
->bgp_vrf
->name_pretty
, vpn
->tenant_vrf_id
,
6444 vpn
->vni
, (resolve
? "add" : "delete"), buf
);
6448 * MAC/IP route or SVI or tenant vrf being added to EVI.
6449 * Set nexthop as valid only if it is already L3 reachable
6451 if (resolve
&& bnc
->flags
& BGP_NEXTHOP_EVPN_INCOMPLETE
) {
6452 bnc
->flags
&= ~BGP_NEXTHOP_EVPN_INCOMPLETE
;
6453 bnc
->flags
|= BGP_NEXTHOP_VALID
;
6454 bnc
->change_flags
|= BGP_NEXTHOP_MACIP_CHANGED
;
6455 evaluate_paths(bnc
);
6458 /* MAC/IP route or SVI or tenant vrf being deleted from EVI */
6459 if (!resolve
&& bnc
->flags
& BGP_NEXTHOP_VALID
) {
6460 bnc
->flags
&= ~BGP_NEXTHOP_VALID
;
6461 bnc
->flags
|= BGP_NEXTHOP_EVPN_INCOMPLETE
;
6462 bnc
->change_flags
|= BGP_NEXTHOP_MACIP_CHANGED
;
6463 evaluate_paths(bnc
);
6467 void bgp_evpn_handle_resolve_overlay_index_set(struct hash_bucket
*bucket
,
6470 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
6471 struct bgp_dest
*dest
;
6472 struct bgp_path_info
*pi
;
6474 bgp_evpn_remote_ip_hash_init(vpn
);
6476 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
6477 dest
= bgp_route_next(dest
))
6478 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
6479 bgp_evpn_remote_ip_hash_add(vpn
, pi
);
6482 void bgp_evpn_handle_resolve_overlay_index_unset(struct hash_bucket
*bucket
,
6485 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
6487 bgp_evpn_remote_ip_hash_destroy(vpn
);