Skip to content

testing/nettest: Add a TCP case with a lossy peer on a TUN device. - #4101

Merged
acassis merged 1 commit into
apache:masterfrom
casaroli:tcp-wrb-expiry-test-up
Nov 24, 2026
Merged

acassis merged 1 commit into
apache:masterfrom
casaroli:tcp-wrb-expiry-test-up

Conversation

@casaroli

@casaroli casaroli commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add cmocka_net_tun: a TCP case against a peer that the program plays on a TUN device (/dev/tun), so the case controls every segment that the peer sends.

test_tun_tcp_lossy: the link loses the first transmission of each segment. The peer opens a window of mss / 8 bytes, ignores a segment the first time and acknowledges it when it comes again. So each timeout of the server retransmits one segment, the peer acknowledges it, and the connection makes progress at each timeout. After CONFIG_NET_TCP_MAXRTX + 2 rounds the window opens and the peer acknowledges everything. The server writes 8192 bytes in one send(). The case checks that each byte comes at its own sequence number, that the whole stream and the FIN come, and that the server does not reset the connection. The time limits come from CONFIG_NET_TCP_RTO. The case takes about 16 s.

With TCP write buffers, NuttX freed a write buffer at its eighth retransmission, also when the peer had acknowledged a part of it at each timeout. The next data then went out at the sequence numbers of the freed data, and the next timeout reset the connection. apache/nuttx#22132 fixed that.

Impact

New program, CONFIG_TESTING_NET_TUN (default y when NET_TUN, NET_TCP, NET_IPv4, NET_TCPBACKLOG and NET_SOCKOPTS are set and pthreads are enabled). No other test changes. Uses the addresses 10.78.0.1/24 (the TUN interface) and 10.78.0.2 (the peer). No CI configuration enables both NET_TUN and TESTING_NET_TEST yet, so CI does not build or run the case.

Testing

qemu-armv8a:knsh (kernel build) and qemu-armv8a:nsh (flat build) under HVF (Apple M-series), 96 IOBs of 196 bytes, IOB_THROTTLE 8, TCP write buffers, NET_TUN_PKTSIZE 1500; 3 runs of cmocka_net_tun in one boot.

Configuration nuttx Result
knsh, 1 CPU master 1d494c0e787 (with apache/nuttx#22132) PASS 3 of 3, 15 to 16 s each
knsh, 1 CPU, 4 CPUs (CONFIG_SMP), flat nsh master 1d494c0e787 PASS 3 of 3 each (the same case, run with a second case on top)
knsh, 1 CPU c51b1af1ee2 (before apache/nuttx#22132) FAIL 3 of 3: RST after 7 rounds

Builds in the NuttX CI image (EXTRAFLAGS="-Wno-cpp -Werror"), all OK, 0 compiler warnings, cmocka_net_tun built: qemu-armv8a:netnsh + TCP write buffers + NET_TUN (Make, CMake, without write buffers, with NET_IPv6, with the default NET_TUN_PKTSIZE), sim:tcpblaster (NET_TUN is on there).

tools/checkpatch.sh -c -u -m -g passes.

@cederom

cederom commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Thanks, nice regression test. It hits the bug well: the 10 rounds of mss/8 all stay inside one write buffer (4 × mss), and each retransmission is fully ACKed, so nrtx goes back to 0 with #22132. A few things before merge:

Blocking

  1. Kconfig (L126): please add NET_TCPBACKLOG, NET_SOCKOPTS and !DISABLE_PTHREAD. Without a backlog, the final ACK of the handshake gets an RST if accept() isn't waiting yet. listening = 1 is set before accept(), so on SMP this can latch st->rst and fail the test even with the fix.
  2. Idle nudge in the lossy loop (L605–610): it ACKs tun_contiguous(), which includes the "lost" first transmission, and advertises 8192 before the next ACK shrinks the window back to mss/8. With NET_TCP_RTO >= 4 or after a backoff, it fires every round and the case tests something else. Suggest tun_send_tcp(st, st->isn + 1 + lastack, TCP_ACK, wnd);. Or is the nudge needed there at all?

Should fix

  1. L62: TUN_ROUNDS assumes TCP_MAXRTX = 8, but it's configurable. Use (CONFIG_NET_TCP_MAXRTX + 2). TUN_IDLE_MS and the 60 s budget also assume the default NET_TCP_RTO.
  2. L379–382, L418–419: setsockopt() return values aren't checked. If the timeouts don't apply, accept()/send() can block forever. Record the failure the way the bind/listen path does.
  3. L633–636: volatile doesn't order the sent/done stores on weakly ordered SMP (e.g. the 4-CPU armv8a runs). Please pthread_join() here before asserting on st->sent, and skip the join in teardown.

Nits

  • L64: use CONFIG_NET_TUN_PKTSIZE. On a too-small buffer, tun_read() returns -EINVAL and keeps the packet, so the read loop spins on it.
  • L579: assert st->mss > 0. If the MSS option isn't the first option, the window silently becomes 1 byte.
  • L323/L578: isn == 0 as the "no handshake yet" sentinel excludes a valid ISN.
  • L481–490: if an assert in setup fails, cmocka skips teardown, so the fd and the thread leak.
  • A defconfig with NET_TUN + nettest would let CI run this.

cmocka_net_tun plays a TCP peer on a TUN device.  The link loses the
first transmission of each segment: the peer opens a window of mss / 8
bytes, ignores a segment the first time and acknowledges it when it
comes again.  So each timeout of the server retransmits one segment,
the peer acknowledges it, and the connection makes progress at each
timeout.  After TCP_MAXRTX + 2 such rounds the window opens.  The server
writes 8192 bytes in one send(): the case checks that each byte comes at
its own sequence number, that the whole stream and the FIN come, and
that the server does not reset the connection.  The time limits come
from CONFIG_NET_TCP_RTO.

With TCP write buffers, NuttX freed a write buffer at its eighth
retransmission, also when the peer had acknowledged a part of it at
each timeout, and reset the connection at the next timeout.  Data sent
in between went out at the sequence numbers of the freed data.

The case takes about 16 seconds.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@casaroli

Copy link
Copy Markdown
Contributor Author

Done.

@casaroli
casaroli marked this pull request as ready for review November 22, 2026 18:52
@acassis
acassis merged commit 8f9d183 into apache:master Nov 24, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants