]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_evpn_private.h
*: conform with COMMUNITY.md formatting rules, via 'make indent'
[mirror_frr.git] / bgpd / bgp_evpn_private.h
CommitLineData
a4168ebc 1/* BGP EVPN internal definitions
2 * Copyright (C) 2017 Cumulus Networks, Inc.
3 *
4 * This file is part of FRR.
5 *
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
9 * later version.
10 *
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.
15 *
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
19 * 02111-1307, USA.
20 */
21
22#ifndef _BGP_EVPN_PRIVATE_H
23#define _BGP_EVPN_PRIVATE_H
24
25#include "vxlan.h"
26#include "zebra.h"
27
28#include "bgpd/bgpd.h"
29#include "bgpd/bgp_ecommunity.h"
30
60466a63 31#define RT_ADDRSTRLEN 28
9c92b5f7 32
e9fc2840 33/* EVPN prefix lengths. This reprsent the sizeof struct prefix_evpn */
a4168ebc 34#define EVPN_TYPE_2_ROUTE_PREFIXLEN 224
35#define EVPN_TYPE_3_ROUTE_PREFIXLEN 224
e9fc2840 36#define EVPN_TYPE_5_ROUTE_PREFIXLEN 224
a4168ebc 37
38/* EVPN route types. */
d62a17ae 39typedef enum {
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 */
a4168ebc 45} bgp_evpn_route_type;
46
47/*
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").
54 */
d62a17ae 55struct bgpevpn {
56 vni_t vni;
29c53922 57 vrf_id_t tenant_vrf_id;
d62a17ae 58 u_int32_t flags;
a4168ebc 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 */
c48d9f5f
MK
64#define VNI_FLAG_USE_TWO_LABELS 0x20 /* Attach both L2-VNI and L3-VNI if
65 needed for this VPN */
a4168ebc 66
bc4606bd
MK
67 struct bgp *bgp_vrf; /* back pointer to the vrf instance */
68
996c9314
LB
69 /* Flag to indicate if we are
70 * advertising the g/w mac ip for
71 * this VNI*/
72 u_int8_t advertise_gw_macip;
1a98c087 73
996c9314
LB
74 /* Flag to indicate if we are
75 * advertising subnet for this VNI */
76 u_int8_t advertise_subnet;
31310b25 77
996c9314
LB
78 /* Id for deriving the RD
79 * automatically for this VNI */
80 u_int16_t rd_id;
a4168ebc 81
996c9314
LB
82 /* RD for this VNI. */
83 struct prefix_rd prd;
a4168ebc 84
996c9314
LB
85 /* Route type 3 field */
86 struct in_addr originator_ip;
a4168ebc 87
996c9314
LB
88 /* Import and Export RTs. */
89 struct list *import_rtl;
90 struct list *export_rtl;
a4168ebc 91
996c9314
LB
92 /* Route table for EVPN routes for
93 * this VNI. */
94 struct bgp_table *route_table;
a4168ebc 95
996c9314 96 QOBJ_FIELDS
a4168ebc 97};
98
99DECLARE_QOBJ_TYPE(bgpevpn)
100
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.
104 */
d62a17ae 105struct irt_node {
106 /* RT */
107 struct ecommunity_val rt;
a4168ebc 108
d62a17ae 109 /* List of VNIs importing routes matching this RT. */
110 struct list *vnis;
a4168ebc 111};
112
10ebe1ab
MK
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.
116 */
117struct vrf_irt_node {
118 /* RT */
119 struct ecommunity_val rt;
120
121 /* List of VNIs importing routes matching this RT. */
122 struct list *vrfs;
123};
124
125
128ea8ab 126#define RT_TYPE_IMPORT 1
127#define RT_TYPE_EXPORT 2
128#define RT_TYPE_BOTH 3
129
676f83b9 130static inline int is_vrf_rd_configured(struct bgp *bgp_vrf)
131{
996c9314 132 return (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_RD_CFGD));
676f83b9 133}
134
135static inline int bgp_evpn_vrf_rd_matches_existing(struct bgp *bgp_vrf,
136 struct prefix_rd *prd)
137{
138 return (memcmp(&bgp_vrf->vrf_prd.val, prd->val, ECOMMUNITY_SIZE) == 0);
139}
140
6d8c58b7
MK
141static inline vni_t bgpevpn_get_l3vni(struct bgpevpn *vpn)
142{
bc4606bd 143 return vpn->bgp_vrf ? vpn->bgp_vrf->l3vni : 0;
6d8c58b7
MK
144}
145
146static inline void bgpevpn_get_rmac(struct bgpevpn *vpn, struct ethaddr *rmac)
147{
6d8c58b7 148 memset(rmac, 0, sizeof(struct ethaddr));
bc4606bd 149 if (!vpn->bgp_vrf)
6d8c58b7 150 return;
bc4606bd 151 memcpy(rmac, &vpn->bgp_vrf->rmac, sizeof(struct ethaddr));
6d8c58b7
MK
152}
153
154static inline struct list *bgpevpn_get_vrf_export_rtl(struct bgpevpn *vpn)
155{
bc4606bd 156 if (!vpn->bgp_vrf)
6d8c58b7
MK
157 return NULL;
158
bc4606bd 159 return vpn->bgp_vrf->vrf_export_rtl;
6d8c58b7
MK
160}
161
162static inline struct list *bgpevpn_get_vrf_import_rtl(struct bgpevpn *vpn)
163{
bc4606bd 164 if (!vpn->bgp_vrf)
6d8c58b7
MK
165 return NULL;
166
bc4606bd 167 return vpn->bgp_vrf->vrf_import_rtl;
6d8c58b7
MK
168}
169
6a8657d0
MK
170static inline void bgpevpn_unlink_from_l3vni(struct bgpevpn *vpn)
171{
bc4606bd
MK
172 /* bail if vpn is not associated to bgp_vrf */
173 if (!vpn->bgp_vrf)
6a8657d0 174 return;
996c9314 175
c48d9f5f 176 UNSET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
bc4606bd 177 listnode_delete(vpn->bgp_vrf->l2vnis, vpn);
996c9314 178
bc4606bd
MK
179 /* remove the backpointer to the vrf instance */
180 vpn->bgp_vrf = NULL;
6a8657d0
MK
181}
182
183static inline void bgpevpn_link_to_l3vni(struct bgpevpn *vpn)
184{
185 struct bgp *bgp_vrf = NULL;
186
bc4606bd
MK
187 /* bail if vpn is already associated to vrf */
188 if (vpn->bgp_vrf)
189 return;
190
6a8657d0 191 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
c48d9f5f 192 if (!bgp_vrf)
6a8657d0 193 return;
bc4606bd
MK
194
195 /* associate the vpn to the bgp_vrf instance */
196 vpn->bgp_vrf = bgp_vrf;
6a8657d0 197 listnode_add_sort(bgp_vrf->l2vnis, vpn);
83ea2eb0 198
c48d9f5f 199 /* check if we are advertising two labels for this vpn */
996c9314 200 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY))
c48d9f5f 201 SET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
6a8657d0
MK
202}
203
d62a17ae 204static inline int is_vni_configured(struct bgpevpn *vpn)
128ea8ab 205{
d62a17ae 206 return (CHECK_FLAG(vpn->flags, VNI_FLAG_CFGD));
128ea8ab 207}
208
d62a17ae 209static inline int is_vni_live(struct bgpevpn *vpn)
128ea8ab 210{
d62a17ae 211 return (CHECK_FLAG(vpn->flags, VNI_FLAG_LIVE));
128ea8ab 212}
213
d62a17ae 214static inline int is_rd_configured(struct bgpevpn *vpn)
128ea8ab 215{
d62a17ae 216 return (CHECK_FLAG(vpn->flags, VNI_FLAG_RD_CFGD));
128ea8ab 217}
218
d62a17ae 219static inline int bgp_evpn_rd_matches_existing(struct bgpevpn *vpn,
220 struct prefix_rd *prd)
128ea8ab 221{
d62a17ae 222 return (memcmp(&vpn->prd.val, prd->val, ECOMMUNITY_SIZE) == 0);
128ea8ab 223}
224
d62a17ae 225static inline int is_import_rt_configured(struct bgpevpn *vpn)
128ea8ab 226{
d62a17ae 227 return (CHECK_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD));
128ea8ab 228}
229
d62a17ae 230static inline int is_export_rt_configured(struct bgpevpn *vpn)
128ea8ab 231{
d62a17ae 232 return (CHECK_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD));
128ea8ab 233}
234
d62a17ae 235static inline int is_vni_param_configured(struct bgpevpn *vpn)
128ea8ab 236{
d62a17ae 237 return (is_rd_configured(vpn) || is_import_rt_configured(vpn)
238 || is_export_rt_configured(vpn));
128ea8ab 239}
240
bc59a672
MK
241static inline void encode_rmac_extcomm(struct ecommunity_val *eval,
242 struct ethaddr *rmac)
243{
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);
248}
249
ead40654
MK
250static inline void encode_default_gw_extcomm(struct ecommunity_val *eval)
251{
252 memset(eval, 0, sizeof(*eval));
253 eval->val[0] = ECOMMUNITY_ENCODE_OPAQUE;
254 eval->val[1] = ECOMMUNITY_EVPN_SUBTYPE_DEF_GW;
255}
256
d62a17ae 257static inline void encode_mac_mobility_extcomm(int static_mac, u_int32_t seq,
258 struct ecommunity_val *eval)
128ea8ab 259{
d62a17ae 260 memset(eval, 0, sizeof(*eval));
261 eval->val[0] = ECOMMUNITY_ENCODE_EVPN;
262 eval->val[1] = ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY;
263 if (static_mac)
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;
128ea8ab 269}
270
90264d64
MK
271static inline void ip_prefix_from_type5_prefix(struct prefix_evpn *evp,
272 struct prefix *ip)
273{
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;
996c9314 278 memcpy(&(ip->u.prefix4), &(evp->prefix.ip.ip),
90264d64
MK
279 IPV4_MAX_BYTELEN);
280 } else if (IS_EVPN_PREFIX_IPADDR_V6(evp)) {
281 ip->family = AF_INET6;
282 ip->prefixlen = evp->prefix.ip_prefix_length;
996c9314 283 memcpy(&(ip->u.prefix6), &(evp->prefix.ip.ip),
90264d64
MK
284 IPV6_MAX_BYTELEN);
285 }
286}
287
d3135ba3 288static inline void ip_prefix_from_type2_prefix(struct prefix_evpn *evp,
289 struct prefix *ip)
290{
291 memset(ip, 0, sizeof(struct prefix));
292 if (IS_EVPN_PREFIX_IPADDR_V4(evp)) {
293 ip->family = AF_INET;
294 ip->prefixlen = IPV4_MAX_BITLEN;
996c9314 295 memcpy(&(ip->u.prefix4), &(evp->prefix.ip.ip),
d3135ba3 296 IPV4_MAX_BYTELEN);
297 } else if (IS_EVPN_PREFIX_IPADDR_V6(evp)) {
298 ip->family = AF_INET6;
299 ip->prefixlen = IPV6_MAX_BITLEN;
996c9314 300 memcpy(&(ip->u.prefix6), &(evp->prefix.ip.ip),
d3135ba3 301 IPV6_MAX_BYTELEN);
302 }
303}
304
d62a17ae 305static inline void build_evpn_type2_prefix(struct prefix_evpn *p,
306 struct ethaddr *mac,
307 struct ipaddr *ip)
128ea8ab 308{
d62a17ae 309 memset(p, 0, sizeof(struct prefix_evpn));
b03b8898 310 p->family = AF_EVPN;
d62a17ae 311 p->prefixlen = EVPN_TYPE_2_ROUTE_PREFIXLEN;
312 p->prefix.route_type = BGP_EVPN_MAC_IP_ROUTE;
28328ea9 313 memcpy(&p->prefix.mac.octet, mac->octet, ETH_ALEN);
d62a17ae 314 p->prefix.ip.ipa_type = IPADDR_NONE;
315 if (ip)
316 memcpy(&p->prefix.ip, ip, sizeof(*ip));
128ea8ab 317}
318
342dd0c6 319static inline void build_type5_prefix_from_ip_prefix(struct prefix_evpn *evp,
320 struct prefix *ip_prefix)
321{
322 struct ipaddr ip;
323
324 memset(&ip, 0, sizeof(struct ipaddr));
325 if (ip_prefix->family == AF_INET) {
326 ip.ipa_type = IPADDR_V4;
327 memcpy(&ip.ipaddr_v4, &ip_prefix->u.prefix4,
328 sizeof(struct in_addr));
329 } else {
330 ip.ipa_type = IPADDR_V6;
331 memcpy(&ip.ipaddr_v6, &ip_prefix->u.prefix6,
332 sizeof(struct in6_addr));
333 }
334
335 memset(evp, 0, sizeof(struct prefix_evpn));
336 evp->family = AF_EVPN;
337 evp->prefixlen = EVPN_TYPE_5_ROUTE_PREFIXLEN;
338 evp->prefix.ip_prefix_length = ip_prefix->prefixlen;
339 evp->prefix.route_type = BGP_EVPN_IP_PREFIX_ROUTE;
340 evp->prefix.ip.ipa_type = ip.ipa_type;
341 memcpy(&evp->prefix.ip, &ip, sizeof(struct ipaddr));
342}
343
d62a17ae 344static inline void build_evpn_type3_prefix(struct prefix_evpn *p,
345 struct in_addr originator_ip)
128ea8ab 346{
d62a17ae 347 memset(p, 0, sizeof(struct prefix_evpn));
b03b8898 348 p->family = AF_EVPN;
d62a17ae 349 p->prefixlen = EVPN_TYPE_3_ROUTE_PREFIXLEN;
350 p->prefix.route_type = BGP_EVPN_IMET_ROUTE;
351 p->prefix.ip.ipa_type = IPADDR_V4;
352 p->prefix.ip.ipaddr_v4 = originator_ip;
128ea8ab 353}
354
996c9314 355static inline int advertise_type5_routes(struct bgp *bgp_vrf, afi_t afi)
06d2e8f3
MK
356{
357 if (!bgp_vrf->l3vni)
358 return 0;
359
996c9314
LB
360 if (afi == AFI_IP
361 && CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_ADVERTISE_IPV4_IN_EVPN))
06d2e8f3
MK
362 return 1;
363
996c9314
LB
364 if (afi == AFI_IP6
365 && CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_ADVERTISE_IPV6_IN_EVPN))
06d2e8f3
MK
366 return 1;
367
368 return 0;
369}
370
996c9314 371extern void evpn_rt_delete_auto(struct bgp *, vni_t, struct list *);
523cafc4 372extern void bgp_evpn_configure_export_rt_for_vrf(struct bgp *bgp_vrf,
373 struct ecommunity *ecomadd);
374extern void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp *bgp_vrf,
375 struct ecommunity *ecomdel);
376extern void bgp_evpn_configure_import_rt_for_vrf(struct bgp *bgp_vrf,
377 struct ecommunity *ecomadd);
378extern void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp *bgp_vrf,
379 struct ecommunity *ecomdel);
d62a17ae 380extern int bgp_evpn_handle_export_rt_change(struct bgp *bgp,
381 struct bgpevpn *vpn);
676f83b9 382extern void bgp_evpn_handle_vrf_rd_change(struct bgp *bgp_vrf, int withdraw);
d62a17ae 383extern void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
384 int withdraw);
385extern int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn);
386extern int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn);
523cafc4 387extern void bgp_evpn_map_vrf_to_its_rts(struct bgp *bgp_vrf);
388extern void bgp_evpn_unmap_vrf_from_its_rts(struct bgp *bgp_vrf);
d62a17ae 389extern void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn);
390extern void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp,
391 struct bgpevpn *vpn);
392extern void bgp_evpn_derive_auto_rt_import(struct bgp *bgp,
393 struct bgpevpn *vpn);
394extern void bgp_evpn_derive_auto_rt_export(struct bgp *bgp,
395 struct bgpevpn *vpn);
396extern void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn);
676f83b9 397extern void bgp_evpn_derive_auto_rd_for_vrf(struct bgp *bgp);
d62a17ae 398extern struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni);
399extern struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
29c53922
MK
400 struct in_addr originator_ip,
401 vrf_id_t tenant_vrf_id);
d62a17ae 402extern void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn);
a4168ebc 403#endif /* _BGP_EVPN_PRIVATE_H */