]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_evpn_private.h
bgpd: set vrf originator ip to kernels local-ip
[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
a4168ebc 33/* EVPN prefix lengths. */
34#define EVPN_TYPE_2_ROUTE_PREFIXLEN 224
35#define EVPN_TYPE_3_ROUTE_PREFIXLEN 224
36
37/* EVPN route types. */
d62a17ae 38typedef enum {
39 BGP_EVPN_AD_ROUTE = 1, /* Ethernet Auto-Discovery (A-D) route */
40 BGP_EVPN_MAC_IP_ROUTE, /* MAC/IP Advertisement route */
41 BGP_EVPN_IMET_ROUTE, /* Inclusive Multicast Ethernet Tag route */
42 BGP_EVPN_ES_ROUTE, /* Ethernet Segment route */
43 BGP_EVPN_IP_PREFIX_ROUTE, /* IP Prefix route */
a4168ebc 44} bgp_evpn_route_type;
45
46/*
47 * Hash table of EVIs. Right now, the only type of EVI supported is with
48 * VxLAN encapsulation, hence each EVI corresponds to a L2 VNI.
49 * The VNIs are not "created" through BGP but through some other interface
50 * on the system. This table stores VNIs that BGP comes to know as present
51 * on the system (through interaction with zebra) as well as pre-configured
52 * VNIs (which need to be defined in the system to become "live").
53 */
d62a17ae 54struct bgpevpn {
55 vni_t vni;
29c53922 56 vrf_id_t tenant_vrf_id;
d62a17ae 57 u_int32_t flags;
a4168ebc 58#define VNI_FLAG_CFGD 0x1 /* VNI is user configured */
59#define VNI_FLAG_LIVE 0x2 /* VNI is "live" */
60#define VNI_FLAG_RD_CFGD 0x4 /* RD is user configured. */
61#define VNI_FLAG_IMPRT_CFGD 0x8 /* Import RT is user configured */
62#define VNI_FLAG_EXPRT_CFGD 0x10 /* Export RT is user configured */
63
1a98c087
MK
64 /* Flag to indicate if we are advertising the g/w mac ip for this VNI*/
65 u_int8_t advertise_gw_macip;
66
d62a17ae 67 /* Id for deriving the RD automatically for this VNI */
68 u_int16_t rd_id;
a4168ebc 69
d62a17ae 70 /* RD for this VNI. */
71 struct prefix_rd prd;
a4168ebc 72
d62a17ae 73 /* Route type 3 field */
74 struct in_addr originator_ip;
a4168ebc 75
d62a17ae 76 /* Import and Export RTs. */
77 struct list *import_rtl;
78 struct list *export_rtl;
a4168ebc 79
d62a17ae 80 /* Route table for EVPN routes for this VNI. */
81 struct bgp_table *route_table;
a4168ebc 82
d62a17ae 83 QOBJ_FIELDS
a4168ebc 84};
85
86DECLARE_QOBJ_TYPE(bgpevpn)
87
88/* Mapping of Import RT to VNIs.
89 * The Import RTs of all VNIs are maintained in a hash table with each
90 * RT linking to all VNIs that will import routes matching this RT.
91 */
d62a17ae 92struct irt_node {
93 /* RT */
94 struct ecommunity_val rt;
a4168ebc 95
d62a17ae 96 /* List of VNIs importing routes matching this RT. */
97 struct list *vnis;
a4168ebc 98};
99
10ebe1ab
MK
100/* Mapping of Import RT to VRFs.
101 * The Import RTs of all VRFss are maintained in a hash table with each
102 * RT linking to all VRFs that will import routes matching this RT.
103 */
104struct vrf_irt_node {
105 /* RT */
106 struct ecommunity_val rt;
107
108 /* List of VNIs importing routes matching this RT. */
109 struct list *vrfs;
110};
111
112
128ea8ab 113#define RT_TYPE_IMPORT 1
114#define RT_TYPE_EXPORT 2
115#define RT_TYPE_BOTH 3
116
676f83b9 117static inline int is_vrf_rd_configured(struct bgp *bgp_vrf)
118{
119 return (CHECK_FLAG(bgp_vrf->vrf_flags,
120 BGP_VRF_RD_CFGD));
121}
122
123static inline int bgp_evpn_vrf_rd_matches_existing(struct bgp *bgp_vrf,
124 struct prefix_rd *prd)
125{
126 return (memcmp(&bgp_vrf->vrf_prd.val, prd->val, ECOMMUNITY_SIZE) == 0);
127}
128
129static inline int is_evpn_prefix_routes_adv_enabled(struct bgp *bgp_vrf)
130{
131 if (!bgp_vrf->l3vni ||
132 !CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_ADVERTISE_EVPN_PREFIX_ROUTE))
133 return 0;
134
135 return 1;
136}
137
6d8c58b7
MK
138static inline vni_t bgpevpn_get_l3vni(struct bgpevpn *vpn)
139{
140 struct bgp *bgp_vrf = NULL;
141
142 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
143 if (!bgp_vrf)
144 return 0;
145
146 return bgp_vrf->l3vni;
147}
148
149static inline void bgpevpn_get_rmac(struct bgpevpn *vpn, struct ethaddr *rmac)
150{
151 struct bgp *bgp_vrf = NULL;
152
153 memset(rmac, 0, sizeof(struct ethaddr));
154 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
155 if (!bgp_vrf)
156 return;
157 memcpy(rmac, &bgp_vrf->rmac, sizeof(struct ethaddr));
158}
159
160static inline struct list *bgpevpn_get_vrf_export_rtl(struct bgpevpn *vpn)
161{
162 struct bgp *bgp_vrf = NULL;
163
164 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
165 if (!bgp_vrf)
166 return NULL;
167
168 return bgp_vrf->vrf_export_rtl;
169}
170
171static inline struct list *bgpevpn_get_vrf_import_rtl(struct bgpevpn *vpn)
172{
173 struct bgp *bgp_vrf = NULL;
174
175 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
176 if (!bgp_vrf)
177 return NULL;
178
179 return bgp_vrf->vrf_import_rtl;
180}
181
6a8657d0
MK
182static inline void bgpevpn_unlink_from_l3vni(struct bgpevpn *vpn)
183{
184 struct bgp *bgp_vrf = NULL;
185
186 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
187 if (!bgp_vrf || !bgp_vrf->l2vnis)
188 return;
189 listnode_delete(bgp_vrf->l2vnis, vpn);
190}
191
192static inline void bgpevpn_link_to_l3vni(struct bgpevpn *vpn)
193{
194 struct bgp *bgp_vrf = NULL;
195
196 bgp_vrf = bgp_lookup_by_vrf_id(vpn->tenant_vrf_id);
197 if (!bgp_vrf || !bgp_vrf->l2vnis)
198 return;
199 listnode_add_sort(bgp_vrf->l2vnis, vpn);
200}
201
d62a17ae 202static inline int is_vni_configured(struct bgpevpn *vpn)
128ea8ab 203{
d62a17ae 204 return (CHECK_FLAG(vpn->flags, VNI_FLAG_CFGD));
128ea8ab 205}
206
d62a17ae 207static inline int is_vni_live(struct bgpevpn *vpn)
128ea8ab 208{
d62a17ae 209 return (CHECK_FLAG(vpn->flags, VNI_FLAG_LIVE));
128ea8ab 210}
211
d62a17ae 212static inline int is_rd_configured(struct bgpevpn *vpn)
128ea8ab 213{
d62a17ae 214 return (CHECK_FLAG(vpn->flags, VNI_FLAG_RD_CFGD));
128ea8ab 215}
216
d62a17ae 217static inline int bgp_evpn_rd_matches_existing(struct bgpevpn *vpn,
218 struct prefix_rd *prd)
128ea8ab 219{
d62a17ae 220 return (memcmp(&vpn->prd.val, prd->val, ECOMMUNITY_SIZE) == 0);
128ea8ab 221}
222
d62a17ae 223static inline int is_import_rt_configured(struct bgpevpn *vpn)
128ea8ab 224{
d62a17ae 225 return (CHECK_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD));
128ea8ab 226}
227
d62a17ae 228static inline int is_export_rt_configured(struct bgpevpn *vpn)
128ea8ab 229{
d62a17ae 230 return (CHECK_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD));
128ea8ab 231}
232
d62a17ae 233static inline int is_vni_param_configured(struct bgpevpn *vpn)
128ea8ab 234{
d62a17ae 235 return (is_rd_configured(vpn) || is_import_rt_configured(vpn)
236 || is_export_rt_configured(vpn));
128ea8ab 237}
238
d62a17ae 239static inline void vni2label(vni_t vni, mpls_label_t *label)
128ea8ab 240{
d62a17ae 241 u_char *tag = (u_char *)label;
242 tag[0] = (vni >> 16) & 0xFF;
243 tag[1] = (vni >> 8) & 0xFF;
244 tag[2] = vni & 0xFF;
128ea8ab 245}
246
d62a17ae 247static inline vni_t label2vni(mpls_label_t *label)
128ea8ab 248{
d62a17ae 249 u_char *tag = (u_char *)label;
250 vni_t vni;
128ea8ab 251
d62a17ae 252 vni = ((u_int32_t)*tag++ << 16);
253 vni |= (u_int32_t)*tag++ << 8;
254 vni |= (u_int32_t)(*tag & 0xFF);
128ea8ab 255
d62a17ae 256 return vni;
128ea8ab 257}
258
bc59a672
MK
259static inline void encode_rmac_extcomm(struct ecommunity_val *eval,
260 struct ethaddr *rmac)
261{
262 memset(eval, 0, sizeof(*eval));
263 eval->val[0] = ECOMMUNITY_ENCODE_EVPN;
264 eval->val[1] = ECOMMUNITY_EVPN_SUBTYPE_ROUTERMAC;
265 memcpy(&eval->val[2], rmac, ETH_ALEN);
266}
267
d62a17ae 268static inline void encode_mac_mobility_extcomm(int static_mac, u_int32_t seq,
269 struct ecommunity_val *eval)
128ea8ab 270{
d62a17ae 271 memset(eval, 0, sizeof(*eval));
272 eval->val[0] = ECOMMUNITY_ENCODE_EVPN;
273 eval->val[1] = ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY;
274 if (static_mac)
275 eval->val[2] = ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY_FLAG_STICKY;
276 eval->val[4] = (seq >> 24) & 0xff;
277 eval->val[5] = (seq >> 16) & 0xff;
278 eval->val[6] = (seq >> 8) & 0xff;
279 eval->val[7] = seq & 0xff;
128ea8ab 280}
281
d3135ba3 282static inline void ip_prefix_from_type2_prefix(struct prefix_evpn *evp,
283 struct prefix *ip)
284{
285 memset(ip, 0, sizeof(struct prefix));
286 if (IS_EVPN_PREFIX_IPADDR_V4(evp)) {
287 ip->family = AF_INET;
288 ip->prefixlen = IPV4_MAX_BITLEN;
289 memcpy(&(ip->u.prefix4),
290 &(evp->prefix.ip.ip),
291 IPV4_MAX_BYTELEN);
292 } else if (IS_EVPN_PREFIX_IPADDR_V6(evp)) {
293 ip->family = AF_INET6;
294 ip->prefixlen = IPV6_MAX_BITLEN;
295 memcpy(&(ip->u.prefix6),
296 &(evp->prefix.ip.ip),
297 IPV6_MAX_BYTELEN);
298 }
299}
300
d62a17ae 301static inline void build_evpn_type2_prefix(struct prefix_evpn *p,
302 struct ethaddr *mac,
303 struct ipaddr *ip)
128ea8ab 304{
d62a17ae 305 memset(p, 0, sizeof(struct prefix_evpn));
b03b8898 306 p->family = AF_EVPN;
d62a17ae 307 p->prefixlen = EVPN_TYPE_2_ROUTE_PREFIXLEN;
308 p->prefix.route_type = BGP_EVPN_MAC_IP_ROUTE;
28328ea9 309 memcpy(&p->prefix.mac.octet, mac->octet, ETH_ALEN);
d62a17ae 310 p->prefix.ip.ipa_type = IPADDR_NONE;
311 if (ip)
312 memcpy(&p->prefix.ip, ip, sizeof(*ip));
128ea8ab 313}
314
d62a17ae 315static inline void build_evpn_type3_prefix(struct prefix_evpn *p,
316 struct in_addr originator_ip)
128ea8ab 317{
d62a17ae 318 memset(p, 0, sizeof(struct prefix_evpn));
b03b8898 319 p->family = AF_EVPN;
d62a17ae 320 p->prefixlen = EVPN_TYPE_3_ROUTE_PREFIXLEN;
321 p->prefix.route_type = BGP_EVPN_IMET_ROUTE;
322 p->prefix.ip.ipa_type = IPADDR_V4;
323 p->prefix.ip.ipaddr_v4 = originator_ip;
128ea8ab 324}
325
c581d8b0
MK
326extern void evpn_rt_delete_auto(struct bgp*, vni_t, struct list*);
327extern void bgp_evpn_configure_export_rt_for_vrf(struct bgp*,
328 struct ecommunity*);
329extern void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp*,
330 struct ecommunity*);
331extern void bgp_evpn_configure_import_rt_for_vrf(struct bgp*,
332 struct ecommunity*);
333extern void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp*,
334 struct ecommunity*);
d62a17ae 335extern int bgp_evpn_handle_export_rt_change(struct bgp *bgp,
336 struct bgpevpn *vpn);
676f83b9 337extern void bgp_evpn_handle_vrf_rd_change(struct bgp *bgp_vrf, int withdraw);
d62a17ae 338extern void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
339 int withdraw);
340extern int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn);
341extern int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn);
10ebe1ab
MK
342extern void bgp_evpn_map_vrf_to_its_rts(struct bgp *);
343extern void bgp_evpn_unmap_vrf_from_its_rts(struct bgp *);
d62a17ae 344extern void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn);
345extern void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp,
346 struct bgpevpn *vpn);
347extern void bgp_evpn_derive_auto_rt_import(struct bgp *bgp,
348 struct bgpevpn *vpn);
349extern void bgp_evpn_derive_auto_rt_export(struct bgp *bgp,
350 struct bgpevpn *vpn);
351extern void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn);
676f83b9 352extern void bgp_evpn_derive_auto_rd_for_vrf(struct bgp *bgp);
d62a17ae 353extern struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni);
354extern struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
29c53922
MK
355 struct in_addr originator_ip,
356 vrf_id_t tenant_vrf_id);
d62a17ae 357extern void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn);
a4168ebc 358#endif /* _BGP_EVPN_PRIVATE_H */