Skip to content

Commit ca2d03e

Browse files
committed
bpf: Support specifying linear xdp packet data size in test_run
To test bpf_xdp_pull_data(), an xdp packet containing fragments as well as free linear data area 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 the xdp program. This is because ctx->data_end is populated according to the xdp_buff passed to test_run, which always contains PAGE_SIZE - headroom - tailroom bytes of linear data if the linear data given by the user exceeds it. This patch will allow user 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. If it is larger than the maximum linear data area size, the layout suggested by the user will not be honored. Data beyond the maximum linear data area size will still be put into fragments. Signed-off-by: Amery Hung <[email protected]>
1 parent 43191ad commit ca2d03e

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

net/bpf/test_run.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,8 +1207,8 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
12071207
{
12081208
bool do_live = (kattr->test.flags & BPF_F_TEST_XDP_LIVE_FRAMES);
12091209
u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1210+
u32 retval = 0, duration, max_data_sz, linear_data_sz;
12101211
u32 batch_size = kattr->test.batch_size;
1211-
u32 retval = 0, duration, max_data_sz;
12121212
u32 size = kattr->test.data_size_in;
12131213
u32 headroom = XDP_PACKET_HEADROOM;
12141214
u32 repeat = kattr->test.repeat;
@@ -1246,7 +1246,7 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
12461246

12471247
if (ctx) {
12481248
/* There can't be user provided data before the meta data */
1249-
if (ctx->data_meta || ctx->data_end != size ||
1249+
if (ctx->data_meta || ctx->data_end > size ||
12501250
ctx->data > ctx->data_end ||
12511251
unlikely(xdp_metalen_invalid(ctx->data)) ||
12521252
(do_live && (kattr->test.data_out || kattr->test.ctx_out)))
@@ -1256,11 +1256,12 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
12561256
}
12571257

12581258
max_data_sz = PAGE_SIZE - headroom - tailroom;
1259-
if (size > max_data_sz) {
1259+
linear_data_sz = (ctx && ctx->data_end != size) ? ctx->data_end : max_data_sz;
1260+
if (size > linear_data_sz) {
12601261
/* disallow live data mode for jumbo frames */
12611262
if (do_live)
12621263
goto free_ctx;
1263-
size = max_data_sz;
1264+
size = linear_data_sz;
12641265
}
12651266

12661267
data = bpf_test_init(kattr, size, max_data_sz, 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
@@ -96,9 +96,7 @@ void test_xdp_context_test_run(void)
9696
/* Meta data must be 255 bytes or smaller */
9797
test_xdp_context_error(prog_fd, opts, 0, 256, sizeof(data), 0, 0, 0);
9898

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

0 commit comments

Comments
 (0)