On Tue, Feb 18, 2025 at 11:01 AM Mykyta Yatsenko
<mykyta.yatsenko5@xxxxxxxxx> wrote:
From: Mykyta Yatsenko <yatsenko@xxxxxxxx>
Add XDP setup type for dynptr tests, enabling testing for
non-contiguous buffer.
Add 2 tests:
- test_dynptr_copy - verify correctness for the fast (contiguous
buffer) code path.
- test_dynptr_copy_xdp - verifies code paths that handle
non-contiguous buffer.
Signed-off-by: Mykyta Yatsenko <yatsenko@xxxxxxxx>
---
tools/testing/selftests/bpf/bpf_kfuncs.h | 8 ++
.../testing/selftests/bpf/prog_tests/dynptr.c | 25 ++++++
.../selftests/bpf/progs/dynptr_success.c | 77 +++++++++++++++++++
3 files changed, 110 insertions(+)
diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h
index 8215c9b3115e..e9c193036c82 100644
--- a/tools/testing/selftests/bpf/bpf_kfuncs.h
+++ b/tools/testing/selftests/bpf/bpf_kfuncs.h
@@ -43,6 +43,14 @@ extern bool bpf_dynptr_is_rdonly(const struct bpf_dynptr *ptr) __ksym __weak;
extern __u32 bpf_dynptr_size(const struct bpf_dynptr *ptr) __ksym __weak;
extern int bpf_dynptr_clone(const struct bpf_dynptr *ptr, struct bpf_dynptr *clone__init) __ksym __weak;
+/* Description
+ * Copy data from one dynptr to another
+ * Returns
+ * Error code
+ */
+extern int bpf_dynptr_copy(struct bpf_dynptr *dst, __u32 dst_off,
+ struct bpf_dynptr *src, __u32 src_off, __u32 size) __ksym __weak;
+
Do we *need* this? Doesn't all this come from vmlinux.h nowadays?
/* Description
* Modify the address of a AF_UNIX sockaddr.
* Returns
diff --git a/tools/testing/selftests/bpf/prog_tests/dynptr.c b/tools/testing/selftests/bpf/prog_tests/dynptr.c
index b614a5272dfd..247618958155 100644
--- a/tools/testing/selftests/bpf/prog_tests/dynptr.c
+++ b/tools/testing/selftests/bpf/prog_tests/dynptr.c
@@ -10,6 +10,7 @@ enum test_setup_type {
SETUP_SYSCALL_SLEEP,
SETUP_SKB_PROG,
SETUP_SKB_PROG_TP,
+ SETUP_XDP_PROG,
};
static struct {
@@ -18,6 +19,8 @@ static struct {
} success_tests[] = {
{"test_read_write", SETUP_SYSCALL_SLEEP},
{"test_dynptr_data", SETUP_SYSCALL_SLEEP},
+ {"test_dynptr_copy", SETUP_SYSCALL_SLEEP},
+ {"test_dynptr_copy_xdp", SETUP_XDP_PROG},
{"test_ringbuf", SETUP_SYSCALL_SLEEP},
{"test_skb_readonly", SETUP_SKB_PROG},
{"test_dynptr_skb_data", SETUP_SKB_PROG},
@@ -120,6 +123,28 @@ static void verify_success(const char *prog_name, enum test_setup_type setup_typ
break;
}
+ case SETUP_XDP_PROG:
+ {
+ char data[5000];
+ int err, prog_fd;
+
no empty line here, opts is a variable
+ LIBBPF_OPTS(bpf_test_run_opts, opts,
+ .data_in = &data,
+ .data_size_in = sizeof(data),
+ .repeat = 1,
+ );
+
+ prog_fd = bpf_program__fd(prog);
+ if (!ASSERT_GE(prog_fd, 0, "prog_fd"))
+ goto cleanup;
we shouldn't check this, if program loaded successfully this will
always be true (and yeah, I know that existing code does that, we
should remove or at least not duplicate this)
+
+ err = bpf_prog_test_run_opts(prog_fd, &opts);
+
+ if (!ASSERT_OK(err, "test_run"))
+ goto cleanup;
+
+ break;
+ }
}
ASSERT_EQ(skel->bss->err, 0, "err");
diff --git a/tools/testing/selftests/bpf/progs/dynptr_success.c b/tools/testing/selftests/bpf/progs/dynptr_success.c
index bfcc85686cf0..8a6b35418e39 100644
--- a/tools/testing/selftests/bpf/progs/dynptr_success.c
+++ b/tools/testing/selftests/bpf/progs/dynptr_success.c
@@ -567,3 +567,80 @@ int BPF_PROG(test_dynptr_skb_tp_btf, void *skb, void *location)
return 1;
}
+
+SEC("?tp/syscalls/sys_enter_nanosleep")
+int test_dynptr_copy(void *ctx)
+{
+ char *data = "hello there, world!!";
+ char buf[32] = {'\0'};
+ __u32 sz = strlen(data);
this is fragile, this is not guaranteed to work (only if compiler just
substituted a constant value). maybe just use data[] = "hello
there..." and use sizeof(data) then?
+ struct bpf_dynptr src, dst;
+
+ bpf_ringbuf_reserve_dynptr(&ringbuf, sz, 0, &src);
+ bpf_ringbuf_reserve_dynptr(&ringbuf, sz, 0, &dst);
+
+ err = bpf_dynptr_write(&src, 0, data, sz, 0);
+ err = err ?: bpf_dynptr_copy(&dst, 0, &src, 0, sz);
+ err = err ?: bpf_dynptr_read(buf, sz, &dst, 0, 0);
+ err = err ?: __builtin_memcmp(data, buf, sz);
+
+ err = err ?: bpf_dynptr_copy(&dst, 3, &src, 5, sz - 5);
+ err = err ?: bpf_dynptr_read(buf, sz - 5, &dst, 3, 0);
+ err = err ?: __builtin_memcmp(data + 5, buf, sz - 5);
+
+ bpf_ringbuf_discard_dynptr(&src, 0);
+ bpf_ringbuf_discard_dynptr(&dst, 0);
+ return 0;
+}
+
+SEC("xdp")
+int test_dynptr_copy_xdp(struct xdp_md *xdp)
+{
+ struct bpf_dynptr ptr_buf, ptr_xdp;
+ char *data = "qwertyuiopasdfghjkl;";
+ char buf[32] = {'\0'};
+ __u32 len = strlen(data);
ditto
+ int i, chunks = 200;
+
+ bpf_dynptr_from_xdp(xdp, 0, &ptr_xdp);
+ bpf_ringbuf_reserve_dynptr(&ringbuf, len * chunks, 0, &ptr_buf);
+
+ bpf_for(i, 0, chunks) {
+ err = err ?: bpf_dynptr_write(&ptr_buf, i * len, data, len, 0);
+ }
+
+ err = err ?: bpf_dynptr_copy(&ptr_xdp, 0, &ptr_buf, 0, len * chunks);
+
+ bpf_for(i, 0, chunks) {
+ memset(buf, 0, sizeof(buf));
__builtin_memset(), memset() works only because compiler optimizes it
to built-in, but let's not rely on that
+ err = err ?: bpf_dynptr_read(&buf, len, &ptr_xdp, i * len, 0);
+ err = err ?: memcmp(data, buf, len);
__builtin_memcmp() and all the other cases below, please
+ }
+
+ memset(buf, 0, sizeof(buf));
+ bpf_for(i, 0, chunks) {
+ err = err ?: bpf_dynptr_write(&ptr_buf, i * len, buf, len, 0);
+ }
+
+ err = err ?: bpf_dynptr_copy(&ptr_buf, 0, &ptr_xdp, 0, len * chunks);
+
+ bpf_for(i, 0, chunks) {
+ memset(buf, 0, sizeof(buf));
+ err = err ?: bpf_dynptr_read(&buf, len, &ptr_buf, i * len, 0);
+ err = err ?: memcmp(data, buf, len);
+ }
+
+ bpf_ringbuf_discard_dynptr(&ptr_buf, 0);
+
+ err = err ?: bpf_dynptr_copy(&ptr_xdp, 2, &ptr_xdp, len, len * (chunks - 1));
+
+ bpf_for(i, 0, chunks - 1) {
+ memset(buf, 0, sizeof(buf));
+ err = err ?: bpf_dynptr_read(&buf, len, &ptr_xdp, 2 + i * len, 0);
+ err = err ?: memcmp(data, buf, len);
+ }
+
+ err = err ?: (bpf_dynptr_copy(&ptr_xdp, 2000, &ptr_xdp, 0, len * chunks) == -E2BIG ? 0 : 1);
overdoing it a bit with the whole `err ?: ` pattern, IMO
BTW, more questions to networking folks (maybe Martin knows). Is there
a way to setup SKB or XDP packet with a non-linear region for testing?