-
Notifications
You must be signed in to change notification settings - Fork 0
/
reuseport_cpu.c
369 lines (284 loc) · 6.76 KB
/
reuseport_cpu.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
// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <netinet/in.h>
#include <sched.h>
#include <stdio.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include "reuseport_cpu.skel.h"
#define PORT_START 10000
#define PORT_RANGE 32
#define PATH_LEN 128
#define PATH_MAP "/sys/fs/bpf/reuseport_map_%05d"
#define PATH_PROG "/sys/fs/bpf/reuseport_prog_%05d"
struct worker {
struct reuseport_cpu_bpf *skel;
int cpu;
int fds[PORT_RANGE];
int epoll_fd;
};
static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
{
return vfprintf(stderr, format, args);
}
static int pin_bpf_obj(int port)
{
struct reuseport_cpu_bpf *skel;
char path[PATH_LEN];
int err;
/* Open BPF skeleton */
skel = reuseport_cpu_bpf__open();
if (!skel) {
fprintf(stderr, "Failed to open BPF skeleton\n");
return -1;
}
/* Load & verify BPF programs */
err = reuseport_cpu_bpf__load(skel);
if (err) {
fprintf(stderr, "Failed to load and verify BPF skeleton\n");
goto cleanup;
}
snprintf(path, PATH_LEN, PATH_MAP, port);
/* Unpin already pinned BPF map */
unlink(path);
/* Pin BPF map */
err = bpf_map__pin(skel->maps.reuseport_map, path);
if (err) {
fprintf(stderr, "Failed to pin BPF map at %s\n", path);
goto cleanup;
}
snprintf(path, PATH_LEN, PATH_PROG, port);
/* Unpin already pinned BPF prog */
unlink(path);
/* Pin BPF prog */
err = bpf_program__pin(skel->progs.migrate_reuseport, path);
if (err)
fprintf(stderr, "Failed to pin BPF prog at %s\n", path);
cleanup:
reuseport_cpu_bpf__destroy(skel);
return err;
}
static int setup_parent(void)
{
int port, err;
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
libbpf_set_print(libbpf_print_fn);
for (port = PORT_START; port < PORT_START + PORT_RANGE; port++) {
err = pin_bpf_obj(port);
if (err)
break;
}
return err;
}
static int set_affinity(int cpu)
{
cpu_set_t cpu_set;
int err;
CPU_ZERO(&cpu_set);
CPU_SET(cpu, &cpu_set);
err = sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set);
if (err)
fprintf(stderr, "CPU[%02d]: Failed to set CPU affinity\n", cpu);
return err;
}
static int create_socket(int cpu, int port)
{
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_addr.s_addr = htonl(INADDR_ANY),
};
int fd, err;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd == -1) {
fprintf(stderr, "CPU[%02d]: Failed to create a socket\n", cpu);
return -1;
}
err = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &(int){1}, sizeof(int));
if (err) {
fprintf(stderr, "CPU[%02d]: Failed to set SO_REUSEPORT\n", cpu);
goto close;
}
addr.sin_port = htons(port);
err = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
if (err) {
fprintf(stderr, "CPU[%02d]: Failed to bind a socket\n", cpu);
goto close;
}
return fd;
close:
close(fd);
return err;
}
static int update_reuseport_map(struct worker *worker, int i)
{
char path[PATH_LEN];
int map_fd, err;
snprintf(path, PATH_LEN, PATH_MAP, PORT_START + i);
/* Load pinned BPF map */
map_fd = bpf_obj_get(path);
if (map_fd < 0)
return map_fd;
err = bpf_map_update_elem(map_fd, &worker->cpu, &worker->fds[i], BPF_NOEXIST);
if (err)
fprintf(stderr, "CPU[%02d]: Failed to update BPF map\n", worker->cpu);
close(map_fd);
return err;
}
static int attach_reuseport_prog(struct worker *worker, int i)
{
char path[PATH_LEN];
int prog_fd, err;
snprintf(path, PATH_LEN, PATH_PROG, PORT_START + i);
prog_fd = bpf_obj_get(path);
if (prog_fd < 0)
return prog_fd;
err = setsockopt(worker->fds[i], SOL_SOCKET, SO_ATTACH_REUSEPORT_EBPF,
&prog_fd, sizeof(prog_fd));
if (err)
fprintf(stderr, "CPU[%02d]: Failed to attach BPF prog\n", worker->cpu);
close(prog_fd);
return err;
}
static int setup_port(struct worker *worker, int i)
{
struct epoll_event event = {
.events = EPOLLIN,
.data.u32 = i,
};
int err;
/* Listen on port PORT_START + i */
worker->fds[i] = create_socket(worker->cpu, PORT_START + i);
if (worker->fds[i] < 0)
return worker->fds[i];
/* Update BPF map like: map[cpu_id] = socket_fd */
err = update_reuseport_map(worker, i);
if (err)
goto close;
/* Attach BPF program to reuseport group */
err = attach_reuseport_prog(worker, i);
if (err < 0)
goto close;
err = epoll_ctl(worker->epoll_fd, EPOLL_CTL_ADD, worker->fds[i], &event);
if (err) {
fprintf(stderr, "CPU[%02d]: Failed to register socket on %d for epoll\n",
worker->cpu, PORT_START + i);
goto close;
}
return 0;
close:
close(worker->fds[i]);
return err;
}
static void close_sockets(struct worker *worker)
{
int i;
for (i = 0; i < PORT_RANGE; i++)
close(worker->fds[i]);
}
struct worker *setup_worker(int cpu)
{
struct worker *worker;
int err, i;
/* Bind process on a single CPU */
err = set_affinity(cpu);
if (err)
goto err;
worker = malloc(sizeof(struct worker));
if (!worker) {
fprintf(stderr, "CPU[%02d]: Failed to allocate memory\n", cpu);
goto err;
}
worker->cpu = cpu;
worker->epoll_fd = epoll_create(PORT_RANGE);
if (worker->epoll_fd < 0) {
fprintf(stderr, "CPU[%02d]: Failed to create epoll instance\n", cpu);
goto free;
}
for (i = 0; i < PORT_RANGE; i++) {
err = setup_port(worker, i);
if (err)
goto close;
}
return worker;
close:
close_sockets(worker);
free:
free(worker);
err:
return NULL;
}
static void teardown_worker(struct worker *worker)
{
close_sockets(worker);
free(worker);
}
static int run_worker(int cpu)
{
struct sockaddr_in addr;
struct worker *worker;
socklen_t addrlen;
char buf[1024];
int err;
worker = setup_worker(cpu);
if (!worker)
return -1;
while (1) {
struct epoll_event events[PORT_RANGE];
int total, i;
total = epoll_wait(worker->epoll_fd, events, PORT_RANGE, -1);
if (total < 0) {
fprintf(stderr, "CPU[%02d]: Failed to wait epoll\n", worker->cpu);
break;
}
for (i = 0; i < total; i++) {
err = recvfrom(worker->fds[events[i].data.u32], buf, 1024, 0,
(struct sockaddr *)&addr, &addrlen);
if (err < 0)
continue;
fprintf(stdout, "CPU[%02d]: Received data on %d: %s\n",
worker->cpu, PORT_START + events[i].data.u32, buf);
}
}
teardown_worker(worker);
return 0;
}
static int run_workers()
{
int nr_cpu, i;
pid_t pid;
nr_cpu = libbpf_num_possible_cpus();
for (i = 0; i < nr_cpu; i++) {
pid = fork();
if (pid < 0) {
fprintf(stderr, "Failed to create %dth child\n", i);
nr_cpu = i;
break;
}
if (pid == 0)
return run_worker(i);
}
return nr_cpu;
}
static int wait_workers(int nr_worker)
{
int i, wstatus;
for (i = 0; i < nr_worker; i++)
wait(&wstatus);
}
int main(int argc, char **argv)
{
int err, nr_worker;
err = setup_parent();
if (err)
return err;
/* Run workers on each CPU */
nr_worker = run_workers();
if (nr_worker)
wait_workers(nr_worker);
return 0;
}