public inbox for io-uring@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH liburing v3 1/1] tests: test io_uring bpf ops
@ 2026-03-18 17:36 Pavel Begunkov
  2026-03-18 17:43 ` Pavel Begunkov
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Pavel Begunkov @ 2026-03-18 17:36 UTC (permalink / raw)
  To: io-uring; +Cc: asml.silence, axboe

Add some BPF struct ops io_uring tests/examples, one is issuing nops in
a loop, the other copies a file.

Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
---

v3: Fix Makefile choosing a wrong rule
    Fix naming issues
    Probe toolchain support
v2: reworked Makefile, added bpf_cp

 configure                 |  49 +++++++++++++
 test/Makefile             |  36 +++++++++-
 test/bpf-progs/cp.bpf.c   | 142 ++++++++++++++++++++++++++++++++++++++
 test/bpf-progs/nops.bpf.c |  99 ++++++++++++++++++++++++++
 test/bpf_cp.c             | 138 ++++++++++++++++++++++++++++++++++++
 test/bpf_nops.c           |  99 ++++++++++++++++++++++++++
 6 files changed, 562 insertions(+), 1 deletion(-)
 create mode 100644 test/bpf-progs/cp.bpf.c
 create mode 100644 test/bpf-progs/nops.bpf.c
 create mode 100644 test/bpf_cp.c
 create mode 100644 test/bpf_nops.c

diff --git a/configure b/configure
index af0e10ab..3a08e0f1 100755
--- a/configure
+++ b/configure
@@ -22,6 +22,8 @@ for opt do
   ;;
   --datadir=*) datadir="$optarg"
   ;;
+  --vmlinux=*) vmlinux="$optarg"
+  ;;
   --cc=*) cc="$optarg"
   ;;
   --cxx=*) cxx="$optarg"
@@ -468,6 +470,53 @@ if compile_prog "" "" "idtype_t"; then
 fi
 print_config "has_idtype_t" "$has_idtype_t"
 
+##########################################
+# Check for BPF toolchain
+
+has_bpftool="no"
+if command -v bpftool >/dev/null 2>&1; then
+  has_bpftool="yes"
+fi
+
+tmp_bpf_c=$(mktemp /tmp/test_bpfXXXX.c)
+tmp_bpf_o=$(mktemp /tmp/test_bpfXXXX.o)
+
+cat > "$tmp_bpf_c" << EOF
+int tmp = 0;
+EOF
+
+has_bpf_clang="no"
+if clang -target bpf -mcpu=v4 -c "$tmp_bpf_c" -o "$tmp_bpf_o"; then
+  has_bpf_clang="yes"
+fi
+rm "$tmp_bpf_c" "$tmp_bpf_o"
+
+has_libbpf="no"
+if pkg-config --exists libbpf; then
+  has_libbpf="yes"
+fi
+
+print_config "has_bpftool" "$has_bpftool"
+print_config "has_bpf_clang" "$has_bpf_clang"
+print_config "has_libbpf" "$has_libbpf"
+
+if test $vmlinux == ""; then
+  vmlinux="/sys/kernel/btf/vmlinux"
+else
+  vmlinux=$(realpath $vmlinux)
+fi
+print_and_output_mak bpf_vmlinux_path $vmlinux
+
+has_bpf_loop_ops="no"
+if test "$has_bpftool" == "yes" && test "$has_bpf_clang" == yes && test "$has_libbpf" == yes; then
+  if bpftool btf dump file $vmlinux format c 2>&1 | grep -qF bpf_io_uring_submit_sqes; then
+      has_bpf_loop_ops="yes"
+      output_sym "CONFIG_HAVE_BPF_LOOP_OPS"
+  fi
+fi
+
+print_config "has_bpf_loop_ops" "$has_bpf_loop_ops"
+
 #############################################################################
 liburing_nolibc="no"
 if test "$use_libc" != "yes"; then
diff --git a/test/Makefile b/test/Makefile
index 10c3bcfa..377d06cf 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -1,6 +1,11 @@
 prefix ?= /usr
 datadir ?= $(prefix)/share
 
+CLANG ?= clang
+BPFTOOL ?= bpftool
+BPF_PROGS_DIR = bpf-progs
+BPF_OUTPUT = output/bpf
+
 INSTALL=install
 
 ifneq ($(MAKECMDGOALS),clean)
@@ -312,11 +317,19 @@ ifdef CONFIG_HAVE_CXX
 endif
 all_targets += sq-full-cpp.t
 
+bpf_test_srcs := bpf_nops.c bpf_cp.c
+bpf_progs := $(patsubst bpf_%.c, %.bpf.c, $(bpf_test_srcs))
+bpf_test_targets :=
+
+ifeq ($(CONFIG_HAVE_BPF_LOOP_OPS),y)
+	bpf_test_targets := $(patsubst %.c,%.tt,$(bpf_test_srcs))
+endif
 
 test_targets := $(patsubst %.c,%,$(test_srcs))
 test_targets := $(patsubst %.cc,%,$(test_targets))
 run_test_targets := $(patsubst %,%.run_test,$(test_targets))
 test_targets := $(patsubst %,%.t,$(test_targets))
+test_targets += $(bpf_test_targets)
 all_targets += $(test_targets)
 helpers = helpers.o
 
@@ -338,6 +351,9 @@ LIBURING := $(shell if [ -e ../src/liburing.a ]; then echo ../src/liburing.a; fi
 %.t: %.c $(helpers) helpers.h $(LIBURING)
 	$(QUIET_CC)$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ $< $(helpers) $(LDFLAGS)
 
+bpf_%.tt: bpf_%.c $(helpers) helpers.h $(LIBURING) $(BPF_OUTPUT)/%.bpf.o $(BPF_OUTPUT)/%.skel.h
+	$(QUIET_CC)$(CC) $(CPPFLAGS) $(CFLAGS) -I$(BPF_OUTPUT) -o $@ $< $(helpers) $(LDFLAGS) -lbpf
+
 #
 # Clang++ is not happy with -Wmissing-prototypes:
 #
@@ -350,6 +366,24 @@ LIBURING := $(shell if [ -e ../src/liburing.a ]; then echo ../src/liburing.a; fi
 	$(patsubst -Wmissing-prototypes,,$(CXXFLAGS)) \
 	-o $@ $< $(helpers) $(LDFLAGS)
 
+CLANG_BPF_SYS_INCLUDES ?= $(shell $(CLANG) -v -E - </dev/null 2>&1 \
+	| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')
+
+$(BPF_OUTPUT)/vmlinux.h:
+	mkdir -p $(BPF_OUTPUT)
+	$(BPFTOOL) btf dump file $(bpf_vmlinux_path) format c > $@
+
+# Build BPF code
+$(BPF_OUTPUT)/%.bpf.o: $(BPF_PROGS_DIR)/%.bpf.c $(wildcard %.h) $(BPF_OUTPUT)/vmlinux.h
+	mkdir -p ${BPF_OUTPUT}
+	$(QUIET_CC)$(CLANG) -g -O2 -target bpf \
+		     -I$(BPF_OUTPUT) $(CLANG_BPF_SYS_INCLUDES) \
+		     -Wno-missing-declarations \
+		     -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@) -mcpu=v4
+	$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@)
+
+$(BPF_OUTPUT)/%.skel.h: $(BPF_OUTPUT)/%.bpf.o $(BPF_OUTPUT)/vmlinux.h
+	$(BPFTOOL) gen skeleton $< > $@
 
 install: $(test_targets) runtests.sh runtests-loop.sh
 	$(INSTALL) -D -d -m 755 $(datadir)/liburing-test/
@@ -361,7 +395,7 @@ uninstall:
 	@rm -rf $(datadir)/liburing-test/
 
 clean:
-	@rm -f $(all_targets) helpers.o output/*
+	@rm -rf $(all_targets) helpers.o output/*
 	@rm -rf output/
 
 runtests: all
diff --git a/test/bpf-progs/cp.bpf.c b/test/bpf-progs/cp.bpf.c
new file mode 100644
index 00000000..42aee2cd
--- /dev/null
+++ b/test/bpf-progs/cp.bpf.c
@@ -0,0 +1,142 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#include "vmlinux.h"
+#include <linux/errno.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char LICENSE[] SEC("license") = "Dual BSD/GPL";
+
+enum {
+	REQ_TOKEN_READ = 1,
+	REQ_TOKEN_WRITE
+};
+
+const volatile unsigned cq_hdr_offset;
+const volatile unsigned sq_hdr_offset;
+const volatile unsigned cqes_offset;
+const volatile unsigned sq_entries;
+const volatile unsigned cq_entries;
+
+int input_fd;
+int output_fd;
+void *buffer_uptr;
+unsigned nr_infligt;
+unsigned cur_offset;
+size_t buffer_size;
+int cp_result;
+
+#define t_min(a, b) ((a) < (b) ? (a) : (b))
+
+static inline void sqe_prep_rw(struct io_uring_sqe *sqe, unsigned opcode,
+				   int fd, void *addr,
+				   __u32 len, __u64 offset)
+{
+	*sqe = (struct io_uring_sqe){};
+	sqe->opcode = opcode;
+	sqe->fd = fd;
+	sqe->off = offset;
+	sqe->addr = (__u64)(unsigned long)addr;
+	sqe->len = len;
+}
+
+static int issue_next_req(struct io_ring_ctx *ring, struct io_uring_sqe *sqes,
+			  int type, size_t size)
+{
+	struct io_uring_sqe *sqe = sqes;
+	__u8 req_type;
+	int fd, ret;
+
+	if (type == REQ_TOKEN_READ) {
+		req_type = IORING_OP_READ;
+		fd = input_fd;
+	} else {
+		req_type = IORING_OP_WRITE;
+		fd = output_fd;
+	}
+
+	sqe_prep_rw(sqes, req_type, fd, buffer_uptr, size, cur_offset);
+	sqe->user_data = type;
+
+	ret = bpf_io_uring_submit_sqes(ring, 1);
+	if (ret != 1) {
+		cp_result = ret;
+		return ret < 0 ? ret : -EFAULT;
+	}
+	return 0;
+}
+
+SEC("struct_ops.s/cp_loop_step")
+int BPF_PROG(cp_loop_step, struct io_ring_ctx *ring, struct iou_loop_params *ls)
+{
+	struct io_uring_sqe *sqes;
+	struct io_uring_cqe *cqes;
+	struct io_uring *cq_hdr;
+	void *rings;
+	int ret;
+
+	sqes = (void *)bpf_io_uring_get_region(ring, IOU_REGION_SQ,
+				sq_entries * sizeof(struct io_uring_sqe));
+	rings = (void *)bpf_io_uring_get_region(ring, IOU_REGION_CQ,
+				cqes_offset + cq_entries * sizeof(struct io_uring_cqe));
+	if (!rings || !sqes)
+		return IOU_LOOP_STOP;
+	cq_hdr = rings + cq_hdr_offset;
+	cqes = rings + cqes_offset;
+
+	if (!nr_infligt) {
+		nr_infligt++;
+		ret = issue_next_req(ring, sqes, REQ_TOKEN_READ,
+				     buffer_size);
+		if (ret)
+			return IOU_LOOP_STOP;
+	}
+
+	if (cq_hdr->tail != cq_hdr->head) {
+		struct io_uring_cqe *cqe;
+
+		if (cq_hdr->tail - cq_hdr->head != 1) {
+			cp_result = -ERANGE;
+			return IOU_LOOP_STOP;
+		}
+
+		cqe = &cqes[cq_hdr->head & (cq_entries - 1)];
+		if (cqe->res < 0) {
+			cp_result = cqe->res;
+			return IOU_LOOP_STOP;
+		}
+
+		switch (cqe->user_data) {
+		case REQ_TOKEN_READ:
+			if (cqe->res == 0) {
+				cp_result = 0;
+				return IOU_LOOP_STOP;
+			}
+			ret = issue_next_req(ring, sqes, REQ_TOKEN_WRITE,
+					     cqe->res);
+			if (ret)
+				return IOU_LOOP_STOP;
+			break;
+		case REQ_TOKEN_WRITE:
+			cur_offset += cqe->res;
+			ret = issue_next_req(ring, sqes, REQ_TOKEN_READ,
+					     buffer_size);
+			if (ret)
+				return IOU_LOOP_STOP;
+			break;
+		default:
+			bpf_printk("invalid token\n");
+			cp_result = -EINVAL;
+			return IOU_LOOP_STOP;
+		};
+
+		cq_hdr->head++;
+	}
+
+	ls->cq_wait_idx = cq_hdr->head + 1;
+	return IOU_LOOP_CONTINUE;
+}
+
+SEC(".struct_ops.link")
+struct io_uring_bpf_ops cp_ops = {
+	.loop_step = (void *)cp_loop_step,
+};
diff --git a/test/bpf-progs/nops.bpf.c b/test/bpf-progs/nops.bpf.c
new file mode 100644
index 00000000..00075bb6
--- /dev/null
+++ b/test/bpf-progs/nops.bpf.c
@@ -0,0 +1,99 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <linux/errno.h>
+
+char LICENSE[] SEC("license") = "Dual BSD/GPL";
+
+#define REQ_TOKEN 0xabba1741
+
+const unsigned max_inflight = 8;
+const volatile unsigned cq_hdr_offset;
+const volatile unsigned sq_hdr_offset;
+const volatile unsigned cqes_offset;
+const volatile unsigned cq_entries;
+const volatile unsigned sq_entries;
+
+unsigned reqs_inflight = 0;
+int reqs_to_run;
+
+#define t_min(a, b) ((a) < (b) ? (a) : (b))
+
+static unsigned nr_to_submit(void)
+{
+	unsigned to_submit = 0;
+	unsigned inflight = reqs_inflight;
+
+	if (inflight < max_inflight) {
+		to_submit = max_inflight - inflight;
+		to_submit = t_min(to_submit, reqs_to_run - inflight);
+	}
+	return to_submit;
+}
+
+SEC("struct_ops.s/nops_loop_step")
+int BPF_PROG(nops_loop_step, struct io_ring_ctx *ring, struct iou_loop_params *ls)
+{
+	struct io_uring_sqe *sqes;
+	struct io_uring_cqe *cqes;
+	struct io_uring *cq_hdr;
+	unsigned to_submit;
+	unsigned to_wait;
+	unsigned nr_cqes;
+	void *rings;
+	int ret, i;
+
+	sqes = (void *)bpf_io_uring_get_region(ring, IOU_REGION_SQ,
+				sq_entries * sizeof(struct io_uring_sqe));
+	rings = (void *)bpf_io_uring_get_region(ring, IOU_REGION_CQ,
+				cqes_offset + cq_entries * sizeof(struct io_uring_cqe));
+	if (!rings || !sqes)
+		return IOU_LOOP_STOP;
+	cq_hdr = rings + cq_hdr_offset;
+	cqes = rings + cqes_offset;
+
+	to_submit = nr_to_submit();
+	if (to_submit) {
+		for (i = 0; i < to_submit; i++) {
+			struct io_uring_sqe *sqe = &sqes[i];
+
+			*sqe = (struct io_uring_sqe){};
+			sqe->opcode = IORING_OP_NOP;
+			sqe->user_data = REQ_TOKEN;
+		}
+
+		ret = bpf_io_uring_submit_sqes(ring, to_submit);
+		if (ret != to_submit)
+			return IOU_LOOP_STOP;
+		reqs_inflight += to_submit;
+	}
+
+	nr_cqes = cq_hdr->tail - cq_hdr->head;
+	nr_cqes = t_min(nr_cqes, max_inflight);
+	for (i = 0; i < nr_cqes; i++) {
+		struct io_uring_cqe *cqe = &cqes[cq_hdr->head & (cq_entries - 1)];
+
+		if (cqe->user_data != REQ_TOKEN)
+			return IOU_LOOP_STOP;
+		cq_hdr->head++;
+	}
+
+	reqs_inflight -= nr_cqes;
+	reqs_to_run -= nr_cqes;
+
+	if (reqs_to_run <= 0 && !reqs_inflight)
+		return IOU_LOOP_STOP;
+
+	to_wait = reqs_inflight;
+	/* Don't sleep if there are still CQEs left */
+	if (cq_hdr->tail != cq_hdr->head)
+		to_wait = 0;
+	ls->cq_wait_idx = cq_hdr->head + to_wait;
+	return IOU_LOOP_CONTINUE;
+}
+
+SEC(".struct_ops.link")
+struct io_uring_bpf_ops nops_ops = {
+	.loop_step = (void *)nops_loop_step,
+};
diff --git a/test/bpf_cp.c b/test/bpf_cp.c
new file mode 100644
index 00000000..d04fdcb6
--- /dev/null
+++ b/test/bpf_cp.c
@@ -0,0 +1,138 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#include <linux/stddef.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <bpf/libbpf.h>
+
+#include "liburing.h"
+#include "cp.skel.h"
+#include "helpers.h"
+
+static struct cp_bpf *skel;
+static struct bpf_link *cp_bpf_link;
+
+static char *in_fname;
+static char *out_fname;
+
+static size_t buffer_size = 4096;
+static int input_fd;
+static int output_fd;
+static void *buffer;
+
+#define CQ_ENTRIES 8
+#define SQ_ENTRIES 8
+
+static int setup_ring_ops(struct io_uring *ring)
+{
+	struct io_uring_params params;
+	int ret;
+
+	memset(&params, 0, sizeof(params));
+	params.cq_entries = CQ_ENTRIES;
+	params.flags = IORING_SETUP_SINGLE_ISSUER |
+			IORING_SETUP_DEFER_TASKRUN |
+			IORING_SETUP_NO_SQARRAY |
+			IORING_SETUP_CQSIZE |
+			IORING_SETUP_SQ_REWIND;
+
+	ret = t_create_ring_params(SQ_ENTRIES, ring, &params);
+	if (ret == T_SETUP_SKIP) {
+		printf("Can't setup a ring, skip\n");
+		return T_EXIT_SKIP;
+	}
+	if (ret != T_SETUP_OK)
+		return T_EXIT_FAIL;
+
+	skel = cp_bpf__open();
+	if (!skel) {
+		fprintf(stderr, "can't generate skeleton\n");
+		return T_EXIT_FAIL;
+	}
+
+	skel->struct_ops.cp_ops->ring_fd = ring->ring_fd;
+	skel->rodata->sq_hdr_offset = params.sq_off.head;
+	skel->rodata->cq_hdr_offset = params.cq_off.head;
+	skel->rodata->cqes_offset = params.cq_off.cqes;
+	skel->rodata->cq_entries = CQ_ENTRIES;
+	skel->rodata->sq_entries = SQ_ENTRIES;
+	skel->bss->input_fd = input_fd;
+	skel->bss->output_fd = output_fd;
+	skel->bss->buffer_uptr = buffer;
+	skel->bss->buffer_size = buffer_size;
+	skel->bss->cp_result = -EBUSY;
+
+	ret = cp_bpf__load(skel);
+	if (ret) {
+		if (ret == -ESRCH) {
+			printf("io_uring BPF ops are not supported\n");
+			return T_EXIT_SKIP;
+		}
+		fprintf(stderr, "failed to load skeleton\n");
+		return T_EXIT_FAIL;
+	}
+
+	cp_bpf_link = bpf_map__attach_struct_ops(skel->maps.cp_ops);
+	if (!cp_bpf_link) {
+		fprintf(stderr, "failed to attach ops\n");
+		return T_EXIT_FAIL;
+	}
+	return T_EXIT_PASS;
+}
+
+int main(int argc, char *argv[])
+{
+	struct io_uring ring;
+	size_t file_size;
+	struct stat st;
+	int ret;
+
+	if (argc != 3)
+		return 0;
+
+	in_fname = argv[1];
+	out_fname = argv[2];
+
+	input_fd = open(in_fname, O_RDONLY | O_DIRECT);
+	output_fd = open(out_fname, O_WRONLY | O_DIRECT | O_CREAT, 0644);
+	if (input_fd < 0 || output_fd < 0) {
+		fprintf(stderr, "can't open files");
+		return T_EXIT_FAIL;
+	}
+	if (fstat(input_fd, &st) == -1) {
+		fprintf(stderr, "stat failed\n");
+		return T_EXIT_FAIL;
+	}
+	file_size = st.st_size;
+
+	buffer = aligned_alloc(4096, buffer_size);
+	if (!buffer) {
+		fprintf(stderr, "can't allocate buffer\n");
+		return T_EXIT_FAIL;
+	}
+
+	ret = setup_ring_ops(&ring);
+	if (ret != T_EXIT_PASS)
+		return ret;
+
+	if (ftruncate(output_fd, file_size) == -1) {
+		fprintf(stderr, "ftruncate failed\n");
+		return T_EXIT_FAIL;
+	}
+
+	ret = io_uring_enter(ring.ring_fd, 0, 0, IORING_ENTER_GETEVENTS, NULL);
+	if (ret) {
+		fprintf(stderr, "run failed\n");
+		return T_EXIT_FAIL;
+	}
+
+	ret = skel->bss->cp_result;
+	if (ret) {
+		fprintf(stderr, "cp failed %i\n", ret);
+		return T_EXIT_FAIL;
+	}
+
+	bpf_link__destroy(cp_bpf_link);
+	cp_bpf__destroy(skel);
+	return 0;
+}
diff --git a/test/bpf_nops.c b/test/bpf_nops.c
new file mode 100644
index 00000000..bdd4df47
--- /dev/null
+++ b/test/bpf_nops.c
@@ -0,0 +1,99 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#include <linux/stddef.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <bpf/libbpf.h>
+
+#include "liburing.h"
+#include "nops.skel.h"
+#include "helpers.h"
+
+static struct nops_bpf *skel;
+static struct bpf_link *nops_bpf_link;
+
+#define CQ_ENTRIES 8
+#define SQ_ENTRIES 8
+#define NR_ITERS 1000
+
+static int setup_ring_ops(struct io_uring *ring)
+{
+	struct io_uring_params params;
+	int ret;
+
+	memset(&params, 0, sizeof(params));
+	params.cq_entries = CQ_ENTRIES;
+	params.flags = IORING_SETUP_SINGLE_ISSUER |
+			IORING_SETUP_DEFER_TASKRUN |
+			IORING_SETUP_NO_SQARRAY |
+			IORING_SETUP_CQSIZE |
+			IORING_SETUP_SQ_REWIND;
+
+	ret = t_create_ring_params(SQ_ENTRIES, ring, &params);
+	if (ret == T_SETUP_SKIP) {
+		printf("Can't setup a ring, skip\n");
+		return T_EXIT_SKIP;
+	}
+	if (ret != T_SETUP_OK)
+		return T_EXIT_FAIL;
+
+	skel = nops_bpf__open();
+	if (!skel) {
+		fprintf(stderr, "can't generate skeleton\n");
+		return T_EXIT_FAIL;
+	}
+
+	skel->struct_ops.nops_ops->ring_fd = ring->ring_fd;
+	skel->bss->reqs_to_run = NR_ITERS;
+	skel->rodata->sq_hdr_offset = params.sq_off.head;
+	skel->rodata->cq_hdr_offset = params.cq_off.head;
+	skel->rodata->cqes_offset = params.cq_off.cqes;
+	skel->rodata->cq_entries = CQ_ENTRIES;
+	skel->rodata->sq_entries = SQ_ENTRIES;
+
+	ret = nops_bpf__load(skel);
+	if (ret) {
+		if (ret == -ESRCH) {
+			printf("io_uring BPF ops are not supported\n");
+			return T_EXIT_SKIP;
+		}
+		fprintf(stderr, "failed to load skeleton\n");
+		return T_EXIT_FAIL;
+	}
+
+	nops_bpf_link = bpf_map__attach_struct_ops(skel->maps.nops_ops);
+	if (!nops_bpf_link) {
+		fprintf(stderr, "failed to attach ops\n");
+		return T_EXIT_FAIL;
+	}
+	return T_EXIT_PASS;
+}
+
+int main()
+{
+	struct io_uring ring;
+	unsigned left;
+	int ret;
+
+	ret = setup_ring_ops(&ring);
+	if (ret != T_EXIT_PASS)
+		return ret;
+
+	ret = io_uring_enter(ring.ring_fd, 0, 0, IORING_ENTER_GETEVENTS, NULL);
+	if (ret) {
+		fprintf(stderr, "run failed\n");
+		return T_EXIT_FAIL;
+	}
+
+	left = skel->bss->reqs_to_run;
+	if (left) {
+		fprintf(stderr, "Run failed, couldn't submit all nops %i / %i\n",
+			NR_ITERS - left, NR_ITERS);
+		return T_EXIT_FAIL;
+	}
+
+	bpf_link__destroy(nops_bpf_link);
+	nops_bpf__destroy(skel);
+	io_uring_queue_exit(&ring);
+	return T_EXIT_PASS;
+}
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH liburing v3 1/1] tests: test io_uring bpf ops
  2026-03-18 17:36 [PATCH liburing v3 1/1] tests: test io_uring bpf ops Pavel Begunkov
@ 2026-03-18 17:43 ` Pavel Begunkov
  2026-03-18 18:24   ` Jens Axboe
  2026-03-18 20:59 ` Pavel Begunkov
  2026-03-18 21:01 ` Pavel Begunkov
  2 siblings, 1 reply; 7+ messages in thread
From: Pavel Begunkov @ 2026-03-18 17:43 UTC (permalink / raw)
  To: io-uring; +Cc: axboe

On 3/18/26 17:36, Pavel Begunkov wrote:
> Add some BPF struct ops io_uring tests/examples, one is issuing nops in
> a loop, the other copies a file.

I needed to conditionally compile based on whether vmlinux.h contains
io_uring BPF definitions, so now configure probes it by generating a
temp vmlinux.h. And since I want to be able to pass a path to the
target vmlinux, it also became a configure parameter. Not sure if there
is a better way to handle that.

-- 
Pavel Begunkov


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH liburing v3 1/1] tests: test io_uring bpf ops
  2026-03-18 17:43 ` Pavel Begunkov
