diff options
author | thegeorg <thegeorg@yandex-team.com> | 2024-06-09 11:55:21 +0300 |
---|---|---|
committer | thegeorg <thegeorg@yandex-team.com> | 2024-06-09 12:07:55 +0300 |
commit | afd4899380eea1c70e2a68714b5da1c9919ccdbd (patch) | |
tree | cd5120708784139bc6a0f8881da1ed8389a065b3 /contrib/libs/liburing/test/buf-ring-nommap.c | |
parent | a83bd2dd3c21e38c6c0807ec5e679497ab567f24 (diff) | |
download | ydb-afd4899380eea1c70e2a68714b5da1c9919ccdbd.tar.gz |
Update contrib/libs/liburing to 2.6
3b51a9fb14de805208d11f1c077c78bb5d487e0f
Diffstat (limited to 'contrib/libs/liburing/test/buf-ring-nommap.c')
-rw-r--r-- | contrib/libs/liburing/test/buf-ring-nommap.c | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/contrib/libs/liburing/test/buf-ring-nommap.c b/contrib/libs/liburing/test/buf-ring-nommap.c new file mode 100644 index 0000000000..7624c67fa0 --- /dev/null +++ b/contrib/libs/liburing/test/buf-ring-nommap.c @@ -0,0 +1,122 @@ +#include "../config-host.h" +/* SPDX-License-Identifier: MIT */ +/* + * Description: test IOU_PBUF_RING_MMAP with a ring setup with a ring + * setup without mmap'ing sq/cq arrays + * + */ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <fcntl.h> +#include <string.h> +#include <sys/mman.h> + +#include "liburing.h" +#include "helpers.h" + +static int bgid = 5; +static int bid = 89; + +int main(int argc, char *argv[]) +{ + struct io_uring_buf_ring *br; + struct io_uring_sqe *sqe; + struct io_uring_cqe *cqe; + struct io_uring ring; + size_t ring_size; + int ret, ring_mask, fds[2]; + struct io_uring_buf_reg reg = { + .ring_entries = 1, + .bgid = bgid, + .flags = IOU_PBUF_RING_MMAP, + }; + struct io_uring_params p = { }; + void *ring_mem; + char buf[32]; + off_t off; + + if (argc > 1) + return T_EXIT_SKIP; + + if (posix_memalign(&ring_mem, 16384, 16384)) + return T_EXIT_FAIL; + + p.flags = IORING_SETUP_NO_MMAP; + ret = io_uring_queue_init_mem(1, &ring, &p, ring_mem, 16384); + if (ret < 0) { + if (ret == -EINVAL) + return T_EXIT_SKIP; + fprintf(stderr, "queue init failed %d\n", ret); + return T_EXIT_FAIL; + } + + if (pipe(fds) < 0) { + perror("pipe"); + return T_EXIT_FAIL; + } + + ring_size = sizeof(struct io_uring_buf); + ring_mask = io_uring_buf_ring_mask(1); + + ret = io_uring_register_buf_ring(&ring, ®, 0); + if (ret) { + if (ret == -EINVAL) + return T_EXIT_SKIP; + fprintf(stderr, "reg buf ring: %d\n", ret); + return T_EXIT_FAIL; + } + + off = IORING_OFF_PBUF_RING | + (unsigned long long) bgid << IORING_OFF_PBUF_SHIFT; + br = mmap(NULL, ring_size, PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_POPULATE, ring.ring_fd, off); + if (br == MAP_FAILED) { + if (errno == ENOMEM) + return T_EXIT_SKIP; + perror("mmap"); + return T_EXIT_FAIL; + } + + io_uring_buf_ring_add(br, buf, sizeof(buf), bid, ring_mask, 0); + io_uring_buf_ring_advance(br, 1); + + sqe = io_uring_get_sqe(&ring); + io_uring_prep_read(sqe, fds[0], NULL, 0, 0); + sqe->flags |= IOSQE_BUFFER_SELECT; + sqe->buf_group = bgid; + + io_uring_submit(&ring); + + ret = write(fds[1], "Hello", 5); + if (ret < 0) { + perror("write"); + return T_EXIT_FAIL; + } else if (ret != 5) { + fprintf(stderr, "short write %d\n", ret); + return T_EXIT_FAIL; + } + + ret = io_uring_wait_cqe(&ring, &cqe); + if (ret) { + fprintf(stderr, "wait %d\n", ret); + return T_EXIT_FAIL; + } + if (cqe->res < 0) { + fprintf(stderr, "cqe res %d\n", cqe->res); + return T_EXIT_FAIL; + } + if (!(cqe->flags & IORING_CQE_F_BUFFER)) { + fprintf(stderr, "buffer not selected in cqe\n"); + return T_EXIT_FAIL; + } + if ((cqe->flags >> IORING_CQE_BUFFER_SHIFT) != bid) { + fprintf(stderr, "wrong buffer id returned\n"); + return T_EXIT_FAIL; + } + + io_uring_cqe_seen(&ring, cqe); + + io_uring_queue_exit(&ring); + return T_EXIT_PASS; +} |