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 "lib/printfrr.h"
36 #include "bgpd/bgp_attr_evpn.h"
37 #include "bgpd/bgpd.h"
38 #include "bgpd/bgp_table.h"
39 #include "bgpd/bgp_route.h"
40 #include "bgpd/bgp_attr.h"
41 #include "bgpd/bgp_mplsvpn.h"
42 #include "bgpd/bgp_label.h"
43 #include "bgpd/bgp_evpn.h"
44 #include "bgpd/bgp_evpn_private.h"
45 #include "bgpd/bgp_evpn_mh.h"
46 #include "bgpd/bgp_ecommunity.h"
47 #include "bgpd/bgp_encap_types.h"
48 #include "bgpd/bgp_debug.h"
49 #include "bgpd/bgp_errors.h"
50 #include "bgpd/bgp_aspath.h"
51 #include "bgpd/bgp_zebra.h"
52 #include "bgpd/bgp_nexthop.h"
53 #include "bgpd/bgp_addpath.h"
54 #include "bgpd/bgp_mac.h"
55 #include "bgpd/bgp_vty.h"
58 * Definitions and external declarations.
60 DEFINE_QOBJ_TYPE(bgpevpn
)
61 DEFINE_QOBJ_TYPE(bgp_evpn_es
)
65 * Static function declarations
67 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
68 static void bgp_evpn_update_type2_route_entry(struct bgp
*bgp
,
70 struct bgp_dest
*dest
,
71 struct bgp_path_info
*local_pi
,
73 static struct in_addr zero_vtep_ip
;
82 static unsigned int vni_hash_key_make(const void *p
)
84 const struct bgpevpn
*vpn
= p
;
85 return (jhash_1word(vpn
->vni
, 0));
89 * Comparison function for vni hash
91 static bool vni_hash_cmp(const void *p1
, const void *p2
)
93 const struct bgpevpn
*vpn1
= p1
;
94 const struct bgpevpn
*vpn2
= p2
;
100 return (vpn1
->vni
== vpn2
->vni
);
103 int vni_list_cmp(void *p1
, void *p2
)
105 const struct bgpevpn
*vpn1
= p1
;
106 const struct bgpevpn
*vpn2
= p2
;
108 return vpn1
->vni
- vpn2
->vni
;
112 * Make vrf import route target hash key.
114 static unsigned int vrf_import_rt_hash_key_make(const void *p
)
116 const struct vrf_irt_node
*irt
= p
;
117 const char *pnt
= irt
->rt
.val
;
119 return jhash(pnt
, 8, 0x5abc1234);
123 * Comparison function for vrf import rt hash
125 static bool vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
127 const struct vrf_irt_node
*irt1
= p1
;
128 const struct vrf_irt_node
*irt2
= p2
;
130 if (irt1
== NULL
&& irt2
== NULL
)
133 if (irt1
== NULL
|| irt2
== NULL
)
136 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
140 * Create a new vrf import_rt in evpn instance
142 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
144 struct bgp
*bgp_evpn
= NULL
;
145 struct vrf_irt_node
*irt
;
147 bgp_evpn
= bgp_get_evpn();
149 flog_err(EC_BGP_NO_DFLT
,
150 "vrf import rt new - evpn instance not created yet");
154 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
155 sizeof(struct vrf_irt_node
));
158 irt
->vrfs
= list_new();
161 if (!hash_get(bgp_evpn
->vrf_import_rt_hash
, irt
, hash_alloc_intern
)) {
162 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
170 * Free the vrf import rt node
172 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
174 struct bgp
*bgp_evpn
= NULL
;
176 bgp_evpn
= bgp_get_evpn();
178 flog_err(EC_BGP_NO_DFLT
,
179 "vrf import rt free - evpn instance not created yet");
183 hash_release(bgp_evpn
->vrf_import_rt_hash
, irt
);
184 list_delete(&irt
->vrfs
);
185 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
189 * Function to lookup Import RT node - used to map a RT to set of
190 * VNIs importing routes with that RT.
192 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
194 struct bgp
*bgp_evpn
= NULL
;
195 struct vrf_irt_node
*irt
;
196 struct vrf_irt_node tmp
;
198 bgp_evpn
= bgp_get_evpn();
202 "vrf import rt lookup - evpn instance not created yet");
206 memset(&tmp
, 0, sizeof(struct vrf_irt_node
));
207 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
208 irt
= hash_lookup(bgp_evpn
->vrf_import_rt_hash
, &tmp
);
213 * Is specified VRF present on the RT's list of "importing" VRFs?
215 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
, struct bgp
*bgp_vrf
)
217 struct listnode
*node
= NULL
, *nnode
= NULL
;
218 struct bgp
*tmp_bgp_vrf
= NULL
;
220 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
221 if (tmp_bgp_vrf
== bgp_vrf
)
228 * Make import route target hash key.
230 static unsigned int import_rt_hash_key_make(const void *p
)
232 const struct irt_node
*irt
= p
;
233 const char *pnt
= irt
->rt
.val
;
235 return jhash(pnt
, 8, 0xdeadbeef);
239 * Comparison function for import rt hash
241 static bool import_rt_hash_cmp(const void *p1
, const void *p2
)
243 const struct irt_node
*irt1
= p1
;
244 const struct irt_node
*irt2
= p2
;
246 if (irt1
== NULL
&& irt2
== NULL
)
249 if (irt1
== NULL
|| irt2
== NULL
)
252 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
256 * Create a new import_rt
258 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
259 struct ecommunity_val
*rt
)
261 struct irt_node
*irt
;
266 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
269 irt
->vnis
= list_new();
272 if (!hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
)) {
273 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
281 * Free the import rt node
283 static void import_rt_free(struct bgp
*bgp
, struct irt_node
*irt
)
285 hash_release(bgp
->import_rt_hash
, irt
);
286 list_delete(&irt
->vnis
);
287 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
291 * Function to lookup Import RT node - used to map a RT to set of
292 * VNIs importing routes with that RT.
294 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
295 struct ecommunity_val
*rt
)
297 struct irt_node
*irt
;
300 memset(&tmp
, 0, sizeof(struct irt_node
));
301 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
302 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
307 * Is specified VNI present on the RT's list of "importing" VNIs?
309 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
311 struct listnode
*node
, *nnode
;
312 struct bgpevpn
*tmp_vpn
;
314 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
323 * Compare Route Targets.
325 static int evpn_route_target_cmp(struct ecommunity
*ecom1
,
326 struct ecommunity
*ecom2
)
334 if (!ecom1
&& !ecom2
)
337 if (ecom1
->str
&& !ecom2
->str
)
340 if (!ecom1
->str
&& ecom2
->str
)
343 if (!ecom1
->str
&& !ecom2
->str
)
346 return strcmp(ecom1
->str
, ecom2
->str
);
349 static void evpn_xxport_delete_ecomm(void *val
)
351 struct ecommunity
*ecomm
= val
;
352 ecommunity_free(&ecomm
);
356 * Mask off global-admin field of specified extended community (RT),
357 * just retain the local-admin field.
359 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
360 struct ecommunity_val
*src
)
366 if (type
== ECOMMUNITY_ENCODE_AS
) {
367 dst
->val
[2] = dst
->val
[3] = 0;
368 } else if (type
== ECOMMUNITY_ENCODE_AS4
369 || type
== ECOMMUNITY_ENCODE_IP
) {
370 dst
->val
[2] = dst
->val
[3] = 0;
371 dst
->val
[4] = dst
->val
[5] = 0;
376 * Map one RT to specified VRF.
377 * bgp_vrf = BGP vrf instance
379 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct ecommunity_val
*eval
)
381 struct vrf_irt_node
*irt
= NULL
;
382 struct ecommunity_val eval_tmp
;
384 /* If using "automatic" RT,
385 * we only care about the local-admin sub-field.
386 * This is to facilitate using L3VNI(VRF-VNI)
387 * as the RT for EBGP peering too.
389 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
390 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
391 mask_ecom_global_admin(&eval_tmp
, eval
);
393 irt
= lookup_vrf_import_rt(&eval_tmp
);
394 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
395 /* Already mapped. */
399 irt
= vrf_import_rt_new(&eval_tmp
);
401 /* Add VRF to the list for this RT. */
402 listnode_add(irt
->vrfs
, bgp_vrf
);
406 * Unmap specified VRF from specified RT. If there are no other
407 * VRFs for this RT, then the RT hash is deleted.
408 * bgp_vrf: BGP VRF specific instance
410 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
, struct vrf_irt_node
*irt
)
412 /* Delete VRF from list for this RT. */
413 listnode_delete(irt
->vrfs
, bgp_vrf
);
414 if (!listnode_head(irt
->vrfs
)) {
415 vrf_import_rt_free(irt
);
420 * Map one RT to specified VNI.
422 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
423 struct ecommunity_val
*eval
)
425 struct irt_node
*irt
;
426 struct ecommunity_val eval_tmp
;
428 /* If using "automatic" RT, we only care about the local-admin
430 * This is to facilitate using VNI as the RT for EBGP peering too.
432 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
433 if (!is_import_rt_configured(vpn
))
434 mask_ecom_global_admin(&eval_tmp
, eval
);
436 irt
= lookup_import_rt(bgp
, &eval_tmp
);
438 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
439 /* Already mapped. */
443 irt
= import_rt_new(bgp
, &eval_tmp
);
447 /* Add VNI to the hash list for this RT. */
448 listnode_add(irt
->vnis
, vpn
);
452 * Unmap specified VNI from specified RT. If there are no other
453 * VNIs for this RT, then the RT hash is deleted.
455 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
456 struct irt_node
*irt
)
458 /* Delete VNI from hash list for this RT. */
459 listnode_delete(irt
->vnis
, vpn
);
460 if (!listnode_head(irt
->vnis
)) {
461 import_rt_free(bgp
, irt
);
465 static void bgp_evpn_get_rmac_nexthop(struct bgpevpn
*vpn
,
466 const struct prefix_evpn
*p
,
467 struct attr
*attr
, uint8_t flags
)
469 struct bgp
*bgp_vrf
= vpn
->bgp_vrf
;
471 memset(&attr
->rmac
, 0, sizeof(struct ethaddr
));
475 if (p
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
478 /* Copy sys (pip) RMAC and PIP IP as nexthop
479 * in case of route is self MAC-IP,
480 * advertise-pip and advertise-svi-ip features
482 * Otherwise, for all host MAC-IP route's
485 if (CHECK_FLAG(flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
)
486 && bgp_vrf
->evpn_info
->advertise_pip
&&
487 bgp_vrf
->evpn_info
->is_anycast_mac
) {
489 memcpy(&attr
->rmac
, &bgp_vrf
->evpn_info
->pip_rmac
,
491 attr
->nexthop
= bgp_vrf
->evpn_info
->pip_ip
;
492 attr
->mp_nexthop_global_in
=
493 bgp_vrf
->evpn_info
->pip_ip
;
495 memcpy(&attr
->rmac
, &bgp_vrf
->rmac
, ETH_ALEN
);
499 * Create RT extended community automatically from passed information:
500 * of the form AS:VNI.
501 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
502 * the need is to get a RT value that will be unique across different
503 * VNIs but the same across routers (in the same AS) for a particular
506 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
508 struct ecommunity_val eval
;
509 struct ecommunity
*ecomadd
, *ecom
;
510 bool ecom_found
= false;
511 struct listnode
*node
;
513 if (bgp
->advertise_autort_rfc8365
)
514 vni
|= EVPN_AUTORT_VXLAN
;
515 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
517 ecomadd
= ecommunity_new();
518 ecommunity_add_val(ecomadd
, &eval
, false, false);
519 for (ALL_LIST_ELEMENTS_RO(rtl
, node
, ecom
))
520 if (ecommunity_cmp(ecomadd
, ecom
))
524 listnode_add_sort(rtl
, ecomadd
);
526 ecommunity_free(&ecomadd
);
530 * Derive RD and RT for a VNI automatically. Invoked at the time of
533 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
535 bgp_evpn_derive_auto_rd(bgp
, vpn
);
536 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
537 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
541 * Convert nexthop (remote VTEP IP) into an IPv6 address.
543 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
545 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
547 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
548 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
551 struct bgp_dest
*bgp_global_evpn_node_get(struct bgp_table
*table
, afi_t afi
,
553 const struct prefix_evpn
*evp
,
554 struct prefix_rd
*prd
)
556 struct prefix_evpn global_p
;
558 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
) {
559 /* prefix in the global table doesn't include the VTEP-IP so
560 * we need to create a different copy of the prefix
562 evpn_type1_prefix_global_copy(&global_p
, evp
);
565 return bgp_afi_node_get(table
, afi
, safi
, (struct prefix
*)evp
, prd
);
568 struct bgp_dest
*bgp_global_evpn_node_lookup(struct bgp_table
*table
, afi_t afi
,
570 const struct prefix_evpn
*evp
,
571 struct prefix_rd
*prd
)
573 struct prefix_evpn global_p
;
575 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
) {
576 /* prefix in the global table doesn't include the VTEP-IP so
577 * we need to create a different copy of the prefix
579 evpn_type1_prefix_global_copy(&global_p
, evp
);
582 return bgp_afi_node_lookup(table
, afi
, safi
, (struct prefix
*)evp
, prd
);
586 * Add (update) or delete MACIP from zebra.
588 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
589 const struct prefix_evpn
*p
,
590 struct in_addr remote_vtep_ip
, int add
,
591 uint8_t flags
, uint32_t seq
, esi_t
*esi
)
595 char buf1
[ETHER_ADDR_STRLEN
];
596 char buf2
[INET6_ADDRSTRLEN
];
597 char buf3
[INET6_ADDRSTRLEN
];
598 static struct in_addr zero_remote_vtep_ip
;
601 if (!zclient
|| zclient
->sock
< 0)
604 /* Don't try to register if Zebra doesn't know of this instance. */
605 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
606 if (BGP_DEBUG(zebra
, ZEBRA
))
608 "%s: No zebra instance to talk to, not installing remote macip",
618 zclient_create_header(
619 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
621 stream_putl(s
, vpn
->vni
);
622 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
623 /* IP address length and IP address, if any. */
624 if (is_evpn_prefix_ipaddr_none(p
))
627 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
629 stream_putl(s
, ipa_len
);
630 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
632 /* If the ESI is valid that becomes the nexthop; tape out the
633 * VTEP-IP for that case
635 if (bgp_evpn_is_esi_valid(esi
))
636 stream_put_in_addr(s
, &zero_remote_vtep_ip
);
638 stream_put_in_addr(s
, &remote_vtep_ip
);
640 /* TX flags - MAC sticky status and/or gateway mac */
641 /* Also TX the sequence number of the best route. */
643 stream_putc(s
, flags
);
645 stream_put(s
, esi
, sizeof(esi_t
));
648 stream_putw_at(s
, 0, stream_get_endp(s
));
650 if (bgp_debug_zebra(NULL
))
652 "Tx %s MACIP, VNI %u MAC %s IP %s flags 0x%x seq %u remote VTEP %s",
653 add
? "ADD" : "DEL", vpn
->vni
,
654 prefix_mac2str(&p
->prefix
.macip_addr
.mac
,
656 ipaddr2str(&p
->prefix
.macip_addr
.ip
,
657 buf3
, sizeof(buf3
)), flags
, seq
,
658 inet_ntop(AF_INET
, &remote_vtep_ip
, buf2
,
661 return zclient_send_message(zclient
);
665 * Add (update) or delete remote VTEP from zebra.
667 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
668 const struct prefix_evpn
*p
,
669 int flood_control
, int add
)
674 if (!zclient
|| zclient
->sock
< 0)
677 /* Don't try to register if Zebra doesn't know of this instance. */
678 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
679 if (BGP_DEBUG(zebra
, ZEBRA
))
681 "%s: No zebra instance to talk to, not installing remote vtep",
689 zclient_create_header(
690 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
692 stream_putl(s
, vpn
->vni
);
693 if (is_evpn_prefix_ipaddr_v4(p
))
694 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
695 else if (is_evpn_prefix_ipaddr_v6(p
)) {
698 "Bad remote IP when trying to %s remote VTEP for VNI %u",
699 add
? "ADD" : "DEL", vpn
->vni
);
702 stream_putl(s
, flood_control
);
704 stream_putw_at(s
, 0, stream_get_endp(s
));
706 if (bgp_debug_zebra(NULL
))
707 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %pI4",
708 add
? "ADD" : "DEL", vpn
->vni
,
709 &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
711 return zclient_send_message(zclient
);
715 * Build extended communities for EVPN prefix route.
717 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
720 struct ecommunity ecom_encap
;
721 struct ecommunity_val eval
;
722 struct ecommunity_val eval_rmac
;
723 bgp_encap_types tnl_type
;
724 struct listnode
*node
, *nnode
;
725 struct ecommunity
*ecom
;
726 struct ecommunity
*old_ecom
;
727 struct list
*vrf_export_rtl
= NULL
;
730 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
731 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
732 encode_encap_extcomm(tnl_type
, &eval
);
734 ecom_encap
.unit_size
= ECOMMUNITY_SIZE
;
735 ecom_encap
.val
= (uint8_t *)eval
.val
;
738 if (attr
->ecommunity
) {
739 old_ecom
= attr
->ecommunity
;
740 ecom
= ecommunity_merge(ecommunity_dup(old_ecom
), &ecom_encap
);
741 if (!old_ecom
->refcnt
)
742 ecommunity_free(&old_ecom
);
744 ecom
= ecommunity_dup(&ecom_encap
);
745 attr
->ecommunity
= ecom
;
746 attr
->encap_tunneltype
= tnl_type
;
748 /* Add the export RTs for L3VNI/VRF */
749 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
750 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
752 ecommunity_merge(attr
->ecommunity
, ecom
);
754 /* add the router mac extended community */
755 if (!is_zero_mac(&attr
->rmac
)) {
756 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
757 ecommunity_add_val(attr
->ecommunity
, &eval_rmac
, true, true);
760 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
764 * Build extended communities for EVPN route.
765 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
766 * and ENCAP extended communities are applicable for all routes.
767 * The default gateway extended community and MAC mobility (sticky) extended
768 * community are added as needed based on passed settings - only for type-2
769 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
770 * added, if present, based on passed settings - only for non-link-local
773 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
776 struct ecommunity ecom_encap
;
777 struct ecommunity ecom_sticky
;
778 struct ecommunity ecom_default_gw
;
779 struct ecommunity ecom_na
;
780 struct ecommunity_val eval
;
781 struct ecommunity_val eval_sticky
;
782 struct ecommunity_val eval_default_gw
;
783 struct ecommunity_val eval_rmac
;
784 struct ecommunity_val eval_na
;
787 bgp_encap_types tnl_type
;
788 struct listnode
*node
, *nnode
;
789 struct ecommunity
*ecom
;
791 struct list
*vrf_export_rtl
= NULL
;
794 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
795 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
796 encode_encap_extcomm(tnl_type
, &eval
);
798 ecom_encap
.unit_size
= ECOMMUNITY_SIZE
;
799 ecom_encap
.val
= (uint8_t *)eval
.val
;
802 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
803 attr
->encap_tunneltype
= tnl_type
;
805 /* Add the export RTs for L2VNI */
806 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
807 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
809 /* Add the export RTs for L3VNI if told to - caller determines
810 * when this should be done.
813 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
814 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
815 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
817 attr
->ecommunity
= ecommunity_merge(
818 attr
->ecommunity
, ecom
);
822 /* Add MAC mobility (sticky) if needed. */
825 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
826 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
827 ecom_sticky
.size
= 1;
828 ecom_sticky
.unit_size
= ECOMMUNITY_SIZE
;
829 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
831 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
834 /* Add RMAC, if told to. */
836 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
837 ecommunity_add_val(attr
->ecommunity
, &eval_rmac
, true, true);
840 /* Add default gateway, if needed. */
841 if (attr
->default_gw
) {
842 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
843 encode_default_gw_extcomm(&eval_default_gw
);
844 ecom_default_gw
.size
= 1;
845 ecom_default_gw
.unit_size
= ECOMMUNITY_SIZE
;
846 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
848 ecommunity_merge(attr
->ecommunity
, &ecom_default_gw
);
851 proxy
= !!(attr
->es_flags
& ATTR_ES_PROXY_ADVERT
);
852 if (attr
->router_flag
|| proxy
) {
853 memset(&ecom_na
, 0, sizeof(ecom_na
));
854 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
, proxy
);
856 ecom_na
.unit_size
= ECOMMUNITY_SIZE
;
857 ecom_na
.val
= (uint8_t *)eval_na
.val
;
858 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
862 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
866 * Add MAC mobility extended community to attribute.
868 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
870 struct ecommunity ecom_tmp
;
871 struct ecommunity_val eval
;
872 uint8_t *ecom_val_ptr
;
879 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
881 /* Find current MM ecommunity */
884 if (attr
->ecommunity
) {
885 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
886 pnt
= attr
->ecommunity
->val
+
887 (i
* attr
->ecommunity
->unit_size
);
891 if (type
== ECOMMUNITY_ENCODE_EVPN
893 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
895 (attr
->ecommunity
->val
+
896 (i
* attr
->ecommunity
->unit_size
));
902 /* Update the existing MM ecommunity */
904 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char)
905 * attr
->ecommunity
->unit_size
);
907 /* Add MM to existing */
909 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
911 ecom_tmp
.unit_size
= ECOMMUNITY_SIZE
;
912 ecom_tmp
.val
= (uint8_t *)eval
.val
;
914 if (attr
->ecommunity
)
916 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
918 attr
->ecommunity
= ecommunity_dup(&ecom_tmp
);
922 /* Install EVPN route into zebra. */
923 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
924 const struct prefix_evpn
*p
,
925 struct bgp_path_info
*pi
)
932 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
935 if (pi
->sub_type
== BGP_ROUTE_IMPORTED
) {
936 if (pi
->attr
->sticky
)
937 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
938 if (pi
->attr
->default_gw
)
939 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
940 if (is_evpn_prefix_ipaddr_v6(p
) &&
941 pi
->attr
->router_flag
)
942 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
944 seq
= mac_mobility_seqnum(pi
->attr
);
945 /* if local ES notify zebra that this is a sync path */
946 if (bgp_evpn_attr_is_local_es(pi
->attr
)) {
947 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_SYNC_PATH
);
948 if (bgp_evpn_attr_is_proxy(pi
->attr
))
950 ZEBRA_MACIP_TYPE_PROXY_ADVERT
);
953 if (!bgp_evpn_attr_is_sync(pi
->attr
))
956 /* if a local path is being turned around and sent
957 * to zebra it is because it is a sync path on
960 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_SYNC_PATH
);
961 /* supply the highest peer seq number to zebra
964 seq
= bgp_evpn_attr_get_sync_seq(pi
->attr
);
965 /* if any of the paths from the peer have the ROUTER
966 * flag set install the local entry as a router entry
968 if (is_evpn_prefix_ipaddr_v6(p
) &&
969 (pi
->attr
->es_flags
&
970 ATTR_ES_PEER_ROUTER
))
972 ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
974 if (!(pi
->attr
->es_flags
& ATTR_ES_PEER_ACTIVE
))
976 ZEBRA_MACIP_TYPE_PROXY_ADVERT
);
979 ret
= bgp_zebra_send_remote_macip(
980 bgp
, vpn
, p
, pi
->attr
->nexthop
, 1, flags
,
981 seq
, bgp_evpn_attr_get_esi(pi
->attr
));
982 } else if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
) {
983 ret
= bgp_evpn_remote_es_evi_add(bgp
, vpn
, p
);
985 switch (bgp_attr_get_pmsi_tnl_type(pi
->attr
)) {
986 case PMSI_TNLTYPE_INGR_REPL
:
987 flood_control
= VXLAN_FLOOD_HEAD_END_REPL
;
990 case PMSI_TNLTYPE_PIM_SM
:
991 flood_control
= VXLAN_FLOOD_PIM_SM
;
995 flood_control
= VXLAN_FLOOD_DISABLED
;
998 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, flood_control
, 1);
1004 /* Uninstall EVPN route from zebra. */
1005 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1006 const struct prefix_evpn
*p
,
1007 struct in_addr remote_vtep_ip
)
1011 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1012 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
1014 else if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
1015 ret
= bgp_evpn_remote_es_evi_del(bgp
, vpn
, p
);
1017 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
,
1018 VXLAN_FLOOD_DISABLED
, 0);
1024 * Due to MAC mobility, the prior "local" best route has been supplanted
1025 * by a "remote" best route. The prior route has to be deleted and withdrawn
1028 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1029 struct bgp_dest
*dest
,
1030 struct bgp_path_info
*old_local
,
1031 struct bgp_path_info
*new_select
)
1033 struct bgp_dest
*global_dest
;
1034 struct bgp_path_info
*pi
;
1035 afi_t afi
= AFI_L2VPN
;
1036 safi_t safi
= SAFI_EVPN
;
1038 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
1039 char esi_buf
[ESI_STR_LEN
];
1040 char esi_buf2
[ESI_STR_LEN
];
1041 struct prefix_evpn
*evp
=
1042 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1044 zlog_debug("local path deleted %pFX es %s; new-path-es %s", evp
,
1045 esi_to_str(&old_local
->attr
->esi
, esi_buf
,
1047 new_select
? esi_to_str(&new_select
->attr
->esi
,
1048 esi_buf2
, sizeof(esi_buf2
))
1052 /* Locate route node in the global EVPN routing table. Note that
1053 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1056 global_dest
= bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1057 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1060 /* Delete route entry in the global EVPN table. */
1061 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
1063 /* Schedule for processing - withdraws to peers happen from
1067 bgp_process(bgp
, global_dest
, afi
, safi
);
1068 bgp_dest_unlock_node(global_dest
);
1071 /* Delete route entry in the VNI route table, caller to remove. */
1072 bgp_path_info_delete(dest
, old_local
);
1076 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1078 * Note: vpn is NULL for local EAD-ES routes.
1080 int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1081 struct bgp_dest
*dest
)
1083 struct bgp_path_info
*old_select
, *new_select
;
1084 struct bgp_path_info_pair old_and_new
;
1085 afi_t afi
= AFI_L2VPN
;
1086 safi_t safi
= SAFI_EVPN
;
1089 /* Compute the best path. */
1090 bgp_best_selection(bgp
, dest
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1092 old_select
= old_and_new
.old
;
1093 new_select
= old_and_new
.new;
1095 /* If the best path hasn't changed - see if there is still something to
1096 * update to zebra RIB.
1097 * Remote routes and SYNC route (i.e. local routes with
1098 * SYNCED_FROM_PEER flag) need to updated to zebra on any attr
1101 if (old_select
&& old_select
== new_select
1102 && old_select
->type
== ZEBRA_ROUTE_BGP
1103 && (old_select
->sub_type
== BGP_ROUTE_IMPORTED
||
1104 bgp_evpn_attr_is_sync(old_select
->attr
))
1105 && !CHECK_FLAG(dest
->flags
, BGP_NODE_USER_CLEAR
)
1106 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1107 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1108 if (bgp_zebra_has_route_changed(old_select
))
1109 ret
= evpn_zebra_install(
1111 (const struct prefix_evpn
*)bgp_dest_get_prefix(
1114 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1115 UNSET_FLAG(old_select
->flags
, BGP_PATH_LINK_BW_CHG
);
1116 bgp_zebra_clear_route_change_flags(dest
);
1120 /* If the user did a "clear" this flag will be set */
1121 UNSET_FLAG(dest
->flags
, BGP_NODE_USER_CLEAR
);
1123 /* bestpath has changed; update relevant fields and install or uninstall
1124 * into the zebra RIB.
1126 if (old_select
|| new_select
)
1127 bgp_bump_version(dest
);
1130 bgp_path_info_unset_flag(dest
, old_select
, BGP_PATH_SELECTED
);
1132 bgp_path_info_set_flag(dest
, new_select
, BGP_PATH_SELECTED
);
1133 bgp_path_info_unset_flag(dest
, new_select
,
1134 BGP_PATH_ATTR_CHANGED
);
1135 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1136 UNSET_FLAG(new_select
->flags
, BGP_PATH_LINK_BW_CHG
);
1139 /* a local entry with the SYNC flag also results in a MAC-IP update
1142 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1143 && (new_select
->sub_type
== BGP_ROUTE_IMPORTED
||
1144 bgp_evpn_attr_is_sync(new_select
->attr
))) {
1145 ret
= evpn_zebra_install(
1147 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1150 /* If an old best existed and it was a "local" route, the only
1152 * it would be supplanted is due to MAC mobility procedures. So,
1154 * need to do an implicit delete and withdraw that route from
1157 if (new_select
->sub_type
== BGP_ROUTE_IMPORTED
&&
1158 old_select
&& old_select
->peer
== bgp
->peer_self
1159 && old_select
->type
== ZEBRA_ROUTE_BGP
1160 && old_select
->sub_type
== BGP_ROUTE_STATIC
1162 evpn_delete_old_local_route(bgp
, vpn
, dest
,
1163 old_select
, new_select
);
1165 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1166 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1167 ret
= evpn_zebra_uninstall(
1169 (const struct prefix_evpn
*)bgp_dest_get_prefix(
1171 old_select
->attr
->nexthop
);
1174 /* Clear any route change flags. */
1175 bgp_zebra_clear_route_change_flags(dest
);
1177 /* Reap old select bgp_path_info, if it has been removed */
1178 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1179 bgp_path_info_reap(dest
, old_select
);
1184 static struct bgp_path_info
*bgp_evpn_route_get_local_path(
1185 struct bgp
*bgp
, struct bgp_dest
*dest
)
1187 struct bgp_path_info
*tmp_pi
;
1188 struct bgp_path_info
*local_pi
= NULL
;
1190 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1191 tmp_pi
= tmp_pi
->next
) {
1192 if (bgp_evpn_is_path_local(bgp
, tmp_pi
)) {
1201 static int update_evpn_type5_route_entry(struct bgp
*bgp_evpn
,
1202 struct bgp
*bgp_vrf
, afi_t afi
,
1203 safi_t safi
, struct bgp_dest
*dest
,
1204 struct attr
*attr
, int *route_changed
)
1206 struct attr
*attr_new
= NULL
;
1207 struct bgp_path_info
*pi
= NULL
;
1208 mpls_label_t label
= MPLS_INVALID_LABEL
;
1209 struct bgp_path_info
*local_pi
= NULL
;
1210 struct bgp_path_info
*tmp_pi
= NULL
;
1213 /* locate the local route entry if any */
1214 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1215 tmp_pi
= tmp_pi
->next
) {
1216 if (tmp_pi
->peer
== bgp_evpn
->peer_self
1217 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1218 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1223 * create a new route entry if one doesn't exist.
1224 * Otherwise see if route attr has changed
1228 /* route has changed as this is the first entry */
1231 /* Add (or update) attribute to hash. */
1232 attr_new
= bgp_attr_intern(attr
);
1234 /* create the route info from attribute */
1235 pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1236 bgp_evpn
->peer_self
, attr_new
, dest
);
1237 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
1239 /* Type-5 routes advertise the L3-VNI */
1240 bgp_path_info_extra_get(pi
);
1241 vni2label(bgp_vrf
->l3vni
, &label
);
1242 memcpy(&pi
->extra
->label
, &label
, sizeof(label
));
1243 pi
->extra
->num_labels
= 1;
1245 /* add the route entry to route node*/
1246 bgp_path_info_add(dest
, pi
);
1250 if (!attrhash_cmp(tmp_pi
->attr
, attr
)) {
1252 /* attribute changed */
1255 /* The attribute has changed. */
1256 /* Add (or update) attribute to hash. */
1257 attr_new
= bgp_attr_intern(attr
);
1258 bgp_path_info_set_flag(dest
, tmp_pi
,
1259 BGP_PATH_ATTR_CHANGED
);
1261 /* Restore route, if needed. */
1262 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1263 bgp_path_info_restore(dest
, tmp_pi
);
1265 /* Unintern existing, set to new. */
1266 bgp_attr_unintern(&tmp_pi
->attr
);
1267 tmp_pi
->attr
= attr_new
;
1268 tmp_pi
->uptime
= bgp_clock();
1274 /* update evpn type-5 route entry */
1275 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1276 struct attr
*src_attr
)
1278 afi_t afi
= AFI_L2VPN
;
1279 safi_t safi
= SAFI_EVPN
;
1281 struct bgp_dest
*dest
= NULL
;
1282 struct bgp
*bgp_evpn
= NULL
;
1283 int route_changed
= 0;
1285 bgp_evpn
= bgp_get_evpn();
1289 /* Build path attribute for this route - use the source attr, if
1290 * present, else treat as locally originated.
1295 memset(&attr
, 0, sizeof(struct attr
));
1296 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1299 /* Advertise Primary IP (PIP) is enabled, send individual
1300 * IP (default instance router-id) as nexthop.
1301 * PIP is disabled or vrr interface is not present
1302 * use anycast-IP as nexthop and anycast RMAC.
1304 if (!bgp_vrf
->evpn_info
->advertise_pip
||
1305 (!bgp_vrf
->evpn_info
->is_anycast_mac
)) {
1306 attr
.nexthop
= bgp_vrf
->originator_ip
;
1307 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1308 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, ETH_ALEN
);
1311 memcpy(&attr
.rmac
, &bgp_vrf
->evpn_info
->pip_rmac
, ETH_ALEN
);
1312 if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
!= INADDR_ANY
) {
1313 attr
.nexthop
= bgp_vrf
->evpn_info
->pip_ip
;
1314 attr
.mp_nexthop_global_in
= bgp_vrf
->evpn_info
->pip_ip
;
1315 } else if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
== INADDR_ANY
)
1316 if (bgp_debug_zebra(NULL
))
1318 "VRF %s evp %pFX advertise-pip primary ip is not configured",
1319 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
);
1322 if (bgp_debug_zebra(NULL
)) {
1323 char buf
[ETHER_ADDR_STRLEN
];
1324 char buf2
[INET6_ADDRSTRLEN
];
1326 zlog_debug("VRF %s type-5 route evp %pFX RMAC %s nexthop %s",
1327 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
,
1328 prefix_mac2str(&attr
.rmac
, buf
, sizeof(buf
)),
1329 inet_ntop(AF_INET
, &attr
.nexthop
, buf2
,
1333 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1335 /* Setup RT and encap extended community */
1336 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1338 /* get the route node in global table */
1339 dest
= bgp_global_evpn_node_get(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1340 (const struct prefix_evpn
*)evp
,
1344 /* create or update the route entry within the route node */
1345 update_evpn_type5_route_entry(bgp_evpn
, bgp_vrf
, afi
, safi
, dest
, &attr
,
1348 /* schedule for processing and unlock node */
1349 if (route_changed
) {
1350 bgp_process(bgp_evpn
, dest
, afi
, safi
);
1351 bgp_dest_unlock_node(dest
);
1354 /* uninten temporary */
1356 aspath_unintern(&attr
.aspath
);
1360 static void bgp_evpn_get_sync_info(struct bgp
*bgp
, esi_t
*esi
,
1361 struct bgp_dest
*dest
, uint32_t loc_seq
,
1362 uint32_t *max_sync_seq
, bool *active_on_peer
,
1363 bool *peer_router
, bool *proxy_from_peer
)
1365 struct bgp_path_info
*tmp_pi
;
1366 struct bgp_path_info
*second_best_path
= NULL
;
1367 uint32_t tmp_mm_seq
= 0;
1371 /* find the best non-local path. a local path can only be present
1374 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1375 tmp_pi
= tmp_pi
->next
) {
1376 if (tmp_pi
->sub_type
!= BGP_ROUTE_IMPORTED
||
1377 !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1380 if (bgp_evpn_path_info_cmp(bgp
, tmp_pi
,
1381 second_best_path
, &paths_eq
))
1382 second_best_path
= tmp_pi
;
1385 if (!second_best_path
)
1388 tmp_esi
= bgp_evpn_attr_get_esi(second_best_path
->attr
);
1389 /* if this has the same ES desination as the local path
1392 if (!memcmp(esi
, tmp_esi
, sizeof(esi_t
))) {
1393 tmp_mm_seq
= mac_mobility_seqnum(second_best_path
->attr
);
1394 if (tmp_mm_seq
< loc_seq
)
1397 /* we have a non-proxy path from the ES peer. */
1398 if (second_best_path
->attr
->es_flags
&
1399 ATTR_ES_PROXY_ADVERT
) {
1400 *proxy_from_peer
= true;
1402 *active_on_peer
= true;
1405 if (second_best_path
->attr
->router_flag
)
1406 *peer_router
= true;
1408 /* we use both proxy and non-proxy imports to
1409 * determine the max sync sequence
1411 if (tmp_mm_seq
> *max_sync_seq
)
1412 *max_sync_seq
= tmp_mm_seq
;
1416 /* Bubble up sync-info from all paths (non-best) to the local-path.
1417 * This is need for MM sequence number syncing and proxy advertisement.
1418 * Note: The local path can only exist as a best path in the
1419 * VPN route table. It will take precedence over all sync paths.
1421 static void update_evpn_route_entry_sync_info(struct bgp
*bgp
,
1422 struct bgp_dest
*dest
,
1424 uint32_t loc_seq
, bool setup_sync
)
1427 struct prefix_evpn
*evp
=
1428 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1430 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1433 esi
= bgp_evpn_attr_get_esi(attr
);
1434 if (bgp_evpn_is_esi_valid(esi
)) {
1436 uint32_t max_sync_seq
= 0;
1437 bool active_on_peer
= false;
1438 bool peer_router
= false;
1439 bool proxy_from_peer
= false;
1441 bgp_evpn_get_sync_info(bgp
, esi
, dest
, loc_seq
,
1442 &max_sync_seq
, &active_on_peer
,
1443 &peer_router
, &proxy_from_peer
);
1444 attr
->mm_sync_seqnum
= max_sync_seq
;
1446 attr
->es_flags
|= ATTR_ES_PEER_ACTIVE
;
1448 attr
->es_flags
&= ~ATTR_ES_PEER_ACTIVE
;
1449 if (proxy_from_peer
)
1450 attr
->es_flags
|= ATTR_ES_PEER_PROXY
;
1452 attr
->es_flags
&= ~ATTR_ES_PEER_PROXY
;
1454 attr
->es_flags
|= ATTR_ES_PEER_ROUTER
;
1456 attr
->es_flags
&= ~ATTR_ES_PEER_ROUTER
;
1458 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
1459 char esi_buf
[ESI_STR_LEN
];
1462 "setup sync info for %pFX es %s max_seq %d %s%s%s",
1464 esi_to_str(esi
, esi_buf
,
1467 (attr
->es_flags
& ATTR_ES_PEER_ACTIVE
)
1470 (attr
->es_flags
& ATTR_ES_PEER_PROXY
)
1473 (attr
->es_flags
& ATTR_ES_PEER_ROUTER
)
1479 attr
->mm_sync_seqnum
= 0;
1480 attr
->es_flags
&= ~ATTR_ES_PEER_ACTIVE
;
1481 attr
->es_flags
&= ~ATTR_ES_PEER_PROXY
;
1486 * Create or update EVPN route entry. This could be in the VNI route table
1487 * or the global route table.
1489 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1490 afi_t afi
, safi_t safi
,
1491 struct bgp_dest
*dest
, struct attr
*attr
,
1492 int add
, struct bgp_path_info
**pi
,
1493 uint8_t flags
, uint32_t seq
, bool vpn_rt
,
1496 struct bgp_path_info
*tmp_pi
;
1497 struct bgp_path_info
*local_pi
;
1498 struct attr
*attr_new
;
1499 mpls_label_t label
[BGP_MAX_LABELS
];
1500 uint32_t num_labels
= 1;
1501 int route_change
= 1;
1503 const struct prefix_evpn
*evp
;
1506 evp
= (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1507 memset(&label
, 0, sizeof(label
));
1509 /* See if this is an update of an existing route, or a new add. */
1510 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
1512 /* If route doesn't exist already, create a new one, if told to.
1513 * Otherwise act based on whether the attributes of the route have
1516 if (!local_pi
&& !add
)
1519 if (old_is_sync
&& local_pi
)
1520 *old_is_sync
= bgp_evpn_attr_is_sync(local_pi
->attr
);
1522 /* if a local path is being added with a non-zero esi look
1523 * for SYNC paths from ES peers and bubble up the sync-info
1525 update_evpn_route_entry_sync_info(bgp
, dest
, attr
, seq
, vpn_rt
);
1527 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1528 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1529 add_mac_mobility_to_attr(seq
, attr
);
1532 /* Add (or update) attribute to hash. */
1533 attr_new
= bgp_attr_intern(attr
);
1535 /* Extract MAC mobility sequence number, if any. */
1536 attr_new
->mm_seqnum
=
1537 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1538 attr_new
->sticky
= sticky
;
1540 /* Create new route with its attribute. */
1541 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1542 bgp
->peer_self
, attr_new
, dest
);
1543 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1544 bgp_path_info_extra_get(tmp_pi
);
1546 /* The VNI goes into the 'label' field of the route */
1547 vni2label(vpn
->vni
, &label
[0]);
1549 /* Type-2 routes may carry a second VNI - the L3-VNI.
1550 * Only attach second label if we are advertising two labels for
1553 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1554 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1557 l3vni
= bgpevpn_get_l3vni(vpn
);
1559 vni2label(l3vni
, &label
[1]);
1564 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1565 tmp_pi
->extra
->num_labels
= num_labels
;
1566 /* Mark route as self type-2 route */
1567 if (flags
&& CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1568 tmp_pi
->extra
->af_flags
= BGP_EVPN_MACIP_TYPE_SVI_IP
;
1569 bgp_path_info_add(dest
, tmp_pi
);
1572 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1573 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1577 * The attributes have changed, type-2 routes needs to
1578 * be advertised with right labels.
1580 vni2label(vpn
->vni
, &label
[0]);
1581 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1582 && CHECK_FLAG(vpn
->flags
,
1583 VNI_FLAG_USE_TWO_LABELS
)) {
1586 l3vni
= bgpevpn_get_l3vni(vpn
);
1588 vni2label(l3vni
, &label
[1]);
1592 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1593 tmp_pi
->extra
->num_labels
= num_labels
;
1595 /* The attribute has changed. */
1596 /* Add (or update) attribute to hash. */
1597 attr_new
= bgp_attr_intern(attr
);
1598 bgp_path_info_set_flag(dest
, tmp_pi
,
1599 BGP_PATH_ATTR_CHANGED
);
1601 /* Extract MAC mobility sequence number, if any. */
1602 attr_new
->mm_seqnum
=
1603 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1604 attr_new
->sticky
= sticky
;
1606 /* Restore route, if needed. */
1607 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1608 bgp_path_info_restore(dest
, tmp_pi
);
1610 /* Unintern existing, set to new. */
1611 bgp_attr_unintern(&tmp_pi
->attr
);
1612 tmp_pi
->attr
= attr_new
;
1613 tmp_pi
->uptime
= bgp_clock();
1617 /* MAC-IP routes in the VNI route table are linked to the
1620 if (route_change
&& vpn_rt
1621 && (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
))
1622 bgp_evpn_path_es_link(tmp_pi
, vpn
->vni
,
1623 bgp_evpn_attr_get_esi(tmp_pi
->attr
));
1625 /* Return back the route entry. */
1627 return route_change
;
1630 static void evpn_zebra_reinstall_best_route(struct bgp
*bgp
,
1631 struct bgpevpn
*vpn
,
1632 struct bgp_dest
*dest
)
1634 struct bgp_path_info
*tmp_ri
;
1635 struct bgp_path_info
*curr_select
= NULL
;
1637 for (tmp_ri
= bgp_dest_get_bgp_path_info(dest
); tmp_ri
;
1638 tmp_ri
= tmp_ri
->next
) {
1639 if (CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_SELECTED
)) {
1640 curr_select
= tmp_ri
;
1645 if (curr_select
&& curr_select
->type
== ZEBRA_ROUTE_BGP
1646 && (curr_select
->sub_type
== BGP_ROUTE_IMPORTED
||
1647 bgp_evpn_attr_is_sync(curr_select
->attr
)))
1648 evpn_zebra_install(bgp
, vpn
,
1649 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1654 * If the local route was not selected evict it and tell zebra to re-add
1655 * the best remote dest.
1657 * Typically a local path added by zebra is expected to be selected as
1658 * best. In which case when a remote path wins as best (later)
1659 * evpn_route_select_install itself evicts the older-local-best path.
1661 * However if bgp's add and zebra's add cross paths (race condition) it
1662 * is possible that the local path is no longer the "older" best path.
1663 * It is a path that was never designated as best and hence requires
1664 * additional handling to prevent bgp from injecting and holding on to a
1665 * non-best local path.
1667 static void evpn_cleanup_local_non_best_route(struct bgp
*bgp
,
1668 struct bgpevpn
*vpn
,
1669 struct bgp_dest
*dest
,
1670 struct bgp_path_info
*local_pi
)
1672 /* local path was not picked as the winner; kick it out */
1673 if (bgp_debug_zebra(NULL
))
1674 zlog_debug("evicting local evpn prefix %pBD as remote won",
1677 evpn_delete_old_local_route(bgp
, vpn
, dest
, local_pi
, NULL
);
1678 bgp_path_info_reap(dest
, local_pi
);
1680 /* tell zebra to re-add the best remote path */
1681 evpn_zebra_reinstall_best_route(bgp
, vpn
, dest
);
1685 * Create or update EVPN route (of type based on prefix) for specified VNI
1686 * and schedule for processing.
1688 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1689 struct prefix_evpn
*p
, uint8_t flags
,
1690 uint32_t seq
, esi_t
*esi
)
1692 struct bgp_dest
*dest
;
1694 struct attr
*attr_new
;
1695 int add_l3_ecomm
= 0;
1696 struct bgp_path_info
*pi
;
1697 afi_t afi
= AFI_L2VPN
;
1698 safi_t safi
= SAFI_EVPN
;
1700 bool old_is_sync
= false;
1702 memset(&attr
, 0, sizeof(struct attr
));
1704 /* Build path-attribute for this route. */
1705 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1706 attr
.nexthop
= vpn
->originator_ip
;
1707 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1708 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1709 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1710 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1711 attr
.router_flag
= CHECK_FLAG(flags
,
1712 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1713 if (CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_PROXY_ADVERT
))
1714 attr
.es_flags
|= ATTR_ES_PROXY_ADVERT
;
1716 if (esi
&& bgp_evpn_is_esi_valid(esi
)) {
1717 memcpy(&attr
.esi
, esi
, sizeof(esi_t
));
1718 attr
.es_flags
|= ATTR_ES_IS_LOCAL
;
1721 /* PMSI is only needed for type-3 routes */
1722 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
1723 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1724 bgp_attr_set_pmsi_tnl_type(&attr
, PMSI_TNLTYPE_INGR_REPL
);
1727 if (bgp_debug_zebra(NULL
)) {
1728 char buf
[ETHER_ADDR_STRLEN
];
1729 char buf3
[ESI_STR_LEN
];
1732 "VRF %s vni %u type-2 route evp %pFX RMAC %s nexthop %pI4 esi %s",
1733 vpn
->bgp_vrf
? vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
)
1736 prefix_mac2str(&attr
.rmac
, buf
, sizeof(buf
)),
1737 &attr
.mp_nexthop_global_in
,
1738 esi_to_str(esi
, buf3
, sizeof(buf3
)));
1740 /* router mac is only needed for type-2 routes here. */
1741 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
1742 uint8_t af_flags
= 0;
1744 if (CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1745 SET_FLAG(af_flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
);
1747 bgp_evpn_get_rmac_nexthop(vpn
, p
, &attr
, af_flags
);
1750 vni2label(vpn
->vni
, &(attr
.label
));
1752 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1753 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1756 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1757 (is_evpn_prefix_ipaddr_v4(p
) ||
1758 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1759 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1760 bgpevpn_get_l3vni(vpn
))
1763 /* Set up extended community. */
1764 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1766 /* First, create (or fetch) route node within the VNI. */
1767 /* NOTE: There is no RD here. */
1768 dest
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1770 /* Create or update route entry. */
1771 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, dest
, &attr
,
1773 true /* setup_sync */, &old_is_sync
);
1775 attr_new
= pi
->attr
;
1777 /* lock ri to prevent freeing in evpn_route_select_install */
1778 bgp_path_info_lock(pi
);
1780 /* Perform route selection. Normally, the local route in the
1781 * VNI is expected to win and be the best route. However, if
1782 * there is a race condition where a host moved from local to
1783 * remote and the remote route was received in BGP just prior
1784 * to the local MACIP notification from zebra, the remote
1785 * route would win, and we should evict the defunct local route
1786 * and (re)install the remote route into zebra.
1788 evpn_route_select_install(bgp
, vpn
, dest
);
1790 * If the new local route was not selected evict it and tell zebra
1791 * to re-add the best remote dest. BGP doesn't retain non-best local
1794 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
1797 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1799 evpn_cleanup_local_non_best_route(bgp
, vpn
, dest
, pi
);
1803 /* If the local path already existed and is still the
1804 * best path we need to also check if it transitioned
1805 * from being a sync path to a non-sync path. If it
1806 * it did we need to notify zebra that the sync-path
1809 new_is_sync
= bgp_evpn_attr_is_sync(pi
->attr
);
1810 if (!new_is_sync
&& old_is_sync
)
1811 evpn_zebra_uninstall(bgp
, vpn
, p
, zero_vtep_ip
);
1814 bgp_path_info_unlock(pi
);
1816 bgp_dest_unlock_node(dest
);
1818 /* If this is a new route or some attribute has changed, export the
1819 * route to the global table. The route will be advertised to peers
1820 * from there. Note that this table is a 2-level tree (RD-level +
1821 * Prefix-level) similar to L3VPN routes.
1824 struct bgp_path_info
*global_pi
;
1826 dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1827 (const struct prefix_evpn
*)p
,
1829 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, dest
, attr_new
, 1,
1830 &global_pi
, flags
, seq
,
1831 false /* setup_sync */, NULL
/* old_is_sync */);
1833 /* Schedule for processing and unlock node. */
1834 bgp_process(bgp
, dest
, afi
, safi
);
1835 bgp_dest_unlock_node(dest
);
1838 /* Unintern temporary. */
1839 aspath_unintern(&attr
.aspath
);
1845 * Delete EVPN route entry.
1846 * The entry can be in ESI/VNI table or the global table.
1848 void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1849 struct bgp_dest
*dest
,
1850 struct bgp_path_info
**pi
)
1852 struct bgp_path_info
*tmp_pi
;
1856 /* Now, find matching route. */
1857 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1858 tmp_pi
= tmp_pi
->next
)
1859 if (tmp_pi
->peer
== bgp
->peer_self
1860 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1861 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1866 /* Mark route for delete. */
1868 bgp_path_info_delete(dest
, tmp_pi
);
1871 /* Delete EVPN type5 route */
1872 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1874 afi_t afi
= AFI_L2VPN
;
1875 safi_t safi
= SAFI_EVPN
;
1876 struct bgp_dest
*dest
= NULL
;
1877 struct bgp_path_info
*pi
= NULL
;
1878 struct bgp
*bgp_evpn
= NULL
; /* evpn bgp instance */
1880 bgp_evpn
= bgp_get_evpn();
1884 /* locate the global route entry for this type-5 prefix */
1885 dest
= bgp_global_evpn_node_lookup(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1886 (const struct prefix_evpn
*)evp
, &bgp_vrf
->vrf_prd
);
1890 delete_evpn_route_entry(bgp_evpn
, afi
, safi
, dest
, &pi
);
1892 bgp_process(bgp_evpn
, dest
, afi
, safi
);
1893 bgp_dest_unlock_node(dest
);
1898 * Delete EVPN route (of type based on prefix) for specified VNI and
1899 * schedule for processing.
1901 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1902 struct prefix_evpn
*p
)
1904 struct bgp_dest
*dest
, *global_dest
;
1905 struct bgp_path_info
*pi
;
1906 afi_t afi
= AFI_L2VPN
;
1907 safi_t safi
= SAFI_EVPN
;
1909 /* First, locate the route node within the VNI. If it doesn't exist,
1911 * is nothing further to do.
1913 /* NOTE: There is no RD here. */
1914 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1918 /* Next, locate route node in the global EVPN routing table. Note that
1919 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1922 global_dest
= bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1923 (const struct prefix_evpn
*)p
, &vpn
->prd
);
1925 /* Delete route entry in the global EVPN table. */
1926 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
1928 /* Schedule for processing - withdraws to peers happen from
1932 bgp_process(bgp
, global_dest
, afi
, safi
);
1933 bgp_dest_unlock_node(global_dest
);
1936 /* Delete route entry in the VNI route table. This can just be removed.
1938 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
1940 bgp_path_info_reap(dest
, pi
);
1941 evpn_route_select_install(bgp
, vpn
, dest
);
1943 bgp_dest_unlock_node(dest
);
1948 static void bgp_evpn_update_type2_route_entry(struct bgp
*bgp
,
1949 struct bgpevpn
*vpn
,
1950 struct bgp_dest
*dest
,
1951 struct bgp_path_info
*local_pi
,
1954 afi_t afi
= AFI_L2VPN
;
1955 safi_t safi
= SAFI_EVPN
;
1956 struct bgp_path_info
*pi
;
1958 struct attr
*attr_new
;
1960 int add_l3_ecomm
= 0;
1961 struct bgp_dest
*global_dest
;
1962 struct bgp_path_info
*global_pi
;
1963 struct prefix_evpn
*evp
=
1964 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1966 bool old_is_sync
= false;
1968 if (CHECK_FLAG(local_pi
->flags
, BGP_PATH_REMOVED
))
1972 * Build attribute per local route as the MAC mobility and
1973 * some other values could differ for different routes. The
1974 * attributes will be shared in the hash table.
1976 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1977 attr
.nexthop
= vpn
->originator_ip
;
1978 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1979 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1980 attr
.sticky
= (local_pi
->attr
->sticky
) ? 1 : 0;
1981 attr
.router_flag
= (local_pi
->attr
->router_flag
) ? 1 : 0;
1982 attr
.es_flags
= local_pi
->attr
->es_flags
;
1983 if (local_pi
->attr
->default_gw
) {
1984 attr
.default_gw
= 1;
1985 if (is_evpn_prefix_ipaddr_v6(evp
))
1986 attr
.router_flag
= 1;
1988 memcpy(&attr
.esi
, &local_pi
->attr
->esi
, sizeof(esi_t
));
1989 bgp_evpn_get_rmac_nexthop(vpn
, evp
, &attr
,
1990 local_pi
->extra
->af_flags
);
1991 vni2label(vpn
->vni
, &(attr
.label
));
1992 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
1993 * using L3 VNI for type-2 routes also.
1995 if ((is_evpn_prefix_ipaddr_v4(evp
) ||
1996 !IN6_IS_ADDR_LINKLOCAL(
1997 &evp
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1998 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1999 bgpevpn_get_l3vni(vpn
))
2002 /* Set up extended community. */
2003 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2004 seq
= mac_mobility_seqnum(local_pi
->attr
);
2006 if (bgp_debug_zebra(NULL
)) {
2007 char buf
[ETHER_ADDR_STRLEN
];
2008 char buf3
[ESI_STR_LEN
];
2011 "VRF %s vni %u evp %pFX RMAC %s nexthop %pI4 esi %s esf 0x%x from %s",
2012 vpn
->bgp_vrf
? vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
)
2015 prefix_mac2str(&attr
.rmac
, buf
, sizeof(buf
)),
2016 &attr
.mp_nexthop_global_in
,
2017 esi_to_str(&attr
.esi
, buf3
, sizeof(buf3
)),
2018 attr
.es_flags
, caller
);
2021 /* Update the route entry. */
2022 route_change
= update_evpn_route_entry(
2023 bgp
, vpn
, afi
, safi
, dest
, &attr
, 0, &pi
, 0, seq
,
2024 true /* setup_sync */, &old_is_sync
);
2027 attr_new
= pi
->attr
;
2028 /* lock ri to prevent freeing in evpn_route_select_install */
2029 bgp_path_info_lock(pi
);
2031 /* Perform route selection. Normally, the local route in the
2032 * VNI is expected to win and be the best route. However,
2033 * under peculiar situations (e.g., tunnel (next hop) IP change
2034 * that causes best selection to be based on next hop), a
2035 * remote route could win. If the local route is the best,
2036 * ensure it is updated in the global EVPN route table and
2037 * advertised to peers; otherwise, ensure it is evicted and
2038 * (re)install the remote route into zebra.
2040 evpn_route_select_install(bgp
, vpn
, dest
);
2042 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2045 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
2047 evpn_cleanup_local_non_best_route(bgp
, vpn
, dest
, pi
);
2051 /* If the local path already existed and is still the
2052 * best path we need to also check if it transitioned
2053 * from being a sync path to a non-sync path. If it
2054 * it did we need to notify zebra that the sync-path
2057 new_is_sync
= bgp_evpn_attr_is_sync(pi
->attr
);
2058 if (!new_is_sync
&& old_is_sync
)
2059 evpn_zebra_uninstall(bgp
, vpn
,
2066 bgp_path_info_unlock(pi
);
2069 /* Update route in global routing table. */
2070 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
,
2071 safi
, evp
, &vpn
->prd
);
2072 assert(global_dest
);
2073 update_evpn_route_entry(
2074 bgp
, vpn
, afi
, safi
, global_dest
, attr_new
, 0,
2075 &global_pi
, 0, mac_mobility_seqnum(attr_new
),
2076 false /* setup_sync */, NULL
/* old_is_sync */);
2078 /* Schedule for processing and unlock node. */
2079 bgp_process(bgp
, global_dest
, afi
, safi
);
2080 bgp_dest_unlock_node(global_dest
);
2083 /* Unintern temporary. */
2084 aspath_unintern(&attr
.aspath
);
2088 * Update all type-2 (MACIP) local routes for this VNI - these should also
2089 * be scheduled for advertise to peers.
2091 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2093 struct bgp_dest
*dest
;
2094 struct bgp_path_info
*tmp_pi
;
2096 /* Walk this VNI's route table and update local type-2 routes. For any
2097 * routes updated, update corresponding entry in the global table too.
2099 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2100 dest
= bgp_route_next(dest
)) {
2101 const struct prefix_evpn
*evp
=
2102 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
2104 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2107 /* Identify local route. */
2108 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
2109 tmp_pi
= tmp_pi
->next
) {
2110 if (tmp_pi
->peer
== bgp
->peer_self
2111 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2112 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2119 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, tmp_pi
,
2127 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2128 * global routing table. These are also scheduled for withdraw from peers.
2130 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2134 struct bgp_dest
*rddest
, *dest
;
2135 struct bgp_table
*table
;
2136 struct bgp_path_info
*pi
;
2141 rddest
= bgp_node_lookup(bgp
->rib
[afi
][safi
],
2142 (struct prefix
*)&vpn
->prd
);
2143 if (rddest
&& bgp_dest_has_bgp_path_info_data(rddest
)) {
2144 table
= bgp_dest_get_bgp_table_info(rddest
);
2145 for (dest
= bgp_table_top(table
); dest
;
2146 dest
= bgp_route_next(dest
)) {
2147 const struct prefix_evpn
*evp
=
2148 (const struct prefix_evpn
*)bgp_dest_get_prefix(
2151 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2154 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2156 bgp_process(bgp
, dest
, afi
, safi
);
2160 /* Unlock RD node. */
2162 bgp_dest_unlock_node(rddest
);
2168 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2169 * table as well as the per-VNI route table.
2171 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2175 struct bgp_dest
*dest
;
2176 struct bgp_path_info
*pi
;
2181 /* First, walk the global route table for this VNI's type-2 local
2183 * EVPN routes are a 2-level table, first get the RD table.
2185 delete_global_type2_routes(bgp
, vpn
);
2187 /* Next, walk this VNI's route table and delete local type-2 routes. */
2188 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2189 dest
= bgp_route_next(dest
)) {
2190 const struct prefix_evpn
*evp
=
2191 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
2193 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2196 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2198 /* Route entry in local table gets deleted immediately. */
2200 bgp_path_info_reap(dest
, pi
);
2207 * Delete all routes in the per-VNI route table.
2209 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2211 struct bgp_dest
*dest
;
2212 struct bgp_path_info
*pi
, *nextpi
;
2214 /* Walk this VNI's route table and delete all routes. */
2215 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2216 dest
= bgp_route_next(dest
)) {
2217 for (pi
= bgp_dest_get_bgp_path_info(dest
);
2218 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2219 bgp_path_info_delete(dest
, pi
);
2220 bgp_path_info_reap(dest
, pi
);
2227 /* BUM traffic flood mode per-l2-vni */
2228 static int bgp_evpn_vni_flood_mode_get(struct bgp
*bgp
,
2229 struct bgpevpn
*vpn
)
2231 /* if flooding has been globally disabled per-vni mode is
2234 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
2235 return VXLAN_FLOOD_DISABLED
;
2237 /* if mcast group ip has been specified we use a PIM-SM MDT */
2238 if (vpn
->mcast_grp
.s_addr
!= INADDR_ANY
)
2239 return VXLAN_FLOOD_PIM_SM
;
2241 /* default is ingress replication */
2242 return VXLAN_FLOOD_HEAD_END_REPL
;
2246 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2247 * export RT getting modified or change to tunnel IP. Note that these
2248 * situations need the route in the per-VNI table as well as the global
2249 * table to be updated (as attributes change).
2251 int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2254 struct prefix_evpn p
;
2256 /* Update and advertise the type-3 route (only one) followed by the
2257 * locally learnt type-2 routes (MACIP) - for this VNI.
2259 * RT-3 only if doing head-end replication
2261 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
2262 == VXLAN_FLOOD_HEAD_END_REPL
) {
2263 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2264 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
);
2269 return update_all_type2_routes(bgp
, vpn
);
2273 * Delete (and withdraw) local routes for specified VNI from the global
2274 * table and per-VNI table. After this, remove all other routes from
2275 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2276 * (advertise-all-vni) being disabled.
2278 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2281 struct prefix_evpn p
;
2283 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2284 * followed by type-3 routes (only one) - for this VNI.
2286 ret
= delete_all_type2_routes(bgp
, vpn
);
2290 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2291 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2295 /* Delete all routes from the per-VNI table. */
2296 return delete_all_vni_routes(bgp
, vpn
);
2300 * There is a flood mcast IP address change. Update the mcast-grp and
2301 * remove the type-3 route if any. A new type-3 route will be generated
2302 * post tunnel_ip update if the new flood mode is head-end-replication.
2304 static int bgp_evpn_mcast_grp_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2305 struct in_addr mcast_grp
)
2307 struct prefix_evpn p
;
2309 vpn
->mcast_grp
= mcast_grp
;
2311 if (is_vni_live(vpn
)) {
2312 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2313 delete_evpn_route(bgp
, vpn
, &p
);
2320 * There is a tunnel endpoint IP address change for this VNI, delete
2321 * prior type-3 route (if needed) and update.
2322 * Note: Route re-advertisement happens elsewhere after other processing
2325 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2326 struct in_addr originator_ip
)
2328 struct prefix_evpn p
;
2330 /* If VNI is not live, we only need to update the originator ip */
2331 if (!is_vni_live(vpn
)) {
2332 vpn
->originator_ip
= originator_ip
;
2336 /* Update the tunnel-ip hash */
2337 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2338 bgp_tip_add(bgp
, &originator_ip
);
2340 /* filter routes as martian nexthop db has changed */
2341 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2343 /* Need to withdraw type-3 route as the originator IP is part
2346 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2347 delete_evpn_route(bgp
, vpn
, &p
);
2349 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2350 vpn
->originator_ip
= originator_ip
;
2354 static struct bgp_path_info
*
2355 bgp_create_evpn_bgp_path_info(struct bgp_path_info
*parent_pi
,
2356 struct bgp_dest
*dest
, struct attr
*attr
)
2358 struct attr
*attr_new
;
2359 struct bgp_path_info
*pi
;
2361 /* Add (or update) attribute to hash. */
2362 attr_new
= bgp_attr_intern(attr
);
2364 /* Create new route with its attribute. */
2365 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0, parent_pi
->peer
,
2367 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2368 bgp_path_info_extra_get(pi
);
2369 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2370 bgp_dest_lock_node((struct bgp_dest
*)parent_pi
->net
);
2371 if (parent_pi
->extra
) {
2372 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2373 sizeof(pi
->extra
->label
));
2374 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2376 bgp_path_info_add(dest
, pi
);
2382 * Install route entry into the VRF routing table and invoke route selection.
2384 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2385 const struct prefix_evpn
*evp
,
2386 struct bgp_path_info
*parent_pi
)
2388 struct bgp_dest
*dest
;
2389 struct bgp_path_info
*pi
;
2391 struct attr
*attr_new
;
2394 struct prefix
*pp
= &p
;
2397 bool new_pi
= false;
2399 memset(pp
, 0, sizeof(struct prefix
));
2400 ip_prefix_from_evpn_prefix(evp
, pp
);
2402 if (bgp_debug_zebra(NULL
))
2404 "vrf %s: import evpn prefix %pFX parent %p flags 0x%x",
2405 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
, parent_pi
,
2408 /* Create (or fetch) route within the VRF. */
2409 /* NOTE: There is no RD here. */
2410 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2412 safi
= SAFI_UNICAST
;
2413 dest
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2414 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2416 safi
= SAFI_UNICAST
;
2417 dest
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2421 /* EVPN routes currently only support a IPv4 next hop which corresponds
2422 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2423 * or prefix route, we have to convert the next hop to an IPv4-mapped
2424 * address for the rest of the code to flow through. In the case of IPv4,
2425 * make sure to set the flag for next hop attribute.
2427 attr
= *parent_pi
->attr
;
2429 evpn_convert_nexthop_to_ipv6(&attr
);
2431 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2433 /* Check if route entry is already present. */
2434 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2436 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2440 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, dest
, &attr
);
2443 if (attrhash_cmp(pi
->attr
, &attr
)
2444 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2445 bgp_dest_unlock_node(dest
);
2448 /* The attribute has changed. */
2449 /* Add (or update) attribute to hash. */
2450 attr_new
= bgp_attr_intern(&attr
);
2452 /* Restore route, if needed. */
2453 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2454 bgp_path_info_restore(dest
, pi
);
2456 /* Mark if nexthop has changed. */
2458 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2460 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2461 &attr_new
->mp_nexthop_global
)))
2462 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2464 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ATTR_CHANGED
);
2465 /* Unintern existing, set to new. */
2466 bgp_attr_unintern(&pi
->attr
);
2467 pi
->attr
= attr_new
;
2468 pi
->uptime
= bgp_clock();
2470 /* as it is an importation, change nexthop */
2471 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ANNC_NH_SELF
);
2473 bgp_aggregate_increment(bgp_vrf
, bgp_dest_get_prefix(dest
), pi
, afi
,
2476 /* Perform route selection and update zebra, if required. */
2477 bgp_process(bgp_vrf
, dest
, afi
, safi
);
2479 /* Process for route leaking. */
2480 vpn_leak_from_vrf_update(bgp_get_default(), bgp_vrf
, pi
);
2482 bgp_dest_unlock_node(dest
);
2484 if (bgp_debug_zebra(NULL
))
2485 zlog_debug("... %s pi dest %p (l %d) pi %p (l %d, f 0x%x)",
2486 new_pi
? "new" : "update", dest
,
2487 bgp_dest_get_lock_count(dest
), pi
, pi
->lock
,
2494 * Install route entry into the VNI routing table and invoke route selection.
2496 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2497 const struct prefix_evpn
*p
,
2498 struct bgp_path_info
*parent_pi
)
2500 struct bgp_dest
*dest
;
2501 struct bgp_path_info
*pi
;
2502 struct bgp_path_info
*local_pi
;
2503 struct attr
*attr_new
;
2505 struct prefix_evpn ad_evp
;
2507 /* EAD prefix in the global table doesn't include the VTEP-IP so
2508 * we need to create a different copy for the VNI
2510 if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
2511 p
= evpn_type1_prefix_vni_copy(&ad_evp
, p
,
2512 parent_pi
->attr
->nexthop
);
2514 /* Create (or fetch) route within the VNI. */
2515 /* NOTE: There is no RD here. */
2516 dest
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2518 /* Check if route entry is already present. */
2519 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2521 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2525 /* Create an info */
2526 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, dest
,
2529 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2530 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2531 bgp_dest_unlock_node(dest
);
2534 /* The attribute has changed. */
2535 /* Add (or update) attribute to hash. */
2536 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2538 /* Restore route, if needed. */
2539 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2540 bgp_path_info_restore(dest
, pi
);
2542 /* Mark if nexthop has changed. */
2543 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2544 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2546 /* Unintern existing, set to new. */
2547 bgp_attr_unintern(&pi
->attr
);
2548 pi
->attr
= attr_new
;
2549 pi
->uptime
= bgp_clock();
2552 /* MAC-IP routes in the VNI table are linked to the destination ES */
2553 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
2554 bgp_evpn_path_es_link(pi
, vpn
->vni
,
2555 bgp_evpn_attr_get_esi(pi
->attr
));
2557 /* Perform route selection and update zebra, if required. */
2558 ret
= evpn_route_select_install(bgp
, vpn
, dest
);
2560 /* if the best path is a local path with a non-zero ES
2561 * sync info against the local path may need to be updated
2562 * when a remote path is added/updated (including changes
2563 * from sync-path to remote-path)
2565 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
2566 if (local_pi
&& bgp_evpn_attr_is_local_es(local_pi
->attr
))
2567 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, local_pi
,
2570 bgp_dest_unlock_node(dest
);
2576 * Uninstall route entry from the VRF routing table and send message
2577 * to zebra, if appropriate.
2579 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2580 const struct prefix_evpn
*evp
,
2581 struct bgp_path_info
*parent_pi
)
2583 struct bgp_dest
*dest
;
2584 struct bgp_path_info
*pi
;
2587 struct prefix
*pp
= &p
;
2591 memset(pp
, 0, sizeof(struct prefix
));
2592 ip_prefix_from_evpn_prefix(evp
, pp
);
2594 if (bgp_debug_zebra(NULL
))
2596 "vrf %s: unimport evpn prefix %pFX parent %p flags 0x%x",
2597 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
, parent_pi
,
2600 /* Locate route within the VRF. */
2601 /* NOTE: There is no RD here. */
2602 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2604 safi
= SAFI_UNICAST
;
2605 dest
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2608 safi
= SAFI_UNICAST
;
2609 dest
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2615 /* Find matching route entry. */
2616 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2618 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2624 if (bgp_debug_zebra(NULL
))
2625 zlog_debug("... delete dest %p (l %d) pi %p (l %d, f 0x%x)",
2626 dest
, bgp_dest_get_lock_count(dest
), pi
, pi
->lock
,
2629 /* Process for route leaking. */
2630 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp_vrf
, pi
);
2632 bgp_aggregate_decrement(bgp_vrf
, bgp_dest_get_prefix(dest
), pi
, afi
,
2635 /* Mark entry for deletion */
2636 bgp_path_info_delete(dest
, pi
);
2638 /* Perform route selection and update zebra, if required. */
2639 bgp_process(bgp_vrf
, dest
, afi
, safi
);
2641 /* Unlock route node. */
2642 bgp_dest_unlock_node(dest
);
2648 * Uninstall route entry from the VNI routing table and send message
2649 * to zebra, if appropriate.
2651 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2652 const struct prefix_evpn
*p
,
2653 struct bgp_path_info
*parent_pi
)
2655 struct bgp_dest
*dest
;
2656 struct bgp_path_info
*pi
;
2657 struct bgp_path_info
*local_pi
;
2659 struct prefix_evpn ad_evp
;
2661 /* EAD prefix in the global table doesn't include the VTEP-IP so
2662 * we need to create a different copy for the VNI
2664 if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
2665 p
= evpn_type1_prefix_vni_copy(&ad_evp
, p
,
2666 parent_pi
->attr
->nexthop
);
2668 /* Locate route within the VNI. */
2669 /* NOTE: There is no RD here. */
2670 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2674 /* Find matching route entry. */
2675 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2677 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2683 /* Mark entry for deletion */
2684 bgp_path_info_delete(dest
, pi
);
2686 /* Perform route selection and update zebra, if required. */
2687 ret
= evpn_route_select_install(bgp
, vpn
, dest
);
2689 /* if the best path is a local path with a non-zero ES
2690 * sync info against the local path may need to be updated
2691 * when a remote path is deleted
2693 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
2694 if (local_pi
&& bgp_evpn_attr_is_local_es(local_pi
->attr
))
2695 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, local_pi
,
2698 /* Unlock route node. */
2699 bgp_dest_unlock_node(dest
);
2705 * Given a route entry and a VRF, see if this route entry should be
2706 * imported into the VRF i.e., RTs match.
2708 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2709 struct bgp_path_info
*pi
)
2711 struct attr
*attr
= pi
->attr
;
2712 struct ecommunity
*ecom
;
2716 /* Route should have valid RT to be even considered. */
2717 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2720 ecom
= attr
->ecommunity
;
2721 if (!ecom
|| !ecom
->size
)
2724 /* For each extended community RT, see if it matches this VNI. If any RT
2725 * matches, we're done.
2727 for (i
= 0; i
< ecom
->size
; i
++) {
2729 uint8_t type
, sub_type
;
2730 struct ecommunity_val
*eval
;
2731 struct ecommunity_val eval_tmp
;
2732 struct vrf_irt_node
*irt
;
2734 /* Only deal with RTs */
2735 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
2736 eval
= (struct ecommunity_val
*)(ecom
->val
2737 + (i
* ecom
->unit_size
));
2740 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2743 /* See if this RT matches specified VNIs import RTs */
2744 irt
= lookup_vrf_import_rt(eval
);
2746 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2749 /* Also check for non-exact match. In this, we mask out the AS
2751 * only check on the local-admin sub-field. This is to
2753 * VNI as the RT for EBGP peering too.
2756 if (type
== ECOMMUNITY_ENCODE_AS
2757 || type
== ECOMMUNITY_ENCODE_AS4
2758 || type
== ECOMMUNITY_ENCODE_IP
) {
2759 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
2760 mask_ecom_global_admin(&eval_tmp
, eval
);
2761 irt
= lookup_vrf_import_rt(&eval_tmp
);
2764 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2772 * Given a route entry and a VNI, see if this route entry should be
2773 * imported into the VNI i.e., RTs match.
2775 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2776 struct bgp_path_info
*pi
)
2778 struct attr
*attr
= pi
->attr
;
2779 struct ecommunity
*ecom
;
2783 /* Route should have valid RT to be even considered. */
2784 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2787 ecom
= attr
->ecommunity
;
2788 if (!ecom
|| !ecom
->size
)
2791 /* For each extended community RT, see if it matches this VNI. If any RT
2792 * matches, we're done.
2794 for (i
= 0; i
< ecom
->size
; i
++) {
2796 uint8_t type
, sub_type
;
2797 struct ecommunity_val
*eval
;
2798 struct ecommunity_val eval_tmp
;
2799 struct irt_node
*irt
;
2801 /* Only deal with RTs */
2802 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
2803 eval
= (struct ecommunity_val
*)(ecom
->val
2804 + (i
* ecom
->unit_size
));
2807 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2810 /* See if this RT matches specified VNIs import RTs */
2811 irt
= lookup_import_rt(bgp
, eval
);
2813 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2816 /* Also check for non-exact match. In this, we mask out the AS
2818 * only check on the local-admin sub-field. This is to
2820 * VNI as the RT for EBGP peering too.
2823 if (type
== ECOMMUNITY_ENCODE_AS
2824 || type
== ECOMMUNITY_ENCODE_AS4
2825 || type
== ECOMMUNITY_ENCODE_IP
) {
2826 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
2827 mask_ecom_global_admin(&eval_tmp
, eval
);
2828 irt
= lookup_import_rt(bgp
, &eval_tmp
);
2831 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
2838 /* This API will scan evpn routes for checking attribute's rmac
2839 * macthes with bgp instance router mac. It avoid installing
2840 * route into bgp vrf table and remote rmac in bridge table.
2842 static int bgp_evpn_route_rmac_self_check(struct bgp
*bgp_vrf
,
2843 const struct prefix_evpn
*evp
,
2844 struct bgp_path_info
*pi
)
2846 /* evpn route could have learnt prior to L3vni has come up,
2847 * perform rmac check before installing route and
2848 * remote router mac.
2849 * The route will be removed from global bgp table once
2850 * SVI comes up with MAC and stored in hash, triggers
2851 * bgp_mac_rescan_all_evpn_tables.
2853 if (memcmp(&bgp_vrf
->rmac
, &pi
->attr
->rmac
, ETH_ALEN
) == 0) {
2854 if (bgp_debug_update(pi
->peer
, NULL
, NULL
, 1)) {
2855 char attr_str
[BUFSIZ
] = {0};
2857 bgp_dump_attr(pi
->attr
, attr_str
, sizeof(attr_str
));
2860 "%s: bgp %u prefix %pFX with attr %s - DENIED due to self mac",
2861 __func__
, bgp_vrf
->vrf_id
, evp
, attr_str
);
2870 /* don't import hosts that are locally attached */
2872 bgp_evpn_skip_vrf_import_of_local_es(const struct prefix_evpn
*evp
,
2873 struct bgp_path_info
*pi
, int install
)
2878 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
2879 esi
= bgp_evpn_attr_get_esi(pi
->attr
);
2881 /* Don't import routes that point to a local destination */
2882 if (bgp_evpn_attr_is_local_es(pi
->attr
)) {
2883 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
2884 char esi_buf
[ESI_STR_LEN
];
2887 "vrf %s of evpn prefix %pFX skipped, local es %s",
2888 install
? "import" : "unimport", evp
,
2889 esi_to_str(esi
, esi_buf
,
2895 /* Don't import routes with ES as destination if the nexthop
2896 * has not been advertised via the EAD-ES
2899 nh
= pi
->attr
->nexthop
;
2901 nh
.s_addr
= INADDR_ANY
;
2902 if (install
&& !bgp_evpn_es_is_vtep_active(esi
, nh
)) {
2903 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
2904 char esi_buf
[ESI_STR_LEN
];
2907 "vrf %s of evpn prefix %pFX skipped, nh %pI4 inactive in es %s",
2908 install
? "import" : "unimport", evp
,
2910 esi_to_str(esi
, esi_buf
,
2920 * Install or uninstall mac-ip routes are appropriate for this
2923 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
2927 struct bgp_dest
*rd_dest
, *dest
;
2928 struct bgp_table
*table
;
2929 struct bgp_path_info
*pi
;
2931 struct bgp
*bgp_evpn
= NULL
;
2935 bgp_evpn
= bgp_get_evpn();
2939 /* Walk entire global routing table and evaluate routes which could be
2940 * imported into this VRF. Note that we need to loop through all global
2941 * routes to determine which route matches the import rt on vrf
2943 for (rd_dest
= bgp_table_top(bgp_evpn
->rib
[afi
][safi
]); rd_dest
;
2944 rd_dest
= bgp_route_next(rd_dest
)) {
2945 table
= bgp_dest_get_bgp_table_info(rd_dest
);
2949 for (dest
= bgp_table_top(table
); dest
;
2950 dest
= bgp_route_next(dest
)) {
2951 const struct prefix_evpn
*evp
=
2952 (const struct prefix_evpn
*)bgp_dest_get_prefix(
2955 /* if not mac-ip route skip this route */
2956 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2957 || evp
->prefix
.route_type
2958 == BGP_EVPN_IP_PREFIX_ROUTE
))
2961 /* if not a mac+ip route skip this route */
2962 if (!(is_evpn_prefix_ipaddr_v4(evp
)
2963 || is_evpn_prefix_ipaddr_v6(evp
)))
2966 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
2968 /* Consider "valid" remote routes applicable for
2971 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
2972 && pi
->type
== ZEBRA_ROUTE_BGP
2973 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
2976 /* don't import hosts that are locally attached
2978 if (bgp_evpn_skip_vrf_import_of_local_es(
2982 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
2983 if (bgp_evpn_route_rmac_self_check(
2988 ret
= install_evpn_route_entry_in_vrf(
2991 ret
= uninstall_evpn_route_entry_in_vrf(
2997 "Failed to %s EVPN %pFX route in VRF %s",
3003 bgp_dest_unlock_node(rd_dest
);
3004 bgp_dest_unlock_node(dest
);
3016 * Install or uninstall routes of specified type that are appropriate for this
3019 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
3020 struct bgpevpn
*vpn
,
3021 bgp_evpn_route_type rtype
,
3026 struct bgp_dest
*rd_dest
, *dest
;
3027 struct bgp_table
*table
;
3028 struct bgp_path_info
*pi
;
3034 /* Walk entire global routing table and evaluate routes which could be
3035 * imported into this VPN. Note that we cannot just look at the routes
3037 * the VNI's RD - remote routes applicable for this VNI could have any
3040 /* EVPN routes are a 2-level table. */
3041 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
3042 rd_dest
= bgp_route_next(rd_dest
)) {
3043 table
= bgp_dest_get_bgp_table_info(rd_dest
);
3047 for (dest
= bgp_table_top(table
); dest
;
3048 dest
= bgp_route_next(dest
)) {
3049 const struct prefix_evpn
*evp
=
3050 (const struct prefix_evpn
*)bgp_dest_get_prefix(
3053 if (evp
->prefix
.route_type
!= rtype
)
3056 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
3058 /* Consider "valid" remote routes applicable for
3060 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3061 && pi
->type
== ZEBRA_ROUTE_BGP
3062 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3065 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3067 ret
= install_evpn_route_entry(
3070 ret
= uninstall_evpn_route_entry(
3076 "%u: Failed to %s EVPN %s route in VNI %u",
3080 rtype
== BGP_EVPN_MAC_IP_ROUTE
3085 bgp_dest_unlock_node(rd_dest
);
3086 bgp_dest_unlock_node(dest
);
3097 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3098 * is invoked upon l3vni-add or l3vni import rt change
3100 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3102 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3107 * Install any existing remote routes applicable for this VNI into its
3108 * routing table. This is invoked when a VNI becomes "live" or its Import
3111 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3115 /* Install type-3 routes followed by type-2 routes - the ones applicable
3118 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3123 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_AD_ROUTE
,
3128 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3132 /* uninstall routes from l3vni vrf. */
3133 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3135 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3140 * Uninstall any existing remote routes for this VNI. One scenario in which
3141 * this is invoked is upon an import RT change.
3143 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3147 /* Uninstall type-2 routes followed by type-3 routes - the ones
3151 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3156 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_AD_ROUTE
,
3162 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3167 * Install or uninstall route in matching VRFs (list).
3169 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3170 safi_t safi
, struct prefix_evpn
*evp
,
3171 struct bgp_path_info
*pi
,
3172 struct list
*vrfs
, int install
)
3174 struct bgp
*bgp_vrf
;
3175 struct listnode
*node
, *nnode
;
3177 /* Only type-2/type-5 routes go into a VRF */
3178 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3179 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3182 /* if it is type-2 route and not a mac+ip route skip this route */
3183 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3184 && !(is_evpn_prefix_ipaddr_v4(evp
)
3185 || is_evpn_prefix_ipaddr_v6(evp
)))
3188 /* don't import hosts that are locally attached */
3189 if (bgp_evpn_skip_vrf_import_of_local_es(evp
, pi
, install
))
3192 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3196 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3198 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3202 flog_err(EC_BGP_EVPN_FAIL
,
3203 "%u: Failed to %s prefix %pFX in VRF %s",
3205 install
? "install" : "uninstall", evp
,
3206 vrf_id_to_name(bgp_vrf
->vrf_id
));
3215 * Install or uninstall route in matching VNIs (list).
3217 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3218 safi_t safi
, struct prefix_evpn
*evp
,
3219 struct bgp_path_info
*pi
,
3220 struct list
*vnis
, int install
)
3222 struct bgpevpn
*vpn
;
3223 struct listnode
*node
, *nnode
;
3225 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3228 if (!is_vni_live(vpn
))
3232 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3234 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3237 flog_err(EC_BGP_EVPN_FAIL
,
3238 "%u: Failed to %s EVPN %s route in VNI %u",
3239 bgp
->vrf_id
, install
? "install" : "uninstall",
3240 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3252 * Install or uninstall route for appropriate VNIs/ESIs.
3254 static int bgp_evpn_install_uninstall_table(struct bgp
*bgp
, afi_t afi
,
3255 safi_t safi
, const struct prefix
*p
,
3256 struct bgp_path_info
*pi
,
3257 int import
, bool in_vni_rt
,
3260 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3261 struct attr
*attr
= pi
->attr
;
3262 struct ecommunity
*ecom
;
3264 struct prefix_evpn ad_evp
;
3268 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3269 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3270 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3271 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3272 || evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
3273 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3276 /* If we don't have Route Target, nothing much to do. */
3277 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3280 /* EAD prefix in the global table doesn't include the VTEP-IP so
3281 * we need to create a different copy for the VNI
3283 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
3284 evp
= evpn_type1_prefix_vni_copy(&ad_evp
, evp
, attr
->nexthop
);
3286 ecom
= attr
->ecommunity
;
3287 if (!ecom
|| !ecom
->size
)
3290 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3291 * attached to the route */
3292 for (i
= 0; i
< ecom
->size
; i
++) {
3294 uint8_t type
, sub_type
;
3295 struct ecommunity_val
*eval
;
3296 struct ecommunity_val eval_tmp
;
3297 struct irt_node
*irt
; /* import rt for l2vni */
3298 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3299 struct bgp_evpn_es
*es
;
3301 /* Only deal with RTs */
3302 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
3303 eval
= (struct ecommunity_val
*)(ecom
->val
3304 + (i
* ecom
->unit_size
));
3307 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3311 * macip routes (type-2) are imported into VNI and VRF tables.
3312 * IMET route is imported into VNI table.
3313 * prefix routes are imported into VRF table.
3315 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3316 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3317 evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
||
3318 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3320 irt
= in_vni_rt
? lookup_import_rt(bgp
, eval
) : NULL
;
3322 install_uninstall_route_in_vnis(
3323 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3326 vrf_irt
= in_vrf_rt
? lookup_vrf_import_rt(eval
) : NULL
;
3328 install_uninstall_route_in_vrfs(
3329 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3332 /* Also check for non-exact match.
3333 * In this, we mask out the AS and
3334 * only check on the local-admin sub-field.
3335 * This is to facilitate using
3336 * VNI as the RT for EBGP peering too.
3340 if (type
== ECOMMUNITY_ENCODE_AS
3341 || type
== ECOMMUNITY_ENCODE_AS4
3342 || type
== ECOMMUNITY_ENCODE_IP
) {
3343 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
3344 mask_ecom_global_admin(&eval_tmp
, eval
);
3346 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3349 lookup_vrf_import_rt(&eval_tmp
);
3353 install_uninstall_route_in_vnis(
3354 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3357 install_uninstall_route_in_vrfs(
3358 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3362 /* es route is imported into the es table */
3363 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3365 /* we will match based on the entire esi to avoid
3366 * imoort of an es route for esi2 into esi1
3368 es
= bgp_evpn_es_find(&evp
->prefix
.es_addr
.esi
);
3369 if (es
&& bgp_evpn_is_es_local(es
))
3370 bgp_evpn_es_route_install_uninstall(
3371 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3379 * Install or uninstall route for appropriate VNIs/ESIs.
3381 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3382 const struct prefix
*p
,
3383 struct bgp_path_info
*pi
, int import
)
3385 return bgp_evpn_install_uninstall_table(bgp
, afi
, safi
, p
, pi
, import
,
3389 /* Import the pi into vrf routing tables */
3390 void bgp_evpn_import_route_in_vrfs(struct bgp_path_info
*pi
, int import
)
3392 struct bgp
*bgp_evpn
;
3394 bgp_evpn
= bgp_get_evpn();
3398 bgp_evpn_install_uninstall_table(bgp_evpn
, AFI_L2VPN
, SAFI_EVPN
,
3399 &pi
->net
->p
, pi
, import
, false /*vpn*/,
3404 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3407 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3409 /* Delete ipv4 default route and withdraw from peers */
3410 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3411 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3412 SAFI_UNICAST
, false);
3414 /* delete all ipv4 routes and withdraw from peers */
3415 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3416 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3418 /* Delete ipv6 default route and withdraw from peers */
3419 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3420 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3421 SAFI_UNICAST
, false);
3423 /* delete all ipv6 routes and withdraw from peers */
3424 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3425 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3429 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3432 void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3434 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
3436 bgp_evpn
= bgp_get_evpn();
3440 /* update all ipv4 routes */
3441 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3442 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3444 /* update ipv4 default route and withdraw from peers */
3445 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3446 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3447 SAFI_UNICAST
, true);
3449 /* update all ipv6 routes */
3450 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3451 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3453 /* update ipv6 default route and withdraw from peers */
3454 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3455 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3456 SAFI_UNICAST
, true);
3461 * update and advertise local routes for a VRF as type-5 routes.
3462 * This is invoked upon RD change for a VRF. Note taht the processing is only
3463 * done in the global route table using the routes which already exist in the
3466 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3468 /* skip if the RD is configured */
3469 if (is_vrf_rd_configured(bgp_vrf
))
3472 /* derive the RD for the VRF based on new router-id */
3473 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3475 /* update advertise ipv4|ipv6 routes as type-5 routes */
3476 update_advertise_vrf_routes(bgp_vrf
);
3480 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3481 * This is invoked upon VRF RD change. The processing is done only from global
3484 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3486 /* skip if the RD is configured */
3487 if (is_vrf_rd_configured(bgp_vrf
))
3490 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3491 delete_withdraw_vrf_routes(bgp_vrf
);
3495 * Update and advertise local routes for a VNI. Invoked upon router-id
3496 * change. Note that the processing is done only on the global route table
3497 * using routes that already exist in the per-VNI table.
3499 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3501 struct prefix_evpn p
;
3502 struct bgp_dest
*dest
, *global_dest
;
3503 struct bgp_path_info
*pi
, *global_pi
;
3505 afi_t afi
= AFI_L2VPN
;
3506 safi_t safi
= SAFI_EVPN
;
3508 /* Locate type-3 route for VNI in the per-VNI table and use its
3509 * attributes to create and advertise the type-3 route for this VNI
3510 * in the global table.
3512 * RT-3 only if doing head-end replication
3514 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3515 == VXLAN_FLOOD_HEAD_END_REPL
) {
3516 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3517 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3518 if (!dest
) /* unexpected */
3520 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
3521 if (pi
->peer
== bgp
->peer_self
&&
3522 pi
->type
== ZEBRA_ROUTE_BGP
3523 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3525 if (!pi
) /* unexpected */
3529 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
],
3530 afi
, safi
, &p
, &vpn
->prd
);
3531 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_dest
, attr
,
3532 1, &pi
, 0, mac_mobility_seqnum(attr
),
3533 false /* setup_sync */, NULL
/* old_is_sync */);
3535 /* Schedule for processing and unlock node. */
3536 bgp_process(bgp
, global_dest
, afi
, safi
);
3537 bgp_dest_unlock_node(global_dest
);
3540 /* Now, walk this VNI's route table and use the route and its attribute
3541 * to create and schedule route in global table.
3543 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
3544 dest
= bgp_route_next(dest
)) {
3545 const struct prefix_evpn
*evp
=
3546 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
3548 /* Identify MAC-IP local routes. */
3549 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3552 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
3553 if (pi
->peer
== bgp
->peer_self
3554 && pi
->type
== ZEBRA_ROUTE_BGP
3555 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3560 /* Create route in global routing table using this route entry's
3564 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3566 assert(global_dest
);
3567 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_dest
, attr
, 1,
3569 mac_mobility_seqnum(attr
),
3570 false /* setup_sync */, NULL
/* old_is_sync */);
3572 /* Schedule for processing and unlock node. */
3573 bgp_process(bgp
, global_dest
, afi
, safi
);
3574 bgp_dest_unlock_node(global_dest
);
3581 * Delete (and withdraw) local routes for a VNI - only from the global
3582 * table. Invoked upon router-id change.
3584 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3587 struct prefix_evpn p
;
3588 struct bgp_dest
*global_dest
;
3589 struct bgp_path_info
*pi
;
3590 afi_t afi
= AFI_L2VPN
;
3591 safi_t safi
= SAFI_EVPN
;
3593 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3594 * for this VNI - from the global table.
3596 ret
= delete_global_type2_routes(bgp
, vpn
);
3600 /* Remove type-3 route for this VNI from global table. */
3601 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3602 global_dest
= bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3603 (const struct prefix_evpn
*)&p
, &vpn
->prd
);
3605 /* Delete route entry in the global EVPN table. */
3606 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
3608 /* Schedule for processing - withdraws to peers happen from
3612 bgp_process(bgp
, global_dest
, afi
, safi
);
3613 bgp_dest_unlock_node(global_dest
);
3620 * Handle router-id change. Update and advertise local routes corresponding
3621 * to this VNI from peers. Note that this is invoked after updating the
3622 * router-id. The routes in the per-VNI table are used to create routes in
3623 * the global table and schedule them.
3625 static void update_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3627 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3629 /* Skip VNIs with configured RD. */
3630 if (is_rd_configured(vpn
))
3633 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3634 update_advertise_vni_routes(bgp
, vpn
);
3638 * Handle router-id change. Delete and withdraw local routes corresponding
3639 * to this VNI from peers. Note that this is invoked prior to updating
3640 * the router-id and is done only on the global route table, the routes
3641 * are needed in the per-VNI table to re-advertise with new router id.
3643 static void withdraw_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3645 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3647 /* Skip VNIs with configured RD. */
3648 if (is_rd_configured(vpn
))
3651 delete_withdraw_vni_routes(bgp
, vpn
);
3655 * Create RT-3 for a VNI and schedule for processing and advertisement.
3656 * This is invoked upon flooding mode changing to head-end replication.
3658 static void create_advertise_type3(struct hash_bucket
*bucket
, void *data
)
3660 struct bgpevpn
*vpn
= bucket
->data
;
3661 struct bgp
*bgp
= data
;
3662 struct prefix_evpn p
;
3664 if (!vpn
|| !is_vni_live(vpn
) ||
3665 bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3666 != VXLAN_FLOOD_HEAD_END_REPL
)
3669 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3670 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
))
3671 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3672 "Type3 route creation failure for VNI %u", vpn
->vni
);
3676 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3677 * This is invoked upon flooding mode changing to drop BUM packets.
3679 static void delete_withdraw_type3(struct hash_bucket
*bucket
, void *data
)
3681 struct bgpevpn
*vpn
= bucket
->data
;
3682 struct bgp
*bgp
= data
;
3683 struct prefix_evpn p
;
3685 if (!vpn
|| !is_vni_live(vpn
))
3688 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3689 delete_evpn_route(bgp
, vpn
, &p
);
3693 * Process received EVPN type-2 route (advertise or withdraw).
3695 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3696 struct attr
*attr
, uint8_t *pfx
, int psize
,
3697 uint32_t addpath_id
)
3699 struct prefix_rd prd
;
3700 struct prefix_evpn p
= {};
3701 struct bgp_route_evpn evpn
= {};
3703 uint8_t macaddr_len
;
3704 /* holds the VNI(s) as in packet */
3705 mpls_label_t label
[BGP_MAX_LABELS
] = {};
3706 uint32_t num_labels
= 0;
3710 /* Type-2 route should be either 33, 37 or 49 bytes or an
3711 * additional 3 bytes if there is a second label (VNI):
3712 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3713 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3714 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3716 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3717 && psize
!= 40 && psize
!= 52) {
3718 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3719 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3720 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3724 struct stream
*pkt
= stream_new(psize
);
3725 stream_put(pkt
, pfx
, psize
);
3727 /* Make prefix_rd */
3728 prd
.family
= AF_UNSPEC
;
3731 STREAM_GET(&prd
.val
, pkt
, 8);
3733 /* Make EVPN prefix. */
3735 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3736 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3738 /* Copy Ethernet Seg Identifier */
3740 STREAM_GET(&attr
->esi
, pkt
, sizeof(esi_t
));
3742 if (bgp_evpn_is_esi_local(&attr
->esi
))
3743 attr
->es_flags
|= ATTR_ES_IS_LOCAL
;
3745 attr
->es_flags
&= ~ATTR_ES_IS_LOCAL
;
3747 STREAM_FORWARD_GETP(pkt
, sizeof(esi_t
));
3750 /* Copy Ethernet Tag */
3751 STREAM_GET(ð_tag
, pkt
, 4);
3752 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3754 /* Get the MAC Addr len */
3755 STREAM_GETC(pkt
, macaddr_len
);
3757 /* Get the MAC Addr */
3758 if (macaddr_len
== (ETH_ALEN
* 8)) {
3759 STREAM_GET(&p
.prefix
.macip_addr
.mac
.octet
, pkt
, ETH_ALEN
);
3762 EC_BGP_EVPN_ROUTE_INVALID
,
3763 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3764 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3770 STREAM_GETC(pkt
, ipaddr_len
);
3772 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3773 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3775 EC_BGP_EVPN_ROUTE_INVALID
,
3776 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3777 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3782 ipaddr_len
/= 8; /* Convert to bytes. */
3783 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3786 STREAM_GET(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pkt
, ipaddr_len
);
3789 /* Get the VNI(s). Stored as bytes here. */
3790 STREAM_GET(&label
[0], pkt
, BGP_LABEL_BYTES
);
3793 /* Do we have a second VNI? */
3794 if (STREAM_READABLE(pkt
)) {
3796 STREAM_GET(&label
[1], pkt
, BGP_LABEL_BYTES
);
3799 /* Process the route. */
3801 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3802 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3803 &prd
, &label
[0], num_labels
, 0, &evpn
);
3805 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3806 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3807 &prd
, &label
[0], num_labels
, &evpn
);
3812 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3813 "%u:%s - Rx EVPN Type-2 NLRI - corrupt, discarding",
3814 peer
->bgp
->vrf_id
, peer
->host
);
3822 * Process received EVPN type-3 route (advertise or withdraw).
3824 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3825 struct attr
*attr
, uint8_t *pfx
, int psize
,
3826 uint32_t addpath_id
)
3828 struct prefix_rd prd
;
3829 struct prefix_evpn p
;
3834 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3835 * IP len (1) and IP (4 or 16).
3837 if (psize
!= 17 && psize
!= 29) {
3838 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3839 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3840 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3844 /* If PMSI is present, log if it is anything other than IR.
3845 * Note: We just simply ignore the values as it is not clear if
3846 * doing anything else is better.
3849 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3850 enum pta_type pmsi_tnl_type
= bgp_attr_get_pmsi_tnl_type(attr
);
3852 if (pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
3853 && pmsi_tnl_type
!= PMSI_TNLTYPE_PIM_SM
) {
3855 EC_BGP_EVPN_PMSI_PRESENT
,
3856 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3857 peer
->bgp
->vrf_id
, peer
->host
, pmsi_tnl_type
);
3861 /* Make prefix_rd */
3862 prd
.family
= AF_UNSPEC
;
3864 memcpy(&prd
.val
, pfx
, 8);
3867 /* Make EVPN prefix. */
3868 memset(&p
, 0, sizeof(struct prefix_evpn
));
3870 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3871 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3873 /* Copy Ethernet Tag */
3874 memcpy(ð_tag
, pfx
, 4);
3875 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
3879 ipaddr_len
= *pfx
++;
3880 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3881 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
3882 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
3885 EC_BGP_EVPN_ROUTE_INVALID
,
3886 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3887 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3891 /* Process the route. */
3893 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3894 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3895 &prd
, NULL
, 0, 0, NULL
);
3897 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3898 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3899 &prd
, NULL
, 0, NULL
);
3904 * Process received EVPN type-5 route (advertise or withdraw).
3906 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3907 struct attr
*attr
, uint8_t *pfx
, int psize
,
3908 uint32_t addpath_id
)
3910 struct prefix_rd prd
;
3911 struct prefix_evpn p
;
3912 struct bgp_route_evpn evpn
;
3915 mpls_label_t label
; /* holds the VNI as in the packet */
3918 bool is_valid_update
= false;
3920 /* Type-5 route should be 34 or 58 bytes:
3921 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3922 * GW (4 or 16) and VNI (3).
3923 * Note that the IP and GW should both be IPv4 or both IPv6.
3925 if (psize
!= 34 && psize
!= 58) {
3926 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3927 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3928 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3932 /* Make prefix_rd */
3933 prd
.family
= AF_UNSPEC
;
3935 memcpy(&prd
.val
, pfx
, 8);
3938 /* Make EVPN prefix. */
3939 memset(&p
, 0, sizeof(struct prefix_evpn
));
3941 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3942 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
3944 /* Additional information outside of prefix - ESI and GW IP */
3945 memset(&evpn
, 0, sizeof(evpn
));
3949 memcpy(&attr
->esi
, pfx
, sizeof(esi_t
));
3952 /* Fetch Ethernet Tag. */
3953 memcpy(ð_tag
, pfx
, 4);
3954 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
3957 /* Fetch IP prefix length. */
3959 if (ippfx_len
> IPV6_MAX_BITLEN
) {
3961 EC_BGP_EVPN_ROUTE_INVALID
,
3962 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3963 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
3966 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
3968 /* Determine IPv4 or IPv6 prefix */
3969 /* Since the address and GW are from the same family, this just becomes
3970 * a simple check on the total size.
3973 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
3974 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
3976 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
3980 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
3981 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
3983 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
3988 /* Get the VNI (in MPLS label field). Stored as bytes here. */
3989 memset(&label
, 0, sizeof(label
));
3990 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
3993 * If in future, we are required to access additional fields,
3994 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
3999 is_valid_update
= true;
4000 if (is_zero_mac(&attr
->rmac
) &&
4001 is_zero_gw_ip(&evpn
.gw_ip
, gw_afi
))
4002 is_valid_update
= false;
4004 if (is_mcast_mac(&attr
->rmac
) || is_bcast_mac(&attr
->rmac
))
4005 is_valid_update
= false;
4008 /* Process the route. */
4009 if (is_valid_update
)
4010 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4011 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4012 &prd
, &label
, 1, 0, &evpn
);
4014 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4015 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4016 &prd
, &label
, 1, &evpn
);
4021 static void evpn_mpattr_encode_type5(struct stream
*s
, const struct prefix
*p
,
4022 const struct prefix_rd
*prd
,
4023 mpls_label_t
*label
, uint32_t num_labels
,
4028 const struct evpn_addr
*p_evpn_p
;
4030 memset(&temp
, 0, 16);
4031 if (p
->family
!= AF_EVPN
)
4033 p_evpn_p
= &(p
->u
.prefix_evpn
);
4035 /* len denites the total len of IP and GW-IP in the route
4036 IP and GW-IP have to be both ipv4 or ipv6
4038 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4039 len
= 8; /* IP and GWIP are both ipv4 */
4041 len
= 32; /* IP and GWIP are both ipv6 */
4042 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4043 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4044 stream_put(s
, prd
->val
, 8);
4046 stream_put(s
, &attr
->esi
, sizeof(esi_t
));
4048 stream_put(s
, 0, sizeof(esi_t
));
4049 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4050 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4051 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4052 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4054 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4056 const struct bgp_route_evpn
*evpn_overlay
=
4057 bgp_attr_get_evpn_overlay(attr
);
4059 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4060 stream_put_ipv4(s
, evpn_overlay
->gw_ip
.ipv4
.s_addr
);
4062 stream_put(s
, &(evpn_overlay
->gw_ip
.ipv6
), 16);
4064 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4065 stream_put_ipv4(s
, 0);
4067 stream_put(s
, &temp
, 16);
4071 stream_put(s
, label
, 3);
4077 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4079 static void cleanup_vni_on_disable(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4081 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4083 /* Remove EVPN routes and schedule for processing. */
4084 delete_routes_for_vni(bgp
, vpn
);
4086 /* Clear "live" flag and see if hash needs to be freed. */
4087 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4088 if (!is_vni_configured(vpn
))
4089 bgp_evpn_free(bgp
, vpn
);
4093 * Free a VNI entry; iterator function called during cleanup.
4095 static void free_vni_entry(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4097 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4099 delete_all_vni_routes(bgp
, vpn
);
4100 bgp_evpn_free(bgp
, vpn
);
4104 * Derive AUTO import RT for BGP VRF - L3VNI
4106 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4108 struct bgp
*bgp_evpn
= NULL
;
4110 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4111 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4114 bgp_evpn
= bgp_get_evpn();
4117 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4121 * Delete AUTO import RT from BGP VRF - L3VNI
4123 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4125 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4129 * Derive AUTO export RT for BGP VRF - L3VNI
4131 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4133 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4134 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4138 * Delete AUTO export RT from BGP VRF - L3VNI
4140 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4142 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4145 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4147 struct bgp
*bgp_evpn
= NULL
;
4148 struct listnode
*node
= NULL
;
4149 struct bgpevpn
*vpn
= NULL
;
4151 bgp_evpn
= bgp_get_evpn();
4155 /* update all type-5 routes */
4156 update_advertise_vrf_routes(bgp_vrf
);
4158 /* update all type-2 routes */
4159 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4160 update_routes_for_vni(bgp_evpn
, vpn
);
4164 * Handle autort change for a given VNI.
4166 static void update_autort_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4168 struct bgpevpn
*vpn
= bucket
->data
;
4170 if (!is_import_rt_configured(vpn
)) {
4171 if (is_vni_live(vpn
))
4172 bgp_evpn_uninstall_routes(bgp
, vpn
);
4173 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4174 list_delete_all_node(vpn
->import_rtl
);
4175 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4176 if (is_vni_live(vpn
))
4177 bgp_evpn_install_routes(bgp
, vpn
);
4179 if (!is_export_rt_configured(vpn
)) {
4180 list_delete_all_node(vpn
->export_rtl
);
4181 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4182 if (is_vni_live(vpn
))
4183 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4191 /* withdraw type-5 route corresponding to ip prefix */
4192 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4193 afi_t afi
, safi_t safi
)
4196 struct prefix_evpn evp
;
4198 build_type5_prefix_from_ip_prefix(&evp
, p
);
4199 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4202 EC_BGP_EVPN_ROUTE_DELETE
,
4203 "%u failed to delete type-5 route for prefix %pFX in vrf %s",
4204 bgp_vrf
->vrf_id
, p
, vrf_id_to_name(bgp_vrf
->vrf_id
));
4207 /* withdraw all type-5 routes for an address family */
4208 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4210 struct bgp_table
*table
= NULL
;
4211 struct bgp_dest
*dest
= NULL
;
4212 struct bgp_path_info
*pi
;
4214 table
= bgp_vrf
->rib
[afi
][safi
];
4215 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
4216 /* Only care about "selected" routes. Also ensure that
4217 * these are routes that are injectable into EVPN.
4219 /* TODO: Support for AddPath for EVPN. */
4220 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
4221 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4222 && is_route_injectable_into_evpn(pi
)) {
4223 bgp_evpn_withdraw_type5_route(
4224 bgp_vrf
, bgp_dest_get_prefix(dest
), afi
,
4233 * evpn - enable advertisement of default g/w
4235 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
4236 safi_t safi
, bool add
)
4238 struct prefix ip_prefix
;
4240 /* form the default prefix 0.0.0.0/0 */
4241 memset(&ip_prefix
, 0, sizeof(struct prefix
));
4242 ip_prefix
.family
= afi2family(afi
);
4245 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
4248 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
4255 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4256 * to this function correspond to those of the source IP prefix (best
4257 * path in the case of the attr. In the case of a local prefix (when we
4258 * are advertising local subnets), the src_attr will be NULL.
4260 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4261 struct attr
*src_attr
, afi_t afi
,
4265 struct prefix_evpn evp
;
4267 build_type5_prefix_from_ip_prefix(&evp
, p
);
4268 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4270 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4271 "%u: Failed to create type-5 route for prefix %pFX",
4272 bgp_vrf
->vrf_id
, p
);
4275 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4276 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4277 * advertisement is enabled.
4279 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4282 struct bgp_table
*table
= NULL
;
4283 struct bgp_dest
*dest
= NULL
;
4284 struct bgp_path_info
*pi
;
4286 table
= bgp_vrf
->rib
[afi
][safi
];
4287 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
4288 /* Need to identify the "selected" route entry to use its
4289 * attribute. Also, ensure that the route is injectable
4291 * TODO: Support for AddPath for EVPN.
4293 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
4294 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4295 && is_route_injectable_into_evpn(pi
)) {
4297 /* apply the route-map */
4298 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4299 route_map_result_t ret
;
4300 struct bgp_path_info tmp_pi
;
4301 struct bgp_path_info_extra tmp_pie
;
4302 struct attr tmp_attr
;
4304 tmp_attr
= *pi
->attr
;
4306 /* Fill temp path_info */
4307 prep_for_rmap_apply(&tmp_pi
, &tmp_pie
,
4311 RESET_FLAG(tmp_attr
.rmap_change_flags
);
4313 ret
= route_map_apply(
4314 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4316 bgp_dest_get_prefix(dest
),
4318 if (ret
== RMAP_DENYMATCH
) {
4319 bgp_attr_flush(&tmp_attr
);
4322 bgp_evpn_advertise_type5_route(
4324 bgp_dest_get_prefix(dest
),
4325 &tmp_attr
, afi
, safi
);
4327 bgp_evpn_advertise_type5_route(
4329 bgp_dest_get_prefix(dest
),
4330 pi
->attr
, afi
, safi
);
4337 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4339 struct listnode
*node
, *nnode
, *node_to_del
;
4340 struct ecommunity
*ecom
, *ecom_auto
;
4341 struct ecommunity_val eval
;
4343 if (bgp
->advertise_autort_rfc8365
)
4344 vni
|= EVPN_AUTORT_VXLAN
;
4345 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4347 ecom_auto
= ecommunity_new();
4348 ecommunity_add_val(ecom_auto
, &eval
, false, false);
4351 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4352 if (ecommunity_match(ecom
, ecom_auto
)) {
4353 ecommunity_free(&ecom
);
4359 list_delete_node(rtl
, node_to_del
);
4361 ecommunity_free(&ecom_auto
);
4364 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4365 struct ecommunity
*ecomadd
)
4367 /* uninstall routes from vrf */
4368 if (is_l3vni_live(bgp_vrf
))
4369 uninstall_routes_for_vrf(bgp_vrf
);
4371 /* Cleanup the RT to VRF mapping */
4372 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4374 /* Remove auto generated RT */
4375 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4377 /* Add the newly configured RT to RT list */
4378 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4379 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4381 /* map VRF to its RTs and install routes matching the new RTs */
4382 if (is_l3vni_live(bgp_vrf
)) {
4383 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4384 install_routes_for_vrf(bgp_vrf
);
4388 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4389 struct ecommunity
*ecomdel
)
4391 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4392 struct ecommunity
*ecom
= NULL
;
4394 /* uninstall routes from vrf */
4395 if (is_l3vni_live(bgp_vrf
))
4396 uninstall_routes_for_vrf(bgp_vrf
);
4398 /* Cleanup the RT to VRF mapping */
4399 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4401 /* remove the RT from the RT list */
4402 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4403 if (ecommunity_match(ecom
, ecomdel
)) {
4404 ecommunity_free(&ecom
);
4411 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4413 assert(bgp_vrf
->vrf_import_rtl
);
4414 /* fallback to auto import rt, if this was the last RT */
4415 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4416 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4417 if (is_l3vni_live(bgp_vrf
))
4418 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4421 /* map VRFs to its RTs and install routes matching this new RT */
4422 if (is_l3vni_live(bgp_vrf
)) {
4423 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4424 install_routes_for_vrf(bgp_vrf
);
4428 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4429 struct ecommunity
*ecomadd
)
4431 /* remove auto-generated RT */
4432 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4434 /* Add the new RT to the RT list */
4435 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4436 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4438 if (is_l3vni_live(bgp_vrf
))
4439 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4442 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4443 struct ecommunity
*ecomdel
)
4445 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4446 struct ecommunity
*ecom
= NULL
;
4448 /* Remove the RT from the RT list */
4449 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4450 if (ecommunity_match(ecom
, ecomdel
)) {
4451 ecommunity_free(&ecom
);
4458 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4461 * Temporary assert to make SA happy.
4462 * The ALL_LIST_ELEMENTS macro above has a NULL check
4463 * which means that SA is going to complain about
4464 * the list_isempty call, which doesn't NULL check.
4465 * So until we get this situation cleaned up, here
4468 assert(bgp_vrf
->vrf_export_rtl
);
4470 /* fall back to auto-generated RT if this was the last RT */
4471 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4472 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4473 if (is_l3vni_live(bgp_vrf
))
4474 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4477 if (is_l3vni_live(bgp_vrf
))
4478 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4482 * Handle change to BGP router id. This is invoked twice by the change
4483 * handler, first before the router id has been changed and then after
4484 * the router id has been changed. The first invocation will result in
4485 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4486 * will result in the routes being re-advertised.
4488 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4490 struct listnode
*node
;
4491 struct bgp
*bgp_vrf
;
4495 /* delete and withdraw all the type-5 routes
4496 stored in the global table for this vrf
4498 withdraw_router_id_vrf(bgp
);
4500 /* delete all the VNI routes (type-2/type-3) routes for all the
4503 hash_iterate(bgp
->vnihash
,
4504 (void (*)(struct hash_bucket
*,
4505 void *))withdraw_router_id_vni
,
4508 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4509 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4510 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4511 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4513 bgp_vrf
->evpn_info
->pip_ip
.s_addr
4519 /* Assign new default instance router-id */
4520 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4521 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4522 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4523 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4525 bgp_vrf
->evpn_info
->pip_ip
=
4527 /* advertise type-5 routes with
4530 update_advertise_vrf_routes(bgp_vrf
);
4535 /* advertise all routes in the vrf as type-5 routes with the new
4538 update_router_id_vrf(bgp
);
4540 /* advertise all the VNI routes (type-2/type-3) routes with the
4543 hash_iterate(bgp
->vnihash
,
4544 (void (*)(struct hash_bucket
*,
4545 void *))update_router_id_vni
,
4551 * Handle change to auto-RT algorithm - update and advertise local routes.
4553 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4555 hash_iterate(bgp
->vnihash
,
4556 (void (*)(struct hash_bucket
*,
4557 void*))update_autort_vni
,
4562 * Handle change to export RT - update and advertise local routes.
4564 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4566 return update_routes_for_vni(bgp
, vpn
);
4569 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4572 delete_withdraw_vrf_routes(bgp_vrf
);
4574 update_advertise_vrf_routes(bgp_vrf
);
4578 * Handle change to RD. This is invoked twice by the change handler,
4579 * first before the RD has been changed and then after the RD has
4580 * been changed. The first invocation will result in local routes
4581 * of this VNI being deleted and withdrawn and the next will result
4582 * in the routes being re-advertised.
4584 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4588 delete_withdraw_vni_routes(bgp
, vpn
);
4590 update_advertise_vni_routes(bgp
, vpn
);
4594 * Install routes for this VNI. Invoked upon change to Import RT.
4596 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4598 return install_routes_for_vni(bgp
, vpn
);
4602 * Uninstall all routes installed for this VNI. Invoked upon change
4605 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4607 return uninstall_routes_for_vni(bgp
, vpn
);
4611 * TODO: Hardcoded for a maximum of 2 VNIs right now
4613 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4618 vni1
= label2vni(label
);
4619 if (num_labels
== 2) {
4620 vni2
= label2vni(label
+ 1);
4621 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4623 snprintf(buf
, len
, "%u", vni1
);
4628 * Function to convert evpn route to json format.
4629 * NOTE: We don't use prefix2str as the output here is a bit different.
4631 void bgp_evpn_route2json(const struct prefix_evpn
*p
, json_object
*json
)
4633 char buf1
[ETHER_ADDR_STRLEN
];
4634 char buf2
[PREFIX2STR_BUFFER
];
4641 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4643 switch (p
->prefix
.route_type
) {
4644 case BGP_EVPN_MAC_IP_ROUTE
:
4645 json_object_int_add(json
, "ethTag",
4646 p
->prefix
.macip_addr
.eth_tag
);
4647 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4648 json_object_string_add(json
, "mac",
4649 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4652 if (!is_evpn_prefix_ipaddr_none(p
)) {
4653 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
:
4655 prefixlen
= (family
== AF_INET
) ?
4656 IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
;
4657 inet_ntop(family
, &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4658 buf2
, PREFIX2STR_BUFFER
);
4659 json_object_int_add(json
, "ipLen", prefixlen
);
4660 json_object_string_add(json
, "ip", buf2
);
4664 case BGP_EVPN_IMET_ROUTE
:
4665 json_object_int_add(json
, "ethTag",
4666 p
->prefix
.imet_addr
.eth_tag
);
4667 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4668 prefixlen
= (family
== AF_INET
) ? IPV4_MAX_BITLEN
:
4670 inet_ntop(family
, &p
->prefix
.imet_addr
.ip
.ip
.addr
, buf2
,
4672 json_object_int_add(json
, "ipLen", prefixlen
);
4673 json_object_string_add(json
, "ip", buf2
);
4676 case BGP_EVPN_IP_PREFIX_ROUTE
:
4677 json_object_int_add(json
, "ethTag",
4678 p
->prefix
.prefix_addr
.eth_tag
);
4679 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4680 inet_ntop(family
, &p
->prefix
.prefix_addr
.ip
.ip
.addr
,
4681 buf2
, sizeof(buf2
));
4682 json_object_int_add(json
, "ipLen",
4683 p
->prefix
.prefix_addr
.ip_prefix_length
);
4684 json_object_string_add(json
, "ip", buf2
);
4693 * Encode EVPN prefix in Update (MP_REACH)
4695 void bgp_evpn_encode_prefix(struct stream
*s
, const struct prefix
*p
,
4696 const struct prefix_rd
*prd
, mpls_label_t
*label
,
4697 uint32_t num_labels
, struct attr
*attr
,
4698 int addpath_encode
, uint32_t addpath_tx_id
)
4700 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4701 int len
, ipa_len
= 0;
4704 stream_putl(s
, addpath_tx_id
);
4707 stream_putc(s
, evp
->prefix
.route_type
);
4709 switch (evp
->prefix
.route_type
) {
4710 case BGP_EVPN_MAC_IP_ROUTE
:
4711 if (is_evpn_prefix_ipaddr_v4(evp
))
4712 ipa_len
= IPV4_MAX_BYTELEN
;
4713 else if (is_evpn_prefix_ipaddr_v6(evp
))
4714 ipa_len
= IPV6_MAX_BYTELEN
;
4715 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4716 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4717 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4719 stream_putc(s
, len
);
4720 stream_put(s
, prd
->val
, 8); /* RD */
4722 stream_put(s
, &attr
->esi
, ESI_BYTES
);
4724 stream_put(s
, 0, 10);
4725 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4726 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4727 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4728 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4729 if (ipa_len
) /* IP */
4730 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4732 /* 1st label is the L2 VNI */
4733 stream_put(s
, label
, BGP_LABEL_BYTES
);
4734 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4735 if (ipa_len
&& num_labels
> 1)
4736 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4739 case BGP_EVPN_IMET_ROUTE
:
4740 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4741 stream_put(s
, prd
->val
, 8); /* RD */
4742 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4743 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4744 /* Originating Router's IP Addr */
4745 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4748 case BGP_EVPN_ES_ROUTE
:
4749 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4750 stream_put(s
, prd
->val
, 8); /* RD */
4751 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4752 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4754 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4757 case BGP_EVPN_AD_ROUTE
:
4758 /* RD, ESI, EthTag, 1 VNI */
4759 len
= RD_BYTES
+ ESI_BYTES
+ EVPN_ETH_TAG_BYTES
+ BGP_LABEL_BYTES
;
4760 stream_putc(s
, len
);
4761 stream_put(s
, prd
->val
, RD_BYTES
); /* RD */
4762 stream_put(s
, evp
->prefix
.ead_addr
.esi
.val
, ESI_BYTES
); /* ESI */
4763 stream_putl(s
, evp
->prefix
.ead_addr
.eth_tag
); /* Ethernet Tag */
4764 stream_put(s
, label
, BGP_LABEL_BYTES
);
4767 case BGP_EVPN_IP_PREFIX_ROUTE
:
4768 /* TODO: AddPath support. */
4769 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4777 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4778 struct bgp_nlri
*packet
, int withdraw
)
4784 uint32_t addpath_id
;
4785 int addpath_encoded
;
4790 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4792 lim
= pnt
+ packet
->length
;
4794 safi
= packet
->safi
;
4798 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
4799 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
4800 PEER_CAP_ADDPATH_AF_TX_RCV
));
4802 for (; pnt
< lim
; pnt
+= psize
) {
4803 /* Clear prefix structure. */
4804 memset(&p
, 0, sizeof(struct prefix
));
4806 /* Deal with path-id if AddPath is supported. */
4807 if (addpath_encoded
) {
4808 /* When packet overflow occurs return immediately. */
4809 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4810 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
4812 memcpy(&addpath_id
, pnt
, BGP_ADDPATH_ID_LEN
);
4813 addpath_id
= ntohl(addpath_id
);
4814 pnt
+= BGP_ADDPATH_ID_LEN
;
4817 /* All EVPN NLRI types start with type and length. */
4819 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE
;
4824 /* When packet overflow occur return immediately. */
4825 if (pnt
+ psize
> lim
)
4826 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
4829 case BGP_EVPN_MAC_IP_ROUTE
:
4830 if (process_type2_route(peer
, afi
, safi
,
4831 withdraw
? NULL
: attr
, pnt
,
4832 psize
, addpath_id
)) {
4835 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4836 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4837 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE
;
4841 case BGP_EVPN_IMET_ROUTE
:
4842 if (process_type3_route(peer
, afi
, safi
,
4843 withdraw
? NULL
: attr
, pnt
,
4844 psize
, addpath_id
)) {
4847 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4848 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4849 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE
;
4853 case BGP_EVPN_ES_ROUTE
:
4854 if (bgp_evpn_type4_route_process(peer
, afi
, safi
,
4855 withdraw
? NULL
: attr
, pnt
,
4856 psize
, addpath_id
)) {
4859 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4860 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4861 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE
;
4865 case BGP_EVPN_AD_ROUTE
:
4866 if (bgp_evpn_type1_route_process(peer
, afi
, safi
,
4867 withdraw
? NULL
: attr
, pnt
,
4868 psize
, addpath_id
)) {
4871 "%u:%s - Error in processing EVPN type-1 NLRI size %d",
4872 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4873 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE1_SIZE
;
4877 case BGP_EVPN_IP_PREFIX_ROUTE
:
4878 if (process_type5_route(peer
, afi
, safi
,
4879 withdraw
? NULL
: attr
, pnt
,
4880 psize
, addpath_id
)) {
4883 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4884 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4885 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE
;
4894 /* Packet length consistency check. */
4896 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH
;
4898 return BGP_NLRI_PARSE_OK
;
4902 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
4903 * The mapping will be used during route processing.
4904 * bgp_def: default bgp instance
4905 * bgp_vrf: specific bgp vrf instance on which RT is configured
4907 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
4910 struct ecommunity_val
*eval
= NULL
;
4911 struct listnode
*node
= NULL
, *nnode
= NULL
;
4912 struct ecommunity
*ecom
= NULL
;
4914 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4915 for (i
= 0; i
< ecom
->size
; i
++) {
4916 eval
= (struct ecommunity_val
*)(ecom
->val
4918 * ECOMMUNITY_SIZE
));
4919 map_vrf_to_rt(bgp_vrf
, eval
);
4925 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
4927 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
4930 struct ecommunity_val
*eval
;
4931 struct listnode
*node
, *nnode
;
4932 struct ecommunity
*ecom
;
4934 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4935 for (i
= 0; i
< ecom
->size
; i
++) {
4936 struct vrf_irt_node
*irt
;
4937 struct ecommunity_val eval_tmp
;
4939 eval
= (struct ecommunity_val
*)(ecom
->val
4941 * ECOMMUNITY_SIZE
));
4942 /* If using "automatic" RT, we only care about the
4943 * local-admin sub-field.
4944 * This is to facilitate using VNI as the RT for EBGP
4947 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4948 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
4949 BGP_VRF_IMPORT_RT_CFGD
))
4950 mask_ecom_global_admin(&eval_tmp
, eval
);
4952 irt
= lookup_vrf_import_rt(&eval_tmp
);
4954 unmap_vrf_from_rt(bgp_vrf
, irt
);
4961 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
4962 * The mapping will be used during route processing.
4964 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4967 struct ecommunity_val
*eval
;
4968 struct listnode
*node
, *nnode
;
4969 struct ecommunity
*ecom
;
4971 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4972 for (i
= 0; i
< ecom
->size
; i
++) {
4973 eval
= (struct ecommunity_val
*)(ecom
->val
4975 * ECOMMUNITY_SIZE
));
4976 map_vni_to_rt(bgp
, vpn
, eval
);
4982 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
4984 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4987 struct ecommunity_val
*eval
;
4988 struct listnode
*node
, *nnode
;
4989 struct ecommunity
*ecom
;
4991 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4992 for (i
= 0; i
< ecom
->size
; i
++) {
4993 struct irt_node
*irt
;
4994 struct ecommunity_val eval_tmp
;
4996 eval
= (struct ecommunity_val
*)(ecom
->val
4998 * ECOMMUNITY_SIZE
));
4999 /* If using "automatic" RT, we only care about the
5000 * local-admin sub-field.
5001 * This is to facilitate using VNI as the RT for EBGP
5004 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5005 if (!is_import_rt_configured(vpn
))
5006 mask_ecom_global_admin(&eval_tmp
, eval
);
5008 irt
= lookup_import_rt(bgp
, &eval_tmp
);
5010 unmap_vni_from_rt(bgp
, vpn
, irt
);
5016 * Derive Import RT automatically for VNI and map VNI to RT.
5017 * The mapping will be used during route processing.
5019 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5021 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
5022 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
5025 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
5029 * Derive Export RT automatically for VNI.
5031 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5033 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
5034 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5038 * Derive RD automatically for VNI using passed information - it
5039 * is of the form RouterId:unique-id-for-vni.
5041 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5043 if (is_vrf_rd_configured(bgp
))
5046 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5050 * Derive RD automatically for VNI using passed information - it
5051 * is of the form RouterId:unique-id-for-vni.
5053 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5055 char buf
[BGP_EVPN_PREFIX_RD_LEN
];
5057 vpn
->prd
.family
= AF_UNSPEC
;
5058 vpn
->prd
.prefixlen
= 64;
5059 snprintfrr(buf
, sizeof(buf
), "%pI4:%hu", &bgp
->router_id
, vpn
->rd_id
);
5060 (void)str2prefix_rd(buf
, &vpn
->prd
);
5061 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5067 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5069 struct list
*inst
= bm
->bgp
;
5070 struct listnode
*node
;
5071 struct bgp
*bgp_vrf
;
5073 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5074 if (bgp_vrf
->l3vni
== vni
)
5084 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5086 struct bgpevpn
*vpn
;
5089 memset(&tmp
, 0, sizeof(struct bgpevpn
));
5091 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5096 * Create a new vpn - invoked upon configuration or zebra notification.
5098 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5099 struct in_addr originator_ip
,
5100 vrf_id_t tenant_vrf_id
,
5101 struct in_addr mcast_grp
)
5103 struct bgpevpn
*vpn
;
5108 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5110 /* Set values - RD and RT set to defaults. */
5112 vpn
->originator_ip
= originator_ip
;
5113 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5114 vpn
->mcast_grp
= mcast_grp
;
5116 /* Initialize route-target import and export lists */
5117 vpn
->import_rtl
= list_new();
5118 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5119 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
5120 vpn
->export_rtl
= list_new();
5121 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5122 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
5123 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5124 derive_rd_rt_for_vni(bgp
, vpn
);
5126 /* Initialize EVPN route table. */
5127 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5130 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
5131 XFREE(MTYPE_BGP_EVPN
, vpn
);
5135 /* add to l2vni list on corresponding vrf */
5136 bgpevpn_link_to_l3vni(vpn
);
5138 bgp_evpn_vni_es_init(vpn
);
5140 QOBJ_REG(vpn
, bgpevpn
);
5145 * Free a given VPN - called in multiple scenarios such as zebra
5146 * notification, configuration being deleted, advertise-all-vni disabled etc.
5147 * This just frees appropriate memory, caller should have taken other
5150 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5152 bgp_evpn_vni_es_cleanup(vpn
);
5153 bgpevpn_unlink_from_l3vni(vpn
);
5154 bgp_table_unlock(vpn
->route_table
);
5155 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5156 list_delete(&vpn
->import_rtl
);
5157 list_delete(&vpn
->export_rtl
);
5158 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5159 hash_release(bgp
->vnihash
, vpn
);
5161 XFREE(MTYPE_BGP_EVPN
, vpn
);
5165 * Import evpn route from global table to VNI/VRF/ESI.
5167 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5168 const struct prefix
*p
, struct bgp_path_info
*pi
)
5170 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5174 * Unimport evpn route from VNI/VRF/ESI.
5176 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5177 const struct prefix
*p
, struct bgp_path_info
*pi
)
5179 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5182 /* filter routes which have martian next hops */
5183 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5187 struct bgp_dest
*rd_dest
, *dest
;
5188 struct bgp_table
*table
;
5189 struct bgp_path_info
*pi
;
5194 /* Walk entire global routing table and evaluate routes which could be
5195 * imported into this VPN. Note that we cannot just look at the routes
5196 * for the VNI's RD -
5197 * remote routes applicable for this VNI could have any RD.
5199 /* EVPN routes are a 2-level table. */
5200 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
5201 rd_dest
= bgp_route_next(rd_dest
)) {
5202 table
= bgp_dest_get_bgp_table_info(rd_dest
);
5206 for (dest
= bgp_table_top(table
); dest
;
5207 dest
= bgp_route_next(dest
)) {
5209 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
5212 /* Consider "valid" remote routes applicable for
5214 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5215 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5217 if (bgp_nexthop_self(bgp
, afi
, pi
->type
,
5218 pi
->sub_type
, pi
->attr
,
5220 const struct prefix
*p
=
5221 bgp_dest_get_prefix(dest
);
5223 if (bgp_debug_update(pi
->peer
, p
, NULL
,
5225 char attr_str
[BUFSIZ
] = {0};
5227 bgp_dump_attr(pi
->attr
,
5232 "%u: prefix %pBD with attr %s - DENIED due to martian or self nexthop",
5236 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5239 bgp_rib_remove(dest
, pi
, pi
->peer
, afi
,
5250 * Handle del of a local MACIP.
5252 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5253 struct ipaddr
*ip
, int state
)
5255 struct bgpevpn
*vpn
;
5256 struct prefix_evpn p
;
5257 struct bgp_dest
*dest
;
5259 /* Lookup VNI hash - should exist. */
5260 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5261 if (!vpn
|| !is_vni_live(vpn
)) {
5262 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5263 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5264 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5268 build_evpn_type2_prefix(&p
, mac
, ip
);
5269 if (state
== ZEBRA_NEIGH_ACTIVE
) {
5270 /* Remove EVPN type-2 route and schedule for processing. */
5271 delete_evpn_route(bgp
, vpn
, &p
);
5273 /* Re-instate the current remote best path if any */
5274 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
5276 evpn_zebra_reinstall_best_route(bgp
, vpn
, dest
);
5283 * Handle add of a local MACIP.
5285 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5286 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
, esi_t
*esi
)
5288 struct bgpevpn
*vpn
;
5289 struct prefix_evpn p
;
5291 /* Lookup VNI hash - should exist. */
5292 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5293 if (!vpn
|| !is_vni_live(vpn
)) {
5294 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5295 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5296 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5300 /* Create EVPN type-2 route and schedule for processing. */
5301 build_evpn_type2_prefix(&p
, mac
, ip
);
5302 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
, esi
)) {
5303 char buf
[ETHER_ADDR_STRLEN
];
5304 char buf2
[INET6_ADDRSTRLEN
];
5307 EC_BGP_EVPN_ROUTE_CREATE
,
5308 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5309 bgp
->vrf_id
, vpn
->vni
,
5310 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5313 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5314 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5321 static void link_l2vni_hash_to_l3vni(struct hash_bucket
*bucket
,
5322 struct bgp
*bgp_vrf
)
5324 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
5325 struct bgp
*bgp_evpn
= NULL
;
5327 bgp_evpn
= bgp_get_evpn();
5330 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5331 bgpevpn_link_to_l3vni(vpn
);
5334 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
,
5335 struct ethaddr
*svi_rmac
,
5336 struct ethaddr
*vrr_rmac
,
5337 struct in_addr originator_ip
, int filter
,
5338 ifindex_t svi_ifindex
,
5339 bool is_anycast_mac
)
5341 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5342 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5343 struct listnode
*node
= NULL
;
5344 struct bgpevpn
*vpn
= NULL
;
5347 /* get the EVPN instance - required to get the AS number for VRF
5350 bgp_evpn
= bgp_get_evpn();
5354 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
5360 /* if the BGP vrf instance doesn't exist - create one */
5361 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5366 ret
= bgp_get_vty(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5367 vrf_id
== VRF_DEFAULT
5368 ? BGP_INSTANCE_TYPE_DEFAULT
5369 : BGP_INSTANCE_TYPE_VRF
);
5371 case BGP_ERR_AS_MISMATCH
:
5372 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5373 "BGP is already running; AS is %u\n", as
);
5375 case BGP_ERR_INSTANCE_MISMATCH
:
5376 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5377 "BGP instance name and AS number mismatch\n");
5381 /* mark as auto created */
5382 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5385 /* associate the vrf with l3vni and related parameters */
5386 bgp_vrf
->l3vni
= l3vni
;
5387 bgp_vrf
->originator_ip
= originator_ip
;
5388 bgp_vrf
->l3vni_svi_ifindex
= svi_ifindex
;
5389 bgp_vrf
->evpn_info
->is_anycast_mac
= is_anycast_mac
;
5391 /* copy anycast MAC from VRR MAC */
5392 memcpy(&bgp_vrf
->rmac
, vrr_rmac
, ETH_ALEN
);
5393 /* copy sys RMAC from SVI MAC */
5394 memcpy(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, svi_rmac
, ETH_ALEN
);
5395 /* PIP user configured mac is not present use svi mac as sys mac */
5396 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
))
5397 memcpy(&bgp_vrf
->evpn_info
->pip_rmac
, svi_rmac
, ETH_ALEN
);
5399 if (bgp_debug_zebra(NULL
)) {
5400 char buf
[ETHER_ADDR_STRLEN
];
5401 char buf1
[ETHER_ADDR_STRLEN
];
5402 char buf2
[ETHER_ADDR_STRLEN
];
5404 zlog_debug("VRF %s vni %u pip %s RMAC %s sys RMAC %s static RMAC %s is_anycast_mac %s",
5405 vrf_id_to_name(bgp_vrf
->vrf_id
),
5407 bgp_vrf
->evpn_info
->advertise_pip
? "enable"
5409 prefix_mac2str(&bgp_vrf
->rmac
, buf
, sizeof(buf
)),
5410 prefix_mac2str(&bgp_vrf
->evpn_info
->pip_rmac
,
5411 buf1
, sizeof(buf1
)),
5412 prefix_mac2str(&bgp_vrf
->evpn_info
->pip_rmac_static
,
5413 buf2
, sizeof(buf2
)),
5414 is_anycast_mac
? "Enable" : "Disable");
5416 /* set the right filter - are we using l3vni only for prefix routes? */
5418 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5421 * VNI_FLAG_USE_TWO_LABELS flag for linked L2VNIs should not be
5422 * set before linking vrf to L3VNI. Thus, no need to clear
5426 UNSET_FLAG(bgp_vrf
->vrf_flags
,
5427 BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5429 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5430 if (!CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
5433 * If we are flapping VNI_FLAG_USE_TWO_LABELS
5434 * flag, update all MACIP routes in this VNI
5436 SET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5437 update_all_type2_routes(bgp_evpn
, vpn
);
5442 /* Map auto derive or configured RTs */
5443 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5444 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5446 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
5448 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5449 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5451 /* auto derive RD */
5452 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5454 /* link all corresponding l2vnis */
5455 hash_iterate(bgp_evpn
->vnihash
,
5456 (void (*)(struct hash_bucket
*,
5457 void *))link_l2vni_hash_to_l3vni
,
5460 /* Only update all corresponding type-2 routes if we are advertising two
5461 * labels along with type-2 routes
5464 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5465 update_routes_for_vni(bgp_evpn
, vpn
);
5467 /* advertise type-5 routes if needed */
5468 update_advertise_vrf_routes(bgp_vrf
);
5470 /* install all remote routes belonging to this l3vni into correspondng
5472 install_routes_for_vrf(bgp_vrf
);
5477 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5479 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5480 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5481 struct listnode
*node
= NULL
;
5482 struct listnode
*next
= NULL
;
5483 struct bgpevpn
*vpn
= NULL
;
5485 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5489 "Cannot process L3VNI %u Del - Could not find BGP instance",
5494 bgp_evpn
= bgp_get_evpn();
5498 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
5503 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5504 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5505 * routes. This will uninstalling the routes from zebra and decremnt the
5508 uninstall_routes_for_vrf(bgp_vrf
);
5510 /* delete/withdraw all type-5 routes */
5511 delete_withdraw_vrf_routes(bgp_vrf
);
5513 /* remove the l3vni from vrf instance */
5516 /* remove the Rmac from the BGP vrf */
5517 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5518 memset(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, 0, ETH_ALEN
);
5519 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
) &&
5520 !is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac
))
5521 memset(&bgp_vrf
->evpn_info
->pip_rmac
, 0, ETH_ALEN
);
5523 /* remove default import RT or Unmap non-default import RT */
5524 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5525 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5526 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5527 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5530 /* remove default export RT */
5531 if (!list_isempty(bgp_vrf
->vrf_export_rtl
) &&
5532 !CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
5533 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5536 /* update all corresponding local mac-ip routes */
5537 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5538 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5539 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5540 update_routes_for_vni(bgp_evpn
, vpn
);
5544 /* If any L2VNIs point to this instance, unlink them. */
5545 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5546 bgpevpn_unlink_from_l3vni(vpn
);
5548 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5550 /* Delete the instance if it was autocreated */
5551 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5552 bgp_delete(bgp_vrf
);
5558 * Handle del of a local VNI.
5560 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5562 struct bgpevpn
*vpn
;
5564 /* Locate VNI hash */
5565 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5567 if (bgp_debug_zebra(NULL
))
5569 EC_BGP_EVPN_VPN_VNI
,
5570 "%u: VNI hash entry for VNI %u not found at DEL",
5575 /* Remove all local EVPN routes and schedule for processing (to
5576 * withdraw from peers).
5578 delete_routes_for_vni(bgp
, vpn
);
5581 * tunnel is no longer active, del tunnel ip address from tip_hash
5583 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5585 /* Clear "live" flag and see if hash needs to be freed. */
5586 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5587 if (!is_vni_configured(vpn
))
5588 bgp_evpn_free(bgp
, vpn
);
5594 * Handle add (or update) of a local VNI. The VNI changes we care
5595 * about are for the local-tunnel-ip and the (tenant) VRF.
5597 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5598 struct in_addr originator_ip
,
5599 vrf_id_t tenant_vrf_id
,
5600 struct in_addr mcast_grp
)
5603 struct bgpevpn
*vpn
;
5604 struct prefix_evpn p
;
5606 /* Lookup VNI. If present and no change, exit. */
5607 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5610 if (is_vni_live(vpn
)
5611 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5612 && IPV4_ADDR_SAME(&vpn
->mcast_grp
, &mcast_grp
)
5613 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5614 /* Probably some other param has changed that we don't
5618 bgp_evpn_mcast_grp_change(bgp
, vpn
, mcast_grp
);
5620 /* Update tenant_vrf_id if it has changed. */
5621 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5622 bgpevpn_unlink_from_l3vni(vpn
);
5623 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5624 bgpevpn_link_to_l3vni(vpn
);
5627 /* If tunnel endpoint IP has changed, update (and delete prior
5628 * type-3 route, if needed.)
5630 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5631 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5633 /* Update all routes with new endpoint IP and/or export RT
5636 if (is_vni_live(vpn
))
5637 update_routes_for_vni(bgp
, vpn
);
5640 /* Create or update as appropriate. */
5642 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
,
5647 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5653 /* if the VNI is live already, there is nothing more to do */
5654 if (is_vni_live(vpn
))
5657 /* Mark as "live" */
5658 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5660 /* tunnel is now active, add tunnel-ip to db */
5661 bgp_tip_add(bgp
, &originator_ip
);
5663 /* filter routes as nexthop database has changed */
5664 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5667 * Create EVPN type-3 route and schedule for processing.
5669 * RT-3 only if doing head-end replication
5671 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
5672 == VXLAN_FLOOD_HEAD_END_REPL
) {
5673 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5674 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
)) {
5675 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5676 "%u: Type3 route creation failure for VNI %u",
5682 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5686 install_routes_for_vni(bgp
, vpn
);
5688 /* If we are advertising gateway mac-ip
5689 It needs to be conveyed again to zebra */
5690 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5692 /* advertise svi mac-ip knob to zebra */
5693 bgp_zebra_advertise_svi_macip(bgp
, vpn
->advertise_svi_macip
, vpn
->vni
);
5699 * Handle change in setting for BUM handling. The supported values
5700 * are head-end replication and dropping all BUM packets. Any change
5701 * should be registered with zebra. Also, if doing head-end replication,
5702 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5703 * to be dropped, the RT-3s must be withdrawn.
5705 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
5707 zlog_info("L2VPN EVPN BUM handling is %s",
5708 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
5709 "Flooding" : "Flooding Disabled");
5711 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
5712 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
5713 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
5714 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
5715 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
5719 * Cleanup EVPN information on disable - Need to delete and withdraw
5720 * EVPN routes from peers.
5722 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5724 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_bucket
*,
5725 void *))cleanup_vni_on_disable
,
5730 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5731 * BGP instance (default) is being freed.
5733 void bgp_evpn_cleanup(struct bgp
*bgp
)
5735 hash_iterate(bgp
->vnihash
,
5736 (void (*)(struct hash_bucket
*, void *))free_vni_entry
,
5739 hash_free(bgp
->import_rt_hash
);
5740 bgp
->import_rt_hash
= NULL
;
5742 hash_free(bgp
->vrf_import_rt_hash
);
5743 bgp
->vrf_import_rt_hash
= NULL
;
5745 hash_free(bgp
->vnihash
);
5746 bgp
->vnihash
= NULL
;
5748 list_delete(&bgp
->vrf_import_rtl
);
5749 list_delete(&bgp
->vrf_export_rtl
);
5750 list_delete(&bgp
->l2vnis
);
5754 * Initialization for EVPN
5757 * hash for RT to VNI
5759 void bgp_evpn_init(struct bgp
*bgp
)
5762 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
5763 bgp
->import_rt_hash
=
5764 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
5765 "BGP Import RT Hash");
5766 bgp
->vrf_import_rt_hash
=
5767 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
5768 "BGP VRF Import RT Hash");
5769 bgp
->vrf_import_rtl
= list_new();
5770 bgp
->vrf_import_rtl
->cmp
=
5771 (int (*)(void *, void *))evpn_route_target_cmp
;
5772 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
5773 bgp
->vrf_export_rtl
= list_new();
5774 bgp
->vrf_export_rtl
->cmp
=
5775 (int (*)(void *, void *))evpn_route_target_cmp
;
5776 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
5777 bgp
->l2vnis
= list_new();
5778 bgp
->l2vnis
->cmp
= vni_list_cmp
;
5779 /* By default Duplicate Address Dection is enabled.
5780 * Max-moves (N) 5, detection time (M) 180
5781 * default action is warning-only
5782 * freeze action permanently freezes address,
5783 * and freeze time (auto-recovery) is disabled.
5785 if (bgp
->evpn_info
) {
5786 bgp
->evpn_info
->dup_addr_detect
= true;
5787 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
5788 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
5789 bgp
->evpn_info
->dad_freeze
= false;
5790 bgp
->evpn_info
->dad_freeze_time
= 0;
5791 /* Initialize zebra vxlan */
5792 bgp_zebra_dup_addr_detection(bgp
);
5793 /* Enable PIP feature by default for bgp vrf instance */
5794 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
5795 struct bgp
*bgp_default
;
5797 bgp
->evpn_info
->advertise_pip
= true;
5798 bgp_default
= bgp_get_default();
5800 bgp
->evpn_info
->pip_ip
= bgp_default
->router_id
;
5804 /* Default BUM handling is to do head-end replication. */
5805 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
5808 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
5810 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5814 * Get the prefixlen of the ip prefix carried within the type5 evpn route.
5816 int bgp_evpn_get_type5_prefixlen(const struct prefix
*pfx
)
5818 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
5820 if (!pfx
|| pfx
->family
!= AF_EVPN
)
5823 if (evp
->prefix
.route_type
!= BGP_EVPN_IP_PREFIX_ROUTE
)
5826 return evp
->prefix
.prefix_addr
.ip_prefix_length
;
5830 * Should we register nexthop for this EVPN prefix for nexthop tracking?
5832 bool bgp_evpn_is_prefix_nht_supported(const struct prefix
*pfx
)
5834 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
5837 * EVPN routes should be marked as valid only if the nexthop is
5838 * reachable. Only if this happens, the route should be imported
5839 * (into VNI or VRF routing tables) and/or advertised.
5840 * Note: This is currently applied for EVPN type-2, type-3 and
5841 * type-5 routes. It may be tweaked later on for other routes, or
5842 * even removed completely when all routes are handled.
5844 if (pfx
&& pfx
->family
== AF_EVPN
&&
5845 (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
5846 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
5847 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))