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
));
189 irt
->vrfs
= list_new();
192 if (!hash_get(bgp_def
->vrf_import_rt_hash
, irt
, hash_alloc_intern
)) {
193 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
201 * Free the vrf import rt node
203 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
205 struct bgp
*bgp_def
= NULL
;
207 bgp_def
= bgp_get_default();
209 flog_err(EC_BGP_NO_DFLT
,
210 "vrf import rt free - def instance not created yet");
214 hash_release(bgp_def
->vrf_import_rt_hash
, irt
);
215 list_delete(&irt
->vrfs
);
216 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
220 * Function to lookup Import RT node - used to map a RT to set of
221 * VNIs importing routes with that RT.
223 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
225 struct bgp
*bgp_def
= NULL
;
226 struct vrf_irt_node
*irt
;
227 struct vrf_irt_node tmp
;
229 bgp_def
= bgp_get_default();
231 flog_err(EC_BGP_NO_DFLT
,
232 "vrf import rt lookup - def instance not created yet");
236 memset(&tmp
, 0, sizeof(struct vrf_irt_node
));
237 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
238 irt
= hash_lookup(bgp_def
->vrf_import_rt_hash
, &tmp
);
243 * Is specified VRF present on the RT's list of "importing" VRFs?
245 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
, struct bgp
*bgp_vrf
)
247 struct listnode
*node
= NULL
, *nnode
= NULL
;
248 struct bgp
*tmp_bgp_vrf
= NULL
;
250 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
251 if (tmp_bgp_vrf
== bgp_vrf
)
258 * Make import route target hash key.
260 static unsigned int import_rt_hash_key_make(void *p
)
262 struct irt_node
*irt
= p
;
263 char *pnt
= irt
->rt
.val
;
265 return jhash(pnt
, 8, 0xdeadbeef);
269 * Comparison function for import rt hash
271 static bool import_rt_hash_cmp(const void *p1
, const void *p2
)
273 const struct irt_node
*irt1
= p1
;
274 const struct irt_node
*irt2
= p2
;
276 if (irt1
== NULL
&& irt2
== NULL
)
279 if (irt1
== NULL
|| irt2
== NULL
)
282 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
286 * Create a new import_rt
288 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
289 struct ecommunity_val
*rt
)
291 struct irt_node
*irt
;
296 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
299 irt
->vnis
= list_new();
302 if (!hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
)) {
303 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
311 * Free the import rt node
313 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
315 hash_release(bgp
->import_rt_hash
, irt
);
316 list_delete(&irt
->vnis
);
317 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
321 * Function to lookup Import RT node - used to map a RT to set of
322 * VNIs importing routes with that RT.
324 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
325 struct ecommunity_val
*rt
)
327 struct irt_node
*irt
;
330 memset(&tmp
, 0, sizeof(struct irt_node
));
331 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
332 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
337 * Is specified VNI present on the RT's list of "importing" VNIs?
339 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
341 struct listnode
*node
, *nnode
;
342 struct bgpevpn
*tmp_vpn
;
344 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
353 * Compare Route Targets.
355 static int evpn_route_target_cmp(struct ecommunity
*ecom1
,
356 struct ecommunity
*ecom2
)
364 if (!ecom1
&& !ecom2
)
367 if (ecom1
->str
&& !ecom2
->str
)
370 if (!ecom1
->str
&& ecom2
->str
)
373 if (!ecom1
->str
&& !ecom2
->str
)
376 return strcmp(ecom1
->str
, ecom2
->str
);
379 static void evpn_xxport_delete_ecomm(void *val
)
381 struct ecommunity
*ecomm
= val
;
382 ecommunity_free(&ecomm
);
386 * Mask off global-admin field of specified extended community (RT),
387 * just retain the local-admin field.
389 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
390 struct ecommunity_val
*src
)
396 if (type
== ECOMMUNITY_ENCODE_AS
) {
397 dst
->val
[2] = dst
->val
[3] = 0;
398 } else if (type
== ECOMMUNITY_ENCODE_AS4
399 || type
== ECOMMUNITY_ENCODE_IP
) {
400 dst
->val
[2] = dst
->val
[3] = 0;
401 dst
->val
[4] = dst
->val
[5] = 0;
406 * Map one RT to specified VRF.
407 * bgp_vrf = BGP vrf instance
409 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct ecommunity_val
*eval
)
411 struct vrf_irt_node
*irt
= NULL
;
412 struct ecommunity_val eval_tmp
;
414 /* If using "automatic" RT,
415 * we only care about the local-admin sub-field.
416 * This is to facilitate using L3VNI(VRF-VNI)
417 * as the RT for EBGP peering too.
419 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
420 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
421 mask_ecom_global_admin(&eval_tmp
, eval
);
423 irt
= lookup_vrf_import_rt(&eval_tmp
);
424 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
425 /* Already mapped. */
429 irt
= vrf_import_rt_new(&eval_tmp
);
431 /* Add VRF to the list for this RT. */
432 listnode_add(irt
->vrfs
, bgp_vrf
);
436 * Unmap specified VRF from specified RT. If there are no other
437 * VRFs for this RT, then the RT hash is deleted.
438 * bgp_vrf: BGP VRF specific instance
440 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
, struct vrf_irt_node
*irt
)
442 /* Delete VRF from list for this RT. */
443 listnode_delete(irt
->vrfs
, bgp_vrf
);
444 if (!listnode_head(irt
->vrfs
)) {
445 vrf_import_rt_free(irt
);
450 * Map one RT to specified VNI.
452 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
453 struct ecommunity_val
*eval
)
455 struct irt_node
*irt
;
456 struct ecommunity_val eval_tmp
;
458 /* If using "automatic" RT, we only care about the local-admin
460 * This is to facilitate using VNI as the RT for EBGP peering too.
462 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
463 if (!is_import_rt_configured(vpn
))
464 mask_ecom_global_admin(&eval_tmp
, eval
);
466 irt
= lookup_import_rt(bgp
, &eval_tmp
);
468 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
469 /* Already mapped. */
473 irt
= import_rt_new(bgp
, &eval_tmp
);
477 /* Add VNI to the hash list for this RT. */
478 listnode_add(irt
->vnis
, vpn
);
482 * Unmap specified VNI from specified RT. If there are no other
483 * VNIs for this RT, then the RT hash is deleted.
485 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
486 struct irt_node
*irt
)
488 /* Delete VNI from hash list for this RT. */
489 listnode_delete(irt
->vnis
, vpn
);
490 if (!listnode_head(irt
->vnis
)) {
491 import_rt_free(bgp
, irt
);
496 * Create RT extended community automatically from passed information:
497 * of the form AS:VNI.
498 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
499 * the need is to get a RT value that will be unique across different
500 * VNIs but the same across routers (in the same AS) for a particular
503 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
505 struct ecommunity_val eval
;
506 struct ecommunity
*ecomadd
;
508 if (bgp
->advertise_autort_rfc8365
)
509 vni
|= EVPN_AUTORT_VXLAN
;
510 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
512 ecomadd
= ecommunity_new();
513 ecommunity_add_val(ecomadd
, &eval
);
514 listnode_add_sort(rtl
, ecomadd
);
518 * Derive RD and RT for a VNI automatically. Invoked at the time of
521 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
523 bgp_evpn_derive_auto_rd(bgp
, vpn
);
524 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
525 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
529 * Convert nexthop (remote VTEP IP) into an IPv6 address.
531 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
533 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
535 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
536 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
540 * Add (update) or delete MACIP from zebra.
542 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
543 struct prefix_evpn
*p
,
544 struct in_addr remote_vtep_ip
, int add
,
545 uint8_t flags
, uint32_t seq
)
549 char buf1
[ETHER_ADDR_STRLEN
];
550 char buf2
[INET6_ADDRSTRLEN
];
551 char buf3
[INET6_ADDRSTRLEN
];
554 if (!zclient
|| zclient
->sock
< 0)
557 /* Don't try to register if Zebra doesn't know of this instance. */
558 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
559 if (BGP_DEBUG(zebra
, ZEBRA
))
560 zlog_debug("%s: No zebra instance to talk to, not installing remote macip",
561 __PRETTY_FUNCTION__
);
567 zclient_create_header(
568 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
570 stream_putl(s
, vpn
->vni
);
571 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
572 /* IP address length and IP address, if any. */
573 if (is_evpn_prefix_ipaddr_none(p
))
576 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
578 stream_putl(s
, ipa_len
);
579 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
581 stream_put_in_addr(s
, &remote_vtep_ip
);
583 /* TX flags - MAC sticky status and/or gateway mac */
584 /* Also TX the sequence number of the best route. */
586 stream_putc(s
, flags
);
590 stream_putw_at(s
, 0, stream_get_endp(s
));
592 if (bgp_debug_zebra(NULL
))
594 "Tx %s MACIP, VNI %u MAC %s IP %s flags 0x%x seq %u remote VTEP %s",
595 add
? "ADD" : "DEL", vpn
->vni
,
596 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
598 ipaddr2str(&p
->prefix
.macip_addr
.ip
,
599 buf3
, sizeof(buf3
)), flags
, seq
,
600 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
603 return zclient_send_message(zclient
);
607 * Add (update) or delete remote VTEP from zebra.
609 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
610 struct prefix_evpn
*p
, int add
)
615 if (!zclient
|| zclient
->sock
< 0)
618 /* Don't try to register if Zebra doesn't know of this instance. */
619 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
620 if (BGP_DEBUG(zebra
, ZEBRA
))
621 zlog_debug("%s: No zebra instance to talk to, not installing remote vtep",
622 __PRETTY_FUNCTION__
);
629 zclient_create_header(
630 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
632 stream_putl(s
, vpn
->vni
);
633 if (is_evpn_prefix_ipaddr_v4(p
))
634 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
635 else if (is_evpn_prefix_ipaddr_v6(p
)) {
638 "Bad remote IP when trying to %s remote VTEP for VNI %u",
639 add
? "ADD" : "DEL", vpn
->vni
);
643 stream_putw_at(s
, 0, stream_get_endp(s
));
645 if (bgp_debug_zebra(NULL
))
646 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
647 add
? "ADD" : "DEL", vpn
->vni
,
648 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
650 return zclient_send_message(zclient
);
654 * Build extended community for EVPN ES (type-4) route
656 static void build_evpn_type4_route_extcomm(struct evpnes
*es
,
659 struct ecommunity ecom_encap
;
660 struct ecommunity ecom_es_rt
;
661 struct ecommunity_val eval
;
662 struct ecommunity_val eval_es_rt
;
663 bgp_encap_types tnl_type
;
667 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
668 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
669 encode_encap_extcomm(tnl_type
, &eval
);
671 ecom_encap
.val
= (uint8_t *)eval
.val
;
672 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
675 memset(&mac
, 0, sizeof(struct ethaddr
));
676 memset(&ecom_es_rt
, 0, sizeof(ecom_es_rt
));
677 es_get_system_mac(&es
->esi
, &mac
);
678 encode_es_rt_extcomm(&eval_es_rt
, &mac
);
680 ecom_es_rt
.val
= (uint8_t *)eval_es_rt
.val
;
682 ecommunity_merge(attr
->ecommunity
, &ecom_es_rt
);
684 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
688 * Build extended communities for EVPN prefix route.
690 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
693 struct ecommunity ecom_encap
;
694 struct ecommunity ecom_rmac
;
695 struct ecommunity_val eval
;
696 struct ecommunity_val eval_rmac
;
697 bgp_encap_types tnl_type
;
698 struct listnode
*node
, *nnode
;
699 struct ecommunity
*ecom
;
700 struct list
*vrf_export_rtl
= NULL
;
703 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
704 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
705 encode_encap_extcomm(tnl_type
, &eval
);
707 ecom_encap
.val
= (uint8_t *)eval
.val
;
710 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
712 /* Add the export RTs for L3VNI/VRF */
713 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
714 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
716 ecommunity_merge(attr
->ecommunity
, ecom
);
718 /* add the router mac extended community */
719 if (!is_zero_mac(&attr
->rmac
)) {
720 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
721 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
723 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
725 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
728 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
732 * Build extended communities for EVPN route.
733 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
734 * and ENCAP extended communities are applicable for all routes.
735 * The default gateway extended community and MAC mobility (sticky) extended
736 * community are added as needed based on passed settings - only for type-2
737 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
738 * added, if present, based on passed settings - only for non-link-local
741 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
744 struct ecommunity ecom_encap
;
745 struct ecommunity ecom_sticky
;
746 struct ecommunity ecom_default_gw
;
747 struct ecommunity ecom_rmac
;
748 struct ecommunity ecom_na
;
749 struct ecommunity_val eval
;
750 struct ecommunity_val eval_sticky
;
751 struct ecommunity_val eval_default_gw
;
752 struct ecommunity_val eval_rmac
;
753 struct ecommunity_val eval_na
;
755 bgp_encap_types tnl_type
;
756 struct listnode
*node
, *nnode
;
757 struct ecommunity
*ecom
;
759 struct list
*vrf_export_rtl
= NULL
;
762 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
763 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
764 encode_encap_extcomm(tnl_type
, &eval
);
766 ecom_encap
.val
= (uint8_t *)eval
.val
;
769 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
771 /* Add the export RTs for L2VNI */
772 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
773 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
775 /* Add the export RTs for L3VNI if told to - caller determines
776 * when this should be done.
779 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
780 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
781 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
783 attr
->ecommunity
= ecommunity_merge(
784 attr
->ecommunity
, ecom
);
788 /* Add MAC mobility (sticky) if needed. */
791 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
792 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
793 ecom_sticky
.size
= 1;
794 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
796 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
799 /* Add RMAC, if told to. */
801 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
802 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
804 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
806 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
809 /* Add default gateway, if needed. */
810 if (attr
->default_gw
) {
811 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
812 encode_default_gw_extcomm(&eval_default_gw
);
813 ecom_default_gw
.size
= 1;
814 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
816 ecommunity_merge(attr
->ecommunity
, &ecom_default_gw
);
819 if (attr
->router_flag
) {
820 memset(&ecom_na
, 0, sizeof(ecom_na
));
821 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
);
823 ecom_na
.val
= (uint8_t *)eval_na
.val
;
824 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
828 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
832 * Add MAC mobility extended community to attribute.
834 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
836 struct ecommunity ecom_tmp
;
837 struct ecommunity_val eval
;
838 uint8_t *ecom_val_ptr
;
845 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
847 /* Find current MM ecommunity */
850 if (attr
->ecommunity
) {
851 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
852 pnt
= attr
->ecommunity
->val
+ (i
* 8);
856 if (type
== ECOMMUNITY_ENCODE_EVPN
858 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
859 ecom_val_ptr
= (uint8_t *)(attr
->ecommunity
->val
866 /* Update the existing MM ecommunity */
868 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
870 /* Add MM to existing */
872 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
874 ecom_tmp
.val
= (uint8_t *)eval
.val
;
876 if (attr
->ecommunity
)
878 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
880 attr
->ecommunity
= ecommunity_dup(&ecom_tmp
);
884 /* Install EVPN route into zebra. */
885 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
886 struct prefix_evpn
*p
, struct bgp_path_info
*pi
)
891 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
893 if (pi
->attr
->sticky
)
894 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
895 if (pi
->attr
->default_gw
)
896 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
897 if (is_evpn_prefix_ipaddr_v6(p
) &&
898 pi
->attr
->router_flag
)
899 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
900 ret
= bgp_zebra_send_remote_macip(
901 bgp
, vpn
, p
, pi
->attr
->nexthop
, 1, flags
,
902 mac_mobility_seqnum(pi
->attr
));
904 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 1);
910 /* Uninstall EVPN route from zebra. */
911 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
912 struct prefix_evpn
*p
,
913 struct in_addr remote_vtep_ip
)
917 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
918 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
921 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 0);
927 * Due to MAC mobility, the prior "local" best route has been supplanted
928 * by a "remote" best route. The prior route has to be deleted and withdrawn
931 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
933 struct bgp_path_info
*old_local
)
935 struct bgp_node
*global_rn
;
936 struct bgp_path_info
*pi
;
937 afi_t afi
= AFI_L2VPN
;
938 safi_t safi
= SAFI_EVPN
;
940 /* Locate route node in the global EVPN routing table. Note that
941 * this table is a 2-level tree (RD-level + Prefix-level) similar to
944 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
945 (struct prefix
*)&rn
->p
, &vpn
->prd
);
947 /* Delete route entry in the global EVPN table. */
948 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
950 /* Schedule for processing - withdraws to peers happen from
954 bgp_process(bgp
, global_rn
, afi
, safi
);
955 bgp_unlock_node(global_rn
);
958 /* Delete route entry in the VNI route table, caller to remove. */
959 bgp_path_info_delete(rn
, old_local
);
962 static struct in_addr
*es_vtep_new(struct in_addr vtep
)
966 ip
= XCALLOC(MTYPE_BGP_EVPN_ES_VTEP
, sizeof(struct in_addr
));
968 ip
->s_addr
= vtep
.s_addr
;
972 static void es_vtep_free(struct in_addr
*ip
)
974 XFREE(MTYPE_BGP_EVPN_ES_VTEP
, ip
);
977 /* check if VTEP is already part of the list */
978 static int is_vtep_present_in_list(struct list
*list
,
981 struct listnode
*node
= NULL
;
984 for (ALL_LIST_ELEMENTS_RO(list
, node
, tmp
)) {
985 if (tmp
->s_addr
== vtep
.s_addr
)
992 * Best path for ES route was changed,
993 * update the list of VTEPs for this ES
995 static int evpn_es_install_vtep(struct bgp
*bgp
,
997 struct prefix_evpn
*p
,
998 struct in_addr rvtep
)
1000 struct in_addr
*vtep_ip
;
1002 if (is_vtep_present_in_list(es
->vtep_list
, rvtep
))
1006 vtep_ip
= es_vtep_new(rvtep
);
1008 listnode_add_sort(es
->vtep_list
, vtep_ip
);
1013 * Best path for ES route was changed,
1014 * update the list of VTEPs for this ES
1016 static int evpn_es_uninstall_vtep(struct bgp
*bgp
,
1018 struct prefix_evpn
*p
,
1019 struct in_addr rvtep
)
1021 struct listnode
*node
, *nnode
, *node_to_del
= NULL
;
1022 struct in_addr
*tmp
;
1024 for (ALL_LIST_ELEMENTS(es
->vtep_list
, node
, nnode
, tmp
)) {
1025 if (tmp
->s_addr
== rvtep
.s_addr
) {
1032 list_delete_node(es
->vtep_list
, node_to_del
);
1038 * Calculate the best path for a ES(type-4) route.
1040 static int evpn_es_route_select_install(struct bgp
*bgp
,
1042 struct bgp_node
*rn
)
1045 afi_t afi
= AFI_L2VPN
;
1046 safi_t safi
= SAFI_EVPN
;
1047 struct bgp_path_info
*old_select
; /* old best */
1048 struct bgp_path_info
*new_select
; /* new best */
1049 struct bgp_path_info_pair old_and_new
;
1051 /* Compute the best path. */
1052 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
],
1053 &old_and_new
, afi
, safi
);
1054 old_select
= old_and_new
.old
;
1055 new_select
= old_and_new
.new;
1058 * If the best path hasn't changed - see if something needs to be
1061 if (old_select
&& old_select
== new_select
1062 && old_select
->type
== ZEBRA_ROUTE_BGP
1063 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1064 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1065 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1066 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1067 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1068 ret
= evpn_es_install_vtep(bgp
, es
,
1069 (struct prefix_evpn
*)&rn
->p
,
1070 old_select
->attr
->nexthop
);
1072 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1073 bgp_zebra_clear_route_change_flags(rn
);
1077 /* If the user did a "clear" this flag will be set */
1078 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1081 * bestpath has changed; update relevant fields and install or uninstall
1082 * into the zebra RIB.
1084 if (old_select
|| new_select
)
1085 bgp_bump_version(rn
);
1088 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1090 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1091 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1092 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1095 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1096 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1097 ret
= evpn_es_install_vtep(bgp
, es
,
1098 (struct prefix_evpn
*)&rn
->p
,
1099 new_select
->attr
->nexthop
);
1101 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1102 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1103 ret
= evpn_es_uninstall_vtep(
1104 bgp
, es
, (struct prefix_evpn
*)&rn
->p
,
1105 old_select
->attr
->nexthop
);
1108 /* Clear any route change flags. */
1109 bgp_zebra_clear_route_change_flags(rn
);
1111 /* Reap old select bgp_path_info, if it has been removed */
1112 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1113 bgp_path_info_reap(rn
, old_select
);
1119 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1122 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1123 struct bgp_node
*rn
)
1125 struct bgp_path_info
*old_select
, *new_select
;
1126 struct bgp_path_info_pair old_and_new
;
1127 afi_t afi
= AFI_L2VPN
;
1128 safi_t safi
= SAFI_EVPN
;
1131 /* Compute the best path. */
1132 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1134 old_select
= old_and_new
.old
;
1135 new_select
= old_and_new
.new;
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 ret
= evpn_zebra_install(
1149 bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1151 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1152 bgp_zebra_clear_route_change_flags(rn
);
1156 /* If the user did a "clear" this flag will be set */
1157 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1159 /* bestpath has changed; update relevant fields and install or uninstall
1160 * into the zebra RIB.
1162 if (old_select
|| new_select
)
1163 bgp_bump_version(rn
);
1166 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1168 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1169 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1170 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1173 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1174 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1175 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1178 /* If an old best existed and it was a "local" route, the only
1180 * it would be supplanted is due to MAC mobility procedures. So,
1182 * need to do an implicit delete and withdraw that route from
1185 if (old_select
&& old_select
->peer
== bgp
->peer_self
1186 && old_select
->type
== ZEBRA_ROUTE_BGP
1187 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
1188 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
1190 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1191 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1192 ret
= evpn_zebra_uninstall(bgp
, vpn
,
1193 (struct prefix_evpn
*)&rn
->p
,
1194 old_select
->attr
->nexthop
);
1197 /* Clear any route change flags. */
1198 bgp_zebra_clear_route_change_flags(rn
);
1200 /* Reap old select bgp_path_info, if it has been removed */
1201 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1202 bgp_path_info_reap(rn
, old_select
);
1208 * Return true if the local ri for this rn is of type gateway mac
1210 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
1212 struct bgp_path_info
*tmp_pi
= NULL
;
1213 struct bgp_path_info
*local_pi
= NULL
;
1216 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1217 tmp_pi
= tmp_pi
->next
) {
1218 if (tmp_pi
->peer
== bgp
->peer_self
1219 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1220 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1227 return local_pi
->attr
->default_gw
;
1232 * Return true if the local ri for this rn has sticky set
1234 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
1236 struct bgp_path_info
*tmp_pi
;
1237 struct bgp_path_info
*local_pi
;
1240 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1241 tmp_pi
= tmp_pi
->next
) {
1242 if (tmp_pi
->peer
== bgp
->peer_self
1243 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1244 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1251 return local_pi
->attr
->sticky
;
1255 * create or update EVPN type4 route entry.
1256 * This could be in the ES table or the global table.
1257 * TODO: handle remote ES (type4) routes as well
1259 static int update_evpn_type4_route_entry(struct bgp
*bgp
, struct evpnes
*es
,
1260 afi_t afi
, safi_t safi
,
1261 struct bgp_node
*rn
, struct attr
*attr
,
1262 int add
, struct bgp_path_info
**ri
,
1265 char buf
[ESI_STR_LEN
];
1266 char buf1
[INET6_ADDRSTRLEN
];
1267 struct bgp_path_info
*tmp_pi
= NULL
;
1268 struct bgp_path_info
*local_pi
= NULL
; /* local route entry if any */
1269 struct bgp_path_info
*remote_pi
= NULL
; /* remote route entry if any */
1270 struct attr
*attr_new
= NULL
;
1271 struct prefix_evpn
*evp
= NULL
;
1275 evp
= (struct prefix_evpn
*)&rn
->p
;
1277 /* locate the local and remote entries if any */
1278 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1279 tmp_pi
= tmp_pi
->next
) {
1280 if (tmp_pi
->peer
== bgp
->peer_self
1281 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1282 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1284 if (tmp_pi
->type
== ZEBRA_ROUTE_BGP
1285 && tmp_pi
->sub_type
== BGP_ROUTE_IMPORTED
1286 && CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1290 /* we don't expect to see a remote_ri at this point.
1291 * An ES route has esi + vtep_ip as the key,
1292 * We shouldn't see the same route from any other vtep.
1297 "%u ERROR: local es route for ESI: %s Vtep %s also learnt from remote",
1299 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1300 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1304 if (!local_pi
&& !add
)
1307 /* create or update the entry */
1310 /* Add or update attribute to hash */
1311 attr_new
= bgp_attr_intern(attr
);
1313 /* Create new route with its attribute. */
1314 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1315 bgp
->peer_self
, attr_new
, rn
);
1316 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1318 /* add the newly created path to the route-node */
1319 bgp_path_info_add(rn
, tmp_pi
);
1322 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1323 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1326 /* The attribute has changed.
1327 * Add (or update) attribute to hash. */
1328 attr_new
= bgp_attr_intern(attr
);
1329 bgp_path_info_set_flag(rn
, tmp_pi
,
1330 BGP_PATH_ATTR_CHANGED
);
1332 /* Restore route, if needed. */
1333 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1334 bgp_path_info_restore(rn
, tmp_pi
);
1336 /* Unintern existing, set to new. */
1337 bgp_attr_unintern(&tmp_pi
->attr
);
1338 tmp_pi
->attr
= attr_new
;
1339 tmp_pi
->uptime
= bgp_clock();
1343 /* Return back the route entry. */
1348 /* update evpn es (type-4) route */
1349 static int update_evpn_type4_route(struct bgp
*bgp
,
1351 struct prefix_evpn
*p
)
1354 int route_changed
= 0;
1355 char buf
[ESI_STR_LEN
];
1356 char buf1
[INET6_ADDRSTRLEN
];
1357 afi_t afi
= AFI_L2VPN
;
1358 safi_t safi
= SAFI_EVPN
;
1360 struct attr
*attr_new
= NULL
;
1361 struct bgp_node
*rn
= NULL
;
1362 struct bgp_path_info
*pi
= NULL
;
1364 memset(&attr
, 0, sizeof(struct attr
));
1366 /* Build path-attribute for this route. */
1367 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1368 attr
.nexthop
= es
->originator_ip
.ipaddr_v4
;
1369 attr
.mp_nexthop_global_in
= es
->originator_ip
.ipaddr_v4
;
1370 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1372 /* Set up extended community. */
1373 build_evpn_type4_route_extcomm(es
, &attr
);
1375 /* First, create (or fetch) route node within the ESI. */
1376 /* NOTE: There is no RD here. */
1377 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
1379 /* Create or update route entry. */
1380 ret
= update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, &attr
, 1,
1381 &pi
, &route_changed
);
1383 flog_err(EC_BGP_ES_INVALID
,
1384 "%u ERROR: Failed to updated ES route ESI: %s VTEP %s",
1386 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1387 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1391 attr_new
= pi
->attr
;
1393 /* Perform route selection;
1394 * this is just to set the flags correctly
1395 * as local route in the ES always wins.
1397 evpn_es_route_select_install(bgp
, es
, rn
);
1398 bgp_unlock_node(rn
);
1400 /* If this is a new route or some attribute has changed, export the
1401 * route to the global table. The route will be advertised to peers
1402 * from there. Note that this table is a 2-level tree (RD-level +
1403 * Prefix-level) similar to L3VPN routes.
1405 if (route_changed
) {
1406 struct bgp_path_info
*global_pi
;
1408 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1409 (struct prefix
*)p
, &es
->prd
);
1410 update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, attr_new
,
1411 1, &global_pi
, &route_changed
);
1413 /* Schedule for processing and unlock node. */
1414 bgp_process(bgp
, rn
, afi
, safi
);
1415 bgp_unlock_node(rn
);
1418 /* Unintern temporary. */
1419 aspath_unintern(&attr
.aspath
);
1423 static int update_evpn_type5_route_entry(struct bgp
*bgp_def
,
1424 struct bgp
*bgp_vrf
, afi_t afi
,
1425 safi_t safi
, struct bgp_node
*rn
,
1426 struct attr
*attr
, int *route_changed
)
1428 struct attr
*attr_new
= NULL
;
1429 struct bgp_path_info
*pi
= NULL
;
1430 mpls_label_t label
= MPLS_INVALID_LABEL
;
1431 struct bgp_path_info
*local_pi
= NULL
;
1432 struct bgp_path_info
*tmp_pi
= NULL
;
1435 /* locate the local route entry if any */
1436 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1437 tmp_pi
= tmp_pi
->next
) {
1438 if (tmp_pi
->peer
== bgp_def
->peer_self
1439 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1440 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1445 * create a new route entry if one doesn't exist.
1446 * Otherwise see if route attr has changed
1450 /* route has changed as this is the first entry */
1453 /* Add (or update) attribute to hash. */
1454 attr_new
= bgp_attr_intern(attr
);
1456 /* create the route info from attribute */
1457 pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1458 bgp_def
->peer_self
, attr_new
, rn
);
1459 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
1461 /* Type-5 routes advertise the L3-VNI */
1462 bgp_path_info_extra_get(pi
);
1463 vni2label(bgp_vrf
->l3vni
, &label
);
1464 memcpy(&pi
->extra
->label
, &label
, sizeof(label
));
1465 pi
->extra
->num_labels
= 1;
1467 /* add the route entry to route node*/
1468 bgp_path_info_add(rn
, pi
);
1472 if (!attrhash_cmp(tmp_pi
->attr
, attr
)) {
1474 /* attribute changed */
1477 /* The attribute has changed. */
1478 /* Add (or update) attribute to hash. */
1479 attr_new
= bgp_attr_intern(attr
);
1480 bgp_path_info_set_flag(rn
, tmp_pi
,
1481 BGP_PATH_ATTR_CHANGED
);
1483 /* Restore route, if needed. */
1484 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1485 bgp_path_info_restore(rn
, tmp_pi
);
1487 /* Unintern existing, set to new. */
1488 bgp_attr_unintern(&tmp_pi
->attr
);
1489 tmp_pi
->attr
= attr_new
;
1490 tmp_pi
->uptime
= bgp_clock();
1496 /* update evpn type-5 route entry */
1497 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1498 struct attr
*src_attr
)
1500 afi_t afi
= AFI_L2VPN
;
1501 safi_t safi
= SAFI_EVPN
;
1503 struct bgp_node
*rn
= NULL
;
1504 struct bgp
*bgp_def
= NULL
;
1505 int route_changed
= 0;
1507 bgp_def
= bgp_get_default();
1511 /* Build path attribute for this route - use the source attr, if
1512 * present, else treat as locally originated.
1515 bgp_attr_dup(&attr
, src_attr
);
1517 memset(&attr
, 0, sizeof(struct attr
));
1518 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1520 /* Set nexthop to ourselves and fill in the Router MAC. */
1521 attr
.nexthop
= bgp_vrf
->originator_ip
;
1522 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1523 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1524 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
1526 /* Setup RT and encap extended community */
1527 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1529 /* get the route node in global table */
1530 rn
= bgp_afi_node_get(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1531 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1534 /* create or update the route entry within the route node */
1535 update_evpn_type5_route_entry(bgp_def
, bgp_vrf
, afi
, safi
, rn
, &attr
,
1538 /* schedule for processing and unlock node */
1539 if (route_changed
) {
1540 bgp_process(bgp_def
, rn
, afi
, safi
);
1541 bgp_unlock_node(rn
);
1544 /* uninten temporary */
1546 aspath_unintern(&attr
.aspath
);
1551 * Create or update EVPN route entry. This could be in the VNI route table
1552 * or the global route table.
1554 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1555 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1556 struct attr
*attr
, int add
,
1557 struct bgp_path_info
**pi
, uint8_t flags
,
1560 struct bgp_path_info
*tmp_pi
;
1561 struct bgp_path_info
*local_pi
;
1562 struct attr
*attr_new
;
1563 mpls_label_t label
[BGP_MAX_LABELS
];
1564 uint32_t num_labels
= 1;
1565 int route_change
= 1;
1567 struct prefix_evpn
*evp
;
1570 evp
= (struct prefix_evpn
*)&rn
->p
;
1571 memset(&label
, 0, sizeof(label
));
1573 /* See if this is an update of an existing route, or a new add. */
1575 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1576 tmp_pi
= tmp_pi
->next
) {
1577 if (tmp_pi
->peer
== bgp
->peer_self
1578 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1579 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1583 /* If route doesn't exist already, create a new one, if told to.
1584 * Otherwise act based on whether the attributes of the route have
1587 if (!local_pi
&& !add
)
1590 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1591 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1592 add_mac_mobility_to_attr(seq
, attr
);
1595 /* Add (or update) attribute to hash. */
1596 attr_new
= bgp_attr_intern(attr
);
1598 /* Extract MAC mobility sequence number, if any. */
1599 attr_new
->mm_seqnum
=
1600 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1601 attr_new
->sticky
= sticky
;
1603 /* Create new route with its attribute. */
1604 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1605 bgp
->peer_self
, attr_new
, rn
);
1606 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1607 bgp_path_info_extra_get(tmp_pi
);
1609 /* The VNI goes into the 'label' field of the route */
1610 vni2label(vpn
->vni
, &label
[0]);
1612 /* Type-2 routes may carry a second VNI - the L3-VNI.
1613 * Only attach second label if we are advertising two labels for
1616 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1617 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1620 l3vni
= bgpevpn_get_l3vni(vpn
);
1622 vni2label(l3vni
, &label
[1]);
1627 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1628 tmp_pi
->extra
->num_labels
= num_labels
;
1629 bgp_path_info_add(rn
, tmp_pi
);
1632 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1633 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1637 * The attributes have changed, type-2 routes needs to
1638 * be advertised with right labels.
1640 vni2label(vpn
->vni
, &label
[0]);
1641 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1642 && CHECK_FLAG(vpn
->flags
,
1643 VNI_FLAG_USE_TWO_LABELS
)) {
1646 l3vni
= bgpevpn_get_l3vni(vpn
);
1648 vni2label(l3vni
, &label
[1]);
1652 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1653 tmp_pi
->extra
->num_labels
= num_labels
;
1655 /* The attribute has changed. */
1656 /* Add (or update) attribute to hash. */
1657 attr_new
= bgp_attr_intern(attr
);
1658 bgp_path_info_set_flag(rn
, tmp_pi
,
1659 BGP_PATH_ATTR_CHANGED
);
1661 /* Extract MAC mobility sequence number, if any. */
1662 attr_new
->mm_seqnum
=
1663 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1664 attr_new
->sticky
= sticky
;
1666 /* Restore route, if needed. */
1667 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1668 bgp_path_info_restore(rn
, tmp_pi
);
1670 /* Unintern existing, set to new. */
1671 bgp_attr_unintern(&tmp_pi
->attr
);
1672 tmp_pi
->attr
= attr_new
;
1673 tmp_pi
->uptime
= bgp_clock();
1677 /* Return back the route entry. */
1679 return route_change
;
1682 static void evpn_zebra_reinstall_best_route(struct bgp
*bgp
,
1683 struct bgpevpn
*vpn
, struct bgp_node
*rn
)
1685 struct bgp_path_info
*tmp_ri
;
1686 struct bgp_path_info
*curr_select
= NULL
;
1688 for (tmp_ri
= bgp_node_get_bgp_path_info(rn
);
1689 tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1690 if (CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_SELECTED
)) {
1691 curr_select
= tmp_ri
;
1696 if (curr_select
&& curr_select
->type
== ZEBRA_ROUTE_BGP
1697 && curr_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1698 evpn_zebra_install(bgp
, vpn
,
1699 (struct prefix_evpn
*)&rn
->p
,
1704 * If the local route was not selected evict it and tell zebra to re-add
1705 * the best remote dest.
1707 * Typically a local path added by zebra is expected to be selected as
1708 * best. In which case when a remote path wins as best (later)
1709 * evpn_route_select_install itself evicts the older-local-best path.
1711 * However if bgp's add and zebra's add cross paths (race condition) it
1712 * is possible that the local path is no longer the "older" best path.
1713 * It is a path that was never designated as best and hence requires
1714 * additional handling to prevent bgp from injecting and holding on to a
1715 * non-best local path.
1717 static void evpn_cleanup_local_non_best_route(struct bgp
*bgp
,
1718 struct bgpevpn
*vpn
,
1719 struct bgp_node
*rn
,
1720 struct bgp_path_info
*local_pi
)
1722 char buf
[PREFIX_STRLEN
];
1724 /* local path was not picked as the winner; kick it out */
1725 if (bgp_debug_zebra(NULL
)) {
1726 zlog_debug("evicting local evpn prefix %s as remote won",
1727 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
1729 evpn_delete_old_local_route(bgp
, vpn
, rn
, local_pi
);
1730 bgp_path_info_reap(rn
, local_pi
);
1732 /* tell zebra to re-add the best remote path */
1733 evpn_zebra_reinstall_best_route(bgp
, vpn
, rn
);
1737 * Create or update EVPN route (of type based on prefix) for specified VNI
1738 * and schedule for processing.
1740 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1741 struct prefix_evpn
*p
, uint8_t flags
,
1744 struct bgp_node
*rn
;
1746 struct attr
*attr_new
;
1747 int add_l3_ecomm
= 0;
1748 struct bgp_path_info
*pi
;
1749 afi_t afi
= AFI_L2VPN
;
1750 safi_t safi
= SAFI_EVPN
;
1753 memset(&attr
, 0, sizeof(struct attr
));
1755 /* Build path-attribute for this route. */
1756 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1757 attr
.nexthop
= vpn
->originator_ip
;
1758 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1759 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1760 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1761 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1762 attr
.router_flag
= CHECK_FLAG(flags
,
1763 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1765 /* PMSI is only needed for type-3 routes */
1766 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
1767 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1768 attr
.pmsi_tnl_type
= PMSI_TNLTYPE_INGR_REPL
;
1771 /* router mac is only needed for type-2 routes here. */
1772 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1773 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1774 vni2label(vpn
->vni
, &(attr
.label
));
1776 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1777 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1780 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1781 (is_evpn_prefix_ipaddr_v4(p
) ||
1782 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1783 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1784 bgpevpn_get_l3vni(vpn
))
1787 /* Set up extended community. */
1788 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1790 /* First, create (or fetch) route node within the VNI. */
1791 /* NOTE: There is no RD here. */
1792 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1794 /* Create or update route entry. */
1795 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1796 1, &pi
, flags
, seq
);
1798 attr_new
= pi
->attr
;
1800 /* lock ri to prevent freeing in evpn_route_select_install */
1801 bgp_path_info_lock(pi
);
1802 /* Perform route selection; this is just to set the flags correctly
1803 * as local route in the VNI always wins.
1805 evpn_route_select_install(bgp
, vpn
, rn
);
1807 * If the new local route was not selected evict it and tell zebra
1808 * to re-add the best remote dest. BGP doesn't retain non-best local
1811 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1813 evpn_cleanup_local_non_best_route(bgp
, vpn
, rn
, pi
);
1815 bgp_path_info_unlock(pi
);
1817 bgp_unlock_node(rn
);
1819 /* If this is a new route or some attribute has changed, export the
1820 * route to the global table. The route will be advertised to peers
1821 * from there. Note that this table is a 2-level tree (RD-level +
1822 * Prefix-level) similar to L3VPN routes.
1825 struct bgp_path_info
*global_pi
;
1827 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1828 (struct prefix
*)p
, &vpn
->prd
);
1829 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1,
1830 &global_pi
, flags
, seq
);
1832 /* Schedule for processing and unlock node. */
1833 bgp_process(bgp
, rn
, afi
, safi
);
1834 bgp_unlock_node(rn
);
1837 /* Unintern temporary. */
1838 aspath_unintern(&attr
.aspath
);
1844 * Delete EVPN route entry.
1845 * The entry can be in ESI/VNI table or the global table.
1847 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1848 struct bgp_node
*rn
,
1849 struct bgp_path_info
**pi
)
1851 struct bgp_path_info
*tmp_pi
;
1855 /* Now, find matching route. */
1856 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1857 tmp_pi
= tmp_pi
->next
)
1858 if (tmp_pi
->peer
== bgp
->peer_self
1859 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1860 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1865 /* Mark route for delete. */
1867 bgp_path_info_delete(rn
, tmp_pi
);
1872 /* Delete EVPN ES (type-4) route */
1873 static int delete_evpn_type4_route(struct bgp
*bgp
,
1875 struct prefix_evpn
*p
)
1877 afi_t afi
= AFI_L2VPN
;
1878 safi_t safi
= SAFI_EVPN
;
1879 struct bgp_path_info
*pi
;
1880 struct bgp_node
*rn
= NULL
; /* rn in esi table */
1881 struct bgp_node
*global_rn
= NULL
; /* rn in global table */
1883 /* First, locate the route node within the ESI.
1884 * If it doesn't exist, ther is nothing to do.
1885 * Note: there is no RD here.
1887 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
1891 /* Next, locate route node in the global EVPN routing table.
1892 * Note that this table is a 2-level tree (RD-level + Prefix-level)
1894 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1895 (struct prefix
*)p
, &es
->prd
);
1898 /* Delete route entry in the global EVPN table. */
1899 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1901 /* Schedule for processing - withdraws to peers happen from
1905 bgp_process(bgp
, global_rn
, afi
, safi
);
1906 bgp_unlock_node(global_rn
);
1910 * Delete route entry in the ESI route table.
1911 * This can just be removed.
1913 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
1915 bgp_path_info_reap(rn
, pi
);
1916 bgp_unlock_node(rn
);
1920 /* Delete EVPN type5 route */
1921 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1923 afi_t afi
= AFI_L2VPN
;
1924 safi_t safi
= SAFI_EVPN
;
1925 struct bgp_node
*rn
= NULL
;
1926 struct bgp_path_info
*pi
= NULL
;
1927 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
1929 bgp_def
= bgp_get_default();
1933 /* locate the global route entry for this type-5 prefix */
1934 rn
= bgp_afi_node_lookup(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1935 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1939 delete_evpn_route_entry(bgp_def
, afi
, safi
, rn
, &pi
);
1941 bgp_process(bgp_def
, rn
, afi
, safi
);
1942 bgp_unlock_node(rn
);
1947 * Delete EVPN route (of type based on prefix) for specified VNI and
1948 * schedule for processing.
1950 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1951 struct prefix_evpn
*p
)
1953 struct bgp_node
*rn
, *global_rn
;
1954 struct bgp_path_info
*pi
;
1955 afi_t afi
= AFI_L2VPN
;
1956 safi_t safi
= SAFI_EVPN
;
1958 /* First, locate the route node within the VNI. If it doesn't exist,
1960 * is nothing further to do.
1962 /* NOTE: There is no RD here. */
1963 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1967 /* Next, locate route node in the global EVPN routing table. Note that
1968 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1971 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1972 (struct prefix
*)p
, &vpn
->prd
);
1974 /* Delete route entry in the global EVPN table. */
1975 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1977 /* Schedule for processing - withdraws to peers happen from
1981 bgp_process(bgp
, global_rn
, afi
, safi
);
1982 bgp_unlock_node(global_rn
);
1985 /* Delete route entry in the VNI route table. This can just be removed.
1987 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
1989 bgp_path_info_reap(rn
, pi
);
1990 evpn_route_select_install(bgp
, vpn
, rn
);
1992 bgp_unlock_node(rn
);
1998 * Update all type-2 (MACIP) local routes for this VNI - these should also
1999 * be scheduled for advertise to peers.
2001 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2005 struct bgp_node
*rn
;
2006 struct bgp_path_info
*pi
, *tmp_pi
;
2008 struct attr
*attr_new
;
2010 int add_l3_ecomm
= 0;
2015 /* Walk this VNI's route table and update local type-2 routes. For any
2016 * routes updated, update corresponding entry in the global table too.
2018 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2019 rn
= bgp_route_next(rn
)) {
2020 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2021 struct bgp_node
*rd_rn
;
2022 struct bgp_path_info
*global_pi
;
2024 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2027 /* Identify local route. */
2028 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
2029 tmp_pi
= tmp_pi
->next
) {
2030 if (tmp_pi
->peer
== bgp
->peer_self
2031 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2032 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2040 * Build attribute per local route as the MAC mobility and
2041 * some other values could differ for different routes. The
2042 * attributes will be shared in the hash table.
2044 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
2045 attr
.nexthop
= vpn
->originator_ip
;
2046 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
2047 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
2048 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
2050 if (evpn_route_is_sticky(bgp
, rn
))
2052 else if (evpn_route_is_def_gw(bgp
, rn
)) {
2053 attr
.default_gw
= 1;
2054 if (is_evpn_prefix_ipaddr_v6(evp
))
2055 attr
.router_flag
= 1;
2058 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2059 * using L3 VNI for type-2 routes also.
2061 if ((is_evpn_prefix_ipaddr_v4(evp
) ||
2062 !IN6_IS_ADDR_LINKLOCAL(
2063 &evp
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
2064 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
2065 bgpevpn_get_l3vni(vpn
))
2068 /* Set up extended community. */
2069 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2071 seq
= mac_mobility_seqnum(tmp_pi
->attr
);
2073 /* Update the route entry. */
2074 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
, 0, &pi
,
2077 /* Perform route selection; this is just to set the flags
2078 * correctly as local route in the VNI always wins.
2080 evpn_route_select_install(bgp
, vpn
, rn
);
2082 attr_new
= pi
->attr
;
2084 /* Update route in global routing table. */
2085 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2086 (struct prefix
*)evp
, &vpn
->prd
);
2088 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
2090 mac_mobility_seqnum(attr_new
));
2092 /* Schedule for processing and unlock node. */
2093 bgp_process(bgp
, rd_rn
, afi
, safi
);
2094 bgp_unlock_node(rd_rn
);
2096 /* Unintern temporary. */
2097 aspath_unintern(&attr
.aspath
);
2105 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2106 * global routing table. These are also scheduled for withdraw from peers.
2108 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2112 struct bgp_node
*rdrn
, *rn
;
2113 struct bgp_table
*table
;
2114 struct bgp_path_info
*pi
;
2119 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
2120 if (rdrn
&& bgp_node_has_bgp_path_info_data(rdrn
)) {
2121 table
= bgp_node_get_bgp_table_info(rdrn
);
2122 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2123 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2125 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2128 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2130 bgp_process(bgp
, rn
, afi
, safi
);
2134 /* Unlock RD node. */
2136 bgp_unlock_node(rdrn
);
2142 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2143 * table as well as the per-VNI route table.
2145 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2149 struct bgp_node
*rn
;
2150 struct bgp_path_info
*pi
;
2155 /* First, walk the global route table for this VNI's type-2 local
2157 * EVPN routes are a 2-level table, first get the RD table.
2159 delete_global_type2_routes(bgp
, vpn
);
2161 /* Next, walk this VNI's route table and delete local type-2 routes. */
2162 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2163 rn
= bgp_route_next(rn
)) {
2164 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2166 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2169 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2171 /* Route entry in local table gets deleted immediately. */
2173 bgp_path_info_reap(rn
, pi
);
2180 * Delete all routes in per ES route-table
2182 static int delete_all_es_routes(struct bgp
*bgp
, struct evpnes
*es
)
2184 struct bgp_node
*rn
;
2185 struct bgp_path_info
*pi
, *nextpi
;
2187 /* Walk this ES's route table and delete all routes. */
2188 for (rn
= bgp_table_top(es
->route_table
); rn
;
2189 rn
= bgp_route_next(rn
)) {
2190 for (pi
= bgp_node_get_bgp_path_info(rn
);
2191 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2192 bgp_path_info_delete(rn
, pi
);
2193 bgp_path_info_reap(rn
, pi
);
2201 * Delete all routes in the per-VNI route table.
2203 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2205 struct bgp_node
*rn
;
2206 struct bgp_path_info
*pi
, *nextpi
;
2208 /* Walk this VNI's route table and delete all routes. */
2209 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2210 rn
= bgp_route_next(rn
)) {
2211 for (pi
= bgp_node_get_bgp_path_info(rn
);
2212 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2213 bgp_path_info_delete(rn
, pi
);
2214 bgp_path_info_reap(rn
, pi
);
2222 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2223 * export RT getting modified or change to tunnel IP. Note that these
2224 * situations need the route in the per-VNI table as well as the global
2225 * table to be updated (as attributes change).
2227 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2230 struct prefix_evpn p
;
2232 /* Update and advertise the type-3 route (only one) followed by the
2233 * locally learnt type-2 routes (MACIP) - for this VNI.
2235 * RT-3 only if doing head-end replication
2237 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
2238 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2239 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0);
2244 return update_all_type2_routes(bgp
, vpn
);
2247 /* Delete (and withdraw) local routes for specified ES from global and ES table.
2248 * Also remove all other routes from the per ES table.
2249 * Invoked when ES is deleted.
2251 static int delete_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
2254 char buf
[ESI_STR_LEN
];
2255 struct prefix_evpn p
;
2257 /* Delete and withdraw locally learnt ES route */
2258 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
.ipaddr_v4
);
2259 ret
= delete_evpn_type4_route(bgp
, es
, &p
);
2261 flog_err(EC_BGP_EVPN_ROUTE_DELETE
,
2262 "%u failed to delete type-4 route for ESI %s",
2263 bgp
->vrf_id
, esi_to_str(&es
->esi
, buf
, sizeof(buf
)));
2266 /* Delete all routes from per ES table */
2267 return delete_all_es_routes(bgp
, es
);
2271 * Delete (and withdraw) local routes for specified VNI from the global
2272 * table and per-VNI table. After this, remove all other routes from
2273 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2274 * (advertise-all-vni) being disabled.
2276 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2279 struct prefix_evpn p
;
2281 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2282 * followed by type-3 routes (only one) - for this VNI.
2284 ret
= delete_all_type2_routes(bgp
, vpn
);
2288 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2289 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2293 /* Delete all routes from the per-VNI table. */
2294 return delete_all_vni_routes(bgp
, vpn
);
2298 * There is a tunnel endpoint IP address change for this VNI, delete
2299 * prior type-3 route (if needed) and update.
2300 * Note: Route re-advertisement happens elsewhere after other processing
2303 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2304 struct in_addr originator_ip
)
2306 struct prefix_evpn p
;
2308 /* If VNI is not live, we only need to update the originator ip */
2309 if (!is_vni_live(vpn
)) {
2310 vpn
->originator_ip
= originator_ip
;
2314 /* Update the tunnel-ip hash */
2315 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2316 bgp_tip_add(bgp
, &originator_ip
);
2318 /* filter routes as martian nexthop db has changed */
2319 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2321 /* Need to withdraw type-3 route as the originator IP is part
2324 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2325 delete_evpn_route(bgp
, vpn
, &p
);
2327 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2328 vpn
->originator_ip
= originator_ip
;
2332 /* Install EVPN route entry in ES */
2333 static int install_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2334 struct prefix_evpn
*p
,
2335 struct bgp_path_info
*parent_pi
)
2338 struct bgp_node
*rn
= NULL
;
2339 struct bgp_path_info
*pi
= NULL
;
2340 struct attr
*attr_new
= NULL
;
2342 /* Create (or fetch) route within the VNI.
2343 * NOTE: There is no RD here.
2345 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
2347 /* Check if route entry is already present. */
2348 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2350 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2354 /* Add (or update) attribute to hash. */
2355 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2357 /* Create new route with its attribute. */
2358 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2359 parent_pi
->peer
, attr_new
, rn
);
2360 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2361 bgp_path_info_extra_get(pi
);
2362 pi
->extra
->parent
= parent_pi
;
2363 bgp_path_info_add(rn
, pi
);
2365 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2366 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2367 bgp_unlock_node(rn
);
2370 /* The attribute has changed. */
2371 /* Add (or update) attribute to hash. */
2372 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2374 /* Restore route, if needed. */
2375 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2376 bgp_path_info_restore(rn
, pi
);
2378 /* Mark if nexthop has changed. */
2379 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2380 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2382 /* Unintern existing, set to new. */
2383 bgp_attr_unintern(&pi
->attr
);
2384 pi
->attr
= attr_new
;
2385 pi
->uptime
= bgp_clock();
2388 /* Perform route selection and update zebra, if required. */
2389 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2394 * Install route entry into the VRF routing table and invoke route selection.
2396 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2397 struct prefix_evpn
*evp
,
2398 struct bgp_path_info
*parent_pi
)
2400 struct bgp_node
*rn
;
2401 struct bgp_path_info
*pi
;
2403 struct attr
*attr_new
;
2406 struct prefix
*pp
= &p
;
2409 char buf
[PREFIX_STRLEN
];
2410 char buf1
[PREFIX_STRLEN
];
2412 memset(pp
, 0, sizeof(struct prefix
));
2413 ip_prefix_from_evpn_prefix(evp
, pp
);
2415 if (bgp_debug_zebra(NULL
)) {
2417 "installing evpn prefix %s as ip prefix %s in vrf %s",
2418 prefix2str(evp
, buf
, sizeof(buf
)),
2419 prefix2str(pp
, buf1
, sizeof(buf
)),
2420 vrf_id_to_name(bgp_vrf
->vrf_id
));
2423 /* Create (or fetch) route within the VRF. */
2424 /* NOTE: There is no RD here. */
2425 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2427 safi
= SAFI_UNICAST
;
2428 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2429 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2431 safi
= SAFI_UNICAST
;
2432 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2436 /* EVPN routes currently only support a IPv4 next hop which corresponds
2437 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2438 * or prefix route, we have to convert the next hop to an IPv4-mapped
2439 * address for the rest of the code to flow through. In the case of IPv4,
2440 * make sure to set the flag for next hop attribute.
2442 bgp_attr_dup(&attr
, parent_pi
->attr
);
2444 evpn_convert_nexthop_to_ipv6(&attr
);
2446 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2448 /* Check if route entry is already present. */
2449 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2451 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2455 /* Add (or update) attribute to hash. */
2456 attr_new
= bgp_attr_intern(&attr
);
2458 /* Create new route with its attribute. */
2459 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2460 parent_pi
->peer
, attr_new
, rn
);
2461 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2462 bgp_path_info_extra_get(pi
);
2463 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2464 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2465 if (parent_pi
->extra
) {
2466 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2467 sizeof(pi
->extra
->label
));
2468 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2470 bgp_path_info_add(rn
, pi
);
2472 if (attrhash_cmp(pi
->attr
, &attr
)
2473 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2474 bgp_unlock_node(rn
);
2477 /* The attribute has changed. */
2478 /* Add (or update) attribute to hash. */
2479 attr_new
= bgp_attr_intern(&attr
);
2481 /* Restore route, if needed. */
2482 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2483 bgp_path_info_restore(rn
, pi
);
2485 /* Mark if nexthop has changed. */
2487 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2489 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2490 &attr_new
->mp_nexthop_global
)))
2491 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2493 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_ATTR_CHANGED
);
2494 /* Unintern existing, set to new. */
2495 bgp_attr_unintern(&pi
->attr
);
2496 pi
->attr
= attr_new
;
2497 pi
->uptime
= bgp_clock();
2500 bgp_aggregate_increment(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2502 /* Perform route selection and update zebra, if required. */
2503 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2505 /* Process for route leaking. */
2506 vpn_leak_from_vrf_update(bgp_get_default(), bgp_vrf
, pi
);
2512 * Install route entry into the VNI routing table and invoke route selection.
2514 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2515 struct prefix_evpn
*p
,
2516 struct bgp_path_info
*parent_pi
)
2518 struct bgp_node
*rn
;
2519 struct bgp_path_info
*pi
;
2520 struct attr
*attr_new
;
2523 /* Create (or fetch) route within the VNI. */
2524 /* NOTE: There is no RD here. */
2525 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2527 /* Check if route entry is already present. */
2528 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2530 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2534 /* Add (or update) attribute to hash. */
2535 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2537 /* Create new route with its attribute. */
2538 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2539 parent_pi
->peer
, attr_new
, rn
);
2540 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2541 bgp_path_info_extra_get(pi
);
2542 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2543 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2544 if (parent_pi
->extra
) {
2545 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2546 sizeof(pi
->extra
->label
));
2547 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2549 bgp_path_info_add(rn
, pi
);
2551 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2552 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2553 bgp_unlock_node(rn
);
2556 /* The attribute has changed. */
2557 /* Add (or update) attribute to hash. */
2558 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2560 /* Restore route, if needed. */
2561 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2562 bgp_path_info_restore(rn
, pi
);
2564 /* Mark if nexthop has changed. */
2565 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2566 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2568 /* Unintern existing, set to new. */
2569 bgp_attr_unintern(&pi
->attr
);
2570 pi
->attr
= attr_new
;
2571 pi
->uptime
= bgp_clock();
2574 /* Perform route selection and update zebra, if required. */
2575 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2580 /* Uninstall EVPN route entry from ES route table */
2581 static int uninstall_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2582 struct prefix_evpn
*p
,
2583 struct bgp_path_info
*parent_pi
)
2586 struct bgp_node
*rn
;
2587 struct bgp_path_info
*pi
;
2589 if (!es
->route_table
)
2592 /* Locate route within the ESI.
2593 * NOTE: There is no RD here.
2595 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2599 /* Find matching route entry. */
2600 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2602 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2608 /* Mark entry for deletion */
2609 bgp_path_info_delete(rn
, pi
);
2611 /* Perform route selection and update zebra, if required. */
2612 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2614 /* Unlock route node. */
2615 bgp_unlock_node(rn
);
2621 * Uninstall route entry from the VRF routing table and send message
2622 * to zebra, if appropriate.
2624 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2625 struct prefix_evpn
*evp
,
2626 struct bgp_path_info
*parent_pi
)
2628 struct bgp_node
*rn
;
2629 struct bgp_path_info
*pi
;
2632 struct prefix
*pp
= &p
;
2635 char buf
[PREFIX_STRLEN
];
2636 char buf1
[PREFIX_STRLEN
];
2638 memset(pp
, 0, sizeof(struct prefix
));
2639 ip_prefix_from_evpn_prefix(evp
, pp
);
2641 if (bgp_debug_zebra(NULL
)) {
2643 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2644 prefix2str(evp
, buf
, sizeof(buf
)),
2645 prefix2str(pp
, buf1
, sizeof(buf
)),
2646 vrf_id_to_name(bgp_vrf
->vrf_id
));
2649 /* Locate route within the VRF. */
2650 /* NOTE: There is no RD here. */
2651 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2653 safi
= SAFI_UNICAST
;
2654 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2657 safi
= SAFI_UNICAST
;
2658 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2664 /* Find matching route entry. */
2665 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2667 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2673 /* Process for route leaking. */
2674 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp_vrf
, pi
);
2676 bgp_aggregate_decrement(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2678 /* Mark entry for deletion */
2679 bgp_path_info_delete(rn
, pi
);
2681 /* Perform route selection and update zebra, if required. */
2682 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2684 /* Unlock route node. */
2685 bgp_unlock_node(rn
);
2691 * Uninstall route entry from the VNI routing table and send message
2692 * to zebra, if appropriate.
2694 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2695 struct prefix_evpn
*p
,
2696 struct bgp_path_info
*parent_pi
)
2698 struct bgp_node
*rn
;
2699 struct bgp_path_info
*pi
;
2702 /* Locate route within the VNI. */
2703 /* NOTE: There is no RD here. */
2704 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2708 /* Find matching route entry. */
2709 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2711 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2717 /* Mark entry for deletion */
2718 bgp_path_info_delete(rn
, pi
);
2720 /* Perform route selection and update zebra, if required. */
2721 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2723 /* Unlock route node. */
2724 bgp_unlock_node(rn
);
2730 * Given a prefix, see if it belongs to ES.
2732 static int is_prefix_matching_for_es(struct prefix_evpn
*p
,
2735 /* if not an ES route return false */
2736 if (p
->prefix
.route_type
!= BGP_EVPN_ES_ROUTE
)
2739 if (memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
)) == 0)
2746 * Given a route entry and a VRF, see if this route entry should be
2747 * imported into the VRF i.e., RTs match.
2749 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2750 struct bgp_path_info
*pi
)
2752 struct attr
*attr
= pi
->attr
;
2753 struct ecommunity
*ecom
;
2757 /* Route should have valid RT to be even considered. */
2758 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2761 ecom
= attr
->ecommunity
;
2762 if (!ecom
|| !ecom
->size
)
2765 /* For each extended community RT, see if it matches this VNI. If any RT
2766 * matches, we're done.
2768 for (i
= 0; i
< ecom
->size
; i
++) {
2770 uint8_t type
, sub_type
;
2771 struct ecommunity_val
*eval
;
2772 struct ecommunity_val eval_tmp
;
2773 struct vrf_irt_node
*irt
;
2775 /* Only deal with RTs */
2776 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2777 eval
= (struct ecommunity_val
*)(ecom
->val
2778 + (i
* ECOMMUNITY_SIZE
));
2781 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2784 /* See if this RT matches specified VNIs import RTs */
2785 irt
= lookup_vrf_import_rt(eval
);
2787 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2790 /* Also check for non-exact match. In this, we mask out the AS
2792 * only check on the local-admin sub-field. This is to
2794 * VNI as the RT for EBGP peering too.
2797 if (type
== ECOMMUNITY_ENCODE_AS
2798 || type
== ECOMMUNITY_ENCODE_AS4
2799 || type
== ECOMMUNITY_ENCODE_IP
) {
2800 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2801 mask_ecom_global_admin(&eval_tmp
, eval
);
2802 irt
= lookup_vrf_import_rt(&eval_tmp
);
2805 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2813 * Given a route entry and a VNI, see if this route entry should be
2814 * imported into the VNI i.e., RTs match.
2816 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2817 struct bgp_path_info
*pi
)
2819 struct attr
*attr
= pi
->attr
;
2820 struct ecommunity
*ecom
;
2824 /* Route should have valid RT to be even considered. */
2825 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2828 ecom
= attr
->ecommunity
;
2829 if (!ecom
|| !ecom
->size
)
2832 /* For each extended community RT, see if it matches this VNI. If any RT
2833 * matches, we're done.
2835 for (i
= 0; i
< ecom
->size
; i
++) {
2837 uint8_t type
, sub_type
;
2838 struct ecommunity_val
*eval
;
2839 struct ecommunity_val eval_tmp
;
2840 struct irt_node
*irt
;
2842 /* Only deal with RTs */
2843 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2844 eval
= (struct ecommunity_val
*)(ecom
->val
2845 + (i
* ECOMMUNITY_SIZE
));
2848 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2851 /* See if this RT matches specified VNIs import RTs */
2852 irt
= lookup_import_rt(bgp
, eval
);
2854 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2857 /* Also check for non-exact match. In this, we mask out the AS
2859 * only check on the local-admin sub-field. This is to
2861 * VNI as the RT for EBGP peering too.
2864 if (type
== ECOMMUNITY_ENCODE_AS
2865 || type
== ECOMMUNITY_ENCODE_AS4
2866 || type
== ECOMMUNITY_ENCODE_IP
) {
2867 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2868 mask_ecom_global_admin(&eval_tmp
, eval
);
2869 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2872 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2879 static int install_uninstall_routes_for_es(struct bgp
*bgp
,
2886 char buf
[PREFIX_STRLEN
];
2887 char buf1
[ESI_STR_LEN
];
2888 struct bgp_node
*rd_rn
, *rn
;
2889 struct bgp_table
*table
;
2890 struct bgp_path_info
*pi
;
2896 * Walk entire global routing table and evaluate routes which could be
2897 * imported into this VRF. Note that we need to loop through all global
2898 * routes to determine which route matches the import rt on vrf
2900 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2901 rd_rn
= bgp_route_next(rd_rn
)) {
2902 table
= bgp_node_get_bgp_table_info(rd_rn
);
2906 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2907 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2909 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
2912 * Consider "valid" remote routes applicable for
2915 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
2916 && pi
->type
== ZEBRA_ROUTE_BGP
2917 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
2920 if (!is_prefix_matching_for_es(evp
, es
))
2924 ret
= install_evpn_route_entry_in_es(
2927 ret
= uninstall_evpn_route_entry_in_es(
2933 "Failed to %s EVPN %s route in ESI %s",
2936 prefix2str(evp
, buf
,
2938 esi_to_str(&es
->esi
, buf1
,
2949 * Install or uninstall mac-ip routes are appropriate for this
2952 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
2956 struct bgp_node
*rd_rn
, *rn
;
2957 struct bgp_table
*table
;
2958 struct bgp_path_info
*pi
;
2960 char buf
[PREFIX_STRLEN
];
2961 struct bgp
*bgp_def
= NULL
;
2965 bgp_def
= bgp_get_default();
2969 /* Walk entire global routing table and evaluate routes which could be
2970 * imported into this VRF. Note that we need to loop through all global
2971 * routes to determine which route matches the import rt on vrf
2973 for (rd_rn
= bgp_table_top(bgp_def
->rib
[afi
][safi
]); rd_rn
;
2974 rd_rn
= bgp_route_next(rd_rn
)) {
2975 table
= bgp_node_get_bgp_table_info(rd_rn
);
2979 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2980 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2982 /* if not mac-ip route skip this route */
2983 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2984 || evp
->prefix
.route_type
2985 == BGP_EVPN_IP_PREFIX_ROUTE
))
2988 /* if not a mac+ip route skip this route */
2989 if (!(is_evpn_prefix_ipaddr_v4(evp
)
2990 || is_evpn_prefix_ipaddr_v6(evp
)))
2993 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
2995 /* Consider "valid" remote routes applicable for
2998 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
2999 && pi
->type
== ZEBRA_ROUTE_BGP
3000 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3003 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
3005 ret
= install_evpn_route_entry_in_vrf(
3008 ret
= uninstall_evpn_route_entry_in_vrf(
3014 "Failed to %s EVPN %s route in VRF %s",
3017 prefix2str(evp
, buf
,
3032 * Install or uninstall routes of specified type that are appropriate for this
3035 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
3036 struct bgpevpn
*vpn
,
3037 bgp_evpn_route_type rtype
,
3042 struct bgp_node
*rd_rn
, *rn
;
3043 struct bgp_table
*table
;
3044 struct bgp_path_info
*pi
;
3050 /* Walk entire global routing table and evaluate routes which could be
3051 * imported into this VPN. Note that we cannot just look at the routes
3053 * the VNI's RD - remote routes applicable for this VNI could have any
3056 /* EVPN routes are a 2-level table. */
3057 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
3058 rd_rn
= bgp_route_next(rd_rn
)) {
3059 table
= bgp_node_get_bgp_table_info(rd_rn
);
3063 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3064 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3066 if (evp
->prefix
.route_type
!= rtype
)
3069 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3071 /* Consider "valid" remote routes applicable for
3073 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3074 && pi
->type
== ZEBRA_ROUTE_BGP
3075 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3078 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3080 ret
= install_evpn_route_entry(
3083 ret
= uninstall_evpn_route_entry(
3089 "%u: Failed to %s EVPN %s route in VNI %u",
3093 rtype
== BGP_EVPN_MAC_IP_ROUTE
3107 /* Install any existing remote ES routes applicable for this ES into its routing
3108 * table. This is invoked when ES comes up.
3110 static int install_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
3112 return install_uninstall_routes_for_es(bgp
, es
, 1);
3116 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3117 * is invoked upon l3vni-add or l3vni import rt change
3119 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3121 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3126 * Install any existing remote routes applicable for this VNI into its
3127 * routing table. This is invoked when a VNI becomes "live" or its Import
3130 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3134 /* Install type-3 routes followed by type-2 routes - the ones applicable
3137 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3142 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3146 /* uninstall routes from l3vni vrf. */
3147 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3149 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3154 * Uninstall any existing remote routes for this VNI. One scenario in which
3155 * this is invoked is upon an import RT change.
3157 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3161 /* Uninstall type-2 routes followed by type-3 routes - the ones
3165 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3170 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3174 /* Install or unistall route in ES */
3175 static int install_uninstall_route_in_es(struct bgp
*bgp
, struct evpnes
*es
,
3176 afi_t afi
, safi_t safi
,
3177 struct prefix_evpn
*evp
,
3178 struct bgp_path_info
*pi
, int install
)
3181 char buf
[ESI_STR_LEN
];
3184 ret
= install_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3186 ret
= uninstall_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3191 "%u: Failed to %s EVPN %s route in ESI %s", bgp
->vrf_id
,
3192 install
? "install" : "uninstall", "ES",
3193 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)));
3200 * Install or uninstall route in matching VRFs (list).
3202 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3203 safi_t safi
, struct prefix_evpn
*evp
,
3204 struct bgp_path_info
*pi
,
3205 struct list
*vrfs
, int install
)
3207 char buf
[PREFIX2STR_BUFFER
];
3208 struct bgp
*bgp_vrf
;
3209 struct listnode
*node
, *nnode
;
3211 /* Only type-2/type-5 routes go into a VRF */
3212 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3213 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3216 /* if it is type-2 route and not a mac+ip route skip this route */
3217 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3218 && !(is_evpn_prefix_ipaddr_v4(evp
)
3219 || is_evpn_prefix_ipaddr_v6(evp
)))
3222 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3226 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3228 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3232 flog_err(EC_BGP_EVPN_FAIL
,
3233 "%u: Failed to %s prefix %s in VRF %s",
3235 install
? "install" : "uninstall",
3236 prefix2str(evp
, buf
, sizeof(buf
)),
3237 vrf_id_to_name(bgp_vrf
->vrf_id
));
3246 * Install or uninstall route in matching VNIs (list).
3248 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3249 safi_t safi
, struct prefix_evpn
*evp
,
3250 struct bgp_path_info
*pi
,
3251 struct list
*vnis
, int install
)
3253 struct bgpevpn
*vpn
;
3254 struct listnode
*node
, *nnode
;
3256 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3259 if (!is_vni_live(vpn
))
3263 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3265 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3268 flog_err(EC_BGP_EVPN_FAIL
,
3269 "%u: Failed to %s EVPN %s route in VNI %u",
3270 bgp
->vrf_id
, install
? "install" : "uninstall",
3271 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3283 * Install or uninstall route for appropriate VNIs/ESIs.
3285 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3287 struct bgp_path_info
*pi
, int import
)
3289 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3290 struct attr
*attr
= pi
->attr
;
3291 struct ecommunity
*ecom
;
3296 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3297 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3298 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3299 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3300 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3303 /* If we don't have Route Target, nothing much to do. */
3304 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3307 ecom
= attr
->ecommunity
;
3308 if (!ecom
|| !ecom
->size
)
3311 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3312 * attached to the route */
3313 for (i
= 0; i
< ecom
->size
; i
++) {
3315 uint8_t type
, sub_type
;
3316 struct ecommunity_val
*eval
;
3317 struct ecommunity_val eval_tmp
;
3318 struct irt_node
*irt
; /* import rt for l2vni */
3319 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3322 /* Only deal with RTs */
3323 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3324 eval
= (struct ecommunity_val
*)(ecom
->val
3325 + (i
* ECOMMUNITY_SIZE
));
3328 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3332 * macip routes (type-2) are imported into VNI and VRF tables.
3333 * IMET route is imported into VNI table.
3334 * prefix routes are imported into VRF table.
3336 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3337 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3338 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3340 irt
= lookup_import_rt(bgp
, eval
);
3342 install_uninstall_route_in_vnis(
3343 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3346 vrf_irt
= lookup_vrf_import_rt(eval
);
3348 install_uninstall_route_in_vrfs(
3349 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3352 /* Also check for non-exact match.
3353 * In this, we mask out the AS and
3354 * only check on the local-admin sub-field.
3355 * This is to facilitate using
3356 * VNI as the RT for EBGP peering too.
3360 if (type
== ECOMMUNITY_ENCODE_AS
3361 || type
== ECOMMUNITY_ENCODE_AS4
3362 || type
== ECOMMUNITY_ENCODE_IP
) {
3363 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3364 mask_ecom_global_admin(&eval_tmp
, eval
);
3365 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3366 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3370 install_uninstall_route_in_vnis(
3371 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3374 install_uninstall_route_in_vrfs(
3375 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3379 /* es route is imported into the es table */
3380 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3382 /* we will match based on the entire esi to avoid
3383 * imoort of an es route for esi2 into esi1
3385 es
= bgp_evpn_lookup_es(bgp
, &evp
->prefix
.es_addr
.esi
);
3386 if (es
&& is_es_local(es
))
3387 install_uninstall_route_in_es(
3388 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3396 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3399 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3401 /* Delete ipv4 default route and withdraw from peers */
3402 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3403 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3404 SAFI_UNICAST
, false);
3406 /* delete all ipv4 routes and withdraw from peers */
3407 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3408 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3410 /* Delete ipv6 default route and withdraw from peers */
3411 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3412 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3413 SAFI_UNICAST
, false);
3415 /* delete all ipv6 routes and withdraw from peers */
3416 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3417 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3421 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3424 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3426 /* update all ipv4 routes */
3427 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3428 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3430 /* update ipv4 default route and withdraw from peers */
3431 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3432 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3433 SAFI_UNICAST
, true);
3435 /* update all ipv6 routes */
3436 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3437 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3439 /* update ipv6 default route and withdraw from peers */
3440 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3441 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3442 SAFI_UNICAST
, true);
3447 * update and advertise local routes for a VRF as type-5 routes.
3448 * This is invoked upon RD change for a VRF. Note taht the processing is only
3449 * done in the global route table using the routes which already exist in the
3452 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3454 /* skip if the RD is configured */
3455 if (is_vrf_rd_configured(bgp_vrf
))
3458 /* derive the RD for the VRF based on new router-id */
3459 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3461 /* update advertise ipv4|ipv6 routes as type-5 routes */
3462 update_advertise_vrf_routes(bgp_vrf
);
3466 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3467 * This is invoked upon VRF RD change. The processing is done only from global
3470 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3472 /* skip if the RD is configured */
3473 if (is_vrf_rd_configured(bgp_vrf
))
3476 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3477 delete_withdraw_vrf_routes(bgp_vrf
);
3481 * Update and advertise local routes for a VNI. Invoked upon router-id
3482 * change. Note that the processing is done only on the global route table
3483 * using routes that already exist in the per-VNI table.
3485 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3487 struct prefix_evpn p
;
3488 struct bgp_node
*rn
, *global_rn
;
3489 struct bgp_path_info
*pi
, *global_pi
;
3491 afi_t afi
= AFI_L2VPN
;
3492 safi_t safi
= SAFI_EVPN
;
3494 /* Locate type-3 route for VNI in the per-VNI table and use its
3495 * attributes to create and advertise the type-3 route for this VNI
3496 * in the global table.
3498 * RT-3 only if doing head-end replication
3500 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
3501 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3502 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3503 if (!rn
) /* unexpected */
3505 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3506 if (pi
->peer
== bgp
->peer_self
&&
3507 pi
->type
== ZEBRA_ROUTE_BGP
3508 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3510 if (!pi
) /* unexpected */
3514 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3515 (struct prefix
*)&p
, &vpn
->prd
);
3516 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
,
3517 1, &pi
, 0, mac_mobility_seqnum(attr
));
3519 /* Schedule for processing and unlock node. */
3520 bgp_process(bgp
, global_rn
, afi
, safi
);
3521 bgp_unlock_node(global_rn
);
3524 /* Now, walk this VNI's route table and use the route and its attribute
3525 * to create and schedule route in global table.
3527 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
3528 rn
= bgp_route_next(rn
)) {
3529 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3531 /* Identify MAC-IP local routes. */
3532 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3535 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3536 if (pi
->peer
== bgp
->peer_self
3537 && pi
->type
== ZEBRA_ROUTE_BGP
3538 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3543 /* Create route in global routing table using this route entry's
3547 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3548 (struct prefix
*)evp
, &vpn
->prd
);
3550 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
3552 mac_mobility_seqnum(attr
));
3554 /* Schedule for processing and unlock node. */
3555 bgp_process(bgp
, global_rn
, afi
, safi
);
3556 bgp_unlock_node(global_rn
);
3563 * Delete (and withdraw) local routes for a VNI - only from the global
3564 * table. Invoked upon router-id change.
3566 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3569 struct prefix_evpn p
;
3570 struct bgp_node
*global_rn
;
3571 struct bgp_path_info
*pi
;
3572 afi_t afi
= AFI_L2VPN
;
3573 safi_t safi
= SAFI_EVPN
;
3575 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3576 * for this VNI - from the global table.
3578 ret
= delete_global_type2_routes(bgp
, vpn
);
3582 /* Remove type-3 route for this VNI from global table. */
3583 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3584 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3585 (struct prefix
*)&p
, &vpn
->prd
);
3587 /* Delete route entry in the global EVPN table. */
3588 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
3590 /* Schedule for processing - withdraws to peers happen from
3594 bgp_process(bgp
, global_rn
, afi
, safi
);
3595 bgp_unlock_node(global_rn
);
3602 * Handle router-id change. Update and advertise local routes corresponding
3603 * to this VNI from peers. Note that this is invoked after updating the
3604 * router-id. The routes in the per-VNI table are used to create routes in
3605 * the global table and schedule them.
3607 static void update_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3609 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3611 /* Skip VNIs with configured RD. */
3612 if (is_rd_configured(vpn
))
3615 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3616 update_advertise_vni_routes(bgp
, vpn
);
3620 * Handle router-id change. Delete and withdraw local routes corresponding
3621 * to this VNI from peers. Note that this is invoked prior to updating
3622 * the router-id and is done only on the global route table, the routes
3623 * are needed in the per-VNI table to re-advertise with new router id.
3625 static void withdraw_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3627 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3629 /* Skip VNIs with configured RD. */
3630 if (is_rd_configured(vpn
))
3633 delete_withdraw_vni_routes(bgp
, vpn
);
3637 * Create RT-3 for a VNI and schedule for processing and advertisement.
3638 * This is invoked upon flooding mode changing to head-end replication.
3640 static void create_advertise_type3(struct hash_bucket
*bucket
, void *data
)
3642 struct bgpevpn
*vpn
= bucket
->data
;
3643 struct bgp
*bgp
= data
;
3644 struct prefix_evpn p
;
3646 if (!vpn
|| !is_vni_live(vpn
))
3649 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3650 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0))
3651 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3652 "Type3 route creation failure for VNI %u", vpn
->vni
);
3656 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3657 * This is invoked upon flooding mode changing to drop BUM packets.
3659 static void delete_withdraw_type3(struct hash_bucket
*bucket
, void *data
)
3661 struct bgpevpn
*vpn
= bucket
->data
;
3662 struct bgp
*bgp
= data
;
3663 struct prefix_evpn p
;
3665 if (!vpn
|| !is_vni_live(vpn
))
3668 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3669 delete_evpn_route(bgp
, vpn
, &p
);
3673 * Process received EVPN type-2 route (advertise or withdraw).
3675 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3676 struct attr
*attr
, uint8_t *pfx
, int psize
,
3677 uint32_t addpath_id
)
3679 struct prefix_rd prd
;
3680 struct prefix_evpn p
;
3681 struct bgp_route_evpn evpn
;
3683 uint8_t macaddr_len
;
3684 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
3685 uint32_t num_labels
= 0;
3689 /* Type-2 route should be either 33, 37 or 49 bytes or an
3690 * additional 3 bytes if there is a second label (VNI):
3691 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3692 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3693 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3695 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3696 && psize
!= 40 && psize
!= 52) {
3697 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3698 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3699 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3703 memset(&evpn
, 0, sizeof(evpn
));
3705 /* Make prefix_rd */
3706 prd
.family
= AF_UNSPEC
;
3708 memcpy(&prd
.val
, pfx
, 8);
3711 /* Make EVPN prefix. */
3712 memset(&p
, 0, sizeof(struct prefix_evpn
));
3714 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3715 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3717 /* Copy Ethernet Seg Identifier */
3718 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
3721 /* Copy Ethernet Tag */
3722 memcpy(ð_tag
, pfx
, 4);
3723 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3726 /* Get the MAC Addr len */
3727 macaddr_len
= *pfx
++;
3729 /* Get the MAC Addr */
3730 if (macaddr_len
== (ETH_ALEN
* 8)) {
3731 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
3735 EC_BGP_EVPN_ROUTE_INVALID
,
3736 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3737 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3743 ipaddr_len
= *pfx
++;
3744 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3745 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3747 EC_BGP_EVPN_ROUTE_INVALID
,
3748 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3749 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3754 ipaddr_len
/= 8; /* Convert to bytes. */
3755 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3758 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
3762 /* Get the VNI(s). Stored as bytes here. */
3764 memset(label
, 0, sizeof(label
));
3765 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
3766 pfx
+= BGP_LABEL_BYTES
;
3767 psize
-= (33 + ipaddr_len
);
3768 /* Do we have a second VNI? */
3771 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
3773 * If in future, we are required to access additional fields,
3774 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
3779 /* Process the route. */
3781 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3782 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3783 &prd
, &label
[0], num_labels
, 0, &evpn
);
3785 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3786 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3787 &prd
, &label
[0], num_labels
, &evpn
);
3792 * Process received EVPN type-3 route (advertise or withdraw).
3794 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3795 struct attr
*attr
, uint8_t *pfx
, int psize
,
3796 uint32_t addpath_id
)
3798 struct prefix_rd prd
;
3799 struct prefix_evpn p
;
3804 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3805 * IP len (1) and IP (4 or 16).
3807 if (psize
!= 17 && psize
!= 29) {
3808 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3809 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3810 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3814 /* If PMSI is present, log if it is anything other than IR.
3815 * Note: We just simply ignore the values as it is not clear if
3816 * doing anything else is better.
3819 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3820 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
) {
3822 EC_BGP_EVPN_PMSI_PRESENT
,
3823 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3824 peer
->bgp
->vrf_id
, peer
->host
,
3825 attr
->pmsi_tnl_type
);
3829 /* Make prefix_rd */
3830 prd
.family
= AF_UNSPEC
;
3832 memcpy(&prd
.val
, pfx
, 8);
3835 /* Make EVPN prefix. */
3836 memset(&p
, 0, sizeof(struct prefix_evpn
));
3838 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3839 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3841 /* Copy Ethernet Tag */
3842 memcpy(ð_tag
, pfx
, 4);
3843 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
3847 ipaddr_len
= *pfx
++;
3848 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3849 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
3850 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
3853 EC_BGP_EVPN_ROUTE_INVALID
,
3854 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3855 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3859 /* Process the route. */
3861 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3862 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3863 &prd
, NULL
, 0, 0, NULL
);
3865 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3866 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3867 &prd
, NULL
, 0, NULL
);
3872 * Process received EVPN type-4 route (advertise or withdraw).
3874 static int process_type4_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3875 struct attr
*attr
, uint8_t *pfx
, int psize
,
3876 uint32_t addpath_id
)
3881 struct in_addr vtep_ip
;
3882 struct prefix_rd prd
;
3883 struct prefix_evpn p
;
3885 /* Type-4 route should be either 23 or 35 bytes
3886 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
3888 if (psize
!= 23 && psize
!= 35) {
3889 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3890 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
3891 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3895 /* Make prefix_rd */
3896 prd
.family
= AF_UNSPEC
;
3898 memcpy(&prd
.val
, pfx
, 8);
3902 memcpy(&esi
, pfx
, ESI_BYTES
);
3907 ipaddr_len
= *pfx
++;
3908 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3909 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
3912 EC_BGP_EVPN_ROUTE_INVALID
,
3913 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
3914 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3918 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
3919 /* Process the route. */
3921 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3922 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3923 &prd
, NULL
, 0, 0, NULL
);
3925 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3926 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3927 &prd
, NULL
, 0, NULL
);
3934 * Process received EVPN type-5 route (advertise or withdraw).
3936 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3937 struct attr
*attr
, uint8_t *pfx
, int psize
,
3938 uint32_t addpath_id
)
3940 struct prefix_rd prd
;
3941 struct prefix_evpn p
;
3942 struct bgp_route_evpn evpn
;
3945 mpls_label_t label
; /* holds the VNI as in the packet */
3948 /* Type-5 route should be 34 or 58 bytes:
3949 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3950 * GW (4 or 16) and VNI (3).
3951 * Note that the IP and GW should both be IPv4 or both IPv6.
3953 if (psize
!= 34 && psize
!= 58) {
3954 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3955 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3956 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3960 /* Make prefix_rd */
3961 prd
.family
= AF_UNSPEC
;
3963 memcpy(&prd
.val
, pfx
, 8);
3966 /* Make EVPN prefix. */
3967 memset(&p
, 0, sizeof(struct prefix_evpn
));
3969 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3970 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
3972 /* Additional information outside of prefix - ESI and GW IP */
3973 memset(&evpn
, 0, sizeof(evpn
));
3976 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
3979 /* Fetch Ethernet Tag. */
3980 memcpy(ð_tag
, pfx
, 4);
3981 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
3984 /* Fetch IP prefix length. */
3986 if (ippfx_len
> IPV6_MAX_BITLEN
) {
3988 EC_BGP_EVPN_ROUTE_INVALID
,
3989 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3990 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
3993 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
3995 /* Determine IPv4 or IPv6 prefix */
3996 /* Since the address and GW are from the same family, this just becomes
3997 * a simple check on the total size.
4000 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
4001 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
4003 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
4006 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
4007 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
4009 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
4013 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4014 memset(&label
, 0, sizeof(label
));
4015 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
4018 * If in future, we are required to access additional fields,
4019 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4023 /* Process the route. */
4025 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4026 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4027 &prd
, &label
, 1, 0, &evpn
);
4029 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4030 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4031 &prd
, &label
, 1, &evpn
);
4036 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
4037 struct prefix_rd
*prd
, mpls_label_t
*label
,
4038 uint32_t num_labels
, struct attr
*attr
)
4042 struct evpn_addr
*p_evpn_p
;
4044 memset(&temp
, 0, 16);
4045 if (p
->family
!= AF_EVPN
)
4047 p_evpn_p
= &(p
->u
.prefix_evpn
);
4049 /* len denites the total len of IP and GW-IP in the route
4050 IP and GW-IP have to be both ipv4 or ipv6
4052 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4053 len
= 8; /* IP and GWIP are both ipv4 */
4055 len
= 32; /* IP and GWIP are both ipv6 */
4056 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4057 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4058 stream_put(s
, prd
->val
, 8);
4060 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
4062 stream_put(s
, &temp
, 10);
4063 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4064 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4065 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4066 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4068 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4070 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4072 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
4074 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
4076 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4077 stream_put_ipv4(s
, 0);
4079 stream_put(s
, &temp
, 16);
4083 stream_put(s
, label
, 3);
4089 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4091 static void cleanup_vni_on_disable(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4093 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4095 /* Remove EVPN routes and schedule for processing. */
4096 delete_routes_for_vni(bgp
, vpn
);
4098 /* Clear "live" flag and see if hash needs to be freed. */
4099 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4100 if (!is_vni_configured(vpn
))
4101 bgp_evpn_free(bgp
, vpn
);
4105 * Free a VNI entry; iterator function called during cleanup.
4107 static void free_vni_entry(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4109 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4111 delete_all_vni_routes(bgp
, vpn
);
4112 bgp_evpn_free(bgp
, vpn
);
4116 * Derive AUTO import RT for BGP VRF - L3VNI
4118 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4120 struct bgp
*bgp_def
= NULL
;
4122 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4123 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4126 bgp_def
= bgp_get_default();
4129 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4133 * Delete AUTO import RT from BGP VRF - L3VNI
4135 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4137 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4141 * Derive AUTO export RT for BGP VRF - L3VNI
4143 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4145 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4146 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4150 * Delete AUTO export RT from BGP VRF - L3VNI
4152 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4154 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4157 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4159 struct bgp
*bgp_def
= NULL
;
4160 struct listnode
*node
= NULL
;
4161 struct bgpevpn
*vpn
= NULL
;
4163 bgp_def
= bgp_get_default();
4167 /* update all type-5 routes */
4168 update_advertise_vrf_routes(bgp_vrf
);
4170 /* update all type-2 routes */
4171 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4172 update_routes_for_vni(bgp_def
, vpn
);
4176 * Handle autort change for a given VNI.
4178 static void update_autort_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4180 struct bgpevpn
*vpn
= bucket
->data
;
4182 if (!is_import_rt_configured(vpn
)) {
4183 if (is_vni_live(vpn
))
4184 bgp_evpn_uninstall_routes(bgp
, vpn
);
4185 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4186 list_delete_all_node(vpn
->import_rtl
);
4187 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4188 if (is_vni_live(vpn
))
4189 bgp_evpn_install_routes(bgp
, vpn
);
4191 if (!is_export_rt_configured(vpn
)) {
4192 list_delete_all_node(vpn
->export_rtl
);
4193 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4194 if (is_vni_live(vpn
))
4195 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4203 /* withdraw type-5 route corresponding to ip prefix */
4204 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4205 afi_t afi
, safi_t safi
)
4208 struct prefix_evpn evp
;
4209 char buf
[PREFIX_STRLEN
];
4211 build_type5_prefix_from_ip_prefix(&evp
, p
);
4212 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4215 EC_BGP_EVPN_ROUTE_DELETE
,
4216 "%u failed to delete type-5 route for prefix %s in vrf %s",
4217 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4218 vrf_id_to_name(bgp_vrf
->vrf_id
));
4222 /* withdraw all type-5 routes for an address family */
4223 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4225 struct bgp_table
*table
= NULL
;
4226 struct bgp_node
*rn
= NULL
;
4227 struct bgp_path_info
*pi
;
4229 table
= bgp_vrf
->rib
[afi
][safi
];
4230 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4231 /* Only care about "selected" routes. Also ensure that
4232 * these are routes that are injectable into EVPN.
4234 /* TODO: Support for AddPath for EVPN. */
4235 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4236 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4237 && is_route_injectable_into_evpn(pi
)) {
4238 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
4247 * evpn - enable advertisement of default g/w
4249 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
4250 safi_t safi
, bool add
)
4252 struct prefix ip_prefix
;
4254 /* form the default prefix 0.0.0.0/0 */
4255 memset(&ip_prefix
, 0, sizeof(struct prefix
));
4256 ip_prefix
.family
= afi2family(afi
);
4259 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
4262 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
4269 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4270 * to this function correspond to those of the source IP prefix (best
4271 * path in the case of the attr. In the case of a local prefix (when we
4272 * are advertising local subnets), the src_attr will be NULL.
4274 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4275 struct attr
*src_attr
, afi_t afi
,
4279 struct prefix_evpn evp
;
4280 char buf
[PREFIX_STRLEN
];
4282 build_type5_prefix_from_ip_prefix(&evp
, p
);
4283 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4285 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4286 "%u: Failed to create type-5 route for prefix %s",
4287 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4290 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4291 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4292 * advertisement is enabled.
4294 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4297 struct bgp_table
*table
= NULL
;
4298 struct bgp_node
*rn
= NULL
;
4299 struct bgp_path_info
*pi
;
4301 table
= bgp_vrf
->rib
[afi
][safi
];
4302 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4303 /* Need to identify the "selected" route entry to use its
4304 * attribute. Also, ensure that the route is injectable
4306 * TODO: Support for AddPath for EVPN.
4308 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4309 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4310 && is_route_injectable_into_evpn(pi
)) {
4312 /* apply the route-map */
4313 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4316 ret
= route_map_apply(
4317 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4319 &rn
->p
, RMAP_BGP
, pi
);
4320 if (ret
== RMAP_DENYMATCH
)
4323 bgp_evpn_advertise_type5_route(
4324 bgp_vrf
, &rn
->p
, pi
->attr
, afi
, safi
);
4331 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4333 struct listnode
*node
, *nnode
, *node_to_del
;
4334 struct ecommunity
*ecom
, *ecom_auto
;
4335 struct ecommunity_val eval
;
4337 if (bgp
->advertise_autort_rfc8365
)
4338 vni
|= EVPN_AUTORT_VXLAN
;
4339 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4341 ecom_auto
= ecommunity_new();
4342 ecommunity_add_val(ecom_auto
, &eval
);
4345 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4346 if (ecommunity_match(ecom
, ecom_auto
)) {
4347 ecommunity_free(&ecom
);
4353 list_delete_node(rtl
, node_to_del
);
4355 ecommunity_free(&ecom_auto
);
4358 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4359 struct ecommunity
*ecomadd
)
4361 /* uninstall routes from vrf */
4362 uninstall_routes_for_vrf(bgp_vrf
);
4364 /* Cleanup the RT to VRF mapping */
4365 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4367 /* Remove auto generated RT */
4368 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4370 /* Add the newly configured RT to RT list */
4371 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4372 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4374 /* map VRF to its RTs */
4375 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4377 /* install routes matching the new VRF */
4378 install_routes_for_vrf(bgp_vrf
);
4381 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4382 struct ecommunity
*ecomdel
)
4384 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4385 struct ecommunity
*ecom
= NULL
;
4387 /* uninstall routes from vrf */
4388 uninstall_routes_for_vrf(bgp_vrf
);
4390 /* Cleanup the RT to VRF mapping */
4391 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4393 /* remove the RT from the RT list */
4394 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4395 if (ecommunity_match(ecom
, ecomdel
)) {
4396 ecommunity_free(&ecom
);
4403 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4405 assert(bgp_vrf
->vrf_import_rtl
);
4406 /* fallback to auto import rt, if this was the last RT */
4407 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4408 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4409 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4412 /* map VRFs to its RTs */
4413 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4415 /* install routes matching this new RT */
4416 install_routes_for_vrf(bgp_vrf
);
4419 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4420 struct ecommunity
*ecomadd
)
4422 /* remove auto-generated RT */
4423 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4425 /* Add the new RT to the RT list */
4426 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4427 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4429 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4432 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4433 struct ecommunity
*ecomdel
)
4435 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4436 struct ecommunity
*ecom
= NULL
;
4438 /* Remove the RT from the RT list */
4439 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4440 if (ecommunity_match(ecom
, ecomdel
)) {
4441 ecommunity_free(&ecom
);
4448 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4451 * Temporary assert to make SA happy.
4452 * The ALL_LIST_ELEMENTS macro above has a NULL check
4453 * which means that SA is going to complain about
4454 * the list_isempty call, which doesn't NULL check.
4455 * So until we get this situation cleaned up, here
4458 assert(bgp_vrf
->vrf_export_rtl
);
4460 /* fall back to auto-generated RT if this was the last RT */
4461 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4462 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4463 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4466 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4470 * Handle change to BGP router id. This is invoked twice by the change
4471 * handler, first before the router id has been changed and then after
4472 * the router id has been changed. The first invocation will result in
4473 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4474 * will result in the routes being re-advertised.
4476 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4480 /* delete and withdraw all the type-5 routes
4481 stored in the global table for this vrf
4483 withdraw_router_id_vrf(bgp
);
4485 /* delete all the VNI routes (type-2/type-3) routes for all the
4488 hash_iterate(bgp
->vnihash
,
4489 (void (*)(struct hash_bucket
*,
4490 void *))withdraw_router_id_vni
,
4494 /* advertise all routes in the vrf as type-5 routes with the new
4497 update_router_id_vrf(bgp
);
4499 /* advertise all the VNI routes (type-2/type-3) routes with the
4502 hash_iterate(bgp
->vnihash
,
4503 (void (*)(struct hash_bucket
*,
4504 void *))update_router_id_vni
,
4510 * Handle change to auto-RT algorithm - update and advertise local routes.
4512 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4514 hash_iterate(bgp
->vnihash
,
4515 (void (*)(struct hash_bucket
*,
4516 void*))update_autort_vni
,
4521 * Handle change to export RT - update and advertise local routes.
4523 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4525 return update_routes_for_vni(bgp
, vpn
);
4528 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4531 delete_withdraw_vrf_routes(bgp_vrf
);
4533 update_advertise_vrf_routes(bgp_vrf
);
4537 * Handle change to RD. This is invoked twice by the change handler,
4538 * first before the RD has been changed and then after the RD has
4539 * been changed. The first invocation will result in local routes
4540 * of this VNI being deleted and withdrawn and the next will result
4541 * in the routes being re-advertised.
4543 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4547 delete_withdraw_vni_routes(bgp
, vpn
);
4549 update_advertise_vni_routes(bgp
, vpn
);
4553 * Install routes for this VNI. Invoked upon change to Import RT.
4555 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4557 return install_routes_for_vni(bgp
, vpn
);
4561 * Uninstall all routes installed for this VNI. Invoked upon change
4564 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4566 return uninstall_routes_for_vni(bgp
, vpn
);
4570 * TODO: Hardcoded for a maximum of 2 VNIs right now
4572 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4577 vni1
= label2vni(label
);
4578 if (num_labels
== 2) {
4579 vni2
= label2vni(label
+ 1);
4580 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4582 snprintf(buf
, len
, "%u", vni1
);
4587 * Function to convert evpn route to json format.
4588 * NOTE: We don't use prefix2str as the output here is a bit different.
4590 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
4592 char buf1
[ETHER_ADDR_STRLEN
];
4593 char buf2
[PREFIX2STR_BUFFER
];
4598 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4599 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4600 json_object_int_add(json
, "ethTag",
4601 p
->prefix
.imet_addr
.eth_tag
);
4602 json_object_int_add(json
, "ipLen",
4603 is_evpn_prefix_ipaddr_v4(p
)
4606 json_object_string_add(json
, "ip",
4607 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4608 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4609 if (is_evpn_prefix_ipaddr_none(p
)) {
4610 json_object_int_add(json
, "routeType",
4611 p
->prefix
.route_type
);
4612 json_object_int_add(json
, "ethTag",
4613 p
->prefix
.macip_addr
.eth_tag
);
4614 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4615 json_object_string_add(json
, "mac",
4616 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4622 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4625 json_object_int_add(json
, "routeType",
4626 p
->prefix
.route_type
);
4627 json_object_int_add(json
, "ethTag",
4628 p
->prefix
.macip_addr
.eth_tag
);
4629 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4630 json_object_string_add(json
, "mac",
4631 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4634 json_object_int_add(json
, "ipLen",
4635 is_evpn_prefix_ipaddr_v4(p
)
4638 json_object_string_add(
4641 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4643 PREFIX2STR_BUFFER
));
4646 /* Currently, this is to cater to other AF_ETHERNET code. */
4651 * Function to convert evpn route to string.
4652 * NOTE: We don't use prefix2str as the output here is a bit different.
4654 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
4656 char buf1
[ETHER_ADDR_STRLEN
];
4657 char buf2
[PREFIX2STR_BUFFER
];
4658 char buf3
[ESI_STR_LEN
];
4660 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4661 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
4662 p
->prefix
.imet_addr
.eth_tag
,
4663 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4665 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4666 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4667 if (is_evpn_prefix_ipaddr_none(p
))
4668 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4669 p
->prefix
.route_type
,
4670 p
->prefix
.macip_addr
.eth_tag
,
4672 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4677 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4679 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
4680 p
->prefix
.route_type
,
4681 p
->prefix
.macip_addr
.eth_tag
,
4683 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4685 family
== AF_INET
? IPV4_MAX_BITLEN
4688 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4690 PREFIX2STR_BUFFER
));
4692 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
4693 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4694 p
->prefix
.route_type
,
4695 p
->prefix
.prefix_addr
.eth_tag
,
4696 p
->prefix
.prefix_addr
.ip_prefix_length
,
4697 is_evpn_prefix_ipaddr_v4(p
)
4698 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
4699 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
4700 } else if (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
4701 snprintf(buf
, len
, "[%d]:[%s]:[%d]:[%s]",
4702 p
->prefix
.route_type
,
4703 esi_to_str(&p
->prefix
.es_addr
.esi
, buf3
, sizeof(buf3
)),
4704 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4706 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
));
4708 /* For EVPN route types not supported yet. */
4709 snprintf(buf
, len
, "(unsupported route type %d)",
4710 p
->prefix
.route_type
);
4717 * Encode EVPN prefix in Update (MP_REACH)
4719 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
4720 struct prefix_rd
*prd
, mpls_label_t
*label
,
4721 uint32_t num_labels
, struct attr
*attr
,
4722 int addpath_encode
, uint32_t addpath_tx_id
)
4724 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4725 int len
, ipa_len
= 0;
4728 stream_putl(s
, addpath_tx_id
);
4731 stream_putc(s
, evp
->prefix
.route_type
);
4733 switch (evp
->prefix
.route_type
) {
4734 case BGP_EVPN_MAC_IP_ROUTE
:
4735 if (is_evpn_prefix_ipaddr_v4(evp
))
4736 ipa_len
= IPV4_MAX_BYTELEN
;
4737 else if (is_evpn_prefix_ipaddr_v6(evp
))
4738 ipa_len
= IPV6_MAX_BYTELEN
;
4739 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4740 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4741 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4743 stream_putc(s
, len
);
4744 stream_put(s
, prd
->val
, 8); /* RD */
4746 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
4748 stream_put(s
, 0, 10);
4749 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4750 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4751 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4752 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4753 if (ipa_len
) /* IP */
4754 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4756 /* 1st label is the L2 VNI */
4757 stream_put(s
, label
, BGP_LABEL_BYTES
);
4758 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4759 if (ipa_len
&& num_labels
> 1)
4760 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4763 case BGP_EVPN_IMET_ROUTE
:
4764 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4765 stream_put(s
, prd
->val
, 8); /* RD */
4766 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4767 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4768 /* Originating Router's IP Addr */
4769 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4772 case BGP_EVPN_ES_ROUTE
:
4773 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4774 stream_put(s
, prd
->val
, 8); /* RD */
4775 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4776 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4778 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4781 case BGP_EVPN_IP_PREFIX_ROUTE
:
4782 /* TODO: AddPath support. */
4783 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4791 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4792 struct bgp_nlri
*packet
, int withdraw
)
4798 uint32_t addpath_id
;
4799 int addpath_encoded
;
4804 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4806 lim
= pnt
+ packet
->length
;
4808 safi
= packet
->safi
;
4812 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
4813 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
4814 PEER_CAP_ADDPATH_AF_TX_RCV
));
4816 for (; pnt
< lim
; pnt
+= psize
) {
4817 /* Clear prefix structure. */
4818 memset(&p
, 0, sizeof(struct prefix
));
4820 /* Deal with path-id if AddPath is supported. */
4821 if (addpath_encoded
) {
4822 /* When packet overflow occurs return immediately. */
4823 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4826 addpath_id
= ntohl(*((uint32_t *)pnt
));
4827 pnt
+= BGP_ADDPATH_ID_LEN
;
4830 /* All EVPN NLRI types start with type and length. */
4837 /* When packet overflow occur return immediately. */
4838 if (pnt
+ psize
> lim
)
4842 case BGP_EVPN_MAC_IP_ROUTE
:
4843 if (process_type2_route(peer
, afi
, safi
,
4844 withdraw
? NULL
: attr
, pnt
,
4845 psize
, addpath_id
)) {
4848 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4849 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4854 case BGP_EVPN_IMET_ROUTE
:
4855 if (process_type3_route(peer
, afi
, safi
,
4856 withdraw
? NULL
: attr
, pnt
,
4857 psize
, addpath_id
)) {
4860 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4861 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4866 case BGP_EVPN_ES_ROUTE
:
4867 if (process_type4_route(peer
, afi
, safi
,
4868 withdraw
? NULL
: attr
, pnt
,
4869 psize
, addpath_id
)) {
4872 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4873 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4878 case BGP_EVPN_IP_PREFIX_ROUTE
:
4879 if (process_type5_route(peer
, afi
, safi
,
4880 withdraw
? NULL
: attr
, pnt
,
4881 psize
, addpath_id
)) {
4884 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4885 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4895 /* Packet length consistency check. */
4903 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
4904 * The mapping will be used during route processing.
4905 * bgp_def: default bgp instance
4906 * bgp_vrf: specific bgp vrf instance on which RT is configured
4908 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
4911 struct ecommunity_val
*eval
= NULL
;
4912 struct listnode
*node
= NULL
, *nnode
= NULL
;
4913 struct ecommunity
*ecom
= NULL
;
4915 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4916 for (i
= 0; i
< ecom
->size
; i
++) {
4917 eval
= (struct ecommunity_val
*)(ecom
->val
4919 * ECOMMUNITY_SIZE
));
4920 map_vrf_to_rt(bgp_vrf
, eval
);
4926 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
4928 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
4931 struct ecommunity_val
*eval
;
4932 struct listnode
*node
, *nnode
;
4933 struct ecommunity
*ecom
;
4935 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4936 for (i
= 0; i
< ecom
->size
; i
++) {
4937 struct vrf_irt_node
*irt
;
4938 struct ecommunity_val eval_tmp
;
4940 eval
= (struct ecommunity_val
*)(ecom
->val
4942 * ECOMMUNITY_SIZE
));
4943 /* If using "automatic" RT, we only care about the
4944 * local-admin sub-field.
4945 * This is to facilitate using VNI as the RT for EBGP
4948 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4949 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
4950 BGP_VRF_IMPORT_RT_CFGD
))
4951 mask_ecom_global_admin(&eval_tmp
, eval
);
4953 irt
= lookup_vrf_import_rt(&eval_tmp
);
4955 unmap_vrf_from_rt(bgp_vrf
, irt
);
4962 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
4963 * The mapping will be used during route processing.
4965 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4968 struct ecommunity_val
*eval
;
4969 struct listnode
*node
, *nnode
;
4970 struct ecommunity
*ecom
;
4972 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4973 for (i
= 0; i
< ecom
->size
; i
++) {
4974 eval
= (struct ecommunity_val
*)(ecom
->val
4976 * ECOMMUNITY_SIZE
));
4977 map_vni_to_rt(bgp
, vpn
, eval
);
4983 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
4985 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4988 struct ecommunity_val
*eval
;
4989 struct listnode
*node
, *nnode
;
4990 struct ecommunity
*ecom
;
4992 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4993 for (i
= 0; i
< ecom
->size
; i
++) {
4994 struct irt_node
*irt
;
4995 struct ecommunity_val eval_tmp
;
4997 eval
= (struct ecommunity_val
*)(ecom
->val
4999 * ECOMMUNITY_SIZE
));
5000 /* If using "automatic" RT, we only care about the
5001 * local-admin sub-field.
5002 * This is to facilitate using VNI as the RT for EBGP
5005 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5006 if (!is_import_rt_configured(vpn
))
5007 mask_ecom_global_admin(&eval_tmp
, eval
);
5009 irt
= lookup_import_rt(bgp
, &eval_tmp
);
5011 unmap_vni_from_rt(bgp
, vpn
, irt
);
5017 * Derive Import RT automatically for VNI and map VNI to RT.
5018 * The mapping will be used during route processing.
5020 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5022 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
5023 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
5026 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
5030 * Derive Export RT automatically for VNI.
5032 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5034 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
5035 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5039 * Derive RD automatically for VNI using passed information - it
5040 * is of the form RouterId:unique-id-for-vni.
5042 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5044 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5048 * Derive RD automatically for VNI using passed information - it
5049 * is of the form RouterId:unique-id-for-vni.
5051 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5055 vpn
->prd
.family
= AF_UNSPEC
;
5056 vpn
->prd
.prefixlen
= 64;
5057 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
5058 (void)str2prefix_rd(buf
, &vpn
->prd
);
5059 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5065 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5067 struct list
*inst
= bm
->bgp
;
5068 struct listnode
*node
;
5069 struct bgp
*bgp_vrf
;
5071 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5072 if (bgp_vrf
->l3vni
== vni
)
5082 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5084 struct bgpevpn
*vpn
;
5087 memset(&tmp
, 0, sizeof(struct bgpevpn
));
5089 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5094 * Create a new vpn - invoked upon configuration or zebra notification.
5096 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5097 struct in_addr originator_ip
,
5098 vrf_id_t tenant_vrf_id
)
5100 struct bgpevpn
*vpn
;
5105 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5107 /* Set values - RD and RT set to defaults. */
5109 vpn
->originator_ip
= originator_ip
;
5110 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5112 /* Initialize route-target import and export lists */
5113 vpn
->import_rtl
= list_new();
5114 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5115 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
5116 vpn
->export_rtl
= list_new();
5117 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5118 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
5119 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5120 derive_rd_rt_for_vni(bgp
, vpn
);
5122 /* Initialize EVPN route table. */
5123 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5126 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
5127 XFREE(MTYPE_BGP_EVPN
, vpn
);
5131 /* add to l2vni list on corresponding vrf */
5132 bgpevpn_link_to_l3vni(vpn
);
5134 QOBJ_REG(vpn
, bgpevpn
);
5139 * Free a given VPN - called in multiple scenarios such as zebra
5140 * notification, configuration being deleted, advertise-all-vni disabled etc.
5141 * This just frees appropriate memory, caller should have taken other
5144 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5146 bgpevpn_unlink_from_l3vni(vpn
);
5147 bgp_table_unlock(vpn
->route_table
);
5148 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5149 list_delete(&vpn
->import_rtl
);
5150 list_delete(&vpn
->export_rtl
);
5151 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5152 hash_release(bgp
->vnihash
, vpn
);
5154 XFREE(MTYPE_BGP_EVPN
, vpn
);
5160 struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
)
5165 memset(&tmp
, 0, sizeof(struct evpnes
));
5166 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
5167 es
= hash_lookup(bgp
->esihash
, &tmp
);
5172 * Create a new local es - invoked upon zebra notification.
5174 struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
,
5176 struct ipaddr
*originator_ip
)
5184 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct evpnes
));
5186 /* set the ESI and originator_ip */
5187 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
5188 memcpy(&es
->originator_ip
, originator_ip
, sizeof(struct ipaddr
));
5190 /* Initialise the VTEP list */
5191 es
->vtep_list
= list_new();
5192 es
->vtep_list
->cmp
= evpn_vtep_ip_cmp
;
5194 /* auto derive RD for this es */
5195 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
5196 es
->prd
.family
= AF_UNSPEC
;
5197 es
->prd
.prefixlen
= 64;
5198 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), es
->rd_id
);
5199 (void)str2prefix_rd(buf
, &es
->prd
);
5201 /* Initialize the ES route table */
5202 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5205 if (!hash_get(bgp
->esihash
, es
, hash_alloc_intern
)) {
5206 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5210 QOBJ_REG(es
, evpnes
);
5216 * This just frees appropriate memory, caller should have taken other
5219 void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
)
5221 list_delete(&es
->vtep_list
);
5222 bgp_table_unlock(es
->route_table
);
5223 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
5224 hash_release(bgp
->esihash
, es
);
5226 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5230 * Import evpn route from global table to VNI/VRF/ESI.
5232 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5233 struct prefix
*p
, struct bgp_path_info
*pi
)
5235 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5239 * Unimport evpn route from VNI/VRF/ESI.
5241 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5242 struct prefix
*p
, struct bgp_path_info
*pi
)
5244 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5247 /* filter routes which have martian next hops */
5248 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5252 struct bgp_node
*rd_rn
, *rn
;
5253 struct bgp_table
*table
;
5254 struct bgp_path_info
*pi
;
5259 /* Walk entire global routing table and evaluate routes which could be
5260 * imported into this VPN. Note that we cannot just look at the routes
5261 * for the VNI's RD -
5262 * remote routes applicable for this VNI could have any RD.
5264 /* EVPN routes are a 2-level table. */
5265 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
5266 rd_rn
= bgp_route_next(rd_rn
)) {
5267 table
= bgp_node_get_bgp_table_info(rd_rn
);
5271 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5273 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
5276 /* Consider "valid" remote routes applicable for
5278 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5279 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5282 if (bgp_nexthop_self(bgp
, pi
->attr
->nexthop
)) {
5284 char attr_str
[BUFSIZ
] = {0};
5285 char pbuf
[PREFIX_STRLEN
];
5287 bgp_dump_attr(pi
->attr
, attr_str
,
5290 if (bgp_debug_update(pi
->peer
, &rn
->p
,
5293 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
5300 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5303 bgp_rib_remove(rn
, pi
, pi
->peer
, afi
,
5314 * Handle del of a local MACIP.
5316 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5317 struct ipaddr
*ip
, int state
)
5319 struct bgpevpn
*vpn
;
5320 struct prefix_evpn p
;
5321 struct bgp_node
*rn
;
5323 /* Lookup VNI hash - should exist. */
5324 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5325 if (!vpn
|| !is_vni_live(vpn
)) {
5326 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5327 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5328 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5332 build_evpn_type2_prefix(&p
, mac
, ip
);
5333 if (state
== ZEBRA_NEIGH_ACTIVE
) {
5334 /* Remove EVPN type-2 route and schedule for processing. */
5335 delete_evpn_route(bgp
, vpn
, &p
);
5337 /* Re-instate the current remote best path if any */
5338 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
5340 evpn_zebra_reinstall_best_route(bgp
, vpn
, rn
);
5347 * Handle add of a local MACIP.
5349 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5350 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
)
5352 struct bgpevpn
*vpn
;
5353 struct prefix_evpn p
;
5355 /* Lookup VNI hash - should exist. */
5356 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5357 if (!vpn
|| !is_vni_live(vpn
)) {
5358 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5359 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5360 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5364 /* Create EVPN type-2 route and schedule for processing. */
5365 build_evpn_type2_prefix(&p
, mac
, ip
);
5366 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
)) {
5367 char buf
[ETHER_ADDR_STRLEN
];
5368 char buf2
[INET6_ADDRSTRLEN
];
5371 EC_BGP_EVPN_ROUTE_CREATE
,
5372 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5373 bgp
->vrf_id
, vpn
->vni
,
5374 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5377 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5378 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5385 static void link_l2vni_hash_to_l3vni(struct hash_bucket
*bucket
,
5386 struct bgp
*bgp_vrf
)
5388 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
5389 struct bgp
*bgp_def
= NULL
;
5391 bgp_def
= bgp_get_default();
5394 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5395 bgpevpn_link_to_l3vni(vpn
);
5398 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
, struct ethaddr
*rmac
,
5399 struct in_addr originator_ip
, int filter
,
5400 ifindex_t svi_ifindex
)
5402 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5403 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5404 struct listnode
*node
= NULL
;
5405 struct bgpevpn
*vpn
= NULL
;
5408 /* get the default instance - required to get the AS number for VRF
5411 bgp_def
= bgp_get_default();
5415 "Cannot process L3VNI %u ADD - default BGP instance not yet created",
5421 /* if the BGP vrf instance doesn't exist - create one */
5422 bgp_vrf
= bgp_lookup_by_name(vrf_id_to_name(vrf_id
));
5427 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5428 BGP_INSTANCE_TYPE_VRF
);
5430 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
:
5431 flog_err(EC_BGP_MULTI_INSTANCE
,
5432 "'bgp multiple-instance' not present\n");
5434 case BGP_ERR_AS_MISMATCH
:
5435 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5436 "BGP is already running; AS is %u\n", as
);
5438 case BGP_ERR_INSTANCE_MISMATCH
:
5439 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5440 "BGP instance name and AS number mismatch\n");
5444 /* mark as auto created */
5445 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5448 /* associate the vrf with l3vni and related parameters */
5449 bgp_vrf
->l3vni
= l3vni
;
5450 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
5451 bgp_vrf
->originator_ip
= originator_ip
;
5452 bgp_vrf
->l3vni_svi_ifindex
= svi_ifindex
;
5454 /* set the right filter - are we using l3vni only for prefix routes? */
5456 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5458 /* Map auto derive or configured RTs */
5459 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5460 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5462 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
5464 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5465 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5467 /* auto derive RD */
5468 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5470 /* link all corresponding l2vnis */
5471 hash_iterate(bgp_def
->vnihash
,
5472 (void (*)(struct hash_bucket
*,
5473 void *))link_l2vni_hash_to_l3vni
,
5476 /* Only update all corresponding type-2 routes if we are advertising two
5477 * labels along with type-2 routes
5480 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5481 update_routes_for_vni(bgp_def
, vpn
);
5483 /* advertise type-5 routes if needed */
5484 update_advertise_vrf_routes(bgp_vrf
);
5486 /* install all remote routes belonging to this l3vni into correspondng
5488 install_routes_for_vrf(bgp_vrf
);
5493 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5495 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5496 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5497 struct listnode
*node
= NULL
;
5498 struct listnode
*next
= NULL
;
5499 struct bgpevpn
*vpn
= NULL
;
5501 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5505 "Cannot process L3VNI %u Del - Could not find BGP instance",
5510 bgp_def
= bgp_get_default();
5514 "Cannot process L3VNI %u Del - Could not find default BGP instance",
5519 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5520 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5521 * routes. This will uninstalling the routes from zebra and decremnt the
5524 uninstall_routes_for_vrf(bgp_vrf
);
5526 /* delete/withdraw all type-5 routes */
5527 delete_withdraw_vrf_routes(bgp_vrf
);
5529 /* remove the l3vni from vrf instance */
5532 /* remove the Rmac from the BGP vrf */
5533 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5535 /* remove default import RT or Unmap non-default import RT */
5536 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5537 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5538 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5539 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5542 /* remove default export RT */
5543 if (!list_isempty(bgp_vrf
->vrf_export_rtl
) &&
5544 !CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
5545 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5548 /* update all corresponding local mac-ip routes */
5549 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5550 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5551 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5552 update_routes_for_vni(bgp_def
, vpn
);
5556 /* If any L2VNIs point to this instance, unlink them. */
5557 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5558 bgpevpn_unlink_from_l3vni(vpn
);
5560 /* Delete the instance if it was autocreated */
5561 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5562 bgp_delete(bgp_vrf
);
5568 * Handle del of a local VNI.
5570 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5572 struct bgpevpn
*vpn
;
5574 /* Locate VNI hash */
5575 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5577 if (bgp_debug_zebra(NULL
))
5579 EC_BGP_EVPN_VPN_VNI
,
5580 "%u: VNI hash entry for VNI %u not found at DEL",
5585 /* Remove all local EVPN routes and schedule for processing (to
5586 * withdraw from peers).
5588 delete_routes_for_vni(bgp
, vpn
);
5591 * tunnel is no longer active, del tunnel ip address from tip_hash
5593 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5595 /* Clear "live" flag and see if hash needs to be freed. */
5596 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5597 if (!is_vni_configured(vpn
))
5598 bgp_evpn_free(bgp
, vpn
);
5604 * Handle add (or update) of a local VNI. The VNI changes we care
5605 * about are for the local-tunnel-ip and the (tenant) VRF.
5607 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5608 struct in_addr originator_ip
, vrf_id_t tenant_vrf_id
)
5610 struct bgpevpn
*vpn
;
5611 struct prefix_evpn p
;
5613 /* Lookup VNI. If present and no change, exit. */
5614 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5617 if (is_vni_live(vpn
)
5618 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5619 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5620 /* Probably some other param has changed that we don't
5624 /* Update tenant_vrf_id if it has changed. */
5625 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5626 bgpevpn_unlink_from_l3vni(vpn
);
5627 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5628 bgpevpn_link_to_l3vni(vpn
);
5631 /* If tunnel endpoint IP has changed, update (and delete prior
5632 * type-3 route, if needed.)
5634 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5635 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5637 /* Update all routes with new endpoint IP and/or export RT
5640 if (is_vni_live(vpn
))
5641 update_routes_for_vni(bgp
, vpn
);
5644 /* Create or update as appropriate. */
5646 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
);
5650 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5656 /* if the VNI is live already, there is nothing more to do */
5657 if (is_vni_live(vpn
))
5660 /* Mark as "live" */
5661 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5663 /* tunnel is now active, add tunnel-ip to db */
5664 bgp_tip_add(bgp
, &originator_ip
);
5666 /* filter routes as nexthop database has changed */
5667 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5670 * Create EVPN type-3 route and schedule for processing.
5672 * RT-3 only if doing head-end replication
5674 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
5675 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5676 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0)) {
5677 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5678 "%u: Type3 route creation failure for VNI %u",
5684 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5688 install_routes_for_vni(bgp
, vpn
);
5690 /* If we are advertising gateway mac-ip
5691 It needs to be conveyed again to zebra */
5692 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5698 * bgp_evpn_local_es_del
5700 int bgp_evpn_local_es_del(struct bgp
*bgp
,
5702 struct ipaddr
*originator_ip
)
5704 char buf
[ESI_STR_LEN
];
5705 struct evpnes
*es
= NULL
;
5707 if (!bgp
->esihash
) {
5708 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5713 /* Lookup ESI hash - should exist. */
5714 es
= bgp_evpn_lookup_es(bgp
, esi
);
5716 flog_warn(EC_BGP_EVPN_ESI
,
5717 "%u: ESI hash entry for ESI %s at Local ES DEL",
5718 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5722 /* Delete all local EVPN ES routes from ESI table
5723 * and schedule for processing (to withdraw from peers))
5725 delete_routes_for_es(bgp
, es
);
5727 /* free the hash entry */
5728 bgp_evpn_es_free(bgp
, es
);
5734 * bgp_evpn_local_es_add
5736 int bgp_evpn_local_es_add(struct bgp
*bgp
,
5738 struct ipaddr
*originator_ip
)
5740 char buf
[ESI_STR_LEN
];
5741 struct evpnes
*es
= NULL
;
5742 struct prefix_evpn p
;
5744 if (!bgp
->esihash
) {
5745 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5750 /* create the new es */
5751 es
= bgp_evpn_lookup_es(bgp
, esi
);
5753 es
= bgp_evpn_es_new(bgp
, esi
, originator_ip
);
5757 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
5758 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5762 UNSET_FLAG(es
->flags
, EVPNES_REMOTE
);
5763 SET_FLAG(es
->flags
, EVPNES_LOCAL
);
5765 build_evpn_type4_prefix(&p
, esi
, originator_ip
->ipaddr_v4
);
5766 if (update_evpn_type4_route(bgp
, es
, &p
)) {
5767 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5768 "%u: Type4 route creation failure for ESI %s",
5769 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5773 /* import all remote ES routes in th ES table */
5774 install_routes_for_es(bgp
, es
);
5780 * Handle change in setting for BUM handling. The supported values
5781 * are head-end replication and dropping all BUM packets. Any change
5782 * should be registered with zebra. Also, if doing head-end replication,
5783 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5784 * to be dropped, the RT-3s must be withdrawn.
5786 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
5788 zlog_info("L2VPN EVPN BUM handling is %s",
5789 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
5790 "Flooding" : "Flooding Disabled");
5792 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
5793 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
5794 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
5795 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
5796 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
5800 * Cleanup EVPN information on disable - Need to delete and withdraw
5801 * EVPN routes from peers.
5803 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5805 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_bucket
*,
5806 void *))cleanup_vni_on_disable
,
5811 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5812 * BGP instance (default) is being freed.
5814 void bgp_evpn_cleanup(struct bgp
*bgp
)
5816 hash_iterate(bgp
->vnihash
,
5817 (void (*)(struct hash_bucket
*, void *))free_vni_entry
,
5820 hash_free(bgp
->import_rt_hash
);
5821 bgp
->import_rt_hash
= NULL
;
5823 hash_free(bgp
->vrf_import_rt_hash
);
5824 bgp
->vrf_import_rt_hash
= NULL
;
5826 hash_free(bgp
->vnihash
);
5827 bgp
->vnihash
= NULL
;
5829 hash_free(bgp
->esihash
);
5830 bgp
->esihash
= NULL
;
5832 list_delete(&bgp
->vrf_import_rtl
);
5833 list_delete(&bgp
->vrf_export_rtl
);
5834 list_delete(&bgp
->l2vnis
);
5838 * Initialization for EVPN
5841 * hash for RT to VNI
5843 void bgp_evpn_init(struct bgp
*bgp
)
5846 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
5848 hash_create(esi_hash_keymake
, esi_cmp
,
5849 "BGP EVPN Local ESI Hash");
5850 bgp
->import_rt_hash
=
5851 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
5852 "BGP Import RT Hash");
5853 bgp
->vrf_import_rt_hash
=
5854 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
5855 "BGP VRF Import RT Hash");
5856 bgp
->vrf_import_rtl
= list_new();
5857 bgp
->vrf_import_rtl
->cmp
=
5858 (int (*)(void *, void *))evpn_route_target_cmp
;
5859 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
5860 bgp
->vrf_export_rtl
= list_new();
5861 bgp
->vrf_export_rtl
->cmp
=
5862 (int (*)(void *, void *))evpn_route_target_cmp
;
5863 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
5864 bgp
->l2vnis
= list_new();
5865 bgp
->l2vnis
->cmp
= vni_list_cmp
;
5866 /* By default Duplicate Address Dection is enabled.
5867 * Max-moves (N) 5, detection time (M) 180
5868 * default action is warning-only
5869 * freeze action permanently freezes address,
5870 * and freeze time (auto-recovery) is disabled.
5872 if (bgp
->evpn_info
) {
5873 bgp
->evpn_info
->dup_addr_detect
= true;
5874 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
5875 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
5876 bgp
->evpn_info
->dad_freeze
= false;
5877 bgp
->evpn_info
->dad_freeze_time
= 0;
5878 /* Initialize zebra vxlan */
5879 bgp_zebra_dup_addr_detection(bgp
);
5882 /* Default BUM handling is to do head-end replication. */
5883 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
5886 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
5888 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);