tcp: 3 fixes for retrans_stamp and undo logic
Subsystems (3)
cover tcp: 3 fixes for retrans_stamp and undo logic
From: Neal Cardwell <[email protected]>
Geumhwan Yu <[email protected]> recently reported and diagnosed a regression in TCP loss recovery undo logic in the case where a TCP connection enters fast recovery, is unable to retransmit anything due to TSQ, and then receives an ACK allowing forward progress. The sender should be able to undo the spurious loss recovery in this case, but was not doing so. The first patch fixes this regression.
Running our suite of packetdrill tests with the first fix, the tests highlighted two other small bugs in the way retrans_stamp is updated in some rare corner cases. The second two patches fix those other two small bugs.
Thanks to Geumhwan Yu for the bug report!
Neal Cardwell (3):
tcp: fix to allow timestamp undo if no retransmits were sent
tcp: fix tcp_enter_recovery() to zero retrans_stamp when it's safe
tcp: fix TFO SYN_RECV to not zero retrans_stamp with retransmits out
net/ipv4/tcp_input.c | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-)
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
PATCH 1/3 tcp: fix to allow timestamp undo if no retransmits were sent
From: Neal Cardwell <[email protected]>
Fix the TCP loss recovery undo logic in tcp_packet_delayed() so that it can trigger undo even if TSQ prevents a fast recovery episode from reaching tcp_retransmit_skb().
Geumhwan Yu <[email protected]> recently reported that after this commit from 2019:
commit bc9f38c8328e ("tcp: avoid unconditional congestion window undo on SYN retransmit")
...and before this fix we could have buggy scenarios like the following:
+ Due to reordering, a TCP connection receives some SACKs and enters a spurious fast recovery.
+ TSQ prevents all invocations of tcp_retransmit_skb(), because many skbs are queued in lower layers of the sending machine's network stack; thus tp->retrans_stamp remains 0.
+ The connection receives a TCP timestamp ECR value echoing a timestamp before the fast recovery, indicating that the fast recovery was spurious.
+ The connection fails to undo the spurious fast recovery because tp->retrans_stamp is 0, and thus tcp_packet_delayed() returns false, due to the new logic in the 2019 commit: commit bc9f38c8328e ("tcp:
avoid unconditional congestion window undo on SYN retransmit")
This fix tweaks the logic to be more similar to the tcp_packet_delayed() logic before bc9f38c8328e, except that we take care not to be fooled by the FLAG_SYN_ACKED code path zeroing out tp->retrans_stamp (the bug noted and fixed by Yuchung in bc9f38c8328e).
Note that this returns the high-level behavior of tcp_packet_delayed() to again match the comment for the function, which says: "Nothing was retransmitted or returned timestamp is less than timestamp of the first retransmission." Note that this comment is in the original 2005-04-16 Linux git commit, so this is evidently long-standing behavior.
--- net/ipv4/tcp_input.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
net/ipv4/tcp_input.c +16 -2
@@ -2473,8 +2473,22 @@ static bool tcp_skb_spurious_retrans(const struct tcp_sock *tp,*/ static inline bool tcp_packet_delayed (const struct tcp_sock *tp) { -return tp->retrans_stamp && -tcp_tsopt_ecr_before (tp, tp->retrans_stamp); +const struct sock *sk = (const struct sock *)tp; + +if (tp->retrans_stamp && +tcp_tsopt_ecr_before (tp, tp->retrans_stamp)) +return true;/* got echoed TS before first retransmission */ + +/* Check if nothing was retransmitted (retrans_stamp==0), which may +* happen in fast recovery due to TSQ. But we ignore zero retrans_stamp +* in TCP_SYN_SENT, since when we set FLAG_SYN_ACKED we also clear +* retrans_stamp even if we had retransmitted the SYN. +*/ +if (!tp->retrans_stamp &&/* no record of a retransmit/SYN? */ + sk->sk_state !=TCP_SYN_SENT )/* not the FLAG_SYN_ACKED case? */ +return true;/* nothing was retransmitted */ + +return false; }/* Undo procedures. */
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
PATCH 2/3 tcp: fix tcp_enter_recovery() to zero retrans_stamp when it's safe
In reply to Neal CardwellFrom: Neal Cardwell <[email protected]>
Fix tcp_enter_recovery() so that if there are no retransmits out then we zero retrans_stamp when entering fast recovery. This is necessary to fix two buggy behaviors.
Currently a non-zero retrans_stamp value can persist across multiple back-to-back loss recovery episodes. This is because we generally only clears retrans_stamp if we are completely done with loss recoveries, and get to tcp_try_to_open() and find !tcp_any_retrans_done(sk). This behavior causes two bugs:
(1) When a loss recovery episode (CA_Loss or CA_Recovery) is followed immediately by a new CA_Recovery, the retrans_stamp value can persist and can be a time before this new CA_Recovery episode starts. That means that timestamp-based undo will be using the wrong retrans_stamp (a value that is too old) when comparing incoming TS ecr values to retrans_stamp to see if the current fast recovery episode can be undone.
(2) If there is a roughly minutes-long sequence of back-to-back fast recovery episodes, one after another (e.g. in a shallow-buffered or policed bottleneck), where each fast recovery successfully makes forward progress and recovers one window of sequence space (but leaves at least one retransmit in flight at the end of the recovery), followed by several RTOs, then the ETIMEDOUT check may be using the wrong retrans_stamp (a value set at the start of the first fast recovery in the sequence). This can cause a very premature ETIMEDOUT, killing the connection prematurely.
This commit changes the code to zero retrans_stamp when entering fast recovery, when this is known to be safe (no retransmits are out in the network). That ensures that when starting a fast recovery episode, and it is safe to do so, retrans_stamp is set when we send the fast retransmit packet. That addresses both bug (1) and bug (2) by ensuring that (if no retransmits are out when we start a fast recovery) we use the initial fast retransmit of this fast recovery as the time value for undo and ETIMEDOUT calculations.
This makes intuitive sense, since the start of a new fast recovery episode (in a scenario where no lost packets are out in the network) means that the connection has made forward progress since the last RTO or fast recovery, and we should thus "restart the clock" used for both undo and ETIMEDOUT logic.
Note that if when we start fast recovery there *are* retransmits out in the network, there can still be undesirable (1)/(2) issues. For example, after this patch we can still have the (1) and (2) problems in cases like this:
+ round 1: sender sends flight 1
+ round 2: sender receives SACKs and enters fast recovery 1, retransmits some packets in flight 1 and then sends some new data as flight 2
+ round 3: sender receives some SACKs for flight 2, notes losses, and retransmits some packets to fill the holes in flight 2
+ fast recovery has some lost retransmits in flight 1 and continues for one or more rounds sending retransmits for flight 1 and flight 2
+ fast recovery 1 completes when snd_una reaches high_seq at end of flight 1
+ there are still holes in the SACK scoreboard in flight 2, so we enter fast recovery 2, but some retransmits in the flight 2 sequence range are still in flight (retrans_out > 0), so we can't execute the new retrans_stamp=0 added here to clear retrans_stamp
It's not yet clear how to fix these remaining (1)/(2) issues in an efficient way without breaking undo behavior, given that retrans_stamp is currently used for undo and ETIMEDOUT. Perhaps the optimal (but expensive) strategy would be to set retrans_stamp to the timestamp of the earliest outstanding retransmit when entering fast recovery. But at least this commit makes things better.
Note that this does not change the semantics of retrans_stamp; it simply makes retrans_stamp accurate in some cases where it was not before:
(1) Some loss recovery, followed by an immediate entry into a fast recovery, where there are no retransmits out when entering the fast recovery.
(2) When a TFO server has a SYNACK retransmit that sets retrans_stamp, and then the ACK that completes the 3-way handshake has SACK blocks that trigger a fast recovery. In this case when entering fast recovery we want to zero out the retrans_stamp from the TFO SYNACK retransmit, and set the retrans_stamp based on the timestamp of the fast recovery.
We introduce a tcp_retrans_stamp_cleanup() helper, because this two-line sequence already appears in 3 places and is about to appear in 2 more as a result of this bug fix patch series. Once this bug fix patches series in the net branch makes it into the net-next branch we'll update the 3 other call sites to use the new helper.
This is a long-standing issue. The Fixes tag below is chosen to be the oldest commit at which the patch will apply cleanly, which is from Linux v3.5 in 2012.
--- net/ipv4/tcp_input.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
net/ipv4/tcp_input.c +13 -0
@@ -2522,6 +2522,16 @@ static bool tcp_any_retrans_done(const struct sock *sk)return false; } +/* If loss recovery is finished and there are no retransmits out in the +* network, then we clear retrans_stamp so that upon the next loss recovery +* retransmits_timed_out() and timestamp-undo are using the correct value. +*/ +static void tcp_retrans_stamp_cleanup (struct sock *sk) +{ +if (!tcp_any_retrans_done (sk)) +tcp_sk (sk)->retrans_stamp =0 ; +} +static void DBGUNDO (struct sock *sk,const char *msg) {#if FASTRETRANS_DEBUG >1
@@ -2889,6 +2899,9 @@ void tcp_enter_recovery(struct sock *sk, bool ece_ack)struct tcp_sock *tp =tcp_sk (sk);int mib_idx; +/* Start the clock with our fast retransmit, for undo and ETIMEDOUT. */ +tcp_retrans_stamp_cleanup (sk); +if (tcp_is_reno (tp)) mib_idx =LINUX_MIB_TCPRENORECOVERY ;else
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
PATCH 3/3 tcp: fix TFO SYN_RECV to not zero retrans_stamp with retransmits out
In reply to Neal CardwellFrom: Neal Cardwell <[email protected]>
Fix tcp_rcv_synrecv_state_fastopen() to not zero retrans_stamp if retransmits are outstanding.
tcp_fastopen_synack_timer() sets retrans_stamp, so typically we'll need to zero retrans_stamp here to prevent spurious retransmits_timed_out(). The logic to zero retrans_stamp is from this 2019 commit:
commit cd736d8b67fb ("tcp: fix retrans timestamp on passive Fast Open")
However, in the corner case where the ACK of our TFO SYNACK carried some SACK blocks that caused us to enter TCP_CA_Recovery then that non-zero retrans_stamp corresponds to the active fast recovery, and we need to leave retrans_stamp with its current non-zero value, for correct ETIMEDOUT and undo behavior.
--- net/ipv4/tcp_input.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
net/ipv4/tcp_input.c +9 -2
@@ -6684,10 +6684,17 @@ static void tcp_rcv_synrecv_state_fastopen(struct sock *sk)if (inet_csk (sk)->icsk_ca_state == TCP_CA_Loss && !tp->packets_out)tcp_try_undo_recovery (sk); -/* Reset rtx states to prevent spurious retransmits_timed_out() */ tcp_update_rto_time (tp); - tp->retrans_stamp =0 ;inet_csk (sk)->icsk_retransmits =0 ; +/* In tcp_fastopen_synack_timer() on the first SYNACK RTO we set +* retrans_stamp but don't enter CA_Loss, so in case that happened we +* need to zero retrans_stamp here to prevent spurious +* retransmits_timed_out(). However, if the ACK of our SYNACK caused us +* to enter CA_Recovery then we need to leave retrans_stamp as it was +* set entering CA_Recovery, for correct retransmits_timed_out() and +* undo behavior. +*/ +tcp_retrans_stamp_cleanup (sk);/* Once we leave TCP_SYN_RECV or TCP_FIN_WAIT_1, * we no longer need req so release it.
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 10 lines · On Tue, 1 Oct 2024 20:05:14 +0000 you wrote:
From: Neal Cardwell <[email protected]>
Geumhwan Yu <[email protected]> recently reported and diagnosed a regression in TCP loss recovery undo logic in the case where a TCP connection enters fast recovery, is unable to retransmit anything due to TSQ, and then receives an ACK allowing forward progress. The sender should be able to undo the spurious loss recovery in this case, but was not doing so. The first patch fixes this regression.
[...]
Here is the summary with links:
- [net,1/3] tcp: fix to allow timestamp undo if no retransmits were sent
https://git.kernel.org/netdev/net/c/e37ab7373696 - [net,2/3] tcp: fix tcp_enter_recovery() to zero retrans_stamp when it's safe https://git.kernel.org/netdev/net/c/b41b4cbd9655 - [net,3/3] tcp: fix TFO SYN_RECV to not zero retrans_stamp with retransmits out https://git.kernel.org/netdev/net/c/27c80efcc204
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html