]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_evpn_private.h
bgpd: Fix peer determination from parent for imported routes
[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;
d7c0a89a 58 uint32_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*/
d7c0a89a 72 uint8_t advertise_gw_macip;
1a98c087 73
d7c0a89a
QY
74 /* Flag to indicate if we are
75 * advertising subnet for this VNI */
76 uint8_t advertise_subnet;
31310b25 77
d7c0a89a
QY
78 /* Id for deriving the RD
79 * automatically for this VNI */
80 uint16_t rd_id;
a4168ebc 81
d7c0a89a
QY
82 /* RD for this VNI. */
83 struct prefix_rd prd;
a4168ebc 84
d7c0a89a
QY
85 /* Route type 3 field */
86 struct in_addr originator_ip;
a4168ebc 87
d7c0a89a
QY
88 /* Import and Export RTs. */
89 struct list *import_rtl;
90 struct list *export_rtl;
a4168ebc 91
d7c0a89a
QY
92 /* Route table for EVPN routes for
93 * this VNI. */
94 struct bgp_table *route_table;
a4168ebc 95
d7c0a89a 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;
c48d9f5f 194
bc4606bd
MK
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
d7c0a89a 257static inline void encode_mac_mobility_extcomm(int static_mac, uint32_t seq,
d62a17ae 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
6fb219da
MK
288static 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.ip_prefix_length == 0) ? 1 : 0);
294}
295
d3135ba3 296static inline void ip_prefix_from_type2_prefix(struct prefix_evpn *evp,
297 struct prefix *ip)
298{
299 memset(ip, 0, sizeof(struct prefix));
300 if (IS_EVPN_PREFIX_IPADDR_V4(evp)) {
301 ip->family = AF_INET;
302 ip->prefixlen = IPV4_MAX_BITLEN;
996c9314 303 memcpy(&(ip->u.prefix4), &(evp->prefix.ip.ip),
d3135ba3 304 IPV4_MAX_BYTELEN);
305 } else if (IS_EVPN_PREFIX_IPADDR_V6(evp)) {
306 ip->family = AF_INET6;
307 ip->prefixlen = IPV6_MAX_BITLEN;
996c9314 308 memcpy(&(ip->u.prefix6), &(evp->prefix.ip.ip),
d3135ba3 309 IPV6_MAX_BYTELEN);
310 }
311}
312
d62a17ae 313static inline void build_evpn_type2_prefix(struct prefix_evpn *p,
314 struct ethaddr *mac,
315 struct ipaddr *ip)
128ea8ab 316{
d62a17ae 317 memset(p, 0, sizeof(struct prefix_evpn));
b03b8898 318 p->family = AF_EVPN;
d62a17ae 319 p->prefixlen = EVPN_TYPE_2_ROUTE_PREFIXLEN;
320 p->prefix.route_type = BGP_EVPN_MAC_IP_ROUTE;
28328ea9 321 memcpy(&p->prefix.mac.octet, mac->octet, ETH_ALEN);
d62a17ae 322 p->prefix.ip.ipa_type = IPADDR_NONE;
323 if (ip)
324 memcpy(&p->prefix.ip, ip, sizeof(*ip));
128ea8ab 325}
326
342dd0c6 327static inline void build_type5_prefix_from_ip_prefix(struct prefix_evpn *evp,
328 struct prefix *ip_prefix)
329{
330 struct ipaddr ip;
331
332 memset(&ip, 0, sizeof(struct ipaddr));
333 if (ip_prefix->family == AF_INET) {
334 ip.ipa_type = IPADDR_V4;
335 memcpy(&ip.ipaddr_v4, &ip_prefix->u.prefix4,
336 sizeof(struct in_addr));
337 } else {
338 ip.ipa_type = IPADDR_V6;
339 memcpy(&ip.ipaddr_v6, &ip_prefix->u.prefix6,
340 sizeof(struct in6_addr));
341 }
342
343 memset(evp, 0, sizeof(struct prefix_evpn));
344 evp->family = AF_EVPN;
345 evp->prefixlen = EVPN_TYPE_5_ROUTE_PREFIXLEN;
346 evp->prefix.ip_prefix_length = ip_prefix->prefixlen;
347 evp->prefix.route_type = BGP_EVPN_IP_PREFIX_ROUTE;
348 evp->prefix.ip.ipa_type = ip.ipa_type;
349 memcpy(&evp->prefix.ip, &ip, sizeof(struct ipaddr));
350}
351
d62a17ae 352static inline void build_evpn_type3_prefix(struct prefix_evpn *p,
353 struct in_addr originator_ip)
128ea8ab 354{
d62a17ae 355 memset(p, 0, sizeof(struct prefix_evpn));
b03b8898 356 p->family = AF_EVPN;
d62a17ae 357 p->prefixlen = EVPN_TYPE_3_ROUTE_PREFIXLEN;
358 p->prefix.route_type = BGP_EVPN_IMET_ROUTE;
359 p->prefix.ip.ipa_type = IPADDR_V4;
360 p->prefix.ip.ipaddr_v4 = originator_ip;
128ea8ab 361}
362
486456ca
MK
363static inline int evpn_default_originate_set(struct bgp *bgp, afi_t afi,
364 safi_t safi)
365{
366 if (afi == AFI_IP &&
367 CHECK_FLAG(bgp->af_flags[AFI_L2VPN][SAFI_EVPN],
368 BGP_L2VPN_EVPN_DEFAULT_ORIGINATE_IPV4))
369 return 1;
370 else if (afi == AFI_IP6 &&
371 CHECK_FLAG(bgp->af_flags[AFI_L2VPN][SAFI_EVPN],
372 BGP_L2VPN_EVPN_DEFAULT_ORIGINATE_IPV6))
373 return 1;
374 return 0;
375}
376
996c9314 377extern void evpn_rt_delete_auto(struct bgp *, vni_t, struct list *);
523cafc4 378extern void bgp_evpn_configure_export_rt_for_vrf(struct bgp *bgp_vrf,
379 struct ecommunity *ecomadd);
380extern void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp *bgp_vrf,
381 struct ecommunity *ecomdel);
382extern void bgp_evpn_configure_import_rt_for_vrf(struct bgp *bgp_vrf,
383 struct ecommunity *ecomadd);
384extern void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp *bgp_vrf,
385 struct ecommunity *ecomdel);
d62a17ae 386extern int bgp_evpn_handle_export_rt_change(struct bgp *bgp,
387 struct bgpevpn *vpn);
676f83b9 388extern void bgp_evpn_handle_vrf_rd_change(struct bgp *bgp_vrf, int withdraw);
d62a17ae 389extern void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
390 int withdraw);
391extern int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn);
392extern int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn);
523cafc4 393extern void bgp_evpn_map_vrf_to_its_rts(struct bgp *bgp_vrf);
394extern void bgp_evpn_unmap_vrf_from_its_rts(struct bgp *bgp_vrf);
d62a17ae 395extern void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn);
396extern void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp,
397 struct bgpevpn *vpn);
398extern void bgp_evpn_derive_auto_rt_import(struct bgp *bgp,
399 struct bgpevpn *vpn);
400extern void bgp_evpn_derive_auto_rt_export(struct bgp *bgp,
401 struct bgpevpn *vpn);
402extern void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn);
676f83b9 403extern void bgp_evpn_derive_auto_rd_for_vrf(struct bgp *bgp);
d62a17ae 404extern struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni);
405extern struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
29c53922
MK
406 struct in_addr originator_ip,
407 vrf_id_t tenant_vrf_id);
d62a17ae 408extern void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn);
a4168ebc 409#endif /* _BGP_EVPN_PRIVATE_H */