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"
53 * Definitions and external declarations.
55 extern struct zclient
*zclient
;
57 DEFINE_QOBJ_TYPE(bgpevpn
)
58 DEFINE_QOBJ_TYPE(evpnes
)
62 * Static function declarations
64 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
66 struct bgp_path_info
**pi
);
67 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
73 /* compare two IPV4 VTEP IPs */
74 static int evpn_vtep_ip_cmp(void *p1
, void *p2
)
76 const struct in_addr
*ip1
= p1
;
77 const struct in_addr
*ip2
= p2
;
79 return ip1
->s_addr
- ip2
->s_addr
;
83 * Make hash key for ESI.
85 static unsigned int esi_hash_keymake(void *p
)
87 struct evpnes
*pes
= p
;
88 const void *pnt
= (void *)pes
->esi
.val
;
90 return jhash(pnt
, ESI_BYTES
, 0xa5a5a55a);
96 static bool esi_cmp(const void *p1
, const void *p2
)
98 const struct evpnes
*pes1
= p1
;
99 const struct evpnes
*pes2
= p2
;
101 if (pes1
== NULL
&& pes2
== NULL
)
104 if (pes1
== NULL
|| pes2
== NULL
)
107 return (memcmp(pes1
->esi
.val
, pes2
->esi
.val
, ESI_BYTES
) == 0);
113 static unsigned int vni_hash_key_make(void *p
)
115 struct bgpevpn
*vpn
= p
;
116 return (jhash_1word(vpn
->vni
, 0));
120 * Comparison function for vni hash
122 static bool vni_hash_cmp(const void *p1
, const void *p2
)
124 const struct bgpevpn
*vpn1
= p1
;
125 const struct bgpevpn
*vpn2
= p2
;
131 return (vpn1
->vni
== vpn2
->vni
);
134 static int vni_list_cmp(void *p1
, void *p2
)
136 const struct bgpevpn
*vpn1
= p1
;
137 const struct bgpevpn
*vpn2
= p2
;
139 return vpn1
->vni
- vpn2
->vni
;
143 * Make vrf import route target hash key.
145 static unsigned int vrf_import_rt_hash_key_make(void *p
)
147 struct vrf_irt_node
*irt
= p
;
148 char *pnt
= irt
->rt
.val
;
150 return jhash(pnt
, 8, 0x5abc1234);
154 * Comparison function for vrf import rt hash
156 static bool vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
158 const struct vrf_irt_node
*irt1
= p1
;
159 const struct vrf_irt_node
*irt2
= p2
;
161 if (irt1
== NULL
&& irt2
== NULL
)
164 if (irt1
== NULL
|| irt2
== NULL
)
167 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
171 * Create a new vrf import_rt in default instance
173 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
175 struct bgp
*bgp_def
= NULL
;
176 struct vrf_irt_node
*irt
;
178 bgp_def
= bgp_get_default();
180 flog_err(EC_BGP_NO_DFLT
,
181 "vrf import rt new - def instance not created yet");
185 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
186 sizeof(struct vrf_irt_node
));
191 irt
->vrfs
= list_new();
194 if (!hash_get(bgp_def
->vrf_import_rt_hash
, irt
, hash_alloc_intern
)) {
195 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
203 * Free the vrf import rt node
205 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
207 struct bgp
*bgp_def
= NULL
;
209 bgp_def
= bgp_get_default();
211 flog_err(EC_BGP_NO_DFLT
,
212 "vrf import rt free - def instance not created yet");
216 hash_release(bgp_def
->vrf_import_rt_hash
, irt
);
217 list_delete(&irt
->vrfs
);
218 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
222 * Function to lookup Import RT node - used to map a RT to set of
223 * VNIs importing routes with that RT.
225 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
227 struct bgp
*bgp_def
= NULL
;
228 struct vrf_irt_node
*irt
;
229 struct vrf_irt_node tmp
;
231 bgp_def
= bgp_get_default();
233 flog_err(EC_BGP_NO_DFLT
,
234 "vrf import rt lookup - def 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_def
->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(void *p
)
264 struct irt_node
*irt
= p
;
265 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
));
303 irt
->vnis
= list_new();
306 if (!hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
)) {
307 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
315 * Free the import rt node
317 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
319 hash_release(bgp
->import_rt_hash
, irt
);
320 list_delete(&irt
->vnis
);
321 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
325 * Function to lookup Import RT node - used to map a RT to set of
326 * VNIs importing routes with that RT.
328 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
329 struct ecommunity_val
*rt
)
331 struct irt_node
*irt
;
334 memset(&tmp
, 0, sizeof(struct irt_node
));
335 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
336 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
341 * Is specified VNI present on the RT's list of "importing" VNIs?
343 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
345 struct listnode
*node
, *nnode
;
346 struct bgpevpn
*tmp_vpn
;
348 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
357 * Compare Route Targets.
359 static int evpn_route_target_cmp(struct ecommunity
*ecom1
,
360 struct ecommunity
*ecom2
)
368 if (!ecom1
&& !ecom2
)
371 if (ecom1
->str
&& !ecom2
->str
)
374 if (!ecom1
->str
&& ecom2
->str
)
377 if (!ecom1
->str
&& !ecom2
->str
)
380 return strcmp(ecom1
->str
, ecom2
->str
);
383 static void evpn_xxport_delete_ecomm(void *val
)
385 struct ecommunity
*ecomm
= val
;
386 ecommunity_free(&ecomm
);
390 * Mask off global-admin field of specified extended community (RT),
391 * just retain the local-admin field.
393 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
394 struct ecommunity_val
*src
)
400 if (type
== ECOMMUNITY_ENCODE_AS
) {
401 dst
->val
[2] = dst
->val
[3] = 0;
402 } else if (type
== ECOMMUNITY_ENCODE_AS4
403 || type
== ECOMMUNITY_ENCODE_IP
) {
404 dst
->val
[2] = dst
->val
[3] = 0;
405 dst
->val
[4] = dst
->val
[5] = 0;
410 * Map one RT to specified VRF.
411 * bgp_vrf = BGP vrf instance
413 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct ecommunity_val
*eval
)
415 struct vrf_irt_node
*irt
= NULL
;
416 struct ecommunity_val eval_tmp
;
418 /* If using "automatic" RT,
419 * we only care about the local-admin sub-field.
420 * This is to facilitate using L3VNI(VRF-VNI)
421 * as the RT for EBGP peering too.
423 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
424 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
425 mask_ecom_global_admin(&eval_tmp
, eval
);
427 irt
= lookup_vrf_import_rt(&eval_tmp
);
428 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
429 /* Already mapped. */
433 irt
= vrf_import_rt_new(&eval_tmp
);
435 /* Add VRF to the list for this RT. */
436 listnode_add(irt
->vrfs
, bgp_vrf
);
440 * Unmap specified VRF from specified RT. If there are no other
441 * VRFs for this RT, then the RT hash is deleted.
442 * bgp_vrf: BGP VRF specific instance
444 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
, struct vrf_irt_node
*irt
)
446 /* Delete VRF from list for this RT. */
447 listnode_delete(irt
->vrfs
, bgp_vrf
);
448 if (!listnode_head(irt
->vrfs
)) {
449 vrf_import_rt_free(irt
);
454 * Map one RT to specified VNI.
456 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
457 struct ecommunity_val
*eval
)
459 struct irt_node
*irt
;
460 struct ecommunity_val eval_tmp
;
462 /* If using "automatic" RT, we only care about the local-admin
464 * This is to facilitate using VNI as the RT for EBGP peering too.
466 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
467 if (!is_import_rt_configured(vpn
))
468 mask_ecom_global_admin(&eval_tmp
, eval
);
470 irt
= lookup_import_rt(bgp
, &eval_tmp
);
472 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
473 /* Already mapped. */
477 irt
= import_rt_new(bgp
, &eval_tmp
);
481 /* Add VNI to the hash list for this RT. */
482 listnode_add(irt
->vnis
, vpn
);
486 * Unmap specified VNI from specified RT. If there are no other
487 * VNIs for this RT, then the RT hash is deleted.
489 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
490 struct irt_node
*irt
)
492 /* Delete VNI from hash list for this RT. */
493 listnode_delete(irt
->vnis
, vpn
);
494 if (!listnode_head(irt
->vnis
)) {
495 import_rt_free(bgp
, irt
);
500 * Create RT extended community automatically from passed information:
501 * of the form AS:VNI.
502 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
503 * the need is to get a RT value that will be unique across different
504 * VNIs but the same across routers (in the same AS) for a particular
507 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
509 struct ecommunity_val eval
;
510 struct ecommunity
*ecomadd
;
512 if (bgp
->advertise_autort_rfc8365
)
513 vni
|= EVPN_AUTORT_VXLAN
;
514 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
516 ecomadd
= ecommunity_new();
517 ecommunity_add_val(ecomadd
, &eval
);
518 listnode_add_sort(rtl
, ecomadd
);
522 * Derive RD and RT for a VNI automatically. Invoked at the time of
525 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
527 bgp_evpn_derive_auto_rd(bgp
, vpn
);
528 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
529 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
533 * Convert nexthop (remote VTEP IP) into an IPv6 address.
535 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
537 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
539 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
540 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
544 * Add (update) or delete MACIP from zebra.
546 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
547 struct prefix_evpn
*p
,
548 struct in_addr remote_vtep_ip
, int add
,
549 uint8_t flags
, uint32_t seq
)
553 char buf1
[ETHER_ADDR_STRLEN
];
554 char buf2
[INET6_ADDRSTRLEN
];
555 char buf3
[INET6_ADDRSTRLEN
];
558 if (!zclient
|| zclient
->sock
< 0)
561 /* Don't try to register if Zebra doesn't know of this instance. */
562 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
563 if (BGP_DEBUG(zebra
, ZEBRA
))
564 zlog_debug("%s: No zebra instance to talk to, not installing remote macip",
565 __PRETTY_FUNCTION__
);
571 zclient_create_header(
572 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
574 stream_putl(s
, vpn
->vni
);
575 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
576 /* IP address length and IP address, if any. */
577 if (is_evpn_prefix_ipaddr_none(p
))
580 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
582 stream_putl(s
, ipa_len
);
583 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
585 stream_put_in_addr(s
, &remote_vtep_ip
);
587 /* TX flags - MAC sticky status and/or gateway mac */
588 /* Also TX the sequence number of the best route. */
590 stream_putc(s
, flags
);
594 stream_putw_at(s
, 0, stream_get_endp(s
));
596 if (bgp_debug_zebra(NULL
))
598 "Tx %s MACIP, VNI %u MAC %s IP %s flags 0x%x seq %u remote VTEP %s",
599 add
? "ADD" : "DEL", vpn
->vni
,
600 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
602 ipaddr2str(&p
->prefix
.macip_addr
.ip
,
603 buf3
, sizeof(buf3
)), flags
, seq
,
604 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
607 return zclient_send_message(zclient
);
611 * Add (update) or delete remote VTEP from zebra.
613 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
614 struct prefix_evpn
*p
, int add
)
619 if (!zclient
|| zclient
->sock
< 0)
622 /* Don't try to register if Zebra doesn't know of this instance. */
623 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
624 if (BGP_DEBUG(zebra
, ZEBRA
))
625 zlog_debug("%s: No zebra instance to talk to, not installing remote vtep",
626 __PRETTY_FUNCTION__
);
633 zclient_create_header(
634 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
636 stream_putl(s
, vpn
->vni
);
637 if (is_evpn_prefix_ipaddr_v4(p
))
638 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
639 else if (is_evpn_prefix_ipaddr_v6(p
)) {
642 "Bad remote IP when trying to %s remote VTEP for VNI %u",
643 add
? "ADD" : "DEL", vpn
->vni
);
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
,
891 struct in_addr remote_vtep_ip
, uint8_t flags
,
896 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
897 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
900 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 1);
905 /* Uninstall EVPN route from zebra. */
906 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
907 struct prefix_evpn
*p
,
908 struct in_addr remote_vtep_ip
)
912 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
913 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
916 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 0);
922 * Due to MAC mobility, the prior "local" best route has been supplanted
923 * by a "remote" best route. The prior route has to be deleted and withdrawn
926 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
928 struct bgp_path_info
*old_local
)
930 struct bgp_node
*global_rn
;
931 struct bgp_path_info
*pi
;
932 afi_t afi
= AFI_L2VPN
;
933 safi_t safi
= SAFI_EVPN
;
935 /* Locate route node in the global EVPN routing table. Note that
936 * this table is a 2-level tree (RD-level + Prefix-level) similar to
939 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
940 (struct prefix
*)&rn
->p
, &vpn
->prd
);
942 /* Delete route entry in the global EVPN table. */
943 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
945 /* Schedule for processing - withdraws to peers happen from
949 bgp_process(bgp
, global_rn
, afi
, safi
);
950 bgp_unlock_node(global_rn
);
953 /* Delete route entry in the VNI route table, caller to remove. */
954 bgp_path_info_delete(rn
, old_local
);
957 static struct in_addr
*es_vtep_new(struct in_addr vtep
)
961 ip
= XCALLOC(MTYPE_BGP_EVPN_ES_VTEP
, sizeof(struct in_addr
));
965 ip
->s_addr
= vtep
.s_addr
;
969 static void es_vtep_free(struct in_addr
*ip
)
971 XFREE(MTYPE_BGP_EVPN_ES_VTEP
, ip
);
974 /* check if VTEP is already part of the list */
975 static int is_vtep_present_in_list(struct list
*list
,
978 struct listnode
*node
= NULL
;
981 for (ALL_LIST_ELEMENTS_RO(list
, node
, tmp
)) {
982 if (tmp
->s_addr
== vtep
.s_addr
)
989 * Best path for ES route was changed,
990 * update the list of VTEPs for this ES
992 static int evpn_es_install_vtep(struct bgp
*bgp
,
994 struct prefix_evpn
*p
,
995 struct in_addr rvtep
)
997 struct in_addr
*vtep_ip
;
999 if (is_vtep_present_in_list(es
->vtep_list
, rvtep
))
1003 vtep_ip
= es_vtep_new(rvtep
);
1005 listnode_add_sort(es
->vtep_list
, vtep_ip
);
1010 * Best path for ES route was changed,
1011 * update the list of VTEPs for this ES
1013 static int evpn_es_uninstall_vtep(struct bgp
*bgp
,
1015 struct prefix_evpn
*p
,
1016 struct in_addr rvtep
)
1018 struct listnode
*node
, *nnode
, *node_to_del
= NULL
;
1019 struct in_addr
*tmp
;
1021 for (ALL_LIST_ELEMENTS(es
->vtep_list
, node
, nnode
, tmp
)) {
1022 if (tmp
->s_addr
== rvtep
.s_addr
) {
1029 list_delete_node(es
->vtep_list
, node_to_del
);
1035 * Calculate the best path for a ES(type-4) route.
1037 static int evpn_es_route_select_install(struct bgp
*bgp
,
1039 struct bgp_node
*rn
)
1042 afi_t afi
= AFI_L2VPN
;
1043 safi_t safi
= SAFI_EVPN
;
1044 struct bgp_path_info
*old_select
; /* old best */
1045 struct bgp_path_info
*new_select
; /* new best */
1046 struct bgp_path_info_pair old_and_new
;
1048 /* Compute the best path. */
1049 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
],
1050 &old_and_new
, afi
, safi
);
1051 old_select
= old_and_new
.old
;
1052 new_select
= old_and_new
.new;
1055 * If the best path hasn't changed - see if something needs to be
1058 if (old_select
&& old_select
== new_select
1059 && old_select
->type
== ZEBRA_ROUTE_BGP
1060 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1061 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1062 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1063 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1064 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1065 ret
= evpn_es_install_vtep(bgp
, es
,
1066 (struct prefix_evpn
*)&rn
->p
,
1067 old_select
->attr
->nexthop
);
1069 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1070 bgp_zebra_clear_route_change_flags(rn
);
1074 /* If the user did a "clear" this flag will be set */
1075 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1078 * bestpath has changed; update relevant fields and install or uninstall
1079 * into the zebra RIB.
1081 if (old_select
|| new_select
)
1082 bgp_bump_version(rn
);
1085 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1087 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1088 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1089 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1092 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1093 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1094 ret
= evpn_es_install_vtep(bgp
, es
,
1095 (struct prefix_evpn
*)&rn
->p
,
1096 new_select
->attr
->nexthop
);
1098 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1099 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1100 ret
= evpn_es_uninstall_vtep(
1101 bgp
, es
, (struct prefix_evpn
*)&rn
->p
,
1102 old_select
->attr
->nexthop
);
1105 /* Clear any route change flags. */
1106 bgp_zebra_clear_route_change_flags(rn
);
1108 /* Reap old select bgp_path_info, if it has been removed */
1109 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1110 bgp_path_info_reap(rn
, old_select
);
1116 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1119 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1120 struct bgp_node
*rn
)
1122 struct bgp_path_info
*old_select
, *new_select
;
1123 struct bgp_path_info_pair old_and_new
;
1124 struct prefix_evpn
*evp
;
1125 afi_t afi
= AFI_L2VPN
;
1126 safi_t safi
= SAFI_EVPN
;
1130 /* Compute the best path. */
1131 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1133 old_select
= old_and_new
.old
;
1134 new_select
= old_and_new
.new;
1136 evp
= (struct prefix_evpn
*)&rn
->p
;
1137 /* If the best path hasn't changed - see if there is still something to
1141 if (old_select
&& old_select
== new_select
1142 && old_select
->type
== ZEBRA_ROUTE_BGP
1143 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1144 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1145 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1146 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1147 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1148 if (old_select
->attr
->sticky
)
1149 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
1150 if (old_select
->attr
->default_gw
)
1151 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
1152 if (is_evpn_prefix_ipaddr_v6(evp
) &&
1153 old_select
->attr
->router_flag
)
1154 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
1156 ret
= evpn_zebra_install(
1157 bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1158 old_select
->attr
->nexthop
, flags
,
1159 mac_mobility_seqnum(old_select
->attr
));
1161 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1162 bgp_zebra_clear_route_change_flags(rn
);
1166 /* If the user did a "clear" this flag will be set */
1167 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1169 /* bestpath has changed; update relevant fields and install or uninstall
1170 * into the zebra RIB.
1172 if (old_select
|| new_select
)
1173 bgp_bump_version(rn
);
1176 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1178 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1179 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1180 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1183 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1184 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1186 if (new_select
->attr
->sticky
)
1187 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
1188 if (new_select
->attr
->default_gw
)
1189 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
1190 if (is_evpn_prefix_ipaddr_v6(evp
) &&
1191 new_select
->attr
->router_flag
)
1192 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
1194 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1195 new_select
->attr
->nexthop
, flags
,
1196 mac_mobility_seqnum(new_select
->attr
));
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
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
) {
1236 if (tmp_pi
->peer
== bgp
->peer_self
1237 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1238 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1245 return local_pi
->attr
->default_gw
;
1250 * Return true if the local ri for this rn has sticky set
1252 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
1254 struct bgp_path_info
*tmp_pi
;
1255 struct bgp_path_info
*local_pi
;
1258 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
) {
1259 if (tmp_pi
->peer
== bgp
->peer_self
1260 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1261 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1268 return local_pi
->attr
->sticky
;
1272 * create or update EVPN type4 route entry.
1273 * This could be in the ES table or the global table.
1274 * TODO: handle remote ES (type4) routes as well
1276 static int update_evpn_type4_route_entry(struct bgp
*bgp
, struct evpnes
*es
,
1277 afi_t afi
, safi_t safi
,
1278 struct bgp_node
*rn
, struct attr
*attr
,
1279 int add
, struct bgp_path_info
**ri
,
1282 char buf
[ESI_STR_LEN
];
1283 char buf1
[INET6_ADDRSTRLEN
];
1284 struct bgp_path_info
*tmp_pi
= NULL
;
1285 struct bgp_path_info
*local_pi
= NULL
; /* local route entry if any */
1286 struct bgp_path_info
*remote_pi
= NULL
; /* remote route entry if any */
1287 struct attr
*attr_new
= NULL
;
1288 struct prefix_evpn
*evp
= NULL
;
1292 evp
= (struct prefix_evpn
*)&rn
->p
;
1294 /* locate the local and remote entries if any */
1295 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
) {
1296 if (tmp_pi
->peer
== bgp
->peer_self
1297 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1298 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1300 if (tmp_pi
->type
== ZEBRA_ROUTE_BGP
1301 && tmp_pi
->sub_type
== BGP_ROUTE_IMPORTED
1302 && CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1306 /* we don't expect to see a remote_pi at this point.
1307 * An ES route has esi + vtep_ip as the key,
1308 * We shouldn't see the same route from any other vtep.
1313 "%u ERROR: local es route for ESI: %s Vtep %s also learnt from remote",
1315 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1316 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1320 if (!local_pi
&& !add
)
1323 /* create or update the entry */
1326 /* Add or update attribute to hash */
1327 attr_new
= bgp_attr_intern(attr
);
1329 /* Create new route with its attribute. */
1330 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1331 bgp
->peer_self
, attr_new
, rn
);
1332 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1334 /* add the newly created path to the route-node */
1335 bgp_path_info_add(rn
, tmp_pi
);
1338 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1339 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1342 /* The attribute has changed.
1343 * Add (or update) attribute to hash. */
1344 attr_new
= bgp_attr_intern(attr
);
1345 bgp_path_info_set_flag(rn
, tmp_pi
,
1346 BGP_PATH_ATTR_CHANGED
);
1348 /* Restore route, if needed. */
1349 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1350 bgp_path_info_restore(rn
, tmp_pi
);
1352 /* Unintern existing, set to new. */
1353 bgp_attr_unintern(&tmp_pi
->attr
);
1354 tmp_pi
->attr
= attr_new
;
1355 tmp_pi
->uptime
= bgp_clock();
1359 /* Return back the route entry. */
1364 /* update evpn es (type-4) route */
1365 static int update_evpn_type4_route(struct bgp
*bgp
,
1367 struct prefix_evpn
*p
)
1370 int route_changed
= 0;
1371 char buf
[ESI_STR_LEN
];
1372 char buf1
[INET6_ADDRSTRLEN
];
1373 afi_t afi
= AFI_L2VPN
;
1374 safi_t safi
= SAFI_EVPN
;
1376 struct attr
*attr_new
= NULL
;
1377 struct bgp_node
*rn
= NULL
;
1378 struct bgp_path_info
*pi
= NULL
;
1380 memset(&attr
, 0, sizeof(struct attr
));
1382 /* Build path-attribute for this route. */
1383 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1384 attr
.nexthop
= es
->originator_ip
.ipaddr_v4
;
1385 attr
.mp_nexthop_global_in
= es
->originator_ip
.ipaddr_v4
;
1386 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1388 /* Set up extended community. */
1389 build_evpn_type4_route_extcomm(es
, &attr
);
1391 /* First, create (or fetch) route node within the ESI. */
1392 /* NOTE: There is no RD here. */
1393 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
1395 /* Create or update route entry. */
1396 ret
= update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, &attr
, 1,
1397 &pi
, &route_changed
);
1399 flog_err(EC_BGP_ES_INVALID
,
1400 "%u ERROR: Failed to updated ES route ESI: %s VTEP %s",
1402 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1403 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1407 attr_new
= pi
->attr
;
1409 /* Perform route selection;
1410 * this is just to set the flags correctly
1411 * as local route in the ES always wins.
1413 evpn_es_route_select_install(bgp
, es
, rn
);
1414 bgp_unlock_node(rn
);
1416 /* If this is a new route or some attribute has changed, export the
1417 * route to the global table. The route will be advertised to peers
1418 * from there. Note that this table is a 2-level tree (RD-level +
1419 * Prefix-level) similar to L3VPN routes.
1421 if (route_changed
) {
1422 struct bgp_path_info
*global_pi
;
1424 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1425 (struct prefix
*)p
, &es
->prd
);
1426 update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, attr_new
,
1427 1, &global_pi
, &route_changed
);
1429 /* Schedule for processing and unlock node. */
1430 bgp_process(bgp
, rn
, afi
, safi
);
1431 bgp_unlock_node(rn
);
1434 /* Unintern temporary. */
1435 aspath_unintern(&attr
.aspath
);
1439 static int update_evpn_type5_route_entry(struct bgp
*bgp_def
,
1440 struct bgp
*bgp_vrf
, afi_t afi
,
1441 safi_t safi
, struct bgp_node
*rn
,
1442 struct attr
*attr
, int *route_changed
)
1444 struct attr
*attr_new
= NULL
;
1445 struct bgp_path_info
*pi
= NULL
;
1446 mpls_label_t label
= MPLS_INVALID_LABEL
;
1447 struct bgp_path_info
*local_pi
= NULL
;
1448 struct bgp_path_info
*tmp_pi
= NULL
;
1451 /* locate the local route entry if any */
1452 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
) {
1453 if (tmp_pi
->peer
== bgp_def
->peer_self
1454 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1455 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1460 * create a new route entry if one doesn't exist.
1461 * Otherwise see if route attr has changed
1465 /* route has changed as this is the first entry */
1468 /* Add (or update) attribute to hash. */
1469 attr_new
= bgp_attr_intern(attr
);
1471 /* create the route info from attribute */
1472 pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1473 bgp_def
->peer_self
, attr_new
, rn
);
1474 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
1476 /* Type-5 routes advertise the L3-VNI */
1477 bgp_path_info_extra_get(pi
);
1478 vni2label(bgp_vrf
->l3vni
, &label
);
1479 memcpy(&pi
->extra
->label
, &label
, sizeof(label
));
1480 pi
->extra
->num_labels
= 1;
1482 /* add the route entry to route node*/
1483 bgp_path_info_add(rn
, pi
);
1487 if (!attrhash_cmp(tmp_pi
->attr
, attr
)) {
1489 /* attribute changed */
1492 /* The attribute has changed. */
1493 /* Add (or update) attribute to hash. */
1494 attr_new
= bgp_attr_intern(attr
);
1495 bgp_path_info_set_flag(rn
, tmp_pi
,
1496 BGP_PATH_ATTR_CHANGED
);
1498 /* Restore route, if needed. */
1499 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1500 bgp_path_info_restore(rn
, tmp_pi
);
1502 /* Unintern existing, set to new. */
1503 bgp_attr_unintern(&tmp_pi
->attr
);
1504 tmp_pi
->attr
= attr_new
;
1505 tmp_pi
->uptime
= bgp_clock();
1511 /* update evpn type-5 route entry */
1512 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1513 struct attr
*src_attr
)
1515 afi_t afi
= AFI_L2VPN
;
1516 safi_t safi
= SAFI_EVPN
;
1518 struct bgp_node
*rn
= NULL
;
1519 struct bgp
*bgp_def
= NULL
;
1520 int route_changed
= 0;
1522 bgp_def
= bgp_get_default();
1526 /* Build path attribute for this route - use the source attr, if
1527 * present, else treat as locally originated.
1530 bgp_attr_dup(&attr
, src_attr
);
1532 memset(&attr
, 0, sizeof(struct attr
));
1533 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1535 /* Set nexthop to ourselves and fill in the Router MAC. */
1536 attr
.nexthop
= bgp_vrf
->originator_ip
;
1537 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1538 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1539 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
1541 /* Setup RT and encap extended community */
1542 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1544 /* get the route node in global table */
1545 rn
= bgp_afi_node_get(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1546 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1549 /* create or update the route entry within the route node */
1550 update_evpn_type5_route_entry(bgp_def
, bgp_vrf
, afi
, safi
, rn
, &attr
,
1553 /* schedule for processing and unlock node */
1554 if (route_changed
) {
1555 bgp_process(bgp_def
, rn
, afi
, safi
);
1556 bgp_unlock_node(rn
);
1559 /* uninten temporary */
1561 aspath_unintern(&attr
.aspath
);
1566 * Create or update EVPN route entry. This could be in the VNI route table
1567 * or the global route table.
1569 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1570 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1571 struct attr
*attr
, int add
,
1572 struct bgp_path_info
**pi
, uint8_t flags
,
1575 struct bgp_path_info
*tmp_pi
;
1576 struct bgp_path_info
*local_pi
;
1577 struct attr
*attr_new
;
1578 mpls_label_t label
[BGP_MAX_LABELS
];
1579 uint32_t num_labels
= 1;
1580 int route_change
= 1;
1582 struct prefix_evpn
*evp
;
1585 evp
= (struct prefix_evpn
*)&rn
->p
;
1586 memset(&label
, 0, sizeof(label
));
1588 /* See if this is an update of an existing route, or a new add. */
1590 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
) {
1591 if (tmp_pi
->peer
== bgp
->peer_self
1592 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1593 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1597 /* If route doesn't exist already, create a new one, if told to.
1598 * Otherwise act based on whether the attributes of the route have
1601 if (!local_pi
&& !add
)
1604 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1605 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1606 add_mac_mobility_to_attr(seq
, attr
);
1609 /* Add (or update) attribute to hash. */
1610 attr_new
= bgp_attr_intern(attr
);
1612 /* Extract MAC mobility sequence number, if any. */
1613 attr_new
->mm_seqnum
=
1614 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1615 attr_new
->sticky
= sticky
;
1617 /* Create new route with its attribute. */
1618 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1619 bgp
->peer_self
, attr_new
, rn
);
1620 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1621 bgp_path_info_extra_get(tmp_pi
);
1623 /* The VNI goes into the 'label' field of the route */
1624 vni2label(vpn
->vni
, &label
[0]);
1626 /* Type-2 routes may carry a second VNI - the L3-VNI.
1627 * Only attach second label if we are advertising two labels for
1630 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1631 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1634 l3vni
= bgpevpn_get_l3vni(vpn
);
1636 vni2label(l3vni
, &label
[1]);
1641 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1642 tmp_pi
->extra
->num_labels
= num_labels
;
1643 bgp_path_info_add(rn
, tmp_pi
);
1646 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1647 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1651 * The attributes have changed, type-2 routes needs to
1652 * be advertised with right labels.
1654 vni2label(vpn
->vni
, &label
[0]);
1655 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1656 && CHECK_FLAG(vpn
->flags
,
1657 VNI_FLAG_USE_TWO_LABELS
)) {
1660 l3vni
= bgpevpn_get_l3vni(vpn
);
1662 vni2label(l3vni
, &label
[1]);
1666 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1667 tmp_pi
->extra
->num_labels
= num_labels
;
1669 /* The attribute has changed. */
1670 /* Add (or update) attribute to hash. */
1671 attr_new
= bgp_attr_intern(attr
);
1672 bgp_path_info_set_flag(rn
, tmp_pi
,
1673 BGP_PATH_ATTR_CHANGED
);
1675 /* Extract MAC mobility sequence number, if any. */
1676 attr_new
->mm_seqnum
=
1677 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1678 attr_new
->sticky
= sticky
;
1680 /* Restore route, if needed. */
1681 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1682 bgp_path_info_restore(rn
, tmp_pi
);
1684 /* Unintern existing, set to new. */
1685 bgp_attr_unintern(&tmp_pi
->attr
);
1686 tmp_pi
->attr
= attr_new
;
1687 tmp_pi
->uptime
= bgp_clock();
1691 /* Return back the route entry. */
1693 return route_change
;
1697 * If the local route was not selected evict it and tell zebra to re-add
1698 * the best remote dest.
1700 * Typically a local path added by zebra is expected to be selected as
1701 * best. In which case when a remote path wins as best (later)
1702 * evpn_route_select_install itself evicts the older-local-best path.
1704 * However if bgp's add and zebra's add cross paths (race condition) it
1705 * is possible that the local path is no longer the "older" best path.
1706 * It is a path that was never designated as best and hence requires
1707 * additional handling to prevent bgp from injecting and holding on to a
1708 * non-best local path.
1710 static void evpn_cleanup_local_non_best_route(struct bgp
*bgp
,
1711 struct bgpevpn
*vpn
,
1712 struct bgp_node
*rn
,
1713 struct bgp_path_info
*local_pi
)
1715 struct bgp_path_info
*tmp_pi
;
1716 struct bgp_path_info
*curr_select
= NULL
;
1718 char buf
[PREFIX_STRLEN
];
1720 /* local path was not picked as the winner; kick it out */
1721 if (bgp_debug_zebra(NULL
)) {
1722 zlog_debug("evicting local evpn prefix %s as remote won",
1723 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
1725 evpn_delete_old_local_route(bgp
, vpn
, rn
, local_pi
);
1726 bgp_path_info_reap(rn
, local_pi
);
1728 /* tell zebra to re-add the best remote path */
1729 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
) {
1730 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_SELECTED
)) {
1731 curr_select
= tmp_pi
;
1736 curr_select
->type
== ZEBRA_ROUTE_BGP
1737 && curr_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1738 if (curr_select
->attr
->sticky
)
1739 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
1740 if (curr_select
->attr
->default_gw
)
1741 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
1742 evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1743 curr_select
->attr
->nexthop
, flags
,
1744 mac_mobility_seqnum(curr_select
->attr
));
1749 * Create or update EVPN route (of type based on prefix) for specified VNI
1750 * and schedule for processing.
1752 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1753 struct prefix_evpn
*p
, uint8_t flags
,
1756 struct bgp_node
*rn
;
1758 struct attr
*attr_new
;
1759 int add_l3_ecomm
= 0;
1760 struct bgp_path_info
*pi
;
1761 afi_t afi
= AFI_L2VPN
;
1762 safi_t safi
= SAFI_EVPN
;
1765 memset(&attr
, 0, sizeof(struct attr
));
1767 /* Build path-attribute for this route. */
1768 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1769 attr
.nexthop
= vpn
->originator_ip
;
1770 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1771 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1772 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1773 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1774 attr
.router_flag
= CHECK_FLAG(flags
,
1775 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1777 /* PMSI is only needed for type-3 routes */
1778 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
)
1779 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1781 /* router mac is only needed for type-2 routes here. */
1782 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1783 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1784 vni2label(vpn
->vni
, &(attr
.label
));
1786 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1787 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1790 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1791 (is_evpn_prefix_ipaddr_v4(p
) ||
1792 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1793 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1794 bgpevpn_get_l3vni(vpn
))
1797 /* Set up extended community. */
1798 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1800 /* First, create (or fetch) route node within the VNI. */
1801 /* NOTE: There is no RD here. */
1802 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1804 /* Create or update route entry. */
1805 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1806 1, &pi
, flags
, seq
);
1808 attr_new
= pi
->attr
;
1810 /* lock ri to prevent freeing in evpn_route_select_install */
1811 bgp_path_info_lock(pi
);
1812 /* Perform route selection; this is just to set the flags correctly
1813 * as local route in the VNI always wins.
1815 evpn_route_select_install(bgp
, vpn
, rn
);
1817 * If the new local route was not selected evict it and tell zebra
1818 * to re-add the best remote dest. BGP doesn't retain non-best local
1821 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1823 evpn_cleanup_local_non_best_route(bgp
, vpn
, rn
, pi
);
1825 bgp_path_info_unlock(pi
);
1827 bgp_unlock_node(rn
);
1829 /* If this is a new route or some attribute has changed, export the
1830 * route to the global table. The route will be advertised to peers
1831 * from there. Note that this table is a 2-level tree (RD-level +
1832 * Prefix-level) similar to L3VPN routes.
1835 struct bgp_path_info
*global_pi
;
1837 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1838 (struct prefix
*)p
, &vpn
->prd
);
1839 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1,
1840 &global_pi
, flags
, seq
);
1842 /* Schedule for processing and unlock node. */
1843 bgp_process(bgp
, rn
, afi
, safi
);
1844 bgp_unlock_node(rn
);
1847 /* Unintern temporary. */
1848 aspath_unintern(&attr
.aspath
);
1854 * Delete EVPN route entry.
1855 * The entry can be in ESI/VNI table or the global table.
1857 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1858 struct bgp_node
*rn
,
1859 struct bgp_path_info
**pi
)
1861 struct bgp_path_info
*tmp_pi
;
1865 /* Now, find matching route. */
1866 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
)
1867 if (tmp_pi
->peer
== bgp
->peer_self
1868 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1869 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1874 /* Mark route for delete. */
1876 bgp_path_info_delete(rn
, tmp_pi
);
1881 /* Delete EVPN ES (type-4) route */
1882 static int delete_evpn_type4_route(struct bgp
*bgp
,
1884 struct prefix_evpn
*p
)
1886 afi_t afi
= AFI_L2VPN
;
1887 safi_t safi
= SAFI_EVPN
;
1888 struct bgp_path_info
*pi
;
1889 struct bgp_node
*rn
= NULL
; /* rn in esi table */
1890 struct bgp_node
*global_rn
= NULL
; /* rn in global table */
1892 /* First, locate the route node within the ESI.
1893 * If it doesn't exist, ther is nothing to do.
1894 * Note: there is no RD here.
1896 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
1900 /* Next, locate route node in the global EVPN routing table.
1901 * Note that this table is a 2-level tree (RD-level + Prefix-level)
1903 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1904 (struct prefix
*)p
, &es
->prd
);
1907 /* Delete route entry in the global EVPN table. */
1908 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1910 /* Schedule for processing - withdraws to peers happen from
1914 bgp_process(bgp
, global_rn
, afi
, safi
);
1915 bgp_unlock_node(global_rn
);
1919 * Delete route entry in the ESI route table.
1920 * This can just be removed.
1922 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
1924 bgp_path_info_reap(rn
, pi
);
1925 bgp_unlock_node(rn
);
1929 /* Delete EVPN type5 route */
1930 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1932 afi_t afi
= AFI_L2VPN
;
1933 safi_t safi
= SAFI_EVPN
;
1934 struct bgp_node
*rn
= NULL
;
1935 struct bgp_path_info
*pi
= NULL
;
1936 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
1938 bgp_def
= bgp_get_default();
1942 /* locate the global route entry for this type-5 prefix */
1943 rn
= bgp_afi_node_lookup(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1944 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1948 delete_evpn_route_entry(bgp_def
, afi
, safi
, rn
, &pi
);
1950 bgp_process(bgp_def
, rn
, afi
, safi
);
1951 bgp_unlock_node(rn
);
1956 * Delete EVPN route (of type based on prefix) for specified VNI and
1957 * schedule for processing.
1959 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1960 struct prefix_evpn
*p
)
1962 struct bgp_node
*rn
, *global_rn
;
1963 struct bgp_path_info
*pi
;
1964 afi_t afi
= AFI_L2VPN
;
1965 safi_t safi
= SAFI_EVPN
;
1967 /* First, locate the route node within the VNI. If it doesn't exist,
1969 * is nothing further to do.
1971 /* NOTE: There is no RD here. */
1972 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1976 /* Next, locate route node in the global EVPN routing table. Note that
1977 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1980 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1981 (struct prefix
*)p
, &vpn
->prd
);
1983 /* Delete route entry in the global EVPN table. */
1984 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1986 /* Schedule for processing - withdraws to peers happen from
1990 bgp_process(bgp
, global_rn
, afi
, safi
);
1991 bgp_unlock_node(global_rn
);
1994 /* Delete route entry in the VNI route table. This can just be removed.
1996 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
1998 bgp_path_info_reap(rn
, pi
);
1999 evpn_route_select_install(bgp
, vpn
, rn
);
2001 bgp_unlock_node(rn
);
2007 * Update all type-2 (MACIP) local routes for this VNI - these should also
2008 * be scheduled for advertise to peers.
2010 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2014 struct bgp_node
*rn
;
2015 struct bgp_path_info
*pi
, *tmp_pi
;
2017 struct attr
*attr_new
;
2019 int add_l3_ecomm
= 0;
2024 /* Walk this VNI's route table and update local type-2 routes. For any
2025 * routes updated, update corresponding entry in the global table too.
2027 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2028 rn
= bgp_route_next(rn
)) {
2029 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2030 struct bgp_node
*rd_rn
;
2031 struct bgp_path_info
*global_pi
;
2033 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2036 /* Identify local route. */
2037 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
) {
2038 if (tmp_pi
->peer
== bgp
->peer_self
2039 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2040 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2048 * Build attribute per local route as the MAC mobility and
2049 * some other values could differ for different routes. The
2050 * attributes will be shared in the hash table.
2052 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
2053 attr
.nexthop
= vpn
->originator_ip
;
2054 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
2055 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
2056 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
2058 if (evpn_route_is_sticky(bgp
, rn
))
2060 else if (evpn_route_is_def_gw(bgp
, rn
)) {
2061 attr
.default_gw
= 1;
2062 if (is_evpn_prefix_ipaddr_v6(evp
))
2063 attr
.router_flag
= 1;
2066 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2067 * using L3 VNI for type-2 routes also.
2069 if ((is_evpn_prefix_ipaddr_v4(evp
) ||
2070 !IN6_IS_ADDR_LINKLOCAL(
2071 &evp
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
2072 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
2073 bgpevpn_get_l3vni(vpn
))
2076 /* Set up extended community. */
2077 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2079 seq
= mac_mobility_seqnum(tmp_pi
->attr
);
2081 /* Update the route entry. */
2082 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
, 0, &pi
,
2085 /* Perform route selection; this is just to set the flags
2086 * correctly as local route in the VNI always wins.
2088 evpn_route_select_install(bgp
, vpn
, rn
);
2090 attr_new
= pi
->attr
;
2092 /* Update route in global routing table. */
2093 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2094 (struct prefix
*)evp
, &vpn
->prd
);
2096 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
2098 mac_mobility_seqnum(attr_new
));
2100 /* Schedule for processing and unlock node. */
2101 bgp_process(bgp
, rd_rn
, afi
, safi
);
2102 bgp_unlock_node(rd_rn
);
2104 /* Unintern temporary. */
2105 aspath_unintern(&attr
.aspath
);
2113 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2114 * global routing table. These are also scheduled for withdraw from peers.
2116 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2120 struct bgp_node
*rdrn
, *rn
;
2121 struct bgp_table
*table
;
2122 struct bgp_path_info
*pi
;
2127 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
2128 if (rdrn
&& rdrn
->info
) {
2129 table
= (struct bgp_table
*)rdrn
->info
;
2130 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2131 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2133 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2136 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2138 bgp_process(bgp
, rn
, afi
, safi
);
2142 /* Unlock RD node. */
2144 bgp_unlock_node(rdrn
);
2150 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2151 * table as well as the per-VNI route table.
2153 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2157 struct bgp_node
*rn
;
2158 struct bgp_path_info
*pi
;
2163 /* First, walk the global route table for this VNI's type-2 local
2165 * EVPN routes are a 2-level table, first get the RD table.
2167 delete_global_type2_routes(bgp
, vpn
);
2169 /* Next, walk this VNI's route table and delete local type-2 routes. */
2170 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2171 rn
= bgp_route_next(rn
)) {
2172 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2174 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2177 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2179 /* Route entry in local table gets deleted immediately. */
2181 bgp_path_info_reap(rn
, pi
);
2188 * Delete all routes in per ES route-table
2190 static int delete_all_es_routes(struct bgp
*bgp
, struct evpnes
*es
)
2192 struct bgp_node
*rn
;
2193 struct bgp_path_info
*pi
, *nextpi
;
2195 /* Walk this ES's route table and delete all routes. */
2196 for (rn
= bgp_table_top(es
->route_table
); rn
;
2197 rn
= bgp_route_next(rn
)) {
2198 for (pi
= rn
->info
; (pi
!= NULL
) && (nextpi
= pi
->next
, 1);
2200 bgp_path_info_delete(rn
, pi
);
2201 bgp_path_info_reap(rn
, pi
);
2209 * Delete all routes in the per-VNI route table.
2211 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2213 struct bgp_node
*rn
;
2214 struct bgp_path_info
*pi
, *nextpi
;
2216 /* Walk this VNI's route table and delete all routes. */
2217 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2218 rn
= bgp_route_next(rn
)) {
2219 for (pi
= rn
->info
; (pi
!= NULL
) && (nextpi
= pi
->next
, 1);
2221 bgp_path_info_delete(rn
, pi
);
2222 bgp_path_info_reap(rn
, pi
);
2230 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2231 * export RT getting modified or change to tunnel IP. Note that these
2232 * situations need the route in the per-VNI table as well as the global
2233 * table to be updated (as attributes change).
2235 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2238 struct prefix_evpn p
;
2240 /* Update and advertise the type-3 route (only one) followed by the
2241 * locally learnt type-2 routes (MACIP) - for this VNI.
2243 * RT-3 only if doing head-end replication
2245 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
2246 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2247 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0);
2252 return update_all_type2_routes(bgp
, vpn
);
2255 /* Delete (and withdraw) local routes for specified ES from global and ES table.
2256 * Also remove all other routes from the per ES table.
2257 * Invoked when ES is deleted.
2259 static int delete_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
2262 char buf
[ESI_STR_LEN
];
2263 struct prefix_evpn p
;
2265 /* Delete and withdraw locally learnt ES route */
2266 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
.ipaddr_v4
);
2267 ret
= delete_evpn_type4_route(bgp
, es
, &p
);
2269 flog_err(EC_BGP_EVPN_ROUTE_DELETE
,
2270 "%u failed to delete type-4 route for ESI %s",
2271 bgp
->vrf_id
, esi_to_str(&es
->esi
, buf
, sizeof(buf
)));
2274 /* Delete all routes from per ES table */
2275 return delete_all_es_routes(bgp
, es
);
2279 * Delete (and withdraw) local routes for specified VNI from the global
2280 * table and per-VNI table. After this, remove all other routes from
2281 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2282 * (advertise-all-vni) being disabled.
2284 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2287 struct prefix_evpn p
;
2289 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2290 * followed by type-3 routes (only one) - for this VNI.
2292 ret
= delete_all_type2_routes(bgp
, vpn
);
2296 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2297 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2301 /* Delete all routes from the per-VNI table. */
2302 return delete_all_vni_routes(bgp
, vpn
);
2306 * There is a tunnel endpoint IP address change for this VNI, delete
2307 * prior type-3 route (if needed) and update.
2308 * Note: Route re-advertisement happens elsewhere after other processing
2311 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2312 struct in_addr originator_ip
)
2314 struct prefix_evpn p
;
2316 /* If VNI is not live, we only need to update the originator ip */
2317 if (!is_vni_live(vpn
)) {
2318 vpn
->originator_ip
= originator_ip
;
2322 /* Update the tunnel-ip hash */
2323 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2324 bgp_tip_add(bgp
, &originator_ip
);
2326 /* filter routes as martian nexthop db has changed */
2327 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2329 /* Need to withdraw type-3 route as the originator IP is part
2332 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2333 delete_evpn_route(bgp
, vpn
, &p
);
2335 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2336 vpn
->originator_ip
= originator_ip
;
2340 /* Install EVPN route entry in ES */
2341 static int install_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2342 struct prefix_evpn
*p
,
2343 struct bgp_path_info
*parent_pi
)
2346 struct bgp_node
*rn
= NULL
;
2347 struct bgp_path_info
*pi
= NULL
;
2348 struct attr
*attr_new
= NULL
;
2350 /* Create (or fetch) route within the VNI.
2351 * NOTE: There is no RD here.
2353 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
2355 /* Check if route entry is already present. */
2356 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
2358 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2362 /* Add (or update) attribute to hash. */
2363 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2365 /* Create new route with its attribute. */
2366 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2367 parent_pi
->peer
, attr_new
, rn
);
2368 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2369 bgp_path_info_extra_get(pi
);
2370 pi
->extra
->parent
= parent_pi
;
2371 bgp_path_info_add(rn
, pi
);
2373 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2374 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2375 bgp_unlock_node(rn
);
2378 /* The attribute has changed. */
2379 /* Add (or update) attribute to hash. */
2380 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2382 /* Restore route, if needed. */
2383 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2384 bgp_path_info_restore(rn
, pi
);
2386 /* Mark if nexthop has changed. */
2387 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2388 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2390 /* Unintern existing, set to new. */
2391 bgp_attr_unintern(&pi
->attr
);
2392 pi
->attr
= attr_new
;
2393 pi
->uptime
= bgp_clock();
2396 /* Perform route selection and update zebra, if required. */
2397 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2402 * Install route entry into the VRF routing table and invoke route selection.
2404 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2405 struct prefix_evpn
*evp
,
2406 struct bgp_path_info
*parent_pi
)
2408 struct bgp_node
*rn
;
2409 struct bgp_path_info
*pi
;
2411 struct attr
*attr_new
;
2414 struct prefix
*pp
= &p
;
2417 char buf
[PREFIX_STRLEN
];
2418 char buf1
[PREFIX_STRLEN
];
2420 memset(pp
, 0, sizeof(struct prefix
));
2421 ip_prefix_from_evpn_prefix(evp
, pp
);
2423 if (bgp_debug_zebra(NULL
)) {
2425 "installing evpn prefix %s as ip prefix %s in vrf %s",
2426 prefix2str(evp
, buf
, sizeof(buf
)),
2427 prefix2str(pp
, buf1
, sizeof(buf
)),
2428 vrf_id_to_name(bgp_vrf
->vrf_id
));
2431 /* Create (or fetch) route within the VRF. */
2432 /* NOTE: There is no RD here. */
2433 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2435 safi
= SAFI_UNICAST
;
2436 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2437 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2439 safi
= SAFI_UNICAST
;
2440 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2444 /* EVPN routes currently only support a IPv4 next hop which corresponds
2445 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2446 * or prefix route, we have to convert the next hop to an IPv4-mapped
2447 * address for the rest of the code to flow through. In the case of IPv4,
2448 * make sure to set the flag for next hop attribute.
2450 bgp_attr_dup(&attr
, parent_pi
->attr
);
2452 evpn_convert_nexthop_to_ipv6(&attr
);
2454 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2456 /* Check if route entry is already present. */
2457 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
2459 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2463 /* Add (or update) attribute to hash. */
2464 attr_new
= bgp_attr_intern(&attr
);
2466 /* Create new route with its attribute. */
2467 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2468 parent_pi
->peer
, attr_new
, rn
);
2469 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2470 bgp_path_info_extra_get(pi
);
2471 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2472 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2473 if (parent_pi
->extra
) {
2474 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2475 sizeof(pi
->extra
->label
));
2476 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2478 bgp_path_info_add(rn
, pi
);
2480 if (attrhash_cmp(pi
->attr
, &attr
)
2481 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2482 bgp_unlock_node(rn
);
2485 /* The attribute has changed. */
2486 /* Add (or update) attribute to hash. */
2487 attr_new
= bgp_attr_intern(&attr
);
2489 /* Restore route, if needed. */
2490 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2491 bgp_path_info_restore(rn
, pi
);
2493 /* Mark if nexthop has changed. */
2495 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2497 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2498 &attr_new
->mp_nexthop_global
)))
2499 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2501 /* Unintern existing, set to new. */
2502 bgp_attr_unintern(&pi
->attr
);
2503 pi
->attr
= attr_new
;
2504 pi
->uptime
= bgp_clock();
2507 bgp_aggregate_increment(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2509 /* Perform route selection and update zebra, if required. */
2510 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2516 * Install route entry into the VNI routing table and invoke route selection.
2518 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2519 struct prefix_evpn
*p
,
2520 struct bgp_path_info
*parent_pi
)
2522 struct bgp_node
*rn
;
2523 struct bgp_path_info
*pi
;
2524 struct attr
*attr_new
;
2527 /* Create (or fetch) route within the VNI. */
2528 /* NOTE: There is no RD here. */
2529 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2531 /* Check if route entry is already present. */
2532 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
2534 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2538 /* Add (or update) attribute to hash. */
2539 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2541 /* Create new route with its attribute. */
2542 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2543 parent_pi
->peer
, attr_new
, rn
);
2544 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2545 bgp_path_info_extra_get(pi
);
2546 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2547 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2548 if (parent_pi
->extra
) {
2549 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2550 sizeof(pi
->extra
->label
));
2551 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2553 bgp_path_info_add(rn
, pi
);
2555 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2556 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2557 bgp_unlock_node(rn
);
2560 /* The attribute has changed. */
2561 /* Add (or update) attribute to hash. */
2562 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2564 /* Restore route, if needed. */
2565 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2566 bgp_path_info_restore(rn
, pi
);
2568 /* Mark if nexthop has changed. */
2569 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2570 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2572 /* Unintern existing, set to new. */
2573 bgp_attr_unintern(&pi
->attr
);
2574 pi
->attr
= attr_new
;
2575 pi
->uptime
= bgp_clock();
2578 /* Perform route selection and update zebra, if required. */
2579 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2584 /* Uninstall EVPN route entry from ES route table */
2585 static int uninstall_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2586 struct prefix_evpn
*p
,
2587 struct bgp_path_info
*parent_pi
)
2590 struct bgp_node
*rn
;
2591 struct bgp_path_info
*pi
;
2593 if (!es
->route_table
)
2596 /* Locate route within the ESI.
2597 * NOTE: There is no RD here.
2599 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2603 /* Find matching route entry. */
2604 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
2606 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2612 /* Mark entry for deletion */
2613 bgp_path_info_delete(rn
, pi
);
2615 /* Perform route selection and update zebra, if required. */
2616 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2618 /* Unlock route node. */
2619 bgp_unlock_node(rn
);
2625 * Uninstall route entry from the VRF routing table and send message
2626 * to zebra, if appropriate.
2628 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2629 struct prefix_evpn
*evp
,
2630 struct bgp_path_info
*parent_pi
)
2632 struct bgp_node
*rn
;
2633 struct bgp_path_info
*pi
;
2636 struct prefix
*pp
= &p
;
2639 char buf
[PREFIX_STRLEN
];
2640 char buf1
[PREFIX_STRLEN
];
2642 memset(pp
, 0, sizeof(struct prefix
));
2643 ip_prefix_from_evpn_prefix(evp
, pp
);
2645 if (bgp_debug_zebra(NULL
)) {
2647 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2648 prefix2str(evp
, buf
, sizeof(buf
)),
2649 prefix2str(pp
, buf1
, sizeof(buf
)),
2650 vrf_id_to_name(bgp_vrf
->vrf_id
));
2653 /* Locate route within the VRF. */
2654 /* NOTE: There is no RD here. */
2655 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2657 safi
= SAFI_UNICAST
;
2658 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2661 safi
= SAFI_UNICAST
;
2662 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2668 /* Find matching route entry. */
2669 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
2671 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2677 bgp_aggregate_decrement(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2679 /* Mark entry for deletion */
2680 bgp_path_info_delete(rn
, pi
);
2682 /* Perform route selection and update zebra, if required. */
2683 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2685 /* Unlock route node. */
2686 bgp_unlock_node(rn
);
2692 * Uninstall route entry from the VNI routing table and send message
2693 * to zebra, if appropriate.
2695 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2696 struct prefix_evpn
*p
,
2697 struct bgp_path_info
*parent_pi
)
2699 struct bgp_node
*rn
;
2700 struct bgp_path_info
*pi
;
2703 /* Locate route within the VNI. */
2704 /* NOTE: There is no RD here. */
2705 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2709 /* Find matching route entry. */
2710 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
2712 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2718 /* Mark entry for deletion */
2719 bgp_path_info_delete(rn
, pi
);
2721 /* Perform route selection and update zebra, if required. */
2722 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2724 /* Unlock route node. */
2725 bgp_unlock_node(rn
);
2731 * Given a prefix, see if it belongs to ES.
2733 static int is_prefix_matching_for_es(struct prefix_evpn
*p
,
2736 /* if not an ES route return false */
2737 if (p
->prefix
.route_type
!= BGP_EVPN_ES_ROUTE
)
2740 if (memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
)) == 0)
2747 * Given a route entry and a VRF, see if this route entry should be
2748 * imported into the VRF i.e., RTs match.
2750 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2751 struct bgp_path_info
*pi
)
2753 struct attr
*attr
= pi
->attr
;
2754 struct ecommunity
*ecom
;
2758 /* Route should have valid RT to be even considered. */
2759 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2762 ecom
= attr
->ecommunity
;
2763 if (!ecom
|| !ecom
->size
)
2766 /* For each extended community RT, see if it matches this VNI. If any RT
2767 * matches, we're done.
2769 for (i
= 0; i
< ecom
->size
; i
++) {
2771 uint8_t type
, sub_type
;
2772 struct ecommunity_val
*eval
;
2773 struct ecommunity_val eval_tmp
;
2774 struct vrf_irt_node
*irt
;
2776 /* Only deal with RTs */
2777 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2778 eval
= (struct ecommunity_val
*)(ecom
->val
2779 + (i
* ECOMMUNITY_SIZE
));
2782 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2785 /* See if this RT matches specified VNIs import RTs */
2786 irt
= lookup_vrf_import_rt(eval
);
2788 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2791 /* Also check for non-exact match. In this, we mask out the AS
2793 * only check on the local-admin sub-field. This is to
2795 * VNI as the RT for EBGP peering too.
2798 if (type
== ECOMMUNITY_ENCODE_AS
2799 || type
== ECOMMUNITY_ENCODE_AS4
2800 || type
== ECOMMUNITY_ENCODE_IP
) {
2801 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2802 mask_ecom_global_admin(&eval_tmp
, eval
);
2803 irt
= lookup_vrf_import_rt(&eval_tmp
);
2806 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2814 * Given a route entry and a VNI, see if this route entry should be
2815 * imported into the VNI i.e., RTs match.
2817 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2818 struct bgp_path_info
*pi
)
2820 struct attr
*attr
= pi
->attr
;
2821 struct ecommunity
*ecom
;
2825 /* Route should have valid RT to be even considered. */
2826 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2829 ecom
= attr
->ecommunity
;
2830 if (!ecom
|| !ecom
->size
)
2833 /* For each extended community RT, see if it matches this VNI. If any RT
2834 * matches, we're done.
2836 for (i
= 0; i
< ecom
->size
; i
++) {
2838 uint8_t type
, sub_type
;
2839 struct ecommunity_val
*eval
;
2840 struct ecommunity_val eval_tmp
;
2841 struct irt_node
*irt
;
2843 /* Only deal with RTs */
2844 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2845 eval
= (struct ecommunity_val
*)(ecom
->val
2846 + (i
* ECOMMUNITY_SIZE
));
2849 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2852 /* See if this RT matches specified VNIs import RTs */
2853 irt
= lookup_import_rt(bgp
, eval
);
2855 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2858 /* Also check for non-exact match. In this, we mask out the AS
2860 * only check on the local-admin sub-field. This is to
2862 * VNI as the RT for EBGP peering too.
2865 if (type
== ECOMMUNITY_ENCODE_AS
2866 || type
== ECOMMUNITY_ENCODE_AS4
2867 || type
== ECOMMUNITY_ENCODE_IP
) {
2868 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2869 mask_ecom_global_admin(&eval_tmp
, eval
);
2870 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2873 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2880 static int install_uninstall_routes_for_es(struct bgp
*bgp
,
2887 char buf
[PREFIX_STRLEN
];
2888 char buf1
[ESI_STR_LEN
];
2889 struct bgp_node
*rd_rn
, *rn
;
2890 struct bgp_table
*table
;
2891 struct bgp_path_info
*pi
;
2897 * Walk entire global routing table and evaluate routes which could be
2898 * imported into this VRF. Note that we need to loop through all global
2899 * routes to determine which route matches the import rt on vrf
2901 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2902 rd_rn
= bgp_route_next(rd_rn
)) {
2903 table
= (struct bgp_table
*)(rd_rn
->info
);
2907 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2908 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2910 for (pi
= rn
->info
; pi
; pi
= pi
->next
) {
2912 * Consider "valid" remote routes applicable for
2915 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
2916 && pi
->type
== ZEBRA_ROUTE_BGP
2917 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
2920 if (!is_prefix_matching_for_es(evp
, es
))
2924 ret
= install_evpn_route_entry_in_es(
2927 ret
= uninstall_evpn_route_entry_in_es(
2933 "Failed to %s EVPN %s route in ESI %s",
2936 prefix2str(evp
, buf
,
2938 esi_to_str(&es
->esi
, buf1
,
2949 * Install or uninstall mac-ip routes are appropriate for this
2952 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
2956 struct bgp_node
*rd_rn
, *rn
;
2957 struct bgp_table
*table
;
2958 struct bgp_path_info
*pi
;
2960 char buf
[PREFIX_STRLEN
];
2961 struct bgp
*bgp_def
= NULL
;
2965 bgp_def
= bgp_get_default();
2969 /* Walk entire global routing table and evaluate routes which could be
2970 * imported into this VRF. Note that we need to loop through all global
2971 * routes to determine which route matches the import rt on vrf
2973 for (rd_rn
= bgp_table_top(bgp_def
->rib
[afi
][safi
]); rd_rn
;
2974 rd_rn
= bgp_route_next(rd_rn
)) {
2975 table
= (struct bgp_table
*)(rd_rn
->info
);
2979 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2980 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2982 /* if not mac-ip route skip this route */
2983 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2984 || evp
->prefix
.route_type
2985 == BGP_EVPN_IP_PREFIX_ROUTE
))
2988 /* if not a mac+ip route skip this route */
2989 if (!(is_evpn_prefix_ipaddr_v4(evp
)
2990 || is_evpn_prefix_ipaddr_v6(evp
)))
2993 for (pi
= rn
->info
; pi
; pi
= pi
->next
) {
2994 /* Consider "valid" remote routes applicable for
2997 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
2998 && pi
->type
== ZEBRA_ROUTE_BGP
2999 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3002 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
3004 ret
= install_evpn_route_entry_in_vrf(
3007 ret
= uninstall_evpn_route_entry_in_vrf(
3013 "Failed to %s EVPN %s route in VRF %s",
3016 prefix2str(evp
, buf
,
3031 * Install or uninstall routes of specified type that are appropriate for this
3034 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
3035 struct bgpevpn
*vpn
,
3036 bgp_evpn_route_type rtype
,
3041 struct bgp_node
*rd_rn
, *rn
;
3042 struct bgp_table
*table
;
3043 struct bgp_path_info
*pi
;
3049 /* Walk entire global routing table and evaluate routes which could be
3050 * imported into this VPN. Note that we cannot just look at the routes
3052 * the VNI's RD - remote routes applicable for this VNI could have any
3055 /* EVPN routes are a 2-level table. */
3056 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
3057 rd_rn
= bgp_route_next(rd_rn
)) {
3058 table
= (struct bgp_table
*)(rd_rn
->info
);
3062 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3063 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3065 if (evp
->prefix
.route_type
!= rtype
)
3068 for (pi
= rn
->info
; pi
; pi
= pi
->next
) {
3069 /* Consider "valid" remote routes applicable for
3071 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3072 && pi
->type
== ZEBRA_ROUTE_BGP
3073 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3076 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3078 ret
= install_evpn_route_entry(
3081 ret
= uninstall_evpn_route_entry(
3087 "%u: Failed to %s EVPN %s route in VNI %u",
3091 rtype
== BGP_EVPN_MAC_IP_ROUTE
3105 /* Install any existing remote ES routes applicable for this ES into its routing
3106 * table. This is invoked when ES comes up.
3108 static int install_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
3110 return install_uninstall_routes_for_es(bgp
, es
, 1);
3114 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3115 * is invoked upon l3vni-add or l3vni import rt change
3117 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3119 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3124 * Install any existing remote routes applicable for this VNI into its
3125 * routing table. This is invoked when a VNI becomes "live" or its Import
3128 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3132 /* Install type-3 routes followed by type-2 routes - the ones applicable
3135 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3140 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3144 /* uninstall routes from l3vni vrf. */
3145 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3147 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3152 * Uninstall any existing remote routes for this VNI. One scenario in which
3153 * this is invoked is upon an import RT change.
3155 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3159 /* Uninstall type-2 routes followed by type-3 routes - the ones
3163 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3168 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3172 /* Install or unistall route in ES */
3173 static int install_uninstall_route_in_es(struct bgp
*bgp
, struct evpnes
*es
,
3174 afi_t afi
, safi_t safi
,
3175 struct prefix_evpn
*evp
,
3176 struct bgp_path_info
*pi
, int install
)
3179 char buf
[ESI_STR_LEN
];
3182 ret
= install_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3184 ret
= uninstall_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3189 "%u: Failed to %s EVPN %s route in ESI %s", bgp
->vrf_id
,
3190 install
? "install" : "uninstall", "ES",
3191 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)));
3198 * Install or uninstall route in matching VRFs (list).
3200 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3201 safi_t safi
, struct prefix_evpn
*evp
,
3202 struct bgp_path_info
*pi
,
3203 struct list
*vrfs
, int install
)
3205 char buf
[PREFIX2STR_BUFFER
];
3206 struct bgp
*bgp_vrf
;
3207 struct listnode
*node
, *nnode
;
3209 /* Only type-2/type-5 routes go into a VRF */
3210 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3211 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3214 /* if it is type-2 route and not a mac+ip route skip this route */
3215 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3216 && !(is_evpn_prefix_ipaddr_v4(evp
)
3217 || is_evpn_prefix_ipaddr_v6(evp
)))
3220 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3224 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3226 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3230 flog_err(EC_BGP_EVPN_FAIL
,
3231 "%u: Failed to %s prefix %s in VRF %s",
3233 install
? "install" : "uninstall",
3234 prefix2str(evp
, buf
, sizeof(buf
)),
3235 vrf_id_to_name(bgp_vrf
->vrf_id
));
3244 * Install or uninstall route in matching VNIs (list).
3246 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3247 safi_t safi
, struct prefix_evpn
*evp
,
3248 struct bgp_path_info
*pi
,
3249 struct list
*vnis
, int install
)
3251 struct bgpevpn
*vpn
;
3252 struct listnode
*node
, *nnode
;
3254 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3257 if (!is_vni_live(vpn
))
3261 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3263 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3266 flog_err(EC_BGP_EVPN_FAIL
,
3267 "%u: Failed to %s EVPN %s route in VNI %u",
3268 bgp
->vrf_id
, install
? "install" : "uninstall",
3269 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3281 * Install or uninstall route for appropriate VNIs/ESIs.
3283 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3285 struct bgp_path_info
*pi
, int import
)
3287 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3288 struct attr
*attr
= pi
->attr
;
3289 struct ecommunity
*ecom
;
3294 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3295 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3296 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3297 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3298 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3301 /* If we don't have Route Target, nothing much to do. */
3302 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3305 ecom
= attr
->ecommunity
;
3306 if (!ecom
|| !ecom
->size
)
3309 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3310 * attached to the route */
3311 for (i
= 0; i
< ecom
->size
; i
++) {
3313 uint8_t type
, sub_type
;
3314 struct ecommunity_val
*eval
;
3315 struct ecommunity_val eval_tmp
;
3316 struct irt_node
*irt
; /* import rt for l2vni */
3317 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3320 /* Only deal with RTs */
3321 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3322 eval
= (struct ecommunity_val
*)(ecom
->val
3323 + (i
* ECOMMUNITY_SIZE
));
3326 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3330 * macip routes (type-2) are imported into VNI and VRF tables.
3331 * IMET route is imported into VNI table.
3332 * prefix routes are imported into VRF table.
3334 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3335 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3336 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3338 irt
= lookup_import_rt(bgp
, eval
);
3340 install_uninstall_route_in_vnis(
3341 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3344 vrf_irt
= lookup_vrf_import_rt(eval
);
3346 install_uninstall_route_in_vrfs(
3347 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3350 /* Also check for non-exact match.
3351 * In this, we mask out the AS and
3352 * only check on the local-admin sub-field.
3353 * This is to facilitate using
3354 * VNI as the RT for EBGP peering too.
3358 if (type
== ECOMMUNITY_ENCODE_AS
3359 || type
== ECOMMUNITY_ENCODE_AS4
3360 || type
== ECOMMUNITY_ENCODE_IP
) {
3361 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3362 mask_ecom_global_admin(&eval_tmp
, eval
);
3363 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3364 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3368 install_uninstall_route_in_vnis(
3369 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3372 install_uninstall_route_in_vrfs(
3373 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3377 /* es route is imported into the es table */
3378 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3380 /* we will match based on the entire esi to avoid
3381 * imoort of an es route for esi2 into esi1
3383 es
= bgp_evpn_lookup_es(bgp
, &evp
->prefix
.es_addr
.esi
);
3384 if (es
&& is_es_local(es
))
3385 install_uninstall_route_in_es(
3386 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3394 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3397 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3399 /* Delete ipv4 default route and withdraw from peers */
3400 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3401 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3402 SAFI_UNICAST
, false);
3404 /* delete all ipv4 routes and withdraw from peers */
3405 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3406 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3408 /* Delete ipv6 default route and withdraw from peers */
3409 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3410 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3411 SAFI_UNICAST
, false);
3413 /* delete all ipv6 routes and withdraw from peers */
3414 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3415 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3419 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3422 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3424 /* update all ipv4 routes */
3425 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3426 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3428 /* update ipv4 default route and withdraw from peers */
3429 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3430 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3431 SAFI_UNICAST
, true);
3433 /* update all ipv6 routes */
3434 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3435 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3437 /* update ipv6 default route and withdraw from peers */
3438 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3439 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3440 SAFI_UNICAST
, true);
3445 * update and advertise local routes for a VRF as type-5 routes.
3446 * This is invoked upon RD change for a VRF. Note taht the processing is only
3447 * done in the global route table using the routes which already exist in the
3450 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3452 /* skip if the RD is configured */
3453 if (is_vrf_rd_configured(bgp_vrf
))
3456 /* derive the RD for the VRF based on new router-id */
3457 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3459 /* update advertise ipv4|ipv6 routes as type-5 routes */
3460 update_advertise_vrf_routes(bgp_vrf
);
3464 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3465 * This is invoked upon VRF RD change. The processing is done only from global
3468 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3470 /* skip if the RD is configured */
3471 if (is_vrf_rd_configured(bgp_vrf
))
3474 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3475 delete_withdraw_vrf_routes(bgp_vrf
);
3479 * Update and advertise local routes for a VNI. Invoked upon router-id
3480 * change. Note that the processing is done only on the global route table
3481 * using routes that already exist in the per-VNI table.
3483 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3485 struct prefix_evpn p
;
3486 struct bgp_node
*rn
, *global_rn
;
3487 struct bgp_path_info
*pi
, *global_pi
;
3489 afi_t afi
= AFI_L2VPN
;
3490 safi_t safi
= SAFI_EVPN
;
3492 /* Locate type-3 route for VNI in the per-VNI table and use its
3493 * attributes to create and advertise the type-3 route for this VNI
3494 * in the global table.
3496 * RT-3 only if doing head-end replication
3498 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
3499 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3500 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3501 if (!rn
) /* unexpected */
3503 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
3504 if (pi
->peer
== bgp
->peer_self
&&
3505 pi
->type
== ZEBRA_ROUTE_BGP
3506 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3508 if (!pi
) /* unexpected */
3512 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3513 (struct prefix
*)&p
, &vpn
->prd
);
3514 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
,
3515 1, &pi
, 0, mac_mobility_seqnum(attr
));
3517 /* Schedule for processing and unlock node. */
3518 bgp_process(bgp
, global_rn
, afi
, safi
);
3519 bgp_unlock_node(global_rn
);
3522 /* Now, walk this VNI's route table and use the route and its attribute
3523 * to create and schedule route in global table.
3525 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
3526 rn
= bgp_route_next(rn
)) {
3527 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3529 /* Identify MAC-IP local routes. */
3530 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3533 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
3534 if (pi
->peer
== bgp
->peer_self
3535 && pi
->type
== ZEBRA_ROUTE_BGP
3536 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3541 /* Create route in global routing table using this route entry's
3545 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3546 (struct prefix
*)evp
, &vpn
->prd
);
3548 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
3550 mac_mobility_seqnum(attr
));
3552 /* Schedule for processing and unlock node. */
3553 bgp_process(bgp
, global_rn
, afi
, safi
);
3554 bgp_unlock_node(global_rn
);
3561 * Delete (and withdraw) local routes for a VNI - only from the global
3562 * table. Invoked upon router-id change.
3564 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3567 struct prefix_evpn p
;
3568 struct bgp_node
*global_rn
;
3569 struct bgp_path_info
*pi
;
3570 afi_t afi
= AFI_L2VPN
;
3571 safi_t safi
= SAFI_EVPN
;
3573 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3574 * for this VNI - from the global table.
3576 ret
= delete_global_type2_routes(bgp
, vpn
);
3580 /* Remove type-3 route for this VNI from global table. */
3581 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3582 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3583 (struct prefix
*)&p
, &vpn
->prd
);
3585 /* Delete route entry in the global EVPN table. */
3586 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
3588 /* Schedule for processing - withdraws to peers happen from
3592 bgp_process(bgp
, global_rn
, afi
, safi
);
3593 bgp_unlock_node(global_rn
);
3600 * Handle router-id change. Update and advertise local routes corresponding
3601 * to this VNI from peers. Note that this is invoked after updating the
3602 * router-id. The routes in the per-VNI table are used to create routes in
3603 * the global table and schedule them.
3605 static void update_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3607 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3609 /* Skip VNIs with configured RD. */
3610 if (is_rd_configured(vpn
))
3613 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3614 update_advertise_vni_routes(bgp
, vpn
);
3618 * Handle router-id change. Delete and withdraw local routes corresponding
3619 * to this VNI from peers. Note that this is invoked prior to updating
3620 * the router-id and is done only on the global route table, the routes
3621 * are needed in the per-VNI table to re-advertise with new router id.
3623 static void withdraw_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3625 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3627 /* Skip VNIs with configured RD. */
3628 if (is_rd_configured(vpn
))
3631 delete_withdraw_vni_routes(bgp
, vpn
);
3635 * Create RT-3 for a VNI and schedule for processing and advertisement.
3636 * This is invoked upon flooding mode changing to head-end replication.
3638 static void create_advertise_type3(struct hash_backet
*backet
, void *data
)
3640 struct bgpevpn
*vpn
= backet
->data
;
3641 struct bgp
*bgp
= data
;
3642 struct prefix_evpn p
;
3644 if (!vpn
|| !is_vni_live(vpn
))
3647 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3648 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0))
3649 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3650 "Type3 route creation failure for VNI %u", vpn
->vni
);
3654 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3655 * This is invoked upon flooding mode changing to drop BUM packets.
3657 static void delete_withdraw_type3(struct hash_backet
*backet
, void *data
)
3659 struct bgpevpn
*vpn
= backet
->data
;
3660 struct bgp
*bgp
= data
;
3661 struct prefix_evpn p
;
3663 if (!vpn
|| !is_vni_live(vpn
))
3666 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3667 delete_evpn_route(bgp
, vpn
, &p
);
3671 * Process received EVPN type-2 route (advertise or withdraw).
3673 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3674 struct attr
*attr
, uint8_t *pfx
, int psize
,
3675 uint32_t addpath_id
)
3677 struct prefix_rd prd
;
3678 struct prefix_evpn p
;
3679 struct bgp_route_evpn evpn
;
3681 uint8_t macaddr_len
;
3682 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
3683 uint32_t num_labels
= 0;
3687 /* Type-2 route should be either 33, 37 or 49 bytes or an
3688 * additional 3 bytes if there is a second label (VNI):
3689 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3690 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3691 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3693 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3694 && psize
!= 40 && psize
!= 52) {
3695 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3696 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3697 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3701 memset(&evpn
, 0, sizeof(evpn
));
3703 /* Make prefix_rd */
3704 prd
.family
= AF_UNSPEC
;
3706 memcpy(&prd
.val
, pfx
, 8);
3709 /* Make EVPN prefix. */
3710 memset(&p
, 0, sizeof(struct prefix_evpn
));
3712 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3713 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3715 /* Copy Ethernet Seg Identifier */
3716 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
3719 /* Copy Ethernet Tag */
3720 memcpy(ð_tag
, pfx
, 4);
3721 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3724 /* Get the MAC Addr len */
3725 macaddr_len
= *pfx
++;
3727 /* Get the MAC Addr */
3728 if (macaddr_len
== (ETH_ALEN
* 8)) {
3729 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
3733 EC_BGP_EVPN_ROUTE_INVALID
,
3734 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3735 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3741 ipaddr_len
= *pfx
++;
3742 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3743 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3745 EC_BGP_EVPN_ROUTE_INVALID
,
3746 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3747 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3752 ipaddr_len
/= 8; /* Convert to bytes. */
3753 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3756 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
3760 /* Get the VNI(s). Stored as bytes here. */
3762 memset(label
, 0, sizeof(label
));
3763 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
3764 pfx
+= BGP_LABEL_BYTES
;
3765 psize
-= (33 + ipaddr_len
);
3766 /* Do we have a second VNI? */
3769 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
3771 * If in future, we are required to access additional fields,
3772 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
3777 /* Process the route. */
3779 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3780 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3781 &prd
, &label
[0], num_labels
, 0, &evpn
);
3783 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3784 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3785 &prd
, &label
[0], num_labels
, &evpn
);
3790 * Process received EVPN type-3 route (advertise or withdraw).
3792 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3793 struct attr
*attr
, uint8_t *pfx
, int psize
,
3794 uint32_t addpath_id
)
3796 struct prefix_rd prd
;
3797 struct prefix_evpn p
;
3802 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3803 * IP len (1) and IP (4 or 16).
3805 if (psize
!= 17 && psize
!= 29) {
3806 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3807 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3808 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3812 /* If PMSI is present, log if it is anything other than IR.
3813 * Note: We just simply ignore the values as it is not clear if
3814 * doing anything else is better.
3817 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3818 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
) {
3820 EC_BGP_EVPN_PMSI_PRESENT
,
3821 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3822 peer
->bgp
->vrf_id
, peer
->host
,
3823 attr
->pmsi_tnl_type
);
3827 /* Make prefix_rd */
3828 prd
.family
= AF_UNSPEC
;
3830 memcpy(&prd
.val
, pfx
, 8);
3833 /* Make EVPN prefix. */
3834 memset(&p
, 0, sizeof(struct prefix_evpn
));
3836 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3837 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3839 /* Copy Ethernet Tag */
3840 memcpy(ð_tag
, pfx
, 4);
3841 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
3845 ipaddr_len
= *pfx
++;
3846 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3847 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
3848 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
3851 EC_BGP_EVPN_ROUTE_INVALID
,
3852 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3853 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3857 /* Process the route. */
3859 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3860 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3861 &prd
, NULL
, 0, 0, NULL
);
3863 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3864 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3865 &prd
, NULL
, 0, NULL
);
3870 * Process received EVPN type-4 route (advertise or withdraw).
3872 static int process_type4_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3873 struct attr
*attr
, uint8_t *pfx
, int psize
,
3874 uint32_t addpath_id
)
3879 struct in_addr vtep_ip
;
3880 struct prefix_rd prd
;
3881 struct prefix_evpn p
;
3883 /* Type-4 route should be either 23 or 35 bytes
3884 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
3886 if (psize
!= 23 && psize
!= 35) {
3887 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3888 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
3889 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3893 /* Make prefix_rd */
3894 prd
.family
= AF_UNSPEC
;
3896 memcpy(&prd
.val
, pfx
, 8);
3900 memcpy(&esi
, pfx
, ESI_BYTES
);
3905 ipaddr_len
= *pfx
++;
3906 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3907 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
3910 EC_BGP_EVPN_ROUTE_INVALID
,
3911 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
3912 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3916 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
3917 /* Process the route. */
3919 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3920 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3921 &prd
, NULL
, 0, 0, NULL
);
3923 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3924 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3925 &prd
, NULL
, 0, NULL
);
3932 * Process received EVPN type-5 route (advertise or withdraw).
3934 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3935 struct attr
*attr
, uint8_t *pfx
, int psize
,
3936 uint32_t addpath_id
, int withdraw
)
3938 struct prefix_rd prd
;
3939 struct prefix_evpn p
;
3940 struct bgp_route_evpn evpn
;
3943 mpls_label_t label
; /* holds the VNI as in the packet */
3946 /* Type-5 route should be 34 or 58 bytes:
3947 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3948 * GW (4 or 16) and VNI (3).
3949 * Note that the IP and GW should both be IPv4 or both IPv6.
3951 if (psize
!= 34 && psize
!= 58) {
3952 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3953 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3954 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3958 /* Make prefix_rd */
3959 prd
.family
= AF_UNSPEC
;
3961 memcpy(&prd
.val
, pfx
, 8);
3964 /* Make EVPN prefix. */
3965 memset(&p
, 0, sizeof(struct prefix_evpn
));
3967 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3968 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
3970 /* Additional information outside of prefix - ESI and GW IP */
3971 memset(&evpn
, 0, sizeof(evpn
));
3974 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
3977 /* Fetch Ethernet Tag. */
3978 memcpy(ð_tag
, pfx
, 4);
3979 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
3982 /* Fetch IP prefix length. */
3984 if (ippfx_len
> IPV6_MAX_BITLEN
) {
3986 EC_BGP_EVPN_ROUTE_INVALID
,
3987 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3988 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
3991 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
3993 /* Determine IPv4 or IPv6 prefix */
3994 /* Since the address and GW are from the same family, this just becomes
3995 * a simple check on the total size.
3998 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
3999 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
4001 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
4004 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
4005 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
4007 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
4011 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4012 memset(&label
, 0, sizeof(label
));
4013 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
4016 * If in future, we are required to access additional fields,
4017 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4021 /* Process the route. */
4023 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4024 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4025 &prd
, &label
, 1, 0, &evpn
);
4027 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4028 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4029 &prd
, &label
, 1, &evpn
);
4034 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
4035 struct prefix_rd
*prd
, mpls_label_t
*label
,
4036 uint32_t num_labels
, struct attr
*attr
)
4040 struct evpn_addr
*p_evpn_p
;
4042 memset(&temp
, 0, 16);
4043 if (p
->family
!= AF_EVPN
)
4045 p_evpn_p
= &(p
->u
.prefix_evpn
);
4047 /* len denites the total len of IP and GW-IP in the route
4048 IP and GW-IP have to be both ipv4 or ipv6
4050 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4051 len
= 8; /* IP and GWIP are both ipv4 */
4053 len
= 32; /* IP and GWIP are both ipv6 */
4054 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4055 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4056 stream_put(s
, prd
->val
, 8);
4058 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
4060 stream_put(s
, &temp
, 10);
4061 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4062 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4063 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4064 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4066 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4068 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4070 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
4072 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
4074 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4075 stream_put_ipv4(s
, 0);
4077 stream_put(s
, &temp
, 16);
4081 stream_put(s
, label
, 3);
4087 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4089 static void cleanup_vni_on_disable(struct hash_backet
*backet
, struct bgp
*bgp
)
4091 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
4093 /* Remove EVPN routes and schedule for processing. */
4094 delete_routes_for_vni(bgp
, vpn
);
4096 /* Clear "live" flag and see if hash needs to be freed. */
4097 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4098 if (!is_vni_configured(vpn
))
4099 bgp_evpn_free(bgp
, vpn
);
4103 * Free a VNI entry; iterator function called during cleanup.
4105 static void free_vni_entry(struct hash_backet
*backet
, struct bgp
*bgp
)
4107 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
4109 delete_all_vni_routes(bgp
, vpn
);
4110 bgp_evpn_free(bgp
, vpn
);
4114 * Derive AUTO import RT for BGP VRF - L3VNI
4116 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4118 struct bgp
*bgp_def
= NULL
;
4120 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4121 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4124 bgp_def
= bgp_get_default();
4127 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4131 * Delete AUTO import RT from BGP VRF - L3VNI
4133 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4135 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4139 * Derive AUTO export RT for BGP VRF - L3VNI
4141 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4143 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4144 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4148 * Delete AUTO export RT from BGP VRF - L3VNI
4150 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4152 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4155 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4157 struct bgp
*bgp_def
= NULL
;
4158 struct listnode
*node
= NULL
;
4159 struct bgpevpn
*vpn
= NULL
;
4161 bgp_def
= bgp_get_default();
4165 /* update all type-5 routes */
4166 update_advertise_vrf_routes(bgp_vrf
);
4168 /* update all type-2 routes */
4169 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4170 update_routes_for_vni(bgp_def
, vpn
);
4174 * Handle autort change for a given VNI.
4176 static void update_autort_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
4178 struct bgpevpn
*vpn
= backet
->data
;
4180 if (!is_import_rt_configured(vpn
)) {
4181 if (is_vni_live(vpn
))
4182 bgp_evpn_uninstall_routes(bgp
, vpn
);
4183 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4184 list_delete_all_node(vpn
->import_rtl
);
4185 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4186 if (is_vni_live(vpn
))
4187 bgp_evpn_install_routes(bgp
, vpn
);
4189 if (!is_export_rt_configured(vpn
)) {
4190 list_delete_all_node(vpn
->export_rtl
);
4191 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4192 if (is_vni_live(vpn
))
4193 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4201 /* withdraw type-5 route corresponding to ip prefix */
4202 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4203 afi_t afi
, safi_t safi
)
4206 struct prefix_evpn evp
;
4207 char buf
[PREFIX_STRLEN
];
4209 build_type5_prefix_from_ip_prefix(&evp
, p
);
4210 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4213 EC_BGP_EVPN_ROUTE_DELETE
,
4214 "%u failed to delete type-5 route for prefix %s in vrf %s",
4215 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4216 vrf_id_to_name(bgp_vrf
->vrf_id
));
4220 /* withdraw all type-5 routes for an address family */
4221 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4223 struct bgp_table
*table
= NULL
;
4224 struct bgp_node
*rn
= NULL
;
4225 struct bgp_path_info
*pi
;
4227 table
= bgp_vrf
->rib
[afi
][safi
];
4228 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4229 /* Only care about "selected" routes - non-imported. */
4230 /* TODO: Support for AddPath for EVPN. */
4231 for (pi
= rn
->info
; pi
; pi
= pi
->next
) {
4232 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4233 && (!pi
->extra
|| !pi
->extra
->parent
)) {
4234 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
4243 * evpn - enable advertisement of default g/w
4245 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
4246 safi_t safi
, bool add
)
4248 struct prefix ip_prefix
;
4250 /* form the default prefix 0.0.0.0/0 */
4251 memset(&ip_prefix
, 0, sizeof(struct prefix
));
4252 ip_prefix
.family
= afi2family(afi
);
4255 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
4258 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
4265 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4266 * to this function correspond to those of the source IP prefix (best
4267 * path in the case of the attr. In the case of a local prefix (when we
4268 * are advertising local subnets), the src_attr will be NULL.
4270 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4271 struct attr
*src_attr
, afi_t afi
,
4275 struct prefix_evpn evp
;
4276 char buf
[PREFIX_STRLEN
];
4278 build_type5_prefix_from_ip_prefix(&evp
, p
);
4279 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4281 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4282 "%u: Failed to create type-5 route for prefix %s",
4283 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4286 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4287 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4288 * advertisement is enabled.
4290 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4293 struct bgp_table
*table
= NULL
;
4294 struct bgp_node
*rn
= NULL
;
4295 struct bgp_path_info
*pi
;
4297 table
= bgp_vrf
->rib
[afi
][safi
];
4298 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4299 /* Need to identify the "selected" route entry to use its
4300 * attribute. Also, we only consider "non-imported" routes.
4301 * TODO: Support for AddPath for EVPN.
4303 for (pi
= rn
->info
; pi
; pi
= pi
->next
) {
4304 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4305 && (!pi
->extra
|| !pi
->extra
->parent
)) {
4307 /* apply the route-map */
4308 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4311 ret
= route_map_apply(
4312 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4314 &rn
->p
, RMAP_BGP
, pi
);
4315 if (ret
== RMAP_DENYMATCH
)
4318 bgp_evpn_advertise_type5_route(
4319 bgp_vrf
, &rn
->p
, pi
->attr
, afi
, safi
);
4326 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4328 struct listnode
*node
, *nnode
, *node_to_del
;
4329 struct ecommunity
*ecom
, *ecom_auto
;
4330 struct ecommunity_val eval
;
4332 if (bgp
->advertise_autort_rfc8365
)
4333 vni
|= EVPN_AUTORT_VXLAN
;
4334 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4336 ecom_auto
= ecommunity_new();
4337 ecommunity_add_val(ecom_auto
, &eval
);
4340 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4341 if (ecommunity_match(ecom
, ecom_auto
)) {
4342 ecommunity_free(&ecom
);
4348 list_delete_node(rtl
, node_to_del
);
4350 ecommunity_free(&ecom_auto
);
4353 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4354 struct ecommunity
*ecomadd
)
4356 /* uninstall routes from vrf */
4357 uninstall_routes_for_vrf(bgp_vrf
);
4359 /* Cleanup the RT to VRF mapping */
4360 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4362 /* Remove auto generated RT */
4363 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4365 /* Add the newly configured RT to RT list */
4366 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4367 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4369 /* map VRF to its RTs */
4370 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4372 /* install routes matching the new VRF */
4373 install_routes_for_vrf(bgp_vrf
);
4376 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4377 struct ecommunity
*ecomdel
)
4379 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4380 struct ecommunity
*ecom
= NULL
;
4382 /* uninstall routes from vrf */
4383 uninstall_routes_for_vrf(bgp_vrf
);
4385 /* Cleanup the RT to VRF mapping */
4386 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4388 /* remove the RT from the RT list */
4389 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4390 if (ecommunity_match(ecom
, ecomdel
)) {
4391 ecommunity_free(&ecom
);
4398 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4400 assert(bgp_vrf
->vrf_import_rtl
);
4401 /* fallback to auto import rt, if this was the last RT */
4402 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4403 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4404 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4407 /* map VRFs to its RTs */
4408 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4410 /* install routes matching this new RT */
4411 install_routes_for_vrf(bgp_vrf
);
4414 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4415 struct ecommunity
*ecomadd
)
4417 /* remove auto-generated RT */
4418 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4420 /* Add the new RT to the RT list */
4421 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4422 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4424 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4427 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4428 struct ecommunity
*ecomdel
)
4430 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4431 struct ecommunity
*ecom
= NULL
;
4433 /* Remove the RT from the RT list */
4434 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4435 if (ecommunity_match(ecom
, ecomdel
)) {
4436 ecommunity_free(&ecom
);
4443 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4446 * Temporary assert to make SA happy.
4447 * The ALL_LIST_ELEMENTS macro above has a NULL check
4448 * which means that SA is going to complain about
4449 * the list_isempty call, which doesn't NULL check.
4450 * So until we get this situation cleaned up, here
4453 assert(bgp_vrf
->vrf_export_rtl
);
4455 /* fall back to auto-generated RT if this was the last RT */
4456 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4457 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4458 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4461 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4465 * Handle change to BGP router id. This is invoked twice by the change
4466 * handler, first before the router id has been changed and then after
4467 * the router id has been changed. The first invocation will result in
4468 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4469 * will result in the routes being re-advertised.
4471 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4475 /* delete and withdraw all the type-5 routes
4476 stored in the global table for this vrf
4478 withdraw_router_id_vrf(bgp
);
4480 /* delete all the VNI routes (type-2/type-3) routes for all the
4483 hash_iterate(bgp
->vnihash
,
4484 (void (*)(struct hash_backet
*,
4485 void *))withdraw_router_id_vni
,
4489 /* advertise all routes in the vrf as type-5 routes with the new
4492 update_router_id_vrf(bgp
);
4494 /* advertise all the VNI routes (type-2/type-3) routes with the
4497 hash_iterate(bgp
->vnihash
,
4498 (void (*)(struct hash_backet
*,
4499 void *))update_router_id_vni
,
4505 * Handle change to auto-RT algorithm - update and advertise local routes.
4507 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4509 hash_iterate(bgp
->vnihash
,
4510 (void (*)(struct hash_backet
*,
4511 void*))update_autort_vni
,
4516 * Handle change to export RT - update and advertise local routes.
4518 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4520 return update_routes_for_vni(bgp
, vpn
);
4523 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4526 delete_withdraw_vrf_routes(bgp_vrf
);
4528 update_advertise_vrf_routes(bgp_vrf
);
4532 * Handle change to RD. This is invoked twice by the change handler,
4533 * first before the RD has been changed and then after the RD has
4534 * been changed. The first invocation will result in local routes
4535 * of this VNI being deleted and withdrawn and the next will result
4536 * in the routes being re-advertised.
4538 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4542 delete_withdraw_vni_routes(bgp
, vpn
);
4544 update_advertise_vni_routes(bgp
, vpn
);
4548 * Install routes for this VNI. Invoked upon change to Import RT.
4550 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4552 return install_routes_for_vni(bgp
, vpn
);
4556 * Uninstall all routes installed for this VNI. Invoked upon change
4559 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4561 return uninstall_routes_for_vni(bgp
, vpn
);
4565 * TODO: Hardcoded for a maximum of 2 VNIs right now
4567 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4572 vni1
= label2vni(label
);
4573 if (num_labels
== 2) {
4574 vni2
= label2vni(label
+ 1);
4575 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4577 snprintf(buf
, len
, "%u", vni1
);
4582 * Function to convert evpn route to json format.
4583 * NOTE: We don't use prefix2str as the output here is a bit different.
4585 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
4587 char buf1
[ETHER_ADDR_STRLEN
];
4588 char buf2
[PREFIX2STR_BUFFER
];
4593 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4594 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4595 json_object_int_add(json
, "ethTag",
4596 p
->prefix
.imet_addr
.eth_tag
);
4597 json_object_int_add(json
, "ipLen",
4598 is_evpn_prefix_ipaddr_v4(p
)
4601 json_object_string_add(json
, "ip",
4602 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4603 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4604 if (is_evpn_prefix_ipaddr_none(p
)) {
4605 json_object_int_add(json
, "routeType",
4606 p
->prefix
.route_type
);
4607 json_object_int_add(json
, "ethTag",
4608 p
->prefix
.macip_addr
.eth_tag
);
4609 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4610 json_object_string_add(json
, "mac",
4611 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4617 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4620 json_object_int_add(json
, "routeType",
4621 p
->prefix
.route_type
);
4622 json_object_int_add(json
, "ethTag",
4623 p
->prefix
.macip_addr
.eth_tag
);
4624 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4625 json_object_string_add(json
, "mac",
4626 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4629 json_object_int_add(json
, "ipLen",
4630 is_evpn_prefix_ipaddr_v4(p
)
4633 json_object_string_add(
4636 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4638 PREFIX2STR_BUFFER
));
4641 /* Currently, this is to cater to other AF_ETHERNET code. */
4646 * Function to convert evpn route to string.
4647 * NOTE: We don't use prefix2str as the output here is a bit different.
4649 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
4651 char buf1
[ETHER_ADDR_STRLEN
];
4652 char buf2
[PREFIX2STR_BUFFER
];
4653 char buf3
[ESI_STR_LEN
];
4655 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4656 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
4657 p
->prefix
.imet_addr
.eth_tag
,
4658 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4660 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4661 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4662 if (is_evpn_prefix_ipaddr_none(p
))
4663 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4664 p
->prefix
.route_type
,
4665 p
->prefix
.macip_addr
.eth_tag
,
4667 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4672 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4674 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
4675 p
->prefix
.route_type
,
4676 p
->prefix
.macip_addr
.eth_tag
,
4678 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4680 family
== AF_INET
? IPV4_MAX_BITLEN
4683 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4685 PREFIX2STR_BUFFER
));
4687 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
4688 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4689 p
->prefix
.route_type
,
4690 p
->prefix
.prefix_addr
.eth_tag
,
4691 p
->prefix
.prefix_addr
.ip_prefix_length
,
4692 is_evpn_prefix_ipaddr_v4(p
)
4693 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
4694 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
4695 } else if (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
4696 snprintf(buf
, len
, "[%d]:[%s]:[%d]:[%s]",
4697 p
->prefix
.route_type
,
4698 esi_to_str(&p
->prefix
.es_addr
.esi
, buf3
, sizeof(buf3
)),
4699 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4701 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
));
4703 /* For EVPN route types not supported yet. */
4704 snprintf(buf
, len
, "(unsupported route type %d)",
4705 p
->prefix
.route_type
);
4712 * Encode EVPN prefix in Update (MP_REACH)
4714 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
4715 struct prefix_rd
*prd
, mpls_label_t
*label
,
4716 uint32_t num_labels
, struct attr
*attr
,
4717 int addpath_encode
, uint32_t addpath_tx_id
)
4719 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4720 int len
, ipa_len
= 0;
4723 stream_putl(s
, addpath_tx_id
);
4726 stream_putc(s
, evp
->prefix
.route_type
);
4728 switch (evp
->prefix
.route_type
) {
4729 case BGP_EVPN_MAC_IP_ROUTE
:
4730 if (is_evpn_prefix_ipaddr_v4(evp
))
4731 ipa_len
= IPV4_MAX_BYTELEN
;
4732 else if (is_evpn_prefix_ipaddr_v6(evp
))
4733 ipa_len
= IPV6_MAX_BYTELEN
;
4734 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4735 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4736 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4738 stream_putc(s
, len
);
4739 stream_put(s
, prd
->val
, 8); /* RD */
4741 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
4743 stream_put(s
, 0, 10);
4744 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4745 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4746 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4747 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4748 if (ipa_len
) /* IP */
4749 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4751 /* 1st label is the L2 VNI */
4752 stream_put(s
, label
, BGP_LABEL_BYTES
);
4753 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4754 if (ipa_len
&& num_labels
> 1)
4755 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4758 case BGP_EVPN_IMET_ROUTE
:
4759 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4760 stream_put(s
, prd
->val
, 8); /* RD */
4761 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4762 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4763 /* Originating Router's IP Addr */
4764 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4767 case BGP_EVPN_ES_ROUTE
:
4768 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4769 stream_put(s
, prd
->val
, 8); /* RD */
4770 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4771 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4773 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4776 case BGP_EVPN_IP_PREFIX_ROUTE
:
4777 /* TODO: AddPath support. */
4778 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4786 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4787 struct bgp_nlri
*packet
, int withdraw
)
4793 uint32_t addpath_id
;
4794 int addpath_encoded
;
4799 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4801 lim
= pnt
+ packet
->length
;
4803 safi
= packet
->safi
;
4807 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
4808 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
4809 PEER_CAP_ADDPATH_AF_TX_RCV
));
4811 for (; pnt
< lim
; pnt
+= psize
) {
4812 /* Clear prefix structure. */
4813 memset(&p
, 0, sizeof(struct prefix
));
4815 /* Deal with path-id if AddPath is supported. */
4816 if (addpath_encoded
) {
4817 /* When packet overflow occurs return immediately. */
4818 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4821 addpath_id
= ntohl(*((uint32_t *)pnt
));
4822 pnt
+= BGP_ADDPATH_ID_LEN
;
4825 /* All EVPN NLRI types start with type and length. */
4832 /* When packet overflow occur return immediately. */
4833 if (pnt
+ psize
> lim
)
4837 case BGP_EVPN_MAC_IP_ROUTE
:
4838 if (process_type2_route(peer
, afi
, safi
,
4839 withdraw
? NULL
: attr
, pnt
,
4840 psize
, addpath_id
)) {
4843 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4844 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4849 case BGP_EVPN_IMET_ROUTE
:
4850 if (process_type3_route(peer
, afi
, safi
,
4851 withdraw
? NULL
: attr
, pnt
,
4852 psize
, addpath_id
)) {
4855 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4856 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4861 case BGP_EVPN_ES_ROUTE
:
4862 if (process_type4_route(peer
, afi
, safi
,
4863 withdraw
? NULL
: attr
, pnt
,
4864 psize
, addpath_id
)) {
4867 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4868 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4873 case BGP_EVPN_IP_PREFIX_ROUTE
:
4874 if (process_type5_route(peer
, afi
, safi
, attr
, pnt
,
4875 psize
, addpath_id
, withdraw
)) {
4878 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4879 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4889 /* Packet length consistency check. */
4897 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
4898 * The mapping will be used during route processing.
4899 * bgp_def: default bgp instance
4900 * bgp_vrf: specific bgp vrf instance on which RT is configured
4902 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
4905 struct ecommunity_val
*eval
= NULL
;
4906 struct listnode
*node
= NULL
, *nnode
= NULL
;
4907 struct ecommunity
*ecom
= NULL
;
4909 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4910 for (i
= 0; i
< ecom
->size
; i
++) {
4911 eval
= (struct ecommunity_val
*)(ecom
->val
4913 * ECOMMUNITY_SIZE
));
4914 map_vrf_to_rt(bgp_vrf
, eval
);
4920 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
4922 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
4925 struct ecommunity_val
*eval
;
4926 struct listnode
*node
, *nnode
;
4927 struct ecommunity
*ecom
;
4929 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4930 for (i
= 0; i
< ecom
->size
; i
++) {
4931 struct vrf_irt_node
*irt
;
4932 struct ecommunity_val eval_tmp
;
4934 eval
= (struct ecommunity_val
*)(ecom
->val
4936 * ECOMMUNITY_SIZE
));
4937 /* If using "automatic" RT, we only care about the
4938 * local-admin sub-field.
4939 * This is to facilitate using VNI as the RT for EBGP
4942 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4943 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
4944 BGP_VRF_IMPORT_RT_CFGD
))
4945 mask_ecom_global_admin(&eval_tmp
, eval
);
4947 irt
= lookup_vrf_import_rt(&eval_tmp
);
4949 unmap_vrf_from_rt(bgp_vrf
, irt
);
4956 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
4957 * The mapping will be used during route processing.
4959 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4962 struct ecommunity_val
*eval
;
4963 struct listnode
*node
, *nnode
;
4964 struct ecommunity
*ecom
;
4966 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4967 for (i
= 0; i
< ecom
->size
; i
++) {
4968 eval
= (struct ecommunity_val
*)(ecom
->val
4970 * ECOMMUNITY_SIZE
));
4971 map_vni_to_rt(bgp
, vpn
, eval
);
4977 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
4979 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4982 struct ecommunity_val
*eval
;
4983 struct listnode
*node
, *nnode
;
4984 struct ecommunity
*ecom
;
4986 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4987 for (i
= 0; i
< ecom
->size
; i
++) {
4988 struct irt_node
*irt
;
4989 struct ecommunity_val eval_tmp
;
4991 eval
= (struct ecommunity_val
*)(ecom
->val
4993 * ECOMMUNITY_SIZE
));
4994 /* If using "automatic" RT, we only care about the
4995 * local-admin sub-field.
4996 * This is to facilitate using VNI as the RT for EBGP
4999 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5000 if (!is_import_rt_configured(vpn
))
5001 mask_ecom_global_admin(&eval_tmp
, eval
);
5003 irt
= lookup_import_rt(bgp
, &eval_tmp
);
5005 unmap_vni_from_rt(bgp
, vpn
, irt
);
5011 * Derive Import RT automatically for VNI and map VNI to RT.
5012 * The mapping will be used during route processing.
5014 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5016 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
5017 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
5020 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
5024 * Derive Export RT automatically for VNI.
5026 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5028 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
5029 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5033 * Derive RD automatically for VNI using passed information - it
5034 * is of the form RouterId:unique-id-for-vni.
5036 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5038 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5042 * Derive RD automatically for VNI using passed information - it
5043 * is of the form RouterId:unique-id-for-vni.
5045 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5049 vpn
->prd
.family
= AF_UNSPEC
;
5050 vpn
->prd
.prefixlen
= 64;
5051 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
5052 (void)str2prefix_rd(buf
, &vpn
->prd
);
5053 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5059 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5061 struct list
*inst
= bm
->bgp
;
5062 struct listnode
*node
;
5063 struct bgp
*bgp_vrf
;
5065 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5066 if (bgp_vrf
->l3vni
== vni
)
5076 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5078 struct bgpevpn
*vpn
;
5081 memset(&tmp
, 0, sizeof(struct bgpevpn
));
5083 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5088 * Create a new vpn - invoked upon configuration or zebra notification.
5090 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5091 struct in_addr originator_ip
,
5092 vrf_id_t tenant_vrf_id
)
5094 struct bgpevpn
*vpn
;
5099 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5103 /* Set values - RD and RT set to defaults. */
5105 vpn
->originator_ip
= originator_ip
;
5106 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5108 /* Initialize route-target import and export lists */
5109 vpn
->import_rtl
= list_new();
5110 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5111 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
5112 vpn
->export_rtl
= list_new();
5113 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5114 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
5115 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5116 derive_rd_rt_for_vni(bgp
, vpn
);
5118 /* Initialize EVPN route table. */
5119 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5122 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
5123 XFREE(MTYPE_BGP_EVPN
, vpn
);
5127 /* add to l2vni list on corresponding vrf */
5128 bgpevpn_link_to_l3vni(vpn
);
5130 QOBJ_REG(vpn
, bgpevpn
);
5135 * Free a given VPN - called in multiple scenarios such as zebra
5136 * notification, configuration being deleted, advertise-all-vni disabled etc.
5137 * This just frees appropriate memory, caller should have taken other
5140 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5142 bgpevpn_unlink_from_l3vni(vpn
);
5143 bgp_table_unlock(vpn
->route_table
);
5144 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5145 list_delete(&vpn
->import_rtl
);
5146 list_delete(&vpn
->export_rtl
);
5147 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5148 hash_release(bgp
->vnihash
, vpn
);
5150 XFREE(MTYPE_BGP_EVPN
, vpn
);
5156 struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
)
5161 memset(&tmp
, 0, sizeof(struct evpnes
));
5162 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
5163 es
= hash_lookup(bgp
->esihash
, &tmp
);
5168 * Create a new local es - invoked upon zebra notification.
5170 struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
,
5172 struct ipaddr
*originator_ip
)
5180 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct evpnes
));
5184 /* set the ESI and originator_ip */
5185 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
5186 memcpy(&es
->originator_ip
, originator_ip
, sizeof(struct ipaddr
));
5188 /* Initialise the VTEP list */
5189 es
->vtep_list
= list_new();
5190 es
->vtep_list
->cmp
= evpn_vtep_ip_cmp
;
5192 /* auto derive RD for this es */
5193 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
5194 es
->prd
.family
= AF_UNSPEC
;
5195 es
->prd
.prefixlen
= 64;
5196 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), es
->rd_id
);
5197 (void)str2prefix_rd(buf
, &es
->prd
);
5199 /* Initialize the ES route table */
5200 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5203 if (!hash_get(bgp
->esihash
, es
, hash_alloc_intern
)) {
5204 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5208 QOBJ_REG(es
, evpnes
);
5214 * This just frees appropriate memory, caller should have taken other
5217 void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
)
5219 list_delete(&es
->vtep_list
);
5220 bgp_table_unlock(es
->route_table
);
5221 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
5222 hash_release(bgp
->esihash
, es
);
5224 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5228 * Import evpn route from global table to VNI/VRF/ESI.
5230 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5231 struct prefix
*p
, struct bgp_path_info
*pi
)
5233 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5237 * Unimport evpn route from VNI/VRF/ESI.
5239 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5240 struct prefix
*p
, struct bgp_path_info
*pi
)
5242 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5245 /* filter routes which have martian next hops */
5246 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5250 struct bgp_node
*rd_rn
, *rn
;
5251 struct bgp_table
*table
;
5252 struct bgp_path_info
*pi
;
5257 /* Walk entire global routing table and evaluate routes which could be
5258 * imported into this VPN. Note that we cannot just look at the routes
5259 * for the VNI's RD -
5260 * remote routes applicable for this VNI could have any RD.
5262 /* EVPN routes are a 2-level table. */
5263 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
5264 rd_rn
= bgp_route_next(rd_rn
)) {
5265 table
= (struct bgp_table
*)(rd_rn
->info
);
5269 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5271 for (pi
= rn
->info
; pi
; pi
= pi
->next
) {
5273 /* Consider "valid" remote routes applicable for
5275 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5276 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5279 if (bgp_nexthop_self(bgp
, pi
->attr
->nexthop
)) {
5281 char attr_str
[BUFSIZ
];
5282 char pbuf
[PREFIX_STRLEN
];
5284 bgp_dump_attr(pi
->attr
, attr_str
,
5287 if (bgp_debug_update(pi
->peer
, &rn
->p
,
5290 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
5297 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5300 bgp_rib_remove(rn
, pi
, pi
->peer
, afi
,
5311 * Handle del of a local MACIP.
5313 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5316 struct bgpevpn
*vpn
;
5317 struct prefix_evpn p
;
5319 /* Lookup VNI hash - should exist. */
5320 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5321 if (!vpn
|| !is_vni_live(vpn
)) {
5322 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5323 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5324 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5328 /* Remove EVPN type-2 route and schedule for processing. */
5329 build_evpn_type2_prefix(&p
, mac
, ip
);
5330 delete_evpn_route(bgp
, vpn
, &p
);
5336 * Handle add of a local MACIP.
5338 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5339 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
)
5341 struct bgpevpn
*vpn
;
5342 struct prefix_evpn p
;
5344 /* Lookup VNI hash - should exist. */
5345 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5346 if (!vpn
|| !is_vni_live(vpn
)) {
5347 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5348 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5349 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5353 /* Create EVPN type-2 route and schedule for processing. */
5354 build_evpn_type2_prefix(&p
, mac
, ip
);
5355 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
)) {
5356 char buf
[ETHER_ADDR_STRLEN
];
5357 char buf2
[INET6_ADDRSTRLEN
];
5360 EC_BGP_EVPN_ROUTE_CREATE
,
5361 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5362 bgp
->vrf_id
, vpn
->vni
,
5363 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5366 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5367 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5374 static void link_l2vni_hash_to_l3vni(struct hash_backet
*backet
,
5375 struct bgp
*bgp_vrf
)
5377 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
5378 struct bgp
*bgp_def
= NULL
;
5380 bgp_def
= bgp_get_default();
5383 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5384 bgpevpn_link_to_l3vni(vpn
);
5387 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
, struct ethaddr
*rmac
,
5388 struct in_addr originator_ip
, int filter
)
5390 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5391 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5392 struct listnode
*node
= NULL
;
5393 struct bgpevpn
*vpn
= NULL
;
5396 /* get the default instance - required to get the AS number for VRF
5399 bgp_def
= bgp_get_default();
5403 "Cannot process L3VNI %u ADD - default BGP instance not yet created",
5409 /* if the BGP vrf instance doesn't exist - create one */
5410 bgp_vrf
= bgp_lookup_by_name(vrf_id_to_name(vrf_id
));
5415 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5416 BGP_INSTANCE_TYPE_VRF
);
5418 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
:
5419 flog_err(EC_BGP_MULTI_INSTANCE
,
5420 "'bgp multiple-instance' not present\n");
5422 case BGP_ERR_AS_MISMATCH
:
5423 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5424 "BGP is already running; AS is %u\n", as
);
5426 case BGP_ERR_INSTANCE_MISMATCH
:
5427 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5428 "BGP instance name and AS number mismatch\n");
5432 /* mark as auto created */
5433 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5436 /* associate with l3vni */
5437 bgp_vrf
->l3vni
= l3vni
;
5439 /* set the router mac - to be used in mac-ip routes for this vrf */
5440 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
5442 /* set the originator ip */
5443 bgp_vrf
->originator_ip
= originator_ip
;
5445 /* set the right filter - are we using l3vni only for prefix routes? */
5447 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5449 /* auto derive RD/RT */
5450 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5451 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5452 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5453 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5454 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5456 /* link all corresponding l2vnis */
5457 hash_iterate(bgp_def
->vnihash
,
5458 (void (*)(struct hash_backet
*,
5459 void *))link_l2vni_hash_to_l3vni
,
5462 /* Only update all corresponding type-2 routes if we are advertising two
5463 * labels along with type-2 routes
5466 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5467 update_routes_for_vni(bgp_def
, vpn
);
5469 /* advertise type-5 routes if needed */
5470 update_advertise_vrf_routes(bgp_vrf
);
5472 /* install all remote routes belonging to this l3vni into correspondng
5474 install_routes_for_vrf(bgp_vrf
);
5479 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5481 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5482 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5483 struct listnode
*node
= NULL
;
5484 struct listnode
*next
= NULL
;
5485 struct bgpevpn
*vpn
= NULL
;
5487 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5491 "Cannot process L3VNI %u Del - Could not find BGP instance",
5496 bgp_def
= bgp_get_default();
5500 "Cannot process L3VNI %u Del - Could not find default BGP instance",
5505 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5506 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5507 * routes. This will uninstalling the routes from zebra and decremnt the
5510 uninstall_routes_for_vrf(bgp_vrf
);
5512 /* delete/withdraw all type-5 routes */
5513 delete_withdraw_vrf_routes(bgp_vrf
);
5515 /* remove the l3vni from vrf instance */
5518 /* remove the Rmac from the BGP vrf */
5519 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5522 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5523 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5524 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5526 if (!list_isempty(bgp_vrf
->vrf_export_rtl
)) {
5527 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5530 /* update all corresponding local mac-ip routes */
5531 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5532 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5533 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5534 update_routes_for_vni(bgp_def
, vpn
);
5538 /* If any L2VNIs point to this instance, unlink them. */
5539 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5540 bgpevpn_unlink_from_l3vni(vpn
);
5542 /* Delete the instance if it was autocreated */
5543 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5544 bgp_delete(bgp_vrf
);
5550 * Handle del of a local VNI.
5552 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5554 struct bgpevpn
*vpn
;
5556 /* Locate VNI hash */
5557 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5559 if (bgp_debug_zebra(NULL
))
5561 EC_BGP_EVPN_VPN_VNI
,
5562 "%u: VNI hash entry for VNI %u not found at DEL",
5567 /* Remove all local EVPN routes and schedule for processing (to
5568 * withdraw from peers).
5570 delete_routes_for_vni(bgp
, vpn
);
5573 * tunnel is no longer active, del tunnel ip address from tip_hash
5575 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5577 /* Clear "live" flag and see if hash needs to be freed. */
5578 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5579 if (!is_vni_configured(vpn
))
5580 bgp_evpn_free(bgp
, vpn
);
5586 * Handle add (or update) of a local VNI. The VNI changes we care
5587 * about are for the local-tunnel-ip and the (tenant) VRF.
5589 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5590 struct in_addr originator_ip
, vrf_id_t tenant_vrf_id
)
5592 struct bgpevpn
*vpn
;
5593 struct prefix_evpn p
;
5595 /* Lookup VNI. If present and no change, exit. */
5596 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5599 if (is_vni_live(vpn
)
5600 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5601 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5602 /* Probably some other param has changed that we don't
5606 /* Update tenant_vrf_id if it has changed. */
5607 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5608 bgpevpn_unlink_from_l3vni(vpn
);
5609 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5610 bgpevpn_link_to_l3vni(vpn
);
5613 /* If tunnel endpoint IP has changed, update (and delete prior
5614 * type-3 route, if needed.)
5616 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5617 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5619 /* Update all routes with new endpoint IP and/or export RT
5622 if (is_vni_live(vpn
))
5623 update_routes_for_vni(bgp
, vpn
);
5626 /* Create or update as appropriate. */
5628 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
);
5632 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5638 /* if the VNI is live already, there is nothing more to do */
5639 if (is_vni_live(vpn
))
5642 /* Mark as "live" */
5643 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5645 /* tunnel is now active, add tunnel-ip to db */
5646 bgp_tip_add(bgp
, &originator_ip
);
5648 /* filter routes as nexthop database has changed */
5649 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5652 * Create EVPN type-3 route and schedule for processing.
5654 * RT-3 only if doing head-end replication
5656 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
5657 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5658 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0)) {
5659 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5660 "%u: Type3 route creation failure for VNI %u",
5666 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5670 install_routes_for_vni(bgp
, vpn
);
5672 /* If we are advertising gateway mac-ip
5673 It needs to be conveyed again to zebra */
5674 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5680 * bgp_evpn_local_es_del
5682 int bgp_evpn_local_es_del(struct bgp
*bgp
,
5684 struct ipaddr
*originator_ip
)
5686 char buf
[ESI_STR_LEN
];
5687 struct evpnes
*es
= NULL
;
5689 if (!bgp
->esihash
) {
5690 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5695 /* Lookup ESI hash - should exist. */
5696 es
= bgp_evpn_lookup_es(bgp
, esi
);
5698 flog_warn(EC_BGP_EVPN_ESI
,
5699 "%u: ESI hash entry for ESI %s at Local ES DEL",
5700 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5704 /* Delete all local EVPN ES routes from ESI table
5705 * and schedule for processing (to withdraw from peers))
5707 delete_routes_for_es(bgp
, es
);
5709 /* free the hash entry */
5710 bgp_evpn_es_free(bgp
, es
);
5716 * bgp_evpn_local_es_add
5718 int bgp_evpn_local_es_add(struct bgp
*bgp
,
5720 struct ipaddr
*originator_ip
)
5722 char buf
[ESI_STR_LEN
];
5723 struct evpnes
*es
= NULL
;
5724 struct prefix_evpn p
;
5726 if (!bgp
->esihash
) {
5727 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5732 /* create the new es */
5733 es
= bgp_evpn_lookup_es(bgp
, esi
);
5735 es
= bgp_evpn_es_new(bgp
, esi
, originator_ip
);
5739 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
5740 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5744 UNSET_FLAG(es
->flags
, EVPNES_REMOTE
);
5745 SET_FLAG(es
->flags
, EVPNES_LOCAL
);
5747 build_evpn_type4_prefix(&p
, esi
, originator_ip
->ipaddr_v4
);
5748 if (update_evpn_type4_route(bgp
, es
, &p
)) {
5749 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5750 "%u: Type4 route creation failure for ESI %s",
5751 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5755 /* import all remote ES routes in th ES table */
5756 install_routes_for_es(bgp
, es
);
5762 * Handle change in setting for BUM handling. The supported values
5763 * are head-end replication and dropping all BUM packets. Any change
5764 * should be registered with zebra. Also, if doing head-end replication,
5765 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5766 * to be dropped, the RT-3s must be withdrawn.
5768 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
5770 zlog_info("L2VPN EVPN BUM handling is %s",
5771 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
5772 "Flooding" : "Flooding Disabled");
5774 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
5775 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
5776 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
5777 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
5778 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
5782 * Cleanup EVPN information on disable - Need to delete and withdraw
5783 * EVPN routes from peers.
5785 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5787 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
5788 void *))cleanup_vni_on_disable
,
5793 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5794 * BGP instance (default) is being freed.
5796 void bgp_evpn_cleanup(struct bgp
*bgp
)
5798 hash_iterate(bgp
->vnihash
,
5799 (void (*)(struct hash_backet
*, void *))free_vni_entry
,
5802 hash_free(bgp
->import_rt_hash
);
5803 bgp
->import_rt_hash
= NULL
;
5805 hash_free(bgp
->vrf_import_rt_hash
);
5806 bgp
->vrf_import_rt_hash
= NULL
;
5808 hash_free(bgp
->vnihash
);
5809 bgp
->vnihash
= NULL
;
5811 hash_free(bgp
->esihash
);
5812 bgp
->esihash
= NULL
;
5814 list_delete(&bgp
->vrf_import_rtl
);
5815 list_delete(&bgp
->vrf_export_rtl
);
5816 list_delete(&bgp
->l2vnis
);
5820 * Initialization for EVPN
5823 * hash for RT to VNI
5825 void bgp_evpn_init(struct bgp
*bgp
)
5828 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
5830 hash_create(esi_hash_keymake
, esi_cmp
,
5831 "BGP EVPN Local ESI Hash");
5832 bgp
->import_rt_hash
=
5833 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
5834 "BGP Import RT Hash");
5835 bgp
->vrf_import_rt_hash
=
5836 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
5837 "BGP VRF Import RT Hash");
5838 bgp
->vrf_import_rtl
= list_new();
5839 bgp
->vrf_import_rtl
->cmp
=
5840 (int (*)(void *, void *))evpn_route_target_cmp
;
5841 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
5842 bgp
->vrf_export_rtl
= list_new();
5843 bgp
->vrf_export_rtl
->cmp
=
5844 (int (*)(void *, void *))evpn_route_target_cmp
;
5845 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
5846 bgp
->l2vnis
= list_new();
5847 bgp
->l2vnis
->cmp
= vni_list_cmp
;
5848 /* By default Duplicate Address Dection is enabled.
5849 * Max-moves (N) 5, detection time (M) 180
5850 * default action is warning-only
5851 * freeze action permanently freezes address,
5852 * and freeze time (auto-recovery) is disabled.
5854 if (bgp
->evpn_info
) {
5855 bgp
->evpn_info
->dup_addr_detect
= true;
5856 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
5857 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
5858 bgp
->evpn_info
->dad_freeze
= false;
5859 bgp
->evpn_info
->dad_freeze_time
= 0;
5860 /* Initialize zebra vxlan */
5861 bgp_zebra_dup_addr_detection(bgp
);
5864 /* Default BUM handling is to do head-end replication. */
5865 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
5868 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
5870 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);