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
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1236 tmp_pi
= tmp_pi
->next
) {
1237 if (tmp_pi
->peer
== bgp
->peer_self
1238 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1239 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1246 return local_pi
->attr
->default_gw
;
1251 * Return true if the local ri for this rn has sticky set
1253 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
1255 struct bgp_path_info
*tmp_pi
;
1256 struct bgp_path_info
*local_pi
;
1259 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1260 tmp_pi
= tmp_pi
->next
) {
1261 if (tmp_pi
->peer
== bgp
->peer_self
1262 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1263 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1270 return local_pi
->attr
->sticky
;
1274 * create or update EVPN type4 route entry.
1275 * This could be in the ES table or the global table.
1276 * TODO: handle remote ES (type4) routes as well
1278 static int update_evpn_type4_route_entry(struct bgp
*bgp
, struct evpnes
*es
,
1279 afi_t afi
, safi_t safi
,
1280 struct bgp_node
*rn
, struct attr
*attr
,
1281 int add
, struct bgp_path_info
**ri
,
1284 char buf
[ESI_STR_LEN
];
1285 char buf1
[INET6_ADDRSTRLEN
];
1286 struct bgp_path_info
*tmp_pi
= NULL
;
1287 struct bgp_path_info
*local_pi
= NULL
; /* local route entry if any */
1288 struct bgp_path_info
*remote_pi
= NULL
; /* remote route entry if any */
1289 struct attr
*attr_new
= NULL
;
1290 struct prefix_evpn
*evp
= NULL
;
1294 evp
= (struct prefix_evpn
*)&rn
->p
;
1296 /* locate the local and remote entries if any */
1297 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1298 tmp_pi
= tmp_pi
->next
) {
1299 if (tmp_pi
->peer
== bgp
->peer_self
1300 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1301 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1303 if (tmp_pi
->type
== ZEBRA_ROUTE_BGP
1304 && tmp_pi
->sub_type
== BGP_ROUTE_IMPORTED
1305 && CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1309 /* we don't expect to see a remote_ri at this point.
1310 * An ES route has esi + vtep_ip as the key,
1311 * We shouldn't see the same route from any other vtep.
1316 "%u ERROR: local es route for ESI: %s Vtep %s also learnt from remote",
1318 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1319 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1323 if (!local_pi
&& !add
)
1326 /* create or update the entry */
1329 /* Add or update attribute to hash */
1330 attr_new
= bgp_attr_intern(attr
);
1332 /* Create new route with its attribute. */
1333 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1334 bgp
->peer_self
, attr_new
, rn
);
1335 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1337 /* add the newly created path to the route-node */
1338 bgp_path_info_add(rn
, tmp_pi
);
1341 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1342 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1345 /* The attribute has changed.
1346 * Add (or update) attribute to hash. */
1347 attr_new
= bgp_attr_intern(attr
);
1348 bgp_path_info_set_flag(rn
, tmp_pi
,
1349 BGP_PATH_ATTR_CHANGED
);
1351 /* Restore route, if needed. */
1352 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1353 bgp_path_info_restore(rn
, tmp_pi
);
1355 /* Unintern existing, set to new. */
1356 bgp_attr_unintern(&tmp_pi
->attr
);
1357 tmp_pi
->attr
= attr_new
;
1358 tmp_pi
->uptime
= bgp_clock();
1362 /* Return back the route entry. */
1367 /* update evpn es (type-4) route */
1368 static int update_evpn_type4_route(struct bgp
*bgp
,
1370 struct prefix_evpn
*p
)
1373 int route_changed
= 0;
1374 char buf
[ESI_STR_LEN
];
1375 char buf1
[INET6_ADDRSTRLEN
];
1376 afi_t afi
= AFI_L2VPN
;
1377 safi_t safi
= SAFI_EVPN
;
1379 struct attr
*attr_new
= NULL
;
1380 struct bgp_node
*rn
= NULL
;
1381 struct bgp_path_info
*pi
= NULL
;
1383 memset(&attr
, 0, sizeof(struct attr
));
1385 /* Build path-attribute for this route. */
1386 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1387 attr
.nexthop
= es
->originator_ip
.ipaddr_v4
;
1388 attr
.mp_nexthop_global_in
= es
->originator_ip
.ipaddr_v4
;
1389 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1391 /* Set up extended community. */
1392 build_evpn_type4_route_extcomm(es
, &attr
);
1394 /* First, create (or fetch) route node within the ESI. */
1395 /* NOTE: There is no RD here. */
1396 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
1398 /* Create or update route entry. */
1399 ret
= update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, &attr
, 1,
1400 &pi
, &route_changed
);
1402 flog_err(EC_BGP_ES_INVALID
,
1403 "%u ERROR: Failed to updated ES route ESI: %s VTEP %s",
1405 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1406 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1410 attr_new
= pi
->attr
;
1412 /* Perform route selection;
1413 * this is just to set the flags correctly
1414 * as local route in the ES always wins.
1416 evpn_es_route_select_install(bgp
, es
, rn
);
1417 bgp_unlock_node(rn
);
1419 /* If this is a new route or some attribute has changed, export the
1420 * route to the global table. The route will be advertised to peers
1421 * from there. Note that this table is a 2-level tree (RD-level +
1422 * Prefix-level) similar to L3VPN routes.
1424 if (route_changed
) {
1425 struct bgp_path_info
*global_pi
;
1427 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1428 (struct prefix
*)p
, &es
->prd
);
1429 update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, attr_new
,
1430 1, &global_pi
, &route_changed
);
1432 /* Schedule for processing and unlock node. */
1433 bgp_process(bgp
, rn
, afi
, safi
);
1434 bgp_unlock_node(rn
);
1437 /* Unintern temporary. */
1438 aspath_unintern(&attr
.aspath
);
1442 static int update_evpn_type5_route_entry(struct bgp
*bgp_def
,
1443 struct bgp
*bgp_vrf
, afi_t afi
,
1444 safi_t safi
, struct bgp_node
*rn
,
1445 struct attr
*attr
, int *route_changed
)
1447 struct attr
*attr_new
= NULL
;
1448 struct bgp_path_info
*pi
= NULL
;
1449 mpls_label_t label
= MPLS_INVALID_LABEL
;
1450 struct bgp_path_info
*local_pi
= NULL
;
1451 struct bgp_path_info
*tmp_pi
= NULL
;
1454 /* locate the local route entry if any */
1455 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1456 tmp_pi
= tmp_pi
->next
) {
1457 if (tmp_pi
->peer
== bgp_def
->peer_self
1458 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1459 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1464 * create a new route entry if one doesn't exist.
1465 * Otherwise see if route attr has changed
1469 /* route has changed as this is the first entry */
1472 /* Add (or update) attribute to hash. */
1473 attr_new
= bgp_attr_intern(attr
);
1475 /* create the route info from attribute */
1476 pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1477 bgp_def
->peer_self
, attr_new
, rn
);
1478 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
1480 /* Type-5 routes advertise the L3-VNI */
1481 bgp_path_info_extra_get(pi
);
1482 vni2label(bgp_vrf
->l3vni
, &label
);
1483 memcpy(&pi
->extra
->label
, &label
, sizeof(label
));
1484 pi
->extra
->num_labels
= 1;
1486 /* add the route entry to route node*/
1487 bgp_path_info_add(rn
, pi
);
1491 if (!attrhash_cmp(tmp_pi
->attr
, attr
)) {
1493 /* attribute changed */
1496 /* The attribute has changed. */
1497 /* Add (or update) attribute to hash. */
1498 attr_new
= bgp_attr_intern(attr
);
1499 bgp_path_info_set_flag(rn
, tmp_pi
,
1500 BGP_PATH_ATTR_CHANGED
);
1502 /* Restore route, if needed. */
1503 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1504 bgp_path_info_restore(rn
, tmp_pi
);
1506 /* Unintern existing, set to new. */
1507 bgp_attr_unintern(&tmp_pi
->attr
);
1508 tmp_pi
->attr
= attr_new
;
1509 tmp_pi
->uptime
= bgp_clock();
1515 /* update evpn type-5 route entry */
1516 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1517 struct attr
*src_attr
)
1519 afi_t afi
= AFI_L2VPN
;
1520 safi_t safi
= SAFI_EVPN
;
1522 struct bgp_node
*rn
= NULL
;
1523 struct bgp
*bgp_def
= NULL
;
1524 int route_changed
= 0;
1526 bgp_def
= bgp_get_default();
1530 /* Build path attribute for this route - use the source attr, if
1531 * present, else treat as locally originated.
1534 bgp_attr_dup(&attr
, src_attr
);
1536 memset(&attr
, 0, sizeof(struct attr
));
1537 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1539 /* Set nexthop to ourselves and fill in the Router MAC. */
1540 attr
.nexthop
= bgp_vrf
->originator_ip
;
1541 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1542 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1543 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
1545 /* Setup RT and encap extended community */
1546 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1548 /* get the route node in global table */
1549 rn
= bgp_afi_node_get(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1550 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1553 /* create or update the route entry within the route node */
1554 update_evpn_type5_route_entry(bgp_def
, bgp_vrf
, afi
, safi
, rn
, &attr
,
1557 /* schedule for processing and unlock node */
1558 if (route_changed
) {
1559 bgp_process(bgp_def
, rn
, afi
, safi
);
1560 bgp_unlock_node(rn
);
1563 /* uninten temporary */
1565 aspath_unintern(&attr
.aspath
);
1570 * Create or update EVPN route entry. This could be in the VNI route table
1571 * or the global route table.
1573 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1574 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1575 struct attr
*attr
, int add
,
1576 struct bgp_path_info
**pi
, uint8_t flags
,
1579 struct bgp_path_info
*tmp_pi
;
1580 struct bgp_path_info
*local_pi
;
1581 struct attr
*attr_new
;
1582 mpls_label_t label
[BGP_MAX_LABELS
];
1583 uint32_t num_labels
= 1;
1584 int route_change
= 1;
1586 struct prefix_evpn
*evp
;
1589 evp
= (struct prefix_evpn
*)&rn
->p
;
1590 memset(&label
, 0, sizeof(label
));
1592 /* See if this is an update of an existing route, or a new add. */
1594 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1595 tmp_pi
= tmp_pi
->next
) {
1596 if (tmp_pi
->peer
== bgp
->peer_self
1597 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1598 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1602 /* If route doesn't exist already, create a new one, if told to.
1603 * Otherwise act based on whether the attributes of the route have
1606 if (!local_pi
&& !add
)
1609 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1610 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1611 add_mac_mobility_to_attr(seq
, attr
);
1614 /* Add (or update) attribute to hash. */
1615 attr_new
= bgp_attr_intern(attr
);
1617 /* Extract MAC mobility sequence number, if any. */
1618 attr_new
->mm_seqnum
=
1619 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1620 attr_new
->sticky
= sticky
;
1622 /* Create new route with its attribute. */
1623 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1624 bgp
->peer_self
, attr_new
, rn
);
1625 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1626 bgp_path_info_extra_get(tmp_pi
);
1628 /* The VNI goes into the 'label' field of the route */
1629 vni2label(vpn
->vni
, &label
[0]);
1631 /* Type-2 routes may carry a second VNI - the L3-VNI.
1632 * Only attach second label if we are advertising two labels for
1635 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1636 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1639 l3vni
= bgpevpn_get_l3vni(vpn
);
1641 vni2label(l3vni
, &label
[1]);
1646 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1647 tmp_pi
->extra
->num_labels
= num_labels
;
1648 bgp_path_info_add(rn
, tmp_pi
);
1651 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1652 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1656 * The attributes have changed, type-2 routes needs to
1657 * be advertised with right labels.
1659 vni2label(vpn
->vni
, &label
[0]);
1660 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1661 && CHECK_FLAG(vpn
->flags
,
1662 VNI_FLAG_USE_TWO_LABELS
)) {
1665 l3vni
= bgpevpn_get_l3vni(vpn
);
1667 vni2label(l3vni
, &label
[1]);
1671 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1672 tmp_pi
->extra
->num_labels
= num_labels
;
1674 /* The attribute has changed. */
1675 /* Add (or update) attribute to hash. */
1676 attr_new
= bgp_attr_intern(attr
);
1677 bgp_path_info_set_flag(rn
, tmp_pi
,
1678 BGP_PATH_ATTR_CHANGED
);
1680 /* Extract MAC mobility sequence number, if any. */
1681 attr_new
->mm_seqnum
=
1682 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1683 attr_new
->sticky
= sticky
;
1685 /* Restore route, if needed. */
1686 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1687 bgp_path_info_restore(rn
, tmp_pi
);
1689 /* Unintern existing, set to new. */
1690 bgp_attr_unintern(&tmp_pi
->attr
);
1691 tmp_pi
->attr
= attr_new
;
1692 tmp_pi
->uptime
= bgp_clock();
1696 /* Return back the route entry. */
1698 return route_change
;
1702 * If the local route was not selected evict it and tell zebra to re-add
1703 * the best remote dest.
1705 * Typically a local path added by zebra is expected to be selected as
1706 * best. In which case when a remote path wins as best (later)
1707 * evpn_route_select_install itself evicts the older-local-best path.
1709 * However if bgp's add and zebra's add cross paths (race condition) it
1710 * is possible that the local path is no longer the "older" best path.
1711 * It is a path that was never designated as best and hence requires
1712 * additional handling to prevent bgp from injecting and holding on to a
1713 * non-best local path.
1715 static void evpn_cleanup_local_non_best_route(struct bgp
*bgp
,
1716 struct bgpevpn
*vpn
,
1717 struct bgp_node
*rn
,
1718 struct bgp_path_info
*local_pi
)
1720 struct bgp_path_info
*tmp_pi
;
1721 struct bgp_path_info
*curr_select
= NULL
;
1723 char buf
[PREFIX_STRLEN
];
1725 /* local path was not picked as the winner; kick it out */
1726 if (bgp_debug_zebra(NULL
)) {
1727 zlog_debug("evicting local evpn prefix %s as remote won",
1728 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
1730 evpn_delete_old_local_route(bgp
, vpn
, rn
, local_pi
);
1731 bgp_path_info_reap(rn
, local_pi
);
1733 /* tell zebra to re-add the best remote path */
1734 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
);
1735 tmp_pi
; tmp_pi
= tmp_pi
->next
) {
1736 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_SELECTED
)) {
1737 curr_select
= tmp_pi
;
1742 curr_select
->type
== ZEBRA_ROUTE_BGP
1743 && curr_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1744 if (curr_select
->attr
->sticky
)
1745 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
1746 if (curr_select
->attr
->default_gw
)
1747 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
1748 evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1749 curr_select
->attr
->nexthop
, flags
,
1750 mac_mobility_seqnum(curr_select
->attr
));
1755 * Create or update EVPN route (of type based on prefix) for specified VNI
1756 * and schedule for processing.
1758 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1759 struct prefix_evpn
*p
, uint8_t flags
,
1762 struct bgp_node
*rn
;
1764 struct attr
*attr_new
;
1765 int add_l3_ecomm
= 0;
1766 struct bgp_path_info
*pi
;
1767 afi_t afi
= AFI_L2VPN
;
1768 safi_t safi
= SAFI_EVPN
;
1771 memset(&attr
, 0, sizeof(struct attr
));
1773 /* Build path-attribute for this route. */
1774 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1775 attr
.nexthop
= vpn
->originator_ip
;
1776 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1777 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1778 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1779 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1780 attr
.router_flag
= CHECK_FLAG(flags
,
1781 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1783 /* PMSI is only needed for type-3 routes */
1784 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
)
1785 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1787 /* router mac is only needed for type-2 routes here. */
1788 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1789 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1790 vni2label(vpn
->vni
, &(attr
.label
));
1792 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1793 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1796 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1797 (is_evpn_prefix_ipaddr_v4(p
) ||
1798 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1799 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1800 bgpevpn_get_l3vni(vpn
))
1803 /* Set up extended community. */
1804 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1806 /* First, create (or fetch) route node within the VNI. */
1807 /* NOTE: There is no RD here. */
1808 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1810 /* Create or update route entry. */
1811 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1812 1, &pi
, flags
, seq
);
1814 attr_new
= pi
->attr
;
1816 /* lock ri to prevent freeing in evpn_route_select_install */
1817 bgp_path_info_lock(pi
);
1818 /* Perform route selection; this is just to set the flags correctly
1819 * as local route in the VNI always wins.
1821 evpn_route_select_install(bgp
, vpn
, rn
);
1823 * If the new local route was not selected evict it and tell zebra
1824 * to re-add the best remote dest. BGP doesn't retain non-best local
1827 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1829 evpn_cleanup_local_non_best_route(bgp
, vpn
, rn
, pi
);
1831 bgp_path_info_unlock(pi
);
1833 bgp_unlock_node(rn
);
1835 /* If this is a new route or some attribute has changed, export the
1836 * route to the global table. The route will be advertised to peers
1837 * from there. Note that this table is a 2-level tree (RD-level +
1838 * Prefix-level) similar to L3VPN routes.
1841 struct bgp_path_info
*global_pi
;
1843 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1844 (struct prefix
*)p
, &vpn
->prd
);
1845 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1,
1846 &global_pi
, flags
, seq
);
1848 /* Schedule for processing and unlock node. */
1849 bgp_process(bgp
, rn
, afi
, safi
);
1850 bgp_unlock_node(rn
);
1853 /* Unintern temporary. */
1854 aspath_unintern(&attr
.aspath
);
1860 * Delete EVPN route entry.
1861 * The entry can be in ESI/VNI table or the global table.
1863 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1864 struct bgp_node
*rn
,
1865 struct bgp_path_info
**pi
)
1867 struct bgp_path_info
*tmp_pi
;
1871 /* Now, find matching route. */
1872 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1873 tmp_pi
= tmp_pi
->next
)
1874 if (tmp_pi
->peer
== bgp
->peer_self
1875 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1876 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1881 /* Mark route for delete. */
1883 bgp_path_info_delete(rn
, tmp_pi
);
1888 /* Delete EVPN ES (type-4) route */
1889 static int delete_evpn_type4_route(struct bgp
*bgp
,
1891 struct prefix_evpn
*p
)
1893 afi_t afi
= AFI_L2VPN
;
1894 safi_t safi
= SAFI_EVPN
;
1895 struct bgp_path_info
*pi
;
1896 struct bgp_node
*rn
= NULL
; /* rn in esi table */
1897 struct bgp_node
*global_rn
= NULL
; /* rn in global table */
1899 /* First, locate the route node within the ESI.
1900 * If it doesn't exist, ther is nothing to do.
1901 * Note: there is no RD here.
1903 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
1907 /* Next, locate route node in the global EVPN routing table.
1908 * Note that this table is a 2-level tree (RD-level + Prefix-level)
1910 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1911 (struct prefix
*)p
, &es
->prd
);
1914 /* Delete route entry in the global EVPN table. */
1915 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1917 /* Schedule for processing - withdraws to peers happen from
1921 bgp_process(bgp
, global_rn
, afi
, safi
);
1922 bgp_unlock_node(global_rn
);
1926 * Delete route entry in the ESI route table.
1927 * This can just be removed.
1929 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
1931 bgp_path_info_reap(rn
, pi
);
1932 bgp_unlock_node(rn
);
1936 /* Delete EVPN type5 route */
1937 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1939 afi_t afi
= AFI_L2VPN
;
1940 safi_t safi
= SAFI_EVPN
;
1941 struct bgp_node
*rn
= NULL
;
1942 struct bgp_path_info
*pi
= NULL
;
1943 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
1945 bgp_def
= bgp_get_default();
1949 /* locate the global route entry for this type-5 prefix */
1950 rn
= bgp_afi_node_lookup(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1951 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1955 delete_evpn_route_entry(bgp_def
, afi
, safi
, rn
, &pi
);
1957 bgp_process(bgp_def
, rn
, afi
, safi
);
1958 bgp_unlock_node(rn
);
1963 * Delete EVPN route (of type based on prefix) for specified VNI and
1964 * schedule for processing.
1966 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1967 struct prefix_evpn
*p
)
1969 struct bgp_node
*rn
, *global_rn
;
1970 struct bgp_path_info
*pi
;
1971 afi_t afi
= AFI_L2VPN
;
1972 safi_t safi
= SAFI_EVPN
;
1974 /* First, locate the route node within the VNI. If it doesn't exist,
1976 * is nothing further to do.
1978 /* NOTE: There is no RD here. */
1979 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1983 /* Next, locate route node in the global EVPN routing table. Note that
1984 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1987 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1988 (struct prefix
*)p
, &vpn
->prd
);
1990 /* Delete route entry in the global EVPN table. */
1991 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1993 /* Schedule for processing - withdraws to peers happen from
1997 bgp_process(bgp
, global_rn
, afi
, safi
);
1998 bgp_unlock_node(global_rn
);
2001 /* Delete route entry in the VNI route table. This can just be removed.
2003 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2005 bgp_path_info_reap(rn
, pi
);
2006 evpn_route_select_install(bgp
, vpn
, rn
);
2008 bgp_unlock_node(rn
);
2014 * Update all type-2 (MACIP) local routes for this VNI - these should also
2015 * be scheduled for advertise to peers.
2017 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2021 struct bgp_node
*rn
;
2022 struct bgp_path_info
*pi
, *tmp_pi
;
2024 struct attr
*attr_new
;
2026 int add_l3_ecomm
= 0;
2031 /* Walk this VNI's route table and update local type-2 routes. For any
2032 * routes updated, update corresponding entry in the global table too.
2034 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2035 rn
= bgp_route_next(rn
)) {
2036 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2037 struct bgp_node
*rd_rn
;
2038 struct bgp_path_info
*global_pi
;
2040 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2043 /* Identify local route. */
2044 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
2045 tmp_pi
= tmp_pi
->next
) {
2046 if (tmp_pi
->peer
== bgp
->peer_self
2047 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2048 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2056 * Build attribute per local route as the MAC mobility and
2057 * some other values could differ for different routes. The
2058 * attributes will be shared in the hash table.
2060 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
2061 attr
.nexthop
= vpn
->originator_ip
;
2062 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
2063 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
2064 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
2066 if (evpn_route_is_sticky(bgp
, rn
))
2068 else if (evpn_route_is_def_gw(bgp
, rn
)) {
2069 attr
.default_gw
= 1;
2070 if (is_evpn_prefix_ipaddr_v6(evp
))
2071 attr
.router_flag
= 1;
2074 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2075 * using L3 VNI for type-2 routes also.
2077 if ((is_evpn_prefix_ipaddr_v4(evp
) ||
2078 !IN6_IS_ADDR_LINKLOCAL(
2079 &evp
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
2080 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
2081 bgpevpn_get_l3vni(vpn
))
2084 /* Set up extended community. */
2085 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2087 seq
= mac_mobility_seqnum(tmp_pi
->attr
);
2089 /* Update the route entry. */
2090 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
, 0, &pi
,
2093 /* Perform route selection; this is just to set the flags
2094 * correctly as local route in the VNI always wins.
2096 evpn_route_select_install(bgp
, vpn
, rn
);
2098 attr_new
= pi
->attr
;
2100 /* Update route in global routing table. */
2101 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2102 (struct prefix
*)evp
, &vpn
->prd
);
2104 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
2106 mac_mobility_seqnum(attr_new
));
2108 /* Schedule for processing and unlock node. */
2109 bgp_process(bgp
, rd_rn
, afi
, safi
);
2110 bgp_unlock_node(rd_rn
);
2112 /* Unintern temporary. */
2113 aspath_unintern(&attr
.aspath
);
2121 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2122 * global routing table. These are also scheduled for withdraw from peers.
2124 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2128 struct bgp_node
*rdrn
, *rn
;
2129 struct bgp_table
*table
;
2130 struct bgp_path_info
*pi
;
2135 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
2136 if (rdrn
&& bgp_node_has_bgp_path_info_data(rdrn
)) {
2137 table
= bgp_node_get_bgp_table_info(rdrn
);
2138 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2139 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2141 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2144 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2146 bgp_process(bgp
, rn
, afi
, safi
);
2150 /* Unlock RD node. */
2152 bgp_unlock_node(rdrn
);
2158 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2159 * table as well as the per-VNI route table.
2161 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2165 struct bgp_node
*rn
;
2166 struct bgp_path_info
*pi
;
2171 /* First, walk the global route table for this VNI's type-2 local
2173 * EVPN routes are a 2-level table, first get the RD table.
2175 delete_global_type2_routes(bgp
, vpn
);
2177 /* Next, walk this VNI's route table and delete local type-2 routes. */
2178 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2179 rn
= bgp_route_next(rn
)) {
2180 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2182 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2185 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2187 /* Route entry in local table gets deleted immediately. */
2189 bgp_path_info_reap(rn
, pi
);
2196 * Delete all routes in per ES route-table
2198 static int delete_all_es_routes(struct bgp
*bgp
, struct evpnes
*es
)
2200 struct bgp_node
*rn
;
2201 struct bgp_path_info
*pi
, *nextpi
;
2203 /* Walk this ES's route table and delete all routes. */
2204 for (rn
= bgp_table_top(es
->route_table
); rn
;
2205 rn
= bgp_route_next(rn
)) {
2206 for (pi
= bgp_node_get_bgp_path_info(rn
);
2207 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2208 bgp_path_info_delete(rn
, pi
);
2209 bgp_path_info_reap(rn
, pi
);
2217 * Delete all routes in the per-VNI route table.
2219 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2221 struct bgp_node
*rn
;
2222 struct bgp_path_info
*pi
, *nextpi
;
2224 /* Walk this VNI's route table and delete all routes. */
2225 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2226 rn
= bgp_route_next(rn
)) {
2227 for (pi
= bgp_node_get_bgp_path_info(rn
);
2228 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2229 bgp_path_info_delete(rn
, pi
);
2230 bgp_path_info_reap(rn
, pi
);
2238 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2239 * export RT getting modified or change to tunnel IP. Note that these
2240 * situations need the route in the per-VNI table as well as the global
2241 * table to be updated (as attributes change).
2243 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2246 struct prefix_evpn p
;
2248 /* Update and advertise the type-3 route (only one) followed by the
2249 * locally learnt type-2 routes (MACIP) - for this VNI.
2251 * RT-3 only if doing head-end replication
2253 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
2254 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2255 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0);
2260 return update_all_type2_routes(bgp
, vpn
);
2263 /* Delete (and withdraw) local routes for specified ES from global and ES table.
2264 * Also remove all other routes from the per ES table.
2265 * Invoked when ES is deleted.
2267 static int delete_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
2270 char buf
[ESI_STR_LEN
];
2271 struct prefix_evpn p
;
2273 /* Delete and withdraw locally learnt ES route */
2274 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
.ipaddr_v4
);
2275 ret
= delete_evpn_type4_route(bgp
, es
, &p
);
2277 flog_err(EC_BGP_EVPN_ROUTE_DELETE
,
2278 "%u failed to delete type-4 route for ESI %s",
2279 bgp
->vrf_id
, esi_to_str(&es
->esi
, buf
, sizeof(buf
)));
2282 /* Delete all routes from per ES table */
2283 return delete_all_es_routes(bgp
, es
);
2287 * Delete (and withdraw) local routes for specified VNI from the global
2288 * table and per-VNI table. After this, remove all other routes from
2289 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2290 * (advertise-all-vni) being disabled.
2292 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2295 struct prefix_evpn p
;
2297 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2298 * followed by type-3 routes (only one) - for this VNI.
2300 ret
= delete_all_type2_routes(bgp
, vpn
);
2304 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2305 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2309 /* Delete all routes from the per-VNI table. */
2310 return delete_all_vni_routes(bgp
, vpn
);
2314 * There is a tunnel endpoint IP address change for this VNI, delete
2315 * prior type-3 route (if needed) and update.
2316 * Note: Route re-advertisement happens elsewhere after other processing
2319 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2320 struct in_addr originator_ip
)
2322 struct prefix_evpn p
;
2324 /* If VNI is not live, we only need to update the originator ip */
2325 if (!is_vni_live(vpn
)) {
2326 vpn
->originator_ip
= originator_ip
;
2330 /* Update the tunnel-ip hash */
2331 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2332 bgp_tip_add(bgp
, &originator_ip
);
2334 /* filter routes as martian nexthop db has changed */
2335 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2337 /* Need to withdraw type-3 route as the originator IP is part
2340 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2341 delete_evpn_route(bgp
, vpn
, &p
);
2343 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2344 vpn
->originator_ip
= originator_ip
;
2348 /* Install EVPN route entry in ES */
2349 static int install_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2350 struct prefix_evpn
*p
,
2351 struct bgp_path_info
*parent_pi
)
2354 struct bgp_node
*rn
= NULL
;
2355 struct bgp_path_info
*pi
= NULL
;
2356 struct attr
*attr_new
= NULL
;
2358 /* Create (or fetch) route within the VNI.
2359 * NOTE: There is no RD here.
2361 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
2363 /* Check if route entry is already present. */
2364 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2366 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2370 /* Add (or update) attribute to hash. */
2371 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2373 /* Create new route with its attribute. */
2374 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2375 parent_pi
->peer
, attr_new
, rn
);
2376 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2377 bgp_path_info_extra_get(pi
);
2378 pi
->extra
->parent
= parent_pi
;
2379 bgp_path_info_add(rn
, pi
);
2381 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2382 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2383 bgp_unlock_node(rn
);
2386 /* The attribute has changed. */
2387 /* Add (or update) attribute to hash. */
2388 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2390 /* Restore route, if needed. */
2391 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2392 bgp_path_info_restore(rn
, pi
);
2394 /* Mark if nexthop has changed. */
2395 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2396 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2398 /* Unintern existing, set to new. */
2399 bgp_attr_unintern(&pi
->attr
);
2400 pi
->attr
= attr_new
;
2401 pi
->uptime
= bgp_clock();
2404 /* Perform route selection and update zebra, if required. */
2405 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2410 * Install route entry into the VRF routing table and invoke route selection.
2412 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2413 struct prefix_evpn
*evp
,
2414 struct bgp_path_info
*parent_pi
)
2416 struct bgp_node
*rn
;
2417 struct bgp_path_info
*pi
;
2419 struct attr
*attr_new
;
2422 struct prefix
*pp
= &p
;
2425 char buf
[PREFIX_STRLEN
];
2426 char buf1
[PREFIX_STRLEN
];
2428 memset(pp
, 0, sizeof(struct prefix
));
2429 ip_prefix_from_evpn_prefix(evp
, pp
);
2431 if (bgp_debug_zebra(NULL
)) {
2433 "installing evpn prefix %s as ip prefix %s in vrf %s",
2434 prefix2str(evp
, buf
, sizeof(buf
)),
2435 prefix2str(pp
, buf1
, sizeof(buf
)),
2436 vrf_id_to_name(bgp_vrf
->vrf_id
));
2439 /* Create (or fetch) route within the VRF. */
2440 /* NOTE: There is no RD here. */
2441 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2443 safi
= SAFI_UNICAST
;
2444 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2445 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2447 safi
= SAFI_UNICAST
;
2448 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2452 /* EVPN routes currently only support a IPv4 next hop which corresponds
2453 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2454 * or prefix route, we have to convert the next hop to an IPv4-mapped
2455 * address for the rest of the code to flow through. In the case of IPv4,
2456 * make sure to set the flag for next hop attribute.
2458 bgp_attr_dup(&attr
, parent_pi
->attr
);
2460 evpn_convert_nexthop_to_ipv6(&attr
);
2462 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2464 /* Check if route entry is already present. */
2465 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2467 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2471 /* Add (or update) attribute to hash. */
2472 attr_new
= bgp_attr_intern(&attr
);
2474 /* Create new route with its attribute. */
2475 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2476 parent_pi
->peer
, attr_new
, rn
);
2477 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2478 bgp_path_info_extra_get(pi
);
2479 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2480 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2481 if (parent_pi
->extra
) {
2482 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2483 sizeof(pi
->extra
->label
));
2484 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2486 bgp_path_info_add(rn
, pi
);
2488 if (attrhash_cmp(pi
->attr
, &attr
)
2489 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2490 bgp_unlock_node(rn
);
2493 /* The attribute has changed. */
2494 /* Add (or update) attribute to hash. */
2495 attr_new
= bgp_attr_intern(&attr
);
2497 /* Restore route, if needed. */
2498 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2499 bgp_path_info_restore(rn
, pi
);
2501 /* Mark if nexthop has changed. */
2503 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2505 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2506 &attr_new
->mp_nexthop_global
)))
2507 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2509 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_ATTR_CHANGED
);
2510 /* Unintern existing, set to new. */
2511 bgp_attr_unintern(&pi
->attr
);
2512 pi
->attr
= attr_new
;
2513 pi
->uptime
= bgp_clock();
2516 bgp_aggregate_increment(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2518 /* Perform route selection and update zebra, if required. */
2519 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2525 * Install route entry into the VNI routing table and invoke route selection.
2527 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2528 struct prefix_evpn
*p
,
2529 struct bgp_path_info
*parent_pi
)
2531 struct bgp_node
*rn
;
2532 struct bgp_path_info
*pi
;
2533 struct attr
*attr_new
;
2536 /* Create (or fetch) route within the VNI. */
2537 /* NOTE: There is no RD here. */
2538 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2540 /* Check if route entry is already present. */
2541 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2543 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2547 /* Add (or update) attribute to hash. */
2548 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2550 /* Create new route with its attribute. */
2551 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2552 parent_pi
->peer
, attr_new
, rn
);
2553 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2554 bgp_path_info_extra_get(pi
);
2555 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2556 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2557 if (parent_pi
->extra
) {
2558 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2559 sizeof(pi
->extra
->label
));
2560 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2562 bgp_path_info_add(rn
, pi
);
2564 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2565 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2566 bgp_unlock_node(rn
);
2569 /* The attribute has changed. */
2570 /* Add (or update) attribute to hash. */
2571 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2573 /* Restore route, if needed. */
2574 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2575 bgp_path_info_restore(rn
, pi
);
2577 /* Mark if nexthop has changed. */
2578 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2579 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2581 /* Unintern existing, set to new. */
2582 bgp_attr_unintern(&pi
->attr
);
2583 pi
->attr
= attr_new
;
2584 pi
->uptime
= bgp_clock();
2587 /* Perform route selection and update zebra, if required. */
2588 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2593 /* Uninstall EVPN route entry from ES route table */
2594 static int uninstall_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2595 struct prefix_evpn
*p
,
2596 struct bgp_path_info
*parent_pi
)
2599 struct bgp_node
*rn
;
2600 struct bgp_path_info
*pi
;
2602 if (!es
->route_table
)
2605 /* Locate route within the ESI.
2606 * NOTE: There is no RD here.
2608 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2612 /* Find matching route entry. */
2613 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2615 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2621 /* Mark entry for deletion */
2622 bgp_path_info_delete(rn
, pi
);
2624 /* Perform route selection and update zebra, if required. */
2625 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2627 /* Unlock route node. */
2628 bgp_unlock_node(rn
);
2634 * Uninstall route entry from the VRF routing table and send message
2635 * to zebra, if appropriate.
2637 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2638 struct prefix_evpn
*evp
,
2639 struct bgp_path_info
*parent_pi
)
2641 struct bgp_node
*rn
;
2642 struct bgp_path_info
*pi
;
2645 struct prefix
*pp
= &p
;
2648 char buf
[PREFIX_STRLEN
];
2649 char buf1
[PREFIX_STRLEN
];
2651 memset(pp
, 0, sizeof(struct prefix
));
2652 ip_prefix_from_evpn_prefix(evp
, pp
);
2654 if (bgp_debug_zebra(NULL
)) {
2656 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2657 prefix2str(evp
, buf
, sizeof(buf
)),
2658 prefix2str(pp
, buf1
, sizeof(buf
)),
2659 vrf_id_to_name(bgp_vrf
->vrf_id
));
2662 /* Locate route within the VRF. */
2663 /* NOTE: There is no RD here. */
2664 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2666 safi
= SAFI_UNICAST
;
2667 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2670 safi
= SAFI_UNICAST
;
2671 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2677 /* Find matching route entry. */
2678 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2680 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2686 bgp_aggregate_decrement(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2688 /* Mark entry for deletion */
2689 bgp_path_info_delete(rn
, pi
);
2691 /* Perform route selection and update zebra, if required. */
2692 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2694 /* Unlock route node. */
2695 bgp_unlock_node(rn
);
2701 * Uninstall route entry from the VNI routing table and send message
2702 * to zebra, if appropriate.
2704 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2705 struct prefix_evpn
*p
,
2706 struct bgp_path_info
*parent_pi
)
2708 struct bgp_node
*rn
;
2709 struct bgp_path_info
*pi
;
2712 /* Locate route within the VNI. */
2713 /* NOTE: There is no RD here. */
2714 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2718 /* Find matching route entry. */
2719 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2721 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2727 /* Mark entry for deletion */
2728 bgp_path_info_delete(rn
, pi
);
2730 /* Perform route selection and update zebra, if required. */
2731 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2733 /* Unlock route node. */
2734 bgp_unlock_node(rn
);
2740 * Given a prefix, see if it belongs to ES.
2742 static int is_prefix_matching_for_es(struct prefix_evpn
*p
,
2745 /* if not an ES route return false */
2746 if (p
->prefix
.route_type
!= BGP_EVPN_ES_ROUTE
)
2749 if (memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
)) == 0)
2756 * Given a route entry and a VRF, see if this route entry should be
2757 * imported into the VRF i.e., RTs match.
2759 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2760 struct bgp_path_info
*pi
)
2762 struct attr
*attr
= pi
->attr
;
2763 struct ecommunity
*ecom
;
2767 /* Route should have valid RT to be even considered. */
2768 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2771 ecom
= attr
->ecommunity
;
2772 if (!ecom
|| !ecom
->size
)
2775 /* For each extended community RT, see if it matches this VNI. If any RT
2776 * matches, we're done.
2778 for (i
= 0; i
< ecom
->size
; i
++) {
2780 uint8_t type
, sub_type
;
2781 struct ecommunity_val
*eval
;
2782 struct ecommunity_val eval_tmp
;
2783 struct vrf_irt_node
*irt
;
2785 /* Only deal with RTs */
2786 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2787 eval
= (struct ecommunity_val
*)(ecom
->val
2788 + (i
* ECOMMUNITY_SIZE
));
2791 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2794 /* See if this RT matches specified VNIs import RTs */
2795 irt
= lookup_vrf_import_rt(eval
);
2797 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2800 /* Also check for non-exact match. In this, we mask out the AS
2802 * only check on the local-admin sub-field. This is to
2804 * VNI as the RT for EBGP peering too.
2807 if (type
== ECOMMUNITY_ENCODE_AS
2808 || type
== ECOMMUNITY_ENCODE_AS4
2809 || type
== ECOMMUNITY_ENCODE_IP
) {
2810 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2811 mask_ecom_global_admin(&eval_tmp
, eval
);
2812 irt
= lookup_vrf_import_rt(&eval_tmp
);
2815 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2823 * Given a route entry and a VNI, see if this route entry should be
2824 * imported into the VNI i.e., RTs match.
2826 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2827 struct bgp_path_info
*pi
)
2829 struct attr
*attr
= pi
->attr
;
2830 struct ecommunity
*ecom
;
2834 /* Route should have valid RT to be even considered. */
2835 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2838 ecom
= attr
->ecommunity
;
2839 if (!ecom
|| !ecom
->size
)
2842 /* For each extended community RT, see if it matches this VNI. If any RT
2843 * matches, we're done.
2845 for (i
= 0; i
< ecom
->size
; i
++) {
2847 uint8_t type
, sub_type
;
2848 struct ecommunity_val
*eval
;
2849 struct ecommunity_val eval_tmp
;
2850 struct irt_node
*irt
;
2852 /* Only deal with RTs */
2853 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2854 eval
= (struct ecommunity_val
*)(ecom
->val
2855 + (i
* ECOMMUNITY_SIZE
));
2858 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2861 /* See if this RT matches specified VNIs import RTs */
2862 irt
= lookup_import_rt(bgp
, eval
);
2864 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2867 /* Also check for non-exact match. In this, we mask out the AS
2869 * only check on the local-admin sub-field. This is to
2871 * VNI as the RT for EBGP peering too.
2874 if (type
== ECOMMUNITY_ENCODE_AS
2875 || type
== ECOMMUNITY_ENCODE_AS4
2876 || type
== ECOMMUNITY_ENCODE_IP
) {
2877 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2878 mask_ecom_global_admin(&eval_tmp
, eval
);
2879 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2882 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2889 static int install_uninstall_routes_for_es(struct bgp
*bgp
,
2896 char buf
[PREFIX_STRLEN
];
2897 char buf1
[ESI_STR_LEN
];
2898 struct bgp_node
*rd_rn
, *rn
;
2899 struct bgp_table
*table
;
2900 struct bgp_path_info
*pi
;
2906 * Walk entire global routing table and evaluate routes which could be
2907 * imported into this VRF. Note that we need to loop through all global
2908 * routes to determine which route matches the import rt on vrf
2910 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2911 rd_rn
= bgp_route_next(rd_rn
)) {
2912 table
= bgp_node_get_bgp_table_info(rd_rn
);
2916 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2917 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2919 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
2922 * Consider "valid" remote routes applicable for
2925 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
2926 && pi
->type
== ZEBRA_ROUTE_BGP
2927 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
2930 if (!is_prefix_matching_for_es(evp
, es
))
2934 ret
= install_evpn_route_entry_in_es(
2937 ret
= uninstall_evpn_route_entry_in_es(
2943 "Failed to %s EVPN %s route in ESI %s",
2946 prefix2str(evp
, buf
,
2948 esi_to_str(&es
->esi
, buf1
,
2959 * Install or uninstall mac-ip routes are appropriate for this
2962 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
2966 struct bgp_node
*rd_rn
, *rn
;
2967 struct bgp_table
*table
;
2968 struct bgp_path_info
*pi
;
2970 char buf
[PREFIX_STRLEN
];
2971 struct bgp
*bgp_def
= NULL
;
2975 bgp_def
= bgp_get_default();
2979 /* Walk entire global routing table and evaluate routes which could be
2980 * imported into this VRF. Note that we need to loop through all global
2981 * routes to determine which route matches the import rt on vrf
2983 for (rd_rn
= bgp_table_top(bgp_def
->rib
[afi
][safi
]); rd_rn
;
2984 rd_rn
= bgp_route_next(rd_rn
)) {
2985 table
= bgp_node_get_bgp_table_info(rd_rn
);
2989 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2990 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2992 /* if not mac-ip route skip this route */
2993 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2994 || evp
->prefix
.route_type
2995 == BGP_EVPN_IP_PREFIX_ROUTE
))
2998 /* if not a mac+ip route skip this route */
2999 if (!(is_evpn_prefix_ipaddr_v4(evp
)
3000 || is_evpn_prefix_ipaddr_v6(evp
)))
3003 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3005 /* Consider "valid" remote routes applicable for
3008 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3009 && pi
->type
== ZEBRA_ROUTE_BGP
3010 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3013 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
3015 ret
= install_evpn_route_entry_in_vrf(
3018 ret
= uninstall_evpn_route_entry_in_vrf(
3024 "Failed to %s EVPN %s route in VRF %s",
3027 prefix2str(evp
, buf
,
3042 * Install or uninstall routes of specified type that are appropriate for this
3045 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
3046 struct bgpevpn
*vpn
,
3047 bgp_evpn_route_type rtype
,
3052 struct bgp_node
*rd_rn
, *rn
;
3053 struct bgp_table
*table
;
3054 struct bgp_path_info
*pi
;
3060 /* Walk entire global routing table and evaluate routes which could be
3061 * imported into this VPN. Note that we cannot just look at the routes
3063 * the VNI's RD - remote routes applicable for this VNI could have any
3066 /* EVPN routes are a 2-level table. */
3067 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
3068 rd_rn
= bgp_route_next(rd_rn
)) {
3069 table
= bgp_node_get_bgp_table_info(rd_rn
);
3073 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3074 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3076 if (evp
->prefix
.route_type
!= rtype
)
3079 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3081 /* Consider "valid" remote routes applicable for
3083 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3084 && pi
->type
== ZEBRA_ROUTE_BGP
3085 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3088 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3090 ret
= install_evpn_route_entry(
3093 ret
= uninstall_evpn_route_entry(
3099 "%u: Failed to %s EVPN %s route in VNI %u",
3103 rtype
== BGP_EVPN_MAC_IP_ROUTE
3117 /* Install any existing remote ES routes applicable for this ES into its routing
3118 * table. This is invoked when ES comes up.
3120 static int install_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
3122 return install_uninstall_routes_for_es(bgp
, es
, 1);
3126 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3127 * is invoked upon l3vni-add or l3vni import rt change
3129 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3131 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3136 * Install any existing remote routes applicable for this VNI into its
3137 * routing table. This is invoked when a VNI becomes "live" or its Import
3140 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3144 /* Install type-3 routes followed by type-2 routes - the ones applicable
3147 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3152 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3156 /* uninstall routes from l3vni vrf. */
3157 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3159 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3164 * Uninstall any existing remote routes for this VNI. One scenario in which
3165 * this is invoked is upon an import RT change.
3167 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3171 /* Uninstall type-2 routes followed by type-3 routes - the ones
3175 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3180 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3184 /* Install or unistall route in ES */
3185 static int install_uninstall_route_in_es(struct bgp
*bgp
, struct evpnes
*es
,
3186 afi_t afi
, safi_t safi
,
3187 struct prefix_evpn
*evp
,
3188 struct bgp_path_info
*pi
, int install
)
3191 char buf
[ESI_STR_LEN
];
3194 ret
= install_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3196 ret
= uninstall_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3201 "%u: Failed to %s EVPN %s route in ESI %s", bgp
->vrf_id
,
3202 install
? "install" : "uninstall", "ES",
3203 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)));
3210 * Install or uninstall route in matching VRFs (list).
3212 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3213 safi_t safi
, struct prefix_evpn
*evp
,
3214 struct bgp_path_info
*pi
,
3215 struct list
*vrfs
, int install
)
3217 char buf
[PREFIX2STR_BUFFER
];
3218 struct bgp
*bgp_vrf
;
3219 struct listnode
*node
, *nnode
;
3221 /* Only type-2/type-5 routes go into a VRF */
3222 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3223 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3226 /* if it is type-2 route and not a mac+ip route skip this route */
3227 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3228 && !(is_evpn_prefix_ipaddr_v4(evp
)
3229 || is_evpn_prefix_ipaddr_v6(evp
)))
3232 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3236 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3238 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3242 flog_err(EC_BGP_EVPN_FAIL
,
3243 "%u: Failed to %s prefix %s in VRF %s",
3245 install
? "install" : "uninstall",
3246 prefix2str(evp
, buf
, sizeof(buf
)),
3247 vrf_id_to_name(bgp_vrf
->vrf_id
));
3256 * Install or uninstall route in matching VNIs (list).
3258 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3259 safi_t safi
, struct prefix_evpn
*evp
,
3260 struct bgp_path_info
*pi
,
3261 struct list
*vnis
, int install
)
3263 struct bgpevpn
*vpn
;
3264 struct listnode
*node
, *nnode
;
3266 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3269 if (!is_vni_live(vpn
))
3273 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3275 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3278 flog_err(EC_BGP_EVPN_FAIL
,
3279 "%u: Failed to %s EVPN %s route in VNI %u",
3280 bgp
->vrf_id
, install
? "install" : "uninstall",
3281 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3293 * Install or uninstall route for appropriate VNIs/ESIs.
3295 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3297 struct bgp_path_info
*pi
, int import
)
3299 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3300 struct attr
*attr
= pi
->attr
;
3301 struct ecommunity
*ecom
;
3306 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3307 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3308 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3309 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3310 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3313 /* If we don't have Route Target, nothing much to do. */
3314 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3317 ecom
= attr
->ecommunity
;
3318 if (!ecom
|| !ecom
->size
)
3321 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3322 * attached to the route */
3323 for (i
= 0; i
< ecom
->size
; i
++) {
3325 uint8_t type
, sub_type
;
3326 struct ecommunity_val
*eval
;
3327 struct ecommunity_val eval_tmp
;
3328 struct irt_node
*irt
; /* import rt for l2vni */
3329 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3332 /* Only deal with RTs */
3333 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3334 eval
= (struct ecommunity_val
*)(ecom
->val
3335 + (i
* ECOMMUNITY_SIZE
));
3338 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3342 * macip routes (type-2) are imported into VNI and VRF tables.
3343 * IMET route is imported into VNI table.
3344 * prefix routes are imported into VRF table.
3346 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3347 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3348 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3350 irt
= lookup_import_rt(bgp
, eval
);
3352 install_uninstall_route_in_vnis(
3353 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3356 vrf_irt
= lookup_vrf_import_rt(eval
);
3358 install_uninstall_route_in_vrfs(
3359 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3362 /* Also check for non-exact match.
3363 * In this, we mask out the AS and
3364 * only check on the local-admin sub-field.
3365 * This is to facilitate using
3366 * VNI as the RT for EBGP peering too.
3370 if (type
== ECOMMUNITY_ENCODE_AS
3371 || type
== ECOMMUNITY_ENCODE_AS4
3372 || type
== ECOMMUNITY_ENCODE_IP
) {
3373 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3374 mask_ecom_global_admin(&eval_tmp
, eval
);
3375 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3376 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3380 install_uninstall_route_in_vnis(
3381 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3384 install_uninstall_route_in_vrfs(
3385 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3389 /* es route is imported into the es table */
3390 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3392 /* we will match based on the entire esi to avoid
3393 * imoort of an es route for esi2 into esi1
3395 es
= bgp_evpn_lookup_es(bgp
, &evp
->prefix
.es_addr
.esi
);
3396 if (es
&& is_es_local(es
))
3397 install_uninstall_route_in_es(
3398 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3406 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3409 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3411 /* Delete ipv4 default route and withdraw from peers */
3412 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3413 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3414 SAFI_UNICAST
, false);
3416 /* delete all ipv4 routes and withdraw from peers */
3417 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3418 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3420 /* Delete ipv6 default route and withdraw from peers */
3421 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3422 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3423 SAFI_UNICAST
, false);
3425 /* delete all ipv6 routes and withdraw from peers */
3426 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3427 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3431 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3434 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3436 /* update all ipv4 routes */
3437 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3438 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3440 /* update ipv4 default route and withdraw from peers */
3441 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3442 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3443 SAFI_UNICAST
, true);
3445 /* update all ipv6 routes */
3446 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3447 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3449 /* update ipv6 default route and withdraw from peers */
3450 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3451 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3452 SAFI_UNICAST
, true);
3457 * update and advertise local routes for a VRF as type-5 routes.
3458 * This is invoked upon RD change for a VRF. Note taht the processing is only
3459 * done in the global route table using the routes which already exist in the
3462 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3464 /* skip if the RD is configured */
3465 if (is_vrf_rd_configured(bgp_vrf
))
3468 /* derive the RD for the VRF based on new router-id */
3469 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3471 /* update advertise ipv4|ipv6 routes as type-5 routes */
3472 update_advertise_vrf_routes(bgp_vrf
);
3476 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3477 * This is invoked upon VRF RD change. The processing is done only from global
3480 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3482 /* skip if the RD is configured */
3483 if (is_vrf_rd_configured(bgp_vrf
))
3486 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3487 delete_withdraw_vrf_routes(bgp_vrf
);
3491 * Update and advertise local routes for a VNI. Invoked upon router-id
3492 * change. Note that the processing is done only on the global route table
3493 * using routes that already exist in the per-VNI table.
3495 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3497 struct prefix_evpn p
;
3498 struct bgp_node
*rn
, *global_rn
;
3499 struct bgp_path_info
*pi
, *global_pi
;
3501 afi_t afi
= AFI_L2VPN
;
3502 safi_t safi
= SAFI_EVPN
;
3504 /* Locate type-3 route for VNI in the per-VNI table and use its
3505 * attributes to create and advertise the type-3 route for this VNI
3506 * in the global table.
3508 * RT-3 only if doing head-end replication
3510 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
3511 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3512 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3513 if (!rn
) /* unexpected */
3515 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3516 if (pi
->peer
== bgp
->peer_self
&&
3517 pi
->type
== ZEBRA_ROUTE_BGP
3518 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3520 if (!pi
) /* unexpected */
3524 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3525 (struct prefix
*)&p
, &vpn
->prd
);
3526 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
,
3527 1, &pi
, 0, mac_mobility_seqnum(attr
));
3529 /* Schedule for processing and unlock node. */
3530 bgp_process(bgp
, global_rn
, afi
, safi
);
3531 bgp_unlock_node(global_rn
);
3534 /* Now, walk this VNI's route table and use the route and its attribute
3535 * to create and schedule route in global table.
3537 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
3538 rn
= bgp_route_next(rn
)) {
3539 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3541 /* Identify MAC-IP local routes. */
3542 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3545 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3546 if (pi
->peer
== bgp
->peer_self
3547 && pi
->type
== ZEBRA_ROUTE_BGP
3548 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3553 /* Create route in global routing table using this route entry's
3557 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3558 (struct prefix
*)evp
, &vpn
->prd
);
3560 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
3562 mac_mobility_seqnum(attr
));
3564 /* Schedule for processing and unlock node. */
3565 bgp_process(bgp
, global_rn
, afi
, safi
);
3566 bgp_unlock_node(global_rn
);
3573 * Delete (and withdraw) local routes for a VNI - only from the global
3574 * table. Invoked upon router-id change.
3576 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3579 struct prefix_evpn p
;
3580 struct bgp_node
*global_rn
;
3581 struct bgp_path_info
*pi
;
3582 afi_t afi
= AFI_L2VPN
;
3583 safi_t safi
= SAFI_EVPN
;
3585 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3586 * for this VNI - from the global table.
3588 ret
= delete_global_type2_routes(bgp
, vpn
);
3592 /* Remove type-3 route for this VNI from global table. */
3593 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3594 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3595 (struct prefix
*)&p
, &vpn
->prd
);
3597 /* Delete route entry in the global EVPN table. */
3598 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
3600 /* Schedule for processing - withdraws to peers happen from
3604 bgp_process(bgp
, global_rn
, afi
, safi
);
3605 bgp_unlock_node(global_rn
);
3612 * Handle router-id change. Update and advertise local routes corresponding
3613 * to this VNI from peers. Note that this is invoked after updating the
3614 * router-id. The routes in the per-VNI table are used to create routes in
3615 * the global table and schedule them.
3617 static void update_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3619 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3621 /* Skip VNIs with configured RD. */
3622 if (is_rd_configured(vpn
))
3625 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3626 update_advertise_vni_routes(bgp
, vpn
);
3630 * Handle router-id change. Delete and withdraw local routes corresponding
3631 * to this VNI from peers. Note that this is invoked prior to updating
3632 * the router-id and is done only on the global route table, the routes
3633 * are needed in the per-VNI table to re-advertise with new router id.
3635 static void withdraw_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3637 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3639 /* Skip VNIs with configured RD. */
3640 if (is_rd_configured(vpn
))
3643 delete_withdraw_vni_routes(bgp
, vpn
);
3647 * Create RT-3 for a VNI and schedule for processing and advertisement.
3648 * This is invoked upon flooding mode changing to head-end replication.
3650 static void create_advertise_type3(struct hash_backet
*backet
, void *data
)
3652 struct bgpevpn
*vpn
= backet
->data
;
3653 struct bgp
*bgp
= data
;
3654 struct prefix_evpn p
;
3656 if (!vpn
|| !is_vni_live(vpn
))
3659 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3660 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0))
3661 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3662 "Type3 route creation failure for VNI %u", vpn
->vni
);
3666 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3667 * This is invoked upon flooding mode changing to drop BUM packets.
3669 static void delete_withdraw_type3(struct hash_backet
*backet
, void *data
)
3671 struct bgpevpn
*vpn
= backet
->data
;
3672 struct bgp
*bgp
= data
;
3673 struct prefix_evpn p
;
3675 if (!vpn
|| !is_vni_live(vpn
))
3678 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3679 delete_evpn_route(bgp
, vpn
, &p
);
3683 * Process received EVPN type-2 route (advertise or withdraw).
3685 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3686 struct attr
*attr
, uint8_t *pfx
, int psize
,
3687 uint32_t addpath_id
)
3689 struct prefix_rd prd
;
3690 struct prefix_evpn p
;
3691 struct bgp_route_evpn evpn
;
3693 uint8_t macaddr_len
;
3694 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
3695 uint32_t num_labels
= 0;
3699 /* Type-2 route should be either 33, 37 or 49 bytes or an
3700 * additional 3 bytes if there is a second label (VNI):
3701 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3702 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3703 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3705 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3706 && psize
!= 40 && psize
!= 52) {
3707 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3708 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3709 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3713 memset(&evpn
, 0, sizeof(evpn
));
3715 /* Make prefix_rd */
3716 prd
.family
= AF_UNSPEC
;
3718 memcpy(&prd
.val
, pfx
, 8);
3721 /* Make EVPN prefix. */
3722 memset(&p
, 0, sizeof(struct prefix_evpn
));
3724 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3725 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3727 /* Copy Ethernet Seg Identifier */
3728 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
3731 /* Copy Ethernet Tag */
3732 memcpy(ð_tag
, pfx
, 4);
3733 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3736 /* Get the MAC Addr len */
3737 macaddr_len
= *pfx
++;
3739 /* Get the MAC Addr */
3740 if (macaddr_len
== (ETH_ALEN
* 8)) {
3741 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
3745 EC_BGP_EVPN_ROUTE_INVALID
,
3746 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3747 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3753 ipaddr_len
= *pfx
++;
3754 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3755 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3757 EC_BGP_EVPN_ROUTE_INVALID
,
3758 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3759 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3764 ipaddr_len
/= 8; /* Convert to bytes. */
3765 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3768 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
3772 /* Get the VNI(s). Stored as bytes here. */
3774 memset(label
, 0, sizeof(label
));
3775 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
3776 pfx
+= BGP_LABEL_BYTES
;
3777 psize
-= (33 + ipaddr_len
);
3778 /* Do we have a second VNI? */
3781 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
3783 * If in future, we are required to access additional fields,
3784 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
3789 /* Process the route. */
3791 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3792 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3793 &prd
, &label
[0], num_labels
, 0, &evpn
);
3795 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3796 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3797 &prd
, &label
[0], num_labels
, &evpn
);
3802 * Process received EVPN type-3 route (advertise or withdraw).
3804 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3805 struct attr
*attr
, uint8_t *pfx
, int psize
,
3806 uint32_t addpath_id
)
3808 struct prefix_rd prd
;
3809 struct prefix_evpn p
;
3814 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3815 * IP len (1) and IP (4 or 16).
3817 if (psize
!= 17 && psize
!= 29) {
3818 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3819 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3820 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3824 /* If PMSI is present, log if it is anything other than IR.
3825 * Note: We just simply ignore the values as it is not clear if
3826 * doing anything else is better.
3829 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3830 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
) {
3832 EC_BGP_EVPN_PMSI_PRESENT
,
3833 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3834 peer
->bgp
->vrf_id
, peer
->host
,
3835 attr
->pmsi_tnl_type
);
3839 /* Make prefix_rd */
3840 prd
.family
= AF_UNSPEC
;
3842 memcpy(&prd
.val
, pfx
, 8);
3845 /* Make EVPN prefix. */
3846 memset(&p
, 0, sizeof(struct prefix_evpn
));
3848 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3849 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3851 /* Copy Ethernet Tag */
3852 memcpy(ð_tag
, pfx
, 4);
3853 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
3857 ipaddr_len
= *pfx
++;
3858 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3859 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
3860 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
3863 EC_BGP_EVPN_ROUTE_INVALID
,
3864 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3865 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3869 /* Process the route. */
3871 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3872 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3873 &prd
, NULL
, 0, 0, NULL
);
3875 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3876 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3877 &prd
, NULL
, 0, NULL
);
3882 * Process received EVPN type-4 route (advertise or withdraw).
3884 static int process_type4_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3885 struct attr
*attr
, uint8_t *pfx
, int psize
,
3886 uint32_t addpath_id
)
3891 struct in_addr vtep_ip
;
3892 struct prefix_rd prd
;
3893 struct prefix_evpn p
;
3895 /* Type-4 route should be either 23 or 35 bytes
3896 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
3898 if (psize
!= 23 && psize
!= 35) {
3899 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3900 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
3901 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3905 /* Make prefix_rd */
3906 prd
.family
= AF_UNSPEC
;
3908 memcpy(&prd
.val
, pfx
, 8);
3912 memcpy(&esi
, pfx
, ESI_BYTES
);
3917 ipaddr_len
= *pfx
++;
3918 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3919 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
3922 EC_BGP_EVPN_ROUTE_INVALID
,
3923 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
3924 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3928 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
3929 /* Process the route. */
3931 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3932 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3933 &prd
, NULL
, 0, 0, NULL
);
3935 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3936 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3937 &prd
, NULL
, 0, NULL
);
3944 * Process received EVPN type-5 route (advertise or withdraw).
3946 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3947 struct attr
*attr
, uint8_t *pfx
, int psize
,
3948 uint32_t addpath_id
, int withdraw
)
3950 struct prefix_rd prd
;
3951 struct prefix_evpn p
;
3952 struct bgp_route_evpn evpn
;
3955 mpls_label_t label
; /* holds the VNI as in the packet */
3958 /* Type-5 route should be 34 or 58 bytes:
3959 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3960 * GW (4 or 16) and VNI (3).
3961 * Note that the IP and GW should both be IPv4 or both IPv6.
3963 if (psize
!= 34 && psize
!= 58) {
3964 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3965 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3966 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3970 /* Make prefix_rd */
3971 prd
.family
= AF_UNSPEC
;
3973 memcpy(&prd
.val
, pfx
, 8);
3976 /* Make EVPN prefix. */
3977 memset(&p
, 0, sizeof(struct prefix_evpn
));
3979 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3980 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
3982 /* Additional information outside of prefix - ESI and GW IP */
3983 memset(&evpn
, 0, sizeof(evpn
));
3986 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
3989 /* Fetch Ethernet Tag. */
3990 memcpy(ð_tag
, pfx
, 4);
3991 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
3994 /* Fetch IP prefix length. */
3996 if (ippfx_len
> IPV6_MAX_BITLEN
) {
3998 EC_BGP_EVPN_ROUTE_INVALID
,
3999 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4000 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
4003 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
4005 /* Determine IPv4 or IPv6 prefix */
4006 /* Since the address and GW are from the same family, this just becomes
4007 * a simple check on the total size.
4010 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
4011 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
4013 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
4016 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
4017 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
4019 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
4023 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4024 memset(&label
, 0, sizeof(label
));
4025 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
4028 * If in future, we are required to access additional fields,
4029 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4033 /* Process the route. */
4035 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4036 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4037 &prd
, &label
, 1, 0, &evpn
);
4039 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4040 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4041 &prd
, &label
, 1, &evpn
);
4046 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
4047 struct prefix_rd
*prd
, mpls_label_t
*label
,
4048 uint32_t num_labels
, struct attr
*attr
)
4052 struct evpn_addr
*p_evpn_p
;
4054 memset(&temp
, 0, 16);
4055 if (p
->family
!= AF_EVPN
)
4057 p_evpn_p
= &(p
->u
.prefix_evpn
);
4059 /* len denites the total len of IP and GW-IP in the route
4060 IP and GW-IP have to be both ipv4 or ipv6
4062 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4063 len
= 8; /* IP and GWIP are both ipv4 */
4065 len
= 32; /* IP and GWIP are both ipv6 */
4066 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4067 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4068 stream_put(s
, prd
->val
, 8);
4070 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
4072 stream_put(s
, &temp
, 10);
4073 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4074 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4075 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4076 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4078 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4080 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4082 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
4084 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
4086 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4087 stream_put_ipv4(s
, 0);
4089 stream_put(s
, &temp
, 16);
4093 stream_put(s
, label
, 3);
4099 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4101 static void cleanup_vni_on_disable(struct hash_backet
*backet
, struct bgp
*bgp
)
4103 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
4105 /* Remove EVPN routes and schedule for processing. */
4106 delete_routes_for_vni(bgp
, vpn
);
4108 /* Clear "live" flag and see if hash needs to be freed. */
4109 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4110 if (!is_vni_configured(vpn
))
4111 bgp_evpn_free(bgp
, vpn
);
4115 * Free a VNI entry; iterator function called during cleanup.
4117 static void free_vni_entry(struct hash_backet
*backet
, struct bgp
*bgp
)
4119 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
4121 delete_all_vni_routes(bgp
, vpn
);
4122 bgp_evpn_free(bgp
, vpn
);
4126 * Derive AUTO import RT for BGP VRF - L3VNI
4128 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4130 struct bgp
*bgp_def
= NULL
;
4132 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4133 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4136 bgp_def
= bgp_get_default();
4139 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4143 * Delete AUTO import RT from BGP VRF - L3VNI
4145 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4147 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4151 * Derive AUTO export RT for BGP VRF - L3VNI
4153 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4155 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4156 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4160 * Delete AUTO export RT from BGP VRF - L3VNI
4162 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4164 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4167 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4169 struct bgp
*bgp_def
= NULL
;
4170 struct listnode
*node
= NULL
;
4171 struct bgpevpn
*vpn
= NULL
;
4173 bgp_def
= bgp_get_default();
4177 /* update all type-5 routes */
4178 update_advertise_vrf_routes(bgp_vrf
);
4180 /* update all type-2 routes */
4181 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4182 update_routes_for_vni(bgp_def
, vpn
);
4186 * Handle autort change for a given VNI.
4188 static void update_autort_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
4190 struct bgpevpn
*vpn
= backet
->data
;
4192 if (!is_import_rt_configured(vpn
)) {
4193 if (is_vni_live(vpn
))
4194 bgp_evpn_uninstall_routes(bgp
, vpn
);
4195 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4196 list_delete_all_node(vpn
->import_rtl
);
4197 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4198 if (is_vni_live(vpn
))
4199 bgp_evpn_install_routes(bgp
, vpn
);
4201 if (!is_export_rt_configured(vpn
)) {
4202 list_delete_all_node(vpn
->export_rtl
);
4203 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4204 if (is_vni_live(vpn
))
4205 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4213 /* withdraw type-5 route corresponding to ip prefix */
4214 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4215 afi_t afi
, safi_t safi
)
4218 struct prefix_evpn evp
;
4219 char buf
[PREFIX_STRLEN
];
4221 build_type5_prefix_from_ip_prefix(&evp
, p
);
4222 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4225 EC_BGP_EVPN_ROUTE_DELETE
,
4226 "%u failed to delete type-5 route for prefix %s in vrf %s",
4227 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4228 vrf_id_to_name(bgp_vrf
->vrf_id
));
4232 /* withdraw all type-5 routes for an address family */
4233 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4235 struct bgp_table
*table
= NULL
;
4236 struct bgp_node
*rn
= NULL
;
4237 struct bgp_path_info
*pi
;
4239 table
= bgp_vrf
->rib
[afi
][safi
];
4240 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4241 /* Only care about "selected" routes - non-imported. */
4242 /* TODO: Support for AddPath for EVPN. */
4243 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4244 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4245 && (!pi
->extra
|| !pi
->extra
->parent
)) {
4246 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
4255 * evpn - enable advertisement of default g/w
4257 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
4258 safi_t safi
, bool add
)
4260 struct prefix ip_prefix
;
4262 /* form the default prefix 0.0.0.0/0 */
4263 memset(&ip_prefix
, 0, sizeof(struct prefix
));
4264 ip_prefix
.family
= afi2family(afi
);
4267 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
4270 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
4277 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4278 * to this function correspond to those of the source IP prefix (best
4279 * path in the case of the attr. In the case of a local prefix (when we
4280 * are advertising local subnets), the src_attr will be NULL.
4282 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4283 struct attr
*src_attr
, afi_t afi
,
4287 struct prefix_evpn evp
;
4288 char buf
[PREFIX_STRLEN
];
4290 build_type5_prefix_from_ip_prefix(&evp
, p
);
4291 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4293 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4294 "%u: Failed to create type-5 route for prefix %s",
4295 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4298 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4299 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4300 * advertisement is enabled.
4302 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4305 struct bgp_table
*table
= NULL
;
4306 struct bgp_node
*rn
= NULL
;
4307 struct bgp_path_info
*pi
;
4309 table
= bgp_vrf
->rib
[afi
][safi
];
4310 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4311 /* Need to identify the "selected" route entry to use its
4312 * attribute. Also, we only consider "non-imported" routes.
4313 * TODO: Support for AddPath for EVPN.
4315 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4316 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4317 && (!pi
->extra
|| !pi
->extra
->parent
)) {
4319 /* apply the route-map */
4320 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4323 ret
= route_map_apply(
4324 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4326 &rn
->p
, RMAP_BGP
, pi
);
4327 if (ret
== RMAP_DENYMATCH
)
4330 bgp_evpn_advertise_type5_route(
4331 bgp_vrf
, &rn
->p
, pi
->attr
, afi
, safi
);
4338 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4340 struct listnode
*node
, *nnode
, *node_to_del
;
4341 struct ecommunity
*ecom
, *ecom_auto
;
4342 struct ecommunity_val eval
;
4344 if (bgp
->advertise_autort_rfc8365
)
4345 vni
|= EVPN_AUTORT_VXLAN
;
4346 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4348 ecom_auto
= ecommunity_new();
4349 ecommunity_add_val(ecom_auto
, &eval
);
4352 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4353 if (ecommunity_match(ecom
, ecom_auto
)) {
4354 ecommunity_free(&ecom
);
4360 list_delete_node(rtl
, node_to_del
);
4362 ecommunity_free(&ecom_auto
);
4365 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4366 struct ecommunity
*ecomadd
)
4368 /* uninstall routes from vrf */
4369 uninstall_routes_for_vrf(bgp_vrf
);
4371 /* Cleanup the RT to VRF mapping */
4372 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4374 /* Remove auto generated RT */
4375 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4377 /* Add the newly configured RT to RT list */
4378 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4379 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4381 /* map VRF to its RTs */
4382 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4384 /* install routes matching the new VRF */
4385 install_routes_for_vrf(bgp_vrf
);
4388 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4389 struct ecommunity
*ecomdel
)
4391 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4392 struct ecommunity
*ecom
= NULL
;
4394 /* uninstall routes from vrf */
4395 uninstall_routes_for_vrf(bgp_vrf
);
4397 /* Cleanup the RT to VRF mapping */
4398 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4400 /* remove the RT from the RT list */
4401 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4402 if (ecommunity_match(ecom
, ecomdel
)) {
4403 ecommunity_free(&ecom
);
4410 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4412 assert(bgp_vrf
->vrf_import_rtl
);
4413 /* fallback to auto import rt, if this was the last RT */
4414 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4415 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4416 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4419 /* map VRFs to its RTs */
4420 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4422 /* install routes matching this new RT */
4423 install_routes_for_vrf(bgp_vrf
);
4426 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4427 struct ecommunity
*ecomadd
)
4429 /* remove auto-generated RT */
4430 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4432 /* Add the new RT to the RT list */
4433 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4434 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4436 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4439 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4440 struct ecommunity
*ecomdel
)
4442 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4443 struct ecommunity
*ecom
= NULL
;
4445 /* Remove the RT from the RT list */
4446 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4447 if (ecommunity_match(ecom
, ecomdel
)) {
4448 ecommunity_free(&ecom
);
4455 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4458 * Temporary assert to make SA happy.
4459 * The ALL_LIST_ELEMENTS macro above has a NULL check
4460 * which means that SA is going to complain about
4461 * the list_isempty call, which doesn't NULL check.
4462 * So until we get this situation cleaned up, here
4465 assert(bgp_vrf
->vrf_export_rtl
);
4467 /* fall back to auto-generated RT if this was the last RT */
4468 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4469 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4470 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4473 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4477 * Handle change to BGP router id. This is invoked twice by the change
4478 * handler, first before the router id has been changed and then after
4479 * the router id has been changed. The first invocation will result in
4480 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4481 * will result in the routes being re-advertised.
4483 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4487 /* delete and withdraw all the type-5 routes
4488 stored in the global table for this vrf
4490 withdraw_router_id_vrf(bgp
);
4492 /* delete all the VNI routes (type-2/type-3) routes for all the
4495 hash_iterate(bgp
->vnihash
,
4496 (void (*)(struct hash_backet
*,
4497 void *))withdraw_router_id_vni
,
4501 /* advertise all routes in the vrf as type-5 routes with the new
4504 update_router_id_vrf(bgp
);
4506 /* advertise all the VNI routes (type-2/type-3) routes with the
4509 hash_iterate(bgp
->vnihash
,
4510 (void (*)(struct hash_backet
*,
4511 void *))update_router_id_vni
,
4517 * Handle change to auto-RT algorithm - update and advertise local routes.
4519 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4521 hash_iterate(bgp
->vnihash
,
4522 (void (*)(struct hash_backet
*,
4523 void*))update_autort_vni
,
4528 * Handle change to export RT - update and advertise local routes.
4530 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4532 return update_routes_for_vni(bgp
, vpn
);
4535 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4538 delete_withdraw_vrf_routes(bgp_vrf
);
4540 update_advertise_vrf_routes(bgp_vrf
);
4544 * Handle change to RD. This is invoked twice by the change handler,
4545 * first before the RD has been changed and then after the RD has
4546 * been changed. The first invocation will result in local routes
4547 * of this VNI being deleted and withdrawn and the next will result
4548 * in the routes being re-advertised.
4550 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4554 delete_withdraw_vni_routes(bgp
, vpn
);
4556 update_advertise_vni_routes(bgp
, vpn
);
4560 * Install routes for this VNI. Invoked upon change to Import RT.
4562 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4564 return install_routes_for_vni(bgp
, vpn
);
4568 * Uninstall all routes installed for this VNI. Invoked upon change
4571 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4573 return uninstall_routes_for_vni(bgp
, vpn
);
4577 * TODO: Hardcoded for a maximum of 2 VNIs right now
4579 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4584 vni1
= label2vni(label
);
4585 if (num_labels
== 2) {
4586 vni2
= label2vni(label
+ 1);
4587 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4589 snprintf(buf
, len
, "%u", vni1
);
4594 * Function to convert evpn route to json format.
4595 * NOTE: We don't use prefix2str as the output here is a bit different.
4597 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
4599 char buf1
[ETHER_ADDR_STRLEN
];
4600 char buf2
[PREFIX2STR_BUFFER
];
4605 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4606 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4607 json_object_int_add(json
, "ethTag",
4608 p
->prefix
.imet_addr
.eth_tag
);
4609 json_object_int_add(json
, "ipLen",
4610 is_evpn_prefix_ipaddr_v4(p
)
4613 json_object_string_add(json
, "ip",
4614 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4615 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4616 if (is_evpn_prefix_ipaddr_none(p
)) {
4617 json_object_int_add(json
, "routeType",
4618 p
->prefix
.route_type
);
4619 json_object_int_add(json
, "ethTag",
4620 p
->prefix
.macip_addr
.eth_tag
);
4621 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4622 json_object_string_add(json
, "mac",
4623 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4629 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4632 json_object_int_add(json
, "routeType",
4633 p
->prefix
.route_type
);
4634 json_object_int_add(json
, "ethTag",
4635 p
->prefix
.macip_addr
.eth_tag
);
4636 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4637 json_object_string_add(json
, "mac",
4638 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4641 json_object_int_add(json
, "ipLen",
4642 is_evpn_prefix_ipaddr_v4(p
)
4645 json_object_string_add(
4648 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4650 PREFIX2STR_BUFFER
));
4653 /* Currently, this is to cater to other AF_ETHERNET code. */
4658 * Function to convert evpn route to string.
4659 * NOTE: We don't use prefix2str as the output here is a bit different.
4661 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
4663 char buf1
[ETHER_ADDR_STRLEN
];
4664 char buf2
[PREFIX2STR_BUFFER
];
4665 char buf3
[ESI_STR_LEN
];
4667 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4668 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
4669 p
->prefix
.imet_addr
.eth_tag
,
4670 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4672 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4673 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4674 if (is_evpn_prefix_ipaddr_none(p
))
4675 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4676 p
->prefix
.route_type
,
4677 p
->prefix
.macip_addr
.eth_tag
,
4679 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4684 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4686 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
4687 p
->prefix
.route_type
,
4688 p
->prefix
.macip_addr
.eth_tag
,
4690 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4692 family
== AF_INET
? IPV4_MAX_BITLEN
4695 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4697 PREFIX2STR_BUFFER
));
4699 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
4700 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4701 p
->prefix
.route_type
,
4702 p
->prefix
.prefix_addr
.eth_tag
,
4703 p
->prefix
.prefix_addr
.ip_prefix_length
,
4704 is_evpn_prefix_ipaddr_v4(p
)
4705 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
4706 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
4707 } else if (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
4708 snprintf(buf
, len
, "[%d]:[%s]:[%d]:[%s]",
4709 p
->prefix
.route_type
,
4710 esi_to_str(&p
->prefix
.es_addr
.esi
, buf3
, sizeof(buf3
)),
4711 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4713 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
));
4715 /* For EVPN route types not supported yet. */
4716 snprintf(buf
, len
, "(unsupported route type %d)",
4717 p
->prefix
.route_type
);
4724 * Encode EVPN prefix in Update (MP_REACH)
4726 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
4727 struct prefix_rd
*prd
, mpls_label_t
*label
,
4728 uint32_t num_labels
, struct attr
*attr
,
4729 int addpath_encode
, uint32_t addpath_tx_id
)
4731 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4732 int len
, ipa_len
= 0;
4735 stream_putl(s
, addpath_tx_id
);
4738 stream_putc(s
, evp
->prefix
.route_type
);
4740 switch (evp
->prefix
.route_type
) {
4741 case BGP_EVPN_MAC_IP_ROUTE
:
4742 if (is_evpn_prefix_ipaddr_v4(evp
))
4743 ipa_len
= IPV4_MAX_BYTELEN
;
4744 else if (is_evpn_prefix_ipaddr_v6(evp
))
4745 ipa_len
= IPV6_MAX_BYTELEN
;
4746 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4747 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4748 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4750 stream_putc(s
, len
);
4751 stream_put(s
, prd
->val
, 8); /* RD */
4753 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
4755 stream_put(s
, 0, 10);
4756 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4757 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4758 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4759 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4760 if (ipa_len
) /* IP */
4761 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4763 /* 1st label is the L2 VNI */
4764 stream_put(s
, label
, BGP_LABEL_BYTES
);
4765 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4766 if (ipa_len
&& num_labels
> 1)
4767 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4770 case BGP_EVPN_IMET_ROUTE
:
4771 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4772 stream_put(s
, prd
->val
, 8); /* RD */
4773 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4774 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4775 /* Originating Router's IP Addr */
4776 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4779 case BGP_EVPN_ES_ROUTE
:
4780 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4781 stream_put(s
, prd
->val
, 8); /* RD */
4782 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4783 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4785 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4788 case BGP_EVPN_IP_PREFIX_ROUTE
:
4789 /* TODO: AddPath support. */
4790 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4798 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4799 struct bgp_nlri
*packet
, int withdraw
)
4805 uint32_t addpath_id
;
4806 int addpath_encoded
;
4811 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4813 lim
= pnt
+ packet
->length
;
4815 safi
= packet
->safi
;
4819 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
4820 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
4821 PEER_CAP_ADDPATH_AF_TX_RCV
));
4823 for (; pnt
< lim
; pnt
+= psize
) {
4824 /* Clear prefix structure. */
4825 memset(&p
, 0, sizeof(struct prefix
));
4827 /* Deal with path-id if AddPath is supported. */
4828 if (addpath_encoded
) {
4829 /* When packet overflow occurs return immediately. */
4830 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4833 addpath_id
= ntohl(*((uint32_t *)pnt
));
4834 pnt
+= BGP_ADDPATH_ID_LEN
;
4837 /* All EVPN NLRI types start with type and length. */
4844 /* When packet overflow occur return immediately. */
4845 if (pnt
+ psize
> lim
)
4849 case BGP_EVPN_MAC_IP_ROUTE
:
4850 if (process_type2_route(peer
, afi
, safi
,
4851 withdraw
? NULL
: attr
, pnt
,
4852 psize
, addpath_id
)) {
4855 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4856 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4861 case BGP_EVPN_IMET_ROUTE
:
4862 if (process_type3_route(peer
, afi
, safi
,
4863 withdraw
? NULL
: attr
, pnt
,
4864 psize
, addpath_id
)) {
4867 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4868 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4873 case BGP_EVPN_ES_ROUTE
:
4874 if (process_type4_route(peer
, afi
, safi
,
4875 withdraw
? NULL
: attr
, pnt
,
4876 psize
, addpath_id
)) {
4879 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4880 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4885 case BGP_EVPN_IP_PREFIX_ROUTE
:
4886 if (process_type5_route(peer
, afi
, safi
, attr
, pnt
,
4887 psize
, addpath_id
, withdraw
)) {
4890 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4891 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4901 /* Packet length consistency check. */
4909 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
4910 * The mapping will be used during route processing.
4911 * bgp_def: default bgp instance
4912 * bgp_vrf: specific bgp vrf instance on which RT is configured
4914 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
4917 struct ecommunity_val
*eval
= NULL
;
4918 struct listnode
*node
= NULL
, *nnode
= NULL
;
4919 struct ecommunity
*ecom
= NULL
;
4921 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4922 for (i
= 0; i
< ecom
->size
; i
++) {
4923 eval
= (struct ecommunity_val
*)(ecom
->val
4925 * ECOMMUNITY_SIZE
));
4926 map_vrf_to_rt(bgp_vrf
, eval
);
4932 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
4934 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
4937 struct ecommunity_val
*eval
;
4938 struct listnode
*node
, *nnode
;
4939 struct ecommunity
*ecom
;
4941 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4942 for (i
= 0; i
< ecom
->size
; i
++) {
4943 struct vrf_irt_node
*irt
;
4944 struct ecommunity_val eval_tmp
;
4946 eval
= (struct ecommunity_val
*)(ecom
->val
4948 * ECOMMUNITY_SIZE
));
4949 /* If using "automatic" RT, we only care about the
4950 * local-admin sub-field.
4951 * This is to facilitate using VNI as the RT for EBGP
4954 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4955 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
4956 BGP_VRF_IMPORT_RT_CFGD
))
4957 mask_ecom_global_admin(&eval_tmp
, eval
);
4959 irt
= lookup_vrf_import_rt(&eval_tmp
);
4961 unmap_vrf_from_rt(bgp_vrf
, irt
);
4968 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
4969 * The mapping will be used during route processing.
4971 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4974 struct ecommunity_val
*eval
;
4975 struct listnode
*node
, *nnode
;
4976 struct ecommunity
*ecom
;
4978 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4979 for (i
= 0; i
< ecom
->size
; i
++) {
4980 eval
= (struct ecommunity_val
*)(ecom
->val
4982 * ECOMMUNITY_SIZE
));
4983 map_vni_to_rt(bgp
, vpn
, eval
);
4989 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
4991 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4994 struct ecommunity_val
*eval
;
4995 struct listnode
*node
, *nnode
;
4996 struct ecommunity
*ecom
;
4998 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4999 for (i
= 0; i
< ecom
->size
; i
++) {
5000 struct irt_node
*irt
;
5001 struct ecommunity_val eval_tmp
;
5003 eval
= (struct ecommunity_val
*)(ecom
->val
5005 * ECOMMUNITY_SIZE
));
5006 /* If using "automatic" RT, we only care about the
5007 * local-admin sub-field.
5008 * This is to facilitate using VNI as the RT for EBGP
5011 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5012 if (!is_import_rt_configured(vpn
))
5013 mask_ecom_global_admin(&eval_tmp
, eval
);
5015 irt
= lookup_import_rt(bgp
, &eval_tmp
);
5017 unmap_vni_from_rt(bgp
, vpn
, irt
);
5023 * Derive Import RT automatically for VNI and map VNI to RT.
5024 * The mapping will be used during route processing.
5026 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5028 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
5029 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
5032 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
5036 * Derive Export RT automatically for VNI.
5038 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5040 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
5041 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5045 * Derive RD automatically for VNI using passed information - it
5046 * is of the form RouterId:unique-id-for-vni.
5048 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5050 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5054 * Derive RD automatically for VNI using passed information - it
5055 * is of the form RouterId:unique-id-for-vni.
5057 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5061 vpn
->prd
.family
= AF_UNSPEC
;
5062 vpn
->prd
.prefixlen
= 64;
5063 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
5064 (void)str2prefix_rd(buf
, &vpn
->prd
);
5065 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5071 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5073 struct list
*inst
= bm
->bgp
;
5074 struct listnode
*node
;
5075 struct bgp
*bgp_vrf
;
5077 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5078 if (bgp_vrf
->l3vni
== vni
)
5088 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5090 struct bgpevpn
*vpn
;
5093 memset(&tmp
, 0, sizeof(struct bgpevpn
));
5095 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5100 * Create a new vpn - invoked upon configuration or zebra notification.
5102 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5103 struct in_addr originator_ip
,
5104 vrf_id_t tenant_vrf_id
)
5106 struct bgpevpn
*vpn
;
5111 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5115 /* Set values - RD and RT set to defaults. */
5117 vpn
->originator_ip
= originator_ip
;
5118 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5120 /* Initialize route-target import and export lists */
5121 vpn
->import_rtl
= list_new();
5122 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5123 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
5124 vpn
->export_rtl
= list_new();
5125 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5126 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
5127 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5128 derive_rd_rt_for_vni(bgp
, vpn
);
5130 /* Initialize EVPN route table. */
5131 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5134 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
5135 XFREE(MTYPE_BGP_EVPN
, vpn
);
5139 /* add to l2vni list on corresponding vrf */
5140 bgpevpn_link_to_l3vni(vpn
);
5142 QOBJ_REG(vpn
, bgpevpn
);
5147 * Free a given VPN - called in multiple scenarios such as zebra
5148 * notification, configuration being deleted, advertise-all-vni disabled etc.
5149 * This just frees appropriate memory, caller should have taken other
5152 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5154 bgpevpn_unlink_from_l3vni(vpn
);
5155 bgp_table_unlock(vpn
->route_table
);
5156 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5157 list_delete(&vpn
->import_rtl
);
5158 list_delete(&vpn
->export_rtl
);
5159 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5160 hash_release(bgp
->vnihash
, vpn
);
5162 XFREE(MTYPE_BGP_EVPN
, vpn
);
5168 struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
)
5173 memset(&tmp
, 0, sizeof(struct evpnes
));
5174 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
5175 es
= hash_lookup(bgp
->esihash
, &tmp
);
5180 * Create a new local es - invoked upon zebra notification.
5182 struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
,
5184 struct ipaddr
*originator_ip
)
5192 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct evpnes
));
5196 /* set the ESI and originator_ip */
5197 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
5198 memcpy(&es
->originator_ip
, originator_ip
, sizeof(struct ipaddr
));
5200 /* Initialise the VTEP list */
5201 es
->vtep_list
= list_new();
5202 es
->vtep_list
->cmp
= evpn_vtep_ip_cmp
;
5204 /* auto derive RD for this es */
5205 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
5206 es
->prd
.family
= AF_UNSPEC
;
5207 es
->prd
.prefixlen
= 64;
5208 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), es
->rd_id
);
5209 (void)str2prefix_rd(buf
, &es
->prd
);
5211 /* Initialize the ES route table */
5212 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5215 if (!hash_get(bgp
->esihash
, es
, hash_alloc_intern
)) {
5216 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5220 QOBJ_REG(es
, evpnes
);
5226 * This just frees appropriate memory, caller should have taken other
5229 void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
)
5231 list_delete(&es
->vtep_list
);
5232 bgp_table_unlock(es
->route_table
);
5233 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
5234 hash_release(bgp
->esihash
, es
);
5236 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5240 * Import evpn route from global table to VNI/VRF/ESI.
5242 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5243 struct prefix
*p
, struct bgp_path_info
*pi
)
5245 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5249 * Unimport evpn route from VNI/VRF/ESI.
5251 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5252 struct prefix
*p
, struct bgp_path_info
*pi
)
5254 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5257 /* filter routes which have martian next hops */
5258 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5262 struct bgp_node
*rd_rn
, *rn
;
5263 struct bgp_table
*table
;
5264 struct bgp_path_info
*pi
;
5269 /* Walk entire global routing table and evaluate routes which could be
5270 * imported into this VPN. Note that we cannot just look at the routes
5271 * for the VNI's RD -
5272 * remote routes applicable for this VNI could have any RD.
5274 /* EVPN routes are a 2-level table. */
5275 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
5276 rd_rn
= bgp_route_next(rd_rn
)) {
5277 table
= bgp_node_get_bgp_table_info(rd_rn
);
5281 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5283 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
5286 /* Consider "valid" remote routes applicable for
5288 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5289 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5292 if (bgp_nexthop_self(bgp
, pi
->attr
->nexthop
)) {
5294 char attr_str
[BUFSIZ
];
5295 char pbuf
[PREFIX_STRLEN
];
5297 bgp_dump_attr(pi
->attr
, attr_str
,
5300 if (bgp_debug_update(pi
->peer
, &rn
->p
,
5303 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
5310 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5313 bgp_rib_remove(rn
, pi
, pi
->peer
, afi
,
5324 * Handle del of a local MACIP.
5326 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5329 struct bgpevpn
*vpn
;
5330 struct prefix_evpn p
;
5332 /* Lookup VNI hash - should exist. */
5333 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5334 if (!vpn
|| !is_vni_live(vpn
)) {
5335 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5336 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5337 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5341 /* Remove EVPN type-2 route and schedule for processing. */
5342 build_evpn_type2_prefix(&p
, mac
, ip
);
5343 delete_evpn_route(bgp
, vpn
, &p
);
5349 * Handle add of a local MACIP.
5351 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5352 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
)
5354 struct bgpevpn
*vpn
;
5355 struct prefix_evpn p
;
5357 /* Lookup VNI hash - should exist. */
5358 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5359 if (!vpn
|| !is_vni_live(vpn
)) {
5360 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5361 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5362 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5366 /* Create EVPN type-2 route and schedule for processing. */
5367 build_evpn_type2_prefix(&p
, mac
, ip
);
5368 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
)) {
5369 char buf
[ETHER_ADDR_STRLEN
];
5370 char buf2
[INET6_ADDRSTRLEN
];
5373 EC_BGP_EVPN_ROUTE_CREATE
,
5374 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5375 bgp
->vrf_id
, vpn
->vni
,
5376 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5379 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5380 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5387 static void link_l2vni_hash_to_l3vni(struct hash_backet
*backet
,
5388 struct bgp
*bgp_vrf
)
5390 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
5391 struct bgp
*bgp_def
= NULL
;
5393 bgp_def
= bgp_get_default();
5396 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5397 bgpevpn_link_to_l3vni(vpn
);
5400 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
, struct ethaddr
*rmac
,
5401 struct in_addr originator_ip
, int filter
)
5403 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5404 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5405 struct listnode
*node
= NULL
;
5406 struct bgpevpn
*vpn
= NULL
;
5409 /* get the default instance - required to get the AS number for VRF
5412 bgp_def
= bgp_get_default();
5416 "Cannot process L3VNI %u ADD - default BGP instance not yet created",
5422 /* if the BGP vrf instance doesn't exist - create one */
5423 bgp_vrf
= bgp_lookup_by_name(vrf_id_to_name(vrf_id
));
5428 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5429 BGP_INSTANCE_TYPE_VRF
);
5431 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
:
5432 flog_err(EC_BGP_MULTI_INSTANCE
,
5433 "'bgp multiple-instance' not present\n");
5435 case BGP_ERR_AS_MISMATCH
:
5436 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5437 "BGP is already running; AS is %u\n", as
);
5439 case BGP_ERR_INSTANCE_MISMATCH
:
5440 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5441 "BGP instance name and AS number mismatch\n");
5445 /* mark as auto created */
5446 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5449 /* associate with l3vni */
5450 bgp_vrf
->l3vni
= l3vni
;
5452 /* set the router mac - to be used in mac-ip routes for this vrf */
5453 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
5455 /* set the originator ip */
5456 bgp_vrf
->originator_ip
= originator_ip
;
5458 /* set the right filter - are we using l3vni only for prefix routes? */
5460 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5462 /* Map auto derive or configured RTs */
5463 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5464 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5466 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
5468 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5469 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5471 /* auto derive RD */
5472 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5474 /* link all corresponding l2vnis */
5475 hash_iterate(bgp_def
->vnihash
,
5476 (void (*)(struct hash_backet
*,
5477 void *))link_l2vni_hash_to_l3vni
,
5480 /* Only update all corresponding type-2 routes if we are advertising two
5481 * labels along with type-2 routes
5484 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5485 update_routes_for_vni(bgp_def
, vpn
);
5487 /* advertise type-5 routes if needed */
5488 update_advertise_vrf_routes(bgp_vrf
);
5490 /* install all remote routes belonging to this l3vni into correspondng
5492 install_routes_for_vrf(bgp_vrf
);
5497 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5499 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5500 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5501 struct listnode
*node
= NULL
;
5502 struct listnode
*next
= NULL
;
5503 struct bgpevpn
*vpn
= NULL
;
5505 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5509 "Cannot process L3VNI %u Del - Could not find BGP instance",
5514 bgp_def
= bgp_get_default();
5518 "Cannot process L3VNI %u Del - Could not find default BGP instance",
5523 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5524 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5525 * routes. This will uninstalling the routes from zebra and decremnt the
5528 uninstall_routes_for_vrf(bgp_vrf
);
5530 /* delete/withdraw all type-5 routes */
5531 delete_withdraw_vrf_routes(bgp_vrf
);
5533 /* remove the l3vni from vrf instance */
5536 /* remove the Rmac from the BGP vrf */
5537 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5539 /* remove default import RT or Unmap non-default import RT */
5540 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5541 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5542 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5543 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5546 /* remove default export RT */
5547 if (!list_isempty(bgp_vrf
->vrf_export_rtl
) &&
5548 !CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
5549 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5552 /* update all corresponding local mac-ip routes */
5553 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5554 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5555 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5556 update_routes_for_vni(bgp_def
, vpn
);
5560 /* If any L2VNIs point to this instance, unlink them. */
5561 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5562 bgpevpn_unlink_from_l3vni(vpn
);
5564 /* Delete the instance if it was autocreated */
5565 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5566 bgp_delete(bgp_vrf
);
5572 * Handle del of a local VNI.
5574 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5576 struct bgpevpn
*vpn
;
5578 /* Locate VNI hash */
5579 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5581 if (bgp_debug_zebra(NULL
))
5583 EC_BGP_EVPN_VPN_VNI
,
5584 "%u: VNI hash entry for VNI %u not found at DEL",
5589 /* Remove all local EVPN routes and schedule for processing (to
5590 * withdraw from peers).
5592 delete_routes_for_vni(bgp
, vpn
);
5595 * tunnel is no longer active, del tunnel ip address from tip_hash
5597 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5599 /* Clear "live" flag and see if hash needs to be freed. */
5600 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5601 if (!is_vni_configured(vpn
))
5602 bgp_evpn_free(bgp
, vpn
);
5608 * Handle add (or update) of a local VNI. The VNI changes we care
5609 * about are for the local-tunnel-ip and the (tenant) VRF.
5611 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5612 struct in_addr originator_ip
, vrf_id_t tenant_vrf_id
)
5614 struct bgpevpn
*vpn
;
5615 struct prefix_evpn p
;
5617 /* Lookup VNI. If present and no change, exit. */
5618 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5621 if (is_vni_live(vpn
)
5622 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5623 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5624 /* Probably some other param has changed that we don't
5628 /* Update tenant_vrf_id if it has changed. */
5629 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5630 bgpevpn_unlink_from_l3vni(vpn
);
5631 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5632 bgpevpn_link_to_l3vni(vpn
);
5635 /* If tunnel endpoint IP has changed, update (and delete prior
5636 * type-3 route, if needed.)
5638 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5639 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5641 /* Update all routes with new endpoint IP and/or export RT
5644 if (is_vni_live(vpn
))
5645 update_routes_for_vni(bgp
, vpn
);
5648 /* Create or update as appropriate. */
5650 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
);
5654 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5660 /* if the VNI is live already, there is nothing more to do */
5661 if (is_vni_live(vpn
))
5664 /* Mark as "live" */
5665 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5667 /* tunnel is now active, add tunnel-ip to db */
5668 bgp_tip_add(bgp
, &originator_ip
);
5670 /* filter routes as nexthop database has changed */
5671 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5674 * Create EVPN type-3 route and schedule for processing.
5676 * RT-3 only if doing head-end replication
5678 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
5679 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5680 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0)) {
5681 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5682 "%u: Type3 route creation failure for VNI %u",
5688 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5692 install_routes_for_vni(bgp
, vpn
);
5694 /* If we are advertising gateway mac-ip
5695 It needs to be conveyed again to zebra */
5696 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5702 * bgp_evpn_local_es_del
5704 int bgp_evpn_local_es_del(struct bgp
*bgp
,
5706 struct ipaddr
*originator_ip
)
5708 char buf
[ESI_STR_LEN
];
5709 struct evpnes
*es
= NULL
;
5711 if (!bgp
->esihash
) {
5712 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5717 /* Lookup ESI hash - should exist. */
5718 es
= bgp_evpn_lookup_es(bgp
, esi
);
5720 flog_warn(EC_BGP_EVPN_ESI
,
5721 "%u: ESI hash entry for ESI %s at Local ES DEL",
5722 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5726 /* Delete all local EVPN ES routes from ESI table
5727 * and schedule for processing (to withdraw from peers))
5729 delete_routes_for_es(bgp
, es
);
5731 /* free the hash entry */
5732 bgp_evpn_es_free(bgp
, es
);
5738 * bgp_evpn_local_es_add
5740 int bgp_evpn_local_es_add(struct bgp
*bgp
,
5742 struct ipaddr
*originator_ip
)
5744 char buf
[ESI_STR_LEN
];
5745 struct evpnes
*es
= NULL
;
5746 struct prefix_evpn p
;
5748 if (!bgp
->esihash
) {
5749 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5754 /* create the new es */
5755 es
= bgp_evpn_lookup_es(bgp
, esi
);
5757 es
= bgp_evpn_es_new(bgp
, esi
, originator_ip
);
5761 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
5762 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5766 UNSET_FLAG(es
->flags
, EVPNES_REMOTE
);
5767 SET_FLAG(es
->flags
, EVPNES_LOCAL
);
5769 build_evpn_type4_prefix(&p
, esi
, originator_ip
->ipaddr_v4
);
5770 if (update_evpn_type4_route(bgp
, es
, &p
)) {
5771 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5772 "%u: Type4 route creation failure for ESI %s",
5773 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5777 /* import all remote ES routes in th ES table */
5778 install_routes_for_es(bgp
, es
);
5784 * Handle change in setting for BUM handling. The supported values
5785 * are head-end replication and dropping all BUM packets. Any change
5786 * should be registered with zebra. Also, if doing head-end replication,
5787 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5788 * to be dropped, the RT-3s must be withdrawn.
5790 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
5792 zlog_info("L2VPN EVPN BUM handling is %s",
5793 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
5794 "Flooding" : "Flooding Disabled");
5796 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
5797 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
5798 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
5799 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
5800 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
5804 * Cleanup EVPN information on disable - Need to delete and withdraw
5805 * EVPN routes from peers.
5807 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5809 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
5810 void *))cleanup_vni_on_disable
,
5815 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5816 * BGP instance (default) is being freed.
5818 void bgp_evpn_cleanup(struct bgp
*bgp
)
5820 hash_iterate(bgp
->vnihash
,
5821 (void (*)(struct hash_backet
*, void *))free_vni_entry
,
5824 hash_free(bgp
->import_rt_hash
);
5825 bgp
->import_rt_hash
= NULL
;
5827 hash_free(bgp
->vrf_import_rt_hash
);
5828 bgp
->vrf_import_rt_hash
= NULL
;
5830 hash_free(bgp
->vnihash
);
5831 bgp
->vnihash
= NULL
;
5833 hash_free(bgp
->esihash
);
5834 bgp
->esihash
= NULL
;
5836 list_delete(&bgp
->vrf_import_rtl
);
5837 list_delete(&bgp
->vrf_export_rtl
);
5838 list_delete(&bgp
->l2vnis
);
5842 * Initialization for EVPN
5845 * hash for RT to VNI
5847 void bgp_evpn_init(struct bgp
*bgp
)
5850 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
5852 hash_create(esi_hash_keymake
, esi_cmp
,
5853 "BGP EVPN Local ESI Hash");
5854 bgp
->import_rt_hash
=
5855 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
5856 "BGP Import RT Hash");
5857 bgp
->vrf_import_rt_hash
=
5858 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
5859 "BGP VRF Import RT Hash");
5860 bgp
->vrf_import_rtl
= list_new();
5861 bgp
->vrf_import_rtl
->cmp
=
5862 (int (*)(void *, void *))evpn_route_target_cmp
;
5863 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
5864 bgp
->vrf_export_rtl
= list_new();
5865 bgp
->vrf_export_rtl
->cmp
=
5866 (int (*)(void *, void *))evpn_route_target_cmp
;
5867 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
5868 bgp
->l2vnis
= list_new();
5869 bgp
->l2vnis
->cmp
= vni_list_cmp
;
5870 /* By default Duplicate Address Dection is enabled.
5871 * Max-moves (N) 5, detection time (M) 180
5872 * default action is warning-only
5873 * freeze action permanently freezes address,
5874 * and freeze time (auto-recovery) is disabled.
5876 if (bgp
->evpn_info
) {
5877 bgp
->evpn_info
->dup_addr_detect
= true;
5878 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
5879 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
5880 bgp
->evpn_info
->dad_freeze
= false;
5881 bgp
->evpn_info
->dad_freeze_time
= 0;
5882 /* Initialize zebra vxlan */
5883 bgp_zebra_dup_addr_detection(bgp
);
5886 /* Default BUM handling is to do head-end replication. */
5887 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
5890 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
5892 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);