Repository navigation
net/tcp: Do not free a write buffer before its data is acknowledged. - #22132
Conversation
At the TCP_MAXRTX retransmission of a write buffer, psock_send_eventhandler() freed the write buffer and its data, and the connection stayed open until the next timeout of tcp_timer(). The data of the next write buffer then went out at the sequence numbers of the freed data: the peer received wrong data without an error, and then a reset. The count of a write buffer also increases while the peer acknowledges a part of it at each timeout. Thus a connection that made progress at each timeout lost data after TCP_MAXRTX timeouts. Since a91c502, tcp_timer() also closes a connection with write buffers when the retry count of the connection (conn->nrtx) reaches TCP_MAXRTX: it sends a RST and gives TCP_TIMEDOUT to the socket. An ACK of all the data sent sets that count to zero. Remove the expiry of a write buffer and conn->expired. A write buffer now stays until its data is acknowledged or the connection is closed, as without write buffers. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
|
cederom
left a comment
There was a problem hiding this comment.
Thank you @casaroli :-)
-
Description vs. behaviour: with write buffers,
conn->nrtxis only reset when the ACK covers everything sent (TCP_SEQ_LTE(sndseq, ackseq)intcp_input.c). A peer that ACKs part of the window at each timeout (say, the first of two segments) never resets it, so the connection is still reset afterTCP_MAXRTXtimeouts while making progress. No data is lost any more, which is the important part, but the description says the progressing case is fixed. Could you either narrow that wording, or resetnrtxwheneversnd_unaadvances? RFC 1122 4.2.3.5 counts R2 as retransmissions of the same segment. A second case in apache/nuttx-apps#4101 with a window of two segments, losing the second each time, would cover it. -
The moved lines keep the old typo "the number of bytes sent sent" (in both places); worth fixing while they're touched.
-
The REXMIT block at the head of
write_qnow duplicates the first half ofretransmit_segment(). A small helper for "count + reset sent" would remove the duplication (fine as a follow-up). -
Pre-existing, not this PR: in
tcp_input.c,conn->nrtx = 0runs just beforeif (conn->nrtx == 0)gates the RTT estimate, so an ACK of a retransmitted segment produces an RTT sample (against Karn's rule). Maybe worth an issue.
Summary
With
CONFIG_NET_TCP_WRITE_BUFFERS,psock_send_eventhandler()freed a write buffer at itsTCP_MAXRTXretransmission, with its data, and only counted it inconn->expired. The connection stayed open until the next timeout oftcp_timer(). Meanwhile the next write buffer took its sequence number fromconn->sent, which still counted the sent part of the freed buffer. So its data went out at the sequence numbers of the freed data: the peer received wrong data without an error, and then a reset.The count of a write buffer also grew while the peer acknowledged a part of it at each timeout. So a connection that made progress at each timeout was reset after
TCP_MAXRTXtimeouts, and lost data.Since a91c502,
tcp_timer()also closes a connection with write buffers when the retry count of the connection (conn->nrtx) reachesTCP_MAXRTX: it sends a RST and givesTCP_TIMEDOUT(ETIMEDOUT) to the socket. An ACK of all the data sent sets that count to zero. This PR removes the expiry of a write buffer andconn->expired. A write buffer now stays until its data is acknowledged or the connection is closed, as without write buffers.Two examples on master:
tcpechod) with 256 IOBs andIOB_THROTTLE16: the server freed 3 write buffers 1.85 s after boot and sent the data of offset 49152 at offset 46844. The client got 26 segments of wrong data, then a reset.Impact
TCP with
CONFIG_NET_TCP_WRITE_BUFFERSonly (net/tcp/tcp_send_buffered.c,tcp_timer.c,tcp_conn.c,tcp.h). No API, ABI or Kconfig change;conn->expiredwas internal.Behaviour:
TCP_MAXRTXtimeouts, at the same time as before, with a RST andETIMEDOUT. It now also retransmits at the eighth timeout.wb_nrtx) stays for the coalescing check and the debug output; it now stops at 255.Build modes and architectures: common network code, the same in flat, protected and kernel builds and on every architecture. With and without
NET_TCP_FAST_RETRANSMIT,NET_TCP_SELECTIVE_ACK,NET_TCP_CC_NEWRENO,NET_SENDFILE: see the builds below.Testing
qemu-armv8a:knsh (kernel build) and qemu-armv8a:nsh (flat build) under HVF (Apple M-series), 96 IOBs of 196 bytes,
IOB_THROTTLE8, write buffers, a TUN device (NET_TUN_PKTSIZE1500). Master is 5e4ecd1.cmocka_net_tun(testing/nettest: Add a TCP case with a lossy peer on a TUN device. nuttx-apps#4101): a TCP peer that the test plays on the TUN device. The link loses the first transmission of each segment; the peer acknowledges each retransmission, so the connection makes progress at each timeout. After 10 rounds the window opens. The case checks each byte against its sequence number, the whole stream, the FIN, and no RST.wrbexp: the same peer as a standalone program, with two more modes:silent(the peer acknowledges nothing) andpartial(the case above).IOB_THROTTLE16, write buffers): an echo client on node A sends 256 KiB or 1 MiB totcpechodon node B while it reads the echo. A capture on the TAP device of A, checked against the stream.cmocka_net_tun, knsh 1 CPU, 3 runs in one bootcmocka_net_tun, knsh 4 CPUs (CONFIG_SMP)cmocka_net_tun, flat nshwrbexp partial, knsh (CONFIG_DEBUG_NET_WARN)wrbexp silentrecv()givesETIMEDOUTETIMEDOUTENOTCONNinsend()), the client gets a resetcmocka_net_tcp, knsh 1 and 4 CPUs, flat nshtest_tcp_connect_ipv4fails)ostest, knsh 1 and 4 CPUs, flat nshTCP over 127.0.0.1 in this configuration (96 IOBs),
tcplomin(8 MiB from one thread to another): on master the write buffers of the sender hold the pool, the receiver drops the data below the IOB throttle, the sender frees 3 write buffers ("Expiring wrb") at the eighth timeout, the connection is reset, andsend()givesENOTCONNafter 46676 bytes. With this PR the sender keeps its data and retransmits; some retransmissions get through, so the connection does not time out, but it moves about one segment per timeout (71 timeouts in 105 s; the shell stays responsive). The same with #22078. With #22089 (no drop of the data of a peer on this host) and this PR:tcplominandtcplobulk8 MiB PASS (79.7 MiB/s),tcpthruloopback 20 MiB PASS,cmocka_net_tunPASS,osteststatus 0; #22089 alone gives the same numbers, butcmocka_net_tunfails.tcplobulkwith a slow reader fails the time limit with #22089, with and without this PR.In the two-node runs both nodes drop the data of the other below the IOB throttle, so the server's connection makes no progress and times out on master and with this PR; this PR removes only the wrong data. The 8 timeouts come in less than one second because a segment with data that repeats the ACK number makes the fast retransmission code run
tcp_timer(), which counts each call as a timeout; #22078 removes that.Builds in the NuttX CI image (
EXTRAFLAGS="-Wno-cpp -Werror"), master and this PR: all OK, 0 warnings. Text size (bytes):NET_TCP_WRITE_BUFFERS,NET_SOLINGERNET_TCP_FAST_RETRANSMITtools/checkpatch.sh -c -u -m -gpasses. Documentation:Documentation/os/networking/tcp_state_machine.rstalready says that the connection is closed withTCP_TIMEDOUTand a RST when the retransmissions exceed the limit; no change.