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 */
65 /* Flag to indicate if we are advertising the g/w mac ip for this VNI*/
66 u_int8_t advertise_gw_macip
;
68 /* Flag to indicate if we are advertising subnet for this VNI */
69 u_int8_t advertise_subnet
;
71 /* Id for deriving the RD automatically for this VNI */
74 /* RD for this VNI. */
77 /* Route type 3 field */
78 struct in_addr originator_ip
;
80 /* Import and Export RTs. */
81 struct list
*import_rtl
;
82 struct list
*export_rtl
;
84 /* Route table for EVPN routes for this VNI. */
85 struct bgp_table
*route_table
;
90 DECLARE_QOBJ_TYPE(bgpevpn
)
92 /* Mapping of Import RT to VNIs.
93 * The Import RTs of all VNIs are maintained in a hash table with each
94 * RT linking to all VNIs that will import routes matching this RT.
98 struct ecommunity_val rt
;
100 /* List of VNIs importing routes matching this RT. */
104 /* Mapping of Import RT to VRFs.
105 * The Import RTs of all VRFss are maintained in a hash table with each
106 * RT linking to all VRFs that will import routes matching this RT.
108 struct vrf_irt_node
{
110 struct ecommunity_val rt
;
112 /* List of VNIs importing routes matching this RT. */
117 #define RT_TYPE_IMPORT 1
118 #define RT_TYPE_EXPORT 2
119 #define RT_TYPE_BOTH 3
121 static inline int is_vrf_rd_configured(struct bgp
*bgp_vrf
)
123 return (CHECK_FLAG(bgp_vrf
->vrf_flags
,
127 static inline int bgp_evpn_vrf_rd_matches_existing(struct bgp
*bgp_vrf
,
128 struct prefix_rd
*prd
)
130 return (memcmp(&bgp_vrf
->vrf_prd
.val
, prd
->val
, ECOMMUNITY_SIZE
) == 0);
133 static inline vni_t
bgpevpn_get_l3vni(struct bgpevpn
*vpn
)
135 struct bgp
*bgp_vrf
= NULL
;
137 bgp_vrf
= bgp_lookup_by_vrf_id(vpn
->tenant_vrf_id
);
141 return bgp_vrf
->l3vni
;
144 static inline void bgpevpn_get_rmac(struct bgpevpn
*vpn
, struct ethaddr
*rmac
)
146 struct bgp
*bgp_vrf
= NULL
;
148 memset(rmac
, 0, sizeof(struct ethaddr
));
149 bgp_vrf
= bgp_lookup_by_vrf_id(vpn
->tenant_vrf_id
);
152 memcpy(rmac
, &bgp_vrf
->rmac
, sizeof(struct ethaddr
));
155 static inline struct list
*bgpevpn_get_vrf_export_rtl(struct bgpevpn
*vpn
)
157 struct bgp
*bgp_vrf
= NULL
;
159 bgp_vrf
= bgp_lookup_by_vrf_id(vpn
->tenant_vrf_id
);
163 return bgp_vrf
->vrf_export_rtl
;
166 static inline struct list
*bgpevpn_get_vrf_import_rtl(struct bgpevpn
*vpn
)
168 struct bgp
*bgp_vrf
= NULL
;
170 bgp_vrf
= bgp_lookup_by_vrf_id(vpn
->tenant_vrf_id
);
174 return bgp_vrf
->vrf_import_rtl
;
177 static inline void bgpevpn_unlink_from_l3vni(struct bgpevpn
*vpn
)
179 struct bgp
*bgp_vrf
= NULL
;
181 bgp_vrf
= bgp_lookup_by_vrf_id(vpn
->tenant_vrf_id
);
182 if (!bgp_vrf
|| !bgp_vrf
->l2vnis
)
184 listnode_delete(bgp_vrf
->l2vnis
, vpn
);
187 static inline void bgpevpn_link_to_l3vni(struct bgpevpn
*vpn
)
189 struct bgp
*bgp_vrf
= NULL
;
191 bgp_vrf
= bgp_lookup_by_vrf_id(vpn
->tenant_vrf_id
);
192 if (!bgp_vrf
|| !bgp_vrf
->l2vnis
)
194 listnode_add_sort(bgp_vrf
->l2vnis
, vpn
);
197 static inline int is_vni_configured(struct bgpevpn
*vpn
)
199 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_CFGD
));
202 static inline int is_vni_live(struct bgpevpn
*vpn
)
204 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_LIVE
));
207 static inline int is_rd_configured(struct bgpevpn
*vpn
)
209 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
));
212 static inline int bgp_evpn_rd_matches_existing(struct bgpevpn
*vpn
,
213 struct prefix_rd
*prd
)
215 return (memcmp(&vpn
->prd
.val
, prd
->val
, ECOMMUNITY_SIZE
) == 0);
218 static inline int is_import_rt_configured(struct bgpevpn
*vpn
)
220 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
));
223 static inline int is_export_rt_configured(struct bgpevpn
*vpn
)
225 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
));
228 static inline int is_vni_param_configured(struct bgpevpn
*vpn
)
230 return (is_rd_configured(vpn
) || is_import_rt_configured(vpn
)
231 || is_export_rt_configured(vpn
));
234 static inline void encode_rmac_extcomm(struct ecommunity_val
*eval
,
235 struct ethaddr
*rmac
)
237 memset(eval
, 0, sizeof(*eval
));
238 eval
->val
[0] = ECOMMUNITY_ENCODE_EVPN
;
239 eval
->val
[1] = ECOMMUNITY_EVPN_SUBTYPE_ROUTERMAC
;
240 memcpy(&eval
->val
[2], rmac
, ETH_ALEN
);
243 static inline void encode_default_gw_extcomm(struct ecommunity_val
*eval
)
245 memset(eval
, 0, sizeof(*eval
));
246 eval
->val
[0] = ECOMMUNITY_ENCODE_OPAQUE
;
247 eval
->val
[1] = ECOMMUNITY_EVPN_SUBTYPE_DEF_GW
;
250 static inline void encode_mac_mobility_extcomm(int static_mac
, u_int32_t seq
,
251 struct ecommunity_val
*eval
)
253 memset(eval
, 0, sizeof(*eval
));
254 eval
->val
[0] = ECOMMUNITY_ENCODE_EVPN
;
255 eval
->val
[1] = ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
;
257 eval
->val
[2] = ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY_FLAG_STICKY
;
258 eval
->val
[4] = (seq
>> 24) & 0xff;
259 eval
->val
[5] = (seq
>> 16) & 0xff;
260 eval
->val
[6] = (seq
>> 8) & 0xff;
261 eval
->val
[7] = seq
& 0xff;
264 static inline void ip_prefix_from_type5_prefix(struct prefix_evpn
*evp
,
267 memset(ip
, 0, sizeof(struct prefix
));
268 if (IS_EVPN_PREFIX_IPADDR_V4(evp
)) {
269 ip
->family
= AF_INET
;
270 ip
->prefixlen
= evp
->prefix
.ip_prefix_length
;
271 memcpy(&(ip
->u
.prefix4
),
272 &(evp
->prefix
.ip
.ip
),
274 } else if (IS_EVPN_PREFIX_IPADDR_V6(evp
)) {
275 ip
->family
= AF_INET6
;
276 ip
->prefixlen
= evp
->prefix
.ip_prefix_length
;
277 memcpy(&(ip
->u
.prefix6
),
278 &(evp
->prefix
.ip
.ip
),
283 static inline void ip_prefix_from_type2_prefix(struct prefix_evpn
*evp
,
286 memset(ip
, 0, sizeof(struct prefix
));
287 if (IS_EVPN_PREFIX_IPADDR_V4(evp
)) {
288 ip
->family
= AF_INET
;
289 ip
->prefixlen
= IPV4_MAX_BITLEN
;
290 memcpy(&(ip
->u
.prefix4
),
291 &(evp
->prefix
.ip
.ip
),
293 } else if (IS_EVPN_PREFIX_IPADDR_V6(evp
)) {
294 ip
->family
= AF_INET6
;
295 ip
->prefixlen
= IPV6_MAX_BITLEN
;
296 memcpy(&(ip
->u
.prefix6
),
297 &(evp
->prefix
.ip
.ip
),
302 static inline void build_evpn_type2_prefix(struct prefix_evpn
*p
,
306 memset(p
, 0, sizeof(struct prefix_evpn
));
308 p
->prefixlen
= EVPN_TYPE_2_ROUTE_PREFIXLEN
;
309 p
->prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
310 memcpy(&p
->prefix
.mac
.octet
, mac
->octet
, ETH_ALEN
);
311 p
->prefix
.ip
.ipa_type
= IPADDR_NONE
;
313 memcpy(&p
->prefix
.ip
, ip
, sizeof(*ip
));
316 static inline void build_type5_prefix_from_ip_prefix(struct prefix_evpn
*evp
,
317 struct prefix
*ip_prefix
)
321 memset(&ip
, 0, sizeof(struct ipaddr
));
322 if (ip_prefix
->family
== AF_INET
) {
323 ip
.ipa_type
= IPADDR_V4
;
324 memcpy(&ip
.ipaddr_v4
, &ip_prefix
->u
.prefix4
,
325 sizeof(struct in_addr
));
327 ip
.ipa_type
= IPADDR_V6
;
328 memcpy(&ip
.ipaddr_v6
, &ip_prefix
->u
.prefix6
,
329 sizeof(struct in6_addr
));
332 memset(evp
, 0, sizeof(struct prefix_evpn
));
333 evp
->family
= AF_EVPN
;
334 evp
->prefixlen
= EVPN_TYPE_5_ROUTE_PREFIXLEN
;
335 evp
->prefix
.ip_prefix_length
= ip_prefix
->prefixlen
;
336 evp
->prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
337 evp
->prefix
.ip
.ipa_type
= ip
.ipa_type
;
338 memcpy(&evp
->prefix
.ip
, &ip
, sizeof(struct ipaddr
));
341 static inline void build_evpn_type3_prefix(struct prefix_evpn
*p
,
342 struct in_addr originator_ip
)
344 memset(p
, 0, sizeof(struct prefix_evpn
));
346 p
->prefixlen
= EVPN_TYPE_3_ROUTE_PREFIXLEN
;
347 p
->prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
348 p
->prefix
.ip
.ipa_type
= IPADDR_V4
;
349 p
->prefix
.ip
.ipaddr_v4
= originator_ip
;
352 static inline int advertise_type5_routes(struct bgp
*bgp_vrf
,
359 CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_ADVERTISE_IPV4_IN_EVPN
))
362 if (afi
== AFI_IP6
&&
363 CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_ADVERTISE_IPV6_IN_EVPN
))
369 extern void evpn_rt_delete_auto(struct bgp
*, vni_t
, struct list
*);
370 extern void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
371 struct ecommunity
*ecomadd
);
372 extern void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
373 struct ecommunity
*ecomdel
);
374 extern void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
375 struct ecommunity
*ecomadd
);
376 extern void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
377 struct ecommunity
*ecomdel
);
378 extern int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
,
379 struct bgpevpn
*vpn
);
380 extern void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
);
381 extern void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
383 extern int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
384 extern int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
385 extern void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
);
386 extern void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
);
387 extern void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
);
388 extern void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
,
389 struct bgpevpn
*vpn
);
390 extern void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
,
391 struct bgpevpn
*vpn
);
392 extern void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
,
393 struct bgpevpn
*vpn
);
394 extern void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
);
395 extern void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
);
396 extern struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
);
397 extern struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
398 struct in_addr originator_ip
,
399 vrf_id_t tenant_vrf_id
);
400 extern void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
);
401 #endif /* _BGP_EVPN_PRIVATE_H */