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
35 #include "bgpd/bgp_attr_evpn.h"
36 #include "bgpd/bgpd.h"
37 #include "bgpd/bgp_table.h"
38 #include "bgpd/bgp_route.h"
39 #include "bgpd/bgp_attr.h"
40 #include "bgpd/bgp_mplsvpn.h"
41 #include "bgpd/bgp_label.h"
42 #include "bgpd/bgp_evpn.h"
43 #include "bgpd/bgp_evpn_private.h"
44 #include "bgpd/bgp_ecommunity.h"
45 #include "bgpd/bgp_encap_types.h"
46 #include "bgpd/bgp_debug.h"
47 #include "bgpd/bgp_aspath.h"
48 #include "bgpd/bgp_zebra.h"
49 #include "bgpd/bgp_nexthop.h"
52 * Definitions and external declarations.
54 extern struct zclient
*zclient
;
56 DEFINE_QOBJ_TYPE(bgpevpn
)
60 * Static function declarations
62 static void delete_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
63 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
64 struct bgp_info
**ri
);
65 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
74 static unsigned int vni_hash_key_make(void *p
)
76 struct bgpevpn
*vpn
= p
;
77 return (jhash_1word(vpn
->vni
, 0));
81 * Comparison function for vni hash
83 static int vni_hash_cmp(const void *p1
, const void *p2
)
85 const struct bgpevpn
*vpn1
= p1
;
86 const struct bgpevpn
*vpn2
= p2
;
92 return (vpn1
->vni
== vpn2
->vni
);
96 * Make vrf import route target hash key.
98 static unsigned int vrf_import_rt_hash_key_make(void *p
)
100 struct vrf_irt_node
*irt
= p
;
101 char *pnt
= irt
->rt
.val
;
103 return jhash(pnt
, 8, 0x5abc1234);
107 * Comparison function for vrf import rt hash
109 static int vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
111 const struct vrf_irt_node
*irt1
= p1
;
112 const struct vrf_irt_node
*irt2
= p2
;
114 if (irt1
== NULL
&& irt2
== NULL
)
117 if (irt1
== NULL
|| irt2
== NULL
)
120 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
124 * Create a new vrf import_rt in default instance
126 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
128 struct bgp
*bgp_def
= NULL
;
129 struct vrf_irt_node
*irt
;
131 bgp_def
= bgp_get_default();
133 zlog_err("vrf import rt new - def instance not created yet");
137 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
138 sizeof(struct vrf_irt_node
));
143 irt
->vrfs
= list_new();
146 if (!hash_get(bgp_def
->vrf_import_rt_hash
, irt
, hash_alloc_intern
)) {
147 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
155 * Free the vrf import rt node
157 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
159 struct bgp
*bgp_def
= NULL
;
161 bgp_def
= bgp_get_default();
163 zlog_err("vrf import rt free - def instance not created yet");
167 hash_release(bgp_def
->vrf_import_rt_hash
, irt
);
168 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
172 * Function to lookup Import RT node - used to map a RT to set of
173 * VNIs importing routes with that RT.
175 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
177 struct bgp
*bgp_def
= NULL
;
178 struct vrf_irt_node
*irt
;
179 struct vrf_irt_node tmp
;
181 bgp_def
= bgp_get_default();
183 zlog_err("vrf import rt lookup - def instance not created yet");
187 memset(&tmp
, 0, sizeof(struct vrf_irt_node
));
188 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
189 irt
= hash_lookup(bgp_def
->vrf_import_rt_hash
, &tmp
);
194 * Is specified VRF present on the RT's list of "importing" VRFs?
196 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
,
199 struct listnode
*node
= NULL
, *nnode
= NULL
;
200 struct bgp
*tmp_bgp_vrf
= NULL
;
202 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
203 if (tmp_bgp_vrf
== bgp_vrf
)
210 * Make import route target hash key.
212 static unsigned int import_rt_hash_key_make(void *p
)
214 struct irt_node
*irt
= p
;
215 char *pnt
= irt
->rt
.val
;
217 return jhash(pnt
, 8, 0xdeadbeef);
221 * Comparison function for import rt hash
223 static int import_rt_hash_cmp(const void *p1
, const void *p2
)
225 const struct irt_node
*irt1
= p1
;
226 const struct irt_node
*irt2
= p2
;
228 if (irt1
== NULL
&& irt2
== NULL
)
231 if (irt1
== NULL
|| irt2
== NULL
)
234 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
238 * Create a new import_rt
240 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
241 struct ecommunity_val
*rt
)
243 struct irt_node
*irt
;
248 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
253 irt
->vnis
= list_new();
256 if (!hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
)) {
257 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
265 * Free the import rt node
267 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
269 hash_release(bgp
->import_rt_hash
, irt
);
270 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
274 * Function to lookup Import RT node - used to map a RT to set of
275 * VNIs importing routes with that RT.
277 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
278 struct ecommunity_val
*rt
)
280 struct irt_node
*irt
;
283 memset(&tmp
, 0, sizeof(struct irt_node
));
284 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
285 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
290 * Is specified VNI present on the RT's list of "importing" VNIs?
292 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
294 struct listnode
*node
, *nnode
;
295 struct bgpevpn
*tmp_vpn
;
297 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
306 * Compare Route Targets.
308 static int evpn_route_target_cmp(struct ecommunity
*ecom1
,
309 struct ecommunity
*ecom2
)
317 if (!ecom1
&& !ecom2
)
320 if (ecom1
->str
&& !ecom2
->str
)
323 if (!ecom1
->str
&& ecom2
->str
)
326 if (!ecom1
->str
&& !ecom2
->str
)
329 return strcmp(ecom1
->str
, ecom2
->str
);
333 * Mask off global-admin field of specified extended community (RT),
334 * just retain the local-admin field.
336 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
337 struct ecommunity_val
*src
)
343 if (type
== ECOMMUNITY_ENCODE_AS
) {
344 dst
->val
[2] = dst
->val
[3] = 0;
345 } else if (type
== ECOMMUNITY_ENCODE_AS4
346 || type
== ECOMMUNITY_ENCODE_IP
) {
347 dst
->val
[2] = dst
->val
[3] = 0;
348 dst
->val
[4] = dst
->val
[5] = 0;
353 * Map one RT to specified VRF.
354 * bgp_vrf = BGP vrf instance
356 static void map_vrf_to_rt(struct bgp
*bgp_vrf
,
357 struct ecommunity_val
*eval
)
359 struct vrf_irt_node
*irt
= NULL
;
360 struct ecommunity_val eval_tmp
;
362 /* If using "automatic" RT,
363 * we only care about the local-admin sub-field.
364 * This is to facilitate using L3VNI(VRF-VNI)
365 * as the RT for EBGP peering too.
367 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
368 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
369 BGP_VRF_IMPORT_RT_CFGD
))
370 mask_ecom_global_admin(&eval_tmp
, eval
);
372 irt
= lookup_vrf_import_rt(&eval_tmp
);
373 if (irt
&& irt
->vrfs
)
374 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
375 /* Already mapped. */
379 irt
= vrf_import_rt_new(&eval_tmp
);
383 /* Add VRF to the list for this RT. */
384 listnode_add(irt
->vrfs
, bgp_vrf
);
388 * Unmap specified VRF from specified RT. If there are no other
389 * VRFs for this RT, then the RT hash is deleted.
390 * bgp_vrf: BGP VRF specific instance
392 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
,
393 struct vrf_irt_node
*irt
)
395 /* Delete VRF from list for this RT. */
396 listnode_delete(irt
->vrfs
, bgp_vrf
);
397 if (!listnode_head(irt
->vrfs
)) {
398 list_delete_and_null(&irt
->vrfs
);
399 vrf_import_rt_free(irt
);
404 * Map one RT to specified VNI.
406 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
407 struct ecommunity_val
*eval
)
409 struct irt_node
*irt
;
410 struct ecommunity_val eval_tmp
;
412 /* If using "automatic" RT, we only care about the local-admin
414 * This is to facilitate using VNI as the RT for EBGP peering too.
416 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
417 if (!is_import_rt_configured(vpn
))
418 mask_ecom_global_admin(&eval_tmp
, eval
);
420 irt
= lookup_import_rt(bgp
, &eval_tmp
);
421 if (irt
&& irt
->vnis
)
422 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
423 /* Already mapped. */
427 irt
= import_rt_new(bgp
, &eval_tmp
);
431 /* Add VNI to the hash list for this RT. */
432 listnode_add(irt
->vnis
, vpn
);
436 * Unmap specified VNI from specified RT. If there are no other
437 * VNIs for this RT, then the RT hash is deleted.
439 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
440 struct irt_node
*irt
)
442 /* Delete VNI from hash list for this RT. */
443 listnode_delete(irt
->vnis
, vpn
);
444 if (!listnode_head(irt
->vnis
)) {
445 list_delete_and_null(&irt
->vnis
);
446 import_rt_free(bgp
, irt
);
451 * Create RT extended community automatically from passed information:
452 * of the form AS:VNI.
453 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
454 * the need is to get a RT value that will be unique across different
455 * VNIs but the same across routers (in the same AS) for a particular
458 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
460 struct ecommunity_val eval
;
461 struct ecommunity
*ecomadd
;
463 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
465 ecomadd
= ecommunity_new();
466 ecommunity_add_val(ecomadd
, &eval
);
467 listnode_add_sort(rtl
, ecomadd
);
471 * Derive RD and RT for a VNI automatically. Invoked at the time of
474 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
476 bgp_evpn_derive_auto_rd(bgp
, vpn
);
477 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
478 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
482 * Convert nexthop (remote VTEP IP) into an IPv6 address.
484 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
486 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
488 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
489 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
493 * Add (update) or delete MACIP from zebra.
495 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
496 struct prefix_evpn
*p
,
497 struct in_addr remote_vtep_ip
, int add
,
502 char buf1
[ETHER_ADDR_STRLEN
];
503 char buf2
[INET6_ADDRSTRLEN
];
504 char buf3
[INET6_ADDRSTRLEN
];
507 if (!zclient
|| zclient
->sock
< 0)
510 /* Don't try to register if Zebra doesn't know of this instance. */
511 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
517 zclient_create_header(s
, add
? ZEBRA_REMOTE_MACIP_ADD
518 : ZEBRA_REMOTE_MACIP_DEL
,
520 stream_putl(s
, vpn
->vni
);
521 stream_put(s
, &p
->prefix
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
522 /* IP address length and IP address, if any. */
523 if (IS_EVPN_PREFIX_IPADDR_NONE(p
))
526 ipa_len
= IS_EVPN_PREFIX_IPADDR_V4(p
) ? IPV4_MAX_BYTELEN
528 stream_putl(s
, ipa_len
);
529 stream_put(s
, &p
->prefix
.ip
.ip
.addr
, ipa_len
);
531 stream_put_in_addr(s
, &remote_vtep_ip
);
533 /* TX flags - MAC sticky status and/or gateway mac */
535 stream_putc(s
, flags
);
537 stream_putw_at(s
, 0, stream_get_endp(s
));
539 if (bgp_debug_zebra(NULL
))
540 zlog_debug("Tx %s MACIP, VNI %u MAC %s IP %s (flags: 0x%x) remote VTEP %s",
541 add
? "ADD" : "DEL", vpn
->vni
,
542 prefix_mac2str(&p
->prefix
.mac
, buf1
, sizeof(buf1
)),
543 ipaddr2str(&p
->prefix
.ip
, buf3
, sizeof(buf3
)),
545 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
548 return zclient_send_message(zclient
);
552 * Add (update) or delete remote VTEP from zebra.
554 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
555 struct prefix_evpn
*p
, int add
)
560 if (!zclient
|| zclient
->sock
< 0)
563 /* Don't try to register if Zebra doesn't know of this instance. */
564 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
570 zclient_create_header(s
, add
? ZEBRA_REMOTE_VTEP_ADD
571 : ZEBRA_REMOTE_VTEP_DEL
,
573 stream_putl(s
, vpn
->vni
);
574 if (IS_EVPN_PREFIX_IPADDR_V4(p
))
575 stream_put_in_addr(s
, &p
->prefix
.ip
.ipaddr_v4
);
576 else if (IS_EVPN_PREFIX_IPADDR_V6(p
)) {
578 "Bad remote IP when trying to %s remote VTEP for VNI %u",
579 add
? "ADD" : "DEL", vpn
->vni
);
583 stream_putw_at(s
, 0, stream_get_endp(s
));
585 if (bgp_debug_zebra(NULL
))
586 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
587 add
? "ADD" : "DEL", vpn
->vni
,
588 inet_ntoa(p
->prefix
.ip
.ipaddr_v4
));
590 return zclient_send_message(zclient
);
594 * Build extended communities for EVPN prefix route.
596 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
599 struct ecommunity ecom_encap
;
600 struct ecommunity ecom_rmac
;
601 struct ecommunity_val eval
;
602 struct ecommunity_val eval_rmac
;
603 bgp_encap_types tnl_type
;
604 struct listnode
*node
, *nnode
;
605 struct ecommunity
*ecom
;
606 struct list
*vrf_export_rtl
= NULL
;
609 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
610 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
611 encode_encap_extcomm(tnl_type
, &eval
);
613 ecom_encap
.val
= (u_int8_t
*)eval
.val
;
616 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
618 /* Add the export RTs for L3VNI/VRF */
619 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
620 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
621 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
622 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
626 /* add the router mac extended community */
627 if (!is_zero_mac(&attr
->rmac
)) {
628 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
629 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
631 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
632 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
636 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
640 * Build extended communities for EVPN route.
641 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
642 * and ENCAP extended communities are applicable for all routes.
643 * The default gateway extended community and MAC mobility (sticky) extended
644 * community are added as needed based on passed settings - only for type-2
645 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
646 * added, if present, based on passed settings - only for non-link-local
649 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
652 struct ecommunity ecom_encap
;
653 struct ecommunity ecom_sticky
;
654 struct ecommunity ecom_default_gw
;
655 struct ecommunity ecom_rmac
;
656 struct ecommunity_val eval
;
657 struct ecommunity_val eval_sticky
;
658 struct ecommunity_val eval_default_gw
;
659 struct ecommunity_val eval_rmac
;
660 bgp_encap_types tnl_type
;
661 struct listnode
*node
, *nnode
;
662 struct ecommunity
*ecom
;
664 struct list
*vrf_export_rtl
= NULL
;
667 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
668 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
669 encode_encap_extcomm(tnl_type
, &eval
);
671 ecom_encap
.val
= (u_int8_t
*)eval
.val
;
674 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
676 /* Add the export RTs for L2VNI */
677 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
678 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
680 /* Add the export RTs for L3VNI if told to - caller determines
681 * when this should be done.
684 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
685 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
686 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
689 ecommunity_merge(attr
->ecommunity
,
694 /* Add MAC mobility (sticky) if needed. */
697 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
698 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
699 ecom_sticky
.size
= 1;
700 ecom_sticky
.val
= (u_int8_t
*)eval_sticky
.val
;
702 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
705 /* Add RMAC, if told to. */
707 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
708 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
710 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
711 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
715 /* Add default gateway, if needed. */
716 if (attr
->default_gw
) {
717 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
718 encode_default_gw_extcomm(&eval_default_gw
);
719 ecom_default_gw
.size
= 1;
720 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
721 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
725 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
729 * Add MAC mobility extended community to attribute.
731 static void add_mac_mobility_to_attr(u_int32_t seq_num
, struct attr
*attr
)
733 struct ecommunity ecom_tmp
;
734 struct ecommunity_val eval
;
735 u_int8_t
*ecom_val_ptr
;
742 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
744 /* Find current MM ecommunity */
747 if (attr
->ecommunity
) {
748 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
749 pnt
= attr
->ecommunity
->val
+ (i
* 8);
753 if (type
== ECOMMUNITY_ENCODE_EVPN
755 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
757 (u_int8_t
*)(attr
->ecommunity
->val
764 /* Update the existing MM ecommunity */
766 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
768 /* Add MM to existing */
770 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
772 ecom_tmp
.val
= (u_int8_t
*)eval
.val
;
775 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
779 /* Install EVPN route into zebra. */
780 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
781 struct prefix_evpn
*p
,
782 struct in_addr remote_vtep_ip
, u_char flags
)
786 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
787 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
790 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 1);
795 /* Uninstall EVPN route from zebra. */
796 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
797 struct prefix_evpn
*p
,
798 struct in_addr remote_vtep_ip
)
802 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
803 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
806 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 0);
812 * Due to MAC mobility, the prior "local" best route has been supplanted
813 * by a "remote" best route. The prior route has to be deleted and withdrawn
816 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
818 struct bgp_info
*old_local
)
820 struct bgp_node
*global_rn
;
822 afi_t afi
= AFI_L2VPN
;
823 safi_t safi
= SAFI_EVPN
;
825 /* Locate route node in the global EVPN routing table. Note that
826 * this table is a 2-level tree (RD-level + Prefix-level) similar to
829 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
830 (struct prefix
*)&rn
->p
, &vpn
->prd
);
832 /* Delete route entry in the global EVPN table. */
833 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
835 /* Schedule for processing - withdraws to peers happen from
839 bgp_process(bgp
, global_rn
, afi
, safi
);
840 bgp_unlock_node(global_rn
);
843 /* Delete route entry in the VNI route table, caller to remove. */
844 bgp_info_delete(rn
, old_local
);
848 * Calculate the best path for an EVPN route. Install/update best path in zebra,
851 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
854 struct bgp_info
*old_select
, *new_select
;
855 struct bgp_info_pair old_and_new
;
856 afi_t afi
= AFI_L2VPN
;
857 safi_t safi
= SAFI_EVPN
;
861 /* Compute the best path. */
862 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
864 old_select
= old_and_new
.old
;
865 new_select
= old_and_new
.new;
867 /* If the best path hasn't changed - see if there is still something to
871 if (old_select
&& old_select
== new_select
872 && old_select
->type
== ZEBRA_ROUTE_BGP
873 && old_select
->sub_type
== BGP_ROUTE_NORMAL
874 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
875 && !CHECK_FLAG(old_select
->flags
, BGP_INFO_ATTR_CHANGED
)
876 && !bgp
->addpath_tx_used
[afi
][safi
]) {
877 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
878 if (old_select
->attr
->sticky
)
879 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
880 if (old_select
->attr
->default_gw
)
881 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
882 ret
= evpn_zebra_install(bgp
, vpn
,
883 (struct prefix_evpn
*)&rn
->p
,
884 old_select
->attr
->nexthop
,
887 UNSET_FLAG(old_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
888 bgp_zebra_clear_route_change_flags(rn
);
892 /* If the user did a "clear" this flag will be set */
893 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
895 /* bestpath has changed; update relevant fields and install or uninstall
896 * into the zebra RIB.
898 if (old_select
|| new_select
)
899 bgp_bump_version(rn
);
902 bgp_info_unset_flag(rn
, old_select
, BGP_INFO_SELECTED
);
904 bgp_info_set_flag(rn
, new_select
, BGP_INFO_SELECTED
);
905 bgp_info_unset_flag(rn
, new_select
, BGP_INFO_ATTR_CHANGED
);
906 UNSET_FLAG(new_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
909 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
910 && new_select
->sub_type
== BGP_ROUTE_NORMAL
) {
912 if (new_select
->attr
->sticky
)
913 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
914 if (new_select
->attr
->default_gw
)
915 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
916 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
917 new_select
->attr
->nexthop
,
919 /* If an old best existed and it was a "local" route, the only
921 * it would be supplanted is due to MAC mobility procedures. So,
923 * need to do an implicit delete and withdraw that route from
926 if (old_select
&& old_select
->peer
== bgp
->peer_self
927 && old_select
->type
== ZEBRA_ROUTE_BGP
928 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
929 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
931 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
932 && old_select
->sub_type
== BGP_ROUTE_NORMAL
)
933 ret
= evpn_zebra_uninstall(bgp
, vpn
,
934 (struct prefix_evpn
*)&rn
->p
,
935 old_select
->attr
->nexthop
);
938 /* Clear any route change flags. */
939 bgp_zebra_clear_route_change_flags(rn
);
941 /* Reap old select bgp_info, if it has been removed */
942 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_INFO_REMOVED
))
943 bgp_info_reap(rn
, old_select
);
949 * Return true if the local ri for this rn is of type gateway mac
951 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
953 struct bgp_info
*tmp_ri
= NULL
;
954 struct bgp_info
*local_ri
= NULL
;
957 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
958 if (tmp_ri
->peer
== bgp
->peer_self
959 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
960 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
967 return local_ri
->attr
->default_gw
;
972 * Return true if the local ri for this rn has sticky set
974 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
976 struct bgp_info
*tmp_ri
;
977 struct bgp_info
*local_ri
;
980 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
981 if (tmp_ri
->peer
== bgp
->peer_self
982 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
983 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
990 return local_ri
->attr
->sticky
;
993 static int update_evpn_type5_route_entry(struct bgp
*bgp_def
,
994 struct bgp
*bgp_vrf
, afi_t afi
,
995 safi_t safi
, struct bgp_node
*rn
,
996 struct attr
*attr
, int *route_changed
)
998 struct attr
*attr_new
= NULL
;
999 struct bgp_info
*ri
= NULL
;
1000 mpls_label_t label
= MPLS_INVALID_LABEL
;
1001 struct bgp_info
*local_ri
= NULL
;
1002 struct bgp_info
*tmp_ri
= NULL
;
1005 /* locate the local route entry if any */
1006 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1007 if (tmp_ri
->peer
== bgp_def
->peer_self
1008 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1009 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1013 /* create a new route entry if one doesnt exist.
1014 Otherwise see if route attr has changed
1018 /* route has changed as this is the first entry */
1021 /* Add (or update) attribute to hash. */
1022 attr_new
= bgp_attr_intern(attr
);
1024 /* create the route info from attribute */
1025 ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1026 bgp_def
->peer_self
, attr_new
, rn
);
1027 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1029 /* Type-5 routes advertise the L3-VNI */
1030 bgp_info_extra_get(ri
);
1031 vni2label(bgp_vrf
->l3vni
, &label
);
1032 memcpy(&ri
->extra
->label
, &label
, sizeof(label
));
1033 ri
->extra
->num_labels
= 1;
1035 /* add the route entry to route node*/
1036 bgp_info_add(rn
, ri
);
1040 if (!attrhash_cmp(tmp_ri
->attr
, attr
)) {
1042 /* attribute changed */
1045 /* The attribute has changed. */
1046 /* Add (or update) attribute to hash. */
1047 attr_new
= bgp_attr_intern(attr
);
1048 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1050 /* Restore route, if needed. */
1051 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1052 bgp_info_restore(rn
, tmp_ri
);
1054 /* Unintern existing, set to new. */
1055 bgp_attr_unintern(&tmp_ri
->attr
);
1056 tmp_ri
->attr
= attr_new
;
1057 tmp_ri
->uptime
= bgp_clock();
1063 /* update evpn type-5 route entry */
1064 static int update_evpn_type5_route(struct bgp
*bgp_vrf
,
1065 struct prefix_evpn
*evp
,
1066 struct attr
* src_attr
)
1068 afi_t afi
= AFI_L2VPN
;
1069 safi_t safi
= SAFI_EVPN
;
1071 struct bgp_node
*rn
= NULL
;
1072 struct bgp
*bgp_def
= NULL
;
1073 int route_changed
= 0;
1075 bgp_def
= bgp_get_default();
1079 /* Build path attribute for this route - use the source attr, if
1080 * present, else treat as locally originated.
1083 bgp_attr_dup(&attr
, src_attr
);
1085 memset(&attr
, 0, sizeof(struct attr
));
1086 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1088 /* Set nexthop to ourselves and fill in the Router MAC. */
1089 attr
.nexthop
= bgp_vrf
->originator_ip
;
1090 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1091 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1092 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
1094 /* Setup RT and encap extended community */
1095 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1097 /* get the route node in global table */
1098 rn
= bgp_afi_node_get(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1099 (struct prefix
*)evp
,
1103 /* create or update the route entry within the route node */
1104 update_evpn_type5_route_entry(bgp_def
, bgp_vrf
,
1106 rn
, &attr
, &route_changed
);
1108 /* schedule for processing and unlock node */
1109 if (route_changed
) {
1110 bgp_process(bgp_def
, rn
, afi
, safi
);
1111 bgp_unlock_node(rn
);
1114 /* uninten temporary */
1116 aspath_unintern(&attr
.aspath
);
1121 * Create or update EVPN route entry. This could be in the VNI route table
1122 * or the global route table.
1124 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1125 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1126 struct attr
*attr
, int add
, int vni_table
,
1127 struct bgp_info
**ri
, u_char flags
)
1129 struct bgp_info
*tmp_ri
;
1130 struct bgp_info
*local_ri
, *remote_ri
;
1131 struct attr
*attr_new
;
1132 mpls_label_t label
[BGP_MAX_LABELS
];
1133 u_int32_t num_labels
= 1;
1134 int route_change
= 1;
1136 struct prefix_evpn
*evp
;
1139 evp
= (struct prefix_evpn
*)&rn
->p
;
1140 memset(&label
, 0, sizeof(label
));
1142 /* See if this is an update of an existing route, or a new add. Also,
1143 * identify if already known from remote, and if so, the one with the
1144 * highest sequence number; this is only when adding to the VNI routing
1147 local_ri
= remote_ri
= NULL
;
1148 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1149 if (tmp_ri
->peer
== bgp
->peer_self
1150 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1151 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1154 if (tmp_ri
->type
== ZEBRA_ROUTE_BGP
1155 && tmp_ri
->sub_type
== BGP_ROUTE_NORMAL
1156 && CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
)) {
1159 else if (mac_mobility_seqnum(tmp_ri
->attr
)
1160 > mac_mobility_seqnum(remote_ri
->attr
))
1166 /* If route doesn't exist already, create a new one, if told to.
1167 * Otherwise act based on whether the attributes of the route have
1170 if (!local_ri
&& !add
)
1174 /* When learnt locally for the first time but already known from
1175 * remote, we have to initiate appropriate MAC mobility steps.
1177 * is applicable when updating the VNI routing table.
1178 * We need to skip mobility steps for g/w macs (local mac on g/w
1179 * SVI) advertised in EVPN.
1180 * This will ensure that local routes are preferred for g/w macs
1182 if (remote_ri
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
)) {
1183 u_int32_t cur_seqnum
;
1185 /* Add MM extended community to route. */
1186 cur_seqnum
= mac_mobility_seqnum(remote_ri
->attr
);
1187 add_mac_mobility_to_attr(cur_seqnum
+ 1, attr
);
1190 /* Add (or update) attribute to hash. */
1191 attr_new
= bgp_attr_intern(attr
);
1193 /* Extract MAC mobility sequence number, if any. */
1194 attr_new
->mm_seqnum
=
1195 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1196 attr_new
->sticky
= sticky
;
1198 /* Create new route with its attribute. */
1199 tmp_ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1200 bgp
->peer_self
, attr_new
, rn
);
1201 SET_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
);
1202 bgp_info_extra_get(tmp_ri
);
1204 /* The VNI goes into the 'label' field of the route */
1205 vni2label(vpn
->vni
, &label
[0]);
1207 /* Type-2 routes may carry a second VNI - the L3-VNI.
1208 * Only attach second label if we are advertising two labels for
1211 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1212 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1215 l3vni
= bgpevpn_get_l3vni(vpn
);
1217 vni2label(l3vni
, &label
[1]);
1222 memcpy(&tmp_ri
->extra
->label
, label
, sizeof(label
));
1223 tmp_ri
->extra
->num_labels
= num_labels
;
1224 bgp_info_add(rn
, tmp_ri
);
1227 if (attrhash_cmp(tmp_ri
->attr
, attr
)
1228 && !CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1232 * The attributes have changed, type-2 routes needs to
1233 * be advertised with right labels.
1235 vni2label(vpn
->vni
, &label
[0]);
1236 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1237 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1240 l3vni
= bgpevpn_get_l3vni(vpn
);
1242 vni2label(l3vni
, &label
[1]);
1246 memcpy(&tmp_ri
->extra
->label
, label
, sizeof(label
));
1247 tmp_ri
->extra
->num_labels
= num_labels
;
1249 /* The attribute has changed. */
1250 /* Add (or update) attribute to hash. */
1251 attr_new
= bgp_attr_intern(attr
);
1252 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1254 /* Restore route, if needed. */
1255 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1256 bgp_info_restore(rn
, tmp_ri
);
1258 /* Unintern existing, set to new. */
1259 bgp_attr_unintern(&tmp_ri
->attr
);
1260 tmp_ri
->attr
= attr_new
;
1261 tmp_ri
->uptime
= bgp_clock();
1265 /* Return back the route entry. */
1267 return route_change
;
1271 * Create or update EVPN route (of type based on prefix) for specified VNI
1272 * and schedule for processing.
1274 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1275 struct prefix_evpn
*p
, u_char flags
)
1277 struct bgp_node
*rn
;
1279 struct attr
*attr_new
;
1280 int add_l3_ecomm
= 0;
1281 struct bgp_info
*ri
;
1282 afi_t afi
= AFI_L2VPN
;
1283 safi_t safi
= SAFI_EVPN
;
1286 memset(&attr
, 0, sizeof(struct attr
));
1288 /* Build path-attribute for this route. */
1289 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1290 attr
.nexthop
= vpn
->originator_ip
;
1291 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1292 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1293 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1294 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1296 /* PMSI is only needed for type-3 routes */
1297 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
)
1298 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1300 /* router mac is only needed for type-2 routes here. */
1301 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1302 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1303 vni2label(vpn
->vni
, &(attr
.label
));
1305 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1306 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1309 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1310 (IS_EVPN_PREFIX_IPADDR_V4(p
) ||
1311 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.ip
.ipaddr_v6
)) &&
1312 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
))
1315 /* Set up extended community. */
1316 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1318 /* First, create (or fetch) route node within the VNI. */
1319 /* NOTE: There is no RD here. */
1320 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1322 /* Create or update route entry. */
1323 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1326 attr_new
= ri
->attr
;
1328 /* Perform route selection; this is just to set the flags correctly
1329 * as local route in the VNI always wins.
1331 evpn_route_select_install(bgp
, vpn
, rn
);
1332 bgp_unlock_node(rn
);
1334 /* If this is a new route or some attribute has changed, export the
1335 * route to the global table. The route will be advertised to peers
1336 * from there. Note that this table is a 2-level tree (RD-level +
1337 * Prefix-level) similar to L3VPN routes.
1340 struct bgp_info
*global_ri
;
1342 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1343 (struct prefix
*)p
, &vpn
->prd
);
1344 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1, 0,
1347 /* Schedule for processing and unlock node. */
1348 bgp_process(bgp
, rn
, afi
, safi
);
1349 bgp_unlock_node(rn
);
1352 /* Unintern temporary. */
1353 aspath_unintern(&attr
.aspath
);
1358 /* Delete EVPN type5 route entry from global table */
1359 static void delete_evpn_type5_route_entry(struct bgp
*bgp_def
,
1360 struct bgp
*bgp_vrf
,
1361 afi_t afi
, safi_t safi
,
1362 struct bgp_node
*rn
,
1363 struct bgp_info
**ri
)
1365 struct bgp_info
*tmp_ri
= NULL
;
1369 /* find the matching route entry */
1370 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
)
1371 if (tmp_ri
->peer
== bgp_def
->peer_self
1372 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1373 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1378 /* Mark route for delete. */
1380 bgp_info_delete(rn
, tmp_ri
);
1383 /* Delete EVPN type5 route */
1384 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
,
1385 struct prefix_evpn
*evp
)
1387 afi_t afi
= AFI_L2VPN
;
1388 safi_t safi
= SAFI_EVPN
;
1389 struct bgp_node
*rn
= NULL
;
1390 struct bgp_info
*ri
= NULL
;
1391 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
1393 bgp_def
= bgp_get_default();
1397 /* locate the global route entry for this type-5 prefix */
1398 rn
= bgp_afi_node_lookup(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1399 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1403 delete_evpn_type5_route_entry(bgp_def
, bgp_vrf
, afi
, safi
, rn
, &ri
);
1405 bgp_process(bgp_def
, rn
, afi
, safi
);
1406 bgp_unlock_node(rn
);
1411 * Delete EVPN route entry. This could be in the VNI route table
1412 * or the global route table.
1414 static void delete_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1415 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1416 struct bgp_info
**ri
)
1418 struct bgp_info
*tmp_ri
;
1422 /* Now, find matching route. */
1423 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
)
1424 if (tmp_ri
->peer
== bgp
->peer_self
1425 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1426 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1431 /* Mark route for delete. */
1433 bgp_info_delete(rn
, tmp_ri
);
1437 * Delete EVPN route (of type based on prefix) for specified VNI and
1438 * schedule for processing.
1440 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1441 struct prefix_evpn
*p
)
1443 struct bgp_node
*rn
, *global_rn
;
1444 struct bgp_info
*ri
;
1445 afi_t afi
= AFI_L2VPN
;
1446 safi_t safi
= SAFI_EVPN
;
1448 /* First, locate the route node within the VNI. If it doesn't exist,
1450 * is nothing further to do.
1452 /* NOTE: There is no RD here. */
1453 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1457 /* Next, locate route node in the global EVPN routing table. Note that
1458 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1461 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1462 (struct prefix
*)p
, &vpn
->prd
);
1464 /* Delete route entry in the global EVPN table. */
1465 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
1467 /* Schedule for processing - withdraws to peers happen from
1471 bgp_process(bgp
, global_rn
, afi
, safi
);
1472 bgp_unlock_node(global_rn
);
1475 /* Delete route entry in the VNI route table. This can just be removed.
1477 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1479 bgp_info_reap(rn
, ri
);
1480 bgp_unlock_node(rn
);
1486 * Update all type-2 (MACIP) local routes for this VNI - these should also
1487 * be scheduled for advertise to peers.
1489 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1493 struct bgp_node
*rn
;
1494 struct bgp_info
*ri
;
1496 struct attr attr_sticky
;
1497 struct attr attr_def_gw
;
1498 struct attr attr_ip6_ll
;
1499 struct attr
*attr_new
;
1500 int add_l3_ecomm
= 0;
1504 memset(&attr
, 0, sizeof(struct attr
));
1505 memset(&attr_sticky
, 0, sizeof(struct attr
));
1506 memset(&attr_def_gw
, 0, sizeof(struct attr
));
1507 memset(&attr_ip6_ll
, 0, sizeof(struct attr
));
1509 /* Build path-attribute - multiple type-2 routes for this VNI will share
1510 * the same path attribute, but we need separate structures for sticky
1511 * MACs, default gateway and IPv6 link-local addresses (no L3 RT/RMAC).
1513 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1514 bgp_attr_default_set(&attr_sticky
, BGP_ORIGIN_IGP
);
1515 bgp_attr_default_set(&attr_def_gw
, BGP_ORIGIN_IGP
);
1516 attr
.nexthop
= vpn
->originator_ip
;
1517 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1518 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1519 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1520 attr_sticky
.nexthop
= vpn
->originator_ip
;
1521 attr_sticky
.mp_nexthop_global_in
= vpn
->originator_ip
;
1522 attr_sticky
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1523 attr_sticky
.sticky
= 1;
1524 bgpevpn_get_rmac(vpn
, &attr_sticky
.rmac
);
1525 attr_def_gw
.nexthop
= vpn
->originator_ip
;
1526 attr_def_gw
.mp_nexthop_global_in
= vpn
->originator_ip
;
1527 attr_def_gw
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1528 attr_def_gw
.default_gw
= 1;
1529 bgpevpn_get_rmac(vpn
, &attr_def_gw
.rmac
);
1530 bgp_attr_default_set(&attr_ip6_ll
, BGP_ORIGIN_IGP
);
1531 attr_ip6_ll
.nexthop
= vpn
->originator_ip
;
1532 attr_ip6_ll
.mp_nexthop_global_in
= vpn
->originator_ip
;
1533 attr_ip6_ll
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1535 /* Add L3 VNI RTs and RMAC for non IPv6 link-local attributes if
1536 * using L3 VNI for type-2 routes also.
1538 if (CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
))
1541 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1542 build_evpn_route_extcomm(vpn
, &attr_sticky
, add_l3_ecomm
);
1543 build_evpn_route_extcomm(vpn
, &attr_def_gw
, add_l3_ecomm
);
1544 build_evpn_route_extcomm(vpn
, &attr_ip6_ll
, 0);
1546 /* Walk this VNI's route table and update local type-2 routes. For any
1547 * routes updated, update corresponding entry in the global table too.
1549 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1550 rn
= bgp_route_next(rn
)) {
1551 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1552 struct bgp_node
*rd_rn
;
1553 struct bgp_info
*global_ri
;
1555 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1558 if (IS_EVPN_PREFIX_IPADDR_V6(evp
) &&
1559 IN6_IS_ADDR_LINKLOCAL(&evp
->prefix
.ip
.ipaddr_v6
))
1560 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1561 &attr_ip6_ll
, 0, 1, &ri
, 0);
1563 if (evpn_route_is_sticky(bgp
, rn
))
1564 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1567 else if (evpn_route_is_def_gw(bgp
, rn
))
1568 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1572 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1573 &attr
, 0, 1, &ri
, 0);
1576 /* If a local route exists for this prefix, we need to update
1577 * the global routing table too.
1582 /* Perform route selection; this is just to set the flags
1584 * as local route in the VNI always wins.
1586 evpn_route_select_install(bgp
, vpn
, rn
);
1588 attr_new
= ri
->attr
;
1590 /* Update route in global routing table. */
1591 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1592 (struct prefix
*)evp
, &vpn
->prd
);
1594 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
1597 /* Schedule for processing and unlock node. */
1598 bgp_process(bgp
, rd_rn
, afi
, safi
);
1599 bgp_unlock_node(rd_rn
);
1602 /* Unintern temporary. */
1603 aspath_unintern(&attr
.aspath
);
1604 aspath_unintern(&attr_sticky
.aspath
);
1605 aspath_unintern(&attr_def_gw
.aspath
);
1606 aspath_unintern(&attr_ip6_ll
.aspath
);
1612 * Delete all type-2 (MACIP) local routes for this VNI - only from the
1613 * global routing table. These are also scheduled for withdraw from peers.
1615 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1619 struct bgp_node
*rdrn
, *rn
;
1620 struct bgp_table
*table
;
1621 struct bgp_info
*ri
;
1626 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
1627 if (rdrn
&& rdrn
->info
) {
1628 table
= (struct bgp_table
*)rdrn
->info
;
1629 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
1630 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1632 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1635 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1637 bgp_process(bgp
, rn
, afi
, safi
);
1641 /* Unlock RD node. */
1643 bgp_unlock_node(rdrn
);
1649 * Delete all type-2 (MACIP) local routes for this VNI - from the global
1650 * table as well as the per-VNI route table.
1652 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1656 struct bgp_node
*rn
;
1657 struct bgp_info
*ri
;
1662 /* First, walk the global route table for this VNI's type-2 local
1664 * EVPN routes are a 2-level table, first get the RD table.
1666 delete_global_type2_routes(bgp
, vpn
);
1668 /* Next, walk this VNI's route table and delete local type-2 routes. */
1669 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1670 rn
= bgp_route_next(rn
)) {
1671 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1673 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1676 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1678 /* Route entry in local table gets deleted immediately. */
1680 bgp_info_reap(rn
, ri
);
1687 * Delete all routes in the per-VNI route table.
1689 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1691 struct bgp_node
*rn
;
1692 struct bgp_info
*ri
, *nextri
;
1694 /* Walk this VNI's route table and delete all routes. */
1695 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1696 rn
= bgp_route_next(rn
)) {
1697 for (ri
= rn
->info
; (ri
!= NULL
) && (nextri
= ri
->next
, 1);
1699 bgp_info_delete(rn
, ri
);
1700 bgp_info_reap(rn
, ri
);
1708 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
1709 * export RT getting modified or change to tunnel IP. Note that these
1710 * situations need the route in the per-VNI table as well as the global
1711 * table to be updated (as attributes change).
1713 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1716 struct prefix_evpn p
;
1718 /* Update and advertise the type-3 route (only one) followed by the
1719 * locally learnt type-2 routes (MACIP) - for this VNI.
1721 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1722 ret
= update_evpn_route(bgp
, vpn
, &p
, 0);
1726 return update_all_type2_routes(bgp
, vpn
);
1730 * Delete (and withdraw) local routes for specified VNI from the global
1731 * table and per-VNI table. After this, remove all other routes from
1732 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
1733 * (advertise-all-vni) being disabled.
1735 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1738 struct prefix_evpn p
;
1740 /* Delete and withdraw locally learnt type-2 routes (MACIP)
1741 * followed by type-3 routes (only one) - for this VNI.
1743 ret
= delete_all_type2_routes(bgp
, vpn
);
1747 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1748 ret
= delete_evpn_route(bgp
, vpn
, &p
);
1752 /* Delete all routes from the per-VNI table. */
1753 return delete_all_vni_routes(bgp
, vpn
);
1757 * There is a tunnel endpoint IP address change for this VNI, delete
1758 * prior type-3 route (if needed) and update.
1759 * Note: Route re-advertisement happens elsewhere after other processing
1762 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1763 struct in_addr originator_ip
)
1765 struct prefix_evpn p
;
1767 /* If VNI is not live, we only need to update the originator ip */
1768 if (!is_vni_live(vpn
)) {
1769 vpn
->originator_ip
= originator_ip
;
1773 /* Update the tunnel-ip hash */
1774 bgp_tip_del(bgp
, &vpn
->originator_ip
);
1775 bgp_tip_add(bgp
, &originator_ip
);
1777 /* filter routes as martian nexthop db has changed */
1778 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
1780 /* Need to withdraw type-3 route as the originator IP is part
1783 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1784 delete_evpn_route(bgp
, vpn
, &p
);
1786 /* Update the tunnel IP and re-advertise all routes for this VNI. */
1787 vpn
->originator_ip
= originator_ip
;
1792 * Install route entry into the VRF routing table and invoke route selection.
1794 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
1795 struct prefix_evpn
*evp
,
1796 struct bgp_info
*parent_ri
)
1798 struct bgp_node
*rn
;
1799 struct bgp_info
*ri
;
1801 struct attr
*attr_new
;
1804 struct prefix
*pp
= &p
;
1807 char buf
[PREFIX_STRLEN
];
1808 char buf1
[PREFIX_STRLEN
];
1810 memset(pp
, 0, sizeof(struct prefix
));
1811 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1812 ip_prefix_from_type2_prefix(evp
, pp
);
1813 else if (evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
)
1814 ip_prefix_from_type5_prefix(evp
, pp
);
1816 if (bgp_debug_zebra(NULL
)) {
1817 zlog_debug("installing evpn prefix %s as ip prefix %s in vrf %s",
1818 prefix2str(evp
, buf
, sizeof(buf
)),
1819 prefix2str(pp
, buf1
, sizeof(buf
)),
1820 vrf_id_to_name(bgp_vrf
->vrf_id
));
1823 /* Create (or fetch) route within the VRF. */
1824 /* NOTE: There is no RD here. */
1825 if (IS_EVPN_PREFIX_IPADDR_V4(evp
)) {
1827 safi
= SAFI_UNICAST
;
1828 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
1829 } else if (IS_EVPN_PREFIX_IPADDR_V6(evp
)) {
1831 safi
= SAFI_UNICAST
;
1832 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
1836 /* EVPN routes currently only support a IPv4 next hop which corresponds
1837 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
1838 * route, we have to convert the next hop to an IPv4-mapped address
1839 * for the rest of the code to flow through.
1841 bgp_attr_dup(&attr
, parent_ri
->attr
);
1843 evpn_convert_nexthop_to_ipv6(&attr
);
1845 /* Check if route entry is already present. */
1846 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1848 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
1852 /* Add (or update) attribute to hash. */
1853 attr_new
= bgp_attr_intern(&attr
);
1855 /* Create new route with its attribute. */
1856 ri
= info_make(parent_ri
->type
, parent_ri
->sub_type
, 0,
1857 parent_ri
->peer
, attr_new
, rn
);
1858 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1859 bgp_info_extra_get(ri
);
1860 ri
->extra
->parent
= parent_ri
;
1861 if (parent_ri
->extra
) {
1862 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
1863 sizeof(ri
->extra
->label
));
1864 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
1866 bgp_info_add(rn
, ri
);
1868 if (attrhash_cmp(ri
->attr
, &attr
)
1869 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
1870 bgp_unlock_node(rn
);
1873 /* The attribute has changed. */
1874 /* Add (or update) attribute to hash. */
1875 attr_new
= bgp_attr_intern(&attr
);
1877 /* Restore route, if needed. */
1878 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
1879 bgp_info_restore(rn
, ri
);
1881 /* Mark if nexthop has changed. */
1882 if ((afi
== AFI_IP
&&
1883 !IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
)) ||
1885 !IPV6_ADDR_SAME(&ri
->attr
->mp_nexthop_global
,
1886 &attr_new
->mp_nexthop_global
)))
1887 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
1889 /* Unintern existing, set to new. */
1890 bgp_attr_unintern(&ri
->attr
);
1891 ri
->attr
= attr_new
;
1892 ri
->uptime
= bgp_clock();
1895 /* Perform route selection and update zebra, if required. */
1896 bgp_process(bgp_vrf
, rn
, afi
, safi
);
1902 * Install route entry into the VNI routing table and invoke route selection.
1904 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1905 struct prefix_evpn
*p
,
1906 struct bgp_info
*parent_ri
)
1908 struct bgp_node
*rn
;
1909 struct bgp_info
*ri
;
1910 struct attr
*attr_new
;
1913 /* Create (or fetch) route within the VNI. */
1914 /* NOTE: There is no RD here. */
1915 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1917 /* Check if route entry is already present. */
1918 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1920 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
1924 /* Add (or update) attribute to hash. */
1925 attr_new
= bgp_attr_intern(parent_ri
->attr
);
1927 /* Create new route with its attribute. */
1928 ri
= info_make(parent_ri
->type
, parent_ri
->sub_type
, 0,
1929 parent_ri
->peer
, attr_new
, rn
);
1930 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1931 bgp_info_extra_get(ri
);
1932 ri
->extra
->parent
= parent_ri
;
1933 if (parent_ri
->extra
) {
1934 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
1935 sizeof(ri
->extra
->label
));
1936 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
1938 bgp_info_add(rn
, ri
);
1940 if (attrhash_cmp(ri
->attr
, parent_ri
->attr
)
1941 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
1942 bgp_unlock_node(rn
);
1945 /* The attribute has changed. */
1946 /* Add (or update) attribute to hash. */
1947 attr_new
= bgp_attr_intern(parent_ri
->attr
);
1949 /* Restore route, if needed. */
1950 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
1951 bgp_info_restore(rn
, ri
);
1953 /* Mark if nexthop has changed. */
1954 if (!IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
))
1955 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
1957 /* Unintern existing, set to new. */
1958 bgp_attr_unintern(&ri
->attr
);
1959 ri
->attr
= attr_new
;
1960 ri
->uptime
= bgp_clock();
1963 /* Perform route selection and update zebra, if required. */
1964 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
1970 * Uninstall route entry from the VRF routing table and send message
1971 * to zebra, if appropriate.
1973 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
1974 struct prefix_evpn
*evp
,
1975 struct bgp_info
*parent_ri
)
1977 struct bgp_node
*rn
;
1978 struct bgp_info
*ri
;
1981 struct prefix
*pp
= &p
;
1984 char buf
[PREFIX_STRLEN
];
1985 char buf1
[PREFIX_STRLEN
];
1987 memset(pp
, 0, sizeof(struct prefix
));
1988 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1989 ip_prefix_from_type2_prefix(evp
, pp
);
1990 else if (evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
)
1991 ip_prefix_from_type5_prefix(evp
, pp
);
1993 if (bgp_debug_zebra(NULL
)) {
1994 zlog_debug("uninstalling evpn prefix %s as ip prefix %s in vrf %s",
1995 prefix2str(evp
, buf
, sizeof(buf
)),
1996 prefix2str(pp
, buf1
, sizeof(buf
)),
1997 vrf_id_to_name(bgp_vrf
->vrf_id
));
2000 /* Locate route within the VRF. */
2001 /* NOTE: There is no RD here. */
2002 if (IS_EVPN_PREFIX_IPADDR_V4(evp
)) {
2004 safi
= SAFI_UNICAST
;
2005 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2008 safi
= SAFI_UNICAST
;
2009 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2015 /* Find matching route entry. */
2016 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2018 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2024 /* Mark entry for deletion */
2025 bgp_info_delete(rn
, ri
);
2027 /* Perform route selection and update zebra, if required. */
2028 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2030 /* Unlock route node. */
2031 bgp_unlock_node(rn
);
2037 * Uninstall route entry from the VNI routing table and send message
2038 * to zebra, if appropriate.
2040 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2041 struct prefix_evpn
*p
,
2042 struct bgp_info
*parent_ri
)
2044 struct bgp_node
*rn
;
2045 struct bgp_info
*ri
;
2048 /* Locate route within the VNI. */
2049 /* NOTE: There is no RD here. */
2050 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2054 /* Find matching route entry. */
2055 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2057 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2063 /* Mark entry for deletion */
2064 bgp_info_delete(rn
, ri
);
2066 /* Perform route selection and update zebra, if required. */
2067 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2069 /* Unlock route node. */
2070 bgp_unlock_node(rn
);
2076 * Given a route entry and a VRF, see if this route entry should be
2077 * imported into the VRF i.e., RTs match.
2079 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2080 struct bgp_info
*ri
)
2082 struct attr
*attr
= ri
->attr
;
2083 struct ecommunity
*ecom
;
2087 /* Route should have valid RT to be even considered. */
2088 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2091 ecom
= attr
->ecommunity
;
2092 if (!ecom
|| !ecom
->size
)
2095 /* For each extended community RT, see if it matches this VNI. If any RT
2096 * matches, we're done.
2098 for (i
= 0; i
< ecom
->size
; i
++) {
2100 u_char type
, sub_type
;
2101 struct ecommunity_val
*eval
;
2102 struct ecommunity_val eval_tmp
;
2103 struct vrf_irt_node
*irt
;
2105 /* Only deal with RTs */
2106 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2107 eval
= (struct ecommunity_val
*)(ecom
->val
2108 + (i
* ECOMMUNITY_SIZE
));
2111 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2114 /* See if this RT matches specified VNIs import RTs */
2115 irt
= lookup_vrf_import_rt(eval
);
2116 if (irt
&& irt
->vrfs
)
2117 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2120 /* Also check for non-exact match. In this, we mask out the AS
2122 * only check on the local-admin sub-field. This is to
2124 * VNI as the RT for EBGP peering too.
2127 if (type
== ECOMMUNITY_ENCODE_AS
2128 || type
== ECOMMUNITY_ENCODE_AS4
2129 || type
== ECOMMUNITY_ENCODE_IP
) {
2130 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2131 mask_ecom_global_admin(&eval_tmp
, eval
);
2132 irt
= lookup_vrf_import_rt(&eval_tmp
);
2134 if (irt
&& irt
->vrfs
)
2135 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2143 * Given a route entry and a VNI, see if this route entry should be
2144 * imported into the VNI i.e., RTs match.
2146 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2147 struct bgp_info
*ri
)
2149 struct attr
*attr
= ri
->attr
;
2150 struct ecommunity
*ecom
;
2154 /* Route should have valid RT to be even considered. */
2155 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2158 ecom
= attr
->ecommunity
;
2159 if (!ecom
|| !ecom
->size
)
2162 /* For each extended community RT, see if it matches this VNI. If any RT
2163 * matches, we're done.
2165 for (i
= 0; i
< ecom
->size
; i
++) {
2167 u_char type
, sub_type
;
2168 struct ecommunity_val
*eval
;
2169 struct ecommunity_val eval_tmp
;
2170 struct irt_node
*irt
;
2172 /* Only deal with RTs */
2173 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2174 eval
= (struct ecommunity_val
*)(ecom
->val
2175 + (i
* ECOMMUNITY_SIZE
));
2178 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2181 /* See if this RT matches specified VNIs import RTs */
2182 irt
= lookup_import_rt(bgp
, eval
);
2183 if (irt
&& irt
->vnis
)
2184 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2187 /* Also check for non-exact match. In this, we mask out the AS
2189 * only check on the local-admin sub-field. This is to
2191 * VNI as the RT for EBGP peering too.
2194 if (type
== ECOMMUNITY_ENCODE_AS
2195 || type
== ECOMMUNITY_ENCODE_AS4
2196 || type
== ECOMMUNITY_ENCODE_IP
) {
2197 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2198 mask_ecom_global_admin(&eval_tmp
, eval
);
2199 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2201 if (irt
&& irt
->vnis
)
2202 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2210 * Install or uninstall mac-ip routes are appropriate for this
2213 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
,
2218 struct bgp_node
*rd_rn
, *rn
;
2219 struct bgp_table
*table
;
2220 struct bgp_info
*ri
;
2222 char buf
[PREFIX_STRLEN
];
2223 struct bgp
*bgp_def
= NULL
;
2227 bgp_def
= bgp_get_default();
2231 /* Walk entire global routing table and evaluate routes which could be
2232 * imported into this VRF. Note that we need to loop through all global
2233 * routes to determine which route matches the import rt on vrf
2235 for (rd_rn
= bgp_table_top(bgp_def
->rib
[afi
][safi
]); rd_rn
;
2236 rd_rn
= bgp_route_next(rd_rn
)) {
2237 table
= (struct bgp_table
*)(rd_rn
->info
);
2241 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2242 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2244 /* if not mac-ip route skip this route */
2245 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
2246 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
2249 /* if not a mac+ip route skip this route */
2250 if (!(IS_EVPN_PREFIX_IPADDR_V4(evp
) ||
2251 IS_EVPN_PREFIX_IPADDR_V6(evp
)))
2254 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2255 /* Consider "valid" remote routes applicable for
2258 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2259 && ri
->type
== ZEBRA_ROUTE_BGP
2260 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2263 if (is_route_matching_for_vrf(bgp_vrf
, ri
)) {
2266 install_evpn_route_entry_in_vrf(
2270 uninstall_evpn_route_entry_in_vrf(
2275 "Failed to %s EVPN %s route in VRF %s",
2278 prefix2str(evp
, buf
,
2280 vrf_id_to_name(bgp_vrf
->vrf_id
));
2292 * Install or uninstall routes of specified type that are appropriate for this
2295 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
2296 struct bgpevpn
*vpn
,
2297 bgp_evpn_route_type rtype
,
2302 struct bgp_node
*rd_rn
, *rn
;
2303 struct bgp_table
*table
;
2304 struct bgp_info
*ri
;
2310 /* Walk entire global routing table and evaluate routes which could be
2311 * imported into this VPN. Note that we cannot just look at the routes
2313 * the VNI's RD - remote routes applicable for this VNI could have any
2316 /* EVPN routes are a 2-level table. */
2317 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2318 rd_rn
= bgp_route_next(rd_rn
)) {
2319 table
= (struct bgp_table
*)(rd_rn
->info
);
2323 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2324 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2326 if (evp
->prefix
.route_type
!= rtype
)
2329 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2330 /* Consider "valid" remote routes applicable for
2332 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2333 && ri
->type
== ZEBRA_ROUTE_BGP
2334 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2337 if (is_route_matching_for_vni(bgp
, vpn
, ri
)) {
2339 ret
= install_evpn_route_entry(
2342 ret
= uninstall_evpn_route_entry(
2347 "%u: Failed to %s EVPN %s route in VNI %u",
2351 rtype
== BGP_EVPN_MAC_IP_ROUTE
2365 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
2366 * is invoked upon l3vni-add or l3vni import rt change
2368 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
2370 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
2375 * Install any existing remote routes applicable for this VNI into its
2376 * routing table. This is invoked when a VNI becomes "live" or its Import
2379 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2383 /* Install type-3 routes followed by type-2 routes - the ones applicable
2386 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
2391 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
2395 /* uninstall routes from l3vni vrf. */
2396 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
2398 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
2403 * Uninstall any existing remote routes for this VNI. One scenario in which
2404 * this is invoked is upon an import RT change.
2406 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2410 /* Uninstall type-2 routes followed by type-3 routes - the ones
2414 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
2419 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
2424 * Install or uninstall route in matching VRFs (list).
2426 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
2427 safi_t safi
, struct prefix_evpn
*evp
,
2428 struct bgp_info
*ri
,
2429 struct list
*vrfs
, int install
)
2431 char buf
[PREFIX2STR_BUFFER
];
2432 struct bgp
*bgp_vrf
;
2433 struct listnode
*node
, *nnode
;
2435 /* Only type-2/type-5 routes go into a VRF */
2436 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
2437 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
2440 /* if it is type-2 route and not a mac+ip route skip this route */
2441 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) &&
2442 !(IS_EVPN_PREFIX_IPADDR_V4(evp
) || IS_EVPN_PREFIX_IPADDR_V6(evp
)))
2445 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
2449 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
,
2452 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
,
2456 zlog_err("%u: Failed to %s prefix %s in VRF %s",
2458 install
? "install" : "uninstall",
2459 prefix2str(evp
, buf
, sizeof(buf
)),
2460 vrf_id_to_name(bgp_vrf
->vrf_id
));
2469 * Install or uninstall route in matching VNIs (list).
2471 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
2472 safi_t safi
, struct prefix_evpn
*evp
,
2473 struct bgp_info
*ri
,
2474 struct list
*vnis
, int install
)
2476 struct bgpevpn
*vpn
;
2477 struct listnode
*node
, *nnode
;
2479 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
2482 if (!is_vni_live(vpn
))
2486 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, ri
);
2488 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, ri
);
2491 zlog_err("%u: Failed to %s EVPN %s route in VNI %u",
2492 bgp
->vrf_id
, install
? "install" : "uninstall",
2493 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2505 * Install or uninstall route for appropriate VNIs.
2507 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
2508 struct prefix
*p
, struct bgp_info
*ri
,
2511 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
2512 struct attr
*attr
= ri
->attr
;
2513 struct ecommunity
*ecom
;
2518 /* Only type-2 and type-3 and type-5 are supported currently */
2519 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2520 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
2521 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
2524 /* If we don't have Route Target, nothing much to do. */
2525 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2528 ecom
= attr
->ecommunity
;
2529 if (!ecom
|| !ecom
->size
)
2532 /* For each extended community RT, see which VNIs/VRFs match and import
2533 * the route into matching VNIs/VRFs.
2535 for (i
= 0; i
< ecom
->size
; i
++) {
2537 u_char type
, sub_type
;
2538 struct ecommunity_val
*eval
;
2539 struct ecommunity_val eval_tmp
;
2540 struct irt_node
*irt
; /* import rt for l2vni */
2541 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
2543 /* Only deal with RTs */
2544 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2545 eval
= (struct ecommunity_val
*)(ecom
->val
2546 + (i
* ECOMMUNITY_SIZE
));
2549 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2552 /* Import route into matching l2-vnis (type-2/type-3 routes go
2555 irt
= lookup_import_rt(bgp
, eval
);
2556 if (irt
&& irt
->vnis
)
2557 install_uninstall_route_in_vnis(bgp
, afi
, safi
, evp
, ri
,
2560 /* Import route into matching l3-vnis (type-2/type-5 routes go
2561 * into l3vni/vrf table)
2563 vrf_irt
= lookup_vrf_import_rt(eval
);
2564 if (vrf_irt
&& vrf_irt
->vrfs
)
2565 install_uninstall_route_in_vrfs(bgp
, afi
, safi
, evp
, ri
,
2566 vrf_irt
->vrfs
, import
);
2568 /* Also check for non-exact match. In this,
2569 * we mask out the AS and
2570 * only check on the local-admin sub-field.
2571 * This is to facilitate using
2572 * VNI as the RT for EBGP peering too.
2576 if (type
== ECOMMUNITY_ENCODE_AS
2577 || type
== ECOMMUNITY_ENCODE_AS4
2578 || type
== ECOMMUNITY_ENCODE_IP
) {
2579 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2580 mask_ecom_global_admin(&eval_tmp
, eval
);
2581 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2582 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
2584 if (irt
&& irt
->vnis
)
2585 install_uninstall_route_in_vnis(bgp
, afi
, safi
, evp
, ri
,
2587 if (vrf_irt
&& vrf_irt
->vrfs
)
2588 install_uninstall_route_in_vrfs(bgp
, afi
, safi
, evp
,
2596 /* delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
2598 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
2600 /* delete all ipv4 routes and withdraw from peers */
2601 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
2603 /* delete all ipv6 routes and withdraw from peers */
2604 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
2607 /* update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
2609 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
2611 /* update all ipv4 routes */
2612 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
2614 /* update all ipv6 routes */
2615 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
2619 * update and advertise local routes for a VRF as type-5 routes.
2620 * This is invoked upon RD change for a VRF. Note taht the processing is only
2621 * done in the global route table using the routes which already exist in the
2624 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
2626 /* skip if the RD is configured */
2627 if (is_vrf_rd_configured(bgp_vrf
))
2630 /* derive the RD for the VRF based on new router-id */
2631 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
2633 /* update advertise ipv4|ipv6 routes as type-5 routes */
2634 update_advertise_vrf_routes(bgp_vrf
);
2638 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
2639 * This is invoked upon VRF RD change. The processing is done only from global
2642 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
2644 /* skip if the RD is configured */
2645 if (is_vrf_rd_configured(bgp_vrf
))
2648 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
2649 delete_withdraw_vrf_routes(bgp_vrf
);
2653 * Update and advertise local routes for a VNI. Invoked upon router-id
2654 * change. Note that the processing is done only on the global route table
2655 * using routes that already exist in the per-VNI table.
2657 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2659 struct prefix_evpn p
;
2660 struct bgp_node
*rn
, *global_rn
;
2661 struct bgp_info
*ri
, *global_ri
;
2663 afi_t afi
= AFI_L2VPN
;
2664 safi_t safi
= SAFI_EVPN
;
2666 /* Locate type-3 route for VNI in the per-VNI table and use its
2667 * attributes to create and advertise the type-3 route for this VNI
2668 * in the global table.
2670 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2671 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
2672 if (!rn
) /* unexpected */
2674 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2675 if (ri
->peer
== bgp
->peer_self
&& ri
->type
== ZEBRA_ROUTE_BGP
2676 && ri
->sub_type
== BGP_ROUTE_STATIC
)
2678 if (!ri
) /* unexpected */
2682 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2683 (struct prefix
*)&p
, &vpn
->prd
);
2684 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1, 0, &ri
,
2687 /* Schedule for processing and unlock node. */
2688 bgp_process(bgp
, global_rn
, afi
, safi
);
2689 bgp_unlock_node(global_rn
);
2691 /* Now, walk this VNI's route table and use the route and its attribute
2692 * to create and schedule route in global table.
2694 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2695 rn
= bgp_route_next(rn
)) {
2696 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2698 /* Identify MAC-IP local routes. */
2699 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2702 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2703 if (ri
->peer
== bgp
->peer_self
2704 && ri
->type
== ZEBRA_ROUTE_BGP
2705 && ri
->sub_type
== BGP_ROUTE_STATIC
)
2710 /* Create route in global routing table using this route entry's
2714 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2715 (struct prefix
*)evp
, &vpn
->prd
);
2717 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
2720 /* Schedule for processing and unlock node. */
2721 bgp_process(bgp
, global_rn
, afi
, safi
);
2722 bgp_unlock_node(global_rn
);
2729 * Delete (and withdraw) local routes for a VNI - only from the global
2730 * table. Invoked upon router-id change.
2732 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2735 struct prefix_evpn p
;
2736 struct bgp_node
*global_rn
;
2737 struct bgp_info
*ri
;
2738 afi_t afi
= AFI_L2VPN
;
2739 safi_t safi
= SAFI_EVPN
;
2741 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2742 * for this VNI - from the global table.
2744 ret
= delete_global_type2_routes(bgp
, vpn
);
2748 /* Remove type-3 route for this VNI from global table. */
2749 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2750 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
2751 (struct prefix
*)&p
, &vpn
->prd
);
2753 /* Delete route entry in the global EVPN table. */
2754 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
2756 /* Schedule for processing - withdraws to peers happen from
2760 bgp_process(bgp
, global_rn
, afi
, safi
);
2761 bgp_unlock_node(global_rn
);
2768 * Handle router-id change. Update and advertise local routes corresponding
2769 * to this VNI from peers. Note that this is invoked after updating the
2770 * router-id. The routes in the per-VNI table are used to create routes in
2771 * the global table and schedule them.
2773 static void update_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
2775 struct bgpevpn
*vpn
;
2777 vpn
= (struct bgpevpn
*)backet
->data
;
2780 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__
);
2784 /* Skip VNIs with configured RD. */
2785 if (is_rd_configured(vpn
))
2788 bgp_evpn_derive_auto_rd(bgp
, vpn
);
2789 update_advertise_vni_routes(bgp
, vpn
);
2793 * Handle router-id change. Delete and withdraw local routes corresponding
2794 * to this VNI from peers. Note that this is invoked prior to updating
2795 * the router-id and is done only on the global route table, the routes
2796 * are needed in the per-VNI table to re-advertise with new router id.
2798 static void withdraw_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
2800 struct bgpevpn
*vpn
;
2802 vpn
= (struct bgpevpn
*)backet
->data
;
2805 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__
);
2809 /* Skip VNIs with configured RD. */
2810 if (is_rd_configured(vpn
))
2813 delete_withdraw_vni_routes(bgp
, vpn
);
2817 * Process received EVPN type-2 route (advertise or withdraw).
2819 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
2820 struct attr
*attr
, u_char
*pfx
, int psize
,
2821 u_int32_t addpath_id
)
2823 struct prefix_rd prd
;
2824 struct prefix_evpn p
;
2827 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
2828 u_int32_t num_labels
= 0;
2831 /* Type-2 route should be either 33, 37 or 49 bytes or an
2832 * additional 3 bytes if there is a second label (VNI):
2833 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
2834 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
2835 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
2837 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
2838 && psize
!= 40 && psize
!= 52) {
2839 zlog_err("%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
2840 peer
->bgp
->vrf_id
, peer
->host
, psize
);
2844 /* Make prefix_rd */
2845 prd
.family
= AF_UNSPEC
;
2847 memcpy(&prd
.val
, pfx
, 8);
2850 /* Make EVPN prefix. */
2851 memset(&p
, 0, sizeof(struct prefix_evpn
));
2853 p
.prefixlen
= EVPN_TYPE_2_ROUTE_PREFIXLEN
;
2854 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
2856 /* Skip over Ethernet Seg Identifier for now. */
2859 /* Skip over Ethernet Tag for now. */
2862 /* Get the MAC Addr len */
2863 macaddr_len
= *pfx
++;
2865 /* Get the MAC Addr */
2866 if (macaddr_len
== (ETH_ALEN
* 8)) {
2867 memcpy(&p
.prefix
.mac
.octet
, pfx
, ETH_ALEN
);
2871 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
2872 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
2878 ipaddr_len
= *pfx
++;
2879 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
2880 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
2882 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
2883 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
2888 ipaddr_len
/= 8; /* Convert to bytes. */
2889 p
.prefix
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
2892 memcpy(&p
.prefix
.ip
.ip
.addr
, pfx
, ipaddr_len
);
2896 /* Get the VNI(s). Stored as bytes here. */
2898 memset(label
, 0, sizeof(label
));
2899 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
2900 pfx
+= BGP_LABEL_BYTES
;
2901 psize
-= (33 + ipaddr_len
);
2902 /* Do we have a second VNI? */
2905 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
2907 * If in future, we are required to access additional fields,
2908 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next field
2912 /* Process the route. */
2914 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2915 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2916 &prd
, &label
[0], num_labels
, 0, NULL
);
2918 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2919 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2920 &prd
, &label
[0], num_labels
, NULL
);
2925 * Process received EVPN type-3 route (advertise or withdraw).
2927 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
2928 struct attr
*attr
, u_char
*pfx
, int psize
,
2929 u_int32_t addpath_id
)
2931 struct prefix_rd prd
;
2932 struct prefix_evpn p
;
2936 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
2937 * IP len (1) and IP (4 or 16).
2939 if (psize
!= 17 && psize
!= 29) {
2940 zlog_err("%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
2941 peer
->bgp
->vrf_id
, peer
->host
, psize
);
2945 /* Make prefix_rd */
2946 prd
.family
= AF_UNSPEC
;
2948 memcpy(&prd
.val
, pfx
, 8);
2951 /* Make EVPN prefix. */
2952 memset(&p
, 0, sizeof(struct prefix_evpn
));
2954 p
.prefixlen
= EVPN_TYPE_3_ROUTE_PREFIXLEN
;
2955 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
2957 /* Skip over Ethernet Tag for now. */
2961 ipaddr_len
= *pfx
++;
2962 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
2963 p
.prefix
.ip
.ipa_type
= IPADDR_V4
;
2964 memcpy(&p
.prefix
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
2967 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
2968 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
2972 /* Process the route. */
2974 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2975 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2976 &prd
, NULL
, 0, 0, NULL
);
2978 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2979 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2980 &prd
, NULL
, 0, NULL
);
2985 * Process received EVPN type-5 route (advertise or withdraw).
2987 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
2988 struct attr
*attr
, u_char
*pfx
, int psize
,
2989 u_int32_t addpath_id
, int withdraw
)
2991 struct prefix_rd prd
;
2992 struct prefix_evpn p
;
2993 struct bgp_route_evpn evpn
;
2996 mpls_label_t label
; /* holds the VNI as in the packet */
2999 /* Type-5 route should be 34 or 58 bytes:
3000 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3001 * GW (4 or 16) and VNI (3).
3002 * Note that the IP and GW should both be IPv4 or both IPv6.
3004 if (psize
!= 34 && psize
!= 58) {
3005 zlog_err("%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3006 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3010 /* Make prefix_rd */
3011 prd
.family
= AF_UNSPEC
;
3013 memcpy(&prd
.val
, pfx
, 8);
3016 /* Make EVPN prefix. */
3017 memset(&p
, 0, sizeof(struct prefix_evpn
));
3019 p
.prefixlen
= EVPN_TYPE_5_ROUTE_PREFIXLEN
;
3020 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
3022 /* Additional information outside of prefix - ESI and GW IP */
3023 memset(&evpn
, 0, sizeof(evpn
));
3026 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
3029 /* Fetch Ethernet Tag. */
3030 memcpy(ð_tag
, pfx
, 4);
3031 p
.prefix
.eth_tag
= ntohl(eth_tag
);
3034 /* Fetch IP prefix length. */
3036 if (ippfx_len
> IPV6_MAX_BITLEN
) {
3038 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3039 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
3042 p
.prefix
.ip_prefix_length
= ippfx_len
;
3044 /* Determine IPv4 or IPv6 prefix */
3045 /* Since the address and GW are from the same family, this just becomes
3046 * a simple check on the total size.
3049 SET_IPADDR_V4(&p
.prefix
.ip
);
3050 memcpy(&p
.prefix
.ip
.ipaddr_v4
, pfx
, 4);
3052 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
3055 SET_IPADDR_V6(&p
.prefix
.ip
);
3056 memcpy(&p
.prefix
.ip
.ipaddr_v6
, pfx
, 16);
3058 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
3062 /* Get the VNI (in MPLS label field). Stored as bytes here. */
3063 memset(&label
, 0, sizeof(label
));
3064 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
3067 * If in future, we are required to access additional fields,
3068 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next field
3071 /* Process the route. */
3073 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3074 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3075 &prd
, &label
, 1, 0, &evpn
);
3077 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3078 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3079 &prd
, &label
, 1, &evpn
);
3084 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
3085 struct prefix_rd
*prd
,
3086 mpls_label_t
*label
, u_int32_t num_labels
,
3091 struct evpn_addr
*p_evpn_p
;
3093 memset(&temp
, 0, 16);
3094 if (p
->family
!= AF_EVPN
)
3096 p_evpn_p
= &(p
->u
.prefix_evpn
);
3098 /* len denites the total len of IP and GW-IP in the route
3099 IP and GW-IP have to be both ipv4 or ipv6
3101 if (IS_IPADDR_V4(&p_evpn_p
->ip
))
3102 len
= 8; /* IP and GWIP are both ipv4 */
3104 len
= 32; /* IP and GWIP are both ipv6 */
3105 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
3106 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
3107 stream_put(s
, prd
->val
, 8);
3109 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
3111 stream_put(s
, &temp
, 10);
3112 stream_putl(s
, p_evpn_p
->eth_tag
);
3113 stream_putc(s
, p_evpn_p
->ip_prefix_length
);
3114 if (IS_IPADDR_V4(&p_evpn_p
->ip
))
3115 stream_put_ipv4(s
, p_evpn_p
->ip
.ipaddr_v4
.s_addr
);
3117 stream_put(s
, &p_evpn_p
->ip
.ipaddr_v6
, 16);
3119 if (IS_IPADDR_V4(&p_evpn_p
->ip
))
3121 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
3123 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
3125 if (IS_IPADDR_V4(&p_evpn_p
->ip
))
3126 stream_put_ipv4(s
, 0);
3128 stream_put(s
, &temp
, 16);
3132 stream_put(s
, label
, 3);
3138 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
3140 static void cleanup_vni_on_disable(struct hash_backet
*backet
, struct bgp
*bgp
)
3142 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3144 /* Remove EVPN routes and schedule for processing. */
3145 delete_routes_for_vni(bgp
, vpn
);
3147 /* Clear "live" flag and see if hash needs to be freed. */
3148 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
3149 if (!is_vni_configured(vpn
))
3150 bgp_evpn_free(bgp
, vpn
);
3154 * Free a VNI entry; iterator function called during cleanup.
3156 static void free_vni_entry(struct hash_backet
*backet
, struct bgp
*bgp
)
3158 struct bgpevpn
*vpn
;
3160 vpn
= (struct bgpevpn
*)backet
->data
;
3161 delete_all_vni_routes(bgp
, vpn
);
3162 bgp_evpn_free(bgp
, vpn
);
3166 * Derive AUTO import RT for BGP VRF - L3VNI
3168 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
3170 struct bgp
*bgp_def
= NULL
;
3172 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
3173 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
3176 bgp_def
= bgp_get_default();
3179 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
3183 * Delete AUTO import RT from BGP VRF - L3VNI
3185 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
3187 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
3191 * Derive AUTO export RT for BGP VRF - L3VNI
3193 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
3195 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
3196 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
3200 * Delete AUTO export RT from BGP VRF - L3VNI
3202 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
3204 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
3207 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
3209 struct bgp
*bgp_def
= NULL
;
3210 struct listnode
*node
= NULL
;
3211 struct bgpevpn
*vpn
= NULL
;
3213 bgp_def
= bgp_get_default();
3217 /* update all type-5 routes */
3218 update_advertise_vrf_routes(bgp_vrf
);
3220 /* update all type-2 routes */
3221 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
3222 update_routes_for_vni(bgp_def
, vpn
);
3229 /* withdraw type-5 route corresponding to ip prefix */
3230 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
3231 afi_t afi
, safi_t safi
)
3234 struct prefix_evpn evp
;
3235 char buf
[PREFIX_STRLEN
];
3237 /* NOTE: Check needed as this is called per-route also. */
3238 if (!advertise_type5_routes(bgp_vrf
, afi
))
3241 build_type5_prefix_from_ip_prefix(&evp
, p
);
3242 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
3245 "%u failed to delete type-5 route for prefix %s in vrf %s",
3247 prefix2str(p
, buf
, sizeof(buf
)),
3248 vrf_id_to_name(bgp_vrf
->vrf_id
));
3252 /* withdraw all type-5 routes for an address family */
3253 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
,
3254 afi_t afi
, safi_t safi
)
3256 struct bgp_table
*table
= NULL
;
3257 struct bgp_node
*rn
= NULL
;
3258 struct bgp_info
*ri
;
3260 /* Bail out early if we don't have to advertise type-5 routes. */
3261 if (!advertise_type5_routes(bgp_vrf
, afi
))
3264 table
= bgp_vrf
->rib
[afi
][safi
];
3265 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3266 /* Only care about "selected" routes - non-imported. */
3267 /* TODO: Support for AddPath for EVPN. */
3268 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
3269 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
) &&
3270 (!ri
->extra
|| !ri
->extra
->parent
)) {
3271 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
3280 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
3281 * to this function correspond to those of the source IP prefix (best
3282 * path in the case of the attr. In the case of a local prefix (when we
3283 * are advertising local subnets), the src_attr will be NULL.
3285 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
3286 struct attr
*src_attr
,
3287 afi_t afi
, safi_t safi
)
3290 struct prefix_evpn evp
;
3291 char buf
[PREFIX_STRLEN
];
3293 /* NOTE: Check needed as this is called per-route also. */
3294 if (!advertise_type5_routes(bgp_vrf
, afi
))
3297 build_type5_prefix_from_ip_prefix(&evp
, p
);
3298 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
3301 "%u: Failed to create type-5 route for prefix %s",
3303 prefix2str(p
, buf
, sizeof(buf
)));
3306 /* Inject all prefixes of a particular address-family (currently, IPv4 or
3307 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
3308 * advertisement is enabled.
3310 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
,
3311 afi_t afi
, safi_t safi
)
3313 struct bgp_table
*table
= NULL
;
3314 struct bgp_node
*rn
= NULL
;
3315 struct bgp_info
*ri
;
3317 /* Bail out early if we don't have to advertise type-5 routes. */
3318 if (!advertise_type5_routes(bgp_vrf
, afi
))
3321 table
= bgp_vrf
->rib
[afi
][safi
];
3322 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3323 /* Need to identify the "selected" route entry to use its
3324 * attribute. Also, we only consider "non-imported" routes.
3325 * TODO: Support for AddPath for EVPN.
3327 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
3328 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
) &&
3329 (!ri
->extra
|| !ri
->extra
->parent
)) {
3331 /* apply the route-map */
3332 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
3337 bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
,
3338 &rn
->p
, RMAP_BGP
, ri
);
3339 if (ret
== RMAP_DENYMATCH
)
3342 bgp_evpn_advertise_type5_route(bgp_vrf
, &rn
->p
,
3351 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
,
3354 struct listnode
*node
, *nnode
, *node_to_del
;
3355 struct ecommunity
*ecom
, *ecom_auto
;
3356 struct ecommunity_val eval
;
3358 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
3360 ecom_auto
= ecommunity_new();
3361 ecommunity_add_val(ecom_auto
, &eval
);
3364 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
3365 if (ecommunity_match(ecom
, ecom_auto
)) {
3366 ecommunity_free(&ecom
);
3372 list_delete_node(rtl
, node_to_del
);
3374 ecommunity_free(&ecom_auto
);
3377 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
3378 struct ecommunity
*ecomadd
)
3380 /* uninstall routes from vrf */
3381 uninstall_routes_for_vrf(bgp_vrf
);
3383 /* Cleanup the RT to VRF mapping */
3384 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
3386 /* Remove auto generated RT */
3387 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
3389 /* Add the newly configured RT to RT list */
3390 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
3391 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
3393 /* map VRF to its RTs */
3394 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
3396 /* install routes matching the new VRF */
3397 install_routes_for_vrf(bgp_vrf
);
3400 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
3401 struct ecommunity
*ecomdel
)
3403 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
3404 struct ecommunity
*ecom
= NULL
;
3406 /* uninstall routes from vrf */
3407 uninstall_routes_for_vrf(bgp_vrf
);
3409 /* Cleanup the RT to VRF mapping */
3410 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
3412 /* remove the RT from the RT list */
3413 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
3414 if (ecommunity_match(ecom
, ecomdel
)) {
3415 ecommunity_free(&ecom
);
3422 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
3424 /* fallback to auto import rt, if this was the last RT */
3425 if (list_isempty(bgp_vrf
->vrf_import_rtl
)) {
3426 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
3427 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
3430 /* map VRFs to its RTs */
3431 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
3433 /* install routes matching this new RT */
3434 install_routes_for_vrf(bgp_vrf
);
3437 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
3438 struct ecommunity
*ecomadd
)
3440 /* remove auto-generated RT */
3441 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
3443 /* Add the new RT to the RT list */
3444 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
3445 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
3447 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
3451 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
3452 struct ecommunity
*ecomdel
)
3454 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
3455 struct ecommunity
*ecom
= NULL
;
3457 /* Remove the RT from the RT list */
3458 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
3459 if (ecommunity_match(ecom
, ecomdel
)) {
3460 ecommunity_free(&ecom
);
3467 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
3469 /* fall back to auto-generated RT if this was the last RT */
3470 if (bgp_vrf
->vrf_export_rtl
&& list_isempty(bgp_vrf
->vrf_export_rtl
)) {
3471 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
3472 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
3475 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
3479 * Handle change to BGP router id. This is invoked twice by the change
3480 * handler, first before the router id has been changed and then after
3481 * the router id has been changed. The first invocation will result in
3482 * local routes for all VNIs/VRF being deleted and withdrawn and the next
3483 * will result in the routes being re-advertised.
3485 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
3489 /* delete and withdraw all the type-5 routes
3490 stored in the global table for this vrf
3492 withdraw_router_id_vrf(bgp
);
3494 /* delete all the VNI routes (type-2/type-3) routes for all the
3497 hash_iterate(bgp
->vnihash
,
3498 (void (*)(struct hash_backet
*,
3499 void *))withdraw_router_id_vni
,
3503 /* advertise all routes in the vrf as type-5 routes with the new
3506 update_router_id_vrf(bgp
);
3508 /* advertise all the VNI routes (type-2/type-3) routes with the
3511 hash_iterate(bgp
->vnihash
,
3512 (void (*)(struct hash_backet
*,
3513 void *))update_router_id_vni
,
3519 * Handle change to export RT - update and advertise local routes.
3521 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3523 return update_routes_for_vni(bgp
, vpn
);
3526 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
,
3530 delete_withdraw_vrf_routes(bgp_vrf
);
3532 update_advertise_vrf_routes(bgp_vrf
);
3536 * Handle change to RD. This is invoked twice by the change handler,
3537 * first before the RD has been changed and then after the RD has
3538 * been changed. The first invocation will result in local routes
3539 * of this VNI being deleted and withdrawn and the next will result
3540 * in the routes being re-advertised.
3542 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
3546 delete_withdraw_vni_routes(bgp
, vpn
);
3548 update_advertise_vni_routes(bgp
, vpn
);
3552 * Install routes for this VNI. Invoked upon change to Import RT.
3554 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3556 return install_routes_for_vni(bgp
, vpn
);
3560 * Uninstall all routes installed for this VNI. Invoked upon change
3563 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3565 return uninstall_routes_for_vni(bgp
, vpn
);
3569 * TODO: Hardcoded for a maximum of 2 VNIs right now
3571 char *bgp_evpn_label2str(mpls_label_t
*label
, u_int32_t num_labels
,
3576 vni1
= label2vni(label
);
3577 if (num_labels
== 2) {
3578 vni2
= label2vni(label
+1);
3579 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
3581 snprintf(buf
, len
, "%u", vni1
);
3586 * Function to convert evpn route to json format.
3587 * NOTE: We don't use prefix2str as the output here is a bit different.
3589 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
3591 char buf1
[ETHER_ADDR_STRLEN
];
3592 char buf2
[PREFIX2STR_BUFFER
];
3597 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
3598 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
3599 json_object_int_add(json
, "ethTag", 0);
3600 json_object_int_add(json
, "ipLen",
3601 IS_EVPN_PREFIX_IPADDR_V4(p
)
3604 json_object_string_add(json
, "ip",
3605 inet_ntoa(p
->prefix
.ip
.ipaddr_v4
));
3606 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3607 if (IS_EVPN_PREFIX_IPADDR_NONE(p
)) {
3608 json_object_int_add(json
, "routeType",
3609 p
->prefix
.route_type
);
3610 json_object_int_add(
3612 0); /* TODO: we don't support esi yet */
3613 json_object_int_add(json
, "ethTag", 0);
3614 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
3615 json_object_string_add(json
, "mac",
3616 prefix_mac2str(&p
->prefix
.mac
,
3622 family
= IS_EVPN_PREFIX_IPADDR_V4(p
) ? AF_INET
3625 json_object_int_add(json
, "routeType",
3626 p
->prefix
.route_type
);
3627 json_object_int_add(
3629 0); /* TODO: we don't support esi yet */
3630 json_object_int_add(json
, "ethTag", 0);
3631 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
3632 json_object_string_add(json
, "mac",
3633 prefix_mac2str(&p
->prefix
.mac
,
3636 json_object_int_add(json
, "ipLen",
3637 IS_EVPN_PREFIX_IPADDR_V4(p
)
3640 json_object_string_add(
3642 inet_ntop(family
, &p
->prefix
.ip
.ip
.addr
, buf2
,
3643 PREFIX2STR_BUFFER
));
3646 /* Currently, this is to cater to other AF_ETHERNET code. */
3651 * Function to convert evpn route to string.
3652 * NOTE: We don't use prefix2str as the output here is a bit different.
3654 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
3656 char buf1
[ETHER_ADDR_STRLEN
];
3657 char buf2
[PREFIX2STR_BUFFER
];
3659 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
3660 snprintf(buf
, len
, "[%d]:[0]:[%d]:[%s]", p
->prefix
.route_type
,
3661 IS_EVPN_PREFIX_IPADDR_V4(p
) ? IPV4_MAX_BITLEN
3663 inet_ntoa(p
->prefix
.ip
.ipaddr_v4
));
3664 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3665 if (IS_EVPN_PREFIX_IPADDR_NONE(p
))
3666 snprintf(buf
, len
, "[%d]:[0]:[0]:[%d]:[%s]",
3667 p
->prefix
.route_type
, 8 * ETH_ALEN
,
3668 prefix_mac2str(&p
->prefix
.mac
, buf1
,
3673 family
= IS_EVPN_PREFIX_IPADDR_V4(p
) ? AF_INET
3675 snprintf(buf
, len
, "[%d]:[0]:[0]:[%d]:[%s]:[%d]:[%s]",
3676 p
->prefix
.route_type
, 8 * ETH_ALEN
,
3677 prefix_mac2str(&p
->prefix
.mac
, buf1
,
3679 family
== AF_INET
? IPV4_MAX_BITLEN
3681 inet_ntop(family
, &p
->prefix
.ip
.ip
.addr
, buf2
,
3682 PREFIX2STR_BUFFER
));
3684 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3685 snprintf(buf
, len
, "[%d]:[0]:[0]:[%d]:[%s]",
3686 p
->prefix
.route_type
,
3687 p
->prefix
.ip_prefix_length
,
3688 IS_EVPN_PREFIX_IPADDR_V4(p
) ?
3689 inet_ntoa(p
->prefix
.ip
.ipaddr_v4
) :
3690 inet6_ntoa(p
->prefix
.ip
.ipaddr_v6
));
3692 /* For EVPN route types not supported yet. */
3693 snprintf(buf
, len
, "(unsupported route type %d)",
3694 p
->prefix
.route_type
);
3701 * Encode EVPN prefix in Update (MP_REACH)
3703 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
3704 struct prefix_rd
*prd
,
3705 mpls_label_t
*label
, u_int32_t num_labels
,
3706 struct attr
*attr
, int addpath_encode
,
3707 u_int32_t addpath_tx_id
)
3709 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3710 int len
, ipa_len
= 0;
3713 stream_putl(s
, addpath_tx_id
);
3716 stream_putc(s
, evp
->prefix
.route_type
);
3718 switch (evp
->prefix
.route_type
) {
3719 case BGP_EVPN_MAC_IP_ROUTE
:
3720 if (IS_EVPN_PREFIX_IPADDR_V4(evp
))
3721 ipa_len
= IPV4_MAX_BYTELEN
;
3722 else if (IS_EVPN_PREFIX_IPADDR_V6(evp
))
3723 ipa_len
= IPV6_MAX_BYTELEN
;
3724 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
3725 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
3726 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
3728 stream_putc(s
, len
);
3729 stream_put(s
, prd
->val
, 8); /* RD */
3730 stream_put(s
, 0, 10); /* ESI */
3731 stream_putl(s
, 0); /* Ethernet Tag ID */
3732 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
3733 stream_put(s
, evp
->prefix
.mac
.octet
, 6); /* Mac Addr */
3734 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
3735 if (ipa_len
) /* IP */
3736 stream_put(s
, &evp
->prefix
.ip
.ip
.addr
, ipa_len
);
3737 /* 1st label is the L2 VNI */
3738 stream_put(s
, label
, BGP_LABEL_BYTES
);
3739 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
3740 if (ipa_len
&& num_labels
> 1)
3741 stream_put(s
, label
+1, BGP_LABEL_BYTES
);
3744 case BGP_EVPN_IMET_ROUTE
:
3745 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
3746 stream_put(s
, prd
->val
, 8); /* RD */
3747 stream_putl(s
, 0); /* Ethernet Tag ID */
3748 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
3749 /* Originating Router's IP Addr */
3750 stream_put_in_addr(s
, &evp
->prefix
.ip
.ipaddr_v4
);
3753 case BGP_EVPN_IP_PREFIX_ROUTE
:
3754 /* TODO: AddPath support. */
3755 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
3763 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
3764 struct bgp_nlri
*packet
, int withdraw
)
3770 u_int32_t addpath_id
;
3771 int addpath_encoded
;
3777 /* Check peer status. */
3778 if (peer
->status
!= Established
) {
3779 zlog_err("%u:%s - EVPN update received in state %d",
3780 peer
->bgp
->vrf_id
, peer
->host
, peer
->status
);
3784 /* Start processing the NLRI - there may be multiple in the MP_REACH */
3786 lim
= pnt
+ packet
->length
;
3788 safi
= packet
->safi
;
3792 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
3793 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
3794 PEER_CAP_ADDPATH_AF_TX_RCV
));
3796 for (; pnt
< lim
; pnt
+= psize
) {
3797 /* Clear prefix structure. */
3798 memset(&p
, 0, sizeof(struct prefix
));
3800 /* Deal with path-id if AddPath is supported. */
3801 if (addpath_encoded
) {
3802 /* When packet overflow occurs return immediately. */
3803 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
3806 addpath_id
= ntohl(*((uint32_t *)pnt
));
3807 pnt
+= BGP_ADDPATH_ID_LEN
;
3810 /* All EVPN NLRI types start with type and length. */
3815 psize
= rlen
= *pnt
++;
3817 /* When packet overflow occur return immediately. */
3818 if (pnt
+ psize
> lim
)
3822 case BGP_EVPN_MAC_IP_ROUTE
:
3823 if (process_type2_route(peer
, afi
, safi
,
3824 withdraw
? NULL
: attr
, pnt
,
3825 psize
, addpath_id
)) {
3827 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
3828 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3833 case BGP_EVPN_IMET_ROUTE
:
3834 if (process_type3_route(peer
, afi
, safi
,
3835 withdraw
? NULL
: attr
, pnt
,
3836 psize
, addpath_id
)) {
3838 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
3839 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3844 case BGP_EVPN_IP_PREFIX_ROUTE
:
3845 if (process_type5_route(peer
, afi
, safi
, attr
, pnt
,
3846 psize
, addpath_id
, withdraw
)) {
3848 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
3849 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3859 /* Packet length consistency check. */
3867 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
3868 * The mapping will be used during route processing.
3869 * bgp_def: default bgp instance
3870 * bgp_vrf: specific bgp vrf instance on which RT is configured
3872 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
3875 struct ecommunity_val
*eval
= NULL
;
3876 struct listnode
*node
= NULL
, *nnode
= NULL
;
3877 struct ecommunity
*ecom
= NULL
;
3879 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
3880 for (i
= 0; i
< ecom
->size
; i
++) {
3881 eval
= (struct ecommunity_val
*)(ecom
->val
3883 * ECOMMUNITY_SIZE
));
3884 map_vrf_to_rt(bgp_vrf
, eval
);
3890 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
3892 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
3895 struct ecommunity_val
*eval
;
3896 struct listnode
*node
, *nnode
;
3897 struct ecommunity
*ecom
;
3899 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
3900 for (i
= 0; i
< ecom
->size
; i
++) {
3901 struct vrf_irt_node
*irt
;
3902 struct ecommunity_val eval_tmp
;
3904 eval
= (struct ecommunity_val
*)(ecom
->val
3906 * ECOMMUNITY_SIZE
));
3907 /* If using "automatic" RT, we only care about the
3908 * local-admin sub-field.
3909 * This is to facilitate using VNI as the RT for EBGP
3912 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3913 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
3914 BGP_VRF_IMPORT_RT_CFGD
))
3915 mask_ecom_global_admin(&eval_tmp
, eval
);
3917 irt
= lookup_vrf_import_rt(&eval_tmp
);
3919 unmap_vrf_from_rt(bgp_vrf
, irt
);
3927 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
3928 * The mapping will be used during route processing.
3930 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3933 struct ecommunity_val
*eval
;
3934 struct listnode
*node
, *nnode
;
3935 struct ecommunity
*ecom
;
3937 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
3938 for (i
= 0; i
< ecom
->size
; i
++) {
3939 eval
= (struct ecommunity_val
*)(ecom
->val
3941 * ECOMMUNITY_SIZE
));
3942 map_vni_to_rt(bgp
, vpn
, eval
);
3948 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
3950 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3953 struct ecommunity_val
*eval
;
3954 struct listnode
*node
, *nnode
;
3955 struct ecommunity
*ecom
;
3957 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
3958 for (i
= 0; i
< ecom
->size
; i
++) {
3959 struct irt_node
*irt
;
3960 struct ecommunity_val eval_tmp
;
3962 eval
= (struct ecommunity_val
*)(ecom
->val
3964 * ECOMMUNITY_SIZE
));
3965 /* If using "automatic" RT, we only care about the
3966 * local-admin sub-field.
3967 * This is to facilitate using VNI as the RT for EBGP
3970 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3971 if (!is_import_rt_configured(vpn
))
3972 mask_ecom_global_admin(&eval_tmp
, eval
);
3974 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3976 unmap_vni_from_rt(bgp
, vpn
, irt
);
3982 * Derive Import RT automatically for VNI and map VNI to RT.
3983 * The mapping will be used during route processing.
3985 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3987 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
3988 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
3991 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
3995 * Derive Export RT automatically for VNI.
3997 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3999 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
4000 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
4004 * Derive RD automatically for VNI using passed information - it
4005 * is of the form RouterId:unique-id-for-vni.
4007 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
4011 bgp
->vrf_prd
.family
= AF_UNSPEC
;
4012 bgp
->vrf_prd
.prefixlen
= 64;
4013 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), bgp
->vrf_rd_id
);
4014 str2prefix_rd(buf
, &bgp
->vrf_prd
);
4018 * Derive RD automatically for VNI using passed information - it
4019 * is of the form RouterId:unique-id-for-vni.
4021 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4025 vpn
->prd
.family
= AF_UNSPEC
;
4026 vpn
->prd
.prefixlen
= 64;
4027 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
4028 (void)str2prefix_rd(buf
, &vpn
->prd
);
4029 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
4035 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
4037 struct bgpevpn
*vpn
;
4040 memset(&tmp
, 0, sizeof(struct bgpevpn
));
4042 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
4047 * Create a new vpn - invoked upon configuration or zebra notification.
4049 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
4050 struct in_addr originator_ip
,
4051 vrf_id_t tenant_vrf_id
)
4053 struct bgpevpn
*vpn
;
4058 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
4062 /* Set values - RD and RT set to defaults. */
4064 vpn
->originator_ip
= originator_ip
;
4065 vpn
->tenant_vrf_id
= tenant_vrf_id
;
4067 /* Initialize route-target import and export lists */
4068 vpn
->import_rtl
= list_new();
4069 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4070 vpn
->export_rtl
= list_new();
4071 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4072 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
4073 derive_rd_rt_for_vni(bgp
, vpn
);
4075 /* Initialize EVPN route table. */
4076 vpn
->route_table
= bgp_table_init(AFI_L2VPN
, SAFI_EVPN
);
4079 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
4080 XFREE(MTYPE_BGP_EVPN
, vpn
);
4084 /* add to l2vni list on corresponding vrf */
4085 bgpevpn_link_to_l3vni(vpn
);
4087 QOBJ_REG(vpn
, bgpevpn
);
4092 * Free a given VPN - called in multiple scenarios such as zebra
4093 * notification, configuration being deleted, advertise-all-vni disabled etc.
4094 * This just frees appropriate memory, caller should have taken other
4097 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4099 bgpevpn_unlink_from_l3vni(vpn
);
4100 bgp_table_unlock(vpn
->route_table
);
4101 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4102 list_delete_and_null(&vpn
->import_rtl
);
4103 list_delete_and_null(&vpn
->export_rtl
);
4104 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
4105 hash_release(bgp
->vnihash
, vpn
);
4107 XFREE(MTYPE_BGP_EVPN
, vpn
);
4111 * Import route into matching VNI(s).
4113 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
4114 struct prefix
*p
, struct bgp_info
*ri
)
4116 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 1);
4120 * Unimport route from matching VNI(s).
4122 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
4123 struct prefix
*p
, struct bgp_info
*ri
)
4125 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 0);
4128 /* filter routes which have martian next hops */
4129 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
4133 struct bgp_node
*rd_rn
, *rn
;
4134 struct bgp_table
*table
;
4135 struct bgp_info
*ri
;
4140 /* Walk entire global routing table and evaluate routes which could be
4141 * imported into this VPN. Note that we cannot just look at the routes
4142 * for the VNI's RD -
4143 * remote routes applicable for this VNI could have any RD.
4145 /* EVPN routes are a 2-level table. */
4146 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
4147 rd_rn
= bgp_route_next(rd_rn
)) {
4148 table
= (struct bgp_table
*)(rd_rn
->info
);
4152 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4154 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
4156 /* Consider "valid" remote routes applicable for
4158 if (!(ri
->type
== ZEBRA_ROUTE_BGP
4159 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
4162 if (bgp_nexthop_self(bgp
, ri
->attr
->nexthop
)) {
4164 char attr_str
[BUFSIZ
];
4165 char pbuf
[PREFIX_STRLEN
];
4167 bgp_dump_attr(ri
->attr
, attr_str
,
4170 if (bgp_debug_update(ri
->peer
, &rn
->p
,
4173 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
4180 bgp_evpn_unimport_route(bgp
, afi
, safi
,
4183 bgp_rib_remove(rn
, ri
, ri
->peer
, afi
,
4194 * Handle del of a local MACIP.
4196 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
4199 struct bgpevpn
*vpn
;
4200 struct prefix_evpn p
;
4202 if (!bgp
->vnihash
) {
4203 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4207 /* Lookup VNI hash - should exist. */
4208 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4209 if (!vpn
|| !is_vni_live(vpn
)) {
4210 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP DEL",
4211 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
4215 /* Remove EVPN type-2 route and schedule for processing. */
4216 build_evpn_type2_prefix(&p
, mac
, ip
);
4217 delete_evpn_route(bgp
, vpn
, &p
);
4223 * Handle add of a local MACIP.
4225 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
4226 struct ipaddr
*ip
, u_char flags
)
4228 struct bgpevpn
*vpn
;
4229 struct prefix_evpn p
;
4231 if (!bgp
->vnihash
) {
4232 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4236 /* Lookup VNI hash - should exist. */
4237 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4238 if (!vpn
|| !is_vni_live(vpn
)) {
4239 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP ADD",
4240 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
4244 /* Create EVPN type-2 route and schedule for processing. */
4245 build_evpn_type2_prefix(&p
, mac
, ip
);
4246 if (update_evpn_route(bgp
, vpn
, &p
, flags
)) {
4247 char buf
[ETHER_ADDR_STRLEN
];
4248 char buf2
[INET6_ADDRSTRLEN
];
4251 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
4252 bgp
->vrf_id
, vpn
->vni
,
4253 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? "sticky gateway"
4255 prefix_mac2str(mac
, buf
, sizeof(buf
)),
4256 ipaddr2str(ip
, buf2
, sizeof(buf2
)),
4264 static void link_l2vni_hash_to_l3vni(struct hash_backet
*backet
,
4265 struct bgp
*bgp_vrf
)
4267 struct bgpevpn
*vpn
= NULL
;
4268 struct bgp
*bgp_def
= NULL
;
4270 bgp_def
= bgp_get_default();
4273 vpn
= (struct bgpevpn
*)backet
->data
;
4274 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
4275 bgpevpn_link_to_l3vni(vpn
);
4278 int bgp_evpn_local_l3vni_add(vni_t l3vni
,
4280 struct ethaddr
*rmac
,
4281 struct in_addr originator_ip
,
4284 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
4285 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
4286 struct listnode
*node
= NULL
;
4287 struct bgpevpn
*vpn
= NULL
;
4290 /* get the default instamce - required to get the AS number for VRF
4293 bgp_def
= bgp_get_default();
4295 zlog_err("Cannot process L3VNI %u ADD - default BGP instance not yet created",
4301 /* if the BGP vrf instance doesnt exist - create one */
4302 bgp_vrf
= bgp_lookup_by_name(vrf_id_to_name(vrf_id
));
4307 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
4308 BGP_INSTANCE_TYPE_VRF
);
4310 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
:
4311 zlog_err("'bgp multiple-instance' not present\n");
4313 case BGP_ERR_AS_MISMATCH
:
4314 zlog_err("BGP is already running; AS is %u\n", as
);
4316 case BGP_ERR_INSTANCE_MISMATCH
:
4317 zlog_err("BGP instance name and AS number mismatch\n");
4321 /* mark as auto created */
4322 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
4325 /* associate with l3vni */
4326 bgp_vrf
->l3vni
= l3vni
;
4328 /* set the router mac - to be used in mac-ip routes for this vrf */
4329 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
4331 /* set the originator ip */
4332 bgp_vrf
->originator_ip
= originator_ip
;
4334 /* set the right filter - are we using l3vni only for prefix routes? */
4336 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
4338 /* auto derive RD/RT */
4339 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
4340 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4341 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
4342 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4343 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
4345 /* link all corresponding l2vnis */
4346 hash_iterate(bgp_def
->vnihash
,
4347 (void (*)(struct hash_backet
*, void *))
4348 link_l2vni_hash_to_l3vni
,
4351 /* Only update all corresponding type-2 routes if we are advertising two
4352 * labels along with type-2 routes
4355 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4356 update_routes_for_vni(bgp_def
, vpn
);
4358 /* advertise type-5 routes if needed */
4359 update_advertise_vrf_routes(bgp_vrf
);
4361 /* install all remote routes belonging to this l3vni into correspondng
4363 install_routes_for_vrf(bgp_vrf
);
4368 int bgp_evpn_local_l3vni_del(vni_t l3vni
,
4371 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
4372 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
4373 struct listnode
*node
= NULL
;
4374 struct bgpevpn
*vpn
= NULL
;
4376 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
4378 zlog_err("Cannot process L3VNI %u Del - Could not find BGP instance",
4383 bgp_def
= bgp_get_default();
4385 zlog_err("Cannot process L3VNI %u Del - Could not find default BGP instance",
4390 /* unimport remote routes from VRF, if it is AUTO vrf bgp_delete will
4391 * take care of uninstalling the routes from zebra
4393 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
4394 uninstall_routes_for_vrf(bgp_vrf
);
4396 /* delete/withdraw all type-5 routes */
4397 delete_withdraw_vrf_routes(bgp_vrf
);
4399 /* remove the l3vni from vrf instance */
4402 /* remove the Rmac from the BGP vrf */
4403 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
4406 if (bgp_vrf
->vrf_import_rtl
&& !list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4407 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4408 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
4410 if (bgp_vrf
->vrf_export_rtl
&& !list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4411 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
4414 /* update all corresponding local mac-ip routes */
4415 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
4416 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
4417 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
4418 update_routes_for_vni(bgp_def
, vpn
);
4422 /* Delete the instance if it was autocreated */
4423 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
4424 bgp_delete(bgp_vrf
);
4430 * Handle del of a local VNI.
4432 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
4434 struct bgpevpn
*vpn
;
4436 if (!bgp
->vnihash
) {
4437 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4441 /* Locate VNI hash */
4442 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4444 zlog_warn("%u: VNI hash entry for VNI %u not found at DEL",
4449 /* Remove all local EVPN routes and schedule for processing (to
4450 * withdraw from peers).
4452 delete_routes_for_vni(bgp
, vpn
);
4455 * tunnel is no longer active, del tunnel ip address from tip_hash
4457 bgp_tip_del(bgp
, &vpn
->originator_ip
);
4459 /* Clear "live" flag and see if hash needs to be freed. */
4460 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4461 if (!is_vni_configured(vpn
))
4462 bgp_evpn_free(bgp
, vpn
);
4468 * Handle add (or update) of a local VNI. The VNI changes we care
4469 * about are for the local-tunnel-ip and the (tenant) VRF.
4471 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
4472 struct in_addr originator_ip
,
4473 vrf_id_t tenant_vrf_id
)
4475 struct bgpevpn
*vpn
;
4476 struct prefix_evpn p
;
4478 if (!bgp
->vnihash
) {
4479 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4483 /* Lookup VNI. If present and no change, exit. */
4484 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4487 if (is_vni_live(vpn
)
4488 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
4489 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
4490 /* Probably some other param has changed that we don't
4494 /* Update tenant_vrf_id if it has changed. */
4495 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
4496 bgpevpn_unlink_from_l3vni(vpn
);
4497 vpn
->tenant_vrf_id
= tenant_vrf_id
;
4498 bgpevpn_link_to_l3vni(vpn
);
4501 /* If tunnel endpoint IP has changed, update (and delete prior
4502 * type-3 route, if needed.)
4504 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
4505 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
4507 /* Update all routes with new endpoint IP and/or export RT
4510 if (is_vni_live(vpn
))
4511 update_routes_for_vni(bgp
, vpn
);
4514 /* Create or update as appropriate. */
4516 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
);
4519 "%u: Failed to allocate VNI entry for VNI %u - at Add",
4525 /* if the VNI is live already, there is nothing more to do */
4526 if (is_vni_live(vpn
))
4529 /* Mark as "live" */
4530 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4532 /* tunnel is now active, add tunnel-ip to db */
4533 bgp_tip_add(bgp
, &originator_ip
);
4535 /* filter routes as nexthop database has changed */
4536 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
4538 /* Create EVPN type-3 route and schedule for processing. */
4539 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
4540 if (update_evpn_route(bgp
, vpn
, &p
, 0)) {
4541 zlog_err("%u: Type3 route creation failure for VNI %u",
4546 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
4550 install_routes_for_vni(bgp
, vpn
);
4552 /* If we are advertising gateway mac-ip
4553 It needs to be conveyed again to zebra */
4554 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
4560 * Cleanup EVPN information on disable - Need to delete and withdraw
4561 * EVPN routes from peers.
4563 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
4565 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
4566 void *))cleanup_vni_on_disable
,
4571 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
4572 * BGP instance (default) is being freed.
4574 void bgp_evpn_cleanup(struct bgp
*bgp
)
4577 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
4578 void *))free_vni_entry
,
4580 if (bgp
->import_rt_hash
)
4581 hash_free(bgp
->import_rt_hash
);
4582 bgp
->import_rt_hash
= NULL
;
4583 if (bgp
->vrf_import_rt_hash
)
4584 hash_free(bgp
->vrf_import_rt_hash
);
4585 bgp
->vrf_import_rt_hash
= NULL
;
4587 hash_free(bgp
->vnihash
);
4588 bgp
->vnihash
= NULL
;
4589 if (bgp
->vrf_import_rtl
)
4590 list_delete_and_null(&bgp
->vrf_import_rtl
);
4591 if (bgp
->vrf_export_rtl
)
4592 list_delete_and_null(&bgp
->vrf_export_rtl
);
4594 list_delete_and_null(&bgp
->l2vnis
);
4595 bf_release_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
4599 * Initialization for EVPN
4602 * hash for RT to VNI
4603 * assign a unique rd id for auto derivation of vrf_prd
4605 void bgp_evpn_init(struct bgp
*bgp
)
4608 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
4609 bgp
->import_rt_hash
=
4610 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
4611 "BGP Import RT Hash");
4612 bgp
->vrf_import_rt_hash
=
4613 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
4614 "BGP VRF Import RT Hash");
4615 bgp
->vrf_import_rtl
= list_new();
4616 bgp
->vrf_import_rtl
->cmp
=
4617 (int (*)(void *, void *))evpn_route_target_cmp
;
4619 bgp
->vrf_export_rtl
= list_new();
4620 bgp
->vrf_export_rtl
->cmp
=
4621 (int (*)(void *, void *))evpn_route_target_cmp
;
4622 bgp
->l2vnis
= list_new();
4624 (int (*)(void *, void *))vni_hash_cmp
;
4625 bf_assign_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
4629 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
4631 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);