Skip to content

Commit 9d15404

Browse files
mykyta5anakryiko
authored andcommitted
bpf/helpers: Introduce bpf_dynptr_copy kfunc
Introducing bpf_dynptr_copy kfunc allowing copying data from one dynptr to another. This functionality is useful in scenarios such as capturing XDP data to a ring buffer. The implementation consists of 4 branches: * A fast branch for contiguous buffer capacity in both source and destination dynptrs * 3 branches utilizing __bpf_dynptr_read and __bpf_dynptr_write to copy data to/from non-contiguous buffer Signed-off-by: Mykyta Yatsenko <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent bacac21 commit 9d15404

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

kernel/bpf/helpers.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2770,6 +2770,61 @@ __bpf_kfunc int bpf_dynptr_clone(const struct bpf_dynptr *p,
27702770
return 0;
27712771
}
27722772

2773+
/**
2774+
* bpf_dynptr_copy() - Copy data from one dynptr to another.
2775+
* @dst_ptr: Destination dynptr - where data should be copied to
2776+
* @dst_off: Offset into the destination dynptr
2777+
* @src_ptr: Source dynptr - where data should be copied from
2778+
* @src_off: Offset into the source dynptr
2779+
* @size: Length of the data to copy from source to destination
2780+
*
2781+
* Copies data from source dynptr to destination dynptr.
2782+
* Returns 0 on success; negative error, otherwise.
2783+
*/
2784+
__bpf_kfunc int bpf_dynptr_copy(struct bpf_dynptr *dst_ptr, u32 dst_off,
2785+
struct bpf_dynptr *src_ptr, u32 src_off, u32 size)
2786+
{
2787+
struct bpf_dynptr_kern *dst = (struct bpf_dynptr_kern *)dst_ptr;
2788+
struct bpf_dynptr_kern *src = (struct bpf_dynptr_kern *)src_ptr;
2789+
void *src_slice, *dst_slice;
2790+
char buf[256];
2791+
u32 off;
2792+
2793+
src_slice = bpf_dynptr_slice(src_ptr, src_off, NULL, size);
2794+
dst_slice = bpf_dynptr_slice_rdwr(dst_ptr, dst_off, NULL, size);
2795+
2796+
if (src_slice && dst_slice) {
2797+
memmove(dst_slice, src_slice, size);
2798+
return 0;
2799+
}
2800+
2801+
if (src_slice)
2802+
return __bpf_dynptr_write(dst, dst_off, src_slice, size, 0);
2803+
2804+
if (dst_slice)
2805+
return __bpf_dynptr_read(dst_slice, size, src, src_off, 0);
2806+
2807+
if (bpf_dynptr_check_off_len(dst, dst_off, size) ||
2808+
bpf_dynptr_check_off_len(src, src_off, size))
2809+
return -E2BIG;
2810+
2811+
off = 0;
2812+
while (off < size) {
2813+
u32 chunk_sz = min_t(u32, sizeof(buf), size - off);
2814+
int err;
2815+
2816+
err = __bpf_dynptr_read(buf, chunk_sz, src, src_off + off, 0);
2817+
if (err)
2818+
return err;
2819+
err = __bpf_dynptr_write(dst, dst_off + off, buf, chunk_sz, 0);
2820+
if (err)
2821+
return err;
2822+
2823+
off += chunk_sz;
2824+
}
2825+
return 0;
2826+
}
2827+
27732828
__bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
27742829
{
27752830
return obj;
@@ -3218,6 +3273,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_is_null)
32183273
BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly)
32193274
BTF_ID_FLAGS(func, bpf_dynptr_size)
32203275
BTF_ID_FLAGS(func, bpf_dynptr_clone)
3276+
BTF_ID_FLAGS(func, bpf_dynptr_copy)
32213277
#ifdef CONFIG_NET
32223278
BTF_ID_FLAGS(func, bpf_modify_return_test_tp)
32233279
#endif

0 commit comments

Comments
 (0)