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

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


The following commit(s) were added to refs/heads/master by this push:
     new e54d06774d6 net/tcp: Do not free a write buffer before its data is 
acknowledged.
e54d06774d6 is described below

commit e54d06774d60e5cb316ebf6537021a6342f870b3
Author: Marco Casaroli <[email protected]>
AuthorDate: Fri Nov 20 16:57:58 2026 +0100

    net/tcp: Do not free a write buffer before its data is acknowledged.
    
    At the TCP_MAXRTX retransmission of a write buffer,
    psock_send_eventhandler() freed the write buffer and its data, and the
    connection stayed open until the next timeout of tcp_timer().  The data
    of the next write buffer then went out at the sequence numbers of the
    freed data: the peer received wrong data without an error, and then a
    reset.
    
    The count of a write buffer also increases while the peer acknowledges a
    part of it at each timeout.  Thus a connection that made progress at
    each timeout lost data after TCP_MAXRTX timeouts.
    
    Since a91c50243ef, tcp_timer() also closes a connection with write
    buffers when the retry count of the connection (conn->nrtx) reaches
    TCP_MAXRTX: it sends a RST and gives TCP_TIMEDOUT to the socket.  An ACK
    of all the data sent sets that count to zero.  Remove the expiry of a
    write buffer and conn->expired.  A write buffer now stays until its data
    is acknowledged or the connection is closed, as without write buffers.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/tcp/tcp.h               |   2 -
 net/tcp/tcp_conn.c          |   2 -
 net/tcp/tcp_send_buffered.c | 145 ++++++++++++++------------------------------
 net/tcp/tcp_timer.c         |  12 ++--
 4 files changed, 49 insertions(+), 112 deletions(-)

diff --git a/net/tcp/tcp.h b/net/tcp/tcp.h
index 57f5f048dee..c739f302538 100644
--- a/net/tcp/tcp.h
+++ b/net/tcp/tcp.h
@@ -336,8 +336,6 @@ struct tcp_conn_s
 
   sq_queue_t write_q;     /* Write buffering for segments */
   sq_queue_t unacked_q;   /* Write buffering for un-ACKed segments */
-  uint16_t   expired;     /* Number segments retransmitted but not yet ACKed,
-                           * it can only be updated at TCP_ESTABLISHED state */
   uint32_t   sent;        /* The number of bytes sent (ACKed and un-ACKed) */
   uint32_t   isn;         /* Initial sequence number */
   uint32_t   sndseq_max;  /* The sequence number of next not-retransmitted
diff --git a/net/tcp/tcp_conn.c b/net/tcp/tcp_conn.c
index 98d3c1c6641..2a5d792f57e 100644
--- a/net/tcp/tcp_conn.c
+++ b/net/tcp/tcp_conn.c
@@ -1329,7 +1329,6 @@ FAR struct tcp_conn_s *tcp_alloc_accept(FAR struct 
net_driver_s *dev,
 
       conn->tx_unacked       = 1;
 #ifdef CONFIG_NET_TCP_WRITE_BUFFERS
-      conn->expired          = 0;
       conn->isn              = 0;
       conn->sent             = 0;
       conn->sndseq_max       = tcp_getsequence(conn->sndseq) + 1;
@@ -1646,7 +1645,6 @@ int tcp_connect(FAR struct tcp_conn_s *conn, FAR const 
struct sockaddr *addr)
   tcp_initsequence(conn);
 
 #ifdef CONFIG_NET_TCP_WRITE_BUFFERS
-  conn->expired    = 0;
   conn->isn        = 0;
   conn->sent       = 0;
   conn->sndseq_max = tcp_getsequence(conn->sndseq) + 1;
diff --git a/net/tcp/tcp_send_buffered.c b/net/tcp/tcp_send_buffered.c
index e5d1fa2d84b..cba7ffb35c3 100644
--- a/net/tcp/tcp_send_buffered.c
+++ b/net/tcp/tcp_send_buffered.c
@@ -177,68 +177,47 @@ static void psock_writebuffer_notify(FAR struct 
tcp_conn_s *conn)
 static void retransmit_segment(FAR struct tcp_conn_s *conn,
                                FAR struct tcp_wrbuffer_s *wrb)
 {
-  /* Free any write buffers that have exceed the retry count */
+  uint16_t sent;
 
