Blame view

net/ipv4/tcp_rate.c 7.81 KB
457c89965   Thomas Gleixner   treewide: Add SPD...
1
  // SPDX-License-Identifier: GPL-2.0-only
b9f64820f   Yuchung Cheng   tcp: track data d...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  #include <net/tcp.h>
  
  /* The bandwidth estimator estimates the rate at which the network
   * can currently deliver outbound data packets for this flow. At a high
   * level, it operates by taking a delivery rate sample for each ACK.
   *
   * A rate sample records the rate at which the network delivered packets
   * for this flow, calculated over the time interval between the transmission
   * of a data packet and the acknowledgment of that packet.
   *
   * Specifically, over the interval between each transmit and corresponding ACK,
   * the estimator generates a delivery rate sample. Typically it uses the rate
   * at which packets were acknowledged. However, the approach of using only the
   * acknowledgment rate faces a challenge under the prevalent ACK decimation or
   * compression: packets can temporarily appear to be delivered much quicker
   * than the bottleneck rate. Since it is physically impossible to do that in a
   * sustained fashion, when the estimator notices that the ACK rate is faster
   * than the transmit rate, it uses the latter:
   *
   *    send_rate = #pkts_delivered/(last_snd_time - first_snd_time)
   *    ack_rate  = #pkts_delivered/(last_ack_time - first_ack_time)
   *    bw = min(send_rate, ack_rate)
   *
   * Notice the estimator essentially estimates the goodput, not always the
   * network bottleneck link rate when the sending or receiving is limited by
   * other factors like applications or receiver window limits.  The estimator
   * deliberately avoids using the inter-packet spacing approach because that
   * approach requires a large number of samples and sophisticated filtering.
d7722e857   Soheil Hassas Yeganeh   tcp: track applic...
30
31
32
33
34
   *
   * TCP flows can often be application-limited in request/response workloads.
   * The estimator marks a bandwidth sample as application-limited if there
   * was some moment during the sampled window of packets when there was no data
   * ready to send in the write queue.
b9f64820f   Yuchung Cheng   tcp: track data d...
35
   */
