Jens Axboe | e502435 | 2020-02-11 20:34:12 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: MIT */ |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 2 | /* |
| 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 Axboe | c117712 | 2019-11-10 21:23:56 -0700 | [diff] [blame] | 15 | #include <sys/time.h> |
| 16 | #include <sys/resource.h> |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 17 | #include <sys/un.h> |
| 18 | #include <netinet/tcp.h> |
| 19 | #include <netinet/in.h> |
| 20 | |
Zhiqiang Liu | 9749981 | 2021-02-21 22:58:12 +0800 | [diff] [blame] | 21 | #include "helpers.h" |
Jens Axboe | a8f8536 | 2019-11-12 12:30:38 -0700 | [diff] [blame] | 22 | #include "liburing.h" |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 23 | |
Jens Axboe | a9bb08d | 2019-10-18 08:20:31 -0600 | [diff] [blame] | 24 | static int no_accept; |
| 25 | |
Jens Axboe | a80dabe | 2019-12-24 10:25:45 -0700 | [diff] [blame] | 26 | struct data { |
| 27 | char buf[128]; |
| 28 | struct iovec iov; |
| 29 | }; |
| 30 | |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 31 | static void queue_send(struct io_uring *ring, int fd) |
| 32 | { |
| 33 | struct io_uring_sqe *sqe; |
Jens Axboe | a80dabe | 2019-12-24 10:25:45 -0700 | [diff] [blame] | 34 | struct data *d; |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 35 | |
Zhiqiang Liu | 9749981 | 2021-02-21 22:58:12 +0800 | [diff] [blame] | 36 | d = io_uring_malloc(sizeof(*d)); |
Jens Axboe | a80dabe | 2019-12-24 10:25:45 -0700 | [diff] [blame] | 37 | d->iov.iov_base = d->buf; |
| 38 | d->iov.iov_len = sizeof(d->buf); |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 39 | |
| 40 | sqe = io_uring_get_sqe(ring); |
Jens Axboe | a80dabe | 2019-12-24 10:25:45 -0700 | [diff] [blame] | 41 | io_uring_prep_writev(sqe, fd, &d->iov, 1, 0); |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static void queue_recv(struct io_uring *ring, int fd) |
| 45 | { |
| 46 | struct io_uring_sqe *sqe; |
Jens Axboe | a80dabe | 2019-12-24 10:25:45 -0700 | [diff] [blame] | 47 | struct data *d; |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 48 | |
Zhiqiang Liu | 9749981 | 2021-02-21 22:58:12 +0800 | [diff] [blame] | 49 | d = io_uring_malloc(sizeof(*d)); |
Jens Axboe | a80dabe | 2019-12-24 10:25:45 -0700 | [diff] [blame] | 50 | d->iov.iov_base = d->buf; |
| 51 | d->iov.iov_len = sizeof(d->buf); |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 52 | |
| 53 | sqe = io_uring_get_sqe(ring); |
Jens Axboe | a80dabe | 2019-12-24 10:25:45 -0700 | [diff] [blame] | 54 | io_uring_prep_readv(sqe, fd, &d->iov, 1, 0); |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static int accept_conn(struct io_uring *ring, int fd) |
| 58 | { |
| 59 | struct io_uring_sqe *sqe; |
| 60 | struct io_uring_cqe *cqe; |
| 61 | int ret; |
| 62 | |
| 63 | sqe = io_uring_get_sqe(ring); |
| 64 | io_uring_prep_accept(sqe, fd, NULL, NULL, 0); |
| 65 | |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 66 | ret = io_uring_submit(ring); |
| 67 | assert(ret != -1); |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 68 | |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 69 | ret = io_uring_wait_cqe(ring, &cqe); |
| 70 | assert(!ret); |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 71 | ret = cqe->res; |
| 72 | io_uring_cqe_seen(ring, cqe); |
| 73 | return ret; |
| 74 | } |
| 75 | |
Jens Axboe | c117712 | 2019-11-10 21:23:56 -0700 | [diff] [blame] | 76 | static int start_accept_listen(struct sockaddr_in *addr, int port_off) |
Jens Axboe | 7de6253 | 2019-11-10 10:09:36 -0700 | [diff] [blame] | 77 | { |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 78 | int fd, ret; |
Jens Axboe | 7de6253 | 2019-11-10 10:09:36 -0700 | [diff] [blame] | 79 | |
| 80 | fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP); |
| 81 | |
| 82 | int32_t val = 1; |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 83 | ret = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val)); |
| 84 | assert(ret != -1); |
| 85 | ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); |
| 86 | assert(ret != -1); |
Jens Axboe | 7de6253 | 2019-11-10 10:09:36 -0700 | [diff] [blame] | 87 | |
| 88 | struct sockaddr_in laddr; |
| 89 | |
| 90 | if (!addr) |
| 91 | addr = &laddr; |
| 92 | |
| 93 | addr->sin_family = AF_INET; |
Jens Axboe | c117712 | 2019-11-10 21:23:56 -0700 | [diff] [blame] | 94 | addr->sin_port = 0x1235 + port_off; |
Jens Axboe | 7de6253 | 2019-11-10 10:09:36 -0700 | [diff] [blame] | 95 | addr->sin_addr.s_addr = 0x0100007fU; |
| 96 | |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 97 | ret = bind(fd, (struct sockaddr*)addr, sizeof(*addr)); |
| 98 | assert(ret != -1); |
| 99 | ret = listen(fd, 128); |
| 100 | assert(ret != -1); |
Jens Axboe | 7de6253 | 2019-11-10 10:09:36 -0700 | [diff] [blame] | 101 | |
| 102 | return fd; |
| 103 | } |
| 104 | |
Jens Axboe | 6b99855 | 2019-10-18 11:12:26 -0600 | [diff] [blame] | 105 | static int test(struct io_uring *ring, int accept_should_error) |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 106 | { |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 107 | struct io_uring_cqe *cqe; |
Jens Axboe | 7de6253 | 2019-11-10 10:09:36 -0700 | [diff] [blame] | 108 | struct sockaddr_in addr; |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 109 | uint32_t head; |
| 110 | uint32_t count = 0; |
| 111 | int done = 0; |
| 112 | int p_fd[2]; |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 113 | int ret; |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 114 | |
Jens Axboe | c117712 | 2019-11-10 21:23:56 -0700 | [diff] [blame] | 115 | int32_t val, recv_s0 = start_accept_listen(&addr, 0); |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 116 | |
| 117 | p_fd[1] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP); |
| 118 | |
| 119 | val = 1; |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 120 | ret = setsockopt(p_fd[1], IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)); |
| 121 | assert(ret != -1); |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 122 | |
| 123 | int32_t flags = fcntl(p_fd[1], F_GETFL, 0); |
| 124 | assert(flags != -1); |
| 125 | |
| 126 | flags |= O_NONBLOCK; |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 127 | ret = fcntl(p_fd[1], F_SETFL, flags); |
| 128 | assert(ret != -1); |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 129 | |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 130 | ret = connect(p_fd[1], (struct sockaddr*)&addr, sizeof(addr)); |
| 131 | assert(ret == -1); |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 132 | |
| 133 | flags = fcntl(p_fd[1], F_GETFL, 0); |
| 134 | assert(flags != -1); |
| 135 | |
| 136 | flags &= ~O_NONBLOCK; |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 137 | ret = fcntl(p_fd[1], F_SETFL, flags); |
| 138 | assert(ret != -1); |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 139 | |
Jens Axboe | a9bb08d | 2019-10-18 08:20:31 -0600 | [diff] [blame] | 140 | p_fd[0] = accept_conn(ring, recv_s0); |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 141 | if (p_fd[0] == -EINVAL) { |
Jens Axboe | 6b99855 | 2019-10-18 11:12:26 -0600 | [diff] [blame] | 142 | if (accept_should_error) |
Jens Axboe | a9bb08d | 2019-10-18 08:20:31 -0600 | [diff] [blame] | 143 | goto out; |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 144 | fprintf(stdout, "Accept not supported, skipping\n"); |
Jens Axboe | a9bb08d | 2019-10-18 08:20:31 -0600 | [diff] [blame] | 145 | no_accept = 1; |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 146 | goto out; |
Jens Axboe | 6b99855 | 2019-10-18 11:12:26 -0600 | [diff] [blame] | 147 | } else if (p_fd[0] < 0) { |
| 148 | if (accept_should_error && |
| 149 | (p_fd[0] == -EBADF || p_fd[0] == -EINVAL)) |
| 150 | goto out; |
| 151 | fprintf(stderr, "Accept got %d\n", p_fd[0]); |
| 152 | goto err; |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 153 | } |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 154 | |
Jens Axboe | a9bb08d | 2019-10-18 08:20:31 -0600 | [diff] [blame] | 155 | queue_send(ring, p_fd[1]); |
| 156 | queue_recv(ring, p_fd[0]); |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 157 | |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 158 | ret = io_uring_submit_and_wait(ring, 2); |
| 159 | assert(ret != -1); |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 160 | |
| 161 | while (count < 2) { |
Jens Axboe | a9bb08d | 2019-10-18 08:20:31 -0600 | [diff] [blame] | 162 | io_uring_for_each_cqe(ring, head, cqe) { |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 163 | if (cqe->res < 0) { |
| 164 | fprintf(stderr, "Got cqe res %d\n", cqe->res); |
| 165 | done = 1; |
| 166 | break; |
| 167 | } |
| 168 | assert(cqe->res == 128); |
| 169 | count++; |
| 170 | } |
| 171 | |
| 172 | assert(count <= 2); |
Jens Axboe | a9bb08d | 2019-10-18 08:20:31 -0600 | [diff] [blame] | 173 | io_uring_cq_advance(ring, count); |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 174 | if (done) |
| 175 | goto err; |
| 176 | } |
| 177 | |
| 178 | out: |
Jens Axboe | a9bb08d | 2019-10-18 08:20:31 -0600 | [diff] [blame] | 179 | close(p_fd[0]); |
| 180 | close(p_fd[1]); |
Jens Axboe | cfddbbf | 2020-09-05 13:30:28 -0600 | [diff] [blame] | 181 | close(recv_s0); |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 182 | return 0; |
| 183 | err: |
Jens Axboe | a9bb08d | 2019-10-18 08:20:31 -0600 | [diff] [blame] | 184 | close(p_fd[0]); |
| 185 | close(p_fd[1]); |
Jens Axboe | cfddbbf | 2020-09-05 13:30:28 -0600 | [diff] [blame] | 186 | close(recv_s0); |
Jens Axboe | f2ee043 | 2019-10-17 17:21:25 -0600 | [diff] [blame] | 187 | return 1; |
| 188 | } |
Jens Axboe | a9bb08d | 2019-10-18 08:20:31 -0600 | [diff] [blame] | 189 | |
Jens Axboe | d974c9f | 2019-10-24 13:43:33 -0600 | [diff] [blame] | 190 | static void sig_alrm(int sig) |
| 191 | { |
| 192 | exit(0); |
| 193 | } |
| 194 | |
Jens Axboe | c6aa23e | 2019-11-10 09:49:47 -0700 | [diff] [blame] | 195 | static int test_accept_pending_on_exit(void) |
| 196 | { |
| 197 | struct io_uring m_io_uring; |
| 198 | struct io_uring_cqe *cqe; |
| 199 | struct io_uring_sqe *sqe; |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 200 | int fd, ret; |
Jens Axboe | c6aa23e | 2019-11-10 09:49:47 -0700 | [diff] [blame] | 201 | |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 202 | ret = io_uring_queue_init(32, &m_io_uring, 0); |
| 203 | assert(ret >= 0); |
Jens Axboe | c6aa23e | 2019-11-10 09:49:47 -0700 | [diff] [blame] | 204 | |
Jens Axboe | c117712 | 2019-11-10 21:23:56 -0700 | [diff] [blame] | 205 | fd = start_accept_listen(NULL, 0); |
Jens Axboe | c6aa23e | 2019-11-10 09:49:47 -0700 | [diff] [blame] | 206 | |
Jens Axboe | d974c9f | 2019-10-24 13:43:33 -0600 | [diff] [blame] | 207 | sqe = io_uring_get_sqe(&m_io_uring); |
| 208 | io_uring_prep_accept(sqe, fd, NULL, NULL, 0); |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 209 | ret = io_uring_submit(&m_io_uring); |
| 210 | assert(ret != -1); |
Jens Axboe | d974c9f | 2019-10-24 13:43:33 -0600 | [diff] [blame] | 211 | |
| 212 | signal(SIGALRM, sig_alrm); |
| 213 | alarm(1); |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 214 | ret = io_uring_wait_cqe(&m_io_uring, &cqe); |
| 215 | assert(!ret); |
Jens Axboe | d974c9f | 2019-10-24 13:43:33 -0600 | [diff] [blame] | 216 | io_uring_cqe_seen(&m_io_uring, cqe); |
| 217 | |
| 218 | io_uring_queue_exit(&m_io_uring); |
| 219 | return 0; |
| 220 | } |
| 221 | |
Jens Axboe | c117712 | 2019-11-10 21:23:56 -0700 | [diff] [blame] | 222 | /* |
| 223 | * Test issue many accepts and see if we handle cancellation on exit |
| 224 | */ |
| 225 | static int test_accept_many(unsigned nr, unsigned usecs) |
| 226 | { |
| 227 | struct io_uring m_io_uring; |
| 228 | struct io_uring_cqe *cqe; |
| 229 | struct io_uring_sqe *sqe; |
| 230 | unsigned long cur_lim; |
| 231 | struct rlimit rlim; |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 232 | int *fds, i, ret; |
Jens Axboe | c117712 | 2019-11-10 21:23:56 -0700 | [diff] [blame] | 233 | |
| 234 | if (getrlimit(RLIMIT_NPROC, &rlim) < 0) { |
| 235 | perror("getrlimit"); |
| 236 | return 1; |
| 237 | } |
| 238 | |
| 239 | cur_lim = rlim.rlim_cur; |
| 240 | rlim.rlim_cur = nr / 4; |
| 241 | |
| 242 | if (setrlimit(RLIMIT_NPROC, &rlim) < 0) { |
| 243 | perror("setrlimit"); |
| 244 | return 1; |
| 245 | } |
| 246 | |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 247 | ret = io_uring_queue_init(2 * nr, &m_io_uring, 0); |
| 248 | assert(ret >= 0); |
Jens Axboe | c117712 | 2019-11-10 21:23:56 -0700 | [diff] [blame] | 249 | |
Zhiqiang Liu | b7b089a | 2021-02-23 14:43:57 +0800 | [diff] [blame^] | 250 | fds = io_uring_calloc(nr, sizeof(int)); |
Jens Axboe | c117712 | 2019-11-10 21:23:56 -0700 | [diff] [blame] | 251 | |
| 252 | for (i = 0; i < nr; i++) |
| 253 | fds[i] = start_accept_listen(NULL, i); |
| 254 | |
| 255 | for (i = 0; i < nr; i++) { |
| 256 | sqe = io_uring_get_sqe(&m_io_uring); |
| 257 | io_uring_prep_accept(sqe, fds[i], NULL, NULL, 0); |
| 258 | sqe->user_data = 1 + i; |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 259 | ret = io_uring_submit(&m_io_uring); |
| 260 | assert(ret == 1); |
Jens Axboe | c117712 | 2019-11-10 21:23:56 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | if (usecs) |
| 264 | usleep(usecs); |
| 265 | |
| 266 | for (i = 0; i < nr; i++) { |
| 267 | if (io_uring_peek_cqe(&m_io_uring, &cqe)) |
| 268 | break; |
| 269 | if (cqe->res != -ECANCELED) { |
| 270 | fprintf(stderr, "Expected cqe to be cancelled\n"); |
| 271 | goto err; |
| 272 | } |
| 273 | io_uring_cqe_seen(&m_io_uring, cqe); |
| 274 | } |
| 275 | out: |
| 276 | rlim.rlim_cur = cur_lim; |
| 277 | if (setrlimit(RLIMIT_NPROC, &rlim) < 0) { |
| 278 | perror("setrlimit"); |
| 279 | return 1; |
| 280 | } |
| 281 | |
| 282 | free(fds); |
| 283 | io_uring_queue_exit(&m_io_uring); |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 284 | return 0; |
Jens Axboe | c117712 | 2019-11-10 21:23:56 -0700 | [diff] [blame] | 285 | err: |
| 286 | ret = 1; |
| 287 | goto out; |
| 288 | } |
| 289 | |
Jens Axboe | c6aa23e | 2019-11-10 09:49:47 -0700 | [diff] [blame] | 290 | static int test_accept_cancel(unsigned usecs) |
| 291 | { |
| 292 | struct io_uring m_io_uring; |
| 293 | struct io_uring_cqe *cqe; |
| 294 | struct io_uring_sqe *sqe; |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 295 | int fd, i, ret; |
Jens Axboe | c6aa23e | 2019-11-10 09:49:47 -0700 | [diff] [blame] | 296 | |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 297 | ret = io_uring_queue_init(32, &m_io_uring, 0); |
| 298 | assert(ret >= 0); |
Jens Axboe | c6aa23e | 2019-11-10 09:49:47 -0700 | [diff] [blame] | 299 | |
Jens Axboe | c117712 | 2019-11-10 21:23:56 -0700 | [diff] [blame] | 300 | fd = start_accept_listen(NULL, 0); |
Jens Axboe | c6aa23e | 2019-11-10 09:49:47 -0700 | [diff] [blame] | 301 | |
| 302 | sqe = io_uring_get_sqe(&m_io_uring); |
| 303 | io_uring_prep_accept(sqe, fd, NULL, NULL, 0); |
| 304 | sqe->user_data = 1; |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 305 | ret = io_uring_submit(&m_io_uring); |
| 306 | assert(ret == 1); |
Jens Axboe | c6aa23e | 2019-11-10 09:49:47 -0700 | [diff] [blame] | 307 | |
| 308 | if (usecs) |
| 309 | usleep(usecs); |
| 310 | |
| 311 | sqe = io_uring_get_sqe(&m_io_uring); |
| 312 | io_uring_prep_cancel(sqe, (void *) 1, 0); |
| 313 | sqe->user_data = 2; |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 314 | ret = io_uring_submit(&m_io_uring); |
| 315 | assert(ret == 1); |
Jens Axboe | c6aa23e | 2019-11-10 09:49:47 -0700 | [diff] [blame] | 316 | |
| 317 | for (i = 0; i < 2; i++) { |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 318 | ret = io_uring_wait_cqe(&m_io_uring, &cqe); |
| 319 | assert(!ret); |
Jens Axboe | c6aa23e | 2019-11-10 09:49:47 -0700 | [diff] [blame] | 320 | /* |
| 321 | * Two cases here: |
| 322 | * |
| 323 | * 1) We cancel the accept4() before it got started, we should |
| 324 | * get '0' for the cancel request and '-ECANCELED' for the |
| 325 | * accept request. |
| 326 | * 2) We cancel the accept4() after it's already running, we |
| 327 | * should get '-EALREADY' for the cancel request and |
| 328 | * '-EINTR' for the accept request. |
| 329 | */ |
| 330 | if (cqe->user_data == 1) { |
| 331 | if (cqe->res != -EINTR && cqe->res != -ECANCELED) { |
| 332 | fprintf(stderr, "Cancelled accept got %d\n", cqe->res); |
| 333 | goto err; |
| 334 | } |
| 335 | } else if (cqe->user_data == 2) { |
| 336 | if (cqe->res != -EALREADY && cqe->res != 0) { |
| 337 | fprintf(stderr, "Cancel got %d\n", cqe->res); |
| 338 | goto err; |
| 339 | } |
| 340 | } |
| 341 | io_uring_cqe_seen(&m_io_uring, cqe); |
| 342 | } |
| 343 | |
| 344 | io_uring_queue_exit(&m_io_uring); |
| 345 | return 0; |
| 346 | err: |
| 347 | io_uring_queue_exit(&m_io_uring); |
| 348 | return 1; |
| 349 | } |
| 350 | |
Jens Axboe | a9bb08d | 2019-10-18 08:20:31 -0600 | [diff] [blame] | 351 | static int test_accept(void) |
| 352 | { |
| 353 | struct io_uring m_io_uring; |
| 354 | int ret; |
| 355 | |
Ryan Sharpelletti | e3adbfc | 2020-10-27 21:04:11 +0000 | [diff] [blame] | 356 | ret = io_uring_queue_init(32, &m_io_uring, 0); |
| 357 | assert(ret >= 0); |
Jens Axboe | a9bb08d | 2019-10-18 08:20:31 -0600 | [diff] [blame] | 358 | ret = test(&m_io_uring, 0); |
| 359 | io_uring_queue_exit(&m_io_uring); |
| 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | static int test_accept_sqpoll(void) |
| 364 | { |
| 365 | struct io_uring m_io_uring; |
Jens Axboe | 27b4479 | 2020-09-05 12:00:09 -0600 | [diff] [blame] | 366 | struct io_uring_params p = { }; |
| 367 | int ret, should_fail; |
Jens Axboe | a9bb08d | 2019-10-18 08:20:31 -0600 | [diff] [blame] | 368 | |
Jens Axboe | 27b4479 | 2020-09-05 12:00:09 -0600 | [diff] [blame] | 369 | p.flags = IORING_SETUP_SQPOLL; |
| 370 | ret = io_uring_queue_init_params(32, &m_io_uring, &p); |
Jens Axboe | 9fca869 | 2019-11-10 09:48:47 -0700 | [diff] [blame] | 371 | if (ret && geteuid()) { |
| 372 | printf("%s: skipped, not root\n", __FUNCTION__); |
| 373 | return 0; |
| 374 | } else if (ret) |
| 375 | return ret; |
| 376 | |
Jens Axboe | 27b4479 | 2020-09-05 12:00:09 -0600 | [diff] [blame] | 377 | should_fail = 1; |
| 378 | if (p.features & IORING_FEAT_SQPOLL_NONFIXED) |
| 379 | should_fail = 0; |
| 380 | |
| 381 | ret = test(&m_io_uring, should_fail); |
Jens Axboe | a9bb08d | 2019-10-18 08:20:31 -0600 | [diff] [blame] | 382 | io_uring_queue_exit(&m_io_uring); |
| 383 | return ret; |
| 384 | } |
| 385 | |
| 386 | int main(int argc, char *argv[]) |
| 387 | { |
| 388 | int ret; |
| 389 | |
Jens Axboe | a2141fc | 2020-05-19 17:36:19 -0600 | [diff] [blame] | 390 | if (argc > 1) |
| 391 | return 0; |
| 392 | |
Jens Axboe | a9bb08d | 2019-10-18 08:20:31 -0600 | [diff] [blame] | 393 | ret = test_accept(); |
| 394 | if (ret) { |
| 395 | fprintf(stderr, "test_accept failed\n"); |
| 396 | return ret; |
| 397 | } |
| 398 | if (no_accept) |
| 399 | return 0; |
| 400 | |
| 401 | ret = test_accept_sqpoll(); |
| 402 | if (ret) { |
| 403 | fprintf(stderr, "test_accept_sqpoll failed\n"); |
| 404 | return ret; |
| 405 | } |
| 406 | |
Jens Axboe | c6aa23e | 2019-11-10 09:49:47 -0700 | [diff] [blame] | 407 | ret = test_accept_cancel(0); |
Jens Axboe | d974c9f | 2019-10-24 13:43:33 -0600 | [diff] [blame] | 408 | if (ret) { |
Jens Axboe | c6aa23e | 2019-11-10 09:49:47 -0700 | [diff] [blame] | 409 | fprintf(stderr, "test_accept_cancel nodelay failed\n"); |
| 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | ret = test_accept_cancel(10000); |
| 414 | if (ret) { |
| 415 | fprintf(stderr, "test_accept_cancel delay failed\n"); |
| 416 | return ret; |
| 417 | } |
| 418 | |
Jens Axboe | c117712 | 2019-11-10 21:23:56 -0700 | [diff] [blame] | 419 | ret = test_accept_many(128, 0); |
| 420 | if (ret) { |
| 421 | fprintf(stderr, "test_accept_many failed\n"); |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | ret = test_accept_many(128, 100000); |
| 426 | if (ret) { |
| 427 | fprintf(stderr, "test_accept_many failed\n"); |
| 428 | return ret; |
| 429 | } |
| 430 | |
Jens Axboe | c6aa23e | 2019-11-10 09:49:47 -0700 | [diff] [blame] | 431 | ret = test_accept_pending_on_exit(); |
| 432 | if (ret) { |
| 433 | fprintf(stderr, "test_accept_pending_on_exit failed\n"); |
Jens Axboe | d974c9f | 2019-10-24 13:43:33 -0600 | [diff] [blame] | 434 | return ret; |
| 435 | } |
| 436 | |
Jens Axboe | a9bb08d | 2019-10-18 08:20:31 -0600 | [diff] [blame] | 437 | return 0; |
| 438 | } |