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
;
510 if (bgp
->advertise_autort_rfc8365
)
511 vni
|= EVPN_AUTORT_VXLAN
;
512 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
514 ecomadd
= ecommunity_new();
515 ecommunity_add_val(ecomadd
, &eval
);
516 listnode_add_sort(rtl
, ecomadd
);
520 * Derive RD and RT for a VNI automatically. Invoked at the time of
523 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
525 bgp_evpn_derive_auto_rd(bgp
, vpn
);
526 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
527 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
531 * Convert nexthop (remote VTEP IP) into an IPv6 address.
533 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
535 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
537 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
538 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
542 * Add (update) or delete MACIP from zebra.
544 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
545 struct prefix_evpn
*p
,
546 struct in_addr remote_vtep_ip
, int add
,
547 uint8_t flags
, uint32_t seq
)
551 char buf1
[ETHER_ADDR_STRLEN
];
552 char buf2
[INET6_ADDRSTRLEN
];
553 char buf3
[INET6_ADDRSTRLEN
];
556 if (!zclient
|| zclient
->sock
< 0)
559 /* Don't try to register if Zebra doesn't know of this instance. */
560 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
561 if (BGP_DEBUG(zebra
, ZEBRA
))
562 zlog_debug("%s: No zebra instance to talk to, not installing remote macip",
563 __PRETTY_FUNCTION__
);
569 zclient_create_header(
570 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
572 stream_putl(s
, vpn
->vni
);
573 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
574 /* IP address length and IP address, if any. */
575 if (is_evpn_prefix_ipaddr_none(p
))
578 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
580 stream_putl(s
, ipa_len
);
581 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
583 stream_put_in_addr(s
, &remote_vtep_ip
);
585 /* TX flags - MAC sticky status and/or gateway mac */
586 /* Also TX the sequence number of the best route. */
588 stream_putc(s
, flags
);
592 stream_putw_at(s
, 0, stream_get_endp(s
));
594 if (bgp_debug_zebra(NULL
))
596 "Tx %s MACIP, VNI %u MAC %s IP %s flags 0x%x seq %u remote VTEP %s",
597 add
? "ADD" : "DEL", vpn
->vni
,
598 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
600 ipaddr2str(&p
->prefix
.macip_addr
.ip
,
601 buf3
, sizeof(buf3
)), flags
, seq
,
602 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
605 return zclient_send_message(zclient
);
609 * Add (update) or delete remote VTEP from zebra.
611 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
612 struct prefix_evpn
*p
,
613 int flood_control
, int add
)
618 if (!zclient
|| zclient
->sock
< 0)
621 /* Don't try to register if Zebra doesn't know of this instance. */
622 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
623 if (BGP_DEBUG(zebra
, ZEBRA
))
624 zlog_debug("%s: No zebra instance to talk to, not installing remote vtep",
625 __PRETTY_FUNCTION__
);
632 zclient_create_header(
633 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
635 stream_putl(s
, vpn
->vni
);
636 if (is_evpn_prefix_ipaddr_v4(p
))
637 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
638 else if (is_evpn_prefix_ipaddr_v6(p
)) {
641 "Bad remote IP when trying to %s remote VTEP for VNI %u",
642 add
? "ADD" : "DEL", vpn
->vni
);
645 stream_putl(s
, flood_control
);
647 stream_putw_at(s
, 0, stream_get_endp(s
));
649 if (bgp_debug_zebra(NULL
))
650 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
651 add
? "ADD" : "DEL", vpn
->vni
,
652 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
654 return zclient_send_message(zclient
);
658 * Build extended community for EVPN ES (type-4) route
660 static void build_evpn_type4_route_extcomm(struct evpnes
*es
,
663 struct ecommunity ecom_encap
;
664 struct ecommunity ecom_es_rt
;
665 struct ecommunity_val eval
;
666 struct ecommunity_val eval_es_rt
;
667 bgp_encap_types tnl_type
;
671 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
672 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
673 encode_encap_extcomm(tnl_type
, &eval
);
675 ecom_encap
.val
= (uint8_t *)eval
.val
;
676 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
679 memset(&mac
, 0, sizeof(struct ethaddr
));
680 memset(&ecom_es_rt
, 0, sizeof(ecom_es_rt
));
681 es_get_system_mac(&es
->esi
, &mac
);
682 encode_es_rt_extcomm(&eval_es_rt
, &mac
);
684 ecom_es_rt
.val
= (uint8_t *)eval_es_rt
.val
;
686 ecommunity_merge(attr
->ecommunity
, &ecom_es_rt
);
688 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
692 * Build extended communities for EVPN prefix route.
694 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
697 struct ecommunity ecom_encap
;
698 struct ecommunity ecom_rmac
;
699 struct ecommunity_val eval
;
700 struct ecommunity_val eval_rmac
;
701 bgp_encap_types tnl_type
;
702 struct listnode
*node
, *nnode
;
703 struct ecommunity
*ecom
;
704 struct list
*vrf_export_rtl
= NULL
;
707 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
708 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
709 encode_encap_extcomm(tnl_type
, &eval
);
711 ecom_encap
.val
= (uint8_t *)eval
.val
;
714 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
716 /* Add the export RTs for L3VNI/VRF */
717 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
718 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
720 ecommunity_merge(attr
->ecommunity
, ecom
);
722 /* add the router mac extended community */
723 if (!is_zero_mac(&attr
->rmac
)) {
724 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
725 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
727 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
729 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
732 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
736 * Build extended communities for EVPN route.
737 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
738 * and ENCAP extended communities are applicable for all routes.
739 * The default gateway extended community and MAC mobility (sticky) extended
740 * community are added as needed based on passed settings - only for type-2
741 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
742 * added, if present, based on passed settings - only for non-link-local
745 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
748 struct ecommunity ecom_encap
;
749 struct ecommunity ecom_sticky
;
750 struct ecommunity ecom_default_gw
;
751 struct ecommunity ecom_rmac
;
752 struct ecommunity ecom_na
;
753 struct ecommunity_val eval
;
754 struct ecommunity_val eval_sticky
;
755 struct ecommunity_val eval_default_gw
;
756 struct ecommunity_val eval_rmac
;
757 struct ecommunity_val eval_na
;
759 bgp_encap_types tnl_type
;
760 struct listnode
*node
, *nnode
;
761 struct ecommunity
*ecom
;
763 struct list
*vrf_export_rtl
= NULL
;
766 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
767 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
768 encode_encap_extcomm(tnl_type
, &eval
);
770 ecom_encap
.val
= (uint8_t *)eval
.val
;
773 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
775 /* Add the export RTs for L2VNI */
776 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
777 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
779 /* Add the export RTs for L3VNI if told to - caller determines
780 * when this should be done.
783 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
784 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
785 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
787 attr
->ecommunity
= ecommunity_merge(
788 attr
->ecommunity
, ecom
);
792 /* Add MAC mobility (sticky) if needed. */
795 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
796 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
797 ecom_sticky
.size
= 1;
798 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
800 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
803 /* Add RMAC, if told to. */
805 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
806 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
808 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
810 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
813 /* Add default gateway, if needed. */
814 if (attr
->default_gw
) {
815 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
816 encode_default_gw_extcomm(&eval_default_gw
);
817 ecom_default_gw
.size
= 1;
818 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
820 ecommunity_merge(attr
->ecommunity
, &ecom_default_gw
);
823 if (attr
->router_flag
) {
824 memset(&ecom_na
, 0, sizeof(ecom_na
));
825 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
);
827 ecom_na
.val
= (uint8_t *)eval_na
.val
;
828 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
832 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
836 * Add MAC mobility extended community to attribute.
838 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
840 struct ecommunity ecom_tmp
;
841 struct ecommunity_val eval
;
842 uint8_t *ecom_val_ptr
;
849 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
851 /* Find current MM ecommunity */
854 if (attr
->ecommunity
) {
855 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
856 pnt
= attr
->ecommunity
->val
+ (i
* 8);
860 if (type
== ECOMMUNITY_ENCODE_EVPN
862 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
863 ecom_val_ptr
= (uint8_t *)(attr
->ecommunity
->val
870 /* Update the existing MM ecommunity */
872 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
874 /* Add MM to existing */
876 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
878 ecom_tmp
.val
= (uint8_t *)eval
.val
;
880 if (attr
->ecommunity
)
882 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
884 attr
->ecommunity
= ecommunity_dup(&ecom_tmp
);
888 /* Install EVPN route into zebra. */
889 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
890 struct prefix_evpn
*p
, struct bgp_path_info
*pi
)
896 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
898 if (pi
->attr
->sticky
)
899 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
900 if (pi
->attr
->default_gw
)
901 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
902 if (is_evpn_prefix_ipaddr_v6(p
) &&
903 pi
->attr
->router_flag
)
904 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
905 ret
= bgp_zebra_send_remote_macip(
906 bgp
, vpn
, p
, pi
->attr
->nexthop
, 1, flags
,
907 mac_mobility_seqnum(pi
->attr
));
909 switch (pi
->attr
->pmsi_tnl_type
) {
910 case PMSI_TNLTYPE_INGR_REPL
:
911 flood_control
= VXLAN_FLOOD_HEAD_END_REPL
;
914 case PMSI_TNLTYPE_PIM_SM
:
915 flood_control
= VXLAN_FLOOD_PIM_SM
;
919 flood_control
= VXLAN_FLOOD_DISABLED
;
922 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, flood_control
, 1);
928 /* Uninstall EVPN route from zebra. */
929 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
930 struct prefix_evpn
*p
,
931 struct in_addr remote_vtep_ip
)
935 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
936 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
939 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
,
940 VXLAN_FLOOD_DISABLED
, 0);
946 * Due to MAC mobility, the prior "local" best route has been supplanted
947 * by a "remote" best route. The prior route has to be deleted and withdrawn
950 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
952 struct bgp_path_info
*old_local
)
954 struct bgp_node
*global_rn
;
955 struct bgp_path_info
*pi
;
956 afi_t afi
= AFI_L2VPN
;
957 safi_t safi
= SAFI_EVPN
;
959 /* Locate route node in the global EVPN routing table. Note that
960 * this table is a 2-level tree (RD-level + Prefix-level) similar to
963 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
964 (struct prefix
*)&rn
->p
, &vpn
->prd
);
966 /* Delete route entry in the global EVPN table. */
967 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
969 /* Schedule for processing - withdraws to peers happen from
973 bgp_process(bgp
, global_rn
, afi
, safi
);
974 bgp_unlock_node(global_rn
);
977 /* Delete route entry in the VNI route table, caller to remove. */
978 bgp_path_info_delete(rn
, old_local
);
981 static struct in_addr
*es_vtep_new(struct in_addr vtep
)
985 ip
= XCALLOC(MTYPE_BGP_EVPN_ES_VTEP
, sizeof(struct in_addr
));
987 ip
->s_addr
= vtep
.s_addr
;
991 static void es_vtep_free(struct in_addr
*ip
)
993 XFREE(MTYPE_BGP_EVPN_ES_VTEP
, ip
);
996 /* check if VTEP is already part of the list */
997 static int is_vtep_present_in_list(struct list
*list
,
1000 struct listnode
*node
= NULL
;
1001 struct in_addr
*tmp
;
1003 for (ALL_LIST_ELEMENTS_RO(list
, node
, tmp
)) {
1004 if (tmp
->s_addr
== vtep
.s_addr
)
1011 * Best path for ES route was changed,
1012 * update the list of VTEPs for this ES
1014 static int evpn_es_install_vtep(struct bgp
*bgp
,
1016 struct prefix_evpn
*p
,
1017 struct in_addr rvtep
)
1019 struct in_addr
*vtep_ip
;
1021 if (is_vtep_present_in_list(es
->vtep_list
, rvtep
))
1025 vtep_ip
= es_vtep_new(rvtep
);
1027 listnode_add_sort(es
->vtep_list
, vtep_ip
);
1032 * Best path for ES route was changed,
1033 * update the list of VTEPs for this ES
1035 static int evpn_es_uninstall_vtep(struct bgp
*bgp
,
1037 struct prefix_evpn
*p
,
1038 struct in_addr rvtep
)
1040 struct listnode
*node
, *nnode
, *node_to_del
= NULL
;
1041 struct in_addr
*tmp
;
1043 for (ALL_LIST_ELEMENTS(es
->vtep_list
, node
, nnode
, tmp
)) {
1044 if (tmp
->s_addr
== rvtep
.s_addr
) {
1051 list_delete_node(es
->vtep_list
, node_to_del
);
1057 * Calculate the best path for a ES(type-4) route.
1059 static int evpn_es_route_select_install(struct bgp
*bgp
,
1061 struct bgp_node
*rn
)
1064 afi_t afi
= AFI_L2VPN
;
1065 safi_t safi
= SAFI_EVPN
;
1066 struct bgp_path_info
*old_select
; /* old best */
1067 struct bgp_path_info
*new_select
; /* new best */
1068 struct bgp_path_info_pair old_and_new
;
1070 /* Compute the best path. */
1071 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
],
1072 &old_and_new
, afi
, safi
);
1073 old_select
= old_and_new
.old
;
1074 new_select
= old_and_new
.new;
1077 * If the best path hasn't changed - see if something needs to be
1080 if (old_select
&& old_select
== new_select
1081 && old_select
->type
== ZEBRA_ROUTE_BGP
1082 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1083 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1084 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1085 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1086 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1087 ret
= evpn_es_install_vtep(bgp
, es
,
1088 (struct prefix_evpn
*)&rn
->p
,
1089 old_select
->attr
->nexthop
);
1091 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1092 bgp_zebra_clear_route_change_flags(rn
);
1096 /* If the user did a "clear" this flag will be set */
1097 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1100 * bestpath has changed; update relevant fields and install or uninstall
1101 * into the zebra RIB.
1103 if (old_select
|| new_select
)
1104 bgp_bump_version(rn
);
1107 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1109 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1110 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1111 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1114 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1115 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1116 ret
= evpn_es_install_vtep(bgp
, es
,
1117 (struct prefix_evpn
*)&rn
->p
,
1118 new_select
->attr
->nexthop
);
1120 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1121 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1122 ret
= evpn_es_uninstall_vtep(
1123 bgp
, es
, (struct prefix_evpn
*)&rn
->p
,
1124 old_select
->attr
->nexthop
);
1127 /* Clear any route change flags. */
1128 bgp_zebra_clear_route_change_flags(rn
);
1130 /* Reap old select bgp_path_info, if it has been removed */
1131 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1132 bgp_path_info_reap(rn
, old_select
);
1138 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1141 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1142 struct bgp_node
*rn
)
1144 struct bgp_path_info
*old_select
, *new_select
;
1145 struct bgp_path_info_pair old_and_new
;
1146 afi_t afi
= AFI_L2VPN
;
1147 safi_t safi
= SAFI_EVPN
;
1150 /* Compute the best path. */
1151 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1153 old_select
= old_and_new
.old
;
1154 new_select
= old_and_new
.new;
1156 /* If the best path hasn't changed - see if there is still something to
1160 if (old_select
&& old_select
== new_select
1161 && old_select
->type
== ZEBRA_ROUTE_BGP
1162 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1163 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1164 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1165 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1166 if (bgp_zebra_has_route_changed(rn
, old_select
))
1167 ret
= evpn_zebra_install(
1168 bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1170 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1171 bgp_zebra_clear_route_change_flags(rn
);
1175 /* If the user did a "clear" this flag will be set */
1176 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1178 /* bestpath has changed; update relevant fields and install or uninstall
1179 * into the zebra RIB.
1181 if (old_select
|| new_select
)
1182 bgp_bump_version(rn
);
1185 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1187 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1188 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1189 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1192 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1193 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1194 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1197 /* If an old best existed and it was a "local" route, the only
1199 * it would be supplanted is due to MAC mobility procedures. So,
1201 * need to do an implicit delete and withdraw that route from
1204 if (old_select
&& old_select
->peer
== bgp
->peer_self
1205 && old_select
->type
== ZEBRA_ROUTE_BGP
1206 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
1207 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
1209 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1210 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1211 ret
= evpn_zebra_uninstall(bgp
, vpn
,
1212 (struct prefix_evpn
*)&rn
->p
,
1213 old_select
->attr
->nexthop
);
1216 /* Clear any route change flags. */
1217 bgp_zebra_clear_route_change_flags(rn
);
1219 /* Reap old select bgp_path_info, if it has been removed */
1220 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1221 bgp_path_info_reap(rn
, old_select
);
1227 * Return true if the local ri for this rn is of type gateway mac
1229 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
1231 struct bgp_path_info
*tmp_pi
= NULL
;
1232 struct bgp_path_info
*local_pi
= NULL
;
1235 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1236 tmp_pi
= tmp_pi
->next
) {
1237 if (tmp_pi
->peer
== bgp
->peer_self
1238 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1239 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1246 return local_pi
->attr
->default_gw
;
1251 * Return true if the local ri for this rn has sticky set
1253 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
1255 struct bgp_path_info
*tmp_pi
;
1256 struct bgp_path_info
*local_pi
;
1259 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1260 tmp_pi
= tmp_pi
->next
) {
1261 if (tmp_pi
->peer
== bgp
->peer_self
1262 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1263 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1270 return local_pi
->attr
->sticky
;
1274 * create or update EVPN type4 route entry.
1275 * This could be in the ES table or the global table.
1276 * TODO: handle remote ES (type4) routes as well
1278 static int update_evpn_type4_route_entry(struct bgp
*bgp
, struct evpnes
*es
,
1279 afi_t afi
, safi_t safi
,
1280 struct bgp_node
*rn
, struct attr
*attr
,
1281 int add
, struct bgp_path_info
**ri
,
1284 char buf
[ESI_STR_LEN
];
1285 char buf1
[INET6_ADDRSTRLEN
];
1286 struct bgp_path_info
*tmp_pi
= NULL
;
1287 struct bgp_path_info
*local_pi
= NULL
; /* local route entry if any */
1288 struct bgp_path_info
*remote_pi
= NULL
; /* remote route entry if any */
1289 struct attr
*attr_new
= NULL
;
1290 struct prefix_evpn
*evp
= NULL
;
1294 evp
= (struct prefix_evpn
*)&rn
->p
;
1296 /* locate the local and remote entries if any */
1297 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1298 tmp_pi
= tmp_pi
->next
) {
1299 if (tmp_pi
->peer
== bgp
->peer_self
1300 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1301 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1303 if (tmp_pi
->type
== ZEBRA_ROUTE_BGP
1304 && tmp_pi
->sub_type
== BGP_ROUTE_IMPORTED
1305 && CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1309 /* we don't expect to see a remote_ri at this point.
1310 * An ES route has esi + vtep_ip as the key,
1311 * We shouldn't see the same route from any other vtep.
1316 "%u ERROR: local es route for ESI: %s Vtep %s also learnt from remote",
1318 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1319 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1323 if (!local_pi
&& !add
)
1326 /* create or update the entry */
1329 /* Add or update attribute to hash */
1330 attr_new
= bgp_attr_intern(attr
);
1332 /* Create new route with its attribute. */
1333 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1334 bgp
->peer_self
, attr_new
, rn
);
1335 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1337 /* add the newly created path to the route-node */
1338 bgp_path_info_add(rn
, tmp_pi
);
1341 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1342 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1345 /* The attribute has changed.
1346 * Add (or update) attribute to hash. */
1347 attr_new
= bgp_attr_intern(attr
);
1348 bgp_path_info_set_flag(rn
, tmp_pi
,
1349 BGP_PATH_ATTR_CHANGED
);
1351 /* Restore route, if needed. */
1352 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1353 bgp_path_info_restore(rn
, tmp_pi
);
1355 /* Unintern existing, set to new. */
1356 bgp_attr_unintern(&tmp_pi
->attr
);
1357 tmp_pi
->attr
= attr_new
;
1358 tmp_pi
->uptime
= bgp_clock();
1362 /* Return back the route entry. */
1367 /* update evpn es (type-4) route */
1368 static int update_evpn_type4_route(struct bgp
*bgp
,
1370 struct prefix_evpn
*p
)
1373 int route_changed
= 0;
1374 char buf
[ESI_STR_LEN
];
1375 char buf1
[INET6_ADDRSTRLEN
];
1376 afi_t afi
= AFI_L2VPN
;
1377 safi_t safi
= SAFI_EVPN
;
1379 struct attr
*attr_new
= NULL
;
1380 struct bgp_node
*rn
= NULL
;
1381 struct bgp_path_info
*pi
= NULL
;
1383 memset(&attr
, 0, sizeof(struct attr
));
1385 /* Build path-attribute for this route. */
1386 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1387 attr
.nexthop
= es
->originator_ip
.ipaddr_v4
;
1388 attr
.mp_nexthop_global_in
= es
->originator_ip
.ipaddr_v4
;
1389 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1391 /* Set up extended community. */
1392 build_evpn_type4_route_extcomm(es
, &attr
);
1394 /* First, create (or fetch) route node within the ESI. */
1395 /* NOTE: There is no RD here. */
1396 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
1398 /* Create or update route entry. */
1399 ret
= update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, &attr
, 1,
1400 &pi
, &route_changed
);
1402 flog_err(EC_BGP_ES_INVALID
,
1403 "%u ERROR: Failed to updated ES route ESI: %s VTEP %s",
1405 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1406 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1410 attr_new
= pi
->attr
;
1412 /* Perform route selection;
1413 * this is just to set the flags correctly
1414 * as local route in the ES always wins.
1416 evpn_es_route_select_install(bgp
, es
, rn
);
1417 bgp_unlock_node(rn
);
1419 /* If this is a new route or some attribute has changed, export the
1420 * route to the global table. The route will be advertised to peers
1421 * from there. Note that this table is a 2-level tree (RD-level +
1422 * Prefix-level) similar to L3VPN routes.
1424 if (route_changed
) {
1425 struct bgp_path_info
*global_pi
;
1427 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1428 (struct prefix
*)p
, &es
->prd
);
1429 update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, attr_new
,
1430 1, &global_pi
, &route_changed
);
1432 /* Schedule for processing and unlock node. */
1433 bgp_process(bgp
, rn
, afi
, safi
);
1434 bgp_unlock_node(rn
);
1437 /* Unintern temporary. */
1438 aspath_unintern(&attr
.aspath
);
1442 static int update_evpn_type5_route_entry(struct bgp
*bgp_evpn
,
1443 struct bgp
*bgp_vrf
, afi_t afi
,
1444 safi_t safi
, struct bgp_node
*rn
,
1445 struct attr
*attr
, int *route_changed
)
1447 struct attr
*attr_new
= NULL
;
1448 struct bgp_path_info
*pi
= NULL
;
1449 mpls_label_t label
= MPLS_INVALID_LABEL
;
1450 struct bgp_path_info
*local_pi
= NULL
;
1451 struct bgp_path_info
*tmp_pi
= NULL
;
1454 /* locate the local route entry if any */
1455 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1456 tmp_pi
= tmp_pi
->next
) {
1457 if (tmp_pi
->peer
== bgp_evpn
->peer_self
1458 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1459 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1464 * create a new route entry if one doesn't exist.
1465 * Otherwise see if route attr has changed
1469 /* route has changed as this is the first entry */
1472 /* Add (or update) attribute to hash. */
1473 attr_new
= bgp_attr_intern(attr
);
1475 /* create the route info from attribute */
1476 pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1477 bgp_evpn
->peer_self
, attr_new
, rn
);
1478 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
1480 /* Type-5 routes advertise the L3-VNI */
1481 bgp_path_info_extra_get(pi
);
1482 vni2label(bgp_vrf
->l3vni
, &label
);
1483 memcpy(&pi
->extra
->label
, &label
, sizeof(label
));
1484 pi
->extra
->num_labels
= 1;
1486 /* add the route entry to route node*/
1487 bgp_path_info_add(rn
, pi
);
1491 if (!attrhash_cmp(tmp_pi
->attr
, attr
)) {
1493 /* attribute changed */
1496 /* The attribute has changed. */
1497 /* Add (or update) attribute to hash. */
1498 attr_new
= bgp_attr_intern(attr
);
1499 bgp_path_info_set_flag(rn
, tmp_pi
,
1500 BGP_PATH_ATTR_CHANGED
);
1502 /* Restore route, if needed. */
1503 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1504 bgp_path_info_restore(rn
, tmp_pi
);
1506 /* Unintern existing, set to new. */
1507 bgp_attr_unintern(&tmp_pi
->attr
);
1508 tmp_pi
->attr
= attr_new
;
1509 tmp_pi
->uptime
= bgp_clock();
1515 /* update evpn type-5 route entry */
1516 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1517 struct attr
*src_attr
)
1519 afi_t afi
= AFI_L2VPN
;
1520 safi_t safi
= SAFI_EVPN
;
1522 struct bgp_node
*rn
= NULL
;
1523 struct bgp
*bgp_evpn
= NULL
;
1524 int route_changed
= 0;
1526 bgp_evpn
= bgp_get_evpn();
1530 /* Build path attribute for this route - use the source attr, if
1531 * present, else treat as locally originated.
1534 bgp_attr_dup(&attr
, src_attr
);
1536 memset(&attr
, 0, sizeof(struct attr
));
1537 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1539 /* Set nexthop to ourselves and fill in the Router MAC. */
1540 attr
.nexthop
= bgp_vrf
->originator_ip
;
1541 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1542 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1543 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
1545 /* Setup RT and encap extended community */
1546 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1548 /* get the route node in global table */
1549 rn
= bgp_afi_node_get(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1550 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1553 /* create or update the route entry within the route node */
1554 update_evpn_type5_route_entry(bgp_evpn
, bgp_vrf
, afi
, safi
, rn
, &attr
,
1557 /* schedule for processing and unlock node */
1558 if (route_changed
) {
1559 bgp_process(bgp_evpn
, rn
, afi
, safi
);
1560 bgp_unlock_node(rn
);
1563 /* uninten temporary */
1565 aspath_unintern(&attr
.aspath
);
1570 * Create or update EVPN route entry. This could be in the VNI route table
1571 * or the global route table.
1573 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1574 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1575 struct attr
*attr
, int add
,
1576 struct bgp_path_info
**pi
, uint8_t flags
,
1579 struct bgp_path_info
*tmp_pi
;
1580 struct bgp_path_info
*local_pi
;
1581 struct attr
*attr_new
;
1582 mpls_label_t label
[BGP_MAX_LABELS
];
1583 uint32_t num_labels
= 1;
1584 int route_change
= 1;
1586 struct prefix_evpn
*evp
;
1589 evp
= (struct prefix_evpn
*)&rn
->p
;
1590 memset(&label
, 0, sizeof(label
));
1592 /* See if this is an update of an existing route, or a new add. */
1594 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1595 tmp_pi
= tmp_pi
->next
) {
1596 if (tmp_pi
->peer
== bgp
->peer_self
1597 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1598 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1602 /* If route doesn't exist already, create a new one, if told to.
1603 * Otherwise act based on whether the attributes of the route have
1606 if (!local_pi
&& !add
)
1609 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1610 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1611 add_mac_mobility_to_attr(seq
, attr
);
1614 /* Add (or update) attribute to hash. */
1615 attr_new
= bgp_attr_intern(attr
);
1617 /* Extract MAC mobility sequence number, if any. */
1618 attr_new
->mm_seqnum
=
1619 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1620 attr_new
->sticky
= sticky
;
1622 /* Create new route with its attribute. */
1623 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1624 bgp
->peer_self
, attr_new
, rn
);
1625 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1626 bgp_path_info_extra_get(tmp_pi
);
1628 /* The VNI goes into the 'label' field of the route */
1629 vni2label(vpn
->vni
, &label
[0]);
1631 /* Type-2 routes may carry a second VNI - the L3-VNI.
1632 * Only attach second label if we are advertising two labels for
1635 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1636 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1639 l3vni
= bgpevpn_get_l3vni(vpn
);
1641 vni2label(l3vni
, &label
[1]);
1646 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1647 tmp_pi
->extra
->num_labels
= num_labels
;
1648 bgp_path_info_add(rn
, tmp_pi
);
1651 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1652 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1656 * The attributes have changed, type-2 routes needs to
1657 * be advertised with right labels.
1659 vni2label(vpn
->vni
, &label
[0]);
1660 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1661 && CHECK_FLAG(vpn
->flags
,
1662 VNI_FLAG_USE_TWO_LABELS
)) {
1665 l3vni
= bgpevpn_get_l3vni(vpn
);
1667 vni2label(l3vni
, &label
[1]);
1671 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1672 tmp_pi
->extra
->num_labels
= num_labels
;
1674 /* The attribute has changed. */
1675 /* Add (or update) attribute to hash. */
1676 attr_new
= bgp_attr_intern(attr
);
1677 bgp_path_info_set_flag(rn
, tmp_pi
,
1678 BGP_PATH_ATTR_CHANGED
);
1680 /* Extract MAC mobility sequence number, if any. */
1681 attr_new
->mm_seqnum
=
1682 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1683 attr_new
->sticky
= sticky
;
1685 /* Restore route, if needed. */
1686 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1687 bgp_path_info_restore(rn
, tmp_pi
);
1689 /* Unintern existing, set to new. */
1690 bgp_attr_unintern(&tmp_pi
->attr
);
1691 tmp_pi
->attr
= attr_new
;
1692 tmp_pi
->uptime
= bgp_clock();
1696 /* Return back the route entry. */
1698 return route_change
;
1701 static void evpn_zebra_reinstall_best_route(struct bgp
*bgp
,
1702 struct bgpevpn
*vpn
, struct bgp_node
*rn
)
1704 struct bgp_path_info
*tmp_ri
;
1705 struct bgp_path_info
*curr_select
= NULL
;
1707 for (tmp_ri
= bgp_node_get_bgp_path_info(rn
);
1708 tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1709 if (CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_SELECTED
)) {
1710 curr_select
= tmp_ri
;
1715 if (curr_select
&& curr_select
->type
== ZEBRA_ROUTE_BGP
1716 && curr_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1717 evpn_zebra_install(bgp
, vpn
,
1718 (struct prefix_evpn
*)&rn
->p
,
1723 * If the local route was not selected evict it and tell zebra to re-add
1724 * the best remote dest.
1726 * Typically a local path added by zebra is expected to be selected as
1727 * best. In which case when a remote path wins as best (later)
1728 * evpn_route_select_install itself evicts the older-local-best path.
1730 * However if bgp's add and zebra's add cross paths (race condition) it
1731 * is possible that the local path is no longer the "older" best path.
1732 * It is a path that was never designated as best and hence requires
1733 * additional handling to prevent bgp from injecting and holding on to a
1734 * non-best local path.
1736 static void evpn_cleanup_local_non_best_route(struct bgp
*bgp
,
1737 struct bgpevpn
*vpn
,
1738 struct bgp_node
*rn
,
1739 struct bgp_path_info
*local_pi
)
1741 char buf
[PREFIX_STRLEN
];
1743 /* local path was not picked as the winner; kick it out */
1744 if (bgp_debug_zebra(NULL
)) {
1745 zlog_debug("evicting local evpn prefix %s as remote won",
1746 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
1748 evpn_delete_old_local_route(bgp
, vpn
, rn
, local_pi
);
1749 bgp_path_info_reap(rn
, local_pi
);
1751 /* tell zebra to re-add the best remote path */
1752 evpn_zebra_reinstall_best_route(bgp
, vpn
, rn
);
1756 * Create or update EVPN route (of type based on prefix) for specified VNI
1757 * and schedule for processing.
1759 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1760 struct prefix_evpn
*p
, uint8_t flags
,
1763 struct bgp_node
*rn
;
1765 struct attr
*attr_new
;
1766 int add_l3_ecomm
= 0;
1767 struct bgp_path_info
*pi
;
1768 afi_t afi
= AFI_L2VPN
;
1769 safi_t safi
= SAFI_EVPN
;
1772 memset(&attr
, 0, sizeof(struct attr
));
1774 /* Build path-attribute for this route. */
1775 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1776 attr
.nexthop
= vpn
->originator_ip
;
1777 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1778 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1779 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1780 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1781 attr
.router_flag
= CHECK_FLAG(flags
,
1782 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1784 /* PMSI is only needed for type-3 routes */
1785 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
1786 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1787 attr
.pmsi_tnl_type
= PMSI_TNLTYPE_INGR_REPL
;
1790 /* router mac is only needed for type-2 routes here. */
1791 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1792 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1793 vni2label(vpn
->vni
, &(attr
.label
));
1795 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1796 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1799 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1800 (is_evpn_prefix_ipaddr_v4(p
) ||
1801 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1802 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1803 bgpevpn_get_l3vni(vpn
))
1806 /* Set up extended community. */
1807 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1809 /* First, create (or fetch) route node within the VNI. */
1810 /* NOTE: There is no RD here. */
1811 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1813 /* Create or update route entry. */
1814 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1815 1, &pi
, flags
, seq
);
1817 attr_new
= pi
->attr
;
1819 /* lock ri to prevent freeing in evpn_route_select_install */
1820 bgp_path_info_lock(pi
);
1821 /* Perform route selection; this is just to set the flags correctly
1822 * as local route in the VNI always wins.
1824 evpn_route_select_install(bgp
, vpn
, rn
);
1826 * If the new local route was not selected evict it and tell zebra
1827 * to re-add the best remote dest. BGP doesn't retain non-best local
1830 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1832 evpn_cleanup_local_non_best_route(bgp
, vpn
, rn
, pi
);
1834 bgp_path_info_unlock(pi
);
1836 bgp_unlock_node(rn
);
1838 /* If this is a new route or some attribute has changed, export the
1839 * route to the global table. The route will be advertised to peers
1840 * from there. Note that this table is a 2-level tree (RD-level +
1841 * Prefix-level) similar to L3VPN routes.
1844 struct bgp_path_info
*global_pi
;
1846 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1847 (struct prefix
*)p
, &vpn
->prd
);
1848 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1,
1849 &global_pi
, flags
, seq
);
1851 /* Schedule for processing and unlock node. */
1852 bgp_process(bgp
, rn
, afi
, safi
);
1853 bgp_unlock_node(rn
);
1856 /* Unintern temporary. */
1857 aspath_unintern(&attr
.aspath
);
1863 * Delete EVPN route entry.
1864 * The entry can be in ESI/VNI table or the global table.
1866 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1867 struct bgp_node
*rn
,
1868 struct bgp_path_info
**pi
)
1870 struct bgp_path_info
*tmp_pi
;
1874 /* Now, find matching route. */
1875 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1876 tmp_pi
= tmp_pi
->next
)
1877 if (tmp_pi
->peer
== bgp
->peer_self
1878 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1879 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1884 /* Mark route for delete. */
1886 bgp_path_info_delete(rn
, tmp_pi
);
1891 /* Delete EVPN ES (type-4) route */
1892 static int delete_evpn_type4_route(struct bgp
*bgp
,
1894 struct prefix_evpn
*p
)
1896 afi_t afi
= AFI_L2VPN
;
1897 safi_t safi
= SAFI_EVPN
;
1898 struct bgp_path_info
*pi
;
1899 struct bgp_node
*rn
= NULL
; /* rn in esi table */
1900 struct bgp_node
*global_rn
= NULL
; /* rn in global table */
1902 /* First, locate the route node within the ESI.
1903 * If it doesn't exist, ther is nothing to do.
1904 * Note: there is no RD here.
1906 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
1910 /* Next, locate route node in the global EVPN routing table.
1911 * Note that this table is a 2-level tree (RD-level + Prefix-level)
1913 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1914 (struct prefix
*)p
, &es
->prd
);
1917 /* Delete route entry in the global EVPN table. */
1918 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1920 /* Schedule for processing - withdraws to peers happen from
1924 bgp_process(bgp
, global_rn
, afi
, safi
);
1925 bgp_unlock_node(global_rn
);
1929 * Delete route entry in the ESI route table.
1930 * This can just be removed.
1932 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
1934 bgp_path_info_reap(rn
, pi
);
1935 bgp_unlock_node(rn
);
1939 /* Delete EVPN type5 route */
1940 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1942 afi_t afi
= AFI_L2VPN
;
1943 safi_t safi
= SAFI_EVPN
;
1944 struct bgp_node
*rn
= NULL
;
1945 struct bgp_path_info
*pi
= NULL
;
1946 struct bgp
*bgp_evpn
= NULL
; /* evpn bgp instance */
1948 bgp_evpn
= bgp_get_evpn();
1952 /* locate the global route entry for this type-5 prefix */
1953 rn
= bgp_afi_node_lookup(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1954 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1958 delete_evpn_route_entry(bgp_evpn
, afi
, safi
, rn
, &pi
);
1960 bgp_process(bgp_evpn
, rn
, afi
, safi
);
1961 bgp_unlock_node(rn
);
1966 * Delete EVPN route (of type based on prefix) for specified VNI and
1967 * schedule for processing.
1969 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1970 struct prefix_evpn
*p
)
1972 struct bgp_node
*rn
, *global_rn
;
1973 struct bgp_path_info
*pi
;
1974 afi_t afi
= AFI_L2VPN
;
1975 safi_t safi
= SAFI_EVPN
;
1977 /* First, locate the route node within the VNI. If it doesn't exist,
1979 * is nothing further to do.
1981 /* NOTE: There is no RD here. */
1982 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1986 /* Next, locate route node in the global EVPN routing table. Note that
1987 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1990 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1991 (struct prefix
*)p
, &vpn
->prd
);
1993 /* Delete route entry in the global EVPN table. */
1994 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1996 /* Schedule for processing - withdraws to peers happen from
2000 bgp_process(bgp
, global_rn
, afi
, safi
);
2001 bgp_unlock_node(global_rn
);
2004 /* Delete route entry in the VNI route table. This can just be removed.
2006 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2008 bgp_path_info_reap(rn
, pi
);
2009 evpn_route_select_install(bgp
, vpn
, rn
);
2011 bgp_unlock_node(rn
);
2017 * Update all type-2 (MACIP) local routes for this VNI - these should also
2018 * be scheduled for advertise to peers.
2020 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2024 struct bgp_node
*rn
;
2025 struct bgp_path_info
*pi
, *tmp_pi
;
2027 struct attr
*attr_new
;
2029 int add_l3_ecomm
= 0;
2034 /* Walk this VNI's route table and update local type-2 routes. For any
2035 * routes updated, update corresponding entry in the global table too.
2037 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2038 rn
= bgp_route_next(rn
)) {
2039 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2040 struct bgp_node
*rd_rn
;
2041 struct bgp_path_info
*global_pi
;
2043 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2046 /* Identify local route. */
2047 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
2048 tmp_pi
= tmp_pi
->next
) {
2049 if (tmp_pi
->peer
== bgp
->peer_self
2050 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2051 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2059 * Build attribute per local route as the MAC mobility and
2060 * some other values could differ for different routes. The
2061 * attributes will be shared in the hash table.
2063 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
2064 attr
.nexthop
= vpn
->originator_ip
;
2065 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
2066 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
2067 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
2069 if (evpn_route_is_sticky(bgp
, rn
))
2071 else if (evpn_route_is_def_gw(bgp
, rn
)) {
2072 attr
.default_gw
= 1;
2073 if (is_evpn_prefix_ipaddr_v6(evp
))
2074 attr
.router_flag
= 1;
2077 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2078 * using L3 VNI for type-2 routes also.
2080 if ((is_evpn_prefix_ipaddr_v4(evp
) ||
2081 !IN6_IS_ADDR_LINKLOCAL(
2082 &evp
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
2083 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
2084 bgpevpn_get_l3vni(vpn
))
2087 /* Set up extended community. */
2088 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2090 seq
= mac_mobility_seqnum(tmp_pi
->attr
);
2092 /* Update the route entry. */
2093 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
, 0, &pi
,
2096 /* Perform route selection; this is just to set the flags
2097 * correctly as local route in the VNI always wins.
2099 evpn_route_select_install(bgp
, vpn
, rn
);
2101 attr_new
= pi
->attr
;
2103 /* Update route in global routing table. */
2104 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2105 (struct prefix
*)evp
, &vpn
->prd
);
2107 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
2109 mac_mobility_seqnum(attr_new
));
2111 /* Schedule for processing and unlock node. */
2112 bgp_process(bgp
, rd_rn
, afi
, safi
);
2113 bgp_unlock_node(rd_rn
);
2115 /* Unintern temporary. */
2116 aspath_unintern(&attr
.aspath
);
2124 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2125 * global routing table. These are also scheduled for withdraw from peers.
2127 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2131 struct bgp_node
*rdrn
, *rn
;
2132 struct bgp_table
*table
;
2133 struct bgp_path_info
*pi
;
2138 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
2139 if (rdrn
&& bgp_node_has_bgp_path_info_data(rdrn
)) {
2140 table
= bgp_node_get_bgp_table_info(rdrn
);
2141 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2142 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2144 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2147 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2149 bgp_process(bgp
, rn
, afi
, safi
);
2153 /* Unlock RD node. */
2155 bgp_unlock_node(rdrn
);
2161 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2162 * table as well as the per-VNI route table.
2164 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2168 struct bgp_node
*rn
;
2169 struct bgp_path_info
*pi
;
2174 /* First, walk the global route table for this VNI's type-2 local
2176 * EVPN routes are a 2-level table, first get the RD table.
2178 delete_global_type2_routes(bgp
, vpn
);
2180 /* Next, walk this VNI's route table and delete local type-2 routes. */
2181 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2182 rn
= bgp_route_next(rn
)) {
2183 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2185 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2188 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2190 /* Route entry in local table gets deleted immediately. */
2192 bgp_path_info_reap(rn
, pi
);
2199 * Delete all routes in per ES route-table
2201 static int delete_all_es_routes(struct bgp
*bgp
, struct evpnes
*es
)
2203 struct bgp_node
*rn
;
2204 struct bgp_path_info
*pi
, *nextpi
;
2206 /* Walk this ES's route table and delete all routes. */
2207 for (rn
= bgp_table_top(es
->route_table
); rn
;
2208 rn
= bgp_route_next(rn
)) {
2209 for (pi
= bgp_node_get_bgp_path_info(rn
);
2210 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2211 bgp_path_info_delete(rn
, pi
);
2212 bgp_path_info_reap(rn
, pi
);
2220 * Delete all routes in the per-VNI route table.
2222 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2224 struct bgp_node
*rn
;
2225 struct bgp_path_info
*pi
, *nextpi
;
2227 /* Walk this VNI's route table and delete all routes. */
2228 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2229 rn
= bgp_route_next(rn
)) {
2230 for (pi
= bgp_node_get_bgp_path_info(rn
);
2231 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2232 bgp_path_info_delete(rn
, pi
);
2233 bgp_path_info_reap(rn
, pi
);
2240 /* BUM traffic flood mode per-l2-vni */
2241 static int bgp_evpn_vni_flood_mode_get(struct bgp
*bgp
,
2242 struct bgpevpn
*vpn
)
2244 /* if flooding has been globally disabled per-vni mode is
2247 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
2248 return VXLAN_FLOOD_DISABLED
;
2250 /* if mcast group ip has been specified we use a PIM-SM MDT */
2251 if (vpn
->mcast_grp
.s_addr
!= INADDR_ANY
)
2252 return VXLAN_FLOOD_PIM_SM
;
2254 /* default is ingress replication */
2255 return VXLAN_FLOOD_HEAD_END_REPL
;
2259 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2260 * export RT getting modified or change to tunnel IP. Note that these
2261 * situations need the route in the per-VNI table as well as the global
2262 * table to be updated (as attributes change).
2264 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2267 struct prefix_evpn p
;
2269 /* Update and advertise the type-3 route (only one) followed by the
2270 * locally learnt type-2 routes (MACIP) - for this VNI.
2272 * RT-3 only if doing head-end replication
2274 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
2275 == VXLAN_FLOOD_HEAD_END_REPL
) {
2276 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2277 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0);
2282 return update_all_type2_routes(bgp
, vpn
);
2285 /* Delete (and withdraw) local routes for specified ES from global and ES table.
2286 * Also remove all other routes from the per ES table.
2287 * Invoked when ES is deleted.
2289 static int delete_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
2292 char buf
[ESI_STR_LEN
];
2293 struct prefix_evpn p
;
2295 /* Delete and withdraw locally learnt ES route */
2296 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
.ipaddr_v4
);
2297 ret
= delete_evpn_type4_route(bgp
, es
, &p
);
2299 flog_err(EC_BGP_EVPN_ROUTE_DELETE
,
2300 "%u failed to delete type-4 route for ESI %s",
2301 bgp
->vrf_id
, esi_to_str(&es
->esi
, buf
, sizeof(buf
)));
2304 /* Delete all routes from per ES table */
2305 return delete_all_es_routes(bgp
, es
);
2309 * Delete (and withdraw) local routes for specified VNI from the global
2310 * table and per-VNI table. After this, remove all other routes from
2311 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2312 * (advertise-all-vni) being disabled.
2314 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2317 struct prefix_evpn p
;
2319 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2320 * followed by type-3 routes (only one) - for this VNI.
2322 ret
= delete_all_type2_routes(bgp
, vpn
);
2326 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2327 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2331 /* Delete all routes from the per-VNI table. */
2332 return delete_all_vni_routes(bgp
, vpn
);
2336 * There is a flood mcast IP address change. Update the mcast-grp and
2337 * remove the type-3 route if any. A new type-3 route will be generated
2338 * post tunnel_ip update if the new flood mode is head-end-replication.
2340 static int bgp_evpn_mcast_grp_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2341 struct in_addr mcast_grp
)
2343 struct prefix_evpn p
;
2345 vpn
->mcast_grp
= mcast_grp
;
2347 if (is_vni_live(vpn
)) {
2348 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2349 delete_evpn_route(bgp
, vpn
, &p
);
2356 * There is a tunnel endpoint IP address change for this VNI, delete
2357 * prior type-3 route (if needed) and update.
2358 * Note: Route re-advertisement happens elsewhere after other processing
2361 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2362 struct in_addr originator_ip
)
2364 struct prefix_evpn p
;
2366 /* If VNI is not live, we only need to update the originator ip */
2367 if (!is_vni_live(vpn
)) {
2368 vpn
->originator_ip
= originator_ip
;
2372 /* Update the tunnel-ip hash */
2373 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2374 bgp_tip_add(bgp
, &originator_ip
);
2376 /* filter routes as martian nexthop db has changed */
2377 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2379 /* Need to withdraw type-3 route as the originator IP is part
2382 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2383 delete_evpn_route(bgp
, vpn
, &p
);
2385 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2386 vpn
->originator_ip
= originator_ip
;
2390 /* Install EVPN route entry in ES */
2391 static int install_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2392 struct prefix_evpn
*p
,
2393 struct bgp_path_info
*parent_pi
)
2396 struct bgp_node
*rn
= NULL
;
2397 struct bgp_path_info
*pi
= NULL
;
2398 struct attr
*attr_new
= NULL
;
2400 /* Create (or fetch) route within the VNI.
2401 * NOTE: There is no RD here.
2403 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
2405 /* Check if route entry is already present. */
2406 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2408 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2412 /* Add (or update) attribute to hash. */
2413 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2415 /* Create new route with its attribute. */
2416 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2417 parent_pi
->peer
, attr_new
, rn
);
2418 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2419 bgp_path_info_extra_get(pi
);
2420 pi
->extra
->parent
= parent_pi
;
2421 bgp_path_info_add(rn
, pi
);
2423 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2424 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2425 bgp_unlock_node(rn
);
2428 /* The attribute has changed. */
2429 /* Add (or update) attribute to hash. */
2430 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2432 /* Restore route, if needed. */
2433 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2434 bgp_path_info_restore(rn
, pi
);
2436 /* Mark if nexthop has changed. */
2437 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2438 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2440 /* Unintern existing, set to new. */
2441 bgp_attr_unintern(&pi
->attr
);
2442 pi
->attr
= attr_new
;
2443 pi
->uptime
= bgp_clock();
2446 /* Perform route selection and update zebra, if required. */
2447 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2452 * Install route entry into the VRF routing table and invoke route selection.
2454 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2455 struct prefix_evpn
*evp
,
2456 struct bgp_path_info
*parent_pi
)
2458 struct bgp_node
*rn
;
2459 struct bgp_path_info
*pi
;
2461 struct attr
*attr_new
;
2464 struct prefix
*pp
= &p
;
2467 char buf
[PREFIX_STRLEN
];
2468 char buf1
[PREFIX_STRLEN
];
2470 memset(pp
, 0, sizeof(struct prefix
));
2471 ip_prefix_from_evpn_prefix(evp
, pp
);
2473 if (bgp_debug_zebra(NULL
)) {
2475 "import evpn prefix %s as ip prefix %s in vrf %s",
2476 prefix2str(evp
, buf
, sizeof(buf
)),
2477 prefix2str(pp
, buf1
, sizeof(buf
)),
2478 vrf_id_to_name(bgp_vrf
->vrf_id
));
2481 /* Create (or fetch) route within the VRF. */
2482 /* NOTE: There is no RD here. */
2483 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2485 safi
= SAFI_UNICAST
;
2486 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2487 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2489 safi
= SAFI_UNICAST
;
2490 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2494 /* EVPN routes currently only support a IPv4 next hop which corresponds
2495 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2496 * or prefix route, we have to convert the next hop to an IPv4-mapped
2497 * address for the rest of the code to flow through. In the case of IPv4,
2498 * make sure to set the flag for next hop attribute.
2500 bgp_attr_dup(&attr
, parent_pi
->attr
);
2502 evpn_convert_nexthop_to_ipv6(&attr
);
2504 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2506 /* Check if route entry is already present. */
2507 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2509 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2513 /* Add (or update) attribute to hash. */
2514 attr_new
= bgp_attr_intern(&attr
);
2516 /* Create new route with its attribute. */
2517 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2518 parent_pi
->peer
, attr_new
, rn
);
2519 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2520 bgp_path_info_extra_get(pi
);
2521 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2522 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2523 if (parent_pi
->extra
) {
2524 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2525 sizeof(pi
->extra
->label
));
2526 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2528 bgp_path_info_add(rn
, pi
);
2530 if (attrhash_cmp(pi
->attr
, &attr
)
2531 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2532 bgp_unlock_node(rn
);
2535 /* The attribute has changed. */
2536 /* Add (or update) attribute to hash. */
2537 attr_new
= bgp_attr_intern(&attr
);
2539 /* Restore route, if needed. */
2540 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2541 bgp_path_info_restore(rn
, pi
);
2543 /* Mark if nexthop has changed. */
2545 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2547 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2548 &attr_new
->mp_nexthop_global
)))
2549 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2551 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_ATTR_CHANGED
);
2552 /* Unintern existing, set to new. */
2553 bgp_attr_unintern(&pi
->attr
);
2554 pi
->attr
= attr_new
;
2555 pi
->uptime
= bgp_clock();
2558 bgp_aggregate_increment(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2560 /* Perform route selection and update zebra, if required. */
2561 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2563 /* Process for route leaking. */
2564 vpn_leak_from_vrf_update(bgp_get_default(), bgp_vrf
, pi
);
2570 * Install route entry into the VNI routing table and invoke route selection.
2572 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2573 struct prefix_evpn
*p
,
2574 struct bgp_path_info
*parent_pi
)
2576 struct bgp_node
*rn
;
2577 struct bgp_path_info
*pi
;
2578 struct attr
*attr_new
;
2581 /* Create (or fetch) route within the VNI. */
2582 /* NOTE: There is no RD here. */
2583 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2585 /* Check if route entry is already present. */
2586 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2588 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2592 /* Add (or update) attribute to hash. */
2593 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2595 /* Create new route with its attribute. */
2596 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2597 parent_pi
->peer
, attr_new
, rn
);
2598 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2599 bgp_path_info_extra_get(pi
);
2600 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2601 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2602 if (parent_pi
->extra
) {
2603 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2604 sizeof(pi
->extra
->label
));
2605 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2607 bgp_path_info_add(rn
, pi
);
2609 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2610 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2611 bgp_unlock_node(rn
);
2614 /* The attribute has changed. */
2615 /* Add (or update) attribute to hash. */
2616 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2618 /* Restore route, if needed. */
2619 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2620 bgp_path_info_restore(rn
, pi
);
2622 /* Mark if nexthop has changed. */
2623 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2624 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2626 /* Unintern existing, set to new. */
2627 bgp_attr_unintern(&pi
->attr
);
2628 pi
->attr
= attr_new
;
2629 pi
->uptime
= bgp_clock();
2632 /* Perform route selection and update zebra, if required. */
2633 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2638 /* Uninstall EVPN route entry from ES route table */
2639 static int uninstall_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2640 struct prefix_evpn
*p
,
2641 struct bgp_path_info
*parent_pi
)
2644 struct bgp_node
*rn
;
2645 struct bgp_path_info
*pi
;
2647 if (!es
->route_table
)
2650 /* Locate route within the ESI.
2651 * NOTE: There is no RD here.
2653 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2657 /* Find matching route entry. */
2658 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2660 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2666 /* Mark entry for deletion */
2667 bgp_path_info_delete(rn
, pi
);
2669 /* Perform route selection and update zebra, if required. */
2670 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2672 /* Unlock route node. */
2673 bgp_unlock_node(rn
);
2679 * Uninstall route entry from the VRF routing table and send message
2680 * to zebra, if appropriate.
2682 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2683 struct prefix_evpn
*evp
,
2684 struct bgp_path_info
*parent_pi
)
2686 struct bgp_node
*rn
;
2687 struct bgp_path_info
*pi
;
2690 struct prefix
*pp
= &p
;
2693 char buf
[PREFIX_STRLEN
];
2694 char buf1
[PREFIX_STRLEN
];
2696 memset(pp
, 0, sizeof(struct prefix
));
2697 ip_prefix_from_evpn_prefix(evp
, pp
);
2699 if (bgp_debug_zebra(NULL
)) {
2701 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2702 prefix2str(evp
, buf
, sizeof(buf
)),
2703 prefix2str(pp
, buf1
, sizeof(buf
)),
2704 vrf_id_to_name(bgp_vrf
->vrf_id
));
2707 /* Locate route within the VRF. */
2708 /* NOTE: There is no RD here. */
2709 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2711 safi
= SAFI_UNICAST
;
2712 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2715 safi
= SAFI_UNICAST
;
2716 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2722 /* Find matching route entry. */
2723 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2725 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2731 /* Process for route leaking. */
2732 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp_vrf
, pi
);
2734 bgp_aggregate_decrement(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2736 /* Mark entry for deletion */
2737 bgp_path_info_delete(rn
, pi
);
2739 /* Perform route selection and update zebra, if required. */
2740 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2742 /* Unlock route node. */
2743 bgp_unlock_node(rn
);
2749 * Uninstall route entry from the VNI routing table and send message
2750 * to zebra, if appropriate.
2752 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2753 struct prefix_evpn
*p
,
2754 struct bgp_path_info
*parent_pi
)
2756 struct bgp_node
*rn
;
2757 struct bgp_path_info
*pi
;
2760 /* Locate route within the VNI. */
2761 /* NOTE: There is no RD here. */
2762 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2766 /* Find matching route entry. */
2767 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2769 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2775 /* Mark entry for deletion */
2776 bgp_path_info_delete(rn
, pi
);
2778 /* Perform route selection and update zebra, if required. */
2779 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2781 /* Unlock route node. */
2782 bgp_unlock_node(rn
);
2788 * Given a prefix, see if it belongs to ES.
2790 static int is_prefix_matching_for_es(struct prefix_evpn
*p
,
2793 /* if not an ES route return false */
2794 if (p
->prefix
.route_type
!= BGP_EVPN_ES_ROUTE
)
2797 if (memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
)) == 0)
2804 * Given a route entry and a VRF, see if this route entry should be
2805 * imported into the VRF i.e., RTs match.
2807 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2808 struct bgp_path_info
*pi
)
2810 struct attr
*attr
= pi
->attr
;
2811 struct ecommunity
*ecom
;
2815 /* Route should have valid RT to be even considered. */
2816 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2819 ecom
= attr
->ecommunity
;
2820 if (!ecom
|| !ecom
->size
)
2823 /* For each extended community RT, see if it matches this VNI. If any RT
2824 * matches, we're done.
2826 for (i
= 0; i
< ecom
->size
; i
++) {
2828 uint8_t type
, sub_type
;
2829 struct ecommunity_val
*eval
;
2830 struct ecommunity_val eval_tmp
;
2831 struct vrf_irt_node
*irt
;
2833 /* Only deal with RTs */
2834 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2835 eval
= (struct ecommunity_val
*)(ecom
->val
2836 + (i
* ECOMMUNITY_SIZE
));
2839 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2842 /* See if this RT matches specified VNIs import RTs */
2843 irt
= lookup_vrf_import_rt(eval
);
2845 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2848 /* Also check for non-exact match. In this, we mask out the AS
2850 * only check on the local-admin sub-field. This is to
2852 * VNI as the RT for EBGP peering too.
2855 if (type
== ECOMMUNITY_ENCODE_AS
2856 || type
== ECOMMUNITY_ENCODE_AS4
2857 || type
== ECOMMUNITY_ENCODE_IP
) {
2858 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2859 mask_ecom_global_admin(&eval_tmp
, eval
);
2860 irt
= lookup_vrf_import_rt(&eval_tmp
);
2863 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2871 * Given a route entry and a VNI, see if this route entry should be
2872 * imported into the VNI i.e., RTs match.
2874 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2875 struct bgp_path_info
*pi
)
2877 struct attr
*attr
= pi
->attr
;
2878 struct ecommunity
*ecom
;
2882 /* Route should have valid RT to be even considered. */
2883 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2886 ecom
= attr
->ecommunity
;
2887 if (!ecom
|| !ecom
->size
)
2890 /* For each extended community RT, see if it matches this VNI. If any RT
2891 * matches, we're done.
2893 for (i
= 0; i
< ecom
->size
; i
++) {
2895 uint8_t type
, sub_type
;
2896 struct ecommunity_val
*eval
;
2897 struct ecommunity_val eval_tmp
;
2898 struct irt_node
*irt
;
2900 /* Only deal with RTs */
2901 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2902 eval
= (struct ecommunity_val
*)(ecom
->val
2903 + (i
* ECOMMUNITY_SIZE
));
2906 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2909 /* See if this RT matches specified VNIs import RTs */
2910 irt
= lookup_import_rt(bgp
, eval
);
2912 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2915 /* Also check for non-exact match. In this, we mask out the AS
2917 * only check on the local-admin sub-field. This is to
2919 * VNI as the RT for EBGP peering too.
2922 if (type
== ECOMMUNITY_ENCODE_AS
2923 || type
== ECOMMUNITY_ENCODE_AS4
2924 || type
== ECOMMUNITY_ENCODE_IP
) {
2925 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2926 mask_ecom_global_admin(&eval_tmp
, eval
);
2927 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2930 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2937 static int install_uninstall_routes_for_es(struct bgp
*bgp
,
2944 char buf
[PREFIX_STRLEN
];
2945 char buf1
[ESI_STR_LEN
];
2946 struct bgp_node
*rd_rn
, *rn
;
2947 struct bgp_table
*table
;
2948 struct bgp_path_info
*pi
;
2954 * Walk entire global routing table and evaluate routes which could be
2955 * imported into this VRF. Note that we need to loop through all global
2956 * routes to determine which route matches the import rt on vrf
2958 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2959 rd_rn
= bgp_route_next(rd_rn
)) {
2960 table
= bgp_node_get_bgp_table_info(rd_rn
);
2964 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2965 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2967 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
2970 * Consider "valid" remote routes applicable for
2973 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
2974 && pi
->type
== ZEBRA_ROUTE_BGP
2975 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
2978 if (!is_prefix_matching_for_es(evp
, es
))
2982 ret
= install_evpn_route_entry_in_es(
2985 ret
= uninstall_evpn_route_entry_in_es(
2991 "Failed to %s EVPN %s route in ESI %s",
2994 prefix2str(evp
, buf
,
2996 esi_to_str(&es
->esi
, buf1
,
3006 /* This API will scan evpn routes for checking attribute's rmac
3007 * macthes with bgp instance router mac. It avoid installing
3008 * route into bgp vrf table and remote rmac in bridge table.
3010 static int bgp_evpn_route_rmac_self_check(struct bgp
*bgp_vrf
,
3011 struct prefix_evpn
*evp
,
3012 struct bgp_path_info
*pi
)
3014 /* evpn route could have learnt prior to L3vni has come up,
3015 * perform rmac check before installing route and
3016 * remote router mac.
3017 * The route will be removed from global bgp table once
3018 * SVI comes up with MAC and stored in hash, triggers
3019 * bgp_mac_rescan_all_evpn_tables.
3022 memcmp(&bgp_vrf
->rmac
, &pi
->attr
->rmac
, ETH_ALEN
) == 0) {
3023 if (bgp_debug_update(pi
->peer
, NULL
, NULL
, 1)) {
3024 char buf1
[PREFIX_STRLEN
];
3025 char attr_str
[BUFSIZ
] = {0};
3027 bgp_dump_attr(pi
->attr
, attr_str
, BUFSIZ
);
3029 zlog_debug("%s: bgp %u prefix %s with attr %s - DENIED due to self mac",
3030 __func__
, bgp_vrf
->vrf_id
,
3031 prefix2str(evp
, buf1
, sizeof(buf1
)),
3042 * Install or uninstall mac-ip routes are appropriate for this
3045 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
3049 struct bgp_node
*rd_rn
, *rn
;
3050 struct bgp_table
*table
;
3051 struct bgp_path_info
*pi
;
3053 char buf
[PREFIX_STRLEN
];
3054 struct bgp
*bgp_evpn
= NULL
;
3058 bgp_evpn
= bgp_get_evpn();
3062 /* Walk entire global routing table and evaluate routes which could be
3063 * imported into this VRF. Note that we need to loop through all global
3064 * routes to determine which route matches the import rt on vrf
3066 for (rd_rn
= bgp_table_top(bgp_evpn
->rib
[afi
][safi
]); rd_rn
;
3067 rd_rn
= bgp_route_next(rd_rn
)) {
3068 table
= bgp_node_get_bgp_table_info(rd_rn
);
3072 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3073 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3075 /* if not mac-ip route skip this route */
3076 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3077 || evp
->prefix
.route_type
3078 == BGP_EVPN_IP_PREFIX_ROUTE
))
3081 /* if not a mac+ip route skip this route */
3082 if (!(is_evpn_prefix_ipaddr_v4(evp
)
3083 || is_evpn_prefix_ipaddr_v6(evp
)))
3086 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3088 /* Consider "valid" remote routes applicable for
3091 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3092 && pi
->type
== ZEBRA_ROUTE_BGP
3093 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3096 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
3097 if (bgp_evpn_route_rmac_self_check(
3102 ret
= install_evpn_route_entry_in_vrf(
3105 ret
= uninstall_evpn_route_entry_in_vrf(
3111 "Failed to %s EVPN %s route in VRF %s",
3114 prefix2str(evp
, buf
,
3129 * Install or uninstall routes of specified type that are appropriate for this
3132 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
3133 struct bgpevpn
*vpn
,
3134 bgp_evpn_route_type rtype
,
3139 struct bgp_node
*rd_rn
, *rn
;
3140 struct bgp_table
*table
;
3141 struct bgp_path_info
*pi
;
3147 /* Walk entire global routing table and evaluate routes which could be
3148 * imported into this VPN. Note that we cannot just look at the routes
3150 * the VNI's RD - remote routes applicable for this VNI could have any
3153 /* EVPN routes are a 2-level table. */
3154 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
3155 rd_rn
= bgp_route_next(rd_rn
)) {
3156 table
= bgp_node_get_bgp_table_info(rd_rn
);
3160 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3161 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3163 if (evp
->prefix
.route_type
!= rtype
)
3166 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3168 /* Consider "valid" remote routes applicable for
3170 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3171 && pi
->type
== ZEBRA_ROUTE_BGP
3172 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3175 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3177 ret
= install_evpn_route_entry(
3180 ret
= uninstall_evpn_route_entry(
3186 "%u: Failed to %s EVPN %s route in VNI %u",
3190 rtype
== BGP_EVPN_MAC_IP_ROUTE
3204 /* Install any existing remote ES routes applicable for this ES into its routing
3205 * table. This is invoked when ES comes up.
3207 static int install_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
3209 return install_uninstall_routes_for_es(bgp
, es
, 1);
3213 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3214 * is invoked upon l3vni-add or l3vni import rt change
3216 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3218 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3223 * Install any existing remote routes applicable for this VNI into its
3224 * routing table. This is invoked when a VNI becomes "live" or its Import
3227 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3231 /* Install type-3 routes followed by type-2 routes - the ones applicable
3234 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3239 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3243 /* uninstall routes from l3vni vrf. */
3244 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3246 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3251 * Uninstall any existing remote routes for this VNI. One scenario in which
3252 * this is invoked is upon an import RT change.
3254 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3258 /* Uninstall type-2 routes followed by type-3 routes - the ones
3262 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3267 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3271 /* Install or unistall route in ES */
3272 static int install_uninstall_route_in_es(struct bgp
*bgp
, struct evpnes
*es
,
3273 afi_t afi
, safi_t safi
,
3274 struct prefix_evpn
*evp
,
3275 struct bgp_path_info
*pi
, int install
)
3278 char buf
[ESI_STR_LEN
];
3281 ret
= install_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3283 ret
= uninstall_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3288 "%u: Failed to %s EVPN %s route in ESI %s", bgp
->vrf_id
,
3289 install
? "install" : "uninstall", "ES",
3290 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)));
3297 * Install or uninstall route in matching VRFs (list).
3299 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3300 safi_t safi
, struct prefix_evpn
*evp
,
3301 struct bgp_path_info
*pi
,
3302 struct list
*vrfs
, int install
)
3304 char buf
[PREFIX2STR_BUFFER
];
3305 struct bgp
*bgp_vrf
;
3306 struct listnode
*node
, *nnode
;
3308 /* Only type-2/type-5 routes go into a VRF */
3309 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3310 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3313 /* if it is type-2 route and not a mac+ip route skip this route */
3314 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3315 && !(is_evpn_prefix_ipaddr_v4(evp
)
3316 || is_evpn_prefix_ipaddr_v6(evp
)))
3319 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3323 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3325 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3329 flog_err(EC_BGP_EVPN_FAIL
,
3330 "%u: Failed to %s prefix %s in VRF %s",
3332 install
? "install" : "uninstall",
3333 prefix2str(evp
, buf
, sizeof(buf
)),
3334 vrf_id_to_name(bgp_vrf
->vrf_id
));
3343 * Install or uninstall route in matching VNIs (list).
3345 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3346 safi_t safi
, struct prefix_evpn
*evp
,
3347 struct bgp_path_info
*pi
,
3348 struct list
*vnis
, int install
)
3350 struct bgpevpn
*vpn
;
3351 struct listnode
*node
, *nnode
;
3353 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3356 if (!is_vni_live(vpn
))
3360 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3362 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3365 flog_err(EC_BGP_EVPN_FAIL
,
3366 "%u: Failed to %s EVPN %s route in VNI %u",
3367 bgp
->vrf_id
, install
? "install" : "uninstall",
3368 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3380 * Install or uninstall route for appropriate VNIs/ESIs.
3382 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3384 struct bgp_path_info
*pi
, int import
)
3386 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3387 struct attr
*attr
= pi
->attr
;
3388 struct ecommunity
*ecom
;
3393 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3394 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3395 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3396 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3397 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3400 /* If we don't have Route Target, nothing much to do. */
3401 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3404 ecom
= attr
->ecommunity
;
3405 if (!ecom
|| !ecom
->size
)
3408 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3409 * attached to the route */
3410 for (i
= 0; i
< ecom
->size
; i
++) {
3412 uint8_t type
, sub_type
;
3413 struct ecommunity_val
*eval
;
3414 struct ecommunity_val eval_tmp
;
3415 struct irt_node
*irt
; /* import rt for l2vni */
3416 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3419 /* Only deal with RTs */
3420 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3421 eval
= (struct ecommunity_val
*)(ecom
->val
3422 + (i
* ECOMMUNITY_SIZE
));
3425 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3429 * macip routes (type-2) are imported into VNI and VRF tables.
3430 * IMET route is imported into VNI table.
3431 * prefix routes are imported into VRF table.
3433 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3434 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3435 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3437 irt
= lookup_import_rt(bgp
, eval
);
3439 install_uninstall_route_in_vnis(
3440 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3443 vrf_irt
= lookup_vrf_import_rt(eval
);
3445 install_uninstall_route_in_vrfs(
3446 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3449 /* Also check for non-exact match.
3450 * In this, we mask out the AS and
3451 * only check on the local-admin sub-field.
3452 * This is to facilitate using
3453 * VNI as the RT for EBGP peering too.
3457 if (type
== ECOMMUNITY_ENCODE_AS
3458 || type
== ECOMMUNITY_ENCODE_AS4
3459 || type
== ECOMMUNITY_ENCODE_IP
) {
3460 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3461 mask_ecom_global_admin(&eval_tmp
, eval
);
3462 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3463 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3467 install_uninstall_route_in_vnis(
3468 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3471 install_uninstall_route_in_vrfs(
3472 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3476 /* es route is imported into the es table */
3477 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3479 /* we will match based on the entire esi to avoid
3480 * imoort of an es route for esi2 into esi1
3482 es
= bgp_evpn_lookup_es(bgp
, &evp
->prefix
.es_addr
.esi
);
3483 if (es
&& is_es_local(es
))
3484 install_uninstall_route_in_es(
3485 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3493 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3496 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3498 /* Delete ipv4 default route and withdraw from peers */
3499 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3500 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3501 SAFI_UNICAST
, false);
3503 /* delete all ipv4 routes and withdraw from peers */
3504 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3505 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3507 /* Delete ipv6 default route and withdraw from peers */
3508 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3509 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3510 SAFI_UNICAST
, false);
3512 /* delete all ipv6 routes and withdraw from peers */
3513 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3514 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3518 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3521 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3523 /* update all ipv4 routes */
3524 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3525 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3527 /* update ipv4 default route and withdraw from peers */
3528 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3529 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3530 SAFI_UNICAST
, true);
3532 /* update all ipv6 routes */
3533 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3534 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3536 /* update ipv6 default route and withdraw from peers */
3537 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3538 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3539 SAFI_UNICAST
, true);
3544 * update and advertise local routes for a VRF as type-5 routes.
3545 * This is invoked upon RD change for a VRF. Note taht the processing is only
3546 * done in the global route table using the routes which already exist in the
3549 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3551 /* skip if the RD is configured */
3552 if (is_vrf_rd_configured(bgp_vrf
))
3555 /* derive the RD for the VRF based on new router-id */
3556 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3558 /* update advertise ipv4|ipv6 routes as type-5 routes */
3559 update_advertise_vrf_routes(bgp_vrf
);
3563 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3564 * This is invoked upon VRF RD change. The processing is done only from global
3567 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3569 /* skip if the RD is configured */
3570 if (is_vrf_rd_configured(bgp_vrf
))
3573 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3574 delete_withdraw_vrf_routes(bgp_vrf
);
3578 * Update and advertise local routes for a VNI. Invoked upon router-id
3579 * change. Note that the processing is done only on the global route table
3580 * using routes that already exist in the per-VNI table.
3582 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3584 struct prefix_evpn p
;
3585 struct bgp_node
*rn
, *global_rn
;
3586 struct bgp_path_info
*pi
, *global_pi
;
3588 afi_t afi
= AFI_L2VPN
;
3589 safi_t safi
= SAFI_EVPN
;
3591 /* Locate type-3 route for VNI in the per-VNI table and use its
3592 * attributes to create and advertise the type-3 route for this VNI
3593 * in the global table.
3595 * RT-3 only if doing head-end replication
3597 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3598 == VXLAN_FLOOD_HEAD_END_REPL
) {
3599 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3600 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3601 if (!rn
) /* unexpected */
3603 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3604 if (pi
->peer
== bgp
->peer_self
&&
3605 pi
->type
== ZEBRA_ROUTE_BGP
3606 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3608 if (!pi
) /* unexpected */
3612 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3613 (struct prefix
*)&p
, &vpn
->prd
);
3614 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
,
3615 1, &pi
, 0, mac_mobility_seqnum(attr
));
3617 /* Schedule for processing and unlock node. */
3618 bgp_process(bgp
, global_rn
, afi
, safi
);
3619 bgp_unlock_node(global_rn
);
3622 /* Now, walk this VNI's route table and use the route and its attribute
3623 * to create and schedule route in global table.
3625 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
3626 rn
= bgp_route_next(rn
)) {
3627 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3629 /* Identify MAC-IP local routes. */
3630 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3633 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3634 if (pi
->peer
== bgp
->peer_self
3635 && pi
->type
== ZEBRA_ROUTE_BGP
3636 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3641 /* Create route in global routing table using this route entry's
3645 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3646 (struct prefix
*)evp
, &vpn
->prd
);
3648 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
3650 mac_mobility_seqnum(attr
));
3652 /* Schedule for processing and unlock node. */
3653 bgp_process(bgp
, global_rn
, afi
, safi
);
3654 bgp_unlock_node(global_rn
);
3661 * Delete (and withdraw) local routes for a VNI - only from the global
3662 * table. Invoked upon router-id change.
3664 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3667 struct prefix_evpn p
;
3668 struct bgp_node
*global_rn
;
3669 struct bgp_path_info
*pi
;
3670 afi_t afi
= AFI_L2VPN
;
3671 safi_t safi
= SAFI_EVPN
;
3673 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3674 * for this VNI - from the global table.
3676 ret
= delete_global_type2_routes(bgp
, vpn
);
3680 /* Remove type-3 route for this VNI from global table. */
3681 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3682 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3683 (struct prefix
*)&p
, &vpn
->prd
);
3685 /* Delete route entry in the global EVPN table. */
3686 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
3688 /* Schedule for processing - withdraws to peers happen from
3692 bgp_process(bgp
, global_rn
, afi
, safi
);
3693 bgp_unlock_node(global_rn
);
3700 * Handle router-id change. Update and advertise local routes corresponding
3701 * to this VNI from peers. Note that this is invoked after updating the
3702 * router-id. The routes in the per-VNI table are used to create routes in
3703 * the global table and schedule them.
3705 static void update_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3707 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3709 /* Skip VNIs with configured RD. */
3710 if (is_rd_configured(vpn
))
3713 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3714 update_advertise_vni_routes(bgp
, vpn
);
3718 * Handle router-id change. Delete and withdraw local routes corresponding
3719 * to this VNI from peers. Note that this is invoked prior to updating
3720 * the router-id and is done only on the global route table, the routes
3721 * are needed in the per-VNI table to re-advertise with new router id.
3723 static void withdraw_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3725 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3727 /* Skip VNIs with configured RD. */
3728 if (is_rd_configured(vpn
))
3731 delete_withdraw_vni_routes(bgp
, vpn
);
3735 * Create RT-3 for a VNI and schedule for processing and advertisement.
3736 * This is invoked upon flooding mode changing to head-end replication.
3738 static void create_advertise_type3(struct hash_bucket
*bucket
, void *data
)
3740 struct bgpevpn
*vpn
= bucket
->data
;
3741 struct bgp
*bgp
= data
;
3742 struct prefix_evpn p
;
3744 if (!vpn
|| !is_vni_live(vpn
) ||
3745 bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3746 != VXLAN_FLOOD_HEAD_END_REPL
)
3749 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3750 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0))
3751 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3752 "Type3 route creation failure for VNI %u", vpn
->vni
);
3756 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3757 * This is invoked upon flooding mode changing to drop BUM packets.
3759 static void delete_withdraw_type3(struct hash_bucket
*bucket
, void *data
)
3761 struct bgpevpn
*vpn
= bucket
->data
;
3762 struct bgp
*bgp
= data
;
3763 struct prefix_evpn p
;
3765 if (!vpn
|| !is_vni_live(vpn
))
3768 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3769 delete_evpn_route(bgp
, vpn
, &p
);
3773 * Process received EVPN type-2 route (advertise or withdraw).
3775 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3776 struct attr
*attr
, uint8_t *pfx
, int psize
,
3777 uint32_t addpath_id
)
3779 struct prefix_rd prd
;
3780 struct prefix_evpn p
;
3781 struct bgp_route_evpn evpn
;
3783 uint8_t macaddr_len
;
3784 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
3785 uint32_t num_labels
= 0;
3789 /* Type-2 route should be either 33, 37 or 49 bytes or an
3790 * additional 3 bytes if there is a second label (VNI):
3791 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3792 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3793 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3795 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3796 && psize
!= 40 && psize
!= 52) {
3797 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3798 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3799 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3803 memset(&evpn
, 0, sizeof(evpn
));
3805 /* Make prefix_rd */
3806 prd
.family
= AF_UNSPEC
;
3808 memcpy(&prd
.val
, pfx
, 8);
3811 /* Make EVPN prefix. */
3812 memset(&p
, 0, sizeof(struct prefix_evpn
));
3814 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3815 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3817 /* Copy Ethernet Seg Identifier */
3818 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
3821 /* Copy Ethernet Tag */
3822 memcpy(ð_tag
, pfx
, 4);
3823 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3826 /* Get the MAC Addr len */
3827 macaddr_len
= *pfx
++;
3829 /* Get the MAC Addr */
3830 if (macaddr_len
== (ETH_ALEN
* 8)) {
3831 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
3835 EC_BGP_EVPN_ROUTE_INVALID
,
3836 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3837 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3843 ipaddr_len
= *pfx
++;
3844 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3845 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3847 EC_BGP_EVPN_ROUTE_INVALID
,
3848 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3849 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3854 ipaddr_len
/= 8; /* Convert to bytes. */
3855 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3858 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
3862 /* Get the VNI(s). Stored as bytes here. */
3864 memset(label
, 0, sizeof(label
));
3865 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
3866 pfx
+= BGP_LABEL_BYTES
;
3867 psize
-= (33 + ipaddr_len
);
3868 /* Do we have a second VNI? */
3871 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
3873 * If in future, we are required to access additional fields,
3874 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
3879 /* Process the route. */
3881 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3882 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3883 &prd
, &label
[0], num_labels
, 0, &evpn
);
3885 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3886 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3887 &prd
, &label
[0], num_labels
, &evpn
);
3892 * Process received EVPN type-3 route (advertise or withdraw).
3894 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3895 struct attr
*attr
, uint8_t *pfx
, int psize
,
3896 uint32_t addpath_id
)
3898 struct prefix_rd prd
;
3899 struct prefix_evpn p
;
3904 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3905 * IP len (1) and IP (4 or 16).
3907 if (psize
!= 17 && psize
!= 29) {
3908 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3909 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3910 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3914 /* If PMSI is present, log if it is anything other than IR.
3915 * Note: We just simply ignore the values as it is not clear if
3916 * doing anything else is better.
3919 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3920 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
&&
3921 attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_PIM_SM
) {
3922 flog_warn(EC_BGP_EVPN_PMSI_PRESENT
,
3923 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3924 peer
->bgp
->vrf_id
, peer
->host
,
3925 attr
->pmsi_tnl_type
);
3929 /* Make prefix_rd */
3930 prd
.family
= AF_UNSPEC
;
3932 memcpy(&prd
.val
, pfx
, 8);
3935 /* Make EVPN prefix. */
3936 memset(&p
, 0, sizeof(struct prefix_evpn
));
3938 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3939 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3941 /* Copy Ethernet Tag */
3942 memcpy(ð_tag
, pfx
, 4);
3943 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
3947 ipaddr_len
= *pfx
++;
3948 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3949 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
3950 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
3953 EC_BGP_EVPN_ROUTE_INVALID
,
3954 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3955 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3959 /* Process the route. */
3961 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3962 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3963 &prd
, NULL
, 0, 0, NULL
);
3965 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3966 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3967 &prd
, NULL
, 0, NULL
);
3972 * Process received EVPN type-4 route (advertise or withdraw).
3974 static int process_type4_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3975 struct attr
*attr
, uint8_t *pfx
, int psize
,
3976 uint32_t addpath_id
)
3981 struct in_addr vtep_ip
;
3982 struct prefix_rd prd
;
3983 struct prefix_evpn p
;
3985 /* Type-4 route should be either 23 or 35 bytes
3986 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
3988 if (psize
!= 23 && psize
!= 35) {
3989 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3990 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
3991 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3995 /* Make prefix_rd */
3996 prd
.family
= AF_UNSPEC
;
3998 memcpy(&prd
.val
, pfx
, 8);
4002 memcpy(&esi
, pfx
, ESI_BYTES
);
4007 ipaddr_len
= *pfx
++;
4008 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
4009 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
4012 EC_BGP_EVPN_ROUTE_INVALID
,
4013 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
4014 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4018 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
4019 /* Process the route. */
4021 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4022 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4023 &prd
, NULL
, 0, 0, NULL
);
4025 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4026 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4027 &prd
, NULL
, 0, NULL
);
4034 * Process received EVPN type-5 route (advertise or withdraw).
4036 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4037 struct attr
*attr
, uint8_t *pfx
, int psize
,
4038 uint32_t addpath_id
)
4040 struct prefix_rd prd
;
4041 struct prefix_evpn p
;
4042 struct bgp_route_evpn evpn
;
4045 mpls_label_t label
; /* holds the VNI as in the packet */
4048 /* Type-5 route should be 34 or 58 bytes:
4049 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
4050 * GW (4 or 16) and VNI (3).
4051 * Note that the IP and GW should both be IPv4 or both IPv6.
4053 if (psize
!= 34 && psize
!= 58) {
4054 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4055 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
4056 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4060 /* Make prefix_rd */
4061 prd
.family
= AF_UNSPEC
;
4063 memcpy(&prd
.val
, pfx
, 8);
4066 /* Make EVPN prefix. */
4067 memset(&p
, 0, sizeof(struct prefix_evpn
));
4069 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4070 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
4072 /* Additional information outside of prefix - ESI and GW IP */
4073 memset(&evpn
, 0, sizeof(evpn
));
4076 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
4079 /* Fetch Ethernet Tag. */
4080 memcpy(ð_tag
, pfx
, 4);
4081 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
4084 /* Fetch IP prefix length. */
4086 if (ippfx_len
> IPV6_MAX_BITLEN
) {
4088 EC_BGP_EVPN_ROUTE_INVALID
,
4089 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4090 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
4093 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
4095 /* Determine IPv4 or IPv6 prefix */
4096 /* Since the address and GW are from the same family, this just becomes
4097 * a simple check on the total size.
4100 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
4101 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
4103 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
4106 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
4107 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
4109 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
4113 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4114 memset(&label
, 0, sizeof(label
));
4115 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
4118 * If in future, we are required to access additional fields,
4119 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4123 /* Process the route. */
4125 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4126 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4127 &prd
, &label
, 1, 0, &evpn
);
4129 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4130 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4131 &prd
, &label
, 1, &evpn
);
4136 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
4137 struct prefix_rd
*prd
, mpls_label_t
*label
,
4138 uint32_t num_labels
, struct attr
*attr
)
4142 struct evpn_addr
*p_evpn_p
;
4144 memset(&temp
, 0, 16);
4145 if (p
->family
!= AF_EVPN
)
4147 p_evpn_p
= &(p
->u
.prefix_evpn
);
4149 /* len denites the total len of IP and GW-IP in the route
4150 IP and GW-IP have to be both ipv4 or ipv6
4152 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4153 len
= 8; /* IP and GWIP are both ipv4 */
4155 len
= 32; /* IP and GWIP are both ipv6 */
4156 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4157 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4158 stream_put(s
, prd
->val
, 8);
4160 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
4162 stream_put(s
, &temp
, 10);
4163 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4164 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4165 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4166 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4168 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4170 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4172 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
4174 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
4176 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4177 stream_put_ipv4(s
, 0);
4179 stream_put(s
, &temp
, 16);
4183 stream_put(s
, label
, 3);
4189 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4191 static void cleanup_vni_on_disable(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4193 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4195 /* Remove EVPN routes and schedule for processing. */
4196 delete_routes_for_vni(bgp
, vpn
);
4198 /* Clear "live" flag and see if hash needs to be freed. */
4199 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4200 if (!is_vni_configured(vpn
))
4201 bgp_evpn_free(bgp
, vpn
);
4205 * Free a VNI entry; iterator function called during cleanup.
4207 static void free_vni_entry(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4209 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4211 delete_all_vni_routes(bgp
, vpn
);
4212 bgp_evpn_free(bgp
, vpn
);
4216 * Derive AUTO import RT for BGP VRF - L3VNI
4218 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4220 struct bgp
*bgp_evpn
= NULL
;
4222 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4223 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4226 bgp_evpn
= bgp_get_evpn();
4229 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4233 * Delete AUTO import RT from BGP VRF - L3VNI
4235 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4237 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4241 * Derive AUTO export RT for BGP VRF - L3VNI
4243 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4245 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4246 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4250 * Delete AUTO export RT from BGP VRF - L3VNI
4252 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4254 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4257 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4259 struct bgp
*bgp_evpn
= NULL
;
4260 struct listnode
*node
= NULL
;
4261 struct bgpevpn
*vpn
= NULL
;
4263 bgp_evpn
= bgp_get_evpn();
4267 /* update all type-5 routes */
4268 update_advertise_vrf_routes(bgp_vrf
);
4270 /* update all type-2 routes */
4271 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4272 update_routes_for_vni(bgp_evpn
, vpn
);
4276 * Handle autort change for a given VNI.
4278 static void update_autort_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4280 struct bgpevpn
*vpn
= bucket
->data
;
4282 if (!is_import_rt_configured(vpn
)) {
4283 if (is_vni_live(vpn
))
4284 bgp_evpn_uninstall_routes(bgp
, vpn
);
4285 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4286 list_delete_all_node(vpn
->import_rtl
);
4287 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4288 if (is_vni_live(vpn
))
4289 bgp_evpn_install_routes(bgp
, vpn
);
4291 if (!is_export_rt_configured(vpn
)) {
4292 list_delete_all_node(vpn
->export_rtl
);
4293 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4294 if (is_vni_live(vpn
))
4295 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4303 /* withdraw type-5 route corresponding to ip prefix */
4304 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4305 afi_t afi
, safi_t safi
)
4308 struct prefix_evpn evp
;
4309 char buf
[PREFIX_STRLEN
];
4311 build_type5_prefix_from_ip_prefix(&evp
, p
);
4312 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4315 EC_BGP_EVPN_ROUTE_DELETE
,
4316 "%u failed to delete type-5 route for prefix %s in vrf %s",
4317 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4318 vrf_id_to_name(bgp_vrf
->vrf_id
));
4322 /* withdraw all type-5 routes for an address family */
4323 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4325 struct bgp_table
*table
= NULL
;
4326 struct bgp_node
*rn
= NULL
;
4327 struct bgp_path_info
*pi
;
4329 table
= bgp_vrf
->rib
[afi
][safi
];
4330 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4331 /* Only care about "selected" routes. Also ensure that
4332 * these are routes that are injectable into EVPN.
4334 /* TODO: Support for AddPath for EVPN. */
4335 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4336 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4337 && is_route_injectable_into_evpn(pi
)) {
4338 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
4347 * evpn - enable advertisement of default g/w
4349 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
4350 safi_t safi
, bool add
)
4352 struct prefix ip_prefix
;
4354 /* form the default prefix 0.0.0.0/0 */
4355 memset(&ip_prefix
, 0, sizeof(struct prefix
));
4356 ip_prefix
.family
= afi2family(afi
);
4359 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
4362 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
4369 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4370 * to this function correspond to those of the source IP prefix (best
4371 * path in the case of the attr. In the case of a local prefix (when we
4372 * are advertising local subnets), the src_attr will be NULL.
4374 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4375 struct attr
*src_attr
, afi_t afi
,
4379 struct prefix_evpn evp
;
4380 char buf
[PREFIX_STRLEN
];
4382 build_type5_prefix_from_ip_prefix(&evp
, p
);
4383 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4385 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4386 "%u: Failed to create type-5 route for prefix %s",
4387 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4390 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4391 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4392 * advertisement is enabled.
4394 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4397 struct bgp_table
*table
= NULL
;
4398 struct bgp_node
*rn
= NULL
;
4399 struct bgp_path_info
*pi
;
4401 table
= bgp_vrf
->rib
[afi
][safi
];
4402 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4403 /* Need to identify the "selected" route entry to use its
4404 * attribute. Also, ensure that the route is injectable
4406 * TODO: Support for AddPath for EVPN.
4408 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4409 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4410 && is_route_injectable_into_evpn(pi
)) {
4412 /* apply the route-map */
4413 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4414 route_map_result_t ret
;
4416 ret
= route_map_apply(
4417 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4419 &rn
->p
, RMAP_BGP
, pi
);
4420 if (ret
== RMAP_DENYMATCH
)
4423 bgp_evpn_advertise_type5_route(
4424 bgp_vrf
, &rn
->p
, pi
->attr
, afi
, safi
);
4431 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4433 struct listnode
*node
, *nnode
, *node_to_del
;
4434 struct ecommunity
*ecom
, *ecom_auto
;
4435 struct ecommunity_val eval
;
4437 if (bgp
->advertise_autort_rfc8365
)
4438 vni
|= EVPN_AUTORT_VXLAN
;
4439 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4441 ecom_auto
= ecommunity_new();
4442 ecommunity_add_val(ecom_auto
, &eval
);
4445 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4446 if (ecommunity_match(ecom
, ecom_auto
)) {
4447 ecommunity_free(&ecom
);
4453 list_delete_node(rtl
, node_to_del
);
4455 ecommunity_free(&ecom_auto
);
4458 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4459 struct ecommunity
*ecomadd
)
4461 /* uninstall routes from vrf */
4462 uninstall_routes_for_vrf(bgp_vrf
);
4464 /* Cleanup the RT to VRF mapping */
4465 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4467 /* Remove auto generated RT */
4468 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4470 /* Add the newly configured RT to RT list */
4471 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4472 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4474 /* map VRF to its RTs */
4475 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4477 /* install routes matching the new VRF */
4478 install_routes_for_vrf(bgp_vrf
);
4481 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4482 struct ecommunity
*ecomdel
)
4484 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4485 struct ecommunity
*ecom
= NULL
;
4487 /* uninstall routes from vrf */
4488 uninstall_routes_for_vrf(bgp_vrf
);
4490 /* Cleanup the RT to VRF mapping */
4491 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4493 /* remove the RT from the RT list */
4494 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4495 if (ecommunity_match(ecom
, ecomdel
)) {
4496 ecommunity_free(&ecom
);
4503 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4505 assert(bgp_vrf
->vrf_import_rtl
);
4506 /* fallback to auto import rt, if this was the last RT */
4507 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4508 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4509 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4512 /* map VRFs to its RTs */
4513 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4515 /* install routes matching this new RT */
4516 install_routes_for_vrf(bgp_vrf
);
4519 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4520 struct ecommunity
*ecomadd
)
4522 /* remove auto-generated RT */
4523 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4525 /* Add the new RT to the RT list */
4526 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4527 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4529 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4532 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4533 struct ecommunity
*ecomdel
)
4535 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4536 struct ecommunity
*ecom
= NULL
;
4538 /* Remove the RT from the RT list */
4539 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4540 if (ecommunity_match(ecom
, ecomdel
)) {
4541 ecommunity_free(&ecom
);
4548 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4551 * Temporary assert to make SA happy.
4552 * The ALL_LIST_ELEMENTS macro above has a NULL check
4553 * which means that SA is going to complain about
4554 * the list_isempty call, which doesn't NULL check.
4555 * So until we get this situation cleaned up, here
4558 assert(bgp_vrf
->vrf_export_rtl
);
4560 /* fall back to auto-generated RT if this was the last RT */
4561 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4562 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4563 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4566 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4570 * Handle change to BGP router id. This is invoked twice by the change
4571 * handler, first before the router id has been changed and then after
4572 * the router id has been changed. The first invocation will result in
4573 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4574 * will result in the routes being re-advertised.
4576 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4580 /* delete and withdraw all the type-5 routes
4581 stored in the global table for this vrf
4583 withdraw_router_id_vrf(bgp
);
4585 /* delete all the VNI routes (type-2/type-3) routes for all the
4588 hash_iterate(bgp
->vnihash
,
4589 (void (*)(struct hash_bucket
*,
4590 void *))withdraw_router_id_vni
,
4594 /* advertise all routes in the vrf as type-5 routes with the new
4597 update_router_id_vrf(bgp
);
4599 /* advertise all the VNI routes (type-2/type-3) routes with the
4602 hash_iterate(bgp
->vnihash
,
4603 (void (*)(struct hash_bucket
*,
4604 void *))update_router_id_vni
,
4610 * Handle change to auto-RT algorithm - update and advertise local routes.
4612 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4614 hash_iterate(bgp
->vnihash
,
4615 (void (*)(struct hash_bucket
*,
4616 void*))update_autort_vni
,
4621 * Handle change to export RT - update and advertise local routes.
4623 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4625 return update_routes_for_vni(bgp
, vpn
);
4628 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4631 delete_withdraw_vrf_routes(bgp_vrf
);
4633 update_advertise_vrf_routes(bgp_vrf
);
4637 * Handle change to RD. This is invoked twice by the change handler,
4638 * first before the RD has been changed and then after the RD has
4639 * been changed. The first invocation will result in local routes
4640 * of this VNI being deleted and withdrawn and the next will result
4641 * in the routes being re-advertised.
4643 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4647 delete_withdraw_vni_routes(bgp
, vpn
);
4649 update_advertise_vni_routes(bgp
, vpn
);
4653 * Install routes for this VNI. Invoked upon change to Import RT.
4655 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4657 return install_routes_for_vni(bgp
, vpn
);
4661 * Uninstall all routes installed for this VNI. Invoked upon change
4664 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4666 return uninstall_routes_for_vni(bgp
, vpn
);
4670 * TODO: Hardcoded for a maximum of 2 VNIs right now
4672 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4677 vni1
= label2vni(label
);
4678 if (num_labels
== 2) {
4679 vni2
= label2vni(label
+ 1);
4680 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4682 snprintf(buf
, len
, "%u", vni1
);
4687 * Function to convert evpn route to json format.
4688 * NOTE: We don't use prefix2str as the output here is a bit different.
4690 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
4692 char buf1
[ETHER_ADDR_STRLEN
];
4693 char buf2
[PREFIX2STR_BUFFER
];
4698 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4699 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4700 json_object_int_add(json
, "ethTag",
4701 p
->prefix
.imet_addr
.eth_tag
);
4702 json_object_int_add(json
, "ipLen",
4703 is_evpn_prefix_ipaddr_v4(p
)
4706 json_object_string_add(json
, "ip",
4707 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4708 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4709 if (is_evpn_prefix_ipaddr_none(p
)) {
4710 json_object_int_add(json
, "routeType",
4711 p
->prefix
.route_type
);
4712 json_object_int_add(json
, "ethTag",
4713 p
->prefix
.macip_addr
.eth_tag
);
4714 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4715 json_object_string_add(json
, "mac",
4716 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4722 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4725 json_object_int_add(json
, "routeType",
4726 p
->prefix
.route_type
);
4727 json_object_int_add(json
, "ethTag",
4728 p
->prefix
.macip_addr
.eth_tag
);
4729 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4730 json_object_string_add(json
, "mac",
4731 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4734 json_object_int_add(json
, "ipLen",
4735 is_evpn_prefix_ipaddr_v4(p
)
4738 json_object_string_add(
4741 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4743 PREFIX2STR_BUFFER
));
4746 /* Currently, this is to cater to other AF_ETHERNET code. */
4751 * Function to convert evpn route to string.
4752 * NOTE: We don't use prefix2str as the output here is a bit different.
4754 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
4756 char buf1
[ETHER_ADDR_STRLEN
];
4757 char buf2
[PREFIX2STR_BUFFER
];
4758 char buf3
[ESI_STR_LEN
];
4760 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4761 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
4762 p
->prefix
.imet_addr
.eth_tag
,
4763 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4765 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4766 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4767 if (is_evpn_prefix_ipaddr_none(p
))
4768 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4769 p
->prefix
.route_type
,
4770 p
->prefix
.macip_addr
.eth_tag
,
4772 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4777 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4779 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
4780 p
->prefix
.route_type
,
4781 p
->prefix
.macip_addr
.eth_tag
,
4783 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4785 family
== AF_INET
? IPV4_MAX_BITLEN
4788 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4790 PREFIX2STR_BUFFER
));
4792 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
4793 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4794 p
->prefix
.route_type
,
4795 p
->prefix
.prefix_addr
.eth_tag
,
4796 p
->prefix
.prefix_addr
.ip_prefix_length
,
4797 is_evpn_prefix_ipaddr_v4(p
)
4798 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
4799 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
4800 } else if (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
4801 snprintf(buf
, len
, "[%d]:[%s]:[%d]:[%s]",
4802 p
->prefix
.route_type
,
4803 esi_to_str(&p
->prefix
.es_addr
.esi
, buf3
, sizeof(buf3
)),
4804 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4806 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
));
4808 /* For EVPN route types not supported yet. */
4809 snprintf(buf
, len
, "(unsupported route type %d)",
4810 p
->prefix
.route_type
);
4817 * Encode EVPN prefix in Update (MP_REACH)
4819 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
4820 struct prefix_rd
*prd
, mpls_label_t
*label
,
4821 uint32_t num_labels
, struct attr
*attr
,
4822 int addpath_encode
, uint32_t addpath_tx_id
)
4824 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4825 int len
, ipa_len
= 0;
4828 stream_putl(s
, addpath_tx_id
);
4831 stream_putc(s
, evp
->prefix
.route_type
);
4833 switch (evp
->prefix
.route_type
) {
4834 case BGP_EVPN_MAC_IP_ROUTE
:
4835 if (is_evpn_prefix_ipaddr_v4(evp
))
4836 ipa_len
= IPV4_MAX_BYTELEN
;
4837 else if (is_evpn_prefix_ipaddr_v6(evp
))
4838 ipa_len
= IPV6_MAX_BYTELEN
;
4839 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4840 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4841 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4843 stream_putc(s
, len
);
4844 stream_put(s
, prd
->val
, 8); /* RD */
4846 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
4848 stream_put(s
, 0, 10);
4849 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4850 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4851 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4852 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4853 if (ipa_len
) /* IP */
4854 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4856 /* 1st label is the L2 VNI */
4857 stream_put(s
, label
, BGP_LABEL_BYTES
);
4858 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4859 if (ipa_len
&& num_labels
> 1)
4860 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4863 case BGP_EVPN_IMET_ROUTE
:
4864 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4865 stream_put(s
, prd
->val
, 8); /* RD */
4866 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4867 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4868 /* Originating Router's IP Addr */
4869 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4872 case BGP_EVPN_ES_ROUTE
:
4873 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4874 stream_put(s
, prd
->val
, 8); /* RD */
4875 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4876 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4878 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4881 case BGP_EVPN_IP_PREFIX_ROUTE
:
4882 /* TODO: AddPath support. */
4883 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4891 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4892 struct bgp_nlri
*packet
, int withdraw
)
4898 uint32_t addpath_id
;
4899 int addpath_encoded
;
4904 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4906 lim
= pnt
+ packet
->length
;
4908 safi
= packet
->safi
;
4912 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
4913 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
4914 PEER_CAP_ADDPATH_AF_TX_RCV
));
4916 for (; pnt
< lim
; pnt
+= psize
) {
4917 /* Clear prefix structure. */
4918 memset(&p
, 0, sizeof(struct prefix
));
4920 /* Deal with path-id if AddPath is supported. */
4921 if (addpath_encoded
) {
4922 /* When packet overflow occurs return immediately. */
4923 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4924 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
4926 addpath_id
= ntohl(*((uint32_t *)pnt
));
4927 pnt
+= BGP_ADDPATH_ID_LEN
;
4930 /* All EVPN NLRI types start with type and length. */
4932 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE
;
4937 /* When packet overflow occur return immediately. */
4938 if (pnt
+ psize
> lim
)
4939 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
4942 case BGP_EVPN_MAC_IP_ROUTE
:
4943 if (process_type2_route(peer
, afi
, safi
,
4944 withdraw
? NULL
: attr
, pnt
,
4945 psize
, addpath_id
)) {
4948 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4949 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4950 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE
;
4954 case BGP_EVPN_IMET_ROUTE
:
4955 if (process_type3_route(peer
, afi
, safi
,
4956 withdraw
? NULL
: attr
, pnt
,
4957 psize
, addpath_id
)) {
4960 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4961 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4962 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE
;
4966 case BGP_EVPN_ES_ROUTE
:
4967 if (process_type4_route(peer
, afi
, safi
,
4968 withdraw
? NULL
: attr
, pnt
,
4969 psize
, addpath_id
)) {
4972 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4973 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4974 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE
;
4978 case BGP_EVPN_IP_PREFIX_ROUTE
:
4979 if (process_type5_route(peer
, afi
, safi
,
4980 withdraw
? NULL
: attr
, pnt
,
4981 psize
, addpath_id
)) {
4984 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4985 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4986 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE
;
4995 /* Packet length consistency check. */
4997 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH
;
4999 return BGP_NLRI_PARSE_OK
;
5003 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
5004 * The mapping will be used during route processing.
5005 * bgp_def: default bgp instance
5006 * bgp_vrf: specific bgp vrf instance on which RT is configured
5008 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
5011 struct ecommunity_val
*eval
= NULL
;
5012 struct listnode
*node
= NULL
, *nnode
= NULL
;
5013 struct ecommunity
*ecom
= NULL
;
5015 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5016 for (i
= 0; i
< ecom
->size
; i
++) {
5017 eval
= (struct ecommunity_val
*)(ecom
->val
5019 * ECOMMUNITY_SIZE
));
5020 map_vrf_to_rt(bgp_vrf
, eval
);
5026 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
5028 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
5031 struct ecommunity_val
*eval
;
5032 struct listnode
*node
, *nnode
;
5033 struct ecommunity
*ecom
;
5035 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5036 for (i
= 0; i
< ecom
->size
; i
++) {
5037 struct vrf_irt_node
*irt
;
5038 struct ecommunity_val eval_tmp
;
5040 eval
= (struct ecommunity_val
*)(ecom
->val
5042 * ECOMMUNITY_SIZE
));
5043 /* If using "automatic" RT, we only care about the
5044 * local-admin sub-field.
5045 * This is to facilitate using VNI as the RT for EBGP
5048 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5049 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
5050 BGP_VRF_IMPORT_RT_CFGD
))
5051 mask_ecom_global_admin(&eval_tmp
, eval
);
5053 irt
= lookup_vrf_import_rt(&eval_tmp
);
5055 unmap_vrf_from_rt(bgp_vrf
, irt
);
5062 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
5063 * The mapping will be used during route processing.
5065 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5068 struct ecommunity_val
*eval
;
5069 struct listnode
*node
, *nnode
;
5070 struct ecommunity
*ecom
;
5072 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5073 for (i
= 0; i
< ecom
->size
; i
++) {
5074 eval
= (struct ecommunity_val
*)(ecom
->val
5076 * ECOMMUNITY_SIZE
));
5077 map_vni_to_rt(bgp
, vpn
, eval
);
5083 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
5085 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5088 struct ecommunity_val
*eval
;
5089 struct listnode
*node
, *nnode
;
5090 struct ecommunity
*ecom
;
5092 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5093 for (i
= 0; i
< ecom
->size
; i
++) {
5094 struct irt_node
*irt
;
5095 struct ecommunity_val eval_tmp
;
5097 eval
= (struct ecommunity_val
*)(ecom
->val
5099 * ECOMMUNITY_SIZE
));
5100 /* If using "automatic" RT, we only care about the
5101 * local-admin sub-field.
5102 * This is to facilitate using VNI as the RT for EBGP
5105 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5106 if (!is_import_rt_configured(vpn
))
5107 mask_ecom_global_admin(&eval_tmp
, eval
);
5109 irt
= lookup_import_rt(bgp
, &eval_tmp
);
5111 unmap_vni_from_rt(bgp
, vpn
, irt
);
5117 * Derive Import RT automatically for VNI and map VNI to RT.
5118 * The mapping will be used during route processing.
5120 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5122 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
5123 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
5126 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
5130 * Derive Export RT automatically for VNI.
5132 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5134 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
5135 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5139 * Derive RD automatically for VNI using passed information - it
5140 * is of the form RouterId:unique-id-for-vni.
5142 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5144 if (is_vrf_rd_configured(bgp
))
5147 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5151 * Derive RD automatically for VNI using passed information - it
5152 * is of the form RouterId:unique-id-for-vni.
5154 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5158 vpn
->prd
.family
= AF_UNSPEC
;
5159 vpn
->prd
.prefixlen
= 64;
5160 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
5161 (void)str2prefix_rd(buf
, &vpn
->prd
);
5162 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5168 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5170 struct list
*inst
= bm
->bgp
;
5171 struct listnode
*node
;
5172 struct bgp
*bgp_vrf
;
5174 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5175 if (bgp_vrf
->l3vni
== vni
)
5185 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5187 struct bgpevpn
*vpn
;
5190 memset(&tmp
, 0, sizeof(struct bgpevpn
));
5192 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5197 * Create a new vpn - invoked upon configuration or zebra notification.
5199 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5200 struct in_addr originator_ip
,
5201 vrf_id_t tenant_vrf_id
,
5202 struct in_addr mcast_grp
)
5204 struct bgpevpn
*vpn
;
5209 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5211 /* Set values - RD and RT set to defaults. */
5213 vpn
->originator_ip
= originator_ip
;
5214 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5215 vpn
->mcast_grp
= mcast_grp
;
5217 /* Initialize route-target import and export lists */
5218 vpn
->import_rtl
= list_new();
5219 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5220 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
5221 vpn
->export_rtl
= list_new();
5222 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5223 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
5224 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5225 derive_rd_rt_for_vni(bgp
, vpn
);
5227 /* Initialize EVPN route table. */
5228 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5231 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
5232 XFREE(MTYPE_BGP_EVPN
, vpn
);
5236 /* add to l2vni list on corresponding vrf */
5237 bgpevpn_link_to_l3vni(vpn
);
5239 QOBJ_REG(vpn
, bgpevpn
);
5244 * Free a given VPN - called in multiple scenarios such as zebra
5245 * notification, configuration being deleted, advertise-all-vni disabled etc.
5246 * This just frees appropriate memory, caller should have taken other
5249 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5251 bgpevpn_unlink_from_l3vni(vpn
);
5252 bgp_table_unlock(vpn
->route_table
);
5253 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5254 list_delete(&vpn
->import_rtl
);
5255 list_delete(&vpn
->export_rtl
);
5256 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5257 hash_release(bgp
->vnihash
, vpn
);
5259 XFREE(MTYPE_BGP_EVPN
, vpn
);
5265 struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
)
5270 memset(&tmp
, 0, sizeof(struct evpnes
));
5271 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
5272 es
= hash_lookup(bgp
->esihash
, &tmp
);
5277 * Create a new local es - invoked upon zebra notification.
5279 struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
,
5281 struct ipaddr
*originator_ip
)
5289 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct evpnes
));
5291 /* set the ESI and originator_ip */
5292 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
5293 memcpy(&es
->originator_ip
, originator_ip
, sizeof(struct ipaddr
));
5295 /* Initialise the VTEP list */
5296 es
->vtep_list
= list_new();
5297 es
->vtep_list
->cmp
= evpn_vtep_ip_cmp
;
5299 /* auto derive RD for this es */
5300 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
5301 es
->prd
.family
= AF_UNSPEC
;
5302 es
->prd
.prefixlen
= 64;
5303 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), es
->rd_id
);
5304 (void)str2prefix_rd(buf
, &es
->prd
);
5306 /* Initialize the ES route table */
5307 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5310 if (!hash_get(bgp
->esihash
, es
, hash_alloc_intern
)) {
5311 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5315 QOBJ_REG(es
, evpnes
);
5321 * This just frees appropriate memory, caller should have taken other
5324 void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
)
5326 list_delete(&es
->vtep_list
);
5327 bgp_table_unlock(es
->route_table
);
5328 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
5329 hash_release(bgp
->esihash
, es
);
5331 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5335 * Import evpn route from global table to VNI/VRF/ESI.
5337 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5338 struct prefix
*p
, struct bgp_path_info
*pi
)
5340 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5344 * Unimport evpn route from VNI/VRF/ESI.
5346 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5347 struct prefix
*p
, struct bgp_path_info
*pi
)
5349 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5352 /* filter routes which have martian next hops */
5353 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5357 struct bgp_node
*rd_rn
, *rn
;
5358 struct bgp_table
*table
;
5359 struct bgp_path_info
*pi
;
5364 /* Walk entire global routing table and evaluate routes which could be
5365 * imported into this VPN. Note that we cannot just look at the routes
5366 * for the VNI's RD -
5367 * remote routes applicable for this VNI could have any RD.
5369 /* EVPN routes are a 2-level table. */
5370 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
5371 rd_rn
= bgp_route_next(rd_rn
)) {
5372 table
= bgp_node_get_bgp_table_info(rd_rn
);
5376 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5378 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
5381 /* Consider "valid" remote routes applicable for
5383 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5384 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5387 if (bgp_nexthop_self(bgp
, pi
->attr
->nexthop
)) {
5389 char attr_str
[BUFSIZ
] = {0};
5390 char pbuf
[PREFIX_STRLEN
];
5392 bgp_dump_attr(pi
->attr
, attr_str
,
5395 if (bgp_debug_update(pi
->peer
, &rn
->p
,
5398 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
5405 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5408 bgp_rib_remove(rn
, pi
, pi
->peer
, afi
,
5419 * Handle del of a local MACIP.
5421 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5422 struct ipaddr
*ip
, int state
)
5424 struct bgpevpn
*vpn
;
5425 struct prefix_evpn p
;
5426 struct bgp_node
*rn
;
5428 /* Lookup VNI hash - should exist. */
5429 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5430 if (!vpn
|| !is_vni_live(vpn
)) {
5431 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5432 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5433 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5437 build_evpn_type2_prefix(&p
, mac
, ip
);
5438 if (state
== ZEBRA_NEIGH_ACTIVE
) {
5439 /* Remove EVPN type-2 route and schedule for processing. */
5440 delete_evpn_route(bgp
, vpn
, &p
);
5442 /* Re-instate the current remote best path if any */
5443 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
5445 evpn_zebra_reinstall_best_route(bgp
, vpn
, rn
);
5452 * Handle add of a local MACIP.
5454 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5455 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
)
5457 struct bgpevpn
*vpn
;
5458 struct prefix_evpn p
;
5460 /* Lookup VNI hash - should exist. */
5461 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5462 if (!vpn
|| !is_vni_live(vpn
)) {
5463 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5464 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5465 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5469 /* Create EVPN type-2 route and schedule for processing. */
5470 build_evpn_type2_prefix(&p
, mac
, ip
);
5471 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
)) {
5472 char buf
[ETHER_ADDR_STRLEN
];
5473 char buf2
[INET6_ADDRSTRLEN
];
5476 EC_BGP_EVPN_ROUTE_CREATE
,
5477 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5478 bgp
->vrf_id
, vpn
->vni
,
5479 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5482 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5483 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5490 static void link_l2vni_hash_to_l3vni(struct hash_bucket
*bucket
,
5491 struct bgp
*bgp_vrf
)
5493 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
5494 struct bgp
*bgp_evpn
= NULL
;
5496 bgp_evpn
= bgp_get_evpn();
5499 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5500 bgpevpn_link_to_l3vni(vpn
);
5503 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
, struct ethaddr
*rmac
,
5504 struct in_addr originator_ip
, int filter
,
5505 ifindex_t svi_ifindex
)
5507 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5508 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5509 struct listnode
*node
= NULL
;
5510 struct bgpevpn
*vpn
= NULL
;
5513 /* get the EVPN instance - required to get the AS number for VRF
5516 bgp_evpn
= bgp_get_evpn();
5520 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
5526 /* if the BGP vrf instance doesn't exist - create one */
5527 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5532 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5533 vrf_id
== VRF_DEFAULT
? BGP_INSTANCE_TYPE_DEFAULT
5534 : BGP_INSTANCE_TYPE_VRF
);
5536 case BGP_ERR_AS_MISMATCH
:
5537 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5538 "BGP is already running; AS is %u\n", as
);
5540 case BGP_ERR_INSTANCE_MISMATCH
:
5541 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5542 "BGP instance name and AS number mismatch\n");
5546 /* mark as auto created */
5547 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5550 /* associate the vrf with l3vni and related parameters */
5551 bgp_vrf
->l3vni
= l3vni
;
5552 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
5553 bgp_vrf
->originator_ip
= originator_ip
;
5554 bgp_vrf
->l3vni_svi_ifindex
= svi_ifindex
;
5556 /* set the right filter - are we using l3vni only for prefix routes? */
5558 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5560 /* Map auto derive or configured RTs */
5561 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5562 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5564 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
5566 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5567 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5569 /* auto derive RD */
5570 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5572 /* link all corresponding l2vnis */
5573 hash_iterate(bgp_evpn
->vnihash
,
5574 (void (*)(struct hash_bucket
*,
5575 void *))link_l2vni_hash_to_l3vni
,
5578 /* Only update all corresponding type-2 routes if we are advertising two
5579 * labels along with type-2 routes
5582 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5583 update_routes_for_vni(bgp_evpn
, vpn
);
5585 /* advertise type-5 routes if needed */
5586 update_advertise_vrf_routes(bgp_vrf
);
5588 /* install all remote routes belonging to this l3vni into correspondng
5590 install_routes_for_vrf(bgp_vrf
);
5595 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5597 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5598 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5599 struct listnode
*node
= NULL
;
5600 struct listnode
*next
= NULL
;
5601 struct bgpevpn
*vpn
= NULL
;
5603 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5607 "Cannot process L3VNI %u Del - Could not find BGP instance",
5612 bgp_evpn
= bgp_get_evpn();
5616 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
5621 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5622 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5623 * routes. This will uninstalling the routes from zebra and decremnt the
5626 uninstall_routes_for_vrf(bgp_vrf
);
5628 /* delete/withdraw all type-5 routes */
5629 delete_withdraw_vrf_routes(bgp_vrf
);
5631 /* remove the l3vni from vrf instance */
5634 /* remove the Rmac from the BGP vrf */
5635 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5637 /* remove default import RT or Unmap non-default import RT */
5638 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5639 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5640 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5641 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5644 /* remove default export RT */
5645 if (!list_isempty(bgp_vrf
->vrf_export_rtl
) &&
5646 !CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
5647 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5650 /* update all corresponding local mac-ip routes */
5651 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5652 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5653 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5654 update_routes_for_vni(bgp_evpn
, vpn
);
5658 /* If any L2VNIs point to this instance, unlink them. */
5659 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5660 bgpevpn_unlink_from_l3vni(vpn
);
5662 /* Delete the instance if it was autocreated */
5663 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5664 bgp_delete(bgp_vrf
);
5670 * Handle del of a local VNI.
5672 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5674 struct bgpevpn
*vpn
;
5676 /* Locate VNI hash */
5677 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5679 if (bgp_debug_zebra(NULL
))
5681 EC_BGP_EVPN_VPN_VNI
,
5682 "%u: VNI hash entry for VNI %u not found at DEL",
5687 /* Remove all local EVPN routes and schedule for processing (to
5688 * withdraw from peers).
5690 delete_routes_for_vni(bgp
, vpn
);
5693 * tunnel is no longer active, del tunnel ip address from tip_hash
5695 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5697 /* Clear "live" flag and see if hash needs to be freed. */
5698 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5699 if (!is_vni_configured(vpn
))
5700 bgp_evpn_free(bgp
, vpn
);
5706 * Handle add (or update) of a local VNI. The VNI changes we care
5707 * about are for the local-tunnel-ip and the (tenant) VRF.
5709 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5710 struct in_addr originator_ip
,
5711 vrf_id_t tenant_vrf_id
,
5712 struct in_addr mcast_grp
)
5715 struct bgpevpn
*vpn
;
5716 struct prefix_evpn p
;
5718 /* Lookup VNI. If present and no change, exit. */
5719 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5722 if (is_vni_live(vpn
)
5723 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5724 && IPV4_ADDR_SAME(&vpn
->mcast_grp
, &mcast_grp
)
5725 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5726 /* Probably some other param has changed that we don't
5730 bgp_evpn_mcast_grp_change(bgp
, vpn
, mcast_grp
);
5732 /* Update tenant_vrf_id if it has changed. */
5733 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5734 bgpevpn_unlink_from_l3vni(vpn
);
5735 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5736 bgpevpn_link_to_l3vni(vpn
);
5739 /* If tunnel endpoint IP has changed, update (and delete prior
5740 * type-3 route, if needed.)
5742 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5743 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5745 /* Update all routes with new endpoint IP and/or export RT
5748 if (is_vni_live(vpn
))
5749 update_routes_for_vni(bgp
, vpn
);
5752 /* Create or update as appropriate. */
5754 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
,
5759 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5765 /* if the VNI is live already, there is nothing more to do */
5766 if (is_vni_live(vpn
))
5769 /* Mark as "live" */
5770 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5772 /* tunnel is now active, add tunnel-ip to db */
5773 bgp_tip_add(bgp
, &originator_ip
);
5775 /* filter routes as nexthop database has changed */
5776 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5779 * Create EVPN type-3 route and schedule for processing.
5781 * RT-3 only if doing head-end replication
5783 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
5784 == VXLAN_FLOOD_HEAD_END_REPL
) {
5785 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5786 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0)) {
5787 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5788 "%u: Type3 route creation failure for VNI %u",
5794 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5798 install_routes_for_vni(bgp
, vpn
);
5800 /* If we are advertising gateway mac-ip
5801 It needs to be conveyed again to zebra */
5802 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5808 * bgp_evpn_local_es_del
5810 int bgp_evpn_local_es_del(struct bgp
*bgp
,
5812 struct ipaddr
*originator_ip
)
5814 char buf
[ESI_STR_LEN
];
5815 struct evpnes
*es
= NULL
;
5817 if (!bgp
->esihash
) {
5818 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5823 /* Lookup ESI hash - should exist. */
5824 es
= bgp_evpn_lookup_es(bgp
, esi
);
5826 flog_warn(EC_BGP_EVPN_ESI
,
5827 "%u: ESI hash entry for ESI %s at Local ES DEL",
5828 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5832 /* Delete all local EVPN ES routes from ESI table
5833 * and schedule for processing (to withdraw from peers))
5835 delete_routes_for_es(bgp
, es
);
5837 /* free the hash entry */
5838 bgp_evpn_es_free(bgp
, es
);
5844 * bgp_evpn_local_es_add
5846 int bgp_evpn_local_es_add(struct bgp
*bgp
,
5848 struct ipaddr
*originator_ip
)
5850 char buf
[ESI_STR_LEN
];
5851 struct evpnes
*es
= NULL
;
5852 struct prefix_evpn p
;
5854 if (!bgp
->esihash
) {
5855 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5860 /* create the new es */
5861 es
= bgp_evpn_lookup_es(bgp
, esi
);
5863 es
= bgp_evpn_es_new(bgp
, esi
, originator_ip
);
5867 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
5868 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5872 UNSET_FLAG(es
->flags
, EVPNES_REMOTE
);
5873 SET_FLAG(es
->flags
, EVPNES_LOCAL
);
5875 build_evpn_type4_prefix(&p
, esi
, originator_ip
->ipaddr_v4
);
5876 if (update_evpn_type4_route(bgp
, es
, &p
)) {
5877 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5878 "%u: Type4 route creation failure for ESI %s",
5879 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5883 /* import all remote ES routes in th ES table */
5884 install_routes_for_es(bgp
, es
);
5890 * Handle change in setting for BUM handling. The supported values
5891 * are head-end replication and dropping all BUM packets. Any change
5892 * should be registered with zebra. Also, if doing head-end replication,
5893 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5894 * to be dropped, the RT-3s must be withdrawn.
5896 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
5898 zlog_info("L2VPN EVPN BUM handling is %s",
5899 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
5900 "Flooding" : "Flooding Disabled");
5902 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
5903 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
5904 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
5905 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
5906 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
5910 * Cleanup EVPN information on disable - Need to delete and withdraw
5911 * EVPN routes from peers.
5913 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5915 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_bucket
*,
5916 void *))cleanup_vni_on_disable
,
5921 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5922 * BGP instance (default) is being freed.
5924 void bgp_evpn_cleanup(struct bgp
*bgp
)
5926 hash_iterate(bgp
->vnihash
,
5927 (void (*)(struct hash_bucket
*, void *))free_vni_entry
,
5930 hash_free(bgp
->import_rt_hash
);
5931 bgp
->import_rt_hash
= NULL
;
5933 hash_free(bgp
->vrf_import_rt_hash
);
5934 bgp
->vrf_import_rt_hash
= NULL
;
5936 hash_free(bgp
->vnihash
);
5937 bgp
->vnihash
= NULL
;
5939 hash_free(bgp
->esihash
);
5940 bgp
->esihash
= NULL
;
5942 list_delete(&bgp
->vrf_import_rtl
);
5943 list_delete(&bgp
->vrf_export_rtl
);
5944 list_delete(&bgp
->l2vnis
);
5948 * Initialization for EVPN
5951 * hash for RT to VNI
5953 void bgp_evpn_init(struct bgp
*bgp
)
5956 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
5958 hash_create(esi_hash_keymake
, esi_cmp
,
5959 "BGP EVPN Local ESI Hash");
5960 bgp
->import_rt_hash
=
5961 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
5962 "BGP Import RT Hash");
5963 bgp
->vrf_import_rt_hash
=
5964 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
5965 "BGP VRF Import RT Hash");
5966 bgp
->vrf_import_rtl
= list_new();
5967 bgp
->vrf_import_rtl
->cmp
=
5968 (int (*)(void *, void *))evpn_route_target_cmp
;
5969 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
5970 bgp
->vrf_export_rtl
= list_new();
5971 bgp
->vrf_export_rtl
->cmp
=
5972 (int (*)(void *, void *))evpn_route_target_cmp
;
5973 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
5974 bgp
->l2vnis
= list_new();
5975 bgp
->l2vnis
->cmp
= vni_list_cmp
;
5976 /* By default Duplicate Address Dection is enabled.
5977 * Max-moves (N) 5, detection time (M) 180
5978 * default action is warning-only
5979 * freeze action permanently freezes address,
5980 * and freeze time (auto-recovery) is disabled.
5982 if (bgp
->evpn_info
) {
5983 bgp
->evpn_info
->dup_addr_detect
= true;
5984 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
5985 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
5986 bgp
->evpn_info
->dad_freeze
= false;
5987 bgp
->evpn_info
->dad_freeze_time
= 0;
5988 /* Initialize zebra vxlan */
5989 bgp_zebra_dup_addr_detection(bgp
);
5992 /* Default BUM handling is to do head-end replication. */
5993 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
5996 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
5998 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);