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
|
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
* Description: Check that {g,s}etsockopt CMD operations on sockets are
* consistent.
*
* The tests basically do the same socket operation using regular system calls
* and io_uring commands, and then compare the results.
*/
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <linux/tcp.h>
#include "liburing.h"
#include "helpers.h"
#define USERDATA 0xff42ff
#define MSG "foobarbaz"
static int no_sock_opt;
struct fds {
int tx;
int rx;
};
static struct fds create_sockets(void)
{
struct fds retval;
int fd[2];
t_create_socket_pair(fd, true);
retval.tx = fd[0];
retval.rx = fd[1];
return retval;
}
static struct io_uring create_ring(void)
{
struct io_uring ring;
int ring_flags = 0;
int err;
err = io_uring_queue_init(32, &ring, ring_flags);
assert(err == 0);
return ring;
}
static int submit_cmd_sqe(struct io_uring *ring, int32_t fd,
int op, int level, int optname,
void *optval, int optlen,
bool async)
{
struct io_uring_sqe *sqe;
int err;
assert(fd > 0);
sqe = io_uring_get_sqe(ring);
assert(sqe != NULL);
io_uring_prep_cmd_sock(sqe, op, fd, level, optname, optval, optlen);
sqe->user_data = USERDATA;
if (async)
sqe->flags |= IOSQE_ASYNC;
/* Submitting SQE */
err = io_uring_submit_and_wait(ring, 1);
if (err != 1)
fprintf(stderr, "Failure: io_uring_submit_and_wait returned %d\n", err);
return err;
}
static int receive_cqe(struct io_uring *ring)
{
struct io_uring_cqe *cqe;
int err;
err = io_uring_wait_cqe(ring, &cqe);
assert(err == 0);
assert(cqe->user_data == USERDATA);
io_uring_cqe_seen(ring, cqe);
/* Return the result of the operation */
return cqe->res;
}
/*
* Run getsock operation using SO_RCVBUF using io_uring cmd operation and
* getsockopt(2) and compare the results.
*/
static int run_get_rcvbuf(struct io_uring *ring, struct fds *sockfds, bool async)
{
int sval, uval, ulen, err;
unsigned int slen;
/* System call values */
slen = sizeof(sval);
/* io_uring values */
ulen = sizeof(uval);
/* get through io_uring cmd */
err = submit_cmd_sqe(ring, sockfds->rx, SOCKET_URING_OP_GETSOCKOPT,
SOL_SOCKET, SO_RCVBUF, &uval, ulen, async);
assert(err == 1);
/* Wait for the CQE */
err = receive_cqe(ring);
if (err == -EOPNOTSUPP)
return T_EXIT_SKIP;
if (err < 0) {
fprintf(stderr, "Error received. %d\n", err);
return T_EXIT_FAIL;
}
/* The output of CQE->res contains the length */
ulen = err;
/* Executes the same operation using system call */
err = getsockopt(sockfds->rx, SOL_SOCKET, SO_RCVBUF, &sval, &slen);
assert(err == 0);
/* Make sure that io_uring operation returns the same value as the systemcall */
assert(ulen == slen);
assert(uval == sval);
return T_EXIT_PASS;
}
/*
* Run getsock operation using SO_PEERNAME using io_uring cmd operation
* and getsockopt(2) and compare the results.
*/
static int run_get_peername(struct io_uring *ring, struct fds *sockfds, bool async)
{
struct sockaddr sval, uval = {};
socklen_t slen = sizeof(sval);
socklen_t ulen = sizeof(uval);
int err;
/* Get values from the systemcall */
err = getsockopt(sockfds->tx, SOL_SOCKET, SO_PEERNAME, &sval, &slen);
assert(err == 0);
/* Getting SO_PEERNAME */
err = submit_cmd_sqe(ring, sockfds->rx, SOCKET_URING_OP_GETSOCKOPT,
SOL_SOCKET, SO_PEERNAME, &uval, ulen, async);
assert(err == 1);
/* Wait for the CQE */
err = receive_cqe(ring);
if (err == -EOPNOTSUPP || err == -EINVAL) {
no_sock_opt = 1;
return T_EXIT_SKIP;
}
if (err < 0) {
fprintf(stderr, "%s: Error in the CQE: %d\n", __func__, err);
return T_EXIT_FAIL;
}
/* The length comes from cqe->res, which is returned from receive_cqe() */
ulen = err;
/* Make sure that io_uring operation returns the same values as the systemcall */
assert(sval.sa_family == uval.sa_family);
assert(slen == ulen);
return T_EXIT_PASS;
}
/*
* Run getsockopt tests. Basically comparing io_uring output and systemcall results
*/
static int run_getsockopt_test(struct io_uring *ring, struct fds *sockfds)
{
int err;
err = run_get_peername(ring, sockfds, false);
if (err)
return err;
err = run_get_peername(ring, sockfds, true);
if (err)
return err;
err = run_get_rcvbuf(ring, sockfds, false);
if (err)
return err;
return run_get_rcvbuf(ring, sockfds, true);
}
/*
* Given a `val` value, set it in SO_REUSEPORT using io_uring cmd, and read using
* getsockopt(2), and make sure they match.
*/
static int run_setsockopt_reuseport(struct io_uring *ring, struct fds *sockfds,
int val, bool async)
{
unsigned int slen, ulen;
int sval, uval = val;
int err;
slen = sizeof(sval);
ulen = sizeof(uval);
/* Setting SO_REUSEPORT */
err = submit_cmd_sqe(ring, sockfds->rx, SOCKET_URING_OP_SETSOCKOPT,
SOL_SOCKET, SO_REUSEPORT, &uval, ulen, async);
assert(err == 1);
err = receive_cqe(ring);
if (err == -EOPNOTSUPP)
return T_EXIT_SKIP;
/* Get values from the systemcall */
err = getsockopt(sockfds->rx, SOL_SOCKET, SO_REUSEPORT, &sval, &slen);
assert(err == 0);
/* Make sure the set using io_uring cmd matches what systemcall returns */
assert(uval == sval);
assert(ulen == slen);
return T_EXIT_PASS;
}
/*
* Given a `val` value, set the TCP_USER_TIMEOUT using io_uring and read using
* getsockopt(2). Make sure they match
*/
static int run_setsockopt_usertimeout(struct io_uring *ring, struct fds *sockfds,
int val, bool async)
{
int optname = TCP_USER_TIMEOUT;
int level = IPPROTO_TCP;
unsigned int slen, ulen;
int sval, uval, err;
slen = sizeof(uval);
ulen = sizeof(uval);
uval = val;
/* Setting timeout */
err = submit_cmd_sqe(ring, sockfds->rx, SOCKET_URING_OP_SETSOCKOPT,
level, optname, &uval, ulen, async);
assert(err == 1);
err = receive_cqe(ring);
if (err == -EOPNOTSUPP)
return T_EXIT_SKIP;
if (err < 0) {
fprintf(stderr, "%s: Got an error: %d\n", __func__, err);
return T_EXIT_FAIL;
}
/* Get the value from the systemcall, to make sure it was set */
err = getsockopt(sockfds->rx, level, optname, &sval, &slen);
assert(err == 0);
assert(uval == sval);
return T_EXIT_PASS;
}
/* Test setsockopt() for SOL_SOCKET */
static int run_setsockopt_test(struct io_uring *ring, struct fds *sockfds)
{
int err, i;
int j;
for (j = 0; j < 2; j++) {
bool async = j & 1;
for (i = 0; i <= 1; i++) {
err = run_setsockopt_reuseport(ring, sockfds, i, async);
if (err)
return err;
}
for (i = 1; i <= 10; i++) {
err = run_setsockopt_usertimeout(ring, sockfds, i, async);
if (err)
return err;
}
}
return err;
}
/* Send data through the sockets */
static void send_data(struct fds *s)
{
int written_bytes;
/* Send data sing the sockstruct->send */
written_bytes = write(s->tx, MSG, strlen(MSG));
assert(written_bytes == strlen(MSG));
}
int main(int argc, char *argv[])
{
struct fds sockfds;
struct io_uring ring;
int err;
if (argc > 1)
return T_EXIT_SKIP;
/* Simply io_uring ring creation */
ring = create_ring();
/* Create sockets */
sockfds = create_sockets();
send_data(&sockfds);
err = run_getsockopt_test(&ring, &sockfds);
if (err) {
if (err == T_EXIT_SKIP) {
fprintf(stderr, "Skipping tests.\n");
return T_EXIT_SKIP;
}
fprintf(stderr, "Failed to run test: %d\n", err);
return err;
}
if (no_sock_opt)
return T_EXIT_SKIP;
err = run_setsockopt_test(&ring, &sockfds);
if (err) {
if (err == T_EXIT_SKIP) {
fprintf(stderr, "Skipping tests.\n");
return T_EXIT_SKIP;
}
fprintf(stderr, "Failed to run test: %d\n", err);
return err;
}
io_uring_queue_exit(&ring);
return err;
}
|