1 /* BGP EVPN internal definitions
2 * Copyright (C) 2017 Cumulus Networks, Inc.
4 * This file is part of FRR.
6 * FRR is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * FRR is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with FRR; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 #ifndef _BGP_EVPN_PRIVATE_H
23 #define _BGP_EVPN_PRIVATE_H
28 #include "bgpd/bgpd.h"
29 #include "bgpd/bgp_ecommunity.h"
31 #define RT_ADDRSTRLEN 28
33 /* EVPN prefix lengths. This represents the sizeof struct evpn_addr
35 #define EVPN_ROUTE_PREFIXLEN (sizeof(struct evpn_addr) * 8)
37 /* EVPN route types. */
39 BGP_EVPN_AD_ROUTE
= 1, /* Ethernet Auto-Discovery (A-D) route */
40 BGP_EVPN_MAC_IP_ROUTE
, /* MAC/IP Advertisement route */
41 BGP_EVPN_IMET_ROUTE
, /* Inclusive Multicast Ethernet Tag route */
42 BGP_EVPN_ES_ROUTE
, /* Ethernet Segment route */
43 BGP_EVPN_IP_PREFIX_ROUTE
, /* IP Prefix route */
44 } bgp_evpn_route_type
;
47 * Hash table of EVIs. Right now, the only type of EVI supported is with
48 * VxLAN encapsulation, hence each EVI corresponds to a L2 VNI.
49 * The VNIs are not "created" through BGP but through some other interface
50 * on the system. This table stores VNIs that BGP comes to know as present
51 * on the system (through interaction with zebra) as well as pre-configured
52 * VNIs (which need to be defined in the system to become "live").
56 vrf_id_t tenant_vrf_id
;
58 #define VNI_FLAG_CFGD 0x1 /* VNI is user configured */
59 #define VNI_FLAG_LIVE 0x2 /* VNI is "live" */
60 #define VNI_FLAG_RD_CFGD 0x4 /* RD is user configured. */
61 #define VNI_FLAG_IMPRT_CFGD 0x8 /* Import RT is user configured */
62 #define VNI_FLAG_EXPRT_CFGD 0x10 /* Export RT is user configured */
63 #define VNI_FLAG_USE_TWO_LABELS 0x20 /* Attach both L2-VNI and L3-VNI if
64 needed for this VPN */
66 struct bgp
*bgp_vrf
; /* back pointer to the vrf instance */
68 /* Flag to indicate if we are
69 * advertising the g/w mac ip for
71 uint8_t advertise_gw_macip
;
73 /* Flag to indicate if we are
74 * advertising subnet for this VNI */
75 uint8_t advertise_subnet
;
77 /* Flag to indicate if we are advertising the svi mac ip for this VNI*/
78 uint8_t advertise_svi_macip
;
80 /* Id for deriving the RD
81 * automatically for this VNI */
84 /* RD for this VNI. */
87 /* Route type 3 field */
88 struct in_addr originator_ip
;
90 /* PIM-SM MDT group for BUM flooding */
91 struct in_addr mcast_grp
;
93 /* Import and Export RTs. */
94 struct list
*import_rtl
;
95 struct list
*export_rtl
;
97 /* Route table for EVPN routes for
99 struct bgp_table
*route_table
;
104 DECLARE_QOBJ_TYPE(bgpevpn
)
108 /* Ethernet Segment Identifier */
113 #define EVPNES_LOCAL 0x01
114 #define EVPNES_REMOTE 0x02
117 * Id for deriving the RD
118 * automatically for this ESI
122 /* RD for this VNI. */
123 struct prefix_rd prd
;
125 /* originator ip address */
126 struct ipaddr originator_ip
;
128 /* list of VTEPs in the same site */
129 struct list
*vtep_list
;
132 * Route table for EVPN routes for
133 * this ESI. - type4 routes
135 struct bgp_table
*route_table
;
140 DECLARE_QOBJ_TYPE(evpnes
)
142 /* Mapping of Import RT to VNIs.
143 * The Import RTs of all VNIs are maintained in a hash table with each
144 * RT linking to all VNIs that will import routes matching this RT.
148 struct ecommunity_val rt
;
150 /* List of VNIs importing routes matching this RT. */
154 /* Mapping of Import RT to VRFs.
155 * The Import RTs of all VRFss are maintained in a hash table with each
156 * RT linking to all VRFs that will import routes matching this RT.
158 struct vrf_irt_node
{
160 struct ecommunity_val rt
;
162 /* List of VNIs importing routes matching this RT. */
167 #define RT_TYPE_IMPORT 1
168 #define RT_TYPE_EXPORT 2
169 #define RT_TYPE_BOTH 3
171 #define EVPN_DAD_DEFAULT_TIME 180 /* secs */
172 #define EVPN_DAD_DEFAULT_MAX_MOVES 5 /* default from RFC 7432 */
173 #define EVPN_DAD_DEFAULT_AUTO_RECOVERY_TIME 1800 /* secs */
175 struct bgp_evpn_info
{
176 /* enable disable dup detect */
177 bool dup_addr_detect
;
179 /* Detection time(M) */
181 /* Detection max moves(N) */
182 uint32_t dad_max_moves
;
183 /* Permanent freeze */
186 uint32_t dad_freeze_time
;
188 /* EVPN enable - advertise svi macip routes */
189 int advertise_svi_macip
;
191 /* PIP feature knob */
193 /* PIP IP (sys ip) */
194 struct in_addr pip_ip
;
195 struct in_addr pip_ip_static
;
196 /* PIP MAC (sys MAC) */
197 struct ethaddr pip_rmac
;
198 struct ethaddr pip_rmac_static
;
199 struct ethaddr pip_rmac_zebra
;
203 static inline int is_vrf_rd_configured(struct bgp
*bgp_vrf
)
205 return (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_RD_CFGD
));
208 static inline int bgp_evpn_vrf_rd_matches_existing(struct bgp
*bgp_vrf
,
209 struct prefix_rd
*prd
)
211 return (memcmp(&bgp_vrf
->vrf_prd
.val
, prd
->val
, ECOMMUNITY_SIZE
) == 0);
214 static inline vni_t
bgpevpn_get_l3vni(struct bgpevpn
*vpn
)
216 return vpn
->bgp_vrf
? vpn
->bgp_vrf
->l3vni
: 0;
219 static inline void bgpevpn_get_rmac(struct bgpevpn
*vpn
, struct ethaddr
*rmac
)
221 memset(rmac
, 0, sizeof(struct ethaddr
));
224 memcpy(rmac
, &vpn
->bgp_vrf
->rmac
, sizeof(struct ethaddr
));
227 static inline struct list
*bgpevpn_get_vrf_export_rtl(struct bgpevpn
*vpn
)
232 return vpn
->bgp_vrf
->vrf_export_rtl
;
235 static inline struct list
*bgpevpn_get_vrf_import_rtl(struct bgpevpn
*vpn
)
240 return vpn
->bgp_vrf
->vrf_import_rtl
;
243 static inline void bgpevpn_unlink_from_l3vni(struct bgpevpn
*vpn
)
245 /* bail if vpn is not associated to bgp_vrf */
249 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
250 listnode_delete(vpn
->bgp_vrf
->l2vnis
, vpn
);
252 /* remove the backpointer to the vrf instance */
253 bgp_unlock(vpn
->bgp_vrf
);
257 static inline void bgpevpn_link_to_l3vni(struct bgpevpn
*vpn
)
259 struct bgp
*bgp_vrf
= NULL
;
261 /* bail if vpn is already associated to vrf */
265 bgp_vrf
= bgp_lookup_by_vrf_id(vpn
->tenant_vrf_id
);
269 /* associate the vpn to the bgp_vrf instance */
270 vpn
->bgp_vrf
= bgp_lock(bgp_vrf
);
271 listnode_add_sort(bgp_vrf
->l2vnis
, vpn
);
273 /* check if we are advertising two labels for this vpn */
274 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
))
275 SET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
278 static inline int is_vni_configured(struct bgpevpn
*vpn
)
280 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_CFGD
));
283 static inline int is_vni_live(struct bgpevpn
*vpn
)
285 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_LIVE
));
288 static inline int is_l3vni_live(struct bgp
*bgp_vrf
)
290 return (bgp_vrf
->l3vni
&& bgp_vrf
->l3vni_svi_ifindex
);
293 static inline int is_rd_configured(struct bgpevpn
*vpn
)
295 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
));
298 static inline int bgp_evpn_rd_matches_existing(struct bgpevpn
*vpn
,
299 struct prefix_rd
*prd
)
301 return (memcmp(&vpn
->prd
.val
, prd
->val
, ECOMMUNITY_SIZE
) == 0);
304 static inline int is_import_rt_configured(struct bgpevpn
*vpn
)
306 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
));
309 static inline int is_export_rt_configured(struct bgpevpn
*vpn
)
311 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
));
314 static inline int is_vni_param_configured(struct bgpevpn
*vpn
)
316 return (is_rd_configured(vpn
) || is_import_rt_configured(vpn
)
317 || is_export_rt_configured(vpn
));
320 static inline void encode_es_rt_extcomm(struct ecommunity_val
*eval
,
323 memset(eval
, 0, sizeof(struct ecommunity_val
));
324 eval
->val
[0] = ECOMMUNITY_ENCODE_EVPN
;
325 eval
->val
[1] = ECOMMUNITY_EVPN_SUBTYPE_ES_IMPORT_RT
;
326 memcpy(&eval
->val
[2], mac
, ETH_ALEN
);
329 static inline void encode_rmac_extcomm(struct ecommunity_val
*eval
,
330 struct ethaddr
*rmac
)
332 memset(eval
, 0, sizeof(*eval
));
333 eval
->val
[0] = ECOMMUNITY_ENCODE_EVPN
;
334 eval
->val
[1] = ECOMMUNITY_EVPN_SUBTYPE_ROUTERMAC
;
335 memcpy(&eval
->val
[2], rmac
, ETH_ALEN
);
338 static inline void encode_default_gw_extcomm(struct ecommunity_val
*eval
)
340 memset(eval
, 0, sizeof(*eval
));
341 eval
->val
[0] = ECOMMUNITY_ENCODE_OPAQUE
;
342 eval
->val
[1] = ECOMMUNITY_EVPN_SUBTYPE_DEF_GW
;
345 static inline void encode_mac_mobility_extcomm(int static_mac
, uint32_t seq
,
346 struct ecommunity_val
*eval
)
348 memset(eval
, 0, sizeof(*eval
));
349 eval
->val
[0] = ECOMMUNITY_ENCODE_EVPN
;
350 eval
->val
[1] = ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
;
352 eval
->val
[2] = ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY_FLAG_STICKY
;
353 eval
->val
[4] = (seq
>> 24) & 0xff;
354 eval
->val
[5] = (seq
>> 16) & 0xff;
355 eval
->val
[6] = (seq
>> 8) & 0xff;
356 eval
->val
[7] = seq
& 0xff;
359 static inline void encode_na_flag_extcomm(struct ecommunity_val
*eval
,
362 memset(eval
, 0, sizeof(*eval
));
363 eval
->val
[0] = ECOMMUNITY_ENCODE_EVPN
;
364 eval
->val
[1] = ECOMMUNITY_EVPN_SUBTYPE_ND
;
366 eval
->val
[2] |= ECOMMUNITY_EVPN_SUBTYPE_ND_ROUTER_FLAG
;
369 static inline void ip_prefix_from_type5_prefix(struct prefix_evpn
*evp
,
372 memset(ip
, 0, sizeof(struct prefix
));
373 if (is_evpn_prefix_ipaddr_v4(evp
)) {
374 ip
->family
= AF_INET
;
375 ip
->prefixlen
= evp
->prefix
.prefix_addr
.ip_prefix_length
;
376 memcpy(&(ip
->u
.prefix4
), &(evp
->prefix
.prefix_addr
.ip
.ip
),
378 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
379 ip
->family
= AF_INET6
;
380 ip
->prefixlen
= evp
->prefix
.prefix_addr
.ip_prefix_length
;
381 memcpy(&(ip
->u
.prefix6
), &(evp
->prefix
.prefix_addr
.ip
.ip
),
386 static inline int is_evpn_prefix_default(const struct prefix
*evp
)
388 if (evp
->family
!= AF_EVPN
)
391 return ((evp
->u
.prefix_evpn
.prefix_addr
.ip_prefix_length
== 0) ?
395 static inline void ip_prefix_from_type2_prefix(struct prefix_evpn
*evp
,
398 memset(ip
, 0, sizeof(struct prefix
));
399 if (is_evpn_prefix_ipaddr_v4(evp
)) {
400 ip
->family
= AF_INET
;
401 ip
->prefixlen
= IPV4_MAX_BITLEN
;
402 memcpy(&(ip
->u
.prefix4
), &(evp
->prefix
.macip_addr
.ip
.ip
),
404 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
405 ip
->family
= AF_INET6
;
406 ip
->prefixlen
= IPV6_MAX_BITLEN
;
407 memcpy(&(ip
->u
.prefix6
), &(evp
->prefix
.macip_addr
.ip
.ip
),
412 static inline void ip_prefix_from_evpn_prefix(struct prefix_evpn
*evp
,
415 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
416 ip_prefix_from_type2_prefix(evp
, ip
);
417 else if (evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
)
418 ip_prefix_from_type5_prefix(evp
, ip
);
421 static inline void build_evpn_type2_prefix(struct prefix_evpn
*p
,
425 memset(p
, 0, sizeof(struct prefix_evpn
));
427 p
->prefixlen
= EVPN_ROUTE_PREFIXLEN
;
428 p
->prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
429 memcpy(&p
->prefix
.macip_addr
.mac
.octet
, mac
->octet
, ETH_ALEN
);
430 p
->prefix
.macip_addr
.ip
.ipa_type
= IPADDR_NONE
;
432 memcpy(&p
->prefix
.macip_addr
.ip
, ip
, sizeof(*ip
));
435 static inline void build_type5_prefix_from_ip_prefix(struct prefix_evpn
*evp
,
436 struct prefix
*ip_prefix
)
440 memset(&ip
, 0, sizeof(struct ipaddr
));
441 if (ip_prefix
->family
== AF_INET
) {
442 ip
.ipa_type
= IPADDR_V4
;
443 memcpy(&ip
.ipaddr_v4
, &ip_prefix
->u
.prefix4
,
444 sizeof(struct in_addr
));
446 ip
.ipa_type
= IPADDR_V6
;
447 memcpy(&ip
.ipaddr_v6
, &ip_prefix
->u
.prefix6
,
448 sizeof(struct in6_addr
));
451 memset(evp
, 0, sizeof(struct prefix_evpn
));
452 evp
->family
= AF_EVPN
;
453 evp
->prefixlen
= EVPN_ROUTE_PREFIXLEN
;
454 evp
->prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
455 evp
->prefix
.prefix_addr
.ip_prefix_length
= ip_prefix
->prefixlen
;
456 evp
->prefix
.prefix_addr
.ip
.ipa_type
= ip
.ipa_type
;
457 memcpy(&evp
->prefix
.prefix_addr
.ip
, &ip
, sizeof(struct ipaddr
));
460 static inline void build_evpn_type3_prefix(struct prefix_evpn
*p
,
461 struct in_addr originator_ip
)
463 memset(p
, 0, sizeof(struct prefix_evpn
));
465 p
->prefixlen
= EVPN_ROUTE_PREFIXLEN
;
466 p
->prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
467 p
->prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
468 p
->prefix
.imet_addr
.ip
.ipaddr_v4
= originator_ip
;
471 static inline void build_evpn_type4_prefix(struct prefix_evpn
*p
,
473 struct in_addr originator_ip
)
475 memset(p
, 0, sizeof(struct prefix_evpn
));
477 p
->prefixlen
= EVPN_ROUTE_PREFIXLEN
;
478 p
->prefix
.route_type
= BGP_EVPN_ES_ROUTE
;
479 p
->prefix
.es_addr
.ip_prefix_length
= IPV4_MAX_BITLEN
;
480 p
->prefix
.es_addr
.ip
.ipa_type
= IPADDR_V4
;
481 p
->prefix
.es_addr
.ip
.ipaddr_v4
= originator_ip
;
482 memcpy(&p
->prefix
.es_addr
.esi
, esi
, sizeof(esi_t
));
485 static inline int evpn_default_originate_set(struct bgp
*bgp
, afi_t afi
,
489 CHECK_FLAG(bgp
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
490 BGP_L2VPN_EVPN_DEFAULT_ORIGINATE_IPV4
))
492 else if (afi
== AFI_IP6
&&
493 CHECK_FLAG(bgp
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
494 BGP_L2VPN_EVPN_DEFAULT_ORIGINATE_IPV6
))
499 static inline void es_get_system_mac(esi_t
*esi
,
503 * for type-1 and type-3 ESIs,
504 * the system mac starts at val[1]
506 memcpy(mac
, &esi
->val
[1], ETH_ALEN
);
509 static inline int is_es_local(struct evpnes
*es
)
511 return CHECK_FLAG(es
->flags
, EVPNES_LOCAL
) ? 1 : 0;
514 static inline bool bgp_evpn_is_svi_macip_enabled(struct bgpevpn
*vpn
)
516 struct bgp
*bgp_evpn
= NULL
;
518 bgp_evpn
= bgp_get_evpn();
520 return (bgp_evpn
->evpn_info
->advertise_svi_macip
||
521 vpn
->advertise_svi_macip
);
524 extern void bgp_evpn_install_uninstall_default_route(struct bgp
*bgp_vrf
,
525 afi_t afi
, safi_t safi
,
527 extern void evpn_rt_delete_auto(struct bgp
*, vni_t
, struct list
*);
528 extern void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
529 struct ecommunity
*ecomadd
);
530 extern void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
531 struct ecommunity
*ecomdel
);
532 extern void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
533 struct ecommunity
*ecomadd
);
534 extern void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
535 struct ecommunity
*ecomdel
);
536 extern int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
,
537 struct bgpevpn
*vpn
);
538 extern void bgp_evpn_handle_autort_change(struct bgp
*bgp
);
539 extern void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
);
540 extern void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
542 extern int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
543 extern int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
544 extern void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
);
545 extern void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
);
546 extern void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
);
547 extern void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
,
548 struct bgpevpn
*vpn
);
549 extern void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
,
550 struct bgpevpn
*vpn
);
551 extern void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
,
552 struct bgpevpn
*vpn
);
553 extern void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
);
554 extern void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
);
555 extern struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
);
556 extern struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
557 struct in_addr originator_ip
,
558 vrf_id_t tenant_vrf_id
,
559 struct in_addr mcast_grp
);
560 extern void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
);
561 extern struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
);
562 extern struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
, esi_t
*esi
,
563 struct ipaddr
*originator_ip
);
564 extern void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
);
565 extern bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni
);
566 extern int update_routes_for_vni(struct bgp
*bgp
, struct bgpevpn
*vpn
);
567 #endif /* _BGP_EVPN_PRIVATE_H */