Skip to contentSkip to conversation

Add query and mock tests

PATCH Applied Pavel Begunkov 2 participants 12 messages Sep 11, 2025 – Sep 22, 2025 io_uring

lore
Share & tools
lore
#0 lore raw

cover Add query and mock tests

Also introduces a bunch of test/ helper functions.

Pavel Begunkov (6):
tests: test the query interface
tests: add t_submit_and_wait_single helper
tests: introduce t_iovec_data_length helper
tests: add t_sqe_prep_cmd helper
tests: add helper for iov data verification
tests: add mock file based tests

 src/include/liburing.h                |   1 +
 src/include/liburing/io_uring.h       |   3 +
 src/include/liburing/io_uring/query.h |  41 +++
 test/Makefile                         |   2 +
 test/helpers.c                        |  71 +++++
 test/helpers.h                        |  14 +
 test/mock_file.c                      | 373 ++++++++++++++++++++++++++
 test/mock_file.h                      |  47 ++++
 test/ring-query.c                     | 322 ++++++++++++++++++++++
 test/vec-regbuf.c                     |   6 +-
 10 files changed, 876 insertions(+), 4 deletions(-)
 create mode 100644 src/include/liburing/io_uring/query.h
 create mode 100644 test/mock_file.c
 create mode 100644 test/mock_file.h
 create mode 100644 test/ring-query.c
Signature · 1 line
2.49.0
#1 lore raw

PATCH 1/6 tests: test the query interface

Add the header / definitions and tests for the query interface.

---
 src/include/liburing.h                |   1 +
 src/include/liburing/io_uring.h       |   3 +
 src/include/liburing/io_uring/query.h |  41 ++++
 test/Makefile                         |   1 +
 test/ring-query.c                     | 322 ++++++++++++++++++++++++++
 5 files changed, 368 insertions(+)
 create mode 100644 src/include/liburing/io_uring/query.h
 create mode 100644 test/ring-query.c
src/include/liburing.h +1 -0
index e3f394ea..46d3cccf 100644
@@ -16,6 +16,7 @@
 #include <sys/wait.h>
 #include "liburing/compat.h"
 #include "liburing/io_uring.h"
+#include "liburing/io_uring/query.h"
 #include "liburing/io_uring_version.h"
 #include "liburing/barrier.h"
 
src/include/liburing/io_uring.h +3 -0
index 212b5874..55b69f9d 100644
@@ -641,6 +641,9 @@ enum io_uring_register_op {
 
 	IORING_REGISTER_MEM_REGION		= 34,
 
+	/* query various aspects of io_uring, see linux/io_uring/query.h */
+	IORING_REGISTER_QUERY			= 35,
+
 	/* this goes last */
 	IORING_REGISTER_LAST,
 
src/include/liburing/io_uring/query.h new file +41 -0

41 lines not on this page · raw message

test/Makefile +1 -0
index edfc0df7..626ae674 100644
@@ -255,6 +255,7 @@ test_srcs := \
 	zcrx.c \
 	vec-regbuf.c \
 	timestamp.c \
+	ring-query.c \
 	# EOL
 
 # Please keep this list sorted alphabetically.
test/ring-query.c new file +322 -0

322 lines not on this page · raw message

Signature · 1 line
2.49.0
#3 lore raw

PATCH 2/6 tests: add t_submit_and_wait_single helper

In reply to Pavel Begunkov
---
 test/helpers.c | 17 +++++++++++++++++
 test/helpers.h |  2 ++
 2 files changed, 19 insertions(+)
test/helpers.c +17 -0
index 05895486..18af2be8 100644
@@ -509,3 +509,20 @@ void t_clear_nonblock(int fd)
 {
 	__t_toggle_nonblock(fd, 0);
 }
+
+int t_submit_and_wait_single(struct io_uring *ring, struct io_uring_cqe **cqe)
+{
+	int ret;
+
+	ret = io_uring_submit(ring);
+	if (ret <= 0) {
+		fprintf(stderr, "sqe submit failed: %d\n", ret);
+		return -1;
+	}
+	ret = io_uring_wait_cqe(ring, cqe);
+	if (ret < 0) {
+		fprintf(stderr, "wait completion %d\n", ret);
+		return ret;
+	}
+	return 0;
+}
test/helpers.h +2 -0
index 3f0c11ab..b7465890 100644
@@ -122,6 +122,8 @@ unsigned long long mtime_since_now(struct timeval *tv);
 unsigned long long utime_since(const struct timeval *s, const struct timeval *e);
 unsigned long long utime_since_now(struct timeval *tv);
 
+int t_submit_and_wait_single(struct io_uring *ring, struct io_uring_cqe **cqe);
+
 #ifdef __cplusplus
 }
 #endif
