[RFC bpf-next 4/4] net: xdp: Update rx timestamp of xdp_rx_meta struct running xmo_rx_timestamp callback

Lorenzo Bianconi
2024-09-21T16:53:18Z

Set rx_timestamp in xdp_rx_meta struct of xdp_buff/xdp_frame according
to the value reported by the hw.
Update the xmo_rx_timestamp callback of xdp_metadata_ops for the
following drivers:
- ice
- igc
- mlx5
- mlx4
- stmmac
- veth
Set rx_timestamp reported by the hw converting the xdp_frame into a
sk_buff.

Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
---
drivers/net/ethernet/intel/ice/ice_txrx_lib.c | 3 +++
drivers/net/ethernet/intel/igc/igc_main.c | 3 +++
drivers/net/ethernet/mellanox/mlx4/en_rx.c | 2 ++
.../net/ethernet/mellanox/mlx5/core/en/xdp.c | 3 +++
.../net/ethernet/stmicro/stmmac/stmmac_main.c | 3 +++
drivers/net/veth.c | 5 +++++
include/net/xdp.h | 20 +++++++++++++++++++
net/core/xdp.c | 6 ++++++
8 files changed, 45 insertions(+)

diff --git a/drivers/net/ethernet/intel/ice/ice_txrx_lib.c b/drivers/net/ethernet/intel/ice/ice_txrx_lib.c
index 74dabe5b0c35c..d5ff77784d756 100644
--- a/drivers/net/ethernet/intel/ice/ice_txrx_lib.c
+++ b/drivers/net/ethernet/intel/ice/ice_txrx_lib.c
@@ -489,12 +489,15 @@ void ice_finalize_xdp_rx(struct ice_tx_ring *xdp_ring, unsigned int xdp_res,
static int ice_xdp_rx_hw_ts(const struct xdp_md *ctx, u64 *ts_ns)
{
const struct ice_xdp_buff *xdp_ext = (void *)ctx;
+ struct xdp_buff *xdp = (void *)&(xdp_ext->xdp_buff);

*ts_ns = ice_ptp_get_rx_hwts(xdp_ext->eop_desc,
xdp_ext->pkt_ctx);
if (!*ts_ns)
return -ENODATA;

+ xdp_set_rx_meta_ts(xdp, *ts_ns);
+
return 0;
}

diff --git a/drivers/net/ethernet/intel/igc/igc_main.c b/drivers/net/ethernet/intel/igc/igc_main.c
index ed22a7a70695e..240c52f0c6ec4 100644
--- a/drivers/net/ethernet/intel/igc/igc_main.c
+++ b/drivers/net/ethernet/intel/igc/igc_main.c
@@ -6795,7 +6795,10 @@ static int igc_xdp_rx_timestamp(const struct xdp_md *_ctx, u64 *timestamp)
struct igc_inline_rx_tstamps *tstamp = ctx->rx_ts;

if (igc_test_staterr(ctx->rx_desc, IGC_RXDADV_STAT_TSIP)) {
+ struct xdp_buff *xdp = (void *)&(ctx->xdp);
+
*timestamp = igc_ptp_rx_pktstamp(adapter, tstamp->timer0);
+ xdp_set_rx_meta_ts(xdp, *timestamp);

return 0;
}
diff --git a/drivers/net/ethernet/mellanox/mlx4/en_rx.c b/drivers/net/ethernet/mellanox/mlx4/en_rx.c
index ef6c687866f9d..dc714b3d9631c 100644
--- a/drivers/net/ethernet/mellanox/mlx4/en_rx.c
+++ b/drivers/net/ethernet/mellanox/mlx4/en_rx.c
@@ -681,6 +681,8 @@ int mlx4_en_xdp_rx_timestamp(const struct xdp_md *ctx, u64 *timestamp)

*timestamp = mlx4_en_get_hwtstamp(_ctx->mdev,
mlx4_en_get_cqe_ts(_ctx->cqe));
+ xdp_set_rx_meta_ts(&(_ctx->xdp), *timestamp);
+
return 0;
}

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en/xdp.c b/drivers/net/ethernet/mellanox/mlx5/core/en/xdp.c
index d3b7eee031470..507322e0c8dd0 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en/xdp.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en/xdp.c
@@ -178,12 +178,15 @@ mlx5e_xmit_xdp_buff(struct mlx5e_xdpsq *sq, struct mlx5e_rq *rq,
static int mlx5e_xdp_rx_timestamp(const struct xdp_md *ctx, u64 *timestamp)
{
const struct mlx5e_xdp_buff *_ctx = (void *)ctx;
+ struct xdp_buff *xdp = (void *)&(_ctx->xdp);

if (unlikely(!mlx5e_rx_hw_stamp(_ctx->rq->tstamp)))
return -ENODATA;

*timestamp = mlx5e_cqe_ts_to_ns(_ctx->rq->ptp_cyc2time,
_ctx->rq->clock, get_cqe_ts(_ctx->cqe));
+ xdp_set_rx_meta_ts(xdp, *timestamp);
+
return 0;
}

diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
index f3a1b179aaeac..4da3200816eda 100644
--- a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
+++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
@@ -7458,9 +7458,12 @@ static int stmmac_xdp_rx_timestamp(const struct xdp_md *_ctx, u64 *timestamp)

/* Check if timestamp is available */
if (stmmac_get_rx_timestamp_status(priv, desc, ndesc, priv->adv_ts)) {
+ struct xdp_buff *xdp = (void *)&(ctx->xdp);
+
stmmac_get_timestamp(priv, desc_contains_ts, priv->adv_ts, &ns);
ns -= priv->plat->cdc_error_adj;
*timestamp = ns_to_ktime(ns);
+ xdp_set_rx_meta_ts(xdp, *timestamp);
return 0;
}

diff --git a/drivers/net/veth.c b/drivers/net/veth.c
index 3a4b81104a6bd..3eaba1709a915 100644
--- a/drivers/net/veth.c
+++ b/drivers/net/veth.c
@@ -1619,6 +1619,11 @@ static int veth_xdp_rx_timestamp(const struct xdp_md *ctx, u64 *timestamp)
return -ENODATA;

*timestamp = skb_hwtstamps(_ctx->skb)->hwtstamp;
+ /* Here we just need to set the XDP_FLAGS_META_RX_TS bit in the
+ * xdp_buff flags.
+ */
+ xdp_buff_set_rx_meta_ts_flag(&(_ctx->xdp));
+
return 0;
}

