summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJens Axboe <axboe@kernel.dk>2019-11-05 14:02:23 -0700
committerJens Axboe <axboe@kernel.dk>2019-11-05 14:02:23 -0700
commitbc1bd5e97e2c758d6fd975bd35843b9b2c770c5a (patch)
tree9359e3bd18a187d278646935b54119568b7bc68a
parentba97eaf8f9ef2030d581be10df9d875dbfadc91d (diff)
downloadliburing-link-timeout.tar.gz
liburing-link-timeout.tar.bz2
Add IORING_OP_LINK_TIMEOUT test caseslink-timeout
Tries to cover a wide variety of things that could go wrong. Signed-off-by: Jens Axboe <axboe@kernel.dk>
-rw-r--r--src/include/liburing.h8
-rw-r--r--src/include/liburing/io_uring.h1
-rw-r--r--test/Makefile5
-rw-r--r--test/link-timeout.c435
4 files changed, 447 insertions, 2 deletions
diff --git a/src/include/liburing.h b/src/include/liburing.h
index e8b6adb..b110b11 100644
--- a/src/include/liburing.h
+++ b/src/include/liburing.h
@@ -285,6 +285,14 @@ static inline void io_uring_prep_cancel(struct io_uring_sqe *sqe, void *user_dat
sqe->cancel_flags = flags;
}
+static inline void io_uring_prep_link_timeout(struct io_uring_sqe *sqe,
+ struct __kernel_timespec *ts,
+ unsigned flags)
+{
+ io_uring_prep_rw(IORING_OP_LINK_TIMEOUT, sqe, 0, ts, 1, 0);
+ sqe->timeout_flags = flags;
+}
+
static inline unsigned io_uring_sq_space_left(struct io_uring *ring)
{
return *ring->sq.kring_entries - (ring->sq.sqe_tail - ring->sq.sqe_head);
diff --git a/src/include/liburing/io_uring.h b/src/include/liburing/io_uring.h
index 6877cf8..f1a118b 100644
--- a/src/include/liburing/io_uring.h
+++ b/src/include/liburing/io_uring.h
@@ -72,6 +72,7 @@ struct io_uring_sqe {
#define IORING_OP_TIMEOUT_REMOVE 12
#define IORING_OP_ACCEPT 13
#define IORING_OP_ASYNC_CANCEL 14
+#define IORING_OP_LINK_TIMEOUT 15
/*
* sqe->fsync_flags
diff --git a/test/Makefile b/test/Makefile
index 25cf4d9..345e663 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -8,7 +8,8 @@ all_targets += poll poll-cancel ring-leak fsync io_uring_setup io_uring_register
send_recvmsg a4c0b3decb33-test 500f9fbadef8-test timeout \
sq-space_left stdout cq-ready cq-peek-batch file-register \
cq-size 8a9973408177-test a0908ae19763-test 232c93d07b74-test \
- socket-rw accept timeout-overflow defer read-write io-cancel
+ socket-rw accept timeout-overflow defer read-write io-cancel \
+ link-timeout
include ../Makefile.quiet
@@ -24,7 +25,7 @@ test_srcs := poll.c poll-cancel.c ring-leak.c fsync.c io_uring_setup.c \
500f9fbadef8-test.c timeout.c sq-space_left.c stdout.c cq-ready.c\
cq-peek-batch.c file-register.c cq-size.c 8a9973408177-test.c \
a0908ae19763-test.c 232c93d07b74-test.c socket-rw.c accept.c \
- timeout-overflow.c defer.c read-write.c io-cancel.c
+ timeout-overflow.c defer.c read-write.c io-cancel.c link-timeout.c
test_objs := $(patsubst %.c,%.ol,$(test_srcs))
diff --git a/test/link-timeout.c b/test/link-timeout.c
new file mode 100644
index 0000000..c8dfc6a
--- /dev/null
+++ b/test/link-timeout.c
@@ -0,0 +1,435 @@
+/*
+ * Description: run various linked timeout cases
+ *
+ */
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+
+#include "liburing.h"
+
+/*
+ * Test linked timeout with timeout (timeoutception)
+ */
+static int test_single_link_timeout_ception(struct io_uring *ring)
+{
+ struct __kernel_timespec ts1, ts2;
+ struct io_uring_cqe *cqe;
+ struct io_uring_sqe *sqe;
+ int ret, i;
+
+ sqe = io_uring_get_sqe(ring);
+ if (!sqe) {
+ printf("get sqe failed\n");
+ goto err;
+ }
+
+ ts1.tv_sec = 1;
+ ts1.tv_nsec = 0;
+ io_uring_prep_timeout(sqe, &ts1, -1U, 0);
+ sqe->flags |= IOSQE_IO_LINK;
+ sqe->user_data = 1;
+
+ sqe = io_uring_get_sqe(ring);
+ if (!sqe) {
+ printf("get sqe failed\n");
+ goto err;
+ }
+
+ ts2.tv_sec = 2;
+ ts2.tv_nsec = 0;
+ io_uring_prep_link_timeout(sqe, &ts2, 0);
+ sqe->user_data = 2;
+
+ ret = io_uring_submit(ring);
+ if (ret != 2) {
+ printf("sqe submit failed: %d\n", ret);
+ goto err;
+ }
+
+ for (i = 0; i < 2; i++) {
+ ret = io_uring_wait_cqe(ring, &cqe);
+ if (ret < 0) {
+ printf("wait completion %d\n", ret);
+ goto err;
+ }
+ switch (cqe->user_data) {
+ case 1:
+ if (cqe->res != -EINVAL) {
+ fprintf(stderr, "Timeout got %d, wanted -EINVAL\n",
+ cqe->res);
+ goto err;
+ }
+ break;
+ case 2:
+ if (cqe->res != -ECANCELED) {
+ fprintf(stderr, "Link timeout got %d, wanted -ECANCELED\n", cqe->res);
+ goto err;
+ }
+ break;
+ }
+ io_uring_cqe_seen(ring, cqe);
+ }
+
+ return 0;
+err:
+ return 1;
+}
+
+/*
+ * Test linked timeout with NOP
+ */
+static int test_single_link_timeout_nop(struct io_uring *ring)
+{
+ struct __kernel_timespec ts;
+ struct io_uring_cqe *cqe;
+ struct io_uring_sqe *sqe;
+ int ret, i;
+
+ sqe = io_uring_get_sqe(ring);
+ if (!sqe) {
+ printf("get sqe failed\n");
+ goto err;
+ }
+
+ io_uring_prep_nop(sqe);
+ sqe->flags |= IOSQE_IO_LINK;
+ sqe->user_data = 1;
+
+ sqe = io_uring_get_sqe(ring);
+ if (!sqe) {
+ printf("get sqe failed\n");
+ goto err;
+ }
+
+ ts.tv_sec = 1;
+ ts.tv_nsec = 0;
+ io_uring_prep_link_timeout(sqe, &ts, 0);
+ sqe->user_data = 2;
+
+ ret = io_uring_submit(ring);
+ if (ret != 2) {
+ printf("sqe submit failed: %d\n", ret);
+ goto err;
+ }
+
+ for (i = 0; i < 2; i++) {
+ ret = io_uring_wait_cqe(ring, &cqe);
+ if (ret < 0) {
+ printf("wait completion %d\n", ret);
+ goto err;
+ }
+ switch (cqe->user_data) {
+ case 1:
+ if (cqe->res) {
+ fprintf(stderr, "NOP got %d, wanted 0\n", cqe->res);
+ goto err;
+ }
+ break;
+ case 2:
+ if (cqe->res != -ECANCELED) {
+ fprintf(stderr, "Link timeout got %d, wanted -ECACNCELED\n", cqe->res);
+ goto err;
+ }
+ break;
+ }
+ io_uring_cqe_seen(ring, cqe);
+ }
+
+ return 0;
+err:
+ return 1;
+}
+
+/*
+ * Test read that will not complete, with a linked timeout behind it that
+ * has errors in the SQE
+ */
+static int test_single_link_timeout_error(struct io_uring *ring)
+{
+ struct __kernel_timespec ts;
+ struct io_uring_cqe *cqe;
+ struct io_uring_sqe *sqe;
+ int fds[2], ret, i;
+ struct iovec iov;
+ char buffer[128];
+
+ if (pipe(fds)) {
+ perror("pipe");
+ return 1;
+ }
+
+ sqe = io_uring_get_sqe(ring);
+ if (!sqe) {
+ printf("get sqe failed\n");
+ goto err;
+ }
+
+ iov.iov_base = buffer;
+ iov.iov_len = sizeof(buffer);
+ io_uring_prep_readv(sqe, fds[0], &iov, 1, 0);
+ sqe->flags |= IOSQE_IO_LINK;
+ sqe->user_data = 1;
+
+ sqe = io_uring_get_sqe(ring);
+ if (!sqe) {
+ printf("get sqe failed\n");
+ goto err;
+ }
+
+ ts.tv_sec = 1;
+ ts.tv_nsec = 0;
+ io_uring_prep_link_timeout(sqe, &ts, 0);
+ /* set invalid field, it'll get failed */
+ sqe->ioprio = 89;
+ sqe->user_data = 2;
+
+ ret = io_uring_submit(ring);
+ if (ret != 2) {
+ printf("sqe submit failed: %d\n", ret);
+ goto err;
+ }
+
+ for (i = 0; i < 2; i++) {
+ ret = io_uring_wait_cqe(ring, &cqe);
+ if (ret < 0) {
+ printf("wait completion %d\n", ret);
+ goto err;
+ }
+ switch (cqe->user_data) {
+ case 1:
+ if (cqe->res != -ECANCELED) {
+ fprintf(stderr, "Read got %d, wanted -EINTR\n", cqe->res);
+ goto err;
+ }
+ break;
+ case 2:
+ if (cqe->res != -EINVAL) {
+ fprintf(stderr, "Link timeout got %d, wanted -EALREADY\n", cqe->res);
+ goto err;
+ }
+ break;
+ }
+ io_uring_cqe_seen(ring, cqe);
+ }
+
+ return 0;
+err:
+ return 1;
+}
+
+/*
+ * Test read that will complete, with a linked timeout behind it
+ */
+static int test_single_link_no_timeout(struct io_uring *ring)
+{
+ struct __kernel_timespec ts;
+ struct io_uring_cqe *cqe;
+ struct io_uring_sqe *sqe;
+ int fds[2], ret, i;
+ struct iovec iov;
+ char buffer[128];
+
+ if (pipe(fds)) {
+ perror("pipe");
+ return 1;
+ }
+
+ sqe = io_uring_get_sqe(ring);
+ if (!sqe) {
+ printf("get sqe failed\n");
+ goto err;
+ }
+
+ iov.iov_base = buffer;
+ iov.iov_len = sizeof(buffer);
+ io_uring_prep_readv(sqe, fds[0], &iov, 1, 0);
+ sqe->flags |= IOSQE_IO_LINK;
+ sqe->user_data = 1;
+
+ sqe = io_uring_get_sqe(ring);
+ if (!sqe) {
+ printf("get sqe failed\n");
+ goto err;
+ }
+
+ ts.tv_sec = 1;
+ ts.tv_nsec = 0;
+ io_uring_prep_link_timeout(sqe, &ts, 0);
+ sqe->user_data = 2;
+
+ sqe = io_uring_get_sqe(ring);
+ if (!sqe) {
+ printf("get sqe failed\n");
+ goto err;
+ }
+
+ iov.iov_base = buffer;
+ iov.iov_len = sizeof(buffer);
+ io_uring_prep_writev(sqe, fds[1], &iov, 1, 0);
+ sqe->user_data = 3;
+
+ ret = io_uring_submit(ring);
+ if (ret != 3) {
+ printf("sqe submit failed: %d\n", ret);
+ goto err;
+ }
+
+ for (i = 0; i < 3; i++) {
+ ret = io_uring_wait_cqe(ring, &cqe);
+ if (ret < 0) {
+ printf("wait completion %d\n", ret);
+ goto err;
+ }
+ switch (cqe->user_data) {
+ case 1:
+ case 3:
+ if (cqe->res != sizeof(buffer)) {
+ fprintf(stderr, "R/W got %d, wanted %d\n", cqe->res,
+ (int) sizeof(buffer));
+ goto err;
+ }
+ break;
+ case 2:
+ if (cqe->res != -ECANCELED) {
+ fprintf(stderr, "Link timeout %d, wanted -ECANCELED\n",
+ cqe->res);
+ goto err;
+ }
+ break;
+ }
+ io_uring_cqe_seen(ring, cqe);
+ }
+
+ return 0;
+err:
+ return 1;
+}
+
+/*
+ * Test read that will not complete, with a linked timeout behind it
+ */
+static int test_single_link_timeout(struct io_uring *ring, unsigned nsec)
+{
+ struct __kernel_timespec ts;
+ struct io_uring_cqe *cqe;
+ struct io_uring_sqe *sqe;
+ int fds[2], ret, i;
+ struct iovec iov;
+ char buffer[128];
+
+ if (pipe(fds)) {
+ perror("pipe");
+ return 1;
+ }
+
+ sqe = io_uring_get_sqe(ring);
+ if (!sqe) {
+ printf("get sqe failed\n");
+ goto err;
+ }
+
+ iov.iov_base = buffer;
+ iov.iov_len = sizeof(buffer);
+ io_uring_prep_readv(sqe, fds[0], &iov, 1, 0);
+ sqe->flags |= IOSQE_IO_LINK;
+ sqe->user_data = 1;
+
+ sqe = io_uring_get_sqe(ring);
+ if (!sqe) {
+ printf("get sqe failed\n");
+ goto err;
+ }
+
+ ts.tv_sec = 0;
+ ts.tv_nsec = nsec;
+ io_uring_prep_link_timeout(sqe, &ts, 0);
+ sqe->user_data = 2;
+
+ ret = io_uring_submit(ring);
+ if (ret != 2) {
+ printf("sqe submit failed: %d\n", ret);
+ goto err;
+ }
+
+ for (i = 0; i < 2; i++) {
+ ret = io_uring_wait_cqe(ring, &cqe);
+ if (ret < 0) {
+ printf("wait completion %d\n", ret);
+ goto err;
+ }
+ switch (cqe->user_data) {
+ case 1:
+ if (cqe->res != -EINTR && cqe->res != -ECANCELED) {
+ fprintf(stderr, "Read got %d\n", cqe->res);
+ goto err;
+ }
+ break;
+ case 2:
+ if (cqe->res != -EALREADY && cqe->res != 0) {
+ fprintf(stderr, "Link timeout got %d\n", cqe->res);
+ goto err;
+ }
+ break;
+ }
+ io_uring_cqe_seen(ring, cqe);
+ }
+
+ return 0;
+err:
+ return 1;
+}
+
+int main(int argc, char *argv[])
+{
+ struct io_uring ring;
+ int ret;
+
+ ret = io_uring_queue_init(8, &ring, 0);
+ if (ret) {
+ printf("ring setup failed\n");
+ return 1;
+
+ }
+
+ ret = test_single_link_timeout(&ring, 100000ULL);
+ if (ret) {
+ printf("test_single_link_timeout failed\n");
+ return ret;
+ }
+
+ ret = test_single_link_timeout(&ring, 500000000ULL);
+ if (ret) {
+ printf("test_single_link_timeout failed\n");
+ return ret;
+ }
+
+ ret = test_single_link_no_timeout(&ring);
+ if (ret) {
+ printf("test_single_link_no_timeout failed\n");
+ return ret;
+ }
+
+ ret = test_single_link_timeout_error(&ring);
+ if (ret) {
+ printf("test_single_link_timeout_error failed\n");
+ return ret;
+ }
+
+ ret = test_single_link_timeout_nop(&ring);
+ if (ret) {
+ printf("test_single_link_timeout_nop failed\n");
+ return ret;
+ }
+
+ ret = test_single_link_timeout_ception(&ring);
+ if (ret) {
+ printf("test_single_link_timeout_ception failed\n");
+ return ret;
+ }
+
+ return 0;
+}