1 /* Ethernet-VPN Packet and vty Processing File
2 * Copyright (C) 2016 6WIND
3 * Copyright (C) 2017 Cumulus Networks, Inc.
5 * This file is part of FRR.
7 * FRRouting is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * FRRouting is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "bgpd/bgp_attr_evpn.h"
35 #include "bgpd/bgpd.h"
36 #include "bgpd/bgp_table.h"
37 #include "bgpd/bgp_route.h"
38 #include "bgpd/bgp_attr.h"
39 #include "bgpd/bgp_mplsvpn.h"
40 #include "bgpd/bgp_label.h"
41 #include "bgpd/bgp_evpn.h"
42 #include "bgpd/bgp_evpn_private.h"
43 #include "bgpd/bgp_ecommunity.h"
44 #include "bgpd/bgp_encap_types.h"
45 #include "bgpd/bgp_debug.h"
46 #include "bgpd/bgp_errors.h"
47 #include "bgpd/bgp_aspath.h"
48 #include "bgpd/bgp_zebra.h"
49 #include "bgpd/bgp_nexthop.h"
52 * Definitions and external declarations.
54 extern struct zclient
*zclient
;
56 DEFINE_QOBJ_TYPE(bgpevpn
)
57 DEFINE_QOBJ_TYPE(evpnes
)
61 * Static function declarations
63 static void delete_evpn_route_entry(struct bgp
*bgp
,
64 afi_t afi
, safi_t safi
,
66 struct bgp_info
**ri
);
67 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
73 /* compare two IPV4 VTEP IPs */
74 static int evpn_vtep_ip_cmp(const void *p1
, const void *p2
)
76 const struct in_addr
*ip1
= p1
;
77 const struct in_addr
*ip2
= p2
;
83 return (ip1
->s_addr
== ip2
->s_addr
);
87 * Make hash key for ESI.
89 static unsigned int esi_hash_keymake(void *p
)
91 struct evpnes
*pes
= p
;
92 const void *pnt
= (void *)pes
->esi
.val
;
94 return jhash(pnt
, ESI_BYTES
, 0xa5a5a55a);
100 static int esi_cmp(const void *p1
, const void *p2
)
102 const struct evpnes
*pes1
= p1
;
103 const struct evpnes
*pes2
= p2
;
105 if (pes1
== NULL
&& pes2
== NULL
)
108 if (pes1
== NULL
|| pes2
== NULL
)
111 return (memcmp(pes1
->esi
.val
, pes2
->esi
.val
, ESI_BYTES
) == 0);
117 static unsigned int vni_hash_key_make(void *p
)
119 struct bgpevpn
*vpn
= p
;
120 return (jhash_1word(vpn
->vni
, 0));
124 * Comparison function for vni hash
126 static int vni_hash_cmp(const void *p1
, const void *p2
)
128 const struct bgpevpn
*vpn1
= p1
;
129 const struct bgpevpn
*vpn2
= p2
;
135 return (vpn1
->vni
== vpn2
->vni
);
139 * Make vrf import route target hash key.
141 static unsigned int vrf_import_rt_hash_key_make(void *p
)
143 struct vrf_irt_node
*irt
= p
;
144 char *pnt
= irt
->rt
.val
;
146 return jhash(pnt
, 8, 0x5abc1234);
150 * Comparison function for vrf import rt hash
152 static int vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
154 const struct vrf_irt_node
*irt1
= p1
;
155 const struct vrf_irt_node
*irt2
= p2
;
157 if (irt1
== NULL
&& irt2
== NULL
)
160 if (irt1
== NULL
|| irt2
== NULL
)
163 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
167 * Create a new vrf import_rt in default instance
169 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
171 struct bgp
*bgp_def
= NULL
;
172 struct vrf_irt_node
*irt
;
174 bgp_def
= bgp_get_default();
176 flog_err(BGP_ERR_NO_DFLT
,
177 "vrf import rt new - def instance not created yet");
181 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
182 sizeof(struct vrf_irt_node
));
187 irt
->vrfs
= list_new();
190 if (!hash_get(bgp_def
->vrf_import_rt_hash
, irt
, hash_alloc_intern
)) {
191 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
199 * Free the vrf import rt node
201 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
203 struct bgp
*bgp_def
= NULL
;
205 bgp_def
= bgp_get_default();
207 flog_err(BGP_ERR_NO_DFLT
,
208 "vrf import rt free - def instance not created yet");
212 hash_release(bgp_def
->vrf_import_rt_hash
, irt
);
213 list_delete_and_null(&irt
->vrfs
);
214 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
218 * Function to lookup Import RT node - used to map a RT to set of
219 * VNIs importing routes with that RT.
221 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
223 struct bgp
*bgp_def
= NULL
;
224 struct vrf_irt_node
*irt
;
225 struct vrf_irt_node tmp
;
227 bgp_def
= bgp_get_default();
231 "vrf import rt lookup - def instance not created yet");
235 memset(&tmp
, 0, sizeof(struct vrf_irt_node
));
236 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
237 irt
= hash_lookup(bgp_def
->vrf_import_rt_hash
, &tmp
);
242 * Is specified VRF present on the RT's list of "importing" VRFs?
244 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
, struct bgp
*bgp_vrf
)
246 struct listnode
*node
= NULL
, *nnode
= NULL
;
247 struct bgp
*tmp_bgp_vrf
= NULL
;
249 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
250 if (tmp_bgp_vrf
== bgp_vrf
)
257 * Make import route target hash key.
259 static unsigned int import_rt_hash_key_make(void *p
)
261 struct irt_node
*irt
= p
;
262 char *pnt
= irt
->rt
.val
;
264 return jhash(pnt
, 8, 0xdeadbeef);
268 * Comparison function for import rt hash
270 static int import_rt_hash_cmp(const void *p1
, const void *p2
)
272 const struct irt_node
*irt1
= p1
;
273 const struct irt_node
*irt2
= p2
;
275 if (irt1
== NULL
&& irt2
== NULL
)
278 if (irt1
== NULL
|| irt2
== NULL
)
281 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
285 * Create a new import_rt
287 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
288 struct ecommunity_val
*rt
)
290 struct irt_node
*irt
;
295 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
300 irt
->vnis
= list_new();
303 if (!hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
)) {
304 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
312 * Free the import rt node
314 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
316 hash_release(bgp
->import_rt_hash
, irt
);
317 list_delete_and_null(&irt
->vnis
);
318 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
322 * Function to lookup Import RT node - used to map a RT to set of
323 * VNIs importing routes with that RT.
325 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
326 struct ecommunity_val
*rt
)
328 struct irt_node
*irt
;
331 memset(&tmp
, 0, sizeof(struct irt_node
));
332 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
333 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
338 * Is specified VNI present on the RT's list of "importing" VNIs?
340 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
342 struct listnode
*node
, *nnode
;
343 struct bgpevpn
*tmp_vpn
;
345 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
354 * Compare Route Targets.
356 static int evpn_route_target_cmp(struct ecommunity
*ecom1
,
357 struct ecommunity
*ecom2
)
365 if (!ecom1
&& !ecom2
)
368 if (ecom1
->str
&& !ecom2
->str
)
371 if (!ecom1
->str
&& ecom2
->str
)
374 if (!ecom1
->str
&& !ecom2
->str
)
377 return strcmp(ecom1
->str
, ecom2
->str
);
380 static void evpn_xxport_delete_ecomm(void *val
)
382 struct ecommunity
*ecomm
= val
;
383 ecommunity_free(&ecomm
);
387 * Mask off global-admin field of specified extended community (RT),
388 * just retain the local-admin field.
390 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
391 struct ecommunity_val
*src
)
397 if (type
== ECOMMUNITY_ENCODE_AS
) {
398 dst
->val
[2] = dst
->val
[3] = 0;
399 } else if (type
== ECOMMUNITY_ENCODE_AS4
400 || type
== ECOMMUNITY_ENCODE_IP
) {
401 dst
->val
[2] = dst
->val
[3] = 0;
402 dst
->val
[4] = dst
->val
[5] = 0;
407 * Map one RT to specified VRF.
408 * bgp_vrf = BGP vrf instance
410 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct ecommunity_val
*eval
)
412 struct vrf_irt_node
*irt
= NULL
;
413 struct ecommunity_val eval_tmp
;
415 /* If using "automatic" RT,
416 * we only care about the local-admin sub-field.
417 * This is to facilitate using L3VNI(VRF-VNI)
418 * as the RT for EBGP peering too.
420 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
421 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
422 mask_ecom_global_admin(&eval_tmp
, eval
);
424 irt
= lookup_vrf_import_rt(&eval_tmp
);
425 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
426 /* Already mapped. */
430 irt
= vrf_import_rt_new(&eval_tmp
);
432 /* Add VRF to the list for this RT. */
433 listnode_add(irt
->vrfs
, bgp_vrf
);
437 * Unmap specified VRF from specified RT. If there are no other
438 * VRFs for this RT, then the RT hash is deleted.
439 * bgp_vrf: BGP VRF specific instance
441 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
, struct vrf_irt_node
*irt
)
443 /* Delete VRF from list for this RT. */
444 listnode_delete(irt
->vrfs
, bgp_vrf
);
445 if (!listnode_head(irt
->vrfs
)) {
446 vrf_import_rt_free(irt
);
451 * Map one RT to specified VNI.
453 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
454 struct ecommunity_val
*eval
)
456 struct irt_node
*irt
;
457 struct ecommunity_val eval_tmp
;
459 /* If using "automatic" RT, we only care about the local-admin
461 * This is to facilitate using VNI as the RT for EBGP peering too.
463 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
464 if (!is_import_rt_configured(vpn
))
465 mask_ecom_global_admin(&eval_tmp
, eval
);
467 irt
= lookup_import_rt(bgp
, &eval_tmp
);
469 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
470 /* Already mapped. */
474 irt
= import_rt_new(bgp
, &eval_tmp
);
478 /* Add VNI to the hash list for this RT. */
479 listnode_add(irt
->vnis
, vpn
);
483 * Unmap specified VNI from specified RT. If there are no other
484 * VNIs for this RT, then the RT hash is deleted.
486 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
487 struct irt_node
*irt
)
489 /* Delete VNI from hash list for this RT. */
490 listnode_delete(irt
->vnis
, vpn
);
491 if (!listnode_head(irt
->vnis
)) {
492 import_rt_free(bgp
, irt
);
497 * Create RT extended community automatically from passed information:
498 * of the form AS:VNI.
499 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
500 * the need is to get a RT value that will be unique across different
501 * VNIs but the same across routers (in the same AS) for a particular
504 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
506 struct ecommunity_val eval
;
507 struct ecommunity
*ecomadd
;
509 if (bgp
->advertise_autort_rfc8365
)
510 vni
|= EVPN_AUTORT_VXLAN
;
511 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
513 ecomadd
= ecommunity_new();
514 ecommunity_add_val(ecomadd
, &eval
);
515 listnode_add_sort(rtl
, ecomadd
);
519 * Derive RD and RT for a VNI automatically. Invoked at the time of
522 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
524 bgp_evpn_derive_auto_rd(bgp
, vpn
);
525 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
526 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
530 * Convert nexthop (remote VTEP IP) into an IPv6 address.
532 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
534 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
536 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
537 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
541 * Add (update) or delete MACIP from zebra.
543 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
544 struct prefix_evpn
*p
,
545 struct in_addr remote_vtep_ip
, int add
,
550 char buf1
[ETHER_ADDR_STRLEN
];
551 char buf2
[INET6_ADDRSTRLEN
];
552 char buf3
[INET6_ADDRSTRLEN
];
555 if (!zclient
|| zclient
->sock
< 0)
558 /* Don't try to register if Zebra doesn't know of this instance. */
559 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
565 zclient_create_header(
566 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
568 stream_putl(s
, vpn
->vni
);
569 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
570 /* IP address length and IP address, if any. */
571 if (is_evpn_prefix_ipaddr_none(p
))
574 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
576 stream_putl(s
, ipa_len
);
577 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
579 stream_put_in_addr(s
, &remote_vtep_ip
);
581 /* TX flags - MAC sticky status and/or gateway mac */
583 stream_putc(s
, flags
);
585 stream_putw_at(s
, 0, stream_get_endp(s
));
587 if (bgp_debug_zebra(NULL
))
589 "Tx %s MACIP, VNI %u MAC %s IP %s (flags: 0x%x) remote VTEP %s",
590 add
? "ADD" : "DEL", vpn
->vni
,
591 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
593 ipaddr2str(&p
->prefix
.macip_addr
.ip
,
594 buf3
, sizeof(buf3
)), flags
,
595 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
598 return zclient_send_message(zclient
);
602 * Add (update) or delete remote VTEP from zebra.
604 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
605 struct prefix_evpn
*p
, int add
)
610 if (!zclient
|| zclient
->sock
< 0)
613 /* Don't try to register if Zebra doesn't know of this instance. */
614 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
620 zclient_create_header(
621 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
623 stream_putl(s
, vpn
->vni
);
624 if (is_evpn_prefix_ipaddr_v4(p
))
625 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
626 else if (is_evpn_prefix_ipaddr_v6(p
)) {
628 BGP_ERR_VTEP_INVALID
,
629 "Bad remote IP when trying to %s remote VTEP for VNI %u",
630 add
? "ADD" : "DEL", vpn
->vni
);
634 stream_putw_at(s
, 0, stream_get_endp(s
));
636 if (bgp_debug_zebra(NULL
))
637 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
638 add
? "ADD" : "DEL", vpn
->vni
,
639 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
641 return zclient_send_message(zclient
);
645 * Build extended community for EVPN ES (type-4) route
647 static void build_evpn_type4_route_extcomm(struct evpnes
*es
,
650 struct ecommunity ecom_encap
;
651 struct ecommunity ecom_es_rt
;
652 struct ecommunity_val eval
;
653 struct ecommunity_val eval_es_rt
;
654 bgp_encap_types tnl_type
;
658 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
659 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
660 encode_encap_extcomm(tnl_type
, &eval
);
662 ecom_encap
.val
= (uint8_t *)eval
.val
;
663 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
666 memset(&mac
, 0, sizeof(struct ethaddr
));
667 memset(&ecom_es_rt
, 0, sizeof(ecom_es_rt
));
668 es_get_system_mac(&es
->esi
, &mac
);
669 encode_es_rt_extcomm(&eval_es_rt
, &mac
);
671 ecom_es_rt
.val
= (uint8_t *)eval_es_rt
.val
;
673 ecommunity_merge(attr
->ecommunity
, &ecom_es_rt
);
675 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
679 * Build extended communities for EVPN prefix route.
681 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
684 struct ecommunity ecom_encap
;
685 struct ecommunity ecom_rmac
;
686 struct ecommunity_val eval
;
687 struct ecommunity_val eval_rmac
;
688 bgp_encap_types tnl_type
;
689 struct listnode
*node
, *nnode
;
690 struct ecommunity
*ecom
;
691 struct list
*vrf_export_rtl
= NULL
;
694 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
695 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
696 encode_encap_extcomm(tnl_type
, &eval
);
698 ecom_encap
.val
= (uint8_t *)eval
.val
;
701 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
703 /* Add the export RTs for L3VNI/VRF */
704 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
705 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
707 ecommunity_merge(attr
->ecommunity
, ecom
);
709 /* add the router mac extended community */
710 if (!is_zero_mac(&attr
->rmac
)) {
711 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
712 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
714 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
716 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
719 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
723 * Build extended communities for EVPN route.
724 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
725 * and ENCAP extended communities are applicable for all routes.
726 * The default gateway extended community and MAC mobility (sticky) extended
727 * community are added as needed based on passed settings - only for type-2
728 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
729 * added, if present, based on passed settings - only for non-link-local
732 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
735 struct ecommunity ecom_encap
;
736 struct ecommunity ecom_sticky
;
737 struct ecommunity ecom_default_gw
;
738 struct ecommunity ecom_rmac
;
739 struct ecommunity ecom_na
;
740 struct ecommunity_val eval
;
741 struct ecommunity_val eval_sticky
;
742 struct ecommunity_val eval_default_gw
;
743 struct ecommunity_val eval_rmac
;
744 struct ecommunity_val eval_na
;
746 bgp_encap_types tnl_type
;
747 struct listnode
*node
, *nnode
;
748 struct ecommunity
*ecom
;
750 struct list
*vrf_export_rtl
= NULL
;
753 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
754 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
755 encode_encap_extcomm(tnl_type
, &eval
);
757 ecom_encap
.val
= (uint8_t *)eval
.val
;
760 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
762 /* Add the export RTs for L2VNI */
763 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
764 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
766 /* Add the export RTs for L3VNI if told to - caller determines
767 * when this should be done.
770 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
771 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
772 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
774 attr
->ecommunity
= ecommunity_merge(
775 attr
->ecommunity
, ecom
);
779 /* Add MAC mobility (sticky) if needed. */
782 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
783 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
784 ecom_sticky
.size
= 1;
785 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
787 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
790 /* Add RMAC, if told to. */
792 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
793 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
795 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
797 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
800 /* Add default gateway, if needed. */
801 if (attr
->default_gw
) {
802 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
803 encode_default_gw_extcomm(&eval_default_gw
);
804 ecom_default_gw
.size
= 1;
805 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
807 ecommunity_merge(attr
->ecommunity
, &ecom_default_gw
);
810 if (attr
->router_flag
) {
811 memset(&ecom_na
, 0, sizeof(ecom_na
));
812 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
);
814 ecom_na
.val
= (uint8_t *)eval_na
.val
;
815 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
819 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
823 * Add MAC mobility extended community to attribute.
825 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
827 struct ecommunity ecom_tmp
;
828 struct ecommunity_val eval
;
829 uint8_t *ecom_val_ptr
;
836 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
838 /* Find current MM ecommunity */
841 if (attr
->ecommunity
) {
842 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
843 pnt
= attr
->ecommunity
->val
+ (i
* 8);
847 if (type
== ECOMMUNITY_ENCODE_EVPN
849 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
850 ecom_val_ptr
= (uint8_t *)(attr
->ecommunity
->val
857 /* Update the existing MM ecommunity */
859 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
861 /* Add MM to existing */
863 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
865 ecom_tmp
.val
= (uint8_t *)eval
.val
;
867 if (attr
->ecommunity
)
869 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
871 attr
->ecommunity
= ecommunity_dup(&ecom_tmp
);
875 /* Install EVPN route into zebra. */
876 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
877 struct prefix_evpn
*p
,
878 struct in_addr remote_vtep_ip
, uint8_t flags
)
882 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
883 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
886 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 1);
891 /* Uninstall EVPN route from zebra. */
892 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
893 struct prefix_evpn
*p
,
894 struct in_addr remote_vtep_ip
)
898 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
899 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
902 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 0);
908 * Due to MAC mobility, the prior "local" best route has been supplanted
909 * by a "remote" best route. The prior route has to be deleted and withdrawn
912 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
914 struct bgp_info
*old_local
)
916 struct bgp_node
*global_rn
;
918 afi_t afi
= AFI_L2VPN
;
919 safi_t safi
= SAFI_EVPN
;
921 /* Locate route node in the global EVPN routing table. Note that
922 * this table is a 2-level tree (RD-level + Prefix-level) similar to
925 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
926 (struct prefix
*)&rn
->p
, &vpn
->prd
);
928 /* Delete route entry in the global EVPN table. */
929 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &ri
);
931 /* Schedule for processing - withdraws to peers happen from
935 bgp_process(bgp
, global_rn
, afi
, safi
);
936 bgp_unlock_node(global_rn
);
939 /* Delete route entry in the VNI route table, caller to remove. */
940 bgp_info_delete(rn
, old_local
);
943 static struct in_addr
*es_vtep_new(struct in_addr vtep
)
947 ip
= XCALLOC(MTYPE_BGP_EVPN_ES_VTEP
, sizeof(struct in_addr
));
951 ip
->s_addr
= vtep
.s_addr
;
955 static void es_vtep_free(struct in_addr
*ip
)
957 XFREE(MTYPE_BGP_EVPN_ES_VTEP
, ip
);
960 /* check if VTEP is already part of the list */
961 static int is_vtep_present_in_list(struct list
*list
,
964 struct listnode
*node
= NULL
;
967 for (ALL_LIST_ELEMENTS_RO(list
, node
, tmp
)) {
968 if (tmp
->s_addr
== vtep
.s_addr
)
975 * Best path for ES route was changed,
976 * update the list of VTEPs for this ES
978 static int evpn_es_install_vtep(struct bgp
*bgp
,
980 struct prefix_evpn
*p
,
981 struct in_addr rvtep
)
983 struct in_addr
*vtep_ip
;
985 if (is_vtep_present_in_list(es
->vtep_list
, rvtep
))
989 vtep_ip
= es_vtep_new(rvtep
);
991 listnode_add_sort(es
->vtep_list
, vtep_ip
);
996 * Best path for ES route was changed,
997 * update the list of VTEPs for this ES
999 static int evpn_es_uninstall_vtep(struct bgp
*bgp
,
1001 struct prefix_evpn
*p
,
1002 struct in_addr rvtep
)
1004 struct listnode
*node
, *nnode
, *node_to_del
= NULL
;
1005 struct in_addr
*tmp
;
1007 for (ALL_LIST_ELEMENTS(es
->vtep_list
, node
, nnode
, tmp
)) {
1008 if (tmp
->s_addr
== rvtep
.s_addr
) {
1015 list_delete_node(es
->vtep_list
, node_to_del
);
1021 * Calculate the best path for a ES(type-4) route.
1023 static int evpn_es_route_select_install(struct bgp
*bgp
,
1025 struct bgp_node
*rn
)
1028 afi_t afi
= AFI_L2VPN
;
1029 safi_t safi
= SAFI_EVPN
;
1030 struct bgp_info
*old_select
; /* old best */
1031 struct bgp_info
*new_select
; /* new best */
1032 struct bgp_info_pair old_and_new
;
1034 /* Compute the best path. */
1035 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
],
1036 &old_and_new
, afi
, safi
);
1037 old_select
= old_and_new
.old
;
1038 new_select
= old_and_new
.new;
1041 * If the best path hasn't changed - see if something needs to be
1044 if (old_select
&& old_select
== new_select
1045 && old_select
->type
== ZEBRA_ROUTE_BGP
1046 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1047 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1048 && !CHECK_FLAG(old_select
->flags
, BGP_INFO_ATTR_CHANGED
)
1049 && !bgp
->addpath_tx_used
[afi
][safi
]) {
1050 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1051 ret
= evpn_es_install_vtep(bgp
, es
,
1052 (struct prefix_evpn
*)&rn
->p
,
1053 old_select
->attr
->nexthop
);
1055 UNSET_FLAG(old_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
1056 bgp_zebra_clear_route_change_flags(rn
);
1060 /* If the user did a "clear" this flag will be set */
1061 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1064 * bestpath has changed; update relevant fields and install or uninstall
1065 * into the zebra RIB.
1067 if (old_select
|| new_select
)
1068 bgp_bump_version(rn
);
1071 bgp_info_unset_flag(rn
, old_select
, BGP_INFO_SELECTED
);
1073 bgp_info_set_flag(rn
, new_select
, BGP_INFO_SELECTED
);
1074 bgp_info_unset_flag(rn
, new_select
, BGP_INFO_ATTR_CHANGED
);
1075 UNSET_FLAG(new_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
1078 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1079 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1080 ret
= evpn_es_install_vtep(bgp
, es
,
1081 (struct prefix_evpn
*)&rn
->p
,
1082 new_select
->attr
->nexthop
);
1084 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1085 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1086 ret
= evpn_es_uninstall_vtep(
1087 bgp
, es
, (struct prefix_evpn
*)&rn
->p
,
1088 old_select
->attr
->nexthop
);
1091 /* Clear any route change flags. */
1092 bgp_zebra_clear_route_change_flags(rn
);
1094 /* Reap old select bgp_info, if it has been removed */
1095 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_INFO_REMOVED
))
1096 bgp_info_reap(rn
, old_select
);
1102 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1105 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1106 struct bgp_node
*rn
)
1108 struct bgp_info
*old_select
, *new_select
;
1109 struct bgp_info_pair old_and_new
;
1110 struct prefix_evpn
*evp
;
1111 afi_t afi
= AFI_L2VPN
;
1112 safi_t safi
= SAFI_EVPN
;
1116 /* Compute the best path. */
1117 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1119 old_select
= old_and_new
.old
;
1120 new_select
= old_and_new
.new;
1122 evp
= (struct prefix_evpn
*)&rn
->p
;
1123 /* If the best path hasn't changed - see if there is still something to
1127 if (old_select
&& old_select
== new_select
1128 && old_select
->type
== ZEBRA_ROUTE_BGP
1129 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1130 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1131 && !CHECK_FLAG(old_select
->flags
, BGP_INFO_ATTR_CHANGED
)
1132 && !bgp
->addpath_tx_used
[afi
][safi
]) {
1133 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1134 if (old_select
->attr
->sticky
)
1135 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
1136 if (old_select
->attr
->default_gw
)
1137 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
1138 if (is_evpn_prefix_ipaddr_v6(evp
) &&
1139 old_select
->attr
->router_flag
)
1140 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
1142 ret
= evpn_zebra_install(
1143 bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1144 old_select
->attr
->nexthop
, flags
);
1146 UNSET_FLAG(old_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
1147 bgp_zebra_clear_route_change_flags(rn
);
1151 /* If the user did a "clear" this flag will be set */
1152 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1154 /* bestpath has changed; update relevant fields and install or uninstall
1155 * into the zebra RIB.
1157 if (old_select
|| new_select
)
1158 bgp_bump_version(rn
);
1161 bgp_info_unset_flag(rn
, old_select
, BGP_INFO_SELECTED
);
1163 bgp_info_set_flag(rn
, new_select
, BGP_INFO_SELECTED
);
1164 bgp_info_unset_flag(rn
, new_select
, BGP_INFO_ATTR_CHANGED
);
1165 UNSET_FLAG(new_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
1168 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1169 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1171 if (new_select
->attr
->sticky
)
1172 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
1173 if (new_select
->attr
->default_gw
)
1174 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
1175 if (is_evpn_prefix_ipaddr_v6(evp
) &&
1176 new_select
->attr
->router_flag
)
1177 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
1179 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1180 new_select
->attr
->nexthop
, flags
);
1181 /* If an old best existed and it was a "local" route, the only
1183 * it would be supplanted is due to MAC mobility procedures. So,
1185 * need to do an implicit delete and withdraw that route from
1188 if (old_select
&& old_select
->peer
== bgp
->peer_self
1189 && old_select
->type
== ZEBRA_ROUTE_BGP
1190 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
1191 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
1193 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1194 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1195 ret
= evpn_zebra_uninstall(bgp
, vpn
,
1196 (struct prefix_evpn
*)&rn
->p
,
1197 old_select
->attr
->nexthop
);
1200 /* Clear any route change flags. */
1201 bgp_zebra_clear_route_change_flags(rn
);
1203 /* Reap old select bgp_info, if it has been removed */
1204 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_INFO_REMOVED
))
1205 bgp_info_reap(rn
, old_select
);
1211 * Return true if the local ri for this rn is of type gateway mac
1213 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
1215 struct bgp_info
*tmp_ri
= NULL
;
1216 struct bgp_info
*local_ri
= NULL
;
1219 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1220 if (tmp_ri
->peer
== bgp
->peer_self
1221 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1222 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1229 return local_ri
->attr
->default_gw
;
1234 * Return true if the local ri for this rn has sticky set
1236 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
1238 struct bgp_info
*tmp_ri
;
1239 struct bgp_info
*local_ri
;
1242 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1243 if (tmp_ri
->peer
== bgp
->peer_self
1244 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1245 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1252 return local_ri
->attr
->sticky
;
1256 * create or update EVPN type4 route entry.
1257 * This could be in the ES table or the global table.
1258 * TODO: handle remote ES (type4) routes as well
1260 static int update_evpn_type4_route_entry(struct bgp
*bgp
,
1262 afi_t afi
, safi_t safi
,
1263 struct bgp_node
*rn
,
1266 struct bgp_info
**ri
,
1269 char buf
[ESI_STR_LEN
];
1270 char buf1
[INET6_ADDRSTRLEN
];
1271 struct bgp_info
*tmp_ri
= NULL
;
1272 struct bgp_info
*local_ri
= NULL
; /* local route entry if any */
1273 struct bgp_info
*remote_ri
= NULL
; /* remote route entry if any */
1274 struct attr
*attr_new
= NULL
;
1275 struct prefix_evpn
*evp
= NULL
;
1279 evp
= (struct prefix_evpn
*)&rn
->p
;
1281 /* locate the local and remote entries if any */
1282 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1283 if (tmp_ri
->peer
== bgp
->peer_self
&&
1284 tmp_ri
->type
== ZEBRA_ROUTE_BGP
&&
1285 tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1287 if (tmp_ri
->type
== ZEBRA_ROUTE_BGP
&&
1288 tmp_ri
->sub_type
== BGP_ROUTE_IMPORTED
&&
1289 CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
))
1293 /* we don't expect to see a remote_ri at this point.
1294 * An ES route has esi + vtep_ip as the key,
1295 * We shouldn't see the same route from any other vtep.
1300 "%u ERROR: local es route for ESI: %s Vtep %s also learnt from remote",
1302 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1303 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1307 if (!local_ri
&& !add
)
1310 /* create or update the entry */
1313 /* Add or update attribute to hash */
1314 attr_new
= bgp_attr_intern(attr
);
1316 /* Create new route with its attribute. */
1317 tmp_ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
,
1318 0, bgp
->peer_self
, attr_new
, rn
);
1319 SET_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
);
1321 /* add the newly created path to the route-node */
1322 bgp_info_add(rn
, tmp_ri
);
1325 if (attrhash_cmp(tmp_ri
->attr
, attr
)
1326 && !CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1329 /* The attribute has changed.
1330 * Add (or update) attribute to hash. */
1331 attr_new
= bgp_attr_intern(attr
);
1332 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1334 /* Restore route, if needed. */
1335 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1336 bgp_info_restore(rn
, tmp_ri
);
1338 /* Unintern existing, set to new. */
1339 bgp_attr_unintern(&tmp_ri
->attr
);
1340 tmp_ri
->attr
= attr_new
;
1341 tmp_ri
->uptime
= bgp_clock();
1345 /* Return back the route entry. */
1350 /* update evpn es (type-4) route */
1351 static int update_evpn_type4_route(struct bgp
*bgp
,
1353 struct prefix_evpn
*p
)
1356 int route_changed
= 0;
1357 char buf
[ESI_STR_LEN
];
1358 char buf1
[INET6_ADDRSTRLEN
];
1359 afi_t afi
= AFI_L2VPN
;
1360 safi_t safi
= SAFI_EVPN
;
1362 struct attr
*attr_new
= NULL
;
1363 struct bgp_node
*rn
= NULL
;
1364 struct bgp_info
*ri
= NULL
;
1366 memset(&attr
, 0, sizeof(struct attr
));
1368 /* Build path-attribute for this route. */
1369 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1370 attr
.nexthop
= es
->originator_ip
.ipaddr_v4
;
1371 attr
.mp_nexthop_global_in
= es
->originator_ip
.ipaddr_v4
;
1372 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1374 /* Set up extended community. */
1375 build_evpn_type4_route_extcomm(es
, &attr
);
1377 /* First, create (or fetch) route node within the ESI. */
1378 /* NOTE: There is no RD here. */
1379 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
1381 /* Create or update route entry. */
1382 ret
= update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
,
1388 "%u ERROR: Failed to updated ES route ESI: %s VTEP %s",
1390 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1391 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1395 attr_new
= ri
->attr
;
1397 /* Perform route selection;
1398 * this is just to set the flags correctly
1399 * as local route in the ES always wins.
1401 evpn_es_route_select_install(bgp
, es
, rn
);
1402 bgp_unlock_node(rn
);
1404 /* If this is a new route or some attribute has changed, export the
1405 * route to the global table. The route will be advertised to peers
1406 * from there. Note that this table is a 2-level tree (RD-level +
1407 * Prefix-level) similar to L3VPN routes.
1409 if (route_changed
) {
1410 struct bgp_info
*global_ri
;
1412 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1413 (struct prefix
*)p
, &es
->prd
);
1414 update_evpn_type4_route_entry(bgp
, es
, afi
, safi
,
1419 /* Schedule for processing and unlock node. */
1420 bgp_process(bgp
, rn
, afi
, safi
);
1421 bgp_unlock_node(rn
);
1424 /* Unintern temporary. */
1425 aspath_unintern(&attr
.aspath
);
1429 static int update_evpn_type5_route_entry(struct bgp
*bgp_def
,
1430 struct bgp
*bgp_vrf
, afi_t afi
,
1431 safi_t safi
, struct bgp_node
*rn
,
1432 struct attr
*attr
, int *route_changed
)
1434 struct attr
*attr_new
= NULL
;
1435 struct bgp_info
*ri
= NULL
;
1436 mpls_label_t label
= MPLS_INVALID_LABEL
;
1437 struct bgp_info
*local_ri
= NULL
;
1438 struct bgp_info
*tmp_ri
= NULL
;
1441 /* locate the local route entry if any */
1442 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1443 if (tmp_ri
->peer
== bgp_def
->peer_self
1444 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1445 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1450 * create a new route entry if one doesn't exist.
1451 * Otherwise see if route attr has changed
1455 /* route has changed as this is the first entry */
1458 /* Add (or update) attribute to hash. */
1459 attr_new
= bgp_attr_intern(attr
);
1461 /* create the route info from attribute */
1462 ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1463 bgp_def
->peer_self
, attr_new
, rn
);
1464 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1466 /* Type-5 routes advertise the L3-VNI */
1467 bgp_info_extra_get(ri
);
1468 vni2label(bgp_vrf
->l3vni
, &label
);
1469 memcpy(&ri
->extra
->label
, &label
, sizeof(label
));
1470 ri
->extra
->num_labels
= 1;
1472 /* add the route entry to route node*/
1473 bgp_info_add(rn
, ri
);
1477 if (!attrhash_cmp(tmp_ri
->attr
, attr
)) {
1479 /* attribute changed */
1482 /* The attribute has changed. */
1483 /* Add (or update) attribute to hash. */
1484 attr_new
= bgp_attr_intern(attr
);
1485 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1487 /* Restore route, if needed. */
1488 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1489 bgp_info_restore(rn
, tmp_ri
);
1491 /* Unintern existing, set to new. */
1492 bgp_attr_unintern(&tmp_ri
->attr
);
1493 tmp_ri
->attr
= attr_new
;
1494 tmp_ri
->uptime
= bgp_clock();
1500 /* update evpn type-5 route entry */
1501 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1502 struct attr
*src_attr
)
1504 afi_t afi
= AFI_L2VPN
;
1505 safi_t safi
= SAFI_EVPN
;
1507 struct bgp_node
*rn
= NULL
;
1508 struct bgp
*bgp_def
= NULL
;
1509 int route_changed
= 0;
1511 bgp_def
= bgp_get_default();
1515 /* Build path attribute for this route - use the source attr, if
1516 * present, else treat as locally originated.
1519 bgp_attr_dup(&attr
, src_attr
);
1521 memset(&attr
, 0, sizeof(struct attr
));
1522 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1524 /* Set nexthop to ourselves and fill in the Router MAC. */
1525 attr
.nexthop
= bgp_vrf
->originator_ip
;
1526 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1527 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1528 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
1530 /* Setup RT and encap extended community */
1531 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1533 /* get the route node in global table */
1534 rn
= bgp_afi_node_get(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1535 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1538 /* create or update the route entry within the route node */
1539 update_evpn_type5_route_entry(bgp_def
, bgp_vrf
, afi
, safi
, rn
, &attr
,
1542 /* schedule for processing and unlock node */
1543 if (route_changed
) {
1544 bgp_process(bgp_def
, rn
, afi
, safi
);
1545 bgp_unlock_node(rn
);
1548 /* uninten temporary */
1550 aspath_unintern(&attr
.aspath
);
1555 * Create or update EVPN route entry. This could be in the VNI route table
1556 * or the global route table.
1558 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1559 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1560 struct attr
*attr
, int add
, int vni_table
,
1561 struct bgp_info
**ri
, uint8_t flags
)
1563 struct bgp_info
*tmp_ri
;
1564 struct bgp_info
*local_ri
, *remote_ri
;
1565 struct attr
*attr_new
;
1566 mpls_label_t label
[BGP_MAX_LABELS
];
1567 uint32_t num_labels
= 1;
1568 int route_change
= 1;
1570 struct prefix_evpn
*evp
;
1573 evp
= (struct prefix_evpn
*)&rn
->p
;
1574 memset(&label
, 0, sizeof(label
));
1576 /* See if this is an update of an existing route, or a new add. Also,
1577 * identify if already known from remote, and if so, the one with the
1578 * highest sequence number; this is only when adding to the VNI routing
1581 local_ri
= remote_ri
= NULL
;
1582 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1583 if (tmp_ri
->peer
== bgp
->peer_self
1584 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1585 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1588 if (tmp_ri
->type
== ZEBRA_ROUTE_BGP
1589 && tmp_ri
->sub_type
== BGP_ROUTE_IMPORTED
1590 && CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
)) {
1593 else if (mac_mobility_seqnum(tmp_ri
->attr
)
1594 > mac_mobility_seqnum(remote_ri
->attr
))
1600 /* If route doesn't exist already, create a new one, if told to.
1601 * Otherwise act based on whether the attributes of the route have
1604 if (!local_ri
&& !add
)
1608 /* When learnt locally for the first time but already known from
1609 * remote, we have to initiate appropriate MAC mobility steps.
1610 * This is applicable when updating the VNI routing table.
1611 * We need to skip mobility steps for g/w macs (local mac on g/w
1612 * SVI) advertised in EVPN.
1613 * This will ensure that local routes are preferred for g/w macs
1615 if (remote_ri
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
)) {
1616 uint32_t cur_seqnum
;
1618 /* Add MM extended community to route. */
1619 cur_seqnum
= mac_mobility_seqnum(remote_ri
->attr
);
1620 add_mac_mobility_to_attr(cur_seqnum
+ 1, attr
);
1623 /* Add (or update) attribute to hash. */
1624 attr_new
= bgp_attr_intern(attr
);
1626 /* Extract MAC mobility sequence number, if any. */
1627 attr_new
->mm_seqnum
=
1628 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1629 attr_new
->sticky
= sticky
;
1631 /* Create new route with its attribute. */
1632 tmp_ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1633 bgp
->peer_self
, attr_new
, rn
);
1634 SET_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
);
1635 bgp_info_extra_get(tmp_ri
);
1637 /* The VNI goes into the 'label' field of the route */
1638 vni2label(vpn
->vni
, &label
[0]);
1640 /* Type-2 routes may carry a second VNI - the L3-VNI.
1641 * Only attach second label if we are advertising two labels for
1644 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1645 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1648 l3vni
= bgpevpn_get_l3vni(vpn
);
1650 vni2label(l3vni
, &label
[1]);
1655 memcpy(&tmp_ri
->extra
->label
, label
, sizeof(label
));
1656 tmp_ri
->extra
->num_labels
= num_labels
;
1657 bgp_info_add(rn
, tmp_ri
);
1660 if (attrhash_cmp(tmp_ri
->attr
, attr
)
1661 && !CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1665 * The attributes have changed, type-2 routes needs to
1666 * be advertised with right labels.
1668 vni2label(vpn
->vni
, &label
[0]);
1669 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1670 && CHECK_FLAG(vpn
->flags
,
1671 VNI_FLAG_USE_TWO_LABELS
)) {
1674 l3vni
= bgpevpn_get_l3vni(vpn
);
1676 vni2label(l3vni
, &label
[1]);
1680 memcpy(&tmp_ri
->extra
->label
, label
, sizeof(label
));
1681 tmp_ri
->extra
->num_labels
= num_labels
;
1683 /* The attribute has changed. */
1684 /* Add (or update) attribute to hash. */
1685 attr_new
= bgp_attr_intern(attr
);
1686 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1688 /* Restore route, if needed. */
1689 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1690 bgp_info_restore(rn
, tmp_ri
);
1692 /* Unintern existing, set to new. */
1693 bgp_attr_unintern(&tmp_ri
->attr
);
1694 tmp_ri
->attr
= attr_new
;
1695 tmp_ri
->uptime
= bgp_clock();
1699 /* Return back the route entry. */
1701 return route_change
;
1705 * Create or update EVPN route (of type based on prefix) for specified VNI
1706 * and schedule for processing.
1708 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1709 struct prefix_evpn
*p
, uint8_t flags
)
1711 struct bgp_node
*rn
;
1713 struct attr
*attr_new
;
1714 int add_l3_ecomm
= 0;
1715 struct bgp_info
*ri
;
1716 afi_t afi
= AFI_L2VPN
;
1717 safi_t safi
= SAFI_EVPN
;
1720 memset(&attr
, 0, sizeof(struct attr
));
1722 /* Build path-attribute for this route. */
1723 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1724 attr
.nexthop
= vpn
->originator_ip
;
1725 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1726 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1727 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1728 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1729 attr
.router_flag
= CHECK_FLAG(flags
,
1730 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1732 /* PMSI is only needed for type-3 routes */
1733 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
)
1734 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1736 /* router mac is only needed for type-2 routes here. */
1737 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1738 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1739 vni2label(vpn
->vni
, &(attr
.label
));
1741 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1742 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1745 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1746 (is_evpn_prefix_ipaddr_v4(p
) ||
1747 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1748 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1749 bgpevpn_get_l3vni(vpn
))
1752 /* Set up extended community. */
1753 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1755 /* First, create (or fetch) route node within the VNI. */
1756 /* NOTE: There is no RD here. */
1757 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1759 /* Create or update route entry. */
1760 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1763 attr_new
= ri
->attr
;
1765 /* Perform route selection; this is just to set the flags correctly
1766 * as local route in the VNI always wins.
1768 evpn_route_select_install(bgp
, vpn
, rn
);
1769 bgp_unlock_node(rn
);
1771 /* If this is a new route or some attribute has changed, export the
1772 * route to the global table. The route will be advertised to peers
1773 * from there. Note that this table is a 2-level tree (RD-level +
1774 * Prefix-level) similar to L3VPN routes.
1777 struct bgp_info
*global_ri
;
1779 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1780 (struct prefix
*)p
, &vpn
->prd
);
1781 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1, 0,
1784 /* Schedule for processing and unlock node. */
1785 bgp_process(bgp
, rn
, afi
, safi
);
1786 bgp_unlock_node(rn
);
1789 /* Unintern temporary. */
1790 aspath_unintern(&attr
.aspath
);
1796 * Delete EVPN route entry.
1797 * The entry can be in ESI/VNI table or the global table.
1799 static void delete_evpn_route_entry(struct bgp
*bgp
,
1800 afi_t afi
, safi_t safi
,
1801 struct bgp_node
*rn
,
1802 struct bgp_info
**ri
)
1804 struct bgp_info
*tmp_ri
;
1808 /* Now, find matching route. */
1809 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
)
1810 if (tmp_ri
->peer
== bgp
->peer_self
1811 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1812 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1817 /* Mark route for delete. */
1819 bgp_info_delete(rn
, tmp_ri
);
1824 /* Delete EVPN ES (type-4) route */
1825 static int delete_evpn_type4_route(struct bgp
*bgp
,
1827 struct prefix_evpn
*p
)
1829 afi_t afi
= AFI_L2VPN
;
1830 safi_t safi
= SAFI_EVPN
;
1831 struct bgp_info
*ri
;
1832 struct bgp_node
*rn
= NULL
; /* rn in esi table */
1833 struct bgp_node
*global_rn
= NULL
; /* rn in global table */
1835 /* First, locate the route node within the ESI.
1836 * If it doesn't exist, ther is nothing to do.
1837 * Note: there is no RD here.
1839 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
1843 /* Next, locate route node in the global EVPN routing table.
1844 * Note that this table is a 2-level tree (RD-level + Prefix-level)
1846 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1847 (struct prefix
*)p
, &es
->prd
);
1850 /* Delete route entry in the global EVPN table. */
1851 delete_evpn_route_entry(bgp
, afi
, safi
,
1854 /* Schedule for processing - withdraws to peers happen from
1858 bgp_process(bgp
, global_rn
, afi
, safi
);
1859 bgp_unlock_node(global_rn
);
1863 * Delete route entry in the ESI route table.
1864 * This can just be removed.
1866 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &ri
);
1868 bgp_info_reap(rn
, ri
);
1869 bgp_unlock_node(rn
);
1873 /* Delete EVPN type5 route */
1874 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1876 afi_t afi
= AFI_L2VPN
;
1877 safi_t safi
= SAFI_EVPN
;
1878 struct bgp_node
*rn
= NULL
;
1879 struct bgp_info
*ri
= NULL
;
1880 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
1882 bgp_def
= bgp_get_default();
1886 /* locate the global route entry for this type-5 prefix */
1887 rn
= bgp_afi_node_lookup(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1888 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1892 delete_evpn_route_entry(bgp_def
, afi
, safi
, rn
, &ri
);
1894 bgp_process(bgp_def
, rn
, afi
, safi
);
1895 bgp_unlock_node(rn
);
1900 * Delete EVPN route (of type based on prefix) for specified VNI and
1901 * schedule for processing.
1903 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1904 struct prefix_evpn
*p
)
1906 struct bgp_node
*rn
, *global_rn
;
1907 struct bgp_info
*ri
;
1908 afi_t afi
= AFI_L2VPN
;
1909 safi_t safi
= SAFI_EVPN
;
1911 /* First, locate the route node within the VNI. If it doesn't exist,
1913 * is nothing further to do.
1915 /* NOTE: There is no RD here. */
1916 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1920 /* Next, locate route node in the global EVPN routing table. Note that
1921 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1924 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1925 (struct prefix
*)p
, &vpn
->prd
);
1927 /* Delete route entry in the global EVPN table. */
1928 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &ri
);
1930 /* Schedule for processing - withdraws to peers happen from
1934 bgp_process(bgp
, global_rn
, afi
, safi
);
1935 bgp_unlock_node(global_rn
);
1938 /* Delete route entry in the VNI route table. This can just be removed.
1940 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &ri
);
1942 bgp_info_reap(rn
, ri
);
1943 bgp_unlock_node(rn
);
1949 * Update all type-2 (MACIP) local routes for this VNI - these should also
1950 * be scheduled for advertise to peers.
1952 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1956 struct bgp_node
*rn
;
1957 struct bgp_info
*ri
;
1959 struct attr attr_sticky
;
1960 struct attr attr_def_gw
;
1961 struct attr attr_ip6_ll
;
1962 struct attr
*attr_new
;
1963 int add_l3_ecomm
= 0;
1967 memset(&attr
, 0, sizeof(struct attr
));
1968 memset(&attr_sticky
, 0, sizeof(struct attr
));
1969 memset(&attr_def_gw
, 0, sizeof(struct attr
));
1970 memset(&attr_ip6_ll
, 0, sizeof(struct attr
));
1972 /* Build path-attribute - multiple type-2 routes for this VNI will share
1973 * the same path attribute, but we need separate structures for sticky
1974 * MACs, default gateway and IPv6 link-local addresses (no L3 RT/RMAC).
1976 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1977 bgp_attr_default_set(&attr_sticky
, BGP_ORIGIN_IGP
);
1978 bgp_attr_default_set(&attr_def_gw
, BGP_ORIGIN_IGP
);
1979 attr
.nexthop
= vpn
->originator_ip
;
1980 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1981 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1982 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1983 attr_sticky
.nexthop
= vpn
->originator_ip
;
1984 attr_sticky
.mp_nexthop_global_in
= vpn
->originator_ip
;
1985 attr_sticky
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1986 attr_sticky
.sticky
= 1;
1987 bgpevpn_get_rmac(vpn
, &attr_sticky
.rmac
);
1988 attr_def_gw
.nexthop
= vpn
->originator_ip
;
1989 attr_def_gw
.mp_nexthop_global_in
= vpn
->originator_ip
;
1990 attr_def_gw
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1991 attr_def_gw
.default_gw
= 1;
1992 bgpevpn_get_rmac(vpn
, &attr_def_gw
.rmac
);
1993 bgp_attr_default_set(&attr_ip6_ll
, BGP_ORIGIN_IGP
);
1994 attr_ip6_ll
.nexthop
= vpn
->originator_ip
;
1995 attr_ip6_ll
.mp_nexthop_global_in
= vpn
->originator_ip
;
1996 attr_ip6_ll
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1998 /* Add L3 VNI RTs and RMAC for non IPv6 link-local attributes if
1999 * using L3 VNI for type-2 routes also.
2001 if (CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
2002 bgpevpn_get_l3vni(vpn
))
2005 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2006 build_evpn_route_extcomm(vpn
, &attr_sticky
, add_l3_ecomm
);
2007 build_evpn_route_extcomm(vpn
, &attr_def_gw
, add_l3_ecomm
);
2008 build_evpn_route_extcomm(vpn
, &attr_ip6_ll
, 0);
2010 /* Walk this VNI's route table and update local type-2 routes. For any
2011 * routes updated, update corresponding entry in the global table too.
2013 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2014 rn
= bgp_route_next(rn
)) {
2015 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2016 struct bgp_node
*rd_rn
;
2017 struct bgp_info
*global_ri
;
2019 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2022 if (is_evpn_prefix_ipaddr_v6(evp
) &&
2023 IN6_IS_ADDR_LINKLOCAL(&evp
->prefix
.macip_addr
.ip
.ipaddr_v6
))
2024 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
2025 &attr_ip6_ll
, 0, 1, &ri
, 0);
2027 if (evpn_route_is_sticky(bgp
, rn
))
2028 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
2029 &attr_sticky
, 0, 1, &ri
,
2031 else if (evpn_route_is_def_gw(bgp
, rn
)) {
2032 if (is_evpn_prefix_ipaddr_v6(evp
))
2033 attr_def_gw
.router_flag
= 1;
2034 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
2035 &attr_def_gw
, 0, 1, &ri
,
2038 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
2039 &attr
, 0, 1, &ri
, 0);
2042 /* If a local route exists for this prefix, we need to update
2043 * the global routing table too.
2048 /* Perform route selection; this is just to set the flags
2050 * as local route in the VNI always wins.
2052 evpn_route_select_install(bgp
, vpn
, rn
);
2054 attr_new
= ri
->attr
;
2056 /* Update route in global routing table. */
2057 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2058 (struct prefix
*)evp
, &vpn
->prd
);
2060 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
2063 /* Schedule for processing and unlock node. */
2064 bgp_process(bgp
, rd_rn
, afi
, safi
);
2065 bgp_unlock_node(rd_rn
);
2068 /* Unintern temporary. */
2069 aspath_unintern(&attr
.aspath
);
2070 aspath_unintern(&attr_sticky
.aspath
);
2071 aspath_unintern(&attr_def_gw
.aspath
);
2072 aspath_unintern(&attr_ip6_ll
.aspath
);
2078 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2079 * global routing table. These are also scheduled for withdraw from peers.
2081 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2085 struct bgp_node
*rdrn
, *rn
;
2086 struct bgp_table
*table
;
2087 struct bgp_info
*ri
;
2092 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
2093 if (rdrn
&& rdrn
->info
) {
2094 table
= (struct bgp_table
*)rdrn
->info
;
2095 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2096 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2098 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2101 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &ri
);
2103 bgp_process(bgp
, rn
, afi
, safi
);
2107 /* Unlock RD node. */
2109 bgp_unlock_node(rdrn
);
2115 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2116 * table as well as the per-VNI route table.
2118 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2122 struct bgp_node
*rn
;
2123 struct bgp_info
*ri
;
2128 /* First, walk the global route table for this VNI's type-2 local
2130 * EVPN routes are a 2-level table, first get the RD table.
2132 delete_global_type2_routes(bgp
, vpn
);
2134 /* Next, walk this VNI's route table and delete local type-2 routes. */
2135 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2136 rn
= bgp_route_next(rn
)) {
2137 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2139 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2142 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &ri
);
2144 /* Route entry in local table gets deleted immediately. */
2146 bgp_info_reap(rn
, ri
);
2153 * Delete all routes in per ES route-table
2155 static int delete_all_es_routes(struct bgp
*bgp
, struct evpnes
*es
)
2157 struct bgp_node
*rn
;
2158 struct bgp_info
*ri
, *nextri
;
2160 /* Walk this ES's route table and delete all routes. */
2161 for (rn
= bgp_table_top(es
->route_table
); rn
;
2162 rn
= bgp_route_next(rn
)) {
2163 for (ri
= rn
->info
; (ri
!= NULL
) && (nextri
= ri
->next
, 1);
2165 bgp_info_delete(rn
, ri
);
2166 bgp_info_reap(rn
, ri
);
2174 * Delete all routes in the per-VNI route table.
2176 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2178 struct bgp_node
*rn
;
2179 struct bgp_info
*ri
, *nextri
;
2181 /* Walk this VNI's route table and delete all routes. */
2182 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2183 rn
= bgp_route_next(rn
)) {
2184 for (ri
= rn
->info
; (ri
!= NULL
) && (nextri
= ri
->next
, 1);
2186 bgp_info_delete(rn
, ri
);
2187 bgp_info_reap(rn
, ri
);
2195 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2196 * export RT getting modified or change to tunnel IP. Note that these
2197 * situations need the route in the per-VNI table as well as the global
2198 * table to be updated (as attributes change).
2200 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2203 struct prefix_evpn p
;
2205 /* Update and advertise the type-3 route (only one) followed by the
2206 * locally learnt type-2 routes (MACIP) - for this VNI.
2208 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2209 ret
= update_evpn_route(bgp
, vpn
, &p
, 0);
2213 return update_all_type2_routes(bgp
, vpn
);
2216 /* Delete (and withdraw) local routes for specified ES from global and ES table.
2217 * Also remove all other routes from the per ES table.
2218 * Invoked when ES is deleted.
2220 static int delete_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
2223 char buf
[ESI_STR_LEN
];
2224 struct prefix_evpn p
;
2226 /* Delete and withdraw locally learnt ES route */
2227 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
.ipaddr_v4
);
2228 ret
= delete_evpn_type4_route(bgp
, es
, &p
);
2230 flog_err(BGP_ERR_EVPN_ROUTE_DELETE
,
2231 "%u failed to delete type-4 route for ESI %s",
2232 bgp
->vrf_id
, esi_to_str(&es
->esi
, buf
, sizeof(buf
)));
2235 /* Delete all routes from per ES table */
2236 return delete_all_es_routes(bgp
, es
);
2240 * Delete (and withdraw) local routes for specified VNI from the global
2241 * table and per-VNI table. After this, remove all other routes from
2242 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2243 * (advertise-all-vni) being disabled.
2245 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2248 struct prefix_evpn p
;
2250 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2251 * followed by type-3 routes (only one) - for this VNI.
2253 ret
= delete_all_type2_routes(bgp
, vpn
);
2257 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2258 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2262 /* Delete all routes from the per-VNI table. */
2263 return delete_all_vni_routes(bgp
, vpn
);
2267 * There is a tunnel endpoint IP address change for this VNI, delete
2268 * prior type-3 route (if needed) and update.
2269 * Note: Route re-advertisement happens elsewhere after other processing
2272 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2273 struct in_addr originator_ip
)
2275 struct prefix_evpn p
;
2277 /* If VNI is not live, we only need to update the originator ip */
2278 if (!is_vni_live(vpn
)) {
2279 vpn
->originator_ip
= originator_ip
;
2283 /* Update the tunnel-ip hash */
2284 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2285 bgp_tip_add(bgp
, &originator_ip
);
2287 /* filter routes as martian nexthop db has changed */
2288 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2290 /* Need to withdraw type-3 route as the originator IP is part
2293 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2294 delete_evpn_route(bgp
, vpn
, &p
);
2296 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2297 vpn
->originator_ip
= originator_ip
;
2301 /* Install EVPN route entry in ES */
2302 static int install_evpn_route_entry_in_es(struct bgp
*bgp
,
2304 struct prefix_evpn
*p
,
2305 struct bgp_info
*parent_ri
)
2308 struct bgp_node
*rn
= NULL
;
2309 struct bgp_info
*ri
= NULL
;
2310 struct attr
*attr_new
= NULL
;
2312 /* Create (or fetch) route within the VNI.
2313 * NOTE: There is no RD here.
2315 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
2317 /* Check if route entry is already present. */
2318 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2320 (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2324 /* Add (or update) attribute to hash. */
2325 attr_new
= bgp_attr_intern(parent_ri
->attr
);
2327 /* Create new route with its attribute. */
2328 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
2329 parent_ri
->peer
, attr_new
, rn
);
2330 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
2331 bgp_info_extra_get(ri
);
2332 ri
->extra
->parent
= parent_ri
;
2333 bgp_info_add(rn
, ri
);
2335 if (attrhash_cmp(ri
->attr
, parent_ri
->attr
)
2336 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
2337 bgp_unlock_node(rn
);
2340 /* The attribute has changed. */
2341 /* Add (or update) attribute to hash. */
2342 attr_new
= bgp_attr_intern(parent_ri
->attr
);
2344 /* Restore route, if needed. */
2345 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
2346 bgp_info_restore(rn
, ri
);
2348 /* Mark if nexthop has changed. */
2349 if (!IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
))
2350 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
2352 /* Unintern existing, set to new. */
2353 bgp_attr_unintern(&ri
->attr
);
2354 ri
->attr
= attr_new
;
2355 ri
->uptime
= bgp_clock();
2358 /* Perform route selection and update zebra, if required. */
2359 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2364 * Install route entry into the VRF routing table and invoke route selection.
2366 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2367 struct prefix_evpn
*evp
,
2368 struct bgp_info
*parent_ri
)
2370 struct bgp_node
*rn
;
2371 struct bgp_info
*ri
;
2373 struct attr
*attr_new
;
2376 struct prefix
*pp
= &p
;
2379 char buf
[PREFIX_STRLEN
];
2380 char buf1
[PREFIX_STRLEN
];
2382 memset(pp
, 0, sizeof(struct prefix
));
2383 ip_prefix_from_evpn_prefix(evp
, pp
);
2385 if (bgp_debug_zebra(NULL
)) {
2387 "installing evpn prefix %s as ip prefix %s in vrf %s",
2388 prefix2str(evp
, buf
, sizeof(buf
)),
2389 prefix2str(pp
, buf1
, sizeof(buf
)),
2390 vrf_id_to_name(bgp_vrf
->vrf_id
));
2393 /* Create (or fetch) route within the VRF. */
2394 /* NOTE: There is no RD here. */
2395 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2397 safi
= SAFI_UNICAST
;
2398 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2399 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2401 safi
= SAFI_UNICAST
;
2402 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2406 /* EVPN routes currently only support a IPv4 next hop which corresponds
2407 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2408 * or prefix route, we have to convert the next hop to an IPv4-mapped
2409 * address for the rest of the code to flow through. In the case of IPv4,
2410 * make sure to set the flag for next hop attribute.
2412 bgp_attr_dup(&attr
, parent_ri
->attr
);
2414 evpn_convert_nexthop_to_ipv6(&attr
);
2416 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2418 /* Check if route entry is already present. */
2419 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2421 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2425 /* Add (or update) attribute to hash. */
2426 attr_new
= bgp_attr_intern(&attr
);
2428 /* Create new route with its attribute. */
2429 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
2430 parent_ri
->peer
, attr_new
, rn
);
2431 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
2432 bgp_info_extra_get(ri
);
2433 ri
->extra
->parent
= bgp_info_lock(parent_ri
);
2434 bgp_lock_node((struct bgp_node
*)parent_ri
->net
);
2435 if (parent_ri
->extra
) {
2436 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
2437 sizeof(ri
->extra
->label
));
2438 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
2440 bgp_info_add(rn
, ri
);
2442 if (attrhash_cmp(ri
->attr
, &attr
)
2443 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
2444 bgp_unlock_node(rn
);
2447 /* The attribute has changed. */
2448 /* Add (or update) attribute to hash. */
2449 attr_new
= bgp_attr_intern(&attr
);
2451 /* Restore route, if needed. */
2452 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
2453 bgp_info_restore(rn
, ri
);
2455 /* Mark if nexthop has changed. */
2456 if ((afi
== AFI_IP
&&
2457 !IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
)) ||
2459 !IPV6_ADDR_SAME(&ri
->attr
->mp_nexthop_global
,
2460 &attr_new
->mp_nexthop_global
)))
2461 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
2463 /* Unintern existing, set to new. */
2464 bgp_attr_unintern(&ri
->attr
);
2465 ri
->attr
= attr_new
;
2466 ri
->uptime
= bgp_clock();
2469 /* Perform route selection and update zebra, if required. */
2470 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2476 * Install route entry into the VNI routing table and invoke route selection.
2478 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2479 struct prefix_evpn
*p
,
2480 struct bgp_info
*parent_ri
)
2482 struct bgp_node
*rn
;
2483 struct bgp_info
*ri
;
2484 struct attr
*attr_new
;
2487 /* Create (or fetch) route within the VNI. */
2488 /* NOTE: There is no RD here. */
2489 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2491 /* Check if route entry is already present. */
2492 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2494 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2498 /* Add (or update) attribute to hash. */
2499 attr_new
= bgp_attr_intern(parent_ri
->attr
);
2501 /* Create new route with its attribute. */
2502 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
2503 parent_ri
->peer
, attr_new
, rn
);
2504 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
2505 bgp_info_extra_get(ri
);
2506 ri
->extra
->parent
= bgp_info_lock(parent_ri
);
2507 bgp_lock_node((struct bgp_node
*)parent_ri
->net
);
2508 if (parent_ri
->extra
) {
2509 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
2510 sizeof(ri
->extra
->label
));
2511 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
2513 bgp_info_add(rn
, ri
);
2515 if (attrhash_cmp(ri
->attr
, parent_ri
->attr
)
2516 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
2517 bgp_unlock_node(rn
);
2520 /* The attribute has changed. */
2521 /* Add (or update) attribute to hash. */
2522 attr_new
= bgp_attr_intern(parent_ri
->attr
);
2524 /* Restore route, if needed. */
2525 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
2526 bgp_info_restore(rn
, ri
);
2528 /* Mark if nexthop has changed. */
2529 if (!IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
))
2530 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
2532 /* Unintern existing, set to new. */
2533 bgp_attr_unintern(&ri
->attr
);
2534 ri
->attr
= attr_new
;
2535 ri
->uptime
= bgp_clock();
2538 /* Perform route selection and update zebra, if required. */
2539 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2544 /* Uninstall EVPN route entry from ES route table */
2545 static int uninstall_evpn_route_entry_in_es(struct bgp
*bgp
,
2547 struct prefix_evpn
*p
,
2548 struct bgp_info
*parent_ri
)
2551 struct bgp_node
*rn
;
2552 struct bgp_info
*ri
;
2554 if (!es
->route_table
)
2557 /* Locate route within the ESI.
2558 * NOTE: There is no RD here.
2560 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2564 /* Find matching route entry. */
2565 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2567 (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2573 /* Mark entry for deletion */
2574 bgp_info_delete(rn
, ri
);
2576 /* Perform route selection and update zebra, if required. */
2577 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2579 /* Unlock route node. */
2580 bgp_unlock_node(rn
);
2586 * Uninstall route entry from the VRF routing table and send message
2587 * to zebra, if appropriate.
2589 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2590 struct prefix_evpn
*evp
,
2591 struct bgp_info
*parent_ri
)
2593 struct bgp_node
*rn
;
2594 struct bgp_info
*ri
;
2597 struct prefix
*pp
= &p
;
2600 char buf
[PREFIX_STRLEN
];
2601 char buf1
[PREFIX_STRLEN
];
2603 memset(pp
, 0, sizeof(struct prefix
));
2604 ip_prefix_from_evpn_prefix(evp
, pp
);
2606 if (bgp_debug_zebra(NULL
)) {
2608 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2609 prefix2str(evp
, buf
, sizeof(buf
)),
2610 prefix2str(pp
, buf1
, sizeof(buf
)),
2611 vrf_id_to_name(bgp_vrf
->vrf_id
));
2614 /* Locate route within the VRF. */
2615 /* NOTE: There is no RD here. */
2616 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2618 safi
= SAFI_UNICAST
;
2619 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2622 safi
= SAFI_UNICAST
;
2623 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2629 /* Find matching route entry. */
2630 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2632 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2638 /* Mark entry for deletion */
2639 bgp_info_delete(rn
, ri
);
2641 /* Perform route selection and update zebra, if required. */
2642 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2644 /* Unlock route node. */
2645 bgp_unlock_node(rn
);
2651 * Uninstall route entry from the VNI routing table and send message
2652 * to zebra, if appropriate.
2654 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2655 struct prefix_evpn
*p
,
2656 struct bgp_info
*parent_ri
)
2658 struct bgp_node
*rn
;
2659 struct bgp_info
*ri
;
2662 /* Locate route within the VNI. */
2663 /* NOTE: There is no RD here. */
2664 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2668 /* Find matching route entry. */
2669 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2671 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2677 /* Mark entry for deletion */
2678 bgp_info_delete(rn
, ri
);
2680 /* Perform route selection and update zebra, if required. */
2681 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2683 /* Unlock route node. */
2684 bgp_unlock_node(rn
);
2690 * Given a prefix, see if it belongs to ES.
2692 static int is_prefix_matching_for_es(struct prefix_evpn
*p
,
2695 /* if not an ES route return false */
2696 if (p
->prefix
.route_type
!= BGP_EVPN_ES_ROUTE
)
2699 if (memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
)) == 0)
2706 * Given a route entry and a VRF, see if this route entry should be
2707 * imported into the VRF i.e., RTs match.
2709 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
, struct bgp_info
*ri
)
2711 struct attr
*attr
= ri
->attr
;
2712 struct ecommunity
*ecom
;
2716 /* Route should have valid RT to be even considered. */
2717 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2720 ecom
= attr
->ecommunity
;
2721 if (!ecom
|| !ecom
->size
)
2724 /* For each extended community RT, see if it matches this VNI. If any RT
2725 * matches, we're done.
2727 for (i
= 0; i
< ecom
->size
; i
++) {
2729 uint8_t type
, sub_type
;
2730 struct ecommunity_val
*eval
;
2731 struct ecommunity_val eval_tmp
;
2732 struct vrf_irt_node
*irt
;
2734 /* Only deal with RTs */
2735 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2736 eval
= (struct ecommunity_val
*)(ecom
->val
2737 + (i
* ECOMMUNITY_SIZE
));
2740 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2743 /* See if this RT matches specified VNIs import RTs */
2744 irt
= lookup_vrf_import_rt(eval
);
2746 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2749 /* Also check for non-exact match. In this, we mask out the AS
2751 * only check on the local-admin sub-field. This is to
2753 * VNI as the RT for EBGP peering too.
2756 if (type
== ECOMMUNITY_ENCODE_AS
2757 || type
== ECOMMUNITY_ENCODE_AS4
2758 || type
== ECOMMUNITY_ENCODE_IP
) {
2759 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2760 mask_ecom_global_admin(&eval_tmp
, eval
);
2761 irt
= lookup_vrf_import_rt(&eval_tmp
);
2764 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2772 * Given a route entry and a VNI, see if this route entry should be
2773 * imported into the VNI i.e., RTs match.
2775 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2776 struct bgp_info
*ri
)
2778 struct attr
*attr
= ri
->attr
;
2779 struct ecommunity
*ecom
;
2783 /* Route should have valid RT to be even considered. */
2784 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2787 ecom
= attr
->ecommunity
;
2788 if (!ecom
|| !ecom
->size
)
2791 /* For each extended community RT, see if it matches this VNI. If any RT
2792 * matches, we're done.
2794 for (i
= 0; i
< ecom
->size
; i
++) {
2796 uint8_t type
, sub_type
;
2797 struct ecommunity_val
*eval
;
2798 struct ecommunity_val eval_tmp
;
2799 struct irt_node
*irt
;
2801 /* Only deal with RTs */
2802 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2803 eval
= (struct ecommunity_val
*)(ecom
->val
2804 + (i
* ECOMMUNITY_SIZE
));
2807 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2810 /* See if this RT matches specified VNIs import RTs */
2811 irt
= lookup_import_rt(bgp
, eval
);
2813 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2816 /* Also check for non-exact match. In this, we mask out the AS
2818 * only check on the local-admin sub-field. This is to
2820 * VNI as the RT for EBGP peering too.
2823 if (type
== ECOMMUNITY_ENCODE_AS
2824 || type
== ECOMMUNITY_ENCODE_AS4
2825 || type
== ECOMMUNITY_ENCODE_IP
) {
2826 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2827 mask_ecom_global_admin(&eval_tmp
, eval
);
2828 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2831 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2838 static int install_uninstall_routes_for_es(struct bgp
*bgp
,
2845 char buf
[PREFIX_STRLEN
];
2846 char buf1
[ESI_STR_LEN
];
2847 struct bgp_node
*rd_rn
, *rn
;
2848 struct bgp_table
*table
;
2849 struct bgp_info
*ri
;
2855 * Walk entire global routing table and evaluate routes which could be
2856 * imported into this VRF. Note that we need to loop through all global
2857 * routes to determine which route matches the import rt on vrf
2859 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2860 rd_rn
= bgp_route_next(rd_rn
)) {
2861 table
= (struct bgp_table
*)(rd_rn
->info
);
2865 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2866 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2868 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2870 * Consider "valid" remote routes applicable for
2873 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2874 && ri
->type
== ZEBRA_ROUTE_BGP
2875 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2878 if (!is_prefix_matching_for_es(evp
, es
))
2882 ret
= install_evpn_route_entry_in_es(
2885 ret
= uninstall_evpn_route_entry_in_es(
2891 "Failed to %s EVPN %s route in ESI %s",
2894 prefix2str(evp
, buf
,
2896 esi_to_str(&es
->esi
, buf1
,
2907 * Install or uninstall mac-ip routes are appropriate for this
2910 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
2914 struct bgp_node
*rd_rn
, *rn
;
2915 struct bgp_table
*table
;
2916 struct bgp_info
*ri
;
2918 char buf
[PREFIX_STRLEN
];
2919 struct bgp
*bgp_def
= NULL
;
2923 bgp_def
= bgp_get_default();
2927 /* Walk entire global routing table and evaluate routes which could be
2928 * imported into this VRF. Note that we need to loop through all global
2929 * routes to determine which route matches the import rt on vrf
2931 for (rd_rn
= bgp_table_top(bgp_def
->rib
[afi
][safi
]); rd_rn
;
2932 rd_rn
= bgp_route_next(rd_rn
)) {
2933 table
= (struct bgp_table
*)(rd_rn
->info
);
2937 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2938 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2940 /* if not mac-ip route skip this route */
2941 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2942 || evp
->prefix
.route_type
2943 == BGP_EVPN_IP_PREFIX_ROUTE
))
2946 /* if not a mac+ip route skip this route */
2947 if (!(is_evpn_prefix_ipaddr_v4(evp
)
2948 || is_evpn_prefix_ipaddr_v6(evp
)))
2951 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2952 /* Consider "valid" remote routes applicable for
2955 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2956 && ri
->type
== ZEBRA_ROUTE_BGP
2957 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2960 if (is_route_matching_for_vrf(bgp_vrf
, ri
)) {
2962 ret
= install_evpn_route_entry_in_vrf(
2965 ret
= uninstall_evpn_route_entry_in_vrf(
2971 "Failed to %s EVPN %s route in VRF %s",
2974 prefix2str(evp
, buf
,
2989 * Install or uninstall routes of specified type that are appropriate for this
2992 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
2993 struct bgpevpn
*vpn
,
2994 bgp_evpn_route_type rtype
,
2999 struct bgp_node
*rd_rn
, *rn
;
3000 struct bgp_table
*table
;
3001 struct bgp_info
*ri
;
3007 /* Walk entire global routing table and evaluate routes which could be
3008 * imported into this VPN. Note that we cannot just look at the routes
3010 * the VNI's RD - remote routes applicable for this VNI could have any
3013 /* EVPN routes are a 2-level table. */
3014 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
3015 rd_rn
= bgp_route_next(rd_rn
)) {
3016 table
= (struct bgp_table
*)(rd_rn
->info
);
3020 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3021 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3023 if (evp
->prefix
.route_type
!= rtype
)
3026 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
3027 /* Consider "valid" remote routes applicable for
3029 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
3030 && ri
->type
== ZEBRA_ROUTE_BGP
3031 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
3034 if (is_route_matching_for_vni(bgp
, vpn
, ri
)) {
3036 ret
= install_evpn_route_entry(
3039 ret
= uninstall_evpn_route_entry(
3045 "%u: Failed to %s EVPN %s route in VNI %u",
3049 rtype
== BGP_EVPN_MAC_IP_ROUTE
3063 /* Install any existing remote ES routes applicable for this ES into its routing
3064 * table. This is invoked when ES comes up.
3066 static int install_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
3068 return install_uninstall_routes_for_es(bgp
, es
, 1);
3072 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3073 * is invoked upon l3vni-add or l3vni import rt change
3075 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3077 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3082 * Install any existing remote routes applicable for this VNI into its
3083 * routing table. This is invoked when a VNI becomes "live" or its Import
3086 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3090 /* Install type-3 routes followed by type-2 routes - the ones applicable
3093 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3098 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3102 /* uninstall routes from l3vni vrf. */
3103 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3105 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3110 * Uninstall any existing remote routes for this VNI. One scenario in which
3111 * this is invoked is upon an import RT change.
3113 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3117 /* Uninstall type-2 routes followed by type-3 routes - the ones
3121 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3126 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3130 /* Install or unistall route in ES */
3131 static int install_uninstall_route_in_es(struct bgp
*bgp
, struct evpnes
*es
,
3132 afi_t afi
, safi_t safi
,
3133 struct prefix_evpn
*evp
,
3134 struct bgp_info
*ri
,
3138 char buf
[ESI_STR_LEN
];
3141 ret
= install_evpn_route_entry_in_es(bgp
, es
, evp
, ri
);
3143 ret
= uninstall_evpn_route_entry_in_es(bgp
, es
, evp
, ri
);
3148 "%u: Failed to %s EVPN %s route in ESI %s", bgp
->vrf_id
,
3149 install
? "install" : "uninstall", "ES",
3150 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)));
3157 * Install or uninstall route in matching VRFs (list).
3159 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3160 safi_t safi
, struct prefix_evpn
*evp
,
3161 struct bgp_info
*ri
,
3162 struct list
*vrfs
, int install
)
3164 char buf
[PREFIX2STR_BUFFER
];
3165 struct bgp
*bgp_vrf
;
3166 struct listnode
*node
, *nnode
;
3168 /* Only type-2/type-5 routes go into a VRF */
3169 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3170 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3173 /* if it is type-2 route and not a mac+ip route skip this route */
3174 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3175 && !(is_evpn_prefix_ipaddr_v4(evp
)
3176 || is_evpn_prefix_ipaddr_v6(evp
)))
3179 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3183 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, ri
);
3185 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3189 flog_err(BGP_ERR_EVPN_FAIL
,
3190 "%u: Failed to %s prefix %s in VRF %s",
3192 install
? "install" : "uninstall",
3193 prefix2str(evp
, buf
, sizeof(buf
)),
3194 vrf_id_to_name(bgp_vrf
->vrf_id
));
3203 * Install or uninstall route in matching VNIs (list).
3205 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3206 safi_t safi
, struct prefix_evpn
*evp
,
3207 struct bgp_info
*ri
,
3208 struct list
*vnis
, int install
)
3210 struct bgpevpn
*vpn
;
3211 struct listnode
*node
, *nnode
;
3213 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3216 if (!is_vni_live(vpn
))
3220 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, ri
);
3222 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, ri
);
3227 "%u: Failed to %s EVPN %s route in VNI %u",
3228 bgp
->vrf_id
, install
? "install" : "uninstall",
3229 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3241 * Install or uninstall route for appropriate VNIs/ESIs.
3243 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3244 struct prefix
*p
, struct bgp_info
*ri
,
3247 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3248 struct attr
*attr
= ri
->attr
;
3249 struct ecommunity
*ecom
;
3254 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3255 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3256 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3257 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3258 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3261 /* If we don't have Route Target, nothing much to do. */
3262 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3265 ecom
= attr
->ecommunity
;
3266 if (!ecom
|| !ecom
->size
)
3269 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3270 * attached to the route */
3271 for (i
= 0; i
< ecom
->size
; i
++) {
3273 uint8_t type
, sub_type
;
3274 struct ecommunity_val
*eval
;
3275 struct ecommunity_val eval_tmp
;
3276 struct irt_node
*irt
; /* import rt for l2vni */
3277 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3280 /* Only deal with RTs */
3281 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3282 eval
= (struct ecommunity_val
*)(ecom
->val
3283 + (i
* ECOMMUNITY_SIZE
));
3286 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3290 * macip routes (type-2) are imported into VNI and VRF tables.
3291 * IMET route is imported into VNI table.
3292 * prefix routes are imported into VRF table.
3294 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3295 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3296 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3298 irt
= lookup_import_rt(bgp
, eval
);
3300 install_uninstall_route_in_vnis(bgp
, afi
, safi
,
3305 vrf_irt
= lookup_vrf_import_rt(eval
);
3307 install_uninstall_route_in_vrfs(bgp
, afi
, safi
,
3312 /* Also check for non-exact match.
3313 * In this, we mask out the AS and
3314 * only check on the local-admin sub-field.
3315 * This is to facilitate using
3316 * VNI as the RT for EBGP peering too.
3320 if (type
== ECOMMUNITY_ENCODE_AS
3321 || type
== ECOMMUNITY_ENCODE_AS4
3322 || type
== ECOMMUNITY_ENCODE_IP
) {
3323 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3324 mask_ecom_global_admin(&eval_tmp
, eval
);
3325 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3326 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3330 install_uninstall_route_in_vnis(bgp
, afi
, safi
,
3335 install_uninstall_route_in_vrfs(bgp
, afi
, safi
,
3341 /* es route is imported into the es table */
3342 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3344 /* we will match based on the entire esi to avoid
3345 * imoort of an es route for esi2 into esi1
3347 es
= bgp_evpn_lookup_es(bgp
, &evp
->prefix
.es_addr
.esi
);
3348 if (es
&& is_es_local(es
))
3349 install_uninstall_route_in_es(bgp
, es
,
3359 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3362 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3364 /* delete all ipv4 routes and withdraw from peers */
3365 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3366 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3368 /* delete all ipv6 routes and withdraw from peers */
3369 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3370 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3374 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3377 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3379 /* update all ipv4 routes */
3380 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3381 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3383 /* update all ipv6 routes */
3384 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3385 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3389 * update and advertise local routes for a VRF as type-5 routes.
3390 * This is invoked upon RD change for a VRF. Note taht the processing is only
3391 * done in the global route table using the routes which already exist in the
3394 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3396 /* skip if the RD is configured */
3397 if (is_vrf_rd_configured(bgp_vrf
))
3400 /* derive the RD for the VRF based on new router-id */
3401 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3403 /* update advertise ipv4|ipv6 routes as type-5 routes */
3404 update_advertise_vrf_routes(bgp_vrf
);
3408 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3409 * This is invoked upon VRF RD change. The processing is done only from global
3412 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3414 /* skip if the RD is configured */
3415 if (is_vrf_rd_configured(bgp_vrf
))
3418 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3419 delete_withdraw_vrf_routes(bgp_vrf
);
3423 * Update and advertise local routes for a VNI. Invoked upon router-id
3424 * change. Note that the processing is done only on the global route table
3425 * using routes that already exist in the per-VNI table.
3427 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3429 struct prefix_evpn p
;
3430 struct bgp_node
*rn
, *global_rn
;
3431 struct bgp_info
*ri
, *global_ri
;
3433 afi_t afi
= AFI_L2VPN
;
3434 safi_t safi
= SAFI_EVPN
;
3436 /* Locate type-3 route for VNI in the per-VNI table and use its
3437 * attributes to create and advertise the type-3 route for this VNI
3438 * in the global table.
3440 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3441 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3442 if (!rn
) /* unexpected */
3444 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
3445 if (ri
->peer
== bgp
->peer_self
&& ri
->type
== ZEBRA_ROUTE_BGP
3446 && ri
->sub_type
== BGP_ROUTE_STATIC
)
3448 if (!ri
) /* unexpected */
3452 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3453 (struct prefix
*)&p
, &vpn
->prd
);
3454 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1, 0, &ri
,
3457 /* Schedule for processing and unlock node. */
3458 bgp_process(bgp
, global_rn
, afi
, safi
);
3459 bgp_unlock_node(global_rn
);
3461 /* Now, walk this VNI's route table and use the route and its attribute
3462 * to create and schedule route in global table.
3464 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
3465 rn
= bgp_route_next(rn
)) {
3466 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3468 /* Identify MAC-IP local routes. */
3469 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3472 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
3473 if (ri
->peer
== bgp
->peer_self
3474 && ri
->type
== ZEBRA_ROUTE_BGP
3475 && ri
->sub_type
== BGP_ROUTE_STATIC
)
3480 /* Create route in global routing table using this route entry's
3484 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3485 (struct prefix
*)evp
, &vpn
->prd
);
3487 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
3490 /* Schedule for processing and unlock node. */
3491 bgp_process(bgp
, global_rn
, afi
, safi
);
3492 bgp_unlock_node(global_rn
);
3499 * Delete (and withdraw) local routes for a VNI - only from the global
3500 * table. Invoked upon router-id change.
3502 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3505 struct prefix_evpn p
;
3506 struct bgp_node
*global_rn
;
3507 struct bgp_info
*ri
;
3508 afi_t afi
= AFI_L2VPN
;
3509 safi_t safi
= SAFI_EVPN
;
3511 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3512 * for this VNI - from the global table.
3514 ret
= delete_global_type2_routes(bgp
, vpn
);
3518 /* Remove type-3 route for this VNI from global table. */
3519 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3520 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3521 (struct prefix
*)&p
, &vpn
->prd
);
3523 /* Delete route entry in the global EVPN table. */
3524 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &ri
);
3526 /* Schedule for processing - withdraws to peers happen from
3530 bgp_process(bgp
, global_rn
, afi
, safi
);
3531 bgp_unlock_node(global_rn
);
3538 * Handle router-id change. Update and advertise local routes corresponding
3539 * to this VNI from peers. Note that this is invoked after updating the
3540 * router-id. The routes in the per-VNI table are used to create routes in
3541 * the global table and schedule them.
3543 static void update_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3545 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3547 /* Skip VNIs with configured RD. */
3548 if (is_rd_configured(vpn
))
3551 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3552 update_advertise_vni_routes(bgp
, vpn
);
3556 * Handle router-id change. Delete and withdraw local routes corresponding
3557 * to this VNI from peers. Note that this is invoked prior to updating
3558 * the router-id and is done only on the global route table, the routes
3559 * are needed in the per-VNI table to re-advertise with new router id.
3561 static void withdraw_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3563 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3565 /* Skip VNIs with configured RD. */
3566 if (is_rd_configured(vpn
))
3569 delete_withdraw_vni_routes(bgp
, vpn
);
3573 * Process received EVPN type-2 route (advertise or withdraw).
3575 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3576 struct attr
*attr
, uint8_t *pfx
, int psize
,
3577 uint32_t addpath_id
)
3579 struct prefix_rd prd
;
3580 struct prefix_evpn p
;
3581 struct bgp_route_evpn evpn
;
3583 uint8_t macaddr_len
;
3584 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
3585 uint32_t num_labels
= 0;
3589 /* Type-2 route should be either 33, 37 or 49 bytes or an
3590 * additional 3 bytes if there is a second label (VNI):
3591 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3592 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3593 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3595 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3596 && psize
!= 40 && psize
!= 52) {
3597 flog_err(BGP_ERR_EVPN_ROUTE_INVALID
,
3598 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3599 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3603 memset(&evpn
, 0, sizeof(evpn
));
3605 /* Make prefix_rd */
3606 prd
.family
= AF_UNSPEC
;
3608 memcpy(&prd
.val
, pfx
, 8);
3611 /* Make EVPN prefix. */
3612 memset(&p
, 0, sizeof(struct prefix_evpn
));
3614 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3615 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3617 /* Copy Ethernet Seg Identifier */
3618 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
3621 /* Copy Ethernet Tag */
3622 memcpy(ð_tag
, pfx
, 4);
3623 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3626 /* Get the MAC Addr len */
3627 macaddr_len
= *pfx
++;
3629 /* Get the MAC Addr */
3630 if (macaddr_len
== (ETH_ALEN
* 8)) {
3631 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
3635 BGP_ERR_EVPN_ROUTE_INVALID
,
3636 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3637 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3643 ipaddr_len
= *pfx
++;
3644 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3645 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3647 BGP_ERR_EVPN_ROUTE_INVALID
,
3648 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3649 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3654 ipaddr_len
/= 8; /* Convert to bytes. */
3655 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3658 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
3662 /* Get the VNI(s). Stored as bytes here. */
3664 memset(label
, 0, sizeof(label
));
3665 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
3666 pfx
+= BGP_LABEL_BYTES
;
3667 psize
-= (33 + ipaddr_len
);
3668 /* Do we have a second VNI? */
3671 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
3673 * If in future, we are required to access additional fields,
3674 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
3679 /* Process the route. */
3681 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3682 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3683 &prd
, &label
[0], num_labels
, 0, &evpn
);
3685 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3686 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3687 &prd
, &label
[0], num_labels
, &evpn
);
3692 * Process received EVPN type-3 route (advertise or withdraw).
3694 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3695 struct attr
*attr
, uint8_t *pfx
, int psize
,
3696 uint32_t addpath_id
)
3698 struct prefix_rd prd
;
3699 struct prefix_evpn p
;
3704 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3705 * IP len (1) and IP (4 or 16).
3707 if (psize
!= 17 && psize
!= 29) {
3708 flog_err(BGP_ERR_EVPN_ROUTE_INVALID
,
3709 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3710 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3714 /* If PMSI is present, log if it is anything other than IR.
3715 * Note: We just simply ignore the values as it is not clear if
3716 * doing anything else is better.
3719 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3720 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
) {
3721 zlog_warn("%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3722 peer
->bgp
->vrf_id
, peer
->host
,
3723 attr
->pmsi_tnl_type
);
3727 /* Make prefix_rd */
3728 prd
.family
= AF_UNSPEC
;
3730 memcpy(&prd
.val
, pfx
, 8);
3733 /* Make EVPN prefix. */
3734 memset(&p
, 0, sizeof(struct prefix_evpn
));
3736 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3737 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3739 /* Copy Ethernet Tag */
3740 memcpy(ð_tag
, pfx
, 4);
3741 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
3745 ipaddr_len
= *pfx
++;
3746 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3747 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
3748 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
3751 BGP_ERR_EVPN_ROUTE_INVALID
,
3752 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3753 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3757 /* Process the route. */
3759 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3760 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3761 &prd
, NULL
, 0, 0, NULL
);
3763 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3764 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3765 &prd
, NULL
, 0, NULL
);
3770 * Process received EVPN type-4 route (advertise or withdraw).
3772 static int process_type4_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3773 struct attr
*attr
, uint8_t *pfx
, int psize
,
3774 uint32_t addpath_id
)
3779 struct in_addr vtep_ip
;
3780 struct prefix_rd prd
;
3781 struct prefix_evpn p
;
3783 /* Type-4 route should be either 23 or 35 bytes
3784 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
3786 if (psize
!= 23 && psize
!= 35) {
3787 flog_err(BGP_ERR_EVPN_ROUTE_INVALID
,
3788 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
3789 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3793 /* Make prefix_rd */
3794 prd
.family
= AF_UNSPEC
;
3796 memcpy(&prd
.val
, pfx
, 8);
3800 memcpy(&esi
, pfx
, ESI_BYTES
);
3805 ipaddr_len
= *pfx
++;
3806 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3807 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
3810 BGP_ERR_EVPN_ROUTE_INVALID
,
3811 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
3812 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3816 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
3817 /* Process the route. */
3819 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3820 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3821 &prd
, NULL
, 0, 0, NULL
);
3823 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3824 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3825 &prd
, NULL
, 0, NULL
);
3832 * Process received EVPN type-5 route (advertise or withdraw).
3834 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3835 struct attr
*attr
, uint8_t *pfx
, int psize
,
3836 uint32_t addpath_id
, int withdraw
)
3838 struct prefix_rd prd
;
3839 struct prefix_evpn p
;
3840 struct bgp_route_evpn evpn
;
3843 mpls_label_t label
; /* holds the VNI as in the packet */
3846 /* Type-5 route should be 34 or 58 bytes:
3847 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3848 * GW (4 or 16) and VNI (3).
3849 * Note that the IP and GW should both be IPv4 or both IPv6.
3851 if (psize
!= 34 && psize
!= 58) {
3852 flog_err(BGP_ERR_EVPN_ROUTE_INVALID
,
3853 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3854 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3858 /* Make prefix_rd */
3859 prd
.family
= AF_UNSPEC
;
3861 memcpy(&prd
.val
, pfx
, 8);
3864 /* Make EVPN prefix. */
3865 memset(&p
, 0, sizeof(struct prefix_evpn
));
3867 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3868 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
3870 /* Additional information outside of prefix - ESI and GW IP */
3871 memset(&evpn
, 0, sizeof(evpn
));
3874 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
3877 /* Fetch Ethernet Tag. */
3878 memcpy(ð_tag
, pfx
, 4);
3879 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
3882 /* Fetch IP prefix length. */
3884 if (ippfx_len
> IPV6_MAX_BITLEN
) {
3886 BGP_ERR_EVPN_ROUTE_INVALID
,
3887 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3888 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
3891 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
3893 /* Determine IPv4 or IPv6 prefix */
3894 /* Since the address and GW are from the same family, this just becomes
3895 * a simple check on the total size.
3898 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
3899 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
3901 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
3904 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
3905 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
3907 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
3911 /* Get the VNI (in MPLS label field). Stored as bytes here. */
3912 memset(&label
, 0, sizeof(label
));
3913 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
3916 * If in future, we are required to access additional fields,
3917 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
3921 /* Process the route. */
3923 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3924 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3925 &prd
, &label
, 1, 0, &evpn
);
3927 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3928 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3929 &prd
, &label
, 1, &evpn
);
3934 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
3935 struct prefix_rd
*prd
, mpls_label_t
*label
,
3936 uint32_t num_labels
, struct attr
*attr
)
3940 struct evpn_addr
*p_evpn_p
;
3942 memset(&temp
, 0, 16);
3943 if (p
->family
!= AF_EVPN
)
3945 p_evpn_p
= &(p
->u
.prefix_evpn
);
3947 /* len denites the total len of IP and GW-IP in the route
3948 IP and GW-IP have to be both ipv4 or ipv6
3950 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3951 len
= 8; /* IP and GWIP are both ipv4 */
3953 len
= 32; /* IP and GWIP are both ipv6 */
3954 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
3955 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
3956 stream_put(s
, prd
->val
, 8);
3958 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
3960 stream_put(s
, &temp
, 10);
3961 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
3962 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
3963 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3964 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
3966 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
3968 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3970 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
3972 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
3974 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3975 stream_put_ipv4(s
, 0);
3977 stream_put(s
, &temp
, 16);
3981 stream_put(s
, label
, 3);
3987 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
3989 static void cleanup_vni_on_disable(struct hash_backet
*backet
, struct bgp
*bgp
)
3991 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3993 /* Remove EVPN routes and schedule for processing. */
3994 delete_routes_for_vni(bgp
, vpn
);
3996 /* Clear "live" flag and see if hash needs to be freed. */
3997 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
3998 if (!is_vni_configured(vpn
))
3999 bgp_evpn_free(bgp
, vpn
);
4003 * Free a VNI entry; iterator function called during cleanup.
4005 static void free_vni_entry(struct hash_backet
*backet
, struct bgp
*bgp
)
4007 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
4009 delete_all_vni_routes(bgp
, vpn
);
4010 bgp_evpn_free(bgp
, vpn
);
4014 * Derive AUTO import RT for BGP VRF - L3VNI
4016 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4018 struct bgp
*bgp_def
= NULL
;
4020 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4021 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4024 bgp_def
= bgp_get_default();
4027 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4031 * Delete AUTO import RT from BGP VRF - L3VNI
4033 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4035 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4039 * Derive AUTO export RT for BGP VRF - L3VNI
4041 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4043 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4044 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4048 * Delete AUTO export RT from BGP VRF - L3VNI
4050 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4052 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4055 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4057 struct bgp
*bgp_def
= NULL
;
4058 struct listnode
*node
= NULL
;
4059 struct bgpevpn
*vpn
= NULL
;
4061 bgp_def
= bgp_get_default();
4065 /* update all type-5 routes */
4066 update_advertise_vrf_routes(bgp_vrf
);
4068 /* update all type-2 routes */
4069 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4070 update_routes_for_vni(bgp_def
, vpn
);
4074 * Handle autort change for a given VNI.
4076 static void update_autort_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
4078 struct bgpevpn
*vpn
= backet
->data
;
4080 if (!is_import_rt_configured(vpn
)) {
4081 if (is_vni_live(vpn
))
4082 bgp_evpn_uninstall_routes(bgp
, vpn
);
4083 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4084 list_delete_all_node(vpn
->import_rtl
);
4085 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4086 if (is_vni_live(vpn
))
4087 bgp_evpn_install_routes(bgp
, vpn
);
4089 if (!is_export_rt_configured(vpn
)) {
4090 list_delete_all_node(vpn
->export_rtl
);
4091 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4092 if (is_vni_live(vpn
))
4093 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4101 /* withdraw type-5 route corresponding to ip prefix */
4102 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4103 afi_t afi
, safi_t safi
)
4106 struct prefix_evpn evp
;
4107 char buf
[PREFIX_STRLEN
];
4109 build_type5_prefix_from_ip_prefix(&evp
, p
);
4110 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4113 BGP_ERR_EVPN_ROUTE_DELETE
,
4114 "%u failed to delete type-5 route for prefix %s in vrf %s",
4115 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4116 vrf_id_to_name(bgp_vrf
->vrf_id
));
4120 /* withdraw all type-5 routes for an address family */
4121 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4123 struct bgp_table
*table
= NULL
;
4124 struct bgp_node
*rn
= NULL
;
4125 struct bgp_info
*ri
;
4127 table
= bgp_vrf
->rib
[afi
][safi
];
4128 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4129 /* Only care about "selected" routes - non-imported. */
4130 /* TODO: Support for AddPath for EVPN. */
4131 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
4132 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
)
4133 && (!ri
->extra
|| !ri
->extra
->parent
)) {
4134 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
4143 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4144 * to this function correspond to those of the source IP prefix (best
4145 * path in the case of the attr. In the case of a local prefix (when we
4146 * are advertising local subnets), the src_attr will be NULL.
4148 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4149 struct attr
*src_attr
, afi_t afi
,
4153 struct prefix_evpn evp
;
4154 char buf
[PREFIX_STRLEN
];
4156 build_type5_prefix_from_ip_prefix(&evp
, p
);
4157 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4159 flog_err(BGP_ERR_EVPN_ROUTE_CREATE
,
4160 "%u: Failed to create type-5 route for prefix %s",
4161 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4164 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4165 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4166 * advertisement is enabled.
4168 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4171 struct bgp_table
*table
= NULL
;
4172 struct bgp_node
*rn
= NULL
;
4173 struct bgp_info
*ri
;
4175 table
= bgp_vrf
->rib
[afi
][safi
];
4176 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4177 /* Need to identify the "selected" route entry to use its
4178 * attribute. Also, we only consider "non-imported" routes.
4179 * TODO: Support for AddPath for EVPN.
4181 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
4182 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
)
4183 && (!ri
->extra
|| !ri
->extra
->parent
)) {
4185 /* apply the route-map */
4186 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4189 ret
= route_map_apply(
4190 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4192 &rn
->p
, RMAP_BGP
, ri
);
4193 if (ret
== RMAP_DENYMATCH
)
4196 bgp_evpn_advertise_type5_route(
4197 bgp_vrf
, &rn
->p
, ri
->attr
, afi
, safi
);
4204 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4206 struct listnode
*node
, *nnode
, *node_to_del
;
4207 struct ecommunity
*ecom
, *ecom_auto
;
4208 struct ecommunity_val eval
;
4210 if (bgp
->advertise_autort_rfc8365
)
4211 vni
|= EVPN_AUTORT_VXLAN
;
4212 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4214 ecom_auto
= ecommunity_new();
4215 ecommunity_add_val(ecom_auto
, &eval
);
4218 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4219 if (ecommunity_match(ecom
, ecom_auto
)) {
4220 ecommunity_free(&ecom
);
4226 list_delete_node(rtl
, node_to_del
);
4228 ecommunity_free(&ecom_auto
);
4231 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4232 struct ecommunity
*ecomadd
)
4234 /* uninstall routes from vrf */
4235 uninstall_routes_for_vrf(bgp_vrf
);
4237 /* Cleanup the RT to VRF mapping */
4238 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4240 /* Remove auto generated RT */
4241 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4243 /* Add the newly configured RT to RT list */
4244 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4245 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4247 /* map VRF to its RTs */
4248 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4250 /* install routes matching the new VRF */
4251 install_routes_for_vrf(bgp_vrf
);
4254 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4255 struct ecommunity
*ecomdel
)
4257 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4258 struct ecommunity
*ecom
= NULL
;
4260 /* uninstall routes from vrf */
4261 uninstall_routes_for_vrf(bgp_vrf
);
4263 /* Cleanup the RT to VRF mapping */
4264 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4266 /* remove the RT from the RT list */
4267 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4268 if (ecommunity_match(ecom
, ecomdel
)) {
4269 ecommunity_free(&ecom
);
4276 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4278 assert(bgp_vrf
->vrf_import_rtl
);
4279 /* fallback to auto import rt, if this was the last RT */
4280 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4281 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4282 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4285 /* map VRFs to its RTs */
4286 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4288 /* install routes matching this new RT */
4289 install_routes_for_vrf(bgp_vrf
);
4292 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4293 struct ecommunity
*ecomadd
)
4295 /* remove auto-generated RT */
4296 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4298 /* Add the new RT to the RT list */
4299 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4300 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4302 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4305 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4306 struct ecommunity
*ecomdel
)
4308 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4309 struct ecommunity
*ecom
= NULL
;
4311 /* Remove the RT from the RT list */
4312 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4313 if (ecommunity_match(ecom
, ecomdel
)) {
4314 ecommunity_free(&ecom
);
4321 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4324 * Temporary assert to make SA happy.
4325 * The ALL_LIST_ELEMENTS macro above has a NULL check
4326 * which means that SA is going to complain about
4327 * the list_isempty call, which doesn't NULL check.
4328 * So until we get this situation cleaned up, here
4331 assert(bgp_vrf
->vrf_export_rtl
);
4333 /* fall back to auto-generated RT if this was the last RT */
4334 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4335 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4336 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4339 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4343 * Handle change to BGP router id. This is invoked twice by the change
4344 * handler, first before the router id has been changed and then after
4345 * the router id has been changed. The first invocation will result in
4346 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4347 * will result in the routes being re-advertised.
4349 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4353 /* delete and withdraw all the type-5 routes
4354 stored in the global table for this vrf
4356 withdraw_router_id_vrf(bgp
);
4358 /* delete all the VNI routes (type-2/type-3) routes for all the
4361 hash_iterate(bgp
->vnihash
,
4362 (void (*)(struct hash_backet
*,
4363 void *))withdraw_router_id_vni
,
4367 /* advertise all routes in the vrf as type-5 routes with the new
4370 update_router_id_vrf(bgp
);
4372 /* advertise all the VNI routes (type-2/type-3) routes with the
4375 hash_iterate(bgp
->vnihash
,
4376 (void (*)(struct hash_backet
*,
4377 void *))update_router_id_vni
,
4383 * Handle change to auto-RT algorithm - update and advertise local routes.
4385 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4387 hash_iterate(bgp
->vnihash
,
4388 (void (*)(struct hash_backet
*,
4389 void*))update_autort_vni
,
4394 * Handle change to export RT - update and advertise local routes.
4396 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4398 return update_routes_for_vni(bgp
, vpn
);
4401 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4404 delete_withdraw_vrf_routes(bgp_vrf
);
4406 update_advertise_vrf_routes(bgp_vrf
);
4410 * Handle change to RD. This is invoked twice by the change handler,
4411 * first before the RD has been changed and then after the RD has
4412 * been changed. The first invocation will result in local routes
4413 * of this VNI being deleted and withdrawn and the next will result
4414 * in the routes being re-advertised.
4416 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4420 delete_withdraw_vni_routes(bgp
, vpn
);
4422 update_advertise_vni_routes(bgp
, vpn
);
4426 * Install routes for this VNI. Invoked upon change to Import RT.
4428 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4430 return install_routes_for_vni(bgp
, vpn
);
4434 * Uninstall all routes installed for this VNI. Invoked upon change
4437 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4439 return uninstall_routes_for_vni(bgp
, vpn
);
4443 * TODO: Hardcoded for a maximum of 2 VNIs right now
4445 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4450 vni1
= label2vni(label
);
4451 if (num_labels
== 2) {
4452 vni2
= label2vni(label
+ 1);
4453 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4455 snprintf(buf
, len
, "%u", vni1
);
4460 * Function to convert evpn route to json format.
4461 * NOTE: We don't use prefix2str as the output here is a bit different.
4463 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
4465 char buf1
[ETHER_ADDR_STRLEN
];
4466 char buf2
[PREFIX2STR_BUFFER
];
4471 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4472 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4473 json_object_int_add(json
, "ethTag",
4474 p
->prefix
.imet_addr
.eth_tag
);
4475 json_object_int_add(json
, "ipLen",
4476 is_evpn_prefix_ipaddr_v4(p
)
4479 json_object_string_add(json
, "ip",
4480 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4481 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4482 if (is_evpn_prefix_ipaddr_none(p
)) {
4483 json_object_int_add(json
, "routeType",
4484 p
->prefix
.route_type
);
4485 json_object_int_add(json
, "ethTag",
4486 p
->prefix
.macip_addr
.eth_tag
);
4487 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4488 json_object_string_add(json
, "mac",
4489 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4495 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4498 json_object_int_add(json
, "routeType",
4499 p
->prefix
.route_type
);
4500 json_object_int_add(json
, "ethTag",
4501 p
->prefix
.macip_addr
.eth_tag
);
4502 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4503 json_object_string_add(json
, "mac",
4504 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4507 json_object_int_add(json
, "ipLen",
4508 is_evpn_prefix_ipaddr_v4(p
)
4511 json_object_string_add(
4514 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4516 PREFIX2STR_BUFFER
));
4519 /* Currently, this is to cater to other AF_ETHERNET code. */
4524 * Function to convert evpn route to string.
4525 * NOTE: We don't use prefix2str as the output here is a bit different.
4527 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
4529 char buf1
[ETHER_ADDR_STRLEN
];
4530 char buf2
[PREFIX2STR_BUFFER
];
4531 char buf3
[ESI_STR_LEN
];
4533 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4534 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
4535 p
->prefix
.imet_addr
.eth_tag
,
4536 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4538 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4539 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4540 if (is_evpn_prefix_ipaddr_none(p
))
4541 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4542 p
->prefix
.route_type
,
4543 p
->prefix
.macip_addr
.eth_tag
,
4545 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4550 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4552 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
4553 p
->prefix
.route_type
,
4554 p
->prefix
.macip_addr
.eth_tag
,
4556 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4558 family
== AF_INET
? IPV4_MAX_BITLEN
4561 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4563 PREFIX2STR_BUFFER
));
4565 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
4566 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4567 p
->prefix
.route_type
,
4568 p
->prefix
.prefix_addr
.eth_tag
,
4569 p
->prefix
.prefix_addr
.ip_prefix_length
,
4570 is_evpn_prefix_ipaddr_v4(p
)
4571 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
4572 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
4573 } else if (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
4574 snprintf(buf
, len
, "[%d]:[%s]:[%d]:[%s]",
4575 p
->prefix
.route_type
,
4576 esi_to_str(&p
->prefix
.es_addr
.esi
, buf3
, sizeof(buf3
)),
4577 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4579 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
));
4581 /* For EVPN route types not supported yet. */
4582 snprintf(buf
, len
, "(unsupported route type %d)",
4583 p
->prefix
.route_type
);
4590 * Encode EVPN prefix in Update (MP_REACH)
4592 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
4593 struct prefix_rd
*prd
, mpls_label_t
*label
,
4594 uint32_t num_labels
, struct attr
*attr
,
4595 int addpath_encode
, uint32_t addpath_tx_id
)
4597 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4598 int len
, ipa_len
= 0;
4601 stream_putl(s
, addpath_tx_id
);
4604 stream_putc(s
, evp
->prefix
.route_type
);
4606 switch (evp
->prefix
.route_type
) {
4607 case BGP_EVPN_MAC_IP_ROUTE
:
4608 if (is_evpn_prefix_ipaddr_v4(evp
))
4609 ipa_len
= IPV4_MAX_BYTELEN
;
4610 else if (is_evpn_prefix_ipaddr_v6(evp
))
4611 ipa_len
= IPV6_MAX_BYTELEN
;
4612 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4613 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4614 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4616 stream_putc(s
, len
);
4617 stream_put(s
, prd
->val
, 8); /* RD */
4619 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
4621 stream_put(s
, 0, 10);
4622 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4623 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4624 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4625 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4626 if (ipa_len
) /* IP */
4627 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4629 /* 1st label is the L2 VNI */
4630 stream_put(s
, label
, BGP_LABEL_BYTES
);
4631 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4632 if (ipa_len
&& num_labels
> 1)
4633 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4636 case BGP_EVPN_IMET_ROUTE
:
4637 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4638 stream_put(s
, prd
->val
, 8); /* RD */
4639 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4640 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4641 /* Originating Router's IP Addr */
4642 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4645 case BGP_EVPN_ES_ROUTE
:
4646 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4647 stream_put(s
, prd
->val
, 8); /* RD */
4648 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4649 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4651 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4654 case BGP_EVPN_IP_PREFIX_ROUTE
:
4655 /* TODO: AddPath support. */
4656 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4664 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4665 struct bgp_nlri
*packet
, int withdraw
)
4671 uint32_t addpath_id
;
4672 int addpath_encoded
;
4677 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4679 lim
= pnt
+ packet
->length
;
4681 safi
= packet
->safi
;
4685 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
4686 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
4687 PEER_CAP_ADDPATH_AF_TX_RCV
));
4689 for (; pnt
< lim
; pnt
+= psize
) {
4690 /* Clear prefix structure. */
4691 memset(&p
, 0, sizeof(struct prefix
));
4693 /* Deal with path-id if AddPath is supported. */
4694 if (addpath_encoded
) {
4695 /* When packet overflow occurs return immediately. */
4696 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4699 addpath_id
= ntohl(*((uint32_t *)pnt
));
4700 pnt
+= BGP_ADDPATH_ID_LEN
;
4703 /* All EVPN NLRI types start with type and length. */
4710 /* When packet overflow occur return immediately. */
4711 if (pnt
+ psize
> lim
)
4715 case BGP_EVPN_MAC_IP_ROUTE
:
4716 if (process_type2_route(peer
, afi
, safi
,
4717 withdraw
? NULL
: attr
, pnt
,
4718 psize
, addpath_id
)) {
4721 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4722 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4727 case BGP_EVPN_IMET_ROUTE
:
4728 if (process_type3_route(peer
, afi
, safi
,
4729 withdraw
? NULL
: attr
, pnt
,
4730 psize
, addpath_id
)) {
4732 BGP_ERR_PKT_PROCESS
,
4733 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4734 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4739 case BGP_EVPN_ES_ROUTE
:
4740 if (process_type4_route(peer
, afi
, safi
,
4741 withdraw
? NULL
: attr
, pnt
,
4742 psize
, addpath_id
)) {
4744 BGP_ERR_PKT_PROCESS
,
4745 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4746 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4751 case BGP_EVPN_IP_PREFIX_ROUTE
:
4752 if (process_type5_route(peer
, afi
, safi
, attr
, pnt
,
4753 psize
, addpath_id
, withdraw
)) {
4755 BGP_ERR_PKT_PROCESS
,
4756 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4757 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4767 /* Packet length consistency check. */
4775 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
4776 * The mapping will be used during route processing.
4777 * bgp_def: default bgp instance
4778 * bgp_vrf: specific bgp vrf instance on which RT is configured
4780 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
4783 struct ecommunity_val
*eval
= NULL
;
4784 struct listnode
*node
= NULL
, *nnode
= NULL
;
4785 struct ecommunity
*ecom
= NULL
;
4787 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4788 for (i
= 0; i
< ecom
->size
; i
++) {
4789 eval
= (struct ecommunity_val
*)(ecom
->val
4791 * ECOMMUNITY_SIZE
));
4792 map_vrf_to_rt(bgp_vrf
, eval
);
4798 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
4800 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
4803 struct ecommunity_val
*eval
;
4804 struct listnode
*node
, *nnode
;
4805 struct ecommunity
*ecom
;
4807 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4808 for (i
= 0; i
< ecom
->size
; i
++) {
4809 struct vrf_irt_node
*irt
;
4810 struct ecommunity_val eval_tmp
;
4812 eval
= (struct ecommunity_val
*)(ecom
->val
4814 * ECOMMUNITY_SIZE
));
4815 /* If using "automatic" RT, we only care about the
4816 * local-admin sub-field.
4817 * This is to facilitate using VNI as the RT for EBGP
4820 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4821 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
4822 BGP_VRF_IMPORT_RT_CFGD
))
4823 mask_ecom_global_admin(&eval_tmp
, eval
);
4825 irt
= lookup_vrf_import_rt(&eval_tmp
);
4827 unmap_vrf_from_rt(bgp_vrf
, irt
);
4834 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
4835 * The mapping will be used during route processing.
4837 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4840 struct ecommunity_val
*eval
;
4841 struct listnode
*node
, *nnode
;
4842 struct ecommunity
*ecom
;
4844 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4845 for (i
= 0; i
< ecom
->size
; i
++) {
4846 eval
= (struct ecommunity_val
*)(ecom
->val
4848 * ECOMMUNITY_SIZE
));
4849 map_vni_to_rt(bgp
, vpn
, eval
);
4855 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
4857 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4860 struct ecommunity_val
*eval
;
4861 struct listnode
*node
, *nnode
;
4862 struct ecommunity
*ecom
;
4864 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4865 for (i
= 0; i
< ecom
->size
; i
++) {
4866 struct irt_node
*irt
;
4867 struct ecommunity_val eval_tmp
;
4869 eval
= (struct ecommunity_val
*)(ecom
->val
4871 * ECOMMUNITY_SIZE
));
4872 /* If using "automatic" RT, we only care about the
4873 * local-admin sub-field.
4874 * This is to facilitate using VNI as the RT for EBGP
4877 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4878 if (!is_import_rt_configured(vpn
))
4879 mask_ecom_global_admin(&eval_tmp
, eval
);
4881 irt
= lookup_import_rt(bgp
, &eval_tmp
);
4883 unmap_vni_from_rt(bgp
, vpn
, irt
);
4889 * Derive Import RT automatically for VNI and map VNI to RT.
4890 * The mapping will be used during route processing.
4892 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4894 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
4895 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
4898 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
4902 * Derive Export RT automatically for VNI.
4904 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4906 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
4907 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
4911 * Derive RD automatically for VNI using passed information - it
4912 * is of the form RouterId:unique-id-for-vni.
4914 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
4916 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
4920 * Derive RD automatically for VNI using passed information - it
4921 * is of the form RouterId:unique-id-for-vni.
4923 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4927 vpn
->prd
.family
= AF_UNSPEC
;
4928 vpn
->prd
.prefixlen
= 64;
4929 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
4930 (void)str2prefix_rd(buf
, &vpn
->prd
);
4931 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
4937 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
4939 struct list
*inst
= bm
->bgp
;
4940 struct listnode
*node
;
4941 struct bgp
*bgp_vrf
;
4943 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
4944 if (bgp_vrf
->l3vni
== vni
)
4954 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
4956 struct bgpevpn
*vpn
;
4959 memset(&tmp
, 0, sizeof(struct bgpevpn
));
4961 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
4966 * Create a new vpn - invoked upon configuration or zebra notification.
4968 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
4969 struct in_addr originator_ip
,
4970 vrf_id_t tenant_vrf_id
)
4972 struct bgpevpn
*vpn
;
4977 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
4981 /* Set values - RD and RT set to defaults. */
4983 vpn
->originator_ip
= originator_ip
;
4984 vpn
->tenant_vrf_id
= tenant_vrf_id
;
4986 /* Initialize route-target import and export lists */
4987 vpn
->import_rtl
= list_new();
4988 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4989 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
4990 vpn
->export_rtl
= list_new();
4991 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4992 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
4993 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
4994 derive_rd_rt_for_vni(bgp
, vpn
);
4996 /* Initialize EVPN route table. */
4997 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5000 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
5001 XFREE(MTYPE_BGP_EVPN
, vpn
);
5005 /* add to l2vni list on corresponding vrf */
5006 bgpevpn_link_to_l3vni(vpn
);
5008 QOBJ_REG(vpn
, bgpevpn
);
5013 * Free a given VPN - called in multiple scenarios such as zebra
5014 * notification, configuration being deleted, advertise-all-vni disabled etc.
5015 * This just frees appropriate memory, caller should have taken other
5018 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5020 bgpevpn_unlink_from_l3vni(vpn
);
5021 bgp_table_unlock(vpn
->route_table
);
5022 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5023 list_delete_and_null(&vpn
->import_rtl
);
5024 list_delete_and_null(&vpn
->export_rtl
);
5025 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5026 hash_release(bgp
->vnihash
, vpn
);
5028 XFREE(MTYPE_BGP_EVPN
, vpn
);
5034 struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
)
5039 memset(&tmp
, 0, sizeof(struct evpnes
));
5040 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
5041 es
= hash_lookup(bgp
->esihash
, &tmp
);
5046 * Create a new local es - invoked upon zebra notification.
5048 struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
,
5050 struct ipaddr
*originator_ip
)
5058 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct evpnes
));
5062 /* set the ESI and originator_ip */
5063 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
5064 memcpy(&es
->originator_ip
, originator_ip
, sizeof(struct ipaddr
));
5066 /* Initialise the VTEP list */
5067 es
->vtep_list
= list_new();
5068 es
->vtep_list
->cmp
= (int (*)(void *, void *))evpn_vtep_ip_cmp
;
5070 /* auto derive RD for this es */
5071 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
5072 es
->prd
.family
= AF_UNSPEC
;
5073 es
->prd
.prefixlen
= 64;
5074 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), es
->rd_id
);
5075 (void)str2prefix_rd(buf
, &es
->prd
);
5077 /* Initialize the ES route table */
5078 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5081 if (!hash_get(bgp
->esihash
, es
, hash_alloc_intern
)) {
5082 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5086 QOBJ_REG(es
, evpnes
);
5092 * This just frees appropriate memory, caller should have taken other
5095 void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
)
5097 list_delete_and_null(&es
->vtep_list
);
5098 bgp_table_unlock(es
->route_table
);
5099 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
5100 hash_release(bgp
->esihash
, es
);
5102 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5106 * Import evpn route from global table to VNI/VRF/ESI.
5108 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5109 struct prefix
*p
, struct bgp_info
*ri
)
5111 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 1);
5115 * Unimport evpn route from VNI/VRF/ESI.
5117 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5118 struct prefix
*p
, struct bgp_info
*ri
)
5120 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 0);
5123 /* filter routes which have martian next hops */
5124 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5128 struct bgp_node
*rd_rn
, *rn
;
5129 struct bgp_table
*table
;
5130 struct bgp_info
*ri
;
5135 /* Walk entire global routing table and evaluate routes which could be
5136 * imported into this VPN. Note that we cannot just look at the routes
5137 * for the VNI's RD -
5138 * remote routes applicable for this VNI could have any RD.
5140 /* EVPN routes are a 2-level table. */
5141 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
5142 rd_rn
= bgp_route_next(rd_rn
)) {
5143 table
= (struct bgp_table
*)(rd_rn
->info
);
5147 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5149 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
5151 /* Consider "valid" remote routes applicable for
5153 if (!(ri
->type
== ZEBRA_ROUTE_BGP
5154 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
5157 if (bgp_nexthop_self(bgp
, ri
->attr
->nexthop
)) {
5159 char attr_str
[BUFSIZ
];
5160 char pbuf
[PREFIX_STRLEN
];
5162 bgp_dump_attr(ri
->attr
, attr_str
,
5165 if (bgp_debug_update(ri
->peer
, &rn
->p
,
5168 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
5175 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5178 bgp_rib_remove(rn
, ri
, ri
->peer
, afi
,
5189 * Handle del of a local MACIP.
5191 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5194 struct bgpevpn
*vpn
;
5195 struct prefix_evpn p
;
5197 /* Lookup VNI hash - should exist. */
5198 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5199 if (!vpn
|| !is_vni_live(vpn
)) {
5200 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP DEL",
5201 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5205 /* Remove EVPN type-2 route and schedule for processing. */
5206 build_evpn_type2_prefix(&p
, mac
, ip
);
5207 delete_evpn_route(bgp
, vpn
, &p
);
5213 * Handle add of a local MACIP.
5215 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5216 struct ipaddr
*ip
, uint8_t flags
)
5218 struct bgpevpn
*vpn
;
5219 struct prefix_evpn p
;
5221 /* Lookup VNI hash - should exist. */
5222 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5223 if (!vpn
|| !is_vni_live(vpn
)) {
5224 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP ADD",
5225 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5229 /* Create EVPN type-2 route and schedule for processing. */
5230 build_evpn_type2_prefix(&p
, mac
, ip
);
5231 if (update_evpn_route(bgp
, vpn
, &p
, flags
)) {
5232 char buf
[ETHER_ADDR_STRLEN
];
5233 char buf2
[INET6_ADDRSTRLEN
];
5236 BGP_ERR_EVPN_ROUTE_CREATE
,
5237 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5238 bgp
->vrf_id
, vpn
->vni
,
5239 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5242 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5243 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5250 static void link_l2vni_hash_to_l3vni(struct hash_backet
*backet
,
5251 struct bgp
*bgp_vrf
)
5253 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
5254 struct bgp
*bgp_def
= NULL
;
5256 bgp_def
= bgp_get_default();
5259 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5260 bgpevpn_link_to_l3vni(vpn
);
5263 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
, struct ethaddr
*rmac
,
5264 struct in_addr originator_ip
, int filter
)
5266 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5267 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5268 struct listnode
*node
= NULL
;
5269 struct bgpevpn
*vpn
= NULL
;
5272 /* get the default instance - required to get the AS number for VRF
5275 bgp_def
= bgp_get_default();
5279 "Cannot process L3VNI %u ADD - default BGP instance not yet created",
5285 /* if the BGP vrf instance doesn't exist - create one */
5286 bgp_vrf
= bgp_lookup_by_name(vrf_id_to_name(vrf_id
));
5291 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5292 BGP_INSTANCE_TYPE_VRF
);
5294 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
:
5295 flog_err(BGP_ERR_MULTI_INSTANCE
,
5296 "'bgp multiple-instance' not present\n");
5298 case BGP_ERR_AS_MISMATCH
:
5299 flog_err(BGP_ERR_EVPN_AS_MISMATCH
,
5300 "BGP is already running; AS is %u\n", as
);
5302 case BGP_ERR_INSTANCE_MISMATCH
:
5303 flog_err(BGP_ERR_EVPN_INSTANCE_MISMATCH
,
5304 "BGP instance name and AS number mismatch\n");
5308 /* mark as auto created */
5309 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5312 /* associate with l3vni */
5313 bgp_vrf
->l3vni
= l3vni
;
5315 /* set the router mac - to be used in mac-ip routes for this vrf */
5316 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
5318 /* set the originator ip */
5319 bgp_vrf
->originator_ip
= originator_ip
;
5321 /* set the right filter - are we using l3vni only for prefix routes? */
5323 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5325 /* auto derive RD/RT */
5326 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5327 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5328 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5329 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5330 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5332 /* link all corresponding l2vnis */
5333 hash_iterate(bgp_def
->vnihash
,
5334 (void (*)(struct hash_backet
*,
5335 void *))link_l2vni_hash_to_l3vni
,
5338 /* Only update all corresponding type-2 routes if we are advertising two
5339 * labels along with type-2 routes
5342 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5343 update_routes_for_vni(bgp_def
, vpn
);
5345 /* advertise type-5 routes if needed */
5346 update_advertise_vrf_routes(bgp_vrf
);
5348 /* install all remote routes belonging to this l3vni into correspondng
5350 install_routes_for_vrf(bgp_vrf
);
5355 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5357 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5358 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5359 struct listnode
*node
= NULL
;
5360 struct listnode
*next
= NULL
;
5361 struct bgpevpn
*vpn
= NULL
;
5363 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5367 "Cannot process L3VNI %u Del - Could not find BGP instance",
5372 bgp_def
= bgp_get_default();
5376 "Cannot process L3VNI %u Del - Could not find default BGP instance",
5381 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5382 * bgp_delete would not remove/decrement bgp_info of the ip_prefix
5383 * routes. This will uninstalling the routes from zebra and decremnt the
5386 uninstall_routes_for_vrf(bgp_vrf
);
5388 /* delete/withdraw all type-5 routes */
5389 delete_withdraw_vrf_routes(bgp_vrf
);
5391 /* remove the l3vni from vrf instance */
5394 /* remove the Rmac from the BGP vrf */
5395 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5398 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5399 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5400 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5402 if (!list_isempty(bgp_vrf
->vrf_export_rtl
)) {
5403 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5406 /* update all corresponding local mac-ip routes */
5407 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5408 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5409 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5410 update_routes_for_vni(bgp_def
, vpn
);
5414 /* If any L2VNIs point to this instance, unlink them. */
5415 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5416 bgpevpn_unlink_from_l3vni(vpn
);
5418 /* Delete the instance if it was autocreated */
5419 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5420 bgp_delete(bgp_vrf
);
5426 * Handle del of a local VNI.
5428 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5430 struct bgpevpn
*vpn
;
5432 /* Locate VNI hash */
5433 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5435 if (bgp_debug_zebra(NULL
))
5437 "%u: VNI hash entry for VNI %u not found at DEL",
5442 /* Remove all local EVPN routes and schedule for processing (to
5443 * withdraw from peers).
5445 delete_routes_for_vni(bgp
, vpn
);
5448 * tunnel is no longer active, del tunnel ip address from tip_hash
5450 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5452 /* Clear "live" flag and see if hash needs to be freed. */
5453 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5454 if (!is_vni_configured(vpn
))
5455 bgp_evpn_free(bgp
, vpn
);
5461 * Handle add (or update) of a local VNI. The VNI changes we care
5462 * about are for the local-tunnel-ip and the (tenant) VRF.
5464 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5465 struct in_addr originator_ip
, vrf_id_t tenant_vrf_id
)
5467 struct bgpevpn
*vpn
;
5468 struct prefix_evpn p
;
5470 /* Lookup VNI. If present and no change, exit. */
5471 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5474 if (is_vni_live(vpn
)
5475 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5476 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5477 /* Probably some other param has changed that we don't
5481 /* Update tenant_vrf_id if it has changed. */
5482 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5483 bgpevpn_unlink_from_l3vni(vpn
);
5484 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5485 bgpevpn_link_to_l3vni(vpn
);
5488 /* If tunnel endpoint IP has changed, update (and delete prior
5489 * type-3 route, if needed.)
5491 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5492 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5494 /* Update all routes with new endpoint IP and/or export RT
5497 if (is_vni_live(vpn
))
5498 update_routes_for_vni(bgp
, vpn
);
5501 /* Create or update as appropriate. */
5503 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
);
5507 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5513 /* if the VNI is live already, there is nothing more to do */
5514 if (is_vni_live(vpn
))
5517 /* Mark as "live" */
5518 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5520 /* tunnel is now active, add tunnel-ip to db */
5521 bgp_tip_add(bgp
, &originator_ip
);
5523 /* filter routes as nexthop database has changed */
5524 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5526 /* Create EVPN type-3 route and schedule for processing. */
5527 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5528 if (update_evpn_route(bgp
, vpn
, &p
, 0)) {
5529 flog_err(BGP_ERR_EVPN_ROUTE_CREATE
,
5530 "%u: Type3 route creation failure for VNI %u",
5535 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5539 install_routes_for_vni(bgp
, vpn
);
5541 /* If we are advertising gateway mac-ip
5542 It needs to be conveyed again to zebra */
5543 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5549 * bgp_evpn_local_es_del
5551 int bgp_evpn_local_es_del(struct bgp
*bgp
,
5553 struct ipaddr
*originator_ip
)
5555 char buf
[ESI_STR_LEN
];
5556 struct evpnes
*es
= NULL
;
5558 if (!bgp
->esihash
) {
5559 flog_err(BGP_ERR_ES_CREATE
, "%u: ESI hash not yet created",
5564 /* Lookup ESI hash - should exist. */
5565 es
= bgp_evpn_lookup_es(bgp
, esi
);
5567 zlog_warn("%u: ESI hash entry for ESI %s at Local ES DEL",
5569 esi_to_str(esi
, buf
, sizeof(buf
)));
5573 /* Delete all local EVPN ES routes from ESI table
5574 * and schedule for processing (to withdraw from peers))
5576 delete_routes_for_es(bgp
, es
);
5578 /* free the hash entry */
5579 bgp_evpn_es_free(bgp
, es
);
5585 * bgp_evpn_local_es_add
5587 int bgp_evpn_local_es_add(struct bgp
*bgp
,
5589 struct ipaddr
*originator_ip
)
5591 char buf
[ESI_STR_LEN
];
5592 struct evpnes
*es
= NULL
;
5593 struct prefix_evpn p
;
5595 if (!bgp
->esihash
) {
5596 flog_err(BGP_ERR_ES_CREATE
, "%u: ESI hash not yet created",
5601 /* create the new es */
5602 es
= bgp_evpn_lookup_es(bgp
, esi
);
5604 es
= bgp_evpn_es_new(bgp
, esi
, originator_ip
);
5608 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
5609 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5613 UNSET_FLAG(es
->flags
, EVPNES_REMOTE
);
5614 SET_FLAG(es
->flags
, EVPNES_LOCAL
);
5616 build_evpn_type4_prefix(&p
, esi
, originator_ip
->ipaddr_v4
);
5617 if (update_evpn_type4_route(bgp
, es
, &p
)) {
5618 flog_err(BGP_ERR_EVPN_ROUTE_CREATE
,
5619 "%u: Type4 route creation failure for ESI %s",
5620 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5624 /* import all remote ES routes in th ES table */
5625 install_routes_for_es(bgp
, es
);
5631 * Cleanup EVPN information on disable - Need to delete and withdraw
5632 * EVPN routes from peers.
5634 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5636 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
5637 void *))cleanup_vni_on_disable
,
5642 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5643 * BGP instance (default) is being freed.
5645 void bgp_evpn_cleanup(struct bgp
*bgp
)
5647 hash_iterate(bgp
->vnihash
,
5648 (void (*)(struct hash_backet
*, void *))free_vni_entry
,
5651 hash_free(bgp
->import_rt_hash
);
5652 bgp
->import_rt_hash
= NULL
;
5654 hash_free(bgp
->vrf_import_rt_hash
);
5655 bgp
->vrf_import_rt_hash
= NULL
;
5657 hash_free(bgp
->vnihash
);
5658 bgp
->vnihash
= NULL
;
5660 hash_free(bgp
->esihash
);
5661 bgp
->esihash
= NULL
;
5663 list_delete_and_null(&bgp
->vrf_import_rtl
);
5664 list_delete_and_null(&bgp
->vrf_export_rtl
);
5665 list_delete_and_null(&bgp
->l2vnis
);
5669 * Initialization for EVPN
5672 * hash for RT to VNI
5674 void bgp_evpn_init(struct bgp
*bgp
)
5677 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
5679 hash_create(esi_hash_keymake
, esi_cmp
,
5680 "BGP EVPN Local ESI Hash");
5681 bgp
->import_rt_hash
=
5682 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
5683 "BGP Import RT Hash");
5684 bgp
->vrf_import_rt_hash
=
5685 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
5686 "BGP VRF Import RT Hash");
5687 bgp
->vrf_import_rtl
= list_new();
5688 bgp
->vrf_import_rtl
->cmp
=
5689 (int (*)(void *, void *))evpn_route_target_cmp
;
5690 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
5691 bgp
->vrf_export_rtl
= list_new();
5692 bgp
->vrf_export_rtl
->cmp
=
5693 (int (*)(void *, void *))evpn_route_target_cmp
;
5694 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
5695 bgp
->l2vnis
= list_new();
5696 bgp
->l2vnis
->cmp
= (int (*)(void *, void *))vni_hash_cmp
;
5699 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
5701 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);