mirror of
https://github.com/torvalds/linux.git
synced 2026-05-26 08:02:27 +02:00
selftests/bpf: Cover write access to skb metadata via dynptr
Add tests what exercise writes to skb metadata in two ways: 1. indirectly, using bpf_dynptr_write helper, 2. directly, using a read-write dynptr slice. Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com> Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org> Reviewed-by: Jesse Brandeburg <jbrandeburg@cloudflare.com> Acked-by: Eduard Zingerman <eddyz87@gmail.com> Link: https://patch.msgid.link/20250814-skb-metadata-thru-dynptr-v7-7-8a39e636e0fb@cloudflare.com
This commit is contained in:
parent
153f6bfd48
commit
ed93360807
|
|
@ -269,7 +269,8 @@ void test_xdp_context_veth(void)
|
|||
}
|
||||
|
||||
static void test_tuntap(struct bpf_program *xdp_prog,
|
||||
struct bpf_program *tc_prog,
|
||||
struct bpf_program *tc_prio_1_prog,
|
||||
struct bpf_program *tc_prio_2_prog,
|
||||
struct bpf_map *result_map)
|
||||
{
|
||||
LIBBPF_OPTS(bpf_tc_hook, tc_hook, .attach_point = BPF_TC_INGRESS);
|
||||
|
|
@ -302,11 +303,20 @@ static void test_tuntap(struct bpf_program *xdp_prog,
|
|||
if (!ASSERT_OK(ret, "bpf_tc_hook_create"))
|
||||
goto close;
|
||||
|
||||
tc_opts.prog_fd = bpf_program__fd(tc_prog);
|
||||
tc_opts.prog_fd = bpf_program__fd(tc_prio_1_prog);
|
||||
ret = bpf_tc_attach(&tc_hook, &tc_opts);
|
||||
if (!ASSERT_OK(ret, "bpf_tc_attach"))
|
||||
goto close;
|
||||
|
||||
if (tc_prio_2_prog) {
|
||||
LIBBPF_OPTS(bpf_tc_opts, tc_opts, .handle = 1, .priority = 2,
|
||||
.prog_fd = bpf_program__fd(tc_prio_2_prog));
|
||||
|
||||
ret = bpf_tc_attach(&tc_hook, &tc_opts);
|
||||
if (!ASSERT_OK(ret, "bpf_tc_attach"))
|
||||
goto close;
|
||||
}
|
||||
|
||||
ret = bpf_xdp_attach(tap_ifindex, bpf_program__fd(xdp_prog),
|
||||
0, NULL);
|
||||
if (!ASSERT_GE(ret, 0, "bpf_xdp_attach"))
|
||||
|
|
@ -341,13 +351,29 @@ void test_xdp_context_tuntap(void)
|
|||
return;
|
||||
|
||||
if (test__start_subtest("data_meta"))
|
||||
test_tuntap(skel->progs.ing_xdp, skel->progs.ing_cls,
|
||||
test_tuntap(skel->progs.ing_xdp,
|
||||
skel->progs.ing_cls,
|
||||
NULL, /* tc prio 2 */
|
||||
skel->maps.test_result);
|
||||
if (test__start_subtest("dynptr_read"))
|
||||
test_tuntap(skel->progs.ing_xdp, skel->progs.ing_cls_dynptr_read,
|
||||
test_tuntap(skel->progs.ing_xdp,
|
||||
skel->progs.ing_cls_dynptr_read,
|
||||
NULL, /* tc prio 2 */
|
||||
skel->maps.test_result);
|
||||
if (test__start_subtest("dynptr_slice"))
|
||||
test_tuntap(skel->progs.ing_xdp, skel->progs.ing_cls_dynptr_slice,
|
||||
test_tuntap(skel->progs.ing_xdp,
|
||||
skel->progs.ing_cls_dynptr_slice,
|
||||
NULL, /* tc prio 2 */
|
||||
skel->maps.test_result);
|
||||
if (test__start_subtest("dynptr_write"))
|
||||
test_tuntap(skel->progs.ing_xdp_zalloc_meta,
|
||||
skel->progs.ing_cls_dynptr_write,
|
||||
skel->progs.ing_cls_dynptr_read,
|
||||
skel->maps.test_result);
|
||||
if (test__start_subtest("dynptr_slice_rdwr"))
|
||||
test_tuntap(skel->progs.ing_xdp_zalloc_meta,
|
||||
skel->progs.ing_cls_dynptr_slice_rdwr,
|
||||
skel->progs.ing_cls_dynptr_slice,
|
||||
skel->maps.test_result);
|
||||
|
||||
test_xdp_meta__destroy(skel);
|
||||
|
|
|
|||
|
|
@ -60,6 +60,24 @@ int ing_cls_dynptr_read(struct __sk_buff *ctx)
|
|||
return TC_ACT_SHOT;
|
||||
}
|
||||
|
||||
/* Write to metadata using bpf_dynptr_write helper */
|
||||
SEC("tc")
|
||||
int ing_cls_dynptr_write(struct __sk_buff *ctx)
|
||||
{
|
||||
struct bpf_dynptr data, meta;
|
||||
__u8 *src;
|
||||
|
||||
bpf_dynptr_from_skb(ctx, 0, &data);
|
||||
src = bpf_dynptr_slice(&data, sizeof(struct ethhdr), NULL, META_SIZE);
|
||||
if (!src)
|
||||
return TC_ACT_SHOT;
|
||||
|
||||
bpf_dynptr_from_skb_meta(ctx, 0, &meta);
|
||||
bpf_dynptr_write(&meta, 0, src, META_SIZE, 0);
|
||||
|
||||
return TC_ACT_UNSPEC; /* pass */
|
||||
}
|
||||
|
||||
/* Read from metadata using read-only dynptr slice */
|
||||
SEC("tc")
|
||||
int ing_cls_dynptr_slice(struct __sk_buff *ctx)
|
||||
|
|
@ -82,6 +100,55 @@ int ing_cls_dynptr_slice(struct __sk_buff *ctx)
|
|||
return TC_ACT_SHOT;
|
||||
}
|
||||
|
||||
/* Write to metadata using writeable dynptr slice */
|
||||
SEC("tc")
|
||||
int ing_cls_dynptr_slice_rdwr(struct __sk_buff *ctx)
|
||||
{
|
||||
struct bpf_dynptr data, meta;
|
||||
__u8 *src, *dst;
|
||||
|
||||
bpf_dynptr_from_skb(ctx, 0, &data);
|
||||
src = bpf_dynptr_slice(&data, sizeof(struct ethhdr), NULL, META_SIZE);
|
||||
if (!src)
|
||||
return TC_ACT_SHOT;
|
||||
|
||||
bpf_dynptr_from_skb_meta(ctx, 0, &meta);
|
||||
dst = bpf_dynptr_slice_rdwr(&meta, 0, NULL, META_SIZE);
|
||||
if (!dst)
|
||||
return TC_ACT_SHOT;
|
||||
|
||||
__builtin_memcpy(dst, src, META_SIZE);
|
||||
|
||||
return TC_ACT_UNSPEC; /* pass */
|
||||
}
|
||||
|
||||
/* Reserve and clear space for metadata but don't populate it */
|
||||
SEC("xdp")
|
||||
int ing_xdp_zalloc_meta(struct xdp_md *ctx)
|
||||
{
|
||||
struct ethhdr *eth = ctx_ptr(ctx, data);
|
||||
__u8 *meta;
|
||||
int ret;
|
||||
|
||||
/* Drop any non-test packets */
|
||||
if (eth + 1 > ctx_ptr(ctx, data_end))
|
||||
return XDP_DROP;
|
||||
if (eth->h_proto != 0)
|
||||
return XDP_DROP;
|
||||
|
||||
ret = bpf_xdp_adjust_meta(ctx, -META_SIZE);
|
||||
if (ret < 0)
|
||||
return XDP_DROP;
|
||||
|
||||
meta = ctx_ptr(ctx, data_meta);
|
||||
if (meta + META_SIZE > ctx_ptr(ctx, data))
|
||||
return XDP_DROP;
|
||||
|
||||
__builtin_memset(meta, 0, META_SIZE);
|
||||
|
||||
return XDP_PASS;
|
||||
}
|
||||
|
||||
SEC("xdp")
|
||||
int ing_xdp(struct xdp_md *ctx)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user