1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Description: basic read/write tests with polled IO
4 */
5 #include <errno.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <fcntl.h>
11 #include <sys/types.h>
12 #include <poll.h>
13 #include <sys/eventfd.h>
14 #include <sys/resource.h>
15 #include "helpers.h"
16 #include "liburing.h"
17 #include "../src/syscall.h"
18
19 #define FILE_SIZE (128 * 1024)
20 #define BS 4096
21 #define BUFFERS (FILE_SIZE / BS)
22
23 static struct iovec *vecs;
24 static int no_buf_select;
25 static int no_iopoll;
26
provide_buffers(struct io_uring * ring)27 static int provide_buffers(struct io_uring *ring)
28 {
29 struct io_uring_sqe *sqe;
30 struct io_uring_cqe *cqe;
31 int ret, i;
32
33 for (i = 0; i < BUFFERS; i++) {
34 sqe = io_uring_get_sqe(ring);
35 io_uring_prep_provide_buffers(sqe, vecs[i].iov_base,
36 vecs[i].iov_len, 1, 1, i);
37 }
38
39 ret = io_uring_submit(ring);
40 if (ret != BUFFERS) {
41 fprintf(stderr, "submit: %d\n", ret);
42 return 1;
43 }
44
45 for (i = 0; i < BUFFERS; i++) {
46 ret = io_uring_wait_cqe(ring, &cqe);
47 if (cqe->res < 0) {
48 fprintf(stderr, "cqe->res=%d\n", cqe->res);
49 return 1;
50 }
51 io_uring_cqe_seen(ring, cqe);
52 }
53
54 return 0;
55 }
56
__test_io(const char * file,struct io_uring * ring,int write,int sqthread,int fixed,int buf_select)57 static int __test_io(const char *file, struct io_uring *ring, int write, int sqthread,
58 int fixed, int buf_select)
59 {
60 struct io_uring_sqe *sqe;
61 struct io_uring_cqe *cqe;
62 int open_flags;
63 int i, fd = -1, ret;
64 off_t offset;
65
66 if (buf_select) {
67 write = 0;
68 fixed = 0;
69 }
70 if (buf_select && provide_buffers(ring))
71 return 1;
72
73 if (write)
74 open_flags = O_WRONLY;
75 else
76 open_flags = O_RDONLY;
77 open_flags |= O_DIRECT;
78
79 if (fixed) {
80 ret = t_register_buffers(ring, vecs, BUFFERS);
81 if (ret == T_SETUP_SKIP)
82 return 0;
83 if (ret != T_SETUP_OK) {
84 fprintf(stderr, "buffer reg failed: %d\n", ret);
85 goto err;
86 }
87 }
88 fd = open(file, open_flags);
89 if (fd < 0) {
90 perror("file open");
91 goto err;
92 }
93 if (sqthread) {
94 ret = io_uring_register_files(ring, &fd, 1);
95 if (ret) {
96 fprintf(stderr, "file reg failed: %d\n", ret);
97 goto err;
98 }
99 }
100
101 offset = 0;
102 for (i = 0; i < BUFFERS; i++) {
103 sqe = io_uring_get_sqe(ring);
104 if (!sqe) {
105 fprintf(stderr, "sqe get failed\n");
106 goto err;
107 }
108 offset = BS * (rand() % BUFFERS);
109 if (write) {
110 int do_fixed = fixed;
111 int use_fd = fd;
112
113 if (sqthread)
114 use_fd = 0;
115 if (fixed && (i & 1))
116 do_fixed = 0;
117 if (do_fixed) {
118 io_uring_prep_write_fixed(sqe, use_fd, vecs[i].iov_base,
119 vecs[i].iov_len,
120 offset, i);
121 } else {
122 io_uring_prep_writev(sqe, use_fd, &vecs[i], 1,
123 offset);
124 }
125 } else {
126 int do_fixed = fixed;
127 int use_fd = fd;
128
129 if (sqthread)
130 use_fd = 0;
131 if (fixed && (i & 1))
132 do_fixed = 0;
133 if (do_fixed) {
134 io_uring_prep_read_fixed(sqe, use_fd, vecs[i].iov_base,
135 vecs[i].iov_len,
136 offset, i);
137 } else {
138 io_uring_prep_readv(sqe, use_fd, &vecs[i], 1,
139 offset);
140 }
141
142 }
143 if (sqthread)
144 sqe->flags |= IOSQE_FIXED_FILE;
145 if (buf_select) {
146 sqe->flags |= IOSQE_BUFFER_SELECT;
147 sqe->buf_group = buf_select;
148 sqe->user_data = i;
149 }
150 }
151
152 ret = io_uring_submit(ring);
153 if (ret != BUFFERS) {
154 ret = io_uring_peek_cqe(ring, &cqe);
155 if (!ret && cqe->res == -EOPNOTSUPP) {
156 no_iopoll = 1;
157 io_uring_cqe_seen(ring, cqe);
158 goto out;
159 }
160 fprintf(stderr, "submit got %d, wanted %d\n", ret, BUFFERS);
161 goto err;
162 }
163
164 for (i = 0; i < BUFFERS; i++) {
165 ret = io_uring_wait_cqe(ring, &cqe);
166 if (ret) {
167 fprintf(stderr, "wait_cqe=%d\n", ret);
168 goto err;
169 } else if (cqe->res == -EOPNOTSUPP) {
170 fprintf(stdout, "File/device/fs doesn't support polled IO\n");
171 no_iopoll = 1;
172 goto out;
173 } else if (cqe->res != BS) {
174 fprintf(stderr, "cqe res %d, wanted %d\n", cqe->res, BS);
175 goto err;
176 }
177 io_uring_cqe_seen(ring, cqe);
178 }
179
180 if (fixed) {
181 ret = io_uring_unregister_buffers(ring);
182 if (ret) {
183 fprintf(stderr, "buffer unreg failed: %d\n", ret);
184 goto err;
185 }
186 }
187 if (sqthread) {
188 ret = io_uring_unregister_files(ring);
189 if (ret) {
190 fprintf(stderr, "file unreg failed: %d\n", ret);
191 goto err;
192 }
193 }
194
195 out:
196 close(fd);
197 return 0;
198 err:
199 if (fd != -1)
200 close(fd);
201 return 1;
202 }
203
204 extern int __io_uring_flush_sq(struct io_uring *ring);
205
206 /*
207 * if we are polling io_uring_submit needs to always enter the
208 * kernel to fetch events
209 */
test_io_uring_submit_enters(const char * file)210 static int test_io_uring_submit_enters(const char *file)
211 {
212 struct io_uring ring;
213 int fd, i, ret, ring_flags, open_flags;
214 unsigned head;
215 struct io_uring_cqe *cqe;
216
217 if (no_iopoll)
218 return 0;
219
220 ring_flags = IORING_SETUP_IOPOLL;
221 ret = io_uring_queue_init(64, &ring, ring_flags);
222 if (ret) {
223 fprintf(stderr, "ring create failed: %d\n", ret);
224 return 1;
225 }
226
227 open_flags = O_WRONLY | O_DIRECT;
228 fd = open(file, open_flags);
229 if (fd < 0) {
230 perror("file open");
231 goto err;
232 }
233
234 for (i = 0; i < BUFFERS; i++) {
235 struct io_uring_sqe *sqe;
236 off_t offset = BS * (rand() % BUFFERS);
237
238 sqe = io_uring_get_sqe(&ring);
239 io_uring_prep_writev(sqe, fd, &vecs[i], 1, offset);
240 sqe->user_data = 1;
241 }
242
243 /* submit manually to avoid adding IORING_ENTER_GETEVENTS */
244 ret = __sys_io_uring_enter(ring.ring_fd, __io_uring_flush_sq(&ring), 0,
245 0, NULL);
246 if (ret < 0)
247 goto err;
248
249 for (i = 0; i < 500; i++) {
250 ret = io_uring_submit(&ring);
251 if (ret != 0) {
252 fprintf(stderr, "still had %d sqes to submit, this is unexpected", ret);
253 goto err;
254 }
255
256 io_uring_for_each_cqe(&ring, head, cqe) {
257 /* runs after test_io so should not have happened */
258 if (cqe->res == -EOPNOTSUPP) {
259 fprintf(stdout, "File/device/fs doesn't support polled IO\n");
260 goto err;
261 }
262 goto ok;
263 }
264 usleep(10000);
265 }
266 err:
267 ret = 1;
268 if (fd != -1)
269 close(fd);
270
271 ok:
272 io_uring_queue_exit(&ring);
273 return ret;
274 }
275
test_io(const char * file,int write,int sqthread,int fixed,int buf_select)276 static int test_io(const char *file, int write, int sqthread, int fixed,
277 int buf_select)
278 {
279 struct io_uring ring;
280 int ret, ring_flags = IORING_SETUP_IOPOLL;
281
282 if (no_iopoll)
283 return 0;
284
285 ret = t_create_ring(64, &ring, ring_flags);
286 if (ret == T_SETUP_SKIP)
287 return 0;
288 if (ret != T_SETUP_OK) {
289 fprintf(stderr, "ring create failed: %d\n", ret);
290 return 1;
291 }
292 ret = __test_io(file, &ring, write, sqthread, fixed, buf_select);
293 io_uring_queue_exit(&ring);
294 return ret;
295 }
296
probe_buf_select(void)297 static int probe_buf_select(void)
298 {
299 struct io_uring_probe *p;
300 struct io_uring ring;
301 int ret;
302
303 ret = io_uring_queue_init(1, &ring, 0);
304 if (ret) {
305 fprintf(stderr, "ring create failed: %d\n", ret);
306 return 1;
307 }
308
309 p = io_uring_get_probe_ring(&ring);
310 if (!p || !io_uring_opcode_supported(p, IORING_OP_PROVIDE_BUFFERS)) {
311 no_buf_select = 1;
312 fprintf(stdout, "Buffer select not supported, skipping\n");
313 return 0;
314 }
315 io_uring_free_probe(p);
316 return 0;
317 }
318
main(int argc,char * argv[])319 int main(int argc, char *argv[])
320 {
321 int i, ret, nr;
322 char buf[256];
323 char *fname;
324
325 if (probe_buf_select())
326 return 1;
327
328 if (argc > 1) {
329 fname = argv[1];
330 } else {
331 srand((unsigned)time(NULL));
332 snprintf(buf, sizeof(buf), ".basic-rw-%u-%u",
333 (unsigned)rand(), (unsigned)getpid());
334 fname = buf;
335 t_create_file(fname, FILE_SIZE);
336 }
337
338 vecs = t_create_buffers(BUFFERS, BS);
339
340 nr = 16;
341 if (no_buf_select)
342 nr = 8;
343 for (i = 0; i < nr; i++) {
344 int write = (i & 1) != 0;
345 int sqthread = (i & 2) != 0;
346 int fixed = (i & 4) != 0;
347 int buf_select = (i & 8) != 0;
348
349 ret = test_io(fname, write, sqthread, fixed, buf_select);
350 if (ret) {
351 fprintf(stderr, "test_io failed %d/%d/%d/%d\n",
352 write, sqthread, fixed, buf_select);
353 goto err;
354 }
355 if (no_iopoll)
356 break;
357 }
358
359 ret = test_io_uring_submit_enters(fname);
360 if (ret) {
361 fprintf(stderr, "test_io_uring_submit_enters failed\n");
362 goto err;
363 }
364
365 if (fname != argv[1])
366 unlink(fname);
367 return 0;
368 err:
369 if (fname != argv[1])
370 unlink(fname);
371 return 1;
372 }
373