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
;
747 /* Add the export RTs for L3VNI/VRF */
748 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
749 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, ecom
))
751 ecommunity_merge(attr
->ecommunity
, ecom
);
753 /* add the router mac extended community */
754 if (!is_zero_mac(&attr
->rmac
)) {
755 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
756 ecommunity_add_val(attr
->ecommunity
, &eval_rmac
, true, true);
759 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
763 * Build extended communities for EVPN route.
764 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
765 * and ENCAP extended communities are applicable for all routes.
766 * The default gateway extended community and MAC mobility (sticky) extended
767 * community are added as needed based on passed settings - only for type-2
768 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
769 * added, if present, based on passed settings - only for non-link-local
772 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
775 struct ecommunity ecom_encap
;
776 struct ecommunity ecom_sticky
;
777 struct ecommunity ecom_default_gw
;
778 struct ecommunity ecom_na
;
779 struct ecommunity_val eval
;
780 struct ecommunity_val eval_sticky
;
781 struct ecommunity_val eval_default_gw
;
782 struct ecommunity_val eval_rmac
;
783 struct ecommunity_val eval_na
;
786 bgp_encap_types tnl_type
;
787 struct listnode
*node
, *nnode
;
788 struct ecommunity
*ecom
;
790 struct list
*vrf_export_rtl
= NULL
;
793 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
794 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
795 encode_encap_extcomm(tnl_type
, &eval
);
797 ecom_encap
.unit_size
= ECOMMUNITY_SIZE
;
798 ecom_encap
.val
= (uint8_t *)eval
.val
;
801 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
803 /* Add the export RTs for L2VNI */
804 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
805 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
807 /* Add the export RTs for L3VNI if told to - caller determines
808 * when this should be done.
811 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
812 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
813 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
815 attr
->ecommunity
= ecommunity_merge(
816 attr
->ecommunity
, ecom
);
820 /* Add MAC mobility (sticky) if needed. */
823 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
824 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
825 ecom_sticky
.size
= 1;
826 ecom_sticky
.unit_size
= ECOMMUNITY_SIZE
;
827 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
829 ecommunity_merge(attr
->ecommunity
, &ecom_sticky
);
832 /* Add RMAC, if told to. */
834 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
835 ecommunity_add_val(attr
->ecommunity
, &eval_rmac
, true, true);
838 /* Add default gateway, if needed. */
839 if (attr
->default_gw
) {
840 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
841 encode_default_gw_extcomm(&eval_default_gw
);
842 ecom_default_gw
.size
= 1;
843 ecom_default_gw
.unit_size
= ECOMMUNITY_SIZE
;
844 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
846 ecommunity_merge(attr
->ecommunity
, &ecom_default_gw
);
849 proxy
= !!(attr
->es_flags
& ATTR_ES_PROXY_ADVERT
);
850 if (attr
->router_flag
|| proxy
) {
851 memset(&ecom_na
, 0, sizeof(ecom_na
));
852 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
, proxy
);
854 ecom_na
.unit_size
= ECOMMUNITY_SIZE
;
855 ecom_na
.val
= (uint8_t *)eval_na
.val
;
856 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
860 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
864 * Add MAC mobility extended community to attribute.
866 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
868 struct ecommunity ecom_tmp
;
869 struct ecommunity_val eval
;
870 uint8_t *ecom_val_ptr
;
877 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
879 /* Find current MM ecommunity */
882 if (attr
->ecommunity
) {
883 for (i
= 0; i
< attr
->ecommunity
->size
; i
++) {
884 pnt
= attr
->ecommunity
->val
+
885 (i
* attr
->ecommunity
->unit_size
);
889 if (type
== ECOMMUNITY_ENCODE_EVPN
891 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
893 (attr
->ecommunity
->val
+
894 (i
* attr
->ecommunity
->unit_size
));
900 /* Update the existing MM ecommunity */
902 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char)
903 * attr
->ecommunity
->unit_size
);
905 /* Add MM to existing */
907 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
909 ecom_tmp
.unit_size
= ECOMMUNITY_SIZE
;
910 ecom_tmp
.val
= (uint8_t *)eval
.val
;
912 if (attr
->ecommunity
)
914 ecommunity_merge(attr
->ecommunity
, &ecom_tmp
);
916 attr
->ecommunity
= ecommunity_dup(&ecom_tmp
);
920 /* Install EVPN route into zebra. */
921 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
922 const struct prefix_evpn
*p
,
923 struct bgp_path_info
*pi
)
930 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
933 if (pi
->sub_type
== BGP_ROUTE_IMPORTED
) {
934 if (pi
->attr
->sticky
)
935 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
936 if (pi
->attr
->default_gw
)
937 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
938 if (is_evpn_prefix_ipaddr_v6(p
) &&
939 pi
->attr
->router_flag
)
940 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
942 seq
= mac_mobility_seqnum(pi
->attr
);
943 /* if local ES notify zebra that this is a sync path */
944 if (bgp_evpn_attr_is_local_es(pi
->attr
)) {
945 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_SYNC_PATH
);
946 if (bgp_evpn_attr_is_proxy(pi
->attr
))
948 ZEBRA_MACIP_TYPE_PROXY_ADVERT
);
951 if (!bgp_evpn_attr_is_sync(pi
->attr
))
954 /* if a local path is being turned around and sent
955 * to zebra it is because it is a sync path on
958 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_SYNC_PATH
);
959 /* supply the highest peer seq number to zebra
962 seq
= bgp_evpn_attr_get_sync_seq(pi
->attr
);
963 /* if any of the paths from the peer have the ROUTER
964 * flag set install the local entry as a router entry
966 if (is_evpn_prefix_ipaddr_v6(p
) &&
967 (pi
->attr
->es_flags
&
968 ATTR_ES_PEER_ROUTER
))
970 ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
972 if (!(pi
->attr
->es_flags
& ATTR_ES_PEER_ACTIVE
))
974 ZEBRA_MACIP_TYPE_PROXY_ADVERT
);
977 ret
= bgp_zebra_send_remote_macip(
978 bgp
, vpn
, p
, pi
->attr
->nexthop
, 1, flags
,
979 seq
, bgp_evpn_attr_get_esi(pi
->attr
));
980 } else if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
) {
981 ret
= bgp_evpn_remote_es_evi_add(bgp
, vpn
, p
);
983 switch (pi
->attr
->pmsi_tnl_type
) {
984 case PMSI_TNLTYPE_INGR_REPL
:
985 flood_control
= VXLAN_FLOOD_HEAD_END_REPL
;
988 case PMSI_TNLTYPE_PIM_SM
:
989 flood_control
= VXLAN_FLOOD_PIM_SM
;
993 flood_control
= VXLAN_FLOOD_DISABLED
;
996 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, flood_control
, 1);
1002 /* Uninstall EVPN route from zebra. */
1003 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1004 const struct prefix_evpn
*p
,
1005 struct in_addr remote_vtep_ip
)
1009 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1010 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
1012 else if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
1013 ret
= bgp_evpn_remote_es_evi_del(bgp
, vpn
, p
);
1015 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
,
1016 VXLAN_FLOOD_DISABLED
, 0);
1022 * Due to MAC mobility, the prior "local" best route has been supplanted
1023 * by a "remote" best route. The prior route has to be deleted and withdrawn
1026 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1027 struct bgp_dest
*dest
,
1028 struct bgp_path_info
*old_local
,
1029 struct bgp_path_info
*new_select
)
1031 struct bgp_dest
*global_dest
;
1032 struct bgp_path_info
*pi
;
1033 afi_t afi
= AFI_L2VPN
;
1034 safi_t safi
= SAFI_EVPN
;
1036 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
1037 char prefix_buf
[PREFIX_STRLEN
];
1038 char esi_buf
[ESI_STR_LEN
];
1039 char esi_buf2
[ESI_STR_LEN
];
1040 struct prefix_evpn
*evp
=
1041 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1043 zlog_debug("local path deleted %s es %s; new-path-es %s",
1045 prefix_buf
, sizeof(prefix_buf
)),
1046 esi_to_str(&old_local
->attr
->esi
,
1047 esi_buf
, sizeof(esi_buf
)),
1048 new_select
? esi_to_str(&new_select
->attr
->esi
,
1049 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
)) {
1317 char buf1
[PREFIX_STRLEN
];
1319 zlog_debug("VRF %s evp %s advertise-pip primary ip is not configured",
1320 vrf_id_to_name(bgp_vrf
->vrf_id
),
1321 prefix2str(evp
, buf1
, sizeof(buf1
)));
1325 if (bgp_debug_zebra(NULL
)) {
1326 char buf
[ETHER_ADDR_STRLEN
];
1327 char buf1
[PREFIX_STRLEN
];
1328 char buf2
[INET6_ADDRSTRLEN
];
1330 zlog_debug("VRF %s type-5 route evp %s RMAC %s nexthop %s",
1331 vrf_id_to_name(bgp_vrf
->vrf_id
),
1332 prefix2str(evp
, buf1
, sizeof(buf1
)),
1333 prefix_mac2str(&attr
.rmac
, buf
, sizeof(buf
)),
1334 inet_ntop(AF_INET
, &attr
.nexthop
, buf2
,
1338 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1340 /* Setup RT and encap extended community */
1341 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1343 /* get the route node in global table */
1344 dest
= bgp_global_evpn_node_get(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1345 (const struct prefix_evpn
*)evp
,
1349 /* create or update the route entry within the route node */
1350 update_evpn_type5_route_entry(bgp_evpn
, bgp_vrf
, afi
, safi
, dest
, &attr
,
1353 /* schedule for processing and unlock node */
1354 if (route_changed
) {
1355 bgp_process(bgp_evpn
, dest
, afi
, safi
);
1356 bgp_dest_unlock_node(dest
);
1359 /* uninten temporary */
1361 aspath_unintern(&attr
.aspath
);
1365 static void bgp_evpn_get_sync_info(struct bgp
*bgp
, esi_t
*esi
,
1366 struct bgp_dest
*dest
, uint32_t loc_seq
,
1367 uint32_t *max_sync_seq
, bool *active_on_peer
,
1368 bool *peer_router
, bool *proxy_from_peer
)
1370 struct bgp_path_info
*tmp_pi
;
1371 struct bgp_path_info
*second_best_path
= NULL
;
1372 uint32_t tmp_mm_seq
= 0;
1376 /* find the best non-local path. a local path can only be present
1379 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1380 tmp_pi
= tmp_pi
->next
) {
1381 if (tmp_pi
->sub_type
!= BGP_ROUTE_IMPORTED
||
1382 !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1385 if (bgp_evpn_path_info_cmp(bgp
, tmp_pi
,
1386 second_best_path
, &paths_eq
))
1387 second_best_path
= tmp_pi
;
1390 if (!second_best_path
)
1393 tmp_esi
= bgp_evpn_attr_get_esi(second_best_path
->attr
);
1394 /* if this has the same ES desination as the local path
1397 if (!memcmp(esi
, tmp_esi
, sizeof(esi_t
))) {
1398 tmp_mm_seq
= mac_mobility_seqnum(second_best_path
->attr
);
1399 if (tmp_mm_seq
< loc_seq
)
1402 /* we have a non-proxy path from the ES peer. */
1403 if (second_best_path
->attr
->es_flags
&
1404 ATTR_ES_PROXY_ADVERT
) {
1405 *proxy_from_peer
= true;
1407 *active_on_peer
= true;
1410 if (second_best_path
->attr
->router_flag
)
1411 *peer_router
= true;
1413 /* we use both proxy and non-proxy imports to
1414 * determine the max sync sequence
1416 if (tmp_mm_seq
> *max_sync_seq
)
1417 *max_sync_seq
= tmp_mm_seq
;
1421 /* Bubble up sync-info from all paths (non-best) to the local-path.
1422 * This is need for MM sequence number syncing and proxy advertisement.
1423 * Note: The local path can only exist as a best path in the
1424 * VPN route table. It will take precedence over all sync paths.
1426 static void update_evpn_route_entry_sync_info(struct bgp
*bgp
,
1427 struct bgp_dest
*dest
,
1429 uint32_t loc_seq
, bool setup_sync
)
1432 struct prefix_evpn
*evp
=
1433 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1435 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1438 esi
= bgp_evpn_attr_get_esi(attr
);
1439 if (bgp_evpn_is_esi_valid(esi
)) {
1441 uint32_t max_sync_seq
= 0;
1442 bool active_on_peer
= false;
1443 bool peer_router
= false;
1444 bool proxy_from_peer
= false;
1446 bgp_evpn_get_sync_info(bgp
, esi
, dest
, loc_seq
,
1447 &max_sync_seq
, &active_on_peer
,
1448 &peer_router
, &proxy_from_peer
);
1449 attr
->mm_sync_seqnum
= max_sync_seq
;
1451 attr
->es_flags
|= ATTR_ES_PEER_ACTIVE
;
1453 attr
->es_flags
&= ~ATTR_ES_PEER_ACTIVE
;
1454 if (proxy_from_peer
)
1455 attr
->es_flags
|= ATTR_ES_PEER_PROXY
;
1457 attr
->es_flags
&= ~ATTR_ES_PEER_PROXY
;
1459 attr
->es_flags
|= ATTR_ES_PEER_ROUTER
;
1461 attr
->es_flags
&= ~ATTR_ES_PEER_ROUTER
;
1463 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
1464 char prefix_buf
[PREFIX_STRLEN
];
1465 char esi_buf
[ESI_STR_LEN
];
1467 zlog_debug("setup sync info for %s es %s max_seq %d %s%s%s",
1468 prefix2str(evp
, prefix_buf
,
1469 sizeof(prefix_buf
)),
1470 esi_to_str(esi
, esi_buf
,
1473 (attr
->es_flags
& ATTR_ES_PEER_ACTIVE
) ?
1474 "peer-active " : "",
1475 (attr
->es_flags
& ATTR_ES_PEER_PROXY
) ?
1477 (attr
->es_flags
& ATTR_ES_PEER_ROUTER
) ?
1478 "peer-router " : "");
1482 attr
->mm_sync_seqnum
= 0;
1483 attr
->es_flags
&= ~ATTR_ES_PEER_ACTIVE
;
1484 attr
->es_flags
&= ~ATTR_ES_PEER_PROXY
;
1489 * Create or update EVPN route entry. This could be in the VNI route table
1490 * or the global route table.
1492 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1493 afi_t afi
, safi_t safi
, struct bgp_dest
*dest
,
1494 struct attr
*attr
, int add
,
1495 struct bgp_path_info
**pi
, uint8_t flags
,
1496 uint32_t seq
, bool setup_sync
,
1499 struct bgp_path_info
*tmp_pi
;
1500 struct bgp_path_info
*local_pi
;
1501 struct attr
*attr_new
;
1502 mpls_label_t label
[BGP_MAX_LABELS
];
1503 uint32_t num_labels
= 1;
1504 int route_change
= 1;
1506 const struct prefix_evpn
*evp
;
1509 evp
= (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1510 memset(&label
, 0, sizeof(label
));
1512 /* See if this is an update of an existing route, or a new add. */
1513 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
1515 /* If route doesn't exist already, create a new one, if told to.
1516 * Otherwise act based on whether the attributes of the route have
1519 if (!local_pi
&& !add
)
1522 if (old_is_sync
&& local_pi
)
1523 *old_is_sync
= bgp_evpn_attr_is_sync(local_pi
->attr
);
1525 /* if a local path is being added with a non-zero esi look
1526 * for SYNC paths from ES peers and bubble up the sync-info
1528 update_evpn_route_entry_sync_info(bgp
, dest
, attr
, seq
, setup_sync
);
1530 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1531 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1532 add_mac_mobility_to_attr(seq
, attr
);
1535 /* Add (or update) attribute to hash. */
1536 attr_new
= bgp_attr_intern(attr
);
1538 /* Extract MAC mobility sequence number, if any. */
1539 attr_new
->mm_seqnum
=
1540 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1541 attr_new
->sticky
= sticky
;
1543 /* Create new route with its attribute. */
1544 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1545 bgp
->peer_self
, attr_new
, dest
);
1546 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1547 bgp_path_info_extra_get(tmp_pi
);
1549 /* The VNI goes into the 'label' field of the route */
1550 vni2label(vpn
->vni
, &label
[0]);
1552 /* Type-2 routes may carry a second VNI - the L3-VNI.
1553 * Only attach second label if we are advertising two labels for
1556 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1557 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1560 l3vni
= bgpevpn_get_l3vni(vpn
);
1562 vni2label(l3vni
, &label
[1]);
1567 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1568 tmp_pi
->extra
->num_labels
= num_labels
;
1569 /* Mark route as self type-2 route */
1570 if (flags
&& CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1571 tmp_pi
->extra
->af_flags
= BGP_EVPN_MACIP_TYPE_SVI_IP
;
1572 bgp_path_info_add(dest
, tmp_pi
);
1575 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1576 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1580 * The attributes have changed, type-2 routes needs to
1581 * be advertised with right labels.
1583 vni2label(vpn
->vni
, &label
[0]);
1584 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1585 && CHECK_FLAG(vpn
->flags
,
1586 VNI_FLAG_USE_TWO_LABELS
)) {
1589 l3vni
= bgpevpn_get_l3vni(vpn
);
1591 vni2label(l3vni
, &label
[1]);
1595 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1596 tmp_pi
->extra
->num_labels
= num_labels
;
1598 /* The attribute has changed. */
1599 /* Add (or update) attribute to hash. */
1600 attr_new
= bgp_attr_intern(attr
);
1601 bgp_path_info_set_flag(dest
, tmp_pi
,
1602 BGP_PATH_ATTR_CHANGED
);
1604 /* Extract MAC mobility sequence number, if any. */
1605 attr_new
->mm_seqnum
=
1606 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1607 attr_new
->sticky
= sticky
;
1609 /* Restore route, if needed. */
1610 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1611 bgp_path_info_restore(dest
, tmp_pi
);
1613 /* Unintern existing, set to new. */
1614 bgp_attr_unintern(&tmp_pi
->attr
);
1615 tmp_pi
->attr
= attr_new
;
1616 tmp_pi
->uptime
= bgp_clock();
1620 /* Return back the route entry. */
1622 return route_change
;
1625 static void evpn_zebra_reinstall_best_route(struct bgp
*bgp
,
1626 struct bgpevpn
*vpn
,
1627 struct bgp_dest
*dest
)
1629 struct bgp_path_info
*tmp_ri
;
1630 struct bgp_path_info
*curr_select
= NULL
;
1632 for (tmp_ri
= bgp_dest_get_bgp_path_info(dest
); tmp_ri
;
1633 tmp_ri
= tmp_ri
->next
) {
1634 if (CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_SELECTED
)) {
1635 curr_select
= tmp_ri
;
1640 if (curr_select
&& curr_select
->type
== ZEBRA_ROUTE_BGP
1641 && (curr_select
->sub_type
== BGP_ROUTE_IMPORTED
||
1642 bgp_evpn_attr_is_sync(curr_select
->attr
)))
1643 evpn_zebra_install(bgp
, vpn
,
1644 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1649 * If the local route was not selected evict it and tell zebra to re-add
1650 * the best remote dest.
1652 * Typically a local path added by zebra is expected to be selected as
1653 * best. In which case when a remote path wins as best (later)
1654 * evpn_route_select_install itself evicts the older-local-best path.
1656 * However if bgp's add and zebra's add cross paths (race condition) it
1657 * is possible that the local path is no longer the "older" best path.
1658 * It is a path that was never designated as best and hence requires
1659 * additional handling to prevent bgp from injecting and holding on to a
1660 * non-best local path.
1662 static void evpn_cleanup_local_non_best_route(struct bgp
*bgp
,
1663 struct bgpevpn
*vpn
,
1664 struct bgp_dest
*dest
,
1665 struct bgp_path_info
*local_pi
)
1667 /* local path was not picked as the winner; kick it out */
1668 if (bgp_debug_zebra(NULL
))
1669 zlog_debug("evicting local evpn prefix %pBD as remote won",
1672 evpn_delete_old_local_route(bgp
, vpn
, dest
, local_pi
, NULL
);
1673 bgp_path_info_reap(dest
, local_pi
);
1675 /* tell zebra to re-add the best remote path */
1676 evpn_zebra_reinstall_best_route(bgp
, vpn
, dest
);
1680 * Create or update EVPN route (of type based on prefix) for specified VNI
1681 * and schedule for processing.
1683 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1684 struct prefix_evpn
*p
, uint8_t flags
,
1685 uint32_t seq
, esi_t
*esi
)
1687 struct bgp_dest
*dest
;
1689 struct attr
*attr_new
;
1690 int add_l3_ecomm
= 0;
1691 struct bgp_path_info
*pi
;
1692 afi_t afi
= AFI_L2VPN
;
1693 safi_t safi
= SAFI_EVPN
;
1695 bool old_is_sync
= false;
1697 memset(&attr
, 0, sizeof(struct attr
));
1699 /* Build path-attribute for this route. */
1700 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1701 attr
.nexthop
= vpn
->originator_ip
;
1702 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1703 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1704 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1705 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1706 attr
.router_flag
= CHECK_FLAG(flags
,
1707 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1708 if (CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_PROXY_ADVERT
))
1709 attr
.es_flags
|= ATTR_ES_PROXY_ADVERT
;
1711 if (esi
&& bgp_evpn_is_esi_valid(esi
)) {
1712 memcpy(&attr
.esi
, esi
, sizeof(esi_t
));
1713 attr
.es_flags
|= ATTR_ES_IS_LOCAL
;
1716 /* PMSI is only needed for type-3 routes */
1717 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
1718 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1719 attr
.pmsi_tnl_type
= PMSI_TNLTYPE_INGR_REPL
;
1722 if (bgp_debug_zebra(NULL
)) {
1723 char buf
[ETHER_ADDR_STRLEN
];
1724 char buf1
[PREFIX_STRLEN
];
1725 char buf3
[ESI_STR_LEN
];
1728 "VRF %s vni %u type-2 route evp %s RMAC %s nexthop %pI4 esi %s",
1729 vpn
->bgp_vrf
? vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
)
1731 vpn
->vni
, prefix2str(p
, buf1
, sizeof(buf1
)),
1732 prefix_mac2str(&attr
.rmac
, buf
, sizeof(buf
)),
1733 &attr
.mp_nexthop_global_in
,
1734 esi_to_str(esi
, buf3
, sizeof(buf3
)));
1736 /* router mac is only needed for type-2 routes here. */
1737 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
1738 uint8_t af_flags
= 0;
1740 if (CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1741 SET_FLAG(af_flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
);
1743 bgp_evpn_get_rmac_nexthop(vpn
, p
, &attr
, af_flags
);
1746 vni2label(vpn
->vni
, &(attr
.label
));
1748 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1749 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1752 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
&&
1753 (is_evpn_prefix_ipaddr_v4(p
) ||
1754 !IN6_IS_ADDR_LINKLOCAL(&p
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1755 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1756 bgpevpn_get_l3vni(vpn
))
1759 /* Set up extended community. */
1760 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1762 /* First, create (or fetch) route node within the VNI. */
1763 /* NOTE: There is no RD here. */
1764 dest
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1766 /* Create or update route entry. */
1767 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, dest
, &attr
,
1769 true /* setup_sync */, &old_is_sync
);
1771 attr_new
= pi
->attr
;
1773 /* lock ri to prevent freeing in evpn_route_select_install */
1774 bgp_path_info_lock(pi
);
1776 /* Perform route selection. Normally, the local route in the
1777 * VNI is expected to win and be the best route. However, if
1778 * there is a race condition where a host moved from local to
1779 * remote and the remote route was received in BGP just prior
1780 * to the local MACIP notification from zebra, the remote
1781 * route would win, and we should evict the defunct local route
1782 * and (re)install the remote route into zebra.
1784 evpn_route_select_install(bgp
, vpn
, dest
);
1786 * If the new local route was not selected evict it and tell zebra
1787 * to re-add the best remote dest. BGP doesn't retain non-best local
1790 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
1793 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1795 evpn_cleanup_local_non_best_route(bgp
, vpn
, dest
, pi
);
1799 /* If the local path already existed and is still the
1800 * best path we need to also check if it transitioned
1801 * from being a sync path to a non-sync path. If it
1802 * it did we need to notify zebra that the sync-path
1805 new_is_sync
= bgp_evpn_attr_is_sync(pi
->attr
);
1806 if (!new_is_sync
&& old_is_sync
)
1807 evpn_zebra_uninstall(bgp
, vpn
, p
, zero_vtep_ip
);
1810 bgp_path_info_unlock(pi
);
1812 bgp_dest_unlock_node(dest
);
1814 /* If this is a new route or some attribute has changed, export the
1815 * route to the global table. The route will be advertised to peers
1816 * from there. Note that this table is a 2-level tree (RD-level +
1817 * Prefix-level) similar to L3VPN routes.
1820 struct bgp_path_info
*global_pi
;
1822 dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1823 (const struct prefix_evpn
*)p
,
1825 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, dest
, attr_new
, 1,
1826 &global_pi
, flags
, seq
,
1827 false /* setup_sync */, NULL
/* old_is_sync */);
1829 /* Schedule for processing and unlock node. */
1830 bgp_process(bgp
, dest
, afi
, safi
);
1831 bgp_dest_unlock_node(dest
);
1834 /* Unintern temporary. */
1835 aspath_unintern(&attr
.aspath
);
1841 * Delete EVPN route entry.
1842 * The entry can be in ESI/VNI table or the global table.
1844 void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1845 struct bgp_dest
*dest
,
1846 struct bgp_path_info
**pi
)
1848 struct bgp_path_info
*tmp_pi
;
1852 /* Now, find matching route. */
1853 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1854 tmp_pi
= tmp_pi
->next
)
1855 if (tmp_pi
->peer
== bgp
->peer_self
1856 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1857 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1862 /* Mark route for delete. */
1864 bgp_path_info_delete(dest
, tmp_pi
);
1867 /* Delete EVPN type5 route */
1868 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
1870 afi_t afi
= AFI_L2VPN
;
1871 safi_t safi
= SAFI_EVPN
;
1872 struct bgp_dest
*dest
= NULL
;
1873 struct bgp_path_info
*pi
= NULL
;
1874 struct bgp
*bgp_evpn
= NULL
; /* evpn bgp instance */
1876 bgp_evpn
= bgp_get_evpn();
1880 /* locate the global route entry for this type-5 prefix */
1881 dest
= bgp_global_evpn_node_lookup(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1882 (const struct prefix_evpn
*)evp
, &bgp_vrf
->vrf_prd
);
1886 delete_evpn_route_entry(bgp_evpn
, afi
, safi
, dest
, &pi
);
1888 bgp_process(bgp_evpn
, dest
, afi
, safi
);
1889 bgp_dest_unlock_node(dest
);
1894 * Delete EVPN route (of type based on prefix) for specified VNI and
1895 * schedule for processing.
1897 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1898 struct prefix_evpn
*p
)
1900 struct bgp_dest
*dest
, *global_dest
;
1901 struct bgp_path_info
*pi
;
1902 afi_t afi
= AFI_L2VPN
;
1903 safi_t safi
= SAFI_EVPN
;
1905 /* First, locate the route node within the VNI. If it doesn't exist,
1907 * is nothing further to do.
1909 /* NOTE: There is no RD here. */
1910 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
1914 /* Next, locate route node in the global EVPN routing table. Note that
1915 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1918 global_dest
= bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1919 (const struct prefix_evpn
*)p
, &vpn
->prd
);
1921 /* Delete route entry in the global EVPN table. */
1922 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
1924 /* Schedule for processing - withdraws to peers happen from
1928 bgp_process(bgp
, global_dest
, afi
, safi
);
1929 bgp_dest_unlock_node(global_dest
);
1932 /* Delete route entry in the VNI route table. This can just be removed.
1934 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
1936 bgp_path_info_reap(dest
, pi
);
1937 evpn_route_select_install(bgp
, vpn
, dest
);
1939 bgp_dest_unlock_node(dest
);
1944 static void bgp_evpn_update_type2_route_entry(struct bgp
*bgp
,
1945 struct bgpevpn
*vpn
,
1946 struct bgp_dest
*dest
,
1947 struct bgp_path_info
*local_pi
,
1950 afi_t afi
= AFI_L2VPN
;
1951 safi_t safi
= SAFI_EVPN
;
1952 struct bgp_path_info
*pi
;
1954 struct attr
*attr_new
;
1956 int add_l3_ecomm
= 0;
1957 struct bgp_dest
*global_dest
;
1958 struct bgp_path_info
*global_pi
;
1959 struct prefix_evpn
*evp
=
1960 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1962 bool old_is_sync
= false;
1964 if (CHECK_FLAG(local_pi
->flags
, BGP_PATH_REMOVED
))
1968 * Build attribute per local route as the MAC mobility and
1969 * some other values could differ for different routes. The
1970 * attributes will be shared in the hash table.
1972 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
1973 attr
.nexthop
= vpn
->originator_ip
;
1974 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1975 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1976 attr
.sticky
= (local_pi
->attr
->sticky
) ? 1 : 0;
1977 attr
.router_flag
= (local_pi
->attr
->router_flag
) ? 1 : 0;
1978 attr
.es_flags
= local_pi
->attr
->es_flags
;
1979 if (local_pi
->attr
->default_gw
) {
1980 attr
.default_gw
= 1;
1981 if (is_evpn_prefix_ipaddr_v6(evp
))
1982 attr
.router_flag
= 1;
1984 memcpy(&attr
.esi
, &local_pi
->attr
->esi
, sizeof(esi_t
));
1985 bgp_evpn_get_rmac_nexthop(vpn
, evp
, &attr
,
1986 local_pi
->extra
->af_flags
);
1987 vni2label(vpn
->vni
, &(attr
.label
));
1988 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
1989 * using L3 VNI for type-2 routes also.
1991 if ((is_evpn_prefix_ipaddr_v4(evp
) ||
1992 !IN6_IS_ADDR_LINKLOCAL(
1993 &evp
->prefix
.macip_addr
.ip
.ipaddr_v6
)) &&
1994 CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
) &&
1995 bgpevpn_get_l3vni(vpn
))
1998 /* Set up extended community. */
1999 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2000 seq
= mac_mobility_seqnum(local_pi
->attr
);
2002 if (bgp_debug_zebra(NULL
)) {
2003 char buf
[ETHER_ADDR_STRLEN
];
2004 char buf1
[PREFIX_STRLEN
];
2005 char buf3
[ESI_STR_LEN
];
2008 "VRF %s vni %u evp %s RMAC %s nexthop %pI4 esi %s esf 0x%x from %s",
2009 vpn
->bgp_vrf
? vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
)
2011 vpn
->vni
, prefix2str(evp
, buf1
, sizeof(buf1
)),
2012 prefix_mac2str(&attr
.rmac
, buf
, sizeof(buf
)),
2013 &attr
.mp_nexthop_global_in
,
2014 esi_to_str(&attr
.esi
, buf3
, sizeof(buf3
)),
2015 attr
.es_flags
, caller
);
2018 /* Update the route entry. */
2019 route_change
= update_evpn_route_entry(
2020 bgp
, vpn
, afi
, safi
, dest
, &attr
, 0, &pi
, 0, seq
,
2021 true /* setup_sync */, &old_is_sync
);
2024 attr_new
= pi
->attr
;
2025 /* lock ri to prevent freeing in evpn_route_select_install */
2026 bgp_path_info_lock(pi
);
2028 /* Perform route selection. Normally, the local route in the
2029 * VNI is expected to win and be the best route. However,
2030 * under peculiar situations (e.g., tunnel (next hop) IP change
2031 * that causes best selection to be based on next hop), a
2032 * remote route could win. If the local route is the best,
2033 * ensure it is updated in the global EVPN route table and
2034 * advertised to peers; otherwise, ensure it is evicted and
2035 * (re)install the remote route into zebra.
2037 evpn_route_select_install(bgp
, vpn
, dest
);
2039 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2042 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
2044 evpn_cleanup_local_non_best_route(bgp
, vpn
, dest
, pi
);
2048 /* If the local path already existed and is still the
2049 * best path we need to also check if it transitioned
2050 * from being a sync path to a non-sync path. If it
2051 * it did we need to notify zebra that the sync-path
2054 new_is_sync
= bgp_evpn_attr_is_sync(pi
->attr
);
2055 if (!new_is_sync
&& old_is_sync
)
2056 evpn_zebra_uninstall(bgp
, vpn
,
2063 bgp_path_info_unlock(pi
);
2066 /* Update route in global routing table. */
2067 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
,
2068 safi
, evp
, &vpn
->prd
);
2069 assert(global_dest
);
2070 update_evpn_route_entry(
2071 bgp
, vpn
, afi
, safi
, global_dest
, attr_new
, 0,
2072 &global_pi
, 0, mac_mobility_seqnum(attr_new
),
2073 false /* setup_sync */, NULL
/* old_is_sync */);
2075 /* Schedule for processing and unlock node. */
2076 bgp_process(bgp
, global_dest
, afi
, safi
);
2077 bgp_dest_unlock_node(global_dest
);
2080 /* Unintern temporary. */
2081 aspath_unintern(&attr
.aspath
);
2085 * Update all type-2 (MACIP) local routes for this VNI - these should also
2086 * be scheduled for advertise to peers.
2088 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2090 struct bgp_dest
*dest
;
2091 struct bgp_path_info
*tmp_pi
;
2093 /* Walk this VNI's route table and update local type-2 routes. For any
2094 * routes updated, update corresponding entry in the global table too.
2096 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2097 dest
= bgp_route_next(dest
)) {
2098 const struct prefix_evpn
*evp
=
2099 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
2101 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2104 /* Identify local route. */
2105 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
2106 tmp_pi
= tmp_pi
->next
) {
2107 if (tmp_pi
->peer
== bgp
->peer_self
2108 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2109 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2116 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, tmp_pi
,
2124 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2125 * global routing table. These are also scheduled for withdraw from peers.
2127 static int delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2131 struct bgp_dest
*rddest
, *dest
;
2132 struct bgp_table
*table
;
2133 struct bgp_path_info
*pi
;
2138 rddest
= bgp_node_lookup(bgp
->rib
[afi
][safi
],
2139 (struct prefix
*)&vpn
->prd
);
2140 if (rddest
&& bgp_dest_has_bgp_path_info_data(rddest
)) {
2141 table
= bgp_dest_get_bgp_table_info(rddest
);
2142 for (dest
= bgp_table_top(table
); dest
;
2143 dest
= bgp_route_next(dest
)) {
2144 const struct prefix_evpn
*evp
=
2145 (const struct prefix_evpn
*)bgp_dest_get_prefix(
2148 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2151 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2153 bgp_process(bgp
, dest
, afi
, safi
);
2157 /* Unlock RD node. */
2159 bgp_dest_unlock_node(rddest
);
2165 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2166 * table as well as the per-VNI route table.
2168 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2172 struct bgp_dest
*dest
;
2173 struct bgp_path_info
*pi
;
2178 /* First, walk the global route table for this VNI's type-2 local
2180 * EVPN routes are a 2-level table, first get the RD table.
2182 delete_global_type2_routes(bgp
, vpn
);
2184 /* Next, walk this VNI's route table and delete local type-2 routes. */
2185 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2186 dest
= bgp_route_next(dest
)) {
2187 const struct prefix_evpn
*evp
=
2188 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
2190 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2193 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2195 /* Route entry in local table gets deleted immediately. */
2197 bgp_path_info_reap(dest
, pi
);
2204 * Delete all routes in the per-VNI route table.
2206 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2208 struct bgp_dest
*dest
;
2209 struct bgp_path_info
*pi
, *nextpi
;
2211 /* Walk this VNI's route table and delete all routes. */
2212 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2213 dest
= bgp_route_next(dest
)) {
2214 for (pi
= bgp_dest_get_bgp_path_info(dest
);
2215 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2216 bgp_path_info_delete(dest
, pi
);
2217 bgp_path_info_reap(dest
, pi
);
2224 /* BUM traffic flood mode per-l2-vni */
2225 static int bgp_evpn_vni_flood_mode_get(struct bgp
*bgp
,
2226 struct bgpevpn
*vpn
)
2228 /* if flooding has been globally disabled per-vni mode is
2231 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
2232 return VXLAN_FLOOD_DISABLED
;
2234 /* if mcast group ip has been specified we use a PIM-SM MDT */
2235 if (vpn
->mcast_grp
.s_addr
!= INADDR_ANY
)
2236 return VXLAN_FLOOD_PIM_SM
;
2238 /* default is ingress replication */
2239 return VXLAN_FLOOD_HEAD_END_REPL
;
2243 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2244 * export RT getting modified or change to tunnel IP. Note that these
2245 * situations need the route in the per-VNI table as well as the global
2246 * table to be updated (as attributes change).
2248 int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2251 struct prefix_evpn p
;
2253 /* Update and advertise the type-3 route (only one) followed by the
2254 * locally learnt type-2 routes (MACIP) - for this VNI.
2256 * RT-3 only if doing head-end replication
2258 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
2259 == VXLAN_FLOOD_HEAD_END_REPL
) {
2260 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2261 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
);
2266 return update_all_type2_routes(bgp
, vpn
);
2270 * Delete (and withdraw) local routes for specified VNI from the global
2271 * table and per-VNI table. After this, remove all other routes from
2272 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2273 * (advertise-all-vni) being disabled.
2275 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2278 struct prefix_evpn p
;
2280 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2281 * followed by type-3 routes (only one) - for this VNI.
2283 ret
= delete_all_type2_routes(bgp
, vpn
);
2287 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2288 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2292 /* Delete all routes from the per-VNI table. */
2293 return delete_all_vni_routes(bgp
, vpn
);
2297 * There is a flood mcast IP address change. Update the mcast-grp and
2298 * remove the type-3 route if any. A new type-3 route will be generated
2299 * post tunnel_ip update if the new flood mode is head-end-replication.
2301 static int bgp_evpn_mcast_grp_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2302 struct in_addr mcast_grp
)
2304 struct prefix_evpn p
;
2306 vpn
->mcast_grp
= mcast_grp
;
2308 if (is_vni_live(vpn
)) {
2309 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2310 delete_evpn_route(bgp
, vpn
, &p
);
2317 * There is a tunnel endpoint IP address change for this VNI, delete
2318 * prior type-3 route (if needed) and update.
2319 * Note: Route re-advertisement happens elsewhere after other processing
2322 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2323 struct in_addr originator_ip
)
2325 struct prefix_evpn p
;
2327 /* If VNI is not live, we only need to update the originator ip */
2328 if (!is_vni_live(vpn
)) {
2329 vpn
->originator_ip
= originator_ip
;
2333 /* Update the tunnel-ip hash */
2334 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2335 bgp_tip_add(bgp
, &originator_ip
);
2337 /* filter routes as martian nexthop db has changed */
2338 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2340 /* Need to withdraw type-3 route as the originator IP is part
2343 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2344 delete_evpn_route(bgp
, vpn
, &p
);
2346 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2347 vpn
->originator_ip
= originator_ip
;
2351 static struct bgp_path_info
*
2352 bgp_create_evpn_bgp_path_info(struct bgp_path_info
*parent_pi
,
2353 struct bgp_dest
*dest
, struct attr
*attr
)
2355 struct attr
*attr_new
;
2356 struct bgp_path_info
*pi
;
2358 /* Add (or update) attribute to hash. */
2359 attr_new
= bgp_attr_intern(attr
);
2361 /* Create new route with its attribute. */
2362 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0, parent_pi
->peer
,
2364 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2365 bgp_path_info_extra_get(pi
);
2366 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2367 bgp_dest_lock_node((struct bgp_dest
*)parent_pi
->net
);
2368 if (parent_pi
->extra
) {
2369 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2370 sizeof(pi
->extra
->label
));
2371 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2373 bgp_path_info_add(dest
, pi
);
2379 * Install route entry into the VRF routing table and invoke route selection.
2381 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2382 const struct prefix_evpn
*evp
,
2383 struct bgp_path_info
*parent_pi
)
2385 struct bgp_dest
*dest
;
2386 struct bgp_path_info
*pi
;
2388 struct attr
*attr_new
;
2391 struct prefix
*pp
= &p
;
2394 char buf
[PREFIX_STRLEN
];
2395 bool new_pi
= false;
2397 memset(pp
, 0, sizeof(struct prefix
));
2398 ip_prefix_from_evpn_prefix(evp
, pp
);
2400 if (bgp_debug_zebra(NULL
)) {
2402 "vrf %s: import evpn prefix %s parent %p flags 0x%x",
2403 vrf_id_to_name(bgp_vrf
->vrf_id
),
2404 prefix2str(evp
, buf
, sizeof(buf
)),
2405 parent_pi
, parent_pi
->flags
);
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
))
2486 "... %s pi dest %p (l %d) pi %p (l %d, f 0x%x)",
2487 new_pi
? "new" : "update",
2488 dest
, bgp_dest_to_rnode(dest
)->lock
,
2489 pi
, pi
->lock
, pi
->flags
);
2495 * Install route entry into the VNI routing table and invoke route selection.
2497 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2498 const struct prefix_evpn
*p
,
2499 struct bgp_path_info
*parent_pi
)
2501 struct bgp_dest
*dest
;
2502 struct bgp_path_info
*pi
;
2503 struct bgp_path_info
*local_pi
;
2504 struct attr
*attr_new
;
2506 struct prefix_evpn ad_evp
;
2508 /* EAD prefix in the global table doesn't include the VTEP-IP so
2509 * we need to create a different copy for the VNI
2511 if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
2512 p
= evpn_type1_prefix_vni_copy(&ad_evp
, p
,
2513 parent_pi
->attr
->nexthop
);
2515 /* Create (or fetch) route within the VNI. */
2516 /* NOTE: There is no RD here. */
2517 dest
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2519 /* Check if route entry is already present. */
2520 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2522 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2526 /* Create an info */
2527 (void)bgp_create_evpn_bgp_path_info(parent_pi
, dest
,
2530 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2531 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2532 bgp_dest_unlock_node(dest
);
2535 /* The attribute has changed. */
2536 /* Add (or update) attribute to hash. */
2537 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2539 /* Restore route, if needed. */
2540 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2541 bgp_path_info_restore(dest
, pi
);
2543 /* Mark if nexthop has changed. */
2544 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2545 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2547 /* Unintern existing, set to new. */
2548 bgp_attr_unintern(&pi
->attr
);
2549 pi
->attr
= attr_new
;
2550 pi
->uptime
= bgp_clock();
2553 /* Perform route selection and update zebra, if required. */
2554 ret
= evpn_route_select_install(bgp
, vpn
, dest
);
2556 /* if the best path is a local path with a non-zero ES
2557 * sync info against the local path may need to be updated
2558 * when a remote path is added/updated (including changes
2559 * from sync-path to remote-path)
2561 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
2562 if (local_pi
&& bgp_evpn_attr_is_local_es(local_pi
->attr
))
2563 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, local_pi
,
2566 bgp_dest_unlock_node(dest
);
2572 * Uninstall route entry from the VRF routing table and send message
2573 * to zebra, if appropriate.
2575 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2576 const struct prefix_evpn
*evp
,
2577 struct bgp_path_info
*parent_pi
)
2579 struct bgp_dest
*dest
;
2580 struct bgp_path_info
*pi
;
2583 struct prefix
*pp
= &p
;
2586 char buf
[PREFIX_STRLEN
];
2588 memset(pp
, 0, sizeof(struct prefix
));
2589 ip_prefix_from_evpn_prefix(evp
, pp
);
2591 if (bgp_debug_zebra(NULL
)) {
2593 "vrf %s: unimport evpn prefix %s parent %p flags 0x%x",
2594 vrf_id_to_name(bgp_vrf
->vrf_id
),
2595 prefix2str(evp
, buf
, sizeof(buf
)),
2596 parent_pi
, parent_pi
->flags
);
2599 /* Locate route within the VRF. */
2600 /* NOTE: There is no RD here. */
2601 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2603 safi
= SAFI_UNICAST
;
2604 dest
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2607 safi
= SAFI_UNICAST
;
2608 dest
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2614 /* Find matching route entry. */
2615 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2617 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2623 if (bgp_debug_zebra(NULL
))
2625 "... delete dest %p (l %d) pi %p (l %d, f 0x%x)",
2626 dest
, bgp_dest_to_rnode(dest
)->lock
,
2627 pi
, pi
->lock
, pi
->flags
);
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 buf1
[PREFIX_STRLEN
];
2856 char attr_str
[BUFSIZ
] = {0};
2858 bgp_dump_attr(pi
->attr
, attr_str
, sizeof(attr_str
));
2860 zlog_debug("%s: bgp %u prefix %s with attr %s - DENIED due to self mac",
2861 __func__
, bgp_vrf
->vrf_id
,
2862 prefix2str(evp
, buf1
, sizeof(buf1
)),
2873 * Install or uninstall mac-ip routes are appropriate for this
2876 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
2880 struct bgp_dest
*rd_dest
, *dest
;
2881 struct bgp_table
*table
;
2882 struct bgp_path_info
*pi
;
2884 char buf
[PREFIX_STRLEN
];
2885 struct bgp
*bgp_evpn
= NULL
;
2889 bgp_evpn
= bgp_get_evpn();
2893 /* Walk entire global routing table and evaluate routes which could be
2894 * imported into this VRF. Note that we need to loop through all global
2895 * routes to determine which route matches the import rt on vrf
2897 for (rd_dest
= bgp_table_top(bgp_evpn
->rib
[afi
][safi
]); rd_dest
;
2898 rd_dest
= bgp_route_next(rd_dest
)) {
2899 table
= bgp_dest_get_bgp_table_info(rd_dest
);
2903 for (dest
= bgp_table_top(table
); dest
;
2904 dest
= bgp_route_next(dest
)) {
2905 const struct prefix_evpn
*evp
=
2906 (const struct prefix_evpn
*)bgp_dest_get_prefix(
2909 /* if not mac-ip route skip this route */
2910 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
2911 || evp
->prefix
.route_type
2912 == BGP_EVPN_IP_PREFIX_ROUTE
))
2915 /* if not a mac+ip route skip this route */
2916 if (!(is_evpn_prefix_ipaddr_v4(evp
)
2917 || is_evpn_prefix_ipaddr_v6(evp
)))
2920 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
2922 /* Consider "valid" remote routes applicable for
2925 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
2926 && pi
->type
== ZEBRA_ROUTE_BGP
2927 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
2930 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
2931 if (bgp_evpn_route_rmac_self_check(
2936 ret
= install_evpn_route_entry_in_vrf(
2939 ret
= uninstall_evpn_route_entry_in_vrf(
2945 "Failed to %s EVPN %s route in VRF %s",
2948 prefix2str(evp
, buf
,
2963 * Install or uninstall routes of specified type that are appropriate for this
2966 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
2967 struct bgpevpn
*vpn
,
2968 bgp_evpn_route_type rtype
,
2973 struct bgp_dest
*rd_dest
, *dest
;
2974 struct bgp_table
*table
;
2975 struct bgp_path_info
*pi
;
2981 /* Walk entire global routing table and evaluate routes which could be
2982 * imported into this VPN. Note that we cannot just look at the routes
2984 * the VNI's RD - remote routes applicable for this VNI could have any
2987 /* EVPN routes are a 2-level table. */
2988 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
2989 rd_dest
= bgp_route_next(rd_dest
)) {
2990 table
= bgp_dest_get_bgp_table_info(rd_dest
);
2994 for (dest
= bgp_table_top(table
); dest
;
2995 dest
= bgp_route_next(dest
)) {
2996 const struct prefix_evpn
*evp
=
2997 (const struct prefix_evpn
*)bgp_dest_get_prefix(
3000 if (evp
->prefix
.route_type
!= rtype
)
3003 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
3005 /* Consider "valid" remote routes applicable for
3007 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3008 && pi
->type
== ZEBRA_ROUTE_BGP
3009 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3012 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3014 ret
= install_evpn_route_entry(
3017 ret
= uninstall_evpn_route_entry(
3023 "%u: Failed to %s EVPN %s route in VNI %u",
3027 rtype
== BGP_EVPN_MAC_IP_ROUTE
3041 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3042 * is invoked upon l3vni-add or l3vni import rt change
3044 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3046 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3051 * Install any existing remote routes applicable for this VNI into its
3052 * routing table. This is invoked when a VNI becomes "live" or its Import
3055 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3059 /* Install type-3 routes followed by type-2 routes - the ones applicable
3062 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3067 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_AD_ROUTE
,
3072 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3076 /* uninstall routes from l3vni vrf. */
3077 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3079 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3084 * Uninstall any existing remote routes for this VNI. One scenario in which
3085 * this is invoked is upon an import RT change.
3087 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3091 /* Uninstall type-2 routes followed by type-3 routes - the ones
3095 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3100 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_AD_ROUTE
,
3106 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3111 * Install or uninstall route in matching VRFs (list).
3113 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3114 safi_t safi
, struct prefix_evpn
*evp
,
3115 struct bgp_path_info
*pi
,
3116 struct list
*vrfs
, int install
)
3118 char buf
[PREFIX2STR_BUFFER
];
3119 struct bgp
*bgp_vrf
;
3120 struct listnode
*node
, *nnode
;
3122 /* Only type-2/type-5 routes go into a VRF */
3123 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3124 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3127 /* if it is type-2 route and not a mac+ip route skip this route */
3128 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3129 && !(is_evpn_prefix_ipaddr_v4(evp
)
3130 || is_evpn_prefix_ipaddr_v6(evp
)))
3133 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3137 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3139 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3143 flog_err(EC_BGP_EVPN_FAIL
,
3144 "%u: Failed to %s prefix %s in VRF %s",
3146 install
? "install" : "uninstall",
3147 prefix2str(evp
, buf
, sizeof(buf
)),
3148 vrf_id_to_name(bgp_vrf
->vrf_id
));
3157 * Install or uninstall route in matching VNIs (list).
3159 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3160 safi_t safi
, struct prefix_evpn
*evp
,
3161 struct bgp_path_info
*pi
,
3162 struct list
*vnis
, int install
)
3164 struct bgpevpn
*vpn
;
3165 struct listnode
*node
, *nnode
;
3167 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3170 if (!is_vni_live(vpn
))
3174 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3176 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3179 flog_err(EC_BGP_EVPN_FAIL
,
3180 "%u: Failed to %s EVPN %s route in VNI %u",
3181 bgp
->vrf_id
, install
? "install" : "uninstall",
3182 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3194 * Install or uninstall route for appropriate VNIs/ESIs.
3196 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3197 const struct prefix
*p
,
3198 struct bgp_path_info
*pi
, int import
)
3200 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3201 struct attr
*attr
= pi
->attr
;
3202 struct ecommunity
*ecom
;
3204 struct prefix_evpn ad_evp
;
3208 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3209 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3210 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3211 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3212 || evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
3213 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3216 /* If we don't have Route Target, nothing much to do. */
3217 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3220 /* EAD prefix in the global table doesn't include the VTEP-IP so
3221 * we need to create a different copy for the VNI
3223 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
3224 evp
= evpn_type1_prefix_vni_copy(&ad_evp
, evp
, attr
->nexthop
);
3226 ecom
= attr
->ecommunity
;
3227 if (!ecom
|| !ecom
->size
)
3230 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3231 * attached to the route */
3232 for (i
= 0; i
< ecom
->size
; i
++) {
3234 uint8_t type
, sub_type
;
3235 struct ecommunity_val
*eval
;
3236 struct ecommunity_val eval_tmp
;
3237 struct irt_node
*irt
; /* import rt for l2vni */
3238 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3239 struct bgp_evpn_es
*es
;
3241 /* Only deal with RTs */
3242 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
3243 eval
= (struct ecommunity_val
*)(ecom
->val
3244 + (i
* ecom
->unit_size
));
3247 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3251 * macip routes (type-2) are imported into VNI and VRF tables.
3252 * IMET route is imported into VNI table.
3253 * prefix routes are imported into VRF table.
3255 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3256 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3257 evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
||
3258 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3260 irt
= lookup_import_rt(bgp
, eval
);
3262 install_uninstall_route_in_vnis(
3263 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3266 vrf_irt
= lookup_vrf_import_rt(eval
);
3268 install_uninstall_route_in_vrfs(
3269 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3272 /* Also check for non-exact match.
3273 * In this, we mask out the AS and
3274 * only check on the local-admin sub-field.
3275 * This is to facilitate using
3276 * VNI as the RT for EBGP peering too.
3280 if (type
== ECOMMUNITY_ENCODE_AS
3281 || type
== ECOMMUNITY_ENCODE_AS4
3282 || type
== ECOMMUNITY_ENCODE_IP
) {
3283 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
3284 mask_ecom_global_admin(&eval_tmp
, eval
);
3285 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3286 vrf_irt
= lookup_vrf_import_rt(&eval_tmp
);
3290 install_uninstall_route_in_vnis(
3291 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3294 install_uninstall_route_in_vrfs(
3295 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3299 /* es route is imported into the es table */
3300 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3302 /* we will match based on the entire esi to avoid
3303 * imoort of an es route for esi2 into esi1
3305 es
= bgp_evpn_es_find(&evp
->prefix
.es_addr
.esi
);
3306 if (es
&& bgp_evpn_is_es_local(es
))
3307 bgp_evpn_es_route_install_uninstall(
3308 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3316 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3319 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3321 /* Delete ipv4 default route and withdraw from peers */
3322 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3323 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3324 SAFI_UNICAST
, false);
3326 /* delete all ipv4 routes and withdraw from peers */
3327 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3328 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3330 /* Delete ipv6 default route and withdraw from peers */
3331 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3332 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3333 SAFI_UNICAST
, false);
3335 /* delete all ipv6 routes and withdraw from peers */
3336 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3337 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3341 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3344 void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3346 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
3348 bgp_evpn
= bgp_get_evpn();
3352 /* update all ipv4 routes */
3353 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3354 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3356 /* update ipv4 default route and withdraw from peers */
3357 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3358 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3359 SAFI_UNICAST
, true);
3361 /* update all ipv6 routes */
3362 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3363 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3365 /* update ipv6 default route and withdraw from peers */
3366 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3367 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3368 SAFI_UNICAST
, true);
3373 * update and advertise local routes for a VRF as type-5 routes.
3374 * This is invoked upon RD change for a VRF. Note taht the processing is only
3375 * done in the global route table using the routes which already exist in the
3378 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3380 /* skip if the RD is configured */
3381 if (is_vrf_rd_configured(bgp_vrf
))
3384 /* derive the RD for the VRF based on new router-id */
3385 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3387 /* update advertise ipv4|ipv6 routes as type-5 routes */
3388 update_advertise_vrf_routes(bgp_vrf
);
3392 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3393 * This is invoked upon VRF RD change. The processing is done only from global
3396 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3398 /* skip if the RD is configured */
3399 if (is_vrf_rd_configured(bgp_vrf
))
3402 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3403 delete_withdraw_vrf_routes(bgp_vrf
);
3407 * Update and advertise local routes for a VNI. Invoked upon router-id
3408 * change. Note that the processing is done only on the global route table
3409 * using routes that already exist in the per-VNI table.
3411 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3413 struct prefix_evpn p
;
3414 struct bgp_dest
*dest
, *global_dest
;
3415 struct bgp_path_info
*pi
, *global_pi
;
3417 afi_t afi
= AFI_L2VPN
;
3418 safi_t safi
= SAFI_EVPN
;
3420 /* Locate type-3 route for VNI in the per-VNI table and use its
3421 * attributes to create and advertise the type-3 route for this VNI
3422 * in the global table.
3424 * RT-3 only if doing head-end replication
3426 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3427 == VXLAN_FLOOD_HEAD_END_REPL
) {
3428 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3429 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3430 if (!dest
) /* unexpected */
3432 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
3433 if (pi
->peer
== bgp
->peer_self
&&
3434 pi
->type
== ZEBRA_ROUTE_BGP
3435 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3437 if (!pi
) /* unexpected */
3441 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
],
3442 afi
, safi
, &p
, &vpn
->prd
);
3443 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_dest
, attr
,
3444 1, &pi
, 0, mac_mobility_seqnum(attr
),
3445 false /* setup_sync */, NULL
/* old_is_sync */);
3447 /* Schedule for processing and unlock node. */
3448 bgp_process(bgp
, global_dest
, afi
, safi
);
3449 bgp_dest_unlock_node(global_dest
);
3452 /* Now, walk this VNI's route table and use the route and its attribute
3453 * to create and schedule route in global table.
3455 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
3456 dest
= bgp_route_next(dest
)) {
3457 const struct prefix_evpn
*evp
=
3458 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
3460 /* Identify MAC-IP local routes. */
3461 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
3464 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
3465 if (pi
->peer
== bgp
->peer_self
3466 && pi
->type
== ZEBRA_ROUTE_BGP
3467 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3472 /* Create route in global routing table using this route entry's
3476 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3478 assert(global_dest
);
3479 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_dest
, attr
, 1,
3481 mac_mobility_seqnum(attr
),
3482 false /* setup_sync */, NULL
/* old_is_sync */);
3484 /* Schedule for processing and unlock node. */
3485 bgp_process(bgp
, global_dest
, afi
, safi
);
3486 bgp_dest_unlock_node(global_dest
);
3493 * Delete (and withdraw) local routes for a VNI - only from the global
3494 * table. Invoked upon router-id change.
3496 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3499 struct prefix_evpn p
;
3500 struct bgp_dest
*global_dest
;
3501 struct bgp_path_info
*pi
;
3502 afi_t afi
= AFI_L2VPN
;
3503 safi_t safi
= SAFI_EVPN
;
3505 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3506 * for this VNI - from the global table.
3508 ret
= delete_global_type2_routes(bgp
, vpn
);
3512 /* Remove type-3 route for this VNI from global table. */
3513 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3514 global_dest
= bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3515 (const struct prefix_evpn
*)&p
, &vpn
->prd
);
3517 /* Delete route entry in the global EVPN table. */
3518 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
3520 /* Schedule for processing - withdraws to peers happen from
3524 bgp_process(bgp
, global_dest
, afi
, safi
);
3525 bgp_dest_unlock_node(global_dest
);
3532 * Handle router-id change. Update and advertise local routes corresponding
3533 * to this VNI from peers. Note that this is invoked after updating the
3534 * router-id. The routes in the per-VNI table are used to create routes in
3535 * the global table and schedule them.
3537 static void update_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3539 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3541 /* Skip VNIs with configured RD. */
3542 if (is_rd_configured(vpn
))
3545 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3546 update_advertise_vni_routes(bgp
, vpn
);
3550 * Handle router-id change. Delete and withdraw local routes corresponding
3551 * to this VNI from peers. Note that this is invoked prior to updating
3552 * the router-id and is done only on the global route table, the routes
3553 * are needed in the per-VNI table to re-advertise with new router id.
3555 static void withdraw_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3557 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3559 /* Skip VNIs with configured RD. */
3560 if (is_rd_configured(vpn
))
3563 delete_withdraw_vni_routes(bgp
, vpn
);
3567 * Create RT-3 for a VNI and schedule for processing and advertisement.
3568 * This is invoked upon flooding mode changing to head-end replication.
3570 static void create_advertise_type3(struct hash_bucket
*bucket
, void *data
)
3572 struct bgpevpn
*vpn
= bucket
->data
;
3573 struct bgp
*bgp
= data
;
3574 struct prefix_evpn p
;
3576 if (!vpn
|| !is_vni_live(vpn
) ||
3577 bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3578 != VXLAN_FLOOD_HEAD_END_REPL
)
3581 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3582 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
))
3583 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3584 "Type3 route creation failure for VNI %u", vpn
->vni
);
3588 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3589 * This is invoked upon flooding mode changing to drop BUM packets.
3591 static void delete_withdraw_type3(struct hash_bucket
*bucket
, void *data
)
3593 struct bgpevpn
*vpn
= bucket
->data
;
3594 struct bgp
*bgp
= data
;
3595 struct prefix_evpn p
;
3597 if (!vpn
|| !is_vni_live(vpn
))
3600 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3601 delete_evpn_route(bgp
, vpn
, &p
);
3605 * Process received EVPN type-2 route (advertise or withdraw).
3607 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3608 struct attr
*attr
, uint8_t *pfx
, int psize
,
3609 uint32_t addpath_id
)
3611 struct prefix_rd prd
;
3612 struct prefix_evpn p
= {};
3613 struct bgp_route_evpn evpn
= {};
3615 uint8_t macaddr_len
;
3616 /* holds the VNI(s) as in packet */
3617 mpls_label_t label
[BGP_MAX_LABELS
] = {};
3618 uint32_t num_labels
= 0;
3622 /* Type-2 route should be either 33, 37 or 49 bytes or an
3623 * additional 3 bytes if there is a second label (VNI):
3624 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3625 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3626 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3628 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3629 && psize
!= 40 && psize
!= 52) {
3630 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3631 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3632 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3636 struct stream
*pkt
= stream_new(psize
);
3637 stream_put(pkt
, pfx
, psize
);
3639 /* Make prefix_rd */
3640 prd
.family
= AF_UNSPEC
;
3643 STREAM_GET(&prd
.val
, pkt
, 8);
3645 /* Make EVPN prefix. */
3647 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3648 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3650 /* Copy Ethernet Seg Identifier */
3652 STREAM_GET(&attr
->esi
, pkt
, sizeof(esi_t
));
3654 if (bgp_evpn_is_esi_local(&attr
->esi
))
3655 attr
->es_flags
|= ATTR_ES_IS_LOCAL
;
3657 attr
->es_flags
&= ~ATTR_ES_IS_LOCAL
;
3659 STREAM_FORWARD_GETP(pkt
, sizeof(esi_t
));
3662 /* Copy Ethernet Tag */
3663 STREAM_GET(ð_tag
, pkt
, 4);
3664 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3666 /* Get the MAC Addr len */
3667 STREAM_GETC(pkt
, macaddr_len
);
3669 /* Get the MAC Addr */
3670 if (macaddr_len
== (ETH_ALEN
* 8)) {
3671 STREAM_GET(&p
.prefix
.macip_addr
.mac
.octet
, pkt
, ETH_ALEN
);
3674 EC_BGP_EVPN_ROUTE_INVALID
,
3675 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3676 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3682 STREAM_GETC(pkt
, ipaddr_len
);
3684 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3685 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3687 EC_BGP_EVPN_ROUTE_INVALID
,
3688 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3689 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3694 ipaddr_len
/= 8; /* Convert to bytes. */
3695 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
3698 STREAM_GET(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pkt
, ipaddr_len
);
3701 /* Get the VNI(s). Stored as bytes here. */
3702 STREAM_GET(&label
[0], pkt
, BGP_LABEL_BYTES
);
3705 /* Do we have a second VNI? */
3706 if (STREAM_READABLE(pkt
)) {
3708 STREAM_GET(&label
[1], pkt
, BGP_LABEL_BYTES
);
3711 /* Process the route. */
3713 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3714 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3715 &prd
, &label
[0], num_labels
, 0, &evpn
);
3717 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3718 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3719 &prd
, &label
[0], num_labels
, &evpn
);
3724 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3725 "%u:%s - Rx EVPN Type-2 NLRI - corrupt, discarding",
3726 peer
->bgp
->vrf_id
, peer
->host
);
3734 * Process received EVPN type-3 route (advertise or withdraw).
3736 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3737 struct attr
*attr
, uint8_t *pfx
, int psize
,
3738 uint32_t addpath_id
)
3740 struct prefix_rd prd
;
3741 struct prefix_evpn p
;
3746 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3747 * IP len (1) and IP (4 or 16).
3749 if (psize
!= 17 && psize
!= 29) {
3750 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3751 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3752 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3756 /* If PMSI is present, log if it is anything other than IR.
3757 * Note: We just simply ignore the values as it is not clear if
3758 * doing anything else is better.
3761 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
3762 if (attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
&&
3763 attr
->pmsi_tnl_type
!= PMSI_TNLTYPE_PIM_SM
) {
3764 flog_warn(EC_BGP_EVPN_PMSI_PRESENT
,
3765 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3766 peer
->bgp
->vrf_id
, peer
->host
,
3767 attr
->pmsi_tnl_type
);
3771 /* Make prefix_rd */
3772 prd
.family
= AF_UNSPEC
;
3774 memcpy(&prd
.val
, pfx
, 8);
3777 /* Make EVPN prefix. */
3778 memset(&p
, 0, sizeof(struct prefix_evpn
));
3780 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3781 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
3783 /* Copy Ethernet Tag */
3784 memcpy(ð_tag
, pfx
, 4);
3785 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
3789 ipaddr_len
= *pfx
++;
3790 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
3791 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
3792 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
3795 EC_BGP_EVPN_ROUTE_INVALID
,
3796 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3797 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
3801 /* Process the route. */
3803 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3804 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3805 &prd
, NULL
, 0, 0, NULL
);
3807 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3808 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3809 &prd
, NULL
, 0, NULL
);
3814 * Process received EVPN type-5 route (advertise or withdraw).
3816 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3817 struct attr
*attr
, uint8_t *pfx
, int psize
,
3818 uint32_t addpath_id
)
3820 struct prefix_rd prd
;
3821 struct prefix_evpn p
;
3822 struct bgp_route_evpn evpn
;
3825 mpls_label_t label
; /* holds the VNI as in the packet */
3828 bool is_valid_update
= false;
3830 /* Type-5 route should be 34 or 58 bytes:
3831 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3832 * GW (4 or 16) and VNI (3).
3833 * Note that the IP and GW should both be IPv4 or both IPv6.
3835 if (psize
!= 34 && psize
!= 58) {
3836 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3837 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3838 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3842 /* Make prefix_rd */
3843 prd
.family
= AF_UNSPEC
;
3845 memcpy(&prd
.val
, pfx
, 8);
3848 /* Make EVPN prefix. */
3849 memset(&p
, 0, sizeof(struct prefix_evpn
));
3851 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3852 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
3854 /* Additional information outside of prefix - ESI and GW IP */
3855 memset(&evpn
, 0, sizeof(evpn
));
3859 memcpy(&attr
->esi
, pfx
, sizeof(esi_t
));
3862 /* Fetch Ethernet Tag. */
3863 memcpy(ð_tag
, pfx
, 4);
3864 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
3867 /* Fetch IP prefix length. */
3869 if (ippfx_len
> IPV6_MAX_BITLEN
) {
3871 EC_BGP_EVPN_ROUTE_INVALID
,
3872 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3873 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
3876 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
3878 /* Determine IPv4 or IPv6 prefix */
3879 /* Since the address and GW are from the same family, this just becomes
3880 * a simple check on the total size.
3883 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
3884 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
3886 memcpy(&evpn
.gw_ip
.ipv4
, pfx
, 4);
3890 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
3891 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
, 16);
3893 memcpy(&evpn
.gw_ip
.ipv6
, pfx
, 16);
3898 /* Get the VNI (in MPLS label field). Stored as bytes here. */
3899 memset(&label
, 0, sizeof(label
));
3900 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
3903 * If in future, we are required to access additional fields,
3904 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
3909 is_valid_update
= true;
3910 if (is_zero_mac(&attr
->rmac
) &&
3911 is_zero_gw_ip(&evpn
.gw_ip
, gw_afi
))
3912 is_valid_update
= false;
3914 if (is_mcast_mac(&attr
->rmac
) || is_bcast_mac(&attr
->rmac
))
3915 is_valid_update
= false;
3918 /* Process the route. */
3919 if (is_valid_update
)
3920 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3921 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3922 &prd
, &label
, 1, 0, &evpn
);
3924 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
3925 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
3926 &prd
, &label
, 1, &evpn
);
3931 static void evpn_mpattr_encode_type5(struct stream
*s
, const struct prefix
*p
,
3932 const struct prefix_rd
*prd
,
3933 mpls_label_t
*label
, uint32_t num_labels
,
3938 const struct evpn_addr
*p_evpn_p
;
3940 memset(&temp
, 0, 16);
3941 if (p
->family
!= AF_EVPN
)
3943 p_evpn_p
= &(p
->u
.prefix_evpn
);
3945 /* len denites the total len of IP and GW-IP in the route
3946 IP and GW-IP have to be both ipv4 or ipv6
3948 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3949 len
= 8; /* IP and GWIP are both ipv4 */
3951 len
= 32; /* IP and GWIP are both ipv6 */
3952 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
3953 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
3954 stream_put(s
, prd
->val
, 8);
3956 stream_put(s
, &attr
->esi
, sizeof(esi_t
));
3958 stream_put(s
, 0, sizeof(esi_t
));
3959 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
3960 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
3961 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3962 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
3964 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
3966 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3968 attr
->evpn_overlay
.gw_ip
.ipv4
.s_addr
);
3970 stream_put(s
, &(attr
->evpn_overlay
.gw_ip
.ipv6
), 16);
3972 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
3973 stream_put_ipv4(s
, 0);
3975 stream_put(s
, &temp
, 16);
3979 stream_put(s
, label
, 3);
3985 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
3987 static void cleanup_vni_on_disable(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3989 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3991 /* Remove EVPN routes and schedule for processing. */
3992 delete_routes_for_vni(bgp
, vpn
);
3994 /* Clear "live" flag and see if hash needs to be freed. */
3995 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
3996 if (!is_vni_configured(vpn
))
3997 bgp_evpn_free(bgp
, vpn
);
4001 * Free a VNI entry; iterator function called during cleanup.
4003 static void free_vni_entry(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4005 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4007 delete_all_vni_routes(bgp
, vpn
);
4008 bgp_evpn_free(bgp
, vpn
);
4012 * Derive AUTO import RT for BGP VRF - L3VNI
4014 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4016 struct bgp
*bgp_evpn
= NULL
;
4018 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4019 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4022 bgp_evpn
= bgp_get_evpn();
4025 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4029 * Delete AUTO import RT from BGP VRF - L3VNI
4031 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4033 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
);
4037 * Derive AUTO export RT for BGP VRF - L3VNI
4039 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4041 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4042 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4046 * Delete AUTO export RT from BGP VRF - L3VNI
4048 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4050 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
);
4053 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4055 struct bgp
*bgp_evpn
= NULL
;
4056 struct listnode
*node
= NULL
;
4057 struct bgpevpn
*vpn
= NULL
;
4059 bgp_evpn
= bgp_get_evpn();
4063 /* update all type-5 routes */
4064 update_advertise_vrf_routes(bgp_vrf
);
4066 /* update all type-2 routes */
4067 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4068 update_routes_for_vni(bgp_evpn
, vpn
);
4072 * Handle autort change for a given VNI.
4074 static void update_autort_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4076 struct bgpevpn
*vpn
= bucket
->data
;
4078 if (!is_import_rt_configured(vpn
)) {
4079 if (is_vni_live(vpn
))
4080 bgp_evpn_uninstall_routes(bgp
, vpn
);
4081 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4082 list_delete_all_node(vpn
->import_rtl
);
4083 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4084 if (is_vni_live(vpn
))
4085 bgp_evpn_install_routes(bgp
, vpn
);
4087 if (!is_export_rt_configured(vpn
)) {
4088 list_delete_all_node(vpn
->export_rtl
);
4089 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4090 if (is_vni_live(vpn
))
4091 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4099 /* withdraw type-5 route corresponding to ip prefix */
4100 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4101 afi_t afi
, safi_t safi
)
4104 struct prefix_evpn evp
;
4105 char buf
[PREFIX_STRLEN
];
4107 build_type5_prefix_from_ip_prefix(&evp
, p
);
4108 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4111 EC_BGP_EVPN_ROUTE_DELETE
,
4112 "%u failed to delete type-5 route for prefix %s in vrf %s",
4113 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)),
4114 vrf_id_to_name(bgp_vrf
->vrf_id
));
4118 /* withdraw all type-5 routes for an address family */
4119 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4121 struct bgp_table
*table
= NULL
;
4122 struct bgp_dest
*dest
= NULL
;
4123 struct bgp_path_info
*pi
;
4125 table
= bgp_vrf
->rib
[afi
][safi
];
4126 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
4127 /* Only care about "selected" routes. Also ensure that
4128 * these are routes that are injectable into EVPN.
4130 /* TODO: Support for AddPath for EVPN. */
4131 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
4132 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4133 && is_route_injectable_into_evpn(pi
)) {
4134 bgp_evpn_withdraw_type5_route(
4135 bgp_vrf
, bgp_dest_get_prefix(dest
), afi
,
4144 * evpn - enable advertisement of default g/w
4146 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
4147 safi_t safi
, bool add
)
4149 struct prefix ip_prefix
;
4151 /* form the default prefix 0.0.0.0/0 */
4152 memset(&ip_prefix
, 0, sizeof(struct prefix
));
4153 ip_prefix
.family
= afi2family(afi
);
4156 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
4159 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
4166 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4167 * to this function correspond to those of the source IP prefix (best
4168 * path in the case of the attr. In the case of a local prefix (when we
4169 * are advertising local subnets), the src_attr will be NULL.
4171 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4172 struct attr
*src_attr
, afi_t afi
,
4176 struct prefix_evpn evp
;
4177 char buf
[PREFIX_STRLEN
];
4179 build_type5_prefix_from_ip_prefix(&evp
, p
);
4180 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
);
4182 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4183 "%u: Failed to create type-5 route for prefix %s",
4184 bgp_vrf
->vrf_id
, prefix2str(p
, buf
, sizeof(buf
)));
4187 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4188 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4189 * advertisement is enabled.
4191 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4194 struct bgp_table
*table
= NULL
;
4195 struct bgp_dest
*dest
= NULL
;
4196 struct bgp_path_info
*pi
;
4198 table
= bgp_vrf
->rib
[afi
][safi
];
4199 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
4200 /* Need to identify the "selected" route entry to use its
4201 * attribute. Also, ensure that the route is injectable
4203 * TODO: Support for AddPath for EVPN.
4205 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
4206 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4207 && is_route_injectable_into_evpn(pi
)) {
4209 /* apply the route-map */
4210 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4211 route_map_result_t ret
;
4212 struct bgp_path_info tmp_pi
;
4213 struct bgp_path_info_extra tmp_pie
;
4214 struct attr tmp_attr
;
4216 tmp_attr
= *pi
->attr
;
4218 /* Fill temp path_info */
4219 prep_for_rmap_apply(&tmp_pi
, &tmp_pie
,
4223 RESET_FLAG(tmp_attr
.rmap_change_flags
);
4225 ret
= route_map_apply(
4226 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4228 bgp_dest_get_prefix(dest
),
4230 if (ret
== RMAP_DENYMATCH
) {
4231 bgp_attr_flush(&tmp_attr
);
4234 bgp_evpn_advertise_type5_route(
4236 bgp_dest_get_prefix(dest
),
4237 &tmp_attr
, afi
, safi
);
4239 bgp_evpn_advertise_type5_route(
4241 bgp_dest_get_prefix(dest
),
4242 pi
->attr
, afi
, safi
);
4249 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
)
4251 struct listnode
*node
, *nnode
, *node_to_del
;
4252 struct ecommunity
*ecom
, *ecom_auto
;
4253 struct ecommunity_val eval
;
4255 if (bgp
->advertise_autort_rfc8365
)
4256 vni
|= EVPN_AUTORT_VXLAN
;
4257 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4259 ecom_auto
= ecommunity_new();
4260 ecommunity_add_val(ecom_auto
, &eval
, false, false);
4263 for (ALL_LIST_ELEMENTS(rtl
, node
, nnode
, ecom
)) {
4264 if (ecommunity_match(ecom
, ecom_auto
)) {
4265 ecommunity_free(&ecom
);
4271 list_delete_node(rtl
, node_to_del
);
4273 ecommunity_free(&ecom_auto
);
4276 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4277 struct ecommunity
*ecomadd
)
4279 /* uninstall routes from vrf */
4280 if (is_l3vni_live(bgp_vrf
))
4281 uninstall_routes_for_vrf(bgp_vrf
);
4283 /* Cleanup the RT to VRF mapping */
4284 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4286 /* Remove auto generated RT */
4287 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4289 /* Add the newly configured RT to RT list */
4290 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, ecomadd
);
4291 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4293 /* map VRF to its RTs and install routes matching the new RTs */
4294 if (is_l3vni_live(bgp_vrf
)) {
4295 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4296 install_routes_for_vrf(bgp_vrf
);
4300 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4301 struct ecommunity
*ecomdel
)
4303 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4304 struct ecommunity
*ecom
= NULL
;
4306 /* uninstall routes from vrf */
4307 if (is_l3vni_live(bgp_vrf
))
4308 uninstall_routes_for_vrf(bgp_vrf
);
4310 /* Cleanup the RT to VRF mapping */
4311 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4313 /* remove the RT from the RT list */
4314 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4315 if (ecommunity_match(ecom
, ecomdel
)) {
4316 ecommunity_free(&ecom
);
4323 list_delete_node(bgp_vrf
->vrf_import_rtl
, node_to_del
);
4325 assert(bgp_vrf
->vrf_import_rtl
);
4326 /* fallback to auto import rt, if this was the last RT */
4327 if (bgp_vrf
->vrf_import_rtl
&& list_isempty(bgp_vrf
->vrf_import_rtl
)) {
4328 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4329 if (is_l3vni_live(bgp_vrf
))
4330 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4333 /* map VRFs to its RTs and install routes matching this new RT */
4334 if (is_l3vni_live(bgp_vrf
)) {
4335 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4336 install_routes_for_vrf(bgp_vrf
);
4340 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4341 struct ecommunity
*ecomadd
)
4343 /* remove auto-generated RT */
4344 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4346 /* Add the new RT to the RT list */
4347 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, ecomadd
);
4348 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4350 if (is_l3vni_live(bgp_vrf
))
4351 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4354 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4355 struct ecommunity
*ecomdel
)
4357 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4358 struct ecommunity
*ecom
= NULL
;
4360 /* Remove the RT from the RT list */
4361 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_export_rtl
, node
, nnode
, ecom
)) {
4362 if (ecommunity_match(ecom
, ecomdel
)) {
4363 ecommunity_free(&ecom
);
4370 list_delete_node(bgp_vrf
->vrf_export_rtl
, node_to_del
);
4373 * Temporary assert to make SA happy.
4374 * The ALL_LIST_ELEMENTS macro above has a NULL check
4375 * which means that SA is going to complain about
4376 * the list_isempty call, which doesn't NULL check.
4377 * So until we get this situation cleaned up, here
4380 assert(bgp_vrf
->vrf_export_rtl
);
4382 /* fall back to auto-generated RT if this was the last RT */
4383 if (list_isempty(bgp_vrf
->vrf_export_rtl
)) {
4384 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4385 if (is_l3vni_live(bgp_vrf
))
4386 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4389 if (is_l3vni_live(bgp_vrf
))
4390 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4394 * Handle change to BGP router id. This is invoked twice by the change
4395 * handler, first before the router id has been changed and then after
4396 * the router id has been changed. The first invocation will result in
4397 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4398 * will result in the routes being re-advertised.
4400 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4402 struct listnode
*node
;
4403 struct bgp
*bgp_vrf
;
4407 /* delete and withdraw all the type-5 routes
4408 stored in the global table for this vrf
4410 withdraw_router_id_vrf(bgp
);
4412 /* delete all the VNI routes (type-2/type-3) routes for all the
4415 hash_iterate(bgp
->vnihash
,
4416 (void (*)(struct hash_bucket
*,
4417 void *))withdraw_router_id_vni
,
4420 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4421 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4422 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4423 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4425 bgp_vrf
->evpn_info
->pip_ip
.s_addr
4431 /* Assign new default instance router-id */
4432 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4433 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4434 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4435 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4437 bgp_vrf
->evpn_info
->pip_ip
=
4439 /* advertise type-5 routes with
4442 update_advertise_vrf_routes(bgp_vrf
);
4447 /* advertise all routes in the vrf as type-5 routes with the new
4450 update_router_id_vrf(bgp
);
4452 /* advertise all the VNI routes (type-2/type-3) routes with the
4455 hash_iterate(bgp
->vnihash
,
4456 (void (*)(struct hash_bucket
*,
4457 void *))update_router_id_vni
,
4463 * Handle change to auto-RT algorithm - update and advertise local routes.
4465 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4467 hash_iterate(bgp
->vnihash
,
4468 (void (*)(struct hash_bucket
*,
4469 void*))update_autort_vni
,
4474 * Handle change to export RT - update and advertise local routes.
4476 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4478 return update_routes_for_vni(bgp
, vpn
);
4481 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4484 delete_withdraw_vrf_routes(bgp_vrf
);
4486 update_advertise_vrf_routes(bgp_vrf
);
4490 * Handle change to RD. This is invoked twice by the change handler,
4491 * first before the RD has been changed and then after the RD has
4492 * been changed. The first invocation will result in local routes
4493 * of this VNI being deleted and withdrawn and the next will result
4494 * in the routes being re-advertised.
4496 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4500 delete_withdraw_vni_routes(bgp
, vpn
);
4502 update_advertise_vni_routes(bgp
, vpn
);
4506 * Install routes for this VNI. Invoked upon change to Import RT.
4508 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4510 return install_routes_for_vni(bgp
, vpn
);
4514 * Uninstall all routes installed for this VNI. Invoked upon change
4517 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4519 return uninstall_routes_for_vni(bgp
, vpn
);
4523 * TODO: Hardcoded for a maximum of 2 VNIs right now
4525 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
4530 vni1
= label2vni(label
);
4531 if (num_labels
== 2) {
4532 vni2
= label2vni(label
+ 1);
4533 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
4535 snprintf(buf
, len
, "%u", vni1
);
4540 * Function to convert evpn route to json format.
4541 * NOTE: We don't use prefix2str as the output here is a bit different.
4543 void bgp_evpn_route2json(const struct prefix_evpn
*p
, json_object
*json
)
4545 char buf1
[ETHER_ADDR_STRLEN
];
4546 char buf2
[PREFIX2STR_BUFFER
];
4553 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
4555 switch (p
->prefix
.route_type
) {
4556 case BGP_EVPN_MAC_IP_ROUTE
:
4557 json_object_int_add(json
, "ethTag",
4558 p
->prefix
.macip_addr
.eth_tag
);
4559 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
4560 json_object_string_add(json
, "mac",
4561 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
4564 if (!is_evpn_prefix_ipaddr_none(p
)) {
4565 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
:
4567 prefixlen
= (family
== AF_INET
) ?
4568 IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
;
4569 inet_ntop(family
, &p
->prefix
.macip_addr
.ip
.ip
.addr
,
4570 buf2
, PREFIX2STR_BUFFER
);
4571 json_object_int_add(json
, "ipLen", prefixlen
);
4572 json_object_string_add(json
, "ip", buf2
);
4576 case BGP_EVPN_IMET_ROUTE
:
4577 json_object_int_add(json
, "ethTag",
4578 p
->prefix
.imet_addr
.eth_tag
);
4579 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4580 prefixlen
= (family
== AF_INET
) ? IPV4_MAX_BITLEN
:
4582 inet_ntop(family
, &p
->prefix
.imet_addr
.ip
.ip
.addr
, buf2
,
4584 json_object_int_add(json
, "ipLen", prefixlen
);
4585 json_object_string_add(json
, "ip", buf2
);
4588 case BGP_EVPN_IP_PREFIX_ROUTE
:
4589 json_object_int_add(json
, "ethTag",
4590 p
->prefix
.prefix_addr
.eth_tag
);
4591 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
4592 inet_ntop(family
, &p
->prefix
.prefix_addr
.ip
.ip
.addr
,
4593 buf2
, sizeof(buf2
));
4594 json_object_int_add(json
, "ipLen",
4595 p
->prefix
.prefix_addr
.ip_prefix_length
);
4596 json_object_string_add(json
, "ip", buf2
);
4605 * Encode EVPN prefix in Update (MP_REACH)
4607 void bgp_evpn_encode_prefix(struct stream
*s
, const struct prefix
*p
,
4608 const struct prefix_rd
*prd
, mpls_label_t
*label
,
4609 uint32_t num_labels
, struct attr
*attr
,
4610 int addpath_encode
, uint32_t addpath_tx_id
)
4612 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
4613 int len
, ipa_len
= 0;
4616 stream_putl(s
, addpath_tx_id
);
4619 stream_putc(s
, evp
->prefix
.route_type
);
4621 switch (evp
->prefix
.route_type
) {
4622 case BGP_EVPN_MAC_IP_ROUTE
:
4623 if (is_evpn_prefix_ipaddr_v4(evp
))
4624 ipa_len
= IPV4_MAX_BYTELEN
;
4625 else if (is_evpn_prefix_ipaddr_v6(evp
))
4626 ipa_len
= IPV6_MAX_BYTELEN
;
4627 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4628 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
4629 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
4631 stream_putc(s
, len
);
4632 stream_put(s
, prd
->val
, 8); /* RD */
4634 stream_put(s
, &attr
->esi
, ESI_BYTES
);
4636 stream_put(s
, 0, 10);
4637 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
4638 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
4639 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
4640 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
4641 if (ipa_len
) /* IP */
4642 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
4644 /* 1st label is the L2 VNI */
4645 stream_put(s
, label
, BGP_LABEL_BYTES
);
4646 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4647 if (ipa_len
&& num_labels
> 1)
4648 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
4651 case BGP_EVPN_IMET_ROUTE
:
4652 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
4653 stream_put(s
, prd
->val
, 8); /* RD */
4654 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
4655 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4656 /* Originating Router's IP Addr */
4657 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
4660 case BGP_EVPN_ES_ROUTE
:
4661 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
4662 stream_put(s
, prd
->val
, 8); /* RD */
4663 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
4664 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
4666 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
4669 case BGP_EVPN_AD_ROUTE
:
4670 /* RD, ESI, EthTag, 1 VNI */
4671 len
= RD_BYTES
+ ESI_BYTES
+ EVPN_ETH_TAG_BYTES
+ BGP_LABEL_BYTES
;
4672 stream_putc(s
, len
);
4673 stream_put(s
, prd
->val
, RD_BYTES
); /* RD */
4674 stream_put(s
, evp
->prefix
.ead_addr
.esi
.val
, ESI_BYTES
); /* ESI */
4675 stream_putl(s
, evp
->prefix
.ead_addr
.eth_tag
); /* Ethernet Tag */
4676 stream_put(s
, label
, BGP_LABEL_BYTES
);
4679 case BGP_EVPN_IP_PREFIX_ROUTE
:
4680 /* TODO: AddPath support. */
4681 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
4689 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
4690 struct bgp_nlri
*packet
, int withdraw
)
4696 uint32_t addpath_id
;
4697 int addpath_encoded
;
4702 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4704 lim
= pnt
+ packet
->length
;
4706 safi
= packet
->safi
;
4710 (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
4711 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
4712 PEER_CAP_ADDPATH_AF_TX_RCV
));
4714 for (; pnt
< lim
; pnt
+= psize
) {
4715 /* Clear prefix structure. */
4716 memset(&p
, 0, sizeof(struct prefix
));
4718 /* Deal with path-id if AddPath is supported. */
4719 if (addpath_encoded
) {
4720 /* When packet overflow occurs return immediately. */
4721 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4722 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
4724 memcpy(&addpath_id
, pnt
, BGP_ADDPATH_ID_LEN
);
4725 addpath_id
= ntohl(addpath_id
);
4726 pnt
+= BGP_ADDPATH_ID_LEN
;
4729 /* All EVPN NLRI types start with type and length. */
4731 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE
;
4736 /* When packet overflow occur return immediately. */
4737 if (pnt
+ psize
> lim
)
4738 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
4741 case BGP_EVPN_MAC_IP_ROUTE
:
4742 if (process_type2_route(peer
, afi
, safi
,
4743 withdraw
? NULL
: attr
, pnt
,
4744 psize
, addpath_id
)) {
4747 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4748 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4749 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE
;
4753 case BGP_EVPN_IMET_ROUTE
:
4754 if (process_type3_route(peer
, afi
, safi
,
4755 withdraw
? NULL
: attr
, pnt
,
4756 psize
, addpath_id
)) {
4759 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4760 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4761 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE
;
4765 case BGP_EVPN_ES_ROUTE
:
4766 if (bgp_evpn_type4_route_process(peer
, afi
, safi
,
4767 withdraw
? NULL
: attr
, pnt
,
4768 psize
, addpath_id
)) {
4771 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4772 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4773 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE
;
4777 case BGP_EVPN_AD_ROUTE
:
4778 if (bgp_evpn_type1_route_process(peer
, afi
, safi
,
4779 withdraw
? NULL
: attr
, pnt
,
4780 psize
, addpath_id
)) {
4783 "%u:%s - Error in processing EVPN type-1 NLRI size %d",
4784 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4785 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE1_SIZE
;
4789 case BGP_EVPN_IP_PREFIX_ROUTE
:
4790 if (process_type5_route(peer
, afi
, safi
,
4791 withdraw
? NULL
: attr
, pnt
,
4792 psize
, addpath_id
)) {
4795 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4796 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4797 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE
;
4806 /* Packet length consistency check. */
4808 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH
;
4810 return BGP_NLRI_PARSE_OK
;
4814 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
4815 * The mapping will be used during route processing.
4816 * bgp_def: default bgp instance
4817 * bgp_vrf: specific bgp vrf instance on which RT is configured
4819 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
4822 struct ecommunity_val
*eval
= NULL
;
4823 struct listnode
*node
= NULL
, *nnode
= NULL
;
4824 struct ecommunity
*ecom
= NULL
;
4826 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4827 for (i
= 0; i
< ecom
->size
; i
++) {
4828 eval
= (struct ecommunity_val
*)(ecom
->val
4830 * ECOMMUNITY_SIZE
));
4831 map_vrf_to_rt(bgp_vrf
, eval
);
4837 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
4839 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
4842 struct ecommunity_val
*eval
;
4843 struct listnode
*node
, *nnode
;
4844 struct ecommunity
*ecom
;
4846 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, ecom
)) {
4847 for (i
= 0; i
< ecom
->size
; i
++) {
4848 struct vrf_irt_node
*irt
;
4849 struct ecommunity_val eval_tmp
;
4851 eval
= (struct ecommunity_val
*)(ecom
->val
4853 * ECOMMUNITY_SIZE
));
4854 /* If using "automatic" RT, we only care about the
4855 * local-admin sub-field.
4856 * This is to facilitate using VNI as the RT for EBGP
4859 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4860 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
,
4861 BGP_VRF_IMPORT_RT_CFGD
))
4862 mask_ecom_global_admin(&eval_tmp
, eval
);
4864 irt
= lookup_vrf_import_rt(&eval_tmp
);
4866 unmap_vrf_from_rt(bgp_vrf
, irt
);
4873 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
4874 * The mapping will be used during route processing.
4876 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4879 struct ecommunity_val
*eval
;
4880 struct listnode
*node
, *nnode
;
4881 struct ecommunity
*ecom
;
4883 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4884 for (i
= 0; i
< ecom
->size
; i
++) {
4885 eval
= (struct ecommunity_val
*)(ecom
->val
4887 * ECOMMUNITY_SIZE
));
4888 map_vni_to_rt(bgp
, vpn
, eval
);
4894 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
4896 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4899 struct ecommunity_val
*eval
;
4900 struct listnode
*node
, *nnode
;
4901 struct ecommunity
*ecom
;
4903 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
4904 for (i
= 0; i
< ecom
->size
; i
++) {
4905 struct irt_node
*irt
;
4906 struct ecommunity_val eval_tmp
;
4908 eval
= (struct ecommunity_val
*)(ecom
->val
4910 * ECOMMUNITY_SIZE
));
4911 /* If using "automatic" RT, we only care about the
4912 * local-admin sub-field.
4913 * This is to facilitate using VNI as the RT for EBGP
4916 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
4917 if (!is_import_rt_configured(vpn
))
4918 mask_ecom_global_admin(&eval_tmp
, eval
);
4920 irt
= lookup_import_rt(bgp
, &eval_tmp
);
4922 unmap_vni_from_rt(bgp
, vpn
, irt
);
4928 * Derive Import RT automatically for VNI and map VNI to RT.
4929 * The mapping will be used during route processing.
4931 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4933 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
);
4934 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
4937 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
4941 * Derive Export RT automatically for VNI.
4943 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4945 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
);
4946 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
4950 * Derive RD automatically for VNI using passed information - it
4951 * is of the form RouterId:unique-id-for-vni.
4953 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
4955 if (is_vrf_rd_configured(bgp
))
4958 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
4962 * Derive RD automatically for VNI using passed information - it
4963 * is of the form RouterId:unique-id-for-vni.
4965 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4967 char buf
[BGP_EVPN_PREFIX_RD_LEN
];
4969 vpn
->prd
.family
= AF_UNSPEC
;
4970 vpn
->prd
.prefixlen
= 64;
4971 snprintfrr(buf
, sizeof(buf
), "%pI4:%hu", &bgp
->router_id
, vpn
->rd_id
);
4972 (void)str2prefix_rd(buf
, &vpn
->prd
);
4973 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
4979 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
4981 struct list
*inst
= bm
->bgp
;
4982 struct listnode
*node
;
4983 struct bgp
*bgp_vrf
;
4985 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
4986 if (bgp_vrf
->l3vni
== vni
)
4996 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
4998 struct bgpevpn
*vpn
;
5001 memset(&tmp
, 0, sizeof(struct bgpevpn
));
5003 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5008 * Create a new vpn - invoked upon configuration or zebra notification.
5010 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5011 struct in_addr originator_ip
,
5012 vrf_id_t tenant_vrf_id
,
5013 struct in_addr mcast_grp
)
5015 struct bgpevpn
*vpn
;
5020 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5022 /* Set values - RD and RT set to defaults. */
5024 vpn
->originator_ip
= originator_ip
;
5025 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5026 vpn
->mcast_grp
= mcast_grp
;
5028 /* Initialize route-target import and export lists */
5029 vpn
->import_rtl
= list_new();
5030 vpn
->import_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5031 vpn
->import_rtl
->del
= evpn_xxport_delete_ecomm
;
5032 vpn
->export_rtl
= list_new();
5033 vpn
->export_rtl
->cmp
= (int (*)(void *, void *))evpn_route_target_cmp
;
5034 vpn
->export_rtl
->del
= evpn_xxport_delete_ecomm
;
5035 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5036 derive_rd_rt_for_vni(bgp
, vpn
);
5038 /* Initialize EVPN route table. */
5039 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5042 if (!hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
)) {
5043 XFREE(MTYPE_BGP_EVPN
, vpn
);
5047 /* add to l2vni list on corresponding vrf */
5048 bgpevpn_link_to_l3vni(vpn
);
5050 bgp_evpn_vni_es_init(vpn
);
5052 QOBJ_REG(vpn
, bgpevpn
);
5057 * Free a given VPN - called in multiple scenarios such as zebra
5058 * notification, configuration being deleted, advertise-all-vni disabled etc.
5059 * This just frees appropriate memory, caller should have taken other
5062 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5064 bgp_evpn_vni_es_cleanup(vpn
);
5065 bgpevpn_unlink_from_l3vni(vpn
);
5066 bgp_table_unlock(vpn
->route_table
);
5067 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5068 list_delete(&vpn
->import_rtl
);
5069 list_delete(&vpn
->export_rtl
);
5070 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5071 hash_release(bgp
->vnihash
, vpn
);
5073 XFREE(MTYPE_BGP_EVPN
, vpn
);
5077 * Import evpn route from global table to VNI/VRF/ESI.
5079 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5080 const struct prefix
*p
, struct bgp_path_info
*pi
)
5082 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5086 * Unimport evpn route from VNI/VRF/ESI.
5088 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5089 const struct prefix
*p
, struct bgp_path_info
*pi
)
5091 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5094 /* filter routes which have martian next hops */
5095 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5099 struct bgp_dest
*rd_dest
, *dest
;
5100 struct bgp_table
*table
;
5101 struct bgp_path_info
*pi
;
5106 /* Walk entire global routing table and evaluate routes which could be
5107 * imported into this VPN. Note that we cannot just look at the routes
5108 * for the VNI's RD -
5109 * remote routes applicable for this VNI could have any RD.
5111 /* EVPN routes are a 2-level table. */
5112 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
5113 rd_dest
= bgp_route_next(rd_dest
)) {
5114 table
= bgp_dest_get_bgp_table_info(rd_dest
);
5118 for (dest
= bgp_table_top(table
); dest
;
5119 dest
= bgp_route_next(dest
)) {
5121 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
5124 /* Consider "valid" remote routes applicable for
5126 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5127 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5129 if (bgp_nexthop_self(bgp
, afi
, pi
->type
,
5130 pi
->sub_type
, pi
->attr
,
5132 const struct prefix
*p
=
5133 bgp_dest_get_prefix(dest
);
5135 if (bgp_debug_update(pi
->peer
, p
, NULL
,
5137 char attr_str
[BUFSIZ
] = {0};
5139 bgp_dump_attr(pi
->attr
,
5144 "%u: prefix %pBD with attr %s - DENIED due to martian or self nexthop",
5148 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5151 bgp_rib_remove(dest
, pi
, pi
->peer
, afi
,
5162 * Handle del of a local MACIP.
5164 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5165 struct ipaddr
*ip
, int state
)
5167 struct bgpevpn
*vpn
;
5168 struct prefix_evpn p
;
5169 struct bgp_dest
*dest
;
5171 /* Lookup VNI hash - should exist. */
5172 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5173 if (!vpn
|| !is_vni_live(vpn
)) {
5174 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5175 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5176 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5180 build_evpn_type2_prefix(&p
, mac
, ip
);
5181 if (state
== ZEBRA_NEIGH_ACTIVE
) {
5182 /* Remove EVPN type-2 route and schedule for processing. */
5183 delete_evpn_route(bgp
, vpn
, &p
);
5185 /* Re-instate the current remote best path if any */
5186 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
5188 evpn_zebra_reinstall_best_route(bgp
, vpn
, dest
);
5195 * Handle add of a local MACIP.
5197 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5198 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
, esi_t
*esi
)
5200 struct bgpevpn
*vpn
;
5201 struct prefix_evpn p
;
5203 /* Lookup VNI hash - should exist. */
5204 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5205 if (!vpn
|| !is_vni_live(vpn
)) {
5206 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5207 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5208 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5212 /* Create EVPN type-2 route and schedule for processing. */
5213 build_evpn_type2_prefix(&p
, mac
, ip
);
5214 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
, esi
)) {
5215 char buf
[ETHER_ADDR_STRLEN
];
5216 char buf2
[INET6_ADDRSTRLEN
];
5219 EC_BGP_EVPN_ROUTE_CREATE
,
5220 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5221 bgp
->vrf_id
, vpn
->vni
,
5222 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5225 prefix_mac2str(mac
, buf
, sizeof(buf
)),
5226 ipaddr2str(ip
, buf2
, sizeof(buf2
)), flags
);
5233 static void link_l2vni_hash_to_l3vni(struct hash_bucket
*bucket
,
5234 struct bgp
*bgp_vrf
)
5236 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
5237 struct bgp
*bgp_evpn
= NULL
;
5239 bgp_evpn
= bgp_get_evpn();
5242 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5243 bgpevpn_link_to_l3vni(vpn
);
5246 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
,
5247 struct ethaddr
*svi_rmac
,
5248 struct ethaddr
*vrr_rmac
,
5249 struct in_addr originator_ip
, int filter
,
5250 ifindex_t svi_ifindex
,
5251 bool is_anycast_mac
)
5253 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5254 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5255 struct listnode
*node
= NULL
;
5256 struct bgpevpn
*vpn
= NULL
;
5259 /* get the EVPN instance - required to get the AS number for VRF
5262 bgp_evpn
= bgp_get_evpn();
5266 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
5272 /* if the BGP vrf instance doesn't exist - create one */
5273 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5278 ret
= bgp_get_vty(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5279 vrf_id
== VRF_DEFAULT
5280 ? BGP_INSTANCE_TYPE_DEFAULT
5281 : BGP_INSTANCE_TYPE_VRF
);
5283 case BGP_ERR_AS_MISMATCH
:
5284 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5285 "BGP is already running; AS is %u\n", as
);
5287 case BGP_ERR_INSTANCE_MISMATCH
:
5288 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5289 "BGP instance name and AS number mismatch\n");
5293 /* mark as auto created */
5294 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5297 /* associate the vrf with l3vni and related parameters */
5298 bgp_vrf
->l3vni
= l3vni
;
5299 bgp_vrf
->originator_ip
= originator_ip
;
5300 bgp_vrf
->l3vni_svi_ifindex
= svi_ifindex
;
5301 bgp_vrf
->evpn_info
->is_anycast_mac
= is_anycast_mac
;
5303 /* copy anycast MAC from VRR MAC */
5304 memcpy(&bgp_vrf
->rmac
, vrr_rmac
, ETH_ALEN
);
5305 /* copy sys RMAC from SVI MAC */
5306 memcpy(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, svi_rmac
, ETH_ALEN
);
5307 /* PIP user configured mac is not present use svi mac as sys mac */
5308 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
))
5309 memcpy(&bgp_vrf
->evpn_info
->pip_rmac
, svi_rmac
, ETH_ALEN
);
5311 if (bgp_debug_zebra(NULL
)) {
5312 char buf
[ETHER_ADDR_STRLEN
];
5313 char buf1
[ETHER_ADDR_STRLEN
];
5314 char buf2
[ETHER_ADDR_STRLEN
];
5316 zlog_debug("VRF %s vni %u pip %s RMAC %s sys RMAC %s static RMAC %s is_anycast_mac %s",
5317 vrf_id_to_name(bgp_vrf
->vrf_id
),
5319 bgp_vrf
->evpn_info
->advertise_pip
? "enable"
5321 prefix_mac2str(&bgp_vrf
->rmac
, buf
, sizeof(buf
)),
5322 prefix_mac2str(&bgp_vrf
->evpn_info
->pip_rmac
,
5323 buf1
, sizeof(buf1
)),
5324 prefix_mac2str(&bgp_vrf
->evpn_info
->pip_rmac_static
,
5325 buf2
, sizeof(buf2
)),
5326 is_anycast_mac
? "Enable" : "Disable");
5328 /* set the right filter - are we using l3vni only for prefix routes? */
5330 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5333 * VNI_FLAG_USE_TWO_LABELS flag for linked L2VNIs should not be
5334 * set before linking vrf to L3VNI. Thus, no need to clear
5338 UNSET_FLAG(bgp_vrf
->vrf_flags
,
5339 BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5341 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5342 if (!CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
5345 * If we are flapping VNI_FLAG_USE_TWO_LABELS
5346 * flag, update all MACIP routes in this VNI
5348 SET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5349 update_all_type2_routes(bgp_evpn
, vpn
);
5354 /* Map auto derive or configured RTs */
5355 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5356 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5358 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
5360 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
5361 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5363 /* auto derive RD */
5364 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5366 /* link all corresponding l2vnis */
5367 hash_iterate(bgp_evpn
->vnihash
,
5368 (void (*)(struct hash_bucket
*,
5369 void *))link_l2vni_hash_to_l3vni
,
5372 /* Only update all corresponding type-2 routes if we are advertising two
5373 * labels along with type-2 routes
5376 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5377 update_routes_for_vni(bgp_evpn
, vpn
);
5379 /* advertise type-5 routes if needed */
5380 update_advertise_vrf_routes(bgp_vrf
);
5382 /* install all remote routes belonging to this l3vni into correspondng
5384 install_routes_for_vrf(bgp_vrf
);
5389 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5391 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5392 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5393 struct listnode
*node
= NULL
;
5394 struct listnode
*next
= NULL
;
5395 struct bgpevpn
*vpn
= NULL
;
5397 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5401 "Cannot process L3VNI %u Del - Could not find BGP instance",
5406 bgp_evpn
= bgp_get_evpn();
5410 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
5415 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5416 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5417 * routes. This will uninstalling the routes from zebra and decremnt the
5420 uninstall_routes_for_vrf(bgp_vrf
);
5422 /* delete/withdraw all type-5 routes */
5423 delete_withdraw_vrf_routes(bgp_vrf
);
5425 /* remove the l3vni from vrf instance */
5428 /* remove the Rmac from the BGP vrf */
5429 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5430 memset(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, 0, ETH_ALEN
);
5431 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
) &&
5432 !is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac
))
5433 memset(&bgp_vrf
->evpn_info
->pip_rmac
, 0, ETH_ALEN
);
5435 /* remove default import RT or Unmap non-default import RT */
5436 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5437 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5438 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5439 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5442 /* remove default export RT */
5443 if (!list_isempty(bgp_vrf
->vrf_export_rtl
) &&
5444 !CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
5445 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5448 /* update all corresponding local mac-ip routes */
5449 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5450 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5451 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5452 update_routes_for_vni(bgp_evpn
, vpn
);
5456 /* If any L2VNIs point to this instance, unlink them. */
5457 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5458 bgpevpn_unlink_from_l3vni(vpn
);
5460 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5462 /* Delete the instance if it was autocreated */
5463 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5464 bgp_delete(bgp_vrf
);
5470 * Handle del of a local VNI.
5472 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5474 struct bgpevpn
*vpn
;
5476 /* Locate VNI hash */
5477 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5479 if (bgp_debug_zebra(NULL
))
5481 EC_BGP_EVPN_VPN_VNI
,
5482 "%u: VNI hash entry for VNI %u not found at DEL",
5487 /* Remove all local EVPN routes and schedule for processing (to
5488 * withdraw from peers).
5490 delete_routes_for_vni(bgp
, vpn
);
5493 * tunnel is no longer active, del tunnel ip address from tip_hash
5495 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5497 /* Clear "live" flag and see if hash needs to be freed. */
5498 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5499 if (!is_vni_configured(vpn
))
5500 bgp_evpn_free(bgp
, vpn
);
5506 * Handle add (or update) of a local VNI. The VNI changes we care
5507 * about are for the local-tunnel-ip and the (tenant) VRF.
5509 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5510 struct in_addr originator_ip
,
5511 vrf_id_t tenant_vrf_id
,
5512 struct in_addr mcast_grp
)
5515 struct bgpevpn
*vpn
;
5516 struct prefix_evpn p
;
5518 /* Lookup VNI. If present and no change, exit. */
5519 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5522 if (is_vni_live(vpn
)
5523 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5524 && IPV4_ADDR_SAME(&vpn
->mcast_grp
, &mcast_grp
)
5525 && vpn
->tenant_vrf_id
== tenant_vrf_id
)
5526 /* Probably some other param has changed that we don't
5530 bgp_evpn_mcast_grp_change(bgp
, vpn
, mcast_grp
);
5532 /* Update tenant_vrf_id if it has changed. */
5533 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
5534 bgpevpn_unlink_from_l3vni(vpn
);
5535 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5536 bgpevpn_link_to_l3vni(vpn
);
5539 /* If tunnel endpoint IP has changed, update (and delete prior
5540 * type-3 route, if needed.)
5542 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
5543 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
5545 /* Update all routes with new endpoint IP and/or export RT
5548 if (is_vni_live(vpn
))
5549 update_routes_for_vni(bgp
, vpn
);
5552 /* Create or update as appropriate. */
5554 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
,
5559 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5565 /* if the VNI is live already, there is nothing more to do */
5566 if (is_vni_live(vpn
))
5569 /* Mark as "live" */
5570 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5572 /* tunnel is now active, add tunnel-ip to db */
5573 bgp_tip_add(bgp
, &originator_ip
);
5575 /* filter routes as nexthop database has changed */
5576 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
5579 * Create EVPN type-3 route and schedule for processing.
5581 * RT-3 only if doing head-end replication
5583 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
5584 == VXLAN_FLOOD_HEAD_END_REPL
) {
5585 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
5586 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
)) {
5587 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
5588 "%u: Type3 route creation failure for VNI %u",
5594 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5598 install_routes_for_vni(bgp
, vpn
);
5600 /* If we are advertising gateway mac-ip
5601 It needs to be conveyed again to zebra */
5602 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
5604 /* advertise svi mac-ip knob to zebra */
5605 bgp_zebra_advertise_svi_macip(bgp
, vpn
->advertise_svi_macip
, vpn
->vni
);
5611 * Handle change in setting for BUM handling. The supported values
5612 * are head-end replication and dropping all BUM packets. Any change
5613 * should be registered with zebra. Also, if doing head-end replication,
5614 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5615 * to be dropped, the RT-3s must be withdrawn.
5617 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
5619 zlog_info("L2VPN EVPN BUM handling is %s",
5620 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
5621 "Flooding" : "Flooding Disabled");
5623 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
5624 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
5625 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
5626 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
5627 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
5631 * Cleanup EVPN information on disable - Need to delete and withdraw
5632 * EVPN routes from peers.
5634 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
5636 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_bucket
*,
5637 void *))cleanup_vni_on_disable
,
5642 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5643 * BGP instance (default) is being freed.
5645 void bgp_evpn_cleanup(struct bgp
*bgp
)
5647 hash_iterate(bgp
->vnihash
,
5648 (void (*)(struct hash_bucket
*, void *))free_vni_entry
,
5651 hash_free(bgp
->import_rt_hash
);
5652 bgp
->import_rt_hash
= NULL
;
5654 hash_free(bgp
->vrf_import_rt_hash
);
5655 bgp
->vrf_import_rt_hash
= NULL
;
5657 hash_free(bgp
->vnihash
);
5658 bgp
->vnihash
= NULL
;
5660 list_delete(&bgp
->vrf_import_rtl
);
5661 list_delete(&bgp
->vrf_export_rtl
);
5662 list_delete(&bgp
->l2vnis
);
5666 * Initialization for EVPN
5669 * hash for RT to VNI
5671 void bgp_evpn_init(struct bgp
*bgp
)
5674 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
5675 bgp
->import_rt_hash
=
5676 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
5677 "BGP Import RT Hash");
5678 bgp
->vrf_import_rt_hash
=
5679 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
5680 "BGP VRF Import RT Hash");
5681 bgp
->vrf_import_rtl
= list_new();
5682 bgp
->vrf_import_rtl
->cmp
=
5683 (int (*)(void *, void *))evpn_route_target_cmp
;
5684 bgp
->vrf_import_rtl
->del
= evpn_xxport_delete_ecomm
;
5685 bgp
->vrf_export_rtl
= list_new();
5686 bgp
->vrf_export_rtl
->cmp
=
5687 (int (*)(void *, void *))evpn_route_target_cmp
;
5688 bgp
->vrf_export_rtl
->del
= evpn_xxport_delete_ecomm
;
5689 bgp
->l2vnis
= list_new();
5690 bgp
->l2vnis
->cmp
= vni_list_cmp
;
5691 /* By default Duplicate Address Dection is enabled.
5692 * Max-moves (N) 5, detection time (M) 180
5693 * default action is warning-only
5694 * freeze action permanently freezes address,
5695 * and freeze time (auto-recovery) is disabled.
5697 if (bgp
->evpn_info
) {
5698 bgp
->evpn_info
->dup_addr_detect
= true;
5699 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
5700 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
5701 bgp
->evpn_info
->dad_freeze
= false;
5702 bgp
->evpn_info
->dad_freeze_time
= 0;
5703 /* Initialize zebra vxlan */
5704 bgp_zebra_dup_addr_detection(bgp
);
5705 /* Enable PIP feature by default for bgp vrf instance */
5706 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
5707 struct bgp
*bgp_default
;
5709 bgp
->evpn_info
->advertise_pip
= true;
5710 bgp_default
= bgp_get_default();
5712 bgp
->evpn_info
->pip_ip
= bgp_default
->router_id
;
5716 /* Default BUM handling is to do head-end replication. */
5717 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
5720 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
5722 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5726 * Get the prefixlen of the ip prefix carried within the type5 evpn route.
5728 int bgp_evpn_get_type5_prefixlen(const struct prefix
*pfx
)
5730 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
5732 if (!pfx
|| pfx
->family
!= AF_EVPN
)
5735 if (evp
->prefix
.route_type
!= BGP_EVPN_IP_PREFIX_ROUTE
)
5738 return evp
->prefix
.prefix_addr
.ip_prefix_length
;
5742 * Should we register nexthop for this EVPN prefix for nexthop tracking?
5744 bool bgp_evpn_is_prefix_nht_supported(const struct prefix
*pfx
)
5746 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
5749 * EVPN routes should be marked as valid only if the nexthop is
5750 * reachable. Only if this happens, the route should be imported
5751 * (into VNI or VRF routing tables) and/or advertised.
5752 * Note: This is currently applied for EVPN type-2, type-3 and
5753 * type-5 routes. It may be tweaked later on for other routes, or
5754 * even removed completely when all routes are handled.
5756 if (pfx
&& pfx
->family
== AF_EVPN
&&
5757 (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
5758 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
5759 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))