OSDN Git Service

net: hns3: split function hns3_nic_get_stats64()
authorYufeng Mo <moyufeng@huawei.com>
Mon, 29 Nov 2021 14:00:25 +0000 (22:00 +0800)
committerDavid S. Miller <davem@davemloft.net>
Mon, 29 Nov 2021 14:26:17 +0000 (14:26 +0000)
Function hns3_nic_get_stats64() is a bit too long. So add a
new function hns3_fetch_stats() to simplify code and improve
code readability.

Signed-off-by: Yufeng Mo <moyufeng@huawei.com>
Signed-off-by: Guangbin Huang <huangguangbin2@huawei.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/ethernet/hisilicon/hns3/hns3_enet.c

index cc962e5..fe1f5ea 100644 (file)
@@ -2383,90 +2383,89 @@ static netdev_features_t hns3_features_check(struct sk_buff *skb,
        return features;
 }
 
+static void hns3_fetch_stats(struct rtnl_link_stats64 *stats,
+                            struct hns3_enet_ring *ring, bool is_tx)
+{
+       unsigned int start;
+
+       do {
+               start = u64_stats_fetch_begin_irq(&ring->syncp);
+               if (is_tx) {
+                       stats->tx_bytes += ring->stats.tx_bytes;
+                       stats->tx_packets += ring->stats.tx_pkts;
+                       stats->tx_dropped += ring->stats.sw_err_cnt;
+                       stats->tx_dropped += ring->stats.tx_vlan_err;
+                       stats->tx_dropped += ring->stats.tx_l4_proto_err;
+                       stats->tx_dropped += ring->stats.tx_l2l3l4_err;
+                       stats->tx_dropped += ring->stats.tx_tso_err;
+                       stats->tx_dropped += ring->stats.over_max_recursion;
+                       stats->tx_dropped += ring->stats.hw_limitation;
+                       stats->tx_dropped += ring->stats.copy_bits_err;
+                       stats->tx_dropped += ring->stats.skb2sgl_err;
+                       stats->tx_dropped += ring->stats.map_sg_err;
+                       stats->tx_errors += ring->stats.sw_err_cnt;
+                       stats->tx_errors += ring->stats.tx_vlan_err;
+                       stats->tx_errors += ring->stats.tx_l4_proto_err;
+                       stats->tx_errors += ring->stats.tx_l2l3l4_err;
+                       stats->tx_errors += ring->stats.tx_tso_err;
+                       stats->tx_errors += ring->stats.over_max_recursion;
+                       stats->tx_errors += ring->stats.hw_limitation;
+                       stats->tx_errors += ring->stats.copy_bits_err;
+                       stats->tx_errors += ring->stats.skb2sgl_err;
+                       stats->tx_errors += ring->stats.map_sg_err;
+               } else {
+                       stats->rx_bytes += ring->stats.rx_bytes;
+                       stats->rx_packets += ring->stats.rx_pkts;
+                       stats->rx_dropped += ring->stats.l2_err;
+                       stats->rx_errors += ring->stats.l2_err;
+                       stats->rx_errors += ring->stats.l3l4_csum_err;
+                       stats->rx_crc_errors += ring->stats.l2_err;
+                       stats->multicast += ring->stats.rx_multicast;
+                       stats->rx_length_errors += ring->stats.err_pkt_len;
+               }
+       } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
+}
+
 static void hns3_nic_get_stats64(struct net_device *netdev,
                                 struct rtnl_link_stats64 *stats)
 {
        struct hns3_nic_priv *priv = netdev_priv(netdev);
        int queue_num = priv->ae_handle->kinfo.num_tqps;
        struct hnae3_handle *handle = priv->ae_handle;
+       struct rtnl_link_stats64 ring_total_stats;
        struct hns3_enet_ring *ring;
-       u64 rx_length_errors = 0;
-       u64 rx_crc_errors = 0;
-       u64 rx_multicast = 0;
-       unsigned int start;
-       u64 tx_errors = 0;
-       u64 rx_errors = 0;
        unsigned int idx;
-       u64 tx_bytes = 0;
-       u64 rx_bytes = 0;
-       u64 tx_pkts = 0;
-       u64 rx_pkts = 0;
-       u64 tx_drop = 0;
-       u64 rx_drop = 0;
 
        if (test_bit(HNS3_NIC_STATE_DOWN, &priv->state))
                return;
 
        handle->ae_algo->ops->update_stats(handle, &netdev->stats);
 
+       memset(&ring_total_stats, 0, sizeof(ring_total_stats));
        for (idx = 0; idx < queue_num; idx++) {
                /* fetch the tx stats */
                ring = &priv->ring[idx];
-               do {
-                       start = u64_stats_fetch_begin_irq(&ring->syncp);
-                       tx_bytes += ring->stats.tx_bytes;
-                       tx_pkts += ring->stats.tx_pkts;
-                       tx_drop += ring->stats.sw_err_cnt;
-                       tx_drop += ring->stats.tx_vlan_err;
-                       tx_drop += ring->stats.tx_l4_proto_err;
-                       tx_drop += ring->stats.tx_l2l3l4_err;
-                       tx_drop += ring->stats.tx_tso_err;
-                       tx_drop += ring->stats.over_max_recursion;
-                       tx_drop += ring->stats.hw_limitation;
-                       tx_drop += ring->stats.copy_bits_err;
-                       tx_drop += ring->stats.skb2sgl_err;
-                       tx_drop += ring->stats.map_sg_err;
-                       tx_errors += ring->stats.sw_err_cnt;
-                       tx_errors += ring->stats.tx_vlan_err;
-                       tx_errors += ring->stats.tx_l4_proto_err;
-                       tx_errors += ring->stats.tx_l2l3l4_err;
-                       tx_errors += ring->stats.tx_tso_err;
-                       tx_errors += ring->stats.over_max_recursion;
-                       tx_errors += ring->stats.hw_limitation;
-                       tx_errors += ring->stats.copy_bits_err;
-                       tx_errors += ring->stats.skb2sgl_err;
-                       tx_errors += ring->stats.map_sg_err;
-               } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
+               hns3_fetch_stats(&ring_total_stats, ring, true);
 
                /* fetch the rx stats */
                ring = &priv->ring[idx + queue_num];
-               do {
-                       start = u64_stats_fetch_begin_irq(&ring->syncp);
-                       rx_bytes += ring->stats.rx_bytes;
-                       rx_pkts += ring->stats.rx_pkts;
-                       rx_drop += ring->stats.l2_err;
-                       rx_errors += ring->stats.l2_err;
-                       rx_errors += ring->stats.l3l4_csum_err;
-                       rx_crc_errors += ring->stats.l2_err;
-                       rx_multicast += ring->stats.rx_multicast;
-                       rx_length_errors += ring->stats.err_pkt_len;
-               } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
-       }
-
-       stats->tx_bytes = tx_bytes;
-       stats->tx_packets = tx_pkts;
-       stats->rx_bytes = rx_bytes;
-       stats->rx_packets = rx_pkts;
-
-       stats->rx_errors = rx_errors;
-       stats->multicast = rx_multicast;
-       stats->rx_length_errors = rx_length_errors;
-       stats->rx_crc_errors = rx_crc_errors;
+               hns3_fetch_stats(&ring_total_stats, ring, false);
+       }
+
+       stats->tx_bytes = ring_total_stats.tx_bytes;
+       stats->tx_packets = ring_total_stats.tx_packets;
+       stats->rx_bytes = ring_total_stats.rx_bytes;
+       stats->rx_packets = ring_total_stats.rx_packets;
+
+       stats->rx_errors = ring_total_stats.rx_errors;
+       stats->multicast = ring_total_stats.multicast;
+       stats->rx_length_errors = ring_total_stats.rx_length_errors;
+       stats->rx_crc_errors = ring_total_stats.rx_crc_errors;
        stats->rx_missed_errors = netdev->stats.rx_missed_errors;
 
-       stats->tx_errors = tx_errors;
-       stats->rx_dropped = rx_drop;
-       stats->tx_dropped = tx_drop;
+       stats->tx_errors = ring_total_stats.tx_errors;
+       stats->rx_dropped = ring_total_stats.rx_dropped;
+       stats->tx_dropped = ring_total_stats.tx_dropped;
        stats->collisions = netdev->stats.collisions;
        stats->rx_over_errors = netdev->stats.rx_over_errors;
        stats->rx_frame_errors = netdev->stats.rx_frame_errors;