This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 99ac4c32ae1a94817fd2e423173b48f11fbbfbf2
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 19 12:22:05 2026 +0100

    net/procfs: Count the lines of /proc/net/tcp and udp in full.
    
    The TCP and UDP tables counted the lines already written in
    netprocfs_file_s.offset, a uint8_t, and found the place of the next read
    by skipping that many connections from the head of the list.  With 255
    connections or more the count wrapped, the next read started again from
    the head, and a read of the file never ended (many connections in
    TIME_WAIT are enough).  A count from the head also skipped a connection
    for each written one that went away between two reads.  The tables went
    through the connections once per address family, so a new IPv4
    connection could move an IPv6 line that was already written.
    
    Now the tables go through the list once, in its order, and find their
    place by the list id of the last connection written.  A TCP connection
    has a list id since the previous commit; a UDP connection now gets one
    in udp_alloc().  The list ids grow from the head to the tail, so a
    connection that goes away does not move the place.  The first read
    takes the list id of the last connection (limit), and the connections
    that come later are not listed.  So a read of the file to its end ends,
    and lists once each connection that is there from the first read to the
    end.  The line number (sl) is an unsigned int.
    
    The IPv4 and IPv6 lines of a table now come in the order of the list,
    not the IPv4 lines first.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/procfs/net_tcp.c | 174 +++++++++++++++++++++++++++++----------------------
 net/procfs/net_udp.c | 152 +++++++++++++++++++++++++-------------------
 net/procfs/procfs.h  |   5 ++
 net/udp/udp.h        |  22 +++++++
 net/udp/udp_conn.c   |  27 +++++++-
 5 files changed, 240 insertions(+), 140 deletions(-)

diff --git a/net/procfs/net_tcp.c b/net/procfs/net_tcp.c
index 33563c86f29..33c1796b93d 100644
--- a/net/procfs/net_tcp.c
+++ b/net/procfs/net_tcp.c
@@ -55,29 +55,86 @@
  * Private Functions
  ****************************************************************************/
 
