Add hardware packets/bytes statisticsi query and reset.

Signed-off-by: Mingxia Liu <mingxia....@intel.com>
Signed-off-by: Junfeng Guo <junfeng....@intel.com>
---
 drivers/net/idpf/idpf_ethdev.c | 82 +++++++++++++++++++++++++++++++++-
 drivers/net/idpf/idpf_ethdev.h |  2 +
 drivers/net/idpf/idpf_vchnl.c  | 26 +++++++++++
 3 files changed, 109 insertions(+), 1 deletion(-)

diff --git a/drivers/net/idpf/idpf_ethdev.c b/drivers/net/idpf/idpf_ethdev.c
index 4b8e3c4d5b..b934488d0b 100644
--- a/drivers/net/idpf/idpf_ethdev.c
+++ b/drivers/net/idpf/idpf_ethdev.c
@@ -35,6 +35,9 @@ static int idpf_dev_close(struct rte_eth_dev *dev);
 static int idpf_dev_info_get(struct rte_eth_dev *dev,
                             struct rte_eth_dev_info *dev_info);
 static int idpf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
+static int idpf_dev_stats_get(struct rte_eth_dev *dev,
+                       struct rte_eth_stats *stats);
+static int idpf_dev_stats_reset(struct rte_eth_dev *dev);
 
 int
 idpf_dev_link_update(struct rte_eth_dev *dev,
@@ -74,6 +77,8 @@ static const struct eth_dev_ops idpf_eth_dev_ops = {
        .dev_infos_get                  = idpf_dev_info_get,
        .link_update                    = idpf_dev_link_update,
        .mtu_set                        = idpf_dev_mtu_set,
+       .stats_get                              = idpf_dev_stats_get,
+       .stats_reset                    = idpf_dev_stats_reset,
 };
 
 static int
@@ -157,8 +162,79 @@ idpf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu 
__rte_unused)
        return 0;
 }
 
+static void
+idpf_stat_update(uint64_t *offset, uint64_t *stat)
+{
+       *stat = *stat - *offset;
+}
+
+
+static void
+idpf_update_stats(struct virtchnl2_vport_stats *oes, struct 
virtchnl2_vport_stats *nes)
+{
+       idpf_stat_update(&oes->rx_bytes, &nes->rx_bytes);
+       idpf_stat_update(&oes->rx_unicast, &nes->rx_unicast);
+       idpf_stat_update(&oes->rx_multicast, &nes->rx_multicast);
+       idpf_stat_update(&oes->rx_broadcast, &nes->rx_broadcast);
+       idpf_stat_update(&oes->rx_discards, &nes->rx_discards);
+       idpf_stat_update(&oes->tx_bytes, &nes->tx_bytes);
+       idpf_stat_update(&oes->tx_unicast, &nes->tx_unicast);
+       idpf_stat_update(&oes->tx_multicast, &nes->tx_multicast);
+       idpf_stat_update(&oes->tx_broadcast, &nes->tx_broadcast);
+       idpf_stat_update(&oes->tx_errors, &nes->tx_errors);
+       idpf_stat_update(&oes->tx_discards, &nes->tx_discards);
+}
+
 static int