@ 2026-03-18 18:24   ` Jens Axboe
  2026-03-18 18:28     ` Jens Axboe
  2026-03-18 20:57     ` Pavel Begunkov
  0 siblings, 2 replies; 7+ messages in thread
From: Jens Axboe @ 2026-03-18 18:24 UTC (permalink / raw)
  To: Pavel Begunkov, io-uring

On 3/18/26 11:43 AM, Pavel Begunkov wrote:
> On 3/18/26 17:36, Pavel Begunkov wrote:
>> Add some BPF struct ops io_uring tests/examples, one is issuing nops in
>> a loop, the other copies a file.
> 
> I needed to conditionally compile based on whether vmlinux.h contains
> io_uring BPF definitions, so now configure probes it by generating a
> temp vmlinux.h. And since I want to be able to pass a path to the
> target vmlinux, it also became a configure parameter. Not sure if there
> is a better way to handle that.

Looks good to me. The configure changes are a bit broken though, you'd
need something like this on top to not have it fail:

diff --git a/configure b/configure
index 3a08e0f12448..7aea26fa7681 100755
--- a/configure
+++ b/configure
@@ -500,15 +500,15 @@ print_config "has_bpftool" "$has_bpftool"
 print_config "has_bpf_clang" "$has_bpf_clang"
 print_config "has_libbpf" "$has_libbpf"
 
-if test $vmlinux == ""; then
+if test -z "$vmlinux"; then
   vmlinux="/sys/kernel/btf/vmlinux"
 else
-  vmlinux=$(realpath $vmlinux)
+  vmlinux=$(realpath "$vmlinux")
 fi
 print_and_output_mak bpf_vmlinux_path $vmlinux
 
 has_bpf_loop_ops="no"
-if test "$has_bpftool" == "yes" && test "$has_bpf_clang" == yes && test "$has_libbpf" == yes; then
+if test "$has_bpftool" = "yes" && test "$has_bpf_clang" = "yes" && test "$has_libbpf" = "yes"; then
   if bpftool btf dump file $vmlinux format c 2>&1 | grep -qF bpf_io_uring_submit_sqes; then
       has_bpf_loop_ops="yes"
       output_sym "CONFIG_HAVE_BPF_LOOP_OPS"

With that, it does detect it fine here. However, it ends in misery with:

axboe@m2max ~/gi/liburing (master)> make                                        6.490s
make[1]: Entering directory '/home/axboe/git/liburing/src'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/home/axboe/git/liburing/src'
make[1]: Entering directory '/home/axboe/git/liburing/test'
mkdir -p output/bpf
     CC output/bpf/nops.bpf.o
In file included from bpf-progs/nops.bpf.c:3:
/usr/include/bpf/bpf_helpers.h:318:12: error: conflicting types
      for 'bpf_stream_vprintk'
  318 | extern int bpf_stream_vprintk(int stream_id, const char *fmt__str, const void *args,
      |            ^
output/bpf/vmlinux.h:170697:12: note: previous declaration is
      here
 170697 | extern int bpf_stream_vprintk(int stream_id, const char *fmt__str, const void ...
        |            ^
1 error generated.
make[1]: *** [Makefile:379: output/bpf/nops.bpf.o] Error 1
make[1]: Leaving directory '/home/axboe/git/liburing/test'
make: *** [Makefile:11: all] Error 2


-- 
Jens Axboe

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH liburing v3 1/1] tests: test io_uring bpf ops
  2026-03-18 18:24   ` Jens Axboe
