-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathvrf.c
More file actions
364 lines (293 loc) · 8.72 KB
/
Copy pathvrf.c
File metadata and controls
364 lines (293 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2024 Christophe Fontaine
#include <gr_errno.h>
#include <gr_iface.h>
#include <gr_infra.h>
#include <gr_log.h>
#include <gr_net_types.h>
#include <gr_netlink.h>
#include <gr_vrf.h>
#include <rte_ethdev.h>
#include <assert.h>
#include <linux/rtnetlink.h>
#include <net/if.h>
GR_LOG_TYPE("vrf");
static const struct vrf_fib_ops *fib_ops[256];
void vrf_fib_ops_register(addr_family_t af, const struct vrf_fib_ops *ops) {
if (!gr_af_valid(af))
ABORT("invalid af value %hhu", af);
if (ops == NULL || ops->init == NULL || ops->reconfig == NULL || ops->fini == NULL)
ABORT("invalid vrf fib ops");
if (fib_ops[af] != NULL)
ABORT("duplicate vrf fib ops %s", gr_af_name(af));
fib_ops[af] = ops;
}
static struct gr_iface_info_vrf_fib *vrf_fib_conf(struct iface_info_vrf *vrf, addr_family_t af) {
if (af == GR_AF_IP4)
return &vrf->ipv4;
if (af == GR_AF_IP6)
return &vrf->ipv6;
return NULL;
}
static const struct gr_iface_info_vrf_fib *
vrf_fib_api(const struct gr_iface_info_vrf *info, addr_family_t af) {
if (af == GR_AF_IP4)
return &info->ipv4;
if (af == GR_AF_IP6)
return &info->ipv6;
return NULL;
}
struct iface *get_vrf_iface(uint16_t vrf_id) {
struct iface *iface = iface_from_id(vrf_id);
if (iface == NULL || iface->type != GR_IFACE_TYPE_VRF)
return errno_set_null(ENONET);
return iface;
}
#define GR_VRF_IFALIAS "Grout control plane vrf"
static int netlink_create_vrf_and_enslave(
const char *vrf_name,
uint32_t vrf_table,
uint32_t loop_ifindex,
uint32_t *vrf_ifindex
) {
char ifalias[IFALIASZ];
char kind[IFNAMSIZ];
uint32_t ifindex;
int ret;
// Check for a stale VRF device left behind by a previous crash.
ifindex = if_nametoindex(vrf_name);
if (ifindex != 0) {
ret = netlink_link_get_kind(vrf_name, kind, sizeof(kind));
if (ret < 0 || strcmp(kind, "vrf") != 0)
return errno_set(EEXIST);
ret = netlink_get_ifalias(vrf_name, ifalias, sizeof(ifalias));
if (ret < 0 || strcmp(ifalias, GR_VRF_IFALIAS) != 0)
return errno_set(EEXIST);
LOG(WARNING, "deleting stale VRF device %s (ifindex %u)", vrf_name, ifindex);
ret = netlink_link_del_iface(ifindex);
if (ret < 0)
return ret;
}
ret = netlink_link_add_vrf(vrf_name, vrf_table);
if (ret < 0)
return ret;
*vrf_ifindex = ret;
ret = netlink_set_ifalias(*vrf_ifindex, GR_VRF_IFALIAS);
if (ret < 0)
return ret;
ret = netlink_link_set_master(loop_ifindex, *vrf_ifindex);
if (ret < 0)
return ret;
ret = netlink_link_set_admin_state(*vrf_ifindex, true, false);
if (ret < 0)
return ret;
ret = netlink_link_set_admin_state(loop_ifindex, true, false);
if (ret < 0)
return ret;
return 0;
}
static int netlink_delete_vrf_and_unslave(uint32_t vrf_ifindex, uint32_t loop_ifindex) {
int ret;
ret = netlink_link_set_master(loop_ifindex, 0);
if (ret < 0)
return ret;
ret = netlink_link_set_admin_state(loop_ifindex, false, false);
if (ret < 0)
return ret;
return netlink_link_del_iface(vrf_ifindex);
}
static uint32_t vrf_id_to_table_id(uint16_t vrf_id) {
if (vrf_id == GR_VRF_DEFAULT_ID)
return RT_TABLE_MAIN;
// Reserved values for table_id are 0, 252, 253, 254 and 255.
// Since we may have more than 252 VRFs, use an arbitrary value greater than 255.
return vrf_id + 1000;
}
static int netlink_vrf_add(const struct iface *iface) {
uint32_t table_id = vrf_id_to_table_id(iface->vrf_id);
struct iface_info_vrf *vrf = iface_info_vrf(iface);
int ret;
// Only create kernel VRF device for non-default VRFs
if (iface->id != GR_VRF_DEFAULT_ID) {
ret = netlink_create_vrf_and_enslave(
iface->name, table_id, iface->cp_id, &vrf->vrf_ifindex
);
if (ret < 0) {
LOG(WARNING,
"create vrf %s (id %u) failed: %s",
iface->name,
iface->vrf_id,
strerror(errno));
return ret;
}
}
ret = netlink_add_route(iface->cp_id, table_id);
if (ret < 0) {
LOG(WARNING, "add route on %s failed: %s", iface->name, strerror(errno));
if (iface->id != GR_VRF_DEFAULT_ID)
netlink_delete_vrf_and_unslave(vrf->vrf_ifindex, iface->cp_id);
return ret;
}
return 0;
}
static int netlink_vrf_del(const struct iface *iface) {
struct iface_info_vrf *vrf = iface_info_vrf(iface);
int ret;
if (iface->id != GR_VRF_DEFAULT_ID) {
ret = netlink_delete_vrf_and_unslave(vrf->vrf_ifindex, iface->cp_id);
if (ret < 0) {
LOG(WARNING,
"delete vrf %s (id %u) failed: %s",
iface->name,
iface->vrf_id,
strerror(errno));
return ret;
}
vrf->vrf_ifindex = 0;
} else {
uint32_t table_id = vrf_id_to_table_id(iface->vrf_id);
ret = netlink_del_route(iface->cp_id, table_id);
if (ret < 0) {
LOG(WARNING, "delete route on %s failed: %s", iface->name, strerror(errno));
return ret;
}
}
return 0;
}
bool vrf_has_interfaces(uint16_t vrf_id) {
struct iface *iface = get_vrf_iface(vrf_id);
if (iface == NULL)
return false;
return iface_info_vrf(iface)->ref_count > 0;
}
int vrf_incref(uint16_t vrf_id) {
struct iface *iface = get_vrf_iface(vrf_id);
if (iface == NULL)
return -errno;
iface_info_vrf(iface)->ref_count++;
return 0;
}
void vrf_decref(uint16_t vrf_id) {
struct iface *iface = get_vrf_iface(vrf_id);
if (iface == NULL)
return;
struct iface_info_vrf *vrf = iface_info_vrf(iface);
if (vrf->ref_count > 0)
vrf->ref_count--;
}
// VRF interface type //////////////////////////////////////////////////////////
static int iface_vrf_init(struct iface *iface, const void *api_info) {
struct iface_info_vrf *vrf = iface_info_vrf(iface);
const struct gr_iface_info_vrf *info = api_info;
unsigned af;
// VRF's vrf_id is its own iface_id (VRF identifier)
iface->vrf_id = iface->id;
vrf->ref_count = 0;
if (iface_loopback_create(iface) < 0)
return -errno;
if (netlink_vrf_add(iface) < 0)
goto netlink_fail;
for (af = 0; af < ARRAY_DIM(fib_ops); af++) {
if (fib_ops[af] == NULL)
continue;
// info is NULL when the default VRF is created internally.
const struct gr_iface_info_vrf_fib *api = info ? vrf_fib_api(info, af) : NULL;
if (api != NULL)
*vrf_fib_conf(vrf, af) = *api;
if (fib_ops[af]->init(iface) < 0)
goto fib_fail;
}
iface->flags = GR_IFACE_F_UP;
iface->state = GR_IFACE_S_RUNNING;
iface->speed = RTE_ETH_SPEED_NUM_10G;
return 0;
fib_fail:
// Only fini AFs that completed init; the failing one cleans up itself.
for (unsigned i = 0; i < af; i++) {
if (fib_ops[i] == NULL)
continue;
fib_ops[i]->fini(iface);
}
netlink_vrf_del(iface);
netlink_fail:
iface_loopback_destroy(iface);
return -errno;
}
static int iface_vrf_fini(struct iface *iface) {
for (unsigned af = 0; af < ARRAY_DIM(fib_ops); af++)
if (fib_ops[af] != NULL)
fib_ops[af]->fini(iface);
if (netlink_vrf_del(iface) < 0)
return -errno;
return iface_loopback_destroy(iface);
}
static int iface_vrf_reconfig(
struct iface *iface,
uint64_t set_attrs,
const struct gr_iface *conf,
const void *api_info
) {
struct iface_info_vrf *vrf = iface_info_vrf(iface);
const struct gr_iface_info_vrf *info = api_info;
if (set_attrs & ~(GR_IFACE_SET_NAME | GR_IFACE_SET_DESCR | GR_VRF_SET_FIB))
return errno_set(EOPNOTSUPP);
if (set_attrs & GR_IFACE_SET_NAME) {
uint32_t ifindex;
// Default VRF: TUN device uses the VRF name directly.
// Non-default VRFs: kernel VRF device uses the VRF name.
if (iface->id == GR_VRF_DEFAULT_ID)
ifindex = iface->cp_id;
else
ifindex = vrf->vrf_ifindex;
if (netlink_link_set_name(ifindex, conf->name) < 0)
return -errno;
}
if (set_attrs & GR_VRF_SET_FIB) {
for (unsigned af = 0; af < ARRAY_DIM(fib_ops); af++) {
const struct gr_iface_info_vrf_fib *api;
struct gr_iface_info_vrf_fib *fib_conf;
struct gr_iface_info_vrf_fib old;
api = vrf_fib_api(info, af);
if (api == NULL)
continue;
if (api->max_routes == 0)
continue;
assert(fib_ops[af] != NULL);
fib_conf = vrf_fib_conf(vrf, af);
old = *fib_conf;
fib_conf->max_routes = api->max_routes;
if (fib_conf->max_routes == old.max_routes)
continue;
if (fib_ops[af]->reconfig(iface) < 0) {
*fib_conf = old;
return -errno;
}
LOG(INFO,
"resized %s FIB VRF %s(%u) max_routes %u -> %u",
gr_af_name(af),
iface->name,
iface->id,
old.max_routes,
fib_conf->max_routes);
}
}
return 0;
}
static void iface_vrf_to_api(void *info, const struct iface *iface) {
const struct iface_info_vrf *vrf = iface_info_vrf(iface);
struct gr_iface_info_vrf *api = info;
*api = vrf->base;
}
static struct iface_type iface_type_vrf = {
.id = GR_IFACE_TYPE_VRF,
.pub_size = sizeof(struct gr_iface_info_vrf),
.priv_size = sizeof(struct iface_info_vrf),
.init = iface_vrf_init,
.reconfig = iface_vrf_reconfig,
.fini = iface_vrf_fini,
.to_api = iface_vrf_to_api,
};
RTE_INIT(vrf_type_constructor) {
iface_type_register(&iface_type_vrf);
iface_name_reserve(GR_DEFAULT_VRF_NAME, false);
}