-idpf_init_vport_req_info(__rte_unused struct rte_eth_dev *dev)
+idpf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
+{
+       struct idpf_vport *vport =
+               (struct idpf_vport *)dev->data->dev_private;
+       struct virtchnl2_vport_stats *pstats = NULL;
+       int ret;
+
+       ret = idpf_query_stats(vport, &pstats);
+       if (ret == 0) {
+               uint8_t crc_stats_len = (dev->data->dev_conf.rxmode.offloads &
+                                        RTE_ETH_RX_OFFLOAD_KEEP_CRC) ? 0 :
+                                        RTE_ETHER_CRC_LEN;
+               idpf_update_stats(&vport->eth_stats_offset, pstats);
+               stats->ipackets = pstats->rx_unicast + pstats->rx_multicast +
+                               pstats->rx_broadcast - pstats->rx_discards;
+               stats->opackets = pstats->tx_broadcast + pstats->tx_multicast +
+                                               pstats->tx_unicast;
+               stats->imissed = pstats->rx_discards;
+               stats->oerrors = pstats->tx_errors + pstats->tx_discards;
+               stats->ibytes = pstats->rx_bytes;
+               stats->ibytes -= stats->ipackets * crc_stats_len;
+               stats->obytes = pstats->tx_bytes;
+       } else {
+               PMD_DRV_LOG(ERR, "Get statistics failed");
+       }
+       return ret;
+}
+
+
+static int
+idpf_dev_stats_reset(struct rte_eth_dev *dev)
+{
+       struct idpf_vport *vport =
+               (struct idpf_vport *)dev->data->dev_private;
+       struct virtchnl2_vport_stats *pstats = NULL;
+       int ret;
+
+       ret = idpf_query_stats(vport, &pstats);
+       if (ret != 0)
+               return ret;
+
+       /* set stats offset base on current values */
+       vport->eth_stats_offset = *pstats;
+
+       return 0;
+}
+
+static int
+idpf_init_vport_req_info(struct rte_eth_dev *dev)
 {
        struct virtchnl2_create_vport *vport_info;
        uint16_t idx = adapter->next_vport_idx;
@@ -499,6 +575,10 @@ idpf_dev_start(struct rte_eth_dev *dev)
                goto err_vport;
        }
 
+       if (idpf_dev_stats_reset(dev)) {
+               PMD_DRV_LOG(ERR, "Failed to reset stats");
+               goto err_vport;
+       }
        return 0;
 
 err_vport:
diff --git a/drivers/net/idpf/idpf_ethdev.h b/drivers/net/idpf/idpf_ethdev.h
index 22096c4185..c0cbf4c3c6 100644
--- a/drivers/net/idpf/idpf_ethdev.h
+++ b/drivers/net/idpf/idpf_ethdev.h
@@ -227,6 +227,8 @@ int idpf_switch_queue(struct idpf_vport *vport, uint16_t 
qid,
                      bool rx, bool on);
 int idpf_ena_dis_queues(struct idpf_vport *vport, bool enable);
 int idpf_ena_dis_vport(struct idpf_vport *vport, bool enable);
+int idpf_query_stats(struct idpf_vport *vport,
+                       struct virtchnl2_vport_stats **pstats);
 
 #endif /* _IDPF_ETHDEV_H_ */
 
diff --git a/drivers/net/idpf/idpf_vchnl.c b/drivers/net/idpf/idpf_vchnl.c
index 07b220016f..563f8f649e 100644
--- a/drivers/net/idpf/idpf_vchnl.c
+++ b/drivers/net/idpf/idpf_vchnl.c
@@ -219,6 +219,7 @@ idpf_execute_vc_cmd(struct idpf_adapter *adapter, struct 
idpf_cmd_info *args)
        case VIRTCHNL2_OP_DISABLE_QUEUES:
        case VIRTCHNL2_OP_ENABLE_VPORT:
        case VIRTCHNL2_OP_DISABLE_VPORT:
+       case VIRTCHNL2_OP_GET_STATS:
                /* for init virtchnl ops, need to poll the response */
                do {
                        result = idpf_read_msg_from_ipf(adapter,
@@ -1070,3 +1071,28 @@ idpf_ena_dis_vport(struct idpf_vport *vport, bool enable)
        return err;
 }
 
+int
+idpf_query_stats(struct idpf_vport *vport,
+               struct virtchnl2_vport_stats **pstats)
+{
+       struct virtchnl2_vport vc_vport;
+       struct idpf_cmd_info args;
+       int err;
+
+       vc_vport.vport_id = vport->vport_id;
+       args.ops = VIRTCHNL2_OP_GET_STATS;
+       args.in_args = (u8 *)&vc_vport;
+       args.in_args_size = sizeof(vc_vport);
+       args.out_buffer = adapter->mbx_resp;
+       args.out_size = IDPF_DFLT_MBX_BUF_SIZE;
+
+       err = idpf_execute_vc_cmd(adapter, &args);
+       if (err) {
+               PMD_DRV_LOG(ERR, "Failed to execute command of 
VIRTCHNL2_OP_GET_STATS");
+               *pstats = NULL;
+               return err;
+       }
+       *pstats = (struct virtchnl2_vport_stats *)args.out_buffer;
+       return 0;
+}
+
-- 
2.25.1

Reply via email to