Thread View: linux.kernel.netdev
2 messages
2 total messages
Started by "Alexei Starovoitov"
Thu, 12 Nov 2026 00:34
Re: [PATCH bpf-next 00/15] xdp: add SKB core extensions to keep RX metadata until skb creation
Author: "Alexei Starovoitov"
Date: Thu, 12 Nov 2026 00:34
Date: Thu, 12 Nov 2026 00:34
344 lines
24182 bytes
24182 bytes
On Mon, Nov 09, 2026 at 07:56 PM Tianyi Gao <tianyi@cloudflare.com> wrote:
> The layout of the area follows skb extensions (struct skb_ext). A small
> header gives the offset of each entry. An entry takes headroom only when
> the program stores it, so the area does not grow with each new type of
> RX metadata, unlike [4]. Unlike skb_ext, the area needs no allocation.
I'm with Stan on this. It looks heavy.
How about the following idea:
diff --git a/include/net/xdp.h b/include/net/xdp.h
index 07231ad..7f30d24 100644
--- a/include/net/xdp.h
+++ b/include/net/xdp.h
@@ -81,8 +81,15 @@ enum xdp_buff_flags {
* XDP program is not attached.
*/
XDP_FLAGS_FRAGS_UNREADABLE = BIT(2),
+ /* The prog stored these members of struct xdp_rx_meta */
+ XDP_FLAGS_RX_HASH = BIT(3),
+ XDP_FLAGS_RX_VLAN = BIT(4),
+ XDP_FLAGS_RX_CSUM = BIT(5),
};
+#define XDP_FLAGS_RX_META \
+ (XDP_FLAGS_RX_HASH | XDP_FLAGS_RX_VLAN | XDP_FLAGS_RX_CSUM)
+
struct xdp_buff {
void *data;
void *data_end;
@@ -174,6 +181,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->flags &= ~XDP_FLAGS_RX_META;
}
/* Reserve memory area at end-of data area.
@@ -310,6 +318,48 @@ struct xdp_frame {
u32 flags; /* supported values defined in xdp_buff_flags */
};
+/*
+ * RX metadata that an XDP prog stores for the skb that is built later.
+ * It follows the xdp_frame in the headroom. The headroom is taken only
+ * when one of XDP_FLAGS_RX_META is set.
+ */
+struct xdp_rx_meta {
+ u32 hash;
+ __be16 vlan_proto;
+ u16 vlan_tci;
+ u8 hash_type; /* enum pkt_hash_types */
+ u8 ip_summed;
+ u8 csum_level;
+};
+
+static __always_inline struct xdp_rx_meta *xdp_rx_meta(void *data_hard_start)
+{
+ return data_hard_start + sizeof(struct xdp_frame);
+}
+
+/* The lowest address that the packet and data_meta can use */
+static __always_inline void *xdp_buff_headroom_start(const struct xdp_buff *xdp)
+{
+ struct xdp_rx_meta *meta = xdp_rx_meta(xdp->data_hard_start);
+
+ return xdp->flags & XDP_FLAGS_RX_META ? meta + 1 : meta;
+}
+
+void __xdp_rx_meta_to_skb(struct sk_buff *skb, const struct xdp_rx_meta *meta,
+ u32 flags);
+
+/*
+ * A driver calls it on XDP_PASS after it sets the skb fields from
+ * the RX descriptor.
+ */
+static inline void xdp_buff_rx_meta_to_skb(const struct xdp_buff *xdp,
+ struct sk_buff *skb)
+{
+ if (unlikely(xdp->flags & XDP_FLAGS_RX_META))
+ __xdp_rx_meta_to_skb(skb, xdp_rx_meta(xdp->data_hard_start),
+ xdp->flags);
+}
+
static __always_inline bool xdp_frame_has_frags(const struct xdp_frame *frame)
{
return !!(frame->flags & XDP_FLAGS_HAS_FRAGS);
@@ -648,6 +698,12 @@ enum xdp_rss_hash_type {
XDP_RSS_TYPE_L4_IPV6_SCTP_EX = XDP_RSS_TYPE_L4_IPV6_SCTP | XDP_RSS_L3_DYNHDR,
};
+enum xdp_checksum {
+ XDP_CHECKSUM_NONE = BIT(CHECKSUM_NONE),
+ XDP_CHECKSUM_UNNECESSARY = BIT(CHECKSUM_UNNECESSARY),
+ XDP_CHECKSUM_COMPLETE = BIT(CHECKSUM_COMPLETE),
+};
+
struct xdp_metadata_ops {
int (*xmo_rx_timestamp)(const struct xdp_md *ctx, u64 *timestamp);
int (*xmo_rx_hash)(const struct xdp_md *ctx, u32 *hash,
diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
index d6c9dc6..f255818 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 9245009..efbe293 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_buff_headroom_start(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_buff_headroom_start(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_buff_headroom_start(xdp);
tailroom = data_hard_end - xdp->data_end;
delta = len - data_len;
diff --git a/net/core/xdp.c b/net/core/xdp.c
index 1d679e8..ca17609 100644
--- a/net/core/xdp.c
+++ b/net/core/xdp.c
@@ -829,10 +829,10 @@ struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,
/* Essential SKB info: protocol and skb->dev */
skb->protocol = eth_type_trans(skb, dev);
+ if (xdpf->flags & XDP_FLAGS_RX_META)
+ __xdp_rx_meta_to_skb(skb, xdp_rx_meta(xdpf), xdpf->flags);
/* Optional SKB info, currently missing:
- * - HW checksum info (skb->ip_summed)
- * - HW RX hash (skb_set_hash)
* - RX ring dev queue index (skb_record_rx_queue)
*/
@@ -857,6 +857,9 @@ struct sk_buff *xdp_build_skb_from_frame(struct xdp_frame *xdpf,
memset(skb, 0, offsetof(struct sk_buff, tail));
+ /* The callers set the RX fields themselves or transmit the skb */
+ xdpf->flags &= ~XDP_FLAGS_RX_META;
+
return __xdp_build_skb_from_frame(xdpf, skb, dev);
}
EXPORT_SYMBOL_GPL(xdp_build_skb_from_frame);
@@ -990,9 +993,154 @@ bool bpf_dev_bound_kfunc_id(u32 btf_id)
return btf_id_set8_contains(&xdp_metadata_kfunc_ids, btf_id);
}
+void __xdp_rx_meta_to_skb(struct sk_buff *skb, const struct xdp_rx_meta *meta,
+ u32 flags)
+{
+ if (flags & XDP_FLAGS_RX_HASH)
+ skb_set_hash(skb, meta->hash, meta->hash_type);
+ if (flags & XDP_FLAGS_RX_VLAN)
+ __vlan_hwaccel_put_tag(skb, meta->vlan_proto, meta->vlan_tci);
+ if (flags & XDP_FLAGS_RX_CSUM) {
+ skb->ip_summed = meta->ip_summed;
+ skb->csum_level = meta->csum_level;
+ }
+}
+EXPORT_SYMBOL_GPL(__xdp_rx_meta_to_skb);
+
+static struct xdp_rx_meta *xdp_rx_meta_reserve(const struct xdp_buff *xdp)
+{
+ struct xdp_rx_meta *meta = xdp_rx_meta(xdp->data_hard_start);
+ void *start = xdp_data_meta_unsupported(xdp) ? xdp->data : xdp->data_meta;
+
+ /* User space can write the headroom of AF_XDP zero-copy buffers */
+ if (xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL)
+ return ERR_PTR(-EOPNOTSUPP);
+ if ((void *)(meta + 1) > start)
+ return ERR_PTR(-ENOSPC);
+ return meta;
+}
+
+__bpf_kfunc_start_defs();
+
+/**
+ * bpf_xdp_store_rx_hash - Store the RX hash for the skb of this packet.
+ * @ctx: XDP context pointer.
+ * @hash: RX hash value.
+ * @rss_type: RSS hash type, see &enum xdp_rss_hash_type.
+ *
+ * Return:
+ * * Returns 0 on success or ``-errno`` on error.
+ * * ``-ENOSPC`` : not enough headroom
+ * * ``-EOPNOTSUPP`` : AF_XDP zero-copy buffer
+ */
+__bpf_kfunc int bpf_xdp_store_rx_hash(struct xdp_md *ctx, u32 hash,
+ enum xdp_rss_hash_type rss_type)
+{
+ struct xdp_buff *xdp = (struct xdp_buff *)ctx;
+ struct xdp_rx_meta *meta = xdp_rx_meta_reserve(xdp);
+
+ if (IS_ERR(meta))
+ return PTR_ERR(meta);
+
+ meta->hash = hash;
+ if (rss_type & XDP_RSS_L4)
+ meta->hash_type = PKT_HASH_TYPE_L4;
+ else if (rss_type & (XDP_RSS_L3_IPV4 | XDP_RSS_L3_IPV6))
+ meta->hash_type = PKT_HASH_TYPE_L3;
+ else
+ meta->hash_type = PKT_HASH_TYPE_NONE;
+ xdp->flags |= XDP_FLAGS_RX_HASH;
+
+ return 0;
+}
+
+/**
+ * bpf_xdp_store_rx_vlan_tag - Store the VLAN tag for the skb of this packet.
+ * @ctx: XDP context pointer.
+ * @vlan_proto: VLAN tag protocol identifier (TPID), in network byte order.
+ * @vlan_tci: VLAN TCI, in host byte order.
+ *
+ * Return:
+ * * Returns 0 on success or ``-errno`` on error.
+ * * ``-EINVAL`` : @vlan_proto is not 802.1Q or 802.1AD
+ * * ``-ENOSPC`` : not enough headroom
+ * * ``-EOPNOTSUPP`` : AF_XDP zero-copy buffer
+ */
+__bpf_kfunc int bpf_xdp_store_rx_vlan_tag(struct xdp_md *ctx, __be16 vlan_proto,
+ u16 vlan_tci)
+{
+ struct xdp_buff *xdp = (struct xdp_buff *)ctx;
+ struct xdp_rx_meta *meta = xdp_rx_meta_reserve(xdp);
+
+ if (IS_ERR(meta))
+ return PTR_ERR(meta);
+ if (vlan_proto != htons(ETH_P_8021Q) &&
+ vlan_proto != htons(ETH_P_8021AD))
+ return -EINVAL;
+
+ meta->vlan_proto = vlan_proto;
+ meta->vlan_tci = vlan_tci;
+ xdp->flags |= XDP_FLAGS_RX_VLAN;
+
+ return 0;
+}
+
+/**
+ * bpf_xdp_store_rx_csum - Store the checksum status for the skb of this packet.
+ * @ctx: XDP context pointer.
+ * @ip_summed: ``XDP_CHECKSUM_NONE`` or ``XDP_CHECKSUM_UNNECESSARY``.
+ * @csum_level: Number of consecutive verified checksums minus one, up to
+ * ``SKB_MAX_CSUM_LEVEL``. Must be 0 with ``XDP_CHECKSUM_NONE``.
+ *
+ * Return:
+ * * Returns 0 on success or ``-errno`` on error.
+ * * ``-EINVAL`` : @ip_summed is not supported, or @csum_level is not valid
+ * for it
+ * * ``-ENOSPC`` : not enough headroom
+ * * ``-EOPNOTSUPP`` : AF_XDP zero-copy buffer
+ */
+__bpf_kfunc int bpf_xdp_store_rx_csum(struct xdp_md *ctx,
+ enum xdp_checksum ip_summed,
+ u8 csum_level)
+{
+ struct xdp_buff *xdp = (struct xdp_buff *)ctx;
+ struct xdp_rx_meta *meta = xdp_rx_meta_reserve(xdp);
+
+ if (IS_ERR(meta))
+ return PTR_ERR(meta);
+ if (ip_summed == XDP_CHECKSUM_NONE && !csum_level)
+ meta->ip_summed = CHECKSUM_NONE;
+ else if (ip_summed == XDP_CHECKSUM_UNNECESSARY &&
+ csum_level <= SKB_MAX_CSUM_LEVEL)
+ meta->ip_summed = CHECKSUM_UNNECESSARY;
+ else
+ return -EINVAL;
+
+ meta->csum_level = csum_level;
+ xdp->flags |= XDP_FLAGS_RX_CSUM;
+
+ return 0;
+}
+
+__bpf_kfunc_end_defs();
+
+BTF_KFUNCS_START(xdp_rx_meta_kfunc_ids)
+BTF_ID_FLAGS(func, bpf_xdp_store_rx_hash)
+BTF_ID_FLAGS(func, bpf_xdp_store_rx_vlan_tag)
+BTF_ID_FLAGS(func, bpf_xdp_store_rx_csum)
+BTF_KFUNCS_END(xdp_rx_meta_kfunc_ids)
+
+static const struct btf_kfunc_id_set xdp_rx_meta_kfunc_set = {
+ .owner = THIS_MODULE,
+ .set = &xdp_rx_meta_kfunc_ids,
+};
+
static int __init xdp_metadata_init(void)
{
- return register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &xdp_metadata_kfunc_set);
+ int ret;
+
+ ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &xdp_rx_meta_kfunc_set);
+ return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &xdp_metadata_kfunc_set);
}
late_initcall(xdp_metadata_init);
Re: [PATCH bpf-next 00/15] xdp: add SKB core extensions to keep RX metadata until skb creation
Author: Jesper Dangaard Brouer
Date: Tue, 01 Dec 2026 09:09
Date: Tue, 01 Dec 2026 09:09
94 lines
14176 bytes
14176 bytes
On 11/12/26 15:55, Tianyi Gao wrote:
> Hi Alexei,
>
> On Thu, 12 Nov 2026 00:34:54 +0000, Alexei Starovoitov wrote:
>> I'm with Stan on this. It looks heavy.
>>
>> How about the following idea:
>
> Thank you very much for the patch and the idea. I really appreciate it!
>
> v1 uses the skb_ext-like layout mainly because of the earlier pushback
> on a fixed struct [1][2]. This series adds only the three most commonly
> needed types of RX metadata. A 12-byte fixed struct that takes headroom
> only when any RX metadata is stored can be a reasonable middle ground.
>
> I would like to hear from Jesper and Jakub before I move to v2.
>
Short version: the RX metadata must travel with the xdp_frame, in a
format the kernel knows. Fixed struct or skb_ext-like is secondary.
Jakub, this builds on your "driver always populates" idea [1].
I like Alexei's fixed struct, and I have changed my mind. In July I
proposed the skb_ext-like layout [2]. A fixed struct is simpler and
good enough. The fields are HW offloads that map to fixed skb members
(hash, VLAN, csum). The list is short and stable: 12 bytes today, and
below 24 bytes even with RX timestamp and RX queue index. It stays
inside the cache line of the xdp_frame.
Our main use case is XDP_REDIRECT into cpumap and veth. There, core
code builds the skb from the xdp_frame on another CPU: the cpumap
kthread, or veth in threaded NAPI mode, which we run in production.
Today the RX hash and the csum status are lost on the way. GRO on the
remote CPU then puts all flows in one bucket, and veth verifies every
checksum in software.
The values could travel in data_meta, with a second XDP prog at the
target that calls a setter. We do not want that. data_meta is a
contract between BPF progs, in a format the kernel does not know. And
it needs a BPF prog at every place an skb is built, also on veths
where a service owns the XDP prog.
Jakub, I know your objection is the model, not only the layout [3].
How I see it: the RX descriptor is a HW ABI. The NIC defines and
writes it, the kernel only reads it. The xmo callbacks (xmo_rx_hash,
xmo_rx_vlan_tag, ...) read the descriptor and decode it into one
common format, which maps to the skb.
A per-driver setter must store the value in one of two places:
- Back in the descriptor, in HW format. That is device-owned memory,
and some values have no field there (e.g. csum_level).
- In driver memory, in the decoded format. Then every driver keeps
its own copy of the same struct, with the same setter.
A kernel-known area after the xdp_frame is that copy, done once.
The bigger reason is the direction. When the kernel knows the format
(fixed struct or skb_ext-like), the xdp_frame becomes a mini-SKB: it
carries what the skb needs, so core code can build the skb. The end
goal is that NIC drivers stop allocating skbs, and send the xdp_frame
into the netstack instead. ice is already close: it builds the skb
with the generic xdp_build_skb_from_buff(), and then fills the fields
from its descriptor. Later, GRO can check an xdp_frame against the
current GRO packet, and skip the skb for every packet it merges. A
per-driver setter cannot get there. Driver-local state is gone when
the frame leaves the driver, and core code cannot read it.
Jakub, I agree with you that the driver should always populate the
area [1]. The difference is when: after the XDP prog has run.
1. The prog runs first. Nothing is populated, so XDP_DROP stays as
fast as today.
2. The prog stores the values it changed, for example the csum status
after a decap [4], or a new or cleared VLAN tag.
3. On XDP_PASS and XDP_REDIRECT, the driver fills the other fields
from its descriptor.
This also fixes XDP_PASS. Today the kernel does not know what XDP
changed, so the driver applies HW info that may no longer match the
packet, and XDP cannot change a stripped VLAN tag at all. We run with
VLAN offload disabled for this reason. A return code like
XDP_PASS_MODIFIED would only say "something changed". With a store,
the BPF-prog corrects exactly the fields it changed. This is the
explicit update/invalidate plan Jakub asked for [5].
--Jesper
[1] https://lore.kernel.org/all/20250805172831.213ddd8d@kernel.org/
[2]
https://lore.kernel.org/all/1d5521eb-84fc-4417-a3f5-6366694d160a@kernel.org/
[3] https://lore.kernel.org/all/20261117161856.6f6c2ce9@kernel.org/
[4] https://lore.kernel.org/all/20260227153231.78d16b69@kernel.org/
[5] https://lore.kernel.org/all/20260722113525.34092660@kernel.org/
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads