> Subject: [PATCH v6 4/9] net/af_xdp: use generic SW stats
> 
> Use common code for all SW stats.
> 
> Signed-off-by: Stephen Hemminger <step...@networkplumber.org>
> ---
>  drivers/net/af_xdp/rte_eth_af_xdp.c | 98 ++++++++---------------------
>  1 file changed, 25 insertions(+), 73 deletions(-)
> 
> diff --git a/drivers/net/af_xdp/rte_eth_af_xdp.c
> b/drivers/net/af_xdp/rte_eth_af_xdp.c
> index 268a130c49..65fc2f478f 100644
> --- a/drivers/net/af_xdp/rte_eth_af_xdp.c
> +++ b/drivers/net/af_xdp/rte_eth_af_xdp.c

[snip]

> @@ -541,6 +521,7 @@ af_xdp_tx_zc(void *queue, struct rte_mbuf **bufs,
> uint16_t nb_pkts)
> 
>       for (i = 0; i < nb_pkts; i++) {
>               mbuf = bufs[i];
> +             pkt_len = rte_pktmbuf_pkt_len(mbuf);
> 
>               if (mbuf->pool == umem->mb_pool) {
>                       if (!xsk_ring_prod__reserve(&txq->tx, 1, &idx_tx)) {
> @@ -589,17 +570,13 @@ af_xdp_tx_zc(void *queue, struct rte_mbuf **bufs,
> uint16_t nb_pkts)
>                       count++;
>               }
> 
> -             tx_bytes += mbuf->pkt_len;
> +             rte_eth_count_packet(&txq->stats, pkt_len);

This change resolves the bugzilla you reported recently (1440 - use after free 
in af_xdp). Should this be mentioned in the commit message? We probably still 
need a separate patch for backporting that can be used without this entire 
series.

>       }
> 
>  out:
>       xsk_ring_prod__submit(&txq->tx, count);
>       kick_tx(txq, cq);
> 
> -     txq->stats.tx_pkts += count;
> -     txq->stats.tx_bytes += tx_bytes;
> -     txq->stats.tx_dropped += nb_pkts - count;
> -
>       return count;
>  }
>  #else
> @@ -610,7 +587,6 @@ af_xdp_tx_cp(void *queue, struct rte_mbuf **bufs,
> uint16_t nb_pkts)
>       struct xsk_umem_info *umem = txq->umem;
>       struct rte_mbuf *mbuf;
>       void *addrs[ETH_AF_XDP_TX_BATCH_SIZE];
> -     unsigned long tx_bytes = 0;
>       int i;
>       uint32_t idx_tx;
>       struct xsk_ring_cons *cq = &txq->pair->cq;
> @@ -640,7 +616,8 @@ af_xdp_tx_cp(void *queue, struct rte_mbuf **bufs,
> uint16_t nb_pkts)
>               pkt = xsk_umem__get_data(umem->mz->addr,
>                                        desc->addr);
>               rte_memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *), desc-
> >len);
> -             tx_bytes += mbuf->pkt_len;
> +             rte_eth_qsw_update(&txq->stats, mbuf);

Typo? Assume this should be rte_eth_count_packet

> +
>               rte_pktmbuf_free(mbuf);
>       }
> 
> @@ -648,9 +625,6 @@ af_xdp_tx_cp(void *queue, struct rte_mbuf **bufs,
> uint16_t nb_pkts)
> 
>       kick_tx(txq, cq);
> 
> -     txq->stats.tx_pkts += nb_pkts;
> -     txq->stats.tx_bytes += tx_bytes;
> -
>       return nb_pkts;
>  }
> 
> @@ -847,39 +821,26 @@ eth_dev_info(struct rte_eth_dev *dev, struct
> rte_eth_dev_info *dev_info)
>  static int
>  eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
>  {
> -     struct pmd_internals *internals = dev->data->dev_private;
>       struct pmd_process_private *process_private = dev->process_private;
> -     struct xdp_statistics xdp_stats;
> -     struct pkt_rx_queue *rxq;
> -     struct pkt_tx_queue *txq;
> -     socklen_t optlen;
> -     int i, ret, fd;
> +     unsigned int i;
> 
> -     for (i = 0; i < dev->data->nb_rx_queues; i++) {
> -             optlen = sizeof(struct xdp_statistics);
> -             rxq = &internals->rx_queues[i];
> -             txq = rxq->pair;
> -             stats->q_ipackets[i] = rxq->stats.rx_pkts;
> -             stats->q_ibytes[i] = rxq->stats.rx_bytes;
> +     rte_eth_counters_stats_get(dev, offsetof(struct pkt_tx_queue, stats),
> +                                offsetof(struct pkt_rx_queue, stats), stats);
> 
> -             stats->q_opackets[i] = txq->stats.tx_pkts;
> -             stats->q_obytes[i] = txq->stats.tx_bytes;
> +     for (i = 0; i < dev->data->nb_rx_queues; i++) {
> +             struct xdp_statistics xdp_stats;
> +             socklen_t optlen = sizeof(xdp_stats);
> +             int fd;
> 
> -             stats->ipackets += stats->q_ipackets[i];
> -             stats->ibytes += stats->q_ibytes[i];
> -             stats->imissed += rxq->stats.rx_dropped;
> -             stats->oerrors += txq->stats.tx_dropped;
>               fd = process_private->rxq_xsk_fds[i];
> -             ret = fd >= 0 ? getsockopt(fd, SOL_XDP, XDP_STATISTICS,
> -                                        &xdp_stats, &optlen) : -1;
> -             if (ret != 0) {
> +             if (fd < 0)
> +                     continue;
> +             if (getsockopt(fd, SOL_XDP, XDP_STATISTICS,
> +                            &xdp_stats, &optlen)  < 0) {
>                       AF_XDP_LOG(ERR, "getsockopt() failed for
> XDP_STATISTICS.\n");
>                       return -1;
>               }
>               stats->imissed += xdp_stats.rx_dropped;
> -
> -             stats->opackets += stats->q_opackets[i];
> -             stats->obytes += stats->q_obytes[i];
>       }
> 
>       return 0;
> @@ -888,17 +849,8 @@ eth_stats_get(struct rte_eth_dev *dev, struct
> rte_eth_stats *stats)
>  static int
>  eth_stats_reset(struct rte_eth_dev *dev)
>  {
> -     struct pmd_internals *internals = dev->data->dev_private;
> -     int i;
> -
> -     for (i = 0; i < internals->queue_cnt; i++) {
> -             memset(&internals->rx_queues[i].stats, 0,
> -                                     sizeof(struct rx_stats));
> -             memset(&internals->tx_queues[i].stats, 0,
> -                                     sizeof(struct tx_stats));
> -     }
> -
> -     return 0;
> +     return rte_eth_counters_reset(dev, offsetof(struct pkt_tx_queue,
> stats),
> +                                   offsetof(struct pkt_rx_queue, stats));
>  }
> 
>  #ifdef RTE_NET_AF_XDP_LIBBPF_XDP_ATTACH
> --
> 2.43.0

Reply via email to