Repository navigation
net/tcp: Fix data sent back after a FIN, ACKs after a gap, and the timing of connect(). - #22144
Conversation
|
|
Commit 1 ( Commit 2 (
Commit 3 (
Commit 5 (
Doc commits ( |
tcp_input() did not clear dev->d_sndlen, the length of the data that a connection callback queues for the response. It still holds the length of the data that the device sent last: over the loopback device, the length of the segment that tcp_input() now handles. The paths for ESTABLISHED, SYN_RCVD, SYN_SENT and CLOSE_WAIT clear it before they call the callbacks; the paths for data in FIN_WAIT_1 and FIN_WAIT_2 and for a FIN in ESTABLISHED do not. tcp_appsend() then sent that many bytes of the input buffer, the data that just came from the peer, back to the peer as new data, after our FIN. A client that shuts down writing and then reads the answer of the server sent each segment of the answer back. Its sequence numbers then went beyond its FIN: the server dropped its ACKs, sent the last segment of the answer again at each retransmission timeout, and stayed in CLOSE_WAIT with that segment in its write buffers; the client never got the FIN of the server. Clear d_sndlen where tcp_input() sets the length of the received data, as udp_input() does. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
0ac64ef ("net/tcp: Do not answer an empty segment in the receive window") stops the ACK that tcp_input() gave to a segment without data, SYN or FIN in the receive window after the next expected sequence number: an ACK or a window update of a peer whose segment was lost. But it gives only a new ACK to the connection and returns before the window update and the state machine. RFC 9293, section 3.10.7.4, processes such a segment in full. So the window that such a segment opens is not used: the sender waits for its next zero window probe. An ACK of our FIN in such a segment does not move FIN_WAIT_1 to FIN_WAIT_2, and in SYN_RCVD an ACK of our SYN-ACK does not complete the handshake. Such a segment now goes on to the window update and the state machine, as a segment at the next expected sequence number does. It still gets no ACK. A segment at or after rcv_adv, the right edge of the window that we advertised, is not in the window: it still gets the ACK and is dropped, so it cannot move snd_wl1 ahead of the peer. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
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 872bff9 ("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 <marco.casaroli@gmail.com>
The first SYN of connect() goes out with the initial retransmission timeout and does not count as a retransmission. NET_TCP_MAXSYNRTX limits the resends of the SYN. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
psock_tcp_connect() waits for the handshake without a time limit. A blocking connect() to a peer that drops the SYNs waits until the SYN retransmissions give up, about 70 s, whatever SO_SNDTIMEO says. Linux ends the wait at the send timeout with EINPROGRESS, as if the socket were non-blocking (socket(7), SO_SNDTIMEO), and the connection goes on. Do the same: wait at most SO_SNDTIMEO. When it expires, connect() gives EINPROGRESS and the connection monitor takes the connection over, as for a non-blocking connect(): poll() reports POLLOUT when the handshake completes, and SO_ERROR gives the error of a connection that fails. tcp_start_monitor() takes a connection in TCP_SYN_SENT as one in progress when the socket is connecting, not only when it is non-blocking. A result that the event handler sets just before the wait ends (with the timeout or a signal) is now used, as the handler already tore its callback down. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
A blocking connect() gives EINPROGRESS when the send timeout of the socket expires before the handshake completes. The connection goes on. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
f5f3cef to
0a7c8e2
Compare
|
Done. Commit 2 is now on top of #22137, which merged today: it keeps the check of the right edge there ( |
|
Re-reviewed the new head (range-diff against the previous one). Resolved
Remaining
nxstyle is clean on the changed files. With the rebase and the Impact note, LGTM. |
|
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.net/tcp: Do not send received data back with the ACK.tcp_input()does not cleardev->d_sndlenon 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, sotcp_appsend()sends that many bytes of the input buffer back to the peer as new data, after our FIN. A client sends 100 bytes, callsshutdown(SHUT_WR)and reads a 16 KiB answer: the transfer never ends (176496 segments in 60 s), and the server stays in CLOSE_WAIT. Nowtcp_input()clearsd_sndlenwhere it sets the length of the received data, asudp_input()does.net/tcp: Process a segment without data after a gap.net/tcp: Do not answer an empty segment in the receive window #22137 stops the ACK to a segment without data, SYN or FIN in the receive window after a gap (RFC 9293, 3.10.7.4), but it gives only a new ACK to the connection and returns before the window update and the state machine. So the window that such a segment opens is not used until the next zero window probe, an ACK of our FIN does not move FIN_WAIT_1 to FIN_WAIT_2, and in SYN_RCVD an ACK of our SYN-ACK does not complete the handshake. Now such a segment goes on to the window update and the state machine, as a segment atrcvseqdoes, and still gets no ACK. A segment at or afterrcv_adv(the right edge) still gets the ACK and is dropped.net/tcp: Do not send the first SYN as a retransmission.The first SYN ofconnect()goes out through the timeout branch oftcp_timer(), which counts a retransmission and, since 872bff9, doubles the RTO first. A lost first SYN is sent again after 3 s instead of 1.5 s. NowTCP_SYNFIRSTmarks the first SYN, andTCP_MAXSYNRTXcounts only the resends, asTCP_MAXRTXdoes for data.NET_TCP_MAXSYNRTXgets a help text that says so.net/tcp: End a blocking connect() at the send timeout.A blockingconnect()waits for the handshake about 70 s, whateverSO_SNDTIMEOsays. Now it waits at mostSO_SNDTIMEO, then givesEINPROGRESSand the connection goes on as for a non-blockingconnect(), as on Linux (socket(7)).Impact
net/tcponly (tcp_input.c,tcp_timer.c,tcp_conn.c,tcp_connect.c,tcp_monitor.c,tcp.h,Kconfig). No API or ABI change, no new Kconfig option. Common network code: the same in flat, protected and kernel builds and on every architecture.connect()to a silent peer sends 6 SYNs instead of 5 (1.5, 3, 6, ... s apart) and givesETIMEDOUTafter 70.5 s instead of 69 s. Therexmitstatistic no longer counts the first SYN.SO_SNDTIMEO. A program that handlesEINPROGRESSas for a non-blocking socket works; one that closes the socket on it ends the attempt, as on Linux. These setSO_SNDTIMEObefore a blockingconnect()and take any error as fatal, so a handshake slower than the timeout now fails withEINPROGRESS: in nuttx-appsnetutils/webclient/webclient.c(its configured timeout) andsystem/nxplayer/nxplayer.c(10 s); in NuttX the DNS client over TCP (libs/libc/netdb/lib_dnsbind.c, 5 s, doubled at each retry: the query is tried again).testing/nettesttest_tcp_connect_ipv4and_ipv6set 10 ms before aconnect()over the loopback device; the handshake completes before it (below).connect()while the handshake runs givesEISCONN(Linux:EALREADY), as it did before for a non-blocking socket.Testing
qemu-armv8a:knsh (kernel build, 4 CPUs) and qemu-armv8a:nsh (flat build, 1 CPU) under HVF (Apple M-series), TCP write buffers,
NET_TCPBACKLOG,NET_SOCKOPTS, on master c51b1af. Fixes 2 to 4 use a TUN device (NET_TUN_PKTSIZE1500) and the peer that the test plays on it.test_tcp_halfclose_answer(apache/nuttx-apps#4105): request,shutdown(SHUT_WR), 16 KiB answer over loopbackackahead: the TUN peer sends 20 ACKs ahead ofrcvseq, then opens the windowwndedge: the TUN peer sends segments without data at and after the right edge of the window, then readssynrto: the TUN peer drops the first SYNsyngiveup: a silent peerETIMEDOUTafter 69.0 sETIMEDOUTafter 70.5 sconnect()withSO_SNDTIMEO1 s, the TUN peer answers after 2.5 sEINPROGRESSafter 1.0 s, thenPOLLOUT,SO_ERROR0, data reaches the peerThe TUN cases run 3 times in each boot. No other case of
cmocka_net_tcp,cmocka_net_udpandcmocka_net_tunchanges. In flat nshtest_tcp_reset_sendfails on master and with this PR alike: theSIGPIPEgoes to another thread of the process.test_tun_tcp_lossyneeds #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") of these fixes on master 5814b40, before the rebase on #22137: 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 -gpasses. Documentation: the commit after fix 3 adds the first SYN to the retransmission timer part ofDocumentation/os/networking/tcp_state_machine.rst; the commit after fix 4 adds theSO_SNDTIMEOcase toEINPROGRESSofconnect()inDocumentation/reference/user/11_network.rst.