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 "bgpd/bgp_attr_evpn.h"
35 #include "bgpd/bgpd.h"
36 #include "bgpd/bgp_table.h"
37 #include "bgpd/bgp_route.h"
38 #include "bgpd/bgp_attr.h"
39 #include "bgpd/bgp_mplsvpn.h"
40 #include "bgpd/bgp_label.h"
41 #include "bgpd/bgp_evpn.h"
42 #include "bgpd/bgp_evpn_private.h"
43 #include "bgpd/bgp_ecommunity.h"
44 #include "bgpd/bgp_encap_types.h"
45 #include "bgpd/bgp_debug.h"
46 #include "bgpd/bgp_errors.h"
47 #include "bgpd/bgp_aspath.h"
48 #include "bgpd/bgp_zebra.h"
49 #include "bgpd/bgp_nexthop.h"
50 #include "bgpd/bgp_addpath.h"
51 #include "bgpd/bgp_mac.h"
54 * Definitions and external declarations.
56 extern struct zclient
*zclient
;
58 DEFINE_QOBJ_TYPE(bgpevpn
)
59 DEFINE_QOBJ_TYPE(evpnes
)
63 * Static function declarations
65 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
67 struct bgp_path_info
**pi
);
68 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
74 /* compare two IPV4 VTEP IPs */
75 static int evpn_vtep_ip_cmp(void *p1
, void *p2
)
77 const struct in_addr
*ip1
= p1
;
78 const struct in_addr
*ip2
= p2
;
80 return ip1
->s_addr
- ip2
->s_addr
;
84 * Make hash key for ESI.
86 static unsigned int esi_hash_keymake(const void *p
)
88 const struct evpnes
*pes
= p
;
89 const void *pnt
= (void *)pes
->esi
.val
;
91 return jhash(pnt
, ESI_BYTES
, 0xa5a5a55a);
97 static bool esi_cmp(const void *p1
, const void *p2
)
99 const struct evpnes
*pes1
= p1
;
100 const struct evpnes
*pes2
= p2
;
102 if (pes1
== NULL
&& pes2
== NULL
)
105 if (pes1
== NULL
|| pes2
== NULL
)
108 return (memcmp(pes1
->esi
.val
, pes2
->esi
.val
, ESI_BYTES
) == 0);
114 static unsigned int vni_hash_key_make(const void *p
)
116 const struct bgpevpn
*vpn
= p
;
117 return (jhash_1word(vpn
->vni
, 0));
121 * Comparison function for vni hash
123 static bool vni_hash_cmp(const void *p1
, const void *p2
)
125 const struct bgpevpn
*vpn1
= p1
;
126 const struct bgpevpn
*vpn2
= p2
;
132 return (vpn1
->vni
== vpn2
->vni
);
135 static int vni_list_cmp(void *p1
, void *p2
)
137 const struct bgpevpn
*vpn1
= p1
;
138 const struct bgpevpn
*vpn2
= p2
;
140 return vpn1
->vni
- vpn2
->vni
;
144 * Make vrf import route target hash key.
146 static unsigned int vrf_import_rt_hash_key_make(const void *p
)
148 const struct vrf_irt_node
*irt
= p
;
149 const char *pnt
= irt
->rt
.val
;
151 return jhash(pnt
, 8, 0x5abc1234);
155 * Comparison function for vrf import rt hash
157 static bool vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
159 const struct vrf_irt_node
*irt1
= p1
;
160 const struct vrf_irt_node
*irt2
= p2
;
162 if (irt1
== NULL
&& irt2
== NULL
)
165 if (irt1
== NULL
|| irt2
== NULL
)
168 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
172 * Create a new vrf import_rt in evpn instance
174 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
176 struct bgp
*bgp_evpn
= NULL
;
177 struct vrf_irt_node
*irt
;
179 bgp_evpn
= bgp_get_evpn();
181 flog_err(EC_BGP_NO_DFLT
,
182 "vrf import rt new - evpn instance not created yet");
186 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
187 sizeof(struct vrf_irt_node
));
190 irt
->vrfs
= list_new();
193 if (!hash_get(bgp_evpn
->vrf_import_rt_hash
, irt
, hash_alloc_intern
)) {
194 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
202 * Free the vrf import rt node
204 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
206 struct bgp
*bgp_evpn
= NULL
;
208 bgp_evpn
= bgp_get_evpn();
210 flog_err(EC_BGP_NO_DFLT
,
211 "vrf import rt free - evpn instance not created yet");
215 hash_release(bgp_evpn
->vrf_import_rt_hash
, irt
);
216 list_delete(&irt
->vrfs
);
217 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
221 * Function to lookup Import RT node - used to map a RT to set of
222 * VNIs importing routes with that RT.
224 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
226 struct bgp
*bgp_evpn
= NULL
;
227 struct vrf_irt_node
*irt
;
228 struct vrf_irt_node tmp
;
230 bgp_evpn
= bgp_get_evpn();
234 "vrf import rt lookup - evpn instance not created yet");
238 memset(&tmp
, 0, sizeof(struct vrf_irt_node
));
239 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
240 irt
= hash_lookup(bgp_evpn
->vrf_import_rt_hash
, &tmp
);
245 * Is specified VRF present on the RT's list of "importing" VRFs?
247 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
, struct bgp
*bgp_vrf
)
249 struct listnode
*node
= NULL
, *nnode
= NULL
;
250 struct bgp
*tmp_bgp_vrf
= NULL
;
252 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
253 if (tmp_bgp_vrf
== bgp_vrf
)
260 * Make import route target hash key.
262 static unsigned int import_rt_hash_key_make(const void *p
)
264 const struct irt_node
*irt
= p
;
265 const char *pnt
= irt
->rt
.val
;
267 return jhash(pnt
, 8, 0xdeadbeef);
271 * Comparison function for import rt hash
273 static bool import_rt_hash_cmp(const void *p1
, const void *p2
)
275 const struct irt_node
*irt1
= p1
;
276 const struct irt_node
*irt2
= p2
;
278 if (irt1
== NULL
&& irt2
== NULL
)
281 if (irt1
== NULL
|| irt2
== NULL
)
284 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
288 * Create a new import_rt
290 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
291 struct ecommunity_val
*rt
)
293 struct irt_node
*irt
;
298 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
301 irt
->vnis
= list_new();
304 if (!hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
)) {
305 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
313 * Free the import rt node
315 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
317 hash_release(bgp
->import_rt_hash
, irt
);
318 list_delete(&irt
->vnis
);
319 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
323 * Function to lookup Import RT node - used to map a RT to set of
324 * VNIs importing routes with that RT.
326 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
327 struct ecommunity_val
*rt
)
329 struct irt_node
*irt
;
332 memset(&tmp
, 0, sizeof(struct irt_node
));
333 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
334 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
339 * Is specified VNI present on the RT's list of "importing" VNIs?
341 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
343 struct listnode
*node
, *nnode
;
344 struct bgpevpn
*tmp_vpn
;
346 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
355 * Compare Route Targets.
357 static int evpn_route_target_cmp(struct ecommunity
*ecom1
,
358 struct ecommunity
*ecom2
)
366 if (!ecom1
&& !ecom2
)
369 if (ecom1
->str
&& !ecom2
->str
)
372 if (!ecom1
->str
&& ecom2
->str
)
375 if (!ecom1
->str
&& !ecom2
->str
)
378 return strcmp(ecom1
->str
, ecom2
->str
);
381 static void evpn_xxport_delete_ecomm(void *val
)
383 struct ecommunity
*ecomm
= val
;
384 ecommunity_free(&ecomm
);
388 * Mask off global-admin field of specified extended community (RT),
389 * just retain the local-admin field.
391 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
392 struct ecommunity_val
*src
)
398 if (type
== ECOMMUNITY_ENCODE_AS
) {
399 dst
->val
[2] = dst
->val
[3] = 0;
400 } else if (type
== ECOMMUNITY_ENCODE_AS4
401 || type
== ECOMMUNITY_ENCODE_IP
) {
402 dst
->val
[2] = dst
->val
[3] = 0;
403 dst
->val
[4] = dst
->val
[5] = 0;
408 * Map one RT to specified VRF.
409 * bgp_vrf = BGP vrf instance
411 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct ecommunity_val
*eval
)
413 struct vrf_irt_node
*irt
= NULL
;
414 struct ecommunity_val eval_tmp
;
416 /* If using "automatic" RT,
417 * we only care about the local-admin sub-field.
418 * This is to facilitate using L3VNI(VRF-VNI)
419 * as the RT for EBGP peering too.
421 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
422 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
423 mask_ecom_global_admin(&eval_tmp
, eval
);
425 irt
= lookup_vrf_import_rt(&eval_tmp
);
426 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
427 /* Already mapped. */
431 irt
= vrf_import_rt_new(&eval_tmp
);
433 /* Add VRF to the list for this RT. */
434 listnode_add(irt
->vrfs
, bgp_vrf
);
438 * Unmap specified VRF from specified RT. If there are no other
439 * VRFs for this RT, then the RT hash is deleted.
440 * bgp_vrf: BGP VRF specific instance
442 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
, struct vrf_irt_node
*irt
)
444 /* Delete VRF from list for this RT. */
445 listnode_delete(irt
->vrfs
, bgp_vrf
);
446 if (!listnode_head(irt
->vrfs
)) {
447 vrf_import_rt_free(irt
);
452 * Map one RT to specified VNI.
454 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
455 struct ecommunity_val
*eval
)
457 struct irt_node
*irt
;
458 struct ecommunity_val eval_tmp
;
460 /* If using "automatic" RT, we only care about the local-admin
462 * This is to facilitate using VNI as the RT for EBGP peering too.
464 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
465 if (!is_import_rt_configured(vpn
))
466 mask_ecom_global_admin(&eval_tmp
, eval
);
468 irt
= lookup_import_rt(bgp
, &eval_tmp
);
470 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
471 /* Already mapped. */
475 irt
= import_rt_new(bgp
, &eval_tmp
);
479 /* Add VNI to the hash list for this RT. */
480 listnode_add(irt
->vnis
, vpn
);
484 * Unmap specified VNI from specified RT. If there are no other
485 * VNIs for this RT, then the RT hash is deleted.
487 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
488 struct irt_node
*irt
)
490 /* Delete VNI from hash list for this RT. */
491 listnode_delete(irt
->vnis
, vpn
);
492 if (!listnode_head(irt
->vnis
)) {
493 import_rt_free(bgp
, irt
);
497 static void bgp_evpn_get_rmac_nexthop(struct bgpevpn
*vpn
,
498 struct prefix_evpn
*p
,
499 struct attr
*attr
, uint8_t flags
)
501 struct bgp
*bgp_vrf
= vpn
->bgp_vrf
;
503 memset(&attr
->rmac
, 0, sizeof(struct ethaddr
));
507 if (p
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
510 /* Copy sys (pip) RMAC and PIP IP as nexthop
511 * in case of route is self MAC-IP,
512 * advertise-pip and advertise-svi-ip features
514 * Otherwise, for all host MAC-IP route's
517 if (CHECK_FLAG(flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
)
518 && bgp_vrf
->evpn_info
->advertise_pip
&&
519 bgp_vrf
->evpn_info
->is_anycast_mac
) {
521 memcpy(&attr
->rmac
, &bgp_vrf
->evpn_info
->pip_rmac
,
523 attr
->nexthop
= bgp_vrf
->evpn_info
->pip_ip
;
524 attr
->mp_nexthop_global_in
=
525 bgp_vrf
->evpn_info
->pip_ip
;
527 memcpy(&attr
->rmac
, &bgp_vrf
->rmac
, ETH_ALEN
);
531 * Create RT extended community automatically from passed information:
532 * of the form AS:VNI.
533 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
534 * the need is to get a RT value that will be unique across different
535 * VNIs but the same across routers (in the same AS) for a particular
538 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
540 struct ecommunity_val eval
;
541 struct ecommunity
*ecomadd
, *ecom
;
542 bool ecom_found
= false;
543 struct listnode
*node
;
545 if (bgp
->advertise_autort_rfc8365
)
546 vni
|= EVPN_AUTORT_VXLAN
;
547 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
549 ecomadd
= ecommunity_new();
550 ecommunity_add_val(ecomadd
, &eval
);
551 for (ALL_LIST_ELEMENTS_RO(rtl
, node
, ecom
))
552 if (ecommunity_cmp(ecomadd
, ecom
))
556 listnode_add_sort(rtl
, ecomadd
);
560 * Derive RD and RT for a VNI automatically. Invoked at the time of
563 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
565 bgp_evpn_derive_auto_rd(bgp
, vpn
);
566 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
567 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
571 * Convert nexthop (remote VTEP IP) into an IPv6 address.
573 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
575 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
577 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
578 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
582 * Add (update) or delete MACIP from zebra.
584 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
585 struct prefix_evpn
*p
,
586 struct in_addr remote_vtep_ip
, int add
,
587 uint8_t flags
, uint32_t seq
)
591 char buf1
[ETHER_ADDR_STRLEN
];
592 char buf2
[INET6_ADDRSTRLEN
];
593 char buf3
[INET6_ADDRSTRLEN
];
596 if (!zclient
|| zclient
->sock
< 0)
599 /* Don't try to register if Zebra doesn't know of this instance. */
600 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
601 if (BGP_DEBUG(zebra
, ZEBRA
))
602 zlog_debug("%s: No zebra instance to talk to, not installing remote macip",
603 __PRETTY_FUNCTION__
);
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_putl(s
, ipa_len
);
621 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
623 stream_put_in_addr(s
, &remote_vtep_ip
);
625 /* TX flags - MAC sticky status and/or gateway mac */
626 /* Also TX the sequence number of the best route. */
628 stream_putc(s
, flags
);
632 stream_putw_at(s
, 0, stream_get_endp(s
));
634 if (bgp_debug_zebra(NULL
))
636 "Tx %s MACIP, VNI %u MAC %s IP %s flags 0x%x seq %u remote VTEP %s",
637 add
? "ADD" : "DEL", vpn
->vni
,
638 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
640 ipaddr2str(&p
->prefix
.macip_addr
.ip
,
641 buf3
, sizeof(buf3
)), flags
, seq
,
642 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
645 return zclient_send_message(zclient
);
649 * Add (update) or delete remote VTEP from zebra.
651 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
652 struct prefix_evpn
*p
,
653 int flood_control
, int add
)
658 if (!zclient
|| zclient
->sock
< 0)
661 /* Don't try to register if Zebra doesn't know of this instance. */
662 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
663 if (BGP_DEBUG(zebra
, ZEBRA
))
664 zlog_debug("%s: No zebra instance to talk to, not installing remote vtep",
665 __PRETTY_FUNCTION__
);
672 zclient_create_header(
673 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
675 stream_putl(s
, vpn
->vni
);
676 if (is_evpn_prefix_ipaddr_v4(p
))
677 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
678 else if (is_evpn_prefix_ipaddr_v6(p
)) {
681 "Bad remote IP when trying to %s remote VTEP for VNI %u",
682 add
? "ADD" : "DEL", vpn
->vni
);
685 stream_putl(s
, flood_control
);
687 stream_putw_at(s
, 0, stream_get_endp(s
));
689 if (bgp_debug_zebra(NULL
))
690 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
691 add
? "ADD" : "DEL", vpn
->vni
,
692 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
694 return zclient_send_message(zclient
);
698 * Build extended community for EVPN ES (type-4) route
700 static void build_evpn_type4_route_extcomm(struct evpnes
*es
,
703 struct ecommunity ecom_encap
;
704 struct ecommunity ecom_es_rt
;
705 struct ecommunity_val eval
;
706 struct ecommunity_val eval_es_rt
;
707 bgp_encap_types tnl_type
;
711 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
712 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
713 encode_encap_extcomm(tnl_type
, &eval
);
715 ecom_encap
.val
= (uint8_t *)eval
.val
;
716 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
719 memset(&mac
, 0, sizeof(struct ethaddr
));
720 memset(&ecom_es_rt
, 0, sizeof(ecom_es_rt
));
721 es_get_system_mac(&es
->esi
, &mac
);
722 encode_es_rt_extcomm(&eval_es_rt
, &mac
);
724 ecom_es_rt
.val
= (uint8_t *)eval_es_rt
.val
;
726 ecommunity_merge(attr
->ecommunity
, &ecom_es_rt
);
728 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
732 * Build extended communities for EVPN prefix route.
734 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
737 struct ecommunity ecom_encap
;
738 struct ecommunity ecom_rmac
;
739 struct ecommunity_val eval
;
740 struct ecommunity_val eval_rmac
;
741 bgp_encap_types tnl_type
;
742 struct listnode
*node
, *nnode
;
743 struct ecommunity
*ecom
;
744 struct list
*vrf_export_rtl
= NULL
;
747 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
748 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
749 encode_encap_extcomm(tnl_type
, &eval
);
751 ecom_encap
.val
= (uint8_t *)eval
.val
;
754 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
756 /* Add the export RTs for L3VNI/VRF */
757 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
758 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
760 ecommunity_merge(attr
->ecommunity
, ecom
);
762 /* add the router mac extended community */
763 if (!is_zero_mac(&attr
->rmac
)) {
764 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
765 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
767 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
769 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
772 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
776 * Build extended communities for EVPN route.
777 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
778 * and ENCAP extended communities are applicable for all routes.
779 * The default gateway extended community and MAC mobility (sticky) extended
780 * community are added as needed based on passed settings - only for type-2
781 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
782 * added, if present, based on passed settings - only for non-link-local
785 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
788 struct ecommunity ecom_encap
;
789 struct ecommunity ecom_sticky
;
790 struct ecommunity ecom_default_gw
;
791 struct ecommunity ecom_rmac
;
792 struct ecommunity ecom_na
;
793 struct ecommunity_val eval
;
794 struct ecommunity_val eval_sticky
;
795 struct ecommunity_val eval_default_gw
;
796 struct ecommunity_val eval_rmac
;
797 struct ecommunity_val eval_na
;
799 bgp_encap_types tnl_type
;
800 struct listnode
*node
, *nnode
;
801 struct ecommunity
*ecom
;
803 struct list
*vrf_export_rtl
= NULL
;
806 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
807 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
808 encode_encap_extcomm(tnl_type
, &eval
);
810 ecom_encap
.val
= (uint8_t *)eval
.val
;
813 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
815 /* Add the export RTs for L2VNI */
816 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
817 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
819 /* Add the export RTs for L3VNI if told to - caller determines
820 * when this should be done.
823 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
824 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
825 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
827 attr
->ecommunity
= ecommunity_merge(
828 attr
->ecommunity
, ecom
);
832 /* Add MAC mobility (sticky) if needed. */
835 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
836 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
837 ecom_sticky
.size
= 1;
838 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
840 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
843 /* Add RMAC, if told to. */
845 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
846 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
848 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
850 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
853 /* Add default gateway, if needed. */
854 if (attr
->default_gw
) {
855 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
856 encode_default_gw_extcomm(&eval_default_gw
);
857 ecom_default_gw
.size
= 1;
858 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
860 ecommunity_merge(attr
->ecommunity
, &ecom_default_gw
);
863 if (attr
->router_flag
) {
864 memset(&ecom_na
, 0, sizeof(ecom_na
));
865 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
);
867 ecom_na
.val
= (uint8_t *)eval_na
.val
;
868 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
872 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
876 * Add MAC mobility extended community to attribute.
878 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
880 struct ecommunity ecom_tmp
;
881 struct ecommunity_val eval
;
882 uint8_t *ecom_val_ptr
;
889 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
891 /* Find current MM ecommunity */
894 if (attr
->ecommunity
) {
895 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
896 pnt
= attr
->ecommunity
->val
+ (i
* 8);
900 if (type
== ECOMMUNITY_ENCODE_EVPN
902 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
903 ecom_val_ptr
= (uint8_t *)(attr
->ecommunity
->val
910 /* Update the existing MM ecommunity */
912 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
914 /* Add MM to existing */
916 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
918 ecom_tmp
.val
= (uint8_t *)eval
.val
;
920 if (attr
->ecommunity
)
922 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
924 attr
->ecommunity
= ecommunity_dup(&ecom_tmp
);
928 /* Install EVPN route into zebra. */
929 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
930 struct prefix_evpn
*p
, struct bgp_path_info
*pi
)
936 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
938 if (pi
->attr
->sticky
)
939 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
940 if (pi
->attr
->default_gw
)
941 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
942 if (is_evpn_prefix_ipaddr_v6(p
) &&
943 pi
->attr
->router_flag
)
944 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
945 ret
= bgp_zebra_send_remote_macip(
946 bgp
, vpn
, p
, pi
->attr
->nexthop
, 1, flags
,
947 mac_mobility_seqnum(pi
->attr
));
949 switch (pi
->attr
->pmsi_tnl_type
) {
950 case PMSI_TNLTYPE_INGR_REPL
:
951 flood_control
= VXLAN_FLOOD_HEAD_END_REPL
;
954 case PMSI_TNLTYPE_PIM_SM
:
955 flood_control
= VXLAN_FLOOD_PIM_SM
;
959 flood_control
= VXLAN_FLOOD_DISABLED
;
962 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, flood_control
, 1);
968 /* Uninstall EVPN route from zebra. */
969 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
970 struct prefix_evpn
*p
,
971 struct in_addr remote_vtep_ip
)
975 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
976 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
979 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
,
980 VXLAN_FLOOD_DISABLED
, 0);
986 * Due to MAC mobility, the prior "local" best route has been supplanted
987 * by a "remote" best route. The prior route has to be deleted and withdrawn
990 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
992 struct bgp_path_info
*old_local
)
994 struct bgp_node
*global_rn
;
995 struct bgp_path_info
*pi
;
996 afi_t afi
= AFI_L2VPN
;
997 safi_t safi
= SAFI_EVPN
;
999 /* Locate route node in the global EVPN routing table. Note that
1000 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1003 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1004 (struct prefix
*)&rn
->p
, &vpn
->prd
);
1006 /* Delete route entry in the global EVPN table. */
1007 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1009 /* Schedule for processing - withdraws to peers happen from
1013 bgp_process(bgp
, global_rn
, afi
, safi
);
1014 bgp_unlock_node(global_rn
);
1017 /* Delete route entry in the VNI route table, caller to remove. */
1018 bgp_path_info_delete(rn
, old_local
);
1021 static struct in_addr
*es_vtep_new(struct in_addr vtep
)
1025 ip
= XCALLOC(MTYPE_BGP_EVPN_ES_VTEP
, sizeof(struct in_addr
));
1027 ip
->s_addr
= vtep
.s_addr
;
1031 static void es_vtep_free(struct in_addr
*ip
)
1033 XFREE(MTYPE_BGP_EVPN_ES_VTEP
, ip
);
1036 /* check if VTEP is already part of the list */
1037 static int is_vtep_present_in_list(struct list
*list
,
1038 struct in_addr vtep
)
1040 struct listnode
*node
= NULL
;
1041 struct in_addr
*tmp
;
1043 for (ALL_LIST_ELEMENTS_RO(list
, node
, tmp
)) {
1044 if (tmp
->s_addr
== vtep
.s_addr
)
1051 * Best path for ES route was changed,
1052 * update the list of VTEPs for this ES
1054 static int evpn_es_install_vtep(struct bgp
*bgp
,
1056 struct prefix_evpn
*p
,
1057 struct in_addr rvtep
)
1059 struct in_addr
*vtep_ip
;
1061 if (is_vtep_present_in_list(es
->vtep_list
, rvtep
))
1065 vtep_ip
= es_vtep_new(rvtep
);
1067 listnode_add_sort(es
->vtep_list
, vtep_ip
);
1072 * Best path for ES route was changed,
1073 * update the list of VTEPs for this ES
1075 static int evpn_es_uninstall_vtep(struct bgp
*bgp
,
1077 struct prefix_evpn
*p
,
1078 struct in_addr rvtep
)
1080 struct listnode
*node
, *nnode
, *node_to_del
= NULL
;
1081 struct in_addr
*tmp
;
1083 for (ALL_LIST_ELEMENTS(es
->vtep_list
, node
, nnode
, tmp
)) {
1084 if (tmp
->s_addr
== rvtep
.s_addr
) {
1091 list_delete_node(es
->vtep_list
, node_to_del
);
1097 * Calculate the best path for a ES(type-4) route.
1099 static int evpn_es_route_select_install(struct bgp
*bgp
,
1101 struct bgp_node
*rn
)
1104 afi_t afi
= AFI_L2VPN
;
1105 safi_t safi
= SAFI_EVPN
;
1106 struct bgp_path_info
*old_select
; /* old best */
1107 struct bgp_path_info
*new_select
; /* new best */
1108 struct bgp_path_info_pair old_and_new
;
1110 /* Compute the best path. */
1111 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
],
1112 &old_and_new
, afi
, safi
);
1113 old_select
= old_and_new
.old
;
1114 new_select
= old_and_new
.new;
1117 * If the best path hasn't changed - see if something needs to be
1120 if (old_select
&& old_select
== new_select
1121 && old_select
->type
== ZEBRA_ROUTE_BGP
1122 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1123 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1124 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1125 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1126 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1127 ret
= evpn_es_install_vtep(bgp
, es
,
1128 (struct prefix_evpn
*)&rn
->p
,
1129 old_select
->attr
->nexthop
);
1131 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1132 bgp_zebra_clear_route_change_flags(rn
);
1136 /* If the user did a "clear" this flag will be set */
1137 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1140 * bestpath has changed; update relevant fields and install or uninstall
1141 * into the zebra RIB.
1143 if (old_select
|| new_select
)
1144 bgp_bump_version(rn
);
1147 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1149 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1150 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1151 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1154 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1155 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1156 ret
= evpn_es_install_vtep(bgp
, es
,
1157 (struct prefix_evpn
*)&rn
->p
,
1158 new_select
->attr
->nexthop
);
1160 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1161 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1162 ret
= evpn_es_uninstall_vtep(
1163 bgp
, es
, (struct prefix_evpn
*)&rn
->p
,
1164 old_select
->attr
->nexthop
);
1167 /* Clear any route change flags. */
1168 bgp_zebra_clear_route_change_flags(rn
);
1170 /* Reap old select bgp_path_info, if it has been removed */
1171 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1172 bgp_path_info_reap(rn
, old_select
);
1178 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1181 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1182 struct bgp_node
*rn
)
1184 struct bgp_path_info
*old_select
, *new_select
;
1185 struct bgp_path_info_pair old_and_new
;
1186 afi_t afi
= AFI_L2VPN
;
1187 safi_t safi
= SAFI_EVPN
;
1190 /* Compute the best path. */
1191 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1193 old_select
= old_and_new
.old
;
1194 new_select
= old_and_new
.new;
1196 /* If the best path hasn't changed - see if there is still something to
1200 if (old_select
&& old_select
== new_select
1201 && old_select
->type
== ZEBRA_ROUTE_BGP
1202 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1203 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1204 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1205 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1206 if (bgp_zebra_has_route_changed(rn
, old_select
))
1207 ret
= evpn_zebra_install(
1208 bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1210 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1211 bgp_zebra_clear_route_change_flags(rn
);
1215 /* If the user did a "clear" this flag will be set */
1216 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1218 /* bestpath has changed; update relevant fields and install or uninstall
1219 * into the zebra RIB.
1221 if (old_select
|| new_select
)
1222 bgp_bump_version(rn
);
1225 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1227 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1228 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1229 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1232 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1233 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1234 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1237 /* If an old best existed and it was a "local" route, the only
1239 * it would be supplanted is due to MAC mobility procedures. So,
1241 * need to do an implicit delete and withdraw that route from
1244 if (old_select
&& old_select
->peer
== bgp
->peer_self
1245 && old_select
->type
== ZEBRA_ROUTE_BGP
1246 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
1247 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
1249 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1250 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1251 ret
= evpn_zebra_uninstall(bgp
, vpn
,
1252 (struct prefix_evpn
*)&rn
->p
,
1253 old_select
->attr
->nexthop
);
1256 /* Clear any route change flags. */
1257 bgp_zebra_clear_route_change_flags(rn
);
1259 /* Reap old select bgp_path_info, if it has been removed */
1260 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1261 bgp_path_info_reap(rn
, old_select
);
1267 * Return true if the local ri for this rn is of type gateway mac
1269 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
1271 struct bgp_path_info
*tmp_pi
= NULL
;
1272 struct bgp_path_info
*local_pi
= NULL
;
1275 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1276 tmp_pi
= tmp_pi
->next
) {
1277 if (tmp_pi
->peer
== bgp
->peer_self
1278 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1279 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1286 return local_pi
->attr
->default_gw
;
1291 * Return true if the local ri for this rn has sticky set
1293 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
1295 struct bgp_path_info
*tmp_pi
;
1296 struct bgp_path_info
*local_pi
;
1299 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1300 tmp_pi
= tmp_pi
->next
) {
1301 if (tmp_pi
->peer
== bgp
->peer_self
1302 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1303 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1310 return local_pi
->attr
->sticky
;
1314 * create or update EVPN type4 route entry.
1315 * This could be in the ES table or the global table.
1316 * TODO: handle remote ES (type4) routes as well
1318 static int update_evpn_type4_route_entry(struct bgp
*bgp
, struct evpnes
*es
,
1319 afi_t afi
, safi_t safi
,
1320 struct bgp_node
*rn
, struct attr
*attr
,
1321 int add
, struct bgp_path_info
**ri
,
1324 char buf
[ESI_STR_LEN
];
1325 char buf1
[INET6_ADDRSTRLEN
];
1326 struct bgp_path_info
*tmp_pi
= NULL
;
1327 struct bgp_path_info
*local_pi
= NULL
; /* local route entry if any */
1328 struct bgp_path_info
*remote_pi
= NULL
; /* remote route entry if any */
1329 struct attr
*attr_new
= NULL
;
1330 struct prefix_evpn
*evp
= NULL
;
1334 evp
= (struct prefix_evpn
*)&rn
->p
;
1336 /* locate the local and remote entries if any */
1337 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1338 tmp_pi
= tmp_pi
->next
) {
1339 if (tmp_pi
->peer
== bgp
->peer_self
1340 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1341 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1343 if (tmp_pi
->type
== ZEBRA_ROUTE_BGP
1344 && tmp_pi
->sub_type
== BGP_ROUTE_IMPORTED
1345 && CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1349 /* we don't expect to see a remote_ri at this point.
1350 * An ES route has esi + vtep_ip as the key,
1351 * We shouldn't see the same route from any other vtep.
1356 "%u ERROR: local es route for ESI: %s Vtep %s also learnt from remote",
1358 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1359 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1363 if (!local_pi
&& !add
)
1366 /* create or update the entry */
1369 /* Add or update attribute to hash */
1370 attr_new
= bgp_attr_intern(attr
);
1372 /* Create new route with its attribute. */
1373 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1374 bgp
->peer_self
, attr_new
, rn
);
1375 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1377 /* add the newly created path to the route-node */
1378 bgp_path_info_add(rn
, tmp_pi
);
1381 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1382 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1385 /* The attribute has changed.
1386 * Add (or update) attribute to hash. */
1387 attr_new
= bgp_attr_intern(attr
);
1388 bgp_path_info_set_flag(rn
, tmp_pi
,
1389 BGP_PATH_ATTR_CHANGED
);
1391 /* Restore route, if needed. */
1392 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1393 bgp_path_info_restore(rn
, tmp_pi
);
1395 /* Unintern existing, set to new. */
1396 bgp_attr_unintern(&tmp_pi
->attr
);
1397 tmp_pi
->attr
= attr_new
;
1398 tmp_pi
->uptime
= bgp_clock();
1402 /* Return back the route entry. */
1407 /* update evpn es (type-4) route */
1408 static int update_evpn_type4_route(struct bgp
*bgp
,
1410 struct prefix_evpn
*p
)
1413 int route_changed
= 0;
1414 char buf
[ESI_STR_LEN
];
1415 char buf1
[INET6_ADDRSTRLEN
];
1416 afi_t afi
= AFI_L2VPN
;
1417 safi_t safi
= SAFI_EVPN
;
1419 struct attr
*attr_new
= NULL
;
1420 struct bgp_node
*rn
= NULL
;
1421 struct bgp_path_info
*pi
= NULL
;
1423 memset(&attr
, 0, sizeof(struct attr
));
1425 /* Build path-attribute for this route. */
1426 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1427 attr
.nexthop
= es
->originator_ip
.ipaddr_v4
;
1428 attr
.mp_nexthop_global_in
= es
->originator_ip
.ipaddr_v4
;
1429 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1431 /* Set up extended community. */
1432 build_evpn_type4_route_extcomm(es
, &attr
);
1434 /* First, create (or fetch) route node within the ESI. */
1435 /* NOTE: There is no RD here. */
1436 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
1438 /* Create or update route entry. */
1439 ret
= update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, &attr
, 1,
1440 &pi
, &route_changed
);
1442 flog_err(EC_BGP_ES_INVALID
,
1443 "%u ERROR: Failed to updated ES route ESI: %s VTEP %s",
1445 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1446 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1450 attr_new
= pi
->attr
;
1452 /* Perform route selection;
1453 * this is just to set the flags correctly
1454 * as local route in the ES always wins.
1456 evpn_es_route_select_install(bgp
, es
, rn
);
1457 bgp_unlock_node(rn
);
1459 /* If this is a new route or some attribute has changed, export the
1460 * route to the global table. The route will be advertised to peers
1461 * from there. Note that this table is a 2-level tree (RD-level +
1462 * Prefix-level) similar to L3VPN routes.
1464 if (route_changed
) {
1465 struct bgp_path_info
*global_pi
;
1467 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1468 (struct prefix
*)p
, &es
->prd
);
1469 update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, attr_new
,
1470 1, &global_pi
, &route_changed
);
1472 /* Schedule for processing and unlock node. */
1473 bgp_process(bgp
, rn
, afi
, safi
);
1474 bgp_unlock_node(rn
);
1477 /* Unintern temporary. */
1478 aspath_unintern(&attr
.aspath
);
1482 static int update_evpn_type5_route_entry(struct bgp
*bgp_evpn
,
1483 struct bgp
*bgp_vrf
, afi_t afi
,
1484 safi_t safi
, struct bgp_node
*rn
,
1485 struct attr
*attr
, int *route_changed
)
1487 struct attr
*attr_new
= NULL
;
1488 struct bgp_path_info
*pi
= NULL
;
1489 mpls_label_t label
= MPLS_INVALID_LABEL
;
1490 struct bgp_path_info
*local_pi
= NULL
;
1491 struct bgp_path_info
*tmp_pi
= NULL
;
1494 /* locate the local route entry if any */
1495 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1496 tmp_pi
= tmp_pi
->next
) {
1497 if (tmp_pi
->peer
== bgp_evpn
->peer_self
1498 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1499 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1504 * create a new route entry if one doesn't exist.
1505 * Otherwise see if route attr has changed
1509 /* route has changed as this is the first entry */
1512 /* Add (or update) attribute to hash. */
1513 attr_new
= bgp_attr_intern(attr
);
1515 /* create the route info from attribute */
1516 pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1517 bgp_evpn
->peer_self
, attr_new
, rn
);
1518 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
1520 /* Type-5 routes advertise the L3-VNI */
1521 bgp_path_info_extra_get(pi
);
1522 vni2label(bgp_vrf
->l3vni
, &label
);
1523 memcpy(&pi
->extra
->label
, &label
, sizeof(label
));
1524 pi
->extra
->num_labels
= 1;
1526 /* add the route entry to route node*/
1527 bgp_path_info_add(rn
, pi
);
1531 if (!attrhash_cmp(tmp_pi
->attr
, attr
)) {
1533 /* attribute changed */
1536 /* The attribute has changed. */
1537 /* Add (or update) attribute to hash. */
1538 attr_new
= bgp_attr_intern(attr
);
1539 bgp_path_info_set_flag(rn
, tmp_pi
,
1540 BGP_PATH_ATTR_CHANGED
);
1542 /* Restore route, if needed. */
1543 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1544 bgp_path_info_restore(rn
, tmp_pi
);
1546 /* Unintern existing, set to new. */
1547 bgp_attr_unintern(&tmp_pi
->attr
);
1548 tmp_pi
->attr
= attr_new
;
1549 tmp_pi
->uptime
= bgp_clock();
1555 /* update evpn type-5 route entry */
1556 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1557 struct attr
*src_attr
)
1559 afi_t afi
= AFI_L2VPN
;
1560 safi_t safi
= SAFI_EVPN
;
1562 struct bgp_node
*rn
= NULL
;
1563 struct bgp
*bgp_evpn
= NULL
;
1564 int route_changed
= 0;
1566 bgp_evpn
= bgp_get_evpn();
1570 /* Build path attribute for this route - use the source attr, if
1571 * present, else treat as locally originated.
1574 bgp_attr_dup(&attr
, src_attr
);
1576 memset(&attr
, 0, sizeof(struct attr
));
1577 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1580 /* Advertise Primary IP (PIP) is enabled, send individual
1581 * IP (default instance router-id) as nexthop.
1582 * PIP is disabled or vrr interface is not present
1583 * use anycast-IP as nexthop and anycast RMAC.
1585 if (!bgp_vrf
->evpn_info
->advertise_pip
||
1586 (!bgp_vrf
->evpn_info
->is_anycast_mac
)) {
1587 attr
.nexthop
= bgp_vrf
->originator_ip
;
1588 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1589 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, ETH_ALEN
);
1592 memcpy(&attr
.rmac
, &bgp_vrf
->evpn_info
->pip_rmac
, ETH_ALEN
);
1593 if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
!= INADDR_ANY
) {
1594 attr
.nexthop
= bgp_vrf
->evpn_info
->pip_ip
;
1595 attr
.mp_nexthop_global_in
= bgp_vrf
->evpn_info
->pip_ip
;
1596 } else if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
== INADDR_ANY
)
1597 if (bgp_debug_zebra(NULL
)) {
1598 char buf1
[PREFIX_STRLEN
];
1600 zlog_debug("VRF %s evp %s advertise-pip primary ip is not configured",
1601 vrf_id_to_name(bgp_vrf
->vrf_id
),
1602 prefix2str(evp
, buf1
, sizeof(buf1
)));
1606 if (bgp_debug_zebra(NULL
)) {
1607 char buf
[ETHER_ADDR_STRLEN
];
1608 char buf1
[PREFIX_STRLEN
];
1609 char buf2
[INET6_ADDRSTRLEN
];
1611 zlog_debug("VRF %s type-5 route evp %s RMAC %s nexthop %s",
1612 vrf_id_to_name(bgp_vrf
->vrf_id
),
1613 prefix2str(evp
, buf1
, sizeof(buf1
)),
1614 prefix_mac2str(&attr
.rmac
, buf
, sizeof(buf
)),
1615 inet_ntop(AF_INET
, &attr
.nexthop
, buf2
,
1619 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1621 /* Setup RT and encap extended community */
1622 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1624 /* get the route node in global table */
1625 rn
= bgp_afi_node_get(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1626 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1629 /* create or update the route entry within the route node */
1630 update_evpn_type5_route_entry(bgp_evpn
, bgp_vrf
, afi
, safi
, rn
, &attr
,
1633 /* schedule for processing and unlock node */
1634 if (route_changed
) {
1635 bgp_process(bgp_evpn
, rn
, afi
, safi
);
1636 bgp_unlock_node(rn
);
1639 /* uninten temporary */
1641 aspath_unintern(&attr
.aspath
);
1646 * Create or update EVPN route entry. This could be in the VNI route table
1647 * or the global route table.
1649 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1650 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1651 struct attr
*attr
, int add
,
1652 struct bgp_path_info
**pi
, uint8_t flags
,
1655 struct bgp_path_info
*tmp_pi
;
1656 struct bgp_path_info
*local_pi
;
1657 struct attr
*attr_new
;
1658 mpls_label_t label
[BGP_MAX_LABELS
];
1659 uint32_t num_labels
= 1;
1660 int route_change
= 1;
1662 struct prefix_evpn
*evp
;
1665 evp
= (struct prefix_evpn
*)&rn
->p
;
1666 memset(&label
, 0, sizeof(label
));
1668 /* See if this is an update of an existing route, or a new add. */
1670 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1671 tmp_pi
= tmp_pi
->next
) {
1672 if (tmp_pi
->peer
== bgp
->peer_self
1673 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1674 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1678 /* If route doesn't exist already, create a new one, if told to.
1679 * Otherwise act based on whether the attributes of the route have
1682 if (!local_pi
&& !add
)
1685 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1686 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1687 add_mac_mobility_to_attr(seq
, attr
);
1690 /* Add (or update) attribute to hash. */
1691 attr_new
= bgp_attr_intern(attr
);
1693 /* Extract MAC mobility sequence number, if any. */
1694 attr_new
->mm_seqnum
=
1695 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1696 attr_new
->sticky
= sticky
;
1698 /* Create new route with its attribute. */
1699 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1700 bgp
->peer_self
, attr_new
, rn
);
1701 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1702 bgp_path_info_extra_get(tmp_pi
);
1704 /* The VNI goes into the 'label' field of the route */
1705 vni2label(vpn
->vni
, &label
[0]);
1707 /* Type-2 routes may carry a second VNI - the L3-VNI.
1708 * Only attach second label if we are advertising two labels for
1711 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1712 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1715 l3vni
= bgpevpn_get_l3vni(vpn
);
1717 vni2label(l3vni
, &label
[1]);
1722 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1723 tmp_pi
->extra
->num_labels
= num_labels
;
1724 /* Mark route as self type-2 route */
1725 if (flags
&& CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1726 tmp_pi
->extra
->af_flags
= BGP_EVPN_MACIP_TYPE_SVI_IP
;
1727 bgp_path_info_add(rn
, tmp_pi
);
1730 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1731 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1735 * The attributes have changed, type-2 routes needs to
1736 * be advertised with right labels.
1738 vni2label(vpn
->vni
, &label
[0]);
1739 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1740 && CHECK_FLAG(vpn
->flags
,
1741 VNI_FLAG_USE_TWO_LABELS
)) {
1744 l3vni
= bgpevpn_get_l3vni(vpn
);
1746 vni2label(l3vni
, &label
[1]);
1750 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1751 tmp_pi
->extra
->num_labels
= num_labels
;
1753 /* The attribute has changed. */
1754 /* Add (or update) attribute to hash. */
1755 attr_new
= bgp_attr_intern(attr
);
1756 bgp_path_info_set_flag(rn
, tmp_pi
,
1757 BGP_PATH_ATTR_CHANGED
);
1759 /* Extract MAC mobility sequence number, if any. */
1760 attr_new
->mm_seqnum
=
1761 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1762 attr_new
->sticky
= sticky
;
1764 /* Restore route, if needed. */
1765 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1766 bgp_path_info_restore(rn
, tmp_pi
);
1768 /* Unintern existing, set to new. */
1769 bgp_attr_unintern(&tmp_pi
->attr
);
1770 tmp_pi
->attr
= attr_new
;
1771 tmp_pi
->uptime
= bgp_clock();
1775 /* Return back the route entry. */
1777 return route_change
;
1780 static void evpn_zebra_reinstall_best_route(struct bgp
*bgp
,
1781 struct bgpevpn
*vpn
, struct bgp_node
*rn
)
1783 struct bgp_path_info
*tmp_ri
;
1784 struct bgp_path_info
*curr_select
= NULL
;
1786 for (tmp_ri
= bgp_node_get_bgp_path_info(rn
);
1787 tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1788 if (CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_SELECTED
)) {
1789 curr_select
= tmp_ri
;
1794 if (curr_select
&& curr_select
->type
== ZEBRA_ROUTE_BGP
1795 && curr_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1796 evpn_zebra_install(bgp
, vpn
,
1797 (struct prefix_evpn
*)&rn
->p
,
1802 * If the local route was not selected evict it and tell zebra to re-add
1803 * the best remote dest.
1805 * Typically a local path added by zebra is expected to be selected as
1806 * best. In which case when a remote path wins as best (later)
1807 * evpn_route_select_install itself evicts the older-local-best path.
1809 * However if bgp's add and zebra's add cross paths (race condition) it
1810 * is possible that the local path is no longer the "older" best path.
1811 * It is a path that was never designated as best and hence requires
1812 * additional handling to prevent bgp from injecting and holding on to a
1813 * non-best local path.
1815 static void evpn_cleanup_local_non_best_route(struct bgp
*bgp
,
1816 struct bgpevpn
*vpn
,
1817 struct bgp_node
*rn
,
1818 struct bgp_path_info
*local_pi
)
1820 char buf
[PREFIX_STRLEN
];
1822 /* local path was not picked as the winner; kick it out */
1823 if (bgp_debug_zebra(NULL
)) {
1824 zlog_debug("evicting local evpn prefix %s as remote won",
1825 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
1827 evpn_delete_old_local_route(bgp
, vpn
, rn
, local_pi
);
1828 bgp_path_info_reap(rn
, local_pi
);
1830 /* tell zebra to re-add the best remote path */
1831 evpn_zebra_reinstall_best_route(bgp
, vpn
, rn
);
1835 * Create or update EVPN route (of type based on prefix) for specified VNI
1836 * and schedule for processing.
1838 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1839 struct prefix_evpn
*p
, uint8_t flags
,
1842 struct bgp_node
*rn
;
1844 struct attr
*attr_new
;
1845 int add_l3_ecomm
= 0;
1846 struct bgp_path_info
*pi
;
1847 afi_t afi
= AFI_L2VPN
;
1848 safi_t safi
= SAFI_EVPN
;
1851 memset(&attr
, 0, sizeof(struct attr
));
1853 /* Build path-attribute for this route. */
1854 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1855 attr
.nexthop
= vpn
->originator_ip
;
1856 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1857 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1858 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1859 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1860 attr
.router_flag
= CHECK_FLAG(flags
,
1861 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1863 /* PMSI is only needed for type-3 routes */
1864 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
1865 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1866 attr
.pmsi_tnl_type
= PMSI_TNLTYPE_INGR_REPL
;
1869 /* router mac is only needed for type-2 routes here. */
1870 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
1871 uint8_t af_flags
= 0;
1873 if (CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1874 SET_FLAG(af_flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
);
1876 bgp_evpn_get_rmac_nexthop(vpn
, p
, &attr
, af_flags
);
1878 if (bgp_debug_zebra(NULL
)) {
1879 char buf
[ETHER_ADDR_STRLEN
];
1880 char buf1
[PREFIX_STRLEN
];
1882 zlog_debug("VRF %s vni %u type-2 route evp %s RMAC %s nexthop %s",
1884 vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
) : " ",
1886 prefix2str(p
, buf1
, sizeof(buf1
)),
1887 prefix_mac2str(&attr
.rmac
, buf
,
1889 inet_ntoa(attr
.mp_nexthop_global_in
));
1893 vni2label(vpn
->vni
, &(attr
.label
));
1895 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1896 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1899 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1900 (is_evpn_prefix_ipaddr_v4(p
) ||
1901 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1902 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1903 bgpevpn_get_l3vni(vpn
))
1906 /* Set up extended community. */
1907 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1909 /* First, create (or fetch) route node within the VNI. */
1910 /* NOTE: There is no RD here. */
1911 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1913 /* Create or update route entry. */
1914 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1915 1, &pi
, flags
, seq
);
1917 attr_new
= pi
->attr
;
1919 /* lock ri to prevent freeing in evpn_route_select_install */
1920 bgp_path_info_lock(pi
);
1922 /* Perform route selection. Normally, the local route in the
1923 * VNI is expected to win and be the best route. However, if
1924 * there is a race condition where a host moved from local to
1925 * remote and the remote route was received in BGP just prior
1926 * to the local MACIP notification from zebra, the remote
1927 * route would win, and we should evict the defunct local route
1928 * and (re)install the remote route into zebra.
1930 evpn_route_select_install(bgp
, vpn
, rn
);
1932 * If the new local route was not selected evict it and tell zebra
1933 * to re-add the best remote dest. BGP doesn't retain non-best local
1936 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1938 evpn_cleanup_local_non_best_route(bgp
, vpn
, rn
, pi
);
1940 bgp_path_info_unlock(pi
);
1942 bgp_unlock_node(rn
);
1944 /* If this is a new route or some attribute has changed, export the
1945 * route to the global table. The route will be advertised to peers
1946 * from there. Note that this table is a 2-level tree (RD-level +
1947 * Prefix-level) similar to L3VPN routes.
1950 struct bgp_path_info
*global_pi
;
1952 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1953 (struct prefix
*)p
, &vpn
->prd
);
1954 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1,
1955 &global_pi
, flags
, seq
);
1957 /* Schedule for processing and unlock node. */
1958 bgp_process(bgp
, rn
, afi
, safi
);
1959 bgp_unlock_node(rn
);
1962 /* Unintern temporary. */
1963 aspath_unintern(&attr
.aspath
);
1969 * Delete EVPN route entry.
1970 * The entry can be in ESI/VNI table or the global table.
1972 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1973 struct bgp_node
*rn
,
1974 struct bgp_path_info
**pi
)
1976 struct bgp_path_info
*tmp_pi
;
1980 /* Now, find matching route. */
1981 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1982 tmp_pi
= tmp_pi
->next
)
1983 if (tmp_pi
->peer
== bgp
->peer_self
1984 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1985 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1990 /* Mark route for delete. */
1992 bgp_path_info_delete(rn
, tmp_pi
);
1997 /* Delete EVPN ES (type-4) route */
1998 static int delete_evpn_type4_route(struct bgp
*bgp
,
2000 struct prefix_evpn
*p
)
2002 afi_t afi
= AFI_L2VPN
;
2003 safi_t safi
= SAFI_EVPN
;
2004 struct bgp_path_info
*pi
;
2005 struct bgp_node
*rn
= NULL
; /* rn in esi table */
2006 struct bgp_node
*global_rn
= NULL
; /* rn in global table */
2008 /* First, locate the route node within the ESI.
2009 * If it doesn't exist, ther is nothing to do.
2010 * Note: there is no RD here.
2012 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2016 /* Next, locate route node in the global EVPN routing table.
2017 * Note that this table is a 2-level tree (RD-level + Prefix-level)
2019 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
2020 (struct prefix
*)p
, &es
->prd
);
2023 /* Delete route entry in the global EVPN table. */
2024 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
2026 /* Schedule for processing - withdraws to peers happen from
2030 bgp_process(bgp
, global_rn
, afi
, safi
);
2031 bgp_unlock_node(global_rn
);
2035 * Delete route entry in the ESI route table.
2036 * This can just be removed.
2038 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2040 bgp_path_info_reap(rn
, pi
);
2041 bgp_unlock_node(rn
);
2045 /* Delete EVPN type5 route */
2046 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
2048 afi_t afi
= AFI_L2VPN
;
2049 safi_t safi
= SAFI_EVPN
;
2050 struct bgp_node
*rn
= NULL
;
2051 struct bgp_path_info
*pi
= NULL
;
2052 struct bgp
*bgp_evpn
= NULL
; /* evpn bgp instance */
2054 bgp_evpn
= bgp_get_evpn();
2058 /* locate the global route entry for this type-5 prefix */
2059 rn
= bgp_afi_node_lookup(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
2060 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
2064 delete_evpn_route_entry(bgp_evpn
, afi
, safi
, rn
, &pi
);
2066 bgp_process(bgp_evpn
, rn
, afi
, safi
);
2067 bgp_unlock_node(rn
);
2072 * Delete EVPN route (of type based on prefix) for specified VNI and
2073 * schedule for processing.
2075 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2076 struct prefix_evpn
*p
)
2078 struct bgp_node
*rn
, *global_rn
;
2079 struct bgp_path_info
*pi
;
2080 afi_t afi
= AFI_L2VPN
;
2081 safi_t safi
= SAFI_EVPN
;
2083 /* First, locate the route node within the VNI. If it doesn't exist,
2085 * is nothing further to do.
2087 /* NOTE: There is no RD here. */
2088 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2092 /* Next, locate route node in the global EVPN routing table. Note that
2093 * this table is a 2-level tree (RD-level + Prefix-level) similar to
2096 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
2097 (struct prefix
*)p
, &vpn
->prd
);
2099 /* Delete route entry in the global EVPN table. */
2100 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
2102 /* Schedule for processing - withdraws to peers happen from
2106 bgp_process(bgp
, global_rn
, afi
, safi
);
2107 bgp_unlock_node(global_rn
);
2110 /* Delete route entry in the VNI route table. This can just be removed.
2112 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2114 bgp_path_info_reap(rn
, pi
);
2115 evpn_route_select_install(bgp
, vpn
, rn
);
2117 bgp_unlock_node(rn
);
2123 * Update all type-2 (MACIP) local routes for this VNI - these should also
2124 * be scheduled for advertise to peers.
2126 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2130 struct bgp_node
*rn
;
2131 struct bgp_path_info
*pi
, *tmp_pi
;
2133 struct attr
*attr_new
;
2135 int add_l3_ecomm
= 0;
2140 /* Walk this VNI's route table and update local type-2 routes. For any
2141 * routes updated, update corresponding entry in the global table too.
2143 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2144 rn
= bgp_route_next(rn
)) {
2145 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2146 struct bgp_node
*rd_rn
;
2147 struct bgp_path_info
*global_pi
;
2149 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2152 /* Identify local route. */
2153 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
2154 tmp_pi
= tmp_pi
->next
) {
2155 if (tmp_pi
->peer
== bgp
->peer_self
2156 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2157 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2165 * Build attribute per local route as the MAC mobility and
2166 * some other values could differ for different routes. The
2167 * attributes will be shared in the hash table.
2169 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
2170 attr
.nexthop
= vpn
->originator_ip
;
2171 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
2172 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
2173 bgp_evpn_get_rmac_nexthop(vpn
, evp
, &attr
,
2174 tmp_pi
->extra
->af_flags
);
2176 if (evpn_route_is_sticky(bgp
, rn
))
2178 else if (evpn_route_is_def_gw(bgp
, rn
)) {
2179 attr
.default_gw
= 1;
2180 if (is_evpn_prefix_ipaddr_v6(evp
))
2181 attr
.router_flag
= 1;
2184 if (bgp_debug_zebra(NULL
)) {
2185 char buf
[ETHER_ADDR_STRLEN
];
2186 char buf1
[PREFIX_STRLEN
];
2188 zlog_debug("VRF %s vni %u evp %s RMAC %s nexthop %s",
2190 vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
) : " ",
2192 prefix2str(evp
, buf1
, sizeof(buf1
)),
2193 prefix_mac2str(&attr
.rmac
, buf
, sizeof(buf
)),
2194 inet_ntoa(attr
.mp_nexthop_global_in
));
2197 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2198 * using L3 VNI for type-2 routes also.
2200 if ((is_evpn_prefix_ipaddr_v4(evp
) ||
2201 !IN6_IS_ADDR_LINKLOCAL(
2202 &evp
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
2203 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
2204 bgpevpn_get_l3vni(vpn
))
2207 /* Set up extended community. */
2208 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2210 seq
= mac_mobility_seqnum(tmp_pi
->attr
);
2212 /* Update the route entry. */
2213 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
, 0, &pi
,
2216 /* lock ri to prevent freeing in evpn_route_select_install */
2217 bgp_path_info_lock(pi
);
2219 /* Perform route selection. Normally, the local route in the
2220 * VNI is expected to win and be the best route. However,
2221 * under peculiar situations (e.g., tunnel (next hop) IP change
2222 * that causes best selection to be based on next hop), a
2223 * remote route could win. If the local route is the best,
2224 * ensure it is updated in the global EVPN route table and
2225 * advertised to peers; otherwise, ensure it is evicted and
2226 * (re)install the remote route into zebra.
2228 evpn_route_select_install(bgp
, vpn
, rn
);
2229 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
2230 evpn_cleanup_local_non_best_route(bgp
, vpn
, rn
, pi
);
2232 bgp_path_info_unlock(pi
);
2234 attr_new
= pi
->attr
;
2236 bgp_path_info_unlock(pi
);
2238 /* Update route in global routing table. */
2239 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2240 (struct prefix
*)evp
, &vpn
->prd
);
2242 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
,
2243 attr_new
, 0, &global_pi
, 0,
2244 mac_mobility_seqnum(attr_new
));
2246 /* Schedule for processing and unlock node. */
2247 bgp_process(bgp
, rd_rn
, afi
, safi
);
2248 bgp_unlock_node(rd_rn
);
2251 /* Unintern temporary. */
2252 aspath_unintern(&attr
.aspath
);
2260 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2261 * global routing table. These are also scheduled for withdraw from peers.
2263 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2267 struct bgp_node
*rdrn
, *rn
;
2268 struct bgp_table
*table
;
2269 struct bgp_path_info
*pi
;
2274 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
2275 if (rdrn
&& bgp_node_has_bgp_path_info_data(rdrn
)) {
2276 table
= bgp_node_get_bgp_table_info(rdrn
);
2277 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2278 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2280 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2283 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2285 bgp_process(bgp
, rn
, afi
, safi
);
2289 /* Unlock RD node. */
2291 bgp_unlock_node(rdrn
);
2297 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2298 * table as well as the per-VNI route table.
2300 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2304 struct bgp_node
*rn
;
2305 struct bgp_path_info
*pi
;
2310 /* First, walk the global route table for this VNI's type-2 local
2312 * EVPN routes are a 2-level table, first get the RD table.
2314 delete_global_type2_routes(bgp
, vpn
);
2316 /* Next, walk this VNI's route table and delete local type-2 routes. */
2317 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2318 rn
= bgp_route_next(rn
)) {
2319 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2321 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2324 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2326 /* Route entry in local table gets deleted immediately. */
2328 bgp_path_info_reap(rn
, pi
);
2335 * Delete all routes in per ES route-table
2337 static int delete_all_es_routes(struct bgp
*bgp
, struct evpnes
*es
)
2339 struct bgp_node
*rn
;
2340 struct bgp_path_info
*pi
, *nextpi
;
2342 /* Walk this ES's route table and delete all routes. */
2343 for (rn
= bgp_table_top(es
->route_table
); rn
;
2344 rn
= bgp_route_next(rn
)) {
2345 for (pi
= bgp_node_get_bgp_path_info(rn
);
2346 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2347 bgp_path_info_delete(rn
, pi
);
2348 bgp_path_info_reap(rn
, pi
);
2356 * Delete all routes in the per-VNI route table.
2358 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2360 struct bgp_node
*rn
;
2361 struct bgp_path_info
*pi
, *nextpi
;
2363 /* Walk this VNI's route table and delete all routes. */
2364 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2365 rn
= bgp_route_next(rn
)) {
2366 for (pi
= bgp_node_get_bgp_path_info(rn
);
2367 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2368 bgp_path_info_delete(rn
, pi
);
2369 bgp_path_info_reap(rn
, pi
);
2376 /* BUM traffic flood mode per-l2-vni */
2377 static int bgp_evpn_vni_flood_mode_get(struct bgp
*bgp
,
2378 struct bgpevpn
*vpn
)
2380 /* if flooding has been globally disabled per-vni mode is
2383 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
2384 return VXLAN_FLOOD_DISABLED
;
2386 /* if mcast group ip has been specified we use a PIM-SM MDT */
2387 if (vpn
->mcast_grp
.s_addr
!= INADDR_ANY
)
2388 return VXLAN_FLOOD_PIM_SM
;
2390 /* default is ingress replication */
2391 return VXLAN_FLOOD_HEAD_END_REPL
;
2395 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2396 * export RT getting modified or change to tunnel IP. Note that these
2397 * situations need the route in the per-VNI table as well as the global
2398 * table to be updated (as attributes change).
2400 int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2403 struct prefix_evpn p
;
2405 /* Update and advertise the type-3 route (only one) followed by the
2406 * locally learnt type-2 routes (MACIP) - for this VNI.
2408 * RT-3 only if doing head-end replication
2410 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
2411 == VXLAN_FLOOD_HEAD_END_REPL
) {
2412 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2413 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0);
2418 return update_all_type2_routes(bgp
, vpn
);
2421 /* Delete (and withdraw) local routes for specified ES from global and ES table.
2422 * Also remove all other routes from the per ES table.
2423 * Invoked when ES is deleted.
2425 static int delete_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
2428 char buf
[ESI_STR_LEN
];
2429 struct prefix_evpn p
;
2431 /* Delete and withdraw locally learnt ES route */
2432 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
.ipaddr_v4
);
2433 ret
= delete_evpn_type4_route(bgp
, es
, &p
);
2435 flog_err(EC_BGP_EVPN_ROUTE_DELETE
,
2436 "%u failed to delete type-4 route for ESI %s",
2437 bgp
->vrf_id
, esi_to_str(&es
->esi
, buf
, sizeof(buf
)));
2440 /* Delete all routes from per ES table */
2441 return delete_all_es_routes(bgp
, es
);
2445 * Delete (and withdraw) local routes for specified VNI from the global
2446 * table and per-VNI table. After this, remove all other routes from
2447 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2448 * (advertise-all-vni) being disabled.
2450 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2453 struct prefix_evpn p
;
2455 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2456 * followed by type-3 routes (only one) - for this VNI.
2458 ret
= delete_all_type2_routes(bgp
, vpn
);
2462 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2463 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2467 /* Delete all routes from the per-VNI table. */
2468 return delete_all_vni_routes(bgp
, vpn
);
2472 * There is a flood mcast IP address change. Update the mcast-grp and
2473 * remove the type-3 route if any. A new type-3 route will be generated
2474 * post tunnel_ip update if the new flood mode is head-end-replication.
2476 static int bgp_evpn_mcast_grp_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2477 struct in_addr mcast_grp
)
2479 struct prefix_evpn p
;
2481 vpn
->mcast_grp
= mcast_grp
;
2483 if (is_vni_live(vpn
)) {
2484 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2485 delete_evpn_route(bgp
, vpn
, &p
);
2492 * There is a tunnel endpoint IP address change for this VNI, delete
2493 * prior type-3 route (if needed) and update.
2494 * Note: Route re-advertisement happens elsewhere after other processing
2497 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2498 struct in_addr originator_ip
)
2500 struct prefix_evpn p
;
2502 /* If VNI is not live, we only need to update the originator ip */
2503 if (!is_vni_live(vpn
)) {
2504 vpn
->originator_ip
= originator_ip
;
2508 /* Update the tunnel-ip hash */
2509 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2510 bgp_tip_add(bgp
, &originator_ip
);
2512 /* filter routes as martian nexthop db has changed */
2513 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2515 /* Need to withdraw type-3 route as the originator IP is part
2518 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2519 delete_evpn_route(bgp
, vpn
, &p
);
2521 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2522 vpn
->originator_ip
= originator_ip
;
2526 static struct bgp_path_info
*
2527 bgp_create_evpn_bgp_path_info(struct bgp_path_info
*parent_pi
,
2528 struct bgp_node
*rn
)
2530 struct attr
*attr_new
;
2531 struct bgp_path_info
*pi
;
2533 /* Add (or update) attribute to hash. */
2534 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2536 /* Create new route with its attribute. */
2537 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0, parent_pi
->peer
,
2539 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2540 bgp_path_info_extra_get(pi
);
2541 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2542 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2543 if (parent_pi
->extra
) {
2544 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2545 sizeof(pi
->extra
->label
));
2546 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2548 bgp_path_info_add(rn
, pi
);
2553 /* Install EVPN route entry in ES */
2554 static int install_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2555 struct prefix_evpn
*p
,
2556 struct bgp_path_info
*parent_pi
)
2559 struct bgp_node
*rn
= NULL
;
2560 struct bgp_path_info
*pi
= NULL
;
2561 struct attr
*attr_new
= NULL
;
2563 /* Create (or fetch) route within the VNI.
2564 * NOTE: There is no RD here.
2566 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
2568 /* Check if route entry is already present. */
2569 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2571 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2575 /* Add (or update) attribute to hash. */
2576 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2578 /* Create new route with its attribute. */
2579 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2580 parent_pi
->peer
, attr_new
, rn
);
2581 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2582 bgp_path_info_extra_get(pi
);
2583 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2584 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2585 bgp_path_info_add(rn
, pi
);
2587 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2588 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2589 bgp_unlock_node(rn
);
2592 /* The attribute has changed. */
2593 /* Add (or update) attribute to hash. */
2594 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2596 /* Restore route, if needed. */
2597 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2598 bgp_path_info_restore(rn
, pi
);
2600 /* Mark if nexthop has changed. */
2601 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2602 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2604 /* Unintern existing, set to new. */
2605 bgp_attr_unintern(&pi
->attr
);
2606 pi
->attr
= attr_new
;
2607 pi
->uptime
= bgp_clock();
2610 /* Perform route selection and update zebra, if required. */
2611 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2616 * Install route entry into the VRF routing table and invoke route selection.
2618 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2619 struct prefix_evpn
*evp
,
2620 struct bgp_path_info
*parent_pi
)
2622 struct bgp_node
*rn
;
2623 struct bgp_path_info
*pi
;
2625 struct attr
*attr_new
;
2628 struct prefix
*pp
= &p
;
2631 char buf
[PREFIX_STRLEN
];
2632 char buf1
[PREFIX_STRLEN
];
2634 memset(pp
, 0, sizeof(struct prefix
));
2635 ip_prefix_from_evpn_prefix(evp
, pp
);
2637 if (bgp_debug_zebra(NULL
)) {
2639 "import evpn prefix %s as ip prefix %s in vrf %s",
2640 prefix2str(evp
, buf
, sizeof(buf
)),
2641 prefix2str(pp
, buf1
, sizeof(buf
)),
2642 vrf_id_to_name(bgp_vrf
->vrf_id
));
2645 /* Create (or fetch) route within the VRF. */
2646 /* NOTE: There is no RD here. */
2647 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2649 safi
= SAFI_UNICAST
;
2650 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2651 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2653 safi
= SAFI_UNICAST
;
2654 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2658 /* EVPN routes currently only support a IPv4 next hop which corresponds
2659 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2660 * or prefix route, we have to convert the next hop to an IPv4-mapped
2661 * address for the rest of the code to flow through. In the case of IPv4,
2662 * make sure to set the flag for next hop attribute.
2664 bgp_attr_dup(&attr
, parent_pi
->attr
);
2666 evpn_convert_nexthop_to_ipv6(&attr
);
2668 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2670 /* Check if route entry is already present. */
2671 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2673 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2677 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, rn
);
2679 if (attrhash_cmp(pi
->attr
, &attr
)
2680 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2681 bgp_unlock_node(rn
);
2684 /* The attribute has changed. */
2685 /* Add (or update) attribute to hash. */
2686 attr_new
= bgp_attr_intern(&attr
);
2688 /* Restore route, if needed. */
2689 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2690 bgp_path_info_restore(rn
, pi
);
2692 /* Mark if nexthop has changed. */
2694 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2696 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2697 &attr_new
->mp_nexthop_global
)))
2698 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2700 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_ATTR_CHANGED
);
2701 /* Unintern existing, set to new. */
2702 bgp_attr_unintern(&pi
->attr
);
2703 pi
->attr
= attr_new
;
2704 pi
->uptime
= bgp_clock();
2707 bgp_aggregate_increment(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2709 /* Perform route selection and update zebra, if required. */
2710 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2712 /* Process for route leaking. */
2713 vpn_leak_from_vrf_update(bgp_get_default(), bgp_vrf
, pi
);
2715 bgp_unlock_node(rn
);
2721 * Install route entry into the VNI routing table and invoke route selection.
2723 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2724 struct prefix_evpn
*p
,
2725 struct bgp_path_info
*parent_pi
)
2727 struct bgp_node
*rn
;
2728 struct bgp_path_info
*pi
;
2729 struct attr
*attr_new
;
2732 /* Create (or fetch) route within the VNI. */
2733 /* NOTE: There is no RD here. */
2734 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2736 /* Check if route entry is already present. */
2737 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2739 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2743 /* Create an info */
2744 (void)bgp_create_evpn_bgp_path_info(parent_pi
, rn
);
2746 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2747 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2748 bgp_unlock_node(rn
);
2751 /* The attribute has changed. */
2752 /* Add (or update) attribute to hash. */
2753 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2755 /* Restore route, if needed. */
2756 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2757 bgp_path_info_restore(rn
, pi
);
2759 /* Mark if nexthop has changed. */
2760 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2761 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2763 /* Unintern existing, set to new. */
2764 bgp_attr_unintern(&pi
->attr
);
2765 pi
->attr
= attr_new
;
2766 pi
->uptime
= bgp_clock();
2769 /* Perform route selection and update zebra, if required. */
2770 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2772 bgp_unlock_node(rn
);
2777 /* Uninstall EVPN route entry from ES route table */
2778 static int uninstall_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2779 struct prefix_evpn
*p
,
2780 struct bgp_path_info
*parent_pi
)
2783 struct bgp_node
*rn
;
2784 struct bgp_path_info
*pi
;
2786 if (!es
->route_table
)
2789 /* Locate route within the ESI.
2790 * NOTE: There is no RD here.
2792 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2796 /* Find matching route entry. */
2797 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2799 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2805 /* Mark entry for deletion */
2806 bgp_path_info_delete(rn
, pi
);
2808 /* Perform route selection and update zebra, if required. */
2809 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2811 /* Unlock route node. */
2812 bgp_unlock_node(rn
);
2818 * Uninstall route entry from the VRF routing table and send message
2819 * to zebra, if appropriate.
2821 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2822 struct prefix_evpn
*evp
,
2823 struct bgp_path_info
*parent_pi
)
2825 struct bgp_node
*rn
;
2826 struct bgp_path_info
*pi
;
2829 struct prefix
*pp
= &p
;
2832 char buf
[PREFIX_STRLEN
];
2833 char buf1
[PREFIX_STRLEN
];
2835 memset(pp
, 0, sizeof(struct prefix
));
2836 ip_prefix_from_evpn_prefix(evp
, pp
);
2838 if (bgp_debug_zebra(NULL
)) {
2840 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2841 prefix2str(evp
, buf
, sizeof(buf
)),
2842 prefix2str(pp
, buf1
, sizeof(buf
)),
2843 vrf_id_to_name(bgp_vrf
->vrf_id
));
2846 /* Locate route within the VRF. */
2847 /* NOTE: There is no RD here. */
2848 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2850 safi
= SAFI_UNICAST
;
2851 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2854 safi
= SAFI_UNICAST
;
2855 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2861 /* Find matching route entry. */
2862 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2864 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2870 /* Process for route leaking. */
2871 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp_vrf
, pi
);
2873 bgp_aggregate_decrement(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2875 /* Mark entry for deletion */
2876 bgp_path_info_delete(rn
, pi
);
2878 /* Perform route selection and update zebra, if required. */
2879 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2881 /* Unlock route node. */
2882 bgp_unlock_node(rn
);
2888 * Uninstall route entry from the VNI routing table and send message
2889 * to zebra, if appropriate.
2891 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2892 struct prefix_evpn
*p
,
2893 struct bgp_path_info
*parent_pi
)
2895 struct bgp_node
*rn
;
2896 struct bgp_path_info
*pi
;
2899 /* Locate route within the VNI. */
2900 /* NOTE: There is no RD here. */
2901 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2905 /* Find matching route entry. */
2906 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2908 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2914 /* Mark entry for deletion */
2915 bgp_path_info_delete(rn
, pi
);
2917 /* Perform route selection and update zebra, if required. */
2918 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2920 /* Unlock route node. */
2921 bgp_unlock_node(rn
);
2927 * Given a prefix, see if it belongs to ES.
2929 static int is_prefix_matching_for_es(struct prefix_evpn
*p
,
2932 /* if not an ES route return false */
2933 if (p
->prefix
.route_type
!= BGP_EVPN_ES_ROUTE
)
2936 if (memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
)) == 0)
2943 * Given a route entry and a VRF, see if this route entry should be
2944 * imported into the VRF i.e., RTs match.
2946 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2947 struct bgp_path_info
*pi
)
2949 struct attr
*attr
= pi
->attr
;
2950 struct ecommunity
*ecom
;
2954 /* Route should have valid RT to be even considered. */
2955 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2958 ecom
= attr
->ecommunity
;
2959 if (!ecom
|| !ecom
->size
)
2962 /* For each extended community RT, see if it matches this VNI. If any RT
2963 * matches, we're done.
2965 for (i
= 0; i
< ecom
->size
; i
++) {
2967 uint8_t type
, sub_type
;
2968 struct ecommunity_val
*eval
;
2969 struct ecommunity_val eval_tmp
;
2970 struct vrf_irt_node
*irt
;
2972 /* Only deal with RTs */
2973 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2974 eval
= (struct ecommunity_val
*)(ecom
->val
2975 + (i
* ECOMMUNITY_SIZE
));
2978 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2981 /* See if this RT matches specified VNIs import RTs */
2982 irt
= lookup_vrf_import_rt(eval
);
2984 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2987 /* Also check for non-exact match. In this, we mask out the AS
2989 * only check on the local-admin sub-field. This is to
2991 * VNI as the RT for EBGP peering too.
2994 if (type
== ECOMMUNITY_ENCODE_AS
2995 || type
== ECOMMUNITY_ENCODE_AS4
2996 || type
== ECOMMUNITY_ENCODE_IP
) {
2997 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2998 mask_ecom_global_admin(&eval_tmp
, eval
);
2999 irt
= lookup_vrf_import_rt(&eval_tmp
);
3002 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
3010 * Given a route entry and a VNI, see if this route entry should be
3011 * imported into the VNI i.e., RTs match.
3013 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
3014 struct bgp_path_info
*pi
)
3016 struct attr
*attr
= pi
->attr
;
3017 struct ecommunity
*ecom
;
3021 /* Route should have valid RT to be even considered. */
3022 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3025 ecom
= attr
->ecommunity
;
3026 if (!ecom
|| !ecom
->size
)
3029 /* For each extended community RT, see if it matches this VNI. If any RT
3030 * matches, we're done.
3032 for (i
= 0; i
< ecom
->size
; i
++) {
3034 uint8_t type
, sub_type
;
3035 struct ecommunity_val
*eval
;
3036 struct ecommunity_val eval_tmp
;
3037 struct irt_node
*irt
;
3039 /* Only deal with RTs */
3040 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3041 eval
= (struct ecommunity_val
*)(ecom
->val
3042 + (i
* ECOMMUNITY_SIZE
));
3045 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3048 /* See if this RT matches specified VNIs import RTs */
3049 irt
= lookup_import_rt(bgp
, eval
);
3051 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
3054 /* Also check for non-exact match. In this, we mask out the AS
3056 * only check on the local-admin sub-field. This is to
3058 * VNI as the RT for EBGP peering too.
3061 if (type
== ECOMMUNITY_ENCODE_AS
3062 || type
== ECOMMUNITY_ENCODE_AS4
3063 || type
== ECOMMUNITY_ENCODE_IP
) {
3064 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3065 mask_ecom_global_admin(&eval_tmp
, eval
);
3066 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3069 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
3076 static int install_uninstall_routes_for_es(struct bgp
*bgp
,
3083 char buf
[PREFIX_STRLEN
];
3084 char buf1
[ESI_STR_LEN
];
3085 struct bgp_node
*rd_rn
, *rn
;
3086 struct bgp_table
*table
;
3087 struct bgp_path_info
*pi
;
3093 * Walk entire global routing table and evaluate routes which could be
3094 * imported into this VRF. Note that we need to loop through all global
3095 * routes to determine which route matches the import rt on vrf
3097 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
3098 rd_rn
= bgp_route_next(rd_rn
)) {
3099 table
= bgp_node_get_bgp_table_info(rd_rn
);
3103 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3104 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3106 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3109 * Consider "valid" remote routes applicable for
3112 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3113 && pi
->type
== ZEBRA_ROUTE_BGP
3114 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3117 if (!is_prefix_matching_for_es(evp
, es
))
3121 ret
= install_evpn_route_entry_in_es(
3124 ret
= uninstall_evpn_route_entry_in_es(
3130 "Failed to %s EVPN %s route in ESI %s",
3133 prefix2str(evp
, buf
,
3135 esi_to_str(&es
->esi
, buf1
,
3145 /* This API will scan evpn routes for checking attribute's rmac
3146 * macthes with bgp instance router mac. It avoid installing
3147 * route into bgp vrf table and remote rmac in bridge table.
3149 static int bgp_evpn_route_rmac_self_check(struct bgp
*bgp_vrf
,
3150 struct prefix_evpn
*evp
,
3151 struct bgp_path_info
*pi
)
3153 /* evpn route could have learnt prior to L3vni has come up,
3154 * perform rmac check before installing route and
3155 * remote router mac.
3156 * The route will be removed from global bgp table once
3157 * SVI comes up with MAC and stored in hash, triggers
3158 * bgp_mac_rescan_all_evpn_tables.
3160 if (memcmp(&bgp_vrf
->rmac
, &pi
->attr
->rmac
, ETH_ALEN
) == 0) {
3161 if (bgp_debug_update(pi
->peer
, NULL
, NULL
, 1)) {
3162 char buf1
[PREFIX_STRLEN
];
3163 char attr_str
[BUFSIZ
] = {0};
3165 bgp_dump_attr(pi
->attr
, attr_str
, BUFSIZ
);
3167 zlog_debug("%s: bgp %u prefix %s with attr %s - DENIED due to self mac",
3168 __func__
, bgp_vrf
->vrf_id
,
3169 prefix2str(evp
, buf1
, sizeof(buf1
)),
3180 * Install or uninstall mac-ip routes are appropriate for this
3183 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
3187 struct bgp_node
*rd_rn
, *rn
;
3188 struct bgp_table
*table
;
3189 struct bgp_path_info
*pi
;
3191 char buf
[PREFIX_STRLEN
];
3192 struct bgp
*bgp_evpn
= NULL
;
3196 bgp_evpn
= bgp_get_evpn();
3200 /* Walk entire global routing table and evaluate routes which could be
3201 * imported into this VRF. Note that we need to loop through all global
3202 * routes to determine which route matches the import rt on vrf
3204 for (rd_rn
= bgp_table_top(bgp_evpn
->rib
[afi
][safi
]); rd_rn
;
3205 rd_rn
= bgp_route_next(rd_rn
)) {
3206 table
= bgp_node_get_bgp_table_info(rd_rn
);
3210 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3211 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3213 /* if not mac-ip route skip this route */
3214 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3215 || evp
->prefix
.route_type
3216 == BGP_EVPN_IP_PREFIX_ROUTE
))
3219 /* if not a mac+ip route skip this route */
3220 if (!(is_evpn_prefix_ipaddr_v4(evp
)
3221 || is_evpn_prefix_ipaddr_v6(evp
)))
3224 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3226 /* Consider "valid" remote routes applicable for
3229 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3230 && pi
->type
== ZEBRA_ROUTE_BGP
3231 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3234 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
3235 if (bgp_evpn_route_rmac_self_check(
3240 ret
= install_evpn_route_entry_in_vrf(
3243 ret
= uninstall_evpn_route_entry_in_vrf(
3249 "Failed to %s EVPN %s route in VRF %s",
3252 prefix2str(evp
, buf
,
3267 * Install or uninstall routes of specified type that are appropriate for this
3270 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
3271 struct bgpevpn
*vpn
,
3272 bgp_evpn_route_type rtype
,
3277 struct bgp_node
*rd_rn
, *rn
;
3278 struct bgp_table
*table
;
3279 struct bgp_path_info
*pi
;
3285 /* Walk entire global routing table and evaluate routes which could be
3286 * imported into this VPN. Note that we cannot just look at the routes
3288 * the VNI's RD - remote routes applicable for this VNI could have any
3291 /* EVPN routes are a 2-level table. */
3292 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
3293 rd_rn
= bgp_route_next(rd_rn
)) {
3294 table
= bgp_node_get_bgp_table_info(rd_rn
);
3298 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3299 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3301 if (evp
->prefix
.route_type
!= rtype
)
3304 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3306 /* Consider "valid" remote routes applicable for
3308 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3309 && pi
->type
== ZEBRA_ROUTE_BGP
3310 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3313 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3315 ret
= install_evpn_route_entry(
3318 ret
= uninstall_evpn_route_entry(
3324 "%u: Failed to %s EVPN %s route in VNI %u",
3328 rtype
== BGP_EVPN_MAC_IP_ROUTE
3342 /* Install any existing remote ES routes applicable for this ES into its routing
3343 * table. This is invoked when ES comes up.
3345 static int install_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
3347 return install_uninstall_routes_for_es(bgp
, es
, 1);
3351 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3352 * is invoked upon l3vni-add or l3vni import rt change
3354 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3356 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3361 * Install any existing remote routes applicable for this VNI into its
3362 * routing table. This is invoked when a VNI becomes "live" or its Import
3365 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3369 /* Install type-3 routes followed by type-2 routes - the ones applicable
3372 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3377 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3381 /* uninstall routes from l3vni vrf. */
3382 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3384 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3389 * Uninstall any existing remote routes for this VNI. One scenario in which
3390 * this is invoked is upon an import RT change.
3392 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3396 /* Uninstall type-2 routes followed by type-3 routes - the ones
3400 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3405 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3409 /* Install or unistall route in ES */
3410 static int install_uninstall_route_in_es(struct bgp
*bgp
, struct evpnes
*es
,
3411 afi_t afi
, safi_t safi
,
3412 struct prefix_evpn
*evp
,
3413 struct bgp_path_info
*pi
, int install
)
3416 char buf
[ESI_STR_LEN
];
3419 ret
= install_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3421 ret
= uninstall_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3426 "%u: Failed to %s EVPN %s route in ESI %s", bgp
->vrf_id
,
3427 install
? "install" : "uninstall", "ES",
3428 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)));
3435 * Install or uninstall route in matching VRFs (list).
3437 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3438 safi_t safi
, struct prefix_evpn
*evp
,
3439 struct bgp_path_info
*pi
,
3440 struct list
*vrfs
, int install
)
3442 char buf
[PREFIX2STR_BUFFER
];
3443 struct bgp
*bgp_vrf
;
3444 struct listnode
*node
, *nnode
;
3446 /* Only type-2/type-5 routes go into a VRF */
3447 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3448 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3451 /* if it is type-2 route and not a mac+ip route skip this route */
3452 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3453 && !(is_evpn_prefix_ipaddr_v4(evp
)
3454 || is_evpn_prefix_ipaddr_v6(evp
)))
3457 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3461 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3463 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3467 flog_err(EC_BGP_EVPN_FAIL
,
3468 "%u: Failed to %s prefix %s in VRF %s",
3470 install
? "install" : "uninstall",
3471 prefix2str(evp
, buf
, sizeof(buf
)),
3472 vrf_id_to_name(bgp_vrf
->vrf_id
));
3481 * Install or uninstall route in matching VNIs (list).
3483 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3484 safi_t safi
, struct prefix_evpn
*evp
,
3485 struct bgp_path_info
*pi
,
3486 struct list
*vnis
, int install
)
3488 struct bgpevpn
*vpn
;
3489 struct listnode
*node
, *nnode
;
3491 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3494 if (!is_vni_live(vpn
))
3498 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3500 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3503 flog_err(EC_BGP_EVPN_FAIL
,
3504 "%u: Failed to %s EVPN %s route in VNI %u",
3505 bgp
->vrf_id
, install
? "install" : "uninstall",
3506 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3518 * Install or uninstall route for appropriate VNIs/ESIs.
3520 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3522 struct bgp_path_info
*pi
, int import
)
3524 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3525 struct attr
*attr
= pi
->attr
;
3526 struct ecommunity
*ecom
;
3531 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3532 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3533 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3534 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3535 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3538 /* If we don't have Route Target, nothing much to do. */
3539 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3542 ecom
= attr
->ecommunity
;
3543 if (!ecom
|| !ecom
->size
)
3546 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3547 * attached to the route */
3548 for (i
= 0; i
< ecom
->size
; i
++) {
3550 uint8_t type
, sub_type
;
3551 struct ecommunity_val
*eval
;
3552 struct ecommunity_val eval_tmp
;
3553 struct irt_node
*irt
; /* import rt for l2vni */
3554 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3557 /* Only deal with RTs */
3558 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3559 eval
= (struct ecommunity_val
*)(ecom
->val
3560 + (i
* ECOMMUNITY_SIZE
));
3563 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3567 * macip routes (type-2) are imported into VNI and VRF tables.
3568 * IMET route is imported into VNI table.
3569 * prefix routes are imported into VRF table.
3571 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3572 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3573 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3575 irt
= lookup_import_rt(bgp
, eval
);
3577 install_uninstall_route_in_vnis(
3578 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3581 vrf_irt
= lookup_vrf_import_rt(eval
);
3583 install_uninstall_route_in_vrfs(
3584 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3587 /* Also check for non-exact match.
3588 * In this, we mask out the AS and
3589 * only check on the local-admin sub-field.
3590 * This is to facilitate using
3591 * VNI as the RT for EBGP peering too.
3595 if (type
== ECOMMUNITY_ENCODE_AS
3596 || type
== ECOMMUNITY_ENCODE_AS4
3597 || type
== ECOMMUNITY_ENCODE_IP
) {
3598 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3599 mask_ecom_global_admin(&eval_tmp
, eval
);
3600 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3601 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3605 install_uninstall_route_in_vnis(
3606 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3609 install_uninstall_route_in_vrfs(
3610 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3614 /* es route is imported into the es table */
3615 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3617 /* we will match based on the entire esi to avoid
3618 * imoort of an es route for esi2 into esi1
3620 es
= bgp_evpn_lookup_es(bgp
, &evp
->prefix
.es_addr
.esi
);
3621 if (es
&& is_es_local(es
))
3622 install_uninstall_route_in_es(
3623 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3631 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3634 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3636 /* Delete ipv4 default route and withdraw from peers */
3637 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3638 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3639 SAFI_UNICAST
, false);
3641 /* delete all ipv4 routes and withdraw from peers */
3642 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3643 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3645 /* Delete ipv6 default route and withdraw from peers */
3646 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3647 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3648 SAFI_UNICAST
, false);
3650 /* delete all ipv6 routes and withdraw from peers */
3651 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3652 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3656 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3659 void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3661 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
3663 bgp_evpn
= bgp_get_evpn();
3667 /* update all ipv4 routes */
3668 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3669 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3671 /* update ipv4 default route and withdraw from peers */
3672 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3673 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3674 SAFI_UNICAST
, true);
3676 /* update all ipv6 routes */
3677 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3678 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3680 /* update ipv6 default route and withdraw from peers */
3681 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3682 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3683 SAFI_UNICAST
, true);
3688 * update and advertise local routes for a VRF as type-5 routes.
3689 * This is invoked upon RD change for a VRF. Note taht the processing is only
3690 * done in the global route table using the routes which already exist in the
3693 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3695 /* skip if the RD is configured */
3696 if (is_vrf_rd_configured(bgp_vrf
))
3699 /* derive the RD for the VRF based on new router-id */
3700 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3702 /* update advertise ipv4|ipv6 routes as type-5 routes */
3703 update_advertise_vrf_routes(bgp_vrf
);
3707 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3708 * This is invoked upon VRF RD change. The processing is done only from global
3711 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3713 /* skip if the RD is configured */
3714 if (is_vrf_rd_configured(bgp_vrf
))
3717 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3718 delete_withdraw_vrf_routes(bgp_vrf
);
3722 * Update and advertise local routes for a VNI. Invoked upon router-id
3723 * change. Note that the processing is done only on the global route table
3724 * using routes that already exist in the per-VNI table.
3726 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3728 struct prefix_evpn p
;
3729 struct bgp_node
*rn
, *global_rn
;
3730 struct bgp_path_info
*pi
, *global_pi
;
3732 afi_t afi
= AFI_L2VPN
;
3733 safi_t safi
= SAFI_EVPN
;
3735 /* Locate type-3 route for VNI in the per-VNI table and use its
3736 * attributes to create and advertise the type-3 route for this VNI
3737 * in the global table.
3739 * RT-3 only if doing head-end replication
3741 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3742 == VXLAN_FLOOD_HEAD_END_REPL
) {
3743 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3744 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3745 if (!rn
) /* unexpected */
3747 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3748 if (pi
->peer
== bgp
->peer_self
&&
3749 pi
->type
== ZEBRA_ROUTE_BGP
3750 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3752 if (!pi
) /* unexpected */
3756 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3757 (struct prefix
*)&p
, &vpn
->prd
);
3758 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
,
3759 1, &pi
, 0, mac_mobility_seqnum(attr
));
3761 /* Schedule for processing and unlock node. */
3762 bgp_process(bgp
, global_rn
, afi
, safi
);
3763 bgp_unlock_node(global_rn
);
3766 /* Now, walk this VNI's route table and use the route and its attribute
3767 * to create and schedule route in global table.
3769 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
3770 rn
= bgp_route_next(rn
)) {
3771 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3773 /* Identify MAC-IP local routes. */
3774 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3777 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3778 if (pi
->peer
== bgp
->peer_self
3779 && pi
->type
== ZEBRA_ROUTE_BGP
3780 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3785 /* Create route in global routing table using this route entry's
3789 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3790 (struct prefix
*)evp
, &vpn
->prd
);
3792 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
3794 mac_mobility_seqnum(attr
));
3796 /* Schedule for processing and unlock node. */
3797 bgp_process(bgp
, global_rn
, afi
, safi
);
3798 bgp_unlock_node(global_rn
);
3805 * Delete (and withdraw) local routes for a VNI - only from the global
3806 * table. Invoked upon router-id change.
3808 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3811 struct prefix_evpn p
;
3812 struct bgp_node
*global_rn
;
3813 struct bgp_path_info
*pi
;
3814 afi_t afi
= AFI_L2VPN
;
3815 safi_t safi
= SAFI_EVPN
;
3817 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3818 * for this VNI - from the global table.
3820 ret
= delete_global_type2_routes(bgp
, vpn
);
3824 /* Remove type-3 route for this VNI from global table. */
3825 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3826 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3827 (struct prefix
*)&p
, &vpn
->prd
);
3829 /* Delete route entry in the global EVPN table. */
3830 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
3832 /* Schedule for processing - withdraws to peers happen from
3836 bgp_process(bgp
, global_rn
, afi
, safi
);
3837 bgp_unlock_node(global_rn
);
3844 * Handle router-id change. Update and advertise local routes corresponding
3845 * to this VNI from peers. Note that this is invoked after updating the
3846 * router-id. The routes in the per-VNI table are used to create routes in
3847 * the global table and schedule them.
3849 static void update_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3851 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3853 /* Skip VNIs with configured RD. */
3854 if (is_rd_configured(vpn
))
3857 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3858 update_advertise_vni_routes(bgp
, vpn
);
3862 * Handle router-id change. Delete and withdraw local routes corresponding
3863 * to this VNI from peers. Note that this is invoked prior to updating
3864 * the router-id and is done only on the global route table, the routes
3865 * are needed in the per-VNI table to re-advertise with new router id.
3867 static void withdraw_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3869 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3871 /* Skip VNIs with configured RD. */
3872 if (is_rd_configured(vpn
))
3875 delete_withdraw_vni_routes(bgp
, vpn
);
3879 * Create RT-3 for a VNI and schedule for processing and advertisement.
3880 * This is invoked upon flooding mode changing to head-end replication.
3882 static void create_advertise_type3(struct hash_bucket
*bucket
, void *data
)
3884 struct bgpevpn
*vpn
= bucket
->data
;
3885 struct bgp
*bgp
= data
;
3886 struct prefix_evpn p
;
3888 if (!vpn
|| !is_vni_live(vpn
) ||
3889 bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3890 != VXLAN_FLOOD_HEAD_END_REPL
)
3893 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3894 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0))
3895 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3896 "Type3 route creation failure for VNI %u", vpn
->vni
);
3900 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3901 * This is invoked upon flooding mode changing to drop BUM packets.
3903 static void delete_withdraw_type3(struct hash_bucket
*bucket
, void *data
)
3905 struct bgpevpn
*vpn
= bucket
->data
;
3906 struct bgp
*bgp
= data
;
3907 struct prefix_evpn p
;
3909 if (!vpn
|| !is_vni_live(vpn
))
3912 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3913 delete_evpn_route(bgp
, vpn
, &p
);
3917 * Process received EVPN type-2 route (advertise or withdraw).
3919 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3920 struct attr
*attr
, uint8_t *pfx
, int psize
,
3921 uint32_t addpath_id
)
3923 struct prefix_rd prd
;
3924 struct prefix_evpn p
;
3925 struct bgp_route_evpn evpn
;
3927 uint8_t macaddr_len
;
3928 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
3929 uint32_t num_labels
= 0;
3933 /* Type-2 route should be either 33, 37 or 49 bytes or an
3934 * additional 3 bytes if there is a second label (VNI):
3935 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3936 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3937 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3939 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3940 && psize
!= 40 && psize
!= 52) {
3941 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3942 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3943 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3947 memset(&evpn
, 0, sizeof(evpn
));
3949 /* Make prefix_rd */
3950 prd
.family
= AF_UNSPEC
;
3952 memcpy(&prd
.val
, pfx
, 8);
3955 /* Make EVPN prefix. */
3956 memset(&p
, 0, sizeof(struct prefix_evpn
));
3958 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3959 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3961 /* Copy Ethernet Seg Identifier */
3962 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
3965 /* Copy Ethernet Tag */
3966 memcpy(ð_tag
, pfx
, 4);
3967 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3970 /* Get the MAC Addr len */
3971 macaddr_len
= *pfx
++;
3973 /* Get the MAC Addr */
3974 if (macaddr_len
== (ETH_ALEN
* 8)) {
3975 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
3979 EC_BGP_EVPN_ROUTE_INVALID
,
3980 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3981 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3987 ipaddr_len
= *pfx
++;
3988 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3989 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3991 EC_BGP_EVPN_ROUTE_INVALID
,
3992 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3993 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3998 ipaddr_len
/= 8; /* Convert to bytes. */
3999 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
4002 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
4006 /* Get the VNI(s). Stored as bytes here. */
4008 memset(label
, 0, sizeof(label
));
4009 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
4010 pfx
+= BGP_LABEL_BYTES
;
4011 psize
-= (33 + ipaddr_len
);
4012 /* Do we have a second VNI? */
4015 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
4017 * If in future, we are required to access additional fields,
4018 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
4023 /* Process the route. */
4025 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4026 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4027 &prd
, &label
[0], num_labels
, 0, &evpn
);
4029 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4030 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4031 &prd
, &label
[0], num_labels
, &evpn
);
4036 * Process received EVPN type-3 route (advertise or withdraw).
4038 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4039 struct attr
*attr
, uint8_t *pfx
, int psize
,
4040 uint32_t addpath_id
)
4042 struct prefix_rd prd
;
4043 struct prefix_evpn p
;
4048 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
4049 * IP len (1) and IP (4 or 16).
4051 if (psize
!= 17 && psize
!= 29) {
4052 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4053 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
4054 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4058 /* If PMSI is present, log if it is anything other than IR.
4059 * Note: We just simply ignore the values as it is not clear if
4060 * doing anything else is better.
4063 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
4064 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
&&
4065 attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_PIM_SM
) {
4066 flog_warn(EC_BGP_EVPN_PMSI_PRESENT
,
4067 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
4068 peer
->bgp
->vrf_id
, peer
->host
,
4069 attr
->pmsi_tnl_type
);
4073 /* Make prefix_rd */
4074 prd
.family
= AF_UNSPEC
;
4076 memcpy(&prd
.val
, pfx
, 8);
4079 /* Make EVPN prefix. */
4080 memset(&p
, 0, sizeof(struct prefix_evpn
));
4082 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4083 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
4085 /* Copy Ethernet Tag */
4086 memcpy(ð_tag
, pfx
, 4);
4087 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
4091 ipaddr_len
= *pfx
++;
4092 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
4093 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
4094 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
4097 EC_BGP_EVPN_ROUTE_INVALID
,
4098 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
4099 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4103 /* Process the route. */
4105 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4106 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4107 &prd
, NULL
, 0, 0, NULL
);
4109 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4110 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4111 &prd
, NULL
, 0, NULL
);
4116 * Process received EVPN type-4 route (advertise or withdraw).
4118 static int process_type4_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4119 struct attr
*attr
, uint8_t *pfx
, int psize
,
4120 uint32_t addpath_id
)
4125 struct in_addr vtep_ip
;
4126 struct prefix_rd prd
;
4127 struct prefix_evpn p
;
4129 /* Type-4 route should be either 23 or 35 bytes
4130 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
4132 if (psize
!= 23 && psize
!= 35) {
4133 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4134 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
4135 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4139 /* Make prefix_rd */
4140 prd
.family
= AF_UNSPEC
;
4142 memcpy(&prd
.val
, pfx
, 8);
4146 memcpy(&esi
, pfx
, ESI_BYTES
);
4151 ipaddr_len
= *pfx
++;
4152 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
4153 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
4156 EC_BGP_EVPN_ROUTE_INVALID
,
4157 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
4158 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4162 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
4163 /* Process the route. */
4165 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4166 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4167 &prd
, NULL
, 0, 0, NULL
);
4169 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4170 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4171 &prd
, NULL
, 0, NULL
);
4178 * Process received EVPN type-5 route (advertise or withdraw).
4180 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4181 struct attr
*attr
, uint8_t *pfx
, int psize
,
4182 uint32_t addpath_id
)
4184 struct prefix_rd prd
;
4185 struct prefix_evpn p
;
4186 struct bgp_route_evpn evpn
;
4189 mpls_label_t label
; /* holds the VNI as in the packet */
4192 /* Type-5 route should be 34 or 58 bytes:
4193 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
4194 * GW (4 or 16) and VNI (3).
4195 * Note that the IP and GW should both be IPv4 or both IPv6.
4197 if (psize
!= 34 && psize
!= 58) {
4198 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4199 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
4200 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4204 /* Make prefix_rd */
4205 prd
.family
= AF_UNSPEC
;
4207 memcpy(&prd
.val
, pfx
, 8);
4210 /* Make EVPN prefix. */
4211 memset(&p
, 0, sizeof(struct prefix_evpn
));
4213 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4214 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
4216 /* Additional information outside of prefix - ESI and GW IP */
4217 memset(&evpn
, 0, sizeof(evpn
));
4220 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
4223 /* Fetch Ethernet Tag. */
4224 memcpy(ð_tag
, pfx
, 4);
4225 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
4228 /* Fetch IP prefix length. */
4230 if (ippfx_len
> IPV6_MAX_BITLEN
) {
4232 EC_BGP_EVPN_ROUTE_INVALID
,
4233 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4234 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
4237 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
4239 /* Determine IPv4 or IPv6 prefix */
4240 /* Since the address and GW are from the same family, this just becomes
4241 * a simple check on the total size.
4244 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
4245 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
4247 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
4250 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
4251 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
4253 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
4257 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4258 memset(&label
, 0, sizeof(label
));
4259 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
4262 * If in future, we are required to access additional fields,
4263 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4267 /* Process the route. */
4269 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4270 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4271 &prd
, &label
, 1, 0, &evpn
);
4273 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4274 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4275 &prd
, &label
, 1, &evpn
);
4280 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
4281 struct prefix_rd
*prd
, mpls_label_t
*label
,
4282 uint32_t num_labels
, struct attr
*attr
)
4286 struct evpn_addr
*p_evpn_p
;
4288 memset(&temp
, 0, 16);
4289 if (p
->family
!= AF_EVPN
)
4291 p_evpn_p
= &(p
->u
.prefix_evpn
);
4293 /* len denites the total len of IP and GW-IP in the route
4294 IP and GW-IP have to be both ipv4 or ipv6
4296 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4297 len
= 8; /* IP and GWIP are both ipv4 */
4299 len
= 32; /* IP and GWIP are both ipv6 */
4300 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4301 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4302 stream_put(s
, prd
->val
, 8);
4304 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
4306 stream_put(s
, &temp
, 10);
4307 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4308 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4309 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4310 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4312 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4314 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4316 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
4318 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
4320 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4321 stream_put_ipv4(s
, 0);
4323 stream_put(s
, &temp
, 16);
4327 stream_put(s
, label
, 3);
4333 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4335 static void cleanup_vni_on_disable(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4337 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4339 /* Remove EVPN routes and schedule for processing. */
4340 delete_routes_for_vni(bgp
, vpn
);
4342 /* Clear "live" flag and see if hash needs to be freed. */
4343 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4344 if (!is_vni_configured(vpn
))
4345 bgp_evpn_free(bgp
, vpn
);
4349 * Free a VNI entry; iterator function called during cleanup.
4351 static void free_vni_entry(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4353 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4355 delete_all_vni_routes(bgp
, vpn
);
4356 bgp_evpn_free(bgp
, vpn
);
4360 * Derive AUTO import RT for BGP VRF - L3VNI
4362 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4364 struct bgp
*bgp_evpn
= NULL
;
4366 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4367 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4370 bgp_evpn
= bgp_get_evpn();
4373 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4377 * Delete AUTO import RT from BGP VRF - L3VNI
4379 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4381 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4385 * Derive AUTO export RT for BGP VRF - L3VNI
4387 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4389 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4390 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4394 * Delete AUTO export RT from BGP VRF - L3VNI
4396 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4398 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4401 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4403 struct bgp
*bgp_evpn
= NULL
;
4404 struct listnode
*node
= NULL
;
4405 struct bgpevpn
*vpn
= NULL
;
4407 bgp_evpn
= bgp_get_evpn();
4411 /* update all type-5 routes */
4412 update_advertise_vrf_routes(bgp_vrf
);
4414 /* update all type-2 routes */
4415 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4416 update_routes_for_vni(bgp_evpn
, vpn
);
4420 * Handle autort change for a given VNI.
4422 static void update_autort_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4424 struct bgpevpn
*vpn
= bucket
->data
;
4426 if (!is_import_rt_configured(vpn
)) {
4427 if (is_vni_live(vpn
))
4428 bgp_evpn_uninstall_routes(bgp
, vpn
);
4429 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4430 list_delete_all_node(vpn
->import_rtl
);
4431 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4432 if (is_vni_live(vpn
))
4433 bgp_evpn_install_routes(bgp
, vpn
);
4435 if (!is_export_rt_configured(vpn
)) {
4436 list_delete_all_node(vpn
->export_rtl
);
4437 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4438 if (is_vni_live(vpn
))
4439 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4447 /* withdraw type-5 route corresponding to ip prefix */
4448 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4449 afi_t afi
, safi_t safi
)
4452 struct prefix_evpn evp
;
4453 char buf
[PREFIX_STRLEN
];
4455 build_type5_prefix_from_ip_prefix(&evp
, p
);
4456 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4459 EC_BGP_EVPN_ROUTE_DELETE
,
4460 "%u failed to delete type-5 route for prefix %s in vrf %s",
4461 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4462 vrf_id_to_name(bgp_vrf
->vrf_id
));
4466 /* withdraw all type-5 routes for an address family */
4467 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4469 struct bgp_table
*table
= NULL
;
4470 struct bgp_node
*rn
= NULL
;
4471 struct bgp_path_info
*pi
;
4473 table
= bgp_vrf
->rib
[afi
][safi
];
4474 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4475 /* Only care about "selected" routes. Also ensure that
4476 * these are routes that are injectable into EVPN.
4478 /* TODO: Support for AddPath for EVPN. */
4479 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4480 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4481 && is_route_injectable_into_evpn(pi
)) {
4482 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
4491 * evpn - enable advertisement of default g/w
4493 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
4494 safi_t safi
, bool add
)
4496 struct prefix ip_prefix
;
4498 /* form the default prefix 0.0.0.0/0 */
4499 memset(&ip_prefix
, 0, sizeof(struct prefix
));
4500 ip_prefix
.family
= afi2family(afi
);
4503 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
4506 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
4513 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4514 * to this function correspond to those of the source IP prefix (best
4515 * path in the case of the attr. In the case of a local prefix (when we
4516 * are advertising local subnets), the src_attr will be NULL.
4518 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4519 struct attr
*src_attr
, afi_t afi
,
4523 struct prefix_evpn evp
;
4524 char buf
[PREFIX_STRLEN
];
4526 build_type5_prefix_from_ip_prefix(&evp
, p
);
4527 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4529 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4530 "%u: Failed to create type-5 route for prefix %s",
4531 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4534 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4535 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4536 * advertisement is enabled.
4538 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4541 struct bgp_table
*table
= NULL
;
4542 struct bgp_node
*rn
= NULL
;
4543 struct bgp_path_info
*pi
;
4545 table
= bgp_vrf
->rib
[afi
][safi
];
4546 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4547 /* Need to identify the "selected" route entry to use its
4548 * attribute. Also, ensure that the route is injectable
4550 * TODO: Support for AddPath for EVPN.
4552 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4553 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4554 && is_route_injectable_into_evpn(pi
)) {
4556 /* apply the route-map */
4557 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4558 route_map_result_t ret
;
4560 ret
= route_map_apply(
4561 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4563 &rn
->p
, RMAP_BGP
, pi
);
4564 if (ret
== RMAP_DENYMATCH
)
4567 bgp_evpn_advertise_type5_route(
4568 bgp_vrf
, &rn
->p
, pi
->attr
, afi
, safi
);
4575 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4577 struct listnode
*node
, *nnode
, *node_to_del
;
4578 struct ecommunity
*ecom
, *ecom_auto
;
4579 struct ecommunity_val eval
;
4581 if (bgp
->advertise_autort_rfc8365
)
4582 vni
|= EVPN_AUTORT_VXLAN
;
4583 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4585 ecom_auto
= ecommunity_new();
4586 ecommunity_add_val(ecom_auto
, &eval
);
4589 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4590 if (ecommunity_match(ecom
, ecom_auto
)) {
4591 ecommunity_free(&ecom
);
4597 list_delete_node(rtl
, node_to_del
);
4599 ecommunity_free(&ecom_auto
);
4602 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4603 struct ecommunity
*ecomadd
)
4605 /* uninstall routes from vrf */
4606 if (is_l3vni_live(bgp_vrf
))
4607 uninstall_routes_for_vrf(bgp_vrf
);
4609 /* Cleanup the RT to VRF mapping */
4610 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4612 /* Remove auto generated RT */
4613 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4615 /* Add the newly configured RT to RT list */
4616 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4617 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4619 /* map VRF to its RTs and install routes matching the new RTs */
4620 if (is_l3vni_live(bgp_vrf
)) {
4621 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4622 install_routes_for_vrf(bgp_vrf
);
4626 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4627 struct ecommunity
*ecomdel
)
4629 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4630 struct ecommunity
*ecom
= NULL
;
4632 /* uninstall routes from vrf */
4633 if (is_l3vni_live(bgp_vrf
))
4634 uninstall_routes_for_vrf(bgp_vrf
);
4636 /* Cleanup the RT to VRF mapping */
4637 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4639 /* remove the RT from the RT list */
4640 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4641 if (ecommunity_match(ecom
, ecomdel
)) {
4642 ecommunity_free(&ecom
);
4649 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4651 assert(bgp_vrf
->vrf_import_rtl
);
4652 /* fallback to auto import rt, if this was the last RT */
4653 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4654 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4655 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4658 /* map VRFs to its RTs and install routes matching this new RT */
4659 if (is_l3vni_live(bgp_vrf
)) {
4660 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4661 install_routes_for_vrf(bgp_vrf
);
4665 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4666 struct ecommunity
*ecomadd
)
4668 /* remove auto-generated RT */
4669 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4671 /* Add the new RT to the RT list */
4672 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4673 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4675 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4678 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4679 struct ecommunity
*ecomdel
)
4681 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4682 struct ecommunity
*ecom
= NULL
;
4684 /* Remove the RT from the RT list */
4685 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4686 if (ecommunity_match(ecom
, ecomdel
)) {
4687 ecommunity_free(&ecom
);
4694 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4697 * Temporary assert to make SA happy.
4698 * The ALL_LIST_ELEMENTS macro above has a NULL check
4699 * which means that SA is going to complain about
4700 * the list_isempty call, which doesn't NULL check.
4701 * So until we get this situation cleaned up, here
4704 assert(bgp_vrf
->vrf_export_rtl
);
4706 /* fall back to auto-generated RT if this was the last RT */
4707 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4708 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4709 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4712 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4716 * Handle change to BGP router id. This is invoked twice by the change
4717 * handler, first before the router id has been changed and then after
4718 * the router id has been changed. The first invocation will result in
4719 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4720 * will result in the routes being re-advertised.
4722 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4724 struct listnode
*node
;
4725 struct bgp
*bgp_vrf
;
4729 /* delete and withdraw all the type-5 routes
4730 stored in the global table for this vrf
4732 withdraw_router_id_vrf(bgp
);
4734 /* delete all the VNI routes (type-2/type-3) routes for all the
4737 hash_iterate(bgp
->vnihash
,
4738 (void (*)(struct hash_bucket
*,
4739 void *))withdraw_router_id_vni
,
4742 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4743 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4744 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4745 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4747 bgp_vrf
->evpn_info
->pip_ip
.s_addr
4753 /* Assign new default instance router-id */
4754 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4755 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4756 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4757 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4759 bgp_vrf
->evpn_info
->pip_ip
=
4761 /* advertise type-5 routes with
4764 update_advertise_vrf_routes(bgp_vrf
);
4769 /* advertise all routes in the vrf as type-5 routes with the new
4772 update_router_id_vrf(bgp
);
4774 /* advertise all the VNI routes (type-2/type-3) routes with the
4777 hash_iterate(bgp
->vnihash
,
4778 (void (*)(struct hash_bucket
*,
4779 void *))update_router_id_vni
,
4785 * Handle change to auto-RT algorithm - update and advertise local routes.
4787 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4789 hash_iterate(bgp
->vnihash
,
4790 (void (*)(struct hash_bucket
*,
4791 void*))update_autort_vni
,
4796 * Handle change to export RT - update and advertise local routes.
4798 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4800 return update_routes_for_vni(bgp
, vpn
);
4803 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4806 delete_withdraw_vrf_routes(bgp_vrf
);
4808 update_advertise_vrf_routes(bgp_vrf
);
4812 * Handle change to RD. This is invoked twice by the change handler,
4813 * first before the RD has been changed and then after the RD has
4814 * been changed. The first invocation will result in local routes
4815 * of this VNI being deleted and withdrawn and the next will result
4816 * in the routes being re-advertised.
4818 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4822 delete_withdraw_vni_routes(bgp
, vpn
);
4824 update_advertise_vni_routes(bgp
, vpn
);
4828 * Install routes for this VNI. Invoked upon change to Import RT.
4830 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4832 return install_routes_for_vni(bgp
, vpn
);
4836 * Uninstall all routes installed for this VNI. Invoked upon change
4839 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4841 return uninstall_routes_for_vni(bgp
, vpn
);
4845 * TODO: Hardcoded for a maximum of 2 VNIs right now
4847 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4852 vni1
= label2vni(label
);
4853 if (num_labels
== 2) {
4854 vni2
= label2vni(label
+ 1);
4855 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4857 snprintf(buf
, len
, "%u", vni1
);
4862 * Function to convert evpn route to json format.
4863 * NOTE: We don't use prefix2str as the output here is a bit different.
4865 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
4867 char buf1
[ETHER_ADDR_STRLEN
];
4868 char buf2
[PREFIX2STR_BUFFER
];
4875 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4877 switch (p
->prefix
.route_type
) {
4878 case BGP_EVPN_MAC_IP_ROUTE
:
4879 json_object_int_add(json
, "ethTag",
4880 p
->prefix
.macip_addr
.eth_tag
);
4881 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4882 json_object_string_add(json
, "mac",
4883 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4886 if (!is_evpn_prefix_ipaddr_none(p
)) {
4887 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
:
4889 prefixlen
= (family
== AF_INET
) ?
4890 IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
;
4891 inet_ntop(family
, &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4892 buf2
, PREFIX2STR_BUFFER
);
4893 json_object_int_add(json
, "ipLen", prefixlen
);
4894 json_object_string_add(json
, "ip", buf2
);
4898 case BGP_EVPN_IMET_ROUTE
:
4899 json_object_int_add(json
, "ethTag",
4900 p
->prefix
.imet_addr
.eth_tag
);
4901 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4902 prefixlen
= (family
== AF_INET
) ? IPV4_MAX_BITLEN
:
4904 inet_ntop(family
, &p
->prefix
.imet_addr
.ip
.ip
.addr
, buf2
,
4906 json_object_int_add(json
, "ipLen", prefixlen
);
4907 json_object_string_add(json
, "ip", buf2
);
4910 case BGP_EVPN_IP_PREFIX_ROUTE
:
4911 json_object_int_add(json
, "ethTag",
4912 p
->prefix
.prefix_addr
.eth_tag
);
4913 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4914 inet_ntop(family
, &p
->prefix
.prefix_addr
.ip
.ip
.addr
,
4915 buf2
, sizeof(buf2
));
4916 json_object_int_add(json
, "ipLen",
4917 p
->prefix
.prefix_addr
.ip_prefix_length
);
4918 json_object_string_add(json
, "ip", buf2
);
4927 * Function to convert evpn route to string.
4928 * NOTE: We don't use prefix2str as the output here is a bit different.
4930 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
4932 char buf1
[ETHER_ADDR_STRLEN
];
4933 char buf2
[PREFIX2STR_BUFFER
];
4934 char buf3
[ESI_STR_LEN
];
4936 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4937 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
4938 p
->prefix
.imet_addr
.eth_tag
,
4939 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4941 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4942 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4943 if (is_evpn_prefix_ipaddr_none(p
))
4944 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4945 p
->prefix
.route_type
,
4946 p
->prefix
.macip_addr
.eth_tag
,
4948 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4953 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4955 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
4956 p
->prefix
.route_type
,
4957 p
->prefix
.macip_addr
.eth_tag
,
4959 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4961 family
== AF_INET
? IPV4_MAX_BITLEN
4964 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4966 PREFIX2STR_BUFFER
));
4968 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
4969 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4970 p
->prefix
.route_type
,
4971 p
->prefix
.prefix_addr
.eth_tag
,
4972 p
->prefix
.prefix_addr
.ip_prefix_length
,
4973 is_evpn_prefix_ipaddr_v4(p
)
4974 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
4975 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
4976 } else if (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
4977 snprintf(buf
, len
, "[%d]:[%s]:[%d]:[%s]",
4978 p
->prefix
.route_type
,
4979 esi_to_str(&p
->prefix
.es_addr
.esi
, buf3
, sizeof(buf3
)),
4980 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4982 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
));
4984 /* For EVPN route types not supported yet. */
4985 snprintf(buf
, len
, "(unsupported route type %d)",
4986 p
->prefix
.route_type
);
4993 * Encode EVPN prefix in Update (MP_REACH)
4995 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
4996 struct prefix_rd
*prd
, mpls_label_t
*label
,
4997 uint32_t num_labels
, struct attr
*attr
,
4998 int addpath_encode
, uint32_t addpath_tx_id
)
5000 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
5001 int len
, ipa_len
= 0;
5004 stream_putl(s
, addpath_tx_id
);
5007 stream_putc(s
, evp
->prefix
.route_type
);
5009 switch (evp
->prefix
.route_type
) {
5010 case BGP_EVPN_MAC_IP_ROUTE
:
5011 if (is_evpn_prefix_ipaddr_v4(evp
))
5012 ipa_len
= IPV4_MAX_BYTELEN
;
5013 else if (is_evpn_prefix_ipaddr_v6(evp
))
5014 ipa_len
= IPV6_MAX_BYTELEN
;
5015 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
5016 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
5017 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
5019 stream_putc(s
, len
);
5020 stream_put(s
, prd
->val
, 8); /* RD */
5022 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
5024 stream_put(s
, 0, 10);
5025 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
5026 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
5027 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
5028 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
5029 if (ipa_len
) /* IP */
5030 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
5032 /* 1st label is the L2 VNI */
5033 stream_put(s
, label
, BGP_LABEL_BYTES
);
5034 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
5035 if (ipa_len
&& num_labels
> 1)
5036 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
5039 case BGP_EVPN_IMET_ROUTE
:
5040 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
5041 stream_put(s
, prd
->val
, 8); /* RD */
5042 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
5043 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
5044 /* Originating Router's IP Addr */
5045 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
5048 case BGP_EVPN_ES_ROUTE
:
5049 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
5050 stream_put(s
, prd
->val
, 8); /* RD */
5051 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
5052 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
5054 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
5057 case BGP_EVPN_IP_PREFIX_ROUTE
:
5058 /* TODO: AddPath support. */
5059 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
5067 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
5068 struct bgp_nlri
*packet
, int withdraw
)
5074 uint32_t addpath_id
;
5075 int addpath_encoded
;
5080 /* Start processing the NLRI - there may be multiple in the MP_REACH */
5082 lim
= pnt
+ packet
->length
;
5084 safi
= packet
->safi
;
5088 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
5089 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5090 PEER_CAP_ADDPATH_AF_TX_RCV
));
5092 for (; pnt
< lim
; pnt
+= psize
) {
5093 /* Clear prefix structure. */
5094 memset(&p
, 0, sizeof(struct prefix
));
5096 /* Deal with path-id if AddPath is supported. */
5097 if (addpath_encoded
) {
5098 /* When packet overflow occurs return immediately. */
5099 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
5100 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5102 addpath_id
= ntohl(*((uint32_t *)pnt
));
5103 pnt
+= BGP_ADDPATH_ID_LEN
;
5106 /* All EVPN NLRI types start with type and length. */
5108 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE
;
5113 /* When packet overflow occur return immediately. */
5114 if (pnt
+ psize
> lim
)
5115 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5118 case BGP_EVPN_MAC_IP_ROUTE
:
5119 if (process_type2_route(peer
, afi
, safi
,
5120 withdraw
? NULL
: attr
, pnt
,
5121 psize
, addpath_id
)) {
5124 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
5125 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5126 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE
;
5130 case BGP_EVPN_IMET_ROUTE
:
5131 if (process_type3_route(peer
, afi
, safi
,
5132 withdraw
? NULL
: attr
, pnt
,
5133 psize
, addpath_id
)) {
5136 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
5137 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5138 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE
;
5142 case BGP_EVPN_ES_ROUTE
:
5143 if (process_type4_route(peer
, afi
, safi
,
5144 withdraw
? NULL
: attr
, pnt
,
5145 psize
, addpath_id
)) {
5148 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
5149 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5150 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE
;
5154 case BGP_EVPN_IP_PREFIX_ROUTE
:
5155 if (process_type5_route(peer
, afi
, safi
,
5156 withdraw
? NULL
: attr
, pnt
,
5157 psize
, addpath_id
)) {
5160 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
5161 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5162 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE
;
5171 /* Packet length consistency check. */
5173 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH
;
5175 return BGP_NLRI_PARSE_OK
;
5179 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
5180 * The mapping will be used during route processing.
5181 * bgp_def: default bgp instance
5182 * bgp_vrf: specific bgp vrf instance on which RT is configured
5184 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
5187 struct ecommunity_val
*eval
= NULL
;
5188 struct listnode
*node
= NULL
, *nnode
= NULL
;
5189 struct ecommunity
*ecom
= NULL
;
5191 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5192 for (i
= 0; i
< ecom
->size
; i
++) {
5193 eval
= (struct ecommunity_val
*)(ecom
->val
5195 * ECOMMUNITY_SIZE
));
5196 map_vrf_to_rt(bgp_vrf
, eval
);
5202 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
5204 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
5207 struct ecommunity_val
*eval
;
5208 struct listnode
*node
, *nnode
;
5209 struct ecommunity
*ecom
;
5211 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5212 for (i
= 0; i
< ecom
->size
; i
++) {
5213 struct vrf_irt_node
*irt
;
5214 struct ecommunity_val eval_tmp
;
5216 eval
= (struct ecommunity_val
*)(ecom
->val
5218 * ECOMMUNITY_SIZE
));
5219 /* If using "automatic" RT, we only care about the
5220 * local-admin sub-field.
5221 * This is to facilitate using VNI as the RT for EBGP
5224 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5225 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
5226 BGP_VRF_IMPORT_RT_CFGD
))
5227 mask_ecom_global_admin(&eval_tmp
, eval
);
5229 irt
= lookup_vrf_import_rt(&eval_tmp
);
5231 unmap_vrf_from_rt(bgp_vrf
, irt
);
5238 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
5239 * The mapping will be used during route processing.
5241 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5244 struct ecommunity_val
*eval
;
5245 struct listnode
*node
, *nnode
;
5246 struct ecommunity
*ecom
;
5248 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5249 for (i
= 0; i
< ecom
->size
; i
++) {
5250 eval
= (struct ecommunity_val
*)(ecom
->val
5252 * ECOMMUNITY_SIZE
));
5253 map_vni_to_rt(bgp
, vpn
, eval
);
5259 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
5261 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5264 struct ecommunity_val
*eval
;
5265 struct listnode
*node
, *nnode
;
5266 struct ecommunity
*ecom
;
5268 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5269 for (i
= 0; i
< ecom
->size
; i
++) {
5270 struct irt_node
*irt
;
5271 struct ecommunity_val eval_tmp
;
5273 eval
= (struct ecommunity_val
*)(ecom
->val
5275 * ECOMMUNITY_SIZE
));
5276 /* If using "automatic" RT, we only care about the
5277 * local-admin sub-field.
5278 * This is to facilitate using VNI as the RT for EBGP
5281 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5282 if (!is_import_rt_configured(vpn
))
5283 mask_ecom_global_admin(&eval_tmp
, eval
);
5285 irt
= lookup_import_rt(bgp
, &eval_tmp
);
5287 unmap_vni_from_rt(bgp
, vpn
, irt
);
5293 * Derive Import RT automatically for VNI and map VNI to RT.
5294 * The mapping will be used during route processing.
5296 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5298 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
5299 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
5302 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
5306 * Derive Export RT automatically for VNI.
5308 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5310 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
5311 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5315 * Derive RD automatically for VNI using passed information - it
5316 * is of the form RouterId:unique-id-for-vni.
5318 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5320 if (is_vrf_rd_configured(bgp
))
5323 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5327 * Derive RD automatically for VNI using passed information - it
5328 * is of the form RouterId:unique-id-for-vni.
5330 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5334 vpn
->prd
.family
= AF_UNSPEC
;
5335 vpn
->prd
.prefixlen
= 64;
5336 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
5337 (void)str2prefix_rd(buf
, &vpn
->prd
);
5338 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5344 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5346 struct list
*inst
= bm
->bgp
;
5347 struct listnode
*node
;
5348 struct bgp
*bgp_vrf
;
5350 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5351 if (bgp_vrf
->l3vni
== vni
)
5361 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5363 struct bgpevpn
*vpn
;
5366 memset(&tmp
, 0, sizeof(struct bgpevpn
));
5368 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5373 * Create a new vpn - invoked upon configuration or zebra notification.
5375 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5376 struct in_addr originator_ip
,
5377 vrf_id_t tenant_vrf_id
,
5378 struct in_addr mcast_grp
)
5380 struct bgpevpn
*vpn
;
5385 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5387 /* Set values - RD and RT set to defaults. */
5389 vpn
->originator_ip
= originator_ip
;
5390 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5391 vpn
->mcast_grp
= mcast_grp
;
5393 /* Initialize route-target import and export lists */
5394 vpn
->import_rtl
= list_new();
5395 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5396 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
5397 vpn
->export_rtl
= list_new();
5398 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5399 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
5400 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5401 derive_rd_rt_for_vni(bgp
, vpn
);
5403 /* Initialize EVPN route table. */
5404 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5407 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
5408 XFREE(MTYPE_BGP_EVPN
, vpn
);
5412 /* add to l2vni list on corresponding vrf */
5413 bgpevpn_link_to_l3vni(vpn
);
5415 QOBJ_REG(vpn
, bgpevpn
);
5420 * Free a given VPN - called in multiple scenarios such as zebra
5421 * notification, configuration being deleted, advertise-all-vni disabled etc.
5422 * This just frees appropriate memory, caller should have taken other
5425 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5427 bgpevpn_unlink_from_l3vni(vpn
);
5428 bgp_table_unlock(vpn
->route_table
);
5429 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5430 list_delete(&vpn
->import_rtl
);
5431 list_delete(&vpn
->export_rtl
);
5432 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5433 hash_release(bgp
->vnihash
, vpn
);
5435 XFREE(MTYPE_BGP_EVPN
, vpn
);
5441 struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
)
5446 memset(&tmp
, 0, sizeof(struct evpnes
));
5447 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
5448 es
= hash_lookup(bgp
->esihash
, &tmp
);
5453 * Create a new local es - invoked upon zebra notification.
5455 struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
,
5457 struct ipaddr
*originator_ip
)
5465 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct evpnes
));
5467 /* set the ESI and originator_ip */
5468 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
5469 memcpy(&es
->originator_ip
, originator_ip
, sizeof(struct ipaddr
));
5471 /* Initialise the VTEP list */
5472 es
->vtep_list
= list_new();
5473 es
->vtep_list
->cmp
= evpn_vtep_ip_cmp
;
5475 /* auto derive RD for this es */
5476 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
5477 es
->prd
.family
= AF_UNSPEC
;
5478 es
->prd
.prefixlen
= 64;
5479 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), es
->rd_id
);
5480 (void)str2prefix_rd(buf
, &es
->prd
);
5482 /* Initialize the ES route table */
5483 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5486 if (!hash_get(bgp
->esihash
, es
, hash_alloc_intern
)) {
5487 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5491 QOBJ_REG(es
, evpnes
);
5497 * This just frees appropriate memory, caller should have taken other
5500 void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
)
5502 list_delete(&es
->vtep_list
);
5503 bgp_table_unlock(es
->route_table
);
5504 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
5505 hash_release(bgp
->esihash
, es
);
5507 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5511 * Import evpn route from global table to VNI/VRF/ESI.
5513 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5514 struct prefix
*p
, struct bgp_path_info
*pi
)
5516 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5520 * Unimport evpn route from VNI/VRF/ESI.
5522 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5523 struct prefix
*p
, struct bgp_path_info
*pi
)
5525 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5528 /* filter routes which have martian next hops */
5529 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5533 struct bgp_node
*rd_rn
, *rn
;
5534 struct bgp_table
*table
;
5535 struct bgp_path_info
*pi
;
5540 /* Walk entire global routing table and evaluate routes which could be
5541 * imported into this VPN. Note that we cannot just look at the routes
5542 * for the VNI's RD -
5543 * remote routes applicable for this VNI could have any RD.
5545 /* EVPN routes are a 2-level table. */
5546 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
5547 rd_rn
= bgp_route_next(rd_rn
)) {
5548 table
= bgp_node_get_bgp_table_info(rd_rn
);
5552 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5554 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
5557 /* Consider "valid" remote routes applicable for
5559 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5560 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5562 if (bgp_nexthop_self(bgp
, afi
,
5563 pi
->type
, pi
->sub_type
,
5566 char attr_str
[BUFSIZ
] = {0};
5567 char pbuf
[PREFIX_STRLEN
];
5569 bgp_dump_attr(pi
->attr
, attr_str
,
5572 if (bgp_debug_update(pi
->peer
, &rn
->p
,
5575 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
5582 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5585 bgp_rib_remove(rn
, pi
, pi
->peer
, afi
,
5596 * Handle del of a local MACIP.
5598 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5599 struct ipaddr
*ip
, int state
)
5601 struct bgpevpn
*vpn
;
5602 struct prefix_evpn p
;
5603 struct bgp_node
*rn
;
5605 /* Lookup VNI hash - should exist. */
5606 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5607 if (!vpn
|| !is_vni_live(vpn
)) {
5608 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5609 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5610 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5614 build_evpn_type2_prefix(&p
, mac
, ip
);
5615 if (state
== ZEBRA_NEIGH_ACTIVE
) {
5616 /* Remove EVPN type-2 route and schedule for processing. */
5617 delete_evpn_route(bgp
, vpn
, &p
);
5619 /* Re-instate the current remote best path if any */
5620 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
5622 evpn_zebra_reinstall_best_route(bgp
, vpn
, rn
);
5629 * Handle add of a local MACIP.
5631 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5632 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
)
5634 struct bgpevpn
*vpn
;
5635 struct prefix_evpn p
;
5637 /* Lookup VNI hash - should exist. */
5638 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5639 if (!vpn
|| !is_vni_live(vpn
)) {
5640 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5641 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5642 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5646 /* Create EVPN type-2 route and schedule for processing. */
5647 build_evpn_type2_prefix(&p
, mac
, ip
);
5648 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
)) {
5649 char buf
[ETHER_ADDR_STRLEN
];
5650 char buf2
[INET6_ADDRSTRLEN
];
5653 EC_BGP_EVPN_ROUTE_CREATE
,
5654 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5655 bgp
->vrf_id
, vpn
->vni
,
5656 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5659 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5660 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5667 static void link_l2vni_hash_to_l3vni(struct hash_bucket
*bucket
,
5668 struct bgp
*bgp_vrf
)
5670 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
5671 struct bgp
*bgp_evpn
= NULL
;
5673 bgp_evpn
= bgp_get_evpn();
5676 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5677 bgpevpn_link_to_l3vni(vpn
);
5680 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
,
5681 struct ethaddr
*svi_rmac
,
5682 struct ethaddr
*vrr_rmac
,
5683 struct in_addr originator_ip
, int filter
,
5684 ifindex_t svi_ifindex
,
5685 bool is_anycast_mac
)
5687 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5688 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5689 struct listnode
*node
= NULL
;
5690 struct bgpevpn
*vpn
= NULL
;
5693 /* get the EVPN instance - required to get the AS number for VRF
5696 bgp_evpn
= bgp_get_evpn();
5700 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
5706 /* if the BGP vrf instance doesn't exist - create one */
5707 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5712 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5713 vrf_id
== VRF_DEFAULT
? BGP_INSTANCE_TYPE_DEFAULT
5714 : BGP_INSTANCE_TYPE_VRF
);
5716 case BGP_ERR_AS_MISMATCH
:
5717 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5718 "BGP is already running; AS is %u\n", as
);
5720 case BGP_ERR_INSTANCE_MISMATCH
:
5721 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5722 "BGP instance name and AS number mismatch\n");
5726 /* mark as auto created */
5727 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5730 /* associate the vrf with l3vni and related parameters */
5731 bgp_vrf
->l3vni
= l3vni
;
5732 bgp_vrf
->originator_ip
= originator_ip
;
5733 bgp_vrf
->l3vni_svi_ifindex
= svi_ifindex
;
5734 bgp_vrf
->evpn_info
->is_anycast_mac
= is_anycast_mac
;
5736 /* copy anycast MAC from VRR MAC */
5737 memcpy(&bgp_vrf
->rmac
, vrr_rmac
, ETH_ALEN
);
5738 /* copy sys RMAC from SVI MAC */
5739 memcpy(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, svi_rmac
, ETH_ALEN
);
5740 /* PIP user configured mac is not present use svi mac as sys mac */
5741 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
))
5742 memcpy(&bgp_vrf
->evpn_info
->pip_rmac
, svi_rmac
, ETH_ALEN
);
5744 if (bgp_debug_zebra(NULL
)) {
5745 char buf
[ETHER_ADDR_STRLEN
];
5746 char buf1
[ETHER_ADDR_STRLEN
];
5747 char buf2
[ETHER_ADDR_STRLEN
];
5749 zlog_debug("VRF %s vni %u pip %s RMAC %s sys RMAC %s static RMAC %s is_anycast_mac %s",
5750 vrf_id_to_name(bgp_vrf
->vrf_id
),
5752 bgp_vrf
->evpn_info
->advertise_pip
? "enable"
5754 prefix_mac2str(&bgp_vrf
->rmac
, buf
, sizeof(buf
)),
5755 prefix_mac2str(&bgp_vrf
->evpn_info
->pip_rmac
,
5756 buf1
, sizeof(buf1
)),
5757 prefix_mac2str(&bgp_vrf
->evpn_info
->pip_rmac_static
,
5758 buf2
, sizeof(buf2
)),
5759 is_anycast_mac
? "Enable" : "Disable");
5761 /* set the right filter - are we using l3vni only for prefix routes? */
5763 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5765 /* Map auto derive or configured RTs */
5766 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5767 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5769 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
5771 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5772 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5774 /* auto derive RD */
5775 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5777 /* link all corresponding l2vnis */
5778 hash_iterate(bgp_evpn
->vnihash
,
5779 (void (*)(struct hash_bucket
*,
5780 void *))link_l2vni_hash_to_l3vni
,
5783 /* Only update all corresponding type-2 routes if we are advertising two
5784 * labels along with type-2 routes
5787 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5788 update_routes_for_vni(bgp_evpn
, vpn
);
5790 /* advertise type-5 routes if needed */
5791 update_advertise_vrf_routes(bgp_vrf
);
5793 /* install all remote routes belonging to this l3vni into correspondng
5795 install_routes_for_vrf(bgp_vrf
);
5800 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5802 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5803 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5804 struct listnode
*node
= NULL
;
5805 struct listnode
*next
= NULL
;
5806 struct bgpevpn
*vpn
= NULL
;
5808 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5812 "Cannot process L3VNI %u Del - Could not find BGP instance",
5817 bgp_evpn
= bgp_get_evpn();
5821 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
5826 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5827 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5828 * routes. This will uninstalling the routes from zebra and decremnt the
5831 uninstall_routes_for_vrf(bgp_vrf
);
5833 /* delete/withdraw all type-5 routes */
5834 delete_withdraw_vrf_routes(bgp_vrf
);
5836 /* remove the l3vni from vrf instance */
5839 /* remove the Rmac from the BGP vrf */
5840 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5841 memset(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, 0, ETH_ALEN
);
5842 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
) &&
5843 !is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac
))
5844 memset(&bgp_vrf
->evpn_info
->pip_rmac
, 0, ETH_ALEN
);
5846 /* remove default import RT or Unmap non-default import RT */
5847 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5848 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5849 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5850 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5853 /* remove default export RT */
5854 if (!list_isempty(bgp_vrf
->vrf_export_rtl
) &&
5855 !CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
5856 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5859 /* update all corresponding local mac-ip routes */
5860 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5861 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5862 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5863 update_routes_for_vni(bgp_evpn
, vpn
);
5867 /* If any L2VNIs point to this instance, unlink them. */
5868 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5869 bgpevpn_unlink_from_l3vni(vpn
);
5871 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5873 /* Delete the instance if it was autocreated */
5874 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5875 bgp_delete(bgp_vrf
);
5881 * Handle del of a local VNI.
5883 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5885 struct bgpevpn
*vpn
;
5887 /* Locate VNI hash */
5888 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5890 if (bgp_debug_zebra(NULL
))
5892 EC_BGP_EVPN_VPN_VNI
,
5893 "%u: VNI hash entry for VNI %u not found at DEL",
5898 /* Remove all local EVPN routes and schedule for processing (to
5899 * withdraw from peers).
5901 delete_routes_for_vni(bgp
, vpn
);
5904 * tunnel is no longer active, del tunnel ip address from tip_hash
5906 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5908 /* Clear "live" flag and see if hash needs to be freed. */
5909 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5910 if (!is_vni_configured(vpn
))
5911 bgp_evpn_free(bgp
, vpn
);
5917 * Handle add (or update) of a local VNI. The VNI changes we care
5918 * about are for the local-tunnel-ip and the (tenant) VRF.
5920 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5921 struct in_addr originator_ip
,
5922 vrf_id_t tenant_vrf_id
,
5923 struct in_addr mcast_grp
)
5926 struct bgpevpn
*vpn
;
5927 struct prefix_evpn p
;
5929 /* Lookup VNI. If present and no change, exit. */
5930 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5933 if (is_vni_live(vpn
)
5934 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5935 && IPV4_ADDR_SAME(&vpn
->mcast_grp
, &mcast_grp
)
5936 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5937 /* Probably some other param has changed that we don't
5941 bgp_evpn_mcast_grp_change(bgp
, vpn
, mcast_grp
);
5943 /* Update tenant_vrf_id if it has changed. */
5944 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5945 bgpevpn_unlink_from_l3vni(vpn
);
5946 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5947 bgpevpn_link_to_l3vni(vpn
);
5950 /* If tunnel endpoint IP has changed, update (and delete prior
5951 * type-3 route, if needed.)
5953 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5954 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5956 /* Update all routes with new endpoint IP and/or export RT
5959 if (is_vni_live(vpn
))
5960 update_routes_for_vni(bgp
, vpn
);
5963 /* Create or update as appropriate. */
5965 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
,
5970 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5976 /* if the VNI is live already, there is nothing more to do */
5977 if (is_vni_live(vpn
))
5980 /* Mark as "live" */
5981 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5983 /* tunnel is now active, add tunnel-ip to db */
5984 bgp_tip_add(bgp
, &originator_ip
);
5986 /* filter routes as nexthop database has changed */
5987 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5990 * Create EVPN type-3 route and schedule for processing.
5992 * RT-3 only if doing head-end replication
5994 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
5995 == VXLAN_FLOOD_HEAD_END_REPL
) {
5996 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5997 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0)) {
5998 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5999 "%u: Type3 route creation failure for VNI %u",
6005 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
6009 install_routes_for_vni(bgp
, vpn
);
6011 /* If we are advertising gateway mac-ip
6012 It needs to be conveyed again to zebra */
6013 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
6015 /* advertise svi mac-ip knob to zebra */
6016 bgp_zebra_advertise_svi_macip(bgp
, vpn
->advertise_svi_macip
, vpn
->vni
);
6022 * bgp_evpn_local_es_del
6024 int bgp_evpn_local_es_del(struct bgp
*bgp
,
6026 struct ipaddr
*originator_ip
)
6028 char buf
[ESI_STR_LEN
];
6029 struct evpnes
*es
= NULL
;
6031 if (!bgp
->esihash
) {
6032 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
6037 /* Lookup ESI hash - should exist. */
6038 es
= bgp_evpn_lookup_es(bgp
, esi
);
6040 flog_warn(EC_BGP_EVPN_ESI
,
6041 "%u: ESI hash entry for ESI %s at Local ES DEL",
6042 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
6046 /* Delete all local EVPN ES routes from ESI table
6047 * and schedule for processing (to withdraw from peers))
6049 delete_routes_for_es(bgp
, es
);
6051 /* free the hash entry */
6052 bgp_evpn_es_free(bgp
, es
);
6058 * bgp_evpn_local_es_add
6060 int bgp_evpn_local_es_add(struct bgp
*bgp
,
6062 struct ipaddr
*originator_ip
)
6064 char buf
[ESI_STR_LEN
];
6065 struct evpnes
*es
= NULL
;
6066 struct prefix_evpn p
;
6068 if (!bgp
->esihash
) {
6069 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
6074 /* create the new es */
6075 es
= bgp_evpn_lookup_es(bgp
, esi
);
6077 es
= bgp_evpn_es_new(bgp
, esi
, originator_ip
);
6081 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
6082 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
6086 UNSET_FLAG(es
->flags
, EVPNES_REMOTE
);
6087 SET_FLAG(es
->flags
, EVPNES_LOCAL
);
6089 build_evpn_type4_prefix(&p
, esi
, originator_ip
->ipaddr_v4
);
6090 if (update_evpn_type4_route(bgp
, es
, &p
)) {
6091 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
6092 "%u: Type4 route creation failure for ESI %s",
6093 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
6097 /* import all remote ES routes in th ES table */
6098 install_routes_for_es(bgp
, es
);
6104 * Handle change in setting for BUM handling. The supported values
6105 * are head-end replication and dropping all BUM packets. Any change
6106 * should be registered with zebra. Also, if doing head-end replication,
6107 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
6108 * to be dropped, the RT-3s must be withdrawn.
6110 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
6112 zlog_info("L2VPN EVPN BUM handling is %s",
6113 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
6114 "Flooding" : "Flooding Disabled");
6116 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
6117 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
6118 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
6119 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
6120 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
6124 * Cleanup EVPN information on disable - Need to delete and withdraw
6125 * EVPN routes from peers.
6127 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
6129 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_bucket
*,
6130 void *))cleanup_vni_on_disable
,
6135 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
6136 * BGP instance (default) is being freed.
6138 void bgp_evpn_cleanup(struct bgp
*bgp
)
6140 hash_iterate(bgp
->vnihash
,
6141 (void (*)(struct hash_bucket
*, void *))free_vni_entry
,
6144 hash_free(bgp
->import_rt_hash
);
6145 bgp
->import_rt_hash
= NULL
;
6147 hash_free(bgp
->vrf_import_rt_hash
);
6148 bgp
->vrf_import_rt_hash
= NULL
;
6150 hash_free(bgp
->vnihash
);
6151 bgp
->vnihash
= NULL
;
6153 hash_free(bgp
->esihash
);
6154 bgp
->esihash
= NULL
;
6156 list_delete(&bgp
->vrf_import_rtl
);
6157 list_delete(&bgp
->vrf_export_rtl
);
6158 list_delete(&bgp
->l2vnis
);
6162 * Initialization for EVPN
6165 * hash for RT to VNI
6167 void bgp_evpn_init(struct bgp
*bgp
)
6170 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
6172 hash_create(esi_hash_keymake
, esi_cmp
,
6173 "BGP EVPN Local ESI Hash");
6174 bgp
->import_rt_hash
=
6175 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
6176 "BGP Import RT Hash");
6177 bgp
->vrf_import_rt_hash
=
6178 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
6179 "BGP VRF Import RT Hash");
6180 bgp
->vrf_import_rtl
= list_new();
6181 bgp
->vrf_import_rtl
->cmp
=
6182 (int (*)(void *, void *))evpn_route_target_cmp
;
6183 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
6184 bgp
->vrf_export_rtl
= list_new();
6185 bgp
->vrf_export_rtl
->cmp
=
6186 (int (*)(void *, void *))evpn_route_target_cmp
;
6187 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
6188 bgp
->l2vnis
= list_new();
6189 bgp
->l2vnis
->cmp
= vni_list_cmp
;
6190 /* By default Duplicate Address Dection is enabled.
6191 * Max-moves (N) 5, detection time (M) 180
6192 * default action is warning-only
6193 * freeze action permanently freezes address,
6194 * and freeze time (auto-recovery) is disabled.
6196 if (bgp
->evpn_info
) {
6197 bgp
->evpn_info
->dup_addr_detect
= true;
6198 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
6199 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
6200 bgp
->evpn_info
->dad_freeze
= false;
6201 bgp
->evpn_info
->dad_freeze_time
= 0;
6202 /* Initialize zebra vxlan */
6203 bgp_zebra_dup_addr_detection(bgp
);
6204 /* Enable PIP feature by default for bgp vrf instance */
6205 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
6206 struct bgp
*bgp_default
;
6208 bgp
->evpn_info
->advertise_pip
= true;
6209 bgp_default
= bgp_get_default();
6211 bgp
->evpn_info
->pip_ip
= bgp_default
->router_id
;
6215 /* Default BUM handling is to do head-end replication. */
6216 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
6219 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
6221 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
6225 * Get the prefixlen of the ip prefix carried within the type5 evpn route.
6227 int bgp_evpn_get_type5_prefixlen(struct prefix
*pfx
)
6229 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6231 if (!pfx
|| pfx
->family
!= AF_EVPN
)
6234 if (evp
->prefix
.route_type
!= BGP_EVPN_IP_PREFIX_ROUTE
)
6237 return evp
->prefix
.prefix_addr
.ip_prefix_length
;
6241 * Should we register nexthop for this EVPN prefix for nexthop tracking?
6243 bool bgp_evpn_is_prefix_nht_supported(struct prefix
*pfx
)
6245 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6248 * EVPN RT-5 should not be marked as valid and imported to vrfs if the
6249 * BGP nexthop is not reachable. To check for the nexthop reachability,
6250 * Add nexthop for EVPN RT-5 for nexthop tracking.
6252 * Ideally, a BGP route should be marked as valid only if the
6253 * nexthop is reachable. Thus, other EVPN route types also should be
6254 * added here after testing is performed for them.
6256 if (pfx
&& pfx
->family
== AF_EVPN
&&
6257 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
)