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"
52 * Definitions and external declarations.
54 extern struct zclient
*zclient
;
56 DEFINE_QOBJ_TYPE(bgpevpn
)
57 DEFINE_QOBJ_TYPE(evpnes
)
61 * Static function declarations
63 static void delete_evpn_route_entry(struct bgp
*bgp
,
64 afi_t afi
, safi_t safi
,
66 struct bgp_info
**ri
);
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(const void *p1
, const void *p2
)
76 const struct in_addr
*ip1
= p1
;
77 const struct in_addr
*ip2
= p2
;
83 return (ip1
->s_addr
== ip2
->s_addr
);
87 * Make hash key for ESI.
89 static unsigned int esi_hash_keymake(void *p
)
91 struct evpnes
*pes
= p
;
92 const void *pnt
= (void *)pes
->esi
.val
;
94 return jhash(pnt
, ESI_BYTES
, 0xa5a5a55a);
100 static int esi_cmp(const void *p1
, const void *p2
)
102 const struct evpnes
*pes1
= p1
;
103 const struct evpnes
*pes2
= p2
;
105 if (pes1
== NULL
&& pes2
== NULL
)
108 if (pes1
== NULL
|| pes2
== NULL
)
111 return (memcmp(pes1
->esi
.val
, pes2
->esi
.val
, ESI_BYTES
) == 0);
117 static unsigned int vni_hash_key_make(void *p
)
119 struct bgpevpn
*vpn
= p
;
120 return (jhash_1word(vpn
->vni
, 0));
124 * Comparison function for vni hash
126 static int vni_hash_cmp(const void *p1
, const void *p2
)
128 const struct bgpevpn
*vpn1
= p1
;
129 const struct bgpevpn
*vpn2
= p2
;
135 return (vpn1
->vni
== vpn2
->vni
);
139 * Make vrf import route target hash key.
141 static unsigned int vrf_import_rt_hash_key_make(void *p
)
143 struct vrf_irt_node
*irt
= p
;
144 char *pnt
= irt
->rt
.val
;
146 return jhash(pnt
, 8, 0x5abc1234);
150 * Comparison function for vrf import rt hash
152 static int vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
154 const struct vrf_irt_node
*irt1
= p1
;
155 const struct vrf_irt_node
*irt2
= p2
;
157 if (irt1
== NULL
&& irt2
== NULL
)
160 if (irt1
== NULL
|| irt2
== NULL
)
163 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
167 * Create a new vrf import_rt in default instance
169 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
171 struct bgp
*bgp_def
= NULL
;
172 struct vrf_irt_node
*irt
;
174 bgp_def
= bgp_get_default();
176 flog_err(EC_BGP_NO_DFLT
,
177 "vrf import rt new - def instance not created yet");
181 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
182 sizeof(struct vrf_irt_node
));
187 irt
->vrfs
= list_new();
190 if (!hash_get(bgp_def
->vrf_import_rt_hash
, irt
, hash_alloc_intern
)) {
191 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
199 * Free the vrf import rt node
201 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
203 struct bgp
*bgp_def
= NULL
;
205 bgp_def
= bgp_get_default();
207 flog_err(EC_BGP_NO_DFLT
,
208 "vrf import rt free - def instance not created yet");
212 hash_release(bgp_def
->vrf_import_rt_hash
, irt
);
213 list_delete(&irt
->vrfs
);
214 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
218 * Function to lookup Import RT node - used to map a RT to set of
219 * VNIs importing routes with that RT.
221 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
223 struct bgp
*bgp_def
= NULL
;
224 struct vrf_irt_node
*irt
;
225 struct vrf_irt_node tmp
;
227 bgp_def
= bgp_get_default();
229 flog_err(EC_BGP_NO_DFLT
,
230 "vrf import rt lookup - def instance not created yet");
234 memset(&tmp
, 0, sizeof(struct vrf_irt_node
));
235 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
236 irt
= hash_lookup(bgp_def
->vrf_import_rt_hash
, &tmp
);
241 * Is specified VRF present on the RT's list of "importing" VRFs?
243 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
, struct bgp
*bgp_vrf
)
245 struct listnode
*node
= NULL
, *nnode
= NULL
;
246 struct bgp
*tmp_bgp_vrf
= NULL
;
248 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
249 if (tmp_bgp_vrf
== bgp_vrf
)
256 * Make import route target hash key.
258 static unsigned int import_rt_hash_key_make(void *p
)
260 struct irt_node
*irt
= p
;
261 char *pnt
= irt
->rt
.val
;
263 return jhash(pnt
, 8, 0xdeadbeef);
267 * Comparison function for import rt hash
269 static int import_rt_hash_cmp(const void *p1
, const void *p2
)
271 const struct irt_node
*irt1
= p1
;
272 const struct irt_node
*irt2
= p2
;
274 if (irt1
== NULL
&& irt2
== NULL
)
277 if (irt1
== NULL
|| irt2
== NULL
)
280 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
284 * Create a new import_rt
286 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
287 struct ecommunity_val
*rt
)
289 struct irt_node
*irt
;
294 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
299 irt
->vnis
= list_new();
302 if (!hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
)) {
303 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
311 * Free the import rt node
313 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
315 hash_release(bgp
->import_rt_hash
, irt
);
316 list_delete(&irt
->vnis
);
317 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
321 * Function to lookup Import RT node - used to map a RT to set of
322 * VNIs importing routes with that RT.
324 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
325 struct ecommunity_val
*rt
)
327 struct irt_node
*irt
;
330 memset(&tmp
, 0, sizeof(struct irt_node
));
331 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
332 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
337 * Is specified VNI present on the RT's list of "importing" VNIs?
339 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
341 struct listnode
*node
, *nnode
;
342 struct bgpevpn
*tmp_vpn
;
344 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
353 * Compare Route Targets.
355 static int evpn_route_target_cmp(struct ecommunity
*ecom1
,
356 struct ecommunity
*ecom2
)
364 if (!ecom1
&& !ecom2
)
367 if (ecom1
->str
&& !ecom2
->str
)
370 if (!ecom1
->str
&& ecom2
->str
)
373 if (!ecom1
->str
&& !ecom2
->str
)
376 return strcmp(ecom1
->str
, ecom2
->str
);
379 static void evpn_xxport_delete_ecomm(void *val
)
381 struct ecommunity
*ecomm
= val
;
382 ecommunity_free(&ecomm
);
386 * Mask off global-admin field of specified extended community (RT),
387 * just retain the local-admin field.
389 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
390 struct ecommunity_val
*src
)
396 if (type
== ECOMMUNITY_ENCODE_AS
) {
397 dst
->val
[2] = dst
->val
[3] = 0;
398 } else if (type
== ECOMMUNITY_ENCODE_AS4
399 || type
== ECOMMUNITY_ENCODE_IP
) {
400 dst
->val
[2] = dst
->val
[3] = 0;
401 dst
->val
[4] = dst
->val
[5] = 0;
406 * Map one RT to specified VRF.
407 * bgp_vrf = BGP vrf instance
409 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct ecommunity_val
*eval
)
411 struct vrf_irt_node
*irt
= NULL
;
412 struct ecommunity_val eval_tmp
;
414 /* If using "automatic" RT,
415 * we only care about the local-admin sub-field.
416 * This is to facilitate using L3VNI(VRF-VNI)
417 * as the RT for EBGP peering too.
419 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
420 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
421 mask_ecom_global_admin(&eval_tmp
, eval
);
423 irt
= lookup_vrf_import_rt(&eval_tmp
);
424 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
425 /* Already mapped. */
429 irt
= vrf_import_rt_new(&eval_tmp
);
431 /* Add VRF to the list for this RT. */
432 listnode_add(irt
->vrfs
, bgp_vrf
);
436 * Unmap specified VRF from specified RT. If there are no other
437 * VRFs for this RT, then the RT hash is deleted.
438 * bgp_vrf: BGP VRF specific instance
440 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
, struct vrf_irt_node
*irt
)
442 /* Delete VRF from list for this RT. */
443 listnode_delete(irt
->vrfs
, bgp_vrf
);
444 if (!listnode_head(irt
->vrfs
)) {
445 vrf_import_rt_free(irt
);
450 * Map one RT to specified VNI.
452 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
453 struct ecommunity_val
*eval
)
455 struct irt_node
*irt
;
456 struct ecommunity_val eval_tmp
;
458 /* If using "automatic" RT, we only care about the local-admin
460 * This is to facilitate using VNI as the RT for EBGP peering too.
462 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
463 if (!is_import_rt_configured(vpn
))
464 mask_ecom_global_admin(&eval_tmp
, eval
);
466 irt
= lookup_import_rt(bgp
, &eval_tmp
);
468 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
469 /* Already mapped. */
473 irt
= import_rt_new(bgp
, &eval_tmp
);
477 /* Add VNI to the hash list for this RT. */
478 listnode_add(irt
->vnis
, vpn
);
482 * Unmap specified VNI from specified RT. If there are no other
483 * VNIs for this RT, then the RT hash is deleted.
485 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
486 struct irt_node
*irt
)
488 /* Delete VNI from hash list for this RT. */
489 listnode_delete(irt
->vnis
, vpn
);
490 if (!listnode_head(irt
->vnis
)) {
491 import_rt_free(bgp
, irt
);
496 * Create RT extended community automatically from passed information:
497 * of the form AS:VNI.
498 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
499 * the need is to get a RT value that will be unique across different
500 * VNIs but the same across routers (in the same AS) for a particular
503 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
505 struct ecommunity_val eval
;
506 struct ecommunity
*ecomadd
;
508 if (bgp
->advertise_autort_rfc8365
)
509 vni
|= EVPN_AUTORT_VXLAN
;
510 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
512 ecomadd
= ecommunity_new();
513 ecommunity_add_val(ecomadd
, &eval
);
514 listnode_add_sort(rtl
, ecomadd
);
518 * Derive RD and RT for a VNI automatically. Invoked at the time of
521 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
523 bgp_evpn_derive_auto_rd(bgp
, vpn
);
524 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
525 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
529 * Convert nexthop (remote VTEP IP) into an IPv6 address.
531 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
533 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
535 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
536 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
540 * Add (update) or delete MACIP from zebra.
542 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
543 struct prefix_evpn
*p
,
544 struct in_addr remote_vtep_ip
, int add
,
545 uint8_t flags
, uint32_t seq
)
549 char buf1
[ETHER_ADDR_STRLEN
];
550 char buf2
[INET6_ADDRSTRLEN
];
551 char buf3
[INET6_ADDRSTRLEN
];
554 if (!zclient
|| zclient
->sock
< 0)
557 /* Don't try to register if Zebra doesn't know of this instance. */
558 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
564 zclient_create_header(
565 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
567 stream_putl(s
, vpn
->vni
);
568 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
569 /* IP address length and IP address, if any. */
570 if (is_evpn_prefix_ipaddr_none(p
))
573 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
575 stream_putl(s
, ipa_len
);
576 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
578 stream_put_in_addr(s
, &remote_vtep_ip
);
580 /* TX flags - MAC sticky status and/or gateway mac */
581 /* Also TX the sequence number of the best route. */
583 stream_putc(s
, flags
);
587 stream_putw_at(s
, 0, stream_get_endp(s
));
589 if (bgp_debug_zebra(NULL
))
591 "Tx %s MACIP, VNI %u MAC %s IP %s flags 0x%x seq %u remote VTEP %s",
592 add
? "ADD" : "DEL", vpn
->vni
,
593 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
595 ipaddr2str(&p
->prefix
.macip_addr
.ip
,
596 buf3
, sizeof(buf3
)), flags
, seq
,
597 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
600 return zclient_send_message(zclient
);
604 * Add (update) or delete remote VTEP from zebra.
606 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
607 struct prefix_evpn
*p
, int add
)
612 if (!zclient
|| zclient
->sock
< 0)
615 /* Don't try to register if Zebra doesn't know of this instance. */
616 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
622 zclient_create_header(
623 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
625 stream_putl(s
, vpn
->vni
);
626 if (is_evpn_prefix_ipaddr_v4(p
))
627 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
628 else if (is_evpn_prefix_ipaddr_v6(p
)) {
631 "Bad remote IP when trying to %s remote VTEP for VNI %u",
632 add
? "ADD" : "DEL", vpn
->vni
);
636 stream_putw_at(s
, 0, stream_get_endp(s
));
638 if (bgp_debug_zebra(NULL
))
639 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
640 add
? "ADD" : "DEL", vpn
->vni
,
641 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
643 return zclient_send_message(zclient
);
647 * Build extended community for EVPN ES (type-4) route
649 static void build_evpn_type4_route_extcomm(struct evpnes
*es
,
652 struct ecommunity ecom_encap
;
653 struct ecommunity ecom_es_rt
;
654 struct ecommunity_val eval
;
655 struct ecommunity_val eval_es_rt
;
656 bgp_encap_types tnl_type
;
660 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
661 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
662 encode_encap_extcomm(tnl_type
, &eval
);
664 ecom_encap
.val
= (uint8_t *)eval
.val
;
665 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
668 memset(&mac
, 0, sizeof(struct ethaddr
));
669 memset(&ecom_es_rt
, 0, sizeof(ecom_es_rt
));
670 es_get_system_mac(&es
->esi
, &mac
);
671 encode_es_rt_extcomm(&eval_es_rt
, &mac
);
673 ecom_es_rt
.val
= (uint8_t *)eval_es_rt
.val
;
675 ecommunity_merge(attr
->ecommunity
, &ecom_es_rt
);
677 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
681 * Build extended communities for EVPN prefix route.
683 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
686 struct ecommunity ecom_encap
;
687 struct ecommunity ecom_rmac
;
688 struct ecommunity_val eval
;
689 struct ecommunity_val eval_rmac
;
690 bgp_encap_types tnl_type
;
691 struct listnode
*node
, *nnode
;
692 struct ecommunity
*ecom
;
693 struct list
*vrf_export_rtl
= NULL
;
696 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
697 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
698 encode_encap_extcomm(tnl_type
, &eval
);
700 ecom_encap
.val
= (uint8_t *)eval
.val
;
703 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
705 /* Add the export RTs for L3VNI/VRF */
706 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
707 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
709 ecommunity_merge(attr
->ecommunity
, ecom
);
711 /* add the router mac extended community */
712 if (!is_zero_mac(&attr
->rmac
)) {
713 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
714 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
716 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
718 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
721 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
725 * Build extended communities for EVPN route.
726 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
727 * and ENCAP extended communities are applicable for all routes.
728 * The default gateway extended community and MAC mobility (sticky) extended
729 * community are added as needed based on passed settings - only for type-2
730 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
731 * added, if present, based on passed settings - only for non-link-local
734 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
737 struct ecommunity ecom_encap
;
738 struct ecommunity ecom_sticky
;
739 struct ecommunity ecom_default_gw
;
740 struct ecommunity ecom_rmac
;
741 struct ecommunity ecom_na
;
742 struct ecommunity_val eval
;
743 struct ecommunity_val eval_sticky
;
744 struct ecommunity_val eval_default_gw
;
745 struct ecommunity_val eval_rmac
;
746 struct ecommunity_val eval_na
;
748 bgp_encap_types tnl_type
;
749 struct listnode
*node
, *nnode
;
750 struct ecommunity
*ecom
;
752 struct list
*vrf_export_rtl
= NULL
;
755 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
756 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
757 encode_encap_extcomm(tnl_type
, &eval
);
759 ecom_encap
.val
= (uint8_t *)eval
.val
;
762 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
764 /* Add the export RTs for L2VNI */
765 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
766 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
768 /* Add the export RTs for L3VNI if told to - caller determines
769 * when this should be done.
772 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
773 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
774 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
776 attr
->ecommunity
= ecommunity_merge(
777 attr
->ecommunity
, ecom
);
781 /* Add MAC mobility (sticky) if needed. */
784 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
785 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
786 ecom_sticky
.size
= 1;
787 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
789 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
792 /* Add RMAC, if told to. */
794 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
795 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
797 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
799 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
802 /* Add default gateway, if needed. */
803 if (attr
->default_gw
) {
804 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
805 encode_default_gw_extcomm(&eval_default_gw
);
806 ecom_default_gw
.size
= 1;
807 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
809 ecommunity_merge(attr
->ecommunity
, &ecom_default_gw
);
812 if (attr
->router_flag
) {
813 memset(&ecom_na
, 0, sizeof(ecom_na
));
814 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
);
816 ecom_na
.val
= (uint8_t *)eval_na
.val
;
817 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
821 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
825 * Add MAC mobility extended community to attribute.
827 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
829 struct ecommunity ecom_tmp
;
830 struct ecommunity_val eval
;
831 uint8_t *ecom_val_ptr
;
838 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
840 /* Find current MM ecommunity */
843 if (attr
->ecommunity
) {
844 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
845 pnt
= attr
->ecommunity
->val
+ (i
* 8);
849 if (type
== ECOMMUNITY_ENCODE_EVPN
851 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
852 ecom_val_ptr
= (uint8_t *)(attr
->ecommunity
->val
859 /* Update the existing MM ecommunity */
861 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
863 /* Add MM to existing */
865 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
867 ecom_tmp
.val
= (uint8_t *)eval
.val
;
869 if (attr
->ecommunity
)
871 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
873 attr
->ecommunity
= ecommunity_dup(&ecom_tmp
);
877 /* Install EVPN route into zebra. */
878 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
879 struct prefix_evpn
*p
,
880 struct in_addr remote_vtep_ip
, uint8_t flags
,
885 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
886 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
889 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 1);
894 /* Uninstall EVPN route from zebra. */
895 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
896 struct prefix_evpn
*p
,
897 struct in_addr remote_vtep_ip
)
901 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
902 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
905 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 0);
911 * Due to MAC mobility, the prior "local" best route has been supplanted
912 * by a "remote" best route. The prior route has to be deleted and withdrawn
915 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
917 struct bgp_info
*old_local
)
919 struct bgp_node
*global_rn
;
921 afi_t afi
= AFI_L2VPN
;
922 safi_t safi
= SAFI_EVPN
;
924 /* Locate route node in the global EVPN routing table. Note that
925 * this table is a 2-level tree (RD-level + Prefix-level) similar to
928 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
929 (struct prefix
*)&rn
->p
, &vpn
->prd
);
931 /* Delete route entry in the global EVPN table. */
932 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &ri
);
934 /* Schedule for processing - withdraws to peers happen from
938 bgp_process(bgp
, global_rn
, afi
, safi
);
939 bgp_unlock_node(global_rn
);
942 /* Delete route entry in the VNI route table, caller to remove. */
943 bgp_info_delete(rn
, old_local
);
946 static struct in_addr
*es_vtep_new(struct in_addr vtep
)
950 ip
= XCALLOC(MTYPE_BGP_EVPN_ES_VTEP
, sizeof(struct in_addr
));
954 ip
->s_addr
= vtep
.s_addr
;
958 static void es_vtep_free(struct in_addr
*ip
)
960 XFREE(MTYPE_BGP_EVPN_ES_VTEP
, ip
);
963 /* check if VTEP is already part of the list */
964 static int is_vtep_present_in_list(struct list
*list
,
967 struct listnode
*node
= NULL
;
970 for (ALL_LIST_ELEMENTS_RO(list
, node
, tmp
)) {
971 if (tmp
->s_addr
== vtep
.s_addr
)
978 * Best path for ES route was changed,
979 * update the list of VTEPs for this ES
981 static int evpn_es_install_vtep(struct bgp
*bgp
,
983 struct prefix_evpn
*p
,
984 struct in_addr rvtep
)
986 struct in_addr
*vtep_ip
;
988 if (is_vtep_present_in_list(es
->vtep_list
, rvtep
))
992 vtep_ip
= es_vtep_new(rvtep
);
994 listnode_add_sort(es
->vtep_list
, vtep_ip
);
999 * Best path for ES route was changed,
1000 * update the list of VTEPs for this ES
1002 static int evpn_es_uninstall_vtep(struct bgp
*bgp
,
1004 struct prefix_evpn
*p
,
1005 struct in_addr rvtep
)
1007 struct listnode
*node
, *nnode
, *node_to_del
= NULL
;
1008 struct in_addr
*tmp
;
1010 for (ALL_LIST_ELEMENTS(es
->vtep_list
, node
, nnode
, tmp
)) {
1011 if (tmp
->s_addr
== rvtep
.s_addr
) {
1018 list_delete_node(es
->vtep_list
, node_to_del
);
1024 * Calculate the best path for a ES(type-4) route.
1026 static int evpn_es_route_select_install(struct bgp
*bgp
,
1028 struct bgp_node
*rn
)
1031 afi_t afi
= AFI_L2VPN
;
1032 safi_t safi
= SAFI_EVPN
;
1033 struct bgp_info
*old_select
; /* old best */
1034 struct bgp_info
*new_select
; /* new best */
1035 struct bgp_info_pair old_and_new
;
1037 /* Compute the best path. */
1038 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
],
1039 &old_and_new
, afi
, safi
);
1040 old_select
= old_and_new
.old
;
1041 new_select
= old_and_new
.new;
1044 * If the best path hasn't changed - see if something needs to be
1047 if (old_select
&& old_select
== new_select
1048 && old_select
->type
== ZEBRA_ROUTE_BGP
1049 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1050 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1051 && !CHECK_FLAG(old_select
->flags
, BGP_INFO_ATTR_CHANGED
)
1052 && !bgp
->addpath_tx_used
[afi
][safi
]) {
1053 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1054 ret
= evpn_es_install_vtep(bgp
, es
,
1055 (struct prefix_evpn
*)&rn
->p
,
1056 old_select
->attr
->nexthop
);
1058 UNSET_FLAG(old_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
1059 bgp_zebra_clear_route_change_flags(rn
);
1063 /* If the user did a "clear" this flag will be set */
1064 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1067 * bestpath has changed; update relevant fields and install or uninstall
1068 * into the zebra RIB.
1070 if (old_select
|| new_select
)
1071 bgp_bump_version(rn
);
1074 bgp_info_unset_flag(rn
, old_select
, BGP_INFO_SELECTED
);
1076 bgp_info_set_flag(rn
, new_select
, BGP_INFO_SELECTED
);
1077 bgp_info_unset_flag(rn
, new_select
, BGP_INFO_ATTR_CHANGED
);
1078 UNSET_FLAG(new_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
1081 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1082 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1083 ret
= evpn_es_install_vtep(bgp
, es
,
1084 (struct prefix_evpn
*)&rn
->p
,
1085 new_select
->attr
->nexthop
);
1087 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1088 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1089 ret
= evpn_es_uninstall_vtep(
1090 bgp
, es
, (struct prefix_evpn
*)&rn
->p
,
1091 old_select
->attr
->nexthop
);
1094 /* Clear any route change flags. */
1095 bgp_zebra_clear_route_change_flags(rn
);
1097 /* Reap old select bgp_info, if it has been removed */
1098 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_INFO_REMOVED
))
1099 bgp_info_reap(rn
, old_select
);
1105 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1108 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1109 struct bgp_node
*rn
)
1111 struct bgp_info
*old_select
, *new_select
;
1112 struct bgp_info_pair old_and_new
;
1113 struct prefix_evpn
*evp
;
1114 afi_t afi
= AFI_L2VPN
;
1115 safi_t safi
= SAFI_EVPN
;
1119 /* Compute the best path. */
1120 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1122 old_select
= old_and_new
.old
;
1123 new_select
= old_and_new
.new;
1125 evp
= (struct prefix_evpn
*)&rn
->p
;
1126 /* If the best path hasn't changed - see if there is still something to
1130 if (old_select
&& old_select
== new_select
1131 && old_select
->type
== ZEBRA_ROUTE_BGP
1132 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1133 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1134 && !CHECK_FLAG(old_select
->flags
, BGP_INFO_ATTR_CHANGED
)
1135 && !bgp
->addpath_tx_used
[afi
][safi
]) {
1136 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1137 if (old_select
->attr
->sticky
)
1138 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
1139 if (old_select
->attr
->default_gw
)
1140 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
1141 if (is_evpn_prefix_ipaddr_v6(evp
) &&
1142 old_select
->attr
->router_flag
)
1143 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
1145 ret
= evpn_zebra_install(
1146 bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1147 old_select
->attr
->nexthop
, flags
,
1148 mac_mobility_seqnum(old_select
->attr
));
1150 UNSET_FLAG(old_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
1151 bgp_zebra_clear_route_change_flags(rn
);
1155 /* If the user did a "clear" this flag will be set */
1156 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1158 /* bestpath has changed; update relevant fields and install or uninstall
1159 * into the zebra RIB.
1161 if (old_select
|| new_select
)
1162 bgp_bump_version(rn
);
1165 bgp_info_unset_flag(rn
, old_select
, BGP_INFO_SELECTED
);
1167 bgp_info_set_flag(rn
, new_select
, BGP_INFO_SELECTED
);
1168 bgp_info_unset_flag(rn
, new_select
, BGP_INFO_ATTR_CHANGED
);
1169 UNSET_FLAG(new_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
1172 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1173 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1175 if (new_select
->attr
->sticky
)
1176 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
1177 if (new_select
->attr
->default_gw
)
1178 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
1179 if (is_evpn_prefix_ipaddr_v6(evp
) &&
1180 new_select
->attr
->router_flag
)
1181 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
1183 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1184 new_select
->attr
->nexthop
, flags
,
1185 mac_mobility_seqnum(new_select
->attr
));
1186 /* If an old best existed and it was a "local" route, the only
1188 * it would be supplanted is due to MAC mobility procedures. So,
1190 * need to do an implicit delete and withdraw that route from
1193 if (old_select
&& old_select
->peer
== bgp
->peer_self
1194 && old_select
->type
== ZEBRA_ROUTE_BGP
1195 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
1196 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
1198 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1199 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1200 ret
= evpn_zebra_uninstall(bgp
, vpn
,
1201 (struct prefix_evpn
*)&rn
->p
,
1202 old_select
->attr
->nexthop
);
1205 /* Clear any route change flags. */
1206 bgp_zebra_clear_route_change_flags(rn
);
1208 /* Reap old select bgp_info, if it has been removed */
1209 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_INFO_REMOVED
))
1210 bgp_info_reap(rn
, old_select
);
1216 * Return true if the local ri for this rn is of type gateway mac
1218 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
1220 struct bgp_info
*tmp_ri
= NULL
;
1221 struct bgp_info
*local_ri
= NULL
;
1224 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1225 if (tmp_ri
->peer
== bgp
->peer_self
1226 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1227 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1234 return local_ri
->attr
->default_gw
;
1239 * Return true if the local ri for this rn has sticky set
1241 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
1243 struct bgp_info
*tmp_ri
;
1244 struct bgp_info
*local_ri
;
1247 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1248 if (tmp_ri
->peer
== bgp
->peer_self
1249 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1250 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1257 return local_ri
->attr
->sticky
;
1261 * create or update EVPN type4 route entry.
1262 * This could be in the ES table or the global table.
1263 * TODO: handle remote ES (type4) routes as well
1265 static int update_evpn_type4_route_entry(struct bgp
*bgp
,
1267 afi_t afi
, safi_t safi
,
1268 struct bgp_node
*rn
,
1271 struct bgp_info
**ri
,
1274 char buf
[ESI_STR_LEN
];
1275 char buf1
[INET6_ADDRSTRLEN
];
1276 struct bgp_info
*tmp_ri
= NULL
;
1277 struct bgp_info
*local_ri
= NULL
; /* local route entry if any */
1278 struct bgp_info
*remote_ri
= NULL
; /* remote route entry if any */
1279 struct attr
*attr_new
= NULL
;
1280 struct prefix_evpn
*evp
= NULL
;
1284 evp
= (struct prefix_evpn
*)&rn
->p
;
1286 /* locate the local and remote entries if any */
1287 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1288 if (tmp_ri
->peer
== bgp
->peer_self
&&
1289 tmp_ri
->type
== ZEBRA_ROUTE_BGP
&&
1290 tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1292 if (tmp_ri
->type
== ZEBRA_ROUTE_BGP
&&
1293 tmp_ri
->sub_type
== BGP_ROUTE_IMPORTED
&&
1294 CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
))
1298 /* we don't expect to see a remote_ri at this point.
1299 * An ES route has esi + vtep_ip as the key,
1300 * We shouldn't see the same route from any other vtep.
1305 "%u ERROR: local es route for ESI: %s Vtep %s also learnt from remote",
1307 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1308 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1312 if (!local_ri
&& !add
)
1315 /* create or update the entry */
1318 /* Add or update attribute to hash */
1319 attr_new
= bgp_attr_intern(attr
);
1321 /* Create new route with its attribute. */
1322 tmp_ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
,
1323 0, bgp
->peer_self
, attr_new
, rn
);
1324 SET_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
);
1326 /* add the newly created path to the route-node */
1327 bgp_info_add(rn
, tmp_ri
);
1330 if (attrhash_cmp(tmp_ri
->attr
, attr
)
1331 && !CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1334 /* The attribute has changed.
1335 * Add (or update) attribute to hash. */
1336 attr_new
= bgp_attr_intern(attr
);
1337 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1339 /* Restore route, if needed. */
1340 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1341 bgp_info_restore(rn
, tmp_ri
);
1343 /* Unintern existing, set to new. */
1344 bgp_attr_unintern(&tmp_ri
->attr
);
1345 tmp_ri
->attr
= attr_new
;
1346 tmp_ri
->uptime
= bgp_clock();
1350 /* Return back the route entry. */
1355 /* update evpn es (type-4) route */
1356 static int update_evpn_type4_route(struct bgp
*bgp
,
1358 struct prefix_evpn
*p
)
1361 int route_changed
= 0;
1362 char buf
[ESI_STR_LEN
];
1363 char buf1
[INET6_ADDRSTRLEN
];
1364 afi_t afi
= AFI_L2VPN
;
1365 safi_t safi
= SAFI_EVPN
;
1367 struct attr
*attr_new
= NULL
;
1368 struct bgp_node
*rn
= NULL
;
1369 struct bgp_info
*ri
= NULL
;
1371 memset(&attr
, 0, sizeof(struct attr
));
1373 /* Build path-attribute for this route. */
1374 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1375 attr
.nexthop
= es
->originator_ip
.ipaddr_v4
;
1376 attr
.mp_nexthop_global_in
= es
->originator_ip
.ipaddr_v4
;
1377 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1379 /* Set up extended community. */
1380 build_evpn_type4_route_extcomm(es
, &attr
);
1382 /* First, create (or fetch) route node within the ESI. */
1383 /* NOTE: There is no RD here. */
1384 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
1386 /* Create or update route entry. */
1387 ret
= update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
,
1391 flog_err(EC_BGP_ES_INVALID
,
1392 "%u ERROR: Failed to updated ES route ESI: %s VTEP %s",
1394 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1395 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1399 attr_new
= ri
->attr
;
1401 /* Perform route selection;
1402 * this is just to set the flags correctly
1403 * as local route in the ES always wins.
1405 evpn_es_route_select_install(bgp
, es
, rn
);
1406 bgp_unlock_node(rn
);
1408 /* If this is a new route or some attribute has changed, export the
1409 * route to the global table. The route will be advertised to peers
1410 * from there. Note that this table is a 2-level tree (RD-level +
1411 * Prefix-level) similar to L3VPN routes.
1413 if (route_changed
) {
1414 struct bgp_info
*global_ri
;
1416 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1417 (struct prefix
*)p
, &es
->prd
);
1418 update_evpn_type4_route_entry(bgp
, es
, afi
, safi
,
1423 /* Schedule for processing and unlock node. */
1424 bgp_process(bgp
, rn
, afi
, safi
);
1425 bgp_unlock_node(rn
);
1428 /* Unintern temporary. */
1429 aspath_unintern(&attr
.aspath
);
1433 static int update_evpn_type5_route_entry(struct bgp
*bgp_def
,
1434 struct bgp
*bgp_vrf
, afi_t afi
,
1435 safi_t safi
, struct bgp_node
*rn
,
1436 struct attr
*attr
, int *route_changed
)
1438 struct attr
*attr_new
= NULL
;
1439 struct bgp_info
*ri
= NULL
;
1440 mpls_label_t label
= MPLS_INVALID_LABEL
;
1441 struct bgp_info
*local_ri
= NULL
;
1442 struct bgp_info
*tmp_ri
= NULL
;
1445 /* locate the local route entry if any */
1446 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1447 if (tmp_ri
->peer
== bgp_def
->peer_self
1448 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1449 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1454 * create a new route entry if one doesnt exist.
1455 * Otherwise see if route attr has changed
1459 /* route has changed as this is the first entry */
1462 /* Add (or update) attribute to hash. */
1463 attr_new
= bgp_attr_intern(attr
);
1465 /* create the route info from attribute */
1466 ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1467 bgp_def
->peer_self
, attr_new
, rn
);
1468 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1470 /* Type-5 routes advertise the L3-VNI */
1471 bgp_info_extra_get(ri
);
1472 vni2label(bgp_vrf
->l3vni
, &label
);
1473 memcpy(&ri
->extra
->label
, &label
, sizeof(label
));
1474 ri
->extra
->num_labels
= 1;
1476 /* add the route entry to route node*/
1477 bgp_info_add(rn
, ri
);
1481 if (!attrhash_cmp(tmp_ri
->attr
, attr
)) {
1483 /* attribute changed */
1486 /* The attribute has changed. */
1487 /* Add (or update) attribute to hash. */
1488 attr_new
= bgp_attr_intern(attr
);
1489 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1491 /* Restore route, if needed. */
1492 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1493 bgp_info_restore(rn
, tmp_ri
);
1495 /* Unintern existing, set to new. */
1496 bgp_attr_unintern(&tmp_ri
->attr
);
1497 tmp_ri
->attr
= attr_new
;
1498 tmp_ri
->uptime
= bgp_clock();
1504 /* update evpn type-5 route entry */
1505 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1506 struct attr
*src_attr
)
1508 afi_t afi
= AFI_L2VPN
;
1509 safi_t safi
= SAFI_EVPN
;
1511 struct bgp_node
*rn
= NULL
;
1512 struct bgp
*bgp_def
= NULL
;
1513 int route_changed
= 0;
1515 bgp_def
= bgp_get_default();
1519 /* Build path attribute for this route - use the source attr, if
1520 * present, else treat as locally originated.
1523 bgp_attr_dup(&attr
, src_attr
);
1525 memset(&attr
, 0, sizeof(struct attr
));
1526 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1528 /* Set nexthop to ourselves and fill in the Router MAC. */
1529 attr
.nexthop
= bgp_vrf
->originator_ip
;
1530 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1531 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1532 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
1534 /* Setup RT and encap extended community */
1535 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1537 /* get the route node in global table */
1538 rn
= bgp_afi_node_get(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1539 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1542 /* create or update the route entry within the route node */
1543 update_evpn_type5_route_entry(bgp_def
, bgp_vrf
, afi
, safi
, rn
, &attr
,
1546 /* schedule for processing and unlock node */
1547 if (route_changed
) {
1548 bgp_process(bgp_def
, rn
, afi
, safi
);
1549 bgp_unlock_node(rn
);
1552 /* uninten temporary */
1554 aspath_unintern(&attr
.aspath
);
1559 * Create or update EVPN route entry. This could be in the VNI route table
1560 * or the global route table.
1562 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1563 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1564 struct attr
*attr
, int add
,
1565 struct bgp_info
**ri
, uint8_t flags
,
1568 struct bgp_info
*tmp_ri
;
1569 struct bgp_info
*local_ri
;
1570 struct attr
*attr_new
;
1571 mpls_label_t label
[BGP_MAX_LABELS
];
1572 uint32_t num_labels
= 1;
1573 int route_change
= 1;
1575 struct prefix_evpn
*evp
;
1578 evp
= (struct prefix_evpn
*)&rn
->p
;
1579 memset(&label
, 0, sizeof(label
));
1581 /* See if this is an update of an existing route, or a new add. */
1583 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1584 if (tmp_ri
->peer
== bgp
->peer_self
1585 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1586 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1590 /* If route doesn't exist already, create a new one, if told to.
1591 * Otherwise act based on whether the attributes of the route have
1594 if (!local_ri
&& !add
)
1597 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1598 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1599 add_mac_mobility_to_attr(seq
, attr
);
1602 /* Add (or update) attribute to hash. */
1603 attr_new
= bgp_attr_intern(attr
);
1605 /* Extract MAC mobility sequence number, if any. */
1606 attr_new
->mm_seqnum
=
1607 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1608 attr_new
->sticky
= sticky
;
1610 /* Create new route with its attribute. */
1611 tmp_ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1612 bgp
->peer_self
, attr_new
, rn
);
1613 SET_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
);
1614 bgp_info_extra_get(tmp_ri
);
1616 /* The VNI goes into the 'label' field of the route */
1617 vni2label(vpn
->vni
, &label
[0]);
1619 /* Type-2 routes may carry a second VNI - the L3-VNI.
1620 * Only attach second label if we are advertising two labels for
1623 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1624 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1627 l3vni
= bgpevpn_get_l3vni(vpn
);
1629 vni2label(l3vni
, &label
[1]);
1634 memcpy(&tmp_ri
->extra
->label
, label
, sizeof(label
));
1635 tmp_ri
->extra
->num_labels
= num_labels
;
1636 bgp_info_add(rn
, tmp_ri
);
1639 if (attrhash_cmp(tmp_ri
->attr
, attr
)
1640 && !CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1644 * The attributes have changed, type-2 routes needs to
1645 * be advertised with right labels.
1647 vni2label(vpn
->vni
, &label
[0]);
1648 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1649 && CHECK_FLAG(vpn
->flags
,
1650 VNI_FLAG_USE_TWO_LABELS
)) {
1653 l3vni
= bgpevpn_get_l3vni(vpn
);
1655 vni2label(l3vni
, &label
[1]);
1659 memcpy(&tmp_ri
->extra
->label
, label
, sizeof(label
));
1660 tmp_ri
->extra
->num_labels
= num_labels
;
1662 /* The attribute has changed. */
1663 /* Add (or update) attribute to hash. */
1664 attr_new
= bgp_attr_intern(attr
);
1665 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1667 /* Extract MAC mobility sequence number, if any. */
1668 attr_new
->mm_seqnum
=
1669 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1670 attr_new
->sticky
= sticky
;
1672 /* Restore route, if needed. */
1673 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1674 bgp_info_restore(rn
, tmp_ri
);
1676 /* Unintern existing, set to new. */
1677 bgp_attr_unintern(&tmp_ri
->attr
);
1678 tmp_ri
->attr
= attr_new
;
1679 tmp_ri
->uptime
= bgp_clock();
1683 /* Return back the route entry. */
1685 return route_change
;
1689 * Create or update EVPN route (of type based on prefix) for specified VNI
1690 * and schedule for processing.
1692 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1693 struct prefix_evpn
*p
, uint8_t flags
,
1696 struct bgp_node
*rn
;
1698 struct attr
*attr_new
;
1699 int add_l3_ecomm
= 0;
1700 struct bgp_info
*ri
;
1701 afi_t afi
= AFI_L2VPN
;
1702 safi_t safi
= SAFI_EVPN
;
1705 memset(&attr
, 0, sizeof(struct attr
));
1707 /* Build path-attribute for this route. */
1708 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1709 attr
.nexthop
= vpn
->originator_ip
;
1710 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1711 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1712 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1713 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1714 attr
.router_flag
= CHECK_FLAG(flags
,
1715 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1717 /* PMSI is only needed for type-3 routes */
1718 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
)
1719 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1721 /* router mac is only needed for type-2 routes here. */
1722 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1723 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1724 vni2label(vpn
->vni
, &(attr
.label
));
1726 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1727 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1730 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1731 (is_evpn_prefix_ipaddr_v4(p
) ||
1732 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1733 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1734 bgpevpn_get_l3vni(vpn
))
1737 /* Set up extended community. */
1738 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1740 /* First, create (or fetch) route node within the VNI. */
1741 /* NOTE: There is no RD here. */
1742 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1744 /* Create or update route entry. */
1745 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1746 1, &ri
, flags
, seq
);
1748 attr_new
= ri
->attr
;
1750 /* Perform route selection; this is just to set the flags correctly
1751 * as local route in the VNI always wins.
1753 evpn_route_select_install(bgp
, vpn
, rn
);
1754 bgp_unlock_node(rn
);
1756 /* If this is a new route or some attribute has changed, export the
1757 * route to the global table. The route will be advertised to peers
1758 * from there. Note that this table is a 2-level tree (RD-level +
1759 * Prefix-level) similar to L3VPN routes.
1762 struct bgp_info
*global_ri
;
1764 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1765 (struct prefix
*)p
, &vpn
->prd
);
1766 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1,
1767 &global_ri
, flags
, seq
);
1769 /* Schedule for processing and unlock node. */
1770 bgp_process(bgp
, rn
, afi
, safi
);
1771 bgp_unlock_node(rn
);
1774 /* Unintern temporary. */
1775 aspath_unintern(&attr
.aspath
);
1781 * Delete EVPN route entry.
1782 * The entry can be in ESI/VNI table or the global table.
1784 static void delete_evpn_route_entry(struct bgp
*bgp
,
1785 afi_t afi
, safi_t safi
,
1786 struct bgp_node
*rn
,
1787 struct bgp_info
**ri
)
1789 struct bgp_info
*tmp_ri
;
1793 /* Now, find matching route. */
1794 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
)
1795 if (tmp_ri
->peer
== bgp
->peer_self
1796 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1797 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1802 /* Mark route for delete. */
1804 bgp_info_delete(rn
, tmp_ri
);
1809 /* Delete EVPN ES (type-4) route */
1810 static int delete_evpn_type4_route(struct bgp
*bgp
,
1812 struct prefix_evpn
*p
)
1814 afi_t afi
= AFI_L2VPN
;
1815 safi_t safi
= SAFI_EVPN
;
1816 struct bgp_info
*ri
;
1817 struct bgp_node
*rn
= NULL
; /* rn in esi table */
1818 struct bgp_node
*global_rn
= NULL
; /* rn in global table */
1820 /* First, locate the route node within the ESI.
1821 * If it doesn't exist, ther is nothing to do.
1822 * Note: there is no RD here.
1824 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
1828 /* Next, locate route node in the global EVPN routing table.
1829 * Note that this table is a 2-level tree (RD-level + Prefix-level)
1831 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1832 (struct prefix
*)p
, &es
->prd
);
1835 /* Delete route entry in the global EVPN table. */
1836 delete_evpn_route_entry(bgp
, afi
, safi
,
1839 /* Schedule for processing - withdraws to peers happen from
1843 bgp_process(bgp
, global_rn
, afi
, safi
);
1844 bgp_unlock_node(global_rn
);
1848 * Delete route entry in the ESI route table.
1849 * This can just be removed.
1851 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &ri
);
1853 bgp_info_reap(rn
, ri
);
1854 bgp_unlock_node(rn
);
1858 /* Delete EVPN type5 route */
1859 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1861 afi_t afi
= AFI_L2VPN
;
1862 safi_t safi
= SAFI_EVPN
;
1863 struct bgp_node
*rn
= NULL
;
1864 struct bgp_info
*ri
= NULL
;
1865 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
1867 bgp_def
= bgp_get_default();
1871 /* locate the global route entry for this type-5 prefix */
1872 rn
= bgp_afi_node_lookup(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1873 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1877 delete_evpn_route_entry(bgp_def
, afi
, safi
, rn
, &ri
);
1879 bgp_process(bgp_def
, rn
, afi
, safi
);
1880 bgp_unlock_node(rn
);
1885 * Delete EVPN route (of type based on prefix) for specified VNI and
1886 * schedule for processing.
1888 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1889 struct prefix_evpn
*p
)
1891 struct bgp_node
*rn
, *global_rn
;
1892 struct bgp_info
*ri
;
1893 afi_t afi
= AFI_L2VPN
;
1894 safi_t safi
= SAFI_EVPN
;
1896 /* First, locate the route node within the VNI. If it doesn't exist,
1898 * is nothing further to do.
1900 /* NOTE: There is no RD here. */
1901 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1905 /* Next, locate route node in the global EVPN routing table. Note that
1906 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1909 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1910 (struct prefix
*)p
, &vpn
->prd
);
1912 /* Delete route entry in the global EVPN table. */
1913 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &ri
);
1915 /* Schedule for processing - withdraws to peers happen from
1919 bgp_process(bgp
, global_rn
, afi
, safi
);
1920 bgp_unlock_node(global_rn
);
1923 /* Delete route entry in the VNI route table. This can just be removed.
1925 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &ri
);
1927 bgp_info_reap(rn
, ri
);
1928 bgp_unlock_node(rn
);
1934 * Update all type-2 (MACIP) local routes for this VNI - these should also
1935 * be scheduled for advertise to peers.
1937 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1941 struct bgp_node
*rn
;
1942 struct bgp_info
*ri
, *tmp_ri
;
1944 struct attr
*attr_new
;
1946 int add_l3_ecomm
= 0;
1951 /* Walk this VNI's route table and update local type-2 routes. For any
1952 * routes updated, update corresponding entry in the global table too.
1954 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1955 rn
= bgp_route_next(rn
)) {
1956 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1957 struct bgp_node
*rd_rn
;
1958 struct bgp_info
*global_ri
;
1960 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1963 /* Identify local route. */
1964 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1965 if (tmp_ri
->peer
== bgp
->peer_self
1966 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1967 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1975 * Build attribute per local route as the MAC mobility and
1976 * some other values could differ for different routes. The
1977 * attributes will be shared in the hash table.
1979 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1980 attr
.nexthop
= vpn
->originator_ip
;
1981 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1982 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1983 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1985 if (evpn_route_is_sticky(bgp
, rn
))
1987 else if (evpn_route_is_def_gw(bgp
, rn
)) {
1988 attr
.default_gw
= 1;
1989 if (is_evpn_prefix_ipaddr_v6(evp
))
1990 attr
.router_flag
= 1;
1993 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
1994 * using L3 VNI for type-2 routes also.
1996 if ((is_evpn_prefix_ipaddr_v4(evp
) ||
1997 !IN6_IS_ADDR_LINKLOCAL(
1998 &evp
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1999 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
2000 bgpevpn_get_l3vni(vpn
))
2003 /* Set up extended community. */
2004 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2006 seq
= mac_mobility_seqnum(tmp_ri
->attr
);
2008 /* Update the route entry. */
2009 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
2010 &attr
, 0, &ri
, 0, seq
);
2012 /* Perform route selection; this is just to set the flags
2013 * correctly as local route in the VNI always wins.
2015 evpn_route_select_install(bgp
, vpn
, rn
);
2017 attr_new
= ri
->attr
;
2019 /* Update route in global routing table. */
2020 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2021 (struct prefix
*)evp
, &vpn
->prd
);
2023 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
2025 mac_mobility_seqnum(attr_new
));
2027 /* Schedule for processing and unlock node. */
2028 bgp_process(bgp
, rd_rn
, afi
, safi
);
2029 bgp_unlock_node(rd_rn
);
2031 /* Unintern temporary. */
2032 aspath_unintern(&attr
.aspath
);
2040 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2041 * global routing table. These are also scheduled for withdraw from peers.
2043 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2047 struct bgp_node
*rdrn
, *rn
;
2048 struct bgp_table
*table
;
2049 struct bgp_info
*ri
;
2054 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
2055 if (rdrn
&& rdrn
->info
) {
2056 table
= (struct bgp_table
*)rdrn
->info
;
2057 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2058 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2060 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2063 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &ri
);
2065 bgp_process(bgp
, rn
, afi
, safi
);
2069 /* Unlock RD node. */
2071 bgp_unlock_node(rdrn
);
2077 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2078 * table as well as the per-VNI route table.
2080 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2084 struct bgp_node
*rn
;
2085 struct bgp_info
*ri
;
2090 /* First, walk the global route table for this VNI's type-2 local
2092 * EVPN routes are a 2-level table, first get the RD table.
2094 delete_global_type2_routes(bgp
, vpn
);
2096 /* Next, walk this VNI's route table and delete local type-2 routes. */
2097 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2098 rn
= bgp_route_next(rn
)) {
2099 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2101 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2104 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &ri
);
2106 /* Route entry in local table gets deleted immediately. */
2108 bgp_info_reap(rn
, ri
);
2115 * Delete all routes in per ES route-table
2117 static int delete_all_es_routes(struct bgp
*bgp
, struct evpnes
*es
)
2119 struct bgp_node
*rn
;
2120 struct bgp_info
*ri
, *nextri
;
2122 /* Walk this ES's route table and delete all routes. */
2123 for (rn
= bgp_table_top(es
->route_table
); rn
;
2124 rn
= bgp_route_next(rn
)) {
2125 for (ri
= rn
->info
; (ri
!= NULL
) && (nextri
= ri
->next
, 1);
2127 bgp_info_delete(rn
, ri
);
2128 bgp_info_reap(rn
, ri
);
2136 * Delete all routes in the per-VNI route table.
2138 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2140 struct bgp_node
*rn
;
2141 struct bgp_info
*ri
, *nextri
;
2143 /* Walk this VNI's route table and delete all routes. */
2144 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2145 rn
= bgp_route_next(rn
)) {
2146 for (ri
= rn
->info
; (ri
!= NULL
) && (nextri
= ri
->next
, 1);
2148 bgp_info_delete(rn
, ri
);
2149 bgp_info_reap(rn
, ri
);
2157 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2158 * export RT getting modified or change to tunnel IP. Note that these
2159 * situations need the route in the per-VNI table as well as the global
2160 * table to be updated (as attributes change).
2162 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2165 struct prefix_evpn p
;
2167 /* Update and advertise the type-3 route (only one) followed by the
2168 * locally learnt type-2 routes (MACIP) - for this VNI.
2170 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2171 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0);
2175 return update_all_type2_routes(bgp
, vpn
);
2178 /* Delete (and withdraw) local routes for specified ES from global and ES table.
2179 * Also remove all other routes from the per ES table.
2180 * Invoked when ES is deleted.
2182 static int delete_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
2185 char buf
[ESI_STR_LEN
];
2186 struct prefix_evpn p
;
2188 /* Delete and withdraw locally learnt ES route */
2189 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
.ipaddr_v4
);
2190 ret
= delete_evpn_type4_route(bgp
, es
, &p
);
2192 flog_err(EC_BGP_EVPN_ROUTE_DELETE
,
2193 "%u failed to delete type-4 route for ESI %s",
2194 bgp
->vrf_id
, esi_to_str(&es
->esi
, buf
, sizeof(buf
)));
2197 /* Delete all routes from per ES table */
2198 return delete_all_es_routes(bgp
, es
);
2202 * Delete (and withdraw) local routes for specified VNI from the global
2203 * table and per-VNI table. After this, remove all other routes from
2204 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2205 * (advertise-all-vni) being disabled.
2207 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2210 struct prefix_evpn p
;
2212 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2213 * followed by type-3 routes (only one) - for this VNI.
2215 ret
= delete_all_type2_routes(bgp
, vpn
);
2219 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2220 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2224 /* Delete all routes from the per-VNI table. */
2225 return delete_all_vni_routes(bgp
, vpn
);
2229 * There is a tunnel endpoint IP address change for this VNI, delete
2230 * prior type-3 route (if needed) and update.
2231 * Note: Route re-advertisement happens elsewhere after other processing
2234 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2235 struct in_addr originator_ip
)
2237 struct prefix_evpn p
;
2239 /* If VNI is not live, we only need to update the originator ip */
2240 if (!is_vni_live(vpn
)) {
2241 vpn
->originator_ip
= originator_ip
;
2245 /* Update the tunnel-ip hash */
2246 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2247 bgp_tip_add(bgp
, &originator_ip
);
2249 /* filter routes as martian nexthop db has changed */
2250 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2252 /* Need to withdraw type-3 route as the originator IP is part
2255 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2256 delete_evpn_route(bgp
, vpn
, &p
);
2258 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2259 vpn
->originator_ip
= originator_ip
;
2263 /* Install EVPN route entry in ES */
2264 static int install_evpn_route_entry_in_es(struct bgp
*bgp
,
2266 struct prefix_evpn
*p
,
2267 struct bgp_info
*parent_ri
)
2270 struct bgp_node
*rn
= NULL
;
2271 struct bgp_info
*ri
= NULL
;
2272 struct attr
*attr_new
= NULL
;
2274 /* Create (or fetch) route within the VNI.
2275 * NOTE: There is no RD here.
2277 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
2279 /* Check if route entry is already present. */
2280 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2282 (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2286 /* Add (or update) attribute to hash. */
2287 attr_new
= bgp_attr_intern(parent_ri
->attr
);
2289 /* Create new route with its attribute. */
2290 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
2291 parent_ri
->peer
, attr_new
, rn
);
2292 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
2293 bgp_info_extra_get(ri
);
2294 ri
->extra
->parent
= parent_ri
;
2295 bgp_info_add(rn
, ri
);
2297 if (attrhash_cmp(ri
->attr
, parent_ri
->attr
)
2298 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
2299 bgp_unlock_node(rn
);
2302 /* The attribute has changed. */
2303 /* Add (or update) attribute to hash. */
2304 attr_new
= bgp_attr_intern(parent_ri
->attr
);
2306 /* Restore route, if needed. */
2307 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
2308 bgp_info_restore(rn
, ri
);
2310 /* Mark if nexthop has changed. */
2311 if (!IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
))
2312 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
2314 /* Unintern existing, set to new. */
2315 bgp_attr_unintern(&ri
->attr
);
2316 ri
->attr
= attr_new
;
2317 ri
->uptime
= bgp_clock();
2320 /* Perform route selection and update zebra, if required. */
2321 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2326 * Install route entry into the VRF routing table and invoke route selection.
2328 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2329 struct prefix_evpn
*evp
,
2330 struct bgp_info
*parent_ri
)
2332 struct bgp_node
*rn
;
2333 struct bgp_info
*ri
;
2335 struct attr
*attr_new
;
2338 struct prefix
*pp
= &p
;
2341 char buf
[PREFIX_STRLEN
];
2342 char buf1
[PREFIX_STRLEN
];
2344 memset(pp
, 0, sizeof(struct prefix
));
2345 ip_prefix_from_evpn_prefix(evp
, pp
);
2347 if (bgp_debug_zebra(NULL
)) {
2349 "installing evpn prefix %s as ip prefix %s in vrf %s",
2350 prefix2str(evp
, buf
, sizeof(buf
)),
2351 prefix2str(pp
, buf1
, sizeof(buf
)),
2352 vrf_id_to_name(bgp_vrf
->vrf_id
));
2355 /* Create (or fetch) route within the VRF. */
2356 /* NOTE: There is no RD here. */
2357 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2359 safi
= SAFI_UNICAST
;
2360 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2361 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2363 safi
= SAFI_UNICAST
;
2364 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2368 /* EVPN routes currently only support a IPv4 next hop which corresponds
2369 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2370 * or prefix route, we have to convert the next hop to an IPv4-mapped
2371 * address for the rest of the code to flow through. In the case of IPv4,
2372 * make sure to set the flag for next hop attribute.
2374 bgp_attr_dup(&attr
, parent_ri
->attr
);
2376 evpn_convert_nexthop_to_ipv6(&attr
);
2378 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2380 /* Check if route entry is already present. */
2381 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2383 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2387 /* Add (or update) attribute to hash. */
2388 attr_new
= bgp_attr_intern(&attr
);
2390 /* Create new route with its attribute. */
2391 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
2392 parent_ri
->peer
, attr_new
, rn
);
2393 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
2394 bgp_info_extra_get(ri
);
2395 ri
->extra
->parent
= bgp_info_lock(parent_ri
);
2396 bgp_lock_node((struct bgp_node
*)parent_ri
->net
);
2397 if (parent_ri
->extra
) {
2398 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
2399 sizeof(ri
->extra
->label
));
2400 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
2402 bgp_info_add(rn
, ri
);
2404 if (attrhash_cmp(ri
->attr
, &attr
)
2405 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
2406 bgp_unlock_node(rn
);
2409 /* The attribute has changed. */
2410 /* Add (or update) attribute to hash. */
2411 attr_new
= bgp_attr_intern(&attr
);
2413 /* Restore route, if needed. */
2414 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
2415 bgp_info_restore(rn
, ri
);
2417 /* Mark if nexthop has changed. */
2418 if ((afi
== AFI_IP
&&
2419 !IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
)) ||
2421 !IPV6_ADDR_SAME(&ri
->attr
->mp_nexthop_global
,
2422 &attr_new
->mp_nexthop_global
)))
2423 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
2425 /* Unintern existing, set to new. */
2426 bgp_attr_unintern(&ri
->attr
);
2427 ri
->attr
= attr_new
;
2428 ri
->uptime
= bgp_clock();
2431 bgp_aggregate_increment(bgp_vrf
, &rn
->p
, ri
, afi
, safi
);
2433 /* Perform route selection and update zebra, if required. */
2434 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2440 * Install route entry into the VNI routing table and invoke route selection.
2442 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2443 struct prefix_evpn
*p
,
2444 struct bgp_info
*parent_ri
)
2446 struct bgp_node
*rn
;
2447 struct bgp_info
*ri
;
2448 struct attr
*attr_new
;
2451 /* Create (or fetch) route within the VNI. */
2452 /* NOTE: There is no RD here. */
2453 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2455 /* Check if route entry is already present. */
2456 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2458 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2462 /* Add (or update) attribute to hash. */
2463 attr_new
= bgp_attr_intern(parent_ri
->attr
);
2465 /* Create new route with its attribute. */
2466 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
2467 parent_ri
->peer
, attr_new
, rn
);
2468 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
2469 bgp_info_extra_get(ri
);
2470 ri
->extra
->parent
= bgp_info_lock(parent_ri
);
2471 bgp_lock_node((struct bgp_node
*)parent_ri
->net
);
2472 if (parent_ri
->extra
) {
2473 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
2474 sizeof(ri
->extra
->label
));
2475 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
2477 bgp_info_add(rn
, ri
);
2479 if (attrhash_cmp(ri
->attr
, parent_ri
->attr
)
2480 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
2481 bgp_unlock_node(rn
);
2484 /* The attribute has changed. */
2485 /* Add (or update) attribute to hash. */
2486 attr_new
= bgp_attr_intern(parent_ri
->attr
);
2488 /* Restore route, if needed. */
2489 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
2490 bgp_info_restore(rn
, ri
);
2492 /* Mark if nexthop has changed. */
2493 if (!IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
))
2494 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
2496 /* Unintern existing, set to new. */
2497 bgp_attr_unintern(&ri
->attr
);
2498 ri
->attr
= attr_new
;
2499 ri
->uptime
= bgp_clock();
2502 /* Perform route selection and update zebra, if required. */
2503 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2508 /* Uninstall EVPN route entry from ES route table */
2509 static int uninstall_evpn_route_entry_in_es(struct bgp
*bgp
,
2511 struct prefix_evpn
*p
,
2512 struct bgp_info
*parent_ri
)
2515 struct bgp_node
*rn
;
2516 struct bgp_info
*ri
;
2518 if (!es
->route_table
)
2521 /* Locate route within the ESI.
2522 * NOTE: There is no RD here.
2524 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2528 /* Find matching route entry. */
2529 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2531 (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2537 /* Mark entry for deletion */
2538 bgp_info_delete(rn
, ri
);
2540 /* Perform route selection and update zebra, if required. */
2541 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2543 /* Unlock route node. */
2544 bgp_unlock_node(rn
);
2550 * Uninstall route entry from the VRF routing table and send message
2551 * to zebra, if appropriate.
2553 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2554 struct prefix_evpn
*evp
,
2555 struct bgp_info
*parent_ri
)
2557 struct bgp_node
*rn
;
2558 struct bgp_info
*ri
;
2561 struct prefix
*pp
= &p
;
2564 char buf
[PREFIX_STRLEN
];
2565 char buf1
[PREFIX_STRLEN
];
2567 memset(pp
, 0, sizeof(struct prefix
));
2568 ip_prefix_from_evpn_prefix(evp
, pp
);
2570 if (bgp_debug_zebra(NULL
)) {
2572 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2573 prefix2str(evp
, buf
, sizeof(buf
)),
2574 prefix2str(pp
, buf1
, sizeof(buf
)),
2575 vrf_id_to_name(bgp_vrf
->vrf_id
));
2578 /* Locate route within the VRF. */
2579 /* NOTE: There is no RD here. */
2580 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2582 safi
= SAFI_UNICAST
;
2583 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2586 safi
= SAFI_UNICAST
;
2587 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2593 /* Find matching route entry. */
2594 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2596 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2602 bgp_aggregate_decrement(bgp_vrf
, &rn
->p
, ri
, afi
, safi
);
2604 /* Mark entry for deletion */
2605 bgp_info_delete(rn
, ri
);
2607 /* Perform route selection and update zebra, if required. */
2608 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2610 /* Unlock route node. */
2611 bgp_unlock_node(rn
);
2617 * Uninstall route entry from the VNI routing table and send message
2618 * to zebra, if appropriate.
2620 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2621 struct prefix_evpn
*p
,
2622 struct bgp_info
*parent_ri
)
2624 struct bgp_node
*rn
;
2625 struct bgp_info
*ri
;
2628 /* Locate route within the VNI. */
2629 /* NOTE: There is no RD here. */
2630 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2634 /* Find matching route entry. */
2635 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2637 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2643 /* Mark entry for deletion */
2644 bgp_info_delete(rn
, ri
);
2646 /* Perform route selection and update zebra, if required. */
2647 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2649 /* Unlock route node. */
2650 bgp_unlock_node(rn
);
2656 * Given a prefix, see if it belongs to ES.
2658 static int is_prefix_matching_for_es(struct prefix_evpn
*p
,
2661 /* if not an ES route return false */
2662 if (p
->prefix
.route_type
!= BGP_EVPN_ES_ROUTE
)
2665 if (memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
)) == 0)
2672 * Given a route entry and a VRF, see if this route entry should be
2673 * imported into the VRF i.e., RTs match.
2675 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
, struct bgp_info
*ri
)
2677 struct attr
*attr
= ri
->attr
;
2678 struct ecommunity
*ecom
;
2682 /* Route should have valid RT to be even considered. */
2683 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2686 ecom
= attr
->ecommunity
;
2687 if (!ecom
|| !ecom
->size
)
2690 /* For each extended community RT, see if it matches this VNI. If any RT
2691 * matches, we're done.
2693 for (i
= 0; i
< ecom
->size
; i
++) {
2695 uint8_t type
, sub_type
;
2696 struct ecommunity_val
*eval
;
2697 struct ecommunity_val eval_tmp
;
2698 struct vrf_irt_node
*irt
;
2700 /* Only deal with RTs */
2701 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2702 eval
= (struct ecommunity_val
*)(ecom
->val
2703 + (i
* ECOMMUNITY_SIZE
));
2706 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2709 /* See if this RT matches specified VNIs import RTs */
2710 irt
= lookup_vrf_import_rt(eval
);
2712 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2715 /* Also check for non-exact match. In this, we mask out the AS
2717 * only check on the local-admin sub-field. This is to
2719 * VNI as the RT for EBGP peering too.
2722 if (type
== ECOMMUNITY_ENCODE_AS
2723 || type
== ECOMMUNITY_ENCODE_AS4
2724 || type
== ECOMMUNITY_ENCODE_IP
) {
2725 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2726 mask_ecom_global_admin(&eval_tmp
, eval
);
2727 irt
= lookup_vrf_import_rt(&eval_tmp
);
2730 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2738 * Given a route entry and a VNI, see if this route entry should be
2739 * imported into the VNI i.e., RTs match.
2741 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2742 struct bgp_info
*ri
)
2744 struct attr
*attr
= ri
->attr
;
2745 struct ecommunity
*ecom
;
2749 /* Route should have valid RT to be even considered. */
2750 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2753 ecom
= attr
->ecommunity
;
2754 if (!ecom
|| !ecom
->size
)
2757 /* For each extended community RT, see if it matches this VNI. If any RT
2758 * matches, we're done.
2760 for (i
= 0; i
< ecom
->size
; i
++) {
2762 uint8_t type
, sub_type
;
2763 struct ecommunity_val
*eval
;
2764 struct ecommunity_val eval_tmp
;
2765 struct irt_node
*irt
;
2767 /* Only deal with RTs */
2768 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2769 eval
= (struct ecommunity_val
*)(ecom
->val
2770 + (i
* ECOMMUNITY_SIZE
));
2773 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2776 /* See if this RT matches specified VNIs import RTs */
2777 irt
= lookup_import_rt(bgp
, eval
);
2779 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2782 /* Also check for non-exact match. In this, we mask out the AS
2784 * only check on the local-admin sub-field. This is to
2786 * VNI as the RT for EBGP peering too.
2789 if (type
== ECOMMUNITY_ENCODE_AS
2790 || type
== ECOMMUNITY_ENCODE_AS4
2791 || type
== ECOMMUNITY_ENCODE_IP
) {
2792 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2793 mask_ecom_global_admin(&eval_tmp
, eval
);
2794 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2797 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2804 static int install_uninstall_routes_for_es(struct bgp
*bgp
,
2811 char buf
[PREFIX_STRLEN
];
2812 char buf1
[ESI_STR_LEN
];
2813 struct bgp_node
*rd_rn
, *rn
;
2814 struct bgp_table
*table
;
2815 struct bgp_info
*ri
;
2821 * Walk entire global routing table and evaluate routes which could be
2822 * imported into this VRF. Note that we need to loop through all global
2823 * routes to determine which route matches the import rt on vrf
2825 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2826 rd_rn
= bgp_route_next(rd_rn
)) {
2827 table
= (struct bgp_table
*)(rd_rn
->info
);
2831 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2832 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2834 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2836 * Consider "valid" remote routes applicable for
2839 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2840 && ri
->type
== ZEBRA_ROUTE_BGP
2841 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2844 if (!is_prefix_matching_for_es(evp
, es
))
2848 ret
= install_evpn_route_entry_in_es(
2851 ret
= uninstall_evpn_route_entry_in_es(
2857 "Failed to %s EVPN %s route in ESI %s",
2860 prefix2str(evp
, buf
,
2862 esi_to_str(&es
->esi
, buf1
,
2873 * Install or uninstall mac-ip routes are appropriate for this
2876 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
2880 struct bgp_node
*rd_rn
, *rn
;
2881 struct bgp_table
*table
;
2882 struct bgp_info
*ri
;
2884 char buf
[PREFIX_STRLEN
];
2885 struct bgp
*bgp_def
= NULL
;
2889 bgp_def
= bgp_get_default();
2893 /* Walk entire global routing table and evaluate routes which could be
2894 * imported into this VRF. Note that we need to loop through all global
2895 * routes to determine which route matches the import rt on vrf
2897 for (rd_rn
= bgp_table_top(bgp_def
->rib
[afi
][safi
]); rd_rn
;
2898 rd_rn
= bgp_route_next(rd_rn
)) {
2899 table
= (struct bgp_table
*)(rd_rn
->info
);
2903 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2904 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2906 /* if not mac-ip route skip this route */
2907 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2908 || evp
->prefix
.route_type
2909 == BGP_EVPN_IP_PREFIX_ROUTE
))
2912 /* if not a mac+ip route skip this route */
2913 if (!(is_evpn_prefix_ipaddr_v4(evp
)
2914 || is_evpn_prefix_ipaddr_v6(evp
)))
2917 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2918 /* Consider "valid" remote routes applicable for
2921 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2922 && ri
->type
== ZEBRA_ROUTE_BGP
2923 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2926 if (is_route_matching_for_vrf(bgp_vrf
, ri
)) {
2928 ret
= install_evpn_route_entry_in_vrf(
2931 ret
= uninstall_evpn_route_entry_in_vrf(
2937 "Failed to %s EVPN %s route in VRF %s",
2940 prefix2str(evp
, buf
,
2955 * Install or uninstall routes of specified type that are appropriate for this
2958 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
2959 struct bgpevpn
*vpn
,
2960 bgp_evpn_route_type rtype
,
2965 struct bgp_node
*rd_rn
, *rn
;
2966 struct bgp_table
*table
;
2967 struct bgp_info
*ri
;
2973 /* Walk entire global routing table and evaluate routes which could be
2974 * imported into this VPN. Note that we cannot just look at the routes
2976 * the VNI's RD - remote routes applicable for this VNI could have any
2979 /* EVPN routes are a 2-level table. */
2980 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2981 rd_rn
= bgp_route_next(rd_rn
)) {
2982 table
= (struct bgp_table
*)(rd_rn
->info
);
2986 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2987 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2989 if (evp
->prefix
.route_type
!= rtype
)
2992 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2993 /* Consider "valid" remote routes applicable for
2995 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2996 && ri
->type
== ZEBRA_ROUTE_BGP
2997 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
3000 if (is_route_matching_for_vni(bgp
, vpn
, ri
)) {
3002 ret
= install_evpn_route_entry(
3005 ret
= uninstall_evpn_route_entry(
3011 "%u: Failed to %s EVPN %s route in VNI %u",
3015 rtype
== BGP_EVPN_MAC_IP_ROUTE
3029 /* Install any existing remote ES routes applicable for this ES into its routing
3030 * table. This is invoked when ES comes up.
3032 static int install_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
3034 return install_uninstall_routes_for_es(bgp
, es
, 1);
3038 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3039 * is invoked upon l3vni-add or l3vni import rt change
3041 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3043 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3048 * Install any existing remote routes applicable for this VNI into its
3049 * routing table. This is invoked when a VNI becomes "live" or its Import
3052 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3056 /* Install type-3 routes followed by type-2 routes - the ones applicable
3059 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3064 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3068 /* uninstall routes from l3vni vrf. */
3069 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3071 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3076 * Uninstall any existing remote routes for this VNI. One scenario in which
3077 * this is invoked is upon an import RT change.
3079 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3083 /* Uninstall type-2 routes followed by type-3 routes - the ones
3087 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3092 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3096 /* Install or unistall route in ES */
3097 static int install_uninstall_route_in_es(struct bgp
*bgp
, struct evpnes
*es
,
3098 afi_t afi
, safi_t safi
,
3099 struct prefix_evpn
*evp
,
3100 struct bgp_info
*ri
,
3104 char buf
[ESI_STR_LEN
];
3107 ret
= install_evpn_route_entry_in_es(bgp
, es
, evp
, ri
);
3109 ret
= uninstall_evpn_route_entry_in_es(bgp
, es
, evp
, ri
);
3114 "%u: Failed to %s EVPN %s route in ESI %s", bgp
->vrf_id
,
3115 install
? "install" : "uninstall", "ES",
3116 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)));
3123 * Install or uninstall route in matching VRFs (list).
3125 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3126 safi_t safi
, struct prefix_evpn
*evp
,
3127 struct bgp_info
*ri
,
3128 struct list
*vrfs
, int install
)
3130 char buf
[PREFIX2STR_BUFFER
];
3131 struct bgp
*bgp_vrf
;
3132 struct listnode
*node
, *nnode
;
3134 /* Only type-2/type-5 routes go into a VRF */
3135 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3136 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3139 /* if it is type-2 route and not a mac+ip route skip this route */
3140 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3141 && !(is_evpn_prefix_ipaddr_v4(evp
)
3142 || is_evpn_prefix_ipaddr_v6(evp
)))
3145 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3149 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, ri
);
3151 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3155 flog_err(EC_BGP_EVPN_FAIL
,
3156 "%u: Failed to %s prefix %s in VRF %s",
3158 install
? "install" : "uninstall",
3159 prefix2str(evp
, buf
, sizeof(buf
)),
3160 vrf_id_to_name(bgp_vrf
->vrf_id
));
3169 * Install or uninstall route in matching VNIs (list).
3171 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3172 safi_t safi
, struct prefix_evpn
*evp
,
3173 struct bgp_info
*ri
,
3174 struct list
*vnis
, int install
)
3176 struct bgpevpn
*vpn
;
3177 struct listnode
*node
, *nnode
;
3179 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3182 if (!is_vni_live(vpn
))
3186 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, ri
);
3188 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, ri
);
3191 flog_err(EC_BGP_EVPN_FAIL
,
3192 "%u: Failed to %s EVPN %s route in VNI %u",
3193 bgp
->vrf_id
, install
? "install" : "uninstall",
3194 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3206 * Install or uninstall route for appropriate VNIs/ESIs.
3208 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3209 struct prefix
*p
, struct bgp_info
*ri
,
3212 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3213 struct attr
*attr
= ri
->attr
;
3214 struct ecommunity
*ecom
;
3219 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3220 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3221 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3222 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3223 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3226 /* If we don't have Route Target, nothing much to do. */
3227 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3230 ecom
= attr
->ecommunity
;
3231 if (!ecom
|| !ecom
->size
)
3234 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3235 * attached to the route */
3236 for (i
= 0; i
< ecom
->size
; i
++) {
3238 uint8_t type
, sub_type
;
3239 struct ecommunity_val
*eval
;
3240 struct ecommunity_val eval_tmp
;
3241 struct irt_node
*irt
; /* import rt for l2vni */
3242 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3245 /* Only deal with RTs */
3246 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3247 eval
= (struct ecommunity_val
*)(ecom
->val
3248 + (i
* ECOMMUNITY_SIZE
));
3251 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3255 * macip routes (type-2) are imported into VNI and VRF tables.
3256 * IMET route is imported into VNI table.
3257 * prefix routes are imported into VRF table.
3259 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3260 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3261 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3263 irt
= lookup_import_rt(bgp
, eval
);
3265 install_uninstall_route_in_vnis(bgp
, afi
, safi
,
3270 vrf_irt
= lookup_vrf_import_rt(eval
);
3272 install_uninstall_route_in_vrfs(bgp
, afi
, safi
,
3277 /* Also check for non-exact match.
3278 * In this, we mask out the AS and
3279 * only check on the local-admin sub-field.
3280 * This is to facilitate using
3281 * VNI as the RT for EBGP peering too.
3285 if (type
== ECOMMUNITY_ENCODE_AS
3286 || type
== ECOMMUNITY_ENCODE_AS4
3287 || type
== ECOMMUNITY_ENCODE_IP
) {
3288 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3289 mask_ecom_global_admin(&eval_tmp
, eval
);
3290 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3291 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3295 install_uninstall_route_in_vnis(bgp
, afi
, safi
,
3300 install_uninstall_route_in_vrfs(bgp
, afi
, safi
,
3306 /* es route is imported into the es table */
3307 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3309 /* we will match based on the entire esi to avoid
3310 * imoort of an es route for esi2 into esi1
3312 es
= bgp_evpn_lookup_es(bgp
, &evp
->prefix
.es_addr
.esi
);
3313 if (es
&& is_es_local(es
))
3314 install_uninstall_route_in_es(bgp
, es
,
3324 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3327 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3329 /* delete all ipv4 routes and withdraw from peers */
3330 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3331 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3333 /* delete all ipv6 routes and withdraw from peers */
3334 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3335 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3339 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3342 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3344 /* update all ipv4 routes */
3345 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3346 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3348 /* update all ipv6 routes */
3349 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3350 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3354 * update and advertise local routes for a VRF as type-5 routes.
3355 * This is invoked upon RD change for a VRF. Note taht the processing is only
3356 * done in the global route table using the routes which already exist in the
3359 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3361 /* skip if the RD is configured */
3362 if (is_vrf_rd_configured(bgp_vrf
))
3365 /* derive the RD for the VRF based on new router-id */
3366 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3368 /* update advertise ipv4|ipv6 routes as type-5 routes */
3369 update_advertise_vrf_routes(bgp_vrf
);
3373 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3374 * This is invoked upon VRF RD change. The processing is done only from global
3377 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3379 /* skip if the RD is configured */
3380 if (is_vrf_rd_configured(bgp_vrf
))
3383 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3384 delete_withdraw_vrf_routes(bgp_vrf
);
3388 * Update and advertise local routes for a VNI. Invoked upon router-id
3389 * change. Note that the processing is done only on the global route table
3390 * using routes that already exist in the per-VNI table.
3392 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3394 struct prefix_evpn p
;
3395 struct bgp_node
*rn
, *global_rn
;
3396 struct bgp_info
*ri
, *global_ri
;
3398 afi_t afi
= AFI_L2VPN
;
3399 safi_t safi
= SAFI_EVPN
;
3401 /* Locate type-3 route for VNI in the per-VNI table and use its
3402 * attributes to create and advertise the type-3 route for this VNI
3403 * in the global table.
3405 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3406 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3407 if (!rn
) /* unexpected */
3409 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
3410 if (ri
->peer
== bgp
->peer_self
&& ri
->type
== ZEBRA_ROUTE_BGP
3411 && ri
->sub_type
== BGP_ROUTE_STATIC
)
3413 if (!ri
) /* unexpected */
3417 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3418 (struct prefix
*)&p
, &vpn
->prd
);
3419 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1, &ri
,
3420 0, mac_mobility_seqnum(attr
));
3422 /* Schedule for processing and unlock node. */
3423 bgp_process(bgp
, global_rn
, afi
, safi
);
3424 bgp_unlock_node(global_rn
);
3426 /* Now, walk this VNI's route table and use the route and its attribute
3427 * to create and schedule route in global table.
3429 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
3430 rn
= bgp_route_next(rn
)) {
3431 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3433 /* Identify MAC-IP local routes. */
3434 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3437 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
3438 if (ri
->peer
== bgp
->peer_self
3439 && ri
->type
== ZEBRA_ROUTE_BGP
3440 && ri
->sub_type
== BGP_ROUTE_STATIC
)
3445 /* Create route in global routing table using this route entry's
3449 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3450 (struct prefix
*)evp
, &vpn
->prd
);
3452 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
3454 mac_mobility_seqnum(attr
));
3456 /* Schedule for processing and unlock node. */
3457 bgp_process(bgp
, global_rn
, afi
, safi
);
3458 bgp_unlock_node(global_rn
);
3465 * Delete (and withdraw) local routes for a VNI - only from the global
3466 * table. Invoked upon router-id change.
3468 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3471 struct prefix_evpn p
;
3472 struct bgp_node
*global_rn
;
3473 struct bgp_info
*ri
;
3474 afi_t afi
= AFI_L2VPN
;
3475 safi_t safi
= SAFI_EVPN
;
3477 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3478 * for this VNI - from the global table.
3480 ret
= delete_global_type2_routes(bgp
, vpn
);
3484 /* Remove type-3 route for this VNI from global table. */
3485 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3486 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3487 (struct prefix
*)&p
, &vpn
->prd
);
3489 /* Delete route entry in the global EVPN table. */
3490 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &ri
);
3492 /* Schedule for processing - withdraws to peers happen from
3496 bgp_process(bgp
, global_rn
, afi
, safi
);
3497 bgp_unlock_node(global_rn
);
3504 * Handle router-id change. Update and advertise local routes corresponding
3505 * to this VNI from peers. Note that this is invoked after updating the
3506 * router-id. The routes in the per-VNI table are used to create routes in
3507 * the global table and schedule them.
3509 static void update_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3511 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3513 /* Skip VNIs with configured RD. */
3514 if (is_rd_configured(vpn
))
3517 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3518 update_advertise_vni_routes(bgp
, vpn
);
3522 * Handle router-id change. Delete and withdraw local routes corresponding
3523 * to this VNI from peers. Note that this is invoked prior to updating
3524 * the router-id and is done only on the global route table, the routes
3525 * are needed in the per-VNI table to re-advertise with new router id.
3527 static void withdraw_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3529 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3531 /* Skip VNIs with configured RD. */
3532 if (is_rd_configured(vpn
))
3535 delete_withdraw_vni_routes(bgp
, vpn
);
3539 * Process received EVPN type-2 route (advertise or withdraw).
3541 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3542 struct attr
*attr
, uint8_t *pfx
, int psize
,
3543 uint32_t addpath_id
)
3545 struct prefix_rd prd
;
3546 struct prefix_evpn p
;
3547 struct bgp_route_evpn evpn
;
3549 uint8_t macaddr_len
;
3550 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
3551 uint32_t num_labels
= 0;
3555 /* Type-2 route should be either 33, 37 or 49 bytes or an
3556 * additional 3 bytes if there is a second label (VNI):
3557 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3558 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3559 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3561 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3562 && psize
!= 40 && psize
!= 52) {
3563 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3564 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3565 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3569 memset(&evpn
, 0, sizeof(evpn
));
3571 /* Make prefix_rd */
3572 prd
.family
= AF_UNSPEC
;
3574 memcpy(&prd
.val
, pfx
, 8);
3577 /* Make EVPN prefix. */
3578 memset(&p
, 0, sizeof(struct prefix_evpn
));
3580 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3581 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3583 /* Copy Ethernet Seg Identifier */
3584 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
3587 /* Copy Ethernet Tag */
3588 memcpy(ð_tag
, pfx
, 4);
3589 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3592 /* Get the MAC Addr len */
3593 macaddr_len
= *pfx
++;
3595 /* Get the MAC Addr */
3596 if (macaddr_len
== (ETH_ALEN
* 8)) {
3597 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
3601 EC_BGP_EVPN_ROUTE_INVALID
,
3602 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3603 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3609 ipaddr_len
= *pfx
++;
3610 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3611 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3613 EC_BGP_EVPN_ROUTE_INVALID
,
3614 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3615 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3620 ipaddr_len
/= 8; /* Convert to bytes. */
3621 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3624 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
3628 /* Get the VNI(s). Stored as bytes here. */
3630 memset(label
, 0, sizeof(label
));
3631 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
3632 pfx
+= BGP_LABEL_BYTES
;
3633 psize
-= (33 + ipaddr_len
);
3634 /* Do we have a second VNI? */
3637 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
3639 * If in future, we are required to access additional fields,
3640 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
3645 /* Process the route. */
3647 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3648 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3649 &prd
, &label
[0], num_labels
, 0, &evpn
);
3651 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3652 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3653 &prd
, &label
[0], num_labels
, &evpn
);
3658 * Process received EVPN type-3 route (advertise or withdraw).
3660 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3661 struct attr
*attr
, uint8_t *pfx
, int psize
,
3662 uint32_t addpath_id
)
3664 struct prefix_rd prd
;
3665 struct prefix_evpn p
;
3670 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3671 * IP len (1) and IP (4 or 16).
3673 if (psize
!= 17 && psize
!= 29) {
3674 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3675 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3676 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3680 /* If PMSI is present, log if it is anything other than IR.
3681 * Note: We just simply ignore the values as it is not clear if
3682 * doing anything else is better.
3685 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3686 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
) {
3688 EC_BGP_EVPN_PMSI_PRESENT
,
3689 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3690 peer
->bgp
->vrf_id
, peer
->host
,
3691 attr
->pmsi_tnl_type
);
3695 /* Make prefix_rd */
3696 prd
.family
= AF_UNSPEC
;
3698 memcpy(&prd
.val
, pfx
, 8);
3701 /* Make EVPN prefix. */
3702 memset(&p
, 0, sizeof(struct prefix_evpn
));
3704 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3705 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3707 /* Copy Ethernet Tag */
3708 memcpy(ð_tag
, pfx
, 4);
3709 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
3713 ipaddr_len
= *pfx
++;
3714 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3715 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
3716 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
3719 EC_BGP_EVPN_ROUTE_INVALID
,
3720 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3721 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3725 /* Process the route. */
3727 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3728 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3729 &prd
, NULL
, 0, 0, NULL
);
3731 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3732 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3733 &prd
, NULL
, 0, NULL
);
3738 * Process received EVPN type-4 route (advertise or withdraw).
3740 static int process_type4_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3741 struct attr
*attr
, uint8_t *pfx
, int psize
,
3742 uint32_t addpath_id
)
3747 struct in_addr vtep_ip
;
3748 struct prefix_rd prd
;
3749 struct prefix_evpn p
;
3751 /* Type-4 route should be either 23 or 35 bytes
3752 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
3754 if (psize
!= 23 && psize
!= 35) {
3755 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3756 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
3757 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3761 /* Make prefix_rd */
3762 prd
.family
= AF_UNSPEC
;
3764 memcpy(&prd
.val
, pfx
, 8);
3768 memcpy(&esi
, pfx
, ESI_BYTES
);
3773 ipaddr_len
= *pfx
++;
3774 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3775 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
3778 EC_BGP_EVPN_ROUTE_INVALID
,
3779 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
3780 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3784 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
3785 /* Process the route. */
3787 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3788 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3789 &prd
, NULL
, 0, 0, NULL
);
3791 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3792 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3793 &prd
, NULL
, 0, NULL
);
3800 * Process received EVPN type-5 route (advertise or withdraw).
3802 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3803 struct attr
*attr
, uint8_t *pfx
, int psize
,
3804 uint32_t addpath_id
, int withdraw
)
3806 struct prefix_rd prd
;
3807 struct prefix_evpn p
;
3808 struct bgp_route_evpn evpn
;
3811 mpls_label_t label
; /* holds the VNI as in the packet */
3814 /* Type-5 route should be 34 or 58 bytes:
3815 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3816 * GW (4 or 16) and VNI (3).
3817 * Note that the IP and GW should both be IPv4 or both IPv6.
3819 if (psize
!= 34 && psize
!= 58) {
3820 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3821 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3822 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3826 /* Make prefix_rd */
3827 prd
.family
= AF_UNSPEC
;
3829 memcpy(&prd
.val
, pfx
, 8);
3832 /* Make EVPN prefix. */
3833 memset(&p
, 0, sizeof(struct prefix_evpn
));
3835 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3836 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
3838 /* Additional information outside of prefix - ESI and GW IP */
3839 memset(&evpn
, 0, sizeof(evpn
));
3842 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
3845 /* Fetch Ethernet Tag. */
3846 memcpy(ð_tag
, pfx
, 4);
3847 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
3850 /* Fetch IP prefix length. */
3852 if (ippfx_len
> IPV6_MAX_BITLEN
) {
3854 EC_BGP_EVPN_ROUTE_INVALID
,
3855 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3856 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
3859 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
3861 /* Determine IPv4 or IPv6 prefix */
3862 /* Since the address and GW are from the same family, this just becomes
3863 * a simple check on the total size.
3866 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
3867 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
3869 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
3872 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
3873 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
3875 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
3879 /* Get the VNI (in MPLS label field). Stored as bytes here. */
3880 memset(&label
, 0, sizeof(label
));
3881 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
3884 * If in future, we are required to access additional fields,
3885 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
3889 /* Process the route. */
3891 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3892 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3893 &prd
, &label
, 1, 0, &evpn
);
3895 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3896 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3897 &prd
, &label
, 1, &evpn
);
3902 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
3903 struct prefix_rd
*prd
, mpls_label_t
*label
,
3904 uint32_t num_labels
, struct attr
*attr
)
3908 struct evpn_addr
*p_evpn_p
;
3910 memset(&temp
, 0, 16);
3911 if (p
->family
!= AF_EVPN
)
3913 p_evpn_p
= &(p
->u
.prefix_evpn
);
3915 /* len denites the total len of IP and GW-IP in the route
3916 IP and GW-IP have to be both ipv4 or ipv6
3918 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3919 len
= 8; /* IP and GWIP are both ipv4 */
3921 len
= 32; /* IP and GWIP are both ipv6 */
3922 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
3923 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
3924 stream_put(s
, prd
->val
, 8);
3926 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
3928 stream_put(s
, &temp
, 10);
3929 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
3930 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
3931 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3932 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
3934 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
3936 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3938 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
3940 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
3942 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3943 stream_put_ipv4(s
, 0);
3945 stream_put(s
, &temp
, 16);
3949 stream_put(s
, label
, 3);
3955 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
3957 static void cleanup_vni_on_disable(struct hash_backet
*backet
, struct bgp
*bgp
)
3959 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3961 /* Remove EVPN routes and schedule for processing. */
3962 delete_routes_for_vni(bgp
, vpn
);
3964 /* Clear "live" flag and see if hash needs to be freed. */
3965 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
3966 if (!is_vni_configured(vpn
))
3967 bgp_evpn_free(bgp
, vpn
);
3971 * Free a VNI entry; iterator function called during cleanup.
3973 static void free_vni_entry(struct hash_backet
*backet
, struct bgp
*bgp
)
3975 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3977 delete_all_vni_routes(bgp
, vpn
);
3978 bgp_evpn_free(bgp
, vpn
);
3982 * Derive AUTO import RT for BGP VRF - L3VNI
3984 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
3986 struct bgp
*bgp_def
= NULL
;
3988 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
3989 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
3992 bgp_def
= bgp_get_default();
3995 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
3999 * Delete AUTO import RT from BGP VRF - L3VNI
4001 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4003 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4007 * Derive AUTO export RT for BGP VRF - L3VNI
4009 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4011 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4012 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4016 * Delete AUTO export RT from BGP VRF - L3VNI
4018 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4020 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4023 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4025 struct bgp
*bgp_def
= NULL
;
4026 struct listnode
*node
= NULL
;
4027 struct bgpevpn
*vpn
= NULL
;
4029 bgp_def
= bgp_get_default();
4033 /* update all type-5 routes */
4034 update_advertise_vrf_routes(bgp_vrf
);
4036 /* update all type-2 routes */
4037 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4038 update_routes_for_vni(bgp_def
, vpn
);
4042 * Handle autort change for a given VNI.
4044 static void update_autort_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
4046 struct bgpevpn
*vpn
= backet
->data
;
4048 if (!is_import_rt_configured(vpn
)) {
4049 if (is_vni_live(vpn
))
4050 bgp_evpn_uninstall_routes(bgp
, vpn
);
4051 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4052 list_delete_all_node(vpn
->import_rtl
);
4053 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4054 if (is_vni_live(vpn
))
4055 bgp_evpn_install_routes(bgp
, vpn
);
4057 if (!is_export_rt_configured(vpn
)) {
4058 list_delete_all_node(vpn
->export_rtl
);
4059 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4060 if (is_vni_live(vpn
))
4061 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4069 /* withdraw type-5 route corresponding to ip prefix */
4070 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4071 afi_t afi
, safi_t safi
)
4074 struct prefix_evpn evp
;
4075 char buf
[PREFIX_STRLEN
];
4077 build_type5_prefix_from_ip_prefix(&evp
, p
);
4078 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4081 EC_BGP_EVPN_ROUTE_DELETE
,
4082 "%u failed to delete type-5 route for prefix %s in vrf %s",
4083 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4084 vrf_id_to_name(bgp_vrf
->vrf_id
));
4088 /* withdraw all type-5 routes for an address family */
4089 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4091 struct bgp_table
*table
= NULL
;
4092 struct bgp_node
*rn
= NULL
;
4093 struct bgp_info
*ri
;
4095 table
= bgp_vrf
->rib
[afi
][safi
];
4096 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4097 /* Only care about "selected" routes - non-imported. */
4098 /* TODO: Support for AddPath for EVPN. */
4099 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
4100 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
)
4101 && (!ri
->extra
|| !ri
->extra
->parent
)) {
4102 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
4111 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4112 * to this function correspond to those of the source IP prefix (best
4113 * path in the case of the attr. In the case of a local prefix (when we
4114 * are advertising local subnets), the src_attr will be NULL.
4116 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4117 struct attr
*src_attr
, afi_t afi
,
4121 struct prefix_evpn evp
;
4122 char buf
[PREFIX_STRLEN
];
4124 build_type5_prefix_from_ip_prefix(&evp
, p
);
4125 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4127 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4128 "%u: Failed to create type-5 route for prefix %s",
4129 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4132 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4133 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4134 * advertisement is enabled.
4136 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4139 struct bgp_table
*table
= NULL
;
4140 struct bgp_node
*rn
= NULL
;
4141 struct bgp_info
*ri
;
4143 table
= bgp_vrf
->rib
[afi
][safi
];
4144 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4145 /* Need to identify the "selected" route entry to use its
4146 * attribute. Also, we only consider "non-imported" routes.
4147 * TODO: Support for AddPath for EVPN.
4149 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
4150 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
)
4151 && (!ri
->extra
|| !ri
->extra
->parent
)) {
4153 /* apply the route-map */
4154 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4157 ret
= route_map_apply(
4158 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4160 &rn
->p
, RMAP_BGP
, ri
);
4161 if (ret
== RMAP_DENYMATCH
)
4164 bgp_evpn_advertise_type5_route(
4165 bgp_vrf
, &rn
->p
, ri
->attr
, afi
, safi
);
4172 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4174 struct listnode
*node
, *nnode
, *node_to_del
;
4175 struct ecommunity
*ecom
, *ecom_auto
;
4176 struct ecommunity_val eval
;
4178 if (bgp
->advertise_autort_rfc8365
)
4179 vni
|= EVPN_AUTORT_VXLAN
;
4180 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4182 ecom_auto
= ecommunity_new();
4183 ecommunity_add_val(ecom_auto
, &eval
);
4186 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4187 if (ecommunity_match(ecom
, ecom_auto
)) {
4188 ecommunity_free(&ecom
);
4194 list_delete_node(rtl
, node_to_del
);
4196 ecommunity_free(&ecom_auto
);
4199 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4200 struct ecommunity
*ecomadd
)
4202 /* uninstall routes from vrf */
4203 uninstall_routes_for_vrf(bgp_vrf
);
4205 /* Cleanup the RT to VRF mapping */
4206 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4208 /* Remove auto generated RT */
4209 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4211 /* Add the newly configured RT to RT list */
4212 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4213 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4215 /* map VRF to its RTs */
4216 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4218 /* install routes matching the new VRF */
4219 install_routes_for_vrf(bgp_vrf
);
4222 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4223 struct ecommunity
*ecomdel
)
4225 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4226 struct ecommunity
*ecom
= NULL
;
4228 /* uninstall routes from vrf */
4229 uninstall_routes_for_vrf(bgp_vrf
);
4231 /* Cleanup the RT to VRF mapping */
4232 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4234 /* remove the RT from the RT list */
4235 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4236 if (ecommunity_match(ecom
, ecomdel
)) {
4237 ecommunity_free(&ecom
);
4244 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4246 assert(bgp_vrf
->vrf_import_rtl
);
4247 /* fallback to auto import rt, if this was the last RT */
4248 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4249 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4250 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4253 /* map VRFs to its RTs */
4254 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4256 /* install routes matching this new RT */
4257 install_routes_for_vrf(bgp_vrf
);
4260 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4261 struct ecommunity
*ecomadd
)
4263 /* remove auto-generated RT */
4264 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4266 /* Add the new RT to the RT list */
4267 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4268 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4270 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4273 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4274 struct ecommunity
*ecomdel
)
4276 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4277 struct ecommunity
*ecom
= NULL
;
4279 /* Remove the RT from the RT list */
4280 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4281 if (ecommunity_match(ecom
, ecomdel
)) {
4282 ecommunity_free(&ecom
);
4289 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4292 * Temporary assert to make SA happy.
4293 * The ALL_LIST_ELEMENTS macro above has a NULL check
4294 * which means that SA is going to complain about
4295 * the list_isempty call, which doesn't NULL check.
4296 * So until we get this situation cleaned up, here
4299 assert(bgp_vrf
->vrf_export_rtl
);
4301 /* fall back to auto-generated RT if this was the last RT */
4302 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4303 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4304 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4307 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4311 * Handle change to BGP router id. This is invoked twice by the change
4312 * handler, first before the router id has been changed and then after
4313 * the router id has been changed. The first invocation will result in
4314 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4315 * will result in the routes being re-advertised.
4317 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4321 /* delete and withdraw all the type-5 routes
4322 stored in the global table for this vrf
4324 withdraw_router_id_vrf(bgp
);
4326 /* delete all the VNI routes (type-2/type-3) routes for all the
4329 hash_iterate(bgp
->vnihash
,
4330 (void (*)(struct hash_backet
*,
4331 void *))withdraw_router_id_vni
,
4335 /* advertise all routes in the vrf as type-5 routes with the new
4338 update_router_id_vrf(bgp
);
4340 /* advertise all the VNI routes (type-2/type-3) routes with the
4343 hash_iterate(bgp
->vnihash
,
4344 (void (*)(struct hash_backet
*,
4345 void *))update_router_id_vni
,
4351 * Handle change to auto-RT algorithm - update and advertise local routes.
4353 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4355 hash_iterate(bgp
->vnihash
,
4356 (void (*)(struct hash_backet
*,
4357 void*))update_autort_vni
,
4362 * Handle change to export RT - update and advertise local routes.
4364 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4366 return update_routes_for_vni(bgp
, vpn
);
4369 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4372 delete_withdraw_vrf_routes(bgp_vrf
);
4374 update_advertise_vrf_routes(bgp_vrf
);
4378 * Handle change to RD. This is invoked twice by the change handler,
4379 * first before the RD has been changed and then after the RD has
4380 * been changed. The first invocation will result in local routes
4381 * of this VNI being deleted and withdrawn and the next will result
4382 * in the routes being re-advertised.
4384 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4388 delete_withdraw_vni_routes(bgp
, vpn
);
4390 update_advertise_vni_routes(bgp
, vpn
);
4394 * Install routes for this VNI. Invoked upon change to Import RT.
4396 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4398 return install_routes_for_vni(bgp
, vpn
);
4402 * Uninstall all routes installed for this VNI. Invoked upon change
4405 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4407 return uninstall_routes_for_vni(bgp
, vpn
);
4411 * TODO: Hardcoded for a maximum of 2 VNIs right now
4413 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4418 vni1
= label2vni(label
);
4419 if (num_labels
== 2) {
4420 vni2
= label2vni(label
+ 1);
4421 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4423 snprintf(buf
, len
, "%u", vni1
);
4428 * Function to convert evpn route to json format.
4429 * NOTE: We don't use prefix2str as the output here is a bit different.
4431 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
4433 char buf1
[ETHER_ADDR_STRLEN
];
4434 char buf2
[PREFIX2STR_BUFFER
];
4439 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4440 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4441 json_object_int_add(json
, "ethTag",
4442 p
->prefix
.imet_addr
.eth_tag
);
4443 json_object_int_add(json
, "ipLen",
4444 is_evpn_prefix_ipaddr_v4(p
)
4447 json_object_string_add(json
, "ip",
4448 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4449 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4450 if (is_evpn_prefix_ipaddr_none(p
)) {
4451 json_object_int_add(json
, "routeType",
4452 p
->prefix
.route_type
);
4453 json_object_int_add(json
, "ethTag",
4454 p
->prefix
.macip_addr
.eth_tag
);
4455 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4456 json_object_string_add(json
, "mac",
4457 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4463 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4466 json_object_int_add(json
, "routeType",
4467 p
->prefix
.route_type
);
4468 json_object_int_add(json
, "ethTag",
4469 p
->prefix
.macip_addr
.eth_tag
);
4470 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4471 json_object_string_add(json
, "mac",
4472 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4475 json_object_int_add(json
, "ipLen",
4476 is_evpn_prefix_ipaddr_v4(p
)
4479 json_object_string_add(
4482 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4484 PREFIX2STR_BUFFER
));
4487 /* Currently, this is to cater to other AF_ETHERNET code. */
4492 * Function to convert evpn route to string.
4493 * NOTE: We don't use prefix2str as the output here is a bit different.
4495 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
4497 char buf1
[ETHER_ADDR_STRLEN
];
4498 char buf2
[PREFIX2STR_BUFFER
];
4499 char buf3
[ESI_STR_LEN
];
4501 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4502 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
4503 p
->prefix
.imet_addr
.eth_tag
,
4504 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4506 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4507 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4508 if (is_evpn_prefix_ipaddr_none(p
))
4509 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4510 p
->prefix
.route_type
,
4511 p
->prefix
.macip_addr
.eth_tag
,
4513 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4518 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4520 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
4521 p
->prefix
.route_type
,
4522 p
->prefix
.macip_addr
.eth_tag
,
4524 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4526 family
== AF_INET
? IPV4_MAX_BITLEN
4529 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4531 PREFIX2STR_BUFFER
));
4533 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
4534 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4535 p
->prefix
.route_type
,
4536 p
->prefix
.prefix_addr
.eth_tag
,
4537 p
->prefix
.prefix_addr
.ip_prefix_length
,
4538 is_evpn_prefix_ipaddr_v4(p
)
4539 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
4540 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
4541 } else if (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
4542 snprintf(buf
, len
, "[%d]:[%s]:[%d]:[%s]",
4543 p
->prefix
.route_type
,
4544 esi_to_str(&p
->prefix
.es_addr
.esi
, buf3
, sizeof(buf3
)),
4545 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4547 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
));
4549 /* For EVPN route types not supported yet. */
4550 snprintf(buf
, len
, "(unsupported route type %d)",
4551 p
->prefix
.route_type
);
4558 * Encode EVPN prefix in Update (MP_REACH)
4560 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
4561 struct prefix_rd
*prd
, mpls_label_t
*label
,
4562 uint32_t num_labels
, struct attr
*attr
,
4563 int addpath_encode
, uint32_t addpath_tx_id
)
4565 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4566 int len
, ipa_len
= 0;
4569 stream_putl(s
, addpath_tx_id
);
4572 stream_putc(s
, evp
->prefix
.route_type
);
4574 switch (evp
->prefix
.route_type
) {
4575 case BGP_EVPN_MAC_IP_ROUTE
:
4576 if (is_evpn_prefix_ipaddr_v4(evp
))
4577 ipa_len
= IPV4_MAX_BYTELEN
;
4578 else if (is_evpn_prefix_ipaddr_v6(evp
))
4579 ipa_len
= IPV6_MAX_BYTELEN
;
4580 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4581 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4582 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4584 stream_putc(s
, len
);
4585 stream_put(s
, prd
->val
, 8); /* RD */
4587 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
4589 stream_put(s
, 0, 10);
4590 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4591 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4592 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4593 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4594 if (ipa_len
) /* IP */
4595 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4597 /* 1st label is the L2 VNI */
4598 stream_put(s
, label
, BGP_LABEL_BYTES
);
4599 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4600 if (ipa_len
&& num_labels
> 1)
4601 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4604 case BGP_EVPN_IMET_ROUTE
:
4605 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4606 stream_put(s
, prd
->val
, 8); /* RD */
4607 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4608 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4609 /* Originating Router's IP Addr */
4610 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4613 case BGP_EVPN_ES_ROUTE
:
4614 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4615 stream_put(s
, prd
->val
, 8); /* RD */
4616 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4617 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4619 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4622 case BGP_EVPN_IP_PREFIX_ROUTE
:
4623 /* TODO: AddPath support. */
4624 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4632 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4633 struct bgp_nlri
*packet
, int withdraw
)
4639 uint32_t addpath_id
;
4640 int addpath_encoded
;
4645 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4647 lim
= pnt
+ packet
->length
;
4649 safi
= packet
->safi
;
4653 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
4654 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
4655 PEER_CAP_ADDPATH_AF_TX_RCV
));
4657 for (; pnt
< lim
; pnt
+= psize
) {
4658 /* Clear prefix structure. */
4659 memset(&p
, 0, sizeof(struct prefix
));
4661 /* Deal with path-id if AddPath is supported. */
4662 if (addpath_encoded
) {
4663 /* When packet overflow occurs return immediately. */
4664 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4667 addpath_id
= ntohl(*((uint32_t *)pnt
));
4668 pnt
+= BGP_ADDPATH_ID_LEN
;
4671 /* All EVPN NLRI types start with type and length. */
4678 /* When packet overflow occur return immediately. */
4679 if (pnt
+ psize
> lim
)
4683 case BGP_EVPN_MAC_IP_ROUTE
:
4684 if (process_type2_route(peer
, afi
, safi
,
4685 withdraw
? NULL
: attr
, pnt
,
4686 psize
, addpath_id
)) {
4689 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4690 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4695 case BGP_EVPN_IMET_ROUTE
:
4696 if (process_type3_route(peer
, afi
, safi
,
4697 withdraw
? NULL
: attr
, pnt
,
4698 psize
, addpath_id
)) {
4701 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4702 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4707 case BGP_EVPN_ES_ROUTE
:
4708 if (process_type4_route(peer
, afi
, safi
,
4709 withdraw
? NULL
: attr
, pnt
,
4710 psize
, addpath_id
)) {
4713 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4714 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4719 case BGP_EVPN_IP_PREFIX_ROUTE
:
4720 if (process_type5_route(peer
, afi
, safi
, attr
, pnt
,
4721 psize
, addpath_id
, withdraw
)) {
4724 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4725 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4735 /* Packet length consistency check. */
4743 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
4744 * The mapping will be used during route processing.
4745 * bgp_def: default bgp instance
4746 * bgp_vrf: specific bgp vrf instance on which RT is configured
4748 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
4751 struct ecommunity_val
*eval
= NULL
;
4752 struct listnode
*node
= NULL
, *nnode
= NULL
;
4753 struct ecommunity
*ecom
= NULL
;
4755 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4756 for (i
= 0; i
< ecom
->size
; i
++) {
4757 eval
= (struct ecommunity_val
*)(ecom
->val
4759 * ECOMMUNITY_SIZE
));
4760 map_vrf_to_rt(bgp_vrf
, eval
);
4766 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
4768 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
4771 struct ecommunity_val
*eval
;
4772 struct listnode
*node
, *nnode
;
4773 struct ecommunity
*ecom
;
4775 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4776 for (i
= 0; i
< ecom
->size
; i
++) {
4777 struct vrf_irt_node
*irt
;
4778 struct ecommunity_val eval_tmp
;
4780 eval
= (struct ecommunity_val
*)(ecom
->val
4782 * ECOMMUNITY_SIZE
));
4783 /* If using "automatic" RT, we only care about the
4784 * local-admin sub-field.
4785 * This is to facilitate using VNI as the RT for EBGP
4788 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4789 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
4790 BGP_VRF_IMPORT_RT_CFGD
))
4791 mask_ecom_global_admin(&eval_tmp
, eval
);
4793 irt
= lookup_vrf_import_rt(&eval_tmp
);
4795 unmap_vrf_from_rt(bgp_vrf
, irt
);
4802 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
4803 * The mapping will be used during route processing.
4805 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4808 struct ecommunity_val
*eval
;
4809 struct listnode
*node
, *nnode
;
4810 struct ecommunity
*ecom
;
4812 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4813 for (i
= 0; i
< ecom
->size
; i
++) {
4814 eval
= (struct ecommunity_val
*)(ecom
->val
4816 * ECOMMUNITY_SIZE
));
4817 map_vni_to_rt(bgp
, vpn
, eval
);
4823 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
4825 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4828 struct ecommunity_val
*eval
;
4829 struct listnode
*node
, *nnode
;
4830 struct ecommunity
*ecom
;
4832 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4833 for (i
= 0; i
< ecom
->size
; i
++) {
4834 struct irt_node
*irt
;
4835 struct ecommunity_val eval_tmp
;
4837 eval
= (struct ecommunity_val
*)(ecom
->val
4839 * ECOMMUNITY_SIZE
));
4840 /* If using "automatic" RT, we only care about the
4841 * local-admin sub-field.
4842 * This is to facilitate using VNI as the RT for EBGP
4845 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4846 if (!is_import_rt_configured(vpn
))
4847 mask_ecom_global_admin(&eval_tmp
, eval
);
4849 irt
= lookup_import_rt(bgp
, &eval_tmp
);
4851 unmap_vni_from_rt(bgp
, vpn
, irt
);
4857 * Derive Import RT automatically for VNI and map VNI to RT.
4858 * The mapping will be used during route processing.
4860 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4862 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
4863 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
4866 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
4870 * Derive Export RT automatically for VNI.
4872 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4874 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
4875 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
4879 * Derive RD automatically for VNI using passed information - it
4880 * is of the form RouterId:unique-id-for-vni.
4882 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
4884 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
4888 * Derive RD automatically for VNI using passed information - it
4889 * is of the form RouterId:unique-id-for-vni.
4891 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4895 vpn
->prd
.family
= AF_UNSPEC
;
4896 vpn
->prd
.prefixlen
= 64;
4897 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
4898 (void)str2prefix_rd(buf
, &vpn
->prd
);
4899 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
4905 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
4907 struct list
*inst
= bm
->bgp
;
4908 struct listnode
*node
;
4909 struct bgp
*bgp_vrf
;
4911 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
4912 if (bgp_vrf
->l3vni
== vni
)
4922 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
4924 struct bgpevpn
*vpn
;
4927 memset(&tmp
, 0, sizeof(struct bgpevpn
));
4929 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
4934 * Create a new vpn - invoked upon configuration or zebra notification.
4936 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
4937 struct in_addr originator_ip
,
4938 vrf_id_t tenant_vrf_id
)
4940 struct bgpevpn
*vpn
;
4945 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
4949 /* Set values - RD and RT set to defaults. */
4951 vpn
->originator_ip
= originator_ip
;
4952 vpn
->tenant_vrf_id
= tenant_vrf_id
;
4954 /* Initialize route-target import and export lists */
4955 vpn
->import_rtl
= list_new();
4956 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4957 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
4958 vpn
->export_rtl
= list_new();
4959 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4960 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
4961 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
4962 derive_rd_rt_for_vni(bgp
, vpn
);
4964 /* Initialize EVPN route table. */
4965 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
4968 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
4969 XFREE(MTYPE_BGP_EVPN
, vpn
);
4973 /* add to l2vni list on corresponding vrf */
4974 bgpevpn_link_to_l3vni(vpn
);
4976 QOBJ_REG(vpn
, bgpevpn
);
4981 * Free a given VPN - called in multiple scenarios such as zebra
4982 * notification, configuration being deleted, advertise-all-vni disabled etc.
4983 * This just frees appropriate memory, caller should have taken other
4986 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4988 bgpevpn_unlink_from_l3vni(vpn
);
4989 bgp_table_unlock(vpn
->route_table
);
4990 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4991 list_delete(&vpn
->import_rtl
);
4992 list_delete(&vpn
->export_rtl
);
4993 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
4994 hash_release(bgp
->vnihash
, vpn
);
4996 XFREE(MTYPE_BGP_EVPN
, vpn
);
5002 struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
)
5007 memset(&tmp
, 0, sizeof(struct evpnes
));
5008 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
5009 es
= hash_lookup(bgp
->esihash
, &tmp
);
5014 * Create a new local es - invoked upon zebra notification.
5016 struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
,
5018 struct ipaddr
*originator_ip
)
5026 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct evpnes
));
5030 /* set the ESI and originator_ip */
5031 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
5032 memcpy(&es
->originator_ip
, originator_ip
, sizeof(struct ipaddr
));
5034 /* Initialise the VTEP list */
5035 es
->vtep_list
= list_new();
5036 es
->vtep_list
->cmp
= (int (*)(void *, void *))evpn_vtep_ip_cmp
;
5038 /* auto derive RD for this es */
5039 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
5040 es
->prd
.family
= AF_UNSPEC
;
5041 es
->prd
.prefixlen
= 64;
5042 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), es
->rd_id
);
5043 (void)str2prefix_rd(buf
, &es
->prd
);
5045 /* Initialize the ES route table */
5046 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5049 if (!hash_get(bgp
->esihash
, es
, hash_alloc_intern
)) {
5050 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5054 QOBJ_REG(es
, evpnes
);
5060 * This just frees appropriate memory, caller should have taken other
5063 void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
)
5065 list_delete(&es
->vtep_list
);
5066 bgp_table_unlock(es
->route_table
);
5067 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
5068 hash_release(bgp
->esihash
, es
);
5070 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5074 * Import evpn route from global table to VNI/VRF/ESI.
5076 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5077 struct prefix
*p
, struct bgp_info
*ri
)
5079 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 1);
5083 * Unimport evpn route from VNI/VRF/ESI.
5085 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5086 struct prefix
*p
, struct bgp_info
*ri
)
5088 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 0);
5091 /* filter routes which have martian next hops */
5092 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5096 struct bgp_node
*rd_rn
, *rn
;
5097 struct bgp_table
*table
;
5098 struct bgp_info
*ri
;
5103 /* Walk entire global routing table and evaluate routes which could be
5104 * imported into this VPN. Note that we cannot just look at the routes
5105 * for the VNI's RD -
5106 * remote routes applicable for this VNI could have any RD.
5108 /* EVPN routes are a 2-level table. */
5109 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
5110 rd_rn
= bgp_route_next(rd_rn
)) {
5111 table
= (struct bgp_table
*)(rd_rn
->info
);
5115 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5117 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
5119 /* Consider "valid" remote routes applicable for
5121 if (!(ri
->type
== ZEBRA_ROUTE_BGP
5122 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
5125 if (bgp_nexthop_self(bgp
, ri
->attr
->nexthop
)) {
5127 char attr_str
[BUFSIZ
];
5128 char pbuf
[PREFIX_STRLEN
];
5130 bgp_dump_attr(ri
->attr
, attr_str
,
5133 if (bgp_debug_update(ri
->peer
, &rn
->p
,
5136 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
5143 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5146 bgp_rib_remove(rn
, ri
, ri
->peer
, afi
,
5157 * Handle del of a local MACIP.
5159 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5162 struct bgpevpn
*vpn
;
5163 struct prefix_evpn p
;
5165 /* Lookup VNI hash - should exist. */
5166 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5167 if (!vpn
|| !is_vni_live(vpn
)) {
5168 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5169 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5170 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5174 /* Remove EVPN type-2 route and schedule for processing. */
5175 build_evpn_type2_prefix(&p
, mac
, ip
);
5176 delete_evpn_route(bgp
, vpn
, &p
);
5182 * Handle add of a local MACIP.
5184 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5185 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
)
5187 struct bgpevpn
*vpn
;
5188 struct prefix_evpn p
;
5190 /* Lookup VNI hash - should exist. */
5191 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5192 if (!vpn
|| !is_vni_live(vpn
)) {
5193 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5194 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5195 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5199 /* Create EVPN type-2 route and schedule for processing. */
5200 build_evpn_type2_prefix(&p
, mac
, ip
);
5201 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
)) {
5202 char buf
[ETHER_ADDR_STRLEN
];
5203 char buf2
[INET6_ADDRSTRLEN
];
5206 EC_BGP_EVPN_ROUTE_CREATE
,
5207 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5208 bgp
->vrf_id
, vpn
->vni
,
5209 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5212 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5213 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5220 static void link_l2vni_hash_to_l3vni(struct hash_backet
*backet
,
5221 struct bgp
*bgp_vrf
)
5223 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
5224 struct bgp
*bgp_def
= NULL
;
5226 bgp_def
= bgp_get_default();
5229 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5230 bgpevpn_link_to_l3vni(vpn
);
5233 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
, struct ethaddr
*rmac
,
5234 struct in_addr originator_ip
, int filter
)
5236 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5237 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5238 struct listnode
*node
= NULL
;
5239 struct bgpevpn
*vpn
= NULL
;
5242 /* get the default instance - required to get the AS number for VRF
5245 bgp_def
= bgp_get_default();
5249 "Cannot process L3VNI %u ADD - default BGP instance not yet created",
5255 /* if the BGP vrf instance doesnt exist - create one */
5256 bgp_vrf
= bgp_lookup_by_name(vrf_id_to_name(vrf_id
));
5261 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5262 BGP_INSTANCE_TYPE_VRF
);
5264 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
:
5265 flog_err(EC_BGP_MULTI_INSTANCE
,
5266 "'bgp multiple-instance' not present\n");
5268 case BGP_ERR_AS_MISMATCH
:
5269 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5270 "BGP is already running; AS is %u\n", as
);
5272 case BGP_ERR_INSTANCE_MISMATCH
:
5273 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5274 "BGP instance name and AS number mismatch\n");
5278 /* mark as auto created */
5279 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5282 /* associate with l3vni */
5283 bgp_vrf
->l3vni
= l3vni
;
5285 /* set the router mac - to be used in mac-ip routes for this vrf */
5286 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
5288 /* set the originator ip */
5289 bgp_vrf
->originator_ip
= originator_ip
;
5291 /* set the right filter - are we using l3vni only for prefix routes? */
5293 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5295 /* auto derive RD/RT */
5296 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5297 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5298 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5299 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5300 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5302 /* link all corresponding l2vnis */
5303 hash_iterate(bgp_def
->vnihash
,
5304 (void (*)(struct hash_backet
*,
5305 void *))link_l2vni_hash_to_l3vni
,
5308 /* Only update all corresponding type-2 routes if we are advertising two
5309 * labels along with type-2 routes
5312 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5313 update_routes_for_vni(bgp_def
, vpn
);
5315 /* advertise type-5 routes if needed */
5316 update_advertise_vrf_routes(bgp_vrf
);
5318 /* install all remote routes belonging to this l3vni into correspondng
5320 install_routes_for_vrf(bgp_vrf
);
5325 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5327 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5328 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5329 struct listnode
*node
= NULL
;
5330 struct listnode
*next
= NULL
;
5331 struct bgpevpn
*vpn
= NULL
;
5333 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5337 "Cannot process L3VNI %u Del - Could not find BGP instance",
5342 bgp_def
= bgp_get_default();
5346 "Cannot process L3VNI %u Del - Could not find default BGP instance",
5351 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5352 * bgp_delete would not remove/decrement bgp_info of the ip_prefix
5353 * routes. This will uninstalling the routes from zebra and decremnt the
5356 uninstall_routes_for_vrf(bgp_vrf
);
5358 /* delete/withdraw all type-5 routes */
5359 delete_withdraw_vrf_routes(bgp_vrf
);
5361 /* remove the l3vni from vrf instance */
5364 /* remove the Rmac from the BGP vrf */
5365 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5368 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5369 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5370 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5372 if (!list_isempty(bgp_vrf
->vrf_export_rtl
)) {
5373 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5376 /* update all corresponding local mac-ip routes */
5377 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5378 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5379 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5380 update_routes_for_vni(bgp_def
, vpn
);
5384 /* If any L2VNIs point to this instance, unlink them. */
5385 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5386 bgpevpn_unlink_from_l3vni(vpn
);
5388 /* Delete the instance if it was autocreated */
5389 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5390 bgp_delete(bgp_vrf
);
5396 * Handle del of a local VNI.
5398 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5400 struct bgpevpn
*vpn
;
5402 /* Locate VNI hash */
5403 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5405 if (bgp_debug_zebra(NULL
))
5407 EC_BGP_EVPN_VPN_VNI
,
5408 "%u: VNI hash entry for VNI %u not found at DEL",
5413 /* Remove all local EVPN routes and schedule for processing (to
5414 * withdraw from peers).
5416 delete_routes_for_vni(bgp
, vpn
);
5419 * tunnel is no longer active, del tunnel ip address from tip_hash
5421 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5423 /* Clear "live" flag and see if hash needs to be freed. */
5424 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5425 if (!is_vni_configured(vpn
))
5426 bgp_evpn_free(bgp
, vpn
);
5432 * Handle add (or update) of a local VNI. The VNI changes we care
5433 * about are for the local-tunnel-ip and the (tenant) VRF.
5435 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5436 struct in_addr originator_ip
, vrf_id_t tenant_vrf_id
)
5438 struct bgpevpn
*vpn
;
5439 struct prefix_evpn p
;
5441 /* Lookup VNI. If present and no change, exit. */
5442 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5445 if (is_vni_live(vpn
)
5446 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5447 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5448 /* Probably some other param has changed that we don't
5452 /* Update tenant_vrf_id if it has changed. */
5453 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5454 bgpevpn_unlink_from_l3vni(vpn
);
5455 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5456 bgpevpn_link_to_l3vni(vpn
);
5459 /* If tunnel endpoint IP has changed, update (and delete prior
5460 * type-3 route, if needed.)
5462 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5463 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5465 /* Update all routes with new endpoint IP and/or export RT
5468 if (is_vni_live(vpn
))
5469 update_routes_for_vni(bgp
, vpn
);
5472 /* Create or update as appropriate. */
5474 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
);
5478 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5484 /* if the VNI is live already, there is nothing more to do */
5485 if (is_vni_live(vpn
))
5488 /* Mark as "live" */
5489 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5491 /* tunnel is now active, add tunnel-ip to db */
5492 bgp_tip_add(bgp
, &originator_ip
);
5494 /* filter routes as nexthop database has changed */
5495 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5497 /* Create EVPN type-3 route and schedule for processing. */
5498 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5499 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0)) {
5500 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5501 "%u: Type3 route creation failure for VNI %u",
5506 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5510 install_routes_for_vni(bgp
, vpn
);
5512 /* If we are advertising gateway mac-ip
5513 It needs to be conveyed again to zebra */
5514 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5520 * bgp_evpn_local_es_del
5522 int bgp_evpn_local_es_del(struct bgp
*bgp
,
5524 struct ipaddr
*originator_ip
)
5526 char buf
[ESI_STR_LEN
];
5527 struct evpnes
*es
= NULL
;
5529 if (!bgp
->esihash
) {
5530 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5535 /* Lookup ESI hash - should exist. */
5536 es
= bgp_evpn_lookup_es(bgp
, esi
);
5538 flog_warn(EC_BGP_EVPN_ESI
,
5539 "%u: ESI hash entry for ESI %s at Local ES DEL",
5540 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5544 /* Delete all local EVPN ES routes from ESI table
5545 * and schedule for processing (to withdraw from peers))
5547 delete_routes_for_es(bgp
, es
);
5549 /* free the hash entry */
5550 bgp_evpn_es_free(bgp
, es
);
5556 * bgp_evpn_local_es_add
5558 int bgp_evpn_local_es_add(struct bgp
*bgp
,
5560 struct ipaddr
*originator_ip
)
5562 char buf
[ESI_STR_LEN
];
5563 struct evpnes
*es
= NULL
;
5564 struct prefix_evpn p
;
5566 if (!bgp
->esihash
) {
5567 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5572 /* create the new es */
5573 es
= bgp_evpn_lookup_es(bgp
, esi
);
5575 es
= bgp_evpn_es_new(bgp
, esi
, originator_ip
);
5579 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
5580 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5584 UNSET_FLAG(es
->flags
, EVPNES_REMOTE
);
5585 SET_FLAG(es
->flags
, EVPNES_LOCAL
);
5587 build_evpn_type4_prefix(&p
, esi
, originator_ip
->ipaddr_v4
);
5588 if (update_evpn_type4_route(bgp
, es
, &p
)) {
5589 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5590 "%u: Type4 route creation failure for ESI %s",
5591 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5595 /* import all remote ES routes in th ES table */
5596 install_routes_for_es(bgp
, es
);
5602 * Cleanup EVPN information on disable - Need to delete and withdraw
5603 * EVPN routes from peers.
5605 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5607 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
5608 void *))cleanup_vni_on_disable
,
5613 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5614 * BGP instance (default) is being freed.
5616 void bgp_evpn_cleanup(struct bgp
*bgp
)
5618 hash_iterate(bgp
->vnihash
,
5619 (void (*)(struct hash_backet
*, void *))free_vni_entry
,
5622 hash_free(bgp
->import_rt_hash
);
5623 bgp
->import_rt_hash
= NULL
;
5625 hash_free(bgp
->vrf_import_rt_hash
);
5626 bgp
->vrf_import_rt_hash
= NULL
;
5628 hash_free(bgp
->vnihash
);
5629 bgp
->vnihash
= NULL
;
5631 hash_free(bgp
->esihash
);
5632 bgp
->esihash
= NULL
;
5634 list_delete(&bgp
->vrf_import_rtl
);
5635 list_delete(&bgp
->vrf_export_rtl
);
5636 list_delete(&bgp
->l2vnis
);
5640 * Initialization for EVPN
5643 * hash for RT to VNI
5645 void bgp_evpn_init(struct bgp
*bgp
)
5648 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
5650 hash_create(esi_hash_keymake
, esi_cmp
,
5651 "BGP EVPN Local ESI Hash");
5652 bgp
->import_rt_hash
=
5653 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
5654 "BGP Import RT Hash");
5655 bgp
->vrf_import_rt_hash
=
5656 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
5657 "BGP VRF Import RT Hash");
5658 bgp
->vrf_import_rtl
= list_new();
5659 bgp
->vrf_import_rtl
->cmp
=
5660 (int (*)(void *, void *))evpn_route_target_cmp
;
5661 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
5662 bgp
->vrf_export_rtl
= list_new();
5663 bgp
->vrf_export_rtl
->cmp
=
5664 (int (*)(void *, void *))evpn_route_target_cmp
;
5665 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
5666 bgp
->l2vnis
= list_new();
5667 bgp
->l2vnis
->cmp
= (int (*)(void *, void *))vni_hash_cmp
;
5670 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
5672 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);