tcp_rate.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include <net/tcp.h>
  2. /* The bandwidth estimator estimates the rate at which the network
  3. * can currently deliver outbound data packets for this flow. At a high
  4. * level, it operates by taking a delivery rate sample for each ACK.
  5. *
  6. * A rate sample records the rate at which the network delivered packets
  7. * for this flow, calculated over the time interval between the transmission
  8. * of a data packet and the acknowledgment of that packet.
  9. *
  10. * Specifically, over the interval between each transmit and corresponding ACK,
  11. * the estimator generates a delivery rate sample. Typically it uses the rate
  12. * at which packets were acknowledged. However, the approach of using only the
  13. * acknowledgment rate faces a challenge under the prevalent ACK decimation or
  14. * compression: packets can temporarily appear to be delivered much quicker
  15. * than the bottleneck rate. Since it is physically impossible to do that in a
  16. * sustained fashion, when the estimator notices that the ACK rate is faster
  17. * than the transmit rate, it uses the latter:
  18. *
  19. * send_rate = #pkts_delivered/(last_snd_time - first_snd_time)
  20. * ack_rate = #pkts_delivered/(last_ack_time - first_ack_time)
  21. * bw = min(send_rate, ack_rate)
  22. *
  23. * Notice the estimator essentially estimates the goodput, not always the
  24. * network bottleneck link rate when the sending or receiving is limited by
  25. * other factors like applications or receiver window limits. The estimator
  26. * deliberately avoids using the inter-packet spacing approach because that
  27. * approach requires a large number of samples and sophisticated filtering.
  28. *
  29. * TCP flows can often be application-limited in request/response workloads.
  30. * The estimator marks a bandwidth sample as application-limited if there
  31. * was some moment during the sampled window of packets when there was no data
  32. * ready to send in the write queue.
  33. */
  34. /* Snapshot the current delivery information in the skb, to generate
  35. * a rate sample later when the skb is (s)acked in tcp_rate_skb_delivered().
  36. */
  37. void tcp_rate_skb_sent(struct sock *sk, struct sk_buff *skb)
  38. {
  39. struct tcp_sock *tp = tcp_sk(sk);
  40. /* In general we need to start delivery rate samples from the
  41. * time we received the most recent ACK, to ensure we include
  42. * the full time the network needs to deliver all in-flight
  43. * packets. If there are no packets in flight yet, then we
  44. * know that any ACKs after now indicate that the network was
  45. * able to deliver those packets completely in the sampling
  46. * interval between now and the next ACK.
  47. *
  48. * Note that we use packets_out instead of tcp_packets_in_flight(tp)
  49. * because the latter is a guess based on RTO and loss-marking
  50. * heuristics. We don't want spurious RTOs or loss markings to cause
  51. * a spuriously small time interval, causing a spuriously high
  52. * bandwidth estimate.
  53. */
  54. if (!tp->packets_out) {
  55. tp->first_tx_mstamp = skb->skb_mstamp;
  56. tp->delivered_mstamp = skb->skb_mstamp;
  57. }
  58. TCP_SKB_CB(skb)->tx.first_tx_mstamp = tp->first_tx_mstamp;
  59. TCP_SKB_CB(skb)->tx.delivered_mstamp = tp->delivered_mstamp;
  60. TCP_SKB_CB(skb)->tx.delivered = tp->delivered;
  61. TCP_SKB_CB(skb)->tx.is_app_limited = tp->app_limited ? 1 : 0;
  62. }
  63. /* When an skb is sacked or acked, we fill in the rate sample with the (prior)
  64. * delivery information when the skb was last transmitted.
  65. *
  66. * If an ACK (s)acks multiple skbs (e.g., stretched-acks), this function is
  67. * called multiple times. We favor the information from the most recently
  68. * sent skb, i.e., the skb with the highest prior_delivered count.
  69. */
  70. void tcp_rate_skb_delivered(struct sock *sk, struct sk_buff *skb,
  71. struct rate_sample *rs)
  72. {
  73. struct tcp_sock *tp = tcp_sk(sk);
  74. struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
  75. if (!scb->tx.delivered_mstamp.v64)
  76. return;
  77. if (!rs->prior_delivered ||
  78. after(scb->tx.delivered, rs->prior_delivered)) {
  79. rs->prior_delivered = scb->tx.delivered;
  80. rs->prior_mstamp = scb->tx.delivered_mstamp;
  81. rs->is_app_limited = scb->tx.is_app_limited;
  82. rs->is_retrans = scb->sacked & TCPCB_RETRANS;
  83. /* Find the duration of the "send phase" of this window: */
  84. rs->interval_us = skb_mstamp_us_delta(
  85. &skb->skb_mstamp,
  86. &scb->tx.first_tx_mstamp);
  87. /* Record send time of most recently ACKed packet: */
  88. tp->first_tx_mstamp = skb->skb_mstamp;
  89. }
  90. /* Mark off the skb delivered once it's sacked to avoid being
  91. * used again when it's cumulatively acked. For acked packets
  92. * we don't need to reset since it'll be freed soon.
  93. */
  94. if (scb->sacked & TCPCB_SACKED_ACKED)
  95. scb->tx.delivered_mstamp.v64 = 0;
  96. }
  97. /* Update the connection delivery information and generate a rate sample. */
  98. void tcp_rate_gen(struct sock *sk, u32 delivered, u32 lost,
  99. bool is_sack_reneg, struct skb_mstamp *now, struct rate_sample *rs)
  100. {
  101. struct tcp_sock *tp = tcp_sk(sk);
  102. u32 snd_us, ack_us;
  103. /* Clear app limited if bubble is acked and gone. */
  104. if (tp->app_limited && after(tp->delivered, tp->app_limited))
  105. tp->app_limited = 0;
  106. /* TODO: there are multiple places throughout tcp_ack() to get
  107. * current time. Refactor the code using a new "tcp_acktag_state"
  108. * to carry current time, flags, stats like "tcp_sacktag_state".
  109. */
  110. if (delivered)
  111. tp->delivered_mstamp = *now;
  112. rs->acked_sacked = delivered; /* freshly ACKed or SACKed */
  113. rs->losses = lost; /* freshly marked lost */
  114. /* Return an invalid sample if no timing information is available or
  115. * in recovery from loss with SACK reneging. Rate samples taken during
  116. * a SACK reneging event may overestimate bw by including packets that
  117. * were SACKed before the reneg.
  118. */
  119. if (!rs->prior_mstamp.v64 || is_sack_reneg) {
  120. rs->delivered = -1;
  121. rs->interval_us = -1;
  122. return;
  123. }
  124. rs->delivered = tp->delivered - rs->prior_delivered;
  125. /* Model sending data and receiving ACKs as separate pipeline phases
  126. * for a window. Usually the ACK phase is longer, but with ACK
  127. * compression the send phase can be longer. To be safe we use the
  128. * longer phase.
  129. */
  130. snd_us = rs->interval_us; /* send phase */
  131. ack_us = skb_mstamp_us_delta(now, &rs->prior_mstamp); /* ack phase */
  132. rs->interval_us = max(snd_us, ack_us);
  133. /* Normally we expect interval_us >= min-rtt.
  134. * Note that rate may still be over-estimated when a spuriously
  135. * retransmistted skb was first (s)acked because "interval_us"
  136. * is under-estimated (up to an RTT). However continuously
  137. * measuring the delivery rate during loss recovery is crucial
  138. * for connections suffer heavy or prolonged losses.
  139. */
  140. if (unlikely(rs->interval_us < tcp_min_rtt(tp))) {
  141. if (!rs->is_retrans)
  142. pr_debug("tcp rate: %ld %d %u %u %u\n",
  143. rs->interval_us, rs->delivered,
  144. inet_csk(sk)->icsk_ca_state,
  145. tp->rx_opt.sack_ok, tcp_min_rtt(tp));
  146. rs->interval_us = -1;
  147. return;
  148. }
  149. /* Record the last non-app-limited or the highest app-limited bw */
  150. if (!rs->is_app_limited ||
  151. ((u64)rs->delivered * tp->rate_interval_us >=
  152. (u64)tp->rate_delivered * rs->interval_us)) {
  153. tp->rate_delivered = rs->delivered;
  154. tp->rate_interval_us = rs->interval_us;
  155. tp->rate_app_limited = rs->is_app_limited;
  156. }
  157. }
  158. /* If a gap is detected between sends, mark the socket application-limited. */
  159. void tcp_rate_check_app_limited(struct sock *sk)
  160. {
  161. struct tcp_sock *tp = tcp_sk(sk);
  162. if (/* We have less than one packet to send. */
  163. tp->write_seq - tp->snd_nxt < tp->mss_cache &&
  164. /* Nothing in sending host's qdisc queues or NIC tx queue. */
  165. sk_wmem_alloc_get(sk) < SKB_TRUESIZE(1) &&
  166. /* We are not limited by CWND. */
  167. tcp_packets_in_flight(tp) < tp->snd_cwnd &&
  168. /* All lost packets have been retransmitted. */
  169. tp->lost_out <= tp->retrans_out)
  170. tp->app_limited =
  171. (tp->delivered + tcp_packets_in_flight(tp)) ? : 1;
  172. }