-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudp_multi_thread.c
More file actions
276 lines (217 loc) · 7.11 KB
/
udp_multi_thread.c
File metadata and controls
276 lines (217 loc) · 7.11 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
// Heavily influenced by https://github.com/q2ven/reuseport_cpu
// But we adapted this approach to be multi-threaded instead of multi-process.
// You'll need to install gcc, clang, bpftool, and libbpf-devel
// sudo mount -t bpf bpf /sys/fs/bpf
// run with `sudo ./udp_multi_thread`
//
// Use the following command to see the CPU which the application threads are pinned to
// ps -mo pid,tid,%cpu,psr -p <pid>
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sched.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include "reuseport_cpu.skel.h"
#define PORT 2048
#define BUFFER_SIZE 1024
#define PATH_LEN 128
#define PATH_MAP "/sys/fs/bpf/reuseport_map_%05d"
#define PATH_PROG "/sys/fs/bpf/reuseport_prog_%05d"
// Structure to pass data to threads
typedef struct {
// thread_id is overloaded, it's also the cpu id
// that we will pin the thread to.
int thread_id;
} thread_data_t;
static int pin_thread_to_cpu(int cpu_id) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(cpu_id, &cpuset);
// Set the CPU affinity for the current thread
int result = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
if (result != 0) {
// Handle error
return -1;
}
return 0;
}
static int libbpf_print_fn(enum libbpf_print_level level __attribute__((unused)), const char *format, va_list args)
{
return vfprintf(stderr, format, args);
}
static int attach_reuseport_prog(thread_data_t *data, int sockfd, int port)
{
char path[PATH_LEN];
int prog_fd, err;
snprintf(path, PATH_LEN, PATH_PROG, port);
prog_fd = bpf_obj_get(path);
if (prog_fd < 0)
return prog_fd;
err = setsockopt(sockfd, SOL_SOCKET, SO_ATTACH_REUSEPORT_EBPF,
&prog_fd, sizeof(prog_fd));
if (err)
fprintf(stderr, "CPU[%02d]: Failed to attach BPF prog\n", data->thread_id);
close(prog_fd);
return err;
}
static int update_reuseport_map(thread_data_t *data, int sockfd)
{
char path[PATH_LEN];
int map_fd, err;
// This picks the map of sockfds for a port
snprintf(path, PATH_LEN, PATH_MAP, PORT);
/* Load pinned BPF map */
map_fd = bpf_obj_get(path);
if (map_fd < 0) {
fprintf(stderr, "CPU[%02d]: Failed to open BPF map %s\n", data->thread_id, path);
}
err = bpf_map_update_elem(map_fd, &data->thread_id, &sockfd, BPF_NOEXIST);
if (err) {
fprintf(stderr, "CPU[%02d]: Failed to update BPF map for sockfd %d\n", data->thread_id, sockfd);
}
close(map_fd);
return err;
}
void *socket_thread(void *arg) {
thread_data_t *data = (thread_data_t *)arg;
int thread_id = data->thread_id;
int sockfd = -1;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len = sizeof(client_addr);
char buffer[BUFFER_SIZE];
int opt = 1;
if (pin_thread_to_cpu(thread_id) < 0) {
perror("Failed to pin thread to CPU");
goto close;
}
// Create UDP socket
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("Socket creation failed");
goto close;
}
// Set SO_REUSEPORT option
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
perror("setsockopt SO_REUSEPORT failed");
goto close;
}
// Configure server address
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
// Bind socket to address and port
if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Bind failed");
goto close;
}
printf("Thread %d: Socket bound to port %d with SO_REUSEPORT\n", thread_id, PORT);
/* Update BPF map like: map[cpu_id] = socket_fd */
if (update_reuseport_map(data, sockfd) != 0) {
perror("update_reuseport_map failed");
goto close;
}
/* Attach BPF program to reuseport group. We only need to attach to one of the sockets,
* so restrict this to thread 0. */
if (thread_id == 0) {
if (attach_reuseport_prog(data, sockfd, PORT) != 0) {
perror("attach_reuseport_prog failed");
goto close;
}
}
// Main receive loop
while (1) {
int n = recvfrom(sockfd, buffer, BUFFER_SIZE, 0,
(struct sockaddr *)&client_addr, &client_len);
if (n < 0) {
perror("recvfrom failed");
continue;
}
buffer[n] = '\0';
char client_ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &client_addr.sin_addr, client_ip, sizeof(client_ip));
printf("Thread %d received from %s:%d: %s\n",
thread_id, client_ip, ntohs(client_addr.sin_port), buffer);
// Echo back to client
sendto(sockfd, buffer, n, 0, (struct sockaddr *)&client_addr, client_len);
}
close:
if (sockfd != -1) {
close(sockfd);
}
pthread_exit(NULL);
}
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_bpf_map(void)
{
libbpf_set_print(libbpf_print_fn);
return pin_bpf_obj(PORT);
}
int main() {
int num_cpus = libbpf_num_possible_cpus();
pthread_t threads[num_cpus];
thread_data_t thread_data[num_cpus];
printf("Starting UDP server with %d threads on port %d\n", num_cpus, PORT);
if (setup_bpf_map() != 0) {
perror("setup_bpf_map failed");
exit(1);
}
// Create threads
for (int i = 0; i < num_cpus; i++) {
thread_data[i].thread_id = i;
if (pthread_create(&threads[i], NULL, socket_thread, &thread_data[i]) != 0) {
perror("Failed to create thread");
return 1;
}
printf("Thread %d created\n", i);
}
// Wait for all threads to complete (which won't happen in this example)
for (int i = 0; i < num_cpus; i++) {
pthread_join(threads[i], NULL);
}
return 0;
}