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

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


The following commit(s) were added to refs/heads/master by this push:
     new df967c629 testing/nettest: Add a TUN case that acknowledges a part of 
the data.
df967c629 is described below

commit df967c629557af45ecc4bdbd0937336343604a7b
Author: Marco Casaroli <[email protected]>
AuthorDate: Sun Nov 22 17:57:05 2026 +0100

    testing/nettest: Add a TUN case that acknowledges a part of the data.
    
    cmocka_net_tun gets a second case, test_tun_tcp_partack.  The peer
    sends an MSS of 256 bytes and opens a window of two segments.  It
    ignores the first transmission of each segment.  Of the segments that
    come again after a timeout, it keeps only the first one, and
    acknowledges it when no segment comes for 100 ms.  So at each timeout
    the peer acknowledges one segment, but not all the data sent: the
    connection makes progress at each timeout.  After TCP_MAXRTX + 2 rounds
    the window opens.  The server must send every byte at its own sequence
    number, the whole stream and the FIN, and no RST.
    
    With TCP write buffers, NuttX set the retry count of the connection to
    zero only when an ACK covered the last segment sent.  The count grew at
    each timeout: the timeout doubled at each round, and the connection was
    reset after TCP_MAXRTX timeouts.
    
    The peer of the first case sends an MSS of 1460, as before.  The two
    cases take about one minute.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 testing/nettest/Kconfig            |  7 ++--
 testing/nettest/tun/test_tun.c     |  3 ++
 testing/nettest/tun/test_tun.h     |  6 +++
 testing/nettest/tun/test_tun_tcp.c | 79 +++++++++++++++++++++++++++++++++++++-
 4 files changed, 90 insertions(+), 5 deletions(-)

diff --git a/testing/nettest/Kconfig b/testing/nettest/Kconfig
index 17567c222..31992d72d 100644
--- a/testing/nettest/Kconfig
+++ b/testing/nettest/Kconfig
@@ -137,10 +137,11 @@ config TESTING_NET_TUN
        default y
        ---help---
                Test TCP against a peer that the program plays on a TUN device
-               (cmocka_net_tun).  The link loses the first transmission of each
-               segment, and the peer acknowledges each retransmission, so the
+               (cmocka_net_tun).  The link loses segments, and the peer
+               acknowledges a part of the data at each timeout, so the
                connection makes progress at each timeout.  The server must send
                the whole stream with each byte at its own sequence number, and
-               must not reset the connection.  The case takes about 30 seconds.
+               must not reset the connection.  The two cases take about one
+               minute.
 
 endif
diff --git a/testing/nettest/tun/test_tun.c b/testing/nettest/tun/test_tun.c
index ac45b9d1f..f5f784c52 100644
--- a/testing/nettest/tun/test_tun.c
+++ b/testing/nettest/tun/test_tun.c
@@ -43,6 +43,9 @@ int main(int argc, FAR char *argv[])
       cmocka_unit_test_setup_teardown(test_tun_tcp_lossy,
                                       test_tun_tcp_setup,
                                       test_tun_tcp_teardown),
+      cmocka_unit_test_setup_teardown(test_tun_tcp_partack,
+                                      test_tun_tcp_setup,
+                                      test_tun_tcp_teardown),
     };
 
   return cmocka_run_group_tests(tun_tests, NULL, NULL);
diff --git a/testing/nettest/tun/test_tun.h b/testing/nettest/tun/test_tun.h
index 84883b7de..c5b4d48ac 100644
--- a/testing/nettest/tun/test_tun.h
+++ b/testing/nettest/tun/test_tun.h
@@ -45,4 +45,10 @@ int test_tun_tcp_teardown(FAR void **state);
 
 void test_tun_tcp_lossy(FAR void **state);
 
+/****************************************************************************
+ * Name: test_tun_tcp_partack
+ ****************************************************************************/
+
+void test_tun_tcp_partack(FAR void **state);
+
 #endif /* __APPS_TESTING_NETTEST_TUN_TEST_TUN_H */
diff --git a/testing/nettest/tun/test_tun_tcp.c 
b/testing/nettest/tun/test_tun_tcp.c
index 294e3858c..e2cca51b7 100644
--- a/testing/nettest/tun/test_tun_tcp.c
+++ b/testing/nettest/tun/test_tun_tcp.c
@@ -58,8 +58,11 @@
 #define TUN_ADDR_P     0x0a4e0002  /* 10.78.0.2: the peer */
 #define TUN_PORT_A     5025
 #define TUN_ISN_P      0x52300000u
-#define TUN_TOTAL      8192        /* Bytes that the server writes */
 #define TUN_PKTSIZE    CONFIG_NET_TUN_PKTSIZE
