tcp_recovery.c 5.4 KB

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