commit d07d17adab5b918bd0543ce78f75a4747a057379 Author: Jens Axboe Date: Wed Mar 9 19:31:57 2022 -0700 test/wakeup-ring: add test cases for IORING_OP_WAKEUP_RING Signed-off-by: Jens Axboe diff --git a/.gitignore b/.gitignore index c9dc77fbe162..961fd9e96dc7 100644 --- a/.gitignore +++ b/.gitignore @@ -128,6 +128,7 @@ /test/timeout-overflow /test/unlink /test/wakeup-hang +/test/wakeup-ring /test/multicqes_drain /test/poll-mshot-update /test/rsrc_tags diff --git a/src/include/liburing/io_uring.h b/src/include/liburing/io_uring.h index a7d193d0df38..8f919b42a8ea 100644 --- a/src/include/liburing/io_uring.h +++ b/src/include/liburing/io_uring.h @@ -147,6 +147,7 @@ enum { IORING_OP_MKDIRAT, IORING_OP_SYMLINKAT, IORING_OP_LINKAT, + IORING_OP_WAKEUP_RING, /* this goes last, obviously */ IORING_OP_LAST, diff --git a/test/Makefile b/test/Makefile index f421f536df87..4aafbae826ca 100644 --- a/test/Makefile +++ b/test/Makefile @@ -151,6 +151,7 @@ test_srcs := \ timeout-overflow.c \ unlink.c \ wakeup-hang.c \ + wakeup-ring.c \ skip-cqe.c \ # EOL @@ -221,6 +222,7 @@ ring-leak2: override LDFLAGS += -lpthread poll-mshot-update: override LDFLAGS += -lpthread exit-no-cleanup: override LDFLAGS += -lpthread pollfree: override LDFLAGS += -lpthread +wakeup-ring: override LDFLAGS += -lpthread install: $(test_targets) runtests.sh runtests-loop.sh $(INSTALL) -D -d -m 755 $(datadir)/liburing-test/ diff --git a/test/wakeup-ring.c b/test/wakeup-ring.c new file mode 100644 index 000000000000..0ccae92d4c93 --- /dev/null +++ b/test/wakeup-ring.c @@ -0,0 +1,172 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Description: test ring wakeup command + * + */ +#include +#include +#include +#include +#include +#include +#include + +#include "liburing.h" + +static int no_wakeup; + +static int test_own(struct io_uring *ring) +{ + struct io_uring_cqe *cqe; + struct io_uring_sqe *sqe; + int ret, i; + + sqe = io_uring_get_sqe(ring); + if (!sqe) { + fprintf(stderr, "get sqe failed\n"); + goto err; + } + + sqe->opcode = IORING_OP_WAKEUP_RING; + sqe->fd = ring->ring_fd; + sqe->user_data = 1; + + ret = io_uring_submit(ring); + if (ret <= 0) { + fprintf(stderr, "sqe submit failed: %d\n", ret); + goto err; + } + + for (i = 0; i < 2; i++) { + ret = io_uring_peek_cqe(ring, &cqe); + if (ret < 0) { + fprintf(stderr, "wait completion %d\n", ret); + goto err; + } + switch (cqe->user_data) { + case 1: + if (cqe->res == -EINVAL || cqe->res == -EOPNOTSUPP) { + no_wakeup = 1; + return 0; + } + if (cqe->res != 0) { + fprintf(stderr, "wakeup res %d\n", cqe->res); + return -1; + } + break; + case 0: + if (!(cqe->flags & (1U << 2))) { + fprintf(stderr, "invalid flags %x\n", cqe->flags); + return -1; + } + break; + } + io_uring_cqe_seen(ring, cqe); + } + + return 0; +err: + return 1; +} + +static void *wait_cqe_fn(void *data) +{ + struct io_uring *ring = data; + struct io_uring_cqe *cqe; + int ret; + + ret = io_uring_wait_cqe(ring, &cqe); + if (ret) { + fprintf(stderr, "wait cqe %d\n", ret); + goto err; + } + + if (!(cqe->flags & (1U << 2))) { + fprintf(stderr, "invalid flags %x\n", cqe->flags); + goto err; + } + + return NULL; +err: + return (void *) (unsigned long) 1; +} + +static int test_remote(struct io_uring *ring, struct io_uring *target) +{ + struct io_uring_cqe *cqe; + struct io_uring_sqe *sqe; + int ret; + + sqe = io_uring_get_sqe(ring); + if (!sqe) { + fprintf(stderr, "get sqe failed\n"); + goto err; + } + + sqe->opcode = IORING_OP_WAKEUP_RING; + sqe->fd = target->ring_fd; + sqe->user_data = 1; + + ret = io_uring_submit(ring); + if (ret <= 0) { + fprintf(stderr, "sqe submit failed: %d\n", ret); + goto err; + } + + ret = io_uring_peek_cqe(ring, &cqe); + if (ret < 0) { + fprintf(stderr, "wait completion %d\n", ret); + goto err; + } + if (cqe->res != 0) { + fprintf(stderr, "wakeup res %d\n", cqe->res); + return -1; + } + + io_uring_cqe_seen(ring, cqe); + return 0; +err: + return 1; +} + +int main(int argc, char *argv[]) +{ + struct io_uring ring, ring2; + pthread_t thread; + void *tret; + int ret; + + if (argc > 1) + return 0; + + ret = io_uring_queue_init(8, &ring, 0); + if (ret) { + fprintf(stderr, "ring setup failed: %d\n", ret); + return 1; + } + ret = io_uring_queue_init(8, &ring2, 0); + if (ret) { + fprintf(stderr, "ring setup failed: %d\n", ret); + return 1; + } + + pthread_create(&thread, NULL, wait_cqe_fn, &ring2); + + ret = test_own(&ring); + if (ret) { + fprintf(stderr, "test_own failed\n"); + return ret; + } + if (no_wakeup) + return 0; + + ret = test_remote(&ring, &ring2); + if (ret) { + fprintf(stderr, "test_remote failed\n"); + return ret; + } + + pthread_join(thread, &tret); + + return 0; +}