1 /* Ethernet-VPN Packet and vty Processing File
2 * Copyright (C) 2016 6WIND
3 * Copyright (C) 2017 Cumulus Networks, Inc.
5 * This file is part of FRR.
7 * FRRouting is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * FRRouting is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "bgpd/bgp_attr_evpn.h"
35 #include "bgpd/bgpd.h"
36 #include "bgpd/bgp_table.h"
37 #include "bgpd/bgp_route.h"
38 #include "bgpd/bgp_attr.h"
39 #include "bgpd/bgp_mplsvpn.h"
40 #include "bgpd/bgp_label.h"
41 #include "bgpd/bgp_evpn.h"
42 #include "bgpd/bgp_evpn_private.h"
43 #include "bgpd/bgp_ecommunity.h"
44 #include "bgpd/bgp_encap_types.h"
45 #include "bgpd/bgp_debug.h"
46 #include "bgpd/bgp_errors.h"
47 #include "bgpd/bgp_aspath.h"
48 #include "bgpd/bgp_zebra.h"
49 #include "bgpd/bgp_nexthop.h"
50 #include "bgpd/bgp_addpath.h"
53 * Definitions and external declarations.
55 extern struct zclient
*zclient
;
57 DEFINE_QOBJ_TYPE(bgpevpn
)
58 DEFINE_QOBJ_TYPE(evpnes
)
62 * Static function declarations
64 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
66 struct bgp_path_info
**pi
);
67 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
73 /* compare two IPV4 VTEP IPs */
74 static int evpn_vtep_ip_cmp(void *p1
, void *p2
)
76 const struct in_addr
*ip1
= p1
;
77 const struct in_addr
*ip2
= p2
;
79 return ip1
->s_addr
- ip2
->s_addr
;
83 * Make hash key for ESI.
85 static unsigned int esi_hash_keymake(void *p
)
87 struct evpnes
*pes
= p
;
88 const void *pnt
= (void *)pes
->esi
.val
;
90 return jhash(pnt
, ESI_BYTES
, 0xa5a5a55a);
96 static bool esi_cmp(const void *p1
, const void *p2
)
98 const struct evpnes
*pes1
= p1
;
99 const struct evpnes
*pes2
= p2
;
101 if (pes1
== NULL
&& pes2
== NULL
)
104 if (pes1
== NULL
|| pes2
== NULL
)
107 return (memcmp(pes1
->esi
.val
, pes2
->esi
.val
, ESI_BYTES
) == 0);
113 static unsigned int vni_hash_key_make(void *p
)
115 struct bgpevpn
*vpn
= p
;
116 return (jhash_1word(vpn
->vni
, 0));
120 * Comparison function for vni hash
122 static bool vni_hash_cmp(const void *p1
, const void *p2
)
124 const struct bgpevpn
*vpn1
= p1
;
125 const struct bgpevpn
*vpn2
= p2
;
131 return (vpn1
->vni
== vpn2
->vni
);
134 static int vni_list_cmp(void *p1
, void *p2
)
136 const struct bgpevpn
*vpn1
= p1
;
137 const struct bgpevpn
*vpn2
= p2
;
139 return vpn1
->vni
- vpn2
->vni
;
143 * Make vrf import route target hash key.
145 static unsigned int vrf_import_rt_hash_key_make(void *p
)
147 struct vrf_irt_node
*irt
= p
;
148 char *pnt
= irt
->rt
.val
;
150 return jhash(pnt
, 8, 0x5abc1234);
154 * Comparison function for vrf import rt hash
156 static bool vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
158 const struct vrf_irt_node
*irt1
= p1
;
159 const struct vrf_irt_node
*irt2
= p2
;
161 if (irt1
== NULL
&& irt2
== NULL
)
164 if (irt1
== NULL
|| irt2
== NULL
)
167 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
171 * Create a new vrf import_rt in evpn instance
173 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
175 struct bgp
*bgp_evpn
= NULL
;
176 struct vrf_irt_node
*irt
;
178 bgp_evpn
= bgp_get_evpn();
180 flog_err(EC_BGP_NO_DFLT
,
181 "vrf import rt new - evpn instance not created yet");
185 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
186 sizeof(struct vrf_irt_node
));
189 irt
->vrfs
= list_new();
192 if (!hash_get(bgp_evpn
->vrf_import_rt_hash
, irt
, hash_alloc_intern
)) {
193 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
201 * Free the vrf import rt node
203 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
205 struct bgp
*bgp_evpn
= NULL
;
207 bgp_evpn
= bgp_get_evpn();
209 flog_err(EC_BGP_NO_DFLT
,
210 "vrf import rt free - evpn instance not created yet");
214 hash_release(bgp_evpn
->vrf_import_rt_hash
, irt
);
215 list_delete(&irt
->vrfs
);
216 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
220 * Function to lookup Import RT node - used to map a RT to set of
221 * VNIs importing routes with that RT.
223 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
225 struct bgp
*bgp_evpn
= NULL
;
226 struct vrf_irt_node
*irt
;
227 struct vrf_irt_node tmp
;
229 bgp_evpn
= bgp_get_evpn();
233 "vrf import rt lookup - evpn instance not created yet");
237 memset(&tmp
, 0, sizeof(struct vrf_irt_node
));
238 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
239 irt
= hash_lookup(bgp_evpn
->vrf_import_rt_hash
, &tmp
);
244 * Is specified VRF present on the RT's list of "importing" VRFs?
246 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
, struct bgp
*bgp_vrf
)
248 struct listnode
*node
= NULL
, *nnode
= NULL
;
249 struct bgp
*tmp_bgp_vrf
= NULL
;
251 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
252 if (tmp_bgp_vrf
== bgp_vrf
)
259 * Make import route target hash key.
261 static unsigned int import_rt_hash_key_make(void *p
)
263 struct irt_node
*irt
= p
;
264 char *pnt
= irt
->rt
.val
;
266 return jhash(pnt
, 8, 0xdeadbeef);
270 * Comparison function for import rt hash
272 static bool import_rt_hash_cmp(const void *p1
, const void *p2
)
274 const struct irt_node
*irt1
= p1
;
275 const struct irt_node
*irt2
= p2
;
277 if (irt1
== NULL
&& irt2
== NULL
)
280 if (irt1
== NULL
|| irt2
== NULL
)
283 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
287 * Create a new import_rt
289 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
290 struct ecommunity_val
*rt
)
292 struct irt_node
*irt
;
297 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
300 irt
->vnis
= list_new();
303 if (!hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
)) {
304 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
312 * Free the import rt node
314 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
316 hash_release(bgp
->import_rt_hash
, irt
);
317 list_delete(&irt
->vnis
);
318 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
322 * Function to lookup Import RT node - used to map a RT to set of
323 * VNIs importing routes with that RT.
325 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
326 struct ecommunity_val
*rt
)
328 struct irt_node
*irt
;
331 memset(&tmp
, 0, sizeof(struct irt_node
));
332 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
333 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
338 * Is specified VNI present on the RT's list of "importing" VNIs?
340 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
342 struct listnode
*node
, *nnode
;
343 struct bgpevpn
*tmp_vpn
;
345 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
354 * Compare Route Targets.
356 static int evpn_route_target_cmp(struct ecommunity
*ecom1
,
357 struct ecommunity
*ecom2
)
365 if (!ecom1
&& !ecom2
)
368 if (ecom1
->str
&& !ecom2
->str
)
371 if (!ecom1
->str
&& ecom2
->str
)
374 if (!ecom1
->str
&& !ecom2
->str
)
377 return strcmp(ecom1
->str
, ecom2
->str
);
380 static void evpn_xxport_delete_ecomm(void *val
)
382 struct ecommunity
*ecomm
= val
;
383 ecommunity_free(&ecomm
);
387 * Mask off global-admin field of specified extended community (RT),
388 * just retain the local-admin field.
390 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
391 struct ecommunity_val
*src
)
397 if (type
== ECOMMUNITY_ENCODE_AS
) {
398 dst
->val
[2] = dst
->val
[3] = 0;
399 } else if (type
== ECOMMUNITY_ENCODE_AS4
400 || type
== ECOMMUNITY_ENCODE_IP
) {
401 dst
->val
[2] = dst
->val
[3] = 0;
402 dst
->val
[4] = dst
->val
[5] = 0;
407 * Map one RT to specified VRF.
408 * bgp_vrf = BGP vrf instance
410 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct ecommunity_val
*eval
)
412 struct vrf_irt_node
*irt
= NULL
;
413 struct ecommunity_val eval_tmp
;
415 /* If using "automatic" RT,
416 * we only care about the local-admin sub-field.
417 * This is to facilitate using L3VNI(VRF-VNI)
418 * as the RT for EBGP peering too.
420 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
421 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
422 mask_ecom_global_admin(&eval_tmp
, eval
);
424 irt
= lookup_vrf_import_rt(&eval_tmp
);
425 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
426 /* Already mapped. */
430 irt
= vrf_import_rt_new(&eval_tmp
);
432 /* Add VRF to the list for this RT. */
433 listnode_add(irt
->vrfs
, bgp_vrf
);
437 * Unmap specified VRF from specified RT. If there are no other
438 * VRFs for this RT, then the RT hash is deleted.
439 * bgp_vrf: BGP VRF specific instance
441 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
, struct vrf_irt_node
*irt
)
443 /* Delete VRF from list for this RT. */
444 listnode_delete(irt
->vrfs
, bgp_vrf
);
445 if (!listnode_head(irt
->vrfs
)) {
446 vrf_import_rt_free(irt
);
451 * Map one RT to specified VNI.
453 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
454 struct ecommunity_val
*eval
)
456 struct irt_node
*irt
;
457 struct ecommunity_val eval_tmp
;
459 /* If using "automatic" RT, we only care about the local-admin
461 * This is to facilitate using VNI as the RT for EBGP peering too.
463 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
464 if (!is_import_rt_configured(vpn
))
465 mask_ecom_global_admin(&eval_tmp
, eval
);
467 irt
= lookup_import_rt(bgp
, &eval_tmp
);
469 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
470 /* Already mapped. */
474 irt
= import_rt_new(bgp
, &eval_tmp
);
478 /* Add VNI to the hash list for this RT. */
479 listnode_add(irt
->vnis
, vpn
);
483 * Unmap specified VNI from specified RT. If there are no other
484 * VNIs for this RT, then the RT hash is deleted.
486 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
487 struct irt_node
*irt
)
489 /* Delete VNI from hash list for this RT. */
490 listnode_delete(irt
->vnis
, vpn
);
491 if (!listnode_head(irt
->vnis
)) {
492 import_rt_free(bgp
, irt
);
497 * Create RT extended community automatically from passed information:
498 * of the form AS:VNI.
499 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
500 * the need is to get a RT value that will be unique across different
501 * VNIs but the same across routers (in the same AS) for a particular
504 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
506 struct ecommunity_val eval
;
507 struct ecommunity
*ecomadd
;
509 if (bgp
->advertise_autort_rfc8365
)
510 vni
|= EVPN_AUTORT_VXLAN
;
511 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
513 ecomadd
= ecommunity_new();
514 ecommunity_add_val(ecomadd
, &eval
);
515 listnode_add_sort(rtl
, ecomadd
);
519 * Derive RD and RT for a VNI automatically. Invoked at the time of
522 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
524 bgp_evpn_derive_auto_rd(bgp
, vpn
);
525 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
526 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
530 * Convert nexthop (remote VTEP IP) into an IPv6 address.
532 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
534 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
536 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
537 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
541 * Add (update) or delete MACIP from zebra.
543 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
544 struct prefix_evpn
*p
,
545 struct in_addr remote_vtep_ip
, int add
,
546 uint8_t flags
, uint32_t seq
)
550 char buf1
[ETHER_ADDR_STRLEN
];
551 char buf2
[INET6_ADDRSTRLEN
];
552 char buf3
[INET6_ADDRSTRLEN
];
555 if (!zclient
|| zclient
->sock
< 0)
558 /* Don't try to register if Zebra doesn't know of this instance. */
559 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
560 if (BGP_DEBUG(zebra
, ZEBRA
))
561 zlog_debug("%s: No zebra instance to talk to, not installing remote macip",
562 __PRETTY_FUNCTION__
);
568 zclient_create_header(
569 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
571 stream_putl(s
, vpn
->vni
);
572 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
573 /* IP address length and IP address, if any. */
574 if (is_evpn_prefix_ipaddr_none(p
))
577 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
579 stream_putl(s
, ipa_len
);
580 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
582 stream_put_in_addr(s
, &remote_vtep_ip
);
584 /* TX flags - MAC sticky status and/or gateway mac */
585 /* Also TX the sequence number of the best route. */
587 stream_putc(s
, flags
);
591 stream_putw_at(s
, 0, stream_get_endp(s
));
593 if (bgp_debug_zebra(NULL
))
595 "Tx %s MACIP, VNI %u MAC %s IP %s flags 0x%x seq %u remote VTEP %s",
596 add
? "ADD" : "DEL", vpn
->vni
,
597 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
599 ipaddr2str(&p
->prefix
.macip_addr
.ip
,
600 buf3
, sizeof(buf3
)), flags
, seq
,
601 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
604 return zclient_send_message(zclient
);
608 * Add (update) or delete remote VTEP from zebra.
610 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
611 struct prefix_evpn
*p
, int add
)
616 if (!zclient
|| zclient
->sock
< 0)
619 /* Don't try to register if Zebra doesn't know of this instance. */
620 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
621 if (BGP_DEBUG(zebra
, ZEBRA
))
622 zlog_debug("%s: No zebra instance to talk to, not installing remote vtep",
623 __PRETTY_FUNCTION__
);
630 zclient_create_header(
631 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
633 stream_putl(s
, vpn
->vni
);
634 if (is_evpn_prefix_ipaddr_v4(p
))
635 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
636 else if (is_evpn_prefix_ipaddr_v6(p
)) {
639 "Bad remote IP when trying to %s remote VTEP for VNI %u",
640 add
? "ADD" : "DEL", vpn
->vni
);
644 stream_putw_at(s
, 0, stream_get_endp(s
));
646 if (bgp_debug_zebra(NULL
))
647 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
648 add
? "ADD" : "DEL", vpn
->vni
,
649 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
651 return zclient_send_message(zclient
);
655 * Build extended community for EVPN ES (type-4) route
657 static void build_evpn_type4_route_extcomm(struct evpnes
*es
,
660 struct ecommunity ecom_encap
;
661 struct ecommunity ecom_es_rt
;
662 struct ecommunity_val eval
;
663 struct ecommunity_val eval_es_rt
;
664 bgp_encap_types tnl_type
;
668 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
669 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
670 encode_encap_extcomm(tnl_type
, &eval
);
672 ecom_encap
.val
= (uint8_t *)eval
.val
;
673 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
676 memset(&mac
, 0, sizeof(struct ethaddr
));
677 memset(&ecom_es_rt
, 0, sizeof(ecom_es_rt
));
678 es_get_system_mac(&es
->esi
, &mac
);
679 encode_es_rt_extcomm(&eval_es_rt
, &mac
);
681 ecom_es_rt
.val
= (uint8_t *)eval_es_rt
.val
;
683 ecommunity_merge(attr
->ecommunity
, &ecom_es_rt
);
685 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
689 * Build extended communities for EVPN prefix route.
691 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
694 struct ecommunity ecom_encap
;
695 struct ecommunity ecom_rmac
;
696 struct ecommunity_val eval
;
697 struct ecommunity_val eval_rmac
;
698 bgp_encap_types tnl_type
;
699 struct listnode
*node
, *nnode
;
700 struct ecommunity
*ecom
;
701 struct list
*vrf_export_rtl
= NULL
;
704 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
705 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
706 encode_encap_extcomm(tnl_type
, &eval
);
708 ecom_encap
.val
= (uint8_t *)eval
.val
;
711 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
713 /* Add the export RTs for L3VNI/VRF */
714 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
715 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
717 ecommunity_merge(attr
->ecommunity
, ecom
);
719 /* add the router mac extended community */
720 if (!is_zero_mac(&attr
->rmac
)) {
721 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
722 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
724 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
726 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
729 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
733 * Build extended communities for EVPN route.
734 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
735 * and ENCAP extended communities are applicable for all routes.
736 * The default gateway extended community and MAC mobility (sticky) extended
737 * community are added as needed based on passed settings - only for type-2
738 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
739 * added, if present, based on passed settings - only for non-link-local
742 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
745 struct ecommunity ecom_encap
;
746 struct ecommunity ecom_sticky
;
747 struct ecommunity ecom_default_gw
;
748 struct ecommunity ecom_rmac
;
749 struct ecommunity ecom_na
;
750 struct ecommunity_val eval
;
751 struct ecommunity_val eval_sticky
;
752 struct ecommunity_val eval_default_gw
;
753 struct ecommunity_val eval_rmac
;
754 struct ecommunity_val eval_na
;
756 bgp_encap_types tnl_type
;
757 struct listnode
*node
, *nnode
;
758 struct ecommunity
*ecom
;
760 struct list
*vrf_export_rtl
= NULL
;
763 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
764 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
765 encode_encap_extcomm(tnl_type
, &eval
);
767 ecom_encap
.val
= (uint8_t *)eval
.val
;
770 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
772 /* Add the export RTs for L2VNI */
773 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
774 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
776 /* Add the export RTs for L3VNI if told to - caller determines
777 * when this should be done.
780 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
781 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
782 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
784 attr
->ecommunity
= ecommunity_merge(
785 attr
->ecommunity
, ecom
);
789 /* Add MAC mobility (sticky) if needed. */
792 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
793 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
794 ecom_sticky
.size
= 1;
795 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
797 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
800 /* Add RMAC, if told to. */
802 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
803 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
805 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
807 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
810 /* Add default gateway, if needed. */
811 if (attr
->default_gw
) {
812 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
813 encode_default_gw_extcomm(&eval_default_gw
);
814 ecom_default_gw
.size
= 1;
815 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
817 ecommunity_merge(attr
->ecommunity
, &ecom_default_gw
);
820 if (attr
->router_flag
) {
821 memset(&ecom_na
, 0, sizeof(ecom_na
));
822 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
);
824 ecom_na
.val
= (uint8_t *)eval_na
.val
;
825 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
829 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
833 * Add MAC mobility extended community to attribute.
835 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
837 struct ecommunity ecom_tmp
;
838 struct ecommunity_val eval
;
839 uint8_t *ecom_val_ptr
;
846 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
848 /* Find current MM ecommunity */
851 if (attr
->ecommunity
) {
852 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
853 pnt
= attr
->ecommunity
->val
+ (i
* 8);
857 if (type
== ECOMMUNITY_ENCODE_EVPN
859 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
860 ecom_val_ptr
= (uint8_t *)(attr
->ecommunity
->val
867 /* Update the existing MM ecommunity */
869 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
871 /* Add MM to existing */
873 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
875 ecom_tmp
.val
= (uint8_t *)eval
.val
;
877 if (attr
->ecommunity
)
879 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
881 attr
->ecommunity
= ecommunity_dup(&ecom_tmp
);
885 /* Install EVPN route into zebra. */
886 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
887 struct prefix_evpn
*p
, struct bgp_path_info
*pi
)
892 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
894 if (pi
->attr
->sticky
)
895 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
896 if (pi
->attr
->default_gw
)
897 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
898 if (is_evpn_prefix_ipaddr_v6(p
) &&
899 pi
->attr
->router_flag
)
900 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
901 ret
= bgp_zebra_send_remote_macip(
902 bgp
, vpn
, p
, pi
->attr
->nexthop
, 1, flags
,
903 mac_mobility_seqnum(pi
->attr
));
905 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 1);
911 /* Uninstall EVPN route from zebra. */
912 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
913 struct prefix_evpn
*p
,
914 struct in_addr remote_vtep_ip
)
918 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
919 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
922 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 0);
928 * Due to MAC mobility, the prior "local" best route has been supplanted
929 * by a "remote" best route. The prior route has to be deleted and withdrawn
932 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
934 struct bgp_path_info
*old_local
)
936 struct bgp_node
*global_rn
;
937 struct bgp_path_info
*pi
;
938 afi_t afi
= AFI_L2VPN
;
939 safi_t safi
= SAFI_EVPN
;
941 /* Locate route node in the global EVPN routing table. Note that
942 * this table is a 2-level tree (RD-level + Prefix-level) similar to
945 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
946 (struct prefix
*)&rn
->p
, &vpn
->prd
);
948 /* Delete route entry in the global EVPN table. */
949 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
951 /* Schedule for processing - withdraws to peers happen from
955 bgp_process(bgp
, global_rn
, afi
, safi
);
956 bgp_unlock_node(global_rn
);
959 /* Delete route entry in the VNI route table, caller to remove. */
960 bgp_path_info_delete(rn
, old_local
);
963 static struct in_addr
*es_vtep_new(struct in_addr vtep
)
967 ip
= XCALLOC(MTYPE_BGP_EVPN_ES_VTEP
, sizeof(struct in_addr
));
969 ip
->s_addr
= vtep
.s_addr
;
973 static void es_vtep_free(struct in_addr
*ip
)
975 XFREE(MTYPE_BGP_EVPN_ES_VTEP
, ip
);
978 /* check if VTEP is already part of the list */
979 static int is_vtep_present_in_list(struct list
*list
,
982 struct listnode
*node
= NULL
;
985 for (ALL_LIST_ELEMENTS_RO(list
, node
, tmp
)) {
986 if (tmp
->s_addr
== vtep
.s_addr
)
993 * Best path for ES route was changed,
994 * update the list of VTEPs for this ES
996 static int evpn_es_install_vtep(struct bgp
*bgp
,
998 struct prefix_evpn
*p
,
999 struct in_addr rvtep
)
1001 struct in_addr
*vtep_ip
;
1003 if (is_vtep_present_in_list(es
->vtep_list
, rvtep
))
1007 vtep_ip
= es_vtep_new(rvtep
);
1009 listnode_add_sort(es
->vtep_list
, vtep_ip
);
1014 * Best path for ES route was changed,
1015 * update the list of VTEPs for this ES
1017 static int evpn_es_uninstall_vtep(struct bgp
*bgp
,
1019 struct prefix_evpn
*p
,
1020 struct in_addr rvtep
)
1022 struct listnode
*node
, *nnode
, *node_to_del
= NULL
;
1023 struct in_addr
*tmp
;
1025 for (ALL_LIST_ELEMENTS(es
->vtep_list
, node
, nnode
, tmp
)) {
1026 if (tmp
->s_addr
== rvtep
.s_addr
) {
1033 list_delete_node(es
->vtep_list
, node_to_del
);
1039 * Calculate the best path for a ES(type-4) route.
1041 static int evpn_es_route_select_install(struct bgp
*bgp
,
1043 struct bgp_node
*rn
)
1046 afi_t afi
= AFI_L2VPN
;
1047 safi_t safi
= SAFI_EVPN
;
1048 struct bgp_path_info
*old_select
; /* old best */
1049 struct bgp_path_info
*new_select
; /* new best */
1050 struct bgp_path_info_pair old_and_new
;
1052 /* Compute the best path. */
1053 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
],
1054 &old_and_new
, afi
, safi
);
1055 old_select
= old_and_new
.old
;
1056 new_select
= old_and_new
.new;
1059 * If the best path hasn't changed - see if something needs to be
1062 if (old_select
&& old_select
== new_select
1063 && old_select
->type
== ZEBRA_ROUTE_BGP
1064 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1065 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1066 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1067 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1068 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1069 ret
= evpn_es_install_vtep(bgp
, es
,
1070 (struct prefix_evpn
*)&rn
->p
,
1071 old_select
->attr
->nexthop
);
1073 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1074 bgp_zebra_clear_route_change_flags(rn
);
1078 /* If the user did a "clear" this flag will be set */
1079 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1082 * bestpath has changed; update relevant fields and install or uninstall
1083 * into the zebra RIB.
1085 if (old_select
|| new_select
)
1086 bgp_bump_version(rn
);
1089 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1091 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1092 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1093 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1096 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1097 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1098 ret
= evpn_es_install_vtep(bgp
, es
,
1099 (struct prefix_evpn
*)&rn
->p
,
1100 new_select
->attr
->nexthop
);
1102 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1103 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1104 ret
= evpn_es_uninstall_vtep(
1105 bgp
, es
, (struct prefix_evpn
*)&rn
->p
,
1106 old_select
->attr
->nexthop
);
1109 /* Clear any route change flags. */
1110 bgp_zebra_clear_route_change_flags(rn
);
1112 /* Reap old select bgp_path_info, if it has been removed */
1113 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1114 bgp_path_info_reap(rn
, old_select
);
1120 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1123 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1124 struct bgp_node
*rn
)
1126 struct bgp_path_info
*old_select
, *new_select
;
1127 struct bgp_path_info_pair old_and_new
;
1128 afi_t afi
= AFI_L2VPN
;
1129 safi_t safi
= SAFI_EVPN
;
1132 /* Compute the best path. */
1133 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1135 old_select
= old_and_new
.old
;
1136 new_select
= old_and_new
.new;
1138 /* If the best path hasn't changed - see if there is still something to
1142 if (old_select
&& old_select
== new_select
1143 && old_select
->type
== ZEBRA_ROUTE_BGP
1144 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1145 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1146 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1147 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1148 if (bgp_zebra_has_route_changed(rn
, old_select
))
1149 ret
= evpn_zebra_install(
1150 bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1152 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1153 bgp_zebra_clear_route_change_flags(rn
);
1157 /* If the user did a "clear" this flag will be set */
1158 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1160 /* bestpath has changed; update relevant fields and install or uninstall
1161 * into the zebra RIB.
1163 if (old_select
|| new_select
)
1164 bgp_bump_version(rn
);
1167 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1169 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1170 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1171 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1174 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1175 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1176 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1179 /* If an old best existed and it was a "local" route, the only
1181 * it would be supplanted is due to MAC mobility procedures. So,
1183 * need to do an implicit delete and withdraw that route from
1186 if (old_select
&& old_select
->peer
== bgp
->peer_self
1187 && old_select
->type
== ZEBRA_ROUTE_BGP
1188 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
1189 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
1191 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1192 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1193 ret
= evpn_zebra_uninstall(bgp
, vpn
,
1194 (struct prefix_evpn
*)&rn
->p
,
1195 old_select
->attr
->nexthop
);
1198 /* Clear any route change flags. */
1199 bgp_zebra_clear_route_change_flags(rn
);
1201 /* Reap old select bgp_path_info, if it has been removed */
1202 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1203 bgp_path_info_reap(rn
, old_select
);
1209 * Return true if the local ri for this rn is of type gateway mac
1211 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
1213 struct bgp_path_info
*tmp_pi
= NULL
;
1214 struct bgp_path_info
*local_pi
= NULL
;
1217 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1218 tmp_pi
= tmp_pi
->next
) {
1219 if (tmp_pi
->peer
== bgp
->peer_self
1220 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1221 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1228 return local_pi
->attr
->default_gw
;
1233 * Return true if the local ri for this rn has sticky set
1235 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
1237 struct bgp_path_info
*tmp_pi
;
1238 struct bgp_path_info
*local_pi
;
1241 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1242 tmp_pi
= tmp_pi
->next
) {
1243 if (tmp_pi
->peer
== bgp
->peer_self
1244 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1245 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1252 return local_pi
->attr
->sticky
;
1256 * create or update EVPN type4 route entry.
1257 * This could be in the ES table or the global table.
1258 * TODO: handle remote ES (type4) routes as well
1260 static int update_evpn_type4_route_entry(struct bgp
*bgp
, struct evpnes
*es
,
1261 afi_t afi
, safi_t safi
,
1262 struct bgp_node
*rn
, struct attr
*attr
,
1263 int add
, struct bgp_path_info
**ri
,
1266 char buf
[ESI_STR_LEN
];
1267 char buf1
[INET6_ADDRSTRLEN
];
1268 struct bgp_path_info
*tmp_pi
= NULL
;
1269 struct bgp_path_info
*local_pi
= NULL
; /* local route entry if any */
1270 struct bgp_path_info
*remote_pi
= NULL
; /* remote route entry if any */
1271 struct attr
*attr_new
= NULL
;
1272 struct prefix_evpn
*evp
= NULL
;
1276 evp
= (struct prefix_evpn
*)&rn
->p
;
1278 /* locate the local and remote entries if any */
1279 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1280 tmp_pi
= tmp_pi
->next
) {
1281 if (tmp_pi
->peer
== bgp
->peer_self
1282 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1283 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1285 if (tmp_pi
->type
== ZEBRA_ROUTE_BGP
1286 && tmp_pi
->sub_type
== BGP_ROUTE_IMPORTED
1287 && CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1291 /* we don't expect to see a remote_ri at this point.
1292 * An ES route has esi + vtep_ip as the key,
1293 * We shouldn't see the same route from any other vtep.
1298 "%u ERROR: local es route for ESI: %s Vtep %s also learnt from remote",
1300 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1301 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1305 if (!local_pi
&& !add
)
1308 /* create or update the entry */
1311 /* Add or update attribute to hash */
1312 attr_new
= bgp_attr_intern(attr
);
1314 /* Create new route with its attribute. */
1315 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1316 bgp
->peer_self
, attr_new
, rn
);
1317 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1319 /* add the newly created path to the route-node */
1320 bgp_path_info_add(rn
, tmp_pi
);
1323 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1324 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1327 /* The attribute has changed.
1328 * Add (or update) attribute to hash. */
1329 attr_new
= bgp_attr_intern(attr
);
1330 bgp_path_info_set_flag(rn
, tmp_pi
,
1331 BGP_PATH_ATTR_CHANGED
);
1333 /* Restore route, if needed. */
1334 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1335 bgp_path_info_restore(rn
, tmp_pi
);
1337 /* Unintern existing, set to new. */
1338 bgp_attr_unintern(&tmp_pi
->attr
);
1339 tmp_pi
->attr
= attr_new
;
1340 tmp_pi
->uptime
= bgp_clock();
1344 /* Return back the route entry. */
1349 /* update evpn es (type-4) route */
1350 static int update_evpn_type4_route(struct bgp
*bgp
,
1352 struct prefix_evpn
*p
)
1355 int route_changed
= 0;
1356 char buf
[ESI_STR_LEN
];
1357 char buf1
[INET6_ADDRSTRLEN
];
1358 afi_t afi
= AFI_L2VPN
;
1359 safi_t safi
= SAFI_EVPN
;
1361 struct attr
*attr_new
= NULL
;
1362 struct bgp_node
*rn
= NULL
;
1363 struct bgp_path_info
*pi
= NULL
;
1365 memset(&attr
, 0, sizeof(struct attr
));
1367 /* Build path-attribute for this route. */
1368 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1369 attr
.nexthop
= es
->originator_ip
.ipaddr_v4
;
1370 attr
.mp_nexthop_global_in
= es
->originator_ip
.ipaddr_v4
;
1371 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1373 /* Set up extended community. */
1374 build_evpn_type4_route_extcomm(es
, &attr
);
1376 /* First, create (or fetch) route node within the ESI. */
1377 /* NOTE: There is no RD here. */
1378 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
1380 /* Create or update route entry. */
1381 ret
= update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, &attr
, 1,
1382 &pi
, &route_changed
);
1384 flog_err(EC_BGP_ES_INVALID
,
1385 "%u ERROR: Failed to updated ES route ESI: %s VTEP %s",
1387 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1388 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1392 attr_new
= pi
->attr
;
1394 /* Perform route selection;
1395 * this is just to set the flags correctly
1396 * as local route in the ES always wins.
1398 evpn_es_route_select_install(bgp
, es
, rn
);
1399 bgp_unlock_node(rn
);
1401 /* If this is a new route or some attribute has changed, export the
1402 * route to the global table. The route will be advertised to peers
1403 * from there. Note that this table is a 2-level tree (RD-level +
1404 * Prefix-level) similar to L3VPN routes.
1406 if (route_changed
) {
1407 struct bgp_path_info
*global_pi
;
1409 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1410 (struct prefix
*)p
, &es
->prd
);
1411 update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, attr_new
,
1412 1, &global_pi
, &route_changed
);
1414 /* Schedule for processing and unlock node. */
1415 bgp_process(bgp
, rn
, afi
, safi
);
1416 bgp_unlock_node(rn
);
1419 /* Unintern temporary. */
1420 aspath_unintern(&attr
.aspath
);
1424 static int update_evpn_type5_route_entry(struct bgp
*bgp_evpn
,
1425 struct bgp
*bgp_vrf
, afi_t afi
,
1426 safi_t safi
, struct bgp_node
*rn
,
1427 struct attr
*attr
, int *route_changed
)
1429 struct attr
*attr_new
= NULL
;
1430 struct bgp_path_info
*pi
= NULL
;
1431 mpls_label_t label
= MPLS_INVALID_LABEL
;
1432 struct bgp_path_info
*local_pi
= NULL
;
1433 struct bgp_path_info
*tmp_pi
= NULL
;
1436 /* locate the local route entry if any */
1437 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1438 tmp_pi
= tmp_pi
->next
) {
1439 if (tmp_pi
->peer
== bgp_evpn
->peer_self
1440 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1441 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1446 * create a new route entry if one doesn't exist.
1447 * Otherwise see if route attr has changed
1451 /* route has changed as this is the first entry */
1454 /* Add (or update) attribute to hash. */
1455 attr_new
= bgp_attr_intern(attr
);
1457 /* create the route info from attribute */
1458 pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1459 bgp_evpn
->peer_self
, attr_new
, rn
);
1460 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
1462 /* Type-5 routes advertise the L3-VNI */
1463 bgp_path_info_extra_get(pi
);
1464 vni2label(bgp_vrf
->l3vni
, &label
);
1465 memcpy(&pi
->extra
->label
, &label
, sizeof(label
));
1466 pi
->extra
->num_labels
= 1;
1468 /* add the route entry to route node*/
1469 bgp_path_info_add(rn
, pi
);
1473 if (!attrhash_cmp(tmp_pi
->attr
, attr
)) {
1475 /* attribute changed */
1478 /* The attribute has changed. */
1479 /* Add (or update) attribute to hash. */
1480 attr_new
= bgp_attr_intern(attr
);
1481 bgp_path_info_set_flag(rn
, tmp_pi
,
1482 BGP_PATH_ATTR_CHANGED
);
1484 /* Restore route, if needed. */
1485 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1486 bgp_path_info_restore(rn
, tmp_pi
);
1488 /* Unintern existing, set to new. */
1489 bgp_attr_unintern(&tmp_pi
->attr
);
1490 tmp_pi
->attr
= attr_new
;
1491 tmp_pi
->uptime
= bgp_clock();
1497 /* update evpn type-5 route entry */
1498 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1499 struct attr
*src_attr
)
1501 afi_t afi
= AFI_L2VPN
;
1502 safi_t safi
= SAFI_EVPN
;
1504 struct bgp_node
*rn
= NULL
;
1505 struct bgp
*bgp_evpn
= NULL
;
1506 int route_changed
= 0;
1508 bgp_evpn
= bgp_get_evpn();
1512 /* Build path attribute for this route - use the source attr, if
1513 * present, else treat as locally originated.
1516 bgp_attr_dup(&attr
, src_attr
);
1518 memset(&attr
, 0, sizeof(struct attr
));
1519 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1521 /* Set nexthop to ourselves and fill in the Router MAC. */
1522 attr
.nexthop
= bgp_vrf
->originator_ip
;
1523 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1524 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1525 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
1527 /* Setup RT and encap extended community */
1528 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1530 /* get the route node in global table */
1531 rn
= bgp_afi_node_get(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1532 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1535 /* create or update the route entry within the route node */
1536 update_evpn_type5_route_entry(bgp_evpn
, bgp_vrf
, afi
, safi
, rn
, &attr
,
1539 /* schedule for processing and unlock node */
1540 if (route_changed
) {
1541 bgp_process(bgp_evpn
, rn
, afi
, safi
);
1542 bgp_unlock_node(rn
);
1545 /* uninten temporary */
1547 aspath_unintern(&attr
.aspath
);
1552 * Create or update EVPN route entry. This could be in the VNI route table
1553 * or the global route table.
1555 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1556 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1557 struct attr
*attr
, int add
,
1558 struct bgp_path_info
**pi
, uint8_t flags
,
1561 struct bgp_path_info
*tmp_pi
;
1562 struct bgp_path_info
*local_pi
;
1563 struct attr
*attr_new
;
1564 mpls_label_t label
[BGP_MAX_LABELS
];
1565 uint32_t num_labels
= 1;
1566 int route_change
= 1;
1568 struct prefix_evpn
*evp
;
1571 evp
= (struct prefix_evpn
*)&rn
->p
;
1572 memset(&label
, 0, sizeof(label
));
1574 /* See if this is an update of an existing route, or a new add. */
1576 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1577 tmp_pi
= tmp_pi
->next
) {
1578 if (tmp_pi
->peer
== bgp
->peer_self
1579 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1580 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1584 /* If route doesn't exist already, create a new one, if told to.
1585 * Otherwise act based on whether the attributes of the route have
1588 if (!local_pi
&& !add
)
1591 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1592 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1593 add_mac_mobility_to_attr(seq
, attr
);
1596 /* Add (or update) attribute to hash. */
1597 attr_new
= bgp_attr_intern(attr
);
1599 /* Extract MAC mobility sequence number, if any. */
1600 attr_new
->mm_seqnum
=
1601 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1602 attr_new
->sticky
= sticky
;
1604 /* Create new route with its attribute. */
1605 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1606 bgp
->peer_self
, attr_new
, rn
);
1607 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1608 bgp_path_info_extra_get(tmp_pi
);
1610 /* The VNI goes into the 'label' field of the route */
1611 vni2label(vpn
->vni
, &label
[0]);
1613 /* Type-2 routes may carry a second VNI - the L3-VNI.
1614 * Only attach second label if we are advertising two labels for
1617 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1618 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1621 l3vni
= bgpevpn_get_l3vni(vpn
);
1623 vni2label(l3vni
, &label
[1]);
1628 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1629 tmp_pi
->extra
->num_labels
= num_labels
;
1630 bgp_path_info_add(rn
, tmp_pi
);
1633 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1634 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1638 * The attributes have changed, type-2 routes needs to
1639 * be advertised with right labels.
1641 vni2label(vpn
->vni
, &label
[0]);
1642 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1643 && CHECK_FLAG(vpn
->flags
,
1644 VNI_FLAG_USE_TWO_LABELS
)) {
1647 l3vni
= bgpevpn_get_l3vni(vpn
);
1649 vni2label(l3vni
, &label
[1]);
1653 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1654 tmp_pi
->extra
->num_labels
= num_labels
;
1656 /* The attribute has changed. */
1657 /* Add (or update) attribute to hash. */
1658 attr_new
= bgp_attr_intern(attr
);
1659 bgp_path_info_set_flag(rn
, tmp_pi
,
1660 BGP_PATH_ATTR_CHANGED
);
1662 /* Extract MAC mobility sequence number, if any. */
1663 attr_new
->mm_seqnum
=
1664 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1665 attr_new
->sticky
= sticky
;
1667 /* Restore route, if needed. */
1668 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1669 bgp_path_info_restore(rn
, tmp_pi
);
1671 /* Unintern existing, set to new. */
1672 bgp_attr_unintern(&tmp_pi
->attr
);
1673 tmp_pi
->attr
= attr_new
;
1674 tmp_pi
->uptime
= bgp_clock();
1678 /* Return back the route entry. */
1680 return route_change
;
1683 static void evpn_zebra_reinstall_best_route(struct bgp
*bgp
,
1684 struct bgpevpn
*vpn
, struct bgp_node
*rn
)
1686 struct bgp_path_info
*tmp_ri
;
1687 struct bgp_path_info
*curr_select
= NULL
;
1689 for (tmp_ri
= bgp_node_get_bgp_path_info(rn
);
1690 tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1691 if (CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_SELECTED
)) {
1692 curr_select
= tmp_ri
;
1697 if (curr_select
&& curr_select
->type
== ZEBRA_ROUTE_BGP
1698 && curr_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1699 evpn_zebra_install(bgp
, vpn
,
1700 (struct prefix_evpn
*)&rn
->p
,
1705 * If the local route was not selected evict it and tell zebra to re-add
1706 * the best remote dest.
1708 * Typically a local path added by zebra is expected to be selected as
1709 * best. In which case when a remote path wins as best (later)
1710 * evpn_route_select_install itself evicts the older-local-best path.
1712 * However if bgp's add and zebra's add cross paths (race condition) it
1713 * is possible that the local path is no longer the "older" best path.
1714 * It is a path that was never designated as best and hence requires
1715 * additional handling to prevent bgp from injecting and holding on to a
1716 * non-best local path.
1718 static void evpn_cleanup_local_non_best_route(struct bgp
*bgp
,
1719 struct bgpevpn
*vpn
,
1720 struct bgp_node
*rn
,
1721 struct bgp_path_info
*local_pi
)
1723 char buf
[PREFIX_STRLEN
];
1725 /* local path was not picked as the winner; kick it out */
1726 if (bgp_debug_zebra(NULL
)) {
1727 zlog_debug("evicting local evpn prefix %s as remote won",
1728 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
1730 evpn_delete_old_local_route(bgp
, vpn
, rn
, local_pi
);
1731 bgp_path_info_reap(rn
, local_pi
);
1733 /* tell zebra to re-add the best remote path */
1734 evpn_zebra_reinstall_best_route(bgp
, vpn
, rn
);
1738 * Create or update EVPN route (of type based on prefix) for specified VNI
1739 * and schedule for processing.
1741 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1742 struct prefix_evpn
*p
, uint8_t flags
,
1745 struct bgp_node
*rn
;
1747 struct attr
*attr_new
;
1748 int add_l3_ecomm
= 0;
1749 struct bgp_path_info
*pi
;
1750 afi_t afi
= AFI_L2VPN
;
1751 safi_t safi
= SAFI_EVPN
;
1754 memset(&attr
, 0, sizeof(struct attr
));
1756 /* Build path-attribute for this route. */
1757 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1758 attr
.nexthop
= vpn
->originator_ip
;
1759 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1760 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1761 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1762 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1763 attr
.router_flag
= CHECK_FLAG(flags
,
1764 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1766 /* PMSI is only needed for type-3 routes */
1767 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
1768 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1769 attr
.pmsi_tnl_type
= PMSI_TNLTYPE_INGR_REPL
;
1772 /* router mac is only needed for type-2 routes here. */
1773 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1774 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1775 vni2label(vpn
->vni
, &(attr
.label
));
1777 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1778 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1781 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1782 (is_evpn_prefix_ipaddr_v4(p
) ||
1783 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1784 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1785 bgpevpn_get_l3vni(vpn
))
1788 /* Set up extended community. */
1789 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1791 /* First, create (or fetch) route node within the VNI. */
1792 /* NOTE: There is no RD here. */
1793 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1795 /* Create or update route entry. */
1796 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1797 1, &pi
, flags
, seq
);
1799 attr_new
= pi
->attr
;
1801 /* lock ri to prevent freeing in evpn_route_select_install */
1802 bgp_path_info_lock(pi
);
1803 /* Perform route selection; this is just to set the flags correctly
1804 * as local route in the VNI always wins.
1806 evpn_route_select_install(bgp
, vpn
, rn
);
1808 * If the new local route was not selected evict it and tell zebra
1809 * to re-add the best remote dest. BGP doesn't retain non-best local
1812 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1814 evpn_cleanup_local_non_best_route(bgp
, vpn
, rn
, pi
);
1816 bgp_path_info_unlock(pi
);
1818 bgp_unlock_node(rn
);
1820 /* If this is a new route or some attribute has changed, export the
1821 * route to the global table. The route will be advertised to peers
1822 * from there. Note that this table is a 2-level tree (RD-level +
1823 * Prefix-level) similar to L3VPN routes.
1826 struct bgp_path_info
*global_pi
;
1828 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1829 (struct prefix
*)p
, &vpn
->prd
);
1830 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1,
1831 &global_pi
, flags
, seq
);
1833 /* Schedule for processing and unlock node. */
1834 bgp_process(bgp
, rn
, afi
, safi
);
1835 bgp_unlock_node(rn
);
1838 /* Unintern temporary. */
1839 aspath_unintern(&attr
.aspath
);
1845 * Delete EVPN route entry.
1846 * The entry can be in ESI/VNI table or the global table.
1848 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1849 struct bgp_node
*rn
,
1850 struct bgp_path_info
**pi
)
1852 struct bgp_path_info
*tmp_pi
;
1856 /* Now, find matching route. */
1857 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
1858 tmp_pi
= tmp_pi
->next
)
1859 if (tmp_pi
->peer
== bgp
->peer_self
1860 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1861 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1866 /* Mark route for delete. */
1868 bgp_path_info_delete(rn
, tmp_pi
);
1873 /* Delete EVPN ES (type-4) route */
1874 static int delete_evpn_type4_route(struct bgp
*bgp
,
1876 struct prefix_evpn
*p
)
1878 afi_t afi
= AFI_L2VPN
;
1879 safi_t safi
= SAFI_EVPN
;
1880 struct bgp_path_info
*pi
;
1881 struct bgp_node
*rn
= NULL
; /* rn in esi table */
1882 struct bgp_node
*global_rn
= NULL
; /* rn in global table */
1884 /* First, locate the route node within the ESI.
1885 * If it doesn't exist, ther is nothing to do.
1886 * Note: there is no RD here.
1888 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
1892 /* Next, locate route node in the global EVPN routing table.
1893 * Note that this table is a 2-level tree (RD-level + Prefix-level)
1895 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1896 (struct prefix
*)p
, &es
->prd
);
1899 /* Delete route entry in the global EVPN table. */
1900 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1902 /* Schedule for processing - withdraws to peers happen from
1906 bgp_process(bgp
, global_rn
, afi
, safi
);
1907 bgp_unlock_node(global_rn
);
1911 * Delete route entry in the ESI route table.
1912 * This can just be removed.
1914 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
1916 bgp_path_info_reap(rn
, pi
);
1917 bgp_unlock_node(rn
);
1921 /* Delete EVPN type5 route */
1922 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1924 afi_t afi
= AFI_L2VPN
;
1925 safi_t safi
= SAFI_EVPN
;
1926 struct bgp_node
*rn
= NULL
;
1927 struct bgp_path_info
*pi
= NULL
;
1928 struct bgp
*bgp_evpn
= NULL
; /* evpn bgp instance */
1930 bgp_evpn
= bgp_get_evpn();
1934 /* locate the global route entry for this type-5 prefix */
1935 rn
= bgp_afi_node_lookup(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1936 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1940 delete_evpn_route_entry(bgp_evpn
, afi
, safi
, rn
, &pi
);
1942 bgp_process(bgp_evpn
, rn
, afi
, safi
);
1943 bgp_unlock_node(rn
);
1948 * Delete EVPN route (of type based on prefix) for specified VNI and
1949 * schedule for processing.
1951 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1952 struct prefix_evpn
*p
)
1954 struct bgp_node
*rn
, *global_rn
;
1955 struct bgp_path_info
*pi
;
1956 afi_t afi
= AFI_L2VPN
;
1957 safi_t safi
= SAFI_EVPN
;
1959 /* First, locate the route node within the VNI. If it doesn't exist,
1961 * is nothing further to do.
1963 /* NOTE: There is no RD here. */
1964 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1968 /* Next, locate route node in the global EVPN routing table. Note that
1969 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1972 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1973 (struct prefix
*)p
, &vpn
->prd
);
1975 /* Delete route entry in the global EVPN table. */
1976 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1978 /* Schedule for processing - withdraws to peers happen from
1982 bgp_process(bgp
, global_rn
, afi
, safi
);
1983 bgp_unlock_node(global_rn
);
1986 /* Delete route entry in the VNI route table. This can just be removed.
1988 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
1990 bgp_path_info_reap(rn
, pi
);
1991 evpn_route_select_install(bgp
, vpn
, rn
);
1993 bgp_unlock_node(rn
);
1999 * Update all type-2 (MACIP) local routes for this VNI - these should also
2000 * be scheduled for advertise to peers.
2002 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2006 struct bgp_node
*rn
;
2007 struct bgp_path_info
*pi
, *tmp_pi
;
2009 struct attr
*attr_new
;
2011 int add_l3_ecomm
= 0;
2016 /* Walk this VNI's route table and update local type-2 routes. For any
2017 * routes updated, update corresponding entry in the global table too.
2019 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2020 rn
= bgp_route_next(rn
)) {
2021 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2022 struct bgp_node
*rd_rn
;
2023 struct bgp_path_info
*global_pi
;
2025 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2028 /* Identify local route. */
2029 for (tmp_pi
= bgp_node_get_bgp_path_info(rn
); tmp_pi
;
2030 tmp_pi
= tmp_pi
->next
) {
2031 if (tmp_pi
->peer
== bgp
->peer_self
2032 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2033 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2041 * Build attribute per local route as the MAC mobility and
2042 * some other values could differ for different routes. The
2043 * attributes will be shared in the hash table.
2045 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
2046 attr
.nexthop
= vpn
->originator_ip
;
2047 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
2048 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
2049 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
2051 if (evpn_route_is_sticky(bgp
, rn
))
2053 else if (evpn_route_is_def_gw(bgp
, rn
)) {
2054 attr
.default_gw
= 1;
2055 if (is_evpn_prefix_ipaddr_v6(evp
))
2056 attr
.router_flag
= 1;
2059 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2060 * using L3 VNI for type-2 routes also.
2062 if ((is_evpn_prefix_ipaddr_v4(evp
) ||
2063 !IN6_IS_ADDR_LINKLOCAL(
2064 &evp
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
2065 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
2066 bgpevpn_get_l3vni(vpn
))
2069 /* Set up extended community. */
2070 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2072 seq
= mac_mobility_seqnum(tmp_pi
->attr
);
2074 /* Update the route entry. */
2075 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
, 0, &pi
,
2078 /* Perform route selection; this is just to set the flags
2079 * correctly as local route in the VNI always wins.
2081 evpn_route_select_install(bgp
, vpn
, rn
);
2083 attr_new
= pi
->attr
;
2085 /* Update route in global routing table. */
2086 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2087 (struct prefix
*)evp
, &vpn
->prd
);
2089 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
2091 mac_mobility_seqnum(attr_new
));
2093 /* Schedule for processing and unlock node. */
2094 bgp_process(bgp
, rd_rn
, afi
, safi
);
2095 bgp_unlock_node(rd_rn
);
2097 /* Unintern temporary. */
2098 aspath_unintern(&attr
.aspath
);
2106 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2107 * global routing table. These are also scheduled for withdraw from peers.
2109 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2113 struct bgp_node
*rdrn
, *rn
;
2114 struct bgp_table
*table
;
2115 struct bgp_path_info
*pi
;
2120 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
2121 if (rdrn
&& bgp_node_has_bgp_path_info_data(rdrn
)) {
2122 table
= bgp_node_get_bgp_table_info(rdrn
);
2123 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2124 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2126 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2129 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2131 bgp_process(bgp
, rn
, afi
, safi
);
2135 /* Unlock RD node. */
2137 bgp_unlock_node(rdrn
);
2143 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2144 * table as well as the per-VNI route table.
2146 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2150 struct bgp_node
*rn
;
2151 struct bgp_path_info
*pi
;
2156 /* First, walk the global route table for this VNI's type-2 local
2158 * EVPN routes are a 2-level table, first get the RD table.
2160 delete_global_type2_routes(bgp
, vpn
);
2162 /* Next, walk this VNI's route table and delete local type-2 routes. */
2163 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2164 rn
= bgp_route_next(rn
)) {
2165 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2167 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2170 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2172 /* Route entry in local table gets deleted immediately. */
2174 bgp_path_info_reap(rn
, pi
);
2181 * Delete all routes in per ES route-table
2183 static int delete_all_es_routes(struct bgp
*bgp
, struct evpnes
*es
)
2185 struct bgp_node
*rn
;
2186 struct bgp_path_info
*pi
, *nextpi
;
2188 /* Walk this ES's route table and delete all routes. */
2189 for (rn
= bgp_table_top(es
->route_table
); rn
;
2190 rn
= bgp_route_next(rn
)) {
2191 for (pi
= bgp_node_get_bgp_path_info(rn
);
2192 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2193 bgp_path_info_delete(rn
, pi
);
2194 bgp_path_info_reap(rn
, pi
);
2202 * Delete all routes in the per-VNI route table.
2204 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2206 struct bgp_node
*rn
;
2207 struct bgp_path_info
*pi
, *nextpi
;
2209 /* Walk this VNI's route table and delete all routes. */
2210 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2211 rn
= bgp_route_next(rn
)) {
2212 for (pi
= bgp_node_get_bgp_path_info(rn
);
2213 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2214 bgp_path_info_delete(rn
, pi
);
2215 bgp_path_info_reap(rn
, pi
);
2223 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2224 * export RT getting modified or change to tunnel IP. Note that these
2225 * situations need the route in the per-VNI table as well as the global
2226 * table to be updated (as attributes change).
2228 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2231 struct prefix_evpn p
;
2233 /* Update and advertise the type-3 route (only one) followed by the
2234 * locally learnt type-2 routes (MACIP) - for this VNI.
2236 * RT-3 only if doing head-end replication
2238 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
2239 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2240 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0);
2245 return update_all_type2_routes(bgp
, vpn
);
2248 /* Delete (and withdraw) local routes for specified ES from global and ES table.
2249 * Also remove all other routes from the per ES table.
2250 * Invoked when ES is deleted.
2252 static int delete_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
2255 char buf
[ESI_STR_LEN
];
2256 struct prefix_evpn p
;
2258 /* Delete and withdraw locally learnt ES route */
2259 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
.ipaddr_v4
);
2260 ret
= delete_evpn_type4_route(bgp
, es
, &p
);
2262 flog_err(EC_BGP_EVPN_ROUTE_DELETE
,
2263 "%u failed to delete type-4 route for ESI %s",
2264 bgp
->vrf_id
, esi_to_str(&es
->esi
, buf
, sizeof(buf
)));
2267 /* Delete all routes from per ES table */
2268 return delete_all_es_routes(bgp
, es
);
2272 * Delete (and withdraw) local routes for specified VNI from the global
2273 * table and per-VNI table. After this, remove all other routes from
2274 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2275 * (advertise-all-vni) being disabled.
2277 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2280 struct prefix_evpn p
;
2282 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2283 * followed by type-3 routes (only one) - for this VNI.
2285 ret
= delete_all_type2_routes(bgp
, vpn
);
2289 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2290 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2294 /* Delete all routes from the per-VNI table. */
2295 return delete_all_vni_routes(bgp
, vpn
);
2299 * There is a tunnel endpoint IP address change for this VNI, delete
2300 * prior type-3 route (if needed) and update.
2301 * Note: Route re-advertisement happens elsewhere after other processing
2304 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2305 struct in_addr originator_ip
)
2307 struct prefix_evpn p
;
2309 /* If VNI is not live, we only need to update the originator ip */
2310 if (!is_vni_live(vpn
)) {
2311 vpn
->originator_ip
= originator_ip
;
2315 /* Update the tunnel-ip hash */
2316 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2317 bgp_tip_add(bgp
, &originator_ip
);
2319 /* filter routes as martian nexthop db has changed */
2320 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2322 /* Need to withdraw type-3 route as the originator IP is part
2325 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2326 delete_evpn_route(bgp
, vpn
, &p
);
2328 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2329 vpn
->originator_ip
= originator_ip
;
2333 /* Install EVPN route entry in ES */
2334 static int install_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2335 struct prefix_evpn
*p
,
2336 struct bgp_path_info
*parent_pi
)
2339 struct bgp_node
*rn
= NULL
;
2340 struct bgp_path_info
*pi
= NULL
;
2341 struct attr
*attr_new
= NULL
;
2343 /* Create (or fetch) route within the VNI.
2344 * NOTE: There is no RD here.
2346 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
2348 /* Check if route entry is already present. */
2349 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2351 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2355 /* Add (or update) attribute to hash. */
2356 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2358 /* Create new route with its attribute. */
2359 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2360 parent_pi
->peer
, attr_new
, rn
);
2361 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2362 bgp_path_info_extra_get(pi
);
2363 pi
->extra
->parent
= parent_pi
;
2364 bgp_path_info_add(rn
, pi
);
2366 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2367 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2368 bgp_unlock_node(rn
);
2371 /* The attribute has changed. */
2372 /* Add (or update) attribute to hash. */
2373 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2375 /* Restore route, if needed. */
2376 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2377 bgp_path_info_restore(rn
, pi
);
2379 /* Mark if nexthop has changed. */
2380 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2381 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2383 /* Unintern existing, set to new. */
2384 bgp_attr_unintern(&pi
->attr
);
2385 pi
->attr
= attr_new
;
2386 pi
->uptime
= bgp_clock();
2389 /* Perform route selection and update zebra, if required. */
2390 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2395 * Install route entry into the VRF routing table and invoke route selection.
2397 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2398 struct prefix_evpn
*evp
,
2399 struct bgp_path_info
*parent_pi
)
2401 struct bgp_node
*rn
;
2402 struct bgp_path_info
*pi
;
2404 struct attr
*attr_new
;
2407 struct prefix
*pp
= &p
;
2410 char buf
[PREFIX_STRLEN
];
2411 char buf1
[PREFIX_STRLEN
];
2413 memset(pp
, 0, sizeof(struct prefix
));
2414 ip_prefix_from_evpn_prefix(evp
, pp
);
2416 if (bgp_debug_zebra(NULL
)) {
2418 "installing evpn prefix %s as ip prefix %s in vrf %s",
2419 prefix2str(evp
, buf
, sizeof(buf
)),
2420 prefix2str(pp
, buf1
, sizeof(buf
)),
2421 vrf_id_to_name(bgp_vrf
->vrf_id
));
2424 /* Create (or fetch) route within the VRF. */
2425 /* NOTE: There is no RD here. */
2426 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2428 safi
= SAFI_UNICAST
;
2429 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2430 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2432 safi
= SAFI_UNICAST
;
2433 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2437 /* EVPN routes currently only support a IPv4 next hop which corresponds
2438 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2439 * or prefix route, we have to convert the next hop to an IPv4-mapped
2440 * address for the rest of the code to flow through. In the case of IPv4,
2441 * make sure to set the flag for next hop attribute.
2443 bgp_attr_dup(&attr
, parent_pi
->attr
);
2445 evpn_convert_nexthop_to_ipv6(&attr
);
2447 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2449 /* Check if route entry is already present. */
2450 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2452 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2456 /* Add (or update) attribute to hash. */
2457 attr_new
= bgp_attr_intern(&attr
);
2459 /* Create new route with its attribute. */
2460 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2461 parent_pi
->peer
, attr_new
, rn
);
2462 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2463 bgp_path_info_extra_get(pi
);
2464 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2465 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2466 if (parent_pi
->extra
) {
2467 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2468 sizeof(pi
->extra
->label
));
2469 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2471 bgp_path_info_add(rn
, pi
);
2473 if (attrhash_cmp(pi
->attr
, &attr
)
2474 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2475 bgp_unlock_node(rn
);
2478 /* The attribute has changed. */
2479 /* Add (or update) attribute to hash. */
2480 attr_new
= bgp_attr_intern(&attr
);
2482 /* Restore route, if needed. */
2483 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2484 bgp_path_info_restore(rn
, pi
);
2486 /* Mark if nexthop has changed. */
2488 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2490 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2491 &attr_new
->mp_nexthop_global
)))
2492 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2494 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_ATTR_CHANGED
);
2495 /* Unintern existing, set to new. */
2496 bgp_attr_unintern(&pi
->attr
);
2497 pi
->attr
= attr_new
;
2498 pi
->uptime
= bgp_clock();
2501 bgp_aggregate_increment(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2503 /* Perform route selection and update zebra, if required. */
2504 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2510 * Install route entry into the VNI routing table and invoke route selection.
2512 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2513 struct prefix_evpn
*p
,
2514 struct bgp_path_info
*parent_pi
)
2516 struct bgp_node
*rn
;
2517 struct bgp_path_info
*pi
;
2518 struct attr
*attr_new
;
2521 /* Create (or fetch) route within the VNI. */
2522 /* NOTE: There is no RD here. */
2523 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2525 /* Check if route entry is already present. */
2526 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2528 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2532 /* Add (or update) attribute to hash. */
2533 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2535 /* Create new route with its attribute. */
2536 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2537 parent_pi
->peer
, attr_new
, rn
);
2538 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2539 bgp_path_info_extra_get(pi
);
2540 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2541 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2542 if (parent_pi
->extra
) {
2543 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2544 sizeof(pi
->extra
->label
));
2545 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2547 bgp_path_info_add(rn
, pi
);
2549 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2550 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2551 bgp_unlock_node(rn
);
2554 /* The attribute has changed. */
2555 /* Add (or update) attribute to hash. */
2556 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2558 /* Restore route, if needed. */
2559 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2560 bgp_path_info_restore(rn
, pi
);
2562 /* Mark if nexthop has changed. */
2563 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2564 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2566 /* Unintern existing, set to new. */
2567 bgp_attr_unintern(&pi
->attr
);
2568 pi
->attr
= attr_new
;
2569 pi
->uptime
= bgp_clock();
2572 /* Perform route selection and update zebra, if required. */
2573 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2578 /* Uninstall EVPN route entry from ES route table */
2579 static int uninstall_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2580 struct prefix_evpn
*p
,
2581 struct bgp_path_info
*parent_pi
)
2584 struct bgp_node
*rn
;
2585 struct bgp_path_info
*pi
;
2587 if (!es
->route_table
)
2590 /* Locate route within the ESI.
2591 * NOTE: There is no RD here.
2593 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2597 /* Find matching route entry. */
2598 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2600 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2606 /* Mark entry for deletion */
2607 bgp_path_info_delete(rn
, pi
);
2609 /* Perform route selection and update zebra, if required. */
2610 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2612 /* Unlock route node. */
2613 bgp_unlock_node(rn
);
2619 * Uninstall route entry from the VRF routing table and send message
2620 * to zebra, if appropriate.
2622 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2623 struct prefix_evpn
*evp
,
2624 struct bgp_path_info
*parent_pi
)
2626 struct bgp_node
*rn
;
2627 struct bgp_path_info
*pi
;
2630 struct prefix
*pp
= &p
;
2633 char buf
[PREFIX_STRLEN
];
2634 char buf1
[PREFIX_STRLEN
];
2636 memset(pp
, 0, sizeof(struct prefix
));
2637 ip_prefix_from_evpn_prefix(evp
, pp
);
2639 if (bgp_debug_zebra(NULL
)) {
2641 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2642 prefix2str(evp
, buf
, sizeof(buf
)),
2643 prefix2str(pp
, buf1
, sizeof(buf
)),
2644 vrf_id_to_name(bgp_vrf
->vrf_id
));
2647 /* Locate route within the VRF. */
2648 /* NOTE: There is no RD here. */
2649 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2651 safi
= SAFI_UNICAST
;
2652 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2655 safi
= SAFI_UNICAST
;
2656 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2662 /* Find matching route entry. */
2663 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2665 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2671 bgp_aggregate_decrement(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2673 /* Mark entry for deletion */
2674 bgp_path_info_delete(rn
, pi
);
2676 /* Perform route selection and update zebra, if required. */
2677 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2679 /* Unlock route node. */
2680 bgp_unlock_node(rn
);
2686 * Uninstall route entry from the VNI routing table and send message
2687 * to zebra, if appropriate.
2689 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2690 struct prefix_evpn
*p
,
2691 struct bgp_path_info
*parent_pi
)
2693 struct bgp_node
*rn
;
2694 struct bgp_path_info
*pi
;
2697 /* Locate route within the VNI. */
2698 /* NOTE: There is no RD here. */
2699 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2703 /* Find matching route entry. */
2704 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
2706 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2712 /* Mark entry for deletion */
2713 bgp_path_info_delete(rn
, pi
);
2715 /* Perform route selection and update zebra, if required. */
2716 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2718 /* Unlock route node. */
2719 bgp_unlock_node(rn
);
2725 * Given a prefix, see if it belongs to ES.
2727 static int is_prefix_matching_for_es(struct prefix_evpn
*p
,
2730 /* if not an ES route return false */
2731 if (p
->prefix
.route_type
!= BGP_EVPN_ES_ROUTE
)
2734 if (memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
)) == 0)
2741 * Given a route entry and a VRF, see if this route entry should be
2742 * imported into the VRF i.e., RTs match.
2744 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2745 struct bgp_path_info
*pi
)
2747 struct attr
*attr
= pi
->attr
;
2748 struct ecommunity
*ecom
;
2752 /* Route should have valid RT to be even considered. */
2753 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2756 ecom
= attr
->ecommunity
;
2757 if (!ecom
|| !ecom
->size
)
2760 /* For each extended community RT, see if it matches this VNI. If any RT
2761 * matches, we're done.
2763 for (i
= 0; i
< ecom
->size
; i
++) {
2765 uint8_t type
, sub_type
;
2766 struct ecommunity_val
*eval
;
2767 struct ecommunity_val eval_tmp
;
2768 struct vrf_irt_node
*irt
;
2770 /* Only deal with RTs */
2771 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2772 eval
= (struct ecommunity_val
*)(ecom
->val
2773 + (i
* ECOMMUNITY_SIZE
));
2776 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2779 /* See if this RT matches specified VNIs import RTs */
2780 irt
= lookup_vrf_import_rt(eval
);
2782 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2785 /* Also check for non-exact match. In this, we mask out the AS
2787 * only check on the local-admin sub-field. This is to
2789 * VNI as the RT for EBGP peering too.
2792 if (type
== ECOMMUNITY_ENCODE_AS
2793 || type
== ECOMMUNITY_ENCODE_AS4
2794 || type
== ECOMMUNITY_ENCODE_IP
) {
2795 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2796 mask_ecom_global_admin(&eval_tmp
, eval
);
2797 irt
= lookup_vrf_import_rt(&eval_tmp
);
2800 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2808 * Given a route entry and a VNI, see if this route entry should be
2809 * imported into the VNI i.e., RTs match.
2811 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2812 struct bgp_path_info
*pi
)
2814 struct attr
*attr
= pi
->attr
;
2815 struct ecommunity
*ecom
;
2819 /* Route should have valid RT to be even considered. */
2820 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2823 ecom
= attr
->ecommunity
;
2824 if (!ecom
|| !ecom
->size
)
2827 /* For each extended community RT, see if it matches this VNI. If any RT
2828 * matches, we're done.
2830 for (i
= 0; i
< ecom
->size
; i
++) {
2832 uint8_t type
, sub_type
;
2833 struct ecommunity_val
*eval
;
2834 struct ecommunity_val eval_tmp
;
2835 struct irt_node
*irt
;
2837 /* Only deal with RTs */
2838 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2839 eval
= (struct ecommunity_val
*)(ecom
->val
2840 + (i
* ECOMMUNITY_SIZE
));
2843 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2846 /* See if this RT matches specified VNIs import RTs */
2847 irt
= lookup_import_rt(bgp
, eval
);
2849 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2852 /* Also check for non-exact match. In this, we mask out the AS
2854 * only check on the local-admin sub-field. This is to
2856 * VNI as the RT for EBGP peering too.
2859 if (type
== ECOMMUNITY_ENCODE_AS
2860 || type
== ECOMMUNITY_ENCODE_AS4
2861 || type
== ECOMMUNITY_ENCODE_IP
) {
2862 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2863 mask_ecom_global_admin(&eval_tmp
, eval
);
2864 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2867 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2874 static int install_uninstall_routes_for_es(struct bgp
*bgp
,
2881 char buf
[PREFIX_STRLEN
];
2882 char buf1
[ESI_STR_LEN
];
2883 struct bgp_node
*rd_rn
, *rn
;
2884 struct bgp_table
*table
;
2885 struct bgp_path_info
*pi
;
2891 * Walk entire global routing table and evaluate routes which could be
2892 * imported into this VRF. Note that we need to loop through all global
2893 * routes to determine which route matches the import rt on vrf
2895 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2896 rd_rn
= bgp_route_next(rd_rn
)) {
2897 table
= bgp_node_get_bgp_table_info(rd_rn
);
2901 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2902 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2904 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
2907 * Consider "valid" remote routes applicable for
2910 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
2911 && pi
->type
== ZEBRA_ROUTE_BGP
2912 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
2915 if (!is_prefix_matching_for_es(evp
, es
))
2919 ret
= install_evpn_route_entry_in_es(
2922 ret
= uninstall_evpn_route_entry_in_es(
2928 "Failed to %s EVPN %s route in ESI %s",
2931 prefix2str(evp
, buf
,
2933 esi_to_str(&es
->esi
, buf1
,
2944 * Install or uninstall mac-ip routes are appropriate for this
2947 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
2951 struct bgp_node
*rd_rn
, *rn
;
2952 struct bgp_table
*table
;
2953 struct bgp_path_info
*pi
;
2955 char buf
[PREFIX_STRLEN
];
2956 struct bgp
*bgp_evpn
= NULL
;
2960 bgp_evpn
= bgp_get_evpn();
2964 /* Walk entire global routing table and evaluate routes which could be
2965 * imported into this VRF. Note that we need to loop through all global
2966 * routes to determine which route matches the import rt on vrf
2968 for (rd_rn
= bgp_table_top(bgp_evpn
->rib
[afi
][safi
]); rd_rn
;
2969 rd_rn
= bgp_route_next(rd_rn
)) {
2970 table
= bgp_node_get_bgp_table_info(rd_rn
);
2974 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2975 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2977 /* if not mac-ip route skip this route */
2978 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2979 || evp
->prefix
.route_type
2980 == BGP_EVPN_IP_PREFIX_ROUTE
))
2983 /* if not a mac+ip route skip this route */
2984 if (!(is_evpn_prefix_ipaddr_v4(evp
)
2985 || is_evpn_prefix_ipaddr_v6(evp
)))
2988 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
2990 /* Consider "valid" remote routes applicable for
2993 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
2994 && pi
->type
== ZEBRA_ROUTE_BGP
2995 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
2998 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
3000 ret
= install_evpn_route_entry_in_vrf(
3003 ret
= uninstall_evpn_route_entry_in_vrf(
3009 "Failed to %s EVPN %s route in VRF %s",
3012 prefix2str(evp
, buf
,
3027 * Install or uninstall routes of specified type that are appropriate for this
3030 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
3031 struct bgpevpn
*vpn
,
3032 bgp_evpn_route_type rtype
,
3037 struct bgp_node
*rd_rn
, *rn
;
3038 struct bgp_table
*table
;
3039 struct bgp_path_info
*pi
;
3045 /* Walk entire global routing table and evaluate routes which could be
3046 * imported into this VPN. Note that we cannot just look at the routes
3048 * the VNI's RD - remote routes applicable for this VNI could have any
3051 /* EVPN routes are a 2-level table. */
3052 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
3053 rd_rn
= bgp_route_next(rd_rn
)) {
3054 table
= bgp_node_get_bgp_table_info(rd_rn
);
3058 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3059 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3061 if (evp
->prefix
.route_type
!= rtype
)
3064 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
3066 /* Consider "valid" remote routes applicable for
3068 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3069 && pi
->type
== ZEBRA_ROUTE_BGP
3070 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3073 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3075 ret
= install_evpn_route_entry(
3078 ret
= uninstall_evpn_route_entry(
3084 "%u: Failed to %s EVPN %s route in VNI %u",
3088 rtype
== BGP_EVPN_MAC_IP_ROUTE
3102 /* Install any existing remote ES routes applicable for this ES into its routing
3103 * table. This is invoked when ES comes up.
3105 static int install_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
3107 return install_uninstall_routes_for_es(bgp
, es
, 1);
3111 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3112 * is invoked upon l3vni-add or l3vni import rt change
3114 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3116 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3121 * Install any existing remote routes applicable for this VNI into its
3122 * routing table. This is invoked when a VNI becomes "live" or its Import
3125 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3129 /* Install type-3 routes followed by type-2 routes - the ones applicable
3132 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3137 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3141 /* uninstall routes from l3vni vrf. */
3142 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3144 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3149 * Uninstall any existing remote routes for this VNI. One scenario in which
3150 * this is invoked is upon an import RT change.
3152 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3156 /* Uninstall type-2 routes followed by type-3 routes - the ones
3160 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3165 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3169 /* Install or unistall route in ES */
3170 static int install_uninstall_route_in_es(struct bgp
*bgp
, struct evpnes
*es
,
3171 afi_t afi
, safi_t safi
,
3172 struct prefix_evpn
*evp
,
3173 struct bgp_path_info
*pi
, int install
)
3176 char buf
[ESI_STR_LEN
];
3179 ret
= install_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3181 ret
= uninstall_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3186 "%u: Failed to %s EVPN %s route in ESI %s", bgp
->vrf_id
,
3187 install
? "install" : "uninstall", "ES",
3188 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)));
3195 * Install or uninstall route in matching VRFs (list).
3197 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3198 safi_t safi
, struct prefix_evpn
*evp
,
3199 struct bgp_path_info
*pi
,
3200 struct list
*vrfs
, int install
)
3202 char buf
[PREFIX2STR_BUFFER
];
3203 struct bgp
*bgp_vrf
;
3204 struct listnode
*node
, *nnode
;
3206 /* Only type-2/type-5 routes go into a VRF */
3207 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3208 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3211 /* if it is type-2 route and not a mac+ip route skip this route */
3212 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3213 && !(is_evpn_prefix_ipaddr_v4(evp
)
3214 || is_evpn_prefix_ipaddr_v6(evp
)))
3217 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3221 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3223 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3227 flog_err(EC_BGP_EVPN_FAIL
,
3228 "%u: Failed to %s prefix %s in VRF %s",
3230 install
? "install" : "uninstall",
3231 prefix2str(evp
, buf
, sizeof(buf
)),
3232 vrf_id_to_name(bgp_vrf
->vrf_id
));
3241 * Install or uninstall route in matching VNIs (list).
3243 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3244 safi_t safi
, struct prefix_evpn
*evp
,
3245 struct bgp_path_info
*pi
,
3246 struct list
*vnis
, int install
)
3248 struct bgpevpn
*vpn
;
3249 struct listnode
*node
, *nnode
;
3251 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3254 if (!is_vni_live(vpn
))
3258 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3260 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3263 flog_err(EC_BGP_EVPN_FAIL
,
3264 "%u: Failed to %s EVPN %s route in VNI %u",
3265 bgp
->vrf_id
, install
? "install" : "uninstall",
3266 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3278 * Install or uninstall route for appropriate VNIs/ESIs.
3280 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3282 struct bgp_path_info
*pi
, int import
)
3284 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3285 struct attr
*attr
= pi
->attr
;
3286 struct ecommunity
*ecom
;
3291 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3292 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3293 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3294 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3295 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3298 /* If we don't have Route Target, nothing much to do. */
3299 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3302 ecom
= attr
->ecommunity
;
3303 if (!ecom
|| !ecom
->size
)
3306 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3307 * attached to the route */
3308 for (i
= 0; i
< ecom
->size
; i
++) {
3310 uint8_t type
, sub_type
;
3311 struct ecommunity_val
*eval
;
3312 struct ecommunity_val eval_tmp
;
3313 struct irt_node
*irt
; /* import rt for l2vni */
3314 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3317 /* Only deal with RTs */
3318 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3319 eval
= (struct ecommunity_val
*)(ecom
->val
3320 + (i
* ECOMMUNITY_SIZE
));
3323 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3327 * macip routes (type-2) are imported into VNI and VRF tables.
3328 * IMET route is imported into VNI table.
3329 * prefix routes are imported into VRF table.
3331 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3332 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3333 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3335 irt
= lookup_import_rt(bgp
, eval
);
3337 install_uninstall_route_in_vnis(
3338 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3341 vrf_irt
= lookup_vrf_import_rt(eval
);
3343 install_uninstall_route_in_vrfs(
3344 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3347 /* Also check for non-exact match.
3348 * In this, we mask out the AS and
3349 * only check on the local-admin sub-field.
3350 * This is to facilitate using
3351 * VNI as the RT for EBGP peering too.
3355 if (type
== ECOMMUNITY_ENCODE_AS
3356 || type
== ECOMMUNITY_ENCODE_AS4
3357 || type
== ECOMMUNITY_ENCODE_IP
) {
3358 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3359 mask_ecom_global_admin(&eval_tmp
, eval
);
3360 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3361 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3365 install_uninstall_route_in_vnis(
3366 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3369 install_uninstall_route_in_vrfs(
3370 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3374 /* es route is imported into the es table */
3375 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3377 /* we will match based on the entire esi to avoid
3378 * imoort of an es route for esi2 into esi1
3380 es
= bgp_evpn_lookup_es(bgp
, &evp
->prefix
.es_addr
.esi
);
3381 if (es
&& is_es_local(es
))
3382 install_uninstall_route_in_es(
3383 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3391 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3394 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3396 /* Delete ipv4 default route and withdraw from peers */
3397 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3398 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3399 SAFI_UNICAST
, false);
3401 /* delete all ipv4 routes and withdraw from peers */
3402 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3403 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3405 /* Delete ipv6 default route and withdraw from peers */
3406 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3407 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3408 SAFI_UNICAST
, false);
3410 /* delete all ipv6 routes and withdraw from peers */
3411 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3412 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3416 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3419 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3421 /* update all ipv4 routes */
3422 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3423 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3425 /* update ipv4 default route and withdraw from peers */
3426 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3427 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3428 SAFI_UNICAST
, true);
3430 /* update all ipv6 routes */
3431 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3432 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3434 /* update ipv6 default route and withdraw from peers */
3435 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3436 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3437 SAFI_UNICAST
, true);
3442 * update and advertise local routes for a VRF as type-5 routes.
3443 * This is invoked upon RD change for a VRF. Note taht the processing is only
3444 * done in the global route table using the routes which already exist in the
3447 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3449 /* skip if the RD is configured */
3450 if (is_vrf_rd_configured(bgp_vrf
))
3453 /* derive the RD for the VRF based on new router-id */
3454 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3456 /* update advertise ipv4|ipv6 routes as type-5 routes */
3457 update_advertise_vrf_routes(bgp_vrf
);
3461 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3462 * This is invoked upon VRF RD change. The processing is done only from global
3465 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3467 /* skip if the RD is configured */
3468 if (is_vrf_rd_configured(bgp_vrf
))
3471 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3472 delete_withdraw_vrf_routes(bgp_vrf
);
3476 * Update and advertise local routes for a VNI. Invoked upon router-id
3477 * change. Note that the processing is done only on the global route table
3478 * using routes that already exist in the per-VNI table.
3480 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3482 struct prefix_evpn p
;
3483 struct bgp_node
*rn
, *global_rn
;
3484 struct bgp_path_info
*pi
, *global_pi
;
3486 afi_t afi
= AFI_L2VPN
;
3487 safi_t safi
= SAFI_EVPN
;
3489 /* Locate type-3 route for VNI in the per-VNI table and use its
3490 * attributes to create and advertise the type-3 route for this VNI
3491 * in the global table.
3493 * RT-3 only if doing head-end replication
3495 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
3496 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3497 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3498 if (!rn
) /* unexpected */
3500 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3501 if (pi
->peer
== bgp
->peer_self
&&
3502 pi
->type
== ZEBRA_ROUTE_BGP
3503 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3505 if (!pi
) /* unexpected */
3509 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3510 (struct prefix
*)&p
, &vpn
->prd
);
3511 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
,
3512 1, &pi
, 0, mac_mobility_seqnum(attr
));
3514 /* Schedule for processing and unlock node. */
3515 bgp_process(bgp
, global_rn
, afi
, safi
);
3516 bgp_unlock_node(global_rn
);
3519 /* Now, walk this VNI's route table and use the route and its attribute
3520 * to create and schedule route in global table.
3522 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
3523 rn
= bgp_route_next(rn
)) {
3524 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3526 /* Identify MAC-IP local routes. */
3527 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3530 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3531 if (pi
->peer
== bgp
->peer_self
3532 && pi
->type
== ZEBRA_ROUTE_BGP
3533 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3538 /* Create route in global routing table using this route entry's
3542 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3543 (struct prefix
*)evp
, &vpn
->prd
);
3545 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
3547 mac_mobility_seqnum(attr
));
3549 /* Schedule for processing and unlock node. */
3550 bgp_process(bgp
, global_rn
, afi
, safi
);
3551 bgp_unlock_node(global_rn
);
3558 * Delete (and withdraw) local routes for a VNI - only from the global
3559 * table. Invoked upon router-id change.
3561 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3564 struct prefix_evpn p
;
3565 struct bgp_node
*global_rn
;
3566 struct bgp_path_info
*pi
;
3567 afi_t afi
= AFI_L2VPN
;
3568 safi_t safi
= SAFI_EVPN
;
3570 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3571 * for this VNI - from the global table.
3573 ret
= delete_global_type2_routes(bgp
, vpn
);
3577 /* Remove type-3 route for this VNI from global table. */
3578 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3579 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3580 (struct prefix
*)&p
, &vpn
->prd
);
3582 /* Delete route entry in the global EVPN table. */
3583 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
3585 /* Schedule for processing - withdraws to peers happen from
3589 bgp_process(bgp
, global_rn
, afi
, safi
);
3590 bgp_unlock_node(global_rn
);
3597 * Handle router-id change. Update and advertise local routes corresponding
3598 * to this VNI from peers. Note that this is invoked after updating the
3599 * router-id. The routes in the per-VNI table are used to create routes in
3600 * the global table and schedule them.
3602 static void update_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3604 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3606 /* Skip VNIs with configured RD. */
3607 if (is_rd_configured(vpn
))
3610 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3611 update_advertise_vni_routes(bgp
, vpn
);
3615 * Handle router-id change. Delete and withdraw local routes corresponding
3616 * to this VNI from peers. Note that this is invoked prior to updating
3617 * the router-id and is done only on the global route table, the routes
3618 * are needed in the per-VNI table to re-advertise with new router id.
3620 static void withdraw_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3622 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3624 /* Skip VNIs with configured RD. */
3625 if (is_rd_configured(vpn
))
3628 delete_withdraw_vni_routes(bgp
, vpn
);
3632 * Create RT-3 for a VNI and schedule for processing and advertisement.
3633 * This is invoked upon flooding mode changing to head-end replication.
3635 static void create_advertise_type3(struct hash_bucket
*bucket
, void *data
)
3637 struct bgpevpn
*vpn
= bucket
->data
;
3638 struct bgp
*bgp
= data
;
3639 struct prefix_evpn p
;
3641 if (!vpn
|| !is_vni_live(vpn
))
3644 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3645 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0))
3646 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3647 "Type3 route creation failure for VNI %u", vpn
->vni
);
3651 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3652 * This is invoked upon flooding mode changing to drop BUM packets.
3654 static void delete_withdraw_type3(struct hash_bucket
*bucket
, void *data
)
3656 struct bgpevpn
*vpn
= bucket
->data
;
3657 struct bgp
*bgp
= data
;
3658 struct prefix_evpn p
;
3660 if (!vpn
|| !is_vni_live(vpn
))
3663 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3664 delete_evpn_route(bgp
, vpn
, &p
);
3668 * Process received EVPN type-2 route (advertise or withdraw).
3670 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3671 struct attr
*attr
, uint8_t *pfx
, int psize
,
3672 uint32_t addpath_id
)
3674 struct prefix_rd prd
;
3675 struct prefix_evpn p
;
3676 struct bgp_route_evpn evpn
;
3678 uint8_t macaddr_len
;
3679 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
3680 uint32_t num_labels
= 0;
3684 /* Type-2 route should be either 33, 37 or 49 bytes or an
3685 * additional 3 bytes if there is a second label (VNI):
3686 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3687 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3688 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3690 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3691 && psize
!= 40 && psize
!= 52) {
3692 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3693 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3694 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3698 memset(&evpn
, 0, sizeof(evpn
));
3700 /* Make prefix_rd */
3701 prd
.family
= AF_UNSPEC
;
3703 memcpy(&prd
.val
, pfx
, 8);
3706 /* Make EVPN prefix. */
3707 memset(&p
, 0, sizeof(struct prefix_evpn
));
3709 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3710 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3712 /* Copy Ethernet Seg Identifier */
3713 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
3716 /* Copy Ethernet Tag */
3717 memcpy(ð_tag
, pfx
, 4);
3718 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3721 /* Get the MAC Addr len */
3722 macaddr_len
= *pfx
++;
3724 /* Get the MAC Addr */
3725 if (macaddr_len
== (ETH_ALEN
* 8)) {
3726 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
3730 EC_BGP_EVPN_ROUTE_INVALID
,
3731 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3732 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3738 ipaddr_len
= *pfx
++;
3739 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3740 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3742 EC_BGP_EVPN_ROUTE_INVALID
,
3743 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3744 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3749 ipaddr_len
/= 8; /* Convert to bytes. */
3750 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3753 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
3757 /* Get the VNI(s). Stored as bytes here. */
3759 memset(label
, 0, sizeof(label
));
3760 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
3761 pfx
+= BGP_LABEL_BYTES
;
3762 psize
-= (33 + ipaddr_len
);
3763 /* Do we have a second VNI? */
3766 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
3768 * If in future, we are required to access additional fields,
3769 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
3774 /* Process the route. */
3776 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3777 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3778 &prd
, &label
[0], num_labels
, 0, &evpn
);
3780 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3781 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3782 &prd
, &label
[0], num_labels
, &evpn
);
3787 * Process received EVPN type-3 route (advertise or withdraw).
3789 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3790 struct attr
*attr
, uint8_t *pfx
, int psize
,
3791 uint32_t addpath_id
)
3793 struct prefix_rd prd
;
3794 struct prefix_evpn p
;
3799 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3800 * IP len (1) and IP (4 or 16).
3802 if (psize
!= 17 && psize
!= 29) {
3803 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3804 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3805 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3809 /* If PMSI is present, log if it is anything other than IR.
3810 * Note: We just simply ignore the values as it is not clear if
3811 * doing anything else is better.
3814 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3815 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
) {
3817 EC_BGP_EVPN_PMSI_PRESENT
,
3818 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3819 peer
->bgp
->vrf_id
, peer
->host
,
3820 attr
->pmsi_tnl_type
);
3824 /* Make prefix_rd */
3825 prd
.family
= AF_UNSPEC
;
3827 memcpy(&prd
.val
, pfx
, 8);
3830 /* Make EVPN prefix. */
3831 memset(&p
, 0, sizeof(struct prefix_evpn
));
3833 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3834 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3836 /* Copy Ethernet Tag */
3837 memcpy(ð_tag
, pfx
, 4);
3838 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
3842 ipaddr_len
= *pfx
++;
3843 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3844 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
3845 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
3848 EC_BGP_EVPN_ROUTE_INVALID
,
3849 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3850 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3854 /* Process the route. */
3856 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3857 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3858 &prd
, NULL
, 0, 0, NULL
);
3860 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3861 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3862 &prd
, NULL
, 0, NULL
);
3867 * Process received EVPN type-4 route (advertise or withdraw).
3869 static int process_type4_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3870 struct attr
*attr
, uint8_t *pfx
, int psize
,
3871 uint32_t addpath_id
)
3876 struct in_addr vtep_ip
;
3877 struct prefix_rd prd
;
3878 struct prefix_evpn p
;
3880 /* Type-4 route should be either 23 or 35 bytes
3881 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
3883 if (psize
!= 23 && psize
!= 35) {
3884 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3885 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
3886 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3890 /* Make prefix_rd */
3891 prd
.family
= AF_UNSPEC
;
3893 memcpy(&prd
.val
, pfx
, 8);
3897 memcpy(&esi
, pfx
, ESI_BYTES
);
3902 ipaddr_len
= *pfx
++;
3903 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3904 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
3907 EC_BGP_EVPN_ROUTE_INVALID
,
3908 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
3909 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3913 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
3914 /* Process the route. */
3916 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3917 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3918 &prd
, NULL
, 0, 0, NULL
);
3920 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3921 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3922 &prd
, NULL
, 0, NULL
);
3929 * Process received EVPN type-5 route (advertise or withdraw).
3931 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3932 struct attr
*attr
, uint8_t *pfx
, int psize
,
3933 uint32_t addpath_id
)
3935 struct prefix_rd prd
;
3936 struct prefix_evpn p
;
3937 struct bgp_route_evpn evpn
;
3940 mpls_label_t label
; /* holds the VNI as in the packet */
3943 /* Type-5 route should be 34 or 58 bytes:
3944 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3945 * GW (4 or 16) and VNI (3).
3946 * Note that the IP and GW should both be IPv4 or both IPv6.
3948 if (psize
!= 34 && psize
!= 58) {
3949 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3950 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3951 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3955 /* Make prefix_rd */
3956 prd
.family
= AF_UNSPEC
;
3958 memcpy(&prd
.val
, pfx
, 8);
3961 /* Make EVPN prefix. */
3962 memset(&p
, 0, sizeof(struct prefix_evpn
));
3964 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3965 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
3967 /* Additional information outside of prefix - ESI and GW IP */
3968 memset(&evpn
, 0, sizeof(evpn
));
3971 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
3974 /* Fetch Ethernet Tag. */
3975 memcpy(ð_tag
, pfx
, 4);
3976 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
3979 /* Fetch IP prefix length. */
3981 if (ippfx_len
> IPV6_MAX_BITLEN
) {
3983 EC_BGP_EVPN_ROUTE_INVALID
,
3984 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3985 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
3988 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
3990 /* Determine IPv4 or IPv6 prefix */
3991 /* Since the address and GW are from the same family, this just becomes
3992 * a simple check on the total size.
3995 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
3996 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
3998 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
4001 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
4002 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
4004 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
4008 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4009 memset(&label
, 0, sizeof(label
));
4010 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
4013 * If in future, we are required to access additional fields,
4014 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4018 /* Process the route. */
4020 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4021 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4022 &prd
, &label
, 1, 0, &evpn
);
4024 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4025 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4026 &prd
, &label
, 1, &evpn
);
4031 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
4032 struct prefix_rd
*prd
, mpls_label_t
*label
,
4033 uint32_t num_labels
, struct attr
*attr
)
4037 struct evpn_addr
*p_evpn_p
;
4039 memset(&temp
, 0, 16);
4040 if (p
->family
!= AF_EVPN
)
4042 p_evpn_p
= &(p
->u
.prefix_evpn
);
4044 /* len denites the total len of IP and GW-IP in the route
4045 IP and GW-IP have to be both ipv4 or ipv6
4047 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4048 len
= 8; /* IP and GWIP are both ipv4 */
4050 len
= 32; /* IP and GWIP are both ipv6 */
4051 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4052 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4053 stream_put(s
, prd
->val
, 8);
4055 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
4057 stream_put(s
, &temp
, 10);
4058 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4059 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4060 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4061 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4063 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4065 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4067 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
4069 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
4071 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4072 stream_put_ipv4(s
, 0);
4074 stream_put(s
, &temp
, 16);
4078 stream_put(s
, label
, 3);
4084 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4086 static void cleanup_vni_on_disable(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4088 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4090 /* Remove EVPN routes and schedule for processing. */
4091 delete_routes_for_vni(bgp
, vpn
);
4093 /* Clear "live" flag and see if hash needs to be freed. */
4094 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4095 if (!is_vni_configured(vpn
))
4096 bgp_evpn_free(bgp
, vpn
);
4100 * Free a VNI entry; iterator function called during cleanup.
4102 static void free_vni_entry(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4104 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4106 delete_all_vni_routes(bgp
, vpn
);
4107 bgp_evpn_free(bgp
, vpn
);
4111 * Derive AUTO import RT for BGP VRF - L3VNI
4113 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4115 struct bgp
*bgp_evpn
= NULL
;
4117 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4118 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4121 bgp_evpn
= bgp_get_evpn();
4124 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4128 * Delete AUTO import RT from BGP VRF - L3VNI
4130 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4132 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4136 * Derive AUTO export RT for BGP VRF - L3VNI
4138 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4140 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4141 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4145 * Delete AUTO export RT from BGP VRF - L3VNI
4147 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4149 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4152 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4154 struct bgp
*bgp_evpn
= NULL
;
4155 struct listnode
*node
= NULL
;
4156 struct bgpevpn
*vpn
= NULL
;
4158 bgp_evpn
= bgp_get_evpn();
4162 /* update all type-5 routes */
4163 update_advertise_vrf_routes(bgp_vrf
);
4165 /* update all type-2 routes */
4166 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4167 update_routes_for_vni(bgp_evpn
, vpn
);
4171 * Handle autort change for a given VNI.
4173 static void update_autort_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4175 struct bgpevpn
*vpn
= bucket
->data
;
4177 if (!is_import_rt_configured(vpn
)) {
4178 if (is_vni_live(vpn
))
4179 bgp_evpn_uninstall_routes(bgp
, vpn
);
4180 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4181 list_delete_all_node(vpn
->import_rtl
);
4182 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4183 if (is_vni_live(vpn
))
4184 bgp_evpn_install_routes(bgp
, vpn
);
4186 if (!is_export_rt_configured(vpn
)) {
4187 list_delete_all_node(vpn
->export_rtl
);
4188 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4189 if (is_vni_live(vpn
))
4190 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4198 /* withdraw type-5 route corresponding to ip prefix */
4199 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4200 afi_t afi
, safi_t safi
)
4203 struct prefix_evpn evp
;
4204 char buf
[PREFIX_STRLEN
];
4206 build_type5_prefix_from_ip_prefix(&evp
, p
);
4207 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4210 EC_BGP_EVPN_ROUTE_DELETE
,
4211 "%u failed to delete type-5 route for prefix %s in vrf %s",
4212 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4213 vrf_id_to_name(bgp_vrf
->vrf_id
));
4217 /* withdraw all type-5 routes for an address family */
4218 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4220 struct bgp_table
*table
= NULL
;
4221 struct bgp_node
*rn
= NULL
;
4222 struct bgp_path_info
*pi
;
4224 table
= bgp_vrf
->rib
[afi
][safi
];
4225 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4226 /* Only care about "selected" routes - non-imported. */
4227 /* TODO: Support for AddPath for EVPN. */
4228 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4229 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4230 && (!pi
->extra
|| !pi
->extra
->parent
)) {
4231 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
4240 * evpn - enable advertisement of default g/w
4242 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
4243 safi_t safi
, bool add
)
4245 struct prefix ip_prefix
;
4247 /* form the default prefix 0.0.0.0/0 */
4248 memset(&ip_prefix
, 0, sizeof(struct prefix
));
4249 ip_prefix
.family
= afi2family(afi
);
4252 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
4255 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
4262 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4263 * to this function correspond to those of the source IP prefix (best
4264 * path in the case of the attr. In the case of a local prefix (when we
4265 * are advertising local subnets), the src_attr will be NULL.
4267 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4268 struct attr
*src_attr
, afi_t afi
,
4272 struct prefix_evpn evp
;
4273 char buf
[PREFIX_STRLEN
];
4275 build_type5_prefix_from_ip_prefix(&evp
, p
);
4276 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4278 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4279 "%u: Failed to create type-5 route for prefix %s",
4280 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4283 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4284 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4285 * advertisement is enabled.
4287 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4290 struct bgp_table
*table
= NULL
;
4291 struct bgp_node
*rn
= NULL
;
4292 struct bgp_path_info
*pi
;
4294 table
= bgp_vrf
->rib
[afi
][safi
];
4295 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4296 /* Need to identify the "selected" route entry to use its
4297 * attribute. Also, we only consider "non-imported" routes.
4298 * TODO: Support for AddPath for EVPN.
4300 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
4301 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4302 && (!pi
->extra
|| !pi
->extra
->parent
)) {
4304 /* apply the route-map */
4305 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4308 ret
= route_map_apply(
4309 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4311 &rn
->p
, RMAP_BGP
, pi
);
4312 if (ret
== RMAP_DENYMATCH
)
4315 bgp_evpn_advertise_type5_route(
4316 bgp_vrf
, &rn
->p
, pi
->attr
, afi
, safi
);
4323 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4325 struct listnode
*node
, *nnode
, *node_to_del
;
4326 struct ecommunity
*ecom
, *ecom_auto
;
4327 struct ecommunity_val eval
;
4329 if (bgp
->advertise_autort_rfc8365
)
4330 vni
|= EVPN_AUTORT_VXLAN
;
4331 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4333 ecom_auto
= ecommunity_new();
4334 ecommunity_add_val(ecom_auto
, &eval
);
4337 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4338 if (ecommunity_match(ecom
, ecom_auto
)) {
4339 ecommunity_free(&ecom
);
4345 list_delete_node(rtl
, node_to_del
);
4347 ecommunity_free(&ecom_auto
);
4350 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4351 struct ecommunity
*ecomadd
)
4353 /* uninstall routes from vrf */
4354 uninstall_routes_for_vrf(bgp_vrf
);
4356 /* Cleanup the RT to VRF mapping */
4357 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4359 /* Remove auto generated RT */
4360 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4362 /* Add the newly configured RT to RT list */
4363 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4364 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4366 /* map VRF to its RTs */
4367 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4369 /* install routes matching the new VRF */
4370 install_routes_for_vrf(bgp_vrf
);
4373 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4374 struct ecommunity
*ecomdel
)
4376 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4377 struct ecommunity
*ecom
= NULL
;
4379 /* uninstall routes from vrf */
4380 uninstall_routes_for_vrf(bgp_vrf
);
4382 /* Cleanup the RT to VRF mapping */
4383 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4385 /* remove the RT from the RT list */
4386 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4387 if (ecommunity_match(ecom
, ecomdel
)) {
4388 ecommunity_free(&ecom
);
4395 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4397 assert(bgp_vrf
->vrf_import_rtl
);
4398 /* fallback to auto import rt, if this was the last RT */
4399 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4400 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4401 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4404 /* map VRFs to its RTs */
4405 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4407 /* install routes matching this new RT */
4408 install_routes_for_vrf(bgp_vrf
);
4411 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4412 struct ecommunity
*ecomadd
)
4414 /* remove auto-generated RT */
4415 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4417 /* Add the new RT to the RT list */
4418 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4419 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4421 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4424 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4425 struct ecommunity
*ecomdel
)
4427 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4428 struct ecommunity
*ecom
= NULL
;
4430 /* Remove the RT from the RT list */
4431 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4432 if (ecommunity_match(ecom
, ecomdel
)) {
4433 ecommunity_free(&ecom
);
4440 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4443 * Temporary assert to make SA happy.
4444 * The ALL_LIST_ELEMENTS macro above has a NULL check
4445 * which means that SA is going to complain about
4446 * the list_isempty call, which doesn't NULL check.
4447 * So until we get this situation cleaned up, here
4450 assert(bgp_vrf
->vrf_export_rtl
);
4452 /* fall back to auto-generated RT if this was the last RT */
4453 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4454 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4455 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4458 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4462 * Handle change to BGP router id. This is invoked twice by the change
4463 * handler, first before the router id has been changed and then after
4464 * the router id has been changed. The first invocation will result in
4465 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4466 * will result in the routes being re-advertised.
4468 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4472 /* delete and withdraw all the type-5 routes
4473 stored in the global table for this vrf
4475 withdraw_router_id_vrf(bgp
);
4477 /* delete all the VNI routes (type-2/type-3) routes for all the
4480 hash_iterate(bgp
->vnihash
,
4481 (void (*)(struct hash_bucket
*,
4482 void *))withdraw_router_id_vni
,
4486 /* advertise all routes in the vrf as type-5 routes with the new
4489 update_router_id_vrf(bgp
);
4491 /* advertise all the VNI routes (type-2/type-3) routes with the
4494 hash_iterate(bgp
->vnihash
,
4495 (void (*)(struct hash_bucket
*,
4496 void *))update_router_id_vni
,
4502 * Handle change to auto-RT algorithm - update and advertise local routes.
4504 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4506 hash_iterate(bgp
->vnihash
,
4507 (void (*)(struct hash_bucket
*,
4508 void*))update_autort_vni
,
4513 * Handle change to export RT - update and advertise local routes.
4515 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4517 return update_routes_for_vni(bgp
, vpn
);
4520 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4523 delete_withdraw_vrf_routes(bgp_vrf
);
4525 update_advertise_vrf_routes(bgp_vrf
);
4529 * Handle change to RD. This is invoked twice by the change handler,
4530 * first before the RD has been changed and then after the RD has
4531 * been changed. The first invocation will result in local routes
4532 * of this VNI being deleted and withdrawn and the next will result
4533 * in the routes being re-advertised.
4535 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4539 delete_withdraw_vni_routes(bgp
, vpn
);
4541 update_advertise_vni_routes(bgp
, vpn
);
4545 * Install routes for this VNI. Invoked upon change to Import RT.
4547 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4549 return install_routes_for_vni(bgp
, vpn
);
4553 * Uninstall all routes installed for this VNI. Invoked upon change
4556 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4558 return uninstall_routes_for_vni(bgp
, vpn
);
4562 * TODO: Hardcoded for a maximum of 2 VNIs right now
4564 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4569 vni1
= label2vni(label
);
4570 if (num_labels
== 2) {
4571 vni2
= label2vni(label
+ 1);
4572 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4574 snprintf(buf
, len
, "%u", vni1
);
4579 * Function to convert evpn route to json format.
4580 * NOTE: We don't use prefix2str as the output here is a bit different.
4582 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
4584 char buf1
[ETHER_ADDR_STRLEN
];
4585 char buf2
[PREFIX2STR_BUFFER
];
4590 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4591 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4592 json_object_int_add(json
, "ethTag",
4593 p
->prefix
.imet_addr
.eth_tag
);
4594 json_object_int_add(json
, "ipLen",
4595 is_evpn_prefix_ipaddr_v4(p
)
4598 json_object_string_add(json
, "ip",
4599 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4600 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4601 if (is_evpn_prefix_ipaddr_none(p
)) {
4602 json_object_int_add(json
, "routeType",
4603 p
->prefix
.route_type
);
4604 json_object_int_add(json
, "ethTag",
4605 p
->prefix
.macip_addr
.eth_tag
);
4606 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4607 json_object_string_add(json
, "mac",
4608 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4614 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4617 json_object_int_add(json
, "routeType",
4618 p
->prefix
.route_type
);
4619 json_object_int_add(json
, "ethTag",
4620 p
->prefix
.macip_addr
.eth_tag
);
4621 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4622 json_object_string_add(json
, "mac",
4623 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4626 json_object_int_add(json
, "ipLen",
4627 is_evpn_prefix_ipaddr_v4(p
)
4630 json_object_string_add(
4633 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4635 PREFIX2STR_BUFFER
));
4638 /* Currently, this is to cater to other AF_ETHERNET code. */
4643 * Function to convert evpn route to string.
4644 * NOTE: We don't use prefix2str as the output here is a bit different.
4646 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
4648 char buf1
[ETHER_ADDR_STRLEN
];
4649 char buf2
[PREFIX2STR_BUFFER
];
4650 char buf3
[ESI_STR_LEN
];
4652 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4653 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
4654 p
->prefix
.imet_addr
.eth_tag
,
4655 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4657 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4658 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4659 if (is_evpn_prefix_ipaddr_none(p
))
4660 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4661 p
->prefix
.route_type
,
4662 p
->prefix
.macip_addr
.eth_tag
,
4664 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4669 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4671 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
4672 p
->prefix
.route_type
,
4673 p
->prefix
.macip_addr
.eth_tag
,
4675 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4677 family
== AF_INET
? IPV4_MAX_BITLEN
4680 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4682 PREFIX2STR_BUFFER
));
4684 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
4685 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4686 p
->prefix
.route_type
,
4687 p
->prefix
.prefix_addr
.eth_tag
,
4688 p
->prefix
.prefix_addr
.ip_prefix_length
,
4689 is_evpn_prefix_ipaddr_v4(p
)
4690 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
4691 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
4692 } else if (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
4693 snprintf(buf
, len
, "[%d]:[%s]:[%d]:[%s]",
4694 p
->prefix
.route_type
,
4695 esi_to_str(&p
->prefix
.es_addr
.esi
, buf3
, sizeof(buf3
)),
4696 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4698 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
));
4700 /* For EVPN route types not supported yet. */
4701 snprintf(buf
, len
, "(unsupported route type %d)",
4702 p
->prefix
.route_type
);
4709 * Encode EVPN prefix in Update (MP_REACH)
4711 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
4712 struct prefix_rd
*prd
, mpls_label_t
*label
,
4713 uint32_t num_labels
, struct attr
*attr
,
4714 int addpath_encode
, uint32_t addpath_tx_id
)
4716 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4717 int len
, ipa_len
= 0;
4720 stream_putl(s
, addpath_tx_id
);
4723 stream_putc(s
, evp
->prefix
.route_type
);
4725 switch (evp
->prefix
.route_type
) {
4726 case BGP_EVPN_MAC_IP_ROUTE
:
4727 if (is_evpn_prefix_ipaddr_v4(evp
))
4728 ipa_len
= IPV4_MAX_BYTELEN
;
4729 else if (is_evpn_prefix_ipaddr_v6(evp
))
4730 ipa_len
= IPV6_MAX_BYTELEN
;
4731 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4732 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4733 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4735 stream_putc(s
, len
);
4736 stream_put(s
, prd
->val
, 8); /* RD */
4738 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
4740 stream_put(s
, 0, 10);
4741 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4742 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4743 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4744 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4745 if (ipa_len
) /* IP */
4746 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4748 /* 1st label is the L2 VNI */
4749 stream_put(s
, label
, BGP_LABEL_BYTES
);
4750 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4751 if (ipa_len
&& num_labels
> 1)
4752 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4755 case BGP_EVPN_IMET_ROUTE
:
4756 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4757 stream_put(s
, prd
->val
, 8); /* RD */
4758 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4759 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4760 /* Originating Router's IP Addr */
4761 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4764 case BGP_EVPN_ES_ROUTE
:
4765 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4766 stream_put(s
, prd
->val
, 8); /* RD */
4767 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4768 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4770 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4773 case BGP_EVPN_IP_PREFIX_ROUTE
:
4774 /* TODO: AddPath support. */
4775 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4783 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4784 struct bgp_nlri
*packet
, int withdraw
)
4790 uint32_t addpath_id
;
4791 int addpath_encoded
;
4796 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4798 lim
= pnt
+ packet
->length
;
4800 safi
= packet
->safi
;
4804 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
4805 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
4806 PEER_CAP_ADDPATH_AF_TX_RCV
));
4808 for (; pnt
< lim
; pnt
+= psize
) {
4809 /* Clear prefix structure. */
4810 memset(&p
, 0, sizeof(struct prefix
));
4812 /* Deal with path-id if AddPath is supported. */
4813 if (addpath_encoded
) {
4814 /* When packet overflow occurs return immediately. */
4815 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4818 addpath_id
= ntohl(*((uint32_t *)pnt
));
4819 pnt
+= BGP_ADDPATH_ID_LEN
;
4822 /* All EVPN NLRI types start with type and length. */
4829 /* When packet overflow occur return immediately. */
4830 if (pnt
+ psize
> lim
)
4834 case BGP_EVPN_MAC_IP_ROUTE
:
4835 if (process_type2_route(peer
, afi
, safi
,
4836 withdraw
? NULL
: attr
, pnt
,
4837 psize
, addpath_id
)) {
4840 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4841 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4846 case BGP_EVPN_IMET_ROUTE
:
4847 if (process_type3_route(peer
, afi
, safi
,
4848 withdraw
? NULL
: attr
, pnt
,
4849 psize
, addpath_id
)) {
4852 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4853 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4858 case BGP_EVPN_ES_ROUTE
:
4859 if (process_type4_route(peer
, afi
, safi
,
4860 withdraw
? NULL
: attr
, pnt
,
4861 psize
, addpath_id
)) {
4864 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4865 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4870 case BGP_EVPN_IP_PREFIX_ROUTE
:
4871 if (process_type5_route(peer
, afi
, safi
,
4872 withdraw
? NULL
: attr
, pnt
,
4873 psize
, addpath_id
)) {
4876 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4877 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4887 /* Packet length consistency check. */
4895 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
4896 * The mapping will be used during route processing.
4897 * bgp_def: default bgp instance
4898 * bgp_vrf: specific bgp vrf instance on which RT is configured
4900 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
4903 struct ecommunity_val
*eval
= NULL
;
4904 struct listnode
*node
= NULL
, *nnode
= NULL
;
4905 struct ecommunity
*ecom
= NULL
;
4907 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4908 for (i
= 0; i
< ecom
->size
; i
++) {
4909 eval
= (struct ecommunity_val
*)(ecom
->val
4911 * ECOMMUNITY_SIZE
));
4912 map_vrf_to_rt(bgp_vrf
, eval
);
4918 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
4920 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
4923 struct ecommunity_val
*eval
;
4924 struct listnode
*node
, *nnode
;
4925 struct ecommunity
*ecom
;
4927 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4928 for (i
= 0; i
< ecom
->size
; i
++) {
4929 struct vrf_irt_node
*irt
;
4930 struct ecommunity_val eval_tmp
;
4932 eval
= (struct ecommunity_val
*)(ecom
->val
4934 * ECOMMUNITY_SIZE
));
4935 /* If using "automatic" RT, we only care about the
4936 * local-admin sub-field.
4937 * This is to facilitate using VNI as the RT for EBGP
4940 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4941 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
4942 BGP_VRF_IMPORT_RT_CFGD
))
4943 mask_ecom_global_admin(&eval_tmp
, eval
);
4945 irt
= lookup_vrf_import_rt(&eval_tmp
);
4947 unmap_vrf_from_rt(bgp_vrf
, irt
);
4954 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
4955 * The mapping will be used during route processing.
4957 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4960 struct ecommunity_val
*eval
;
4961 struct listnode
*node
, *nnode
;
4962 struct ecommunity
*ecom
;
4964 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4965 for (i
= 0; i
< ecom
->size
; i
++) {
4966 eval
= (struct ecommunity_val
*)(ecom
->val
4968 * ECOMMUNITY_SIZE
));
4969 map_vni_to_rt(bgp
, vpn
, eval
);
4975 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
4977 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4980 struct ecommunity_val
*eval
;
4981 struct listnode
*node
, *nnode
;
4982 struct ecommunity
*ecom
;
4984 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4985 for (i
= 0; i
< ecom
->size
; i
++) {
4986 struct irt_node
*irt
;
4987 struct ecommunity_val eval_tmp
;
4989 eval
= (struct ecommunity_val
*)(ecom
->val
4991 * ECOMMUNITY_SIZE
));
4992 /* If using "automatic" RT, we only care about the
4993 * local-admin sub-field.
4994 * This is to facilitate using VNI as the RT for EBGP
4997 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4998 if (!is_import_rt_configured(vpn
))
4999 mask_ecom_global_admin(&eval_tmp
, eval
);
5001 irt
= lookup_import_rt(bgp
, &eval_tmp
);
5003 unmap_vni_from_rt(bgp
, vpn
, irt
);
5009 * Derive Import RT automatically for VNI and map VNI to RT.
5010 * The mapping will be used during route processing.
5012 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5014 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
5015 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
5018 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
5022 * Derive Export RT automatically for VNI.
5024 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5026 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
5027 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5031 * Derive RD automatically for VNI using passed information - it
5032 * is of the form RouterId:unique-id-for-vni.
5034 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5036 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5040 * Derive RD automatically for VNI using passed information - it
5041 * is of the form RouterId:unique-id-for-vni.
5043 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5047 vpn
->prd
.family
= AF_UNSPEC
;
5048 vpn
->prd
.prefixlen
= 64;
5049 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
5050 (void)str2prefix_rd(buf
, &vpn
->prd
);
5051 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5057 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5059 struct list
*inst
= bm
->bgp
;
5060 struct listnode
*node
;
5061 struct bgp
*bgp_vrf
;
5063 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5064 if (bgp_vrf
->l3vni
== vni
)
5074 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5076 struct bgpevpn
*vpn
;
5079 memset(&tmp
, 0, sizeof(struct bgpevpn
));
5081 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5086 * Create a new vpn - invoked upon configuration or zebra notification.
5088 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5089 struct in_addr originator_ip
,
5090 vrf_id_t tenant_vrf_id
)
5092 struct bgpevpn
*vpn
;
5097 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5099 /* Set values - RD and RT set to defaults. */
5101 vpn
->originator_ip
= originator_ip
;
5102 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5104 /* Initialize route-target import and export lists */
5105 vpn
->import_rtl
= list_new();
5106 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5107 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
5108 vpn
->export_rtl
= list_new();
5109 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5110 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
5111 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5112 derive_rd_rt_for_vni(bgp
, vpn
);
5114 /* Initialize EVPN route table. */
5115 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5118 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
5119 XFREE(MTYPE_BGP_EVPN
, vpn
);
5123 /* add to l2vni list on corresponding vrf */
5124 bgpevpn_link_to_l3vni(vpn
);
5126 QOBJ_REG(vpn
, bgpevpn
);
5131 * Free a given VPN - called in multiple scenarios such as zebra
5132 * notification, configuration being deleted, advertise-all-vni disabled etc.
5133 * This just frees appropriate memory, caller should have taken other
5136 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5138 bgpevpn_unlink_from_l3vni(vpn
);
5139 bgp_table_unlock(vpn
->route_table
);
5140 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5141 list_delete(&vpn
->import_rtl
);
5142 list_delete(&vpn
->export_rtl
);
5143 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5144 hash_release(bgp
->vnihash
, vpn
);
5146 XFREE(MTYPE_BGP_EVPN
, vpn
);
5152 struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
)
5157 memset(&tmp
, 0, sizeof(struct evpnes
));
5158 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
5159 es
= hash_lookup(bgp
->esihash
, &tmp
);
5164 * Create a new local es - invoked upon zebra notification.
5166 struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
,
5168 struct ipaddr
*originator_ip
)
5176 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct evpnes
));
5178 /* set the ESI and originator_ip */
5179 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
5180 memcpy(&es
->originator_ip
, originator_ip
, sizeof(struct ipaddr
));
5182 /* Initialise the VTEP list */
5183 es
->vtep_list
= list_new();
5184 es
->vtep_list
->cmp
= evpn_vtep_ip_cmp
;
5186 /* auto derive RD for this es */
5187 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
5188 es
->prd
.family
= AF_UNSPEC
;
5189 es
->prd
.prefixlen
= 64;
5190 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), es
->rd_id
);
5191 (void)str2prefix_rd(buf
, &es
->prd
);
5193 /* Initialize the ES route table */
5194 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5197 if (!hash_get(bgp
->esihash
, es
, hash_alloc_intern
)) {
5198 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5202 QOBJ_REG(es
, evpnes
);
5208 * This just frees appropriate memory, caller should have taken other
5211 void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
)
5213 list_delete(&es
->vtep_list
);
5214 bgp_table_unlock(es
->route_table
);
5215 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
5216 hash_release(bgp
->esihash
, es
);
5218 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5222 * Import evpn route from global table to VNI/VRF/ESI.
5224 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5225 struct prefix
*p
, struct bgp_path_info
*pi
)
5227 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5231 * Unimport evpn route from VNI/VRF/ESI.
5233 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5234 struct prefix
*p
, struct bgp_path_info
*pi
)
5236 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5239 /* filter routes which have martian next hops */
5240 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5244 struct bgp_node
*rd_rn
, *rn
;
5245 struct bgp_table
*table
;
5246 struct bgp_path_info
*pi
;
5251 /* Walk entire global routing table and evaluate routes which could be
5252 * imported into this VPN. Note that we cannot just look at the routes
5253 * for the VNI's RD -
5254 * remote routes applicable for this VNI could have any RD.
5256 /* EVPN routes are a 2-level table. */
5257 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
5258 rd_rn
= bgp_route_next(rd_rn
)) {
5259 table
= bgp_node_get_bgp_table_info(rd_rn
);
5263 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5265 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
5268 /* Consider "valid" remote routes applicable for
5270 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5271 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5274 if (bgp_nexthop_self(bgp
, pi
->attr
->nexthop
)) {
5276 char attr_str
[BUFSIZ
] = {0};
5277 char pbuf
[PREFIX_STRLEN
];
5279 bgp_dump_attr(pi
->attr
, attr_str
,
5282 if (bgp_debug_update(pi
->peer
, &rn
->p
,
5285 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
5292 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5295 bgp_rib_remove(rn
, pi
, pi
->peer
, afi
,
5306 * Handle del of a local MACIP.
5308 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5309 struct ipaddr
*ip
, int state
)
5311 struct bgpevpn
*vpn
;
5312 struct prefix_evpn p
;
5313 struct bgp_node
*rn
;
5315 /* Lookup VNI hash - should exist. */
5316 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5317 if (!vpn
|| !is_vni_live(vpn
)) {
5318 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5319 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5320 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5324 build_evpn_type2_prefix(&p
, mac
, ip
);
5325 if (state
== ZEBRA_NEIGH_ACTIVE
) {
5326 /* Remove EVPN type-2 route and schedule for processing. */
5327 delete_evpn_route(bgp
, vpn
, &p
);
5329 /* Re-instate the current remote best path if any */
5330 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
5332 evpn_zebra_reinstall_best_route(bgp
, vpn
, rn
);
5339 * Handle add of a local MACIP.
5341 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5342 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
)
5344 struct bgpevpn
*vpn
;
5345 struct prefix_evpn p
;
5347 /* Lookup VNI hash - should exist. */
5348 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5349 if (!vpn
|| !is_vni_live(vpn
)) {
5350 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5351 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5352 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5356 /* Create EVPN type-2 route and schedule for processing. */
5357 build_evpn_type2_prefix(&p
, mac
, ip
);
5358 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
)) {
5359 char buf
[ETHER_ADDR_STRLEN
];
5360 char buf2
[INET6_ADDRSTRLEN
];
5363 EC_BGP_EVPN_ROUTE_CREATE
,
5364 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5365 bgp
->vrf_id
, vpn
->vni
,
5366 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5369 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5370 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5377 static void link_l2vni_hash_to_l3vni(struct hash_bucket
*bucket
,
5378 struct bgp
*bgp_vrf
)
5380 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
5381 struct bgp
*bgp_evpn
= NULL
;
5383 bgp_evpn
= bgp_get_evpn();
5386 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5387 bgpevpn_link_to_l3vni(vpn
);
5390 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
, struct ethaddr
*rmac
,
5391 struct in_addr originator_ip
, int filter
,
5392 ifindex_t svi_ifindex
)
5394 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5395 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5396 struct listnode
*node
= NULL
;
5397 struct bgpevpn
*vpn
= NULL
;
5400 /* get the EVPN instance - required to get the AS number for VRF
5403 bgp_evpn
= bgp_get_evpn();
5407 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
5413 /* if the BGP vrf instance doesn't exist - create one */
5414 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5419 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5420 vrf_id
== VRF_DEFAULT
? BGP_INSTANCE_TYPE_DEFAULT
5421 : BGP_INSTANCE_TYPE_VRF
);
5423 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
:
5424 flog_err(EC_BGP_MULTI_INSTANCE
,
5425 "'bgp multiple-instance' not present\n");
5427 case BGP_ERR_AS_MISMATCH
:
5428 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5429 "BGP is already running; AS is %u\n", as
);
5431 case BGP_ERR_INSTANCE_MISMATCH
:
5432 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5433 "BGP instance name and AS number mismatch\n");
5437 /* mark as auto created */
5438 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5441 /* associate the vrf with l3vni and related parameters */
5442 bgp_vrf
->l3vni
= l3vni
;
5443 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
5444 bgp_vrf
->originator_ip
= originator_ip
;
5445 bgp_vrf
->l3vni_svi_ifindex
= svi_ifindex
;
5447 /* set the right filter - are we using l3vni only for prefix routes? */
5449 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5451 /* Map auto derive or configured RTs */
5452 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5453 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5455 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
5457 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5458 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5460 /* auto derive RD */
5461 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5463 /* link all corresponding l2vnis */
5464 hash_iterate(bgp_evpn
->vnihash
,
5465 (void (*)(struct hash_bucket
*,
5466 void *))link_l2vni_hash_to_l3vni
,
5469 /* Only update all corresponding type-2 routes if we are advertising two
5470 * labels along with type-2 routes
5473 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5474 update_routes_for_vni(bgp_evpn
, vpn
);
5476 /* advertise type-5 routes if needed */
5477 update_advertise_vrf_routes(bgp_vrf
);
5479 /* install all remote routes belonging to this l3vni into correspondng
5481 install_routes_for_vrf(bgp_vrf
);
5486 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5488 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5489 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5490 struct listnode
*node
= NULL
;
5491 struct listnode
*next
= NULL
;
5492 struct bgpevpn
*vpn
= NULL
;
5494 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5498 "Cannot process L3VNI %u Del - Could not find BGP instance",
5503 bgp_evpn
= bgp_get_evpn();
5507 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
5512 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5513 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5514 * routes. This will uninstalling the routes from zebra and decremnt the
5517 uninstall_routes_for_vrf(bgp_vrf
);
5519 /* delete/withdraw all type-5 routes */
5520 delete_withdraw_vrf_routes(bgp_vrf
);
5522 /* remove the l3vni from vrf instance */
5525 /* remove the Rmac from the BGP vrf */
5526 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5528 /* remove default import RT or Unmap non-default import RT */
5529 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5530 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5531 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5532 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5535 /* remove default export RT */
5536 if (!list_isempty(bgp_vrf
->vrf_export_rtl
) &&
5537 !CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
5538 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5541 /* update all corresponding local mac-ip routes */
5542 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5543 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5544 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5545 update_routes_for_vni(bgp_evpn
, vpn
);
5549 /* If any L2VNIs point to this instance, unlink them. */
5550 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5551 bgpevpn_unlink_from_l3vni(vpn
);
5553 /* Delete the instance if it was autocreated */
5554 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5555 bgp_delete(bgp_vrf
);
5561 * Handle del of a local VNI.
5563 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5565 struct bgpevpn
*vpn
;
5567 /* Locate VNI hash */
5568 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5570 if (bgp_debug_zebra(NULL
))
5572 EC_BGP_EVPN_VPN_VNI
,
5573 "%u: VNI hash entry for VNI %u not found at DEL",
5578 /* Remove all local EVPN routes and schedule for processing (to
5579 * withdraw from peers).
5581 delete_routes_for_vni(bgp
, vpn
);
5584 * tunnel is no longer active, del tunnel ip address from tip_hash
5586 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5588 /* Clear "live" flag and see if hash needs to be freed. */
5589 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5590 if (!is_vni_configured(vpn
))
5591 bgp_evpn_free(bgp
, vpn
);
5597 * Handle add (or update) of a local VNI. The VNI changes we care
5598 * about are for the local-tunnel-ip and the (tenant) VRF.
5600 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5601 struct in_addr originator_ip
, vrf_id_t tenant_vrf_id
)
5603 struct bgpevpn
*vpn
;
5604 struct prefix_evpn p
;
5606 /* Lookup VNI. If present and no change, exit. */
5607 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5610 if (is_vni_live(vpn
)
5611 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5612 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5613 /* Probably some other param has changed that we don't
5617 /* Update tenant_vrf_id if it has changed. */
5618 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5619 bgpevpn_unlink_from_l3vni(vpn
);
5620 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5621 bgpevpn_link_to_l3vni(vpn
);
5624 /* If tunnel endpoint IP has changed, update (and delete prior
5625 * type-3 route, if needed.)
5627 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5628 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5630 /* Update all routes with new endpoint IP and/or export RT
5633 if (is_vni_live(vpn
))
5634 update_routes_for_vni(bgp
, vpn
);
5637 /* Create or update as appropriate. */
5639 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
);
5643 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5649 /* if the VNI is live already, there is nothing more to do */
5650 if (is_vni_live(vpn
))
5653 /* Mark as "live" */
5654 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5656 /* tunnel is now active, add tunnel-ip to db */
5657 bgp_tip_add(bgp
, &originator_ip
);
5659 /* filter routes as nexthop database has changed */
5660 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5663 * Create EVPN type-3 route and schedule for processing.
5665 * RT-3 only if doing head-end replication
5667 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
5668 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5669 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0)) {
5670 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5671 "%u: Type3 route creation failure for VNI %u",
5677 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5681 install_routes_for_vni(bgp
, vpn
);
5683 /* If we are advertising gateway mac-ip
5684 It needs to be conveyed again to zebra */
5685 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5691 * bgp_evpn_local_es_del
5693 int bgp_evpn_local_es_del(struct bgp
*bgp
,
5695 struct ipaddr
*originator_ip
)
5697 char buf
[ESI_STR_LEN
];
5698 struct evpnes
*es
= NULL
;
5700 if (!bgp
->esihash
) {
5701 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5706 /* Lookup ESI hash - should exist. */
5707 es
= bgp_evpn_lookup_es(bgp
, esi
);
5709 flog_warn(EC_BGP_EVPN_ESI
,
5710 "%u: ESI hash entry for ESI %s at Local ES DEL",
5711 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5715 /* Delete all local EVPN ES routes from ESI table
5716 * and schedule for processing (to withdraw from peers))
5718 delete_routes_for_es(bgp
, es
);
5720 /* free the hash entry */
5721 bgp_evpn_es_free(bgp
, es
);
5727 * bgp_evpn_local_es_add
5729 int bgp_evpn_local_es_add(struct bgp
*bgp
,
5731 struct ipaddr
*originator_ip
)
5733 char buf
[ESI_STR_LEN
];
5734 struct evpnes
*es
= NULL
;
5735 struct prefix_evpn p
;
5737 if (!bgp
->esihash
) {
5738 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5743 /* create the new es */
5744 es
= bgp_evpn_lookup_es(bgp
, esi
);
5746 es
= bgp_evpn_es_new(bgp
, esi
, originator_ip
);
5750 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
5751 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5755 UNSET_FLAG(es
->flags
, EVPNES_REMOTE
);
5756 SET_FLAG(es
->flags
, EVPNES_LOCAL
);
5758 build_evpn_type4_prefix(&p
, esi
, originator_ip
->ipaddr_v4
);
5759 if (update_evpn_type4_route(bgp
, es
, &p
)) {
5760 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5761 "%u: Type4 route creation failure for ESI %s",
5762 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5766 /* import all remote ES routes in th ES table */
5767 install_routes_for_es(bgp
, es
);
5773 * Handle change in setting for BUM handling. The supported values
5774 * are head-end replication and dropping all BUM packets. Any change
5775 * should be registered with zebra. Also, if doing head-end replication,
5776 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5777 * to be dropped, the RT-3s must be withdrawn.
5779 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
5781 zlog_info("L2VPN EVPN BUM handling is %s",
5782 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
5783 "Flooding" : "Flooding Disabled");
5785 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
5786 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
5787 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
5788 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
5789 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
5793 * Cleanup EVPN information on disable - Need to delete and withdraw
5794 * EVPN routes from peers.
5796 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5798 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_bucket
*,
5799 void *))cleanup_vni_on_disable
,
5804 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5805 * BGP instance (default) is being freed.
5807 void bgp_evpn_cleanup(struct bgp
*bgp
)
5809 hash_iterate(bgp
->vnihash
,
5810 (void (*)(struct hash_bucket
*, void *))free_vni_entry
,
5813 hash_free(bgp
->import_rt_hash
);
5814 bgp
->import_rt_hash
= NULL
;
5816 hash_free(bgp
->vrf_import_rt_hash
);
5817 bgp
->vrf_import_rt_hash
= NULL
;
5819 hash_free(bgp
->vnihash
);
5820 bgp
->vnihash
= NULL
;
5822 hash_free(bgp
->esihash
);
5823 bgp
->esihash
= NULL
;
5825 list_delete(&bgp
->vrf_import_rtl
);
5826 list_delete(&bgp
->vrf_export_rtl
);
5827 list_delete(&bgp
->l2vnis
);
5831 * Initialization for EVPN
5834 * hash for RT to VNI
5836 void bgp_evpn_init(struct bgp
*bgp
)
5839 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
5841 hash_create(esi_hash_keymake
, esi_cmp
,
5842 "BGP EVPN Local ESI Hash");
5843 bgp
->import_rt_hash
=
5844 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
5845 "BGP Import RT Hash");
5846 bgp
->vrf_import_rt_hash
=
5847 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
5848 "BGP VRF Import RT Hash");
5849 bgp
->vrf_import_rtl
= list_new();
5850 bgp
->vrf_import_rtl
->cmp
=
5851 (int (*)(void *, void *))evpn_route_target_cmp
;
5852 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
5853 bgp
->vrf_export_rtl
= list_new();
5854 bgp
->vrf_export_rtl
->cmp
=
5855 (int (*)(void *, void *))evpn_route_target_cmp
;
5856 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
5857 bgp
->l2vnis
= list_new();
5858 bgp
->l2vnis
->cmp
= vni_list_cmp
;
5859 /* By default Duplicate Address Dection is enabled.
5860 * Max-moves (N) 5, detection time (M) 180
5861 * default action is warning-only
5862 * freeze action permanently freezes address,
5863 * and freeze time (auto-recovery) is disabled.
5865 if (bgp
->evpn_info
) {
5866 bgp
->evpn_info
->dup_addr_detect
= true;
5867 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
5868 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
5869 bgp
->evpn_info
->dad_freeze
= false;
5870 bgp
->evpn_info
->dad_freeze_time
= 0;
5871 /* Initialize zebra vxlan */
5872 bgp_zebra_dup_addr_detection(bgp
);
5875 /* Default BUM handling is to do head-end replication. */
5876 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
5879 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
5881 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);