@ 2026-03-18 18:28     ` Jens Axboe
  2026-03-18 20:57     ` Pavel Begunkov
  1 sibling, 0 replies; 7+ messages in thread
From: Jens Axboe @ 2026-03-18 18:28 UTC (permalink / raw)
  To: Pavel Begunkov, io-uring

On 3/18/26 12:24 PM, Jens Axboe wrote:
> On 3/18/26 11:43 AM, Pavel Begunkov wrote:
>> On 3/18/26 17:36, Pavel Begunkov wrote:
>>> Add some BPF struct ops io_uring tests/examples, one is issuing nops in
>>> a loop, the other copies a file.
>>
>> I needed to conditionally compile based on whether vmlinux.h contains
>> io_uring BPF definitions, so now configure probes it by generating a
>> temp vmlinux.h. And since I want to be able to pass a path to the
>> target vmlinux, it also became a configure parameter. Not sure if there
>> is a better way to handle that.
> 
> Looks good to me. The configure changes are a bit broken though, you'd
> need something like this on top to not have it fail:

Oh, and additionally, at least my my debian test boxes, bpftool is in
/usr/sbin/bpftool and hence it won't get detected unless you run
configure as root...

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH liburing v3 1/1] tests: test io_uring bpf ops
  2026-03-18 18:24   ` Jens Axboe
  2026-03-18 18:28     ` Jens Axboe
