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 reprsent the sizeof struct prefix_evpn */
34 #define EVPN_TYPE_2_ROUTE_PREFIXLEN 224
35 #define EVPN_TYPE_3_ROUTE_PREFIXLEN 224
36 #define EVPN_TYPE_5_ROUTE_PREFIXLEN 224
38 /* EVPN route types. */
40 BGP_EVPN_AD_ROUTE
= 1, /* Ethernet Auto-Discovery (A-D) route */
41 BGP_EVPN_MAC_IP_ROUTE
, /* MAC/IP Advertisement route */
42 BGP_EVPN_IMET_ROUTE
, /* Inclusive Multicast Ethernet Tag route */
43 BGP_EVPN_ES_ROUTE
, /* Ethernet Segment route */
44 BGP_EVPN_IP_PREFIX_ROUTE
, /* IP Prefix route */
45 } bgp_evpn_route_type
;
48 * Hash table of EVIs. Right now, the only type of EVI supported is with
49 * VxLAN encapsulation, hence each EVI corresponds to a L2 VNI.
50 * The VNIs are not "created" through BGP but through some other interface
51 * on the system. This table stores VNIs that BGP comes to know as present
52 * on the system (through interaction with zebra) as well as pre-configured
53 * VNIs (which need to be defined in the system to become "live").
57 vrf_id_t tenant_vrf_id
;
59 #define VNI_FLAG_CFGD 0x1 /* VNI is user configured */
60 #define VNI_FLAG_LIVE 0x2 /* VNI is "live" */
61 #define VNI_FLAG_RD_CFGD 0x4 /* RD is user configured. */
62 #define VNI_FLAG_IMPRT_CFGD 0x8 /* Import RT is user configured */
63 #define VNI_FLAG_EXPRT_CFGD 0x10 /* Export RT is user configured */
64 #define VNI_FLAG_USE_TWO_LABELS 0x20 /* Attach both L2-VNI and L3-VNI if
65 needed for this VPN */
67 struct bgp
*bgp_vrf
; /* back pointer to the vrf instance */
69 /* Flag to indicate if we are
70 * advertising the g/w mac ip for
72 uint8_t advertise_gw_macip
;
74 /* Flag to indicate if we are
75 * advertising subnet for this VNI */
76 uint8_t advertise_subnet
;
78 /* Id for deriving the RD
79 * automatically for this VNI */
82 /* RD for this VNI. */
85 /* Route type 3 field */
86 struct in_addr originator_ip
;
88 /* Import and Export RTs. */
89 struct list
*import_rtl
;
90 struct list
*export_rtl
;
92 /* Route table for EVPN routes for
94 struct bgp_table
*route_table
;
99 DECLARE_QOBJ_TYPE(bgpevpn
)
101 /* Mapping of Import RT to VNIs.
102 * The Import RTs of all VNIs are maintained in a hash table with each
103 * RT linking to all VNIs that will import routes matching this RT.
107 struct ecommunity_val rt
;
109 /* List of VNIs importing routes matching this RT. */
113 /* Mapping of Import RT to VRFs.
114 * The Import RTs of all VRFss are maintained in a hash table with each
115 * RT linking to all VRFs that will import routes matching this RT.
117 struct vrf_irt_node
{
119 struct ecommunity_val rt
;
121 /* List of VNIs importing routes matching this RT. */
126 #define RT_TYPE_IMPORT 1
127 #define RT_TYPE_EXPORT 2
128 #define RT_TYPE_BOTH 3
130 static inline int is_vrf_rd_configured(struct bgp
*bgp_vrf
)
132 return (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_RD_CFGD
));
135 static inline int bgp_evpn_vrf_rd_matches_existing(struct bgp
*bgp_vrf
,
136 struct prefix_rd
*prd
)
138 return (memcmp(&bgp_vrf
->vrf_prd
.val
, prd
->val
, ECOMMUNITY_SIZE
) == 0);
141 static inline vni_t
bgpevpn_get_l3vni(struct bgpevpn
*vpn
)
143 return vpn
->bgp_vrf
? vpn
->bgp_vrf
->l3vni
: 0;
146 static inline void bgpevpn_get_rmac(struct bgpevpn
*vpn
, struct ethaddr
*rmac
)
148 memset(rmac
, 0, sizeof(struct ethaddr
));
151 memcpy(rmac
, &vpn
->bgp_vrf
->rmac
, sizeof(struct ethaddr
));
154 static inline struct list
*bgpevpn_get_vrf_export_rtl(struct bgpevpn
*vpn
)
159 return vpn
->bgp_vrf
->vrf_export_rtl
;
162 static inline struct list
*bgpevpn_get_vrf_import_rtl(struct bgpevpn
*vpn
)
167 return vpn
->bgp_vrf
->vrf_import_rtl
;
170 static inline void bgpevpn_unlink_from_l3vni(struct bgpevpn
*vpn
)
172 /* bail if vpn is not associated to bgp_vrf */
176 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
177 listnode_delete(vpn
->bgp_vrf
->l2vnis
, vpn
);
179 /* remove the backpointer to the vrf instance */
183 static inline void bgpevpn_link_to_l3vni(struct bgpevpn
*vpn
)
185 struct bgp
*bgp_vrf
= NULL
;
187 /* bail if vpn is already associated to vrf */
191 bgp_vrf
= bgp_lookup_by_vrf_id(vpn
->tenant_vrf_id
);
195 /* associate the vpn to the bgp_vrf instance */
196 vpn
->bgp_vrf
= bgp_vrf
;
197 listnode_add_sort(bgp_vrf
->l2vnis
, vpn
);
199 /* check if we are advertising two labels for this vpn */
200 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
))
201 SET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
204 static inline int is_vni_configured(struct bgpevpn
*vpn
)
206 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_CFGD
));
209 static inline int is_vni_live(struct bgpevpn
*vpn
)
211 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_LIVE
));
214 static inline int is_rd_configured(struct bgpevpn
*vpn
)
216 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
));
219 static inline int bgp_evpn_rd_matches_existing(struct bgpevpn
*vpn
,
220 struct prefix_rd
*prd
)
222 return (memcmp(&vpn
->prd
.val
, prd
->val
, ECOMMUNITY_SIZE
) == 0);
225 static inline int is_import_rt_configured(struct bgpevpn
*vpn
)
227 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
));
230 static inline int is_export_rt_configured(struct bgpevpn
*vpn
)
232 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
));
235 static inline int is_vni_param_configured(struct bgpevpn
*vpn
)
237 return (is_rd_configured(vpn
) || is_import_rt_configured(vpn
)
238 || is_export_rt_configured(vpn
));
241 static inline void encode_rmac_extcomm(struct ecommunity_val
*eval
,
242 struct ethaddr
*rmac
)
244 memset(eval
, 0, sizeof(*eval
));
245 eval
->val
[0] = ECOMMUNITY_ENCODE_EVPN
;
246 eval
->val
[1] = ECOMMUNITY_EVPN_SUBTYPE_ROUTERMAC
;
247 memcpy(&eval
->val
[2], rmac
, ETH_ALEN
);
250 static inline void encode_default_gw_extcomm(struct ecommunity_val
*eval
)
252 memset(eval
, 0, sizeof(*eval
));
253 eval
->val
[0] = ECOMMUNITY_ENCODE_OPAQUE
;
254 eval
->val
[1] = ECOMMUNITY_EVPN_SUBTYPE_DEF_GW
;
257 static inline void encode_mac_mobility_extcomm(int static_mac
, uint32_t seq
,
258 struct ecommunity_val
*eval
)
260 memset(eval
, 0, sizeof(*eval
));
261 eval
->val
[0] = ECOMMUNITY_ENCODE_EVPN
;
262 eval
->val
[1] = ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
;
264 eval
->val
[2] = ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY_FLAG_STICKY
;
265 eval
->val
[4] = (seq
>> 24) & 0xff;
266 eval
->val
[5] = (seq
>> 16) & 0xff;
267 eval
->val
[6] = (seq
>> 8) & 0xff;
268 eval
->val
[7] = seq
& 0xff;
271 static inline void ip_prefix_from_type5_prefix(struct prefix_evpn
*evp
,
274 memset(ip
, 0, sizeof(struct prefix
));
275 if (IS_EVPN_PREFIX_IPADDR_V4(evp
)) {
276 ip
->family
= AF_INET
;
277 ip
->prefixlen
= evp
->prefix
.ip_prefix_length
;
278 memcpy(&(ip
->u
.prefix4
), &(evp
->prefix
.ip
.ip
),
280 } else if (IS_EVPN_PREFIX_IPADDR_V6(evp
)) {
281 ip
->family
= AF_INET6
;
282 ip
->prefixlen
= evp
->prefix
.ip_prefix_length
;
283 memcpy(&(ip
->u
.prefix6
), &(evp
->prefix
.ip
.ip
),
288 static inline int is_evpn_prefix_default(struct prefix
*evp
)
290 if (evp
->family
!= AF_EVPN
)
293 return ((evp
->u
.prefix_evpn
.ip_prefix_length
== 0) ? 1 : 0);
296 static inline void ip_prefix_from_type2_prefix(struct prefix_evpn
*evp
,
299 memset(ip
, 0, sizeof(struct prefix
));
300 if (IS_EVPN_PREFIX_IPADDR_V4(evp
)) {
301 ip
->family
= AF_INET
;
302 ip
->prefixlen
= IPV4_MAX_BITLEN
;
303 memcpy(&(ip
->u
.prefix4
), &(evp
->prefix
.ip
.ip
),
305 } else if (IS_EVPN_PREFIX_IPADDR_V6(evp
)) {
306 ip
->family
= AF_INET6
;
307 ip
->prefixlen
= IPV6_MAX_BITLEN
;
308 memcpy(&(ip
->u
.prefix6
), &(evp
->prefix
.ip
.ip
),
313 static inline void build_evpn_type2_prefix(struct prefix_evpn
*p
,
317 memset(p
, 0, sizeof(struct prefix_evpn
));
319 p
->prefixlen
= EVPN_TYPE_2_ROUTE_PREFIXLEN
;
320 p
->prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
321 memcpy(&p
->prefix
.mac
.octet
, mac
->octet
, ETH_ALEN
);
322 p
->prefix
.ip
.ipa_type
= IPADDR_NONE
;
324 memcpy(&p
->prefix
.ip
, ip
, sizeof(*ip
));
327 static inline void build_type5_prefix_from_ip_prefix(struct prefix_evpn
*evp
,
328 struct prefix
*ip_prefix
)
332 memset(&ip
, 0, sizeof(struct ipaddr
));
333 if (ip_prefix
->family
== AF_INET
) {
334 ip
.ipa_type
= IPADDR_V4
;
335 memcpy(&ip
.ipaddr_v4
, &ip_prefix
->u
.prefix4
,
336 sizeof(struct in_addr
));
338 ip
.ipa_type
= IPADDR_V6
;
339 memcpy(&ip
.ipaddr_v6
, &ip_prefix
->u
.prefix6
,
340 sizeof(struct in6_addr
));
343 memset(evp
, 0, sizeof(struct prefix_evpn
));
344 evp
->family
= AF_EVPN
;
345 evp
->prefixlen
= EVPN_TYPE_5_ROUTE_PREFIXLEN
;
346 evp
->prefix
.ip_prefix_length
= ip_prefix
->prefixlen
;
347 evp
->prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
348 evp
->prefix
.ip
.ipa_type
= ip
.ipa_type
;
349 memcpy(&evp
->prefix
.ip
, &ip
, sizeof(struct ipaddr
));
352 static inline void build_evpn_type3_prefix(struct prefix_evpn
*p
,
353 struct in_addr originator_ip
)
355 memset(p
, 0, sizeof(struct prefix_evpn
));
357 p
->prefixlen
= EVPN_TYPE_3_ROUTE_PREFIXLEN
;
358 p
->prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
359 p
->prefix
.ip
.ipa_type
= IPADDR_V4
;
360 p
->prefix
.ip
.ipaddr_v4
= originator_ip
;
363 static inline int evpn_default_originate_set(struct bgp
*bgp
, afi_t afi
,
367 CHECK_FLAG(bgp
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
368 BGP_L2VPN_EVPN_DEFAULT_ORIGINATE_IPV4
))
370 else if (afi
== AFI_IP6
&&
371 CHECK_FLAG(bgp
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
372 BGP_L2VPN_EVPN_DEFAULT_ORIGINATE_IPV6
))
377 extern void evpn_rt_delete_auto(struct bgp
*, vni_t
, struct list
*);
378 extern void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
379 struct ecommunity
*ecomadd
);
380 extern void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
381 struct ecommunity
*ecomdel
);
382 extern void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
383 struct ecommunity
*ecomadd
);
384 extern void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
385 struct ecommunity
*ecomdel
);
386 extern int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
,
387 struct bgpevpn
*vpn
);
388 extern void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
);
389 extern void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
391 extern int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
392 extern int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
393 extern void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
);
394 extern void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
);
395 extern void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
);
396 extern void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
,
397 struct bgpevpn
*vpn
);
398 extern void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
,
399 struct bgpevpn
*vpn
);
400 extern void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
,
401 struct bgpevpn
*vpn
);
402 extern void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
);
403 extern void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
);
404 extern struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
);
405 extern struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
406 struct in_addr originator_ip
,
407 vrf_id_t tenant_vrf_id
);
408 extern void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
);
409 #endif /* _BGP_EVPN_PRIVATE_H */