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(void *p
)
88 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(void *p
)
116 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(void *p
)
148 struct vrf_irt_node
*irt
= p
;
149 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(void *p
)
264 struct irt_node
*irt
= p
;
265 char *pnt
= irt
->rt
.val
;
267 return jhash(pnt
, 8, 0xdeadbeef);
271 * Comparison function for import rt hash
273 static bool import_rt_hash_cmp(const void *p1
, const void *p2
)
275 const struct irt_node
*irt1
= p1
;
276 const struct irt_node
*irt2
= p2
;
278 if (irt1
== NULL
&& irt2
== NULL
)
281 if (irt1
== NULL
|| irt2
== NULL
)
284 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
288 * Create a new import_rt
290 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
291 struct ecommunity_val
*rt
)
293 struct irt_node
*irt
;
298 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
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
);
498 * Create RT extended community automatically from passed information:
499 * of the form AS:VNI.
500 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
501 * the need is to get a RT value that will be unique across different
502 * VNIs but the same across routers (in the same AS) for a particular
505 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
507 struct ecommunity_val eval
;
508 struct ecommunity
*ecomadd
;
510 if (bgp
->advertise_autort_rfc8365
)
511 vni
|= EVPN_AUTORT_VXLAN
;
512 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
514 ecomadd
= ecommunity_new();
515 ecommunity_add_val(ecomadd
, &eval
);
516 listnode_add_sort(rtl
, ecomadd
);
520 * Derive RD and RT for a VNI automatically. Invoked at the time of
523 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
525 bgp_evpn_derive_auto_rd(bgp
, vpn
);
526 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
527 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
531 * Convert nexthop (remote VTEP IP) into an IPv6 address.
533 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
535 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
537 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
538 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
542 * Add (update) or delete MACIP from zebra.
544 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
545 struct prefix_evpn
*p
,
546 struct in_addr remote_vtep_ip
, int add
,
547 uint8_t flags
, uint32_t seq
)
551 char buf1
[ETHER_ADDR_STRLEN
];
552 char buf2
[INET6_ADDRSTRLEN
];
553 char buf3
[INET6_ADDRSTRLEN
];
556 if (!zclient
|| zclient
->sock
< 0)
559 /* Don't try to register if Zebra doesn't know of this instance. */
560 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
561 if (BGP_DEBUG(zebra
, ZEBRA
))
562 zlog_debug("%s: No zebra instance to talk to, not installing remote macip",
563 __PRETTY_FUNCTION__
);
569 zclient_create_header(
570 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
572 stream_putl(s
, vpn
->vni
);
573 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
574 /* IP address length and IP address, if any. */
575 if (is_evpn_prefix_ipaddr_none(p
))
578 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
580 stream_putl(s
, ipa_len
);
581 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
583 stream_put_in_addr(s
, &remote_vtep_ip
);
585 /* TX flags - MAC sticky status and/or gateway mac */
586 /* Also TX the sequence number of the best route. */
588 stream_putc(s
, flags
);
592 stream_putw_at(s
, 0, stream_get_endp(s
));
594 if (bgp_debug_zebra(NULL
))
596 "Tx %s MACIP, VNI %u MAC %s IP %s flags 0x%x seq %u remote VTEP %s",
597 add
? "ADD" : "DEL", vpn
->vni
,
598 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
600 ipaddr2str(&p
->prefix
.macip_addr
.ip
,
601 buf3
, sizeof(buf3
)), flags
, seq
,
602 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
605 return zclient_send_message(zclient
);
609 * Add (update) or delete remote VTEP from zebra.
611 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
612 struct prefix_evpn
*p
, int add
)
617 if (!zclient
|| zclient
->sock
< 0)
620 /* Don't try to register if Zebra doesn't know of this instance. */
621 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
622 if (BGP_DEBUG(zebra
, ZEBRA
))
623 zlog_debug("%s: No zebra instance to talk to, not installing remote vtep",
624 __PRETTY_FUNCTION__
);
631 zclient_create_header(
632 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
634 stream_putl(s
, vpn
->vni
);
635 if (is_evpn_prefix_ipaddr_v4(p
))
636 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
637 else if (is_evpn_prefix_ipaddr_v6(p
)) {
640 "Bad remote IP when trying to %s remote VTEP for VNI %u",
641 add
? "ADD" : "DEL", vpn
->vni
);
645 stream_putw_at(s
, 0, stream_get_endp(s
));
647 if (bgp_debug_zebra(NULL
))
648 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
649 add
? "ADD" : "DEL", vpn
->vni
,
650 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
652 return zclient_send_message(zclient
);
656 * Build extended community for EVPN ES (type-4) route
658 static void build_evpn_type4_route_extcomm(struct evpnes
*es
,
661 struct ecommunity ecom_encap
;
662 struct ecommunity ecom_es_rt
;
663 struct ecommunity_val eval
;
664 struct ecommunity_val eval_es_rt
;
665 bgp_encap_types tnl_type
;
669 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
670 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
671 encode_encap_extcomm(tnl_type
, &eval
);
673 ecom_encap
.val
= (uint8_t *)eval
.val
;
674 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
677 memset(&mac
, 0, sizeof(struct ethaddr
));
678 memset(&ecom_es_rt
, 0, sizeof(ecom_es_rt
));
679 es_get_system_mac(&es
->esi
, &mac
);
680 encode_es_rt_extcomm(&eval_es_rt
, &mac
);
682 ecom_es_rt
.val
= (uint8_t *)eval_es_rt
.val
;
684 ecommunity_merge(attr
->ecommunity
, &ecom_es_rt
);
686 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
690 * Build extended communities for EVPN prefix route.
692 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
695 struct ecommunity ecom_encap
;
696 struct ecommunity ecom_rmac
;
697 struct ecommunity_val eval
;
698 struct ecommunity_val eval_rmac
;
699 bgp_encap_types tnl_type
;
700 struct listnode
*node
, *nnode
;
701 struct ecommunity
*ecom
;
702 struct list
*vrf_export_rtl
= NULL
;
705 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
706 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
707 encode_encap_extcomm(tnl_type
, &eval
);
709 ecom_encap
.val
= (uint8_t *)eval
.val
;
712 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
714 /* Add the export RTs for L3VNI/VRF */
715 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
716 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
718 ecommunity_merge(attr
->ecommunity
, ecom
);
720 /* add the router mac extended community */
721 if (!is_zero_mac(&attr
->rmac
)) {
722 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
723 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
725 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
727 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
730 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
734 * Build extended communities for EVPN route.
735 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
736 * and ENCAP extended communities are applicable for all routes.
737 * The default gateway extended community and MAC mobility (sticky) extended
738 * community are added as needed based on passed settings - only for type-2
739 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
740 * added, if present, based on passed settings - only for non-link-local
743 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
746 struct ecommunity ecom_encap
;
747 struct ecommunity ecom_sticky
;
748 struct ecommunity ecom_default_gw
;
749 struct ecommunity ecom_rmac
;
750 struct ecommunity ecom_na
;
751 struct ecommunity_val eval
;
752 struct ecommunity_val eval_sticky
;
753 struct ecommunity_val eval_default_gw
;
754 struct ecommunity_val eval_rmac
;
755 struct ecommunity_val eval_na
;
757 bgp_encap_types tnl_type
;
758 struct listnode
*node
, *nnode
;
759 struct ecommunity
*ecom
;
761 struct list
*vrf_export_rtl
= NULL
;
764 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
765 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
766 encode_encap_extcomm(tnl_type
, &eval
);
768 ecom_encap
.val
= (uint8_t *)eval
.val
;
771 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
773 /* Add the export RTs for L2VNI */
774 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
775 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
777 /* Add the export RTs for L3VNI if told to - caller determines
778 * when this should be done.
781 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
782 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
783 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
785 attr
->ecommunity
= ecommunity_merge(
786 attr
->ecommunity
, ecom
);
790 /* Add MAC mobility (sticky) if needed. */
793 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
794 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
795 ecom_sticky
.size
= 1;
796 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
798 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
801 /* Add RMAC, if told to. */
803 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
804 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
806 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
808 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
811 /* Add default gateway, if needed. */
812 if (attr
->default_gw
) {
813 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
814 encode_default_gw_extcomm(&eval_default_gw
);
815 ecom_default_gw
.size
= 1;
816 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
818 ecommunity_merge(attr
->ecommunity
, &ecom_default_gw
);
821 if (attr
->router_flag
) {
822 memset(&ecom_na
, 0, sizeof(ecom_na
));
823 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
);
825 ecom_na
.val
= (uint8_t *)eval_na
.val
;
826 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
830 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
834 * Add MAC mobility extended community to attribute.
836 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
838 struct ecommunity ecom_tmp
;
839 struct ecommunity_val eval
;
840 uint8_t *ecom_val_ptr
;
847 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
849 /* Find current MM ecommunity */
852 if (attr
->ecommunity
) {
853 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
854 pnt
= attr
->ecommunity
->val
+ (i
* 8);
858 if (type
== ECOMMUNITY_ENCODE_EVPN
860 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
861 ecom_val_ptr
= (uint8_t *)(attr
->ecommunity
->val
868 /* Update the existing MM ecommunity */
870 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
872 /* Add MM to existing */
874 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
876 ecom_tmp
.val
= (uint8_t *)eval
.val
;
878 if (attr
->ecommunity
)
880 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
882 attr
->ecommunity
= ecommunity_dup(&ecom_tmp
);
886 /* Install EVPN route into zebra. */
887 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
888 struct prefix_evpn
*p
, struct bgp_path_info
*pi
)
893 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
895 if (pi
->attr
->sticky
)
896 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
897 if (pi
->attr
->default_gw
)
898 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
899 if (is_evpn_prefix_ipaddr_v6(p
) &&
900 pi
->attr
->router_flag
)
901 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
902 ret
= bgp_zebra_send_remote_macip(
903 bgp
, vpn
, p
, pi
->attr
->nexthop
, 1, flags
,
904 mac_mobility_seqnum(pi
->attr
));
906 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 1);
912 /* Uninstall EVPN route from zebra. */
913 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
914 struct prefix_evpn
*p
,
915 struct in_addr remote_vtep_ip
)
919 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
920 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
923 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 0);
929 * Due to MAC mobility, the prior "local" best route has been supplanted
930 * by a "remote" best route. The prior route has to be deleted and withdrawn
933 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
935 struct bgp_path_info
*old_local
)
937 struct bgp_node
*global_rn
;
938 struct bgp_path_info
*pi
;
939 afi_t afi
= AFI_L2VPN
;
940 safi_t safi
= SAFI_EVPN
;
942 /* Locate route node in the global EVPN routing table. Note that
943 * this table is a 2-level tree (RD-level + Prefix-level) similar to
946 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
947 (struct prefix
*)&rn
->p
, &vpn
->prd
);
949 /* Delete route entry in the global EVPN table. */
950 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
952 /* Schedule for processing - withdraws to peers happen from
956 bgp_process(bgp
, global_rn
, afi
, safi
);
957 bgp_unlock_node(global_rn
);
960 /* Delete route entry in the VNI route table, caller to remove. */
961 bgp_path_info_delete(rn
, old_local
);
964 static struct in_addr
*es_vtep_new(struct in_addr vtep
)
968 ip
= XCALLOC(MTYPE_BGP_EVPN_ES_VTEP
, sizeof(struct in_addr
));
970 ip
->s_addr
= vtep
.s_addr
;
974 static void es_vtep_free(struct in_addr
*ip
)
976 XFREE(MTYPE_BGP_EVPN_ES_VTEP
, ip
);
979 /* check if VTEP is already part of the list */
980 static int is_vtep_present_in_list(struct list
*list
,
983 struct listnode
*node
= NULL
;
986 for (ALL_LIST_ELEMENTS_RO(list
, node
, tmp
)) {
987 if (tmp
->s_addr
== vtep
.s_addr
)
994 * Best path for ES route was changed,
995 * update the list of VTEPs for this ES
997 static int evpn_es_install_vtep(struct bgp
*bgp
,
999 struct prefix_evpn
*p
,
1000 struct in_addr rvtep
)
1002 struct in_addr
*vtep_ip
;
1004 if (is_vtep_present_in_list(es
->vtep_list
, rvtep
))
1008 vtep_ip
= es_vtep_new(rvtep
);
1010 listnode_add_sort(es
->vtep_list
, vtep_ip
);
1015 * Best path for ES route was changed,
1016 * update the list of VTEPs for this ES
1018 static int evpn_es_uninstall_vtep(struct bgp
*bgp
,
1020 struct prefix_evpn
*p
,
1021 struct in_addr rvtep
)
1023 struct listnode
*node
, *nnode
, *node_to_del
= NULL
;
1024 struct in_addr
*tmp
;
1026 for (ALL_LIST_ELEMENTS(es
->vtep_list
, node
, nnode
, tmp
)) {
1027 if (tmp
->s_addr
== rvtep
.s_addr
) {
1034 list_delete_node(es
->vtep_list
, node_to_del
);
1040 * Calculate the best path for a ES(type-4) route.
1042 static int evpn_es_route_select_install(struct bgp
*bgp
,
1044 struct bgp_node
*rn
)
1047 afi_t afi
= AFI_L2VPN
;
1048 safi_t safi
= SAFI_EVPN
;
1049 struct bgp_path_info
*old_select
; /* old best */
1050 struct bgp_path_info
*new_select
; /* new best */
1051 struct bgp_path_info_pair old_and_new
;
1053 /* Compute the best path. */
1054 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
],
1055 &old_and_new
, afi
, safi
);
1056 old_select
= old_and_new
.old
;
1057 new_select
= old_and_new
.new;
1060 * If the best path hasn't changed - see if something needs to be
1063 if (old_select
&& old_select
== new_select
1064 && old_select
->type
== ZEBRA_ROUTE_BGP
1065 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1066 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1067 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1068 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1069 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1070 ret
= evpn_es_install_vtep(bgp
, es
,
1071 (struct prefix_evpn
*)&rn
->p
,
1072 old_select
->attr
->nexthop
);
1074 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1075 bgp_zebra_clear_route_change_flags(rn
);
1079 /* If the user did a "clear" this flag will be set */
1080 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1083 * bestpath has changed; update relevant fields and install or uninstall
1084 * into the zebra RIB.
1086 if (old_select
|| new_select
)
1087 bgp_bump_version(rn
);
1090 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1092 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1093 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1094 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1097 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1098 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1099 ret
= evpn_es_install_vtep(bgp
, es
,
1100 (struct prefix_evpn
*)&rn
->p
,
1101 new_select
->attr
->nexthop
);
1103 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1104 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1105 ret
= evpn_es_uninstall_vtep(
1106 bgp
, es
, (struct prefix_evpn
*)&rn
->p
,
1107 old_select
->attr
->nexthop
);
1110 /* Clear any route change flags. */
1111 bgp_zebra_clear_route_change_flags(rn
);
1113 /* Reap old select bgp_path_info, if it has been removed */
1114 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1115 bgp_path_info_reap(rn
, old_select
);
1121 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1124 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1125 struct bgp_node
*rn
)
1127 struct bgp_path_info
*old_select
, *new_select
;
1128 struct bgp_path_info_pair old_and_new
;
1129 afi_t afi
= AFI_L2VPN
;
1130 safi_t safi
= SAFI_EVPN
;
1133 /* Compute the best path. */
1134 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1136 old_select
= old_and_new
.old
;
1137 new_select
= old_and_new
.new;
1139 /* If the best path hasn't changed - see if there is still something to
1143 if (old_select
&& old_select
== new_select
1144 && old_select
->type
== ZEBRA_ROUTE_BGP
1145 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1146 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1147 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1148 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1149 if (bgp_zebra_has_route_changed(rn
, old_select
))
1150 ret
= evpn_zebra_install(
1151 bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1153 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1154 bgp_zebra_clear_route_change_flags(rn
);
1158 /* If the user did a "clear" this flag will be set */
1159 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1161 /* bestpath has changed; update relevant fields and install or uninstall
1162 * into the zebra RIB.
1164 if (old_select
|| new_select
)
1165 bgp_bump_version(rn
);
1168 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1170 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1171 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1172 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1175 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1176 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1177 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1180 /* If an old best existed and it was a "local" route, the only
1182 * it would be supplanted is due to MAC mobility procedures. So,
1184 * need to do an implicit delete and withdraw that route from
1187 if (old_select
&& old_select
->peer
== bgp
->peer_self
1188 && old_select
->type
== ZEBRA_ROUTE_BGP
1189 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
1190 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
1192 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1193 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1194 ret
= evpn_zebra_uninstall(bgp
, vpn
,
1195 (struct prefix_evpn
*)&rn
->p
,
1196 old_select
->attr
->nexthop
);
1199 /* Clear any route change flags. */
1200 bgp_zebra_clear_route_change_flags(rn
);
1202 /* Reap old select bgp_path_info, if it has been removed */
1203 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1204 bgp_path_info_reap(rn
, old_select
);
1210 * Return true if the local ri for this rn is of type gateway mac
1212 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
1214 struct bgp_path_info
*tmp_pi
= NULL
;
1215 struct bgp_path_info
*local_pi
= NULL
;
1218 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1219 tmp_pi
= tmp_pi
->next
) {
1220 if (tmp_pi
->peer
== bgp
->peer_self
1221 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1222 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1229 return local_pi
->attr
->default_gw
;
1234 * Return true if the local ri for this rn has sticky set
1236 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
1238 struct bgp_path_info
*tmp_pi
;
1239 struct bgp_path_info
*local_pi
;
1242 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1243 tmp_pi
= tmp_pi
->next
) {
1244 if (tmp_pi
->peer
== bgp
->peer_self
1245 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1246 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1253 return local_pi
->attr
->sticky
;
1257 * create or update EVPN type4 route entry.
1258 * This could be in the ES table or the global table.
1259 * TODO: handle remote ES (type4) routes as well
1261 static int update_evpn_type4_route_entry(struct bgp
*bgp
, struct evpnes
*es
,
1262 afi_t afi
, safi_t safi
,
1263 struct bgp_node
*rn
, struct attr
*attr
,
1264 int add
, struct bgp_path_info
**ri
,
1267 char buf
[ESI_STR_LEN
];
1268 char buf1
[INET6_ADDRSTRLEN
];
1269 struct bgp_path_info
*tmp_pi
= NULL
;
1270 struct bgp_path_info
*local_pi
= NULL
; /* local route entry if any */
1271 struct bgp_path_info
*remote_pi
= NULL
; /* remote route entry if any */
1272 struct attr
*attr_new
= NULL
;
1273 struct prefix_evpn
*evp
= NULL
;
1277 evp
= (struct prefix_evpn
*)&rn
->p
;
1279 /* locate the local and remote entries if any */
1280 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1281 tmp_pi
= tmp_pi
->next
) {
1282 if (tmp_pi
->peer
== bgp
->peer_self
1283 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1284 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1286 if (tmp_pi
->type
== ZEBRA_ROUTE_BGP
1287 && tmp_pi
->sub_type
== BGP_ROUTE_IMPORTED
1288 && CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1292 /* we don't expect to see a remote_ri at this point.
1293 * An ES route has esi + vtep_ip as the key,
1294 * We shouldn't see the same route from any other vtep.
1299 "%u ERROR: local es route for ESI: %s Vtep %s also learnt from remote",
1301 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1302 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1306 if (!local_pi
&& !add
)
1309 /* create or update the entry */
1312 /* Add or update attribute to hash */
1313 attr_new
= bgp_attr_intern(attr
);
1315 /* Create new route with its attribute. */
1316 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1317 bgp
->peer_self
, attr_new
, rn
);
1318 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1320 /* add the newly created path to the route-node */
1321 bgp_path_info_add(rn
, tmp_pi
);
1324 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1325 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1328 /* The attribute has changed.
1329 * Add (or update) attribute to hash. */
1330 attr_new
= bgp_attr_intern(attr
);
1331 bgp_path_info_set_flag(rn
, tmp_pi
,
1332 BGP_PATH_ATTR_CHANGED
);
1334 /* Restore route, if needed. */
1335 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1336 bgp_path_info_restore(rn
, tmp_pi
);
1338 /* Unintern existing, set to new. */
1339 bgp_attr_unintern(&tmp_pi
->attr
);
1340 tmp_pi
->attr
= attr_new
;
1341 tmp_pi
->uptime
= bgp_clock();
1345 /* Return back the route entry. */
1350 /* update evpn es (type-4) route */
1351 static int update_evpn_type4_route(struct bgp
*bgp
,
1353 struct prefix_evpn
*p
)
1356 int route_changed
= 0;
1357 char buf
[ESI_STR_LEN
];
1358 char buf1
[INET6_ADDRSTRLEN
];
1359 afi_t afi
= AFI_L2VPN
;
1360 safi_t safi
= SAFI_EVPN
;
1362 struct attr
*attr_new
= NULL
;
1363 struct bgp_node
*rn
= NULL
;
1364 struct bgp_path_info
*pi
= NULL
;
1366 memset(&attr
, 0, sizeof(struct attr
));
1368 /* Build path-attribute for this route. */
1369 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1370 attr
.nexthop
= es
->originator_ip
.ipaddr_v4
;
1371 attr
.mp_nexthop_global_in
= es
->originator_ip
.ipaddr_v4
;
1372 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1374 /* Set up extended community. */
1375 build_evpn_type4_route_extcomm(es
, &attr
);
1377 /* First, create (or fetch) route node within the ESI. */
1378 /* NOTE: There is no RD here. */
1379 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
1381 /* Create or update route entry. */
1382 ret
= update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, &attr
, 1,
1383 &pi
, &route_changed
);
1385 flog_err(EC_BGP_ES_INVALID
,
1386 "%u ERROR: Failed to updated ES route ESI: %s VTEP %s",
1388 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1389 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1393 attr_new
= pi
->attr
;
1395 /* Perform route selection;
1396 * this is just to set the flags correctly
1397 * as local route in the ES always wins.
1399 evpn_es_route_select_install(bgp
, es
, rn
);
1400 bgp_unlock_node(rn
);
1402 /* If this is a new route or some attribute has changed, export the
1403 * route to the global table. The route will be advertised to peers
1404 * from there. Note that this table is a 2-level tree (RD-level +
1405 * Prefix-level) similar to L3VPN routes.
1407 if (route_changed
) {
1408 struct bgp_path_info
*global_pi
;
1410 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1411 (struct prefix
*)p
, &es
->prd
);
1412 update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, attr_new
,
1413 1, &global_pi
, &route_changed
);
1415 /* Schedule for processing and unlock node. */
1416 bgp_process(bgp
, rn
, afi
, safi
);
1417 bgp_unlock_node(rn
);
1420 /* Unintern temporary. */
1421 aspath_unintern(&attr
.aspath
);
1425 static int update_evpn_type5_route_entry(struct bgp
*bgp_evpn
,
1426 struct bgp
*bgp_vrf
, afi_t afi
,
1427 safi_t safi
, struct bgp_node
*rn
,
1428 struct attr
*attr
, int *route_changed
)
1430 struct attr
*attr_new
= NULL
;
1431 struct bgp_path_info
*pi
= NULL
;
1432 mpls_label_t label
= MPLS_INVALID_LABEL
;
1433 struct bgp_path_info
*local_pi
= NULL
;
1434 struct bgp_path_info
*tmp_pi
= NULL
;
1437 /* locate the local route entry if any */
1438 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1439 tmp_pi
= tmp_pi
->next
) {
1440 if (tmp_pi
->peer
== bgp_evpn
->peer_self
1441 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1442 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1447 * create a new route entry if one doesn't exist.
1448 * Otherwise see if route attr has changed
1452 /* route has changed as this is the first entry */
1455 /* Add (or update) attribute to hash. */
1456 attr_new
= bgp_attr_intern(attr
);
1458 /* create the route info from attribute */
1459 pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1460 bgp_evpn
->peer_self
, attr_new
, rn
);
1461 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
1463 /* Type-5 routes advertise the L3-VNI */
1464 bgp_path_info_extra_get(pi
);
1465 vni2label(bgp_vrf
->l3vni
, &label
);
1466 memcpy(&pi
->extra
->label
, &label
, sizeof(label
));
1467 pi
->extra
->num_labels
= 1;
1469 /* add the route entry to route node*/
1470 bgp_path_info_add(rn
, pi
);
1474 if (!attrhash_cmp(tmp_pi
->attr
, attr
)) {
1476 /* attribute changed */
1479 /* The attribute has changed. */
1480 /* Add (or update) attribute to hash. */
1481 attr_new
= bgp_attr_intern(attr
);
1482 bgp_path_info_set_flag(rn
, tmp_pi
,
1483 BGP_PATH_ATTR_CHANGED
);
1485 /* Restore route, if needed. */
1486 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1487 bgp_path_info_restore(rn
, tmp_pi
);
1489 /* Unintern existing, set to new. */
1490 bgp_attr_unintern(&tmp_pi
->attr
);
1491 tmp_pi
->attr
= attr_new
;
1492 tmp_pi
->uptime
= bgp_clock();
1498 /* update evpn type-5 route entry */
1499 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1500 struct attr
*src_attr
)
1502 afi_t afi
= AFI_L2VPN
;
1503 safi_t safi
= SAFI_EVPN
;
1505 struct bgp_node
*rn
= NULL
;
1506 struct bgp
*bgp_evpn
= NULL
;
1507 int route_changed
= 0;
1509 bgp_evpn
= bgp_get_evpn();
1513 /* Build path attribute for this route - use the source attr, if
1514 * present, else treat as locally originated.
1517 bgp_attr_dup(&attr
, src_attr
);
1519 memset(&attr
, 0, sizeof(struct attr
));
1520 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1522 /* Set nexthop to ourselves and fill in the Router MAC. */
1523 attr
.nexthop
= bgp_vrf
->originator_ip
;
1524 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1525 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1526 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
1528 /* Setup RT and encap extended community */
1529 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1531 /* get the route node in global table */
1532 rn
= bgp_afi_node_get(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1533 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1536 /* create or update the route entry within the route node */
1537 update_evpn_type5_route_entry(bgp_evpn
, bgp_vrf
, afi
, safi
, rn
, &attr
,
1540 /* schedule for processing and unlock node */
1541 if (route_changed
) {
1542 bgp_process(bgp_evpn
, rn
, afi
, safi
);
1543 bgp_unlock_node(rn
);
1546 /* uninten temporary */
1548 aspath_unintern(&attr
.aspath
);
1553 * Create or update EVPN route entry. This could be in the VNI route table
1554 * or the global route table.
1556 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1557 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1558 struct attr
*attr
, int add
,
1559 struct bgp_path_info
**pi
, uint8_t flags
,
1562 struct bgp_path_info
*tmp_pi
;
1563 struct bgp_path_info
*local_pi
;
1564 struct attr
*attr_new
;
1565 mpls_label_t label
[BGP_MAX_LABELS
];
1566 uint32_t num_labels
= 1;
1567 int route_change
= 1;
1569 struct prefix_evpn
*evp
;
1572 evp
= (struct prefix_evpn
*)&rn
->p
;
1573 memset(&label
, 0, sizeof(label
));
1575 /* See if this is an update of an existing route, or a new add. */
1577 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1578 tmp_pi
= tmp_pi
->next
) {
1579 if (tmp_pi
->peer
== bgp
->peer_self
1580 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1581 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1585 /* If route doesn't exist already, create a new one, if told to.
1586 * Otherwise act based on whether the attributes of the route have
1589 if (!local_pi
&& !add
)
1592 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1593 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1594 add_mac_mobility_to_attr(seq
, attr
);
1597 /* Add (or update) attribute to hash. */
1598 attr_new
= bgp_attr_intern(attr
);
1600 /* Extract MAC mobility sequence number, if any. */
1601 attr_new
->mm_seqnum
=
1602 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1603 attr_new
->sticky
= sticky
;
1605 /* Create new route with its attribute. */
1606 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1607 bgp
->peer_self
, attr_new
, rn
);
1608 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1609 bgp_path_info_extra_get(tmp_pi
);
1611 /* The VNI goes into the 'label' field of the route */
1612 vni2label(vpn
->vni
, &label
[0]);
1614 /* Type-2 routes may carry a second VNI - the L3-VNI.
1615 * Only attach second label if we are advertising two labels for
1618 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1619 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1622 l3vni
= bgpevpn_get_l3vni(vpn
);
1624 vni2label(l3vni
, &label
[1]);
1629 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1630 tmp_pi
->extra
->num_labels
= num_labels
;
1631 bgp_path_info_add(rn
, tmp_pi
);
1634 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1635 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1639 * The attributes have changed, type-2 routes needs to
1640 * be advertised with right labels.
1642 vni2label(vpn
->vni
, &label
[0]);
1643 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1644 && CHECK_FLAG(vpn
->flags
,
1645 VNI_FLAG_USE_TWO_LABELS
)) {
1648 l3vni
= bgpevpn_get_l3vni(vpn
);
1650 vni2label(l3vni
, &label
[1]);
1654 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1655 tmp_pi
->extra
->num_labels
= num_labels
;
1657 /* The attribute has changed. */
1658 /* Add (or update) attribute to hash. */
1659 attr_new
= bgp_attr_intern(attr
);
1660 bgp_path_info_set_flag(rn
, tmp_pi
,
1661 BGP_PATH_ATTR_CHANGED
);
1663 /* Extract MAC mobility sequence number, if any. */
1664 attr_new
->mm_seqnum
=
1665 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1666 attr_new
->sticky
= sticky
;
1668 /* Restore route, if needed. */
1669 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1670 bgp_path_info_restore(rn
, tmp_pi
);
1672 /* Unintern existing, set to new. */
1673 bgp_attr_unintern(&tmp_pi
->attr
);
1674 tmp_pi
->attr
= attr_new
;
1675 tmp_pi
->uptime
= bgp_clock();
1679 /* Return back the route entry. */
1681 return route_change
;
1684 static void evpn_zebra_reinstall_best_route(struct bgp
*bgp
,
1685 struct bgpevpn
*vpn
, struct bgp_node
*rn
)
1687 struct bgp_path_info
*tmp_ri
;
1688 struct bgp_path_info
*curr_select
= NULL
;
1690 for (tmp_ri
= bgp_node_get_bgp_path_info(rn
);
1691 tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1692 if (CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_SELECTED
)) {
1693 curr_select
= tmp_ri
;
1698 if (curr_select
&& curr_select
->type
== ZEBRA_ROUTE_BGP
1699 && curr_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1700 evpn_zebra_install(bgp
, vpn
,
1701 (struct prefix_evpn
*)&rn
->p
,
1706 * If the local route was not selected evict it and tell zebra to re-add
1707 * the best remote dest.
1709 * Typically a local path added by zebra is expected to be selected as
1710 * best. In which case when a remote path wins as best (later)
1711 * evpn_route_select_install itself evicts the older-local-best path.
1713 * However if bgp's add and zebra's add cross paths (race condition) it
1714 * is possible that the local path is no longer the "older" best path.
1715 * It is a path that was never designated as best and hence requires
1716 * additional handling to prevent bgp from injecting and holding on to a
1717 * non-best local path.
1719 static void evpn_cleanup_local_non_best_route(struct bgp
*bgp
,
1720 struct bgpevpn
*vpn
,
1721 struct bgp_node
*rn
,
1722 struct bgp_path_info
*local_pi
)
1724 char buf
[PREFIX_STRLEN
];
1726 /* local path was not picked as the winner; kick it out */
1727 if (bgp_debug_zebra(NULL
)) {
1728 zlog_debug("evicting local evpn prefix %s as remote won",
1729 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
1731 evpn_delete_old_local_route(bgp
, vpn
, rn
, local_pi
);
1732 bgp_path_info_reap(rn
, local_pi
);
1734 /* tell zebra to re-add the best remote path */
1735 evpn_zebra_reinstall_best_route(bgp
, vpn
, rn
);
1739 * Create or update EVPN route (of type based on prefix) for specified VNI
1740 * and schedule for processing.
1742 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1743 struct prefix_evpn
*p
, uint8_t flags
,
1746 struct bgp_node
*rn
;
1748 struct attr
*attr_new
;
1749 int add_l3_ecomm
= 0;
1750 struct bgp_path_info
*pi
;
1751 afi_t afi
= AFI_L2VPN
;
1752 safi_t safi
= SAFI_EVPN
;
1755 memset(&attr
, 0, sizeof(struct attr
));
1757 /* Build path-attribute for this route. */
1758 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1759 attr
.nexthop
= vpn
->originator_ip
;
1760 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1761 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1762 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1763 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1764 attr
.router_flag
= CHECK_FLAG(flags
,
1765 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1767 /* PMSI is only needed for type-3 routes */
1768 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
1769 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1770 attr
.pmsi_tnl_type
= PMSI_TNLTYPE_INGR_REPL
;
1773 /* router mac is only needed for type-2 routes here. */
1774 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1775 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1776 vni2label(vpn
->vni
, &(attr
.label
));
1778 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1779 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1782 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1783 (is_evpn_prefix_ipaddr_v4(p
) ||
1784 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1785 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1786 bgpevpn_get_l3vni(vpn
))
1789 /* Set up extended community. */
1790 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1792 /* First, create (or fetch) route node within the VNI. */
1793 /* NOTE: There is no RD here. */
1794 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1796 /* Create or update route entry. */
1797 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1798 1, &pi
, flags
, seq
);
1800 attr_new
= pi
->attr
;
1802 /* lock ri to prevent freeing in evpn_route_select_install */
1803 bgp_path_info_lock(pi
);
1804 /* Perform route selection; this is just to set the flags correctly
1805 * as local route in the VNI always wins.
1807 evpn_route_select_install(bgp
, vpn
, rn
);
1809 * If the new local route was not selected evict it and tell zebra
1810 * to re-add the best remote dest. BGP doesn't retain non-best local
1813 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1815 evpn_cleanup_local_non_best_route(bgp
, vpn
, rn
, pi
);
1817 bgp_path_info_unlock(pi
);
1819 bgp_unlock_node(rn
);
1821 /* If this is a new route or some attribute has changed, export the
1822 * route to the global table. The route will be advertised to peers
1823 * from there. Note that this table is a 2-level tree (RD-level +
1824 * Prefix-level) similar to L3VPN routes.
1827 struct bgp_path_info
*global_pi
;
1829 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1830 (struct prefix
*)p
, &vpn
->prd
);
1831 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1,
1832 &global_pi
, flags
, seq
);
1834 /* Schedule for processing and unlock node. */
1835 bgp_process(bgp
, rn
, afi
, safi
);
1836 bgp_unlock_node(rn
);
1839 /* Unintern temporary. */
1840 aspath_unintern(&attr
.aspath
);
1846 * Delete EVPN route entry.
1847 * The entry can be in ESI/VNI table or the global table.
1849 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1850 struct bgp_node
*rn
,
1851 struct bgp_path_info
**pi
)
1853 struct bgp_path_info
*tmp_pi
;
1857 /* Now, find matching route. */
1858 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1859 tmp_pi
= tmp_pi
->next
)
1860 if (tmp_pi
->peer
== bgp
->peer_self
1861 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1862 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1867 /* Mark route for delete. */
1869 bgp_path_info_delete(rn
, tmp_pi
);
1874 /* Delete EVPN ES (type-4) route */
1875 static int delete_evpn_type4_route(struct bgp
*bgp
,
1877 struct prefix_evpn
*p
)
1879 afi_t afi
= AFI_L2VPN
;
1880 safi_t safi
= SAFI_EVPN
;
1881 struct bgp_path_info
*pi
;
1882 struct bgp_node
*rn
= NULL
; /* rn in esi table */
1883 struct bgp_node
*global_rn
= NULL
; /* rn in global table */
1885 /* First, locate the route node within the ESI.
1886 * If it doesn't exist, ther is nothing to do.
1887 * Note: there is no RD here.
1889 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
1893 /* Next, locate route node in the global EVPN routing table.
1894 * Note that this table is a 2-level tree (RD-level + Prefix-level)
1896 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1897 (struct prefix
*)p
, &es
->prd
);
1900 /* Delete route entry in the global EVPN table. */
1901 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1903 /* Schedule for processing - withdraws to peers happen from
1907 bgp_process(bgp
, global_rn
, afi
, safi
);
1908 bgp_unlock_node(global_rn
);
1912 * Delete route entry in the ESI route table.
1913 * This can just be removed.
1915 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
1917 bgp_path_info_reap(rn
, pi
);
1918 bgp_unlock_node(rn
);
1922 /* Delete EVPN type5 route */
1923 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1925 afi_t afi
= AFI_L2VPN
;
1926 safi_t safi
= SAFI_EVPN
;
1927 struct bgp_node
*rn
= NULL
;
1928 struct bgp_path_info
*pi
= NULL
;
1929 struct bgp
*bgp_evpn
= NULL
; /* evpn bgp instance */
1931 bgp_evpn
= bgp_get_evpn();
1935 /* locate the global route entry for this type-5 prefix */
1936 rn
= bgp_afi_node_lookup(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1937 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1941 delete_evpn_route_entry(bgp_evpn
, afi
, safi
, rn
, &pi
);
1943 bgp_process(bgp_evpn
, rn
, afi
, safi
);
1944 bgp_unlock_node(rn
);
1949 * Delete EVPN route (of type based on prefix) for specified VNI and
1950 * schedule for processing.
1952 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1953 struct prefix_evpn
*p
)
1955 struct bgp_node
*rn
, *global_rn
;
1956 struct bgp_path_info
*pi
;
1957 afi_t afi
= AFI_L2VPN
;
1958 safi_t safi
= SAFI_EVPN
;
1960 /* First, locate the route node within the VNI. If it doesn't exist,
1962 * is nothing further to do.
1964 /* NOTE: There is no RD here. */
1965 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1969 /* Next, locate route node in the global EVPN routing table. Note that
1970 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1973 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1974 (struct prefix
*)p
, &vpn
->prd
);
1976 /* Delete route entry in the global EVPN table. */
1977 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1979 /* Schedule for processing - withdraws to peers happen from
1983 bgp_process(bgp
, global_rn
, afi
, safi
);
1984 bgp_unlock_node(global_rn
);
1987 /* Delete route entry in the VNI route table. This can just be removed.
1989 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
1991 bgp_path_info_reap(rn
, pi
);
1992 evpn_route_select_install(bgp
, vpn
, rn
);
1994 bgp_unlock_node(rn
);
2000 * Update all type-2 (MACIP) local routes for this VNI - these should also
2001 * be scheduled for advertise to peers.
2003 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2007 struct bgp_node
*rn
;
2008 struct bgp_path_info
*pi
, *tmp_pi
;
2010 struct attr
*attr_new
;
2012 int add_l3_ecomm
= 0;
2017 /* Walk this VNI's route table and update local type-2 routes. For any
2018 * routes updated, update corresponding entry in the global table too.
2020 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2021 rn
= bgp_route_next(rn
)) {
2022 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2023 struct bgp_node
*rd_rn
;
2024 struct bgp_path_info
*global_pi
;
2026 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2029 /* Identify local route. */
2030 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
2031 tmp_pi
= tmp_pi
->next
) {
2032 if (tmp_pi
->peer
== bgp
->peer_self
2033 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2034 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2042 * Build attribute per local route as the MAC mobility and
2043 * some other values could differ for different routes. The
2044 * attributes will be shared in the hash table.
2046 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
2047 attr
.nexthop
= vpn
->originator_ip
;
2048 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
2049 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
2050 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
2052 if (evpn_route_is_sticky(bgp
, rn
))
2054 else if (evpn_route_is_def_gw(bgp
, rn
)) {
2055 attr
.default_gw
= 1;
2056 if (is_evpn_prefix_ipaddr_v6(evp
))
2057 attr
.router_flag
= 1;
2060 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2061 * using L3 VNI for type-2 routes also.
2063 if ((is_evpn_prefix_ipaddr_v4(evp
) ||
2064 !IN6_IS_ADDR_LINKLOCAL(
2065 &evp
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
2066 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
2067 bgpevpn_get_l3vni(vpn
))
2070 /* Set up extended community. */
2071 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2073 seq
= mac_mobility_seqnum(tmp_pi
->attr
);
2075 /* Update the route entry. */
2076 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
, 0, &pi
,
2079 /* Perform route selection; this is just to set the flags
2080 * correctly as local route in the VNI always wins.
2082 evpn_route_select_install(bgp
, vpn
, rn
);
2084 attr_new
= pi
->attr
;
2086 /* Update route in global routing table. */
2087 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2088 (struct prefix
*)evp
, &vpn
->prd
);
2090 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
2092 mac_mobility_seqnum(attr_new
));
2094 /* Schedule for processing and unlock node. */
2095 bgp_process(bgp
, rd_rn
, afi
, safi
);
2096 bgp_unlock_node(rd_rn
);
2098 /* Unintern temporary. */
2099 aspath_unintern(&attr
.aspath
);
2107 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2108 * global routing table. These are also scheduled for withdraw from peers.
2110 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2114 struct bgp_node
*rdrn
, *rn
;
2115 struct bgp_table
*table
;
2116 struct bgp_path_info
*pi
;
2121 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
2122 if (rdrn
&& bgp_node_has_bgp_path_info_data(rdrn
)) {
2123 table
= bgp_node_get_bgp_table_info(rdrn
);
2124 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2125 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2127 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2130 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2132 bgp_process(bgp
, rn
, afi
, safi
);
2136 /* Unlock RD node. */
2138 bgp_unlock_node(rdrn
);
2144 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2145 * table as well as the per-VNI route table.
2147 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2151 struct bgp_node
*rn
;
2152 struct bgp_path_info
*pi
;
2157 /* First, walk the global route table for this VNI's type-2 local
2159 * EVPN routes are a 2-level table, first get the RD table.
2161 delete_global_type2_routes(bgp
, vpn
);
2163 /* Next, walk this VNI's route table and delete local type-2 routes. */
2164 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2165 rn
= bgp_route_next(rn
)) {
2166 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2168 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2171 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2173 /* Route entry in local table gets deleted immediately. */
2175 bgp_path_info_reap(rn
, pi
);
2182 * Delete all routes in per ES route-table
2184 static int delete_all_es_routes(struct bgp
*bgp
, struct evpnes
*es
)
2186 struct bgp_node
*rn
;
2187 struct bgp_path_info
*pi
, *nextpi
;
2189 /* Walk this ES's route table and delete all routes. */
2190 for (rn
= bgp_table_top(es
->route_table
); rn
;
2191 rn
= bgp_route_next(rn
)) {
2192 for (pi
= bgp_node_get_bgp_path_info(rn
);
2193 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2194 bgp_path_info_delete(rn
, pi
);
2195 bgp_path_info_reap(rn
, pi
);
2203 * Delete all routes in the per-VNI route table.
2205 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2207 struct bgp_node
*rn
;
2208 struct bgp_path_info
*pi
, *nextpi
;
2210 /* Walk this VNI's route table and delete all routes. */
2211 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2212 rn
= bgp_route_next(rn
)) {
2213 for (pi
= bgp_node_get_bgp_path_info(rn
);
2214 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2215 bgp_path_info_delete(rn
, pi
);
2216 bgp_path_info_reap(rn
, pi
);
2224 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2225 * export RT getting modified or change to tunnel IP. Note that these
2226 * situations need the route in the per-VNI table as well as the global
2227 * table to be updated (as attributes change).
2229 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2232 struct prefix_evpn p
;
2234 /* Update and advertise the type-3 route (only one) followed by the
2235 * locally learnt type-2 routes (MACIP) - for this VNI.
2237 * RT-3 only if doing head-end replication
2239 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
2240 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2241 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0);
2246 return update_all_type2_routes(bgp
, vpn
);
2249 /* Delete (and withdraw) local routes for specified ES from global and ES table.
2250 * Also remove all other routes from the per ES table.
2251 * Invoked when ES is deleted.
2253 static int delete_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
2256 char buf
[ESI_STR_LEN
];
2257 struct prefix_evpn p
;
2259 /* Delete and withdraw locally learnt ES route */
2260 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
.ipaddr_v4
);
2261 ret
= delete_evpn_type4_route(bgp
, es
, &p
);
2263 flog_err(EC_BGP_EVPN_ROUTE_DELETE
,
2264 "%u failed to delete type-4 route for ESI %s",
2265 bgp
->vrf_id
, esi_to_str(&es
->esi
, buf
, sizeof(buf
)));
2268 /* Delete all routes from per ES table */
2269 return delete_all_es_routes(bgp
, es
);
2273 * Delete (and withdraw) local routes for specified VNI from the global
2274 * table and per-VNI table. After this, remove all other routes from
2275 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2276 * (advertise-all-vni) being disabled.
2278 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2281 struct prefix_evpn p
;
2283 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2284 * followed by type-3 routes (only one) - for this VNI.
2286 ret
= delete_all_type2_routes(bgp
, vpn
);
2290 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2291 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2295 /* Delete all routes from the per-VNI table. */
2296 return delete_all_vni_routes(bgp
, vpn
);
2300 * There is a tunnel endpoint IP address change for this VNI, delete
2301 * prior type-3 route (if needed) and update.
2302 * Note: Route re-advertisement happens elsewhere after other processing
2305 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2306 struct in_addr originator_ip
)
2308 struct prefix_evpn p
;
2310 /* If VNI is not live, we only need to update the originator ip */
2311 if (!is_vni_live(vpn
)) {
2312 vpn
->originator_ip
= originator_ip
;
2316 /* Update the tunnel-ip hash */
2317 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2318 bgp_tip_add(bgp
, &originator_ip
);
2320 /* filter routes as martian nexthop db has changed */
2321 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2323 /* Need to withdraw type-3 route as the originator IP is part
2326 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2327 delete_evpn_route(bgp
, vpn
, &p
);
2329 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2330 vpn
->originator_ip
= originator_ip
;
2334 /* Install EVPN route entry in ES */
2335 static int install_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2336 struct prefix_evpn
*p
,
2337 struct bgp_path_info
*parent_pi
)
2340 struct bgp_node
*rn
= NULL
;
2341 struct bgp_path_info
*pi
= NULL
;
2342 struct attr
*attr_new
= NULL
;
2344 /* Create (or fetch) route within the VNI.
2345 * NOTE: There is no RD here.
2347 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
2349 /* Check if route entry is already present. */
2350 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2352 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2356 /* Add (or update) attribute to hash. */
2357 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2359 /* Create new route with its attribute. */
2360 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2361 parent_pi
->peer
, attr_new
, rn
);
2362 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2363 bgp_path_info_extra_get(pi
);
2364 pi
->extra
->parent
= parent_pi
;
2365 bgp_path_info_add(rn
, pi
);
2367 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2368 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2369 bgp_unlock_node(rn
);
2372 /* The attribute has changed. */
2373 /* Add (or update) attribute to hash. */
2374 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2376 /* Restore route, if needed. */
2377 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2378 bgp_path_info_restore(rn
, pi
);
2380 /* Mark if nexthop has changed. */
2381 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2382 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2384 /* Unintern existing, set to new. */
2385 bgp_attr_unintern(&pi
->attr
);
2386 pi
->attr
= attr_new
;
2387 pi
->uptime
= bgp_clock();
2390 /* Perform route selection and update zebra, if required. */
2391 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2396 * Install route entry into the VRF routing table and invoke route selection.
2398 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2399 struct prefix_evpn
*evp
,
2400 struct bgp_path_info
*parent_pi
)
2402 struct bgp_node
*rn
;
2403 struct bgp_path_info
*pi
;
2405 struct attr
*attr_new
;
2408 struct prefix
*pp
= &p
;
2411 char buf
[PREFIX_STRLEN
];
2412 char buf1
[PREFIX_STRLEN
];
2414 memset(pp
, 0, sizeof(struct prefix
));
2415 ip_prefix_from_evpn_prefix(evp
, pp
);
2417 if (bgp_debug_zebra(NULL
)) {
2419 "installing evpn prefix %s as ip prefix %s in vrf %s",
2420 prefix2str(evp
, buf
, sizeof(buf
)),
2421 prefix2str(pp
, buf1
, sizeof(buf
)),
2422 vrf_id_to_name(bgp_vrf
->vrf_id
));
2425 /* Create (or fetch) route within the VRF. */
2426 /* NOTE: There is no RD here. */
2427 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2429 safi
= SAFI_UNICAST
;
2430 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2431 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2433 safi
= SAFI_UNICAST
;
2434 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2438 /* EVPN routes currently only support a IPv4 next hop which corresponds
2439 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2440 * or prefix route, we have to convert the next hop to an IPv4-mapped
2441 * address for the rest of the code to flow through. In the case of IPv4,
2442 * make sure to set the flag for next hop attribute.
2444 bgp_attr_dup(&attr
, parent_pi
->attr
);
2446 evpn_convert_nexthop_to_ipv6(&attr
);
2448 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2450 /* Check if route entry is already present. */
2451 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2453 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2457 /* Add (or update) attribute to hash. */
2458 attr_new
= bgp_attr_intern(&attr
);
2460 /* Create new route with its attribute. */
2461 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2462 parent_pi
->peer
, attr_new
, rn
);
2463 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2464 bgp_path_info_extra_get(pi
);
2465 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2466 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2467 if (parent_pi
->extra
) {
2468 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2469 sizeof(pi
->extra
->label
));
2470 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2472 bgp_path_info_add(rn
, pi
);
2474 if (attrhash_cmp(pi
->attr
, &attr
)
2475 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2476 bgp_unlock_node(rn
);
2479 /* The attribute has changed. */
2480 /* Add (or update) attribute to hash. */
2481 attr_new
= bgp_attr_intern(&attr
);
2483 /* Restore route, if needed. */
2484 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2485 bgp_path_info_restore(rn
, pi
);
2487 /* Mark if nexthop has changed. */
2489 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2491 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2492 &attr_new
->mp_nexthop_global
)))
2493 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2495 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_ATTR_CHANGED
);
2496 /* Unintern existing, set to new. */
2497 bgp_attr_unintern(&pi
->attr
);
2498 pi
->attr
= attr_new
;
2499 pi
->uptime
= bgp_clock();
2502 bgp_aggregate_increment(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2504 /* Perform route selection and update zebra, if required. */
2505 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2507 /* Process for route leaking. */
2508 vpn_leak_from_vrf_update(bgp_get_default(), bgp_vrf
, pi
);
2514 * Install route entry into the VNI routing table and invoke route selection.
2516 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2517 struct prefix_evpn
*p
,
2518 struct bgp_path_info
*parent_pi
)
2520 struct bgp_node
*rn
;
2521 struct bgp_path_info
*pi
;
2522 struct attr
*attr_new
;
2525 /* Create (or fetch) route within the VNI. */
2526 /* NOTE: There is no RD here. */
2527 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2529 /* Check if route entry is already present. */
2530 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2532 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2536 /* Add (or update) attribute to hash. */
2537 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2539 /* Create new route with its attribute. */
2540 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2541 parent_pi
->peer
, attr_new
, rn
);
2542 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2543 bgp_path_info_extra_get(pi
);
2544 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2545 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2546 if (parent_pi
->extra
) {
2547 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2548 sizeof(pi
->extra
->label
));
2549 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2551 bgp_path_info_add(rn
, pi
);
2553 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2554 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2555 bgp_unlock_node(rn
);
2558 /* The attribute has changed. */
2559 /* Add (or update) attribute to hash. */
2560 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2562 /* Restore route, if needed. */
2563 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2564 bgp_path_info_restore(rn
, pi
);
2566 /* Mark if nexthop has changed. */
2567 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2568 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2570 /* Unintern existing, set to new. */
2571 bgp_attr_unintern(&pi
->attr
);
2572 pi
->attr
= attr_new
;
2573 pi
->uptime
= bgp_clock();
2576 /* Perform route selection and update zebra, if required. */
2577 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2582 /* Uninstall EVPN route entry from ES route table */
2583 static int uninstall_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2584 struct prefix_evpn
*p
,
2585 struct bgp_path_info
*parent_pi
)
2588 struct bgp_node
*rn
;
2589 struct bgp_path_info
*pi
;
2591 if (!es
->route_table
)
2594 /* Locate route within the ESI.
2595 * NOTE: There is no RD here.
2597 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2601 /* Find matching route entry. */
2602 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2604 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2610 /* Mark entry for deletion */
2611 bgp_path_info_delete(rn
, pi
);
2613 /* Perform route selection and update zebra, if required. */
2614 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2616 /* Unlock route node. */
2617 bgp_unlock_node(rn
);
2623 * Uninstall route entry from the VRF routing table and send message
2624 * to zebra, if appropriate.
2626 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2627 struct prefix_evpn
*evp
,
2628 struct bgp_path_info
*parent_pi
)
2630 struct bgp_node
*rn
;
2631 struct bgp_path_info
*pi
;
2634 struct prefix
*pp
= &p
;
2637 char buf
[PREFIX_STRLEN
];
2638 char buf1
[PREFIX_STRLEN
];
2640 memset(pp
, 0, sizeof(struct prefix
));
2641 ip_prefix_from_evpn_prefix(evp
, pp
);
2643 if (bgp_debug_zebra(NULL
)) {
2645 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2646 prefix2str(evp
, buf
, sizeof(buf
)),
2647 prefix2str(pp
, buf1
, sizeof(buf
)),
2648 vrf_id_to_name(bgp_vrf
->vrf_id
));
2651 /* Locate route within the VRF. */
2652 /* NOTE: There is no RD here. */
2653 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2655 safi
= SAFI_UNICAST
;
2656 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2659 safi
= SAFI_UNICAST
;
2660 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2666 /* Find matching route entry. */
2667 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2669 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2675 /* Process for route leaking. */
2676 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp_vrf
, pi
);
2678 bgp_aggregate_decrement(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2680 /* Mark entry for deletion */
2681 bgp_path_info_delete(rn
, pi
);
2683 /* Perform route selection and update zebra, if required. */
2684 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2686 /* Unlock route node. */
2687 bgp_unlock_node(rn
);
2693 * Uninstall route entry from the VNI routing table and send message
2694 * to zebra, if appropriate.
2696 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2697 struct prefix_evpn
*p
,
2698 struct bgp_path_info
*parent_pi
)
2700 struct bgp_node
*rn
;
2701 struct bgp_path_info
*pi
;
2704 /* Locate route within the VNI. */
2705 /* NOTE: There is no RD here. */
2706 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2710 /* Find matching route entry. */
2711 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2713 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2719 /* Mark entry for deletion */
2720 bgp_path_info_delete(rn
, pi
);
2722 /* Perform route selection and update zebra, if required. */
2723 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2725 /* Unlock route node. */
2726 bgp_unlock_node(rn
);
2732 * Given a prefix, see if it belongs to ES.
2734 static int is_prefix_matching_for_es(struct prefix_evpn
*p
,
2737 /* if not an ES route return false */
2738 if (p
->prefix
.route_type
!= BGP_EVPN_ES_ROUTE
)
2741 if (memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
)) == 0)
2748 * Given a route entry and a VRF, see if this route entry should be
2749 * imported into the VRF i.e., RTs match.
2751 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2752 struct bgp_path_info
*pi
)
2754 struct attr
*attr
= pi
->attr
;
2755 struct ecommunity
*ecom
;
2759 /* Route should have valid RT to be even considered. */
2760 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2763 ecom
= attr
->ecommunity
;
2764 if (!ecom
|| !ecom
->size
)
2767 /* For each extended community RT, see if it matches this VNI. If any RT
2768 * matches, we're done.
2770 for (i
= 0; i
< ecom
->size
; i
++) {
2772 uint8_t type
, sub_type
;
2773 struct ecommunity_val
*eval
;
2774 struct ecommunity_val eval_tmp
;
2775 struct vrf_irt_node
*irt
;
2777 /* Only deal with RTs */
2778 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2779 eval
= (struct ecommunity_val
*)(ecom
->val
2780 + (i
* ECOMMUNITY_SIZE
));
2783 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2786 /* See if this RT matches specified VNIs import RTs */
2787 irt
= lookup_vrf_import_rt(eval
);
2789 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2792 /* Also check for non-exact match. In this, we mask out the AS
2794 * only check on the local-admin sub-field. This is to
2796 * VNI as the RT for EBGP peering too.
2799 if (type
== ECOMMUNITY_ENCODE_AS
2800 || type
== ECOMMUNITY_ENCODE_AS4
2801 || type
== ECOMMUNITY_ENCODE_IP
) {
2802 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2803 mask_ecom_global_admin(&eval_tmp
, eval
);
2804 irt
= lookup_vrf_import_rt(&eval_tmp
);
2807 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2815 * Given a route entry and a VNI, see if this route entry should be
2816 * imported into the VNI i.e., RTs match.
2818 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2819 struct bgp_path_info
*pi
)
2821 struct attr
*attr
= pi
->attr
;
2822 struct ecommunity
*ecom
;
2826 /* Route should have valid RT to be even considered. */
2827 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2830 ecom
= attr
->ecommunity
;
2831 if (!ecom
|| !ecom
->size
)
2834 /* For each extended community RT, see if it matches this VNI. If any RT
2835 * matches, we're done.
2837 for (i
= 0; i
< ecom
->size
; i
++) {
2839 uint8_t type
, sub_type
;
2840 struct ecommunity_val
*eval
;
2841 struct ecommunity_val eval_tmp
;
2842 struct irt_node
*irt
;
2844 /* Only deal with RTs */
2845 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2846 eval
= (struct ecommunity_val
*)(ecom
->val
2847 + (i
* ECOMMUNITY_SIZE
));
2850 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2853 /* See if this RT matches specified VNIs import RTs */
2854 irt
= lookup_import_rt(bgp
, eval
);
2856 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2859 /* Also check for non-exact match. In this, we mask out the AS
2861 * only check on the local-admin sub-field. This is to
2863 * VNI as the RT for EBGP peering too.
2866 if (type
== ECOMMUNITY_ENCODE_AS
2867 || type
== ECOMMUNITY_ENCODE_AS4
2868 || type
== ECOMMUNITY_ENCODE_IP
) {
2869 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2870 mask_ecom_global_admin(&eval_tmp
, eval
);
2871 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2874 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2881 static int install_uninstall_routes_for_es(struct bgp
*bgp
,
2888 char buf
[PREFIX_STRLEN
];
2889 char buf1
[ESI_STR_LEN
];
2890 struct bgp_node
*rd_rn
, *rn
;
2891 struct bgp_table
*table
;
2892 struct bgp_path_info
*pi
;
2898 * Walk entire global routing table and evaluate routes which could be
2899 * imported into this VRF. Note that we need to loop through all global
2900 * routes to determine which route matches the import rt on vrf
2902 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2903 rd_rn
= bgp_route_next(rd_rn
)) {
2904 table
= bgp_node_get_bgp_table_info(rd_rn
);
2908 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2909 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2911 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
2914 * Consider "valid" remote routes applicable for
2917 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
2918 && pi
->type
== ZEBRA_ROUTE_BGP
2919 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
2922 if (!is_prefix_matching_for_es(evp
, es
))
2926 ret
= install_evpn_route_entry_in_es(
2929 ret
= uninstall_evpn_route_entry_in_es(
2935 "Failed to %s EVPN %s route in ESI %s",
2938 prefix2str(evp
, buf
,
2940 esi_to_str(&es
->esi
, buf1
,
2950 /* This API will scan evpn routes for checking attribute's rmac
2951 * macthes with bgp instance router mac. It avoid installing
2952 * route into bgp vrf table and remote rmac in bridge table.
2954 static int bgp_evpn_route_rmac_self_check(struct bgp
*bgp_vrf
,
2955 struct prefix_evpn
*evp
,
2956 struct bgp_path_info
*pi
)
2958 /* evpn route could have learnt prior to L3vni has come up,
2959 * perform rmac check before installing route and
2960 * remote router mac.
2961 * The route will be removed from global bgp table once
2962 * SVI comes up with MAC and stored in hash, triggers
2963 * bgp_mac_rescan_all_evpn_tables.
2966 memcmp(&bgp_vrf
->rmac
, &pi
->attr
->rmac
, ETH_ALEN
) == 0) {
2967 if (bgp_debug_update(pi
->peer
, NULL
, NULL
, 1)) {
2968 char buf1
[PREFIX_STRLEN
];
2969 char attr_str
[BUFSIZ
] = {0};
2971 bgp_dump_attr(pi
->attr
, attr_str
, BUFSIZ
);
2973 zlog_debug("%s: bgp %u prefix %s with attr %s - DENIED due to self mac",
2974 __func__
, bgp_vrf
->vrf_id
,
2975 prefix2str(evp
, buf1
, sizeof(buf1
)),
2986 * Install or uninstall mac-ip routes are appropriate for this
2989 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
2993 struct bgp_node
*rd_rn
, *rn
;
2994 struct bgp_table
*table
;
2995 struct bgp_path_info
*pi
;
2997 char buf
[PREFIX_STRLEN
];
2998 struct bgp
*bgp_evpn
= NULL
;
3002 bgp_evpn
= bgp_get_evpn();
3006 /* Walk entire global routing table and evaluate routes which could be
3007 * imported into this VRF. Note that we need to loop through all global
3008 * routes to determine which route matches the import rt on vrf
3010 for (rd_rn
= bgp_table_top(bgp_evpn
->rib
[afi
][safi
]); rd_rn
;
3011 rd_rn
= bgp_route_next(rd_rn
)) {
3012 table
= bgp_node_get_bgp_table_info(rd_rn
);
3016 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3017 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3019 /* if not mac-ip route skip this route */
3020 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3021 || evp
->prefix
.route_type
3022 == BGP_EVPN_IP_PREFIX_ROUTE
))
3025 /* if not a mac+ip route skip this route */
3026 if (!(is_evpn_prefix_ipaddr_v4(evp
)
3027 || is_evpn_prefix_ipaddr_v6(evp
)))
3030 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3032 /* Consider "valid" remote routes applicable for
3035 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3036 && pi
->type
== ZEBRA_ROUTE_BGP
3037 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3040 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
3041 if (bgp_evpn_route_rmac_self_check(
3046 ret
= install_evpn_route_entry_in_vrf(
3049 ret
= uninstall_evpn_route_entry_in_vrf(
3055 "Failed to %s EVPN %s route in VRF %s",
3058 prefix2str(evp
, buf
,
3073 * Install or uninstall routes of specified type that are appropriate for this
3076 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
3077 struct bgpevpn
*vpn
,
3078 bgp_evpn_route_type rtype
,
3083 struct bgp_node
*rd_rn
, *rn
;
3084 struct bgp_table
*table
;
3085 struct bgp_path_info
*pi
;
3091 /* Walk entire global routing table and evaluate routes which could be
3092 * imported into this VPN. Note that we cannot just look at the routes
3094 * the VNI's RD - remote routes applicable for this VNI could have any
3097 /* EVPN routes are a 2-level table. */
3098 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
3099 rd_rn
= bgp_route_next(rd_rn
)) {
3100 table
= bgp_node_get_bgp_table_info(rd_rn
);
3104 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3105 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3107 if (evp
->prefix
.route_type
!= rtype
)
3110 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3112 /* Consider "valid" remote routes applicable for
3114 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3115 && pi
->type
== ZEBRA_ROUTE_BGP
3116 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3119 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3121 ret
= install_evpn_route_entry(
3124 ret
= uninstall_evpn_route_entry(
3130 "%u: Failed to %s EVPN %s route in VNI %u",
3134 rtype
== BGP_EVPN_MAC_IP_ROUTE
3148 /* Install any existing remote ES routes applicable for this ES into its routing
3149 * table. This is invoked when ES comes up.
3151 static int install_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
3153 return install_uninstall_routes_for_es(bgp
, es
, 1);
3157 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3158 * is invoked upon l3vni-add or l3vni import rt change
3160 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3162 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3167 * Install any existing remote routes applicable for this VNI into its
3168 * routing table. This is invoked when a VNI becomes "live" or its Import
3171 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3175 /* Install type-3 routes followed by type-2 routes - the ones applicable
3178 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3183 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3187 /* uninstall routes from l3vni vrf. */
3188 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3190 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3195 * Uninstall any existing remote routes for this VNI. One scenario in which
3196 * this is invoked is upon an import RT change.
3198 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3202 /* Uninstall type-2 routes followed by type-3 routes - the ones
3206 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3211 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3215 /* Install or unistall route in ES */
3216 static int install_uninstall_route_in_es(struct bgp
*bgp
, struct evpnes
*es
,
3217 afi_t afi
, safi_t safi
,
3218 struct prefix_evpn
*evp
,
3219 struct bgp_path_info
*pi
, int install
)
3222 char buf
[ESI_STR_LEN
];
3225 ret
= install_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3227 ret
= uninstall_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3232 "%u: Failed to %s EVPN %s route in ESI %s", bgp
->vrf_id
,
3233 install
? "install" : "uninstall", "ES",
3234 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)));
3241 * Install or uninstall route in matching VRFs (list).
3243 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3244 safi_t safi
, struct prefix_evpn
*evp
,
3245 struct bgp_path_info
*pi
,
3246 struct list
*vrfs
, int install
)
3248 char buf
[PREFIX2STR_BUFFER
];
3249 struct bgp
*bgp_vrf
;
3250 struct listnode
*node
, *nnode
;
3252 /* Only type-2/type-5 routes go into a VRF */
3253 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3254 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3257 /* if it is type-2 route and not a mac+ip route skip this route */
3258 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3259 && !(is_evpn_prefix_ipaddr_v4(evp
)
3260 || is_evpn_prefix_ipaddr_v6(evp
)))
3263 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3267 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3269 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3273 flog_err(EC_BGP_EVPN_FAIL
,
3274 "%u: Failed to %s prefix %s in VRF %s",
3276 install
? "install" : "uninstall",
3277 prefix2str(evp
, buf
, sizeof(buf
)),
3278 vrf_id_to_name(bgp_vrf
->vrf_id
));
3287 * Install or uninstall route in matching VNIs (list).
3289 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3290 safi_t safi
, struct prefix_evpn
*evp
,
3291 struct bgp_path_info
*pi
,
3292 struct list
*vnis
, int install
)
3294 struct bgpevpn
*vpn
;
3295 struct listnode
*node
, *nnode
;
3297 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3300 if (!is_vni_live(vpn
))
3304 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3306 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3309 flog_err(EC_BGP_EVPN_FAIL
,
3310 "%u: Failed to %s EVPN %s route in VNI %u",
3311 bgp
->vrf_id
, install
? "install" : "uninstall",
3312 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3324 * Install or uninstall route for appropriate VNIs/ESIs.
3326 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3328 struct bgp_path_info
*pi
, int import
)
3330 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3331 struct attr
*attr
= pi
->attr
;
3332 struct ecommunity
*ecom
;
3337 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3338 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3339 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3340 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3341 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3344 /* If we don't have Route Target, nothing much to do. */
3345 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3348 ecom
= attr
->ecommunity
;
3349 if (!ecom
|| !ecom
->size
)
3352 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3353 * attached to the route */
3354 for (i
= 0; i
< ecom
->size
; i
++) {
3356 uint8_t type
, sub_type
;
3357 struct ecommunity_val
*eval
;
3358 struct ecommunity_val eval_tmp
;
3359 struct irt_node
*irt
; /* import rt for l2vni */
3360 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3363 /* Only deal with RTs */
3364 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3365 eval
= (struct ecommunity_val
*)(ecom
->val
3366 + (i
* ECOMMUNITY_SIZE
));
3369 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3373 * macip routes (type-2) are imported into VNI and VRF tables.
3374 * IMET route is imported into VNI table.
3375 * prefix routes are imported into VRF table.
3377 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3378 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3379 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3381 irt
= lookup_import_rt(bgp
, eval
);
3383 install_uninstall_route_in_vnis(
3384 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3387 vrf_irt
= lookup_vrf_import_rt(eval
);
3389 install_uninstall_route_in_vrfs(
3390 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3393 /* Also check for non-exact match.
3394 * In this, we mask out the AS and
3395 * only check on the local-admin sub-field.
3396 * This is to facilitate using
3397 * VNI as the RT for EBGP peering too.
3401 if (type
== ECOMMUNITY_ENCODE_AS
3402 || type
== ECOMMUNITY_ENCODE_AS4
3403 || type
== ECOMMUNITY_ENCODE_IP
) {
3404 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3405 mask_ecom_global_admin(&eval_tmp
, eval
);
3406 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3407 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3411 install_uninstall_route_in_vnis(
3412 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3415 install_uninstall_route_in_vrfs(
3416 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3420 /* es route is imported into the es table */
3421 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3423 /* we will match based on the entire esi to avoid
3424 * imoort of an es route for esi2 into esi1
3426 es
= bgp_evpn_lookup_es(bgp
, &evp
->prefix
.es_addr
.esi
);
3427 if (es
&& is_es_local(es
))
3428 install_uninstall_route_in_es(
3429 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3437 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3440 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3442 /* Delete ipv4 default route and withdraw from peers */
3443 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3444 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3445 SAFI_UNICAST
, false);
3447 /* delete all ipv4 routes and withdraw from peers */
3448 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3449 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3451 /* Delete ipv6 default route and withdraw from peers */
3452 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3453 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3454 SAFI_UNICAST
, false);
3456 /* delete all ipv6 routes and withdraw from peers */
3457 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3458 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3462 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3465 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3467 /* update all ipv4 routes */
3468 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3469 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3471 /* update ipv4 default route and withdraw from peers */
3472 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3473 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3474 SAFI_UNICAST
, true);
3476 /* update all ipv6 routes */
3477 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3478 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3480 /* update ipv6 default route and withdraw from peers */
3481 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3482 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3483 SAFI_UNICAST
, true);
3488 * update and advertise local routes for a VRF as type-5 routes.
3489 * This is invoked upon RD change for a VRF. Note taht the processing is only
3490 * done in the global route table using the routes which already exist in the
3493 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3495 /* skip if the RD is configured */
3496 if (is_vrf_rd_configured(bgp_vrf
))
3499 /* derive the RD for the VRF based on new router-id */
3500 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3502 /* update advertise ipv4|ipv6 routes as type-5 routes */
3503 update_advertise_vrf_routes(bgp_vrf
);
3507 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3508 * This is invoked upon VRF RD change. The processing is done only from global
3511 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3513 /* skip if the RD is configured */
3514 if (is_vrf_rd_configured(bgp_vrf
))
3517 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3518 delete_withdraw_vrf_routes(bgp_vrf
);
3522 * Update and advertise local routes for a VNI. Invoked upon router-id
3523 * change. Note that the processing is done only on the global route table
3524 * using routes that already exist in the per-VNI table.
3526 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3528 struct prefix_evpn p
;
3529 struct bgp_node
*rn
, *global_rn
;
3530 struct bgp_path_info
*pi
, *global_pi
;
3532 afi_t afi
= AFI_L2VPN
;
3533 safi_t safi
= SAFI_EVPN
;
3535 /* Locate type-3 route for VNI in the per-VNI table and use its
3536 * attributes to create and advertise the type-3 route for this VNI
3537 * in the global table.
3539 * RT-3 only if doing head-end replication
3541 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
3542 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3543 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3544 if (!rn
) /* unexpected */
3546 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3547 if (pi
->peer
== bgp
->peer_self
&&
3548 pi
->type
== ZEBRA_ROUTE_BGP
3549 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3551 if (!pi
) /* unexpected */
3555 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3556 (struct prefix
*)&p
, &vpn
->prd
);
3557 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
,
3558 1, &pi
, 0, mac_mobility_seqnum(attr
));
3560 /* Schedule for processing and unlock node. */
3561 bgp_process(bgp
, global_rn
, afi
, safi
);
3562 bgp_unlock_node(global_rn
);
3565 /* Now, walk this VNI's route table and use the route and its attribute
3566 * to create and schedule route in global table.
3568 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
3569 rn
= bgp_route_next(rn
)) {
3570 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3572 /* Identify MAC-IP local routes. */
3573 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3576 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3577 if (pi
->peer
== bgp
->peer_self
3578 && pi
->type
== ZEBRA_ROUTE_BGP
3579 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3584 /* Create route in global routing table using this route entry's
3588 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3589 (struct prefix
*)evp
, &vpn
->prd
);
3591 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
3593 mac_mobility_seqnum(attr
));
3595 /* Schedule for processing and unlock node. */
3596 bgp_process(bgp
, global_rn
, afi
, safi
);
3597 bgp_unlock_node(global_rn
);
3604 * Delete (and withdraw) local routes for a VNI - only from the global
3605 * table. Invoked upon router-id change.
3607 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3610 struct prefix_evpn p
;
3611 struct bgp_node
*global_rn
;
3612 struct bgp_path_info
*pi
;
3613 afi_t afi
= AFI_L2VPN
;
3614 safi_t safi
= SAFI_EVPN
;
3616 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3617 * for this VNI - from the global table.
3619 ret
= delete_global_type2_routes(bgp
, vpn
);
3623 /* Remove type-3 route for this VNI from global table. */
3624 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3625 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3626 (struct prefix
*)&p
, &vpn
->prd
);
3628 /* Delete route entry in the global EVPN table. */
3629 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
3631 /* Schedule for processing - withdraws to peers happen from
3635 bgp_process(bgp
, global_rn
, afi
, safi
);
3636 bgp_unlock_node(global_rn
);
3643 * Handle router-id change. Update and advertise local routes corresponding
3644 * to this VNI from peers. Note that this is invoked after updating the
3645 * router-id. The routes in the per-VNI table are used to create routes in
3646 * the global table and schedule them.
3648 static void update_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3650 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3652 /* Skip VNIs with configured RD. */
3653 if (is_rd_configured(vpn
))
3656 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3657 update_advertise_vni_routes(bgp
, vpn
);
3661 * Handle router-id change. Delete and withdraw local routes corresponding
3662 * to this VNI from peers. Note that this is invoked prior to updating
3663 * the router-id and is done only on the global route table, the routes
3664 * are needed in the per-VNI table to re-advertise with new router id.
3666 static void withdraw_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3668 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3670 /* Skip VNIs with configured RD. */
3671 if (is_rd_configured(vpn
))
3674 delete_withdraw_vni_routes(bgp
, vpn
);
3678 * Create RT-3 for a VNI and schedule for processing and advertisement.
3679 * This is invoked upon flooding mode changing to head-end replication.
3681 static void create_advertise_type3(struct hash_bucket
*bucket
, void *data
)
3683 struct bgpevpn
*vpn
= bucket
->data
;
3684 struct bgp
*bgp
= data
;
3685 struct prefix_evpn p
;
3687 if (!vpn
|| !is_vni_live(vpn
))
3690 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3691 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0))
3692 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3693 "Type3 route creation failure for VNI %u", vpn
->vni
);
3697 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3698 * This is invoked upon flooding mode changing to drop BUM packets.
3700 static void delete_withdraw_type3(struct hash_bucket
*bucket
, void *data
)
3702 struct bgpevpn
*vpn
= bucket
->data
;
3703 struct bgp
*bgp
= data
;
3704 struct prefix_evpn p
;
3706 if (!vpn
|| !is_vni_live(vpn
))
3709 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3710 delete_evpn_route(bgp
, vpn
, &p
);
3714 * Process received EVPN type-2 route (advertise or withdraw).
3716 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3717 struct attr
*attr
, uint8_t *pfx
, int psize
,
3718 uint32_t addpath_id
)
3720 struct prefix_rd prd
;
3721 struct prefix_evpn p
;
3722 struct bgp_route_evpn evpn
;
3724 uint8_t macaddr_len
;
3725 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
3726 uint32_t num_labels
= 0;
3730 /* Type-2 route should be either 33, 37 or 49 bytes or an
3731 * additional 3 bytes if there is a second label (VNI):
3732 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3733 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3734 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3736 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3737 && psize
!= 40 && psize
!= 52) {
3738 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3739 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3740 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3744 memset(&evpn
, 0, sizeof(evpn
));
3746 /* Make prefix_rd */
3747 prd
.family
= AF_UNSPEC
;
3749 memcpy(&prd
.val
, pfx
, 8);
3752 /* Make EVPN prefix. */
3753 memset(&p
, 0, sizeof(struct prefix_evpn
));
3755 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3756 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3758 /* Copy Ethernet Seg Identifier */
3759 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
3762 /* Copy Ethernet Tag */
3763 memcpy(ð_tag
, pfx
, 4);
3764 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3767 /* Get the MAC Addr len */
3768 macaddr_len
= *pfx
++;
3770 /* Get the MAC Addr */
3771 if (macaddr_len
== (ETH_ALEN
* 8)) {
3772 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
3776 EC_BGP_EVPN_ROUTE_INVALID
,
3777 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3778 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3784 ipaddr_len
= *pfx
++;
3785 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3786 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3788 EC_BGP_EVPN_ROUTE_INVALID
,
3789 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3790 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3795 ipaddr_len
/= 8; /* Convert to bytes. */
3796 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3799 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
3803 /* Get the VNI(s). Stored as bytes here. */
3805 memset(label
, 0, sizeof(label
));
3806 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
3807 pfx
+= BGP_LABEL_BYTES
;
3808 psize
-= (33 + ipaddr_len
);
3809 /* Do we have a second VNI? */
3812 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
3814 * If in future, we are required to access additional fields,
3815 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
3820 /* Process the route. */
3822 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3823 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3824 &prd
, &label
[0], num_labels
, 0, &evpn
);
3826 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3827 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3828 &prd
, &label
[0], num_labels
, &evpn
);
3833 * Process received EVPN type-3 route (advertise or withdraw).
3835 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3836 struct attr
*attr
, uint8_t *pfx
, int psize
,
3837 uint32_t addpath_id
)
3839 struct prefix_rd prd
;
3840 struct prefix_evpn p
;
3845 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3846 * IP len (1) and IP (4 or 16).
3848 if (psize
!= 17 && psize
!= 29) {
3849 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3850 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3851 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3855 /* If PMSI is present, log if it is anything other than IR.
3856 * Note: We just simply ignore the values as it is not clear if
3857 * doing anything else is better.
3860 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3861 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
) {
3863 EC_BGP_EVPN_PMSI_PRESENT
,
3864 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3865 peer
->bgp
->vrf_id
, peer
->host
,
3866 attr
->pmsi_tnl_type
);
3870 /* Make prefix_rd */
3871 prd
.family
= AF_UNSPEC
;
3873 memcpy(&prd
.val
, pfx
, 8);
3876 /* Make EVPN prefix. */
3877 memset(&p
, 0, sizeof(struct prefix_evpn
));
3879 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3880 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3882 /* Copy Ethernet Tag */
3883 memcpy(ð_tag
, pfx
, 4);
3884 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
3888 ipaddr_len
= *pfx
++;
3889 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3890 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
3891 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
3894 EC_BGP_EVPN_ROUTE_INVALID
,
3895 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3896 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3900 /* Process the route. */
3902 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3903 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3904 &prd
, NULL
, 0, 0, NULL
);
3906 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3907 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3908 &prd
, NULL
, 0, NULL
);
3913 * Process received EVPN type-4 route (advertise or withdraw).
3915 static int process_type4_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3916 struct attr
*attr
, uint8_t *pfx
, int psize
,
3917 uint32_t addpath_id
)
3922 struct in_addr vtep_ip
;
3923 struct prefix_rd prd
;
3924 struct prefix_evpn p
;
3926 /* Type-4 route should be either 23 or 35 bytes
3927 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
3929 if (psize
!= 23 && psize
!= 35) {
3930 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3931 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
3932 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3936 /* Make prefix_rd */
3937 prd
.family
= AF_UNSPEC
;
3939 memcpy(&prd
.val
, pfx
, 8);
3943 memcpy(&esi
, pfx
, ESI_BYTES
);
3948 ipaddr_len
= *pfx
++;
3949 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3950 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
3953 EC_BGP_EVPN_ROUTE_INVALID
,
3954 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
3955 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3959 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
3960 /* Process the route. */
3962 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3963 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3964 &prd
, NULL
, 0, 0, NULL
);
3966 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3967 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3968 &prd
, NULL
, 0, NULL
);
3975 * Process received EVPN type-5 route (advertise or withdraw).
3977 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3978 struct attr
*attr
, uint8_t *pfx
, int psize
,
3979 uint32_t addpath_id
)
3981 struct prefix_rd prd
;
3982 struct prefix_evpn p
;
3983 struct bgp_route_evpn evpn
;
3986 mpls_label_t label
; /* holds the VNI as in the packet */
3989 /* Type-5 route should be 34 or 58 bytes:
3990 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3991 * GW (4 or 16) and VNI (3).
3992 * Note that the IP and GW should both be IPv4 or both IPv6.
3994 if (psize
!= 34 && psize
!= 58) {
3995 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3996 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3997 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4001 /* Make prefix_rd */
4002 prd
.family
= AF_UNSPEC
;
4004 memcpy(&prd
.val
, pfx
, 8);
4007 /* Make EVPN prefix. */
4008 memset(&p
, 0, sizeof(struct prefix_evpn
));
4010 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4011 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
4013 /* Additional information outside of prefix - ESI and GW IP */
4014 memset(&evpn
, 0, sizeof(evpn
));
4017 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
4020 /* Fetch Ethernet Tag. */
4021 memcpy(ð_tag
, pfx
, 4);
4022 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
4025 /* Fetch IP prefix length. */
4027 if (ippfx_len
> IPV6_MAX_BITLEN
) {
4029 EC_BGP_EVPN_ROUTE_INVALID
,
4030 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4031 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
4034 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
4036 /* Determine IPv4 or IPv6 prefix */
4037 /* Since the address and GW are from the same family, this just becomes
4038 * a simple check on the total size.
4041 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
4042 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
4044 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
4047 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
4048 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
4050 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
4054 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4055 memset(&label
, 0, sizeof(label
));
4056 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
4059 * If in future, we are required to access additional fields,
4060 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4064 /* Process the route. */
4066 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4067 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4068 &prd
, &label
, 1, 0, &evpn
);
4070 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4071 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4072 &prd
, &label
, 1, &evpn
);
4077 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
4078 struct prefix_rd
*prd
, mpls_label_t
*label
,
4079 uint32_t num_labels
, struct attr
*attr
)
4083 struct evpn_addr
*p_evpn_p
;
4085 memset(&temp
, 0, 16);
4086 if (p
->family
!= AF_EVPN
)
4088 p_evpn_p
= &(p
->u
.prefix_evpn
);
4090 /* len denites the total len of IP and GW-IP in the route
4091 IP and GW-IP have to be both ipv4 or ipv6
4093 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4094 len
= 8; /* IP and GWIP are both ipv4 */
4096 len
= 32; /* IP and GWIP are both ipv6 */
4097 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4098 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4099 stream_put(s
, prd
->val
, 8);
4101 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
4103 stream_put(s
, &temp
, 10);
4104 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4105 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4106 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4107 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4109 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4111 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4113 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
4115 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
4117 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4118 stream_put_ipv4(s
, 0);
4120 stream_put(s
, &temp
, 16);
4124 stream_put(s
, label
, 3);
4130 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4132 static void cleanup_vni_on_disable(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4134 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4136 /* Remove EVPN routes and schedule for processing. */
4137 delete_routes_for_vni(bgp
, vpn
);
4139 /* Clear "live" flag and see if hash needs to be freed. */
4140 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4141 if (!is_vni_configured(vpn
))
4142 bgp_evpn_free(bgp
, vpn
);
4146 * Free a VNI entry; iterator function called during cleanup.
4148 static void free_vni_entry(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4150 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4152 delete_all_vni_routes(bgp
, vpn
);
4153 bgp_evpn_free(bgp
, vpn
);
4157 * Derive AUTO import RT for BGP VRF - L3VNI
4159 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4161 struct bgp
*bgp_evpn
= NULL
;
4163 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4164 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4167 bgp_evpn
= bgp_get_evpn();
4170 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4174 * Delete AUTO import RT from BGP VRF - L3VNI
4176 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4178 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4182 * Derive AUTO export RT for BGP VRF - L3VNI
4184 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4186 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4187 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4191 * Delete AUTO export RT from BGP VRF - L3VNI
4193 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4195 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4198 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4200 struct bgp
*bgp_evpn
= NULL
;
4201 struct listnode
*node
= NULL
;
4202 struct bgpevpn
*vpn
= NULL
;
4204 bgp_evpn
= bgp_get_evpn();
4208 /* update all type-5 routes */
4209 update_advertise_vrf_routes(bgp_vrf
);
4211 /* update all type-2 routes */
4212 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4213 update_routes_for_vni(bgp_evpn
, vpn
);
4217 * Handle autort change for a given VNI.
4219 static void update_autort_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4221 struct bgpevpn
*vpn
= bucket
->data
;
4223 if (!is_import_rt_configured(vpn
)) {
4224 if (is_vni_live(vpn
))
4225 bgp_evpn_uninstall_routes(bgp
, vpn
);
4226 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4227 list_delete_all_node(vpn
->import_rtl
);
4228 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4229 if (is_vni_live(vpn
))
4230 bgp_evpn_install_routes(bgp
, vpn
);
4232 if (!is_export_rt_configured(vpn
)) {
4233 list_delete_all_node(vpn
->export_rtl
);
4234 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4235 if (is_vni_live(vpn
))
4236 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4244 /* withdraw type-5 route corresponding to ip prefix */
4245 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4246 afi_t afi
, safi_t safi
)
4249 struct prefix_evpn evp
;
4250 char buf
[PREFIX_STRLEN
];
4252 build_type5_prefix_from_ip_prefix(&evp
, p
);
4253 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4256 EC_BGP_EVPN_ROUTE_DELETE
,
4257 "%u failed to delete type-5 route for prefix %s in vrf %s",
4258 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4259 vrf_id_to_name(bgp_vrf
->vrf_id
));
4263 /* withdraw all type-5 routes for an address family */
4264 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4266 struct bgp_table
*table
= NULL
;
4267 struct bgp_node
*rn
= NULL
;
4268 struct bgp_path_info
*pi
;
4270 table
= bgp_vrf
->rib
[afi
][safi
];
4271 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4272 /* Only care about "selected" routes. Also ensure that
4273 * these are routes that are injectable into EVPN.
4275 /* TODO: Support for AddPath for EVPN. */
4276 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4277 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4278 && is_route_injectable_into_evpn(pi
)) {
4279 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
4288 * evpn - enable advertisement of default g/w
4290 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
4291 safi_t safi
, bool add
)
4293 struct prefix ip_prefix
;
4295 /* form the default prefix 0.0.0.0/0 */
4296 memset(&ip_prefix
, 0, sizeof(struct prefix
));
4297 ip_prefix
.family
= afi2family(afi
);
4300 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
4303 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
4310 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4311 * to this function correspond to those of the source IP prefix (best
4312 * path in the case of the attr. In the case of a local prefix (when we
4313 * are advertising local subnets), the src_attr will be NULL.
4315 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4316 struct attr
*src_attr
, afi_t afi
,
4320 struct prefix_evpn evp
;
4321 char buf
[PREFIX_STRLEN
];
4323 build_type5_prefix_from_ip_prefix(&evp
, p
);
4324 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4326 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4327 "%u: Failed to create type-5 route for prefix %s",
4328 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4331 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4332 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4333 * advertisement is enabled.
4335 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4338 struct bgp_table
*table
= NULL
;
4339 struct bgp_node
*rn
= NULL
;
4340 struct bgp_path_info
*pi
;
4342 table
= bgp_vrf
->rib
[afi
][safi
];
4343 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4344 /* Need to identify the "selected" route entry to use its
4345 * attribute. Also, ensure that the route is injectable
4347 * TODO: Support for AddPath for EVPN.
4349 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4350 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4351 && is_route_injectable_into_evpn(pi
)) {
4353 /* apply the route-map */
4354 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4357 ret
= route_map_apply(
4358 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4360 &rn
->p
, RMAP_BGP
, pi
);
4361 if (ret
== RMAP_DENYMATCH
)
4364 bgp_evpn_advertise_type5_route(
4365 bgp_vrf
, &rn
->p
, pi
->attr
, afi
, safi
);
4372 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4374 struct listnode
*node
, *nnode
, *node_to_del
;
4375 struct ecommunity
*ecom
, *ecom_auto
;
4376 struct ecommunity_val eval
;
4378 if (bgp
->advertise_autort_rfc8365
)
4379 vni
|= EVPN_AUTORT_VXLAN
;
4380 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4382 ecom_auto
= ecommunity_new();
4383 ecommunity_add_val(ecom_auto
, &eval
);
4386 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4387 if (ecommunity_match(ecom
, ecom_auto
)) {
4388 ecommunity_free(&ecom
);
4394 list_delete_node(rtl
, node_to_del
);
4396 ecommunity_free(&ecom_auto
);
4399 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4400 struct ecommunity
*ecomadd
)
4402 /* uninstall routes from vrf */
4403 uninstall_routes_for_vrf(bgp_vrf
);
4405 /* Cleanup the RT to VRF mapping */
4406 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4408 /* Remove auto generated RT */
4409 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4411 /* Add the newly configured RT to RT list */
4412 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4413 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4415 /* map VRF to its RTs */
4416 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4418 /* install routes matching the new VRF */
4419 install_routes_for_vrf(bgp_vrf
);
4422 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4423 struct ecommunity
*ecomdel
)
4425 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4426 struct ecommunity
*ecom
= NULL
;
4428 /* uninstall routes from vrf */
4429 uninstall_routes_for_vrf(bgp_vrf
);
4431 /* Cleanup the RT to VRF mapping */
4432 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4434 /* remove the RT from the RT list */
4435 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4436 if (ecommunity_match(ecom
, ecomdel
)) {
4437 ecommunity_free(&ecom
);
4444 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4446 assert(bgp_vrf
->vrf_import_rtl
);
4447 /* fallback to auto import rt, if this was the last RT */
4448 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4449 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4450 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4453 /* map VRFs to its RTs */
4454 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4456 /* install routes matching this new RT */
4457 install_routes_for_vrf(bgp_vrf
);
4460 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4461 struct ecommunity
*ecomadd
)
4463 /* remove auto-generated RT */
4464 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4466 /* Add the new RT to the RT list */
4467 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4468 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4470 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4473 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4474 struct ecommunity
*ecomdel
)
4476 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4477 struct ecommunity
*ecom
= NULL
;
4479 /* Remove the RT from the RT list */
4480 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4481 if (ecommunity_match(ecom
, ecomdel
)) {
4482 ecommunity_free(&ecom
);
4489 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4492 * Temporary assert to make SA happy.
4493 * The ALL_LIST_ELEMENTS macro above has a NULL check
4494 * which means that SA is going to complain about
4495 * the list_isempty call, which doesn't NULL check.
4496 * So until we get this situation cleaned up, here
4499 assert(bgp_vrf
->vrf_export_rtl
);
4501 /* fall back to auto-generated RT if this was the last RT */
4502 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4503 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4504 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4507 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4511 * Handle change to BGP router id. This is invoked twice by the change
4512 * handler, first before the router id has been changed and then after
4513 * the router id has been changed. The first invocation will result in
4514 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4515 * will result in the routes being re-advertised.
4517 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4521 /* delete and withdraw all the type-5 routes
4522 stored in the global table for this vrf
4524 withdraw_router_id_vrf(bgp
);
4526 /* delete all the VNI routes (type-2/type-3) routes for all the
4529 hash_iterate(bgp
->vnihash
,
4530 (void (*)(struct hash_bucket
*,
4531 void *))withdraw_router_id_vni
,
4535 /* advertise all routes in the vrf as type-5 routes with the new
4538 update_router_id_vrf(bgp
);
4540 /* advertise all the VNI routes (type-2/type-3) routes with the
4543 hash_iterate(bgp
->vnihash
,
4544 (void (*)(struct hash_bucket
*,
4545 void *))update_router_id_vni
,
4551 * Handle change to auto-RT algorithm - update and advertise local routes.
4553 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4555 hash_iterate(bgp
->vnihash
,
4556 (void (*)(struct hash_bucket
*,
4557 void*))update_autort_vni
,
4562 * Handle change to export RT - update and advertise local routes.
4564 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4566 return update_routes_for_vni(bgp
, vpn
);
4569 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4572 delete_withdraw_vrf_routes(bgp_vrf
);
4574 update_advertise_vrf_routes(bgp_vrf
);
4578 * Handle change to RD. This is invoked twice by the change handler,
4579 * first before the RD has been changed and then after the RD has
4580 * been changed. The first invocation will result in local routes
4581 * of this VNI being deleted and withdrawn and the next will result
4582 * in the routes being re-advertised.
4584 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4588 delete_withdraw_vni_routes(bgp
, vpn
);
4590 update_advertise_vni_routes(bgp
, vpn
);
4594 * Install routes for this VNI. Invoked upon change to Import RT.
4596 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4598 return install_routes_for_vni(bgp
, vpn
);
4602 * Uninstall all routes installed for this VNI. Invoked upon change
4605 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4607 return uninstall_routes_for_vni(bgp
, vpn
);
4611 * TODO: Hardcoded for a maximum of 2 VNIs right now
4613 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4618 vni1
= label2vni(label
);
4619 if (num_labels
== 2) {
4620 vni2
= label2vni(label
+ 1);
4621 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4623 snprintf(buf
, len
, "%u", vni1
);
4628 * Function to convert evpn route to json format.
4629 * NOTE: We don't use prefix2str as the output here is a bit different.
4631 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
4633 char buf1
[ETHER_ADDR_STRLEN
];
4634 char buf2
[PREFIX2STR_BUFFER
];
4639 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4640 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4641 json_object_int_add(json
, "ethTag",
4642 p
->prefix
.imet_addr
.eth_tag
);
4643 json_object_int_add(json
, "ipLen",
4644 is_evpn_prefix_ipaddr_v4(p
)
4647 json_object_string_add(json
, "ip",
4648 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4649 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4650 if (is_evpn_prefix_ipaddr_none(p
)) {
4651 json_object_int_add(json
, "routeType",
4652 p
->prefix
.route_type
);
4653 json_object_int_add(json
, "ethTag",
4654 p
->prefix
.macip_addr
.eth_tag
);
4655 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4656 json_object_string_add(json
, "mac",
4657 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4663 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4666 json_object_int_add(json
, "routeType",
4667 p
->prefix
.route_type
);
4668 json_object_int_add(json
, "ethTag",
4669 p
->prefix
.macip_addr
.eth_tag
);
4670 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4671 json_object_string_add(json
, "mac",
4672 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4675 json_object_int_add(json
, "ipLen",
4676 is_evpn_prefix_ipaddr_v4(p
)
4679 json_object_string_add(
4682 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4684 PREFIX2STR_BUFFER
));
4687 /* Currently, this is to cater to other AF_ETHERNET code. */
4692 * Function to convert evpn route to string.
4693 * NOTE: We don't use prefix2str as the output here is a bit different.
4695 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
4697 char buf1
[ETHER_ADDR_STRLEN
];
4698 char buf2
[PREFIX2STR_BUFFER
];
4699 char buf3
[ESI_STR_LEN
];
4701 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4702 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
4703 p
->prefix
.imet_addr
.eth_tag
,
4704 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4706 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4707 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4708 if (is_evpn_prefix_ipaddr_none(p
))
4709 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4710 p
->prefix
.route_type
,
4711 p
->prefix
.macip_addr
.eth_tag
,
4713 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4718 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4720 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
4721 p
->prefix
.route_type
,
4722 p
->prefix
.macip_addr
.eth_tag
,
4724 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4726 family
== AF_INET
? IPV4_MAX_BITLEN
4729 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4731 PREFIX2STR_BUFFER
));
4733 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
4734 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4735 p
->prefix
.route_type
,
4736 p
->prefix
.prefix_addr
.eth_tag
,
4737 p
->prefix
.prefix_addr
.ip_prefix_length
,
4738 is_evpn_prefix_ipaddr_v4(p
)
4739 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
4740 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
4741 } else if (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
4742 snprintf(buf
, len
, "[%d]:[%s]:[%d]:[%s]",
4743 p
->prefix
.route_type
,
4744 esi_to_str(&p
->prefix
.es_addr
.esi
, buf3
, sizeof(buf3
)),
4745 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4747 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
));
4749 /* For EVPN route types not supported yet. */
4750 snprintf(buf
, len
, "(unsupported route type %d)",
4751 p
->prefix
.route_type
);
4758 * Encode EVPN prefix in Update (MP_REACH)
4760 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
4761 struct prefix_rd
*prd
, mpls_label_t
*label
,
4762 uint32_t num_labels
, struct attr
*attr
,
4763 int addpath_encode
, uint32_t addpath_tx_id
)
4765 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4766 int len
, ipa_len
= 0;
4769 stream_putl(s
, addpath_tx_id
);
4772 stream_putc(s
, evp
->prefix
.route_type
);
4774 switch (evp
->prefix
.route_type
) {
4775 case BGP_EVPN_MAC_IP_ROUTE
:
4776 if (is_evpn_prefix_ipaddr_v4(evp
))
4777 ipa_len
= IPV4_MAX_BYTELEN
;
4778 else if (is_evpn_prefix_ipaddr_v6(evp
))
4779 ipa_len
= IPV6_MAX_BYTELEN
;
4780 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4781 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4782 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4784 stream_putc(s
, len
);
4785 stream_put(s
, prd
->val
, 8); /* RD */
4787 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
4789 stream_put(s
, 0, 10);
4790 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4791 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4792 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4793 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4794 if (ipa_len
) /* IP */
4795 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4797 /* 1st label is the L2 VNI */
4798 stream_put(s
, label
, BGP_LABEL_BYTES
);
4799 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4800 if (ipa_len
&& num_labels
> 1)
4801 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4804 case BGP_EVPN_IMET_ROUTE
:
4805 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4806 stream_put(s
, prd
->val
, 8); /* RD */
4807 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4808 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4809 /* Originating Router's IP Addr */
4810 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4813 case BGP_EVPN_ES_ROUTE
:
4814 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4815 stream_put(s
, prd
->val
, 8); /* RD */
4816 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4817 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4819 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4822 case BGP_EVPN_IP_PREFIX_ROUTE
:
4823 /* TODO: AddPath support. */
4824 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4832 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4833 struct bgp_nlri
*packet
, int withdraw
)
4839 uint32_t addpath_id
;
4840 int addpath_encoded
;
4845 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4847 lim
= pnt
+ packet
->length
;
4849 safi
= packet
->safi
;
4853 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
4854 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
4855 PEER_CAP_ADDPATH_AF_TX_RCV
));
4857 for (; pnt
< lim
; pnt
+= psize
) {
4858 /* Clear prefix structure. */
4859 memset(&p
, 0, sizeof(struct prefix
));
4861 /* Deal with path-id if AddPath is supported. */
4862 if (addpath_encoded
) {
4863 /* When packet overflow occurs return immediately. */
4864 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4867 addpath_id
= ntohl(*((uint32_t *)pnt
));
4868 pnt
+= BGP_ADDPATH_ID_LEN
;
4871 /* All EVPN NLRI types start with type and length. */
4878 /* When packet overflow occur return immediately. */
4879 if (pnt
+ psize
> lim
)
4883 case BGP_EVPN_MAC_IP_ROUTE
:
4884 if (process_type2_route(peer
, afi
, safi
,
4885 withdraw
? NULL
: attr
, pnt
,
4886 psize
, addpath_id
)) {
4889 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4890 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4895 case BGP_EVPN_IMET_ROUTE
:
4896 if (process_type3_route(peer
, afi
, safi
,
4897 withdraw
? NULL
: attr
, pnt
,
4898 psize
, addpath_id
)) {
4901 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4902 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4907 case BGP_EVPN_ES_ROUTE
:
4908 if (process_type4_route(peer
, afi
, safi
,
4909 withdraw
? NULL
: attr
, pnt
,
4910 psize
, addpath_id
)) {
4913 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4914 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4919 case BGP_EVPN_IP_PREFIX_ROUTE
:
4920 if (process_type5_route(peer
, afi
, safi
,
4921 withdraw
? NULL
: attr
, pnt
,
4922 psize
, addpath_id
)) {
4925 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4926 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4936 /* Packet length consistency check. */
4944 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
4945 * The mapping will be used during route processing.
4946 * bgp_def: default bgp instance
4947 * bgp_vrf: specific bgp vrf instance on which RT is configured
4949 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
4952 struct ecommunity_val
*eval
= NULL
;
4953 struct listnode
*node
= NULL
, *nnode
= NULL
;
4954 struct ecommunity
*ecom
= NULL
;
4956 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4957 for (i
= 0; i
< ecom
->size
; i
++) {
4958 eval
= (struct ecommunity_val
*)(ecom
->val
4960 * ECOMMUNITY_SIZE
));
4961 map_vrf_to_rt(bgp_vrf
, eval
);
4967 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
4969 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
4972 struct ecommunity_val
*eval
;
4973 struct listnode
*node
, *nnode
;
4974 struct ecommunity
*ecom
;
4976 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4977 for (i
= 0; i
< ecom
->size
; i
++) {
4978 struct vrf_irt_node
*irt
;
4979 struct ecommunity_val eval_tmp
;
4981 eval
= (struct ecommunity_val
*)(ecom
->val
4983 * ECOMMUNITY_SIZE
));
4984 /* If using "automatic" RT, we only care about the
4985 * local-admin sub-field.
4986 * This is to facilitate using VNI as the RT for EBGP
4989 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4990 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
4991 BGP_VRF_IMPORT_RT_CFGD
))
4992 mask_ecom_global_admin(&eval_tmp
, eval
);
4994 irt
= lookup_vrf_import_rt(&eval_tmp
);
4996 unmap_vrf_from_rt(bgp_vrf
, irt
);
5003 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
5004 * The mapping will be used during route processing.
5006 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5009 struct ecommunity_val
*eval
;
5010 struct listnode
*node
, *nnode
;
5011 struct ecommunity
*ecom
;
5013 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5014 for (i
= 0; i
< ecom
->size
; i
++) {
5015 eval
= (struct ecommunity_val
*)(ecom
->val
5017 * ECOMMUNITY_SIZE
));
5018 map_vni_to_rt(bgp
, vpn
, eval
);
5024 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
5026 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5029 struct ecommunity_val
*eval
;
5030 struct listnode
*node
, *nnode
;
5031 struct ecommunity
*ecom
;
5033 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5034 for (i
= 0; i
< ecom
->size
; i
++) {
5035 struct irt_node
*irt
;
5036 struct ecommunity_val eval_tmp
;
5038 eval
= (struct ecommunity_val
*)(ecom
->val
5040 * ECOMMUNITY_SIZE
));
5041 /* If using "automatic" RT, we only care about the
5042 * local-admin sub-field.
5043 * This is to facilitate using VNI as the RT for EBGP
5046 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5047 if (!is_import_rt_configured(vpn
))
5048 mask_ecom_global_admin(&eval_tmp
, eval
);
5050 irt
= lookup_import_rt(bgp
, &eval_tmp
);
5052 unmap_vni_from_rt(bgp
, vpn
, irt
);
5058 * Derive Import RT automatically for VNI and map VNI to RT.
5059 * The mapping will be used during route processing.
5061 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5063 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
5064 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
5067 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
5071 * Derive Export RT automatically for VNI.
5073 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5075 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
5076 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5080 * Derive RD automatically for VNI using passed information - it
5081 * is of the form RouterId:unique-id-for-vni.
5083 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5085 if (is_vrf_rd_configured(bgp
))
5088 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5092 * Derive RD automatically for VNI using passed information - it
5093 * is of the form RouterId:unique-id-for-vni.
5095 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5099 vpn
->prd
.family
= AF_UNSPEC
;
5100 vpn
->prd
.prefixlen
= 64;
5101 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
5102 (void)str2prefix_rd(buf
, &vpn
->prd
);
5103 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5109 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5111 struct list
*inst
= bm
->bgp
;
5112 struct listnode
*node
;
5113 struct bgp
*bgp_vrf
;
5115 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5116 if (bgp_vrf
->l3vni
== vni
)
5126 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5128 struct bgpevpn
*vpn
;
5131 memset(&tmp
, 0, sizeof(struct bgpevpn
));
5133 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5138 * Create a new vpn - invoked upon configuration or zebra notification.
5140 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5141 struct in_addr originator_ip
,
5142 vrf_id_t tenant_vrf_id
)
5144 struct bgpevpn
*vpn
;
5149 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5151 /* Set values - RD and RT set to defaults. */
5153 vpn
->originator_ip
= originator_ip
;
5154 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5156 /* Initialize route-target import and export lists */
5157 vpn
->import_rtl
= list_new();
5158 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5159 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
5160 vpn
->export_rtl
= list_new();
5161 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5162 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
5163 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5164 derive_rd_rt_for_vni(bgp
, vpn
);
5166 /* Initialize EVPN route table. */
5167 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5170 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
5171 XFREE(MTYPE_BGP_EVPN
, vpn
);
5175 /* add to l2vni list on corresponding vrf */
5176 bgpevpn_link_to_l3vni(vpn
);
5178 QOBJ_REG(vpn
, bgpevpn
);
5183 * Free a given VPN - called in multiple scenarios such as zebra
5184 * notification, configuration being deleted, advertise-all-vni disabled etc.
5185 * This just frees appropriate memory, caller should have taken other
5188 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5190 bgpevpn_unlink_from_l3vni(vpn
);
5191 bgp_table_unlock(vpn
->route_table
);
5192 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5193 list_delete(&vpn
->import_rtl
);
5194 list_delete(&vpn
->export_rtl
);
5195 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5196 hash_release(bgp
->vnihash
, vpn
);
5198 XFREE(MTYPE_BGP_EVPN
, vpn
);
5204 struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
)
5209 memset(&tmp
, 0, sizeof(struct evpnes
));
5210 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
5211 es
= hash_lookup(bgp
->esihash
, &tmp
);
5216 * Create a new local es - invoked upon zebra notification.
5218 struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
,
5220 struct ipaddr
*originator_ip
)
5228 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct evpnes
));
5230 /* set the ESI and originator_ip */
5231 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
5232 memcpy(&es
->originator_ip
, originator_ip
, sizeof(struct ipaddr
));
5234 /* Initialise the VTEP list */
5235 es
->vtep_list
= list_new();
5236 es
->vtep_list
->cmp
= evpn_vtep_ip_cmp
;
5238 /* auto derive RD for this es */
5239 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
5240 es
->prd
.family
= AF_UNSPEC
;
5241 es
->prd
.prefixlen
= 64;
5242 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), es
->rd_id
);
5243 (void)str2prefix_rd(buf
, &es
->prd
);
5245 /* Initialize the ES route table */
5246 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5249 if (!hash_get(bgp
->esihash
, es
, hash_alloc_intern
)) {
5250 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5254 QOBJ_REG(es
, evpnes
);
5260 * This just frees appropriate memory, caller should have taken other
5263 void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
)
5265 list_delete(&es
->vtep_list
);
5266 bgp_table_unlock(es
->route_table
);
5267 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
5268 hash_release(bgp
->esihash
, es
);
5270 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5274 * Import evpn route from global table to VNI/VRF/ESI.
5276 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5277 struct prefix
*p
, struct bgp_path_info
*pi
)
5279 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5283 * Unimport evpn route from VNI/VRF/ESI.
5285 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5286 struct prefix
*p
, struct bgp_path_info
*pi
)
5288 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5291 /* filter routes which have martian next hops */
5292 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5296 struct bgp_node
*rd_rn
, *rn
;
5297 struct bgp_table
*table
;
5298 struct bgp_path_info
*pi
;
5303 /* Walk entire global routing table and evaluate routes which could be
5304 * imported into this VPN. Note that we cannot just look at the routes
5305 * for the VNI's RD -
5306 * remote routes applicable for this VNI could have any RD.
5308 /* EVPN routes are a 2-level table. */
5309 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
5310 rd_rn
= bgp_route_next(rd_rn
)) {
5311 table
= bgp_node_get_bgp_table_info(rd_rn
);
5315 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5317 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
5320 /* Consider "valid" remote routes applicable for
5322 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5323 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5326 if (bgp_nexthop_self(bgp
, pi
->attr
->nexthop
)) {
5328 char attr_str
[BUFSIZ
] = {0};
5329 char pbuf
[PREFIX_STRLEN
];
5331 bgp_dump_attr(pi
->attr
, attr_str
,
5334 if (bgp_debug_update(pi
->peer
, &rn
->p
,
5337 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
5344 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5347 bgp_rib_remove(rn
, pi
, pi
->peer
, afi
,
5358 * Handle del of a local MACIP.
5360 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5361 struct ipaddr
*ip
, int state
)
5363 struct bgpevpn
*vpn
;
5364 struct prefix_evpn p
;
5365 struct bgp_node
*rn
;
5367 /* Lookup VNI hash - should exist. */
5368 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5369 if (!vpn
|| !is_vni_live(vpn
)) {
5370 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5371 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5372 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5376 build_evpn_type2_prefix(&p
, mac
, ip
);
5377 if (state
== ZEBRA_NEIGH_ACTIVE
) {
5378 /* Remove EVPN type-2 route and schedule for processing. */
5379 delete_evpn_route(bgp
, vpn
, &p
);
5381 /* Re-instate the current remote best path if any */
5382 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
5384 evpn_zebra_reinstall_best_route(bgp
, vpn
, rn
);
5391 * Handle add of a local MACIP.
5393 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5394 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
)
5396 struct bgpevpn
*vpn
;
5397 struct prefix_evpn p
;
5399 /* Lookup VNI hash - should exist. */
5400 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5401 if (!vpn
|| !is_vni_live(vpn
)) {
5402 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5403 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5404 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5408 /* Create EVPN type-2 route and schedule for processing. */
5409 build_evpn_type2_prefix(&p
, mac
, ip
);
5410 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
)) {
5411 char buf
[ETHER_ADDR_STRLEN
];
5412 char buf2
[INET6_ADDRSTRLEN
];
5415 EC_BGP_EVPN_ROUTE_CREATE
,
5416 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5417 bgp
->vrf_id
, vpn
->vni
,
5418 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5421 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5422 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5429 static void link_l2vni_hash_to_l3vni(struct hash_bucket
*bucket
,
5430 struct bgp
*bgp_vrf
)
5432 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
5433 struct bgp
*bgp_evpn
= NULL
;
5435 bgp_evpn
= bgp_get_evpn();
5438 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5439 bgpevpn_link_to_l3vni(vpn
);
5442 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
, struct ethaddr
*rmac
,
5443 struct in_addr originator_ip
, int filter
,
5444 ifindex_t svi_ifindex
)
5446 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5447 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5448 struct listnode
*node
= NULL
;
5449 struct bgpevpn
*vpn
= NULL
;
5452 /* get the EVPN instance - required to get the AS number for VRF
5455 bgp_evpn
= bgp_get_evpn();
5459 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
5465 /* if the BGP vrf instance doesn't exist - create one */
5466 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5471 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5472 vrf_id
== VRF_DEFAULT
? BGP_INSTANCE_TYPE_DEFAULT
5473 : BGP_INSTANCE_TYPE_VRF
);
5475 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
:
5476 flog_err(EC_BGP_MULTI_INSTANCE
,
5477 "'bgp multiple-instance' not present\n");
5479 case BGP_ERR_AS_MISMATCH
:
5480 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5481 "BGP is already running; AS is %u\n", as
);
5483 case BGP_ERR_INSTANCE_MISMATCH
:
5484 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5485 "BGP instance name and AS number mismatch\n");
5489 /* mark as auto created */
5490 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5493 /* associate the vrf with l3vni and related parameters */
5494 bgp_vrf
->l3vni
= l3vni
;
5495 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
5496 bgp_vrf
->originator_ip
= originator_ip
;
5497 bgp_vrf
->l3vni_svi_ifindex
= svi_ifindex
;
5499 /* set the right filter - are we using l3vni only for prefix routes? */
5501 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5503 /* Map auto derive or configured RTs */
5504 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5505 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5507 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
5509 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5510 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5512 /* auto derive RD */
5513 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5515 /* link all corresponding l2vnis */
5516 hash_iterate(bgp_evpn
->vnihash
,
5517 (void (*)(struct hash_bucket
*,
5518 void *))link_l2vni_hash_to_l3vni
,
5521 /* Only update all corresponding type-2 routes if we are advertising two
5522 * labels along with type-2 routes
5525 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5526 update_routes_for_vni(bgp_evpn
, vpn
);
5528 /* advertise type-5 routes if needed */
5529 update_advertise_vrf_routes(bgp_vrf
);
5531 /* install all remote routes belonging to this l3vni into correspondng
5533 install_routes_for_vrf(bgp_vrf
);
5538 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5540 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5541 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5542 struct listnode
*node
= NULL
;
5543 struct listnode
*next
= NULL
;
5544 struct bgpevpn
*vpn
= NULL
;
5546 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5550 "Cannot process L3VNI %u Del - Could not find BGP instance",
5555 bgp_evpn
= bgp_get_evpn();
5559 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
5564 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5565 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5566 * routes. This will uninstalling the routes from zebra and decremnt the
5569 uninstall_routes_for_vrf(bgp_vrf
);
5571 /* delete/withdraw all type-5 routes */
5572 delete_withdraw_vrf_routes(bgp_vrf
);
5574 /* remove the l3vni from vrf instance */
5577 /* remove the Rmac from the BGP vrf */
5578 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5580 /* remove default import RT or Unmap non-default import RT */
5581 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5582 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5583 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5584 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5587 /* remove default export RT */
5588 if (!list_isempty(bgp_vrf
->vrf_export_rtl
) &&
5589 !CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
5590 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5593 /* update all corresponding local mac-ip routes */
5594 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5595 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5596 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5597 update_routes_for_vni(bgp_evpn
, vpn
);
5601 /* If any L2VNIs point to this instance, unlink them. */
5602 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5603 bgpevpn_unlink_from_l3vni(vpn
);
5605 /* Delete the instance if it was autocreated */
5606 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5607 bgp_delete(bgp_vrf
);
5613 * Handle del of a local VNI.
5615 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5617 struct bgpevpn
*vpn
;
5619 /* Locate VNI hash */
5620 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5622 if (bgp_debug_zebra(NULL
))
5624 EC_BGP_EVPN_VPN_VNI
,
5625 "%u: VNI hash entry for VNI %u not found at DEL",
5630 /* Remove all local EVPN routes and schedule for processing (to
5631 * withdraw from peers).
5633 delete_routes_for_vni(bgp
, vpn
);
5636 * tunnel is no longer active, del tunnel ip address from tip_hash
5638 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5640 /* Clear "live" flag and see if hash needs to be freed. */
5641 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5642 if (!is_vni_configured(vpn
))
5643 bgp_evpn_free(bgp
, vpn
);
5649 * Handle add (or update) of a local VNI. The VNI changes we care
5650 * about are for the local-tunnel-ip and the (tenant) VRF.
5652 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5653 struct in_addr originator_ip
, vrf_id_t tenant_vrf_id
)
5655 struct bgpevpn
*vpn
;
5656 struct prefix_evpn p
;
5658 /* Lookup VNI. If present and no change, exit. */
5659 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5662 if (is_vni_live(vpn
)
5663 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5664 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5665 /* Probably some other param has changed that we don't
5669 /* Update tenant_vrf_id if it has changed. */
5670 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5671 bgpevpn_unlink_from_l3vni(vpn
);
5672 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5673 bgpevpn_link_to_l3vni(vpn
);
5676 /* If tunnel endpoint IP has changed, update (and delete prior
5677 * type-3 route, if needed.)
5679 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5680 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5682 /* Update all routes with new endpoint IP and/or export RT
5685 if (is_vni_live(vpn
))
5686 update_routes_for_vni(bgp
, vpn
);
5689 /* Create or update as appropriate. */
5691 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
);
5695 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5701 /* if the VNI is live already, there is nothing more to do */
5702 if (is_vni_live(vpn
))
5705 /* Mark as "live" */
5706 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5708 /* tunnel is now active, add tunnel-ip to db */
5709 bgp_tip_add(bgp
, &originator_ip
);
5711 /* filter routes as nexthop database has changed */
5712 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5715 * Create EVPN type-3 route and schedule for processing.
5717 * RT-3 only if doing head-end replication
5719 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
5720 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5721 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0)) {
5722 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5723 "%u: Type3 route creation failure for VNI %u",
5729 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5733 install_routes_for_vni(bgp
, vpn
);
5735 /* If we are advertising gateway mac-ip
5736 It needs to be conveyed again to zebra */
5737 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5743 * bgp_evpn_local_es_del
5745 int bgp_evpn_local_es_del(struct bgp
*bgp
,
5747 struct ipaddr
*originator_ip
)
5749 char buf
[ESI_STR_LEN
];
5750 struct evpnes
*es
= NULL
;
5752 if (!bgp
->esihash
) {
5753 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5758 /* Lookup ESI hash - should exist. */
5759 es
= bgp_evpn_lookup_es(bgp
, esi
);
5761 flog_warn(EC_BGP_EVPN_ESI
,
5762 "%u: ESI hash entry for ESI %s at Local ES DEL",
5763 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5767 /* Delete all local EVPN ES routes from ESI table
5768 * and schedule for processing (to withdraw from peers))
5770 delete_routes_for_es(bgp
, es
);
5772 /* free the hash entry */
5773 bgp_evpn_es_free(bgp
, es
);
5779 * bgp_evpn_local_es_add
5781 int bgp_evpn_local_es_add(struct bgp
*bgp
,
5783 struct ipaddr
*originator_ip
)
5785 char buf
[ESI_STR_LEN
];
5786 struct evpnes
*es
= NULL
;
5787 struct prefix_evpn p
;
5789 if (!bgp
->esihash
) {
5790 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5795 /* create the new es */
5796 es
= bgp_evpn_lookup_es(bgp
, esi
);
5798 es
= bgp_evpn_es_new(bgp
, esi
, originator_ip
);
5802 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
5803 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5807 UNSET_FLAG(es
->flags
, EVPNES_REMOTE
);
5808 SET_FLAG(es
->flags
, EVPNES_LOCAL
);
5810 build_evpn_type4_prefix(&p
, esi
, originator_ip
->ipaddr_v4
);
5811 if (update_evpn_type4_route(bgp
, es
, &p
)) {
5812 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5813 "%u: Type4 route creation failure for ESI %s",
5814 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5818 /* import all remote ES routes in th ES table */
5819 install_routes_for_es(bgp
, es
);
5825 * Handle change in setting for BUM handling. The supported values
5826 * are head-end replication and dropping all BUM packets. Any change
5827 * should be registered with zebra. Also, if doing head-end replication,
5828 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5829 * to be dropped, the RT-3s must be withdrawn.
5831 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
5833 zlog_info("L2VPN EVPN BUM handling is %s",
5834 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
5835 "Flooding" : "Flooding Disabled");
5837 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
5838 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
5839 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
5840 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
5841 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
5845 * Cleanup EVPN information on disable - Need to delete and withdraw
5846 * EVPN routes from peers.
5848 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5850 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_bucket
*,
5851 void *))cleanup_vni_on_disable
,
5856 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5857 * BGP instance (default) is being freed.
5859 void bgp_evpn_cleanup(struct bgp
*bgp
)
5861 hash_iterate(bgp
->vnihash
,
5862 (void (*)(struct hash_bucket
*, void *))free_vni_entry
,
5865 hash_free(bgp
->import_rt_hash
);
5866 bgp
->import_rt_hash
= NULL
;
5868 hash_free(bgp
->vrf_import_rt_hash
);
5869 bgp
->vrf_import_rt_hash
= NULL
;
5871 hash_free(bgp
->vnihash
);
5872 bgp
->vnihash
= NULL
;
5874 hash_free(bgp
->esihash
);
5875 bgp
->esihash
= NULL
;
5877 list_delete(&bgp
->vrf_import_rtl
);
5878 list_delete(&bgp
->vrf_export_rtl
);
5879 list_delete(&bgp
->l2vnis
);
5883 * Initialization for EVPN
5886 * hash for RT to VNI
5888 void bgp_evpn_init(struct bgp
*bgp
)
5891 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
5893 hash_create(esi_hash_keymake
, esi_cmp
,
5894 "BGP EVPN Local ESI Hash");
5895 bgp
->import_rt_hash
=
5896 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
5897 "BGP Import RT Hash");
5898 bgp
->vrf_import_rt_hash
=
5899 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
5900 "BGP VRF Import RT Hash");
5901 bgp
->vrf_import_rtl
= list_new();
5902 bgp
->vrf_import_rtl
->cmp
=
5903 (int (*)(void *, void *))evpn_route_target_cmp
;
5904 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
5905 bgp
->vrf_export_rtl
= list_new();
5906 bgp
->vrf_export_rtl
->cmp
=
5907 (int (*)(void *, void *))evpn_route_target_cmp
;
5908 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
5909 bgp
->l2vnis
= list_new();
5910 bgp
->l2vnis
->cmp
= vni_list_cmp
;
5911 /* By default Duplicate Address Dection is enabled.
5912 * Max-moves (N) 5, detection time (M) 180
5913 * default action is warning-only
5914 * freeze action permanently freezes address,
5915 * and freeze time (auto-recovery) is disabled.
5917 if (bgp
->evpn_info
) {
5918 bgp
->evpn_info
->dup_addr_detect
= true;
5919 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
5920 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
5921 bgp
->evpn_info
->dad_freeze
= false;
5922 bgp
->evpn_info
->dad_freeze_time
= 0;
5923 /* Initialize zebra vxlan */
5924 bgp_zebra_dup_addr_detection(bgp
);
5927 /* Default BUM handling is to do head-end replication. */
5928 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
5931 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
5933 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);