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 * Add (update) or delete MACIP from zebra.
484 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
485 struct prefix_evpn
*p
,
486 struct in_addr remote_vtep_ip
, int add
,
491 char buf1
[ETHER_ADDR_STRLEN
];
492 char buf2
[INET6_ADDRSTRLEN
];
493 char buf3
[INET6_ADDRSTRLEN
];
496 if (!zclient
|| zclient
->sock
< 0)
499 /* Don't try to register if Zebra doesn't know of this instance. */
500 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
506 zclient_create_header(s
, add
? ZEBRA_REMOTE_MACIP_ADD
507 : ZEBRA_REMOTE_MACIP_DEL
,
509 stream_putl(s
, vpn
->vni
);
510 stream_put(s
, &p
->prefix
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
511 /* IP address length and IP address, if any. */
512 if (IS_EVPN_PREFIX_IPADDR_NONE(p
))
515 ipa_len
= IS_EVPN_PREFIX_IPADDR_V4(p
) ? IPV4_MAX_BYTELEN
517 stream_putl(s
, ipa_len
);
518 stream_put(s
, &p
->prefix
.ip
.ip
.addr
, ipa_len
);
520 stream_put_in_addr(s
, &remote_vtep_ip
);
522 /* TX flags - MAC sticky status and/or gateway mac */
524 stream_putc(s
, flags
);
526 stream_putw_at(s
, 0, stream_get_endp(s
));
528 if (bgp_debug_zebra(NULL
))
529 zlog_debug("Tx %s MACIP, VNI %u MAC %s IP %s (flags: 0x%x) remote VTEP %s",
530 add
? "ADD" : "DEL", vpn
->vni
,
531 prefix_mac2str(&p
->prefix
.mac
, buf1
, sizeof(buf1
)),
532 ipaddr2str(&p
->prefix
.ip
, buf3
, sizeof(buf3
)),
534 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
537 return zclient_send_message(zclient
);
541 * Add (update) or delete remote VTEP from zebra.
543 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
544 struct prefix_evpn
*p
, int add
)
549 if (!zclient
|| zclient
->sock
< 0)
552 /* Don't try to register if Zebra doesn't know of this instance. */
553 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
559 zclient_create_header(s
, add
? ZEBRA_REMOTE_VTEP_ADD
560 : ZEBRA_REMOTE_VTEP_DEL
,
562 stream_putl(s
, vpn
->vni
);
563 if (IS_EVPN_PREFIX_IPADDR_V4(p
))
564 stream_put_in_addr(s
, &p
->prefix
.ip
.ipaddr_v4
);
565 else if (IS_EVPN_PREFIX_IPADDR_V6(p
)) {
567 "Bad remote IP when trying to %s remote VTEP for VNI %u",
568 add
? "ADD" : "DEL", vpn
->vni
);
572 stream_putw_at(s
, 0, stream_get_endp(s
));
574 if (bgp_debug_zebra(NULL
))
575 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
576 add
? "ADD" : "DEL", vpn
->vni
,
577 inet_ntoa(p
->prefix
.ip
.ipaddr_v4
));
579 return zclient_send_message(zclient
);
583 * Build extended communities for EVPN prefix route.
585 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
588 struct ecommunity ecom_encap
;
589 struct ecommunity ecom_rmac
;
590 struct ecommunity_val eval
;
591 struct ecommunity_val eval_rmac
;
592 bgp_encap_types tnl_type
;
593 struct listnode
*node
, *nnode
;
594 struct ecommunity
*ecom
;
595 struct list
*vrf_export_rtl
= NULL
;
598 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
599 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
600 encode_encap_extcomm(tnl_type
, &eval
);
602 ecom_encap
.val
= (u_int8_t
*)eval
.val
;
605 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
607 /* Add the export RTs for L3VNI/VRF */
608 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
609 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
610 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
611 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
615 /* add the router mac extended community */
616 if (!is_zero_mac(&attr
->rmac
)) {
617 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
618 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
620 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
621 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
625 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
629 * Build extended communities for EVPN route. RT and ENCAP are
630 * applicable to all routes.
631 * TODO: currently kernel doesnt support ipv6 routes with ipv4 nexthops.
632 * This means that we can't do symmetric routing for ipv6 hosts routes
633 * in the same way as ipv4 host routes.
634 * We wont attach l3-vni related RTs for ipv6 routes.
635 * For now, We will only adevrtise ipv4 host routes
636 * with L3-VNI related ext-comm.
638 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
641 struct ecommunity ecom_encap
;
642 struct ecommunity ecom_sticky
;
643 struct ecommunity ecom_default_gw
;
644 struct ecommunity ecom_rmac
;
645 struct ecommunity_val eval
;
646 struct ecommunity_val eval_sticky
;
647 struct ecommunity_val eval_default_gw
;
648 struct ecommunity_val eval_rmac
;
649 bgp_encap_types tnl_type
;
650 struct listnode
*node
, *nnode
;
651 struct ecommunity
*ecom
;
653 struct list
*vrf_export_rtl
= NULL
;
656 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
657 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
658 encode_encap_extcomm(tnl_type
, &eval
);
660 ecom_encap
.val
= (u_int8_t
*)eval
.val
;
663 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
665 /* Add the export RTs for L2VNI */
666 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
667 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
669 /* Add the export RTs for L3VNI - currently only supported for IPV4 host
673 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
674 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
675 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
678 ecommunity_merge(attr
->ecommunity
,
685 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
686 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
687 ecom_sticky
.size
= 1;
688 ecom_sticky
.val
= (u_int8_t
*)eval_sticky
.val
;
690 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
693 if (afi
== AFI_IP
&& !is_zero_mac(&attr
->rmac
)) {
694 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
695 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
697 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
698 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
702 if (attr
->default_gw
) {
703 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
704 encode_default_gw_extcomm(&eval_default_gw
);
705 ecom_default_gw
.size
= 1;
706 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
707 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
711 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
715 * Add MAC mobility extended community to attribute.
717 static void add_mac_mobility_to_attr(u_int32_t seq_num
, struct attr
*attr
)
719 struct ecommunity ecom_tmp
;
720 struct ecommunity_val eval
;
721 u_int8_t
*ecom_val_ptr
;
728 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
730 /* Find current MM ecommunity */
733 if (attr
->ecommunity
) {
734 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
735 pnt
= attr
->ecommunity
->val
+ (i
* 8);
739 if (type
== ECOMMUNITY_ENCODE_EVPN
741 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
743 (u_int8_t
*)(attr
->ecommunity
->val
750 /* Update the existing MM ecommunity */
752 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
754 /* Add MM to existing */
756 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
758 ecom_tmp
.val
= (u_int8_t
*)eval
.val
;
761 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
765 /* Install EVPN route into zebra. */
766 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
767 struct prefix_evpn
*p
,
768 struct in_addr remote_vtep_ip
, u_char flags
)
772 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
773 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
776 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 1);
781 /* Uninstall EVPN route from zebra. */
782 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
783 struct prefix_evpn
*p
,
784 struct in_addr remote_vtep_ip
)
788 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
789 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
792 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 0);
798 * Due to MAC mobility, the prior "local" best route has been supplanted
799 * by a "remote" best route. The prior route has to be deleted and withdrawn
802 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
804 struct bgp_info
*old_local
)
806 struct bgp_node
*global_rn
;
808 afi_t afi
= AFI_L2VPN
;
809 safi_t safi
= SAFI_EVPN
;
811 /* Locate route node in the global EVPN routing table. Note that
812 * this table is a 2-level tree (RD-level + Prefix-level) similar to
815 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
816 (struct prefix
*)&rn
->p
, &vpn
->prd
);
818 /* Delete route entry in the global EVPN table. */
819 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
821 /* Schedule for processing - withdraws to peers happen from
825 bgp_process(bgp
, global_rn
, afi
, safi
);
826 bgp_unlock_node(global_rn
);
829 /* Delete route entry in the VNI route table, caller to remove. */
830 bgp_info_delete(rn
, old_local
);
834 * Calculate the best path for an EVPN route. Install/update best path in zebra,
837 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
840 struct bgp_info
*old_select
, *new_select
;
841 struct bgp_info_pair old_and_new
;
842 afi_t afi
= AFI_L2VPN
;
843 safi_t safi
= SAFI_EVPN
;
847 /* Compute the best path. */
848 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
850 old_select
= old_and_new
.old
;
851 new_select
= old_and_new
.new;
853 /* If the best path hasn't changed - see if there is still something to
857 if (old_select
&& old_select
== new_select
858 && old_select
->type
== ZEBRA_ROUTE_BGP
859 && old_select
->sub_type
== BGP_ROUTE_NORMAL
860 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
861 && !CHECK_FLAG(old_select
->flags
, BGP_INFO_ATTR_CHANGED
)
862 && !bgp
->addpath_tx_used
[afi
][safi
]) {
863 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
864 if (old_select
->attr
->sticky
)
865 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
866 if (old_select
->attr
->default_gw
)
867 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
868 ret
= evpn_zebra_install(bgp
, vpn
,
869 (struct prefix_evpn
*)&rn
->p
,
870 old_select
->attr
->nexthop
,
873 UNSET_FLAG(old_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
874 bgp_zebra_clear_route_change_flags(rn
);
878 /* If the user did a "clear" this flag will be set */
879 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
881 /* bestpath has changed; update relevant fields and install or uninstall
882 * into the zebra RIB.
884 if (old_select
|| new_select
)
885 bgp_bump_version(rn
);
888 bgp_info_unset_flag(rn
, old_select
, BGP_INFO_SELECTED
);
890 bgp_info_set_flag(rn
, new_select
, BGP_INFO_SELECTED
);
891 bgp_info_unset_flag(rn
, new_select
, BGP_INFO_ATTR_CHANGED
);
892 UNSET_FLAG(new_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
895 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
896 && new_select
->sub_type
== BGP_ROUTE_NORMAL
) {
898 if (new_select
->attr
->sticky
)
899 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
900 if (new_select
->attr
->default_gw
)
901 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
902 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
903 new_select
->attr
->nexthop
,
905 /* If an old best existed and it was a "local" route, the only
907 * it would be supplanted is due to MAC mobility procedures. So,
909 * need to do an implicit delete and withdraw that route from
912 if (old_select
&& old_select
->peer
== bgp
->peer_self
913 && old_select
->type
== ZEBRA_ROUTE_BGP
914 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
915 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
917 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
918 && old_select
->sub_type
== BGP_ROUTE_NORMAL
)
919 ret
= evpn_zebra_uninstall(bgp
, vpn
,
920 (struct prefix_evpn
*)&rn
->p
,
921 old_select
->attr
->nexthop
);
924 /* Clear any route change flags. */
925 bgp_zebra_clear_route_change_flags(rn
);
927 /* Reap old select bgp_info, if it has been removed */
928 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_INFO_REMOVED
))
929 bgp_info_reap(rn
, old_select
);
935 * Return true if the local ri for this rn is of type gateway mac
937 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
939 struct bgp_info
*tmp_ri
= NULL
;
940 struct bgp_info
*local_ri
= NULL
;
943 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
944 if (tmp_ri
->peer
== bgp
->peer_self
945 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
946 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
953 return local_ri
->attr
->default_gw
;
958 * Return true if the local ri for this rn has sticky set
960 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
962 struct bgp_info
*tmp_ri
;
963 struct bgp_info
*local_ri
;
966 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
967 if (tmp_ri
->peer
== bgp
->peer_self
968 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
969 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
976 return local_ri
->attr
->sticky
;
979 static int update_evpn_type5_route_entry(struct bgp
*bgp_def
,
980 struct bgp
*bgp_vrf
, afi_t afi
,
981 safi_t safi
, struct bgp_node
*rn
,
982 struct attr
*attr
, int *route_changed
)
984 struct attr
*attr_new
= NULL
;
985 struct bgp_info
*ri
= NULL
;
986 mpls_label_t label
= MPLS_INVALID_LABEL
;
987 struct bgp_info
*local_ri
= NULL
;
988 struct bgp_info
*tmp_ri
= NULL
;
991 /* locate the local route entry if any */
992 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
993 if (tmp_ri
->peer
== bgp_def
->peer_self
994 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
995 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
999 /* create a new route entry if one doesnt exist.
1000 Otherwise see if route attr has changed
1004 /* route has changed as this is the first entry */
1007 /* Add (or update) attribute to hash. */
1008 attr_new
= bgp_attr_intern(attr
);
1010 /* create the route info from attribute */
1011 ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1012 bgp_def
->peer_self
, attr_new
, rn
);
1013 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1015 /* Type-5 routes advertise the L3-VNI */
1016 bgp_info_extra_get(ri
);
1017 vni2label(bgp_vrf
->l3vni
, &label
);
1018 memcpy(&ri
->extra
->label
, &label
, sizeof(label
));
1019 ri
->extra
->num_labels
= 1;
1021 /* add the route entry to route node*/
1022 bgp_info_add(rn
, ri
);
1026 if (!attrhash_cmp(tmp_ri
->attr
, attr
)) {
1028 /* attribute changed */
1031 /* The attribute has changed. */
1032 /* Add (or update) attribute to hash. */
1033 attr_new
= bgp_attr_intern(attr
);
1034 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1036 /* Restore route, if needed. */
1037 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1038 bgp_info_restore(rn
, tmp_ri
);
1040 /* Unintern existing, set to new. */
1041 bgp_attr_unintern(&tmp_ri
->attr
);
1042 tmp_ri
->attr
= attr_new
;
1043 tmp_ri
->uptime
= bgp_clock();
1049 /* update evpn type-5 route entry */
1050 static int update_evpn_type5_route(struct bgp
*bgp_vrf
,
1051 struct prefix_evpn
*evp
,
1052 struct attr
* src_attr
)
1054 afi_t afi
= AFI_L2VPN
;
1055 safi_t safi
= SAFI_EVPN
;
1057 struct bgp_node
*rn
= NULL
;
1058 struct bgp
*bgp_def
= NULL
;
1059 int route_changed
= 0;
1061 bgp_def
= bgp_get_default();
1065 /* Build path attribute for this route - use the source attr, if
1066 * present, else treat as locally originated.
1069 bgp_attr_dup(&attr
, src_attr
);
1071 memset(&attr
, 0, sizeof(struct attr
));
1072 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1074 /* Set nexthop to ourselves and fill in the Router MAC. */
1075 attr
.nexthop
= bgp_vrf
->originator_ip
;
1076 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1077 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1078 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
1080 /* Setup RT and encap extended community */
1081 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1083 /* get the route node in global table */
1084 rn
= bgp_afi_node_get(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1085 (struct prefix
*)evp
,
1089 /* create or update the route entry within the route node */
1090 update_evpn_type5_route_entry(bgp_def
, bgp_vrf
,
1092 rn
, &attr
, &route_changed
);
1094 /* schedule for processing and unlock node */
1095 if (route_changed
) {
1096 bgp_process(bgp_def
, rn
, afi
, safi
);
1097 bgp_unlock_node(rn
);
1100 /* uninten temporary */
1102 aspath_unintern(&attr
.aspath
);
1107 * Create or update EVPN route entry. This could be in the VNI route table
1108 * or the global route table.
1110 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1111 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1112 struct attr
*attr
, int add
, int vni_table
,
1113 struct bgp_info
**ri
, u_char flags
)
1115 struct bgp_info
*tmp_ri
;
1116 struct bgp_info
*local_ri
, *remote_ri
;
1117 struct attr
*attr_new
;
1118 mpls_label_t label
[BGP_MAX_LABELS
];
1119 u_int32_t num_labels
= 1;
1120 int route_change
= 1;
1122 struct prefix_evpn
*evp
;
1125 evp
= (struct prefix_evpn
*)&rn
->p
;
1126 memset(&label
, 0, sizeof(label
));
1128 /* See if this is an update of an existing route, or a new add. Also,
1129 * identify if already known from remote, and if so, the one with the
1130 * highest sequence number; this is only when adding to the VNI routing
1133 local_ri
= remote_ri
= NULL
;
1134 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1135 if (tmp_ri
->peer
== bgp
->peer_self
1136 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1137 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1140 if (tmp_ri
->type
== ZEBRA_ROUTE_BGP
1141 && tmp_ri
->sub_type
== BGP_ROUTE_NORMAL
1142 && CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
)) {
1145 else if (mac_mobility_seqnum(tmp_ri
->attr
)
1146 > mac_mobility_seqnum(remote_ri
->attr
))
1152 /* If route doesn't exist already, create a new one, if told to.
1153 * Otherwise act based on whether the attributes of the route have
1156 if (!local_ri
&& !add
)
1160 /* When learnt locally for the first time but already known from
1161 * remote, we have to initiate appropriate MAC mobility steps.
1163 * is applicable when updating the VNI routing table.
1164 * We need to skip mobility steps for g/w macs (local mac on g/w
1165 * SVI) advertised in EVPN.
1166 * This will ensure that local routes are preferred for g/w macs
1168 if (remote_ri
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
)) {
1169 u_int32_t cur_seqnum
;
1171 /* Add MM extended community to route. */
1172 cur_seqnum
= mac_mobility_seqnum(remote_ri
->attr
);
1173 add_mac_mobility_to_attr(cur_seqnum
+ 1, attr
);
1176 /* Add (or update) attribute to hash. */
1177 attr_new
= bgp_attr_intern(attr
);
1179 /* Extract MAC mobility sequence number, if any. */
1180 attr_new
->mm_seqnum
=
1181 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1182 attr_new
->sticky
= sticky
;
1184 /* Create new route with its attribute. */
1185 tmp_ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1186 bgp
->peer_self
, attr_new
, rn
);
1187 SET_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
);
1188 bgp_info_extra_get(tmp_ri
);
1190 /* The VNI goes into the 'label' field of the route */
1191 vni2label(vpn
->vni
, &label
[0]);
1192 /* Type-2 routes may carry a second VNI - the L3-VNI */
1193 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
1196 l3vni
= bgpevpn_get_l3vni(vpn
);
1198 vni2label(l3vni
, &label
[1]);
1203 memcpy(&tmp_ri
->extra
->label
, label
, sizeof(label
));
1204 tmp_ri
->extra
->num_labels
= num_labels
;
1205 bgp_info_add(rn
, tmp_ri
);
1208 if (attrhash_cmp(tmp_ri
->attr
, attr
)
1209 && !CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1212 /* The attribute has changed. */
1213 /* Add (or update) attribute to hash. */
1214 attr_new
= bgp_attr_intern(attr
);
1215 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1217 /* Restore route, if needed. */
1218 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1219 bgp_info_restore(rn
, tmp_ri
);
1221 /* Unintern existing, set to new. */
1222 bgp_attr_unintern(&tmp_ri
->attr
);
1223 tmp_ri
->attr
= attr_new
;
1224 tmp_ri
->uptime
= bgp_clock();
1228 /* Return back the route entry. */
1230 return route_change
;
1234 * Create or update EVPN route (of type based on prefix) for specified VNI
1235 * and schedule for processing.
1237 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1238 struct prefix_evpn
*p
, u_char flags
)
1240 struct bgp_node
*rn
;
1242 struct attr
*attr_new
;
1243 struct bgp_info
*ri
;
1244 afi_t afi
= AFI_L2VPN
;
1245 safi_t safi
= SAFI_EVPN
;
1248 memset(&attr
, 0, sizeof(struct attr
));
1250 /* Build path-attribute for this route. */
1251 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1252 attr
.nexthop
= vpn
->originator_ip
;
1253 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1254 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1255 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1256 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1258 /* PMSI is only needed for type-3 routes */
1259 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
)
1260 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1262 /* router mac is only needed for type-2 and type-5 routes */
1263 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1264 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1265 vni2label(vpn
->vni
, &(attr
.label
));
1267 /* Set up RT and ENCAP extended community. */
1268 build_evpn_route_extcomm(vpn
, &attr
,
1269 IS_EVPN_PREFIX_IPADDR_V4(p
) ?
1272 /* First, create (or fetch) route node within the VNI. */
1273 /* NOTE: There is no RD here. */
1274 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1276 /* Create or update route entry. */
1277 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1280 attr_new
= ri
->attr
;
1282 /* Perform route selection; this is just to set the flags correctly
1283 * as local route in the VNI always wins.
1285 evpn_route_select_install(bgp
, vpn
, rn
);
1286 bgp_unlock_node(rn
);
1288 /* If this is a new route or some attribute has changed, export the
1289 * route to the global table. The route will be advertised to peers
1290 * from there. Note that this table is a 2-level tree (RD-level +
1291 * Prefix-level) similar to L3VPN routes.
1294 struct bgp_info
*global_ri
;
1296 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1297 (struct prefix
*)p
, &vpn
->prd
);
1298 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1, 0,
1301 /* Schedule for processing and unlock node. */
1302 bgp_process(bgp
, rn
, afi
, safi
);
1303 bgp_unlock_node(rn
);
1306 /* Unintern temporary. */
1307 aspath_unintern(&attr
.aspath
);
1312 /* Delete EVPN type5 route entry from global table */
1313 static void delete_evpn_type5_route_entry(struct bgp
*bgp_def
,
1314 struct bgp
*bgp_vrf
,
1315 afi_t afi
, safi_t safi
,
1316 struct bgp_node
*rn
,
1317 struct bgp_info
**ri
)
1319 struct bgp_info
*tmp_ri
= NULL
;
1323 /* find the matching route entry */
1324 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
)
1325 if (tmp_ri
->peer
== bgp_def
->peer_self
1326 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1327 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1332 /* Mark route for delete. */
1334 bgp_info_delete(rn
, tmp_ri
);
1337 /* Delete EVPN type5 route */
1338 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
,
1339 struct prefix_evpn
*evp
)
1341 afi_t afi
= AFI_L2VPN
;
1342 safi_t safi
= SAFI_EVPN
;
1343 struct bgp_node
*rn
= NULL
;
1344 struct bgp_info
*ri
= NULL
;
1345 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
1347 bgp_def
= bgp_get_default();
1351 /* locate the global route entry for this type-5 prefix */
1352 rn
= bgp_afi_node_lookup(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1353 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1357 delete_evpn_type5_route_entry(bgp_def
, bgp_vrf
, afi
, safi
, rn
, &ri
);
1359 bgp_process(bgp_def
, rn
, afi
, safi
);
1360 bgp_unlock_node(rn
);
1365 * Delete EVPN route entry. This could be in the VNI route table
1366 * or the global route table.
1368 static void delete_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1369 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1370 struct bgp_info
**ri
)
1372 struct bgp_info
*tmp_ri
;
1376 /* Now, find matching route. */
1377 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
)
1378 if (tmp_ri
->peer
== bgp
->peer_self
1379 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1380 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1385 /* Mark route for delete. */
1387 bgp_info_delete(rn
, tmp_ri
);
1391 * Delete EVPN route (of type based on prefix) for specified VNI and
1392 * schedule for processing.
1394 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1395 struct prefix_evpn
*p
)
1397 struct bgp_node
*rn
, *global_rn
;
1398 struct bgp_info
*ri
;
1399 afi_t afi
= AFI_L2VPN
;
1400 safi_t safi
= SAFI_EVPN
;
1402 /* First, locate the route node within the VNI. If it doesn't exist,
1404 * is nothing further to do.
1406 /* NOTE: There is no RD here. */
1407 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1411 /* Next, locate route node in the global EVPN routing table. Note that
1412 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1415 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1416 (struct prefix
*)p
, &vpn
->prd
);
1418 /* Delete route entry in the global EVPN table. */
1419 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
1421 /* Schedule for processing - withdraws to peers happen from
1425 bgp_process(bgp
, global_rn
, afi
, safi
);
1426 bgp_unlock_node(global_rn
);
1429 /* Delete route entry in the VNI route table. This can just be removed.
1431 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1433 bgp_info_reap(rn
, ri
);
1434 bgp_unlock_node(rn
);
1440 * Update all type-2 (MACIP) local routes for this VNI - these should also
1441 * be scheduled for advertise to peers.
1443 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1447 struct bgp_node
*rn
;
1448 struct bgp_info
*ri
;
1450 struct attr attr_sticky
;
1451 struct attr attr_def_gw
;
1452 struct attr attr_ip6
;
1453 struct attr attr_sticky_ip6
;
1454 struct attr attr_def_gw_ip6
;
1455 struct attr
*attr_new
;
1459 memset(&attr
, 0, sizeof(struct attr
));
1460 memset(&attr_sticky
, 0, sizeof(struct attr
));
1461 memset(&attr_def_gw
, 0, sizeof(struct attr
));
1462 memset(&attr_ip6
, 0, sizeof(struct attr
));
1463 memset(&attr_sticky_ip6
, 0, sizeof(struct attr
));
1464 memset(&attr_def_gw_ip6
, 0, sizeof(struct attr
));
1466 /* Build path-attribute - all type-2 routes for this VNI will share the
1467 * same path attribute.
1469 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1470 bgp_attr_default_set(&attr_sticky
, BGP_ORIGIN_IGP
);
1471 bgp_attr_default_set(&attr_def_gw
, BGP_ORIGIN_IGP
);
1472 attr
.nexthop
= vpn
->originator_ip
;
1473 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1474 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1475 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1476 attr_sticky
.nexthop
= vpn
->originator_ip
;
1477 attr_sticky
.mp_nexthop_global_in
= vpn
->originator_ip
;
1478 attr_sticky
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1479 attr_sticky
.sticky
= 1;
1480 bgpevpn_get_rmac(vpn
, &attr_sticky
.rmac
);
1481 attr_def_gw
.nexthop
= vpn
->originator_ip
;
1482 attr_def_gw
.mp_nexthop_global_in
= vpn
->originator_ip
;
1483 attr_def_gw
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1484 attr_def_gw
.default_gw
= 1;
1485 bgpevpn_get_rmac(vpn
, &attr_def_gw
.rmac
);
1486 bgp_attr_default_set(&attr_ip6
, BGP_ORIGIN_IGP
);
1487 bgp_attr_default_set(&attr_sticky_ip6
, BGP_ORIGIN_IGP
);
1488 bgp_attr_default_set(&attr_def_gw_ip6
, BGP_ORIGIN_IGP
);
1489 attr_ip6
.nexthop
= vpn
->originator_ip
;
1490 attr_ip6
.mp_nexthop_global_in
= vpn
->originator_ip
;
1491 attr_ip6
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1492 bgpevpn_get_rmac(vpn
, &attr_ip6
.rmac
);
1493 attr_sticky_ip6
.nexthop
= vpn
->originator_ip
;
1494 attr_sticky_ip6
.mp_nexthop_global_in
= vpn
->originator_ip
;
1495 attr_sticky_ip6
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1496 attr_sticky_ip6
.sticky
= 1;
1497 bgpevpn_get_rmac(vpn
, &attr_sticky_ip6
.rmac
);
1498 attr_def_gw_ip6
.nexthop
= vpn
->originator_ip
;
1499 attr_def_gw_ip6
.mp_nexthop_global_in
= vpn
->originator_ip
;
1500 attr_def_gw_ip6
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1501 attr_def_gw_ip6
.default_gw
= 1;
1502 bgpevpn_get_rmac(vpn
, &attr_def_gw_ip6
.rmac
);
1504 /* Set up RT, ENCAP and sticky MAC extended community. */
1505 build_evpn_route_extcomm(vpn
, &attr
, AFI_IP
);
1506 build_evpn_route_extcomm(vpn
, &attr_sticky
, AFI_IP
);
1507 build_evpn_route_extcomm(vpn
, &attr_def_gw
, AFI_IP
);
1508 build_evpn_route_extcomm(vpn
, &attr_ip6
, AFI_IP6
);
1509 build_evpn_route_extcomm(vpn
, &attr_sticky_ip6
, AFI_IP6
);
1510 build_evpn_route_extcomm(vpn
, &attr_def_gw_ip6
, AFI_IP
);
1512 /* Walk this VNI's route table and update local type-2 routes. For any
1513 * routes updated, update corresponding entry in the global table too.
1515 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1516 rn
= bgp_route_next(rn
)) {
1517 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1518 struct bgp_node
*rd_rn
;
1519 struct bgp_info
*global_ri
;
1521 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1524 if (IS_EVPN_PREFIX_IPADDR_V4(evp
)) {
1525 if (evpn_route_is_sticky(bgp
, rn
))
1526 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1529 else if (evpn_route_is_def_gw(bgp
, rn
))
1530 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1534 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1535 &attr
, 0, 1, &ri
, 0);
1537 if (evpn_route_is_sticky(bgp
, rn
))
1538 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1539 &attr_sticky_ip6
, 0, 1,
1541 else if (evpn_route_is_def_gw(bgp
, rn
))
1542 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1543 &attr_def_gw_ip6
, 0, 1,
1546 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1551 /* If a local route exists for this prefix, we need to update
1552 * the global routing table too.
1557 /* Perform route selection; this is just to set the flags
1559 * as local route in the VNI always wins.
1561 evpn_route_select_install(bgp
, vpn
, rn
);
1563 attr_new
= ri
->attr
;
1565 /* Update route in global routing table. */
1566 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1567 (struct prefix
*)evp
, &vpn
->prd
);
1569 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
1572 /* Schedule for processing and unlock node. */
1573 bgp_process(bgp
, rd_rn
, afi
, safi
);
1574 bgp_unlock_node(rd_rn
);
1577 /* Unintern temporary. */
1578 aspath_unintern(&attr
.aspath
);
1579 aspath_unintern(&attr_ip6
.aspath
);
1580 aspath_unintern(&attr_sticky
.aspath
);
1581 aspath_unintern(&attr_sticky_ip6
.aspath
);
1582 aspath_unintern(&attr_def_gw
.aspath
);
1583 aspath_unintern(&attr_def_gw_ip6
.aspath
);
1589 * Delete all type-2 (MACIP) local routes for this VNI - only from the
1590 * global routing table. These are also scheduled for withdraw from peers.
1592 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1596 struct bgp_node
*rdrn
, *rn
;
1597 struct bgp_table
*table
;
1598 struct bgp_info
*ri
;
1603 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
1604 if (rdrn
&& rdrn
->info
) {
1605 table
= (struct bgp_table
*)rdrn
->info
;
1606 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
1607 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1609 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1612 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1614 bgp_process(bgp
, rn
, afi
, safi
);
1618 /* Unlock RD node. */
1620 bgp_unlock_node(rdrn
);
1626 * Delete all type-2 (MACIP) local routes for this VNI - from the global
1627 * table as well as the per-VNI route table.
1629 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1633 struct bgp_node
*rn
;
1634 struct bgp_info
*ri
;
1639 /* First, walk the global route table for this VNI's type-2 local
1641 * EVPN routes are a 2-level table, first get the RD table.
1643 delete_global_type2_routes(bgp
, vpn
);
1645 /* Next, walk this VNI's route table and delete local type-2 routes. */
1646 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1647 rn
= bgp_route_next(rn
)) {
1648 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1650 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1653 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1655 /* Route entry in local table gets deleted immediately. */
1657 bgp_info_reap(rn
, ri
);
1664 * Delete all routes in the per-VNI route table.
1666 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1668 struct bgp_node
*rn
;
1669 struct bgp_info
*ri
, *nextri
;
1671 /* Walk this VNI's route table and delete all routes. */
1672 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1673 rn
= bgp_route_next(rn
)) {
1674 for (ri
= rn
->info
; (ri
!= NULL
) && (nextri
= ri
->next
, 1);
1676 bgp_info_delete(rn
, ri
);
1677 bgp_info_reap(rn
, ri
);
1685 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
1686 * export RT getting modified or change to tunnel IP. Note that these
1687 * situations need the route in the per-VNI table as well as the global
1688 * table to be updated (as attributes change).
1690 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1693 struct prefix_evpn p
;
1695 /* Update and advertise the type-3 route (only one) followed by the
1696 * locally learnt type-2 routes (MACIP) - for this VNI.
1698 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1699 ret
= update_evpn_route(bgp
, vpn
, &p
, 0);
1703 return update_all_type2_routes(bgp
, vpn
);
1707 * Delete (and withdraw) local routes for specified VNI from the global
1708 * table and per-VNI table. After this, remove all other routes from
1709 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
1710 * (advertise-all-vni) being disabled.
1712 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1715 struct prefix_evpn p
;
1717 /* Delete and withdraw locally learnt type-2 routes (MACIP)
1718 * followed by type-3 routes (only one) - for this VNI.
1720 ret
= delete_all_type2_routes(bgp
, vpn
);
1724 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1725 ret
= delete_evpn_route(bgp
, vpn
, &p
);
1729 /* Delete all routes from the per-VNI table. */
1730 return delete_all_vni_routes(bgp
, vpn
);
1734 * There is a tunnel endpoint IP address change for this VNI,
1735 * need to re-advertise routes with the new nexthop.
1737 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1738 struct in_addr originator_ip
)
1740 struct prefix_evpn p
;
1742 /* If VNI is not live, we only need to update the originator ip */
1743 if (!is_vni_live(vpn
)) {
1744 vpn
->originator_ip
= originator_ip
;
1748 /* Update the tunnel-ip hash */
1749 bgp_tip_del(bgp
, &vpn
->originator_ip
);
1750 bgp_tip_add(bgp
, &originator_ip
);
1752 /* filter routes as martian nexthop db has changed */
1753 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
1755 /* Need to withdraw type-3 route as the originator IP is part
1758 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1759 delete_evpn_route(bgp
, vpn
, &p
);
1761 /* Update the tunnel IP and re-advertise all routes for this VNI. */
1762 vpn
->originator_ip
= originator_ip
;
1763 return update_routes_for_vni(bgp
, vpn
);
1767 * Install route entry into the VRF routing table and invoke route selection.
1769 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
1770 struct prefix_evpn
*evp
,
1771 struct bgp_info
*parent_ri
)
1773 struct bgp_node
*rn
;
1774 struct bgp_info
*ri
;
1775 struct attr
*attr_new
;
1778 struct prefix
*pp
= &p
;
1781 char buf
[PREFIX_STRLEN
];
1782 char buf1
[PREFIX_STRLEN
];
1784 memset(pp
, 0, sizeof(struct prefix
));
1785 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1786 ip_prefix_from_type2_prefix(evp
, pp
);
1787 else if (evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
)
1788 ip_prefix_from_type5_prefix(evp
, pp
);
1790 if (bgp_debug_zebra(NULL
)) {
1791 zlog_debug("installing evpn prefix %s as ip prefix %s in vrf %s",
1792 prefix2str(evp
, buf
, sizeof(buf
)),
1793 prefix2str(pp
, buf1
, sizeof(buf
)),
1794 vrf_id_to_name(bgp_vrf
->vrf_id
));
1797 /* Create (or fetch) route within the VRF. */
1798 /* NOTE: There is no RD here. */
1799 if (IS_EVPN_PREFIX_IPADDR_V4(evp
)) {
1801 safi
= SAFI_UNICAST
;
1802 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
1803 } else if (IS_EVPN_PREFIX_IPADDR_V6(evp
)) {
1805 safi
= SAFI_UNICAST
;
1806 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
1810 /* Check if route entry is already present. */
1811 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1813 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
1817 /* Add (or update) attribute to hash. */
1818 attr_new
= bgp_attr_intern(parent_ri
->attr
);
1820 /* Create new route with its attribute. */
1821 ri
= info_make(parent_ri
->type
, parent_ri
->sub_type
, 0,
1822 parent_ri
->peer
, attr_new
, rn
);
1823 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1824 bgp_info_extra_get(ri
);
1825 ri
->extra
->parent
= parent_ri
;
1826 if (parent_ri
->extra
) {
1827 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
1828 sizeof(ri
->extra
->label
));
1829 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
1831 bgp_info_add(rn
, ri
);
1833 if (attrhash_cmp(ri
->attr
, parent_ri
->attr
)
1834 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
1835 bgp_unlock_node(rn
);
1838 /* The attribute has changed. */
1839 /* Add (or update) attribute to hash. */
1840 attr_new
= bgp_attr_intern(parent_ri
->attr
);
1842 /* Restore route, if needed. */
1843 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
1844 bgp_info_restore(rn
, ri
);
1846 /* Mark if nexthop has changed. */
1847 if (!IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
))
1848 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
1850 /* Unintern existing, set to new. */
1851 bgp_attr_unintern(&ri
->attr
);
1852 ri
->attr
= attr_new
;
1853 ri
->uptime
= bgp_clock();
1856 /* Perform route selection and update zebra, if required. */
1857 bgp_process(bgp_vrf
, rn
, afi
, safi
);
1863 * Install route entry into the VNI routing table and invoke route selection.
1865 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1866 struct prefix_evpn
*p
,
1867 struct bgp_info
*parent_ri
)
1869 struct bgp_node
*rn
;
1870 struct bgp_info
*ri
;
1871 struct attr
*attr_new
;
1874 /* Create (or fetch) route within the VNI. */
1875 /* NOTE: There is no RD here. */
1876 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1878 /* Check if route entry is already present. */
1879 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1881 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
1885 /* Add (or update) attribute to hash. */
1886 attr_new
= bgp_attr_intern(parent_ri
->attr
);
1888 /* Create new route with its attribute. */
1889 ri
= info_make(parent_ri
->type
, parent_ri
->sub_type
, 0,
1890 parent_ri
->peer
, attr_new
, rn
);
1891 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1892 bgp_info_extra_get(ri
);
1893 ri
->extra
->parent
= parent_ri
;
1894 if (parent_ri
->extra
) {
1895 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
1896 sizeof(ri
->extra
->label
));
1897 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
1899 bgp_info_add(rn
, ri
);
1901 if (attrhash_cmp(ri
->attr
, parent_ri
->attr
)
1902 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
1903 bgp_unlock_node(rn
);
1906 /* The attribute has changed. */
1907 /* Add (or update) attribute to hash. */
1908 attr_new
= bgp_attr_intern(parent_ri
->attr
);
1910 /* Restore route, if needed. */
1911 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
1912 bgp_info_restore(rn
, ri
);
1914 /* Mark if nexthop has changed. */
1915 if (!IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
))
1916 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
1918 /* Unintern existing, set to new. */
1919 bgp_attr_unintern(&ri
->attr
);
1920 ri
->attr
= attr_new
;
1921 ri
->uptime
= bgp_clock();
1924 /* Perform route selection and update zebra, if required. */
1925 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
1931 * Uninstall route entry from the VRF routing table and send message
1932 * to zebra, if appropriate.
1934 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
1935 struct prefix_evpn
*evp
,
1936 struct bgp_info
*parent_ri
)
1938 struct bgp_node
*rn
;
1939 struct bgp_info
*ri
;
1942 struct prefix
*pp
= &p
;
1945 char buf
[PREFIX_STRLEN
];
1946 char buf1
[PREFIX_STRLEN
];
1948 memset(pp
, 0, sizeof(struct prefix
));
1949 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1950 ip_prefix_from_type2_prefix(evp
, pp
);
1951 else if (evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
)
1952 ip_prefix_from_type5_prefix(evp
, pp
);
1954 if (bgp_debug_zebra(NULL
)) {
1955 zlog_debug("uninstalling evpn prefix %s as ip prefix %s in vrf %s",
1956 prefix2str(evp
, buf
, sizeof(buf
)),
1957 prefix2str(pp
, buf1
, sizeof(buf
)),
1958 vrf_id_to_name(bgp_vrf
->vrf_id
));
1961 /* Locate route within the VRF. */
1962 /* NOTE: There is no RD here. */
1963 if (IS_EVPN_PREFIX_IPADDR_V4(evp
)) {
1965 safi
= SAFI_UNICAST
;
1966 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
1969 safi
= SAFI_UNICAST
;
1970 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
1976 /* Find matching route entry. */
1977 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1979 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
1985 /* Mark entry for deletion */
1986 bgp_info_delete(rn
, ri
);
1988 /* Perform route selection and update zebra, if required. */
1989 bgp_process(bgp_vrf
, rn
, afi
, safi
);
1991 /* Unlock route node. */
1992 bgp_unlock_node(rn
);
1998 * Uninstall route entry from the VNI routing table and send message
1999 * to zebra, if appropriate.
2001 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2002 struct prefix_evpn
*p
,
2003 struct bgp_info
*parent_ri
)
2005 struct bgp_node
*rn
;
2006 struct bgp_info
*ri
;
2009 /* Locate route within the VNI. */
2010 /* NOTE: There is no RD here. */
2011 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
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 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2030 /* Unlock route node. */
2031 bgp_unlock_node(rn
);
2037 * Given a route entry and a VRF, see if this route entry should be
2038 * imported into the VRF i.e., RTs match.
2040 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2041 struct bgp_info
*ri
)
2043 struct attr
*attr
= ri
->attr
;
2044 struct ecommunity
*ecom
;
2048 /* Route should have valid RT to be even considered. */
2049 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2052 ecom
= attr
->ecommunity
;
2053 if (!ecom
|| !ecom
->size
)
2056 /* For each extended community RT, see if it matches this VNI. If any RT
2057 * matches, we're done.
2059 for (i
= 0; i
< ecom
->size
; i
++) {
2061 u_char type
, sub_type
;
2062 struct ecommunity_val
*eval
;
2063 struct ecommunity_val eval_tmp
;
2064 struct vrf_irt_node
*irt
;
2066 /* Only deal with RTs */
2067 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2068 eval
= (struct ecommunity_val
*)(ecom
->val
2069 + (i
* ECOMMUNITY_SIZE
));
2072 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2075 /* See if this RT matches specified VNIs import RTs */
2076 irt
= lookup_vrf_import_rt(eval
);
2077 if (irt
&& irt
->vrfs
)
2078 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2081 /* Also check for non-exact match. In this, we mask out the AS
2083 * only check on the local-admin sub-field. This is to
2085 * VNI as the RT for EBGP peering too.
2088 if (type
== ECOMMUNITY_ENCODE_AS
2089 || type
== ECOMMUNITY_ENCODE_AS4
2090 || type
== ECOMMUNITY_ENCODE_IP
) {
2091 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2092 mask_ecom_global_admin(&eval_tmp
, eval
);
2093 irt
= lookup_vrf_import_rt(&eval_tmp
);
2095 if (irt
&& irt
->vrfs
)
2096 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2104 * Given a route entry and a VNI, see if this route entry should be
2105 * imported into the VNI i.e., RTs match.
2107 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2108 struct bgp_info
*ri
)
2110 struct attr
*attr
= ri
->attr
;
2111 struct ecommunity
*ecom
;
2115 /* Route should have valid RT to be even considered. */
2116 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2119 ecom
= attr
->ecommunity
;
2120 if (!ecom
|| !ecom
->size
)
2123 /* For each extended community RT, see if it matches this VNI. If any RT
2124 * matches, we're done.
2126 for (i
= 0; i
< ecom
->size
; i
++) {
2128 u_char type
, sub_type
;
2129 struct ecommunity_val
*eval
;
2130 struct ecommunity_val eval_tmp
;
2131 struct irt_node
*irt
;
2133 /* Only deal with RTs */
2134 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2135 eval
= (struct ecommunity_val
*)(ecom
->val
2136 + (i
* ECOMMUNITY_SIZE
));
2139 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2142 /* See if this RT matches specified VNIs import RTs */
2143 irt
= lookup_import_rt(bgp
, eval
);
2144 if (irt
&& irt
->vnis
)
2145 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2148 /* Also check for non-exact match. In this, we mask out the AS
2150 * only check on the local-admin sub-field. This is to
2152 * VNI as the RT for EBGP peering too.
2155 if (type
== ECOMMUNITY_ENCODE_AS
2156 || type
== ECOMMUNITY_ENCODE_AS4
2157 || type
== ECOMMUNITY_ENCODE_IP
) {
2158 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2159 mask_ecom_global_admin(&eval_tmp
, eval
);
2160 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2162 if (irt
&& irt
->vnis
)
2163 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2171 * Install or uninstall mac-ip routes are appropriate for this
2174 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
,
2179 struct bgp_node
*rd_rn
, *rn
;
2180 struct bgp_table
*table
;
2181 struct bgp_info
*ri
;
2183 char buf
[PREFIX_STRLEN
];
2184 struct bgp
*bgp_def
= NULL
;
2188 bgp_def
= bgp_get_default();
2192 /* Walk entire global routing table and evaluate routes which could be
2193 * imported into this VRF. Note that we need to loop through all global
2194 * routes to determine which route matches the import rt on vrf
2196 for (rd_rn
= bgp_table_top(bgp_def
->rib
[afi
][safi
]); rd_rn
;
2197 rd_rn
= bgp_route_next(rd_rn
)) {
2198 table
= (struct bgp_table
*)(rd_rn
->info
);
2202 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2203 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2205 /* if not mac-ip route skip this route */
2206 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
2207 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
2210 /* if not a mac+ip route skip this route */
2211 if (!(IS_EVPN_PREFIX_IPADDR_V4(evp
) ||
2212 IS_EVPN_PREFIX_IPADDR_V6(evp
)))
2215 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2216 /* Consider "valid" remote routes applicable for
2219 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2220 && ri
->type
== ZEBRA_ROUTE_BGP
2221 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2224 if (is_route_matching_for_vrf(bgp_vrf
, ri
)) {
2227 install_evpn_route_entry_in_vrf(
2231 uninstall_evpn_route_entry_in_vrf(
2236 "Failed to %s EVPN %s route in VRF %s",
2239 prefix2str(evp
, buf
,
2241 vrf_id_to_name(bgp_vrf
->vrf_id
));
2253 * Install or uninstall routes of specified type that are appropriate for this
2256 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
2257 struct bgpevpn
*vpn
,
2258 bgp_evpn_route_type rtype
,
2263 struct bgp_node
*rd_rn
, *rn
;
2264 struct bgp_table
*table
;
2265 struct bgp_info
*ri
;
2271 /* Walk entire global routing table and evaluate routes which could be
2272 * imported into this VPN. Note that we cannot just look at the routes
2274 * the VNI's RD - remote routes applicable for this VNI could have any
2277 /* EVPN routes are a 2-level table. */
2278 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2279 rd_rn
= bgp_route_next(rd_rn
)) {
2280 table
= (struct bgp_table
*)(rd_rn
->info
);
2284 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2285 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2287 if (evp
->prefix
.route_type
!= rtype
)
2290 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2291 /* Consider "valid" remote routes applicable for
2293 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2294 && ri
->type
== ZEBRA_ROUTE_BGP
2295 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2298 if (is_route_matching_for_vni(bgp
, vpn
, ri
)) {
2300 ret
= install_evpn_route_entry(
2303 ret
= uninstall_evpn_route_entry(
2308 "%u: Failed to %s EVPN %s route in VNI %u",
2312 rtype
== BGP_EVPN_MAC_IP_ROUTE
2326 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
2327 * is invoked upon l3vni-add or l3vni import rt change
2329 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
2331 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
2336 * Install any existing remote routes applicable for this VNI into its
2337 * routing table. This is invoked when a VNI becomes "live" or its Import
2340 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2344 /* Install type-3 routes followed by type-2 routes - the ones applicable
2347 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
2352 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
2356 /* uninstall routes from l3vni vrf. */
2357 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
2359 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
2364 * Uninstall any existing remote routes for this VNI. One scenario in which
2365 * this is invoked is upon an import RT change.
2367 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2371 /* Uninstall type-2 routes followed by type-3 routes - the ones
2375 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
2380 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
2385 * Install or uninstall route in matching VRFs (list).
2387 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
2388 safi_t safi
, struct prefix_evpn
*evp
,
2389 struct bgp_info
*ri
,
2390 struct list
*vrfs
, int install
)
2392 char buf
[PREFIX2STR_BUFFER
];
2393 struct bgp
*bgp_vrf
;
2394 struct listnode
*node
, *nnode
;
2396 /* Only type-2/type-5 routes go into a VRF */
2397 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
2398 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
2401 /* if it is type-2 route and not a mac+ip route skip this route */
2402 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) &&
2403 !(IS_EVPN_PREFIX_IPADDR_V4(evp
) || IS_EVPN_PREFIX_IPADDR_V6(evp
)))
2406 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
2410 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
,
2413 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
,
2417 zlog_err("%u: Failed to %s prefix %s in VRF %s",
2419 install
? "install" : "uninstall",
2420 prefix2str(evp
, buf
, sizeof(buf
)),
2421 vrf_id_to_name(bgp_vrf
->vrf_id
));
2430 * Install or uninstall route in matching VNIs (list).
2432 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
2433 safi_t safi
, struct prefix_evpn
*evp
,
2434 struct bgp_info
*ri
,
2435 struct list
*vnis
, int install
)
2437 struct bgpevpn
*vpn
;
2438 struct listnode
*node
, *nnode
;
2440 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
2443 if (!is_vni_live(vpn
))
2447 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, ri
);
2449 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, ri
);
2452 zlog_err("%u: Failed to %s EVPN %s route in VNI %u",
2453 bgp
->vrf_id
, install
? "install" : "uninstall",
2454 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2466 * Install or uninstall route for appropriate VNIs.
2468 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
2469 struct prefix
*p
, struct bgp_info
*ri
,
2472 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
2473 struct attr
*attr
= ri
->attr
;
2474 struct ecommunity
*ecom
;
2479 /* Only type-2 and type-3 and type-5 are supported currently */
2480 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2481 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
2482 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
2485 /* If we don't have Route Target, nothing much to do. */
2486 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2489 ecom
= attr
->ecommunity
;
2490 if (!ecom
|| !ecom
->size
)
2493 /* For each extended community RT, see which VNIs/VRFs match and import
2494 * the route into matching VNIs/VRFs.
2496 for (i
= 0; i
< ecom
->size
; i
++) {
2498 u_char type
, sub_type
;
2499 struct ecommunity_val
*eval
;
2500 struct ecommunity_val eval_tmp
;
2501 struct irt_node
*irt
; /* import rt for l2vni */
2502 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
2504 /* Only deal with RTs */
2505 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2506 eval
= (struct ecommunity_val
*)(ecom
->val
2507 + (i
* ECOMMUNITY_SIZE
));
2510 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2513 /* Import route into matching l2-vnis (type-2/type-3 routes go
2516 irt
= lookup_import_rt(bgp
, eval
);
2517 if (irt
&& irt
->vnis
)
2518 install_uninstall_route_in_vnis(bgp
, afi
, safi
, evp
, ri
,
2521 /* Import route into matching l3-vnis (type-2/type-5 routes go
2522 * into l3vni/vrf table)
2524 vrf_irt
= lookup_vrf_import_rt(eval
);
2525 if (vrf_irt
&& vrf_irt
->vrfs
)
2526 install_uninstall_route_in_vrfs(bgp
, afi
, safi
, evp
, ri
,
2527 vrf_irt
->vrfs
, import
);
2529 /* Also check for non-exact match. In this,
2530 * we mask out the AS and
2531 * only check on the local-admin sub-field.
2532 * This is to facilitate using
2533 * VNI as the RT for EBGP peering too.
2537 if (type
== ECOMMUNITY_ENCODE_AS
2538 || type
== ECOMMUNITY_ENCODE_AS4
2539 || type
== ECOMMUNITY_ENCODE_IP
) {
2540 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2541 mask_ecom_global_admin(&eval_tmp
, eval
);
2542 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2543 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
2545 if (irt
&& irt
->vnis
)
2546 install_uninstall_route_in_vnis(bgp
, afi
, safi
, evp
, ri
,
2548 if (vrf_irt
&& vrf_irt
->vrfs
)
2549 install_uninstall_route_in_vrfs(bgp
, afi
, safi
, evp
,
2557 /* delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
2559 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
2561 /* delete all ipv4 routes and withdraw from peers */
2562 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
2564 /* delete all ipv6 routes and withdraw from peers */
2565 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
2568 /* update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
2570 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
2572 /* update all ipv4 routes */
2573 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
2575 /* update all ipv6 routes */
2576 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
2580 * update and advertise local routes for a VRF as type-5 routes.
2581 * This is invoked upon RD change for a VRF. Note taht the processing is only
2582 * done in the global route table using the routes which already exist in the
2585 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
2587 /* skip if the RD is configured */
2588 if (is_vrf_rd_configured(bgp_vrf
))
2591 /* derive the RD for the VRF based on new router-id */
2592 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
2594 /* update advertise ipv4|ipv6 routes as type-5 routes */
2595 update_advertise_vrf_routes(bgp_vrf
);
2599 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
2600 * This is invoked upon VRF RD change. The processing is done only from global
2603 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
2605 /* skip if the RD is configured */
2606 if (is_vrf_rd_configured(bgp_vrf
))
2609 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
2610 delete_withdraw_vrf_routes(bgp_vrf
);
2614 * Update and advertise local routes for a VNI. Invoked upon router-id
2615 * change. Note that the processing is done only on the global route table
2616 * using routes that already exist in the per-VNI table.
2618 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2620 struct prefix_evpn p
;
2621 struct bgp_node
*rn
, *global_rn
;
2622 struct bgp_info
*ri
, *global_ri
;
2624 afi_t afi
= AFI_L2VPN
;
2625 safi_t safi
= SAFI_EVPN
;
2627 /* Locate type-3 route for VNI in the per-VNI table and use its
2628 * attributes to create and advertise the type-3 route for this VNI
2629 * in the global table.
2631 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2632 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
2633 if (!rn
) /* unexpected */
2635 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2636 if (ri
->peer
== bgp
->peer_self
&& ri
->type
== ZEBRA_ROUTE_BGP
2637 && ri
->sub_type
== BGP_ROUTE_STATIC
)
2639 if (!ri
) /* unexpected */
2643 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2644 (struct prefix
*)&p
, &vpn
->prd
);
2645 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1, 0, &ri
,
2648 /* Schedule for processing and unlock node. */
2649 bgp_process(bgp
, global_rn
, afi
, safi
);
2650 bgp_unlock_node(global_rn
);
2652 /* Now, walk this VNI's route table and use the route and its attribute
2653 * to create and schedule route in global table.
2655 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2656 rn
= bgp_route_next(rn
)) {
2657 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2659 /* Identify MAC-IP local routes. */
2660 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2663 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2664 if (ri
->peer
== bgp
->peer_self
2665 && ri
->type
== ZEBRA_ROUTE_BGP
2666 && ri
->sub_type
== BGP_ROUTE_STATIC
)
2671 /* Create route in global routing table using this route entry's
2675 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2676 (struct prefix
*)evp
, &vpn
->prd
);
2678 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
2681 /* Schedule for processing and unlock node. */
2682 bgp_process(bgp
, global_rn
, afi
, safi
);
2683 bgp_unlock_node(global_rn
);
2690 * Delete (and withdraw) local routes for a VNI - only from the global
2691 * table. Invoked upon router-id change.
2693 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2696 struct prefix_evpn p
;
2697 struct bgp_node
*global_rn
;
2698 struct bgp_info
*ri
;
2699 afi_t afi
= AFI_L2VPN
;
2700 safi_t safi
= SAFI_EVPN
;
2702 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2703 * for this VNI - from the global table.
2705 ret
= delete_global_type2_routes(bgp
, vpn
);
2709 /* Remove type-3 route for this VNI from global table. */
2710 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2711 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
2712 (struct prefix
*)&p
, &vpn
->prd
);
2714 /* Delete route entry in the global EVPN table. */
2715 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
2717 /* Schedule for processing - withdraws to peers happen from
2721 bgp_process(bgp
, global_rn
, afi
, safi
);
2722 bgp_unlock_node(global_rn
);
2729 * Handle router-id change. Update and advertise local routes corresponding
2730 * to this VNI from peers. Note that this is invoked after updating the
2731 * router-id. The routes in the per-VNI table are used to create routes in
2732 * the global table and schedule them.
2734 static void update_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
2736 struct bgpevpn
*vpn
;
2738 vpn
= (struct bgpevpn
*)backet
->data
;
2741 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__
);
2745 /* Skip VNIs with configured RD. */
2746 if (is_rd_configured(vpn
))
2749 bgp_evpn_derive_auto_rd(bgp
, vpn
);
2750 update_advertise_vni_routes(bgp
, vpn
);
2754 * Handle router-id change. Delete and withdraw local routes corresponding
2755 * to this VNI from peers. Note that this is invoked prior to updating
2756 * the router-id and is done only on the global route table, the routes
2757 * are needed in the per-VNI table to re-advertise with new router id.
2759 static void withdraw_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
2761 struct bgpevpn
*vpn
;
2763 vpn
= (struct bgpevpn
*)backet
->data
;
2766 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__
);
2770 /* Skip VNIs with configured RD. */
2771 if (is_rd_configured(vpn
))
2774 delete_withdraw_vni_routes(bgp
, vpn
);
2778 * Process received EVPN type-2 route (advertise or withdraw).
2780 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
2781 struct attr
*attr
, u_char
*pfx
, int psize
,
2782 u_int32_t addpath_id
)
2784 struct prefix_rd prd
;
2785 struct prefix_evpn p
;
2788 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
2789 u_int32_t num_labels
= 0;
2792 /* Type-2 route should be either 33, 37 or 49 bytes or an
2793 * additional 3 bytes if there is a second label (VNI):
2794 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
2795 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
2796 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
2798 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
2799 && psize
!= 40 && psize
!= 52) {
2800 zlog_err("%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
2801 peer
->bgp
->vrf_id
, peer
->host
, psize
);
2805 /* Make prefix_rd */
2806 prd
.family
= AF_UNSPEC
;
2808 memcpy(&prd
.val
, pfx
, 8);
2811 /* Make EVPN prefix. */
2812 memset(&p
, 0, sizeof(struct prefix_evpn
));
2814 p
.prefixlen
= EVPN_TYPE_2_ROUTE_PREFIXLEN
;
2815 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
2817 /* Skip over Ethernet Seg Identifier for now. */
2820 /* Skip over Ethernet Tag for now. */
2823 /* Get the MAC Addr len */
2824 macaddr_len
= *pfx
++;
2826 /* Get the MAC Addr */
2827 if (macaddr_len
== (ETH_ALEN
* 8)) {
2828 memcpy(&p
.prefix
.mac
.octet
, pfx
, ETH_ALEN
);
2832 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
2833 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
2839 ipaddr_len
= *pfx
++;
2840 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
2841 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
2843 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
2844 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
2849 ipaddr_len
/= 8; /* Convert to bytes. */
2850 p
.prefix
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
2853 memcpy(&p
.prefix
.ip
.ip
.addr
, pfx
, ipaddr_len
);
2857 /* Get the VNI(s). Stored as bytes here. */
2859 memset(label
, 0, sizeof(label
));
2860 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
2861 pfx
+= BGP_LABEL_BYTES
;
2862 psize
-= (33 + ipaddr_len
);
2863 /* Do we have a second VNI? */
2866 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
2868 * If in future, we are required to access additional fields,
2869 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next field
2873 /* Process the route. */
2875 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2876 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2877 &prd
, &label
[0], num_labels
, 0, NULL
);
2879 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2880 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2881 &prd
, &label
[0], num_labels
, NULL
);
2886 * Process received EVPN type-3 route (advertise or withdraw).
2888 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
2889 struct attr
*attr
, u_char
*pfx
, int psize
,
2890 u_int32_t addpath_id
)
2892 struct prefix_rd prd
;
2893 struct prefix_evpn p
;
2897 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
2898 * IP len (1) and IP (4 or 16).
2900 if (psize
!= 17 && psize
!= 29) {
2901 zlog_err("%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
2902 peer
->bgp
->vrf_id
, peer
->host
, psize
);
2906 /* Make prefix_rd */
2907 prd
.family
= AF_UNSPEC
;
2909 memcpy(&prd
.val
, pfx
, 8);
2912 /* Make EVPN prefix. */
2913 memset(&p
, 0, sizeof(struct prefix_evpn
));
2915 p
.prefixlen
= EVPN_TYPE_3_ROUTE_PREFIXLEN
;
2916 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
2918 /* Skip over Ethernet Tag for now. */
2922 ipaddr_len
= *pfx
++;
2923 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
2924 p
.prefix
.ip
.ipa_type
= IPADDR_V4
;
2925 memcpy(&p
.prefix
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
2928 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
2929 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
2933 /* Process the route. */
2935 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2936 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2937 &prd
, NULL
, 0, 0, NULL
);
2939 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2940 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2941 &prd
, NULL
, 0, NULL
);
2946 * Process received EVPN type-5 route (advertise or withdraw).
2948 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
2949 struct attr
*attr
, u_char
*pfx
, int psize
,
2950 u_int32_t addpath_id
, int withdraw
)
2952 struct prefix_rd prd
;
2953 struct prefix_evpn p
;
2954 struct bgp_route_evpn evpn
;
2957 mpls_label_t label
; /* holds the VNI as in the packet */
2960 /* Type-5 route should be 34 or 58 bytes:
2961 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
2962 * GW (4 or 16) and VNI (3).
2963 * Note that the IP and GW should both be IPv4 or both IPv6.
2965 if (psize
!= 34 && psize
!= 58) {
2966 zlog_err("%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
2967 peer
->bgp
->vrf_id
, peer
->host
, psize
);
2971 /* Make prefix_rd */
2972 prd
.family
= AF_UNSPEC
;
2974 memcpy(&prd
.val
, pfx
, 8);
2977 /* Make EVPN prefix. */
2978 memset(&p
, 0, sizeof(struct prefix_evpn
));
2980 p
.prefixlen
= EVPN_TYPE_5_ROUTE_PREFIXLEN
;
2981 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
2983 /* Additional information outside of prefix - ESI and GW IP */
2984 memset(&evpn
, 0, sizeof(evpn
));
2987 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
2990 /* Fetch Ethernet Tag. */
2991 memcpy(ð_tag
, pfx
, 4);
2992 p
.prefix
.eth_tag
= ntohl(eth_tag
);
2995 /* Fetch IP prefix length. */
2997 if (ippfx_len
> IPV6_MAX_BITLEN
) {
2999 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3000 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
3003 p
.prefix
.ip_prefix_length
= ippfx_len
;
3005 /* Determine IPv4 or IPv6 prefix */
3006 /* Since the address and GW are from the same family, this just becomes
3007 * a simple check on the total size.
3010 SET_IPADDR_V4(&p
.prefix
.ip
);
3011 memcpy(&p
.prefix
.ip
.ipaddr_v4
, pfx
, 4);
3013 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
3016 SET_IPADDR_V6(&p
.prefix
.ip
);
3017 memcpy(&p
.prefix
.ip
.ipaddr_v6
, pfx
, 16);
3019 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
3023 /* Get the VNI (in MPLS label field). Stored as bytes here. */
3024 memset(&label
, 0, sizeof(label
));
3025 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
3028 * If in future, we are required to access additional fields,
3029 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next field
3032 /* Process the route. */
3034 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3035 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3036 &prd
, &label
, 1, 0, &evpn
);
3038 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3039 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3040 &prd
, &label
, 1, &evpn
);
3045 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
3046 struct prefix_rd
*prd
,
3047 mpls_label_t
*label
, u_int32_t num_labels
,
3052 struct evpn_addr
*p_evpn_p
;
3054 memset(&temp
, 0, 16);
3055 if (p
->family
!= AF_EVPN
)
3057 p_evpn_p
= &(p
->u
.prefix_evpn
);
3059 /* len denites the total len of IP and GW-IP in the route
3060 IP and GW-IP have to be both ipv4 or ipv6
3062 if (IS_IPADDR_V4(&p_evpn_p
->ip
))
3063 len
= 8; /* IP and GWIP are both ipv4 */
3065 len
= 32; /* IP and GWIP are both ipv6 */
3066 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
3067 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
3068 stream_put(s
, prd
->val
, 8);
3070 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
3072 stream_put(s
, &temp
, 10);
3073 stream_putl(s
, p_evpn_p
->eth_tag
);
3074 stream_putc(s
, p_evpn_p
->ip_prefix_length
);
3075 if (IS_IPADDR_V4(&p_evpn_p
->ip
))
3076 stream_put_ipv4(s
, p_evpn_p
->ip
.ipaddr_v4
.s_addr
);
3078 stream_put(s
, &p_evpn_p
->ip
.ipaddr_v6
, 16);
3080 if (IS_IPADDR_V4(&p_evpn_p
->ip
))
3082 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
3084 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
3086 if (IS_IPADDR_V4(&p_evpn_p
->ip
))
3087 stream_put_ipv4(s
, 0);
3089 stream_put(s
, &temp
, 16);
3093 stream_put(s
, label
, 3);
3099 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
3101 static void cleanup_vni_on_disable(struct hash_backet
*backet
, struct bgp
*bgp
)
3103 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3105 /* Remove EVPN routes and schedule for processing. */
3106 delete_routes_for_vni(bgp
, vpn
);
3108 /* Clear "live" flag and see if hash needs to be freed. */
3109 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
3110 if (!is_vni_configured(vpn
))
3111 bgp_evpn_free(bgp
, vpn
);
3115 * Free a VNI entry; iterator function called during cleanup.
3117 static void free_vni_entry(struct hash_backet
*backet
, struct bgp
*bgp
)
3119 struct bgpevpn
*vpn
;
3121 vpn
= (struct bgpevpn
*)backet
->data
;
3122 delete_all_vni_routes(bgp
, vpn
);
3123 bgp_evpn_free(bgp
, vpn
);
3127 * Derive AUTO import RT for BGP VRF - L3VNI
3129 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
3131 struct bgp
*bgp_def
= NULL
;
3133 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
3134 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
3137 bgp_def
= bgp_get_default();
3140 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
3144 * Delete AUTO import RT from BGP VRF - L3VNI
3146 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
3148 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
3152 * Derive AUTO export RT for BGP VRF - L3VNI
3154 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
3156 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
3157 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
3161 * Delete AUTO export RT from BGP VRF - L3VNI
3163 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
3165 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
3168 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
3170 struct bgp
*bgp_def
= NULL
;
3171 struct listnode
*node
= NULL
;
3172 struct bgpevpn
*vpn
= NULL
;
3174 bgp_def
= bgp_get_default();
3178 /* update all type-5 routes */
3179 update_advertise_vrf_routes(bgp_vrf
);
3181 /* update all type-2 routes */
3182 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
3183 update_routes_for_vni(bgp_def
, vpn
);
3190 /* withdraw type-5 route corresponding to ip prefix */
3191 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
3192 afi_t afi
, safi_t safi
)
3195 struct prefix_evpn evp
;
3196 char buf
[PREFIX_STRLEN
];
3198 /* NOTE: Check needed as this is called per-route also. */
3199 if (!advertise_type5_routes(bgp_vrf
, afi
))
3202 build_type5_prefix_from_ip_prefix(&evp
, p
);
3203 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
3206 "%u failed to delete type-5 route for prefix %s in vrf %s",
3208 prefix2str(p
, buf
, sizeof(buf
)),
3209 vrf_id_to_name(bgp_vrf
->vrf_id
));
3213 /* withdraw all type-5 routes for an address family */
3214 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
,
3215 afi_t afi
, safi_t safi
)
3217 struct bgp_table
*table
= NULL
;
3218 struct bgp_node
*rn
= NULL
;
3220 /* Bail out early if we don't have to advertise type-5 routes. */
3221 if (!advertise_type5_routes(bgp_vrf
, afi
))
3224 table
= bgp_vrf
->rib
[afi
][safi
];
3225 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
))
3226 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
, afi
, safi
);
3231 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
3232 * to this function correspond to those of the source IP prefix (best
3233 * path in the case of the attr. In the case of a local prefix (when we
3234 * are advertising local subnets), the src_attr will be NULL.
3236 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
3237 struct attr
*src_attr
,
3238 afi_t afi
, safi_t safi
)
3241 struct prefix_evpn evp
;
3242 char buf
[PREFIX_STRLEN
];
3244 /* NOTE: Check needed as this is called per-route also. */
3245 if (!advertise_type5_routes(bgp_vrf
, afi
))
3248 /* only advertise subnet routes as type-5 */
3249 if (is_host_route(p
))
3252 build_type5_prefix_from_ip_prefix(&evp
, p
);
3253 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
3256 "%u: Failed to create type-5 route for prefix %s",
3258 prefix2str(p
, buf
, sizeof(buf
)));
3261 /* Inject all prefixes of a particular address-family (currently, IPv4 or
3262 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
3263 * advertisement is enabled.
3265 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
,
3266 afi_t afi
, safi_t safi
)
3268 struct bgp_table
*table
= NULL
;
3269 struct bgp_node
*rn
= NULL
;
3270 struct bgp_info
*ri
;
3272 /* Bail out early if we don't have to advertise type-5 routes. */
3273 if (!advertise_type5_routes(bgp_vrf
, afi
))
3276 table
= bgp_vrf
->rib
[afi
][safi
];
3277 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3278 /* Need to identify the "selected" route entry to use its
3280 * TODO: Support for AddPath for EVPN.
3282 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
3283 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
)) {
3285 /* apply the route-map */
3286 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
3291 bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
,
3292 &rn
->p
, RMAP_BGP
, ri
);
3293 if (ret
== RMAP_DENYMATCH
)
3297 bgp_evpn_advertise_type5_route(bgp_vrf
, &rn
->p
,
3306 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
,
3309 struct listnode
*node
, *nnode
, *node_to_del
;
3310 struct ecommunity
*ecom
, *ecom_auto
;
3311 struct ecommunity_val eval
;
3313 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
3315 ecom_auto
= ecommunity_new();
3316 ecommunity_add_val(ecom_auto
, &eval
);
3319 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
3320 if (ecommunity_match(ecom
, ecom_auto
)) {
3321 ecommunity_free(&ecom
);
3327 list_delete_node(rtl
, node_to_del
);
3329 ecommunity_free(&ecom_auto
);
3332 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
3333 struct ecommunity
*ecomadd
)
3335 /* uninstall routes from vrf */
3336 uninstall_routes_for_vrf(bgp_vrf
);
3338 /* Cleanup the RT to VRF mapping */
3339 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
3341 /* Remove auto generated RT */
3342 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
3344 /* Add the newly configured RT to RT list */
3345 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
3346 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
3348 /* map VRF to its RTs */
3349 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
3351 /* install routes matching the new VRF */
3352 install_routes_for_vrf(bgp_vrf
);
3355 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
3356 struct ecommunity
*ecomdel
)
3358 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
3359 struct ecommunity
*ecom
= NULL
;
3361 /* uninstall routes from vrf */
3362 uninstall_routes_for_vrf(bgp_vrf
);
3364 /* Cleanup the RT to VRF mapping */
3365 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
3367 /* remove the RT from the RT list */
3368 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
3369 if (ecommunity_match(ecom
, ecomdel
)) {
3370 ecommunity_free(&ecom
);
3377 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
3379 /* fallback to auto import rt, if this was the last RT */
3380 if (list_isempty(bgp_vrf
->vrf_import_rtl
)) {
3381 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
3382 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
3385 /* map VRFs to its RTs */
3386 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
3388 /* install routes matching this new RT */
3389 install_routes_for_vrf(bgp_vrf
);
3392 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
3393 struct ecommunity
*ecomadd
)
3395 /* remove auto-generated RT */
3396 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
3398 /* Add the new RT to the RT list */
3399 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
3400 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
3402 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
3406 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
3407 struct ecommunity
*ecomdel
)
3409 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
3410 struct ecommunity
*ecom
= NULL
;
3412 /* Remove the RT from the RT list */
3413 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
3414 if (ecommunity_match(ecom
, ecomdel
)) {
3415 ecommunity_free(&ecom
);
3422 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
3424 /* fall back to auto-generated RT if this was the last RT */
3425 if (bgp_vrf
->vrf_export_rtl
&& list_isempty(bgp_vrf
->vrf_export_rtl
)) {
3426 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
3427 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
3430 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
3434 * Handle change to BGP router id. This is invoked twice by the change
3435 * handler, first before the router id has been changed and then after
3436 * the router id has been changed. The first invocation will result in
3437 * local routes for all VNIs/VRF being deleted and withdrawn and the next
3438 * will result in the routes being re-advertised.
3440 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
3444 /* delete and withdraw all the type-5 routes
3445 stored in the global table for this vrf
3447 withdraw_router_id_vrf(bgp
);
3449 /* delete all the VNI routes (type-2/type-3) routes for all the
3452 hash_iterate(bgp
->vnihash
,
3453 (void (*)(struct hash_backet
*,
3454 void *))withdraw_router_id_vni
,
3458 /* advertise all routes in the vrf as type-5 routes with the new
3461 update_router_id_vrf(bgp
);
3463 /* advertise all the VNI routes (type-2/type-3) routes with the
3466 hash_iterate(bgp
->vnihash
,
3467 (void (*)(struct hash_backet
*,
3468 void *))update_router_id_vni
,
3474 * Handle change to export RT - update and advertise local routes.
3476 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3478 return update_routes_for_vni(bgp
, vpn
);
3481 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
,
3485 delete_withdraw_vrf_routes(bgp_vrf
);
3487 update_advertise_vrf_routes(bgp_vrf
);
3491 * Handle change to RD. This is invoked twice by the change handler,
3492 * first before the RD has been changed and then after the RD has
3493 * been changed. The first invocation will result in local routes
3494 * of this VNI being deleted and withdrawn and the next will result
3495 * in the routes being re-advertised.
3497 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
3501 delete_withdraw_vni_routes(bgp
, vpn
);
3503 update_advertise_vni_routes(bgp
, vpn
);
3507 * Install routes for this VNI. Invoked upon change to Import RT.
3509 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3511 return install_routes_for_vni(bgp
, vpn
);
3515 * Uninstall all routes installed for this VNI. Invoked upon change
3518 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3520 return uninstall_routes_for_vni(bgp
, vpn
);
3524 * TODO: Hardcoded for a maximum of 2 VNIs right now
3526 char *bgp_evpn_label2str(mpls_label_t
*label
, u_int32_t num_labels
,
3531 vni1
= label2vni(label
);
3532 if (num_labels
== 2) {
3533 vni2
= label2vni(label
+1);
3534 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
3536 snprintf(buf
, len
, "%u", vni1
);
3541 * Function to convert evpn route to json format.
3542 * NOTE: We don't use prefix2str as the output here is a bit different.
3544 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
3546 char buf1
[ETHER_ADDR_STRLEN
];
3547 char buf2
[PREFIX2STR_BUFFER
];
3552 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
3553 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
3554 json_object_int_add(json
, "ethTag", 0);
3555 json_object_int_add(json
, "ipLen",
3556 IS_EVPN_PREFIX_IPADDR_V4(p
)
3559 json_object_string_add(json
, "ip",
3560 inet_ntoa(p
->prefix
.ip
.ipaddr_v4
));
3561 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3562 if (IS_EVPN_PREFIX_IPADDR_NONE(p
)) {
3563 json_object_int_add(json
, "routeType",
3564 p
->prefix
.route_type
);
3565 json_object_int_add(
3567 0); /* TODO: we don't support esi yet */
3568 json_object_int_add(json
, "ethTag", 0);
3569 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
3570 json_object_string_add(json
, "mac",
3571 prefix_mac2str(&p
->prefix
.mac
,
3577 family
= IS_EVPN_PREFIX_IPADDR_V4(p
) ? AF_INET
3580 json_object_int_add(json
, "routeType",
3581 p
->prefix
.route_type
);
3582 json_object_int_add(
3584 0); /* TODO: we don't support esi yet */
3585 json_object_int_add(json
, "ethTag", 0);
3586 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
3587 json_object_string_add(json
, "mac",
3588 prefix_mac2str(&p
->prefix
.mac
,
3591 json_object_int_add(json
, "ipLen",
3592 IS_EVPN_PREFIX_IPADDR_V4(p
)
3595 json_object_string_add(
3597 inet_ntop(family
, &p
->prefix
.ip
.ip
.addr
, buf2
,
3598 PREFIX2STR_BUFFER
));
3601 /* Currently, this is to cater to other AF_ETHERNET code. */
3606 * Function to convert evpn route to string.
3607 * NOTE: We don't use prefix2str as the output here is a bit different.
3609 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
3611 char buf1
[ETHER_ADDR_STRLEN
];
3612 char buf2
[PREFIX2STR_BUFFER
];
3614 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
3615 snprintf(buf
, len
, "[%d]:[0]:[%d]:[%s]", p
->prefix
.route_type
,
3616 IS_EVPN_PREFIX_IPADDR_V4(p
) ? IPV4_MAX_BITLEN
3618 inet_ntoa(p
->prefix
.ip
.ipaddr_v4
));
3619 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3620 if (IS_EVPN_PREFIX_IPADDR_NONE(p
))
3621 snprintf(buf
, len
, "[%d]:[0]:[0]:[%d]:[%s]",
3622 p
->prefix
.route_type
, 8 * ETH_ALEN
,
3623 prefix_mac2str(&p
->prefix
.mac
, buf1
,
3628 family
= IS_EVPN_PREFIX_IPADDR_V4(p
) ? AF_INET
3630 snprintf(buf
, len
, "[%d]:[0]:[0]:[%d]:[%s]:[%d]:[%s]",
3631 p
->prefix
.route_type
, 8 * ETH_ALEN
,
3632 prefix_mac2str(&p
->prefix
.mac
, buf1
,
3634 family
== AF_INET
? IPV4_MAX_BITLEN
3636 inet_ntop(family
, &p
->prefix
.ip
.ip
.addr
, buf2
,
3637 PREFIX2STR_BUFFER
));
3639 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3640 snprintf(buf
, len
, "[%d]:[0]:[0]:[%d]:[%s]",
3641 p
->prefix
.route_type
,
3642 p
->prefix
.ip_prefix_length
,
3643 IS_EVPN_PREFIX_IPADDR_V4(p
) ?
3644 inet_ntoa(p
->prefix
.ip
.ipaddr_v4
) :
3645 inet6_ntoa(p
->prefix
.ip
.ipaddr_v6
));
3647 /* For EVPN route types not supported yet. */
3648 snprintf(buf
, len
, "(unsupported route type %d)",
3649 p
->prefix
.route_type
);
3656 * Encode EVPN prefix in Update (MP_REACH)
3658 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
3659 struct prefix_rd
*prd
,
3660 mpls_label_t
*label
, u_int32_t num_labels
,
3661 struct attr
*attr
, int addpath_encode
,
3662 u_int32_t addpath_tx_id
)
3664 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3665 int len
, ipa_len
= 0;
3668 stream_putl(s
, addpath_tx_id
);
3671 stream_putc(s
, evp
->prefix
.route_type
);
3673 switch (evp
->prefix
.route_type
) {
3674 case BGP_EVPN_MAC_IP_ROUTE
:
3675 if (IS_EVPN_PREFIX_IPADDR_V4(evp
))
3676 ipa_len
= IPV4_MAX_BYTELEN
;
3677 else if (IS_EVPN_PREFIX_IPADDR_V6(evp
))
3678 ipa_len
= IPV6_MAX_BYTELEN
;
3679 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
3680 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
3681 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
3683 stream_putc(s
, len
);
3684 stream_put(s
, prd
->val
, 8); /* RD */
3685 stream_put(s
, 0, 10); /* ESI */
3686 stream_putl(s
, 0); /* Ethernet Tag ID */
3687 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
3688 stream_put(s
, evp
->prefix
.mac
.octet
, 6); /* Mac Addr */
3689 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
3690 if (ipa_len
) /* IP */
3691 stream_put(s
, &evp
->prefix
.ip
.ip
.addr
, ipa_len
);
3692 /* 1st label is the L2 VNI */
3693 stream_put(s
, label
, BGP_LABEL_BYTES
);
3694 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
3695 if (ipa_len
&& num_labels
> 1)
3696 stream_put(s
, label
+1, BGP_LABEL_BYTES
);
3699 case BGP_EVPN_IMET_ROUTE
:
3700 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
3701 stream_put(s
, prd
->val
, 8); /* RD */
3702 stream_putl(s
, 0); /* Ethernet Tag ID */
3703 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
3704 /* Originating Router's IP Addr */
3705 stream_put_in_addr(s
, &evp
->prefix
.ip
.ipaddr_v4
);
3708 case BGP_EVPN_IP_PREFIX_ROUTE
:
3709 /* TODO: AddPath support. */
3710 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
3718 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
3719 struct bgp_nlri
*packet
, int withdraw
)
3725 u_int32_t addpath_id
;
3726 int addpath_encoded
;
3732 /* Check peer status. */
3733 if (peer
->status
!= Established
) {
3734 zlog_err("%u:%s - EVPN update received in state %d",
3735 peer
->bgp
->vrf_id
, peer
->host
, peer
->status
);
3739 /* Start processing the NLRI - there may be multiple in the MP_REACH */
3741 lim
= pnt
+ packet
->length
;
3743 safi
= packet
->safi
;
3747 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
3748 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
3749 PEER_CAP_ADDPATH_AF_TX_RCV
));
3751 for (; pnt
< lim
; pnt
+= psize
) {
3752 /* Clear prefix structure. */
3753 memset(&p
, 0, sizeof(struct prefix
));
3755 /* Deal with path-id if AddPath is supported. */
3756 if (addpath_encoded
) {
3757 /* When packet overflow occurs return immediately. */
3758 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
3761 addpath_id
= ntohl(*((uint32_t *)pnt
));
3762 pnt
+= BGP_ADDPATH_ID_LEN
;
3765 /* All EVPN NLRI types start with type and length. */
3770 psize
= rlen
= *pnt
++;
3772 /* When packet overflow occur return immediately. */
3773 if (pnt
+ psize
> lim
)
3777 case BGP_EVPN_MAC_IP_ROUTE
:
3778 if (process_type2_route(peer
, afi
, safi
,
3779 withdraw
? NULL
: attr
, pnt
,
3780 psize
, addpath_id
)) {
3782 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
3783 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3788 case BGP_EVPN_IMET_ROUTE
:
3789 if (process_type3_route(peer
, afi
, safi
,
3790 withdraw
? NULL
: attr
, pnt
,
3791 psize
, addpath_id
)) {
3793 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
3794 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3799 case BGP_EVPN_IP_PREFIX_ROUTE
:
3800 if (process_type5_route(peer
, afi
, safi
, attr
, pnt
,
3801 psize
, addpath_id
, withdraw
)) {
3803 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
3804 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3814 /* Packet length consistency check. */
3822 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
3823 * The mapping will be used during route processing.
3824 * bgp_def: default bgp instance
3825 * bgp_vrf: specific bgp vrf instance on which RT is configured
3827 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
3830 struct ecommunity_val
*eval
= NULL
;
3831 struct listnode
*node
= NULL
, *nnode
= NULL
;
3832 struct ecommunity
*ecom
= NULL
;
3834 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
3835 for (i
= 0; i
< ecom
->size
; i
++) {
3836 eval
= (struct ecommunity_val
*)(ecom
->val
3838 * ECOMMUNITY_SIZE
));
3839 map_vrf_to_rt(bgp_vrf
, eval
);
3845 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
3847 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
3850 struct ecommunity_val
*eval
;
3851 struct listnode
*node
, *nnode
;
3852 struct ecommunity
*ecom
;
3854 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
3855 for (i
= 0; i
< ecom
->size
; i
++) {
3856 struct vrf_irt_node
*irt
;
3857 struct ecommunity_val eval_tmp
;
3859 eval
= (struct ecommunity_val
*)(ecom
->val
3861 * ECOMMUNITY_SIZE
));
3862 /* If using "automatic" RT, we only care about the
3863 * local-admin sub-field.
3864 * This is to facilitate using VNI as the RT for EBGP
3867 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3868 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
3869 BGP_VRF_IMPORT_RT_CFGD
))
3870 mask_ecom_global_admin(&eval_tmp
, eval
);
3872 irt
= lookup_vrf_import_rt(&eval_tmp
);
3874 unmap_vrf_from_rt(bgp_vrf
, irt
);
3882 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
3883 * The mapping will be used during route processing.
3885 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3888 struct ecommunity_val
*eval
;
3889 struct listnode
*node
, *nnode
;
3890 struct ecommunity
*ecom
;
3892 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
3893 for (i
= 0; i
< ecom
->size
; i
++) {
3894 eval
= (struct ecommunity_val
*)(ecom
->val
3896 * ECOMMUNITY_SIZE
));
3897 map_vni_to_rt(bgp
, vpn
, eval
);
3903 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
3905 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3908 struct ecommunity_val
*eval
;
3909 struct listnode
*node
, *nnode
;
3910 struct ecommunity
*ecom
;
3912 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
3913 for (i
= 0; i
< ecom
->size
; i
++) {
3914 struct irt_node
*irt
;
3915 struct ecommunity_val eval_tmp
;
3917 eval
= (struct ecommunity_val
*)(ecom
->val
3919 * ECOMMUNITY_SIZE
));
3920 /* If using "automatic" RT, we only care about the
3921 * local-admin sub-field.
3922 * This is to facilitate using VNI as the RT for EBGP
3925 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3926 if (!is_import_rt_configured(vpn
))
3927 mask_ecom_global_admin(&eval_tmp
, eval
);
3929 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3931 unmap_vni_from_rt(bgp
, vpn
, irt
);
3937 * Derive Import RT automatically for VNI and map VNI to RT.
3938 * The mapping will be used during route processing.
3940 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3942 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
3943 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
3946 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
3950 * Derive Export RT automatically for VNI.
3952 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3954 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
3955 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
3959 * Derive RD automatically for VNI using passed information - it
3960 * is of the form RouterId:unique-id-for-vni.
3962 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
3966 bgp
->vrf_prd
.family
= AF_UNSPEC
;
3967 bgp
->vrf_prd
.prefixlen
= 64;
3968 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), bgp
->vrf_rd_id
);
3969 str2prefix_rd(buf
, &bgp
->vrf_prd
);
3973 * Derive RD automatically for VNI using passed information - it
3974 * is of the form RouterId:unique-id-for-vni.
3976 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3980 vpn
->prd
.family
= AF_UNSPEC
;
3981 vpn
->prd
.prefixlen
= 64;
3982 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
3983 (void)str2prefix_rd(buf
, &vpn
->prd
);
3984 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
3990 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
3992 struct bgpevpn
*vpn
;
3995 memset(&tmp
, 0, sizeof(struct bgpevpn
));
3997 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
4002 * Create a new vpn - invoked upon configuration or zebra notification.
4004 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
4005 struct in_addr originator_ip
,
4006 vrf_id_t tenant_vrf_id
)
4008 struct bgpevpn
*vpn
;
4013 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
4017 /* Set values - RD and RT set to defaults. */
4019 vpn
->originator_ip
= originator_ip
;
4020 vpn
->tenant_vrf_id
= tenant_vrf_id
;
4022 /* Initialize route-target import and export lists */
4023 vpn
->import_rtl
= list_new();
4024 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4025 vpn
->export_rtl
= list_new();
4026 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4027 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
4028 derive_rd_rt_for_vni(bgp
, vpn
);
4030 /* Initialize EVPN route table. */
4031 vpn
->route_table
= bgp_table_init(AFI_L2VPN
, SAFI_EVPN
);
4034 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
4035 XFREE(MTYPE_BGP_EVPN
, vpn
);
4039 /* add to l2vni list on corresponding vrf */
4040 bgpevpn_link_to_l3vni(vpn
);
4042 QOBJ_REG(vpn
, bgpevpn
);
4047 * Free a given VPN - called in multiple scenarios such as zebra
4048 * notification, configuration being deleted, advertise-all-vni disabled etc.
4049 * This just frees appropriate memory, caller should have taken other
4052 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4054 bgpevpn_unlink_from_l3vni(vpn
);
4055 bgp_table_unlock(vpn
->route_table
);
4056 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4057 list_delete_and_null(&vpn
->import_rtl
);
4058 list_delete_and_null(&vpn
->export_rtl
);
4059 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
4060 hash_release(bgp
->vnihash
, vpn
);
4062 XFREE(MTYPE_BGP_EVPN
, vpn
);
4066 * Import route into matching VNI(s).
4068 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
4069 struct prefix
*p
, struct bgp_info
*ri
)
4071 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 1);
4075 * Unimport route from matching VNI(s).
4077 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
4078 struct prefix
*p
, struct bgp_info
*ri
)
4080 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 0);
4083 /* filter routes which have martian next hops */
4084 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
4088 struct bgp_node
*rd_rn
, *rn
;
4089 struct bgp_table
*table
;
4090 struct bgp_info
*ri
;
4095 /* Walk entire global routing table and evaluate routes which could be
4096 * imported into this VPN. Note that we cannot just look at the routes
4097 * for the VNI's RD -
4098 * remote routes applicable for this VNI could have any RD.
4100 /* EVPN routes are a 2-level table. */
4101 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
4102 rd_rn
= bgp_route_next(rd_rn
)) {
4103 table
= (struct bgp_table
*)(rd_rn
->info
);
4107 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4109 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
4111 /* Consider "valid" remote routes applicable for
4113 if (!(ri
->type
== ZEBRA_ROUTE_BGP
4114 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
4117 if (bgp_nexthop_self(bgp
, ri
->attr
->nexthop
)) {
4119 char attr_str
[BUFSIZ
];
4120 char pbuf
[PREFIX_STRLEN
];
4122 bgp_dump_attr(ri
->attr
, attr_str
,
4125 if (bgp_debug_update(ri
->peer
, &rn
->p
,
4128 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
4135 bgp_evpn_unimport_route(bgp
, afi
, safi
,
4138 bgp_rib_remove(rn
, ri
, ri
->peer
, afi
,
4149 * Handle del of a local MACIP.
4151 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
4154 struct bgpevpn
*vpn
;
4155 struct prefix_evpn p
;
4157 if (!bgp
->vnihash
) {
4158 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4162 /* Lookup VNI hash - should exist. */
4163 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4164 if (!vpn
|| !is_vni_live(vpn
)) {
4165 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP DEL",
4166 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
4170 /* Remove EVPN type-2 route and schedule for processing. */
4171 build_evpn_type2_prefix(&p
, mac
, ip
);
4172 delete_evpn_route(bgp
, vpn
, &p
);
4178 * Handle add of a local MACIP.
4180 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
4181 struct ipaddr
*ip
, u_char flags
)
4183 struct bgpevpn
*vpn
;
4184 struct prefix_evpn p
;
4186 if (!bgp
->vnihash
) {
4187 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4191 /* Lookup VNI hash - should exist. */
4192 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4193 if (!vpn
|| !is_vni_live(vpn
)) {
4194 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP ADD",
4195 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
4199 /* Create EVPN type-2 route and schedule for processing. */
4200 build_evpn_type2_prefix(&p
, mac
, ip
);
4201 if (update_evpn_route(bgp
, vpn
, &p
, flags
)) {
4202 char buf
[ETHER_ADDR_STRLEN
];
4203 char buf2
[INET6_ADDRSTRLEN
];
4206 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
4207 bgp
->vrf_id
, vpn
->vni
,
4208 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? "sticky gateway"
4210 prefix_mac2str(mac
, buf
, sizeof(buf
)),
4211 ipaddr2str(ip
, buf2
, sizeof(buf2
)),
4219 static void link_l2vni_hash_to_l3vni(struct hash_backet
*backet
,
4220 struct bgp
*bgp_vrf
)
4222 struct bgpevpn
*vpn
= NULL
;
4223 struct bgp
*bgp_def
= NULL
;
4225 bgp_def
= bgp_get_default();
4228 vpn
= (struct bgpevpn
*)backet
->data
;
4229 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
4230 bgpevpn_link_to_l3vni(vpn
);
4233 int bgp_evpn_local_l3vni_add(vni_t l3vni
,
4235 struct ethaddr
*rmac
,
4236 struct in_addr originator_ip
)
4238 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
4239 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
4240 struct listnode
*node
= NULL
;
4241 struct bgpevpn
*vpn
= NULL
;
4244 /* get the default instamce - required to get the AS number for VRF
4247 bgp_def
= bgp_get_default();
4249 zlog_err("Cannot process L3VNI %u ADD - default BGP instance not yet created",
4255 /* if the BGP vrf instance doesnt exist - create one */
4256 bgp_vrf
= bgp_lookup_by_name(vrf_id_to_name(vrf_id
));
4261 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
4262 BGP_INSTANCE_TYPE_VRF
);
4264 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
:
4265 zlog_err("'bgp multiple-instance' not present\n");
4267 case BGP_ERR_AS_MISMATCH
:
4268 zlog_err("BGP is already running; AS is %u\n", as
);
4270 case BGP_ERR_INSTANCE_MISMATCH
:
4271 zlog_err("BGP instance name and AS number mismatch\n");
4275 /* mark as auto created */
4276 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
4279 /* associate with l3vni */
4280 bgp_vrf
->l3vni
= l3vni
;
4282 /* set the router mac - to be used in mac-ip routes for this vrf */
4283 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
4285 /* set the originator ip */
4286 bgp_vrf
->originator_ip
= originator_ip
;
4288 /* auto derive RD/RT */
4289 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
4290 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4291 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
4292 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4293 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
4295 /* link all corresponding l2vnis */
4296 hash_iterate(bgp_def
->vnihash
,
4297 (void (*)(struct hash_backet
*, void *))
4298 link_l2vni_hash_to_l3vni
,
4301 /* updates all corresponding local mac-ip routes */
4302 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4303 update_routes_for_vni(bgp_def
, vpn
);
4305 /* advertise type-5 routes if needed */
4306 update_advertise_vrf_routes(bgp_vrf
);
4308 /* install all remote routes belonging to this l3vni into correspondng
4310 install_routes_for_vrf(bgp_vrf
);
4315 int bgp_evpn_local_l3vni_del(vni_t l3vni
,
4318 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
4319 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
4320 struct listnode
*node
= NULL
;
4321 struct bgpevpn
*vpn
= NULL
;
4323 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
4325 zlog_err("Cannot process L3VNI %u Del - Could not find BGP instance",
4330 bgp_def
= bgp_get_default();
4332 zlog_err("Cannot process L3VNI %u Del - Could not find default BGP instance",
4337 /* unimport remote routes from VRF, if it is AUTO vrf bgp_delete will
4338 * take care of uninstalling the routes from zebra
4340 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
4341 uninstall_routes_for_vrf(bgp_vrf
);
4343 /* delete/withdraw all type-5 routes */
4344 delete_withdraw_vrf_routes(bgp_vrf
);
4346 /* remove the l3vni from vrf instance */
4349 /* remove the Rmac from the BGP vrf */
4350 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
4353 if (bgp_vrf
->vrf_import_rtl
&& !list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4354 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4355 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
4357 if (bgp_vrf
->vrf_export_rtl
&& !list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4358 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
4361 /* update all corresponding local mac-ip routes */
4362 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4363 update_routes_for_vni(bgp_def
, vpn
);
4366 /* Delete the instance if it was autocreated */
4367 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
4368 bgp_delete(bgp_vrf
);
4374 * Handle del of a local VNI.
4376 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
4378 struct bgpevpn
*vpn
;
4380 if (!bgp
->vnihash
) {
4381 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4385 /* Locate VNI hash */
4386 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4388 zlog_warn("%u: VNI hash entry for VNI %u not found at DEL",
4393 /* Remove all local EVPN routes and schedule for processing (to
4394 * withdraw from peers).
4396 delete_routes_for_vni(bgp
, vpn
);
4399 * tunnel is no longer active, del tunnel ip address from tip_hash
4401 bgp_tip_del(bgp
, &vpn
->originator_ip
);
4403 /* Clear "live" flag and see if hash needs to be freed. */
4404 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4405 if (!is_vni_configured(vpn
))
4406 bgp_evpn_free(bgp
, vpn
);
4412 * Handle add (or update) of a local VNI. The only VNI change we care
4413 * about is change to local-tunnel-ip.
4415 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
4416 struct in_addr originator_ip
,
4417 vrf_id_t tenant_vrf_id
)
4419 struct bgpevpn
*vpn
;
4420 struct prefix_evpn p
;
4422 if (!bgp
->vnihash
) {
4423 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4427 /* Lookup VNI. If present and no change, exit. */
4428 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4431 /* update tenant_vrf_id if required */
4432 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
4433 bgpevpn_unlink_from_l3vni(vpn
);
4434 vpn
->tenant_vrf_id
= tenant_vrf_id
;
4435 bgpevpn_link_to_l3vni(vpn
);
4437 /* update all routes with new export RT for VRFs */
4438 update_routes_for_vni(bgp
, vpn
);
4441 if (is_vni_live(vpn
)
4442 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
4443 /* Probably some other param has changed that we don't
4447 /* Local tunnel endpoint IP address has changed */
4448 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
4451 /* Create or update as appropriate. */
4453 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
);
4456 "%u: Failed to allocate VNI entry for VNI %u - at Add",
4462 /* if the VNI is live already, there is nothing more to do */
4463 if (is_vni_live(vpn
))
4466 /* Mark as "live" */
4467 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4469 /* tunnel is now active, add tunnel-ip to db */
4470 bgp_tip_add(bgp
, &originator_ip
);
4472 /* filter routes as nexthop database has changed */
4473 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
4475 /* Create EVPN type-3 route and schedule for processing. */
4476 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
4477 if (update_evpn_route(bgp
, vpn
, &p
, 0)) {
4478 zlog_err("%u: Type3 route creation failure for VNI %u",
4483 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
4487 install_routes_for_vni(bgp
, vpn
);
4489 /* If we are advertising gateway mac-ip
4490 It needs to be conveyed again to zebra */
4491 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
4497 * Cleanup EVPN information on disable - Need to delete and withdraw
4498 * EVPN routes from peers.
4500 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
4502 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
4503 void *))cleanup_vni_on_disable
,
4508 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
4509 * BGP instance (default) is being freed.
4511 void bgp_evpn_cleanup(struct bgp
*bgp
)
4514 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
4515 void *))free_vni_entry
,
4517 if (bgp
->import_rt_hash
)
4518 hash_free(bgp
->import_rt_hash
);
4519 bgp
->import_rt_hash
= NULL
;
4520 if (bgp
->vrf_import_rt_hash
)
4521 hash_free(bgp
->vrf_import_rt_hash
);
4522 bgp
->vrf_import_rt_hash
= NULL
;
4524 hash_free(bgp
->vnihash
);
4525 bgp
->vnihash
= NULL
;
4526 if (bgp
->vrf_import_rtl
)
4527 list_delete_and_null(&bgp
->vrf_import_rtl
);
4528 if (bgp
->vrf_export_rtl
)
4529 list_delete_and_null(&bgp
->vrf_export_rtl
);
4531 list_delete_and_null(&bgp
->l2vnis
);
4532 bf_release_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
4536 * Initialization for EVPN
4539 * hash for RT to VNI
4540 * assign a unique rd id for auto derivation of vrf_prd
4542 void bgp_evpn_init(struct bgp
*bgp
)
4545 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
4546 bgp
->import_rt_hash
=
4547 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
4548 "BGP Import RT Hash");
4549 bgp
->vrf_import_rt_hash
=
4550 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
4551 "BGP VRF Import RT Hash");
4552 bgp
->vrf_import_rtl
= list_new();
4553 bgp
->vrf_import_rtl
->cmp
=
4554 (int (*)(void *, void *))evpn_route_target_cmp
;
4556 bgp
->vrf_export_rtl
= list_new();
4557 bgp
->vrf_export_rtl
->cmp
=
4558 (int (*)(void *, void *))evpn_route_target_cmp
;
4559 bgp
->l2vnis
= list_new();
4561 (int (*)(void *, void *))vni_hash_cmp
;
4562 bf_assign_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
4566 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
4568 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);