Skip to content

Commit 7cd5c66

Browse files
ameryhungKernel Patches Daemon
authored andcommitted
bpf: Support specifying linear xdp packet data size for BPF_PROG_TEST_RUN
To test bpf_xdp_pull_data(), an xdp packet containing fragments as well as free linear data area after xdp->data_end needs to be created. However, bpf_prog_test_run_xdp() always fills the linear area with data_in before creating fragments, leaving no space to pull data. This patch will allow users to specify the linear data size through ctx->data_end. Currently, ctx_in->data_end must match data_size_in and will not be the final ctx->data_end seen by xdp programs. This is because ctx->data_end is populated according to the xdp_buff passed to test_run. The linear data area available in an xdp_buff, max_data_sz, is alawys filled up before copying data_in into fragments. This patch will allow users to specify the size of data that goes into the linear area. When ctx_in->data_end is different from data_size_in, only ctx_in->data_end bytes of data will be put into the linear area when creating the xdp_buff. While ctx_in->data_end will be allowed to be different from data_size_in, it cannot be larger than the data_size_in as there will be no data to copy from user space. If it is larger than the maximum linear data area size, the layout suggested by the user will not be honored. Data beyond max_data_sz bytes will still be copied into fragments. Finally, since it is possible for a NIC to produce a xdp_buff with empty linear data area, allow it when calling bpf_test_init() from bpf_prog_test_run_xdp() so that we can test XDP kfuncs with such xdp_buff. This is done by moving lower-bound check to callers as most of them already do except bpf_prog_test_run_skb(). Signed-off-by: Amery Hung <[email protected]>
1 parent 9bb43f9 commit 7cd5c66

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

net/bpf/test_run.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size,
665665
void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
666666
void *data;
667667

668-
if (user_size < ETH_HLEN || user_size > PAGE_SIZE - headroom - tailroom)
668+
if (user_size > PAGE_SIZE - headroom - tailroom)
669669
return ERR_PTR(-EINVAL);
670670

671671
size = SKB_DATA_ALIGN(size);
@@ -1001,6 +1001,9 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
10011001
kattr->test.cpu || kattr->test.batch_size)
10021002
return -EINVAL;
10031003

1004+
if (size < ETH_HLEN)
1005+
return -EINVAL;
1006+
10041007
data = bpf_test_init(kattr, kattr->test.data_size_in,
10051008
size, NET_SKB_PAD + NET_IP_ALIGN,
10061009
SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
@@ -1246,13 +1249,15 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
12461249

12471250
if (ctx) {
12481251
/* There can't be user provided data before the meta data */
1249-
if (ctx->data_meta || ctx->data_end != size ||
1252+
if (ctx->data_meta || ctx->data_end > size ||
12501253
ctx->data > ctx->data_end ||
12511254
unlikely(xdp_metalen_invalid(ctx->data)) ||
12521255
(do_live && (kattr->test.data_out || kattr->test.ctx_out)))
12531256
goto free_ctx;
12541257
/* Meta data is allocated from the headroom */
12551258
headroom -= ctx->data;
1259+
1260+
size = ctx->data_end;
12561261
}
12571262

12581263
max_data_sz = PAGE_SIZE - headroom - tailroom;

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ void test_xdp_context_test_run(void)
9797
/* Meta data must be 255 bytes or smaller */
9898
test_xdp_context_error(prog_fd, opts, 0, 256, sizeof(data), 0, 0, 0);
9999

100-
/* Total size of data must match data_end - data_meta */
101-
test_xdp_context_error(prog_fd, opts, 0, sizeof(__u32),
102-
sizeof(data) - 1, 0, 0, 0);
100+
/* Total size of data must be data_end - data_meta or larger */
103101
test_xdp_context_error(prog_fd, opts, 0, sizeof(__u32),
104102
sizeof(data) + 1, 0, 0, 0);
105103

0 commit comments

Comments
 (0)