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 f71b98e2c4ad105d35a94909cb13d7a10a898040
Author: Marco Casaroli <[email protected]>
AuthorDate: Fri Nov 20 14:02:51 2026 +0100

    net/tcp: Do not send the first SYN as a retransmission.
    
    tcp_connect() sets conn->timeout, so the first SYN goes out through the
    timeout branch of tcp_timer().  That branch counts a retransmission
    (nrtx and the rexmit statistic) and, since 872bff9cae4 ("Double the
    retransmission timeout at the first expiry too"), doubles the RTO
    before it sends the SYN.  So the first resend of a lost SYN comes after
    twice the initial RTO (3 s with the default CONFIG_NET_TCP_RTO of
    1.5 s), and the statistic counts every SYN as a retransmission.
    
    tcp_connect() now marks the connection with TCP_SYNFIRST.  The timer
    sends the first SYN with the initial RTO, without a count, and clears
    the mark.  A later timeout is a retransmission as before.  The first
    resend now comes after the initial RTO (RFC 6298, section 2.1).
    
    TCP_MAXSYNRTX now counts only the resends, as TCP_MAXRTX counts the
    resends of data: a connect() sends one SYN more before it gives up
    (with the default 5: the SYN and 5 resends).  This is what the name and
    RFC 1122, section 4.2.3.5 (R2 for a SYN), mean: the limit is on
    retransmissions, and the first SYN is not one.  The time until a
    connect() to a silent peer fails stays about the same (70 s, was 69 s).
    NET_TCP_MAXSYNRTX gets a help text that says so.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/tcp/Kconfig     |  5 +++++
 net/tcp/tcp.h       |  1 +
 net/tcp/tcp_conn.c  |  1 +
 net/tcp/tcp_timer.c | 15 +++++++++++++++
 4 files changed, 22 insertions(+)

diff --git a/net/tcp/Kconfig b/net/tcp/Kconfig
index fc75b8135d9..ed1c82adaf7 100644
--- a/net/tcp/Kconfig
+++ b/net/tcp/Kconfig
@@ -176,6 +176,11 @@ config NET_TCP_MAXRTX
 config NET_TCP_MAXSYNRTX
        int "Maximum retransmitted number of TCP/IP SYN packet"
        default 5
+       ---help---
+               The number of times that the SYN of connect(), or the SYN-ACK 
of a
+               connection that a listener accepts, is sent again when no answer
+               comes.  The first SYN or SYN-ACK is not counted: connect() sends
+               at most NET_TCP_MAXSYNRTX + 1 SYNs.
 
 config NET_TCP_WAIT_TIMEOUT
        int "TIME_WAIT Length of TCP/IP connections"
diff --git a/net/tcp/tcp.h b/net/tcp/tcp.h
index 189a26ef6b5..fae1aa5007a 100644
--- a/net/tcp/tcp.h
+++ b/net/tcp/tcp.h
@@ -150,6 +150,7 @@
 
 #define TCP_RESET             0x20U /* The peer reset the connection */
 #define TCP_RESETERR          0x40U /* ECONNRESET of it not yet reported */
+#define TCP_SYNFIRST          0x80U /* The first SYN of connect() not sent */
 
 /* The Max Range count of TCP Selective ACKs */
 
diff --git a/net/tcp/tcp_conn.c b/net/tcp/tcp_conn.c
index 602a5a0ae89..e25bbdc80c9 100644
--- a/net/tcp/tcp_conn.c
+++ b/net/tcp/tcp_conn.c
@@ -1625,6 +1625,7 @@ int tcp_connect(FAR struct tcp_conn_s *conn, FAR const 
struct sockaddr *addr)
   conn->tx_unacked = 1;    /* TCP length of the SYN is one. */
   conn->nrtx       = 0;
   conn->timeout    = true; /* Send the SYN immediately. */
+  conn->flags     |= TCP_SYNFIRST;
   conn->rto        = TCP_RTO;
   conn->sa         = 0;
   conn->sv         = 16;   /* Initial value of the RTT variance. */
diff --git a/net/tcp/tcp_timer.c b/net/tcp/tcp_timer.c
index 566c13ef936..ca3ab604c8e 100644
--- a/net/tcp/tcp_timer.c
+++ b/net/tcp/tcp_timer.c
@@ -536,6 +536,21 @@ void tcp_timer(FAR struct net_driver_s *dev, FAR struct 
tcp_conn_s *conn)
 
               conn->timer = 0;
 
+              /* The first SYN of tcp_connect() is not a retransmission:
+               * send it with the initial RTO and do not count it.
+               */
+
+              if ((conn->flags & TCP_SYNFIRST) != 0)
+                {
+                  conn->flags &= ~TCP_SYNFIRST;
+                  if (conn->tcpstateflags == TCP_SYN_SENT)
+                    {
+                      tcp_setsequence(conn->sndseq, conn->rexmit_seq);
+                      tcp_synack(dev, conn, TCP_SYN);
+                      goto done;
+                    }
+                }
+
               /* Check for a timeout on connection in the TCP_SYN_RCVD state.
                * On such timeouts, we would normally resend the SYNACK until
                * the ACK is received, completing the 3-way handshake.  But if

Reply via email to