1 /* Ethernet-VPN Packet and vty Processing File
2 * Copyright (C) 2016 6WIND
3 * Copyright (C) 2017 Cumulus Networks, Inc.
5 * This file is part of FRR.
7 * FRRouting is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * FRRouting is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "bgpd/bgp_attr_evpn.h"
35 #include "bgpd/bgpd.h"
36 #include "bgpd/bgp_table.h"
37 #include "bgpd/bgp_route.h"
38 #include "bgpd/bgp_attr.h"
39 #include "bgpd/bgp_mplsvpn.h"
40 #include "bgpd/bgp_label.h"
41 #include "bgpd/bgp_evpn.h"
42 #include "bgpd/bgp_evpn_private.h"
43 #include "bgpd/bgp_ecommunity.h"
44 #include "bgpd/bgp_encap_types.h"
45 #include "bgpd/bgp_debug.h"
46 #include "bgpd/bgp_errors.h"
47 #include "bgpd/bgp_aspath.h"
48 #include "bgpd/bgp_zebra.h"
49 #include "bgpd/bgp_nexthop.h"
50 #include "bgpd/bgp_addpath.h"
51 #include "bgpd/bgp_mac.h"
54 * Definitions and external declarations.
56 extern struct zclient
*zclient
;
58 DEFINE_QOBJ_TYPE(bgpevpn
)
59 DEFINE_QOBJ_TYPE(evpnes
)
63 * Static function declarations
65 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
67 struct bgp_path_info
**pi
);
68 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
74 /* compare two IPV4 VTEP IPs */
75 static int evpn_vtep_ip_cmp(void *p1
, void *p2
)
77 const struct in_addr
*ip1
= p1
;
78 const struct in_addr
*ip2
= p2
;
80 return ip1
->s_addr
- ip2
->s_addr
;
84 * Make hash key for ESI.
86 static unsigned int esi_hash_keymake(const void *p
)
88 const struct evpnes
*pes
= p
;
89 const void *pnt
= (void *)pes
->esi
.val
;
91 return jhash(pnt
, ESI_BYTES
, 0xa5a5a55a);
97 static bool esi_cmp(const void *p1
, const void *p2
)
99 const struct evpnes
*pes1
= p1
;
100 const struct evpnes
*pes2
= p2
;
102 if (pes1
== NULL
&& pes2
== NULL
)
105 if (pes1
== NULL
|| pes2
== NULL
)
108 return (memcmp(pes1
->esi
.val
, pes2
->esi
.val
, ESI_BYTES
) == 0);
114 static unsigned int vni_hash_key_make(const void *p
)
116 const struct bgpevpn
*vpn
= p
;
117 return (jhash_1word(vpn
->vni
, 0));
121 * Comparison function for vni hash
123 static bool vni_hash_cmp(const void *p1
, const void *p2
)
125 const struct bgpevpn
*vpn1
= p1
;
126 const struct bgpevpn
*vpn2
= p2
;
132 return (vpn1
->vni
== vpn2
->vni
);
135 static int vni_list_cmp(void *p1
, void *p2
)
137 const struct bgpevpn
*vpn1
= p1
;
138 const struct bgpevpn
*vpn2
= p2
;
140 return vpn1
->vni
- vpn2
->vni
;
144 * Make vrf import route target hash key.
146 static unsigned int vrf_import_rt_hash_key_make(const void *p
)
148 const struct vrf_irt_node
*irt
= p
;
149 const char *pnt
= irt
->rt
.val
;
151 return jhash(pnt
, 8, 0x5abc1234);
155 * Comparison function for vrf import rt hash
157 static bool vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
159 const struct vrf_irt_node
*irt1
= p1
;
160 const struct vrf_irt_node
*irt2
= p2
;
162 if (irt1
== NULL
&& irt2
== NULL
)
165 if (irt1
== NULL
|| irt2
== NULL
)
168 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
172 * Create a new vrf import_rt in evpn instance
174 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
176 struct bgp
*bgp_evpn
= NULL
;
177 struct vrf_irt_node
*irt
;
179 bgp_evpn
= bgp_get_evpn();
181 flog_err(EC_BGP_NO_DFLT
,
182 "vrf import rt new - evpn instance not created yet");
186 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
187 sizeof(struct vrf_irt_node
));
190 irt
->vrfs
= list_new();
193 if (!hash_get(bgp_evpn
->vrf_import_rt_hash
, irt
, hash_alloc_intern
)) {
194 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
202 * Free the vrf import rt node
204 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
206 struct bgp
*bgp_evpn
= NULL
;
208 bgp_evpn
= bgp_get_evpn();
210 flog_err(EC_BGP_NO_DFLT
,
211 "vrf import rt free - evpn instance not created yet");
215 hash_release(bgp_evpn
->vrf_import_rt_hash
, irt
);
216 list_delete(&irt
->vrfs
);
217 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
221 * Function to lookup Import RT node - used to map a RT to set of
222 * VNIs importing routes with that RT.
224 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
226 struct bgp
*bgp_evpn
= NULL
;
227 struct vrf_irt_node
*irt
;
228 struct vrf_irt_node tmp
;
230 bgp_evpn
= bgp_get_evpn();
234 "vrf import rt lookup - evpn instance not created yet");
238 memset(&tmp
, 0, sizeof(struct vrf_irt_node
));
239 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
240 irt
= hash_lookup(bgp_evpn
->vrf_import_rt_hash
, &tmp
);
245 * Is specified VRF present on the RT's list of "importing" VRFs?
247 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
, struct bgp
*bgp_vrf
)
249 struct listnode
*node
= NULL
, *nnode
= NULL
;
250 struct bgp
*tmp_bgp_vrf
= NULL
;
252 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
253 if (tmp_bgp_vrf
== bgp_vrf
)
260 * Make import route target hash key.
262 static unsigned int import_rt_hash_key_make(const void *p
)
264 const struct irt_node
*irt
= p
;
265 const char *pnt
= irt
->rt
.val
;
267 return jhash(pnt
, 8, 0xdeadbeef);
271 * Comparison function for import rt hash
273 static bool import_rt_hash_cmp(const void *p1
, const void *p2
)
275 const struct irt_node
*irt1
= p1
;
276 const struct irt_node
*irt2
= p2
;
278 if (irt1
== NULL
&& irt2
== NULL
)
281 if (irt1
== NULL
|| irt2
== NULL
)
284 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
288 * Create a new import_rt
290 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
291 struct ecommunity_val
*rt
)
293 struct irt_node
*irt
;
298 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
301 irt
->vnis
= list_new();
304 if (!hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
)) {
305 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
313 * Free the import rt node
315 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
317 hash_release(bgp
->import_rt_hash
, irt
);
318 list_delete(&irt
->vnis
);
319 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
323 * Function to lookup Import RT node - used to map a RT to set of
324 * VNIs importing routes with that RT.
326 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
327 struct ecommunity_val
*rt
)
329 struct irt_node
*irt
;
332 memset(&tmp
, 0, sizeof(struct irt_node
));
333 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
334 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
339 * Is specified VNI present on the RT's list of "importing" VNIs?
341 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
343 struct listnode
*node
, *nnode
;
344 struct bgpevpn
*tmp_vpn
;
346 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
355 * Compare Route Targets.
357 static int evpn_route_target_cmp(struct ecommunity
*ecom1
,
358 struct ecommunity
*ecom2
)
366 if (!ecom1
&& !ecom2
)
369 if (ecom1
->str
&& !ecom2
->str
)
372 if (!ecom1
->str
&& ecom2
->str
)
375 if (!ecom1
->str
&& !ecom2
->str
)
378 return strcmp(ecom1
->str
, ecom2
->str
);
381 static void evpn_xxport_delete_ecomm(void *val
)
383 struct ecommunity
*ecomm
= val
;
384 ecommunity_free(&ecomm
);
388 * Mask off global-admin field of specified extended community (RT),
389 * just retain the local-admin field.
391 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
392 struct ecommunity_val
*src
)
398 if (type
== ECOMMUNITY_ENCODE_AS
) {
399 dst
->val
[2] = dst
->val
[3] = 0;
400 } else if (type
== ECOMMUNITY_ENCODE_AS4
401 || type
== ECOMMUNITY_ENCODE_IP
) {
402 dst
->val
[2] = dst
->val
[3] = 0;
403 dst
->val
[4] = dst
->val
[5] = 0;
408 * Map one RT to specified VRF.
409 * bgp_vrf = BGP vrf instance
411 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct ecommunity_val
*eval
)
413 struct vrf_irt_node
*irt
= NULL
;
414 struct ecommunity_val eval_tmp
;
416 /* If using "automatic" RT,
417 * we only care about the local-admin sub-field.
418 * This is to facilitate using L3VNI(VRF-VNI)
419 * as the RT for EBGP peering too.
421 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
422 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
423 mask_ecom_global_admin(&eval_tmp
, eval
);
425 irt
= lookup_vrf_import_rt(&eval_tmp
);
426 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
427 /* Already mapped. */
431 irt
= vrf_import_rt_new(&eval_tmp
);
433 /* Add VRF to the list for this RT. */
434 listnode_add(irt
->vrfs
, bgp_vrf
);
438 * Unmap specified VRF from specified RT. If there are no other
439 * VRFs for this RT, then the RT hash is deleted.
440 * bgp_vrf: BGP VRF specific instance
442 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
, struct vrf_irt_node
*irt
)
444 /* Delete VRF from list for this RT. */
445 listnode_delete(irt
->vrfs
, bgp_vrf
);
446 if (!listnode_head(irt
->vrfs
)) {
447 vrf_import_rt_free(irt
);
452 * Map one RT to specified VNI.
454 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
455 struct ecommunity_val
*eval
)
457 struct irt_node
*irt
;
458 struct ecommunity_val eval_tmp
;
460 /* If using "automatic" RT, we only care about the local-admin
462 * This is to facilitate using VNI as the RT for EBGP peering too.
464 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
465 if (!is_import_rt_configured(vpn
))
466 mask_ecom_global_admin(&eval_tmp
, eval
);
468 irt
= lookup_import_rt(bgp
, &eval_tmp
);
470 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
471 /* Already mapped. */
475 irt
= import_rt_new(bgp
, &eval_tmp
);
479 /* Add VNI to the hash list for this RT. */
480 listnode_add(irt
->vnis
, vpn
);
484 * Unmap specified VNI from specified RT. If there are no other
485 * VNIs for this RT, then the RT hash is deleted.
487 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
488 struct irt_node
*irt
)
490 /* Delete VNI from hash list for this RT. */
491 listnode_delete(irt
->vnis
, vpn
);
492 if (!listnode_head(irt
->vnis
)) {
493 import_rt_free(bgp
, irt
);
497 static void bgp_evpn_get_rmac_nexthop(struct bgpevpn
*vpn
,
498 struct prefix_evpn
*p
,
499 struct attr
*attr
, uint8_t flags
)
501 struct bgp
*bgp_vrf
= vpn
->bgp_vrf
;
503 memset(&attr
->rmac
, 0, sizeof(struct ethaddr
));
507 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
508 /* Copy sys (pip) RMAC and PIP IP as nexthop
509 * in case of route is self MAC-IP,
510 * advertise-pip and advertise-svi-ip features
512 * Otherwise, for all host MAC-IP route's
515 if (CHECK_FLAG(flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
)
516 && bgp_evpn_is_svi_macip_enabled(vpn
)) {
518 memcpy(&attr
->rmac
, &bgp_vrf
->evpn_info
->pip_rmac
,
520 if (bgp_vrf
->evpn_info
->advertise_pip
&&
521 bgp_vrf
->evpn_info
->is_anycast_mac
) {
522 attr
->nexthop
= bgp_vrf
->evpn_info
->pip_ip
;
523 attr
->mp_nexthop_global_in
=
524 bgp_vrf
->evpn_info
->pip_ip
;
527 memcpy(&attr
->rmac
, &bgp_vrf
->rmac
, ETH_ALEN
);
531 * Create RT extended community automatically from passed information:
532 * of the form AS:VNI.
533 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
534 * the need is to get a RT value that will be unique across different
535 * VNIs but the same across routers (in the same AS) for a particular
538 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
540 struct ecommunity_val eval
;
541 struct ecommunity
*ecomadd
, *ecom
;
542 bool ecom_found
= false;
543 struct listnode
*node
;
545 if (bgp
->advertise_autort_rfc8365
)
546 vni
|= EVPN_AUTORT_VXLAN
;
547 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
549 ecomadd
= ecommunity_new();
550 ecommunity_add_val(ecomadd
, &eval
);
551 for (ALL_LIST_ELEMENTS_RO(rtl
, node
, ecom
))
552 if (ecommunity_cmp(ecomadd
, ecom
))
556 listnode_add_sort(rtl
, ecomadd
);
560 * Derive RD and RT for a VNI automatically. Invoked at the time of
563 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
565 bgp_evpn_derive_auto_rd(bgp
, vpn
);
566 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
567 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
571 * Convert nexthop (remote VTEP IP) into an IPv6 address.
573 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
575 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
577 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
578 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
582 * Add (update) or delete MACIP from zebra.
584 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
585 struct prefix_evpn
*p
,
586 struct in_addr remote_vtep_ip
, int add
,
587 uint8_t flags
, uint32_t seq
)
591 char buf1
[ETHER_ADDR_STRLEN
];
592 char buf2
[INET6_ADDRSTRLEN
];
593 char buf3
[INET6_ADDRSTRLEN
];
596 if (!zclient
|| zclient
->sock
< 0)
599 /* Don't try to register if Zebra doesn't know of this instance. */
600 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
601 if (BGP_DEBUG(zebra
, ZEBRA
))
602 zlog_debug("%s: No zebra instance to talk to, not installing remote macip",
603 __PRETTY_FUNCTION__
);
609 zclient_create_header(
610 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
612 stream_putl(s
, vpn
->vni
);
613 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
614 /* IP address length and IP address, if any. */
615 if (is_evpn_prefix_ipaddr_none(p
))
618 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
620 stream_putl(s
, ipa_len
);
621 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
623 stream_put_in_addr(s
, &remote_vtep_ip
);
625 /* TX flags - MAC sticky status and/or gateway mac */
626 /* Also TX the sequence number of the best route. */
628 stream_putc(s
, flags
);
632 stream_putw_at(s
, 0, stream_get_endp(s
));
634 if (bgp_debug_zebra(NULL
))
636 "Tx %s MACIP, VNI %u MAC %s IP %s flags 0x%x seq %u remote VTEP %s",
637 add
? "ADD" : "DEL", vpn
->vni
,
638 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
640 ipaddr2str(&p
->prefix
.macip_addr
.ip
,
641 buf3
, sizeof(buf3
)), flags
, seq
,
642 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
645 return zclient_send_message(zclient
);
649 * Add (update) or delete remote VTEP from zebra.
651 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
652 struct prefix_evpn
*p
,
653 int flood_control
, int add
)
658 if (!zclient
|| zclient
->sock
< 0)
661 /* Don't try to register if Zebra doesn't know of this instance. */
662 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
663 if (BGP_DEBUG(zebra
, ZEBRA
))
664 zlog_debug("%s: No zebra instance to talk to, not installing remote vtep",
665 __PRETTY_FUNCTION__
);
672 zclient_create_header(
673 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
675 stream_putl(s
, vpn
->vni
);
676 if (is_evpn_prefix_ipaddr_v4(p
))
677 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
678 else if (is_evpn_prefix_ipaddr_v6(p
)) {
681 "Bad remote IP when trying to %s remote VTEP for VNI %u",
682 add
? "ADD" : "DEL", vpn
->vni
);
685 stream_putl(s
, flood_control
);
687 stream_putw_at(s
, 0, stream_get_endp(s
));
689 if (bgp_debug_zebra(NULL
))
690 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
691 add
? "ADD" : "DEL", vpn
->vni
,
692 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
694 return zclient_send_message(zclient
);
698 * Build extended community for EVPN ES (type-4) route
700 static void build_evpn_type4_route_extcomm(struct evpnes
*es
,
703 struct ecommunity ecom_encap
;
704 struct ecommunity ecom_es_rt
;
705 struct ecommunity_val eval
;
706 struct ecommunity_val eval_es_rt
;
707 bgp_encap_types tnl_type
;
711 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
712 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
713 encode_encap_extcomm(tnl_type
, &eval
);
715 ecom_encap
.val
= (uint8_t *)eval
.val
;
716 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
719 memset(&mac
, 0, sizeof(struct ethaddr
));
720 memset(&ecom_es_rt
, 0, sizeof(ecom_es_rt
));
721 es_get_system_mac(&es
->esi
, &mac
);
722 encode_es_rt_extcomm(&eval_es_rt
, &mac
);
724 ecom_es_rt
.val
= (uint8_t *)eval_es_rt
.val
;
726 ecommunity_merge(attr
->ecommunity
, &ecom_es_rt
);
728 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
732 * Build extended communities for EVPN prefix route.
734 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
737 struct ecommunity ecom_encap
;
738 struct ecommunity ecom_rmac
;
739 struct ecommunity_val eval
;
740 struct ecommunity_val eval_rmac
;
741 bgp_encap_types tnl_type
;
742 struct listnode
*node
, *nnode
;
743 struct ecommunity
*ecom
;
744 struct list
*vrf_export_rtl
= NULL
;
747 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
748 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
749 encode_encap_extcomm(tnl_type
, &eval
);
751 ecom_encap
.val
= (uint8_t *)eval
.val
;
754 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
756 /* Add the export RTs for L3VNI/VRF */
757 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
758 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
760 ecommunity_merge(attr
->ecommunity
, ecom
);
762 /* add the router mac extended community */
763 if (!is_zero_mac(&attr
->rmac
)) {
764 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
765 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
767 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
769 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
772 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
776 * Build extended communities for EVPN route.
777 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
778 * and ENCAP extended communities are applicable for all routes.
779 * The default gateway extended community and MAC mobility (sticky) extended
780 * community are added as needed based on passed settings - only for type-2
781 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
782 * added, if present, based on passed settings - only for non-link-local
785 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
788 struct ecommunity ecom_encap
;
789 struct ecommunity ecom_sticky
;
790 struct ecommunity ecom_default_gw
;
791 struct ecommunity ecom_rmac
;
792 struct ecommunity ecom_na
;
793 struct ecommunity_val eval
;
794 struct ecommunity_val eval_sticky
;
795 struct ecommunity_val eval_default_gw
;
796 struct ecommunity_val eval_rmac
;
797 struct ecommunity_val eval_na
;
799 bgp_encap_types tnl_type
;
800 struct listnode
*node
, *nnode
;
801 struct ecommunity
*ecom
;
803 struct list
*vrf_export_rtl
= NULL
;
806 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
807 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
808 encode_encap_extcomm(tnl_type
, &eval
);
810 ecom_encap
.val
= (uint8_t *)eval
.val
;
813 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
815 /* Add the export RTs for L2VNI */
816 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
817 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
819 /* Add the export RTs for L3VNI if told to - caller determines
820 * when this should be done.
823 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
824 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
825 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
827 attr
->ecommunity
= ecommunity_merge(
828 attr
->ecommunity
, ecom
);
832 /* Add MAC mobility (sticky) if needed. */
835 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
836 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
837 ecom_sticky
.size
= 1;
838 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
840 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
843 /* Add RMAC, if told to. */
845 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
846 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
848 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
850 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
853 /* Add default gateway, if needed. */
854 if (attr
->default_gw
) {
855 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
856 encode_default_gw_extcomm(&eval_default_gw
);
857 ecom_default_gw
.size
= 1;
858 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
860 ecommunity_merge(attr
->ecommunity
, &ecom_default_gw
);
863 if (attr
->router_flag
) {
864 memset(&ecom_na
, 0, sizeof(ecom_na
));
865 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
);
867 ecom_na
.val
= (uint8_t *)eval_na
.val
;
868 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
872 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
876 * Add MAC mobility extended community to attribute.
878 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
880 struct ecommunity ecom_tmp
;
881 struct ecommunity_val eval
;
882 uint8_t *ecom_val_ptr
;
889 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
891 /* Find current MM ecommunity */
894 if (attr
->ecommunity
) {
895 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
896 pnt
= attr
->ecommunity
->val
+ (i
* 8);
900 if (type
== ECOMMUNITY_ENCODE_EVPN
902 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
903 ecom_val_ptr
= (uint8_t *)(attr
->ecommunity
->val
910 /* Update the existing MM ecommunity */
912 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
914 /* Add MM to existing */
916 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
918 ecom_tmp
.val
= (uint8_t *)eval
.val
;
920 if (attr
->ecommunity
)
922 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
924 attr
->ecommunity
= ecommunity_dup(&ecom_tmp
);
928 /* Install EVPN route into zebra. */
929 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
930 struct prefix_evpn
*p
, struct bgp_path_info
*pi
)
936 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
938 if (pi
->attr
->sticky
)
939 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
940 if (pi
->attr
->default_gw
)
941 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
942 if (is_evpn_prefix_ipaddr_v6(p
) &&
943 pi
->attr
->router_flag
)
944 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
945 ret
= bgp_zebra_send_remote_macip(
946 bgp
, vpn
, p
, pi
->attr
->nexthop
, 1, flags
,
947 mac_mobility_seqnum(pi
->attr
));
949 switch (pi
->attr
->pmsi_tnl_type
) {
950 case PMSI_TNLTYPE_INGR_REPL
:
951 flood_control
= VXLAN_FLOOD_HEAD_END_REPL
;
954 case PMSI_TNLTYPE_PIM_SM
:
955 flood_control
= VXLAN_FLOOD_PIM_SM
;
959 flood_control
= VXLAN_FLOOD_DISABLED
;
962 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, flood_control
, 1);
968 /* Uninstall EVPN route from zebra. */
969 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
970 struct prefix_evpn
*p
,
971 struct in_addr remote_vtep_ip
)
975 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
976 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
979 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
,
980 VXLAN_FLOOD_DISABLED
, 0);
986 * Due to MAC mobility, the prior "local" best route has been supplanted
987 * by a "remote" best route. The prior route has to be deleted and withdrawn
990 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
992 struct bgp_path_info
*old_local
)
994 struct bgp_node
*global_rn
;
995 struct bgp_path_info
*pi
;
996 afi_t afi
= AFI_L2VPN
;
997 safi_t safi
= SAFI_EVPN
;
999 /* Locate route node in the global EVPN routing table. Note that
1000 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1003 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1004 (struct prefix
*)&rn
->p
, &vpn
->prd
);
1006 /* Delete route entry in the global EVPN table. */
1007 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1009 /* Schedule for processing - withdraws to peers happen from
1013 bgp_process(bgp
, global_rn
, afi
, safi
);
1014 bgp_unlock_node(global_rn
);
1017 /* Delete route entry in the VNI route table, caller to remove. */
1018 bgp_path_info_delete(rn
, old_local
);
1021 static struct in_addr
*es_vtep_new(struct in_addr vtep
)
1025 ip
= XCALLOC(MTYPE_BGP_EVPN_ES_VTEP
, sizeof(struct in_addr
));
1027 ip
->s_addr
= vtep
.s_addr
;
1031 static void es_vtep_free(struct in_addr
*ip
)
1033 XFREE(MTYPE_BGP_EVPN_ES_VTEP
, ip
);
1036 /* check if VTEP is already part of the list */
1037 static int is_vtep_present_in_list(struct list
*list
,
1038 struct in_addr vtep
)
1040 struct listnode
*node
= NULL
;
1041 struct in_addr
*tmp
;
1043 for (ALL_LIST_ELEMENTS_RO(list
, node
, tmp
)) {
1044 if (tmp
->s_addr
== vtep
.s_addr
)
1051 * Best path for ES route was changed,
1052 * update the list of VTEPs for this ES
1054 static int evpn_es_install_vtep(struct bgp
*bgp
,
1056 struct prefix_evpn
*p
,
1057 struct in_addr rvtep
)
1059 struct in_addr
*vtep_ip
;
1061 if (is_vtep_present_in_list(es
->vtep_list
, rvtep
))
1065 vtep_ip
= es_vtep_new(rvtep
);
1067 listnode_add_sort(es
->vtep_list
, vtep_ip
);
1072 * Best path for ES route was changed,
1073 * update the list of VTEPs for this ES
1075 static int evpn_es_uninstall_vtep(struct bgp
*bgp
,
1077 struct prefix_evpn
*p
,
1078 struct in_addr rvtep
)
1080 struct listnode
*node
, *nnode
, *node_to_del
= NULL
;
1081 struct in_addr
*tmp
;
1083 for (ALL_LIST_ELEMENTS(es
->vtep_list
, node
, nnode
, tmp
)) {
1084 if (tmp
->s_addr
== rvtep
.s_addr
) {
1091 list_delete_node(es
->vtep_list
, node_to_del
);
1097 * Calculate the best path for a ES(type-4) route.
1099 static int evpn_es_route_select_install(struct bgp
*bgp
,
1101 struct bgp_node
*rn
)
1104 afi_t afi
= AFI_L2VPN
;
1105 safi_t safi
= SAFI_EVPN
;
1106 struct bgp_path_info
*old_select
; /* old best */
1107 struct bgp_path_info
*new_select
; /* new best */
1108 struct bgp_path_info_pair old_and_new
;
1110 /* Compute the best path. */
1111 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
],
1112 &old_and_new
, afi
, safi
);
1113 old_select
= old_and_new
.old
;
1114 new_select
= old_and_new
.new;
1117 * If the best path hasn't changed - see if something needs to be
1120 if (old_select
&& old_select
== new_select
1121 && old_select
->type
== ZEBRA_ROUTE_BGP
1122 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1123 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1124 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1125 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1126 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1127 ret
= evpn_es_install_vtep(bgp
, es
,
1128 (struct prefix_evpn
*)&rn
->p
,
1129 old_select
->attr
->nexthop
);
1131 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1132 bgp_zebra_clear_route_change_flags(rn
);
1136 /* If the user did a "clear" this flag will be set */
1137 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1140 * bestpath has changed; update relevant fields and install or uninstall
1141 * into the zebra RIB.
1143 if (old_select
|| new_select
)
1144 bgp_bump_version(rn
);
1147 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1149 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1150 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1151 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1154 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1155 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1156 ret
= evpn_es_install_vtep(bgp
, es
,
1157 (struct prefix_evpn
*)&rn
->p
,
1158 new_select
->attr
->nexthop
);
1160 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1161 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1162 ret
= evpn_es_uninstall_vtep(
1163 bgp
, es
, (struct prefix_evpn
*)&rn
->p
,
1164 old_select
->attr
->nexthop
);
1167 /* Clear any route change flags. */
1168 bgp_zebra_clear_route_change_flags(rn
);
1170 /* Reap old select bgp_path_info, if it has been removed */
1171 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1172 bgp_path_info_reap(rn
, old_select
);
1178 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1181 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1182 struct bgp_node
*rn
)
1184 struct bgp_path_info
*old_select
, *new_select
;
1185 struct bgp_path_info_pair old_and_new
;
1186 afi_t afi
= AFI_L2VPN
;
1187 safi_t safi
= SAFI_EVPN
;
1190 /* Compute the best path. */
1191 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1193 old_select
= old_and_new
.old
;
1194 new_select
= old_and_new
.new;
1196 /* If the best path hasn't changed - see if there is still something to
1200 if (old_select
&& old_select
== new_select
1201 && old_select
->type
== ZEBRA_ROUTE_BGP
1202 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1203 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1204 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1205 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1206 if (bgp_zebra_has_route_changed(rn
, old_select
))
1207 ret
= evpn_zebra_install(
1208 bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1210 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1211 bgp_zebra_clear_route_change_flags(rn
);
1215 /* If the user did a "clear" this flag will be set */
1216 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1218 /* bestpath has changed; update relevant fields and install or uninstall
1219 * into the zebra RIB.
1221 if (old_select
|| new_select
)
1222 bgp_bump_version(rn
);
1225 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1227 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1228 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1229 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1232 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1233 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1234 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1237 /* If an old best existed and it was a "local" route, the only
1239 * it would be supplanted is due to MAC mobility procedures. So,
1241 * need to do an implicit delete and withdraw that route from
1244 if (old_select
&& old_select
->peer
== bgp
->peer_self
1245 && old_select
->type
== ZEBRA_ROUTE_BGP
1246 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
1247 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
1249 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1250 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1251 ret
= evpn_zebra_uninstall(bgp
, vpn
,
1252 (struct prefix_evpn
*)&rn
->p
,
1253 old_select
->attr
->nexthop
);
1256 /* Clear any route change flags. */
1257 bgp_zebra_clear_route_change_flags(rn
);
1259 /* Reap old select bgp_path_info, if it has been removed */
1260 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1261 bgp_path_info_reap(rn
, old_select
);
1267 * Return true if the local ri for this rn is of type gateway mac
1269 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
1271 struct bgp_path_info
*tmp_pi
= NULL
;
1272 struct bgp_path_info
*local_pi
= NULL
;
1275 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1276 tmp_pi
= tmp_pi
->next
) {
1277 if (tmp_pi
->peer
== bgp
->peer_self
1278 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1279 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1286 return local_pi
->attr
->default_gw
;
1291 * Return true if the local ri for this rn has sticky set
1293 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
1295 struct bgp_path_info
*tmp_pi
;
1296 struct bgp_path_info
*local_pi
;
1299 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1300 tmp_pi
= tmp_pi
->next
) {
1301 if (tmp_pi
->peer
== bgp
->peer_self
1302 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1303 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1310 return local_pi
->attr
->sticky
;
1314 * create or update EVPN type4 route entry.
1315 * This could be in the ES table or the global table.
1316 * TODO: handle remote ES (type4) routes as well
1318 static int update_evpn_type4_route_entry(struct bgp
*bgp
, struct evpnes
*es
,
1319 afi_t afi
, safi_t safi
,
1320 struct bgp_node
*rn
, struct attr
*attr
,
1321 int add
, struct bgp_path_info
**ri
,
1324 char buf
[ESI_STR_LEN
];
1325 char buf1
[INET6_ADDRSTRLEN
];
1326 struct bgp_path_info
*tmp_pi
= NULL
;
1327 struct bgp_path_info
*local_pi
= NULL
; /* local route entry if any */
1328 struct bgp_path_info
*remote_pi
= NULL
; /* remote route entry if any */
1329 struct attr
*attr_new
= NULL
;
1330 struct prefix_evpn
*evp
= NULL
;
1334 evp
= (struct prefix_evpn
*)&rn
->p
;
1336 /* locate the local and remote entries if any */
1337 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1338 tmp_pi
= tmp_pi
->next
) {
1339 if (tmp_pi
->peer
== bgp
->peer_self
1340 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1341 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1343 if (tmp_pi
->type
== ZEBRA_ROUTE_BGP
1344 && tmp_pi
->sub_type
== BGP_ROUTE_IMPORTED
1345 && CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1349 /* we don't expect to see a remote_ri at this point.
1350 * An ES route has esi + vtep_ip as the key,
1351 * We shouldn't see the same route from any other vtep.
1356 "%u ERROR: local es route for ESI: %s Vtep %s also learnt from remote",
1358 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1359 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1363 if (!local_pi
&& !add
)
1366 /* create or update the entry */
1369 /* Add or update attribute to hash */
1370 attr_new
= bgp_attr_intern(attr
);
1372 /* Create new route with its attribute. */
1373 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1374 bgp
->peer_self
, attr_new
, rn
);
1375 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1377 /* add the newly created path to the route-node */
1378 bgp_path_info_add(rn
, tmp_pi
);
1381 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1382 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1385 /* The attribute has changed.
1386 * Add (or update) attribute to hash. */
1387 attr_new
= bgp_attr_intern(attr
);
1388 bgp_path_info_set_flag(rn
, tmp_pi
,
1389 BGP_PATH_ATTR_CHANGED
);
1391 /* Restore route, if needed. */
1392 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1393 bgp_path_info_restore(rn
, tmp_pi
);
1395 /* Unintern existing, set to new. */
1396 bgp_attr_unintern(&tmp_pi
->attr
);
1397 tmp_pi
->attr
= attr_new
;
1398 tmp_pi
->uptime
= bgp_clock();
1402 /* Return back the route entry. */
1407 /* update evpn es (type-4) route */
1408 static int update_evpn_type4_route(struct bgp
*bgp
,
1410 struct prefix_evpn
*p
)
1413 int route_changed
= 0;
1414 char buf
[ESI_STR_LEN
];
1415 char buf1
[INET6_ADDRSTRLEN
];
1416 afi_t afi
= AFI_L2VPN
;
1417 safi_t safi
= SAFI_EVPN
;
1419 struct attr
*attr_new
= NULL
;
1420 struct bgp_node
*rn
= NULL
;
1421 struct bgp_path_info
*pi
= NULL
;
1423 memset(&attr
, 0, sizeof(struct attr
));
1425 /* Build path-attribute for this route. */
1426 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1427 attr
.nexthop
= es
->originator_ip
.ipaddr_v4
;
1428 attr
.mp_nexthop_global_in
= es
->originator_ip
.ipaddr_v4
;
1429 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1431 /* Set up extended community. */
1432 build_evpn_type4_route_extcomm(es
, &attr
);
1434 /* First, create (or fetch) route node within the ESI. */
1435 /* NOTE: There is no RD here. */
1436 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
1438 /* Create or update route entry. */
1439 ret
= update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, &attr
, 1,
1440 &pi
, &route_changed
);
1442 flog_err(EC_BGP_ES_INVALID
,
1443 "%u ERROR: Failed to updated ES route ESI: %s VTEP %s",
1445 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1446 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1450 attr_new
= pi
->attr
;
1452 /* Perform route selection;
1453 * this is just to set the flags correctly
1454 * as local route in the ES always wins.
1456 evpn_es_route_select_install(bgp
, es
, rn
);
1457 bgp_unlock_node(rn
);
1459 /* If this is a new route or some attribute has changed, export the
1460 * route to the global table. The route will be advertised to peers
1461 * from there. Note that this table is a 2-level tree (RD-level +
1462 * Prefix-level) similar to L3VPN routes.
1464 if (route_changed
) {
1465 struct bgp_path_info
*global_pi
;
1467 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1468 (struct prefix
*)p
, &es
->prd
);
1469 update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, attr_new
,
1470 1, &global_pi
, &route_changed
);
1472 /* Schedule for processing and unlock node. */
1473 bgp_process(bgp
, rn
, afi
, safi
);
1474 bgp_unlock_node(rn
);
1477 /* Unintern temporary. */
1478 aspath_unintern(&attr
.aspath
);
1482 static int update_evpn_type5_route_entry(struct bgp
*bgp_evpn
,
1483 struct bgp
*bgp_vrf
, afi_t afi
,
1484 safi_t safi
, struct bgp_node
*rn
,
1485 struct attr
*attr
, int *route_changed
)
1487 struct attr
*attr_new
= NULL
;
1488 struct bgp_path_info
*pi
= NULL
;
1489 mpls_label_t label
= MPLS_INVALID_LABEL
;
1490 struct bgp_path_info
*local_pi
= NULL
;
1491 struct bgp_path_info
*tmp_pi
= NULL
;
1494 /* locate the local route entry if any */
1495 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1496 tmp_pi
= tmp_pi
->next
) {
1497 if (tmp_pi
->peer
== bgp_evpn
->peer_self
1498 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1499 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1504 * create a new route entry if one doesn't exist.
1505 * Otherwise see if route attr has changed
1509 /* route has changed as this is the first entry */
1512 /* Add (or update) attribute to hash. */
1513 attr_new
= bgp_attr_intern(attr
);
1515 /* create the route info from attribute */
1516 pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1517 bgp_evpn
->peer_self
, attr_new
, rn
);
1518 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
1520 /* Type-5 routes advertise the L3-VNI */
1521 bgp_path_info_extra_get(pi
);
1522 vni2label(bgp_vrf
->l3vni
, &label
);
1523 memcpy(&pi
->extra
->label
, &label
, sizeof(label
));
1524 pi
->extra
->num_labels
= 1;
1526 /* add the route entry to route node*/
1527 bgp_path_info_add(rn
, pi
);
1531 if (!attrhash_cmp(tmp_pi
->attr
, attr
)) {
1533 /* attribute changed */
1536 /* The attribute has changed. */
1537 /* Add (or update) attribute to hash. */
1538 attr_new
= bgp_attr_intern(attr
);
1539 bgp_path_info_set_flag(rn
, tmp_pi
,
1540 BGP_PATH_ATTR_CHANGED
);
1542 /* Restore route, if needed. */
1543 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1544 bgp_path_info_restore(rn
, tmp_pi
);
1546 /* Unintern existing, set to new. */
1547 bgp_attr_unintern(&tmp_pi
->attr
);
1548 tmp_pi
->attr
= attr_new
;
1549 tmp_pi
->uptime
= bgp_clock();
1555 /* update evpn type-5 route entry */
1556 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1557 struct attr
*src_attr
)
1559 afi_t afi
= AFI_L2VPN
;
1560 safi_t safi
= SAFI_EVPN
;
1562 struct bgp_node
*rn
= NULL
;
1563 struct bgp
*bgp_evpn
= NULL
;
1564 int route_changed
= 0;
1566 bgp_evpn
= bgp_get_evpn();
1570 /* Build path attribute for this route - use the source attr, if
1571 * present, else treat as locally originated.
1574 bgp_attr_dup(&attr
, src_attr
);
1576 memset(&attr
, 0, sizeof(struct attr
));
1577 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1581 memcpy(&attr
.rmac
, &bgp_vrf
->evpn_info
->pip_rmac
, ETH_ALEN
);
1582 /* Advertise Primary IP (PIP) is enabled, send individual
1583 * IP (default instance router-id) as nexthop.
1584 * PIP is disabled or vrr interface is not present
1585 * use anycast-IP as nexthop.
1587 if (!bgp_vrf
->evpn_info
->advertise_pip
||
1588 (!bgp_vrf
->evpn_info
->is_anycast_mac
)) {
1589 attr
.nexthop
= bgp_vrf
->originator_ip
;
1590 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1592 if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
!= INADDR_ANY
) {
1593 attr
.nexthop
= bgp_vrf
->evpn_info
->pip_ip
;
1594 attr
.mp_nexthop_global_in
= bgp_vrf
->evpn_info
->pip_ip
;
1595 } else if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
== INADDR_ANY
)
1596 if (bgp_debug_zebra(NULL
)) {
1597 char buf1
[PREFIX_STRLEN
];
1599 zlog_debug("VRF %s evp %s advertise-pip primary ip is not configured",
1600 vrf_id_to_name(bgp_vrf
->vrf_id
),
1601 prefix2str(evp
, buf1
, sizeof(buf1
)));
1605 if (bgp_debug_zebra(NULL
)) {
1606 char buf
[ETHER_ADDR_STRLEN
];
1607 char buf1
[PREFIX_STRLEN
];
1609 zlog_debug("VRF %s type-5 route evp %s RMAC %s nexthop %s",
1610 vrf_id_to_name(bgp_vrf
->vrf_id
),
1611 prefix2str(evp
, buf1
, sizeof(buf1
)),
1612 prefix_mac2str(&attr
.rmac
, buf
, sizeof(buf
)),
1613 inet_ntoa(attr
.nexthop
));
1616 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1618 /* Setup RT and encap extended community */
1619 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1621 /* get the route node in global table */
1622 rn
= bgp_afi_node_get(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1623 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1626 /* create or update the route entry within the route node */
1627 update_evpn_type5_route_entry(bgp_evpn
, bgp_vrf
, afi
, safi
, rn
, &attr
,
1630 /* schedule for processing and unlock node */
1631 if (route_changed
) {
1632 bgp_process(bgp_evpn
, rn
, afi
, safi
);
1633 bgp_unlock_node(rn
);
1636 /* uninten temporary */
1638 aspath_unintern(&attr
.aspath
);
1643 * Create or update EVPN route entry. This could be in the VNI route table
1644 * or the global route table.
1646 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1647 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1648 struct attr
*attr
, int add
,
1649 struct bgp_path_info
**pi
, uint8_t flags
,
1652 struct bgp_path_info
*tmp_pi
;
1653 struct bgp_path_info
*local_pi
;
1654 struct attr
*attr_new
;
1655 mpls_label_t label
[BGP_MAX_LABELS
];
1656 uint32_t num_labels
= 1;
1657 int route_change
= 1;
1659 struct prefix_evpn
*evp
;
1662 evp
= (struct prefix_evpn
*)&rn
->p
;
1663 memset(&label
, 0, sizeof(label
));
1665 /* See if this is an update of an existing route, or a new add. */
1667 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1668 tmp_pi
= tmp_pi
->next
) {
1669 if (tmp_pi
->peer
== bgp
->peer_self
1670 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1671 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1675 /* If route doesn't exist already, create a new one, if told to.
1676 * Otherwise act based on whether the attributes of the route have
1679 if (!local_pi
&& !add
)
1682 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1683 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1684 add_mac_mobility_to_attr(seq
, attr
);
1687 /* Add (or update) attribute to hash. */
1688 attr_new
= bgp_attr_intern(attr
);
1690 /* Extract MAC mobility sequence number, if any. */
1691 attr_new
->mm_seqnum
=
1692 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1693 attr_new
->sticky
= sticky
;
1695 /* Create new route with its attribute. */
1696 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1697 bgp
->peer_self
, attr_new
, rn
);
1698 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1699 bgp_path_info_extra_get(tmp_pi
);
1701 /* The VNI goes into the 'label' field of the route */
1702 vni2label(vpn
->vni
, &label
[0]);
1704 /* Type-2 routes may carry a second VNI - the L3-VNI.
1705 * Only attach second label if we are advertising two labels for
1708 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1709 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1712 l3vni
= bgpevpn_get_l3vni(vpn
);
1714 vni2label(l3vni
, &label
[1]);
1719 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1720 tmp_pi
->extra
->num_labels
= num_labels
;
1721 /* Mark route as self type-2 route */
1722 if (flags
&& CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1723 tmp_pi
->extra
->af_flags
= BGP_EVPN_MACIP_TYPE_SVI_IP
;
1724 bgp_path_info_add(rn
, tmp_pi
);
1727 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1728 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1732 * The attributes have changed, type-2 routes needs to
1733 * be advertised with right labels.
1735 vni2label(vpn
->vni
, &label
[0]);
1736 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1737 && CHECK_FLAG(vpn
->flags
,
1738 VNI_FLAG_USE_TWO_LABELS
)) {
1741 l3vni
= bgpevpn_get_l3vni(vpn
);
1743 vni2label(l3vni
, &label
[1]);
1747 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1748 tmp_pi
->extra
->num_labels
= num_labels
;
1750 /* The attribute has changed. */
1751 /* Add (or update) attribute to hash. */
1752 attr_new
= bgp_attr_intern(attr
);
1753 bgp_path_info_set_flag(rn
, tmp_pi
,
1754 BGP_PATH_ATTR_CHANGED
);
1756 /* Extract MAC mobility sequence number, if any. */
1757 attr_new
->mm_seqnum
=
1758 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1759 attr_new
->sticky
= sticky
;
1761 /* Restore route, if needed. */
1762 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1763 bgp_path_info_restore(rn
, tmp_pi
);
1765 /* Unintern existing, set to new. */
1766 bgp_attr_unintern(&tmp_pi
->attr
);
1767 tmp_pi
->attr
= attr_new
;
1768 tmp_pi
->uptime
= bgp_clock();
1772 /* Return back the route entry. */
1774 return route_change
;
1777 static void evpn_zebra_reinstall_best_route(struct bgp
*bgp
,
1778 struct bgpevpn
*vpn
, struct bgp_node
*rn
)
1780 struct bgp_path_info
*tmp_ri
;
1781 struct bgp_path_info
*curr_select
= NULL
;
1783 for (tmp_ri
= bgp_node_get_bgp_path_info(rn
);
1784 tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1785 if (CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_SELECTED
)) {
1786 curr_select
= tmp_ri
;
1791 if (curr_select
&& curr_select
->type
== ZEBRA_ROUTE_BGP
1792 && curr_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1793 evpn_zebra_install(bgp
, vpn
,
1794 (struct prefix_evpn
*)&rn
->p
,
1799 * If the local route was not selected evict it and tell zebra to re-add
1800 * the best remote dest.
1802 * Typically a local path added by zebra is expected to be selected as
1803 * best. In which case when a remote path wins as best (later)
1804 * evpn_route_select_install itself evicts the older-local-best path.
1806 * However if bgp's add and zebra's add cross paths (race condition) it
1807 * is possible that the local path is no longer the "older" best path.
1808 * It is a path that was never designated as best and hence requires
1809 * additional handling to prevent bgp from injecting and holding on to a
1810 * non-best local path.
1812 static void evpn_cleanup_local_non_best_route(struct bgp
*bgp
,
1813 struct bgpevpn
*vpn
,
1814 struct bgp_node
*rn
,
1815 struct bgp_path_info
*local_pi
)
1817 char buf
[PREFIX_STRLEN
];
1819 /* local path was not picked as the winner; kick it out */
1820 if (bgp_debug_zebra(NULL
)) {
1821 zlog_debug("evicting local evpn prefix %s as remote won",
1822 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
1824 evpn_delete_old_local_route(bgp
, vpn
, rn
, local_pi
);
1825 bgp_path_info_reap(rn
, local_pi
);
1827 /* tell zebra to re-add the best remote path */
1828 evpn_zebra_reinstall_best_route(bgp
, vpn
, rn
);
1832 * Create or update EVPN route (of type based on prefix) for specified VNI
1833 * and schedule for processing.
1835 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1836 struct prefix_evpn
*p
, uint8_t flags
,
1839 struct bgp_node
*rn
;
1841 struct attr
*attr_new
;
1842 int add_l3_ecomm
= 0;
1843 struct bgp_path_info
*pi
;
1844 afi_t afi
= AFI_L2VPN
;
1845 safi_t safi
= SAFI_EVPN
;
1848 memset(&attr
, 0, sizeof(struct attr
));
1850 /* Build path-attribute for this route. */
1851 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1852 attr
.nexthop
= vpn
->originator_ip
;
1853 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1854 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1855 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1856 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1857 attr
.router_flag
= CHECK_FLAG(flags
,
1858 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1860 /* PMSI is only needed for type-3 routes */
1861 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
1862 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1863 attr
.pmsi_tnl_type
= PMSI_TNLTYPE_INGR_REPL
;
1866 /* router mac is only needed for type-2 routes here. */
1867 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
1868 uint8_t af_flags
= 0;
1870 if (CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1871 SET_FLAG(af_flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
);
1873 bgp_evpn_get_rmac_nexthop(vpn
, p
, &attr
, af_flags
);
1875 if (bgp_debug_zebra(NULL
)) {
1876 char buf
[ETHER_ADDR_STRLEN
];
1877 char buf1
[PREFIX_STRLEN
];
1879 zlog_debug("VRF %s vni %u type-2 route evp %s RMAC %s nexthop %s",
1881 vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
) : " ",
1883 prefix2str(p
, buf1
, sizeof(buf1
)),
1884 prefix_mac2str(&attr
.rmac
, buf
,
1886 inet_ntoa(attr
.mp_nexthop_global_in
));
1890 vni2label(vpn
->vni
, &(attr
.label
));
1892 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1893 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1896 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1897 (is_evpn_prefix_ipaddr_v4(p
) ||
1898 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1899 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1900 bgpevpn_get_l3vni(vpn
))
1903 /* Set up extended community. */
1904 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1906 /* First, create (or fetch) route node within the VNI. */
1907 /* NOTE: There is no RD here. */
1908 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1910 /* Create or update route entry. */
1911 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1912 1, &pi
, flags
, seq
);
1914 attr_new
= pi
->attr
;
1916 /* lock ri to prevent freeing in evpn_route_select_install */
1917 bgp_path_info_lock(pi
);
1918 /* Perform route selection; this is just to set the flags correctly
1919 * as local route in the VNI always wins.
1921 evpn_route_select_install(bgp
, vpn
, rn
);
1923 * If the new local route was not selected evict it and tell zebra
1924 * to re-add the best remote dest. BGP doesn't retain non-best local
1927 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1929 evpn_cleanup_local_non_best_route(bgp
, vpn
, rn
, pi
);
1931 bgp_path_info_unlock(pi
);
1933 bgp_unlock_node(rn
);
1935 /* If this is a new route or some attribute has changed, export the
1936 * route to the global table. The route will be advertised to peers
1937 * from there. Note that this table is a 2-level tree (RD-level +
1938 * Prefix-level) similar to L3VPN routes.
1941 struct bgp_path_info
*global_pi
;
1943 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1944 (struct prefix
*)p
, &vpn
->prd
);
1945 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1,
1946 &global_pi
, flags
, seq
);
1948 /* Schedule for processing and unlock node. */
1949 bgp_process(bgp
, rn
, afi
, safi
);
1950 bgp_unlock_node(rn
);
1953 /* Unintern temporary. */
1954 aspath_unintern(&attr
.aspath
);
1960 * Delete EVPN route entry.
1961 * The entry can be in ESI/VNI table or the global table.
1963 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1964 struct bgp_node
*rn
,
1965 struct bgp_path_info
**pi
)
1967 struct bgp_path_info
*tmp_pi
;
1971 /* Now, find matching route. */
1972 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1973 tmp_pi
= tmp_pi
->next
)
1974 if (tmp_pi
->peer
== bgp
->peer_self
1975 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1976 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1981 /* Mark route for delete. */
1983 bgp_path_info_delete(rn
, tmp_pi
);
1988 /* Delete EVPN ES (type-4) route */
1989 static int delete_evpn_type4_route(struct bgp
*bgp
,
1991 struct prefix_evpn
*p
)
1993 afi_t afi
= AFI_L2VPN
;
1994 safi_t safi
= SAFI_EVPN
;
1995 struct bgp_path_info
*pi
;
1996 struct bgp_node
*rn
= NULL
; /* rn in esi table */
1997 struct bgp_node
*global_rn
= NULL
; /* rn in global table */
1999 /* First, locate the route node within the ESI.
2000 * If it doesn't exist, ther is nothing to do.
2001 * Note: there is no RD here.
2003 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2007 /* Next, locate route node in the global EVPN routing table.
2008 * Note that this table is a 2-level tree (RD-level + Prefix-level)
2010 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
2011 (struct prefix
*)p
, &es
->prd
);
2014 /* Delete route entry in the global EVPN table. */
2015 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
2017 /* Schedule for processing - withdraws to peers happen from
2021 bgp_process(bgp
, global_rn
, afi
, safi
);
2022 bgp_unlock_node(global_rn
);
2026 * Delete route entry in the ESI route table.
2027 * This can just be removed.
2029 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2031 bgp_path_info_reap(rn
, pi
);
2032 bgp_unlock_node(rn
);
2036 /* Delete EVPN type5 route */
2037 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
2039 afi_t afi
= AFI_L2VPN
;
2040 safi_t safi
= SAFI_EVPN
;
2041 struct bgp_node
*rn
= NULL
;
2042 struct bgp_path_info
*pi
= NULL
;
2043 struct bgp
*bgp_evpn
= NULL
; /* evpn bgp instance */
2045 bgp_evpn
= bgp_get_evpn();
2049 /* locate the global route entry for this type-5 prefix */
2050 rn
= bgp_afi_node_lookup(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
2051 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
2055 delete_evpn_route_entry(bgp_evpn
, afi
, safi
, rn
, &pi
);
2057 bgp_process(bgp_evpn
, rn
, afi
, safi
);
2058 bgp_unlock_node(rn
);
2063 * Delete EVPN route (of type based on prefix) for specified VNI and
2064 * schedule for processing.
2066 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2067 struct prefix_evpn
*p
)
2069 struct bgp_node
*rn
, *global_rn
;
2070 struct bgp_path_info
*pi
;
2071 afi_t afi
= AFI_L2VPN
;
2072 safi_t safi
= SAFI_EVPN
;
2074 /* First, locate the route node within the VNI. If it doesn't exist,
2076 * is nothing further to do.
2078 /* NOTE: There is no RD here. */
2079 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2083 /* Next, locate route node in the global EVPN routing table. Note that
2084 * this table is a 2-level tree (RD-level + Prefix-level) similar to
2087 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
2088 (struct prefix
*)p
, &vpn
->prd
);
2090 /* Delete route entry in the global EVPN table. */
2091 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
2093 /* Schedule for processing - withdraws to peers happen from
2097 bgp_process(bgp
, global_rn
, afi
, safi
);
2098 bgp_unlock_node(global_rn
);
2101 /* Delete route entry in the VNI route table. This can just be removed.
2103 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2105 bgp_path_info_reap(rn
, pi
);
2106 evpn_route_select_install(bgp
, vpn
, rn
);
2108 bgp_unlock_node(rn
);
2114 * Update all type-2 (MACIP) local routes for this VNI - these should also
2115 * be scheduled for advertise to peers.
2117 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2121 struct bgp_node
*rn
;
2122 struct bgp_path_info
*pi
, *tmp_pi
;
2124 struct attr
*attr_new
;
2126 int add_l3_ecomm
= 0;
2131 /* Walk this VNI's route table and update local type-2 routes. For any
2132 * routes updated, update corresponding entry in the global table too.
2134 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2135 rn
= bgp_route_next(rn
)) {
2136 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2137 struct bgp_node
*rd_rn
;
2138 struct bgp_path_info
*global_pi
;
2140 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2143 /* Identify local route. */
2144 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
2145 tmp_pi
= tmp_pi
->next
) {
2146 if (tmp_pi
->peer
== bgp
->peer_self
2147 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2148 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2156 * Build attribute per local route as the MAC mobility and
2157 * some other values could differ for different routes. The
2158 * attributes will be shared in the hash table.
2160 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
2161 attr
.nexthop
= vpn
->originator_ip
;
2162 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
2163 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
2164 bgp_evpn_get_rmac_nexthop(vpn
, evp
, &attr
,
2165 tmp_pi
->extra
->af_flags
);
2167 if (evpn_route_is_sticky(bgp
, rn
))
2169 else if (evpn_route_is_def_gw(bgp
, rn
)) {
2170 attr
.default_gw
= 1;
2171 if (is_evpn_prefix_ipaddr_v6(evp
))
2172 attr
.router_flag
= 1;
2175 if (bgp_debug_zebra(NULL
)) {
2176 char buf
[ETHER_ADDR_STRLEN
];
2177 char buf1
[PREFIX_STRLEN
];
2179 zlog_debug("VRF %s vni %u evp %s RMAC %s nexthop %s",
2181 vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
) : " ",
2183 prefix2str(evp
, buf1
, sizeof(buf1
)),
2184 prefix_mac2str(&attr
.rmac
, buf
, sizeof(buf
)),
2185 inet_ntoa(attr
.mp_nexthop_global_in
));
2188 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2189 * using L3 VNI for type-2 routes also.
2191 if ((is_evpn_prefix_ipaddr_v4(evp
) ||
2192 !IN6_IS_ADDR_LINKLOCAL(
2193 &evp
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
2194 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
2195 bgpevpn_get_l3vni(vpn
))
2198 /* Set up extended community. */
2199 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2201 seq
= mac_mobility_seqnum(tmp_pi
->attr
);
2203 /* Update the route entry. */
2204 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
, 0, &pi
,
2207 /* Perform route selection; this is just to set the flags
2208 * correctly as local route in the VNI always wins.
2210 evpn_route_select_install(bgp
, vpn
, rn
);
2212 attr_new
= pi
->attr
;
2214 /* Update route in global routing table. */
2215 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2216 (struct prefix
*)evp
, &vpn
->prd
);
2218 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
2220 mac_mobility_seqnum(attr_new
));
2222 /* Schedule for processing and unlock node. */
2223 bgp_process(bgp
, rd_rn
, afi
, safi
);
2224 bgp_unlock_node(rd_rn
);
2226 /* Unintern temporary. */
2227 aspath_unintern(&attr
.aspath
);
2235 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2236 * global routing table. These are also scheduled for withdraw from peers.
2238 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2242 struct bgp_node
*rdrn
, *rn
;
2243 struct bgp_table
*table
;
2244 struct bgp_path_info
*pi
;
2249 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
2250 if (rdrn
&& bgp_node_has_bgp_path_info_data(rdrn
)) {
2251 table
= bgp_node_get_bgp_table_info(rdrn
);
2252 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2253 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2255 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2258 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2260 bgp_process(bgp
, rn
, afi
, safi
);
2264 /* Unlock RD node. */
2266 bgp_unlock_node(rdrn
);
2272 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2273 * table as well as the per-VNI route table.
2275 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2279 struct bgp_node
*rn
;
2280 struct bgp_path_info
*pi
;
2285 /* First, walk the global route table for this VNI's type-2 local
2287 * EVPN routes are a 2-level table, first get the RD table.
2289 delete_global_type2_routes(bgp
, vpn
);
2291 /* Next, walk this VNI's route table and delete local type-2 routes. */
2292 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2293 rn
= bgp_route_next(rn
)) {
2294 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2296 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2299 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2301 /* Route entry in local table gets deleted immediately. */
2303 bgp_path_info_reap(rn
, pi
);
2310 * Delete all routes in per ES route-table
2312 static int delete_all_es_routes(struct bgp
*bgp
, struct evpnes
*es
)
2314 struct bgp_node
*rn
;
2315 struct bgp_path_info
*pi
, *nextpi
;
2317 /* Walk this ES's route table and delete all routes. */
2318 for (rn
= bgp_table_top(es
->route_table
); rn
;
2319 rn
= bgp_route_next(rn
)) {
2320 for (pi
= bgp_node_get_bgp_path_info(rn
);
2321 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2322 bgp_path_info_delete(rn
, pi
);
2323 bgp_path_info_reap(rn
, pi
);
2331 * Delete all routes in the per-VNI route table.
2333 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2335 struct bgp_node
*rn
;
2336 struct bgp_path_info
*pi
, *nextpi
;
2338 /* Walk this VNI's route table and delete all routes. */
2339 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2340 rn
= bgp_route_next(rn
)) {
2341 for (pi
= bgp_node_get_bgp_path_info(rn
);
2342 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2343 bgp_path_info_delete(rn
, pi
);
2344 bgp_path_info_reap(rn
, pi
);
2351 /* BUM traffic flood mode per-l2-vni */
2352 static int bgp_evpn_vni_flood_mode_get(struct bgp
*bgp
,
2353 struct bgpevpn
*vpn
)
2355 /* if flooding has been globally disabled per-vni mode is
2358 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
2359 return VXLAN_FLOOD_DISABLED
;
2361 /* if mcast group ip has been specified we use a PIM-SM MDT */
2362 if (vpn
->mcast_grp
.s_addr
!= INADDR_ANY
)
2363 return VXLAN_FLOOD_PIM_SM
;
2365 /* default is ingress replication */
2366 return VXLAN_FLOOD_HEAD_END_REPL
;
2370 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2371 * export RT getting modified or change to tunnel IP. Note that these
2372 * situations need the route in the per-VNI table as well as the global
2373 * table to be updated (as attributes change).
2375 int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2378 struct prefix_evpn p
;
2380 /* Update and advertise the type-3 route (only one) followed by the
2381 * locally learnt type-2 routes (MACIP) - for this VNI.
2383 * RT-3 only if doing head-end replication
2385 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
2386 == VXLAN_FLOOD_HEAD_END_REPL
) {
2387 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2388 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0);
2393 return update_all_type2_routes(bgp
, vpn
);
2396 /* Delete (and withdraw) local routes for specified ES from global and ES table.
2397 * Also remove all other routes from the per ES table.
2398 * Invoked when ES is deleted.
2400 static int delete_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
2403 char buf
[ESI_STR_LEN
];
2404 struct prefix_evpn p
;
2406 /* Delete and withdraw locally learnt ES route */
2407 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
.ipaddr_v4
);
2408 ret
= delete_evpn_type4_route(bgp
, es
, &p
);
2410 flog_err(EC_BGP_EVPN_ROUTE_DELETE
,
2411 "%u failed to delete type-4 route for ESI %s",
2412 bgp
->vrf_id
, esi_to_str(&es
->esi
, buf
, sizeof(buf
)));
2415 /* Delete all routes from per ES table */
2416 return delete_all_es_routes(bgp
, es
);
2420 * Delete (and withdraw) local routes for specified VNI from the global
2421 * table and per-VNI table. After this, remove all other routes from
2422 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2423 * (advertise-all-vni) being disabled.
2425 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2428 struct prefix_evpn p
;
2430 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2431 * followed by type-3 routes (only one) - for this VNI.
2433 ret
= delete_all_type2_routes(bgp
, vpn
);
2437 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2438 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2442 /* Delete all routes from the per-VNI table. */
2443 return delete_all_vni_routes(bgp
, vpn
);
2447 * There is a flood mcast IP address change. Update the mcast-grp and
2448 * remove the type-3 route if any. A new type-3 route will be generated
2449 * post tunnel_ip update if the new flood mode is head-end-replication.
2451 static int bgp_evpn_mcast_grp_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2452 struct in_addr mcast_grp
)
2454 struct prefix_evpn p
;
2456 vpn
->mcast_grp
= mcast_grp
;
2458 if (is_vni_live(vpn
)) {
2459 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2460 delete_evpn_route(bgp
, vpn
, &p
);
2467 * There is a tunnel endpoint IP address change for this VNI, delete
2468 * prior type-3 route (if needed) and update.
2469 * Note: Route re-advertisement happens elsewhere after other processing
2472 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2473 struct in_addr originator_ip
)
2475 struct prefix_evpn p
;
2477 /* If VNI is not live, we only need to update the originator ip */
2478 if (!is_vni_live(vpn
)) {
2479 vpn
->originator_ip
= originator_ip
;
2483 /* Update the tunnel-ip hash */
2484 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2485 bgp_tip_add(bgp
, &originator_ip
);
2487 /* filter routes as martian nexthop db has changed */
2488 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2490 /* Need to withdraw type-3 route as the originator IP is part
2493 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2494 delete_evpn_route(bgp
, vpn
, &p
);
2496 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2497 vpn
->originator_ip
= originator_ip
;
2501 static struct bgp_path_info
*
2502 bgp_create_evpn_bgp_path_info(struct bgp_path_info
*parent_pi
,
2503 struct bgp_node
*rn
)
2505 struct attr
*attr_new
;
2506 struct bgp_path_info
*pi
;
2508 /* Add (or update) attribute to hash. */
2509 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2511 /* Create new route with its attribute. */
2512 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0, parent_pi
->peer
,
2514 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2515 bgp_path_info_extra_get(pi
);
2516 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2517 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2518 if (parent_pi
->extra
) {
2519 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2520 sizeof(pi
->extra
->label
));
2521 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2523 bgp_path_info_add(rn
, pi
);
2528 /* Install EVPN route entry in ES */
2529 static int install_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2530 struct prefix_evpn
*p
,
2531 struct bgp_path_info
*parent_pi
)
2534 struct bgp_node
*rn
= NULL
;
2535 struct bgp_path_info
*pi
= NULL
;
2536 struct attr
*attr_new
= NULL
;
2538 /* Create (or fetch) route within the VNI.
2539 * NOTE: There is no RD here.
2541 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
2543 /* Check if route entry is already present. */
2544 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2546 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2550 /* Add (or update) attribute to hash. */
2551 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2553 /* Create new route with its attribute. */
2554 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2555 parent_pi
->peer
, attr_new
, rn
);
2556 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2557 bgp_path_info_extra_get(pi
);
2558 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2559 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2560 bgp_path_info_add(rn
, pi
);
2562 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2563 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2564 bgp_unlock_node(rn
);
2567 /* The attribute has changed. */
2568 /* Add (or update) attribute to hash. */
2569 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2571 /* Restore route, if needed. */
2572 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2573 bgp_path_info_restore(rn
, pi
);
2575 /* Mark if nexthop has changed. */
2576 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2577 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2579 /* Unintern existing, set to new. */
2580 bgp_attr_unintern(&pi
->attr
);
2581 pi
->attr
= attr_new
;
2582 pi
->uptime
= bgp_clock();
2585 /* Perform route selection and update zebra, if required. */
2586 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2591 * Install route entry into the VRF routing table and invoke route selection.
2593 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2594 struct prefix_evpn
*evp
,
2595 struct bgp_path_info
*parent_pi
)
2597 struct bgp_node
*rn
;
2598 struct bgp_path_info
*pi
;
2600 struct attr
*attr_new
;
2603 struct prefix
*pp
= &p
;
2606 char buf
[PREFIX_STRLEN
];
2607 char buf1
[PREFIX_STRLEN
];
2609 memset(pp
, 0, sizeof(struct prefix
));
2610 ip_prefix_from_evpn_prefix(evp
, pp
);
2612 if (bgp_debug_zebra(NULL
)) {
2614 "import evpn prefix %s as ip prefix %s in vrf %s",
2615 prefix2str(evp
, buf
, sizeof(buf
)),
2616 prefix2str(pp
, buf1
, sizeof(buf
)),
2617 vrf_id_to_name(bgp_vrf
->vrf_id
));
2620 /* Create (or fetch) route within the VRF. */
2621 /* NOTE: There is no RD here. */
2622 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2624 safi
= SAFI_UNICAST
;
2625 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2626 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2628 safi
= SAFI_UNICAST
;
2629 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2633 /* EVPN routes currently only support a IPv4 next hop which corresponds
2634 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2635 * or prefix route, we have to convert the next hop to an IPv4-mapped
2636 * address for the rest of the code to flow through. In the case of IPv4,
2637 * make sure to set the flag for next hop attribute.
2639 bgp_attr_dup(&attr
, parent_pi
->attr
);
2641 evpn_convert_nexthop_to_ipv6(&attr
);
2643 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2645 /* Check if route entry is already present. */
2646 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2648 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2652 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, rn
);
2654 if (attrhash_cmp(pi
->attr
, &attr
)
2655 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2656 bgp_unlock_node(rn
);
2659 /* The attribute has changed. */
2660 /* Add (or update) attribute to hash. */
2661 attr_new
= bgp_attr_intern(&attr
);
2663 /* Restore route, if needed. */
2664 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2665 bgp_path_info_restore(rn
, pi
);
2667 /* Mark if nexthop has changed. */
2669 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2671 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2672 &attr_new
->mp_nexthop_global
)))
2673 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2675 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_ATTR_CHANGED
);
2676 /* Unintern existing, set to new. */
2677 bgp_attr_unintern(&pi
->attr
);
2678 pi
->attr
= attr_new
;
2679 pi
->uptime
= bgp_clock();
2682 bgp_aggregate_increment(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2684 /* Perform route selection and update zebra, if required. */
2685 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2687 /* Process for route leaking. */
2688 vpn_leak_from_vrf_update(bgp_get_default(), bgp_vrf
, pi
);
2690 bgp_unlock_node(rn
);
2696 * Install route entry into the VNI routing table and invoke route selection.
2698 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2699 struct prefix_evpn
*p
,
2700 struct bgp_path_info
*parent_pi
)
2702 struct bgp_node
*rn
;
2703 struct bgp_path_info
*pi
;
2704 struct attr
*attr_new
;
2707 /* Create (or fetch) route within the VNI. */
2708 /* NOTE: There is no RD here. */
2709 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2711 /* Check if route entry is already present. */
2712 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2714 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2718 /* Create an info */
2719 (void)bgp_create_evpn_bgp_path_info(parent_pi
, rn
);
2721 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2722 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2723 bgp_unlock_node(rn
);
2726 /* The attribute has changed. */
2727 /* Add (or update) attribute to hash. */
2728 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2730 /* Restore route, if needed. */
2731 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2732 bgp_path_info_restore(rn
, pi
);
2734 /* Mark if nexthop has changed. */
2735 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2736 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2738 /* Unintern existing, set to new. */
2739 bgp_attr_unintern(&pi
->attr
);
2740 pi
->attr
= attr_new
;
2741 pi
->uptime
= bgp_clock();
2744 /* Perform route selection and update zebra, if required. */
2745 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2747 bgp_unlock_node(rn
);
2752 /* Uninstall EVPN route entry from ES route table */
2753 static int uninstall_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2754 struct prefix_evpn
*p
,
2755 struct bgp_path_info
*parent_pi
)
2758 struct bgp_node
*rn
;
2759 struct bgp_path_info
*pi
;
2761 if (!es
->route_table
)
2764 /* Locate route within the ESI.
2765 * NOTE: There is no RD here.
2767 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2771 /* Find matching route entry. */
2772 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2774 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2780 /* Mark entry for deletion */
2781 bgp_path_info_delete(rn
, pi
);
2783 /* Perform route selection and update zebra, if required. */
2784 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2786 /* Unlock route node. */
2787 bgp_unlock_node(rn
);
2793 * Uninstall route entry from the VRF routing table and send message
2794 * to zebra, if appropriate.
2796 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2797 struct prefix_evpn
*evp
,
2798 struct bgp_path_info
*parent_pi
)
2800 struct bgp_node
*rn
;
2801 struct bgp_path_info
*pi
;
2804 struct prefix
*pp
= &p
;
2807 char buf
[PREFIX_STRLEN
];
2808 char buf1
[PREFIX_STRLEN
];
2810 memset(pp
, 0, sizeof(struct prefix
));
2811 ip_prefix_from_evpn_prefix(evp
, pp
);
2813 if (bgp_debug_zebra(NULL
)) {
2815 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2816 prefix2str(evp
, buf
, sizeof(buf
)),
2817 prefix2str(pp
, buf1
, sizeof(buf
)),
2818 vrf_id_to_name(bgp_vrf
->vrf_id
));
2821 /* Locate route within the VRF. */
2822 /* NOTE: There is no RD here. */
2823 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2825 safi
= SAFI_UNICAST
;
2826 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2829 safi
= SAFI_UNICAST
;
2830 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2836 /* Find matching route entry. */
2837 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2839 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2845 /* Process for route leaking. */
2846 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp_vrf
, pi
);
2848 bgp_aggregate_decrement(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2850 /* Mark entry for deletion */
2851 bgp_path_info_delete(rn
, pi
);
2853 /* Perform route selection and update zebra, if required. */
2854 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2856 /* Unlock route node. */
2857 bgp_unlock_node(rn
);
2863 * Uninstall route entry from the VNI routing table and send message
2864 * to zebra, if appropriate.
2866 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2867 struct prefix_evpn
*p
,
2868 struct bgp_path_info
*parent_pi
)
2870 struct bgp_node
*rn
;
2871 struct bgp_path_info
*pi
;
2874 /* Locate route within the VNI. */
2875 /* NOTE: There is no RD here. */
2876 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2880 /* Find matching route entry. */
2881 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2883 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2889 /* Mark entry for deletion */
2890 bgp_path_info_delete(rn
, pi
);
2892 /* Perform route selection and update zebra, if required. */
2893 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2895 /* Unlock route node. */
2896 bgp_unlock_node(rn
);
2902 * Given a prefix, see if it belongs to ES.
2904 static int is_prefix_matching_for_es(struct prefix_evpn
*p
,
2907 /* if not an ES route return false */
2908 if (p
->prefix
.route_type
!= BGP_EVPN_ES_ROUTE
)
2911 if (memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
)) == 0)
2918 * Given a route entry and a VRF, see if this route entry should be
2919 * imported into the VRF i.e., RTs match.
2921 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2922 struct bgp_path_info
*pi
)
2924 struct attr
*attr
= pi
->attr
;
2925 struct ecommunity
*ecom
;
2929 /* Route should have valid RT to be even considered. */
2930 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2933 ecom
= attr
->ecommunity
;
2934 if (!ecom
|| !ecom
->size
)
2937 /* For each extended community RT, see if it matches this VNI. If any RT
2938 * matches, we're done.
2940 for (i
= 0; i
< ecom
->size
; i
++) {
2942 uint8_t type
, sub_type
;
2943 struct ecommunity_val
*eval
;
2944 struct ecommunity_val eval_tmp
;
2945 struct vrf_irt_node
*irt
;
2947 /* Only deal with RTs */
2948 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2949 eval
= (struct ecommunity_val
*)(ecom
->val
2950 + (i
* ECOMMUNITY_SIZE
));
2953 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2956 /* See if this RT matches specified VNIs import RTs */
2957 irt
= lookup_vrf_import_rt(eval
);
2959 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2962 /* Also check for non-exact match. In this, we mask out the AS
2964 * only check on the local-admin sub-field. This is to
2966 * VNI as the RT for EBGP peering too.
2969 if (type
== ECOMMUNITY_ENCODE_AS
2970 || type
== ECOMMUNITY_ENCODE_AS4
2971 || type
== ECOMMUNITY_ENCODE_IP
) {
2972 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2973 mask_ecom_global_admin(&eval_tmp
, eval
);
2974 irt
= lookup_vrf_import_rt(&eval_tmp
);
2977 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2985 * Given a route entry and a VNI, see if this route entry should be
2986 * imported into the VNI i.e., RTs match.
2988 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2989 struct bgp_path_info
*pi
)
2991 struct attr
*attr
= pi
->attr
;
2992 struct ecommunity
*ecom
;
2996 /* Route should have valid RT to be even considered. */
2997 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3000 ecom
= attr
->ecommunity
;
3001 if (!ecom
|| !ecom
->size
)
3004 /* For each extended community RT, see if it matches this VNI. If any RT
3005 * matches, we're done.
3007 for (i
= 0; i
< ecom
->size
; i
++) {
3009 uint8_t type
, sub_type
;
3010 struct ecommunity_val
*eval
;
3011 struct ecommunity_val eval_tmp
;
3012 struct irt_node
*irt
;
3014 /* Only deal with RTs */
3015 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3016 eval
= (struct ecommunity_val
*)(ecom
->val
3017 + (i
* ECOMMUNITY_SIZE
));
3020 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3023 /* See if this RT matches specified VNIs import RTs */
3024 irt
= lookup_import_rt(bgp
, eval
);
3026 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
3029 /* Also check for non-exact match. In this, we mask out the AS
3031 * only check on the local-admin sub-field. This is to
3033 * VNI as the RT for EBGP peering too.
3036 if (type
== ECOMMUNITY_ENCODE_AS
3037 || type
== ECOMMUNITY_ENCODE_AS4
3038 || type
== ECOMMUNITY_ENCODE_IP
) {
3039 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3040 mask_ecom_global_admin(&eval_tmp
, eval
);
3041 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3044 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
3051 static int install_uninstall_routes_for_es(struct bgp
*bgp
,
3058 char buf
[PREFIX_STRLEN
];
3059 char buf1
[ESI_STR_LEN
];
3060 struct bgp_node
*rd_rn
, *rn
;
3061 struct bgp_table
*table
;
3062 struct bgp_path_info
*pi
;
3068 * Walk entire global routing table and evaluate routes which could be
3069 * imported into this VRF. Note that we need to loop through all global
3070 * routes to determine which route matches the import rt on vrf
3072 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
3073 rd_rn
= bgp_route_next(rd_rn
)) {
3074 table
= bgp_node_get_bgp_table_info(rd_rn
);
3078 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3079 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3081 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3084 * Consider "valid" remote routes applicable for
3087 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3088 && pi
->type
== ZEBRA_ROUTE_BGP
3089 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3092 if (!is_prefix_matching_for_es(evp
, es
))
3096 ret
= install_evpn_route_entry_in_es(
3099 ret
= uninstall_evpn_route_entry_in_es(
3105 "Failed to %s EVPN %s route in ESI %s",
3108 prefix2str(evp
, buf
,
3110 esi_to_str(&es
->esi
, buf1
,
3120 /* This API will scan evpn routes for checking attribute's rmac
3121 * macthes with bgp instance router mac. It avoid installing
3122 * route into bgp vrf table and remote rmac in bridge table.
3124 static int bgp_evpn_route_rmac_self_check(struct bgp
*bgp_vrf
,
3125 struct prefix_evpn
*evp
,
3126 struct bgp_path_info
*pi
)
3128 /* evpn route could have learnt prior to L3vni has come up,
3129 * perform rmac check before installing route and
3130 * remote router mac.
3131 * The route will be removed from global bgp table once
3132 * SVI comes up with MAC and stored in hash, triggers
3133 * bgp_mac_rescan_all_evpn_tables.
3135 if (memcmp(&bgp_vrf
->rmac
, &pi
->attr
->rmac
, ETH_ALEN
) == 0) {
3136 if (bgp_debug_update(pi
->peer
, NULL
, NULL
, 1)) {
3137 char buf1
[PREFIX_STRLEN
];
3138 char attr_str
[BUFSIZ
] = {0};
3140 bgp_dump_attr(pi
->attr
, attr_str
, BUFSIZ
);
3142 zlog_debug("%s: bgp %u prefix %s with attr %s - DENIED due to self mac",
3143 __func__
, bgp_vrf
->vrf_id
,
3144 prefix2str(evp
, buf1
, sizeof(buf1
)),
3155 * Install or uninstall mac-ip routes are appropriate for this
3158 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
3162 struct bgp_node
*rd_rn
, *rn
;
3163 struct bgp_table
*table
;
3164 struct bgp_path_info
*pi
;
3166 char buf
[PREFIX_STRLEN
];
3167 struct bgp
*bgp_evpn
= NULL
;
3171 bgp_evpn
= bgp_get_evpn();
3175 /* Walk entire global routing table and evaluate routes which could be
3176 * imported into this VRF. Note that we need to loop through all global
3177 * routes to determine which route matches the import rt on vrf
3179 for (rd_rn
= bgp_table_top(bgp_evpn
->rib
[afi
][safi
]); rd_rn
;
3180 rd_rn
= bgp_route_next(rd_rn
)) {
3181 table
= bgp_node_get_bgp_table_info(rd_rn
);
3185 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3186 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3188 /* if not mac-ip route skip this route */
3189 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3190 || evp
->prefix
.route_type
3191 == BGP_EVPN_IP_PREFIX_ROUTE
))
3194 /* if not a mac+ip route skip this route */
3195 if (!(is_evpn_prefix_ipaddr_v4(evp
)
3196 || is_evpn_prefix_ipaddr_v6(evp
)))
3199 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3201 /* Consider "valid" remote routes applicable for
3204 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3205 && pi
->type
== ZEBRA_ROUTE_BGP
3206 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3209 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
3210 if (bgp_evpn_route_rmac_self_check(
3215 ret
= install_evpn_route_entry_in_vrf(
3218 ret
= uninstall_evpn_route_entry_in_vrf(
3224 "Failed to %s EVPN %s route in VRF %s",
3227 prefix2str(evp
, buf
,
3242 * Install or uninstall routes of specified type that are appropriate for this
3245 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
3246 struct bgpevpn
*vpn
,
3247 bgp_evpn_route_type rtype
,
3252 struct bgp_node
*rd_rn
, *rn
;
3253 struct bgp_table
*table
;
3254 struct bgp_path_info
*pi
;
3260 /* Walk entire global routing table and evaluate routes which could be
3261 * imported into this VPN. Note that we cannot just look at the routes
3263 * the VNI's RD - remote routes applicable for this VNI could have any
3266 /* EVPN routes are a 2-level table. */
3267 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
3268 rd_rn
= bgp_route_next(rd_rn
)) {
3269 table
= bgp_node_get_bgp_table_info(rd_rn
);
3273 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3274 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3276 if (evp
->prefix
.route_type
!= rtype
)
3279 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3281 /* Consider "valid" remote routes applicable for
3283 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3284 && pi
->type
== ZEBRA_ROUTE_BGP
3285 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3288 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3290 ret
= install_evpn_route_entry(
3293 ret
= uninstall_evpn_route_entry(
3299 "%u: Failed to %s EVPN %s route in VNI %u",
3303 rtype
== BGP_EVPN_MAC_IP_ROUTE
3317 /* Install any existing remote ES routes applicable for this ES into its routing
3318 * table. This is invoked when ES comes up.
3320 static int install_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
3322 return install_uninstall_routes_for_es(bgp
, es
, 1);
3326 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3327 * is invoked upon l3vni-add or l3vni import rt change
3329 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3331 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3336 * Install any existing remote routes applicable for this VNI into its
3337 * routing table. This is invoked when a VNI becomes "live" or its Import
3340 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3344 /* Install type-3 routes followed by type-2 routes - the ones applicable
3347 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3352 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3356 /* uninstall routes from l3vni vrf. */
3357 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3359 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3364 * Uninstall any existing remote routes for this VNI. One scenario in which
3365 * this is invoked is upon an import RT change.
3367 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3371 /* Uninstall type-2 routes followed by type-3 routes - the ones
3375 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3380 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3384 /* Install or unistall route in ES */
3385 static int install_uninstall_route_in_es(struct bgp
*bgp
, struct evpnes
*es
,
3386 afi_t afi
, safi_t safi
,
3387 struct prefix_evpn
*evp
,
3388 struct bgp_path_info
*pi
, int install
)
3391 char buf
[ESI_STR_LEN
];
3394 ret
= install_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3396 ret
= uninstall_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3401 "%u: Failed to %s EVPN %s route in ESI %s", bgp
->vrf_id
,
3402 install
? "install" : "uninstall", "ES",
3403 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)));
3410 * Install or uninstall route in matching VRFs (list).
3412 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3413 safi_t safi
, struct prefix_evpn
*evp
,
3414 struct bgp_path_info
*pi
,
3415 struct list
*vrfs
, int install
)
3417 char buf
[PREFIX2STR_BUFFER
];
3418 struct bgp
*bgp_vrf
;
3419 struct listnode
*node
, *nnode
;
3421 /* Only type-2/type-5 routes go into a VRF */
3422 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3423 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3426 /* if it is type-2 route and not a mac+ip route skip this route */
3427 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3428 && !(is_evpn_prefix_ipaddr_v4(evp
)
3429 || is_evpn_prefix_ipaddr_v6(evp
)))
3432 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3436 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3438 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3442 flog_err(EC_BGP_EVPN_FAIL
,
3443 "%u: Failed to %s prefix %s in VRF %s",
3445 install
? "install" : "uninstall",
3446 prefix2str(evp
, buf
, sizeof(buf
)),
3447 vrf_id_to_name(bgp_vrf
->vrf_id
));
3456 * Install or uninstall route in matching VNIs (list).
3458 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3459 safi_t safi
, struct prefix_evpn
*evp
,
3460 struct bgp_path_info
*pi
,
3461 struct list
*vnis
, int install
)
3463 struct bgpevpn
*vpn
;
3464 struct listnode
*node
, *nnode
;
3466 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3469 if (!is_vni_live(vpn
))
3473 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3475 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3478 flog_err(EC_BGP_EVPN_FAIL
,
3479 "%u: Failed to %s EVPN %s route in VNI %u",
3480 bgp
->vrf_id
, install
? "install" : "uninstall",
3481 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3493 * Install or uninstall route for appropriate VNIs/ESIs.
3495 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3497 struct bgp_path_info
*pi
, int import
)
3499 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3500 struct attr
*attr
= pi
->attr
;
3501 struct ecommunity
*ecom
;
3506 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3507 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3508 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3509 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3510 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3513 /* If we don't have Route Target, nothing much to do. */
3514 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3517 ecom
= attr
->ecommunity
;
3518 if (!ecom
|| !ecom
->size
)
3521 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3522 * attached to the route */
3523 for (i
= 0; i
< ecom
->size
; i
++) {
3525 uint8_t type
, sub_type
;
3526 struct ecommunity_val
*eval
;
3527 struct ecommunity_val eval_tmp
;
3528 struct irt_node
*irt
; /* import rt for l2vni */
3529 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3532 /* Only deal with RTs */
3533 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3534 eval
= (struct ecommunity_val
*)(ecom
->val
3535 + (i
* ECOMMUNITY_SIZE
));
3538 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3542 * macip routes (type-2) are imported into VNI and VRF tables.
3543 * IMET route is imported into VNI table.
3544 * prefix routes are imported into VRF table.
3546 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3547 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3548 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3550 irt
= lookup_import_rt(bgp
, eval
);
3552 install_uninstall_route_in_vnis(
3553 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3556 vrf_irt
= lookup_vrf_import_rt(eval
);
3558 install_uninstall_route_in_vrfs(
3559 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3562 /* Also check for non-exact match.
3563 * In this, we mask out the AS and
3564 * only check on the local-admin sub-field.
3565 * This is to facilitate using
3566 * VNI as the RT for EBGP peering too.
3570 if (type
== ECOMMUNITY_ENCODE_AS
3571 || type
== ECOMMUNITY_ENCODE_AS4
3572 || type
== ECOMMUNITY_ENCODE_IP
) {
3573 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3574 mask_ecom_global_admin(&eval_tmp
, eval
);
3575 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3576 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3580 install_uninstall_route_in_vnis(
3581 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3584 install_uninstall_route_in_vrfs(
3585 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3589 /* es route is imported into the es table */
3590 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3592 /* we will match based on the entire esi to avoid
3593 * imoort of an es route for esi2 into esi1
3595 es
= bgp_evpn_lookup_es(bgp
, &evp
->prefix
.es_addr
.esi
);
3596 if (es
&& is_es_local(es
))
3597 install_uninstall_route_in_es(
3598 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3606 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3609 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3611 /* Delete ipv4 default route and withdraw from peers */
3612 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3613 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3614 SAFI_UNICAST
, false);
3616 /* delete all ipv4 routes and withdraw from peers */
3617 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3618 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3620 /* Delete ipv6 default route and withdraw from peers */
3621 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3622 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3623 SAFI_UNICAST
, false);
3625 /* delete all ipv6 routes and withdraw from peers */
3626 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3627 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3631 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3634 void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3636 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
3638 bgp_evpn
= bgp_get_evpn();
3642 /* update all ipv4 routes */
3643 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3644 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3646 /* update ipv4 default route and withdraw from peers */
3647 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3648 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3649 SAFI_UNICAST
, true);
3651 /* update all ipv6 routes */
3652 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3653 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3655 /* update ipv6 default route and withdraw from peers */
3656 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3657 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3658 SAFI_UNICAST
, true);
3663 * update and advertise local routes for a VRF as type-5 routes.
3664 * This is invoked upon RD change for a VRF. Note taht the processing is only
3665 * done in the global route table using the routes which already exist in the
3668 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3670 /* skip if the RD is configured */
3671 if (is_vrf_rd_configured(bgp_vrf
))
3674 /* derive the RD for the VRF based on new router-id */
3675 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3677 /* update advertise ipv4|ipv6 routes as type-5 routes */
3678 update_advertise_vrf_routes(bgp_vrf
);
3682 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3683 * This is invoked upon VRF RD change. The processing is done only from global
3686 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3688 /* skip if the RD is configured */
3689 if (is_vrf_rd_configured(bgp_vrf
))
3692 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3693 delete_withdraw_vrf_routes(bgp_vrf
);
3697 * Update and advertise local routes for a VNI. Invoked upon router-id
3698 * change. Note that the processing is done only on the global route table
3699 * using routes that already exist in the per-VNI table.
3701 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3703 struct prefix_evpn p
;
3704 struct bgp_node
*rn
, *global_rn
;
3705 struct bgp_path_info
*pi
, *global_pi
;
3707 afi_t afi
= AFI_L2VPN
;
3708 safi_t safi
= SAFI_EVPN
;
3710 /* Locate type-3 route for VNI in the per-VNI table and use its
3711 * attributes to create and advertise the type-3 route for this VNI
3712 * in the global table.
3714 * RT-3 only if doing head-end replication
3716 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3717 == VXLAN_FLOOD_HEAD_END_REPL
) {
3718 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3719 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3720 if (!rn
) /* unexpected */
3722 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3723 if (pi
->peer
== bgp
->peer_self
&&
3724 pi
->type
== ZEBRA_ROUTE_BGP
3725 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3727 if (!pi
) /* unexpected */
3731 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3732 (struct prefix
*)&p
, &vpn
->prd
);
3733 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
,
3734 1, &pi
, 0, mac_mobility_seqnum(attr
));
3736 /* Schedule for processing and unlock node. */
3737 bgp_process(bgp
, global_rn
, afi
, safi
);
3738 bgp_unlock_node(global_rn
);
3741 /* Now, walk this VNI's route table and use the route and its attribute
3742 * to create and schedule route in global table.
3744 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
3745 rn
= bgp_route_next(rn
)) {
3746 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3748 /* Identify MAC-IP local routes. */
3749 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3752 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3753 if (pi
->peer
== bgp
->peer_self
3754 && pi
->type
== ZEBRA_ROUTE_BGP
3755 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3760 /* Create route in global routing table using this route entry's
3764 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3765 (struct prefix
*)evp
, &vpn
->prd
);
3767 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
3769 mac_mobility_seqnum(attr
));
3771 /* Schedule for processing and unlock node. */
3772 bgp_process(bgp
, global_rn
, afi
, safi
);
3773 bgp_unlock_node(global_rn
);
3780 * Delete (and withdraw) local routes for a VNI - only from the global
3781 * table. Invoked upon router-id change.
3783 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3786 struct prefix_evpn p
;
3787 struct bgp_node
*global_rn
;
3788 struct bgp_path_info
*pi
;
3789 afi_t afi
= AFI_L2VPN
;
3790 safi_t safi
= SAFI_EVPN
;
3792 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3793 * for this VNI - from the global table.
3795 ret
= delete_global_type2_routes(bgp
, vpn
);
3799 /* Remove type-3 route for this VNI from global table. */
3800 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3801 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3802 (struct prefix
*)&p
, &vpn
->prd
);
3804 /* Delete route entry in the global EVPN table. */
3805 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
3807 /* Schedule for processing - withdraws to peers happen from
3811 bgp_process(bgp
, global_rn
, afi
, safi
);
3812 bgp_unlock_node(global_rn
);
3819 * Handle router-id change. Update and advertise local routes corresponding
3820 * to this VNI from peers. Note that this is invoked after updating the
3821 * router-id. The routes in the per-VNI table are used to create routes in
3822 * the global table and schedule them.
3824 static void update_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3826 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3828 /* Skip VNIs with configured RD. */
3829 if (is_rd_configured(vpn
))
3832 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3833 update_advertise_vni_routes(bgp
, vpn
);
3837 * Handle router-id change. Delete and withdraw local routes corresponding
3838 * to this VNI from peers. Note that this is invoked prior to updating
3839 * the router-id and is done only on the global route table, the routes
3840 * are needed in the per-VNI table to re-advertise with new router id.
3842 static void withdraw_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3844 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3846 /* Skip VNIs with configured RD. */
3847 if (is_rd_configured(vpn
))
3850 delete_withdraw_vni_routes(bgp
, vpn
);
3854 * Create RT-3 for a VNI and schedule for processing and advertisement.
3855 * This is invoked upon flooding mode changing to head-end replication.
3857 static void create_advertise_type3(struct hash_bucket
*bucket
, void *data
)
3859 struct bgpevpn
*vpn
= bucket
->data
;
3860 struct bgp
*bgp
= data
;
3861 struct prefix_evpn p
;
3863 if (!vpn
|| !is_vni_live(vpn
) ||
3864 bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3865 != VXLAN_FLOOD_HEAD_END_REPL
)
3868 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3869 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0))
3870 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3871 "Type3 route creation failure for VNI %u", vpn
->vni
);
3875 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3876 * This is invoked upon flooding mode changing to drop BUM packets.
3878 static void delete_withdraw_type3(struct hash_bucket
*bucket
, void *data
)
3880 struct bgpevpn
*vpn
= bucket
->data
;
3881 struct bgp
*bgp
= data
;
3882 struct prefix_evpn p
;
3884 if (!vpn
|| !is_vni_live(vpn
))
3887 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3888 delete_evpn_route(bgp
, vpn
, &p
);
3892 * Process received EVPN type-2 route (advertise or withdraw).
3894 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3895 struct attr
*attr
, uint8_t *pfx
, int psize
,
3896 uint32_t addpath_id
)
3898 struct prefix_rd prd
;
3899 struct prefix_evpn p
;
3900 struct bgp_route_evpn evpn
;
3902 uint8_t macaddr_len
;
3903 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
3904 uint32_t num_labels
= 0;
3908 /* Type-2 route should be either 33, 37 or 49 bytes or an
3909 * additional 3 bytes if there is a second label (VNI):
3910 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3911 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3912 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3914 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3915 && psize
!= 40 && psize
!= 52) {
3916 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3917 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3918 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3922 memset(&evpn
, 0, sizeof(evpn
));
3924 /* Make prefix_rd */
3925 prd
.family
= AF_UNSPEC
;
3927 memcpy(&prd
.val
, pfx
, 8);
3930 /* Make EVPN prefix. */
3931 memset(&p
, 0, sizeof(struct prefix_evpn
));
3933 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3934 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3936 /* Copy Ethernet Seg Identifier */
3937 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
3940 /* Copy Ethernet Tag */
3941 memcpy(ð_tag
, pfx
, 4);
3942 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3945 /* Get the MAC Addr len */
3946 macaddr_len
= *pfx
++;
3948 /* Get the MAC Addr */
3949 if (macaddr_len
== (ETH_ALEN
* 8)) {
3950 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
3954 EC_BGP_EVPN_ROUTE_INVALID
,
3955 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3956 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3962 ipaddr_len
= *pfx
++;
3963 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3964 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3966 EC_BGP_EVPN_ROUTE_INVALID
,
3967 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3968 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3973 ipaddr_len
/= 8; /* Convert to bytes. */
3974 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3977 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
3981 /* Get the VNI(s). Stored as bytes here. */
3983 memset(label
, 0, sizeof(label
));
3984 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
3985 pfx
+= BGP_LABEL_BYTES
;
3986 psize
-= (33 + ipaddr_len
);
3987 /* Do we have a second VNI? */
3990 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
3992 * If in future, we are required to access additional fields,
3993 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
3998 /* Process the route. */
4000 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4001 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4002 &prd
, &label
[0], num_labels
, 0, &evpn
);
4004 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4005 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4006 &prd
, &label
[0], num_labels
, &evpn
);
4011 * Process received EVPN type-3 route (advertise or withdraw).
4013 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4014 struct attr
*attr
, uint8_t *pfx
, int psize
,
4015 uint32_t addpath_id
)
4017 struct prefix_rd prd
;
4018 struct prefix_evpn p
;
4023 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
4024 * IP len (1) and IP (4 or 16).
4026 if (psize
!= 17 && psize
!= 29) {
4027 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4028 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
4029 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4033 /* If PMSI is present, log if it is anything other than IR.
4034 * Note: We just simply ignore the values as it is not clear if
4035 * doing anything else is better.
4038 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
4039 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
&&
4040 attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_PIM_SM
) {
4041 flog_warn(EC_BGP_EVPN_PMSI_PRESENT
,
4042 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
4043 peer
->bgp
->vrf_id
, peer
->host
,
4044 attr
->pmsi_tnl_type
);
4048 /* Make prefix_rd */
4049 prd
.family
= AF_UNSPEC
;
4051 memcpy(&prd
.val
, pfx
, 8);
4054 /* Make EVPN prefix. */
4055 memset(&p
, 0, sizeof(struct prefix_evpn
));
4057 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4058 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
4060 /* Copy Ethernet Tag */
4061 memcpy(ð_tag
, pfx
, 4);
4062 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
4066 ipaddr_len
= *pfx
++;
4067 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
4068 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
4069 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
4072 EC_BGP_EVPN_ROUTE_INVALID
,
4073 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
4074 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4078 /* Process the route. */
4080 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4081 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4082 &prd
, NULL
, 0, 0, NULL
);
4084 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4085 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4086 &prd
, NULL
, 0, NULL
);
4091 * Process received EVPN type-4 route (advertise or withdraw).
4093 static int process_type4_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4094 struct attr
*attr
, uint8_t *pfx
, int psize
,
4095 uint32_t addpath_id
)
4100 struct in_addr vtep_ip
;
4101 struct prefix_rd prd
;
4102 struct prefix_evpn p
;
4104 /* Type-4 route should be either 23 or 35 bytes
4105 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
4107 if (psize
!= 23 && psize
!= 35) {
4108 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4109 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
4110 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4114 /* Make prefix_rd */
4115 prd
.family
= AF_UNSPEC
;
4117 memcpy(&prd
.val
, pfx
, 8);
4121 memcpy(&esi
, pfx
, ESI_BYTES
);
4126 ipaddr_len
= *pfx
++;
4127 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
4128 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
4131 EC_BGP_EVPN_ROUTE_INVALID
,
4132 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
4133 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4137 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
4138 /* Process the route. */
4140 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4141 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4142 &prd
, NULL
, 0, 0, NULL
);
4144 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4145 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4146 &prd
, NULL
, 0, NULL
);
4153 * Process received EVPN type-5 route (advertise or withdraw).
4155 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4156 struct attr
*attr
, uint8_t *pfx
, int psize
,
4157 uint32_t addpath_id
)
4159 struct prefix_rd prd
;
4160 struct prefix_evpn p
;
4161 struct bgp_route_evpn evpn
;
4164 mpls_label_t label
; /* holds the VNI as in the packet */
4167 /* Type-5 route should be 34 or 58 bytes:
4168 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
4169 * GW (4 or 16) and VNI (3).
4170 * Note that the IP and GW should both be IPv4 or both IPv6.
4172 if (psize
!= 34 && psize
!= 58) {
4173 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4174 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
4175 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4179 /* Make prefix_rd */
4180 prd
.family
= AF_UNSPEC
;
4182 memcpy(&prd
.val
, pfx
, 8);
4185 /* Make EVPN prefix. */
4186 memset(&p
, 0, sizeof(struct prefix_evpn
));
4188 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4189 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
4191 /* Additional information outside of prefix - ESI and GW IP */
4192 memset(&evpn
, 0, sizeof(evpn
));
4195 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
4198 /* Fetch Ethernet Tag. */
4199 memcpy(ð_tag
, pfx
, 4);
4200 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
4203 /* Fetch IP prefix length. */
4205 if (ippfx_len
> IPV6_MAX_BITLEN
) {
4207 EC_BGP_EVPN_ROUTE_INVALID
,
4208 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4209 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
4212 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
4214 /* Determine IPv4 or IPv6 prefix */
4215 /* Since the address and GW are from the same family, this just becomes
4216 * a simple check on the total size.
4219 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
4220 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
4222 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
4225 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
4226 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
4228 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
4232 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4233 memset(&label
, 0, sizeof(label
));
4234 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
4237 * If in future, we are required to access additional fields,
4238 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4242 /* Process the route. */
4244 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4245 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4246 &prd
, &label
, 1, 0, &evpn
);
4248 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4249 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4250 &prd
, &label
, 1, &evpn
);
4255 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
4256 struct prefix_rd
*prd
, mpls_label_t
*label
,
4257 uint32_t num_labels
, struct attr
*attr
)
4261 struct evpn_addr
*p_evpn_p
;
4263 memset(&temp
, 0, 16);
4264 if (p
->family
!= AF_EVPN
)
4266 p_evpn_p
= &(p
->u
.prefix_evpn
);
4268 /* len denites the total len of IP and GW-IP in the route
4269 IP and GW-IP have to be both ipv4 or ipv6
4271 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4272 len
= 8; /* IP and GWIP are both ipv4 */
4274 len
= 32; /* IP and GWIP are both ipv6 */
4275 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4276 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4277 stream_put(s
, prd
->val
, 8);
4279 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
4281 stream_put(s
, &temp
, 10);
4282 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4283 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4284 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4285 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4287 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4289 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4291 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
4293 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
4295 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4296 stream_put_ipv4(s
, 0);
4298 stream_put(s
, &temp
, 16);
4302 stream_put(s
, label
, 3);
4308 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4310 static void cleanup_vni_on_disable(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4312 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4314 /* Remove EVPN routes and schedule for processing. */
4315 delete_routes_for_vni(bgp
, vpn
);
4317 /* Clear "live" flag and see if hash needs to be freed. */
4318 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4319 if (!is_vni_configured(vpn
))
4320 bgp_evpn_free(bgp
, vpn
);
4324 * Free a VNI entry; iterator function called during cleanup.
4326 static void free_vni_entry(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4328 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4330 delete_all_vni_routes(bgp
, vpn
);
4331 bgp_evpn_free(bgp
, vpn
);
4335 * Derive AUTO import RT for BGP VRF - L3VNI
4337 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4339 struct bgp
*bgp_evpn
= NULL
;
4341 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4342 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4345 bgp_evpn
= bgp_get_evpn();
4348 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4352 * Delete AUTO import RT from BGP VRF - L3VNI
4354 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4356 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4360 * Derive AUTO export RT for BGP VRF - L3VNI
4362 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4364 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4365 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4369 * Delete AUTO export RT from BGP VRF - L3VNI
4371 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4373 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4376 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4378 struct bgp
*bgp_evpn
= NULL
;
4379 struct listnode
*node
= NULL
;
4380 struct bgpevpn
*vpn
= NULL
;
4382 bgp_evpn
= bgp_get_evpn();
4386 /* update all type-5 routes */
4387 update_advertise_vrf_routes(bgp_vrf
);
4389 /* update all type-2 routes */
4390 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4391 update_routes_for_vni(bgp_evpn
, vpn
);
4395 * Handle autort change for a given VNI.
4397 static void update_autort_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4399 struct bgpevpn
*vpn
= bucket
->data
;
4401 if (!is_import_rt_configured(vpn
)) {
4402 if (is_vni_live(vpn
))
4403 bgp_evpn_uninstall_routes(bgp
, vpn
);
4404 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4405 list_delete_all_node(vpn
->import_rtl
);
4406 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4407 if (is_vni_live(vpn
))
4408 bgp_evpn_install_routes(bgp
, vpn
);
4410 if (!is_export_rt_configured(vpn
)) {
4411 list_delete_all_node(vpn
->export_rtl
);
4412 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4413 if (is_vni_live(vpn
))
4414 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4422 /* withdraw type-5 route corresponding to ip prefix */
4423 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4424 afi_t afi
, safi_t safi
)
4427 struct prefix_evpn evp
;
4428 char buf
[PREFIX_STRLEN
];
4430 build_type5_prefix_from_ip_prefix(&evp
, p
);
4431 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4434 EC_BGP_EVPN_ROUTE_DELETE
,
4435 "%u failed to delete type-5 route for prefix %s in vrf %s",
4436 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4437 vrf_id_to_name(bgp_vrf
->vrf_id
));
4441 /* withdraw all type-5 routes for an address family */
4442 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4444 struct bgp_table
*table
= NULL
;
4445 struct bgp_node
*rn
= NULL
;
4446 struct bgp_path_info
*pi
;
4448 table
= bgp_vrf
->rib
[afi
][safi
];
4449 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4450 /* Only care about "selected" routes. Also ensure that
4451 * these are routes that are injectable into EVPN.
4453 /* TODO: Support for AddPath for EVPN. */
4454 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4455 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4456 && is_route_injectable_into_evpn(pi
)) {
4457 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
4466 * evpn - enable advertisement of default g/w
4468 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
4469 safi_t safi
, bool add
)
4471 struct prefix ip_prefix
;
4473 /* form the default prefix 0.0.0.0/0 */
4474 memset(&ip_prefix
, 0, sizeof(struct prefix
));
4475 ip_prefix
.family
= afi2family(afi
);
4478 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
4481 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
4488 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4489 * to this function correspond to those of the source IP prefix (best
4490 * path in the case of the attr. In the case of a local prefix (when we
4491 * are advertising local subnets), the src_attr will be NULL.
4493 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4494 struct attr
*src_attr
, afi_t afi
,
4498 struct prefix_evpn evp
;
4499 char buf
[PREFIX_STRLEN
];
4501 build_type5_prefix_from_ip_prefix(&evp
, p
);
4502 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4504 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4505 "%u: Failed to create type-5 route for prefix %s",
4506 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4509 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4510 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4511 * advertisement is enabled.
4513 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4516 struct bgp_table
*table
= NULL
;
4517 struct bgp_node
*rn
= NULL
;
4518 struct bgp_path_info
*pi
;
4520 table
= bgp_vrf
->rib
[afi
][safi
];
4521 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4522 /* Need to identify the "selected" route entry to use its
4523 * attribute. Also, ensure that the route is injectable
4525 * TODO: Support for AddPath for EVPN.
4527 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4528 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4529 && is_route_injectable_into_evpn(pi
)) {
4531 /* apply the route-map */
4532 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4533 route_map_result_t ret
;
4535 ret
= route_map_apply(
4536 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4538 &rn
->p
, RMAP_BGP
, pi
);
4539 if (ret
== RMAP_DENYMATCH
)
4542 bgp_evpn_advertise_type5_route(
4543 bgp_vrf
, &rn
->p
, pi
->attr
, afi
, safi
);
4550 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4552 struct listnode
*node
, *nnode
, *node_to_del
;
4553 struct ecommunity
*ecom
, *ecom_auto
;
4554 struct ecommunity_val eval
;
4556 if (bgp
->advertise_autort_rfc8365
)
4557 vni
|= EVPN_AUTORT_VXLAN
;
4558 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4560 ecom_auto
= ecommunity_new();
4561 ecommunity_add_val(ecom_auto
, &eval
);
4564 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4565 if (ecommunity_match(ecom
, ecom_auto
)) {
4566 ecommunity_free(&ecom
);
4572 list_delete_node(rtl
, node_to_del
);
4574 ecommunity_free(&ecom_auto
);
4577 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4578 struct ecommunity
*ecomadd
)
4580 /* uninstall routes from vrf */
4581 if (is_l3vni_live(bgp_vrf
))
4582 uninstall_routes_for_vrf(bgp_vrf
);
4584 /* Cleanup the RT to VRF mapping */
4585 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4587 /* Remove auto generated RT */
4588 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4590 /* Add the newly configured RT to RT list */
4591 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4592 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4594 /* map VRF to its RTs and install routes matching the new RTs */
4595 if (is_l3vni_live(bgp_vrf
)) {
4596 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4597 install_routes_for_vrf(bgp_vrf
);
4601 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4602 struct ecommunity
*ecomdel
)
4604 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4605 struct ecommunity
*ecom
= NULL
;
4607 /* uninstall routes from vrf */
4608 if (is_l3vni_live(bgp_vrf
))
4609 uninstall_routes_for_vrf(bgp_vrf
);
4611 /* Cleanup the RT to VRF mapping */
4612 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4614 /* remove the RT from the RT list */
4615 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4616 if (ecommunity_match(ecom
, ecomdel
)) {
4617 ecommunity_free(&ecom
);
4624 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4626 assert(bgp_vrf
->vrf_import_rtl
);
4627 /* fallback to auto import rt, if this was the last RT */
4628 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4629 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4630 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4633 /* map VRFs to its RTs and install routes matching this new RT */
4634 if (is_l3vni_live(bgp_vrf
)) {
4635 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4636 install_routes_for_vrf(bgp_vrf
);
4640 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4641 struct ecommunity
*ecomadd
)
4643 /* remove auto-generated RT */
4644 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4646 /* Add the new RT to the RT list */
4647 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4648 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4650 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4653 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4654 struct ecommunity
*ecomdel
)
4656 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4657 struct ecommunity
*ecom
= NULL
;
4659 /* Remove the RT from the RT list */
4660 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4661 if (ecommunity_match(ecom
, ecomdel
)) {
4662 ecommunity_free(&ecom
);
4669 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4672 * Temporary assert to make SA happy.
4673 * The ALL_LIST_ELEMENTS macro above has a NULL check
4674 * which means that SA is going to complain about
4675 * the list_isempty call, which doesn't NULL check.
4676 * So until we get this situation cleaned up, here
4679 assert(bgp_vrf
->vrf_export_rtl
);
4681 /* fall back to auto-generated RT if this was the last RT */
4682 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4683 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4684 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4687 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4691 * Handle change to BGP router id. This is invoked twice by the change
4692 * handler, first before the router id has been changed and then after
4693 * the router id has been changed. The first invocation will result in
4694 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4695 * will result in the routes being re-advertised.
4697 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4699 struct listnode
*node
;
4700 struct bgp
*bgp_vrf
;
4704 /* delete and withdraw all the type-5 routes
4705 stored in the global table for this vrf
4707 withdraw_router_id_vrf(bgp
);
4709 /* delete all the VNI routes (type-2/type-3) routes for all the
4712 hash_iterate(bgp
->vnihash
,
4713 (void (*)(struct hash_bucket
*,
4714 void *))withdraw_router_id_vni
,
4717 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4718 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4719 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4720 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4722 bgp_vrf
->evpn_info
->pip_ip
.s_addr
4728 /* Assign new default instance router-id */
4729 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4730 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4731 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4732 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4734 bgp_vrf
->evpn_info
->pip_ip
=
4736 /* advertise type-5 routes with
4739 update_advertise_vrf_routes(bgp_vrf
);
4744 /* advertise all routes in the vrf as type-5 routes with the new
4747 update_router_id_vrf(bgp
);
4749 /* advertise all the VNI routes (type-2/type-3) routes with the
4752 hash_iterate(bgp
->vnihash
,
4753 (void (*)(struct hash_bucket
*,
4754 void *))update_router_id_vni
,
4760 * Handle change to auto-RT algorithm - update and advertise local routes.
4762 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4764 hash_iterate(bgp
->vnihash
,
4765 (void (*)(struct hash_bucket
*,
4766 void*))update_autort_vni
,
4771 * Handle change to export RT - update and advertise local routes.
4773 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4775 return update_routes_for_vni(bgp
, vpn
);
4778 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4781 delete_withdraw_vrf_routes(bgp_vrf
);
4783 update_advertise_vrf_routes(bgp_vrf
);
4787 * Handle change to RD. This is invoked twice by the change handler,
4788 * first before the RD has been changed and then after the RD has
4789 * been changed. The first invocation will result in local routes
4790 * of this VNI being deleted and withdrawn and the next will result
4791 * in the routes being re-advertised.
4793 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4797 delete_withdraw_vni_routes(bgp
, vpn
);
4799 update_advertise_vni_routes(bgp
, vpn
);
4803 * Install routes for this VNI. Invoked upon change to Import RT.
4805 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4807 return install_routes_for_vni(bgp
, vpn
);
4811 * Uninstall all routes installed for this VNI. Invoked upon change
4814 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4816 return uninstall_routes_for_vni(bgp
, vpn
);
4820 * TODO: Hardcoded for a maximum of 2 VNIs right now
4822 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4827 vni1
= label2vni(label
);
4828 if (num_labels
== 2) {
4829 vni2
= label2vni(label
+ 1);
4830 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4832 snprintf(buf
, len
, "%u", vni1
);
4837 * Function to convert evpn route to json format.
4838 * NOTE: We don't use prefix2str as the output here is a bit different.
4840 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
4842 char buf1
[ETHER_ADDR_STRLEN
];
4843 char buf2
[PREFIX2STR_BUFFER
];
4850 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4852 switch (p
->prefix
.route_type
) {
4853 case BGP_EVPN_MAC_IP_ROUTE
:
4854 json_object_int_add(json
, "ethTag",
4855 p
->prefix
.macip_addr
.eth_tag
);
4856 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4857 json_object_string_add(json
, "mac",
4858 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4861 if (!is_evpn_prefix_ipaddr_none(p
)) {
4862 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
:
4864 prefixlen
= (family
== AF_INET
) ?
4865 IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
;
4866 inet_ntop(family
, &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4867 buf2
, PREFIX2STR_BUFFER
);
4868 json_object_int_add(json
, "ipLen", prefixlen
);
4869 json_object_string_add(json
, "ip", buf2
);
4873 case BGP_EVPN_IMET_ROUTE
:
4874 json_object_int_add(json
, "ethTag",
4875 p
->prefix
.imet_addr
.eth_tag
);
4876 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4877 prefixlen
= (family
== AF_INET
) ? IPV4_MAX_BITLEN
:
4879 inet_ntop(family
, &p
->prefix
.imet_addr
.ip
.ip
.addr
, buf2
,
4881 json_object_int_add(json
, "ipLen", prefixlen
);
4882 json_object_string_add(json
, "ip", buf2
);
4885 case BGP_EVPN_IP_PREFIX_ROUTE
:
4886 json_object_int_add(json
, "ethTag",
4887 p
->prefix
.prefix_addr
.eth_tag
);
4888 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4889 inet_ntop(family
, &p
->prefix
.prefix_addr
.ip
.ip
.addr
,
4890 buf2
, sizeof(buf2
));
4891 json_object_int_add(json
, "ipLen",
4892 p
->prefix
.prefix_addr
.ip_prefix_length
);
4893 json_object_string_add(json
, "ip", buf2
);
4902 * Function to convert evpn route to string.
4903 * NOTE: We don't use prefix2str as the output here is a bit different.
4905 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
4907 char buf1
[ETHER_ADDR_STRLEN
];
4908 char buf2
[PREFIX2STR_BUFFER
];
4909 char buf3
[ESI_STR_LEN
];
4911 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4912 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
4913 p
->prefix
.imet_addr
.eth_tag
,
4914 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4916 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4917 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4918 if (is_evpn_prefix_ipaddr_none(p
))
4919 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4920 p
->prefix
.route_type
,
4921 p
->prefix
.macip_addr
.eth_tag
,
4923 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4928 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4930 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
4931 p
->prefix
.route_type
,
4932 p
->prefix
.macip_addr
.eth_tag
,
4934 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4936 family
== AF_INET
? IPV4_MAX_BITLEN
4939 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4941 PREFIX2STR_BUFFER
));
4943 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
4944 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4945 p
->prefix
.route_type
,
4946 p
->prefix
.prefix_addr
.eth_tag
,
4947 p
->prefix
.prefix_addr
.ip_prefix_length
,
4948 is_evpn_prefix_ipaddr_v4(p
)
4949 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
4950 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
4951 } else if (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
4952 snprintf(buf
, len
, "[%d]:[%s]:[%d]:[%s]",
4953 p
->prefix
.route_type
,
4954 esi_to_str(&p
->prefix
.es_addr
.esi
, buf3
, sizeof(buf3
)),
4955 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4957 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
));
4959 /* For EVPN route types not supported yet. */
4960 snprintf(buf
, len
, "(unsupported route type %d)",
4961 p
->prefix
.route_type
);
4968 * Encode EVPN prefix in Update (MP_REACH)
4970 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
4971 struct prefix_rd
*prd
, mpls_label_t
*label
,
4972 uint32_t num_labels
, struct attr
*attr
,
4973 int addpath_encode
, uint32_t addpath_tx_id
)
4975 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4976 int len
, ipa_len
= 0;
4979 stream_putl(s
, addpath_tx_id
);
4982 stream_putc(s
, evp
->prefix
.route_type
);
4984 switch (evp
->prefix
.route_type
) {
4985 case BGP_EVPN_MAC_IP_ROUTE
:
4986 if (is_evpn_prefix_ipaddr_v4(evp
))
4987 ipa_len
= IPV4_MAX_BYTELEN
;
4988 else if (is_evpn_prefix_ipaddr_v6(evp
))
4989 ipa_len
= IPV6_MAX_BYTELEN
;
4990 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4991 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4992 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4994 stream_putc(s
, len
);
4995 stream_put(s
, prd
->val
, 8); /* RD */
4997 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
4999 stream_put(s
, 0, 10);
5000 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
5001 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
5002 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
5003 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
5004 if (ipa_len
) /* IP */
5005 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
5007 /* 1st label is the L2 VNI */
5008 stream_put(s
, label
, BGP_LABEL_BYTES
);
5009 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
5010 if (ipa_len
&& num_labels
> 1)
5011 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
5014 case BGP_EVPN_IMET_ROUTE
:
5015 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
5016 stream_put(s
, prd
->val
, 8); /* RD */
5017 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
5018 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
5019 /* Originating Router's IP Addr */
5020 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
5023 case BGP_EVPN_ES_ROUTE
:
5024 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
5025 stream_put(s
, prd
->val
, 8); /* RD */
5026 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
5027 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
5029 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
5032 case BGP_EVPN_IP_PREFIX_ROUTE
:
5033 /* TODO: AddPath support. */
5034 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
5042 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
5043 struct bgp_nlri
*packet
, int withdraw
)
5049 uint32_t addpath_id
;
5050 int addpath_encoded
;
5055 /* Start processing the NLRI - there may be multiple in the MP_REACH */
5057 lim
= pnt
+ packet
->length
;
5059 safi
= packet
->safi
;
5063 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
5064 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5065 PEER_CAP_ADDPATH_AF_TX_RCV
));
5067 for (; pnt
< lim
; pnt
+= psize
) {
5068 /* Clear prefix structure. */
5069 memset(&p
, 0, sizeof(struct prefix
));
5071 /* Deal with path-id if AddPath is supported. */
5072 if (addpath_encoded
) {
5073 /* When packet overflow occurs return immediately. */
5074 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
5075 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5077 addpath_id
= ntohl(*((uint32_t *)pnt
));
5078 pnt
+= BGP_ADDPATH_ID_LEN
;
5081 /* All EVPN NLRI types start with type and length. */
5083 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE
;
5088 /* When packet overflow occur return immediately. */
5089 if (pnt
+ psize
> lim
)
5090 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5093 case BGP_EVPN_MAC_IP_ROUTE
:
5094 if (process_type2_route(peer
, afi
, safi
,
5095 withdraw
? NULL
: attr
, pnt
,
5096 psize
, addpath_id
)) {
5099 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
5100 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5101 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE
;
5105 case BGP_EVPN_IMET_ROUTE
:
5106 if (process_type3_route(peer
, afi
, safi
,
5107 withdraw
? NULL
: attr
, pnt
,
5108 psize
, addpath_id
)) {
5111 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
5112 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5113 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE
;
5117 case BGP_EVPN_ES_ROUTE
:
5118 if (process_type4_route(peer
, afi
, safi
,
5119 withdraw
? NULL
: attr
, pnt
,
5120 psize
, addpath_id
)) {
5123 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
5124 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5125 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE
;
5129 case BGP_EVPN_IP_PREFIX_ROUTE
:
5130 if (process_type5_route(peer
, afi
, safi
,
5131 withdraw
? NULL
: attr
, pnt
,
5132 psize
, addpath_id
)) {
5135 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
5136 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5137 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE
;
5146 /* Packet length consistency check. */
5148 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH
;
5150 return BGP_NLRI_PARSE_OK
;
5154 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
5155 * The mapping will be used during route processing.
5156 * bgp_def: default bgp instance
5157 * bgp_vrf: specific bgp vrf instance on which RT is configured
5159 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
5162 struct ecommunity_val
*eval
= NULL
;
5163 struct listnode
*node
= NULL
, *nnode
= NULL
;
5164 struct ecommunity
*ecom
= NULL
;
5166 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5167 for (i
= 0; i
< ecom
->size
; i
++) {
5168 eval
= (struct ecommunity_val
*)(ecom
->val
5170 * ECOMMUNITY_SIZE
));
5171 map_vrf_to_rt(bgp_vrf
, eval
);
5177 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
5179 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
5182 struct ecommunity_val
*eval
;
5183 struct listnode
*node
, *nnode
;
5184 struct ecommunity
*ecom
;
5186 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
5187 for (i
= 0; i
< ecom
->size
; i
++) {
5188 struct vrf_irt_node
*irt
;
5189 struct ecommunity_val eval_tmp
;
5191 eval
= (struct ecommunity_val
*)(ecom
->val
5193 * ECOMMUNITY_SIZE
));
5194 /* If using "automatic" RT, we only care about the
5195 * local-admin sub-field.
5196 * This is to facilitate using VNI as the RT for EBGP
5199 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5200 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
5201 BGP_VRF_IMPORT_RT_CFGD
))
5202 mask_ecom_global_admin(&eval_tmp
, eval
);
5204 irt
= lookup_vrf_import_rt(&eval_tmp
);
5206 unmap_vrf_from_rt(bgp_vrf
, irt
);
5213 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
5214 * The mapping will be used during route processing.
5216 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5219 struct ecommunity_val
*eval
;
5220 struct listnode
*node
, *nnode
;
5221 struct ecommunity
*ecom
;
5223 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5224 for (i
= 0; i
< ecom
->size
; i
++) {
5225 eval
= (struct ecommunity_val
*)(ecom
->val
5227 * ECOMMUNITY_SIZE
));
5228 map_vni_to_rt(bgp
, vpn
, eval
);
5234 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
5236 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5239 struct ecommunity_val
*eval
;
5240 struct listnode
*node
, *nnode
;
5241 struct ecommunity
*ecom
;
5243 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5244 for (i
= 0; i
< ecom
->size
; i
++) {
5245 struct irt_node
*irt
;
5246 struct ecommunity_val eval_tmp
;
5248 eval
= (struct ecommunity_val
*)(ecom
->val
5250 * ECOMMUNITY_SIZE
));
5251 /* If using "automatic" RT, we only care about the
5252 * local-admin sub-field.
5253 * This is to facilitate using VNI as the RT for EBGP
5256 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5257 if (!is_import_rt_configured(vpn
))
5258 mask_ecom_global_admin(&eval_tmp
, eval
);
5260 irt
= lookup_import_rt(bgp
, &eval_tmp
);
5262 unmap_vni_from_rt(bgp
, vpn
, irt
);
5268 * Derive Import RT automatically for VNI and map VNI to RT.
5269 * The mapping will be used during route processing.
5271 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5273 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
5274 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
5277 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
5281 * Derive Export RT automatically for VNI.
5283 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5285 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
5286 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5290 * Derive RD automatically for VNI using passed information - it
5291 * is of the form RouterId:unique-id-for-vni.
5293 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5295 if (is_vrf_rd_configured(bgp
))
5298 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5302 * Derive RD automatically for VNI using passed information - it
5303 * is of the form RouterId:unique-id-for-vni.
5305 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5309 vpn
->prd
.family
= AF_UNSPEC
;
5310 vpn
->prd
.prefixlen
= 64;
5311 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
5312 (void)str2prefix_rd(buf
, &vpn
->prd
);
5313 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5319 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5321 struct list
*inst
= bm
->bgp
;
5322 struct listnode
*node
;
5323 struct bgp
*bgp_vrf
;
5325 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5326 if (bgp_vrf
->l3vni
== vni
)
5336 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5338 struct bgpevpn
*vpn
;
5341 memset(&tmp
, 0, sizeof(struct bgpevpn
));
5343 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5348 * Create a new vpn - invoked upon configuration or zebra notification.
5350 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5351 struct in_addr originator_ip
,
5352 vrf_id_t tenant_vrf_id
,
5353 struct in_addr mcast_grp
)
5355 struct bgpevpn
*vpn
;
5360 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5362 /* Set values - RD and RT set to defaults. */
5364 vpn
->originator_ip
= originator_ip
;
5365 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5366 vpn
->mcast_grp
= mcast_grp
;
5368 /* Initialize route-target import and export lists */
5369 vpn
->import_rtl
= list_new();
5370 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5371 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
5372 vpn
->export_rtl
= list_new();
5373 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5374 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
5375 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5376 derive_rd_rt_for_vni(bgp
, vpn
);
5378 /* Initialize EVPN route table. */
5379 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5382 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
5383 XFREE(MTYPE_BGP_EVPN
, vpn
);
5387 /* add to l2vni list on corresponding vrf */
5388 bgpevpn_link_to_l3vni(vpn
);
5390 QOBJ_REG(vpn
, bgpevpn
);
5395 * Free a given VPN - called in multiple scenarios such as zebra
5396 * notification, configuration being deleted, advertise-all-vni disabled etc.
5397 * This just frees appropriate memory, caller should have taken other
5400 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5402 bgpevpn_unlink_from_l3vni(vpn
);
5403 bgp_table_unlock(vpn
->route_table
);
5404 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5405 list_delete(&vpn
->import_rtl
);
5406 list_delete(&vpn
->export_rtl
);
5407 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5408 hash_release(bgp
->vnihash
, vpn
);
5410 XFREE(MTYPE_BGP_EVPN
, vpn
);
5416 struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
)
5421 memset(&tmp
, 0, sizeof(struct evpnes
));
5422 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
5423 es
= hash_lookup(bgp
->esihash
, &tmp
);
5428 * Create a new local es - invoked upon zebra notification.
5430 struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
,
5432 struct ipaddr
*originator_ip
)
5440 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct evpnes
));
5442 /* set the ESI and originator_ip */
5443 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
5444 memcpy(&es
->originator_ip
, originator_ip
, sizeof(struct ipaddr
));
5446 /* Initialise the VTEP list */
5447 es
->vtep_list
= list_new();
5448 es
->vtep_list
->cmp
= evpn_vtep_ip_cmp
;
5450 /* auto derive RD for this es */
5451 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
5452 es
->prd
.family
= AF_UNSPEC
;
5453 es
->prd
.prefixlen
= 64;
5454 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), es
->rd_id
);
5455 (void)str2prefix_rd(buf
, &es
->prd
);
5457 /* Initialize the ES route table */
5458 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5461 if (!hash_get(bgp
->esihash
, es
, hash_alloc_intern
)) {
5462 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5466 QOBJ_REG(es
, evpnes
);
5472 * This just frees appropriate memory, caller should have taken other
5475 void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
)
5477 list_delete(&es
->vtep_list
);
5478 bgp_table_unlock(es
->route_table
);
5479 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
5480 hash_release(bgp
->esihash
, es
);
5482 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5486 * Import evpn route from global table to VNI/VRF/ESI.
5488 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5489 struct prefix
*p
, struct bgp_path_info
*pi
)
5491 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5495 * Unimport evpn route from VNI/VRF/ESI.
5497 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5498 struct prefix
*p
, struct bgp_path_info
*pi
)
5500 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5503 /* filter routes which have martian next hops */
5504 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5508 struct bgp_node
*rd_rn
, *rn
;
5509 struct bgp_table
*table
;
5510 struct bgp_path_info
*pi
;
5515 /* Walk entire global routing table and evaluate routes which could be
5516 * imported into this VPN. Note that we cannot just look at the routes
5517 * for the VNI's RD -
5518 * remote routes applicable for this VNI could have any RD.
5520 /* EVPN routes are a 2-level table. */
5521 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
5522 rd_rn
= bgp_route_next(rd_rn
)) {
5523 table
= bgp_node_get_bgp_table_info(rd_rn
);
5527 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5529 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
5532 /* Consider "valid" remote routes applicable for
5534 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5535 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5537 if (bgp_nexthop_self(bgp
, afi
,
5538 pi
->type
, pi
->sub_type
,
5541 char attr_str
[BUFSIZ
] = {0};
5542 char pbuf
[PREFIX_STRLEN
];
5544 bgp_dump_attr(pi
->attr
, attr_str
,
5547 if (bgp_debug_update(pi
->peer
, &rn
->p
,
5550 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
5557 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5560 bgp_rib_remove(rn
, pi
, pi
->peer
, afi
,
5571 * Handle del of a local MACIP.
5573 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5574 struct ipaddr
*ip
, int state
)
5576 struct bgpevpn
*vpn
;
5577 struct prefix_evpn p
;
5578 struct bgp_node
*rn
;
5580 /* Lookup VNI hash - should exist. */
5581 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5582 if (!vpn
|| !is_vni_live(vpn
)) {
5583 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5584 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5585 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5589 build_evpn_type2_prefix(&p
, mac
, ip
);
5590 if (state
== ZEBRA_NEIGH_ACTIVE
) {
5591 /* Remove EVPN type-2 route and schedule for processing. */
5592 delete_evpn_route(bgp
, vpn
, &p
);
5594 /* Re-instate the current remote best path if any */
5595 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
5597 evpn_zebra_reinstall_best_route(bgp
, vpn
, rn
);
5604 * Handle add of a local MACIP.
5606 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5607 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
)
5609 struct bgpevpn
*vpn
;
5610 struct prefix_evpn p
;
5612 /* Lookup VNI hash - should exist. */
5613 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5614 if (!vpn
|| !is_vni_live(vpn
)) {
5615 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5616 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5617 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5621 /* Create EVPN type-2 route and schedule for processing. */
5622 build_evpn_type2_prefix(&p
, mac
, ip
);
5623 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
)) {
5624 char buf
[ETHER_ADDR_STRLEN
];
5625 char buf2
[INET6_ADDRSTRLEN
];
5628 EC_BGP_EVPN_ROUTE_CREATE
,
5629 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5630 bgp
->vrf_id
, vpn
->vni
,
5631 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5634 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5635 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5642 static void link_l2vni_hash_to_l3vni(struct hash_bucket
*bucket
,
5643 struct bgp
*bgp_vrf
)
5645 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
5646 struct bgp
*bgp_evpn
= NULL
;
5648 bgp_evpn
= bgp_get_evpn();
5651 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5652 bgpevpn_link_to_l3vni(vpn
);
5655 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
,
5656 struct ethaddr
*svi_rmac
,
5657 struct ethaddr
*vrr_rmac
,
5658 struct in_addr originator_ip
, int filter
,
5659 ifindex_t svi_ifindex
,
5660 bool is_anycast_mac
)
5662 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5663 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5664 struct listnode
*node
= NULL
;
5665 struct bgpevpn
*vpn
= NULL
;
5668 /* get the EVPN instance - required to get the AS number for VRF
5671 bgp_evpn
= bgp_get_evpn();
5675 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
5681 /* if the BGP vrf instance doesn't exist - create one */
5682 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5687 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5688 vrf_id
== VRF_DEFAULT
? BGP_INSTANCE_TYPE_DEFAULT
5689 : BGP_INSTANCE_TYPE_VRF
);
5691 case BGP_ERR_AS_MISMATCH
:
5692 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5693 "BGP is already running; AS is %u\n", as
);
5695 case BGP_ERR_INSTANCE_MISMATCH
:
5696 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5697 "BGP instance name and AS number mismatch\n");
5701 /* mark as auto created */
5702 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5705 /* associate the vrf with l3vni and related parameters */
5706 bgp_vrf
->l3vni
= l3vni
;
5707 bgp_vrf
->originator_ip
= originator_ip
;
5708 bgp_vrf
->l3vni_svi_ifindex
= svi_ifindex
;
5709 bgp_vrf
->evpn_info
->is_anycast_mac
= is_anycast_mac
;
5711 /* copy anycast MAC from VRR MAC */
5712 memcpy(&bgp_vrf
->rmac
, vrr_rmac
, ETH_ALEN
);
5713 /* copy sys RMAC from SVI MAC */
5714 memcpy(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, svi_rmac
, ETH_ALEN
);
5715 /* PIP user configured mac is not present use svi mac as sys mac */
5716 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
))
5717 memcpy(&bgp_vrf
->evpn_info
->pip_rmac
, svi_rmac
, ETH_ALEN
);
5719 if (bgp_debug_zebra(NULL
)) {
5720 char buf
[ETHER_ADDR_STRLEN
];
5721 char buf1
[ETHER_ADDR_STRLEN
];
5722 char buf2
[ETHER_ADDR_STRLEN
];
5724 zlog_debug("VRF %s vni %u pip %s RMAC %s sys RMAC %s static RMAC %s is_anycast_mac %s",
5725 vrf_id_to_name(bgp_vrf
->vrf_id
),
5727 bgp_vrf
->evpn_info
->advertise_pip
? "enable"
5729 prefix_mac2str(&bgp_vrf
->rmac
, buf
, sizeof(buf
)),
5730 prefix_mac2str(&bgp_vrf
->evpn_info
->pip_rmac
,
5731 buf1
, sizeof(buf1
)),
5732 prefix_mac2str(&bgp_vrf
->evpn_info
->pip_rmac_static
,
5733 buf2
, sizeof(buf2
)),
5734 is_anycast_mac
? "Enable" : "Disable");
5736 /* set the right filter - are we using l3vni only for prefix routes? */
5738 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5740 /* Map auto derive or configured RTs */
5741 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5742 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5744 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
5746 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5747 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5749 /* auto derive RD */
5750 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5752 /* link all corresponding l2vnis */
5753 hash_iterate(bgp_evpn
->vnihash
,
5754 (void (*)(struct hash_bucket
*,
5755 void *))link_l2vni_hash_to_l3vni
,
5758 /* Only update all corresponding type-2 routes if we are advertising two
5759 * labels along with type-2 routes
5762 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5763 update_routes_for_vni(bgp_evpn
, vpn
);
5765 /* advertise type-5 routes if needed */
5766 update_advertise_vrf_routes(bgp_vrf
);
5768 /* install all remote routes belonging to this l3vni into correspondng
5770 install_routes_for_vrf(bgp_vrf
);
5775 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5777 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5778 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5779 struct listnode
*node
= NULL
;
5780 struct listnode
*next
= NULL
;
5781 struct bgpevpn
*vpn
= NULL
;
5783 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5787 "Cannot process L3VNI %u Del - Could not find BGP instance",
5792 bgp_evpn
= bgp_get_evpn();
5796 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
5801 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5802 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5803 * routes. This will uninstalling the routes from zebra and decremnt the
5806 uninstall_routes_for_vrf(bgp_vrf
);
5808 /* delete/withdraw all type-5 routes */
5809 delete_withdraw_vrf_routes(bgp_vrf
);
5811 /* remove the l3vni from vrf instance */
5814 /* remove the Rmac from the BGP vrf */
5815 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5816 memset(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, 0, ETH_ALEN
);
5817 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
) &&
5818 !is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac
))
5819 memset(&bgp_vrf
->evpn_info
->pip_rmac
, 0, ETH_ALEN
);
5821 /* remove default import RT or Unmap non-default import RT */
5822 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5823 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5824 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5825 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5828 /* remove default export RT */
5829 if (!list_isempty(bgp_vrf
->vrf_export_rtl
) &&
5830 !CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
5831 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5834 /* update all corresponding local mac-ip routes */
5835 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5836 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5837 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5838 update_routes_for_vni(bgp_evpn
, vpn
);
5842 /* If any L2VNIs point to this instance, unlink them. */
5843 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5844 bgpevpn_unlink_from_l3vni(vpn
);
5846 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5848 /* Delete the instance if it was autocreated */
5849 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5850 bgp_delete(bgp_vrf
);
5856 * Handle del of a local VNI.
5858 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5860 struct bgpevpn
*vpn
;
5862 /* Locate VNI hash */
5863 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5865 if (bgp_debug_zebra(NULL
))
5867 EC_BGP_EVPN_VPN_VNI
,
5868 "%u: VNI hash entry for VNI %u not found at DEL",
5873 /* Remove all local EVPN routes and schedule for processing (to
5874 * withdraw from peers).
5876 delete_routes_for_vni(bgp
, vpn
);
5879 * tunnel is no longer active, del tunnel ip address from tip_hash
5881 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5883 /* Clear "live" flag and see if hash needs to be freed. */
5884 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5885 if (!is_vni_configured(vpn
))
5886 bgp_evpn_free(bgp
, vpn
);
5892 * Handle add (or update) of a local VNI. The VNI changes we care
5893 * about are for the local-tunnel-ip and the (tenant) VRF.
5895 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5896 struct in_addr originator_ip
,
5897 vrf_id_t tenant_vrf_id
,
5898 struct in_addr mcast_grp
)
5901 struct bgpevpn
*vpn
;
5902 struct prefix_evpn p
;
5904 /* Lookup VNI. If present and no change, exit. */
5905 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5908 if (is_vni_live(vpn
)
5909 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5910 && IPV4_ADDR_SAME(&vpn
->mcast_grp
, &mcast_grp
)
5911 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5912 /* Probably some other param has changed that we don't
5916 bgp_evpn_mcast_grp_change(bgp
, vpn
, mcast_grp
);
5918 /* Update tenant_vrf_id if it has changed. */
5919 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5920 bgpevpn_unlink_from_l3vni(vpn
);
5921 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5922 bgpevpn_link_to_l3vni(vpn
);
5925 /* If tunnel endpoint IP has changed, update (and delete prior
5926 * type-3 route, if needed.)
5928 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5929 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5931 /* Update all routes with new endpoint IP and/or export RT
5934 if (is_vni_live(vpn
))
5935 update_routes_for_vni(bgp
, vpn
);
5938 /* Create or update as appropriate. */
5940 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
,
5945 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5951 /* if the VNI is live already, there is nothing more to do */
5952 if (is_vni_live(vpn
))
5955 /* Mark as "live" */
5956 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5958 /* tunnel is now active, add tunnel-ip to db */
5959 bgp_tip_add(bgp
, &originator_ip
);
5961 /* filter routes as nexthop database has changed */
5962 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5965 * Create EVPN type-3 route and schedule for processing.
5967 * RT-3 only if doing head-end replication
5969 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
5970 == VXLAN_FLOOD_HEAD_END_REPL
) {
5971 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5972 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0)) {
5973 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5974 "%u: Type3 route creation failure for VNI %u",
5980 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5984 install_routes_for_vni(bgp
, vpn
);
5986 /* If we are advertising gateway mac-ip
5987 It needs to be conveyed again to zebra */
5988 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5990 /* advertise svi mac-ip knob to zebra */
5991 bgp_zebra_advertise_svi_macip(bgp
, vpn
->advertise_svi_macip
, vpn
->vni
);
5997 * bgp_evpn_local_es_del
5999 int bgp_evpn_local_es_del(struct bgp
*bgp
,
6001 struct ipaddr
*originator_ip
)
6003 char buf
[ESI_STR_LEN
];
6004 struct evpnes
*es
= NULL
;
6006 if (!bgp
->esihash
) {
6007 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
6012 /* Lookup ESI hash - should exist. */
6013 es
= bgp_evpn_lookup_es(bgp
, esi
);
6015 flog_warn(EC_BGP_EVPN_ESI
,
6016 "%u: ESI hash entry for ESI %s at Local ES DEL",
6017 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
6021 /* Delete all local EVPN ES routes from ESI table
6022 * and schedule for processing (to withdraw from peers))
6024 delete_routes_for_es(bgp
, es
);
6026 /* free the hash entry */
6027 bgp_evpn_es_free(bgp
, es
);
6033 * bgp_evpn_local_es_add
6035 int bgp_evpn_local_es_add(struct bgp
*bgp
,
6037 struct ipaddr
*originator_ip
)
6039 char buf
[ESI_STR_LEN
];
6040 struct evpnes
*es
= NULL
;
6041 struct prefix_evpn p
;
6043 if (!bgp
->esihash
) {
6044 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
6049 /* create the new es */
6050 es
= bgp_evpn_lookup_es(bgp
, esi
);
6052 es
= bgp_evpn_es_new(bgp
, esi
, originator_ip
);
6056 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
6057 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
6061 UNSET_FLAG(es
->flags
, EVPNES_REMOTE
);
6062 SET_FLAG(es
->flags
, EVPNES_LOCAL
);
6064 build_evpn_type4_prefix(&p
, esi
, originator_ip
->ipaddr_v4
);
6065 if (update_evpn_type4_route(bgp
, es
, &p
)) {
6066 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
6067 "%u: Type4 route creation failure for ESI %s",
6068 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
6072 /* import all remote ES routes in th ES table */
6073 install_routes_for_es(bgp
, es
);
6079 * Handle change in setting for BUM handling. The supported values
6080 * are head-end replication and dropping all BUM packets. Any change
6081 * should be registered with zebra. Also, if doing head-end replication,
6082 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
6083 * to be dropped, the RT-3s must be withdrawn.
6085 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
6087 zlog_info("L2VPN EVPN BUM handling is %s",
6088 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
6089 "Flooding" : "Flooding Disabled");
6091 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
6092 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
6093 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
6094 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
6095 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
6099 * Cleanup EVPN information on disable - Need to delete and withdraw
6100 * EVPN routes from peers.
6102 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
6104 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_bucket
*,
6105 void *))cleanup_vni_on_disable
,
6110 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
6111 * BGP instance (default) is being freed.
6113 void bgp_evpn_cleanup(struct bgp
*bgp
)
6115 hash_iterate(bgp
->vnihash
,
6116 (void (*)(struct hash_bucket
*, void *))free_vni_entry
,
6119 hash_free(bgp
->import_rt_hash
);
6120 bgp
->import_rt_hash
= NULL
;
6122 hash_free(bgp
->vrf_import_rt_hash
);
6123 bgp
->vrf_import_rt_hash
= NULL
;
6125 hash_free(bgp
->vnihash
);
6126 bgp
->vnihash
= NULL
;
6128 hash_free(bgp
->esihash
);
6129 bgp
->esihash
= NULL
;
6131 list_delete(&bgp
->vrf_import_rtl
);
6132 list_delete(&bgp
->vrf_export_rtl
);
6133 list_delete(&bgp
->l2vnis
);
6137 * Initialization for EVPN
6140 * hash for RT to VNI
6142 void bgp_evpn_init(struct bgp
*bgp
)
6145 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
6147 hash_create(esi_hash_keymake
, esi_cmp
,
6148 "BGP EVPN Local ESI Hash");
6149 bgp
->import_rt_hash
=
6150 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
6151 "BGP Import RT Hash");
6152 bgp
->vrf_import_rt_hash
=
6153 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
6154 "BGP VRF Import RT Hash");
6155 bgp
->vrf_import_rtl
= list_new();
6156 bgp
->vrf_import_rtl
->cmp
=
6157 (int (*)(void *, void *))evpn_route_target_cmp
;
6158 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
6159 bgp
->vrf_export_rtl
= list_new();
6160 bgp
->vrf_export_rtl
->cmp
=
6161 (int (*)(void *, void *))evpn_route_target_cmp
;
6162 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
6163 bgp
->l2vnis
= list_new();
6164 bgp
->l2vnis
->cmp
= vni_list_cmp
;
6165 /* By default Duplicate Address Dection is enabled.
6166 * Max-moves (N) 5, detection time (M) 180
6167 * default action is warning-only
6168 * freeze action permanently freezes address,
6169 * and freeze time (auto-recovery) is disabled.
6171 if (bgp
->evpn_info
) {
6172 bgp
->evpn_info
->dup_addr_detect
= true;
6173 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
6174 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
6175 bgp
->evpn_info
->dad_freeze
= false;
6176 bgp
->evpn_info
->dad_freeze_time
= 0;
6177 /* Initialize zebra vxlan */
6178 bgp_zebra_dup_addr_detection(bgp
);
6179 /* Enable PIP feature by default for bgp vrf instance */
6180 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
6181 struct bgp
*bgp_default
;
6183 bgp
->evpn_info
->advertise_pip
= true;
6184 bgp_default
= bgp_get_default();
6186 bgp
->evpn_info
->pip_ip
= bgp_default
->router_id
;
6190 /* Default BUM handling is to do head-end replication. */
6191 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
6194 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
6196 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
6200 * Get the prefixlen of the ip prefix carried within the type5 evpn route.
6202 int bgp_evpn_get_type5_prefixlen(struct prefix
*pfx
)
6204 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6206 if (!pfx
|| pfx
->family
!= AF_EVPN
)
6209 if (evp
->prefix
.route_type
!= BGP_EVPN_IP_PREFIX_ROUTE
)
6212 return evp
->prefix
.prefix_addr
.ip_prefix_length
;
6216 * Should we register nexthop for this EVPN prefix for nexthop tracking?
6218 bool bgp_evpn_is_prefix_nht_supported(struct prefix
*pfx
)
6220 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6223 * EVPN RT-5 should not be marked as valid and imported to vrfs if the
6224 * BGP nexthop is not reachable. To check for the nexthop reachability,
6225 * Add nexthop for EVPN RT-5 for nexthop tracking.
6227 * Ideally, a BGP route should be marked as valid only if the
6228 * nexthop is reachable. Thus, other EVPN route types also should be
6229 * added here after testing is performed for them.
6231 if (pfx
&& pfx
->family
== AF_EVPN
&&
6232 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
)