b9f64820f   Yuchung Cheng   tcp: track data d...
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  /* Snapshot the current delivery information in the skb, to generate
   * a rate sample later when the skb is (s)acked in tcp_rate_skb_delivered().
   */
  void tcp_rate_skb_sent(struct sock *sk, struct sk_buff *skb)
  {
  	struct tcp_sock *tp = tcp_sk(sk);
  
  	 /* In general we need to start delivery rate samples from the
  	  * time we received the most recent ACK, to ensure we include
  	  * the full time the network needs to deliver all in-flight
  	  * packets. If there are no packets in flight yet, then we
  	  * know that any ACKs after now indicate that the network was
  	  * able to deliver those packets completely in the sampling
  	  * interval between now and the next ACK.
  	  *
  	  * Note that we use packets_out instead of tcp_packets_in_flight(tp)
  	  * because the latter is a guess based on RTO and loss-marking
  	  * heuristics. We don't want spurious RTOs or loss markings to cause
  	  * a spuriously small time interval, causing a spuriously high
  	  * bandwidth estimate.
  	  */
  	if (!tp->packets_out) {
2fd66ffba   Eric Dumazet   tcp: introduce tc...
58
59
60
61
  		u64 tstamp_us = tcp_skb_timestamp_us(skb);
  
  		tp->first_tx_mstamp  = tstamp_us;
  		tp->delivered_mstamp = tstamp_us;
b9f64820f   Yuchung Cheng   tcp: track data d...
62
63
64
65
66
  	}
  
  	TCP_SKB_CB(skb)->tx.first_tx_mstamp	= tp->first_tx_mstamp;
  	TCP_SKB_CB(skb)->tx.delivered_mstamp	= tp->delivered_mstamp;
  	TCP_SKB_CB(skb)->tx.delivered		= tp->delivered;
d7722e857   Soheil Hassas Yeganeh   tcp: track applic...
67
  	TCP_SKB_CB(skb)->tx.is_app_limited	= tp->app_limited ? 1 : 0;
b9f64820f   Yuchung Cheng   tcp: track data d...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  }
  
  /* When an skb is sacked or acked, we fill in the rate sample with the (prior)
   * delivery information when the skb was last transmitted.
   *
   * If an ACK (s)acks multiple skbs (e.g., stretched-acks), this function is
   * called multiple times. We favor the information from the most recently
   * sent skb, i.e., the skb with the highest prior_delivered count.
   */
  void tcp_rate_skb_delivered(struct sock *sk, struct sk_buff *skb,
  			    struct rate_sample *rs)
  {
  	struct tcp_sock *tp = tcp_sk(sk);
  	struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
9a568de48   Eric Dumazet   tcp: switch TCP T...
82
  	if (!scb->tx.delivered_mstamp)
b9f64820f   Yuchung Cheng   tcp: track data d...
83
84
85
86
87
88
  		return;
  
  	if (!rs->prior_delivered ||
  	    after(scb->tx.delivered, rs->prior_delivered)) {
  		rs->prior_delivered  = scb->tx.delivered;
  		rs->prior_mstamp     = scb->tx.delivered_mstamp;
d7722e857   Soheil Hassas Yeganeh   tcp: track applic...
89
  		rs->is_app_limited   = scb->tx.is_app_limited;
b9f64820f   Yuchung Cheng   tcp: track data d...
90
  		rs->is_retrans	     = scb->sacked & TCPCB_RETRANS;
2fd66ffba   Eric Dumazet   tcp: introduce tc...
91
92
  		/* Record send time of most recently ACKed packet: */
  		tp->first_tx_mstamp  = tcp_skb_timestamp_us(skb);
b9f64820f   Yuchung Cheng   tcp: track data d...
93
  		/* Find the duration of the "send phase" of this window: */
2fd66ffba   Eric Dumazet   tcp: introduce tc...
94
95
  		rs->interval_us = tcp_stamp_us_delta(tp->first_tx_mstamp,
  						     scb->tx.first_tx_mstamp);
b9f64820f   Yuchung Cheng   tcp: track data d...
96

b9f64820f   Yuchung Cheng   tcp: track data d...
97
98
99
100
101
102
  	}
  	/* Mark off the skb delivered once it's sacked to avoid being
  	 * used again when it's cumulatively acked. For acked packets
  	 * we don't need to reset since it'll be freed soon.
  	 */
  	if (scb->sacked & TCPCB_SACKED_ACKED)
9a568de48   Eric Dumazet   tcp: switch TCP T...
103
  		scb->tx.delivered_mstamp = 0;
b9f64820f   Yuchung Cheng   tcp: track data d...
104
105
106
107
  }
  
  /* Update the connection delivery information and generate a rate sample. */
  void tcp_rate_gen(struct sock *sk, u32 delivered, u32 lost,
d4761754b   Yousuk Seung   tcp: invalidate r...
108
  		  bool is_sack_reneg, struct rate_sample *rs)
