]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_evpn_private.h
bgpd: rd_idspace should be freed in bgp_exit
[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 */
64
1a98c087
MK
65 /* Flag to indicate if we are advertising the g/w mac ip for this VNI*/
66 u_int8_t advertise_gw_macip;
67
d62a17ae 68 /* Id for deriving the RD automatically for this VNI */
69 u_int16_t rd_id;
a4168ebc 70
d62a17ae 71 /* RD for this VNI. */
72 struct prefix_rd prd;
a4168ebc 73
d62a17ae 74 /* Route type 3 field */
75 struct in_addr originator_ip;
a4168ebc 76
d62a17ae 77 /* Import and Export RTs. */
78 struct list *import_rtl;
79 struct list *export_rtl;
a4168ebc 80
d62a17ae 81 /* Route table for EVPN routes for this VNI. */
82 struct bgp_table *route_table;
a4168ebc 83
d62a17ae 84 QOBJ_FIELDS
a4168ebc 85};
86
87DECLARE_QOBJ_TYPE(bgpevpn)
88
89/* Mapping of Import RT to VNIs.
90 * The Import RTs of all VNIs are maintained in a hash table with each
91 * RT linking to all VNIs that will import routes matching this RT.
92 */
d62a17ae 93struct irt_node {
94 /* RT */
95 struct ecommunity_val rt;
a4168ebc 96
d62a17ae 97 /* List of VNIs importing routes matching this RT. */
98 struct list *vnis;
a4168ebc 99};
100
10ebe1ab
MK
101/* Mapping of Import RT to VRFs.
102 * The Import RTs of all VRFss are maintained in a hash table with each
103 * RT linking to all VRFs that will import routes matching this RT.
104 */
105struct vrf_irt_node {
106 /* RT */
107 struct ecommunity_val rt;
108
109 /* List of VNIs importing routes matching this RT. */
110 struct list *vrfs;
111};
112
113
128ea8ab 114#define RT_TYPE_IMPORT 1
115#define RT_TYPE_EXPORT 2
116#define RT_TYPE_BOTH 3
117
676f83b9 118static inline int is_vrf_rd_configured(struct bgp *bgp_vrf)
119{
120 return (CHECK_FLAG(bgp_vrf->vrf_flags,
121 BGP_VRF_RD_CFGD));
122}
123
124static inline int bgp_evpn_vrf_rd_matches_existing(struct bgp *bgp_vrf,
125 struct prefix_rd *prd)
126{
127 return (memcmp(&bgp_vrf->vrf_prd.val, prd->val, ECOMMUNITY_SIZE) == 0);
128}
129
6d8c58b7
MK
130static inline vni_t bgpevpn_get_l3vni(struct bgpevpn *vpn)
131{
132 struct bgp *bgp_vrf = NULL;
133
134 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
135 if (!bgp_vrf)
136 return 0;
137
138 return bgp_vrf->l3vni;
139}
140
141static inline void bgpevpn_get_rmac(struct bgpevpn *vpn, struct ethaddr *rmac)
142{
143 struct bgp *bgp_vrf = NULL;
144
145 memset(rmac, 0, sizeof(struct ethaddr));
146 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
147 if (!bgp_vrf)
148 return;
149 memcpy(rmac, &bgp_vrf->rmac, sizeof(struct ethaddr));
150}
151
152static inline struct list *bgpevpn_get_vrf_export_rtl(struct bgpevpn *vpn)
153{
154 struct bgp *bgp_vrf = NULL;
155
156 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
157 if (!bgp_vrf)
158 return NULL;
159
160 return bgp_vrf->vrf_export_rtl;
161}
162
163static inline struct list *bgpevpn_get_vrf_import_rtl(struct bgpevpn *vpn)
164{
165 struct bgp *bgp_vrf = NULL;
166
167 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
168 if (!bgp_vrf)
169 return NULL;
170
171 return bgp_vrf->vrf_import_rtl;
172}
173
6a8657d0
MK
174static inline void bgpevpn_unlink_from_l3vni(struct bgpevpn *vpn)
175{
176 struct bgp *bgp_vrf = NULL;
177
178 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
179 if (!bgp_vrf || !bgp_vrf->l2vnis)
180 return;
181 listnode_delete(bgp_vrf->l2vnis, vpn);
182}
183
184static inline void bgpevpn_link_to_l3vni(struct bgpevpn *vpn)
185{
186 struct bgp *bgp_vrf = NULL;
187
188 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
189 if (!bgp_vrf || !bgp_vrf->l2vnis)
190 return;
191 listnode_add_sort(bgp_vrf->l2vnis, vpn);
192}
193
d62a17ae 194static inline int is_vni_configured(struct bgpevpn *vpn)
128ea8ab 195{
d62a17ae 196 return (CHECK_FLAG(vpn->flags, VNI_FLAG_CFGD));
128ea8ab 197}
198
d62a17ae 199static inline int is_vni_live(struct bgpevpn *vpn)
128ea8ab 200{
d62a17ae 201 return (CHECK_FLAG(vpn->flags, VNI_FLAG_LIVE));
128ea8ab 202}
203
d62a17ae 204static inline int is_rd_configured(struct bgpevpn *vpn)
128ea8ab 205{
d62a17ae 206 return (CHECK_FLAG(vpn->flags, VNI_FLAG_RD_CFGD));
128ea8ab 207}
208
d62a17ae 209static inline int bgp_evpn_rd_matches_existing(struct bgpevpn *vpn,
210 struct prefix_rd *prd)
128ea8ab 211{
d62a17ae 212 return (memcmp(&vpn->prd.val, prd->val, ECOMMUNITY_SIZE) == 0);
128ea8ab 213}
214
d62a17ae 215static inline int is_import_rt_configured(struct bgpevpn *vpn)
128ea8ab 216{
d62a17ae 217 return (CHECK_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD));
128ea8ab 218}
219
d62a17ae 220static inline int is_export_rt_configured(struct bgpevpn *vpn)
128ea8ab 221{
d62a17ae 222 return (CHECK_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD));
128ea8ab 223}
224
d62a17ae 225static inline int is_vni_param_configured(struct bgpevpn *vpn)
128ea8ab 226{
d62a17ae 227 return (is_rd_configured(vpn) || is_import_rt_configured(vpn)
228 || is_export_rt_configured(vpn));
128ea8ab 229}
230
d62a17ae 231static inline void vni2label(vni_t vni, mpls_label_t *label)
128ea8ab 232{
d62a17ae 233 u_char *tag = (u_char *)label;
234 tag[0] = (vni >> 16) & 0xFF;
235 tag[1] = (vni >> 8) & 0xFF;
236 tag[2] = vni & 0xFF;
128ea8ab 237}
238
d62a17ae 239static inline vni_t label2vni(mpls_label_t *label)
128ea8ab 240{
d62a17ae 241 u_char *tag = (u_char *)label;
242 vni_t vni;
128ea8ab 243
d62a17ae 244 vni = ((u_int32_t)*tag++ << 16);
245 vni |= (u_int32_t)*tag++ << 8;
246 vni |= (u_int32_t)(*tag & 0xFF);
128ea8ab 247
d62a17ae 248 return vni;
128ea8ab 249}
250
bc59a672
MK
251static inline void encode_rmac_extcomm(struct ecommunity_val *eval,
252 struct ethaddr *rmac)
253{
254 memset(eval, 0, sizeof(*eval));
255 eval->val[0] = ECOMMUNITY_ENCODE_EVPN;
256 eval->val[1] = ECOMMUNITY_EVPN_SUBTYPE_ROUTERMAC;
257 memcpy(&eval->val[2], rmac, ETH_ALEN);
258}
259
d62a17ae 260static inline void encode_mac_mobility_extcomm(int static_mac, u_int32_t seq,
261 struct ecommunity_val *eval)
128ea8ab 262{
d62a17ae 263 memset(eval, 0, sizeof(*eval));
264 eval->val[0] = ECOMMUNITY_ENCODE_EVPN;
265 eval->val[1] = ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY;
266 if (static_mac)
267 eval->val[2] = ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY_FLAG_STICKY;
268 eval->val[4] = (seq >> 24) & 0xff;
269 eval->val[5] = (seq >> 16) & 0xff;
270 eval->val[6] = (seq >> 8) & 0xff;
271 eval->val[7] = seq & 0xff;
128ea8ab 272}
273
90264d64
MK
274static inline void ip_prefix_from_type5_prefix(struct prefix_evpn *evp,
275 struct prefix *ip)
276{
277 memset(ip, 0, sizeof(struct prefix));
278 if (IS_EVPN_PREFIX_IPADDR_V4(evp)) {
279 ip->family = AF_INET;
280 ip->prefixlen = evp->prefix.ip_prefix_length;
281 memcpy(&(ip->u.prefix4),
282 &(evp->prefix.ip.ip),
283 IPV4_MAX_BYTELEN);
284 } else if (IS_EVPN_PREFIX_IPADDR_V6(evp)) {
285 ip->family = AF_INET6;
286 ip->prefixlen = evp->prefix.ip_prefix_length;
287 memcpy(&(ip->u.prefix6),
288 &(evp->prefix.ip.ip),
289 IPV6_MAX_BYTELEN);
290 }
291}
292
d3135ba3 293static inline void ip_prefix_from_type2_prefix(struct prefix_evpn *evp,
294 struct prefix *ip)
295{
296 memset(ip, 0, sizeof(struct prefix));
297 if (IS_EVPN_PREFIX_IPADDR_V4(evp)) {
298 ip->family = AF_INET;
299 ip->prefixlen = IPV4_MAX_BITLEN;
300 memcpy(&(ip->u.prefix4),
301 &(evp->prefix.ip.ip),
302 IPV4_MAX_BYTELEN);
303 } else if (IS_EVPN_PREFIX_IPADDR_V6(evp)) {
304 ip->family = AF_INET6;
305 ip->prefixlen = IPV6_MAX_BITLEN;
306 memcpy(&(ip->u.prefix6),
307 &(evp->prefix.ip.ip),
308 IPV6_MAX_BYTELEN);
309 }
310}
311
d62a17ae 312static inline void build_evpn_type2_prefix(struct prefix_evpn *p,
313 struct ethaddr *mac,
314 struct ipaddr *ip)
128ea8ab 315{
d62a17ae 316 memset(p, 0, sizeof(struct prefix_evpn));
b03b8898 317 p->family = AF_EVPN;
d62a17ae 318 p->prefixlen = EVPN_TYPE_2_ROUTE_PREFIXLEN;
319 p->prefix.route_type = BGP_EVPN_MAC_IP_ROUTE;
28328ea9 320 memcpy(&p->prefix.mac.octet, mac->octet, ETH_ALEN);
d62a17ae 321 p->prefix.ip.ipa_type = IPADDR_NONE;
322 if (ip)
323 memcpy(&p->prefix.ip, ip, sizeof(*ip));
128ea8ab 324}
325
342dd0c6 326static inline void build_type5_prefix_from_ip_prefix(struct prefix_evpn *evp,
327 struct prefix *ip_prefix)
328{
329 struct ipaddr ip;
330
331 memset(&ip, 0, sizeof(struct ipaddr));
332 if (ip_prefix->family == AF_INET) {
333 ip.ipa_type = IPADDR_V4;
334 memcpy(&ip.ipaddr_v4, &ip_prefix->u.prefix4,
335 sizeof(struct in_addr));
336 } else {
337 ip.ipa_type = IPADDR_V6;
338 memcpy(&ip.ipaddr_v6, &ip_prefix->u.prefix6,
339 sizeof(struct in6_addr));
340 }
341
342 memset(evp, 0, sizeof(struct prefix_evpn));
343 evp->family = AF_EVPN;
344 evp->prefixlen = EVPN_TYPE_5_ROUTE_PREFIXLEN;
345 evp->prefix.ip_prefix_length = ip_prefix->prefixlen;
346 evp->prefix.route_type = BGP_EVPN_IP_PREFIX_ROUTE;
347 evp->prefix.ip.ipa_type = ip.ipa_type;
348 memcpy(&evp->prefix.ip, &ip, sizeof(struct ipaddr));
349}
350
d62a17ae 351static inline void build_evpn_type3_prefix(struct prefix_evpn *p,
352 struct in_addr originator_ip)
128ea8ab 353{
d62a17ae 354 memset(p, 0, sizeof(struct prefix_evpn));
b03b8898 355 p->family = AF_EVPN;
d62a17ae 356 p->prefixlen = EVPN_TYPE_3_ROUTE_PREFIXLEN;
357 p->prefix.route_type = BGP_EVPN_IMET_ROUTE;
358 p->prefix.ip.ipa_type = IPADDR_V4;
359 p->prefix.ip.ipaddr_v4 = originator_ip;
128ea8ab 360}
361
06d2e8f3
MK
362static inline int advertise_type5_routes(struct bgp *bgp_vrf,
363 afi_t afi)
364{
365 if (!bgp_vrf->l3vni)
366 return 0;
367
368 if (afi == AFI_IP &&
369 CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_ADVERTISE_IPV4_IN_EVPN))
370 return 1;
371
372 if (afi == AFI_IP6 &&
373 CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_ADVERTISE_IPV6_IN_EVPN))
374 return 1;
375
376 return 0;
377}
378
c581d8b0
MK
379extern void evpn_rt_delete_auto(struct bgp*, vni_t, struct list*);
380extern void bgp_evpn_configure_export_rt_for_vrf(struct bgp*,
381 struct ecommunity*);
382extern void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp*,
383 struct ecommunity*);
384extern void bgp_evpn_configure_import_rt_for_vrf(struct bgp*,
385 struct ecommunity*);
386extern void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp*,
387 struct ecommunity*);
d62a17ae 388extern int bgp_evpn_handle_export_rt_change(struct bgp *bgp,
389 struct bgpevpn *vpn);
676f83b9 390extern void bgp_evpn_handle_vrf_rd_change(struct bgp *bgp_vrf, int withdraw);
d62a17ae 391extern void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
392 int withdraw);
393extern int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn);
394extern int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn);
10ebe1ab
MK
395extern void bgp_evpn_map_vrf_to_its_rts(struct bgp *);
396extern void bgp_evpn_unmap_vrf_from_its_rts(struct bgp *);
d62a17ae 397extern void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn);
398extern void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp,
399 struct bgpevpn *vpn);
400extern void bgp_evpn_derive_auto_rt_import(struct bgp *bgp,
401 struct bgpevpn *vpn);
402extern void bgp_evpn_derive_auto_rt_export(struct bgp *bgp,
403 struct bgpevpn *vpn);
404extern void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn);
676f83b9 405extern void bgp_evpn_derive_auto_rd_for_vrf(struct bgp *bgp);
d62a17ae 406extern struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni);
407extern struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
29c53922
MK
408 struct in_addr originator_ip,
409 vrf_id_t tenant_vrf_id);
d62a17ae 410extern void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn);
a4168ebc 411#endif /* _BGP_EVPN_PRIVATE_H */