xref: /linux/net/ipv4/tcp_recovery.c (revision a657db0350bb8f568897835b6189c84a89f13292)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2659a8ad5SYuchung Cheng #include <linux/tcp.h>
3659a8ad5SYuchung Cheng #include <net/tcp.h>
4659a8ad5SYuchung Cheng 
59a568de4SEric Dumazet static bool tcp_rack_sent_after(u64 t1, u64 t2, u32 seq1, u32 seq2)
61d0833dfSYuchung Cheng {
79a568de4SEric Dumazet 	return t1 > t2 || (t1 == t2 && after(seq1, seq2));
81d0833dfSYuchung Cheng }
91d0833dfSYuchung Cheng 
101f7455c3Skbuild test robot static u32 tcp_rack_reo_wnd(const struct sock *sk)
1120b654dfSYuchung Cheng {
1220b654dfSYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
1320b654dfSYuchung Cheng 
147ec65372SWei Wang 	if (!tp->reord_seen) {
1520b654dfSYuchung Cheng 		/* If reordering has not been observed, be aggressive during
1620b654dfSYuchung Cheng 		 * the recovery or starting the recovery by DUPACK threshold.
1720b654dfSYuchung Cheng 		 */
1820b654dfSYuchung Cheng 		if (inet_csk(sk)->icsk_ca_state >= TCP_CA_Recovery)
1920b654dfSYuchung Cheng 			return 0;
2020b654dfSYuchung Cheng 
2120b654dfSYuchung Cheng 		if (tp->sacked_out >= tp->reordering &&
2220b654dfSYuchung Cheng 		    !(sock_net(sk)->ipv4.sysctl_tcp_recovery & TCP_RACK_NO_DUPTHRESH))
2320b654dfSYuchung Cheng 			return 0;
2420b654dfSYuchung Cheng 	}
2520b654dfSYuchung Cheng 
2620b654dfSYuchung Cheng 	/* To be more reordering resilient, allow min_rtt/4 settling delay.
2720b654dfSYuchung Cheng 	 * Use min_rtt instead of the smoothed RTT because reordering is
2820b654dfSYuchung Cheng 	 * often a path property and less related to queuing or delayed ACKs.
2920b654dfSYuchung Cheng 	 * Upon receiving DSACKs, linearly increase the window up to the
3020b654dfSYuchung Cheng 	 * smoothed RTT.
3120b654dfSYuchung Cheng 	 */
3220b654dfSYuchung Cheng 	return min((tcp_min_rtt(tp) >> 2) * tp->rack.reo_wnd_steps,
3320b654dfSYuchung Cheng 		   tp->srtt_us >> 3);
3420b654dfSYuchung Cheng }
3520b654dfSYuchung Cheng 
36b8fef65aSYuchung Cheng s32 tcp_rack_skb_timeout(struct tcp_sock *tp, struct sk_buff *skb, u32 reo_wnd)
37b8fef65aSYuchung Cheng {
38b8fef65aSYuchung Cheng 	return tp->rack.rtt_us + reo_wnd -
392fd66ffbSEric Dumazet 	       tcp_stamp_us_delta(tp->tcp_mstamp, tcp_skb_timestamp_us(skb));
40b8fef65aSYuchung Cheng }
41b8fef65aSYuchung Cheng 
42a0370b3fSYuchung Cheng /* RACK loss detection (IETF draft draft-ietf-tcpm-rack-01):
43a0370b3fSYuchung Cheng  *
44a0370b3fSYuchung Cheng  * Marks a packet lost, if some packet sent later has been (s)acked.
454f41b1c5SYuchung Cheng  * The underlying idea is similar to the traditional dupthresh and FACK
464f41b1c5SYuchung Cheng  * but they look at different metrics:
474f41b1c5SYuchung Cheng  *
484f41b1c5SYuchung Cheng  * dupthresh: 3 OOO packets delivered (packet count)
494f41b1c5SYuchung Cheng  * FACK: sequence delta to highest sacked sequence (sequence space)
504f41b1c5SYuchung Cheng  * RACK: sent time delta to the latest delivered packet (time domain)
514f41b1c5SYuchung Cheng  *
524f41b1c5SYuchung Cheng  * The advantage of RACK is it applies to both original and retransmitted
534f41b1c5SYuchung Cheng  * packet and therefore is robust against tail losses. Another advantage
544f41b1c5SYuchung Cheng  * is being more resilient to reordering by simply allowing some
554f41b1c5SYuchung Cheng  * "settling delay", instead of tweaking the dupthresh.
564f41b1c5SYuchung Cheng  *
57a0370b3fSYuchung Cheng  * When tcp_rack_detect_loss() detects some packets are lost and we
58a0370b3fSYuchung Cheng  * are not already in the CA_Recovery state, either tcp_rack_reo_timeout()
59a0370b3fSYuchung Cheng  * or tcp_time_to_recover()'s "Trick#1: the loss is proven" code path will
60a0370b3fSYuchung Cheng  * make us enter the CA_Recovery state.
614f41b1c5SYuchung Cheng  */
627c1c7308SEric Dumazet static void tcp_rack_detect_loss(struct sock *sk, u32 *reo_timeout)
634f41b1c5SYuchung Cheng {
644f41b1c5SYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
65043b87d7SYuchung Cheng 	struct sk_buff *skb, *n;
66e636f8b0SYuchung Cheng 	u32 reo_wnd;
674f41b1c5SYuchung Cheng 
6857dde7f7SYuchung Cheng 	*reo_timeout = 0;
6920b654dfSYuchung Cheng 	reo_wnd = tcp_rack_reo_wnd(sk);
70043b87d7SYuchung Cheng 	list_for_each_entry_safe(skb, n, &tp->tsorted_sent_queue,
71043b87d7SYuchung Cheng 				 tcp_tsorted_anchor) {
724f41b1c5SYuchung Cheng 		struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
73bef06223SYuchung Cheng 		s32 remaining;
7457dde7f7SYuchung Cheng 
7557dde7f7SYuchung Cheng 		/* Skip ones marked lost but not yet retransmitted */
7657dde7f7SYuchung Cheng 		if ((scb->sacked & TCPCB_LOST) &&
7757dde7f7SYuchung Cheng 		    !(scb->sacked & TCPCB_SACKED_RETRANS))
7857dde7f7SYuchung Cheng 			continue;
7957dde7f7SYuchung Cheng 
802fd66ffbSEric Dumazet 		if (!tcp_rack_sent_after(tp->rack.mstamp,
812fd66ffbSEric Dumazet 					 tcp_skb_timestamp_us(skb),
82bef06223SYuchung Cheng 					 tp->rack.end_seq, scb->end_seq))
83bef06223SYuchung Cheng 			break;
84bef06223SYuchung Cheng 
85bef06223SYuchung Cheng 		/* A packet is lost if it has not been s/acked beyond
86bef06223SYuchung Cheng 		 * the recent RTT plus the reordering window.
87bef06223SYuchung Cheng 		 */
88b8fef65aSYuchung Cheng 		remaining = tcp_rack_skb_timeout(tp, skb, reo_wnd);
89428aec5eSYuchung Cheng 		if (remaining <= 0) {
90d716bfdbSYuchung Cheng 			tcp_mark_skb_lost(sk, skb);
91bef06223SYuchung Cheng 			list_del_init(&skb->tcp_tsorted_anchor);
92bef06223SYuchung Cheng 		} else {
93428aec5eSYuchung Cheng 			/* Record maximum wait time */
94428aec5eSYuchung Cheng 			*reo_timeout = max_t(u32, *reo_timeout, remaining);
954f41b1c5SYuchung Cheng 		}
964f41b1c5SYuchung Cheng 	}
97e636f8b0SYuchung Cheng }
98e636f8b0SYuchung Cheng 
9962d9f1a6SPengcheng Yang bool tcp_rack_mark_lost(struct sock *sk)
100e636f8b0SYuchung Cheng {
101e636f8b0SYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
10257dde7f7SYuchung Cheng 	u32 timeout;
103e636f8b0SYuchung Cheng 
104a0370b3fSYuchung Cheng 	if (!tp->rack.advanced)
10562d9f1a6SPengcheng Yang 		return false;
10657dde7f7SYuchung Cheng 
107e636f8b0SYuchung Cheng 	/* Reset the advanced flag to avoid unnecessary queue scanning */
108e636f8b0SYuchung Cheng 	tp->rack.advanced = 0;
1097c1c7308SEric Dumazet 	tcp_rack_detect_loss(sk, &timeout);
11057dde7f7SYuchung Cheng 	if (timeout) {
111bb4d991aSYuchung Cheng 		timeout = usecs_to_jiffies(timeout) + TCP_TIMEOUT_MIN;
11257dde7f7SYuchung Cheng 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_REO_TIMEOUT,
11357dde7f7SYuchung Cheng 					  timeout, inet_csk(sk)->icsk_rto);
11457dde7f7SYuchung Cheng 	}
11562d9f1a6SPengcheng Yang 	return !!timeout;
1164f41b1c5SYuchung Cheng }
1174f41b1c5SYuchung Cheng 
118deed7be7SYuchung Cheng /* Record the most recently (re)sent time among the (s)acked packets
119deed7be7SYuchung Cheng  * This is "Step 3: Advance RACK.xmit_time and update RACK.RTT" from
120deed7be7SYuchung Cheng  * draft-cheng-tcpm-rack-00.txt
121deed7be7SYuchung Cheng  */
1221d0833dfSYuchung Cheng void tcp_rack_advance(struct tcp_sock *tp, u8 sacked, u32 end_seq,
1239a568de4SEric Dumazet 		      u64 xmit_time)
124659a8ad5SYuchung Cheng {
125deed7be7SYuchung Cheng 	u32 rtt_us;
126deed7be7SYuchung Cheng 
1279a568de4SEric Dumazet 	rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, xmit_time);
1286065fd0dSYuchung Cheng 	if (rtt_us < tcp_min_rtt(tp) && (sacked & TCPCB_RETRANS)) {
129659a8ad5SYuchung Cheng 		/* If the sacked packet was retransmitted, it's ambiguous
130659a8ad5SYuchung Cheng 		 * whether the retransmission or the original (or the prior
131659a8ad5SYuchung Cheng 		 * retransmission) was sacked.
132659a8ad5SYuchung Cheng 		 *
133659a8ad5SYuchung Cheng 		 * If the original is lost, there is no ambiguity. Otherwise
134659a8ad5SYuchung Cheng 		 * we assume the original can be delayed up to aRTT + min_rtt.
135659a8ad5SYuchung Cheng 		 * the aRTT term is bounded by the fast recovery or timeout,
136659a8ad5SYuchung Cheng 		 * so it's at least one RTT (i.e., retransmission is at least
137659a8ad5SYuchung Cheng 		 * an RTT later).
138659a8ad5SYuchung Cheng 		 */
139659a8ad5SYuchung Cheng 		return;
140659a8ad5SYuchung Cheng 	}
1416065fd0dSYuchung Cheng 	tp->rack.advanced = 1;
142deed7be7SYuchung Cheng 	tp->rack.rtt_us = rtt_us;
1436065fd0dSYuchung Cheng 	if (tcp_rack_sent_after(xmit_time, tp->rack.mstamp,
1446065fd0dSYuchung Cheng 				end_seq, tp->rack.end_seq)) {
1459a568de4SEric Dumazet 		tp->rack.mstamp = xmit_time;
1461d0833dfSYuchung Cheng 		tp->rack.end_seq = end_seq;
1476065fd0dSYuchung Cheng 	}
148659a8ad5SYuchung Cheng }
14957dde7f7SYuchung Cheng 
15057dde7f7SYuchung Cheng /* We have waited long enough to accommodate reordering. Mark the expired
15157dde7f7SYuchung Cheng  * packets lost and retransmit them.
15257dde7f7SYuchung Cheng  */
15357dde7f7SYuchung Cheng void tcp_rack_reo_timeout(struct sock *sk)
15457dde7f7SYuchung Cheng {
15557dde7f7SYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
15657dde7f7SYuchung Cheng 	u32 timeout, prior_inflight;
1577e901ee7SYuchung Cheng 	u32 lost = tp->lost;
15857dde7f7SYuchung Cheng 
15957dde7f7SYuchung Cheng 	prior_inflight = tcp_packets_in_flight(tp);
1607c1c7308SEric Dumazet 	tcp_rack_detect_loss(sk, &timeout);
16157dde7f7SYuchung Cheng 	if (prior_inflight != tcp_packets_in_flight(tp)) {
16257dde7f7SYuchung Cheng 		if (inet_csk(sk)->icsk_ca_state != TCP_CA_Recovery) {
16357dde7f7SYuchung Cheng 			tcp_enter_recovery(sk, false);
16457dde7f7SYuchung Cheng 			if (!inet_csk(sk)->icsk_ca_ops->cong_control)
1657e901ee7SYuchung Cheng 				tcp_cwnd_reduction(sk, 1, tp->lost - lost, 0);
16657dde7f7SYuchung Cheng 		}
16757dde7f7SYuchung Cheng 		tcp_xmit_retransmit_queue(sk);
16857dde7f7SYuchung Cheng 	}
16957dde7f7SYuchung Cheng 	if (inet_csk(sk)->icsk_pending != ICSK_TIME_RETRANS)
17057dde7f7SYuchung Cheng 		tcp_rearm_rto(sk);
17157dde7f7SYuchung Cheng }
1721f255691SPriyaranjan Jha 
1731f255691SPriyaranjan Jha /* Updates the RACK's reo_wnd based on DSACK and no. of recoveries.
1741f255691SPriyaranjan Jha  *
175*a657db03SNeal Cardwell  * If a DSACK is received that seems like it may have been due to reordering
176*a657db03SNeal Cardwell  * triggering fast recovery, increment reo_wnd by min_rtt/4 (upper bounded
1771f255691SPriyaranjan Jha  * by srtt), since there is possibility that spurious retransmission was
1781f255691SPriyaranjan Jha  * due to reordering delay longer than reo_wnd.
1791f255691SPriyaranjan Jha  *
1801f255691SPriyaranjan Jha  * Persist the current reo_wnd value for TCP_RACK_RECOVERY_THRESH (16)
1811f255691SPriyaranjan Jha  * no. of successful recoveries (accounts for full DSACK-based loss
1821f255691SPriyaranjan Jha  * recovery undo). After that, reset it to default (min_rtt/4).
1831f255691SPriyaranjan Jha  *
1841f255691SPriyaranjan Jha  * At max, reo_wnd is incremented only once per rtt. So that the new
1851f255691SPriyaranjan Jha  * DSACK on which we are reacting, is due to the spurious retx (approx)
1861f255691SPriyaranjan Jha  * after the reo_wnd has been updated last time.
1871f255691SPriyaranjan Jha  *
1881f255691SPriyaranjan Jha  * reo_wnd is tracked in terms of steps (of min_rtt/4), rather than
1891f255691SPriyaranjan Jha  * absolute value to account for change in rtt.
1901f255691SPriyaranjan Jha  */
1911f255691SPriyaranjan Jha void tcp_rack_update_reo_wnd(struct sock *sk, struct rate_sample *rs)
1921f255691SPriyaranjan Jha {
1931f255691SPriyaranjan Jha 	struct tcp_sock *tp = tcp_sk(sk);
1941f255691SPriyaranjan Jha 
1951f255691SPriyaranjan Jha 	if (sock_net(sk)->ipv4.sysctl_tcp_recovery & TCP_RACK_STATIC_REO_WND ||
1961f255691SPriyaranjan Jha 	    !rs->prior_delivered)
1971f255691SPriyaranjan Jha 		return;
1981f255691SPriyaranjan Jha 
1991f255691SPriyaranjan Jha 	/* Disregard DSACK if a rtt has not passed since we adjusted reo_wnd */
2001f255691SPriyaranjan Jha 	if (before(rs->prior_delivered, tp->rack.last_delivered))
2011f255691SPriyaranjan Jha 		tp->rack.dsack_seen = 0;
2021f255691SPriyaranjan Jha 
2031f255691SPriyaranjan Jha 	/* Adjust the reo_wnd if update is pending */
2041f255691SPriyaranjan Jha 	if (tp->rack.dsack_seen) {
2051f255691SPriyaranjan Jha 		tp->rack.reo_wnd_steps = min_t(u32, 0xFF,
2061f255691SPriyaranjan Jha 					       tp->rack.reo_wnd_steps + 1);
2071f255691SPriyaranjan Jha 		tp->rack.dsack_seen = 0;
2081f255691SPriyaranjan Jha 		tp->rack.last_delivered = tp->delivered;
2091f255691SPriyaranjan Jha 		tp->rack.reo_wnd_persist = TCP_RACK_RECOVERY_THRESH;
2101f255691SPriyaranjan Jha 	} else if (!tp->rack.reo_wnd_persist) {
2111f255691SPriyaranjan Jha 		tp->rack.reo_wnd_steps = 1;
2121f255691SPriyaranjan Jha 	}
2131f255691SPriyaranjan Jha }
2146ac06ecdSYuchung Cheng 
2156ac06ecdSYuchung Cheng /* RFC6582 NewReno recovery for non-SACK connection. It simply retransmits
2166ac06ecdSYuchung Cheng  * the next unacked packet upon receiving
2176ac06ecdSYuchung Cheng  * a) three or more DUPACKs to start the fast recovery
2186ac06ecdSYuchung Cheng  * b) an ACK acknowledging new data during the fast recovery.
2196ac06ecdSYuchung Cheng  */
2206ac06ecdSYuchung Cheng void tcp_newreno_mark_lost(struct sock *sk, bool snd_una_advanced)
2216ac06ecdSYuchung Cheng {
2226ac06ecdSYuchung Cheng 	const u8 state = inet_csk(sk)->icsk_ca_state;
2236ac06ecdSYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
2246ac06ecdSYuchung Cheng 
2256ac06ecdSYuchung Cheng 	if ((state < TCP_CA_Recovery && tp->sacked_out >= tp->reordering) ||
2266ac06ecdSYuchung Cheng 	    (state == TCP_CA_Recovery && snd_una_advanced)) {
2276ac06ecdSYuchung Cheng 		struct sk_buff *skb = tcp_rtx_queue_head(sk);
2286ac06ecdSYuchung Cheng 		u32 mss;
2296ac06ecdSYuchung Cheng 
2306ac06ecdSYuchung Cheng 		if (TCP_SKB_CB(skb)->sacked & TCPCB_LOST)
2316ac06ecdSYuchung Cheng 			return;
2326ac06ecdSYuchung Cheng 
2336ac06ecdSYuchung Cheng 		mss = tcp_skb_mss(skb);
2346ac06ecdSYuchung Cheng 		if (tcp_skb_pcount(skb) > 1 && skb->len > mss)
2356ac06ecdSYuchung Cheng 			tcp_fragment(sk, TCP_FRAG_IN_RTX_QUEUE, skb,
2366ac06ecdSYuchung Cheng 				     mss, mss, GFP_ATOMIC);
2376ac06ecdSYuchung Cheng 
238179ac35fSYuchung Cheng 		tcp_mark_skb_lost(sk, skb);
2396ac06ecdSYuchung Cheng 	}
2406ac06ecdSYuchung Cheng }
241