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
|
/* SPDX-License-Identifier: MIT */
/*
* Description: test io_uring poll handling
*
*/
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <poll.h>
#include <sys/wait.h>
#include <assert.h>
#include "helpers.h"
#include "liburing.h"
static void do_setsockopt(int fd, int level, int optname, int val)
{
if (setsockopt(fd, level, optname, &val, sizeof(val)))
t_error(1, errno, "setsockopt %d.%d: %d", level, optname, val);
}
static bool check_cq_empty(struct io_uring *ring)
{
struct io_uring_cqe *cqe = NULL;
int ret;
ret = io_uring_peek_cqe(ring, &cqe); /* nothing should be there */
return ret == -EAGAIN;
}
static int test_basic(void)
{
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
struct io_uring ring;
int pipe1[2];
pid_t p;
int ret;
if (pipe(pipe1) != 0) {
perror("pipe");
return 1;
}
p = fork();
if (p == -1) {
perror("fork");
exit(2);
} else if (p == 0) {
ret = io_uring_queue_init(1, &ring, 0);
if (ret) {
fprintf(stderr, "child: ring setup failed: %d\n", ret);
return 1;
}
sqe = io_uring_get_sqe(&ring);
if (!sqe) {
fprintf(stderr, "get sqe failed\n");
return 1;
}
io_uring_prep_poll_add(sqe, pipe1[0], POLLIN);
io_uring_sqe_set_data(sqe, sqe);
ret = io_uring_submit(&ring);
if (ret <= 0) {
fprintf(stderr, "child: sqe submit failed: %d\n", ret);
return 1;
}
do {
ret = io_uring_wait_cqe(&ring, &cqe);
if (ret < 0) {
fprintf(stderr, "child: wait completion %d\n", ret);
break;
}
io_uring_cqe_seen(&ring, cqe);
} while (ret != 0);
if (ret < 0)
return 1;
if (cqe->user_data != (unsigned long) sqe) {
fprintf(stderr, "child: cqe doesn't match sqe\n");
return 1;
}
if ((cqe->res & POLLIN) != POLLIN) {
fprintf(stderr, "child: bad return value %ld\n",
(long) cqe->res);
return 1;
}
io_uring_queue_exit(&ring);
exit(0);
}
do {
errno = 0;
ret = write(pipe1[1], "foo", 3);
} while (ret == -1 && errno == EINTR);
if (ret != 3) {
fprintf(stderr, "parent: bad write return %d\n", ret);
return 1;
}
close(pipe1[0]);
close(pipe1[1]);
return 0;
}
static int test_missing_events(void)
{
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
struct io_uring ring;
int i, ret, sp[2];
char buf[2] = {};
int res_mask = 0;
ret = io_uring_queue_init(8, &ring, IORING_SETUP_SINGLE_ISSUER |
IORING_SETUP_DEFER_TASKRUN);
if (ret) {
fprintf(stderr, "ring setup failed: %d\n", ret);
return 1;
}
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) != 0) {
perror("Failed to create Unix-domain socket pair\n");
return 1;
}
do_setsockopt(sp[0], SOL_SOCKET, SO_SNDBUF, 1);
ret = send(sp[0], buf, sizeof(buf), 0);
if (ret != sizeof(buf)) {
perror("send failed\n");
return 1;
}
sqe = io_uring_get_sqe(&ring);
io_uring_prep_poll_multishot(sqe, sp[0], POLLIN|POLLOUT);
ret = io_uring_submit(&ring);
if (ret != 1) {
fprintf(stderr, "sqe submit failed: %d\n", ret);
return 1;
}
/* trigger EPOLLIN */
ret = send(sp[1], buf, sizeof(buf), 0);
if (ret != sizeof(buf)) {
fprintf(stderr, "send sp[1] failed %i %i\n", ret, errno);
return 1;
}
/* trigger EPOLLOUT */
ret = recv(sp[1], buf, sizeof(buf), 0);
if (ret != sizeof(buf)) {
perror("recv failed\n");
return 1;
}
for (i = 0; ; i++) {
if (i == 0)
ret = io_uring_wait_cqe(&ring, &cqe);
else
ret = io_uring_peek_cqe(&ring, &cqe);
if (i != 0 && ret == -EAGAIN) {
break;
}
if (ret) {
fprintf(stderr, "wait completion %d, %i\n", ret, i);
return 1;
}
res_mask |= cqe->res;
io_uring_cqe_seen(&ring, cqe);
}
if ((res_mask & (POLLIN|POLLOUT)) != (POLLIN|POLLOUT)) {
fprintf(stderr, "missing poll events %i\n", res_mask);
return 1;
}
io_uring_queue_exit(&ring);
close(sp[0]);
close(sp[1]);
return 0;
}
#define NR_SQES 2048
static int test_self_poll(void)
{
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
struct io_uring ring;
int ret, i, j;
ret = io_uring_queue_init(NR_SQES, &ring, 0);
if (ret) {
fprintf(stderr, "ring setup failed: %d\n", ret);
return T_EXIT_FAIL;
}
for (j = 0; j < 32; j++) {
for (i = 0; i < NR_SQES; i++) {
sqe = io_uring_get_sqe(&ring);
io_uring_prep_poll_add(sqe, ring.ring_fd, POLLIN);
}
ret = io_uring_submit(&ring);
assert(ret == NR_SQES);
}
sqe = io_uring_get_sqe(&ring);
io_uring_prep_nop(sqe);
ret = io_uring_submit(&ring);
assert(ret == 1);
ret = io_uring_wait_cqe(&ring, &cqe);
io_uring_cqe_seen(&ring, cqe);
io_uring_queue_exit(&ring);
return T_EXIT_PASS;
}
static int test_disabled_ring_lazy_polling(int early_poll)
{
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
struct io_uring ring, ring2;
unsigned head;
int ret, i = 0;
ret = io_uring_queue_init(8, &ring, IORING_SETUP_SINGLE_ISSUER |
IORING_SETUP_DEFER_TASKRUN |
IORING_SETUP_R_DISABLED);
if (ret) {
fprintf(stderr, "ring setup failed: %d\n", ret);
return 1;
}
ret = io_uring_queue_init(8, &ring2, 0);
if (ret) {
fprintf(stderr, "ring2 setup failed: %d\n", ret);
return 1;
}
if (early_poll) {
/* start polling disabled DEFER_TASKRUN ring */
sqe = io_uring_get_sqe(&ring2);
io_uring_prep_poll_add(sqe, ring.ring_fd, POLLIN);
ret = io_uring_submit(&ring2);
assert(ret == 1);
assert(check_cq_empty(&ring2));
}
/* enable rings, which should also activate pollwq */
ret = io_uring_enable_rings(&ring);
assert(ret >= 0);
if (!early_poll) {
/* start polling enabled DEFER_TASKRUN ring */
sqe = io_uring_get_sqe(&ring2);
io_uring_prep_poll_add(sqe, ring.ring_fd, POLLIN);
ret = io_uring_submit(&ring2);
assert(ret == 1);
assert(check_cq_empty(&ring2));
}
sqe = io_uring_get_sqe(&ring);
io_uring_prep_nop(sqe);
ret = io_uring_submit(&ring);
assert(ret == 1);
io_uring_for_each_cqe(&ring2, head, cqe) {
i++;
}
if (i != 1) {
fprintf(stderr, "fail, polling stuck\n");
return 1;
}
io_uring_queue_exit(&ring);
io_uring_queue_exit(&ring2);
return 0;
}
int main(int argc, char *argv[])
{
int ret;
if (argc > 1)
return 0;
ret = test_basic();
if (ret) {
fprintf(stderr, "test_basic() failed %i\n", ret);
return T_EXIT_FAIL;
}
if (t_probe_defer_taskrun()) {
ret = test_missing_events();
if (ret) {
fprintf(stderr, "test_missing_events() failed %i\n", ret);
return T_EXIT_FAIL;
}
ret = test_disabled_ring_lazy_polling(false);
if (ret) {
fprintf(stderr, "test_disabled_ring_lazy_polling(false) failed %i\n", ret);
return T_EXIT_FAIL;
}
ret = test_disabled_ring_lazy_polling(true);
if (ret) {
fprintf(stderr, "test_disabled_ring_lazy_polling(true) failed %i\n", ret);
return T_EXIT_FAIL;
}
}
ret = test_self_poll();
if (ret) {
fprintf(stderr, "test_self_poll failed\n");
return T_EXIT_FAIL;
}
return 0;
}
|