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
= (struct bgp_table
*)rdrn
->info
;
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 /* Unintern existing, set to new. */
2510 bgp_attr_unintern(&pi
->attr
);
2511 pi
->attr
= attr_new
;
2512 pi
->uptime
= bgp_clock();
2515 bgp_aggregate_increment(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2517 /* Perform route selection and update zebra, if required. */
2518 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2524 * Install route entry into the VNI routing table and invoke route selection.
2526 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2527 struct prefix_evpn
*p
,
2528 struct bgp_path_info
*parent_pi
)
2530 struct bgp_node
*rn
;
2531 struct bgp_path_info
*pi
;
2532 struct attr
*attr_new
;
2535 /* Create (or fetch) route within the VNI. */
2536 /* NOTE: There is no RD here. */
2537 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2539 /* Check if route entry is already present. */
2540 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2542 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2546 /* Add (or update) attribute to hash. */
2547 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2549 /* Create new route with its attribute. */
2550 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2551 parent_pi
->peer
, attr_new
, rn
);
2552 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2553 bgp_path_info_extra_get(pi
);
2554 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2555 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2556 if (parent_pi
->extra
) {
2557 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2558 sizeof(pi
->extra
->label
));
2559 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2561 bgp_path_info_add(rn
, pi
);
2563 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2564 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2565 bgp_unlock_node(rn
);
2568 /* The attribute has changed. */
2569 /* Add (or update) attribute to hash. */
2570 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2572 /* Restore route, if needed. */
2573 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2574 bgp_path_info_restore(rn
, pi
);
2576 /* Mark if nexthop has changed. */
2577 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2578 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2580 /* Unintern existing, set to new. */
2581 bgp_attr_unintern(&pi
->attr
);
2582 pi
->attr
= attr_new
;
2583 pi
->uptime
= bgp_clock();
2586 /* Perform route selection and update zebra, if required. */
2587 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2592 /* Uninstall EVPN route entry from ES route table */
2593 static int uninstall_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2594 struct prefix_evpn
*p
,
2595 struct bgp_path_info
*parent_pi
)
2598 struct bgp_node
*rn
;
2599 struct bgp_path_info
*pi
;
2601 if (!es
->route_table
)
2604 /* Locate route within the ESI.
2605 * NOTE: There is no RD here.
2607 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2611 /* Find matching route entry. */
2612 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2614 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2620 /* Mark entry for deletion */
2621 bgp_path_info_delete(rn
, pi
);
2623 /* Perform route selection and update zebra, if required. */
2624 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2626 /* Unlock route node. */
2627 bgp_unlock_node(rn
);
2633 * Uninstall route entry from the VRF routing table and send message
2634 * to zebra, if appropriate.
2636 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2637 struct prefix_evpn
*evp
,
2638 struct bgp_path_info
*parent_pi
)
2640 struct bgp_node
*rn
;
2641 struct bgp_path_info
*pi
;
2644 struct prefix
*pp
= &p
;
2647 char buf
[PREFIX_STRLEN
];
2648 char buf1
[PREFIX_STRLEN
];
2650 memset(pp
, 0, sizeof(struct prefix
));
2651 ip_prefix_from_evpn_prefix(evp
, pp
);
2653 if (bgp_debug_zebra(NULL
)) {
2655 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2656 prefix2str(evp
, buf
, sizeof(buf
)),
2657 prefix2str(pp
, buf1
, sizeof(buf
)),
2658 vrf_id_to_name(bgp_vrf
->vrf_id
));
2661 /* Locate route within the VRF. */
2662 /* NOTE: There is no RD here. */
2663 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2665 safi
= SAFI_UNICAST
;
2666 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2669 safi
= SAFI_UNICAST
;
2670 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2676 /* Find matching route entry. */
2677 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2679 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2685 bgp_aggregate_decrement(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2687 /* Mark entry for deletion */
2688 bgp_path_info_delete(rn
, pi
);
2690 /* Perform route selection and update zebra, if required. */
2691 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2693 /* Unlock route node. */
2694 bgp_unlock_node(rn
);
2700 * Uninstall route entry from the VNI routing table and send message
2701 * to zebra, if appropriate.
2703 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2704 struct prefix_evpn
*p
,
2705 struct bgp_path_info
*parent_pi
)
2707 struct bgp_node
*rn
;
2708 struct bgp_path_info
*pi
;
2711 /* Locate route within the VNI. */
2712 /* NOTE: There is no RD here. */
2713 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2717 /* Find matching route entry. */
2718 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2720 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2726 /* Mark entry for deletion */
2727 bgp_path_info_delete(rn
, pi
);
2729 /* Perform route selection and update zebra, if required. */
2730 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2732 /* Unlock route node. */
2733 bgp_unlock_node(rn
);
2739 * Given a prefix, see if it belongs to ES.
2741 static int is_prefix_matching_for_es(struct prefix_evpn
*p
,
2744 /* if not an ES route return false */
2745 if (p
->prefix
.route_type
!= BGP_EVPN_ES_ROUTE
)
2748 if (memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
)) == 0)
2755 * Given a route entry and a VRF, see if this route entry should be
2756 * imported into the VRF i.e., RTs match.
2758 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2759 struct bgp_path_info
*pi
)
2761 struct attr
*attr
= pi
->attr
;
2762 struct ecommunity
*ecom
;
2766 /* Route should have valid RT to be even considered. */
2767 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2770 ecom
= attr
->ecommunity
;
2771 if (!ecom
|| !ecom
->size
)
2774 /* For each extended community RT, see if it matches this VNI. If any RT
2775 * matches, we're done.
2777 for (i
= 0; i
< ecom
->size
; i
++) {
2779 uint8_t type
, sub_type
;
2780 struct ecommunity_val
*eval
;
2781 struct ecommunity_val eval_tmp
;
2782 struct vrf_irt_node
*irt
;
2784 /* Only deal with RTs */
2785 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2786 eval
= (struct ecommunity_val
*)(ecom
->val
2787 + (i
* ECOMMUNITY_SIZE
));
2790 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2793 /* See if this RT matches specified VNIs import RTs */
2794 irt
= lookup_vrf_import_rt(eval
);
2796 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2799 /* Also check for non-exact match. In this, we mask out the AS
2801 * only check on the local-admin sub-field. This is to
2803 * VNI as the RT for EBGP peering too.
2806 if (type
== ECOMMUNITY_ENCODE_AS
2807 || type
== ECOMMUNITY_ENCODE_AS4
2808 || type
== ECOMMUNITY_ENCODE_IP
) {
2809 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2810 mask_ecom_global_admin(&eval_tmp
, eval
);
2811 irt
= lookup_vrf_import_rt(&eval_tmp
);
2814 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2822 * Given a route entry and a VNI, see if this route entry should be
2823 * imported into the VNI i.e., RTs match.
2825 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2826 struct bgp_path_info
*pi
)
2828 struct attr
*attr
= pi
->attr
;
2829 struct ecommunity
*ecom
;
2833 /* Route should have valid RT to be even considered. */
2834 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2837 ecom
= attr
->ecommunity
;
2838 if (!ecom
|| !ecom
->size
)
2841 /* For each extended community RT, see if it matches this VNI. If any RT
2842 * matches, we're done.
2844 for (i
= 0; i
< ecom
->size
; i
++) {
2846 uint8_t type
, sub_type
;
2847 struct ecommunity_val
*eval
;
2848 struct ecommunity_val eval_tmp
;
2849 struct irt_node
*irt
;
2851 /* Only deal with RTs */
2852 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2853 eval
= (struct ecommunity_val
*)(ecom
->val
2854 + (i
* ECOMMUNITY_SIZE
));
2857 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2860 /* See if this RT matches specified VNIs import RTs */
2861 irt
= lookup_import_rt(bgp
, eval
);
2863 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2866 /* Also check for non-exact match. In this, we mask out the AS
2868 * only check on the local-admin sub-field. This is to
2870 * VNI as the RT for EBGP peering too.
2873 if (type
== ECOMMUNITY_ENCODE_AS
2874 || type
== ECOMMUNITY_ENCODE_AS4
2875 || type
== ECOMMUNITY_ENCODE_IP
) {
2876 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2877 mask_ecom_global_admin(&eval_tmp
, eval
);
2878 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2881 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2888 static int install_uninstall_routes_for_es(struct bgp
*bgp
,
2895 char buf
[PREFIX_STRLEN
];
2896 char buf1
[ESI_STR_LEN
];
2897 struct bgp_node
*rd_rn
, *rn
;
2898 struct bgp_table
*table
;
2899 struct bgp_path_info
*pi
;
2905 * Walk entire global routing table and evaluate routes which could be
2906 * imported into this VRF. Note that we need to loop through all global
2907 * routes to determine which route matches the import rt on vrf
2909 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2910 rd_rn
= bgp_route_next(rd_rn
)) {
2911 table
= (struct bgp_table
*)(rd_rn
->info
);
2915 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2916 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2918 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
2921 * Consider "valid" remote routes applicable for
2924 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
2925 && pi
->type
== ZEBRA_ROUTE_BGP
2926 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
2929 if (!is_prefix_matching_for_es(evp
, es
))
2933 ret
= install_evpn_route_entry_in_es(
2936 ret
= uninstall_evpn_route_entry_in_es(
2942 "Failed to %s EVPN %s route in ESI %s",
2945 prefix2str(evp
, buf
,
2947 esi_to_str(&es
->esi
, buf1
,
2958 * Install or uninstall mac-ip routes are appropriate for this
2961 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
2965 struct bgp_node
*rd_rn
, *rn
;
2966 struct bgp_table
*table
;
2967 struct bgp_path_info
*pi
;
2969 char buf
[PREFIX_STRLEN
];
2970 struct bgp
*bgp_def
= NULL
;
2974 bgp_def
= bgp_get_default();
2978 /* Walk entire global routing table and evaluate routes which could be
2979 * imported into this VRF. Note that we need to loop through all global
2980 * routes to determine which route matches the import rt on vrf
2982 for (rd_rn
= bgp_table_top(bgp_def
->rib
[afi
][safi
]); rd_rn
;
2983 rd_rn
= bgp_route_next(rd_rn
)) {
2984 table
= (struct bgp_table
*)(rd_rn
->info
);
2988 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2989 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2991 /* if not mac-ip route skip this route */
2992 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2993 || evp
->prefix
.route_type
2994 == BGP_EVPN_IP_PREFIX_ROUTE
))
2997 /* if not a mac+ip route skip this route */
2998 if (!(is_evpn_prefix_ipaddr_v4(evp
)
2999 || is_evpn_prefix_ipaddr_v6(evp
)))
3002 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3004 /* Consider "valid" remote routes applicable for
3007 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3008 && pi
->type
== ZEBRA_ROUTE_BGP
3009 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3012 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
3014 ret
= install_evpn_route_entry_in_vrf(
3017 ret
= uninstall_evpn_route_entry_in_vrf(
3023 "Failed to %s EVPN %s route in VRF %s",
3026 prefix2str(evp
, buf
,
3041 * Install or uninstall routes of specified type that are appropriate for this
3044 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
3045 struct bgpevpn
*vpn
,
3046 bgp_evpn_route_type rtype
,
3051 struct bgp_node
*rd_rn
, *rn
;
3052 struct bgp_table
*table
;
3053 struct bgp_path_info
*pi
;
3059 /* Walk entire global routing table and evaluate routes which could be
3060 * imported into this VPN. Note that we cannot just look at the routes
3062 * the VNI's RD - remote routes applicable for this VNI could have any
3065 /* EVPN routes are a 2-level table. */
3066 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
3067 rd_rn
= bgp_route_next(rd_rn
)) {
3068 table
= (struct bgp_table
*)(rd_rn
->info
);
3072 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3073 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3075 if (evp
->prefix
.route_type
!= rtype
)
3078 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3080 /* Consider "valid" remote routes applicable for
3082 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3083 && pi
->type
== ZEBRA_ROUTE_BGP
3084 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3087 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3089 ret
= install_evpn_route_entry(
3092 ret
= uninstall_evpn_route_entry(
3098 "%u: Failed to %s EVPN %s route in VNI %u",
3102 rtype
== BGP_EVPN_MAC_IP_ROUTE
3116 /* Install any existing remote ES routes applicable for this ES into its routing
3117 * table. This is invoked when ES comes up.
3119 static int install_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
3121 return install_uninstall_routes_for_es(bgp
, es
, 1);
3125 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3126 * is invoked upon l3vni-add or l3vni import rt change
3128 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3130 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3135 * Install any existing remote routes applicable for this VNI into its
3136 * routing table. This is invoked when a VNI becomes "live" or its Import
3139 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3143 /* Install type-3 routes followed by type-2 routes - the ones applicable
3146 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3151 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3155 /* uninstall routes from l3vni vrf. */
3156 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3158 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3163 * Uninstall any existing remote routes for this VNI. One scenario in which
3164 * this is invoked is upon an import RT change.
3166 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3170 /* Uninstall type-2 routes followed by type-3 routes - the ones
3174 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3179 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3183 /* Install or unistall route in ES */
3184 static int install_uninstall_route_in_es(struct bgp
*bgp
, struct evpnes
*es
,
3185 afi_t afi
, safi_t safi
,
3186 struct prefix_evpn
*evp
,
3187 struct bgp_path_info
*pi
, int install
)
3190 char buf
[ESI_STR_LEN
];
3193 ret
= install_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3195 ret
= uninstall_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3200 "%u: Failed to %s EVPN %s route in ESI %s", bgp
->vrf_id
,
3201 install
? "install" : "uninstall", "ES",
3202 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)));
3209 * Install or uninstall route in matching VRFs (list).
3211 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3212 safi_t safi
, struct prefix_evpn
*evp
,
3213 struct bgp_path_info
*pi
,
3214 struct list
*vrfs
, int install
)
3216 char buf
[PREFIX2STR_BUFFER
];
3217 struct bgp
*bgp_vrf
;
3218 struct listnode
*node
, *nnode
;
3220 /* Only type-2/type-5 routes go into a VRF */
3221 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3222 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3225 /* if it is type-2 route and not a mac+ip route skip this route */
3226 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3227 && !(is_evpn_prefix_ipaddr_v4(evp
)
3228 || is_evpn_prefix_ipaddr_v6(evp
)))
3231 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3235 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3237 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3241 flog_err(EC_BGP_EVPN_FAIL
,
3242 "%u: Failed to %s prefix %s in VRF %s",
3244 install
? "install" : "uninstall",
3245 prefix2str(evp
, buf
, sizeof(buf
)),
3246 vrf_id_to_name(bgp_vrf
->vrf_id
));
3255 * Install or uninstall route in matching VNIs (list).
3257 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3258 safi_t safi
, struct prefix_evpn
*evp
,
3259 struct bgp_path_info
*pi
,
3260 struct list
*vnis
, int install
)
3262 struct bgpevpn
*vpn
;
3263 struct listnode
*node
, *nnode
;
3265 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3268 if (!is_vni_live(vpn
))
3272 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3274 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3277 flog_err(EC_BGP_EVPN_FAIL
,
3278 "%u: Failed to %s EVPN %s route in VNI %u",
3279 bgp
->vrf_id
, install
? "install" : "uninstall",
3280 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3292 * Install or uninstall route for appropriate VNIs/ESIs.
3294 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3296 struct bgp_path_info
*pi
, int import
)
3298 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3299 struct attr
*attr
= pi
->attr
;
3300 struct ecommunity
*ecom
;
3305 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3306 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3307 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3308 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3309 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3312 /* If we don't have Route Target, nothing much to do. */
3313 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3316 ecom
= attr
->ecommunity
;
3317 if (!ecom
|| !ecom
->size
)
3320 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3321 * attached to the route */
3322 for (i
= 0; i
< ecom
->size
; i
++) {
3324 uint8_t type
, sub_type
;
3325 struct ecommunity_val
*eval
;
3326 struct ecommunity_val eval_tmp
;
3327 struct irt_node
*irt
; /* import rt for l2vni */
3328 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3331 /* Only deal with RTs */
3332 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3333 eval
= (struct ecommunity_val
*)(ecom
->val
3334 + (i
* ECOMMUNITY_SIZE
));
3337 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3341 * macip routes (type-2) are imported into VNI and VRF tables.
3342 * IMET route is imported into VNI table.
3343 * prefix routes are imported into VRF table.
3345 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3346 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3347 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3349 irt
= lookup_import_rt(bgp
, eval
);
3351 install_uninstall_route_in_vnis(
3352 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3355 vrf_irt
= lookup_vrf_import_rt(eval
);
3357 install_uninstall_route_in_vrfs(
3358 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3361 /* Also check for non-exact match.
3362 * In this, we mask out the AS and
3363 * only check on the local-admin sub-field.
3364 * This is to facilitate using
3365 * VNI as the RT for EBGP peering too.
3369 if (type
== ECOMMUNITY_ENCODE_AS
3370 || type
== ECOMMUNITY_ENCODE_AS4
3371 || type
== ECOMMUNITY_ENCODE_IP
) {
3372 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3373 mask_ecom_global_admin(&eval_tmp
, eval
);
3374 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3375 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3379 install_uninstall_route_in_vnis(
3380 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3383 install_uninstall_route_in_vrfs(
3384 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3388 /* es route is imported into the es table */
3389 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3391 /* we will match based on the entire esi to avoid
3392 * imoort of an es route for esi2 into esi1
3394 es
= bgp_evpn_lookup_es(bgp
, &evp
->prefix
.es_addr
.esi
);
3395 if (es
&& is_es_local(es
))
3396 install_uninstall_route_in_es(
3397 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3405 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3408 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3410 /* delete all ipv4 routes and withdraw from peers */
3411 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3412 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3414 /* delete all ipv6 routes and withdraw from peers */
3415 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3416 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3420 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3423 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3425 /* update all ipv4 routes */
3426 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3427 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3429 /* update all ipv6 routes */
3430 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3431 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3435 * update and advertise local routes for a VRF as type-5 routes.
3436 * This is invoked upon RD change for a VRF. Note taht the processing is only
3437 * done in the global route table using the routes which already exist in the
3440 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3442 /* skip if the RD is configured */
3443 if (is_vrf_rd_configured(bgp_vrf
))
3446 /* derive the RD for the VRF based on new router-id */
3447 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3449 /* update advertise ipv4|ipv6 routes as type-5 routes */
3450 update_advertise_vrf_routes(bgp_vrf
);
3454 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3455 * This is invoked upon VRF RD change. The processing is done only from global
3458 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3460 /* skip if the RD is configured */
3461 if (is_vrf_rd_configured(bgp_vrf
))
3464 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3465 delete_withdraw_vrf_routes(bgp_vrf
);
3469 * Update and advertise local routes for a VNI. Invoked upon router-id
3470 * change. Note that the processing is done only on the global route table
3471 * using routes that already exist in the per-VNI table.
3473 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3475 struct prefix_evpn p
;
3476 struct bgp_node
*rn
, *global_rn
;
3477 struct bgp_path_info
*pi
, *global_pi
;
3479 afi_t afi
= AFI_L2VPN
;
3480 safi_t safi
= SAFI_EVPN
;
3482 /* Locate type-3 route for VNI in the per-VNI table and use its
3483 * attributes to create and advertise the type-3 route for this VNI
3484 * in the global table.
3486 * RT-3 only if doing head-end replication
3488 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
3489 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3490 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3491 if (!rn
) /* unexpected */
3493 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3494 if (pi
->peer
== bgp
->peer_self
&&
3495 pi
->type
== ZEBRA_ROUTE_BGP
3496 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3498 if (!pi
) /* unexpected */
3502 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3503 (struct prefix
*)&p
, &vpn
->prd
);
3504 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
,
3505 1, &pi
, 0, mac_mobility_seqnum(attr
));
3507 /* Schedule for processing and unlock node. */
3508 bgp_process(bgp
, global_rn
, afi
, safi
);
3509 bgp_unlock_node(global_rn
);
3512 /* Now, walk this VNI's route table and use the route and its attribute
3513 * to create and schedule route in global table.
3515 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
3516 rn
= bgp_route_next(rn
)) {
3517 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3519 /* Identify MAC-IP local routes. */
3520 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3523 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3524 if (pi
->peer
== bgp
->peer_self
3525 && pi
->type
== ZEBRA_ROUTE_BGP
3526 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3531 /* Create route in global routing table using this route entry's
3535 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3536 (struct prefix
*)evp
, &vpn
->prd
);
3538 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
3540 mac_mobility_seqnum(attr
));
3542 /* Schedule for processing and unlock node. */
3543 bgp_process(bgp
, global_rn
, afi
, safi
);
3544 bgp_unlock_node(global_rn
);
3551 * Delete (and withdraw) local routes for a VNI - only from the global
3552 * table. Invoked upon router-id change.
3554 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3557 struct prefix_evpn p
;
3558 struct bgp_node
*global_rn
;
3559 struct bgp_path_info
*pi
;
3560 afi_t afi
= AFI_L2VPN
;
3561 safi_t safi
= SAFI_EVPN
;
3563 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3564 * for this VNI - from the global table.
3566 ret
= delete_global_type2_routes(bgp
, vpn
);
3570 /* Remove type-3 route for this VNI from global table. */
3571 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3572 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3573 (struct prefix
*)&p
, &vpn
->prd
);
3575 /* Delete route entry in the global EVPN table. */
3576 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
3578 /* Schedule for processing - withdraws to peers happen from
3582 bgp_process(bgp
, global_rn
, afi
, safi
);
3583 bgp_unlock_node(global_rn
);
3590 * Handle router-id change. Update and advertise local routes corresponding
3591 * to this VNI from peers. Note that this is invoked after updating the
3592 * router-id. The routes in the per-VNI table are used to create routes in
3593 * the global table and schedule them.
3595 static void update_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3597 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3599 /* Skip VNIs with configured RD. */
3600 if (is_rd_configured(vpn
))
3603 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3604 update_advertise_vni_routes(bgp
, vpn
);
3608 * Handle router-id change. Delete and withdraw local routes corresponding
3609 * to this VNI from peers. Note that this is invoked prior to updating
3610 * the router-id and is done only on the global route table, the routes
3611 * are needed in the per-VNI table to re-advertise with new router id.
3613 static void withdraw_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3615 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3617 /* Skip VNIs with configured RD. */
3618 if (is_rd_configured(vpn
))
3621 delete_withdraw_vni_routes(bgp
, vpn
);
3625 * Create RT-3 for a VNI and schedule for processing and advertisement.
3626 * This is invoked upon flooding mode changing to head-end replication.
3628 static void create_advertise_type3(struct hash_backet
*backet
, void *data
)
3630 struct bgpevpn
*vpn
= backet
->data
;
3631 struct bgp
*bgp
= data
;
3632 struct prefix_evpn p
;
3634 if (!vpn
|| !is_vni_live(vpn
))
3637 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3638 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0))
3639 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3640 "Type3 route creation failure for VNI %u", vpn
->vni
);
3644 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3645 * This is invoked upon flooding mode changing to drop BUM packets.
3647 static void delete_withdraw_type3(struct hash_backet
*backet
, void *data
)
3649 struct bgpevpn
*vpn
= backet
->data
;
3650 struct bgp
*bgp
= data
;
3651 struct prefix_evpn p
;
3653 if (!vpn
|| !is_vni_live(vpn
))
3656 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3657 delete_evpn_route(bgp
, vpn
, &p
);
3661 * Process received EVPN type-2 route (advertise or withdraw).
3663 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3664 struct attr
*attr
, uint8_t *pfx
, int psize
,
3665 uint32_t addpath_id
)
3667 struct prefix_rd prd
;
3668 struct prefix_evpn p
;
3669 struct bgp_route_evpn evpn
;
3671 uint8_t macaddr_len
;
3672 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
3673 uint32_t num_labels
= 0;
3677 /* Type-2 route should be either 33, 37 or 49 bytes or an
3678 * additional 3 bytes if there is a second label (VNI):
3679 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3680 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3681 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3683 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3684 && psize
!= 40 && psize
!= 52) {
3685 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3686 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3687 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3691 memset(&evpn
, 0, sizeof(evpn
));
3693 /* Make prefix_rd */
3694 prd
.family
= AF_UNSPEC
;
3696 memcpy(&prd
.val
, pfx
, 8);
3699 /* Make EVPN prefix. */
3700 memset(&p
, 0, sizeof(struct prefix_evpn
));
3702 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3703 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3705 /* Copy Ethernet Seg Identifier */
3706 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
3709 /* Copy Ethernet Tag */
3710 memcpy(ð_tag
, pfx
, 4);
3711 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3714 /* Get the MAC Addr len */
3715 macaddr_len
= *pfx
++;
3717 /* Get the MAC Addr */
3718 if (macaddr_len
== (ETH_ALEN
* 8)) {
3719 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
3723 EC_BGP_EVPN_ROUTE_INVALID
,
3724 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3725 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3731 ipaddr_len
= *pfx
++;
3732 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3733 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3735 EC_BGP_EVPN_ROUTE_INVALID
,
3736 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3737 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3742 ipaddr_len
/= 8; /* Convert to bytes. */
3743 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3746 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
3750 /* Get the VNI(s). Stored as bytes here. */
3752 memset(label
, 0, sizeof(label
));
3753 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
3754 pfx
+= BGP_LABEL_BYTES
;
3755 psize
-= (33 + ipaddr_len
);
3756 /* Do we have a second VNI? */
3759 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
3761 * If in future, we are required to access additional fields,
3762 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
3767 /* Process the route. */
3769 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3770 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3771 &prd
, &label
[0], num_labels
, 0, &evpn
);
3773 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3774 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3775 &prd
, &label
[0], num_labels
, &evpn
);
3780 * Process received EVPN type-3 route (advertise or withdraw).
3782 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3783 struct attr
*attr
, uint8_t *pfx
, int psize
,
3784 uint32_t addpath_id
)
3786 struct prefix_rd prd
;
3787 struct prefix_evpn p
;
3792 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3793 * IP len (1) and IP (4 or 16).
3795 if (psize
!= 17 && psize
!= 29) {
3796 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3797 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3798 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3802 /* If PMSI is present, log if it is anything other than IR.
3803 * Note: We just simply ignore the values as it is not clear if
3804 * doing anything else is better.
3807 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3808 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
) {
3810 EC_BGP_EVPN_PMSI_PRESENT
,
3811 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3812 peer
->bgp
->vrf_id
, peer
->host
,
3813 attr
->pmsi_tnl_type
);
3817 /* Make prefix_rd */
3818 prd
.family
= AF_UNSPEC
;
3820 memcpy(&prd
.val
, pfx
, 8);
3823 /* Make EVPN prefix. */
3824 memset(&p
, 0, sizeof(struct prefix_evpn
));
3826 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3827 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3829 /* Copy Ethernet Tag */
3830 memcpy(ð_tag
, pfx
, 4);
3831 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
3835 ipaddr_len
= *pfx
++;
3836 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3837 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
3838 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
3841 EC_BGP_EVPN_ROUTE_INVALID
,
3842 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3843 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3847 /* Process the route. */
3849 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3850 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3851 &prd
, NULL
, 0, 0, NULL
);
3853 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3854 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3855 &prd
, NULL
, 0, NULL
);
3860 * Process received EVPN type-4 route (advertise or withdraw).
3862 static int process_type4_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3863 struct attr
*attr
, uint8_t *pfx
, int psize
,
3864 uint32_t addpath_id
)
3869 struct in_addr vtep_ip
;
3870 struct prefix_rd prd
;
3871 struct prefix_evpn p
;
3873 /* Type-4 route should be either 23 or 35 bytes
3874 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
3876 if (psize
!= 23 && psize
!= 35) {
3877 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3878 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
3879 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3883 /* Make prefix_rd */
3884 prd
.family
= AF_UNSPEC
;
3886 memcpy(&prd
.val
, pfx
, 8);
3890 memcpy(&esi
, pfx
, ESI_BYTES
);
3895 ipaddr_len
= *pfx
++;
3896 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3897 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
3900 EC_BGP_EVPN_ROUTE_INVALID
,
3901 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
3902 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3906 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
3907 /* Process the route. */
3909 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3910 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3911 &prd
, NULL
, 0, 0, NULL
);
3913 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3914 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3915 &prd
, NULL
, 0, NULL
);
3922 * Process received EVPN type-5 route (advertise or withdraw).
3924 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3925 struct attr
*attr
, uint8_t *pfx
, int psize
,
3926 uint32_t addpath_id
, int withdraw
)
3928 struct prefix_rd prd
;
3929 struct prefix_evpn p
;
3930 struct bgp_route_evpn evpn
;
3933 mpls_label_t label
; /* holds the VNI as in the packet */
3936 /* Type-5 route should be 34 or 58 bytes:
3937 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3938 * GW (4 or 16) and VNI (3).
3939 * Note that the IP and GW should both be IPv4 or both IPv6.
3941 if (psize
!= 34 && psize
!= 58) {
3942 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3943 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3944 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3948 /* Make prefix_rd */
3949 prd
.family
= AF_UNSPEC
;
3951 memcpy(&prd
.val
, pfx
, 8);
3954 /* Make EVPN prefix. */
3955 memset(&p
, 0, sizeof(struct prefix_evpn
));
3957 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3958 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
3960 /* Additional information outside of prefix - ESI and GW IP */
3961 memset(&evpn
, 0, sizeof(evpn
));
3964 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
3967 /* Fetch Ethernet Tag. */
3968 memcpy(ð_tag
, pfx
, 4);
3969 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
3972 /* Fetch IP prefix length. */
3974 if (ippfx_len
> IPV6_MAX_BITLEN
) {
3976 EC_BGP_EVPN_ROUTE_INVALID
,
3977 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3978 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
3981 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
3983 /* Determine IPv4 or IPv6 prefix */
3984 /* Since the address and GW are from the same family, this just becomes
3985 * a simple check on the total size.
3988 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
3989 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
3991 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
3994 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
3995 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
3997 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
4001 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4002 memset(&label
, 0, sizeof(label
));
4003 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
4006 * If in future, we are required to access additional fields,
4007 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4011 /* Process the route. */
4013 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4014 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4015 &prd
, &label
, 1, 0, &evpn
);
4017 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4018 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4019 &prd
, &label
, 1, &evpn
);
4024 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
4025 struct prefix_rd
*prd
, mpls_label_t
*label
,
4026 uint32_t num_labels
, struct attr
*attr
)
4030 struct evpn_addr
*p_evpn_p
;
4032 memset(&temp
, 0, 16);
4033 if (p
->family
!= AF_EVPN
)
4035 p_evpn_p
= &(p
->u
.prefix_evpn
);
4037 /* len denites the total len of IP and GW-IP in the route
4038 IP and GW-IP have to be both ipv4 or ipv6
4040 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4041 len
= 8; /* IP and GWIP are both ipv4 */
4043 len
= 32; /* IP and GWIP are both ipv6 */
4044 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4045 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4046 stream_put(s
, prd
->val
, 8);
4048 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
4050 stream_put(s
, &temp
, 10);
4051 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4052 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4053 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4054 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4056 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4058 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4060 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
4062 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
4064 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4065 stream_put_ipv4(s
, 0);
4067 stream_put(s
, &temp
, 16);
4071 stream_put(s
, label
, 3);
4077 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4079 static void cleanup_vni_on_disable(struct hash_backet
*backet
, struct bgp
*bgp
)
4081 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
4083 /* Remove EVPN routes and schedule for processing. */
4084 delete_routes_for_vni(bgp
, vpn
);
4086 /* Clear "live" flag and see if hash needs to be freed. */
4087 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4088 if (!is_vni_configured(vpn
))
4089 bgp_evpn_free(bgp
, vpn
);
4093 * Free a VNI entry; iterator function called during cleanup.
4095 static void free_vni_entry(struct hash_backet
*backet
, struct bgp
*bgp
)
4097 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
4099 delete_all_vni_routes(bgp
, vpn
);
4100 bgp_evpn_free(bgp
, vpn
);
4104 * Derive AUTO import RT for BGP VRF - L3VNI
4106 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4108 struct bgp
*bgp_def
= NULL
;
4110 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4111 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4114 bgp_def
= bgp_get_default();
4117 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4121 * Delete AUTO import RT from BGP VRF - L3VNI
4123 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4125 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4129 * Derive AUTO export RT for BGP VRF - L3VNI
4131 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4133 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4134 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4138 * Delete AUTO export RT from BGP VRF - L3VNI
4140 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4142 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4145 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4147 struct bgp
*bgp_def
= NULL
;
4148 struct listnode
*node
= NULL
;
4149 struct bgpevpn
*vpn
= NULL
;
4151 bgp_def
= bgp_get_default();
4155 /* update all type-5 routes */
4156 update_advertise_vrf_routes(bgp_vrf
);
4158 /* update all type-2 routes */
4159 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4160 update_routes_for_vni(bgp_def
, vpn
);
4164 * Handle autort change for a given VNI.
4166 static void update_autort_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
4168 struct bgpevpn
*vpn
= backet
->data
;
4170 if (!is_import_rt_configured(vpn
)) {
4171 if (is_vni_live(vpn
))
4172 bgp_evpn_uninstall_routes(bgp
, vpn
);
4173 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4174 list_delete_all_node(vpn
->import_rtl
);
4175 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4176 if (is_vni_live(vpn
))
4177 bgp_evpn_install_routes(bgp
, vpn
);
4179 if (!is_export_rt_configured(vpn
)) {
4180 list_delete_all_node(vpn
->export_rtl
);
4181 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4182 if (is_vni_live(vpn
))
4183 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4191 /* withdraw type-5 route corresponding to ip prefix */
4192 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4193 afi_t afi
, safi_t safi
)
4196 struct prefix_evpn evp
;
4197 char buf
[PREFIX_STRLEN
];
4199 build_type5_prefix_from_ip_prefix(&evp
, p
);
4200 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4203 EC_BGP_EVPN_ROUTE_DELETE
,
4204 "%u failed to delete type-5 route for prefix %s in vrf %s",
4205 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4206 vrf_id_to_name(bgp_vrf
->vrf_id
));
4210 /* withdraw all type-5 routes for an address family */
4211 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4213 struct bgp_table
*table
= NULL
;
4214 struct bgp_node
*rn
= NULL
;
4215 struct bgp_path_info
*pi
;
4217 table
= bgp_vrf
->rib
[afi
][safi
];
4218 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4219 /* Only care about "selected" routes - non-imported. */
4220 /* TODO: Support for AddPath for EVPN. */
4221 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4222 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4223 && (!pi
->extra
|| !pi
->extra
->parent
)) {
4224 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
4233 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4234 * to this function correspond to those of the source IP prefix (best
4235 * path in the case of the attr. In the case of a local prefix (when we
4236 * are advertising local subnets), the src_attr will be NULL.
4238 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4239 struct attr
*src_attr
, afi_t afi
,
4243 struct prefix_evpn evp
;
4244 char buf
[PREFIX_STRLEN
];
4246 build_type5_prefix_from_ip_prefix(&evp
, p
);
4247 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4249 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4250 "%u: Failed to create type-5 route for prefix %s",
4251 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4254 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4255 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4256 * advertisement is enabled.
4258 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4261 struct bgp_table
*table
= NULL
;
4262 struct bgp_node
*rn
= NULL
;
4263 struct bgp_path_info
*pi
;
4265 table
= bgp_vrf
->rib
[afi
][safi
];
4266 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4267 /* Need to identify the "selected" route entry to use its
4268 * attribute. Also, we only consider "non-imported" routes.
4269 * TODO: Support for AddPath for EVPN.
4271 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4272 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4273 && (!pi
->extra
|| !pi
->extra
->parent
)) {
4275 /* apply the route-map */
4276 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4279 ret
= route_map_apply(
4280 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4282 &rn
->p
, RMAP_BGP
, pi
);
4283 if (ret
== RMAP_DENYMATCH
)
4286 bgp_evpn_advertise_type5_route(
4287 bgp_vrf
, &rn
->p
, pi
->attr
, afi
, safi
);
4294 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4296 struct listnode
*node
, *nnode
, *node_to_del
;
4297 struct ecommunity
*ecom
, *ecom_auto
;
4298 struct ecommunity_val eval
;
4300 if (bgp
->advertise_autort_rfc8365
)
4301 vni
|= EVPN_AUTORT_VXLAN
;
4302 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4304 ecom_auto
= ecommunity_new();
4305 ecommunity_add_val(ecom_auto
, &eval
);
4308 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4309 if (ecommunity_match(ecom
, ecom_auto
)) {
4310 ecommunity_free(&ecom
);
4316 list_delete_node(rtl
, node_to_del
);
4318 ecommunity_free(&ecom_auto
);
4321 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4322 struct ecommunity
*ecomadd
)
4324 /* uninstall routes from vrf */
4325 uninstall_routes_for_vrf(bgp_vrf
);
4327 /* Cleanup the RT to VRF mapping */
4328 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4330 /* Remove auto generated RT */
4331 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4333 /* Add the newly configured RT to RT list */
4334 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4335 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4337 /* map VRF to its RTs */
4338 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4340 /* install routes matching the new VRF */
4341 install_routes_for_vrf(bgp_vrf
);
4344 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4345 struct ecommunity
*ecomdel
)
4347 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4348 struct ecommunity
*ecom
= NULL
;
4350 /* uninstall routes from vrf */
4351 uninstall_routes_for_vrf(bgp_vrf
);
4353 /* Cleanup the RT to VRF mapping */
4354 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4356 /* remove the RT from the RT list */
4357 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4358 if (ecommunity_match(ecom
, ecomdel
)) {
4359 ecommunity_free(&ecom
);
4366 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4368 assert(bgp_vrf
->vrf_import_rtl
);
4369 /* fallback to auto import rt, if this was the last RT */
4370 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4371 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4372 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4375 /* map VRFs to its RTs */
4376 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4378 /* install routes matching this new RT */
4379 install_routes_for_vrf(bgp_vrf
);
4382 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4383 struct ecommunity
*ecomadd
)
4385 /* remove auto-generated RT */
4386 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4388 /* Add the new RT to the RT list */
4389 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4390 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4392 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4395 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4396 struct ecommunity
*ecomdel
)
4398 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4399 struct ecommunity
*ecom
= NULL
;
4401 /* Remove the RT from the RT list */
4402 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4403 if (ecommunity_match(ecom
, ecomdel
)) {
4404 ecommunity_free(&ecom
);
4411 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4414 * Temporary assert to make SA happy.
4415 * The ALL_LIST_ELEMENTS macro above has a NULL check
4416 * which means that SA is going to complain about
4417 * the list_isempty call, which doesn't NULL check.
4418 * So until we get this situation cleaned up, here
4421 assert(bgp_vrf
->vrf_export_rtl
);
4423 /* fall back to auto-generated RT if this was the last RT */
4424 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4425 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4426 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4429 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4433 * Handle change to BGP router id. This is invoked twice by the change
4434 * handler, first before the router id has been changed and then after
4435 * the router id has been changed. The first invocation will result in
4436 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4437 * will result in the routes being re-advertised.
4439 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4443 /* delete and withdraw all the type-5 routes
4444 stored in the global table for this vrf
4446 withdraw_router_id_vrf(bgp
);
4448 /* delete all the VNI routes (type-2/type-3) routes for all the
4451 hash_iterate(bgp
->vnihash
,
4452 (void (*)(struct hash_backet
*,
4453 void *))withdraw_router_id_vni
,
4457 /* advertise all routes in the vrf as type-5 routes with the new
4460 update_router_id_vrf(bgp
);
4462 /* advertise all the VNI routes (type-2/type-3) routes with the
4465 hash_iterate(bgp
->vnihash
,
4466 (void (*)(struct hash_backet
*,
4467 void *))update_router_id_vni
,
4473 * Handle change to auto-RT algorithm - update and advertise local routes.
4475 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4477 hash_iterate(bgp
->vnihash
,
4478 (void (*)(struct hash_backet
*,
4479 void*))update_autort_vni
,
4484 * Handle change to export RT - update and advertise local routes.
4486 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4488 return update_routes_for_vni(bgp
, vpn
);
4491 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4494 delete_withdraw_vrf_routes(bgp_vrf
);
4496 update_advertise_vrf_routes(bgp_vrf
);
4500 * Handle change to RD. This is invoked twice by the change handler,
4501 * first before the RD has been changed and then after the RD has
4502 * been changed. The first invocation will result in local routes
4503 * of this VNI being deleted and withdrawn and the next will result
4504 * in the routes being re-advertised.
4506 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4510 delete_withdraw_vni_routes(bgp
, vpn
);
4512 update_advertise_vni_routes(bgp
, vpn
);
4516 * Install routes for this VNI. Invoked upon change to Import RT.
4518 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4520 return install_routes_for_vni(bgp
, vpn
);
4524 * Uninstall all routes installed for this VNI. Invoked upon change
4527 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4529 return uninstall_routes_for_vni(bgp
, vpn
);
4533 * TODO: Hardcoded for a maximum of 2 VNIs right now
4535 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4540 vni1
= label2vni(label
);
4541 if (num_labels
== 2) {
4542 vni2
= label2vni(label
+ 1);
4543 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4545 snprintf(buf
, len
, "%u", vni1
);
4550 * Function to convert evpn route to json format.
4551 * NOTE: We don't use prefix2str as the output here is a bit different.
4553 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
4555 char buf1
[ETHER_ADDR_STRLEN
];
4556 char buf2
[PREFIX2STR_BUFFER
];
4561 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4562 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4563 json_object_int_add(json
, "ethTag",
4564 p
->prefix
.imet_addr
.eth_tag
);
4565 json_object_int_add(json
, "ipLen",
4566 is_evpn_prefix_ipaddr_v4(p
)
4569 json_object_string_add(json
, "ip",
4570 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4571 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4572 if (is_evpn_prefix_ipaddr_none(p
)) {
4573 json_object_int_add(json
, "routeType",
4574 p
->prefix
.route_type
);
4575 json_object_int_add(json
, "ethTag",
4576 p
->prefix
.macip_addr
.eth_tag
);
4577 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4578 json_object_string_add(json
, "mac",
4579 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4585 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4588 json_object_int_add(json
, "routeType",
4589 p
->prefix
.route_type
);
4590 json_object_int_add(json
, "ethTag",
4591 p
->prefix
.macip_addr
.eth_tag
);
4592 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4593 json_object_string_add(json
, "mac",
4594 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4597 json_object_int_add(json
, "ipLen",
4598 is_evpn_prefix_ipaddr_v4(p
)
4601 json_object_string_add(
4604 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4606 PREFIX2STR_BUFFER
));
4609 /* Currently, this is to cater to other AF_ETHERNET code. */
4614 * Function to convert evpn route to string.
4615 * NOTE: We don't use prefix2str as the output here is a bit different.
4617 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
4619 char buf1
[ETHER_ADDR_STRLEN
];
4620 char buf2
[PREFIX2STR_BUFFER
];
4621 char buf3
[ESI_STR_LEN
];
4623 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4624 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
4625 p
->prefix
.imet_addr
.eth_tag
,
4626 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4628 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4629 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4630 if (is_evpn_prefix_ipaddr_none(p
))
4631 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4632 p
->prefix
.route_type
,
4633 p
->prefix
.macip_addr
.eth_tag
,
4635 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4640 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4642 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
4643 p
->prefix
.route_type
,
4644 p
->prefix
.macip_addr
.eth_tag
,
4646 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4648 family
== AF_INET
? IPV4_MAX_BITLEN
4651 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4653 PREFIX2STR_BUFFER
));
4655 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
4656 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4657 p
->prefix
.route_type
,
4658 p
->prefix
.prefix_addr
.eth_tag
,
4659 p
->prefix
.prefix_addr
.ip_prefix_length
,
4660 is_evpn_prefix_ipaddr_v4(p
)
4661 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
4662 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
4663 } else if (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
4664 snprintf(buf
, len
, "[%d]:[%s]:[%d]:[%s]",
4665 p
->prefix
.route_type
,
4666 esi_to_str(&p
->prefix
.es_addr
.esi
, buf3
, sizeof(buf3
)),
4667 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4669 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
));
4671 /* For EVPN route types not supported yet. */
4672 snprintf(buf
, len
, "(unsupported route type %d)",
4673 p
->prefix
.route_type
);
4680 * Encode EVPN prefix in Update (MP_REACH)
4682 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
4683 struct prefix_rd
*prd
, mpls_label_t
*label
,
4684 uint32_t num_labels
, struct attr
*attr
,
4685 int addpath_encode
, uint32_t addpath_tx_id
)
4687 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4688 int len
, ipa_len
= 0;
4691 stream_putl(s
, addpath_tx_id
);
4694 stream_putc(s
, evp
->prefix
.route_type
);
4696 switch (evp
->prefix
.route_type
) {
4697 case BGP_EVPN_MAC_IP_ROUTE
:
4698 if (is_evpn_prefix_ipaddr_v4(evp
))
4699 ipa_len
= IPV4_MAX_BYTELEN
;
4700 else if (is_evpn_prefix_ipaddr_v6(evp
))
4701 ipa_len
= IPV6_MAX_BYTELEN
;
4702 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4703 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4704 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4706 stream_putc(s
, len
);
4707 stream_put(s
, prd
->val
, 8); /* RD */
4709 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
4711 stream_put(s
, 0, 10);
4712 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4713 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4714 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4715 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4716 if (ipa_len
) /* IP */
4717 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4719 /* 1st label is the L2 VNI */
4720 stream_put(s
, label
, BGP_LABEL_BYTES
);
4721 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4722 if (ipa_len
&& num_labels
> 1)
4723 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4726 case BGP_EVPN_IMET_ROUTE
:
4727 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4728 stream_put(s
, prd
->val
, 8); /* RD */
4729 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4730 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4731 /* Originating Router's IP Addr */
4732 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4735 case BGP_EVPN_ES_ROUTE
:
4736 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4737 stream_put(s
, prd
->val
, 8); /* RD */
4738 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4739 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4741 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4744 case BGP_EVPN_IP_PREFIX_ROUTE
:
4745 /* TODO: AddPath support. */
4746 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4754 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4755 struct bgp_nlri
*packet
, int withdraw
)
4761 uint32_t addpath_id
;
4762 int addpath_encoded
;
4767 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4769 lim
= pnt
+ packet
->length
;
4771 safi
= packet
->safi
;
4775 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
4776 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
4777 PEER_CAP_ADDPATH_AF_TX_RCV
));
4779 for (; pnt
< lim
; pnt
+= psize
) {
4780 /* Clear prefix structure. */
4781 memset(&p
, 0, sizeof(struct prefix
));
4783 /* Deal with path-id if AddPath is supported. */
4784 if (addpath_encoded
) {
4785 /* When packet overflow occurs return immediately. */
4786 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4789 addpath_id
= ntohl(*((uint32_t *)pnt
));
4790 pnt
+= BGP_ADDPATH_ID_LEN
;
4793 /* All EVPN NLRI types start with type and length. */
4800 /* When packet overflow occur return immediately. */
4801 if (pnt
+ psize
> lim
)
4805 case BGP_EVPN_MAC_IP_ROUTE
:
4806 if (process_type2_route(peer
, afi
, safi
,
4807 withdraw
? NULL
: attr
, pnt
,
4808 psize
, addpath_id
)) {
4811 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4812 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4817 case BGP_EVPN_IMET_ROUTE
:
4818 if (process_type3_route(peer
, afi
, safi
,
4819 withdraw
? NULL
: attr
, pnt
,
4820 psize
, addpath_id
)) {
4823 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4824 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4829 case BGP_EVPN_ES_ROUTE
:
4830 if (process_type4_route(peer
, afi
, safi
,
4831 withdraw
? NULL
: attr
, pnt
,
4832 psize
, addpath_id
)) {
4835 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4836 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4841 case BGP_EVPN_IP_PREFIX_ROUTE
:
4842 if (process_type5_route(peer
, afi
, safi
, attr
, pnt
,
4843 psize
, addpath_id
, withdraw
)) {
4846 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4847 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4857 /* Packet length consistency check. */
4865 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
4866 * The mapping will be used during route processing.
4867 * bgp_def: default bgp instance
4868 * bgp_vrf: specific bgp vrf instance on which RT is configured
4870 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
4873 struct ecommunity_val
*eval
= NULL
;
4874 struct listnode
*node
= NULL
, *nnode
= NULL
;
4875 struct ecommunity
*ecom
= NULL
;
4877 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4878 for (i
= 0; i
< ecom
->size
; i
++) {
4879 eval
= (struct ecommunity_val
*)(ecom
->val
4881 * ECOMMUNITY_SIZE
));
4882 map_vrf_to_rt(bgp_vrf
, eval
);
4888 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
4890 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
4893 struct ecommunity_val
*eval
;
4894 struct listnode
*node
, *nnode
;
4895 struct ecommunity
*ecom
;
4897 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4898 for (i
= 0; i
< ecom
->size
; i
++) {
4899 struct vrf_irt_node
*irt
;
4900 struct ecommunity_val eval_tmp
;
4902 eval
= (struct ecommunity_val
*)(ecom
->val
4904 * ECOMMUNITY_SIZE
));
4905 /* If using "automatic" RT, we only care about the
4906 * local-admin sub-field.
4907 * This is to facilitate using VNI as the RT for EBGP
4910 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4911 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
4912 BGP_VRF_IMPORT_RT_CFGD
))
4913 mask_ecom_global_admin(&eval_tmp
, eval
);
4915 irt
= lookup_vrf_import_rt(&eval_tmp
);
4917 unmap_vrf_from_rt(bgp_vrf
, irt
);
4924 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
4925 * The mapping will be used during route processing.
4927 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4930 struct ecommunity_val
*eval
;
4931 struct listnode
*node
, *nnode
;
4932 struct ecommunity
*ecom
;
4934 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4935 for (i
= 0; i
< ecom
->size
; i
++) {
4936 eval
= (struct ecommunity_val
*)(ecom
->val
4938 * ECOMMUNITY_SIZE
));
4939 map_vni_to_rt(bgp
, vpn
, eval
);
4945 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
4947 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4950 struct ecommunity_val
*eval
;
4951 struct listnode
*node
, *nnode
;
4952 struct ecommunity
*ecom
;
4954 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4955 for (i
= 0; i
< ecom
->size
; i
++) {
4956 struct irt_node
*irt
;
4957 struct ecommunity_val eval_tmp
;
4959 eval
= (struct ecommunity_val
*)(ecom
->val
4961 * ECOMMUNITY_SIZE
));
4962 /* If using "automatic" RT, we only care about the
4963 * local-admin sub-field.
4964 * This is to facilitate using VNI as the RT for EBGP
4967 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4968 if (!is_import_rt_configured(vpn
))
4969 mask_ecom_global_admin(&eval_tmp
, eval
);
4971 irt
= lookup_import_rt(bgp
, &eval_tmp
);
4973 unmap_vni_from_rt(bgp
, vpn
, irt
);
4979 * Derive Import RT automatically for VNI and map VNI to RT.
4980 * The mapping will be used during route processing.
4982 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4984 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
4985 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
4988 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
4992 * Derive Export RT automatically for VNI.
4994 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4996 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
4997 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5001 * Derive RD automatically for VNI using passed information - it
5002 * is of the form RouterId:unique-id-for-vni.
5004 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5006 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5010 * Derive RD automatically for VNI using passed information - it
5011 * is of the form RouterId:unique-id-for-vni.
5013 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5017 vpn
->prd
.family
= AF_UNSPEC
;
5018 vpn
->prd
.prefixlen
= 64;
5019 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
5020 (void)str2prefix_rd(buf
, &vpn
->prd
);
5021 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5027 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5029 struct list
*inst
= bm
->bgp
;
5030 struct listnode
*node
;
5031 struct bgp
*bgp_vrf
;
5033 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5034 if (bgp_vrf
->l3vni
== vni
)
5044 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5046 struct bgpevpn
*vpn
;
5049 memset(&tmp
, 0, sizeof(struct bgpevpn
));
5051 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5056 * Create a new vpn - invoked upon configuration or zebra notification.
5058 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5059 struct in_addr originator_ip
,
5060 vrf_id_t tenant_vrf_id
)
5062 struct bgpevpn
*vpn
;
5067 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5071 /* Set values - RD and RT set to defaults. */
5073 vpn
->originator_ip
= originator_ip
;
5074 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5076 /* Initialize route-target import and export lists */
5077 vpn
->import_rtl
= list_new();
5078 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5079 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
5080 vpn
->export_rtl
= list_new();
5081 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5082 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
5083 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5084 derive_rd_rt_for_vni(bgp
, vpn
);
5086 /* Initialize EVPN route table. */
5087 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5090 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
5091 XFREE(MTYPE_BGP_EVPN
, vpn
);
5095 /* add to l2vni list on corresponding vrf */
5096 bgpevpn_link_to_l3vni(vpn
);
5098 QOBJ_REG(vpn
, bgpevpn
);
5103 * Free a given VPN - called in multiple scenarios such as zebra
5104 * notification, configuration being deleted, advertise-all-vni disabled etc.
5105 * This just frees appropriate memory, caller should have taken other
5108 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5110 bgpevpn_unlink_from_l3vni(vpn
);
5111 bgp_table_unlock(vpn
->route_table
);
5112 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5113 list_delete(&vpn
->import_rtl
);
5114 list_delete(&vpn
->export_rtl
);
5115 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5116 hash_release(bgp
->vnihash
, vpn
);
5118 XFREE(MTYPE_BGP_EVPN
, vpn
);
5124 struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
)
5129 memset(&tmp
, 0, sizeof(struct evpnes
));
5130 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
5131 es
= hash_lookup(bgp
->esihash
, &tmp
);
5136 * Create a new local es - invoked upon zebra notification.
5138 struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
,
5140 struct ipaddr
*originator_ip
)
5148 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct evpnes
));
5152 /* set the ESI and originator_ip */
5153 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
5154 memcpy(&es
->originator_ip
, originator_ip
, sizeof(struct ipaddr
));
5156 /* Initialise the VTEP list */
5157 es
->vtep_list
= list_new();
5158 es
->vtep_list
->cmp
= evpn_vtep_ip_cmp
;
5160 /* auto derive RD for this es */
5161 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
5162 es
->prd
.family
= AF_UNSPEC
;
5163 es
->prd
.prefixlen
= 64;
5164 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), es
->rd_id
);
5165 (void)str2prefix_rd(buf
, &es
->prd
);
5167 /* Initialize the ES route table */
5168 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5171 if (!hash_get(bgp
->esihash
, es
, hash_alloc_intern
)) {
5172 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5176 QOBJ_REG(es
, evpnes
);
5182 * This just frees appropriate memory, caller should have taken other
5185 void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
)
5187 list_delete(&es
->vtep_list
);
5188 bgp_table_unlock(es
->route_table
);
5189 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
5190 hash_release(bgp
->esihash
, es
);
5192 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5196 * Import evpn route from global table to VNI/VRF/ESI.
5198 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5199 struct prefix
*p
, struct bgp_path_info
*pi
)
5201 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5205 * Unimport evpn route from VNI/VRF/ESI.
5207 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5208 struct prefix
*p
, struct bgp_path_info
*pi
)
5210 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5213 /* filter routes which have martian next hops */
5214 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5218 struct bgp_node
*rd_rn
, *rn
;
5219 struct bgp_table
*table
;
5220 struct bgp_path_info
*pi
;
5225 /* Walk entire global routing table and evaluate routes which could be
5226 * imported into this VPN. Note that we cannot just look at the routes
5227 * for the VNI's RD -
5228 * remote routes applicable for this VNI could have any RD.
5230 /* EVPN routes are a 2-level table. */
5231 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
5232 rd_rn
= bgp_route_next(rd_rn
)) {
5233 table
= (struct bgp_table
*)(rd_rn
->info
);
5237 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5239 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
5242 /* Consider "valid" remote routes applicable for
5244 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5245 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5248 if (bgp_nexthop_self(bgp
, pi
->attr
->nexthop
)) {
5250 char attr_str
[BUFSIZ
];
5251 char pbuf
[PREFIX_STRLEN
];
5253 bgp_dump_attr(pi
->attr
, attr_str
,
5256 if (bgp_debug_update(pi
->peer
, &rn
->p
,
5259 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
5266 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5269 bgp_rib_remove(rn
, pi
, pi
->peer
, afi
,
5280 * Handle del of a local MACIP.
5282 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5285 struct bgpevpn
*vpn
;
5286 struct prefix_evpn p
;
5288 /* Lookup VNI hash - should exist. */
5289 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5290 if (!vpn
|| !is_vni_live(vpn
)) {
5291 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5292 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5293 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5297 /* Remove EVPN type-2 route and schedule for processing. */
5298 build_evpn_type2_prefix(&p
, mac
, ip
);
5299 delete_evpn_route(bgp
, vpn
, &p
);
5305 * Handle add of a local MACIP.
5307 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5308 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
)
5310 struct bgpevpn
*vpn
;
5311 struct prefix_evpn p
;
5313 /* Lookup VNI hash - should exist. */
5314 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5315 if (!vpn
|| !is_vni_live(vpn
)) {
5316 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5317 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5318 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5322 /* Create EVPN type-2 route and schedule for processing. */
5323 build_evpn_type2_prefix(&p
, mac
, ip
);
5324 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
)) {
5325 char buf
[ETHER_ADDR_STRLEN
];
5326 char buf2
[INET6_ADDRSTRLEN
];
5329 EC_BGP_EVPN_ROUTE_CREATE
,
5330 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5331 bgp
->vrf_id
, vpn
->vni
,
5332 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5335 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5336 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5343 static void link_l2vni_hash_to_l3vni(struct hash_backet
*backet
,
5344 struct bgp
*bgp_vrf
)
5346 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
5347 struct bgp
*bgp_def
= NULL
;
5349 bgp_def
= bgp_get_default();
5352 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5353 bgpevpn_link_to_l3vni(vpn
);
5356 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
, struct ethaddr
*rmac
,
5357 struct in_addr originator_ip
, int filter
)
5359 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5360 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5361 struct listnode
*node
= NULL
;
5362 struct bgpevpn
*vpn
= NULL
;
5365 /* get the default instance - required to get the AS number for VRF
5368 bgp_def
= bgp_get_default();
5372 "Cannot process L3VNI %u ADD - default BGP instance not yet created",
5378 /* if the BGP vrf instance doesn't exist - create one */
5379 bgp_vrf
= bgp_lookup_by_name(vrf_id_to_name(vrf_id
));
5384 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5385 BGP_INSTANCE_TYPE_VRF
);
5387 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
:
5388 flog_err(EC_BGP_MULTI_INSTANCE
,
5389 "'bgp multiple-instance' not present\n");
5391 case BGP_ERR_AS_MISMATCH
:
5392 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5393 "BGP is already running; AS is %u\n", as
);
5395 case BGP_ERR_INSTANCE_MISMATCH
:
5396 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5397 "BGP instance name and AS number mismatch\n");
5401 /* mark as auto created */
5402 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5405 /* associate with l3vni */
5406 bgp_vrf
->l3vni
= l3vni
;
5408 /* set the router mac - to be used in mac-ip routes for this vrf */
5409 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
5411 /* set the originator ip */
5412 bgp_vrf
->originator_ip
= originator_ip
;
5414 /* set the right filter - are we using l3vni only for prefix routes? */
5416 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5418 /* auto derive RD/RT */
5419 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5420 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5421 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5422 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5423 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5425 /* link all corresponding l2vnis */
5426 hash_iterate(bgp_def
->vnihash
,
5427 (void (*)(struct hash_backet
*,
5428 void *))link_l2vni_hash_to_l3vni
,
5431 /* Only update all corresponding type-2 routes if we are advertising two
5432 * labels along with type-2 routes
5435 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5436 update_routes_for_vni(bgp_def
, vpn
);
5438 /* advertise type-5 routes if needed */
5439 update_advertise_vrf_routes(bgp_vrf
);
5441 /* install all remote routes belonging to this l3vni into correspondng
5443 install_routes_for_vrf(bgp_vrf
);
5448 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5450 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5451 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5452 struct listnode
*node
= NULL
;
5453 struct listnode
*next
= NULL
;
5454 struct bgpevpn
*vpn
= NULL
;
5456 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5460 "Cannot process L3VNI %u Del - Could not find BGP instance",
5465 bgp_def
= bgp_get_default();
5469 "Cannot process L3VNI %u Del - Could not find default BGP instance",
5474 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5475 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5476 * routes. This will uninstalling the routes from zebra and decremnt the
5479 uninstall_routes_for_vrf(bgp_vrf
);
5481 /* delete/withdraw all type-5 routes */
5482 delete_withdraw_vrf_routes(bgp_vrf
);
5484 /* remove the l3vni from vrf instance */
5487 /* remove the Rmac from the BGP vrf */
5488 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5491 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5492 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5493 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5495 if (!list_isempty(bgp_vrf
->vrf_export_rtl
)) {
5496 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5499 /* update all corresponding local mac-ip routes */
5500 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5501 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5502 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5503 update_routes_for_vni(bgp_def
, vpn
);
5507 /* If any L2VNIs point to this instance, unlink them. */
5508 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5509 bgpevpn_unlink_from_l3vni(vpn
);
5511 /* Delete the instance if it was autocreated */
5512 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5513 bgp_delete(bgp_vrf
);
5519 * Handle del of a local VNI.
5521 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5523 struct bgpevpn
*vpn
;
5525 /* Locate VNI hash */
5526 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5528 if (bgp_debug_zebra(NULL
))
5530 EC_BGP_EVPN_VPN_VNI
,
5531 "%u: VNI hash entry for VNI %u not found at DEL",
5536 /* Remove all local EVPN routes and schedule for processing (to
5537 * withdraw from peers).
5539 delete_routes_for_vni(bgp
, vpn
);
5542 * tunnel is no longer active, del tunnel ip address from tip_hash
5544 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5546 /* Clear "live" flag and see if hash needs to be freed. */
5547 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5548 if (!is_vni_configured(vpn
))
5549 bgp_evpn_free(bgp
, vpn
);
5555 * Handle add (or update) of a local VNI. The VNI changes we care
5556 * about are for the local-tunnel-ip and the (tenant) VRF.
5558 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5559 struct in_addr originator_ip
, vrf_id_t tenant_vrf_id
)
5561 struct bgpevpn
*vpn
;
5562 struct prefix_evpn p
;
5564 /* Lookup VNI. If present and no change, exit. */
5565 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5568 if (is_vni_live(vpn
)
5569 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5570 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5571 /* Probably some other param has changed that we don't
5575 /* Update tenant_vrf_id if it has changed. */
5576 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5577 bgpevpn_unlink_from_l3vni(vpn
);
5578 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5579 bgpevpn_link_to_l3vni(vpn
);
5582 /* If tunnel endpoint IP has changed, update (and delete prior
5583 * type-3 route, if needed.)
5585 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5586 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5588 /* Update all routes with new endpoint IP and/or export RT
5591 if (is_vni_live(vpn
))
5592 update_routes_for_vni(bgp
, vpn
);
5595 /* Create or update as appropriate. */
5597 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
);
5601 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5607 /* if the VNI is live already, there is nothing more to do */
5608 if (is_vni_live(vpn
))
5611 /* Mark as "live" */
5612 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5614 /* tunnel is now active, add tunnel-ip to db */
5615 bgp_tip_add(bgp
, &originator_ip
);
5617 /* filter routes as nexthop database has changed */
5618 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5621 * Create EVPN type-3 route and schedule for processing.
5623 * RT-3 only if doing head-end replication
5625 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
5626 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5627 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0)) {
5628 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5629 "%u: Type3 route creation failure for VNI %u",
5635 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5639 install_routes_for_vni(bgp
, vpn
);
5641 /* If we are advertising gateway mac-ip
5642 It needs to be conveyed again to zebra */
5643 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5649 * bgp_evpn_local_es_del
5651 int bgp_evpn_local_es_del(struct bgp
*bgp
,
5653 struct ipaddr
*originator_ip
)
5655 char buf
[ESI_STR_LEN
];
5656 struct evpnes
*es
= NULL
;
5658 if (!bgp
->esihash
) {
5659 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5664 /* Lookup ESI hash - should exist. */
5665 es
= bgp_evpn_lookup_es(bgp
, esi
);
5667 flog_warn(EC_BGP_EVPN_ESI
,
5668 "%u: ESI hash entry for ESI %s at Local ES DEL",
5669 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5673 /* Delete all local EVPN ES routes from ESI table
5674 * and schedule for processing (to withdraw from peers))
5676 delete_routes_for_es(bgp
, es
);
5678 /* free the hash entry */
5679 bgp_evpn_es_free(bgp
, es
);
5685 * bgp_evpn_local_es_add
5687 int bgp_evpn_local_es_add(struct bgp
*bgp
,
5689 struct ipaddr
*originator_ip
)
5691 char buf
[ESI_STR_LEN
];
5692 struct evpnes
*es
= NULL
;
5693 struct prefix_evpn p
;
5695 if (!bgp
->esihash
) {
5696 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5701 /* create the new es */
5702 es
= bgp_evpn_lookup_es(bgp
, esi
);
5704 es
= bgp_evpn_es_new(bgp
, esi
, originator_ip
);
5708 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
5709 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5713 UNSET_FLAG(es
->flags
, EVPNES_REMOTE
);
5714 SET_FLAG(es
->flags
, EVPNES_LOCAL
);
5716 build_evpn_type4_prefix(&p
, esi
, originator_ip
->ipaddr_v4
);
5717 if (update_evpn_type4_route(bgp
, es
, &p
)) {
5718 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5719 "%u: Type4 route creation failure for ESI %s",
5720 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5724 /* import all remote ES routes in th ES table */
5725 install_routes_for_es(bgp
, es
);
5731 * Handle change in setting for BUM handling. The supported values
5732 * are head-end replication and dropping all BUM packets. Any change
5733 * should be registered with zebra. Also, if doing head-end replication,
5734 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5735 * to be dropped, the RT-3s must be withdrawn.
5737 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
5739 zlog_info("L2VPN EVPN BUM handling is %s",
5740 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
5741 "Flooding" : "Flooding Disabled");
5743 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
5744 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
5745 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
5746 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
5747 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
5751 * Cleanup EVPN information on disable - Need to delete and withdraw
5752 * EVPN routes from peers.
5754 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5756 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
5757 void *))cleanup_vni_on_disable
,
5762 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5763 * BGP instance (default) is being freed.
5765 void bgp_evpn_cleanup(struct bgp
*bgp
)
5767 hash_iterate(bgp
->vnihash
,
5768 (void (*)(struct hash_backet
*, void *))free_vni_entry
,
5771 hash_free(bgp
->import_rt_hash
);
5772 bgp
->import_rt_hash
= NULL
;
5774 hash_free(bgp
->vrf_import_rt_hash
);
5775 bgp
->vrf_import_rt_hash
= NULL
;
5777 hash_free(bgp
->vnihash
);
5778 bgp
->vnihash
= NULL
;
5780 hash_free(bgp
->esihash
);
5781 bgp
->esihash
= NULL
;
5783 list_delete(&bgp
->vrf_import_rtl
);
5784 list_delete(&bgp
->vrf_export_rtl
);
5785 list_delete(&bgp
->l2vnis
);
5789 * Initialization for EVPN
5792 * hash for RT to VNI
5794 void bgp_evpn_init(struct bgp
*bgp
)
5797 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
5799 hash_create(esi_hash_keymake
, esi_cmp
,
5800 "BGP EVPN Local ESI Hash");
5801 bgp
->import_rt_hash
=
5802 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
5803 "BGP Import RT Hash");
5804 bgp
->vrf_import_rt_hash
=
5805 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
5806 "BGP VRF Import RT Hash");
5807 bgp
->vrf_import_rtl
= list_new();
5808 bgp
->vrf_import_rtl
->cmp
=
5809 (int (*)(void *, void *))evpn_route_target_cmp
;
5810 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
5811 bgp
->vrf_export_rtl
= list_new();
5812 bgp
->vrf_export_rtl
->cmp
=
5813 (int (*)(void *, void *))evpn_route_target_cmp
;
5814 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
5815 bgp
->l2vnis
= list_new();
5816 bgp
->l2vnis
->cmp
= vni_list_cmp
;
5818 /* Default BUM handling is to do head-end replication. */
5819 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
5822 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
5824 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);