@ 2026-03-18 20:57     ` Pavel Begunkov
  1 sibling, 0 replies; 7+ messages in thread
From: Pavel Begunkov @ 2026-03-18 20:57 UTC (permalink / raw)
  To: Jens Axboe, io-uring

On 3/18/26 18:24, Jens Axboe wrote:
> On 3/18/26 11:43 AM, Pavel Begunkov wrote:
>> On 3/18/26 17:36, Pavel Begunkov wrote:
...
> axboe@m2max ~/gi/liburing (master)> make                                        6.490s
> make[1]: Entering directory '/home/axboe/git/liburing/src'
> make[1]: Nothing to be done for 'all'.
> make[1]: Leaving directory '/home/axboe/git/liburing/src'
> make[1]: Entering directory '/home/axboe/git/liburing/test'
> mkdir -p output/bpf
>       CC output/bpf/nops.bpf.o
> In file included from bpf-progs/nops.bpf.c:3:
> /usr/include/bpf/bpf_helpers.h:318:12: error: conflicting types

I assume it's an outdated libbpf causing issues. I'll kill
vmlinux.h, should be easier this way.

>        for 'bpf_stream_vprintk'
>    318 | extern int bpf_stream_vprintk(int stream_id, const char *fmt__str, const void *args,
>        |            ^
> output/bpf/vmlinux.h:170697:12: note: previous declaration is
>        here
>   170697 | extern int bpf_stream_vprintk(int stream_id, const char *fmt__str, const void ...
>          |            ^
> 1 error generated.
> make[1]: *** [Makefile:379: output/bpf/nops.bpf.o] Error 1
> make[1]: Leaving directory '/home/axboe/git/liburing/test'
> make: *** [Makefile:11: all] Error 2
> 
> 

-- 
Pavel Begunkov


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH liburing v3 1/1] tests: test io_uring bpf ops
  2026-03-18 17:36 [PATCH liburing v3 1/1] tests: test io_uring bpf ops Pavel Begunkov
  2026-03-18 17:43 ` Pavel Begunkov
