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 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
272 * Function to lookup Import RT node - used to map a RT to set of
273 * VNIs importing routes with that RT.
275 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
276 struct ecommunity_val
*rt
)
278 struct irt_node
*irt
;
281 memset(&tmp
, 0, sizeof(struct irt_node
));
282 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
283 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
288 * Is specified VNI present on the RT's list of "importing" VNIs?
290 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
292 struct listnode
*node
, *nnode
;
293 struct bgpevpn
*tmp_vpn
;
295 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
304 * Compare Route Targets.
306 static int evpn_route_target_cmp(struct ecommunity
*ecom1
,
307 struct ecommunity
*ecom2
)
315 if (!ecom1
&& !ecom2
)
318 if (ecom1
->str
&& !ecom2
->str
)
321 if (!ecom1
->str
&& ecom2
->str
)
324 if (!ecom1
->str
&& !ecom2
->str
)
327 return strcmp(ecom1
->str
, ecom2
->str
);
331 * Mask off global-admin field of specified extended community (RT),
332 * just retain the local-admin field.
334 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
335 struct ecommunity_val
*src
)
341 if (type
== ECOMMUNITY_ENCODE_AS
) {
342 dst
->val
[2] = dst
->val
[3] = 0;
343 } else if (type
== ECOMMUNITY_ENCODE_AS4
344 || type
== ECOMMUNITY_ENCODE_IP
) {
345 dst
->val
[2] = dst
->val
[3] = 0;
346 dst
->val
[4] = dst
->val
[5] = 0;
351 * Map one RT to specified VRF.
352 * bgp_vrf = BGP vrf instance
354 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct ecommunity_val
*eval
)
356 struct vrf_irt_node
*irt
= NULL
;
357 struct ecommunity_val eval_tmp
;
359 /* If using "automatic" RT,
360 * we only care about the local-admin sub-field.
361 * This is to facilitate using L3VNI(VRF-VNI)
362 * as the RT for EBGP peering too.
364 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
365 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
366 mask_ecom_global_admin(&eval_tmp
, eval
);
368 irt
= lookup_vrf_import_rt(&eval_tmp
);
369 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
370 /* Already mapped. */
374 irt
= vrf_import_rt_new(&eval_tmp
);
376 /* Add VRF to the list for this RT. */
377 listnode_add(irt
->vrfs
, bgp_vrf
);
381 * Unmap specified VRF from specified RT. If there are no other
382 * VRFs for this RT, then the RT hash is deleted.
383 * bgp_vrf: BGP VRF specific instance
385 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
, struct vrf_irt_node
*irt
)
387 /* Delete VRF from list for this RT. */
388 listnode_delete(irt
->vrfs
, bgp_vrf
);
389 if (!listnode_head(irt
->vrfs
)) {
390 list_delete_and_null(&irt
->vrfs
);
391 vrf_import_rt_free(irt
);
396 * Map one RT to specified VNI.
398 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
399 struct ecommunity_val
*eval
)
401 struct irt_node
*irt
;
402 struct ecommunity_val eval_tmp
;
404 /* If using "automatic" RT, we only care about the local-admin
406 * This is to facilitate using VNI as the RT for EBGP peering too.
408 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
409 if (!is_import_rt_configured(vpn
))
410 mask_ecom_global_admin(&eval_tmp
, eval
);
412 irt
= lookup_import_rt(bgp
, &eval_tmp
);
413 if (irt
&& irt
->vnis
)
414 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
415 /* Already mapped. */
419 irt
= import_rt_new(bgp
, &eval_tmp
);
423 /* Add VNI to the hash list for this RT. */
424 listnode_add(irt
->vnis
, vpn
);
428 * Unmap specified VNI from specified RT. If there are no other
429 * VNIs for this RT, then the RT hash is deleted.
431 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
432 struct irt_node
*irt
)
434 /* Delete VNI from hash list for this RT. */
435 listnode_delete(irt
->vnis
, vpn
);
436 if (!listnode_head(irt
->vnis
)) {
437 list_delete_and_null(&irt
->vnis
);
438 import_rt_free(bgp
, irt
);
443 * Create RT extended community automatically from passed information:
444 * of the form AS:VNI.
445 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
446 * the need is to get a RT value that will be unique across different
447 * VNIs but the same across routers (in the same AS) for a particular
450 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
452 struct ecommunity_val eval
;
453 struct ecommunity
*ecomadd
;
455 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
457 ecomadd
= ecommunity_new();
458 ecommunity_add_val(ecomadd
, &eval
);
459 listnode_add_sort(rtl
, ecomadd
);
463 * Derive RD and RT for a VNI automatically. Invoked at the time of
466 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
468 bgp_evpn_derive_auto_rd(bgp
, vpn
);
469 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
470 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
474 * Convert nexthop (remote VTEP IP) into an IPv6 address.
476 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
478 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
480 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
481 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
485 * Add (update) or delete MACIP from zebra.
487 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
488 struct prefix_evpn
*p
,
489 struct in_addr remote_vtep_ip
, int add
,
494 char buf1
[ETHER_ADDR_STRLEN
];
495 char buf2
[INET6_ADDRSTRLEN
];
496 char buf3
[INET6_ADDRSTRLEN
];
499 if (!zclient
|| zclient
->sock
< 0)
502 /* Don't try to register if Zebra doesn't know of this instance. */
503 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
509 zclient_create_header(
510 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
512 stream_putl(s
, vpn
->vni
);
513 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
514 /* IP address length and IP address, if any. */
515 if (is_evpn_prefix_ipaddr_none(p
))
518 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
520 stream_putl(s
, ipa_len
);
521 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
523 stream_put_in_addr(s
, &remote_vtep_ip
);
525 /* TX flags - MAC sticky status and/or gateway mac */
527 stream_putc(s
, flags
);
529 stream_putw_at(s
, 0, stream_get_endp(s
));
531 if (bgp_debug_zebra(NULL
))
533 "Tx %s MACIP, VNI %u MAC %s IP %s (flags: 0x%x) remote VTEP %s",
534 add
? "ADD" : "DEL", vpn
->vni
,
535 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
537 ipaddr2str(&p
->prefix
.macip_addr
.ip
,
538 buf3
, sizeof(buf3
)), flags
,
539 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
542 return zclient_send_message(zclient
);
546 * Add (update) or delete remote VTEP from zebra.
548 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
549 struct prefix_evpn
*p
, int add
)
554 if (!zclient
|| zclient
->sock
< 0)
557 /* Don't try to register if Zebra doesn't know of this instance. */
558 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
564 zclient_create_header(
565 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
567 stream_putl(s
, vpn
->vni
);
568 if (is_evpn_prefix_ipaddr_v4(p
))
569 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
570 else if (is_evpn_prefix_ipaddr_v6(p
)) {
572 "Bad remote IP when trying to %s remote VTEP for VNI %u",
573 add
? "ADD" : "DEL", vpn
->vni
);
577 stream_putw_at(s
, 0, stream_get_endp(s
));
579 if (bgp_debug_zebra(NULL
))
580 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
581 add
? "ADD" : "DEL", vpn
->vni
,
582 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
584 return zclient_send_message(zclient
);
588 * Build extended communities for EVPN prefix route.
590 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
593 struct ecommunity ecom_encap
;
594 struct ecommunity ecom_rmac
;
595 struct ecommunity_val eval
;
596 struct ecommunity_val eval_rmac
;
597 bgp_encap_types tnl_type
;
598 struct listnode
*node
, *nnode
;
599 struct ecommunity
*ecom
;
600 struct list
*vrf_export_rtl
= NULL
;
603 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
604 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
605 encode_encap_extcomm(tnl_type
, &eval
);
607 ecom_encap
.val
= (uint8_t *)eval
.val
;
610 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
612 /* Add the export RTs for L3VNI/VRF */
613 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
614 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
615 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
617 ecommunity_merge(attr
->ecommunity
, ecom
);
620 /* add the router mac extended community */
621 if (!is_zero_mac(&attr
->rmac
)) {
622 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
623 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
625 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
627 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
630 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
634 * Build extended communities for EVPN route.
635 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
636 * and ENCAP extended communities are applicable for all routes.
637 * The default gateway extended community and MAC mobility (sticky) extended
638 * community are added as needed based on passed settings - only for type-2
639 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
640 * added, if present, based on passed settings - only for non-link-local
643 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
646 struct ecommunity ecom_encap
;
647 struct ecommunity ecom_sticky
;
648 struct ecommunity ecom_default_gw
;
649 struct ecommunity ecom_rmac
;
650 struct ecommunity_val eval
;
651 struct ecommunity_val eval_sticky
;
652 struct ecommunity_val eval_default_gw
;
653 struct ecommunity_val eval_rmac
;
654 bgp_encap_types tnl_type
;
655 struct listnode
*node
, *nnode
;
656 struct ecommunity
*ecom
;
658 struct list
*vrf_export_rtl
= NULL
;
661 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
662 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
663 encode_encap_extcomm(tnl_type
, &eval
);
665 ecom_encap
.val
= (uint8_t *)eval
.val
;
668 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
670 /* Add the export RTs for L2VNI */
671 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
672 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
674 /* Add the export RTs for L3VNI if told to - caller determines
675 * when this should be done.
678 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
679 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
680 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
682 attr
->ecommunity
= ecommunity_merge(
683 attr
->ecommunity
, ecom
);
687 /* Add MAC mobility (sticky) if needed. */
690 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
691 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
692 ecom_sticky
.size
= 1;
693 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
695 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
698 /* Add RMAC, if told to. */
700 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
701 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
703 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
705 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
708 /* Add default gateway, if needed. */
709 if (attr
->default_gw
) {
710 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
711 encode_default_gw_extcomm(&eval_default_gw
);
712 ecom_default_gw
.size
= 1;
713 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
715 ecommunity_merge(attr
->ecommunity
, &ecom_default_gw
);
718 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
722 * Add MAC mobility extended community to attribute.
724 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
726 struct ecommunity ecom_tmp
;
727 struct ecommunity_val eval
;
728 uint8_t *ecom_val_ptr
;
735 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
737 /* Find current MM ecommunity */
740 if (attr
->ecommunity
) {
741 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
742 pnt
= attr
->ecommunity
->val
+ (i
* 8);
746 if (type
== ECOMMUNITY_ENCODE_EVPN
748 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
749 ecom_val_ptr
= (uint8_t *)(attr
->ecommunity
->val
756 /* Update the existing MM ecommunity */
758 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
760 /* Add MM to existing */
762 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
764 ecom_tmp
.val
= (uint8_t *)eval
.val
;
767 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
771 /* Install EVPN route into zebra. */
772 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
773 struct prefix_evpn
*p
,
774 struct in_addr remote_vtep_ip
, uint8_t flags
)
778 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
779 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
782 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 1);
787 /* Uninstall EVPN route from zebra. */
788 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
789 struct prefix_evpn
*p
,
790 struct in_addr remote_vtep_ip
)
794 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
795 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
798 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 0);
804 * Due to MAC mobility, the prior "local" best route has been supplanted
805 * by a "remote" best route. The prior route has to be deleted and withdrawn
808 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
810 struct bgp_info
*old_local
)
812 struct bgp_node
*global_rn
;
814 afi_t afi
= AFI_L2VPN
;
815 safi_t safi
= SAFI_EVPN
;
817 /* Locate route node in the global EVPN routing table. Note that
818 * this table is a 2-level tree (RD-level + Prefix-level) similar to
821 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
822 (struct prefix
*)&rn
->p
, &vpn
->prd
);
824 /* Delete route entry in the global EVPN table. */
825 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
827 /* Schedule for processing - withdraws to peers happen from
831 bgp_process(bgp
, global_rn
, afi
, safi
);
832 bgp_unlock_node(global_rn
);
835 /* Delete route entry in the VNI route table, caller to remove. */
836 bgp_info_delete(rn
, old_local
);
840 * Calculate the best path for an EVPN route. Install/update best path in zebra,
843 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
846 struct bgp_info
*old_select
, *new_select
;
847 struct bgp_info_pair old_and_new
;
848 afi_t afi
= AFI_L2VPN
;
849 safi_t safi
= SAFI_EVPN
;
853 /* Compute the best path. */
854 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
856 old_select
= old_and_new
.old
;
857 new_select
= old_and_new
.new;
859 /* If the best path hasn't changed - see if there is still something to
863 if (old_select
&& old_select
== new_select
864 && old_select
->type
== ZEBRA_ROUTE_BGP
865 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
866 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
867 && !CHECK_FLAG(old_select
->flags
, BGP_INFO_ATTR_CHANGED
)
868 && !bgp
->addpath_tx_used
[afi
][safi
]) {
869 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
870 if (old_select
->attr
->sticky
)
871 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
872 if (old_select
->attr
->default_gw
)
873 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
874 ret
= evpn_zebra_install(
875 bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
876 old_select
->attr
->nexthop
, flags
);
878 UNSET_FLAG(old_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
879 bgp_zebra_clear_route_change_flags(rn
);
883 /* If the user did a "clear" this flag will be set */
884 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
886 /* bestpath has changed; update relevant fields and install or uninstall
887 * into the zebra RIB.
889 if (old_select
|| new_select
)
890 bgp_bump_version(rn
);
893 bgp_info_unset_flag(rn
, old_select
, BGP_INFO_SELECTED
);
895 bgp_info_set_flag(rn
, new_select
, BGP_INFO_SELECTED
);
896 bgp_info_unset_flag(rn
, new_select
, BGP_INFO_ATTR_CHANGED
);
897 UNSET_FLAG(new_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
900 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
901 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
903 if (new_select
->attr
->sticky
)
904 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
905 if (new_select
->attr
->default_gw
)
906 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
907 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
908 new_select
->attr
->nexthop
, flags
);
909 /* If an old best existed and it was a "local" route, the only
911 * it would be supplanted is due to MAC mobility procedures. So,
913 * need to do an implicit delete and withdraw that route from
916 if (old_select
&& old_select
->peer
== bgp
->peer_self
917 && old_select
->type
== ZEBRA_ROUTE_BGP
918 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
919 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
921 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
922 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
923 ret
= evpn_zebra_uninstall(bgp
, vpn
,
924 (struct prefix_evpn
*)&rn
->p
,
925 old_select
->attr
->nexthop
);
928 /* Clear any route change flags. */
929 bgp_zebra_clear_route_change_flags(rn
);
931 /* Reap old select bgp_info, if it has been removed */
932 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_INFO_REMOVED
))
933 bgp_info_reap(rn
, old_select
);
939 * Return true if the local ri for this rn is of type gateway mac
941 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
943 struct bgp_info
*tmp_ri
= NULL
;
944 struct bgp_info
*local_ri
= NULL
;
947 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
948 if (tmp_ri
->peer
== bgp
->peer_self
949 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
950 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
957 return local_ri
->attr
->default_gw
;
962 * Return true if the local ri for this rn has sticky set
964 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
966 struct bgp_info
*tmp_ri
;
967 struct bgp_info
*local_ri
;
970 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
971 if (tmp_ri
->peer
== bgp
->peer_self
972 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
973 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
980 return local_ri
->attr
->sticky
;
983 static int update_evpn_type5_route_entry(struct bgp
*bgp_def
,
984 struct bgp
*bgp_vrf
, afi_t afi
,
985 safi_t safi
, struct bgp_node
*rn
,
986 struct attr
*attr
, int *route_changed
)
988 struct attr
*attr_new
= NULL
;
989 struct bgp_info
*ri
= NULL
;
990 mpls_label_t label
= MPLS_INVALID_LABEL
;
991 struct bgp_info
*local_ri
= NULL
;
992 struct bgp_info
*tmp_ri
= NULL
;
995 /* locate the local route entry if any */
996 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
997 if (tmp_ri
->peer
== bgp_def
->peer_self
998 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
999 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1003 /* create a new route entry if one doesnt exist.
1004 Otherwise see if route attr has changed
1008 /* route has changed as this is the first entry */
1011 /* Add (or update) attribute to hash. */
1012 attr_new
= bgp_attr_intern(attr
);
1014 /* create the route info from attribute */
1015 ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1016 bgp_def
->peer_self
, attr_new
, rn
);
1017 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1019 /* Type-5 routes advertise the L3-VNI */
1020 bgp_info_extra_get(ri
);
1021 vni2label(bgp_vrf
->l3vni
, &label
);
1022 memcpy(&ri
->extra
->label
, &label
, sizeof(label
));
1023 ri
->extra
->num_labels
= 1;
1025 /* add the route entry to route node*/
1026 bgp_info_add(rn
, ri
);
1030 if (!attrhash_cmp(tmp_ri
->attr
, attr
)) {
1032 /* attribute changed */
1035 /* The attribute has changed. */
1036 /* Add (or update) attribute to hash. */
1037 attr_new
= bgp_attr_intern(attr
);
1038 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1040 /* Restore route, if needed. */
1041 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1042 bgp_info_restore(rn
, tmp_ri
);
1044 /* Unintern existing, set to new. */
1045 bgp_attr_unintern(&tmp_ri
->attr
);
1046 tmp_ri
->attr
= attr_new
;
1047 tmp_ri
->uptime
= bgp_clock();
1053 /* update evpn type-5 route entry */
1054 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1055 struct attr
*src_attr
)
1057 afi_t afi
= AFI_L2VPN
;
1058 safi_t safi
= SAFI_EVPN
;
1060 struct bgp_node
*rn
= NULL
;
1061 struct bgp
*bgp_def
= NULL
;
1062 int route_changed
= 0;
1064 bgp_def
= bgp_get_default();
1068 /* Build path attribute for this route - use the source attr, if
1069 * present, else treat as locally originated.
1072 bgp_attr_dup(&attr
, src_attr
);
1074 memset(&attr
, 0, sizeof(struct attr
));
1075 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1077 /* Set nexthop to ourselves and fill in the Router MAC. */
1078 attr
.nexthop
= bgp_vrf
->originator_ip
;
1079 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1080 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1081 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
1083 /* Setup RT and encap extended community */
1084 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1086 /* get the route node in global table */
1087 rn
= bgp_afi_node_get(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1088 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1091 /* create or update the route entry within the route node */
1092 update_evpn_type5_route_entry(bgp_def
, bgp_vrf
, afi
, safi
, rn
, &attr
,
1095 /* schedule for processing and unlock node */
1096 if (route_changed
) {
1097 bgp_process(bgp_def
, rn
, afi
, safi
);
1098 bgp_unlock_node(rn
);
1101 /* uninten temporary */
1103 aspath_unintern(&attr
.aspath
);
1108 * Create or update EVPN route entry. This could be in the VNI route table
1109 * or the global route table.
1111 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1112 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1113 struct attr
*attr
, int add
, int vni_table
,
1114 struct bgp_info
**ri
, uint8_t flags
)
1116 struct bgp_info
*tmp_ri
;
1117 struct bgp_info
*local_ri
, *remote_ri
;
1118 struct attr
*attr_new
;
1119 mpls_label_t label
[BGP_MAX_LABELS
];
1120 uint32_t num_labels
= 1;
1121 int route_change
= 1;
1123 struct prefix_evpn
*evp
;
1126 evp
= (struct prefix_evpn
*)&rn
->p
;
1127 memset(&label
, 0, sizeof(label
));
1129 /* See if this is an update of an existing route, or a new add. Also,
1130 * identify if already known from remote, and if so, the one with the
1131 * highest sequence number; this is only when adding to the VNI routing
1134 local_ri
= remote_ri
= NULL
;
1135 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1136 if (tmp_ri
->peer
== bgp
->peer_self
1137 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1138 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1141 if (tmp_ri
->type
== ZEBRA_ROUTE_BGP
1142 && tmp_ri
->sub_type
== BGP_ROUTE_IMPORTED
1143 && CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
)) {
1146 else if (mac_mobility_seqnum(tmp_ri
->attr
)
1147 > mac_mobility_seqnum(remote_ri
->attr
))
1153 /* If route doesn't exist already, create a new one, if told to.
1154 * Otherwise act based on whether the attributes of the route have
1157 if (!local_ri
&& !add
)
1161 /* When learnt locally for the first time but already known from
1162 * remote, we have to initiate appropriate MAC mobility steps.
1164 * is applicable when updating the VNI routing table.
1165 * We need to skip mobility steps for g/w macs (local mac on g/w
1166 * SVI) advertised in EVPN.
1167 * This will ensure that local routes are preferred for g/w macs
1169 if (remote_ri
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
)) {
1170 uint32_t cur_seqnum
;
1172 /* Add MM extended community to route. */
1173 cur_seqnum
= mac_mobility_seqnum(remote_ri
->attr
);
1174 add_mac_mobility_to_attr(cur_seqnum
+ 1, attr
);
1177 /* Add (or update) attribute to hash. */
1178 attr_new
= bgp_attr_intern(attr
);
1180 /* Extract MAC mobility sequence number, if any. */
1181 attr_new
->mm_seqnum
=
1182 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1183 attr_new
->sticky
= sticky
;
1185 /* Create new route with its attribute. */
1186 tmp_ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1187 bgp
->peer_self
, attr_new
, rn
);
1188 SET_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
);
1189 bgp_info_extra_get(tmp_ri
);
1191 /* The VNI goes into the 'label' field of the route */
1192 vni2label(vpn
->vni
, &label
[0]);
1194 /* Type-2 routes may carry a second VNI - the L3-VNI.
1195 * Only attach second label if we are advertising two labels for
1198 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1199 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1202 l3vni
= bgpevpn_get_l3vni(vpn
);
1204 vni2label(l3vni
, &label
[1]);
1209 memcpy(&tmp_ri
->extra
->label
, label
, sizeof(label
));
1210 tmp_ri
->extra
->num_labels
= num_labels
;
1211 bgp_info_add(rn
, tmp_ri
);
1214 if (attrhash_cmp(tmp_ri
->attr
, attr
)
1215 && !CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1219 * The attributes have changed, type-2 routes needs to
1220 * be advertised with right labels.
1222 vni2label(vpn
->vni
, &label
[0]);
1223 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1224 && CHECK_FLAG(vpn
->flags
,
1225 VNI_FLAG_USE_TWO_LABELS
)) {
1228 l3vni
= bgpevpn_get_l3vni(vpn
);
1230 vni2label(l3vni
, &label
[1]);
1234 memcpy(&tmp_ri
->extra
->label
, label
, sizeof(label
));
1235 tmp_ri
->extra
->num_labels
= num_labels
;
1237 /* The attribute has changed. */
1238 /* Add (or update) attribute to hash. */
1239 attr_new
= bgp_attr_intern(attr
);
1240 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1242 /* Restore route, if needed. */
1243 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1244 bgp_info_restore(rn
, tmp_ri
);
1246 /* Unintern existing, set to new. */
1247 bgp_attr_unintern(&tmp_ri
->attr
);
1248 tmp_ri
->attr
= attr_new
;
1249 tmp_ri
->uptime
= bgp_clock();
1253 /* Return back the route entry. */
1255 return route_change
;
1259 * Create or update EVPN route (of type based on prefix) for specified VNI
1260 * and schedule for processing.
1262 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1263 struct prefix_evpn
*p
, uint8_t flags
)
1265 struct bgp_node
*rn
;
1267 struct attr
*attr_new
;
1268 int add_l3_ecomm
= 0;
1269 struct bgp_info
*ri
;
1270 afi_t afi
= AFI_L2VPN
;
1271 safi_t safi
= SAFI_EVPN
;
1274 memset(&attr
, 0, sizeof(struct attr
));
1276 /* Build path-attribute for this route. */
1277 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1278 attr
.nexthop
= vpn
->originator_ip
;
1279 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1280 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1281 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1282 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1284 /* PMSI is only needed for type-3 routes */
1285 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
)
1286 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1288 /* router mac is only needed for type-2 routes here. */
1289 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1290 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1291 vni2label(vpn
->vni
, &(attr
.label
));
1293 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1294 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1297 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1298 (is_evpn_prefix_ipaddr_v4(p
) ||
1299 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1300 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
))
1303 /* Set up extended community. */
1304 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1306 /* First, create (or fetch) route node within the VNI. */
1307 /* NOTE: There is no RD here. */
1308 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1310 /* Create or update route entry. */
1311 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1314 attr_new
= ri
->attr
;
1316 /* Perform route selection; this is just to set the flags correctly
1317 * as local route in the VNI always wins.
1319 evpn_route_select_install(bgp
, vpn
, rn
);
1320 bgp_unlock_node(rn
);
1322 /* If this is a new route or some attribute has changed, export the
1323 * route to the global table. The route will be advertised to peers
1324 * from there. Note that this table is a 2-level tree (RD-level +
1325 * Prefix-level) similar to L3VPN routes.
1328 struct bgp_info
*global_ri
;
1330 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1331 (struct prefix
*)p
, &vpn
->prd
);
1332 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1, 0,
1335 /* Schedule for processing and unlock node. */
1336 bgp_process(bgp
, rn
, afi
, safi
);
1337 bgp_unlock_node(rn
);
1340 /* Unintern temporary. */
1341 aspath_unintern(&attr
.aspath
);
1346 /* Delete EVPN type5 route entry from global table */
1347 static void delete_evpn_type5_route_entry(struct bgp
*bgp_def
,
1348 struct bgp
*bgp_vrf
, afi_t afi
,
1349 safi_t safi
, struct bgp_node
*rn
,
1350 struct bgp_info
**ri
)
1352 struct bgp_info
*tmp_ri
= NULL
;
1356 /* find the matching route entry */
1357 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
)
1358 if (tmp_ri
->peer
== bgp_def
->peer_self
1359 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1360 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1365 /* Mark route for delete. */
1367 bgp_info_delete(rn
, tmp_ri
);
1370 /* Delete EVPN type5 route */
1371 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1373 afi_t afi
= AFI_L2VPN
;
1374 safi_t safi
= SAFI_EVPN
;
1375 struct bgp_node
*rn
= NULL
;
1376 struct bgp_info
*ri
= NULL
;
1377 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
1379 bgp_def
= bgp_get_default();
1383 /* locate the global route entry for this type-5 prefix */
1384 rn
= bgp_afi_node_lookup(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1385 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1389 delete_evpn_type5_route_entry(bgp_def
, bgp_vrf
, afi
, safi
, rn
, &ri
);
1391 bgp_process(bgp_def
, rn
, afi
, safi
);
1392 bgp_unlock_node(rn
);
1397 * Delete EVPN route entry. This could be in the VNI route table
1398 * or the global route table.
1400 static void delete_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1401 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1402 struct bgp_info
**ri
)
1404 struct bgp_info
*tmp_ri
;
1408 /* Now, find matching route. */
1409 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
)
1410 if (tmp_ri
->peer
== bgp
->peer_self
1411 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1412 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1417 /* Mark route for delete. */
1419 bgp_info_delete(rn
, tmp_ri
);
1423 * Delete EVPN route (of type based on prefix) for specified VNI and
1424 * schedule for processing.
1426 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1427 struct prefix_evpn
*p
)
1429 struct bgp_node
*rn
, *global_rn
;
1430 struct bgp_info
*ri
;
1431 afi_t afi
= AFI_L2VPN
;
1432 safi_t safi
= SAFI_EVPN
;
1434 /* First, locate the route node within the VNI. If it doesn't exist,
1436 * is nothing further to do.
1438 /* NOTE: There is no RD here. */
1439 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1443 /* Next, locate route node in the global EVPN routing table. Note that
1444 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1447 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1448 (struct prefix
*)p
, &vpn
->prd
);
1450 /* Delete route entry in the global EVPN table. */
1451 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
1453 /* Schedule for processing - withdraws to peers happen from
1457 bgp_process(bgp
, global_rn
, afi
, safi
);
1458 bgp_unlock_node(global_rn
);
1461 /* Delete route entry in the VNI route table. This can just be removed.
1463 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1465 bgp_info_reap(rn
, ri
);
1466 bgp_unlock_node(rn
);
1472 * Update all type-2 (MACIP) local routes for this VNI - these should also
1473 * be scheduled for advertise to peers.
1475 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1479 struct bgp_node
*rn
;
1480 struct bgp_info
*ri
;
1482 struct attr attr_sticky
;
1483 struct attr attr_def_gw
;
1484 struct attr attr_ip6_ll
;
1485 struct attr
*attr_new
;
1486 int add_l3_ecomm
= 0;
1490 memset(&attr
, 0, sizeof(struct attr
));
1491 memset(&attr_sticky
, 0, sizeof(struct attr
));
1492 memset(&attr_def_gw
, 0, sizeof(struct attr
));
1493 memset(&attr_ip6_ll
, 0, sizeof(struct attr
));
1495 /* Build path-attribute - multiple type-2 routes for this VNI will share
1496 * the same path attribute, but we need separate structures for sticky
1497 * MACs, default gateway and IPv6 link-local addresses (no L3 RT/RMAC).
1499 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1500 bgp_attr_default_set(&attr_sticky
, BGP_ORIGIN_IGP
);
1501 bgp_attr_default_set(&attr_def_gw
, BGP_ORIGIN_IGP
);
1502 attr
.nexthop
= vpn
->originator_ip
;
1503 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1504 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1505 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1506 attr_sticky
.nexthop
= vpn
->originator_ip
;
1507 attr_sticky
.mp_nexthop_global_in
= vpn
->originator_ip
;
1508 attr_sticky
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1509 attr_sticky
.sticky
= 1;
1510 bgpevpn_get_rmac(vpn
, &attr_sticky
.rmac
);
1511 attr_def_gw
.nexthop
= vpn
->originator_ip
;
1512 attr_def_gw
.mp_nexthop_global_in
= vpn
->originator_ip
;
1513 attr_def_gw
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1514 attr_def_gw
.default_gw
= 1;
1515 bgpevpn_get_rmac(vpn
, &attr_def_gw
.rmac
);
1516 bgp_attr_default_set(&attr_ip6_ll
, BGP_ORIGIN_IGP
);
1517 attr_ip6_ll
.nexthop
= vpn
->originator_ip
;
1518 attr_ip6_ll
.mp_nexthop_global_in
= vpn
->originator_ip
;
1519 attr_ip6_ll
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1521 /* Add L3 VNI RTs and RMAC for non IPv6 link-local attributes if
1522 * using L3 VNI for type-2 routes also.
1524 if (CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
))
1527 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1528 build_evpn_route_extcomm(vpn
, &attr_sticky
, add_l3_ecomm
);
1529 build_evpn_route_extcomm(vpn
, &attr_def_gw
, add_l3_ecomm
);
1530 build_evpn_route_extcomm(vpn
, &attr_ip6_ll
, 0);
1532 /* Walk this VNI's route table and update local type-2 routes. For any
1533 * routes updated, update corresponding entry in the global table too.
1535 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1536 rn
= bgp_route_next(rn
)) {
1537 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1538 struct bgp_node
*rd_rn
;
1539 struct bgp_info
*global_ri
;
1541 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1544 if (is_evpn_prefix_ipaddr_v6(evp
) &&
1545 IN6_IS_ADDR_LINKLOCAL(&evp
->prefix
.macip_addr
.ip
.ipaddr_v6
))
1546 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1547 &attr_ip6_ll
, 0, 1, &ri
, 0);
1549 if (evpn_route_is_sticky(bgp
, rn
))
1550 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1551 &attr_sticky
, 0, 1, &ri
,
1553 else if (evpn_route_is_def_gw(bgp
, rn
))
1554 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1555 &attr_def_gw
, 0, 1, &ri
,
1558 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1559 &attr
, 0, 1, &ri
, 0);
1562 /* If a local route exists for this prefix, we need to update
1563 * the global routing table too.
1568 /* Perform route selection; this is just to set the flags
1570 * as local route in the VNI always wins.
1572 evpn_route_select_install(bgp
, vpn
, rn
);
1574 attr_new
= ri
->attr
;
1576 /* Update route in global routing table. */
1577 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1578 (struct prefix
*)evp
, &vpn
->prd
);
1580 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
1583 /* Schedule for processing and unlock node. */
1584 bgp_process(bgp
, rd_rn
, afi
, safi
);
1585 bgp_unlock_node(rd_rn
);
1588 /* Unintern temporary. */
1589 aspath_unintern(&attr
.aspath
);
1590 aspath_unintern(&attr_sticky
.aspath
);
1591 aspath_unintern(&attr_def_gw
.aspath
);
1592 aspath_unintern(&attr_ip6_ll
.aspath
);
1598 * Delete all type-2 (MACIP) local routes for this VNI - only from the
1599 * global routing table. These are also scheduled for withdraw from peers.
1601 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1605 struct bgp_node
*rdrn
, *rn
;
1606 struct bgp_table
*table
;
1607 struct bgp_info
*ri
;
1612 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
1613 if (rdrn
&& rdrn
->info
) {
1614 table
= (struct bgp_table
*)rdrn
->info
;
1615 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
1616 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1618 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1621 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1623 bgp_process(bgp
, rn
, afi
, safi
);
1627 /* Unlock RD node. */
1629 bgp_unlock_node(rdrn
);
1635 * Delete all type-2 (MACIP) local routes for this VNI - from the global
1636 * table as well as the per-VNI route table.
1638 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1642 struct bgp_node
*rn
;
1643 struct bgp_info
*ri
;
1648 /* First, walk the global route table for this VNI's type-2 local
1650 * EVPN routes are a 2-level table, first get the RD table.
1652 delete_global_type2_routes(bgp
, vpn
);
1654 /* Next, walk this VNI's route table and delete local type-2 routes. */
1655 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1656 rn
= bgp_route_next(rn
)) {
1657 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1659 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1662 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1664 /* Route entry in local table gets deleted immediately. */
1666 bgp_info_reap(rn
, ri
);
1673 * Delete all routes in the per-VNI route table.
1675 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1677 struct bgp_node
*rn
;
1678 struct bgp_info
*ri
, *nextri
;
1680 /* Walk this VNI's route table and delete all routes. */
1681 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1682 rn
= bgp_route_next(rn
)) {
1683 for (ri
= rn
->info
; (ri
!= NULL
) && (nextri
= ri
->next
, 1);
1685 bgp_info_delete(rn
, ri
);
1686 bgp_info_reap(rn
, ri
);
1694 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
1695 * export RT getting modified or change to tunnel IP. Note that these
1696 * situations need the route in the per-VNI table as well as the global
1697 * table to be updated (as attributes change).
1699 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1702 struct prefix_evpn p
;
1704 /* Update and advertise the type-3 route (only one) followed by the
1705 * locally learnt type-2 routes (MACIP) - for this VNI.
1707 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1708 ret
= update_evpn_route(bgp
, vpn
, &p
, 0);
1712 return update_all_type2_routes(bgp
, vpn
);
1716 * Delete (and withdraw) local routes for specified VNI from the global
1717 * table and per-VNI table. After this, remove all other routes from
1718 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
1719 * (advertise-all-vni) being disabled.
1721 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1724 struct prefix_evpn p
;
1726 /* Delete and withdraw locally learnt type-2 routes (MACIP)
1727 * followed by type-3 routes (only one) - for this VNI.
1729 ret
= delete_all_type2_routes(bgp
, vpn
);
1733 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1734 ret
= delete_evpn_route(bgp
, vpn
, &p
);
1738 /* Delete all routes from the per-VNI table. */
1739 return delete_all_vni_routes(bgp
, vpn
);
1743 * There is a tunnel endpoint IP address change for this VNI, delete
1744 * prior type-3 route (if needed) and update.
1745 * Note: Route re-advertisement happens elsewhere after other processing
1748 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1749 struct in_addr originator_ip
)
1751 struct prefix_evpn p
;
1753 /* If VNI is not live, we only need to update the originator ip */
1754 if (!is_vni_live(vpn
)) {
1755 vpn
->originator_ip
= originator_ip
;
1759 /* Update the tunnel-ip hash */
1760 bgp_tip_del(bgp
, &vpn
->originator_ip
);
1761 bgp_tip_add(bgp
, &originator_ip
);
1763 /* filter routes as martian nexthop db has changed */
1764 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
1766 /* Need to withdraw type-3 route as the originator IP is part
1769 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1770 delete_evpn_route(bgp
, vpn
, &p
);
1772 /* Update the tunnel IP and re-advertise all routes for this VNI. */
1773 vpn
->originator_ip
= originator_ip
;
1778 * Install route entry into the VRF routing table and invoke route selection.
1780 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
1781 struct prefix_evpn
*evp
,
1782 struct bgp_info
*parent_ri
)
1784 struct bgp_node
*rn
;
1785 struct bgp_info
*ri
;
1787 struct attr
*attr_new
;
1790 struct prefix
*pp
= &p
;
1793 char buf
[PREFIX_STRLEN
];
1794 char buf1
[PREFIX_STRLEN
];
1796 memset(pp
, 0, sizeof(struct prefix
));
1797 ip_prefix_from_evpn_prefix(evp
, pp
);
1799 if (bgp_debug_zebra(NULL
)) {
1801 "installing evpn prefix %s as ip prefix %s in vrf %s",
1802 prefix2str(evp
, buf
, sizeof(buf
)),
1803 prefix2str(pp
, buf1
, sizeof(buf
)),
1804 vrf_id_to_name(bgp_vrf
->vrf_id
));
1807 /* Create (or fetch) route within the VRF. */
1808 /* NOTE: There is no RD here. */
1809 if (is_evpn_prefix_ipaddr_v4(evp
)) {
1811 safi
= SAFI_UNICAST
;
1812 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
1813 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
1815 safi
= SAFI_UNICAST
;
1816 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
1820 /* EVPN routes currently only support a IPv4 next hop which corresponds
1821 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
1822 * route, we have to convert the next hop to an IPv4-mapped address
1823 * for the rest of the code to flow through.
1825 bgp_attr_dup(&attr
, parent_ri
->attr
);
1827 evpn_convert_nexthop_to_ipv6(&attr
);
1829 /* Check if route entry is already present. */
1830 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1832 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
1836 /* Add (or update) attribute to hash. */
1837 attr_new
= bgp_attr_intern(&attr
);
1839 /* Create new route with its attribute. */
1840 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
1841 parent_ri
->peer
, attr_new
, rn
);
1842 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1843 bgp_info_extra_get(ri
);
1844 ri
->extra
->parent
= parent_ri
;
1845 if (parent_ri
->extra
) {
1846 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
1847 sizeof(ri
->extra
->label
));
1848 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
1850 bgp_info_add(rn
, ri
);
1852 if (attrhash_cmp(ri
->attr
, &attr
)
1853 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
1854 bgp_unlock_node(rn
);
1857 /* The attribute has changed. */
1858 /* Add (or update) attribute to hash. */
1859 attr_new
= bgp_attr_intern(&attr
);
1861 /* Restore route, if needed. */
1862 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
1863 bgp_info_restore(rn
, ri
);
1865 /* Mark if nexthop has changed. */
1866 if ((afi
== AFI_IP
&&
1867 !IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
)) ||
1869 !IPV6_ADDR_SAME(&ri
->attr
->mp_nexthop_global
,
1870 &attr_new
->mp_nexthop_global
)))
1871 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
1873 /* Unintern existing, set to new. */
1874 bgp_attr_unintern(&ri
->attr
);
1875 ri
->attr
= attr_new
;
1876 ri
->uptime
= bgp_clock();
1879 /* Perform route selection and update zebra, if required. */
1880 bgp_process(bgp_vrf
, rn
, afi
, safi
);
1886 * Install route entry into the VNI routing table and invoke route selection.
1888 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1889 struct prefix_evpn
*p
,
1890 struct bgp_info
*parent_ri
)
1892 struct bgp_node
*rn
;
1893 struct bgp_info
*ri
;
1894 struct attr
*attr_new
;
1897 /* Create (or fetch) route within the VNI. */
1898 /* NOTE: There is no RD here. */
1899 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1901 /* Check if route entry is already present. */
1902 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1904 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
1908 /* Add (or update) attribute to hash. */
1909 attr_new
= bgp_attr_intern(parent_ri
->attr
);
1911 /* Create new route with its attribute. */
1912 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
1913 parent_ri
->peer
, attr_new
, rn
);
1914 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1915 bgp_info_extra_get(ri
);
1916 ri
->extra
->parent
= parent_ri
;
1917 if (parent_ri
->extra
) {
1918 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
1919 sizeof(ri
->extra
->label
));
1920 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
1922 bgp_info_add(rn
, ri
);
1924 if (attrhash_cmp(ri
->attr
, parent_ri
->attr
)
1925 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
1926 bgp_unlock_node(rn
);
1929 /* The attribute has changed. */
1930 /* Add (or update) attribute to hash. */
1931 attr_new
= bgp_attr_intern(parent_ri
->attr
);
1933 /* Restore route, if needed. */
1934 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
1935 bgp_info_restore(rn
, ri
);
1937 /* Mark if nexthop has changed. */
1938 if (!IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
))
1939 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
1941 /* Unintern existing, set to new. */
1942 bgp_attr_unintern(&ri
->attr
);
1943 ri
->attr
= attr_new
;
1944 ri
->uptime
= bgp_clock();
1947 /* Perform route selection and update zebra, if required. */
1948 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
1954 * Uninstall route entry from the VRF routing table and send message
1955 * to zebra, if appropriate.
1957 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
1958 struct prefix_evpn
*evp
,
1959 struct bgp_info
*parent_ri
)
1961 struct bgp_node
*rn
;
1962 struct bgp_info
*ri
;
1965 struct prefix
*pp
= &p
;
1968 char buf
[PREFIX_STRLEN
];
1969 char buf1
[PREFIX_STRLEN
];
1971 memset(pp
, 0, sizeof(struct prefix
));
1972 ip_prefix_from_evpn_prefix(evp
, pp
);
1974 if (bgp_debug_zebra(NULL
)) {
1976 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
1977 prefix2str(evp
, buf
, sizeof(buf
)),
1978 prefix2str(pp
, buf1
, sizeof(buf
)),
1979 vrf_id_to_name(bgp_vrf
->vrf_id
));
1982 /* Locate route within the VRF. */
1983 /* NOTE: There is no RD here. */
1984 if (is_evpn_prefix_ipaddr_v4(evp
)) {
1986 safi
= SAFI_UNICAST
;
1987 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
1990 safi
= SAFI_UNICAST
;
1991 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
1997 /* Find matching route entry. */
1998 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2000 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2006 /* Mark entry for deletion */
2007 bgp_info_delete(rn
, ri
);
2009 /* Perform route selection and update zebra, if required. */
2010 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2012 /* Unlock route node. */
2013 bgp_unlock_node(rn
);
2019 * Uninstall route entry from the VNI routing table and send message
2020 * to zebra, if appropriate.
2022 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2023 struct prefix_evpn
*p
,
2024 struct bgp_info
*parent_ri
)
2026 struct bgp_node
*rn
;
2027 struct bgp_info
*ri
;
2030 /* Locate route within the VNI. */
2031 /* NOTE: There is no RD here. */
2032 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2036 /* Find matching route entry. */
2037 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2039 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2045 /* Mark entry for deletion */
2046 bgp_info_delete(rn
, ri
);
2048 /* Perform route selection and update zebra, if required. */
2049 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2051 /* Unlock route node. */
2052 bgp_unlock_node(rn
);
2058 * Given a route entry and a VRF, see if this route entry should be
2059 * imported into the VRF i.e., RTs match.
2061 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
, struct bgp_info
*ri
)
2063 struct attr
*attr
= ri
->attr
;
2064 struct ecommunity
*ecom
;
2068 /* Route should have valid RT to be even considered. */
2069 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2072 ecom
= attr
->ecommunity
;
2073 if (!ecom
|| !ecom
->size
)
2076 /* For each extended community RT, see if it matches this VNI. If any RT
2077 * matches, we're done.
2079 for (i
= 0; i
< ecom
->size
; i
++) {
2081 uint8_t type
, sub_type
;
2082 struct ecommunity_val
*eval
;
2083 struct ecommunity_val eval_tmp
;
2084 struct vrf_irt_node
*irt
;
2086 /* Only deal with RTs */
2087 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2088 eval
= (struct ecommunity_val
*)(ecom
->val
2089 + (i
* ECOMMUNITY_SIZE
));
2092 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2095 /* See if this RT matches specified VNIs import RTs */
2096 irt
= lookup_vrf_import_rt(eval
);
2097 if (irt
&& irt
->vrfs
)
2098 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2101 /* Also check for non-exact match. In this, we mask out the AS
2103 * only check on the local-admin sub-field. This is to
2105 * VNI as the RT for EBGP peering too.
2108 if (type
== ECOMMUNITY_ENCODE_AS
2109 || type
== ECOMMUNITY_ENCODE_AS4
2110 || type
== ECOMMUNITY_ENCODE_IP
) {
2111 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2112 mask_ecom_global_admin(&eval_tmp
, eval
);
2113 irt
= lookup_vrf_import_rt(&eval_tmp
);
2115 if (irt
&& irt
->vrfs
)
2116 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2124 * Given a route entry and a VNI, see if this route entry should be
2125 * imported into the VNI i.e., RTs match.
2127 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2128 struct bgp_info
*ri
)
2130 struct attr
*attr
= ri
->attr
;
2131 struct ecommunity
*ecom
;
2135 /* Route should have valid RT to be even considered. */
2136 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2139 ecom
= attr
->ecommunity
;
2140 if (!ecom
|| !ecom
->size
)
2143 /* For each extended community RT, see if it matches this VNI. If any RT
2144 * matches, we're done.
2146 for (i
= 0; i
< ecom
->size
; i
++) {
2148 uint8_t type
, sub_type
;
2149 struct ecommunity_val
*eval
;
2150 struct ecommunity_val eval_tmp
;
2151 struct irt_node
*irt
;
2153 /* Only deal with RTs */
2154 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2155 eval
= (struct ecommunity_val
*)(ecom
->val
2156 + (i
* ECOMMUNITY_SIZE
));
2159 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2162 /* See if this RT matches specified VNIs import RTs */
2163 irt
= lookup_import_rt(bgp
, eval
);
2164 if (irt
&& irt
->vnis
)
2165 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2168 /* Also check for non-exact match. In this, we mask out the AS
2170 * only check on the local-admin sub-field. This is to
2172 * VNI as the RT for EBGP peering too.
2175 if (type
== ECOMMUNITY_ENCODE_AS
2176 || type
== ECOMMUNITY_ENCODE_AS4
2177 || type
== ECOMMUNITY_ENCODE_IP
) {
2178 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2179 mask_ecom_global_admin(&eval_tmp
, eval
);
2180 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2182 if (irt
&& irt
->vnis
)
2183 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2191 * Install or uninstall mac-ip routes are appropriate for this
2194 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
2198 struct bgp_node
*rd_rn
, *rn
;
2199 struct bgp_table
*table
;
2200 struct bgp_info
*ri
;
2202 char buf
[PREFIX_STRLEN
];
2203 struct bgp
*bgp_def
= NULL
;
2207 bgp_def
= bgp_get_default();
2211 /* Walk entire global routing table and evaluate routes which could be
2212 * imported into this VRF. Note that we need to loop through all global
2213 * routes to determine which route matches the import rt on vrf
2215 for (rd_rn
= bgp_table_top(bgp_def
->rib
[afi
][safi
]); rd_rn
;
2216 rd_rn
= bgp_route_next(rd_rn
)) {
2217 table
= (struct bgp_table
*)(rd_rn
->info
);
2221 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2222 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2224 /* if not mac-ip route skip this route */
2225 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2226 || evp
->prefix
.route_type
2227 == BGP_EVPN_IP_PREFIX_ROUTE
))
2230 /* if not a mac+ip route skip this route */
2231 if (!(is_evpn_prefix_ipaddr_v4(evp
)
2232 || is_evpn_prefix_ipaddr_v6(evp
)))
2235 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2236 /* Consider "valid" remote routes applicable for
2239 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2240 && ri
->type
== ZEBRA_ROUTE_BGP
2241 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2244 if (is_route_matching_for_vrf(bgp_vrf
, ri
)) {
2246 ret
= install_evpn_route_entry_in_vrf(
2249 ret
= uninstall_evpn_route_entry_in_vrf(
2254 "Failed to %s EVPN %s route in VRF %s",
2257 prefix2str(evp
, buf
,
2272 * Install or uninstall routes of specified type that are appropriate for this
2275 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
2276 struct bgpevpn
*vpn
,
2277 bgp_evpn_route_type rtype
,
2282 struct bgp_node
*rd_rn
, *rn
;
2283 struct bgp_table
*table
;
2284 struct bgp_info
*ri
;
2290 /* Walk entire global routing table and evaluate routes which could be
2291 * imported into this VPN. Note that we cannot just look at the routes
2293 * the VNI's RD - remote routes applicable for this VNI could have any
2296 /* EVPN routes are a 2-level table. */
2297 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2298 rd_rn
= bgp_route_next(rd_rn
)) {
2299 table
= (struct bgp_table
*)(rd_rn
->info
);
2303 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2304 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2306 if (evp
->prefix
.route_type
!= rtype
)
2309 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2310 /* Consider "valid" remote routes applicable for
2312 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2313 && ri
->type
== ZEBRA_ROUTE_BGP
2314 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2317 if (is_route_matching_for_vni(bgp
, vpn
, ri
)) {
2319 ret
= install_evpn_route_entry(
2322 ret
= uninstall_evpn_route_entry(
2327 "%u: Failed to %s EVPN %s route in VNI %u",
2331 rtype
== BGP_EVPN_MAC_IP_ROUTE
2345 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
2346 * is invoked upon l3vni-add or l3vni import rt change
2348 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
2350 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
2355 * Install any existing remote routes applicable for this VNI into its
2356 * routing table. This is invoked when a VNI becomes "live" or its Import
2359 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2363 /* Install type-3 routes followed by type-2 routes - the ones applicable
2366 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
2371 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
2375 /* uninstall routes from l3vni vrf. */
2376 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
2378 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
2383 * Uninstall any existing remote routes for this VNI. One scenario in which
2384 * this is invoked is upon an import RT change.
2386 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2390 /* Uninstall type-2 routes followed by type-3 routes - the ones
2394 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
2399 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
2404 * Install or uninstall route in matching VRFs (list).
2406 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
2407 safi_t safi
, struct prefix_evpn
*evp
,
2408 struct bgp_info
*ri
,
2409 struct list
*vrfs
, int install
)
2411 char buf
[PREFIX2STR_BUFFER
];
2412 struct bgp
*bgp_vrf
;
2413 struct listnode
*node
, *nnode
;
2415 /* Only type-2/type-5 routes go into a VRF */
2416 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2417 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
2420 /* if it is type-2 route and not a mac+ip route skip this route */
2421 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
2422 && !(is_evpn_prefix_ipaddr_v4(evp
)
2423 || is_evpn_prefix_ipaddr_v6(evp
)))
2426 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
2430 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, ri
);
2432 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
2436 zlog_err("%u: Failed to %s prefix %s in VRF %s",
2438 install
? "install" : "uninstall",
2439 prefix2str(evp
, buf
, sizeof(buf
)),
2440 vrf_id_to_name(bgp_vrf
->vrf_id
));
2449 * Install or uninstall route in matching VNIs (list).
2451 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
2452 safi_t safi
, struct prefix_evpn
*evp
,
2453 struct bgp_info
*ri
,
2454 struct list
*vnis
, int install
)
2456 struct bgpevpn
*vpn
;
2457 struct listnode
*node
, *nnode
;
2459 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
2462 if (!is_vni_live(vpn
))
2466 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, ri
);
2468 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, ri
);
2471 zlog_err("%u: Failed to %s EVPN %s route in VNI %u",
2472 bgp
->vrf_id
, install
? "install" : "uninstall",
2473 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2485 * Install or uninstall route for appropriate VNIs.
2487 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
2488 struct prefix
*p
, struct bgp_info
*ri
,
2491 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
2492 struct attr
*attr
= ri
->attr
;
2493 struct ecommunity
*ecom
;
2498 /* Only type-2 and type-3 and type-5 are supported currently */
2499 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2500 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
2501 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
2504 /* If we don't have Route Target, nothing much to do. */
2505 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2508 ecom
= attr
->ecommunity
;
2509 if (!ecom
|| !ecom
->size
)
2512 /* For each extended community RT, see which VNIs/VRFs match and import
2513 * the route into matching VNIs/VRFs.
2515 for (i
= 0; i
< ecom
->size
; i
++) {
2517 uint8_t type
, sub_type
;
2518 struct ecommunity_val
*eval
;
2519 struct ecommunity_val eval_tmp
;
2520 struct irt_node
*irt
; /* import rt for l2vni */
2521 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
2523 /* Only deal with RTs */
2524 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2525 eval
= (struct ecommunity_val
*)(ecom
->val
2526 + (i
* ECOMMUNITY_SIZE
));
2529 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2532 /* Import route into matching l2-vnis (type-2/type-3 routes go
2535 irt
= lookup_import_rt(bgp
, eval
);
2536 if (irt
&& irt
->vnis
)
2537 install_uninstall_route_in_vnis(bgp
, afi
, safi
, evp
, ri
,
2540 /* Import route into matching l3-vnis (type-2/type-5 routes go
2541 * into l3vni/vrf table)
2543 vrf_irt
= lookup_vrf_import_rt(eval
);
2544 if (vrf_irt
&& vrf_irt
->vrfs
)
2545 install_uninstall_route_in_vrfs(bgp
, afi
, safi
, evp
, ri
,
2546 vrf_irt
->vrfs
, import
);
2548 /* Also check for non-exact match. In this,
2549 * we mask out the AS and
2550 * only check on the local-admin sub-field.
2551 * This is to facilitate using
2552 * VNI as the RT for EBGP peering too.
2556 if (type
== ECOMMUNITY_ENCODE_AS
2557 || type
== ECOMMUNITY_ENCODE_AS4
2558 || type
== ECOMMUNITY_ENCODE_IP
) {
2559 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2560 mask_ecom_global_admin(&eval_tmp
, eval
);
2561 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2562 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
2564 if (irt
&& irt
->vnis
)
2565 install_uninstall_route_in_vnis(bgp
, afi
, safi
, evp
, ri
,
2567 if (vrf_irt
&& vrf_irt
->vrfs
)
2568 install_uninstall_route_in_vrfs(bgp
, afi
, safi
, evp
, ri
,
2569 vrf_irt
->vrfs
, import
);
2575 /* delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
2577 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
2579 /* delete all ipv4 routes and withdraw from peers */
2580 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
2581 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
2583 /* delete all ipv6 routes and withdraw from peers */
2584 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
2585 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
2588 /* update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
2590 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
2592 /* update all ipv4 routes */
2593 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
2594 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
2596 /* update all ipv6 routes */
2597 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
2598 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
2602 * update and advertise local routes for a VRF as type-5 routes.
2603 * This is invoked upon RD change for a VRF. Note taht the processing is only
2604 * done in the global route table using the routes which already exist in the
2607 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
2609 /* skip if the RD is configured */
2610 if (is_vrf_rd_configured(bgp_vrf
))
2613 /* derive the RD for the VRF based on new router-id */
2614 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
2616 /* update advertise ipv4|ipv6 routes as type-5 routes */
2617 update_advertise_vrf_routes(bgp_vrf
);
2621 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
2622 * This is invoked upon VRF RD change. The processing is done only from global
2625 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
2627 /* skip if the RD is configured */
2628 if (is_vrf_rd_configured(bgp_vrf
))
2631 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
2632 delete_withdraw_vrf_routes(bgp_vrf
);
2636 * Update and advertise local routes for a VNI. Invoked upon router-id
2637 * change. Note that the processing is done only on the global route table
2638 * using routes that already exist in the per-VNI table.
2640 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2642 struct prefix_evpn p
;
2643 struct bgp_node
*rn
, *global_rn
;
2644 struct bgp_info
*ri
, *global_ri
;
2646 afi_t afi
= AFI_L2VPN
;
2647 safi_t safi
= SAFI_EVPN
;
2649 /* Locate type-3 route for VNI in the per-VNI table and use its
2650 * attributes to create and advertise the type-3 route for this VNI
2651 * in the global table.
2653 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2654 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
2655 if (!rn
) /* unexpected */
2657 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2658 if (ri
->peer
== bgp
->peer_self
&& ri
->type
== ZEBRA_ROUTE_BGP
2659 && ri
->sub_type
== BGP_ROUTE_STATIC
)
2661 if (!ri
) /* unexpected */
2665 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2666 (struct prefix
*)&p
, &vpn
->prd
);
2667 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1, 0, &ri
,
2670 /* Schedule for processing and unlock node. */
2671 bgp_process(bgp
, global_rn
, afi
, safi
);
2672 bgp_unlock_node(global_rn
);
2674 /* Now, walk this VNI's route table and use the route and its attribute
2675 * to create and schedule route in global table.
2677 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2678 rn
= bgp_route_next(rn
)) {
2679 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2681 /* Identify MAC-IP local routes. */
2682 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2685 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2686 if (ri
->peer
== bgp
->peer_self
2687 && ri
->type
== ZEBRA_ROUTE_BGP
2688 && ri
->sub_type
== BGP_ROUTE_STATIC
)
2693 /* Create route in global routing table using this route entry's
2697 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2698 (struct prefix
*)evp
, &vpn
->prd
);
2700 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
2703 /* Schedule for processing and unlock node. */
2704 bgp_process(bgp
, global_rn
, afi
, safi
);
2705 bgp_unlock_node(global_rn
);
2712 * Delete (and withdraw) local routes for a VNI - only from the global
2713 * table. Invoked upon router-id change.
2715 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2718 struct prefix_evpn p
;
2719 struct bgp_node
*global_rn
;
2720 struct bgp_info
*ri
;
2721 afi_t afi
= AFI_L2VPN
;
2722 safi_t safi
= SAFI_EVPN
;
2724 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2725 * for this VNI - from the global table.
2727 ret
= delete_global_type2_routes(bgp
, vpn
);
2731 /* Remove type-3 route for this VNI from global table. */
2732 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2733 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
2734 (struct prefix
*)&p
, &vpn
->prd
);
2736 /* Delete route entry in the global EVPN table. */
2737 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
2739 /* Schedule for processing - withdraws to peers happen from
2743 bgp_process(bgp
, global_rn
, afi
, safi
);
2744 bgp_unlock_node(global_rn
);
2751 * Handle router-id change. Update and advertise local routes corresponding
2752 * to this VNI from peers. Note that this is invoked after updating the
2753 * router-id. The routes in the per-VNI table are used to create routes in
2754 * the global table and schedule them.
2756 static void update_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
2758 struct bgpevpn
*vpn
;
2760 vpn
= (struct bgpevpn
*)backet
->data
;
2763 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__
);
2767 /* Skip VNIs with configured RD. */
2768 if (is_rd_configured(vpn
))
2771 bgp_evpn_derive_auto_rd(bgp
, vpn
);
2772 update_advertise_vni_routes(bgp
, vpn
);
2776 * Handle router-id change. Delete and withdraw local routes corresponding
2777 * to this VNI from peers. Note that this is invoked prior to updating
2778 * the router-id and is done only on the global route table, the routes
2779 * are needed in the per-VNI table to re-advertise with new router id.
2781 static void withdraw_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
2783 struct bgpevpn
*vpn
;
2785 vpn
= (struct bgpevpn
*)backet
->data
;
2788 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__
);
2792 /* Skip VNIs with configured RD. */
2793 if (is_rd_configured(vpn
))
2796 delete_withdraw_vni_routes(bgp
, vpn
);
2800 * Process received EVPN type-2 route (advertise or withdraw).
2802 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
2803 struct attr
*attr
, uint8_t *pfx
, int psize
,
2804 uint32_t addpath_id
)
2806 struct prefix_rd prd
;
2807 struct prefix_evpn p
;
2808 struct bgp_route_evpn evpn
;
2810 uint8_t macaddr_len
;
2811 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
2812 uint32_t num_labels
= 0;
2816 /* Type-2 route should be either 33, 37 or 49 bytes or an
2817 * additional 3 bytes if there is a second label (VNI):
2818 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
2819 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
2820 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
2822 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
2823 && psize
!= 40 && psize
!= 52) {
2824 zlog_err("%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
2825 peer
->bgp
->vrf_id
, peer
->host
, psize
);
2829 memset(&evpn
, 0, sizeof(evpn
));
2831 /* Make prefix_rd */
2832 prd
.family
= AF_UNSPEC
;
2834 memcpy(&prd
.val
, pfx
, 8);
2837 /* Make EVPN prefix. */
2838 memset(&p
, 0, sizeof(struct prefix_evpn
));
2840 p
.prefixlen
= EVPN_TYPE_2_ROUTE_PREFIXLEN
;
2841 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
2843 /* Copy Ethernet Seg Identifier */
2844 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
2847 /* Copy Ethernet Tag */
2848 memcpy(ð_tag
, pfx
, 4);
2849 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
2852 /* Get the MAC Addr len */
2853 macaddr_len
= *pfx
++;
2855 /* Get the MAC Addr */
2856 if (macaddr_len
== (ETH_ALEN
* 8)) {
2857 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
2861 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
2862 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
2868 ipaddr_len
= *pfx
++;
2869 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
2870 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
2872 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
2873 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
2878 ipaddr_len
/= 8; /* Convert to bytes. */
2879 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
2882 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
2886 /* Get the VNI(s). Stored as bytes here. */
2888 memset(label
, 0, sizeof(label
));
2889 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
2890 pfx
+= BGP_LABEL_BYTES
;
2891 psize
-= (33 + ipaddr_len
);
2892 /* Do we have a second VNI? */
2895 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
2897 * If in future, we are required to access additional fields,
2898 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
2903 /* Process the route. */
2905 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2906 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2907 &prd
, &label
[0], num_labels
, 0, &evpn
);
2909 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2910 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2911 &prd
, &label
[0], num_labels
, &evpn
);
2916 * Process received EVPN type-3 route (advertise or withdraw).
2918 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
2919 struct attr
*attr
, uint8_t *pfx
, int psize
,
2920 uint32_t addpath_id
)
2922 struct prefix_rd prd
;
2923 struct prefix_evpn p
;
2928 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
2929 * IP len (1) and IP (4 or 16).
2931 if (psize
!= 17 && psize
!= 29) {
2932 zlog_err("%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
2933 peer
->bgp
->vrf_id
, peer
->host
, psize
);
2937 /* If PMSI is present, log if it is anything other than IR.
2938 * Note: We just simply ignore the values as it is not clear if
2939 * doing anything else is better.
2942 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
2943 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
) {
2944 zlog_warn("%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
2945 peer
->bgp
->vrf_id
, peer
->host
,
2946 attr
->pmsi_tnl_type
);
2950 /* Make prefix_rd */
2951 prd
.family
= AF_UNSPEC
;
2953 memcpy(&prd
.val
, pfx
, 8);
2956 /* Make EVPN prefix. */
2957 memset(&p
, 0, sizeof(struct prefix_evpn
));
2959 p
.prefixlen
= EVPN_TYPE_3_ROUTE_PREFIXLEN
;
2960 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
2962 /* Copy Ethernet Tag */
2963 memcpy(ð_tag
, pfx
, 4);
2964 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
2968 ipaddr_len
= *pfx
++;
2969 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
2970 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
2971 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
2974 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
2975 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
2979 /* Process the route. */
2981 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2982 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2983 &prd
, NULL
, 0, 0, NULL
);
2985 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2986 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2987 &prd
, NULL
, 0, NULL
);
2992 * Process received EVPN type-5 route (advertise or withdraw).
2994 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
2995 struct attr
*attr
, uint8_t *pfx
, int psize
,
2996 uint32_t addpath_id
, int withdraw
)
2998 struct prefix_rd prd
;
2999 struct prefix_evpn p
;
3000 struct bgp_route_evpn evpn
;
3003 mpls_label_t label
; /* holds the VNI as in the packet */
3006 /* Type-5 route should be 34 or 58 bytes:
3007 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3008 * GW (4 or 16) and VNI (3).
3009 * Note that the IP and GW should both be IPv4 or both IPv6.
3011 if (psize
!= 34 && psize
!= 58) {
3012 zlog_err("%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3013 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3017 /* Make prefix_rd */
3018 prd
.family
= AF_UNSPEC
;
3020 memcpy(&prd
.val
, pfx
, 8);
3023 /* Make EVPN prefix. */
3024 memset(&p
, 0, sizeof(struct prefix_evpn
));
3026 p
.prefixlen
= EVPN_TYPE_5_ROUTE_PREFIXLEN
;
3027 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
3029 /* Additional information outside of prefix - ESI and GW IP */
3030 memset(&evpn
, 0, sizeof(evpn
));
3033 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
3036 /* Fetch Ethernet Tag. */
3037 memcpy(ð_tag
, pfx
, 4);
3038 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
3041 /* Fetch IP prefix length. */
3043 if (ippfx_len
> IPV6_MAX_BITLEN
) {
3045 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3046 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
3049 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
3051 /* Determine IPv4 or IPv6 prefix */
3052 /* Since the address and GW are from the same family, this just becomes
3053 * a simple check on the total size.
3056 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
3057 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
3059 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
3062 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
3063 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
3065 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
3069 /* Get the VNI (in MPLS label field). Stored as bytes here. */
3070 memset(&label
, 0, sizeof(label
));
3071 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
3074 * If in future, we are required to access additional fields,
3075 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
3079 /* Process the route. */
3081 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3082 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3083 &prd
, &label
, 1, 0, &evpn
);
3085 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3086 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3087 &prd
, &label
, 1, &evpn
);
3092 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
3093 struct prefix_rd
*prd
, mpls_label_t
*label
,
3094 uint32_t num_labels
, struct attr
*attr
)
3098 struct evpn_addr
*p_evpn_p
;
3100 memset(&temp
, 0, 16);
3101 if (p
->family
!= AF_EVPN
)
3103 p_evpn_p
= &(p
->u
.prefix_evpn
);
3105 /* len denites the total len of IP and GW-IP in the route
3106 IP and GW-IP have to be both ipv4 or ipv6
3108 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3109 len
= 8; /* IP and GWIP are both ipv4 */
3111 len
= 32; /* IP and GWIP are both ipv6 */
3112 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
3113 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
3114 stream_put(s
, prd
->val
, 8);
3116 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
3118 stream_put(s
, &temp
, 10);
3119 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
3120 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
3121 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3122 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
3124 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
3126 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3128 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
3130 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
3132 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3133 stream_put_ipv4(s
, 0);
3135 stream_put(s
, &temp
, 16);
3139 stream_put(s
, label
, 3);
3145 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
3147 static void cleanup_vni_on_disable(struct hash_backet
*backet
, struct bgp
*bgp
)
3149 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3151 /* Remove EVPN routes and schedule for processing. */
3152 delete_routes_for_vni(bgp
, vpn
);
3154 /* Clear "live" flag and see if hash needs to be freed. */
3155 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
3156 if (!is_vni_configured(vpn
))
3157 bgp_evpn_free(bgp
, vpn
);
3161 * Free a VNI entry; iterator function called during cleanup.
3163 static void free_vni_entry(struct hash_backet
*backet
, struct bgp
*bgp
)
3165 struct bgpevpn
*vpn
;
3167 vpn
= (struct bgpevpn
*)backet
->data
;
3168 delete_all_vni_routes(bgp
, vpn
);
3169 bgp_evpn_free(bgp
, vpn
);
3173 * Derive AUTO import RT for BGP VRF - L3VNI
3175 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
3177 struct bgp
*bgp_def
= NULL
;
3179 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
3180 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
3183 bgp_def
= bgp_get_default();
3186 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
3190 * Delete AUTO import RT from BGP VRF - L3VNI
3192 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
3194 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
3198 * Derive AUTO export RT for BGP VRF - L3VNI
3200 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
3202 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
3203 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
3207 * Delete AUTO export RT from BGP VRF - L3VNI
3209 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
3211 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
3214 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
3216 struct bgp
*bgp_def
= NULL
;
3217 struct listnode
*node
= NULL
;
3218 struct bgpevpn
*vpn
= NULL
;
3220 bgp_def
= bgp_get_default();
3224 /* update all type-5 routes */
3225 update_advertise_vrf_routes(bgp_vrf
);
3227 /* update all type-2 routes */
3228 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
3229 update_routes_for_vni(bgp_def
, vpn
);
3236 /* withdraw type-5 route corresponding to ip prefix */
3237 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
3238 afi_t afi
, safi_t safi
)
3241 struct prefix_evpn evp
;
3242 char buf
[PREFIX_STRLEN
];
3244 build_type5_prefix_from_ip_prefix(&evp
, p
);
3245 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
3248 "%u failed to delete type-5 route for prefix %s in vrf %s",
3249 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
3250 vrf_id_to_name(bgp_vrf
->vrf_id
));
3254 /* withdraw all type-5 routes for an address family */
3255 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
3257 struct bgp_table
*table
= NULL
;
3258 struct bgp_node
*rn
= NULL
;
3259 struct bgp_info
*ri
;
3261 table
= bgp_vrf
->rib
[afi
][safi
];
3262 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3263 /* Only care about "selected" routes - non-imported. */
3264 /* TODO: Support for AddPath for EVPN. */
3265 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
3266 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
)
3267 && (!ri
->extra
|| !ri
->extra
->parent
)) {
3268 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
3277 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
3278 * to this function correspond to those of the source IP prefix (best
3279 * path in the case of the attr. In the case of a local prefix (when we
3280 * are advertising local subnets), the src_attr will be NULL.
3282 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
3283 struct attr
*src_attr
, afi_t afi
,
3287 struct prefix_evpn evp
;
3288 char buf
[PREFIX_STRLEN
];
3290 build_type5_prefix_from_ip_prefix(&evp
, p
);
3291 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
3293 zlog_err("%u: Failed to create type-5 route for prefix %s",
3294 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
3297 /* Inject all prefixes of a particular address-family (currently, IPv4 or
3298 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
3299 * advertisement is enabled.
3301 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
3304 struct bgp_table
*table
= NULL
;
3305 struct bgp_node
*rn
= NULL
;
3306 struct bgp_info
*ri
;
3308 table
= bgp_vrf
->rib
[afi
][safi
];
3309 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3310 /* Need to identify the "selected" route entry to use its
3311 * attribute. Also, we only consider "non-imported" routes.
3312 * TODO: Support for AddPath for EVPN.
3314 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
3315 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
)
3316 && (!ri
->extra
|| !ri
->extra
->parent
)) {
3318 /* apply the route-map */
3319 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
3322 ret
= route_map_apply(
3323 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
3325 &rn
->p
, RMAP_BGP
, ri
);
3326 if (ret
== RMAP_DENYMATCH
)
3329 bgp_evpn_advertise_type5_route(
3330 bgp_vrf
, &rn
->p
, ri
->attr
, afi
, safi
);
3337 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
3339 struct listnode
*node
, *nnode
, *node_to_del
;
3340 struct ecommunity
*ecom
, *ecom_auto
;
3341 struct ecommunity_val eval
;
3343 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
3345 ecom_auto
= ecommunity_new();
3346 ecommunity_add_val(ecom_auto
, &eval
);
3349 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
3350 if (ecommunity_match(ecom
, ecom_auto
)) {
3351 ecommunity_free(&ecom
);
3357 list_delete_node(rtl
, node_to_del
);
3359 ecommunity_free(&ecom_auto
);
3362 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
3363 struct ecommunity
*ecomadd
)
3365 /* uninstall routes from vrf */
3366 uninstall_routes_for_vrf(bgp_vrf
);
3368 /* Cleanup the RT to VRF mapping */
3369 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
3371 /* Remove auto generated RT */
3372 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
3374 /* Add the newly configured RT to RT list */
3375 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
3376 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
3378 /* map VRF to its RTs */
3379 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
3381 /* install routes matching the new VRF */
3382 install_routes_for_vrf(bgp_vrf
);
3385 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
3386 struct ecommunity
*ecomdel
)
3388 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
3389 struct ecommunity
*ecom
= NULL
;
3391 /* uninstall routes from vrf */
3392 uninstall_routes_for_vrf(bgp_vrf
);
3394 /* Cleanup the RT to VRF mapping */
3395 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
3397 /* remove the RT from the RT list */
3398 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
3399 if (ecommunity_match(ecom
, ecomdel
)) {
3400 ecommunity_free(&ecom
);
3407 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
3409 /* fallback to auto import rt, if this was the last RT */
3410 if (list_isempty(bgp_vrf
->vrf_import_rtl
)) {
3411 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
3412 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
3415 /* map VRFs to its RTs */
3416 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
3418 /* install routes matching this new RT */
3419 install_routes_for_vrf(bgp_vrf
);
3422 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
3423 struct ecommunity
*ecomadd
)
3425 /* remove auto-generated RT */
3426 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
3428 /* Add the new RT to the RT list */
3429 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
3430 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
3432 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
3435 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
3436 struct ecommunity
*ecomdel
)
3438 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
3439 struct ecommunity
*ecom
= NULL
;
3441 /* Remove the RT from the RT list */
3442 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
3443 if (ecommunity_match(ecom
, ecomdel
)) {
3444 ecommunity_free(&ecom
);
3451 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
3453 /* fall back to auto-generated RT if this was the last RT */
3454 if (bgp_vrf
->vrf_export_rtl
&& list_isempty(bgp_vrf
->vrf_export_rtl
)) {
3455 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
3456 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
3459 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
3463 * Handle change to BGP router id. This is invoked twice by the change
3464 * handler, first before the router id has been changed and then after
3465 * the router id has been changed. The first invocation will result in
3466 * local routes for all VNIs/VRF being deleted and withdrawn and the next
3467 * will result in the routes being re-advertised.
3469 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
3473 /* delete and withdraw all the type-5 routes
3474 stored in the global table for this vrf
3476 withdraw_router_id_vrf(bgp
);
3478 /* delete all the VNI routes (type-2/type-3) routes for all the
3481 hash_iterate(bgp
->vnihash
,
3482 (void (*)(struct hash_backet
*,
3483 void *))withdraw_router_id_vni
,
3487 /* advertise all routes in the vrf as type-5 routes with the new
3490 update_router_id_vrf(bgp
);
3492 /* advertise all the VNI routes (type-2/type-3) routes with the
3495 hash_iterate(bgp
->vnihash
,
3496 (void (*)(struct hash_backet
*,
3497 void *))update_router_id_vni
,
3503 * Handle change to export RT - update and advertise local routes.
3505 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3507 return update_routes_for_vni(bgp
, vpn
);
3510 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
3513 delete_withdraw_vrf_routes(bgp_vrf
);
3515 update_advertise_vrf_routes(bgp_vrf
);
3519 * Handle change to RD. This is invoked twice by the change handler,
3520 * first before the RD has been changed and then after the RD has
3521 * been changed. The first invocation will result in local routes
3522 * of this VNI being deleted and withdrawn and the next will result
3523 * in the routes being re-advertised.
3525 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
3529 delete_withdraw_vni_routes(bgp
, vpn
);
3531 update_advertise_vni_routes(bgp
, vpn
);
3535 * Install routes for this VNI. Invoked upon change to Import RT.
3537 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3539 return install_routes_for_vni(bgp
, vpn
);
3543 * Uninstall all routes installed for this VNI. Invoked upon change
3546 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3548 return uninstall_routes_for_vni(bgp
, vpn
);
3552 * TODO: Hardcoded for a maximum of 2 VNIs right now
3554 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
3559 vni1
= label2vni(label
);
3560 if (num_labels
== 2) {
3561 vni2
= label2vni(label
+ 1);
3562 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
3564 snprintf(buf
, len
, "%u", vni1
);
3569 * Function to convert evpn route to json format.
3570 * NOTE: We don't use prefix2str as the output here is a bit different.
3572 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
3574 char buf1
[ETHER_ADDR_STRLEN
];
3575 char buf2
[PREFIX2STR_BUFFER
];
3580 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
3581 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
3582 json_object_int_add(json
, "ethTag",
3583 p
->prefix
.imet_addr
.eth_tag
);
3584 json_object_int_add(json
, "ipLen",
3585 is_evpn_prefix_ipaddr_v4(p
)
3588 json_object_string_add(json
, "ip",
3589 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
3590 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3591 if (is_evpn_prefix_ipaddr_none(p
)) {
3592 json_object_int_add(json
, "routeType",
3593 p
->prefix
.route_type
);
3594 json_object_int_add(json
, "ethTag",
3595 p
->prefix
.macip_addr
.eth_tag
);
3596 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
3597 json_object_string_add(json
, "mac",
3598 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
3604 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
3607 json_object_int_add(json
, "routeType",
3608 p
->prefix
.route_type
);
3609 json_object_int_add(json
, "ethTag",
3610 p
->prefix
.macip_addr
.eth_tag
);
3611 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
3612 json_object_string_add(json
, "mac",
3613 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
3616 json_object_int_add(json
, "ipLen",
3617 is_evpn_prefix_ipaddr_v4(p
)
3620 json_object_string_add(
3623 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
3625 PREFIX2STR_BUFFER
));
3628 /* Currently, this is to cater to other AF_ETHERNET code. */
3633 * Function to convert evpn route to string.
3634 * NOTE: We don't use prefix2str as the output here is a bit different.
3636 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
3638 char buf1
[ETHER_ADDR_STRLEN
];
3639 char buf2
[PREFIX2STR_BUFFER
];
3641 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
3642 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
3643 p
->prefix
.imet_addr
.eth_tag
,
3644 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
3646 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
3647 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3648 if (is_evpn_prefix_ipaddr_none(p
))
3649 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
3650 p
->prefix
.route_type
,
3651 p
->prefix
.macip_addr
.eth_tag
,
3653 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
3658 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
3660 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
3661 p
->prefix
.route_type
,
3662 p
->prefix
.macip_addr
.eth_tag
,
3664 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
3666 family
== AF_INET
? IPV4_MAX_BITLEN
3669 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
3671 PREFIX2STR_BUFFER
));
3673 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3674 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
3675 p
->prefix
.route_type
,
3676 p
->prefix
.prefix_addr
.eth_tag
,
3677 p
->prefix
.prefix_addr
.ip_prefix_length
,
3678 is_evpn_prefix_ipaddr_v4(p
)
3679 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
3680 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
3682 /* For EVPN route types not supported yet. */
3683 snprintf(buf
, len
, "(unsupported route type %d)",
3684 p
->prefix
.route_type
);
3691 * Encode EVPN prefix in Update (MP_REACH)
3693 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
3694 struct prefix_rd
*prd
, mpls_label_t
*label
,
3695 uint32_t num_labels
, struct attr
*attr
,
3696 int addpath_encode
, uint32_t addpath_tx_id
)
3698 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3699 int len
, ipa_len
= 0;
3702 stream_putl(s
, addpath_tx_id
);
3705 stream_putc(s
, evp
->prefix
.route_type
);
3707 switch (evp
->prefix
.route_type
) {
3708 case BGP_EVPN_MAC_IP_ROUTE
:
3709 if (is_evpn_prefix_ipaddr_v4(evp
))
3710 ipa_len
= IPV4_MAX_BYTELEN
;
3711 else if (is_evpn_prefix_ipaddr_v6(evp
))
3712 ipa_len
= IPV6_MAX_BYTELEN
;
3713 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
3714 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
3715 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
3717 stream_putc(s
, len
);
3718 stream_put(s
, prd
->val
, 8); /* RD */
3720 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
3722 stream_put(s
, 0, 10);
3723 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
3724 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
3725 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
3726 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
3727 if (ipa_len
) /* IP */
3728 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
3730 /* 1st label is the L2 VNI */
3731 stream_put(s
, label
, BGP_LABEL_BYTES
);
3732 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
3733 if (ipa_len
&& num_labels
> 1)
3734 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
3737 case BGP_EVPN_IMET_ROUTE
:
3738 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
3739 stream_put(s
, prd
->val
, 8); /* RD */
3740 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
3741 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
3742 /* Originating Router's IP Addr */
3743 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
3746 case BGP_EVPN_IP_PREFIX_ROUTE
:
3747 /* TODO: AddPath support. */
3748 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
3756 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
3757 struct bgp_nlri
*packet
, int withdraw
)
3763 uint32_t addpath_id
;
3764 int addpath_encoded
;
3770 /* Start processing the NLRI - there may be multiple in the MP_REACH */
3772 lim
= pnt
+ packet
->length
;
3774 safi
= packet
->safi
;
3778 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
3779 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
3780 PEER_CAP_ADDPATH_AF_TX_RCV
));
3782 for (; pnt
< lim
; pnt
+= psize
) {
3783 /* Clear prefix structure. */
3784 memset(&p
, 0, sizeof(struct prefix
));
3786 /* Deal with path-id if AddPath is supported. */
3787 if (addpath_encoded
) {
3788 /* When packet overflow occurs return immediately. */
3789 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
3792 addpath_id
= ntohl(*((uint32_t *)pnt
));
3793 pnt
+= BGP_ADDPATH_ID_LEN
;
3796 /* All EVPN NLRI types start with type and length. */
3801 psize
= rlen
= *pnt
++;
3803 /* When packet overflow occur return immediately. */
3804 if (pnt
+ psize
> lim
)
3808 case BGP_EVPN_MAC_IP_ROUTE
:
3809 if (process_type2_route(peer
, afi
, safi
,
3810 withdraw
? NULL
: attr
, pnt
,
3811 psize
, addpath_id
)) {
3813 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
3814 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3819 case BGP_EVPN_IMET_ROUTE
:
3820 if (process_type3_route(peer
, afi
, safi
,
3821 withdraw
? NULL
: attr
, pnt
,
3822 psize
, addpath_id
)) {
3824 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
3825 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3830 case BGP_EVPN_IP_PREFIX_ROUTE
:
3831 if (process_type5_route(peer
, afi
, safi
, attr
, pnt
,
3832 psize
, addpath_id
, withdraw
)) {
3834 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
3835 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3845 /* Packet length consistency check. */
3853 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
3854 * The mapping will be used during route processing.
3855 * bgp_def: default bgp instance
3856 * bgp_vrf: specific bgp vrf instance on which RT is configured
3858 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
3861 struct ecommunity_val
*eval
= NULL
;
3862 struct listnode
*node
= NULL
, *nnode
= NULL
;
3863 struct ecommunity
*ecom
= NULL
;
3865 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
3866 for (i
= 0; i
< ecom
->size
; i
++) {
3867 eval
= (struct ecommunity_val
*)(ecom
->val
3869 * ECOMMUNITY_SIZE
));
3870 map_vrf_to_rt(bgp_vrf
, eval
);
3876 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
3878 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
3881 struct ecommunity_val
*eval
;
3882 struct listnode
*node
, *nnode
;
3883 struct ecommunity
*ecom
;
3885 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
3886 for (i
= 0; i
< ecom
->size
; i
++) {
3887 struct vrf_irt_node
*irt
;
3888 struct ecommunity_val eval_tmp
;
3890 eval
= (struct ecommunity_val
*)(ecom
->val
3892 * ECOMMUNITY_SIZE
));
3893 /* If using "automatic" RT, we only care about the
3894 * local-admin sub-field.
3895 * This is to facilitate using VNI as the RT for EBGP
3898 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3899 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
3900 BGP_VRF_IMPORT_RT_CFGD
))
3901 mask_ecom_global_admin(&eval_tmp
, eval
);
3903 irt
= lookup_vrf_import_rt(&eval_tmp
);
3905 unmap_vrf_from_rt(bgp_vrf
, irt
);
3912 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
3913 * The mapping will be used during route processing.
3915 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3918 struct ecommunity_val
*eval
;
3919 struct listnode
*node
, *nnode
;
3920 struct ecommunity
*ecom
;
3922 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
3923 for (i
= 0; i
< ecom
->size
; i
++) {
3924 eval
= (struct ecommunity_val
*)(ecom
->val
3926 * ECOMMUNITY_SIZE
));
3927 map_vni_to_rt(bgp
, vpn
, eval
);
3933 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
3935 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3938 struct ecommunity_val
*eval
;
3939 struct listnode
*node
, *nnode
;
3940 struct ecommunity
*ecom
;
3942 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
3943 for (i
= 0; i
< ecom
->size
; i
++) {
3944 struct irt_node
*irt
;
3945 struct ecommunity_val eval_tmp
;
3947 eval
= (struct ecommunity_val
*)(ecom
->val
3949 * ECOMMUNITY_SIZE
));
3950 /* If using "automatic" RT, we only care about the
3951 * local-admin sub-field.
3952 * This is to facilitate using VNI as the RT for EBGP
3955 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3956 if (!is_import_rt_configured(vpn
))
3957 mask_ecom_global_admin(&eval_tmp
, eval
);
3959 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3961 unmap_vni_from_rt(bgp
, vpn
, irt
);
3967 * Derive Import RT automatically for VNI and map VNI to RT.
3968 * The mapping will be used during route processing.
3970 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3972 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
3973 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
3976 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
3980 * Derive Export RT automatically for VNI.
3982 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3984 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
3985 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
3989 * Derive RD automatically for VNI using passed information - it
3990 * is of the form RouterId:unique-id-for-vni.
3992 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
3994 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
3998 * Derive RD automatically for VNI using passed information - it
3999 * is of the form RouterId:unique-id-for-vni.
4001 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4005 vpn
->prd
.family
= AF_UNSPEC
;
4006 vpn
->prd
.prefixlen
= 64;
4007 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
4008 (void)str2prefix_rd(buf
, &vpn
->prd
);
4009 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
4015 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
4017 struct bgpevpn
*vpn
;
4020 memset(&tmp
, 0, sizeof(struct bgpevpn
));
4022 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
4027 * Create a new vpn - invoked upon configuration or zebra notification.
4029 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
4030 struct in_addr originator_ip
,
4031 vrf_id_t tenant_vrf_id
)
4033 struct bgpevpn
*vpn
;
4038 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
4042 /* Set values - RD and RT set to defaults. */
4044 vpn
->originator_ip
= originator_ip
;
4045 vpn
->tenant_vrf_id
= tenant_vrf_id
;
4047 /* Initialize route-target import and export lists */
4048 vpn
->import_rtl
= list_new();
4049 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4050 vpn
->export_rtl
= list_new();
4051 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4052 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
4053 derive_rd_rt_for_vni(bgp
, vpn
);
4055 /* Initialize EVPN route table. */
4056 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
4059 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
4060 XFREE(MTYPE_BGP_EVPN
, vpn
);
4064 /* add to l2vni list on corresponding vrf */
4065 bgpevpn_link_to_l3vni(vpn
);
4067 QOBJ_REG(vpn
, bgpevpn
);
4072 * Free a given VPN - called in multiple scenarios such as zebra
4073 * notification, configuration being deleted, advertise-all-vni disabled etc.
4074 * This just frees appropriate memory, caller should have taken other
4077 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4079 bgpevpn_unlink_from_l3vni(vpn
);
4080 bgp_table_unlock(vpn
->route_table
);
4081 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4082 list_delete_and_null(&vpn
->import_rtl
);
4083 list_delete_and_null(&vpn
->export_rtl
);
4084 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
4085 hash_release(bgp
->vnihash
, vpn
);
4087 XFREE(MTYPE_BGP_EVPN
, vpn
);
4091 * Import route into matching VNI(s).
4093 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
4094 struct prefix
*p
, struct bgp_info
*ri
)
4096 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 1);
4100 * Unimport route from matching VNI(s).
4102 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
4103 struct prefix
*p
, struct bgp_info
*ri
)
4105 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 0);
4108 /* filter routes which have martian next hops */
4109 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
4113 struct bgp_node
*rd_rn
, *rn
;
4114 struct bgp_table
*table
;
4115 struct bgp_info
*ri
;
4120 /* Walk entire global routing table and evaluate routes which could be
4121 * imported into this VPN. Note that we cannot just look at the routes
4122 * for the VNI's RD -
4123 * remote routes applicable for this VNI could have any RD.
4125 /* EVPN routes are a 2-level table. */
4126 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
4127 rd_rn
= bgp_route_next(rd_rn
)) {
4128 table
= (struct bgp_table
*)(rd_rn
->info
);
4132 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4134 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
4136 /* Consider "valid" remote routes applicable for
4138 if (!(ri
->type
== ZEBRA_ROUTE_BGP
4139 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
4142 if (bgp_nexthop_self(bgp
, ri
->attr
->nexthop
)) {
4144 char attr_str
[BUFSIZ
];
4145 char pbuf
[PREFIX_STRLEN
];
4147 bgp_dump_attr(ri
->attr
, attr_str
,
4150 if (bgp_debug_update(ri
->peer
, &rn
->p
,
4153 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
4160 bgp_evpn_unimport_route(bgp
, afi
, safi
,
4163 bgp_rib_remove(rn
, ri
, ri
->peer
, afi
,
4174 * Handle del of a local MACIP.
4176 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
4179 struct bgpevpn
*vpn
;
4180 struct prefix_evpn p
;
4182 if (!bgp
->vnihash
) {
4183 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4187 /* Lookup VNI hash - should exist. */
4188 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4189 if (!vpn
|| !is_vni_live(vpn
)) {
4190 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP DEL",
4191 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
4195 /* Remove EVPN type-2 route and schedule for processing. */
4196 build_evpn_type2_prefix(&p
, mac
, ip
);
4197 delete_evpn_route(bgp
, vpn
, &p
);
4203 * Handle add of a local MACIP.
4205 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
4206 struct ipaddr
*ip
, uint8_t flags
)
4208 struct bgpevpn
*vpn
;
4209 struct prefix_evpn p
;
4211 if (!bgp
->vnihash
) {
4212 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4216 /* Lookup VNI hash - should exist. */
4217 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4218 if (!vpn
|| !is_vni_live(vpn
)) {
4219 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP ADD",
4220 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
4224 /* Create EVPN type-2 route and schedule for processing. */
4225 build_evpn_type2_prefix(&p
, mac
, ip
);
4226 if (update_evpn_route(bgp
, vpn
, &p
, flags
)) {
4227 char buf
[ETHER_ADDR_STRLEN
];
4228 char buf2
[INET6_ADDRSTRLEN
];
4231 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
4232 bgp
->vrf_id
, vpn
->vni
,
4233 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
4236 prefix_mac2str(mac
, buf
, sizeof(buf
)),
4237 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
4244 static void link_l2vni_hash_to_l3vni(struct hash_backet
*backet
,
4245 struct bgp
*bgp_vrf
)
4247 struct bgpevpn
*vpn
= NULL
;
4248 struct bgp
*bgp_def
= NULL
;
4250 bgp_def
= bgp_get_default();
4253 vpn
= (struct bgpevpn
*)backet
->data
;
4254 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
4255 bgpevpn_link_to_l3vni(vpn
);
4258 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
, struct ethaddr
*rmac
,
4259 struct in_addr originator_ip
, int filter
)
4261 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
4262 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
4263 struct listnode
*node
= NULL
;
4264 struct bgpevpn
*vpn
= NULL
;
4267 /* get the default instamce - required to get the AS number for VRF
4270 bgp_def
= bgp_get_default();
4273 "Cannot process L3VNI %u ADD - default BGP instance not yet created",
4279 /* if the BGP vrf instance doesnt exist - create one */
4280 bgp_vrf
= bgp_lookup_by_name(vrf_id_to_name(vrf_id
));
4285 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
4286 BGP_INSTANCE_TYPE_VRF
);
4288 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
:
4289 zlog_err("'bgp multiple-instance' not present\n");
4291 case BGP_ERR_AS_MISMATCH
:
4292 zlog_err("BGP is already running; AS is %u\n", as
);
4294 case BGP_ERR_INSTANCE_MISMATCH
:
4295 zlog_err("BGP instance name and AS number mismatch\n");
4299 /* mark as auto created */
4300 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
4303 /* associate with l3vni */
4304 bgp_vrf
->l3vni
= l3vni
;
4306 /* set the router mac - to be used in mac-ip routes for this vrf */
4307 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
4309 /* set the originator ip */
4310 bgp_vrf
->originator_ip
= originator_ip
;
4312 /* set the right filter - are we using l3vni only for prefix routes? */
4314 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
4316 /* auto derive RD/RT */
4317 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
4318 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4319 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
4320 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4321 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
4323 /* link all corresponding l2vnis */
4324 hash_iterate(bgp_def
->vnihash
,
4325 (void (*)(struct hash_backet
*,
4326 void *))link_l2vni_hash_to_l3vni
,
4329 /* Only update all corresponding type-2 routes if we are advertising two
4330 * labels along with type-2 routes
4333 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4334 update_routes_for_vni(bgp_def
, vpn
);
4336 /* advertise type-5 routes if needed */
4337 update_advertise_vrf_routes(bgp_vrf
);
4339 /* install all remote routes belonging to this l3vni into correspondng
4341 install_routes_for_vrf(bgp_vrf
);
4346 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
4348 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
4349 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
4350 struct listnode
*node
= NULL
;
4351 struct listnode
*next
= NULL
;
4352 struct bgpevpn
*vpn
= NULL
;
4354 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
4357 "Cannot process L3VNI %u Del - Could not find BGP instance",
4362 bgp_def
= bgp_get_default();
4365 "Cannot process L3VNI %u Del - Could not find default BGP instance",
4370 /* unimport remote routes from VRF, if it is AUTO vrf bgp_delete will
4371 * take care of uninstalling the routes from zebra
4373 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
4374 uninstall_routes_for_vrf(bgp_vrf
);
4376 /* delete/withdraw all type-5 routes */
4377 delete_withdraw_vrf_routes(bgp_vrf
);
4379 /* remove the l3vni from vrf instance */
4382 /* remove the Rmac from the BGP vrf */
4383 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
4386 if (bgp_vrf
->vrf_import_rtl
&& !list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4387 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4388 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
4390 if (bgp_vrf
->vrf_export_rtl
&& !list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4391 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
4394 /* update all corresponding local mac-ip routes */
4395 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
4396 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
4397 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
4398 update_routes_for_vni(bgp_def
, vpn
);
4402 /* If any L2VNIs point to this instance, unlink them. */
4403 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
4404 bgpevpn_unlink_from_l3vni(vpn
);
4406 /* Delete the instance if it was autocreated */
4407 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
4408 bgp_delete(bgp_vrf
);
4414 * Handle del of a local VNI.
4416 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
4418 struct bgpevpn
*vpn
;
4420 if (!bgp
->vnihash
) {
4421 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4425 /* Locate VNI hash */
4426 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4428 zlog_warn("%u: VNI hash entry for VNI %u not found at DEL",
4433 /* Remove all local EVPN routes and schedule for processing (to
4434 * withdraw from peers).
4436 delete_routes_for_vni(bgp
, vpn
);
4439 * tunnel is no longer active, del tunnel ip address from tip_hash
4441 bgp_tip_del(bgp
, &vpn
->originator_ip
);
4443 /* Clear "live" flag and see if hash needs to be freed. */
4444 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4445 if (!is_vni_configured(vpn
))
4446 bgp_evpn_free(bgp
, vpn
);
4452 * Handle add (or update) of a local VNI. The VNI changes we care
4453 * about are for the local-tunnel-ip and the (tenant) VRF.
4455 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
4456 struct in_addr originator_ip
, vrf_id_t tenant_vrf_id
)
4458 struct bgpevpn
*vpn
;
4459 struct prefix_evpn p
;
4461 if (!bgp
->vnihash
) {
4462 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4466 /* Lookup VNI. If present and no change, exit. */
4467 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4470 if (is_vni_live(vpn
)
4471 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
4472 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
4473 /* Probably some other param has changed that we don't
4477 /* Update tenant_vrf_id if it has changed. */
4478 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
4479 bgpevpn_unlink_from_l3vni(vpn
);
4480 vpn
->tenant_vrf_id
= tenant_vrf_id
;
4481 bgpevpn_link_to_l3vni(vpn
);
4484 /* If tunnel endpoint IP has changed, update (and delete prior
4485 * type-3 route, if needed.)
4487 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
4488 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
4490 /* Update all routes with new endpoint IP and/or export RT
4493 if (is_vni_live(vpn
))
4494 update_routes_for_vni(bgp
, vpn
);
4497 /* Create or update as appropriate. */
4499 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
);
4502 "%u: Failed to allocate VNI entry for VNI %u - at Add",
4508 /* if the VNI is live already, there is nothing more to do */
4509 if (is_vni_live(vpn
))
4512 /* Mark as "live" */
4513 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4515 /* tunnel is now active, add tunnel-ip to db */
4516 bgp_tip_add(bgp
, &originator_ip
);
4518 /* filter routes as nexthop database has changed */
4519 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
4521 /* Create EVPN type-3 route and schedule for processing. */
4522 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
4523 if (update_evpn_route(bgp
, vpn
, &p
, 0)) {
4524 zlog_err("%u: Type3 route creation failure for VNI %u",
4529 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
4533 install_routes_for_vni(bgp
, vpn
);
4535 /* If we are advertising gateway mac-ip
4536 It needs to be conveyed again to zebra */
4537 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
4543 * Cleanup EVPN information on disable - Need to delete and withdraw
4544 * EVPN routes from peers.
4546 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
4548 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
4549 void *))cleanup_vni_on_disable
,
4554 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
4555 * BGP instance (default) is being freed.
4557 void bgp_evpn_cleanup(struct bgp
*bgp
)
4560 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
4561 void *))free_vni_entry
,
4563 if (bgp
->import_rt_hash
)
4564 hash_free(bgp
->import_rt_hash
);
4565 bgp
->import_rt_hash
= NULL
;
4566 if (bgp
->vrf_import_rt_hash
)
4567 hash_free(bgp
->vrf_import_rt_hash
);
4568 bgp
->vrf_import_rt_hash
= NULL
;
4570 hash_free(bgp
->vnihash
);
4571 bgp
->vnihash
= NULL
;
4572 if (bgp
->vrf_import_rtl
)
4573 list_delete_and_null(&bgp
->vrf_import_rtl
);
4574 if (bgp
->vrf_export_rtl
)
4575 list_delete_and_null(&bgp
->vrf_export_rtl
);
4577 list_delete_and_null(&bgp
->l2vnis
);
4581 * Initialization for EVPN
4584 * hash for RT to VNI
4586 void bgp_evpn_init(struct bgp
*bgp
)
4589 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
4590 bgp
->import_rt_hash
=
4591 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
4592 "BGP Import RT Hash");
4593 bgp
->vrf_import_rt_hash
=
4594 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
4595 "BGP VRF Import RT Hash");
4596 bgp
->vrf_import_rtl
= list_new();
4597 bgp
->vrf_import_rtl
->cmp
=
4598 (int (*)(void *, void *))evpn_route_target_cmp
;
4600 bgp
->vrf_export_rtl
= list_new();
4601 bgp
->vrf_export_rtl
->cmp
=
4602 (int (*)(void *, void *))evpn_route_target_cmp
;
4603 bgp
->l2vnis
= list_new();
4604 bgp
->l2vnis
->cmp
= (int (*)(void *, void *))vni_hash_cmp
;
4607 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
4609 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);