Skip to content

Commit 294635a

Browse files
alobakinMartin KaFai Lau
authored andcommitted
bpf, test_run: fix &xdp_frame misplacement for LIVE_FRAMES
&xdp_buff and &xdp_frame are bound in a way that xdp_buff->data_hard_start == xdp_frame It's always the case and e.g. xdp_convert_buff_to_frame() relies on this. IOW, the following: for (u32 i = 0; i < 0xdead; i++) { xdpf = xdp_convert_buff_to_frame(&xdp); xdp_convert_frame_to_buff(xdpf, &xdp); } shouldn't ever modify @xdpf's contents or the pointer itself. However, "live packet" code wrongly treats &xdp_frame as part of its context placed *before* the data_hard_start. With such flow, data_hard_start is sizeof(*xdpf) off to the right and no longer points to the XDP frame. Instead of replacing `sizeof(ctx)` with `offsetof(ctx, xdpf)` in several places and praying that there are no more miscalcs left somewhere in the code, unionize ::frm with ::data in a flex array, so that both starts pointing to the actual data_hard_start and the XDP frame actually starts being a part of it, i.e. a part of the headroom, not the context. A nice side effect is that the maximum frame size for this mode gets increased by 40 bytes, as xdp_buff::frame_sz includes everything from data_hard_start (-> includes xdpf already) to the end of XDP/skb shared info. Also update %MAX_PKT_SIZE accordingly in the selftests code. Leave it hardcoded for 64 bit && 4k pages, it can be made more flexible later on. Minor: align `&head->data` with how `head->frm` is assigned for consistency. Minor #2: rename 'frm' to 'frame' in &xdp_page_head while at it for clarity. (was found while testing XDP traffic generator on ice, which calls xdp_convert_frame_to_buff() for each XDP frame) Fixes: b530e9e ("bpf: Add "live packet" mode for XDP in BPF_PROG_RUN") Acked-by: Toke Høiland-Jørgensen <[email protected]> Signed-off-by: Alexander Lobakin <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent b7abcd9 commit 294635a

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

net/bpf/test_run.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,11 @@ static bool bpf_test_timer_continue(struct bpf_test_timer *t, int iterations,
9797
struct xdp_page_head {
9898
struct xdp_buff orig_ctx;
9999
struct xdp_buff ctx;
100-
struct xdp_frame frm;
101-
u8 data[];
100+
union {
101+
/* ::data_hard_start starts here */
102+
DECLARE_FLEX_ARRAY(struct xdp_frame, frame);
103+
DECLARE_FLEX_ARRAY(u8, data);
104+
};
102105
};
103106

104107
struct xdp_test_data {
@@ -113,6 +116,10 @@ struct xdp_test_data {
113116
u32 frame_cnt;
114117
};
115118

119+
/* tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c:%MAX_PKT_SIZE
120+
* must be updated accordingly this gets changed, otherwise BPF selftests
121+
* will fail.
122+
*/
116123
#define TEST_XDP_FRAME_SIZE (PAGE_SIZE - sizeof(struct xdp_page_head))
117124
#define TEST_XDP_MAX_BATCH 256
118125

@@ -132,8 +139,8 @@ static void xdp_test_run_init_page(struct page *page, void *arg)
132139
headroom -= meta_len;
133140

134141
new_ctx = &head->ctx;
135-
frm = &head->frm;
136-
data = &head->data;
142+
frm = head->frame;
143+
data = head->data;
137144
memcpy(data + headroom, orig_ctx->data_meta, frm_len);
138145

139146
xdp_init_buff(new_ctx, TEST_XDP_FRAME_SIZE, &xdp->rxq);
@@ -223,7 +230,7 @@ static void reset_ctx(struct xdp_page_head *head)
223230
head->ctx.data = head->orig_ctx.data;
224231
head->ctx.data_meta = head->orig_ctx.data_meta;
225232
head->ctx.data_end = head->orig_ctx.data_end;
226-
xdp_update_frame_from_buff(&head->ctx, &head->frm);
233+
xdp_update_frame_from_buff(&head->ctx, head->frame);
227234
}
228235

229236
static int xdp_recv_frames(struct xdp_frame **frames, int nframes,
@@ -285,7 +292,7 @@ static int xdp_test_run_batch(struct xdp_test_data *xdp, struct bpf_prog *prog,
285292
head = phys_to_virt(page_to_phys(page));
286293
reset_ctx(head);
287294
ctx = &head->ctx;
288-
frm = &head->frm;
295+
frm = head->frame;
289296
xdp->frame_cnt++;
290297

291298
act = bpf_prog_run_xdp(prog, ctx);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,13 @@ static int attach_tc_prog(struct bpf_tc_hook *hook, int fd)
6565
}
6666

6767
/* The maximum permissible size is: PAGE_SIZE - sizeof(struct xdp_page_head) -
68-
* sizeof(struct skb_shared_info) - XDP_PACKET_HEADROOM = 3368 bytes
68+
* SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) - XDP_PACKET_HEADROOM =
69+
* 3408 bytes for 64-byte cacheline and 3216 for 256-byte one.
6970
*/
7071
#if defined(__s390x__)
71-
#define MAX_PKT_SIZE 3176
72+
#define MAX_PKT_SIZE 3216
7273
#else
73-
#define MAX_PKT_SIZE 3368
74+
#define MAX_PKT_SIZE 3408
7475
#endif
7576
static void test_max_pkt_size(int fd)
7677
{

0 commit comments

Comments
 (0)