Signature · 1 line
2.49.0
#4 lore raw

PATCH 3/6 tests: introduce t_iovec_data_length helper

In reply to Pavel Begunkov
---
 test/helpers.c    | 10 ++++++++++
 test/helpers.h    |  2 ++
 test/vec-regbuf.c |  6 ++----
 3 files changed, 14 insertions(+), 4 deletions(-)
test/helpers.c +10 -0
index 18af2be8..15ceb17a 100644
@@ -526,3 +526,13 @@ int t_submit_and_wait_single(struct io_uring *ring, struct io_uring_cqe **cqe)
 	}
 	return 0;
 }
+
+size_t t_iovec_data_length(struct iovec *iov, unsigned iov_len)
+{
+	size_t sz = 0;
+	int i;
+
+	for (i = 0; i < iov_len; i++)
+		sz += iov[i].iov_len;
+	return sz;
+}
test/helpers.h +2 -0
index b7465890..a45b8683 100644
@@ -124,6 +124,8 @@ unsigned long long utime_since_now(struct timeval *tv);
 
 int t_submit_and_wait_single(struct io_uring *ring, struct io_uring_cqe **cqe);
 
+size_t t_iovec_data_length(struct iovec *iov, unsigned iov_len);
+
 #ifdef __cplusplus
 }
 #endif
test/vec-regbuf.c +2 -4
index 286b78c6..0cbe2c74 100644
@@ -269,7 +269,7 @@ static int test_vec(struct buf_desc *bd, struct iovec *vecs, int nr_vec,
 	struct sockaddr_storage addr;
 	int sock_server, sock_client;
 	struct verify_data vd;
-	size_t total_len = 0;
+	size_t total_len;
 	int i, ret;
 	void *verify_res;
 	pthread_t th;
@@ -284,9 +284,7 @@ static int test_vec(struct buf_desc *bd, struct iovec *vecs, int nr_vec,
 	for (i = 0; i < bd->size; i++)
 		bd->buf_wr[i] = i;
 	memset(bd->buf_rd, 0, bd->size);
-
-	for (i = 0; i < nr_vec; i++)
-		total_len += vecs[i].iov_len;
+	total_len = t_iovec_data_length(vecs, nr_vec);
 
 	vd.bd = bd;
 	vd.vecs = vecs;
Signature · 1 line
2.49.0
#5 lore raw

PATCH 4/6 tests: add t_sqe_prep_cmd helper

In reply to Pavel Begunkov
---
 test/helpers.h | 7 +++++++
 1 file changed, 7 insertions(+)
test/helpers.h +7 -0
index a45b8683..7dafeb17 100644
@@ -126,6 +126,13 @@ int t_submit_and_wait_single(struct io_uring *ring, struct io_uring_cqe **cqe);
 
 size_t t_iovec_data_length(struct iovec *iov, unsigned iov_len);
 
+static inline void t_sqe_prep_cmd(struct io_uring_sqe *sqe,
+				  int fd, unsigned cmd_op)
+{
+	io_uring_prep_rw(IORING_OP_URING_CMD, sqe, fd, NULL, 0, 0);
+	sqe->cmd_op = cmd_op;
+}
+
 #ifdef __cplusplus
 }
 #endif
Signature · 1 line
2.49.0
#6 lore raw

PATCH 5/6 tests: add helper for iov data verification

In reply to Pavel Begunkov
---
 test/helpers.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 test/helpers.h |  3 +++
 2 files changed, 47 insertions(+)
test/helpers.c +44 -0
index 15ceb17a..83a128e3 100644
@@ -536,3 +536,47 @@ size_t t_iovec_data_length(struct iovec *iov, unsigned iov_len)
 		sz += iov[i].iov_len;
 	return sz;
 }
