Reserve the skb core extension area in the headroom, right after the xdp_frame. An XDP program stores RX metadata there for the skb that is built later.
The area begins with a header, struct xdp_skb_ext. It records the offset of each entry and the end of the last entry, in 4-byte units. bpf_xdp_adjust_head(), bpf_xdp_adjust_meta() and bpf_xdp_pull_data() use xdp_skb_ext_end() as their lower bound, so packet data and metadata cannot overwrite the entries. The XDP_FLAGS_HAS_SKB_EXT flag indicates that the header is valid. Most drivers reuse the xdp_buff between packets, so xdp_prepare_buff() clears it. With BPF_F_TEST_XDP_LIVE_FRAMES, reset_ctx() restores the flags before each run, and ctx_was_changed() compares them. Suggested-by: Jesper Dangaard Brouer <[email protected]> Link: https://lore.kernel.org/all/[email protected]/ Signed-off-by: Tianyi Gao <[email protected]> --- include/net/xdp.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ net/bpf/test_run.c | 4 +++- net/core/filter.c | 8 +++----- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/include/net/xdp.h b/include/net/xdp.h index 07231adfb5f8..c888353425d4 100644 --- a/include/net/xdp.h +++ b/include/net/xdp.h @@ -81,6 +81,9 @@ enum xdp_buff_flags { * XDP program is not attached. */ XDP_FLAGS_FRAGS_UNREADABLE = BIT(2), + XDP_FLAGS_HAS_SKB_EXT = BIT(3), /* skb core extension header + * is valid + */ }; struct xdp_buff { @@ -146,6 +149,16 @@ static __always_inline void xdp_buff_clear_frag_pfmemalloc(struct xdp_buff *xdp) xdp->flags &= ~XDP_FLAGS_FRAGS_PF_MEMALLOC; } +static __always_inline bool xdp_buff_has_skb_ext(const struct xdp_buff *xdp) +{ + return !!(xdp->flags & XDP_FLAGS_HAS_SKB_EXT); +} + +static __always_inline void xdp_buff_clear_skb_ext_flag(struct xdp_buff *xdp) +{ + xdp->flags &= ~XDP_FLAGS_HAS_SKB_EXT; +} + static __always_inline void xdp_init_buff(struct xdp_buff *xdp, u32 frame_sz, struct xdp_rxq_info *rxq) { @@ -174,6 +187,7 @@ xdp_prepare_buff(struct xdp_buff *xdp, unsigned char *hard_start, xdp->data = data; xdp->data_end = data + data_len; xdp->data_meta = meta_valid ? data : data + 1; + xdp_buff_clear_skb_ext_flag(xdp); } /* Reserve memory area at end-of data area. @@ -450,6 +464,37 @@ struct xdp_frame *xdp_convert_buff_to_frame(struct xdp_buff *xdp) return xdp_frame; } +/* skb core extensions: RX metadata that an XDP program stores in the + * headroom right after the xdp_frame, for the skb that is built later. + */ +enum xdp_skb_ext_id { + XDP_SKB_EXT_HASH, + XDP_SKB_EXT_VLAN, + XDP_SKB_EXT_CSUM, + XDP_SKB_EXT_NUM, +}; + +#define XDP_SKB_EXT_ALIGN 4 + +struct xdp_skb_ext { + u8 offset[XDP_SKB_EXT_NUM]; /* in chunks of 4 bytes */ + u8 chunks; /* same */ +}; + +static __always_inline struct xdp_skb_ext *xdp_skb_ext(struct xdp_frame *frame) +{ + return (void *)(frame + 1); +} + +static __always_inline void *xdp_skb_ext_end(const struct xdp_buff *xdp) +{ + struct xdp_skb_ext *ext = xdp_skb_ext(xdp->data_hard_start); + + if (!xdp_buff_has_skb_ext(xdp)) + return ext; + return (void *)ext + ext->chunks * XDP_SKB_EXT_ALIGN; +} + void __xdp_return(netmem_ref netmem, enum xdp_mem_type mem_type, bool napi_direct, struct xdp_buff *xdp); void xdp_return_frame(struct xdp_frame *xdpf); diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c index d6c9dc62182e..f25581851339 100644 --- a/net/bpf/test_run.c +++ b/net/bpf/test_run.c @@ -223,7 +223,8 @@ static bool ctx_was_changed(struct xdp_page_head *head) { return head->orig_ctx.data != head->ctx.data || head->orig_ctx.data_meta != head->ctx.data_meta || - head->orig_ctx.data_end != head->ctx.data_end; + head->orig_ctx.data_end != head->ctx.data_end || + head->orig_ctx.flags != head->ctx.flags; } static void reset_ctx(struct xdp_page_head *head) @@ -234,6 +235,7 @@ static void reset_ctx(struct xdp_page_head *head) head->ctx.data = head->orig_ctx.data; head->ctx.data_meta = head->orig_ctx.data_meta; head->ctx.data_end = head->orig_ctx.data_end; + head->ctx.flags = head->orig_ctx.flags; xdp_update_frame_from_buff(&head->ctx, head->frame); head->frame->mem_type = head->orig_ctx.rxq->mem.type; } diff --git a/net/core/filter.c b/net/core/filter.c index 9245009d9f1e..4f6698f75d90 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -4243,9 +4243,8 @@ static unsigned long xdp_get_metalen(const struct xdp_buff *xdp) BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset) { - void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame); unsigned long metalen = xdp_get_metalen(xdp); - void *data_start = xdp_frame_end + metalen; + void *data_start = xdp_skb_ext_end(xdp) + metalen; void *data = xdp->data + offset; if (unlikely(data < data_start || @@ -4547,13 +4546,12 @@ static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = { BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset) { - void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame); void *meta = xdp->data_meta + offset; unsigned long metalen = xdp->data - meta; if (xdp_data_meta_unsupported(xdp)) return -ENOTSUPP; - if (unlikely(meta < xdp_frame_end || + if (unlikely(meta < xdp_skb_ext_end(xdp) || meta > xdp->data)) return -EINVAL; if (unlikely(xdp_metalen_invalid(metalen))) @@ -12794,7 +12792,7 @@ __bpf_kfunc int bpf_xdp_pull_data(struct xdp_md *x, u32 len) start = xdp_data_meta_unsupported(xdp) ? xdp->data : xdp->data_meta; - headroom = start - xdp->data_hard_start - sizeof(struct xdp_frame); + headroom = start - xdp_skb_ext_end(xdp); tailroom = data_hard_end - xdp->data_end; delta = len - data_len; -- 2.56.0