-  if (++TCP_WBNRTX(wrb) >= TCP_MAXRTX)
-    {
-      nwarn("WARNING: Expiring wrb=%p nrtx=%u\n",
-            wrb, TCP_WBNRTX(wrb));
-
-      /* Return the write buffer to the free list */
+  /* Count the retransmission.  The write buffer stays until its data is
+   * acknowledged: tcp_timer() closes the connection when the retry count
+   * of the connection reaches TCP_MAXRTX.
+   */
 
-      tcp_wrbuffer_release(wrb);
+  if (TCP_WBNRTX(wrb) < UINT8_MAX)
+    {
+      TCP_WBNRTX(wrb)++;
+    }
 
-      /* Notify any waiters if the write buffers have been
-       * drained.
-       */
+  sent = TCP_WBSENT(wrb);
 
-      psock_writebuffer_notify(conn);
+  ninfo("REXMIT: wrb=%p sent=%u, "
+        "conn tx_unacked=%" PRId32 " sent=%" PRId32 "\n",
+        wrb, TCP_WBSENT(wrb), conn->tx_unacked, conn->sent);
 
-      /* NOTE expired is different from un-ACKed, it is designed
-       * to represent the number of segments that have been sent,
-       * retransmitted, and un-ACKed, if expired is not zero, the
-       * connection will be closed.
-       *
-       * field expired can only be updated at TCP_ESTABLISHED and
-       * TCP_CLOSE_WAIT state.
-       */
+  /* Reset the number of bytes sent sent from the write buffer */
 
-      conn->expired++;
+  if (conn->sent > sent)
+    {
+      conn->sent -= sent;
     }
   else
     {
-      uint16_t sent;
-
-      sent = TCP_WBSENT(wrb);
-
-      ninfo("REXMIT: wrb=%p sent=%u, "
-            "conn tx_unacked=%" PRId32 " sent=%" PRId32 "\n",
-            wrb, TCP_WBSENT(wrb), conn->tx_unacked, conn->sent);
-
-      /* Reset the number of bytes sent sent from the write buffer */
-
-      if (conn->sent > sent)
-        {
-          conn->sent -= sent;
-        }
-      else
-        {
-          conn->sent = 0;
-        }
+      conn->sent = 0;
+    }
 
-      TCP_WBSENT(wrb) = 0;
+  TCP_WBSENT(wrb) = 0;
 
-      /* Insert the write buffer into the write_q (in sequence
-       * number order).  The retransmission will occur below
-       * when the write buffer with the lowest sequence number
-       * is pulled from the write_q again.
-       */
+  /* Insert the write buffer into the write_q (in sequence
+   * number order).  The retransmission will occur below
+   * when the write buffer with the lowest sequence number
+   * is pulled from the write_q again.
+   */
 
-      ninfo("REXMIT: Moving wrb=%p nrtx=%u\n",
-            wrb, TCP_WBNRTX(wrb));
+  ninfo("REXMIT: Moving wrb=%p nrtx=%u\n",
+        wrb, TCP_WBNRTX(wrb));
 
-      psock_insert_segment(wrb, &conn->write_q);
-    }
+  psock_insert_segment(wrb, &conn->write_q);
 }
 
 /****************************************************************************
@@ -869,68 +848,34 @@ static uint32_t psock_send_eventhandler(FAR struct 
net_driver_s *dev,
 
       if (wrb != NULL && TCP_WBSENT(wrb) > 0)
         {
-          FAR struct tcp_wrbuffer_s *tmp;
+          uint16_t sent;
 
           /* Increment the retransmit count on this write buffer. */
 
