tcp_recovery.c 5.1 KB

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