@ 2026-03-18 20:59 ` Pavel Begunkov
  2026-03-18 21:01 ` Pavel Begunkov
  2 siblings, 0 replies; 7+ messages in thread
From: Pavel Begunkov @ 2026-03-18 20:59 UTC (permalink / raw)
  To: io-uring; +Cc: asml.silence, axboe

Add some BPF struct ops io_uring tests/examples, one is issuing nops in
a loop, the other copies a file.

Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
---

v3: Fix Makefile choosing a wrong rule
    Fix naming issues
    Probe toolchain support
v2: reworked Makefile, added bpf_cp

 configure                 |  49 +++++++++++++
 test/Makefile             |  36 +++++++++-
 test/bpf-progs/cp.bpf.c   | 142 ++++++++++++++++++++++++++++++++++++++
 test/bpf-progs/nops.bpf.c |  99 ++++++++++++++++++++++++++
 test/bpf_cp.c             | 138 ++++++++++++++++++++++++++++++++++++
 test/bpf_nops.c           |  99 ++++++++++++++++++++++++++
 6 files changed, 562 insertions(+), 1 deletion(-)
 create mode 100644 test/bpf-progs/cp.bpf.c
 create mode 100644 test/bpf-progs/nops.bpf.c
 create mode 100644 test/bpf_cp.c
 create mode 100644 test/bpf_nops.c

diff --git a/configure b/configure
index af0e10ab..3a08e0f1 100755
--- a/configure
+++ b/configure
@@ -22,6 +22,8 @@ for opt do
   ;;
   --datadir=*) datadir="$optarg"
   ;;
+  --vmlinux=*) vmlinux="$optarg"
+  ;;
   --cc=*) cc="$optarg"
   ;;
   --cxx=*) cxx="$optarg"
@@ -468,6 +470,53 @@ if compile_prog "" "" "idtype_t"; then
 fi
 print_config "has_idtype_t" "$has_idtype_t"
 
+##########################################
+# Check for BPF toolchain
+
+has_bpftool="no"
+if command -v bpftool >/dev/null 2>&1; then
+  has_bpftool="yes"
+fi
+
+tmp_bpf_c=$(mktemp /tmp/test_bpfXXXX.c)
+tmp_bpf_o=$(mktemp /tmp/test_bpfXXXX.o)
+
+cat > "$tmp_bpf_c" << EOF
+int tmp = 0;
+EOF
+
+has_bpf_clang="no"
+if clang -target bpf -mcpu=v4 -c "$tmp_bpf_c" -o "$tmp_bpf_o"; then
+  has_bpf_clang="yes"
+fi
+rm "$tmp_bpf_c" "$tmp_bpf_o"
+
+has_libbpf="no"
+if pkg-config --exists libbpf; then
+  has_libbpf="yes"
+fi
+
+print_config "has_bpftool" "$has_bpftool"
+print_config "has_bpf_clang" "$has_bpf_clang"
+print_config "has_libbpf" "$has_libbpf"
+
+if test $vmlinux == ""; then
+  vmlinux="/sys/kernel/btf/vmlinux"
+else
+  vmlinux=$(realpath $vmlinux)
+fi
+print_and_output_mak bpf_vmlinux_path $vmlinux
+
+has_bpf_loop_ops="no"
+if test "$has_bpftool" == "yes" && test "$has_bpf_clang" == yes && test "$has_libbpf" == yes; then
+  if bpftool btf dump file $vmlinux format c 2>&1 | grep -qF bpf_io_uring_submit_sqes; then
+      has_bpf_loop_ops="yes"
+      output_sym "CONFIG_HAVE_BPF_LOOP_OPS"
+  fi
+fi
+
+print_config "has_bpf_loop_ops" "$has_bpf_loop_ops"
+
 #############################################################################
 liburing_nolibc="no"
 if test "$use_libc" != "yes"; then
diff --git a/test/Makefile b/test/Makefile
index 10c3bcfa..377d06cf 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -1,6 +1,11 @@
 prefix ?= /usr
 datadir ?= $(prefix)/share
 