-          if (++TCP_WBNRTX(wrb) >= TCP_MAXRTX)
+          if (TCP_WBNRTX(wrb) < UINT8_MAX)
             {
-              nwarn("WARNING: Expiring wrb=%p nrtx=%u\n",
-                    wrb, TCP_WBNRTX(wrb));
-
-              /* The maximum retry count as been exhausted. Remove the write
-               * buffer at the head of the queue.
-               */
-
-              tmp = (FAR struct tcp_wrbuffer_s *)sq_remfirst(&conn->write_q);
-              DEBUGASSERT(tmp == wrb);
-              UNUSED(tmp);
-
-              /* And return the write buffer to the free list */
-
-              tcp_wrbuffer_release(wrb);
-
-              /* Notify any waiters if the write buffers have been
-               * drained.
-               */
+              TCP_WBNRTX(wrb)++;
+            }
 
-              psock_writebuffer_notify(conn);
+          sent = TCP_WBSENT(wrb);
+          ninfo("REXMIT: wrb=%p sent=%u, "
+                "conn tx_unacked=%" PRId32 " sent=%" PRId32 "\n",
+                wrb, TCP_WBSENT(wrb), conn->tx_unacked, conn->sent);
 
-              /* NOTE expired is different from un-ACKed, it is designed to
-               * represent the number of segments that have been sent,
-               * retransmitted, and un-ACKed, if expired is not zero, the
-               * connection will be closed.
-               *
-               * field expired can only be updated at TCP_ESTABLISHED and
-               * TCP_CLOSE_WAIT state.
-               */
+          /* Yes.. Reset the number of bytes sent sent from
+           * the write buffer
+           */
 
-              conn->expired++;
+          if (conn->sent > sent)
+            {
+              conn->sent -= sent;
             }
           else
             {
-              uint16_t sent;
-
-              sent = TCP_WBSENT(wrb);
-              ninfo("REXMIT: wrb=%p sent=%u, "
-                    "conn tx_unacked=%" PRId32 " sent=%" PRId32 "\n",
-                    wrb, TCP_WBSENT(wrb), conn->tx_unacked, conn->sent);
-
-              /* Yes.. Reset the number of bytes sent sent from
-               * the write buffer
-               */
-
-              if (conn->sent > sent)
-                {
-                  conn->sent -= sent;
-                }
-              else
-                {
-                  conn->sent = 0;
-                }
-
-              TCP_WBSENT(wrb) = 0;
+              conn->sent = 0;
             }
+
+          TCP_WBSENT(wrb) = 0;
         }
 
       /* Move all segments that have been sent but not ACKed to the write
diff --git a/net/tcp/tcp_timer.c b/net/tcp/tcp_timer.c
index 9e17d944311..566c13ef936 100644
--- a/net/tcp/tcp_timer.c
+++ b/net/tcp/tcp_timer.c
@@ -566,14 +566,10 @@ void tcp_timer(FAR struct net_driver_s *dev, FAR struct 
tcp_conn_s *conn)
                * close the connection.
                */
 
-              else if (
-#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
-                  conn->expired > 0 ||
-#endif
-                  (conn->tcpstateflags != TCP_SYN_SENT &&
-                   conn->nrtx >= TCP_MAXRTX) ||
-                  (conn->tcpstateflags == TCP_SYN_SENT &&
-                   conn->nrtx >= TCP_MAXSYNRTX))
+              else if ((conn->tcpstateflags != TCP_SYN_SENT &&
+                        conn->nrtx >= TCP_MAXRTX) ||
+                       (conn->tcpstateflags == TCP_SYN_SENT &&
+                        conn->nrtx >= TCP_MAXSYNRTX))
                 {
                   conn->tcpstateflags = TCP_CLOSED;
                   ninfo("TCP state: TCP_CLOSED\n");

Reply via email to