]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_evpn_private.h
bgpd: advertise VNI subnet
[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 u_int32_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
65 /* Flag to indicate if we are advertising the g/w mac ip for this VNI*/
66 u_int8_t advertise_gw_macip;
67
68 /* Flag to indicate if we are advertising subnet for this VNI */
69 u_int8_t advertise_subnet;
70
71 /* Id for deriving the RD automatically for this VNI */
72 u_int16_t rd_id;
73
74 /* RD for this VNI. */
75 struct prefix_rd prd;
76
77 /* Route type 3 field */
78 struct in_addr originator_ip;
79
80 /* Import and Export RTs. */
81 struct list *import_rtl;
82 struct list *export_rtl;
83
84 /* Route table for EVPN routes for this VNI. */
85 struct bgp_table *route_table;
86
87 QOBJ_FIELDS
88 };
89
90 DECLARE_QOBJ_TYPE(bgpevpn)
91
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.
95 */
96 struct irt_node {
97 /* RT */
98 struct ecommunity_val rt;
99
100 /* List of VNIs importing routes matching this RT. */
101 struct list *vnis;
102 };
103
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.
107 */
108 struct vrf_irt_node {
109 /* RT */
110 struct ecommunity_val rt;
111
112 /* List of VNIs importing routes matching this RT. */
113 struct list *vrfs;
114 };
115
116
117 #define RT_TYPE_IMPORT 1
118 #define RT_TYPE_EXPORT 2
119 #define RT_TYPE_BOTH 3
120
121 static inline int is_vrf_rd_configured(struct bgp *bgp_vrf)
122 {
123 return (CHECK_FLAG(bgp_vrf->vrf_flags,
124 BGP_VRF_RD_CFGD));
125 }
126
127 static inline int bgp_evpn_vrf_rd_matches_existing(struct bgp *bgp_vrf,
128 struct prefix_rd *prd)
129 {
130 return (memcmp(&bgp_vrf->vrf_prd.val, prd->val, ECOMMUNITY_SIZE) == 0);
131 }
132
133 static inline vni_t bgpevpn_get_l3vni(struct bgpevpn *vpn)
134 {
135 struct bgp *bgp_vrf = NULL;
136
137 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
138 if (!bgp_vrf)
139 return 0;
140
141 return bgp_vrf->l3vni;
142 }
143
144 static inline void bgpevpn_get_rmac(struct bgpevpn *vpn, struct ethaddr *rmac)
145 {
146 struct bgp *bgp_vrf = NULL;
147
148 memset(rmac, 0, sizeof(struct ethaddr));
149 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
150 if (!bgp_vrf)
151 return;
152 memcpy(rmac, &bgp_vrf->rmac, sizeof(struct ethaddr));
153 }
154
155 static inline struct list *bgpevpn_get_vrf_export_rtl(struct bgpevpn *vpn)
156 {
157 struct bgp *bgp_vrf = NULL;
158
159 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
160 if (!bgp_vrf)
161 return NULL;
162
163 return bgp_vrf->vrf_export_rtl;
164 }
165
166 static inline struct list *bgpevpn_get_vrf_import_rtl(struct bgpevpn *vpn)
167 {
168 struct bgp *bgp_vrf = NULL;
169
170 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
171 if (!bgp_vrf)
172 return NULL;
173
174 return bgp_vrf->vrf_import_rtl;
175 }
176
177 static inline void bgpevpn_unlink_from_l3vni(struct bgpevpn *vpn)
178 {
179 struct bgp *bgp_vrf = NULL;
180
181 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
182 if (!bgp_vrf || !bgp_vrf->l2vnis)
183 return;
184 listnode_delete(bgp_vrf->l2vnis, vpn);
185 }
186
187 static inline void bgpevpn_link_to_l3vni(struct bgpevpn *vpn)
188 {
189 struct bgp *bgp_vrf = NULL;
190
191 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
192 if (!bgp_vrf || !bgp_vrf->l2vnis)
193 return;
194 listnode_add_sort(bgp_vrf->l2vnis, vpn);
195 }
196
197 static inline int is_vni_configured(struct bgpevpn *vpn)
198 {
199 return (CHECK_FLAG(vpn->flags, VNI_FLAG_CFGD));
200 }
201
202 static inline int is_vni_live(struct bgpevpn *vpn)
203 {
204 return (CHECK_FLAG(vpn->flags, VNI_FLAG_LIVE));
205 }
206
207 static inline int is_rd_configured(struct bgpevpn *vpn)
208 {
209 return (CHECK_FLAG(vpn->flags, VNI_FLAG_RD_CFGD));
210 }
211
212 static inline int bgp_evpn_rd_matches_existing(struct bgpevpn *vpn,
213 struct prefix_rd *prd)
214 {
215 return (memcmp(&vpn->prd.val, prd->val, ECOMMUNITY_SIZE) == 0);
216 }
217
218 static inline int is_import_rt_configured(struct bgpevpn *vpn)
219 {
220 return (CHECK_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD));
221 }
222
223 static inline int is_export_rt_configured(struct bgpevpn *vpn)
224 {
225 return (CHECK_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD));
226 }
227
228 static inline int is_vni_param_configured(struct bgpevpn *vpn)
229 {
230 return (is_rd_configured(vpn) || is_import_rt_configured(vpn)
231 || is_export_rt_configured(vpn));
232 }
233
234 static inline void encode_rmac_extcomm(struct ecommunity_val *eval,
235 struct ethaddr *rmac)
236 {
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);
241 }
242
243 static inline void encode_default_gw_extcomm(struct ecommunity_val *eval)
244 {
245 memset(eval, 0, sizeof(*eval));
246 eval->val[0] = ECOMMUNITY_ENCODE_OPAQUE;
247 eval->val[1] = ECOMMUNITY_EVPN_SUBTYPE_DEF_GW;
248 }
249
250 static inline void encode_mac_mobility_extcomm(int static_mac, u_int32_t seq,
251 struct ecommunity_val *eval)
252 {
253 memset(eval, 0, sizeof(*eval));
254 eval->val[0] = ECOMMUNITY_ENCODE_EVPN;
255 eval->val[1] = ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY;
256 if (static_mac)
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;
262 }
263
264 static inline void ip_prefix_from_type5_prefix(struct prefix_evpn *evp,
265 struct prefix *ip)
266 {
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),
273 IPV4_MAX_BYTELEN);
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),
279 IPV6_MAX_BYTELEN);
280 }
281 }
282
283 static inline void ip_prefix_from_type2_prefix(struct prefix_evpn *evp,
284 struct prefix *ip)
285 {
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),
292 IPV4_MAX_BYTELEN);
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),
298 IPV6_MAX_BYTELEN);
299 }
300 }
301
302 static inline void build_evpn_type2_prefix(struct prefix_evpn *p,
303 struct ethaddr *mac,
304 struct ipaddr *ip)
305 {
306 memset(p, 0, sizeof(struct prefix_evpn));
307 p->family = AF_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;
312 if (ip)
313 memcpy(&p->prefix.ip, ip, sizeof(*ip));
314 }
315
316 static inline void build_type5_prefix_from_ip_prefix(struct prefix_evpn *evp,
317 struct prefix *ip_prefix)
318 {
319 struct ipaddr ip;
320
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));
326 } else {
327 ip.ipa_type = IPADDR_V6;
328 memcpy(&ip.ipaddr_v6, &ip_prefix->u.prefix6,
329 sizeof(struct in6_addr));
330 }
331
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));
339 }
340
341 static inline void build_evpn_type3_prefix(struct prefix_evpn *p,
342 struct in_addr originator_ip)
343 {
344 memset(p, 0, sizeof(struct prefix_evpn));
345 p->family = AF_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;
350 }
351
352 static inline int advertise_type5_routes(struct bgp *bgp_vrf,
353 afi_t afi)
354 {
355 if (!bgp_vrf->l3vni)
356 return 0;
357
358 if (afi == AFI_IP &&
359 CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_ADVERTISE_IPV4_IN_EVPN))
360 return 1;
361
362 if (afi == AFI_IP6 &&
363 CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_ADVERTISE_IPV6_IN_EVPN))
364 return 1;
365
366 return 0;
367 }
368
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,
382 int withdraw);
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 */