Skip to contentSkip to conversation

net: xilinx: axienet: Add statistics support

PATCH Superseded Sean Anderson 3 participants 16 messages Jun 10, 2024 – Jun 18, 2024

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover net: xilinx: axienet: Add statistics support

Add support for hardware statistics counters (if they are enabled) in the AXI Ethernet driver. Unfortunately, the implementation is complicated a bit since the hardware might only support 32-bit counters. In the future, we could add a simpler alternate implementation for 64-bit counters enabled with a device-tree property, but for now I've gone with a unified implementation.

Sean Anderson (3):
net: xilinx: axienet: Use NL_SET_ERR_MSG instead of netdev_err
net: xilinx: axienet: Report RxRject as rx_dropped
net: xilinx: axienet: Add statistics support

 drivers/net/ethernet/xilinx/xilinx_axienet.h  |  81 ++++++
 .../net/ethernet/xilinx/xilinx_axienet_main.c | 275 +++++++++++++++++-
 2 files changed, 349 insertions(+), 7 deletions(-)
Signature · 1 line
2.35.1.1320.gc452695387.dirty
#1 lore raw

PATCH 1/3 net: xilinx: axienet: Use NL_SET_ERR_MSG instead of netdev_err

This error message can be triggered by userspace. Use NL_SET_ERR_MSG so the message is returned to the user and to avoid polluting the kernel logs.

---

 drivers/net/ethernet/xilinx/xilinx_axienet_main.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +3 -3
