Skip to content

Commit fe9544e

Browse files
ameryhungMartin KaFai Lau
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_linear_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_linear_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(). The change also fixes a bug that allows passing an xdp_buff with data < ETH_HLEN. This can happen when ctx is used and metadata is at least ETH_HLEN. Signed-off-by: Amery Hung <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent 7eb83bf commit fe9544e

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

net/bpf/test_run.c

Lines changed: 12 additions & 3 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)));
@@ -1207,7 +1210,7 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
12071210
{
12081211
bool do_live = (kattr->test.flags & BPF_F_TEST_XDP_LIVE_FRAMES);
12091212
u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1210-
u32 retval = 0, duration, max_linear_sz, size;
1213+
u32 retval = 0, meta_sz = 0, duration, max_linear_sz, size;
12111214
u32 linear_sz = kattr->test.data_size_in;
12121215
u32 batch_size = kattr->test.batch_size;
12131216
u32 headroom = XDP_PACKET_HEADROOM;
@@ -1246,13 +1249,16 @@ 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 != kattr->test.data_size_in ||
1252+
if (ctx->data_meta || ctx->data_end > kattr->test.data_size_in ||
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+
meta_sz = ctx->data;
1261+
linear_sz = ctx->data_end;
12561262
}
12571263

12581264
max_linear_sz = PAGE_SIZE - headroom - tailroom;
@@ -1262,6 +1268,9 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
12621268
if (do_live && kattr->test.data_size_in > linear_sz)
12631269
goto free_ctx;
12641270

1271+
if (kattr->test.data_size_in - meta_sz < ETH_HLEN)
1272+
return -EINVAL;
1273+
12651274
data = bpf_test_init(kattr, linear_sz, max_linear_sz, headroom, tailroom);
12661275
if (IS_ERR(data)) {
12671276
ret = PTR_ERR(data);

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)