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
))
1742 /* Set up extended community. */
1743 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1745 /* First, create (or fetch) route node within the VNI. */
1746 /* NOTE: There is no RD here. */
1747 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1749 /* Create or update route entry. */
1750 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1753 attr_new
= ri
->attr
;
1755 /* Perform route selection; this is just to set the flags correctly
1756 * as local route in the VNI always wins.
1758 evpn_route_select_install(bgp
, vpn
, rn
);
1759 bgp_unlock_node(rn
);
1761 /* If this is a new route or some attribute has changed, export the
1762 * route to the global table. The route will be advertised to peers
1763 * from there. Note that this table is a 2-level tree (RD-level +
1764 * Prefix-level) similar to L3VPN routes.
1767 struct bgp_info
*global_ri
;
1769 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1770 (struct prefix
*)p
, &vpn
->prd
);
1771 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1, 0,
1774 /* Schedule for processing and unlock node. */
1775 bgp_process(bgp
, rn
, afi
, safi
);
1776 bgp_unlock_node(rn
);
1779 /* Unintern temporary. */
1780 aspath_unintern(&attr
.aspath
);
1786 * Delete EVPN route entry.
1787 * The entry can be in ESI/VNI table or the global table.
1789 static void delete_evpn_route_entry(struct bgp
*bgp
,
1790 afi_t afi
, safi_t safi
,
1791 struct bgp_node
*rn
,
1792 struct bgp_info
**ri
)
1794 struct bgp_info
*tmp_ri
;
1798 /* Now, find matching route. */
1799 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
)
1800 if (tmp_ri
->peer
== bgp
->peer_self
1801 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1802 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1807 /* Mark route for delete. */
1809 bgp_info_delete(rn
, tmp_ri
);
1814 /* Delete EVPN ES (type-4) route */
1815 static int delete_evpn_type4_route(struct bgp
*bgp
,
1817 struct prefix_evpn
*p
)
1819 afi_t afi
= AFI_L2VPN
;
1820 safi_t safi
= SAFI_EVPN
;
1821 struct bgp_info
*ri
;
1822 struct bgp_node
*rn
= NULL
; /* rn in esi table */
1823 struct bgp_node
*global_rn
= NULL
; /* rn in global table */
1825 /* First, locate the route node within the ESI.
1826 * If it doesn't exist, ther is nothing to do.
1827 * Note: there is no RD here.
1829 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
1833 /* Next, locate route node in the global EVPN routing table.
1834 * Note that this table is a 2-level tree (RD-level + Prefix-level)
1836 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1837 (struct prefix
*)p
, &es
->prd
);
1840 /* Delete route entry in the global EVPN table. */
1841 delete_evpn_route_entry(bgp
, afi
, safi
,
1844 /* Schedule for processing - withdraws to peers happen from
1848 bgp_process(bgp
, global_rn
, afi
, safi
);
1849 bgp_unlock_node(global_rn
);
1853 * Delete route entry in the ESI route table.
1854 * This can just be removed.
1856 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &ri
);
1858 bgp_info_reap(rn
, ri
);
1859 bgp_unlock_node(rn
);
1863 /* Delete EVPN type5 route */
1864 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1866 afi_t afi
= AFI_L2VPN
;
1867 safi_t safi
= SAFI_EVPN
;
1868 struct bgp_node
*rn
= NULL
;
1869 struct bgp_info
*ri
= NULL
;
1870 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
1872 bgp_def
= bgp_get_default();
1876 /* locate the global route entry for this type-5 prefix */
1877 rn
= bgp_afi_node_lookup(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1878 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1882 delete_evpn_route_entry(bgp_def
, afi
, safi
, rn
, &ri
);
1884 bgp_process(bgp_def
, rn
, afi
, safi
);
1885 bgp_unlock_node(rn
);
1890 * Delete EVPN route (of type based on prefix) for specified VNI and
1891 * schedule for processing.
1893 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1894 struct prefix_evpn
*p
)
1896 struct bgp_node
*rn
, *global_rn
;
1897 struct bgp_info
*ri
;
1898 afi_t afi
= AFI_L2VPN
;
1899 safi_t safi
= SAFI_EVPN
;
1901 /* First, locate the route node within the VNI. If it doesn't exist,
1903 * is nothing further to do.
1905 /* NOTE: There is no RD here. */
1906 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1910 /* Next, locate route node in the global EVPN routing table. Note that
1911 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1914 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1915 (struct prefix
*)p
, &vpn
->prd
);
1917 /* Delete route entry in the global EVPN table. */
1918 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &ri
);
1920 /* Schedule for processing - withdraws to peers happen from
1924 bgp_process(bgp
, global_rn
, afi
, safi
);
1925 bgp_unlock_node(global_rn
);
1928 /* Delete route entry in the VNI route table. This can just be removed.
1930 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &ri
);
1932 bgp_info_reap(rn
, ri
);
1933 bgp_unlock_node(rn
);
1939 * Update all type-2 (MACIP) local routes for this VNI - these should also
1940 * be scheduled for advertise to peers.
1942 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1946 struct bgp_node
*rn
;
1947 struct bgp_info
*ri
;
1949 struct attr attr_sticky
;
1950 struct attr attr_def_gw
;
1951 struct attr attr_ip6_ll
;
1952 struct attr
*attr_new
;
1953 int add_l3_ecomm
= 0;
1957 memset(&attr
, 0, sizeof(struct attr
));
1958 memset(&attr_sticky
, 0, sizeof(struct attr
));
1959 memset(&attr_def_gw
, 0, sizeof(struct attr
));
1960 memset(&attr_ip6_ll
, 0, sizeof(struct attr
));
1962 /* Build path-attribute - multiple type-2 routes for this VNI will share
1963 * the same path attribute, but we need separate structures for sticky
1964 * MACs, default gateway and IPv6 link-local addresses (no L3 RT/RMAC).
1966 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1967 bgp_attr_default_set(&attr_sticky
, BGP_ORIGIN_IGP
);
1968 bgp_attr_default_set(&attr_def_gw
, BGP_ORIGIN_IGP
);
1969 attr
.nexthop
= vpn
->originator_ip
;
1970 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1971 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1972 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1973 attr_sticky
.nexthop
= vpn
->originator_ip
;
1974 attr_sticky
.mp_nexthop_global_in
= vpn
->originator_ip
;
1975 attr_sticky
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1976 attr_sticky
.sticky
= 1;
1977 bgpevpn_get_rmac(vpn
, &attr_sticky
.rmac
);
1978 attr_def_gw
.nexthop
= vpn
->originator_ip
;
1979 attr_def_gw
.mp_nexthop_global_in
= vpn
->originator_ip
;
1980 attr_def_gw
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1981 attr_def_gw
.default_gw
= 1;
1982 bgpevpn_get_rmac(vpn
, &attr_def_gw
.rmac
);
1983 bgp_attr_default_set(&attr_ip6_ll
, BGP_ORIGIN_IGP
);
1984 attr_ip6_ll
.nexthop
= vpn
->originator_ip
;
1985 attr_ip6_ll
.mp_nexthop_global_in
= vpn
->originator_ip
;
1986 attr_ip6_ll
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1988 /* Add L3 VNI RTs and RMAC for non IPv6 link-local attributes if
1989 * using L3 VNI for type-2 routes also.
1991 if (CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
))
1994 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1995 build_evpn_route_extcomm(vpn
, &attr_sticky
, add_l3_ecomm
);
1996 build_evpn_route_extcomm(vpn
, &attr_def_gw
, add_l3_ecomm
);
1997 build_evpn_route_extcomm(vpn
, &attr_ip6_ll
, 0);
1999 /* Walk this VNI's route table and update local type-2 routes. For any
2000 * routes updated, update corresponding entry in the global table too.
2002 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2003 rn
= bgp_route_next(rn
)) {
2004 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2005 struct bgp_node
*rd_rn
;
2006 struct bgp_info
*global_ri
;
2008 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2011 if (is_evpn_prefix_ipaddr_v6(evp
) &&
2012 IN6_IS_ADDR_LINKLOCAL(&evp
->prefix
.macip_addr
.ip
.ipaddr_v6
))
2013 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
2014 &attr_ip6_ll
, 0, 1, &ri
, 0);
2016 if (evpn_route_is_sticky(bgp
, rn
))
2017 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
2018 &attr_sticky
, 0, 1, &ri
,
2020 else if (evpn_route_is_def_gw(bgp
, rn
)) {
2021 if (is_evpn_prefix_ipaddr_v6(evp
))
2022 attr_def_gw
.router_flag
= 1;
2023 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
2024 &attr_def_gw
, 0, 1, &ri
,
2027 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
2028 &attr
, 0, 1, &ri
, 0);
2031 /* If a local route exists for this prefix, we need to update
2032 * the global routing table too.
2037 /* Perform route selection; this is just to set the flags
2039 * as local route in the VNI always wins.
2041 evpn_route_select_install(bgp
, vpn
, rn
);
2043 attr_new
= ri
->attr
;
2045 /* Update route in global routing table. */
2046 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2047 (struct prefix
*)evp
, &vpn
->prd
);
2049 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
2052 /* Schedule for processing and unlock node. */
2053 bgp_process(bgp
, rd_rn
, afi
, safi
);
2054 bgp_unlock_node(rd_rn
);
2057 /* Unintern temporary. */
2058 aspath_unintern(&attr
.aspath
);
2059 aspath_unintern(&attr_sticky
.aspath
);
2060 aspath_unintern(&attr_def_gw
.aspath
);
2061 aspath_unintern(&attr_ip6_ll
.aspath
);
2067 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2068 * global routing table. These are also scheduled for withdraw from peers.
2070 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2074 struct bgp_node
*rdrn
, *rn
;
2075 struct bgp_table
*table
;
2076 struct bgp_info
*ri
;
2081 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
2082 if (rdrn
&& rdrn
->info
) {
2083 table
= (struct bgp_table
*)rdrn
->info
;
2084 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2085 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2087 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2090 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &ri
);
2092 bgp_process(bgp
, rn
, afi
, safi
);
2096 /* Unlock RD node. */
2098 bgp_unlock_node(rdrn
);
2104 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2105 * table as well as the per-VNI route table.
2107 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2111 struct bgp_node
*rn
;
2112 struct bgp_info
*ri
;
2117 /* First, walk the global route table for this VNI's type-2 local
2119 * EVPN routes are a 2-level table, first get the RD table.
2121 delete_global_type2_routes(bgp
, vpn
);
2123 /* Next, walk this VNI's route table and delete local type-2 routes. */
2124 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2125 rn
= bgp_route_next(rn
)) {
2126 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2128 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2131 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &ri
);
2133 /* Route entry in local table gets deleted immediately. */
2135 bgp_info_reap(rn
, ri
);
2142 * Delete all routes in per ES route-table
2144 static int delete_all_es_routes(struct bgp
*bgp
, struct evpnes
*es
)
2146 struct bgp_node
*rn
;
2147 struct bgp_info
*ri
, *nextri
;
2149 /* Walk this ES's route table and delete all routes. */
2150 for (rn
= bgp_table_top(es
->route_table
); rn
;
2151 rn
= bgp_route_next(rn
)) {
2152 for (ri
= rn
->info
; (ri
!= NULL
) && (nextri
= ri
->next
, 1);
2154 bgp_info_delete(rn
, ri
);
2155 bgp_info_reap(rn
, ri
);
2163 * Delete all routes in the per-VNI route table.
2165 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2167 struct bgp_node
*rn
;
2168 struct bgp_info
*ri
, *nextri
;
2170 /* Walk this VNI's route table and delete all routes. */
2171 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2172 rn
= bgp_route_next(rn
)) {
2173 for (ri
= rn
->info
; (ri
!= NULL
) && (nextri
= ri
->next
, 1);
2175 bgp_info_delete(rn
, ri
);
2176 bgp_info_reap(rn
, ri
);
2184 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2185 * export RT getting modified or change to tunnel IP. Note that these
2186 * situations need the route in the per-VNI table as well as the global
2187 * table to be updated (as attributes change).
2189 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2192 struct prefix_evpn p
;
2194 /* Update and advertise the type-3 route (only one) followed by the
2195 * locally learnt type-2 routes (MACIP) - for this VNI.
2197 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2198 ret
= update_evpn_route(bgp
, vpn
, &p
, 0);
2202 return update_all_type2_routes(bgp
, vpn
);
2205 /* Delete (and withdraw) local routes for specified ES from global and ES table.
2206 * Also remove all other routes from the per ES table.
2207 * Invoked when ES is deleted.
2209 static int delete_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
2212 char buf
[ESI_STR_LEN
];
2213 struct prefix_evpn p
;
2215 /* Delete and withdraw locally learnt ES route */
2216 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
.ipaddr_v4
);
2217 ret
= delete_evpn_type4_route(bgp
, es
, &p
);
2220 "%u failed to delete type-4 route for ESI %s",
2222 esi_to_str(&es
->esi
, buf
, sizeof(buf
)));
2225 /* Delete all routes from per ES table */
2226 return delete_all_es_routes(bgp
, es
);
2230 * Delete (and withdraw) local routes for specified VNI from the global
2231 * table and per-VNI table. After this, remove all other routes from
2232 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2233 * (advertise-all-vni) being disabled.
2235 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2238 struct prefix_evpn p
;
2240 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2241 * followed by type-3 routes (only one) - for this VNI.
2243 ret
= delete_all_type2_routes(bgp
, vpn
);
2247 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2248 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2252 /* Delete all routes from the per-VNI table. */
2253 return delete_all_vni_routes(bgp
, vpn
);
2257 * There is a tunnel endpoint IP address change for this VNI, delete
2258 * prior type-3 route (if needed) and update.
2259 * Note: Route re-advertisement happens elsewhere after other processing
2262 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2263 struct in_addr originator_ip
)
2265 struct prefix_evpn p
;
2267 /* If VNI is not live, we only need to update the originator ip */
2268 if (!is_vni_live(vpn
)) {
2269 vpn
->originator_ip
= originator_ip
;
2273 /* Update the tunnel-ip hash */
2274 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2275 bgp_tip_add(bgp
, &originator_ip
);
2277 /* filter routes as martian nexthop db has changed */
2278 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2280 /* Need to withdraw type-3 route as the originator IP is part
2283 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2284 delete_evpn_route(bgp
, vpn
, &p
);
2286 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2287 vpn
->originator_ip
= originator_ip
;
2291 /* Install EVPN route entry in ES */
2292 static int install_evpn_route_entry_in_es(struct bgp
*bgp
,
2294 struct prefix_evpn
*p
,
2295 struct bgp_info
*parent_ri
)
2298 struct bgp_node
*rn
= NULL
;
2299 struct bgp_info
*ri
= NULL
;
2300 struct attr
*attr_new
= NULL
;
2302 /* Create (or fetch) route within the VNI.
2303 * NOTE: There is no RD here.
2305 rn
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
2307 /* Check if route entry is already present. */
2308 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2310 (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2314 /* Add (or update) attribute to hash. */
2315 attr_new
= bgp_attr_intern(parent_ri
->attr
);
2317 /* Create new route with its attribute. */
2318 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
2319 parent_ri
->peer
, attr_new
, rn
);
2320 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
2321 bgp_info_extra_get(ri
);
2322 ri
->extra
->parent
= parent_ri
;
2323 bgp_info_add(rn
, ri
);
2325 if (attrhash_cmp(ri
->attr
, parent_ri
->attr
)
2326 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
2327 bgp_unlock_node(rn
);
2330 /* The attribute has changed. */
2331 /* Add (or update) attribute to hash. */
2332 attr_new
= bgp_attr_intern(parent_ri
->attr
);
2334 /* Restore route, if needed. */
2335 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
2336 bgp_info_restore(rn
, ri
);
2338 /* Mark if nexthop has changed. */
2339 if (!IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
))
2340 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
2342 /* Unintern existing, set to new. */
2343 bgp_attr_unintern(&ri
->attr
);
2344 ri
->attr
= attr_new
;
2345 ri
->uptime
= bgp_clock();
2348 /* Perform route selection and update zebra, if required. */
2349 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2354 * Install route entry into the VRF routing table and invoke route selection.
2356 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2357 struct prefix_evpn
*evp
,
2358 struct bgp_info
*parent_ri
)
2360 struct bgp_node
*rn
;
2361 struct bgp_info
*ri
;
2363 struct attr
*attr_new
;
2366 struct prefix
*pp
= &p
;
2369 char buf
[PREFIX_STRLEN
];
2370 char buf1
[PREFIX_STRLEN
];
2372 memset(pp
, 0, sizeof(struct prefix
));
2373 ip_prefix_from_evpn_prefix(evp
, pp
);
2375 if (bgp_debug_zebra(NULL
)) {
2377 "installing evpn prefix %s as ip prefix %s in vrf %s",
2378 prefix2str(evp
, buf
, sizeof(buf
)),
2379 prefix2str(pp
, buf1
, sizeof(buf
)),
2380 vrf_id_to_name(bgp_vrf
->vrf_id
));
2383 /* Create (or fetch) route within the VRF. */
2384 /* NOTE: There is no RD here. */
2385 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2387 safi
= SAFI_UNICAST
;
2388 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2389 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2391 safi
= SAFI_UNICAST
;
2392 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2396 /* EVPN routes currently only support a IPv4 next hop which corresponds
2397 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2398 * or prefix route, we have to convert the next hop to an IPv4-mapped
2399 * address for the rest of the code to flow through. In the case of IPv4,
2400 * make sure to set the flag for next hop attribute.
2402 bgp_attr_dup(&attr
, parent_ri
->attr
);
2404 evpn_convert_nexthop_to_ipv6(&attr
);
2406 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2408 /* Check if route entry is already present. */
2409 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2411 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2415 /* Add (or update) attribute to hash. */
2416 attr_new
= bgp_attr_intern(&attr
);
2418 /* Create new route with its attribute. */
2419 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
2420 parent_ri
->peer
, attr_new
, rn
);
2421 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
2422 bgp_info_extra_get(ri
);
2423 ri
->extra
->parent
= bgp_info_lock(parent_ri
);
2424 bgp_lock_node((struct bgp_node
*)parent_ri
->net
);
2425 if (parent_ri
->extra
) {
2426 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
2427 sizeof(ri
->extra
->label
));
2428 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
2430 bgp_info_add(rn
, ri
);
2432 if (attrhash_cmp(ri
->attr
, &attr
)
2433 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
2434 bgp_unlock_node(rn
);
2437 /* The attribute has changed. */
2438 /* Add (or update) attribute to hash. */
2439 attr_new
= bgp_attr_intern(&attr
);
2441 /* Restore route, if needed. */
2442 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
2443 bgp_info_restore(rn
, ri
);
2445 /* Mark if nexthop has changed. */
2446 if ((afi
== AFI_IP
&&
2447 !IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
)) ||
2449 !IPV6_ADDR_SAME(&ri
->attr
->mp_nexthop_global
,
2450 &attr_new
->mp_nexthop_global
)))
2451 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
2453 /* Unintern existing, set to new. */
2454 bgp_attr_unintern(&ri
->attr
);
2455 ri
->attr
= attr_new
;
2456 ri
->uptime
= bgp_clock();
2459 /* Perform route selection and update zebra, if required. */
2460 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2466 * Install route entry into the VNI routing table and invoke route selection.
2468 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2469 struct prefix_evpn
*p
,
2470 struct bgp_info
*parent_ri
)
2472 struct bgp_node
*rn
;
2473 struct bgp_info
*ri
;
2474 struct attr
*attr_new
;
2477 /* Create (or fetch) route within the VNI. */
2478 /* NOTE: There is no RD here. */
2479 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2481 /* Check if route entry is already present. */
2482 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2484 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2488 /* Add (or update) attribute to hash. */
2489 attr_new
= bgp_attr_intern(parent_ri
->attr
);
2491 /* Create new route with its attribute. */
2492 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
2493 parent_ri
->peer
, attr_new
, rn
);
2494 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
2495 bgp_info_extra_get(ri
);
2496 ri
->extra
->parent
= bgp_info_lock(parent_ri
);
2497 bgp_lock_node((struct bgp_node
*)parent_ri
->net
);
2498 if (parent_ri
->extra
) {
2499 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
2500 sizeof(ri
->extra
->label
));
2501 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
2503 bgp_info_add(rn
, ri
);
2505 if (attrhash_cmp(ri
->attr
, parent_ri
->attr
)
2506 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
2507 bgp_unlock_node(rn
);
2510 /* The attribute has changed. */
2511 /* Add (or update) attribute to hash. */
2512 attr_new
= bgp_attr_intern(parent_ri
->attr
);
2514 /* Restore route, if needed. */
2515 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
2516 bgp_info_restore(rn
, ri
);
2518 /* Mark if nexthop has changed. */
2519 if (!IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
))
2520 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
2522 /* Unintern existing, set to new. */
2523 bgp_attr_unintern(&ri
->attr
);
2524 ri
->attr
= attr_new
;
2525 ri
->uptime
= bgp_clock();
2528 /* Perform route selection and update zebra, if required. */
2529 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2534 /* Uninstall EVPN route entry from ES route table */
2535 static int uninstall_evpn_route_entry_in_es(struct bgp
*bgp
,
2537 struct prefix_evpn
*p
,
2538 struct bgp_info
*parent_ri
)
2541 struct bgp_node
*rn
;
2542 struct bgp_info
*ri
;
2544 if (!es
->route_table
)
2547 /* Locate route within the ESI.
2548 * NOTE: There is no RD here.
2550 rn
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
2554 /* Find matching route entry. */
2555 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2557 (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2563 /* Mark entry for deletion */
2564 bgp_info_delete(rn
, ri
);
2566 /* Perform route selection and update zebra, if required. */
2567 ret
= evpn_es_route_select_install(bgp
, es
, rn
);
2569 /* Unlock route node. */
2570 bgp_unlock_node(rn
);
2576 * Uninstall route entry from the VRF routing table and send message
2577 * to zebra, if appropriate.
2579 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2580 struct prefix_evpn
*evp
,
2581 struct bgp_info
*parent_ri
)
2583 struct bgp_node
*rn
;
2584 struct bgp_info
*ri
;
2587 struct prefix
*pp
= &p
;
2590 char buf
[PREFIX_STRLEN
];
2591 char buf1
[PREFIX_STRLEN
];
2593 memset(pp
, 0, sizeof(struct prefix
));
2594 ip_prefix_from_evpn_prefix(evp
, pp
);
2596 if (bgp_debug_zebra(NULL
)) {
2598 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2599 prefix2str(evp
, buf
, sizeof(buf
)),
2600 prefix2str(pp
, buf1
, sizeof(buf
)),
2601 vrf_id_to_name(bgp_vrf
->vrf_id
));
2604 /* Locate route within the VRF. */
2605 /* NOTE: There is no RD here. */
2606 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2608 safi
= SAFI_UNICAST
;
2609 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2612 safi
= SAFI_UNICAST
;
2613 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2619 /* Find matching route entry. */
2620 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2622 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2628 /* Mark entry for deletion */
2629 bgp_info_delete(rn
, ri
);
2631 /* Perform route selection and update zebra, if required. */
2632 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2634 /* Unlock route node. */
2635 bgp_unlock_node(rn
);
2641 * Uninstall route entry from the VNI routing table and send message
2642 * to zebra, if appropriate.
2644 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2645 struct prefix_evpn
*p
,
2646 struct bgp_info
*parent_ri
)
2648 struct bgp_node
*rn
;
2649 struct bgp_info
*ri
;
2652 /* Locate route within the VNI. */
2653 /* NOTE: There is no RD here. */
2654 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2658 /* Find matching route entry. */
2659 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2661 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2667 /* Mark entry for deletion */
2668 bgp_info_delete(rn
, ri
);
2670 /* Perform route selection and update zebra, if required. */
2671 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2673 /* Unlock route node. */
2674 bgp_unlock_node(rn
);
2680 * Given a prefix, see if it belongs to ES.
2682 static int is_prefix_matching_for_es(struct prefix_evpn
*p
,
2685 /* if not an ES route return false */
2686 if (p
->prefix
.route_type
!= BGP_EVPN_ES_ROUTE
)
2689 if (memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
)) == 0)
2696 * Given a route entry and a VRF, see if this route entry should be
2697 * imported into the VRF i.e., RTs match.
2699 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
, struct bgp_info
*ri
)
2701 struct attr
*attr
= ri
->attr
;
2702 struct ecommunity
*ecom
;
2706 /* Route should have valid RT to be even considered. */
2707 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2710 ecom
= attr
->ecommunity
;
2711 if (!ecom
|| !ecom
->size
)
2714 /* For each extended community RT, see if it matches this VNI. If any RT
2715 * matches, we're done.
2717 for (i
= 0; i
< ecom
->size
; i
++) {
2719 uint8_t type
, sub_type
;
2720 struct ecommunity_val
*eval
;
2721 struct ecommunity_val eval_tmp
;
2722 struct vrf_irt_node
*irt
;
2724 /* Only deal with RTs */
2725 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2726 eval
= (struct ecommunity_val
*)(ecom
->val
2727 + (i
* ECOMMUNITY_SIZE
));
2730 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2733 /* See if this RT matches specified VNIs import RTs */
2734 irt
= lookup_vrf_import_rt(eval
);
2736 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2739 /* Also check for non-exact match. In this, we mask out the AS
2741 * only check on the local-admin sub-field. This is to
2743 * VNI as the RT for EBGP peering too.
2746 if (type
== ECOMMUNITY_ENCODE_AS
2747 || type
== ECOMMUNITY_ENCODE_AS4
2748 || type
== ECOMMUNITY_ENCODE_IP
) {
2749 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2750 mask_ecom_global_admin(&eval_tmp
, eval
);
2751 irt
= lookup_vrf_import_rt(&eval_tmp
);
2754 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2762 * Given a route entry and a VNI, see if this route entry should be
2763 * imported into the VNI i.e., RTs match.
2765 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2766 struct bgp_info
*ri
)
2768 struct attr
*attr
= ri
->attr
;
2769 struct ecommunity
*ecom
;
2773 /* Route should have valid RT to be even considered. */
2774 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2777 ecom
= attr
->ecommunity
;
2778 if (!ecom
|| !ecom
->size
)
2781 /* For each extended community RT, see if it matches this VNI. If any RT
2782 * matches, we're done.
2784 for (i
= 0; i
< ecom
->size
; i
++) {
2786 uint8_t type
, sub_type
;
2787 struct ecommunity_val
*eval
;
2788 struct ecommunity_val eval_tmp
;
2789 struct irt_node
*irt
;
2791 /* Only deal with RTs */
2792 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2793 eval
= (struct ecommunity_val
*)(ecom
->val
2794 + (i
* ECOMMUNITY_SIZE
));
2797 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2800 /* See if this RT matches specified VNIs import RTs */
2801 irt
= lookup_import_rt(bgp
, eval
);
2803 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2806 /* Also check for non-exact match. In this, we mask out the AS
2808 * only check on the local-admin sub-field. This is to
2810 * VNI as the RT for EBGP peering too.
2813 if (type
== ECOMMUNITY_ENCODE_AS
2814 || type
== ECOMMUNITY_ENCODE_AS4
2815 || type
== ECOMMUNITY_ENCODE_IP
) {
2816 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2817 mask_ecom_global_admin(&eval_tmp
, eval
);
2818 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2821 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2828 static int install_uninstall_routes_for_es(struct bgp
*bgp
,
2835 char buf
[PREFIX_STRLEN
];
2836 char buf1
[ESI_STR_LEN
];
2837 struct bgp_node
*rd_rn
, *rn
;
2838 struct bgp_table
*table
;
2839 struct bgp_info
*ri
;
2845 * Walk entire global routing table and evaluate routes which could be
2846 * imported into this VRF. Note that we need to loop through all global
2847 * routes to determine which route matches the import rt on vrf
2849 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2850 rd_rn
= bgp_route_next(rd_rn
)) {
2851 table
= (struct bgp_table
*)(rd_rn
->info
);
2855 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2856 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2858 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2860 * Consider "valid" remote routes applicable for
2863 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2864 && ri
->type
== ZEBRA_ROUTE_BGP
2865 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2868 if (!is_prefix_matching_for_es(evp
, es
))
2872 ret
= install_evpn_route_entry_in_es(
2875 ret
= uninstall_evpn_route_entry_in_es(
2880 "Failed to %s EVPN %s route in ESI %s",
2883 prefix2str(evp
, buf
,
2885 esi_to_str(&es
->esi
, buf1
,
2896 * Install or uninstall mac-ip routes are appropriate for this
2899 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
2903 struct bgp_node
*rd_rn
, *rn
;
2904 struct bgp_table
*table
;
2905 struct bgp_info
*ri
;
2907 char buf
[PREFIX_STRLEN
];
2908 struct bgp
*bgp_def
= NULL
;
2912 bgp_def
= bgp_get_default();
2916 /* Walk entire global routing table and evaluate routes which could be
2917 * imported into this VRF. Note that we need to loop through all global
2918 * routes to determine which route matches the import rt on vrf
2920 for (rd_rn
= bgp_table_top(bgp_def
->rib
[afi
][safi
]); rd_rn
;
2921 rd_rn
= bgp_route_next(rd_rn
)) {
2922 table
= (struct bgp_table
*)(rd_rn
->info
);
2926 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2927 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2929 /* if not mac-ip route skip this route */
2930 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2931 || evp
->prefix
.route_type
2932 == BGP_EVPN_IP_PREFIX_ROUTE
))
2935 /* if not a mac+ip route skip this route */
2936 if (!(is_evpn_prefix_ipaddr_v4(evp
)
2937 || is_evpn_prefix_ipaddr_v6(evp
)))
2940 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2941 /* Consider "valid" remote routes applicable for
2944 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2945 && ri
->type
== ZEBRA_ROUTE_BGP
2946 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2949 if (is_route_matching_for_vrf(bgp_vrf
, ri
)) {
2951 ret
= install_evpn_route_entry_in_vrf(
2954 ret
= uninstall_evpn_route_entry_in_vrf(
2959 "Failed to %s EVPN %s route in VRF %s",
2962 prefix2str(evp
, buf
,
2977 * Install or uninstall routes of specified type that are appropriate for this
2980 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
2981 struct bgpevpn
*vpn
,
2982 bgp_evpn_route_type rtype
,
2987 struct bgp_node
*rd_rn
, *rn
;
2988 struct bgp_table
*table
;
2989 struct bgp_info
*ri
;
2995 /* Walk entire global routing table and evaluate routes which could be
2996 * imported into this VPN. Note that we cannot just look at the routes
2998 * the VNI's RD - remote routes applicable for this VNI could have any
3001 /* EVPN routes are a 2-level table. */
3002 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
3003 rd_rn
= bgp_route_next(rd_rn
)) {
3004 table
= (struct bgp_table
*)(rd_rn
->info
);
3008 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3009 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3011 if (evp
->prefix
.route_type
!= rtype
)
3014 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
3015 /* Consider "valid" remote routes applicable for
3017 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
3018 && ri
->type
== ZEBRA_ROUTE_BGP
3019 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
3022 if (is_route_matching_for_vni(bgp
, vpn
, ri
)) {
3024 ret
= install_evpn_route_entry(
3027 ret
= uninstall_evpn_route_entry(
3032 "%u: Failed to %s EVPN %s route in VNI %u",
3036 rtype
== BGP_EVPN_MAC_IP_ROUTE
3050 /* Install any existing remote ES routes applicable for this ES into its routing
3051 * table. This is invoked when ES comes up.
3053 static int install_routes_for_es(struct bgp
*bgp
, struct evpnes
*es
)
3055 return install_uninstall_routes_for_es(bgp
, es
, 1);
3059 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3060 * is invoked upon l3vni-add or l3vni import rt change
3062 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3064 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3069 * Install any existing remote routes applicable for this VNI into its
3070 * routing table. This is invoked when a VNI becomes "live" or its Import
3073 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3077 /* Install type-3 routes followed by type-2 routes - the ones applicable
3080 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3085 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3089 /* uninstall routes from l3vni vrf. */
3090 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3092 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3097 * Uninstall any existing remote routes for this VNI. One scenario in which
3098 * this is invoked is upon an import RT change.
3100 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3104 /* Uninstall type-2 routes followed by type-3 routes - the ones
3108 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3113 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3117 /* Install or unistall route in ES */
3118 static int install_uninstall_route_in_es(struct bgp
*bgp
, struct evpnes
*es
,
3119 afi_t afi
, safi_t safi
,
3120 struct prefix_evpn
*evp
,
3121 struct bgp_info
*ri
,
3125 char buf
[ESI_STR_LEN
];
3128 ret
= install_evpn_route_entry_in_es(bgp
, es
, evp
, ri
);
3130 ret
= uninstall_evpn_route_entry_in_es(bgp
, es
, evp
, ri
);
3133 zlog_err("%u: Failed to %s EVPN %s route in ESI %s",
3134 bgp
->vrf_id
, install
? "install" : "uninstall",
3136 esi_to_str(&evp
->prefix
.es_addr
.esi
, buf
,
3144 * Install or uninstall route in matching VRFs (list).
3146 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3147 safi_t safi
, struct prefix_evpn
*evp
,
3148 struct bgp_info
*ri
,
3149 struct list
*vrfs
, int install
)
3151 char buf
[PREFIX2STR_BUFFER
];
3152 struct bgp
*bgp_vrf
;
3153 struct listnode
*node
, *nnode
;
3155 /* Only type-2/type-5 routes go into a VRF */
3156 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3157 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3160 /* if it is type-2 route and not a mac+ip route skip this route */
3161 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3162 && !(is_evpn_prefix_ipaddr_v4(evp
)
3163 || is_evpn_prefix_ipaddr_v6(evp
)))
3166 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3170 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, ri
);
3172 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3176 zlog_err("%u: Failed to %s prefix %s in VRF %s",
3178 install
? "install" : "uninstall",
3179 prefix2str(evp
, buf
, sizeof(buf
)),
3180 vrf_id_to_name(bgp_vrf
->vrf_id
));
3189 * Install or uninstall route in matching VNIs (list).
3191 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3192 safi_t safi
, struct prefix_evpn
*evp
,
3193 struct bgp_info
*ri
,
3194 struct list
*vnis
, int install
)
3196 struct bgpevpn
*vpn
;
3197 struct listnode
*node
, *nnode
;
3199 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3202 if (!is_vni_live(vpn
))
3206 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, ri
);
3208 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, ri
);
3211 zlog_err("%u: Failed to %s EVPN %s route in VNI %u",
3212 bgp
->vrf_id
, install
? "install" : "uninstall",
3213 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3225 * Install or uninstall route for appropriate VNIs/ESIs.
3227 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3228 struct prefix
*p
, struct bgp_info
*ri
,
3231 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3232 struct attr
*attr
= ri
->attr
;
3233 struct ecommunity
*ecom
;
3238 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3239 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3240 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3241 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3242 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3245 /* If we don't have Route Target, nothing much to do. */
3246 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3249 ecom
= attr
->ecommunity
;
3250 if (!ecom
|| !ecom
->size
)
3253 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3254 * attached to the route */
3255 for (i
= 0; i
< ecom
->size
; i
++) {
3257 uint8_t type
, sub_type
;
3258 struct ecommunity_val
*eval
;
3259 struct ecommunity_val eval_tmp
;
3260 struct irt_node
*irt
; /* import rt for l2vni */
3261 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3264 /* Only deal with RTs */
3265 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
3266 eval
= (struct ecommunity_val
*)(ecom
->val
3267 + (i
* ECOMMUNITY_SIZE
));
3270 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3274 * macip routes (type-2) are imported into VNI and VRF tables.
3275 * IMET route is imported into VNI table.
3276 * prefix routes are imported into VRF table.
3278 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3279 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3280 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3282 irt
= lookup_import_rt(bgp
, eval
);
3284 install_uninstall_route_in_vnis(bgp
, afi
, safi
,
3289 vrf_irt
= lookup_vrf_import_rt(eval
);
3291 install_uninstall_route_in_vrfs(bgp
, afi
, safi
,
3296 /* Also check for non-exact match.
3297 * In this, we mask out the AS and
3298 * only check on the local-admin sub-field.
3299 * This is to facilitate using
3300 * VNI as the RT for EBGP peering too.
3304 if (type
== ECOMMUNITY_ENCODE_AS
3305 || type
== ECOMMUNITY_ENCODE_AS4
3306 || type
== ECOMMUNITY_ENCODE_IP
) {
3307 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3308 mask_ecom_global_admin(&eval_tmp
, eval
);
3309 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3310 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3314 install_uninstall_route_in_vnis(bgp
, afi
, safi
,
3319 install_uninstall_route_in_vrfs(bgp
, afi
, safi
,
3325 /* es route is imported into the es table */
3326 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3328 /* we will match based on the entire esi to avoid
3329 * imoort of an es route for esi2 into esi1
3331 es
= bgp_evpn_lookup_es(bgp
, &evp
->prefix
.es_addr
.esi
);
3332 if (es
&& is_es_local(es
))
3333 install_uninstall_route_in_es(bgp
, es
,
3343 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3346 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3348 /* delete all ipv4 routes and withdraw from peers */
3349 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3350 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3352 /* delete all ipv6 routes and withdraw from peers */
3353 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3354 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3358 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3361 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3363 /* update all ipv4 routes */
3364 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3365 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3367 /* update all ipv6 routes */
3368 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3369 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3373 * update and advertise local routes for a VRF as type-5 routes.
3374 * This is invoked upon RD change for a VRF. Note taht the processing is only
3375 * done in the global route table using the routes which already exist in the
3378 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3380 /* skip if the RD is configured */
3381 if (is_vrf_rd_configured(bgp_vrf
))
3384 /* derive the RD for the VRF based on new router-id */
3385 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3387 /* update advertise ipv4|ipv6 routes as type-5 routes */
3388 update_advertise_vrf_routes(bgp_vrf
);
3392 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3393 * This is invoked upon VRF RD change. The processing is done only from global
3396 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3398 /* skip if the RD is configured */
3399 if (is_vrf_rd_configured(bgp_vrf
))
3402 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3403 delete_withdraw_vrf_routes(bgp_vrf
);
3407 * Update and advertise local routes for a VNI. Invoked upon router-id
3408 * change. Note that the processing is done only on the global route table
3409 * using routes that already exist in the per-VNI table.
3411 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3413 struct prefix_evpn p
;
3414 struct bgp_node
*rn
, *global_rn
;
3415 struct bgp_info
*ri
, *global_ri
;
3417 afi_t afi
= AFI_L2VPN
;
3418 safi_t safi
= SAFI_EVPN
;
3420 /* Locate type-3 route for VNI in the per-VNI table and use its
3421 * attributes to create and advertise the type-3 route for this VNI
3422 * in the global table.
3424 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3425 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3426 if (!rn
) /* unexpected */
3428 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
3429 if (ri
->peer
== bgp
->peer_self
&& ri
->type
== ZEBRA_ROUTE_BGP
3430 && ri
->sub_type
== BGP_ROUTE_STATIC
)
3432 if (!ri
) /* unexpected */
3436 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3437 (struct prefix
*)&p
, &vpn
->prd
);
3438 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1, 0, &ri
,
3441 /* Schedule for processing and unlock node. */
3442 bgp_process(bgp
, global_rn
, afi
, safi
);
3443 bgp_unlock_node(global_rn
);
3445 /* Now, walk this VNI's route table and use the route and its attribute
3446 * to create and schedule route in global table.
3448 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
3449 rn
= bgp_route_next(rn
)) {
3450 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
3452 /* Identify MAC-IP local routes. */
3453 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3456 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
3457 if (ri
->peer
== bgp
->peer_self
3458 && ri
->type
== ZEBRA_ROUTE_BGP
3459 && ri
->sub_type
== BGP_ROUTE_STATIC
)
3464 /* Create route in global routing table using this route entry's
3468 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3469 (struct prefix
*)evp
, &vpn
->prd
);
3471 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
3474 /* Schedule for processing and unlock node. */
3475 bgp_process(bgp
, global_rn
, afi
, safi
);
3476 bgp_unlock_node(global_rn
);
3483 * Delete (and withdraw) local routes for a VNI - only from the global
3484 * table. Invoked upon router-id change.
3486 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3489 struct prefix_evpn p
;
3490 struct bgp_node
*global_rn
;
3491 struct bgp_info
*ri
;
3492 afi_t afi
= AFI_L2VPN
;
3493 safi_t safi
= SAFI_EVPN
;
3495 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3496 * for this VNI - from the global table.
3498 ret
= delete_global_type2_routes(bgp
, vpn
);
3502 /* Remove type-3 route for this VNI from global table. */
3503 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3504 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3505 (struct prefix
*)&p
, &vpn
->prd
);
3507 /* Delete route entry in the global EVPN table. */
3508 delete_evpn_route_entry(bgp
, afi
, safi
, global_rn
, &ri
);
3510 /* Schedule for processing - withdraws to peers happen from
3514 bgp_process(bgp
, global_rn
, afi
, safi
);
3515 bgp_unlock_node(global_rn
);
3522 * Handle router-id change. Update and advertise local routes corresponding
3523 * to this VNI from peers. Note that this is invoked after updating the
3524 * router-id. The routes in the per-VNI table are used to create routes in
3525 * the global table and schedule them.
3527 static void update_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3529 struct bgpevpn
*vpn
;
3531 vpn
= (struct bgpevpn
*)backet
->data
;
3534 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__
);
3538 /* Skip VNIs with configured RD. */
3539 if (is_rd_configured(vpn
))
3542 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3543 update_advertise_vni_routes(bgp
, vpn
);
3547 * Handle router-id change. Delete and withdraw local routes corresponding
3548 * to this VNI from peers. Note that this is invoked prior to updating
3549 * the router-id and is done only on the global route table, the routes
3550 * are needed in the per-VNI table to re-advertise with new router id.
3552 static void withdraw_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3554 struct bgpevpn
*vpn
;
3556 vpn
= (struct bgpevpn
*)backet
->data
;
3559 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__
);
3563 /* Skip VNIs with configured RD. */
3564 if (is_rd_configured(vpn
))
3567 delete_withdraw_vni_routes(bgp
, vpn
);
3571 * Process received EVPN type-2 route (advertise or withdraw).
3573 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3574 struct attr
*attr
, uint8_t *pfx
, int psize
,
3575 uint32_t addpath_id
)
3577 struct prefix_rd prd
;
3578 struct prefix_evpn p
;
3579 struct bgp_route_evpn evpn
;
3581 uint8_t macaddr_len
;
3582 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
3583 uint32_t num_labels
= 0;
3587 /* Type-2 route should be either 33, 37 or 49 bytes or an
3588 * additional 3 bytes if there is a second label (VNI):
3589 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3590 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3591 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3593 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3594 && psize
!= 40 && psize
!= 52) {
3595 zlog_err("%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3596 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3600 memset(&evpn
, 0, sizeof(evpn
));
3602 /* Make prefix_rd */
3603 prd
.family
= AF_UNSPEC
;
3605 memcpy(&prd
.val
, pfx
, 8);
3608 /* Make EVPN prefix. */
3609 memset(&p
, 0, sizeof(struct prefix_evpn
));
3611 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3612 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3614 /* Copy Ethernet Seg Identifier */
3615 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
3618 /* Copy Ethernet Tag */
3619 memcpy(ð_tag
, pfx
, 4);
3620 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3623 /* Get the MAC Addr len */
3624 macaddr_len
= *pfx
++;
3626 /* Get the MAC Addr */
3627 if (macaddr_len
== (ETH_ALEN
* 8)) {
3628 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
3632 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3633 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3639 ipaddr_len
= *pfx
++;
3640 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3641 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3643 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3644 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3649 ipaddr_len
/= 8; /* Convert to bytes. */
3650 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3653 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
3657 /* Get the VNI(s). Stored as bytes here. */
3659 memset(label
, 0, sizeof(label
));
3660 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
3661 pfx
+= BGP_LABEL_BYTES
;
3662 psize
-= (33 + ipaddr_len
);
3663 /* Do we have a second VNI? */
3666 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
3668 * If in future, we are required to access additional fields,
3669 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
3674 /* Process the route. */
3676 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3677 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3678 &prd
, &label
[0], num_labels
, 0, &evpn
);
3680 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3681 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3682 &prd
, &label
[0], num_labels
, &evpn
);
3687 * Process received EVPN type-3 route (advertise or withdraw).
3689 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3690 struct attr
*attr
, uint8_t *pfx
, int psize
,
3691 uint32_t addpath_id
)
3693 struct prefix_rd prd
;
3694 struct prefix_evpn p
;
3699 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3700 * IP len (1) and IP (4 or 16).
3702 if (psize
!= 17 && psize
!= 29) {
3703 zlog_err("%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3704 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3708 /* If PMSI is present, log if it is anything other than IR.
3709 * Note: We just simply ignore the values as it is not clear if
3710 * doing anything else is better.
3713 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3714 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
) {
3715 zlog_warn("%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3716 peer
->bgp
->vrf_id
, peer
->host
,
3717 attr
->pmsi_tnl_type
);
3721 /* Make prefix_rd */
3722 prd
.family
= AF_UNSPEC
;
3724 memcpy(&prd
.val
, pfx
, 8);
3727 /* Make EVPN prefix. */
3728 memset(&p
, 0, sizeof(struct prefix_evpn
));
3730 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3731 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3733 /* Copy Ethernet Tag */
3734 memcpy(ð_tag
, pfx
, 4);
3735 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
3739 ipaddr_len
= *pfx
++;
3740 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3741 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
3742 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
3745 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3746 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3750 /* Process the route. */
3752 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3753 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3754 &prd
, NULL
, 0, 0, NULL
);
3756 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3757 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3758 &prd
, NULL
, 0, NULL
);
3763 * Process received EVPN type-4 route (advertise or withdraw).
3765 static int process_type4_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3766 struct attr
*attr
, uint8_t *pfx
, int psize
,
3767 uint32_t addpath_id
)
3772 struct in_addr vtep_ip
;
3773 struct prefix_rd prd
;
3774 struct prefix_evpn p
;
3776 /* Type-4 route should be either 23 or 35 bytes
3777 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
3779 if (psize
!= 23 && psize
!= 35) {
3780 zlog_err("%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
3781 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3785 /* Make prefix_rd */
3786 prd
.family
= AF_UNSPEC
;
3788 memcpy(&prd
.val
, pfx
, 8);
3792 memcpy(&esi
, pfx
, ESI_BYTES
);
3797 ipaddr_len
= *pfx
++;
3798 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3799 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
3802 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
3803 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3807 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
3808 /* Process the route. */
3810 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3811 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3812 &prd
, NULL
, 0, 0, NULL
);
3814 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3815 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3816 &prd
, NULL
, 0, NULL
);
3823 * Process received EVPN type-5 route (advertise or withdraw).
3825 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3826 struct attr
*attr
, uint8_t *pfx
, int psize
,
3827 uint32_t addpath_id
, int withdraw
)
3829 struct prefix_rd prd
;
3830 struct prefix_evpn p
;
3831 struct bgp_route_evpn evpn
;
3834 mpls_label_t label
; /* holds the VNI as in the packet */
3837 /* Type-5 route should be 34 or 58 bytes:
3838 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3839 * GW (4 or 16) and VNI (3).
3840 * Note that the IP and GW should both be IPv4 or both IPv6.
3842 if (psize
!= 34 && psize
!= 58) {
3843 zlog_err("%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3844 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3848 /* Make prefix_rd */
3849 prd
.family
= AF_UNSPEC
;
3851 memcpy(&prd
.val
, pfx
, 8);
3854 /* Make EVPN prefix. */
3855 memset(&p
, 0, sizeof(struct prefix_evpn
));
3857 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3858 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
3860 /* Additional information outside of prefix - ESI and GW IP */
3861 memset(&evpn
, 0, sizeof(evpn
));
3864 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
3867 /* Fetch Ethernet Tag. */
3868 memcpy(ð_tag
, pfx
, 4);
3869 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
3872 /* Fetch IP prefix length. */
3874 if (ippfx_len
> IPV6_MAX_BITLEN
) {
3876 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3877 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
3880 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
3882 /* Determine IPv4 or IPv6 prefix */
3883 /* Since the address and GW are from the same family, this just becomes
3884 * a simple check on the total size.
3887 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
3888 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
3890 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
3893 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
3894 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
3896 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
3900 /* Get the VNI (in MPLS label field). Stored as bytes here. */
3901 memset(&label
, 0, sizeof(label
));
3902 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
3905 * If in future, we are required to access additional fields,
3906 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
3910 /* Process the route. */
3912 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3913 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3914 &prd
, &label
, 1, 0, &evpn
);
3916 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3917 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3918 &prd
, &label
, 1, &evpn
);
3923 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
3924 struct prefix_rd
*prd
, mpls_label_t
*label
,
3925 uint32_t num_labels
, struct attr
*attr
)
3929 struct evpn_addr
*p_evpn_p
;
3931 memset(&temp
, 0, 16);
3932 if (p
->family
!= AF_EVPN
)
3934 p_evpn_p
= &(p
->u
.prefix_evpn
);
3936 /* len denites the total len of IP and GW-IP in the route
3937 IP and GW-IP have to be both ipv4 or ipv6
3939 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3940 len
= 8; /* IP and GWIP are both ipv4 */
3942 len
= 32; /* IP and GWIP are both ipv6 */
3943 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
3944 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
3945 stream_put(s
, prd
->val
, 8);
3947 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
3949 stream_put(s
, &temp
, 10);
3950 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
3951 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
3952 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3953 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
3955 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
3957 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3959 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
3961 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
3963 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3964 stream_put_ipv4(s
, 0);
3966 stream_put(s
, &temp
, 16);
3970 stream_put(s
, label
, 3);
3976 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
3978 static void cleanup_vni_on_disable(struct hash_backet
*backet
, struct bgp
*bgp
)
3980 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3982 /* Remove EVPN routes and schedule for processing. */
3983 delete_routes_for_vni(bgp
, vpn
);
3985 /* Clear "live" flag and see if hash needs to be freed. */
3986 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
3987 if (!is_vni_configured(vpn
))
3988 bgp_evpn_free(bgp
, vpn
);
3992 * Free a VNI entry; iterator function called during cleanup.
3994 static void free_vni_entry(struct hash_backet
*backet
, struct bgp
*bgp
)
3996 struct bgpevpn
*vpn
;
3998 vpn
= (struct bgpevpn
*)backet
->data
;
3999 delete_all_vni_routes(bgp
, vpn
);
4000 bgp_evpn_free(bgp
, vpn
);
4004 * Derive AUTO import RT for BGP VRF - L3VNI
4006 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4008 struct bgp
*bgp_def
= NULL
;
4010 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4011 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4014 bgp_def
= bgp_get_default();
4017 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4021 * Delete AUTO import RT from BGP VRF - L3VNI
4023 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4025 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4029 * Derive AUTO export RT for BGP VRF - L3VNI
4031 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4033 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4034 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4038 * Delete AUTO export RT from BGP VRF - L3VNI
4040 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4042 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4045 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4047 struct bgp
*bgp_def
= NULL
;
4048 struct listnode
*node
= NULL
;
4049 struct bgpevpn
*vpn
= NULL
;
4051 bgp_def
= bgp_get_default();
4055 /* update all type-5 routes */
4056 update_advertise_vrf_routes(bgp_vrf
);
4058 /* update all type-2 routes */
4059 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4060 update_routes_for_vni(bgp_def
, vpn
);
4064 * Handle autort change for a given VNI.
4066 static void update_autort_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
4068 struct bgpevpn
*vpn
= backet
->data
;
4071 zlog_warn("%s: VNI hash entry for VNI not found", __PRETTY_FUNCTION__
);
4075 if (!is_import_rt_configured(vpn
)) {
4076 if (is_vni_live(vpn
))
4077 bgp_evpn_uninstall_routes(bgp
, vpn
);
4078 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4079 list_delete_all_node(vpn
->import_rtl
);
4080 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4081 if (is_vni_live(vpn
))
4082 bgp_evpn_install_routes(bgp
, vpn
);
4084 if (!is_export_rt_configured(vpn
)) {
4085 list_delete_all_node(vpn
->export_rtl
);
4086 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4087 if (is_vni_live(vpn
))
4088 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4096 /* withdraw type-5 route corresponding to ip prefix */
4097 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4098 afi_t afi
, safi_t safi
)
4101 struct prefix_evpn evp
;
4102 char buf
[PREFIX_STRLEN
];
4104 build_type5_prefix_from_ip_prefix(&evp
, p
);
4105 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4108 "%u failed to delete type-5 route for prefix %s in vrf %s",
4109 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4110 vrf_id_to_name(bgp_vrf
->vrf_id
));
4114 /* withdraw all type-5 routes for an address family */
4115 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4117 struct bgp_table
*table
= NULL
;
4118 struct bgp_node
*rn
= NULL
;
4119 struct bgp_info
*ri
;
4121 table
= bgp_vrf
->rib
[afi
][safi
];
4122 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4123 /* Only care about "selected" routes - non-imported. */
4124 /* TODO: Support for AddPath for EVPN. */
4125 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
4126 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
)
4127 && (!ri
->extra
|| !ri
->extra
->parent
)) {
4128 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
4137 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4138 * to this function correspond to those of the source IP prefix (best
4139 * path in the case of the attr. In the case of a local prefix (when we
4140 * are advertising local subnets), the src_attr will be NULL.
4142 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
4143 struct attr
*src_attr
, afi_t afi
,
4147 struct prefix_evpn evp
;
4148 char buf
[PREFIX_STRLEN
];
4150 build_type5_prefix_from_ip_prefix(&evp
, p
);
4151 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4153 zlog_err("%u: Failed to create type-5 route for prefix %s",
4154 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4157 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4158 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4159 * advertisement is enabled.
4161 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4164 struct bgp_table
*table
= NULL
;
4165 struct bgp_node
*rn
= NULL
;
4166 struct bgp_info
*ri
;
4168 table
= bgp_vrf
->rib
[afi
][safi
];
4169 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4170 /* Need to identify the "selected" route entry to use its
4171 * attribute. Also, we only consider "non-imported" routes.
4172 * TODO: Support for AddPath for EVPN.
4174 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
4175 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
)
4176 && (!ri
->extra
|| !ri
->extra
->parent
)) {
4178 /* apply the route-map */
4179 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4182 ret
= route_map_apply(
4183 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4185 &rn
->p
, RMAP_BGP
, ri
);
4186 if (ret
== RMAP_DENYMATCH
)
4189 bgp_evpn_advertise_type5_route(
4190 bgp_vrf
, &rn
->p
, ri
->attr
, afi
, safi
);
4197 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4199 struct listnode
*node
, *nnode
, *node_to_del
;
4200 struct ecommunity
*ecom
, *ecom_auto
;
4201 struct ecommunity_val eval
;
4203 if (bgp
->advertise_autort_rfc8365
)
4204 vni
|= EVPN_AUTORT_VXLAN
;
4205 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4207 ecom_auto
= ecommunity_new();
4208 ecommunity_add_val(ecom_auto
, &eval
);
4211 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4212 if (ecommunity_match(ecom
, ecom_auto
)) {
4213 ecommunity_free(&ecom
);
4219 list_delete_node(rtl
, node_to_del
);
4221 ecommunity_free(&ecom_auto
);
4224 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4225 struct ecommunity
*ecomadd
)
4227 /* uninstall routes from vrf */
4228 uninstall_routes_for_vrf(bgp_vrf
);
4230 /* Cleanup the RT to VRF mapping */
4231 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4233 /* Remove auto generated RT */
4234 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4236 /* Add the newly configured RT to RT list */
4237 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4238 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4240 /* map VRF to its RTs */
4241 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4243 /* install routes matching the new VRF */
4244 install_routes_for_vrf(bgp_vrf
);
4247 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4248 struct ecommunity
*ecomdel
)
4250 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4251 struct ecommunity
*ecom
= NULL
;
4253 /* uninstall routes from vrf */
4254 uninstall_routes_for_vrf(bgp_vrf
);
4256 /* Cleanup the RT to VRF mapping */
4257 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4259 /* remove the RT from the RT list */
4260 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4261 if (ecommunity_match(ecom
, ecomdel
)) {
4262 ecommunity_free(&ecom
);
4269 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4271 assert(bgp_vrf
->vrf_import_rtl
);
4272 /* fallback to auto import rt, if this was the last RT */
4273 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4274 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4275 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4278 /* map VRFs to its RTs */
4279 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4281 /* install routes matching this new RT */
4282 install_routes_for_vrf(bgp_vrf
);
4285 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4286 struct ecommunity
*ecomadd
)
4288 /* remove auto-generated RT */
4289 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4291 /* Add the new RT to the RT list */
4292 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4293 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4295 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4298 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4299 struct ecommunity
*ecomdel
)
4301 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4302 struct ecommunity
*ecom
= NULL
;
4304 /* Remove the RT from the RT list */
4305 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4306 if (ecommunity_match(ecom
, ecomdel
)) {
4307 ecommunity_free(&ecom
);
4314 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4317 * Temporary assert to make SA happy.
4318 * The ALL_LIST_ELEMENTS macro above has a NULL check
4319 * which means that SA is going to complain about
4320 * the list_isempty call, which doesn't NULL check.
4321 * So until we get this situation cleaned up, here
4324 assert(bgp_vrf
->vrf_export_rtl
);
4326 /* fall back to auto-generated RT if this was the last RT */
4327 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4328 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4329 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4332 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4336 * Handle change to BGP router id. This is invoked twice by the change
4337 * handler, first before the router id has been changed and then after
4338 * the router id has been changed. The first invocation will result in
4339 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4340 * will result in the routes being re-advertised.
4342 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4346 /* delete and withdraw all the type-5 routes
4347 stored in the global table for this vrf
4349 withdraw_router_id_vrf(bgp
);
4351 /* delete all the VNI routes (type-2/type-3) routes for all the
4354 hash_iterate(bgp
->vnihash
,
4355 (void (*)(struct hash_backet
*,
4356 void *))withdraw_router_id_vni
,
4360 /* advertise all routes in the vrf as type-5 routes with the new
4363 update_router_id_vrf(bgp
);
4365 /* advertise all the VNI routes (type-2/type-3) routes with the
4368 hash_iterate(bgp
->vnihash
,
4369 (void (*)(struct hash_backet
*,
4370 void *))update_router_id_vni
,
4376 * Handle change to auto-RT algorithm - update and advertise local routes.
4378 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4380 hash_iterate(bgp
->vnihash
,
4381 (void (*)(struct hash_backet
*,
4382 void*))update_autort_vni
,
4387 * Handle change to export RT - update and advertise local routes.
4389 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4391 return update_routes_for_vni(bgp
, vpn
);
4394 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4397 delete_withdraw_vrf_routes(bgp_vrf
);
4399 update_advertise_vrf_routes(bgp_vrf
);
4403 * Handle change to RD. This is invoked twice by the change handler,
4404 * first before the RD has been changed and then after the RD has
4405 * been changed. The first invocation will result in local routes
4406 * of this VNI being deleted and withdrawn and the next will result
4407 * in the routes being re-advertised.
4409 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4413 delete_withdraw_vni_routes(bgp
, vpn
);
4415 update_advertise_vni_routes(bgp
, vpn
);
4419 * Install routes for this VNI. Invoked upon change to Import RT.
4421 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4423 return install_routes_for_vni(bgp
, vpn
);
4427 * Uninstall all routes installed for this VNI. Invoked upon change
4430 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4432 return uninstall_routes_for_vni(bgp
, vpn
);
4436 * TODO: Hardcoded for a maximum of 2 VNIs right now
4438 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4443 vni1
= label2vni(label
);
4444 if (num_labels
== 2) {
4445 vni2
= label2vni(label
+ 1);
4446 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4448 snprintf(buf
, len
, "%u", vni1
);
4453 * Function to convert evpn route to json format.
4454 * NOTE: We don't use prefix2str as the output here is a bit different.
4456 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
4458 char buf1
[ETHER_ADDR_STRLEN
];
4459 char buf2
[PREFIX2STR_BUFFER
];
4464 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4465 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4466 json_object_int_add(json
, "ethTag",
4467 p
->prefix
.imet_addr
.eth_tag
);
4468 json_object_int_add(json
, "ipLen",
4469 is_evpn_prefix_ipaddr_v4(p
)
4472 json_object_string_add(json
, "ip",
4473 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4474 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4475 if (is_evpn_prefix_ipaddr_none(p
)) {
4476 json_object_int_add(json
, "routeType",
4477 p
->prefix
.route_type
);
4478 json_object_int_add(json
, "ethTag",
4479 p
->prefix
.macip_addr
.eth_tag
);
4480 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4481 json_object_string_add(json
, "mac",
4482 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4488 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4491 json_object_int_add(json
, "routeType",
4492 p
->prefix
.route_type
);
4493 json_object_int_add(json
, "ethTag",
4494 p
->prefix
.macip_addr
.eth_tag
);
4495 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4496 json_object_string_add(json
, "mac",
4497 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
4500 json_object_int_add(json
, "ipLen",
4501 is_evpn_prefix_ipaddr_v4(p
)
4504 json_object_string_add(
4507 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4509 PREFIX2STR_BUFFER
));
4512 /* Currently, this is to cater to other AF_ETHERNET code. */
4517 * Function to convert evpn route to string.
4518 * NOTE: We don't use prefix2str as the output here is a bit different.
4520 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
4522 char buf1
[ETHER_ADDR_STRLEN
];
4523 char buf2
[PREFIX2STR_BUFFER
];
4524 char buf3
[ESI_STR_LEN
];
4526 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
4527 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
4528 p
->prefix
.imet_addr
.eth_tag
,
4529 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4531 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
4532 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
4533 if (is_evpn_prefix_ipaddr_none(p
))
4534 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4535 p
->prefix
.route_type
,
4536 p
->prefix
.macip_addr
.eth_tag
,
4538 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4543 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
4545 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
4546 p
->prefix
.route_type
,
4547 p
->prefix
.macip_addr
.eth_tag
,
4549 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4551 family
== AF_INET
? IPV4_MAX_BITLEN
4554 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4556 PREFIX2STR_BUFFER
));
4558 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
4559 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
4560 p
->prefix
.route_type
,
4561 p
->prefix
.prefix_addr
.eth_tag
,
4562 p
->prefix
.prefix_addr
.ip_prefix_length
,
4563 is_evpn_prefix_ipaddr_v4(p
)
4564 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
4565 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
4566 } else if (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
4567 snprintf(buf
, len
, "[%d]:[%s]:[%d]:[%s]",
4568 p
->prefix
.route_type
,
4569 esi_to_str(&p
->prefix
.es_addr
.esi
, buf3
, sizeof(buf3
)),
4570 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
4572 inet_ntoa(p
->prefix
.es_addr
.ip
.ipaddr_v4
));
4574 /* For EVPN route types not supported yet. */
4575 snprintf(buf
, len
, "(unsupported route type %d)",
4576 p
->prefix
.route_type
);
4583 * Encode EVPN prefix in Update (MP_REACH)
4585 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
4586 struct prefix_rd
*prd
, mpls_label_t
*label
,
4587 uint32_t num_labels
, struct attr
*attr
,
4588 int addpath_encode
, uint32_t addpath_tx_id
)
4590 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4591 int len
, ipa_len
= 0;
4594 stream_putl(s
, addpath_tx_id
);
4597 stream_putc(s
, evp
->prefix
.route_type
);
4599 switch (evp
->prefix
.route_type
) {
4600 case BGP_EVPN_MAC_IP_ROUTE
:
4601 if (is_evpn_prefix_ipaddr_v4(evp
))
4602 ipa_len
= IPV4_MAX_BYTELEN
;
4603 else if (is_evpn_prefix_ipaddr_v6(evp
))
4604 ipa_len
= IPV6_MAX_BYTELEN
;
4605 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4606 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4607 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4609 stream_putc(s
, len
);
4610 stream_put(s
, prd
->val
, 8); /* RD */
4612 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
4614 stream_put(s
, 0, 10);
4615 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4616 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4617 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4618 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4619 if (ipa_len
) /* IP */
4620 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4622 /* 1st label is the L2 VNI */
4623 stream_put(s
, label
, BGP_LABEL_BYTES
);
4624 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4625 if (ipa_len
&& num_labels
> 1)
4626 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4629 case BGP_EVPN_IMET_ROUTE
:
4630 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4631 stream_put(s
, prd
->val
, 8); /* RD */
4632 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4633 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4634 /* Originating Router's IP Addr */
4635 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4638 case BGP_EVPN_ES_ROUTE
:
4639 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4640 stream_put(s
, prd
->val
, 8); /* RD */
4641 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4642 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4644 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4647 case BGP_EVPN_IP_PREFIX_ROUTE
:
4648 /* TODO: AddPath support. */
4649 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4657 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4658 struct bgp_nlri
*packet
, int withdraw
)
4664 uint32_t addpath_id
;
4665 int addpath_encoded
;
4670 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4672 lim
= pnt
+ packet
->length
;
4674 safi
= packet
->safi
;
4678 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
4679 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
4680 PEER_CAP_ADDPATH_AF_TX_RCV
));
4682 for (; pnt
< lim
; pnt
+= psize
) {
4683 /* Clear prefix structure. */
4684 memset(&p
, 0, sizeof(struct prefix
));
4686 /* Deal with path-id if AddPath is supported. */
4687 if (addpath_encoded
) {
4688 /* When packet overflow occurs return immediately. */
4689 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4692 addpath_id
= ntohl(*((uint32_t *)pnt
));
4693 pnt
+= BGP_ADDPATH_ID_LEN
;
4696 /* All EVPN NLRI types start with type and length. */
4703 /* When packet overflow occur return immediately. */
4704 if (pnt
+ psize
> lim
)
4708 case BGP_EVPN_MAC_IP_ROUTE
:
4709 if (process_type2_route(peer
, afi
, safi
,
4710 withdraw
? NULL
: attr
, pnt
,
4711 psize
, addpath_id
)) {
4713 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4714 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4719 case BGP_EVPN_IMET_ROUTE
:
4720 if (process_type3_route(peer
, afi
, safi
,
4721 withdraw
? NULL
: attr
, pnt
,
4722 psize
, addpath_id
)) {
4724 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4725 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4730 case BGP_EVPN_ES_ROUTE
:
4731 if (process_type4_route(peer
, afi
, safi
,
4732 withdraw
? NULL
: attr
, pnt
,
4733 psize
, addpath_id
)) {
4735 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4736 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4741 case BGP_EVPN_IP_PREFIX_ROUTE
:
4742 if (process_type5_route(peer
, afi
, safi
, attr
, pnt
,
4743 psize
, addpath_id
, withdraw
)) {
4745 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4746 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4756 /* Packet length consistency check. */
4764 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
4765 * The mapping will be used during route processing.
4766 * bgp_def: default bgp instance
4767 * bgp_vrf: specific bgp vrf instance on which RT is configured
4769 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
4772 struct ecommunity_val
*eval
= NULL
;
4773 struct listnode
*node
= NULL
, *nnode
= NULL
;
4774 struct ecommunity
*ecom
= NULL
;
4776 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4777 for (i
= 0; i
< ecom
->size
; i
++) {
4778 eval
= (struct ecommunity_val
*)(ecom
->val
4780 * ECOMMUNITY_SIZE
));
4781 map_vrf_to_rt(bgp_vrf
, eval
);
4787 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
4789 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
4792 struct ecommunity_val
*eval
;
4793 struct listnode
*node
, *nnode
;
4794 struct ecommunity
*ecom
;
4796 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4797 for (i
= 0; i
< ecom
->size
; i
++) {
4798 struct vrf_irt_node
*irt
;
4799 struct ecommunity_val eval_tmp
;
4801 eval
= (struct ecommunity_val
*)(ecom
->val
4803 * ECOMMUNITY_SIZE
));
4804 /* If using "automatic" RT, we only care about the
4805 * local-admin sub-field.
4806 * This is to facilitate using VNI as the RT for EBGP
4809 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4810 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
4811 BGP_VRF_IMPORT_RT_CFGD
))
4812 mask_ecom_global_admin(&eval_tmp
, eval
);
4814 irt
= lookup_vrf_import_rt(&eval_tmp
);
4816 unmap_vrf_from_rt(bgp_vrf
, irt
);
4823 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
4824 * The mapping will be used during route processing.
4826 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4829 struct ecommunity_val
*eval
;
4830 struct listnode
*node
, *nnode
;
4831 struct ecommunity
*ecom
;
4833 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4834 for (i
= 0; i
< ecom
->size
; i
++) {
4835 eval
= (struct ecommunity_val
*)(ecom
->val
4837 * ECOMMUNITY_SIZE
));
4838 map_vni_to_rt(bgp
, vpn
, eval
);
4844 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
4846 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4849 struct ecommunity_val
*eval
;
4850 struct listnode
*node
, *nnode
;
4851 struct ecommunity
*ecom
;
4853 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4854 for (i
= 0; i
< ecom
->size
; i
++) {
4855 struct irt_node
*irt
;
4856 struct ecommunity_val eval_tmp
;
4858 eval
= (struct ecommunity_val
*)(ecom
->val
4860 * ECOMMUNITY_SIZE
));
4861 /* If using "automatic" RT, we only care about the
4862 * local-admin sub-field.
4863 * This is to facilitate using VNI as the RT for EBGP
4866 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4867 if (!is_import_rt_configured(vpn
))
4868 mask_ecom_global_admin(&eval_tmp
, eval
);
4870 irt
= lookup_import_rt(bgp
, &eval_tmp
);
4872 unmap_vni_from_rt(bgp
, vpn
, irt
);
4878 * Derive Import RT automatically for VNI and map VNI to RT.
4879 * The mapping will be used during route processing.
4881 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4883 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
4884 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
4887 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
4891 * Derive Export RT automatically for VNI.
4893 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4895 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
4896 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
4900 * Derive RD automatically for VNI using passed information - it
4901 * is of the form RouterId:unique-id-for-vni.
4903 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
4905 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
4909 * Derive RD automatically for VNI using passed information - it
4910 * is of the form RouterId:unique-id-for-vni.
4912 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4916 vpn
->prd
.family
= AF_UNSPEC
;
4917 vpn
->prd
.prefixlen
= 64;
4918 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
4919 (void)str2prefix_rd(buf
, &vpn
->prd
);
4920 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
4926 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
4928 struct bgpevpn
*vpn
;
4931 memset(&tmp
, 0, sizeof(struct bgpevpn
));
4933 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
4938 * Create a new vpn - invoked upon configuration or zebra notification.
4940 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
4941 struct in_addr originator_ip
,
4942 vrf_id_t tenant_vrf_id
)
4944 struct bgpevpn
*vpn
;
4949 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
4953 /* Set values - RD and RT set to defaults. */
4955 vpn
->originator_ip
= originator_ip
;
4956 vpn
->tenant_vrf_id
= tenant_vrf_id
;
4958 /* Initialize route-target import and export lists */
4959 vpn
->import_rtl
= list_new();
4960 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4961 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
4962 vpn
->export_rtl
= list_new();
4963 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4964 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
4965 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
4966 derive_rd_rt_for_vni(bgp
, vpn
);
4968 /* Initialize EVPN route table. */
4969 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
4972 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
4973 XFREE(MTYPE_BGP_EVPN
, vpn
);
4977 /* add to l2vni list on corresponding vrf */
4978 bgpevpn_link_to_l3vni(vpn
);
4980 QOBJ_REG(vpn
, bgpevpn
);
4985 * Free a given VPN - called in multiple scenarios such as zebra
4986 * notification, configuration being deleted, advertise-all-vni disabled etc.
4987 * This just frees appropriate memory, caller should have taken other
4990 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4992 bgpevpn_unlink_from_l3vni(vpn
);
4993 bgp_table_unlock(vpn
->route_table
);
4994 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4995 list_delete_and_null(&vpn
->import_rtl
);
4996 list_delete_and_null(&vpn
->export_rtl
);
4997 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
4998 hash_release(bgp
->vnihash
, vpn
);
5000 XFREE(MTYPE_BGP_EVPN
, vpn
);
5006 struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
)
5011 memset(&tmp
, 0, sizeof(struct evpnes
));
5012 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
5013 es
= hash_lookup(bgp
->esihash
, &tmp
);
5018 * Create a new local es - invoked upon zebra notification.
5020 struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
,
5022 struct ipaddr
*originator_ip
)
5030 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct evpnes
));
5034 /* set the ESI and originator_ip */
5035 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
5036 memcpy(&es
->originator_ip
, originator_ip
, sizeof(struct ipaddr
));
5038 /* Initialise the VTEP list */
5039 es
->vtep_list
= list_new();
5040 es
->vtep_list
->cmp
= (int (*)(void *, void *))evpn_vtep_ip_cmp
;
5042 /* auto derive RD for this es */
5043 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
5044 es
->prd
.family
= AF_UNSPEC
;
5045 es
->prd
.prefixlen
= 64;
5046 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), es
->rd_id
);
5047 (void)str2prefix_rd(buf
, &es
->prd
);
5049 /* Initialize the ES route table */
5050 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5053 if (!hash_get(bgp
->esihash
, es
, hash_alloc_intern
)) {
5054 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5058 QOBJ_REG(es
, evpnes
);
5064 * This just frees appropriate memory, caller should have taken other
5067 void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
)
5069 list_delete_and_null(&es
->vtep_list
);
5070 bgp_table_unlock(es
->route_table
);
5071 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
5072 hash_release(bgp
->esihash
, es
);
5074 XFREE(MTYPE_BGP_EVPN_ES
, es
);
5078 * Import evpn route from global table to VNI/VRF/ESI.
5080 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5081 struct prefix
*p
, struct bgp_info
*ri
)
5083 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 1);
5087 * Unimport evpn route from VNI/VRF/ESI.
5089 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5090 struct prefix
*p
, struct bgp_info
*ri
)
5092 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 0);
5095 /* filter routes which have martian next hops */
5096 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5100 struct bgp_node
*rd_rn
, *rn
;
5101 struct bgp_table
*table
;
5102 struct bgp_info
*ri
;
5107 /* Walk entire global routing table and evaluate routes which could be
5108 * imported into this VPN. Note that we cannot just look at the routes
5109 * for the VNI's RD -
5110 * remote routes applicable for this VNI could have any RD.
5112 /* EVPN routes are a 2-level table. */
5113 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
5114 rd_rn
= bgp_route_next(rd_rn
)) {
5115 table
= (struct bgp_table
*)(rd_rn
->info
);
5119 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5121 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
5123 /* Consider "valid" remote routes applicable for
5125 if (!(ri
->type
== ZEBRA_ROUTE_BGP
5126 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
5129 if (bgp_nexthop_self(bgp
, ri
->attr
->nexthop
)) {
5131 char attr_str
[BUFSIZ
];
5132 char pbuf
[PREFIX_STRLEN
];
5134 bgp_dump_attr(ri
->attr
, attr_str
,
5137 if (bgp_debug_update(ri
->peer
, &rn
->p
,
5140 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
5147 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5150 bgp_rib_remove(rn
, ri
, ri
->peer
, afi
,
5161 * Handle del of a local MACIP.
5163 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5166 struct bgpevpn
*vpn
;
5167 struct prefix_evpn p
;
5169 /* Lookup VNI hash - should exist. */
5170 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5171 if (!vpn
|| !is_vni_live(vpn
)) {
5172 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP DEL",
5173 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5177 /* Remove EVPN type-2 route and schedule for processing. */
5178 build_evpn_type2_prefix(&p
, mac
, ip
);
5179 delete_evpn_route(bgp
, vpn
, &p
);
5185 * Handle add of a local MACIP.
5187 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5188 struct ipaddr
*ip
, uint8_t flags
)
5190 struct bgpevpn
*vpn
;
5191 struct prefix_evpn p
;
5193 /* Lookup VNI hash - should exist. */
5194 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5195 if (!vpn
|| !is_vni_live(vpn
)) {
5196 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP ADD",
5197 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5201 /* Create EVPN type-2 route and schedule for processing. */
5202 build_evpn_type2_prefix(&p
, mac
, ip
);
5203 if (update_evpn_route(bgp
, vpn
, &p
, flags
)) {
5204 char buf
[ETHER_ADDR_STRLEN
];
5205 char buf2
[INET6_ADDRSTRLEN
];
5208 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5209 bgp
->vrf_id
, vpn
->vni
,
5210 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5213 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5214 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5221 static void link_l2vni_hash_to_l3vni(struct hash_backet
*backet
,
5222 struct bgp
*bgp_vrf
)
5224 struct bgpevpn
*vpn
= NULL
;
5225 struct bgp
*bgp_def
= NULL
;
5227 bgp_def
= bgp_get_default();
5230 vpn
= (struct bgpevpn
*)backet
->data
;
5231 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5232 bgpevpn_link_to_l3vni(vpn
);
5235 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
, struct ethaddr
*rmac
,
5236 struct in_addr originator_ip
, int filter
)
5238 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5239 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5240 struct listnode
*node
= NULL
;
5241 struct bgpevpn
*vpn
= NULL
;
5244 /* get the default instance - required to get the AS number for VRF
5247 bgp_def
= bgp_get_default();
5250 "Cannot process L3VNI %u ADD - default BGP instance not yet created",
5256 /* if the BGP vrf instance doesnt exist - create one */
5257 bgp_vrf
= bgp_lookup_by_name(vrf_id_to_name(vrf_id
));
5262 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5263 BGP_INSTANCE_TYPE_VRF
);
5265 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
:
5266 zlog_err("'bgp multiple-instance' not present\n");
5268 case BGP_ERR_AS_MISMATCH
:
5269 zlog_err("BGP is already running; AS is %u\n", as
);
5271 case BGP_ERR_INSTANCE_MISMATCH
:
5272 zlog_err("BGP instance name and AS number mismatch\n");
5276 /* mark as auto created */
5277 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5280 /* associate with l3vni */
5281 bgp_vrf
->l3vni
= l3vni
;
5283 /* set the router mac - to be used in mac-ip routes for this vrf */
5284 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
5286 /* set the originator ip */
5287 bgp_vrf
->originator_ip
= originator_ip
;
5289 /* set the right filter - are we using l3vni only for prefix routes? */
5291 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5293 /* auto derive RD/RT */
5294 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5295 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5296 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5297 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5298 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5300 /* link all corresponding l2vnis */
5301 hash_iterate(bgp_def
->vnihash
,
5302 (void (*)(struct hash_backet
*,
5303 void *))link_l2vni_hash_to_l3vni
,
5306 /* Only update all corresponding type-2 routes if we are advertising two
5307 * labels along with type-2 routes
5310 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5311 update_routes_for_vni(bgp_def
, vpn
);
5313 /* advertise type-5 routes if needed */
5314 update_advertise_vrf_routes(bgp_vrf
);
5316 /* install all remote routes belonging to this l3vni into correspondng
5318 install_routes_for_vrf(bgp_vrf
);
5323 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5325 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5326 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
5327 struct listnode
*node
= NULL
;
5328 struct listnode
*next
= NULL
;
5329 struct bgpevpn
*vpn
= NULL
;
5331 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5334 "Cannot process L3VNI %u Del - Could not find BGP instance",
5339 bgp_def
= bgp_get_default();
5342 "Cannot process L3VNI %u Del - Could not find default BGP instance",
5347 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5348 * bgp_delete would not remove/decrement bgp_info of the ip_prefix
5349 * routes. This will uninstalling the routes from zebra and decremnt the
5352 uninstall_routes_for_vrf(bgp_vrf
);
5354 /* delete/withdraw all type-5 routes */
5355 delete_withdraw_vrf_routes(bgp_vrf
);
5357 /* remove the l3vni from vrf instance */
5360 /* remove the Rmac from the BGP vrf */
5361 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5364 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5365 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5366 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5368 if (!list_isempty(bgp_vrf
->vrf_export_rtl
)) {
5369 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5372 /* update all corresponding local mac-ip routes */
5373 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5374 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5375 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5376 update_routes_for_vni(bgp_def
, vpn
);
5380 /* If any L2VNIs point to this instance, unlink them. */
5381 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5382 bgpevpn_unlink_from_l3vni(vpn
);
5384 /* Delete the instance if it was autocreated */
5385 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5386 bgp_delete(bgp_vrf
);
5392 * Handle del of a local VNI.
5394 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5396 struct bgpevpn
*vpn
;
5398 /* Locate VNI hash */
5399 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5401 if (bgp_debug_zebra(NULL
))
5403 "%u: VNI hash entry for VNI %u not found at DEL",
5408 /* Remove all local EVPN routes and schedule for processing (to
5409 * withdraw from peers).
5411 delete_routes_for_vni(bgp
, vpn
);
5414 * tunnel is no longer active, del tunnel ip address from tip_hash
5416 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5418 /* Clear "live" flag and see if hash needs to be freed. */
5419 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5420 if (!is_vni_configured(vpn
))
5421 bgp_evpn_free(bgp
, vpn
);
5427 * Handle add (or update) of a local VNI. The VNI changes we care
5428 * about are for the local-tunnel-ip and the (tenant) VRF.
5430 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5431 struct in_addr originator_ip
, vrf_id_t tenant_vrf_id
)
5433 struct bgpevpn
*vpn
;
5434 struct prefix_evpn p
;
5436 /* Lookup VNI. If present and no change, exit. */
5437 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5440 if (is_vni_live(vpn
)
5441 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5442 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5443 /* Probably some other param has changed that we don't
5447 /* Update tenant_vrf_id if it has changed. */
5448 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5449 bgpevpn_unlink_from_l3vni(vpn
);
5450 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5451 bgpevpn_link_to_l3vni(vpn
);
5454 /* If tunnel endpoint IP has changed, update (and delete prior
5455 * type-3 route, if needed.)
5457 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5458 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5460 /* Update all routes with new endpoint IP and/or export RT
5463 if (is_vni_live(vpn
))
5464 update_routes_for_vni(bgp
, vpn
);
5467 /* Create or update as appropriate. */
5469 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
);
5472 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5478 /* if the VNI is live already, there is nothing more to do */
5479 if (is_vni_live(vpn
))
5482 /* Mark as "live" */
5483 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5485 /* tunnel is now active, add tunnel-ip to db */
5486 bgp_tip_add(bgp
, &originator_ip
);
5488 /* filter routes as nexthop database has changed */
5489 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5491 /* Create EVPN type-3 route and schedule for processing. */
5492 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5493 if (update_evpn_route(bgp
, vpn
, &p
, 0)) {
5494 zlog_err("%u: Type3 route creation failure for VNI %u",
5499 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5503 install_routes_for_vni(bgp
, vpn
);
5505 /* If we are advertising gateway mac-ip
5506 It needs to be conveyed again to zebra */
5507 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5513 * bgp_evpn_local_es_del
5515 int bgp_evpn_local_es_del(struct bgp
*bgp
,
5517 struct ipaddr
*originator_ip
)
5519 char buf
[ESI_STR_LEN
];
5520 struct evpnes
*es
= NULL
;
5522 if (!bgp
->esihash
) {
5523 zlog_err("%u: ESI hash not yet created", bgp
->vrf_id
);
5527 /* Lookup ESI hash - should exist. */
5528 es
= bgp_evpn_lookup_es(bgp
, esi
);
5530 zlog_warn("%u: ESI hash entry for ESI %s at Local ES DEL",
5532 esi_to_str(esi
, buf
, sizeof(buf
)));
5536 /* Delete all local EVPN ES routes from ESI table
5537 * and schedule for processing (to withdraw from peers))
5539 delete_routes_for_es(bgp
, es
);
5541 /* free the hash entry */
5542 bgp_evpn_es_free(bgp
, es
);
5548 * bgp_evpn_local_es_add
5550 int bgp_evpn_local_es_add(struct bgp
*bgp
,
5552 struct ipaddr
*originator_ip
)
5554 char buf
[ESI_STR_LEN
];
5555 struct evpnes
*es
= NULL
;
5556 struct prefix_evpn p
;
5558 if (!bgp
->esihash
) {
5559 zlog_err("%u: ESI hash not yet created", bgp
->vrf_id
);
5563 /* create the new es */
5564 es
= bgp_evpn_lookup_es(bgp
, esi
);
5566 es
= bgp_evpn_es_new(bgp
, esi
, originator_ip
);
5569 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
5570 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5574 UNSET_FLAG(es
->flags
, EVPNES_REMOTE
);
5575 SET_FLAG(es
->flags
, EVPNES_LOCAL
);
5577 build_evpn_type4_prefix(&p
, esi
, originator_ip
->ipaddr_v4
);
5578 if (update_evpn_type4_route(bgp
, es
, &p
)) {
5579 zlog_err("%u: Type4 route creation failure for ESI %s",
5580 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
5584 /* import all remote ES routes in th ES table */
5585 install_routes_for_es(bgp
, es
);
5591 * Cleanup EVPN information on disable - Need to delete and withdraw
5592 * EVPN routes from peers.
5594 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5596 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
5597 void *))cleanup_vni_on_disable
,
5602 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5603 * BGP instance (default) is being freed.
5605 void bgp_evpn_cleanup(struct bgp
*bgp
)
5607 hash_iterate(bgp
->vnihash
,
5608 (void (*)(struct hash_backet
*, void *))free_vni_entry
,
5611 hash_free(bgp
->import_rt_hash
);
5612 bgp
->import_rt_hash
= NULL
;
5614 hash_free(bgp
->vrf_import_rt_hash
);
5615 bgp
->vrf_import_rt_hash
= NULL
;
5617 hash_free(bgp
->vnihash
);
5618 bgp
->vnihash
= NULL
;
5620 hash_free(bgp
->esihash
);
5621 bgp
->esihash
= NULL
;
5623 list_delete_and_null(&bgp
->vrf_import_rtl
);
5624 list_delete_and_null(&bgp
->vrf_export_rtl
);
5625 list_delete_and_null(&bgp
->l2vnis
);
5629 * Initialization for EVPN
5632 * hash for RT to VNI
5634 void bgp_evpn_init(struct bgp
*bgp
)
5637 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
5639 hash_create(esi_hash_keymake
, esi_cmp
,
5640 "BGP EVPN Local ESI Hash");
5641 bgp
->import_rt_hash
=
5642 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
5643 "BGP Import RT Hash");
5644 bgp
->vrf_import_rt_hash
=
5645 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
5646 "BGP VRF Import RT Hash");
5647 bgp
->vrf_import_rtl
= list_new();
5648 bgp
->vrf_import_rtl
->cmp
=
5649 (int (*)(void *, void *))evpn_route_target_cmp
;
5650 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
5651 bgp
->vrf_export_rtl
= list_new();
5652 bgp
->vrf_export_rtl
->cmp
=
5653 (int (*)(void *, void *))evpn_route_target_cmp
;
5654 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
5655 bgp
->l2vnis
= list_new();
5656 bgp
->l2vnis
->cmp
= (int (*)(void *, void *))vni_hash_cmp
;
5659 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
5661 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);