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