1 /* Ethernet-VPN Packet and vty Processing File
2 * Copyright (C) 2016 6WIND
3 * Copyright (C) 2017 Cumulus Networks, Inc.
5 * This file is part of FRR.
7 * FRRouting is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * FRRouting is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "bgpd/bgp_attr_evpn.h"
35 #include "bgpd/bgpd.h"
36 #include "bgpd/bgp_table.h"
37 #include "bgpd/bgp_route.h"
38 #include "bgpd/bgp_attr.h"
39 #include "bgpd/bgp_mplsvpn.h"
40 #include "bgpd/bgp_label.h"
41 #include "bgpd/bgp_evpn.h"
42 #include "bgpd/bgp_evpn_private.h"
43 #include "bgpd/bgp_ecommunity.h"
44 #include "bgpd/bgp_encap_types.h"
45 #include "bgpd/bgp_debug.h"
46 #include "bgpd/bgp_aspath.h"
47 #include "bgpd/bgp_zebra.h"
48 #include "bgpd/bgp_nexthop.h"
51 * Definitions and external declarations.
53 extern struct zclient
*zclient
;
55 DEFINE_QOBJ_TYPE(bgpevpn
)
59 * Static function declarations
61 static void delete_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
62 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
63 struct bgp_info
**ri
);
64 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
73 static unsigned int vni_hash_key_make(void *p
)
75 struct bgpevpn
*vpn
= p
;
76 return (jhash_1word(vpn
->vni
, 0));
80 * Comparison function for vni hash
82 static int vni_hash_cmp(const void *p1
, const void *p2
)
84 const struct bgpevpn
*vpn1
= p1
;
85 const struct bgpevpn
*vpn2
= p2
;
91 return (vpn1
->vni
== vpn2
->vni
);
95 * Make vrf import route target hash key.
97 static unsigned int vrf_import_rt_hash_key_make(void *p
)
99 struct vrf_irt_node
*irt
= p
;
100 char *pnt
= irt
->rt
.val
;
102 return jhash(pnt
, 8, 0x5abc1234);
106 * Comparison function for vrf import rt hash
108 static int vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
110 const struct vrf_irt_node
*irt1
= p1
;
111 const struct vrf_irt_node
*irt2
= p2
;
113 if (irt1
== NULL
&& irt2
== NULL
)
116 if (irt1
== NULL
|| irt2
== NULL
)
119 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
123 * Create a new vrf import_rt in default instance
125 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
127 struct bgp
*bgp_def
= NULL
;
128 struct vrf_irt_node
*irt
;
130 bgp_def
= bgp_get_default();
132 zlog_err("vrf import rt new - def instance not created yet");
136 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
137 sizeof(struct vrf_irt_node
));
142 irt
->vrfs
= list_new();
145 if (!hash_get(bgp_def
->vrf_import_rt_hash
, irt
, hash_alloc_intern
)) {
146 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
154 * Free the vrf import rt node
156 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
158 struct bgp
*bgp_def
= NULL
;
160 bgp_def
= bgp_get_default();
162 zlog_err("vrf import rt free - def instance not created yet");
166 hash_release(bgp_def
->vrf_import_rt_hash
, irt
);
167 list_delete_and_null(&irt
->vrfs
);
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
, struct bgp
*bgp_vrf
)
198 struct listnode
*node
= NULL
, *nnode
= NULL
;
199 struct bgp
*tmp_bgp_vrf
= NULL
;
201 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
202 if (tmp_bgp_vrf
== bgp_vrf
)
209 * Make import route target hash key.
211 static unsigned int import_rt_hash_key_make(void *p
)
213 struct irt_node
*irt
= p
;
214 char *pnt
= irt
->rt
.val
;
216 return jhash(pnt
, 8, 0xdeadbeef);
220 * Comparison function for import rt hash
222 static int import_rt_hash_cmp(const void *p1
, const void *p2
)
224 const struct irt_node
*irt1
= p1
;
225 const struct irt_node
*irt2
= p2
;
227 if (irt1
== NULL
&& irt2
== NULL
)
230 if (irt1
== NULL
|| irt2
== NULL
)
233 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
237 * Create a new import_rt
239 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
240 struct ecommunity_val
*rt
)
242 struct irt_node
*irt
;
247 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
252 irt
->vnis
= list_new();
255 if (!hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
)) {
256 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
264 * Free the import rt node
266 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
268 hash_release(bgp
->import_rt_hash
, irt
);
269 list_delete_and_null(&irt
->vnis
);
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
);
332 static void evpn_xxport_delete_ecomm(void *val
)
334 struct ecommunity
*ecomm
= val
;
335 ecommunity_free(&ecomm
);
339 * Mask off global-admin field of specified extended community (RT),
340 * just retain the local-admin field.
342 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
343 struct ecommunity_val
*src
)
349 if (type
== ECOMMUNITY_ENCODE_AS
) {
350 dst
->val
[2] = dst
->val
[3] = 0;
351 } else if (type
== ECOMMUNITY_ENCODE_AS4
352 || type
== ECOMMUNITY_ENCODE_IP
) {
353 dst
->val
[2] = dst
->val
[3] = 0;
354 dst
->val
[4] = dst
->val
[5] = 0;
359 * Map one RT to specified VRF.
360 * bgp_vrf = BGP vrf instance
362 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct ecommunity_val
*eval
)
364 struct vrf_irt_node
*irt
= NULL
;
365 struct ecommunity_val eval_tmp
;
367 /* If using "automatic" RT,
368 * we only care about the local-admin sub-field.
369 * This is to facilitate using L3VNI(VRF-VNI)
370 * as the RT for EBGP peering too.
372 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
373 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
374 mask_ecom_global_admin(&eval_tmp
, eval
);
376 irt
= lookup_vrf_import_rt(&eval_tmp
);
377 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
378 /* Already mapped. */
382 irt
= vrf_import_rt_new(&eval_tmp
);
384 /* Add VRF to the list for this RT. */
385 listnode_add(irt
->vrfs
, bgp_vrf
);
389 * Unmap specified VRF from specified RT. If there are no other
390 * VRFs for this RT, then the RT hash is deleted.
391 * bgp_vrf: BGP VRF specific instance
393 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
, 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 vrf_import_rt_free(irt
);
403 * Map one RT to specified VNI.
405 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
406 struct ecommunity_val
*eval
)
408 struct irt_node
*irt
;
409 struct ecommunity_val eval_tmp
;
411 /* If using "automatic" RT, we only care about the local-admin
413 * This is to facilitate using VNI as the RT for EBGP peering too.
415 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
416 if (!is_import_rt_configured(vpn
))
417 mask_ecom_global_admin(&eval_tmp
, eval
);
419 irt
= lookup_import_rt(bgp
, &eval_tmp
);
421 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
422 /* Already mapped. */
426 irt
= import_rt_new(bgp
, &eval_tmp
);
430 /* Add VNI to the hash list for this RT. */
431 listnode_add(irt
->vnis
, vpn
);
435 * Unmap specified VNI from specified RT. If there are no other
436 * VNIs for this RT, then the RT hash is deleted.
438 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
439 struct irt_node
*irt
)
441 /* Delete VNI from hash list for this RT. */
442 listnode_delete(irt
->vnis
, vpn
);
443 if (!listnode_head(irt
->vnis
)) {
444 import_rt_free(bgp
, irt
);
449 * Create RT extended community automatically from passed information:
450 * of the form AS:VNI.
451 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
452 * the need is to get a RT value that will be unique across different
453 * VNIs but the same across routers (in the same AS) for a particular
456 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
458 struct ecommunity_val eval
;
459 struct ecommunity
*ecomadd
;
461 if (bgp
->advertise_autort_rfc8365
)
462 vni
|= EVPN_AUTORT_VXLAN
;
463 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
465 ecomadd
= ecommunity_new();
466 ecommunity_add_val(ecomadd
, &eval
);
467 listnode_add_sort(rtl
, ecomadd
);
471 * Derive RD and RT for a VNI automatically. Invoked at the time of
474 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
476 bgp_evpn_derive_auto_rd(bgp
, vpn
);
477 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
478 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
482 * Convert nexthop (remote VTEP IP) into an IPv6 address.
484 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
486 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
488 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
489 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
493 * Add (update) or delete MACIP from zebra.
495 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
496 struct prefix_evpn
*p
,
497 struct in_addr remote_vtep_ip
, int add
,
502 char buf1
[ETHER_ADDR_STRLEN
];
503 char buf2
[INET6_ADDRSTRLEN
];
504 char buf3
[INET6_ADDRSTRLEN
];
507 if (!zclient
|| zclient
->sock
< 0)
510 /* Don't try to register if Zebra doesn't know of this instance. */
511 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
517 zclient_create_header(
518 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
520 stream_putl(s
, vpn
->vni
);
521 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
522 /* IP address length and IP address, if any. */
523 if (is_evpn_prefix_ipaddr_none(p
))
526 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
528 stream_putl(s
, ipa_len
);
529 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
531 stream_put_in_addr(s
, &remote_vtep_ip
);
533 /* TX flags - MAC sticky status and/or gateway mac */
535 stream_putc(s
, flags
);
537 stream_putw_at(s
, 0, stream_get_endp(s
));
539 if (bgp_debug_zebra(NULL
))
541 "Tx %s MACIP, VNI %u MAC %s IP %s (flags: 0x%x) remote VTEP %s",
542 add
? "ADD" : "DEL", vpn
->vni
,
543 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
545 ipaddr2str(&p
->prefix
.macip_addr
.ip
,
546 buf3
, sizeof(buf3
)), flags
,
547 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
550 return zclient_send_message(zclient
);
554 * Add (update) or delete remote VTEP from zebra.
556 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
557 struct prefix_evpn
*p
, int add
)
562 if (!zclient
|| zclient
->sock
< 0)
565 /* Don't try to register if Zebra doesn't know of this instance. */
566 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
572 zclient_create_header(
573 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
575 stream_putl(s
, vpn
->vni
);
576 if (is_evpn_prefix_ipaddr_v4(p
))
577 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
578 else if (is_evpn_prefix_ipaddr_v6(p
)) {
580 "Bad remote IP when trying to %s remote VTEP for VNI %u",
581 add
? "ADD" : "DEL", vpn
->vni
);
585 stream_putw_at(s
, 0, stream_get_endp(s
));
587 if (bgp_debug_zebra(NULL
))
588 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
589 add
? "ADD" : "DEL", vpn
->vni
,
590 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
592 return zclient_send_message(zclient
);
596 * Build extended communities for EVPN prefix route.
598 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
601 struct ecommunity ecom_encap
;
602 struct ecommunity ecom_rmac
;
603 struct ecommunity_val eval
;
604 struct ecommunity_val eval_rmac
;
605 bgp_encap_types tnl_type
;
606 struct listnode
*node
, *nnode
;
607 struct ecommunity
*ecom
;
608 struct list
*vrf_export_rtl
= NULL
;
611 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
612 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
613 encode_encap_extcomm(tnl_type
, &eval
);
615 ecom_encap
.val
= (uint8_t *)eval
.val
;
618 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
620 /* Add the export RTs for L3VNI/VRF */
621 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
622 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
623 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
625 ecommunity_merge(attr
->ecommunity
, ecom
);
628 /* add the router mac extended community */
629 if (!is_zero_mac(&attr
->rmac
)) {
630 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
631 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
633 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
635 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
638 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
642 * Build extended communities for EVPN route.
643 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
644 * and ENCAP extended communities are applicable for all routes.
645 * The default gateway extended community and MAC mobility (sticky) extended
646 * community are added as needed based on passed settings - only for type-2
647 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
648 * added, if present, based on passed settings - only for non-link-local
651 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
654 struct ecommunity ecom_encap
;
655 struct ecommunity ecom_sticky
;
656 struct ecommunity ecom_default_gw
;
657 struct ecommunity ecom_rmac
;
658 struct ecommunity_val eval
;
659 struct ecommunity_val eval_sticky
;
660 struct ecommunity_val eval_default_gw
;
661 struct ecommunity_val eval_rmac
;
662 bgp_encap_types tnl_type
;
663 struct listnode
*node
, *nnode
;
664 struct ecommunity
*ecom
;
666 struct list
*vrf_export_rtl
= NULL
;
669 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
670 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
671 encode_encap_extcomm(tnl_type
, &eval
);
673 ecom_encap
.val
= (uint8_t *)eval
.val
;
676 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
678 /* Add the export RTs for L2VNI */
679 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
680 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
682 /* Add the export RTs for L3VNI if told to - caller determines
683 * when this should be done.
686 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
687 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
688 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
690 attr
->ecommunity
= ecommunity_merge(
691 attr
->ecommunity
, ecom
);
695 /* Add MAC mobility (sticky) if needed. */
698 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
699 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
700 ecom_sticky
.size
= 1;
701 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
703 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
706 /* Add RMAC, if told to. */
708 memset(&ecom_rmac
, 0, sizeof(ecom_rmac
));
709 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
711 ecom_rmac
.val
= (uint8_t *)eval_rmac
.val
;
713 ecommunity_merge(attr
->ecommunity
, &ecom_rmac
);
716 /* Add default gateway, if needed. */
717 if (attr
->default_gw
) {
718 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
719 encode_default_gw_extcomm(&eval_default_gw
);
720 ecom_default_gw
.size
= 1;
721 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
723 ecommunity_merge(attr
->ecommunity
, &ecom_default_gw
);
726 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
730 * Add MAC mobility extended community to attribute.
732 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
734 struct ecommunity ecom_tmp
;
735 struct ecommunity_val eval
;
736 uint8_t *ecom_val_ptr
;
743 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
745 /* Find current MM ecommunity */
748 if (attr
->ecommunity
) {
749 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
750 pnt
= attr
->ecommunity
->val
+ (i
* 8);
754 if (type
== ECOMMUNITY_ENCODE_EVPN
756 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
757 ecom_val_ptr
= (uint8_t *)(attr
->ecommunity
->val
764 /* Update the existing MM ecommunity */
766 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ECOMMUNITY_SIZE
);
768 /* Add MM to existing */
770 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
772 ecom_tmp
.val
= (uint8_t *)eval
.val
;
774 if (attr
->ecommunity
)
776 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
778 attr
->ecommunity
= ecommunity_dup(&ecom_tmp
);
782 /* Install EVPN route into zebra. */
783 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
784 struct prefix_evpn
*p
,
785 struct in_addr remote_vtep_ip
, uint8_t flags
)
789 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
790 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
793 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 1);
798 /* Uninstall EVPN route from zebra. */
799 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
800 struct prefix_evpn
*p
,
801 struct in_addr remote_vtep_ip
)
805 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
806 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
809 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, 0);
815 * Due to MAC mobility, the prior "local" best route has been supplanted
816 * by a "remote" best route. The prior route has to be deleted and withdrawn
819 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
821 struct bgp_info
*old_local
)
823 struct bgp_node
*global_rn
;
825 afi_t afi
= AFI_L2VPN
;
826 safi_t safi
= SAFI_EVPN
;
828 /* Locate route node in the global EVPN routing table. Note that
829 * this table is a 2-level tree (RD-level + Prefix-level) similar to
832 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
833 (struct prefix
*)&rn
->p
, &vpn
->prd
);
835 /* Delete route entry in the global EVPN table. */
836 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
838 /* Schedule for processing - withdraws to peers happen from
842 bgp_process(bgp
, global_rn
, afi
, safi
);
843 bgp_unlock_node(global_rn
);
846 /* Delete route entry in the VNI route table, caller to remove. */
847 bgp_info_delete(rn
, old_local
);
851 * Calculate the best path for an EVPN route. Install/update best path in zebra,
854 static int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
857 struct bgp_info
*old_select
, *new_select
;
858 struct bgp_info_pair old_and_new
;
859 afi_t afi
= AFI_L2VPN
;
860 safi_t safi
= SAFI_EVPN
;
864 /* Compute the best path. */
865 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
867 old_select
= old_and_new
.old
;
868 new_select
= old_and_new
.new;
870 /* If the best path hasn't changed - see if there is still something to
874 if (old_select
&& old_select
== new_select
875 && old_select
->type
== ZEBRA_ROUTE_BGP
876 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
877 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
878 && !CHECK_FLAG(old_select
->flags
, BGP_INFO_ATTR_CHANGED
)
879 && !bgp
->addpath_tx_used
[afi
][safi
]) {
880 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
881 if (old_select
->attr
->sticky
)
882 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
883 if (old_select
->attr
->default_gw
)
884 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
885 ret
= evpn_zebra_install(
886 bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
887 old_select
->attr
->nexthop
, flags
);
889 UNSET_FLAG(old_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
890 bgp_zebra_clear_route_change_flags(rn
);
894 /* If the user did a "clear" this flag will be set */
895 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
897 /* bestpath has changed; update relevant fields and install or uninstall
898 * into the zebra RIB.
900 if (old_select
|| new_select
)
901 bgp_bump_version(rn
);
904 bgp_info_unset_flag(rn
, old_select
, BGP_INFO_SELECTED
);
906 bgp_info_set_flag(rn
, new_select
, BGP_INFO_SELECTED
);
907 bgp_info_unset_flag(rn
, new_select
, BGP_INFO_ATTR_CHANGED
);
908 UNSET_FLAG(new_select
->flags
, BGP_INFO_MULTIPATH_CHG
);
911 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
912 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
914 if (new_select
->attr
->sticky
)
915 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
916 if (new_select
->attr
->default_gw
)
917 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
918 ret
= evpn_zebra_install(bgp
, vpn
, (struct prefix_evpn
*)&rn
->p
,
919 new_select
->attr
->nexthop
, flags
);
920 /* If an old best existed and it was a "local" route, the only
922 * it would be supplanted is due to MAC mobility procedures. So,
924 * need to do an implicit delete and withdraw that route from
927 if (old_select
&& old_select
->peer
== bgp
->peer_self
928 && old_select
->type
== ZEBRA_ROUTE_BGP
929 && old_select
->sub_type
== BGP_ROUTE_STATIC
)
930 evpn_delete_old_local_route(bgp
, vpn
, rn
, old_select
);
932 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
933 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
934 ret
= evpn_zebra_uninstall(bgp
, vpn
,
935 (struct prefix_evpn
*)&rn
->p
,
936 old_select
->attr
->nexthop
);
939 /* Clear any route change flags. */
940 bgp_zebra_clear_route_change_flags(rn
);
942 /* Reap old select bgp_info, if it has been removed */
943 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_INFO_REMOVED
))
944 bgp_info_reap(rn
, old_select
);
950 * Return true if the local ri for this rn is of type gateway mac
952 static int evpn_route_is_def_gw(struct bgp
*bgp
, struct bgp_node
*rn
)
954 struct bgp_info
*tmp_ri
= NULL
;
955 struct bgp_info
*local_ri
= NULL
;
958 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
959 if (tmp_ri
->peer
== bgp
->peer_self
960 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
961 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
968 return local_ri
->attr
->default_gw
;
973 * Return true if the local ri for this rn has sticky set
975 static int evpn_route_is_sticky(struct bgp
*bgp
, struct bgp_node
*rn
)
977 struct bgp_info
*tmp_ri
;
978 struct bgp_info
*local_ri
;
981 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
982 if (tmp_ri
->peer
== bgp
->peer_self
983 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
984 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
991 return local_ri
->attr
->sticky
;
994 static int update_evpn_type5_route_entry(struct bgp
*bgp_def
,
995 struct bgp
*bgp_vrf
, afi_t afi
,
996 safi_t safi
, struct bgp_node
*rn
,
997 struct attr
*attr
, int *route_changed
)
999 struct attr
*attr_new
= NULL
;
1000 struct bgp_info
*ri
= NULL
;
1001 mpls_label_t label
= MPLS_INVALID_LABEL
;
1002 struct bgp_info
*local_ri
= NULL
;
1003 struct bgp_info
*tmp_ri
= NULL
;
1006 /* locate the local route entry if any */
1007 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1008 if (tmp_ri
->peer
== bgp_def
->peer_self
1009 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1010 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1014 /* create a new route entry if one doesnt exist.
1015 Otherwise see if route attr has changed
1019 /* route has changed as this is the first entry */
1022 /* Add (or update) attribute to hash. */
1023 attr_new
= bgp_attr_intern(attr
);
1025 /* create the route info from attribute */
1026 ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1027 bgp_def
->peer_self
, attr_new
, rn
);
1028 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1030 /* Type-5 routes advertise the L3-VNI */
1031 bgp_info_extra_get(ri
);
1032 vni2label(bgp_vrf
->l3vni
, &label
);
1033 memcpy(&ri
->extra
->label
, &label
, sizeof(label
));
1034 ri
->extra
->num_labels
= 1;
1036 /* add the route entry to route node*/
1037 bgp_info_add(rn
, ri
);
1041 if (!attrhash_cmp(tmp_ri
->attr
, attr
)) {
1043 /* attribute changed */
1046 /* The attribute has changed. */
1047 /* Add (or update) attribute to hash. */
1048 attr_new
= bgp_attr_intern(attr
);
1049 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1051 /* Restore route, if needed. */
1052 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1053 bgp_info_restore(rn
, tmp_ri
);
1055 /* Unintern existing, set to new. */
1056 bgp_attr_unintern(&tmp_ri
->attr
);
1057 tmp_ri
->attr
= attr_new
;
1058 tmp_ri
->uptime
= bgp_clock();
1064 /* update evpn type-5 route entry */
1065 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1066 struct attr
*src_attr
)
1068 afi_t afi
= AFI_L2VPN
;
1069 safi_t safi
= SAFI_EVPN
;
1071 struct bgp_node
*rn
= NULL
;
1072 struct bgp
*bgp_def
= NULL
;
1073 int route_changed
= 0;
1075 bgp_def
= bgp_get_default();
1079 /* Build path attribute for this route - use the source attr, if
1080 * present, else treat as locally originated.
1083 bgp_attr_dup(&attr
, src_attr
);
1085 memset(&attr
, 0, sizeof(struct attr
));
1086 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1088 /* Set nexthop to ourselves and fill in the Router MAC. */
1089 attr
.nexthop
= bgp_vrf
->originator_ip
;
1090 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1091 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1092 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
1094 /* Setup RT and encap extended community */
1095 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1097 /* get the route node in global table */
1098 rn
= bgp_afi_node_get(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1099 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1102 /* create or update the route entry within the route node */
1103 update_evpn_type5_route_entry(bgp_def
, bgp_vrf
, afi
, safi
, rn
, &attr
,
1106 /* schedule for processing and unlock node */
1107 if (route_changed
) {
1108 bgp_process(bgp_def
, rn
, afi
, safi
);
1109 bgp_unlock_node(rn
);
1112 /* uninten temporary */
1114 aspath_unintern(&attr
.aspath
);
1119 * Create or update EVPN route entry. This could be in the VNI route table
1120 * or the global route table.
1122 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1123 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1124 struct attr
*attr
, int add
, int vni_table
,
1125 struct bgp_info
**ri
, uint8_t flags
)
1127 struct bgp_info
*tmp_ri
;
1128 struct bgp_info
*local_ri
, *remote_ri
;
1129 struct attr
*attr_new
;
1130 mpls_label_t label
[BGP_MAX_LABELS
];
1131 uint32_t num_labels
= 1;
1132 int route_change
= 1;
1134 struct prefix_evpn
*evp
;
1137 evp
= (struct prefix_evpn
*)&rn
->p
;
1138 memset(&label
, 0, sizeof(label
));
1140 /* See if this is an update of an existing route, or a new add. Also,
1141 * identify if already known from remote, and if so, the one with the
1142 * highest sequence number; this is only when adding to the VNI routing
1145 local_ri
= remote_ri
= NULL
;
1146 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
) {
1147 if (tmp_ri
->peer
== bgp
->peer_self
1148 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1149 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1152 if (tmp_ri
->type
== ZEBRA_ROUTE_BGP
1153 && tmp_ri
->sub_type
== BGP_ROUTE_IMPORTED
1154 && CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
)) {
1157 else if (mac_mobility_seqnum(tmp_ri
->attr
)
1158 > mac_mobility_seqnum(remote_ri
->attr
))
1164 /* If route doesn't exist already, create a new one, if told to.
1165 * Otherwise act based on whether the attributes of the route have
1168 if (!local_ri
&& !add
)
1172 /* When learnt locally for the first time but already known from
1173 * remote, we have to initiate appropriate MAC mobility steps.
1175 * is applicable when updating the VNI routing table.
1176 * We need to skip mobility steps for g/w macs (local mac on g/w
1177 * SVI) advertised in EVPN.
1178 * This will ensure that local routes are preferred for g/w macs
1180 if (remote_ri
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
)) {
1181 uint32_t cur_seqnum
;
1183 /* Add MM extended community to route. */
1184 cur_seqnum
= mac_mobility_seqnum(remote_ri
->attr
);
1185 add_mac_mobility_to_attr(cur_seqnum
+ 1, attr
);
1188 /* Add (or update) attribute to hash. */
1189 attr_new
= bgp_attr_intern(attr
);
1191 /* Extract MAC mobility sequence number, if any. */
1192 attr_new
->mm_seqnum
=
1193 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1194 attr_new
->sticky
= sticky
;
1196 /* Create new route with its attribute. */
1197 tmp_ri
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1198 bgp
->peer_self
, attr_new
, rn
);
1199 SET_FLAG(tmp_ri
->flags
, BGP_INFO_VALID
);
1200 bgp_info_extra_get(tmp_ri
);
1202 /* The VNI goes into the 'label' field of the route */
1203 vni2label(vpn
->vni
, &label
[0]);
1205 /* Type-2 routes may carry a second VNI - the L3-VNI.
1206 * Only attach second label if we are advertising two labels for
1209 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1210 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1213 l3vni
= bgpevpn_get_l3vni(vpn
);
1215 vni2label(l3vni
, &label
[1]);
1220 memcpy(&tmp_ri
->extra
->label
, label
, sizeof(label
));
1221 tmp_ri
->extra
->num_labels
= num_labels
;
1222 bgp_info_add(rn
, tmp_ri
);
1225 if (attrhash_cmp(tmp_ri
->attr
, attr
)
1226 && !CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1230 * The attributes have changed, type-2 routes needs to
1231 * be advertised with right labels.
1233 vni2label(vpn
->vni
, &label
[0]);
1234 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1235 && CHECK_FLAG(vpn
->flags
,
1236 VNI_FLAG_USE_TWO_LABELS
)) {
1239 l3vni
= bgpevpn_get_l3vni(vpn
);
1241 vni2label(l3vni
, &label
[1]);
1245 memcpy(&tmp_ri
->extra
->label
, label
, sizeof(label
));
1246 tmp_ri
->extra
->num_labels
= num_labels
;
1248 /* The attribute has changed. */
1249 /* Add (or update) attribute to hash. */
1250 attr_new
= bgp_attr_intern(attr
);
1251 bgp_info_set_flag(rn
, tmp_ri
, BGP_INFO_ATTR_CHANGED
);
1253 /* Restore route, if needed. */
1254 if (CHECK_FLAG(tmp_ri
->flags
, BGP_INFO_REMOVED
))
1255 bgp_info_restore(rn
, tmp_ri
);
1257 /* Unintern existing, set to new. */
1258 bgp_attr_unintern(&tmp_ri
->attr
);
1259 tmp_ri
->attr
= attr_new
;
1260 tmp_ri
->uptime
= bgp_clock();
1264 /* Return back the route entry. */
1266 return route_change
;
1270 * Create or update EVPN route (of type based on prefix) for specified VNI
1271 * and schedule for processing.
1273 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1274 struct prefix_evpn
*p
, uint8_t flags
)
1276 struct bgp_node
*rn
;
1278 struct attr
*attr_new
;
1279 int add_l3_ecomm
= 0;
1280 struct bgp_info
*ri
;
1281 afi_t afi
= AFI_L2VPN
;
1282 safi_t safi
= SAFI_EVPN
;
1285 memset(&attr
, 0, sizeof(struct attr
));
1287 /* Build path-attribute for this route. */
1288 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1289 attr
.nexthop
= vpn
->originator_ip
;
1290 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1291 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1292 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1293 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1295 /* PMSI is only needed for type-3 routes */
1296 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
)
1297 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1299 /* router mac is only needed for type-2 routes here. */
1300 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1301 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1302 vni2label(vpn
->vni
, &(attr
.label
));
1304 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1305 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1308 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1309 (is_evpn_prefix_ipaddr_v4(p
) ||
1310 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1311 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
))
1314 /* Set up extended community. */
1315 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1317 /* First, create (or fetch) route node within the VNI. */
1318 /* NOTE: There is no RD here. */
1319 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1321 /* Create or update route entry. */
1322 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &attr
,
1325 attr_new
= ri
->attr
;
1327 /* Perform route selection; this is just to set the flags correctly
1328 * as local route in the VNI always wins.
1330 evpn_route_select_install(bgp
, vpn
, rn
);
1331 bgp_unlock_node(rn
);
1333 /* If this is a new route or some attribute has changed, export the
1334 * route to the global table. The route will be advertised to peers
1335 * from there. Note that this table is a 2-level tree (RD-level +
1336 * Prefix-level) similar to L3VPN routes.
1339 struct bgp_info
*global_ri
;
1341 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1342 (struct prefix
*)p
, &vpn
->prd
);
1343 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, attr_new
, 1, 0,
1346 /* Schedule for processing and unlock node. */
1347 bgp_process(bgp
, rn
, afi
, safi
);
1348 bgp_unlock_node(rn
);
1351 /* Unintern temporary. */
1352 aspath_unintern(&attr
.aspath
);
1357 /* Delete EVPN type5 route entry from global table */
1358 static void delete_evpn_type5_route_entry(struct bgp
*bgp_def
,
1359 struct bgp
*bgp_vrf
, afi_t afi
,
1360 safi_t safi
, struct bgp_node
*rn
,
1361 struct bgp_info
**ri
)
1363 struct bgp_info
*tmp_ri
= NULL
;
1367 /* find the matching route entry */
1368 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
)
1369 if (tmp_ri
->peer
== bgp_def
->peer_self
1370 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1371 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1376 /* Mark route for delete. */
1378 bgp_info_delete(rn
, tmp_ri
);
1381 /* Delete EVPN type5 route */
1382 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1384 afi_t afi
= AFI_L2VPN
;
1385 safi_t safi
= SAFI_EVPN
;
1386 struct bgp_node
*rn
= NULL
;
1387 struct bgp_info
*ri
= NULL
;
1388 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
1390 bgp_def
= bgp_get_default();
1394 /* locate the global route entry for this type-5 prefix */
1395 rn
= bgp_afi_node_lookup(bgp_def
->rib
[afi
][safi
], afi
, safi
,
1396 (struct prefix
*)evp
, &bgp_vrf
->vrf_prd
);
1400 delete_evpn_type5_route_entry(bgp_def
, bgp_vrf
, afi
, safi
, rn
, &ri
);
1402 bgp_process(bgp_def
, rn
, afi
, safi
);
1403 bgp_unlock_node(rn
);
1408 * Delete EVPN route entry. This could be in the VNI route table
1409 * or the global route table.
1411 static void delete_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1412 afi_t afi
, safi_t safi
, struct bgp_node
*rn
,
1413 struct bgp_info
**ri
)
1415 struct bgp_info
*tmp_ri
;
1419 /* Now, find matching route. */
1420 for (tmp_ri
= rn
->info
; tmp_ri
; tmp_ri
= tmp_ri
->next
)
1421 if (tmp_ri
->peer
== bgp
->peer_self
1422 && tmp_ri
->type
== ZEBRA_ROUTE_BGP
1423 && tmp_ri
->sub_type
== BGP_ROUTE_STATIC
)
1428 /* Mark route for delete. */
1430 bgp_info_delete(rn
, tmp_ri
);
1434 * Delete EVPN route (of type based on prefix) for specified VNI and
1435 * schedule for processing.
1437 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1438 struct prefix_evpn
*p
)
1440 struct bgp_node
*rn
, *global_rn
;
1441 struct bgp_info
*ri
;
1442 afi_t afi
= AFI_L2VPN
;
1443 safi_t safi
= SAFI_EVPN
;
1445 /* First, locate the route node within the VNI. If it doesn't exist,
1447 * is nothing further to do.
1449 /* NOTE: There is no RD here. */
1450 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1454 /* Next, locate route node in the global EVPN routing table. Note that
1455 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1458 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1459 (struct prefix
*)p
, &vpn
->prd
);
1461 /* Delete route entry in the global EVPN table. */
1462 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
1464 /* Schedule for processing - withdraws to peers happen from
1468 bgp_process(bgp
, global_rn
, afi
, safi
);
1469 bgp_unlock_node(global_rn
);
1472 /* Delete route entry in the VNI route table. This can just be removed.
1474 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1476 bgp_info_reap(rn
, ri
);
1477 bgp_unlock_node(rn
);
1483 * Update all type-2 (MACIP) local routes for this VNI - these should also
1484 * be scheduled for advertise to peers.
1486 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1490 struct bgp_node
*rn
;
1491 struct bgp_info
*ri
;
1493 struct attr attr_sticky
;
1494 struct attr attr_def_gw
;
1495 struct attr attr_ip6_ll
;
1496 struct attr
*attr_new
;
1497 int add_l3_ecomm
= 0;
1501 memset(&attr
, 0, sizeof(struct attr
));
1502 memset(&attr_sticky
, 0, sizeof(struct attr
));
1503 memset(&attr_def_gw
, 0, sizeof(struct attr
));
1504 memset(&attr_ip6_ll
, 0, sizeof(struct attr
));
1506 /* Build path-attribute - multiple type-2 routes for this VNI will share
1507 * the same path attribute, but we need separate structures for sticky
1508 * MACs, default gateway and IPv6 link-local addresses (no L3 RT/RMAC).
1510 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1511 bgp_attr_default_set(&attr_sticky
, BGP_ORIGIN_IGP
);
1512 bgp_attr_default_set(&attr_def_gw
, BGP_ORIGIN_IGP
);
1513 attr
.nexthop
= vpn
->originator_ip
;
1514 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1515 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1516 bgpevpn_get_rmac(vpn
, &attr
.rmac
);
1517 attr_sticky
.nexthop
= vpn
->originator_ip
;
1518 attr_sticky
.mp_nexthop_global_in
= vpn
->originator_ip
;
1519 attr_sticky
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1520 attr_sticky
.sticky
= 1;
1521 bgpevpn_get_rmac(vpn
, &attr_sticky
.rmac
);
1522 attr_def_gw
.nexthop
= vpn
->originator_ip
;
1523 attr_def_gw
.mp_nexthop_global_in
= vpn
->originator_ip
;
1524 attr_def_gw
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1525 attr_def_gw
.default_gw
= 1;
1526 bgpevpn_get_rmac(vpn
, &attr_def_gw
.rmac
);
1527 bgp_attr_default_set(&attr_ip6_ll
, BGP_ORIGIN_IGP
);
1528 attr_ip6_ll
.nexthop
= vpn
->originator_ip
;
1529 attr_ip6_ll
.mp_nexthop_global_in
= vpn
->originator_ip
;
1530 attr_ip6_ll
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1532 /* Add L3 VNI RTs and RMAC for non IPv6 link-local attributes if
1533 * using L3 VNI for type-2 routes also.
1535 if (CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
))
1538 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1539 build_evpn_route_extcomm(vpn
, &attr_sticky
, add_l3_ecomm
);
1540 build_evpn_route_extcomm(vpn
, &attr_def_gw
, add_l3_ecomm
);
1541 build_evpn_route_extcomm(vpn
, &attr_ip6_ll
, 0);
1543 /* Walk this VNI's route table and update local type-2 routes. For any
1544 * routes updated, update corresponding entry in the global table too.
1546 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1547 rn
= bgp_route_next(rn
)) {
1548 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1549 struct bgp_node
*rd_rn
;
1550 struct bgp_info
*global_ri
;
1552 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1555 if (is_evpn_prefix_ipaddr_v6(evp
) &&
1556 IN6_IS_ADDR_LINKLOCAL(&evp
->prefix
.macip_addr
.ip
.ipaddr_v6
))
1557 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1558 &attr_ip6_ll
, 0, 1, &ri
, 0);
1560 if (evpn_route_is_sticky(bgp
, rn
))
1561 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1562 &attr_sticky
, 0, 1, &ri
,
1564 else if (evpn_route_is_def_gw(bgp
, rn
))
1565 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1566 &attr_def_gw
, 0, 1, &ri
,
1569 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
,
1570 &attr
, 0, 1, &ri
, 0);
1573 /* If a local route exists for this prefix, we need to update
1574 * the global routing table too.
1579 /* Perform route selection; this is just to set the flags
1581 * as local route in the VNI always wins.
1583 evpn_route_select_install(bgp
, vpn
, rn
);
1585 attr_new
= ri
->attr
;
1587 /* Update route in global routing table. */
1588 rd_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1589 (struct prefix
*)evp
, &vpn
->prd
);
1591 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, rd_rn
, attr_new
, 0,
1594 /* Schedule for processing and unlock node. */
1595 bgp_process(bgp
, rd_rn
, afi
, safi
);
1596 bgp_unlock_node(rd_rn
);
1599 /* Unintern temporary. */
1600 aspath_unintern(&attr
.aspath
);
1601 aspath_unintern(&attr_sticky
.aspath
);
1602 aspath_unintern(&attr_def_gw
.aspath
);
1603 aspath_unintern(&attr_ip6_ll
.aspath
);
1609 * Delete all type-2 (MACIP) local routes for this VNI - only from the
1610 * global routing table. These are also scheduled for withdraw from peers.
1612 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1616 struct bgp_node
*rdrn
, *rn
;
1617 struct bgp_table
*table
;
1618 struct bgp_info
*ri
;
1623 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
1624 if (rdrn
&& rdrn
->info
) {
1625 table
= (struct bgp_table
*)rdrn
->info
;
1626 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
1627 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1629 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1632 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1634 bgp_process(bgp
, rn
, afi
, safi
);
1638 /* Unlock RD node. */
1640 bgp_unlock_node(rdrn
);
1646 * Delete all type-2 (MACIP) local routes for this VNI - from the global
1647 * table as well as the per-VNI route table.
1649 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1653 struct bgp_node
*rn
;
1654 struct bgp_info
*ri
;
1659 /* First, walk the global route table for this VNI's type-2 local
1661 * EVPN routes are a 2-level table, first get the RD table.
1663 delete_global_type2_routes(bgp
, vpn
);
1665 /* Next, walk this VNI's route table and delete local type-2 routes. */
1666 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1667 rn
= bgp_route_next(rn
)) {
1668 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
1670 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1673 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, rn
, &ri
);
1675 /* Route entry in local table gets deleted immediately. */
1677 bgp_info_reap(rn
, ri
);
1684 * Delete all routes in the per-VNI route table.
1686 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1688 struct bgp_node
*rn
;
1689 struct bgp_info
*ri
, *nextri
;
1691 /* Walk this VNI's route table and delete all routes. */
1692 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
1693 rn
= bgp_route_next(rn
)) {
1694 for (ri
= rn
->info
; (ri
!= NULL
) && (nextri
= ri
->next
, 1);
1696 bgp_info_delete(rn
, ri
);
1697 bgp_info_reap(rn
, ri
);
1705 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
1706 * export RT getting modified or change to tunnel IP. Note that these
1707 * situations need the route in the per-VNI table as well as the global
1708 * table to be updated (as attributes change).
1710 static int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1713 struct prefix_evpn p
;
1715 /* Update and advertise the type-3 route (only one) followed by the
1716 * locally learnt type-2 routes (MACIP) - for this VNI.
1718 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1719 ret
= update_evpn_route(bgp
, vpn
, &p
, 0);
1723 return update_all_type2_routes(bgp
, vpn
);
1727 * Delete (and withdraw) local routes for specified VNI from the global
1728 * table and per-VNI table. After this, remove all other routes from
1729 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
1730 * (advertise-all-vni) being disabled.
1732 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1735 struct prefix_evpn p
;
1737 /* Delete and withdraw locally learnt type-2 routes (MACIP)
1738 * followed by type-3 routes (only one) - for this VNI.
1740 ret
= delete_all_type2_routes(bgp
, vpn
);
1744 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1745 ret
= delete_evpn_route(bgp
, vpn
, &p
);
1749 /* Delete all routes from the per-VNI table. */
1750 return delete_all_vni_routes(bgp
, vpn
);
1754 * There is a tunnel endpoint IP address change for this VNI, delete
1755 * prior type-3 route (if needed) and update.
1756 * Note: Route re-advertisement happens elsewhere after other processing
1759 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1760 struct in_addr originator_ip
)
1762 struct prefix_evpn p
;
1764 /* If VNI is not live, we only need to update the originator ip */
1765 if (!is_vni_live(vpn
)) {
1766 vpn
->originator_ip
= originator_ip
;
1770 /* Update the tunnel-ip hash */
1771 bgp_tip_del(bgp
, &vpn
->originator_ip
);
1772 bgp_tip_add(bgp
, &originator_ip
);
1774 /* filter routes as martian nexthop db has changed */
1775 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
1777 /* Need to withdraw type-3 route as the originator IP is part
1780 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
1781 delete_evpn_route(bgp
, vpn
, &p
);
1783 /* Update the tunnel IP and re-advertise all routes for this VNI. */
1784 vpn
->originator_ip
= originator_ip
;
1789 * Install route entry into the VRF routing table and invoke route selection.
1791 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
1792 struct prefix_evpn
*evp
,
1793 struct bgp_info
*parent_ri
)
1795 struct bgp_node
*rn
;
1796 struct bgp_info
*ri
;
1798 struct attr
*attr_new
;
1801 struct prefix
*pp
= &p
;
1804 char buf
[PREFIX_STRLEN
];
1805 char buf1
[PREFIX_STRLEN
];
1807 memset(pp
, 0, sizeof(struct prefix
));
1808 ip_prefix_from_evpn_prefix(evp
, pp
);
1810 if (bgp_debug_zebra(NULL
)) {
1812 "installing evpn prefix %s as ip prefix %s in vrf %s",
1813 prefix2str(evp
, buf
, sizeof(buf
)),
1814 prefix2str(pp
, buf1
, sizeof(buf
)),
1815 vrf_id_to_name(bgp_vrf
->vrf_id
));
1818 /* Create (or fetch) route within the VRF. */
1819 /* NOTE: There is no RD here. */
1820 if (is_evpn_prefix_ipaddr_v4(evp
)) {
1822 safi
= SAFI_UNICAST
;
1823 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
1824 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
1826 safi
= SAFI_UNICAST
;
1827 rn
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
1831 /* EVPN routes currently only support a IPv4 next hop which corresponds
1832 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
1833 * or prefix route, we have to convert the next hop to an IPv4-mapped
1834 * address for the rest of the code to flow through. In the case of IPv4,
1835 * make sure to set the flag for next hop attribute.
1837 bgp_attr_dup(&attr
, parent_ri
->attr
);
1839 evpn_convert_nexthop_to_ipv6(&attr
);
1841 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
1843 /* Check if route entry is already present. */
1844 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1846 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
1850 /* Add (or update) attribute to hash. */
1851 attr_new
= bgp_attr_intern(&attr
);
1853 /* Create new route with its attribute. */
1854 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
1855 parent_ri
->peer
, attr_new
, rn
);
1856 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1857 bgp_info_extra_get(ri
);
1858 ri
->extra
->parent
= bgp_info_lock(parent_ri
);
1859 bgp_lock_node((struct bgp_node
*)parent_ri
->net
);
1860 if (parent_ri
->extra
) {
1861 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
1862 sizeof(ri
->extra
->label
));
1863 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
1865 bgp_info_add(rn
, ri
);
1867 if (attrhash_cmp(ri
->attr
, &attr
)
1868 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
1869 bgp_unlock_node(rn
);
1872 /* The attribute has changed. */
1873 /* Add (or update) attribute to hash. */
1874 attr_new
= bgp_attr_intern(&attr
);
1876 /* Restore route, if needed. */
1877 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
1878 bgp_info_restore(rn
, ri
);
1880 /* Mark if nexthop has changed. */
1881 if ((afi
== AFI_IP
&&
1882 !IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
)) ||
1884 !IPV6_ADDR_SAME(&ri
->attr
->mp_nexthop_global
,
1885 &attr_new
->mp_nexthop_global
)))
1886 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
1888 /* Unintern existing, set to new. */
1889 bgp_attr_unintern(&ri
->attr
);
1890 ri
->attr
= attr_new
;
1891 ri
->uptime
= bgp_clock();
1894 /* Perform route selection and update zebra, if required. */
1895 bgp_process(bgp_vrf
, rn
, afi
, safi
);
1901 * Install route entry into the VNI routing table and invoke route selection.
1903 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1904 struct prefix_evpn
*p
,
1905 struct bgp_info
*parent_ri
)
1907 struct bgp_node
*rn
;
1908 struct bgp_info
*ri
;
1909 struct attr
*attr_new
;
1912 /* Create (or fetch) route within the VNI. */
1913 /* NOTE: There is no RD here. */
1914 rn
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1916 /* Check if route entry is already present. */
1917 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1919 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
1923 /* Add (or update) attribute to hash. */
1924 attr_new
= bgp_attr_intern(parent_ri
->attr
);
1926 /* Create new route with its attribute. */
1927 ri
= info_make(parent_ri
->type
, BGP_ROUTE_IMPORTED
, 0,
1928 parent_ri
->peer
, attr_new
, rn
);
1929 SET_FLAG(ri
->flags
, BGP_INFO_VALID
);
1930 bgp_info_extra_get(ri
);
1931 ri
->extra
->parent
= bgp_info_lock(parent_ri
);
1932 bgp_lock_node((struct bgp_node
*)parent_ri
->net
);
1933 if (parent_ri
->extra
) {
1934 memcpy(&ri
->extra
->label
, &parent_ri
->extra
->label
,
1935 sizeof(ri
->extra
->label
));
1936 ri
->extra
->num_labels
= parent_ri
->extra
->num_labels
;
1938 bgp_info_add(rn
, ri
);
1940 if (attrhash_cmp(ri
->attr
, parent_ri
->attr
)
1941 && !CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
)) {
1942 bgp_unlock_node(rn
);
1945 /* The attribute has changed. */
1946 /* Add (or update) attribute to hash. */
1947 attr_new
= bgp_attr_intern(parent_ri
->attr
);
1949 /* Restore route, if needed. */
1950 if (CHECK_FLAG(ri
->flags
, BGP_INFO_REMOVED
))
1951 bgp_info_restore(rn
, ri
);
1953 /* Mark if nexthop has changed. */
1954 if (!IPV4_ADDR_SAME(&ri
->attr
->nexthop
, &attr_new
->nexthop
))
1955 SET_FLAG(ri
->flags
, BGP_INFO_IGP_CHANGED
);
1957 /* Unintern existing, set to new. */
1958 bgp_attr_unintern(&ri
->attr
);
1959 ri
->attr
= attr_new
;
1960 ri
->uptime
= bgp_clock();
1963 /* Perform route selection and update zebra, if required. */
1964 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
1970 * Uninstall route entry from the VRF routing table and send message
1971 * to zebra, if appropriate.
1973 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
1974 struct prefix_evpn
*evp
,
1975 struct bgp_info
*parent_ri
)
1977 struct bgp_node
*rn
;
1978 struct bgp_info
*ri
;
1981 struct prefix
*pp
= &p
;
1984 char buf
[PREFIX_STRLEN
];
1985 char buf1
[PREFIX_STRLEN
];
1987 memset(pp
, 0, sizeof(struct prefix
));
1988 ip_prefix_from_evpn_prefix(evp
, pp
);
1990 if (bgp_debug_zebra(NULL
)) {
1992 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
1993 prefix2str(evp
, buf
, sizeof(buf
)),
1994 prefix2str(pp
, buf1
, sizeof(buf
)),
1995 vrf_id_to_name(bgp_vrf
->vrf_id
));
1998 /* Locate route within the VRF. */
1999 /* NOTE: There is no RD here. */
2000 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2002 safi
= SAFI_UNICAST
;
2003 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2006 safi
= SAFI_UNICAST
;
2007 rn
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2013 /* Find matching route entry. */
2014 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2016 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2022 /* Mark entry for deletion */
2023 bgp_info_delete(rn
, ri
);
2025 /* Perform route selection and update zebra, if required. */
2026 bgp_process(bgp_vrf
, rn
, afi
, safi
);
2028 /* Unlock route node. */
2029 bgp_unlock_node(rn
);
2035 * Uninstall route entry from the VNI routing table and send message
2036 * to zebra, if appropriate.
2038 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2039 struct prefix_evpn
*p
,
2040 struct bgp_info
*parent_ri
)
2042 struct bgp_node
*rn
;
2043 struct bgp_info
*ri
;
2046 /* Locate route within the VNI. */
2047 /* NOTE: There is no RD here. */
2048 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2052 /* Find matching route entry. */
2053 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2055 && (struct bgp_info
*)ri
->extra
->parent
== parent_ri
)
2061 /* Mark entry for deletion */
2062 bgp_info_delete(rn
, ri
);
2064 /* Perform route selection and update zebra, if required. */
2065 ret
= evpn_route_select_install(bgp
, vpn
, rn
);
2067 /* Unlock route node. */
2068 bgp_unlock_node(rn
);
2074 * Given a route entry and a VRF, see if this route entry should be
2075 * imported into the VRF i.e., RTs match.
2077 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
, struct bgp_info
*ri
)
2079 struct attr
*attr
= ri
->attr
;
2080 struct ecommunity
*ecom
;
2084 /* Route should have valid RT to be even considered. */
2085 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2088 ecom
= attr
->ecommunity
;
2089 if (!ecom
|| !ecom
->size
)
2092 /* For each extended community RT, see if it matches this VNI. If any RT
2093 * matches, we're done.
2095 for (i
= 0; i
< ecom
->size
; i
++) {
2097 uint8_t type
, sub_type
;
2098 struct ecommunity_val
*eval
;
2099 struct ecommunity_val eval_tmp
;
2100 struct vrf_irt_node
*irt
;
2102 /* Only deal with RTs */
2103 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2104 eval
= (struct ecommunity_val
*)(ecom
->val
2105 + (i
* ECOMMUNITY_SIZE
));
2108 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2111 /* See if this RT matches specified VNIs import RTs */
2112 irt
= lookup_vrf_import_rt(eval
);
2114 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2117 /* Also check for non-exact match. In this, we mask out the AS
2119 * only check on the local-admin sub-field. This is to
2121 * VNI as the RT for EBGP peering too.
2124 if (type
== ECOMMUNITY_ENCODE_AS
2125 || type
== ECOMMUNITY_ENCODE_AS4
2126 || type
== ECOMMUNITY_ENCODE_IP
) {
2127 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2128 mask_ecom_global_admin(&eval_tmp
, eval
);
2129 irt
= lookup_vrf_import_rt(&eval_tmp
);
2132 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2140 * Given a route entry and a VNI, see if this route entry should be
2141 * imported into the VNI i.e., RTs match.
2143 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2144 struct bgp_info
*ri
)
2146 struct attr
*attr
= ri
->attr
;
2147 struct ecommunity
*ecom
;
2151 /* Route should have valid RT to be even considered. */
2152 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2155 ecom
= attr
->ecommunity
;
2156 if (!ecom
|| !ecom
->size
)
2159 /* For each extended community RT, see if it matches this VNI. If any RT
2160 * matches, we're done.
2162 for (i
= 0; i
< ecom
->size
; i
++) {
2164 uint8_t type
, sub_type
;
2165 struct ecommunity_val
*eval
;
2166 struct ecommunity_val eval_tmp
;
2167 struct irt_node
*irt
;
2169 /* Only deal with RTs */
2170 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2171 eval
= (struct ecommunity_val
*)(ecom
->val
2172 + (i
* ECOMMUNITY_SIZE
));
2175 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2178 /* See if this RT matches specified VNIs import RTs */
2179 irt
= lookup_import_rt(bgp
, eval
);
2181 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2184 /* Also check for non-exact match. In this, we mask out the AS
2186 * only check on the local-admin sub-field. This is to
2188 * VNI as the RT for EBGP peering too.
2191 if (type
== ECOMMUNITY_ENCODE_AS
2192 || type
== ECOMMUNITY_ENCODE_AS4
2193 || type
== ECOMMUNITY_ENCODE_IP
) {
2194 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2195 mask_ecom_global_admin(&eval_tmp
, eval
);
2196 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2199 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2207 * Install or uninstall mac-ip routes are appropriate for this
2210 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
2214 struct bgp_node
*rd_rn
, *rn
;
2215 struct bgp_table
*table
;
2216 struct bgp_info
*ri
;
2218 char buf
[PREFIX_STRLEN
];
2219 struct bgp
*bgp_def
= NULL
;
2223 bgp_def
= bgp_get_default();
2227 /* Walk entire global routing table and evaluate routes which could be
2228 * imported into this VRF. Note that we need to loop through all global
2229 * routes to determine which route matches the import rt on vrf
2231 for (rd_rn
= bgp_table_top(bgp_def
->rib
[afi
][safi
]); rd_rn
;
2232 rd_rn
= bgp_route_next(rd_rn
)) {
2233 table
= (struct bgp_table
*)(rd_rn
->info
);
2237 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2238 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2240 /* if not mac-ip route skip this route */
2241 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2242 || evp
->prefix
.route_type
2243 == BGP_EVPN_IP_PREFIX_ROUTE
))
2246 /* if not a mac+ip route skip this route */
2247 if (!(is_evpn_prefix_ipaddr_v4(evp
)
2248 || is_evpn_prefix_ipaddr_v6(evp
)))
2251 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2252 /* Consider "valid" remote routes applicable for
2255 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2256 && ri
->type
== ZEBRA_ROUTE_BGP
2257 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2260 if (is_route_matching_for_vrf(bgp_vrf
, ri
)) {
2262 ret
= install_evpn_route_entry_in_vrf(
2265 ret
= uninstall_evpn_route_entry_in_vrf(
2270 "Failed to %s EVPN %s route in VRF %s",
2273 prefix2str(evp
, buf
,
2288 * Install or uninstall routes of specified type that are appropriate for this
2291 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
2292 struct bgpevpn
*vpn
,
2293 bgp_evpn_route_type rtype
,
2298 struct bgp_node
*rd_rn
, *rn
;
2299 struct bgp_table
*table
;
2300 struct bgp_info
*ri
;
2306 /* Walk entire global routing table and evaluate routes which could be
2307 * imported into this VPN. Note that we cannot just look at the routes
2309 * the VNI's RD - remote routes applicable for this VNI could have any
2312 /* EVPN routes are a 2-level table. */
2313 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
2314 rd_rn
= bgp_route_next(rd_rn
)) {
2315 table
= (struct bgp_table
*)(rd_rn
->info
);
2319 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
2320 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2322 if (evp
->prefix
.route_type
!= rtype
)
2325 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
2326 /* Consider "valid" remote routes applicable for
2328 if (!(CHECK_FLAG(ri
->flags
, BGP_INFO_VALID
)
2329 && ri
->type
== ZEBRA_ROUTE_BGP
2330 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
2333 if (is_route_matching_for_vni(bgp
, vpn
, ri
)) {
2335 ret
= install_evpn_route_entry(
2338 ret
= uninstall_evpn_route_entry(
2343 "%u: Failed to %s EVPN %s route in VNI %u",
2347 rtype
== BGP_EVPN_MAC_IP_ROUTE
2361 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
2362 * is invoked upon l3vni-add or l3vni import rt change
2364 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
2366 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
2371 * Install any existing remote routes applicable for this VNI into its
2372 * routing table. This is invoked when a VNI becomes "live" or its Import
2375 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2379 /* Install type-3 routes followed by type-2 routes - the ones applicable
2382 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
2387 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
2391 /* uninstall routes from l3vni vrf. */
2392 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
2394 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
2399 * Uninstall any existing remote routes for this VNI. One scenario in which
2400 * this is invoked is upon an import RT change.
2402 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2406 /* Uninstall type-2 routes followed by type-3 routes - the ones
2410 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
2415 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
2420 * Install or uninstall route in matching VRFs (list).
2422 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
2423 safi_t safi
, struct prefix_evpn
*evp
,
2424 struct bgp_info
*ri
,
2425 struct list
*vrfs
, int install
)
2427 char buf
[PREFIX2STR_BUFFER
];
2428 struct bgp
*bgp_vrf
;
2429 struct listnode
*node
, *nnode
;
2431 /* Only type-2/type-5 routes go into a VRF */
2432 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2433 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
2436 /* if it is type-2 route and not a mac+ip route skip this route */
2437 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
2438 && !(is_evpn_prefix_ipaddr_v4(evp
)
2439 || is_evpn_prefix_ipaddr_v6(evp
)))
2442 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
2446 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, ri
);
2448 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
2452 zlog_err("%u: Failed to %s prefix %s in VRF %s",
2454 install
? "install" : "uninstall",
2455 prefix2str(evp
, buf
, sizeof(buf
)),
2456 vrf_id_to_name(bgp_vrf
->vrf_id
));
2465 * Install or uninstall route in matching VNIs (list).
2467 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
2468 safi_t safi
, struct prefix_evpn
*evp
,
2469 struct bgp_info
*ri
,
2470 struct list
*vnis
, int install
)
2472 struct bgpevpn
*vpn
;
2473 struct listnode
*node
, *nnode
;
2475 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
2478 if (!is_vni_live(vpn
))
2482 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, ri
);
2484 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, ri
);
2487 zlog_err("%u: Failed to %s EVPN %s route in VNI %u",
2488 bgp
->vrf_id
, install
? "install" : "uninstall",
2489 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2501 * Install or uninstall route for appropriate VNIs.
2503 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
2504 struct prefix
*p
, struct bgp_info
*ri
,
2507 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
2508 struct attr
*attr
= ri
->attr
;
2509 struct ecommunity
*ecom
;
2514 /* Only type-2 and type-3 and type-5 are supported currently */
2515 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2516 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
2517 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
2520 /* If we don't have Route Target, nothing much to do. */
2521 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2524 ecom
= attr
->ecommunity
;
2525 if (!ecom
|| !ecom
->size
)
2528 /* For each extended community RT, see which VNIs/VRFs match and import
2529 * the route into matching VNIs/VRFs.
2531 for (i
= 0; i
< ecom
->size
; i
++) {
2533 uint8_t type
, sub_type
;
2534 struct ecommunity_val
*eval
;
2535 struct ecommunity_val eval_tmp
;
2536 struct irt_node
*irt
; /* import rt for l2vni */
2537 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
2539 /* Only deal with RTs */
2540 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
2541 eval
= (struct ecommunity_val
*)(ecom
->val
2542 + (i
* ECOMMUNITY_SIZE
));
2545 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2548 /* Import route into matching l2-vnis (type-2/type-3 routes go
2551 irt
= lookup_import_rt(bgp
, eval
);
2553 install_uninstall_route_in_vnis(bgp
, afi
, safi
, evp
, ri
,
2556 /* Import route into matching l3-vnis (type-2/type-5 routes go
2557 * into l3vni/vrf table)
2559 vrf_irt
= lookup_vrf_import_rt(eval
);
2561 install_uninstall_route_in_vrfs(bgp
, afi
, safi
, evp
, ri
,
2562 vrf_irt
->vrfs
, import
);
2564 /* Also check for non-exact match. In this,
2565 * we mask out the AS and
2566 * only check on the local-admin sub-field.
2567 * This is to facilitate using
2568 * VNI as the RT for EBGP peering too.
2572 if (type
== ECOMMUNITY_ENCODE_AS
2573 || type
== ECOMMUNITY_ENCODE_AS4
2574 || type
== ECOMMUNITY_ENCODE_IP
) {
2575 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
2576 mask_ecom_global_admin(&eval_tmp
, eval
);
2577 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2578 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
2581 install_uninstall_route_in_vnis(bgp
, afi
, safi
, evp
, ri
,
2584 install_uninstall_route_in_vrfs(bgp
, afi
, safi
, evp
, ri
,
2585 vrf_irt
->vrfs
, import
);
2591 /* delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
2593 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
2595 /* delete all ipv4 routes and withdraw from peers */
2596 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
2597 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
2599 /* delete all ipv6 routes and withdraw from peers */
2600 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
2601 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
2604 /* update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
2606 static void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
2608 /* update all ipv4 routes */
2609 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
2610 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
2612 /* update all ipv6 routes */
2613 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
2614 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
2618 * update and advertise local routes for a VRF as type-5 routes.
2619 * This is invoked upon RD change for a VRF. Note taht the processing is only
2620 * done in the global route table using the routes which already exist in the
2623 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
2625 /* skip if the RD is configured */
2626 if (is_vrf_rd_configured(bgp_vrf
))
2629 /* derive the RD for the VRF based on new router-id */
2630 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
2632 /* update advertise ipv4|ipv6 routes as type-5 routes */
2633 update_advertise_vrf_routes(bgp_vrf
);
2637 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
2638 * This is invoked upon VRF RD change. The processing is done only from global
2641 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
2643 /* skip if the RD is configured */
2644 if (is_vrf_rd_configured(bgp_vrf
))
2647 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
2648 delete_withdraw_vrf_routes(bgp_vrf
);
2652 * Update and advertise local routes for a VNI. Invoked upon router-id
2653 * change. Note that the processing is done only on the global route table
2654 * using routes that already exist in the per-VNI table.
2656 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2658 struct prefix_evpn p
;
2659 struct bgp_node
*rn
, *global_rn
;
2660 struct bgp_info
*ri
, *global_ri
;
2662 afi_t afi
= AFI_L2VPN
;
2663 safi_t safi
= SAFI_EVPN
;
2665 /* Locate type-3 route for VNI in the per-VNI table and use its
2666 * attributes to create and advertise the type-3 route for this VNI
2667 * in the global table.
2669 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2670 rn
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
2671 if (!rn
) /* unexpected */
2673 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2674 if (ri
->peer
== bgp
->peer_self
&& ri
->type
== ZEBRA_ROUTE_BGP
2675 && ri
->sub_type
== BGP_ROUTE_STATIC
)
2677 if (!ri
) /* unexpected */
2681 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2682 (struct prefix
*)&p
, &vpn
->prd
);
2683 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1, 0, &ri
,
2686 /* Schedule for processing and unlock node. */
2687 bgp_process(bgp
, global_rn
, afi
, safi
);
2688 bgp_unlock_node(global_rn
);
2690 /* Now, walk this VNI's route table and use the route and its attribute
2691 * to create and schedule route in global table.
2693 for (rn
= bgp_table_top(vpn
->route_table
); rn
;
2694 rn
= bgp_route_next(rn
)) {
2695 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
2697 /* Identify MAC-IP local routes. */
2698 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2701 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
2702 if (ri
->peer
== bgp
->peer_self
2703 && ri
->type
== ZEBRA_ROUTE_BGP
2704 && ri
->sub_type
== BGP_ROUTE_STATIC
)
2709 /* Create route in global routing table using this route entry's
2713 global_rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
2714 (struct prefix
*)evp
, &vpn
->prd
);
2716 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, attr
, 1,
2719 /* Schedule for processing and unlock node. */
2720 bgp_process(bgp
, global_rn
, afi
, safi
);
2721 bgp_unlock_node(global_rn
);
2728 * Delete (and withdraw) local routes for a VNI - only from the global
2729 * table. Invoked upon router-id change.
2731 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2734 struct prefix_evpn p
;
2735 struct bgp_node
*global_rn
;
2736 struct bgp_info
*ri
;
2737 afi_t afi
= AFI_L2VPN
;
2738 safi_t safi
= SAFI_EVPN
;
2740 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2741 * for this VNI - from the global table.
2743 ret
= delete_global_type2_routes(bgp
, vpn
);
2747 /* Remove type-3 route for this VNI from global table. */
2748 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2749 global_rn
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
2750 (struct prefix
*)&p
, &vpn
->prd
);
2752 /* Delete route entry in the global EVPN table. */
2753 delete_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_rn
, &ri
);
2755 /* Schedule for processing - withdraws to peers happen from
2759 bgp_process(bgp
, global_rn
, afi
, safi
);
2760 bgp_unlock_node(global_rn
);
2767 * Handle router-id change. Update and advertise local routes corresponding
2768 * to this VNI from peers. Note that this is invoked after updating the
2769 * router-id. The routes in the per-VNI table are used to create routes in
2770 * the global table and schedule them.
2772 static void update_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
2774 struct bgpevpn
*vpn
;
2776 vpn
= (struct bgpevpn
*)backet
->data
;
2779 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__
);
2783 /* Skip VNIs with configured RD. */
2784 if (is_rd_configured(vpn
))
2787 bgp_evpn_derive_auto_rd(bgp
, vpn
);
2788 update_advertise_vni_routes(bgp
, vpn
);
2792 * Handle router-id change. Delete and withdraw local routes corresponding
2793 * to this VNI from peers. Note that this is invoked prior to updating
2794 * the router-id and is done only on the global route table, the routes
2795 * are needed in the per-VNI table to re-advertise with new router id.
2797 static void withdraw_router_id_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
2799 struct bgpevpn
*vpn
;
2801 vpn
= (struct bgpevpn
*)backet
->data
;
2804 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__
);
2808 /* Skip VNIs with configured RD. */
2809 if (is_rd_configured(vpn
))
2812 delete_withdraw_vni_routes(bgp
, vpn
);
2816 * Process received EVPN type-2 route (advertise or withdraw).
2818 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
2819 struct attr
*attr
, uint8_t *pfx
, int psize
,
2820 uint32_t addpath_id
)
2822 struct prefix_rd prd
;
2823 struct prefix_evpn p
;
2824 struct bgp_route_evpn evpn
;
2826 uint8_t macaddr_len
;
2827 mpls_label_t label
[BGP_MAX_LABELS
]; /* holds the VNI(s) as in packet */
2828 uint32_t num_labels
= 0;
2832 /* Type-2 route should be either 33, 37 or 49 bytes or an
2833 * additional 3 bytes if there is a second label (VNI):
2834 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
2835 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
2836 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
2838 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
2839 && psize
!= 40 && psize
!= 52) {
2840 zlog_err("%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
2841 peer
->bgp
->vrf_id
, peer
->host
, psize
);
2845 memset(&evpn
, 0, sizeof(evpn
));
2847 /* Make prefix_rd */
2848 prd
.family
= AF_UNSPEC
;
2850 memcpy(&prd
.val
, pfx
, 8);
2853 /* Make EVPN prefix. */
2854 memset(&p
, 0, sizeof(struct prefix_evpn
));
2856 p
.prefixlen
= EVPN_TYPE_2_ROUTE_PREFIXLEN
;
2857 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
2859 /* Copy Ethernet Seg Identifier */
2860 memcpy(&evpn
.eth_s_id
.val
, pfx
, ESI_LEN
);
2863 /* Copy Ethernet Tag */
2864 memcpy(ð_tag
, pfx
, 4);
2865 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
2868 /* Get the MAC Addr len */
2869 macaddr_len
= *pfx
++;
2871 /* Get the MAC Addr */
2872 if (macaddr_len
== (ETH_ALEN
* 8)) {
2873 memcpy(&p
.prefix
.macip_addr
.mac
.octet
, pfx
, ETH_ALEN
);
2877 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
2878 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
2884 ipaddr_len
= *pfx
++;
2885 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
2886 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
2888 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
2889 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
2894 ipaddr_len
/= 8; /* Convert to bytes. */
2895 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
2898 memcpy(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pfx
, ipaddr_len
);
2902 /* Get the VNI(s). Stored as bytes here. */
2904 memset(label
, 0, sizeof(label
));
2905 memcpy(&label
[0], pfx
, BGP_LABEL_BYTES
);
2906 pfx
+= BGP_LABEL_BYTES
;
2907 psize
-= (33 + ipaddr_len
);
2908 /* Do we have a second VNI? */
2911 memcpy(&label
[1], pfx
, BGP_LABEL_BYTES
);
2913 * If in future, we are required to access additional fields,
2914 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
2919 /* Process the route. */
2921 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2922 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2923 &prd
, &label
[0], num_labels
, 0, &evpn
);
2925 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2926 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2927 &prd
, &label
[0], num_labels
, &evpn
);
2932 * Process received EVPN type-3 route (advertise or withdraw).
2934 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
2935 struct attr
*attr
, uint8_t *pfx
, int psize
,
2936 uint32_t addpath_id
)
2938 struct prefix_rd prd
;
2939 struct prefix_evpn p
;
2944 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
2945 * IP len (1) and IP (4 or 16).
2947 if (psize
!= 17 && psize
!= 29) {
2948 zlog_err("%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
2949 peer
->bgp
->vrf_id
, peer
->host
, psize
);
2953 /* If PMSI is present, log if it is anything other than IR.
2954 * Note: We just simply ignore the values as it is not clear if
2955 * doing anything else is better.
2958 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
2959 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
) {
2960 zlog_warn("%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
2961 peer
->bgp
->vrf_id
, peer
->host
,
2962 attr
->pmsi_tnl_type
);
2966 /* Make prefix_rd */
2967 prd
.family
= AF_UNSPEC
;
2969 memcpy(&prd
.val
, pfx
, 8);
2972 /* Make EVPN prefix. */
2973 memset(&p
, 0, sizeof(struct prefix_evpn
));
2975 p
.prefixlen
= EVPN_TYPE_3_ROUTE_PREFIXLEN
;
2976 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
2978 /* Copy Ethernet Tag */
2979 memcpy(ð_tag
, pfx
, 4);
2980 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
2984 ipaddr_len
= *pfx
++;
2985 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
2986 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
2987 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
2990 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
2991 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
2995 /* Process the route. */
2997 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
2998 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
2999 &prd
, NULL
, 0, 0, NULL
);
3001 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3002 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3003 &prd
, NULL
, 0, NULL
);
3008 * Process received EVPN type-5 route (advertise or withdraw).
3010 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3011 struct attr
*attr
, uint8_t *pfx
, int psize
,
3012 uint32_t addpath_id
, int withdraw
)
3014 struct prefix_rd prd
;
3015 struct prefix_evpn p
;
3016 struct bgp_route_evpn evpn
;
3019 mpls_label_t label
; /* holds the VNI as in the packet */
3022 /* Type-5 route should be 34 or 58 bytes:
3023 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3024 * GW (4 or 16) and VNI (3).
3025 * Note that the IP and GW should both be IPv4 or both IPv6.
3027 if (psize
!= 34 && psize
!= 58) {
3028 zlog_err("%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3029 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3033 /* Make prefix_rd */
3034 prd
.family
= AF_UNSPEC
;
3036 memcpy(&prd
.val
, pfx
, 8);
3039 /* Make EVPN prefix. */
3040 memset(&p
, 0, sizeof(struct prefix_evpn
));
3042 p
.prefixlen
= EVPN_TYPE_5_ROUTE_PREFIXLEN
;
3043 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
3045 /* Additional information outside of prefix - ESI and GW IP */
3046 memset(&evpn
, 0, sizeof(evpn
));
3049 memcpy(&evpn
.eth_s_id
.val
, pfx
, 10);
3052 /* Fetch Ethernet Tag. */
3053 memcpy(ð_tag
, pfx
, 4);
3054 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
3057 /* Fetch IP prefix length. */
3059 if (ippfx_len
> IPV6_MAX_BITLEN
) {
3061 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3062 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
3065 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
3067 /* Determine IPv4 or IPv6 prefix */
3068 /* Since the address and GW are from the same family, this just becomes
3069 * a simple check on the total size.
3072 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
3073 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
3075 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
3078 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
3079 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
3081 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
3085 /* Get the VNI (in MPLS label field). Stored as bytes here. */
3086 memset(&label
, 0, sizeof(label
));
3087 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
3090 * If in future, we are required to access additional fields,
3091 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
3095 /* Process the route. */
3097 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3098 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3099 &prd
, &label
, 1, 0, &evpn
);
3101 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3102 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3103 &prd
, &label
, 1, &evpn
);
3108 static void evpn_mpattr_encode_type5(struct stream
*s
, struct prefix
*p
,
3109 struct prefix_rd
*prd
, mpls_label_t
*label
,
3110 uint32_t num_labels
, struct attr
*attr
)
3114 struct evpn_addr
*p_evpn_p
;
3116 memset(&temp
, 0, 16);
3117 if (p
->family
!= AF_EVPN
)
3119 p_evpn_p
= &(p
->u
.prefix_evpn
);
3121 /* len denites the total len of IP and GW-IP in the route
3122 IP and GW-IP have to be both ipv4 or ipv6
3124 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3125 len
= 8; /* IP and GWIP are both ipv4 */
3127 len
= 32; /* IP and GWIP are both ipv6 */
3128 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
3129 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
3130 stream_put(s
, prd
->val
, 8);
3132 stream_put(s
, &(attr
->evpn_overlay
.eth_s_id
), 10);
3134 stream_put(s
, &temp
, 10);
3135 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
3136 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
3137 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3138 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
3140 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
3142 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3144 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
3146 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
3148 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3149 stream_put_ipv4(s
, 0);
3151 stream_put(s
, &temp
, 16);
3155 stream_put(s
, label
, 3);
3161 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
3163 static void cleanup_vni_on_disable(struct hash_backet
*backet
, struct bgp
*bgp
)
3165 struct bgpevpn
*vpn
= (struct bgpevpn
*)backet
->data
;
3167 /* Remove EVPN routes and schedule for processing. */
3168 delete_routes_for_vni(bgp
, vpn
);
3170 /* Clear "live" flag and see if hash needs to be freed. */
3171 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
3172 if (!is_vni_configured(vpn
))
3173 bgp_evpn_free(bgp
, vpn
);
3177 * Free a VNI entry; iterator function called during cleanup.
3179 static void free_vni_entry(struct hash_backet
*backet
, struct bgp
*bgp
)
3181 struct bgpevpn
*vpn
;
3183 vpn
= (struct bgpevpn
*)backet
->data
;
3184 delete_all_vni_routes(bgp
, vpn
);
3185 bgp_evpn_free(bgp
, vpn
);
3189 * Derive AUTO import RT for BGP VRF - L3VNI
3191 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
3193 struct bgp
*bgp_def
= NULL
;
3195 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
3196 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
3199 bgp_def
= bgp_get_default();
3202 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
3206 * Delete AUTO import RT from BGP VRF - L3VNI
3208 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
3210 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
3214 * Derive AUTO export RT for BGP VRF - L3VNI
3216 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
3218 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
3219 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
3223 * Delete AUTO export RT from BGP VRF - L3VNI
3225 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
3227 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
3230 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
3232 struct bgp
*bgp_def
= NULL
;
3233 struct listnode
*node
= NULL
;
3234 struct bgpevpn
*vpn
= NULL
;
3236 bgp_def
= bgp_get_default();
3240 /* update all type-5 routes */
3241 update_advertise_vrf_routes(bgp_vrf
);
3243 /* update all type-2 routes */
3244 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
3245 update_routes_for_vni(bgp_def
, vpn
);
3249 * Handle autort change for a given VNI.
3251 static void update_autort_vni(struct hash_backet
*backet
, struct bgp
*bgp
)
3253 struct bgpevpn
*vpn
= backet
->data
;
3256 zlog_warn("%s: VNI hash entry for VNI not found", __PRETTY_FUNCTION__
);
3260 if (!is_import_rt_configured(vpn
)) {
3261 if (is_vni_live(vpn
))
3262 bgp_evpn_uninstall_routes(bgp
, vpn
);
3263 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
3264 list_delete_all_node(vpn
->import_rtl
);
3265 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
3266 if (is_vni_live(vpn
))
3267 bgp_evpn_install_routes(bgp
, vpn
);
3269 if (!is_export_rt_configured(vpn
)) {
3270 list_delete_all_node(vpn
->export_rtl
);
3271 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
3272 if (is_vni_live(vpn
))
3273 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
3281 /* withdraw type-5 route corresponding to ip prefix */
3282 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
3283 afi_t afi
, safi_t safi
)
3286 struct prefix_evpn evp
;
3287 char buf
[PREFIX_STRLEN
];
3289 build_type5_prefix_from_ip_prefix(&evp
, p
);
3290 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
3293 "%u failed to delete type-5 route for prefix %s in vrf %s",
3294 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
3295 vrf_id_to_name(bgp_vrf
->vrf_id
));
3299 /* withdraw all type-5 routes for an address family */
3300 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
3302 struct bgp_table
*table
= NULL
;
3303 struct bgp_node
*rn
= NULL
;
3304 struct bgp_info
*ri
;
3306 table
= bgp_vrf
->rib
[afi
][safi
];
3307 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3308 /* Only care about "selected" routes - non-imported. */
3309 /* TODO: Support for AddPath for EVPN. */
3310 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
3311 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
)
3312 && (!ri
->extra
|| !ri
->extra
->parent
)) {
3313 bgp_evpn_withdraw_type5_route(bgp_vrf
, &rn
->p
,
3322 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
3323 * to this function correspond to those of the source IP prefix (best
3324 * path in the case of the attr. In the case of a local prefix (when we
3325 * are advertising local subnets), the src_attr will be NULL.
3327 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, struct prefix
*p
,
3328 struct attr
*src_attr
, afi_t afi
,
3332 struct prefix_evpn evp
;
3333 char buf
[PREFIX_STRLEN
];
3335 build_type5_prefix_from_ip_prefix(&evp
, p
);
3336 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
3338 zlog_err("%u: Failed to create type-5 route for prefix %s",
3339 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
3342 /* Inject all prefixes of a particular address-family (currently, IPv4 or
3343 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
3344 * advertisement is enabled.
3346 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
3349 struct bgp_table
*table
= NULL
;
3350 struct bgp_node
*rn
= NULL
;
3351 struct bgp_info
*ri
;
3353 table
= bgp_vrf
->rib
[afi
][safi
];
3354 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
3355 /* Need to identify the "selected" route entry to use its
3356 * attribute. Also, we only consider "non-imported" routes.
3357 * TODO: Support for AddPath for EVPN.
3359 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
3360 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
)
3361 && (!ri
->extra
|| !ri
->extra
->parent
)) {
3363 /* apply the route-map */
3364 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
3367 ret
= route_map_apply(
3368 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
3370 &rn
->p
, RMAP_BGP
, ri
);
3371 if (ret
== RMAP_DENYMATCH
)
3374 bgp_evpn_advertise_type5_route(
3375 bgp_vrf
, &rn
->p
, ri
->attr
, afi
, safi
);
3382 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
3384 struct listnode
*node
, *nnode
, *node_to_del
;
3385 struct ecommunity
*ecom
, *ecom_auto
;
3386 struct ecommunity_val eval
;
3388 if (bgp
->advertise_autort_rfc8365
)
3389 vni
|= EVPN_AUTORT_VXLAN
;
3390 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
3392 ecom_auto
= ecommunity_new();
3393 ecommunity_add_val(ecom_auto
, &eval
);
3396 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
3397 if (ecommunity_match(ecom
, ecom_auto
)) {
3398 ecommunity_free(&ecom
);
3404 list_delete_node(rtl
, node_to_del
);
3406 ecommunity_free(&ecom_auto
);
3409 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
3410 struct ecommunity
*ecomadd
)
3412 /* uninstall routes from vrf */
3413 uninstall_routes_for_vrf(bgp_vrf
);
3415 /* Cleanup the RT to VRF mapping */
3416 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
3418 /* Remove auto generated RT */
3419 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
3421 /* Add the newly configured RT to RT list */
3422 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
3423 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
3425 /* map VRF to its RTs */
3426 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
3428 /* install routes matching the new VRF */
3429 install_routes_for_vrf(bgp_vrf
);
3432 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
3433 struct ecommunity
*ecomdel
)
3435 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
3436 struct ecommunity
*ecom
= NULL
;
3438 /* uninstall routes from vrf */
3439 uninstall_routes_for_vrf(bgp_vrf
);
3441 /* Cleanup the RT to VRF mapping */
3442 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
3444 /* remove the RT from the RT list */
3445 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
3446 if (ecommunity_match(ecom
, ecomdel
)) {
3447 ecommunity_free(&ecom
);
3454 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
3456 /* fallback to auto import rt, if this was the last RT */
3457 if (list_isempty(bgp_vrf
->vrf_import_rtl
)) {
3458 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
3459 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
3462 /* map VRFs to its RTs */
3463 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
3465 /* install routes matching this new RT */
3466 install_routes_for_vrf(bgp_vrf
);
3469 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
3470 struct ecommunity
*ecomadd
)
3472 /* remove auto-generated RT */
3473 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
3475 /* Add the new RT to the RT list */
3476 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
3477 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
3479 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
3482 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
3483 struct ecommunity
*ecomdel
)
3485 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
3486 struct ecommunity
*ecom
= NULL
;
3488 /* Remove the RT from the RT list */
3489 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
3490 if (ecommunity_match(ecom
, ecomdel
)) {
3491 ecommunity_free(&ecom
);
3498 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
3500 /* fall back to auto-generated RT if this was the last RT */
3501 if (bgp_vrf
->vrf_export_rtl
&& list_isempty(bgp_vrf
->vrf_export_rtl
)) {
3502 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
3503 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
3506 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
3510 * Handle change to BGP router id. This is invoked twice by the change
3511 * handler, first before the router id has been changed and then after
3512 * the router id has been changed. The first invocation will result in
3513 * local routes for all VNIs/VRF being deleted and withdrawn and the next
3514 * will result in the routes being re-advertised.
3516 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
3520 /* delete and withdraw all the type-5 routes
3521 stored in the global table for this vrf
3523 withdraw_router_id_vrf(bgp
);
3525 /* delete all the VNI routes (type-2/type-3) routes for all the
3528 hash_iterate(bgp
->vnihash
,
3529 (void (*)(struct hash_backet
*,
3530 void *))withdraw_router_id_vni
,
3534 /* advertise all routes in the vrf as type-5 routes with the new
3537 update_router_id_vrf(bgp
);
3539 /* advertise all the VNI routes (type-2/type-3) routes with the
3542 hash_iterate(bgp
->vnihash
,
3543 (void (*)(struct hash_backet
*,
3544 void *))update_router_id_vni
,
3550 * Handle change to auto-RT algorithm - update and advertise local routes.
3552 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
3554 hash_iterate(bgp
->vnihash
,
3555 (void (*)(struct hash_backet
*,
3556 void*))update_autort_vni
,
3561 * Handle change to export RT - update and advertise local routes.
3563 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3565 return update_routes_for_vni(bgp
, vpn
);
3568 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
3571 delete_withdraw_vrf_routes(bgp_vrf
);
3573 update_advertise_vrf_routes(bgp_vrf
);
3577 * Handle change to RD. This is invoked twice by the change handler,
3578 * first before the RD has been changed and then after the RD has
3579 * been changed. The first invocation will result in local routes
3580 * of this VNI being deleted and withdrawn and the next will result
3581 * in the routes being re-advertised.
3583 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
3587 delete_withdraw_vni_routes(bgp
, vpn
);
3589 update_advertise_vni_routes(bgp
, vpn
);
3593 * Install routes for this VNI. Invoked upon change to Import RT.
3595 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3597 return install_routes_for_vni(bgp
, vpn
);
3601 * Uninstall all routes installed for this VNI. Invoked upon change
3604 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3606 return uninstall_routes_for_vni(bgp
, vpn
);
3610 * TODO: Hardcoded for a maximum of 2 VNIs right now
3612 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
3617 vni1
= label2vni(label
);
3618 if (num_labels
== 2) {
3619 vni2
= label2vni(label
+ 1);
3620 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
3622 snprintf(buf
, len
, "%u", vni1
);
3627 * Function to convert evpn route to json format.
3628 * NOTE: We don't use prefix2str as the output here is a bit different.
3630 void bgp_evpn_route2json(struct prefix_evpn
*p
, json_object
*json
)
3632 char buf1
[ETHER_ADDR_STRLEN
];
3633 char buf2
[PREFIX2STR_BUFFER
];
3638 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
3639 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
3640 json_object_int_add(json
, "ethTag",
3641 p
->prefix
.imet_addr
.eth_tag
);
3642 json_object_int_add(json
, "ipLen",
3643 is_evpn_prefix_ipaddr_v4(p
)
3646 json_object_string_add(json
, "ip",
3647 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
3648 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3649 if (is_evpn_prefix_ipaddr_none(p
)) {
3650 json_object_int_add(json
, "routeType",
3651 p
->prefix
.route_type
);
3652 json_object_int_add(json
, "ethTag",
3653 p
->prefix
.macip_addr
.eth_tag
);
3654 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
3655 json_object_string_add(json
, "mac",
3656 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
3662 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
3665 json_object_int_add(json
, "routeType",
3666 p
->prefix
.route_type
);
3667 json_object_int_add(json
, "ethTag",
3668 p
->prefix
.macip_addr
.eth_tag
);
3669 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
3670 json_object_string_add(json
, "mac",
3671 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
3674 json_object_int_add(json
, "ipLen",
3675 is_evpn_prefix_ipaddr_v4(p
)
3678 json_object_string_add(
3681 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
3683 PREFIX2STR_BUFFER
));
3686 /* Currently, this is to cater to other AF_ETHERNET code. */
3691 * Function to convert evpn route to string.
3692 * NOTE: We don't use prefix2str as the output here is a bit different.
3694 char *bgp_evpn_route2str(struct prefix_evpn
*p
, char *buf
, int len
)
3696 char buf1
[ETHER_ADDR_STRLEN
];
3697 char buf2
[PREFIX2STR_BUFFER
];
3699 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
3700 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]", p
->prefix
.route_type
,
3701 p
->prefix
.imet_addr
.eth_tag
,
3702 is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BITLEN
3704 inet_ntoa(p
->prefix
.imet_addr
.ip
.ipaddr_v4
));
3705 } else if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3706 if (is_evpn_prefix_ipaddr_none(p
))
3707 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
3708 p
->prefix
.route_type
,
3709 p
->prefix
.macip_addr
.eth_tag
,
3711 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
3716 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
3718 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
3719 p
->prefix
.route_type
,
3720 p
->prefix
.macip_addr
.eth_tag
,
3722 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
3724 family
== AF_INET
? IPV4_MAX_BITLEN
3727 &p
->prefix
.macip_addr
.ip
.ip
.addr
,
3729 PREFIX2STR_BUFFER
));
3731 } else if (p
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3732 snprintf(buf
, len
, "[%d]:[%d]:[%d]:[%s]",
3733 p
->prefix
.route_type
,
3734 p
->prefix
.prefix_addr
.eth_tag
,
3735 p
->prefix
.prefix_addr
.ip_prefix_length
,
3736 is_evpn_prefix_ipaddr_v4(p
)
3737 ? inet_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v4
)
3738 : inet6_ntoa(p
->prefix
.prefix_addr
.ip
.ipaddr_v6
));
3740 /* For EVPN route types not supported yet. */
3741 snprintf(buf
, len
, "(unsupported route type %d)",
3742 p
->prefix
.route_type
);
3749 * Encode EVPN prefix in Update (MP_REACH)
3751 void bgp_evpn_encode_prefix(struct stream
*s
, struct prefix
*p
,
3752 struct prefix_rd
*prd
, mpls_label_t
*label
,
3753 uint32_t num_labels
, struct attr
*attr
,
3754 int addpath_encode
, uint32_t addpath_tx_id
)
3756 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3757 int len
, ipa_len
= 0;
3760 stream_putl(s
, addpath_tx_id
);
3763 stream_putc(s
, evp
->prefix
.route_type
);
3765 switch (evp
->prefix
.route_type
) {
3766 case BGP_EVPN_MAC_IP_ROUTE
:
3767 if (is_evpn_prefix_ipaddr_v4(evp
))
3768 ipa_len
= IPV4_MAX_BYTELEN
;
3769 else if (is_evpn_prefix_ipaddr_v6(evp
))
3770 ipa_len
= IPV6_MAX_BYTELEN
;
3771 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
3772 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
3773 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
3775 stream_putc(s
, len
);
3776 stream_put(s
, prd
->val
, 8); /* RD */
3778 stream_put(s
, &attr
->evpn_overlay
.eth_s_id
, ESI_LEN
);
3780 stream_put(s
, 0, 10);
3781 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
3782 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
3783 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
3784 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
3785 if (ipa_len
) /* IP */
3786 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
3788 /* 1st label is the L2 VNI */
3789 stream_put(s
, label
, BGP_LABEL_BYTES
);
3790 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
3791 if (ipa_len
&& num_labels
> 1)
3792 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
3795 case BGP_EVPN_IMET_ROUTE
:
3796 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
3797 stream_put(s
, prd
->val
, 8); /* RD */
3798 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
3799 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
3800 /* Originating Router's IP Addr */
3801 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
3804 case BGP_EVPN_IP_PREFIX_ROUTE
:
3805 /* TODO: AddPath support. */
3806 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
3814 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
3815 struct bgp_nlri
*packet
, int withdraw
)
3821 uint32_t addpath_id
;
3822 int addpath_encoded
;
3828 /* Start processing the NLRI - there may be multiple in the MP_REACH */
3830 lim
= pnt
+ packet
->length
;
3832 safi
= packet
->safi
;
3836 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
3837 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
3838 PEER_CAP_ADDPATH_AF_TX_RCV
));
3840 for (; pnt
< lim
; pnt
+= psize
) {
3841 /* Clear prefix structure. */
3842 memset(&p
, 0, sizeof(struct prefix
));
3844 /* Deal with path-id if AddPath is supported. */
3845 if (addpath_encoded
) {
3846 /* When packet overflow occurs return immediately. */
3847 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
3850 addpath_id
= ntohl(*((uint32_t *)pnt
));
3851 pnt
+= BGP_ADDPATH_ID_LEN
;
3854 /* All EVPN NLRI types start with type and length. */
3859 psize
= rlen
= *pnt
++;
3861 /* When packet overflow occur return immediately. */
3862 if (pnt
+ psize
> lim
)
3866 case BGP_EVPN_MAC_IP_ROUTE
:
3867 if (process_type2_route(peer
, afi
, safi
,
3868 withdraw
? NULL
: attr
, pnt
,
3869 psize
, addpath_id
)) {
3871 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
3872 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3877 case BGP_EVPN_IMET_ROUTE
:
3878 if (process_type3_route(peer
, afi
, safi
,
3879 withdraw
? NULL
: attr
, pnt
,
3880 psize
, addpath_id
)) {
3882 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
3883 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3888 case BGP_EVPN_IP_PREFIX_ROUTE
:
3889 if (process_type5_route(peer
, afi
, safi
, attr
, pnt
,
3890 psize
, addpath_id
, withdraw
)) {
3892 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
3893 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3903 /* Packet length consistency check. */
3911 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
3912 * The mapping will be used during route processing.
3913 * bgp_def: default bgp instance
3914 * bgp_vrf: specific bgp vrf instance on which RT is configured
3916 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
3919 struct ecommunity_val
*eval
= NULL
;
3920 struct listnode
*node
= NULL
, *nnode
= NULL
;
3921 struct ecommunity
*ecom
= NULL
;
3923 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
3924 for (i
= 0; i
< ecom
->size
; i
++) {
3925 eval
= (struct ecommunity_val
*)(ecom
->val
3927 * ECOMMUNITY_SIZE
));
3928 map_vrf_to_rt(bgp_vrf
, eval
);
3934 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
3936 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
3939 struct ecommunity_val
*eval
;
3940 struct listnode
*node
, *nnode
;
3941 struct ecommunity
*ecom
;
3943 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
3944 for (i
= 0; i
< ecom
->size
; i
++) {
3945 struct vrf_irt_node
*irt
;
3946 struct ecommunity_val eval_tmp
;
3948 eval
= (struct ecommunity_val
*)(ecom
->val
3950 * ECOMMUNITY_SIZE
));
3951 /* If using "automatic" RT, we only care about the
3952 * local-admin sub-field.
3953 * This is to facilitate using VNI as the RT for EBGP
3956 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
3957 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
3958 BGP_VRF_IMPORT_RT_CFGD
))
3959 mask_ecom_global_admin(&eval_tmp
, eval
);
3961 irt
= lookup_vrf_import_rt(&eval_tmp
);
3963 unmap_vrf_from_rt(bgp_vrf
, irt
);
3970 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
3971 * The mapping will be used during route processing.
3973 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3976 struct ecommunity_val
*eval
;
3977 struct listnode
*node
, *nnode
;
3978 struct ecommunity
*ecom
;
3980 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
3981 for (i
= 0; i
< ecom
->size
; i
++) {
3982 eval
= (struct ecommunity_val
*)(ecom
->val
3984 * ECOMMUNITY_SIZE
));
3985 map_vni_to_rt(bgp
, vpn
, eval
);
3991 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
3993 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3996 struct ecommunity_val
*eval
;
3997 struct listnode
*node
, *nnode
;
3998 struct ecommunity
*ecom
;
4000 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4001 for (i
= 0; i
< ecom
->size
; i
++) {
4002 struct irt_node
*irt
;
4003 struct ecommunity_val eval_tmp
;
4005 eval
= (struct ecommunity_val
*)(ecom
->val
4007 * ECOMMUNITY_SIZE
));
4008 /* If using "automatic" RT, we only care about the
4009 * local-admin sub-field.
4010 * This is to facilitate using VNI as the RT for EBGP
4013 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4014 if (!is_import_rt_configured(vpn
))
4015 mask_ecom_global_admin(&eval_tmp
, eval
);
4017 irt
= lookup_import_rt(bgp
, &eval_tmp
);
4019 unmap_vni_from_rt(bgp
, vpn
, irt
);
4025 * Derive Import RT automatically for VNI and map VNI to RT.
4026 * The mapping will be used during route processing.
4028 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4030 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
4031 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
4034 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
4038 * Derive Export RT automatically for VNI.
4040 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4042 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
4043 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
4047 * Derive RD automatically for VNI using passed information - it
4048 * is of the form RouterId:unique-id-for-vni.
4050 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
4052 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
4056 * Derive RD automatically for VNI using passed information - it
4057 * is of the form RouterId:unique-id-for-vni.
4059 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4063 vpn
->prd
.family
= AF_UNSPEC
;
4064 vpn
->prd
.prefixlen
= 64;
4065 sprintf(buf
, "%s:%hu", inet_ntoa(bgp
->router_id
), vpn
->rd_id
);
4066 (void)str2prefix_rd(buf
, &vpn
->prd
);
4067 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
4073 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
4075 struct bgpevpn
*vpn
;
4078 memset(&tmp
, 0, sizeof(struct bgpevpn
));
4080 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
4085 * Create a new vpn - invoked upon configuration or zebra notification.
4087 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
4088 struct in_addr originator_ip
,
4089 vrf_id_t tenant_vrf_id
)
4091 struct bgpevpn
*vpn
;
4096 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
4100 /* Set values - RD and RT set to defaults. */
4102 vpn
->originator_ip
= originator_ip
;
4103 vpn
->tenant_vrf_id
= tenant_vrf_id
;
4105 /* Initialize route-target import and export lists */
4106 vpn
->import_rtl
= list_new();
4107 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4108 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
4109 vpn
->export_rtl
= list_new();
4110 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
4111 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
4112 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
4113 derive_rd_rt_for_vni(bgp
, vpn
);
4115 /* Initialize EVPN route table. */
4116 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
4119 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
4120 XFREE(MTYPE_BGP_EVPN
, vpn
);
4124 /* add to l2vni list on corresponding vrf */
4125 bgpevpn_link_to_l3vni(vpn
);
4127 QOBJ_REG(vpn
, bgpevpn
);
4132 * Free a given VPN - called in multiple scenarios such as zebra
4133 * notification, configuration being deleted, advertise-all-vni disabled etc.
4134 * This just frees appropriate memory, caller should have taken other
4137 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4139 bgpevpn_unlink_from_l3vni(vpn
);
4140 bgp_table_unlock(vpn
->route_table
);
4141 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4142 list_delete_and_null(&vpn
->import_rtl
);
4143 list_delete_and_null(&vpn
->export_rtl
);
4144 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
4145 hash_release(bgp
->vnihash
, vpn
);
4147 XFREE(MTYPE_BGP_EVPN
, vpn
);
4151 * Import route into matching VNI(s).
4153 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
4154 struct prefix
*p
, struct bgp_info
*ri
)
4156 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 1);
4160 * Unimport route from matching VNI(s).
4162 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
4163 struct prefix
*p
, struct bgp_info
*ri
)
4165 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, ri
, 0);
4168 /* filter routes which have martian next hops */
4169 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
4173 struct bgp_node
*rd_rn
, *rn
;
4174 struct bgp_table
*table
;
4175 struct bgp_info
*ri
;
4180 /* Walk entire global routing table and evaluate routes which could be
4181 * imported into this VPN. Note that we cannot just look at the routes
4182 * for the VNI's RD -
4183 * remote routes applicable for this VNI could have any RD.
4185 /* EVPN routes are a 2-level table. */
4186 for (rd_rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_rn
;
4187 rd_rn
= bgp_route_next(rd_rn
)) {
4188 table
= (struct bgp_table
*)(rd_rn
->info
);
4192 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4194 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
4196 /* Consider "valid" remote routes applicable for
4198 if (!(ri
->type
== ZEBRA_ROUTE_BGP
4199 && ri
->sub_type
== BGP_ROUTE_NORMAL
))
4202 if (bgp_nexthop_self(bgp
, ri
->attr
->nexthop
)) {
4204 char attr_str
[BUFSIZ
];
4205 char pbuf
[PREFIX_STRLEN
];
4207 bgp_dump_attr(ri
->attr
, attr_str
,
4210 if (bgp_debug_update(ri
->peer
, &rn
->p
,
4213 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
4220 bgp_evpn_unimport_route(bgp
, afi
, safi
,
4223 bgp_rib_remove(rn
, ri
, ri
->peer
, afi
,
4234 * Handle del of a local MACIP.
4236 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
4239 struct bgpevpn
*vpn
;
4240 struct prefix_evpn p
;
4242 if (!bgp
->vnihash
) {
4243 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4247 /* Lookup VNI hash - should exist. */
4248 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4249 if (!vpn
|| !is_vni_live(vpn
)) {
4250 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP DEL",
4251 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
4255 /* Remove EVPN type-2 route and schedule for processing. */
4256 build_evpn_type2_prefix(&p
, mac
, ip
);
4257 delete_evpn_route(bgp
, vpn
, &p
);
4263 * Handle add of a local MACIP.
4265 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
4266 struct ipaddr
*ip
, uint8_t flags
)
4268 struct bgpevpn
*vpn
;
4269 struct prefix_evpn p
;
4271 if (!bgp
->vnihash
) {
4272 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4276 /* Lookup VNI hash - should exist. */
4277 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4278 if (!vpn
|| !is_vni_live(vpn
)) {
4279 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP ADD",
4280 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
4284 /* Create EVPN type-2 route and schedule for processing. */
4285 build_evpn_type2_prefix(&p
, mac
, ip
);
4286 if (update_evpn_route(bgp
, vpn
, &p
, flags
)) {
4287 char buf
[ETHER_ADDR_STRLEN
];
4288 char buf2
[INET6_ADDRSTRLEN
];
4291 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
4292 bgp
->vrf_id
, vpn
->vni
,
4293 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
4296 prefix_mac2str(mac
, buf
, sizeof(buf
)),
4297 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
4304 static void link_l2vni_hash_to_l3vni(struct hash_backet
*backet
,
4305 struct bgp
*bgp_vrf
)
4307 struct bgpevpn
*vpn
= NULL
;
4308 struct bgp
*bgp_def
= NULL
;
4310 bgp_def
= bgp_get_default();
4313 vpn
= (struct bgpevpn
*)backet
->data
;
4314 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
4315 bgpevpn_link_to_l3vni(vpn
);
4318 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
, struct ethaddr
*rmac
,
4319 struct in_addr originator_ip
, int filter
)
4321 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
4322 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
4323 struct listnode
*node
= NULL
;
4324 struct bgpevpn
*vpn
= NULL
;
4327 /* get the default instamce - required to get the AS number for VRF
4330 bgp_def
= bgp_get_default();
4333 "Cannot process L3VNI %u ADD - default BGP instance not yet created",
4339 /* if the BGP vrf instance doesnt exist - create one */
4340 bgp_vrf
= bgp_lookup_by_name(vrf_id_to_name(vrf_id
));
4345 ret
= bgp_get(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
4346 BGP_INSTANCE_TYPE_VRF
);
4348 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
:
4349 zlog_err("'bgp multiple-instance' not present\n");
4351 case BGP_ERR_AS_MISMATCH
:
4352 zlog_err("BGP is already running; AS is %u\n", as
);
4354 case BGP_ERR_INSTANCE_MISMATCH
:
4355 zlog_err("BGP instance name and AS number mismatch\n");
4359 /* mark as auto created */
4360 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
4363 /* associate with l3vni */
4364 bgp_vrf
->l3vni
= l3vni
;
4366 /* set the router mac - to be used in mac-ip routes for this vrf */
4367 memcpy(&bgp_vrf
->rmac
, rmac
, sizeof(struct ethaddr
));
4369 /* set the originator ip */
4370 bgp_vrf
->originator_ip
= originator_ip
;
4372 /* set the right filter - are we using l3vni only for prefix routes? */
4374 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
4376 /* auto derive RD/RT */
4377 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
4378 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4379 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
4380 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4381 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
4383 /* link all corresponding l2vnis */
4384 hash_iterate(bgp_def
->vnihash
,
4385 (void (*)(struct hash_backet
*,
4386 void *))link_l2vni_hash_to_l3vni
,
4389 /* Only update all corresponding type-2 routes if we are advertising two
4390 * labels along with type-2 routes
4393 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4394 update_routes_for_vni(bgp_def
, vpn
);
4396 /* advertise type-5 routes if needed */
4397 update_advertise_vrf_routes(bgp_vrf
);
4399 /* install all remote routes belonging to this l3vni into correspondng
4401 install_routes_for_vrf(bgp_vrf
);
4406 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
4408 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
4409 struct bgp
*bgp_def
= NULL
; /* default bgp instance */
4410 struct listnode
*node
= NULL
;
4411 struct listnode
*next
= NULL
;
4412 struct bgpevpn
*vpn
= NULL
;
4414 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
4417 "Cannot process L3VNI %u Del - Could not find BGP instance",
4422 bgp_def
= bgp_get_default();
4425 "Cannot process L3VNI %u Del - Could not find default BGP instance",
4430 /* unimport remote routes from VRF, if it is AUTO vrf bgp_delete will
4431 * take care of uninstalling the routes from zebra
4433 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
4434 uninstall_routes_for_vrf(bgp_vrf
);
4436 /* delete/withdraw all type-5 routes */
4437 delete_withdraw_vrf_routes(bgp_vrf
);
4439 /* remove the l3vni from vrf instance */
4442 /* remove the Rmac from the BGP vrf */
4443 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
4446 if (bgp_vrf
->vrf_import_rtl
&& !list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4447 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4448 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
4450 if (bgp_vrf
->vrf_export_rtl
&& !list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4451 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
4454 /* update all corresponding local mac-ip routes */
4455 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
4456 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
4457 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
4458 update_routes_for_vni(bgp_def
, vpn
);
4462 /* If any L2VNIs point to this instance, unlink them. */
4463 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
4464 bgpevpn_unlink_from_l3vni(vpn
);
4466 /* Delete the instance if it was autocreated */
4467 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
4468 bgp_delete(bgp_vrf
);
4474 * Handle del of a local VNI.
4476 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
4478 struct bgpevpn
*vpn
;
4480 if (!bgp
->vnihash
) {
4481 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4485 /* Locate VNI hash */
4486 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4488 if (bgp_debug_zebra(NULL
))
4490 "%u: VNI hash entry for VNI %u not found at DEL",
4495 /* Remove all local EVPN routes and schedule for processing (to
4496 * withdraw from peers).
4498 delete_routes_for_vni(bgp
, vpn
);
4501 * tunnel is no longer active, del tunnel ip address from tip_hash
4503 bgp_tip_del(bgp
, &vpn
->originator_ip
);
4505 /* Clear "live" flag and see if hash needs to be freed. */
4506 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4507 if (!is_vni_configured(vpn
))
4508 bgp_evpn_free(bgp
, vpn
);
4514 * Handle add (or update) of a local VNI. The VNI changes we care
4515 * about are for the local-tunnel-ip and the (tenant) VRF.
4517 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
4518 struct in_addr originator_ip
, vrf_id_t tenant_vrf_id
)
4520 struct bgpevpn
*vpn
;
4521 struct prefix_evpn p
;
4523 if (!bgp
->vnihash
) {
4524 zlog_err("%u: VNI hash not created", bgp
->vrf_id
);
4528 /* Lookup VNI. If present and no change, exit. */
4529 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
4532 if (is_vni_live(vpn
)
4533 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
4534 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
4535 /* Probably some other param has changed that we don't
4539 /* Update tenant_vrf_id if it has changed. */
4540 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
4541 bgpevpn_unlink_from_l3vni(vpn
);
4542 vpn
->tenant_vrf_id
= tenant_vrf_id
;
4543 bgpevpn_link_to_l3vni(vpn
);
4546 /* If tunnel endpoint IP has changed, update (and delete prior
4547 * type-3 route, if needed.)
4549 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
4550 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
4552 /* Update all routes with new endpoint IP and/or export RT
4555 if (is_vni_live(vpn
))
4556 update_routes_for_vni(bgp
, vpn
);
4559 /* Create or update as appropriate. */
4561 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
);
4564 "%u: Failed to allocate VNI entry for VNI %u - at Add",
4570 /* if the VNI is live already, there is nothing more to do */
4571 if (is_vni_live(vpn
))
4574 /* Mark as "live" */
4575 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4577 /* tunnel is now active, add tunnel-ip to db */
4578 bgp_tip_add(bgp
, &originator_ip
);
4580 /* filter routes as nexthop database has changed */
4581 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
4583 /* Create EVPN type-3 route and schedule for processing. */
4584 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
4585 if (update_evpn_route(bgp
, vpn
, &p
, 0)) {
4586 zlog_err("%u: Type3 route creation failure for VNI %u",
4591 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
4595 install_routes_for_vni(bgp
, vpn
);
4597 /* If we are advertising gateway mac-ip
4598 It needs to be conveyed again to zebra */
4599 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
4605 * Cleanup EVPN information on disable - Need to delete and withdraw
4606 * EVPN routes from peers.
4608 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
4610 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
4611 void *))cleanup_vni_on_disable
,
4616 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
4617 * BGP instance (default) is being freed.
4619 void bgp_evpn_cleanup(struct bgp
*bgp
)
4622 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_backet
*,
4623 void *))free_vni_entry
,
4625 if (bgp
->import_rt_hash
)
4626 hash_free(bgp
->import_rt_hash
);
4627 bgp
->import_rt_hash
= NULL
;
4628 if (bgp
->vrf_import_rt_hash
)
4629 hash_free(bgp
->vrf_import_rt_hash
);
4630 bgp
->vrf_import_rt_hash
= NULL
;
4632 hash_free(bgp
->vnihash
);
4633 bgp
->vnihash
= NULL
;
4634 if (bgp
->vrf_import_rtl
)
4635 list_delete_and_null(&bgp
->vrf_import_rtl
);
4636 if (bgp
->vrf_export_rtl
)
4637 list_delete_and_null(&bgp
->vrf_export_rtl
);
4639 list_delete_and_null(&bgp
->l2vnis
);
4643 * Initialization for EVPN
4646 * hash for RT to VNI
4648 void bgp_evpn_init(struct bgp
*bgp
)
4651 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
4652 bgp
->import_rt_hash
=
4653 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
4654 "BGP Import RT Hash");
4655 bgp
->vrf_import_rt_hash
=
4656 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
4657 "BGP VRF Import RT Hash");
4658 bgp
->vrf_import_rtl
= list_new();
4659 bgp
->vrf_import_rtl
->cmp
=
4660 (int (*)(void *, void *))evpn_route_target_cmp
;
4661 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
4662 bgp
->vrf_export_rtl
= list_new();
4663 bgp
->vrf_export_rtl
->cmp
=
4664 (int (*)(void *, void *))evpn_route_target_cmp
;
4665 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
4666 bgp
->l2vnis
= list_new();
4667 bgp
->l2vnis
->cmp
= (int (*)(void *, void *))vni_hash_cmp
;
4670 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
4672 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);