forked from ilejn/xdpbridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdpbridge_main.c
413 lines (337 loc) · 10.7 KB
/
xdpbridge_main.c
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include "xdpbridge_user.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <locale.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <libgen.h>
#include <signal.h>
#include <sys/resource.h>
#include "bpf_load.h"
#include <bpf/libbpf.h>
#include <bpf/bpf.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include <getopt.h>
#include <pthread.h>
#include "xdpbridge.h"
#ifndef XDP_ZEROCOPY
#define XDP_ZEROCOPY (1 << 2) /* Force zero-copy mode */
#endif
static int opt_iifindex;
static int opt_oifindex;
static int opt_client_queues = 1;
static int opt_bidirectional;
static int opt_userspace;
#define MAX_TRANSMIT_QUEUES 128
static int opt_transmit_queues[MAX_TRANSMIT_QUEUES];
static int opt_transmit_queue_num = 0;
static const char *opt_iif = "";
static const char *opt_oif = "";
static u32 opt_xdp_i_bind_flags;
static u32 opt_xdp_i_flags;
static u32 opt_xdp_o_bind_flags;
static u32 opt_xdp_o_flags;
static struct option long_options[] = {
{"client-interface", required_argument, 0, 'i'},
{"world-interface", required_argument, 0, 'o'},
{"client-queues", required_argument, 0, 'q'},
{"bidirectional", no_argument, 0, 'b'},
{"userspace", no_argument, 0, 'u'},
{"transmit-queue", required_argument, 0, 't'},
{"xdp-skb-client", no_argument, 0, 'S'},
{"xdp-native-client", no_argument, 0, 'N'},
{"xdp-zerocopy-client", no_argument, 0, 'Z'},
{"xdp-skb-world", no_argument, 0, 's'},
{"xdp-native-world", no_argument, 0, 'n'},
{"xdp-zerocopy-world", no_argument, 0, 'z'},
{0, 0, 0, 0}
};
static void usage(const char *prog)
{
const char *str =
" Usage: %s [OPTIONS]\n"
" Options :\n"
" -i, --client-interface=input_iface Client interface\n"
" -o, --world-interface=output_iface World interface\n"
" -q, --client-queues=n Number of client-side incoming queues and threads (default 1)\n"
" -t, --transmit-queue=n Transmit queue (can be set multiple times)\n"
" -b, --bidirectional Transfer packets from world to client as well\n"
" -u, --userspace Transfer packets from world to client via AF_XDP\n"
" -S, --xdp-skb-client Enforce XDP skb-mod for client interface\n"
" -N, --xdp-native-client Enforce XDP native mode for client interface\n"
" -Z, --xdp-zerocopy-client Enforce XDP zerocopy for client interface\n"
" -C, --xdp-copy-client Enforce XDP copy for client interface\n"
" -s, --xdp-skb-world Enforce XDP skb-mod for world interface\n"
" -n, --xdp-native-world Enforce XDP native mode for world interface\n"
" -z, --xdp-zerocopy-world Enforce XDP zerocopy for world interface\n"
" -c, --xdp-copy-world Enforce XDP copy for world interface\n"
"\n";
fprintf(stderr, str, prog);
exit(EXIT_FAILURE);
}
static void parse_command_line(int argc, char **argv)
{
int option_index, c;
opterr = 0;
for (;;) {
c = getopt_long(argc, argv, "i:o:q:t:buSNZCsnzc", long_options,
&option_index);
if (c == -1)
break;
switch (c) {
case 'i':
opt_iif = optarg;
break;
case 'o':
opt_oif = optarg;
break;
case 'q':
opt_client_queues = atoi(optarg);
break;
case 't':
opt_transmit_queues[opt_transmit_queue_num++] = atoi(optarg);
break;
case 'b':
opt_bidirectional = 1;
break;
case 'u':
opt_userspace = 1;
break;
case 'S':
opt_xdp_i_flags |= XDP_FLAGS_SKB_MODE;
break;
case 'N':
opt_xdp_i_flags |= XDP_FLAGS_DRV_MODE;
break;
case 'Z':
opt_xdp_i_bind_flags |= XDP_ZEROCOPY;
break;
case 'C':
opt_xdp_i_bind_flags |= XDP_COPY;
break;
case 's':
opt_xdp_o_flags |= XDP_FLAGS_SKB_MODE;
break;
case 'n':
opt_xdp_o_flags |= XDP_FLAGS_DRV_MODE;
break;
case 'z':
opt_xdp_o_bind_flags |= XDP_ZEROCOPY;
break;
case 'c':
opt_xdp_o_bind_flags |= XDP_COPY;
break;
default:
usage(basename(argv[0]));
}
}
opt_iifindex = if_nametoindex(opt_iif);
if (!opt_iifindex) {
fprintf(stderr, "ERROR: interface \"%s\" does not exist\n",
opt_iif);
usage(basename(argv[0]));
}
opt_oifindex = if_nametoindex(opt_oif);
if (!opt_oifindex) {
fprintf(stderr, "ERROR: interface \"%s\" does not exist\n",
opt_oif);
usage(basename(argv[0]));
}
if (opt_userspace && !opt_bidirectional ||
opt_userspace && opt_transmit_queue_num) {
fprintf(stderr, "ERROR: inconsistent parameters\n");
usage(basename(argv[0]));
}
}
// pass all packets
// app logic stub
static void validate_packets(struct xdp_desc* desc, int *pass_flags, unsigned int rcvd)
{
unsigned int i = 0;
for (; i < rcvd; ++i) {
pass_flags[i] = true;
}
}
static void * XDPRequestHandler(void *arg)
{
struct sock_port *sp = (struct sock_port*) arg;
unsigned int idx = 0;
struct xdp_desc descs[BATCH_SIZE];
int pass_flags[BATCH_SIZE];
for (;;) {
unsigned int rcvd = XDPGet(sp, descs);
validate_packets(descs, pass_flags, rcvd);
if (rcvd) {
XDPPut(sp, descs, pass_flags, rcvd, &idx);
}
}
free(sp);
}
static void * XDPRequestHandler_bidirect(void *arg)
{
struct sock_port *sp = (struct sock_port*) arg;
unsigned int idx = 0;
unsigned int idx_back = 0;
struct xdp_desc descs[BATCH_SIZE];
int pass_flags[BATCH_SIZE];
struct sock_port sp_back;
sp_back.xdps_in = sp->xdps_out;
sp_back.xdps_out = sp->xdps_in;
for (;;) {
unsigned int rcvd;
rcvd = XDPGet(&sp_back, descs);
validate_packets(descs, pass_flags, rcvd);
if (rcvd) {
XDPPut(&sp_back, descs, NULL, rcvd, &idx_back);
}
rcvd = XDPGet(sp, descs);
validate_packets(descs, pass_flags, rcvd);
if (rcvd) {
XDPPut(sp, descs, pass_flags, rcvd, &idx);
}
}
free(sp);
}
static void clean_exit(int exit_code)
{
bpf_set_link_xdp_fd(opt_iifindex, -1, opt_xdp_i_flags);
if (opt_bidirectional) {
bpf_set_link_xdp_fd(opt_oifindex, -1, opt_xdp_o_flags);
}
exit(exit_code);
}
int main(int argc, char **argv)
{
struct xdpsock *xsks[MAX_QUEUES * 2];
sigset_t ss;
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
// struct bpf_prog_load_attr prog_load_attr = {
// .prog_type = BPF_PROG_TYPE_XDP,
// };
// int prog_fd, xdp_bridge_prog_fd, xdp_redirect_map_prog_fd;
// int xsks_map, num_queues_map;
char xdp_filename[256];
// struct bpf_map *map;
int q, key = 0, ret;
struct xdp_umem* umem_arr[MAX_QUEUES];
pthread_t pt[MAX_QUEUES * 2];
parse_command_line(argc, argv);
if (setrlimit(RLIMIT_MEMLOCK, &r)) {
fprintf(stderr, "ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n",
strerror(errno));
clean_exit(EXIT_FAILURE);
}
snprintf(xdp_filename, sizeof(xdp_filename), "%s_kern.o", argv[0]);
if (load_bpf_file(xdp_filename)) {
fprintf(stderr, "ERROR: no program found: %s\n",
xdp_filename);
clean_exit(EXIT_FAILURE);
}
if (!prog_fd[0]) {
fprintf(stderr, "load_bpf_file: %s\n", strerror(errno));
clean_exit(EXIT_FAILURE);
}
if (bpf_set_link_xdp_fd(opt_iifindex, prog_fd[XDP_SOCK_CLIENT_PROG], opt_xdp_i_flags) < 0) {
fprintf(stderr, "ERROR: input link set xdp fd failed\n");
clean_exit(EXIT_FAILURE);
}
if (opt_bidirectional) {
if (opt_userspace) {
if (bpf_set_link_xdp_fd(opt_oifindex, prog_fd[XDP_SOCK_WORLD_PROG], opt_xdp_o_flags) < 0) {
fprintf(stderr, "ERROR: output link set xdp fd failed\n");
clean_exit(EXIT_FAILURE);
}
}
else {
if (bpf_set_link_xdp_fd(opt_oifindex, prog_fd[XDP_REDIRECT_MAP_PROG], opt_xdp_o_flags) < 0) {
fprintf(stderr, "ERROR: output link set xdp fd failed\n");
clean_exit(EXIT_FAILURE);
}
}
}
// Set the number of queues
key = 0;
ret = bpf_map_update_elem(map_fd[NUM_QUEUES_CLIENT_MAP], &key, &opt_client_queues, 0);
if (ret) {
fprintf(stderr, "Error: bpf_map_update_elem NUM_QUEUES_CLIENT_MAP\n");
fprintf(stderr, "ERRNO: %d\n", errno);
fprintf(stderr, "%s", strerror(errno));
clean_exit(EXIT_FAILURE);
}
if (opt_bidirectional ) {
key = 0;
if (!opt_userspace) {
// Set outgoing interface for w=>c kernelspace path
key = 0;
ret = bpf_map_update_elem(map_fd[TX_PORT_MAP], &key, &opt_iifindex, 0);
if (ret) {
fprintf(stderr, "Error: bpf_map_update_elem\n");
fprintf(stderr, "ERRNO: %d\n", errno);
fprintf(stderr, "%s", strerror(errno));
clean_exit(EXIT_FAILURE);
}
} else {
key = 0;
ret = bpf_map_update_elem(map_fd[NUM_QUEUES_WORLD_MAP], &key, &opt_client_queues, 0);
if (ret) {
fprintf(stderr, "Error: bpf_map_update_elem NUM_QUEUES_CLIENT_MAP\n");
fprintf(stderr, "ERRNO: %d\n", errno);
fprintf(stderr, "%s", strerror(errno));
clean_exit(EXIT_FAILURE);
}
}
}
memset(umem_arr, 0, sizeof(umem_arr));
fprintf(stderr, "Let's create Sockets!\n");
/* Create c=>w sockets... */
for (q = 0; q < opt_client_queues; q++) { // q -> queue
int outgoing_q, outgoing_q_ind = 0;
struct sock_port *sp = malloc(sizeof(struct sock_port));
sp->xdps_in = xsks[q] = xsk_configure(NULL, q, opt_iifindex, opt_xdp_i_bind_flags);
if (!xsks[q]) {
fprintf(stderr, "Error: xsk_configure\n");
clean_exit(EXIT_FAILURE);
}
ret = bpf_map_update_elem(map_fd[XSKS_CLIENT_MAP], &q, &xsks[q]->sfd, 0);
if (ret) {
fprintf(stderr, "Error: bpf_map_update_elem %d\n", q);
fprintf(stderr, "ERRNO: %d\n", errno);
fprintf(stderr, "%s", strerror(errno));
clean_exit(EXIT_FAILURE);
}
if (opt_transmit_queue_num) {
outgoing_q_ind = q % opt_transmit_queue_num;
outgoing_q = opt_transmit_queues[outgoing_q_ind];
} else {
// outgoing queue has same num as incoming
outgoing_q = outgoing_q_ind = q;
}
sp->xdps_out = xsks[q + opt_client_queues] = xsk_configure(umem_arr[outgoing_q_ind], outgoing_q, opt_oifindex, opt_xdp_o_bind_flags);
if (!xsks[q + opt_client_queues]) {
fprintf(stderr, "Error: xsk_configure\n");
clean_exit(EXIT_FAILURE);
}
umem_arr[outgoing_q_ind] = sp->xdps_out->umem;
if (opt_userspace) {
ret = bpf_map_update_elem(map_fd[XSKS_WORLD_MAP], &q, &xsks[q + opt_client_queues]->sfd, 0);
if (ret) {
fprintf(stderr, "Error: bpf_map_update_elem %d\n", q);
fprintf(stderr, "ERRNO: %d\n", errno);
fprintf(stderr, "%s", strerror(errno));
clean_exit(EXIT_FAILURE);
}
}
fprintf(stderr, "Socket %d created\n", q);
pthread_create(&pt[q], NULL, opt_userspace ? XDPRequestHandler_bidirect : XDPRequestHandler, sp);
}
fprintf(stderr, "Started %d c=>w threads\n", opt_client_queues);
sigfillset(&ss);
sigprocmask(SIG_BLOCK, &ss, 0);
sigwaitinfo(&ss, NULL);
fprintf(stderr, "Exiting\n");
clean_exit(EXIT_SUCCESS);
return 0;
}