index c29809cd9201..5f98daa5b341 100644
@@ -1945,9 +1945,9 @@ axienet_ethtools_set_coalesce(struct net_device *ndev,
 	struct axienet_local *lp = netdev_priv(ndev);
 
 	if (netif_running(ndev)) {
-		netdev_err(ndev,
-			   "Please stop netif before applying configuration\n");
-		return -EFAULT;
+		NL_SET_ERR_MSG(extack,
+			       "Please stop netif before applying configuration");
+		return -EBUSY;
 	}
 
 	if (ecoalesce->rx_max_coalesced_frames)
Signature · 1 line
2.35.1.1320.gc452695387.dirty
#4 lore raw

PATCH 2/3 net: xilinx: axienet: Report RxRject as rx_dropped

In reply to Sean Anderson

The Receive Frame Rejected interrupt is asserted whenever there was a receive error (bad FCS, bad length, etc.) or whenever the frame was dropped due to a mismatched address. So this is really a combination of rx_otherhost_dropped, rx_length_errors, rx_frame_errors, and rx_crc_errors. Mismatched addresses are common and aren't really errors at all (much like how fragments are normal on half-duplex links). To avoid confusion, report these events as rx_dropped. This better reflects what's going on: the packet was received by the MAC but dropped before being processed.

---

 drivers/net/ethernet/xilinx/xilinx_axienet_main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +1 -1
index 5f98daa5b341..cf8908794409 100644
@@ -1296,7 +1296,7 @@ static irqreturn_t axienet_eth_irq(int irq, void *_ndev)
 		ndev->stats.rx_missed_errors++;
 
 	if (pending & XAE_INT_RXRJECT_MASK)
-		ndev->stats.rx_frame_errors++;
+		ndev->stats.rx_dropped++;
 
 	axienet_iow(lp, XAE_IS_OFFSET, pending);
 	return IRQ_HANDLED;
Signature · 1 line
2.35.1.1320.gc452695387.dirty
#5 lore raw

PATCH 3/3 net: xilinx: axienet: Add statistics support

In reply to Sean Anderson

Add support for reading the statistics counters, if they are enabled. The counters may be 64-bit, but we can't detect this as there's no ability bit for it and the counters are read-only. Therefore, we assume the counters are 32-bits. To ensure we don't miss an overflow, we need to read all counters at regular intervals, configurable with stats-block-usecs. This should be often enough to ensure the bytes counters don't wrap at 2.5 Gbit/s.

Another complication is that the counters may be reset when the device is reset (depending on configuration). To ensure the counters persist across link up/down (including suspend/resume), we maintain our own 64-bit versions along with the last counter value we saw. Because we might wait up to 100 ms for the reset to complete, we use a mutex to protect writing hw_stats. We can't sleep in ndo_get_stats64, so we use a u64_stats_sync to protect readers.

We can't use the byte counters for either get_stats64 or get_eth_mac_stats. This is because the byte counters include everything in the frame (destination address to FCS, inclusive). But rtnl_link_stats64 wants bytes excluding the FCS, and ethtool_eth_mac_stats wants to exclude the L2 overhead (addresses and length/type).

---

 drivers/net/ethernet/xilinx/xilinx_axienet.h  |  81 ++++++
 .../net/ethernet/xilinx/xilinx_axienet_main.c | 267 +++++++++++++++++-
 2 files changed, 345 insertions(+), 3 deletions(-)
drivers/net/ethernet/xilinx/xilinx_axienet.h +81 -0
index fa5500decc96..c121e9b1c41d 100644
@@ -156,6 +156,7 @@
 #define XAE_TPID0_OFFSET	0x00000028 /* VLAN TPID0 register */
 #define XAE_TPID1_OFFSET	0x0000002C /* VLAN TPID1 register */
 #define XAE_PPST_OFFSET		0x00000030 /* PCS PMA Soft Temac Status Reg */
+#define XAE_STATS_OFFSET	0x00000200 /* Statistics counters */
 #define XAE_RCW0_OFFSET		0x00000400 /* Rx Configuration Word 0 */
 #define XAE_RCW1_OFFSET		0x00000404 /* Rx Configuration Word 1 */
 #define XAE_TC_OFFSET		0x00000408 /* Tx Configuration */
@@ -163,6 +164,7 @@
 #define XAE_EMMC_OFFSET		0x00000410 /* EMAC mode configuration */
 #define XAE_PHYC_OFFSET		0x00000414 /* RGMII/SGMII configuration */
 #define XAE_ID_OFFSET		0x000004F8 /* Identification register */
+#define XAE_ABILITY_OFFSET	0x000004FC /* Ability Register offset */
 #define XAE_MDIO_MC_OFFSET	0x00000500 /* MII Management Config */
 #define XAE_MDIO_MCR_OFFSET	0x00000504 /* MII Management Control */
 #define XAE_MDIO_MWD_OFFSET	0x00000508 /* MII Management Write Data */
@@ -283,6 +285,16 @@
 #define XAE_PHYC_SGLINKSPD_100		0x40000000 /* SGMII link 100 Mbit */
 #define XAE_PHYC_SGLINKSPD_1000		0x80000000 /* SGMII link 1000 Mbit */
 
+/* Bit masks for Axi Ethernet ability register */
+#define XAE_ABILITY_PFC			BIT(16)
+#define XAE_ABILITY_FRAME_FILTER	BIT(10)
+#define XAE_ABILITY_HALF_DUPLEX		BIT(9)
+#define XAE_ABILITY_STATS		BIT(8)
+#define XAE_ABILITY_2_5G		BIT(3)
+#define XAE_ABILITY_1G			BIT(2)
+#define XAE_ABILITY_100M		BIT(1)
+#define XAE_ABILITY_10M			BIT(0)
+
 /* Bit masks for Axi Ethernet MDIO interface MC register */
 #define XAE_MDIO_MC_MDIOEN_MASK		0x00000040 /* MII management enable */
 #define XAE_MDIO_MC_CLOCK_DIVIDE_MAX	0x3F	   /* Maximum MDIO divisor */
@@ -331,6 +343,7 @@
 #define XAE_FEATURE_FULL_RX_CSUM	(1 << 2)
 #define XAE_FEATURE_FULL_TX_CSUM	(1 << 3)
 #define XAE_FEATURE_DMA_64BIT		(1 << 4)
+#define XAE_FEATURE_STATS		(1 << 5)
 
 #define XAE_NO_CSUM_OFFLOAD		0
 
@@ -344,6 +357,61 @@
 #define XLNX_MII_STD_SELECT_REG		0x11
 #define XLNX_MII_STD_SELECT_SGMII	BIT(0)
 
+/* enum temac_stat - TEMAC statistics counters
+ *
+ * Index of statistics counters within the TEMAC. This must match the
+ * order/offset of hardware registers exactly.
+ */
+enum temac_stat {
+	STAT_RX_BYTES = 0,
+	STAT_TX_BYTES,
+	STAT_UNDERSIZE_FRAMES,
+	STAT_FRAGMENT_FRAMES,
+	STAT_RX_64_BYTE_FRAMES,
+	STAT_RX_65_127_BYTE_FRAMES,
+	STAT_RX_128_255_BYTE_FRAMES,
+	STAT_RX_256_511_BYTE_FRAMES,
+	STAT_RX_512_1023_BYTE_FRAMES,
+	STAT_RX_1024_MAX_BYTE_FRAMES,
+	STAT_RX_OVERSIZE_FRAMES,
+	STAT_TX_64_BYTE_FRAMES,
+	STAT_TX_65_127_BYTE_FRAMES,
+	STAT_TX_128_255_BYTE_FRAMES,
+	STAT_TX_256_511_BYTE_FRAMES,
+	STAT_TX_512_1023_BYTE_FRAMES,
+	STAT_TX_1024_MAX_BYTE_FRAMES,
+	STAT_TX_OVERSIZE_FRAMES,
+	STAT_RX_GOOD_FRAMES,
+	STAT_RX_FCS_ERRORS,
+	STAT_RX_BROADCAST_FRAMES,
+	STAT_RX_MULTICAST_FRAMES,
+	STAT_RX_CONTROL_FRAMES,
+	STAT_RX_LENGTH_ERRORS,
+	STAT_RX_VLAN_FRAMES,
+	STAT_RX_PAUSE_FRAMES,
+	STAT_RX_CONTROL_OPCODE_ERRORS,
+	STAT_TX_GOOD_FRAMES,
+	STAT_TX_BROADCAST_FRAMES,
+	STAT_TX_MULTICAST_FRAMES,
+	STAT_TX_UNDERRUN_ERRORS,
+	STAT_TX_CONTROL_FRAMES,
+	STAT_TX_VLAN_FRAMES,
+	STAT_TX_PAUSE_FRAMES,
+	STAT_TX_SINGLE_COLLISION_FRAMES,
+	STAT_TX_MULTIPLE_COLLISION_FRAMES,
+	STAT_TX_DEFERRED_FRAMES,
+	STAT_TX_LATE_COLLISIONS,
+	STAT_TX_EXCESS_COLLISIONS,
+	STAT_TX_EXCESS_DEFERRAL,
+	STAT_RX_ALIGNMENT_ERRORS,
+	STAT_TX_PFC_FRAMES,
+	STAT_RX_PFC_FRAMES,
+	STAT_USER_DEFINED0,
+	STAT_USER_DEFINED1,
+	STAT_USER_DEFINED2,
+	STAT_COUNT,
+};
+
 /**
  * struct axidma_bd - Axi Dma buffer descriptor layout
  * @next:         MM2S/S2MM Next Descriptor Pointer
@@ -434,6 +502,11 @@ struct skbuf_dma_descriptor {
  * @tx_packets: TX packet count for statistics
  * @tx_bytes:	TX byte count for statistics
  * @tx_stat_sync: Synchronization object for TX stats
+ * @hw_last_counter: Last-seen value of each statistic
+ * @hw_stats: Interface statistics periodically updated from hardware counters
+ * @hw_stats_sync: Synchronization object for @hw_stats
+ * @stats_lock: Lock for writing @hw_stats and @hw_last_counter
+ * @stats_work: Work for reading the hardware statistics counters
  * @dma_err_task: Work structure to process Axi DMA errors
  * @tx_irq:	Axidma TX IRQ number
  * @rx_irq:	Axidma RX IRQ number
@@ -452,6 +525,7 @@ struct skbuf_dma_descriptor {
  * @coalesce_usec_rx:	IRQ coalesce delay for RX
  * @coalesce_count_tx:	Store the irq coalesce on TX side.
  * @coalesce_usec_tx:	IRQ coalesce delay for TX
+ * @coalesce_usec_stats: Delay between hardware statistics refreshes
  * @use_dmaengine: flag to check dmaengine framework usage.
  * @tx_chan:	TX DMA channel.
  * @rx_chan:	RX DMA channel.
@@ -505,6 +579,12 @@ struct axienet_local {
 	u64_stats_t tx_bytes;
 	struct u64_stats_sync tx_stat_sync;
 
+	u32 hw_last_counter[STAT_COUNT];
+	u64_stats_t hw_stats[STAT_COUNT];
+	struct u64_stats_sync hw_stat_sync;
+	struct mutex stats_lock;
+	struct delayed_work stats_work;
+
 	struct work_struct dma_err_task;
 
 	int tx_irq;
@@ -525,6 +605,7 @@ struct axienet_local {
 	u32 coalesce_usec_rx;
 	u32 coalesce_count_tx;
 	u32 coalesce_usec_tx;
+	u32 coalesce_usec_stats;
 	u8  use_dmaengine;
 	struct dma_chan *tx_chan;
 	struct dma_chan *rx_chan;
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +264 -3

361 lines not on this page · raw message

Signature · 1 line
2.35.1.1320.gc452695387.dirty
#2 lore raw

Re: [PATCH net-next 1/3] net: xilinx: axienet: Use NL_SET_ERR_MSG instead of netdev_err

In reply to Sean Anderson
Quoted 3 lines · Sean Anderson

This error message can be triggered by userspace. Use NL_SET_ERR_MSG so the message is returned to the user and to avoid polluting the kernel logs.

This has nothing to do with statistics. So it would be better to post it as a standalone patch. It is the sort of trivial patch that should get merged quickly.

I would also comment about the change from EFAULT to EBUSY in the commit message.

Andrew

---
pw-bot: cr

#6 lore raw
In reply to Sean Anderson
Quoted 5 lines · Sean Anderson

Add support for reading the statistics counters, if they are enabled. The counters may be 64-bit, but we can't detect this as there's no ability bit for it and the counters are read-only. Therefore, we assume the counters are 32-bits.

drivers/net/ethernet/xilinx/xilinx_axienet_main.c static void axienet_setoptions(struct net_device *ndev, u32 options) · line 534

Quoted 9 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +9 -0
+static void axienet_stats_update(struct axienet_local *lp)
+{
+	enum temac_stat stat;
+
+	lockdep_assert_held(&lp->stats_lock);
+
+	u64_stats_update_begin(&lp->hw_stat_sync);
+	for (stat = 0; stat < STAT_COUNT; stat++) {
+		u32 counter = axienet_ior(lp, XAE_STATS_OFFSET + stat * 8);

The * 8 here suggests the counters are spaced so that they could be 64 bit wide, even when only 32 bits are used. Does the documentation say anything about the upper 32 bits when the counters are only 32 bits? Are they guaranteed to read as zero? I'm just wondering if the code should be forward looking and read all 64 bits?

drivers/net/ethernet/xilinx/xilinx_axienet_main.c static void axienet_setoptions(struct net_device *ndev, u32 options) · line 566

Quoted 10 lines

static int __axienet_device_reset(struct axienet_local *lp) {

drivers/net/ethernet/xilinx/xilinx_axienet_main.c +5 -0
 	u32 value;
 	int ret;
 
+	/* Save statistics counters in case they will be reset */
+	if (lp->features & XAE_FEATURE_STATS) {
+		mutex_lock(&lp->stats_lock);
+		axienet_stats_update(lp);
+	}

It is a pretty unusual pattern to split a mutex lock/unlock like this on an if statement. Maybe just unconditionally hold the mutex? This does not appear to be anyway hot path, so the overhead should not matter.

Andrew

#10 lore raw
In reply to Sean Anderson
Quoted 6 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +4 -0
+static u64 axienet_stat(struct axienet_local *lp, enum temac_stat stat)
+{
+	return u64_stats_read(&lp->hw_stats[stat]);
+}
@@ -1695,6 +1760,35 @@ axienet_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
 		stats->tx_packets = u64_stats_read(&lp->tx_packets);

drivers/net/ethernet/xilinx/xilinx_axienet_main.c axienet_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) · line 1770