+
+#define t_min(a, b) ((a) < (b) ? (a) : (b))
+
+unsigned long t_compare_data_iovec(struct iovec *iov_src, unsigned nr_src,
+				   struct iovec *iov_dst, unsigned nr_dst)
+{
+	size_t src_len = t_iovec_data_length(iov_src, nr_src);
+	size_t dst_len = t_iovec_data_length(iov_dst, nr_dst);
+	size_t len_left = t_min(src_len, dst_len);
+	unsigned long src_off = 0, dst_off = 0;
+	unsigned long offset = 0;
+
+	while (offset != len_left) {
+		size_t len = len_left - offset;
+		unsigned long i;
+
+		len = t_min(len, iov_src->iov_len - src_off);
+		len = t_min(len, iov_dst->iov_len - dst_off);
+
+		for (i = 0; i < len; i++) {
+			char csrc = ((char *)iov_src->iov_base)[src_off + i];
+			char cdst = ((char *)iov_dst->iov_base)[dst_off + i];
+
+			if (csrc != cdst) {
+				fprintf(stderr, "data mismatch, %i vs %i\n",
+					csrc, cdst);
+				return -EINVAL;
+			}
+		}
+
+		src_off += len;
+		dst_off += len;
+		if (src_off == iov_src->iov_len) {
+			src_off = 0;
+			iov_src++;
+		}
+		if (dst_off == iov_dst->iov_len) {
+			dst_off = 0;
+			iov_dst++;
+		}
+		offset += len;
+	}
+	return 0;
+}
test/helpers.h +3 -0
index 7dafeb17..cfada945 100644
@@ -126,6 +126,9 @@ int t_submit_and_wait_single(struct io_uring *ring, struct io_uring_cqe **cqe);
 
 size_t t_iovec_data_length(struct iovec *iov, unsigned iov_len);
 
