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