Yuchung Cheng | 659a8ad | 2015-10-16 21:57:46 -0700 | [diff] [blame] | 1 | #include <linux/tcp.h> |
| 2 | #include <net/tcp.h> |
| 3 | |
Yuchung Cheng | a0370b3 | 2017-01-12 22:11:36 -0800 | [diff] [blame] | 4 | int sysctl_tcp_recovery __read_mostly = TCP_RACK_LOSS_DETECTION; |
Yuchung Cheng | 4f41b1c | 2015-10-16 21:57:47 -0700 | [diff] [blame] | 5 | |
Yuchung Cheng | db8da6b | 2017-01-12 22:11:30 -0800 | [diff] [blame] | 6 | static void tcp_rack_mark_skb_lost(struct sock *sk, struct sk_buff *skb) |
| 7 | { |
| 8 | struct tcp_sock *tp = tcp_sk(sk); |
| 9 | |
| 10 | tcp_skb_mark_lost_uncond_verify(tp, skb); |
| 11 | if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) { |
| 12 | /* Account for retransmits that are lost again */ |
| 13 | TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS; |
| 14 | tp->retrans_out -= tcp_skb_pcount(skb); |
| 15 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPLOSTRETRANSMIT); |
| 16 | } |
| 17 | } |
| 18 | |
Yuchung Cheng | 1d0833d | 2017-01-12 22:11:34 -0800 | [diff] [blame] | 19 | static bool tcp_rack_sent_after(const struct skb_mstamp *t1, |
| 20 | const struct skb_mstamp *t2, |
| 21 | u32 seq1, u32 seq2) |
| 22 | { |
| 23 | return skb_mstamp_after(t1, t2) || |
| 24 | (t1->v64 == t2->v64 && after(seq1, seq2)); |
| 25 | } |
| 26 | |
Yuchung Cheng | a0370b3 | 2017-01-12 22:11:36 -0800 | [diff] [blame] | 27 | /* RACK loss detection (IETF draft draft-ietf-tcpm-rack-01): |
| 28 | * |
| 29 | * Marks a packet lost, if some packet sent later has been (s)acked. |
Yuchung Cheng | 4f41b1c | 2015-10-16 21:57:47 -0700 | [diff] [blame] | 30 | * The underlying idea is similar to the traditional dupthresh and FACK |
| 31 | * but they look at different metrics: |
| 32 | * |
| 33 | * dupthresh: 3 OOO packets delivered (packet count) |
| 34 | * FACK: sequence delta to highest sacked sequence (sequence space) |
| 35 | * RACK: sent time delta to the latest delivered packet (time domain) |
| 36 | * |
| 37 | * The advantage of RACK is it applies to both original and retransmitted |
| 38 | * packet and therefore is robust against tail losses. Another advantage |
| 39 | * is being more resilient to reordering by simply allowing some |
| 40 | * "settling delay", instead of tweaking the dupthresh. |
| 41 | * |
Yuchung Cheng | a0370b3 | 2017-01-12 22:11:36 -0800 | [diff] [blame] | 42 | * When tcp_rack_detect_loss() detects some packets are lost and we |
| 43 | * are not already in the CA_Recovery state, either tcp_rack_reo_timeout() |
| 44 | * or tcp_time_to_recover()'s "Trick#1: the loss is proven" code path will |
| 45 | * make us enter the CA_Recovery state. |
Yuchung Cheng | 4f41b1c | 2015-10-16 21:57:47 -0700 | [diff] [blame] | 46 | */ |
Yuchung Cheng | 57dde7f | 2017-01-12 22:11:33 -0800 | [diff] [blame] | 47 | static void tcp_rack_detect_loss(struct sock *sk, const struct skb_mstamp *now, |
| 48 | u32 *reo_timeout) |
Yuchung Cheng | 4f41b1c | 2015-10-16 21:57:47 -0700 | [diff] [blame] | 49 | { |
| 50 | struct tcp_sock *tp = tcp_sk(sk); |
| 51 | struct sk_buff *skb; |
Yuchung Cheng | e636f8b | 2017-01-12 22:11:31 -0800 | [diff] [blame] | 52 | u32 reo_wnd; |
Yuchung Cheng | 4f41b1c | 2015-10-16 21:57:47 -0700 | [diff] [blame] | 53 | |
Yuchung Cheng | 57dde7f | 2017-01-12 22:11:33 -0800 | [diff] [blame] | 54 | *reo_timeout = 0; |
Yuchung Cheng | 4f41b1c | 2015-10-16 21:57:47 -0700 | [diff] [blame] | 55 | /* To be more reordering resilient, allow min_rtt/4 settling delay |
| 56 | * (lower-bounded to 1000uS). We use min_rtt instead of the smoothed |
| 57 | * RTT because reordering is often a path property and less related |
| 58 | * to queuing or delayed ACKs. |
Yuchung Cheng | 4f41b1c | 2015-10-16 21:57:47 -0700 | [diff] [blame] | 59 | */ |
| 60 | reo_wnd = 1000; |
Yuchung Cheng | a0370b3 | 2017-01-12 22:11:36 -0800 | [diff] [blame] | 61 | if ((tp->rack.reord || !tp->lost_out) && tcp_min_rtt(tp) != ~0U) |
Yuchung Cheng | 4f41b1c | 2015-10-16 21:57:47 -0700 | [diff] [blame] | 62 | reo_wnd = max(tcp_min_rtt(tp) >> 2, reo_wnd); |
| 63 | |
| 64 | tcp_for_write_queue(skb, sk) { |
| 65 | struct tcp_skb_cb *scb = TCP_SKB_CB(skb); |
| 66 | |
| 67 | if (skb == tcp_send_head(sk)) |
| 68 | break; |
| 69 | |
| 70 | /* Skip ones already (s)acked */ |
| 71 | if (!after(scb->end_seq, tp->snd_una) || |
| 72 | scb->sacked & TCPCB_SACKED_ACKED) |
| 73 | continue; |
| 74 | |
Yuchung Cheng | 1d0833d | 2017-01-12 22:11:34 -0800 | [diff] [blame] | 75 | if (tcp_rack_sent_after(&tp->rack.mstamp, &skb->skb_mstamp, |
| 76 | tp->rack.end_seq, scb->end_seq)) { |
Yuchung Cheng | deed7be | 2017-01-12 22:11:32 -0800 | [diff] [blame] | 77 | /* Step 3 in draft-cheng-tcpm-rack-00.txt: |
| 78 | * A packet is lost if its elapsed time is beyond |
| 79 | * the recent RTT plus the reordering window. |
| 80 | */ |
Yuchung Cheng | 57dde7f | 2017-01-12 22:11:33 -0800 | [diff] [blame] | 81 | u32 elapsed = skb_mstamp_us_delta(now, |
| 82 | &skb->skb_mstamp); |
| 83 | s32 remaining = tp->rack.rtt_us + reo_wnd - elapsed; |
| 84 | |
| 85 | if (remaining < 0) { |
Yuchung Cheng | deed7be | 2017-01-12 22:11:32 -0800 | [diff] [blame] | 86 | tcp_rack_mark_skb_lost(sk, skb); |
Yuchung Cheng | 57dde7f | 2017-01-12 22:11:33 -0800 | [diff] [blame] | 87 | continue; |
Yuchung Cheng | deed7be | 2017-01-12 22:11:32 -0800 | [diff] [blame] | 88 | } |
Yuchung Cheng | 57dde7f | 2017-01-12 22:11:33 -0800 | [diff] [blame] | 89 | |
| 90 | /* Skip ones marked lost but not yet retransmitted */ |
| 91 | if ((scb->sacked & TCPCB_LOST) && |
| 92 | !(scb->sacked & TCPCB_SACKED_RETRANS)) |
| 93 | continue; |
| 94 | |
| 95 | /* Record maximum wait time (+1 to avoid 0) */ |
| 96 | *reo_timeout = max_t(u32, *reo_timeout, 1 + remaining); |
| 97 | |
Yuchung Cheng | 4f41b1c | 2015-10-16 21:57:47 -0700 | [diff] [blame] | 98 | } else if (!(scb->sacked & TCPCB_RETRANS)) { |
| 99 | /* Original data are sent sequentially so stop early |
| 100 | * b/c the rest are all sent after rack_sent |
| 101 | */ |
| 102 | break; |
| 103 | } |
| 104 | } |
Yuchung Cheng | e636f8b | 2017-01-12 22:11:31 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Yuchung Cheng | deed7be | 2017-01-12 22:11:32 -0800 | [diff] [blame] | 107 | void tcp_rack_mark_lost(struct sock *sk, const struct skb_mstamp *now) |
Yuchung Cheng | e636f8b | 2017-01-12 22:11:31 -0800 | [diff] [blame] | 108 | { |
| 109 | struct tcp_sock *tp = tcp_sk(sk); |
Yuchung Cheng | 57dde7f | 2017-01-12 22:11:33 -0800 | [diff] [blame] | 110 | u32 timeout; |
Yuchung Cheng | e636f8b | 2017-01-12 22:11:31 -0800 | [diff] [blame] | 111 | |
Yuchung Cheng | a0370b3 | 2017-01-12 22:11:36 -0800 | [diff] [blame] | 112 | if (!tp->rack.advanced) |
Yuchung Cheng | e636f8b | 2017-01-12 22:11:31 -0800 | [diff] [blame] | 113 | return; |
Yuchung Cheng | 57dde7f | 2017-01-12 22:11:33 -0800 | [diff] [blame] | 114 | |
Yuchung Cheng | e636f8b | 2017-01-12 22:11:31 -0800 | [diff] [blame] | 115 | /* Reset the advanced flag to avoid unnecessary queue scanning */ |
| 116 | tp->rack.advanced = 0; |
Yuchung Cheng | 57dde7f | 2017-01-12 22:11:33 -0800 | [diff] [blame] | 117 | tcp_rack_detect_loss(sk, now, &timeout); |
| 118 | if (timeout) { |
| 119 | timeout = usecs_to_jiffies(timeout + TCP_REO_TIMEOUT_MIN); |
| 120 | inet_csk_reset_xmit_timer(sk, ICSK_TIME_REO_TIMEOUT, |
| 121 | timeout, inet_csk(sk)->icsk_rto); |
| 122 | } |
Yuchung Cheng | 4f41b1c | 2015-10-16 21:57:47 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Yuchung Cheng | deed7be | 2017-01-12 22:11:32 -0800 | [diff] [blame] | 125 | /* Record the most recently (re)sent time among the (s)acked packets |
| 126 | * This is "Step 3: Advance RACK.xmit_time and update RACK.RTT" from |
| 127 | * draft-cheng-tcpm-rack-00.txt |
| 128 | */ |
Yuchung Cheng | 1d0833d | 2017-01-12 22:11:34 -0800 | [diff] [blame] | 129 | void tcp_rack_advance(struct tcp_sock *tp, u8 sacked, u32 end_seq, |
Yuchung Cheng | deed7be | 2017-01-12 22:11:32 -0800 | [diff] [blame] | 130 | const struct skb_mstamp *xmit_time, |
| 131 | const struct skb_mstamp *ack_time) |
Yuchung Cheng | 659a8ad | 2015-10-16 21:57:46 -0700 | [diff] [blame] | 132 | { |
Yuchung Cheng | deed7be | 2017-01-12 22:11:32 -0800 | [diff] [blame] | 133 | u32 rtt_us; |
| 134 | |
Yuchung Cheng | 659a8ad | 2015-10-16 21:57:46 -0700 | [diff] [blame] | 135 | if (tp->rack.mstamp.v64 && |
Yuchung Cheng | 1d0833d | 2017-01-12 22:11:34 -0800 | [diff] [blame] | 136 | !tcp_rack_sent_after(xmit_time, &tp->rack.mstamp, |
| 137 | end_seq, tp->rack.end_seq)) |
Yuchung Cheng | 659a8ad | 2015-10-16 21:57:46 -0700 | [diff] [blame] | 138 | return; |
| 139 | |
Yuchung Cheng | deed7be | 2017-01-12 22:11:32 -0800 | [diff] [blame] | 140 | rtt_us = skb_mstamp_us_delta(ack_time, xmit_time); |
Yuchung Cheng | 659a8ad | 2015-10-16 21:57:46 -0700 | [diff] [blame] | 141 | if (sacked & TCPCB_RETRANS) { |
Yuchung Cheng | 659a8ad | 2015-10-16 21:57:46 -0700 | [diff] [blame] | 142 | /* If the sacked packet was retransmitted, it's ambiguous |
| 143 | * whether the retransmission or the original (or the prior |
| 144 | * retransmission) was sacked. |
| 145 | * |
| 146 | * If the original is lost, there is no ambiguity. Otherwise |
| 147 | * we assume the original can be delayed up to aRTT + min_rtt. |
| 148 | * the aRTT term is bounded by the fast recovery or timeout, |
| 149 | * so it's at least one RTT (i.e., retransmission is at least |
| 150 | * an RTT later). |
| 151 | */ |
Yuchung Cheng | deed7be | 2017-01-12 22:11:32 -0800 | [diff] [blame] | 152 | if (rtt_us < tcp_min_rtt(tp)) |
Yuchung Cheng | 659a8ad | 2015-10-16 21:57:46 -0700 | [diff] [blame] | 153 | return; |
| 154 | } |
Yuchung Cheng | deed7be | 2017-01-12 22:11:32 -0800 | [diff] [blame] | 155 | tp->rack.rtt_us = rtt_us; |
Yuchung Cheng | 659a8ad | 2015-10-16 21:57:46 -0700 | [diff] [blame] | 156 | tp->rack.mstamp = *xmit_time; |
Yuchung Cheng | 1d0833d | 2017-01-12 22:11:34 -0800 | [diff] [blame] | 157 | tp->rack.end_seq = end_seq; |
Yuchung Cheng | 659a8ad | 2015-10-16 21:57:46 -0700 | [diff] [blame] | 158 | tp->rack.advanced = 1; |
| 159 | } |
Yuchung Cheng | 57dde7f | 2017-01-12 22:11:33 -0800 | [diff] [blame] | 160 | |
| 161 | /* We have waited long enough to accommodate reordering. Mark the expired |
| 162 | * packets lost and retransmit them. |
| 163 | */ |
| 164 | void tcp_rack_reo_timeout(struct sock *sk) |
| 165 | { |
| 166 | struct tcp_sock *tp = tcp_sk(sk); |
| 167 | struct skb_mstamp now; |
| 168 | u32 timeout, prior_inflight; |
| 169 | |
| 170 | skb_mstamp_get(&now); |
| 171 | prior_inflight = tcp_packets_in_flight(tp); |
| 172 | tcp_rack_detect_loss(sk, &now, &timeout); |
| 173 | if (prior_inflight != tcp_packets_in_flight(tp)) { |
| 174 | if (inet_csk(sk)->icsk_ca_state != TCP_CA_Recovery) { |
| 175 | tcp_enter_recovery(sk, false); |
| 176 | if (!inet_csk(sk)->icsk_ca_ops->cong_control) |
| 177 | tcp_cwnd_reduction(sk, 1, 0); |
| 178 | } |
| 179 | tcp_xmit_retransmit_queue(sk); |
| 180 | } |
| 181 | if (inet_csk(sk)->icsk_pending != ICSK_TIME_RETRANS) |
| 182 | tcp_rearm_rto(sk); |
| 183 | } |