+unsigned long t_compare_data_iovec(struct iovec *iov_src, unsigned nr_src,
+				   struct iovec *iov_dst, unsigned nr_dst);
+
 static inline void t_sqe_prep_cmd(struct io_uring_sqe *sqe,
 				  int fd, unsigned cmd_op)
 {
Signature · 1 line
2.49.0
#7 lore raw

PATCH 6/6 tests: add mock file based tests

In reply to Pavel Begunkov

Tests io_uring cmds with vectored registered buffers, which relies on io_uring mock files. Also test read/write.

---
 test/Makefile    |   1 +
 test/mock_file.c | 373 +++++++++++++++++++++++++++++++++++++++++++++++
 test/mock_file.h |  47 ++++++
 3 files changed, 421 insertions(+)
 create mode 100644 test/mock_file.c
 create mode 100644 test/mock_file.h
test/Makefile +1 -0
index 626ae674..c1afda5c 100644
@@ -256,6 +256,7 @@ test_srcs := \
 	vec-regbuf.c \
 	timestamp.c \
 	ring-query.c \
+	mock_file.c \
 	# EOL
 
 # Please keep this list sorted alphabetically.
test/mock_file.c new file +373 -0

373 lines not on this page · raw message

test/mock_file.h new file +47 -0

47 lines not on this page · raw message

Signature · 1 line
2.49.0
maintainer Applied
#8 lore raw
In reply to Pavel Begunkov
Quoted 11 lines · Pavel Begunkov

Also introduces a bunch of test/ helper functions.

Pavel Begunkov (6):
tests: test the query interface
tests: add t_submit_and_wait_single helper
tests: introduce t_iovec_data_length helper
tests: add t_sqe_prep_cmd helper
tests: add helper for iov data verification
tests: add mock file based tests

[...]

Applied, thanks!

[1/6] tests: test the query interface

      commit: 7e565c0116ba6e0cd1bce3a42409b31fd4dd47d3
[2/6] tests: add t_submit_and_wait_single helper
      commit: f1fc45cbcdcd35064b2fbe3eab6a2b89fb335ec6
[3/6] tests: introduce t_iovec_data_length helper
      commit: 7a936a80be37f50a1851379aa0592eeb3b42a9a1
[4/6] tests: add t_sqe_prep_cmd helper
      commit: 7d3773fd9e5352b113b7d425aa5708acdd48d3c0
[5/6] tests: add helper for iov data verification
      commit: 9e69daf86de39c9b4e70c2dd23e4046293585f34
[6/6] tests: add mock file based tests
      commit: d5673a9b4ad074745e28bf7ddad3692115da01fd

Best regards,

Signature · 1 line
Jens Axboe
#2 lore raw

Re: [PATCH liburing 1/6] tests: test the query interface

In reply to Pavel Begunkov
Quoted 1 line · Pavel Begunkov

Add the header / definitions and tests for the query interface.

...> +static int test_chain_loop(void)

Quoted 26 lines
test/ring-query.c new file +26 -0
+{
+	int ret;
+	struct io_uring_query_opcode op1, op2;
+	struct io_uring_query_hdr hdr2 = {
+		.query_op = IO_URING_QUERY_OPCODES,
+		.query_data = uring_ptr_to_u64(&op2),
+		.size = sizeof(struct io_uring_query_opcode),
+	};
+	struct io_uring_query_hdr hdr1 = {
+		.query_op = IO_URING_QUERY_OPCODES,
+		.query_data = uring_ptr_to_u64(&op1),
+		.size = sizeof(struct io_uring_query_opcode),
+	};
+	struct io_uring_query_hdr hdr_self_circular = {
+		.query_op = IO_URING_QUERY_OPCODES,
+		.query_data = uring_ptr_to_u64(&op1),
+		.size = sizeof(struct io_uring_query_opcode),
+		.next_entry = uring_ptr_to_u64(&hdr_self_circular),
+	};
+
+	hdr1.next_entry = uring_ptr_to_u64(&hdr2);
+	hdr2.next_entry = uring_ptr_to_u64(&hdr1);
+	ret = io_uring_query(NULL, &hdr1);
+	if (!ret) {
+		fprintf(stderr, "chain loop failed %i\n", ret);
+		return T_EXIT_FAIL;

test/ring-query.c line 199

Quoted 10 lines
test/ring-query.c new file +10 -0
+	}
+
+	ret = io_uring_query(NULL, &hdr_self_circular);
+	if (!ret) {
+		fprintf(stderr, "chain loop failed %i\n", ret);
+		return T_EXIT_FAIL;
+	}
+
+	return T_EXIT_PASS;
+}

It might be a good idea not to test the capping and add a "cycle + SIGKILL" test instead. I'll do that later

Signature · 1 line
Pavel Begunkov
maintainer
#9 lore raw
In reply to Jens Axboe
Quoted 17 lines · Jens Axboe
Quoted 11 lines · Pavel Begunkov

Also introduces a bunch of test/ helper functions.

Pavel Begunkov (6):
tests: test the query interface
tests: add t_submit_and_wait_single helper
tests: introduce t_iovec_data_length helper
tests: add t_sqe_prep_cmd helper
tests: add helper for iov data verification
tests: add mock file based tests

[...]

Applied, thanks!

[1/6] tests: test the query interface

Quoted 11 lines
      commit: 7e565c0116ba6e0cd1bce3a42409b31fd4dd47d3
[2/6] tests: add t_submit_and_wait_single helper
      commit: f1fc45cbcdcd35064b2fbe3eab6a2b89fb335ec6
[3/6] tests: introduce t_iovec_data_length helper
      commit: 7a936a80be37f50a1851379aa0592eeb3b42a9a1
[4/6] tests: add t_sqe_prep_cmd helper
      commit: 7d3773fd9e5352b113b7d425aa5708acdd48d3c0
[5/6] tests: add helper for iov data verification
      commit: 9e69daf86de39c9b4e70c2dd23e4046293585f34
[6/6] tests: add mock file based tests
      commit: d5673a9b4ad074745e28bf7ddad3692115da01fd

I noticed that there's no man page additions for this. Can you add something for IORING_REGISTER_QUERY? Might not be a bad idea to add a helper for this so applications don't have to use io_uring_register(), and then the documentation for how to use the query API could just go in there and just get referenced from io_uring_register.2 rather than put all of it in there.

Signature · 1 line
Jens Axboe
#10 lore raw
Quoted 1 line · Jens Axboe

On 9/19/25 7:15 AM, Jens Axboe wrote:

...>> [1/6] tests: test the query interface

Quoted 12 lines
Quoted 11 lines
       commit: 7e565c0116ba6e0cd1bce3a42409b31fd4dd47d3
[2/6] tests: add t_submit_and_wait_single helper
       commit: f1fc45cbcdcd35064b2fbe3eab6a2b89fb335ec6
[3/6] tests: introduce t_iovec_data_length helper
       commit: 7a936a80be37f50a1851379aa0592eeb3b42a9a1
[4/6] tests: add t_sqe_prep_cmd helper
       commit: 7d3773fd9e5352b113b7d425aa5708acdd48d3c0
[5/6] tests: add helper for iov data verification
       commit: 9e69daf86de39c9b4e70c2dd23e4046293585f34
[6/6] tests: add mock file based tests
       commit: d5673a9b4ad074745e28bf7ddad3692115da01fd
Quoted 6 lines

I noticed that there's no man page additions for this. Can you add something for IORING_REGISTER_QUERY? Might not be a bad idea to add a helper for this so applications don't have to use io_uring_register(), and then the documentation for how to use the query API could just go in there and just get referenced from io_uring_register.2 rather than put all of it in there.

Will take a bit to do, but that should be fine as it's not yet released and will take at least few releases to really become useful.

Signature · 1 line
Pavel Begunkov
maintainer
#11 lore raw
Quoted 22 lines · Pavel Begunkov
Quoted 1 line · Jens Axboe

On 9/19/25 7:15 AM, Jens Axboe wrote:

...>> [1/6] tests: test the query interface

Quoted 12 lines
Quoted 11 lines
       commit: 7e565c0116ba6e0cd1bce3a42409b31fd4dd47d3
[2/6] tests: add t_submit_and_wait_single helper
       commit: f1fc45cbcdcd35064b2fbe3eab6a2b89fb335ec6
[3/6] tests: introduce t_iovec_data_length helper
       commit: 7a936a80be37f50a1851379aa0592eeb3b42a9a1
[4/6] tests: add t_sqe_prep_cmd helper
       commit: 7d3773fd9e5352b113b7d425aa5708acdd48d3c0
[5/6] tests: add helper for iov data verification
       commit: 9e69daf86de39c9b4e70c2dd23e4046293585f34
[6/6] tests: add mock file based tests
       commit: d5673a9b4ad074745e28bf7ddad3692115da01fd
Quoted 6 lines

I noticed that there's no man page additions for this. Can you add something for IORING_REGISTER_QUERY? Might not be a bad idea to add a helper for this so applications don't have to use io_uring_register(), and then the documentation for how to use the query API could just go in there and just get referenced from io_uring_register.2 rather than put all of it in there.

Quoted 3 lines

Will take a bit to do, but that should be fine as it's not yet released and will take at least few releases to really become useful.

Yeah that's fine, if we can make the next liburing release with the query helper and man page that is certainly sufficient.

Signature · 1 line
Jens Axboe