+CLANG ?= clang
+BPFTOOL ?= bpftool
+BPF_PROGS_DIR = bpf-progs
+BPF_OUTPUT = output/bpf
+
 INSTALL=install
 
 ifneq ($(MAKECMDGOALS),clean)
@@ -312,11 +317,19 @@ ifdef CONFIG_HAVE_CXX
 endif
 all_targets += sq-full-cpp.t
 
+bpf_test_srcs := bpf_nops.c bpf_cp.c
+bpf_progs := $(patsubst bpf_%.c, %.bpf.c, $(bpf_test_srcs))
+bpf_test_targets :=
+
+ifeq ($(CONFIG_HAVE_BPF_LOOP_OPS),y)
+	bpf_test_targets := $(patsubst %.c,%.tt,$(bpf_test_srcs))
+endif
 
 test_targets := $(patsubst %.c,%,$(test_srcs))
 test_targets := $(patsubst %.cc,%,$(test_targets))
 run_test_targets := $(patsubst %,%.run_test,$(test_targets))
 test_targets := $(patsubst %,%.t,$(test_targets))
+test_targets += $(bpf_test_targets)
 all_targets += $(test_targets)
 helpers = helpers.o
 
@@ -338,6 +351,9 @@ LIBURING := $(shell if [ -e ../src/liburing.a ]; then echo ../src/liburing.a; fi
 %.t: %.c $(helpers) helpers.h $(LIBURING)
 	$(QUIET_CC)$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ $< $(helpers) $(LDFLAGS)
 
+bpf_%.tt: bpf_%.c $(helpers) helpers.h $(LIBURING) $(BPF_OUTPUT)/%.bpf.o $(BPF_OUTPUT)/%.skel.h
+	$(QUIET_CC)$(CC) $(CPPFLAGS) $(CFLAGS) -I$(BPF_OUTPUT) -o $@ $< $(helpers) $(LDFLAGS) -lbpf
+
 #
 # Clang++ is not happy with -Wmissing-prototypes:
 #
@@ -350,6 +366,24 @@ LIBURING := $(shell if [ -e ../src/liburing.a ]; then echo ../src/liburing.a; fi
 	$(patsubst -Wmissing-prototypes,,$(CXXFLAGS)) \
 	-o $@ $< $(helpers) $(LDFLAGS)
 
+CLANG_BPF_SYS_INCLUDES ?= $(shell $(CLANG) -v -E - </dev/null 2>&1 \
+	| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')
+
+$(BPF_OUTPUT)/vmlinux.h:
+	mkdir -p $(BPF_OUTPUT)
+	$(BPFTOOL) btf dump file $(bpf_vmlinux_path) format c > $@
+
+# Build BPF code
+$(BPF_OUTPUT)/%.bpf.o: $(BPF_PROGS_DIR)/%.bpf.c $(wildcard %.h) $(BPF_OUTPUT)/vmlinux.h
+	mkdir -p ${BPF_OUTPUT}
+	$(QUIET_CC)$(CLANG) -g -O2 -target bpf \
+		     -I$(BPF_OUTPUT) $(CLANG_BPF_SYS_INCLUDES) \
+		     -Wno-missing-declarations \
+		     -c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@) -mcpu=v4
+	$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@)
+
+$(BPF_OUTPUT)/%.skel.h: $(BPF_OUTPUT)/%.bpf.o $(BPF_OUTPUT)/vmlinux.h
+	$(BPFTOOL) gen skeleton $< > $@
 
 install: $(test_targets) runtests.sh runtests-loop.sh
 	$(INSTALL) -D -d -m 755 $(datadir)/liburing-test/
@@ -361,7 +395,7 @@ uninstall:
 	@rm -rf $(datadir)/liburing-test/
 
 clean:
-	@rm -f $(all_targets) helpers.o output/*
+	@rm -rf $(all_targets) helpers.o output/*
 	@rm -rf output/
 
 runtests: all
diff --git a/test/bpf-progs/cp.bpf.c b/test/bpf-progs/cp.bpf.c
new file mode 100644
index 00000000..42aee2cd
--- /dev/null
+++ b/test/bpf-progs/cp.bpf.c
@@ -0,0 +1,142 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#include "vmlinux.h"
+#include <linux/errno.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char LICENSE[] SEC("license") = "Dual BSD/GPL";
+
+enum {
+	REQ_TOKEN_READ = 1,
+	REQ_TOKEN_WRITE
+};
+
+const volatile unsigned cq_hdr_offset;
+const volatile unsigned sq_hdr_offset;
+const volatile unsigned cqes_offset;
+const volatile unsigned sq_entries;
+const volatile unsigned cq_entries;
+
+int input_fd;
+int output_fd;
+void *buffer_uptr;
+unsigned nr_infligt;
+unsigned cur_offset;
+size_t buffer_size;
+int cp_result;
+
+#define t_min(a, b) ((a) < (b) ? (a) : (b))
+
+static inline void sqe_prep_rw(struct io_uring_sqe *sqe, unsigned opcode,
+				   int fd, void *addr,
+				   __u32 len, __u64 offset)
+{
+	*sqe = (struct io_uring_sqe){};
+	sqe->opcode = opcode;
+	sqe->fd = fd;
+	sqe->off = offset;
+	sqe->addr = (__u64)(unsigned long)addr;
+	sqe->len = len;
+}
+
+static int issue_next_req(struct io_ring_ctx *ring, struct io_uring_sqe *sqes,
+			  int type, size_t size)
+{
+	struct io_uring_sqe *sqe = sqes;
+	__u8 req_type;
+	int fd, ret;
+
+	if (type == REQ_TOKEN_READ) {
+		req_type = IORING_OP_READ;
+		fd = input_fd;
+	} else {
+		req_type = IORING_OP_WRITE;
+		fd = output_fd;
+	}
+
+	sqe_prep_rw(sqes, req_type, fd, buffer_uptr, size, cur_offset);
+	sqe->user_data = type;
+
+	ret = bpf_io_uring_submit_sqes(ring, 1);
+	if (ret != 1) {
+		cp_result = ret;
+		return ret < 0 ? ret : -EFAULT;
+	}
+	return 0;
+}
+
+SEC("struct_ops.s/cp_loop_step")
+int BPF_PROG(cp_loop_step, struct io_ring_ctx *ring, struct iou_loop_params *ls)
+{
+	struct io_uring_sqe *sqes;
+	struct io_uring_cqe *cqes;
+	struct io_uring *cq_hdr;
+	void *rings;
+	int ret;
+
+	sqes = (void *)bpf_io_uring_get_region(ring, IOU_REGION_SQ,
+				sq_entries * sizeof(struct io_uring_sqe));
+	rings = (void *)bpf_io_uring_get_region(ring, IOU_REGION_CQ,
+				cqes_offset + cq_entries * sizeof(struct io_uring_cqe));
+	if (!rings || !sqes)
+		return IOU_LOOP_STOP;
+	cq_hdr = rings + cq_hdr_offset;
+	cqes = rings + cqes_offset;
+
+	if (!nr_infligt) {
+		nr_infligt++;
+		ret = issue_next_req(ring, sqes, REQ_TOKEN_READ,
+				     buffer_size);
+		if (ret)
+			return IOU_LOOP_STOP;
+	}
+
+	if (cq_hdr->tail != cq_hdr->head) {
+		struct io_uring_cqe *cqe;
+
+		if (cq_hdr->tail - cq_hdr->head != 1) {
+			cp_result = -ERANGE;
+			return IOU_LOOP_STOP;
+		}
+
+		cqe = &cqes[cq_hdr->head & (cq_entries - 1)];
+		if (cqe->res < 0) {
+			cp_result = cqe->res;
+			return IOU_LOOP_STOP;
+		}
+
+		switch (cqe->user_data) {
+		case REQ_TOKEN_READ:
+			if (cqe->res == 0) {
+				cp_result = 0;
+				return IOU_LOOP_STOP;
+			}
+			ret = issue_next_req(ring, sqes, REQ_TOKEN_WRITE,
+					     cqe->res);
+			if (ret)
+				return IOU_LOOP_STOP;
+			break;
+		case REQ_TOKEN_WRITE:
+			cur_offset += cqe->res;
+			ret = issue_next_req(ring, sqes, REQ_TOKEN_READ,
+					     buffer_size);
+			if (ret)
+				return IOU_LOOP_STOP;
+			break;
+		default:
+			bpf_printk("invalid token\n");
+			cp_result = -EINVAL;
+			return IOU_LOOP_STOP;
+		};
+
+		cq_hdr->head++;
+	}
+
+	ls->cq_wait_idx = cq_hdr->head + 1;
+	return IOU_LOOP_CONTINUE;
+}
+
+SEC(".struct_ops.link")
+struct io_uring_bpf_ops cp_ops = {
+	.loop_step = (void *)cp_loop_step,
+};
diff --git a/test/bpf-progs/nops.bpf.c b/test/bpf-progs/nops.bpf.c
new file mode 100644
index 00000000..00075bb6
--- /dev/null
+++ b/test/bpf-progs/nops.bpf.c
@@ -0,0 +1,99 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <linux/errno.h>
+
+char LICENSE[] SEC("license") = "Dual BSD/GPL";
+
+#define REQ_TOKEN 0xabba1741
+
+const unsigned max_inflight = 8;
+const volatile unsigned cq_hdr_offset;
+const volatile unsigned sq_hdr_offset;
+const volatile unsigned cqes_offset;
+const volatile unsigned cq_entries;
+const volatile unsigned sq_entries;
+
+unsigned reqs_inflight = 0;
+int reqs_to_run;
+
+#define t_min(a, b) ((a) < (b) ? (a) : (b))
+
+static unsigned nr_to_submit(void)
+{
+	unsigned to_submit = 0;
+	unsigned inflight = reqs_inflight;
+
+	if (inflight < max_inflight) {
+		to_submit = max_inflight - inflight;
+		to_submit = t_min(to_submit, reqs_to_run - inflight);
+	}
+	return to_submit;
+}
+
+SEC("struct_ops.s/nops_loop_step")
+int BPF_PROG(nops_loop_step, struct io_ring_ctx *ring, struct iou_loop_params *ls)
+{
+	struct io_uring_sqe *sqes;
+	struct io_uring_cqe *cqes;
+	struct io_uring *cq_hdr;
+	unsigned to_submit;
+	unsigned to_wait;
+	unsigned nr_cqes;
+	void *rings;
+	int ret, i;
+
+	sqes = (void *)bpf_io_uring_get_region(ring, IOU_REGION_SQ,
+				sq_entries * sizeof(struct io_uring_sqe));
+	rings = (void *)bpf_io_uring_get_region(ring, IOU_REGION_CQ,
+				cqes_offset + cq_entries * sizeof(struct io_uring_cqe));
+	if (!rings || !sqes)
+		return IOU_LOOP_STOP;
+	cq_hdr = rings + cq_hdr_offset;
+	cqes = rings + cqes_offset;
+
+	to_submit = nr_to_submit();
+	if (to_submit) {
+		for (i = 0; i < to_submit; i++) {
+			struct io_uring_sqe *sqe = &sqes[i];
+
+			*sqe = (struct io_uring_sqe){};
+			sqe->opcode = IORING_OP_NOP;
+			sqe->user_data = REQ_TOKEN;
+		}
+
+		ret = bpf_io_uring_submit_sqes(ring, to_submit);
+		if (ret != to_submit)
+			return IOU_LOOP_STOP;
+		reqs_inflight += to_submit;
+	}
+
+	nr_cqes = cq_hdr->tail - cq_hdr->head;
+	nr_cqes = t_min(nr_cqes, max_inflight);
+	for (i = 0; i < nr_cqes; i++) {
+		struct io_uring_cqe *cqe = &cqes[cq_hdr->head & (cq_entries - 1)];
+
+		if (cqe->user_data != REQ_TOKEN)
+			return IOU_LOOP_STOP;
+		cq_hdr->head++;
+	}
+
+	reqs_inflight -= nr_cqes;
+	reqs_to_run -= nr_cqes;
+
+	if (reqs_to_run <= 0 && !reqs_inflight)
+		return IOU_LOOP_STOP;
+
+	to_wait = reqs_inflight;
+	/* Don't sleep if there are still CQEs left */
+	if (cq_hdr->tail != cq_hdr->head)
+		to_wait = 0;
+	ls->cq_wait_idx = cq_hdr->head + to_wait;
+	return IOU_LOOP_CONTINUE;
+}
+
+SEC(".struct_ops.link")
+struct io_uring_bpf_ops nops_ops = {
+	.loop_step = (void *)nops_loop_step,
+};
diff --git a/test/bpf_cp.c b/test/bpf_cp.c
new file mode 100644
index 00000000..d04fdcb6
--- /dev/null
+++ b/test/bpf_cp.c
@@ -0,0 +1,138 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#include <linux/stddef.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <bpf/libbpf.h>
+
+#include "liburing.h"
+#include "cp.skel.h"
+#include "helpers.h"
+
+static struct cp_bpf *skel;
+static struct bpf_link *cp_bpf_link;
+
+static char *in_fname;
+static char *out_fname;
+
+static size_t buffer_size = 4096;
+static int input_fd;
+static int output_fd;
+static void *buffer;
+
+#define CQ_ENTRIES 8
+#define SQ_ENTRIES 8
+
+static int setup_ring_ops(struct io_uring *ring)
+{
+	struct io_uring_params params;
+	int ret;
+
+	memset(&params, 0, sizeof(params));
+	params.cq_entries = CQ_ENTRIES;
+	params.flags = IORING_SETUP_SINGLE_ISSUER |
+			IORING_SETUP_DEFER_TASKRUN |
+			IORING_SETUP_NO_SQARRAY |
+			IORING_SETUP_CQSIZE |
+			IORING_SETUP_SQ_REWIND;
+
+	ret = t_create_ring_params(SQ_ENTRIES, ring, &params);
+	if (ret == T_SETUP_SKIP) {
+		printf("Can't setup a ring, skip\n");
+		return T_EXIT_SKIP;
+	}
+	if (ret != T_SETUP_OK)
+		return T_EXIT_FAIL;
+
+	skel = cp_bpf__open();
+	if (!skel) {
+		fprintf(stderr, "can't generate skeleton\n");
+		return T_EXIT_FAIL;
+	}
+
+	skel->struct_ops.cp_ops->ring_fd = ring->ring_fd;
+	skel->rodata->sq_hdr_offset = params.sq_off.head;
+	skel->rodata->cq_hdr_offset = params.cq_off.head;
+	skel->rodata->cqes_offset = params.cq_off.cqes;
+	skel->rodata->cq_entries = CQ_ENTRIES;
+	skel->rodata->sq_entries = SQ_ENTRIES;
+	skel->bss->input_fd = input_fd;
+	skel->bss->output_fd = output_fd;
+	skel->bss->buffer_uptr = buffer;
+	skel->bss->buffer_size = buffer_size;
+	skel->bss->cp_result = -EBUSY;
+
+	ret = cp_bpf__load(skel);
+	if (ret) {
+		if (ret == -ESRCH) {
+			printf("io_uring BPF ops are not supported\n");
+			return T_EXIT_SKIP;
+		}
+		fprintf(stderr, "failed to load skeleton\n");
+		return T_EXIT_FAIL;
+	}
+
+	cp_bpf_link = bpf_map__attach_struct_ops(skel->maps.cp_ops);
+	if (!cp_bpf_link) {
+		fprintf(stderr, "failed to attach ops\n");
+		return T_EXIT_FAIL;
+	}
+	return T_EXIT_PASS;
+}
+
+int main(int argc, char *argv[])
+{
+	struct io_uring ring;
+	size_t file_size;
+	struct stat st;
+	int ret;
+
+	if (argc != 3)
+		return 0;
+
+	in_fname = argv[1];
+	out_fname = argv[2];
+
+	input_fd = open(in_fname, O_RDONLY | O_DIRECT);
+	output_fd = open(out_fname, O_WRONLY | O_DIRECT | O_CREAT, 0644);
+	if (input_fd < 0 || output_fd < 0) {
+		fprintf(stderr, "can't open files");
+		return T_EXIT_FAIL;
+	}
+	if (fstat(input_fd, &st) == -1) {
+		fprintf(stderr, "stat failed\n");
+		return T_EXIT_FAIL;
+	}
+	file_size = st.st_size;
+
+	buffer = aligned_alloc(4096, buffer_size);
+	if (!buffer) {
+		fprintf(stderr, "can't allocate buffer\n");
+		return T_EXIT_FAIL;
+	}
+
+	ret = setup_ring_ops(&ring);
+	if (ret != T_EXIT_PASS)
+		return ret;
+
+	if (ftruncate(output_fd, file_size) == -1) {
+		fprintf(stderr, "ftruncate failed\n");
+		return T_EXIT_FAIL;
+	}
+
+	ret = io_uring_enter(ring.ring_fd, 0, 0, IORING_ENTER_GETEVENTS, NULL);
+	if (ret) {
+		fprintf(stderr, "run failed\n");
+		return T_EXIT_FAIL;
+	}
+
+	ret = skel->bss->cp_result;
+	if (ret) {
+		fprintf(stderr, "cp failed %i\n", ret);
+		return T_EXIT_FAIL;
+	}
+
+	bpf_link__destroy(cp_bpf_link);
+	cp_bpf__destroy(skel);
+	return 0;
+}
diff --git a/test/bpf_nops.c b/test/bpf_nops.c
new file mode 100644
index 00000000..bdd4df47
--- /dev/null
+++ b/test/bpf_nops.c
@@ -0,0 +1,99 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#include <linux/stddef.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <bpf/libbpf.h>
+
+#include "liburing.h"
+#include "nops.skel.h"
+#include "helpers.h"
+
+static struct nops_bpf *skel;
+static struct bpf_link *nops_bpf_link;
+
+#define CQ_ENTRIES 8
+#define SQ_ENTRIES 8
+#define NR_ITERS 1000
+
+static int setup_ring_ops(struct io_uring *ring)
+{
+	struct io_uring_params params;
+	int ret;
+
+	memset(&params, 0, sizeof(params));
+	params.cq_entries = CQ_ENTRIES;
+	params.flags = IORING_SETUP_SINGLE_ISSUER |
+			IORING_SETUP_DEFER_TASKRUN |
+			IORING_SETUP_NO_SQARRAY |
+			IORING_SETUP_CQSIZE |
+			IORING_SETUP_SQ_REWIND;
+
+	ret = t_create_ring_params(SQ_ENTRIES, ring, &params);
+	if (ret == T_SETUP_SKIP) {
+		printf("Can't setup a ring, skip\n");
+		return T_EXIT_SKIP;
+	}
+	if (ret != T_SETUP_OK)
+		return T_EXIT_FAIL;
+
+	skel = nops_bpf__open();
+	if (!skel) {
+		fprintf(stderr, "can't generate skeleton\n");
+		return T_EXIT_FAIL;
+	}
+
+	skel->struct_ops.nops_ops->ring_fd = ring->ring_fd;
+	skel->bss->reqs_to_run = NR_ITERS;
+	skel->rodata->sq_hdr_offset = params.sq_off.head;
+	skel->rodata->cq_hdr_offset = params.cq_off.head;
+	skel->rodata->cqes_offset = params.cq_off.cqes;
+	skel->rodata->cq_entries = CQ_ENTRIES;
+	skel->rodata->sq_entries = SQ_ENTRIES;
+
+	ret = nops_bpf__load(skel);
+	if (ret) {
+		if (ret == -ESRCH) {
+			printf("io_uring BPF ops are not supported\n");
+			return T_EXIT_SKIP;
+		}
+		fprintf(stderr, "failed to load skeleton\n");
+		return T_EXIT_FAIL;
+	}
+
+	nops_bpf_link = bpf_map__attach_struct_ops(skel->maps.nops_ops);
+	if (!nops_bpf_link) {
+		fprintf(stderr, "failed to attach ops\n");
+		return T_EXIT_FAIL;
+	}
+	return T_EXIT_PASS;
+}
+
+int main()
+{
+	struct io_uring ring;
+	unsigned left;
+	int ret;
+
+	ret = setup_ring_ops(&ring);
+	if (ret != T_EXIT_PASS)
+		return ret;
+
+	ret = io_uring_enter(ring.ring_fd, 0, 0, IORING_ENTER_GETEVENTS, NULL);
+	if (ret) {
+		fprintf(stderr, "run failed\n");
+		return T_EXIT_FAIL;
+	}
+
+	left = skel->bss->reqs_to_run;
+	if (left) {
+		fprintf(stderr, "Run failed, couldn't submit all nops %i / %i\n",
+			NR_ITERS - left, NR_ITERS);
+		return T_EXIT_FAIL;
+	}
+
+	bpf_link__destroy(nops_bpf_link);
+	nops_bpf__destroy(skel);
+	io_uring_queue_exit(&ring);
+	return T_EXIT_PASS;
+}
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH liburing v3 1/1] tests: test io_uring bpf ops
  2026-03-18 17:36 [PATCH liburing v3 1/1] tests: test io_uring bpf ops Pavel Begunkov
  2026-03-18 17:43 ` Pavel Begunkov
  2026-03-18 20:59 ` Pavel Begunkov
@ 2026-03-18 21:01 ` Pavel Begunkov
  2 siblings, 0 replies; 7+ messages in thread
From: Pavel Begunkov @ 2026-03-18 21:01 UTC (permalink / raw)
  To: io-uring; +Cc: axboe

On 3/18/26 20:59, Pavel Begunkov wrote:
> Add some BPF struct ops io_uring tests/examples, one is issuing nops in
> a loop, the other copies a file.

My bad, ignore this one

-- 
Pavel Begunkov


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-03-18 21:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-18 17:36 [PATCH liburing v3 1/1] tests: test io_uring bpf ops Pavel Begunkov
2026-03-18 17:43 ` Pavel Begunkov
2026-03-18 18:24   ` Jens Axboe
2026-03-18 18:28     ` Jens Axboe
2026-03-18 20:57     ` Pavel Begunkov
2026-03-18 20:59 ` Pavel Begunkov
2026-03-18 21:01 ` Pavel Begunkov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox