Subject: Re: [PATCH net-next 3/4] net: stmmac: add Rx HWTS metadata to XDP receive pkt

From: Stanislav Fomichev <[email-masked]>
Date: Mon, 10 Apr 2023 09:25:15 -0700

On 04/10, Song Yoong Siang wrote:
> Add receive hardware timestamp metadata support via kfunc to XDP receive
> packets.
> 
> Signed-off-by: Song Yoong Siang <[email-masked]>
> ---
>  drivers/net/ethernet/stmicro/stmmac/stmmac.h  |  1 +
>  .../net/ethernet/stmicro/stmmac/stmmac_main.c | 24 +++++++++++++++++--
>  2 files changed, 23 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac.h b/drivers/net/ethernet/stmicro/stmmac/stmmac.h
> index ac8ccf851708..760445275da8 100644
> --- a/drivers/net/ethernet/stmicro/stmmac/stmmac.h
> +++ b/drivers/net/ethernet/stmicro/stmmac/stmmac.h
> @@ -94,6 +94,7 @@ struct stmmac_rx_buffer {
>  
>  struct stmmac_xdp_buff {
>  	struct xdp_buff xdp;
> +	ktime_t rx_hwts;
>  };
>  
>  struct stmmac_rx_queue {
> diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
> index f7bbdf04d20c..ca183fbfde85 100644
> --- a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
> +++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
> @@ -5307,6 +5307,8 @@ static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
>  			}
>  		}
>  

[..]

> +		stmmac_get_rx_hwtstamp(priv, p, np, &ctx.rx_hwts);

Do we want to pay this cost for every packet?

The preferred alternative is to store enough state in the
stmmac_xdp_buff so we can get to this data from stmmac_xdp_rx_timestamp.

I haven't read this code, but tentatively:
- move priv, p, np into stmmac_xdp_buff, assign them here instead of
  calling stmmac_get_rx_hwtstamp
- call stmmac_get_rx_hwtstamp from stmmac_xdp_rx_timestamp with the
  stored priv, p, np

That would ensure that we won't waste the cycles pulling out the rx
timestamp for every packet if the higher levels / users don't care.

Would something like this work?

> +
>  		if (!skb) {
>  			unsigned int pre_len, sync_len;
>  
> @@ -5315,7 +5317,7 @@ static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
>  
>  			xdp_init_buff(&ctx.xdp, buf_sz, &rx_q->xdp_rxq);
>  			xdp_prepare_buff(&ctx.xdp, page_address(buf->page),
> -					 buf->page_offset, buf1_len, false);
> +					 buf->page_offset, buf1_len, true);
>  
>  			pre_len = ctx.xdp.data_end - ctx.xdp.data_hard_start -
>  				  buf->page_offset;
> @@ -5411,7 +5413,7 @@ static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
>  
>  		shhwtstamp = skb_hwtstamps(skb);
>  		memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
> -		stmmac_get_rx_hwtstamp(priv, p, np, &shhwtstamp->hwtstamp);
> +		shhwtstamp->hwtstamp = ctx.rx_hwts;
>  
>  		stmmac_rx_vlan(priv->dev, skb);
>  		skb->protocol = eth_type_trans(skb, priv->dev);
> @@ -7071,6 +7073,22 @@ void stmmac_fpe_handshake(struct stmmac_priv *priv, bool enable)
>  	}
>  }
>  
> +static int stmmac_xdp_rx_timestamp(const struct xdp_md *_ctx, u64 *timestamp)
> +{
> +	const struct stmmac_xdp_buff *ctx = (void *)_ctx;
> +
> +	if (ctx->rx_hwts) {
> +		*timestamp = ctx->rx_hwts;
> +		return 0;
> +	}
> +
> +	return -ENODATA;
> +}
> +
> +const struct xdp_metadata_ops stmmac_xdp_metadata_ops = {
> +	.xmo_rx_timestamp		= stmmac_xdp_rx_timestamp,
> +};
> +
>  /**
>   * stmmac_dvr_probe
>   * @device: device pointer
> @@ -7178,6 +7196,8 @@ int stmmac_dvr_probe(struct device *device,
>  
>  	ndev->netdev_ops = &stmmac_netdev_ops;
>  
> +	ndev->xdp_metadata_ops = &stmmac_xdp_metadata_ops;
> +
>  	ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
>  			    NETIF_F_RXCSUM;
>  	ndev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
> -- 
> 2.34.1
>