123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- #include "../config-host.h"
- /* SPDX-License-Identifier: MIT */
- /*
- * Description: run various linked sqe tests
- *
- */
- #include <errno.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include "liburing.h"
- #include "helpers.h"
- static int no_hardlink;
- /*
- * Timer with single nop
- */
- static int test_single_hardlink(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) {
- fprintf(stderr, "get sqe failed\n");
- goto err;
- }
- ts.tv_sec = 0;
- ts.tv_nsec = 10000000ULL;
- io_uring_prep_timeout(sqe, &ts, 0, 0);
- sqe->flags |= IOSQE_IO_LINK | IOSQE_IO_HARDLINK;
- sqe->user_data = 1;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "get sqe failed\n");
- goto err;
- }
- io_uring_prep_nop(sqe);
- sqe->user_data = 2;
- 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_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "wait completion %d\n", ret);
- goto err;
- }
- if (!cqe) {
- fprintf(stderr, "failed to get cqe\n");
- goto err;
- }
- if (no_hardlink)
- goto next;
- if (cqe->user_data == 1 && cqe->res == -EINVAL) {
- fprintf(stdout, "Hard links not supported, skipping\n");
- no_hardlink = 1;
- goto next;
- }
- if (cqe->user_data == 1 && cqe->res != -ETIME) {
- fprintf(stderr, "timeout failed with %d\n", cqe->res);
- goto err;
- }
- if (cqe->user_data == 2 && cqe->res) {
- fprintf(stderr, "nop failed with %d\n", cqe->res);
- goto err;
- }
- next:
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- err:
- return 1;
- }
- /*
- * Timer -> timer -> nop
- */
- static int test_double_hardlink(struct io_uring *ring)
- {
- struct __kernel_timespec ts1, ts2;
- struct io_uring_cqe *cqe;
- struct io_uring_sqe *sqe;
- int ret, i;
- if (no_hardlink)
- return 0;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "get sqe failed\n");
- goto err;
- }
- ts1.tv_sec = 0;
- ts1.tv_nsec = 10000000ULL;
- io_uring_prep_timeout(sqe, &ts1, 0, 0);
- sqe->flags |= IOSQE_IO_LINK | IOSQE_IO_HARDLINK;
- sqe->user_data = 1;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "get sqe failed\n");
- goto err;
- }
- ts2.tv_sec = 0;
- ts2.tv_nsec = 15000000ULL;
- io_uring_prep_timeout(sqe, &ts2, 0, 0);
- sqe->flags |= IOSQE_IO_LINK | IOSQE_IO_HARDLINK;
- sqe->user_data = 2;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "get sqe failed\n");
- goto err;
- }
- io_uring_prep_nop(sqe);
- sqe->user_data = 3;
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- fprintf(stderr, "sqe submit failed: %d\n", ret);
- goto err;
- }
- for (i = 0; i < 3; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- fprintf(stderr, "wait completion %d\n", ret);
- goto err;
- }
- if (!cqe) {
- fprintf(stderr, "failed to get cqe\n");
- goto err;
- }
- if (cqe->user_data == 1 && cqe->res != -ETIME) {
- fprintf(stderr, "timeout failed with %d\n", cqe->res);
- goto err;
- }
- if (cqe->user_data == 2 && cqe->res != -ETIME) {
- fprintf(stderr, "timeout failed with %d\n", cqe->res);
- goto err;
- }
- if (cqe->user_data == 3 && cqe->res) {
- fprintf(stderr, "nop failed with %d\n", cqe->res);
- goto err;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- err:
- return 1;
- }
- /*
- * Test failing head of chain, and dependent getting -ECANCELED
- */
- static int test_single_link_fail(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) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_remove_buffers(sqe, 10, 1);
- sqe->flags |= IOSQE_IO_LINK;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_nop(sqe);
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- printf("sqe submit failed: %d\n", ret);
- goto err;
- }
- for (i = 0; i < 2; i++) {
- ret = io_uring_peek_cqe(ring, &cqe);
- if (ret < 0) {
- printf("wait completion %d\n", ret);
- goto err;
- }
- if (!cqe) {
- printf("failed to get cqe\n");
- goto err;
- }
- if (i == 0 && cqe->res != -ENOENT) {
- printf("sqe0 failed with %d, wanted -ENOENT\n", cqe->res);
- goto err;
- }
- if (i == 1 && cqe->res != -ECANCELED) {
- printf("sqe1 failed with %d, wanted -ECANCELED\n", cqe->res);
- goto err;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- err:
- return 1;
- }
- /*
- * Test two independent chains
- */
- static int test_double_chain(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) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_nop(sqe);
- sqe->flags |= IOSQE_IO_LINK;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_nop(sqe);
- 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 = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_nop(sqe);
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- printf("sqe submit failed: %d\n", ret);
- goto err;
- }
- for (i = 0; i < 4; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret < 0) {
- printf("wait completion %d\n", ret);
- goto err;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- err:
- return 1;
- }
- /*
- * Test multiple dependents
- */
- static int test_double_link(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) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_nop(sqe);
- sqe->flags |= IOSQE_IO_LINK;
- 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 = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_nop(sqe);
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- 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;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- err:
- return 1;
- }
- /*
- * Test single dependency
- */
- static int test_single_link(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) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_nop(sqe);
- sqe->flags |= IOSQE_IO_LINK;
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_nop(sqe);
- ret = io_uring_submit(ring);
- if (ret <= 0) {
- 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;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- err:
- return 1;
- }
- static int test_early_fail_and_wait(void)
- {
- struct io_uring ring;
- struct io_uring_sqe *sqe;
- int ret, invalid_fd = 42;
- struct iovec iov = { .iov_base = NULL, .iov_len = 0 };
- /* create a new ring as it leaves it dirty */
- ret = io_uring_queue_init(8, &ring, 0);
- if (ret) {
- printf("ring setup failed\n");
- return 1;
- }
- sqe = io_uring_get_sqe(&ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_readv(sqe, invalid_fd, &iov, 1, 0);
- sqe->flags |= IOSQE_IO_LINK;
- sqe = io_uring_get_sqe(&ring);
- if (!sqe) {
- printf("get sqe failed\n");
- goto err;
- }
- io_uring_prep_nop(sqe);
- ret = io_uring_submit_and_wait(&ring, 2);
- if (ret <= 0 && ret != -EAGAIN) {
- printf("sqe submit failed: %d\n", ret);
- goto err;
- }
- io_uring_queue_exit(&ring);
- return 0;
- err:
- io_uring_queue_exit(&ring);
- return 1;
- }
- int main(int argc, char *argv[])
- {
- struct io_uring ring, poll_ring;
- int ret;
- if (argc > 1)
- return T_EXIT_SKIP;
- ret = io_uring_queue_init(8, &ring, 0);
- if (ret) {
- printf("ring setup failed\n");
- return T_EXIT_FAIL;
- }
- ret = io_uring_queue_init(8, &poll_ring, IORING_SETUP_IOPOLL);
- if (ret) {
- printf("poll_ring setup failed\n");
- return T_EXIT_FAIL;
- }
- ret = test_single_link(&ring);
- if (ret) {
- printf("test_single_link failed\n");
- return ret;
- }
- ret = test_double_link(&ring);
- if (ret) {
- printf("test_double_link failed\n");
- return ret;
- }
- ret = test_double_chain(&ring);
- if (ret) {
- printf("test_double_chain failed\n");
- return ret;
- }
- ret = test_single_link_fail(&poll_ring);
- if (ret) {
- printf("test_single_link_fail failed\n");
- return ret;
- }
- ret = test_single_hardlink(&ring);
- if (ret) {
- fprintf(stderr, "test_single_hardlink\n");
- return ret;
- }
- ret = test_double_hardlink(&ring);
- if (ret) {
- fprintf(stderr, "test_double_hardlink\n");
- return ret;
- }
- ret = test_early_fail_and_wait();
- if (ret) {
- fprintf(stderr, "test_early_fail_and_wait\n");
- return ret;
- }
- return T_EXIT_PASS;
- }
|