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
);
498 * Create RT extended community automatically from passed information:
499 * of the form AS:VNI.
500 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
501 * the need is to get a RT value that will be unique across different
502 * VNIs but the same across routers (in the same AS) for a particular
505 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
507 struct ecommunity_val eval
;
508 struct ecommunity
*ecomadd
, *ecom
;
509 bool ecom_found
= false;
510 struct listnode
*node
;
512 if (bgp
->advertise_autort_rfc8365
)
513 vni
|= EVPN_AUTORT_VXLAN
;
514 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
516 ecomadd
= ecommunity_new();
517 ecommunity_add_val(ecomadd
, &eval
);
518 for (ALL_LIST_ELEMENTS_RO(rtl
, node
, ecom
))
519 if (ecommunity_cmp(ecomadd
, ecom
))
523 listnode_add_sort(rtl
, ecomadd
);
527 * Derive RD and RT for a VNI automatically. Invoked at the time of
530 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
532 bgp_evpn_derive_auto_rd(bgp
, vpn
);
533 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
534 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
538 * Convert nexthop (remote VTEP IP) into an IPv6 address.
540 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
542 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
544 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
545 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
549 * Add (update) or delete MACIP from zebra.
551 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
552 struct prefix_evpn
*p
,
553 struct in_addr remote_vtep_ip
, int add
,
554 uint8_t flags
, uint32_t seq
)
558 char buf1
[ETHER_ADDR_STRLEN
];
559 char buf2
[INET6_ADDRSTRLEN
];
560 char buf3
[INET6_ADDRSTRLEN
];
563 if (!zclient
|| zclient
->sock
< 0)
566 /* Don't try to register if Zebra doesn't know of this instance. */
567 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
568 if (BGP_DEBUG(zebra
, ZEBRA
))
569 zlog_debug("%s: No zebra instance to talk to, not installing remote macip",
570 __PRETTY_FUNCTION__
);
576 zclient_create_header(
577 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
579 stream_putl(s
, vpn
->vni
);
580 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
581 /* IP address length and IP address, if any. */
582 if (is_evpn_prefix_ipaddr_none(p
))
585 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
587 stream_putl(s
, ipa_len
);
588 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
590 stream_put_in_addr(s
, &remote_vtep_ip
);
592 /* TX flags - MAC sticky status and/or gateway mac */
593 /* Also TX the sequence number of the best route. */
595 stream_putc(s
, flags
);
599 stream_putw_at(s
, 0, stream_get_endp(s
));
601 if (bgp_debug_zebra(NULL
))
603 "Tx %s MACIP, VNI %u MAC %s IP %s flags 0x%x seq %u remote VTEP %s",
604 add
? "ADD" : "DEL", vpn
->vni
,
605 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
607 ipaddr2str(&p
->prefix
.macip_addr
.ip
,
608 buf3
, sizeof(buf3
)), flags
, seq
,
609 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
612 return zclient_send_message(zclient
);
616 * Add (update) or delete remote VTEP from zebra.
618 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
619 struct prefix_evpn
*p
,
620 int flood_control
, int add
)
625 if (!zclient
|| zclient
->sock
< 0)
628 /* Don't try to register if Zebra doesn't know of this instance. */
629 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
630 if (BGP_DEBUG(zebra
, ZEBRA
))
631 zlog_debug("%s: No zebra instance to talk to, not installing remote vtep",
632 __PRETTY_FUNCTION__
);
639 zclient_create_header(
640 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
642 stream_putl(s
, vpn
->vni
);
643 if (is_evpn_prefix_ipaddr_v4(p
))
644 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
645 else if (is_evpn_prefix_ipaddr_v6(p
)) {
648 "Bad remote IP when trying to %s remote VTEP for VNI %u",
649 add
? "ADD" : "DEL", vpn
->vni
);
652 stream_putl(s
, flood_control
);
654 stream_putw_at(s
, 0, stream_get_endp(s
));
656 if (bgp_debug_zebra(NULL
))
657 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
658 add
? "ADD" : "DEL", vpn
->vni
,
659 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
661 return zclient_send_message(zclient
);
665 * Build extended community for EVPN ES (type-4) route
667 static void build_evpn_type4_route_extcomm(struct evpnes
*es
,
670 struct ecommunity ecom_encap
;
671 struct ecommunity ecom_es_rt
;
672 struct ecommunity_val eval
;
673 struct ecommunity_val eval_es_rt
;
674 bgp_encap_types tnl_type
;
678 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
679 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
680 encode_encap_extcomm(tnl_type
, &eval
);
682 ecom_encap
.val
= (uint8_t *)eval
.val
;
683 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
686 memset(&mac
, 0, sizeof(struct ethaddr
));
687 memset(&ecom_es_rt
, 0, sizeof(ecom_es_rt
));
688 es_get_system_mac(&es
->esi
, &mac
);
689 encode_es_rt_extcomm(&eval_es_rt
, &mac
);
691 ecom_es_rt
.val
= (uint8_t *)eval_es_rt
.val
;
693 ecommunity_merge(attr
->ecommunity
, &ecom_es_rt
);
695 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
699 * Build extended communities for EVPN prefix route.
701 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
704 struct ecommunity ecom_encap
;
705 struct ecommunity ecom_rmac
;
706 struct ecommunity_val eval
;
707 struct ecommunity_val eval_rmac
;
708 bgp_encap_types tnl_type
;
709 struct listnode
*node
, *nnode
;
710 struct ecommunity
*ecom
;
711 struct list
*vrf_export_rtl
= NULL
;
714 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
715 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
716 encode_encap_extcomm(tnl_type
, &eval
);
718 ecom_encap
.val
= (uint8_t *)eval
.val
;
721 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
723 /* Add the export RTs for L3VNI/VRF */
724 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
725 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
727 ecommunity_merge(attr
->ecommunity
, ecom
);
729 /* add the router mac extended community */
730 if (!is_zero_mac(&attr
->rmac
)) {
731 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
732 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
734 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
736 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
739 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
743 * Build extended communities for EVPN route.
744 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
745 * and ENCAP extended communities are applicable for all routes.
746 * The default gateway extended community and MAC mobility (sticky) extended
747 * community are added as needed based on passed settings - only for type-2
748 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
749 * added, if present, based on passed settings - only for non-link-local
752 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
755 struct ecommunity ecom_encap
;
756 struct ecommunity ecom_sticky
;
757 struct ecommunity ecom_default_gw
;
758 struct ecommunity ecom_rmac
;
759 struct ecommunity ecom_na
;
760 struct ecommunity_val eval
;
761 struct ecommunity_val eval_sticky
;
762 struct ecommunity_val eval_default_gw
;
763 struct ecommunity_val eval_rmac
;
764 struct ecommunity_val eval_na
;
766 bgp_encap_types tnl_type
;
767 struct listnode
*node
, *nnode
;
768 struct ecommunity
*ecom
;
770 struct list
*vrf_export_rtl
= NULL
;
773 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
774 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
775 encode_encap_extcomm(tnl_type
, &eval
);
777 ecom_encap
.val
= (uint8_t *)eval
.val
;
780 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
782 /* Add the export RTs for L2VNI */
783 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
784 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
786 /* Add the export RTs for L3VNI if told to - caller determines
787 * when this should be done.
790 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
791 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
792 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
794 attr
->ecommunity
= ecommunity_merge(
795 attr
->ecommunity
, ecom
);
799 /* Add MAC mobility (sticky) if needed. */
802 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
803 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
804 ecom_sticky
.size
= 1;
805 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
807 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
810 /* Add RMAC, if told to. */
812 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
813 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
815 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
817 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
820 /* Add default gateway, if needed. */
821 if (attr
->default_gw
) {
822 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
823 encode_default_gw_extcomm(&eval_default_gw
);
824 ecom_default_gw
.size
= 1;
825 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
827 ecommunity_merge(attr
->ecommunity
, &ecom_default_gw
);
830 if (attr
->router_flag
) {
831 memset(&ecom_na
, 0, sizeof(ecom_na
));
832 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
);
834 ecom_na
.val
= (uint8_t *)eval_na
.val
;
835 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
839 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
843 * Add MAC mobility extended community to attribute.
845 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
847 struct ecommunity ecom_tmp
;
848 struct ecommunity_val eval
;
849 uint8_t *ecom_val_ptr
;
856 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
858 /* Find current MM ecommunity */
861 if (attr
->ecommunity
) {
862 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
863 pnt
= attr
->ecommunity
->val
+ (i
* 8);
867 if (type
== ECOMMUNITY_ENCODE_EVPN
869 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
870 ecom_val_ptr
= (uint8_t *)(attr
->ecommunity
->val
877 /* Update the existing MM ecommunity */
879 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
881 /* Add MM to existing */
883 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
885 ecom_tmp
.val
= (uint8_t *)eval
.val
;
887 if (attr
->ecommunity
)
889 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
891 attr
->ecommunity
= ecommunity_dup(&ecom_tmp
);
895 /* Install EVPN route into zebra. */
896 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
897 struct prefix_evpn
*p
, struct bgp_path_info
*pi
)
903 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
905 if (pi
->attr
->sticky
)
906 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
907 if (pi
->attr
->default_gw
)
908 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
909 if (is_evpn_prefix_ipaddr_v6(p
) &&
910 pi
->attr
->router_flag
)
911 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
912 ret
= bgp_zebra_send_remote_macip(
913 bgp
, vpn
, p
, pi
->attr
->nexthop
, 1, flags
,
914 mac_mobility_seqnum(pi
->attr
));
916 switch (pi
->attr
->pmsi_tnl_type
) {
917 case PMSI_TNLTYPE_INGR_REPL
:
918 flood_control
= VXLAN_FLOOD_HEAD_END_REPL
;
921 case PMSI_TNLTYPE_PIM_SM
:
922 flood_control
= VXLAN_FLOOD_PIM_SM
;
926 flood_control
= VXLAN_FLOOD_DISABLED
;
929 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, flood_control
, 1);
935 /* Uninstall EVPN route from zebra. */
936 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
937 struct prefix_evpn
*p
,
938 struct in_addr remote_vtep_ip
)
942 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
943 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
946 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
,
947 VXLAN_FLOOD_DISABLED
, 0);
953 * Due to MAC mobility, the prior "local" best route has been supplanted
954 * by a "remote" best route. The prior route has to be deleted and withdrawn
957 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
959 struct bgp_path_info
*old_local
)
961 struct bgp_node
*global_rn
;
962 struct bgp_path_info
*pi
;
963 afi_t afi
= AFI_L2VPN
;
964 safi_t safi
= SAFI_EVPN
;
966 /* Locate route node in the global EVPN routing table. Note that
967 * this table is a 2-level tree (RD-level + Prefix-level) similar to
970 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
971 (struct prefix
*)&rn
->p
, &vpn
->prd
);
973 /* Delete route entry in the global EVPN table. */
974 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
976 /* Schedule for processing - withdraws to peers happen from
980 bgp_process(bgp
, global_rn
, afi
, safi
);
981 bgp_unlock_node(global_rn
);
984 /* Delete route entry in the VNI route table, caller to remove. */
985 bgp_path_info_delete(rn
, old_local
);
988 static struct in_addr
*es_vtep_new(struct in_addr vtep
)
992 ip
= XCALLOC(MTYPE_BGP_EVPN_ES_VTEP
, sizeof(struct in_addr
));
994 ip
->s_addr
= vtep
.s_addr
;
998 static void es_vtep_free(struct in_addr
*ip
)
1000 XFREE(MTYPE_BGP_EVPN_ES_VTEP
, ip
);
1003 /* check if VTEP is already part of the list */
1004 static int is_vtep_present_in_list(struct list
*list
,
1005 struct in_addr vtep
)
1007 struct listnode
*node
= NULL
;
1008 struct in_addr
*tmp
;
1010 for (ALL_LIST_ELEMENTS_RO(list
, node
, tmp
)) {
1011 if (tmp
->s_addr
== vtep
.s_addr
)
1018 * Best path for ES route was changed,
1019 * update the list of VTEPs for this ES
1021 static int evpn_es_install_vtep(struct bgp
*bgp
,
1023 struct prefix_evpn
*p
,
1024 struct in_addr rvtep
)
1026 struct in_addr
*vtep_ip
;
1028 if (is_vtep_present_in_list(es
->vtep_list
, rvtep
))
1032 vtep_ip
= es_vtep_new(rvtep
);
1034 listnode_add_sort(es
->vtep_list
, vtep_ip
);
1039 * Best path for ES route was changed,
1040 * update the list of VTEPs for this ES
1042 static int evpn_es_uninstall_vtep(struct bgp
*bgp
,
1044 struct prefix_evpn
*p
,
1045 struct in_addr rvtep
)
1047 struct listnode
*node
, *nnode
, *node_to_del
= NULL
;
1048 struct in_addr
*tmp
;
1050 for (ALL_LIST_ELEMENTS(es
->vtep_list
, node
, nnode
, tmp
)) {
1051 if (tmp
->s_addr
== rvtep
.s_addr
) {
1058 list_delete_node(es
->vtep_list
, node_to_del
);
1064 * Calculate the best path for a ES(type-4) route.
1066 static int evpn_es_route_select_install(struct bgp
*bgp
,
1068 struct bgp_node
*rn
)
1071 afi_t afi
= AFI_L2VPN
;
1072 safi_t safi
= SAFI_EVPN
;
1073 struct bgp_path_info
*old_select
; /* old best */
1074 struct bgp_path_info
*new_select
; /* new best */
1075 struct bgp_path_info_pair old_and_new
;
1077 /* Compute the best path. */
1078 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
],
1079 &old_and_new
, afi
, safi
);
1080 old_select
= old_and_new
.old
;
1081 new_select
= old_and_new
.new;
1084 * If the best path hasn't changed - see if something needs to be
1087 if (old_select
&& old_select
== new_select
1088 && old_select
->type
== ZEBRA_ROUTE_BGP
1089 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1090 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1091 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1092 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1093 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1094 ret
= evpn_es_install_vtep(bgp
, es
,
1095 (struct prefix_evpn
*)&rn
->p
,
1096 old_select
->attr
->nexthop
);
1098 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1099 bgp_zebra_clear_route_change_flags(rn
);
1103 /* If the user did a "clear" this flag will be set */
1104 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1107 * bestpath has changed; update relevant fields and install or uninstall
1108 * into the zebra RIB.
1110 if (old_select
|| new_select
)
1111 bgp_bump_version(rn
);
1114 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1116 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1117 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1118 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1121 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1122 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1123 ret
= evpn_es_install_vtep(bgp
, es
,
1124 (struct prefix_evpn
*)&rn
->p
,
1125 new_select
->attr
->nexthop
);
1127 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1128 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1129 ret
= evpn_es_uninstall_vtep(
1130 bgp
, es
, (struct prefix_evpn
*)&rn
->p
,
1131 old_select
->attr
->nexthop
);
1134 /* Clear any route change flags. */
1135 bgp_zebra_clear_route_change_flags(rn
);
1137 /* Reap old select bgp_path_info, if it has been removed */
1138 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1139 bgp_path_info_reap(rn
, old_select
);
1145 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1148 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1149 struct bgp_node
*rn
)
1151 struct bgp_path_info
*old_select
, *new_select
;
1152 struct bgp_path_info_pair old_and_new
;
1153 afi_t afi
= AFI_L2VPN
;
1154 safi_t safi
= SAFI_EVPN
;
1157 /* Compute the best path. */
1158 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1160 old_select
= old_and_new
.old
;
1161 new_select
= old_and_new
.new;
1163 /* If the best path hasn't changed - see if there is still something to
1167 if (old_select
&& old_select
== new_select
1168 && old_select
->type
== ZEBRA_ROUTE_BGP
1169 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1170 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1171 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1172 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1173 if (bgp_zebra_has_route_changed(rn
, old_select
))
1174 ret
= evpn_zebra_install(
1175 bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1177 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1178 bgp_zebra_clear_route_change_flags(rn
);
1182 /* If the user did a "clear" this flag will be set */
1183 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1185 /* bestpath has changed; update relevant fields and install or uninstall
1186 * into the zebra RIB.
1188 if (old_select
|| new_select
)
1189 bgp_bump_version(rn
);
1192 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1194 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1195 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1196 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1199 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1200 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1201 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1204 /* If an old best existed and it was a "local" route, the only
1206 * it would be supplanted is due to MAC mobility procedures. So,
1208 * need to do an implicit delete and withdraw that route from
1211 if (old_select
&& old_select
->peer
== bgp
->peer_self
1212 && old_select
->type
== ZEBRA_ROUTE_BGP
1213 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
1214 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
1216 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1217 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1218 ret
= evpn_zebra_uninstall(bgp
, vpn
,
1219 (struct prefix_evpn
*)&rn
->p
,
1220 old_select
->attr
->nexthop
);
1223 /* Clear any route change flags. */
1224 bgp_zebra_clear_route_change_flags(rn
);
1226 /* Reap old select bgp_path_info, if it has been removed */
1227 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1228 bgp_path_info_reap(rn
, old_select
);
1234 * Return true if the local ri for this rn is of type gateway mac
1236 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
1238 struct bgp_path_info
*tmp_pi
= NULL
;
1239 struct bgp_path_info
*local_pi
= NULL
;
1242 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1243 tmp_pi
= tmp_pi
->next
) {
1244 if (tmp_pi
->peer
== bgp
->peer_self
1245 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1246 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1253 return local_pi
->attr
->default_gw
;
1258 * Return true if the local ri for this rn has sticky set
1260 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
1262 struct bgp_path_info
*tmp_pi
;
1263 struct bgp_path_info
*local_pi
;
1266 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1267 tmp_pi
= tmp_pi
->next
) {
1268 if (tmp_pi
->peer
== bgp
->peer_self
1269 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1270 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1277 return local_pi
->attr
->sticky
;
1281 * create or update EVPN type4 route entry.
1282 * This could be in the ES table or the global table.
1283 * TODO: handle remote ES (type4) routes as well
1285 static int update_evpn_type4_route_entry(struct bgp
*bgp
, struct evpnes
*es
,
1286 afi_t afi
, safi_t safi
,
1287 struct bgp_node
*rn
, struct attr
*attr
,
1288 int add
, struct bgp_path_info
**ri
,
1291 char buf
[ESI_STR_LEN
];
1292 char buf1
[INET6_ADDRSTRLEN
];
1293 struct bgp_path_info
*tmp_pi
= NULL
;
1294 struct bgp_path_info
*local_pi
= NULL
; /* local route entry if any */
1295 struct bgp_path_info
*remote_pi
= NULL
; /* remote route entry if any */
1296 struct attr
*attr_new
= NULL
;
1297 struct prefix_evpn
*evp
= NULL
;
1301 evp
= (struct prefix_evpn
*)&rn
->p
;
1303 /* locate the local and remote entries if any */
1304 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1305 tmp_pi
= tmp_pi
->next
) {
1306 if (tmp_pi
->peer
== bgp
->peer_self
1307 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1308 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1310 if (tmp_pi
->type
== ZEBRA_ROUTE_BGP
1311 && tmp_pi
->sub_type
== BGP_ROUTE_IMPORTED
1312 && CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1316 /* we don't expect to see a remote_ri at this point.
1317 * An ES route has esi + vtep_ip as the key,
1318 * We shouldn't see the same route from any other vtep.
1323 "%u ERROR: local es route for ESI: %s Vtep %s also learnt from remote",
1325 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1326 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1330 if (!local_pi
&& !add
)
1333 /* create or update the entry */
1336 /* Add or update attribute to hash */
1337 attr_new
= bgp_attr_intern(attr
);
1339 /* Create new route with its attribute. */
1340 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1341 bgp
->peer_self
, attr_new
, rn
);
1342 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1344 /* add the newly created path to the route-node */
1345 bgp_path_info_add(rn
, tmp_pi
);
1348 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1349 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1352 /* The attribute has changed.
1353 * Add (or update) attribute to hash. */
1354 attr_new
= bgp_attr_intern(attr
);
1355 bgp_path_info_set_flag(rn
, tmp_pi
,
1356 BGP_PATH_ATTR_CHANGED
);
1358 /* Restore route, if needed. */
1359 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1360 bgp_path_info_restore(rn
, tmp_pi
);
1362 /* Unintern existing, set to new. */
1363 bgp_attr_unintern(&tmp_pi
->attr
);
1364 tmp_pi
->attr
= attr_new
;
1365 tmp_pi
->uptime
= bgp_clock();
1369 /* Return back the route entry. */
1374 /* update evpn es (type-4) route */
1375 static int update_evpn_type4_route(struct bgp
*bgp
,
1377 struct prefix_evpn
*p
)
1380 int route_changed
= 0;
1381 char buf
[ESI_STR_LEN
];
1382 char buf1
[INET6_ADDRSTRLEN
];
1383 afi_t afi
= AFI_L2VPN
;
1384 safi_t safi
= SAFI_EVPN
;
1386 struct attr
*attr_new
= NULL
;
1387 struct bgp_node
*rn
= NULL
;
1388 struct bgp_path_info
*pi
= NULL
;
1390 memset(&attr
, 0, sizeof(struct attr
));
1392 /* Build path-attribute for this route. */
1393 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1394 attr
.nexthop
= es
->originator_ip
.ipaddr_v4
;
1395 attr
.mp_nexthop_global_in
= es
->originator_ip
.ipaddr_v4
;
1396 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1398 /* Set up extended community. */
1399 build_evpn_type4_route_extcomm(es
, &attr
);
1401 /* First, create (or fetch) route node within the ESI. */
1402 /* NOTE: There is no RD here. */
1403 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
1405 /* Create or update route entry. */
1406 ret
= update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, &attr
, 1,
1407 &pi
, &route_changed
);
1409 flog_err(EC_BGP_ES_INVALID
,
1410 "%u ERROR: Failed to updated ES route ESI: %s VTEP %s",
1412 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1413 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1417 attr_new
= pi
->attr
;
1419 /* Perform route selection;
1420 * this is just to set the flags correctly
1421 * as local route in the ES always wins.
1423 evpn_es_route_select_install(bgp
, es
, rn
);
1424 bgp_unlock_node(rn
);
1426 /* If this is a new route or some attribute has changed, export the
1427 * route to the global table. The route will be advertised to peers
1428 * from there. Note that this table is a 2-level tree (RD-level +
1429 * Prefix-level) similar to L3VPN routes.
1431 if (route_changed
) {
1432 struct bgp_path_info
*global_pi
;
1434 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1435 (struct prefix
*)p
, &es
->prd
);
1436 update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, attr_new
,
1437 1, &global_pi
, &route_changed
);
1439 /* Schedule for processing and unlock node. */
1440 bgp_process(bgp
, rn
, afi
, safi
);
1441 bgp_unlock_node(rn
);
1444 /* Unintern temporary. */
1445 aspath_unintern(&attr
.aspath
);
1449 static int update_evpn_type5_route_entry(struct bgp
*bgp_evpn
,
1450 struct bgp
*bgp_vrf
, afi_t afi
,
1451 safi_t safi
, struct bgp_node
*rn
,
1452 struct attr
*attr
, int *route_changed
)
1454 struct attr
*attr_new
= NULL
;
1455 struct bgp_path_info
*pi
= NULL
;
1456 mpls_label_t label
= MPLS_INVALID_LABEL
;
1457 struct bgp_path_info
*local_pi
= NULL
;
1458 struct bgp_path_info
*tmp_pi
= NULL
;
1461 /* locate the local route entry if any */
1462 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1463 tmp_pi
= tmp_pi
->next
) {
1464 if (tmp_pi
->peer
== bgp_evpn
->peer_self
1465 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1466 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1471 * create a new route entry if one doesn't exist.
1472 * Otherwise see if route attr has changed
1476 /* route has changed as this is the first entry */
1479 /* Add (or update) attribute to hash. */
1480 attr_new
= bgp_attr_intern(attr
);
1482 /* create the route info from attribute */
1483 pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1484 bgp_evpn
->peer_self
, attr_new
, rn
);
1485 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
1487 /* Type-5 routes advertise the L3-VNI */
1488 bgp_path_info_extra_get(pi
);
1489 vni2label(bgp_vrf
->l3vni
, &label
);
1490 memcpy(&pi
->extra
->label
, &label
, sizeof(label
));
1491 pi
->extra
->num_labels
= 1;
1493 /* add the route entry to route node*/
1494 bgp_path_info_add(rn
, pi
);
1498 if (!attrhash_cmp(tmp_pi
->attr
, attr
)) {
1500 /* attribute changed */
1503 /* The attribute has changed. */
1504 /* Add (or update) attribute to hash. */
1505 attr_new
= bgp_attr_intern(attr
);
1506 bgp_path_info_set_flag(rn
, tmp_pi
,
1507 BGP_PATH_ATTR_CHANGED
);
1509 /* Restore route, if needed. */
1510 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1511 bgp_path_info_restore(rn
, tmp_pi
);
1513 /* Unintern existing, set to new. */
1514 bgp_attr_unintern(&tmp_pi
->attr
);
1515 tmp_pi
->attr
= attr_new
;
1516 tmp_pi
->uptime
= bgp_clock();
1522 /* update evpn type-5 route entry */
1523 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1524 struct attr
*src_attr
)
1526 afi_t afi
= AFI_L2VPN
;
1527 safi_t safi
= SAFI_EVPN
;
1529 struct bgp_node
*rn
= NULL
;
1530 struct bgp
*bgp_evpn
= NULL
;
1531 int route_changed
= 0;
1533 bgp_evpn
= bgp_get_evpn();
1537 /* Build path attribute for this route - use the source attr, if
1538 * present, else treat as locally originated.
1541 bgp_attr_dup(&attr
, src_attr
);
1543 memset(&attr
, 0, sizeof(struct attr
));
1544 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1546 /* Set nexthop to ourselves and fill in the Router MAC. */
1547 attr
.nexthop
= bgp_vrf
->originator_ip
;
1548 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1549 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1550 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
1552 /* Setup RT and encap extended community */
1553 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1555 /* get the route node in global table */
1556 rn
= bgp_afi_node_get(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1557 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1560 /* create or update the route entry within the route node */
1561 update_evpn_type5_route_entry(bgp_evpn
, bgp_vrf
, afi
, safi
, rn
, &attr
,
1564 /* schedule for processing and unlock node */
1565 if (route_changed
) {
1566 bgp_process(bgp_evpn
, rn
, afi
, safi
);
1567 bgp_unlock_node(rn
);
1570 /* uninten temporary */
1572 aspath_unintern(&attr
.aspath
);
1577 * Create or update EVPN route entry. This could be in the VNI route table
1578 * or the global route table.
1580 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1581 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1582 struct attr
*attr
, int add
,
1583 struct bgp_path_info
**pi
, uint8_t flags
,
1586 struct bgp_path_info
*tmp_pi
;
1587 struct bgp_path_info
*local_pi
;
1588 struct attr
*attr_new
;
1589 mpls_label_t label
[BGP_MAX_LABELS
];
1590 uint32_t num_labels
= 1;
1591 int route_change
= 1;
1593 struct prefix_evpn
*evp
;
1596 evp
= (struct prefix_evpn
*)&rn
->p
;
1597 memset(&label
, 0, sizeof(label
));
1599 /* See if this is an update of an existing route, or a new add. */
1601 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1602 tmp_pi
= tmp_pi
->next
) {
1603 if (tmp_pi
->peer
== bgp
->peer_self
1604 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1605 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1609 /* If route doesn't exist already, create a new one, if told to.
1610 * Otherwise act based on whether the attributes of the route have
1613 if (!local_pi
&& !add
)
1616 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1617 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1618 add_mac_mobility_to_attr(seq
, attr
);
1621 /* Add (or update) attribute to hash. */
1622 attr_new
= bgp_attr_intern(attr
);
1624 /* Extract MAC mobility sequence number, if any. */
1625 attr_new
->mm_seqnum
=
1626 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1627 attr_new
->sticky
= sticky
;
1629 /* Create new route with its attribute. */
1630 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1631 bgp
->peer_self
, attr_new
, rn
);
1632 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1633 bgp_path_info_extra_get(tmp_pi
);
1635 /* The VNI goes into the 'label' field of the route */
1636 vni2label(vpn
->vni
, &label
[0]);
1638 /* Type-2 routes may carry a second VNI - the L3-VNI.
1639 * Only attach second label if we are advertising two labels for
1642 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1643 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1646 l3vni
= bgpevpn_get_l3vni(vpn
);
1648 vni2label(l3vni
, &label
[1]);
1653 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1654 tmp_pi
->extra
->num_labels
= num_labels
;
1655 bgp_path_info_add(rn
, tmp_pi
);
1658 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1659 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1663 * The attributes have changed, type-2 routes needs to
1664 * be advertised with right labels.
1666 vni2label(vpn
->vni
, &label
[0]);
1667 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1668 && CHECK_FLAG(vpn
->flags
,
1669 VNI_FLAG_USE_TWO_LABELS
)) {
1672 l3vni
= bgpevpn_get_l3vni(vpn
);
1674 vni2label(l3vni
, &label
[1]);
1678 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1679 tmp_pi
->extra
->num_labels
= num_labels
;
1681 /* The attribute has changed. */
1682 /* Add (or update) attribute to hash. */
1683 attr_new
= bgp_attr_intern(attr
);
1684 bgp_path_info_set_flag(rn
, tmp_pi
,
1685 BGP_PATH_ATTR_CHANGED
);
1687 /* Extract MAC mobility sequence number, if any. */
1688 attr_new
->mm_seqnum
=
1689 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1690 attr_new
->sticky
= sticky
;
1692 /* Restore route, if needed. */
1693 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1694 bgp_path_info_restore(rn
, tmp_pi
);
1696 /* Unintern existing, set to new. */
1697 bgp_attr_unintern(&tmp_pi
->attr
);
1698 tmp_pi
->attr
= attr_new
;
1699 tmp_pi
->uptime
= bgp_clock();
1703 /* Return back the route entry. */
1705 return route_change
;
1708 static void evpn_zebra_reinstall_best_route(struct bgp
*bgp
,
1709 struct bgpevpn
*vpn
, struct bgp_node
*rn
)
1711 struct bgp_path_info
*tmp_ri
;
1712 struct bgp_path_info
*curr_select
= NULL
;
1714 for (tmp_ri
= bgp_node_get_bgp_path_info(rn
);
1715 tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1716 if (CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_SELECTED
)) {
1717 curr_select
= tmp_ri
;
1722 if (curr_select
&& curr_select
->type
== ZEBRA_ROUTE_BGP
1723 && curr_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1724 evpn_zebra_install(bgp
, vpn
,
1725 (struct prefix_evpn
*)&rn
->p
,
1730 * If the local route was not selected evict it and tell zebra to re-add
1731 * the best remote dest.
1733 * Typically a local path added by zebra is expected to be selected as
1734 * best. In which case when a remote path wins as best (later)
1735 * evpn_route_select_install itself evicts the older-local-best path.
1737 * However if bgp's add and zebra's add cross paths (race condition) it
1738 * is possible that the local path is no longer the "older" best path.
1739 * It is a path that was never designated as best and hence requires
1740 * additional handling to prevent bgp from injecting and holding on to a
1741 * non-best local path.
1743 static void evpn_cleanup_local_non_best_route(struct bgp
*bgp
,
1744 struct bgpevpn
*vpn
,
1745 struct bgp_node
*rn
,
1746 struct bgp_path_info
*local_pi
)
1748 char buf
[PREFIX_STRLEN
];
1750 /* local path was not picked as the winner; kick it out */
1751 if (bgp_debug_zebra(NULL
)) {
1752 zlog_debug("evicting local evpn prefix %s as remote won",
1753 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
1755 evpn_delete_old_local_route(bgp
, vpn
, rn
, local_pi
);
1756 bgp_path_info_reap(rn
, local_pi
);
1758 /* tell zebra to re-add the best remote path */
1759 evpn_zebra_reinstall_best_route(bgp
, vpn
, rn
);
1763 * Create or update EVPN route (of type based on prefix) for specified VNI
1764 * and schedule for processing.
1766 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1767 struct prefix_evpn
*p
, uint8_t flags
,
1770 struct bgp_node
*rn
;
1772 struct attr
*attr_new
;
1773 int add_l3_ecomm
= 0;
1774 struct bgp_path_info
*pi
;
1775 afi_t afi
= AFI_L2VPN
;
1776 safi_t safi
= SAFI_EVPN
;
1779 memset(&attr
, 0, sizeof(struct attr
));
1781 /* Build path-attribute for this route. */
1782 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1783 attr
.nexthop
= vpn
->originator_ip
;
1784 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1785 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1786 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1787 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1788 attr
.router_flag
= CHECK_FLAG(flags
,
1789 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1791 /* PMSI is only needed for type-3 routes */
1792 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
1793 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1794 attr
.pmsi_tnl_type
= PMSI_TNLTYPE_INGR_REPL
;
1797 /* router mac is only needed for type-2 routes here. */
1798 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1799 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1800 vni2label(vpn
->vni
, &(attr
.label
));
1802 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1803 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1806 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1807 (is_evpn_prefix_ipaddr_v4(p
) ||
1808 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1809 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1810 bgpevpn_get_l3vni(vpn
))
1813 /* Set up extended community. */
1814 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1816 /* First, create (or fetch) route node within the VNI. */
1817 /* NOTE: There is no RD here. */
1818 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1820 /* Create or update route entry. */
1821 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1822 1, &pi
, flags
, seq
);
1824 attr_new
= pi
->attr
;
1826 /* lock ri to prevent freeing in evpn_route_select_install */
1827 bgp_path_info_lock(pi
);
1828 /* Perform route selection; this is just to set the flags correctly
1829 * as local route in the VNI always wins.
1831 evpn_route_select_install(bgp
, vpn
, rn
);
1833 * If the new local route was not selected evict it and tell zebra
1834 * to re-add the best remote dest. BGP doesn't retain non-best local
1837 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1839 evpn_cleanup_local_non_best_route(bgp
, vpn
, rn
, pi
);
1841 bgp_path_info_unlock(pi
);
1843 bgp_unlock_node(rn
);
1845 /* If this is a new route or some attribute has changed, export the
1846 * route to the global table. The route will be advertised to peers
1847 * from there. Note that this table is a 2-level tree (RD-level +
1848 * Prefix-level) similar to L3VPN routes.
1851 struct bgp_path_info
*global_pi
;
1853 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1854 (struct prefix
*)p
, &vpn
->prd
);
1855 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1,
1856 &global_pi
, flags
, seq
);
1858 /* Schedule for processing and unlock node. */
1859 bgp_process(bgp
, rn
, afi
, safi
);
1860 bgp_unlock_node(rn
);
1863 /* Unintern temporary. */
1864 aspath_unintern(&attr
.aspath
);
1870 * Delete EVPN route entry.
1871 * The entry can be in ESI/VNI table or the global table.
1873 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1874 struct bgp_node
*rn
,
1875 struct bgp_path_info
**pi
)
1877 struct bgp_path_info
*tmp_pi
;
1881 /* Now, find matching route. */
1882 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1883 tmp_pi
= tmp_pi
->next
)
1884 if (tmp_pi
->peer
== bgp
->peer_self
1885 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1886 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1891 /* Mark route for delete. */
1893 bgp_path_info_delete(rn
, tmp_pi
);
1898 /* Delete EVPN ES (type-4) route */
1899 static int delete_evpn_type4_route(struct bgp
*bgp
,
1901 struct prefix_evpn
*p
)
1903 afi_t afi
= AFI_L2VPN
;
1904 safi_t safi
= SAFI_EVPN
;
1905 struct bgp_path_info
*pi
;
1906 struct bgp_node
*rn
= NULL
; /* rn in esi table */
1907 struct bgp_node
*global_rn
= NULL
; /* rn in global table */
1909 /* First, locate the route node within the ESI.
1910 * If it doesn't exist, ther is nothing to do.
1911 * Note: there is no RD here.
1913 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
1917 /* Next, locate route node in the global EVPN routing table.
1918 * Note that this table is a 2-level tree (RD-level + Prefix-level)
1920 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1921 (struct prefix
*)p
, &es
->prd
);
1924 /* Delete route entry in the global EVPN table. */
1925 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1927 /* Schedule for processing - withdraws to peers happen from
1931 bgp_process(bgp
, global_rn
, afi
, safi
);
1932 bgp_unlock_node(global_rn
);
1936 * Delete route entry in the ESI route table.
1937 * This can just be removed.
1939 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
1941 bgp_path_info_reap(rn
, pi
);
1942 bgp_unlock_node(rn
);
1946 /* Delete EVPN type5 route */
1947 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1949 afi_t afi
= AFI_L2VPN
;
1950 safi_t safi
= SAFI_EVPN
;
1951 struct bgp_node
*rn
= NULL
;
1952 struct bgp_path_info
*pi
= NULL
;
1953 struct bgp
*bgp_evpn
= NULL
; /* evpn bgp instance */
1955 bgp_evpn
= bgp_get_evpn();
1959 /* locate the global route entry for this type-5 prefix */
1960 rn
= bgp_afi_node_lookup(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1961 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1965 delete_evpn_route_entry(bgp_evpn
, afi
, safi
, rn
, &pi
);
1967 bgp_process(bgp_evpn
, rn
, afi
, safi
);
1968 bgp_unlock_node(rn
);
1973 * Delete EVPN route (of type based on prefix) for specified VNI and
1974 * schedule for processing.
1976 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1977 struct prefix_evpn
*p
)
1979 struct bgp_node
*rn
, *global_rn
;
1980 struct bgp_path_info
*pi
;
1981 afi_t afi
= AFI_L2VPN
;
1982 safi_t safi
= SAFI_EVPN
;
1984 /* First, locate the route node within the VNI. If it doesn't exist,
1986 * is nothing further to do.
1988 /* NOTE: There is no RD here. */
1989 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1993 /* Next, locate route node in the global EVPN routing table. Note that
1994 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1997 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1998 (struct prefix
*)p
, &vpn
->prd
);
2000 /* Delete route entry in the global EVPN table. */
2001 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
2003 /* Schedule for processing - withdraws to peers happen from
2007 bgp_process(bgp
, global_rn
, afi
, safi
);
2008 bgp_unlock_node(global_rn
);
2011 /* Delete route entry in the VNI route table. This can just be removed.
2013 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2015 bgp_path_info_reap(rn
, pi
);
2016 evpn_route_select_install(bgp
, vpn
, rn
);
2018 bgp_unlock_node(rn
);
2024 * Update all type-2 (MACIP) local routes for this VNI - these should also
2025 * be scheduled for advertise to peers.
2027 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2031 struct bgp_node
*rn
;
2032 struct bgp_path_info
*pi
, *tmp_pi
;
2034 struct attr
*attr_new
;
2036 int add_l3_ecomm
= 0;
2041 /* Walk this VNI's route table and update local type-2 routes. For any
2042 * routes updated, update corresponding entry in the global table too.
2044 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2045 rn
= bgp_route_next(rn
)) {
2046 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2047 struct bgp_node
*rd_rn
;
2048 struct bgp_path_info
*global_pi
;
2050 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2053 /* Identify local route. */
2054 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
2055 tmp_pi
= tmp_pi
->next
) {
2056 if (tmp_pi
->peer
== bgp
->peer_self
2057 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2058 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2066 * Build attribute per local route as the MAC mobility and
2067 * some other values could differ for different routes. The
2068 * attributes will be shared in the hash table.
2070 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
2071 attr
.nexthop
= vpn
->originator_ip
;
2072 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
2073 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
2074 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
2076 if (evpn_route_is_sticky(bgp
, rn
))
2078 else if (evpn_route_is_def_gw(bgp
, rn
)) {
2079 attr
.default_gw
= 1;
2080 if (is_evpn_prefix_ipaddr_v6(evp
))
2081 attr
.router_flag
= 1;
2084 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2085 * using L3 VNI for type-2 routes also.
2087 if ((is_evpn_prefix_ipaddr_v4(evp
) ||
2088 !IN6_IS_ADDR_LINKLOCAL(
2089 &evp
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
2090 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
2091 bgpevpn_get_l3vni(vpn
))
2094 /* Set up extended community. */
2095 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2097 seq
= mac_mobility_seqnum(tmp_pi
->attr
);
2099 /* Update the route entry. */
2100 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
, 0, &pi
,
2103 /* Perform route selection; this is just to set the flags
2104 * correctly as local route in the VNI always wins.
2106 evpn_route_select_install(bgp
, vpn
, rn
);
2108 attr_new
= pi
->attr
;
2110 /* Update route in global routing table. */
2111 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2112 (struct prefix
*)evp
, &vpn
->prd
);
2114 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
2116 mac_mobility_seqnum(attr_new
));
2118 /* Schedule for processing and unlock node. */
2119 bgp_process(bgp
, rd_rn
, afi
, safi
);
2120 bgp_unlock_node(rd_rn
);
2122 /* Unintern temporary. */
2123 aspath_unintern(&attr
.aspath
);
2131 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2132 * global routing table. These are also scheduled for withdraw from peers.
2134 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2138 struct bgp_node
*rdrn
, *rn
;
2139 struct bgp_table
*table
;
2140 struct bgp_path_info
*pi
;
2145 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
2146 if (rdrn
&& bgp_node_has_bgp_path_info_data(rdrn
)) {
2147 table
= bgp_node_get_bgp_table_info(rdrn
);
2148 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2149 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2151 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2154 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2156 bgp_process(bgp
, rn
, afi
, safi
);
2160 /* Unlock RD node. */
2162 bgp_unlock_node(rdrn
);
2168 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2169 * table as well as the per-VNI route table.
2171 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2175 struct bgp_node
*rn
;
2176 struct bgp_path_info
*pi
;
2181 /* First, walk the global route table for this VNI's type-2 local
2183 * EVPN routes are a 2-level table, first get the RD table.
2185 delete_global_type2_routes(bgp
, vpn
);
2187 /* Next, walk this VNI's route table and delete local type-2 routes. */
2188 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2189 rn
= bgp_route_next(rn
)) {
2190 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2192 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2195 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2197 /* Route entry in local table gets deleted immediately. */
2199 bgp_path_info_reap(rn
, pi
);
2206 * Delete all routes in per ES route-table
2208 static int delete_all_es_routes(struct bgp
*bgp
, struct evpnes
*es
)
2210 struct bgp_node
*rn
;
2211 struct bgp_path_info
*pi
, *nextpi
;
2213 /* Walk this ES's route table and delete all routes. */
2214 for (rn
= bgp_table_top(es
->route_table
); rn
;
2215 rn
= bgp_route_next(rn
)) {
2216 for (pi
= bgp_node_get_bgp_path_info(rn
);
2217 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2218 bgp_path_info_delete(rn
, pi
);
2219 bgp_path_info_reap(rn
, pi
);
2227 * Delete all routes in the per-VNI route table.
2229 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2231 struct bgp_node
*rn
;
2232 struct bgp_path_info
*pi
, *nextpi
;
2234 /* Walk this VNI's route table and delete all routes. */
2235 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2236 rn
= bgp_route_next(rn
)) {
2237 for (pi
= bgp_node_get_bgp_path_info(rn
);
2238 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2239 bgp_path_info_delete(rn
, pi
);
2240 bgp_path_info_reap(rn
, pi
);
2247 /* BUM traffic flood mode per-l2-vni */
2248 static int bgp_evpn_vni_flood_mode_get(struct bgp
*bgp
,
2249 struct bgpevpn
*vpn
)
2251 /* if flooding has been globally disabled per-vni mode is
2254 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
2255 return VXLAN_FLOOD_DISABLED
;
2257 /* if mcast group ip has been specified we use a PIM-SM MDT */
2258 if (vpn
->mcast_grp
.s_addr
!= INADDR_ANY
)
2259 return VXLAN_FLOOD_PIM_SM
;
2261 /* default is ingress replication */
2262 return VXLAN_FLOOD_HEAD_END_REPL
;
2266 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2267 * export RT getting modified or change to tunnel IP. Note that these
2268 * situations need the route in the per-VNI table as well as the global
2269 * table to be updated (as attributes change).
2271 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2274 struct prefix_evpn p
;
2276 /* Update and advertise the type-3 route (only one) followed by the
2277 * locally learnt type-2 routes (MACIP) - for this VNI.
2279 * RT-3 only if doing head-end replication
2281 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
2282 == VXLAN_FLOOD_HEAD_END_REPL
) {
2283 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2284 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0);
2289 return update_all_type2_routes(bgp
, vpn
);
2292 /* Delete (and withdraw) local routes for specified ES from global and ES table.
2293 * Also remove all other routes from the per ES table.
2294 * Invoked when ES is deleted.
2296 static int delete_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
2299 char buf
[ESI_STR_LEN
];
2300 struct prefix_evpn p
;
2302 /* Delete and withdraw locally learnt ES route */
2303 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
.ipaddr_v4
);
2304 ret
= delete_evpn_type4_route(bgp
, es
, &p
);
2306 flog_err(EC_BGP_EVPN_ROUTE_DELETE
,
2307 "%u failed to delete type-4 route for ESI %s",
2308 bgp
->vrf_id
, esi_to_str(&es
->esi
, buf
, sizeof(buf
)));
2311 /* Delete all routes from per ES table */
2312 return delete_all_es_routes(bgp
, es
);
2316 * Delete (and withdraw) local routes for specified VNI from the global
2317 * table and per-VNI table. After this, remove all other routes from
2318 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2319 * (advertise-all-vni) being disabled.
2321 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2324 struct prefix_evpn p
;
2326 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2327 * followed by type-3 routes (only one) - for this VNI.
2329 ret
= delete_all_type2_routes(bgp
, vpn
);
2333 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2334 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2338 /* Delete all routes from the per-VNI table. */
2339 return delete_all_vni_routes(bgp
, vpn
);
2343 * There is a flood mcast IP address change. Update the mcast-grp and
2344 * remove the type-3 route if any. A new type-3 route will be generated
2345 * post tunnel_ip update if the new flood mode is head-end-replication.
2347 static int bgp_evpn_mcast_grp_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2348 struct in_addr mcast_grp
)
2350 struct prefix_evpn p
;
2352 vpn
->mcast_grp
= mcast_grp
;
2354 if (is_vni_live(vpn
)) {
2355 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2356 delete_evpn_route(bgp
, vpn
, &p
);
2363 * There is a tunnel endpoint IP address change for this VNI, delete
2364 * prior type-3 route (if needed) and update.
2365 * Note: Route re-advertisement happens elsewhere after other processing
2368 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2369 struct in_addr originator_ip
)
2371 struct prefix_evpn p
;
2373 /* If VNI is not live, we only need to update the originator ip */
2374 if (!is_vni_live(vpn
)) {
2375 vpn
->originator_ip
= originator_ip
;
2379 /* Update the tunnel-ip hash */
2380 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2381 bgp_tip_add(bgp
, &originator_ip
);
2383 /* filter routes as martian nexthop db has changed */
2384 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2386 /* Need to withdraw type-3 route as the originator IP is part
2389 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2390 delete_evpn_route(bgp
, vpn
, &p
);
2392 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2393 vpn
->originator_ip
= originator_ip
;
2397 static struct bgp_path_info
*
2398 bgp_create_evpn_bgp_path_info(struct bgp_path_info
*parent_pi
,
2399 struct bgp_node
*rn
)
2401 struct attr
*attr_new
;
2402 struct bgp_path_info
*pi
;
2404 /* Add (or update) attribute to hash. */
2405 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2407 /* Create new route with its attribute. */
2408 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0, parent_pi
->peer
,
2410 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2411 bgp_path_info_extra_get(pi
);
2412 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2413 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2414 if (parent_pi
->extra
) {
2415 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2416 sizeof(pi
->extra
->label
));
2417 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2419 bgp_path_info_add(rn
, pi
);
2424 /* Install EVPN route entry in ES */
2425 static int install_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2426 struct prefix_evpn
*p
,
2427 struct bgp_path_info
*parent_pi
)
2430 struct bgp_node
*rn
= NULL
;
2431 struct bgp_path_info
*pi
= NULL
;
2432 struct attr
*attr_new
= NULL
;
2434 /* Create (or fetch) route within the VNI.
2435 * NOTE: There is no RD here.
2437 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
2439 /* Check if route entry is already present. */
2440 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2442 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2446 /* Add (or update) attribute to hash. */
2447 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2449 /* Create new route with its attribute. */
2450 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2451 parent_pi
->peer
, attr_new
, rn
);
2452 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2453 bgp_path_info_extra_get(pi
);
2454 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2455 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2456 bgp_path_info_add(rn
, pi
);
2458 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2459 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2460 bgp_unlock_node(rn
);
2463 /* The attribute has changed. */
2464 /* Add (or update) attribute to hash. */
2465 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2467 /* Restore route, if needed. */
2468 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2469 bgp_path_info_restore(rn
, pi
);
2471 /* Mark if nexthop has changed. */
2472 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2473 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2475 /* Unintern existing, set to new. */
2476 bgp_attr_unintern(&pi
->attr
);
2477 pi
->attr
= attr_new
;
2478 pi
->uptime
= bgp_clock();
2481 /* Perform route selection and update zebra, if required. */
2482 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2487 * Install route entry into the VRF routing table and invoke route selection.
2489 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2490 struct prefix_evpn
*evp
,
2491 struct bgp_path_info
*parent_pi
)
2493 struct bgp_node
*rn
;
2494 struct bgp_path_info
*pi
;
2496 struct attr
*attr_new
;
2499 struct prefix
*pp
= &p
;
2502 char buf
[PREFIX_STRLEN
];
2503 char buf1
[PREFIX_STRLEN
];
2505 memset(pp
, 0, sizeof(struct prefix
));
2506 ip_prefix_from_evpn_prefix(evp
, pp
);
2508 if (bgp_debug_zebra(NULL
)) {
2510 "import evpn prefix %s as ip prefix %s in vrf %s",
2511 prefix2str(evp
, buf
, sizeof(buf
)),
2512 prefix2str(pp
, buf1
, sizeof(buf
)),
2513 vrf_id_to_name(bgp_vrf
->vrf_id
));
2516 /* Create (or fetch) route within the VRF. */
2517 /* NOTE: There is no RD here. */
2518 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2520 safi
= SAFI_UNICAST
;
2521 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2522 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2524 safi
= SAFI_UNICAST
;
2525 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2529 /* EVPN routes currently only support a IPv4 next hop which corresponds
2530 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2531 * or prefix route, we have to convert the next hop to an IPv4-mapped
2532 * address for the rest of the code to flow through. In the case of IPv4,
2533 * make sure to set the flag for next hop attribute.
2535 bgp_attr_dup(&attr
, parent_pi
->attr
);
2537 evpn_convert_nexthop_to_ipv6(&attr
);
2539 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2541 /* Check if route entry is already present. */
2542 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2544 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2548 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, rn
);
2550 if (attrhash_cmp(pi
->attr
, &attr
)
2551 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2552 bgp_unlock_node(rn
);
2555 /* The attribute has changed. */
2556 /* Add (or update) attribute to hash. */
2557 attr_new
= bgp_attr_intern(&attr
);
2559 /* Restore route, if needed. */
2560 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2561 bgp_path_info_restore(rn
, pi
);
2563 /* Mark if nexthop has changed. */
2565 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2567 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2568 &attr_new
->mp_nexthop_global
)))
2569 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2571 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_ATTR_CHANGED
);
2572 /* Unintern existing, set to new. */
2573 bgp_attr_unintern(&pi
->attr
);
2574 pi
->attr
= attr_new
;
2575 pi
->uptime
= bgp_clock();
2578 bgp_aggregate_increment(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2580 /* Perform route selection and update zebra, if required. */
2581 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2583 /* Process for route leaking. */
2584 vpn_leak_from_vrf_update(bgp_get_default(), bgp_vrf
, pi
);
2590 * Install route entry into the VNI routing table and invoke route selection.
2592 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2593 struct prefix_evpn
*p
,
2594 struct bgp_path_info
*parent_pi
)
2596 struct bgp_node
*rn
;
2597 struct bgp_path_info
*pi
;
2598 struct attr
*attr_new
;
2601 /* Create (or fetch) route within the VNI. */
2602 /* NOTE: There is no RD here. */
2603 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2605 /* Check if route entry is already present. */
2606 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2608 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2612 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, rn
);
2614 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2615 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2616 bgp_unlock_node(rn
);
2619 /* The attribute has changed. */
2620 /* Add (or update) attribute to hash. */
2621 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2623 /* Restore route, if needed. */
2624 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2625 bgp_path_info_restore(rn
, pi
);
2627 /* Mark if nexthop has changed. */
2628 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2629 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2631 /* Unintern existing, set to new. */
2632 bgp_attr_unintern(&pi
->attr
);
2633 pi
->attr
= attr_new
;
2634 pi
->uptime
= bgp_clock();
2637 /* Perform route selection and update zebra, if required. */
2638 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2643 /* Uninstall EVPN route entry from ES route table */
2644 static int uninstall_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2645 struct prefix_evpn
*p
,
2646 struct bgp_path_info
*parent_pi
)
2649 struct bgp_node
*rn
;
2650 struct bgp_path_info
*pi
;
2652 if (!es
->route_table
)
2655 /* Locate route within the ESI.
2656 * NOTE: There is no RD here.
2658 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2662 /* Find matching route entry. */
2663 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2665 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2671 /* Mark entry for deletion */
2672 bgp_path_info_delete(rn
, pi
);
2674 /* Perform route selection and update zebra, if required. */
2675 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2677 /* Unlock route node. */
2678 bgp_unlock_node(rn
);
2684 * Uninstall route entry from the VRF routing table and send message
2685 * to zebra, if appropriate.
2687 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2688 struct prefix_evpn
*evp
,
2689 struct bgp_path_info
*parent_pi
)
2691 struct bgp_node
*rn
;
2692 struct bgp_path_info
*pi
;
2695 struct prefix
*pp
= &p
;
2698 char buf
[PREFIX_STRLEN
];
2699 char buf1
[PREFIX_STRLEN
];
2701 memset(pp
, 0, sizeof(struct prefix
));
2702 ip_prefix_from_evpn_prefix(evp
, pp
);
2704 if (bgp_debug_zebra(NULL
)) {
2706 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2707 prefix2str(evp
, buf
, sizeof(buf
)),
2708 prefix2str(pp
, buf1
, sizeof(buf
)),
2709 vrf_id_to_name(bgp_vrf
->vrf_id
));
2712 /* Locate route within the VRF. */
2713 /* NOTE: There is no RD here. */
2714 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2716 safi
= SAFI_UNICAST
;
2717 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2720 safi
= SAFI_UNICAST
;
2721 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2727 /* Find matching route entry. */
2728 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2730 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2736 /* Process for route leaking. */
2737 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp_vrf
, pi
);
2739 bgp_aggregate_decrement(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2741 /* Mark entry for deletion */
2742 bgp_path_info_delete(rn
, pi
);
2744 /* Perform route selection and update zebra, if required. */
2745 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2747 /* Unlock route node. */
2748 bgp_unlock_node(rn
);
2754 * Uninstall route entry from the VNI routing table and send message
2755 * to zebra, if appropriate.
2757 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2758 struct prefix_evpn
*p
,
2759 struct bgp_path_info
*parent_pi
)
2761 struct bgp_node
*rn
;
2762 struct bgp_path_info
*pi
;
2765 /* Locate route within the VNI. */
2766 /* NOTE: There is no RD here. */
2767 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2771 /* Find matching route entry. */
2772 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2774 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2780 /* Mark entry for deletion */
2781 bgp_path_info_delete(rn
, pi
);
2783 /* Perform route selection and update zebra, if required. */
2784 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2786 /* Unlock route node. */
2787 bgp_unlock_node(rn
);
2793 * Given a prefix, see if it belongs to ES.
2795 static int is_prefix_matching_for_es(struct prefix_evpn
*p
,
2798 /* if not an ES route return false */
2799 if (p
->prefix
.route_type
!= BGP_EVPN_ES_ROUTE
)
2802 if (memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
)) == 0)
2809 * Given a route entry and a VRF, see if this route entry should be
2810 * imported into the VRF i.e., RTs match.
2812 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2813 struct bgp_path_info
*pi
)
2815 struct attr
*attr
= pi
->attr
;
2816 struct ecommunity
*ecom
;
2820 /* Route should have valid RT to be even considered. */
2821 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2824 ecom
= attr
->ecommunity
;
2825 if (!ecom
|| !ecom
->size
)
2828 /* For each extended community RT, see if it matches this VNI. If any RT
2829 * matches, we're done.
2831 for (i
= 0; i
< ecom
->size
; i
++) {
2833 uint8_t type
, sub_type
;
2834 struct ecommunity_val
*eval
;
2835 struct ecommunity_val eval_tmp
;
2836 struct vrf_irt_node
*irt
;
2838 /* Only deal with RTs */
2839 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2840 eval
= (struct ecommunity_val
*)(ecom
->val
2841 + (i
* ECOMMUNITY_SIZE
));
2844 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2847 /* See if this RT matches specified VNIs import RTs */
2848 irt
= lookup_vrf_import_rt(eval
);
2850 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2853 /* Also check for non-exact match. In this, we mask out the AS
2855 * only check on the local-admin sub-field. This is to
2857 * VNI as the RT for EBGP peering too.
2860 if (type
== ECOMMUNITY_ENCODE_AS
2861 || type
== ECOMMUNITY_ENCODE_AS4
2862 || type
== ECOMMUNITY_ENCODE_IP
) {
2863 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2864 mask_ecom_global_admin(&eval_tmp
, eval
);
2865 irt
= lookup_vrf_import_rt(&eval_tmp
);
2868 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2876 * Given a route entry and a VNI, see if this route entry should be
2877 * imported into the VNI i.e., RTs match.
2879 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2880 struct bgp_path_info
*pi
)
2882 struct attr
*attr
= pi
->attr
;
2883 struct ecommunity
*ecom
;
2887 /* Route should have valid RT to be even considered. */
2888 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2891 ecom
= attr
->ecommunity
;
2892 if (!ecom
|| !ecom
->size
)
2895 /* For each extended community RT, see if it matches this VNI. If any RT
2896 * matches, we're done.
2898 for (i
= 0; i
< ecom
->size
; i
++) {
2900 uint8_t type
, sub_type
;
2901 struct ecommunity_val
*eval
;
2902 struct ecommunity_val eval_tmp
;
2903 struct irt_node
*irt
;
2905 /* Only deal with RTs */
2906 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2907 eval
= (struct ecommunity_val
*)(ecom
->val
2908 + (i
* ECOMMUNITY_SIZE
));
2911 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2914 /* See if this RT matches specified VNIs import RTs */
2915 irt
= lookup_import_rt(bgp
, eval
);
2917 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2920 /* Also check for non-exact match. In this, we mask out the AS
2922 * only check on the local-admin sub-field. This is to
2924 * VNI as the RT for EBGP peering too.
2927 if (type
== ECOMMUNITY_ENCODE_AS
2928 || type
== ECOMMUNITY_ENCODE_AS4
2929 || type
== ECOMMUNITY_ENCODE_IP
) {
2930 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2931 mask_ecom_global_admin(&eval_tmp
, eval
);
2932 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2935 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2942 static int install_uninstall_routes_for_es(struct bgp
*bgp
,
2949 char buf
[PREFIX_STRLEN
];
2950 char buf1
[ESI_STR_LEN
];
2951 struct bgp_node
*rd_rn
, *rn
;
2952 struct bgp_table
*table
;
2953 struct bgp_path_info
*pi
;
2959 * Walk entire global routing table and evaluate routes which could be
2960 * imported into this VRF. Note that we need to loop through all global
2961 * routes to determine which route matches the import rt on vrf
2963 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2964 rd_rn
= bgp_route_next(rd_rn
)) {
2965 table
= bgp_node_get_bgp_table_info(rd_rn
);
2969 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2970 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2972 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
2975 * Consider "valid" remote routes applicable for
2978 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
2979 && pi
->type
== ZEBRA_ROUTE_BGP
2980 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
2983 if (!is_prefix_matching_for_es(evp
, es
))
2987 ret
= install_evpn_route_entry_in_es(
2990 ret
= uninstall_evpn_route_entry_in_es(
2996 "Failed to %s EVPN %s route in ESI %s",
2999 prefix2str(evp
, buf
,
3001 esi_to_str(&es
->esi
, buf1
,
3011 /* This API will scan evpn routes for checking attribute's rmac
3012 * macthes with bgp instance router mac. It avoid installing
3013 * route into bgp vrf table and remote rmac in bridge table.
3015 static int bgp_evpn_route_rmac_self_check(struct bgp
*bgp_vrf
,
3016 struct prefix_evpn
*evp
,
3017 struct bgp_path_info
*pi
)
3019 /* evpn route could have learnt prior to L3vni has come up,
3020 * perform rmac check before installing route and
3021 * remote router mac.
3022 * The route will be removed from global bgp table once
3023 * SVI comes up with MAC and stored in hash, triggers
3024 * bgp_mac_rescan_all_evpn_tables.
3026 if (memcmp(&bgp_vrf
->rmac
, &pi
->attr
->rmac
, ETH_ALEN
) == 0) {
3027 if (bgp_debug_update(pi
->peer
, NULL
, NULL
, 1)) {
3028 char buf1
[PREFIX_STRLEN
];
3029 char attr_str
[BUFSIZ
] = {0};
3031 bgp_dump_attr(pi
->attr
, attr_str
, BUFSIZ
);
3033 zlog_debug("%s: bgp %u prefix %s with attr %s - DENIED due to self mac",
3034 __func__
, bgp_vrf
->vrf_id
,
3035 prefix2str(evp
, buf1
, sizeof(buf1
)),
3046 * Install or uninstall mac-ip routes are appropriate for this
3049 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
3053 struct bgp_node
*rd_rn
, *rn
;
3054 struct bgp_table
*table
;
3055 struct bgp_path_info
*pi
;
3057 char buf
[PREFIX_STRLEN
];
3058 struct bgp
*bgp_evpn
= NULL
;
3062 bgp_evpn
= bgp_get_evpn();
3066 /* Walk entire global routing table and evaluate routes which could be
3067 * imported into this VRF. Note that we need to loop through all global
3068 * routes to determine which route matches the import rt on vrf
3070 for (rd_rn
= bgp_table_top(bgp_evpn
->rib
[afi
][safi
]); rd_rn
;
3071 rd_rn
= bgp_route_next(rd_rn
)) {
3072 table
= bgp_node_get_bgp_table_info(rd_rn
);
3076 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3077 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3079 /* if not mac-ip route skip this route */
3080 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3081 || evp
->prefix
.route_type
3082 == BGP_EVPN_IP_PREFIX_ROUTE
))
3085 /* if not a mac+ip route skip this route */
3086 if (!(is_evpn_prefix_ipaddr_v4(evp
)
3087 || is_evpn_prefix_ipaddr_v6(evp
)))
3090 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3092 /* Consider "valid" remote routes applicable for
3095 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3096 && pi
->type
== ZEBRA_ROUTE_BGP
3097 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3100 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
3101 if (bgp_evpn_route_rmac_self_check(
3106 ret
= install_evpn_route_entry_in_vrf(
3109 ret
= uninstall_evpn_route_entry_in_vrf(
3115 "Failed to %s EVPN %s route in VRF %s",
3118 prefix2str(evp
, buf
,
3133 * Install or uninstall routes of specified type that are appropriate for this
3136 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
3137 struct bgpevpn
*vpn
,
3138 bgp_evpn_route_type rtype
,
3143 struct bgp_node
*rd_rn
, *rn
;
3144 struct bgp_table
*table
;
3145 struct bgp_path_info
*pi
;
3151 /* Walk entire global routing table and evaluate routes which could be
3152 * imported into this VPN. Note that we cannot just look at the routes
3154 * the VNI's RD - remote routes applicable for this VNI could have any
3157 /* EVPN routes are a 2-level table. */
3158 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
3159 rd_rn
= bgp_route_next(rd_rn
)) {
3160 table
= bgp_node_get_bgp_table_info(rd_rn
);
3164 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3165 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3167 if (evp
->prefix
.route_type
!= rtype
)
3170 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3172 /* Consider "valid" remote routes applicable for
3174 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3175 && pi
->type
== ZEBRA_ROUTE_BGP
3176 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3179 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3181 ret
= install_evpn_route_entry(
3184 ret
= uninstall_evpn_route_entry(
3190 "%u: Failed to %s EVPN %s route in VNI %u",
3194 rtype
== BGP_EVPN_MAC_IP_ROUTE
3208 /* Install any existing remote ES routes applicable for this ES into its routing
3209 * table. This is invoked when ES comes up.
3211 static int install_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
3213 return install_uninstall_routes_for_es(bgp
, es
, 1);
3217 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3218 * is invoked upon l3vni-add or l3vni import rt change
3220 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3222 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3227 * Install any existing remote routes applicable for this VNI into its
3228 * routing table. This is invoked when a VNI becomes "live" or its Import
3231 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3235 /* Install type-3 routes followed by type-2 routes - the ones applicable
3238 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3243 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3247 /* uninstall routes from l3vni vrf. */
3248 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3250 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3255 * Uninstall any existing remote routes for this VNI. One scenario in which
3256 * this is invoked is upon an import RT change.
3258 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3262 /* Uninstall type-2 routes followed by type-3 routes - the ones
3266 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3271 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3275 /* Install or unistall route in ES */
3276 static int install_uninstall_route_in_es(struct bgp
*bgp
, struct evpnes
*es
,
3277 afi_t afi
, safi_t safi
,
3278 struct prefix_evpn
*evp
,
3279 struct bgp_path_info
*pi
, int install
)
3282 char buf
[ESI_STR_LEN
];
3285 ret
= install_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3287 ret
= uninstall_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3292 "%u: Failed to %s EVPN %s route in ESI %s", bgp
->vrf_id
,
3293 install
? "install" : "uninstall", "ES",
3294 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)));
3301 * Install or uninstall route in matching VRFs (list).
3303 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3304 safi_t safi
, struct prefix_evpn
*evp
,
3305 struct bgp_path_info
*pi
,
3306 struct list
*vrfs
, int install
)
3308 char buf
[PREFIX2STR_BUFFER
];
3309 struct bgp
*bgp_vrf
;
3310 struct listnode
*node
, *nnode
;
3312 /* Only type-2/type-5 routes go into a VRF */
3313 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3314 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3317 /* if it is type-2 route and not a mac+ip route skip this route */
3318 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3319 && !(is_evpn_prefix_ipaddr_v4(evp
)
3320 || is_evpn_prefix_ipaddr_v6(evp
)))
3323 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3327 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3329 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3333 flog_err(EC_BGP_EVPN_FAIL
,
3334 "%u: Failed to %s prefix %s in VRF %s",
3336 install
? "install" : "uninstall",
3337 prefix2str(evp
, buf
, sizeof(buf
)),
3338 vrf_id_to_name(bgp_vrf
->vrf_id
));
3347 * Install or uninstall route in matching VNIs (list).
3349 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3350 safi_t safi
, struct prefix_evpn
*evp
,
3351 struct bgp_path_info
*pi
,
3352 struct list
*vnis
, int install
)
3354 struct bgpevpn
*vpn
;
3355 struct listnode
*node
, *nnode
;
3357 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3360 if (!is_vni_live(vpn
))
3364 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3366 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3369 flog_err(EC_BGP_EVPN_FAIL
,
3370 "%u: Failed to %s EVPN %s route in VNI %u",
3371 bgp
->vrf_id
, install
? "install" : "uninstall",
3372 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3384 * Install or uninstall route for appropriate VNIs/ESIs.
3386 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3388 struct bgp_path_info
*pi
, int import
)
3390 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3391 struct attr
*attr
= pi
->attr
;
3392 struct ecommunity
*ecom
;
3397 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3398 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3399 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3400 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3401 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3404 /* If we don't have Route Target, nothing much to do. */
3405 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3408 ecom
= attr
->ecommunity
;
3409 if (!ecom
|| !ecom
->size
)
3412 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3413 * attached to the route */
3414 for (i
= 0; i
< ecom
->size
; i
++) {
3416 uint8_t type
, sub_type
;
3417 struct ecommunity_val
*eval
;
3418 struct ecommunity_val eval_tmp
;
3419 struct irt_node
*irt
; /* import rt for l2vni */
3420 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3423 /* Only deal with RTs */
3424 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3425 eval
= (struct ecommunity_val
*)(ecom
->val
3426 + (i
* ECOMMUNITY_SIZE
));
3429 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3433 * macip routes (type-2) are imported into VNI and VRF tables.
3434 * IMET route is imported into VNI table.
3435 * prefix routes are imported into VRF table.
3437 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3438 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3439 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3441 irt
= lookup_import_rt(bgp
, eval
);
3443 install_uninstall_route_in_vnis(
3444 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3447 vrf_irt
= lookup_vrf_import_rt(eval
);
3449 install_uninstall_route_in_vrfs(
3450 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3453 /* Also check for non-exact match.
3454 * In this, we mask out the AS and
3455 * only check on the local-admin sub-field.
3456 * This is to facilitate using
3457 * VNI as the RT for EBGP peering too.
3461 if (type
== ECOMMUNITY_ENCODE_AS
3462 || type
== ECOMMUNITY_ENCODE_AS4
3463 || type
== ECOMMUNITY_ENCODE_IP
) {
3464 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3465 mask_ecom_global_admin(&eval_tmp
, eval
);
3466 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3467 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3471 install_uninstall_route_in_vnis(
3472 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3475 install_uninstall_route_in_vrfs(
3476 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3480 /* es route is imported into the es table */
3481 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3483 /* we will match based on the entire esi to avoid
3484 * imoort of an es route for esi2 into esi1
3486 es
= bgp_evpn_lookup_es(bgp
, &evp
->prefix
.es_addr
.esi
);
3487 if (es
&& is_es_local(es
))
3488 install_uninstall_route_in_es(
3489 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3497 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3500 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3502 /* Delete ipv4 default route and withdraw from peers */
3503 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3504 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3505 SAFI_UNICAST
, false);
3507 /* delete all ipv4 routes and withdraw from peers */
3508 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3509 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3511 /* Delete ipv6 default route and withdraw from peers */
3512 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3513 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3514 SAFI_UNICAST
, false);
3516 /* delete all ipv6 routes and withdraw from peers */
3517 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3518 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3522 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3525 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3527 /* update all ipv4 routes */
3528 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3529 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3531 /* update ipv4 default route and withdraw from peers */
3532 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3533 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3534 SAFI_UNICAST
, true);
3536 /* update all ipv6 routes */
3537 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3538 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3540 /* update ipv6 default route and withdraw from peers */
3541 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3542 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3543 SAFI_UNICAST
, true);
3548 * update and advertise local routes for a VRF as type-5 routes.
3549 * This is invoked upon RD change for a VRF. Note taht the processing is only
3550 * done in the global route table using the routes which already exist in the
3553 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3555 /* skip if the RD is configured */
3556 if (is_vrf_rd_configured(bgp_vrf
))
3559 /* derive the RD for the VRF based on new router-id */
3560 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3562 /* update advertise ipv4|ipv6 routes as type-5 routes */
3563 update_advertise_vrf_routes(bgp_vrf
);
3567 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3568 * This is invoked upon VRF RD change. The processing is done only from global
3571 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3573 /* skip if the RD is configured */
3574 if (is_vrf_rd_configured(bgp_vrf
))
3577 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3578 delete_withdraw_vrf_routes(bgp_vrf
);
3582 * Update and advertise local routes for a VNI. Invoked upon router-id
3583 * change. Note that the processing is done only on the global route table
3584 * using routes that already exist in the per-VNI table.
3586 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3588 struct prefix_evpn p
;
3589 struct bgp_node
*rn
, *global_rn
;
3590 struct bgp_path_info
*pi
, *global_pi
;
3592 afi_t afi
= AFI_L2VPN
;
3593 safi_t safi
= SAFI_EVPN
;
3595 /* Locate type-3 route for VNI in the per-VNI table and use its
3596 * attributes to create and advertise the type-3 route for this VNI
3597 * in the global table.
3599 * RT-3 only if doing head-end replication
3601 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3602 == VXLAN_FLOOD_HEAD_END_REPL
) {
3603 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3604 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3605 if (!rn
) /* unexpected */
3607 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3608 if (pi
->peer
== bgp
->peer_self
&&
3609 pi
->type
== ZEBRA_ROUTE_BGP
3610 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3612 if (!pi
) /* unexpected */
3616 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3617 (struct prefix
*)&p
, &vpn
->prd
);
3618 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
,
3619 1, &pi
, 0, mac_mobility_seqnum(attr
));
3621 /* Schedule for processing and unlock node. */
3622 bgp_process(bgp
, global_rn
, afi
, safi
);
3623 bgp_unlock_node(global_rn
);
3626 /* Now, walk this VNI's route table and use the route and its attribute
3627 * to create and schedule route in global table.
3629 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
3630 rn
= bgp_route_next(rn
)) {
3631 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3633 /* Identify MAC-IP local routes. */
3634 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3637 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3638 if (pi
->peer
== bgp
->peer_self
3639 && pi
->type
== ZEBRA_ROUTE_BGP
3640 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3645 /* Create route in global routing table using this route entry's
3649 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3650 (struct prefix
*)evp
, &vpn
->prd
);
3652 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
3654 mac_mobility_seqnum(attr
));
3656 /* Schedule for processing and unlock node. */
3657 bgp_process(bgp
, global_rn
, afi
, safi
);
3658 bgp_unlock_node(global_rn
);
3665 * Delete (and withdraw) local routes for a VNI - only from the global
3666 * table. Invoked upon router-id change.
3668 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3671 struct prefix_evpn p
;
3672 struct bgp_node
*global_rn
;
3673 struct bgp_path_info
*pi
;
3674 afi_t afi
= AFI_L2VPN
;
3675 safi_t safi
= SAFI_EVPN
;
3677 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3678 * for this VNI - from the global table.
3680 ret
= delete_global_type2_routes(bgp
, vpn
);
3684 /* Remove type-3 route for this VNI from global table. */
3685 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3686 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3687 (struct prefix
*)&p
, &vpn
->prd
);
3689 /* Delete route entry in the global EVPN table. */
3690 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
3692 /* Schedule for processing - withdraws to peers happen from
3696 bgp_process(bgp
, global_rn
, afi
, safi
);
3697 bgp_unlock_node(global_rn
);
3704 * Handle router-id change. Update and advertise local routes corresponding
3705 * to this VNI from peers. Note that this is invoked after updating the
3706 * router-id. The routes in the per-VNI table are used to create routes in
3707 * the global table and schedule them.
3709 static void update_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3711 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3713 /* Skip VNIs with configured RD. */
3714 if (is_rd_configured(vpn
))
3717 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3718 update_advertise_vni_routes(bgp
, vpn
);
3722 * Handle router-id change. Delete and withdraw local routes corresponding
3723 * to this VNI from peers. Note that this is invoked prior to updating
3724 * the router-id and is done only on the global route table, the routes
3725 * are needed in the per-VNI table to re-advertise with new router id.
3727 static void withdraw_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3729 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3731 /* Skip VNIs with configured RD. */
3732 if (is_rd_configured(vpn
))
3735 delete_withdraw_vni_routes(bgp
, vpn
);
3739 * Create RT-3 for a VNI and schedule for processing and advertisement.
3740 * This is invoked upon flooding mode changing to head-end replication.
3742 static void create_advertise_type3(struct hash_bucket
*bucket
, void *data
)
3744 struct bgpevpn
*vpn
= bucket
->data
;
3745 struct bgp
*bgp
= data
;
3746 struct prefix_evpn p
;
3748 if (!vpn
|| !is_vni_live(vpn
) ||
3749 bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3750 != VXLAN_FLOOD_HEAD_END_REPL
)
3753 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3754 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0))
3755 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3756 "Type3 route creation failure for VNI %u", vpn
->vni
);
3760 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3761 * This is invoked upon flooding mode changing to drop BUM packets.
3763 static void delete_withdraw_type3(struct hash_bucket
*bucket
, void *data
)
3765 struct bgpevpn
*vpn
= bucket
->data
;
3766 struct bgp
*bgp
= data
;
3767 struct prefix_evpn p
;
3769 if (!vpn
|| !is_vni_live(vpn
))
3772 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3773 delete_evpn_route(bgp
, vpn
, &p
);
3777 * Process received EVPN type-2 route (advertise or withdraw).
3779 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3780 struct attr
*attr
, uint8_t *pfx
, int psize
,
3781 uint32_t addpath_id
)
3783 struct prefix_rd prd
;
3784 struct prefix_evpn p
;
3785 struct bgp_route_evpn evpn
;
3787 uint8_t macaddr_len
;
3788 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
3789 uint32_t num_labels
= 0;
3793 /* Type-2 route should be either 33, 37 or 49 bytes or an
3794 * additional 3 bytes if there is a second label (VNI):
3795 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3796 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3797 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3799 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3800 && psize
!= 40 && psize
!= 52) {
3801 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3802 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3803 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3807 memset(&evpn
, 0, sizeof(evpn
));
3809 /* Make prefix_rd */
3810 prd
.family
= AF_UNSPEC
;
3812 memcpy(&prd
.val
, pfx
, 8);
3815 /* Make EVPN prefix. */
3816 memset(&p
, 0, sizeof(struct prefix_evpn
));
3818 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3819 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3821 /* Copy Ethernet Seg Identifier */
3822 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
3825 /* Copy Ethernet Tag */
3826 memcpy(ð_tag
, pfx
, 4);
3827 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3830 /* Get the MAC Addr len */
3831 macaddr_len
= *pfx
++;
3833 /* Get the MAC Addr */
3834 if (macaddr_len
== (ETH_ALEN
* 8)) {
3835 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
3839 EC_BGP_EVPN_ROUTE_INVALID
,
3840 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3841 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3847 ipaddr_len
= *pfx
++;
3848 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3849 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3851 EC_BGP_EVPN_ROUTE_INVALID
,
3852 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3853 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3858 ipaddr_len
/= 8; /* Convert to bytes. */
3859 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3862 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
3866 /* Get the VNI(s). Stored as bytes here. */
3868 memset(label
, 0, sizeof(label
));
3869 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
3870 pfx
+= BGP_LABEL_BYTES
;
3871 psize
-= (33 + ipaddr_len
);
3872 /* Do we have a second VNI? */
3875 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
3877 * If in future, we are required to access additional fields,
3878 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
3883 /* Process the route. */
3885 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3886 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3887 &prd
, &label
[0], num_labels
, 0, &evpn
);
3889 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3890 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3891 &prd
, &label
[0], num_labels
, &evpn
);
3896 * Process received EVPN type-3 route (advertise or withdraw).
3898 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3899 struct attr
*attr
, uint8_t *pfx
, int psize
,
3900 uint32_t addpath_id
)
3902 struct prefix_rd prd
;
3903 struct prefix_evpn p
;
3908 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3909 * IP len (1) and IP (4 or 16).
3911 if (psize
!= 17 && psize
!= 29) {
3912 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3913 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3914 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3918 /* If PMSI is present, log if it is anything other than IR.
3919 * Note: We just simply ignore the values as it is not clear if
3920 * doing anything else is better.
3923 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3924 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
&&
3925 attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_PIM_SM
) {
3926 flog_warn(EC_BGP_EVPN_PMSI_PRESENT
,
3927 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3928 peer
->bgp
->vrf_id
, peer
->host
,
3929 attr
->pmsi_tnl_type
);
3933 /* Make prefix_rd */
3934 prd
.family
= AF_UNSPEC
;
3936 memcpy(&prd
.val
, pfx
, 8);
3939 /* Make EVPN prefix. */
3940 memset(&p
, 0, sizeof(struct prefix_evpn
));
3942 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3943 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3945 /* Copy Ethernet Tag */
3946 memcpy(ð_tag
, pfx
, 4);
3947 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
3951 ipaddr_len
= *pfx
++;
3952 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3953 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
3954 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
3957 EC_BGP_EVPN_ROUTE_INVALID
,
3958 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3959 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3963 /* Process the route. */
3965 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3966 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3967 &prd
, NULL
, 0, 0, NULL
);
3969 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3970 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3971 &prd
, NULL
, 0, NULL
);
3976 * Process received EVPN type-4 route (advertise or withdraw).
3978 static int process_type4_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3979 struct attr
*attr
, uint8_t *pfx
, int psize
,
3980 uint32_t addpath_id
)
3985 struct in_addr vtep_ip
;
3986 struct prefix_rd prd
;
3987 struct prefix_evpn p
;
3989 /* Type-4 route should be either 23 or 35 bytes
3990 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
3992 if (psize
!= 23 && psize
!= 35) {
3993 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3994 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
3995 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3999 /* Make prefix_rd */
4000 prd
.family
= AF_UNSPEC
;
4002 memcpy(&prd
.val
, pfx
, 8);
4006 memcpy(&esi
, pfx
, ESI_BYTES
);
4011 ipaddr_len
= *pfx
++;
4012 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
4013 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
4016 EC_BGP_EVPN_ROUTE_INVALID
,
4017 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
4018 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4022 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
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
, NULL
, 0, 0, NULL
);
4029 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4030 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4031 &prd
, NULL
, 0, NULL
);
4038 * Process received EVPN type-5 route (advertise or withdraw).
4040 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4041 struct attr
*attr
, uint8_t *pfx
, int psize
,
4042 uint32_t addpath_id
)
4044 struct prefix_rd prd
;
4045 struct prefix_evpn p
;
4046 struct bgp_route_evpn evpn
;
4049 mpls_label_t label
; /* holds the VNI as in the packet */
4052 /* Type-5 route should be 34 or 58 bytes:
4053 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
4054 * GW (4 or 16) and VNI (3).
4055 * Note that the IP and GW should both be IPv4 or both IPv6.
4057 if (psize
!= 34 && psize
!= 58) {
4058 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4059 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
4060 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4064 /* Make prefix_rd */
4065 prd
.family
= AF_UNSPEC
;
4067 memcpy(&prd
.val
, pfx
, 8);
4070 /* Make EVPN prefix. */
4071 memset(&p
, 0, sizeof(struct prefix_evpn
));
4073 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4074 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
4076 /* Additional information outside of prefix - ESI and GW IP */
4077 memset(&evpn
, 0, sizeof(evpn
));
4080 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
4083 /* Fetch Ethernet Tag. */
4084 memcpy(ð_tag
, pfx
, 4);
4085 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
4088 /* Fetch IP prefix length. */
4090 if (ippfx_len
> IPV6_MAX_BITLEN
) {
4092 EC_BGP_EVPN_ROUTE_INVALID
,
4093 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4094 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
4097 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
4099 /* Determine IPv4 or IPv6 prefix */
4100 /* Since the address and GW are from the same family, this just becomes
4101 * a simple check on the total size.
4104 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
4105 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
4107 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
4110 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
4111 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
4113 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
4117 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4118 memset(&label
, 0, sizeof(label
));
4119 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
4122 * If in future, we are required to access additional fields,
4123 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4127 /* Process the route. */
4129 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4130 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4131 &prd
, &label
, 1, 0, &evpn
);
4133 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4134 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4135 &prd
, &label
, 1, &evpn
);
4140 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
4141 struct prefix_rd
*prd
, mpls_label_t
*label
,
4142 uint32_t num_labels
, struct attr
*attr
)
4146 struct evpn_addr
*p_evpn_p
;
4148 memset(&temp
, 0, 16);
4149 if (p
->family
!= AF_EVPN
)
4151 p_evpn_p
= &(p
->u
.prefix_evpn
);
4153 /* len denites the total len of IP and GW-IP in the route
4154 IP and GW-IP have to be both ipv4 or ipv6
4156 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4157 len
= 8; /* IP and GWIP are both ipv4 */
4159 len
= 32; /* IP and GWIP are both ipv6 */
4160 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4161 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4162 stream_put(s
, prd
->val
, 8);
4164 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
4166 stream_put(s
, &temp
, 10);
4167 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4168 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4169 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4170 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4172 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4174 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4176 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
4178 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
4180 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4181 stream_put_ipv4(s
, 0);
4183 stream_put(s
, &temp
, 16);
4187 stream_put(s
, label
, 3);
4193 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4195 static void cleanup_vni_on_disable(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4197 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4199 /* Remove EVPN routes and schedule for processing. */
4200 delete_routes_for_vni(bgp
, vpn
);
4202 /* Clear "live" flag and see if hash needs to be freed. */
4203 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4204 if (!is_vni_configured(vpn
))
4205 bgp_evpn_free(bgp
, vpn
);
4209 * Free a VNI entry; iterator function called during cleanup.
4211 static void free_vni_entry(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4213 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4215 delete_all_vni_routes(bgp
, vpn
);
4216 bgp_evpn_free(bgp
, vpn
);
4220 * Derive AUTO import RT for BGP VRF - L3VNI
4222 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4224 struct bgp
*bgp_evpn
= NULL
;
4226 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4227 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4230 bgp_evpn
= bgp_get_evpn();
4233 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4237 * Delete AUTO import RT from BGP VRF - L3VNI
4239 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4241 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4245 * Derive AUTO export RT for BGP VRF - L3VNI
4247 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4249 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4250 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4254 * Delete AUTO export RT from BGP VRF - L3VNI
4256 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4258 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4261 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4263 struct bgp
*bgp_evpn
= NULL
;
4264 struct listnode
*node
= NULL
;
4265 struct bgpevpn
*vpn
= NULL
;
4267 bgp_evpn
= bgp_get_evpn();
4271 /* update all type-5 routes */
4272 update_advertise_vrf_routes(bgp_vrf
);
4274 /* update all type-2 routes */
4275 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4276 update_routes_for_vni(bgp_evpn
, vpn
);
4280 * Handle autort change for a given VNI.
4282 static void update_autort_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4284 struct bgpevpn
*vpn
= bucket
->data
;
4286 if (!is_import_rt_configured(vpn
)) {
4287 if (is_vni_live(vpn
))
4288 bgp_evpn_uninstall_routes(bgp
, vpn
);
4289 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4290 list_delete_all_node(vpn
->import_rtl
);
4291 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4292 if (is_vni_live(vpn
))
4293 bgp_evpn_install_routes(bgp
, vpn
);
4295 if (!is_export_rt_configured(vpn
)) {
4296 list_delete_all_node(vpn
->export_rtl
);
4297 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4298 if (is_vni_live(vpn
))
4299 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4307 /* withdraw type-5 route corresponding to ip prefix */
4308 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4309 afi_t afi
, safi_t safi
)
4312 struct prefix_evpn evp
;
4313 char buf
[PREFIX_STRLEN
];
4315 build_type5_prefix_from_ip_prefix(&evp
, p
);
4316 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4319 EC_BGP_EVPN_ROUTE_DELETE
,
4320 "%u failed to delete type-5 route for prefix %s in vrf %s",
4321 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4322 vrf_id_to_name(bgp_vrf
->vrf_id
));
4326 /* withdraw all type-5 routes for an address family */
4327 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4329 struct bgp_table
*table
= NULL
;
4330 struct bgp_node
*rn
= NULL
;
4331 struct bgp_path_info
*pi
;
4333 table
= bgp_vrf
->rib
[afi
][safi
];
4334 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4335 /* Only care about "selected" routes. Also ensure that
4336 * these are routes that are injectable into EVPN.
4338 /* TODO: Support for AddPath for EVPN. */
4339 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4340 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4341 && is_route_injectable_into_evpn(pi
)) {
4342 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
4351 * evpn - enable advertisement of default g/w
4353 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
4354 safi_t safi
, bool add
)
4356 struct prefix ip_prefix
;
4358 /* form the default prefix 0.0.0.0/0 */
4359 memset(&ip_prefix
, 0, sizeof(struct prefix
));
4360 ip_prefix
.family
= afi2family(afi
);
4363 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
4366 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
4373 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4374 * to this function correspond to those of the source IP prefix (best
4375 * path in the case of the attr. In the case of a local prefix (when we
4376 * are advertising local subnets), the src_attr will be NULL.
4378 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4379 struct attr
*src_attr
, afi_t afi
,
4383 struct prefix_evpn evp
;
4384 char buf
[PREFIX_STRLEN
];
4386 build_type5_prefix_from_ip_prefix(&evp
, p
);
4387 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4389 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4390 "%u: Failed to create type-5 route for prefix %s",
4391 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4394 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4395 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4396 * advertisement is enabled.
4398 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4401 struct bgp_table
*table
= NULL
;
4402 struct bgp_node
*rn
= NULL
;
4403 struct bgp_path_info
*pi
;
4405 table
= bgp_vrf
->rib
[afi
][safi
];
4406 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4407 /* Need to identify the "selected" route entry to use its
4408 * attribute. Also, ensure that the route is injectable
4410 * TODO: Support for AddPath for EVPN.
4412 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4413 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4414 && is_route_injectable_into_evpn(pi
)) {
4416 /* apply the route-map */
4417 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4418 route_map_result_t ret
;
4420 ret
= route_map_apply(
4421 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4423 &rn
->p
, RMAP_BGP
, pi
);
4424 if (ret
== RMAP_DENYMATCH
)
4427 bgp_evpn_advertise_type5_route(
4428 bgp_vrf
, &rn
->p
, pi
->attr
, afi
, safi
);
4435 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4437 struct listnode
*node
, *nnode
, *node_to_del
;
4438 struct ecommunity
*ecom
, *ecom_auto
;
4439 struct ecommunity_val eval
;
4441 if (bgp
->advertise_autort_rfc8365
)
4442 vni
|= EVPN_AUTORT_VXLAN
;
4443 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4445 ecom_auto
= ecommunity_new();
4446 ecommunity_add_val(ecom_auto
, &eval
);
4449 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4450 if (ecommunity_match(ecom
, ecom_auto
)) {
4451 ecommunity_free(&ecom
);
4457 list_delete_node(rtl
, node_to_del
);
4459 ecommunity_free(&ecom_auto
);
4462 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4463 struct ecommunity
*ecomadd
)
4465 /* uninstall routes from vrf */
4466 if (is_l3vni_live(bgp_vrf
))
4467 uninstall_routes_for_vrf(bgp_vrf
);
4469 /* Cleanup the RT to VRF mapping */
4470 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4472 /* Remove auto generated RT */
4473 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4475 /* Add the newly configured RT to RT list */
4476 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4477 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4479 /* map VRF to its RTs and install routes matching the new RTs */
4480 if (is_l3vni_live(bgp_vrf
)) {
4481 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4482 install_routes_for_vrf(bgp_vrf
);
4486 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4487 struct ecommunity
*ecomdel
)
4489 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4490 struct ecommunity
*ecom
= NULL
;
4492 /* uninstall routes from vrf */
4493 if (is_l3vni_live(bgp_vrf
))
4494 uninstall_routes_for_vrf(bgp_vrf
);
4496 /* Cleanup the RT to VRF mapping */
4497 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4499 /* remove the RT from the RT list */
4500 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4501 if (ecommunity_match(ecom
, ecomdel
)) {
4502 ecommunity_free(&ecom
);
4509 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4511 assert(bgp_vrf
->vrf_import_rtl
);
4512 /* fallback to auto import rt, if this was the last RT */
4513 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4514 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4515 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4518 /* map VRFs to its RTs and install routes matching this new RT */
4519 if (is_l3vni_live(bgp_vrf
)) {
4520 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4521 install_routes_for_vrf(bgp_vrf
);
4525 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4526 struct ecommunity
*ecomadd
)
4528 /* remove auto-generated RT */
4529 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4531 /* Add the new RT to the RT list */
4532 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4533 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4535 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4538 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4539 struct ecommunity
*ecomdel
)
4541 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4542 struct ecommunity
*ecom
= NULL
;
4544 /* Remove the RT from the RT list */
4545 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4546 if (ecommunity_match(ecom
, ecomdel
)) {
4547 ecommunity_free(&ecom
);
4554 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4557 * Temporary assert to make SA happy.
4558 * The ALL_LIST_ELEMENTS macro above has a NULL check
4559 * which means that SA is going to complain about
4560 * the list_isempty call, which doesn't NULL check.
4561 * So until we get this situation cleaned up, here
4564 assert(bgp_vrf
->vrf_export_rtl
);
4566 /* fall back to auto-generated RT if this was the last RT */
4567 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4568 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4569 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4572 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4576 * Handle change to BGP router id. This is invoked twice by the change
4577 * handler, first before the router id has been changed and then after
4578 * the router id has been changed. The first invocation will result in
4579 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4580 * will result in the routes being re-advertised.
4582 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4586 /* delete and withdraw all the type-5 routes
4587 stored in the global table for this vrf
4589 withdraw_router_id_vrf(bgp
);
4591 /* delete all the VNI routes (type-2/type-3) routes for all the
4594 hash_iterate(bgp
->vnihash
,
4595 (void (*)(struct hash_bucket
*,
4596 void *))withdraw_router_id_vni
,
4600 /* advertise all routes in the vrf as type-5 routes with the new
4603 update_router_id_vrf(bgp
);
4605 /* advertise all the VNI routes (type-2/type-3) routes with the
4608 hash_iterate(bgp
->vnihash
,
4609 (void (*)(struct hash_bucket
*,
4610 void *))update_router_id_vni
,
4616 * Handle change to auto-RT algorithm - update and advertise local routes.
4618 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4620 hash_iterate(bgp
->vnihash
,
4621 (void (*)(struct hash_bucket
*,
4622 void*))update_autort_vni
,
4627 * Handle change to export RT - update and advertise local routes.
4629 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4631 return update_routes_for_vni(bgp
, vpn
);
4634 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4637 delete_withdraw_vrf_routes(bgp_vrf
);
4639 update_advertise_vrf_routes(bgp_vrf
);
4643 * Handle change to RD. This is invoked twice by the change handler,
4644 * first before the RD has been changed and then after the RD has
4645 * been changed. The first invocation will result in local routes
4646 * of this VNI being deleted and withdrawn and the next will result
4647 * in the routes being re-advertised.
4649 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4653 delete_withdraw_vni_routes(bgp
, vpn
);
4655 update_advertise_vni_routes(bgp
, vpn
);
4659 * Install routes for this VNI. Invoked upon change to Import RT.
4661 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4663 return install_routes_for_vni(bgp
, vpn
);
4667 * Uninstall all routes installed for this VNI. Invoked upon change
4670 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4672 return uninstall_routes_for_vni(bgp
, vpn
);
4676 * TODO: Hardcoded for a maximum of 2 VNIs right now
4678 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4683 vni1
= label2vni(label
);
4684 if (num_labels
== 2) {
4685 vni2
= label2vni(label
+ 1);
4686 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4688 snprintf(buf
, len
, "%u", vni1
);
4693 * Function to convert evpn route to json format.
4694 * NOTE: We don't use prefix2str as the output here is a bit different.
4696 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
4698 char buf1
[ETHER_ADDR_STRLEN
];
4699 char buf2
[PREFIX2STR_BUFFER
];
4706 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4708 switch (p
->prefix
.route_type
) {
4709 case BGP_EVPN_MAC_IP_ROUTE
:
4710 json_object_int_add(json
, "ethTag",
4711 p
->prefix
.macip_addr
.eth_tag
);
4712 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4713 json_object_string_add(json
, "mac",
4714 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4717 if (!is_evpn_prefix_ipaddr_none(p
)) {
4718 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
:
4720 prefixlen
= (family
== AF_INET
) ?
4721 IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
;
4722 inet_ntop(family
, &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4723 buf2
, PREFIX2STR_BUFFER
);
4724 json_object_int_add(json
, "ipLen", prefixlen
);
4725 json_object_string_add(json
, "ip", buf2
);
4729 case BGP_EVPN_IMET_ROUTE
:
4730 json_object_int_add(json
, "ethTag",
4731 p
->prefix
.imet_addr
.eth_tag
);
4732 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4733 prefixlen
= (family
== AF_INET
) ? IPV4_MAX_BITLEN
:
4735 inet_ntop(family
, &p
->prefix
.imet_addr
.ip
.ip
.addr
, buf2
,
4737 json_object_int_add(json
, "ipLen", prefixlen
);
4738 json_object_string_add(json
, "ip", buf2
);
4741 case BGP_EVPN_IP_PREFIX_ROUTE
:
4742 json_object_int_add(json
, "ethTag",
4743 p
->prefix
.prefix_addr
.eth_tag
);
4744 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4745 inet_ntop(family
, &p
->prefix
.prefix_addr
.ip
.ip
.addr
,
4746 buf2
, sizeof(buf2
));
4747 json_object_int_add(json
, "ipLen",
4748 p
->prefix
.prefix_addr
.ip_prefix_length
);
4749 json_object_string_add(json
, "ip", buf2
);
4758 * Function to convert evpn route to string.
4759 * NOTE: We don't use prefix2str as the output here is a bit different.
4761 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
4763 char buf1
[ETHER_ADDR_STRLEN
];
4764 char buf2
[PREFIX2STR_BUFFER
];
4765 char buf3
[ESI_STR_LEN
];
4767 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4768 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
4769 p
->prefix
.imet_addr
.eth_tag
,
4770 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4772 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4773 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4774 if (is_evpn_prefix_ipaddr_none(p
))
4775 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4776 p
->prefix
.route_type
,
4777 p
->prefix
.macip_addr
.eth_tag
,
4779 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4784 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4786 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
4787 p
->prefix
.route_type
,
4788 p
->prefix
.macip_addr
.eth_tag
,
4790 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4792 family
== AF_INET
? IPV4_MAX_BITLEN
4795 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4797 PREFIX2STR_BUFFER
));
4799 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
4800 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4801 p
->prefix
.route_type
,
4802 p
->prefix
.prefix_addr
.eth_tag
,
4803 p
->prefix
.prefix_addr
.ip_prefix_length
,
4804 is_evpn_prefix_ipaddr_v4(p
)
4805 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
4806 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
4807 } else if (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
4808 snprintf(buf
, len
, "[%d]:[%s]:[%d]:[%s]",
4809 p
->prefix
.route_type
,
4810 esi_to_str(&p
->prefix
.es_addr
.esi
, buf3
, sizeof(buf3
)),
4811 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4813 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
));
4815 /* For EVPN route types not supported yet. */
4816 snprintf(buf
, len
, "(unsupported route type %d)",
4817 p
->prefix
.route_type
);
4824 * Encode EVPN prefix in Update (MP_REACH)
4826 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
4827 struct prefix_rd
*prd
, mpls_label_t
*label
,
4828 uint32_t num_labels
, struct attr
*attr
,
4829 int addpath_encode
, uint32_t addpath_tx_id
)
4831 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4832 int len
, ipa_len
= 0;
4835 stream_putl(s
, addpath_tx_id
);
4838 stream_putc(s
, evp
->prefix
.route_type
);
4840 switch (evp
->prefix
.route_type
) {
4841 case BGP_EVPN_MAC_IP_ROUTE
:
4842 if (is_evpn_prefix_ipaddr_v4(evp
))
4843 ipa_len
= IPV4_MAX_BYTELEN
;
4844 else if (is_evpn_prefix_ipaddr_v6(evp
))
4845 ipa_len
= IPV6_MAX_BYTELEN
;
4846 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4847 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4848 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4850 stream_putc(s
, len
);
4851 stream_put(s
, prd
->val
, 8); /* RD */
4853 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
4855 stream_put(s
, 0, 10);
4856 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4857 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4858 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4859 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4860 if (ipa_len
) /* IP */
4861 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4863 /* 1st label is the L2 VNI */
4864 stream_put(s
, label
, BGP_LABEL_BYTES
);
4865 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4866 if (ipa_len
&& num_labels
> 1)
4867 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4870 case BGP_EVPN_IMET_ROUTE
:
4871 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4872 stream_put(s
, prd
->val
, 8); /* RD */
4873 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4874 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4875 /* Originating Router's IP Addr */
4876 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4879 case BGP_EVPN_ES_ROUTE
:
4880 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4881 stream_put(s
, prd
->val
, 8); /* RD */
4882 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4883 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4885 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4888 case BGP_EVPN_IP_PREFIX_ROUTE
:
4889 /* TODO: AddPath support. */
4890 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4898 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4899 struct bgp_nlri
*packet
, int withdraw
)
4905 uint32_t addpath_id
;
4906 int addpath_encoded
;
4911 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4913 lim
= pnt
+ packet
->length
;
4915 safi
= packet
->safi
;
4919 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
4920 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
4921 PEER_CAP_ADDPATH_AF_TX_RCV
));
4923 for (; pnt
< lim
; pnt
+= psize
) {
4924 /* Clear prefix structure. */
4925 memset(&p
, 0, sizeof(struct prefix
));
4927 /* Deal with path-id if AddPath is supported. */
4928 if (addpath_encoded
) {
4929 /* When packet overflow occurs return immediately. */
4930 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4931 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
4933 addpath_id
= ntohl(*((uint32_t *)pnt
));
4934 pnt
+= BGP_ADDPATH_ID_LEN
;
4937 /* All EVPN NLRI types start with type and length. */
4939 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE
;
4944 /* When packet overflow occur return immediately. */
4945 if (pnt
+ psize
> lim
)
4946 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
4949 case BGP_EVPN_MAC_IP_ROUTE
:
4950 if (process_type2_route(peer
, afi
, safi
,
4951 withdraw
? NULL
: attr
, pnt
,
4952 psize
, addpath_id
)) {
4955 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4956 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4957 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE
;
4961 case BGP_EVPN_IMET_ROUTE
:
4962 if (process_type3_route(peer
, afi
, safi
,
4963 withdraw
? NULL
: attr
, pnt
,
4964 psize
, addpath_id
)) {
4967 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4968 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4969 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE
;
4973 case BGP_EVPN_ES_ROUTE
:
4974 if (process_type4_route(peer
, afi
, safi
,
4975 withdraw
? NULL
: attr
, pnt
,
4976 psize
, addpath_id
)) {
4979 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4980 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4981 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE
;
4985 case BGP_EVPN_IP_PREFIX_ROUTE
:
4986 if (process_type5_route(peer
, afi
, safi
,
4987 withdraw
? NULL
: attr
, pnt
,
4988 psize
, addpath_id
)) {
4991 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4992 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4993 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE
;
5002 /* Packet length consistency check. */
5004 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH
;
5006 return BGP_NLRI_PARSE_OK
;
5010 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
5011 * The mapping will be used during route processing.
5012 * bgp_def: default bgp instance
5013 * bgp_vrf: specific bgp vrf instance on which RT is configured
5015 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
5018 struct ecommunity_val
*eval
= NULL
;
5019 struct listnode
*node
= NULL
, *nnode
= NULL
;
5020 struct ecommunity
*ecom
= NULL
;
5022 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5023 for (i
= 0; i
< ecom
->size
; i
++) {
5024 eval
= (struct ecommunity_val
*)(ecom
->val
5026 * ECOMMUNITY_SIZE
));
5027 map_vrf_to_rt(bgp_vrf
, eval
);
5033 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
5035 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
5038 struct ecommunity_val
*eval
;
5039 struct listnode
*node
, *nnode
;
5040 struct ecommunity
*ecom
;
5042 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5043 for (i
= 0; i
< ecom
->size
; i
++) {
5044 struct vrf_irt_node
*irt
;
5045 struct ecommunity_val eval_tmp
;
5047 eval
= (struct ecommunity_val
*)(ecom
->val
5049 * ECOMMUNITY_SIZE
));
5050 /* If using "automatic" RT, we only care about the
5051 * local-admin sub-field.
5052 * This is to facilitate using VNI as the RT for EBGP
5055 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5056 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
5057 BGP_VRF_IMPORT_RT_CFGD
))
5058 mask_ecom_global_admin(&eval_tmp
, eval
);
5060 irt
= lookup_vrf_import_rt(&eval_tmp
);
5062 unmap_vrf_from_rt(bgp_vrf
, irt
);
5069 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
5070 * The mapping will be used during route processing.
5072 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5075 struct ecommunity_val
*eval
;
5076 struct listnode
*node
, *nnode
;
5077 struct ecommunity
*ecom
;
5079 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5080 for (i
= 0; i
< ecom
->size
; i
++) {
5081 eval
= (struct ecommunity_val
*)(ecom
->val
5083 * ECOMMUNITY_SIZE
));
5084 map_vni_to_rt(bgp
, vpn
, eval
);
5090 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
5092 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5095 struct ecommunity_val
*eval
;
5096 struct listnode
*node
, *nnode
;
5097 struct ecommunity
*ecom
;
5099 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5100 for (i
= 0; i
< ecom
->size
; i
++) {
5101 struct irt_node
*irt
;
5102 struct ecommunity_val eval_tmp
;
5104 eval
= (struct ecommunity_val
*)(ecom
->val
5106 * ECOMMUNITY_SIZE
));
5107 /* If using "automatic" RT, we only care about the
5108 * local-admin sub-field.
5109 * This is to facilitate using VNI as the RT for EBGP
5112 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5113 if (!is_import_rt_configured(vpn
))
5114 mask_ecom_global_admin(&eval_tmp
, eval
);
5116 irt
= lookup_import_rt(bgp
, &eval_tmp
);
5118 unmap_vni_from_rt(bgp
, vpn
, irt
);
5124 * Derive Import RT automatically for VNI and map VNI to RT.
5125 * The mapping will be used during route processing.
5127 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5129 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
5130 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
5133 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
5137 * Derive Export RT automatically for VNI.
5139 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5141 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
5142 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5146 * Derive RD automatically for VNI using passed information - it
5147 * is of the form RouterId:unique-id-for-vni.
5149 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5151 if (is_vrf_rd_configured(bgp
))
5154 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5158 * Derive RD automatically for VNI using passed information - it
5159 * is of the form RouterId:unique-id-for-vni.
5161 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5165 vpn
->prd
.family
= AF_UNSPEC
;
5166 vpn
->prd
.prefixlen
= 64;
5167 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
5168 (void)str2prefix_rd(buf
, &vpn
->prd
);
5169 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5175 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5177 struct list
*inst
= bm
->bgp
;
5178 struct listnode
*node
;
5179 struct bgp
*bgp_vrf
;
5181 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5182 if (bgp_vrf
->l3vni
== vni
)
5192 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5194 struct bgpevpn
*vpn
;
5197 memset(&tmp
, 0, sizeof(struct bgpevpn
));
5199 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5204 * Create a new vpn - invoked upon configuration or zebra notification.
5206 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5207 struct in_addr originator_ip
,
5208 vrf_id_t tenant_vrf_id
,
5209 struct in_addr mcast_grp
)
5211 struct bgpevpn
*vpn
;
5216 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5218 /* Set values - RD and RT set to defaults. */
5220 vpn
->originator_ip
= originator_ip
;
5221 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5222 vpn
->mcast_grp
= mcast_grp
;
5224 /* Initialize route-target import and export lists */
5225 vpn
->import_rtl
= list_new();
5226 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5227 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
5228 vpn
->export_rtl
= list_new();
5229 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5230 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
5231 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5232 derive_rd_rt_for_vni(bgp
, vpn
);
5234 /* Initialize EVPN route table. */
5235 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5238 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
5239 XFREE(MTYPE_BGP_EVPN
, vpn
);
5243 /* add to l2vni list on corresponding vrf */
5244 bgpevpn_link_to_l3vni(vpn
);
5246 QOBJ_REG(vpn
, bgpevpn
);
5251 * Free a given VPN - called in multiple scenarios such as zebra
5252 * notification, configuration being deleted, advertise-all-vni disabled etc.
5253 * This just frees appropriate memory, caller should have taken other
5256 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5258 bgpevpn_unlink_from_l3vni(vpn
);
5259 bgp_table_unlock(vpn
->route_table
);
5260 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5261 list_delete(&vpn
->import_rtl
);
5262 list_delete(&vpn
->export_rtl
);
5263 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5264 hash_release(bgp
->vnihash
, vpn
);
5266 XFREE(MTYPE_BGP_EVPN
, vpn
);
5272 struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
)
5277 memset(&tmp
, 0, sizeof(struct evpnes
));
5278 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
5279 es
= hash_lookup(bgp
->esihash
, &tmp
);
5284 * Create a new local es - invoked upon zebra notification.
5286 struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
,
5288 struct ipaddr
*originator_ip
)
5296 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct evpnes
));
5298 /* set the ESI and originator_ip */
5299 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
5300 memcpy(&es
->originator_ip
, originator_ip
, sizeof(struct ipaddr
));
5302 /* Initialise the VTEP list */
5303 es
->vtep_list
= list_new();
5304 es
->vtep_list
->cmp
= evpn_vtep_ip_cmp
;
5306 /* auto derive RD for this es */
5307 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
5308 es
->prd
.family
= AF_UNSPEC
;
5309 es
->prd
.prefixlen
= 64;
5310 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), es
->rd_id
);
5311 (void)str2prefix_rd(buf
, &es
->prd
);
5313 /* Initialize the ES route table */
5314 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5317 if (!hash_get(bgp
->esihash
, es
, hash_alloc_intern
)) {
5318 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5322 QOBJ_REG(es
, evpnes
);
5328 * This just frees appropriate memory, caller should have taken other
5331 void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
)
5333 list_delete(&es
->vtep_list
);
5334 bgp_table_unlock(es
->route_table
);
5335 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
5336 hash_release(bgp
->esihash
, es
);
5338 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5342 * Import evpn route from global table to VNI/VRF/ESI.
5344 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5345 struct prefix
*p
, struct bgp_path_info
*pi
)
5347 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5351 * Unimport evpn route from VNI/VRF/ESI.
5353 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5354 struct prefix
*p
, struct bgp_path_info
*pi
)
5356 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5359 /* filter routes which have martian next hops */
5360 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5364 struct bgp_node
*rd_rn
, *rn
;
5365 struct bgp_table
*table
;
5366 struct bgp_path_info
*pi
;
5371 /* Walk entire global routing table and evaluate routes which could be
5372 * imported into this VPN. Note that we cannot just look at the routes
5373 * for the VNI's RD -
5374 * remote routes applicable for this VNI could have any RD.
5376 /* EVPN routes are a 2-level table. */
5377 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
5378 rd_rn
= bgp_route_next(rd_rn
)) {
5379 table
= bgp_node_get_bgp_table_info(rd_rn
);
5383 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5385 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
5388 /* Consider "valid" remote routes applicable for
5390 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5391 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5394 if (bgp_nexthop_self(bgp
, pi
->attr
->nexthop
)) {
5396 char attr_str
[BUFSIZ
] = {0};
5397 char pbuf
[PREFIX_STRLEN
];
5399 bgp_dump_attr(pi
->attr
, attr_str
,
5402 if (bgp_debug_update(pi
->peer
, &rn
->p
,
5405 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
5412 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5415 bgp_rib_remove(rn
, pi
, pi
->peer
, afi
,
5426 * Handle del of a local MACIP.
5428 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5429 struct ipaddr
*ip
, int state
)
5431 struct bgpevpn
*vpn
;
5432 struct prefix_evpn p
;
5433 struct bgp_node
*rn
;
5435 /* Lookup VNI hash - should exist. */
5436 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5437 if (!vpn
|| !is_vni_live(vpn
)) {
5438 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5439 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5440 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5444 build_evpn_type2_prefix(&p
, mac
, ip
);
5445 if (state
== ZEBRA_NEIGH_ACTIVE
) {
5446 /* Remove EVPN type-2 route and schedule for processing. */
5447 delete_evpn_route(bgp
, vpn
, &p
);
5449 /* Re-instate the current remote best path if any */
5450 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
5452 evpn_zebra_reinstall_best_route(bgp
, vpn
, rn
);
5459 * Handle add of a local MACIP.
5461 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5462 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
)
5464 struct bgpevpn
*vpn
;
5465 struct prefix_evpn p
;
5467 /* Lookup VNI hash - should exist. */
5468 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5469 if (!vpn
|| !is_vni_live(vpn
)) {
5470 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5471 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5472 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5476 /* Create EVPN type-2 route and schedule for processing. */
5477 build_evpn_type2_prefix(&p
, mac
, ip
);
5478 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
)) {
5479 char buf
[ETHER_ADDR_STRLEN
];
5480 char buf2
[INET6_ADDRSTRLEN
];
5483 EC_BGP_EVPN_ROUTE_CREATE
,
5484 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5485 bgp
->vrf_id
, vpn
->vni
,
5486 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5489 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5490 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5497 static void link_l2vni_hash_to_l3vni(struct hash_bucket
*bucket
,
5498 struct bgp
*bgp_vrf
)
5500 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
5501 struct bgp
*bgp_evpn
= NULL
;
5503 bgp_evpn
= bgp_get_evpn();
5506 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5507 bgpevpn_link_to_l3vni(vpn
);
5510 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
, struct ethaddr
*rmac
,
5511 struct in_addr originator_ip
, int filter
,
5512 ifindex_t svi_ifindex
)
5514 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5515 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5516 struct listnode
*node
= NULL
;
5517 struct bgpevpn
*vpn
= NULL
;
5520 /* get the EVPN instance - required to get the AS number for VRF
5523 bgp_evpn
= bgp_get_evpn();
5527 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
5533 /* if the BGP vrf instance doesn't exist - create one */
5534 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5539 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5540 vrf_id
== VRF_DEFAULT
? BGP_INSTANCE_TYPE_DEFAULT
5541 : BGP_INSTANCE_TYPE_VRF
);
5543 case BGP_ERR_AS_MISMATCH
:
5544 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5545 "BGP is already running; AS is %u\n", as
);
5547 case BGP_ERR_INSTANCE_MISMATCH
:
5548 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5549 "BGP instance name and AS number mismatch\n");
5553 /* mark as auto created */
5554 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5557 /* associate the vrf with l3vni and related parameters */
5558 bgp_vrf
->l3vni
= l3vni
;
5559 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
5560 bgp_vrf
->originator_ip
= originator_ip
;
5561 bgp_vrf
->l3vni_svi_ifindex
= svi_ifindex
;
5563 /* set the right filter - are we using l3vni only for prefix routes? */
5565 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5567 /* Map auto derive or configured RTs */
5568 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5569 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5571 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
5573 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5574 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5576 /* auto derive RD */
5577 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5579 /* link all corresponding l2vnis */
5580 hash_iterate(bgp_evpn
->vnihash
,
5581 (void (*)(struct hash_bucket
*,
5582 void *))link_l2vni_hash_to_l3vni
,
5585 /* Only update all corresponding type-2 routes if we are advertising two
5586 * labels along with type-2 routes
5589 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5590 update_routes_for_vni(bgp_evpn
, vpn
);
5592 /* advertise type-5 routes if needed */
5593 update_advertise_vrf_routes(bgp_vrf
);
5595 /* install all remote routes belonging to this l3vni into correspondng
5597 install_routes_for_vrf(bgp_vrf
);
5602 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5604 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5605 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5606 struct listnode
*node
= NULL
;
5607 struct listnode
*next
= NULL
;
5608 struct bgpevpn
*vpn
= NULL
;
5610 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5614 "Cannot process L3VNI %u Del - Could not find BGP instance",
5619 bgp_evpn
= bgp_get_evpn();
5623 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
5628 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5629 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5630 * routes. This will uninstalling the routes from zebra and decremnt the
5633 uninstall_routes_for_vrf(bgp_vrf
);
5635 /* delete/withdraw all type-5 routes */
5636 delete_withdraw_vrf_routes(bgp_vrf
);
5638 /* remove the l3vni from vrf instance */
5641 /* remove the Rmac from the BGP vrf */
5642 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5644 /* remove default import RT or Unmap non-default import RT */
5645 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5646 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5647 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5648 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5651 /* remove default export RT */
5652 if (!list_isempty(bgp_vrf
->vrf_export_rtl
) &&
5653 !CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
5654 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5657 /* update all corresponding local mac-ip routes */
5658 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5659 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5660 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5661 update_routes_for_vni(bgp_evpn
, vpn
);
5665 /* If any L2VNIs point to this instance, unlink them. */
5666 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5667 bgpevpn_unlink_from_l3vni(vpn
);
5669 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5671 /* Delete the instance if it was autocreated */
5672 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5673 bgp_delete(bgp_vrf
);
5679 * Handle del of a local VNI.
5681 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5683 struct bgpevpn
*vpn
;
5685 /* Locate VNI hash */
5686 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5688 if (bgp_debug_zebra(NULL
))
5690 EC_BGP_EVPN_VPN_VNI
,
5691 "%u: VNI hash entry for VNI %u not found at DEL",
5696 /* Remove all local EVPN routes and schedule for processing (to
5697 * withdraw from peers).
5699 delete_routes_for_vni(bgp
, vpn
);
5702 * tunnel is no longer active, del tunnel ip address from tip_hash
5704 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5706 /* Clear "live" flag and see if hash needs to be freed. */
5707 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5708 if (!is_vni_configured(vpn
))
5709 bgp_evpn_free(bgp
, vpn
);
5715 * Handle add (or update) of a local VNI. The VNI changes we care
5716 * about are for the local-tunnel-ip and the (tenant) VRF.
5718 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5719 struct in_addr originator_ip
,
5720 vrf_id_t tenant_vrf_id
,
5721 struct in_addr mcast_grp
)
5724 struct bgpevpn
*vpn
;
5725 struct prefix_evpn p
;
5727 /* Lookup VNI. If present and no change, exit. */
5728 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5731 if (is_vni_live(vpn
)
5732 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5733 && IPV4_ADDR_SAME(&vpn
->mcast_grp
, &mcast_grp
)
5734 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5735 /* Probably some other param has changed that we don't
5739 bgp_evpn_mcast_grp_change(bgp
, vpn
, mcast_grp
);
5741 /* Update tenant_vrf_id if it has changed. */
5742 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5743 bgpevpn_unlink_from_l3vni(vpn
);
5744 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5745 bgpevpn_link_to_l3vni(vpn
);
5748 /* If tunnel endpoint IP has changed, update (and delete prior
5749 * type-3 route, if needed.)
5751 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5752 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5754 /* Update all routes with new endpoint IP and/or export RT
5757 if (is_vni_live(vpn
))
5758 update_routes_for_vni(bgp
, vpn
);
5761 /* Create or update as appropriate. */
5763 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
,
5768 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5774 /* if the VNI is live already, there is nothing more to do */
5775 if (is_vni_live(vpn
))
5778 /* Mark as "live" */
5779 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5781 /* tunnel is now active, add tunnel-ip to db */
5782 bgp_tip_add(bgp
, &originator_ip
);
5784 /* filter routes as nexthop database has changed */
5785 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5788 * Create EVPN type-3 route and schedule for processing.
5790 * RT-3 only if doing head-end replication
5792 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
5793 == VXLAN_FLOOD_HEAD_END_REPL
) {
5794 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5795 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0)) {
5796 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5797 "%u: Type3 route creation failure for VNI %u",
5803 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5807 install_routes_for_vni(bgp
, vpn
);
5809 /* If we are advertising gateway mac-ip
5810 It needs to be conveyed again to zebra */
5811 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5813 /* advertise svi mac-ip knob to zebra */
5814 bgp_zebra_advertise_svi_macip(bgp
, vpn
->advertise_svi_macip
, vpn
->vni
);
5820 * bgp_evpn_local_es_del
5822 int bgp_evpn_local_es_del(struct bgp
*bgp
,
5824 struct ipaddr
*originator_ip
)
5826 char buf
[ESI_STR_LEN
];
5827 struct evpnes
*es
= NULL
;
5829 if (!bgp
->esihash
) {
5830 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5835 /* Lookup ESI hash - should exist. */
5836 es
= bgp_evpn_lookup_es(bgp
, esi
);
5838 flog_warn(EC_BGP_EVPN_ESI
,
5839 "%u: ESI hash entry for ESI %s at Local ES DEL",
5840 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5844 /* Delete all local EVPN ES routes from ESI table
5845 * and schedule for processing (to withdraw from peers))
5847 delete_routes_for_es(bgp
, es
);
5849 /* free the hash entry */
5850 bgp_evpn_es_free(bgp
, es
);
5856 * bgp_evpn_local_es_add
5858 int bgp_evpn_local_es_add(struct bgp
*bgp
,
5860 struct ipaddr
*originator_ip
)
5862 char buf
[ESI_STR_LEN
];
5863 struct evpnes
*es
= NULL
;
5864 struct prefix_evpn p
;
5866 if (!bgp
->esihash
) {
5867 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5872 /* create the new es */
5873 es
= bgp_evpn_lookup_es(bgp
, esi
);
5875 es
= bgp_evpn_es_new(bgp
, esi
, originator_ip
);
5879 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
5880 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5884 UNSET_FLAG(es
->flags
, EVPNES_REMOTE
);
5885 SET_FLAG(es
->flags
, EVPNES_LOCAL
);
5887 build_evpn_type4_prefix(&p
, esi
, originator_ip
->ipaddr_v4
);
5888 if (update_evpn_type4_route(bgp
, es
, &p
)) {
5889 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5890 "%u: Type4 route creation failure for ESI %s",
5891 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5895 /* import all remote ES routes in th ES table */
5896 install_routes_for_es(bgp
, es
);
5902 * Handle change in setting for BUM handling. The supported values
5903 * are head-end replication and dropping all BUM packets. Any change
5904 * should be registered with zebra. Also, if doing head-end replication,
5905 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5906 * to be dropped, the RT-3s must be withdrawn.
5908 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
5910 zlog_info("L2VPN EVPN BUM handling is %s",
5911 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
5912 "Flooding" : "Flooding Disabled");
5914 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
5915 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
5916 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
5917 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
5918 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
5922 * Cleanup EVPN information on disable - Need to delete and withdraw
5923 * EVPN routes from peers.
5925 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5927 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_bucket
*,
5928 void *))cleanup_vni_on_disable
,
5933 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5934 * BGP instance (default) is being freed.
5936 void bgp_evpn_cleanup(struct bgp
*bgp
)
5938 hash_iterate(bgp
->vnihash
,
5939 (void (*)(struct hash_bucket
*, void *))free_vni_entry
,
5942 hash_free(bgp
->import_rt_hash
);
5943 bgp
->import_rt_hash
= NULL
;
5945 hash_free(bgp
->vrf_import_rt_hash
);
5946 bgp
->vrf_import_rt_hash
= NULL
;
5948 hash_free(bgp
->vnihash
);
5949 bgp
->vnihash
= NULL
;
5951 hash_free(bgp
->esihash
);
5952 bgp
->esihash
= NULL
;
5954 list_delete(&bgp
->vrf_import_rtl
);
5955 list_delete(&bgp
->vrf_export_rtl
);
5956 list_delete(&bgp
->l2vnis
);
5960 * Initialization for EVPN
5963 * hash for RT to VNI
5965 void bgp_evpn_init(struct bgp
*bgp
)
5968 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
5970 hash_create(esi_hash_keymake
, esi_cmp
,
5971 "BGP EVPN Local ESI Hash");
5972 bgp
->import_rt_hash
=
5973 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
5974 "BGP Import RT Hash");
5975 bgp
->vrf_import_rt_hash
=
5976 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
5977 "BGP VRF Import RT Hash");
5978 bgp
->vrf_import_rtl
= list_new();
5979 bgp
->vrf_import_rtl
->cmp
=
5980 (int (*)(void *, void *))evpn_route_target_cmp
;
5981 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
5982 bgp
->vrf_export_rtl
= list_new();
5983 bgp
->vrf_export_rtl
->cmp
=
5984 (int (*)(void *, void *))evpn_route_target_cmp
;
5985 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
5986 bgp
->l2vnis
= list_new();
5987 bgp
->l2vnis
->cmp
= vni_list_cmp
;
5988 /* By default Duplicate Address Dection is enabled.
5989 * Max-moves (N) 5, detection time (M) 180
5990 * default action is warning-only
5991 * freeze action permanently freezes address,
5992 * and freeze time (auto-recovery) is disabled.
5994 if (bgp
->evpn_info
) {
5995 bgp
->evpn_info
->dup_addr_detect
= true;
5996 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
5997 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
5998 bgp
->evpn_info
->dad_freeze
= false;
5999 bgp
->evpn_info
->dad_freeze_time
= 0;
6000 /* Initialize zebra vxlan */
6001 bgp_zebra_dup_addr_detection(bgp
);
6004 /* Default BUM handling is to do head-end replication. */
6005 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
6008 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
6010 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
6014 * Get the prefixlen of the ip prefix carried within the type5 evpn route.
6016 int bgp_evpn_get_type5_prefixlen(struct prefix
*pfx
)
6018 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6020 if (!pfx
|| pfx
->family
!= AF_EVPN
)
6023 if (evp
->prefix
.route_type
!= BGP_EVPN_IP_PREFIX_ROUTE
)
6026 return evp
->prefix
.prefix_addr
.ip_prefix_length
;