Test that a multishot receive with sqe->optlen set receives exactly the limit when more data is queued, with limits that aren't a multiple of the provided buffer size or of sqe->len. Cover plain and bundle receives, normal and incrementally consumed buffer rings, and data queued before or after the receive is armed. recv-mshot-fair uses a limit that is a multiple of its buffer size, so it can't see a receive that goes over. The plain cases fail on kernels without "io_uring/net: cap a non-bundle multishot recv at the remaining limit". Written and tested by Claude Code, I handled the review process. Signed-off-by: Hang Zhengyang --- test/Makefile | 1 + test/recv-mshot-limit.c | 160 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 test/recv-mshot-limit.c diff --git a/test/Makefile b/test/Makefile index 8771aa50..273b3c71 100644 --- a/test/Makefile +++ b/test/Makefile @@ -228,6 +228,7 @@ test_srcs := \ recv-msgall-stream.c \ recv-mshot-drain.c \ recv-mshot-fair.c \ + recv-mshot-limit.c \ recv-multishot.c \ recvmsg-inc-tail.c \ reg-fd-only.c \ diff --git a/test/recv-mshot-limit.c b/test/recv-mshot-limit.c new file mode 100644 index 00000000..9aea42d2 --- /dev/null +++ b/test/recv-mshot-limit.c @@ -0,0 +1,160 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Test that a multishot receive with a total byte limit (sqe->optlen) + * receives exactly the limit when more data is queued, with limits that + * aren't a multiple of the provided buffer size or of the per-receive length. + * Covers plain and bundle receives, normal and incrementally consumed buffer + * rings, and data that is queued before or after the receive is armed. + */ +#include +#include +#include +#include +#include + +#include "liburing.h" +#include "helpers.h" + +#define BUF_SZ 4096 +#define NR_BUFS 8 +#define BGID 1 +#define DATA_SZ 8192 + +static char bufs[NR_BUFS][BUF_SZ]; + +enum { + F_BUNDLE = 1, + F_INC = 2, + F_LATE = 4, + F_ALL = 8, +}; + +/* + * @probe: the first run, where -EINVAL means the kernel has no limit support + */ +static int test(unsigned int limit, unsigned int len, int flags, int probe) +{ + struct __kernel_timespec ts = { .tv_sec = 1, }; + struct io_uring_buf_ring *br; + struct io_uring_sqe *sqe; + struct io_uring_cqe *cqe; + struct io_uring ring; + char data[DATA_SZ]; + unsigned int total = 0; + int fds[2], ret, i, more; + + ret = io_uring_queue_init(8, &ring, 0); + if (ret) { + fprintf(stderr, "queue init: %d\n", ret); + return T_EXIT_FAIL; + } + if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) { + perror("socketpair"); + return T_EXIT_FAIL; + } + br = io_uring_setup_buf_ring(&ring, NR_BUFS, BGID, + flags & F_INC ? IOU_PBUF_RING_INC : 0, &ret); + if (!br) { + if (ret == -EINVAL) { + io_uring_queue_exit(&ring); + close(fds[0]); + close(fds[1]); + return T_EXIT_SKIP; + } + fprintf(stderr, "buf ring setup: %d\n", ret); + return T_EXIT_FAIL; + } + for (i = 0; i < NR_BUFS; i++) + io_uring_buf_ring_add(br, bufs[i], BUF_SZ, i, + io_uring_buf_ring_mask(NR_BUFS), i); + io_uring_buf_ring_advance(br, NR_BUFS); + + memset(data, 0x5a, sizeof(data)); + if (!(flags & F_LATE) && write(fds[1], data, sizeof(data)) != sizeof(data)) { + perror("write"); + return T_EXIT_FAIL; + } + + sqe = io_uring_get_sqe(&ring); + io_uring_prep_recv_multishot(sqe, fds[0], NULL, len, 0); + sqe->flags |= IOSQE_BUFFER_SELECT; + sqe->buf_group = BGID; + sqe->optlen = limit; + if (flags & F_BUNDLE) + sqe->ioprio |= IORING_RECVSEND_BUNDLE; + io_uring_submit(&ring); + + if ((flags & F_LATE) && write(fds[1], data, sizeof(data)) != sizeof(data)) { + perror("write"); + return T_EXIT_FAIL; + } + + do { + ret = io_uring_wait_cqe_timeout(&ring, &cqe, &ts); + if (ret) { + fprintf(stderr, "limit %u len %u flags %x: no final CQE " + "after %u bytes\n", limit, len, flags, total); + return T_EXIT_FAIL; + } + if (cqe->res == -EINVAL && probe && !total) { + /* sqe->optlen was file_index before the limit existed */ + io_uring_cqe_seen(&ring, cqe); + ret = T_EXIT_SKIP; + goto out; + } + if (cqe->res < 0) { + fprintf(stderr, "recv: %d\n", cqe->res); + return T_EXIT_FAIL; + } + total += cqe->res; + more = cqe->flags & IORING_CQE_F_MORE; + io_uring_cqe_seen(&ring, cqe); + } while (more); + + ret = T_EXIT_PASS; + if (total != limit) { + fprintf(stderr, "limit %u len %u flags %x: got %u bytes\n", + limit, len, flags, total); + ret = T_EXIT_FAIL; + } +out: + io_uring_free_buf_ring(&ring, br, NR_BUFS, BGID); + io_uring_queue_exit(&ring); + close(fds[0]); + close(fds[1]); + return ret; +} + +int main(int argc, char *argv[]) +{ + static const unsigned int limits[] = { 100, 1500, 6000 }; + static const unsigned int lens[] = { 0, 1024 }; + unsigned int l, n; + int flags, ret; + + if (argc > 1) + return T_EXIT_SKIP; + + for (flags = 0; flags < F_ALL; flags++) { + for (l = 0; l < ARRAY_SIZE(limits); l++) { + for (n = 0; n < ARRAY_SIZE(lens); n++) { + int probe = !flags && !l && !n; + + ret = test(limits[l], lens[n], flags, probe); + if (ret == T_EXIT_SKIP) { + if (probe) + return T_EXIT_SKIP; + /* no incremental buffer rings */ + if (flags & F_INC) + goto next; + ret = T_EXIT_FAIL; + } + if (ret) + return T_EXIT_FAIL; + } + } +next: + ; + } + return T_EXIT_PASS; +} base-commit: ed312b3c94f16aea679f689d4950284a954d3b5c -- 2.53.0