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_aspath.h"
47 #include "bgpd/bgp_zebra.h"
48 #include "bgpd/bgp_nexthop.h"
51 * Definitions and external declarations.
53 extern struct zclient
*zclient
;
55 DEFINE_QOBJ_TYPE(bgpevpn
)
56 DEFINE_QOBJ_TYPE(evpnes
)
60 * Static function declarations
62 static void delete_evpn_route_entry(struct bgp
*bgp
,
63 afi_t afi
, safi_t safi
,
65 struct bgp_info
**ri
);
66 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
72 /* compare two IPV4 VTEP IPs */
73 static int evpn_vtep_ip_cmp(const void *p1
, const void *p2
)
75 const struct in_addr
*ip1
= p1
;
76 const struct in_addr
*ip2
= p2
;
82 return (ip1
->s_addr
== ip2
->s_addr
);
86 * Make hash key for ESI.
88 static unsigned int esi_hash_keymake(void *p
)
90 struct evpnes
*pes
= p
;
91 const void *pnt
= (void *)pes
->esi
.val
;
93 return jhash(pnt
, ESI_BYTES
, 0xa5a5a55a);
99 static int esi_cmp(const void *p1
, const void *p2
)
101 const struct evpnes
*pes1
= p1
;
102 const struct evpnes
*pes2
= p2
;
104 if (pes1
== NULL
&& pes2
== NULL
)
107 if (pes1
== NULL
|| pes2
== NULL
)
110 return (memcmp(pes1
->esi
.val
, pes2
->esi
.val
, ESI_BYTES
) == 0);
116 static unsigned int vni_hash_key_make(void *p
)
118 struct bgpevpn
*vpn
= p
;
119 return (jhash_1word(vpn
->vni
, 0));
123 * Comparison function for vni hash
125 static int vni_hash_cmp(const void *p1
, const void *p2
)
127 const struct bgpevpn
*vpn1
= p1
;
128 const struct bgpevpn
*vpn2
= p2
;
134 return (vpn1
->vni
== vpn2
->vni
);
138 * Make vrf import route target hash key.
140 static unsigned int vrf_import_rt_hash_key_make(void *p
)
142 struct vrf_irt_node
*irt
= p
;
143 char *pnt
= irt
->rt
.val
;
145 return jhash(pnt
, 8, 0x5abc1234);
149 * Comparison function for vrf import rt hash
151 static int vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
153 const struct vrf_irt_node
*irt1
= p1
;
154 const struct vrf_irt_node
*irt2
= p2
;
156 if (irt1
== NULL
&& irt2
== NULL
)
159 if (irt1
== NULL
|| irt2
== NULL
)
162 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
166 * Create a new vrf import_rt in default instance
168 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
170 struct bgp
*bgp_def
= NULL
;
171 struct vrf_irt_node
*irt
;
173 bgp_def
= bgp_get_default();
175 zlog_err("vrf import rt new - def instance not created yet");
179 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
180 sizeof(struct vrf_irt_node
));
185 irt
->vrfs
= list_new();
188 if (!hash_get(bgp_def
->vrf_import_rt_hash
, irt
, hash_alloc_intern
)) {
189 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
197 * Free the vrf import rt node
199 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
201 struct bgp
*bgp_def
= NULL
;
203 bgp_def
= bgp_get_default();
205 zlog_err("vrf import rt free - def instance not created yet");
209 hash_release(bgp_def
->vrf_import_rt_hash
, irt
);
210 list_delete_and_null(&irt
->vrfs
);
211 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
215 * Function to lookup Import RT node - used to map a RT to set of
216 * VNIs importing routes with that RT.
218 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
220 struct bgp
*bgp_def
= NULL
;
221 struct vrf_irt_node
*irt
;
222 struct vrf_irt_node tmp
;
224 bgp_def
= bgp_get_default();
226 zlog_err("vrf import rt lookup - def instance not created yet");
230 memset(&tmp
, 0, sizeof(struct vrf_irt_node
));
231 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
232 irt
= hash_lookup(bgp_def
->vrf_import_rt_hash
, &tmp
);
237 * Is specified VRF present on the RT's list of "importing" VRFs?
239 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
, struct bgp
*bgp_vrf
)
241 struct listnode
*node
= NULL
, *nnode
= NULL
;
242 struct bgp
*tmp_bgp_vrf
= NULL
;
244 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
245 if (tmp_bgp_vrf
== bgp_vrf
)
252 * Make import route target hash key.
254 static unsigned int import_rt_hash_key_make(void *p
)
256 struct irt_node
*irt
= p
;
257 char *pnt
= irt
->rt
.val
;
259 return jhash(pnt
, 8, 0xdeadbeef);
263 * Comparison function for import rt hash
265 static int import_rt_hash_cmp(const void *p1
, const void *p2
)
267 const struct irt_node
*irt1
= p1
;
268 const struct irt_node
*irt2
= p2
;
270 if (irt1
== NULL
&& irt2
== NULL
)
273 if (irt1
== NULL
|| irt2
== NULL
)
276 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
280 * Create a new import_rt
282 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
283 struct ecommunity_val
*rt
)
285 struct irt_node
*irt
;
290 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
295 irt
->vnis
= list_new();
298 if (!hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
)) {
299 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
307 * Free the import rt node
309 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
311 hash_release(bgp
->import_rt_hash
, irt
);
312 list_delete_and_null(&irt
->vnis
);
313 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
317 * Function to lookup Import RT node - used to map a RT to set of
318 * VNIs importing routes with that RT.
320 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
321 struct ecommunity_val
*rt
)
323 struct irt_node
*irt
;
326 memset(&tmp
, 0, sizeof(struct irt_node
));
327 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
328 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
333 * Is specified VNI present on the RT's list of "importing" VNIs?
335 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
337 struct listnode
*node
, *nnode
;
338 struct bgpevpn
*tmp_vpn
;
340 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
349 * Compare Route Targets.
351 static int evpn_route_target_cmp(struct ecommunity
*ecom1
,
352 struct ecommunity
*ecom2
)
360 if (!ecom1
&& !ecom2
)
363 if (ecom1
->str
&& !ecom2
->str
)
366 if (!ecom1
->str
&& ecom2
->str
)
369 if (!ecom1
->str
&& !ecom2
->str
)
372 return strcmp(ecom1
->str
, ecom2
->str
);
375 static void evpn_xxport_delete_ecomm(void *val
)
377 struct ecommunity
*ecomm
= val
;
378 ecommunity_free(&ecomm
);
382 * Mask off global-admin field of specified extended community (RT),
383 * just retain the local-admin field.
385 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
386 struct ecommunity_val
*src
)
392 if (type
== ECOMMUNITY_ENCODE_AS
) {
393 dst
->val
[2] = dst
->val
[3] = 0;
394 } else if (type
== ECOMMUNITY_ENCODE_AS4
395 || type
== ECOMMUNITY_ENCODE_IP
) {
396 dst
->val
[2] = dst
->val
[3] = 0;
397 dst
->val
[4] = dst
->val
[5] = 0;
402 * Map one RT to specified VRF.
403 * bgp_vrf = BGP vrf instance
405 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct ecommunity_val
*eval
)
407 struct vrf_irt_node
*irt
= NULL
;
408 struct ecommunity_val eval_tmp
;
410 /* If using "automatic" RT,
411 * we only care about the local-admin sub-field.
412 * This is to facilitate using L3VNI(VRF-VNI)
413 * as the RT for EBGP peering too.
415 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
416 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
417 mask_ecom_global_admin(&eval_tmp
, eval
);
419 irt
= lookup_vrf_import_rt(&eval_tmp
);
420 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
421 /* Already mapped. */
425 irt
= vrf_import_rt_new(&eval_tmp
);
427 /* Add VRF to the list for this RT. */
428 listnode_add(irt
->vrfs
, bgp_vrf
);
432 * Unmap specified VRF from specified RT. If there are no other
433 * VRFs for this RT, then the RT hash is deleted.
434 * bgp_vrf: BGP VRF specific instance
436 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
, struct vrf_irt_node
*irt
)
438 /* Delete VRF from list for this RT. */
439 listnode_delete(irt
->vrfs
, bgp_vrf
);
440 if (!listnode_head(irt
->vrfs
)) {
441 vrf_import_rt_free(irt
);
446 * Map one RT to specified VNI.
448 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
449 struct ecommunity_val
*eval
)
451 struct irt_node
*irt
;
452 struct ecommunity_val eval_tmp
;
454 /* If using "automatic" RT, we only care about the local-admin
456 * This is to facilitate using VNI as the RT for EBGP peering too.
458 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
459 if (!is_import_rt_configured(vpn
))
460 mask_ecom_global_admin(&eval_tmp
, eval
);
462 irt
= lookup_import_rt(bgp
, &eval_tmp
);
464 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
465 /* Already mapped. */
469 irt
= import_rt_new(bgp
, &eval_tmp
);
473 /* Add VNI to the hash list for this RT. */
474 listnode_add(irt
->vnis
, vpn
);
478 * Unmap specified VNI from specified RT. If there are no other
479 * VNIs for this RT, then the RT hash is deleted.
481 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
482 struct irt_node
*irt
)
484 /* Delete VNI from hash list for this RT. */
485 listnode_delete(irt
->vnis
, vpn
);
486 if (!listnode_head(irt
->vnis
)) {
487 import_rt_free(bgp
, irt
);
492 * Create RT extended community automatically from passed information:
493 * of the form AS:VNI.
494 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
495 * the need is to get a RT value that will be unique across different
496 * VNIs but the same across routers (in the same AS) for a particular
499 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
501 struct ecommunity_val eval
;
502 struct ecommunity
*ecomadd
;
504 if (bgp
->advertise_autort_rfc8365
)
505 vni
|= EVPN_AUTORT_VXLAN
;
506 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
508 ecomadd
= ecommunity_new();
509 ecommunity_add_val(ecomadd
, &eval
);
510 listnode_add_sort(rtl
, ecomadd
);
514 * Derive RD and RT for a VNI automatically. Invoked at the time of
517 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
519 bgp_evpn_derive_auto_rd(bgp
, vpn
);
520 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
521 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
525 * Convert nexthop (remote VTEP IP) into an IPv6 address.
527 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
529 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
531 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
532 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
536 * Add (update) or delete MACIP from zebra.
538 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
539 struct prefix_evpn
*p
,
540 struct in_addr remote_vtep_ip
, int add
,
545 char buf1
[ETHER_ADDR_STRLEN
];
546 char buf2
[INET6_ADDRSTRLEN
];
547 char buf3
[INET6_ADDRSTRLEN
];
550 if (!zclient
|| zclient
->sock
< 0)
553 /* Don't try to register if Zebra doesn't know of this instance. */
554 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
560 zclient_create_header(
561 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
563 stream_putl(s
, vpn
->vni
);
564 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
565 /* IP address length and IP address, if any. */
566 if (is_evpn_prefix_ipaddr_none(p
))
569 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
571 stream_putl(s
, ipa_len
);
572 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
574 stream_put_in_addr(s
, &remote_vtep_ip
);
576 /* TX flags - MAC sticky status and/or gateway mac */
578 stream_putc(s
, flags
);
580 stream_putw_at(s
, 0, stream_get_endp(s
));
582 if (bgp_debug_zebra(NULL
))
584 "Tx %s MACIP, VNI %u MAC %s IP %s (flags: 0x%x) remote VTEP %s",
585 add
? "ADD" : "DEL", vpn
->vni
,
586 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
588 ipaddr2str(&p
->prefix
.macip_addr
.ip
,
589 buf3
, sizeof(buf3
)), flags
,
590 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
593 return zclient_send_message(zclient
);
597 * Add (update) or delete remote VTEP from zebra.
599 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
600 struct prefix_evpn
*p
, int add
)
605 if (!zclient
|| zclient
->sock
< 0)
608 /* Don't try to register if Zebra doesn't know of this instance. */
609 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
615 zclient_create_header(
616 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
618 stream_putl(s
, vpn
->vni
);
619 if (is_evpn_prefix_ipaddr_v4(p
))
620 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
621 else if (is_evpn_prefix_ipaddr_v6(p
)) {
623 "Bad remote IP when trying to %s remote VTEP for VNI %u",
624 add
? "ADD" : "DEL", vpn
->vni
);
628 stream_putw_at(s
, 0, stream_get_endp(s
));
630 if (bgp_debug_zebra(NULL
))
631 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
632 add
? "ADD" : "DEL", vpn
->vni
,
633 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
635 return zclient_send_message(zclient
);
639 * Build extended community for EVPN ES (type-4) route
641 static void build_evpn_type4_route_extcomm(struct evpnes
*es
,
644 struct ecommunity ecom_encap
;
645 struct ecommunity ecom_es_rt
;
646 struct ecommunity_val eval
;
647 struct ecommunity_val eval_es_rt
;
648 bgp_encap_types tnl_type
;
652 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
653 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
654 encode_encap_extcomm(tnl_type
, &eval
);
656 ecom_encap
.val
= (uint8_t *)eval
.val
;
657 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
660 memset(&mac
, 0, sizeof(struct ethaddr
));
661 memset(&ecom_es_rt
, 0, sizeof(ecom_es_rt
));
662 es_get_system_mac(&es
->esi
, &mac
);
663 encode_es_rt_extcomm(&eval_es_rt
, &mac
);
665 ecom_es_rt
.val
= (uint8_t *)eval_es_rt
.val
;
667 ecommunity_merge(attr
->ecommunity
, &ecom_es_rt
);
669 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
673 * Build extended communities for EVPN prefix route.
675 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
678 struct ecommunity ecom_encap
;
679 struct ecommunity ecom_rmac
;
680 struct ecommunity_val eval
;
681 struct ecommunity_val eval_rmac
;
682 bgp_encap_types tnl_type
;
683 struct listnode
*node
, *nnode
;
684 struct ecommunity
*ecom
;
685 struct list
*vrf_export_rtl
= NULL
;
688 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
689 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
690 encode_encap_extcomm(tnl_type
, &eval
);
692 ecom_encap
.val
= (uint8_t *)eval
.val
;
695 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
697 /* Add the export RTs for L3VNI/VRF */
698 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
699 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
701 ecommunity_merge(attr
->ecommunity
, ecom
);
703 /* add the router mac extended community */
704 if (!is_zero_mac(&attr
->rmac
)) {
705 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
706 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
708 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
710 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
713 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
717 * Build extended communities for EVPN route.
718 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
719 * and ENCAP extended communities are applicable for all routes.
720 * The default gateway extended community and MAC mobility (sticky) extended
721 * community are added as needed based on passed settings - only for type-2
722 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
723 * added, if present, based on passed settings - only for non-link-local
726 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
729 struct ecommunity ecom_encap
;
730 struct ecommunity ecom_sticky
;
731 struct ecommunity ecom_default_gw
;
732 struct ecommunity ecom_rmac
;
733 struct ecommunity ecom_na
;
734 struct ecommunity_val eval
;
735 struct ecommunity_val eval_sticky
;
736 struct ecommunity_val eval_default_gw
;
737 struct ecommunity_val eval_rmac
;
738 struct ecommunity_val eval_na
;
740 bgp_encap_types tnl_type
;
741 struct listnode
*node
, *nnode
;
742 struct ecommunity
*ecom
;
744 struct list
*vrf_export_rtl
= NULL
;
747 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
748 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
749 encode_encap_extcomm(tnl_type
, &eval
);
751 ecom_encap
.val
= (uint8_t *)eval
.val
;
754 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
756 /* Add the export RTs for L2VNI */
757 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
758 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
760 /* Add the export RTs for L3VNI if told to - caller determines
761 * when this should be done.
764 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
765 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
766 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
768 attr
->ecommunity
= ecommunity_merge(
769 attr
->ecommunity
, ecom
);
773 /* Add MAC mobility (sticky) if needed. */
776 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
777 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
778 ecom_sticky
.size
= 1;
779 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
781 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
784 /* Add RMAC, if told to. */
786 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
787 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
789 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
791 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
794 /* Add default gateway, if needed. */
795 if (attr
->default_gw
) {
796 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
797 encode_default_gw_extcomm(&eval_default_gw
);
798 ecom_default_gw
.size
= 1;
799 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
801 ecommunity_merge(attr
->ecommunity
, &ecom_default_gw
);
804 if (attr
->router_flag
) {
805 memset(&ecom_na
, 0, sizeof(ecom_na
));
806 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
);
808 ecom_na
.val
= (uint8_t *)eval_na
.val
;
809 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
813 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
817 * Add MAC mobility extended community to attribute.
819 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
821 struct ecommunity ecom_tmp
;
822 struct ecommunity_val eval
;
823 uint8_t *ecom_val_ptr
;
830 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
832 /* Find current MM ecommunity */
835 if (attr
->ecommunity
) {
836 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
837 pnt
= attr
->ecommunity
->val
+ (i
* 8);
841 if (type
== ECOMMUNITY_ENCODE_EVPN
843 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
844 ecom_val_ptr
= (uint8_t *)(attr
->ecommunity
->val
851 /* Update the existing MM ecommunity */
853 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
855 /* Add MM to existing */
857 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
859 ecom_tmp
.val
= (uint8_t *)eval
.val
;
861 if (attr
->ecommunity
)
863 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
865 attr
->ecommunity
= ecommunity_dup(&ecom_tmp
);
869 /* Install EVPN route into zebra. */
870 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
871 struct prefix_evpn
*p
,
872 struct in_addr remote_vtep_ip
, uint8_t flags
)
876 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
877 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
880 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 1);
885 /* Uninstall EVPN route from zebra. */
886 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
887 struct prefix_evpn
*p
,
888 struct in_addr remote_vtep_ip
)
892 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
893 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
896 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 0);
902 * Due to MAC mobility, the prior "local" best route has been supplanted
903 * by a "remote" best route. The prior route has to be deleted and withdrawn
906 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
908 struct bgp_info
*old_local
)
910 struct bgp_node
*global_rn
;
912 afi_t afi
= AFI_L2VPN
;
913 safi_t safi
= SAFI_EVPN
;
915 /* Locate route node in the global EVPN routing table. Note that
916 * this table is a 2-level tree (RD-level + Prefix-level) similar to
919 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
920 (struct prefix
*)&rn
->p
, &vpn
->prd
);
922 /* Delete route entry in the global EVPN table. */
923 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &ri
);
925 /* Schedule for processing - withdraws to peers happen from
929 bgp_process(bgp
, global_rn
, afi
, safi
);
930 bgp_unlock_node(global_rn
);
933 /* Delete route entry in the VNI route table, caller to remove. */
934 bgp_info_delete(rn
, old_local
);
937 static struct in_addr
*es_vtep_new(struct in_addr vtep
)
941 ip
= XCALLOC(MTYPE_BGP_EVPN_ES_VTEP
, sizeof(struct in_addr
));
945 ip
->s_addr
= vtep
.s_addr
;
949 static void es_vtep_free(struct in_addr
*ip
)
951 XFREE(MTYPE_BGP_EVPN_ES_VTEP
, ip
);
954 /* check if VTEP is already part of the list */
955 static int is_vtep_present_in_list(struct list
*list
,
958 struct listnode
*node
= NULL
;
961 for (ALL_LIST_ELEMENTS_RO(list
, node
, tmp
)) {
962 if (tmp
->s_addr
== vtep
.s_addr
)
969 * Best path for ES route was changed,
970 * update the list of VTEPs for this ES
972 static int evpn_es_install_vtep(struct bgp
*bgp
,
974 struct prefix_evpn
*p
,
975 struct in_addr rvtep
)
977 struct in_addr
*vtep_ip
;
979 if (is_vtep_present_in_list(es
->vtep_list
, rvtep
))
983 vtep_ip
= es_vtep_new(rvtep
);
985 listnode_add_sort(es
->vtep_list
, vtep_ip
);
990 * Best path for ES route was changed,
991 * update the list of VTEPs for this ES
993 static int evpn_es_uninstall_vtep(struct bgp
*bgp
,
995 struct prefix_evpn
*p
,
996 struct in_addr rvtep
)
998 struct listnode
*node
, *nnode
, *node_to_del
= NULL
;
1001 for (ALL_LIST_ELEMENTS(es
->vtep_list
, node
, nnode
, tmp
)) {
1002 if (tmp
->s_addr
== rvtep
.s_addr
) {
1009 list_delete_node(es
->vtep_list
, node_to_del
);
1015 * Calculate the best path for a ES(type-4) route.
1017 static int evpn_es_route_select_install(struct bgp
*bgp
,
1019 struct bgp_node
*rn
)
1022 afi_t afi
= AFI_L2VPN
;
1023 safi_t safi
= SAFI_EVPN
;
1024 struct bgp_info
*old_select
; /* old best */
1025 struct bgp_info
*new_select
; /* new best */
1026 struct bgp_info_pair old_and_new
;
1028 /* Compute the best path. */
1029 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
],
1030 &old_and_new
, afi
, safi
);
1031 old_select
= old_and_new
.old
;
1032 new_select
= old_and_new
.new;
1035 * If the best path hasn't changed - see if something needs to be
1038 if (old_select
&& old_select
== new_select
1039 && old_select
->type
== ZEBRA_ROUTE_BGP
1040 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1041 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1042 && !CHECK_FLAG(old_select
->flags
, BGP_INFO_ATTR_CHANGED
)
1043 && !bgp
->addpath_tx_used
[afi
][safi
]) {
1044 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1045 ret
= evpn_es_install_vtep(bgp
, es
,
1046 (struct prefix_evpn
*)&rn
->p
,
1047 old_select
->attr
->nexthop
);
1049 UNSET_FLAG(old_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
1050 bgp_zebra_clear_route_change_flags(rn
);
1054 /* If the user did a "clear" this flag will be set */
1055 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1058 * bestpath has changed; update relevant fields and install or uninstall
1059 * into the zebra RIB.
1061 if (old_select
|| new_select
)
1062 bgp_bump_version(rn
);
1065 bgp_info_unset_flag(rn
, old_select
, BGP_INFO_SELECTED
);
1067 bgp_info_set_flag(rn
, new_select
, BGP_INFO_SELECTED
);
1068 bgp_info_unset_flag(rn
, new_select
, BGP_INFO_ATTR_CHANGED
);
1069 UNSET_FLAG(new_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
1072 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1073 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1074 ret
= evpn_es_install_vtep(bgp
, es
,
1075 (struct prefix_evpn
*)&rn
->p
,
1076 new_select
->attr
->nexthop
);
1078 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1079 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1080 ret
= evpn_es_uninstall_vtep(
1081 bgp
, es
, (struct prefix_evpn
*)&rn
->p
,
1082 old_select
->attr
->nexthop
);
1085 /* Clear any route change flags. */
1086 bgp_zebra_clear_route_change_flags(rn
);
1088 /* Reap old select bgp_info, if it has been removed */
1089 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_INFO_REMOVED
))
1090 bgp_info_reap(rn
, old_select
);
1096 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1099 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1100 struct bgp_node
*rn
)
1102 struct bgp_info
*old_select
, *new_select
;
1103 struct bgp_info_pair old_and_new
;
1104 struct prefix_evpn
*evp
;
1105 afi_t afi
= AFI_L2VPN
;
1106 safi_t safi
= SAFI_EVPN
;
1110 /* Compute the best path. */
1111 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1113 old_select
= old_and_new
.old
;
1114 new_select
= old_and_new
.new;
1116 evp
= (struct prefix_evpn
*)&rn
->p
;
1117 /* If the best path hasn't changed - see if there is still something to
1121 if (old_select
&& old_select
== new_select
1122 && old_select
->type
== ZEBRA_ROUTE_BGP
1123 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
1124 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
1125 && !CHECK_FLAG(old_select
->flags
, BGP_INFO_ATTR_CHANGED
)
1126 && !bgp
->addpath_tx_used
[afi
][safi
]) {
1127 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
1128 if (old_select
->attr
->sticky
)
1129 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
1130 if (old_select
->attr
->default_gw
)
1131 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
1132 if (is_evpn_prefix_ipaddr_v6(evp
) &&
1133 old_select
->attr
->router_flag
)
1134 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
1136 ret
= evpn_zebra_install(
1137 bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1138 old_select
->attr
->nexthop
, flags
);
1140 UNSET_FLAG(old_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
1141 bgp_zebra_clear_route_change_flags(rn
);
1145 /* If the user did a "clear" this flag will be set */
1146 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
1148 /* bestpath has changed; update relevant fields and install or uninstall
1149 * into the zebra RIB.
1151 if (old_select
|| new_select
)
1152 bgp_bump_version(rn
);
1155 bgp_info_unset_flag(rn
, old_select
, BGP_INFO_SELECTED
);
1157 bgp_info_set_flag(rn
, new_select
, BGP_INFO_SELECTED
);
1158 bgp_info_unset_flag(rn
, new_select
, BGP_INFO_ATTR_CHANGED
);
1159 UNSET_FLAG(new_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
1162 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1163 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
1165 if (new_select
->attr
->sticky
)
1166 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
1167 if (new_select
->attr
->default_gw
)
1168 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
1169 if (is_evpn_prefix_ipaddr_v6(evp
) &&
1170 new_select
->attr
->router_flag
)
1171 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
1173 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
1174 new_select
->attr
->nexthop
, flags
);
1175 /* If an old best existed and it was a "local" route, the only
1177 * it would be supplanted is due to MAC mobility procedures. So,
1179 * need to do an implicit delete and withdraw that route from
1182 if (old_select
&& old_select
->peer
== bgp
->peer_self
1183 && old_select
->type
== ZEBRA_ROUTE_BGP
1184 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
1185 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
1187 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1188 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1189 ret
= evpn_zebra_uninstall(bgp
, vpn
,
1190 (struct prefix_evpn
*)&rn
->p
,
1191 old_select
->attr
->nexthop
);
1194 /* Clear any route change flags. */
1195 bgp_zebra_clear_route_change_flags(rn
);
1197 /* Reap old select bgp_info, if it has been removed */
1198 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_INFO_REMOVED
))
1199 bgp_info_reap(rn
, old_select
);
1205 * Return true if the local ri for this rn is of type gateway mac
1207 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
1209 struct bgp_info
*tmp_ri
= NULL
;
1210 struct bgp_info
*local_ri
= NULL
;
1213 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1214 if (tmp_ri
->peer
== bgp
->peer_self
1215 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1216 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1223 return local_ri
->attr
->default_gw
;
1228 * Return true if the local ri for this rn has sticky set
1230 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
1232 struct bgp_info
*tmp_ri
;
1233 struct bgp_info
*local_ri
;
1236 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1237 if (tmp_ri
->peer
== bgp
->peer_self
1238 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1239 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1246 return local_ri
->attr
->sticky
;
1250 * create or update EVPN type4 route entry.
1251 * This could be in the ES table or the global table.
1252 * TODO: handle remote ES (type4) routes as well
1254 static int update_evpn_type4_route_entry(struct bgp
*bgp
,
1256 afi_t afi
, safi_t safi
,
1257 struct bgp_node
*rn
,
1260 struct bgp_info
**ri
,
1263 char buf
[ESI_STR_LEN
];
1264 char buf1
[INET6_ADDRSTRLEN
];
1265 struct bgp_info
*tmp_ri
= NULL
;
1266 struct bgp_info
*local_ri
= NULL
; /* local route entry if any */
1267 struct bgp_info
*remote_ri
= NULL
; /* remote route entry if any */
1268 struct attr
*attr_new
= NULL
;
1269 struct prefix_evpn
*evp
= NULL
;
1273 evp
= (struct prefix_evpn
*)&rn
->p
;
1275 /* locate the local and remote entries if any */
1276 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1277 if (tmp_ri
->peer
== bgp
->peer_self
&&
1278 tmp_ri
->type
== ZEBRA_ROUTE_BGP
&&
1279 tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1281 if (tmp_ri
->type
== ZEBRA_ROUTE_BGP
&&
1282 tmp_ri
->sub_type
== BGP_ROUTE_IMPORTED
&&
1283 CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
))
1287 /* we don't expect to see a remote_ri at this point.
1288 * An ES route has esi + vtep_ip as the key,
1289 * We shouldn't see the same route from any other vtep.
1293 "%u ERROR: local es route for ESI: %s Vtep %s also learnt from remote",
1295 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1296 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1300 if (!local_ri
&& !add
)
1303 /* create or update the entry */
1306 /* Add or update attribute to hash */
1307 attr_new
= bgp_attr_intern(attr
);
1309 /* Create new route with its attribute. */
1310 tmp_ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
,
1311 0, bgp
->peer_self
, attr_new
, rn
);
1312 SET_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
);
1314 /* add the newly created path to the route-node */
1315 bgp_info_add(rn
, tmp_ri
);
1318 if (attrhash_cmp(tmp_ri
->attr
, attr
)
1319 && !CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1322 /* The attribute has changed.
1323 * Add (or update) attribute to hash. */
1324 attr_new
= bgp_attr_intern(attr
);
1325 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1327 /* Restore route, if needed. */
1328 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1329 bgp_info_restore(rn
, tmp_ri
);
1331 /* Unintern existing, set to new. */
1332 bgp_attr_unintern(&tmp_ri
->attr
);
1333 tmp_ri
->attr
= attr_new
;
1334 tmp_ri
->uptime
= bgp_clock();
1338 /* Return back the route entry. */
1343 /* update evpn es (type-4) route */
1344 static int update_evpn_type4_route(struct bgp
*bgp
,
1346 struct prefix_evpn
*p
)
1349 int route_changed
= 0;
1350 char buf
[ESI_STR_LEN
];
1351 char buf1
[INET6_ADDRSTRLEN
];
1352 afi_t afi
= AFI_L2VPN
;
1353 safi_t safi
= SAFI_EVPN
;
1355 struct attr
*attr_new
= NULL
;
1356 struct bgp_node
*rn
= NULL
;
1357 struct bgp_info
*ri
= NULL
;
1359 memset(&attr
, 0, sizeof(struct attr
));
1361 /* Build path-attribute for this route. */
1362 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1363 attr
.nexthop
= es
->originator_ip
.ipaddr_v4
;
1364 attr
.mp_nexthop_global_in
= es
->originator_ip
.ipaddr_v4
;
1365 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1367 /* Set up extended community. */
1368 build_evpn_type4_route_extcomm(es
, &attr
);
1370 /* First, create (or fetch) route node within the ESI. */
1371 /* NOTE: There is no RD here. */
1372 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
1374 /* Create or update route entry. */
1375 ret
= update_evpn_type4_route_entry(bgp
, es
, afi
, safi
, rn
,
1379 zlog_err("%u ERROR: Failed to updated ES route ESI: %s VTEP %s",
1381 esi_to_str(&p
->prefix
.es_addr
.esi
, buf
, sizeof(buf
)),
1382 ipaddr2str(&es
->originator_ip
, buf1
, sizeof(buf1
)));
1386 attr_new
= ri
->attr
;
1388 /* Perform route selection;
1389 * this is just to set the flags correctly
1390 * as local route in the ES always wins.
1392 evpn_es_route_select_install(bgp
, es
, rn
);
1393 bgp_unlock_node(rn
);
1395 /* If this is a new route or some attribute has changed, export the
1396 * route to the global table. The route will be advertised to peers
1397 * from there. Note that this table is a 2-level tree (RD-level +
1398 * Prefix-level) similar to L3VPN routes.
1400 if (route_changed
) {
1401 struct bgp_info
*global_ri
;
1403 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1404 (struct prefix
*)p
, &es
->prd
);
1405 update_evpn_type4_route_entry(bgp
, es
, afi
, safi
,
1410 /* Schedule for processing and unlock node. */
1411 bgp_process(bgp
, rn
, afi
, safi
);
1412 bgp_unlock_node(rn
);
1415 /* Unintern temporary. */
1416 aspath_unintern(&attr
.aspath
);
1420 static int update_evpn_type5_route_entry(struct bgp
*bgp_def
,
1421 struct bgp
*bgp_vrf
, afi_t afi
,
1422 safi_t safi
, struct bgp_node
*rn
,
1423 struct attr
*attr
, int *route_changed
)
1425 struct attr
*attr_new
= NULL
;
1426 struct bgp_info
*ri
= NULL
;
1427 mpls_label_t label
= MPLS_INVALID_LABEL
;
1428 struct bgp_info
*local_ri
= NULL
;
1429 struct bgp_info
*tmp_ri
= NULL
;
1432 /* locate the local route entry if any */
1433 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1434 if (tmp_ri
->peer
== bgp_def
->peer_self
1435 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1436 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1441 * create a new route entry if one doesnt exist.
1442 * Otherwise see if route attr has changed
1446 /* route has changed as this is the first entry */
1449 /* Add (or update) attribute to hash. */
1450 attr_new
= bgp_attr_intern(attr
);
1452 /* create the route info from attribute */
1453 ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1454 bgp_def
->peer_self
, attr_new
, rn
);
1455 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1457 /* Type-5 routes advertise the L3-VNI */
1458 bgp_info_extra_get(ri
);
1459 vni2label(bgp_vrf
->l3vni
, &label
);
1460 memcpy(&ri
->extra
->label
, &label
, sizeof(label
));
1461 ri
->extra
->num_labels
= 1;
1463 /* add the route entry to route node*/
1464 bgp_info_add(rn
, ri
);
1468 if (!attrhash_cmp(tmp_ri
->attr
, attr
)) {
1470 /* attribute changed */
1473 /* The attribute has changed. */
1474 /* Add (or update) attribute to hash. */
1475 attr_new
= bgp_attr_intern(attr
);
1476 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1478 /* Restore route, if needed. */
1479 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1480 bgp_info_restore(rn
, tmp_ri
);
1482 /* Unintern existing, set to new. */
1483 bgp_attr_unintern(&tmp_ri
->attr
);
1484 tmp_ri
->attr
= attr_new
;
1485 tmp_ri
->uptime
= bgp_clock();
1491 /* update evpn type-5 route entry */
1492 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1493 struct attr
*src_attr
)
1495 afi_t afi
= AFI_L2VPN
;
1496 safi_t safi
= SAFI_EVPN
;
1498 struct bgp_node
*rn
= NULL
;
1499 struct bgp
*bgp_def
= NULL
;
1500 int route_changed
= 0;
1502 bgp_def
= bgp_get_default();
1506 /* Build path attribute for this route - use the source attr, if
1507 * present, else treat as locally originated.
1510 bgp_attr_dup(&attr
, src_attr
);
1512 memset(&attr
, 0, sizeof(struct attr
));
1513 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1515 /* Set nexthop to ourselves and fill in the Router MAC. */
1516 attr
.nexthop
= bgp_vrf
->originator_ip
;
1517 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1518 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1519 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
1521 /* Setup RT and encap extended community */
1522 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1524 /* get the route node in global table */
1525 rn
= bgp_afi_node_get(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1526 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1529 /* create or update the route entry within the route node */
1530 update_evpn_type5_route_entry(bgp_def
, bgp_vrf
, afi
, safi
, rn
, &attr
,
1533 /* schedule for processing and unlock node */
1534 if (route_changed
) {
1535 bgp_process(bgp_def
, rn
, afi
, safi
);
1536 bgp_unlock_node(rn
);
1539 /* uninten temporary */
1541 aspath_unintern(&attr
.aspath
);
1546 * Create or update EVPN route entry. This could be in the VNI route table
1547 * or the global route table.
1549 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1550 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1551 struct attr
*attr
, int add
, int vni_table
,
1552 struct bgp_info
**ri
, uint8_t flags
)
1554 struct bgp_info
*tmp_ri
;
1555 struct bgp_info
*local_ri
, *remote_ri
;
1556 struct attr
*attr_new
;
1557 mpls_label_t label
[BGP_MAX_LABELS
];
1558 uint32_t num_labels
= 1;
1559 int route_change
= 1;
1561 struct prefix_evpn
*evp
;
1564 evp
= (struct prefix_evpn
*)&rn
->p
;
1565 memset(&label
, 0, sizeof(label
));
1567 /* See if this is an update of an existing route, or a new add. Also,
1568 * identify if already known from remote, and if so, the one with the
1569 * highest sequence number; this is only when adding to the VNI routing
1572 local_ri
= remote_ri
= NULL
;
1573 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1574 if (tmp_ri
->peer
== bgp
->peer_self
1575 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1576 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1579 if (tmp_ri
->type
== ZEBRA_ROUTE_BGP
1580 && tmp_ri
->sub_type
== BGP_ROUTE_IMPORTED
1581 && CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
)) {
1584 else if (mac_mobility_seqnum(tmp_ri
->attr
)
1585 > mac_mobility_seqnum(remote_ri
->attr
))
1591 /* If route doesn't exist already, create a new one, if told to.
1592 * Otherwise act based on whether the attributes of the route have
1595 if (!local_ri
&& !add
)
1599 /* When learnt locally for the first time but already known from
1600 * remote, we have to initiate appropriate MAC mobility steps.
1601 * This is applicable when updating the VNI routing table.
1602 * We need to skip mobility steps for g/w macs (local mac on g/w
1603 * SVI) advertised in EVPN.
1604 * This will ensure that local routes are preferred for g/w macs
1606 if (remote_ri
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
)) {
1607 uint32_t cur_seqnum
;
1609 /* Add MM extended community to route. */
1610 cur_seqnum
= mac_mobility_seqnum(remote_ri
->attr
);
1611 add_mac_mobility_to_attr(cur_seqnum
+ 1, attr
);
1614 /* Add (or update) attribute to hash. */
1615 attr_new
= bgp_attr_intern(attr
);
1617 /* Extract MAC mobility sequence number, if any. */
1618 attr_new
->mm_seqnum
=
1619 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1620 attr_new
->sticky
= sticky
;
1622 /* Create new route with its attribute. */
1623 tmp_ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1624 bgp
->peer_self
, attr_new
, rn
);
1625 SET_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
);
1626 bgp_info_extra_get(tmp_ri
);
1628 /* The VNI goes into the 'label' field of the route */
1629 vni2label(vpn
->vni
, &label
[0]);
1631 /* Type-2 routes may carry a second VNI - the L3-VNI.
1632 * Only attach second label if we are advertising two labels for
1635 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1636 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1639 l3vni
= bgpevpn_get_l3vni(vpn
);
1641 vni2label(l3vni
, &label
[1]);
1646 memcpy(&tmp_ri
->extra
->label
, label
, sizeof(label
));
1647 tmp_ri
->extra
->num_labels
= num_labels
;
1648 bgp_info_add(rn
, tmp_ri
);
1651 if (attrhash_cmp(tmp_ri
->attr
, attr
)
1652 && !CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1656 * The attributes have changed, type-2 routes needs to
1657 * be advertised with right labels.
1659 vni2label(vpn
->vni
, &label
[0]);
1660 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1661 && CHECK_FLAG(vpn
->flags
,
1662 VNI_FLAG_USE_TWO_LABELS
)) {
1665 l3vni
= bgpevpn_get_l3vni(vpn
);
1667 vni2label(l3vni
, &label
[1]);
1671 memcpy(&tmp_ri
->extra
->label
, label
, sizeof(label
));
1672 tmp_ri
->extra
->num_labels
= num_labels
;
1674 /* The attribute has changed. */
1675 /* Add (or update) attribute to hash. */
1676 attr_new
= bgp_attr_intern(attr
);
1677 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1679 /* Restore route, if needed. */
1680 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1681 bgp_info_restore(rn
, tmp_ri
);
1683 /* Unintern existing, set to new. */
1684 bgp_attr_unintern(&tmp_ri
->attr
);
1685 tmp_ri
->attr
= attr_new
;
1686 tmp_ri
->uptime
= bgp_clock();
1690 /* Return back the route entry. */
1692 return route_change
;
1696 * Create or update EVPN route (of type based on prefix) for specified VNI
1697 * and schedule for processing.
1699 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1700 struct prefix_evpn
*p
, uint8_t flags
)
1702 struct bgp_node
*rn
;
1704 struct attr
*attr_new
;
1705 int add_l3_ecomm
= 0;
1706 struct bgp_info
*ri
;
1707 afi_t afi
= AFI_L2VPN
;
1708 safi_t safi
= SAFI_EVPN
;
1711 memset(&attr
, 0, sizeof(struct attr
));
1713 /* Build path-attribute for this route. */
1714 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1715 attr
.nexthop
= vpn
->originator_ip
;
1716 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1717 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1718 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1719 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1720 attr
.router_flag
= CHECK_FLAG(flags
,
1721 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1723 /* PMSI is only needed for type-3 routes */
1724 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
)
1725 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1727 /* router mac is only needed for type-2 routes here. */
1728 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1729 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1730 vni2label(vpn
->vni
, &(attr
.label
));
1732 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1733 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1736 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1737 (is_evpn_prefix_ipaddr_v4(p
) ||
1738 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1739 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1740 bgpevpn_get_l3vni(vpn
))
1743 /* Set up extended community. */
1744 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1746 /* First, create (or fetch) route node within the VNI. */
1747 /* NOTE: There is no RD here. */
1748 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1750 /* Create or update route entry. */
1751 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1754 attr_new
= ri
->attr
;
1756 /* Perform route selection; this is just to set the flags correctly
1757 * as local route in the VNI always wins.
1759 evpn_route_select_install(bgp
, vpn
, rn
);
1760 bgp_unlock_node(rn
);
1762 /* If this is a new route or some attribute has changed, export the
1763 * route to the global table. The route will be advertised to peers
1764 * from there. Note that this table is a 2-level tree (RD-level +
1765 * Prefix-level) similar to L3VPN routes.
1768 struct bgp_info
*global_ri
;
1770 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1771 (struct prefix
*)p
, &vpn
->prd
);
1772 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1, 0,
1775 /* Schedule for processing and unlock node. */
1776 bgp_process(bgp
, rn
, afi
, safi
);
1777 bgp_unlock_node(rn
);
1780 /* Unintern temporary. */
1781 aspath_unintern(&attr
.aspath
);
1787 * Delete EVPN route entry.
1788 * The entry can be in ESI/VNI table or the global table.
1790 static void delete_evpn_route_entry(struct bgp
*bgp
,
1791 afi_t afi
, safi_t safi
,
1792 struct bgp_node
*rn
,
1793 struct bgp_info
**ri
)
1795 struct bgp_info
*tmp_ri
;
1799 /* Now, find matching route. */
1800 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
)
1801 if (tmp_ri
->peer
== bgp
->peer_self
1802 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1803 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1808 /* Mark route for delete. */
1810 bgp_info_delete(rn
, tmp_ri
);
1815 /* Delete EVPN ES (type-4) route */
1816 static int delete_evpn_type4_route(struct bgp
*bgp
,
1818 struct prefix_evpn
*p
)
1820 afi_t afi
= AFI_L2VPN
;
1821 safi_t safi
= SAFI_EVPN
;
1822 struct bgp_info
*ri
;
1823 struct bgp_node
*rn
= NULL
; /* rn in esi table */
1824 struct bgp_node
*global_rn
= NULL
; /* rn in global table */
1826 /* First, locate the route node within the ESI.
1827 * If it doesn't exist, ther is nothing to do.
1828 * Note: there is no RD here.
1830 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
1834 /* Next, locate route node in the global EVPN routing table.
1835 * Note that this table is a 2-level tree (RD-level + Prefix-level)
1837 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1838 (struct prefix
*)p
, &es
->prd
);
1841 /* Delete route entry in the global EVPN table. */
1842 delete_evpn_route_entry(bgp
, afi
, safi
,
1845 /* Schedule for processing - withdraws to peers happen from
1849 bgp_process(bgp
, global_rn
, afi
, safi
);
1850 bgp_unlock_node(global_rn
);
1854 * Delete route entry in the ESI route table.
1855 * This can just be removed.
1857 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &ri
);
1859 bgp_info_reap(rn
, ri
);
1860 bgp_unlock_node(rn
);
1864 /* Delete EVPN type5 route */
1865 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1867 afi_t afi
= AFI_L2VPN
;
1868 safi_t safi
= SAFI_EVPN
;
1869 struct bgp_node
*rn
= NULL
;
1870 struct bgp_info
*ri
= NULL
;
1871 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
1873 bgp_def
= bgp_get_default();
1877 /* locate the global route entry for this type-5 prefix */
1878 rn
= bgp_afi_node_lookup(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1879 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1883 delete_evpn_route_entry(bgp_def
, afi
, safi
, rn
, &ri
);
1885 bgp_process(bgp_def
, rn
, afi
, safi
);
1886 bgp_unlock_node(rn
);
1891 * Delete EVPN route (of type based on prefix) for specified VNI and
1892 * schedule for processing.
1894 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1895 struct prefix_evpn
*p
)
1897 struct bgp_node
*rn
, *global_rn
;
1898 struct bgp_info
*ri
;
1899 afi_t afi
= AFI_L2VPN
;
1900 safi_t safi
= SAFI_EVPN
;
1902 /* First, locate the route node within the VNI. If it doesn't exist,
1904 * is nothing further to do.
1906 /* NOTE: There is no RD here. */
1907 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1911 /* Next, locate route node in the global EVPN routing table. Note that
1912 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1915 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1916 (struct prefix
*)p
, &vpn
->prd
);
1918 /* Delete route entry in the global EVPN table. */
1919 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &ri
);
1921 /* Schedule for processing - withdraws to peers happen from
1925 bgp_process(bgp
, global_rn
, afi
, safi
);
1926 bgp_unlock_node(global_rn
);
1929 /* Delete route entry in the VNI route table. This can just be removed.
1931 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &ri
);
1933 bgp_info_reap(rn
, ri
);
1934 bgp_unlock_node(rn
);
1940 * Update all type-2 (MACIP) local routes for this VNI - these should also
1941 * be scheduled for advertise to peers.
1943 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1947 struct bgp_node
*rn
;
1948 struct bgp_info
*ri
;
1950 struct attr attr_sticky
;
1951 struct attr attr_def_gw
;
1952 struct attr attr_ip6_ll
;
1953 struct attr
*attr_new
;
1954 int add_l3_ecomm
= 0;
1958 memset(&attr
, 0, sizeof(struct attr
));
1959 memset(&attr_sticky
, 0, sizeof(struct attr
));
1960 memset(&attr_def_gw
, 0, sizeof(struct attr
));
1961 memset(&attr_ip6_ll
, 0, sizeof(struct attr
));
1963 /* Build path-attribute - multiple type-2 routes for this VNI will share
1964 * the same path attribute, but we need separate structures for sticky
1965 * MACs, default gateway and IPv6 link-local addresses (no L3 RT/RMAC).
1967 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1968 bgp_attr_default_set(&attr_sticky
, BGP_ORIGIN_IGP
);
1969 bgp_attr_default_set(&attr_def_gw
, BGP_ORIGIN_IGP
);
1970 attr
.nexthop
= vpn
->originator_ip
;
1971 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1972 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1973 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1974 attr_sticky
.nexthop
= vpn
->originator_ip
;
1975 attr_sticky
.mp_nexthop_global_in
= vpn
->originator_ip
;
1976 attr_sticky
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1977 attr_sticky
.sticky
= 1;
1978 bgpevpn_get_rmac(vpn
, &attr_sticky
.rmac
);
1979 attr_def_gw
.nexthop
= vpn
->originator_ip
;
1980 attr_def_gw
.mp_nexthop_global_in
= vpn
->originator_ip
;
1981 attr_def_gw
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1982 attr_def_gw
.default_gw
= 1;
1983 bgpevpn_get_rmac(vpn
, &attr_def_gw
.rmac
);
1984 bgp_attr_default_set(&attr_ip6_ll
, BGP_ORIGIN_IGP
);
1985 attr_ip6_ll
.nexthop
= vpn
->originator_ip
;
1986 attr_ip6_ll
.mp_nexthop_global_in
= vpn
->originator_ip
;
1987 attr_ip6_ll
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1989 /* Add L3 VNI RTs and RMAC for non IPv6 link-local attributes if
1990 * using L3 VNI for type-2 routes also.
1992 if (CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1993 bgpevpn_get_l3vni(vpn
))
1996 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1997 build_evpn_route_extcomm(vpn
, &attr_sticky
, add_l3_ecomm
);
1998 build_evpn_route_extcomm(vpn
, &attr_def_gw
, add_l3_ecomm
);
1999 build_evpn_route_extcomm(vpn
, &attr_ip6_ll
, 0);
2001 /* Walk this VNI's route table and update local type-2 routes. For any
2002 * routes updated, update corresponding entry in the global table too.
2004 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2005 rn
= bgp_route_next(rn
)) {
2006 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2007 struct bgp_node
*rd_rn
;
2008 struct bgp_info
*global_ri
;
2010 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2013 if (is_evpn_prefix_ipaddr_v6(evp
) &&
2014 IN6_IS_ADDR_LINKLOCAL(&evp
->prefix
.macip_addr
.ip
.ipaddr_v6
))
2015 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
2016 &attr_ip6_ll
, 0, 1, &ri
, 0);
2018 if (evpn_route_is_sticky(bgp
, rn
))
2019 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
2020 &attr_sticky
, 0, 1, &ri
,
2022 else if (evpn_route_is_def_gw(bgp
, rn
)) {
2023 if (is_evpn_prefix_ipaddr_v6(evp
))
2024 attr_def_gw
.router_flag
= 1;
2025 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
2026 &attr_def_gw
, 0, 1, &ri
,
2029 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
2030 &attr
, 0, 1, &ri
, 0);
2033 /* If a local route exists for this prefix, we need to update
2034 * the global routing table too.
2039 /* Perform route selection; this is just to set the flags
2041 * as local route in the VNI always wins.
2043 evpn_route_select_install(bgp
, vpn
, rn
);
2045 attr_new
= ri
->attr
;
2047 /* Update route in global routing table. */
2048 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2049 (struct prefix
*)evp
, &vpn
->prd
);
2051 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
2054 /* Schedule for processing and unlock node. */
2055 bgp_process(bgp
, rd_rn
, afi
, safi
);
2056 bgp_unlock_node(rd_rn
);
2059 /* Unintern temporary. */
2060 aspath_unintern(&attr
.aspath
);
2061 aspath_unintern(&attr_sticky
.aspath
);
2062 aspath_unintern(&attr_def_gw
.aspath
);
2063 aspath_unintern(&attr_ip6_ll
.aspath
);
2069 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2070 * global routing table. These are also scheduled for withdraw from peers.
2072 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2076 struct bgp_node
*rdrn
, *rn
;
2077 struct bgp_table
*table
;
2078 struct bgp_info
*ri
;
2083 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
2084 if (rdrn
&& rdrn
->info
) {
2085 table
= (struct bgp_table
*)rdrn
->info
;
2086 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2087 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2089 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2092 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &ri
);
2094 bgp_process(bgp
, rn
, afi
, safi
);
2098 /* Unlock RD node. */
2100 bgp_unlock_node(rdrn
);
2106 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2107 * table as well as the per-VNI route table.
2109 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2113 struct bgp_node
*rn
;
2114 struct bgp_info
*ri
;
2119 /* First, walk the global route table for this VNI's type-2 local
2121 * EVPN routes are a 2-level table, first get the RD table.
2123 delete_global_type2_routes(bgp
, vpn
);
2125 /* Next, walk this VNI's route table and delete local type-2 routes. */
2126 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2127 rn
= bgp_route_next(rn
)) {
2128 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2130 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2133 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &ri
);
2135 /* Route entry in local table gets deleted immediately. */
2137 bgp_info_reap(rn
, ri
);
2144 * Delete all routes in per ES route-table
2146 static int delete_all_es_routes(struct bgp
*bgp
, struct evpnes
*es
)
2148 struct bgp_node
*rn
;
2149 struct bgp_info
*ri
, *nextri
;
2151 /* Walk this ES's route table and delete all routes. */
2152 for (rn
= bgp_table_top(es
->route_table
); rn
;
2153 rn
= bgp_route_next(rn
)) {
2154 for (ri
= rn
->info
; (ri
!= NULL
) && (nextri
= ri
->next
, 1);
2156 bgp_info_delete(rn
, ri
);
2157 bgp_info_reap(rn
, ri
);
2165 * Delete all routes in the per-VNI route table.
2167 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2169 struct bgp_node
*rn
;
2170 struct bgp_info
*ri
, *nextri
;
2172 /* Walk this VNI's route table and delete all routes. */
2173 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2174 rn
= bgp_route_next(rn
)) {
2175 for (ri
= rn
->info
; (ri
!= NULL
) && (nextri
= ri
->next
, 1);
2177 bgp_info_delete(rn
, ri
);
2178 bgp_info_reap(rn
, ri
);
2186 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2187 * export RT getting modified or change to tunnel IP. Note that these
2188 * situations need the route in the per-VNI table as well as the global
2189 * table to be updated (as attributes change).
2191 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2194 struct prefix_evpn p
;
2196 /* Update and advertise the type-3 route (only one) followed by the
2197 * locally learnt type-2 routes (MACIP) - for this VNI.
2199 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2200 ret
= update_evpn_route(bgp
, vpn
, &p
, 0);
2204 return update_all_type2_routes(bgp
, vpn
);
2207 /* Delete (and withdraw) local routes for specified ES from global and ES table.
2208 * Also remove all other routes from the per ES table.
2209 * Invoked when ES is deleted.
2211 static int delete_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
2214 char buf
[ESI_STR_LEN
];
2215 struct prefix_evpn p
;
2217 /* Delete and withdraw locally learnt ES route */
2218 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
.ipaddr_v4
);
2219 ret
= delete_evpn_type4_route(bgp
, es
, &p
);
2222 "%u failed to delete type-4 route for ESI %s",
2224 esi_to_str(&es
->esi
, buf
, sizeof(buf
)));
2227 /* Delete all routes from per ES table */
2228 return delete_all_es_routes(bgp
, es
);
2232 * Delete (and withdraw) local routes for specified VNI from the global
2233 * table and per-VNI table. After this, remove all other routes from
2234 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2235 * (advertise-all-vni) being disabled.
2237 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2240 struct prefix_evpn p
;
2242 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2243 * followed by type-3 routes (only one) - for this VNI.
2245 ret
= delete_all_type2_routes(bgp
, vpn
);
2249 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2250 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2254 /* Delete all routes from the per-VNI table. */
2255 return delete_all_vni_routes(bgp
, vpn
);
2259 * There is a tunnel endpoint IP address change for this VNI, delete
2260 * prior type-3 route (if needed) and update.
2261 * Note: Route re-advertisement happens elsewhere after other processing
2264 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2265 struct in_addr originator_ip
)
2267 struct prefix_evpn p
;
2269 /* If VNI is not live, we only need to update the originator ip */
2270 if (!is_vni_live(vpn
)) {
2271 vpn
->originator_ip
= originator_ip
;
2275 /* Update the tunnel-ip hash */
2276 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2277 bgp_tip_add(bgp
, &originator_ip
);
2279 /* filter routes as martian nexthop db has changed */
2280 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2282 /* Need to withdraw type-3 route as the originator IP is part
2285 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2286 delete_evpn_route(bgp
, vpn
, &p
);
2288 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2289 vpn
->originator_ip
= originator_ip
;
2293 /* Install EVPN route entry in ES */
2294 static int install_evpn_route_entry_in_es(struct bgp
*bgp
,
2296 struct prefix_evpn
*p
,
2297 struct bgp_info
*parent_ri
)
2300 struct bgp_node
*rn
= NULL
;
2301 struct bgp_info
*ri
= NULL
;
2302 struct attr
*attr_new
= NULL
;
2304 /* Create (or fetch) route within the VNI.
2305 * NOTE: There is no RD here.
2307 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
2309 /* Check if route entry is already present. */
2310 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2312 (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2316 /* Add (or update) attribute to hash. */
2317 attr_new
= bgp_attr_intern(parent_ri
->attr
);
2319 /* Create new route with its attribute. */
2320 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
2321 parent_ri
->peer
, attr_new
, rn
);
2322 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
2323 bgp_info_extra_get(ri
);
2324 ri
->extra
->parent
= parent_ri
;
2325 bgp_info_add(rn
, ri
);
2327 if (attrhash_cmp(ri
->attr
, parent_ri
->attr
)
2328 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
2329 bgp_unlock_node(rn
);
2332 /* The attribute has changed. */
2333 /* Add (or update) attribute to hash. */
2334 attr_new
= bgp_attr_intern(parent_ri
->attr
);
2336 /* Restore route, if needed. */
2337 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
2338 bgp_info_restore(rn
, ri
);
2340 /* Mark if nexthop has changed. */
2341 if (!IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
))
2342 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
2344 /* Unintern existing, set to new. */
2345 bgp_attr_unintern(&ri
->attr
);
2346 ri
->attr
= attr_new
;
2347 ri
->uptime
= bgp_clock();
2350 /* Perform route selection and update zebra, if required. */
2351 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2356 * Install route entry into the VRF routing table and invoke route selection.
2358 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2359 struct prefix_evpn
*evp
,
2360 struct bgp_info
*parent_ri
)
2362 struct bgp_node
*rn
;
2363 struct bgp_info
*ri
;
2365 struct attr
*attr_new
;
2368 struct prefix
*pp
= &p
;
2371 char buf
[PREFIX_STRLEN
];
2372 char buf1
[PREFIX_STRLEN
];
2374 memset(pp
, 0, sizeof(struct prefix
));
2375 ip_prefix_from_evpn_prefix(evp
, pp
);
2377 if (bgp_debug_zebra(NULL
)) {
2379 "installing evpn prefix %s as ip prefix %s in vrf %s",
2380 prefix2str(evp
, buf
, sizeof(buf
)),
2381 prefix2str(pp
, buf1
, sizeof(buf
)),
2382 vrf_id_to_name(bgp_vrf
->vrf_id
));
2385 /* Create (or fetch) route within the VRF. */
2386 /* NOTE: There is no RD here. */
2387 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2389 safi
= SAFI_UNICAST
;
2390 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2391 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2393 safi
= SAFI_UNICAST
;
2394 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2398 /* EVPN routes currently only support a IPv4 next hop which corresponds
2399 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2400 * or prefix route, we have to convert the next hop to an IPv4-mapped
2401 * address for the rest of the code to flow through. In the case of IPv4,
2402 * make sure to set the flag for next hop attribute.
2404 bgp_attr_dup(&attr
, parent_ri
->attr
);
2406 evpn_convert_nexthop_to_ipv6(&attr
);
2408 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2410 /* Check if route entry is already present. */
2411 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2413 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2417 /* Add (or update) attribute to hash. */
2418 attr_new
= bgp_attr_intern(&attr
);
2420 /* Create new route with its attribute. */
2421 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
2422 parent_ri
->peer
, attr_new
, rn
);
2423 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
2424 bgp_info_extra_get(ri
);
2425 ri
->extra
->parent
= bgp_info_lock(parent_ri
);
2426 bgp_lock_node((struct bgp_node
*)parent_ri
->net
);
2427 if (parent_ri
->extra
) {
2428 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
2429 sizeof(ri
->extra
->label
));
2430 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
2432 bgp_info_add(rn
, ri
);
2434 if (attrhash_cmp(ri
->attr
, &attr
)
2435 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
2436 bgp_unlock_node(rn
);
2439 /* The attribute has changed. */
2440 /* Add (or update) attribute to hash. */
2441 attr_new
= bgp_attr_intern(&attr
);
2443 /* Restore route, if needed. */
2444 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
2445 bgp_info_restore(rn
, ri
);
2447 /* Mark if nexthop has changed. */
2448 if ((afi
== AFI_IP
&&
2449 !IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
)) ||
2451 !IPV6_ADDR_SAME(&ri
->attr
->mp_nexthop_global
,
2452 &attr_new
->mp_nexthop_global
)))
2453 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
2455 /* Unintern existing, set to new. */
2456 bgp_attr_unintern(&ri
->attr
);
2457 ri
->attr
= attr_new
;
2458 ri
->uptime
= bgp_clock();
2461 /* Perform route selection and update zebra, if required. */
2462 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2468 * Install route entry into the VNI routing table and invoke route selection.
2470 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2471 struct prefix_evpn
*p
,
2472 struct bgp_info
*parent_ri
)
2474 struct bgp_node
*rn
;
2475 struct bgp_info
*ri
;
2476 struct attr
*attr_new
;
2479 /* Create (or fetch) route within the VNI. */
2480 /* NOTE: There is no RD here. */
2481 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2483 /* Check if route entry is already present. */
2484 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2486 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2490 /* Add (or update) attribute to hash. */
2491 attr_new
= bgp_attr_intern(parent_ri
->attr
);
2493 /* Create new route with its attribute. */
2494 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
2495 parent_ri
->peer
, attr_new
, rn
);
2496 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
2497 bgp_info_extra_get(ri
);
2498 ri
->extra
->parent
= bgp_info_lock(parent_ri
);
2499 bgp_lock_node((struct bgp_node
*)parent_ri
->net
);
2500 if (parent_ri
->extra
) {
2501 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
2502 sizeof(ri
->extra
->label
));
2503 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
2505 bgp_info_add(rn
, ri
);
2507 if (attrhash_cmp(ri
->attr
, parent_ri
->attr
)
2508 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
2509 bgp_unlock_node(rn
);
2512 /* The attribute has changed. */
2513 /* Add (or update) attribute to hash. */
2514 attr_new
= bgp_attr_intern(parent_ri
->attr
);
2516 /* Restore route, if needed. */
2517 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
2518 bgp_info_restore(rn
, ri
);
2520 /* Mark if nexthop has changed. */
2521 if (!IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
))
2522 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
2524 /* Unintern existing, set to new. */
2525 bgp_attr_unintern(&ri
->attr
);
2526 ri
->attr
= attr_new
;
2527 ri
->uptime
= bgp_clock();
2530 /* Perform route selection and update zebra, if required. */
2531 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2536 /* Uninstall EVPN route entry from ES route table */
2537 static int uninstall_evpn_route_entry_in_es(struct bgp
*bgp
,
2539 struct prefix_evpn
*p
,
2540 struct bgp_info
*parent_ri
)
2543 struct bgp_node
*rn
;
2544 struct bgp_info
*ri
;
2546 if (!es
->route_table
)
2549 /* Locate route within the ESI.
2550 * NOTE: There is no RD here.
2552 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2556 /* Find matching route entry. */
2557 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2559 (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2565 /* Mark entry for deletion */
2566 bgp_info_delete(rn
, ri
);
2568 /* Perform route selection and update zebra, if required. */
2569 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2571 /* Unlock route node. */
2572 bgp_unlock_node(rn
);
2578 * Uninstall route entry from the VRF routing table and send message
2579 * to zebra, if appropriate.
2581 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2582 struct prefix_evpn
*evp
,
2583 struct bgp_info
*parent_ri
)
2585 struct bgp_node
*rn
;
2586 struct bgp_info
*ri
;
2589 struct prefix
*pp
= &p
;
2592 char buf
[PREFIX_STRLEN
];
2593 char buf1
[PREFIX_STRLEN
];
2595 memset(pp
, 0, sizeof(struct prefix
));
2596 ip_prefix_from_evpn_prefix(evp
, pp
);
2598 if (bgp_debug_zebra(NULL
)) {
2600 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2601 prefix2str(evp
, buf
, sizeof(buf
)),
2602 prefix2str(pp
, buf1
, sizeof(buf
)),
2603 vrf_id_to_name(bgp_vrf
->vrf_id
));
2606 /* Locate route within the VRF. */
2607 /* NOTE: There is no RD here. */
2608 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2610 safi
= SAFI_UNICAST
;
2611 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2614 safi
= SAFI_UNICAST
;
2615 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2621 /* Find matching route entry. */
2622 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2624 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2630 /* Mark entry for deletion */
2631 bgp_info_delete(rn
, ri
);
2633 /* Perform route selection and update zebra, if required. */
2634 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2636 /* Unlock route node. */
2637 bgp_unlock_node(rn
);
2643 * Uninstall route entry from the VNI routing table and send message
2644 * to zebra, if appropriate.
2646 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2647 struct prefix_evpn
*p
,
2648 struct bgp_info
*parent_ri
)
2650 struct bgp_node
*rn
;
2651 struct bgp_info
*ri
;
2654 /* Locate route within the VNI. */
2655 /* NOTE: There is no RD here. */
2656 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2660 /* Find matching route entry. */
2661 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2663 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2669 /* Mark entry for deletion */
2670 bgp_info_delete(rn
, ri
);
2672 /* Perform route selection and update zebra, if required. */
2673 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2675 /* Unlock route node. */
2676 bgp_unlock_node(rn
);
2682 * Given a prefix, see if it belongs to ES.
2684 static int is_prefix_matching_for_es(struct prefix_evpn
*p
,
2687 /* if not an ES route return false */
2688 if (p
->prefix
.route_type
!= BGP_EVPN_ES_ROUTE
)
2691 if (memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
)) == 0)
2698 * Given a route entry and a VRF, see if this route entry should be
2699 * imported into the VRF i.e., RTs match.
2701 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
, struct bgp_info
*ri
)
2703 struct attr
*attr
= ri
->attr
;
2704 struct ecommunity
*ecom
;
2708 /* Route should have valid RT to be even considered. */
2709 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2712 ecom
= attr
->ecommunity
;
2713 if (!ecom
|| !ecom
->size
)
2716 /* For each extended community RT, see if it matches this VNI. If any RT
2717 * matches, we're done.
2719 for (i
= 0; i
< ecom
->size
; i
++) {
2721 uint8_t type
, sub_type
;
2722 struct ecommunity_val
*eval
;
2723 struct ecommunity_val eval_tmp
;
2724 struct vrf_irt_node
*irt
;
2726 /* Only deal with RTs */
2727 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2728 eval
= (struct ecommunity_val
*)(ecom
->val
2729 + (i
* ECOMMUNITY_SIZE
));
2732 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2735 /* See if this RT matches specified VNIs import RTs */
2736 irt
= lookup_vrf_import_rt(eval
);
2738 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2741 /* Also check for non-exact match. In this, we mask out the AS
2743 * only check on the local-admin sub-field. This is to
2745 * VNI as the RT for EBGP peering too.
2748 if (type
== ECOMMUNITY_ENCODE_AS
2749 || type
== ECOMMUNITY_ENCODE_AS4
2750 || type
== ECOMMUNITY_ENCODE_IP
) {
2751 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2752 mask_ecom_global_admin(&eval_tmp
, eval
);
2753 irt
= lookup_vrf_import_rt(&eval_tmp
);
2756 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2764 * Given a route entry and a VNI, see if this route entry should be
2765 * imported into the VNI i.e., RTs match.
2767 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2768 struct bgp_info
*ri
)
2770 struct attr
*attr
= ri
->attr
;
2771 struct ecommunity
*ecom
;
2775 /* Route should have valid RT to be even considered. */
2776 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2779 ecom
= attr
->ecommunity
;
2780 if (!ecom
|| !ecom
->size
)
2783 /* For each extended community RT, see if it matches this VNI. If any RT
2784 * matches, we're done.
2786 for (i
= 0; i
< ecom
->size
; i
++) {
2788 uint8_t type
, sub_type
;
2789 struct ecommunity_val
*eval
;
2790 struct ecommunity_val eval_tmp
;
2791 struct irt_node
*irt
;
2793 /* Only deal with RTs */
2794 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2795 eval
= (struct ecommunity_val
*)(ecom
->val
2796 + (i
* ECOMMUNITY_SIZE
));
2799 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2802 /* See if this RT matches specified VNIs import RTs */
2803 irt
= lookup_import_rt(bgp
, eval
);
2805 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2808 /* Also check for non-exact match. In this, we mask out the AS
2810 * only check on the local-admin sub-field. This is to
2812 * VNI as the RT for EBGP peering too.
2815 if (type
== ECOMMUNITY_ENCODE_AS
2816 || type
== ECOMMUNITY_ENCODE_AS4
2817 || type
== ECOMMUNITY_ENCODE_IP
) {
2818 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2819 mask_ecom_global_admin(&eval_tmp
, eval
);
2820 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2823 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2830 static int install_uninstall_routes_for_es(struct bgp
*bgp
,
2837 char buf
[PREFIX_STRLEN
];
2838 char buf1
[ESI_STR_LEN
];
2839 struct bgp_node
*rd_rn
, *rn
;
2840 struct bgp_table
*table
;
2841 struct bgp_info
*ri
;
2847 * Walk entire global routing table and evaluate routes which could be
2848 * imported into this VRF. Note that we need to loop through all global
2849 * routes to determine which route matches the import rt on vrf
2851 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2852 rd_rn
= bgp_route_next(rd_rn
)) {
2853 table
= (struct bgp_table
*)(rd_rn
->info
);
2857 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2858 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2860 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2862 * Consider "valid" remote routes applicable for
2865 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2866 && ri
->type
== ZEBRA_ROUTE_BGP
2867 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2870 if (!is_prefix_matching_for_es(evp
, es
))
2874 ret
= install_evpn_route_entry_in_es(
2877 ret
= uninstall_evpn_route_entry_in_es(
2882 "Failed to %s EVPN %s route in ESI %s",
2885 prefix2str(evp
, buf
,
2887 esi_to_str(&es
->esi
, buf1
,
2898 * Install or uninstall mac-ip routes are appropriate for this
2901 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
2905 struct bgp_node
*rd_rn
, *rn
;
2906 struct bgp_table
*table
;
2907 struct bgp_info
*ri
;
2909 char buf
[PREFIX_STRLEN
];
2910 struct bgp
*bgp_def
= NULL
;
2914 bgp_def
= bgp_get_default();
2918 /* Walk entire global routing table and evaluate routes which could be
2919 * imported into this VRF. Note that we need to loop through all global
2920 * routes to determine which route matches the import rt on vrf
2922 for (rd_rn
= bgp_table_top(bgp_def
->rib
[afi
][safi
]); rd_rn
;
2923 rd_rn
= bgp_route_next(rd_rn
)) {
2924 table
= (struct bgp_table
*)(rd_rn
->info
);
2928 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2929 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2931 /* if not mac-ip route skip this route */
2932 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2933 || evp
->prefix
.route_type
2934 == BGP_EVPN_IP_PREFIX_ROUTE
))
2937 /* if not a mac+ip route skip this route */
2938 if (!(is_evpn_prefix_ipaddr_v4(evp
)
2939 || is_evpn_prefix_ipaddr_v6(evp
)))
2942 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2943 /* Consider "valid" remote routes applicable for
2946 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2947 && ri
->type
== ZEBRA_ROUTE_BGP
2948 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2951 if (is_route_matching_for_vrf(bgp_vrf
, ri
)) {
2953 ret
= install_evpn_route_entry_in_vrf(
2956 ret
= uninstall_evpn_route_entry_in_vrf(
2961 "Failed to %s EVPN %s route in VRF %s",
2964 prefix2str(evp
, buf
,
2979 * Install or uninstall routes of specified type that are appropriate for this
2982 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
2983 struct bgpevpn
*vpn
,
2984 bgp_evpn_route_type rtype
,
2989 struct bgp_node
*rd_rn
, *rn
;
2990 struct bgp_table
*table
;
2991 struct bgp_info
*ri
;
2997 /* Walk entire global routing table and evaluate routes which could be
2998 * imported into this VPN. Note that we cannot just look at the routes
3000 * the VNI's RD - remote routes applicable for this VNI could have any
3003 /* EVPN routes are a 2-level table. */
3004 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
3005 rd_rn
= bgp_route_next(rd_rn
)) {
3006 table
= (struct bgp_table
*)(rd_rn
->info
);
3010 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3011 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3013 if (evp
->prefix
.route_type
!= rtype
)
3016 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
3017 /* Consider "valid" remote routes applicable for
3019 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
3020 && ri
->type
== ZEBRA_ROUTE_BGP
3021 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
3024 if (is_route_matching_for_vni(bgp
, vpn
, ri
)) {
3026 ret
= install_evpn_route_entry(
3029 ret
= uninstall_evpn_route_entry(
3034 "%u: Failed to %s EVPN %s route in VNI %u",
3038 rtype
== BGP_EVPN_MAC_IP_ROUTE
3052 /* Install any existing remote ES routes applicable for this ES into its routing
3053 * table. This is invoked when ES comes up.
3055 static int install_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
3057 return install_uninstall_routes_for_es(bgp
, es
, 1);
3061 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3062 * is invoked upon l3vni-add or l3vni import rt change
3064 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3066 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3071 * Install any existing remote routes applicable for this VNI into its
3072 * routing table. This is invoked when a VNI becomes "live" or its Import
3075 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3079 /* Install type-3 routes followed by type-2 routes - the ones applicable
3082 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3087 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3091 /* uninstall routes from l3vni vrf. */
3092 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3094 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3099 * Uninstall any existing remote routes for this VNI. One scenario in which
3100 * this is invoked is upon an import RT change.
3102 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3106 /* Uninstall type-2 routes followed by type-3 routes - the ones
3110 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3115 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3119 /* Install or unistall route in ES */
3120 static int install_uninstall_route_in_es(struct bgp
*bgp
, struct evpnes
*es
,
3121 afi_t afi
, safi_t safi
,
3122 struct prefix_evpn
*evp
,
3123 struct bgp_info
*ri
,
3127 char buf
[ESI_STR_LEN
];
3130 ret
= install_evpn_route_entry_in_es(bgp
, es
, evp
, ri
);
3132 ret
= uninstall_evpn_route_entry_in_es(bgp
, es
, evp
, ri
);
3135 zlog_err("%u: Failed to %s EVPN %s route in ESI %s",
3136 bgp
->vrf_id
, install
? "install" : "uninstall",
3138 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
,
3146 * Install or uninstall route in matching VRFs (list).
3148 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3149 safi_t safi
, struct prefix_evpn
*evp
,
3150 struct bgp_info
*ri
,
3151 struct list
*vrfs
, int install
)
3153 char buf
[PREFIX2STR_BUFFER
];
3154 struct bgp
*bgp_vrf
;
3155 struct listnode
*node
, *nnode
;
3157 /* Only type-2/type-5 routes go into a VRF */
3158 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3159 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3162 /* if it is type-2 route and not a mac+ip route skip this route */
3163 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3164 && !(is_evpn_prefix_ipaddr_v4(evp
)
3165 || is_evpn_prefix_ipaddr_v6(evp
)))
3168 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3172 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, ri
);
3174 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3178 zlog_err("%u: Failed to %s prefix %s in VRF %s",
3180 install
? "install" : "uninstall",
3181 prefix2str(evp
, buf
, sizeof(buf
)),
3182 vrf_id_to_name(bgp_vrf
->vrf_id
));
3191 * Install or uninstall route in matching VNIs (list).
3193 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3194 safi_t safi
, struct prefix_evpn
*evp
,
3195 struct bgp_info
*ri
,
3196 struct list
*vnis
, int install
)
3198 struct bgpevpn
*vpn
;
3199 struct listnode
*node
, *nnode
;
3201 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3204 if (!is_vni_live(vpn
))
3208 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, ri
);
3210 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, ri
);
3213 zlog_err("%u: Failed to %s EVPN %s route in VNI %u",
3214 bgp
->vrf_id
, install
? "install" : "uninstall",
3215 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3227 * Install or uninstall route for appropriate VNIs/ESIs.
3229 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3230 struct prefix
*p
, struct bgp_info
*ri
,
3233 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3234 struct attr
*attr
= ri
->attr
;
3235 struct ecommunity
*ecom
;
3240 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3241 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3242 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3243 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3244 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3247 /* If we don't have Route Target, nothing much to do. */
3248 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3251 ecom
= attr
->ecommunity
;
3252 if (!ecom
|| !ecom
->size
)
3255 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3256 * attached to the route */
3257 for (i
= 0; i
< ecom
->size
; i
++) {
3259 uint8_t type
, sub_type
;
3260 struct ecommunity_val
*eval
;
3261 struct ecommunity_val eval_tmp
;
3262 struct irt_node
*irt
; /* import rt for l2vni */
3263 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3266 /* Only deal with RTs */
3267 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3268 eval
= (struct ecommunity_val
*)(ecom
->val
3269 + (i
* ECOMMUNITY_SIZE
));
3272 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3276 * macip routes (type-2) are imported into VNI and VRF tables.
3277 * IMET route is imported into VNI table.
3278 * prefix routes are imported into VRF table.
3280 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3281 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3282 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3284 irt
= lookup_import_rt(bgp
, eval
);
3286 install_uninstall_route_in_vnis(bgp
, afi
, safi
,
3291 vrf_irt
= lookup_vrf_import_rt(eval
);
3293 install_uninstall_route_in_vrfs(bgp
, afi
, safi
,
3298 /* Also check for non-exact match.
3299 * In this, we mask out the AS and
3300 * only check on the local-admin sub-field.
3301 * This is to facilitate using
3302 * VNI as the RT for EBGP peering too.
3306 if (type
== ECOMMUNITY_ENCODE_AS
3307 || type
== ECOMMUNITY_ENCODE_AS4
3308 || type
== ECOMMUNITY_ENCODE_IP
) {
3309 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3310 mask_ecom_global_admin(&eval_tmp
, eval
);
3311 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3312 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3316 install_uninstall_route_in_vnis(bgp
, afi
, safi
,
3321 install_uninstall_route_in_vrfs(bgp
, afi
, safi
,
3327 /* es route is imported into the es table */
3328 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3330 /* we will match based on the entire esi to avoid
3331 * imoort of an es route for esi2 into esi1
3333 es
= bgp_evpn_lookup_es(bgp
, &evp
->prefix
.es_addr
.esi
);
3334 if (es
&& is_es_local(es
))
3335 install_uninstall_route_in_es(bgp
, es
,
3345 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3348 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3350 /* delete all ipv4 routes and withdraw from peers */
3351 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3352 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3354 /* delete all ipv6 routes and withdraw from peers */
3355 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3356 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3360 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3363 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3365 /* update all ipv4 routes */
3366 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3367 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3369 /* update all ipv6 routes */
3370 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3371 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3375 * update and advertise local routes for a VRF as type-5 routes.
3376 * This is invoked upon RD change for a VRF. Note taht the processing is only
3377 * done in the global route table using the routes which already exist in the
3380 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3382 /* skip if the RD is configured */
3383 if (is_vrf_rd_configured(bgp_vrf
))
3386 /* derive the RD for the VRF based on new router-id */
3387 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3389 /* update advertise ipv4|ipv6 routes as type-5 routes */
3390 update_advertise_vrf_routes(bgp_vrf
);
3394 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3395 * This is invoked upon VRF RD change. The processing is done only from global
3398 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3400 /* skip if the RD is configured */
3401 if (is_vrf_rd_configured(bgp_vrf
))
3404 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3405 delete_withdraw_vrf_routes(bgp_vrf
);
3409 * Update and advertise local routes for a VNI. Invoked upon router-id
3410 * change. Note that the processing is done only on the global route table
3411 * using routes that already exist in the per-VNI table.
3413 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3415 struct prefix_evpn p
;
3416 struct bgp_node
*rn
, *global_rn
;
3417 struct bgp_info
*ri
, *global_ri
;
3419 afi_t afi
= AFI_L2VPN
;
3420 safi_t safi
= SAFI_EVPN
;
3422 /* Locate type-3 route for VNI in the per-VNI table and use its
3423 * attributes to create and advertise the type-3 route for this VNI
3424 * in the global table.
3426 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3427 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3428 if (!rn
) /* unexpected */
3430 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
3431 if (ri
->peer
== bgp
->peer_self
&& ri
->type
== ZEBRA_ROUTE_BGP
3432 && ri
->sub_type
== BGP_ROUTE_STATIC
)
3434 if (!ri
) /* unexpected */
3438 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3439 (struct prefix
*)&p
, &vpn
->prd
);
3440 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1, 0, &ri
,
3443 /* Schedule for processing and unlock node. */
3444 bgp_process(bgp
, global_rn
, afi
, safi
);
3445 bgp_unlock_node(global_rn
);
3447 /* Now, walk this VNI's route table and use the route and its attribute
3448 * to create and schedule route in global table.
3450 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
3451 rn
= bgp_route_next(rn
)) {
3452 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3454 /* Identify MAC-IP local routes. */
3455 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3458 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
3459 if (ri
->peer
== bgp
->peer_self
3460 && ri
->type
== ZEBRA_ROUTE_BGP
3461 && ri
->sub_type
== BGP_ROUTE_STATIC
)
3466 /* Create route in global routing table using this route entry's
3470 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3471 (struct prefix
*)evp
, &vpn
->prd
);
3473 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
3476 /* Schedule for processing and unlock node. */
3477 bgp_process(bgp
, global_rn
, afi
, safi
);
3478 bgp_unlock_node(global_rn
);
3485 * Delete (and withdraw) local routes for a VNI - only from the global
3486 * table. Invoked upon router-id change.
3488 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3491 struct prefix_evpn p
;
3492 struct bgp_node
*global_rn
;
3493 struct bgp_info
*ri
;
3494 afi_t afi
= AFI_L2VPN
;
3495 safi_t safi
= SAFI_EVPN
;
3497 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3498 * for this VNI - from the global table.
3500 ret
= delete_global_type2_routes(bgp
, vpn
);
3504 /* Remove type-3 route for this VNI from global table. */
3505 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3506 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3507 (struct prefix
*)&p
, &vpn
->prd
);
3509 /* Delete route entry in the global EVPN table. */
3510 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &ri
);
3512 /* Schedule for processing - withdraws to peers happen from
3516 bgp_process(bgp
, global_rn
, afi
, safi
);
3517 bgp_unlock_node(global_rn
);
3524 * Handle router-id change. Update and advertise local routes corresponding
3525 * to this VNI from peers. Note that this is invoked after updating the
3526 * router-id. The routes in the per-VNI table are used to create routes in
3527 * the global table and schedule them.
3529 static void update_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3531 struct bgpevpn
*vpn
;
3533 vpn
= (struct bgpevpn
*)backet
->data
;
3536 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__
);
3540 /* Skip VNIs with configured RD. */
3541 if (is_rd_configured(vpn
))
3544 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3545 update_advertise_vni_routes(bgp
, vpn
);
3549 * Handle router-id change. Delete and withdraw local routes corresponding
3550 * to this VNI from peers. Note that this is invoked prior to updating
3551 * the router-id and is done only on the global route table, the routes
3552 * are needed in the per-VNI table to re-advertise with new router id.
3554 static void withdraw_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3556 struct bgpevpn
*vpn
;
3558 vpn
= (struct bgpevpn
*)backet
->data
;
3561 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__
);
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 zlog_err("%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3598 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3602 memset(&evpn
, 0, sizeof(evpn
));
3604 /* Make prefix_rd */
3605 prd
.family
= AF_UNSPEC
;
3607 memcpy(&prd
.val
, pfx
, 8);
3610 /* Make EVPN prefix. */
3611 memset(&p
, 0, sizeof(struct prefix_evpn
));
3613 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3614 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3616 /* Copy Ethernet Seg Identifier */
3617 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
3620 /* Copy Ethernet Tag */
3621 memcpy(ð_tag
, pfx
, 4);
3622 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3625 /* Get the MAC Addr len */
3626 macaddr_len
= *pfx
++;
3628 /* Get the MAC Addr */
3629 if (macaddr_len
== (ETH_ALEN
* 8)) {
3630 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
3634 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3635 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3641 ipaddr_len
= *pfx
++;
3642 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3643 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3645 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3646 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3651 ipaddr_len
/= 8; /* Convert to bytes. */
3652 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3655 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
3659 /* Get the VNI(s). Stored as bytes here. */
3661 memset(label
, 0, sizeof(label
));
3662 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
3663 pfx
+= BGP_LABEL_BYTES
;
3664 psize
-= (33 + ipaddr_len
);
3665 /* Do we have a second VNI? */
3668 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
3670 * If in future, we are required to access additional fields,
3671 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
3676 /* Process the route. */
3678 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3679 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3680 &prd
, &label
[0], num_labels
, 0, &evpn
);
3682 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3683 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3684 &prd
, &label
[0], num_labels
, &evpn
);
3689 * Process received EVPN type-3 route (advertise or withdraw).
3691 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3692 struct attr
*attr
, uint8_t *pfx
, int psize
,
3693 uint32_t addpath_id
)
3695 struct prefix_rd prd
;
3696 struct prefix_evpn p
;
3701 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3702 * IP len (1) and IP (4 or 16).
3704 if (psize
!= 17 && psize
!= 29) {
3705 zlog_err("%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3706 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3710 /* If PMSI is present, log if it is anything other than IR.
3711 * Note: We just simply ignore the values as it is not clear if
3712 * doing anything else is better.
3715 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3716 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
) {
3717 zlog_warn("%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3718 peer
->bgp
->vrf_id
, peer
->host
,
3719 attr
->pmsi_tnl_type
);
3723 /* Make prefix_rd */
3724 prd
.family
= AF_UNSPEC
;
3726 memcpy(&prd
.val
, pfx
, 8);
3729 /* Make EVPN prefix. */
3730 memset(&p
, 0, sizeof(struct prefix_evpn
));
3732 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3733 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3735 /* Copy Ethernet Tag */
3736 memcpy(ð_tag
, pfx
, 4);
3737 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
3741 ipaddr_len
= *pfx
++;
3742 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3743 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
3744 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
3747 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3748 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3752 /* Process the route. */
3754 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3755 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3756 &prd
, NULL
, 0, 0, NULL
);
3758 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3759 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3760 &prd
, NULL
, 0, NULL
);
3765 * Process received EVPN type-4 route (advertise or withdraw).
3767 static int process_type4_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3768 struct attr
*attr
, uint8_t *pfx
, int psize
,
3769 uint32_t addpath_id
)
3774 struct in_addr vtep_ip
;
3775 struct prefix_rd prd
;
3776 struct prefix_evpn p
;
3778 /* Type-4 route should be either 23 or 35 bytes
3779 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
3781 if (psize
!= 23 && psize
!= 35) {
3782 zlog_err("%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
3783 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3787 /* Make prefix_rd */
3788 prd
.family
= AF_UNSPEC
;
3790 memcpy(&prd
.val
, pfx
, 8);
3794 memcpy(&esi
, pfx
, ESI_BYTES
);
3799 ipaddr_len
= *pfx
++;
3800 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3801 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
3804 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
3805 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3809 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
3810 /* Process the route. */
3812 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3813 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3814 &prd
, NULL
, 0, 0, NULL
);
3816 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3817 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3818 &prd
, NULL
, 0, NULL
);
3825 * Process received EVPN type-5 route (advertise or withdraw).
3827 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3828 struct attr
*attr
, uint8_t *pfx
, int psize
,
3829 uint32_t addpath_id
, int withdraw
)
3831 struct prefix_rd prd
;
3832 struct prefix_evpn p
;
3833 struct bgp_route_evpn evpn
;
3836 mpls_label_t label
; /* holds the VNI as in the packet */
3839 /* Type-5 route should be 34 or 58 bytes:
3840 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3841 * GW (4 or 16) and VNI (3).
3842 * Note that the IP and GW should both be IPv4 or both IPv6.
3844 if (psize
!= 34 && psize
!= 58) {
3845 zlog_err("%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3846 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3850 /* Make prefix_rd */
3851 prd
.family
= AF_UNSPEC
;
3853 memcpy(&prd
.val
, pfx
, 8);
3856 /* Make EVPN prefix. */
3857 memset(&p
, 0, sizeof(struct prefix_evpn
));
3859 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3860 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
3862 /* Additional information outside of prefix - ESI and GW IP */
3863 memset(&evpn
, 0, sizeof(evpn
));
3866 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
3869 /* Fetch Ethernet Tag. */
3870 memcpy(ð_tag
, pfx
, 4);
3871 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
3874 /* Fetch IP prefix length. */
3876 if (ippfx_len
> IPV6_MAX_BITLEN
) {
3878 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3879 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
3882 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
3884 /* Determine IPv4 or IPv6 prefix */
3885 /* Since the address and GW are from the same family, this just becomes
3886 * a simple check on the total size.
3889 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
3890 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
3892 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
3895 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
3896 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
3898 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
3902 /* Get the VNI (in MPLS label field). Stored as bytes here. */
3903 memset(&label
, 0, sizeof(label
));
3904 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
3907 * If in future, we are required to access additional fields,
3908 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
3912 /* Process the route. */
3914 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3915 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3916 &prd
, &label
, 1, 0, &evpn
);
3918 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3919 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3920 &prd
, &label
, 1, &evpn
);
3925 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
3926 struct prefix_rd
*prd
, mpls_label_t
*label
,
3927 uint32_t num_labels
, struct attr
*attr
)
3931 struct evpn_addr
*p_evpn_p
;
3933 memset(&temp
, 0, 16);
3934 if (p
->family
!= AF_EVPN
)
3936 p_evpn_p
= &(p
->u
.prefix_evpn
);
3938 /* len denites the total len of IP and GW-IP in the route
3939 IP and GW-IP have to be both ipv4 or ipv6
3941 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3942 len
= 8; /* IP and GWIP are both ipv4 */
3944 len
= 32; /* IP and GWIP are both ipv6 */
3945 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
3946 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
3947 stream_put(s
, prd
->val
, 8);
3949 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
3951 stream_put(s
, &temp
, 10);
3952 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
3953 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
3954 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3955 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
3957 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
3959 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3961 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
3963 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
3965 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3966 stream_put_ipv4(s
, 0);
3968 stream_put(s
, &temp
, 16);
3972 stream_put(s
, label
, 3);
3978 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
3980 static void cleanup_vni_on_disable(struct hash_backet
*backet
, struct bgp
*bgp
)
3982 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3984 /* Remove EVPN routes and schedule for processing. */
3985 delete_routes_for_vni(bgp
, vpn
);
3987 /* Clear "live" flag and see if hash needs to be freed. */
3988 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
3989 if (!is_vni_configured(vpn
))
3990 bgp_evpn_free(bgp
, vpn
);
3994 * Free a VNI entry; iterator function called during cleanup.
3996 static void free_vni_entry(struct hash_backet
*backet
, struct bgp
*bgp
)
3998 struct bgpevpn
*vpn
;
4000 vpn
= (struct bgpevpn
*)backet
->data
;
4001 delete_all_vni_routes(bgp
, vpn
);
4002 bgp_evpn_free(bgp
, vpn
);
4006 * Derive AUTO import RT for BGP VRF - L3VNI
4008 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4010 struct bgp
*bgp_def
= NULL
;
4012 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4013 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4016 bgp_def
= bgp_get_default();
4019 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4023 * Delete AUTO import RT from BGP VRF - L3VNI
4025 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4027 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4031 * Derive AUTO export RT for BGP VRF - L3VNI
4033 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4035 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4036 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4040 * Delete AUTO export RT from BGP VRF - L3VNI
4042 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4044 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4047 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4049 struct bgp
*bgp_def
= NULL
;
4050 struct listnode
*node
= NULL
;
4051 struct bgpevpn
*vpn
= NULL
;
4053 bgp_def
= bgp_get_default();
4057 /* update all type-5 routes */
4058 update_advertise_vrf_routes(bgp_vrf
);
4060 /* update all type-2 routes */
4061 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4062 update_routes_for_vni(bgp_def
, vpn
);
4066 * Handle autort change for a given VNI.
4068 static void update_autort_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
4070 struct bgpevpn
*vpn
= backet
->data
;
4073 zlog_warn("%s: VNI hash entry for VNI not found", __PRETTY_FUNCTION__
);
4077 if (!is_import_rt_configured(vpn
)) {
4078 if (is_vni_live(vpn
))
4079 bgp_evpn_uninstall_routes(bgp
, vpn
);
4080 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4081 list_delete_all_node(vpn
->import_rtl
);
4082 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4083 if (is_vni_live(vpn
))
4084 bgp_evpn_install_routes(bgp
, vpn
);
4086 if (!is_export_rt_configured(vpn
)) {
4087 list_delete_all_node(vpn
->export_rtl
);
4088 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4089 if (is_vni_live(vpn
))
4090 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4098 /* withdraw type-5 route corresponding to ip prefix */
4099 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4100 afi_t afi
, safi_t safi
)
4103 struct prefix_evpn evp
;
4104 char buf
[PREFIX_STRLEN
];
4106 build_type5_prefix_from_ip_prefix(&evp
, p
);
4107 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4110 "%u failed to delete type-5 route for prefix %s in vrf %s",
4111 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4112 vrf_id_to_name(bgp_vrf
->vrf_id
));
4116 /* withdraw all type-5 routes for an address family */
4117 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4119 struct bgp_table
*table
= NULL
;
4120 struct bgp_node
*rn
= NULL
;
4121 struct bgp_info
*ri
;
4123 table
= bgp_vrf
->rib
[afi
][safi
];
4124 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4125 /* Only care about "selected" routes - non-imported. */
4126 /* TODO: Support for AddPath for EVPN. */
4127 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
4128 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
)
4129 && (!ri
->extra
|| !ri
->extra
->parent
)) {
4130 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
4139 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4140 * to this function correspond to those of the source IP prefix (best
4141 * path in the case of the attr. In the case of a local prefix (when we
4142 * are advertising local subnets), the src_attr will be NULL.
4144 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4145 struct attr
*src_attr
, afi_t afi
,
4149 struct prefix_evpn evp
;
4150 char buf
[PREFIX_STRLEN
];
4152 build_type5_prefix_from_ip_prefix(&evp
, p
);
4153 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4155 zlog_err("%u: Failed to create type-5 route for prefix %s",
4156 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4159 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4160 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4161 * advertisement is enabled.
4163 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4166 struct bgp_table
*table
= NULL
;
4167 struct bgp_node
*rn
= NULL
;
4168 struct bgp_info
*ri
;
4170 table
= bgp_vrf
->rib
[afi
][safi
];
4171 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4172 /* Need to identify the "selected" route entry to use its
4173 * attribute. Also, we only consider "non-imported" routes.
4174 * TODO: Support for AddPath for EVPN.
4176 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
4177 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
)
4178 && (!ri
->extra
|| !ri
->extra
->parent
)) {
4180 /* apply the route-map */
4181 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4184 ret
= route_map_apply(
4185 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4187 &rn
->p
, RMAP_BGP
, ri
);
4188 if (ret
== RMAP_DENYMATCH
)
4191 bgp_evpn_advertise_type5_route(
4192 bgp_vrf
, &rn
->p
, ri
->attr
, afi
, safi
);
4199 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4201 struct listnode
*node
, *nnode
, *node_to_del
;
4202 struct ecommunity
*ecom
, *ecom_auto
;
4203 struct ecommunity_val eval
;
4205 if (bgp
->advertise_autort_rfc8365
)
4206 vni
|= EVPN_AUTORT_VXLAN
;
4207 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4209 ecom_auto
= ecommunity_new();
4210 ecommunity_add_val(ecom_auto
, &eval
);
4213 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4214 if (ecommunity_match(ecom
, ecom_auto
)) {
4215 ecommunity_free(&ecom
);
4221 list_delete_node(rtl
, node_to_del
);
4223 ecommunity_free(&ecom_auto
);
4226 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4227 struct ecommunity
*ecomadd
)
4229 /* uninstall routes from vrf */
4230 uninstall_routes_for_vrf(bgp_vrf
);
4232 /* Cleanup the RT to VRF mapping */
4233 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4235 /* Remove auto generated RT */
4236 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4238 /* Add the newly configured RT to RT list */
4239 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4240 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4242 /* map VRF to its RTs */
4243 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4245 /* install routes matching the new VRF */
4246 install_routes_for_vrf(bgp_vrf
);
4249 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4250 struct ecommunity
*ecomdel
)
4252 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4253 struct ecommunity
*ecom
= NULL
;
4255 /* uninstall routes from vrf */
4256 uninstall_routes_for_vrf(bgp_vrf
);
4258 /* Cleanup the RT to VRF mapping */
4259 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4261 /* remove the RT from the RT list */
4262 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4263 if (ecommunity_match(ecom
, ecomdel
)) {
4264 ecommunity_free(&ecom
);
4271 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4273 assert(bgp_vrf
->vrf_import_rtl
);
4274 /* fallback to auto import rt, if this was the last RT */
4275 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4276 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4277 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4280 /* map VRFs to its RTs */
4281 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4283 /* install routes matching this new RT */
4284 install_routes_for_vrf(bgp_vrf
);
4287 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4288 struct ecommunity
*ecomadd
)
4290 /* remove auto-generated RT */
4291 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4293 /* Add the new RT to the RT list */
4294 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4295 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4297 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4300 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4301 struct ecommunity
*ecomdel
)
4303 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4304 struct ecommunity
*ecom
= NULL
;
4306 /* Remove the RT from the RT list */
4307 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4308 if (ecommunity_match(ecom
, ecomdel
)) {
4309 ecommunity_free(&ecom
);
4316 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4319 * Temporary assert to make SA happy.
4320 * The ALL_LIST_ELEMENTS macro above has a NULL check
4321 * which means that SA is going to complain about
4322 * the list_isempty call, which doesn't NULL check.
4323 * So until we get this situation cleaned up, here
4326 assert(bgp_vrf
->vrf_export_rtl
);
4328 /* fall back to auto-generated RT if this was the last RT */
4329 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4330 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4331 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4334 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4338 * Handle change to BGP router id. This is invoked twice by the change
4339 * handler, first before the router id has been changed and then after
4340 * the router id has been changed. The first invocation will result in
4341 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4342 * will result in the routes being re-advertised.
4344 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4348 /* delete and withdraw all the type-5 routes
4349 stored in the global table for this vrf
4351 withdraw_router_id_vrf(bgp
);
4353 /* delete all the VNI routes (type-2/type-3) routes for all the
4356 hash_iterate(bgp
->vnihash
,
4357 (void (*)(struct hash_backet
*,
4358 void *))withdraw_router_id_vni
,
4362 /* advertise all routes in the vrf as type-5 routes with the new
4365 update_router_id_vrf(bgp
);
4367 /* advertise all the VNI routes (type-2/type-3) routes with the
4370 hash_iterate(bgp
->vnihash
,
4371 (void (*)(struct hash_backet
*,
4372 void *))update_router_id_vni
,
4378 * Handle change to auto-RT algorithm - update and advertise local routes.
4380 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4382 hash_iterate(bgp
->vnihash
,
4383 (void (*)(struct hash_backet
*,
4384 void*))update_autort_vni
,
4389 * Handle change to export RT - update and advertise local routes.
4391 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4393 return update_routes_for_vni(bgp
, vpn
);
4396 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4399 delete_withdraw_vrf_routes(bgp_vrf
);
4401 update_advertise_vrf_routes(bgp_vrf
);
4405 * Handle change to RD. This is invoked twice by the change handler,
4406 * first before the RD has been changed and then after the RD has
4407 * been changed. The first invocation will result in local routes
4408 * of this VNI being deleted and withdrawn and the next will result
4409 * in the routes being re-advertised.
4411 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4415 delete_withdraw_vni_routes(bgp
, vpn
);
4417 update_advertise_vni_routes(bgp
, vpn
);
4421 * Install routes for this VNI. Invoked upon change to Import RT.
4423 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4425 return install_routes_for_vni(bgp
, vpn
);
4429 * Uninstall all routes installed for this VNI. Invoked upon change
4432 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4434 return uninstall_routes_for_vni(bgp
, vpn
);
4438 * TODO: Hardcoded for a maximum of 2 VNIs right now
4440 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4445 vni1
= label2vni(label
);
4446 if (num_labels
== 2) {
4447 vni2
= label2vni(label
+ 1);
4448 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4450 snprintf(buf
, len
, "%u", vni1
);
4455 * Function to convert evpn route to json format.
4456 * NOTE: We don't use prefix2str as the output here is a bit different.
4458 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
4460 char buf1
[ETHER_ADDR_STRLEN
];
4461 char buf2
[PREFIX2STR_BUFFER
];
4466 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4467 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4468 json_object_int_add(json
, "ethTag",
4469 p
->prefix
.imet_addr
.eth_tag
);
4470 json_object_int_add(json
, "ipLen",
4471 is_evpn_prefix_ipaddr_v4(p
)
4474 json_object_string_add(json
, "ip",
4475 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4476 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4477 if (is_evpn_prefix_ipaddr_none(p
)) {
4478 json_object_int_add(json
, "routeType",
4479 p
->prefix
.route_type
);
4480 json_object_int_add(json
, "ethTag",
4481 p
->prefix
.macip_addr
.eth_tag
);
4482 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4483 json_object_string_add(json
, "mac",
4484 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4490 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4493 json_object_int_add(json
, "routeType",
4494 p
->prefix
.route_type
);
4495 json_object_int_add(json
, "ethTag",
4496 p
->prefix
.macip_addr
.eth_tag
);
4497 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4498 json_object_string_add(json
, "mac",
4499 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4502 json_object_int_add(json
, "ipLen",
4503 is_evpn_prefix_ipaddr_v4(p
)
4506 json_object_string_add(
4509 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4511 PREFIX2STR_BUFFER
));
4514 /* Currently, this is to cater to other AF_ETHERNET code. */
4519 * Function to convert evpn route to string.
4520 * NOTE: We don't use prefix2str as the output here is a bit different.
4522 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
4524 char buf1
[ETHER_ADDR_STRLEN
];
4525 char buf2
[PREFIX2STR_BUFFER
];
4526 char buf3
[ESI_STR_LEN
];
4528 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4529 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
4530 p
->prefix
.imet_addr
.eth_tag
,
4531 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4533 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4534 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4535 if (is_evpn_prefix_ipaddr_none(p
))
4536 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4537 p
->prefix
.route_type
,
4538 p
->prefix
.macip_addr
.eth_tag
,
4540 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4545 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4547 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
4548 p
->prefix
.route_type
,
4549 p
->prefix
.macip_addr
.eth_tag
,
4551 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4553 family
== AF_INET
? IPV4_MAX_BITLEN
4556 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4558 PREFIX2STR_BUFFER
));
4560 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
4561 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4562 p
->prefix
.route_type
,
4563 p
->prefix
.prefix_addr
.eth_tag
,
4564 p
->prefix
.prefix_addr
.ip_prefix_length
,
4565 is_evpn_prefix_ipaddr_v4(p
)
4566 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
4567 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
4568 } else if (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
4569 snprintf(buf
, len
, "[%d]:[%s]:[%d]:[%s]",
4570 p
->prefix
.route_type
,
4571 esi_to_str(&p
->prefix
.es_addr
.esi
, buf3
, sizeof(buf3
)),
4572 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4574 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
));
4576 /* For EVPN route types not supported yet. */
4577 snprintf(buf
, len
, "(unsupported route type %d)",
4578 p
->prefix
.route_type
);
4585 * Encode EVPN prefix in Update (MP_REACH)
4587 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
4588 struct prefix_rd
*prd
, mpls_label_t
*label
,
4589 uint32_t num_labels
, struct attr
*attr
,
4590 int addpath_encode
, uint32_t addpath_tx_id
)
4592 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4593 int len
, ipa_len
= 0;
4596 stream_putl(s
, addpath_tx_id
);
4599 stream_putc(s
, evp
->prefix
.route_type
);
4601 switch (evp
->prefix
.route_type
) {
4602 case BGP_EVPN_MAC_IP_ROUTE
:
4603 if (is_evpn_prefix_ipaddr_v4(evp
))
4604 ipa_len
= IPV4_MAX_BYTELEN
;
4605 else if (is_evpn_prefix_ipaddr_v6(evp
))
4606 ipa_len
= IPV6_MAX_BYTELEN
;
4607 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4608 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4609 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4611 stream_putc(s
, len
);
4612 stream_put(s
, prd
->val
, 8); /* RD */
4614 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
4616 stream_put(s
, 0, 10);
4617 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4618 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4619 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4620 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4621 if (ipa_len
) /* IP */
4622 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4624 /* 1st label is the L2 VNI */
4625 stream_put(s
, label
, BGP_LABEL_BYTES
);
4626 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4627 if (ipa_len
&& num_labels
> 1)
4628 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4631 case BGP_EVPN_IMET_ROUTE
:
4632 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4633 stream_put(s
, prd
->val
, 8); /* RD */
4634 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4635 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4636 /* Originating Router's IP Addr */
4637 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4640 case BGP_EVPN_ES_ROUTE
:
4641 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4642 stream_put(s
, prd
->val
, 8); /* RD */
4643 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4644 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4646 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4649 case BGP_EVPN_IP_PREFIX_ROUTE
:
4650 /* TODO: AddPath support. */
4651 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4659 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4660 struct bgp_nlri
*packet
, int withdraw
)
4666 uint32_t addpath_id
;
4667 int addpath_encoded
;
4672 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4674 lim
= pnt
+ packet
->length
;
4676 safi
= packet
->safi
;
4680 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
4681 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
4682 PEER_CAP_ADDPATH_AF_TX_RCV
));
4684 for (; pnt
< lim
; pnt
+= psize
) {
4685 /* Clear prefix structure. */
4686 memset(&p
, 0, sizeof(struct prefix
));
4688 /* Deal with path-id if AddPath is supported. */
4689 if (addpath_encoded
) {
4690 /* When packet overflow occurs return immediately. */
4691 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4694 addpath_id
= ntohl(*((uint32_t *)pnt
));
4695 pnt
+= BGP_ADDPATH_ID_LEN
;
4698 /* All EVPN NLRI types start with type and length. */
4705 /* When packet overflow occur return immediately. */
4706 if (pnt
+ psize
> lim
)
4710 case BGP_EVPN_MAC_IP_ROUTE
:
4711 if (process_type2_route(peer
, afi
, safi
,
4712 withdraw
? NULL
: attr
, pnt
,
4713 psize
, addpath_id
)) {
4715 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4716 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4721 case BGP_EVPN_IMET_ROUTE
:
4722 if (process_type3_route(peer
, afi
, safi
,
4723 withdraw
? NULL
: attr
, pnt
,
4724 psize
, addpath_id
)) {
4726 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4727 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4732 case BGP_EVPN_ES_ROUTE
:
4733 if (process_type4_route(peer
, afi
, safi
,
4734 withdraw
? NULL
: attr
, pnt
,
4735 psize
, addpath_id
)) {
4737 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4738 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4743 case BGP_EVPN_IP_PREFIX_ROUTE
:
4744 if (process_type5_route(peer
, afi
, safi
, attr
, pnt
,
4745 psize
, addpath_id
, withdraw
)) {
4747 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4748 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4758 /* Packet length consistency check. */
4766 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
4767 * The mapping will be used during route processing.
4768 * bgp_def: default bgp instance
4769 * bgp_vrf: specific bgp vrf instance on which RT is configured
4771 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
4774 struct ecommunity_val
*eval
= NULL
;
4775 struct listnode
*node
= NULL
, *nnode
= NULL
;
4776 struct ecommunity
*ecom
= NULL
;
4778 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4779 for (i
= 0; i
< ecom
->size
; i
++) {
4780 eval
= (struct ecommunity_val
*)(ecom
->val
4782 * ECOMMUNITY_SIZE
));
4783 map_vrf_to_rt(bgp_vrf
, eval
);
4789 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
4791 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
4794 struct ecommunity_val
*eval
;
4795 struct listnode
*node
, *nnode
;
4796 struct ecommunity
*ecom
;
4798 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4799 for (i
= 0; i
< ecom
->size
; i
++) {
4800 struct vrf_irt_node
*irt
;
4801 struct ecommunity_val eval_tmp
;
4803 eval
= (struct ecommunity_val
*)(ecom
->val
4805 * ECOMMUNITY_SIZE
));
4806 /* If using "automatic" RT, we only care about the
4807 * local-admin sub-field.
4808 * This is to facilitate using VNI as the RT for EBGP
4811 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4812 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
4813 BGP_VRF_IMPORT_RT_CFGD
))
4814 mask_ecom_global_admin(&eval_tmp
, eval
);
4816 irt
= lookup_vrf_import_rt(&eval_tmp
);
4818 unmap_vrf_from_rt(bgp_vrf
, irt
);
4825 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
4826 * The mapping will be used during route processing.
4828 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4831 struct ecommunity_val
*eval
;
4832 struct listnode
*node
, *nnode
;
4833 struct ecommunity
*ecom
;
4835 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4836 for (i
= 0; i
< ecom
->size
; i
++) {
4837 eval
= (struct ecommunity_val
*)(ecom
->val
4839 * ECOMMUNITY_SIZE
));
4840 map_vni_to_rt(bgp
, vpn
, eval
);
4846 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
4848 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4851 struct ecommunity_val
*eval
;
4852 struct listnode
*node
, *nnode
;
4853 struct ecommunity
*ecom
;
4855 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4856 for (i
= 0; i
< ecom
->size
; i
++) {
4857 struct irt_node
*irt
;
4858 struct ecommunity_val eval_tmp
;
4860 eval
= (struct ecommunity_val
*)(ecom
->val
4862 * ECOMMUNITY_SIZE
));
4863 /* If using "automatic" RT, we only care about the
4864 * local-admin sub-field.
4865 * This is to facilitate using VNI as the RT for EBGP
4868 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4869 if (!is_import_rt_configured(vpn
))
4870 mask_ecom_global_admin(&eval_tmp
, eval
);
4872 irt
= lookup_import_rt(bgp
, &eval_tmp
);
4874 unmap_vni_from_rt(bgp
, vpn
, irt
);
4880 * Derive Import RT automatically for VNI and map VNI to RT.
4881 * The mapping will be used during route processing.
4883 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4885 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
4886 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
4889 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
4893 * Derive Export RT automatically for VNI.
4895 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4897 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
4898 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
4902 * Derive RD automatically for VNI using passed information - it
4903 * is of the form RouterId:unique-id-for-vni.
4905 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
4907 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
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(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4918 vpn
->prd
.family
= AF_UNSPEC
;
4919 vpn
->prd
.prefixlen
= 64;
4920 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
4921 (void)str2prefix_rd(buf
, &vpn
->prd
);
4922 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
4928 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
4930 struct bgpevpn
*vpn
;
4933 memset(&tmp
, 0, sizeof(struct bgpevpn
));
4935 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
4940 * Create a new vpn - invoked upon configuration or zebra notification.
4942 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
4943 struct in_addr originator_ip
,
4944 vrf_id_t tenant_vrf_id
)
4946 struct bgpevpn
*vpn
;
4951 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
4955 /* Set values - RD and RT set to defaults. */
4957 vpn
->originator_ip
= originator_ip
;
4958 vpn
->tenant_vrf_id
= tenant_vrf_id
;
4960 /* Initialize route-target import and export lists */
4961 vpn
->import_rtl
= list_new();
4962 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4963 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
4964 vpn
->export_rtl
= list_new();
4965 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4966 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
4967 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
4968 derive_rd_rt_for_vni(bgp
, vpn
);
4970 /* Initialize EVPN route table. */
4971 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
4974 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
4975 XFREE(MTYPE_BGP_EVPN
, vpn
);
4979 /* add to l2vni list on corresponding vrf */
4980 bgpevpn_link_to_l3vni(vpn
);
4982 QOBJ_REG(vpn
, bgpevpn
);
4987 * Free a given VPN - called in multiple scenarios such as zebra
4988 * notification, configuration being deleted, advertise-all-vni disabled etc.
4989 * This just frees appropriate memory, caller should have taken other
4992 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4994 bgpevpn_unlink_from_l3vni(vpn
);
4995 bgp_table_unlock(vpn
->route_table
);
4996 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4997 list_delete_and_null(&vpn
->import_rtl
);
4998 list_delete_and_null(&vpn
->export_rtl
);
4999 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5000 hash_release(bgp
->vnihash
, vpn
);
5002 XFREE(MTYPE_BGP_EVPN
, vpn
);
5008 struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
)
5013 memset(&tmp
, 0, sizeof(struct evpnes
));
5014 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
5015 es
= hash_lookup(bgp
->esihash
, &tmp
);
5020 * Create a new local es - invoked upon zebra notification.
5022 struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
,
5024 struct ipaddr
*originator_ip
)
5032 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct evpnes
));
5036 /* set the ESI and originator_ip */
5037 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
5038 memcpy(&es
->originator_ip
, originator_ip
, sizeof(struct ipaddr
));
5040 /* Initialise the VTEP list */
5041 es
->vtep_list
= list_new();
5042 es
->vtep_list
->cmp
= (int (*)(void *, void *))evpn_vtep_ip_cmp
;
5044 /* auto derive RD for this es */
5045 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
5046 es
->prd
.family
= AF_UNSPEC
;
5047 es
->prd
.prefixlen
= 64;
5048 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), es
->rd_id
);
5049 (void)str2prefix_rd(buf
, &es
->prd
);
5051 /* Initialize the ES route table */
5052 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5055 if (!hash_get(bgp
->esihash
, es
, hash_alloc_intern
)) {
5056 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5060 QOBJ_REG(es
, evpnes
);
5066 * This just frees appropriate memory, caller should have taken other
5069 void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
)
5071 list_delete_and_null(&es
->vtep_list
);
5072 bgp_table_unlock(es
->route_table
);
5073 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
5074 hash_release(bgp
->esihash
, es
);
5076 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5080 * Import evpn route from global table to VNI/VRF/ESI.
5082 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5083 struct prefix
*p
, struct bgp_info
*ri
)
5085 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 1);
5089 * Unimport evpn route from VNI/VRF/ESI.
5091 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5092 struct prefix
*p
, struct bgp_info
*ri
)
5094 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 0);
5097 /* filter routes which have martian next hops */
5098 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5102 struct bgp_node
*rd_rn
, *rn
;
5103 struct bgp_table
*table
;
5104 struct bgp_info
*ri
;
5109 /* Walk entire global routing table and evaluate routes which could be
5110 * imported into this VPN. Note that we cannot just look at the routes
5111 * for the VNI's RD -
5112 * remote routes applicable for this VNI could have any RD.
5114 /* EVPN routes are a 2-level table. */
5115 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
5116 rd_rn
= bgp_route_next(rd_rn
)) {
5117 table
= (struct bgp_table
*)(rd_rn
->info
);
5121 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5123 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
5125 /* Consider "valid" remote routes applicable for
5127 if (!(ri
->type
== ZEBRA_ROUTE_BGP
5128 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
5131 if (bgp_nexthop_self(bgp
, ri
->attr
->nexthop
)) {
5133 char attr_str
[BUFSIZ
];
5134 char pbuf
[PREFIX_STRLEN
];
5136 bgp_dump_attr(ri
->attr
, attr_str
,
5139 if (bgp_debug_update(ri
->peer
, &rn
->p
,
5142 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
5149 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5152 bgp_rib_remove(rn
, ri
, ri
->peer
, afi
,
5163 * Handle del of a local MACIP.
5165 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5168 struct bgpevpn
*vpn
;
5169 struct prefix_evpn p
;
5171 /* Lookup VNI hash - should exist. */
5172 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5173 if (!vpn
|| !is_vni_live(vpn
)) {
5174 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP DEL",
5175 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5179 /* Remove EVPN type-2 route and schedule for processing. */
5180 build_evpn_type2_prefix(&p
, mac
, ip
);
5181 delete_evpn_route(bgp
, vpn
, &p
);
5187 * Handle add of a local MACIP.
5189 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5190 struct ipaddr
*ip
, uint8_t flags
)
5192 struct bgpevpn
*vpn
;
5193 struct prefix_evpn p
;
5195 /* Lookup VNI hash - should exist. */
5196 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5197 if (!vpn
|| !is_vni_live(vpn
)) {
5198 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP ADD",
5199 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5203 /* Create EVPN type-2 route and schedule for processing. */
5204 build_evpn_type2_prefix(&p
, mac
, ip
);
5205 if (update_evpn_route(bgp
, vpn
, &p
, flags
)) {
5206 char buf
[ETHER_ADDR_STRLEN
];
5207 char buf2
[INET6_ADDRSTRLEN
];
5210 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5211 bgp
->vrf_id
, vpn
->vni
,
5212 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5215 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5216 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5223 static void link_l2vni_hash_to_l3vni(struct hash_backet
*backet
,
5224 struct bgp
*bgp_vrf
)
5226 struct bgpevpn
*vpn
= NULL
;
5227 struct bgp
*bgp_def
= NULL
;
5229 bgp_def
= bgp_get_default();
5232 vpn
= (struct bgpevpn
*)backet
->data
;
5233 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5234 bgpevpn_link_to_l3vni(vpn
);
5237 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
, struct ethaddr
*rmac
,
5238 struct in_addr originator_ip
, int filter
)
5240 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5241 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5242 struct listnode
*node
= NULL
;
5243 struct bgpevpn
*vpn
= NULL
;
5246 /* get the default instance - required to get the AS number for VRF
5249 bgp_def
= bgp_get_default();
5252 "Cannot process L3VNI %u ADD - default BGP instance not yet created",
5258 /* if the BGP vrf instance doesnt exist - create one */
5259 bgp_vrf
= bgp_lookup_by_name(vrf_id_to_name(vrf_id
));
5264 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5265 BGP_INSTANCE_TYPE_VRF
);
5267 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
:
5268 zlog_err("'bgp multiple-instance' not present\n");
5270 case BGP_ERR_AS_MISMATCH
:
5271 zlog_err("BGP is already running; AS is %u\n", as
);
5273 case BGP_ERR_INSTANCE_MISMATCH
:
5274 zlog_err("BGP instance name and AS number mismatch\n");
5278 /* mark as auto created */
5279 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5282 /* associate with l3vni */
5283 bgp_vrf
->l3vni
= l3vni
;
5285 /* set the router mac - to be used in mac-ip routes for this vrf */
5286 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
5288 /* set the originator ip */
5289 bgp_vrf
->originator_ip
= originator_ip
;
5291 /* set the right filter - are we using l3vni only for prefix routes? */
5293 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5295 /* auto derive RD/RT */
5296 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5297 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5298 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5299 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5300 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5302 /* link all corresponding l2vnis */
5303 hash_iterate(bgp_def
->vnihash
,
5304 (void (*)(struct hash_backet
*,
5305 void *))link_l2vni_hash_to_l3vni
,
5308 /* Only update all corresponding type-2 routes if we are advertising two
5309 * labels along with type-2 routes
5312 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5313 update_routes_for_vni(bgp_def
, vpn
);
5315 /* advertise type-5 routes if needed */
5316 update_advertise_vrf_routes(bgp_vrf
);
5318 /* install all remote routes belonging to this l3vni into correspondng
5320 install_routes_for_vrf(bgp_vrf
);
5325 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5327 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5328 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5329 struct listnode
*node
= NULL
;
5330 struct listnode
*next
= NULL
;
5331 struct bgpevpn
*vpn
= NULL
;
5333 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5336 "Cannot process L3VNI %u Del - Could not find BGP instance",
5341 bgp_def
= bgp_get_default();
5344 "Cannot process L3VNI %u Del - Could not find default BGP instance",
5349 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5350 * bgp_delete would not remove/decrement bgp_info of the ip_prefix
5351 * routes. This will uninstalling the routes from zebra and decremnt the
5354 uninstall_routes_for_vrf(bgp_vrf
);
5356 /* delete/withdraw all type-5 routes */
5357 delete_withdraw_vrf_routes(bgp_vrf
);
5359 /* remove the l3vni from vrf instance */
5362 /* remove the Rmac from the BGP vrf */
5363 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5366 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5367 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5368 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5370 if (!list_isempty(bgp_vrf
->vrf_export_rtl
)) {
5371 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5374 /* update all corresponding local mac-ip routes */
5375 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5376 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5377 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5378 update_routes_for_vni(bgp_def
, vpn
);
5382 /* If any L2VNIs point to this instance, unlink them. */
5383 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5384 bgpevpn_unlink_from_l3vni(vpn
);
5386 /* Delete the instance if it was autocreated */
5387 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5388 bgp_delete(bgp_vrf
);
5394 * Handle del of a local VNI.
5396 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5398 struct bgpevpn
*vpn
;
5400 /* Locate VNI hash */
5401 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5403 if (bgp_debug_zebra(NULL
))
5405 "%u: VNI hash entry for VNI %u not found at DEL",
5410 /* Remove all local EVPN routes and schedule for processing (to
5411 * withdraw from peers).
5413 delete_routes_for_vni(bgp
, vpn
);
5416 * tunnel is no longer active, del tunnel ip address from tip_hash
5418 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5420 /* Clear "live" flag and see if hash needs to be freed. */
5421 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5422 if (!is_vni_configured(vpn
))
5423 bgp_evpn_free(bgp
, vpn
);
5429 * Handle add (or update) of a local VNI. The VNI changes we care
5430 * about are for the local-tunnel-ip and the (tenant) VRF.
5432 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5433 struct in_addr originator_ip
, vrf_id_t tenant_vrf_id
)
5435 struct bgpevpn
*vpn
;
5436 struct prefix_evpn p
;
5438 /* Lookup VNI. If present and no change, exit. */
5439 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5442 if (is_vni_live(vpn
)
5443 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5444 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5445 /* Probably some other param has changed that we don't
5449 /* Update tenant_vrf_id if it has changed. */
5450 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5451 bgpevpn_unlink_from_l3vni(vpn
);
5452 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5453 bgpevpn_link_to_l3vni(vpn
);
5456 /* If tunnel endpoint IP has changed, update (and delete prior
5457 * type-3 route, if needed.)
5459 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5460 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5462 /* Update all routes with new endpoint IP and/or export RT
5465 if (is_vni_live(vpn
))
5466 update_routes_for_vni(bgp
, vpn
);
5469 /* Create or update as appropriate. */
5471 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
);
5474 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5480 /* if the VNI is live already, there is nothing more to do */
5481 if (is_vni_live(vpn
))
5484 /* Mark as "live" */
5485 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5487 /* tunnel is now active, add tunnel-ip to db */
5488 bgp_tip_add(bgp
, &originator_ip
);
5490 /* filter routes as nexthop database has changed */
5491 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5493 /* Create EVPN type-3 route and schedule for processing. */
5494 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5495 if (update_evpn_route(bgp
, vpn
, &p
, 0)) {
5496 zlog_err("%u: Type3 route creation failure for VNI %u",
5501 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5505 install_routes_for_vni(bgp
, vpn
);
5507 /* If we are advertising gateway mac-ip
5508 It needs to be conveyed again to zebra */
5509 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5515 * bgp_evpn_local_es_del
5517 int bgp_evpn_local_es_del(struct bgp
*bgp
,
5519 struct ipaddr
*originator_ip
)
5521 char buf
[ESI_STR_LEN
];
5522 struct evpnes
*es
= NULL
;
5524 if (!bgp
->esihash
) {
5525 zlog_err("%u: ESI hash not yet created", bgp
->vrf_id
);
5529 /* Lookup ESI hash - should exist. */
5530 es
= bgp_evpn_lookup_es(bgp
, esi
);
5532 zlog_warn("%u: ESI hash entry for ESI %s at Local ES DEL",
5534 esi_to_str(esi
, buf
, sizeof(buf
)));
5538 /* Delete all local EVPN ES routes from ESI table
5539 * and schedule for processing (to withdraw from peers))
5541 delete_routes_for_es(bgp
, es
);
5543 /* free the hash entry */
5544 bgp_evpn_es_free(bgp
, es
);
5550 * bgp_evpn_local_es_add
5552 int bgp_evpn_local_es_add(struct bgp
*bgp
,
5554 struct ipaddr
*originator_ip
)
5556 char buf
[ESI_STR_LEN
];
5557 struct evpnes
*es
= NULL
;
5558 struct prefix_evpn p
;
5560 if (!bgp
->esihash
) {
5561 zlog_err("%u: ESI hash not yet created", bgp
->vrf_id
);
5565 /* create the new es */
5566 es
= bgp_evpn_lookup_es(bgp
, esi
);
5568 es
= bgp_evpn_es_new(bgp
, esi
, originator_ip
);
5571 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
5572 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5576 UNSET_FLAG(es
->flags
, EVPNES_REMOTE
);
5577 SET_FLAG(es
->flags
, EVPNES_LOCAL
);
5579 build_evpn_type4_prefix(&p
, esi
, originator_ip
->ipaddr_v4
);
5580 if (update_evpn_type4_route(bgp
, es
, &p
)) {
5581 zlog_err("%u: Type4 route creation failure for ESI %s",
5582 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5586 /* import all remote ES routes in th ES table */
5587 install_routes_for_es(bgp
, es
);
5593 * Cleanup EVPN information on disable - Need to delete and withdraw
5594 * EVPN routes from peers.
5596 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5598 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
5599 void *))cleanup_vni_on_disable
,
5604 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5605 * BGP instance (default) is being freed.
5607 void bgp_evpn_cleanup(struct bgp
*bgp
)
5609 hash_iterate(bgp
->vnihash
,
5610 (void (*)(struct hash_backet
*, void *))free_vni_entry
,
5613 hash_free(bgp
->import_rt_hash
);
5614 bgp
->import_rt_hash
= NULL
;
5616 hash_free(bgp
->vrf_import_rt_hash
);
5617 bgp
->vrf_import_rt_hash
= NULL
;
5619 hash_free(bgp
->vnihash
);
5620 bgp
->vnihash
= NULL
;
5622 hash_free(bgp
->esihash
);
5623 bgp
->esihash
= NULL
;
5625 list_delete_and_null(&bgp
->vrf_import_rtl
);
5626 list_delete_and_null(&bgp
->vrf_export_rtl
);
5627 list_delete_and_null(&bgp
->l2vnis
);
5631 * Initialization for EVPN
5634 * hash for RT to VNI
5636 void bgp_evpn_init(struct bgp
*bgp
)
5639 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
5641 hash_create(esi_hash_keymake
, esi_cmp
,
5642 "BGP EVPN Local ESI Hash");
5643 bgp
->import_rt_hash
=
5644 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
5645 "BGP Import RT Hash");
5646 bgp
->vrf_import_rt_hash
=
5647 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
5648 "BGP VRF Import RT Hash");
5649 bgp
->vrf_import_rtl
= list_new();
5650 bgp
->vrf_import_rtl
->cmp
=
5651 (int (*)(void *, void *))evpn_route_target_cmp
;
5652 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
5653 bgp
->vrf_export_rtl
= list_new();
5654 bgp
->vrf_export_rtl
->cmp
=
5655 (int (*)(void *, void *))evpn_route_target_cmp
;
5656 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
5657 bgp
->l2vnis
= list_new();
5658 bgp
->l2vnis
->cmp
= (int (*)(void *, void *))vni_hash_cmp
;
5661 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
5663 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);