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
|
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
* Simple test case using the socket op
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include <assert.h>
#include "liburing.h"
#include "helpers.h"
static char str[] = "This is a test of send and recv over io_uring!";
#define MAX_MSG 128
#define HOST "127.0.0.1"
static int no_socket;
static __be32 g_port;
static int recv_prep(struct io_uring *ring, struct iovec *iov, int *sock,
int registerfiles)
{
struct sockaddr_in saddr;
struct io_uring_sqe *sqe;
int sockfd, ret, val, use_fd;
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("socket");
return 1;
}
val = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
if (t_bind_ephemeral_port(sockfd, &saddr)) {
perror("bind");
goto err;
}
g_port = saddr.sin_port;
if (registerfiles) {
ret = io_uring_register_files(ring, &sockfd, 1);
if (ret) {
fprintf(stderr, "file reg failed\n");
goto err;
}
use_fd = 0;
} else {
use_fd = sockfd;
}
sqe = io_uring_get_sqe(ring);
io_uring_prep_recv(sqe, use_fd, iov->iov_base, iov->iov_len, 0);
if (registerfiles)
sqe->flags |= IOSQE_FIXED_FILE;
sqe->user_data = 2;
ret = io_uring_submit(ring);
if (ret <= 0) {
fprintf(stderr, "submit failed: %d\n", ret);
goto err;
}
*sock = sockfd;
return 0;
err:
close(sockfd);
return 1;
}
static int do_recv(struct io_uring *ring, struct iovec *iov)
{
struct io_uring_cqe *cqe;
int ret;
ret = io_uring_wait_cqe(ring, &cqe);
if (ret) {
fprintf(stdout, "wait_cqe: %d\n", ret);
goto err;
}
if (cqe->res == -EINVAL) {
fprintf(stdout, "recv not supported, skipping\n");
return 0;
}
if (cqe->res < 0) {
fprintf(stderr, "failed cqe: %d\n", cqe->res);
goto err;
}
if (cqe->res -1 != strlen(str)) {
fprintf(stderr, "got wrong length: %d/%d\n", cqe->res,
(int) strlen(str) + 1);
goto err;
}
if (strcmp(str, iov->iov_base)) {
fprintf(stderr, "string mismatch\n");
goto err;
}
return 0;
err:
return 1;
}
struct recv_data {
pthread_mutex_t mutex;
int use_sqthread;
int registerfiles;
};
static void *recv_fn(void *data)
{
struct recv_data *rd = data;
char buf[MAX_MSG + 1];
struct iovec iov = {
.iov_base = buf,
.iov_len = sizeof(buf) - 1,
};
struct io_uring_params p = { };
struct io_uring ring;
int ret, sock;
if (rd->use_sqthread)
p.flags = IORING_SETUP_SQPOLL;
ret = t_create_ring_params(1, &ring, &p);
if (ret == T_SETUP_SKIP) {
pthread_mutex_unlock(&rd->mutex);
ret = 0;
goto err;
} else if (ret < 0) {
pthread_mutex_unlock(&rd->mutex);
goto err;
}
if (rd->use_sqthread && !rd->registerfiles) {
if (!(p.features & IORING_FEAT_SQPOLL_NONFIXED)) {
fprintf(stdout, "Non-registered SQPOLL not available, skipping\n");
pthread_mutex_unlock(&rd->mutex);
goto err;
}
}
ret = recv_prep(&ring, &iov, &sock, rd->registerfiles);
if (ret) {
fprintf(stderr, "recv_prep failed: %d\n", ret);
goto err;
}
pthread_mutex_unlock(&rd->mutex);
ret = do_recv(&ring, &iov);
close(sock);
io_uring_queue_exit(&ring);
err:
return (void *)(intptr_t)ret;
}
static int fallback_send(struct io_uring *ring, struct sockaddr_in *saddr)
{
struct iovec iov = {
.iov_base = str,
.iov_len = sizeof(str),
};
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
int sockfd, ret;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("socket");
return 1;
}
ret = connect(sockfd, (struct sockaddr *)saddr, sizeof(*saddr));
if (ret < 0) {
perror("connect");
return 1;
}
sqe = io_uring_get_sqe(ring);
io_uring_prep_send(sqe, sockfd, iov.iov_base, iov.iov_len, 0);
sqe->user_data = 1;
ret = io_uring_submit(ring);
if (ret <= 0) {
fprintf(stderr, "submit failed: %d\n", ret);
goto err;
}
ret = io_uring_wait_cqe(ring, &cqe);
if (cqe->res == -EINVAL) {
fprintf(stdout, "send not supported, skipping\n");
close(sockfd);
return 0;
}
if (cqe->res != iov.iov_len) {
fprintf(stderr, "failed cqe: %d\n", cqe->res);
goto err;
}
close(sockfd);
return 0;
err:
close(sockfd);
return 1;
}
static int do_send(int socket_direct, int alloc)
{
struct sockaddr_in saddr;
struct iovec iov = {
.iov_base = str,
.iov_len = sizeof(str),
};
struct io_uring ring;
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
int sockfd, ret, fd = -1;
ret = io_uring_queue_init(1, &ring, 0);
if (ret) {
fprintf(stderr, "queue init failed: %d\n", ret);
return 1;
}
if (socket_direct) {
ret = io_uring_register_files(&ring, &fd, 1);
if (ret) {
fprintf(stderr, "file register %d\n", ret);
return 1;
}
}
assert(g_port != 0);
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = g_port;
inet_pton(AF_INET, HOST, &saddr.sin_addr);
sqe = io_uring_get_sqe(&ring);
if (socket_direct) {
unsigned file_index = 0;
if (alloc)
file_index = IORING_FILE_INDEX_ALLOC - 1;
io_uring_prep_socket_direct(sqe, AF_INET, SOCK_DGRAM, 0,
file_index, 0);
} else {
io_uring_prep_socket(sqe, AF_INET, SOCK_DGRAM, 0, 0);
}
ret = io_uring_submit(&ring);
if (ret != 1) {
fprintf(stderr, "socket submit: %d\n", ret);
return 1;
}
ret = io_uring_wait_cqe(&ring, &cqe);
if (ret) {
fprintf(stderr, "wait_cqe: %d\n", ret);
return 1;
}
if (cqe->res < 0) {
if (cqe->res == -EINVAL) {
no_socket = 1;
io_uring_cqe_seen(&ring, cqe);
return fallback_send(&ring, &saddr);
}
fprintf(stderr, "socket res: %d\n", ret);
return 1;
}
sockfd = cqe->res;
if (socket_direct && !alloc)
sockfd = 0;
io_uring_cqe_seen(&ring, cqe);
sqe = io_uring_get_sqe(&ring);
io_uring_prep_connect(sqe, sockfd, (struct sockaddr *) &saddr,
sizeof(saddr));
if (socket_direct)
sqe->flags |= IOSQE_FIXED_FILE;
ret = io_uring_submit(&ring);
if (ret != 1) {
fprintf(stderr, "connect submit: %d\n", ret);
return 1;
}
ret = io_uring_wait_cqe(&ring, &cqe);
if (ret) {
fprintf(stderr, "wait_cqe: %d\n", ret);
return 1;
}
if (cqe->res < 0) {
fprintf(stderr, "connect res: %d\n", cqe->res);
return 1;
}
io_uring_cqe_seen(&ring, cqe);
sqe = io_uring_get_sqe(&ring);
io_uring_prep_send(sqe, sockfd, iov.iov_base, iov.iov_len, 0);
sqe->user_data = 1;
if (socket_direct)
sqe->flags |= IOSQE_FIXED_FILE;
ret = io_uring_submit(&ring);
if (ret <= 0) {
fprintf(stderr, "submit failed: %d\n", ret);
goto err;
}
ret = io_uring_wait_cqe(&ring, &cqe);
if (cqe->res == -EINVAL) {
fprintf(stdout, "send not supported, skipping\n");
close(sockfd);
return 0;
}
if (cqe->res != iov.iov_len) {
fprintf(stderr, "failed cqe: %d\n", cqe->res);
goto err;
}
close(sockfd);
return 0;
err:
close(sockfd);
return 1;
}
static int test(int use_sqthread, int regfiles, int socket_direct, int alloc)
{
pthread_mutexattr_t attr;
pthread_t recv_thread;
struct recv_data rd;
int ret;
void *retval;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, 1);
pthread_mutex_init(&rd.mutex, &attr);
pthread_mutex_lock(&rd.mutex);
rd.use_sqthread = use_sqthread;
rd.registerfiles = regfiles;
ret = pthread_create(&recv_thread, NULL, recv_fn, &rd);
if (ret) {
fprintf(stderr, "Thread create failed: %d\n", ret);
pthread_mutex_unlock(&rd.mutex);
return 1;
}
pthread_mutex_lock(&rd.mutex);
do_send(socket_direct, alloc);
pthread_join(recv_thread, &retval);
return (intptr_t)retval;
}
static int test_bad_socket(void)
{
struct io_uring ring;
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
int ret;
ret = io_uring_queue_init(1, &ring, 0);
if (ret) {
fprintf(stderr, "queue init failed: %d\n", ret);
return 1;
}
sqe = io_uring_get_sqe(&ring);
io_uring_prep_socket(sqe, -1, SOCK_DGRAM, 0, 0);
ret = io_uring_submit(&ring);
if (ret != 1) {
fprintf(stderr, "socket submit: %d\n", ret);
goto err;
}
ret = io_uring_wait_cqe(&ring, &cqe);
if (ret) {
fprintf(stderr, "wait_cqe: %d\n", ret);
goto err;
}
if (cqe->res != -EAFNOSUPPORT) {
fprintf(stderr, "socket res: %d\n", cqe->res);
goto err;
}
io_uring_cqe_seen(&ring, cqe);
io_uring_queue_exit(&ring);
return 0;
err:
io_uring_queue_exit(&ring);
return 1;
}
int main(int argc, char *argv[])
{
int ret;
if (argc > 1)
return 0;
ret = test(0, 0, 0, 0);
if (ret) {
fprintf(stderr, "test sqthread=0 failed\n");
return ret;
}
if (no_socket)
return 0;
ret = test(1, 1, 0, 0);
if (ret) {
fprintf(stderr, "test sqthread=1 reg=1 failed\n");
return ret;
}
ret = test(1, 0, 0, 0);
if (ret) {
fprintf(stderr, "test sqthread=1 reg=0 failed\n");
return ret;
}
ret = test(0, 0, 1, 0);
if (ret) {
fprintf(stderr, "test sqthread=0 direct=1 failed\n");
return ret;
}
ret = test(0, 0, 1, 1);
if (ret) {
fprintf(stderr, "test sqthread=0 direct=alloc failed\n");
return ret;
}
ret = test_bad_socket();
if (ret) {
fprintf(stderr, "test bad socket failed\n");
return 1;
}
return 0;
}
|