From a982b8a7b805eff913e53eeb0163301214f11534 Mon Sep 17 00:00:00 2001 From: meijian Date: Mon, 2 Sep 2024 15:51:53 +0800 Subject: [PATCH] net/netstatistics: add tx/rx bytes statistics for dev add bytes into /proc/net/wlan0 wlan0 Link encap:Ethernet HWaddr 42:37:46:02:16:07 at UP mtu 1500 inet addr:10.0.1.2 DRaddr:10.0.1.1 Mask:255.255.255.0 inet6 DRaddr: :: RX: Received Fragment Errors Bytes 000008c1 00000000 00000000 331ca8 IPv4 IPv6 ARP Dropped 000008a7 00000018 00000002 00000000 TX: Queued Sent Errors Timeouts Bytes 00000973 00000973 00000000 00000000 1b8d3 Total Errors: 00000000 Signed-off-by: meijian --- arch/arm/src/kinetis/kinetis_enet.c | 8 ++-- arch/arm/src/lpc17xx_40xx/lpc17_40_ethernet.c | 8 ++-- arch/arm/src/lpc54xx/lpc54_ethernet.c | 10 ++--- arch/arm/src/rtl8720c/amebaz_netdev.c | 2 +- arch/mips/src/pic32mx/pic32mx_ethernet.c | 8 ++-- arch/misoc/src/common/misoc_net.c | 7 ++-- arch/risc-v/src/litex/litex_emac.c | 4 +- drivers/net/enc28j60.c | 8 ++-- drivers/net/netdev_upperhalf.c | 3 +- drivers/net/tun.c | 2 +- drivers/net/w5500.c | 8 ++-- include/nuttx/net/netdev.h | 41 +++++++++++++++++-- net/procfs/netdev_statistics.c | 20 +++++---- 13 files changed, 84 insertions(+), 45 deletions(-) diff --git a/arch/arm/src/kinetis/kinetis_enet.c b/arch/arm/src/kinetis/kinetis_enet.c index ff48618d9d872..f4fb089be0d64 100644 --- a/arch/arm/src/kinetis/kinetis_enet.c +++ b/arch/arm/src/kinetis/kinetis_enet.c @@ -576,10 +576,6 @@ static void kinetis_receive(struct kinetis_driver_s *priv) while ((priv->rxdesc[priv->rxtail].status1 & RXDESC_E) == 0) { - /* Update statistics */ - - NETDEV_RXPACKETS(&priv->dev); - /* Copy the buffer pointer to priv->dev.d_buf. Set amount of data in * priv->dev.d_len */ @@ -588,6 +584,10 @@ static void kinetis_receive(struct kinetis_driver_s *priv) priv->dev.d_buf = (uint8_t *)kinesis_swap32((uint32_t)priv->rxdesc[priv->rxtail].data); + /* Update statistics */ + + NETDEV_RXPACKETS(&priv->dev); + #ifdef CONFIG_NET_PKT /* When packet sockets are enabled, feed the frame into the tap */ diff --git a/arch/arm/src/lpc17xx_40xx/lpc17_40_ethernet.c b/arch/arm/src/lpc17xx_40xx/lpc17_40_ethernet.c index 871dc0c0fede5..8cc92632556bf 100644 --- a/arch/arm/src/lpc17xx_40xx/lpc17_40_ethernet.c +++ b/arch/arm/src/lpc17xx_40xx/lpc17_40_ethernet.c @@ -821,10 +821,6 @@ static void lpc17_40_rxdone_work(void *arg) fragment = false; while (considx != prodidx) { - /* Update statistics */ - - NETDEV_RXPACKETS(&priv->lp_dev); - /* Get the Rx status and packet length (-4+1) */ rxstat = (uint32_t *)(LPC17_40_RXSTAT_BASE + (considx << 3)); @@ -980,6 +976,10 @@ static void lpc17_40_rxdone_work(void *arg) } } + /* Update statistics */ + + NETDEV_RXPACKETS(&priv->lp_dev); + /* Bump up the consumer index and resample the producer index (which * might also have gotten bumped up by the hardware). */ diff --git a/arch/arm/src/lpc54xx/lpc54_ethernet.c b/arch/arm/src/lpc54xx/lpc54_ethernet.c index bad52f8f528c5..a46dc31891f42 100644 --- a/arch/arm/src/lpc54xx/lpc54_ethernet.c +++ b/arch/arm/src/lpc54xx/lpc54_ethernet.c @@ -1223,7 +1223,7 @@ static void lpc54_eth_txdone(struct lpc54_ethdriver_s *priv, { /* Update statistics */ - NETDEV_TXDONE(priv->eth_dev); + NETDEV_TXDONE(&priv->eth_dev); /* Free the Tx buffer assigned to the descriptor */ @@ -1321,12 +1321,12 @@ static void lpc54_eth_channel_work(struct lpc54_ethdriver_s *priv, if ((pending & LPC54_RXERR_INTMASK) != 0) { - NETDEV_RXERRORS(priv->eth_dev); + NETDEV_RXERRORS(&priv->eth_dev); } if ((pending & LPC54_TXERR_INTMASK) != 0) { - NETDEV_TXERRORS(priv->eth_dev); + NETDEV_TXERRORS(&priv->eth_dev); } /* The Receive Buffer Unavailable (RBU) error is a special case. It @@ -1375,7 +1375,7 @@ static void lpc54_eth_channel_work(struct lpc54_ethdriver_s *priv, { /* Update statistics if a packet was dispatched */ - NETDEV_RXPACKETS(priv->eth_dev); + NETDEV_RXPACKETS(&priv->eth_dev); } else { @@ -1576,7 +1576,7 @@ static void lpc54_eth_txtimeout_work(void *arg) /* Increment statistics and dump debug info */ - NETDEV_TXTIMEOUTS(priv->eth_dev); + NETDEV_TXTIMEOUTS(&priv->eth_dev); /* Then reset the hardware by bringing it down and taking it back up * again. diff --git a/arch/arm/src/rtl8720c/amebaz_netdev.c b/arch/arm/src/rtl8720c/amebaz_netdev.c index 0b5ac0a18f406..68fdb7a9a8c1d 100644 --- a/arch/arm/src/rtl8720c/amebaz_netdev.c +++ b/arch/arm/src/rtl8720c/amebaz_netdev.c @@ -107,12 +107,12 @@ void amebaz_netdev_notify_receive(struct amebaz_dev_s *priv, return; } - NETDEV_RXPACKETS(&priv->dev); net_lock(); oldbuf = priv->dev.d_buf; hdr = (struct eth_hdr_s *)skb->data; priv->dev.d_buf = (void *)skb->data; priv->dev.d_len = len; + NETDEV_RXPACKETS(&priv->dev); #ifdef CONFIG_NET_PKT pkt_input(&priv->dev); #endif diff --git a/arch/mips/src/pic32mx/pic32mx_ethernet.c b/arch/mips/src/pic32mx/pic32mx_ethernet.c index 398caea8766bf..6c340b3390347 100644 --- a/arch/mips/src/pic32mx/pic32mx_ethernet.c +++ b/arch/mips/src/pic32mx/pic32mx_ethernet.c @@ -1290,15 +1290,15 @@ static void pic32mx_rxdone(struct pic32mx_driver_s *priv) pic32mx_dumprxdesc(rxdesc, "RX Complete"); - /* Update statistics */ - - NETDEV_RXPACKETS(&priv->pd_dev); - /* Get the packet length */ priv->pd_dev.d_len = (rxdesc->rsv2 & RXDESC_RSV2_BYTECOUNT_MASK) >> RXDESC_RSV2_BYTECOUNT_SHIFT; + /* Update statistics */ + + NETDEV_RXPACKETS(&priv->pd_dev); + /* Check for errors */ if ((rxdesc->rsv2 & RXDESC_RSV2_OK) == 0) diff --git a/arch/misoc/src/common/misoc_net.c b/arch/misoc/src/common/misoc_net.c index 98945b52f899f..96b314bb67b04 100644 --- a/arch/misoc/src/common/misoc_net.c +++ b/arch/misoc/src/common/misoc_net.c @@ -194,7 +194,7 @@ static int misoc_net_transmit(struct misoc_net_driver_s *priv) /* Increment statistics */ - NETDEV_TXPACKETS(priv->misoc_net_dev); + NETDEV_TXPACKETS(&priv->misoc_net_dev); /* Send the packet: address=priv->misoc_net_dev.d_buf, * length=priv->misoc_net_dev.d_len @@ -353,6 +353,7 @@ static void misoc_net_receive(struct misoc_net_driver_s *priv) priv->misoc_net_dev.d_len = rxlen; + NETDEV_RXPACKETS(&priv->misoc_net_dev); #ifdef CONFIG_NET_PKT /* When packet sockets are enabled, feed the frame into the tap */ @@ -452,7 +453,7 @@ static void misoc_net_txdone(struct misoc_net_driver_s *priv) { /* Check for errors and update statistics */ - NETDEV_TXDONE(priv->misoc_net_dev); + NETDEV_TXDONE(&priv->misoc_net_dev); /* Check if there are pending transmissions */ @@ -598,7 +599,7 @@ static void misoc_net_txtimeout_work(void *arg) /* Increment statistics and dump debug info */ net_lock(); - NETDEV_TXTIMEOUTS(priv->misoc_net_dev); + NETDEV_TXTIMEOUTS(&priv->misoc_net_dev); /* Then reset the hardware */ diff --git a/arch/risc-v/src/litex/litex_emac.c b/arch/risc-v/src/litex/litex_emac.c index 3064f8843bdf2..8ccb5d4bdac27 100644 --- a/arch/risc-v/src/litex/litex_emac.c +++ b/arch/risc-v/src/litex/litex_emac.c @@ -551,12 +551,12 @@ static int litex_transmit(struct litex_emac_s *priv) static void litex_receive(struct litex_emac_s *priv) { + priv->dev.d_len = getreg16(LITEX_ETHMAC_SRAM_WRITER_LENGTH); + /* Update statistics */ NETDEV_RXPACKETS(&priv->dev); - priv->dev.d_len = getreg16(LITEX_ETHMAC_SRAM_WRITER_LENGTH); - if (priv->dev.d_len == 0 || priv->dev.d_len > ETHMAC_SLOT_SIZE) { NETDEV_RXDROPPED(&priv->dev); diff --git a/drivers/net/enc28j60.c b/drivers/net/enc28j60.c index 157f5e2a8fcd0..20b755363defb 100644 --- a/drivers/net/enc28j60.c +++ b/drivers/net/enc28j60.c @@ -1434,10 +1434,6 @@ static void enc_pktif(FAR struct enc_driver_s *priv) uint16_t pktlen; uint16_t rxstat; - /* Update statistics */ - - NETDEV_RXPACKETS(&priv->dev); - /* Set the read pointer to the start of the received packet (ERDPT) */ DEBUGASSERT(priv->nextpkt <= PKTMEM_RX_END); @@ -1506,6 +1502,10 @@ static void enc_pktif(FAR struct enc_driver_s *priv) enc_rxdispatch(priv); } + /* Update statistics */ + + NETDEV_RXPACKETS(&priv->dev); + /* Move the RX read pointer to the start of the next received packet. * This frees the memory we just read. */ diff --git a/drivers/net/netdev_upperhalf.c b/drivers/net/netdev_upperhalf.c index a97f3202b3c63..aa668db15ade8 100644 --- a/drivers/net/netdev_upperhalf.c +++ b/drivers/net/netdev_upperhalf.c @@ -527,8 +527,6 @@ static void netdev_upper_rxpoll_work(FAR struct netdev_upperhalf_s *upper) while ((pkt = lower->ops->receive(lower)) != NULL) { - NETDEV_RXPACKETS(dev); - if (!IFF_IS_UP(dev->d_flags)) { /* Interface down, drop frame */ @@ -539,6 +537,7 @@ static void netdev_upper_rxpoll_work(FAR struct netdev_upperhalf_s *upper) } netpkt_put(dev, pkt, NETPKT_RX); + NETDEV_RXPACKETS(dev); #ifdef CONFIG_NET_PKT /* When packet sockets are enabled, feed the frame into the tap */ diff --git a/drivers/net/tun.c b/drivers/net/tun.c index 9d84b959daee5..fa0b28fc3d436 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -271,7 +271,6 @@ static void tun_pollnotify(FAR struct tun_device_s *priv, static void tun_fd_transmit(FAR struct tun_device_s *priv) { - NETDEV_TXPACKETS(&priv->dev); tun_pollnotify(priv, POLLIN); } @@ -307,6 +306,7 @@ static int tun_txpoll(FAR struct net_driver_s *dev) DEBUGASSERT(priv->read_buf == NULL); + NETDEV_TXPACKETS(dev); #ifdef CONFIG_NET_PKT /* When packet sockets are enabled, feed the frame into the tap */ diff --git a/drivers/net/w5500.c b/drivers/net/w5500.c index fe46992305513..3e06ca30fb93d 100644 --- a/drivers/net/w5500.c +++ b/drivers/net/w5500.c @@ -1104,13 +1104,13 @@ static void w5500_transmit(FAR struct w5500_driver_s *self) if (!w5500_txbuf_numfree(self)) { ninfo("Dropping Tx packet due to no buffer available.\n"); - NETDEV_TXERRORS(self->w_dev); + NETDEV_TXERRORS(&self->w_dev); return; } /* Increment statistics */ - NETDEV_TXPACKETS(self->w_dev); + NETDEV_TXPACKETS(&self->w_dev); /* Copy packet data to TX buffer */ @@ -1457,7 +1457,7 @@ static void w5500_txdone(FAR struct w5500_driver_s *self) { /* Check for errors and update statistics */ - NETDEV_TXDONE(self->w_dev); + NETDEV_TXDONE(&self->w_dev); /* Check if there are pending transmissions. */ @@ -1669,7 +1669,7 @@ static void w5500_txtimeout_work(FAR void *arg) /* Increment statistics and dump debug info */ - NETDEV_TXTIMEOUTS(self->w_dev); + NETDEV_TXTIMEOUTS(&self->w_dev); /* Then reset the hardware */ diff --git a/include/nuttx/net/netdev.h b/include/nuttx/net/netdev.h index 2bd529cd1e46f..75c272e3b3152 100644 --- a/include/nuttx/net/netdev.h +++ b/include/nuttx/net/netdev.h @@ -112,7 +112,36 @@ } \ while (0) -# define NETDEV_RXPACKETS(dev) _NETDEV_STATISTIC(dev,rx_packets) +#define _NETDEV_BYTES(dev,name) \ + do { \ + (dev)->d_statistics.name += (dev)->d_len; \ + } while (0) + +# if CONFIG_NETDEV_STATISTICS_LOG_PERIOD > 0 +# define NETDEV_STATISTICS_WORK LPWORK +# define _NETDEV_STATISTIC_LOG(dev,name) \ + do \ + { \ + _NETDEV_STATISTIC(dev,name); \ + if (work_available(&(dev)->d_statistics.logwork)) \ + { \ + work_queue(NETDEV_STATISTICS_WORK, \ + &(dev)->d_statistics.logwork, \ + netdev_statistics_log, (dev), \ + SEC2TICK(CONFIG_NETDEV_STATISTICS_LOG_PERIOD)); \ + } \ + } \ + while (0) +# else +# define _NETDEV_STATISTIC_LOG(dev,name) _NETDEV_STATISTIC(dev,name) +# endif + +# define NETDEV_RXPACKETS(dev) \ + do { \ + _NETDEV_STATISTIC_LOG(dev,rx_packets); \ + _NETDEV_BYTES(dev,rx_bytes); \ + } while (0) + # define NETDEV_RXFRAGMENTS(dev) _NETDEV_STATISTIC(dev,rx_fragments) # define NETDEV_RXERRORS(dev) _NETDEV_ERROR(dev,rx_errors) # ifdef CONFIG_NET_IPv4 @@ -132,11 +161,15 @@ # endif # define NETDEV_RXDROPPED(dev) _NETDEV_STATISTIC(dev,rx_dropped) -# define NETDEV_TXPACKETS(dev) _NETDEV_STATISTIC(dev,tx_packets) +# define NETDEV_TXPACKETS(dev) \ + do { \ + _NETDEV_STATISTIC_LOG(dev,tx_packets); \ + _NETDEV_BYTES(dev,tx_bytes); \ + } while (0) + # define NETDEV_TXDONE(dev) _NETDEV_STATISTIC(dev,tx_done) # define NETDEV_TXERRORS(dev) _NETDEV_ERROR(dev,tx_errors) # define NETDEV_TXTIMEOUTS(dev) _NETDEV_ERROR(dev,tx_timeouts) - # define NETDEV_ERRORS(dev) _NETDEV_STATISTIC(dev,errors) #else @@ -208,6 +241,7 @@ struct netdev_statistics_s uint32_t rx_arp; /* Number of Rx ARP packets received */ #endif uint32_t rx_dropped; /* Unsupported Rx packets received */ + uint64_t rx_bytes; /* Number of bytes received */ /* Tx Status */ @@ -215,6 +249,7 @@ struct netdev_statistics_s uint32_t tx_done; /* Number of packets completed */ uint32_t tx_errors; /* Number of receive errors (incl timeouts) */ uint32_t tx_timeouts; /* Number of Tx timeout errors */ + uint64_t tx_bytes; /* Number of bytes send */ /* Other status */ diff --git a/net/procfs/netdev_statistics.c b/net/procfs/netdev_statistics.c index aeaa43d3e334a..46d02daf443da 100644 --- a/net/procfs/netdev_statistics.c +++ b/net/procfs/netdev_statistics.c @@ -410,8 +410,8 @@ static int netprocfs_rxstatistics_header( FAR struct netprocfs_file_s *netfile) { DEBUGASSERT(netfile != NULL); - return snprintf(netfile->line, NET_LINELEN , "\tRX: %-8s %-8s %-8s\n", - "Received", "Fragment", "Errors"); + return snprintf(netfile->line, NET_LINELEN , "\tRX: %-8s %-8s %-8s %-8s\n", + "Received", "Fragment", "Errors", "Bytes"); } #endif /* CONFIG_NETDEV_STATISTICS */ @@ -429,10 +429,12 @@ static int netprocfs_rxstatistics(FAR struct netprocfs_file_s *netfile) dev = netfile->dev; stats = &dev->d_statistics; - return snprintf(netfile->line, NET_LINELEN, "\t %08lx %08lx %08lx\n", + return snprintf(netfile->line, NET_LINELEN, \ + "\t %08lx %08lx %08lx %-16llx\n", (unsigned long)stats->rx_packets, (unsigned long)stats->rx_fragments, - (unsigned long)stats->rx_errors); + (unsigned long)stats->rx_errors, + (unsigned long long)stats->rx_bytes); } #endif /* CONFIG_NETDEV_STATISTICS */ @@ -524,8 +526,9 @@ static int netprocfs_txstatistics_header( { DEBUGASSERT(netfile != NULL); - return snprintf(netfile->line, NET_LINELEN, "\tTX: %-8s %-8s %-8s %-8s\n", - "Queued", "Sent", "Errors", "Timeouts"); + return snprintf(netfile->line, NET_LINELEN, + "\tTX: %-8s %-8s %-8s %-8s %-8s\n", + "Queued", "Sent", "Errors", "Timeouts", "Bytes"); } #endif /* CONFIG_NETDEV_STATISTICS */ @@ -544,11 +547,12 @@ static int netprocfs_txstatistics(FAR struct netprocfs_file_s *netfile) stats = &dev->d_statistics; return snprintf(netfile->line, NET_LINELEN, - "\t %08lx %08lx %08lx %08lx\n", + "\t %08lx %08lx %08lx %08lx %-16llx \n", (unsigned long)stats->tx_packets, (unsigned long)stats->tx_done, (unsigned long)stats->tx_errors, - (unsigned long)stats->tx_timeouts); + (unsigned long)stats->tx_timeouts, + (unsigned long long)stats->tx_bytes); } #endif /* CONFIG_NETDEV_STATISTICS */