+#define TUN_MSS        1460        /* MSS of the peer */
+#define TUN_PMSS       256         /* MSS of the peer in the partack case */
+#define TUN_WSEGS      2           /* Its window, in segments */
+#define TUN_QUIET_MS   100         /* No segment this long: the burst ended */
 
 #ifdef CONFIG_NET_TCP_RTO
 #  define TUN_RTO_MS   (CONFIG_NET_TCP_RTO * 500)
@@ -81,6 +84,14 @@
 
 #define TUN_ROUNDS_MS  (TUN_ROUNDS * 4 * TUN_RTO_MS)
 
+/* Bytes that the server writes: more than the rounds take */
+
+#if (TUN_ROUNDS + TUN_WSEGS + 1) * TUN_PMSS > 8192
+#  define TUN_TOTAL    ((TUN_ROUNDS + TUN_WSEGS + 1) * TUN_PMSS)
+#else
+#  define TUN_TOTAL    8192
+#endif
+
 #define TCP_FIN        0x01
 #define TCP_SYN        0x02
 #define TCP_RST        0x04
@@ -110,6 +121,7 @@ struct tun_state_s
   uint32_t  pisn;           /* The peer's ISN */
   uint32_t  isn;            /* The server's ISN */
   uint32_t  pseq;           /* The peer's next sequence number */
+  uint16_t  pmss;           /* The MSS of the peer's SYN */
   bool      synack;         /* The SYN-ACK came: isn is valid */
   int       mss;            /* The MSS of the server's SYN-ACK */
   bool      fin;
@@ -232,7 +244,7 @@ static void tun_send_tcp(FAR struct tun_state_s *st, 
uint32_t ack,
     {
       tcp[20] = 2;              /* MSS option */
       tcp[21] = 4;
-      tun_put16(tcp + 22, 1460);
+      tun_put16(tcp + 22, st->pmss);
     }
 
   /* The pseudo header: addresses, protocol and TCP length */
@@ -665,6 +677,7 @@ int test_tun_tcp_setup(FAR void **state)
   memset(st, 0, sizeof(*st));
   st->fd   = -1;
   st->sent = -1;
+  st->pmss = TUN_MSS;
   *state   = st;
 
   /* No assert here: cmocka does not run the teardown after a failed
@@ -780,3 +793,65 @@ void test_tun_tcp_lossy(FAR void **state)
   tun_finish(st);
   tun_check(st, "test_tun_tcp_lossy", rounds);
 }
+
+/****************************************************************************
+ * Name: test_tun_tcp_partack
+ *
+ * Description:
+ *   The peer sends an MSS of TUN_PMSS bytes and opens a window of
+ *   TUN_WSEGS segments.  It ignores the first transmission of each
+ *   segment.  Of the segments that come again after a timeout, it keeps
+ *   only the first one, and acknowledges it when no segment comes for
+ *   TUN_QUIET_MS.  So at each timeout the peer acknowledges one segment,
+ *   but not all the data sent: the connection makes progress at each
+ *   timeout.  After TUN_ROUNDS rounds (more than TCP_MAXRTX), the window
+ *   opens.  The server must send every byte at its own sequence number,
+ *   the whole stream and the FIN, and no RST.
+ *
+ ****************************************************************************/
+
+void test_tun_tcp_partack(FAR void **state)
+{
+  FAR struct tun_state_s *st = *state;
+  struct tun_seg_s seg;
+  uint32_t lastack = 0;
+  uint32_t end;
+  bool again;
+  int pending = 0;
+  int rounds = 0;
+
+  st->pmss = TUN_PMSS;
+  tun_handshake(st);
+  tun_send_tcp(st, st->isn + 1, TCP_ACK, TUN_WSEGS * TUN_PMSS);
+
+  end = tun_now_ms() + TUN_ROUNDS_MS;
+  while (rounds < TUN_ROUNDS && !st->rst && st->wrong == 0 &&
+         (int32_t)(end - tun_now_ms()) > 0)
+    {
+      if (tun_recv_seg(st, &seg, TUN_QUIET_MS, &again) == 1)
+        {
+          /* Keep the first segment that comes again at the next byte to
+           * acknowledge.  The other segments are lost.
+           */
+
+          if (pending == 0 && seg.len > 0 && again &&
+              seg.seq == st->isn + 1 + lastack)
+            {
+              pending = seg.len;
+            }
+        }
+      else if (pending > 0)
+        {
+          /* The retransmissions of this timeout ended */
+
+          lastack += pending;
+          pending  = 0;
+          rounds++;
+          tun_send_tcp(st, st->isn + 1 + lastack, TCP_ACK,
+                       TUN_WSEGS * TUN_PMSS);
+        }
+    }
+
+  tun_finish(st);
+  tun_check(st, "test_tun_tcp_partack", rounds);
+}

Reply via email to