Skip to content

Commit bc56c91

Browse files
netoptimizerAlexei Starovoitov
authored andcommitted
bpf: Add xdp.frame_sz in bpf_prog_test_run_xdp().
Update the memory requirements, when adding xdp.frame_sz in BPF test_run function bpf_prog_test_run_xdp() which e.g. is used by XDP selftests. Specifically add the expected reserved tailroom, but also allocated a larger memory area to reflect that XDP frames usually comes in this format. Limit the provided packet data size to 4096 minus headroom + tailroom, as this also reflect a common 3520 bytes MTU limit with XDP. Note that bpf_test_init already use a memory allocation method that clears memory. Thus, this already guards against leaking uninit kernel memory. Signed-off-by: Jesper Dangaard Brouer <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/158945349549.97035.15316291762482444006.stgit@firesoul
1 parent ddb47d5 commit bc56c91

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

net/bpf/test_run.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,34 +470,42 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
470470
int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
471471
union bpf_attr __user *uattr)
472472
{
473+
u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
474+
u32 headroom = XDP_PACKET_HEADROOM;
473475
u32 size = kattr->test.data_size_in;
474476
u32 repeat = kattr->test.repeat;
475477
struct netdev_rx_queue *rxqueue;
476478
struct xdp_buff xdp = {};
477479
u32 retval, duration;
480+
u32 max_data_sz;
478481
void *data;
479482
int ret;
480483

481484
if (kattr->test.ctx_in || kattr->test.ctx_out)
482485
return -EINVAL;
483486

484-
data = bpf_test_init(kattr, size, XDP_PACKET_HEADROOM + NET_IP_ALIGN, 0);
487+
/* XDP have extra tailroom as (most) drivers use full page */
488+
max_data_sz = 4096 - headroom - tailroom;
489+
if (size > max_data_sz)
490+
return -EINVAL;
491+
492+
data = bpf_test_init(kattr, max_data_sz, headroom, tailroom);
485493
if (IS_ERR(data))
486494
return PTR_ERR(data);
487495

488496
xdp.data_hard_start = data;
489-
xdp.data = data + XDP_PACKET_HEADROOM + NET_IP_ALIGN;
497+
xdp.data = data + headroom;
490498
xdp.data_meta = xdp.data;
491499
xdp.data_end = xdp.data + size;
500+
xdp.frame_sz = headroom + max_data_sz + tailroom;
492501

493502
rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
494503
xdp.rxq = &rxqueue->xdp_rxq;
495504
bpf_prog_change_xdp(NULL, prog);
496505
ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration, true);
497506
if (ret)
498507
goto out;
499-
if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN ||
500-
xdp.data_end != xdp.data + size)
508+
if (xdp.data != data + headroom || xdp.data_end != xdp.data + size)
501509
size = xdp.data_end - xdp.data;
502510
ret = bpf_test_finish(kattr, uattr, xdp.data, size, retval, duration);
503511
out:

0 commit comments

Comments
 (0)