]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_evpn_private.h
Merge pull request #2684 from netravnen/feature/bgpd/well-known-communities
[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_ROUTE_PREFIXLEN 224
35
36 /* EVPN route types. */
37 typedef enum {
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;
44
45 /*
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").
52 */
53 struct bgpevpn {
54 vni_t vni;
55 vrf_id_t tenant_vrf_id;
56 uint32_t flags;
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 */
64
65 struct bgp *bgp_vrf; /* back pointer to the vrf instance */
66
67 /* Flag to indicate if we are
68 * advertising the g/w mac ip for
69 * this VNI*/
70 uint8_t advertise_gw_macip;
71
72 /* Flag to indicate if we are
73 * advertising subnet for this VNI */
74 uint8_t advertise_subnet;
75
76 /* Id for deriving the RD
77 * automatically for this VNI */
78 uint16_t rd_id;
79
80 /* RD for this VNI. */
81 struct prefix_rd prd;
82
83 /* Route type 3 field */
84 struct in_addr originator_ip;
85
86 /* Import and Export RTs. */
87 struct list *import_rtl;
88 struct list *export_rtl;
89
90 /* Route table for EVPN routes for
91 * this VNI. */
92 struct bgp_table *route_table;
93
94 QOBJ_FIELDS
95 };
96
97 DECLARE_QOBJ_TYPE(bgpevpn)
98
99 struct evpnes {
100
101 /* Ethernet Segment Identifier */
102 esi_t esi;
103
104 /* es flags */
105 uint16_t flags;
106 #define EVPNES_LOCAL 0x01
107 #define EVPNES_REMOTE 0x02
108
109 /*
110 * Id for deriving the RD
111 * automatically for this ESI
112 */
113 uint16_t rd_id;
114
115 /* RD for this VNI. */
116 struct prefix_rd prd;
117
118 /* originator ip address */
119 struct ipaddr originator_ip;
120
121 /* list of VTEPs in the same site */
122 struct list *vtep_list;
123
124 /*
125 * Route table for EVPN routes for
126 * this ESI. - type4 routes
127 */
128 struct bgp_table *route_table;
129
130 QOBJ_FIELDS
131 };
132
133 DECLARE_QOBJ_TYPE(evpnes)
134
135 /* Mapping of Import RT to VNIs.
136 * The Import RTs of all VNIs are maintained in a hash table with each
137 * RT linking to all VNIs that will import routes matching this RT.
138 */
139 struct irt_node {
140 /* RT */
141 struct ecommunity_val rt;
142
143 /* List of VNIs importing routes matching this RT. */
144 struct list *vnis;
145 };
146
147 /* Mapping of Import RT to VRFs.
148 * The Import RTs of all VRFss are maintained in a hash table with each
149 * RT linking to all VRFs that will import routes matching this RT.
150 */
151 struct vrf_irt_node {
152 /* RT */
153 struct ecommunity_val rt;
154
155 /* List of VNIs importing routes matching this RT. */
156 struct list *vrfs;
157 };
158
159
160 #define RT_TYPE_IMPORT 1
161 #define RT_TYPE_EXPORT 2
162 #define RT_TYPE_BOTH 3
163
164 static inline int is_vrf_rd_configured(struct bgp *bgp_vrf)
165 {
166 return (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_RD_CFGD));
167 }
168
169 static inline int bgp_evpn_vrf_rd_matches_existing(struct bgp *bgp_vrf,
170 struct prefix_rd *prd)
171 {
172 return (memcmp(&bgp_vrf->vrf_prd.val, prd->val, ECOMMUNITY_SIZE) == 0);
173 }
174
175 static inline vni_t bgpevpn_get_l3vni(struct bgpevpn *vpn)
176 {
177 return vpn->bgp_vrf ? vpn->bgp_vrf->l3vni : 0;
178 }
179
180 static inline void bgpevpn_get_rmac(struct bgpevpn *vpn, struct ethaddr *rmac)
181 {
182 memset(rmac, 0, sizeof(struct ethaddr));
183 if (!vpn->bgp_vrf)
184 return;
185 memcpy(rmac, &vpn->bgp_vrf->rmac, sizeof(struct ethaddr));
186 }
187
188 static inline struct list *bgpevpn_get_vrf_export_rtl(struct bgpevpn *vpn)
189 {
190 if (!vpn->bgp_vrf)
191 return NULL;
192
193 return vpn->bgp_vrf->vrf_export_rtl;
194 }
195
196 static inline struct list *bgpevpn_get_vrf_import_rtl(struct bgpevpn *vpn)
197 {
198 if (!vpn->bgp_vrf)
199 return NULL;
200
201 return vpn->bgp_vrf->vrf_import_rtl;
202 }
203
204 static inline void bgpevpn_unlink_from_l3vni(struct bgpevpn *vpn)
205 {
206 /* bail if vpn is not associated to bgp_vrf */
207 if (!vpn->bgp_vrf)
208 return;
209
210 UNSET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
211 listnode_delete(vpn->bgp_vrf->l2vnis, vpn);
212
213 /* remove the backpointer to the vrf instance */
214 vpn->bgp_vrf = NULL;
215 }
216
217 static inline void bgpevpn_link_to_l3vni(struct bgpevpn *vpn)
218 {
219 struct bgp *bgp_vrf = NULL;
220
221 /* bail if vpn is already associated to vrf */
222 if (vpn->bgp_vrf)
223 return;
224
225 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
226 if (!bgp_vrf)
227 return;
228
229 /* associate the vpn to the bgp_vrf instance */
230 vpn->bgp_vrf = bgp_vrf;
231 listnode_add_sort(bgp_vrf->l2vnis, vpn);
232
233 /* check if we are advertising two labels for this vpn */
234 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY))
235 SET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
236 }
237
238 static inline int is_vni_configured(struct bgpevpn *vpn)
239 {
240 return (CHECK_FLAG(vpn->flags, VNI_FLAG_CFGD));
241 }
242
243 static inline int is_vni_live(struct bgpevpn *vpn)
244 {
245 return (CHECK_FLAG(vpn->flags, VNI_FLAG_LIVE));
246 }
247
248 static inline int is_rd_configured(struct bgpevpn *vpn)
249 {
250 return (CHECK_FLAG(vpn->flags, VNI_FLAG_RD_CFGD));
251 }
252
253 static inline int bgp_evpn_rd_matches_existing(struct bgpevpn *vpn,
254 struct prefix_rd *prd)
255 {
256 return (memcmp(&vpn->prd.val, prd->val, ECOMMUNITY_SIZE) == 0);
257 }
258
259 static inline int is_import_rt_configured(struct bgpevpn *vpn)
260 {
261 return (CHECK_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD));
262 }
263
264 static inline int is_export_rt_configured(struct bgpevpn *vpn)
265 {
266 return (CHECK_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD));
267 }
268
269 static inline int is_vni_param_configured(struct bgpevpn *vpn)
270 {
271 return (is_rd_configured(vpn) || is_import_rt_configured(vpn)
272 || is_export_rt_configured(vpn));
273 }
274
275 static inline void encode_es_rt_extcomm(struct ecommunity_val *eval,
276 struct ethaddr *mac)
277 {
278 memset(eval, 0, sizeof(struct ecommunity_val));
279 eval->val[0] = ECOMMUNITY_ENCODE_EVPN;
280 eval->val[1] = ECOMMUNITY_EVPN_SUBTYPE_ES_IMPORT_RT;
281 memcpy(&eval->val[2], mac, ETH_ALEN);
282 }
283
284 static inline void encode_rmac_extcomm(struct ecommunity_val *eval,
285 struct ethaddr *rmac)
286 {
287 memset(eval, 0, sizeof(*eval));
288 eval->val[0] = ECOMMUNITY_ENCODE_EVPN;
289 eval->val[1] = ECOMMUNITY_EVPN_SUBTYPE_ROUTERMAC;
290 memcpy(&eval->val[2], rmac, ETH_ALEN);
291 }
292
293 static inline void encode_default_gw_extcomm(struct ecommunity_val *eval)
294 {
295 memset(eval, 0, sizeof(*eval));
296 eval->val[0] = ECOMMUNITY_ENCODE_OPAQUE;
297 eval->val[1] = ECOMMUNITY_EVPN_SUBTYPE_DEF_GW;
298 }
299
300 static inline void encode_mac_mobility_extcomm(int static_mac, uint32_t seq,
301 struct ecommunity_val *eval)
302 {
303 memset(eval, 0, sizeof(*eval));
304 eval->val[0] = ECOMMUNITY_ENCODE_EVPN;
305 eval->val[1] = ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY;
306 if (static_mac)
307 eval->val[2] = ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY_FLAG_STICKY;
308 eval->val[4] = (seq >> 24) & 0xff;
309 eval->val[5] = (seq >> 16) & 0xff;
310 eval->val[6] = (seq >> 8) & 0xff;
311 eval->val[7] = seq & 0xff;
312 }
313
314 static inline void encode_na_flag_extcomm(struct ecommunity_val *eval,
315 uint8_t na_flag)
316 {
317 memset(eval, 0, sizeof(*eval));
318 eval->val[0] = ECOMMUNITY_ENCODE_EVPN;
319 eval->val[1] = ECOMMUNITY_EVPN_SUBTYPE_ND;
320 if (na_flag)
321 eval->val[2] |= ECOMMUNITY_EVPN_SUBTYPE_ND_ROUTER_FLAG;
322 }
323
324 static inline void ip_prefix_from_type5_prefix(struct prefix_evpn *evp,
325 struct prefix *ip)
326 {
327 memset(ip, 0, sizeof(struct prefix));
328 if (is_evpn_prefix_ipaddr_v4(evp)) {
329 ip->family = AF_INET;
330 ip->prefixlen = evp->prefix.prefix_addr.ip_prefix_length;
331 memcpy(&(ip->u.prefix4), &(evp->prefix.prefix_addr.ip.ip),
332 IPV4_MAX_BYTELEN);
333 } else if (is_evpn_prefix_ipaddr_v6(evp)) {
334 ip->family = AF_INET6;
335 ip->prefixlen = evp->prefix.prefix_addr.ip_prefix_length;
336 memcpy(&(ip->u.prefix6), &(evp->prefix.prefix_addr.ip.ip),
337 IPV6_MAX_BYTELEN);
338 }
339 }
340
341 static inline int is_evpn_prefix_default(const struct prefix *evp)
342 {
343 if (evp->family != AF_EVPN)
344 return 0;
345
346 return ((evp->u.prefix_evpn.prefix_addr.ip_prefix_length == 0) ?
347 1 : 0);
348 }
349
350 static inline void ip_prefix_from_type2_prefix(struct prefix_evpn *evp,
351 struct prefix *ip)
352 {
353 memset(ip, 0, sizeof(struct prefix));
354 if (is_evpn_prefix_ipaddr_v4(evp)) {
355 ip->family = AF_INET;
356 ip->prefixlen = IPV4_MAX_BITLEN;
357 memcpy(&(ip->u.prefix4), &(evp->prefix.macip_addr.ip.ip),
358 IPV4_MAX_BYTELEN);
359 } else if (is_evpn_prefix_ipaddr_v6(evp)) {
360 ip->family = AF_INET6;
361 ip->prefixlen = IPV6_MAX_BITLEN;
362 memcpy(&(ip->u.prefix6), &(evp->prefix.macip_addr.ip.ip),
363 IPV6_MAX_BYTELEN);
364 }
365 }
366
367 static inline void ip_prefix_from_evpn_prefix(struct prefix_evpn *evp,
368 struct prefix *ip)
369 {
370 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
371 ip_prefix_from_type2_prefix(evp, ip);
372 else if (evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE)
373 ip_prefix_from_type5_prefix(evp, ip);
374 }
375
376 static inline void build_evpn_type2_prefix(struct prefix_evpn *p,
377 struct ethaddr *mac,
378 struct ipaddr *ip)
379 {
380 memset(p, 0, sizeof(struct prefix_evpn));
381 p->family = AF_EVPN;
382 p->prefixlen = EVPN_ROUTE_PREFIXLEN;
383 p->prefix.route_type = BGP_EVPN_MAC_IP_ROUTE;
384 memcpy(&p->prefix.macip_addr.mac.octet, mac->octet, ETH_ALEN);
385 p->prefix.macip_addr.ip.ipa_type = IPADDR_NONE;
386 if (ip)
387 memcpy(&p->prefix.macip_addr.ip, ip, sizeof(*ip));
388 }
389
390 static inline void build_type5_prefix_from_ip_prefix(struct prefix_evpn *evp,
391 struct prefix *ip_prefix)
392 {
393 struct ipaddr ip;
394
395 memset(&ip, 0, sizeof(struct ipaddr));
396 if (ip_prefix->family == AF_INET) {
397 ip.ipa_type = IPADDR_V4;
398 memcpy(&ip.ipaddr_v4, &ip_prefix->u.prefix4,
399 sizeof(struct in_addr));
400 } else {
401 ip.ipa_type = IPADDR_V6;
402 memcpy(&ip.ipaddr_v6, &ip_prefix->u.prefix6,
403 sizeof(struct in6_addr));
404 }
405
406 memset(evp, 0, sizeof(struct prefix_evpn));
407 evp->family = AF_EVPN;
408 evp->prefixlen = EVPN_ROUTE_PREFIXLEN;
409 evp->prefix.route_type = BGP_EVPN_IP_PREFIX_ROUTE;
410 evp->prefix.prefix_addr.ip_prefix_length = ip_prefix->prefixlen;
411 evp->prefix.prefix_addr.ip.ipa_type = ip.ipa_type;
412 memcpy(&evp->prefix.prefix_addr.ip, &ip, sizeof(struct ipaddr));
413 }
414
415 static inline void build_evpn_type3_prefix(struct prefix_evpn *p,
416 struct in_addr originator_ip)
417 {
418 memset(p, 0, sizeof(struct prefix_evpn));
419 p->family = AF_EVPN;
420 p->prefixlen = EVPN_ROUTE_PREFIXLEN;
421 p->prefix.route_type = BGP_EVPN_IMET_ROUTE;
422 p->prefix.imet_addr.ip.ipa_type = IPADDR_V4;
423 p->prefix.imet_addr.ip.ipaddr_v4 = originator_ip;
424 }
425
426 static inline void build_evpn_type4_prefix(struct prefix_evpn *p,
427 esi_t *esi,
428 struct in_addr originator_ip)
429 {
430 memset(p, 0, sizeof(struct prefix_evpn));
431 p->family = AF_EVPN;
432 p->prefixlen = EVPN_ROUTE_PREFIXLEN;
433 p->prefix.route_type = BGP_EVPN_ES_ROUTE;
434 p->prefix.es_addr.ip_prefix_length = IPV4_MAX_BITLEN;
435 p->prefix.es_addr.ip.ipa_type = IPADDR_V4;
436 p->prefix.es_addr.ip.ipaddr_v4 = originator_ip;
437 memcpy(&p->prefix.es_addr.esi, esi, sizeof(esi_t));
438 }
439
440 static inline int evpn_default_originate_set(struct bgp *bgp, afi_t afi,
441 safi_t safi)
442 {
443 if (afi == AFI_IP &&
444 CHECK_FLAG(bgp->af_flags[AFI_L2VPN][SAFI_EVPN],
445 BGP_L2VPN_EVPN_DEFAULT_ORIGINATE_IPV4))
446 return 1;
447 else if (afi == AFI_IP6 &&
448 CHECK_FLAG(bgp->af_flags[AFI_L2VPN][SAFI_EVPN],
449 BGP_L2VPN_EVPN_DEFAULT_ORIGINATE_IPV6))
450 return 1;
451 return 0;
452 }
453
454 static inline void es_get_system_mac(esi_t *esi,
455 struct ethaddr *mac)
456 {
457 /*
458 * for type-1 and type-3 ESIs,
459 * the system mac starts at val[1]
460 */
461 memcpy(mac, &esi->val[1], ETH_ALEN);
462 }
463
464 static inline int is_es_local(struct evpnes *es)
465 {
466 return CHECK_FLAG(es->flags, EVPNES_LOCAL) ? 1 : 0;
467 }
468
469 extern void evpn_rt_delete_auto(struct bgp *, vni_t, struct list *);
470 extern void bgp_evpn_configure_export_rt_for_vrf(struct bgp *bgp_vrf,
471 struct ecommunity *ecomadd);
472 extern void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp *bgp_vrf,
473 struct ecommunity *ecomdel);
474 extern void bgp_evpn_configure_import_rt_for_vrf(struct bgp *bgp_vrf,
475 struct ecommunity *ecomadd);
476 extern void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp *bgp_vrf,
477 struct ecommunity *ecomdel);
478 extern int bgp_evpn_handle_export_rt_change(struct bgp *bgp,
479 struct bgpevpn *vpn);
480 extern void bgp_evpn_handle_autort_change(struct bgp *bgp);
481 extern void bgp_evpn_handle_vrf_rd_change(struct bgp *bgp_vrf, int withdraw);
482 extern void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
483 int withdraw);
484 extern int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn);
485 extern int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn);
486 extern void bgp_evpn_map_vrf_to_its_rts(struct bgp *bgp_vrf);
487 extern void bgp_evpn_unmap_vrf_from_its_rts(struct bgp *bgp_vrf);
488 extern void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn);
489 extern void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp,
490 struct bgpevpn *vpn);
491 extern void bgp_evpn_derive_auto_rt_import(struct bgp *bgp,
492 struct bgpevpn *vpn);
493 extern void bgp_evpn_derive_auto_rt_export(struct bgp *bgp,
494 struct bgpevpn *vpn);
495 extern void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn);
496 extern void bgp_evpn_derive_auto_rd_for_vrf(struct bgp *bgp);
497 extern struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni);
498 extern struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
499 struct in_addr originator_ip,
500 vrf_id_t tenant_vrf_id);
501 extern void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn);
502 extern struct evpnes *bgp_evpn_lookup_es(struct bgp *bgp, esi_t *esi);
503 extern struct evpnes *bgp_evpn_es_new(struct bgp *bgp, esi_t *esi,
504 struct ipaddr *originator_ip);
505 extern void bgp_evpn_es_free(struct bgp *bgp, struct evpnes *es);
506 #endif /* _BGP_EVPN_PRIVATE_H */