]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_evpn_private.h
Merge pull request #2195 from qlyoung/docuser
[mirror_frr.git] / bgpd / bgp_evpn_private.h
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
31 #define RT_ADDRSTRLEN 28
32
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
37
38 /* EVPN route types. */
39 typedef 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 */
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 */
55 struct bgpevpn {
56 vni_t vni;
57 vrf_id_t tenant_vrf_id;
58 uint32_t flags;
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 */
66
67 struct bgp *bgp_vrf; /* back pointer to the vrf instance */
68
69 /* Flag to indicate if we are
70 * advertising the g/w mac ip for
71 * this VNI*/
72 uint8_t advertise_gw_macip;
73
74 /* Flag to indicate if we are
75 * advertising subnet for this VNI */
76 uint8_t advertise_subnet;
77
78 /* Id for deriving the RD
79 * automatically for this VNI */
80 uint16_t rd_id;
81
82 /* RD for this VNI. */
83 struct prefix_rd prd;
84
85 /* Route type 3 field */
86 struct in_addr originator_ip;
87
88 /* Import and Export RTs. */
89 struct list *import_rtl;
90 struct list *export_rtl;
91
92 /* Route table for EVPN routes for
93 * this VNI. */
94 struct bgp_table *route_table;
95
96 QOBJ_FIELDS
97 };
98
99 DECLARE_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 */
105 struct irt_node {
106 /* RT */
107 struct ecommunity_val rt;
108
109 /* List of VNIs importing routes matching this RT. */
110 struct list *vnis;
111 };
112
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 */
117 struct 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
126 #define RT_TYPE_IMPORT 1
127 #define RT_TYPE_EXPORT 2
128 #define RT_TYPE_BOTH 3
129
130 static inline int is_vrf_rd_configured(struct bgp *bgp_vrf)
131 {
132 return (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_RD_CFGD));
133 }
134
135 static 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
141 static inline vni_t bgpevpn_get_l3vni(struct bgpevpn *vpn)
142 {
143 return vpn->bgp_vrf ? vpn->bgp_vrf->l3vni : 0;
144 }
145
146 static inline void bgpevpn_get_rmac(struct bgpevpn *vpn, struct ethaddr *rmac)
147 {
148 memset(rmac, 0, sizeof(struct ethaddr));
149 if (!vpn->bgp_vrf)
150 return;
151 memcpy(rmac, &vpn->bgp_vrf->rmac, sizeof(struct ethaddr));
152 }
153
154 static inline struct list *bgpevpn_get_vrf_export_rtl(struct bgpevpn *vpn)
155 {
156 if (!vpn->bgp_vrf)
157 return NULL;
158
159 return vpn->bgp_vrf->vrf_export_rtl;
160 }
161
162 static inline struct list *bgpevpn_get_vrf_import_rtl(struct bgpevpn *vpn)
163 {
164 if (!vpn->bgp_vrf)
165 return NULL;
166
167 return vpn->bgp_vrf->vrf_import_rtl;
168 }
169
170 static inline void bgpevpn_unlink_from_l3vni(struct bgpevpn *vpn)
171 {
172 /* bail if vpn is not associated to bgp_vrf */
173 if (!vpn->bgp_vrf)
174 return;
175
176 UNSET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
177 listnode_delete(vpn->bgp_vrf->l2vnis, vpn);
178
179 /* remove the backpointer to the vrf instance */
180 vpn->bgp_vrf = NULL;
181 }
182
183 static inline void bgpevpn_link_to_l3vni(struct bgpevpn *vpn)
184 {
185 struct bgp *bgp_vrf = NULL;
186
187 /* bail if vpn is already associated to vrf */
188 if (vpn->bgp_vrf)
189 return;
190
191 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
192 if (!bgp_vrf)
193 return;
194
195 /* associate the vpn to the bgp_vrf instance */
196 vpn->bgp_vrf = bgp_vrf;
197 listnode_add_sort(bgp_vrf->l2vnis, vpn);
198
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);
202 }
203
204 static inline int is_vni_configured(struct bgpevpn *vpn)
205 {
206 return (CHECK_FLAG(vpn->flags, VNI_FLAG_CFGD));
207 }
208
209 static inline int is_vni_live(struct bgpevpn *vpn)
210 {
211 return (CHECK_FLAG(vpn->flags, VNI_FLAG_LIVE));
212 }
213
214 static inline int is_rd_configured(struct bgpevpn *vpn)
215 {
216 return (CHECK_FLAG(vpn->flags, VNI_FLAG_RD_CFGD));
217 }
218
219 static inline int bgp_evpn_rd_matches_existing(struct bgpevpn *vpn,
220 struct prefix_rd *prd)
221 {
222 return (memcmp(&vpn->prd.val, prd->val, ECOMMUNITY_SIZE) == 0);
223 }
224
225 static inline int is_import_rt_configured(struct bgpevpn *vpn)
226 {
227 return (CHECK_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD));
228 }
229
230 static inline int is_export_rt_configured(struct bgpevpn *vpn)
231 {
232 return (CHECK_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD));
233 }
234
235 static inline int is_vni_param_configured(struct bgpevpn *vpn)
236 {
237 return (is_rd_configured(vpn) || is_import_rt_configured(vpn)
238 || is_export_rt_configured(vpn));
239 }
240
241 static 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
250 static 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
257 static inline void encode_mac_mobility_extcomm(int static_mac, uint32_t seq,
258 struct ecommunity_val *eval)
259 {
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;
269 }
270
271 static 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.prefix_addr.ip_prefix_length;
278 memcpy(&(ip->u.prefix4), &(evp->prefix.prefix_addr.ip.ip),
279 IPV4_MAX_BYTELEN);
280 } else if (is_evpn_prefix_ipaddr_v6(evp)) {
281 ip->family = AF_INET6;
282 ip->prefixlen = evp->prefix.prefix_addr.ip_prefix_length;
283 memcpy(&(ip->u.prefix6), &(evp->prefix.prefix_addr.ip.ip),
284 IPV6_MAX_BYTELEN);
285 }
286 }
287
288 static inline int is_evpn_prefix_default(struct prefix *evp)
289 {
290 if (evp->family != AF_EVPN)
291 return 0;
292
293 return ((evp->u.prefix_evpn.prefix_addr.ip_prefix_length == 0) ?
294 1 : 0);
295 }
296
297 static inline void ip_prefix_from_type2_prefix(struct prefix_evpn *evp,
298 struct prefix *ip)
299 {
300 memset(ip, 0, sizeof(struct prefix));
301 if (is_evpn_prefix_ipaddr_v4(evp)) {
302 ip->family = AF_INET;
303 ip->prefixlen = IPV4_MAX_BITLEN;
304 memcpy(&(ip->u.prefix4), &(evp->prefix.macip_addr.ip.ip),
305 IPV4_MAX_BYTELEN);
306 } else if (is_evpn_prefix_ipaddr_v6(evp)) {
307 ip->family = AF_INET6;
308 ip->prefixlen = IPV6_MAX_BITLEN;
309 memcpy(&(ip->u.prefix6), &(evp->prefix.macip_addr.ip.ip),
310 IPV6_MAX_BYTELEN);
311 }
312 }
313
314 static inline void ip_prefix_from_evpn_prefix(struct prefix_evpn *evp,
315 struct prefix *ip)
316 {
317 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
318 ip_prefix_from_type2_prefix(evp, ip);
319 else if (evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE)
320 ip_prefix_from_type5_prefix(evp, ip);
321 }
322
323 static inline void build_evpn_type2_prefix(struct prefix_evpn *p,
324 struct ethaddr *mac,
325 struct ipaddr *ip)
326 {
327 memset(p, 0, sizeof(struct prefix_evpn));
328 p->family = AF_EVPN;
329 p->prefixlen = EVPN_TYPE_2_ROUTE_PREFIXLEN;
330 p->prefix.route_type = BGP_EVPN_MAC_IP_ROUTE;
331 memcpy(&p->prefix.macip_addr.mac.octet, mac->octet, ETH_ALEN);
332 p->prefix.macip_addr.ip.ipa_type = IPADDR_NONE;
333 if (ip)
334 memcpy(&p->prefix.macip_addr.ip, ip, sizeof(*ip));
335 }
336
337 static inline void build_type5_prefix_from_ip_prefix(struct prefix_evpn *evp,
338 struct prefix *ip_prefix)
339 {
340 struct ipaddr ip;
341
342 memset(&ip, 0, sizeof(struct ipaddr));
343 if (ip_prefix->family == AF_INET) {
344 ip.ipa_type = IPADDR_V4;
345 memcpy(&ip.ipaddr_v4, &ip_prefix->u.prefix4,
346 sizeof(struct in_addr));
347 } else {
348 ip.ipa_type = IPADDR_V6;
349 memcpy(&ip.ipaddr_v6, &ip_prefix->u.prefix6,
350 sizeof(struct in6_addr));
351 }
352
353 memset(evp, 0, sizeof(struct prefix_evpn));
354 evp->family = AF_EVPN;
355 evp->prefixlen = EVPN_TYPE_5_ROUTE_PREFIXLEN;
356 evp->prefix.route_type = BGP_EVPN_IP_PREFIX_ROUTE;
357 evp->prefix.prefix_addr.ip_prefix_length = ip_prefix->prefixlen;
358 evp->prefix.prefix_addr.ip.ipa_type = ip.ipa_type;
359 memcpy(&evp->prefix.prefix_addr.ip, &ip, sizeof(struct ipaddr));
360 }
361
362 static inline void build_evpn_type3_prefix(struct prefix_evpn *p,
363 struct in_addr originator_ip)
364 {
365 memset(p, 0, sizeof(struct prefix_evpn));
366 p->family = AF_EVPN;
367 p->prefixlen = EVPN_TYPE_3_ROUTE_PREFIXLEN;
368 p->prefix.route_type = BGP_EVPN_IMET_ROUTE;
369 p->prefix.imet_addr.ip.ipa_type = IPADDR_V4;
370 p->prefix.imet_addr.ip.ipaddr_v4 = originator_ip;
371 }
372
373 static inline int evpn_default_originate_set(struct bgp *bgp, afi_t afi,
374 safi_t safi)
375 {
376 if (afi == AFI_IP &&
377 CHECK_FLAG(bgp->af_flags[AFI_L2VPN][SAFI_EVPN],
378 BGP_L2VPN_EVPN_DEFAULT_ORIGINATE_IPV4))
379 return 1;
380 else if (afi == AFI_IP6 &&
381 CHECK_FLAG(bgp->af_flags[AFI_L2VPN][SAFI_EVPN],
382 BGP_L2VPN_EVPN_DEFAULT_ORIGINATE_IPV6))
383 return 1;
384 return 0;
385 }
386
387 extern void evpn_rt_delete_auto(struct bgp *, vni_t, struct list *);
388 extern void bgp_evpn_configure_export_rt_for_vrf(struct bgp *bgp_vrf,
389 struct ecommunity *ecomadd);
390 extern void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp *bgp_vrf,
391 struct ecommunity *ecomdel);
392 extern void bgp_evpn_configure_import_rt_for_vrf(struct bgp *bgp_vrf,
393 struct ecommunity *ecomadd);
394 extern void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp *bgp_vrf,
395 struct ecommunity *ecomdel);
396 extern int bgp_evpn_handle_export_rt_change(struct bgp *bgp,
397 struct bgpevpn *vpn);
398 extern void bgp_evpn_handle_vrf_rd_change(struct bgp *bgp_vrf, int withdraw);
399 extern void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
400 int withdraw);
401 extern int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn);
402 extern int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn);
403 extern void bgp_evpn_map_vrf_to_its_rts(struct bgp *bgp_vrf);
404 extern void bgp_evpn_unmap_vrf_from_its_rts(struct bgp *bgp_vrf);
405 extern void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn);
406 extern void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp,
407 struct bgpevpn *vpn);
408 extern void bgp_evpn_derive_auto_rt_import(struct bgp *bgp,
409 struct bgpevpn *vpn);
410 extern void bgp_evpn_derive_auto_rt_export(struct bgp *bgp,
411 struct bgpevpn *vpn);
412 extern void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn);
413 extern void bgp_evpn_derive_auto_rd_for_vrf(struct bgp *bgp);
414 extern struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni);
415 extern struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
416 struct in_addr originator_ip,
417 vrf_id_t tenant_vrf_id);
418 extern void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn);
419 #endif /* _BGP_EVPN_PRIVATE_H */