18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci#include <linux/tcp.h> 38c2ecf20Sopenharmony_ci#include <net/tcp.h> 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_cistatic bool tcp_rack_sent_after(u64 t1, u64 t2, u32 seq1, u32 seq2) 68c2ecf20Sopenharmony_ci{ 78c2ecf20Sopenharmony_ci return t1 > t2 || (t1 == t2 && after(seq1, seq2)); 88c2ecf20Sopenharmony_ci} 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_cistatic u32 tcp_rack_reo_wnd(const struct sock *sk) 118c2ecf20Sopenharmony_ci{ 128c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci if (!tp->reord_seen) { 158c2ecf20Sopenharmony_ci /* If reordering has not been observed, be aggressive during 168c2ecf20Sopenharmony_ci * the recovery or starting the recovery by DUPACK threshold. 178c2ecf20Sopenharmony_ci */ 188c2ecf20Sopenharmony_ci if (inet_csk(sk)->icsk_ca_state >= TCP_CA_Recovery) 198c2ecf20Sopenharmony_ci return 0; 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci if (tp->sacked_out >= tp->reordering && 228c2ecf20Sopenharmony_ci !(READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_recovery) & 238c2ecf20Sopenharmony_ci TCP_RACK_NO_DUPTHRESH)) 248c2ecf20Sopenharmony_ci return 0; 258c2ecf20Sopenharmony_ci } 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci /* To be more reordering resilient, allow min_rtt/4 settling delay. 288c2ecf20Sopenharmony_ci * Use min_rtt instead of the smoothed RTT because reordering is 298c2ecf20Sopenharmony_ci * often a path property and less related to queuing or delayed ACKs. 308c2ecf20Sopenharmony_ci * Upon receiving DSACKs, linearly increase the window up to the 318c2ecf20Sopenharmony_ci * smoothed RTT. 328c2ecf20Sopenharmony_ci */ 338c2ecf20Sopenharmony_ci return min((tcp_min_rtt(tp) >> 2) * tp->rack.reo_wnd_steps, 348c2ecf20Sopenharmony_ci tp->srtt_us >> 3); 358c2ecf20Sopenharmony_ci} 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_cis32 tcp_rack_skb_timeout(struct tcp_sock *tp, struct sk_buff *skb, u32 reo_wnd) 388c2ecf20Sopenharmony_ci{ 398c2ecf20Sopenharmony_ci return tp->rack.rtt_us + reo_wnd - 408c2ecf20Sopenharmony_ci tcp_stamp_us_delta(tp->tcp_mstamp, tcp_skb_timestamp_us(skb)); 418c2ecf20Sopenharmony_ci} 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci/* RACK loss detection (IETF draft draft-ietf-tcpm-rack-01): 448c2ecf20Sopenharmony_ci * 458c2ecf20Sopenharmony_ci * Marks a packet lost, if some packet sent later has been (s)acked. 468c2ecf20Sopenharmony_ci * The underlying idea is similar to the traditional dupthresh and FACK 478c2ecf20Sopenharmony_ci * but they look at different metrics: 488c2ecf20Sopenharmony_ci * 498c2ecf20Sopenharmony_ci * dupthresh: 3 OOO packets delivered (packet count) 508c2ecf20Sopenharmony_ci * FACK: sequence delta to highest sacked sequence (sequence space) 518c2ecf20Sopenharmony_ci * RACK: sent time delta to the latest delivered packet (time domain) 528c2ecf20Sopenharmony_ci * 538c2ecf20Sopenharmony_ci * The advantage of RACK is it applies to both original and retransmitted 548c2ecf20Sopenharmony_ci * packet and therefore is robust against tail losses. Another advantage 558c2ecf20Sopenharmony_ci * is being more resilient to reordering by simply allowing some 568c2ecf20Sopenharmony_ci * "settling delay", instead of tweaking the dupthresh. 578c2ecf20Sopenharmony_ci * 588c2ecf20Sopenharmony_ci * When tcp_rack_detect_loss() detects some packets are lost and we 598c2ecf20Sopenharmony_ci * are not already in the CA_Recovery state, either tcp_rack_reo_timeout() 608c2ecf20Sopenharmony_ci * or tcp_time_to_recover()'s "Trick#1: the loss is proven" code path will 618c2ecf20Sopenharmony_ci * make us enter the CA_Recovery state. 628c2ecf20Sopenharmony_ci */ 638c2ecf20Sopenharmony_cistatic void tcp_rack_detect_loss(struct sock *sk, u32 *reo_timeout) 648c2ecf20Sopenharmony_ci{ 658c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 668c2ecf20Sopenharmony_ci struct sk_buff *skb, *n; 678c2ecf20Sopenharmony_ci u32 reo_wnd; 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci *reo_timeout = 0; 708c2ecf20Sopenharmony_ci reo_wnd = tcp_rack_reo_wnd(sk); 718c2ecf20Sopenharmony_ci list_for_each_entry_safe(skb, n, &tp->tsorted_sent_queue, 728c2ecf20Sopenharmony_ci tcp_tsorted_anchor) { 738c2ecf20Sopenharmony_ci struct tcp_skb_cb *scb = TCP_SKB_CB(skb); 748c2ecf20Sopenharmony_ci s32 remaining; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci /* Skip ones marked lost but not yet retransmitted */ 778c2ecf20Sopenharmony_ci if ((scb->sacked & TCPCB_LOST) && 788c2ecf20Sopenharmony_ci !(scb->sacked & TCPCB_SACKED_RETRANS)) 798c2ecf20Sopenharmony_ci continue; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci if (!tcp_rack_sent_after(tp->rack.mstamp, 828c2ecf20Sopenharmony_ci tcp_skb_timestamp_us(skb), 838c2ecf20Sopenharmony_ci tp->rack.end_seq, scb->end_seq)) 848c2ecf20Sopenharmony_ci break; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci /* A packet is lost if it has not been s/acked beyond 878c2ecf20Sopenharmony_ci * the recent RTT plus the reordering window. 888c2ecf20Sopenharmony_ci */ 898c2ecf20Sopenharmony_ci remaining = tcp_rack_skb_timeout(tp, skb, reo_wnd); 908c2ecf20Sopenharmony_ci if (remaining <= 0) { 918c2ecf20Sopenharmony_ci tcp_mark_skb_lost(sk, skb); 928c2ecf20Sopenharmony_ci list_del_init(&skb->tcp_tsorted_anchor); 938c2ecf20Sopenharmony_ci } else { 948c2ecf20Sopenharmony_ci /* Record maximum wait time */ 958c2ecf20Sopenharmony_ci *reo_timeout = max_t(u32, *reo_timeout, remaining); 968c2ecf20Sopenharmony_ci } 978c2ecf20Sopenharmony_ci } 988c2ecf20Sopenharmony_ci} 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_cibool tcp_rack_mark_lost(struct sock *sk) 1018c2ecf20Sopenharmony_ci{ 1028c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 1038c2ecf20Sopenharmony_ci u32 timeout; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci if (!tp->rack.advanced) 1068c2ecf20Sopenharmony_ci return false; 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci /* Reset the advanced flag to avoid unnecessary queue scanning */ 1098c2ecf20Sopenharmony_ci tp->rack.advanced = 0; 1108c2ecf20Sopenharmony_ci tcp_rack_detect_loss(sk, &timeout); 1118c2ecf20Sopenharmony_ci if (timeout) { 1128c2ecf20Sopenharmony_ci timeout = usecs_to_jiffies(timeout + TCP_TIMEOUT_MIN_US); 1138c2ecf20Sopenharmony_ci inet_csk_reset_xmit_timer(sk, ICSK_TIME_REO_TIMEOUT, 1148c2ecf20Sopenharmony_ci timeout, inet_csk(sk)->icsk_rto); 1158c2ecf20Sopenharmony_ci } 1168c2ecf20Sopenharmony_ci return !!timeout; 1178c2ecf20Sopenharmony_ci} 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci/* Record the most recently (re)sent time among the (s)acked packets 1208c2ecf20Sopenharmony_ci * This is "Step 3: Advance RACK.xmit_time and update RACK.RTT" from 1218c2ecf20Sopenharmony_ci * draft-cheng-tcpm-rack-00.txt 1228c2ecf20Sopenharmony_ci */ 1238c2ecf20Sopenharmony_civoid tcp_rack_advance(struct tcp_sock *tp, u8 sacked, u32 end_seq, 1248c2ecf20Sopenharmony_ci u64 xmit_time) 1258c2ecf20Sopenharmony_ci{ 1268c2ecf20Sopenharmony_ci u32 rtt_us; 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, xmit_time); 1298c2ecf20Sopenharmony_ci if (rtt_us < tcp_min_rtt(tp) && (sacked & TCPCB_RETRANS)) { 1308c2ecf20Sopenharmony_ci /* If the sacked packet was retransmitted, it's ambiguous 1318c2ecf20Sopenharmony_ci * whether the retransmission or the original (or the prior 1328c2ecf20Sopenharmony_ci * retransmission) was sacked. 1338c2ecf20Sopenharmony_ci * 1348c2ecf20Sopenharmony_ci * If the original is lost, there is no ambiguity. Otherwise 1358c2ecf20Sopenharmony_ci * we assume the original can be delayed up to aRTT + min_rtt. 1368c2ecf20Sopenharmony_ci * the aRTT term is bounded by the fast recovery or timeout, 1378c2ecf20Sopenharmony_ci * so it's at least one RTT (i.e., retransmission is at least 1388c2ecf20Sopenharmony_ci * an RTT later). 1398c2ecf20Sopenharmony_ci */ 1408c2ecf20Sopenharmony_ci return; 1418c2ecf20Sopenharmony_ci } 1428c2ecf20Sopenharmony_ci tp->rack.advanced = 1; 1438c2ecf20Sopenharmony_ci tp->rack.rtt_us = rtt_us; 1448c2ecf20Sopenharmony_ci if (tcp_rack_sent_after(xmit_time, tp->rack.mstamp, 1458c2ecf20Sopenharmony_ci end_seq, tp->rack.end_seq)) { 1468c2ecf20Sopenharmony_ci tp->rack.mstamp = xmit_time; 1478c2ecf20Sopenharmony_ci tp->rack.end_seq = end_seq; 1488c2ecf20Sopenharmony_ci } 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci/* We have waited long enough to accommodate reordering. Mark the expired 1528c2ecf20Sopenharmony_ci * packets lost and retransmit them. 1538c2ecf20Sopenharmony_ci */ 1548c2ecf20Sopenharmony_civoid tcp_rack_reo_timeout(struct sock *sk) 1558c2ecf20Sopenharmony_ci{ 1568c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 1578c2ecf20Sopenharmony_ci u32 timeout, prior_inflight; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci prior_inflight = tcp_packets_in_flight(tp); 1608c2ecf20Sopenharmony_ci tcp_rack_detect_loss(sk, &timeout); 1618c2ecf20Sopenharmony_ci if (prior_inflight != tcp_packets_in_flight(tp)) { 1628c2ecf20Sopenharmony_ci if (inet_csk(sk)->icsk_ca_state != TCP_CA_Recovery) { 1638c2ecf20Sopenharmony_ci tcp_enter_recovery(sk, false); 1648c2ecf20Sopenharmony_ci if (!inet_csk(sk)->icsk_ca_ops->cong_control) 1658c2ecf20Sopenharmony_ci tcp_cwnd_reduction(sk, 1, 0); 1668c2ecf20Sopenharmony_ci } 1678c2ecf20Sopenharmony_ci tcp_xmit_retransmit_queue(sk); 1688c2ecf20Sopenharmony_ci } 1698c2ecf20Sopenharmony_ci if (inet_csk(sk)->icsk_pending != ICSK_TIME_RETRANS) 1708c2ecf20Sopenharmony_ci tcp_rearm_rto(sk); 1718c2ecf20Sopenharmony_ci} 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci/* Updates the RACK's reo_wnd based on DSACK and no. of recoveries. 1748c2ecf20Sopenharmony_ci * 1758c2ecf20Sopenharmony_ci * If DSACK is received, increment reo_wnd by min_rtt/4 (upper bounded 1768c2ecf20Sopenharmony_ci * by srtt), since there is possibility that spurious retransmission was 1778c2ecf20Sopenharmony_ci * due to reordering delay longer than reo_wnd. 1788c2ecf20Sopenharmony_ci * 1798c2ecf20Sopenharmony_ci * Persist the current reo_wnd value for TCP_RACK_RECOVERY_THRESH (16) 1808c2ecf20Sopenharmony_ci * no. of successful recoveries (accounts for full DSACK-based loss 1818c2ecf20Sopenharmony_ci * recovery undo). After that, reset it to default (min_rtt/4). 1828c2ecf20Sopenharmony_ci * 1838c2ecf20Sopenharmony_ci * At max, reo_wnd is incremented only once per rtt. So that the new 1848c2ecf20Sopenharmony_ci * DSACK on which we are reacting, is due to the spurious retx (approx) 1858c2ecf20Sopenharmony_ci * after the reo_wnd has been updated last time. 1868c2ecf20Sopenharmony_ci * 1878c2ecf20Sopenharmony_ci * reo_wnd is tracked in terms of steps (of min_rtt/4), rather than 1888c2ecf20Sopenharmony_ci * absolute value to account for change in rtt. 1898c2ecf20Sopenharmony_ci */ 1908c2ecf20Sopenharmony_civoid tcp_rack_update_reo_wnd(struct sock *sk, struct rate_sample *rs) 1918c2ecf20Sopenharmony_ci{ 1928c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci if ((READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_recovery) & 1958c2ecf20Sopenharmony_ci TCP_RACK_STATIC_REO_WND) || 1968c2ecf20Sopenharmony_ci !rs->prior_delivered) 1978c2ecf20Sopenharmony_ci return; 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci /* Disregard DSACK if a rtt has not passed since we adjusted reo_wnd */ 2008c2ecf20Sopenharmony_ci if (before(rs->prior_delivered, tp->rack.last_delivered)) 2018c2ecf20Sopenharmony_ci tp->rack.dsack_seen = 0; 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci /* Adjust the reo_wnd if update is pending */ 2048c2ecf20Sopenharmony_ci if (tp->rack.dsack_seen) { 2058c2ecf20Sopenharmony_ci tp->rack.reo_wnd_steps = min_t(u32, 0xFF, 2068c2ecf20Sopenharmony_ci tp->rack.reo_wnd_steps + 1); 2078c2ecf20Sopenharmony_ci tp->rack.dsack_seen = 0; 2088c2ecf20Sopenharmony_ci tp->rack.last_delivered = tp->delivered; 2098c2ecf20Sopenharmony_ci tp->rack.reo_wnd_persist = TCP_RACK_RECOVERY_THRESH; 2108c2ecf20Sopenharmony_ci } else if (!tp->rack.reo_wnd_persist) { 2118c2ecf20Sopenharmony_ci tp->rack.reo_wnd_steps = 1; 2128c2ecf20Sopenharmony_ci } 2138c2ecf20Sopenharmony_ci} 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci/* RFC6582 NewReno recovery for non-SACK connection. It simply retransmits 2168c2ecf20Sopenharmony_ci * the next unacked packet upon receiving 2178c2ecf20Sopenharmony_ci * a) three or more DUPACKs to start the fast recovery 2188c2ecf20Sopenharmony_ci * b) an ACK acknowledging new data during the fast recovery. 2198c2ecf20Sopenharmony_ci */ 2208c2ecf20Sopenharmony_civoid tcp_newreno_mark_lost(struct sock *sk, bool snd_una_advanced) 2218c2ecf20Sopenharmony_ci{ 2228c2ecf20Sopenharmony_ci const u8 state = inet_csk(sk)->icsk_ca_state; 2238c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci if ((state < TCP_CA_Recovery && tp->sacked_out >= tp->reordering) || 2268c2ecf20Sopenharmony_ci (state == TCP_CA_Recovery && snd_una_advanced)) { 2278c2ecf20Sopenharmony_ci struct sk_buff *skb = tcp_rtx_queue_head(sk); 2288c2ecf20Sopenharmony_ci u32 mss; 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci if (TCP_SKB_CB(skb)->sacked & TCPCB_LOST) 2318c2ecf20Sopenharmony_ci return; 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci mss = tcp_skb_mss(skb); 2348c2ecf20Sopenharmony_ci if (tcp_skb_pcount(skb) > 1 && skb->len > mss) 2358c2ecf20Sopenharmony_ci tcp_fragment(sk, TCP_FRAG_IN_RTX_QUEUE, skb, 2368c2ecf20Sopenharmony_ci mss, mss, GFP_ATOMIC); 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci tcp_mark_skb_lost(sk, skb); 2398c2ecf20Sopenharmony_ci } 2408c2ecf20Sopenharmony_ci} 241