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
)
59 * Static function declarations
61 static void delete_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
62 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
63 struct bgp_info
**ri
);
64 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
73 static unsigned int vni_hash_key_make(void *p
)
75 struct bgpevpn
*vpn
= p
;
76 return (jhash_1word(vpn
->vni
, 0));
80 * Comparison function for vni hash
82 static int vni_hash_cmp(const void *p1
, const void *p2
)
84 const struct bgpevpn
*vpn1
= p1
;
85 const struct bgpevpn
*vpn2
= p2
;
91 return (vpn1
->vni
== vpn2
->vni
);
95 * Make vrf import route target hash key.
97 static unsigned int vrf_import_rt_hash_key_make(void *p
)
99 struct vrf_irt_node
*irt
= p
;
100 char *pnt
= irt
->rt
.val
;
102 return jhash(pnt
, 8, 0x5abc1234);
106 * Comparison function for vrf import rt hash
108 static int vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
110 const struct vrf_irt_node
*irt1
= p1
;
111 const struct vrf_irt_node
*irt2
= p2
;
113 if (irt1
== NULL
&& irt2
== NULL
)
116 if (irt1
== NULL
|| irt2
== NULL
)
119 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
123 * Create a new vrf import_rt in default instance
125 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
127 struct bgp
*bgp_def
= NULL
;
128 struct vrf_irt_node
*irt
;
130 bgp_def
= bgp_get_default();
132 zlog_err("vrf import rt new - def instance not created yet");
136 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
137 sizeof(struct vrf_irt_node
));
142 irt
->vrfs
= list_new();
145 if (!hash_get(bgp_def
->vrf_import_rt_hash
, irt
, hash_alloc_intern
)) {
146 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
154 * Free the vrf import rt node
156 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
158 struct bgp
*bgp_def
= NULL
;
160 bgp_def
= bgp_get_default();
162 zlog_err("vrf import rt free - def instance not created yet");
166 hash_release(bgp_def
->vrf_import_rt_hash
, irt
);
167 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
171 * Function to lookup Import RT node - used to map a RT to set of
172 * VNIs importing routes with that RT.
174 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
176 struct bgp
*bgp_def
= NULL
;
177 struct vrf_irt_node
*irt
;
178 struct vrf_irt_node tmp
;
180 bgp_def
= bgp_get_default();
182 zlog_err("vrf import rt lookup - def instance not created yet");
186 memset(&tmp
, 0, sizeof(struct vrf_irt_node
));
187 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
188 irt
= hash_lookup(bgp_def
->vrf_import_rt_hash
, &tmp
);
193 * Is specified VRF present on the RT's list of "importing" VRFs?
195 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
, struct bgp
*bgp_vrf
)
197 struct listnode
*node
= NULL
, *nnode
= NULL
;
198 struct bgp
*tmp_bgp_vrf
= NULL
;
200 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
201 if (tmp_bgp_vrf
== bgp_vrf
)
208 * Make import route target hash key.
210 static unsigned int import_rt_hash_key_make(void *p
)
212 struct irt_node
*irt
= p
;
213 char *pnt
= irt
->rt
.val
;
215 return jhash(pnt
, 8, 0xdeadbeef);
219 * Comparison function for import rt hash
221 static int import_rt_hash_cmp(const void *p1
, const void *p2
)
223 const struct irt_node
*irt1
= p1
;
224 const struct irt_node
*irt2
= p2
;
226 if (irt1
== NULL
&& irt2
== NULL
)
229 if (irt1
== NULL
|| irt2
== NULL
)
232 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
236 * Create a new import_rt
238 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
239 struct ecommunity_val
*rt
)
241 struct irt_node
*irt
;
246 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
251 irt
->vnis
= list_new();
254 if (!hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
)) {
255 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
263 * Free the import rt node
265 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
267 hash_release(bgp
->import_rt_hash
, irt
);
268 list_delete_and_null(&irt
->vnis
);
269 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
273 * Function to lookup Import RT node - used to map a RT to set of
274 * VNIs importing routes with that RT.
276 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
277 struct ecommunity_val
*rt
)
279 struct irt_node
*irt
;
282 memset(&tmp
, 0, sizeof(struct irt_node
));
283 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
284 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
289 * Is specified VNI present on the RT's list of "importing" VNIs?
291 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
293 struct listnode
*node
, *nnode
;
294 struct bgpevpn
*tmp_vpn
;
296 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
305 * Compare Route Targets.
307 static int evpn_route_target_cmp(struct ecommunity
*ecom1
,
308 struct ecommunity
*ecom2
)
316 if (!ecom1
&& !ecom2
)
319 if (ecom1
->str
&& !ecom2
->str
)
322 if (!ecom1
->str
&& ecom2
->str
)
325 if (!ecom1
->str
&& !ecom2
->str
)
328 return strcmp(ecom1
->str
, ecom2
->str
);
331 static void evpn_xxport_delete_ecomm(void *val
)
333 struct ecommunity
*ecomm
= val
;
334 ecommunity_free(&ecomm
);
338 * Mask off global-admin field of specified extended community (RT),
339 * just retain the local-admin field.
341 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
342 struct ecommunity_val
*src
)
348 if (type
== ECOMMUNITY_ENCODE_AS
) {
349 dst
->val
[2] = dst
->val
[3] = 0;
350 } else if (type
== ECOMMUNITY_ENCODE_AS4
351 || type
== ECOMMUNITY_ENCODE_IP
) {
352 dst
->val
[2] = dst
->val
[3] = 0;
353 dst
->val
[4] = dst
->val
[5] = 0;
358 * Map one RT to specified VRF.
359 * bgp_vrf = BGP vrf instance
361 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct ecommunity_val
*eval
)
363 struct vrf_irt_node
*irt
= NULL
;
364 struct ecommunity_val eval_tmp
;
366 /* If using "automatic" RT,
367 * we only care about the local-admin sub-field.
368 * This is to facilitate using L3VNI(VRF-VNI)
369 * as the RT for EBGP peering too.
371 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
372 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
373 mask_ecom_global_admin(&eval_tmp
, eval
);
375 irt
= lookup_vrf_import_rt(&eval_tmp
);
376 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
377 /* Already mapped. */
381 irt
= vrf_import_rt_new(&eval_tmp
);
383 /* Add VRF to the list for this RT. */
384 listnode_add(irt
->vrfs
, bgp_vrf
);
388 * Unmap specified VRF from specified RT. If there are no other
389 * VRFs for this RT, then the RT hash is deleted.
390 * bgp_vrf: BGP VRF specific instance
392 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
, struct vrf_irt_node
*irt
)
394 /* Delete VRF from list for this RT. */
395 listnode_delete(irt
->vrfs
, bgp_vrf
);
396 if (!listnode_head(irt
->vrfs
)) {
397 list_delete_and_null(&irt
->vrfs
);
398 vrf_import_rt_free(irt
);
403 * Map one RT to specified VNI.
405 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
406 struct ecommunity_val
*eval
)
408 struct irt_node
*irt
;
409 struct ecommunity_val eval_tmp
;
411 /* If using "automatic" RT, we only care about the local-admin
413 * This is to facilitate using VNI as the RT for EBGP peering too.
415 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
416 if (!is_import_rt_configured(vpn
))
417 mask_ecom_global_admin(&eval_tmp
, eval
);
419 irt
= lookup_import_rt(bgp
, &eval_tmp
);
421 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
422 /* Already mapped. */
426 irt
= import_rt_new(bgp
, &eval_tmp
);
430 /* Add VNI to the hash list for this RT. */
431 listnode_add(irt
->vnis
, vpn
);
435 * Unmap specified VNI from specified RT. If there are no other
436 * VNIs for this RT, then the RT hash is deleted.
438 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
439 struct irt_node
*irt
)
441 /* Delete VNI from hash list for this RT. */
442 listnode_delete(irt
->vnis
, vpn
);
443 if (!listnode_head(irt
->vnis
)) {
444 import_rt_free(bgp
, irt
);
449 * Create RT extended community automatically from passed information:
450 * of the form AS:VNI.
451 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
452 * the need is to get a RT value that will be unique across different
453 * VNIs but the same across routers (in the same AS) for a particular
456 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
458 struct ecommunity_val eval
;
459 struct ecommunity
*ecomadd
;
461 if (bgp
->advertise_autort_rfc8365
)
462 vni
|= EVPN_AUTORT_VXLAN
;
463 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
465 ecomadd
= ecommunity_new();
466 ecommunity_add_val(ecomadd
, &eval
);
467 listnode_add_sort(rtl
, ecomadd
);
471 * Derive RD and RT for a VNI automatically. Invoked at the time of
474 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
476 bgp_evpn_derive_auto_rd(bgp
, vpn
);
477 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
478 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
482 * Convert nexthop (remote VTEP IP) into an IPv6 address.
484 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
486 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
488 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
489 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
493 * Add (update) or delete MACIP from zebra.
495 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
496 struct prefix_evpn
*p
,
497 struct in_addr remote_vtep_ip
, int add
,
502 char buf1
[ETHER_ADDR_STRLEN
];
503 char buf2
[INET6_ADDRSTRLEN
];
504 char buf3
[INET6_ADDRSTRLEN
];
507 if (!zclient
|| zclient
->sock
< 0)
510 /* Don't try to register if Zebra doesn't know of this instance. */
511 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
517 zclient_create_header(
518 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
520 stream_putl(s
, vpn
->vni
);
521 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
522 /* IP address length and IP address, if any. */
523 if (is_evpn_prefix_ipaddr_none(p
))
526 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
528 stream_putl(s
, ipa_len
);
529 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
531 stream_put_in_addr(s
, &remote_vtep_ip
);
533 /* TX flags - MAC sticky status and/or gateway mac */
535 stream_putc(s
, flags
);
537 stream_putw_at(s
, 0, stream_get_endp(s
));
539 if (bgp_debug_zebra(NULL
))
541 "Tx %s MACIP, VNI %u MAC %s IP %s (flags: 0x%x) remote VTEP %s",
542 add
? "ADD" : "DEL", vpn
->vni
,
543 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
545 ipaddr2str(&p
->prefix
.macip_addr
.ip
,
546 buf3
, sizeof(buf3
)), flags
,
547 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
550 return zclient_send_message(zclient
);
554 * Add (update) or delete remote VTEP from zebra.
556 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
557 struct prefix_evpn
*p
, int add
)
562 if (!zclient
|| zclient
->sock
< 0)
565 /* Don't try to register if Zebra doesn't know of this instance. */
566 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
572 zclient_create_header(
573 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
575 stream_putl(s
, vpn
->vni
);
576 if (is_evpn_prefix_ipaddr_v4(p
))
577 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
578 else if (is_evpn_prefix_ipaddr_v6(p
)) {
580 "Bad remote IP when trying to %s remote VTEP for VNI %u",
581 add
? "ADD" : "DEL", vpn
->vni
);
585 stream_putw_at(s
, 0, stream_get_endp(s
));
587 if (bgp_debug_zebra(NULL
))
588 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
589 add
? "ADD" : "DEL", vpn
->vni
,
590 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
592 return zclient_send_message(zclient
);
596 * Build extended communities for EVPN prefix route.
598 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
601 struct ecommunity ecom_encap
;
602 struct ecommunity ecom_rmac
;
603 struct ecommunity_val eval
;
604 struct ecommunity_val eval_rmac
;
605 bgp_encap_types tnl_type
;
606 struct listnode
*node
, *nnode
;
607 struct ecommunity
*ecom
;
608 struct list
*vrf_export_rtl
= NULL
;
611 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
612 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
613 encode_encap_extcomm(tnl_type
, &eval
);
615 ecom_encap
.val
= (uint8_t *)eval
.val
;
618 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
620 /* Add the export RTs for L3VNI/VRF */
621 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
622 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
623 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
625 ecommunity_merge(attr
->ecommunity
, ecom
);
628 /* add the router mac extended community */
629 if (!is_zero_mac(&attr
->rmac
)) {
630 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
631 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
633 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
635 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
638 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
642 * Build extended communities for EVPN route.
643 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
644 * and ENCAP extended communities are applicable for all routes.
645 * The default gateway extended community and MAC mobility (sticky) extended
646 * community are added as needed based on passed settings - only for type-2
647 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
648 * added, if present, based on passed settings - only for non-link-local
651 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
654 struct ecommunity ecom_encap
;
655 struct ecommunity ecom_sticky
;
656 struct ecommunity ecom_default_gw
;
657 struct ecommunity ecom_rmac
;
658 struct ecommunity_val eval
;
659 struct ecommunity_val eval_sticky
;
660 struct ecommunity_val eval_default_gw
;
661 struct ecommunity_val eval_rmac
;
662 bgp_encap_types tnl_type
;
663 struct listnode
*node
, *nnode
;
664 struct ecommunity
*ecom
;
666 struct list
*vrf_export_rtl
= NULL
;
669 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
670 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
671 encode_encap_extcomm(tnl_type
, &eval
);
673 ecom_encap
.val
= (uint8_t *)eval
.val
;
676 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
678 /* Add the export RTs for L2VNI */
679 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
680 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
682 /* Add the export RTs for L3VNI if told to - caller determines
683 * when this should be done.
686 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
687 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
688 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
690 attr
->ecommunity
= ecommunity_merge(
691 attr
->ecommunity
, ecom
);
695 /* Add MAC mobility (sticky) if needed. */
698 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
699 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
700 ecom_sticky
.size
= 1;
701 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
703 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
706 /* Add RMAC, if told to. */
708 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
709 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
711 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
713 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
716 /* Add default gateway, if needed. */
717 if (attr
->default_gw
) {
718 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
719 encode_default_gw_extcomm(&eval_default_gw
);
720 ecom_default_gw
.size
= 1;
721 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
723 ecommunity_merge(attr
->ecommunity
, &ecom_default_gw
);
726 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
730 * Add MAC mobility extended community to attribute.
732 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
734 struct ecommunity ecom_tmp
;
735 struct ecommunity_val eval
;
736 uint8_t *ecom_val_ptr
;
743 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
745 /* Find current MM ecommunity */
748 if (attr
->ecommunity
) {
749 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
750 pnt
= attr
->ecommunity
->val
+ (i
* 8);
754 if (type
== ECOMMUNITY_ENCODE_EVPN
756 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
757 ecom_val_ptr
= (uint8_t *)(attr
->ecommunity
->val
764 /* Update the existing MM ecommunity */
766 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
768 /* Add MM to existing */
770 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
772 ecom_tmp
.val
= (uint8_t *)eval
.val
;
774 if (attr
->ecommunity
)
776 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
778 attr
->ecommunity
= ecommunity_dup(&ecom_tmp
);
782 /* Install EVPN route into zebra. */
783 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
784 struct prefix_evpn
*p
,
785 struct in_addr remote_vtep_ip
, uint8_t flags
)
789 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
790 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
793 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 1);
798 /* Uninstall EVPN route from zebra. */
799 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
800 struct prefix_evpn
*p
,
801 struct in_addr remote_vtep_ip
)
805 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
806 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
809 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 0);
815 * Due to MAC mobility, the prior "local" best route has been supplanted
816 * by a "remote" best route. The prior route has to be deleted and withdrawn
819 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
821 struct bgp_info
*old_local
)
823 struct bgp_node
*global_rn
;
825 afi_t afi
= AFI_L2VPN
;
826 safi_t safi
= SAFI_EVPN
;
828 /* Locate route node in the global EVPN routing table. Note that
829 * this table is a 2-level tree (RD-level + Prefix-level) similar to
832 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
833 (struct prefix
*)&rn
->p
, &vpn
->prd
);
835 /* Delete route entry in the global EVPN table. */
836 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
838 /* Schedule for processing - withdraws to peers happen from
842 bgp_process(bgp
, global_rn
, afi
, safi
);
843 bgp_unlock_node(global_rn
);
846 /* Delete route entry in the VNI route table, caller to remove. */
847 bgp_info_delete(rn
, old_local
);
851 * Calculate the best path for an EVPN route. Install/update best path in zebra,
854 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
857 struct bgp_info
*old_select
, *new_select
;
858 struct bgp_info_pair old_and_new
;
859 afi_t afi
= AFI_L2VPN
;
860 safi_t safi
= SAFI_EVPN
;
864 /* Compute the best path. */
865 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
867 old_select
= old_and_new
.old
;
868 new_select
= old_and_new
.new;
870 /* If the best path hasn't changed - see if there is still something to
874 if (old_select
&& old_select
== new_select
875 && old_select
->type
== ZEBRA_ROUTE_BGP
876 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
877 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
878 && !CHECK_FLAG(old_select
->flags
, BGP_INFO_ATTR_CHANGED
)
879 && !bgp
->addpath_tx_used
[afi
][safi
]) {
880 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
881 if (old_select
->attr
->sticky
)
882 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
883 if (old_select
->attr
->default_gw
)
884 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
885 ret
= evpn_zebra_install(
886 bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
887 old_select
->attr
->nexthop
, flags
);
889 UNSET_FLAG(old_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
890 bgp_zebra_clear_route_change_flags(rn
);
894 /* If the user did a "clear" this flag will be set */
895 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
897 /* bestpath has changed; update relevant fields and install or uninstall
898 * into the zebra RIB.
900 if (old_select
|| new_select
)
901 bgp_bump_version(rn
);
904 bgp_info_unset_flag(rn
, old_select
, BGP_INFO_SELECTED
);
906 bgp_info_set_flag(rn
, new_select
, BGP_INFO_SELECTED
);
907 bgp_info_unset_flag(rn
, new_select
, BGP_INFO_ATTR_CHANGED
);
908 UNSET_FLAG(new_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
911 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
912 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
914 if (new_select
->attr
->sticky
)
915 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
916 if (new_select
->attr
->default_gw
)
917 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
918 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
919 new_select
->attr
->nexthop
, flags
);
920 /* If an old best existed and it was a "local" route, the only
922 * it would be supplanted is due to MAC mobility procedures. So,
924 * need to do an implicit delete and withdraw that route from
927 if (old_select
&& old_select
->peer
== bgp
->peer_self
928 && old_select
->type
== ZEBRA_ROUTE_BGP
929 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
930 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
932 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
933 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
934 ret
= evpn_zebra_uninstall(bgp
, vpn
,
935 (struct prefix_evpn
*)&rn
->p
,
936 old_select
->attr
->nexthop
);
939 /* Clear any route change flags. */
940 bgp_zebra_clear_route_change_flags(rn
);
942 /* Reap old select bgp_info, if it has been removed */
943 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_INFO_REMOVED
))
944 bgp_info_reap(rn
, old_select
);
950 * Return true if the local ri for this rn is of type gateway mac
952 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
954 struct bgp_info
*tmp_ri
= NULL
;
955 struct bgp_info
*local_ri
= NULL
;
958 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
959 if (tmp_ri
->peer
== bgp
->peer_self
960 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
961 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
968 return local_ri
->attr
->default_gw
;
973 * Return true if the local ri for this rn has sticky set
975 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
977 struct bgp_info
*tmp_ri
;
978 struct bgp_info
*local_ri
;
981 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
982 if (tmp_ri
->peer
== bgp
->peer_self
983 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
984 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
991 return local_ri
->attr
->sticky
;
994 static int update_evpn_type5_route_entry(struct bgp
*bgp_def
,
995 struct bgp
*bgp_vrf
, afi_t afi
,
996 safi_t safi
, struct bgp_node
*rn
,
997 struct attr
*attr
, int *route_changed
)
999 struct attr
*attr_new
= NULL
;
1000 struct bgp_info
*ri
= NULL
;
1001 mpls_label_t label
= MPLS_INVALID_LABEL
;
1002 struct bgp_info
*local_ri
= NULL
;
1003 struct bgp_info
*tmp_ri
= NULL
;
1006 /* locate the local route entry if any */
1007 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1008 if (tmp_ri
->peer
== bgp_def
->peer_self
1009 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1010 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1014 /* create a new route entry if one doesnt exist.
1015 Otherwise see if route attr has changed
1019 /* route has changed as this is the first entry */
1022 /* Add (or update) attribute to hash. */
1023 attr_new
= bgp_attr_intern(attr
);
1025 /* create the route info from attribute */
1026 ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1027 bgp_def
->peer_self
, attr_new
, rn
);
1028 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1030 /* Type-5 routes advertise the L3-VNI */
1031 bgp_info_extra_get(ri
);
1032 vni2label(bgp_vrf
->l3vni
, &label
);
1033 memcpy(&ri
->extra
->label
, &label
, sizeof(label
));
1034 ri
->extra
->num_labels
= 1;
1036 /* add the route entry to route node*/
1037 bgp_info_add(rn
, ri
);
1041 if (!attrhash_cmp(tmp_ri
->attr
, attr
)) {
1043 /* attribute changed */
1046 /* The attribute has changed. */
1047 /* Add (or update) attribute to hash. */
1048 attr_new
= bgp_attr_intern(attr
);
1049 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1051 /* Restore route, if needed. */
1052 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1053 bgp_info_restore(rn
, tmp_ri
);
1055 /* Unintern existing, set to new. */
1056 bgp_attr_unintern(&tmp_ri
->attr
);
1057 tmp_ri
->attr
= attr_new
;
1058 tmp_ri
->uptime
= bgp_clock();
1064 /* update evpn type-5 route entry */
1065 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1066 struct attr
*src_attr
)
1068 afi_t afi
= AFI_L2VPN
;
1069 safi_t safi
= SAFI_EVPN
;
1071 struct bgp_node
*rn
= NULL
;
1072 struct bgp
*bgp_def
= NULL
;
1073 int route_changed
= 0;
1075 bgp_def
= bgp_get_default();
1079 /* Build path attribute for this route - use the source attr, if
1080 * present, else treat as locally originated.
1083 bgp_attr_dup(&attr
, src_attr
);
1085 memset(&attr
, 0, sizeof(struct attr
));
1086 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1088 /* Set nexthop to ourselves and fill in the Router MAC. */
1089 attr
.nexthop
= bgp_vrf
->originator_ip
;
1090 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1091 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1092 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
1094 /* Setup RT and encap extended community */
1095 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1097 /* get the route node in global table */
1098 rn
= bgp_afi_node_get(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1099 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1102 /* create or update the route entry within the route node */
1103 update_evpn_type5_route_entry(bgp_def
, bgp_vrf
, afi
, safi
, rn
, &attr
,
1106 /* schedule for processing and unlock node */
1107 if (route_changed
) {
1108 bgp_process(bgp_def
, rn
, afi
, safi
);
1109 bgp_unlock_node(rn
);
1112 /* uninten temporary */
1114 aspath_unintern(&attr
.aspath
);
1119 * Create or update EVPN route entry. This could be in the VNI route table
1120 * or the global route table.
1122 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1123 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1124 struct attr
*attr
, int add
, int vni_table
,
1125 struct bgp_info
**ri
, uint8_t flags
)
1127 struct bgp_info
*tmp_ri
;
1128 struct bgp_info
*local_ri
, *remote_ri
;
1129 struct attr
*attr_new
;
1130 mpls_label_t label
[BGP_MAX_LABELS
];
1131 uint32_t num_labels
= 1;
1132 int route_change
= 1;
1134 struct prefix_evpn
*evp
;
1137 evp
= (struct prefix_evpn
*)&rn
->p
;
1138 memset(&label
, 0, sizeof(label
));
1140 /* See if this is an update of an existing route, or a new add. Also,
1141 * identify if already known from remote, and if so, the one with the
1142 * highest sequence number; this is only when adding to the VNI routing
1145 local_ri
= remote_ri
= NULL
;
1146 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1147 if (tmp_ri
->peer
== bgp
->peer_self
1148 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1149 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1152 if (tmp_ri
->type
== ZEBRA_ROUTE_BGP
1153 && tmp_ri
->sub_type
== BGP_ROUTE_IMPORTED
1154 && CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
)) {
1157 else if (mac_mobility_seqnum(tmp_ri
->attr
)
1158 > mac_mobility_seqnum(remote_ri
->attr
))
1164 /* If route doesn't exist already, create a new one, if told to.
1165 * Otherwise act based on whether the attributes of the route have
1168 if (!local_ri
&& !add
)
1172 /* When learnt locally for the first time but already known from
1173 * remote, we have to initiate appropriate MAC mobility steps.
1175 * is applicable when updating the VNI routing table.
1176 * We need to skip mobility steps for g/w macs (local mac on g/w
1177 * SVI) advertised in EVPN.
1178 * This will ensure that local routes are preferred for g/w macs
1180 if (remote_ri
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
)) {
1181 uint32_t cur_seqnum
;
1183 /* Add MM extended community to route. */
1184 cur_seqnum
= mac_mobility_seqnum(remote_ri
->attr
);
1185 add_mac_mobility_to_attr(cur_seqnum
+ 1, attr
);
1188 /* Add (or update) attribute to hash. */
1189 attr_new
= bgp_attr_intern(attr
);
1191 /* Extract MAC mobility sequence number, if any. */
1192 attr_new
->mm_seqnum
=
1193 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1194 attr_new
->sticky
= sticky
;
1196 /* Create new route with its attribute. */
1197 tmp_ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1198 bgp
->peer_self
, attr_new
, rn
);
1199 SET_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
);
1200 bgp_info_extra_get(tmp_ri
);
1202 /* The VNI goes into the 'label' field of the route */
1203 vni2label(vpn
->vni
, &label
[0]);
1205 /* Type-2 routes may carry a second VNI - the L3-VNI.
1206 * Only attach second label if we are advertising two labels for
1209 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1210 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1213 l3vni
= bgpevpn_get_l3vni(vpn
);
1215 vni2label(l3vni
, &label
[1]);
1220 memcpy(&tmp_ri
->extra
->label
, label
, sizeof(label
));
1221 tmp_ri
->extra
->num_labels
= num_labels
;
1222 bgp_info_add(rn
, tmp_ri
);
1225 if (attrhash_cmp(tmp_ri
->attr
, attr
)
1226 && !CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1230 * The attributes have changed, type-2 routes needs to
1231 * be advertised with right labels.
1233 vni2label(vpn
->vni
, &label
[0]);
1234 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1235 && CHECK_FLAG(vpn
->flags
,
1236 VNI_FLAG_USE_TWO_LABELS
)) {
1239 l3vni
= bgpevpn_get_l3vni(vpn
);
1241 vni2label(l3vni
, &label
[1]);
1245 memcpy(&tmp_ri
->extra
->label
, label
, sizeof(label
));
1246 tmp_ri
->extra
->num_labels
= num_labels
;
1248 /* The attribute has changed. */
1249 /* Add (or update) attribute to hash. */
1250 attr_new
= bgp_attr_intern(attr
);
1251 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1253 /* Restore route, if needed. */
1254 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1255 bgp_info_restore(rn
, tmp_ri
);
1257 /* Unintern existing, set to new. */
1258 bgp_attr_unintern(&tmp_ri
->attr
);
1259 tmp_ri
->attr
= attr_new
;
1260 tmp_ri
->uptime
= bgp_clock();
1264 /* Return back the route entry. */
1266 return route_change
;
1270 * Create or update EVPN route (of type based on prefix) for specified VNI
1271 * and schedule for processing.
1273 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1274 struct prefix_evpn
*p
, uint8_t flags
)
1276 struct bgp_node
*rn
;
1278 struct attr
*attr_new
;
1279 int add_l3_ecomm
= 0;
1280 struct bgp_info
*ri
;
1281 afi_t afi
= AFI_L2VPN
;
1282 safi_t safi
= SAFI_EVPN
;
1285 memset(&attr
, 0, sizeof(struct attr
));
1287 /* Build path-attribute for this route. */
1288 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1289 attr
.nexthop
= vpn
->originator_ip
;
1290 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1291 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1292 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1293 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1295 /* PMSI is only needed for type-3 routes */
1296 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
)
1297 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1299 /* router mac is only needed for type-2 routes here. */
1300 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1301 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1302 vni2label(vpn
->vni
, &(attr
.label
));
1304 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1305 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1308 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1309 (is_evpn_prefix_ipaddr_v4(p
) ||
1310 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1311 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
))
1314 /* Set up extended community. */
1315 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1317 /* First, create (or fetch) route node within the VNI. */
1318 /* NOTE: There is no RD here. */
1319 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1321 /* Create or update route entry. */
1322 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1325 attr_new
= ri
->attr
;
1327 /* Perform route selection; this is just to set the flags correctly
1328 * as local route in the VNI always wins.
1330 evpn_route_select_install(bgp
, vpn
, rn
);
1331 bgp_unlock_node(rn
);
1333 /* If this is a new route or some attribute has changed, export the
1334 * route to the global table. The route will be advertised to peers
1335 * from there. Note that this table is a 2-level tree (RD-level +
1336 * Prefix-level) similar to L3VPN routes.
1339 struct bgp_info
*global_ri
;
1341 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1342 (struct prefix
*)p
, &vpn
->prd
);
1343 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1, 0,
1346 /* Schedule for processing and unlock node. */
1347 bgp_process(bgp
, rn
, afi
, safi
);
1348 bgp_unlock_node(rn
);
1351 /* Unintern temporary. */
1352 aspath_unintern(&attr
.aspath
);
1357 /* Delete EVPN type5 route entry from global table */
1358 static void delete_evpn_type5_route_entry(struct bgp
*bgp_def
,
1359 struct bgp
*bgp_vrf
, afi_t afi
,
1360 safi_t safi
, struct bgp_node
*rn
,
1361 struct bgp_info
**ri
)
1363 struct bgp_info
*tmp_ri
= NULL
;
1367 /* find the matching route entry */
1368 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
)
1369 if (tmp_ri
->peer
== bgp_def
->peer_self
1370 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1371 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1376 /* Mark route for delete. */
1378 bgp_info_delete(rn
, tmp_ri
);
1381 /* Delete EVPN type5 route */
1382 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1384 afi_t afi
= AFI_L2VPN
;
1385 safi_t safi
= SAFI_EVPN
;
1386 struct bgp_node
*rn
= NULL
;
1387 struct bgp_info
*ri
= NULL
;
1388 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
1390 bgp_def
= bgp_get_default();
1394 /* locate the global route entry for this type-5 prefix */
1395 rn
= bgp_afi_node_lookup(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1396 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1400 delete_evpn_type5_route_entry(bgp_def
, bgp_vrf
, afi
, safi
, rn
, &ri
);
1402 bgp_process(bgp_def
, rn
, afi
, safi
);
1403 bgp_unlock_node(rn
);
1408 * Delete EVPN route entry. This could be in the VNI route table
1409 * or the global route table.
1411 static void delete_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1412 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1413 struct bgp_info
**ri
)
1415 struct bgp_info
*tmp_ri
;
1419 /* Now, find matching route. */
1420 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
)
1421 if (tmp_ri
->peer
== bgp
->peer_self
1422 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1423 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1428 /* Mark route for delete. */
1430 bgp_info_delete(rn
, tmp_ri
);
1434 * Delete EVPN route (of type based on prefix) for specified VNI and
1435 * schedule for processing.
1437 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1438 struct prefix_evpn
*p
)
1440 struct bgp_node
*rn
, *global_rn
;
1441 struct bgp_info
*ri
;
1442 afi_t afi
= AFI_L2VPN
;
1443 safi_t safi
= SAFI_EVPN
;
1445 /* First, locate the route node within the VNI. If it doesn't exist,
1447 * is nothing further to do.
1449 /* NOTE: There is no RD here. */
1450 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1454 /* Next, locate route node in the global EVPN routing table. Note that
1455 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1458 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1459 (struct prefix
*)p
, &vpn
->prd
);
1461 /* Delete route entry in the global EVPN table. */
1462 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
1464 /* Schedule for processing - withdraws to peers happen from
1468 bgp_process(bgp
, global_rn
, afi
, safi
);
1469 bgp_unlock_node(global_rn
);
1472 /* Delete route entry in the VNI route table. This can just be removed.
1474 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1476 bgp_info_reap(rn
, ri
);
1477 bgp_unlock_node(rn
);
1483 * Update all type-2 (MACIP) local routes for this VNI - these should also
1484 * be scheduled for advertise to peers.
1486 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1490 struct bgp_node
*rn
;
1491 struct bgp_info
*ri
;
1493 struct attr attr_sticky
;
1494 struct attr attr_def_gw
;
1495 struct attr attr_ip6_ll
;
1496 struct attr
*attr_new
;
1497 int add_l3_ecomm
= 0;
1501 memset(&attr
, 0, sizeof(struct attr
));
1502 memset(&attr_sticky
, 0, sizeof(struct attr
));
1503 memset(&attr_def_gw
, 0, sizeof(struct attr
));
1504 memset(&attr_ip6_ll
, 0, sizeof(struct attr
));
1506 /* Build path-attribute - multiple type-2 routes for this VNI will share
1507 * the same path attribute, but we need separate structures for sticky
1508 * MACs, default gateway and IPv6 link-local addresses (no L3 RT/RMAC).
1510 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1511 bgp_attr_default_set(&attr_sticky
, BGP_ORIGIN_IGP
);
1512 bgp_attr_default_set(&attr_def_gw
, BGP_ORIGIN_IGP
);
1513 attr
.nexthop
= vpn
->originator_ip
;
1514 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1515 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1516 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1517 attr_sticky
.nexthop
= vpn
->originator_ip
;
1518 attr_sticky
.mp_nexthop_global_in
= vpn
->originator_ip
;
1519 attr_sticky
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1520 attr_sticky
.sticky
= 1;
1521 bgpevpn_get_rmac(vpn
, &attr_sticky
.rmac
);
1522 attr_def_gw
.nexthop
= vpn
->originator_ip
;
1523 attr_def_gw
.mp_nexthop_global_in
= vpn
->originator_ip
;
1524 attr_def_gw
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1525 attr_def_gw
.default_gw
= 1;
1526 bgpevpn_get_rmac(vpn
, &attr_def_gw
.rmac
);
1527 bgp_attr_default_set(&attr_ip6_ll
, BGP_ORIGIN_IGP
);
1528 attr_ip6_ll
.nexthop
= vpn
->originator_ip
;
1529 attr_ip6_ll
.mp_nexthop_global_in
= vpn
->originator_ip
;
1530 attr_ip6_ll
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1532 /* Add L3 VNI RTs and RMAC for non IPv6 link-local attributes if
1533 * using L3 VNI for type-2 routes also.
1535 if (CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
))
1538 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1539 build_evpn_route_extcomm(vpn
, &attr_sticky
, add_l3_ecomm
);
1540 build_evpn_route_extcomm(vpn
, &attr_def_gw
, add_l3_ecomm
);
1541 build_evpn_route_extcomm(vpn
, &attr_ip6_ll
, 0);
1543 /* Walk this VNI's route table and update local type-2 routes. For any
1544 * routes updated, update corresponding entry in the global table too.
1546 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1547 rn
= bgp_route_next(rn
)) {
1548 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1549 struct bgp_node
*rd_rn
;
1550 struct bgp_info
*global_ri
;
1552 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1555 if (is_evpn_prefix_ipaddr_v6(evp
) &&
1556 IN6_IS_ADDR_LINKLOCAL(&evp
->prefix
.macip_addr
.ip
.ipaddr_v6
))
1557 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1558 &attr_ip6_ll
, 0, 1, &ri
, 0);
1560 if (evpn_route_is_sticky(bgp
, rn
))
1561 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1562 &attr_sticky
, 0, 1, &ri
,
1564 else if (evpn_route_is_def_gw(bgp
, rn
))
1565 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1566 &attr_def_gw
, 0, 1, &ri
,
1569 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1570 &attr
, 0, 1, &ri
, 0);
1573 /* If a local route exists for this prefix, we need to update
1574 * the global routing table too.
1579 /* Perform route selection; this is just to set the flags
1581 * as local route in the VNI always wins.
1583 evpn_route_select_install(bgp
, vpn
, rn
);
1585 attr_new
= ri
->attr
;
1587 /* Update route in global routing table. */
1588 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1589 (struct prefix
*)evp
, &vpn
->prd
);
1591 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
1594 /* Schedule for processing and unlock node. */
1595 bgp_process(bgp
, rd_rn
, afi
, safi
);
1596 bgp_unlock_node(rd_rn
);
1599 /* Unintern temporary. */
1600 aspath_unintern(&attr
.aspath
);
1601 aspath_unintern(&attr_sticky
.aspath
);
1602 aspath_unintern(&attr_def_gw
.aspath
);
1603 aspath_unintern(&attr_ip6_ll
.aspath
);
1609 * Delete all type-2 (MACIP) local routes for this VNI - only from the
1610 * global routing table. These are also scheduled for withdraw from peers.
1612 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1616 struct bgp_node
*rdrn
, *rn
;
1617 struct bgp_table
*table
;
1618 struct bgp_info
*ri
;
1623 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
1624 if (rdrn
&& rdrn
->info
) {
1625 table
= (struct bgp_table
*)rdrn
->info
;
1626 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
1627 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1629 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1632 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1634 bgp_process(bgp
, rn
, afi
, safi
);
1638 /* Unlock RD node. */
1640 bgp_unlock_node(rdrn
);
1646 * Delete all type-2 (MACIP) local routes for this VNI - from the global
1647 * table as well as the per-VNI route table.
1649 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1653 struct bgp_node
*rn
;
1654 struct bgp_info
*ri
;
1659 /* First, walk the global route table for this VNI's type-2 local
1661 * EVPN routes are a 2-level table, first get the RD table.
1663 delete_global_type2_routes(bgp
, vpn
);
1665 /* Next, walk this VNI's route table and delete local type-2 routes. */
1666 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1667 rn
= bgp_route_next(rn
)) {
1668 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1670 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1673 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1675 /* Route entry in local table gets deleted immediately. */
1677 bgp_info_reap(rn
, ri
);
1684 * Delete all routes in the per-VNI route table.
1686 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1688 struct bgp_node
*rn
;
1689 struct bgp_info
*ri
, *nextri
;
1691 /* Walk this VNI's route table and delete all routes. */
1692 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1693 rn
= bgp_route_next(rn
)) {
1694 for (ri
= rn
->info
; (ri
!= NULL
) && (nextri
= ri
->next
, 1);
1696 bgp_info_delete(rn
, ri
);
1697 bgp_info_reap(rn
, ri
);
1705 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
1706 * export RT getting modified or change to tunnel IP. Note that these
1707 * situations need the route in the per-VNI table as well as the global
1708 * table to be updated (as attributes change).
1710 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1713 struct prefix_evpn p
;
1715 /* Update and advertise the type-3 route (only one) followed by the
1716 * locally learnt type-2 routes (MACIP) - for this VNI.
1718 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1719 ret
= update_evpn_route(bgp
, vpn
, &p
, 0);
1723 return update_all_type2_routes(bgp
, vpn
);
1727 * Delete (and withdraw) local routes for specified VNI from the global
1728 * table and per-VNI table. After this, remove all other routes from
1729 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
1730 * (advertise-all-vni) being disabled.
1732 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1735 struct prefix_evpn p
;
1737 /* Delete and withdraw locally learnt type-2 routes (MACIP)
1738 * followed by type-3 routes (only one) - for this VNI.
1740 ret
= delete_all_type2_routes(bgp
, vpn
);
1744 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1745 ret
= delete_evpn_route(bgp
, vpn
, &p
);
1749 /* Delete all routes from the per-VNI table. */
1750 return delete_all_vni_routes(bgp
, vpn
);
1754 * There is a tunnel endpoint IP address change for this VNI, delete
1755 * prior type-3 route (if needed) and update.
1756 * Note: Route re-advertisement happens elsewhere after other processing
1759 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1760 struct in_addr originator_ip
)
1762 struct prefix_evpn p
;
1764 /* If VNI is not live, we only need to update the originator ip */
1765 if (!is_vni_live(vpn
)) {
1766 vpn
->originator_ip
= originator_ip
;
1770 /* Update the tunnel-ip hash */
1771 bgp_tip_del(bgp
, &vpn
->originator_ip
);
1772 bgp_tip_add(bgp
, &originator_ip
);
1774 /* filter routes as martian nexthop db has changed */
1775 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
1777 /* Need to withdraw type-3 route as the originator IP is part
1780 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1781 delete_evpn_route(bgp
, vpn
, &p
);
1783 /* Update the tunnel IP and re-advertise all routes for this VNI. */
1784 vpn
->originator_ip
= originator_ip
;
1789 * Install route entry into the VRF routing table and invoke route selection.
1791 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
1792 struct prefix_evpn
*evp
,
1793 struct bgp_info
*parent_ri
)
1795 struct bgp_node
*rn
;
1796 struct bgp_info
*ri
;
1798 struct attr
*attr_new
;
1801 struct prefix
*pp
= &p
;
1804 char buf
[PREFIX_STRLEN
];
1805 char buf1
[PREFIX_STRLEN
];
1807 memset(pp
, 0, sizeof(struct prefix
));
1808 ip_prefix_from_evpn_prefix(evp
, pp
);
1810 if (bgp_debug_zebra(NULL
)) {
1812 "installing evpn prefix %s as ip prefix %s in vrf %s",
1813 prefix2str(evp
, buf
, sizeof(buf
)),
1814 prefix2str(pp
, buf1
, sizeof(buf
)),
1815 vrf_id_to_name(bgp_vrf
->vrf_id
));
1818 /* Create (or fetch) route within the VRF. */
1819 /* NOTE: There is no RD here. */
1820 if (is_evpn_prefix_ipaddr_v4(evp
)) {
1822 safi
= SAFI_UNICAST
;
1823 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
1824 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
1826 safi
= SAFI_UNICAST
;
1827 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
1831 /* EVPN routes currently only support a IPv4 next hop which corresponds
1832 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
1833 * or prefix route, we have to convert the next hop to an IPv4-mapped
1834 * address for the rest of the code to flow through. In the case of IPv4,
1835 * make sure to set the flag for next hop attribute.
1837 bgp_attr_dup(&attr
, parent_ri
->attr
);
1839 evpn_convert_nexthop_to_ipv6(&attr
);
1841 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
1843 /* Check if route entry is already present. */
1844 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1846 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
1850 /* Add (or update) attribute to hash. */
1851 attr_new
= bgp_attr_intern(&attr
);
1853 /* Create new route with its attribute. */
1854 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
1855 parent_ri
->peer
, attr_new
, rn
);
1856 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1857 bgp_info_extra_get(ri
);
1858 ri
->extra
->parent
= bgp_info_lock(parent_ri
);
1859 if (parent_ri
->extra
) {
1860 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
1861 sizeof(ri
->extra
->label
));
1862 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
1864 bgp_info_add(rn
, ri
);
1866 if (attrhash_cmp(ri
->attr
, &attr
)
1867 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
1868 bgp_unlock_node(rn
);
1871 /* The attribute has changed. */
1872 /* Add (or update) attribute to hash. */
1873 attr_new
= bgp_attr_intern(&attr
);
1875 /* Restore route, if needed. */
1876 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
1877 bgp_info_restore(rn
, ri
);
1879 /* Mark if nexthop has changed. */
1880 if ((afi
== AFI_IP
&&
1881 !IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
)) ||
1883 !IPV6_ADDR_SAME(&ri
->attr
->mp_nexthop_global
,
1884 &attr_new
->mp_nexthop_global
)))
1885 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
1887 /* Unintern existing, set to new. */
1888 bgp_attr_unintern(&ri
->attr
);
1889 ri
->attr
= attr_new
;
1890 ri
->uptime
= bgp_clock();
1893 /* Perform route selection and update zebra, if required. */
1894 bgp_process(bgp_vrf
, rn
, afi
, safi
);
1900 * Install route entry into the VNI routing table and invoke route selection.
1902 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1903 struct prefix_evpn
*p
,
1904 struct bgp_info
*parent_ri
)
1906 struct bgp_node
*rn
;
1907 struct bgp_info
*ri
;
1908 struct attr
*attr_new
;
1911 /* Create (or fetch) route within the VNI. */
1912 /* NOTE: There is no RD here. */
1913 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1915 /* Check if route entry is already present. */
1916 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1918 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
1922 /* Add (or update) attribute to hash. */
1923 attr_new
= bgp_attr_intern(parent_ri
->attr
);
1925 /* Create new route with its attribute. */
1926 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
1927 parent_ri
->peer
, attr_new
, rn
);
1928 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1929 bgp_info_extra_get(ri
);
1930 ri
->extra
->parent
= bgp_info_lock(parent_ri
);
1931 if (parent_ri
->extra
) {
1932 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
1933 sizeof(ri
->extra
->label
));
1934 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
1936 bgp_info_add(rn
, ri
);
1938 if (attrhash_cmp(ri
->attr
, parent_ri
->attr
)
1939 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
1940 bgp_unlock_node(rn
);
1943 /* The attribute has changed. */
1944 /* Add (or update) attribute to hash. */
1945 attr_new
= bgp_attr_intern(parent_ri
->attr
);
1947 /* Restore route, if needed. */
1948 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
1949 bgp_info_restore(rn
, ri
);
1951 /* Mark if nexthop has changed. */
1952 if (!IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
))
1953 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
1955 /* Unintern existing, set to new. */
1956 bgp_attr_unintern(&ri
->attr
);
1957 ri
->attr
= attr_new
;
1958 ri
->uptime
= bgp_clock();
1961 /* Perform route selection and update zebra, if required. */
1962 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
1968 * Uninstall route entry from the VRF routing table and send message
1969 * to zebra, if appropriate.
1971 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
1972 struct prefix_evpn
*evp
,
1973 struct bgp_info
*parent_ri
)
1975 struct bgp_node
*rn
;
1976 struct bgp_info
*ri
;
1979 struct prefix
*pp
= &p
;
1982 char buf
[PREFIX_STRLEN
];
1983 char buf1
[PREFIX_STRLEN
];
1985 memset(pp
, 0, sizeof(struct prefix
));
1986 ip_prefix_from_evpn_prefix(evp
, pp
);
1988 if (bgp_debug_zebra(NULL
)) {
1990 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
1991 prefix2str(evp
, buf
, sizeof(buf
)),
1992 prefix2str(pp
, buf1
, sizeof(buf
)),
1993 vrf_id_to_name(bgp_vrf
->vrf_id
));
1996 /* Locate route within the VRF. */
1997 /* NOTE: There is no RD here. */
1998 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2000 safi
= SAFI_UNICAST
;
2001 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2004 safi
= SAFI_UNICAST
;
2005 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2011 /* Find matching route entry. */
2012 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2014 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2020 /* Mark entry for deletion */
2021 bgp_info_delete(rn
, ri
);
2023 /* Perform route selection and update zebra, if required. */
2024 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2026 /* Unlock route node. */
2027 bgp_unlock_node(rn
);
2033 * Uninstall route entry from the VNI routing table and send message
2034 * to zebra, if appropriate.
2036 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2037 struct prefix_evpn
*p
,
2038 struct bgp_info
*parent_ri
)
2040 struct bgp_node
*rn
;
2041 struct bgp_info
*ri
;
2044 /* Locate route within the VNI. */
2045 /* NOTE: There is no RD here. */
2046 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2050 /* Find matching route entry. */
2051 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2053 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2059 /* Mark entry for deletion */
2060 bgp_info_delete(rn
, ri
);
2062 /* Perform route selection and update zebra, if required. */
2063 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2065 /* Unlock route node. */
2066 bgp_unlock_node(rn
);
2072 * Given a route entry and a VRF, see if this route entry should be
2073 * imported into the VRF i.e., RTs match.
2075 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
, struct bgp_info
*ri
)
2077 struct attr
*attr
= ri
->attr
;
2078 struct ecommunity
*ecom
;
2082 /* Route should have valid RT to be even considered. */
2083 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2086 ecom
= attr
->ecommunity
;
2087 if (!ecom
|| !ecom
->size
)
2090 /* For each extended community RT, see if it matches this VNI. If any RT
2091 * matches, we're done.
2093 for (i
= 0; i
< ecom
->size
; i
++) {
2095 uint8_t type
, sub_type
;
2096 struct ecommunity_val
*eval
;
2097 struct ecommunity_val eval_tmp
;
2098 struct vrf_irt_node
*irt
;
2100 /* Only deal with RTs */
2101 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2102 eval
= (struct ecommunity_val
*)(ecom
->val
2103 + (i
* ECOMMUNITY_SIZE
));
2106 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2109 /* See if this RT matches specified VNIs import RTs */
2110 irt
= lookup_vrf_import_rt(eval
);
2111 if (irt
&& irt
->vrfs
)
2112 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2115 /* Also check for non-exact match. In this, we mask out the AS
2117 * only check on the local-admin sub-field. This is to
2119 * VNI as the RT for EBGP peering too.
2122 if (type
== ECOMMUNITY_ENCODE_AS
2123 || type
== ECOMMUNITY_ENCODE_AS4
2124 || type
== ECOMMUNITY_ENCODE_IP
) {
2125 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2126 mask_ecom_global_admin(&eval_tmp
, eval
);
2127 irt
= lookup_vrf_import_rt(&eval_tmp
);
2129 if (irt
&& irt
->vrfs
)
2130 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2138 * Given a route entry and a VNI, see if this route entry should be
2139 * imported into the VNI i.e., RTs match.
2141 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2142 struct bgp_info
*ri
)
2144 struct attr
*attr
= ri
->attr
;
2145 struct ecommunity
*ecom
;
2149 /* Route should have valid RT to be even considered. */
2150 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2153 ecom
= attr
->ecommunity
;
2154 if (!ecom
|| !ecom
->size
)
2157 /* For each extended community RT, see if it matches this VNI. If any RT
2158 * matches, we're done.
2160 for (i
= 0; i
< ecom
->size
; i
++) {
2162 uint8_t type
, sub_type
;
2163 struct ecommunity_val
*eval
;
2164 struct ecommunity_val eval_tmp
;
2165 struct irt_node
*irt
;
2167 /* Only deal with RTs */
2168 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2169 eval
= (struct ecommunity_val
*)(ecom
->val
2170 + (i
* ECOMMUNITY_SIZE
));
2173 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2176 /* See if this RT matches specified VNIs import RTs */
2177 irt
= lookup_import_rt(bgp
, eval
);
2179 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2182 /* Also check for non-exact match. In this, we mask out the AS
2184 * only check on the local-admin sub-field. This is to
2186 * VNI as the RT for EBGP peering too.
2189 if (type
== ECOMMUNITY_ENCODE_AS
2190 || type
== ECOMMUNITY_ENCODE_AS4
2191 || type
== ECOMMUNITY_ENCODE_IP
) {
2192 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2193 mask_ecom_global_admin(&eval_tmp
, eval
);
2194 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2197 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2205 * Install or uninstall mac-ip routes are appropriate for this
2208 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
2212 struct bgp_node
*rd_rn
, *rn
;
2213 struct bgp_table
*table
;
2214 struct bgp_info
*ri
;
2216 char buf
[PREFIX_STRLEN
];
2217 struct bgp
*bgp_def
= NULL
;
2221 bgp_def
= bgp_get_default();
2225 /* Walk entire global routing table and evaluate routes which could be
2226 * imported into this VRF. Note that we need to loop through all global
2227 * routes to determine which route matches the import rt on vrf
2229 for (rd_rn
= bgp_table_top(bgp_def
->rib
[afi
][safi
]); rd_rn
;
2230 rd_rn
= bgp_route_next(rd_rn
)) {
2231 table
= (struct bgp_table
*)(rd_rn
->info
);
2235 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2236 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2238 /* if not mac-ip route skip this route */
2239 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2240 || evp
->prefix
.route_type
2241 == BGP_EVPN_IP_PREFIX_ROUTE
))
2244 /* if not a mac+ip route skip this route */
2245 if (!(is_evpn_prefix_ipaddr_v4(evp
)
2246 || is_evpn_prefix_ipaddr_v6(evp
)))
2249 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2250 /* Consider "valid" remote routes applicable for
2253 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2254 && ri
->type
== ZEBRA_ROUTE_BGP
2255 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2258 if (is_route_matching_for_vrf(bgp_vrf
, ri
)) {
2260 ret
= install_evpn_route_entry_in_vrf(
2263 ret
= uninstall_evpn_route_entry_in_vrf(
2268 "Failed to %s EVPN %s route in VRF %s",
2271 prefix2str(evp
, buf
,
2286 * Install or uninstall routes of specified type that are appropriate for this
2289 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
2290 struct bgpevpn
*vpn
,
2291 bgp_evpn_route_type rtype
,
2296 struct bgp_node
*rd_rn
, *rn
;
2297 struct bgp_table
*table
;
2298 struct bgp_info
*ri
;
2304 /* Walk entire global routing table and evaluate routes which could be
2305 * imported into this VPN. Note that we cannot just look at the routes
2307 * the VNI's RD - remote routes applicable for this VNI could have any
2310 /* EVPN routes are a 2-level table. */
2311 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2312 rd_rn
= bgp_route_next(rd_rn
)) {
2313 table
= (struct bgp_table
*)(rd_rn
->info
);
2317 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2318 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2320 if (evp
->prefix
.route_type
!= rtype
)
2323 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2324 /* Consider "valid" remote routes applicable for
2326 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2327 && ri
->type
== ZEBRA_ROUTE_BGP
2328 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2331 if (is_route_matching_for_vni(bgp
, vpn
, ri
)) {
2333 ret
= install_evpn_route_entry(
2336 ret
= uninstall_evpn_route_entry(
2341 "%u: Failed to %s EVPN %s route in VNI %u",
2345 rtype
== BGP_EVPN_MAC_IP_ROUTE
2359 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
2360 * is invoked upon l3vni-add or l3vni import rt change
2362 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
2364 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
2369 * Install any existing remote routes applicable for this VNI into its
2370 * routing table. This is invoked when a VNI becomes "live" or its Import
2373 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2377 /* Install type-3 routes followed by type-2 routes - the ones applicable
2380 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
2385 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
2389 /* uninstall routes from l3vni vrf. */
2390 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
2392 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
2397 * Uninstall any existing remote routes for this VNI. One scenario in which
2398 * this is invoked is upon an import RT change.
2400 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2404 /* Uninstall type-2 routes followed by type-3 routes - the ones
2408 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
2413 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
2418 * Install or uninstall route in matching VRFs (list).
2420 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
2421 safi_t safi
, struct prefix_evpn
*evp
,
2422 struct bgp_info
*ri
,
2423 struct list
*vrfs
, int install
)
2425 char buf
[PREFIX2STR_BUFFER
];
2426 struct bgp
*bgp_vrf
;
2427 struct listnode
*node
, *nnode
;
2429 /* Only type-2/type-5 routes go into a VRF */
2430 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2431 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
2434 /* if it is type-2 route and not a mac+ip route skip this route */
2435 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
2436 && !(is_evpn_prefix_ipaddr_v4(evp
)
2437 || is_evpn_prefix_ipaddr_v6(evp
)))
2440 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
2444 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, ri
);
2446 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
2450 zlog_err("%u: Failed to %s prefix %s in VRF %s",
2452 install
? "install" : "uninstall",
2453 prefix2str(evp
, buf
, sizeof(buf
)),
2454 vrf_id_to_name(bgp_vrf
->vrf_id
));
2463 * Install or uninstall route in matching VNIs (list).
2465 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
2466 safi_t safi
, struct prefix_evpn
*evp
,
2467 struct bgp_info
*ri
,
2468 struct list
*vnis
, int install
)
2470 struct bgpevpn
*vpn
;
2471 struct listnode
*node
, *nnode
;
2473 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
2476 if (!is_vni_live(vpn
))
2480 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, ri
);
2482 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, ri
);
2485 zlog_err("%u: Failed to %s EVPN %s route in VNI %u",
2486 bgp
->vrf_id
, install
? "install" : "uninstall",
2487 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2499 * Install or uninstall route for appropriate VNIs.
2501 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
2502 struct prefix
*p
, struct bgp_info
*ri
,
2505 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
2506 struct attr
*attr
= ri
->attr
;
2507 struct ecommunity
*ecom
;
2512 /* Only type-2 and type-3 and type-5 are supported currently */
2513 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2514 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
2515 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
2518 /* If we don't have Route Target, nothing much to do. */
2519 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2522 ecom
= attr
->ecommunity
;
2523 if (!ecom
|| !ecom
->size
)
2526 /* For each extended community RT, see which VNIs/VRFs match and import
2527 * the route into matching VNIs/VRFs.
2529 for (i
= 0; i
< ecom
->size
; i
++) {
2531 uint8_t type
, sub_type
;
2532 struct ecommunity_val
*eval
;
2533 struct ecommunity_val eval_tmp
;
2534 struct irt_node
*irt
; /* import rt for l2vni */
2535 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
2537 /* Only deal with RTs */
2538 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2539 eval
= (struct ecommunity_val
*)(ecom
->val
2540 + (i
* ECOMMUNITY_SIZE
));
2543 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2546 /* Import route into matching l2-vnis (type-2/type-3 routes go
2549 irt
= lookup_import_rt(bgp
, eval
);
2551 install_uninstall_route_in_vnis(bgp
, afi
, safi
, evp
, ri
,
2554 /* Import route into matching l3-vnis (type-2/type-5 routes go
2555 * into l3vni/vrf table)
2557 vrf_irt
= lookup_vrf_import_rt(eval
);
2558 if (vrf_irt
&& vrf_irt
->vrfs
)
2559 install_uninstall_route_in_vrfs(bgp
, afi
, safi
, evp
, ri
,
2560 vrf_irt
->vrfs
, import
);
2562 /* Also check for non-exact match. In this,
2563 * we mask out the AS and
2564 * only check on the local-admin sub-field.
2565 * This is to facilitate using
2566 * VNI as the RT for EBGP peering too.
2570 if (type
== ECOMMUNITY_ENCODE_AS
2571 || type
== ECOMMUNITY_ENCODE_AS4
2572 || type
== ECOMMUNITY_ENCODE_IP
) {
2573 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2574 mask_ecom_global_admin(&eval_tmp
, eval
);
2575 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2576 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
2579 install_uninstall_route_in_vnis(bgp
, afi
, safi
, evp
, ri
,
2581 if (vrf_irt
&& vrf_irt
->vrfs
)
2582 install_uninstall_route_in_vrfs(bgp
, afi
, safi
, evp
, ri
,
2583 vrf_irt
->vrfs
, import
);
2589 /* delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
2591 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
2593 /* delete all ipv4 routes and withdraw from peers */
2594 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
2595 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
2597 /* delete all ipv6 routes and withdraw from peers */
2598 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
2599 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
2602 /* update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
2604 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
2606 /* update all ipv4 routes */
2607 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
2608 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
2610 /* update all ipv6 routes */
2611 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
2612 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
2616 * update and advertise local routes for a VRF as type-5 routes.
2617 * This is invoked upon RD change for a VRF. Note taht the processing is only
2618 * done in the global route table using the routes which already exist in the
2621 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
2623 /* skip if the RD is configured */
2624 if (is_vrf_rd_configured(bgp_vrf
))
2627 /* derive the RD for the VRF based on new router-id */
2628 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
2630 /* update advertise ipv4|ipv6 routes as type-5 routes */
2631 update_advertise_vrf_routes(bgp_vrf
);
2635 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
2636 * This is invoked upon VRF RD change. The processing is done only from global
2639 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
2641 /* skip if the RD is configured */
2642 if (is_vrf_rd_configured(bgp_vrf
))
2645 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
2646 delete_withdraw_vrf_routes(bgp_vrf
);
2650 * Update and advertise local routes for a VNI. Invoked upon router-id
2651 * change. Note that the processing is done only on the global route table
2652 * using routes that already exist in the per-VNI table.
2654 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2656 struct prefix_evpn p
;
2657 struct bgp_node
*rn
, *global_rn
;
2658 struct bgp_info
*ri
, *global_ri
;
2660 afi_t afi
= AFI_L2VPN
;
2661 safi_t safi
= SAFI_EVPN
;
2663 /* Locate type-3 route for VNI in the per-VNI table and use its
2664 * attributes to create and advertise the type-3 route for this VNI
2665 * in the global table.
2667 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2668 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
2669 if (!rn
) /* unexpected */
2671 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2672 if (ri
->peer
== bgp
->peer_self
&& ri
->type
== ZEBRA_ROUTE_BGP
2673 && ri
->sub_type
== BGP_ROUTE_STATIC
)
2675 if (!ri
) /* unexpected */
2679 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2680 (struct prefix
*)&p
, &vpn
->prd
);
2681 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1, 0, &ri
,
2684 /* Schedule for processing and unlock node. */
2685 bgp_process(bgp
, global_rn
, afi
, safi
);
2686 bgp_unlock_node(global_rn
);
2688 /* Now, walk this VNI's route table and use the route and its attribute
2689 * to create and schedule route in global table.
2691 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2692 rn
= bgp_route_next(rn
)) {
2693 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2695 /* Identify MAC-IP local routes. */
2696 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2699 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2700 if (ri
->peer
== bgp
->peer_self
2701 && ri
->type
== ZEBRA_ROUTE_BGP
2702 && ri
->sub_type
== BGP_ROUTE_STATIC
)
2707 /* Create route in global routing table using this route entry's
2711 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2712 (struct prefix
*)evp
, &vpn
->prd
);
2714 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
2717 /* Schedule for processing and unlock node. */
2718 bgp_process(bgp
, global_rn
, afi
, safi
);
2719 bgp_unlock_node(global_rn
);
2726 * Delete (and withdraw) local routes for a VNI - only from the global
2727 * table. Invoked upon router-id change.
2729 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2732 struct prefix_evpn p
;
2733 struct bgp_node
*global_rn
;
2734 struct bgp_info
*ri
;
2735 afi_t afi
= AFI_L2VPN
;
2736 safi_t safi
= SAFI_EVPN
;
2738 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2739 * for this VNI - from the global table.
2741 ret
= delete_global_type2_routes(bgp
, vpn
);
2745 /* Remove type-3 route for this VNI from global table. */
2746 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2747 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
2748 (struct prefix
*)&p
, &vpn
->prd
);
2750 /* Delete route entry in the global EVPN table. */
2751 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
2753 /* Schedule for processing - withdraws to peers happen from
2757 bgp_process(bgp
, global_rn
, afi
, safi
);
2758 bgp_unlock_node(global_rn
);
2765 * Handle router-id change. Update and advertise local routes corresponding
2766 * to this VNI from peers. Note that this is invoked after updating the
2767 * router-id. The routes in the per-VNI table are used to create routes in
2768 * the global table and schedule them.
2770 static void update_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
2772 struct bgpevpn
*vpn
;
2774 vpn
= (struct bgpevpn
*)backet
->data
;
2777 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__
);
2781 /* Skip VNIs with configured RD. */
2782 if (is_rd_configured(vpn
))
2785 bgp_evpn_derive_auto_rd(bgp
, vpn
);
2786 update_advertise_vni_routes(bgp
, vpn
);
2790 * Handle router-id change. Delete and withdraw local routes corresponding
2791 * to this VNI from peers. Note that this is invoked prior to updating
2792 * the router-id and is done only on the global route table, the routes
2793 * are needed in the per-VNI table to re-advertise with new router id.
2795 static void withdraw_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
2797 struct bgpevpn
*vpn
;
2799 vpn
= (struct bgpevpn
*)backet
->data
;
2802 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__
);
2806 /* Skip VNIs with configured RD. */
2807 if (is_rd_configured(vpn
))
2810 delete_withdraw_vni_routes(bgp
, vpn
);
2814 * Process received EVPN type-2 route (advertise or withdraw).
2816 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
2817 struct attr
*attr
, uint8_t *pfx
, int psize
,
2818 uint32_t addpath_id
)
2820 struct prefix_rd prd
;
2821 struct prefix_evpn p
;
2822 struct bgp_route_evpn evpn
;
2824 uint8_t macaddr_len
;
2825 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
2826 uint32_t num_labels
= 0;
2830 /* Type-2 route should be either 33, 37 or 49 bytes or an
2831 * additional 3 bytes if there is a second label (VNI):
2832 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
2833 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
2834 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
2836 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
2837 && psize
!= 40 && psize
!= 52) {
2838 zlog_err("%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
2839 peer
->bgp
->vrf_id
, peer
->host
, psize
);
2843 memset(&evpn
, 0, sizeof(evpn
));
2845 /* Make prefix_rd */
2846 prd
.family
= AF_UNSPEC
;
2848 memcpy(&prd
.val
, pfx
, 8);
2851 /* Make EVPN prefix. */
2852 memset(&p
, 0, sizeof(struct prefix_evpn
));
2854 p
.prefixlen
= EVPN_TYPE_2_ROUTE_PREFIXLEN
;
2855 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
2857 /* Copy Ethernet Seg Identifier */
2858 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
2861 /* Copy Ethernet Tag */
2862 memcpy(ð_tag
, pfx
, 4);
2863 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
2866 /* Get the MAC Addr len */
2867 macaddr_len
= *pfx
++;
2869 /* Get the MAC Addr */
2870 if (macaddr_len
== (ETH_ALEN
* 8)) {
2871 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
2875 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
2876 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
2882 ipaddr_len
= *pfx
++;
2883 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
2884 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
2886 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
2887 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
2892 ipaddr_len
/= 8; /* Convert to bytes. */
2893 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
2896 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
2900 /* Get the VNI(s). Stored as bytes here. */
2902 memset(label
, 0, sizeof(label
));
2903 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
2904 pfx
+= BGP_LABEL_BYTES
;
2905 psize
-= (33 + ipaddr_len
);
2906 /* Do we have a second VNI? */
2909 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
2911 * If in future, we are required to access additional fields,
2912 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
2917 /* Process the route. */
2919 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2920 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2921 &prd
, &label
[0], num_labels
, 0, &evpn
);
2923 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2924 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2925 &prd
, &label
[0], num_labels
, &evpn
);
2930 * Process received EVPN type-3 route (advertise or withdraw).
2932 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
2933 struct attr
*attr
, uint8_t *pfx
, int psize
,
2934 uint32_t addpath_id
)
2936 struct prefix_rd prd
;
2937 struct prefix_evpn p
;
2942 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
2943 * IP len (1) and IP (4 or 16).
2945 if (psize
!= 17 && psize
!= 29) {
2946 zlog_err("%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
2947 peer
->bgp
->vrf_id
, peer
->host
, psize
);
2951 /* If PMSI is present, log if it is anything other than IR.
2952 * Note: We just simply ignore the values as it is not clear if
2953 * doing anything else is better.
2956 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
2957 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
) {
2958 zlog_warn("%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
2959 peer
->bgp
->vrf_id
, peer
->host
,
2960 attr
->pmsi_tnl_type
);
2964 /* Make prefix_rd */
2965 prd
.family
= AF_UNSPEC
;
2967 memcpy(&prd
.val
, pfx
, 8);
2970 /* Make EVPN prefix. */
2971 memset(&p
, 0, sizeof(struct prefix_evpn
));
2973 p
.prefixlen
= EVPN_TYPE_3_ROUTE_PREFIXLEN
;
2974 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
2976 /* Copy Ethernet Tag */
2977 memcpy(ð_tag
, pfx
, 4);
2978 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
2982 ipaddr_len
= *pfx
++;
2983 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
2984 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
2985 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
2988 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
2989 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
2993 /* Process the route. */
2995 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2996 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2997 &prd
, NULL
, 0, 0, NULL
);
2999 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3000 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3001 &prd
, NULL
, 0, NULL
);
3006 * Process received EVPN type-5 route (advertise or withdraw).
3008 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3009 struct attr
*attr
, uint8_t *pfx
, int psize
,
3010 uint32_t addpath_id
, int withdraw
)
3012 struct prefix_rd prd
;
3013 struct prefix_evpn p
;
3014 struct bgp_route_evpn evpn
;
3017 mpls_label_t label
; /* holds the VNI as in the packet */
3020 /* Type-5 route should be 34 or 58 bytes:
3021 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3022 * GW (4 or 16) and VNI (3).
3023 * Note that the IP and GW should both be IPv4 or both IPv6.
3025 if (psize
!= 34 && psize
!= 58) {
3026 zlog_err("%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3027 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3031 /* Make prefix_rd */
3032 prd
.family
= AF_UNSPEC
;
3034 memcpy(&prd
.val
, pfx
, 8);
3037 /* Make EVPN prefix. */
3038 memset(&p
, 0, sizeof(struct prefix_evpn
));
3040 p
.prefixlen
= EVPN_TYPE_5_ROUTE_PREFIXLEN
;
3041 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
3043 /* Additional information outside of prefix - ESI and GW IP */
3044 memset(&evpn
, 0, sizeof(evpn
));
3047 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
3050 /* Fetch Ethernet Tag. */
3051 memcpy(ð_tag
, pfx
, 4);
3052 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
3055 /* Fetch IP prefix length. */
3057 if (ippfx_len
> IPV6_MAX_BITLEN
) {
3059 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3060 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
3063 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
3065 /* Determine IPv4 or IPv6 prefix */
3066 /* Since the address and GW are from the same family, this just becomes
3067 * a simple check on the total size.
3070 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
3071 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
3073 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
3076 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
3077 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
3079 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
3083 /* Get the VNI (in MPLS label field). Stored as bytes here. */
3084 memset(&label
, 0, sizeof(label
));
3085 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
3088 * If in future, we are required to access additional fields,
3089 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
3093 /* Process the route. */
3095 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3096 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3097 &prd
, &label
, 1, 0, &evpn
);
3099 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3100 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3101 &prd
, &label
, 1, &evpn
);
3106 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
3107 struct prefix_rd
*prd
, mpls_label_t
*label
,
3108 uint32_t num_labels
, struct attr
*attr
)
3112 struct evpn_addr
*p_evpn_p
;
3114 memset(&temp
, 0, 16);
3115 if (p
->family
!= AF_EVPN
)
3117 p_evpn_p
= &(p
->u
.prefix_evpn
);
3119 /* len denites the total len of IP and GW-IP in the route
3120 IP and GW-IP have to be both ipv4 or ipv6
3122 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3123 len
= 8; /* IP and GWIP are both ipv4 */
3125 len
= 32; /* IP and GWIP are both ipv6 */
3126 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
3127 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
3128 stream_put(s
, prd
->val
, 8);
3130 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
3132 stream_put(s
, &temp
, 10);
3133 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
3134 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
3135 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3136 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
3138 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
3140 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3142 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
3144 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
3146 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3147 stream_put_ipv4(s
, 0);
3149 stream_put(s
, &temp
, 16);
3153 stream_put(s
, label
, 3);
3159 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
3161 static void cleanup_vni_on_disable(struct hash_backet
*backet
, struct bgp
*bgp
)
3163 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3165 /* Remove EVPN routes and schedule for processing. */
3166 delete_routes_for_vni(bgp
, vpn
);
3168 /* Clear "live" flag and see if hash needs to be freed. */
3169 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
3170 if (!is_vni_configured(vpn
))
3171 bgp_evpn_free(bgp
, vpn
);
3175 * Free a VNI entry; iterator function called during cleanup.
3177 static void free_vni_entry(struct hash_backet
*backet
, struct bgp
*bgp
)
3179 struct bgpevpn
*vpn
;
3181 vpn
= (struct bgpevpn
*)backet
->data
;
3182 delete_all_vni_routes(bgp
, vpn
);
3183 bgp_evpn_free(bgp
, vpn
);
3187 * Derive AUTO import RT for BGP VRF - L3VNI
3189 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
3191 struct bgp
*bgp_def
= NULL
;
3193 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
3194 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
3197 bgp_def
= bgp_get_default();
3200 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
3204 * Delete AUTO import RT from BGP VRF - L3VNI
3206 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
3208 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
3212 * Derive AUTO export RT for BGP VRF - L3VNI
3214 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
3216 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
3217 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
3221 * Delete AUTO export RT from BGP VRF - L3VNI
3223 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
3225 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
3228 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
3230 struct bgp
*bgp_def
= NULL
;
3231 struct listnode
*node
= NULL
;
3232 struct bgpevpn
*vpn
= NULL
;
3234 bgp_def
= bgp_get_default();
3238 /* update all type-5 routes */
3239 update_advertise_vrf_routes(bgp_vrf
);
3241 /* update all type-2 routes */
3242 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
3243 update_routes_for_vni(bgp_def
, vpn
);
3247 * Handle autort change for a given VNI.
3249 static void update_autort_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3251 struct bgpevpn
*vpn
= backet
->data
;
3254 zlog_warn("%s: VNI hash entry for VNI not found", __PRETTY_FUNCTION__
);
3258 if (!is_import_rt_configured(vpn
)) {
3259 if (is_vni_live(vpn
))
3260 bgp_evpn_uninstall_routes(bgp
, vpn
);
3261 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
3262 list_delete_all_node(vpn
->import_rtl
);
3263 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
3264 if (is_vni_live(vpn
))
3265 bgp_evpn_install_routes(bgp
, vpn
);
3267 if (!is_export_rt_configured(vpn
)) {
3268 list_delete_all_node(vpn
->export_rtl
);
3269 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
3270 if (is_vni_live(vpn
))
3271 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
3279 /* withdraw type-5 route corresponding to ip prefix */
3280 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
3281 afi_t afi
, safi_t safi
)
3284 struct prefix_evpn evp
;
3285 char buf
[PREFIX_STRLEN
];
3287 build_type5_prefix_from_ip_prefix(&evp
, p
);
3288 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
3291 "%u failed to delete type-5 route for prefix %s in vrf %s",
3292 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
3293 vrf_id_to_name(bgp_vrf
->vrf_id
));
3297 /* withdraw all type-5 routes for an address family */
3298 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
3300 struct bgp_table
*table
= NULL
;
3301 struct bgp_node
*rn
= NULL
;
3302 struct bgp_info
*ri
;
3304 table
= bgp_vrf
->rib
[afi
][safi
];
3305 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3306 /* Only care about "selected" routes - non-imported. */
3307 /* TODO: Support for AddPath for EVPN. */
3308 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
3309 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
)
3310 && (!ri
->extra
|| !ri
->extra
->parent
)) {
3311 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
3320 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
3321 * to this function correspond to those of the source IP prefix (best
3322 * path in the case of the attr. In the case of a local prefix (when we
3323 * are advertising local subnets), the src_attr will be NULL.
3325 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
3326 struct attr
*src_attr
, afi_t afi
,
3330 struct prefix_evpn evp
;
3331 char buf
[PREFIX_STRLEN
];
3333 build_type5_prefix_from_ip_prefix(&evp
, p
);
3334 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
3336 zlog_err("%u: Failed to create type-5 route for prefix %s",
3337 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
3340 /* Inject all prefixes of a particular address-family (currently, IPv4 or
3341 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
3342 * advertisement is enabled.
3344 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
3347 struct bgp_table
*table
= NULL
;
3348 struct bgp_node
*rn
= NULL
;
3349 struct bgp_info
*ri
;
3351 table
= bgp_vrf
->rib
[afi
][safi
];
3352 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3353 /* Need to identify the "selected" route entry to use its
3354 * attribute. Also, we only consider "non-imported" routes.
3355 * TODO: Support for AddPath for EVPN.
3357 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
3358 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
)
3359 && (!ri
->extra
|| !ri
->extra
->parent
)) {
3361 /* apply the route-map */
3362 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
3365 ret
= route_map_apply(
3366 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
3368 &rn
->p
, RMAP_BGP
, ri
);
3369 if (ret
== RMAP_DENYMATCH
)
3372 bgp_evpn_advertise_type5_route(
3373 bgp_vrf
, &rn
->p
, ri
->attr
, afi
, safi
);
3380 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
3382 struct listnode
*node
, *nnode
, *node_to_del
;
3383 struct ecommunity
*ecom
, *ecom_auto
;
3384 struct ecommunity_val eval
;
3386 if (bgp
->advertise_autort_rfc8365
)
3387 vni
|= EVPN_AUTORT_VXLAN
;
3388 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
3390 ecom_auto
= ecommunity_new();
3391 ecommunity_add_val(ecom_auto
, &eval
);
3394 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
3395 if (ecommunity_match(ecom
, ecom_auto
)) {
3396 ecommunity_free(&ecom
);
3402 list_delete_node(rtl
, node_to_del
);
3404 ecommunity_free(&ecom_auto
);
3407 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
3408 struct ecommunity
*ecomadd
)
3410 /* uninstall routes from vrf */
3411 uninstall_routes_for_vrf(bgp_vrf
);
3413 /* Cleanup the RT to VRF mapping */
3414 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
3416 /* Remove auto generated RT */
3417 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
3419 /* Add the newly configured RT to RT list */
3420 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
3421 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
3423 /* map VRF to its RTs */
3424 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
3426 /* install routes matching the new VRF */
3427 install_routes_for_vrf(bgp_vrf
);
3430 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
3431 struct ecommunity
*ecomdel
)
3433 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
3434 struct ecommunity
*ecom
= NULL
;
3436 /* uninstall routes from vrf */
3437 uninstall_routes_for_vrf(bgp_vrf
);
3439 /* Cleanup the RT to VRF mapping */
3440 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
3442 /* remove the RT from the RT list */
3443 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
3444 if (ecommunity_match(ecom
, ecomdel
)) {
3445 ecommunity_free(&ecom
);
3452 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
3454 /* fallback to auto import rt, if this was the last RT */
3455 if (list_isempty(bgp_vrf
->vrf_import_rtl
)) {
3456 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
3457 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
3460 /* map VRFs to its RTs */
3461 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
3463 /* install routes matching this new RT */
3464 install_routes_for_vrf(bgp_vrf
);
3467 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
3468 struct ecommunity
*ecomadd
)
3470 /* remove auto-generated RT */
3471 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
3473 /* Add the new RT to the RT list */
3474 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
3475 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
3477 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
3480 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
3481 struct ecommunity
*ecomdel
)
3483 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
3484 struct ecommunity
*ecom
= NULL
;
3486 /* Remove the RT from the RT list */
3487 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
3488 if (ecommunity_match(ecom
, ecomdel
)) {
3489 ecommunity_free(&ecom
);
3496 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
3498 /* fall back to auto-generated RT if this was the last RT */
3499 if (bgp_vrf
->vrf_export_rtl
&& list_isempty(bgp_vrf
->vrf_export_rtl
)) {
3500 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
3501 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
3504 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
3508 * Handle change to BGP router id. This is invoked twice by the change
3509 * handler, first before the router id has been changed and then after
3510 * the router id has been changed. The first invocation will result in
3511 * local routes for all VNIs/VRF being deleted and withdrawn and the next
3512 * will result in the routes being re-advertised.
3514 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
3518 /* delete and withdraw all the type-5 routes
3519 stored in the global table for this vrf
3521 withdraw_router_id_vrf(bgp
);
3523 /* delete all the VNI routes (type-2/type-3) routes for all the
3526 hash_iterate(bgp
->vnihash
,
3527 (void (*)(struct hash_backet
*,
3528 void *))withdraw_router_id_vni
,
3532 /* advertise all routes in the vrf as type-5 routes with the new
3535 update_router_id_vrf(bgp
);
3537 /* advertise all the VNI routes (type-2/type-3) routes with the
3540 hash_iterate(bgp
->vnihash
,
3541 (void (*)(struct hash_backet
*,
3542 void *))update_router_id_vni
,
3548 * Handle change to auto-RT algorithm - update and advertise local routes.
3550 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
3552 hash_iterate(bgp
->vnihash
,
3553 (void (*)(struct hash_backet
*,
3554 void*))update_autort_vni
,
3559 * Handle change to export RT - update and advertise local routes.
3561 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3563 return update_routes_for_vni(bgp
, vpn
);
3566 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
3569 delete_withdraw_vrf_routes(bgp_vrf
);
3571 update_advertise_vrf_routes(bgp_vrf
);
3575 * Handle change to RD. This is invoked twice by the change handler,
3576 * first before the RD has been changed and then after the RD has
3577 * been changed. The first invocation will result in local routes
3578 * of this VNI being deleted and withdrawn and the next will result
3579 * in the routes being re-advertised.
3581 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
3585 delete_withdraw_vni_routes(bgp
, vpn
);
3587 update_advertise_vni_routes(bgp
, vpn
);
3591 * Install routes for this VNI. Invoked upon change to Import RT.
3593 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3595 return install_routes_for_vni(bgp
, vpn
);
3599 * Uninstall all routes installed for this VNI. Invoked upon change
3602 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3604 return uninstall_routes_for_vni(bgp
, vpn
);
3608 * TODO: Hardcoded for a maximum of 2 VNIs right now
3610 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
3615 vni1
= label2vni(label
);
3616 if (num_labels
== 2) {
3617 vni2
= label2vni(label
+ 1);
3618 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
3620 snprintf(buf
, len
, "%u", vni1
);
3625 * Function to convert evpn route to json format.
3626 * NOTE: We don't use prefix2str as the output here is a bit different.
3628 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
3630 char buf1
[ETHER_ADDR_STRLEN
];
3631 char buf2
[PREFIX2STR_BUFFER
];
3636 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
3637 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
3638 json_object_int_add(json
, "ethTag",
3639 p
->prefix
.imet_addr
.eth_tag
);
3640 json_object_int_add(json
, "ipLen",
3641 is_evpn_prefix_ipaddr_v4(p
)
3644 json_object_string_add(json
, "ip",
3645 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
3646 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3647 if (is_evpn_prefix_ipaddr_none(p
)) {
3648 json_object_int_add(json
, "routeType",
3649 p
->prefix
.route_type
);
3650 json_object_int_add(json
, "ethTag",
3651 p
->prefix
.macip_addr
.eth_tag
);
3652 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
3653 json_object_string_add(json
, "mac",
3654 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
3660 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
3663 json_object_int_add(json
, "routeType",
3664 p
->prefix
.route_type
);
3665 json_object_int_add(json
, "ethTag",
3666 p
->prefix
.macip_addr
.eth_tag
);
3667 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
3668 json_object_string_add(json
, "mac",
3669 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
3672 json_object_int_add(json
, "ipLen",
3673 is_evpn_prefix_ipaddr_v4(p
)
3676 json_object_string_add(
3679 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
3681 PREFIX2STR_BUFFER
));
3684 /* Currently, this is to cater to other AF_ETHERNET code. */
3689 * Function to convert evpn route to string.
3690 * NOTE: We don't use prefix2str as the output here is a bit different.
3692 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
3694 char buf1
[ETHER_ADDR_STRLEN
];
3695 char buf2
[PREFIX2STR_BUFFER
];
3697 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
3698 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
3699 p
->prefix
.imet_addr
.eth_tag
,
3700 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
3702 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
3703 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3704 if (is_evpn_prefix_ipaddr_none(p
))
3705 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
3706 p
->prefix
.route_type
,
3707 p
->prefix
.macip_addr
.eth_tag
,
3709 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
3714 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
3716 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
3717 p
->prefix
.route_type
,
3718 p
->prefix
.macip_addr
.eth_tag
,
3720 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
3722 family
== AF_INET
? IPV4_MAX_BITLEN
3725 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
3727 PREFIX2STR_BUFFER
));
3729 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3730 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
3731 p
->prefix
.route_type
,
3732 p
->prefix
.prefix_addr
.eth_tag
,
3733 p
->prefix
.prefix_addr
.ip_prefix_length
,
3734 is_evpn_prefix_ipaddr_v4(p
)
3735 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
3736 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
3738 /* For EVPN route types not supported yet. */
3739 snprintf(buf
, len
, "(unsupported route type %d)",
3740 p
->prefix
.route_type
);
3747 * Encode EVPN prefix in Update (MP_REACH)
3749 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
3750 struct prefix_rd
*prd
, mpls_label_t
*label
,
3751 uint32_t num_labels
, struct attr
*attr
,
3752 int addpath_encode
, uint32_t addpath_tx_id
)
3754 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3755 int len
, ipa_len
= 0;
3758 stream_putl(s
, addpath_tx_id
);
3761 stream_putc(s
, evp
->prefix
.route_type
);
3763 switch (evp
->prefix
.route_type
) {
3764 case BGP_EVPN_MAC_IP_ROUTE
:
3765 if (is_evpn_prefix_ipaddr_v4(evp
))
3766 ipa_len
= IPV4_MAX_BYTELEN
;
3767 else if (is_evpn_prefix_ipaddr_v6(evp
))
3768 ipa_len
= IPV6_MAX_BYTELEN
;
3769 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
3770 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
3771 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
3773 stream_putc(s
, len
);
3774 stream_put(s
, prd
->val
, 8); /* RD */
3776 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
3778 stream_put(s
, 0, 10);
3779 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
3780 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
3781 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
3782 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
3783 if (ipa_len
) /* IP */
3784 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
3786 /* 1st label is the L2 VNI */
3787 stream_put(s
, label
, BGP_LABEL_BYTES
);
3788 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
3789 if (ipa_len
&& num_labels
> 1)
3790 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
3793 case BGP_EVPN_IMET_ROUTE
:
3794 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
3795 stream_put(s
, prd
->val
, 8); /* RD */
3796 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
3797 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
3798 /* Originating Router's IP Addr */
3799 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
3802 case BGP_EVPN_IP_PREFIX_ROUTE
:
3803 /* TODO: AddPath support. */
3804 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
3812 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
3813 struct bgp_nlri
*packet
, int withdraw
)
3819 uint32_t addpath_id
;
3820 int addpath_encoded
;
3826 /* Start processing the NLRI - there may be multiple in the MP_REACH */
3828 lim
= pnt
+ packet
->length
;
3830 safi
= packet
->safi
;
3834 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
3835 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
3836 PEER_CAP_ADDPATH_AF_TX_RCV
));
3838 for (; pnt
< lim
; pnt
+= psize
) {
3839 /* Clear prefix structure. */
3840 memset(&p
, 0, sizeof(struct prefix
));
3842 /* Deal with path-id if AddPath is supported. */
3843 if (addpath_encoded
) {
3844 /* When packet overflow occurs return immediately. */
3845 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
3848 addpath_id
= ntohl(*((uint32_t *)pnt
));
3849 pnt
+= BGP_ADDPATH_ID_LEN
;
3852 /* All EVPN NLRI types start with type and length. */
3857 psize
= rlen
= *pnt
++;
3859 /* When packet overflow occur return immediately. */
3860 if (pnt
+ psize
> lim
)
3864 case BGP_EVPN_MAC_IP_ROUTE
:
3865 if (process_type2_route(peer
, afi
, safi
,
3866 withdraw
? NULL
: attr
, pnt
,
3867 psize
, addpath_id
)) {
3869 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
3870 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3875 case BGP_EVPN_IMET_ROUTE
:
3876 if (process_type3_route(peer
, afi
, safi
,
3877 withdraw
? NULL
: attr
, pnt
,
3878 psize
, addpath_id
)) {
3880 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
3881 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3886 case BGP_EVPN_IP_PREFIX_ROUTE
:
3887 if (process_type5_route(peer
, afi
, safi
, attr
, pnt
,
3888 psize
, addpath_id
, withdraw
)) {
3890 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
3891 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3901 /* Packet length consistency check. */
3909 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
3910 * The mapping will be used during route processing.
3911 * bgp_def: default bgp instance
3912 * bgp_vrf: specific bgp vrf instance on which RT is configured
3914 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
3917 struct ecommunity_val
*eval
= NULL
;
3918 struct listnode
*node
= NULL
, *nnode
= NULL
;
3919 struct ecommunity
*ecom
= NULL
;
3921 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
3922 for (i
= 0; i
< ecom
->size
; i
++) {
3923 eval
= (struct ecommunity_val
*)(ecom
->val
3925 * ECOMMUNITY_SIZE
));
3926 map_vrf_to_rt(bgp_vrf
, eval
);
3932 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
3934 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
3937 struct ecommunity_val
*eval
;
3938 struct listnode
*node
, *nnode
;
3939 struct ecommunity
*ecom
;
3941 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
3942 for (i
= 0; i
< ecom
->size
; i
++) {
3943 struct vrf_irt_node
*irt
;
3944 struct ecommunity_val eval_tmp
;
3946 eval
= (struct ecommunity_val
*)(ecom
->val
3948 * ECOMMUNITY_SIZE
));
3949 /* If using "automatic" RT, we only care about the
3950 * local-admin sub-field.
3951 * This is to facilitate using VNI as the RT for EBGP
3954 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3955 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
3956 BGP_VRF_IMPORT_RT_CFGD
))
3957 mask_ecom_global_admin(&eval_tmp
, eval
);
3959 irt
= lookup_vrf_import_rt(&eval_tmp
);
3961 unmap_vrf_from_rt(bgp_vrf
, irt
);
3968 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
3969 * The mapping will be used during route processing.
3971 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3974 struct ecommunity_val
*eval
;
3975 struct listnode
*node
, *nnode
;
3976 struct ecommunity
*ecom
;
3978 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
3979 for (i
= 0; i
< ecom
->size
; i
++) {
3980 eval
= (struct ecommunity_val
*)(ecom
->val
3982 * ECOMMUNITY_SIZE
));
3983 map_vni_to_rt(bgp
, vpn
, eval
);
3989 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
3991 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3994 struct ecommunity_val
*eval
;
3995 struct listnode
*node
, *nnode
;
3996 struct ecommunity
*ecom
;
3998 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
3999 for (i
= 0; i
< ecom
->size
; i
++) {
4000 struct irt_node
*irt
;
4001 struct ecommunity_val eval_tmp
;
4003 eval
= (struct ecommunity_val
*)(ecom
->val
4005 * ECOMMUNITY_SIZE
));
4006 /* If using "automatic" RT, we only care about the
4007 * local-admin sub-field.
4008 * This is to facilitate using VNI as the RT for EBGP
4011 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4012 if (!is_import_rt_configured(vpn
))
4013 mask_ecom_global_admin(&eval_tmp
, eval
);
4015 irt
= lookup_import_rt(bgp
, &eval_tmp
);
4017 unmap_vni_from_rt(bgp
, vpn
, irt
);
4023 * Derive Import RT automatically for VNI and map VNI to RT.
4024 * The mapping will be used during route processing.
4026 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4028 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
4029 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
4032 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
4036 * Derive Export RT automatically for VNI.
4038 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4040 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
4041 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
4045 * Derive RD automatically for VNI using passed information - it
4046 * is of the form RouterId:unique-id-for-vni.
4048 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
4050 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
4054 * Derive RD automatically for VNI using passed information - it
4055 * is of the form RouterId:unique-id-for-vni.
4057 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4061 vpn
->prd
.family
= AF_UNSPEC
;
4062 vpn
->prd
.prefixlen
= 64;
4063 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
4064 (void)str2prefix_rd(buf
, &vpn
->prd
);
4065 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
4071 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
4073 struct bgpevpn
*vpn
;
4076 memset(&tmp
, 0, sizeof(struct bgpevpn
));
4078 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
4083 * Create a new vpn - invoked upon configuration or zebra notification.
4085 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
4086 struct in_addr originator_ip
,
4087 vrf_id_t tenant_vrf_id
)
4089 struct bgpevpn
*vpn
;
4094 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
4098 /* Set values - RD and RT set to defaults. */
4100 vpn
->originator_ip
= originator_ip
;
4101 vpn
->tenant_vrf_id
= tenant_vrf_id
;
4103 /* Initialize route-target import and export lists */
4104 vpn
->import_rtl
= list_new();
4105 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4106 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
4107 vpn
->export_rtl
= list_new();
4108 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4109 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
4110 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
4111 derive_rd_rt_for_vni(bgp
, vpn
);
4113 /* Initialize EVPN route table. */
4114 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
4117 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
4118 XFREE(MTYPE_BGP_EVPN
, vpn
);
4122 /* add to l2vni list on corresponding vrf */
4123 bgpevpn_link_to_l3vni(vpn
);
4125 QOBJ_REG(vpn
, bgpevpn
);
4130 * Free a given VPN - called in multiple scenarios such as zebra
4131 * notification, configuration being deleted, advertise-all-vni disabled etc.
4132 * This just frees appropriate memory, caller should have taken other
4135 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4137 bgpevpn_unlink_from_l3vni(vpn
);
4138 bgp_table_unlock(vpn
->route_table
);
4139 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4140 list_delete_and_null(&vpn
->import_rtl
);
4141 list_delete_and_null(&vpn
->export_rtl
);
4142 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
4143 hash_release(bgp
->vnihash
, vpn
);
4145 XFREE(MTYPE_BGP_EVPN
, vpn
);
4149 * Import route into matching VNI(s).
4151 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
4152 struct prefix
*p
, struct bgp_info
*ri
)
4154 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 1);
4158 * Unimport route from matching VNI(s).
4160 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
4161 struct prefix
*p
, struct bgp_info
*ri
)
4163 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 0);
4166 /* filter routes which have martian next hops */
4167 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
4171 struct bgp_node
*rd_rn
, *rn
;
4172 struct bgp_table
*table
;
4173 struct bgp_info
*ri
;
4178 /* Walk entire global routing table and evaluate routes which could be
4179 * imported into this VPN. Note that we cannot just look at the routes
4180 * for the VNI's RD -
4181 * remote routes applicable for this VNI could have any RD.
4183 /* EVPN routes are a 2-level table. */
4184 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
4185 rd_rn
= bgp_route_next(rd_rn
)) {
4186 table
= (struct bgp_table
*)(rd_rn
->info
);
4190 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4192 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
4194 /* Consider "valid" remote routes applicable for
4196 if (!(ri
->type
== ZEBRA_ROUTE_BGP
4197 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
4200 if (bgp_nexthop_self(bgp
, ri
->attr
->nexthop
)) {
4202 char attr_str
[BUFSIZ
];
4203 char pbuf
[PREFIX_STRLEN
];
4205 bgp_dump_attr(ri
->attr
, attr_str
,
4208 if (bgp_debug_update(ri
->peer
, &rn
->p
,
4211 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
4218 bgp_evpn_unimport_route(bgp
, afi
, safi
,
4221 bgp_rib_remove(rn
, ri
, ri
->peer
, afi
,
4232 * Handle del of a local MACIP.
4234 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
4237 struct bgpevpn
*vpn
;
4238 struct prefix_evpn p
;
4240 if (!bgp
->vnihash
) {
4241 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4245 /* Lookup VNI hash - should exist. */
4246 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4247 if (!vpn
|| !is_vni_live(vpn
)) {
4248 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP DEL",
4249 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
4253 /* Remove EVPN type-2 route and schedule for processing. */
4254 build_evpn_type2_prefix(&p
, mac
, ip
);
4255 delete_evpn_route(bgp
, vpn
, &p
);
4261 * Handle add of a local MACIP.
4263 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
4264 struct ipaddr
*ip
, uint8_t flags
)
4266 struct bgpevpn
*vpn
;
4267 struct prefix_evpn p
;
4269 if (!bgp
->vnihash
) {
4270 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4274 /* Lookup VNI hash - should exist. */
4275 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4276 if (!vpn
|| !is_vni_live(vpn
)) {
4277 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP ADD",
4278 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
4282 /* Create EVPN type-2 route and schedule for processing. */
4283 build_evpn_type2_prefix(&p
, mac
, ip
);
4284 if (update_evpn_route(bgp
, vpn
, &p
, flags
)) {
4285 char buf
[ETHER_ADDR_STRLEN
];
4286 char buf2
[INET6_ADDRSTRLEN
];
4289 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
4290 bgp
->vrf_id
, vpn
->vni
,
4291 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
4294 prefix_mac2str(mac
, buf
, sizeof(buf
)),
4295 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
4302 static void link_l2vni_hash_to_l3vni(struct hash_backet
*backet
,
4303 struct bgp
*bgp_vrf
)
4305 struct bgpevpn
*vpn
= NULL
;
4306 struct bgp
*bgp_def
= NULL
;
4308 bgp_def
= bgp_get_default();
4311 vpn
= (struct bgpevpn
*)backet
->data
;
4312 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
4313 bgpevpn_link_to_l3vni(vpn
);
4316 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
, struct ethaddr
*rmac
,
4317 struct in_addr originator_ip
, int filter
)
4319 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
4320 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
4321 struct listnode
*node
= NULL
;
4322 struct bgpevpn
*vpn
= NULL
;
4325 /* get the default instamce - required to get the AS number for VRF
4328 bgp_def
= bgp_get_default();
4331 "Cannot process L3VNI %u ADD - default BGP instance not yet created",
4337 /* if the BGP vrf instance doesnt exist - create one */
4338 bgp_vrf
= bgp_lookup_by_name(vrf_id_to_name(vrf_id
));
4343 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
4344 BGP_INSTANCE_TYPE_VRF
);
4346 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
:
4347 zlog_err("'bgp multiple-instance' not present\n");
4349 case BGP_ERR_AS_MISMATCH
:
4350 zlog_err("BGP is already running; AS is %u\n", as
);
4352 case BGP_ERR_INSTANCE_MISMATCH
:
4353 zlog_err("BGP instance name and AS number mismatch\n");
4357 /* mark as auto created */
4358 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
4361 /* associate with l3vni */
4362 bgp_vrf
->l3vni
= l3vni
;
4364 /* set the router mac - to be used in mac-ip routes for this vrf */
4365 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
4367 /* set the originator ip */
4368 bgp_vrf
->originator_ip
= originator_ip
;
4370 /* set the right filter - are we using l3vni only for prefix routes? */
4372 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
4374 /* auto derive RD/RT */
4375 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
4376 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4377 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
4378 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4379 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
4381 /* link all corresponding l2vnis */
4382 hash_iterate(bgp_def
->vnihash
,
4383 (void (*)(struct hash_backet
*,
4384 void *))link_l2vni_hash_to_l3vni
,
4387 /* Only update all corresponding type-2 routes if we are advertising two
4388 * labels along with type-2 routes
4391 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4392 update_routes_for_vni(bgp_def
, vpn
);
4394 /* advertise type-5 routes if needed */
4395 update_advertise_vrf_routes(bgp_vrf
);
4397 /* install all remote routes belonging to this l3vni into correspondng
4399 install_routes_for_vrf(bgp_vrf
);
4404 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
4406 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
4407 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
4408 struct listnode
*node
= NULL
;
4409 struct listnode
*next
= NULL
;
4410 struct bgpevpn
*vpn
= NULL
;
4412 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
4415 "Cannot process L3VNI %u Del - Could not find BGP instance",
4420 bgp_def
= bgp_get_default();
4423 "Cannot process L3VNI %u Del - Could not find default BGP instance",
4428 /* unimport remote routes from VRF, if it is AUTO vrf bgp_delete will
4429 * take care of uninstalling the routes from zebra
4431 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
4432 uninstall_routes_for_vrf(bgp_vrf
);
4434 /* delete/withdraw all type-5 routes */
4435 delete_withdraw_vrf_routes(bgp_vrf
);
4437 /* remove the l3vni from vrf instance */
4440 /* remove the Rmac from the BGP vrf */
4441 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
4444 if (bgp_vrf
->vrf_import_rtl
&& !list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4445 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4446 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
4448 if (bgp_vrf
->vrf_export_rtl
&& !list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4449 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
4452 /* update all corresponding local mac-ip routes */
4453 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
4454 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
4455 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
4456 update_routes_for_vni(bgp_def
, vpn
);
4460 /* If any L2VNIs point to this instance, unlink them. */
4461 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
4462 bgpevpn_unlink_from_l3vni(vpn
);
4464 /* Delete the instance if it was autocreated */
4465 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
4466 bgp_delete(bgp_vrf
);
4472 * Handle del of a local VNI.
4474 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
4476 struct bgpevpn
*vpn
;
4478 if (!bgp
->vnihash
) {
4479 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4483 /* Locate VNI hash */
4484 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4486 if (bgp_debug_zebra(NULL
))
4488 "%u: VNI hash entry for VNI %u not found at DEL",
4493 /* Remove all local EVPN routes and schedule for processing (to
4494 * withdraw from peers).
4496 delete_routes_for_vni(bgp
, vpn
);
4499 * tunnel is no longer active, del tunnel ip address from tip_hash
4501 bgp_tip_del(bgp
, &vpn
->originator_ip
);
4503 /* Clear "live" flag and see if hash needs to be freed. */
4504 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4505 if (!is_vni_configured(vpn
))
4506 bgp_evpn_free(bgp
, vpn
);
4512 * Handle add (or update) of a local VNI. The VNI changes we care
4513 * about are for the local-tunnel-ip and the (tenant) VRF.
4515 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
4516 struct in_addr originator_ip
, vrf_id_t tenant_vrf_id
)
4518 struct bgpevpn
*vpn
;
4519 struct prefix_evpn p
;
4521 if (!bgp
->vnihash
) {
4522 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4526 /* Lookup VNI. If present and no change, exit. */
4527 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4530 if (is_vni_live(vpn
)
4531 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
4532 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
4533 /* Probably some other param has changed that we don't
4537 /* Update tenant_vrf_id if it has changed. */
4538 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
4539 bgpevpn_unlink_from_l3vni(vpn
);
4540 vpn
->tenant_vrf_id
= tenant_vrf_id
;
4541 bgpevpn_link_to_l3vni(vpn
);
4544 /* If tunnel endpoint IP has changed, update (and delete prior
4545 * type-3 route, if needed.)
4547 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
4548 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
4550 /* Update all routes with new endpoint IP and/or export RT
4553 if (is_vni_live(vpn
))
4554 update_routes_for_vni(bgp
, vpn
);
4557 /* Create or update as appropriate. */
4559 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
);
4562 "%u: Failed to allocate VNI entry for VNI %u - at Add",
4568 /* if the VNI is live already, there is nothing more to do */
4569 if (is_vni_live(vpn
))
4572 /* Mark as "live" */
4573 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4575 /* tunnel is now active, add tunnel-ip to db */
4576 bgp_tip_add(bgp
, &originator_ip
);
4578 /* filter routes as nexthop database has changed */
4579 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
4581 /* Create EVPN type-3 route and schedule for processing. */
4582 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
4583 if (update_evpn_route(bgp
, vpn
, &p
, 0)) {
4584 zlog_err("%u: Type3 route creation failure for VNI %u",
4589 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
4593 install_routes_for_vni(bgp
, vpn
);
4595 /* If we are advertising gateway mac-ip
4596 It needs to be conveyed again to zebra */
4597 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
4603 * Cleanup EVPN information on disable - Need to delete and withdraw
4604 * EVPN routes from peers.
4606 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
4608 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
4609 void *))cleanup_vni_on_disable
,
4614 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
4615 * BGP instance (default) is being freed.
4617 void bgp_evpn_cleanup(struct bgp
*bgp
)
4620 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
4621 void *))free_vni_entry
,
4623 if (bgp
->import_rt_hash
)
4624 hash_free(bgp
->import_rt_hash
);
4625 bgp
->import_rt_hash
= NULL
;
4626 if (bgp
->vrf_import_rt_hash
)
4627 hash_free(bgp
->vrf_import_rt_hash
);
4628 bgp
->vrf_import_rt_hash
= NULL
;
4630 hash_free(bgp
->vnihash
);
4631 bgp
->vnihash
= NULL
;
4632 if (bgp
->vrf_import_rtl
)
4633 list_delete_and_null(&bgp
->vrf_import_rtl
);
4634 if (bgp
->vrf_export_rtl
)
4635 list_delete_and_null(&bgp
->vrf_export_rtl
);
4637 list_delete_and_null(&bgp
->l2vnis
);
4641 * Initialization for EVPN
4644 * hash for RT to VNI
4646 void bgp_evpn_init(struct bgp
*bgp
)
4649 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
4650 bgp
->import_rt_hash
=
4651 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
4652 "BGP Import RT Hash");
4653 bgp
->vrf_import_rt_hash
=
4654 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
4655 "BGP VRF Import RT Hash");
4656 bgp
->vrf_import_rtl
= list_new();
4657 bgp
->vrf_import_rtl
->cmp
=
4658 (int (*)(void *, void *))evpn_route_target_cmp
;
4659 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
4660 bgp
->vrf_export_rtl
= list_new();
4661 bgp
->vrf_export_rtl
->cmp
=
4662 (int (*)(void *, void *))evpn_route_target_cmp
;
4663 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
4664 bgp
->l2vnis
= list_new();
4665 bgp
->l2vnis
->cmp
= (int (*)(void *, void *))vni_hash_cmp
;
4668 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
4670 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);