[PATCH 6/6] tools/testing/selftests: add sample nvme bpf path selector

Geliang Tang geliang at kernel.org
Tue Jul 29 19:03:42 PDT 2025


Hi Hannes,

I tried to compile this BPF program, but got a compilation error. To
fix it, NVME_BPF and its dependencies need to be enabled in
tools/testing/selftests/bpf/config:

+++ b/tools/testing/selftests/bpf/config
@@ -121,3 +121,8 @@ CONFIG_XDP_SOCKETS=y
 CONFIG_XFRM_INTERFACE=y
 CONFIG_TCP_CONG_DCTCP=y
 CONFIG_TCP_CONG_BBR=y
+CONFIG_NVME_CORE=y
+CONFIG_NVME_FABRICS=y
+CONFIG_NVME_TCP=y
+CONFIG_NVME_MULTIPATH=y
+CONFIG_NVME_BPF=y

On Tue, 2025-07-29 at 09:06 +0200, hare at kernel.org wrote:
> From: Hannes Reinecke <hare at kernel.org>
> 
> As a simple nvme bpf path selector to demonstrate the namespace
> path iteration.
> 
> Signed-off-by: Hannes Reinecke <hare at kernel.org>
> ---
>  .../selftests/bpf/progs/bpf_nvme_simple.c     | 52
> +++++++++++++++++++

We also need to create an nvme test program in
tools/testing/selftests/bpf/prog_tests/ to load and verify this
bpf_nvme_simple BPF program, just like I did in
tools/testing/selftests/bpf/prog_tests/mptcp.c. I can try to implement
it if you think it's necessary.

Thanks,
-Geliang

>  1 file changed, 52 insertions(+)
>  create mode 100644
> tools/testing/selftests/bpf/progs/bpf_nvme_simple.c
> 
> diff --git a/tools/testing/selftests/bpf/progs/bpf_nvme_simple.c
> b/tools/testing/selftests/bpf/progs/bpf_nvme_simple.c
> new file mode 100644
> index 000000000000..c9cafb6bd253
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/bpf_nvme_simple.c
> @@ -0,0 +1,52 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +/*
> + * simple nvme ebpf path selector
> + *
> + * Simulates a RAID layout with chunk size 2M
> + */
> +
> +#include <vmlinux.h>
> +#include <errno.h>
> +#include <bpf/bpf_helpers.h>
> +#include <bpf/bpf_tracing.h>
> +
> +char _license[] SEC("license") = "GPL";
> +
> +static sector_t simple_offset = 0;
> +static sector_t simple_blocksize = 1048576;
> +
> +SEC("struct_ops")
> +int BPF_PROG(simple_select, struct nvme_bpf_iter *iter, sector_t
> sector)
> +{
> +	sector_t offset = simple_offset;
> +	sector_t block_size = simple_blocksize;
> +	u32 num_blks, num_paths, num_iter, i;
> +	int cntlid;
> +
> +	if (sector > offset)
> +		sector -= offset;
> +	cntlid = nvme_bpf_first_path(iter);
> +	if (cntlid < 0)
> +		return cntlid;
> +	if (!block_size || sector < block_size)
> +		return cntlid;
> +
> +	num_blks = (sector / block_size);
> +	num_paths = nvme_bpf_count_paths(iter);
> +	num_iter = num_blks % num_paths;
> +	bpf_for (i, 1, num_iter) {
> +		cntlid = nvme_bpf_next_path(iter);
> +		if (cntlid < 0)
> +			break;
> +	}
> +	return cntlid;
> +}
> +
> +SEC(".struct_ops")
> +struct nvme_bpf_ops bpf_nvme_simple = {
> +	.uuid = { 0x86, 0xee, 0x41, 0xd5, 0x25, 0x6b, 0x45, 0xd0,
> 0xa4, 0x81, 0x5e, 0x35, 0xf6, 0x02, 0xf5, 0x11 },
> +	.subsysnqn = "blktests-subsystem-1",
> +	.nsid = 1,
> +	.select_path = (void *)simple_select,
> +};



More information about the Linux-nvme mailing list