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
**pi
);
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
);
137 static int vni_list_cmp(void *p1
, void *p2
)
139 const struct bgpevpn
*vpn1
= p1
;
140 const struct bgpevpn
*vpn2
= p2
;
142 return vpn1
->vni
- vpn2
->vni
;
146 * Make vrf import route target hash key.
148 static unsigned int vrf_import_rt_hash_key_make(void *p
)
150 struct vrf_irt_node
*irt
= p
;
151 char *pnt
= irt
->rt
.val
;
153 return jhash(pnt
, 8, 0x5abc1234);
157 * Comparison function for vrf import rt hash
159 static int vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
161 const struct vrf_irt_node
*irt1
= p1
;
162 const struct vrf_irt_node
*irt2
= p2
;
164 if (irt1
== NULL
&& irt2
== NULL
)
167 if (irt1
== NULL
|| irt2
== NULL
)
170 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
174 * Create a new vrf import_rt in default instance
176 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
178 struct bgp
*bgp_def
= NULL
;
179 struct vrf_irt_node
*irt
;
181 bgp_def
= bgp_get_default();
183 flog_err(EC_BGP_NO_DFLT
,
184 "vrf import rt new - def instance not created yet");
188 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
189 sizeof(struct vrf_irt_node
));
194 irt
->vrfs
= list_new();
197 if (!hash_get(bgp_def
->vrf_import_rt_hash
, irt
, hash_alloc_intern
)) {
198 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
206 * Free the vrf import rt node
208 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
210 struct bgp
*bgp_def
= NULL
;
212 bgp_def
= bgp_get_default();
214 flog_err(EC_BGP_NO_DFLT
,
215 "vrf import rt free - def instance not created yet");
219 hash_release(bgp_def
->vrf_import_rt_hash
, irt
);
220 list_delete(&irt
->vrfs
);
221 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
225 * Function to lookup Import RT node - used to map a RT to set of
226 * VNIs importing routes with that RT.
228 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
230 struct bgp
*bgp_def
= NULL
;
231 struct vrf_irt_node
*irt
;
232 struct vrf_irt_node tmp
;
234 bgp_def
= bgp_get_default();
236 flog_err(EC_BGP_NO_DFLT
,
237 "vrf import rt lookup - def instance not created yet");
241 memset(&tmp
, 0, sizeof(struct vrf_irt_node
));
242 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
243 irt
= hash_lookup(bgp_def
->vrf_import_rt_hash
, &tmp
);
248 * Is specified VRF present on the RT's list of "importing" VRFs?
250 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
, struct bgp
*bgp_vrf
)
252 struct listnode
*node
= NULL
, *nnode
= NULL
;
253 struct bgp
*tmp_bgp_vrf
= NULL
;
255 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
256 if (tmp_bgp_vrf
== bgp_vrf
)
263 * Make import route target hash key.
265 static unsigned int import_rt_hash_key_make(void *p
)
267 struct irt_node
*irt
= p
;
268 char *pnt
= irt
->rt
.val
;
270 return jhash(pnt
, 8, 0xdeadbeef);
274 * Comparison function for import rt hash
276 static int import_rt_hash_cmp(const void *p1
, const void *p2
)
278 const struct irt_node
*irt1
= p1
;
279 const struct irt_node
*irt2
= p2
;
281 if (irt1
== NULL
&& irt2
== NULL
)
284 if (irt1
== NULL
|| irt2
== NULL
)
287 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
291 * Create a new import_rt
293 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
294 struct ecommunity_val
*rt
)
296 struct irt_node
*irt
;
301 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
306 irt
->vnis
= list_new();
309 if (!hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
)) {
310 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
318 * Free the import rt node
320 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
322 hash_release(bgp
->import_rt_hash
, irt
);
323 list_delete(&irt
->vnis
);
324 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
328 * Function to lookup Import RT node - used to map a RT to set of
329 * VNIs importing routes with that RT.
331 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
332 struct ecommunity_val
*rt
)
334 struct irt_node
*irt
;
337 memset(&tmp
, 0, sizeof(struct irt_node
));
338 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
339 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
344 * Is specified VNI present on the RT's list of "importing" VNIs?
346 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
348 struct listnode
*node
, *nnode
;
349 struct bgpevpn
*tmp_vpn
;
351 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
360 * Compare Route Targets.
362 static int evpn_route_target_cmp(struct ecommunity
*ecom1
,
363 struct ecommunity
*ecom2
)
371 if (!ecom1
&& !ecom2
)
374 if (ecom1
->str
&& !ecom2
->str
)
377 if (!ecom1
->str
&& ecom2
->str
)
380 if (!ecom1
->str
&& !ecom2
->str
)
383 return strcmp(ecom1
->str
, ecom2
->str
);
386 static void evpn_xxport_delete_ecomm(void *val
)
388 struct ecommunity
*ecomm
= val
;
389 ecommunity_free(&ecomm
);
393 * Mask off global-admin field of specified extended community (RT),
394 * just retain the local-admin field.
396 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
397 struct ecommunity_val
*src
)
403 if (type
== ECOMMUNITY_ENCODE_AS
) {
404 dst
->val
[2] = dst
->val
[3] = 0;
405 } else if (type
== ECOMMUNITY_ENCODE_AS4
406 || type
== ECOMMUNITY_ENCODE_IP
) {
407 dst
->val
[2] = dst
->val
[3] = 0;
408 dst
->val
[4] = dst
->val
[5] = 0;
413 * Map one RT to specified VRF.
414 * bgp_vrf = BGP vrf instance
416 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct ecommunity_val
*eval
)
418 struct vrf_irt_node
*irt
= NULL
;
419 struct ecommunity_val eval_tmp
;
421 /* If using "automatic" RT,
422 * we only care about the local-admin sub-field.
423 * This is to facilitate using L3VNI(VRF-VNI)
424 * as the RT for EBGP peering too.
426 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
427 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
428 mask_ecom_global_admin(&eval_tmp
, eval
);
430 irt
= lookup_vrf_import_rt(&eval_tmp
);
431 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
432 /* Already mapped. */
436 irt
= vrf_import_rt_new(&eval_tmp
);
438 /* Add VRF to the list for this RT. */
439 listnode_add(irt
->vrfs
, bgp_vrf
);
443 * Unmap specified VRF from specified RT. If there are no other
444 * VRFs for this RT, then the RT hash is deleted.
445 * bgp_vrf: BGP VRF specific instance
447 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
, struct vrf_irt_node
*irt
)
449 /* Delete VRF from list for this RT. */
450 listnode_delete(irt
->vrfs
, bgp_vrf
);
451 if (!listnode_head(irt
->vrfs
)) {
452 vrf_import_rt_free(irt
);
457 * Map one RT to specified VNI.
459 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
460 struct ecommunity_val
*eval
)
462 struct irt_node
*irt
;
463 struct ecommunity_val eval_tmp
;
465 /* If using "automatic" RT, we only care about the local-admin
467 * This is to facilitate using VNI as the RT for EBGP peering too.
469 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
470 if (!is_import_rt_configured(vpn
))
471 mask_ecom_global_admin(&eval_tmp
, eval
);
473 irt
= lookup_import_rt(bgp
, &eval_tmp
);
475 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
476 /* Already mapped. */
480 irt
= import_rt_new(bgp
, &eval_tmp
);
484 /* Add VNI to the hash list for this RT. */
485 listnode_add(irt
->vnis
, vpn
);
489 * Unmap specified VNI from specified RT. If there are no other
490 * VNIs for this RT, then the RT hash is deleted.
492 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
493 struct irt_node
*irt
)
495 /* Delete VNI from hash list for this RT. */
496 listnode_delete(irt
->vnis
, vpn
);
497 if (!listnode_head(irt
->vnis
)) {
498 import_rt_free(bgp
, irt
);
503 * Create RT extended community automatically from passed information:
504 * of the form AS:VNI.
505 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
506 * the need is to get a RT value that will be unique across different
507 * VNIs but the same across routers (in the same AS) for a particular
510 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
512 struct ecommunity_val eval
;
513 struct ecommunity
*ecomadd
;
515 if (bgp
->advertise_autort_rfc8365
)
516 vni
|= EVPN_AUTORT_VXLAN
;
517 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
519 ecomadd
= ecommunity_new();
520 ecommunity_add_val(ecomadd
, &eval
);
521 listnode_add_sort(rtl
, ecomadd
);
525 * Derive RD and RT for a VNI automatically. Invoked at the time of
528 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
530 bgp_evpn_derive_auto_rd(bgp
, vpn
);
531 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
532 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
536 * Convert nexthop (remote VTEP IP) into an IPv6 address.
538 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
540 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
542 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
543 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
547 * Add (update) or delete MACIP from zebra.
549 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
550 struct prefix_evpn
*p
,
551 struct in_addr remote_vtep_ip
, int add
,
552 uint8_t flags
, uint32_t seq
)
556 char buf1
[ETHER_ADDR_STRLEN
];
557 char buf2
[INET6_ADDRSTRLEN
];
558 char buf3
[INET6_ADDRSTRLEN
];
561 if (!zclient
|| zclient
->sock
< 0)
564 /* Don't try to register if Zebra doesn't know of this instance. */
565 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
571 zclient_create_header(
572 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
574 stream_putl(s
, vpn
->vni
);
575 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
576 /* IP address length and IP address, if any. */
577 if (is_evpn_prefix_ipaddr_none(p
))
580 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
582 stream_putl(s
, ipa_len
);
583 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
585 stream_put_in_addr(s
, &remote_vtep_ip
);
587 /* TX flags - MAC sticky status and/or gateway mac */
588 /* Also TX the sequence number of the best route. */
590 stream_putc(s
, flags
);
594 stream_putw_at(s
, 0, stream_get_endp(s
));
596 if (bgp_debug_zebra(NULL
))
598 "Tx %s MACIP, VNI %u MAC %s IP %s flags 0x%x seq %u remote VTEP %s",
599 add
? "ADD" : "DEL", vpn
->vni
,
600 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
602 ipaddr2str(&p
->prefix
.macip_addr
.ip
,
603 buf3
, sizeof(buf3
)), flags
, seq
,
604 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
607 return zclient_send_message(zclient
);
611 * Add (update) or delete remote VTEP from zebra.
613 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
614 struct prefix_evpn
*p
, int add
)
619 if (!zclient
|| zclient
->sock
< 0)
622 /* Don't try to register if Zebra doesn't know of this instance. */
623 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
629 zclient_create_header(
630 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
632 stream_putl(s
, vpn
->vni
);
633 if (is_evpn_prefix_ipaddr_v4(p
))
634 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
635 else if (is_evpn_prefix_ipaddr_v6(p
)) {
638 "Bad remote IP when trying to %s remote VTEP for VNI %u",
639 add
? "ADD" : "DEL", vpn
->vni
);
643 stream_putw_at(s
, 0, stream_get_endp(s
));
645 if (bgp_debug_zebra(NULL
))
646 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
647 add
? "ADD" : "DEL", vpn
->vni
,
648 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
650 return zclient_send_message(zclient
);
654 * Build extended community for EVPN ES (type-4) route
656 static void build_evpn_type4_route_extcomm(struct evpnes
*es
,
659 struct ecommunity ecom_encap
;
660 struct ecommunity ecom_es_rt
;
661 struct ecommunity_val eval
;
662 struct ecommunity_val eval_es_rt
;
663 bgp_encap_types tnl_type
;
667 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
668 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
669 encode_encap_extcomm(tnl_type
, &eval
);
671 ecom_encap
.val
= (uint8_t *)eval
.val
;
672 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
675 memset(&mac
, 0, sizeof(struct ethaddr
));
676 memset(&ecom_es_rt
, 0, sizeof(ecom_es_rt
));
677 es_get_system_mac(&es
->esi
, &mac
);
678 encode_es_rt_extcomm(&eval_es_rt
, &mac
);
680 ecom_es_rt
.val
= (uint8_t *)eval_es_rt
.val
;
682 ecommunity_merge(attr
->ecommunity
, &ecom_es_rt
);
684 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
688 * Build extended communities for EVPN prefix route.
690 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
693 struct ecommunity ecom_encap
;
694 struct ecommunity ecom_rmac
;
695 struct ecommunity_val eval
;
696 struct ecommunity_val eval_rmac
;
697 bgp_encap_types tnl_type
;
698 struct listnode
*node
, *nnode
;
699 struct ecommunity
*ecom
;
700 struct list
*vrf_export_rtl
= NULL
;
703 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
704 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
705 encode_encap_extcomm(tnl_type
, &eval
);
707 ecom_encap
.val
= (uint8_t *)eval
.val
;
710 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
712 /* Add the export RTs for L3VNI/VRF */
713 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
714 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
716 ecommunity_merge(attr
->ecommunity
, ecom
);
718 /* add the router mac extended community */
719 if (!is_zero_mac(&attr
->rmac
)) {
720 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
721 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
723 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
725 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
728 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
732 * Build extended communities for EVPN route.
733 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
734 * and ENCAP extended communities are applicable for all routes.
735 * The default gateway extended community and MAC mobility (sticky) extended
736 * community are added as needed based on passed settings - only for type-2
737 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
738 * added, if present, based on passed settings - only for non-link-local
741 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
744 struct ecommunity ecom_encap
;
745 struct ecommunity ecom_sticky
;
746 struct ecommunity ecom_default_gw
;
747 struct ecommunity ecom_rmac
;
748 struct ecommunity ecom_na
;
749 struct ecommunity_val eval
;
750 struct ecommunity_val eval_sticky
;
751 struct ecommunity_val eval_default_gw
;
752 struct ecommunity_val eval_rmac
;
753 struct ecommunity_val eval_na
;
755 bgp_encap_types tnl_type
;
756 struct listnode
*node
, *nnode
;
757 struct ecommunity
*ecom
;
759 struct list
*vrf_export_rtl
= NULL
;
762 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
763 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
764 encode_encap_extcomm(tnl_type
, &eval
);
766 ecom_encap
.val
= (uint8_t *)eval
.val
;
769 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
771 /* Add the export RTs for L2VNI */
772 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
773 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
775 /* Add the export RTs for L3VNI if told to - caller determines
776 * when this should be done.
779 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
780 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
781 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
783 attr
->ecommunity
= ecommunity_merge(
784 attr
->ecommunity
, ecom
);
788 /* Add MAC mobility (sticky) if needed. */
791 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
792 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
793 ecom_sticky
.size
= 1;
794 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
796 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
799 /* Add RMAC, if told to. */
801 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
802 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
804 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
806 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
809 /* Add default gateway, if needed. */
810 if (attr
->default_gw
) {
811 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
812 encode_default_gw_extcomm(&eval_default_gw
);
813 ecom_default_gw
.size
= 1;
814 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
816 ecommunity_merge(attr
->ecommunity
, &ecom_default_gw
);
819 if (attr
->router_flag
) {
820 memset(&ecom_na
, 0, sizeof(ecom_na
));
821 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
);
823 ecom_na
.val
= (uint8_t *)eval_na
.val
;
824 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
828 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
832 * Add MAC mobility extended community to attribute.
834 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
836 struct ecommunity ecom_tmp
;
837 struct ecommunity_val eval
;
838 uint8_t *ecom_val_ptr
;
845 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
847 /* Find current MM ecommunity */
850 if (attr
->ecommunity
) {
851 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
852 pnt
= attr
->ecommunity
->val
+ (i
* 8);
856 if (type
== ECOMMUNITY_ENCODE_EVPN
858 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
859 ecom_val_ptr
= (uint8_t *)(attr
->ecommunity
->val
866 /* Update the existing MM ecommunity */
868 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
870 /* Add MM to existing */
872 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
874 ecom_tmp
.val
= (uint8_t *)eval
.val
;
876 if (attr
->ecommunity
)
878 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
880 attr
->ecommunity
= ecommunity_dup(&ecom_tmp
);
884 /* Install EVPN route into zebra. */
885 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
886 struct prefix_evpn
*p
,
887 struct in_addr remote_vtep_ip
, uint8_t flags
,
892 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
893 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
896 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 1);
901 /* Uninstall EVPN route from zebra. */
902 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
903 struct prefix_evpn
*p
,
904 struct in_addr remote_vtep_ip
)
908 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
909 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
912 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 0);
918 * Due to MAC mobility, the prior "local" best route has been supplanted
919 * by a "remote" best route. The prior route has to be deleted and withdrawn
922 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
924 struct bgp_path_info
*old_local
)
926 struct bgp_node
*global_rn
;
927 struct bgp_path_info
*pi
;
928 afi_t afi
= AFI_L2VPN
;
929 safi_t safi
= SAFI_EVPN
;
931 /* Locate route node in the global EVPN routing table. Note that
932 * this table is a 2-level tree (RD-level + Prefix-level) similar to
935 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
936 (struct prefix
*)&rn
->p
, &vpn
->prd
);
938 /* Delete route entry in the global EVPN table. */
939 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
941 /* Schedule for processing - withdraws to peers happen from
945 bgp_process(bgp
, global_rn
, afi
, safi
);
946 bgp_unlock_node(global_rn
);
949 /* Delete route entry in the VNI route table, caller to remove. */
950 bgp_path_info_delete(rn
, old_local
);
953 static struct in_addr
*es_vtep_new(struct in_addr vtep
)
957 ip
= XCALLOC(MTYPE_BGP_EVPN_ES_VTEP
, sizeof(struct in_addr
));
961 ip
->s_addr
= vtep
.s_addr
;
965 static void es_vtep_free(struct in_addr
*ip
)
967 XFREE(MTYPE_BGP_EVPN_ES_VTEP
, ip
);
970 /* check if VTEP is already part of the list */
971 static int is_vtep_present_in_list(struct list
*list
,
974 struct listnode
*node
= NULL
;
977 for (ALL_LIST_ELEMENTS_RO(list
, node
, tmp
)) {
978 if (tmp
->s_addr
== vtep
.s_addr
)
985 * Best path for ES route was changed,
986 * update the list of VTEPs for this ES
988 static int evpn_es_install_vtep(struct bgp
*bgp
,
990 struct prefix_evpn
*p
,
991 struct in_addr rvtep
)
993 struct in_addr
*vtep_ip
;
995 if (is_vtep_present_in_list(es
->vtep_list
, rvtep
))
999 vtep_ip
= es_vtep_new(rvtep
);
1001 listnode_add_sort(es
->vtep_list
, vtep_ip
);
1006 * Best path for ES route was changed,
1007 * update the list of VTEPs for this ES
1009 static int evpn_es_uninstall_vtep(struct bgp
*bgp
,
1011 struct prefix_evpn
*p
,
1012 struct in_addr rvtep
)
1014 struct listnode
*node
, *nnode
, *node_to_del
= NULL
;
1015 struct in_addr
*tmp
;
1017 for (ALL_LIST_ELEMENTS(es
->vtep_list
, node
, nnode
, tmp
)) {
1018 if (tmp
->s_addr
== rvtep
.s_addr
) {
1025 list_delete_node(es
->vtep_list
, node_to_del
);
1031 * Calculate the best path for a ES(type-4) route.
1033 static int evpn_es_route_select_install(struct bgp
*bgp
,
1035 struct bgp_node
*rn
)
1038 afi_t afi
= AFI_L2VPN
;
1039 safi_t safi
= SAFI_EVPN
;
1040 struct bgp_path_info
*old_select
; /* old best */
1041 struct bgp_path_info
*new_select
; /* new best */
1042 struct bgp_path_info_pair old_and_new
;
1044 /* Compute the best path. */
1045 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
],
1046 &old_and_new
, afi
, safi
);
1047 old_select
= old_and_new
.old
;
1048 new_select
= old_and_new
.new;
1051 * If the best path hasn't changed - see if something needs to be
1054 if (old_select
&& old_select
== new_select
1055 && old_select
->type
== ZEBRA_ROUTE_BGP
1056 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1057 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1058 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1059 && !bgp
->addpath_tx_used
[afi
][safi
]) {
1060 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1061 ret
= evpn_es_install_vtep(bgp
, es
,
1062 (struct prefix_evpn
*)&rn
->p
,
1063 old_select
->attr
->nexthop
);
1065 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1066 bgp_zebra_clear_route_change_flags(rn
);
1070 /* If the user did a "clear" this flag will be set */
1071 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1074 * bestpath has changed; update relevant fields and install or uninstall
1075 * into the zebra RIB.
1077 if (old_select
|| new_select
)
1078 bgp_bump_version(rn
);
1081 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1083 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1084 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1085 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1088 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1089 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1090 ret
= evpn_es_install_vtep(bgp
, es
,
1091 (struct prefix_evpn
*)&rn
->p
,
1092 new_select
->attr
->nexthop
);
1094 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1095 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1096 ret
= evpn_es_uninstall_vtep(
1097 bgp
, es
, (struct prefix_evpn
*)&rn
->p
,
1098 old_select
->attr
->nexthop
);
1101 /* Clear any route change flags. */
1102 bgp_zebra_clear_route_change_flags(rn
);
1104 /* Reap old select bgp_path_info, if it has been removed */
1105 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1106 bgp_path_info_reap(rn
, old_select
);
1112 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1115 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1116 struct bgp_node
*rn
)
1118 struct bgp_path_info
*old_select
, *new_select
;
1119 struct bgp_path_info_pair old_and_new
;
1120 struct prefix_evpn
*evp
;
1121 afi_t afi
= AFI_L2VPN
;
1122 safi_t safi
= SAFI_EVPN
;
1126 /* Compute the best path. */
1127 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1129 old_select
= old_and_new
.old
;
1130 new_select
= old_and_new
.new;
1132 evp
= (struct prefix_evpn
*)&rn
->p
;
1133 /* If the best path hasn't changed - see if there is still something to
1137 if (old_select
&& old_select
== new_select
1138 && old_select
->type
== ZEBRA_ROUTE_BGP
1139 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1140 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1141 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1142 && !bgp
->addpath_tx_used
[afi
][safi
]) {
1143 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1144 if (old_select
->attr
->sticky
)
1145 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
1146 if (old_select
->attr
->default_gw
)
1147 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
1148 if (is_evpn_prefix_ipaddr_v6(evp
) &&
1149 old_select
->attr
->router_flag
)
1150 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
1152 ret
= evpn_zebra_install(
1153 bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1154 old_select
->attr
->nexthop
, flags
,
1155 mac_mobility_seqnum(old_select
->attr
));
1157 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1158 bgp_zebra_clear_route_change_flags(rn
);
1162 /* If the user did a "clear" this flag will be set */
1163 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1165 /* bestpath has changed; update relevant fields and install or uninstall
1166 * into the zebra RIB.
1168 if (old_select
|| new_select
)
1169 bgp_bump_version(rn
);
1172 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
1174 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
1175 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
1176 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1179 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1180 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1182 if (new_select
->attr
->sticky
)
1183 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
1184 if (new_select
->attr
->default_gw
)
1185 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
1186 if (is_evpn_prefix_ipaddr_v6(evp
) &&
1187 new_select
->attr
->router_flag
)
1188 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
1190 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1191 new_select
->attr
->nexthop
, flags
,
1192 mac_mobility_seqnum(new_select
->attr
));
1193 /* If an old best existed and it was a "local" route, the only
1195 * it would be supplanted is due to MAC mobility procedures. So,
1197 * need to do an implicit delete and withdraw that route from
1200 if (old_select
&& old_select
->peer
== bgp
->peer_self
1201 && old_select
->type
== ZEBRA_ROUTE_BGP
1202 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
1203 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
1205 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1206 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1207 ret
= evpn_zebra_uninstall(bgp
, vpn
,
1208 (struct prefix_evpn
*)&rn
->p
,
1209 old_select
->attr
->nexthop
);
1212 /* Clear any route change flags. */
1213 bgp_zebra_clear_route_change_flags(rn
);
1215 /* Reap old select bgp_path_info, if it has been removed */
1216 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1217 bgp_path_info_reap(rn
, old_select
);
1223 * Return true if the local ri for this rn is of type gateway mac
1225 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
1227 struct bgp_path_info
*tmp_pi
= NULL
;
1228 struct bgp_path_info
*local_pi
= NULL
;
1231 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
) {
1232 if (tmp_pi
->peer
== bgp
->peer_self
1233 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1234 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1241 return local_pi
->attr
->default_gw
;
1246 * Return true if the local ri for this rn has sticky set
1248 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
1250 struct bgp_path_info
*tmp_pi
;
1251 struct bgp_path_info
*local_pi
;
1254 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
) {
1255 if (tmp_pi
->peer
== bgp
->peer_self
1256 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1257 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1264 return local_pi
->attr
->sticky
;
1268 * create or update EVPN type4 route entry.
1269 * This could be in the ES table or the global table.
1270 * TODO: handle remote ES (type4) routes as well
1272 static int update_evpn_type4_route_entry(struct bgp
*bgp
, struct evpnes
*es
,
1273 afi_t afi
, safi_t safi
,
1274 struct bgp_node
*rn
, struct attr
*attr
,
1275 int add
, struct bgp_path_info
**ri
,
1278 char buf
[ESI_STR_LEN
];
1279 char buf1
[INET6_ADDRSTRLEN
];
1280 struct bgp_path_info
*tmp_pi
= NULL
;
1281 struct bgp_path_info
*local_pi
= NULL
; /* local route entry if any */
1282 struct bgp_path_info
*remote_pi
= NULL
; /* remote route entry if any */
1283 struct attr
*attr_new
= NULL
;
1284 struct prefix_evpn
*evp
= NULL
;
1288 evp
= (struct prefix_evpn
*)&rn
->p
;
1290 /* locate the local and remote entries if any */
1291 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
) {
1292 if (tmp_pi
->peer
== bgp
->peer_self
1293 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1294 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1296 if (tmp_pi
->type
== ZEBRA_ROUTE_BGP
1297 && tmp_pi
->sub_type
== BGP_ROUTE_IMPORTED
1298 && CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1302 /* we don't expect to see a remote_pi at this point.
1303 * An ES route has esi + vtep_ip as the key,
1304 * We shouldn't see the same route from any other vtep.
1309 "%u ERROR: local es route for ESI: %s Vtep %s also learnt from remote",
1311 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1312 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1316 if (!local_pi
&& !add
)
1319 /* create or update the entry */
1322 /* Add or update attribute to hash */
1323 attr_new
= bgp_attr_intern(attr
);
1325 /* Create new route with its attribute. */
1326 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1327 bgp
->peer_self
, attr_new
, rn
);
1328 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1330 /* add the newly created path to the route-node */
1331 bgp_path_info_add(rn
, tmp_pi
);
1334 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1335 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1338 /* The attribute has changed.
1339 * Add (or update) attribute to hash. */
1340 attr_new
= bgp_attr_intern(attr
);
1341 bgp_path_info_set_flag(rn
, tmp_pi
,
1342 BGP_PATH_ATTR_CHANGED
);
1344 /* Restore route, if needed. */
1345 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1346 bgp_path_info_restore(rn
, tmp_pi
);
1348 /* Unintern existing, set to new. */
1349 bgp_attr_unintern(&tmp_pi
->attr
);
1350 tmp_pi
->attr
= attr_new
;
1351 tmp_pi
->uptime
= bgp_clock();
1355 /* Return back the route entry. */
1360 /* update evpn es (type-4) route */
1361 static int update_evpn_type4_route(struct bgp
*bgp
,
1363 struct prefix_evpn
*p
)
1366 int route_changed
= 0;
1367 char buf
[ESI_STR_LEN
];
1368 char buf1
[INET6_ADDRSTRLEN
];
1369 afi_t afi
= AFI_L2VPN
;
1370 safi_t safi
= SAFI_EVPN
;
1372 struct attr
*attr_new
= NULL
;
1373 struct bgp_node
*rn
= NULL
;
1374 struct bgp_path_info
*pi
= NULL
;
1376 memset(&attr
, 0, sizeof(struct attr
));
1378 /* Build path-attribute for this route. */
1379 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1380 attr
.nexthop
= es
->originator_ip
.ipaddr_v4
;
1381 attr
.mp_nexthop_global_in
= es
->originator_ip
.ipaddr_v4
;
1382 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1384 /* Set up extended community. */
1385 build_evpn_type4_route_extcomm(es
, &attr
);
1387 /* First, create (or fetch) route node within the ESI. */
1388 /* NOTE: There is no RD here. */
1389 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
1391 /* Create or update route entry. */
1392 ret
= update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, &attr
, 1,
1393 &pi
, &route_changed
);
1395 flog_err(EC_BGP_ES_INVALID
,
1396 "%u ERROR: Failed to updated ES route ESI: %s VTEP %s",
1398 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1399 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1403 attr_new
= pi
->attr
;
1405 /* Perform route selection;
1406 * this is just to set the flags correctly
1407 * as local route in the ES always wins.
1409 evpn_es_route_select_install(bgp
, es
, rn
);
1410 bgp_unlock_node(rn
);
1412 /* If this is a new route or some attribute has changed, export the
1413 * route to the global table. The route will be advertised to peers
1414 * from there. Note that this table is a 2-level tree (RD-level +
1415 * Prefix-level) similar to L3VPN routes.
1417 if (route_changed
) {
1418 struct bgp_path_info
*global_pi
;
1420 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1421 (struct prefix
*)p
, &es
->prd
);
1422 update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
, attr_new
,
1423 1, &global_pi
, &route_changed
);
1425 /* Schedule for processing and unlock node. */
1426 bgp_process(bgp
, rn
, afi
, safi
);
1427 bgp_unlock_node(rn
);
1430 /* Unintern temporary. */
1431 aspath_unintern(&attr
.aspath
);
1435 static int update_evpn_type5_route_entry(struct bgp
*bgp_def
,
1436 struct bgp
*bgp_vrf
, afi_t afi
,
1437 safi_t safi
, struct bgp_node
*rn
,
1438 struct attr
*attr
, int *route_changed
)
1440 struct attr
*attr_new
= NULL
;
1441 struct bgp_path_info
*pi
= NULL
;
1442 mpls_label_t label
= MPLS_INVALID_LABEL
;
1443 struct bgp_path_info
*local_pi
= NULL
;
1444 struct bgp_path_info
*tmp_pi
= NULL
;
1447 /* locate the local route entry if any */
1448 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
) {
1449 if (tmp_pi
->peer
== bgp_def
->peer_self
1450 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1451 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1456 * create a new route entry if one doesnt exist.
1457 * Otherwise see if route attr has changed
1461 /* route has changed as this is the first entry */
1464 /* Add (or update) attribute to hash. */
1465 attr_new
= bgp_attr_intern(attr
);
1467 /* create the route info from attribute */
1468 pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1469 bgp_def
->peer_self
, attr_new
, rn
);
1470 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
1472 /* Type-5 routes advertise the L3-VNI */
1473 bgp_path_info_extra_get(pi
);
1474 vni2label(bgp_vrf
->l3vni
, &label
);
1475 memcpy(&pi
->extra
->label
, &label
, sizeof(label
));
1476 pi
->extra
->num_labels
= 1;
1478 /* add the route entry to route node*/
1479 bgp_path_info_add(rn
, pi
);
1483 if (!attrhash_cmp(tmp_pi
->attr
, attr
)) {
1485 /* attribute changed */
1488 /* The attribute has changed. */
1489 /* Add (or update) attribute to hash. */
1490 attr_new
= bgp_attr_intern(attr
);
1491 bgp_path_info_set_flag(rn
, tmp_pi
,
1492 BGP_PATH_ATTR_CHANGED
);
1494 /* Restore route, if needed. */
1495 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1496 bgp_path_info_restore(rn
, tmp_pi
);
1498 /* Unintern existing, set to new. */
1499 bgp_attr_unintern(&tmp_pi
->attr
);
1500 tmp_pi
->attr
= attr_new
;
1501 tmp_pi
->uptime
= bgp_clock();
1507 /* update evpn type-5 route entry */
1508 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1509 struct attr
*src_attr
)
1511 afi_t afi
= AFI_L2VPN
;
1512 safi_t safi
= SAFI_EVPN
;
1514 struct bgp_node
*rn
= NULL
;
1515 struct bgp
*bgp_def
= NULL
;
1516 int route_changed
= 0;
1518 bgp_def
= bgp_get_default();
1522 /* Build path attribute for this route - use the source attr, if
1523 * present, else treat as locally originated.
1526 bgp_attr_dup(&attr
, src_attr
);
1528 memset(&attr
, 0, sizeof(struct attr
));
1529 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1531 /* Set nexthop to ourselves and fill in the Router MAC. */
1532 attr
.nexthop
= bgp_vrf
->originator_ip
;
1533 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1534 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1535 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
1537 /* Setup RT and encap extended community */
1538 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1540 /* get the route node in global table */
1541 rn
= bgp_afi_node_get(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1542 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1545 /* create or update the route entry within the route node */
1546 update_evpn_type5_route_entry(bgp_def
, bgp_vrf
, afi
, safi
, rn
, &attr
,
1549 /* schedule for processing and unlock node */
1550 if (route_changed
) {
1551 bgp_process(bgp_def
, rn
, afi
, safi
);
1552 bgp_unlock_node(rn
);
1555 /* uninten temporary */
1557 aspath_unintern(&attr
.aspath
);
1562 * Create or update EVPN route entry. This could be in the VNI route table
1563 * or the global route table.
1565 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1566 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1567 struct attr
*attr
, int add
,
1568 struct bgp_path_info
**pi
, uint8_t flags
,
1571 struct bgp_path_info
*tmp_pi
;
1572 struct bgp_path_info
*local_pi
;
1573 struct attr
*attr_new
;
1574 mpls_label_t label
[BGP_MAX_LABELS
];
1575 uint32_t num_labels
= 1;
1576 int route_change
= 1;
1578 struct prefix_evpn
*evp
;
1581 evp
= (struct prefix_evpn
*)&rn
->p
;
1582 memset(&label
, 0, sizeof(label
));
1584 /* See if this is an update of an existing route, or a new add. */
1586 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
) {
1587 if (tmp_pi
->peer
== bgp
->peer_self
1588 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1589 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1593 /* If route doesn't exist already, create a new one, if told to.
1594 * Otherwise act based on whether the attributes of the route have
1597 if (!local_pi
&& !add
)
1600 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1601 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1602 add_mac_mobility_to_attr(seq
, attr
);
1605 /* Add (or update) attribute to hash. */
1606 attr_new
= bgp_attr_intern(attr
);
1608 /* Extract MAC mobility sequence number, if any. */
1609 attr_new
->mm_seqnum
=
1610 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1611 attr_new
->sticky
= sticky
;
1613 /* Create new route with its attribute. */
1614 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1615 bgp
->peer_self
, attr_new
, rn
);
1616 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1617 bgp_path_info_extra_get(tmp_pi
);
1619 /* The VNI goes into the 'label' field of the route */
1620 vni2label(vpn
->vni
, &label
[0]);
1622 /* Type-2 routes may carry a second VNI - the L3-VNI.
1623 * Only attach second label if we are advertising two labels for
1626 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1627 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1630 l3vni
= bgpevpn_get_l3vni(vpn
);
1632 vni2label(l3vni
, &label
[1]);
1637 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1638 tmp_pi
->extra
->num_labels
= num_labels
;
1639 bgp_path_info_add(rn
, tmp_pi
);
1642 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1643 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1647 * The attributes have changed, type-2 routes needs to
1648 * be advertised with right labels.
1650 vni2label(vpn
->vni
, &label
[0]);
1651 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1652 && CHECK_FLAG(vpn
->flags
,
1653 VNI_FLAG_USE_TWO_LABELS
)) {
1656 l3vni
= bgpevpn_get_l3vni(vpn
);
1658 vni2label(l3vni
, &label
[1]);
1662 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1663 tmp_pi
->extra
->num_labels
= num_labels
;
1665 /* The attribute has changed. */
1666 /* Add (or update) attribute to hash. */
1667 attr_new
= bgp_attr_intern(attr
);
1668 bgp_path_info_set_flag(rn
, tmp_pi
,
1669 BGP_PATH_ATTR_CHANGED
);
1671 /* Extract MAC mobility sequence number, if any. */
1672 attr_new
->mm_seqnum
=
1673 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1674 attr_new
->sticky
= sticky
;
1676 /* Restore route, if needed. */
1677 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1678 bgp_path_info_restore(rn
, tmp_pi
);
1680 /* Unintern existing, set to new. */
1681 bgp_attr_unintern(&tmp_pi
->attr
);
1682 tmp_pi
->attr
= attr_new
;
1683 tmp_pi
->uptime
= bgp_clock();
1687 /* Return back the route entry. */
1689 return route_change
;
1693 * Create or update EVPN route (of type based on prefix) for specified VNI
1694 * and schedule for processing.
1696 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1697 struct prefix_evpn
*p
, uint8_t flags
,
1700 struct bgp_node
*rn
;
1702 struct attr
*attr_new
;
1703 int add_l3_ecomm
= 0;
1704 struct bgp_path_info
*pi
;
1705 afi_t afi
= AFI_L2VPN
;
1706 safi_t safi
= SAFI_EVPN
;
1709 memset(&attr
, 0, sizeof(struct attr
));
1711 /* Build path-attribute for this route. */
1712 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1713 attr
.nexthop
= vpn
->originator_ip
;
1714 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1715 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1716 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1717 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1718 attr
.router_flag
= CHECK_FLAG(flags
,
1719 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1721 /* PMSI is only needed for type-3 routes */
1722 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
)
1723 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1725 /* router mac is only needed for type-2 routes here. */
1726 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1727 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1728 vni2label(vpn
->vni
, &(attr
.label
));
1730 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1731 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1734 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1735 (is_evpn_prefix_ipaddr_v4(p
) ||
1736 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1737 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1738 bgpevpn_get_l3vni(vpn
))
1741 /* Set up extended community. */
1742 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1744 /* First, create (or fetch) route node within the VNI. */
1745 /* NOTE: There is no RD here. */
1746 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1748 /* Create or update route entry. */
1749 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1750 1, &pi
, flags
, seq
);
1752 attr_new
= pi
->attr
;
1754 /* Perform route selection; this is just to set the flags correctly
1755 * as local route in the VNI always wins.
1757 evpn_route_select_install(bgp
, vpn
, rn
);
1758 bgp_unlock_node(rn
);
1760 /* If this is a new route or some attribute has changed, export the
1761 * route to the global table. The route will be advertised to peers
1762 * from there. Note that this table is a 2-level tree (RD-level +
1763 * Prefix-level) similar to L3VPN routes.
1766 struct bgp_path_info
*global_pi
;
1768 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1769 (struct prefix
*)p
, &vpn
->prd
);
1770 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1,
1771 &global_pi
, flags
, seq
);
1773 /* Schedule for processing and unlock node. */
1774 bgp_process(bgp
, rn
, afi
, safi
);
1775 bgp_unlock_node(rn
);
1778 /* Unintern temporary. */
1779 aspath_unintern(&attr
.aspath
);
1785 * Delete EVPN route entry.
1786 * The entry can be in ESI/VNI table or the global table.
1788 static void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1789 struct bgp_node
*rn
,
1790 struct bgp_path_info
**pi
)
1792 struct bgp_path_info
*tmp_pi
;
1796 /* Now, find matching route. */
1797 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
)
1798 if (tmp_pi
->peer
== bgp
->peer_self
1799 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1800 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1805 /* Mark route for delete. */
1807 bgp_path_info_delete(rn
, tmp_pi
);
1812 /* Delete EVPN ES (type-4) route */
1813 static int delete_evpn_type4_route(struct bgp
*bgp
,
1815 struct prefix_evpn
*p
)
1817 afi_t afi
= AFI_L2VPN
;
1818 safi_t safi
= SAFI_EVPN
;
1819 struct bgp_path_info
*pi
;
1820 struct bgp_node
*rn
= NULL
; /* rn in esi table */
1821 struct bgp_node
*global_rn
= NULL
; /* rn in global table */
1823 /* First, locate the route node within the ESI.
1824 * If it doesn't exist, ther is nothing to do.
1825 * Note: there is no RD here.
1827 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
1831 /* Next, locate route node in the global EVPN routing table.
1832 * Note that this table is a 2-level tree (RD-level + Prefix-level)
1834 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1835 (struct prefix
*)p
, &es
->prd
);
1838 /* Delete route entry in the global EVPN table. */
1839 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1841 /* Schedule for processing - withdraws to peers happen from
1845 bgp_process(bgp
, global_rn
, afi
, safi
);
1846 bgp_unlock_node(global_rn
);
1850 * Delete route entry in the ESI route table.
1851 * This can just be removed.
1853 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
1855 bgp_path_info_reap(rn
, pi
);
1856 bgp_unlock_node(rn
);
1860 /* Delete EVPN type5 route */
1861 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1863 afi_t afi
= AFI_L2VPN
;
1864 safi_t safi
= SAFI_EVPN
;
1865 struct bgp_node
*rn
= NULL
;
1866 struct bgp_path_info
*pi
= NULL
;
1867 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
1869 bgp_def
= bgp_get_default();
1873 /* locate the global route entry for this type-5 prefix */
1874 rn
= bgp_afi_node_lookup(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1875 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1879 delete_evpn_route_entry(bgp_def
, afi
, safi
, rn
, &pi
);
1881 bgp_process(bgp_def
, rn
, afi
, safi
);
1882 bgp_unlock_node(rn
);
1887 * Delete EVPN route (of type based on prefix) for specified VNI and
1888 * schedule for processing.
1890 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1891 struct prefix_evpn
*p
)
1893 struct bgp_node
*rn
, *global_rn
;
1894 struct bgp_path_info
*pi
;
1895 afi_t afi
= AFI_L2VPN
;
1896 safi_t safi
= SAFI_EVPN
;
1898 /* First, locate the route node within the VNI. If it doesn't exist,
1900 * is nothing further to do.
1902 /* NOTE: There is no RD here. */
1903 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1907 /* Next, locate route node in the global EVPN routing table. Note that
1908 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1911 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1912 (struct prefix
*)p
, &vpn
->prd
);
1914 /* Delete route entry in the global EVPN table. */
1915 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
1917 /* Schedule for processing - withdraws to peers happen from
1921 bgp_process(bgp
, global_rn
, afi
, safi
);
1922 bgp_unlock_node(global_rn
);
1925 /* Delete route entry in the VNI route table. This can just be removed.
1927 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
1929 bgp_path_info_reap(rn
, pi
);
1930 bgp_unlock_node(rn
);
1936 * Update all type-2 (MACIP) local routes for this VNI - these should also
1937 * be scheduled for advertise to peers.
1939 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1943 struct bgp_node
*rn
;
1944 struct bgp_path_info
*pi
, *tmp_pi
;
1946 struct attr
*attr_new
;
1948 int add_l3_ecomm
= 0;
1953 /* Walk this VNI's route table and update local type-2 routes. For any
1954 * routes updated, update corresponding entry in the global table too.
1956 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1957 rn
= bgp_route_next(rn
)) {
1958 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1959 struct bgp_node
*rd_rn
;
1960 struct bgp_path_info
*global_pi
;
1962 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1965 /* Identify local route. */
1966 for (tmp_pi
= rn
->info
; tmp_pi
; tmp_pi
= tmp_pi
->next
) {
1967 if (tmp_pi
->peer
== bgp
->peer_self
1968 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1969 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1977 * Build attribute per local route as the MAC mobility and
1978 * some other values could differ for different routes. The
1979 * attributes will be shared in the hash table.
1981 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1982 attr
.nexthop
= vpn
->originator_ip
;
1983 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1984 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1985 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1987 if (evpn_route_is_sticky(bgp
, rn
))
1989 else if (evpn_route_is_def_gw(bgp
, rn
)) {
1990 attr
.default_gw
= 1;
1991 if (is_evpn_prefix_ipaddr_v6(evp
))
1992 attr
.router_flag
= 1;
1995 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
1996 * using L3 VNI for type-2 routes also.
1998 if ((is_evpn_prefix_ipaddr_v4(evp
) ||
1999 !IN6_IS_ADDR_LINKLOCAL(
2000 &evp
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
2001 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
2002 bgpevpn_get_l3vni(vpn
))
2005 /* Set up extended community. */
2006 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2008 seq
= mac_mobility_seqnum(tmp_pi
->attr
);
2010 /* Update the route entry. */
2011 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
, 0, &pi
,
2014 /* Perform route selection; this is just to set the flags
2015 * correctly as local route in the VNI always wins.
2017 evpn_route_select_install(bgp
, vpn
, rn
);
2019 attr_new
= pi
->attr
;
2021 /* Update route in global routing table. */
2022 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2023 (struct prefix
*)evp
, &vpn
->prd
);
2025 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
2027 mac_mobility_seqnum(attr_new
));
2029 /* Schedule for processing and unlock node. */
2030 bgp_process(bgp
, rd_rn
, afi
, safi
);
2031 bgp_unlock_node(rd_rn
);
2033 /* Unintern temporary. */
2034 aspath_unintern(&attr
.aspath
);
2042 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2043 * global routing table. These are also scheduled for withdraw from peers.
2045 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2049 struct bgp_node
*rdrn
, *rn
;
2050 struct bgp_table
*table
;
2051 struct bgp_path_info
*pi
;
2056 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
2057 if (rdrn
&& rdrn
->info
) {
2058 table
= (struct bgp_table
*)rdrn
->info
;
2059 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2060 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2062 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2065 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2067 bgp_process(bgp
, rn
, afi
, safi
);
2071 /* Unlock RD node. */
2073 bgp_unlock_node(rdrn
);
2079 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2080 * table as well as the per-VNI route table.
2082 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2086 struct bgp_node
*rn
;
2087 struct bgp_path_info
*pi
;
2092 /* First, walk the global route table for this VNI's type-2 local
2094 * EVPN routes are a 2-level table, first get the RD table.
2096 delete_global_type2_routes(bgp
, vpn
);
2098 /* Next, walk this VNI's route table and delete local type-2 routes. */
2099 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2100 rn
= bgp_route_next(rn
)) {
2101 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2103 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2106 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
2108 /* Route entry in local table gets deleted immediately. */
2110 bgp_path_info_reap(rn
, pi
);
2117 * Delete all routes in per ES route-table
2119 static int delete_all_es_routes(struct bgp
*bgp
, struct evpnes
*es
)
2121 struct bgp_node
*rn
;
2122 struct bgp_path_info
*pi
, *nextpi
;
2124 /* Walk this ES's route table and delete all routes. */
2125 for (rn
= bgp_table_top(es
->route_table
); rn
;
2126 rn
= bgp_route_next(rn
)) {
2127 for (pi
= rn
->info
; (pi
!= NULL
) && (nextpi
= pi
->next
, 1);
2129 bgp_path_info_delete(rn
, pi
);
2130 bgp_path_info_reap(rn
, pi
);
2138 * Delete all routes in the per-VNI route table.
2140 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2142 struct bgp_node
*rn
;
2143 struct bgp_path_info
*pi
, *nextpi
;
2145 /* Walk this VNI's route table and delete all routes. */
2146 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2147 rn
= bgp_route_next(rn
)) {
2148 for (pi
= rn
->info
; (pi
!= NULL
) && (nextpi
= pi
->next
, 1);
2150 bgp_path_info_delete(rn
, pi
);
2151 bgp_path_info_reap(rn
, pi
);
2159 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2160 * export RT getting modified or change to tunnel IP. Note that these
2161 * situations need the route in the per-VNI table as well as the global
2162 * table to be updated (as attributes change).
2164 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2167 struct prefix_evpn p
;
2169 /* Update and advertise the type-3 route (only one) followed by the
2170 * locally learnt type-2 routes (MACIP) - for this VNI.
2172 * RT-3 only if doing head-end replication
2174 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
2175 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2176 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0);
2181 return update_all_type2_routes(bgp
, vpn
);
2184 /* Delete (and withdraw) local routes for specified ES from global and ES table.
2185 * Also remove all other routes from the per ES table.
2186 * Invoked when ES is deleted.
2188 static int delete_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
2191 char buf
[ESI_STR_LEN
];
2192 struct prefix_evpn p
;
2194 /* Delete and withdraw locally learnt ES route */
2195 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
.ipaddr_v4
);
2196 ret
= delete_evpn_type4_route(bgp
, es
, &p
);
2198 flog_err(EC_BGP_EVPN_ROUTE_DELETE
,
2199 "%u failed to delete type-4 route for ESI %s",
2200 bgp
->vrf_id
, esi_to_str(&es
->esi
, buf
, sizeof(buf
)));
2203 /* Delete all routes from per ES table */
2204 return delete_all_es_routes(bgp
, es
);
2208 * Delete (and withdraw) local routes for specified VNI from the global
2209 * table and per-VNI table. After this, remove all other routes from
2210 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2211 * (advertise-all-vni) being disabled.
2213 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2216 struct prefix_evpn p
;
2218 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2219 * followed by type-3 routes (only one) - for this VNI.
2221 ret
= delete_all_type2_routes(bgp
, vpn
);
2225 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2226 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2230 /* Delete all routes from the per-VNI table. */
2231 return delete_all_vni_routes(bgp
, vpn
);
2235 * There is a tunnel endpoint IP address change for this VNI, delete
2236 * prior type-3 route (if needed) and update.
2237 * Note: Route re-advertisement happens elsewhere after other processing
2240 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2241 struct in_addr originator_ip
)
2243 struct prefix_evpn p
;
2245 /* If VNI is not live, we only need to update the originator ip */
2246 if (!is_vni_live(vpn
)) {
2247 vpn
->originator_ip
= originator_ip
;
2251 /* Update the tunnel-ip hash */
2252 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2253 bgp_tip_add(bgp
, &originator_ip
);
2255 /* filter routes as martian nexthop db has changed */
2256 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2258 /* Need to withdraw type-3 route as the originator IP is part
2261 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2262 delete_evpn_route(bgp
, vpn
, &p
);
2264 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2265 vpn
->originator_ip
= originator_ip
;
2269 /* Install EVPN route entry in ES */
2270 static int install_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2271 struct prefix_evpn
*p
,
2272 struct bgp_path_info
*parent_pi
)
2275 struct bgp_node
*rn
= NULL
;
2276 struct bgp_path_info
*pi
= NULL
;
2277 struct attr
*attr_new
= NULL
;
2279 /* Create (or fetch) route within the VNI.
2280 * NOTE: There is no RD here.
2282 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
2284 /* Check if route entry is already present. */
2285 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
2287 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2291 /* Add (or update) attribute to hash. */
2292 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2294 /* Create new route with its attribute. */
2295 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2296 parent_pi
->peer
, attr_new
, rn
);
2297 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2298 bgp_path_info_extra_get(pi
);
2299 pi
->extra
->parent
= parent_pi
;
2300 bgp_path_info_add(rn
, pi
);
2302 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2303 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2304 bgp_unlock_node(rn
);
2307 /* The attribute has changed. */
2308 /* Add (or update) attribute to hash. */
2309 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2311 /* Restore route, if needed. */
2312 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2313 bgp_path_info_restore(rn
, pi
);
2315 /* Mark if nexthop has changed. */
2316 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2317 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2319 /* Unintern existing, set to new. */
2320 bgp_attr_unintern(&pi
->attr
);
2321 pi
->attr
= attr_new
;
2322 pi
->uptime
= bgp_clock();
2325 /* Perform route selection and update zebra, if required. */
2326 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2331 * Install route entry into the VRF routing table and invoke route selection.
2333 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2334 struct prefix_evpn
*evp
,
2335 struct bgp_path_info
*parent_pi
)
2337 struct bgp_node
*rn
;
2338 struct bgp_path_info
*pi
;
2340 struct attr
*attr_new
;
2343 struct prefix
*pp
= &p
;
2346 char buf
[PREFIX_STRLEN
];
2347 char buf1
[PREFIX_STRLEN
];
2349 memset(pp
, 0, sizeof(struct prefix
));
2350 ip_prefix_from_evpn_prefix(evp
, pp
);
2352 if (bgp_debug_zebra(NULL
)) {
2354 "installing evpn prefix %s as ip prefix %s in vrf %s",
2355 prefix2str(evp
, buf
, sizeof(buf
)),
2356 prefix2str(pp
, buf1
, sizeof(buf
)),
2357 vrf_id_to_name(bgp_vrf
->vrf_id
));
2360 /* Create (or fetch) route within the VRF. */
2361 /* NOTE: There is no RD here. */
2362 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2364 safi
= SAFI_UNICAST
;
2365 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2366 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2368 safi
= SAFI_UNICAST
;
2369 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2373 /* EVPN routes currently only support a IPv4 next hop which corresponds
2374 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2375 * or prefix route, we have to convert the next hop to an IPv4-mapped
2376 * address for the rest of the code to flow through. In the case of IPv4,
2377 * make sure to set the flag for next hop attribute.
2379 bgp_attr_dup(&attr
, parent_pi
->attr
);
2381 evpn_convert_nexthop_to_ipv6(&attr
);
2383 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2385 /* Check if route entry is already present. */
2386 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
2388 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2392 /* Add (or update) attribute to hash. */
2393 attr_new
= bgp_attr_intern(&attr
);
2395 /* Create new route with its attribute. */
2396 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2397 parent_pi
->peer
, attr_new
, rn
);
2398 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2399 bgp_path_info_extra_get(pi
);
2400 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2401 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2402 if (parent_pi
->extra
) {
2403 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2404 sizeof(pi
->extra
->label
));
2405 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2407 bgp_path_info_add(rn
, pi
);
2409 if (attrhash_cmp(pi
->attr
, &attr
)
2410 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2411 bgp_unlock_node(rn
);
2414 /* The attribute has changed. */
2415 /* Add (or update) attribute to hash. */
2416 attr_new
= bgp_attr_intern(&attr
);
2418 /* Restore route, if needed. */
2419 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2420 bgp_path_info_restore(rn
, pi
);
2422 /* Mark if nexthop has changed. */
2424 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2426 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2427 &attr_new
->mp_nexthop_global
)))
2428 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2430 /* Unintern existing, set to new. */
2431 bgp_attr_unintern(&pi
->attr
);
2432 pi
->attr
= attr_new
;
2433 pi
->uptime
= bgp_clock();
2436 bgp_aggregate_increment(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2438 /* Perform route selection and update zebra, if required. */
2439 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2445 * Install route entry into the VNI routing table and invoke route selection.
2447 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2448 struct prefix_evpn
*p
,
2449 struct bgp_path_info
*parent_pi
)
2451 struct bgp_node
*rn
;
2452 struct bgp_path_info
*pi
;
2453 struct attr
*attr_new
;
2456 /* Create (or fetch) route within the VNI. */
2457 /* NOTE: There is no RD here. */
2458 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2460 /* Check if route entry is already present. */
2461 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
2463 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2467 /* Add (or update) attribute to hash. */
2468 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2470 /* Create new route with its attribute. */
2471 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
2472 parent_pi
->peer
, attr_new
, rn
);
2473 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2474 bgp_path_info_extra_get(pi
);
2475 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2476 bgp_lock_node((struct bgp_node
*)parent_pi
->net
);
2477 if (parent_pi
->extra
) {
2478 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2479 sizeof(pi
->extra
->label
));
2480 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2482 bgp_path_info_add(rn
, pi
);
2484 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2485 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2486 bgp_unlock_node(rn
);
2489 /* The attribute has changed. */
2490 /* Add (or update) attribute to hash. */
2491 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2493 /* Restore route, if needed. */
2494 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2495 bgp_path_info_restore(rn
, pi
);
2497 /* Mark if nexthop has changed. */
2498 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2499 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2501 /* Unintern existing, set to new. */
2502 bgp_attr_unintern(&pi
->attr
);
2503 pi
->attr
= attr_new
;
2504 pi
->uptime
= bgp_clock();
2507 /* Perform route selection and update zebra, if required. */
2508 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2513 /* Uninstall EVPN route entry from ES route table */
2514 static int uninstall_evpn_route_entry_in_es(struct bgp
*bgp
, struct evpnes
*es
,
2515 struct prefix_evpn
*p
,
2516 struct bgp_path_info
*parent_pi
)
2519 struct bgp_node
*rn
;
2520 struct bgp_path_info
*pi
;
2522 if (!es
->route_table
)
2525 /* Locate route within the ESI.
2526 * NOTE: There is no RD here.
2528 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2532 /* Find matching route entry. */
2533 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
2535 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2541 /* Mark entry for deletion */
2542 bgp_path_info_delete(rn
, pi
);
2544 /* Perform route selection and update zebra, if required. */
2545 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2547 /* Unlock route node. */
2548 bgp_unlock_node(rn
);
2554 * Uninstall route entry from the VRF routing table and send message
2555 * to zebra, if appropriate.
2557 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2558 struct prefix_evpn
*evp
,
2559 struct bgp_path_info
*parent_pi
)
2561 struct bgp_node
*rn
;
2562 struct bgp_path_info
*pi
;
2565 struct prefix
*pp
= &p
;
2568 char buf
[PREFIX_STRLEN
];
2569 char buf1
[PREFIX_STRLEN
];
2571 memset(pp
, 0, sizeof(struct prefix
));
2572 ip_prefix_from_evpn_prefix(evp
, pp
);
2574 if (bgp_debug_zebra(NULL
)) {
2576 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2577 prefix2str(evp
, buf
, sizeof(buf
)),
2578 prefix2str(pp
, buf1
, sizeof(buf
)),
2579 vrf_id_to_name(bgp_vrf
->vrf_id
));
2582 /* Locate route within the VRF. */
2583 /* NOTE: There is no RD here. */
2584 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2586 safi
= SAFI_UNICAST
;
2587 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2590 safi
= SAFI_UNICAST
;
2591 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2597 /* Find matching route entry. */
2598 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
2600 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2606 bgp_aggregate_decrement(bgp_vrf
, &rn
->p
, pi
, afi
, safi
);
2608 /* Mark entry for deletion */
2609 bgp_path_info_delete(rn
, pi
);
2611 /* Perform route selection and update zebra, if required. */
2612 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2614 /* Unlock route node. */
2615 bgp_unlock_node(rn
);
2621 * Uninstall route entry from the VNI routing table and send message
2622 * to zebra, if appropriate.
2624 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2625 struct prefix_evpn
*p
,
2626 struct bgp_path_info
*parent_pi
)
2628 struct bgp_node
*rn
;
2629 struct bgp_path_info
*pi
;
2632 /* Locate route within the VNI. */
2633 /* NOTE: There is no RD here. */
2634 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2638 /* Find matching route entry. */
2639 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
2641 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2647 /* Mark entry for deletion */
2648 bgp_path_info_delete(rn
, pi
);
2650 /* Perform route selection and update zebra, if required. */
2651 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2653 /* Unlock route node. */
2654 bgp_unlock_node(rn
);
2660 * Given a prefix, see if it belongs to ES.
2662 static int is_prefix_matching_for_es(struct prefix_evpn
*p
,
2665 /* if not an ES route return false */
2666 if (p
->prefix
.route_type
!= BGP_EVPN_ES_ROUTE
)
2669 if (memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
)) == 0)
2676 * Given a route entry and a VRF, see if this route entry should be
2677 * imported into the VRF i.e., RTs match.
2679 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2680 struct bgp_path_info
*pi
)
2682 struct attr
*attr
= pi
->attr
;
2683 struct ecommunity
*ecom
;
2687 /* Route should have valid RT to be even considered. */
2688 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2691 ecom
= attr
->ecommunity
;
2692 if (!ecom
|| !ecom
->size
)
2695 /* For each extended community RT, see if it matches this VNI. If any RT
2696 * matches, we're done.
2698 for (i
= 0; i
< ecom
->size
; i
++) {
2700 uint8_t type
, sub_type
;
2701 struct ecommunity_val
*eval
;
2702 struct ecommunity_val eval_tmp
;
2703 struct vrf_irt_node
*irt
;
2705 /* Only deal with RTs */
2706 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2707 eval
= (struct ecommunity_val
*)(ecom
->val
2708 + (i
* ECOMMUNITY_SIZE
));
2711 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2714 /* See if this RT matches specified VNIs import RTs */
2715 irt
= lookup_vrf_import_rt(eval
);
2717 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2720 /* Also check for non-exact match. In this, we mask out the AS
2722 * only check on the local-admin sub-field. This is to
2724 * VNI as the RT for EBGP peering too.
2727 if (type
== ECOMMUNITY_ENCODE_AS
2728 || type
== ECOMMUNITY_ENCODE_AS4
2729 || type
== ECOMMUNITY_ENCODE_IP
) {
2730 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2731 mask_ecom_global_admin(&eval_tmp
, eval
);
2732 irt
= lookup_vrf_import_rt(&eval_tmp
);
2735 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2743 * Given a route entry and a VNI, see if this route entry should be
2744 * imported into the VNI i.e., RTs match.
2746 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2747 struct bgp_path_info
*pi
)
2749 struct attr
*attr
= pi
->attr
;
2750 struct ecommunity
*ecom
;
2754 /* Route should have valid RT to be even considered. */
2755 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2758 ecom
= attr
->ecommunity
;
2759 if (!ecom
|| !ecom
->size
)
2762 /* For each extended community RT, see if it matches this VNI. If any RT
2763 * matches, we're done.
2765 for (i
= 0; i
< ecom
->size
; i
++) {
2767 uint8_t type
, sub_type
;
2768 struct ecommunity_val
*eval
;
2769 struct ecommunity_val eval_tmp
;
2770 struct irt_node
*irt
;
2772 /* Only deal with RTs */
2773 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2774 eval
= (struct ecommunity_val
*)(ecom
->val
2775 + (i
* ECOMMUNITY_SIZE
));
2778 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2781 /* See if this RT matches specified VNIs import RTs */
2782 irt
= lookup_import_rt(bgp
, eval
);
2784 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2787 /* Also check for non-exact match. In this, we mask out the AS
2789 * only check on the local-admin sub-field. This is to
2791 * VNI as the RT for EBGP peering too.
2794 if (type
== ECOMMUNITY_ENCODE_AS
2795 || type
== ECOMMUNITY_ENCODE_AS4
2796 || type
== ECOMMUNITY_ENCODE_IP
) {
2797 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2798 mask_ecom_global_admin(&eval_tmp
, eval
);
2799 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2802 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2809 static int install_uninstall_routes_for_es(struct bgp
*bgp
,
2816 char buf
[PREFIX_STRLEN
];
2817 char buf1
[ESI_STR_LEN
];
2818 struct bgp_node
*rd_rn
, *rn
;
2819 struct bgp_table
*table
;
2820 struct bgp_path_info
*pi
;
2826 * Walk entire global routing table and evaluate routes which could be
2827 * imported into this VRF. Note that we need to loop through all global
2828 * routes to determine which route matches the import rt on vrf
2830 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2831 rd_rn
= bgp_route_next(rd_rn
)) {
2832 table
= (struct bgp_table
*)(rd_rn
->info
);
2836 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2837 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2839 for (pi
= rn
->info
; pi
; pi
= pi
->next
) {
2841 * Consider "valid" remote routes applicable for
2844 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
2845 && pi
->type
== ZEBRA_ROUTE_BGP
2846 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
2849 if (!is_prefix_matching_for_es(evp
, es
))
2853 ret
= install_evpn_route_entry_in_es(
2856 ret
= uninstall_evpn_route_entry_in_es(
2862 "Failed to %s EVPN %s route in ESI %s",
2865 prefix2str(evp
, buf
,
2867 esi_to_str(&es
->esi
, buf1
,
2878 * Install or uninstall mac-ip routes are appropriate for this
2881 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
2885 struct bgp_node
*rd_rn
, *rn
;
2886 struct bgp_table
*table
;
2887 struct bgp_path_info
*pi
;
2889 char buf
[PREFIX_STRLEN
];
2890 struct bgp
*bgp_def
= NULL
;
2894 bgp_def
= bgp_get_default();
2898 /* Walk entire global routing table and evaluate routes which could be
2899 * imported into this VRF. Note that we need to loop through all global
2900 * routes to determine which route matches the import rt on vrf
2902 for (rd_rn
= bgp_table_top(bgp_def
->rib
[afi
][safi
]); rd_rn
;
2903 rd_rn
= bgp_route_next(rd_rn
)) {
2904 table
= (struct bgp_table
*)(rd_rn
->info
);
2908 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2909 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2911 /* if not mac-ip route skip this route */
2912 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2913 || evp
->prefix
.route_type
2914 == BGP_EVPN_IP_PREFIX_ROUTE
))
2917 /* if not a mac+ip route skip this route */
2918 if (!(is_evpn_prefix_ipaddr_v4(evp
)
2919 || is_evpn_prefix_ipaddr_v6(evp
)))
2922 for (pi
= rn
->info
; pi
; pi
= pi
->next
) {
2923 /* Consider "valid" remote routes applicable for
2926 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
2927 && pi
->type
== ZEBRA_ROUTE_BGP
2928 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
2931 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
2933 ret
= install_evpn_route_entry_in_vrf(
2936 ret
= uninstall_evpn_route_entry_in_vrf(
2942 "Failed to %s EVPN %s route in VRF %s",
2945 prefix2str(evp
, buf
,
2960 * Install or uninstall routes of specified type that are appropriate for this
2963 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
2964 struct bgpevpn
*vpn
,
2965 bgp_evpn_route_type rtype
,
2970 struct bgp_node
*rd_rn
, *rn
;
2971 struct bgp_table
*table
;
2972 struct bgp_path_info
*pi
;
2978 /* Walk entire global routing table and evaluate routes which could be
2979 * imported into this VPN. Note that we cannot just look at the routes
2981 * the VNI's RD - remote routes applicable for this VNI could have any
2984 /* EVPN routes are a 2-level table. */
2985 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2986 rd_rn
= bgp_route_next(rd_rn
)) {
2987 table
= (struct bgp_table
*)(rd_rn
->info
);
2991 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2992 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2994 if (evp
->prefix
.route_type
!= rtype
)
2997 for (pi
= rn
->info
; pi
; pi
= pi
->next
) {
2998 /* Consider "valid" remote routes applicable for
3000 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3001 && pi
->type
== ZEBRA_ROUTE_BGP
3002 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3005 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3007 ret
= install_evpn_route_entry(
3010 ret
= uninstall_evpn_route_entry(
3016 "%u: Failed to %s EVPN %s route in VNI %u",
3020 rtype
== BGP_EVPN_MAC_IP_ROUTE
3034 /* Install any existing remote ES routes applicable for this ES into its routing
3035 * table. This is invoked when ES comes up.
3037 static int install_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
3039 return install_uninstall_routes_for_es(bgp
, es
, 1);
3043 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3044 * is invoked upon l3vni-add or l3vni import rt change
3046 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3048 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3053 * Install any existing remote routes applicable for this VNI into its
3054 * routing table. This is invoked when a VNI becomes "live" or its Import
3057 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3061 /* Install type-3 routes followed by type-2 routes - the ones applicable
3064 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3069 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3073 /* uninstall routes from l3vni vrf. */
3074 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3076 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3081 * Uninstall any existing remote routes for this VNI. One scenario in which
3082 * this is invoked is upon an import RT change.
3084 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3088 /* Uninstall type-2 routes followed by type-3 routes - the ones
3092 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3097 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3101 /* Install or unistall route in ES */
3102 static int install_uninstall_route_in_es(struct bgp
*bgp
, struct evpnes
*es
,
3103 afi_t afi
, safi_t safi
,
3104 struct prefix_evpn
*evp
,
3105 struct bgp_path_info
*pi
, int install
)
3108 char buf
[ESI_STR_LEN
];
3111 ret
= install_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3113 ret
= uninstall_evpn_route_entry_in_es(bgp
, es
, evp
, pi
);
3118 "%u: Failed to %s EVPN %s route in ESI %s", bgp
->vrf_id
,
3119 install
? "install" : "uninstall", "ES",
3120 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)));
3127 * Install or uninstall route in matching VRFs (list).
3129 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3130 safi_t safi
, struct prefix_evpn
*evp
,
3131 struct bgp_path_info
*pi
,
3132 struct list
*vrfs
, int install
)
3134 char buf
[PREFIX2STR_BUFFER
];
3135 struct bgp
*bgp_vrf
;
3136 struct listnode
*node
, *nnode
;
3138 /* Only type-2/type-5 routes go into a VRF */
3139 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3140 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3143 /* if it is type-2 route and not a mac+ip route skip this route */
3144 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3145 && !(is_evpn_prefix_ipaddr_v4(evp
)
3146 || is_evpn_prefix_ipaddr_v6(evp
)))
3149 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3153 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3155 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3159 flog_err(EC_BGP_EVPN_FAIL
,
3160 "%u: Failed to %s prefix %s in VRF %s",
3162 install
? "install" : "uninstall",
3163 prefix2str(evp
, buf
, sizeof(buf
)),
3164 vrf_id_to_name(bgp_vrf
->vrf_id
));
3173 * Install or uninstall route in matching VNIs (list).
3175 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3176 safi_t safi
, struct prefix_evpn
*evp
,
3177 struct bgp_path_info
*pi
,
3178 struct list
*vnis
, int install
)
3180 struct bgpevpn
*vpn
;
3181 struct listnode
*node
, *nnode
;
3183 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3186 if (!is_vni_live(vpn
))
3190 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3192 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3195 flog_err(EC_BGP_EVPN_FAIL
,
3196 "%u: Failed to %s EVPN %s route in VNI %u",
3197 bgp
->vrf_id
, install
? "install" : "uninstall",
3198 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3210 * Install or uninstall route for appropriate VNIs/ESIs.
3212 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3214 struct bgp_path_info
*pi
, int import
)
3216 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3217 struct attr
*attr
= pi
->attr
;
3218 struct ecommunity
*ecom
;
3223 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3224 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3225 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3226 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3227 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3230 /* If we don't have Route Target, nothing much to do. */
3231 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3234 ecom
= attr
->ecommunity
;
3235 if (!ecom
|| !ecom
->size
)
3238 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3239 * attached to the route */
3240 for (i
= 0; i
< ecom
->size
; i
++) {
3242 uint8_t type
, sub_type
;
3243 struct ecommunity_val
*eval
;
3244 struct ecommunity_val eval_tmp
;
3245 struct irt_node
*irt
; /* import rt for l2vni */
3246 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3249 /* Only deal with RTs */
3250 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3251 eval
= (struct ecommunity_val
*)(ecom
->val
3252 + (i
* ECOMMUNITY_SIZE
));
3255 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3259 * macip routes (type-2) are imported into VNI and VRF tables.
3260 * IMET route is imported into VNI table.
3261 * prefix routes are imported into VRF table.
3263 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3264 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3265 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3267 irt
= lookup_import_rt(bgp
, eval
);
3269 install_uninstall_route_in_vnis(
3270 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3273 vrf_irt
= lookup_vrf_import_rt(eval
);
3275 install_uninstall_route_in_vrfs(
3276 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3279 /* Also check for non-exact match.
3280 * In this, we mask out the AS and
3281 * only check on the local-admin sub-field.
3282 * This is to facilitate using
3283 * VNI as the RT for EBGP peering too.
3287 if (type
== ECOMMUNITY_ENCODE_AS
3288 || type
== ECOMMUNITY_ENCODE_AS4
3289 || type
== ECOMMUNITY_ENCODE_IP
) {
3290 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3291 mask_ecom_global_admin(&eval_tmp
, eval
);
3292 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3293 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3297 install_uninstall_route_in_vnis(
3298 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3301 install_uninstall_route_in_vrfs(
3302 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3306 /* es route is imported into the es table */
3307 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3309 /* we will match based on the entire esi to avoid
3310 * imoort of an es route for esi2 into esi1
3312 es
= bgp_evpn_lookup_es(bgp
, &evp
->prefix
.es_addr
.esi
);
3313 if (es
&& is_es_local(es
))
3314 install_uninstall_route_in_es(
3315 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3323 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3326 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3328 /* delete all ipv4 routes and withdraw from peers */
3329 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3330 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3332 /* delete all ipv6 routes and withdraw from peers */
3333 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3334 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3338 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3341 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3343 /* update all ipv4 routes */
3344 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3345 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3347 /* update all ipv6 routes */
3348 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3349 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3353 * update and advertise local routes for a VRF as type-5 routes.
3354 * This is invoked upon RD change for a VRF. Note taht the processing is only
3355 * done in the global route table using the routes which already exist in the
3358 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3360 /* skip if the RD is configured */
3361 if (is_vrf_rd_configured(bgp_vrf
))
3364 /* derive the RD for the VRF based on new router-id */
3365 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3367 /* update advertise ipv4|ipv6 routes as type-5 routes */
3368 update_advertise_vrf_routes(bgp_vrf
);
3372 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3373 * This is invoked upon VRF RD change. The processing is done only from global
3376 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3378 /* skip if the RD is configured */
3379 if (is_vrf_rd_configured(bgp_vrf
))
3382 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3383 delete_withdraw_vrf_routes(bgp_vrf
);
3387 * Update and advertise local routes for a VNI. Invoked upon router-id
3388 * change. Note that the processing is done only on the global route table
3389 * using routes that already exist in the per-VNI table.
3391 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3393 struct prefix_evpn p
;
3394 struct bgp_node
*rn
, *global_rn
;
3395 struct bgp_path_info
*pi
, *global_pi
;
3397 afi_t afi
= AFI_L2VPN
;
3398 safi_t safi
= SAFI_EVPN
;
3400 /* Locate type-3 route for VNI in the per-VNI table and use its
3401 * attributes to create and advertise the type-3 route for this VNI
3402 * in the global table.
3404 * RT-3 only if doing head-end replication
3406 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
3407 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3408 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3409 if (!rn
) /* unexpected */
3411 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
3412 if (pi
->peer
== bgp
->peer_self
&&
3413 pi
->type
== ZEBRA_ROUTE_BGP
3414 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3416 if (!pi
) /* unexpected */
3420 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3421 (struct prefix
*)&p
, &vpn
->prd
);
3422 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
,
3423 1, &pi
, 0, mac_mobility_seqnum(attr
));
3425 /* Schedule for processing and unlock node. */
3426 bgp_process(bgp
, global_rn
, afi
, safi
);
3427 bgp_unlock_node(global_rn
);
3430 /* Now, walk this VNI's route table and use the route and its attribute
3431 * to create and schedule route in global table.
3433 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
3434 rn
= bgp_route_next(rn
)) {
3435 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3437 /* Identify MAC-IP local routes. */
3438 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3441 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
3442 if (pi
->peer
== bgp
->peer_self
3443 && pi
->type
== ZEBRA_ROUTE_BGP
3444 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3449 /* Create route in global routing table using this route entry's
3453 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3454 (struct prefix
*)evp
, &vpn
->prd
);
3456 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
3458 mac_mobility_seqnum(attr
));
3460 /* Schedule for processing and unlock node. */
3461 bgp_process(bgp
, global_rn
, afi
, safi
);
3462 bgp_unlock_node(global_rn
);
3469 * Delete (and withdraw) local routes for a VNI - only from the global
3470 * table. Invoked upon router-id change.
3472 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3475 struct prefix_evpn p
;
3476 struct bgp_node
*global_rn
;
3477 struct bgp_path_info
*pi
;
3478 afi_t afi
= AFI_L2VPN
;
3479 safi_t safi
= SAFI_EVPN
;
3481 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3482 * for this VNI - from the global table.
3484 ret
= delete_global_type2_routes(bgp
, vpn
);
3488 /* Remove type-3 route for this VNI from global table. */
3489 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3490 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3491 (struct prefix
*)&p
, &vpn
->prd
);
3493 /* Delete route entry in the global EVPN table. */
3494 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &pi
);
3496 /* Schedule for processing - withdraws to peers happen from
3500 bgp_process(bgp
, global_rn
, afi
, safi
);
3501 bgp_unlock_node(global_rn
);
3508 * Handle router-id change. Update and advertise local routes corresponding
3509 * to this VNI from peers. Note that this is invoked after updating the
3510 * router-id. The routes in the per-VNI table are used to create routes in
3511 * the global table and schedule them.
3513 static void update_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3515 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3517 /* Skip VNIs with configured RD. */
3518 if (is_rd_configured(vpn
))
3521 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3522 update_advertise_vni_routes(bgp
, vpn
);
3526 * Handle router-id change. Delete and withdraw local routes corresponding
3527 * to this VNI from peers. Note that this is invoked prior to updating
3528 * the router-id and is done only on the global route table, the routes
3529 * are needed in the per-VNI table to re-advertise with new router id.
3531 static void withdraw_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3533 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3535 /* Skip VNIs with configured RD. */
3536 if (is_rd_configured(vpn
))
3539 delete_withdraw_vni_routes(bgp
, vpn
);
3543 * Create RT-3 for a VNI and schedule for processing and advertisement.
3544 * This is invoked upon flooding mode changing to head-end replication.
3546 static void create_advertise_type3(struct hash_backet
*backet
, void *data
)
3548 struct bgpevpn
*vpn
= backet
->data
;
3549 struct bgp
*bgp
= data
;
3550 struct prefix_evpn p
;
3552 if (!vpn
|| !is_vni_live(vpn
))
3555 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3556 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0))
3557 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3558 "Type3 route creation failure for VNI %u", vpn
->vni
);
3562 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3563 * This is invoked upon flooding mode changing to drop BUM packets.
3565 static void delete_withdraw_type3(struct hash_backet
*backet
, void *data
)
3567 struct bgpevpn
*vpn
= backet
->data
;
3568 struct bgp
*bgp
= data
;
3569 struct prefix_evpn p
;
3571 if (!vpn
|| !is_vni_live(vpn
))
3574 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3575 delete_evpn_route(bgp
, vpn
, &p
);
3579 * Process received EVPN type-2 route (advertise or withdraw).
3581 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3582 struct attr
*attr
, uint8_t *pfx
, int psize
,
3583 uint32_t addpath_id
)
3585 struct prefix_rd prd
;
3586 struct prefix_evpn p
;
3587 struct bgp_route_evpn evpn
;
3589 uint8_t macaddr_len
;
3590 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
3591 uint32_t num_labels
= 0;
3595 /* Type-2 route should be either 33, 37 or 49 bytes or an
3596 * additional 3 bytes if there is a second label (VNI):
3597 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3598 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3599 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3601 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3602 && psize
!= 40 && psize
!= 52) {
3603 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3604 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3605 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3609 memset(&evpn
, 0, sizeof(evpn
));
3611 /* Make prefix_rd */
3612 prd
.family
= AF_UNSPEC
;
3614 memcpy(&prd
.val
, pfx
, 8);
3617 /* Make EVPN prefix. */
3618 memset(&p
, 0, sizeof(struct prefix_evpn
));
3620 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3621 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3623 /* Copy Ethernet Seg Identifier */
3624 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
3627 /* Copy Ethernet Tag */
3628 memcpy(ð_tag
, pfx
, 4);
3629 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3632 /* Get the MAC Addr len */
3633 macaddr_len
= *pfx
++;
3635 /* Get the MAC Addr */
3636 if (macaddr_len
== (ETH_ALEN
* 8)) {
3637 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
3641 EC_BGP_EVPN_ROUTE_INVALID
,
3642 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3643 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3649 ipaddr_len
= *pfx
++;
3650 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3651 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3653 EC_BGP_EVPN_ROUTE_INVALID
,
3654 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3655 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3660 ipaddr_len
/= 8; /* Convert to bytes. */
3661 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3664 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
3668 /* Get the VNI(s). Stored as bytes here. */
3670 memset(label
, 0, sizeof(label
));
3671 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
3672 pfx
+= BGP_LABEL_BYTES
;
3673 psize
-= (33 + ipaddr_len
);
3674 /* Do we have a second VNI? */
3677 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
3679 * If in future, we are required to access additional fields,
3680 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
3685 /* Process the route. */
3687 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3688 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3689 &prd
, &label
[0], num_labels
, 0, &evpn
);
3691 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3692 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3693 &prd
, &label
[0], num_labels
, &evpn
);
3698 * Process received EVPN type-3 route (advertise or withdraw).
3700 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3701 struct attr
*attr
, uint8_t *pfx
, int psize
,
3702 uint32_t addpath_id
)
3704 struct prefix_rd prd
;
3705 struct prefix_evpn p
;
3710 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3711 * IP len (1) and IP (4 or 16).
3713 if (psize
!= 17 && psize
!= 29) {
3714 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3715 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3716 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3720 /* If PMSI is present, log if it is anything other than IR.
3721 * Note: We just simply ignore the values as it is not clear if
3722 * doing anything else is better.
3725 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3726 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
) {
3728 EC_BGP_EVPN_PMSI_PRESENT
,
3729 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3730 peer
->bgp
->vrf_id
, peer
->host
,
3731 attr
->pmsi_tnl_type
);
3735 /* Make prefix_rd */
3736 prd
.family
= AF_UNSPEC
;
3738 memcpy(&prd
.val
, pfx
, 8);
3741 /* Make EVPN prefix. */
3742 memset(&p
, 0, sizeof(struct prefix_evpn
));
3744 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3745 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3747 /* Copy Ethernet Tag */
3748 memcpy(ð_tag
, pfx
, 4);
3749 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
3753 ipaddr_len
= *pfx
++;
3754 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3755 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
3756 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
3759 EC_BGP_EVPN_ROUTE_INVALID
,
3760 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3761 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3765 /* Process the route. */
3767 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3768 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3769 &prd
, NULL
, 0, 0, NULL
);
3771 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3772 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3773 &prd
, NULL
, 0, NULL
);
3778 * Process received EVPN type-4 route (advertise or withdraw).
3780 static int process_type4_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3781 struct attr
*attr
, uint8_t *pfx
, int psize
,
3782 uint32_t addpath_id
)
3787 struct in_addr vtep_ip
;
3788 struct prefix_rd prd
;
3789 struct prefix_evpn p
;
3791 /* Type-4 route should be either 23 or 35 bytes
3792 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
3794 if (psize
!= 23 && psize
!= 35) {
3795 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3796 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
3797 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3801 /* Make prefix_rd */
3802 prd
.family
= AF_UNSPEC
;
3804 memcpy(&prd
.val
, pfx
, 8);
3808 memcpy(&esi
, pfx
, ESI_BYTES
);
3813 ipaddr_len
= *pfx
++;
3814 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3815 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
3818 EC_BGP_EVPN_ROUTE_INVALID
,
3819 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
3820 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3824 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
3825 /* Process the route. */
3827 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3828 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3829 &prd
, NULL
, 0, 0, NULL
);
3831 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3832 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3833 &prd
, NULL
, 0, NULL
);
3840 * Process received EVPN type-5 route (advertise or withdraw).
3842 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3843 struct attr
*attr
, uint8_t *pfx
, int psize
,
3844 uint32_t addpath_id
, int withdraw
)
3846 struct prefix_rd prd
;
3847 struct prefix_evpn p
;
3848 struct bgp_route_evpn evpn
;
3851 mpls_label_t label
; /* holds the VNI as in the packet */
3854 /* Type-5 route should be 34 or 58 bytes:
3855 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3856 * GW (4 or 16) and VNI (3).
3857 * Note that the IP and GW should both be IPv4 or both IPv6.
3859 if (psize
!= 34 && psize
!= 58) {
3860 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3861 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3862 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3866 /* Make prefix_rd */
3867 prd
.family
= AF_UNSPEC
;
3869 memcpy(&prd
.val
, pfx
, 8);
3872 /* Make EVPN prefix. */
3873 memset(&p
, 0, sizeof(struct prefix_evpn
));
3875 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3876 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
3878 /* Additional information outside of prefix - ESI and GW IP */
3879 memset(&evpn
, 0, sizeof(evpn
));
3882 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
3885 /* Fetch Ethernet Tag. */
3886 memcpy(ð_tag
, pfx
, 4);
3887 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
3890 /* Fetch IP prefix length. */
3892 if (ippfx_len
> IPV6_MAX_BITLEN
) {
3894 EC_BGP_EVPN_ROUTE_INVALID
,
3895 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3896 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
3899 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
3901 /* Determine IPv4 or IPv6 prefix */
3902 /* Since the address and GW are from the same family, this just becomes
3903 * a simple check on the total size.
3906 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
3907 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
3909 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
3912 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
3913 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
3915 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
3919 /* Get the VNI (in MPLS label field). Stored as bytes here. */
3920 memset(&label
, 0, sizeof(label
));
3921 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
3924 * If in future, we are required to access additional fields,
3925 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
3929 /* Process the route. */
3931 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3932 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3933 &prd
, &label
, 1, 0, &evpn
);
3935 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3936 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3937 &prd
, &label
, 1, &evpn
);
3942 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
3943 struct prefix_rd
*prd
, mpls_label_t
*label
,
3944 uint32_t num_labels
, struct attr
*attr
)
3948 struct evpn_addr
*p_evpn_p
;
3950 memset(&temp
, 0, 16);
3951 if (p
->family
!= AF_EVPN
)
3953 p_evpn_p
= &(p
->u
.prefix_evpn
);
3955 /* len denites the total len of IP and GW-IP in the route
3956 IP and GW-IP have to be both ipv4 or ipv6
3958 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3959 len
= 8; /* IP and GWIP are both ipv4 */
3961 len
= 32; /* IP and GWIP are both ipv6 */
3962 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
3963 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
3964 stream_put(s
, prd
->val
, 8);
3966 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
3968 stream_put(s
, &temp
, 10);
3969 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
3970 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
3971 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3972 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
3974 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
3976 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3978 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
3980 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
3982 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3983 stream_put_ipv4(s
, 0);
3985 stream_put(s
, &temp
, 16);
3989 stream_put(s
, label
, 3);
3995 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
3997 static void cleanup_vni_on_disable(struct hash_backet
*backet
, struct bgp
*bgp
)
3999 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
4001 /* Remove EVPN routes and schedule for processing. */
4002 delete_routes_for_vni(bgp
, vpn
);
4004 /* Clear "live" flag and see if hash needs to be freed. */
4005 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4006 if (!is_vni_configured(vpn
))
4007 bgp_evpn_free(bgp
, vpn
);
4011 * Free a VNI entry; iterator function called during cleanup.
4013 static void free_vni_entry(struct hash_backet
*backet
, struct bgp
*bgp
)
4015 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
4017 delete_all_vni_routes(bgp
, vpn
);
4018 bgp_evpn_free(bgp
, vpn
);
4022 * Derive AUTO import RT for BGP VRF - L3VNI
4024 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4026 struct bgp
*bgp_def
= NULL
;
4028 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4029 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4032 bgp_def
= bgp_get_default();
4035 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4039 * Delete AUTO import RT from BGP VRF - L3VNI
4041 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4043 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4047 * Derive AUTO export RT for BGP VRF - L3VNI
4049 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4051 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4052 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4056 * Delete AUTO export RT from BGP VRF - L3VNI
4058 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4060 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4063 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4065 struct bgp
*bgp_def
= NULL
;
4066 struct listnode
*node
= NULL
;
4067 struct bgpevpn
*vpn
= NULL
;
4069 bgp_def
= bgp_get_default();
4073 /* update all type-5 routes */
4074 update_advertise_vrf_routes(bgp_vrf
);
4076 /* update all type-2 routes */
4077 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4078 update_routes_for_vni(bgp_def
, vpn
);
4082 * Handle autort change for a given VNI.
4084 static void update_autort_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
4086 struct bgpevpn
*vpn
= backet
->data
;
4088 if (!is_import_rt_configured(vpn
)) {
4089 if (is_vni_live(vpn
))
4090 bgp_evpn_uninstall_routes(bgp
, vpn
);
4091 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4092 list_delete_all_node(vpn
->import_rtl
);
4093 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4094 if (is_vni_live(vpn
))
4095 bgp_evpn_install_routes(bgp
, vpn
);
4097 if (!is_export_rt_configured(vpn
)) {
4098 list_delete_all_node(vpn
->export_rtl
);
4099 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4100 if (is_vni_live(vpn
))
4101 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4109 /* withdraw type-5 route corresponding to ip prefix */
4110 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4111 afi_t afi
, safi_t safi
)
4114 struct prefix_evpn evp
;
4115 char buf
[PREFIX_STRLEN
];
4117 build_type5_prefix_from_ip_prefix(&evp
, p
);
4118 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4121 EC_BGP_EVPN_ROUTE_DELETE
,
4122 "%u failed to delete type-5 route for prefix %s in vrf %s",
4123 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4124 vrf_id_to_name(bgp_vrf
->vrf_id
));
4128 /* withdraw all type-5 routes for an address family */
4129 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4131 struct bgp_table
*table
= NULL
;
4132 struct bgp_node
*rn
= NULL
;
4133 struct bgp_path_info
*pi
;
4135 table
= bgp_vrf
->rib
[afi
][safi
];
4136 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4137 /* Only care about "selected" routes - non-imported. */
4138 /* TODO: Support for AddPath for EVPN. */
4139 for (pi
= rn
->info
; pi
; pi
= pi
->next
) {
4140 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4141 && (!pi
->extra
|| !pi
->extra
->parent
)) {
4142 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
4151 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4152 * to this function correspond to those of the source IP prefix (best
4153 * path in the case of the attr. In the case of a local prefix (when we
4154 * are advertising local subnets), the src_attr will be NULL.
4156 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4157 struct attr
*src_attr
, afi_t afi
,
4161 struct prefix_evpn evp
;
4162 char buf
[PREFIX_STRLEN
];
4164 build_type5_prefix_from_ip_prefix(&evp
, p
);
4165 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4167 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4168 "%u: Failed to create type-5 route for prefix %s",
4169 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4172 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4173 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4174 * advertisement is enabled.
4176 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4179 struct bgp_table
*table
= NULL
;
4180 struct bgp_node
*rn
= NULL
;
4181 struct bgp_path_info
*pi
;
4183 table
= bgp_vrf
->rib
[afi
][safi
];
4184 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4185 /* Need to identify the "selected" route entry to use its
4186 * attribute. Also, we only consider "non-imported" routes.
4187 * TODO: Support for AddPath for EVPN.
4189 for (pi
= rn
->info
; pi
; pi
= pi
->next
) {
4190 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4191 && (!pi
->extra
|| !pi
->extra
->parent
)) {
4193 /* apply the route-map */
4194 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4197 ret
= route_map_apply(
4198 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4200 &rn
->p
, RMAP_BGP
, pi
);
4201 if (ret
== RMAP_DENYMATCH
)
4204 bgp_evpn_advertise_type5_route(
4205 bgp_vrf
, &rn
->p
, pi
->attr
, afi
, safi
);
4212 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4214 struct listnode
*node
, *nnode
, *node_to_del
;
4215 struct ecommunity
*ecom
, *ecom_auto
;
4216 struct ecommunity_val eval
;
4218 if (bgp
->advertise_autort_rfc8365
)
4219 vni
|= EVPN_AUTORT_VXLAN
;
4220 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4222 ecom_auto
= ecommunity_new();
4223 ecommunity_add_val(ecom_auto
, &eval
);
4226 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4227 if (ecommunity_match(ecom
, ecom_auto
)) {
4228 ecommunity_free(&ecom
);
4234 list_delete_node(rtl
, node_to_del
);
4236 ecommunity_free(&ecom_auto
);
4239 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4240 struct ecommunity
*ecomadd
)
4242 /* uninstall routes from vrf */
4243 uninstall_routes_for_vrf(bgp_vrf
);
4245 /* Cleanup the RT to VRF mapping */
4246 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4248 /* Remove auto generated RT */
4249 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4251 /* Add the newly configured RT to RT list */
4252 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4253 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4255 /* map VRF to its RTs */
4256 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4258 /* install routes matching the new VRF */
4259 install_routes_for_vrf(bgp_vrf
);
4262 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4263 struct ecommunity
*ecomdel
)
4265 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4266 struct ecommunity
*ecom
= NULL
;
4268 /* uninstall routes from vrf */
4269 uninstall_routes_for_vrf(bgp_vrf
);
4271 /* Cleanup the RT to VRF mapping */
4272 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4274 /* remove the RT from the RT list */
4275 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4276 if (ecommunity_match(ecom
, ecomdel
)) {
4277 ecommunity_free(&ecom
);
4284 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4286 assert(bgp_vrf
->vrf_import_rtl
);
4287 /* fallback to auto import rt, if this was the last RT */
4288 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4289 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4290 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4293 /* map VRFs to its RTs */
4294 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4296 /* install routes matching this new RT */
4297 install_routes_for_vrf(bgp_vrf
);
4300 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4301 struct ecommunity
*ecomadd
)
4303 /* remove auto-generated RT */
4304 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4306 /* Add the new RT to the RT list */
4307 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4308 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4310 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4313 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4314 struct ecommunity
*ecomdel
)
4316 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4317 struct ecommunity
*ecom
= NULL
;
4319 /* Remove the RT from the RT list */
4320 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4321 if (ecommunity_match(ecom
, ecomdel
)) {
4322 ecommunity_free(&ecom
);
4329 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4332 * Temporary assert to make SA happy.
4333 * The ALL_LIST_ELEMENTS macro above has a NULL check
4334 * which means that SA is going to complain about
4335 * the list_isempty call, which doesn't NULL check.
4336 * So until we get this situation cleaned up, here
4339 assert(bgp_vrf
->vrf_export_rtl
);
4341 /* fall back to auto-generated RT if this was the last RT */
4342 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4343 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4344 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4347 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4351 * Handle change to BGP router id. This is invoked twice by the change
4352 * handler, first before the router id has been changed and then after
4353 * the router id has been changed. The first invocation will result in
4354 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4355 * will result in the routes being re-advertised.
4357 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4361 /* delete and withdraw all the type-5 routes
4362 stored in the global table for this vrf
4364 withdraw_router_id_vrf(bgp
);
4366 /* delete all the VNI routes (type-2/type-3) routes for all the
4369 hash_iterate(bgp
->vnihash
,
4370 (void (*)(struct hash_backet
*,
4371 void *))withdraw_router_id_vni
,
4375 /* advertise all routes in the vrf as type-5 routes with the new
4378 update_router_id_vrf(bgp
);
4380 /* advertise all the VNI routes (type-2/type-3) routes with the
4383 hash_iterate(bgp
->vnihash
,
4384 (void (*)(struct hash_backet
*,
4385 void *))update_router_id_vni
,
4391 * Handle change to auto-RT algorithm - update and advertise local routes.
4393 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4395 hash_iterate(bgp
->vnihash
,
4396 (void (*)(struct hash_backet
*,
4397 void*))update_autort_vni
,
4402 * Handle change to export RT - update and advertise local routes.
4404 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4406 return update_routes_for_vni(bgp
, vpn
);
4409 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4412 delete_withdraw_vrf_routes(bgp_vrf
);
4414 update_advertise_vrf_routes(bgp_vrf
);
4418 * Handle change to RD. This is invoked twice by the change handler,
4419 * first before the RD has been changed and then after the RD has
4420 * been changed. The first invocation will result in local routes
4421 * of this VNI being deleted and withdrawn and the next will result
4422 * in the routes being re-advertised.
4424 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4428 delete_withdraw_vni_routes(bgp
, vpn
);
4430 update_advertise_vni_routes(bgp
, vpn
);
4434 * Install routes for this VNI. Invoked upon change to Import RT.
4436 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4438 return install_routes_for_vni(bgp
, vpn
);
4442 * Uninstall all routes installed for this VNI. Invoked upon change
4445 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4447 return uninstall_routes_for_vni(bgp
, vpn
);
4451 * TODO: Hardcoded for a maximum of 2 VNIs right now
4453 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4458 vni1
= label2vni(label
);
4459 if (num_labels
== 2) {
4460 vni2
= label2vni(label
+ 1);
4461 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4463 snprintf(buf
, len
, "%u", vni1
);
4468 * Function to convert evpn route to json format.
4469 * NOTE: We don't use prefix2str as the output here is a bit different.
4471 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
4473 char buf1
[ETHER_ADDR_STRLEN
];
4474 char buf2
[PREFIX2STR_BUFFER
];
4479 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4480 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4481 json_object_int_add(json
, "ethTag",
4482 p
->prefix
.imet_addr
.eth_tag
);
4483 json_object_int_add(json
, "ipLen",
4484 is_evpn_prefix_ipaddr_v4(p
)
4487 json_object_string_add(json
, "ip",
4488 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4489 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4490 if (is_evpn_prefix_ipaddr_none(p
)) {
4491 json_object_int_add(json
, "routeType",
4492 p
->prefix
.route_type
);
4493 json_object_int_add(json
, "ethTag",
4494 p
->prefix
.macip_addr
.eth_tag
);
4495 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4496 json_object_string_add(json
, "mac",
4497 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4503 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4506 json_object_int_add(json
, "routeType",
4507 p
->prefix
.route_type
);
4508 json_object_int_add(json
, "ethTag",
4509 p
->prefix
.macip_addr
.eth_tag
);
4510 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4511 json_object_string_add(json
, "mac",
4512 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4515 json_object_int_add(json
, "ipLen",
4516 is_evpn_prefix_ipaddr_v4(p
)
4519 json_object_string_add(
4522 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4524 PREFIX2STR_BUFFER
));
4527 /* Currently, this is to cater to other AF_ETHERNET code. */
4532 * Function to convert evpn route to string.
4533 * NOTE: We don't use prefix2str as the output here is a bit different.
4535 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
4537 char buf1
[ETHER_ADDR_STRLEN
];
4538 char buf2
[PREFIX2STR_BUFFER
];
4539 char buf3
[ESI_STR_LEN
];
4541 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4542 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
4543 p
->prefix
.imet_addr
.eth_tag
,
4544 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4546 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4547 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4548 if (is_evpn_prefix_ipaddr_none(p
))
4549 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4550 p
->prefix
.route_type
,
4551 p
->prefix
.macip_addr
.eth_tag
,
4553 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4558 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4560 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
4561 p
->prefix
.route_type
,
4562 p
->prefix
.macip_addr
.eth_tag
,
4564 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4566 family
== AF_INET
? IPV4_MAX_BITLEN
4569 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4571 PREFIX2STR_BUFFER
));
4573 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
4574 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4575 p
->prefix
.route_type
,
4576 p
->prefix
.prefix_addr
.eth_tag
,
4577 p
->prefix
.prefix_addr
.ip_prefix_length
,
4578 is_evpn_prefix_ipaddr_v4(p
)
4579 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
4580 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
4581 } else if (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
4582 snprintf(buf
, len
, "[%d]:[%s]:[%d]:[%s]",
4583 p
->prefix
.route_type
,
4584 esi_to_str(&p
->prefix
.es_addr
.esi
, buf3
, sizeof(buf3
)),
4585 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4587 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
));
4589 /* For EVPN route types not supported yet. */
4590 snprintf(buf
, len
, "(unsupported route type %d)",
4591 p
->prefix
.route_type
);
4598 * Encode EVPN prefix in Update (MP_REACH)
4600 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
4601 struct prefix_rd
*prd
, mpls_label_t
*label
,
4602 uint32_t num_labels
, struct attr
*attr
,
4603 int addpath_encode
, uint32_t addpath_tx_id
)
4605 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4606 int len
, ipa_len
= 0;
4609 stream_putl(s
, addpath_tx_id
);
4612 stream_putc(s
, evp
->prefix
.route_type
);
4614 switch (evp
->prefix
.route_type
) {
4615 case BGP_EVPN_MAC_IP_ROUTE
:
4616 if (is_evpn_prefix_ipaddr_v4(evp
))
4617 ipa_len
= IPV4_MAX_BYTELEN
;
4618 else if (is_evpn_prefix_ipaddr_v6(evp
))
4619 ipa_len
= IPV6_MAX_BYTELEN
;
4620 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4621 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4622 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4624 stream_putc(s
, len
);
4625 stream_put(s
, prd
->val
, 8); /* RD */
4627 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
4629 stream_put(s
, 0, 10);
4630 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4631 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4632 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4633 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4634 if (ipa_len
) /* IP */
4635 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4637 /* 1st label is the L2 VNI */
4638 stream_put(s
, label
, BGP_LABEL_BYTES
);
4639 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4640 if (ipa_len
&& num_labels
> 1)
4641 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4644 case BGP_EVPN_IMET_ROUTE
:
4645 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4646 stream_put(s
, prd
->val
, 8); /* RD */
4647 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4648 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4649 /* Originating Router's IP Addr */
4650 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4653 case BGP_EVPN_ES_ROUTE
:
4654 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4655 stream_put(s
, prd
->val
, 8); /* RD */
4656 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4657 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4659 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4662 case BGP_EVPN_IP_PREFIX_ROUTE
:
4663 /* TODO: AddPath support. */
4664 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4672 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4673 struct bgp_nlri
*packet
, int withdraw
)
4679 uint32_t addpath_id
;
4680 int addpath_encoded
;
4685 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4687 lim
= pnt
+ packet
->length
;
4689 safi
= packet
->safi
;
4693 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
4694 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
4695 PEER_CAP_ADDPATH_AF_TX_RCV
));
4697 for (; pnt
< lim
; pnt
+= psize
) {
4698 /* Clear prefix structure. */
4699 memset(&p
, 0, sizeof(struct prefix
));
4701 /* Deal with path-id if AddPath is supported. */
4702 if (addpath_encoded
) {
4703 /* When packet overflow occurs return immediately. */
4704 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4707 addpath_id
= ntohl(*((uint32_t *)pnt
));
4708 pnt
+= BGP_ADDPATH_ID_LEN
;
4711 /* All EVPN NLRI types start with type and length. */
4718 /* When packet overflow occur return immediately. */
4719 if (pnt
+ psize
> lim
)
4723 case BGP_EVPN_MAC_IP_ROUTE
:
4724 if (process_type2_route(peer
, afi
, safi
,
4725 withdraw
? NULL
: attr
, pnt
,
4726 psize
, addpath_id
)) {
4729 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4730 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4735 case BGP_EVPN_IMET_ROUTE
:
4736 if (process_type3_route(peer
, afi
, safi
,
4737 withdraw
? NULL
: attr
, pnt
,
4738 psize
, addpath_id
)) {
4741 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4742 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4747 case BGP_EVPN_ES_ROUTE
:
4748 if (process_type4_route(peer
, afi
, safi
,
4749 withdraw
? NULL
: attr
, pnt
,
4750 psize
, addpath_id
)) {
4753 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4754 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4759 case BGP_EVPN_IP_PREFIX_ROUTE
:
4760 if (process_type5_route(peer
, afi
, safi
, attr
, pnt
,
4761 psize
, addpath_id
, withdraw
)) {
4764 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4765 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4775 /* Packet length consistency check. */
4783 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
4784 * The mapping will be used during route processing.
4785 * bgp_def: default bgp instance
4786 * bgp_vrf: specific bgp vrf instance on which RT is configured
4788 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
4791 struct ecommunity_val
*eval
= NULL
;
4792 struct listnode
*node
= NULL
, *nnode
= NULL
;
4793 struct ecommunity
*ecom
= NULL
;
4795 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4796 for (i
= 0; i
< ecom
->size
; i
++) {
4797 eval
= (struct ecommunity_val
*)(ecom
->val
4799 * ECOMMUNITY_SIZE
));
4800 map_vrf_to_rt(bgp_vrf
, eval
);
4806 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
4808 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
4811 struct ecommunity_val
*eval
;
4812 struct listnode
*node
, *nnode
;
4813 struct ecommunity
*ecom
;
4815 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4816 for (i
= 0; i
< ecom
->size
; i
++) {
4817 struct vrf_irt_node
*irt
;
4818 struct ecommunity_val eval_tmp
;
4820 eval
= (struct ecommunity_val
*)(ecom
->val
4822 * ECOMMUNITY_SIZE
));
4823 /* If using "automatic" RT, we only care about the
4824 * local-admin sub-field.
4825 * This is to facilitate using VNI as the RT for EBGP
4828 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4829 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
4830 BGP_VRF_IMPORT_RT_CFGD
))
4831 mask_ecom_global_admin(&eval_tmp
, eval
);
4833 irt
= lookup_vrf_import_rt(&eval_tmp
);
4835 unmap_vrf_from_rt(bgp_vrf
, irt
);
4842 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
4843 * The mapping will be used during route processing.
4845 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4848 struct ecommunity_val
*eval
;
4849 struct listnode
*node
, *nnode
;
4850 struct ecommunity
*ecom
;
4852 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4853 for (i
= 0; i
< ecom
->size
; i
++) {
4854 eval
= (struct ecommunity_val
*)(ecom
->val
4856 * ECOMMUNITY_SIZE
));
4857 map_vni_to_rt(bgp
, vpn
, eval
);
4863 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
4865 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4868 struct ecommunity_val
*eval
;
4869 struct listnode
*node
, *nnode
;
4870 struct ecommunity
*ecom
;
4872 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4873 for (i
= 0; i
< ecom
->size
; i
++) {
4874 struct irt_node
*irt
;
4875 struct ecommunity_val eval_tmp
;
4877 eval
= (struct ecommunity_val
*)(ecom
->val
4879 * ECOMMUNITY_SIZE
));
4880 /* If using "automatic" RT, we only care about the
4881 * local-admin sub-field.
4882 * This is to facilitate using VNI as the RT for EBGP
4885 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4886 if (!is_import_rt_configured(vpn
))
4887 mask_ecom_global_admin(&eval_tmp
, eval
);
4889 irt
= lookup_import_rt(bgp
, &eval_tmp
);
4891 unmap_vni_from_rt(bgp
, vpn
, irt
);
4897 * Derive Import RT automatically for VNI and map VNI to RT.
4898 * The mapping will be used during route processing.
4900 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4902 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
4903 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
4906 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
4910 * Derive Export RT automatically for VNI.
4912 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4914 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
4915 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
4919 * Derive RD automatically for VNI using passed information - it
4920 * is of the form RouterId:unique-id-for-vni.
4922 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
4924 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
4928 * Derive RD automatically for VNI using passed information - it
4929 * is of the form RouterId:unique-id-for-vni.
4931 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4935 vpn
->prd
.family
= AF_UNSPEC
;
4936 vpn
->prd
.prefixlen
= 64;
4937 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
4938 (void)str2prefix_rd(buf
, &vpn
->prd
);
4939 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
4945 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
4947 struct list
*inst
= bm
->bgp
;
4948 struct listnode
*node
;
4949 struct bgp
*bgp_vrf
;
4951 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
4952 if (bgp_vrf
->l3vni
== vni
)
4962 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
4964 struct bgpevpn
*vpn
;
4967 memset(&tmp
, 0, sizeof(struct bgpevpn
));
4969 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
4974 * Create a new vpn - invoked upon configuration or zebra notification.
4976 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
4977 struct in_addr originator_ip
,
4978 vrf_id_t tenant_vrf_id
)
4980 struct bgpevpn
*vpn
;
4985 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
4989 /* Set values - RD and RT set to defaults. */
4991 vpn
->originator_ip
= originator_ip
;
4992 vpn
->tenant_vrf_id
= tenant_vrf_id
;
4994 /* Initialize route-target import and export lists */
4995 vpn
->import_rtl
= list_new();
4996 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4997 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
4998 vpn
->export_rtl
= list_new();
4999 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5000 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
5001 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5002 derive_rd_rt_for_vni(bgp
, vpn
);
5004 /* Initialize EVPN route table. */
5005 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5008 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
5009 XFREE(MTYPE_BGP_EVPN
, vpn
);
5013 /* add to l2vni list on corresponding vrf */
5014 bgpevpn_link_to_l3vni(vpn
);
5016 QOBJ_REG(vpn
, bgpevpn
);
5021 * Free a given VPN - called in multiple scenarios such as zebra
5022 * notification, configuration being deleted, advertise-all-vni disabled etc.
5023 * This just frees appropriate memory, caller should have taken other
5026 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5028 bgpevpn_unlink_from_l3vni(vpn
);
5029 bgp_table_unlock(vpn
->route_table
);
5030 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5031 list_delete(&vpn
->import_rtl
);
5032 list_delete(&vpn
->export_rtl
);
5033 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5034 hash_release(bgp
->vnihash
, vpn
);
5036 XFREE(MTYPE_BGP_EVPN
, vpn
);
5042 struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
)
5047 memset(&tmp
, 0, sizeof(struct evpnes
));
5048 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
5049 es
= hash_lookup(bgp
->esihash
, &tmp
);
5054 * Create a new local es - invoked upon zebra notification.
5056 struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
,
5058 struct ipaddr
*originator_ip
)
5066 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct evpnes
));
5070 /* set the ESI and originator_ip */
5071 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
5072 memcpy(&es
->originator_ip
, originator_ip
, sizeof(struct ipaddr
));
5074 /* Initialise the VTEP list */
5075 es
->vtep_list
= list_new();
5076 es
->vtep_list
->cmp
= (int (*)(void *, void *))evpn_vtep_ip_cmp
;
5078 /* auto derive RD for this es */
5079 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
5080 es
->prd
.family
= AF_UNSPEC
;
5081 es
->prd
.prefixlen
= 64;
5082 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), es
->rd_id
);
5083 (void)str2prefix_rd(buf
, &es
->prd
);
5085 /* Initialize the ES route table */
5086 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5089 if (!hash_get(bgp
->esihash
, es
, hash_alloc_intern
)) {
5090 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5094 QOBJ_REG(es
, evpnes
);
5100 * This just frees appropriate memory, caller should have taken other
5103 void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
)
5105 list_delete(&es
->vtep_list
);
5106 bgp_table_unlock(es
->route_table
);
5107 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
5108 hash_release(bgp
->esihash
, es
);
5110 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5114 * Import evpn route from global table to VNI/VRF/ESI.
5116 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5117 struct prefix
*p
, struct bgp_path_info
*pi
)
5119 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5123 * Unimport evpn route from VNI/VRF/ESI.
5125 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5126 struct prefix
*p
, struct bgp_path_info
*pi
)
5128 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5131 /* filter routes which have martian next hops */
5132 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5136 struct bgp_node
*rd_rn
, *rn
;
5137 struct bgp_table
*table
;
5138 struct bgp_path_info
*pi
;
5143 /* Walk entire global routing table and evaluate routes which could be
5144 * imported into this VPN. Note that we cannot just look at the routes
5145 * for the VNI's RD -
5146 * remote routes applicable for this VNI could have any RD.
5148 /* EVPN routes are a 2-level table. */
5149 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
5150 rd_rn
= bgp_route_next(rd_rn
)) {
5151 table
= (struct bgp_table
*)(rd_rn
->info
);
5155 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5157 for (pi
= rn
->info
; pi
; pi
= pi
->next
) {
5159 /* Consider "valid" remote routes applicable for
5161 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5162 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5165 if (bgp_nexthop_self(bgp
, pi
->attr
->nexthop
)) {
5167 char attr_str
[BUFSIZ
];
5168 char pbuf
[PREFIX_STRLEN
];
5170 bgp_dump_attr(pi
->attr
, attr_str
,
5173 if (bgp_debug_update(pi
->peer
, &rn
->p
,
5176 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
5183 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5186 bgp_rib_remove(rn
, pi
, pi
->peer
, afi
,
5197 * Handle del of a local MACIP.
5199 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5202 struct bgpevpn
*vpn
;
5203 struct prefix_evpn p
;
5205 /* Lookup VNI hash - should exist. */
5206 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5207 if (!vpn
|| !is_vni_live(vpn
)) {
5208 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5209 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5210 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5214 /* Remove EVPN type-2 route and schedule for processing. */
5215 build_evpn_type2_prefix(&p
, mac
, ip
);
5216 delete_evpn_route(bgp
, vpn
, &p
);
5222 * Handle add of a local MACIP.
5224 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5225 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
)
5227 struct bgpevpn
*vpn
;
5228 struct prefix_evpn p
;
5230 /* Lookup VNI hash - should exist. */
5231 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5232 if (!vpn
|| !is_vni_live(vpn
)) {
5233 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5234 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5235 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5239 /* Create EVPN type-2 route and schedule for processing. */
5240 build_evpn_type2_prefix(&p
, mac
, ip
);
5241 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
)) {
5242 char buf
[ETHER_ADDR_STRLEN
];
5243 char buf2
[INET6_ADDRSTRLEN
];
5246 EC_BGP_EVPN_ROUTE_CREATE
,
5247 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5248 bgp
->vrf_id
, vpn
->vni
,
5249 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5252 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5253 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5260 static void link_l2vni_hash_to_l3vni(struct hash_backet
*backet
,
5261 struct bgp
*bgp_vrf
)
5263 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
5264 struct bgp
*bgp_def
= NULL
;
5266 bgp_def
= bgp_get_default();
5269 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5270 bgpevpn_link_to_l3vni(vpn
);
5273 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
, struct ethaddr
*rmac
,
5274 struct in_addr originator_ip
, int filter
)
5276 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5277 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5278 struct listnode
*node
= NULL
;
5279 struct bgpevpn
*vpn
= NULL
;
5282 /* get the default instance - required to get the AS number for VRF
5285 bgp_def
= bgp_get_default();
5289 "Cannot process L3VNI %u ADD - default BGP instance not yet created",
5295 /* if the BGP vrf instance doesnt exist - create one */
5296 bgp_vrf
= bgp_lookup_by_name(vrf_id_to_name(vrf_id
));
5301 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5302 BGP_INSTANCE_TYPE_VRF
);
5304 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
:
5305 flog_err(EC_BGP_MULTI_INSTANCE
,
5306 "'bgp multiple-instance' not present\n");
5308 case BGP_ERR_AS_MISMATCH
:
5309 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5310 "BGP is already running; AS is %u\n", as
);
5312 case BGP_ERR_INSTANCE_MISMATCH
:
5313 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5314 "BGP instance name and AS number mismatch\n");
5318 /* mark as auto created */
5319 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5322 /* associate with l3vni */
5323 bgp_vrf
->l3vni
= l3vni
;
5325 /* set the router mac - to be used in mac-ip routes for this vrf */
5326 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
5328 /* set the originator ip */
5329 bgp_vrf
->originator_ip
= originator_ip
;
5331 /* set the right filter - are we using l3vni only for prefix routes? */
5333 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5335 /* auto derive RD/RT */
5336 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5337 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5338 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5339 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5340 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5342 /* link all corresponding l2vnis */
5343 hash_iterate(bgp_def
->vnihash
,
5344 (void (*)(struct hash_backet
*,
5345 void *))link_l2vni_hash_to_l3vni
,
5348 /* Only update all corresponding type-2 routes if we are advertising two
5349 * labels along with type-2 routes
5352 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5353 update_routes_for_vni(bgp_def
, vpn
);
5355 /* advertise type-5 routes if needed */
5356 update_advertise_vrf_routes(bgp_vrf
);
5358 /* install all remote routes belonging to this l3vni into correspondng
5360 install_routes_for_vrf(bgp_vrf
);
5365 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5367 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5368 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5369 struct listnode
*node
= NULL
;
5370 struct listnode
*next
= NULL
;
5371 struct bgpevpn
*vpn
= NULL
;
5373 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5377 "Cannot process L3VNI %u Del - Could not find BGP instance",
5382 bgp_def
= bgp_get_default();
5386 "Cannot process L3VNI %u Del - Could not find default BGP instance",
5391 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5392 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5393 * routes. This will uninstalling the routes from zebra and decremnt the
5396 uninstall_routes_for_vrf(bgp_vrf
);
5398 /* delete/withdraw all type-5 routes */
5399 delete_withdraw_vrf_routes(bgp_vrf
);
5401 /* remove the l3vni from vrf instance */
5404 /* remove the Rmac from the BGP vrf */
5405 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5408 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5409 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5410 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5412 if (!list_isempty(bgp_vrf
->vrf_export_rtl
)) {
5413 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5416 /* update all corresponding local mac-ip routes */
5417 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5418 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5419 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5420 update_routes_for_vni(bgp_def
, vpn
);
5424 /* If any L2VNIs point to this instance, unlink them. */
5425 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5426 bgpevpn_unlink_from_l3vni(vpn
);
5428 /* Delete the instance if it was autocreated */
5429 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5430 bgp_delete(bgp_vrf
);
5436 * Handle del of a local VNI.
5438 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5440 struct bgpevpn
*vpn
;
5442 /* Locate VNI hash */
5443 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5445 if (bgp_debug_zebra(NULL
))
5447 EC_BGP_EVPN_VPN_VNI
,
5448 "%u: VNI hash entry for VNI %u not found at DEL",
5453 /* Remove all local EVPN routes and schedule for processing (to
5454 * withdraw from peers).
5456 delete_routes_for_vni(bgp
, vpn
);
5459 * tunnel is no longer active, del tunnel ip address from tip_hash
5461 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5463 /* Clear "live" flag and see if hash needs to be freed. */
5464 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5465 if (!is_vni_configured(vpn
))
5466 bgp_evpn_free(bgp
, vpn
);
5472 * Handle add (or update) of a local VNI. The VNI changes we care
5473 * about are for the local-tunnel-ip and the (tenant) VRF.
5475 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5476 struct in_addr originator_ip
, vrf_id_t tenant_vrf_id
)
5478 struct bgpevpn
*vpn
;
5479 struct prefix_evpn p
;
5481 /* Lookup VNI. If present and no change, exit. */
5482 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5485 if (is_vni_live(vpn
)
5486 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5487 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5488 /* Probably some other param has changed that we don't
5492 /* Update tenant_vrf_id if it has changed. */
5493 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5494 bgpevpn_unlink_from_l3vni(vpn
);
5495 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5496 bgpevpn_link_to_l3vni(vpn
);
5499 /* If tunnel endpoint IP has changed, update (and delete prior
5500 * type-3 route, if needed.)
5502 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5503 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5505 /* Update all routes with new endpoint IP and/or export RT
5508 if (is_vni_live(vpn
))
5509 update_routes_for_vni(bgp
, vpn
);
5512 /* Create or update as appropriate. */
5514 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
);
5518 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5524 /* if the VNI is live already, there is nothing more to do */
5525 if (is_vni_live(vpn
))
5528 /* Mark as "live" */
5529 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5531 /* tunnel is now active, add tunnel-ip to db */
5532 bgp_tip_add(bgp
, &originator_ip
);
5534 /* filter routes as nexthop database has changed */
5535 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5538 * Create EVPN type-3 route and schedule for processing.
5540 * RT-3 only if doing head-end replication
5542 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
) {
5543 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5544 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0)) {
5545 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5546 "%u: Type3 route creation failure for VNI %u",
5552 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5556 install_routes_for_vni(bgp
, vpn
);
5558 /* If we are advertising gateway mac-ip
5559 It needs to be conveyed again to zebra */
5560 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5566 * bgp_evpn_local_es_del
5568 int bgp_evpn_local_es_del(struct bgp
*bgp
,
5570 struct ipaddr
*originator_ip
)
5572 char buf
[ESI_STR_LEN
];
5573 struct evpnes
*es
= NULL
;
5575 if (!bgp
->esihash
) {
5576 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5581 /* Lookup ESI hash - should exist. */
5582 es
= bgp_evpn_lookup_es(bgp
, esi
);
5584 flog_warn(EC_BGP_EVPN_ESI
,
5585 "%u: ESI hash entry for ESI %s at Local ES DEL",
5586 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5590 /* Delete all local EVPN ES routes from ESI table
5591 * and schedule for processing (to withdraw from peers))
5593 delete_routes_for_es(bgp
, es
);
5595 /* free the hash entry */
5596 bgp_evpn_es_free(bgp
, es
);
5602 * bgp_evpn_local_es_add
5604 int bgp_evpn_local_es_add(struct bgp
*bgp
,
5606 struct ipaddr
*originator_ip
)
5608 char buf
[ESI_STR_LEN
];
5609 struct evpnes
*es
= NULL
;
5610 struct prefix_evpn p
;
5612 if (!bgp
->esihash
) {
5613 flog_err(EC_BGP_ES_CREATE
, "%u: ESI hash not yet created",
5618 /* create the new es */
5619 es
= bgp_evpn_lookup_es(bgp
, esi
);
5621 es
= bgp_evpn_es_new(bgp
, esi
, originator_ip
);
5625 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
5626 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5630 UNSET_FLAG(es
->flags
, EVPNES_REMOTE
);
5631 SET_FLAG(es
->flags
, EVPNES_LOCAL
);
5633 build_evpn_type4_prefix(&p
, esi
, originator_ip
->ipaddr_v4
);
5634 if (update_evpn_type4_route(bgp
, es
, &p
)) {
5635 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5636 "%u: Type4 route creation failure for ESI %s",
5637 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5641 /* import all remote ES routes in th ES table */
5642 install_routes_for_es(bgp
, es
);
5648 * Handle change in setting for BUM handling. The supported values
5649 * are head-end replication and dropping all BUM packets. Any change
5650 * should be registered with zebra. Also, if doing head-end replication,
5651 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5652 * to be dropped, the RT-3s must be withdrawn.
5654 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
5656 zlog_info("L2VPN EVPN BUM handling is %s",
5657 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
5658 "Flooding" : "Flooding Disabled");
5660 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
5661 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
5662 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
5663 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
5664 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
5668 * Cleanup EVPN information on disable - Need to delete and withdraw
5669 * EVPN routes from peers.
5671 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5673 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
5674 void *))cleanup_vni_on_disable
,
5679 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5680 * BGP instance (default) is being freed.
5682 void bgp_evpn_cleanup(struct bgp
*bgp
)
5684 hash_iterate(bgp
->vnihash
,
5685 (void (*)(struct hash_backet
*, void *))free_vni_entry
,
5688 hash_free(bgp
->import_rt_hash
);
5689 bgp
->import_rt_hash
= NULL
;
5691 hash_free(bgp
->vrf_import_rt_hash
);
5692 bgp
->vrf_import_rt_hash
= NULL
;
5694 hash_free(bgp
->vnihash
);
5695 bgp
->vnihash
= NULL
;
5697 hash_free(bgp
->esihash
);
5698 bgp
->esihash
= NULL
;
5700 list_delete(&bgp
->vrf_import_rtl
);
5701 list_delete(&bgp
->vrf_export_rtl
);
5702 list_delete(&bgp
->l2vnis
);
5706 * Initialization for EVPN
5709 * hash for RT to VNI
5711 void bgp_evpn_init(struct bgp
*bgp
)
5714 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
5716 hash_create(esi_hash_keymake
, esi_cmp
,
5717 "BGP EVPN Local ESI Hash");
5718 bgp
->import_rt_hash
=
5719 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
5720 "BGP Import RT Hash");
5721 bgp
->vrf_import_rt_hash
=
5722 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
5723 "BGP VRF Import RT Hash");
5724 bgp
->vrf_import_rtl
= list_new();
5725 bgp
->vrf_import_rtl
->cmp
=
5726 (int (*)(void *, void *))evpn_route_target_cmp
;
5727 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
5728 bgp
->vrf_export_rtl
= list_new();
5729 bgp
->vrf_export_rtl
->cmp
=
5730 (int (*)(void *, void *))evpn_route_target_cmp
;
5731 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
5732 bgp
->l2vnis
= list_new();
5733 bgp
->l2vnis
->cmp
= vni_list_cmp
;
5735 /* Default BUM handling is to do head-end replication. */
5736 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
5739 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
5741 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);