selftests/bpf: Add tests for bpf_dynptr_copy

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@meta.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20250226183201.332713-4-mykyta.yatsenko5@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Mykyta Yatsenko 2025-02-26 18:32:01 +00:00 committed by Alexei Starovoitov
parent daec295a70
commit 064e9aacfd
2 changed files with 139 additions and 5 deletions

View File

@ -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,24 @@ 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;
LIBBPF_OPTS(bpf_test_run_opts, opts,
.data_in = &data,
.data_size_in = sizeof(data),
.repeat = 1,
);
prog_fd = bpf_program__fd(prog);
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");

View File

@ -1,20 +1,19 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2022 Facebook */
#include <vmlinux.h>
#include <string.h>
#include <stdbool.h>
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include "bpf_misc.h"
#include "bpf_kfuncs.h"
#include "errno.h"
char _license[] SEC("license") = "GPL";
int pid, err, val;
struct sample {
struct ringbuf_sample {
int pid;
int seq;
long value;
@ -121,7 +120,7 @@ int test_dynptr_data(void *ctx)
static int ringbuf_callback(__u32 index, void *data)
{
struct sample *sample;
struct ringbuf_sample *sample;
struct bpf_dynptr *ptr = (struct bpf_dynptr *)data;
@ -138,7 +137,7 @@ SEC("?tp/syscalls/sys_enter_nanosleep")
int test_ringbuf(void *ctx)
{
struct bpf_dynptr ptr;
struct sample *sample;
struct ringbuf_sample *sample;
if (bpf_get_current_pid_tgid() >> 32 != pid)
return 0;
@ -567,3 +566,117 @@ int BPF_PROG(test_dynptr_skb_tp_btf, void *skb, void *location)
return 1;
}
static inline int bpf_memcmp(const char *a, const char *b, u32 size)
{
int i;
bpf_for(i, 0, size) {
if (a[i] != b[i])
return a[i] < b[i] ? -1 : 1;
}
return 0;
}
SEC("?tp/syscalls/sys_enter_nanosleep")
int test_dynptr_copy(void *ctx)
{
char data[] = "hello there, world!!";
char buf[32] = {'\0'};
__u32 sz = sizeof(data);
struct bpf_dynptr src, dst;
bpf_ringbuf_reserve_dynptr(&ringbuf, sz, 0, &src);
bpf_ringbuf_reserve_dynptr(&ringbuf, sz, 0, &dst);
/* Test basic case of copying contiguous memory backed dynptrs */
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 ?: bpf_memcmp(data, buf, sz);
/* Test that offsets are handled correctly */
err = err ?: bpf_dynptr_copy(&dst, 3, &src, 5, sz - 5);
err = err ?: bpf_dynptr_read(buf, sz - 5, &dst, 3, 0);
err = err ?: bpf_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 = sizeof(data);
int i, chunks = 200;
/* ptr_xdp is backed by non-contiguous memory */
bpf_dynptr_from_xdp(xdp, 0, &ptr_xdp);
bpf_ringbuf_reserve_dynptr(&ringbuf, len * chunks, 0, &ptr_buf);
/* Destination dynptr is backed by non-contiguous memory */
bpf_for(i, 0, chunks) {
err = bpf_dynptr_write(&ptr_buf, i * len, data, len, 0);
if (err)
goto out;
}
err = bpf_dynptr_copy(&ptr_xdp, 0, &ptr_buf, 0, len * chunks);
if (err)
goto out;
bpf_for(i, 0, chunks) {
__builtin_memset(buf, 0, sizeof(buf));
err = bpf_dynptr_read(&buf, len, &ptr_xdp, i * len, 0);
if (err)
goto out;
if (bpf_memcmp(data, buf, len) != 0)
goto out;
}
/* Source dynptr is backed by non-contiguous memory */
__builtin_memset(buf, 0, sizeof(buf));
bpf_for(i, 0, chunks) {
err = bpf_dynptr_write(&ptr_buf, i * len, buf, len, 0);
if (err)
goto out;
}
err = bpf_dynptr_copy(&ptr_buf, 0, &ptr_xdp, 0, len * chunks);
if (err)
goto out;
bpf_for(i, 0, chunks) {
__builtin_memset(buf, 0, sizeof(buf));
err = bpf_dynptr_read(&buf, len, &ptr_buf, i * len, 0);
if (err)
goto out;
if (bpf_memcmp(data, buf, len) != 0)
goto out;
}
/* Both source and destination dynptrs are backed by non-contiguous memory */
err = bpf_dynptr_copy(&ptr_xdp, 2, &ptr_xdp, len, len * (chunks - 1));
if (err)
goto out;
bpf_for(i, 0, chunks - 1) {
__builtin_memset(buf, 0, sizeof(buf));
err = bpf_dynptr_read(&buf, len, &ptr_xdp, 2 + i * len, 0);
if (err)
goto out;
if (bpf_memcmp(data, buf, len) != 0)
goto out;
}
if (bpf_dynptr_copy(&ptr_xdp, 2000, &ptr_xdp, 0, len * chunks) != -E2BIG)
err = 1;
out:
bpf_ringbuf_discard_dynptr(&ptr_buf, 0);
return XDP_DROP;
}