-static ssize_t netprocfs_tcpstats(FAR struct netprocfs_file_s *priv,
-                                  FAR char *buffer, size_t buflen,
-                                  uint8_t domain, int *skip)
+static int netprocfs_tcpline(FAR struct netprocfs_file_s *priv,
+                             FAR char *buffer, size_t buflen,
+                             FAR struct tcp_conn_s *conn)
 {
+  uint8_t domain = net_ip_domain_select(conn->domain, PF_INET, PF_INET6);
   int addrlen = (domain == PF_INET) ?
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN;
-  FAR struct tcp_conn_s *conn = NULL;
   char remote[INET6_ADDRSTRLEN];
   char local[INET6_ADDRSTRLEN];
-  int len = 0;
   FAR void *laddr;
   FAR void *raddr;
+  int len;
+
+  laddr = net_ip_binding_laddr(&conn->u, domain);
+  raddr = net_ip_binding_raddr(&conn->u, domain);
+
+  len = snprintf(buffer, buflen,
+                 "    %2u"
+                 ": %02" PRIx8
+                 " %3" PRIx8 " %3" PRIu8
+                 " %3" PRIu8
+                 " %4" PRIu32
+                 " %3" PRIu8
+#if CONFIG_NET_SEND_BUFSIZE > 0 && defined(CONFIG_NET_TCP_WRITE_BUFFERS)
+                 " %6" PRIu32
+#endif
+                 " %6u",
+                 priv->lines,
+                 conn->tcpstateflags,
+                 conn->sconn.s_flags,
+                 conn->crefs,
+                 conn->timer,
+                 (uint32_t)conn->tx_unacked,
+                 conn->nrtx,
+#if CONFIG_NET_SEND_BUFSIZE > 0 && defined(CONFIG_NET_TCP_WRITE_BUFFERS)
+                 tcp_wrbuffer_inqueue_size(conn),
+#endif
+                 (conn->readahead) ? conn->readahead->io_pktlen : 0);
+
+  len += snprintf(buffer + len, buflen - len,
+                  " %*s:%-6" PRIu16 " %*s:%-6" PRIu16 "\n",
+                  (domain == PF_INET6) ? addrlen / 2 : addrlen,
+                  inet_ntop(domain, laddr, local, addrlen),
+                  ntohs(conn->lport),
+                  (domain == PF_INET6) ? addrlen / 2 : addrlen,
+                  inet_ntop(domain, raddr, remote, addrlen),
+                  ntohs(conn->rport));
+
+  return len;
+}
+
+/****************************************************************************
+ * Name: netprocfs_tcpstats
+ *
+ * Description:
+ *   Write the lines of the connections after the last one written.
+ *
+ *   A read of the file to its end lists once each connection that is in
+ *   the list from the first read to the end.  The walk finds its place by
+ *   the list id of the last connection written (priv->last), not by a
+ *   count: the list ids grow from the head to the tail, so a connection
+ *   that goes away does not move the place.  A connection that comes
+ *   after the first read has a list id after priv->limit and is not
+ *   listed, so the read ends.
+ *
+ * Assumptions:
+ *   The TCP connection list is locked.
+ *
+ ****************************************************************************/
+
+static ssize_t netprocfs_tcpstats(FAR struct netprocfs_file_s *priv,
+                                  FAR char *buffer, size_t buflen)
+{
+  FAR struct tcp_conn_s *conn = NULL;
+  int len = 0;
 
   while ((conn = tcp_nextconn(conn)) != NULL)
     {
-#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
-      if (conn->domain != domain)
-        {
-          continue;
-        }
-#endif /* CONFIG_NET_IPv4 && CONFIG_NET_IPv6 */
-
-      if (++(*skip) <= priv->offset)
+      if (!TCP_LISTID_AFTER(conn->listid, priv->last) ||
+          TCP_LISTID_AFTER(conn->listid, priv->limit))
         {
           continue;
         }
@@ -87,40 +144,9 @@ static ssize_t netprocfs_tcpstats(FAR struct 
netprocfs_file_s *priv,
           break;
         }
 
-      laddr = net_ip_binding_laddr(&conn->u, domain);
-      raddr = net_ip_binding_raddr(&conn->u, domain);
-
-      len += snprintf(buffer + len, buflen - len,
-                      "    %2" PRIu8
-                      ": %02" PRIx8
-                      " %3" PRIx8 " %3" PRIu8
-                      " %3" PRIu8
-                      " %4" PRIu32
-                      " %3" PRIu8
-#if CONFIG_NET_SEND_BUFSIZE > 0 && defined(CONFIG_NET_TCP_WRITE_BUFFERS)
-                      " %6" PRIu32
-#endif
-                      " %6u",
-                      priv->offset++,
-                      conn->tcpstateflags,
-                      conn->sconn.s_flags,
-                      conn->crefs,
-                      conn->timer,
-                      (uint32_t)conn->tx_unacked,
-                      conn->nrtx,
-#if CONFIG_NET_SEND_BUFSIZE > 0 && defined(CONFIG_NET_TCP_WRITE_BUFFERS)
-                      tcp_wrbuffer_inqueue_size(conn),
-#endif
-                      (conn->readahead) ? conn->readahead->io_pktlen : 0);
-
-      len += snprintf(buffer + len, buflen - len,
-                      " %*s:%-6" PRIu16 " %*s:%-6" PRIu16 "\n",
-                      (domain == PF_INET6) ? addrlen / 2 : addrlen,
-                      inet_ntop(domain, laddr, local, addrlen),
-                      ntohs(conn->lport),
-                      (domain == PF_INET6) ? addrlen / 2 : addrlen,
-                      inet_ntop(domain, raddr, remote, addrlen),
-                      ntohs(conn->rport));
+      len += netprocfs_tcpline(priv, buffer + len, buflen - len, conn);
+      priv->last = conn->listid;
+      priv->lines++;
     }
 
   return len;
@@ -151,41 +177,41 @@ static ssize_t netprocfs_tcpstats(FAR struct 
netprocfs_file_s *priv,
 ssize_t netprocfs_read_tcpstats(FAR struct netprocfs_file_s *priv,
                                 FAR char *buffer, size_t buflen)
 {
-  int skip = 1;
+  FAR struct tcp_conn_s *conn;
   int len = 0;
 
   tcp_conn_list_lock();
 
-  if (tcp_nextconn(NULL) != NULL)
+  conn = tcp_nextconn(NULL);
+  if (priv->lines == 0 && conn != NULL)
     {
-      if (priv->offset == 0)
-        {
-          len = snprintf(buffer, buflen, "TCP sl  "
-                                         "st flg ref tmr uack nrt   "
+      /* The first read: the header, and the bounds of this read, the
+       * connections from the head of the list to the last one now.
+       */
+
+      priv->last  = conn->listid - 1;
+      priv->limit = tcp_conn_lastid();
+
+      len = snprintf(buffer, buflen, "TCP sl  "
+                                     "st flg ref tmr uack nrt   "
 #if CONFIG_NET_SEND_BUFSIZE > 0 && defined(CONFIG_NET_TCP_WRITE_BUFFERS)
-                                          "txsz   "
+                                      "txsz   "
 #endif
-                                          "rxsz "
-                                          "%-*s "
-                                          "%-*s\n"
-                                          ,
-                                          INET6_ADDRSTRLEN / 2,
-                                          "local_address",
-                                          INET6_ADDRSTRLEN / 2,
-                                          "remote_address"
-                                          );
-          priv->offset = 1;
-        }
-
-#ifdef CONFIG_NET_IPv4
-      len += netprocfs_tcpstats(priv, buffer + len,
-                                buflen - len, PF_INET, &skip);
-#endif /* CONFIG_NET_IPv4 */
+                                      "rxsz "
+                                      "%-*s "
+                                      "%-*s\n"
+                                      ,
+                                      INET6_ADDRSTRLEN / 2,
+                                      "local_address",
+                                      INET6_ADDRSTRLEN / 2,
+                                      "remote_address"
+                                      );
+      priv->lines = 1;
+    }
 
-#ifdef CONFIG_NET_IPv6
-      len += netprocfs_tcpstats(priv, buffer + len,
-                                buflen - len, PF_INET6, &skip);
-#endif /* CONFIG_NET_IPv6 */
+  if (priv->lines > 0)
+    {
+      len += netprocfs_tcpstats(priv, buffer + len, buflen - len);
     }
 
   tcp_conn_list_unlock();
diff --git a/net/procfs/net_udp.c b/net/procfs/net_udp.c
index fe2f3c9d8e1..e32d898bc52 100644
--- a/net/procfs/net_udp.c
+++ b/net/procfs/net_udp.c
@@ -55,29 +55,73 @@
  * Private Functions
  ****************************************************************************/
 
-static ssize_t netprocfs_udpstats(FAR struct netprocfs_file_s *priv,
-                                  FAR char *buffer, size_t buflen,
-                                  uint8_t domain, int *skip)
+static int netprocfs_udpline(FAR struct netprocfs_file_s *priv,
+                             FAR char *buffer, size_t buflen,
+                             FAR struct udp_conn_s *conn)
 {
+  uint8_t domain = net_ip_domain_select(conn->domain, PF_INET, PF_INET6);
   int addrlen = (domain == PF_INET) ?
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN;
-  FAR struct udp_conn_s *conn = NULL;
   char remote[INET6_ADDRSTRLEN];
   char local[INET6_ADDRSTRLEN];
-  int len = 0;
   FAR void *laddr;
   FAR void *raddr;
+  int len;
+
+  laddr = net_ip_binding_laddr(&conn->u, domain);
+  raddr = net_ip_binding_raddr(&conn->u, domain);
+
+  len = snprintf(buffer, buflen,
+                 "    %2u"
+                 ": %3" PRIx8
+#ifdef CONFIG_NET_UDP_WRITE_BUFFERS
+                 " %6" PRIu32
+#endif
+                 " %6u",
+                 priv->lines,
+                 conn->sconn.s_flags,
+#ifdef CONFIG_NET_UDP_WRITE_BUFFERS
+                 udp_wrbuffer_inqueue_size(conn),
+#endif
+                 (conn->readahead) ? conn->readahead->io_pktlen : 0);
+
+  len += snprintf(buffer + len, buflen - len,
+                  " %*s:%-6" PRIu16 " %*s:%-6" PRIu16 "\n",
+                  (domain == PF_INET6) ? addrlen / 2 : addrlen,
+                  inet_ntop(domain, laddr, local, addrlen),
+                  ntohs(conn->lport),
+                  (domain == PF_INET6) ? addrlen / 2 : addrlen,
+                  inet_ntop(domain, raddr, remote, addrlen),
+                  ntohs(conn->rport));
+
+  return len;
+}
+
+/****************************************************************************
+ * Name: netprocfs_udpstats
+ *
+ * Description:
+ *   Write the lines of the connections after the last one written.  The
+ *   walk finds its place by the list id of the last connection written, as
+ *   netprocfs_tcpstats() (net_tcp.c) does: a read of the file to its end
+ *   lists once each connection that is in the list from the first read to
+ *   the end, and it ends.
+ *
+ * Assumptions:
+ *   The UDP connection list is locked.
+ *
+ ****************************************************************************/
+
+static ssize_t netprocfs_udpstats(FAR struct netprocfs_file_s *priv,
+                                  FAR char *buffer, size_t buflen)
+{
+  FAR struct udp_conn_s *conn = NULL;
+  int len = 0;
 
   while ((conn = udp_nextconn(conn)) != NULL)
     {
-#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
-      if (conn->domain != domain)
-        {
-          continue;
-        }
-#endif /* CONFIG_NET_IPv4 && CONFIG_NET_IPv6 */
-
-      if (++(*skip) <= priv->offset)
+      if (!UDP_LISTID_AFTER(conn->listid, priv->last) ||
+          UDP_LISTID_AFTER(conn->listid, priv->limit))
         {
           continue;
         }
@@ -87,31 +131,9 @@ static ssize_t netprocfs_udpstats(FAR struct 
netprocfs_file_s *priv,
           break;
         }
 
-      laddr = net_ip_binding_laddr(&conn->u, domain);
-      raddr = net_ip_binding_raddr(&conn->u, domain);
-
-      len += snprintf(buffer + len, buflen - len,
-                      "    %2" PRIu8
-                      ": %3" PRIx8
-#ifdef CONFIG_NET_UDP_WRITE_BUFFERS
-                      " %6" PRIu32
-#endif
-                      " %6u",
-                      priv->offset++,
-                      conn->sconn.s_flags,
-#ifdef CONFIG_NET_UDP_WRITE_BUFFERS
-                      udp_wrbuffer_inqueue_size(conn),
-#endif
-                      (conn->readahead) ? conn->readahead->io_pktlen : 0);
-
-      len += snprintf(buffer + len, buflen - len,
-                      " %*s:%-6" PRIu16 " %*s:%-6" PRIu16 "\n",
-                      (domain == PF_INET6) ? addrlen / 2 : addrlen,
-                      inet_ntop(domain, laddr, local, addrlen),
-                      ntohs(conn->lport),
-                      (domain == PF_INET6) ? addrlen / 2 : addrlen,
-                      inet_ntop(domain, raddr, remote, addrlen),
-                      ntohs(conn->rport));
+      len += netprocfs_udpline(priv, buffer + len, buflen - len, conn);
+      priv->last = conn->listid;
+      priv->lines++;
     }
 
   return len;
@@ -142,40 +164,40 @@ static ssize_t netprocfs_udpstats(FAR struct 
netprocfs_file_s *priv,
 ssize_t netprocfs_read_udpstats(FAR struct netprocfs_file_s *priv,
                                 FAR char *buffer, size_t buflen)
 {
-  int skip = 1;
+  FAR struct udp_conn_s *conn;
   int len = 0;
 
   udp_conn_list_lock();
-  if (udp_nextconn(NULL) != NULL)
+  conn = udp_nextconn(NULL);
+  if (priv->lines == 0 && conn != NULL)
     {
-      if (priv->offset == 0)
-        {
-          len = snprintf(buffer, buflen, "UDP sl "
-                                         " flg   "
+      /* The first read: the header, and the bounds of this read, the
+       * connections from the head of the list to the last one now.
+       */
+
+      priv->last  = conn->listid - 1;
+      priv->limit = udp_conn_lastid();
+
+      len = snprintf(buffer, buflen, "UDP sl "
+                                     " flg   "
 #if CONFIG_NET_SEND_BUFSIZE > 0
-                                         "txsz   "
+                                     "txsz   "
 #endif
-                                         "rxsz "
-                                          "%-*s "
-                                          "%-*s\n"
-                                          ,
-                                          INET6_ADDRSTRLEN / 2,
-                                          "local_address",
-                                          INET6_ADDRSTRLEN / 2,
-                                          "remote_address"
-                                          );
-          priv->offset = 1;
-        }
-
-#ifdef CONFIG_NET_IPv4
-      len += netprocfs_udpstats(priv, buffer + len,
-                                buflen - len, PF_INET, &skip);
-#endif /* CONFIG_NET_IPv4 */
+                                     "rxsz "
+                                      "%-*s "
+                                      "%-*s\n"
+                                      ,
+                                      INET6_ADDRSTRLEN / 2,
+                                      "local_address",
+                                      INET6_ADDRSTRLEN / 2,
+                                      "remote_address"
+                                      );
+      priv->lines = 1;
+    }
 
-#ifdef CONFIG_NET_IPv6
-      len += netprocfs_udpstats(priv, buffer + len,
-                                buflen - len, PF_INET6, &skip);
-#endif /* CONFIG_NET_IPv6 */
+  if (priv->lines > 0)
+    {
+      len += netprocfs_udpstats(priv, buffer + len, buflen - len);
     }
 
   udp_conn_list_unlock();
diff --git a/net/procfs/procfs.h b/net/procfs/procfs.h
index d9a92bd69d7..45166a50b6c 100644
--- a/net/procfs/procfs.h
+++ b/net/procfs/procfs.h
@@ -63,6 +63,11 @@ struct netprocfs_file_s
   uint8_t linesize;                  /* Number of valid characters in line[] */
   uint8_t offset;                    /* Offset to first valid character in 
line[] */
   uint8_t entry;                     /* Entry index of netprocfs_entry_s */
+  unsigned int lines;                /* Lines of a table written so far */
+  uint32_t last;                     /* List id of the last connection
+                                      * written */
+  uint32_t limit;                    /* List id of the last connection at
+                                      * the first read */
   char line[NET_LINELEN];            /* Pre-allocated buffer for formatted 
lines */
 };
 
diff --git a/net/udp/udp.h b/net/udp/udp.h
index ae11535a37f..6533beb8c90 100644
--- a/net/udp/udp.h
+++ b/net/udp/udp.h
@@ -79,6 +79,12 @@
 #define UDPIPv4BUF ((FAR struct udp_hdr_s *)IPBUF(IPv4_HDRLEN))
 #define UDPIPv6BUF ((FAR struct udp_hdr_s *)IPBUF(IPv6_HDRLEN))
 
+/* True if the connection with list id a entered the list of UDP
+ * connections after the one with list id b (the ids wrap).
+ */
+
+#define UDP_LISTID_AFTER(a, b) ((int32_t)((uint32_t)(a) - (uint32_t)(b)) > 0)
+
 /* UDP read-ahead metadata layout:
  * |datalen|ifindex|src_addr_size|src_addr|[timestamp]|data|
  */
@@ -129,6 +135,8 @@ struct udp_conn_s
   uint8_t  flags;         /* See _UDP_FLAG_* definitions */
   uint8_t  domain;        /* IP domain: PF_INET or PF_INET6 */
   uint8_t  crefs;         /* Reference counts on this instance */
+  uint32_t listid;        /* The order of entry in the list of UDP
+                           * connections (udp_alloc()) */
 
 #if CONFIG_NET_RECV_BUFSIZE > 0
   int32_t  rcvbufs;       /* Maximum amount of bytes queued in recv */
@@ -260,6 +268,20 @@ FAR struct udp_conn_s *udp_active(FAR struct net_driver_s 
*dev,
 
 FAR struct udp_conn_s *udp_nextconn(FAR struct udp_conn_s *conn);
 
+/****************************************************************************
+ * Name: udp_conn_lastid
+ *
+ * Description:
+ *   Return the list id of the connection that entered the list of UDP
+ *   connections last.
+ *
+ * Assumptions:
+ *   The UDP connection list is locked.
+ *
+ ****************************************************************************/
+
+uint32_t udp_conn_lastid(void);
+
 /****************************************************************************
  * Name: udp_conn_list_lock
  *
diff --git a/net/udp/udp_conn.c b/net/udp/udp_conn.c
index 2f9dd871500..c4abe64428e 100644
--- a/net/udp/udp_conn.c
+++ b/net/udp/udp_conn.c
@@ -101,6 +101,10 @@ static rmutex_t g_udp_connections_lock = 
NXRMUTEX_INITIALIZER;
 
 static dq_queue_t g_active_udp_connections;
 
+/* The list id of the connection that entered the list last */
+
+static uint32_t g_udp_listid;
+
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
@@ -724,8 +728,11 @@ FAR struct udp_conn_s *udp_alloc(uint8_t domain)
 
       sq_init(&conn->write_q);
 #endif
-      /* Enqueue the connection into the active list */
+      /* Enqueue the connection into the active list, at its tail, with the
+       * next list id.  So the list ids grow from the head to the tail.
+       */
 
+      conn->listid = ++g_udp_listid;
       dq_addlast(&conn->sconn.s_node, &g_active_udp_connections);
     }
 
@@ -885,6 +892,24 @@ FAR struct udp_conn_s *udp_nextconn(FAR struct udp_conn_s 
*conn)
     }
 }
 
+/****************************************************************************
+ * Name: udp_conn_lastid
+ *
+ * Description:
+ *   Return the list id of the connection that entered the list of UDP
+ *   connections last.  A connection with a later list id
+ *   (UDP_LISTID_AFTER()) entered the list after this call.
+ *
+ * Assumptions:
+ *   The UDP connection list is locked.
+ *
+ ****************************************************************************/
+
+uint32_t udp_conn_lastid(void)
+{
+  return g_udp_listid;
+}
+
 /****************************************************************************
  * Name: udp_bind
  *

Reply via email to