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"
56 #include "bgpd/bgp_nht.h"
57 #include "bgpd/bgp_trace.h"
60 * Definitions and external declarations.
62 DEFINE_QOBJ_TYPE(bgpevpn
);
63 DEFINE_QOBJ_TYPE(bgp_evpn_es
);
65 DEFINE_MTYPE_STATIC(BGPD
, VRF_ROUTE_TARGET
, "L3 Route Target");
68 * Static function declarations
70 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
71 static void bgp_evpn_remote_ip_hash_init(struct bgpevpn
*evpn
);
72 static void bgp_evpn_remote_ip_hash_destroy(struct bgpevpn
*evpn
);
73 static void bgp_evpn_remote_ip_hash_add(struct bgpevpn
*vpn
,
74 struct bgp_path_info
*pi
);
75 static void bgp_evpn_remote_ip_hash_del(struct bgpevpn
*vpn
,
76 struct bgp_path_info
*pi
);
77 static void bgp_evpn_remote_ip_hash_iterate(struct bgpevpn
*vpn
,
78 void (*func
)(struct hash_bucket
*,
81 static void bgp_evpn_link_to_vni_svi_hash(struct bgp
*bgp
, struct bgpevpn
*vpn
);
82 static void bgp_evpn_unlink_from_vni_svi_hash(struct bgp
*bgp
,
84 static unsigned int vni_svi_hash_key_make(const void *p
);
85 static bool vni_svi_hash_cmp(const void *p1
, const void *p2
);
86 static void bgp_evpn_remote_ip_process_nexthops(struct bgpevpn
*vpn
,
89 static void bgp_evpn_remote_ip_hash_link_nexthop(struct hash_bucket
*bucket
,
91 static void bgp_evpn_remote_ip_hash_unlink_nexthop(struct hash_bucket
*bucket
,
93 static struct in_addr zero_vtep_ip
;
102 static unsigned int vni_hash_key_make(const void *p
)
104 const struct bgpevpn
*vpn
= p
;
105 return (jhash_1word(vpn
->vni
, 0));
109 * Comparison function for vni hash
111 static bool vni_hash_cmp(const void *p1
, const void *p2
)
113 const struct bgpevpn
*vpn1
= p1
;
114 const struct bgpevpn
*vpn2
= p2
;
116 return vpn1
->vni
== vpn2
->vni
;
119 int vni_list_cmp(void *p1
, void *p2
)
121 const struct bgpevpn
*vpn1
= p1
;
122 const struct bgpevpn
*vpn2
= p2
;
124 return vpn1
->vni
- vpn2
->vni
;
128 * Make vrf import route target hash key.
130 static unsigned int vrf_import_rt_hash_key_make(const void *p
)
132 const struct vrf_irt_node
*irt
= p
;
133 const char *pnt
= irt
->rt
.val
;
135 return jhash(pnt
, 8, 0x5abc1234);
139 * Comparison function for vrf import rt hash
141 static bool vrf_import_rt_hash_cmp(const void *p1
, const void *p2
)
143 const struct vrf_irt_node
*irt1
= p1
;
144 const struct vrf_irt_node
*irt2
= p2
;
146 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
150 * Create a new vrf import_rt in evpn instance
152 static struct vrf_irt_node
*vrf_import_rt_new(struct ecommunity_val
*rt
)
154 struct bgp
*bgp_evpn
= NULL
;
155 struct vrf_irt_node
*irt
;
157 bgp_evpn
= bgp_get_evpn();
159 flog_err(EC_BGP_NO_DFLT
,
160 "vrf import rt new - evpn instance not created yet");
164 irt
= XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT
,
165 sizeof(struct vrf_irt_node
));
168 irt
->vrfs
= list_new();
171 (void)hash_get(bgp_evpn
->vrf_import_rt_hash
, irt
, hash_alloc_intern
);
177 * Free the vrf import rt node
179 static void vrf_import_rt_free(struct vrf_irt_node
*irt
)
181 struct bgp
*bgp_evpn
= NULL
;
183 bgp_evpn
= bgp_get_evpn();
185 flog_err(EC_BGP_NO_DFLT
,
186 "vrf import rt free - evpn instance not created yet");
190 hash_release(bgp_evpn
->vrf_import_rt_hash
, irt
);
191 list_delete(&irt
->vrfs
);
192 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
195 static void hash_vrf_import_rt_free(struct vrf_irt_node
*irt
)
197 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT
, irt
);
201 * Function to lookup Import RT node - used to map a RT to set of
202 * VNIs importing routes with that RT.
204 static struct vrf_irt_node
*lookup_vrf_import_rt(struct ecommunity_val
*rt
)
206 struct bgp
*bgp_evpn
= NULL
;
207 struct vrf_irt_node
*irt
;
208 struct vrf_irt_node tmp
;
210 bgp_evpn
= bgp_get_evpn();
214 "vrf import rt lookup - evpn instance not created yet");
218 memset(&tmp
, 0, sizeof(tmp
));
219 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
220 irt
= hash_lookup(bgp_evpn
->vrf_import_rt_hash
, &tmp
);
225 * Is specified VRF present on the RT's list of "importing" VRFs?
227 static int is_vrf_present_in_irt_vrfs(struct list
*vrfs
, struct bgp
*bgp_vrf
)
229 struct listnode
*node
= NULL
, *nnode
= NULL
;
230 struct bgp
*tmp_bgp_vrf
= NULL
;
232 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, tmp_bgp_vrf
)) {
233 if (tmp_bgp_vrf
== bgp_vrf
)
240 * Make import route target hash key.
242 static unsigned int import_rt_hash_key_make(const void *p
)
244 const struct irt_node
*irt
= p
;
245 const char *pnt
= irt
->rt
.val
;
247 return jhash(pnt
, 8, 0xdeadbeef);
251 * Comparison function for import rt hash
253 static bool import_rt_hash_cmp(const void *p1
, const void *p2
)
255 const struct irt_node
*irt1
= p1
;
256 const struct irt_node
*irt2
= p2
;
258 return (memcmp(irt1
->rt
.val
, irt2
->rt
.val
, ECOMMUNITY_SIZE
) == 0);
262 * Create a new import_rt
264 static struct irt_node
*import_rt_new(struct bgp
*bgp
,
265 struct ecommunity_val
*rt
)
267 struct irt_node
*irt
;
269 irt
= XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT
, sizeof(struct irt_node
));
272 irt
->vnis
= list_new();
275 (void)hash_get(bgp
->import_rt_hash
, irt
, hash_alloc_intern
);
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
);
290 static void hash_import_rt_free(struct irt_node
*irt
)
292 XFREE(MTYPE_BGP_EVPN_IMPORT_RT
, irt
);
296 * Function to lookup Import RT node - used to map a RT to set of
297 * VNIs importing routes with that RT.
299 static struct irt_node
*lookup_import_rt(struct bgp
*bgp
,
300 struct ecommunity_val
*rt
)
302 struct irt_node
*irt
;
305 memset(&tmp
, 0, sizeof(tmp
));
306 memcpy(&tmp
.rt
, rt
, ECOMMUNITY_SIZE
);
307 irt
= hash_lookup(bgp
->import_rt_hash
, &tmp
);
312 * Is specified VNI present on the RT's list of "importing" VNIs?
314 static int is_vni_present_in_irt_vnis(struct list
*vnis
, struct bgpevpn
*vpn
)
316 struct listnode
*node
, *nnode
;
317 struct bgpevpn
*tmp_vpn
;
319 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, tmp_vpn
)) {
328 * Compare Route Targets.
330 int bgp_evpn_route_target_cmp(struct ecommunity
*ecom1
,
331 struct ecommunity
*ecom2
)
339 if (!ecom1
&& !ecom2
)
342 if (ecom1
->str
&& !ecom2
->str
)
345 if (!ecom1
->str
&& ecom2
->str
)
348 if (!ecom1
->str
&& !ecom2
->str
)
351 return strcmp(ecom1
->str
, ecom2
->str
);
355 * Compare L3 Route Targets.
357 static int evpn_vrf_route_target_cmp(struct vrf_route_target
*rt1
,
358 struct vrf_route_target
*rt2
)
360 return bgp_evpn_route_target_cmp(rt1
->ecom
, rt2
->ecom
);
363 void bgp_evpn_xxport_delete_ecomm(void *val
)
365 struct ecommunity
*ecomm
= val
;
366 ecommunity_free(&ecomm
);
370 * Delete l3 Route Target.
372 static void evpn_vrf_rt_del(void *val
)
374 struct vrf_route_target
*l3rt
= val
;
376 ecommunity_free(&l3rt
->ecom
);
378 XFREE(MTYPE_VRF_ROUTE_TARGET
, l3rt
);
382 * Allocate a new l3 Route Target.
384 static struct vrf_route_target
*evpn_vrf_rt_new(struct ecommunity
*ecom
)
386 struct vrf_route_target
*l3rt
;
388 l3rt
= XCALLOC(MTYPE_VRF_ROUTE_TARGET
, sizeof(struct vrf_route_target
));
396 * Mask off global-admin field of specified extended community (RT),
397 * just retain the local-admin field.
399 static inline void mask_ecom_global_admin(struct ecommunity_val
*dst
,
400 const struct ecommunity_val
*src
)
406 if (type
== ECOMMUNITY_ENCODE_AS
) {
407 dst
->val
[2] = dst
->val
[3] = 0;
408 } else if (type
== ECOMMUNITY_ENCODE_AS4
409 || type
== ECOMMUNITY_ENCODE_IP
) {
410 dst
->val
[2] = dst
->val
[3] = 0;
411 dst
->val
[4] = dst
->val
[5] = 0;
416 * Converts the RT to Ecommunity Value and adjusts masking based
417 * on flags set for RT.
419 static void vrf_rt2ecom_val(struct ecommunity_val
*to_eval
,
420 const struct vrf_route_target
*l3rt
, int iter
)
422 const struct ecommunity_val
*eval
;
424 eval
= (const struct ecommunity_val
*)(l3rt
->ecom
->val
+
425 (iter
* ECOMMUNITY_SIZE
));
426 /* If using "automatic" or "wildcard *" RT,
427 * we only care about the local-admin sub-field.
428 * This is to facilitate using L3VNI(VRF-VNI)
429 * as the RT for EBGP peering too and simplify
430 * configurations by allowing any ASN via '*'.
432 memcpy(to_eval
, eval
, ECOMMUNITY_SIZE
);
434 if (CHECK_FLAG(l3rt
->flags
, BGP_VRF_RT_AUTO
) ||
435 CHECK_FLAG(l3rt
->flags
, BGP_VRF_RT_WILD
))
436 mask_ecom_global_admin(to_eval
, eval
);
440 * Map one RT to specified VRF.
441 * bgp_vrf = BGP vrf instance
443 static void map_vrf_to_rt(struct bgp
*bgp_vrf
, struct vrf_route_target
*l3rt
)
447 for (i
= 0; i
< l3rt
->ecom
->size
; i
++) {
448 struct vrf_irt_node
*irt
= NULL
;
449 struct ecommunity_val eval_tmp
;
451 /* Adjust masking for value */
452 vrf_rt2ecom_val(&eval_tmp
, l3rt
, i
);
454 irt
= lookup_vrf_import_rt(&eval_tmp
);
456 if (irt
&& is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
457 return; /* Already mapped. */
460 irt
= vrf_import_rt_new(&eval_tmp
);
462 /* Add VRF to the list for this RT. */
463 listnode_add(irt
->vrfs
, bgp_vrf
);
468 * Unmap specified VRF from specified RT. If there are no other
469 * VRFs for this RT, then the RT hash is deleted.
470 * bgp_vrf: BGP VRF specific instance
472 static void unmap_vrf_from_rt(struct bgp
*bgp_vrf
,
473 struct vrf_route_target
*l3rt
)
477 for (i
= 0; i
< l3rt
->ecom
->size
; i
++) {
478 struct vrf_irt_node
*irt
;
479 struct ecommunity_val eval_tmp
;
481 /* Adjust masking for value */
482 vrf_rt2ecom_val(&eval_tmp
, l3rt
, i
);
484 irt
= lookup_vrf_import_rt(&eval_tmp
);
487 return; /* Not mapped */
489 /* Delete VRF from list for this RT. */
490 listnode_delete(irt
->vrfs
, bgp_vrf
);
492 if (!listnode_head(irt
->vrfs
))
493 vrf_import_rt_free(irt
);
498 * Map one RT to specified VNI.
500 static void map_vni_to_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
501 struct ecommunity_val
*eval
)
503 struct irt_node
*irt
;
504 struct ecommunity_val eval_tmp
;
506 /* If using "automatic" RT, we only care about the local-admin
508 * This is to facilitate using VNI as the RT for EBGP peering too.
510 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
511 if (!is_import_rt_configured(vpn
))
512 mask_ecom_global_admin(&eval_tmp
, eval
);
514 irt
= lookup_import_rt(bgp
, &eval_tmp
);
516 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
517 /* Already mapped. */
521 irt
= import_rt_new(bgp
, &eval_tmp
);
523 /* Add VNI to the hash list for this RT. */
524 listnode_add(irt
->vnis
, vpn
);
528 * Unmap specified VNI from specified RT. If there are no other
529 * VNIs for this RT, then the RT hash is deleted.
531 static void unmap_vni_from_rt(struct bgp
*bgp
, struct bgpevpn
*vpn
,
532 struct irt_node
*irt
)
534 /* Delete VNI from hash list for this RT. */
535 listnode_delete(irt
->vnis
, vpn
);
536 if (!listnode_head(irt
->vnis
)) {
537 import_rt_free(bgp
, irt
);
541 static void bgp_evpn_get_rmac_nexthop(struct bgpevpn
*vpn
,
542 const struct prefix_evpn
*p
,
543 struct attr
*attr
, uint8_t flags
)
545 struct bgp
*bgp_vrf
= vpn
->bgp_vrf
;
547 memset(&attr
->rmac
, 0, sizeof(struct ethaddr
));
551 if (p
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
554 /* Copy sys (pip) RMAC and PIP IP as nexthop
555 * in case of route is self MAC-IP,
556 * advertise-pip and advertise-svi-ip features
558 * Otherwise, for all host MAC-IP route's
561 if (CHECK_FLAG(flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
)
562 && bgp_vrf
->evpn_info
->advertise_pip
&&
563 bgp_vrf
->evpn_info
->is_anycast_mac
) {
565 memcpy(&attr
->rmac
, &bgp_vrf
->evpn_info
->pip_rmac
,
567 attr
->nexthop
= bgp_vrf
->evpn_info
->pip_ip
;
568 attr
->mp_nexthop_global_in
=
569 bgp_vrf
->evpn_info
->pip_ip
;
571 memcpy(&attr
->rmac
, &bgp_vrf
->rmac
, ETH_ALEN
);
575 * Create RT extended community automatically from passed information:
576 * of the form AS:VNI.
577 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
578 * the need is to get a RT value that will be unique across different
579 * VNIs but the same across routers (in the same AS) for a particular
582 static void form_auto_rt(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
,
585 struct ecommunity_val eval
;
586 struct ecommunity
*ecomadd
;
587 struct ecommunity
*ecom
;
588 struct vrf_route_target
*l3rt
;
589 struct vrf_route_target
*newrt
;
590 bool ecom_found
= false;
591 struct listnode
*node
;
593 if (bgp
->advertise_autort_rfc8365
)
594 vni
|= EVPN_AUTORT_VXLAN
;
595 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
597 ecomadd
= ecommunity_new();
598 ecommunity_add_val(ecomadd
, &eval
, false, false);
601 for (ALL_LIST_ELEMENTS_RO(rtl
, node
, l3rt
))
602 if (ecommunity_cmp(ecomadd
, l3rt
->ecom
)) {
607 for (ALL_LIST_ELEMENTS_RO(rtl
, node
, ecom
))
608 if (ecommunity_cmp(ecomadd
, ecom
)) {
616 newrt
= evpn_vrf_rt_new(ecomadd
);
617 /* Label it as autoderived */
618 SET_FLAG(newrt
->flags
, BGP_VRF_RT_AUTO
);
619 listnode_add_sort(rtl
, newrt
);
621 listnode_add_sort(rtl
, ecomadd
);
623 ecommunity_free(&ecomadd
);
627 * Derive RD and RT for a VNI automatically. Invoked at the time of
630 static void derive_rd_rt_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
632 bgp_evpn_derive_auto_rd(bgp
, vpn
);
633 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
634 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
638 * Convert nexthop (remote VTEP IP) into an IPv6 address.
640 static void evpn_convert_nexthop_to_ipv6(struct attr
*attr
)
642 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
644 ipv4_to_ipv4_mapped_ipv6(&attr
->mp_nexthop_global
, attr
->nexthop
);
645 attr
->mp_nexthop_len
= IPV6_MAX_BYTELEN
;
648 struct bgp_dest
*bgp_global_evpn_node_get(struct bgp_table
*table
, afi_t afi
,
650 const struct prefix_evpn
*evp
,
651 struct prefix_rd
*prd
)
653 struct prefix_evpn global_p
;
655 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
) {
656 /* prefix in the global table doesn't include the VTEP-IP so
657 * we need to create a different copy of the prefix
659 evpn_type1_prefix_global_copy(&global_p
, evp
);
662 return bgp_afi_node_get(table
, afi
, safi
, (struct prefix
*)evp
, prd
);
665 struct bgp_dest
*bgp_global_evpn_node_lookup(struct bgp_table
*table
, afi_t afi
,
667 const struct prefix_evpn
*evp
,
668 struct prefix_rd
*prd
)
670 struct prefix_evpn global_p
;
672 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
) {
673 /* prefix in the global table doesn't include the VTEP-IP so
674 * we need to create a different copy of the prefix
676 evpn_type1_prefix_global_copy(&global_p
, evp
);
679 return bgp_afi_node_lookup(table
, afi
, safi
, (struct prefix
*)evp
, prd
);
683 * Add (update) or delete MACIP from zebra.
685 static int bgp_zebra_send_remote_macip(struct bgp
*bgp
, struct bgpevpn
*vpn
,
686 const struct prefix_evpn
*p
,
687 struct in_addr remote_vtep_ip
, int add
,
688 uint8_t flags
, uint32_t seq
, esi_t
*esi
)
692 static struct in_addr zero_remote_vtep_ip
;
695 if (!zclient
|| zclient
->sock
< 0)
698 /* Don't try to register if Zebra doesn't know of this instance. */
699 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
700 if (BGP_DEBUG(zebra
, ZEBRA
))
702 "%s: No zebra instance to talk to, not installing remote macip",
712 zclient_create_header(
713 s
, add
? ZEBRA_REMOTE_MACIP_ADD
: ZEBRA_REMOTE_MACIP_DEL
,
715 stream_putl(s
, vpn
->vni
);
716 stream_put(s
, &p
->prefix
.macip_addr
.mac
.octet
, ETH_ALEN
); /* Mac Addr */
717 /* IP address length and IP address, if any. */
718 if (is_evpn_prefix_ipaddr_none(p
))
721 ipa_len
= is_evpn_prefix_ipaddr_v4(p
) ? IPV4_MAX_BYTELEN
723 stream_putw(s
, ipa_len
);
724 stream_put(s
, &p
->prefix
.macip_addr
.ip
.ip
.addr
, ipa_len
);
726 /* If the ESI is valid that becomes the nexthop; tape out the
727 * VTEP-IP for that case
729 if (bgp_evpn_is_esi_valid(esi
))
730 stream_put_in_addr(s
, &zero_remote_vtep_ip
);
732 stream_put_in_addr(s
, &remote_vtep_ip
);
734 /* TX flags - MAC sticky status and/or gateway mac */
735 /* Also TX the sequence number of the best route. */
737 stream_putc(s
, flags
);
739 stream_put(s
, esi
, sizeof(esi_t
));
742 stream_putw_at(s
, 0, stream_get_endp(s
));
744 if (bgp_debug_zebra(NULL
))
746 "Tx %s MACIP, VNI %u MAC %pEA IP %pIA flags 0x%x seq %u remote VTEP %pI4",
747 add
? "ADD" : "DEL", vpn
->vni
,
748 &p
->prefix
.macip_addr
.mac
, &p
->prefix
.macip_addr
.ip
,
749 flags
, seq
, &remote_vtep_ip
);
751 frrtrace(5, frr_bgp
, evpn_mac_ip_zsend
, add
, vpn
, p
, remote_vtep_ip
,
754 return zclient_send_message(zclient
);
758 * Add (update) or delete remote VTEP from zebra.
760 static int bgp_zebra_send_remote_vtep(struct bgp
*bgp
, struct bgpevpn
*vpn
,
761 const struct prefix_evpn
*p
,
762 int flood_control
, int add
)
767 if (!zclient
|| zclient
->sock
< 0)
770 /* Don't try to register if Zebra doesn't know of this instance. */
771 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
772 if (BGP_DEBUG(zebra
, ZEBRA
))
774 "%s: No zebra instance to talk to, not installing remote vtep",
782 zclient_create_header(
783 s
, add
? ZEBRA_REMOTE_VTEP_ADD
: ZEBRA_REMOTE_VTEP_DEL
,
785 stream_putl(s
, vpn
->vni
);
786 if (is_evpn_prefix_ipaddr_v4(p
))
787 stream_put_in_addr(s
, &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
788 else if (is_evpn_prefix_ipaddr_v6(p
)) {
791 "Bad remote IP when trying to %s remote VTEP for VNI %u",
792 add
? "ADD" : "DEL", vpn
->vni
);
795 stream_putl(s
, flood_control
);
797 stream_putw_at(s
, 0, stream_get_endp(s
));
799 if (bgp_debug_zebra(NULL
))
800 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %pI4",
801 add
? "ADD" : "DEL", vpn
->vni
,
802 &p
->prefix
.imet_addr
.ip
.ipaddr_v4
);
804 frrtrace(3, frr_bgp
, evpn_bum_vtep_zsend
, add
, vpn
, p
);
806 return zclient_send_message(zclient
);
810 * Build extended communities for EVPN prefix route.
812 static void build_evpn_type5_route_extcomm(struct bgp
*bgp_vrf
,
815 struct ecommunity ecom_encap
;
816 struct ecommunity_val eval
;
817 struct ecommunity_val eval_rmac
;
818 bgp_encap_types tnl_type
;
819 struct listnode
*node
, *nnode
;
820 struct vrf_route_target
*l3rt
;
821 struct ecommunity
*old_ecom
;
822 struct ecommunity
*ecom
;
823 struct list
*vrf_export_rtl
= NULL
;
826 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
827 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
828 encode_encap_extcomm(tnl_type
, &eval
);
830 ecom_encap
.unit_size
= ECOMMUNITY_SIZE
;
831 ecom_encap
.val
= (uint8_t *)eval
.val
;
834 if (bgp_attr_get_ecommunity(attr
)) {
835 old_ecom
= bgp_attr_get_ecommunity(attr
);
836 ecom
= ecommunity_merge(ecommunity_dup(old_ecom
), &ecom_encap
);
837 if (!old_ecom
->refcnt
)
838 ecommunity_free(&old_ecom
);
840 ecom
= ecommunity_dup(&ecom_encap
);
841 bgp_attr_set_ecommunity(attr
, ecom
);
842 attr
->encap_tunneltype
= tnl_type
;
844 /* Add the export RTs for L3VNI/VRF */
845 vrf_export_rtl
= bgp_vrf
->vrf_export_rtl
;
846 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
, l3rt
))
847 bgp_attr_set_ecommunity(
848 attr
, ecommunity_merge(bgp_attr_get_ecommunity(attr
),
851 /* add the router mac extended community */
852 if (!is_zero_mac(&attr
->rmac
)) {
853 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
854 ecommunity_add_val(bgp_attr_get_ecommunity(attr
), &eval_rmac
,
860 * Build extended communities for EVPN route.
861 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
862 * and ENCAP extended communities are applicable for all routes.
863 * The default gateway extended community and MAC mobility (sticky) extended
864 * community are added as needed based on passed settings - only for type-2
865 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
866 * added, if present, based on passed settings - only for non-link-local
869 static void build_evpn_route_extcomm(struct bgpevpn
*vpn
, struct attr
*attr
,
872 struct ecommunity ecom_encap
;
873 struct ecommunity ecom_sticky
;
874 struct ecommunity ecom_default_gw
;
875 struct ecommunity ecom_na
;
876 struct ecommunity_val eval
;
877 struct ecommunity_val eval_sticky
;
878 struct ecommunity_val eval_default_gw
;
879 struct ecommunity_val eval_rmac
;
880 struct ecommunity_val eval_na
;
883 bgp_encap_types tnl_type
;
884 struct listnode
*node
, *nnode
;
885 struct ecommunity
*ecom
;
886 struct vrf_route_target
*l3rt
;
888 struct list
*vrf_export_rtl
= NULL
;
891 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
892 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
893 encode_encap_extcomm(tnl_type
, &eval
);
895 ecom_encap
.unit_size
= ECOMMUNITY_SIZE
;
896 ecom_encap
.val
= (uint8_t *)eval
.val
;
899 bgp_attr_set_ecommunity(attr
, ecommunity_dup(&ecom_encap
));
900 attr
->encap_tunneltype
= tnl_type
;
902 /* Add the export RTs for L2VNI */
903 for (ALL_LIST_ELEMENTS(vpn
->export_rtl
, node
, nnode
, ecom
))
904 bgp_attr_set_ecommunity(
906 ecommunity_merge(bgp_attr_get_ecommunity(attr
), ecom
));
908 /* Add the export RTs for L3VNI if told to - caller determines
909 * when this should be done.
912 vrf_export_rtl
= bgpevpn_get_vrf_export_rtl(vpn
);
913 if (vrf_export_rtl
&& !list_isempty(vrf_export_rtl
)) {
914 for (ALL_LIST_ELEMENTS(vrf_export_rtl
, node
, nnode
,
916 bgp_attr_set_ecommunity(
919 bgp_attr_get_ecommunity(attr
),
924 /* Add MAC mobility (sticky) if needed. */
927 memset(&ecom_sticky
, 0, sizeof(ecom_sticky
));
928 encode_mac_mobility_extcomm(1, seqnum
, &eval_sticky
);
929 ecom_sticky
.size
= 1;
930 ecom_sticky
.unit_size
= ECOMMUNITY_SIZE
;
931 ecom_sticky
.val
= (uint8_t *)eval_sticky
.val
;
932 bgp_attr_set_ecommunity(
933 attr
, ecommunity_merge(bgp_attr_get_ecommunity(attr
),
937 /* Add RMAC, if told to. */
939 encode_rmac_extcomm(&eval_rmac
, &attr
->rmac
);
940 ecommunity_add_val(bgp_attr_get_ecommunity(attr
), &eval_rmac
,
944 /* Add default gateway, if needed. */
945 if (attr
->default_gw
) {
946 memset(&ecom_default_gw
, 0, sizeof(ecom_default_gw
));
947 encode_default_gw_extcomm(&eval_default_gw
);
948 ecom_default_gw
.size
= 1;
949 ecom_default_gw
.unit_size
= ECOMMUNITY_SIZE
;
950 ecom_default_gw
.val
= (uint8_t *)eval_default_gw
.val
;
951 bgp_attr_set_ecommunity(
952 attr
, ecommunity_merge(bgp_attr_get_ecommunity(attr
),
956 proxy
= !!(attr
->es_flags
& ATTR_ES_PROXY_ADVERT
);
957 if (attr
->router_flag
|| proxy
) {
958 memset(&ecom_na
, 0, sizeof(ecom_na
));
959 encode_na_flag_extcomm(&eval_na
, attr
->router_flag
, proxy
);
961 ecom_na
.unit_size
= ECOMMUNITY_SIZE
;
962 ecom_na
.val
= (uint8_t *)eval_na
.val
;
963 bgp_attr_set_ecommunity(
964 attr
, ecommunity_merge(bgp_attr_get_ecommunity(attr
),
970 * Add MAC mobility extended community to attribute.
972 static void add_mac_mobility_to_attr(uint32_t seq_num
, struct attr
*attr
)
974 struct ecommunity ecom_tmp
;
975 struct ecommunity_val eval
;
976 uint8_t *ecom_val_ptr
;
981 struct ecommunity
*ecomm
= bgp_attr_get_ecommunity(attr
);
984 encode_mac_mobility_extcomm(0, seq_num
, &eval
);
986 /* Find current MM ecommunity */
990 for (i
= 0; i
< ecomm
->size
; i
++) {
991 pnt
= ecomm
->val
+ (i
* ecomm
->unit_size
);
995 if (type
== ECOMMUNITY_ENCODE_EVPN
997 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
) {
999 (ecomm
->val
+ (i
* ecomm
->unit_size
));
1005 /* Update the existing MM ecommunity */
1007 memcpy(ecom_val_ptr
, eval
.val
, sizeof(char) * ecomm
->unit_size
);
1009 /* Add MM to existing */
1011 memset(&ecom_tmp
, 0, sizeof(ecom_tmp
));
1013 ecom_tmp
.unit_size
= ECOMMUNITY_SIZE
;
1014 ecom_tmp
.val
= (uint8_t *)eval
.val
;
1017 bgp_attr_set_ecommunity(
1018 attr
, ecommunity_merge(ecomm
, &ecom_tmp
));
1020 bgp_attr_set_ecommunity(attr
,
1021 ecommunity_dup(&ecom_tmp
));
1025 /* Install EVPN route into zebra. */
1026 static int evpn_zebra_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1027 const struct prefix_evpn
*p
,
1028 struct bgp_path_info
*pi
)
1035 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
1038 if (pi
->sub_type
== BGP_ROUTE_IMPORTED
) {
1039 if (pi
->attr
->sticky
)
1040 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
);
1041 if (pi
->attr
->default_gw
)
1042 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
);
1043 if (is_evpn_prefix_ipaddr_v6(p
) &&
1044 pi
->attr
->router_flag
)
1045 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
1047 seq
= mac_mobility_seqnum(pi
->attr
);
1048 /* if local ES notify zebra that this is a sync path */
1049 if (bgp_evpn_attr_is_local_es(pi
->attr
)) {
1050 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_SYNC_PATH
);
1051 if (bgp_evpn_attr_is_proxy(pi
->attr
))
1053 ZEBRA_MACIP_TYPE_PROXY_ADVERT
);
1056 if (!bgp_evpn_attr_is_sync(pi
->attr
))
1059 /* if a local path is being turned around and sent
1060 * to zebra it is because it is a sync path on
1063 SET_FLAG(flags
, ZEBRA_MACIP_TYPE_SYNC_PATH
);
1064 /* supply the highest peer seq number to zebra
1065 * for MM seq syncing
1067 seq
= bgp_evpn_attr_get_sync_seq(pi
->attr
);
1068 /* if any of the paths from the peer have the ROUTER
1069 * flag set install the local entry as a router entry
1071 if (is_evpn_prefix_ipaddr_v6(p
) &&
1072 (pi
->attr
->es_flags
&
1073 ATTR_ES_PEER_ROUTER
))
1075 ZEBRA_MACIP_TYPE_ROUTER_FLAG
);
1077 if (!(pi
->attr
->es_flags
& ATTR_ES_PEER_ACTIVE
))
1079 ZEBRA_MACIP_TYPE_PROXY_ADVERT
);
1082 ret
= bgp_zebra_send_remote_macip(
1083 bgp
, vpn
, p
, pi
->attr
->nexthop
, 1, flags
,
1084 seq
, bgp_evpn_attr_get_esi(pi
->attr
));
1085 } else if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
) {
1086 ret
= bgp_evpn_remote_es_evi_add(bgp
, vpn
, p
);
1088 switch (bgp_attr_get_pmsi_tnl_type(pi
->attr
)) {
1089 case PMSI_TNLTYPE_INGR_REPL
:
1090 flood_control
= VXLAN_FLOOD_HEAD_END_REPL
;
1093 case PMSI_TNLTYPE_PIM_SM
:
1094 flood_control
= VXLAN_FLOOD_PIM_SM
;
1098 flood_control
= VXLAN_FLOOD_DISABLED
;
1101 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
, flood_control
, 1);
1107 /* Uninstall EVPN route from zebra. */
1108 static int evpn_zebra_uninstall(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1109 const struct prefix_evpn
*p
,
1110 struct in_addr remote_vtep_ip
)
1114 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
1115 ret
= bgp_zebra_send_remote_macip(bgp
, vpn
, p
, remote_vtep_ip
,
1117 else if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
1118 ret
= bgp_evpn_remote_es_evi_del(bgp
, vpn
, p
);
1120 ret
= bgp_zebra_send_remote_vtep(bgp
, vpn
, p
,
1121 VXLAN_FLOOD_DISABLED
, 0);
1127 * Due to MAC mobility, the prior "local" best route has been supplanted
1128 * by a "remote" best route. The prior route has to be deleted and withdrawn
1131 static void evpn_delete_old_local_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1132 struct bgp_dest
*dest
,
1133 struct bgp_path_info
*old_local
,
1134 struct bgp_path_info
*new_select
)
1136 struct bgp_dest
*global_dest
;
1137 struct bgp_path_info
*pi
;
1138 afi_t afi
= AFI_L2VPN
;
1139 safi_t safi
= SAFI_EVPN
;
1141 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
1142 char esi_buf
[ESI_STR_LEN
];
1143 char esi_buf2
[ESI_STR_LEN
];
1144 struct prefix_evpn
*evp
=
1145 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1147 zlog_debug("local path deleted %pFX es %s; new-path-es %s", evp
,
1148 esi_to_str(&old_local
->attr
->esi
, esi_buf
,
1150 new_select
? esi_to_str(&new_select
->attr
->esi
,
1151 esi_buf2
, sizeof(esi_buf2
))
1155 /* Locate route node in the global EVPN routing table. Note that
1156 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1159 global_dest
= bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
1160 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1163 /* Delete route entry in the global EVPN table. */
1164 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
1166 /* Schedule for processing - withdraws to peers happen from
1170 bgp_process(bgp
, global_dest
, afi
, safi
);
1171 bgp_dest_unlock_node(global_dest
);
1174 /* Delete route entry in the VNI route table, caller to remove. */
1175 bgp_path_info_delete(dest
, old_local
);
1179 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1181 * Note: vpn is NULL for local EAD-ES routes.
1183 int evpn_route_select_install(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1184 struct bgp_dest
*dest
)
1186 struct bgp_path_info
*old_select
, *new_select
;
1187 struct bgp_path_info_pair old_and_new
;
1188 afi_t afi
= AFI_L2VPN
;
1189 safi_t safi
= SAFI_EVPN
;
1192 /* Compute the best path. */
1193 bgp_best_selection(bgp
, dest
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
1195 old_select
= old_and_new
.old
;
1196 new_select
= old_and_new
.new;
1198 /* If the best path hasn't changed - see if there is still something to
1199 * update to zebra RIB.
1200 * Remote routes and SYNC route (i.e. local routes with
1201 * SYNCED_FROM_PEER flag) need to updated to zebra on any attr
1204 if (old_select
&& old_select
== new_select
1205 && old_select
->type
== ZEBRA_ROUTE_BGP
1206 && (old_select
->sub_type
== BGP_ROUTE_IMPORTED
||
1207 bgp_evpn_attr_is_sync(old_select
->attr
))
1208 && !CHECK_FLAG(dest
->flags
, BGP_NODE_USER_CLEAR
)
1209 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
1210 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
1211 if (bgp_zebra_has_route_changed(old_select
))
1212 ret
= evpn_zebra_install(
1214 (const struct prefix_evpn
*)bgp_dest_get_prefix(
1217 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1218 UNSET_FLAG(old_select
->flags
, BGP_PATH_LINK_BW_CHG
);
1219 bgp_zebra_clear_route_change_flags(dest
);
1223 /* If the user did a "clear" this flag will be set */
1224 UNSET_FLAG(dest
->flags
, BGP_NODE_USER_CLEAR
);
1226 /* bestpath has changed; update relevant fields and install or uninstall
1227 * into the zebra RIB.
1229 if (old_select
|| new_select
)
1230 bgp_bump_version(dest
);
1233 bgp_path_info_unset_flag(dest
, old_select
, BGP_PATH_SELECTED
);
1235 bgp_path_info_set_flag(dest
, new_select
, BGP_PATH_SELECTED
);
1236 bgp_path_info_unset_flag(dest
, new_select
,
1237 BGP_PATH_ATTR_CHANGED
);
1238 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
1239 UNSET_FLAG(new_select
->flags
, BGP_PATH_LINK_BW_CHG
);
1242 /* a local entry with the SYNC flag also results in a MAC-IP update
1245 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
1246 && (new_select
->sub_type
== BGP_ROUTE_IMPORTED
||
1247 bgp_evpn_attr_is_sync(new_select
->attr
))) {
1248 ret
= evpn_zebra_install(
1250 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1253 /* If an old best existed and it was a "local" route, the only
1255 * it would be supplanted is due to MAC mobility procedures. So,
1257 * need to do an implicit delete and withdraw that route from
1260 if (new_select
->sub_type
== BGP_ROUTE_IMPORTED
&&
1261 old_select
&& old_select
->peer
== bgp
->peer_self
1262 && old_select
->type
== ZEBRA_ROUTE_BGP
1263 && old_select
->sub_type
== BGP_ROUTE_STATIC
1265 evpn_delete_old_local_route(bgp
, vpn
, dest
,
1266 old_select
, new_select
);
1268 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
1269 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
1270 ret
= evpn_zebra_uninstall(
1272 (const struct prefix_evpn
*)bgp_dest_get_prefix(
1274 old_select
->attr
->nexthop
);
1277 /* Clear any route change flags. */
1278 bgp_zebra_clear_route_change_flags(dest
);
1280 /* Reap old select bgp_path_info, if it has been removed */
1281 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
1282 bgp_path_info_reap(dest
, old_select
);
1287 static struct bgp_path_info
*bgp_evpn_route_get_local_path(
1288 struct bgp
*bgp
, struct bgp_dest
*dest
)
1290 struct bgp_path_info
*tmp_pi
;
1291 struct bgp_path_info
*local_pi
= NULL
;
1293 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1294 tmp_pi
= tmp_pi
->next
) {
1295 if (bgp_evpn_is_path_local(bgp
, tmp_pi
)) {
1304 static int update_evpn_type5_route_entry(struct bgp
*bgp_evpn
,
1305 struct bgp
*bgp_vrf
, afi_t afi
,
1306 safi_t safi
, struct bgp_dest
*dest
,
1307 struct attr
*attr
, int *route_changed
)
1309 struct attr
*attr_new
= NULL
;
1310 struct bgp_path_info
*pi
= NULL
;
1311 mpls_label_t label
= MPLS_INVALID_LABEL
;
1312 struct bgp_path_info
*local_pi
= NULL
;
1313 struct bgp_path_info
*tmp_pi
= NULL
;
1316 /* locate the local route entry if any */
1317 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1318 tmp_pi
= tmp_pi
->next
) {
1319 if (tmp_pi
->peer
== bgp_evpn
->peer_self
1320 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1321 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1326 * create a new route entry if one doesn't exist.
1327 * Otherwise see if route attr has changed
1331 /* route has changed as this is the first entry */
1334 /* Add (or update) attribute to hash. */
1335 attr_new
= bgp_attr_intern(attr
);
1337 /* create the route info from attribute */
1338 pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1339 bgp_evpn
->peer_self
, attr_new
, dest
);
1340 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
1342 /* Type-5 routes advertise the L3-VNI */
1343 bgp_path_info_extra_get(pi
);
1344 vni2label(bgp_vrf
->l3vni
, &label
);
1345 memcpy(&pi
->extra
->label
, &label
, sizeof(label
));
1346 pi
->extra
->num_labels
= 1;
1348 /* add the route entry to route node*/
1349 bgp_path_info_add(dest
, pi
);
1353 if (!attrhash_cmp(tmp_pi
->attr
, attr
)) {
1355 /* attribute changed */
1358 /* The attribute has changed. */
1359 /* Add (or update) attribute to hash. */
1360 attr_new
= bgp_attr_intern(attr
);
1361 bgp_path_info_set_flag(dest
, tmp_pi
,
1362 BGP_PATH_ATTR_CHANGED
);
1364 /* Restore route, if needed. */
1365 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1366 bgp_path_info_restore(dest
, tmp_pi
);
1368 /* Unintern existing, set to new. */
1369 bgp_attr_unintern(&tmp_pi
->attr
);
1370 tmp_pi
->attr
= attr_new
;
1371 tmp_pi
->uptime
= monotime(NULL
);
1377 /* update evpn type-5 route entry */
1378 static int update_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
,
1379 struct attr
*src_attr
, afi_t src_afi
,
1382 afi_t afi
= AFI_L2VPN
;
1383 safi_t safi
= SAFI_EVPN
;
1385 struct bgp_dest
*dest
= NULL
;
1386 struct bgp
*bgp_evpn
= NULL
;
1387 int route_changed
= 0;
1389 bgp_evpn
= bgp_get_evpn();
1393 /* Build path attribute for this route - use the source attr, if
1394 * present, else treat as locally originated.
1399 memset(&attr
, 0, sizeof(attr
));
1400 bgp_attr_default_set(&attr
, bgp_vrf
, BGP_ORIGIN_IGP
);
1403 /* Advertise Primary IP (PIP) is enabled, send individual
1404 * IP (default instance router-id) as nexthop.
1405 * PIP is disabled or vrr interface is not present
1406 * use anycast-IP as nexthop and anycast RMAC.
1408 if (!bgp_vrf
->evpn_info
->advertise_pip
||
1409 (!bgp_vrf
->evpn_info
->is_anycast_mac
)) {
1410 attr
.nexthop
= bgp_vrf
->originator_ip
;
1411 attr
.mp_nexthop_global_in
= bgp_vrf
->originator_ip
;
1412 memcpy(&attr
.rmac
, &bgp_vrf
->rmac
, ETH_ALEN
);
1415 memcpy(&attr
.rmac
, &bgp_vrf
->evpn_info
->pip_rmac
, ETH_ALEN
);
1416 if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
!= INADDR_ANY
) {
1417 attr
.nexthop
= bgp_vrf
->evpn_info
->pip_ip
;
1418 attr
.mp_nexthop_global_in
= bgp_vrf
->evpn_info
->pip_ip
;
1419 } else if (bgp_vrf
->evpn_info
->pip_ip
.s_addr
== INADDR_ANY
)
1420 if (bgp_debug_zebra(NULL
))
1422 "VRF %s evp %pFX advertise-pip primary ip is not configured",
1423 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
);
1426 if (bgp_debug_zebra(NULL
))
1428 "VRF %s type-5 route evp %pFX RMAC %pEA nexthop %pI4",
1429 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
, &attr
.rmac
,
1432 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1434 if (src_afi
== AFI_IP6
&&
1435 CHECK_FLAG(bgp_vrf
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1436 BGP_L2VPN_EVPN_ADV_IPV6_UNICAST_GW_IP
)) {
1438 !IN6_IS_ADDR_UNSPECIFIED(&src_attr
->mp_nexthop_global
)) {
1439 attr
.evpn_overlay
.type
= OVERLAY_INDEX_GATEWAY_IP
;
1440 SET_IPADDR_V6(&attr
.evpn_overlay
.gw_ip
);
1441 memcpy(&attr
.evpn_overlay
.gw_ip
.ipaddr_v6
,
1442 &src_attr
->mp_nexthop_global
,
1443 sizeof(struct in6_addr
));
1445 } else if (src_afi
== AFI_IP
&&
1446 CHECK_FLAG(bgp_vrf
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1447 BGP_L2VPN_EVPN_ADV_IPV4_UNICAST_GW_IP
)) {
1448 if (src_attr
&& src_attr
->nexthop
.s_addr
!= 0) {
1449 attr
.evpn_overlay
.type
= OVERLAY_INDEX_GATEWAY_IP
;
1450 SET_IPADDR_V4(&attr
.evpn_overlay
.gw_ip
);
1451 memcpy(&attr
.evpn_overlay
.gw_ip
.ipaddr_v4
,
1452 &src_attr
->nexthop
, sizeof(struct in_addr
));
1456 /* Setup RT and encap extended community */
1457 build_evpn_type5_route_extcomm(bgp_vrf
, &attr
);
1459 /* get the route node in global table */
1460 dest
= bgp_global_evpn_node_get(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
1461 (const struct prefix_evpn
*)evp
,
1465 /* create or update the route entry within the route node */
1466 update_evpn_type5_route_entry(bgp_evpn
, bgp_vrf
, afi
, safi
, dest
, &attr
,
1469 /* schedule for processing and unlock node */
1470 if (route_changed
) {
1471 bgp_process(bgp_evpn
, dest
, afi
, safi
);
1472 bgp_dest_unlock_node(dest
);
1475 /* uninten temporary */
1477 aspath_unintern(&attr
.aspath
);
1481 static void bgp_evpn_get_sync_info(struct bgp
*bgp
, esi_t
*esi
,
1482 struct bgp_dest
*dest
, uint32_t loc_seq
,
1483 uint32_t *max_sync_seq
, bool *active_on_peer
,
1484 bool *peer_router
, bool *proxy_from_peer
)
1486 struct bgp_path_info
*tmp_pi
;
1487 struct bgp_path_info
*second_best_path
= NULL
;
1488 uint32_t tmp_mm_seq
= 0;
1492 /* find the best non-local path. a local path can only be present
1495 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1496 tmp_pi
= tmp_pi
->next
) {
1497 if (tmp_pi
->sub_type
!= BGP_ROUTE_IMPORTED
||
1498 !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
1501 if (bgp_evpn_path_info_cmp(bgp
, tmp_pi
,
1502 second_best_path
, &paths_eq
))
1503 second_best_path
= tmp_pi
;
1506 if (!second_best_path
)
1509 tmp_esi
= bgp_evpn_attr_get_esi(second_best_path
->attr
);
1510 /* if this has the same ES desination as the local path
1513 if (!memcmp(esi
, tmp_esi
, sizeof(esi_t
))) {
1514 tmp_mm_seq
= mac_mobility_seqnum(second_best_path
->attr
);
1515 if (tmp_mm_seq
< loc_seq
)
1518 /* we have a non-proxy path from the ES peer. */
1519 if (second_best_path
->attr
->es_flags
&
1520 ATTR_ES_PROXY_ADVERT
) {
1521 *proxy_from_peer
= true;
1523 *active_on_peer
= true;
1526 if (second_best_path
->attr
->router_flag
)
1527 *peer_router
= true;
1529 /* we use both proxy and non-proxy imports to
1530 * determine the max sync sequence
1532 if (tmp_mm_seq
> *max_sync_seq
)
1533 *max_sync_seq
= tmp_mm_seq
;
1537 /* Bubble up sync-info from all paths (non-best) to the local-path.
1538 * This is need for MM sequence number syncing and proxy advertisement.
1539 * Note: The local path can only exist as a best path in the
1540 * VPN route table. It will take precedence over all sync paths.
1542 static void update_evpn_route_entry_sync_info(struct bgp
*bgp
,
1543 struct bgp_dest
*dest
,
1545 uint32_t loc_seq
, bool setup_sync
)
1548 struct prefix_evpn
*evp
=
1549 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1551 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
1554 esi
= bgp_evpn_attr_get_esi(attr
);
1555 if (bgp_evpn_is_esi_valid(esi
)) {
1557 uint32_t max_sync_seq
= 0;
1558 bool active_on_peer
= false;
1559 bool peer_router
= false;
1560 bool proxy_from_peer
= false;
1562 bgp_evpn_get_sync_info(bgp
, esi
, dest
, loc_seq
,
1563 &max_sync_seq
, &active_on_peer
,
1564 &peer_router
, &proxy_from_peer
);
1565 attr
->mm_sync_seqnum
= max_sync_seq
;
1567 attr
->es_flags
|= ATTR_ES_PEER_ACTIVE
;
1569 attr
->es_flags
&= ~ATTR_ES_PEER_ACTIVE
;
1570 if (proxy_from_peer
)
1571 attr
->es_flags
|= ATTR_ES_PEER_PROXY
;
1573 attr
->es_flags
&= ~ATTR_ES_PEER_PROXY
;
1575 attr
->es_flags
|= ATTR_ES_PEER_ROUTER
;
1577 attr
->es_flags
&= ~ATTR_ES_PEER_ROUTER
;
1579 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
1580 char esi_buf
[ESI_STR_LEN
];
1583 "setup sync info for %pFX es %s max_seq %d %s%s%s",
1585 esi_to_str(esi
, esi_buf
,
1588 (attr
->es_flags
& ATTR_ES_PEER_ACTIVE
)
1591 (attr
->es_flags
& ATTR_ES_PEER_PROXY
)
1594 (attr
->es_flags
& ATTR_ES_PEER_ROUTER
)
1600 attr
->mm_sync_seqnum
= 0;
1601 attr
->es_flags
&= ~ATTR_ES_PEER_ACTIVE
;
1602 attr
->es_flags
&= ~ATTR_ES_PEER_PROXY
;
1607 * Create or update EVPN route entry. This could be in the VNI route table
1608 * or the global route table.
1610 static int update_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1611 afi_t afi
, safi_t safi
,
1612 struct bgp_dest
*dest
, struct attr
*attr
,
1613 int add
, struct bgp_path_info
**pi
,
1614 uint8_t flags
, uint32_t seq
, bool vpn_rt
,
1617 struct bgp_path_info
*tmp_pi
;
1618 struct bgp_path_info
*local_pi
;
1619 struct attr
*attr_new
;
1620 mpls_label_t label
[BGP_MAX_LABELS
];
1621 uint32_t num_labels
= 1;
1622 int route_change
= 1;
1624 const struct prefix_evpn
*evp
;
1627 evp
= (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
1628 memset(&label
, 0, sizeof(label
));
1630 /* See if this is an update of an existing route, or a new add. */
1631 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
1633 /* If route doesn't exist already, create a new one, if told to.
1634 * Otherwise act based on whether the attributes of the route have
1637 if (!local_pi
&& !add
)
1640 if (old_is_sync
&& local_pi
)
1641 *old_is_sync
= bgp_evpn_attr_is_sync(local_pi
->attr
);
1643 /* if a local path is being added with a non-zero esi look
1644 * for SYNC paths from ES peers and bubble up the sync-info
1646 update_evpn_route_entry_sync_info(bgp
, dest
, attr
, seq
, vpn_rt
);
1648 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1649 if (seq
&& !CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
))
1650 add_mac_mobility_to_attr(seq
, attr
);
1653 /* Add (or update) attribute to hash. */
1654 attr_new
= bgp_attr_intern(attr
);
1656 /* Extract MAC mobility sequence number, if any. */
1657 attr_new
->mm_seqnum
=
1658 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1659 attr_new
->sticky
= sticky
;
1661 /* Create new route with its attribute. */
1662 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
1663 bgp
->peer_self
, attr_new
, dest
);
1664 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
1665 bgp_path_info_extra_get(tmp_pi
);
1667 /* The VNI goes into the 'label' field of the route */
1668 vni2label(vpn
->vni
, &label
[0]);
1670 /* Type-2 routes may carry a second VNI - the L3-VNI.
1671 * Only attach second label if we are advertising two labels for
1674 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1675 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
1678 l3vni
= bgpevpn_get_l3vni(vpn
);
1680 vni2label(l3vni
, &label
[1]);
1685 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1686 tmp_pi
->extra
->num_labels
= num_labels
;
1687 /* Mark route as self type-2 route */
1688 if (flags
&& CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1689 tmp_pi
->extra
->af_flags
= BGP_EVPN_MACIP_TYPE_SVI_IP
;
1690 bgp_path_info_add(dest
, tmp_pi
);
1693 if (attrhash_cmp(tmp_pi
->attr
, attr
)
1694 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1698 * The attributes have changed, type-2 routes needs to
1699 * be advertised with right labels.
1701 vni2label(vpn
->vni
, &label
[0]);
1702 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1703 && CHECK_FLAG(vpn
->flags
,
1704 VNI_FLAG_USE_TWO_LABELS
)) {
1707 l3vni
= bgpevpn_get_l3vni(vpn
);
1709 vni2label(l3vni
, &label
[1]);
1713 memcpy(&tmp_pi
->extra
->label
, label
, sizeof(label
));
1714 tmp_pi
->extra
->num_labels
= num_labels
;
1716 /* The attribute has changed. */
1717 /* Add (or update) attribute to hash. */
1718 attr_new
= bgp_attr_intern(attr
);
1719 bgp_path_info_set_flag(dest
, tmp_pi
,
1720 BGP_PATH_ATTR_CHANGED
);
1722 /* Extract MAC mobility sequence number, if any. */
1723 attr_new
->mm_seqnum
=
1724 bgp_attr_mac_mobility_seqnum(attr_new
, &sticky
);
1725 attr_new
->sticky
= sticky
;
1727 /* Restore route, if needed. */
1728 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
1729 bgp_path_info_restore(dest
, tmp_pi
);
1731 /* Unintern existing, set to new. */
1732 bgp_attr_unintern(&tmp_pi
->attr
);
1733 tmp_pi
->attr
= attr_new
;
1734 tmp_pi
->uptime
= monotime(NULL
);
1738 /* local MAC-IP routes in the VNI table are linked to
1739 * the destination ES
1741 if (route_change
&& vpn_rt
1742 && (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
))
1743 bgp_evpn_path_es_link(tmp_pi
, vpn
->vni
,
1744 bgp_evpn_attr_get_esi(tmp_pi
->attr
));
1746 /* Return back the route entry. */
1748 return route_change
;
1751 static void evpn_zebra_reinstall_best_route(struct bgp
*bgp
,
1752 struct bgpevpn
*vpn
,
1753 struct bgp_dest
*dest
)
1755 struct bgp_path_info
*tmp_ri
;
1756 struct bgp_path_info
*curr_select
= NULL
;
1758 for (tmp_ri
= bgp_dest_get_bgp_path_info(dest
); tmp_ri
;
1759 tmp_ri
= tmp_ri
->next
) {
1760 if (CHECK_FLAG(tmp_ri
->flags
, BGP_PATH_SELECTED
)) {
1761 curr_select
= tmp_ri
;
1766 if (curr_select
&& curr_select
->type
== ZEBRA_ROUTE_BGP
1767 && (curr_select
->sub_type
== BGP_ROUTE_IMPORTED
||
1768 bgp_evpn_attr_is_sync(curr_select
->attr
)))
1769 evpn_zebra_install(bgp
, vpn
,
1770 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
),
1775 * If the local route was not selected evict it and tell zebra to re-add
1776 * the best remote dest.
1778 * Typically a local path added by zebra is expected to be selected as
1779 * best. In which case when a remote path wins as best (later)
1780 * evpn_route_select_install itself evicts the older-local-best path.
1782 * However if bgp's add and zebra's add cross paths (race condition) it
1783 * is possible that the local path is no longer the "older" best path.
1784 * It is a path that was never designated as best and hence requires
1785 * additional handling to prevent bgp from injecting and holding on to a
1786 * non-best local path.
1788 static void evpn_cleanup_local_non_best_route(struct bgp
*bgp
,
1789 struct bgpevpn
*vpn
,
1790 struct bgp_dest
*dest
,
1791 struct bgp_path_info
*local_pi
)
1793 /* local path was not picked as the winner; kick it out */
1794 if (bgp_debug_zebra(NULL
))
1795 zlog_debug("evicting local evpn prefix %pBD as remote won",
1798 evpn_delete_old_local_route(bgp
, vpn
, dest
, local_pi
, NULL
);
1799 bgp_path_info_reap(dest
, local_pi
);
1801 /* tell zebra to re-add the best remote path */
1802 evpn_zebra_reinstall_best_route(bgp
, vpn
, dest
);
1805 static inline bool bgp_evpn_route_add_l3_ecomm_ok(struct bgpevpn
*vpn
,
1806 const struct prefix_evpn
*p
,
1809 return p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
1810 && (is_evpn_prefix_ipaddr_v4(p
)
1811 || (is_evpn_prefix_ipaddr_v6(p
)
1812 && !IN6_IS_ADDR_LINKLOCAL(
1813 &p
->prefix
.macip_addr
.ip
.ipaddr_v6
)))
1814 && CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)
1815 && bgpevpn_get_l3vni(vpn
) && bgp_evpn_es_add_l3_ecomm_ok(esi
);
1819 * Create or update EVPN route (of type based on prefix) for specified VNI
1820 * and schedule for processing.
1822 static int update_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
1823 struct prefix_evpn
*p
, uint8_t flags
,
1824 uint32_t seq
, esi_t
*esi
)
1826 struct bgp_dest
*dest
;
1828 struct attr
*attr_new
;
1829 int add_l3_ecomm
= 0;
1830 struct bgp_path_info
*pi
;
1831 afi_t afi
= AFI_L2VPN
;
1832 safi_t safi
= SAFI_EVPN
;
1834 bool old_is_sync
= false;
1836 memset(&attr
, 0, sizeof(attr
));
1838 /* Build path-attribute for this route. */
1839 bgp_attr_default_set(&attr
, bgp
, BGP_ORIGIN_IGP
);
1840 attr
.nexthop
= vpn
->originator_ip
;
1841 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
1842 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
1843 attr
.sticky
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
) ? 1 : 0;
1844 attr
.default_gw
= CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_GW
) ? 1 : 0;
1845 attr
.router_flag
= CHECK_FLAG(flags
,
1846 ZEBRA_MACIP_TYPE_ROUTER_FLAG
) ? 1 : 0;
1847 if (CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_PROXY_ADVERT
))
1848 attr
.es_flags
|= ATTR_ES_PROXY_ADVERT
;
1850 if (esi
&& bgp_evpn_is_esi_valid(esi
)) {
1851 memcpy(&attr
.esi
, esi
, sizeof(esi_t
));
1852 attr
.es_flags
|= ATTR_ES_IS_LOCAL
;
1855 /* PMSI is only needed for type-3 routes */
1856 if (p
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
) {
1857 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
);
1858 bgp_attr_set_pmsi_tnl_type(&attr
, PMSI_TNLTYPE_INGR_REPL
);
1861 /* router mac is only needed for type-2 routes here. */
1862 if (p
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
1863 uint8_t af_flags
= 0;
1865 if (CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_SVI_IP
))
1866 SET_FLAG(af_flags
, BGP_EVPN_MACIP_TYPE_SVI_IP
);
1868 bgp_evpn_get_rmac_nexthop(vpn
, p
, &attr
, af_flags
);
1871 if (bgp_debug_zebra(NULL
)) {
1872 char buf3
[ESI_STR_LEN
];
1875 "VRF %s vni %u type-2 route evp %pFX RMAC %pEA nexthop %pI4 esi %s",
1876 vpn
->bgp_vrf
? vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
)
1878 vpn
->vni
, p
, &attr
.rmac
, &attr
.mp_nexthop_global_in
,
1879 esi_to_str(esi
, buf3
, sizeof(buf3
)));
1882 vni2label(vpn
->vni
, &(attr
.label
));
1884 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1885 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1888 add_l3_ecomm
= bgp_evpn_route_add_l3_ecomm_ok(
1889 vpn
, p
, (attr
.es_flags
& ATTR_ES_IS_LOCAL
) ? &attr
.esi
: NULL
);
1891 /* Set up extended community. */
1892 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
1894 /* First, create (or fetch) route node within the VNI. */
1895 /* NOTE: There is no RD here. */
1896 dest
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
1898 /* Create or update route entry. */
1899 route_change
= update_evpn_route_entry(bgp
, vpn
, afi
, safi
, dest
, &attr
,
1901 true /* setup_sync */, &old_is_sync
);
1903 attr_new
= pi
->attr
;
1905 /* lock ri to prevent freeing in evpn_route_select_install */
1906 bgp_path_info_lock(pi
);
1908 /* Perform route selection. Normally, the local route in the
1909 * VNI is expected to win and be the best route. However, if
1910 * there is a race condition where a host moved from local to
1911 * remote and the remote route was received in BGP just prior
1912 * to the local MACIP notification from zebra, the remote
1913 * route would win, and we should evict the defunct local route
1914 * and (re)install the remote route into zebra.
1916 evpn_route_select_install(bgp
, vpn
, dest
);
1918 * If the new local route was not selected evict it and tell zebra
1919 * to re-add the best remote dest. BGP doesn't retain non-best local
1922 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
1925 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1927 evpn_cleanup_local_non_best_route(bgp
, vpn
, dest
, pi
);
1931 /* If the local path already existed and is still the
1932 * best path we need to also check if it transitioned
1933 * from being a sync path to a non-sync path. If it
1934 * it did we need to notify zebra that the sync-path
1937 new_is_sync
= bgp_evpn_attr_is_sync(pi
->attr
);
1938 if (!new_is_sync
&& old_is_sync
)
1939 evpn_zebra_uninstall(bgp
, vpn
, p
, zero_vtep_ip
);
1942 bgp_path_info_unlock(pi
);
1944 bgp_dest_unlock_node(dest
);
1946 /* If this is a new route or some attribute has changed, export the
1947 * route to the global table. The route will be advertised to peers
1948 * from there. Note that this table is a 2-level tree (RD-level +
1949 * Prefix-level) similar to L3VPN routes.
1952 struct bgp_path_info
*global_pi
;
1954 dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1955 (const struct prefix_evpn
*)p
,
1957 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, dest
, attr_new
, 1,
1958 &global_pi
, flags
, seq
,
1959 false /* setup_sync */, NULL
/* old_is_sync */);
1961 /* Schedule for processing and unlock node. */
1962 bgp_process(bgp
, dest
, afi
, safi
);
1963 bgp_dest_unlock_node(dest
);
1966 /* Unintern temporary. */
1967 aspath_unintern(&attr
.aspath
);
1973 * Delete EVPN route entry.
1974 * The entry can be in ESI/VNI table or the global table.
1976 void delete_evpn_route_entry(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1977 struct bgp_dest
*dest
,
1978 struct bgp_path_info
**pi
)
1980 struct bgp_path_info
*tmp_pi
;
1984 /* Now, find matching route. */
1985 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
1986 tmp_pi
= tmp_pi
->next
)
1987 if (tmp_pi
->peer
== bgp
->peer_self
1988 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
1989 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
1994 /* Mark route for delete. */
1996 bgp_path_info_delete(dest
, tmp_pi
);
1999 /* Delete EVPN type5 route */
2000 static int delete_evpn_type5_route(struct bgp
*bgp_vrf
, struct prefix_evpn
*evp
)
2002 afi_t afi
= AFI_L2VPN
;
2003 safi_t safi
= SAFI_EVPN
;
2004 struct bgp_dest
*dest
= NULL
;
2005 struct bgp_path_info
*pi
= NULL
;
2006 struct bgp
*bgp_evpn
= NULL
; /* evpn bgp instance */
2008 bgp_evpn
= bgp_get_evpn();
2012 /* locate the global route entry for this type-5 prefix */
2013 dest
= bgp_global_evpn_node_lookup(bgp_evpn
->rib
[afi
][safi
], afi
, safi
,
2014 (const struct prefix_evpn
*)evp
, &bgp_vrf
->vrf_prd
);
2018 delete_evpn_route_entry(bgp_evpn
, afi
, safi
, dest
, &pi
);
2020 bgp_process(bgp_evpn
, dest
, afi
, safi
);
2021 bgp_dest_unlock_node(dest
);
2026 * Delete EVPN route (of type based on prefix) for specified VNI and
2027 * schedule for processing.
2029 static int delete_evpn_route(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2030 struct prefix_evpn
*p
)
2032 struct bgp_dest
*dest
, *global_dest
;
2033 struct bgp_path_info
*pi
;
2034 afi_t afi
= AFI_L2VPN
;
2035 safi_t safi
= SAFI_EVPN
;
2037 /* First, locate the route node within the VNI. If it doesn't exist,
2039 * is nothing further to do.
2041 /* NOTE: There is no RD here. */
2042 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2046 /* Next, locate route node in the global EVPN routing table. Note that
2047 * this table is a 2-level tree (RD-level + Prefix-level) similar to
2050 global_dest
= bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
2051 (const struct prefix_evpn
*)p
, &vpn
->prd
);
2053 /* Delete route entry in the global EVPN table. */
2054 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
2056 /* Schedule for processing - withdraws to peers happen from
2060 bgp_process(bgp
, global_dest
, afi
, safi
);
2061 bgp_dest_unlock_node(global_dest
);
2064 /* Delete route entry in the VNI route table. This can just be removed.
2066 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2068 bgp_path_info_reap(dest
, pi
);
2069 evpn_route_select_install(bgp
, vpn
, dest
);
2071 bgp_dest_unlock_node(dest
);
2076 void bgp_evpn_update_type2_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2077 struct bgp_dest
*dest
,
2078 struct bgp_path_info
*local_pi
,
2081 afi_t afi
= AFI_L2VPN
;
2082 safi_t safi
= SAFI_EVPN
;
2083 struct bgp_path_info
*pi
;
2085 struct attr
*attr_new
;
2087 int add_l3_ecomm
= 0;
2088 struct bgp_dest
*global_dest
;
2089 struct bgp_path_info
*global_pi
;
2090 struct prefix_evpn
*evp
=
2091 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
2093 bool old_is_sync
= false;
2095 if (CHECK_FLAG(local_pi
->flags
, BGP_PATH_REMOVED
))
2099 * Build attribute per local route as the MAC mobility and
2100 * some other values could differ for different routes. The
2101 * attributes will be shared in the hash table.
2103 bgp_attr_default_set(&attr
, bgp
, BGP_ORIGIN_IGP
);
2104 attr
.nexthop
= vpn
->originator_ip
;
2105 attr
.mp_nexthop_global_in
= vpn
->originator_ip
;
2106 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
2107 attr
.sticky
= (local_pi
->attr
->sticky
) ? 1 : 0;
2108 attr
.router_flag
= (local_pi
->attr
->router_flag
) ? 1 : 0;
2109 attr
.es_flags
= local_pi
->attr
->es_flags
;
2110 if (local_pi
->attr
->default_gw
) {
2111 attr
.default_gw
= 1;
2112 if (is_evpn_prefix_ipaddr_v6(evp
))
2113 attr
.router_flag
= 1;
2115 memcpy(&attr
.esi
, &local_pi
->attr
->esi
, sizeof(esi_t
));
2116 bgp_evpn_get_rmac_nexthop(vpn
, evp
, &attr
,
2117 local_pi
->extra
->af_flags
);
2118 vni2label(vpn
->vni
, &(attr
.label
));
2119 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2120 * using L3 VNI for type-2 routes also.
2122 add_l3_ecomm
= bgp_evpn_route_add_l3_ecomm_ok(
2124 (attr
.es_flags
& ATTR_ES_IS_LOCAL
) ? &attr
.esi
: NULL
);
2126 /* Set up extended community. */
2127 build_evpn_route_extcomm(vpn
, &attr
, add_l3_ecomm
);
2128 seq
= mac_mobility_seqnum(local_pi
->attr
);
2130 if (bgp_debug_zebra(NULL
)) {
2131 char buf3
[ESI_STR_LEN
];
2134 "VRF %s vni %u evp %pFX RMAC %pEA nexthop %pI4 esi %s esf 0x%x from %s",
2135 vpn
->bgp_vrf
? vrf_id_to_name(vpn
->bgp_vrf
->vrf_id
)
2137 vpn
->vni
, evp
, &attr
.rmac
, &attr
.mp_nexthop_global_in
,
2138 esi_to_str(&attr
.esi
, buf3
, sizeof(buf3
)),
2139 attr
.es_flags
, caller
);
2142 /* Update the route entry. */
2143 route_change
= update_evpn_route_entry(
2144 bgp
, vpn
, afi
, safi
, dest
, &attr
, 0, &pi
, 0, seq
,
2145 true /* setup_sync */, &old_is_sync
);
2148 attr_new
= pi
->attr
;
2149 /* lock ri to prevent freeing in evpn_route_select_install */
2150 bgp_path_info_lock(pi
);
2152 /* Perform route selection. Normally, the local route in the
2153 * VNI is expected to win and be the best route. However,
2154 * under peculiar situations (e.g., tunnel (next hop) IP change
2155 * that causes best selection to be based on next hop), a
2156 * remote route could win. If the local route is the best,
2157 * ensure it is updated in the global EVPN route table and
2158 * advertised to peers; otherwise, ensure it is evicted and
2159 * (re)install the remote route into zebra.
2161 evpn_route_select_install(bgp
, vpn
, dest
);
2163 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2166 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
2168 evpn_cleanup_local_non_best_route(bgp
, vpn
, dest
, pi
);
2172 /* If the local path already existed and is still the
2173 * best path we need to also check if it transitioned
2174 * from being a sync path to a non-sync path. If it
2175 * it did we need to notify zebra that the sync-path
2178 new_is_sync
= bgp_evpn_attr_is_sync(pi
->attr
);
2179 if (!new_is_sync
&& old_is_sync
)
2180 evpn_zebra_uninstall(bgp
, vpn
,
2187 bgp_path_info_unlock(pi
);
2190 /* Update route in global routing table. */
2191 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
,
2192 safi
, evp
, &vpn
->prd
);
2193 assert(global_dest
);
2194 update_evpn_route_entry(
2195 bgp
, vpn
, afi
, safi
, global_dest
, attr_new
, 0,
2196 &global_pi
, 0, mac_mobility_seqnum(attr_new
),
2197 false /* setup_sync */, NULL
/* old_is_sync */);
2199 /* Schedule for processing and unlock node. */
2200 bgp_process(bgp
, global_dest
, afi
, safi
);
2201 bgp_dest_unlock_node(global_dest
);
2204 /* Unintern temporary. */
2205 aspath_unintern(&attr
.aspath
);
2209 * Update all type-2 (MACIP) local routes for this VNI - these should also
2210 * be scheduled for advertise to peers.
2212 static int update_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2214 struct bgp_dest
*dest
;
2215 struct bgp_path_info
*tmp_pi
;
2217 /* Walk this VNI's route table and update local type-2 routes. For any
2218 * routes updated, update corresponding entry in the global table too.
2220 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2221 dest
= bgp_route_next(dest
)) {
2222 const struct prefix_evpn
*evp
=
2223 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
2225 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2228 /* Identify local route. */
2229 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
2230 tmp_pi
= tmp_pi
->next
) {
2231 if (tmp_pi
->peer
== bgp
->peer_self
2232 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
2233 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
2240 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, tmp_pi
,
2248 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2249 * global routing table. These are also scheduled for withdraw from peers.
2251 static void delete_global_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2255 struct bgp_dest
*rddest
, *dest
;
2256 struct bgp_table
*table
;
2257 struct bgp_path_info
*pi
;
2262 rddest
= bgp_node_lookup(bgp
->rib
[afi
][safi
],
2263 (struct prefix
*)&vpn
->prd
);
2265 table
= bgp_dest_get_bgp_table_info(rddest
);
2266 for (dest
= bgp_table_top(table
); dest
;
2267 dest
= bgp_route_next(dest
)) {
2268 const struct prefix_evpn
*evp
=
2269 (const struct prefix_evpn
*)bgp_dest_get_prefix(
2272 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2275 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2277 bgp_process(bgp
, dest
, afi
, safi
);
2280 /* Unlock RD node. */
2281 bgp_dest_unlock_node(rddest
);
2286 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2287 * table as well as the per-VNI route table.
2289 static int delete_all_type2_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2293 struct bgp_dest
*dest
;
2294 struct bgp_path_info
*pi
;
2299 /* First, walk the global route table for this VNI's type-2 local
2301 * EVPN routes are a 2-level table, first get the RD table.
2303 delete_global_type2_routes(bgp
, vpn
);
2305 /* Next, walk this VNI's route table and delete local type-2 routes. */
2306 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2307 dest
= bgp_route_next(dest
)) {
2308 const struct prefix_evpn
*evp
=
2309 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
2311 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2314 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
2316 /* Route entry in local table gets deleted immediately. */
2318 bgp_path_info_reap(dest
, pi
);
2325 * Delete all routes in the per-VNI route table.
2327 static int delete_all_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2329 struct bgp_dest
*dest
;
2330 struct bgp_path_info
*pi
, *nextpi
;
2332 /* Walk this VNI's route table and delete all routes. */
2333 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
2334 dest
= bgp_route_next(dest
)) {
2335 for (pi
= bgp_dest_get_bgp_path_info(dest
);
2336 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2337 bgp_evpn_remote_ip_hash_del(vpn
, pi
);
2338 bgp_path_info_delete(dest
, pi
);
2339 bgp_path_info_reap(dest
, pi
);
2346 /* BUM traffic flood mode per-l2-vni */
2347 static int bgp_evpn_vni_flood_mode_get(struct bgp
*bgp
,
2348 struct bgpevpn
*vpn
)
2350 /* if flooding has been globally disabled per-vni mode is
2353 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
2354 return VXLAN_FLOOD_DISABLED
;
2356 /* if mcast group ip has been specified we use a PIM-SM MDT */
2357 if (vpn
->mcast_grp
.s_addr
!= INADDR_ANY
)
2358 return VXLAN_FLOOD_PIM_SM
;
2360 /* default is ingress replication */
2361 return VXLAN_FLOOD_HEAD_END_REPL
;
2365 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2366 * export RT getting modified or change to tunnel IP. Note that these
2367 * situations need the route in the per-VNI table as well as the global
2368 * table to be updated (as attributes change).
2370 int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2373 struct prefix_evpn p
;
2375 update_type1_routes_for_evi(bgp
, vpn
);
2377 /* Update and advertise the type-3 route (only one) followed by the
2378 * locally learnt type-2 routes (MACIP) - for this VNI.
2380 * RT-3 only if doing head-end replication
2382 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
2383 == VXLAN_FLOOD_HEAD_END_REPL
) {
2384 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2385 ret
= update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
);
2390 return update_all_type2_routes(bgp
, vpn
);
2394 * Delete (and withdraw) local routes for specified VNI from the global
2395 * table and per-VNI table. After this, remove all other routes from
2396 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2397 * (advertise-all-vni) being disabled.
2399 static int delete_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
2402 struct prefix_evpn p
;
2404 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2405 * followed by type-3 routes (only one) - for this VNI.
2407 ret
= delete_all_type2_routes(bgp
, vpn
);
2411 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2412 ret
= delete_evpn_route(bgp
, vpn
, &p
);
2416 /* Delete all routes from the per-VNI table. */
2417 return delete_all_vni_routes(bgp
, vpn
);
2421 * There is a flood mcast IP address change. Update the mcast-grp and
2422 * remove the type-3 route if any. A new type-3 route will be generated
2423 * post tunnel_ip update if the new flood mode is head-end-replication.
2425 static int bgp_evpn_mcast_grp_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2426 struct in_addr mcast_grp
)
2428 struct prefix_evpn p
;
2430 vpn
->mcast_grp
= mcast_grp
;
2432 if (is_vni_live(vpn
)) {
2433 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2434 delete_evpn_route(bgp
, vpn
, &p
);
2441 * There is a tunnel endpoint IP address change for this VNI, delete
2442 * prior type-3 route (if needed) and update.
2443 * Note: Route re-advertisement happens elsewhere after other processing
2446 static int handle_tunnel_ip_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2447 struct in_addr originator_ip
)
2449 struct prefix_evpn p
;
2451 /* If VNI is not live, we only need to update the originator ip */
2452 if (!is_vni_live(vpn
)) {
2453 vpn
->originator_ip
= originator_ip
;
2457 /* Update the tunnel-ip hash */
2458 bgp_tip_del(bgp
, &vpn
->originator_ip
);
2459 bgp_tip_add(bgp
, &originator_ip
);
2461 /* filter routes as martian nexthop db has changed */
2462 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
2464 /* Need to withdraw type-3 route as the originator IP is part
2467 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
2468 delete_evpn_route(bgp
, vpn
, &p
);
2470 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2471 vpn
->originator_ip
= originator_ip
;
2475 static struct bgp_path_info
*
2476 bgp_create_evpn_bgp_path_info(struct bgp_path_info
*parent_pi
,
2477 struct bgp_dest
*dest
, struct attr
*attr
)
2479 struct attr
*attr_new
;
2480 struct bgp_path_info
*pi
;
2482 /* Add (or update) attribute to hash. */
2483 attr_new
= bgp_attr_intern(attr
);
2485 /* Create new route with its attribute. */
2486 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0, parent_pi
->peer
,
2488 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
2489 bgp_path_info_extra_get(pi
);
2490 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
2491 bgp_dest_lock_node((struct bgp_dest
*)parent_pi
->net
);
2492 if (parent_pi
->extra
) {
2493 memcpy(&pi
->extra
->label
, &parent_pi
->extra
->label
,
2494 sizeof(pi
->extra
->label
));
2495 pi
->extra
->num_labels
= parent_pi
->extra
->num_labels
;
2496 pi
->extra
->igpmetric
= parent_pi
->extra
->igpmetric
;
2498 bgp_path_info_add(dest
, pi
);
2504 * Install route entry into the VRF routing table and invoke route selection.
2506 static int install_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2507 const struct prefix_evpn
*evp
,
2508 struct bgp_path_info
*parent_pi
)
2510 struct bgp_dest
*dest
;
2511 struct bgp_path_info
*pi
;
2513 struct attr
*attr_new
;
2516 struct prefix
*pp
= &p
;
2519 bool new_pi
= false;
2520 bool use_l3nhg
= false;
2521 bool is_l3nhg_active
= false;
2522 char buf1
[INET6_ADDRSTRLEN
];
2524 memset(pp
, 0, sizeof(struct prefix
));
2525 ip_prefix_from_evpn_prefix(evp
, pp
);
2527 if (bgp_debug_zebra(NULL
))
2529 "vrf %s: import evpn prefix %pFX parent %p flags 0x%x",
2530 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
, parent_pi
,
2533 /* Create (or fetch) route within the VRF. */
2534 /* NOTE: There is no RD here. */
2535 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2537 safi
= SAFI_UNICAST
;
2538 dest
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2539 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
2541 safi
= SAFI_UNICAST
;
2542 dest
= bgp_node_get(bgp_vrf
->rib
[afi
][safi
], pp
);
2546 /* EVPN routes currently only support a IPv4 next hop which corresponds
2547 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2548 * or prefix route, we have to convert the next hop to an IPv4-mapped
2549 * address for the rest of the code to flow through. In the case of IPv4,
2550 * make sure to set the flag for next hop attribute.
2552 attr
= *parent_pi
->attr
;
2553 if (attr
.evpn_overlay
.type
!= OVERLAY_INDEX_GATEWAY_IP
) {
2555 evpn_convert_nexthop_to_ipv6(&attr
);
2557 attr
.nexthop
= attr
.mp_nexthop_global_in
;
2558 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2563 * If gateway IP overlay index is specified in the NLRI of
2564 * EVPN RT-5, this gateway IP should be used as the nexthop
2565 * for the prefix in the VRF
2567 if (bgp_debug_zebra(NULL
)) {
2569 "Install gateway IP %s as nexthop for prefix %pFX in vrf %s",
2570 inet_ntop(pp
->family
, &attr
.evpn_overlay
.gw_ip
,
2571 buf1
, sizeof(buf1
)), pp
,
2572 vrf_id_to_name(bgp_vrf
->vrf_id
));
2575 if (afi
== AFI_IP6
) {
2576 memcpy(&attr
.mp_nexthop_global
,
2577 &attr
.evpn_overlay
.gw_ip
.ipaddr_v6
,
2578 sizeof(struct in6_addr
));
2579 attr
.mp_nexthop_len
= IPV6_MAX_BYTELEN
;
2581 attr
.nexthop
= attr
.evpn_overlay
.gw_ip
.ipaddr_v4
;
2582 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
2586 bgp_evpn_es_vrf_use_nhg(bgp_vrf
, &parent_pi
->attr
->esi
, &use_l3nhg
,
2587 &is_l3nhg_active
, NULL
);
2589 attr
.es_flags
|= ATTR_ES_L3_NHG_USE
;
2590 if (is_l3nhg_active
)
2591 attr
.es_flags
|= ATTR_ES_L3_NHG_ACTIVE
;
2593 /* Check if route entry is already present. */
2594 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2596 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2600 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, dest
, &attr
);
2603 if (attrhash_cmp(pi
->attr
, &attr
)
2604 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2605 bgp_dest_unlock_node(dest
);
2608 /* The attribute has changed. */
2609 /* Add (or update) attribute to hash. */
2610 attr_new
= bgp_attr_intern(&attr
);
2612 /* Restore route, if needed. */
2613 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2614 bgp_path_info_restore(dest
, pi
);
2616 /* Mark if nexthop has changed. */
2618 && !IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2620 && !IPV6_ADDR_SAME(&pi
->attr
->mp_nexthop_global
,
2621 &attr_new
->mp_nexthop_global
)))
2622 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2624 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ATTR_CHANGED
);
2625 /* Unintern existing, set to new. */
2626 bgp_attr_unintern(&pi
->attr
);
2627 pi
->attr
= attr_new
;
2628 pi
->uptime
= monotime(NULL
);
2631 /* Gateway IP nexthop should be resolved */
2632 if (attr
.evpn_overlay
.type
== OVERLAY_INDEX_GATEWAY_IP
) {
2633 if (bgp_find_or_add_nexthop(bgp_vrf
, bgp_vrf
, afi
, safi
, pi
,
2635 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_VALID
);
2637 if (BGP_DEBUG(nht
, NHT
)) {
2638 inet_ntop(pp
->family
,
2639 &attr
.evpn_overlay
.gw_ip
,
2640 buf1
, sizeof(buf1
));
2641 zlog_debug("%s: gateway IP NH unresolved",
2644 bgp_path_info_unset_flag(dest
, pi
, BGP_PATH_VALID
);
2648 /* as it is an importation, change nexthop */
2649 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ANNC_NH_SELF
);
2652 /* Link path to evpn nexthop */
2653 bgp_evpn_path_nh_add(bgp_vrf
, pi
);
2655 bgp_aggregate_increment(bgp_vrf
, bgp_dest_get_prefix(dest
), pi
, afi
,
2658 /* Perform route selection and update zebra, if required. */
2659 bgp_process(bgp_vrf
, dest
, afi
, safi
);
2661 /* Process for route leaking. */
2662 vpn_leak_from_vrf_update(bgp_get_default(), bgp_vrf
, pi
);
2664 bgp_dest_unlock_node(dest
);
2666 if (bgp_debug_zebra(NULL
))
2667 zlog_debug("... %s pi dest %p (l %d) pi %p (l %d, f 0x%x)",
2668 new_pi
? "new" : "update", dest
,
2669 bgp_dest_get_lock_count(dest
), pi
, pi
->lock
,
2676 * Install route entry into the VNI routing table and invoke route selection.
2678 static int install_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2679 const struct prefix_evpn
*p
,
2680 struct bgp_path_info
*parent_pi
)
2682 struct bgp_dest
*dest
;
2683 struct bgp_path_info
*pi
;
2684 struct bgp_path_info
*local_pi
;
2685 struct attr
*attr_new
;
2687 struct prefix_evpn ad_evp
;
2688 bool old_local_es
= false;
2691 /* EAD prefix in the global table doesn't include the VTEP-IP so
2692 * we need to create a different copy for the VNI
2694 if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
2695 p
= evpn_type1_prefix_vni_copy(&ad_evp
, p
,
2696 parent_pi
->attr
->nexthop
);
2698 /* Create (or fetch) route within the VNI. */
2699 /* NOTE: There is no RD here. */
2700 dest
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
2702 /* Check if route entry is already present. */
2703 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2705 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2709 /* Create an info */
2710 pi
= bgp_create_evpn_bgp_path_info(parent_pi
, dest
,
2712 new_local_es
= bgp_evpn_attr_is_local_es(pi
->attr
);
2714 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
2715 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
2716 bgp_dest_unlock_node(dest
);
2719 /* The attribute has changed. */
2720 /* Add (or update) attribute to hash. */
2721 attr_new
= bgp_attr_intern(parent_pi
->attr
);
2723 /* Restore route, if needed. */
2724 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
2725 bgp_path_info_restore(dest
, pi
);
2727 /* Mark if nexthop has changed. */
2728 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
2729 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2731 old_local_es
= bgp_evpn_attr_is_local_es(pi
->attr
);
2732 new_local_es
= bgp_evpn_attr_is_local_es(attr_new
);
2733 /* If ESI is different or if its type has changed we
2734 * need to reinstall the path in zebra
2736 if ((old_local_es
!= new_local_es
)
2737 || memcmp(&pi
->attr
->esi
, &attr_new
->esi
,
2738 sizeof(attr_new
->esi
))) {
2740 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
))
2741 zlog_debug("VNI %d path %pFX chg to %s es",
2742 vpn
->vni
, &pi
->net
->p
,
2743 new_local_es
? "local"
2745 bgp_path_info_set_flag(dest
, pi
, BGP_PATH_ATTR_CHANGED
);
2748 /* Unintern existing, set to new. */
2749 bgp_attr_unintern(&pi
->attr
);
2750 pi
->attr
= attr_new
;
2751 pi
->uptime
= monotime(NULL
);
2754 /* Add this route to remote IP hashtable */
2755 bgp_evpn_remote_ip_hash_add(vpn
, pi
);
2757 /* Perform route selection and update zebra, if required. */
2758 ret
= evpn_route_select_install(bgp
, vpn
, dest
);
2760 /* if the best path is a local path with a non-zero ES
2761 * sync info against the local path may need to be updated
2762 * when a remote path is added/updated (including changes
2763 * from sync-path to remote-path)
2765 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
2766 if (local_pi
&& (old_local_es
|| new_local_es
))
2767 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, local_pi
,
2769 bgp_dest_unlock_node(dest
);
2775 * Uninstall route entry from the VRF routing table and send message
2776 * to zebra, if appropriate.
2778 static int uninstall_evpn_route_entry_in_vrf(struct bgp
*bgp_vrf
,
2779 const struct prefix_evpn
*evp
,
2780 struct bgp_path_info
*parent_pi
)
2782 struct bgp_dest
*dest
;
2783 struct bgp_path_info
*pi
;
2786 struct prefix
*pp
= &p
;
2790 memset(pp
, 0, sizeof(struct prefix
));
2791 ip_prefix_from_evpn_prefix(evp
, pp
);
2793 if (bgp_debug_zebra(NULL
))
2795 "vrf %s: unimport evpn prefix %pFX parent %p flags 0x%x",
2796 vrf_id_to_name(bgp_vrf
->vrf_id
), evp
, parent_pi
,
2799 /* Locate route within the VRF. */
2800 /* NOTE: There is no RD here. */
2801 if (is_evpn_prefix_ipaddr_v4(evp
)) {
2803 safi
= SAFI_UNICAST
;
2804 dest
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2807 safi
= SAFI_UNICAST
;
2808 dest
= bgp_node_lookup(bgp_vrf
->rib
[afi
][safi
], pp
);
2814 /* Find matching route entry. */
2815 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2817 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2821 bgp_dest_unlock_node(dest
);
2825 if (bgp_debug_zebra(NULL
))
2826 zlog_debug("... delete dest %p (l %d) pi %p (l %d, f 0x%x)",
2827 dest
, bgp_dest_get_lock_count(dest
), pi
, pi
->lock
,
2830 /* Process for route leaking. */
2831 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp_vrf
, pi
);
2833 bgp_aggregate_decrement(bgp_vrf
, bgp_dest_get_prefix(dest
), pi
, afi
,
2836 /* Mark entry for deletion */
2837 bgp_path_info_delete(dest
, pi
);
2839 /* Unlink path to evpn nexthop */
2840 bgp_evpn_path_nh_del(bgp_vrf
, pi
);
2842 /* Perform route selection and update zebra, if required. */
2843 bgp_process(bgp_vrf
, dest
, afi
, safi
);
2845 /* Unlock route node. */
2846 bgp_dest_unlock_node(dest
);
2852 * Uninstall route entry from the VNI routing table and send message
2853 * to zebra, if appropriate.
2855 static int uninstall_evpn_route_entry(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2856 const struct prefix_evpn
*p
,
2857 struct bgp_path_info
*parent_pi
)
2859 struct bgp_dest
*dest
;
2860 struct bgp_path_info
*pi
;
2861 struct bgp_path_info
*local_pi
;
2863 struct prefix_evpn ad_evp
;
2865 /* EAD prefix in the global table doesn't include the VTEP-IP so
2866 * we need to create a different copy for the VNI
2868 if (p
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
2869 p
= evpn_type1_prefix_vni_copy(&ad_evp
, p
,
2870 parent_pi
->attr
->nexthop
);
2872 /* Locate route within the VNI. */
2873 /* NOTE: There is no RD here. */
2874 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)p
);
2878 /* Find matching route entry. */
2879 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
2881 && (struct bgp_path_info
*)pi
->extra
->parent
== parent_pi
)
2885 bgp_dest_unlock_node(dest
);
2889 bgp_evpn_remote_ip_hash_del(vpn
, pi
);
2891 /* Mark entry for deletion */
2892 bgp_path_info_delete(dest
, pi
);
2894 /* Perform route selection and update zebra, if required. */
2895 ret
= evpn_route_select_install(bgp
, vpn
, dest
);
2897 /* if the best path is a local path with a non-zero ES
2898 * sync info against the local path may need to be updated
2899 * when a remote path is deleted
2901 local_pi
= bgp_evpn_route_get_local_path(bgp
, dest
);
2902 if (local_pi
&& bgp_evpn_attr_is_local_es(local_pi
->attr
))
2903 bgp_evpn_update_type2_route_entry(bgp
, vpn
, dest
, local_pi
,
2906 /* Unlock route node. */
2907 bgp_dest_unlock_node(dest
);
2913 * Given a route entry and a VRF, see if this route entry should be
2914 * imported into the VRF i.e., RTs match.
2916 static int is_route_matching_for_vrf(struct bgp
*bgp_vrf
,
2917 struct bgp_path_info
*pi
)
2919 struct attr
*attr
= pi
->attr
;
2920 struct ecommunity
*ecom
;
2924 /* Route should have valid RT to be even considered. */
2925 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2928 ecom
= bgp_attr_get_ecommunity(attr
);
2929 if (!ecom
|| !ecom
->size
)
2932 /* For each extended community RT, see if it matches this VNI. If any RT
2933 * matches, we're done.
2935 for (i
= 0; i
< ecom
->size
; i
++) {
2937 uint8_t type
, sub_type
;
2938 struct ecommunity_val
*eval
;
2939 struct ecommunity_val eval_tmp
;
2940 struct vrf_irt_node
*irt
;
2942 /* Only deal with RTs */
2943 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
2944 eval
= (struct ecommunity_val
*)(ecom
->val
2945 + (i
* ecom
->unit_size
));
2948 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
2951 /* See if this RT matches specified VNIs import RTs */
2952 irt
= lookup_vrf_import_rt(eval
);
2954 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2957 /* Also check for non-exact match. In this, we mask out the AS
2959 * only check on the local-admin sub-field. This is to
2961 * VNI as the RT for EBGP peering too.
2964 if (type
== ECOMMUNITY_ENCODE_AS
2965 || type
== ECOMMUNITY_ENCODE_AS4
2966 || type
== ECOMMUNITY_ENCODE_IP
) {
2967 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
2968 mask_ecom_global_admin(&eval_tmp
, eval
);
2969 irt
= lookup_vrf_import_rt(&eval_tmp
);
2972 if (is_vrf_present_in_irt_vrfs(irt
->vrfs
, bgp_vrf
))
2980 * Given a route entry and a VNI, see if this route entry should be
2981 * imported into the VNI i.e., RTs match.
2983 static int is_route_matching_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
,
2984 struct bgp_path_info
*pi
)
2986 struct attr
*attr
= pi
->attr
;
2987 struct ecommunity
*ecom
;
2991 /* Route should have valid RT to be even considered. */
2992 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
2995 ecom
= bgp_attr_get_ecommunity(attr
);
2996 if (!ecom
|| !ecom
->size
)
2999 /* For each extended community RT, see if it matches this VNI. If any RT
3000 * matches, we're done.
3002 for (i
= 0; i
< ecom
->size
; i
++) {
3004 uint8_t type
, sub_type
;
3005 struct ecommunity_val
*eval
;
3006 struct ecommunity_val eval_tmp
;
3007 struct irt_node
*irt
;
3009 /* Only deal with RTs */
3010 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
3011 eval
= (struct ecommunity_val
*)(ecom
->val
3012 + (i
* ecom
->unit_size
));
3015 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3018 /* See if this RT matches specified VNIs import RTs */
3019 irt
= lookup_import_rt(bgp
, eval
);
3021 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
3024 /* Also check for non-exact match. In this, we mask out the AS
3026 * only check on the local-admin sub-field. This is to
3028 * VNI as the RT for EBGP peering too.
3031 if (type
== ECOMMUNITY_ENCODE_AS
3032 || type
== ECOMMUNITY_ENCODE_AS4
3033 || type
== ECOMMUNITY_ENCODE_IP
) {
3034 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
3035 mask_ecom_global_admin(&eval_tmp
, eval
);
3036 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3039 if (is_vni_present_in_irt_vnis(irt
->vnis
, vpn
))
3046 /* This API will scan evpn routes for checking attribute's rmac
3047 * macthes with bgp instance router mac. It avoid installing
3048 * route into bgp vrf table and remote rmac in bridge table.
3050 static int bgp_evpn_route_rmac_self_check(struct bgp
*bgp_vrf
,
3051 const struct prefix_evpn
*evp
,
3052 struct bgp_path_info
*pi
)
3054 /* evpn route could have learnt prior to L3vni has come up,
3055 * perform rmac check before installing route and
3056 * remote router mac.
3057 * The route will be removed from global bgp table once
3058 * SVI comes up with MAC and stored in hash, triggers
3059 * bgp_mac_rescan_all_evpn_tables.
3061 if (memcmp(&bgp_vrf
->rmac
, &pi
->attr
->rmac
, ETH_ALEN
) == 0) {
3062 if (bgp_debug_update(pi
->peer
, NULL
, NULL
, 1)) {
3063 char attr_str
[BUFSIZ
] = {0};
3065 bgp_dump_attr(pi
->attr
, attr_str
, sizeof(attr_str
));
3068 "%s: bgp %u prefix %pFX with attr %s - DENIED due to self mac",
3069 __func__
, bgp_vrf
->vrf_id
, evp
, attr_str
);
3078 /* don't import hosts that are locally attached */
3080 bgp_evpn_skip_vrf_import_of_local_es(struct bgp
*bgp_vrf
,
3081 const struct prefix_evpn
*evp
,
3082 struct bgp_path_info
*pi
, int install
)
3086 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3087 esi
= bgp_evpn_attr_get_esi(pi
->attr
);
3089 /* Don't import routes that point to a local destination */
3090 if (bgp_evpn_attr_is_local_es(pi
->attr
)) {
3091 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
)) {
3092 char esi_buf
[ESI_STR_LEN
];
3095 "vrf %s of evpn prefix %pFX skipped, local es %s",
3096 install
? "import" : "unimport", evp
,
3097 esi_to_str(esi
, esi_buf
,
3107 * Install or uninstall a mac-ip route in the provided vrf if
3108 * there is a rt match
3110 int bgp_evpn_route_entry_install_if_vrf_match(struct bgp
*bgp_vrf
,
3111 struct bgp_path_info
*pi
,
3115 const struct prefix_evpn
*evp
=
3116 (const struct prefix_evpn
*)bgp_dest_get_prefix(pi
->net
);
3118 /* Consider "valid" remote routes applicable for
3121 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3122 && pi
->type
== ZEBRA_ROUTE_BGP
3123 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3126 if (is_route_matching_for_vrf(bgp_vrf
, pi
)) {
3127 if (bgp_evpn_route_rmac_self_check(bgp_vrf
, evp
, pi
))
3130 /* don't import hosts that are locally attached */
3131 if (install
&& bgp_evpn_skip_vrf_import_of_local_es(
3132 bgp_vrf
, evp
, pi
, install
))
3136 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3138 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3142 flog_err(EC_BGP_EVPN_FAIL
,
3143 "Failed to %s EVPN %pFX route in VRF %s",
3144 install
? "install" : "uninstall", evp
,
3145 vrf_id_to_name(bgp_vrf
->vrf_id
));
3152 * Install or uninstall mac-ip routes are appropriate for this
3155 static int install_uninstall_routes_for_vrf(struct bgp
*bgp_vrf
, int install
)
3159 struct bgp_dest
*rd_dest
, *dest
;
3160 struct bgp_table
*table
;
3161 struct bgp_path_info
*pi
;
3163 struct bgp
*bgp_evpn
= NULL
;
3167 bgp_evpn
= bgp_get_evpn();
3171 /* Walk entire global routing table and evaluate routes which could be
3172 * imported into this VRF. Note that we need to loop through all global
3173 * routes to determine which route matches the import rt on vrf
3175 for (rd_dest
= bgp_table_top(bgp_evpn
->rib
[afi
][safi
]); rd_dest
;
3176 rd_dest
= bgp_route_next(rd_dest
)) {
3177 table
= bgp_dest_get_bgp_table_info(rd_dest
);
3181 for (dest
= bgp_table_top(table
); dest
;
3182 dest
= bgp_route_next(dest
)) {
3183 const struct prefix_evpn
*evp
=
3184 (const struct prefix_evpn
*)bgp_dest_get_prefix(
3187 /* if not mac-ip route skip this route */
3188 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3189 || evp
->prefix
.route_type
3190 == BGP_EVPN_IP_PREFIX_ROUTE
))
3193 /* if not a mac+ip route skip this route */
3194 if (!(is_evpn_prefix_ipaddr_v4(evp
)
3195 || is_evpn_prefix_ipaddr_v6(evp
)))
3198 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
3200 ret
= bgp_evpn_route_entry_install_if_vrf_match(
3201 bgp_vrf
, pi
, install
);
3212 * Install or uninstall routes of specified type that are appropriate for this
3215 static int install_uninstall_routes_for_vni(struct bgp
*bgp
,
3216 struct bgpevpn
*vpn
,
3217 bgp_evpn_route_type rtype
,
3222 struct bgp_dest
*rd_dest
, *dest
;
3223 struct bgp_table
*table
;
3224 struct bgp_path_info
*pi
;
3230 /* Walk entire global routing table and evaluate routes which could be
3231 * imported into this VPN. Note that we cannot just look at the routes
3233 * the VNI's RD - remote routes applicable for this VNI could have any
3236 /* EVPN routes are a 2-level table. */
3237 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
3238 rd_dest
= bgp_route_next(rd_dest
)) {
3239 table
= bgp_dest_get_bgp_table_info(rd_dest
);
3243 for (dest
= bgp_table_top(table
); dest
;
3244 dest
= bgp_route_next(dest
)) {
3245 const struct prefix_evpn
*evp
=
3246 (const struct prefix_evpn
*)bgp_dest_get_prefix(
3249 if (evp
->prefix
.route_type
!= rtype
)
3252 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
3254 /* Consider "valid" remote routes applicable for
3256 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
3257 && pi
->type
== ZEBRA_ROUTE_BGP
3258 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
3261 if (is_route_matching_for_vni(bgp
, vpn
, pi
)) {
3263 ret
= install_evpn_route_entry(
3266 ret
= uninstall_evpn_route_entry(
3272 "%u: Failed to %s EVPN %s route in VNI %u",
3276 rtype
== BGP_EVPN_MAC_IP_ROUTE
3281 bgp_dest_unlock_node(rd_dest
);
3282 bgp_dest_unlock_node(dest
);
3293 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3294 * is invoked upon l3vni-add or l3vni import rt change
3296 static int install_routes_for_vrf(struct bgp
*bgp_vrf
)
3298 install_uninstall_routes_for_vrf(bgp_vrf
, 1);
3303 * Install any existing remote routes applicable for this VNI into its
3304 * routing table. This is invoked when a VNI becomes "live" or its Import
3307 static int install_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3311 /* Install type-3 routes followed by type-2 routes - the ones applicable
3314 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3319 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_AD_ROUTE
,
3324 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3328 /* uninstall routes from l3vni vrf. */
3329 static int uninstall_routes_for_vrf(struct bgp
*bgp_vrf
)
3331 install_uninstall_routes_for_vrf(bgp_vrf
, 0);
3336 * Uninstall any existing remote routes for this VNI. One scenario in which
3337 * this is invoked is upon an import RT change.
3339 static int uninstall_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3343 /* Uninstall type-2 routes followed by type-3 routes - the ones
3347 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_MAC_IP_ROUTE
,
3352 ret
= install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_AD_ROUTE
,
3358 return install_uninstall_routes_for_vni(bgp
, vpn
, BGP_EVPN_IMET_ROUTE
,
3363 * Install or uninstall route in matching VRFs (list).
3365 static int install_uninstall_route_in_vrfs(struct bgp
*bgp_def
, afi_t afi
,
3366 safi_t safi
, struct prefix_evpn
*evp
,
3367 struct bgp_path_info
*pi
,
3368 struct list
*vrfs
, int install
)
3370 struct bgp
*bgp_vrf
;
3371 struct listnode
*node
, *nnode
;
3373 /* Only type-2/type-5 routes go into a VRF */
3374 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3375 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3378 /* if it is type-2 route and not a mac+ip route skip this route */
3379 if ((evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3380 && !(is_evpn_prefix_ipaddr_v4(evp
)
3381 || is_evpn_prefix_ipaddr_v6(evp
)))
3384 for (ALL_LIST_ELEMENTS(vrfs
, node
, nnode
, bgp_vrf
)) {
3387 /* don't import hosts that are locally attached */
3388 if (install
&& bgp_evpn_skip_vrf_import_of_local_es(
3389 bgp_vrf
, evp
, pi
, install
))
3393 ret
= install_evpn_route_entry_in_vrf(bgp_vrf
, evp
, pi
);
3395 ret
= uninstall_evpn_route_entry_in_vrf(bgp_vrf
, evp
,
3399 flog_err(EC_BGP_EVPN_FAIL
,
3400 "%u: Failed to %s prefix %pFX in VRF %s",
3402 install
? "install" : "uninstall", evp
,
3403 vrf_id_to_name(bgp_vrf
->vrf_id
));
3412 * Install or uninstall route in matching VNIs (list).
3414 static int install_uninstall_route_in_vnis(struct bgp
*bgp
, afi_t afi
,
3415 safi_t safi
, struct prefix_evpn
*evp
,
3416 struct bgp_path_info
*pi
,
3417 struct list
*vnis
, int install
)
3419 struct bgpevpn
*vpn
;
3420 struct listnode
*node
, *nnode
;
3422 for (ALL_LIST_ELEMENTS(vnis
, node
, nnode
, vpn
)) {
3425 if (!is_vni_live(vpn
))
3429 ret
= install_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3431 ret
= uninstall_evpn_route_entry(bgp
, vpn
, evp
, pi
);
3434 flog_err(EC_BGP_EVPN_FAIL
,
3435 "%u: Failed to %s EVPN %s route in VNI %u",
3436 bgp
->vrf_id
, install
? "install" : "uninstall",
3437 evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3449 * Install or uninstall route for appropriate VNIs/ESIs.
3451 static int bgp_evpn_install_uninstall_table(struct bgp
*bgp
, afi_t afi
,
3452 safi_t safi
, const struct prefix
*p
,
3453 struct bgp_path_info
*pi
,
3454 int import
, bool in_vni_rt
,
3457 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
3458 struct attr
*attr
= pi
->attr
;
3459 struct ecommunity
*ecom
;
3461 struct prefix_evpn ad_evp
;
3465 /* Only type-1, type-2, type-3, type-4 and type-5
3466 * are supported currently
3468 if (!(evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
3469 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
3470 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
3471 || evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
3472 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
3475 /* If we don't have Route Target, nothing much to do. */
3476 if (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)))
3479 /* EAD prefix in the global table doesn't include the VTEP-IP so
3480 * we need to create a different copy for the VNI
3482 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
3483 evp
= evpn_type1_prefix_vni_copy(&ad_evp
, evp
, attr
->nexthop
);
3485 ecom
= bgp_attr_get_ecommunity(attr
);
3486 if (!ecom
|| !ecom
->size
)
3489 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3490 * attached to the route */
3491 for (i
= 0; i
< ecom
->size
; i
++) {
3493 uint8_t type
, sub_type
;
3494 struct ecommunity_val
*eval
;
3495 struct ecommunity_val eval_tmp
;
3496 struct irt_node
*irt
; /* import rt for l2vni */
3497 struct vrf_irt_node
*vrf_irt
; /* import rt for l3vni */
3498 struct bgp_evpn_es
*es
;
3500 /* Only deal with RTs */
3501 pnt
= (ecom
->val
+ (i
* ecom
->unit_size
));
3502 eval
= (struct ecommunity_val
*)(ecom
->val
3503 + (i
* ecom
->unit_size
));
3506 if (sub_type
!= ECOMMUNITY_ROUTE_TARGET
)
3509 /* non-local MAC-IP routes in the global route table are linked
3510 * to the destination ES
3512 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
3513 bgp_evpn_path_es_link(pi
, 0,
3514 bgp_evpn_attr_get_esi(pi
->attr
));
3517 * macip routes (type-2) are imported into VNI and VRF tables.
3518 * IMET route is imported into VNI table.
3519 * prefix routes are imported into VRF table.
3521 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
||
3522 evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
||
3523 evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
||
3524 evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
3526 irt
= in_vni_rt
? lookup_import_rt(bgp
, eval
) : NULL
;
3528 install_uninstall_route_in_vnis(
3529 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3532 vrf_irt
= in_vrf_rt
? lookup_vrf_import_rt(eval
) : NULL
;
3534 install_uninstall_route_in_vrfs(
3535 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3538 /* Also check for non-exact match.
3539 * In this, we mask out the AS and
3540 * only check on the local-admin sub-field.
3541 * This is to facilitate using
3542 * VNI as the RT for EBGP peering too.
3546 if (type
== ECOMMUNITY_ENCODE_AS
3547 || type
== ECOMMUNITY_ENCODE_AS4
3548 || type
== ECOMMUNITY_ENCODE_IP
) {
3549 memcpy(&eval_tmp
, eval
, ecom
->unit_size
);
3550 mask_ecom_global_admin(&eval_tmp
, eval
);
3552 irt
= lookup_import_rt(bgp
, &eval_tmp
);
3555 lookup_vrf_import_rt(&eval_tmp
);
3559 install_uninstall_route_in_vnis(
3560 bgp
, afi
, safi
, evp
, pi
, irt
->vnis
,
3563 install_uninstall_route_in_vrfs(
3564 bgp
, afi
, safi
, evp
, pi
, vrf_irt
->vrfs
,
3568 /* es route is imported into the es table */
3569 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) {
3571 /* we will match based on the entire esi to avoid
3572 * import of an es route for esi2 into esi1
3574 es
= bgp_evpn_es_find(&evp
->prefix
.es_addr
.esi
);
3575 if (es
&& bgp_evpn_is_es_local(es
))
3576 bgp_evpn_es_route_install_uninstall(
3577 bgp
, es
, afi
, safi
, evp
, pi
, import
);
3585 * Install or uninstall route for appropriate VNIs/ESIs.
3587 static int install_uninstall_evpn_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
3588 const struct prefix
*p
,
3589 struct bgp_path_info
*pi
, int import
)
3591 return bgp_evpn_install_uninstall_table(bgp
, afi
, safi
, p
, pi
, import
,
3595 void bgp_evpn_import_type2_route(struct bgp_path_info
*pi
, int import
)
3597 struct bgp
*bgp_evpn
;
3599 bgp_evpn
= bgp_get_evpn();
3603 install_uninstall_evpn_route(bgp_evpn
, AFI_L2VPN
, SAFI_EVPN
,
3604 &pi
->net
->p
, pi
, import
);
3608 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3611 static void delete_withdraw_vrf_routes(struct bgp
*bgp_vrf
)
3613 /* Delete ipv4 default route and withdraw from peers */
3614 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3615 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3616 SAFI_UNICAST
, false);
3618 /* delete all ipv4 routes and withdraw from peers */
3619 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3620 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3622 /* Delete ipv6 default route and withdraw from peers */
3623 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3624 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3625 SAFI_UNICAST
, false);
3627 /* delete all ipv6 routes and withdraw from peers */
3628 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3629 bgp_evpn_withdraw_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3633 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3636 void update_advertise_vrf_routes(struct bgp
*bgp_vrf
)
3638 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
3640 bgp_evpn
= bgp_get_evpn();
3644 /* update all ipv4 routes */
3645 if (advertise_type5_routes(bgp_vrf
, AFI_IP
))
3646 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP
, SAFI_UNICAST
);
3648 /* update ipv4 default route and withdraw from peers */
3649 if (evpn_default_originate_set(bgp_vrf
, AFI_IP
, SAFI_UNICAST
))
3650 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP
,
3651 SAFI_UNICAST
, true);
3653 /* update all ipv6 routes */
3654 if (advertise_type5_routes(bgp_vrf
, AFI_IP6
))
3655 bgp_evpn_advertise_type5_routes(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
);
3657 /* update ipv6 default route and withdraw from peers */
3658 if (evpn_default_originate_set(bgp_vrf
, AFI_IP6
, SAFI_UNICAST
))
3659 bgp_evpn_install_uninstall_default_route(bgp_vrf
, AFI_IP6
,
3660 SAFI_UNICAST
, true);
3665 * update and advertise local routes for a VRF as type-5 routes.
3666 * This is invoked upon RD change for a VRF. Note taht the processing is only
3667 * done in the global route table using the routes which already exist in the
3670 static void update_router_id_vrf(struct bgp
*bgp_vrf
)
3672 /* skip if the RD is configured */
3673 if (is_vrf_rd_configured(bgp_vrf
))
3676 /* derive the RD for the VRF based on new router-id */
3677 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
3679 /* update advertise ipv4|ipv6 routes as type-5 routes */
3680 update_advertise_vrf_routes(bgp_vrf
);
3684 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3685 * This is invoked upon VRF RD change. The processing is done only from global
3688 static void withdraw_router_id_vrf(struct bgp
*bgp_vrf
)
3690 /* skip if the RD is configured */
3691 if (is_vrf_rd_configured(bgp_vrf
))
3694 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3695 delete_withdraw_vrf_routes(bgp_vrf
);
3699 * Update and advertise local routes for a VNI. Invoked upon router-id
3700 * change. Note that the processing is done only on the global route table
3701 * using routes that already exist in the per-VNI table.
3703 static int update_advertise_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3705 struct prefix_evpn p
;
3706 struct bgp_dest
*dest
, *global_dest
;
3707 struct bgp_path_info
*pi
, *global_pi
;
3709 afi_t afi
= AFI_L2VPN
;
3710 safi_t safi
= SAFI_EVPN
;
3712 /* Locate type-3 route for VNI in the per-VNI table and use its
3713 * attributes to create and advertise the type-3 route for this VNI
3714 * in the global table.
3716 * RT-3 only if doing head-end replication
3718 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3719 == VXLAN_FLOOD_HEAD_END_REPL
) {
3720 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3721 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
3722 if (!dest
) /* unexpected */
3724 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
3725 if (pi
->peer
== bgp
->peer_self
&&
3726 pi
->type
== ZEBRA_ROUTE_BGP
3727 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3730 bgp_dest_unlock_node(dest
);
3735 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
],
3736 afi
, safi
, &p
, &vpn
->prd
);
3737 update_evpn_route_entry(bgp
, vpn
, afi
, safi
, global_dest
, attr
,
3738 1, &pi
, 0, mac_mobility_seqnum(attr
),
3739 false /* setup_sync */, NULL
/* old_is_sync */);
3741 /* Schedule for processing and unlock node. */
3742 bgp_process(bgp
, global_dest
, afi
, safi
);
3743 bgp_dest_unlock_node(global_dest
);
3746 /* Now, walk this VNI's route table and use the route and its attribute
3747 * to create and schedule route in global table.
3749 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
3750 dest
= bgp_route_next(dest
)) {
3751 const struct prefix_evpn
*evp
=
3752 (const struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
3755 * We have already processed type-3 routes.
3756 * Process only type-1 and type-2 routes here.
3758 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
3759 && evp
->prefix
.route_type
!= BGP_EVPN_AD_ROUTE
)
3762 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
3763 if (pi
->peer
== bgp
->peer_self
3764 && pi
->type
== ZEBRA_ROUTE_BGP
3765 && pi
->sub_type
== BGP_ROUTE_STATIC
)
3770 /* Create route in global routing table using this route entry's
3774 global_dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
3776 assert(global_dest
);
3778 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
) {
3780 update_evpn_route_entry(
3781 bgp
, vpn
, afi
, safi
, global_dest
, attr
, 1,
3782 &global_pi
, 0, mac_mobility_seqnum(attr
),
3783 false /* setup_sync */, NULL
/* old_is_sync */);
3786 struct bgp_evpn_es
*es
;
3787 int route_changed
= 0;
3789 es
= bgp_evpn_es_find(&evp
->prefix
.ead_addr
.esi
);
3790 bgp_evpn_mh_route_update(bgp
, es
, vpn
, afi
, safi
,
3791 global_dest
, attr
, &global_pi
,
3795 /* Schedule for processing and unlock node. */
3796 bgp_process(bgp
, global_dest
, afi
, safi
);
3797 bgp_dest_unlock_node(global_dest
);
3804 * Delete (and withdraw) local routes for a VNI - only from the global
3805 * table. Invoked upon router-id change.
3807 static int delete_withdraw_vni_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
3809 struct prefix_evpn p
;
3810 struct bgp_dest
*global_dest
;
3811 struct bgp_path_info
*pi
;
3812 afi_t afi
= AFI_L2VPN
;
3813 safi_t safi
= SAFI_EVPN
;
3815 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3816 * for this VNI - from the global table.
3818 delete_global_type2_routes(bgp
, vpn
);
3820 /* Remove type-3 route for this VNI from global table. */
3821 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3822 global_dest
= bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
3823 (const struct prefix_evpn
*)&p
, &vpn
->prd
);
3825 /* Delete route entry in the global EVPN table. */
3826 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
3828 /* Schedule for processing - withdraws to peers happen from
3832 bgp_process(bgp
, global_dest
, afi
, safi
);
3833 bgp_dest_unlock_node(global_dest
);
3837 delete_global_ead_evi_routes(bgp
, vpn
);
3842 * Handle router-id change. Update and advertise local routes corresponding
3843 * to this VNI from peers. Note that this is invoked after updating the
3844 * router-id. The routes in the per-VNI table are used to create routes in
3845 * the global table and schedule them.
3847 static void update_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3849 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3851 /* Skip VNIs with configured RD. */
3852 if (is_rd_configured(vpn
))
3855 bgp_evpn_derive_auto_rd(bgp
, vpn
);
3856 update_advertise_vni_routes(bgp
, vpn
);
3860 * Handle router-id change. Delete and withdraw local routes corresponding
3861 * to this VNI from peers. Note that this is invoked prior to updating
3862 * the router-id and is done only on the global route table, the routes
3863 * are needed in the per-VNI table to re-advertise with new router id.
3865 static void withdraw_router_id_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
3867 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3869 /* Skip VNIs with configured RD. */
3870 if (is_rd_configured(vpn
))
3873 delete_withdraw_vni_routes(bgp
, vpn
);
3877 * Create RT-3 for a VNI and schedule for processing and advertisement.
3878 * This is invoked upon flooding mode changing to head-end replication.
3880 static void create_advertise_type3(struct hash_bucket
*bucket
, void *data
)
3882 struct bgpevpn
*vpn
= bucket
->data
;
3883 struct bgp
*bgp
= data
;
3884 struct prefix_evpn p
;
3886 if (!vpn
|| !is_vni_live(vpn
) ||
3887 bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
3888 != VXLAN_FLOOD_HEAD_END_REPL
)
3891 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3892 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
))
3893 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3894 "Type3 route creation failure for VNI %u", vpn
->vni
);
3898 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3899 * This is invoked upon flooding mode changing to drop BUM packets.
3901 static void delete_withdraw_type3(struct hash_bucket
*bucket
, void *data
)
3903 struct bgpevpn
*vpn
= bucket
->data
;
3904 struct bgp
*bgp
= data
;
3905 struct prefix_evpn p
;
3907 if (!vpn
|| !is_vni_live(vpn
))
3910 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
3911 delete_evpn_route(bgp
, vpn
, &p
);
3915 * Process received EVPN type-2 route (advertise or withdraw).
3917 static int process_type2_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
3918 struct attr
*attr
, uint8_t *pfx
, int psize
,
3919 uint32_t addpath_id
)
3921 struct prefix_rd prd
;
3922 struct prefix_evpn p
= {};
3923 struct bgp_route_evpn evpn
= {};
3925 uint8_t macaddr_len
;
3926 /* holds the VNI(s) as in packet */
3927 mpls_label_t label
[BGP_MAX_LABELS
] = {};
3928 uint32_t num_labels
= 0;
3932 /* Type-2 route should be either 33, 37 or 49 bytes or an
3933 * additional 3 bytes if there is a second label (VNI):
3934 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3935 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3936 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3938 if (psize
!= 33 && psize
!= 37 && psize
!= 49 && psize
!= 36
3939 && psize
!= 40 && psize
!= 52) {
3940 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
3941 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3942 peer
->bgp
->vrf_id
, peer
->host
, psize
);
3946 struct stream
*pkt
= stream_new(psize
);
3947 stream_put(pkt
, pfx
, psize
);
3949 /* Make prefix_rd */
3950 prd
.family
= AF_UNSPEC
;
3953 STREAM_GET(&prd
.val
, pkt
, 8);
3955 /* Make EVPN prefix. */
3957 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
3958 p
.prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
3960 /* Copy Ethernet Seg Identifier */
3962 STREAM_GET(&attr
->esi
, pkt
, sizeof(esi_t
));
3964 if (bgp_evpn_is_esi_local_and_non_bypass(&attr
->esi
))
3965 attr
->es_flags
|= ATTR_ES_IS_LOCAL
;
3967 attr
->es_flags
&= ~ATTR_ES_IS_LOCAL
;
3969 STREAM_FORWARD_GETP(pkt
, sizeof(esi_t
));
3972 /* Copy Ethernet Tag */
3973 STREAM_GET(ð_tag
, pkt
, 4);
3974 p
.prefix
.macip_addr
.eth_tag
= ntohl(eth_tag
);
3976 /* Get the MAC Addr len */
3977 STREAM_GETC(pkt
, macaddr_len
);
3979 /* Get the MAC Addr */
3980 if (macaddr_len
== (ETH_ALEN
* 8)) {
3981 STREAM_GET(&p
.prefix
.macip_addr
.mac
.octet
, pkt
, ETH_ALEN
);
3984 EC_BGP_EVPN_ROUTE_INVALID
,
3985 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3986 peer
->bgp
->vrf_id
, peer
->host
, macaddr_len
);
3992 STREAM_GETC(pkt
, ipaddr_len
);
3994 if (ipaddr_len
!= 0 && ipaddr_len
!= IPV4_MAX_BITLEN
3995 && ipaddr_len
!= IPV6_MAX_BITLEN
) {
3997 EC_BGP_EVPN_ROUTE_INVALID
,
3998 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3999 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4004 ipaddr_len
/= 8; /* Convert to bytes. */
4005 p
.prefix
.macip_addr
.ip
.ipa_type
= (ipaddr_len
== IPV4_MAX_BYTELEN
)
4008 STREAM_GET(&p
.prefix
.macip_addr
.ip
.ip
.addr
, pkt
, ipaddr_len
);
4011 /* Get the VNI(s). Stored as bytes here. */
4012 STREAM_GET(&label
[0], pkt
, BGP_LABEL_BYTES
);
4015 /* Do we have a second VNI? */
4016 if (STREAM_READABLE(pkt
)) {
4018 STREAM_GET(&label
[1], pkt
, BGP_LABEL_BYTES
);
4021 /* Process the route. */
4023 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4024 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4025 &prd
, &label
[0], num_labels
, 0, &evpn
);
4027 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4028 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4029 &prd
, &label
[0], num_labels
, &evpn
);
4034 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4035 "%u:%s - Rx EVPN Type-2 NLRI - corrupt, discarding",
4036 peer
->bgp
->vrf_id
, peer
->host
);
4044 * Process received EVPN type-3 route (advertise or withdraw).
4046 static int process_type3_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4047 struct attr
*attr
, uint8_t *pfx
, int psize
,
4048 uint32_t addpath_id
)
4050 struct prefix_rd prd
;
4051 struct prefix_evpn p
;
4056 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
4057 * IP len (1) and IP (4 or 16).
4059 if (psize
!= 17 && psize
!= 29) {
4060 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4061 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
4062 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4066 /* If PMSI is present, log if it is anything other than IR.
4067 * Note: We just simply ignore the values as it is not clear if
4068 * doing anything else is better.
4071 (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
))) {
4072 enum pta_type pmsi_tnl_type
= bgp_attr_get_pmsi_tnl_type(attr
);
4074 if (pmsi_tnl_type
!= PMSI_TNLTYPE_INGR_REPL
4075 && pmsi_tnl_type
!= PMSI_TNLTYPE_PIM_SM
) {
4077 EC_BGP_EVPN_PMSI_PRESENT
,
4078 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
4079 peer
->bgp
->vrf_id
, peer
->host
, pmsi_tnl_type
);
4083 /* Make prefix_rd */
4084 prd
.family
= AF_UNSPEC
;
4086 memcpy(&prd
.val
, pfx
, 8);
4089 /* Make EVPN prefix. */
4090 memset(&p
, 0, sizeof(p
));
4092 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4093 p
.prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
4095 /* Copy Ethernet Tag */
4096 memcpy(ð_tag
, pfx
, 4);
4097 p
.prefix
.imet_addr
.eth_tag
= ntohl(eth_tag
);
4101 ipaddr_len
= *pfx
++;
4102 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
4103 p
.prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
4104 memcpy(&p
.prefix
.imet_addr
.ip
.ip
.addr
, pfx
, IPV4_MAX_BYTELEN
);
4107 EC_BGP_EVPN_ROUTE_INVALID
,
4108 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
4109 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
4113 /* Process the route. */
4115 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4116 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4117 &prd
, NULL
, 0, 0, NULL
);
4119 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4120 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4121 &prd
, NULL
, 0, NULL
);
4126 * Process received EVPN type-5 route (advertise or withdraw).
4128 static int process_type5_route(struct peer
*peer
, afi_t afi
, safi_t safi
,
4129 struct attr
*attr
, uint8_t *pfx
, int psize
,
4130 uint32_t addpath_id
)
4132 struct prefix_rd prd
;
4133 struct prefix_evpn p
;
4134 struct bgp_route_evpn evpn
;
4137 mpls_label_t label
; /* holds the VNI as in the packet */
4139 bool is_valid_update
= true;
4141 /* Type-5 route should be 34 or 58 bytes:
4142 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
4143 * GW (4 or 16) and VNI (3).
4144 * Note that the IP and GW should both be IPv4 or both IPv6.
4146 if (psize
!= 34 && psize
!= 58) {
4147 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4148 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
4149 peer
->bgp
->vrf_id
, peer
->host
, psize
);
4153 /* Make prefix_rd */
4154 prd
.family
= AF_UNSPEC
;
4156 memcpy(&prd
.val
, pfx
, 8);
4159 /* Make EVPN prefix. */
4160 memset(&p
, 0, sizeof(p
));
4162 p
.prefixlen
= EVPN_ROUTE_PREFIXLEN
;
4163 p
.prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
4165 /* Additional information outside of prefix - ESI and GW IP */
4166 memset(&evpn
, 0, sizeof(evpn
));
4168 /* Fetch ESI overlay index */
4170 memcpy(&evpn
.eth_s_id
, pfx
, sizeof(esi_t
));
4173 /* Fetch Ethernet Tag. */
4174 memcpy(ð_tag
, pfx
, 4);
4175 p
.prefix
.prefix_addr
.eth_tag
= ntohl(eth_tag
);
4178 /* Fetch IP prefix length. */
4180 if (ippfx_len
> IPV6_MAX_BITLEN
) {
4182 EC_BGP_EVPN_ROUTE_INVALID
,
4183 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4184 peer
->bgp
->vrf_id
, peer
->host
, ippfx_len
);
4187 p
.prefix
.prefix_addr
.ip_prefix_length
= ippfx_len
;
4189 /* Determine IPv4 or IPv6 prefix */
4190 /* Since the address and GW are from the same family, this just becomes
4191 * a simple check on the total size.
4194 SET_IPADDR_V4(&p
.prefix
.prefix_addr
.ip
);
4195 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v4
, pfx
, 4);
4197 SET_IPADDR_V4(&evpn
.gw_ip
);
4198 memcpy(&evpn
.gw_ip
.ipaddr_v4
, pfx
, 4);
4201 SET_IPADDR_V6(&p
.prefix
.prefix_addr
.ip
);
4202 memcpy(&p
.prefix
.prefix_addr
.ip
.ipaddr_v6
, pfx
,
4204 pfx
+= IPV6_MAX_BYTELEN
;
4205 SET_IPADDR_V6(&evpn
.gw_ip
);
4206 memcpy(&evpn
.gw_ip
.ipaddr_v6
, pfx
, IPV6_MAX_BYTELEN
);
4207 pfx
+= IPV6_MAX_BYTELEN
;
4210 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4211 memset(&label
, 0, sizeof(label
));
4212 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
4215 * If in future, we are required to access additional fields,
4216 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4221 * An update containing a non-zero gateway IP and a non-zero ESI
4222 * at the same time is should be treated as withdraw
4224 if (bgp_evpn_is_esi_valid(&evpn
.eth_s_id
) &&
4225 !ipaddr_is_zero(&evpn
.gw_ip
)) {
4226 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4227 "%s - Rx EVPN Type-5 ESI and gateway-IP both non-zero.",
4229 is_valid_update
= false;
4230 } else if (bgp_evpn_is_esi_valid(&evpn
.eth_s_id
))
4231 evpn
.type
= OVERLAY_INDEX_ESI
;
4232 else if (!ipaddr_is_zero(&evpn
.gw_ip
))
4233 evpn
.type
= OVERLAY_INDEX_GATEWAY_IP
;
4235 if (is_zero_mac(&attr
->rmac
) &&
4236 !bgp_evpn_is_esi_valid(&evpn
.eth_s_id
) &&
4237 ipaddr_is_zero(&evpn
.gw_ip
) && label
== 0) {
4238 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
4239 "%s - Rx EVPN Type-5 ESI, gateway-IP, RMAC and label all zero",
4241 is_valid_update
= false;
4244 if (is_mcast_mac(&attr
->rmac
) || is_bcast_mac(&attr
->rmac
))
4245 is_valid_update
= false;
4248 /* Process the route. */
4249 if (attr
&& is_valid_update
)
4250 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4251 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4252 &prd
, &label
, 1, 0, &evpn
);
4254 if (!is_valid_update
) {
4255 char attr_str
[BUFSIZ
] = {0};
4257 bgp_dump_attr(attr
, attr_str
, BUFSIZ
);
4259 "Invalid update from peer %s vrf %u prefix %pFX attr %s - treat as withdraw",
4260 peer
->hostname
, peer
->bgp
->vrf_id
, &p
,
4263 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
4264 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4265 &prd
, &label
, 1, &evpn
);
4271 static void evpn_mpattr_encode_type5(struct stream
*s
, const struct prefix
*p
,
4272 const struct prefix_rd
*prd
,
4273 mpls_label_t
*label
, uint32_t num_labels
,
4278 const struct evpn_addr
*p_evpn_p
;
4280 memset(&temp
, 0, sizeof(temp
));
4281 if (p
->family
!= AF_EVPN
)
4283 p_evpn_p
= &(p
->u
.prefix_evpn
);
4285 /* len denites the total len of IP and GW-IP in the route
4286 IP and GW-IP have to be both ipv4 or ipv6
4288 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4289 len
= 8; /* IP and GWIP are both ipv4 */
4291 len
= 32; /* IP and GWIP are both ipv6 */
4292 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4293 stream_putc(s
, 8 + 10 + 4 + 1 + len
+ 3);
4294 stream_put(s
, prd
->val
, 8);
4295 if (attr
&& attr
->evpn_overlay
.type
== OVERLAY_INDEX_ESI
)
4296 stream_put(s
, &attr
->esi
, sizeof(esi_t
));
4298 stream_put(s
, 0, sizeof(esi_t
));
4299 stream_putl(s
, p_evpn_p
->prefix_addr
.eth_tag
);
4300 stream_putc(s
, p_evpn_p
->prefix_addr
.ip_prefix_length
);
4301 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4302 stream_put_ipv4(s
, p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
.s_addr
);
4304 stream_put(s
, &p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
, 16);
4305 if (attr
&& attr
->evpn_overlay
.type
== OVERLAY_INDEX_GATEWAY_IP
) {
4306 const struct bgp_route_evpn
*evpn_overlay
=
4307 bgp_attr_get_evpn_overlay(attr
);
4309 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4311 evpn_overlay
->gw_ip
.ipaddr_v4
.s_addr
);
4313 stream_put(s
, &(evpn_overlay
->gw_ip
.ipaddr_v6
), 16);
4315 if (IS_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
))
4316 stream_put_ipv4(s
, 0);
4318 stream_put(s
, &temp
, 16);
4322 stream_put(s
, label
, 3);
4328 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4330 static void cleanup_vni_on_disable(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4332 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4334 /* Remove EVPN routes and schedule for processing. */
4335 delete_routes_for_vni(bgp
, vpn
);
4337 /* Clear "live" flag and see if hash needs to be freed. */
4338 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
4339 if (!is_vni_configured(vpn
))
4340 bgp_evpn_free(bgp
, vpn
);
4344 * Free a VNI entry; iterator function called during cleanup.
4346 static void free_vni_entry(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4348 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
4350 delete_all_vni_routes(bgp
, vpn
);
4351 bgp_evpn_free(bgp
, vpn
);
4355 * Derive AUTO import RT for BGP VRF - L3VNI
4357 static void evpn_auto_rt_import_add_for_vrf(struct bgp
*bgp_vrf
)
4359 struct bgp
*bgp_evpn
= NULL
;
4361 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
, true);
4364 bgp_evpn
= bgp_get_evpn();
4369 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4373 * Delete AUTO import RT from BGP VRF - L3VNI
4375 static void evpn_auto_rt_import_delete_for_vrf(struct bgp
*bgp_vrf
)
4377 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_import_rtl
,
4382 * Derive AUTO export RT for BGP VRF - L3VNI
4384 static void evpn_auto_rt_export_add_for_vrf(struct bgp
*bgp_vrf
)
4386 form_auto_rt(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
, true);
4390 * Delete AUTO export RT from BGP VRF - L3VNI
4392 static void evpn_auto_rt_export_delete_for_vrf(struct bgp
*bgp_vrf
)
4394 evpn_rt_delete_auto(bgp_vrf
, bgp_vrf
->l3vni
, bgp_vrf
->vrf_export_rtl
,
4398 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp
*bgp_vrf
)
4400 struct bgp
*bgp_evpn
= NULL
;
4401 struct listnode
*node
= NULL
;
4402 struct bgpevpn
*vpn
= NULL
;
4404 bgp_evpn
= bgp_get_evpn();
4408 /* update all type-5 routes */
4409 update_advertise_vrf_routes(bgp_vrf
);
4411 /* update all type-2 routes */
4412 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
4413 update_routes_for_vni(bgp_evpn
, vpn
);
4417 * Handle autort change for a given VNI.
4419 static void update_autort_vni(struct hash_bucket
*bucket
, struct bgp
*bgp
)
4421 struct bgpevpn
*vpn
= bucket
->data
;
4423 if (!is_import_rt_configured(vpn
)) {
4424 if (is_vni_live(vpn
))
4425 bgp_evpn_uninstall_routes(bgp
, vpn
);
4426 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
4427 list_delete_all_node(vpn
->import_rtl
);
4428 bgp_evpn_derive_auto_rt_import(bgp
, vpn
);
4429 if (is_vni_live(vpn
))
4430 bgp_evpn_install_routes(bgp
, vpn
);
4432 if (!is_export_rt_configured(vpn
)) {
4433 list_delete_all_node(vpn
->export_rtl
);
4434 bgp_evpn_derive_auto_rt_export(bgp
, vpn
);
4435 if (is_vni_live(vpn
))
4436 bgp_evpn_handle_export_rt_change(bgp
, vpn
);
4441 * Handle autort change for L3VNI.
4443 static void update_autort_l3vni(struct bgp
*bgp
)
4445 if ((CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
4446 && (CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)))
4449 if (!CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
)) {
4450 if (is_l3vni_live(bgp
))
4451 uninstall_routes_for_vrf(bgp
);
4453 /* Cleanup the RT to VRF mapping */
4454 bgp_evpn_unmap_vrf_from_its_rts(bgp
);
4456 /* Remove auto generated RT */
4457 evpn_auto_rt_import_delete_for_vrf(bgp
);
4459 list_delete_all_node(bgp
->vrf_import_rtl
);
4461 /* Map auto derive or configured RTs */
4462 evpn_auto_rt_import_add_for_vrf(bgp
);
4465 if (!CHECK_FLAG(bgp
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
4466 list_delete_all_node(bgp
->vrf_export_rtl
);
4468 evpn_auto_rt_export_delete_for_vrf(bgp
);
4470 evpn_auto_rt_export_add_for_vrf(bgp
);
4472 if (is_l3vni_live(bgp
))
4473 bgp_evpn_map_vrf_to_its_rts(bgp
);
4476 if (!is_l3vni_live(bgp
))
4479 /* advertise type-5 routes if needed */
4480 update_advertise_vrf_routes(bgp
);
4482 /* install all remote routes belonging to this l3vni
4483 * into corresponding vrf
4485 install_routes_for_vrf(bgp
);
4492 /* withdraw type-5 route corresponding to ip prefix */
4493 void bgp_evpn_withdraw_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4494 afi_t afi
, safi_t safi
)
4497 struct prefix_evpn evp
;
4499 build_type5_prefix_from_ip_prefix(&evp
, p
);
4500 ret
= delete_evpn_type5_route(bgp_vrf
, &evp
);
4503 EC_BGP_EVPN_ROUTE_DELETE
,
4504 "%u failed to delete type-5 route for prefix %pFX in vrf %s",
4505 bgp_vrf
->vrf_id
, p
, vrf_id_to_name(bgp_vrf
->vrf_id
));
4508 /* withdraw all type-5 routes for an address family */
4509 void bgp_evpn_withdraw_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
, safi_t safi
)
4511 struct bgp_table
*table
= NULL
;
4512 struct bgp_dest
*dest
= NULL
;
4513 struct bgp_path_info
*pi
;
4515 table
= bgp_vrf
->rib
[afi
][safi
];
4516 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
4517 /* Only care about "selected" routes. Also ensure that
4518 * these are routes that are injectable into EVPN.
4520 /* TODO: Support for AddPath for EVPN. */
4521 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
4522 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4523 && is_route_injectable_into_evpn(pi
)) {
4524 bgp_evpn_withdraw_type5_route(
4525 bgp_vrf
, bgp_dest_get_prefix(dest
), afi
,
4534 * evpn - enable advertisement of default g/w
4536 void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
, afi_t afi
,
4537 safi_t safi
, bool add
)
4539 struct prefix ip_prefix
;
4541 /* form the default prefix 0.0.0.0/0 */
4542 memset(&ip_prefix
, 0, sizeof(ip_prefix
));
4543 ip_prefix
.family
= afi2family(afi
);
4546 bgp_evpn_advertise_type5_route(bgp_vrf
, &ip_prefix
,
4549 bgp_evpn_withdraw_type5_route(bgp_vrf
, &ip_prefix
,
4556 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4557 * to this function correspond to those of the source IP prefix (best
4558 * path in the case of the attr. In the case of a local prefix (when we
4559 * are advertising local subnets), the src_attr will be NULL.
4561 void bgp_evpn_advertise_type5_route(struct bgp
*bgp_vrf
, const struct prefix
*p
,
4562 struct attr
*src_attr
, afi_t afi
,
4566 struct prefix_evpn evp
;
4568 build_type5_prefix_from_ip_prefix(&evp
, p
);
4569 ret
= update_evpn_type5_route(bgp_vrf
, &evp
, src_attr
, afi
, safi
);
4571 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
4572 "%u: Failed to create type-5 route for prefix %pFX",
4573 bgp_vrf
->vrf_id
, p
);
4576 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4577 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4578 * advertisement is enabled.
4580 void bgp_evpn_advertise_type5_routes(struct bgp
*bgp_vrf
, afi_t afi
,
4583 struct bgp_table
*table
= NULL
;
4584 struct bgp_dest
*dest
= NULL
;
4585 struct bgp_path_info
*pi
;
4587 table
= bgp_vrf
->rib
[afi
][safi
];
4588 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
4589 /* Need to identify the "selected" route entry to use its
4590 * attribute. Also, ensure that the route is injectable
4592 * TODO: Support for AddPath for EVPN.
4594 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
4595 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4596 && is_route_injectable_into_evpn(pi
)) {
4598 /* apply the route-map */
4599 if (bgp_vrf
->adv_cmd_rmap
[afi
][safi
].map
) {
4600 route_map_result_t ret
;
4601 struct bgp_path_info tmp_pi
;
4602 struct bgp_path_info_extra tmp_pie
;
4603 struct attr tmp_attr
;
4605 tmp_attr
= *pi
->attr
;
4607 /* Fill temp path_info */
4608 prep_for_rmap_apply(&tmp_pi
, &tmp_pie
,
4612 RESET_FLAG(tmp_attr
.rmap_change_flags
);
4614 ret
= route_map_apply(
4615 bgp_vrf
->adv_cmd_rmap
[afi
][safi
]
4617 bgp_dest_get_prefix(dest
),
4619 if (ret
== RMAP_DENYMATCH
) {
4620 bgp_attr_flush(&tmp_attr
);
4623 bgp_evpn_advertise_type5_route(
4625 bgp_dest_get_prefix(dest
),
4626 &tmp_attr
, afi
, safi
);
4628 bgp_evpn_advertise_type5_route(
4630 bgp_dest_get_prefix(dest
),
4631 pi
->attr
, afi
, safi
);
4638 static void rt_list_remove_node(struct list
*rt_list
,
4639 struct ecommunity
*ecomdel
, bool is_l3
)
4641 struct listnode
*node
= NULL
, *nnode
= NULL
, *node_to_del
= NULL
;
4642 struct vrf_route_target
*l3rt
= NULL
;
4643 struct ecommunity
*ecom
= NULL
;
4646 for (ALL_LIST_ELEMENTS(rt_list
, node
, nnode
, l3rt
)) {
4647 if (ecommunity_match(l3rt
->ecom
, ecomdel
)) {
4648 evpn_vrf_rt_del(l3rt
);
4654 for (ALL_LIST_ELEMENTS(rt_list
, node
, nnode
, ecom
)) {
4655 if (ecommunity_match(ecom
, ecomdel
)) {
4656 ecommunity_free(&ecom
);
4665 list_delete_node(rt_list
, node_to_del
);
4668 void evpn_rt_delete_auto(struct bgp
*bgp
, vni_t vni
, struct list
*rtl
,
4671 struct ecommunity
*ecom_auto
;
4672 struct ecommunity_val eval
;
4674 if (bgp
->advertise_autort_rfc8365
)
4675 vni
|= EVPN_AUTORT_VXLAN
;
4677 encode_route_target_as((bgp
->as
& 0xFFFF), vni
, &eval
);
4679 ecom_auto
= ecommunity_new();
4680 ecommunity_add_val(ecom_auto
, &eval
, false, false);
4682 rt_list_remove_node(rtl
, ecom_auto
, is_l3
);
4684 ecommunity_free(&ecom_auto
);
4687 static void evpn_vrf_rt_routes_map(struct bgp
*bgp_vrf
)
4689 /* map VRFs to its RTs and install routes matching this new RT */
4690 if (is_l3vni_live(bgp_vrf
)) {
4691 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
4692 install_routes_for_vrf(bgp_vrf
);
4696 static void evpn_vrf_rt_routes_unmap(struct bgp
*bgp_vrf
)
4698 /* uninstall routes from vrf */
4699 if (is_l3vni_live(bgp_vrf
))
4700 uninstall_routes_for_vrf(bgp_vrf
);
4702 /* Cleanup the RT to VRF mapping */
4703 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
4706 static bool rt_list_has_cfgd_rt(struct list
*rt_list
)
4708 struct listnode
*node
= NULL
, *nnode
= NULL
;
4709 struct vrf_route_target
*l3rt
= NULL
;
4711 for (ALL_LIST_ELEMENTS(rt_list
, node
, nnode
, l3rt
)) {
4712 if (!CHECK_FLAG(l3rt
->flags
, BGP_VRF_RT_AUTO
))
4719 static void unconfigure_import_rt_for_vrf_fini(struct bgp
*bgp_vrf
)
4721 if (!bgp_vrf
->vrf_import_rtl
)
4722 return; /* this should never fail */
4724 if (!is_l3vni_live(bgp_vrf
))
4725 return; /* Nothing to do if no vni */
4727 /* fall back to auto-generated RT if this was the last RT */
4728 if (list_isempty(bgp_vrf
->vrf_import_rtl
))
4729 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4732 static void unconfigure_export_rt_for_vrf_fini(struct bgp
*bgp_vrf
)
4735 if (!bgp_vrf
->vrf_export_rtl
)
4736 return; /* this should never fail */
4738 if (!is_l3vni_live(bgp_vrf
))
4739 return; /* Nothing to do if no vni */
4741 /* fall back to auto-generated RT if this was the last RT */
4742 if (list_isempty(bgp_vrf
->vrf_export_rtl
))
4743 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4745 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4748 void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4749 struct ecommunity
*ecomadd
,
4752 struct vrf_route_target
*newrt
;
4754 newrt
= evpn_vrf_rt_new(ecomadd
);
4757 SET_FLAG(newrt
->flags
, BGP_VRF_RT_WILD
);
4759 evpn_vrf_rt_routes_unmap(bgp_vrf
);
4761 /* Remove auto generated RT if not configured */
4762 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_AUTO_RT_CFGD
))
4763 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4765 /* Add the newly configured RT to RT list */
4766 listnode_add_sort(bgp_vrf
->vrf_import_rtl
, newrt
);
4768 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4770 evpn_vrf_rt_routes_map(bgp_vrf
);
4773 void bgp_evpn_configure_import_auto_rt_for_vrf(struct bgp
*bgp_vrf
)
4775 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_AUTO_RT_CFGD
))
4776 return; /* Already configured */
4778 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_AUTO_RT_CFGD
);
4780 if (!is_l3vni_live(bgp_vrf
))
4781 return; /* Wait for VNI before adding rts */
4783 evpn_vrf_rt_routes_unmap(bgp_vrf
);
4785 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
4787 evpn_vrf_rt_routes_map(bgp_vrf
);
4790 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
4791 struct ecommunity
*ecomdel
)
4793 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
4794 return; /* Already un-configured */
4796 evpn_vrf_rt_routes_unmap(bgp_vrf
);
4799 rt_list_remove_node(bgp_vrf
->vrf_import_rtl
, ecomdel
, true);
4801 if (!rt_list_has_cfgd_rt(bgp_vrf
->vrf_import_rtl
))
4802 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
);
4804 unconfigure_import_rt_for_vrf_fini(bgp_vrf
);
4806 evpn_vrf_rt_routes_map(bgp_vrf
);
4809 void bgp_evpn_unconfigure_import_auto_rt_for_vrf(struct bgp
*bgp_vrf
)
4811 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_AUTO_RT_CFGD
))
4812 return; /* Already un-configured */
4814 evpn_vrf_rt_routes_unmap(bgp_vrf
);
4816 /* remove auto-generated RT */
4817 evpn_auto_rt_import_delete_for_vrf(bgp_vrf
);
4819 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_AUTO_RT_CFGD
);
4821 unconfigure_import_rt_for_vrf_fini(bgp_vrf
);
4823 evpn_vrf_rt_routes_map(bgp_vrf
);
4826 void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4827 struct ecommunity
*ecomadd
)
4829 struct vrf_route_target
*newrt
;
4831 newrt
= evpn_vrf_rt_new(ecomadd
);
4833 /* Remove auto generated RT if not configured */
4834 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_AUTO_RT_CFGD
))
4835 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4837 /* Add the new RT to the RT list */
4838 listnode_add_sort(bgp_vrf
->vrf_export_rtl
, newrt
);
4840 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4842 if (is_l3vni_live(bgp_vrf
))
4843 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4846 void bgp_evpn_configure_export_auto_rt_for_vrf(struct bgp
*bgp_vrf
)
4848 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_AUTO_RT_CFGD
))
4849 return; /* Already configured */
4851 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_AUTO_RT_CFGD
);
4853 if (!is_l3vni_live(bgp_vrf
))
4854 return; /* Wait for VNI before adding rts */
4856 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
4858 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf
);
4861 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
4862 struct ecommunity
*ecomdel
)
4864 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
))
4865 return; /* Already un-configured */
4868 rt_list_remove_node(bgp_vrf
->vrf_export_rtl
, ecomdel
, true);
4870 if (!rt_list_has_cfgd_rt(bgp_vrf
->vrf_export_rtl
))
4871 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
);
4873 unconfigure_export_rt_for_vrf_fini(bgp_vrf
);
4876 void bgp_evpn_unconfigure_export_auto_rt_for_vrf(struct bgp
*bgp_vrf
)
4878 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_AUTO_RT_CFGD
))
4879 return; /* Already un-configured */
4881 /* remove auto-generated RT */
4882 evpn_auto_rt_export_delete_for_vrf(bgp_vrf
);
4884 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_AUTO_RT_CFGD
);
4886 unconfigure_export_rt_for_vrf_fini(bgp_vrf
);
4890 * Handle change to BGP router id. This is invoked twice by the change
4891 * handler, first before the router id has been changed and then after
4892 * the router id has been changed. The first invocation will result in
4893 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4894 * will result in the routes being re-advertised.
4896 void bgp_evpn_handle_router_id_update(struct bgp
*bgp
, int withdraw
)
4898 struct listnode
*node
;
4899 struct bgp
*bgp_vrf
;
4903 /* delete and withdraw all the type-5 routes
4904 stored in the global table for this vrf
4906 withdraw_router_id_vrf(bgp
);
4908 /* delete all the VNI routes (type-2/type-3) routes for all the
4911 hash_iterate(bgp
->vnihash
,
4912 (void (*)(struct hash_bucket
*,
4913 void *))withdraw_router_id_vni
,
4916 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4917 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4918 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4919 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4921 bgp_vrf
->evpn_info
->pip_ip
.s_addr
4927 /* Assign new default instance router-id */
4928 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4929 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4930 if (bgp_vrf
->evpn_info
->advertise_pip
&&
4931 (bgp_vrf
->evpn_info
->pip_ip_static
.s_addr
4933 bgp_vrf
->evpn_info
->pip_ip
=
4935 /* advertise type-5 routes with
4938 update_advertise_vrf_routes(bgp_vrf
);
4943 /* advertise all routes in the vrf as type-5 routes with the new
4946 update_router_id_vrf(bgp
);
4948 /* advertise all the VNI routes (type-2/type-3) routes with the
4951 hash_iterate(bgp
->vnihash
,
4952 (void (*)(struct hash_bucket
*,
4953 void *))update_router_id_vni
,
4959 * Handle change to auto-RT algorithm - update and advertise local routes.
4961 void bgp_evpn_handle_autort_change(struct bgp
*bgp
)
4963 hash_iterate(bgp
->vnihash
,
4964 (void (*)(struct hash_bucket
*,
4965 void*))update_autort_vni
,
4968 update_autort_l3vni(bgp
);
4972 * Handle change to export RT - update and advertise local routes.
4974 int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
, struct bgpevpn
*vpn
)
4976 return update_routes_for_vni(bgp
, vpn
);
4979 void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
)
4982 delete_withdraw_vrf_routes(bgp_vrf
);
4984 update_advertise_vrf_routes(bgp_vrf
);
4988 * Handle change to RD. This is invoked twice by the change handler,
4989 * first before the RD has been changed and then after the RD has
4990 * been changed. The first invocation will result in local routes
4991 * of this VNI being deleted and withdrawn and the next will result
4992 * in the routes being re-advertised.
4994 void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
4998 delete_withdraw_vni_routes(bgp
, vpn
);
5000 update_advertise_vni_routes(bgp
, vpn
);
5004 * Install routes for this VNI. Invoked upon change to Import RT.
5006 int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5008 return install_routes_for_vni(bgp
, vpn
);
5012 * Uninstall all routes installed for this VNI. Invoked upon change
5015 int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5017 return uninstall_routes_for_vni(bgp
, vpn
);
5021 * TODO: Hardcoded for a maximum of 2 VNIs right now
5023 char *bgp_evpn_label2str(mpls_label_t
*label
, uint32_t num_labels
, char *buf
,
5028 vni1
= label2vni(label
);
5029 if (num_labels
== 2) {
5030 vni2
= label2vni(label
+ 1);
5031 snprintf(buf
, len
, "%u/%u", vni1
, vni2
);
5033 snprintf(buf
, len
, "%u", vni1
);
5038 * Function to convert evpn route to json format.
5039 * NOTE: We don't use prefix2str as the output here is a bit different.
5041 void bgp_evpn_route2json(const struct prefix_evpn
*p
, json_object
*json
)
5043 char buf1
[ETHER_ADDR_STRLEN
];
5044 char buf2
[PREFIX2STR_BUFFER
];
5051 json_object_int_add(json
, "routeType", p
->prefix
.route_type
);
5053 switch (p
->prefix
.route_type
) {
5054 case BGP_EVPN_MAC_IP_ROUTE
:
5055 json_object_int_add(json
, "ethTag",
5056 p
->prefix
.macip_addr
.eth_tag
);
5057 json_object_int_add(json
, "macLen", 8 * ETH_ALEN
);
5058 json_object_string_add(json
, "mac",
5059 prefix_mac2str(&p
->prefix
.macip_addr
.mac
, buf1
,
5062 if (!is_evpn_prefix_ipaddr_none(p
)) {
5063 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
:
5065 prefixlen
= (family
== AF_INET
) ?
5066 IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
;
5067 inet_ntop(family
, &p
->prefix
.macip_addr
.ip
.ip
.addr
,
5068 buf2
, PREFIX2STR_BUFFER
);
5069 json_object_int_add(json
, "ipLen", prefixlen
);
5070 json_object_string_add(json
, "ip", buf2
);
5074 case BGP_EVPN_IMET_ROUTE
:
5075 json_object_int_add(json
, "ethTag",
5076 p
->prefix
.imet_addr
.eth_tag
);
5077 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
5078 prefixlen
= (family
== AF_INET
) ? IPV4_MAX_BITLEN
:
5080 inet_ntop(family
, &p
->prefix
.imet_addr
.ip
.ip
.addr
, buf2
,
5082 json_object_int_add(json
, "ipLen", prefixlen
);
5083 json_object_string_add(json
, "ip", buf2
);
5086 case BGP_EVPN_IP_PREFIX_ROUTE
:
5087 json_object_int_add(json
, "ethTag",
5088 p
->prefix
.prefix_addr
.eth_tag
);
5089 family
= is_evpn_prefix_ipaddr_v4(p
) ? AF_INET
: AF_INET6
;
5090 inet_ntop(family
, &p
->prefix
.prefix_addr
.ip
.ip
.addr
,
5091 buf2
, sizeof(buf2
));
5092 json_object_int_add(json
, "ipLen",
5093 p
->prefix
.prefix_addr
.ip_prefix_length
);
5094 json_object_string_add(json
, "ip", buf2
);
5103 * Encode EVPN prefix in Update (MP_REACH)
5105 void bgp_evpn_encode_prefix(struct stream
*s
, const struct prefix
*p
,
5106 const struct prefix_rd
*prd
, mpls_label_t
*label
,
5107 uint32_t num_labels
, struct attr
*attr
,
5108 bool addpath_capable
, uint32_t addpath_tx_id
)
5110 struct prefix_evpn
*evp
= (struct prefix_evpn
*)p
;
5111 int len
, ipa_len
= 0;
5113 if (addpath_capable
)
5114 stream_putl(s
, addpath_tx_id
);
5117 stream_putc(s
, evp
->prefix
.route_type
);
5119 switch (evp
->prefix
.route_type
) {
5120 case BGP_EVPN_MAC_IP_ROUTE
:
5121 if (is_evpn_prefix_ipaddr_v4(evp
))
5122 ipa_len
= IPV4_MAX_BYTELEN
;
5123 else if (is_evpn_prefix_ipaddr_v6(evp
))
5124 ipa_len
= IPV6_MAX_BYTELEN
;
5125 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
5126 len
= 8 + 10 + 4 + 1 + 6 + 1 + ipa_len
+ 3;
5127 if (ipa_len
&& num_labels
> 1) /* There are 2 VNIs */
5129 stream_putc(s
, len
);
5130 stream_put(s
, prd
->val
, 8); /* RD */
5132 stream_put(s
, &attr
->esi
, ESI_BYTES
);
5134 stream_put(s
, 0, 10);
5135 stream_putl(s
, evp
->prefix
.macip_addr
.eth_tag
); /* Ethernet Tag ID */
5136 stream_putc(s
, 8 * ETH_ALEN
); /* Mac Addr Len - bits */
5137 stream_put(s
, evp
->prefix
.macip_addr
.mac
.octet
, 6); /* Mac Addr */
5138 stream_putc(s
, 8 * ipa_len
); /* IP address Length */
5139 if (ipa_len
) /* IP */
5140 stream_put(s
, &evp
->prefix
.macip_addr
.ip
.ip
.addr
,
5142 /* 1st label is the L2 VNI */
5143 stream_put(s
, label
, BGP_LABEL_BYTES
);
5144 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
5145 if (ipa_len
&& num_labels
> 1)
5146 stream_put(s
, label
+ 1, BGP_LABEL_BYTES
);
5149 case BGP_EVPN_IMET_ROUTE
:
5150 stream_putc(s
, 17); // TODO: length - assumes IPv4 address
5151 stream_put(s
, prd
->val
, 8); /* RD */
5152 stream_putl(s
, evp
->prefix
.imet_addr
.eth_tag
); /* Ethernet Tag ID */
5153 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
5154 /* Originating Router's IP Addr */
5155 stream_put_in_addr(s
, &evp
->prefix
.imet_addr
.ip
.ipaddr_v4
);
5158 case BGP_EVPN_ES_ROUTE
:
5159 stream_putc(s
, 23); /* TODO: length: assumes ipv4 VTEP */
5160 stream_put(s
, prd
->val
, 8); /* RD */
5161 stream_put(s
, evp
->prefix
.es_addr
.esi
.val
, 10); /* ESI */
5162 stream_putc(s
, IPV4_MAX_BITLEN
); /* IP address Length - bits */
5164 stream_put_in_addr(s
, &evp
->prefix
.es_addr
.ip
.ipaddr_v4
);
5167 case BGP_EVPN_AD_ROUTE
:
5168 /* RD, ESI, EthTag, 1 VNI */
5169 len
= RD_BYTES
+ ESI_BYTES
+ EVPN_ETH_TAG_BYTES
+ BGP_LABEL_BYTES
;
5170 stream_putc(s
, len
);
5171 stream_put(s
, prd
->val
, RD_BYTES
); /* RD */
5172 stream_put(s
, evp
->prefix
.ead_addr
.esi
.val
, ESI_BYTES
); /* ESI */
5173 stream_putl(s
, evp
->prefix
.ead_addr
.eth_tag
); /* Ethernet Tag */
5174 stream_put(s
, label
, BGP_LABEL_BYTES
);
5177 case BGP_EVPN_IP_PREFIX_ROUTE
:
5178 /* TODO: AddPath support. */
5179 evpn_mpattr_encode_type5(s
, p
, prd
, label
, num_labels
, attr
);
5187 int bgp_nlri_parse_evpn(struct peer
*peer
, struct attr
*attr
,
5188 struct bgp_nlri
*packet
, int withdraw
)
5194 uint32_t addpath_id
;
5195 bool addpath_capable
;
5200 /* Start processing the NLRI - there may be multiple in the MP_REACH */
5202 lim
= pnt
+ packet
->length
;
5204 safi
= packet
->safi
;
5207 addpath_capable
= bgp_addpath_encode_rx(peer
, afi
, safi
);
5209 for (; pnt
< lim
; pnt
+= psize
) {
5210 /* Clear prefix structure. */
5211 memset(&p
, 0, sizeof(p
));
5213 /* Deal with path-id if AddPath is supported. */
5214 if (addpath_capable
) {
5215 /* When packet overflow occurs return immediately. */
5216 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
5217 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5219 memcpy(&addpath_id
, pnt
, BGP_ADDPATH_ID_LEN
);
5220 addpath_id
= ntohl(addpath_id
);
5221 pnt
+= BGP_ADDPATH_ID_LEN
;
5224 /* All EVPN NLRI types start with type and length. */
5226 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE
;
5231 /* When packet overflow occur return immediately. */
5232 if (pnt
+ psize
> lim
)
5233 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
5236 case BGP_EVPN_MAC_IP_ROUTE
:
5237 if (process_type2_route(peer
, afi
, safi
,
5238 withdraw
? NULL
: attr
, pnt
,
5239 psize
, addpath_id
)) {
5242 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
5243 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5244 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE
;
5248 case BGP_EVPN_IMET_ROUTE
:
5249 if (process_type3_route(peer
, afi
, safi
,
5250 withdraw
? NULL
: attr
, pnt
,
5251 psize
, addpath_id
)) {
5254 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
5255 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5256 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE
;
5260 case BGP_EVPN_ES_ROUTE
:
5261 if (bgp_evpn_type4_route_process(peer
, afi
, safi
,
5262 withdraw
? NULL
: attr
, pnt
,
5263 psize
, addpath_id
)) {
5266 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
5267 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5268 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE
;
5272 case BGP_EVPN_AD_ROUTE
:
5273 if (bgp_evpn_type1_route_process(peer
, afi
, safi
,
5274 withdraw
? NULL
: attr
, pnt
,
5275 psize
, addpath_id
)) {
5278 "%u:%s - Error in processing EVPN type-1 NLRI size %d",
5279 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5280 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE1_SIZE
;
5284 case BGP_EVPN_IP_PREFIX_ROUTE
:
5285 if (process_type5_route(peer
, afi
, safi
,
5286 withdraw
? NULL
: attr
, pnt
,
5287 psize
, addpath_id
)) {
5290 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
5291 peer
->bgp
->vrf_id
, peer
->host
, psize
);
5292 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE
;
5301 /* Packet length consistency check. */
5303 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH
;
5305 return BGP_NLRI_PARSE_OK
;
5309 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
5310 * The mapping will be used during route processing.
5311 * bgp_vrf: specific bgp vrf instance on which RT is configured
5313 void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
)
5315 struct listnode
*node
, *nnode
;
5316 struct vrf_route_target
*l3rt
;
5318 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, l3rt
))
5319 map_vrf_to_rt(bgp_vrf
, l3rt
);
5323 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
5325 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
)
5327 struct listnode
*node
, *nnode
;
5328 struct vrf_route_target
*l3rt
;
5330 for (ALL_LIST_ELEMENTS(bgp_vrf
->vrf_import_rtl
, node
, nnode
, l3rt
))
5331 unmap_vrf_from_rt(bgp_vrf
, l3rt
);
5335 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
5336 * The mapping will be used during route processing.
5338 void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5341 struct ecommunity_val
*eval
;
5342 struct listnode
*node
, *nnode
;
5343 struct ecommunity
*ecom
;
5345 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5346 for (i
= 0; i
< ecom
->size
; i
++) {
5347 eval
= (struct ecommunity_val
*)(ecom
->val
5349 * ECOMMUNITY_SIZE
));
5350 map_vni_to_rt(bgp
, vpn
, eval
);
5356 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
5358 void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5361 struct ecommunity_val
*eval
;
5362 struct listnode
*node
, *nnode
;
5363 struct ecommunity
*ecom
;
5365 for (ALL_LIST_ELEMENTS(vpn
->import_rtl
, node
, nnode
, ecom
)) {
5366 for (i
= 0; i
< ecom
->size
; i
++) {
5367 struct irt_node
*irt
;
5368 struct ecommunity_val eval_tmp
;
5370 eval
= (struct ecommunity_val
*)(ecom
->val
5372 * ECOMMUNITY_SIZE
));
5373 /* If using "automatic" RT, we only care about the
5374 * local-admin sub-field.
5375 * This is to facilitate using VNI as the RT for EBGP
5378 memcpy(&eval_tmp
, eval
, ECOMMUNITY_SIZE
);
5379 if (!is_import_rt_configured(vpn
))
5380 mask_ecom_global_admin(&eval_tmp
, eval
);
5382 irt
= lookup_import_rt(bgp
, &eval_tmp
);
5384 unmap_vni_from_rt(bgp
, vpn
, irt
);
5390 * Derive Import RT automatically for VNI and map VNI to RT.
5391 * The mapping will be used during route processing.
5393 void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5395 form_auto_rt(bgp
, vpn
->vni
, vpn
->import_rtl
, false);
5396 UNSET_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
);
5399 bgp_evpn_map_vni_to_its_rts(bgp
, vpn
);
5403 * Derive Export RT automatically for VNI.
5405 void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5407 form_auto_rt(bgp
, vpn
->vni
, vpn
->export_rtl
, false);
5408 UNSET_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
);
5412 * Derive RD automatically for VNI using passed information - it
5413 * is of the form RouterId:unique-id-for-vni.
5415 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
)
5417 if (is_vrf_rd_configured(bgp
))
5420 form_auto_rd(bgp
->router_id
, bgp
->vrf_rd_id
, &bgp
->vrf_prd
);
5424 * Derive RD automatically for VNI using passed information - it
5425 * is of the form RouterId:unique-id-for-vni.
5427 void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5429 char buf
[BGP_EVPN_PREFIX_RD_LEN
];
5431 vpn
->prd
.family
= AF_UNSPEC
;
5432 vpn
->prd
.prefixlen
= 64;
5433 snprintfrr(buf
, sizeof(buf
), "%pI4:%hu", &bgp
->router_id
, vpn
->rd_id
);
5434 (void)str2prefix_rd(buf
, &vpn
->prd
);
5435 UNSET_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
);
5441 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
)
5443 struct list
*inst
= bm
->bgp
;
5444 struct listnode
*node
;
5445 struct bgp
*bgp_vrf
;
5447 for (ALL_LIST_ELEMENTS_RO(inst
, node
, bgp_vrf
)) {
5448 if (bgp_vrf
->l3vni
== vni
)
5458 struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
)
5460 struct bgpevpn
*vpn
;
5463 memset(&tmp
, 0, sizeof(tmp
));
5465 vpn
= hash_lookup(bgp
->vnihash
, &tmp
);
5470 * Create a new vpn - invoked upon configuration or zebra notification.
5472 struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
5473 struct in_addr originator_ip
,
5474 vrf_id_t tenant_vrf_id
,
5475 struct in_addr mcast_grp
,
5476 ifindex_t svi_ifindex
)
5478 struct bgpevpn
*vpn
;
5480 vpn
= XCALLOC(MTYPE_BGP_EVPN
, sizeof(struct bgpevpn
));
5482 /* Set values - RD and RT set to defaults. */
5484 vpn
->originator_ip
= originator_ip
;
5485 vpn
->tenant_vrf_id
= tenant_vrf_id
;
5486 vpn
->mcast_grp
= mcast_grp
;
5487 vpn
->svi_ifindex
= svi_ifindex
;
5489 /* Initialize route-target import and export lists */
5490 vpn
->import_rtl
= list_new();
5491 vpn
->import_rtl
->cmp
=
5492 (int (*)(void *, void *))bgp_evpn_route_target_cmp
;
5493 vpn
->import_rtl
->del
= bgp_evpn_xxport_delete_ecomm
;
5494 vpn
->export_rtl
= list_new();
5495 vpn
->export_rtl
->cmp
=
5496 (int (*)(void *, void *))bgp_evpn_route_target_cmp
;
5497 vpn
->export_rtl
->del
= bgp_evpn_xxport_delete_ecomm
;
5498 bf_assign_index(bm
->rd_idspace
, vpn
->rd_id
);
5499 derive_rd_rt_for_vni(bgp
, vpn
);
5501 /* Initialize EVPN route table. */
5502 vpn
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
5505 (void)hash_get(bgp
->vnihash
, vpn
, hash_alloc_intern
);
5507 bgp_evpn_remote_ip_hash_init(vpn
);
5508 bgp_evpn_link_to_vni_svi_hash(bgp
, vpn
);
5510 /* add to l2vni list on corresponding vrf */
5511 bgpevpn_link_to_l3vni(vpn
);
5513 bgp_evpn_vni_es_init(vpn
);
5515 QOBJ_REG(vpn
, bgpevpn
);
5520 * Free a given VPN - called in multiple scenarios such as zebra
5521 * notification, configuration being deleted, advertise-all-vni disabled etc.
5522 * This just frees appropriate memory, caller should have taken other
5525 void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
)
5527 bgp_evpn_remote_ip_hash_destroy(vpn
);
5528 bgp_evpn_vni_es_cleanup(vpn
);
5529 bgpevpn_unlink_from_l3vni(vpn
);
5530 bgp_table_unlock(vpn
->route_table
);
5531 bgp_evpn_unmap_vni_from_its_rts(bgp
, vpn
);
5532 list_delete(&vpn
->import_rtl
);
5533 list_delete(&vpn
->export_rtl
);
5534 bf_release_index(bm
->rd_idspace
, vpn
->rd_id
);
5535 hash_release(bgp
->vni_svi_hash
, vpn
);
5536 hash_release(bgp
->vnihash
, vpn
);
5538 XFREE(MTYPE_BGP_EVPN
, vpn
);
5541 static void hash_evpn_free(struct bgpevpn
*vpn
)
5543 XFREE(MTYPE_BGP_EVPN
, vpn
);
5547 * Import evpn route from global table to VNI/VRF/ESI.
5549 int bgp_evpn_import_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5550 const struct prefix
*p
, struct bgp_path_info
*pi
)
5552 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 1);
5556 * Unimport evpn route from VNI/VRF/ESI.
5558 int bgp_evpn_unimport_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5559 const struct prefix
*p
, struct bgp_path_info
*pi
)
5561 return install_uninstall_evpn_route(bgp
, afi
, safi
, p
, pi
, 0);
5564 /* filter routes which have martian next hops */
5565 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp
*bgp
)
5569 struct bgp_dest
*rd_dest
, *dest
;
5570 struct bgp_table
*table
;
5571 struct bgp_path_info
*pi
;
5576 /* Walk entire global routing table and evaluate routes which could be
5577 * imported into this VPN. Note that we cannot just look at the routes
5578 * for the VNI's RD -
5579 * remote routes applicable for this VNI could have any RD.
5581 /* EVPN routes are a 2-level table. */
5582 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
5583 rd_dest
= bgp_route_next(rd_dest
)) {
5584 table
= bgp_dest_get_bgp_table_info(rd_dest
);
5588 for (dest
= bgp_table_top(table
); dest
;
5589 dest
= bgp_route_next(dest
)) {
5591 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
5594 /* Consider "valid" remote routes applicable for
5596 if (!(pi
->type
== ZEBRA_ROUTE_BGP
5597 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
5599 if (bgp_nexthop_self(bgp
, afi
, pi
->type
,
5600 pi
->sub_type
, pi
->attr
,
5602 const struct prefix
*p
=
5603 bgp_dest_get_prefix(dest
);
5605 if (bgp_debug_update(pi
->peer
, p
, NULL
,
5607 char attr_str
[BUFSIZ
] = {0};
5609 bgp_dump_attr(pi
->attr
,
5614 "%u: prefix %pBD with attr %s - DENIED due to martian or self nexthop",
5618 bgp_evpn_unimport_route(bgp
, afi
, safi
,
5621 bgp_rib_remove(dest
, pi
, pi
->peer
, afi
,
5632 * Handle del of a local MACIP.
5634 int bgp_evpn_local_macip_del(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5635 struct ipaddr
*ip
, int state
)
5637 struct bgpevpn
*vpn
;
5638 struct prefix_evpn p
;
5639 struct bgp_dest
*dest
;
5641 /* Lookup VNI hash - should exist. */
5642 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5643 if (!vpn
|| !is_vni_live(vpn
)) {
5644 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5645 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5646 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5650 build_evpn_type2_prefix(&p
, mac
, ip
);
5651 if (state
== ZEBRA_NEIGH_ACTIVE
) {
5652 /* Remove EVPN type-2 route and schedule for processing. */
5653 delete_evpn_route(bgp
, vpn
, &p
);
5655 /* Re-instate the current remote best path if any */
5656 dest
= bgp_node_lookup(vpn
->route_table
, (struct prefix
*)&p
);
5658 evpn_zebra_reinstall_best_route(bgp
, vpn
, dest
);
5659 bgp_dest_unlock_node(dest
);
5667 * Handle add of a local MACIP.
5669 int bgp_evpn_local_macip_add(struct bgp
*bgp
, vni_t vni
, struct ethaddr
*mac
,
5670 struct ipaddr
*ip
, uint8_t flags
, uint32_t seq
, esi_t
*esi
)
5672 struct bgpevpn
*vpn
;
5673 struct prefix_evpn p
;
5675 /* Lookup VNI hash - should exist. */
5676 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5677 if (!vpn
|| !is_vni_live(vpn
)) {
5678 flog_warn(EC_BGP_EVPN_VPN_VNI
,
5679 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5680 bgp
->vrf_id
, vni
, vpn
? "not live" : "not found");
5684 /* Create EVPN type-2 route and schedule for processing. */
5685 build_evpn_type2_prefix(&p
, mac
, ip
);
5686 if (update_evpn_route(bgp
, vpn
, &p
, flags
, seq
, esi
)) {
5688 EC_BGP_EVPN_ROUTE_CREATE
,
5689 "%u:Failed to create Type-2 route, VNI %u %s MAC %pEA IP %pIA (flags: 0x%x)",
5690 bgp
->vrf_id
, vpn
->vni
,
5691 CHECK_FLAG(flags
, ZEBRA_MACIP_TYPE_STICKY
)
5701 static void link_l2vni_hash_to_l3vni(struct hash_bucket
*bucket
,
5702 struct bgp
*bgp_vrf
)
5704 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
5705 struct bgp
*bgp_evpn
= NULL
;
5707 bgp_evpn
= bgp_get_evpn();
5710 if (vpn
->tenant_vrf_id
== bgp_vrf
->vrf_id
)
5711 bgpevpn_link_to_l3vni(vpn
);
5714 int bgp_evpn_local_l3vni_add(vni_t l3vni
, vrf_id_t vrf_id
,
5715 struct ethaddr
*svi_rmac
,
5716 struct ethaddr
*vrr_rmac
,
5717 struct in_addr originator_ip
, int filter
,
5718 ifindex_t svi_ifindex
,
5719 bool is_anycast_mac
)
5721 struct bgp
*bgp_vrf
= NULL
; /* bgp VRF instance */
5722 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5723 struct listnode
*node
= NULL
;
5724 struct bgpevpn
*vpn
= NULL
;
5727 /* get the EVPN instance - required to get the AS number for VRF
5730 bgp_evpn
= bgp_get_evpn();
5734 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
5740 /* if the BGP vrf instance doesn't exist - create one */
5741 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5746 ret
= bgp_get_vty(&bgp_vrf
, &as
, vrf_id_to_name(vrf_id
),
5747 vrf_id
== VRF_DEFAULT
5748 ? BGP_INSTANCE_TYPE_DEFAULT
5749 : BGP_INSTANCE_TYPE_VRF
);
5751 case BGP_ERR_AS_MISMATCH
:
5752 flog_err(EC_BGP_EVPN_AS_MISMATCH
,
5753 "BGP instance is already running; AS is %u",
5756 case BGP_ERR_INSTANCE_MISMATCH
:
5757 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH
,
5758 "BGP instance type mismatch");
5762 /* mark as auto created */
5763 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
);
5766 /* associate the vrf with l3vni and related parameters */
5767 bgp_vrf
->l3vni
= l3vni
;
5768 bgp_vrf
->originator_ip
= originator_ip
;
5769 bgp_vrf
->l3vni_svi_ifindex
= svi_ifindex
;
5770 bgp_vrf
->evpn_info
->is_anycast_mac
= is_anycast_mac
;
5772 /* copy anycast MAC from VRR MAC */
5773 memcpy(&bgp_vrf
->rmac
, vrr_rmac
, ETH_ALEN
);
5774 /* copy sys RMAC from SVI MAC */
5775 memcpy(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, svi_rmac
, ETH_ALEN
);
5776 /* PIP user configured mac is not present use svi mac as sys mac */
5777 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
))
5778 memcpy(&bgp_vrf
->evpn_info
->pip_rmac
, svi_rmac
, ETH_ALEN
);
5780 if (bgp_debug_zebra(NULL
))
5782 "VRF %s vni %u pip %s RMAC %pEA sys RMAC %pEA static RMAC %pEA is_anycast_mac %s",
5783 vrf_id_to_name(bgp_vrf
->vrf_id
), bgp_vrf
->l3vni
,
5784 bgp_vrf
->evpn_info
->advertise_pip
? "enable"
5786 &bgp_vrf
->rmac
, &bgp_vrf
->evpn_info
->pip_rmac
,
5787 &bgp_vrf
->evpn_info
->pip_rmac_static
,
5788 is_anycast_mac
? "Enable" : "Disable");
5790 /* set the right filter - are we using l3vni only for prefix routes? */
5792 SET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5795 * VNI_FLAG_USE_TWO_LABELS flag for linked L2VNIs should not be
5796 * set before linking vrf to L3VNI. Thus, no need to clear
5800 UNSET_FLAG(bgp_vrf
->vrf_flags
,
5801 BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5803 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5804 if (!CHECK_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
)) {
5807 * If we are flapping VNI_FLAG_USE_TWO_LABELS
5808 * flag, update all MACIP routes in this VNI
5810 SET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5811 update_all_type2_routes(bgp_evpn
, vpn
);
5816 /* Map auto derive or configured RTs */
5817 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
) ||
5818 CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_AUTO_RT_CFGD
))
5819 evpn_auto_rt_import_add_for_vrf(bgp_vrf
);
5821 bgp_evpn_map_vrf_to_its_rts(bgp_vrf
);
5823 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
) ||
5824 CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_AUTO_RT_CFGD
))
5825 evpn_auto_rt_export_add_for_vrf(bgp_vrf
);
5827 /* auto derive RD */
5828 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf
);
5830 /* link all corresponding l2vnis */
5831 hash_iterate(bgp_evpn
->vnihash
,
5832 (void (*)(struct hash_bucket
*,
5833 void *))link_l2vni_hash_to_l3vni
,
5836 /* Only update all corresponding type-2 routes if we are advertising two
5837 * labels along with type-2 routes
5840 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
))
5841 update_routes_for_vni(bgp_evpn
, vpn
);
5843 /* advertise type-5 routes if needed */
5844 update_advertise_vrf_routes(bgp_vrf
);
5846 /* install all remote routes belonging to this l3vni into correspondng
5848 install_routes_for_vrf(bgp_vrf
);
5853 int bgp_evpn_local_l3vni_del(vni_t l3vni
, vrf_id_t vrf_id
)
5855 struct bgp
*bgp_vrf
= NULL
; /* bgp vrf instance */
5856 struct bgp
*bgp_evpn
= NULL
; /* EVPN bgp instance */
5857 struct listnode
*node
= NULL
;
5858 struct listnode
*next
= NULL
;
5859 struct bgpevpn
*vpn
= NULL
;
5861 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
5865 "Cannot process L3VNI %u Del - Could not find BGP instance",
5870 bgp_evpn
= bgp_get_evpn();
5874 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
5879 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5880 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5881 * routes. This will uninstalling the routes from zebra and decremnt the
5884 uninstall_routes_for_vrf(bgp_vrf
);
5886 /* delete/withdraw all type-5 routes */
5887 delete_withdraw_vrf_routes(bgp_vrf
);
5889 /* remove the l3vni from vrf instance */
5892 /* remove the Rmac from the BGP vrf */
5893 memset(&bgp_vrf
->rmac
, 0, sizeof(struct ethaddr
));
5894 memset(&bgp_vrf
->evpn_info
->pip_rmac_zebra
, 0, ETH_ALEN
);
5895 if (is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac_static
) &&
5896 !is_zero_mac(&bgp_vrf
->evpn_info
->pip_rmac
))
5897 memset(&bgp_vrf
->evpn_info
->pip_rmac
, 0, ETH_ALEN
);
5899 /* remove default import RT or Unmap non-default import RT */
5900 if (!list_isempty(bgp_vrf
->vrf_import_rtl
)) {
5901 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
5902 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_IMPORT_RT_CFGD
))
5903 list_delete_all_node(bgp_vrf
->vrf_import_rtl
);
5906 /* remove default export RT */
5907 if (!list_isempty(bgp_vrf
->vrf_export_rtl
) &&
5908 !CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_EXPORT_RT_CFGD
)) {
5909 list_delete_all_node(bgp_vrf
->vrf_export_rtl
);
5912 /* update all corresponding local mac-ip routes */
5913 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
)) {
5914 for (ALL_LIST_ELEMENTS_RO(bgp_vrf
->l2vnis
, node
, vpn
)) {
5915 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
5916 update_routes_for_vni(bgp_evpn
, vpn
);
5920 /* If any L2VNIs point to this instance, unlink them. */
5921 for (ALL_LIST_ELEMENTS(bgp_vrf
->l2vnis
, node
, next
, vpn
))
5922 bgpevpn_unlink_from_l3vni(vpn
);
5924 UNSET_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
);
5926 /* Delete the instance if it was autocreated */
5927 if (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_AUTO
))
5928 bgp_delete(bgp_vrf
);
5934 * Handle del of a local VNI.
5936 int bgp_evpn_local_vni_del(struct bgp
*bgp
, vni_t vni
)
5938 struct bgpevpn
*vpn
;
5940 /* Locate VNI hash */
5941 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5943 if (bgp_debug_zebra(NULL
))
5945 EC_BGP_EVPN_VPN_VNI
,
5946 "%u: VNI hash entry for VNI %u not found at DEL",
5951 /* Remove all local EVPN routes and schedule for processing (to
5952 * withdraw from peers).
5954 delete_routes_for_vni(bgp
, vpn
);
5956 bgp_evpn_unlink_from_vni_svi_hash(bgp
, vpn
);
5958 vpn
->svi_ifindex
= 0;
5960 * tunnel is no longer active, del tunnel ip address from tip_hash
5962 bgp_tip_del(bgp
, &vpn
->originator_ip
);
5964 /* Clear "live" flag and see if hash needs to be freed. */
5965 UNSET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
5966 if (!is_vni_configured(vpn
))
5967 bgp_evpn_free(bgp
, vpn
);
5973 * Handle add (or update) of a local VNI. The VNI changes we care
5974 * about are for the local-tunnel-ip and the (tenant) VRF.
5976 int bgp_evpn_local_vni_add(struct bgp
*bgp
, vni_t vni
,
5977 struct in_addr originator_ip
,
5978 vrf_id_t tenant_vrf_id
,
5979 struct in_addr mcast_grp
,
5980 ifindex_t svi_ifindex
)
5982 struct bgpevpn
*vpn
;
5983 struct prefix_evpn p
;
5985 /* Lookup VNI. If present and no change, exit. */
5986 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
5989 if (is_vni_live(vpn
)
5990 && IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
)
5991 && IPV4_ADDR_SAME(&vpn
->mcast_grp
, &mcast_grp
)
5992 && vpn
->tenant_vrf_id
== tenant_vrf_id
5993 && vpn
->svi_ifindex
== svi_ifindex
)
5994 /* Probably some other param has changed that we don't
5998 bgp_evpn_mcast_grp_change(bgp
, vpn
, mcast_grp
);
6000 if (vpn
->svi_ifindex
!= svi_ifindex
) {
6003 * Unresolve all the gateway IP nexthops for this VNI
6006 bgp_evpn_remote_ip_hash_iterate(
6008 (void (*)(struct hash_bucket
*, void *))
6009 bgp_evpn_remote_ip_hash_unlink_nexthop
,
6011 bgp_evpn_unlink_from_vni_svi_hash(bgp
, vpn
);
6012 vpn
->svi_ifindex
= svi_ifindex
;
6013 bgp_evpn_link_to_vni_svi_hash(bgp
, vpn
);
6016 * Resolve all the gateway IP nexthops for this VNI
6019 bgp_evpn_remote_ip_hash_iterate(
6021 (void (*)(struct hash_bucket
*, void *))
6022 bgp_evpn_remote_ip_hash_link_nexthop
,
6026 /* Update tenant_vrf_id if it has changed. */
6027 if (vpn
->tenant_vrf_id
!= tenant_vrf_id
) {
6030 * Unresolve all the gateway IP nexthops for this VNI
6033 bgp_evpn_remote_ip_hash_iterate(
6035 (void (*)(struct hash_bucket
*, void *))
6036 bgp_evpn_remote_ip_hash_unlink_nexthop
,
6038 bgpevpn_unlink_from_l3vni(vpn
);
6039 vpn
->tenant_vrf_id
= tenant_vrf_id
;
6040 bgpevpn_link_to_l3vni(vpn
);
6043 * Resolve all the gateway IP nexthops for this VNI
6046 bgp_evpn_remote_ip_hash_iterate(
6048 (void (*)(struct hash_bucket
*, void *))
6049 bgp_evpn_remote_ip_hash_link_nexthop
,
6053 /* If tunnel endpoint IP has changed, update (and delete prior
6054 * type-3 route, if needed.)
6056 if (!IPV4_ADDR_SAME(&vpn
->originator_ip
, &originator_ip
))
6057 handle_tunnel_ip_change(bgp
, vpn
, originator_ip
);
6059 /* Update all routes with new endpoint IP and/or export RT
6062 if (is_vni_live(vpn
))
6063 update_routes_for_vni(bgp
, vpn
);
6065 /* Create or update as appropriate. */
6066 vpn
= bgp_evpn_new(bgp
, vni
, originator_ip
, tenant_vrf_id
,
6067 mcast_grp
, svi_ifindex
);
6070 /* if the VNI is live already, there is nothing more to do */
6071 if (is_vni_live(vpn
))
6074 /* Mark as "live" */
6075 SET_FLAG(vpn
->flags
, VNI_FLAG_LIVE
);
6077 /* tunnel is now active, add tunnel-ip to db */
6078 bgp_tip_add(bgp
, &originator_ip
);
6080 /* filter routes as nexthop database has changed */
6081 bgp_filter_evpn_routes_upon_martian_nh_change(bgp
);
6084 * Create EVPN type-3 route and schedule for processing.
6086 * RT-3 only if doing head-end replication
6088 if (bgp_evpn_vni_flood_mode_get(bgp
, vpn
)
6089 == VXLAN_FLOOD_HEAD_END_REPL
) {
6090 build_evpn_type3_prefix(&p
, vpn
->originator_ip
);
6091 if (update_evpn_route(bgp
, vpn
, &p
, 0, 0, NULL
)) {
6092 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
6093 "%u: Type3 route creation failure for VNI %u",
6099 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
6103 install_routes_for_vni(bgp
, vpn
);
6105 /* If we are advertising gateway mac-ip
6106 It needs to be conveyed again to zebra */
6107 bgp_zebra_advertise_gw_macip(bgp
, vpn
->advertise_gw_macip
, vpn
->vni
);
6109 /* advertise svi mac-ip knob to zebra */
6110 bgp_zebra_advertise_svi_macip(bgp
, vpn
->advertise_svi_macip
, vpn
->vni
);
6116 * Handle change in setting for BUM handling. The supported values
6117 * are head-end replication and dropping all BUM packets. Any change
6118 * should be registered with zebra. Also, if doing head-end replication,
6119 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
6120 * to be dropped, the RT-3s must be withdrawn.
6122 void bgp_evpn_flood_control_change(struct bgp
*bgp
)
6124 zlog_info("L2VPN EVPN BUM handling is %s",
6125 bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
?
6126 "Flooding" : "Flooding Disabled");
6128 bgp_zebra_vxlan_flood_control(bgp
, bgp
->vxlan_flood_ctrl
);
6129 if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_HEAD_END_REPL
)
6130 hash_iterate(bgp
->vnihash
, create_advertise_type3
, bgp
);
6131 else if (bgp
->vxlan_flood_ctrl
== VXLAN_FLOOD_DISABLED
)
6132 hash_iterate(bgp
->vnihash
, delete_withdraw_type3
, bgp
);
6136 * Cleanup EVPN information on disable - Need to delete and withdraw
6137 * EVPN routes from peers.
6139 void bgp_evpn_cleanup_on_disable(struct bgp
*bgp
)
6141 hash_iterate(bgp
->vnihash
, (void (*)(struct hash_bucket
*,
6142 void *))cleanup_vni_on_disable
,
6147 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
6148 * BGP instance (default) is being freed.
6150 void bgp_evpn_cleanup(struct bgp
*bgp
)
6152 hash_iterate(bgp
->vnihash
,
6153 (void (*)(struct hash_bucket
*, void *))free_vni_entry
,
6156 hash_clean(bgp
->import_rt_hash
, (void (*)(void *))hash_import_rt_free
);
6157 hash_free(bgp
->import_rt_hash
);
6158 bgp
->import_rt_hash
= NULL
;
6160 hash_clean(bgp
->vrf_import_rt_hash
,
6161 (void (*)(void *))hash_vrf_import_rt_free
);
6162 hash_free(bgp
->vrf_import_rt_hash
);
6163 bgp
->vrf_import_rt_hash
= NULL
;
6165 hash_clean(bgp
->vni_svi_hash
, (void (*)(void *))hash_evpn_free
);
6166 hash_free(bgp
->vni_svi_hash
);
6167 bgp
->vni_svi_hash
= NULL
;
6168 hash_free(bgp
->vnihash
);
6169 bgp
->vnihash
= NULL
;
6171 list_delete(&bgp
->vrf_import_rtl
);
6172 list_delete(&bgp
->vrf_export_rtl
);
6173 list_delete(&bgp
->l2vnis
);
6177 * Initialization for EVPN
6180 * hash for RT to VNI
6182 void bgp_evpn_init(struct bgp
*bgp
)
6185 hash_create(vni_hash_key_make
, vni_hash_cmp
, "BGP VNI Hash");
6187 hash_create(vni_svi_hash_key_make
, vni_svi_hash_cmp
,
6188 "BGP VNI hash based on SVI ifindex");
6189 bgp
->import_rt_hash
=
6190 hash_create(import_rt_hash_key_make
, import_rt_hash_cmp
,
6191 "BGP Import RT Hash");
6192 bgp
->vrf_import_rt_hash
=
6193 hash_create(vrf_import_rt_hash_key_make
, vrf_import_rt_hash_cmp
,
6194 "BGP VRF Import RT Hash");
6195 bgp
->vrf_import_rtl
= list_new();
6196 bgp
->vrf_import_rtl
->cmp
=
6197 (int (*)(void *, void *))evpn_vrf_route_target_cmp
;
6198 bgp
->vrf_import_rtl
->del
= evpn_vrf_rt_del
;
6199 bgp
->vrf_export_rtl
= list_new();
6200 bgp
->vrf_export_rtl
->cmp
=
6201 (int (*)(void *, void *))evpn_vrf_route_target_cmp
;
6202 bgp
->vrf_export_rtl
->del
= evpn_vrf_rt_del
;
6203 bgp
->l2vnis
= list_new();
6204 bgp
->l2vnis
->cmp
= vni_list_cmp
;
6205 /* By default Duplicate Address Dection is enabled.
6206 * Max-moves (N) 5, detection time (M) 180
6207 * default action is warning-only
6208 * freeze action permanently freezes address,
6209 * and freeze time (auto-recovery) is disabled.
6211 if (bgp
->evpn_info
) {
6212 bgp
->evpn_info
->dup_addr_detect
= true;
6213 bgp
->evpn_info
->dad_time
= EVPN_DAD_DEFAULT_TIME
;
6214 bgp
->evpn_info
->dad_max_moves
= EVPN_DAD_DEFAULT_MAX_MOVES
;
6215 bgp
->evpn_info
->dad_freeze
= false;
6216 bgp
->evpn_info
->dad_freeze_time
= 0;
6217 /* Initialize zebra vxlan */
6218 bgp_zebra_dup_addr_detection(bgp
);
6219 /* Enable PIP feature by default for bgp vrf instance */
6220 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
6221 struct bgp
*bgp_default
;
6223 bgp
->evpn_info
->advertise_pip
= true;
6224 bgp_default
= bgp_get_default();
6226 bgp
->evpn_info
->pip_ip
= bgp_default
->router_id
;
6230 /* Default BUM handling is to do head-end replication. */
6231 bgp
->vxlan_flood_ctrl
= VXLAN_FLOOD_HEAD_END_REPL
;
6233 bgp_evpn_nh_init(bgp
);
6236 void bgp_evpn_vrf_delete(struct bgp
*bgp_vrf
)
6238 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf
);
6239 bgp_evpn_nh_finish(bgp_vrf
);
6243 * Get the prefixlen of the ip prefix carried within the type5 evpn route.
6245 int bgp_evpn_get_type5_prefixlen(const struct prefix
*pfx
)
6247 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6249 if (!pfx
|| pfx
->family
!= AF_EVPN
)
6252 if (evp
->prefix
.route_type
!= BGP_EVPN_IP_PREFIX_ROUTE
)
6255 return evp
->prefix
.prefix_addr
.ip_prefix_length
;
6259 * Should we register nexthop for this EVPN prefix for nexthop tracking?
6261 bool bgp_evpn_is_prefix_nht_supported(const struct prefix
*pfx
)
6263 struct prefix_evpn
*evp
= (struct prefix_evpn
*)pfx
;
6266 * EVPN routes should be marked as valid only if the nexthop is
6267 * reachable. Only if this happens, the route should be imported
6268 * (into VNI or VRF routing tables) and/or advertised.
6269 * Note: This is currently applied for EVPN type-1, type-2,
6270 * type-3, type-4 and type-5 routes.
6271 * It may be tweaked later on for other routes, or
6272 * even removed completely when all routes are handled.
6274 if (pfx
&& pfx
->family
== AF_EVPN
6275 && (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
6276 || evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
6277 || evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
6278 || evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
6279 || evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
))
6285 static void *bgp_evpn_remote_ip_hash_alloc(void *p
)
6287 const struct evpn_remote_ip
*key
= (const struct evpn_remote_ip
*)p
;
6288 struct evpn_remote_ip
*ip
;
6290 ip
= XMALLOC(MTYPE_EVPN_REMOTE_IP
, sizeof(struct evpn_remote_ip
));
6292 ip
->macip_path_list
= list_new();
6297 static unsigned int bgp_evpn_remote_ip_hash_key_make(const void *p
)
6299 const struct evpn_remote_ip
*ip
= p
;
6300 const struct ipaddr
*addr
= &ip
->addr
;
6302 if (IS_IPADDR_V4(addr
))
6303 return jhash_1word(addr
->ipaddr_v4
.s_addr
, 0);
6305 return jhash2(addr
->ipaddr_v6
.s6_addr32
,
6306 array_size(addr
->ipaddr_v6
.s6_addr32
), 0);
6309 static bool bgp_evpn_remote_ip_hash_cmp(const void *p1
, const void *p2
)
6311 const struct evpn_remote_ip
*ip1
= p1
;
6312 const struct evpn_remote_ip
*ip2
= p2
;
6314 return !ipaddr_cmp(&ip1
->addr
, &ip2
->addr
);
6317 static void bgp_evpn_remote_ip_hash_init(struct bgpevpn
*vpn
)
6319 if (!evpn_resolve_overlay_index())
6322 vpn
->remote_ip_hash
= hash_create(bgp_evpn_remote_ip_hash_key_make
,
6323 bgp_evpn_remote_ip_hash_cmp
,
6324 "BGP EVPN remote IP hash");
6327 static void bgp_evpn_remote_ip_hash_free(struct hash_bucket
*bucket
, void *args
)
6329 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6330 struct bgpevpn
*vpn
= (struct bgpevpn
*)args
;
6332 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, false);
6334 list_delete(&ip
->macip_path_list
);
6336 hash_release(vpn
->remote_ip_hash
, ip
);
6337 XFREE(MTYPE_EVPN_REMOTE_IP
, ip
);
6340 static void bgp_evpn_remote_ip_hash_destroy(struct bgpevpn
*vpn
)
6342 if (!evpn_resolve_overlay_index() || vpn
->remote_ip_hash
== NULL
)
6345 hash_iterate(vpn
->remote_ip_hash
,
6346 (void (*)(struct hash_bucket
*, void *))bgp_evpn_remote_ip_hash_free
,
6349 hash_free(vpn
->remote_ip_hash
);
6350 vpn
->remote_ip_hash
= NULL
;
6353 /* Add a remote MAC/IP route to hash table */
6354 static void bgp_evpn_remote_ip_hash_add(struct bgpevpn
*vpn
,
6355 struct bgp_path_info
*pi
)
6357 struct evpn_remote_ip tmp
;
6358 struct evpn_remote_ip
*ip
;
6359 struct prefix_evpn
*evp
;
6361 if (!evpn_resolve_overlay_index())
6364 if (pi
->type
!= ZEBRA_ROUTE_BGP
|| pi
->sub_type
!= BGP_ROUTE_IMPORTED
6365 || !CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
))
6368 evp
= (struct prefix_evpn
*)&pi
->net
->p
;
6370 if (evp
->family
!= AF_EVPN
6371 || evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
6372 || is_evpn_prefix_ipaddr_none(evp
))
6375 tmp
.addr
= evp
->prefix
.macip_addr
.ip
;
6376 ip
= hash_lookup(vpn
->remote_ip_hash
, &tmp
);
6378 if (listnode_lookup(ip
->macip_path_list
, pi
) != NULL
)
6380 (void)listnode_add(ip
->macip_path_list
, pi
);
6384 ip
= hash_get(vpn
->remote_ip_hash
, &tmp
, bgp_evpn_remote_ip_hash_alloc
);
6385 (void)listnode_add(ip
->macip_path_list
, pi
);
6387 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, true);
6390 /* Delete a remote MAC/IP route from hash table */
6391 static void bgp_evpn_remote_ip_hash_del(struct bgpevpn
*vpn
,
6392 struct bgp_path_info
*pi
)
6394 struct evpn_remote_ip tmp
;
6395 struct evpn_remote_ip
*ip
;
6396 struct prefix_evpn
*evp
;
6398 if (!evpn_resolve_overlay_index())
6401 evp
= (struct prefix_evpn
*)&pi
->net
->p
;
6403 if (evp
->family
!= AF_EVPN
6404 || evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
6405 || is_evpn_prefix_ipaddr_none(evp
))
6408 tmp
.addr
= evp
->prefix
.macip_addr
.ip
;
6409 ip
= hash_lookup(vpn
->remote_ip_hash
, &tmp
);
6413 listnode_delete(ip
->macip_path_list
, pi
);
6415 if (ip
->macip_path_list
->count
== 0) {
6416 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, false);
6417 hash_release(vpn
->remote_ip_hash
, ip
);
6418 XFREE(MTYPE_EVPN_REMOTE_IP
, ip
);
6422 static void bgp_evpn_remote_ip_hash_iterate(struct bgpevpn
*vpn
,
6423 void (*func
)(struct hash_bucket
*,
6427 if (!evpn_resolve_overlay_index())
6430 hash_iterate(vpn
->remote_ip_hash
, func
, arg
);
6433 static void show_remote_ip_entry(struct hash_bucket
*bucket
, void *args
)
6435 char buf
[INET6_ADDRSTRLEN
];
6436 struct listnode
*node
= NULL
;
6437 struct bgp_path_info
*pi
= NULL
;
6438 struct vty
*vty
= (struct vty
*)args
;
6439 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6441 vty_out(vty
, " Remote IP: %s\n",
6442 ipaddr2str(&ip
->addr
, buf
, sizeof(buf
)));
6443 vty_out(vty
, " Linked MAC/IP routes:\n");
6444 for (ALL_LIST_ELEMENTS_RO(ip
->macip_path_list
, node
, pi
))
6445 vty_out(vty
, " %pFX\n", &pi
->net
->p
);
6448 void bgp_evpn_show_remote_ip_hash(struct hash_bucket
*bucket
, void *args
)
6450 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
6451 struct vty
*vty
= (struct vty
*)args
;
6453 vty_out(vty
, "VNI: %u\n", vpn
->vni
);
6454 bgp_evpn_remote_ip_hash_iterate(
6456 (void (*)(struct hash_bucket
*, void *))show_remote_ip_entry
,
6461 static void bgp_evpn_remote_ip_hash_link_nexthop(struct hash_bucket
*bucket
,
6464 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6465 struct bgpevpn
*vpn
= (struct bgpevpn
*)args
;
6467 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, true);
6470 static void bgp_evpn_remote_ip_hash_unlink_nexthop(struct hash_bucket
*bucket
,
6473 struct evpn_remote_ip
*ip
= (struct evpn_remote_ip
*)bucket
->data
;
6474 struct bgpevpn
*vpn
= (struct bgpevpn
*)args
;
6476 bgp_evpn_remote_ip_process_nexthops(vpn
, &ip
->addr
, false);
6479 static unsigned int vni_svi_hash_key_make(const void *p
)
6481 const struct bgpevpn
*vpn
= p
;
6483 return jhash_1word(vpn
->svi_ifindex
, 0);
6486 static bool vni_svi_hash_cmp(const void *p1
, const void *p2
)
6488 const struct bgpevpn
*vpn1
= p1
;
6489 const struct bgpevpn
*vpn2
= p2
;
6491 return (vpn1
->svi_ifindex
== vpn2
->svi_ifindex
);
6494 static struct bgpevpn
*bgp_evpn_vni_svi_hash_lookup(struct bgp
*bgp
,
6497 struct bgpevpn
*vpn
;
6500 memset(&tmp
, 0, sizeof(tmp
));
6501 tmp
.svi_ifindex
= svi
;
6502 vpn
= hash_lookup(bgp
->vni_svi_hash
, &tmp
);
6506 static void bgp_evpn_link_to_vni_svi_hash(struct bgp
*bgp
, struct bgpevpn
*vpn
)
6508 if (vpn
->svi_ifindex
== 0)
6511 (void)hash_get(bgp
->vni_svi_hash
, vpn
, hash_alloc_intern
);
6514 static void bgp_evpn_unlink_from_vni_svi_hash(struct bgp
*bgp
,
6515 struct bgpevpn
*vpn
)
6517 if (vpn
->svi_ifindex
== 0)
6520 hash_release(bgp
->vni_svi_hash
, vpn
);
6523 void bgp_evpn_show_vni_svi_hash(struct hash_bucket
*bucket
, void *args
)
6525 struct bgpevpn
*evpn
= (struct bgpevpn
*)bucket
->data
;
6526 struct vty
*vty
= (struct vty
*)args
;
6528 vty_out(vty
, "SVI: %u VNI: %u\n", evpn
->svi_ifindex
, evpn
->vni
);
6532 * This function is called for a bgp_nexthop_cache entry when the nexthop is
6533 * gateway IP overlay index.
6534 * This function returns true if there is a remote MAC/IP route for the gateway
6535 * IP in the EVI of the nexthop SVI.
6537 bool bgp_evpn_is_gateway_ip_resolved(struct bgp_nexthop_cache
*bnc
)
6539 struct bgp
*bgp_evpn
= NULL
;
6540 struct bgpevpn
*vpn
= NULL
;
6541 struct evpn_remote_ip tmp
;
6544 if (!evpn_resolve_overlay_index())
6547 if (!bnc
->nexthop
|| bnc
->nexthop
->ifindex
== 0)
6550 bgp_evpn
= bgp_get_evpn();
6555 * Gateway IP is resolved by nht over SVI interface.
6556 * Use this SVI to find corresponding EVI(L2 context)
6558 vpn
= bgp_evpn_vni_svi_hash_lookup(bgp_evpn
, bnc
->nexthop
->ifindex
);
6562 if (vpn
->bgp_vrf
!= bnc
->bgp
)
6566 * Check if the gateway IP is present in the EVI remote_ip_hash table
6567 * which stores all the remote IP addresses received via MAC/IP routes
6570 memset(&tmp
, 0, sizeof(tmp
));
6573 if (p
->family
== AF_INET
) {
6574 tmp
.addr
.ipa_type
= IPADDR_V4
;
6575 memcpy(&(tmp
.addr
.ipaddr_v4
), &(p
->u
.prefix4
),
6576 sizeof(struct in_addr
));
6577 } else if (p
->family
== AF_INET6
) {
6578 tmp
.addr
.ipa_type
= IPADDR_V6
;
6579 memcpy(&(tmp
.addr
.ipaddr_v6
), &(p
->u
.prefix6
),
6580 sizeof(struct in6_addr
));
6584 if (hash_lookup(vpn
->remote_ip_hash
, &tmp
) == NULL
)
6590 /* Resolve/Unresolve nexthops when a MAC/IP route is added/deleted */
6591 static void bgp_evpn_remote_ip_process_nexthops(struct bgpevpn
*vpn
,
6592 struct ipaddr
*addr
,
6597 struct bgp_nexthop_cache
*bnc
;
6598 struct bgp_nexthop_cache_head
*tree
= NULL
;
6600 if (!vpn
->bgp_vrf
|| vpn
->svi_ifindex
== 0)
6603 memset(&p
, 0, sizeof(p
));
6605 if (addr
->ipa_type
== IPADDR_V4
) {
6608 memcpy(&(p
.u
.prefix4
), &(addr
->ipaddr_v4
),
6609 sizeof(struct in_addr
));
6610 p
.prefixlen
= IPV4_MAX_BITLEN
;
6611 } else if (addr
->ipa_type
== IPADDR_V6
) {
6613 p
.family
= AF_INET6
;
6614 memcpy(&(p
.u
.prefix6
), &(addr
->ipaddr_v6
),
6615 sizeof(struct in6_addr
));
6616 p
.prefixlen
= IPV6_MAX_BITLEN
;
6620 tree
= &vpn
->bgp_vrf
->nexthop_cache_table
[afi
];
6621 bnc
= bnc_find(tree
, &p
, 0, 0);
6623 if (!bnc
|| !bnc
->is_evpn_gwip_nexthop
)
6626 if (!bnc
->nexthop
|| bnc
->nexthop
->ifindex
!= vpn
->svi_ifindex
)
6629 if (BGP_DEBUG(nht
, NHT
))
6630 zlog_debug("%s(%u): vni %u mac/ip %s for NH %pFX",
6631 vpn
->bgp_vrf
->name_pretty
, vpn
->tenant_vrf_id
,
6632 vpn
->vni
, (resolve
? "add" : "delete"),
6636 * MAC/IP route or SVI or tenant vrf being added to EVI.
6637 * Set nexthop as valid only if it is already L3 reachable
6639 if (resolve
&& bnc
->flags
& BGP_NEXTHOP_EVPN_INCOMPLETE
) {
6640 bnc
->flags
&= ~BGP_NEXTHOP_EVPN_INCOMPLETE
;
6641 bnc
->flags
|= BGP_NEXTHOP_VALID
;
6642 bnc
->change_flags
|= BGP_NEXTHOP_MACIP_CHANGED
;
6643 evaluate_paths(bnc
);
6646 /* MAC/IP route or SVI or tenant vrf being deleted from EVI */
6647 if (!resolve
&& bnc
->flags
& BGP_NEXTHOP_VALID
) {
6648 bnc
->flags
&= ~BGP_NEXTHOP_VALID
;
6649 bnc
->flags
|= BGP_NEXTHOP_EVPN_INCOMPLETE
;
6650 bnc
->change_flags
|= BGP_NEXTHOP_MACIP_CHANGED
;
6651 evaluate_paths(bnc
);
6655 void bgp_evpn_handle_resolve_overlay_index_set(struct hash_bucket
*bucket
,
6658 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
6659 struct bgp_dest
*dest
;
6660 struct bgp_path_info
*pi
;
6662 bgp_evpn_remote_ip_hash_init(vpn
);
6664 for (dest
= bgp_table_top(vpn
->route_table
); dest
;
6665 dest
= bgp_route_next(dest
))
6666 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
6667 bgp_evpn_remote_ip_hash_add(vpn
, pi
);
6670 void bgp_evpn_handle_resolve_overlay_index_unset(struct hash_bucket
*bucket
,
6673 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
6675 bgp_evpn_remote_ip_hash_destroy(vpn
);