[PATCH bpf-next V1 3/7] net: xdp: Add kfuncs to store hw metadata in xdp_buff

Jesper Dangaard Brouer
2025-06-03T17:46:13Z

From: Lorenzo Bianconi <lorenzo@kernel.org>

Introduce the following kfuncs to store hw metadata provided by the NIC
into the xdp_buff struct:

- rx-hash: bpf_xdp_store_rx_hash
- rx-vlan: bpf_xdp_store_rx_vlan
- rx-hw-ts: bpf_xdp_store_rx_ts

Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
Signed-off-by: Jesper Dangaard Brouer <hawk@kernel.org>
---
include/net/xdp.h | 5 +++++
net/core/xdp.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 50 insertions(+)

diff --git a/include/net/xdp.h b/include/net/xdp.h
index 5dcdf634ae4a..ef73f0bcc441 100644
--- a/include/net/xdp.h
+++ b/include/net/xdp.h
@@ -153,6 +153,11 @@ static __always_inline void xdp_buff_set_frag_pfmemalloc(struct xdp_buff *xdp)
xdp->flags |= XDP_FLAGS_FRAGS_PF_MEMALLOC;
}

+static __always_inline bool xdp_buff_has_valid_meta_area(struct xdp_buff *xdp)
+{
+ return !!(xdp->flags & XDP_FLAGS_META_AREA);
+}
+
static __always_inline void
xdp_init_buff(struct xdp_buff *xdp, u32 frame_sz, struct xdp_rxq_info *rxq)
{
diff --git a/net/core/xdp.c b/net/core/xdp.c
index 61edbd424494..7d59543d7916 100644
--- a/net/core/xdp.c
+++ b/net/core/xdp.c
@@ -957,12 +957,57 @@ __bpf_kfunc int bpf_xdp_metadata_rx_vlan_tag(const struct xdp_md *ctx,
return -EOPNOTSUPP;
}

+__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;
+
+ if (!xdp_buff_has_valid_meta_area(xdp))
+ return -ENOSPC;
+
+ xdp->rx_meta->hash.val = hash;
+ xdp->rx_meta->hash.type = rss_type;
+ xdp->flags |= XDP_FLAGS_META_RX_HASH;
+
+ return 0;
+}
+
+__bpf_kfunc int bpf_xdp_store_rx_vlan(struct xdp_md *ctx, __be16 vlan_proto,
+ u16 vlan_tci)
+{
+ struct xdp_buff *xdp = (struct xdp_buff *)ctx;
+
+ if (!xdp_buff_has_valid_meta_area(xdp))
+ return -ENOSPC;
+
+ xdp->rx_meta->vlan.proto = vlan_proto;
+ xdp->rx_meta->vlan.tci = vlan_tci;
+ xdp->flags |= XDP_FLAGS_META_RX_VLAN;
+
+ return 0;
+}
+
+__bpf_kfunc int bpf_xdp_store_rx_ts(struct xdp_md *ctx, u64 ts)
+{
+ struct xdp_buff *xdp = (struct xdp_buff *)ctx;
+ struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
+ struct skb_shared_hwtstamps *shwt = &sinfo->hwtstamps;
+
+ shwt->hwtstamp = ts;
+ xdp->flags |= XDP_FLAGS_META_RX_TS;
+
+ return 0;
+}
+
__bpf_kfunc_end_defs();

BTF_KFUNCS_START(xdp_metadata_kfunc_ids)
#define XDP_METADATA_KFUNC(_, __, name, ___) BTF_ID_FLAGS(func, name, KF_TRUSTED_ARGS)
XDP_METADATA_KFUNC_xxx
#undef XDP_METADATA_KFUNC
+BTF_ID_FLAGS(func, bpf_xdp_store_rx_hash)
+BTF_ID_FLAGS(func, bpf_xdp_store_rx_vlan)
+BTF_ID_FLAGS(func, bpf_xdp_store_rx_ts)
BTF_KFUNCS_END(xdp_metadata_kfunc_ids)

static const struct btf_kfunc_id_set xdp_metadata_kfunc_set = {