tcp_recovery.c 5.6 KB

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