diff --git a/include/net/xdp.h b/include/net/xdp.h
index 2ffaad806b9ed..c30c01b23709e 100644
--- a/include/net/xdp.h
+++ b/include/net/xdp.h
@@ -88,6 +88,7 @@ enum xdp_buff_flags {
*/
XDP_FLAGS_META_RX_HASH = BIT(2), /* hw rx hash */
XDP_FLAGS_META_RX_VLAN = BIT(3), /* hw rx vlan */
+ XDP_FLAGS_META_RX_TS = BIT(4), /* hw rx timestamp */
};
#define XDP_FLAGS_META_RX (XDP_FLAGS_META_RX_HASH | \
XDP_FLAGS_META_RX_VLAN)
@@ -134,6 +135,11 @@ static __always_inline bool xdp_buff_has_rx_meta(struct xdp_buff *xdp)
return !!(xdp->flags & XDP_FLAGS_META_RX);
}

+static __always_inline void xdp_buff_set_rx_meta_ts_flag(struct xdp_buff *xdp)
+{
+ xdp->flags |= XDP_FLAGS_META_RX_TS;
+}
+
static __always_inline void
xdp_init_buff(struct xdp_buff *xdp, u32 frame_sz, struct xdp_rxq_info *rxq)
{
@@ -224,6 +230,11 @@ static __always_inline bool xdp_frame_has_rx_meta_vlan(struct xdp_frame *frame)
return !!(frame->flags & XDP_FLAGS_META_RX_VLAN);
}

+static __always_inline bool xdp_frame_has_rx_meta_ts(struct xdp_frame *frame)
+{
+ return !!(frame->flags & XDP_FLAGS_META_RX_TS);
+}
+
#define XDP_BULK_QUEUE_SIZE 16
struct xdp_frame_bulk {
int count;
@@ -532,6 +543,15 @@ xdp_set_rx_meta_vlan(struct xdp_buff *xdp, __be16 vlan_proto,
xdp->flags |= XDP_FLAGS_META_RX_VLAN;
}

+static __always_inline void xdp_set_rx_meta_ts(struct xdp_buff *xdp, u64 ts)
+{
+ 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;
+}
+
#ifdef CONFIG_NET
u32 bpf_xdp_metadata_kfunc_id(int id);
bool bpf_dev_bound_kfunc_id(u32 btf_id);
diff --git a/net/core/xdp.c b/net/core/xdp.c
index 84d6b134f8e97..5c9efcf1436d0 100644
--- a/net/core/xdp.c
+++ b/net/core/xdp.c
@@ -619,11 +619,15 @@ struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,
unsigned int headroom, frame_size;
void *hard_start;
u8 nr_frags;
+ u64 ts;

/* xdp frags frame */
if (unlikely(xdp_frame_has_frags(xdpf)))
nr_frags = sinfo->nr_frags;

+ if (unlikely(xdp_frame_has_rx_meta_ts(xdpf)))
+ ts = sinfo->hwtstamps.hwtstamp;
+
/* Part of headroom was reserved to xdpf */
headroom = sizeof(*xdpf) + xdpf->headroom;

@@ -655,6 +659,8 @@ struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,
if (xdp_frame_has_rx_meta_vlan(xdpf))
__vlan_hwaccel_put_tag(skb, xdpf->rx_meta.vlan.proto,
xdpf->rx_meta.vlan.tci);
+ if (unlikely(xdp_frame_has_rx_meta_ts(xdpf)))
+ skb_hwtstamps(skb)->hwtstamp = ts;

/* Optional SKB info, currently missing:
* - HW checksum info (skb->ip_summed)
--
2.46.1