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
, afi_t afi
, safi_t safi
,
65 struct bgp_path_info
**ri
);
66 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
72 /* compare two IPV4 VTEP IPs */
73 static int evpn_vtep_ip_cmp(const void *p1
, const void *p2
)
75 const struct in_addr
*ip1
= p1
;
76 const struct in_addr
*ip2
= p2
;
82 return (ip1
->s_addr
== ip2
->s_addr
);
86 * Make hash key for ESI.
88 static unsigned int esi_hash_keymake(void *p
)
90 struct evpnes
*pes
= p
;
91 const void *pnt
= (void *)pes
->esi
.val
;
93 return jhash(pnt
, ESI_BYTES
, 0xa5a5a55a);
99 static int esi_cmp(const void *p1
, const void *p2
)
101 const struct evpnes
*pes1
= p1
;
102 const struct evpnes
*pes2
= p2
;
104 if (pes1
== NULL
&& pes2
== NULL
)
107 if (pes1
== NULL
|| pes2
== NULL
)
110 return (memcmp(pes1
->esi
.val
, pes2
->esi
.val
, ESI_BYTES
) == 0);
116 static unsigned int vni_hash_key_make(void *p
)
118 struct bgpevpn
*vpn
= p
;
119 return (jhash_1word(vpn
->vni
, 0));
123 * Comparison function for vni hash
125 static int vni_hash_cmp(const void *p1
, const void *p2
)
127 const struct bgpevpn
*vpn1
= p1
;
128 const struct bgpevpn
*vpn2
= p2
;
134 return (vpn1
->vni
== vpn2
->vni
);
138 * Make vrf import route target hash key.
140 static unsigned int vrf_import_rt_hash_key_make(void *p
)
142 struct vrf_irt_node
*irt
= p
;
143 char *pnt
= irt
->rt
.val
;
145 return jhash(pnt
, 8, 0x5abc1234);
149 * Comparison function for vrf import rt hash
151 static int vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
153 const struct vrf_irt_node
*irt1
= p1
;
154 const struct vrf_irt_node
*irt2
= p2
;
156 if (irt1
== NULL
&& irt2
== NULL
)
159 if (irt1
== NULL
|| irt2
== NULL
)
162 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
166 * Create a new vrf import_rt in default instance
168 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
170 struct bgp
*bgp_def
= NULL
;
171 struct vrf_irt_node
*irt
;
173 bgp_def
= bgp_get_default();
175 flog_err(EC_BGP_NO_DFLT
,
176 "vrf import rt new - def instance not created yet");
180 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
181 sizeof(struct vrf_irt_node
));
186 irt
->vrfs
= list_new();
189 if (!hash_get(bgp_def
->vrf_import_rt_hash
, irt
, hash_alloc_intern
)) {
190 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
198 * Free the vrf import rt node
200 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
202 struct bgp
*bgp_def
= NULL
;
204 bgp_def
= bgp_get_default();
206 flog_err(EC_BGP_NO_DFLT
,
207 "vrf import rt free - def instance not created yet");
211 hash_release(bgp_def
->vrf_import_rt_hash
, irt
);
212 list_delete(&irt
->vrfs
);
213 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
217 * Function to lookup Import RT node - used to map a RT to set of
218 * VNIs importing routes with that RT.
220 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
222 struct bgp
*bgp_def
= NULL
;
223 struct vrf_irt_node
*irt
;
224 struct vrf_irt_node tmp
;
226 bgp_def
= bgp_get_default();
228 flog_err(EC_BGP_NO_DFLT
,
229 "vrf import rt lookup - def instance not created yet");
233 memset(&tmp
, 0, sizeof(struct vrf_irt_node
));
234 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
235 irt
= hash_lookup(bgp_def
->vrf_import_rt_hash
, &tmp
);
240 * Is specified VRF present on the RT's list of "importing" VRFs?
242 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
, struct bgp
*bgp_vrf
)
244 struct listnode
*node
= NULL
, *nnode
= NULL
;
245 struct bgp
*tmp_bgp_vrf
= NULL
;
247 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
248 if (tmp_bgp_vrf
== bgp_vrf
)
255 * Make import route target hash key.
257 static unsigned int import_rt_hash_key_make(void *p
)
259 struct irt_node
*irt
= p
;
260 char *pnt
= irt
->rt
.val
;
262 return jhash(pnt
, 8, 0xdeadbeef);
266 * Comparison function for import rt hash
268 static int import_rt_hash_cmp(const void *p1
, const void *p2
)
270 const struct irt_node
*irt1
= p1
;
271 const struct irt_node
*irt2
= p2
;
273 if (irt1
== NULL
&& irt2
== NULL
)
276 if (irt1
== NULL
|| irt2
== NULL
)
279 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
283 * Create a new import_rt
285 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
286 struct ecommunity_val
*rt
)
288 struct irt_node
*irt
;
293 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
298 irt
->vnis
= list_new();
301 if (!hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
)) {
302 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
310 * Free the import rt node
312 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
314 hash_release(bgp
->import_rt_hash
, irt
);
315 list_delete(&irt
->vnis
);
316 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
320 * Function to lookup Import RT node - used to map a RT to set of
321 * VNIs importing routes with that RT.
323 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
324 struct ecommunity_val
*rt
)
326 struct irt_node
*irt
;
329 memset(&tmp
, 0, sizeof(struct irt_node
));
330 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
331 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
336 * Is specified VNI present on the RT's list of "importing" VNIs?
338 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
340 struct listnode
*node
, *nnode
;
341 struct bgpevpn
*tmp_vpn
;
343 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
352 * Compare Route Targets.
354 static int evpn_route_target_cmp(struct ecommunity
*ecom1
,
355 struct ecommunity
*ecom2
)
363 if (!ecom1
&& !ecom2
)
366 if (ecom1
->str
&& !ecom2
->str
)
369 if (!ecom1
->str
&& ecom2
->str
)
372 if (!ecom1
->str
&& !ecom2
->str
)
375 return strcmp(ecom1
->str
, ecom2
->str
);
378 static void evpn_xxport_delete_ecomm(void *val
)
380 struct ecommunity
*ecomm
= val
;
381 ecommunity_free(&ecomm
);
385 * Mask off global-admin field of specified extended community (RT),
386 * just retain the local-admin field.
388 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
389 struct ecommunity_val
*src
)
395 if (type
== ECOMMUNITY_ENCODE_AS
) {
396 dst
->val
[2] = dst
->val
[3] = 0;
397 } else if (type
== ECOMMUNITY_ENCODE_AS4
398 || type
== ECOMMUNITY_ENCODE_IP
) {
399 dst
->val
[2] = dst
->val
[3] = 0;
400 dst
->val
[4] = dst
->val
[5] = 0;
405 * Map one RT to specified VRF.
406 * bgp_vrf = BGP vrf instance
408 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct ecommunity_val
*eval
)
410 struct vrf_irt_node
*irt
= NULL
;
411 struct ecommunity_val eval_tmp
;
413 /* If using "automatic" RT,
414 * we only care about the local-admin sub-field.
415 * This is to facilitate using L3VNI(VRF-VNI)
416 * as the RT for EBGP peering too.
418 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
419 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
420 mask_ecom_global_admin(&eval_tmp
, eval
);
422 irt
= lookup_vrf_import_rt(&eval_tmp
);
423 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
424 /* Already mapped. */
428 irt
= vrf_import_rt_new(&eval_tmp
);
430 /* Add VRF to the list for this RT. */
431 listnode_add(irt
->vrfs
, bgp_vrf
);
435 * Unmap specified VRF from specified RT. If there are no other
436 * VRFs for this RT, then the RT hash is deleted.
437 * bgp_vrf: BGP VRF specific instance
439 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
, struct vrf_irt_node
*irt
)
441 /* Delete VRF from list for this RT. */
442 listnode_delete(irt
->vrfs
, bgp_vrf
);
443 if (!listnode_head(irt
->vrfs
)) {
444 vrf_import_rt_free(irt
);
449 * Map one RT to specified VNI.
451 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
452 struct ecommunity_val
*eval
)
454 struct irt_node
*irt
;
455 struct ecommunity_val eval_tmp
;
457 /* If using "automatic" RT, we only care about the local-admin
459 * This is to facilitate using VNI as the RT for EBGP peering too.
461 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
462 if (!is_import_rt_configured(vpn
))
463 mask_ecom_global_admin(&eval_tmp
, eval
);
465 irt
= lookup_import_rt(bgp
, &eval_tmp
);
467 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
468 /* Already mapped. */
472 irt
= import_rt_new(bgp
, &eval_tmp
);
476 /* Add VNI to the hash list for this RT. */
477 listnode_add(irt
->vnis
, vpn
);
481 * Unmap specified VNI from specified RT. If there are no other
482 * VNIs for this RT, then the RT hash is deleted.
484 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
485 struct irt_node
*irt
)
487 /* Delete VNI from hash list for this RT. */
488 listnode_delete(irt
->vnis
, vpn
);
489 if (!listnode_head(irt
->vnis
)) {
490 import_rt_free(bgp
, irt
);
495 * Create RT extended community automatically from passed information:
496 * of the form AS:VNI.
497 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
498 * the need is to get a RT value that will be unique across different
499 * VNIs but the same across routers (in the same AS) for a particular
502 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
504 struct ecommunity_val eval
;
505 struct ecommunity
*ecomadd
;
507 if (bgp
->advertise_autort_rfc8365
)
508 vni
|= EVPN_AUTORT_VXLAN
;
509 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
511 ecomadd
= ecommunity_new();
512 ecommunity_add_val(ecomadd
, &eval
);
513 listnode_add_sort(rtl
, ecomadd
);
517 * Derive RD and RT for a VNI automatically. Invoked at the time of
520 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
522 bgp_evpn_derive_auto_rd(bgp
, vpn
);
523 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
524 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
528 * Convert nexthop (remote VTEP IP) into an IPv6 address.
530 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
532 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
534 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
535 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
539 * Add (update) or delete MACIP from zebra.
541 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
542 struct prefix_evpn
*p
,
543 struct in_addr remote_vtep_ip
, int add
,
544 uint8_t flags
, uint32_t seq
)
548 char buf1
[ETHER_ADDR_STRLEN
];
549 char buf2
[INET6_ADDRSTRLEN
];
550 char buf3
[INET6_ADDRSTRLEN
];
553 if (!zclient
|| zclient
->sock
< 0)
556 /* Don't try to register if Zebra doesn't know of this instance. */
557 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
563 zclient_create_header(
564 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
566 stream_putl(s
, vpn
->vni
);
567 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
568 /* IP address length and IP address, if any. */
569 if (is_evpn_prefix_ipaddr_none(p
))
572 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
574 stream_putl(s
, ipa_len
);
575 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
577 stream_put_in_addr(s
, &remote_vtep_ip
);
579 /* TX flags - MAC sticky status and/or gateway mac */
580 /* Also TX the sequence number of the best route. */
582 stream_putc(s
, flags
);
586 stream_putw_at(s
, 0, stream_get_endp(s
));
588 if (bgp_debug_zebra(NULL
))
590 "Tx %s MACIP, VNI %u MAC %s IP %s flags 0x%x seq %u remote VTEP %s",
591 add
? "ADD" : "DEL", vpn
->vni
,
592 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
594 ipaddr2str(&p
->prefix
.macip_addr
.ip
,
595 buf3
, sizeof(buf3
)), flags
, seq
,
596 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
599 return zclient_send_message(zclient
);
603 * Add (update) or delete remote VTEP from zebra.
605 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
606 struct prefix_evpn
*p
, int add
)
611 if (!zclient
|| zclient
->sock
< 0)
614 /* Don't try to register if Zebra doesn't know of this instance. */
615 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
621 zclient_create_header(
622 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
624 stream_putl(s
, vpn
->vni
);
625 if (is_evpn_prefix_ipaddr_v4(p
))
626 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
627 else if (is_evpn_prefix_ipaddr_v6(p
)) {
630 "Bad remote IP when trying to %s remote VTEP for VNI %u",
631 add
? "ADD" : "DEL", vpn
->vni
);
635 stream_putw_at(s
, 0, stream_get_endp(s
));
637 if (bgp_debug_zebra(NULL
))
638 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
639 add
? "ADD" : "DEL", vpn
->vni
,
640 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
642 return zclient_send_message(zclient
);
646 * Build extended community for EVPN ES (type-4) route
648 static void build_evpn_type4_route_extcomm(struct evpnes
*es
,
651 struct ecommunity ecom_encap
;
652 struct ecommunity ecom_es_rt
;
653 struct ecommunity_val eval
;
654 struct ecommunity_val eval_es_rt
;
655 bgp_encap_types tnl_type
;
659 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
660 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
661 encode_encap_extcomm(tnl_type
, &eval
);
663 ecom_encap
.val
= (uint8_t *)eval
.val
;
664 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
667 memset(&mac
, 0, sizeof(struct ethaddr
));
668 memset(&ecom_es_rt
, 0, sizeof(ecom_es_rt
));
669 es_get_system_mac(&es
->esi
, &mac
);
670 encode_es_rt_extcomm(&eval_es_rt
, &mac
);
672 ecom_es_rt
.val
= (uint8_t *)eval_es_rt
.val
;
674 ecommunity_merge(attr
->ecommunity
, &ecom_es_rt
);
676 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
680 * Build extended communities for EVPN prefix route.
682 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
685 struct ecommunity ecom_encap
;
686 struct ecommunity ecom_rmac
;
687 struct ecommunity_val eval
;
688 struct ecommunity_val eval_rmac
;
689 bgp_encap_types tnl_type
;
690 struct listnode
*node
, *nnode
;
691 struct ecommunity
*ecom
;
692 struct list
*vrf_export_rtl
= NULL
;
695 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
696 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
697 encode_encap_extcomm(tnl_type
, &eval
);
699 ecom_encap
.val
= (uint8_t *)eval
.val
;
702 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
704 /* Add the export RTs for L3VNI/VRF */
705 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
706 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
708 ecommunity_merge(attr
->ecommunity
, ecom
);
710 /* add the router mac extended community */
711 if (!is_zero_mac(&attr
->rmac
)) {
712 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
713 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
715 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
717 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
720 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
724 * Build extended communities for EVPN route.
725 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
726 * and ENCAP extended communities are applicable for all routes.
727 * The default gateway extended community and MAC mobility (sticky) extended
728 * community are added as needed based on passed settings - only for type-2
729 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
730 * added, if present, based on passed settings - only for non-link-local
733 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
736 struct ecommunity ecom_encap
;
737 struct ecommunity ecom_sticky
;
738 struct ecommunity ecom_default_gw
;
739 struct ecommunity ecom_rmac
;
740 struct ecommunity ecom_na
;
741 struct ecommunity_val eval
;
742 struct ecommunity_val eval_sticky
;
743 struct ecommunity_val eval_default_gw
;
744 struct ecommunity_val eval_rmac
;
745 struct ecommunity_val eval_na
;
747 bgp_encap_types tnl_type
;
748 struct listnode
*node
, *nnode
;
749 struct ecommunity
*ecom
;
751 struct list
*vrf_export_rtl
= NULL
;
754 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
755 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
756 encode_encap_extcomm(tnl_type
, &eval
);
758 ecom_encap
.val
= (uint8_t *)eval
.val
;
761 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
763 /* Add the export RTs for L2VNI */
764 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
765 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
767 /* Add the export RTs for L3VNI if told to - caller determines
768 * when this should be done.
771 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
772 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
773 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
775 attr
->ecommunity
= ecommunity_merge(
776 attr
->ecommunity
, ecom
);
780 /* Add MAC mobility (sticky) if needed. */
783 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
784 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
785 ecom_sticky
.size
= 1;
786 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
788 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
791 /* Add RMAC, if told to. */
793 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
794 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
796 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
798 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
801 /* Add default gateway, if needed. */
802 if (attr
->default_gw
) {
803 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
804 encode_default_gw_extcomm(&eval_default_gw
);
805 ecom_default_gw
.size
= 1;
806 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
808 ecommunity_merge(attr
->ecommunity
, &ecom_default_gw
);
811 if (attr
->router_flag
) {
812 memset(&ecom_na
, 0, sizeof(ecom_na
));
813 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
);
815 ecom_na
.val
= (uint8_t *)eval_na
.val
;
816 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
820 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
824 * Add MAC mobility extended community to attribute.
826 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
828 struct ecommunity ecom_tmp
;
829 struct ecommunity_val eval
;
830 uint8_t *ecom_val_ptr
;
837 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
839 /* Find current MM ecommunity */
842 if (attr
->ecommunity
) {
843 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
844 pnt
= attr
->ecommunity
->val
+ (i
* 8);
848 if (type
== ECOMMUNITY_ENCODE_EVPN
850 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
851 ecom_val_ptr
= (uint8_t *)(attr
->ecommunity
->val
858 /* Update the existing MM ecommunity */
860 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
862 /* Add MM to existing */
864 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
866 ecom_tmp
.val
= (uint8_t *)eval
.val
;
868 if (attr
->ecommunity
)
870 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
872 attr
->ecommunity
= ecommunity_dup(&ecom_tmp
);
876 /* Install EVPN route into zebra. */
877 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
878 struct prefix_evpn
*p
,
879 struct in_addr remote_vtep_ip
, uint8_t flags
,
884 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
885 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
888 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 1);
893 /* Uninstall EVPN route from zebra. */
894 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
895 struct prefix_evpn
*p
,
896 struct in_addr remote_vtep_ip
)
900 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
901 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
904 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 0);
910 * Due to MAC mobility, the prior "local" best route has been supplanted
911 * by a "remote" best route. The prior route has to be deleted and withdrawn
914 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
916 struct bgp_path_info
*old_local
)
918 struct bgp_node
*global_rn
;
919 struct bgp_path_info
*ri
;
920 afi_t afi
= AFI_L2VPN
;
921 safi_t safi
= SAFI_EVPN
;
923 /* Locate route node in the global EVPN routing table. Note that
924 * this table is a 2-level tree (RD-level + Prefix-level) similar to
927 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
928 (struct prefix
*)&rn
->p
, &vpn
->prd
);
930 /* Delete route entry in the global EVPN table. */
931 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &ri
);
933 /* Schedule for processing - withdraws to peers happen from
937 bgp_process(bgp
, global_rn
, afi
, safi
);
938 bgp_unlock_node(global_rn
);
941 /* Delete route entry in the VNI route table, caller to remove. */
942 bgp_path_info_delete(rn
, old_local
);
945 static struct in_addr
*es_vtep_new(struct in_addr vtep
)
949 ip
= XCALLOC(MTYPE_BGP_EVPN_ES_VTEP
, sizeof(struct in_addr
));
953 ip
->s_addr
= vtep
.s_addr
;
957 static void es_vtep_free(struct in_addr
*ip
)
959 XFREE(MTYPE_BGP_EVPN_ES_VTEP
, ip
);
962 /* check if VTEP is already part of the list */
963 static int is_vtep_present_in_list(struct list
*list
,
966 struct listnode
*node
= NULL
;
969 for (ALL_LIST_ELEMENTS_RO(list
, node
, tmp
)) {
970 if (tmp
->s_addr
== vtep
.s_addr
)
977 * Best path for ES route was changed,
978 * update the list of VTEPs for this ES
980 static int evpn_es_install_vtep(struct bgp
*bgp
,
982 struct prefix_evpn
*p
,
983 struct in_addr rvtep
)
985 struct in_addr
*vtep_ip
;
987 if (is_vtep_present_in_list(es
->vtep_list
, rvtep
))
991 vtep_ip
= es_vtep_new(rvtep
);
993 listnode_add_sort(es
->vtep_list
, vtep_ip
);
998 * Best path for ES route was changed,
999 * update the list of VTEPs for this ES
1001 static int evpn_es_uninstall_vtep(struct bgp
*bgp
,
1003 struct prefix_evpn
*p
,
1004 struct in_addr rvtep
)
1006 struct listnode
*node
, *nnode
, *node_to_del
= NULL
;
1007 struct in_addr
*tmp
;
1009 for (ALL_LIST_ELEMENTS(es
->vtep_list
, node
, nnode
, tmp
)) {
1010 if (tmp
->s_addr
== rvtep
.s_addr
) {
1017 list_delete_node(es
->vtep_list
, node_to_del
);
1023 * Calculate the best path for a ES(type-4) route.
1025 static int evpn_es_route_select_install(struct bgp
*bgp
,
1027 struct bgp_node
*rn
)
1030 afi_t afi
= AFI_L2VPN
;
1031 safi_t safi
= SAFI_EVPN
;
1032 struct bgp_path_info
*old_select
; /* old best */
1033 struct bgp_path_info
*new_select
; /* new best */
1034 struct bgp_path_info_pair old_and_new
;
1036 /* Compute the best path. */
1037 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
],
1038 &old_and_new
, afi
, safi
);
1039 old_select
= old_and_new
.old
;
1040 new_select
= old_and_new
.new;
1043 * If the best path hasn't changed - see if something needs to be
1046 if (old_select
&& old_select
== new_select
1047 && old_select
->type
== ZEBRA_ROUTE_BGP
1048 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1049 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1050 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1051 && !bgp
->addpath_tx_used
[afi
][safi
]) {
1052 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1053 ret
= evpn_es_install_vtep(bgp
, es
,
1054 (struct prefix_evpn
*)&rn
->p
,
1055 old_select
->attr
->nexthop
);
1057 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1058 bgp_zebra_clear_route_change_flags(rn
);
1062 /* If the user did a "clear" this flag will be set */
1063 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1066 * bestpath has changed; update relevant fields and install or uninstall
1067 * into the zebra RIB.
1069 if (old_select
|| new_select
)
1070 bgp_bump_version(rn
);
1073 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1075 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1076 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1077 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1080 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1081 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1082 ret
= evpn_es_install_vtep(bgp
, es
,
1083 (struct prefix_evpn
*)&rn
->p
,
1084 new_select
->attr
->nexthop
);
1086 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1087 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1088 ret
= evpn_es_uninstall_vtep(
1089 bgp
, es
, (struct prefix_evpn
*)&rn
->p
,
1090 old_select
->attr
->nexthop
);
1093 /* Clear any route change flags. */
1094 bgp_zebra_clear_route_change_flags(rn
);
1096 /* Reap old select bgp_path_info, if it has been removed */
1097 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1098 bgp_path_info_reap(rn
, old_select
);
1104 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1107 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1108 struct bgp_node
*rn
)
1110 struct bgp_path_info
*old_select
, *new_select
;
1111 struct bgp_path_info_pair old_and_new
;
1112 struct prefix_evpn
*evp
;
1113 afi_t afi
= AFI_L2VPN
;
1114 safi_t safi
= SAFI_EVPN
;
1118 /* Compute the best path. */
1119 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1121 old_select
= old_and_new
.old
;
1122 new_select
= old_and_new
.new;
1124 evp
= (struct prefix_evpn
*)&rn
->p
;
1125 /* If the best path hasn't changed - see if there is still something to
1129 if (old_select
&& old_select
== new_select
1130 && old_select
->type
== ZEBRA_ROUTE_BGP
1131 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1132 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1133 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1134 && !bgp
->addpath_tx_used
[afi
][safi
]) {
1135 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1136 if (old_select
->attr
->sticky
)
1137 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
1138 if (old_select
->attr
->default_gw
)
1139 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
1140 if (is_evpn_prefix_ipaddr_v6(evp
) &&
1141 old_select
->attr
->router_flag
)
1142 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
1144 ret
= evpn_zebra_install(
1145 bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1146 old_select
->attr
->nexthop
, flags
,
1147 mac_mobility_seqnum(old_select
->attr
));
1149 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1150 bgp_zebra_clear_route_change_flags(rn
);
1154 /* If the user did a "clear" this flag will be set */
1155 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1157 /* bestpath has changed; update relevant fields and install or uninstall
1158 * into the zebra RIB.
1160 if (old_select
|| new_select
)
1161 bgp_bump_version(rn
);
1164 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1166 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1167 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1168 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1171 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1172 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1174 if (new_select
->attr
->sticky
)
1175 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
1176 if (new_select
->attr
->default_gw
)
1177 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
1178 if (is_evpn_prefix_ipaddr_v6(evp
) &&
1179 new_select
->attr
->router_flag
)
1180 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
1182 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1183 new_select
->attr
->nexthop
, flags
,
1184 mac_mobility_seqnum(new_select
->attr
));
1185 /* If an old best existed and it was a "local" route, the only
1187 * it would be supplanted is due to MAC mobility procedures. So,
1189 * need to do an implicit delete and withdraw that route from
1192 if (old_select
&& old_select
->peer
== bgp
->peer_self
1193 && old_select
->type
== ZEBRA_ROUTE_BGP
1194 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
1195 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
1197 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1198 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1199 ret
= evpn_zebra_uninstall(bgp
, vpn
,
1200 (struct prefix_evpn
*)&rn
->p
,
1201 old_select
->attr
->nexthop
);
1204 /* Clear any route change flags. */
1205 bgp_zebra_clear_route_change_flags(rn
);
1207 /* Reap old select bgp_path_info, if it has been removed */
1208 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1209 bgp_path_info_reap(rn
, old_select
);
1215 * Return true if the local ri for this rn is of type gateway mac
1217 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
1219 struct bgp_path_info
*tmp_ri
= NULL
;
1220 struct bgp_path_info
*local_ri
= NULL
;
1223 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1224 if (tmp_ri
->peer
== bgp
->peer_self
1225 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1226 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1233 return local_ri
->attr
->default_gw
;
1238 * Return true if the local ri for this rn has sticky set
1240 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
1242 struct bgp_path_info
*tmp_ri
;
1243 struct bgp_path_info
*local_ri
;
1246 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1247 if (tmp_ri
->peer
== bgp
->peer_self
1248 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1249 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1256 return local_ri
->attr
->sticky
;
1260 * create or update EVPN type4 route entry.
1261 * This could be in the ES table or the global table.
1262 * TODO: handle remote ES (type4) routes as well
1264 static int update_evpn_type4_route_entry(struct bgp
*bgp
, struct evpnes
*es
,
1265 afi_t afi
, safi_t safi
,
1266 struct bgp_node
*rn
, struct attr
*attr
,
1267 int add
, struct bgp_path_info
**ri
,
1270 char buf
[ESI_STR_LEN
];
1271 char buf1
[INET6_ADDRSTRLEN
];
1272 struct bgp_path_info
*tmp_ri
= NULL
;
1273 struct bgp_path_info
*local_ri
= NULL
; /* local route entry if any */
1274 struct bgp_path_info
*remote_ri
= NULL
; /* remote route entry if any */
1275 struct attr
*attr_new
= NULL
;
1276 struct prefix_evpn
*evp
= NULL
;
1280 evp
= (struct prefix_evpn
*)&rn
->p
;
1282 /* locate the local and remote entries if any */
1283 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1284 if (tmp_ri
->peer
== bgp
->peer_self
&&
1285 tmp_ri
->type
== ZEBRA_ROUTE_BGP
&&
1286 tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1288 if (tmp_ri
->type
== ZEBRA_ROUTE_BGP
1289 && tmp_ri
->sub_type
== BGP_ROUTE_IMPORTED
1290 && CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_VALID
))
1294 /* we don't expect to see a remote_ri at this point.
1295 * An ES route has esi + vtep_ip as the key,
1296 * We shouldn't see the same route from any other vtep.
1301 "%u ERROR: local es route for ESI: %s Vtep %s also learnt from remote",
1303 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1304 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1308 if (!local_ri
&& !add
)
1311 /* create or update the entry */
1314 /* Add or update attribute to hash */
1315 attr_new
= bgp_attr_intern(attr
);
1317 /* Create new route with its attribute. */
1318 tmp_ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
,
1319 0, bgp
->peer_self
, attr_new
, rn
);
1320 SET_FLAG(tmp_ri
->flags
, BGP_PATH_VALID
);
1322 /* add the newly created path to the route-node */
1323 bgp_path_info_add(rn
, tmp_ri
);
1326 if (attrhash_cmp(tmp_ri
->attr
, attr
)
1327 && !CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_REMOVED
))
1330 /* The attribute has changed.
1331 * Add (or update) attribute to hash. */
1332 attr_new
= bgp_attr_intern(attr
);
1333 bgp_path_info_set_flag(rn
, tmp_ri
,
1334 BGP_PATH_ATTR_CHANGED
);
1336 /* Restore route, if needed. */
1337 if (CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_REMOVED
))
1338 bgp_path_info_restore(rn
, tmp_ri
);
1340 /* Unintern existing, set to new. */
1341 bgp_attr_unintern(&tmp_ri
->attr
);
1342 tmp_ri
->attr
= attr_new
;
1343 tmp_ri
->uptime
= bgp_clock();
1347 /* Return back the route entry. */
1352 /* update evpn es (type-4) route */
1353 static int update_evpn_type4_route(struct bgp
*bgp
,
1355 struct prefix_evpn
*p
)
1358 int route_changed
= 0;
1359 char buf
[ESI_STR_LEN
];
1360 char buf1
[INET6_ADDRSTRLEN
];
1361 afi_t afi
= AFI_L2VPN
;
1362 safi_t safi
= SAFI_EVPN
;
1364 struct attr
*attr_new
= NULL
;
1365 struct bgp_node
*rn
= NULL
;
1366 struct bgp_path_info
*ri
= NULL
;
1368 memset(&attr
, 0, sizeof(struct attr
));
1370 /* Build path-attribute for this route. */
1371 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1372 attr
.nexthop
= es
->originator_ip
.ipaddr_v4
;
1373 attr
.mp_nexthop_global_in
= es
->originator_ip
.ipaddr_v4
;
1374 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1376 /* Set up extended community. */
1377 build_evpn_type4_route_extcomm(es
, &attr
);
1379 /* First, create (or fetch) route node within the ESI. */
1380 /* NOTE: There is no RD here. */
1381 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
1383 /* Create or update route entry. */
1384 ret
= update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
,
1388 flog_err(EC_BGP_ES_INVALID
,
1389 "%u ERROR: Failed to updated ES route ESI: %s VTEP %s",
1391 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1392 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1396 attr_new
= ri
->attr
;
1398 /* Perform route selection;
1399 * this is just to set the flags correctly
1400 * as local route in the ES always wins.
1402 evpn_es_route_select_install(bgp
, es
, rn
);
1403 bgp_unlock_node(rn
);
1405 /* If this is a new route or some attribute has changed, export the
1406 * route to the global table. The route will be advertised to peers
1407 * from there. Note that this table is a 2-level tree (RD-level +
1408 * Prefix-level) similar to L3VPN routes.
1410 if (route_changed
) {
1411 struct bgp_path_info
*global_ri
;
1413 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1414 (struct prefix
*)p
, &es
->prd
);
1415 update_evpn_type4_route_entry(bgp
, es
, afi
, safi
,
1420 /* Schedule for processing and unlock node. */
1421 bgp_process(bgp
, rn
, afi
, safi
);
1422 bgp_unlock_node(rn
);
1425 /* Unintern temporary. */
1426 aspath_unintern(&attr
.aspath
);
1430 static int update_evpn_type5_route_entry(struct bgp
*bgp_def
,
1431 struct bgp
*bgp_vrf
, afi_t afi
,
1432 safi_t safi
, struct bgp_node
*rn
,
1433 struct attr
*attr
, int *route_changed
)
1435 struct attr
*attr_new
= NULL
;
1436 struct bgp_path_info
*ri
= NULL
;
1437 mpls_label_t label
= MPLS_INVALID_LABEL
;
1438 struct bgp_path_info
*local_ri
= NULL
;
1439 struct bgp_path_info
*tmp_ri
= NULL
;
1442 /* locate the local route entry if any */
1443 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1444 if (tmp_ri
->peer
== bgp_def
->peer_self
1445 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1446 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1451 * create a new route entry if one doesnt exist.
1452 * Otherwise see if route attr has changed
1456 /* route has changed as this is the first entry */
1459 /* Add (or update) attribute to hash. */
1460 attr_new
= bgp_attr_intern(attr
);
1462 /* create the route info from attribute */
1463 ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1464 bgp_def
->peer_self
, attr_new
, rn
);
1465 SET_FLAG(ri
->flags
, BGP_PATH_VALID
);
1467 /* Type-5 routes advertise the L3-VNI */
1468 bgp_path_info_extra_get(ri
);
1469 vni2label(bgp_vrf
->l3vni
, &label
);
1470 memcpy(&ri
->extra
->label
, &label
, sizeof(label
));
1471 ri
->extra
->num_labels
= 1;
1473 /* add the route entry to route node*/
1474 bgp_path_info_add(rn
, ri
);
1478 if (!attrhash_cmp(tmp_ri
->attr
, attr
)) {
1480 /* attribute changed */
1483 /* The attribute has changed. */
1484 /* Add (or update) attribute to hash. */
1485 attr_new
= bgp_attr_intern(attr
);
1486 bgp_path_info_set_flag(rn
, tmp_ri
,
1487 BGP_PATH_ATTR_CHANGED
);
1489 /* Restore route, if needed. */
1490 if (CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_REMOVED
))
1491 bgp_path_info_restore(rn
, tmp_ri
);
1493 /* Unintern existing, set to new. */
1494 bgp_attr_unintern(&tmp_ri
->attr
);
1495 tmp_ri
->attr
= attr_new
;
1496 tmp_ri
->uptime
= bgp_clock();
1502 /* update evpn type-5 route entry */
1503 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1504 struct attr
*src_attr
)
1506 afi_t afi
= AFI_L2VPN
;
1507 safi_t safi
= SAFI_EVPN
;
1509 struct bgp_node
*rn
= NULL
;
1510 struct bgp
*bgp_def
= NULL
;
1511 int route_changed
= 0;
1513 bgp_def
= bgp_get_default();
1517 /* Build path attribute for this route - use the source attr, if
1518 * present, else treat as locally originated.
1521 bgp_attr_dup(&attr
, src_attr
);
1523 memset(&attr
, 0, sizeof(struct attr
));
1524 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1526 /* Set nexthop to ourselves and fill in the Router MAC. */
1527 attr
.nexthop
= bgp_vrf
->originator_ip
;
1528 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1529 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1530 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
1532 /* Setup RT and encap extended community */
1533 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1535 /* get the route node in global table */
1536 rn
= bgp_afi_node_get(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1537 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1540 /* create or update the route entry within the route node */
1541 update_evpn_type5_route_entry(bgp_def
, bgp_vrf
, afi
, safi
, rn
, &attr
,
1544 /* schedule for processing and unlock node */
1545 if (route_changed
) {
1546 bgp_process(bgp_def
, rn
, afi
, safi
);
1547 bgp_unlock_node(rn
);
1550 /* uninten temporary */
1552 aspath_unintern(&attr
.aspath
);
1557 * Create or update EVPN route entry. This could be in the VNI route table
1558 * or the global route table.
1560 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1561 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1562 struct attr
*attr
, int add
,
1563 struct bgp_path_info
**ri
, uint8_t flags
,
1566 struct bgp_path_info
*tmp_ri
;
1567 struct bgp_path_info
*local_ri
;
1568 struct attr
*attr_new
;
1569 mpls_label_t label
[BGP_MAX_LABELS
];
1570 uint32_t num_labels
= 1;
1571 int route_change
= 1;
1573 struct prefix_evpn
*evp
;
1576 evp
= (struct prefix_evpn
*)&rn
->p
;
1577 memset(&label
, 0, sizeof(label
));
1579 /* See if this is an update of an existing route, or a new add. */
1581 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1582 if (tmp_ri
->peer
== bgp
->peer_self
1583 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1584 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1588 /* If route doesn't exist already, create a new one, if told to.
1589 * Otherwise act based on whether the attributes of the route have
1592 if (!local_ri
&& !add
)
1595 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1596 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1597 add_mac_mobility_to_attr(seq
, attr
);
1600 /* Add (or update) attribute to hash. */
1601 attr_new
= bgp_attr_intern(attr
);
1603 /* Extract MAC mobility sequence number, if any. */
1604 attr_new
->mm_seqnum
=
1605 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1606 attr_new
->sticky
= sticky
;
1608 /* Create new route with its attribute. */
1609 tmp_ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1610 bgp
->peer_self
, attr_new
, rn
);
1611 SET_FLAG(tmp_ri
->flags
, BGP_PATH_VALID
);
1612 bgp_path_info_extra_get(tmp_ri
);
1614 /* The VNI goes into the 'label' field of the route */
1615 vni2label(vpn
->vni
, &label
[0]);
1617 /* Type-2 routes may carry a second VNI - the L3-VNI.
1618 * Only attach second label if we are advertising two labels for
1621 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1622 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1625 l3vni
= bgpevpn_get_l3vni(vpn
);
1627 vni2label(l3vni
, &label
[1]);
1632 memcpy(&tmp_ri
->extra
->label
, label
, sizeof(label
));
1633 tmp_ri
->extra
->num_labels
= num_labels
;
1634 bgp_path_info_add(rn
, tmp_ri
);
1637 if (attrhash_cmp(tmp_ri
->attr
, attr
)
1638 && !CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_REMOVED
))
1642 * The attributes have changed, type-2 routes needs to
1643 * be advertised with right labels.
1645 vni2label(vpn
->vni
, &label
[0]);
1646 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1647 && CHECK_FLAG(vpn
->flags
,
1648 VNI_FLAG_USE_TWO_LABELS
)) {
1651 l3vni
= bgpevpn_get_l3vni(vpn
);
1653 vni2label(l3vni
, &label
[1]);
1657 memcpy(&tmp_ri
->extra
->label
, label
, sizeof(label
));
1658 tmp_ri
->extra
->num_labels
= num_labels
;
1660 /* The attribute has changed. */
1661 /* Add (or update) attribute to hash. */
1662 attr_new
= bgp_attr_intern(attr
);
1663 bgp_path_info_set_flag(rn
, tmp_ri
,
1664 BGP_PATH_ATTR_CHANGED
);
1666 /* Extract MAC mobility sequence number, if any. */
1667 attr_new
->mm_seqnum
=
1668 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1669 attr_new
->sticky
= sticky
;
1671 /* Restore route, if needed. */
1672 if (CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_REMOVED
))
1673 bgp_path_info_restore(rn
, tmp_ri
);
1675 /* Unintern existing, set to new. */
1676 bgp_attr_unintern(&tmp_ri
->attr
);
1677 tmp_ri
->attr
= attr_new
;
1678 tmp_ri
->uptime
= bgp_clock();
1682 /* Return back the route entry. */
1684 return route_change
;
1688 * Create or update EVPN route (of type based on prefix) for specified VNI
1689 * and schedule for processing.
1691 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1692 struct prefix_evpn
*p
, uint8_t flags
,
1695 struct bgp_node
*rn
;
1697 struct attr
*attr_new
;
1698 int add_l3_ecomm
= 0;
1699 struct bgp_path_info
*ri
;
1700 afi_t afi
= AFI_L2VPN
;
1701 safi_t safi
= SAFI_EVPN
;
1704 memset(&attr
, 0, sizeof(struct attr
));
1706 /* Build path-attribute for this route. */
1707 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1708 attr
.nexthop
= vpn
->originator_ip
;
1709 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1710 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1711 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1712 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1713 attr
.router_flag
= CHECK_FLAG(flags
,
1714 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1716 /* PMSI is only needed for type-3 routes */
1717 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
)
1718 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1720 /* router mac is only needed for type-2 routes here. */
1721 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1722 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1723 vni2label(vpn
->vni
, &(attr
.label
));
1725 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1726 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1729 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1730 (is_evpn_prefix_ipaddr_v4(p
) ||
1731 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1732 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1733 bgpevpn_get_l3vni(vpn
))
1736 /* Set up extended community. */
1737 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1739 /* First, create (or fetch) route node within the VNI. */
1740 /* NOTE: There is no RD here. */
1741 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1743 /* Create or update route entry. */
1744 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1745 1, &ri
, flags
, seq
);
1747 attr_new
= ri
->attr
;
1749 /* Perform route selection; this is just to set the flags correctly
1750 * as local route in the VNI always wins.
1752 evpn_route_select_install(bgp
, vpn
, rn
);
1753 bgp_unlock_node(rn
);
1755 /* If this is a new route or some attribute has changed, export the
1756 * route to the global table. The route will be advertised to peers
1757 * from there. Note that this table is a 2-level tree (RD-level +
1758 * Prefix-level) similar to L3VPN routes.
1761 struct bgp_path_info
*global_ri
;
1763 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1764 (struct prefix
*)p
, &vpn
->prd
);
1765 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1,
1766 &global_ri
, flags
, seq
);
1768 /* Schedule for processing and unlock node. */
1769 bgp_process(bgp
, rn
, afi
, safi
);
1770 bgp_unlock_node(rn
);
1773 /* Unintern temporary. */
1774 aspath_unintern(&attr
.aspath
);
1780 * Delete EVPN route entry.
1781 * The entry can be in ESI/VNI table or the global table.
1783 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1784 struct bgp_node
*rn
,
1785 struct bgp_path_info
**ri
)
1787 struct bgp_path_info
*tmp_ri
;
1791 /* Now, find matching route. */
1792 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
)
1793 if (tmp_ri
->peer
== bgp
->peer_self
1794 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1795 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1800 /* Mark route for delete. */
1802 bgp_path_info_delete(rn
, tmp_ri
);
1807 /* Delete EVPN ES (type-4) route */
1808 static int delete_evpn_type4_route(struct bgp
*bgp
,
1810 struct prefix_evpn
*p
)
1812 afi_t afi
= AFI_L2VPN
;
1813 safi_t safi
= SAFI_EVPN
;
1814 struct bgp_path_info
*ri
;
1815 struct bgp_node
*rn
= NULL
; /* rn in esi table */
1816 struct bgp_node
*global_rn
= NULL
; /* rn in global table */
1818 /* First, locate the route node within the ESI.
1819 * If it doesn't exist, ther is nothing to do.
1820 * Note: there is no RD here.
1822 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
1826 /* Next, locate route node in the global EVPN routing table.
1827 * Note that this table is a 2-level tree (RD-level + Prefix-level)
1829 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1830 (struct prefix
*)p
, &es
->prd
);
1833 /* Delete route entry in the global EVPN table. */
1834 delete_evpn_route_entry(bgp
, afi
, safi
,
1837 /* Schedule for processing - withdraws to peers happen from
1841 bgp_process(bgp
, global_rn
, afi
, safi
);
1842 bgp_unlock_node(global_rn
);
1846 * Delete route entry in the ESI route table.
1847 * This can just be removed.
1849 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &ri
);
1851 bgp_path_info_reap(rn
, ri
);
1852 bgp_unlock_node(rn
);
1856 /* Delete EVPN type5 route */
1857 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1859 afi_t afi
= AFI_L2VPN
;
1860 safi_t safi
= SAFI_EVPN
;
1861 struct bgp_node
*rn
= NULL
;
1862 struct bgp_path_info
*ri
= NULL
;
1863 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
1865 bgp_def
= bgp_get_default();
1869 /* locate the global route entry for this type-5 prefix */
1870 rn
= bgp_afi_node_lookup(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1871 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1875 delete_evpn_route_entry(bgp_def
, afi
, safi
, rn
, &ri
);
1877 bgp_process(bgp_def
, rn
, afi
, safi
);
1878 bgp_unlock_node(rn
);
1883 * Delete EVPN route (of type based on prefix) for specified VNI and
1884 * schedule for processing.
1886 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1887 struct prefix_evpn
*p
)
1889 struct bgp_node
*rn
, *global_rn
;
1890 struct bgp_path_info
*ri
;
1891 afi_t afi
= AFI_L2VPN
;
1892 safi_t safi
= SAFI_EVPN
;
1894 /* First, locate the route node within the VNI. If it doesn't exist,
1896 * is nothing further to do.
1898 /* NOTE: There is no RD here. */
1899 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1903 /* Next, locate route node in the global EVPN routing table. Note that
1904 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1907 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1908 (struct prefix
*)p
, &vpn
->prd
);
1910 /* Delete route entry in the global EVPN table. */
1911 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &ri
);
1913 /* Schedule for processing - withdraws to peers happen from
1917 bgp_process(bgp
, global_rn
, afi
, safi
);
1918 bgp_unlock_node(global_rn
);
1921 /* Delete route entry in the VNI route table. This can just be removed.
1923 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &ri
);
1925 bgp_path_info_reap(rn
, ri
);
1926 bgp_unlock_node(rn
);
1932 * Update all type-2 (MACIP) local routes for this VNI - these should also
1933 * be scheduled for advertise to peers.
1935 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1939 struct bgp_node
*rn
;
1940 struct bgp_path_info
*ri
, *tmp_ri
;
1942 struct attr
*attr_new
;
1944 int add_l3_ecomm
= 0;
1949 /* Walk this VNI's route table and update local type-2 routes. For any
1950 * routes updated, update corresponding entry in the global table too.
1952 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1953 rn
= bgp_route_next(rn
)) {
1954 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1955 struct bgp_node
*rd_rn
;
1956 struct bgp_path_info
*global_ri
;
1958 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1961 /* Identify local route. */
1962 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1963 if (tmp_ri
->peer
== bgp
->peer_self
1964 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1965 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1973 * Build attribute per local route as the MAC mobility and
1974 * some other values could differ for different routes. The
1975 * attributes will be shared in the hash table.
1977 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1978 attr
.nexthop
= vpn
->originator_ip
;
1979 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1980 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1981 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1983 if (evpn_route_is_sticky(bgp
, rn
))
1985 else if (evpn_route_is_def_gw(bgp
, rn
)) {
1986 attr
.default_gw
= 1;
1987 if (is_evpn_prefix_ipaddr_v6(evp
))
1988 attr
.router_flag
= 1;
1991 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
1992 * using L3 VNI for type-2 routes also.
1994 if ((is_evpn_prefix_ipaddr_v4(evp
) ||
1995 !IN6_IS_ADDR_LINKLOCAL(
1996 &evp
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1997 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1998 bgpevpn_get_l3vni(vpn
))
2001 /* Set up extended community. */
2002 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2004 seq
= mac_mobility_seqnum(tmp_ri
->attr
);
2006 /* Update the route entry. */
2007 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
2008 &attr
, 0, &ri
, 0, seq
);
2010 /* Perform route selection; this is just to set the flags
2011 * correctly as local route in the VNI always wins.
2013 evpn_route_select_install(bgp
, vpn
, rn
);
2015 attr_new
= ri
->attr
;
2017 /* Update route in global routing table. */
2018 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2019 (struct prefix
*)evp
, &vpn
->prd
);
2021 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
2023 mac_mobility_seqnum(attr_new
));
2025 /* Schedule for processing and unlock node. */
2026 bgp_process(bgp
, rd_rn
, afi
, safi
);
2027 bgp_unlock_node(rd_rn
);
2029 /* Unintern temporary. */
2030 aspath_unintern(&attr
.aspath
);
2038 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2039 * global routing table. These are also scheduled for withdraw from peers.
2041 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2045 struct bgp_node
*rdrn
, *rn
;
2046 struct bgp_table
*table
;
2047 struct bgp_path_info
*ri
;
2052 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
2053 if (rdrn
&& rdrn
->info
) {
2054 table
= (struct bgp_table
*)rdrn
->info
;
2055 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2056 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2058 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2061 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &ri
);
2063 bgp_process(bgp
, rn
, afi
, safi
);
2067 /* Unlock RD node. */
2069 bgp_unlock_node(rdrn
);
2075 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2076 * table as well as the per-VNI route table.
2078 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2082 struct bgp_node
*rn
;
2083 struct bgp_path_info
*ri
;
2088 /* First, walk the global route table for this VNI's type-2 local
2090 * EVPN routes are a 2-level table, first get the RD table.
2092 delete_global_type2_routes(bgp
, vpn
);
2094 /* Next, walk this VNI's route table and delete local type-2 routes. */
2095 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2096 rn
= bgp_route_next(rn
)) {
2097 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2099 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2102 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &ri
);
2104 /* Route entry in local table gets deleted immediately. */
2106 bgp_path_info_reap(rn
, ri
);
2113 * Delete all routes in per ES route-table
2115 static int delete_all_es_routes(struct bgp
*bgp
, struct evpnes
*es
)
2117 struct bgp_node
*rn
;
2118 struct bgp_path_info
*ri
, *nextri
;
2120 /* Walk this ES's route table and delete all routes. */
2121 for (rn
= bgp_table_top(es
->route_table
); rn
;
2122 rn
= bgp_route_next(rn
)) {
2123 for (ri
= rn
->info
; (ri
!= NULL
) && (nextri
= ri
->next
, 1);
2125 bgp_path_info_delete(rn
, ri
);
2126 bgp_path_info_reap(rn
, ri
);
2134 * Delete all routes in the per-VNI route table.
2136 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2138 struct bgp_node
*rn
;
2139 struct bgp_path_info
*ri
, *nextri
;
2141 /* Walk this VNI's route table and delete all routes. */
2142 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2143 rn
= bgp_route_next(rn
)) {
2144 for (ri
= rn
->info
; (ri
!= NULL
) && (nextri
= ri
->next
, 1);
2146 bgp_path_info_delete(rn
, ri
);
2147 bgp_path_info_reap(rn
, ri
);
2155 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2156 * export RT getting modified or change to tunnel IP. Note that these
2157 * situations need the route in the per-VNI table as well as the global
2158 * table to be updated (as attributes change).
2160 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2163 struct prefix_evpn p
;
2165 /* Update and advertise the type-3 route (only one) followed by the
2166 * locally learnt type-2 routes (MACIP) - for this VNI.
2168 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2169 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0);
2173 return update_all_type2_routes(bgp
, vpn
);
2176 /* Delete (and withdraw) local routes for specified ES from global and ES table.
2177 * Also remove all other routes from the per ES table.
2178 * Invoked when ES is deleted.
2180 static int delete_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
2183 char buf
[ESI_STR_LEN
];
2184 struct prefix_evpn p
;
2186 /* Delete and withdraw locally learnt ES route */
2187 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
.ipaddr_v4
);
2188 ret
= delete_evpn_type4_route(bgp
, es
, &p
);
2190 flog_err(EC_BGP_EVPN_ROUTE_DELETE
,
2191 "%u failed to delete type-4 route for ESI %s",
2192 bgp
->vrf_id
, esi_to_str(&es
->esi
, buf
, sizeof(buf
)));
2195 /* Delete all routes from per ES table */
2196 return delete_all_es_routes(bgp
, es
);
2200 * Delete (and withdraw) local routes for specified VNI from the global
2201 * table and per-VNI table. After this, remove all other routes from
2202 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2203 * (advertise-all-vni) being disabled.
2205 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2208 struct prefix_evpn p
;
2210 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2211 * followed by type-3 routes (only one) - for this VNI.
2213 ret
= delete_all_type2_routes(bgp
, vpn
);
2217 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2218 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2222 /* Delete all routes from the per-VNI table. */
2223 return delete_all_vni_routes(bgp
, vpn
);
2227 * There is a tunnel endpoint IP address change for this VNI, delete
2228 * prior type-3 route (if needed) and update.
2229 * Note: Route re-advertisement happens elsewhere after other processing
2232 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2233 struct in_addr originator_ip
)
2235 struct prefix_evpn p
;
2237 /* If VNI is not live, we only need to update the originator ip */
2238 if (!is_vni_live(vpn
)) {
2239 vpn
->originator_ip
= originator_ip
;
2243 /* Update the tunnel-ip hash */
2244 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2245 bgp_tip_add(bgp
, &originator_ip
);
2247 /* filter routes as martian nexthop db has changed */
2248 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2250 /* Need to withdraw type-3 route as the originator IP is part
2253 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2254 delete_evpn_route(bgp
, vpn
, &p
);
2256 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2257 vpn
->originator_ip
= originator_ip
;
2261 /* Install EVPN route entry in ES */
2262 static int install_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2263 struct prefix_evpn
*p
,
2264 struct bgp_path_info
*parent_ri
)
2267 struct bgp_node
*rn
= NULL
;
2268 struct bgp_path_info
*ri
= NULL
;
2269 struct attr
*attr_new
= NULL
;
2271 /* Create (or fetch) route within the VNI.
2272 * NOTE: There is no RD here.
2274 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
2276 /* Check if route entry is already present. */
2277 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2279 && (struct bgp_path_info
*)ri
->extra
->parent
== parent_ri
)
2283 /* Add (or update) attribute to hash. */
2284 attr_new
= bgp_attr_intern(parent_ri
->attr
);
2286 /* Create new route with its attribute. */
2287 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
2288 parent_ri
->peer
, attr_new
, rn
);
2289 SET_FLAG(ri
->flags
, BGP_PATH_VALID
);
2290 bgp_path_info_extra_get(ri
);
2291 ri
->extra
->parent
= parent_ri
;
2292 bgp_path_info_add(rn
, ri
);
2294 if (attrhash_cmp(ri
->attr
, parent_ri
->attr
)
2295 && !CHECK_FLAG(ri
->flags
, BGP_PATH_REMOVED
)) {
2296 bgp_unlock_node(rn
);
2299 /* The attribute has changed. */
2300 /* Add (or update) attribute to hash. */
2301 attr_new
= bgp_attr_intern(parent_ri
->attr
);
2303 /* Restore route, if needed. */
2304 if (CHECK_FLAG(ri
->flags
, BGP_PATH_REMOVED
))
2305 bgp_path_info_restore(rn
, ri
);
2307 /* Mark if nexthop has changed. */
2308 if (!IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
))
2309 SET_FLAG(ri
->flags
, BGP_PATH_IGP_CHANGED
);
2311 /* Unintern existing, set to new. */
2312 bgp_attr_unintern(&ri
->attr
);
2313 ri
->attr
= attr_new
;
2314 ri
->uptime
= bgp_clock();
2317 /* Perform route selection and update zebra, if required. */
2318 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2323 * Install route entry into the VRF routing table and invoke route selection.
2325 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2326 struct prefix_evpn
*evp
,
2327 struct bgp_path_info
*parent_ri
)
2329 struct bgp_node
*rn
;
2330 struct bgp_path_info
*ri
;
2332 struct attr
*attr_new
;
2335 struct prefix
*pp
= &p
;
2338 char buf
[PREFIX_STRLEN
];
2339 char buf1
[PREFIX_STRLEN
];
2341 memset(pp
, 0, sizeof(struct prefix
));
2342 ip_prefix_from_evpn_prefix(evp
, pp
);
2344 if (bgp_debug_zebra(NULL
)) {
2346 "installing evpn prefix %s as ip prefix %s in vrf %s",
2347 prefix2str(evp
, buf
, sizeof(buf
)),
2348 prefix2str(pp
, buf1
, sizeof(buf
)),
2349 vrf_id_to_name(bgp_vrf
->vrf_id
));
2352 /* Create (or fetch) route within the VRF. */
2353 /* NOTE: There is no RD here. */
2354 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2356 safi
= SAFI_UNICAST
;
2357 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2358 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2360 safi
= SAFI_UNICAST
;
2361 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2365 /* EVPN routes currently only support a IPv4 next hop which corresponds
2366 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2367 * or prefix route, we have to convert the next hop to an IPv4-mapped
2368 * address for the rest of the code to flow through. In the case of IPv4,
2369 * make sure to set the flag for next hop attribute.
2371 bgp_attr_dup(&attr
, parent_ri
->attr
);
2373 evpn_convert_nexthop_to_ipv6(&attr
);
2375 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2377 /* Check if route entry is already present. */
2378 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2380 && (struct bgp_path_info
*)ri
->extra
->parent
== parent_ri
)
2384 /* Add (or update) attribute to hash. */
2385 attr_new
= bgp_attr_intern(&attr
);
2387 /* Create new route with its attribute. */
2388 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
2389 parent_ri
->peer
, attr_new
, rn
);
2390 SET_FLAG(ri
->flags
, BGP_PATH_VALID
);
2391 bgp_path_info_extra_get(ri
);
2392 ri
->extra
->parent
= bgp_path_info_lock(parent_ri
);
2393 bgp_lock_node((struct bgp_node
*)parent_ri
->net
);
2394 if (parent_ri
->extra
) {
2395 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
2396 sizeof(ri
->extra
->label
));
2397 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
2399 bgp_path_info_add(rn
, ri
);
2401 if (attrhash_cmp(ri
->attr
, &attr
)
2402 && !CHECK_FLAG(ri
->flags
, BGP_PATH_REMOVED
)) {
2403 bgp_unlock_node(rn
);
2406 /* The attribute has changed. */
2407 /* Add (or update) attribute to hash. */
2408 attr_new
= bgp_attr_intern(&attr
);
2410 /* Restore route, if needed. */
2411 if (CHECK_FLAG(ri
->flags
, BGP_PATH_REMOVED
))
2412 bgp_path_info_restore(rn
, ri
);
2414 /* Mark if nexthop has changed. */
2415 if ((afi
== AFI_IP
&&
2416 !IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
)) ||
2418 !IPV6_ADDR_SAME(&ri
->attr
->mp_nexthop_global
,
2419 &attr_new
->mp_nexthop_global
)))
2420 SET_FLAG(ri
->flags
, BGP_PATH_IGP_CHANGED
);
2422 /* Unintern existing, set to new. */
2423 bgp_attr_unintern(&ri
->attr
);
2424 ri
->attr
= attr_new
;
2425 ri
->uptime
= bgp_clock();
2428 bgp_aggregate_increment(bgp_vrf
, &rn
->p
, ri
, afi
, safi
);
2430 /* Perform route selection and update zebra, if required. */
2431 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2437 * Install route entry into the VNI routing table and invoke route selection.
2439 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2440 struct prefix_evpn
*p
,
2441 struct bgp_path_info
*parent_ri
)
2443 struct bgp_node
*rn
;
2444 struct bgp_path_info
*ri
;
2445 struct attr
*attr_new
;
2448 /* Create (or fetch) route within the VNI. */
2449 /* NOTE: There is no RD here. */
2450 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2452 /* Check if route entry is already present. */
2453 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2455 && (struct bgp_path_info
*)ri
->extra
->parent
== parent_ri
)
2459 /* Add (or update) attribute to hash. */
2460 attr_new
= bgp_attr_intern(parent_ri
->attr
);
2462 /* Create new route with its attribute. */
2463 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
2464 parent_ri
->peer
, attr_new
, rn
);
2465 SET_FLAG(ri
->flags
, BGP_PATH_VALID
);
2466 bgp_path_info_extra_get(ri
);
2467 ri
->extra
->parent
= bgp_path_info_lock(parent_ri
);
2468 bgp_lock_node((struct bgp_node
*)parent_ri
->net
);
2469 if (parent_ri
->extra
) {
2470 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
2471 sizeof(ri
->extra
->label
));
2472 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
2474 bgp_path_info_add(rn
, ri
);
2476 if (attrhash_cmp(ri
->attr
, parent_ri
->attr
)
2477 && !CHECK_FLAG(ri
->flags
, BGP_PATH_REMOVED
)) {
2478 bgp_unlock_node(rn
);
2481 /* The attribute has changed. */
2482 /* Add (or update) attribute to hash. */
2483 attr_new
= bgp_attr_intern(parent_ri
->attr
);
2485 /* Restore route, if needed. */
2486 if (CHECK_FLAG(ri
->flags
, BGP_PATH_REMOVED
))
2487 bgp_path_info_restore(rn
, ri
);
2489 /* Mark if nexthop has changed. */
2490 if (!IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
))
2491 SET_FLAG(ri
->flags
, BGP_PATH_IGP_CHANGED
);
2493 /* Unintern existing, set to new. */
2494 bgp_attr_unintern(&ri
->attr
);
2495 ri
->attr
= attr_new
;
2496 ri
->uptime
= bgp_clock();
2499 /* Perform route selection and update zebra, if required. */
2500 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2505 /* Uninstall EVPN route entry from ES route table */
2506 static int uninstall_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2507 struct prefix_evpn
*p
,
2508 struct bgp_path_info
*parent_ri
)
2511 struct bgp_node
*rn
;
2512 struct bgp_path_info
*ri
;
2514 if (!es
->route_table
)
2517 /* Locate route within the ESI.
2518 * NOTE: There is no RD here.
2520 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2524 /* Find matching route entry. */
2525 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2527 && (struct bgp_path_info
*)ri
->extra
->parent
== parent_ri
)
2533 /* Mark entry for deletion */
2534 bgp_path_info_delete(rn
, ri
);
2536 /* Perform route selection and update zebra, if required. */
2537 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2539 /* Unlock route node. */
2540 bgp_unlock_node(rn
);
2546 * Uninstall route entry from the VRF routing table and send message
2547 * to zebra, if appropriate.
2549 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2550 struct prefix_evpn
*evp
,
2551 struct bgp_path_info
*parent_ri
)
2553 struct bgp_node
*rn
;
2554 struct bgp_path_info
*ri
;
2557 struct prefix
*pp
= &p
;
2560 char buf
[PREFIX_STRLEN
];
2561 char buf1
[PREFIX_STRLEN
];
2563 memset(pp
, 0, sizeof(struct prefix
));
2564 ip_prefix_from_evpn_prefix(evp
, pp
);
2566 if (bgp_debug_zebra(NULL
)) {
2568 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2569 prefix2str(evp
, buf
, sizeof(buf
)),
2570 prefix2str(pp
, buf1
, sizeof(buf
)),
2571 vrf_id_to_name(bgp_vrf
->vrf_id
));
2574 /* Locate route within the VRF. */
2575 /* NOTE: There is no RD here. */
2576 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2578 safi
= SAFI_UNICAST
;
2579 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2582 safi
= SAFI_UNICAST
;
2583 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2589 /* Find matching route entry. */
2590 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2592 && (struct bgp_path_info
*)ri
->extra
->parent
== parent_ri
)
2598 bgp_aggregate_decrement(bgp_vrf
, &rn
->p
, ri
, afi
, safi
);
2600 /* Mark entry for deletion */
2601 bgp_path_info_delete(rn
, ri
);
2603 /* Perform route selection and update zebra, if required. */
2604 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2606 /* Unlock route node. */
2607 bgp_unlock_node(rn
);
2613 * Uninstall route entry from the VNI routing table and send message
2614 * to zebra, if appropriate.
2616 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2617 struct prefix_evpn
*p
,
2618 struct bgp_path_info
*parent_ri
)
2620 struct bgp_node
*rn
;
2621 struct bgp_path_info
*ri
;
2624 /* Locate route within the VNI. */
2625 /* NOTE: There is no RD here. */
2626 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2630 /* Find matching route entry. */
2631 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2633 && (struct bgp_path_info
*)ri
->extra
->parent
== parent_ri
)
2639 /* Mark entry for deletion */
2640 bgp_path_info_delete(rn
, ri
);
2642 /* Perform route selection and update zebra, if required. */
2643 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2645 /* Unlock route node. */
2646 bgp_unlock_node(rn
);
2652 * Given a prefix, see if it belongs to ES.
2654 static int is_prefix_matching_for_es(struct prefix_evpn
*p
,
2657 /* if not an ES route return false */
2658 if (p
->prefix
.route_type
!= BGP_EVPN_ES_ROUTE
)
2661 if (memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
)) == 0)
2668 * Given a route entry and a VRF, see if this route entry should be
2669 * imported into the VRF i.e., RTs match.
2671 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2672 struct bgp_path_info
*ri
)
2674 struct attr
*attr
= ri
->attr
;
2675 struct ecommunity
*ecom
;
2679 /* Route should have valid RT to be even considered. */
2680 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2683 ecom
= attr
->ecommunity
;
2684 if (!ecom
|| !ecom
->size
)
2687 /* For each extended community RT, see if it matches this VNI. If any RT
2688 * matches, we're done.
2690 for (i
= 0; i
< ecom
->size
; i
++) {
2692 uint8_t type
, sub_type
;
2693 struct ecommunity_val
*eval
;
2694 struct ecommunity_val eval_tmp
;
2695 struct vrf_irt_node
*irt
;
2697 /* Only deal with RTs */
2698 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2699 eval
= (struct ecommunity_val
*)(ecom
->val
2700 + (i
* ECOMMUNITY_SIZE
));
2703 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2706 /* See if this RT matches specified VNIs import RTs */
2707 irt
= lookup_vrf_import_rt(eval
);
2709 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2712 /* Also check for non-exact match. In this, we mask out the AS
2714 * only check on the local-admin sub-field. This is to
2716 * VNI as the RT for EBGP peering too.
2719 if (type
== ECOMMUNITY_ENCODE_AS
2720 || type
== ECOMMUNITY_ENCODE_AS4
2721 || type
== ECOMMUNITY_ENCODE_IP
) {
2722 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2723 mask_ecom_global_admin(&eval_tmp
, eval
);
2724 irt
= lookup_vrf_import_rt(&eval_tmp
);
2727 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2735 * Given a route entry and a VNI, see if this route entry should be
2736 * imported into the VNI i.e., RTs match.
2738 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2739 struct bgp_path_info
*ri
)
2741 struct attr
*attr
= ri
->attr
;
2742 struct ecommunity
*ecom
;
2746 /* Route should have valid RT to be even considered. */
2747 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2750 ecom
= attr
->ecommunity
;
2751 if (!ecom
|| !ecom
->size
)
2754 /* For each extended community RT, see if it matches this VNI. If any RT
2755 * matches, we're done.
2757 for (i
= 0; i
< ecom
->size
; i
++) {
2759 uint8_t type
, sub_type
;
2760 struct ecommunity_val
*eval
;
2761 struct ecommunity_val eval_tmp
;
2762 struct irt_node
*irt
;
2764 /* Only deal with RTs */
2765 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2766 eval
= (struct ecommunity_val
*)(ecom
->val
2767 + (i
* ECOMMUNITY_SIZE
));
2770 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2773 /* See if this RT matches specified VNIs import RTs */
2774 irt
= lookup_import_rt(bgp
, eval
);
2776 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2779 /* Also check for non-exact match. In this, we mask out the AS
2781 * only check on the local-admin sub-field. This is to
2783 * VNI as the RT for EBGP peering too.
2786 if (type
== ECOMMUNITY_ENCODE_AS
2787 || type
== ECOMMUNITY_ENCODE_AS4
2788 || type
== ECOMMUNITY_ENCODE_IP
) {
2789 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2790 mask_ecom_global_admin(&eval_tmp
, eval
);
2791 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2794 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2801 static int install_uninstall_routes_for_es(struct bgp
*bgp
,
2808 char buf
[PREFIX_STRLEN
];
2809 char buf1
[ESI_STR_LEN
];
2810 struct bgp_node
*rd_rn
, *rn
;
2811 struct bgp_table
*table
;
2812 struct bgp_path_info
*ri
;
2818 * Walk entire global routing table and evaluate routes which could be
2819 * imported into this VRF. Note that we need to loop through all global
2820 * routes to determine which route matches the import rt on vrf
2822 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2823 rd_rn
= bgp_route_next(rd_rn
)) {
2824 table
= (struct bgp_table
*)(rd_rn
->info
);
2828 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2829 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2831 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2833 * Consider "valid" remote routes applicable for
2836 if (!(CHECK_FLAG(ri
->flags
, BGP_PATH_VALID
)
2837 && ri
->type
== ZEBRA_ROUTE_BGP
2838 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2841 if (!is_prefix_matching_for_es(evp
, es
))
2845 ret
= install_evpn_route_entry_in_es(
2848 ret
= uninstall_evpn_route_entry_in_es(
2854 "Failed to %s EVPN %s route in ESI %s",
2857 prefix2str(evp
, buf
,
2859 esi_to_str(&es
->esi
, buf1
,
2870 * Install or uninstall mac-ip routes are appropriate for this
2873 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
2877 struct bgp_node
*rd_rn
, *rn
;
2878 struct bgp_table
*table
;
2879 struct bgp_path_info
*ri
;
2881 char buf
[PREFIX_STRLEN
];
2882 struct bgp
*bgp_def
= NULL
;
2886 bgp_def
= bgp_get_default();
2890 /* Walk entire global routing table and evaluate routes which could be
2891 * imported into this VRF. Note that we need to loop through all global
2892 * routes to determine which route matches the import rt on vrf
2894 for (rd_rn
= bgp_table_top(bgp_def
->rib
[afi
][safi
]); rd_rn
;
2895 rd_rn
= bgp_route_next(rd_rn
)) {
2896 table
= (struct bgp_table
*)(rd_rn
->info
);
2900 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2901 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2903 /* if not mac-ip route skip this route */
2904 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2905 || evp
->prefix
.route_type
2906 == BGP_EVPN_IP_PREFIX_ROUTE
))
2909 /* if not a mac+ip route skip this route */
2910 if (!(is_evpn_prefix_ipaddr_v4(evp
)
2911 || is_evpn_prefix_ipaddr_v6(evp
)))
2914 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2915 /* Consider "valid" remote routes applicable for
2918 if (!(CHECK_FLAG(ri
->flags
, BGP_PATH_VALID
)
2919 && ri
->type
== ZEBRA_ROUTE_BGP
2920 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2923 if (is_route_matching_for_vrf(bgp_vrf
, ri
)) {
2925 ret
= install_evpn_route_entry_in_vrf(
2928 ret
= uninstall_evpn_route_entry_in_vrf(
2934 "Failed to %s EVPN %s route in VRF %s",
2937 prefix2str(evp
, buf
,
2952 * Install or uninstall routes of specified type that are appropriate for this
2955 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
2956 struct bgpevpn
*vpn
,
2957 bgp_evpn_route_type rtype
,
2962 struct bgp_node
*rd_rn
, *rn
;
2963 struct bgp_table
*table
;
2964 struct bgp_path_info
*ri
;
2970 /* Walk entire global routing table and evaluate routes which could be
2971 * imported into this VPN. Note that we cannot just look at the routes
2973 * the VNI's RD - remote routes applicable for this VNI could have any
2976 /* EVPN routes are a 2-level table. */
2977 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2978 rd_rn
= bgp_route_next(rd_rn
)) {
2979 table
= (struct bgp_table
*)(rd_rn
->info
);
2983 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2984 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2986 if (evp
->prefix
.route_type
!= rtype
)
2989 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2990 /* Consider "valid" remote routes applicable for
2992 if (!(CHECK_FLAG(ri
->flags
, BGP_PATH_VALID
)
2993 && ri
->type
== ZEBRA_ROUTE_BGP
2994 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2997 if (is_route_matching_for_vni(bgp
, vpn
, ri
)) {
2999 ret
= install_evpn_route_entry(
3002 ret
= uninstall_evpn_route_entry(
3008 "%u: Failed to %s EVPN %s route in VNI %u",
3012 rtype
== BGP_EVPN_MAC_IP_ROUTE
3026 /* Install any existing remote ES routes applicable for this ES into its routing
3027 * table. This is invoked when ES comes up.
3029 static int install_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
3031 return install_uninstall_routes_for_es(bgp
, es
, 1);
3035 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3036 * is invoked upon l3vni-add or l3vni import rt change
3038 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3040 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3045 * Install any existing remote routes applicable for this VNI into its
3046 * routing table. This is invoked when a VNI becomes "live" or its Import
3049 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3053 /* Install type-3 routes followed by type-2 routes - the ones applicable
3056 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3061 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3065 /* uninstall routes from l3vni vrf. */
3066 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3068 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3073 * Uninstall any existing remote routes for this VNI. One scenario in which
3074 * this is invoked is upon an import RT change.
3076 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3080 /* Uninstall type-2 routes followed by type-3 routes - the ones
3084 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3089 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3093 /* Install or unistall route in ES */
3094 static int install_uninstall_route_in_es(struct bgp
*bgp
, struct evpnes
*es
,
3095 afi_t afi
, safi_t safi
,
3096 struct prefix_evpn
*evp
,
3097 struct bgp_path_info
*ri
, int install
)
3100 char buf
[ESI_STR_LEN
];
3103 ret
= install_evpn_route_entry_in_es(bgp
, es
, evp
, ri
);
3105 ret
= uninstall_evpn_route_entry_in_es(bgp
, es
, evp
, ri
);
3110 "%u: Failed to %s EVPN %s route in ESI %s", bgp
->vrf_id
,
3111 install
? "install" : "uninstall", "ES",
3112 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)));
3119 * Install or uninstall route in matching VRFs (list).
3121 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3122 safi_t safi
, struct prefix_evpn
*evp
,
3123 struct bgp_path_info
*ri
,
3124 struct list
*vrfs
, int install
)
3126 char buf
[PREFIX2STR_BUFFER
];
3127 struct bgp
*bgp_vrf
;
3128 struct listnode
*node
, *nnode
;
3130 /* Only type-2/type-5 routes go into a VRF */
3131 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3132 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3135 /* if it is type-2 route and not a mac+ip route skip this route */
3136 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3137 && !(is_evpn_prefix_ipaddr_v4(evp
)
3138 || is_evpn_prefix_ipaddr_v6(evp
)))
3141 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3145 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, ri
);
3147 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3151 flog_err(EC_BGP_EVPN_FAIL
,
3152 "%u: Failed to %s prefix %s in VRF %s",
3154 install
? "install" : "uninstall",
3155 prefix2str(evp
, buf
, sizeof(buf
)),
3156 vrf_id_to_name(bgp_vrf
->vrf_id
));
3165 * Install or uninstall route in matching VNIs (list).
3167 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3168 safi_t safi
, struct prefix_evpn
*evp
,
3169 struct bgp_path_info
*ri
,
3170 struct list
*vnis
, int install
)
3172 struct bgpevpn
*vpn
;
3173 struct listnode
*node
, *nnode
;
3175 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3178 if (!is_vni_live(vpn
))
3182 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, ri
);
3184 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, ri
);
3187 flog_err(EC_BGP_EVPN_FAIL
,
3188 "%u: Failed to %s EVPN %s route in VNI %u",
3189 bgp
->vrf_id
, install
? "install" : "uninstall",
3190 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3202 * Install or uninstall route for appropriate VNIs/ESIs.
3204 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3206 struct bgp_path_info
*ri
, int import
)
3208 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3209 struct attr
*attr
= ri
->attr
;
3210 struct ecommunity
*ecom
;
3215 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3216 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3217 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3218 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3219 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3222 /* If we don't have Route Target, nothing much to do. */
3223 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3226 ecom
= attr
->ecommunity
;
3227 if (!ecom
|| !ecom
->size
)
3230 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3231 * attached to the route */
3232 for (i
= 0; i
< ecom
->size
; i
++) {
3234 uint8_t type
, sub_type
;
3235 struct ecommunity_val
*eval
;
3236 struct ecommunity_val eval_tmp
;
3237 struct irt_node
*irt
; /* import rt for l2vni */
3238 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3241 /* Only deal with RTs */
3242 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3243 eval
= (struct ecommunity_val
*)(ecom
->val
3244 + (i
* ECOMMUNITY_SIZE
));
3247 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3251 * macip routes (type-2) are imported into VNI and VRF tables.
3252 * IMET route is imported into VNI table.
3253 * prefix routes are imported into VRF table.
3255 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3256 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3257 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3259 irt
= lookup_import_rt(bgp
, eval
);
3261 install_uninstall_route_in_vnis(bgp
, afi
, safi
,
3266 vrf_irt
= lookup_vrf_import_rt(eval
);
3268 install_uninstall_route_in_vrfs(bgp
, afi
, safi
,
3273 /* Also check for non-exact match.
3274 * In this, we mask out the AS and
3275 * only check on the local-admin sub-field.
3276 * This is to facilitate using
3277 * VNI as the RT for EBGP peering too.
3281 if (type
== ECOMMUNITY_ENCODE_AS
3282 || type
== ECOMMUNITY_ENCODE_AS4
3283 || type
== ECOMMUNITY_ENCODE_IP
) {
3284 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3285 mask_ecom_global_admin(&eval_tmp
, eval
);
3286 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3287 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3291 install_uninstall_route_in_vnis(bgp
, afi
, safi
,
3296 install_uninstall_route_in_vrfs(bgp
, afi
, safi
,
3302 /* es route is imported into the es table */
3303 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3305 /* we will match based on the entire esi to avoid
3306 * imoort of an es route for esi2 into esi1
3308 es
= bgp_evpn_lookup_es(bgp
, &evp
->prefix
.es_addr
.esi
);
3309 if (es
&& is_es_local(es
))
3310 install_uninstall_route_in_es(bgp
, es
,
3320 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3323 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3325 /* delete all ipv4 routes and withdraw from peers */
3326 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3327 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3329 /* delete all ipv6 routes and withdraw from peers */
3330 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3331 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3335 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3338 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3340 /* update all ipv4 routes */
3341 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3342 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3344 /* update all ipv6 routes */
3345 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3346 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3350 * update and advertise local routes for a VRF as type-5 routes.
3351 * This is invoked upon RD change for a VRF. Note taht the processing is only
3352 * done in the global route table using the routes which already exist in the
3355 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3357 /* skip if the RD is configured */
3358 if (is_vrf_rd_configured(bgp_vrf
))
3361 /* derive the RD for the VRF based on new router-id */
3362 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3364 /* update advertise ipv4|ipv6 routes as type-5 routes */
3365 update_advertise_vrf_routes(bgp_vrf
);
3369 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3370 * This is invoked upon VRF RD change. The processing is done only from global
3373 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3375 /* skip if the RD is configured */
3376 if (is_vrf_rd_configured(bgp_vrf
))
3379 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3380 delete_withdraw_vrf_routes(bgp_vrf
);
3384 * Update and advertise local routes for a VNI. Invoked upon router-id
3385 * change. Note that the processing is done only on the global route table
3386 * using routes that already exist in the per-VNI table.
3388 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3390 struct prefix_evpn p
;
3391 struct bgp_node
*rn
, *global_rn
;
3392 struct bgp_path_info
*ri
, *global_ri
;
3394 afi_t afi
= AFI_L2VPN
;
3395 safi_t safi
= SAFI_EVPN
;
3397 /* Locate type-3 route for VNI in the per-VNI table and use its
3398 * attributes to create and advertise the type-3 route for this VNI
3399 * in the global table.
3401 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3402 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3403 if (!rn
) /* unexpected */
3405 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
3406 if (ri
->peer
== bgp
->peer_self
&& ri
->type
== ZEBRA_ROUTE_BGP
3407 && ri
->sub_type
== BGP_ROUTE_STATIC
)
3409 if (!ri
) /* unexpected */
3413 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3414 (struct prefix
*)&p
, &vpn
->prd
);
3415 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1, &ri
,
3416 0, mac_mobility_seqnum(attr
));
3418 /* Schedule for processing and unlock node. */
3419 bgp_process(bgp
, global_rn
, afi
, safi
);
3420 bgp_unlock_node(global_rn
);
3422 /* Now, walk this VNI's route table and use the route and its attribute
3423 * to create and schedule route in global table.
3425 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
3426 rn
= bgp_route_next(rn
)) {
3427 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3429 /* Identify MAC-IP local routes. */
3430 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3433 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
3434 if (ri
->peer
== bgp
->peer_self
3435 && ri
->type
== ZEBRA_ROUTE_BGP
3436 && ri
->sub_type
== BGP_ROUTE_STATIC
)
3441 /* Create route in global routing table using this route entry's
3445 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3446 (struct prefix
*)evp
, &vpn
->prd
);
3448 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
3450 mac_mobility_seqnum(attr
));
3452 /* Schedule for processing and unlock node. */
3453 bgp_process(bgp
, global_rn
, afi
, safi
);
3454 bgp_unlock_node(global_rn
);
3461 * Delete (and withdraw) local routes for a VNI - only from the global
3462 * table. Invoked upon router-id change.
3464 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3467 struct prefix_evpn p
;
3468 struct bgp_node
*global_rn
;
3469 struct bgp_path_info
*ri
;
3470 afi_t afi
= AFI_L2VPN
;
3471 safi_t safi
= SAFI_EVPN
;
3473 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3474 * for this VNI - from the global table.
3476 ret
= delete_global_type2_routes(bgp
, vpn
);
3480 /* Remove type-3 route for this VNI from global table. */
3481 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3482 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3483 (struct prefix
*)&p
, &vpn
->prd
);
3485 /* Delete route entry in the global EVPN table. */
3486 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &ri
);
3488 /* Schedule for processing - withdraws to peers happen from
3492 bgp_process(bgp
, global_rn
, afi
, safi
);
3493 bgp_unlock_node(global_rn
);
3500 * Handle router-id change. Update and advertise local routes corresponding
3501 * to this VNI from peers. Note that this is invoked after updating the
3502 * router-id. The routes in the per-VNI table are used to create routes in
3503 * the global table and schedule them.
3505 static void update_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3507 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3509 /* Skip VNIs with configured RD. */
3510 if (is_rd_configured(vpn
))
3513 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3514 update_advertise_vni_routes(bgp
, vpn
);
3518 * Handle router-id change. Delete and withdraw local routes corresponding
3519 * to this VNI from peers. Note that this is invoked prior to updating
3520 * the router-id and is done only on the global route table, the routes
3521 * are needed in the per-VNI table to re-advertise with new router id.
3523 static void withdraw_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3525 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3527 /* Skip VNIs with configured RD. */
3528 if (is_rd_configured(vpn
))
3531 delete_withdraw_vni_routes(bgp
, vpn
);
3535 * Process received EVPN type-2 route (advertise or withdraw).
3537 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3538 struct attr
*attr
, uint8_t *pfx
, int psize
,
3539 uint32_t addpath_id
)
3541 struct prefix_rd prd
;
3542 struct prefix_evpn p
;
3543 struct bgp_route_evpn evpn
;
3545 uint8_t macaddr_len
;
3546 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
3547 uint32_t num_labels
= 0;
3551 /* Type-2 route should be either 33, 37 or 49 bytes or an
3552 * additional 3 bytes if there is a second label (VNI):
3553 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3554 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3555 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3557 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3558 && psize
!= 40 && psize
!= 52) {
3559 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3560 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3561 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3565 memset(&evpn
, 0, sizeof(evpn
));
3567 /* Make prefix_rd */
3568 prd
.family
= AF_UNSPEC
;
3570 memcpy(&prd
.val
, pfx
, 8);
3573 /* Make EVPN prefix. */
3574 memset(&p
, 0, sizeof(struct prefix_evpn
));
3576 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3577 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3579 /* Copy Ethernet Seg Identifier */
3580 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
3583 /* Copy Ethernet Tag */
3584 memcpy(ð_tag
, pfx
, 4);
3585 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3588 /* Get the MAC Addr len */
3589 macaddr_len
= *pfx
++;
3591 /* Get the MAC Addr */
3592 if (macaddr_len
== (ETH_ALEN
* 8)) {
3593 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
3597 EC_BGP_EVPN_ROUTE_INVALID
,
3598 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3599 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3605 ipaddr_len
= *pfx
++;
3606 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3607 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3609 EC_BGP_EVPN_ROUTE_INVALID
,
3610 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3611 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3616 ipaddr_len
/= 8; /* Convert to bytes. */
3617 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3620 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
3624 /* Get the VNI(s). Stored as bytes here. */
3626 memset(label
, 0, sizeof(label
));
3627 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
3628 pfx
+= BGP_LABEL_BYTES
;
3629 psize
-= (33 + ipaddr_len
);
3630 /* Do we have a second VNI? */
3633 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
3635 * If in future, we are required to access additional fields,
3636 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
3641 /* Process the route. */
3643 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3644 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3645 &prd
, &label
[0], num_labels
, 0, &evpn
);
3647 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3648 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3649 &prd
, &label
[0], num_labels
, &evpn
);
3654 * Process received EVPN type-3 route (advertise or withdraw).
3656 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3657 struct attr
*attr
, uint8_t *pfx
, int psize
,
3658 uint32_t addpath_id
)
3660 struct prefix_rd prd
;
3661 struct prefix_evpn p
;
3666 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3667 * IP len (1) and IP (4 or 16).
3669 if (psize
!= 17 && psize
!= 29) {
3670 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3671 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3672 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3676 /* If PMSI is present, log if it is anything other than IR.
3677 * Note: We just simply ignore the values as it is not clear if
3678 * doing anything else is better.
3681 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3682 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
) {
3684 EC_BGP_EVPN_PMSI_PRESENT
,
3685 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3686 peer
->bgp
->vrf_id
, peer
->host
,
3687 attr
->pmsi_tnl_type
);
3691 /* Make prefix_rd */
3692 prd
.family
= AF_UNSPEC
;
3694 memcpy(&prd
.val
, pfx
, 8);
3697 /* Make EVPN prefix. */
3698 memset(&p
, 0, sizeof(struct prefix_evpn
));
3700 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3701 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3703 /* Copy Ethernet Tag */
3704 memcpy(ð_tag
, pfx
, 4);
3705 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
3709 ipaddr_len
= *pfx
++;
3710 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3711 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
3712 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
3715 EC_BGP_EVPN_ROUTE_INVALID
,
3716 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3717 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3721 /* Process the route. */
3723 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3724 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3725 &prd
, NULL
, 0, 0, NULL
);
3727 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3728 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3729 &prd
, NULL
, 0, NULL
);
3734 * Process received EVPN type-4 route (advertise or withdraw).
3736 static int process_type4_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3737 struct attr
*attr
, uint8_t *pfx
, int psize
,
3738 uint32_t addpath_id
)
3743 struct in_addr vtep_ip
;
3744 struct prefix_rd prd
;
3745 struct prefix_evpn p
;
3747 /* Type-4 route should be either 23 or 35 bytes
3748 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
3750 if (psize
!= 23 && psize
!= 35) {
3751 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3752 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
3753 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3757 /* Make prefix_rd */
3758 prd
.family
= AF_UNSPEC
;
3760 memcpy(&prd
.val
, pfx
, 8);
3764 memcpy(&esi
, pfx
, ESI_BYTES
);
3769 ipaddr_len
= *pfx
++;
3770 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3771 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
3774 EC_BGP_EVPN_ROUTE_INVALID
,
3775 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
3776 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3780 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
3781 /* Process the route. */
3783 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3784 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3785 &prd
, NULL
, 0, 0, NULL
);
3787 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3788 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3789 &prd
, NULL
, 0, NULL
);
3796 * Process received EVPN type-5 route (advertise or withdraw).
3798 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3799 struct attr
*attr
, uint8_t *pfx
, int psize
,
3800 uint32_t addpath_id
, int withdraw
)
3802 struct prefix_rd prd
;
3803 struct prefix_evpn p
;
3804 struct bgp_route_evpn evpn
;
3807 mpls_label_t label
; /* holds the VNI as in the packet */
3810 /* Type-5 route should be 34 or 58 bytes:
3811 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3812 * GW (4 or 16) and VNI (3).
3813 * Note that the IP and GW should both be IPv4 or both IPv6.
3815 if (psize
!= 34 && psize
!= 58) {
3816 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3817 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3818 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3822 /* Make prefix_rd */
3823 prd
.family
= AF_UNSPEC
;
3825 memcpy(&prd
.val
, pfx
, 8);
3828 /* Make EVPN prefix. */
3829 memset(&p
, 0, sizeof(struct prefix_evpn
));
3831 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3832 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
3834 /* Additional information outside of prefix - ESI and GW IP */
3835 memset(&evpn
, 0, sizeof(evpn
));
3838 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
3841 /* Fetch Ethernet Tag. */
3842 memcpy(ð_tag
, pfx
, 4);
3843 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
3846 /* Fetch IP prefix length. */
3848 if (ippfx_len
> IPV6_MAX_BITLEN
) {
3850 EC_BGP_EVPN_ROUTE_INVALID
,
3851 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3852 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
3855 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
3857 /* Determine IPv4 or IPv6 prefix */
3858 /* Since the address and GW are from the same family, this just becomes
3859 * a simple check on the total size.
3862 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
3863 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
3865 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
3868 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
3869 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
3871 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
3875 /* Get the VNI (in MPLS label field). Stored as bytes here. */
3876 memset(&label
, 0, sizeof(label
));
3877 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
3880 * If in future, we are required to access additional fields,
3881 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
3885 /* Process the route. */
3887 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3888 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3889 &prd
, &label
, 1, 0, &evpn
);
3891 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3892 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3893 &prd
, &label
, 1, &evpn
);
3898 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
3899 struct prefix_rd
*prd
, mpls_label_t
*label
,
3900 uint32_t num_labels
, struct attr
*attr
)
3904 struct evpn_addr
*p_evpn_p
;
3906 memset(&temp
, 0, 16);
3907 if (p
->family
!= AF_EVPN
)
3909 p_evpn_p
= &(p
->u
.prefix_evpn
);
3911 /* len denites the total len of IP and GW-IP in the route
3912 IP and GW-IP have to be both ipv4 or ipv6
3914 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3915 len
= 8; /* IP and GWIP are both ipv4 */
3917 len
= 32; /* IP and GWIP are both ipv6 */
3918 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
3919 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
3920 stream_put(s
, prd
->val
, 8);
3922 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
3924 stream_put(s
, &temp
, 10);
3925 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
3926 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
3927 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3928 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
3930 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
3932 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3934 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
3936 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
3938 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3939 stream_put_ipv4(s
, 0);
3941 stream_put(s
, &temp
, 16);
3945 stream_put(s
, label
, 3);
3951 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
3953 static void cleanup_vni_on_disable(struct hash_backet
*backet
, struct bgp
*bgp
)
3955 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3957 /* Remove EVPN routes and schedule for processing. */
3958 delete_routes_for_vni(bgp
, vpn
);
3960 /* Clear "live" flag and see if hash needs to be freed. */
3961 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
3962 if (!is_vni_configured(vpn
))
3963 bgp_evpn_free(bgp
, vpn
);
3967 * Free a VNI entry; iterator function called during cleanup.
3969 static void free_vni_entry(struct hash_backet
*backet
, struct bgp
*bgp
)
3971 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3973 delete_all_vni_routes(bgp
, vpn
);
3974 bgp_evpn_free(bgp
, vpn
);
3978 * Derive AUTO import RT for BGP VRF - L3VNI
3980 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
3982 struct bgp
*bgp_def
= NULL
;
3984 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
3985 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
3988 bgp_def
= bgp_get_default();
3991 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
3995 * Delete AUTO import RT from BGP VRF - L3VNI
3997 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
3999 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4003 * Derive AUTO export RT for BGP VRF - L3VNI
4005 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4007 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4008 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4012 * Delete AUTO export RT from BGP VRF - L3VNI
4014 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4016 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4019 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4021 struct bgp
*bgp_def
= NULL
;
4022 struct listnode
*node
= NULL
;
4023 struct bgpevpn
*vpn
= NULL
;
4025 bgp_def
= bgp_get_default();
4029 /* update all type-5 routes */
4030 update_advertise_vrf_routes(bgp_vrf
);
4032 /* update all type-2 routes */
4033 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4034 update_routes_for_vni(bgp_def
, vpn
);
4038 * Handle autort change for a given VNI.
4040 static void update_autort_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
4042 struct bgpevpn
*vpn
= backet
->data
;
4044 if (!is_import_rt_configured(vpn
)) {
4045 if (is_vni_live(vpn
))
4046 bgp_evpn_uninstall_routes(bgp
, vpn
);
4047 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4048 list_delete_all_node(vpn
->import_rtl
);
4049 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4050 if (is_vni_live(vpn
))
4051 bgp_evpn_install_routes(bgp
, vpn
);
4053 if (!is_export_rt_configured(vpn
)) {
4054 list_delete_all_node(vpn
->export_rtl
);
4055 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4056 if (is_vni_live(vpn
))
4057 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4065 /* withdraw type-5 route corresponding to ip prefix */
4066 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4067 afi_t afi
, safi_t safi
)
4070 struct prefix_evpn evp
;
4071 char buf
[PREFIX_STRLEN
];
4073 build_type5_prefix_from_ip_prefix(&evp
, p
);
4074 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4077 EC_BGP_EVPN_ROUTE_DELETE
,
4078 "%u failed to delete type-5 route for prefix %s in vrf %s",
4079 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4080 vrf_id_to_name(bgp_vrf
->vrf_id
));
4084 /* withdraw all type-5 routes for an address family */
4085 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4087 struct bgp_table
*table
= NULL
;
4088 struct bgp_node
*rn
= NULL
;
4089 struct bgp_path_info
*ri
;
4091 table
= bgp_vrf
->rib
[afi
][safi
];
4092 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4093 /* Only care about "selected" routes - non-imported. */
4094 /* TODO: Support for AddPath for EVPN. */
4095 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
4096 if (CHECK_FLAG(ri
->flags
, BGP_PATH_SELECTED
)
4097 && (!ri
->extra
|| !ri
->extra
->parent
)) {
4098 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
4107 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4108 * to this function correspond to those of the source IP prefix (best
4109 * path in the case of the attr. In the case of a local prefix (when we
4110 * are advertising local subnets), the src_attr will be NULL.
4112 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4113 struct attr
*src_attr
, afi_t afi
,
4117 struct prefix_evpn evp
;
4118 char buf
[PREFIX_STRLEN
];
4120 build_type5_prefix_from_ip_prefix(&evp
, p
);
4121 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4123 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4124 "%u: Failed to create type-5 route for prefix %s",
4125 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4128 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4129 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4130 * advertisement is enabled.
4132 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4135 struct bgp_table
*table
= NULL
;
4136 struct bgp_node
*rn
= NULL
;
4137 struct bgp_path_info
*ri
;
4139 table
= bgp_vrf
->rib
[afi
][safi
];
4140 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4141 /* Need to identify the "selected" route entry to use its
4142 * attribute. Also, we only consider "non-imported" routes.
4143 * TODO: Support for AddPath for EVPN.
4145 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
4146 if (CHECK_FLAG(ri
->flags
, BGP_PATH_SELECTED
)
4147 && (!ri
->extra
|| !ri
->extra
->parent
)) {
4149 /* apply the route-map */
4150 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4153 ret
= route_map_apply(
4154 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4156 &rn
->p
, RMAP_BGP
, ri
);
4157 if (ret
== RMAP_DENYMATCH
)
4160 bgp_evpn_advertise_type5_route(
4161 bgp_vrf
, &rn
->p
, ri
->attr
, afi
, safi
);
4168 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4170 struct listnode
*node
, *nnode
, *node_to_del
;
4171 struct ecommunity
*ecom
, *ecom_auto
;
4172 struct ecommunity_val eval
;
4174 if (bgp
->advertise_autort_rfc8365
)
4175 vni
|= EVPN_AUTORT_VXLAN
;
4176 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4178 ecom_auto
= ecommunity_new();
4179 ecommunity_add_val(ecom_auto
, &eval
);
4182 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4183 if (ecommunity_match(ecom
, ecom_auto
)) {
4184 ecommunity_free(&ecom
);
4190 list_delete_node(rtl
, node_to_del
);
4192 ecommunity_free(&ecom_auto
);
4195 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4196 struct ecommunity
*ecomadd
)
4198 /* uninstall routes from vrf */
4199 uninstall_routes_for_vrf(bgp_vrf
);
4201 /* Cleanup the RT to VRF mapping */
4202 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4204 /* Remove auto generated RT */
4205 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4207 /* Add the newly configured RT to RT list */
4208 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4209 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4211 /* map VRF to its RTs */
4212 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4214 /* install routes matching the new VRF */
4215 install_routes_for_vrf(bgp_vrf
);
4218 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4219 struct ecommunity
*ecomdel
)
4221 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4222 struct ecommunity
*ecom
= NULL
;
4224 /* uninstall routes from vrf */
4225 uninstall_routes_for_vrf(bgp_vrf
);
4227 /* Cleanup the RT to VRF mapping */
4228 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4230 /* remove the RT from the RT list */
4231 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4232 if (ecommunity_match(ecom
, ecomdel
)) {
4233 ecommunity_free(&ecom
);
4240 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4242 assert(bgp_vrf
->vrf_import_rtl
);
4243 /* fallback to auto import rt, if this was the last RT */
4244 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4245 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4246 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4249 /* map VRFs to its RTs */
4250 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4252 /* install routes matching this new RT */
4253 install_routes_for_vrf(bgp_vrf
);
4256 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4257 struct ecommunity
*ecomadd
)
4259 /* remove auto-generated RT */
4260 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4262 /* Add the new RT to the RT list */
4263 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4264 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4266 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4269 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4270 struct ecommunity
*ecomdel
)
4272 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4273 struct ecommunity
*ecom
= NULL
;
4275 /* Remove the RT from the RT list */
4276 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4277 if (ecommunity_match(ecom
, ecomdel
)) {
4278 ecommunity_free(&ecom
);
4285 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4288 * Temporary assert to make SA happy.
4289 * The ALL_LIST_ELEMENTS macro above has a NULL check
4290 * which means that SA is going to complain about
4291 * the list_isempty call, which doesn't NULL check.
4292 * So until we get this situation cleaned up, here
4295 assert(bgp_vrf
->vrf_export_rtl
);
4297 /* fall back to auto-generated RT if this was the last RT */
4298 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4299 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4300 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4303 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4307 * Handle change to BGP router id. This is invoked twice by the change
4308 * handler, first before the router id has been changed and then after
4309 * the router id has been changed. The first invocation will result in
4310 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4311 * will result in the routes being re-advertised.
4313 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4317 /* delete and withdraw all the type-5 routes
4318 stored in the global table for this vrf
4320 withdraw_router_id_vrf(bgp
);
4322 /* delete all the VNI routes (type-2/type-3) routes for all the
4325 hash_iterate(bgp
->vnihash
,
4326 (void (*)(struct hash_backet
*,
4327 void *))withdraw_router_id_vni
,
4331 /* advertise all routes in the vrf as type-5 routes with the new
4334 update_router_id_vrf(bgp
);
4336 /* advertise all the VNI routes (type-2/type-3) routes with the
4339 hash_iterate(bgp
->vnihash
,
4340 (void (*)(struct hash_backet
*,
4341 void *))update_router_id_vni
,
4347 * Handle change to auto-RT algorithm - update and advertise local routes.
4349 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4351 hash_iterate(bgp
->vnihash
,
4352 (void (*)(struct hash_backet
*,
4353 void*))update_autort_vni
,
4358 * Handle change to export RT - update and advertise local routes.
4360 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4362 return update_routes_for_vni(bgp
, vpn
);
4365 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4368 delete_withdraw_vrf_routes(bgp_vrf
);
4370 update_advertise_vrf_routes(bgp_vrf
);
4374 * Handle change to RD. This is invoked twice by the change handler,
4375 * first before the RD has been changed and then after the RD has
4376 * been changed. The first invocation will result in local routes
4377 * of this VNI being deleted and withdrawn and the next will result
4378 * in the routes being re-advertised.
4380 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4384 delete_withdraw_vni_routes(bgp
, vpn
);
4386 update_advertise_vni_routes(bgp
, vpn
);
4390 * Install routes for this VNI. Invoked upon change to Import RT.
4392 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4394 return install_routes_for_vni(bgp
, vpn
);
4398 * Uninstall all routes installed for this VNI. Invoked upon change
4401 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4403 return uninstall_routes_for_vni(bgp
, vpn
);
4407 * TODO: Hardcoded for a maximum of 2 VNIs right now
4409 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4414 vni1
= label2vni(label
);
4415 if (num_labels
== 2) {
4416 vni2
= label2vni(label
+ 1);
4417 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4419 snprintf(buf
, len
, "%u", vni1
);
4424 * Function to convert evpn route to json format.
4425 * NOTE: We don't use prefix2str as the output here is a bit different.
4427 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
4429 char buf1
[ETHER_ADDR_STRLEN
];
4430 char buf2
[PREFIX2STR_BUFFER
];
4435 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4436 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4437 json_object_int_add(json
, "ethTag",
4438 p
->prefix
.imet_addr
.eth_tag
);
4439 json_object_int_add(json
, "ipLen",
4440 is_evpn_prefix_ipaddr_v4(p
)
4443 json_object_string_add(json
, "ip",
4444 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4445 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4446 if (is_evpn_prefix_ipaddr_none(p
)) {
4447 json_object_int_add(json
, "routeType",
4448 p
->prefix
.route_type
);
4449 json_object_int_add(json
, "ethTag",
4450 p
->prefix
.macip_addr
.eth_tag
);
4451 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4452 json_object_string_add(json
, "mac",
4453 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4459 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4462 json_object_int_add(json
, "routeType",
4463 p
->prefix
.route_type
);
4464 json_object_int_add(json
, "ethTag",
4465 p
->prefix
.macip_addr
.eth_tag
);
4466 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4467 json_object_string_add(json
, "mac",
4468 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4471 json_object_int_add(json
, "ipLen",
4472 is_evpn_prefix_ipaddr_v4(p
)
4475 json_object_string_add(
4478 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4480 PREFIX2STR_BUFFER
));
4483 /* Currently, this is to cater to other AF_ETHERNET code. */
4488 * Function to convert evpn route to string.
4489 * NOTE: We don't use prefix2str as the output here is a bit different.
4491 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
4493 char buf1
[ETHER_ADDR_STRLEN
];
4494 char buf2
[PREFIX2STR_BUFFER
];
4495 char buf3
[ESI_STR_LEN
];
4497 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4498 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
4499 p
->prefix
.imet_addr
.eth_tag
,
4500 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4502 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4503 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4504 if (is_evpn_prefix_ipaddr_none(p
))
4505 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4506 p
->prefix
.route_type
,
4507 p
->prefix
.macip_addr
.eth_tag
,
4509 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4514 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4516 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
4517 p
->prefix
.route_type
,
4518 p
->prefix
.macip_addr
.eth_tag
,
4520 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4522 family
== AF_INET
? IPV4_MAX_BITLEN
4525 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4527 PREFIX2STR_BUFFER
));
4529 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
4530 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4531 p
->prefix
.route_type
,
4532 p
->prefix
.prefix_addr
.eth_tag
,
4533 p
->prefix
.prefix_addr
.ip_prefix_length
,
4534 is_evpn_prefix_ipaddr_v4(p
)
4535 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
4536 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
4537 } else if (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
4538 snprintf(buf
, len
, "[%d]:[%s]:[%d]:[%s]",
4539 p
->prefix
.route_type
,
4540 esi_to_str(&p
->prefix
.es_addr
.esi
, buf3
, sizeof(buf3
)),
4541 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4543 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
));
4545 /* For EVPN route types not supported yet. */
4546 snprintf(buf
, len
, "(unsupported route type %d)",
4547 p
->prefix
.route_type
);
4554 * Encode EVPN prefix in Update (MP_REACH)
4556 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
4557 struct prefix_rd
*prd
, mpls_label_t
*label
,
4558 uint32_t num_labels
, struct attr
*attr
,
4559 int addpath_encode
, uint32_t addpath_tx_id
)
4561 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4562 int len
, ipa_len
= 0;
4565 stream_putl(s
, addpath_tx_id
);
4568 stream_putc(s
, evp
->prefix
.route_type
);
4570 switch (evp
->prefix
.route_type
) {
4571 case BGP_EVPN_MAC_IP_ROUTE
:
4572 if (is_evpn_prefix_ipaddr_v4(evp
))
4573 ipa_len
= IPV4_MAX_BYTELEN
;
4574 else if (is_evpn_prefix_ipaddr_v6(evp
))
4575 ipa_len
= IPV6_MAX_BYTELEN
;
4576 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4577 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4578 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4580 stream_putc(s
, len
);
4581 stream_put(s
, prd
->val
, 8); /* RD */
4583 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
4585 stream_put(s
, 0, 10);
4586 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4587 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4588 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4589 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4590 if (ipa_len
) /* IP */
4591 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4593 /* 1st label is the L2 VNI */
4594 stream_put(s
, label
, BGP_LABEL_BYTES
);
4595 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4596 if (ipa_len
&& num_labels
> 1)
4597 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4600 case BGP_EVPN_IMET_ROUTE
:
4601 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4602 stream_put(s
, prd
->val
, 8); /* RD */
4603 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4604 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4605 /* Originating Router's IP Addr */
4606 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4609 case BGP_EVPN_ES_ROUTE
:
4610 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4611 stream_put(s
, prd
->val
, 8); /* RD */
4612 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4613 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4615 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4618 case BGP_EVPN_IP_PREFIX_ROUTE
:
4619 /* TODO: AddPath support. */
4620 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4628 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4629 struct bgp_nlri
*packet
, int withdraw
)
4635 uint32_t addpath_id
;
4636 int addpath_encoded
;
4641 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4643 lim
= pnt
+ packet
->length
;
4645 safi
= packet
->safi
;
4649 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
4650 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
4651 PEER_CAP_ADDPATH_AF_TX_RCV
));
4653 for (; pnt
< lim
; pnt
+= psize
) {
4654 /* Clear prefix structure. */
4655 memset(&p
, 0, sizeof(struct prefix
));
4657 /* Deal with path-id if AddPath is supported. */
4658 if (addpath_encoded
) {
4659 /* When packet overflow occurs return immediately. */
4660 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4663 addpath_id
= ntohl(*((uint32_t *)pnt
));
4664 pnt
+= BGP_ADDPATH_ID_LEN
;
4667 /* All EVPN NLRI types start with type and length. */
4674 /* When packet overflow occur return immediately. */
4675 if (pnt
+ psize
> lim
)
4679 case BGP_EVPN_MAC_IP_ROUTE
:
4680 if (process_type2_route(peer
, afi
, safi
,
4681 withdraw
? NULL
: attr
, pnt
,
4682 psize
, addpath_id
)) {
4685 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4686 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4691 case BGP_EVPN_IMET_ROUTE
:
4692 if (process_type3_route(peer
, afi
, safi
,
4693 withdraw
? NULL
: attr
, pnt
,
4694 psize
, addpath_id
)) {
4697 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4698 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4703 case BGP_EVPN_ES_ROUTE
:
4704 if (process_type4_route(peer
, afi
, safi
,
4705 withdraw
? NULL
: attr
, pnt
,
4706 psize
, addpath_id
)) {
4709 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4710 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4715 case BGP_EVPN_IP_PREFIX_ROUTE
:
4716 if (process_type5_route(peer
, afi
, safi
, attr
, pnt
,
4717 psize
, addpath_id
, withdraw
)) {
4720 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4721 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4731 /* Packet length consistency check. */
4739 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
4740 * The mapping will be used during route processing.
4741 * bgp_def: default bgp instance
4742 * bgp_vrf: specific bgp vrf instance on which RT is configured
4744 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
4747 struct ecommunity_val
*eval
= NULL
;
4748 struct listnode
*node
= NULL
, *nnode
= NULL
;
4749 struct ecommunity
*ecom
= NULL
;
4751 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4752 for (i
= 0; i
< ecom
->size
; i
++) {
4753 eval
= (struct ecommunity_val
*)(ecom
->val
4755 * ECOMMUNITY_SIZE
));
4756 map_vrf_to_rt(bgp_vrf
, eval
);
4762 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
4764 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
4767 struct ecommunity_val
*eval
;
4768 struct listnode
*node
, *nnode
;
4769 struct ecommunity
*ecom
;
4771 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4772 for (i
= 0; i
< ecom
->size
; i
++) {
4773 struct vrf_irt_node
*irt
;
4774 struct ecommunity_val eval_tmp
;
4776 eval
= (struct ecommunity_val
*)(ecom
->val
4778 * ECOMMUNITY_SIZE
));
4779 /* If using "automatic" RT, we only care about the
4780 * local-admin sub-field.
4781 * This is to facilitate using VNI as the RT for EBGP
4784 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4785 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
4786 BGP_VRF_IMPORT_RT_CFGD
))
4787 mask_ecom_global_admin(&eval_tmp
, eval
);
4789 irt
= lookup_vrf_import_rt(&eval_tmp
);
4791 unmap_vrf_from_rt(bgp_vrf
, irt
);
4798 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
4799 * The mapping will be used during route processing.
4801 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4804 struct ecommunity_val
*eval
;
4805 struct listnode
*node
, *nnode
;
4806 struct ecommunity
*ecom
;
4808 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4809 for (i
= 0; i
< ecom
->size
; i
++) {
4810 eval
= (struct ecommunity_val
*)(ecom
->val
4812 * ECOMMUNITY_SIZE
));
4813 map_vni_to_rt(bgp
, vpn
, eval
);
4819 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
4821 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4824 struct ecommunity_val
*eval
;
4825 struct listnode
*node
, *nnode
;
4826 struct ecommunity
*ecom
;
4828 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4829 for (i
= 0; i
< ecom
->size
; i
++) {
4830 struct irt_node
*irt
;
4831 struct ecommunity_val eval_tmp
;
4833 eval
= (struct ecommunity_val
*)(ecom
->val
4835 * ECOMMUNITY_SIZE
));
4836 /* If using "automatic" RT, we only care about the
4837 * local-admin sub-field.
4838 * This is to facilitate using VNI as the RT for EBGP
4841 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4842 if (!is_import_rt_configured(vpn
))
4843 mask_ecom_global_admin(&eval_tmp
, eval
);
4845 irt
= lookup_import_rt(bgp
, &eval_tmp
);
4847 unmap_vni_from_rt(bgp
, vpn
, irt
);
4853 * Derive Import RT automatically for VNI and map VNI to RT.
4854 * The mapping will be used during route processing.
4856 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4858 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
4859 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
4862 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
4866 * Derive Export RT automatically for VNI.
4868 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4870 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
4871 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
4875 * Derive RD automatically for VNI using passed information - it
4876 * is of the form RouterId:unique-id-for-vni.
4878 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
4880 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
4884 * Derive RD automatically for VNI using passed information - it
4885 * is of the form RouterId:unique-id-for-vni.
4887 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4891 vpn
->prd
.family
= AF_UNSPEC
;
4892 vpn
->prd
.prefixlen
= 64;
4893 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
4894 (void)str2prefix_rd(buf
, &vpn
->prd
);
4895 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
4901 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
4903 struct list
*inst
= bm
->bgp
;
4904 struct listnode
*node
;
4905 struct bgp
*bgp_vrf
;
4907 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
4908 if (bgp_vrf
->l3vni
== vni
)
4918 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
4920 struct bgpevpn
*vpn
;
4923 memset(&tmp
, 0, sizeof(struct bgpevpn
));
4925 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
4930 * Create a new vpn - invoked upon configuration or zebra notification.
4932 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
4933 struct in_addr originator_ip
,
4934 vrf_id_t tenant_vrf_id
)
4936 struct bgpevpn
*vpn
;
4941 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
4945 /* Set values - RD and RT set to defaults. */
4947 vpn
->originator_ip
= originator_ip
;
4948 vpn
->tenant_vrf_id
= tenant_vrf_id
;
4950 /* Initialize route-target import and export lists */
4951 vpn
->import_rtl
= list_new();
4952 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4953 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
4954 vpn
->export_rtl
= list_new();
4955 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4956 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
4957 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
4958 derive_rd_rt_for_vni(bgp
, vpn
);
4960 /* Initialize EVPN route table. */
4961 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
4964 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
4965 XFREE(MTYPE_BGP_EVPN
, vpn
);
4969 /* add to l2vni list on corresponding vrf */
4970 bgpevpn_link_to_l3vni(vpn
);
4972 QOBJ_REG(vpn
, bgpevpn
);
4977 * Free a given VPN - called in multiple scenarios such as zebra
4978 * notification, configuration being deleted, advertise-all-vni disabled etc.
4979 * This just frees appropriate memory, caller should have taken other
4982 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4984 bgpevpn_unlink_from_l3vni(vpn
);
4985 bgp_table_unlock(vpn
->route_table
);
4986 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4987 list_delete(&vpn
->import_rtl
);
4988 list_delete(&vpn
->export_rtl
);
4989 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
4990 hash_release(bgp
->vnihash
, vpn
);
4992 XFREE(MTYPE_BGP_EVPN
, vpn
);
4998 struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
)
5003 memset(&tmp
, 0, sizeof(struct evpnes
));
5004 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
5005 es
= hash_lookup(bgp
->esihash
, &tmp
);
5010 * Create a new local es - invoked upon zebra notification.
5012 struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
,
5014 struct ipaddr
*originator_ip
)
5022 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct evpnes
));
5026 /* set the ESI and originator_ip */
5027 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
5028 memcpy(&es
->originator_ip
, originator_ip
, sizeof(struct ipaddr
));
5030 /* Initialise the VTEP list */
5031 es
->vtep_list
= list_new();
5032 es
->vtep_list
->cmp
= (int (*)(void *, void *))evpn_vtep_ip_cmp
;
5034 /* auto derive RD for this es */
5035 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
5036 es
->prd
.family
= AF_UNSPEC
;
5037 es
->prd
.prefixlen
= 64;
5038 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), es
->rd_id
);
5039 (void)str2prefix_rd(buf
, &es
->prd
);
5041 /* Initialize the ES route table */
5042 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5045 if (!hash_get(bgp
->esihash
, es
, hash_alloc_intern
)) {
5046 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5050 QOBJ_REG(es
, evpnes
);
5056 * This just frees appropriate memory, caller should have taken other
5059 void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
)
5061 list_delete(&es
->vtep_list
);
5062 bgp_table_unlock(es
->route_table
);
5063 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
5064 hash_release(bgp
->esihash
, es
);
5066 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5070 * Import evpn route from global table to VNI/VRF/ESI.
5072 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5073 struct prefix
*p
, struct bgp_path_info
*ri
)
5075 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 1);
5079 * Unimport evpn route from VNI/VRF/ESI.
5081 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5082 struct prefix
*p
, struct bgp_path_info
*ri
)
5084 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 0);
5087 /* filter routes which have martian next hops */
5088 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5092 struct bgp_node
*rd_rn
, *rn
;
5093 struct bgp_table
*table
;
5094 struct bgp_path_info
*ri
;
5099 /* Walk entire global routing table and evaluate routes which could be
5100 * imported into this VPN. Note that we cannot just look at the routes
5101 * for the VNI's RD -
5102 * remote routes applicable for this VNI could have any RD.
5104 /* EVPN routes are a 2-level table. */
5105 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
5106 rd_rn
= bgp_route_next(rd_rn
)) {
5107 table
= (struct bgp_table
*)(rd_rn
->info
);
5111 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5113 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
5115 /* Consider "valid" remote routes applicable for
5117 if (!(ri
->type
== ZEBRA_ROUTE_BGP
5118 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
5121 if (bgp_nexthop_self(bgp
, ri
->attr
->nexthop
)) {
5123 char attr_str
[BUFSIZ
];
5124 char pbuf
[PREFIX_STRLEN
];
5126 bgp_dump_attr(ri
->attr
, attr_str
,
5129 if (bgp_debug_update(ri
->peer
, &rn
->p
,
5132 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
5139 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5142 bgp_rib_remove(rn
, ri
, ri
->peer
, afi
,
5153 * Handle del of a local MACIP.
5155 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5158 struct bgpevpn
*vpn
;
5159 struct prefix_evpn p
;
5161 /* Lookup VNI hash - should exist. */
5162 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5163 if (!vpn
|| !is_vni_live(vpn
)) {
5164 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5165 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5166 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5170 /* Remove EVPN type-2 route and schedule for processing. */
5171 build_evpn_type2_prefix(&p
, mac
, ip
);
5172 delete_evpn_route(bgp
, vpn
, &p
);
5178 * Handle add of a local MACIP.
5180 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5181 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
)
5183 struct bgpevpn
*vpn
;
5184 struct prefix_evpn p
;
5186 /* Lookup VNI hash - should exist. */
5187 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5188 if (!vpn
|| !is_vni_live(vpn
)) {
5189 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5190 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5191 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5195 /* Create EVPN type-2 route and schedule for processing. */
5196 build_evpn_type2_prefix(&p
, mac
, ip
);
5197 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
)) {
5198 char buf
[ETHER_ADDR_STRLEN
];
5199 char buf2
[INET6_ADDRSTRLEN
];
5202 EC_BGP_EVPN_ROUTE_CREATE
,
5203 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5204 bgp
->vrf_id
, vpn
->vni
,
5205 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5208 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5209 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5216 static void link_l2vni_hash_to_l3vni(struct hash_backet
*backet
,
5217 struct bgp
*bgp_vrf
)
5219 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
5220 struct bgp
*bgp_def
= NULL
;
5222 bgp_def
= bgp_get_default();
5225 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5226 bgpevpn_link_to_l3vni(vpn
);
5229 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
, struct ethaddr
*rmac
,
5230 struct in_addr originator_ip
, int filter
)
5232 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5233 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5234 struct listnode
*node
= NULL
;
5235 struct bgpevpn
*vpn
= NULL
;
5238 /* get the default instance - required to get the AS number for VRF
5241 bgp_def
= bgp_get_default();
5245 "Cannot process L3VNI %u ADD - default BGP instance not yet created",
5251 /* if the BGP vrf instance doesnt exist - create one */
5252 bgp_vrf
= bgp_lookup_by_name(vrf_id_to_name(vrf_id
));
5257 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5258 BGP_INSTANCE_TYPE_VRF
);
5260 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
:
5261 flog_err(EC_BGP_MULTI_INSTANCE
,
5262 "'bgp multiple-instance' not present\n");
5264 case BGP_ERR_AS_MISMATCH
:
5265 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5266 "BGP is already running; AS is %u\n", as
);
5268 case BGP_ERR_INSTANCE_MISMATCH
:
5269 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5270 "BGP instance name and AS number mismatch\n");
5274 /* mark as auto created */
5275 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5278 /* associate with l3vni */
5279 bgp_vrf
->l3vni
= l3vni
;
5281 /* set the router mac - to be used in mac-ip routes for this vrf */
5282 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
5284 /* set the originator ip */
5285 bgp_vrf
->originator_ip
= originator_ip
;
5287 /* set the right filter - are we using l3vni only for prefix routes? */
5289 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5291 /* auto derive RD/RT */
5292 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5293 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5294 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5295 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5296 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5298 /* link all corresponding l2vnis */
5299 hash_iterate(bgp_def
->vnihash
,
5300 (void (*)(struct hash_backet
*,
5301 void *))link_l2vni_hash_to_l3vni
,
5304 /* Only update all corresponding type-2 routes if we are advertising two
5305 * labels along with type-2 routes
5308 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5309 update_routes_for_vni(bgp_def
, vpn
);
5311 /* advertise type-5 routes if needed */
5312 update_advertise_vrf_routes(bgp_vrf
);
5314 /* install all remote routes belonging to this l3vni into correspondng
5316 install_routes_for_vrf(bgp_vrf
);
5321 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5323 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5324 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5325 struct listnode
*node
= NULL
;
5326 struct listnode
*next
= NULL
;
5327 struct bgpevpn
*vpn
= NULL
;
5329 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5333 "Cannot process L3VNI %u Del - Could not find BGP instance",
5338 bgp_def
= bgp_get_default();
5342 "Cannot process L3VNI %u Del - Could not find default BGP instance",
5347 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5348 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5349 * routes. This will uninstalling the routes from zebra and decremnt the
5352 uninstall_routes_for_vrf(bgp_vrf
);
5354 /* delete/withdraw all type-5 routes */
5355 delete_withdraw_vrf_routes(bgp_vrf
);
5357 /* remove the l3vni from vrf instance */
5360 /* remove the Rmac from the BGP vrf */
5361 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5364 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5365 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5366 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5368 if (!list_isempty(bgp_vrf
->vrf_export_rtl
)) {
5369 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5372 /* update all corresponding local mac-ip routes */
5373 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5374 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5375 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5376 update_routes_for_vni(bgp_def
, vpn
);
5380 /* If any L2VNIs point to this instance, unlink them. */
5381 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5382 bgpevpn_unlink_from_l3vni(vpn
);
5384 /* Delete the instance if it was autocreated */
5385 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5386 bgp_delete(bgp_vrf
);
5392 * Handle del of a local VNI.
5394 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5396 struct bgpevpn
*vpn
;
5398 /* Locate VNI hash */
5399 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5401 if (bgp_debug_zebra(NULL
))
5403 EC_BGP_EVPN_VPN_VNI
,
5404 "%u: VNI hash entry for VNI %u not found at DEL",
5409 /* Remove all local EVPN routes and schedule for processing (to
5410 * withdraw from peers).
5412 delete_routes_for_vni(bgp
, vpn
);
5415 * tunnel is no longer active, del tunnel ip address from tip_hash
5417 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5419 /* Clear "live" flag and see if hash needs to be freed. */
5420 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5421 if (!is_vni_configured(vpn
))
5422 bgp_evpn_free(bgp
, vpn
);
5428 * Handle add (or update) of a local VNI. The VNI changes we care
5429 * about are for the local-tunnel-ip and the (tenant) VRF.
5431 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5432 struct in_addr originator_ip
, vrf_id_t tenant_vrf_id
)
5434 struct bgpevpn
*vpn
;
5435 struct prefix_evpn p
;
5437 /* Lookup VNI. If present and no change, exit. */
5438 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5441 if (is_vni_live(vpn
)
5442 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5443 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5444 /* Probably some other param has changed that we don't
5448 /* Update tenant_vrf_id if it has changed. */
5449 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5450 bgpevpn_unlink_from_l3vni(vpn
);
5451 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5452 bgpevpn_link_to_l3vni(vpn
);
5455 /* If tunnel endpoint IP has changed, update (and delete prior
5456 * type-3 route, if needed.)
5458 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5459 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5461 /* Update all routes with new endpoint IP and/or export RT
5464 if (is_vni_live(vpn
))
5465 update_routes_for_vni(bgp
, vpn
);
5468 /* Create or update as appropriate. */
5470 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
);
5474 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5480 /* if the VNI is live already, there is nothing more to do */
5481 if (is_vni_live(vpn
))
5484 /* Mark as "live" */
5485 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5487 /* tunnel is now active, add tunnel-ip to db */
5488 bgp_tip_add(bgp
, &originator_ip
);
5490 /* filter routes as nexthop database has changed */
5491 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5493 /* Create EVPN type-3 route and schedule for processing. */
5494 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5495 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0)) {
5496 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5497 "%u: Type3 route creation failure for VNI %u",
5502 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5506 install_routes_for_vni(bgp
, vpn
);
5508 /* If we are advertising gateway mac-ip
5509 It needs to be conveyed again to zebra */
5510 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5516 * bgp_evpn_local_es_del
5518 int bgp_evpn_local_es_del(struct bgp
*bgp
,
5520 struct ipaddr
*originator_ip
)
5522 char buf
[ESI_STR_LEN
];
5523 struct evpnes
*es
= NULL
;
5525 if (!bgp
->esihash
) {
5526 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5531 /* Lookup ESI hash - should exist. */
5532 es
= bgp_evpn_lookup_es(bgp
, esi
);
5534 flog_warn(EC_BGP_EVPN_ESI
,
5535 "%u: ESI hash entry for ESI %s at Local ES DEL",
5536 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5540 /* Delete all local EVPN ES routes from ESI table
5541 * and schedule for processing (to withdraw from peers))
5543 delete_routes_for_es(bgp
, es
);
5545 /* free the hash entry */
5546 bgp_evpn_es_free(bgp
, es
);
5552 * bgp_evpn_local_es_add
5554 int bgp_evpn_local_es_add(struct bgp
*bgp
,
5556 struct ipaddr
*originator_ip
)
5558 char buf
[ESI_STR_LEN
];
5559 struct evpnes
*es
= NULL
;
5560 struct prefix_evpn p
;
5562 if (!bgp
->esihash
) {
5563 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5568 /* create the new es */
5569 es
= bgp_evpn_lookup_es(bgp
, esi
);
5571 es
= bgp_evpn_es_new(bgp
, esi
, originator_ip
);
5575 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
5576 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5580 UNSET_FLAG(es
->flags
, EVPNES_REMOTE
);
5581 SET_FLAG(es
->flags
, EVPNES_LOCAL
);
5583 build_evpn_type4_prefix(&p
, esi
, originator_ip
->ipaddr_v4
);
5584 if (update_evpn_type4_route(bgp
, es
, &p
)) {
5585 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5586 "%u: Type4 route creation failure for ESI %s",
5587 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5591 /* import all remote ES routes in th ES table */
5592 install_routes_for_es(bgp
, es
);
5598 * Cleanup EVPN information on disable - Need to delete and withdraw
5599 * EVPN routes from peers.
5601 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5603 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
5604 void *))cleanup_vni_on_disable
,
5609 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5610 * BGP instance (default) is being freed.
5612 void bgp_evpn_cleanup(struct bgp
*bgp
)
5614 hash_iterate(bgp
->vnihash
,
5615 (void (*)(struct hash_backet
*, void *))free_vni_entry
,
5618 hash_free(bgp
->import_rt_hash
);
5619 bgp
->import_rt_hash
= NULL
;
5621 hash_free(bgp
->vrf_import_rt_hash
);
5622 bgp
->vrf_import_rt_hash
= NULL
;
5624 hash_free(bgp
->vnihash
);
5625 bgp
->vnihash
= NULL
;
5627 hash_free(bgp
->esihash
);
5628 bgp
->esihash
= NULL
;
5630 list_delete(&bgp
->vrf_import_rtl
);
5631 list_delete(&bgp
->vrf_export_rtl
);
5632 list_delete(&bgp
->l2vnis
);
5636 * Initialization for EVPN
5639 * hash for RT to VNI
5641 void bgp_evpn_init(struct bgp
*bgp
)
5644 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
5646 hash_create(esi_hash_keymake
, esi_cmp
,
5647 "BGP EVPN Local ESI Hash");
5648 bgp
->import_rt_hash
=
5649 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
5650 "BGP Import RT Hash");
5651 bgp
->vrf_import_rt_hash
=
5652 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
5653 "BGP VRF Import RT Hash");
5654 bgp
->vrf_import_rtl
= list_new();
5655 bgp
->vrf_import_rtl
->cmp
=
5656 (int (*)(void *, void *))evpn_route_target_cmp
;
5657 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
5658 bgp
->vrf_export_rtl
= list_new();
5659 bgp
->vrf_export_rtl
->cmp
=
5660 (int (*)(void *, void *))evpn_route_target_cmp
;
5661 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
5662 bgp
->l2vnis
= list_new();
5663 bgp
->l2vnis
->cmp
= (int (*)(void *, void *))vni_hash_cmp
;
5666 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
5668 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);