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_ROUTE_PREFIXLEN 224
36 /* EVPN route types. */
38 BGP_EVPN_AD_ROUTE
= 1, /* Ethernet Auto-Discovery (A-D) route */
39 BGP_EVPN_MAC_IP_ROUTE
, /* MAC/IP Advertisement route */
40 BGP_EVPN_IMET_ROUTE
, /* Inclusive Multicast Ethernet Tag route */
41 BGP_EVPN_ES_ROUTE
, /* Ethernet Segment route */
42 BGP_EVPN_IP_PREFIX_ROUTE
, /* IP Prefix route */
43 } bgp_evpn_route_type
;
46 * Hash table of EVIs. Right now, the only type of EVI supported is with
47 * VxLAN encapsulation, hence each EVI corresponds to a L2 VNI.
48 * The VNIs are not "created" through BGP but through some other interface
49 * on the system. This table stores VNIs that BGP comes to know as present
50 * on the system (through interaction with zebra) as well as pre-configured
51 * VNIs (which need to be defined in the system to become "live").
55 vrf_id_t tenant_vrf_id
;
57 #define VNI_FLAG_CFGD 0x1 /* VNI is user configured */
58 #define VNI_FLAG_LIVE 0x2 /* VNI is "live" */
59 #define VNI_FLAG_RD_CFGD 0x4 /* RD is user configured. */
60 #define VNI_FLAG_IMPRT_CFGD 0x8 /* Import RT is user configured */
61 #define VNI_FLAG_EXPRT_CFGD 0x10 /* Export RT is user configured */
62 #define VNI_FLAG_USE_TWO_LABELS 0x20 /* Attach both L2-VNI and L3-VNI if
63 needed for this VPN */
65 struct bgp
*bgp_vrf
; /* back pointer to the vrf instance */
67 /* Flag to indicate if we are
68 * advertising the g/w mac ip for
70 uint8_t advertise_gw_macip
;
72 /* Flag to indicate if we are
73 * advertising subnet for this VNI */
74 uint8_t advertise_subnet
;
76 /* Id for deriving the RD
77 * automatically for this VNI */
80 /* RD for this VNI. */
83 /* Route type 3 field */
84 struct in_addr originator_ip
;
86 /* Import and Export RTs. */
87 struct list
*import_rtl
;
88 struct list
*export_rtl
;
90 /* Route table for EVPN routes for
92 struct bgp_table
*route_table
;
97 DECLARE_QOBJ_TYPE(bgpevpn
)
101 /* Ethernet Segment Identifier */
106 #define EVPNES_LOCAL 0x01
107 #define EVPNES_REMOTE 0x02
109 /* Id for deriving the RD
110 * automatically for this ESI */
113 /* RD for this VNI. */
114 struct prefix_rd prd
;
116 /* originator ip address */
117 struct ipaddr originator_ip
;
119 /* list of VTEPs in the same site */
120 struct list
*vtep_list
;
122 /* Route table for EVPN routes for
123 * this ESI. - type4 routes */
124 struct bgp_table
*route_table
;
129 DECLARE_QOBJ_TYPE(evpnes
)
131 /* Mapping of Import RT to VNIs.
132 * The Import RTs of all VNIs are maintained in a hash table with each
133 * RT linking to all VNIs that will import routes matching this RT.
137 struct ecommunity_val rt
;
139 /* List of VNIs importing routes matching this RT. */
143 /* Mapping of Import RT to VRFs.
144 * The Import RTs of all VRFss are maintained in a hash table with each
145 * RT linking to all VRFs that will import routes matching this RT.
147 struct vrf_irt_node
{
149 struct ecommunity_val rt
;
151 /* List of VNIs importing routes matching this RT. */
156 #define RT_TYPE_IMPORT 1
157 #define RT_TYPE_EXPORT 2
158 #define RT_TYPE_BOTH 3
160 static inline int is_vrf_rd_configured(struct bgp
*bgp_vrf
)
162 return (CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_RD_CFGD
));
165 static inline int bgp_evpn_vrf_rd_matches_existing(struct bgp
*bgp_vrf
,
166 struct prefix_rd
*prd
)
168 return (memcmp(&bgp_vrf
->vrf_prd
.val
, prd
->val
, ECOMMUNITY_SIZE
) == 0);
171 static inline vni_t
bgpevpn_get_l3vni(struct bgpevpn
*vpn
)
173 return vpn
->bgp_vrf
? vpn
->bgp_vrf
->l3vni
: 0;
176 static inline void bgpevpn_get_rmac(struct bgpevpn
*vpn
, struct ethaddr
*rmac
)
178 memset(rmac
, 0, sizeof(struct ethaddr
));
181 memcpy(rmac
, &vpn
->bgp_vrf
->rmac
, sizeof(struct ethaddr
));
184 static inline struct list
*bgpevpn_get_vrf_export_rtl(struct bgpevpn
*vpn
)
189 return vpn
->bgp_vrf
->vrf_export_rtl
;
192 static inline struct list
*bgpevpn_get_vrf_import_rtl(struct bgpevpn
*vpn
)
197 return vpn
->bgp_vrf
->vrf_import_rtl
;
200 static inline void bgpevpn_unlink_from_l3vni(struct bgpevpn
*vpn
)
202 /* bail if vpn is not associated to bgp_vrf */
206 UNSET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
207 listnode_delete(vpn
->bgp_vrf
->l2vnis
, vpn
);
209 /* remove the backpointer to the vrf instance */
213 static inline void bgpevpn_link_to_l3vni(struct bgpevpn
*vpn
)
215 struct bgp
*bgp_vrf
= NULL
;
217 /* bail if vpn is already associated to vrf */
221 bgp_vrf
= bgp_lookup_by_vrf_id(vpn
->tenant_vrf_id
);
225 /* associate the vpn to the bgp_vrf instance */
226 vpn
->bgp_vrf
= bgp_vrf
;
227 listnode_add_sort(bgp_vrf
->l2vnis
, vpn
);
229 /* check if we are advertising two labels for this vpn */
230 if (!CHECK_FLAG(bgp_vrf
->vrf_flags
, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY
))
231 SET_FLAG(vpn
->flags
, VNI_FLAG_USE_TWO_LABELS
);
234 static inline int is_vni_configured(struct bgpevpn
*vpn
)
236 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_CFGD
));
239 static inline int is_vni_live(struct bgpevpn
*vpn
)
241 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_LIVE
));
244 static inline int is_rd_configured(struct bgpevpn
*vpn
)
246 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_RD_CFGD
));
249 static inline int bgp_evpn_rd_matches_existing(struct bgpevpn
*vpn
,
250 struct prefix_rd
*prd
)
252 return (memcmp(&vpn
->prd
.val
, prd
->val
, ECOMMUNITY_SIZE
) == 0);
255 static inline int is_import_rt_configured(struct bgpevpn
*vpn
)
257 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_IMPRT_CFGD
));
260 static inline int is_export_rt_configured(struct bgpevpn
*vpn
)
262 return (CHECK_FLAG(vpn
->flags
, VNI_FLAG_EXPRT_CFGD
));
265 static inline int is_vni_param_configured(struct bgpevpn
*vpn
)
267 return (is_rd_configured(vpn
) || is_import_rt_configured(vpn
)
268 || is_export_rt_configured(vpn
));
271 static inline void encode_es_rt_extcomm(struct ecommunity_val
*eval
,
274 memset(eval
, 0, sizeof(struct ecommunity_val
));
275 eval
->val
[0] = ECOMMUNITY_ENCODE_EVPN
;
276 eval
->val
[1] = ECOMMUNITY_EVPN_SUBTYPE_ES_IMPORT_RT
;
277 memcpy(&eval
->val
[2], mac
, ETH_ALEN
);
280 static inline void encode_rmac_extcomm(struct ecommunity_val
*eval
,
281 struct ethaddr
*rmac
)
283 memset(eval
, 0, sizeof(*eval
));
284 eval
->val
[0] = ECOMMUNITY_ENCODE_EVPN
;
285 eval
->val
[1] = ECOMMUNITY_EVPN_SUBTYPE_ROUTERMAC
;
286 memcpy(&eval
->val
[2], rmac
, ETH_ALEN
);
289 static inline void encode_default_gw_extcomm(struct ecommunity_val
*eval
)
291 memset(eval
, 0, sizeof(*eval
));
292 eval
->val
[0] = ECOMMUNITY_ENCODE_OPAQUE
;
293 eval
->val
[1] = ECOMMUNITY_EVPN_SUBTYPE_DEF_GW
;
296 static inline void encode_mac_mobility_extcomm(int static_mac
, uint32_t seq
,
297 struct ecommunity_val
*eval
)
299 memset(eval
, 0, sizeof(*eval
));
300 eval
->val
[0] = ECOMMUNITY_ENCODE_EVPN
;
301 eval
->val
[1] = ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
;
303 eval
->val
[2] = ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY_FLAG_STICKY
;
304 eval
->val
[4] = (seq
>> 24) & 0xff;
305 eval
->val
[5] = (seq
>> 16) & 0xff;
306 eval
->val
[6] = (seq
>> 8) & 0xff;
307 eval
->val
[7] = seq
& 0xff;
310 static inline void ip_prefix_from_type5_prefix(struct prefix_evpn
*evp
,
313 memset(ip
, 0, sizeof(struct prefix
));
314 if (is_evpn_prefix_ipaddr_v4(evp
)) {
315 ip
->family
= AF_INET
;
316 ip
->prefixlen
= evp
->prefix
.prefix_addr
.ip_prefix_length
;
317 memcpy(&(ip
->u
.prefix4
), &(evp
->prefix
.prefix_addr
.ip
.ip
),
319 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
320 ip
->family
= AF_INET6
;
321 ip
->prefixlen
= evp
->prefix
.prefix_addr
.ip_prefix_length
;
322 memcpy(&(ip
->u
.prefix6
), &(evp
->prefix
.prefix_addr
.ip
.ip
),
327 static inline int is_evpn_prefix_default(struct prefix
*evp
)
329 if (evp
->family
!= AF_EVPN
)
332 return ((evp
->u
.prefix_evpn
.prefix_addr
.ip_prefix_length
== 0) ?
336 static inline void ip_prefix_from_type2_prefix(struct prefix_evpn
*evp
,
339 memset(ip
, 0, sizeof(struct prefix
));
340 if (is_evpn_prefix_ipaddr_v4(evp
)) {
341 ip
->family
= AF_INET
;
342 ip
->prefixlen
= IPV4_MAX_BITLEN
;
343 memcpy(&(ip
->u
.prefix4
), &(evp
->prefix
.macip_addr
.ip
.ip
),
345 } else if (is_evpn_prefix_ipaddr_v6(evp
)) {
346 ip
->family
= AF_INET6
;
347 ip
->prefixlen
= IPV6_MAX_BITLEN
;
348 memcpy(&(ip
->u
.prefix6
), &(evp
->prefix
.macip_addr
.ip
.ip
),
353 static inline void ip_prefix_from_evpn_prefix(struct prefix_evpn
*evp
,
356 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
357 ip_prefix_from_type2_prefix(evp
, ip
);
358 else if (evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
)
359 ip_prefix_from_type5_prefix(evp
, ip
);
362 static inline void build_evpn_type2_prefix(struct prefix_evpn
*p
,
366 memset(p
, 0, sizeof(struct prefix_evpn
));
368 p
->prefixlen
= EVPN_ROUTE_PREFIXLEN
;
369 p
->prefix
.route_type
= BGP_EVPN_MAC_IP_ROUTE
;
370 memcpy(&p
->prefix
.macip_addr
.mac
.octet
, mac
->octet
, ETH_ALEN
);
371 p
->prefix
.macip_addr
.ip
.ipa_type
= IPADDR_NONE
;
373 memcpy(&p
->prefix
.macip_addr
.ip
, ip
, sizeof(*ip
));
376 static inline void build_type5_prefix_from_ip_prefix(struct prefix_evpn
*evp
,
377 struct prefix
*ip_prefix
)
381 memset(&ip
, 0, sizeof(struct ipaddr
));
382 if (ip_prefix
->family
== AF_INET
) {
383 ip
.ipa_type
= IPADDR_V4
;
384 memcpy(&ip
.ipaddr_v4
, &ip_prefix
->u
.prefix4
,
385 sizeof(struct in_addr
));
387 ip
.ipa_type
= IPADDR_V6
;
388 memcpy(&ip
.ipaddr_v6
, &ip_prefix
->u
.prefix6
,
389 sizeof(struct in6_addr
));
392 memset(evp
, 0, sizeof(struct prefix_evpn
));
393 evp
->family
= AF_EVPN
;
394 evp
->prefixlen
= EVPN_ROUTE_PREFIXLEN
;
395 evp
->prefix
.route_type
= BGP_EVPN_IP_PREFIX_ROUTE
;
396 evp
->prefix
.prefix_addr
.ip_prefix_length
= ip_prefix
->prefixlen
;
397 evp
->prefix
.prefix_addr
.ip
.ipa_type
= ip
.ipa_type
;
398 memcpy(&evp
->prefix
.prefix_addr
.ip
, &ip
, sizeof(struct ipaddr
));
401 static inline void build_evpn_type3_prefix(struct prefix_evpn
*p
,
402 struct in_addr originator_ip
)
404 memset(p
, 0, sizeof(struct prefix_evpn
));
406 p
->prefixlen
= EVPN_ROUTE_PREFIXLEN
;
407 p
->prefix
.route_type
= BGP_EVPN_IMET_ROUTE
;
408 p
->prefix
.imet_addr
.ip
.ipa_type
= IPADDR_V4
;
409 p
->prefix
.imet_addr
.ip
.ipaddr_v4
= originator_ip
;
412 static inline void build_evpn_type4_prefix(struct prefix_evpn
*p
,
414 struct in_addr originator_ip
)
416 memset(p
, 0, sizeof(struct prefix_evpn
));
418 p
->prefixlen
= EVPN_ROUTE_PREFIXLEN
;
419 p
->prefix
.route_type
= BGP_EVPN_ES_ROUTE
;
420 p
->prefix
.es_addr
.ip_prefix_length
= IPV4_MAX_BITLEN
;
421 p
->prefix
.es_addr
.ip
.ipa_type
= IPADDR_V4
;
422 p
->prefix
.es_addr
.ip
.ipaddr_v4
= originator_ip
;
423 memcpy(&p
->prefix
.es_addr
.esi
, esi
, sizeof(esi_t
));
426 static inline int evpn_default_originate_set(struct bgp
*bgp
, afi_t afi
,
430 CHECK_FLAG(bgp
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
431 BGP_L2VPN_EVPN_DEFAULT_ORIGINATE_IPV4
))
433 else if (afi
== AFI_IP6
&&
434 CHECK_FLAG(bgp
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
435 BGP_L2VPN_EVPN_DEFAULT_ORIGINATE_IPV6
))
440 static inline void es_get_system_mac(esi_t
*esi
,
443 /* for type-1 and type-3 ESIs,
444 the system mac starts at val[1]
446 memcpy(mac
, &esi
->val
[1], ETH_ALEN
);
449 static inline int is_es_local(struct evpnes
*es
)
451 return CHECK_FLAG(es
->flags
, EVPNES_LOCAL
) ? 1 : 0;
454 extern void evpn_rt_delete_auto(struct bgp
*, vni_t
, struct list
*);
455 extern void bgp_evpn_configure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
456 struct ecommunity
*ecomadd
);
457 extern void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp
*bgp_vrf
,
458 struct ecommunity
*ecomdel
);
459 extern void bgp_evpn_configure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
460 struct ecommunity
*ecomadd
);
461 extern void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp
*bgp_vrf
,
462 struct ecommunity
*ecomdel
);
463 extern int bgp_evpn_handle_export_rt_change(struct bgp
*bgp
,
464 struct bgpevpn
*vpn
);
465 extern void bgp_evpn_handle_autort_change(struct bgp
*bgp
);
466 extern void bgp_evpn_handle_vrf_rd_change(struct bgp
*bgp_vrf
, int withdraw
);
467 extern void bgp_evpn_handle_rd_change(struct bgp
*bgp
, struct bgpevpn
*vpn
,
469 extern int bgp_evpn_install_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
470 extern int bgp_evpn_uninstall_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
);
471 extern void bgp_evpn_map_vrf_to_its_rts(struct bgp
*bgp_vrf
);
472 extern void bgp_evpn_unmap_vrf_from_its_rts(struct bgp
*bgp_vrf
);
473 extern void bgp_evpn_map_vni_to_its_rts(struct bgp
*bgp
, struct bgpevpn
*vpn
);
474 extern void bgp_evpn_unmap_vni_from_its_rts(struct bgp
*bgp
,
475 struct bgpevpn
*vpn
);
476 extern void bgp_evpn_derive_auto_rt_import(struct bgp
*bgp
,
477 struct bgpevpn
*vpn
);
478 extern void bgp_evpn_derive_auto_rt_export(struct bgp
*bgp
,
479 struct bgpevpn
*vpn
);
480 extern void bgp_evpn_derive_auto_rd(struct bgp
*bgp
, struct bgpevpn
*vpn
);
481 extern void bgp_evpn_derive_auto_rd_for_vrf(struct bgp
*bgp
);
482 extern struct bgpevpn
*bgp_evpn_lookup_vni(struct bgp
*bgp
, vni_t vni
);
483 extern struct bgpevpn
*bgp_evpn_new(struct bgp
*bgp
, vni_t vni
,
484 struct in_addr originator_ip
,
485 vrf_id_t tenant_vrf_id
);
486 extern void bgp_evpn_free(struct bgp
*bgp
, struct bgpevpn
*vpn
);
487 extern struct evpnes
*bgp_evpn_lookup_es(struct bgp
*bgp
, esi_t
*esi
);
488 extern struct evpnes
*bgp_evpn_es_new(struct bgp
*bgp
, esi_t
*esi
,
489 struct ipaddr
*originator_ip
);
490 extern void bgp_evpn_es_free(struct bgp
*bgp
, struct evpnes
*es
);
491 #endif /* _BGP_EVPN_PRIVATE_H */