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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
|
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
* Description: stress test multishot accept under rapid connection churn.
* Submits a multishot accept and has client threads rapidly
* connecting and disconnecting, verifying all connections are
* properly accepted.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include "liburing.h"
#include "helpers.h"
#define NR_CONNS 200
#define NR_BURST 32
#define NR_BURST_ROUNDS 4
#define NR_RECONNECTS 1000
#define MSHOT_UDATA 1000
#define SEND_BYTE 0xa5
static int no_mshot_accept;
struct stress_ctx {
pthread_barrier_t barrier;
struct sockaddr_in addr;
int nr_conns;
};
struct burst_ctx {
pthread_barrier_t barrier;
pthread_barrier_t round_barrier;
struct sockaddr_in addr;
int nr_burst;
int nr_rounds;
};
struct reconnect_ctx {
pthread_barrier_t barrier;
struct sockaddr_in addr;
int nr_reconnects;
};
static int create_listen_sock(struct sockaddr_in *addr)
{
int fd, ret, val = 1;
fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
if (fd < 0) {
perror("socket");
return -1;
}
setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr("127.0.0.1");
ret = t_bind_ephemeral_port(fd, addr);
if (ret) {
close(fd);
return -1;
}
ret = listen(fd, 256);
if (ret < 0) {
perror("listen");
close(fd);
return -1;
}
return fd;
}
static int arm_mshot_accept(struct io_uring *ring, int listen_fd)
{
struct io_uring_sqe *sqe;
sqe = io_uring_get_sqe(ring);
if (!sqe)
return -1;
io_uring_prep_multishot_accept(sqe, listen_fd, NULL, NULL, 0);
io_uring_sqe_set_data64(sqe, MSHOT_UDATA);
return io_uring_submit(ring);
}
/*
* Client thread for stress test: rapid connect + close
*/
static void *stress_client_fn(void *data)
{
struct stress_ctx *ctx = data;
int i;
pthread_barrier_wait(&ctx->barrier);
for (i = 0; i < ctx->nr_conns; i++) {
int fd;
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd < 0)
break;
if (connect(fd, (struct sockaddr *)&ctx->addr,
sizeof(ctx->addr)) < 0) {
close(fd);
break;
}
close(fd);
}
return NULL;
}
/*
* Stress test: multishot accept with NR_CONNS rapid connections
*/
static int test_accept_mshot_stress(void)
{
struct io_uring ring;
struct stress_ctx ctx;
pthread_t thread;
int listen_fd, ret, accepted = 0;
struct __kernel_timespec ts = { .tv_sec = 5 };
ret = io_uring_queue_init(512, &ring, 0);
if (ret) {
fprintf(stderr, "ring setup: %d\n", ret);
return T_EXIT_FAIL;
}
listen_fd = create_listen_sock(&ctx.addr);
if (listen_fd < 0) {
io_uring_queue_exit(&ring);
return T_EXIT_FAIL;
}
ctx.nr_conns = NR_CONNS;
pthread_barrier_init(&ctx.barrier, NULL, 2);
ret = arm_mshot_accept(&ring, listen_fd);
if (ret < 0) {
fprintf(stderr, "arm accept: %d\n", ret);
goto err;
}
pthread_create(&thread, NULL, stress_client_fn, &ctx);
pthread_barrier_wait(&ctx.barrier);
while (accepted < NR_CONNS) {
struct io_uring_cqe *cqe;
ret = io_uring_wait_cqe_timeout(&ring, &cqe, &ts);
if (ret == -ETIME)
break;
if (ret) {
fprintf(stderr, "wait: %d\n", ret);
goto err_thread;
}
if (cqe->res == -EINVAL) {
no_mshot_accept = 1;
io_uring_cqe_seen(&ring, cqe);
pthread_join(thread, NULL);
close(listen_fd);
io_uring_queue_exit(&ring);
return T_EXIT_SKIP;
}
if (cqe->res < 0) {
fprintf(stderr, "accept res: %d\n", cqe->res);
io_uring_cqe_seen(&ring, cqe);
goto err_thread;
}
close(cqe->res);
accepted++;
if (!(cqe->flags & IORING_CQE_F_MORE)) {
io_uring_cqe_seen(&ring, cqe);
arm_mshot_accept(&ring, listen_fd);
continue;
}
io_uring_cqe_seen(&ring, cqe);
}
pthread_join(thread, NULL);
if (accepted < NR_CONNS) {
fprintf(stderr, "stress: accepted %d, expected %d\n",
accepted, NR_CONNS);
goto err;
}
close(listen_fd);
io_uring_queue_exit(&ring);
return T_EXIT_PASS;
err_thread:
pthread_join(thread, NULL);
err:
close(listen_fd);
io_uring_queue_exit(&ring);
return T_EXIT_FAIL;
}
/*
* Client thread for burst test: open NR_BURST connections, wait for
* server to drain, close all, repeat.
*/
static void *burst_client_fn(void *data)
{
struct burst_ctx *ctx = data;
int round;
pthread_barrier_wait(&ctx->barrier);
for (round = 0; round < ctx->nr_rounds; round++) {
int fds[NR_BURST];
int i;
for (i = 0; i < ctx->nr_burst; i++) {
fds[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fds[i] < 0)
break;
if (connect(fds[i], (struct sockaddr *)&ctx->addr,
sizeof(ctx->addr)) < 0) {
close(fds[i]);
fds[i] = -1;
break;
}
}
/* Signal server: all connections are open */
pthread_barrier_wait(&ctx->round_barrier);
/* Wait for server to drain */
pthread_barrier_wait(&ctx->round_barrier);
for (i = 0; i < ctx->nr_burst; i++) {
if (fds[i] >= 0)
close(fds[i]);
}
}
return NULL;
}
/*
* Burst test: client opens NR_BURST connections at once, server drains,
* client closes, repeat for NR_BURST_ROUNDS.
*/
static int test_accept_mshot_burst(void)
{
struct io_uring ring;
struct burst_ctx ctx;
pthread_t thread;
int listen_fd, ret, total = 0;
struct __kernel_timespec ts = { .tv_sec = 5 };
if (no_mshot_accept)
return T_EXIT_SKIP;
ret = io_uring_queue_init(256, &ring, 0);
if (ret) {
fprintf(stderr, "ring setup: %d\n", ret);
return T_EXIT_FAIL;
}
listen_fd = create_listen_sock(&ctx.addr);
if (listen_fd < 0) {
io_uring_queue_exit(&ring);
return T_EXIT_FAIL;
}
ctx.nr_burst = NR_BURST;
ctx.nr_rounds = NR_BURST_ROUNDS;
pthread_barrier_init(&ctx.barrier, NULL, 2);
pthread_barrier_init(&ctx.round_barrier, NULL, 2);
ret = arm_mshot_accept(&ring, listen_fd);
if (ret < 0)
goto err;
pthread_create(&thread, NULL, burst_client_fn, &ctx);
pthread_barrier_wait(&ctx.barrier);
for (int round = 0; round < NR_BURST_ROUNDS; round++) {
int round_accepted = 0;
/* Wait for client to open all connections */
pthread_barrier_wait(&ctx.round_barrier);
while (round_accepted < NR_BURST) {
struct io_uring_cqe *cqe;
ret = io_uring_wait_cqe_timeout(&ring, &cqe, &ts);
if (ret == -ETIME) {
fprintf(stderr, "burst round %d timeout at %d\n",
round, round_accepted);
goto err_thread;
}
if (ret) {
fprintf(stderr, "wait: %d\n", ret);
goto err_thread;
}
if (cqe->res < 0) {
fprintf(stderr, "accept res: %d\n", cqe->res);
io_uring_cqe_seen(&ring, cqe);
goto err_thread;
}
close(cqe->res);
round_accepted++;
total++;
if (!(cqe->flags & IORING_CQE_F_MORE)) {
io_uring_cqe_seen(&ring, cqe);
arm_mshot_accept(&ring, listen_fd);
continue;
}
io_uring_cqe_seen(&ring, cqe);
}
/* Signal client: done draining */
pthread_barrier_wait(&ctx.round_barrier);
}
pthread_join(thread, NULL);
if (total != NR_BURST * NR_BURST_ROUNDS) {
fprintf(stderr, "burst: accepted %d, expected %d\n",
total, NR_BURST * NR_BURST_ROUNDS);
goto err;
}
close(listen_fd);
io_uring_queue_exit(&ring);
return T_EXIT_PASS;
err_thread:
pthread_join(thread, NULL);
err:
close(listen_fd);
io_uring_queue_exit(&ring);
return T_EXIT_FAIL;
}
/*
* Client thread for reconnect test: connect, send 1 byte, close, repeat.
*/
static void *reconnect_client_fn(void *data)
{
struct reconnect_ctx *ctx = data;
unsigned char byte = SEND_BYTE;
int i;
pthread_barrier_wait(&ctx->barrier);
for (i = 0; i < ctx->nr_reconnects; i++) {
int fd;
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd < 0)
break;
if (connect(fd, (struct sockaddr *)&ctx->addr,
sizeof(ctx->addr)) < 0) {
close(fd);
break;
}
send(fd, &byte, 1, 0);
close(fd);
}
return NULL;
}
/*
* Reconnect test: client connects, sends 1 byte, closes, reconnects.
* Server accepts each and reads the byte.
*/
static int test_accept_mshot_reconnect(void)
{
struct io_uring ring;
struct reconnect_ctx ctx;
pthread_t thread;
int listen_fd, ret, accepted = 0;
struct __kernel_timespec ts = { .tv_sec = 5 };
if (no_mshot_accept)
return T_EXIT_SKIP;
ret = io_uring_queue_init(512, &ring, 0);
if (ret) {
fprintf(stderr, "ring setup: %d\n", ret);
return T_EXIT_FAIL;
}
listen_fd = create_listen_sock(&ctx.addr);
if (listen_fd < 0) {
io_uring_queue_exit(&ring);
return T_EXIT_FAIL;
}
ctx.nr_reconnects = NR_RECONNECTS;
pthread_barrier_init(&ctx.barrier, NULL, 2);
ret = arm_mshot_accept(&ring, listen_fd);
if (ret < 0)
goto err;
pthread_create(&thread, NULL, reconnect_client_fn, &ctx);
pthread_barrier_wait(&ctx.barrier);
while (accepted < NR_RECONNECTS) {
struct io_uring_cqe *cqe;
unsigned char buf;
int conn_fd;
ret = io_uring_wait_cqe_timeout(&ring, &cqe, &ts);
if (ret == -ETIME)
break;
if (ret) {
fprintf(stderr, "wait: %d\n", ret);
goto err_thread;
}
if (cqe->res < 0) {
fprintf(stderr, "accept res: %d\n", cqe->res);
io_uring_cqe_seen(&ring, cqe);
goto err_thread;
}
conn_fd = cqe->res;
if (!(cqe->flags & IORING_CQE_F_MORE)) {
io_uring_cqe_seen(&ring, cqe);
arm_mshot_accept(&ring, listen_fd);
} else {
io_uring_cqe_seen(&ring, cqe);
}
/* Read the byte the client sent */
ret = read(conn_fd, &buf, 1);
if (ret == 1 && buf != SEND_BYTE) {
fprintf(stderr, "bad byte: 0x%x\n", buf);
close(conn_fd);
goto err_thread;
}
close(conn_fd);
accepted++;
}
pthread_join(thread, NULL);
if (accepted < NR_RECONNECTS) {
fprintf(stderr, "reconnect: accepted %d, expected %d\n",
accepted, NR_RECONNECTS);
goto err;
}
close(listen_fd);
io_uring_queue_exit(&ring);
return T_EXIT_PASS;
err_thread:
pthread_join(thread, NULL);
err:
close(listen_fd);
io_uring_queue_exit(&ring);
return T_EXIT_FAIL;
}
int main(int argc, char *argv[])
{
int ret;
if (argc > 1)
return T_EXIT_SKIP;
ret = test_accept_mshot_stress();
if (ret == T_EXIT_SKIP) {
printf("Multishot accept not supported, skipping\n");
return T_EXIT_SKIP;
}
if (ret == T_EXIT_FAIL) {
fprintf(stderr, "test_accept_mshot_stress failed\n");
return T_EXIT_FAIL;
}
ret = test_accept_mshot_burst();
if (ret == T_EXIT_FAIL) {
fprintf(stderr, "test_accept_mshot_burst failed\n");
return T_EXIT_FAIL;
}
ret = test_accept_mshot_reconnect();
if (ret == T_EXIT_FAIL) {
fprintf(stderr, "test_accept_mshot_reconnect failed\n");
return T_EXIT_FAIL;
}
return T_EXIT_PASS;
}
|