Skip to content

Commit 153f6bf

Browse files
jsitnickiMartin KaFai Lau
authored andcommitted
selftests/bpf: Cover read access to skb metadata via dynptr
Exercise reading from SKB metadata area in two new ways: 1. indirectly, with bpf_dynptr_read(), and 2. directly, with bpf_dynptr_slice(). Signed-off-by: Jakub Sitnicki <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Reviewed-by: Jesse Brandeburg <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Link: https://patch.msgid.link/20250814-skb-metadata-thru-dynptr-v7-6-8a39e636e0fb@cloudflare.com
1 parent dd9f6cf commit 153f6bf

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

tools/testing/selftests/bpf/bpf_kfuncs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ extern int bpf_dynptr_from_skb(struct __sk_buff *skb, __u64 flags,
1919
extern int bpf_dynptr_from_xdp(struct xdp_md *xdp, __u64 flags,
2020
struct bpf_dynptr *ptr__uninit) __ksym __weak;
2121

22+
extern int bpf_dynptr_from_skb_meta(struct __sk_buff *skb, __u64 flags,
23+
struct bpf_dynptr *ptr__uninit) __ksym __weak;
24+
2225
/* Description
2326
* Obtain a read-only pointer to the dynptr's data
2427
* Returns

tools/testing/selftests/bpf/prog_tests/xdp_context_test_run.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,18 @@ static void assert_test_result(const struct bpf_map *result_map)
171171
"test_result map contains test payload");
172172
}
173173

174+
static bool clear_test_result(struct bpf_map *result_map)
175+
{
176+
const __u8 v[sizeof(test_payload)] = {};
177+
const __u32 k = 0;
178+
int err;
179+
180+
err = bpf_map__update_elem(result_map, &k, sizeof(k), v, sizeof(v), BPF_ANY);
181+
ASSERT_OK(err, "update test_result");
182+
183+
return err == 0;
184+
}
185+
174186
void test_xdp_context_veth(void)
175187
{
176188
LIBBPF_OPTS(bpf_tc_hook, tc_hook, .attach_point = BPF_TC_INGRESS);
@@ -268,6 +280,9 @@ static void test_tuntap(struct bpf_program *xdp_prog,
268280
int tap_ifindex;
269281
int ret;
270282

283+
if (!clear_test_result(result_map))
284+
return;
285+
271286
ns = netns_new(TAP_NETNS, true);
272287
if (!ASSERT_OK_PTR(ns, "create and open ns"))
273288
return;
@@ -328,6 +343,12 @@ void test_xdp_context_tuntap(void)
328343
if (test__start_subtest("data_meta"))
329344
test_tuntap(skel->progs.ing_xdp, skel->progs.ing_cls,
330345
skel->maps.test_result);
346+
if (test__start_subtest("dynptr_read"))
347+
test_tuntap(skel->progs.ing_xdp, skel->progs.ing_cls_dynptr_read,
348+
skel->maps.test_result);
349+
if (test__start_subtest("dynptr_slice"))
350+
test_tuntap(skel->progs.ing_xdp, skel->progs.ing_cls_dynptr_slice,
351+
skel->maps.test_result);
331352

332353
test_xdp_meta__destroy(skel);
333354
}

tools/testing/selftests/bpf/progs/test_xdp_meta.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
#include <stdbool.h>
12
#include <linux/bpf.h>
23
#include <linux/if_ether.h>
34
#include <linux/pkt_cls.h>
45

56
#include <bpf/bpf_helpers.h>
7+
#include "bpf_kfuncs.h"
68

79
#define META_SIZE 32
810

@@ -40,6 +42,46 @@ int ing_cls(struct __sk_buff *ctx)
4042
return TC_ACT_SHOT;
4143
}
4244

45+
/* Read from metadata using bpf_dynptr_read helper */
46+
SEC("tc")
47+
int ing_cls_dynptr_read(struct __sk_buff *ctx)
48+
{
49+
struct bpf_dynptr meta;
50+
const __u32 zero = 0;
51+
__u8 *dst;
52+
53+
dst = bpf_map_lookup_elem(&test_result, &zero);
54+
if (!dst)
55+
return TC_ACT_SHOT;
56+
57+
bpf_dynptr_from_skb_meta(ctx, 0, &meta);
58+
bpf_dynptr_read(dst, META_SIZE, &meta, 0, 0);
59+
60+
return TC_ACT_SHOT;
61+
}
62+
63+
/* Read from metadata using read-only dynptr slice */
64+
SEC("tc")
65+
int ing_cls_dynptr_slice(struct __sk_buff *ctx)
66+
{
67+
struct bpf_dynptr meta;
68+
const __u32 zero = 0;
69+
__u8 *dst, *src;
70+
71+
dst = bpf_map_lookup_elem(&test_result, &zero);
72+
if (!dst)
73+
return TC_ACT_SHOT;
74+
75+
bpf_dynptr_from_skb_meta(ctx, 0, &meta);
76+
src = bpf_dynptr_slice(&meta, 0, NULL, META_SIZE);
77+
if (!src)
78+
return TC_ACT_SHOT;
79+
80+
__builtin_memcpy(dst, src, META_SIZE);
81+
82+
return TC_ACT_SHOT;
83+
}
84+
4385
SEC("xdp")
4486
int ing_xdp(struct xdp_md *ctx)
4587
{

0 commit comments

Comments
 (0)