net: add functionality to net core byte/packet counters and use it in r8169
Heiner Kallweit
maintainercover net: add functionality to net core byte/packet counters and use it in r8169
This series adds missing functionality to the net core handling of byte/packet counters and statistics. The extensions are then used to remove private rx/tx byte/packet counters in r8169 driver.
Heiner Kallweit (4):
net: core: add dev_sw_netstats_tx_add
net: core: add devm_netdev_alloc_pcpu_stats
r8169: use struct pcpu_sw_netstats for rx/tx packet/byte counters
r8169: remove no longer needed private rx/tx packet/byte counters
drivers/net/ethernet/realtek/r8169_main.c | 45 ++++------------------- include/linux/netdevice.h | 26 +++++++++++++ 2 files changed, 34 insertions(+), 37 deletions(-)
Signature · 1 line
2.29.1
Heiner Kallweit
maintainerPATCH 1/4 net: core: add dev_sw_netstats_tx_add
Add dev_sw_netstats_tx_add(), complementing already existing dev_sw_netstats_rx_add(). Other than dev_sw_netstats_rx_add allow to pass the number of packets as function argument.
--- include/linux/netdevice.h | 12 ++++++++++++ 1 file changed, 12 insertions(+)
include/linux/netdevice.h +12 -0
@@ -2557,6 +2557,18 @@ static inline void dev_sw_netstats_rx_add(struct net_device *dev, unsigned int lu64_stats_update_end (&tstats->syncp); } +static inline void dev_sw_netstats_tx_add (struct net_device *dev, +unsigned int packets, +unsigned int len) +{ +struct pcpu_sw_netstats *tstats =this_cpu_ptr (dev->tstats); + +u64_stats_update_begin (&tstats->syncp); + tstats->tx_bytes += len; + tstats->tx_packets += packets; +u64_stats_update_end (&tstats->syncp); +} +static inline void dev_lstats_add (struct net_device *dev,unsigned int len) {struct pcpu_lstats *lstats =this_cpu_ptr (dev->lstats);
Signature · 1 line
2.29.1
Heiner Kallweit
maintainerPATCH 2/4 net: core: add devm_netdev_alloc_pcpu_stats
In reply to Heiner KallweitWe have netdev_alloc_pcpu_stats(), and we have devm_alloc_percpu(). Add a managed version of netdev_alloc_pcpu_stats, e.g. for allocating the per-cpu stats in the probe() callback of a driver. It needs to be a macro for dealing properly with the type argument.
--- include/linux/netdevice.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
include/linux/netdevice.h +14 -0
@@ -2596,6 +2596,20 @@ static inline void dev_lstats_add(struct net_device *dev, unsigned int len)#define netdev_alloc_pcpu_stats (type) \ __netdev_alloc_pcpu_stats(type, GFP_KERNEL) +#define devm_netdev_alloc_pcpu_stats (dev, type) \ +({ \ + typeof(type) __percpu *pcpu_stats = devm_alloc_percpu(dev, type);\ + if (pcpu_stats) { \ + int __cpu; \ + for_each_possible_cpu(__cpu) { \ + typeof(type) *stat; \ + stat = per_cpu_ptr(pcpu_stats, __cpu); \ + u64_stats_init(&stat->syncp); \ + } \ + } \ + pcpu_stats; \ +}) +enum netdev_lag_tx_type {NETDEV_LAG_TX_TYPE_UNKNOWN ,NETDEV_LAG_TX_TYPE_RANDOM ,
Signature · 1 line
2.29.1
Heiner Kallweit
maintainerPATCH 3/4 r8169: use struct pcpu_sw_netstats for rx/tx packet/byte counters
In reply to Heiner KallweitSwitch to the net core rx/tx byte/packet counter infrastructure. This simplifies the code, only small drawback is some memory overhead because we use just one queue, but allocate the counters per cpu.
--- drivers/net/ethernet/realtek/r8169_main.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/net/ethernet/realtek/r8169_main.c +8 -3
@@ -4417,6 +4417,7 @@ static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,if (tp->dirty_tx != dirty_tx) {netdev_completed_queue (dev, pkts_compl, bytes_compl); +dev_sw_netstats_tx_add (dev, pkts_compl, bytes_compl);rtl_inc_priv_stats (&tp->tx_stats, pkts_compl, bytes_compl); tp->dirty_tx = dirty_tx;
@@ -4539,6 +4540,7 @@ static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budgetnapi_gro_receive (&tp->napi, skb); +dev_sw_netstats_rx_add (dev, pkt_size);rtl_inc_priv_stats (&tp->rx_stats,1 , pkt_size); release_descriptor:rtl8169_mark_to_asic (desc);
@@ -4790,9 +4792,7 @@ rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)pm_runtime_get_noresume (&pdev->dev);netdev_stats_to_stats64 (stats, &dev->stats); - -rtl_get_priv_stats (&tp->rx_stats, &stats->rx_packets, &stats->rx_bytes); -rtl_get_priv_stats (&tp->tx_stats, &stats->tx_packets, &stats->tx_bytes); +dev_fetch_sw_netstats (stats, dev->tstats);/* * Fetch additional counter values missing in stats collected by driver
@@ -5263,6 +5263,11 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) tp->eee_adv =-1 ; tp->ocp_base =OCP_STD_PHY_BASE ; + dev->tstats =devm_netdev_alloc_pcpu_stats (&pdev->dev, + struct pcpu_sw_netstats); +if (!dev->tstats) +return -ENOMEM ; +/* Get the *optional* external "ether_clk" used on some boards */ rc =rtl_get_ether_clk (tp);if (rc)
Signature · 1 line
2.29.1
Heiner Kallweit
maintainerPATCH 4/4 r8169: remove no longer needed private rx/tx packet/byte counters
In reply to Heiner KallweitAfter switching to the net core rx/tx byte/packet counters we can remove the now unused private version.
--- drivers/net/ethernet/realtek/r8169_main.c | 34 ----------------------- 1 file changed, 34 deletions(-)
drivers/net/ethernet/realtek/r8169_main.c +0 -34
@@ -584,12 +584,6 @@ enum rtl_flag {RTL_FLAG_MAX }; -struct rtl8169_stats { -u64 packets; -u64 bytes; -struct u64_stats_sync syncp; -}; -struct rtl8169_private {void __iomem *mmio_addr;/* memory map physical address */ struct pci_dev *pci_dev;
@@ -600,8 +594,6 @@ struct rtl8169_private {u32 cur_rx;/* Index into the Rx descriptor buffer of next Rx pkt. */ u32 cur_tx;/* Index into the Tx descriptor buffer of next Rx pkt. */ u32 dirty_tx; -struct rtl8169_stats rx_stats; -struct rtl8169_stats tx_stats;struct TxDesc *TxDescArray;/* 256-aligned Tx descriptor ring */ struct RxDesc *RxDescArray;/* 256-aligned Rx descriptor ring */ dma_addr_t TxPhyAddr;
@@ -700,27 +692,6 @@ static bool rtl_supports_eee(struct rtl8169_private *tp) tp->mac_version !=RTL_GIGA_MAC_VER_39 ; } -static void rtl_get_priv_stats (struct rtl8169_stats *stats, -u64 *pkts,u64 *bytes) -{ -unsigned int start; - -do { - start =u64_stats_fetch_begin_irq (&stats->syncp); - *pkts = stats->packets; - *bytes = stats->bytes; - }while (u64_stats_fetch_retry_irq (&stats->syncp, start)); -} - -static void rtl_inc_priv_stats (struct rtl8169_stats *stats, -u64 pkts,u64 bytes) -{ -u64_stats_update_begin (&stats->syncp); - stats->packets += pkts; - stats->bytes += bytes; -u64_stats_update_end (&stats->syncp); -} -static void rtl_read_mac_from_reg (struct rtl8169_private *tp,u8 *mac,int reg) {int i;
@@ -4416,9 +4387,7 @@ static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,if (tp->dirty_tx != dirty_tx) {netdev_completed_queue (dev, pkts_compl, bytes_compl); -dev_sw_netstats_tx_add (dev, pkts_compl, bytes_compl); -rtl_inc_priv_stats (&tp->tx_stats, pkts_compl, bytes_compl); tp->dirty_tx = dirty_tx;/* Sync with rtl8169_start_xmit:
@@ -4541,7 +4510,6 @@ static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budgetnapi_gro_receive (&tp->napi, skb);dev_sw_netstats_rx_add (dev, pkt_size); -rtl_inc_priv_stats (&tp->rx_stats,1 , pkt_size); release_descriptor:rtl8169_mark_to_asic (desc); }
@@ -5345,8 +5313,6 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) }INIT_WORK (&tp->wk.work, rtl_task); -u64_stats_init (&tp->rx_stats.syncp); -u64_stats_init (&tp->tx_stats.syncp);rtl_init_mac_address (tp);
Signature · 1 line
2.29.1
Quoted 3 lines · On Thu, 29 Oct 2020 18:28:52 +0100 Heiner Kallweit wrote:
This series adds missing functionality to the net core handling of byte/packet counters and statistics. The extensions are then used to remove private rx/tx byte/packet counters in r8169 driver.
Applied, thanks!