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

commit 09e7ee8b1cf64652f4c46452e4f8c28ac8fef4a1
Author: Marco Casaroli <[email protected]>
AuthorDate: Sun Nov 22 17:56:00 2026 +0100

    net/tcp: Share the rewind of a write buffer in the retransmission paths.
    
    On TCP_REXMIT, psock_send_eventhandler() rewinds the partly sent write
    buffer at the head of write_q, and retransmit_segment() each write
    buffer of unacked_q, with the same code: count the retransmission, take
    the bytes sent from the write buffer out of conn->sent, and set them to
    zero.  Move that code to rewind_segment() and call it from both places.
    Also fix the comment "the number of bytes sent sent".  No change of
    behaviour.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/tcp/tcp_send_buffered.c | 81 ++++++++++++++++++++++++++-------------------
 1 file changed, 47 insertions(+), 34 deletions(-)

diff --git a/net/tcp/tcp_send_buffered.c b/net/tcp/tcp_send_buffered.c
index bf2f7bc66c1..0823d8bd200 100644
--- a/net/tcp/tcp_send_buffered.c
+++ b/net/tcp/tcp_send_buffered.c
@@ -174,16 +174,30 @@ static void psock_writebuffer_notify(FAR struct 
tcp_conn_s *conn)
 #  define psock_writebuffer_notify(conn)
 #endif
 
-static void retransmit_segment(FAR struct tcp_conn_s *conn,
-                               FAR struct tcp_wrbuffer_s *wrb)
+/****************************************************************************
+ * Name: rewind_segment
+ *
+ * Description:
+ *   Count a retransmission of a write buffer and reset the number of bytes
+ *   sent from it, so that its data is sent again from the start.  The
+ *   write buffer stays until its data is acknowledged: tcp_timer() closes
+ *   the connection when the retry count of the connection reaches
+ *   TCP_MAXRTX.
+ *
+ * Input Parameters:
+ *   conn     The connection structure of the write buffer
+ *   wrb      The write buffer to send again
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+static void rewind_segment(FAR struct tcp_conn_s *conn,
+                           FAR struct tcp_wrbuffer_s *wrb)
 {
   uint16_t sent;
 
-  /* 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.
-   */
-
   if (TCP_WBNRTX(wrb) < UINT8_MAX)
     {
       TCP_WBNRTX(wrb)++;
@@ -195,7 +209,7 @@ static void retransmit_segment(FAR struct tcp_conn_s *conn,
         "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 */
+  /* Reset the number of bytes sent from the write buffer */
 
   if (conn->sent > sent)
     {
@@ -207,6 +221,28 @@ static void retransmit_segment(FAR struct tcp_conn_s *conn,
     }
 
   TCP_WBSENT(wrb) = 0;
+}
+
+/****************************************************************************
+ * Name: retransmit_segment
+ *
+ * Description:
+ *   Rewind a write buffer of the unacked_q and put it back into the
+ *   write_q, so that it is sent again.
+ *
+ * Input Parameters:
+ *   conn     The connection structure of the write buffer
+ *   wrb      The write buffer to send again
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+static void retransmit_segment(FAR struct tcp_conn_s *conn,
+                               FAR struct tcp_wrbuffer_s *wrb)
+{
+  rewind_segment(conn, wrb);
 
   /* Insert the write buffer into the write_q (in sequence
    * number order).  The retransmission will occur below
@@ -846,34 +882,11 @@ static uint32_t psock_send_eventhandler(FAR struct 
net_driver_s *dev,
 
       if (wrb != NULL && TCP_WBSENT(wrb) > 0)
         {
-          uint16_t sent;
-
-          /* Increment the retransmit count on this write buffer. */
-
-          if (TCP_WBNRTX(wrb) < UINT8_MAX)
-            {
-              TCP_WBNRTX(wrb)++;
-            }
-
-          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
+          /* Yes.. Send it again from the start.  It stays at the head of
+           * the write_q.
            */
 
-          if (conn->sent > sent)
-            {
-              conn->sent -= sent;
-            }
-          else
-            {
-              conn->sent = 0;
-            }
-
-          TCP_WBSENT(wrb) = 0;
+          rewind_segment(conn, wrb);
         }
 
       /* Move all segments that have been sent but not ACKed to the write

Reply via email to