blob: 646e90487da45bfef23cf8e1f13726010cb8dfbf [file] [log] [blame]
Jens Axboee5024352020-02-11 20:34:12 -07001/* SPDX-License-Identifier: MIT */
Jens Axboef2ee0432019-10-17 17:21:25 -06002/*
3 * Check that IORING_OP_ACCEPT works, and send some data across to verify we
4 * didn't get a junk fd.
5 */
6#include <stdio.h>
7#include <stdlib.h>
8#include <stdint.h>
9#include <assert.h>
10
11#include <errno.h>
12#include <fcntl.h>
13#include <unistd.h>
14#include <sys/socket.h>
Jens Axboec1177122019-11-10 21:23:56 -070015#include <sys/time.h>
16#include <sys/resource.h>
Jens Axboef2ee0432019-10-17 17:21:25 -060017#include <sys/un.h>
18#include <netinet/tcp.h>
19#include <netinet/in.h>
20
Jens Axboea8f85362019-11-12 12:30:38 -070021#include "liburing.h"
Jens Axboef2ee0432019-10-17 17:21:25 -060022
Jens Axboea9bb08d2019-10-18 08:20:31 -060023static int no_accept;
24
Jens Axboea80dabe2019-12-24 10:25:45 -070025struct data {
26 char buf[128];
27 struct iovec iov;
28};
29
Jens Axboef2ee0432019-10-17 17:21:25 -060030static void queue_send(struct io_uring *ring, int fd)
31{
32 struct io_uring_sqe *sqe;
Jens Axboea80dabe2019-12-24 10:25:45 -070033 struct data *d;
Jens Axboef2ee0432019-10-17 17:21:25 -060034
Jens Axboea80dabe2019-12-24 10:25:45 -070035 d = malloc(sizeof(*d));
36 d->iov.iov_base = d->buf;
37 d->iov.iov_len = sizeof(d->buf);
Jens Axboef2ee0432019-10-17 17:21:25 -060038
39 sqe = io_uring_get_sqe(ring);
Jens Axboea80dabe2019-12-24 10:25:45 -070040 io_uring_prep_writev(sqe, fd, &d->iov, 1, 0);
Jens Axboef2ee0432019-10-17 17:21:25 -060041}
42
43static void queue_recv(struct io_uring *ring, int fd)
44{
45 struct io_uring_sqe *sqe;
Jens Axboea80dabe2019-12-24 10:25:45 -070046 struct data *d;
Jens Axboef2ee0432019-10-17 17:21:25 -060047
Jens Axboea80dabe2019-12-24 10:25:45 -070048 d = malloc(sizeof(*d));
49 d->iov.iov_base = d->buf;
50 d->iov.iov_len = sizeof(d->buf);
Jens Axboef2ee0432019-10-17 17:21:25 -060051
52 sqe = io_uring_get_sqe(ring);
Jens Axboea80dabe2019-12-24 10:25:45 -070053 io_uring_prep_readv(sqe, fd, &d->iov, 1, 0);
Jens Axboef2ee0432019-10-17 17:21:25 -060054}
55
56static int accept_conn(struct io_uring *ring, int fd)
57{
58 struct io_uring_sqe *sqe;
59 struct io_uring_cqe *cqe;
60 int ret;
61
62 sqe = io_uring_get_sqe(ring);
63 io_uring_prep_accept(sqe, fd, NULL, NULL, 0);
64
65 assert(io_uring_submit(ring) != -1);
66
67 assert(!io_uring_wait_cqe(ring, &cqe));
68 ret = cqe->res;
69 io_uring_cqe_seen(ring, cqe);
70 return ret;
71}
72
Jens Axboec1177122019-11-10 21:23:56 -070073static int start_accept_listen(struct sockaddr_in *addr, int port_off)
Jens Axboe7de62532019-11-10 10:09:36 -070074{
75 int fd;
76
77 fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
78
79 int32_t val = 1;
80 assert(setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val)) != -1);
81 assert(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) != -1);
82
83 struct sockaddr_in laddr;
84
85 if (!addr)
86 addr = &laddr;
87
88 addr->sin_family = AF_INET;
Jens Axboec1177122019-11-10 21:23:56 -070089 addr->sin_port = 0x1235 + port_off;
Jens Axboe7de62532019-11-10 10:09:36 -070090 addr->sin_addr.s_addr = 0x0100007fU;
91
92 assert(bind(fd, (struct sockaddr*)addr, sizeof(*addr)) != -1);
93 assert(listen(fd, 128) != -1);
94
95 return fd;
96}
97
Jens Axboe6b998552019-10-18 11:12:26 -060098static int test(struct io_uring *ring, int accept_should_error)
Jens Axboef2ee0432019-10-17 17:21:25 -060099{
Jens Axboef2ee0432019-10-17 17:21:25 -0600100 struct io_uring_cqe *cqe;
Jens Axboe7de62532019-11-10 10:09:36 -0700101 struct sockaddr_in addr;
Jens Axboef2ee0432019-10-17 17:21:25 -0600102 uint32_t head;
103 uint32_t count = 0;
104 int done = 0;
105 int p_fd[2];
106
Jens Axboec1177122019-11-10 21:23:56 -0700107 int32_t val, recv_s0 = start_accept_listen(&addr, 0);
Jens Axboef2ee0432019-10-17 17:21:25 -0600108
109 p_fd[1] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
110
111 val = 1;
112 assert(setsockopt(p_fd[1], IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) != -1);
113
114 int32_t flags = fcntl(p_fd[1], F_GETFL, 0);
115 assert(flags != -1);
116
117 flags |= O_NONBLOCK;
118 assert(fcntl(p_fd[1], F_SETFL, flags) != -1);
119
120 assert(connect(p_fd[1], (struct sockaddr*)&addr, sizeof(addr)) == -1);
121
122 flags = fcntl(p_fd[1], F_GETFL, 0);
123 assert(flags != -1);
124
125 flags &= ~O_NONBLOCK;
126 assert(fcntl(p_fd[1], F_SETFL, flags) != -1);
127
Jens Axboea9bb08d2019-10-18 08:20:31 -0600128 p_fd[0] = accept_conn(ring, recv_s0);
Jens Axboef2ee0432019-10-17 17:21:25 -0600129 if (p_fd[0] == -EINVAL) {
Jens Axboe6b998552019-10-18 11:12:26 -0600130 if (accept_should_error)
Jens Axboea9bb08d2019-10-18 08:20:31 -0600131 goto out;
Jens Axboef2ee0432019-10-17 17:21:25 -0600132 fprintf(stdout, "Accept not supported, skipping\n");
Jens Axboea9bb08d2019-10-18 08:20:31 -0600133 no_accept = 1;
Jens Axboef2ee0432019-10-17 17:21:25 -0600134 goto out;
Jens Axboe6b998552019-10-18 11:12:26 -0600135 } else if (p_fd[0] < 0) {
136 if (accept_should_error &&
137 (p_fd[0] == -EBADF || p_fd[0] == -EINVAL))
138 goto out;
139 fprintf(stderr, "Accept got %d\n", p_fd[0]);
140 goto err;
Jens Axboef2ee0432019-10-17 17:21:25 -0600141 }
Jens Axboef2ee0432019-10-17 17:21:25 -0600142
Jens Axboea9bb08d2019-10-18 08:20:31 -0600143 queue_send(ring, p_fd[1]);
144 queue_recv(ring, p_fd[0]);
Jens Axboef2ee0432019-10-17 17:21:25 -0600145
Jens Axboea9bb08d2019-10-18 08:20:31 -0600146 assert(io_uring_submit_and_wait(ring, 2) != -1);
Jens Axboef2ee0432019-10-17 17:21:25 -0600147
148 while (count < 2) {
Jens Axboea9bb08d2019-10-18 08:20:31 -0600149 io_uring_for_each_cqe(ring, head, cqe) {
Jens Axboef2ee0432019-10-17 17:21:25 -0600150 if (cqe->res < 0) {
151 fprintf(stderr, "Got cqe res %d\n", cqe->res);
152 done = 1;
153 break;
154 }
155 assert(cqe->res == 128);
156 count++;
157 }
158
159 assert(count <= 2);
Jens Axboea9bb08d2019-10-18 08:20:31 -0600160 io_uring_cq_advance(ring, count);
Jens Axboef2ee0432019-10-17 17:21:25 -0600161 if (done)
162 goto err;
163 }
164
165out:
Jens Axboea9bb08d2019-10-18 08:20:31 -0600166 close(p_fd[0]);
167 close(p_fd[1]);
Jens Axboef2ee0432019-10-17 17:21:25 -0600168 return 0;
169err:
Jens Axboea9bb08d2019-10-18 08:20:31 -0600170 close(p_fd[0]);
171 close(p_fd[1]);
Jens Axboef2ee0432019-10-17 17:21:25 -0600172 return 1;
173}
Jens Axboea9bb08d2019-10-18 08:20:31 -0600174
Jens Axboed974c9f2019-10-24 13:43:33 -0600175static void sig_alrm(int sig)
176{
177 exit(0);
178}
179
Jens Axboec6aa23e2019-11-10 09:49:47 -0700180static int test_accept_pending_on_exit(void)
181{
182 struct io_uring m_io_uring;
183 struct io_uring_cqe *cqe;
184 struct io_uring_sqe *sqe;
185 int fd;
186
187 assert(io_uring_queue_init(32, &m_io_uring, 0) >= 0);
188
Jens Axboec1177122019-11-10 21:23:56 -0700189 fd = start_accept_listen(NULL, 0);
Jens Axboec6aa23e2019-11-10 09:49:47 -0700190
Jens Axboed974c9f2019-10-24 13:43:33 -0600191 sqe = io_uring_get_sqe(&m_io_uring);
192 io_uring_prep_accept(sqe, fd, NULL, NULL, 0);
193 assert(io_uring_submit(&m_io_uring) != -1);
194
195 signal(SIGALRM, sig_alrm);
196 alarm(1);
197 assert(!io_uring_wait_cqe(&m_io_uring, &cqe));
198 io_uring_cqe_seen(&m_io_uring, cqe);
199
200 io_uring_queue_exit(&m_io_uring);
201 return 0;
202}
203
Jens Axboec1177122019-11-10 21:23:56 -0700204/*
205 * Test issue many accepts and see if we handle cancellation on exit
206 */
207static int test_accept_many(unsigned nr, unsigned usecs)
208{
209 struct io_uring m_io_uring;
210 struct io_uring_cqe *cqe;
211 struct io_uring_sqe *sqe;
212 unsigned long cur_lim;
213 struct rlimit rlim;
214 int *fds, i, ret = 0;
215
216 if (getrlimit(RLIMIT_NPROC, &rlim) < 0) {
217 perror("getrlimit");
218 return 1;
219 }
220
221 cur_lim = rlim.rlim_cur;
222 rlim.rlim_cur = nr / 4;
223
224 if (setrlimit(RLIMIT_NPROC, &rlim) < 0) {
225 perror("setrlimit");
226 return 1;
227 }
228
229 assert(io_uring_queue_init(2 * nr, &m_io_uring, 0) >= 0);
230
231 fds = calloc(nr, sizeof(int));
232
233 for (i = 0; i < nr; i++)
234 fds[i] = start_accept_listen(NULL, i);
235
236 for (i = 0; i < nr; i++) {
237 sqe = io_uring_get_sqe(&m_io_uring);
238 io_uring_prep_accept(sqe, fds[i], NULL, NULL, 0);
239 sqe->user_data = 1 + i;
240 assert(io_uring_submit(&m_io_uring) == 1);
241 }
242
243 if (usecs)
244 usleep(usecs);
245
246 for (i = 0; i < nr; i++) {
247 if (io_uring_peek_cqe(&m_io_uring, &cqe))
248 break;
249 if (cqe->res != -ECANCELED) {
250 fprintf(stderr, "Expected cqe to be cancelled\n");
251 goto err;
252 }
253 io_uring_cqe_seen(&m_io_uring, cqe);
254 }
255out:
256 rlim.rlim_cur = cur_lim;
257 if (setrlimit(RLIMIT_NPROC, &rlim) < 0) {
258 perror("setrlimit");
259 return 1;
260 }
261
262 free(fds);
263 io_uring_queue_exit(&m_io_uring);
264 return ret;
265err:
266 ret = 1;
267 goto out;
268}
269
Jens Axboec6aa23e2019-11-10 09:49:47 -0700270static int test_accept_cancel(unsigned usecs)
271{
272 struct io_uring m_io_uring;
273 struct io_uring_cqe *cqe;
274 struct io_uring_sqe *sqe;
275 int fd, i;
276
277 assert(io_uring_queue_init(32, &m_io_uring, 0) >= 0);
278
Jens Axboec1177122019-11-10 21:23:56 -0700279 fd = start_accept_listen(NULL, 0);
Jens Axboec6aa23e2019-11-10 09:49:47 -0700280
281 sqe = io_uring_get_sqe(&m_io_uring);
282 io_uring_prep_accept(sqe, fd, NULL, NULL, 0);
283 sqe->user_data = 1;
284 assert(io_uring_submit(&m_io_uring) == 1);
285
286 if (usecs)
287 usleep(usecs);
288
289 sqe = io_uring_get_sqe(&m_io_uring);
290 io_uring_prep_cancel(sqe, (void *) 1, 0);
291 sqe->user_data = 2;
292 assert(io_uring_submit(&m_io_uring) == 1);
293
294 for (i = 0; i < 2; i++) {
295 assert(!io_uring_wait_cqe(&m_io_uring, &cqe));
296 /*
297 * Two cases here:
298 *
299 * 1) We cancel the accept4() before it got started, we should
300 * get '0' for the cancel request and '-ECANCELED' for the
301 * accept request.
302 * 2) We cancel the accept4() after it's already running, we
303 * should get '-EALREADY' for the cancel request and
304 * '-EINTR' for the accept request.
305 */
306 if (cqe->user_data == 1) {
307 if (cqe->res != -EINTR && cqe->res != -ECANCELED) {
308 fprintf(stderr, "Cancelled accept got %d\n", cqe->res);
309 goto err;
310 }
311 } else if (cqe->user_data == 2) {
312 if (cqe->res != -EALREADY && cqe->res != 0) {
313 fprintf(stderr, "Cancel got %d\n", cqe->res);
314 goto err;
315 }
316 }
317 io_uring_cqe_seen(&m_io_uring, cqe);
318 }
319
320 io_uring_queue_exit(&m_io_uring);
321 return 0;
322err:
323 io_uring_queue_exit(&m_io_uring);
324 return 1;
325}
326
Jens Axboea9bb08d2019-10-18 08:20:31 -0600327static int test_accept(void)
328{
329 struct io_uring m_io_uring;
330 int ret;
331
332 assert(io_uring_queue_init(32, &m_io_uring, 0) >= 0);
333 ret = test(&m_io_uring, 0);
334 io_uring_queue_exit(&m_io_uring);
335 return ret;
336}
337
338static int test_accept_sqpoll(void)
339{
340 struct io_uring m_io_uring;
341 int ret;
342
Jens Axboe9fca8692019-11-10 09:48:47 -0700343 ret = io_uring_queue_init(32, &m_io_uring, IORING_SETUP_SQPOLL);
344 if (ret && geteuid()) {
345 printf("%s: skipped, not root\n", __FUNCTION__);
346 return 0;
347 } else if (ret)
348 return ret;
349
Jens Axboea9bb08d2019-10-18 08:20:31 -0600350 ret = test(&m_io_uring, 1);
351 io_uring_queue_exit(&m_io_uring);
352 return ret;
353}
354
355int main(int argc, char *argv[])
356{
357 int ret;
358
Jens Axboea2141fc2020-05-19 17:36:19 -0600359 if (argc > 1)
360 return 0;
361
Jens Axboea9bb08d2019-10-18 08:20:31 -0600362 ret = test_accept();
363 if (ret) {
364 fprintf(stderr, "test_accept failed\n");
365 return ret;
366 }
367 if (no_accept)
368 return 0;
369
370 ret = test_accept_sqpoll();
371 if (ret) {
372 fprintf(stderr, "test_accept_sqpoll failed\n");
373 return ret;
374 }
375
Jens Axboec6aa23e2019-11-10 09:49:47 -0700376 ret = test_accept_cancel(0);
Jens Axboed974c9f2019-10-24 13:43:33 -0600377 if (ret) {
Jens Axboec6aa23e2019-11-10 09:49:47 -0700378 fprintf(stderr, "test_accept_cancel nodelay failed\n");
379 return ret;
380 }
381
382 ret = test_accept_cancel(10000);
383 if (ret) {
384 fprintf(stderr, "test_accept_cancel delay failed\n");
385 return ret;
386 }
387
Jens Axboec1177122019-11-10 21:23:56 -0700388 ret = test_accept_many(128, 0);
389 if (ret) {
390 fprintf(stderr, "test_accept_many failed\n");
391 return ret;
392 }
393
394 ret = test_accept_many(128, 100000);
395 if (ret) {
396 fprintf(stderr, "test_accept_many failed\n");
397 return ret;
398 }
399
Jens Axboec6aa23e2019-11-10 09:49:47 -0700400 ret = test_accept_pending_on_exit();
401 if (ret) {
402 fprintf(stderr, "test_accept_pending_on_exit failed\n");
Jens Axboed974c9f2019-10-24 13:43:33 -0600403 return ret;
404 }
405
Jens Axboea9bb08d2019-10-18 08:20:31 -0600406 return 0;
407}