casaroli opened a new pull request, #22144: URL: https://github.com/apache/nuttx/pull/22144
## Summary This PR fixes four defects of TCP in `net/tcp`. Each fix is one commit; fixes 3 and 4 are each followed by a documentation commit. 1. **`net/tcp: Do not send received data back with the ACK.`** `tcp_input()` does not clear `dev->d_sndlen` on the paths for data in FIN_WAIT_1 and FIN_WAIT_2 and for a FIN in ESTABLISHED. Over the loopback device it still holds the length of the input segment, so `tcp_appsend()` sends that many bytes of the input buffer back to the peer as new data, after our FIN. A client sends 100 bytes, calls `shutdown(SHUT_WR)` and reads a 16 KiB answer: the transfer never ends (176496 segments in 60 s), and the server stays in CLOSE_WAIT. Now `tcp_input()` clears `d_sndlen` where it sets the length of the received data, as `udp_input()` does. 2. **`net/tcp: Do not answer a segment without data after a gap with an ACK.`** `tcp_input()` answers every segment after `rcvseq` with an ACK and drops it, also an ACK or a window update without data. RFC 9293, 3.10.7.4, processes such a segment and does not answer it; the immediate ACK of RFC 5681, 4.2, is for data after a gap. When two ends each lost a segment of the other, they send ACKs to each other without end, and a window update after a gap is lost until the next zero window probe. Now only data, SYN or FIN after a gap gets the immediate ACK. 3. **`net/tcp: Do not send the first SYN as a retransmission.`** The first SYN of `connect()` goes out through the timeout branch of `tcp_timer()`, which counts a retransmission and, since 872bff9cae4, doubles the RTO first. A lost first SYN is sent again after 3 s instead of 1.5 s. Now `TCP_SYNFIRST` marks the first SYN, and `TCP_MAXSYNRTX` counts only the resends, as `TCP_MAXRTX` does for data. 4. **`net/tcp: End a blocking connect() at the send timeout.`** A blocking `connect()` waits for the handshake about 70 s, whatever `SO_SNDTIMEO` says. Now it waits at most `SO_SNDTIMEO`, then gives `EINPROGRESS` and the connection goes on as for a non-blocking `connect()`, as on Linux (socket(7)). ## Impact `net/tcp` only (`tcp_input.c`, `tcp_timer.c`, `tcp_conn.c`, `tcp_connect.c`, `tcp_monitor.c`, `tcp.h`). No API, ABI or Kconfig change. Common network code: the same in flat, protected and kernel builds and on every architecture. - Fix 2: a duplicate ACK still counts for a fast retransmission, and every segment after a gap still limits the receive window (`tcp_recvwindow_loss()`), as before. - Fix 3: a `connect()` to a silent peer sends 6 SYNs instead of 5 (1.5, 3, 6, ... s apart) and gives `ETIMEDOUT` after 70.5 s instead of 69 s. The `rexmit` statistic no longer counts the first SYN. - Fix 4: only sockets with `SO_SNDTIMEO`. A program that handles `EINPROGRESS` as for a non-blocking socket works; one that closes the socket on it ends the attempt, as on Linux. ## Testing qemu-armv8a:knsh (kernel build) and qemu-armv8a:nsh (flat build) under HVF (Apple M-series), 1 and 4 CPUs (`CONFIG_SMP`), TCP write buffers, `NET_TCPBACKLOG`, `NET_SOCKOPTS`. Fixes 2 to 4 use a TUN device (`NET_TUN_PKTSIZE` 1500) and the peer that the test plays on it. | Test | master | this PR | |---|---|---| | `test_tcp_halfclose_answer` (apache/nuttx-apps#APPS): request, `shutdown(SHUT_WR)`, 16 KiB answer over loopback | the answer never ends, 3 of 3 | PASS 3 of 3 | | `ackahead`: the TUN peer sends 20 ACKs ahead of `rcvseq`, then opens the window | 20 answers; the data comes 4.0 s after the window update | at most 2 answers; the data at once | | `synrto`: the TUN peer drops the first SYN | first resend after 3.0 s | after 1.5 s | | `syngiveup`: a silent peer | 5 SYNs, `ETIMEDOUT` after 69.0 s | 6 SYNs, `ETIMEDOUT` after 70.5 s | | `connect()` with `SO_SNDTIMEO` 1 s, the TUN peer answers after 2.5 s | returns 0 at 3.0 s | `EINPROGRESS` after 1.0 s, then `POLLOUT`, `SO_ERROR` 0, data reaches the peer | All of the above again on this branch, on master 5814b404c95, knsh with 4 CPUs and flat nsh: every case fails on master and passes with this PR. No other case of `cmocka_net_tcp`, `cmocka_net_udp` and `cmocka_net_tun` changes. In flat nsh `test_tcp_reset_send` fails on master and with this PR alike: the `SIGPIPE` goes to another thread of the process. `test_tun_tcp_lossy` needs #22132. The TUN cases come to apache/nuttx-apps when the TUN test PR apache/nuttx-apps#4101 merges. Builds in the NuttX CI image (`EXTRAFLAGS="-Wno-cpp -Werror"`): qemu-armv8a:netnsh (Make and CMake), rv-virt:pnsh64 (protected), rv-virt:knetnsh64 (kernel), sim:tcpblaster, lm3s6432-s2e:nsh, qemu-intel64:nsh_pci: 0 warnings. Text size: +20 to +128 bytes per fix. `tools/checkpatch.sh -c -u -m -g` passes. Documentation: the commit after fix 3 adds the first SYN to the retransmission timer part of `Documentation/os/networking/tcp_state_machine.rst`; the commit after fix 4 adds the `SO_SNDTIMEO` case to `EINPROGRESS` of `connect()` in `Documentation/reference/user/11_network.rst`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
