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