Quoted 10 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +8 -0
 		stats->tx_bytes = u64_stats_read(&lp->tx_bytes);
 	} while (u64_stats_fetch_retry(&lp->tx_stat_sync, start));
+
+	if (!(lp->features & XAE_FEATURE_STATS))
+		return;
+
+	do {
+		start = u64_stats_fetch_begin(&lp->hw_stat_sync);
+		stats->rx_length_errors =
+			axienet_stat(lp, STAT_RX_LENGTH_ERRORS);

I'm i reading this correctly. You are returning the counters from the last refresh period. What is that? 2.5Gbps would wrapper around a 32 byte counter in 13 seconds. I hope these statistics are not 13 seconds out of date?

Since axienet_stats_update() also uses the lp->hw_stat_sync, i don't see why you cannot read the hardware counter value and update to the latest value.

Andrew

#7 lore raw
In reply to Andrew Lunn
Quoted 6 lines · Andrew Lunn
Quoted 4 lines · Sean Anderson

Add support for reading the statistics counters, if they are enabled. The counters may be 64-bit, but we can't detect this as there's no ability bit for it and the counters are read-only. Therefore, we assume the counters are 32-bits.

Quoted 15 lines

drivers/net/ethernet/xilinx/xilinx_axienet_main.c static void axienet_setoptions(struct net_device *ndev, u32 options) · line 534

Quoted 9 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +9 -0
+static void axienet_stats_update(struct axienet_local *lp)
+{
+	enum temac_stat stat;
+
+	lockdep_assert_held(&lp->stats_lock);
+
+	u64_stats_update_begin(&lp->hw_stat_sync);
+	for (stat = 0; stat < STAT_COUNT; stat++) {
+		u32 counter = axienet_ior(lp, XAE_STATS_OFFSET + stat * 8);

The * 8 here suggests the counters are spaced so that they could be 64 bit wide, even when only 32 bits are used. Does the documentation say anything about the upper 32 bits when the counters are only 32 bits? Are they guaranteed to read as zero? I'm just wondering if the code should be forward looking and read all 64 bits?

Actually, if you read the upper 32 bits and they are not 0, you know you have 64 bit counters. You can then kill off your period task, it is not needed because your software counters will wrap around the same time as the hardware counters.

Andrew

#3 lore raw

Re: [PATCH net-next 1/3] net: xilinx: axienet: Use NL_SET_ERR_MSG instead of netdev_err

In reply to Andrew Lunn
Quoted 8 lines · Andrew Lunn
Quoted 3 lines · Sean Anderson

This error message can be triggered by userspace. Use NL_SET_ERR_MSG so the message is returned to the user and to avoid polluting the kernel logs.

This has nothing to do with statistics. So it would be better to post it as a standalone patch. It is the sort of trivial patch that should get merged quickly.

I included it in the series since patch 3 touches lines near it. But upon reviewing that patch, it seems that these lines are not in that patch's context. So I will resumbit this separately.

Quoted 2 lines

I would also comment about the change from EFAULT to EBUSY in the commit message.

OK.

--Sean

#9 lore raw
In reply to Andrew Lunn
Quoted 6 lines · Andrew Lunn
Quoted 4 lines · Sean Anderson

Add support for reading the statistics counters, if they are enabled. The counters may be 64-bit, but we can't detect this as there's no ability bit for it and the counters are read-only. Therefore, we assume the counters are 32-bits.

Quoted 12 lines

drivers/net/ethernet/xilinx/xilinx_axienet_main.c static void axienet_setoptions(struct net_device *ndev, u32 options) · line 534

Quoted 9 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +9 -0
+static void axienet_stats_update(struct axienet_local *lp)
+{
+	enum temac_stat stat;
+
+	lockdep_assert_held(&lp->stats_lock);
+
+	u64_stats_update_begin(&lp->hw_stat_sync);
+	for (stat = 0; stat < STAT_COUNT; stat++) {
+		u32 counter = axienet_ior(lp, XAE_STATS_OFFSET + stat * 8);

The * 8 here suggests the counters are spaced so that they could be 64 bit wide, even when only 32 bits are used.

Correct.

Quoted 4 lines

Does the documentation say anything about the upper 32 bits when the counters are only 32 bits? Are they guaranteed to read as zero? I'm just wondering if the code should be forward looking and read all 64 bits?

The registers are documented as being 32-bit, with the upper 32-bits being registered upon reading the lower 32 bits. The documentation doesn't say what the upper registers are when the counters are 32 bits.

Quoted 5 lines
Quoted 5 lines

static int __axienet_device_reset(struct axienet_local *lp) {

drivers/net/ethernet/xilinx/xilinx_axienet_main.c +0 -0
 	u32 value;
 	int ret;
Quoted 10 lines

drivers/net/ethernet/xilinx/xilinx_axienet_main.c static void axienet_setoptions(struct net_device *ndev, u32 options) · line 566

Quoted 5 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +5 -0
+	/* Save statistics counters in case they will be reset */
+	if (lp->features & XAE_FEATURE_STATS) {
+		mutex_lock(&lp->stats_lock);
+		axienet_stats_update(lp);
+	}

It is a pretty unusual pattern to split a mutex lock/unlock like this on an if statement. Maybe just unconditionally hold the mutex? This does not appear to be anyway hot path, so the overhead should not matter.

OK

--Sean

#11 lore raw
In reply to Andrew Lunn
Quoted 13 lines · Andrew Lunn
Quoted 13 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +9 -0
+static u64 axienet_stat(struct axienet_local *lp, enum temac_stat stat)
+{
+	return u64_stats_read(&lp->hw_stats[stat]);
+}
@@ -1695,6 +1760,35 @@ axienet_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
 		stats->tx_packets = u64_stats_read(&lp->tx_packets);
 		stats->tx_bytes = u64_stats_read(&lp->tx_bytes);
 	} while (u64_stats_fetch_retry(&lp->tx_stat_sync, start));
+
+	if (!(lp->features & XAE_FEATURE_STATS))
+		return;
+
+	do {
Quoted 8 lines

drivers/net/ethernet/xilinx/xilinx_axienet_main.c axienet_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) · line 1770

Quoted 3 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +3 -0
+		start = u64_stats_fetch_begin(&lp->hw_stat_sync);
+		stats->rx_length_errors =
+			axienet_stat(lp, STAT_RX_LENGTH_ERRORS);

I'm i reading this correctly. You are returning the counters from the last refresh period. What is that? 2.5Gbps would wrapper around a 32 byte counter in 13 seconds. I hope these statistics are not 13 seconds out of date?

By default we use a 1 Hz refresh period. You can of course configure this up to 13 seconds, but we refuse to raise it further since we risk missing a wrap-around. It's configurable by userspace so they can determine how out-of-date they like their stats (vs how often they want to wake up the CPU).

Quoted 3 lines

Since axienet_stats_update() also uses the lp->hw_stat_sync, i don't see why you cannot read the hardware counter value and update to the latest value.

We would need to synchronize against updates to hw_last_counter. Imagine a scenario like

CPU 1 CPU 2
__axienet_device_reset()

	axienet_stats_update()
					axienet_stat()
						u64_stats_read()
						axienet_ior()
	/* device reset */
	hw_last_counter = 0
						stats->foo = ... - hw_last_counter[...]

and now we have a glitch in the counter values, since we effectively are double-counting the current counter value. Alternatively, we could read the counter after reset but before hw_last_counter was updated and get a glitch due to underflow.

--Sean

#8 lore raw
In reply to Andrew Lunn
Quoted 23 lines · Andrew Lunn
Quoted 6 lines · Andrew Lunn
Quoted 4 lines · Sean Anderson

Add support for reading the statistics counters, if they are enabled. The counters may be 64-bit, but we can't detect this as there's no ability bit for it and the counters are read-only. Therefore, we assume the counters are 32-bits.

Quoted 15 lines

drivers/net/ethernet/xilinx/xilinx_axienet_main.c static void axienet_setoptions(struct net_device *ndev, u32 options) · line 534

Quoted 9 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +9 -0
+static void axienet_stats_update(struct axienet_local *lp)
+{
+	enum temac_stat stat;
+
+	lockdep_assert_held(&lp->stats_lock);
+
+	u64_stats_update_begin(&lp->hw_stat_sync);
+	for (stat = 0; stat < STAT_COUNT; stat++) {
+		u32 counter = axienet_ior(lp, XAE_STATS_OFFSET + stat * 8);

The * 8 here suggests the counters are spaced so that they could be 64 bit wide, even when only 32 bits are used. Does the documentation say anything about the upper 32 bits when the counters are only 32 bits? Are they guaranteed to read as zero? I'm just wondering if the code should be forward looking and read all 64 bits?

Quoted 4 lines

Actually, if you read the upper 32 bits and they are not 0, you know you have 64 bit counters. You can then kill off your period task, it is not needed because your software counters will wrap around the same time as the hardware counters.

Yes, but then our stats remain stale forever, because we don't refresh stats before reading them as detailed in my other response.

--Sean

reviewer
#14 lore raw
In reply to Sean Anderson
Quoted 31 lines · Sean Anderson

Add support for reading the statistics counters, if they are enabled. The counters may be 64-bit, but we can't detect this as there's no ability bit for it and the counters are read-only. Therefore, we assume the counters are 32-bits. To ensure we don't miss an overflow, we need to read all counters at regular intervals, configurable with stats-block-usecs. This should be often enough to ensure the bytes counters don't wrap at 2.5 Gbit/s.

Another complication is that the counters may be reset when the device is reset (depending on configuration). To ensure the counters persist across link up/down (including suspend/resume), we maintain our own 64-bit versions along with the last counter value we saw. Because we might wait up to 100 ms for the reset to complete, we use a mutex to protect writing hw_stats. We can't sleep in ndo_get_stats64, so we use a u64_stats_sync to protect readers.

We can't use the byte counters for either get_stats64 or get_eth_mac_stats. This is because the byte counters include everything in the frame (destination address to FCS, inclusive). But rtnl_link_stats64 wants bytes excluding the FCS, and ethtool_eth_mac_stats wants to exclude the L2 overhead (addresses and length/type).

---

 drivers/net/ethernet/xilinx/xilinx_axienet.h  |  81 ++++++
 .../net/ethernet/xilinx/xilinx_axienet_main.c | 267 +++++++++++++++++-
 2 files changed, 345 insertions(+), 3 deletions(-)
drivers/net/ethernet/xilinx/xilinx_axienet.h +0 -0

...

drivers/net/ethernet/xilinx/xilinx_axienet.h struct skbuf_dma_descriptor { · line 507

Quoted 7 lines
drivers/net/ethernet/xilinx/xilinx_axienet.h +3 -0
@@ -434,6 +502,11 @@ struct skbuf_dma_descriptor {
  * @tx_packets: TX packet count for statistics
  * @tx_bytes:	TX byte count for statistics
  * @tx_stat_sync: Synchronization object for TX stats
+ * @hw_last_counter: Last-seen value of each statistic
+ * @hw_stats: Interface statistics periodically updated from hardware counters
+ * @hw_stats_sync: Synchronization object for @hw_stats

nit: s/hw_stats_sync/hw_stat_sync/

Flagged by kernel-doc -none

Quoted 26 lines
drivers/net/ethernet/xilinx/xilinx_axienet.h +9 -0
+ * @stats_lock: Lock for writing @hw_stats and @hw_last_counter
+ * @stats_work: Work for reading the hardware statistics counters
  * @dma_err_task: Work structure to process Axi DMA errors
  * @tx_irq:	Axidma TX IRQ number
  * @rx_irq:	Axidma RX IRQ number
@@ -452,6 +525,7 @@ struct skbuf_dma_descriptor {
  * @coalesce_usec_rx:	IRQ coalesce delay for RX
  * @coalesce_count_tx:	Store the irq coalesce on TX side.
  * @coalesce_usec_tx:	IRQ coalesce delay for TX
+ * @coalesce_usec_stats: Delay between hardware statistics refreshes
  * @use_dmaengine: flag to check dmaengine framework usage.
  * @tx_chan:	TX DMA channel.
  * @rx_chan:	RX DMA channel.
@@ -505,6 +579,12 @@ struct axienet_local {
 	u64_stats_t tx_bytes;
 	struct u64_stats_sync tx_stat_sync;
 
+	u32 hw_last_counter[STAT_COUNT];
+	u64_stats_t hw_stats[STAT_COUNT];
+	struct u64_stats_sync hw_stat_sync;
+	struct mutex stats_lock;
+	struct delayed_work stats_work;
+
 	struct work_struct dma_err_task;
 
 	int tx_irq;

...

#15 lore raw
Quoted 45 lines · Simon Horman
Quoted 31 lines · Sean Anderson

Add support for reading the statistics counters, if they are enabled. The counters may be 64-bit, but we can't detect this as there's no ability bit for it and the counters are read-only. Therefore, we assume the counters are 32-bits. To ensure we don't miss an overflow, we need to read all counters at regular intervals, configurable with stats-block-usecs. This should be often enough to ensure the bytes counters don't wrap at 2.5 Gbit/s.

Another complication is that the counters may be reset when the device is reset (depending on configuration). To ensure the counters persist across link up/down (including suspend/resume), we maintain our own 64-bit versions along with the last counter value we saw. Because we might wait up to 100 ms for the reset to complete, we use a mutex to protect writing hw_stats. We can't sleep in ndo_get_stats64, so we use a u64_stats_sync to protect readers.

We can't use the byte counters for either get_stats64 or get_eth_mac_stats. This is because the byte counters include everything in the frame (destination address to FCS, inclusive). But rtnl_link_stats64 wants bytes excluding the FCS, and ethtool_eth_mac_stats wants to exclude the L2 overhead (addresses and length/type).

---

 drivers/net/ethernet/xilinx/xilinx_axienet.h  |  81 ++++++
 .../net/ethernet/xilinx/xilinx_axienet_main.c | 267 +++++++++++++++++-
 2 files changed, 345 insertions(+), 3 deletions(-)
drivers/net/ethernet/xilinx/xilinx_axienet.h +0 -0

...

drivers/net/ethernet/xilinx/xilinx_axienet.h struct skbuf_dma_descriptor { · line 507

Quoted 7 lines
drivers/net/ethernet/xilinx/xilinx_axienet.h +3 -0
@@ -434,6 +502,11 @@ struct skbuf_dma_descriptor {
  * @tx_packets: TX packet count for statistics
  * @tx_bytes:	TX byte count for statistics
  * @tx_stat_sync: Synchronization object for TX stats
+ * @hw_last_counter: Last-seen value of each statistic
+ * @hw_stats: Interface statistics periodically updated from hardware counters
+ * @hw_stats_sync: Synchronization object for @hw_stats

nit: s/hw_stats_sync/hw_stat_sync/

Quoted 1 line

Flagged by kernel-doc -none

I think I will rename the member instead.

--Sean

Quoted 28 lines
Quoted 26 lines
drivers/net/ethernet/xilinx/xilinx_axienet.h +9 -0
+ * @stats_lock: Lock for writing @hw_stats and @hw_last_counter
+ * @stats_work: Work for reading the hardware statistics counters
  * @dma_err_task: Work structure to process Axi DMA errors
  * @tx_irq:	Axidma TX IRQ number
  * @rx_irq:	Axidma RX IRQ number
@@ -452,6 +525,7 @@ struct skbuf_dma_descriptor {
  * @coalesce_usec_rx:	IRQ coalesce delay for RX
  * @coalesce_count_tx:	Store the irq coalesce on TX side.
  * @coalesce_usec_tx:	IRQ coalesce delay for TX
+ * @coalesce_usec_stats: Delay between hardware statistics refreshes
  * @use_dmaengine: flag to check dmaengine framework usage.
  * @tx_chan:	TX DMA channel.
  * @rx_chan:	RX DMA channel.
@@ -505,6 +579,12 @@ struct axienet_local {
 	u64_stats_t tx_bytes;
 	struct u64_stats_sync tx_stat_sync;
 
+	u32 hw_last_counter[STAT_COUNT];
+	u64_stats_t hw_stats[STAT_COUNT];
+	struct u64_stats_sync hw_stat_sync;
+	struct mutex stats_lock;
+	struct delayed_work stats_work;
+
 	struct work_struct dma_err_task;
 
 	int tx_irq;

...

#12 lore raw
In reply to Sean Anderson

Hi Andrew,

Quoted 46 lines · Sean Anderson
Quoted 13 lines · Andrew Lunn
Quoted 13 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +9 -0
+static u64 axienet_stat(struct axienet_local *lp, enum temac_stat stat)
+{
+	return u64_stats_read(&lp->hw_stats[stat]);
+}
@@ -1695,6 +1760,35 @@ axienet_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
 		stats->tx_packets = u64_stats_read(&lp->tx_packets);
 		stats->tx_bytes = u64_stats_read(&lp->tx_bytes);
 	} while (u64_stats_fetch_retry(&lp->tx_stat_sync, start));
+
+	if (!(lp->features & XAE_FEATURE_STATS))
+		return;
+
+	do {
Quoted 8 lines

drivers/net/ethernet/xilinx/xilinx_axienet_main.c axienet_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) · line 1770

Quoted 3 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +3 -0
+		start = u64_stats_fetch_begin(&lp->hw_stat_sync);
+		stats->rx_length_errors =
+			axienet_stat(lp, STAT_RX_LENGTH_ERRORS);

I'm i reading this correctly. You are returning the counters from the last refresh period. What is that? 2.5Gbps would wrapper around a 32 byte counter in 13 seconds. I hope these statistics are not 13 seconds out of date?

By default we use a 1 Hz refresh period. You can of course configure this up to 13 seconds, but we refuse to raise it further since we risk missing a wrap-around. It's configurable by userspace so they can determine how out-of-date they like their stats (vs how often they want to wake up the CPU).

Quoted 3 lines

Since axienet_stats_update() also uses the lp->hw_stat_sync, i don't see why you cannot read the hardware counter value and update to the latest value.

We would need to synchronize against updates to hw_last_counter. Imagine a scenario like

CPU 1 CPU 2
__axienet_device_reset()

	axienet_stats_update()
					axienet_stat()
						u64_stats_read()
						axienet_ior()
	/* device reset */
	hw_last_counter = 0
						stats->foo = ... - hw_last_counter[...]
Quoted 4 lines

and now we have a glitch in the counter values, since we effectively are double-counting the current counter value. Alternatively, we could read the counter after reset but before hw_last_counter was updated and get a glitch due to underflow.

Does this make sense to you? If it does, I'll send v2 with just the mutex change and the variable rename pointed out by Simon.

--Sean

#13 lore raw
Quoted 49 lines · Sean Anderson

Hi Andrew,

Quoted 46 lines · Sean Anderson
Quoted 13 lines · Andrew Lunn
Quoted 13 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +9 -0
+static u64 axienet_stat(struct axienet_local *lp, enum temac_stat stat)
+{
+	return u64_stats_read(&lp->hw_stats[stat]);
+}
@@ -1695,6 +1760,35 @@ axienet_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
 		stats->tx_packets = u64_stats_read(&lp->tx_packets);
 		stats->tx_bytes = u64_stats_read(&lp->tx_bytes);
 	} while (u64_stats_fetch_retry(&lp->tx_stat_sync, start));
+
+	if (!(lp->features & XAE_FEATURE_STATS))
+		return;
+
+	do {
Quoted 8 lines

drivers/net/ethernet/xilinx/xilinx_axienet_main.c axienet_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) · line 1770

Quoted 3 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +3 -0
+		start = u64_stats_fetch_begin(&lp->hw_stat_sync);
+		stats->rx_length_errors =
+			axienet_stat(lp, STAT_RX_LENGTH_ERRORS);

I'm i reading this correctly. You are returning the counters from the last refresh period. What is that? 2.5Gbps would wrapper around a 32 byte counter in 13 seconds. I hope these statistics are not 13 seconds out of date?

By default we use a 1 Hz refresh period. You can of course configure this up to 13 seconds, but we refuse to raise it further since we risk missing a wrap-around. It's configurable by userspace so they can determine how out-of-date they like their stats (vs how often they want to wake up the CPU).

Quoted 3 lines

Since axienet_stats_update() also uses the lp->hw_stat_sync, i don't see why you cannot read the hardware counter value and update to the latest value.

We would need to synchronize against updates to hw_last_counter. Imagine a scenario like

CPU 1 CPU 2
__axienet_device_reset()

	axienet_stats_update()
					axienet_stat()
						u64_stats_read()
						axienet_ior()
	/* device reset */
	hw_last_counter = 0
						stats->foo = ... - hw_last_counter[...]
Quoted 7 lines
Quoted 4 lines

and now we have a glitch in the counter values, since we effectively are double-counting the current counter value. Alternatively, we could read the counter after reset but before hw_last_counter was updated and get a glitch due to underflow.

Does this make sense to you? If it does, I'll send v2 with just the mutex change and the variable rename pointed out by Simon.

What you have is O.K. I just think you can do better.

As you point out, there is a potential race. There are a few synchronisation mechanisms for that.

Often you arrange to have exclusive access to a data structure, so you know it cannot change while you use it. But as you pointed out, you are not in a context which can block on a mutex.

Another mechanism is to know if the data structure has changed while you where using it. If it has, throw away what you have, and start again. That is what lp->hw_stat_sync etc is all about. You loop while its value changes, indicating something made changes. You should be able to use this when returning statistics to user space, to return the real current statistics, not old cached values.

Andrew