From: Tang Longjun <tanglong...@kylinos.cn>

virtnet_get_hw_stats will return same value of 0 in the following three cases:
1. VIRTIO_NET_F_DEVICE_STATS feature is not configured on the device side.
2. success to obtain device stats.
3. fail to obtain device stats.
This would lead to case 1 and case 3 being unable to be recognized.

So, it's fixed by returning different values for the three cases.

Fixes: 941168f8b40e ("virtio_net: support device stats")
Signed-off-by: Tang Longjun <tanglong...@kylinos.cn>
---
 drivers/net/virtio_net.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index e53ba600605a..c9a86f325619 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -4897,7 +4897,7 @@ static int __virtnet_get_hw_stats(struct virtnet_info *vi,
                                        &sgs_out, &sgs_in);
 
        if (!ok)
-               return ok;
+               return 1;
 
        for (p = reply; p - reply < res_size; p += le16_to_cpu(hdr->size)) {
                hdr = p;
@@ -4937,7 +4937,7 @@ static int virtnet_get_hw_stats(struct virtnet_info *vi,
        int ok;
 
        if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_DEVICE_STATS))
-               return 0;
+               return -EOPNOTSUPP;
 
        if (qid == -1) {
                last_vq = vi->curr_queue_pairs * 2 - 1;
@@ -5038,10 +5038,12 @@ static void virtnet_get_ethtool_stats(struct net_device 
*dev,
        struct virtnet_stats_ctx ctx = {0};
        unsigned int start, i;
        const u8 *stats_base;
+       int ret;
 
        virtnet_stats_ctx_init(vi, &ctx, data, false);
-       if (virtnet_get_hw_stats(vi, &ctx, -1))
-               dev_warn(&vi->dev->dev, "Failed to get hw stats.\n");
+       ret = virtnet_get_hw_stats(vi, &ctx, -1);
+       if (ret)
+               dev_warn(&vi->dev->dev, "Failed to get hw stats. err 
code:%d\n", ret);
 
        for (i = 0; i < vi->curr_queue_pairs; i++) {
                struct receive_queue *rq = &vi->rq[i];
-- 
2.43.0


Reply via email to