b9f64820f   Yuchung Cheng   tcp: track data d...
109
110
111
  {
  	struct tcp_sock *tp = tcp_sk(sk);
  	u32 snd_us, ack_us;
d7722e857   Soheil Hassas Yeganeh   tcp: track applic...
112
113
114
  	/* Clear app limited if bubble is acked and gone. */
  	if (tp->app_limited && after(tp->delivered, tp->app_limited))
  		tp->app_limited = 0;
b9f64820f   Yuchung Cheng   tcp: track data d...
115
116
117
118
119
  	/* TODO: there are multiple places throughout tcp_ack() to get
  	 * current time. Refactor the code using a new "tcp_acktag_state"
  	 * to carry current time, flags, stats like "tcp_sacktag_state".
  	 */
  	if (delivered)
88d5c6509   Eric Dumazet   tcp: do not pass ...
120
  		tp->delivered_mstamp = tp->tcp_mstamp;
b9f64820f   Yuchung Cheng   tcp: track data d...
121
122
123
  
  	rs->acked_sacked = delivered;	/* freshly ACKed or SACKed */
  	rs->losses = lost;		/* freshly marked lost */
d4761754b   Yousuk Seung   tcp: invalidate r...
124
125
126
127
128
129
  	/* Return an invalid sample if no timing information is available or
  	 * in recovery from loss with SACK reneging. Rate samples taken during
  	 * a SACK reneging event may overestimate bw by including packets that
  	 * were SACKed before the reneg.
  	 */
  	if (!rs->prior_mstamp || is_sack_reneg) {
b9f64820f   Yuchung Cheng   tcp: track data d...
130
131
132
133
134
135
136
137
138
139
140
141
  		rs->delivered = -1;
  		rs->interval_us = -1;
  		return;
  	}
  	rs->delivered   = tp->delivered - rs->prior_delivered;
  
  	/* Model sending data and receiving ACKs as separate pipeline phases
  	 * for a window. Usually the ACK phase is longer, but with ACK
  	 * compression the send phase can be longer. To be safe we use the
  	 * longer phase.
  	 */
  	snd_us = rs->interval_us;				/* send phase */
9a568de48   Eric Dumazet   tcp: switch TCP T...
142
143
  	ack_us = tcp_stamp_us_delta(tp->tcp_mstamp,
  				    rs->prior_mstamp); /* ack phase */
b9f64820f   Yuchung Cheng   tcp: track data d...
144
  	rs->interval_us = max(snd_us, ack_us);
4929c9428   Deepti Raghavan   tcp: expose both ...
145
146
147
  	/* Record both segment send and ack receive intervals */
  	rs->snd_interval_us = snd_us;
  	rs->rcv_interval_us = ack_us;
b9f64820f   Yuchung Cheng   tcp: track data d...
148
149
150
151
152
153
154
155
  	/* Normally we expect interval_us >= min-rtt.
  	 * Note that rate may still be over-estimated when a spuriously
  	 * retransmistted skb was first (s)acked because "interval_us"
  	 * is under-estimated (up to an RTT). However continuously
  	 * measuring the delivery rate during loss recovery is crucial
  	 * for connections suffer heavy or prolonged losses.
  	 */
  	if (unlikely(rs->interval_us < tcp_min_rtt(tp))) {
b9f64820f   Yuchung Cheng   tcp: track data d...
156
157
158
159
160
161
  		if (!rs->is_retrans)
  			pr_debug("tcp rate: %ld %d %u %u %u
  ",
  				 rs->interval_us, rs->delivered,
  				 inet_csk(sk)->icsk_ca_state,
  				 tp->rx_opt.sack_ok, tcp_min_rtt(tp));
eb8329e0a   Yuchung Cheng   tcp: export data ...
162
163
164
165
166
167
168
169
170
171
172
  		rs->interval_us = -1;
  		return;
  	}
  
  	/* Record the last non-app-limited or the highest app-limited bw */
  	if (!rs->is_app_limited ||
  	    ((u64)rs->delivered * tp->rate_interval_us >=
  	     (u64)tp->rate_delivered * rs->interval_us)) {
  		tp->rate_delivered = rs->delivered;
  		tp->rate_interval_us = rs->interval_us;
  		tp->rate_app_limited = rs->is_app_limited;
b9f64820f   Yuchung Cheng   tcp: track data d...
173
174
  	}
  }
d7722e857   Soheil Hassas Yeganeh   tcp: track applic...
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
  
  /* If a gap is detected between sends, mark the socket application-limited. */
  void tcp_rate_check_app_limited(struct sock *sk)
  {
  	struct tcp_sock *tp = tcp_sk(sk);
  
  	if (/* We have less than one packet to send. */
  	    tp->write_seq - tp->snd_nxt < tp->mss_cache &&
  	    /* Nothing in sending host's qdisc queues or NIC tx queue. */
  	    sk_wmem_alloc_get(sk) < SKB_TRUESIZE(1) &&
  	    /* We are not limited by CWND. */
  	    tcp_packets_in_flight(tp) < tp->snd_cwnd &&
  	    /* All lost packets have been retransmitted. */
  	    tp->lost_out <= tp->retrans_out)
  		tp->app_limited =
  			(tp->delivered + tcp_packets_in_flight(tp)) ? : 1;
  }
e3b5616a3   Dave Watson   tcp: export do_tc...
192
  EXPORT_SYMBOL_GPL(tcp_rate_check_app_limited);