162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci#include <net/tcp.h>
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci/* The bandwidth estimator estimates the rate at which the network
562306a36Sopenharmony_ci * can currently deliver outbound data packets for this flow. At a high
662306a36Sopenharmony_ci * level, it operates by taking a delivery rate sample for each ACK.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * A rate sample records the rate at which the network delivered packets
962306a36Sopenharmony_ci * for this flow, calculated over the time interval between the transmission
1062306a36Sopenharmony_ci * of a data packet and the acknowledgment of that packet.
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci * Specifically, over the interval between each transmit and corresponding ACK,
1362306a36Sopenharmony_ci * the estimator generates a delivery rate sample. Typically it uses the rate
1462306a36Sopenharmony_ci * at which packets were acknowledged. However, the approach of using only the
1562306a36Sopenharmony_ci * acknowledgment rate faces a challenge under the prevalent ACK decimation or
1662306a36Sopenharmony_ci * compression: packets can temporarily appear to be delivered much quicker
1762306a36Sopenharmony_ci * than the bottleneck rate. Since it is physically impossible to do that in a
1862306a36Sopenharmony_ci * sustained fashion, when the estimator notices that the ACK rate is faster
1962306a36Sopenharmony_ci * than the transmit rate, it uses the latter:
2062306a36Sopenharmony_ci *
2162306a36Sopenharmony_ci *    send_rate = #pkts_delivered/(last_snd_time - first_snd_time)
2262306a36Sopenharmony_ci *    ack_rate  = #pkts_delivered/(last_ack_time - first_ack_time)
2362306a36Sopenharmony_ci *    bw = min(send_rate, ack_rate)
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * Notice the estimator essentially estimates the goodput, not always the
2662306a36Sopenharmony_ci * network bottleneck link rate when the sending or receiving is limited by
2762306a36Sopenharmony_ci * other factors like applications or receiver window limits.  The estimator
2862306a36Sopenharmony_ci * deliberately avoids using the inter-packet spacing approach because that
2962306a36Sopenharmony_ci * approach requires a large number of samples and sophisticated filtering.
3062306a36Sopenharmony_ci *
3162306a36Sopenharmony_ci * TCP flows can often be application-limited in request/response workloads.
3262306a36Sopenharmony_ci * The estimator marks a bandwidth sample as application-limited if there
3362306a36Sopenharmony_ci * was some moment during the sampled window of packets when there was no data
3462306a36Sopenharmony_ci * ready to send in the write queue.
3562306a36Sopenharmony_ci */
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci/* Snapshot the current delivery information in the skb, to generate
3862306a36Sopenharmony_ci * a rate sample later when the skb is (s)acked in tcp_rate_skb_delivered().
3962306a36Sopenharmony_ci */
4062306a36Sopenharmony_civoid tcp_rate_skb_sent(struct sock *sk, struct sk_buff *skb)
4162306a36Sopenharmony_ci{
4262306a36Sopenharmony_ci	struct tcp_sock *tp = tcp_sk(sk);
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	 /* In general we need to start delivery rate samples from the
4562306a36Sopenharmony_ci	  * time we received the most recent ACK, to ensure we include
4662306a36Sopenharmony_ci	  * the full time the network needs to deliver all in-flight
4762306a36Sopenharmony_ci	  * packets. If there are no packets in flight yet, then we
4862306a36Sopenharmony_ci	  * know that any ACKs after now indicate that the network was
4962306a36Sopenharmony_ci	  * able to deliver those packets completely in the sampling
5062306a36Sopenharmony_ci	  * interval between now and the next ACK.
5162306a36Sopenharmony_ci	  *
5262306a36Sopenharmony_ci	  * Note that we use packets_out instead of tcp_packets_in_flight(tp)
5362306a36Sopenharmony_ci	  * because the latter is a guess based on RTO and loss-marking
5462306a36Sopenharmony_ci	  * heuristics. We don't want spurious RTOs or loss markings to cause
5562306a36Sopenharmony_ci	  * a spuriously small time interval, causing a spuriously high
5662306a36Sopenharmony_ci	  * bandwidth estimate.
5762306a36Sopenharmony_ci	  */
5862306a36Sopenharmony_ci	if (!tp->packets_out) {
5962306a36Sopenharmony_ci		u64 tstamp_us = tcp_skb_timestamp_us(skb);
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci		tp->first_tx_mstamp  = tstamp_us;
6262306a36Sopenharmony_ci		tp->delivered_mstamp = tstamp_us;
6362306a36Sopenharmony_ci	}
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	TCP_SKB_CB(skb)->tx.first_tx_mstamp	= tp->first_tx_mstamp;
6662306a36Sopenharmony_ci	TCP_SKB_CB(skb)->tx.delivered_mstamp	= tp->delivered_mstamp;
6762306a36Sopenharmony_ci	TCP_SKB_CB(skb)->tx.delivered		= tp->delivered;
6862306a36Sopenharmony_ci	TCP_SKB_CB(skb)->tx.delivered_ce	= tp->delivered_ce;
6962306a36Sopenharmony_ci	TCP_SKB_CB(skb)->tx.is_app_limited	= tp->app_limited ? 1 : 0;
7062306a36Sopenharmony_ci}
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci/* When an skb is sacked or acked, we fill in the rate sample with the (prior)
7362306a36Sopenharmony_ci * delivery information when the skb was last transmitted.
7462306a36Sopenharmony_ci *
7562306a36Sopenharmony_ci * If an ACK (s)acks multiple skbs (e.g., stretched-acks), this function is
7662306a36Sopenharmony_ci * called multiple times. We favor the information from the most recently
7762306a36Sopenharmony_ci * sent skb, i.e., the skb with the most recently sent time and the highest
7862306a36Sopenharmony_ci * sequence.
7962306a36Sopenharmony_ci */
8062306a36Sopenharmony_civoid tcp_rate_skb_delivered(struct sock *sk, struct sk_buff *skb,
8162306a36Sopenharmony_ci			    struct rate_sample *rs)
8262306a36Sopenharmony_ci{
8362306a36Sopenharmony_ci	struct tcp_sock *tp = tcp_sk(sk);
8462306a36Sopenharmony_ci	struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
8562306a36Sopenharmony_ci	u64 tx_tstamp;
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	if (!scb->tx.delivered_mstamp)
8862306a36Sopenharmony_ci		return;
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	tx_tstamp = tcp_skb_timestamp_us(skb);
9162306a36Sopenharmony_ci	if (!rs->prior_delivered ||
9262306a36Sopenharmony_ci	    tcp_skb_sent_after(tx_tstamp, tp->first_tx_mstamp,
9362306a36Sopenharmony_ci			       scb->end_seq, rs->last_end_seq)) {
9462306a36Sopenharmony_ci		rs->prior_delivered_ce  = scb->tx.delivered_ce;
9562306a36Sopenharmony_ci		rs->prior_delivered  = scb->tx.delivered;
9662306a36Sopenharmony_ci		rs->prior_mstamp     = scb->tx.delivered_mstamp;
9762306a36Sopenharmony_ci		rs->is_app_limited   = scb->tx.is_app_limited;
9862306a36Sopenharmony_ci		rs->is_retrans	     = scb->sacked & TCPCB_RETRANS;
9962306a36Sopenharmony_ci		rs->last_end_seq     = scb->end_seq;
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci		/* Record send time of most recently ACKed packet: */
10262306a36Sopenharmony_ci		tp->first_tx_mstamp  = tx_tstamp;
10362306a36Sopenharmony_ci		/* Find the duration of the "send phase" of this window: */
10462306a36Sopenharmony_ci		rs->interval_us = tcp_stamp_us_delta(tp->first_tx_mstamp,
10562306a36Sopenharmony_ci						     scb->tx.first_tx_mstamp);
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci	}
10862306a36Sopenharmony_ci	/* Mark off the skb delivered once it's sacked to avoid being
10962306a36Sopenharmony_ci	 * used again when it's cumulatively acked. For acked packets
11062306a36Sopenharmony_ci	 * we don't need to reset since it'll be freed soon.
11162306a36Sopenharmony_ci	 */
11262306a36Sopenharmony_ci	if (scb->sacked & TCPCB_SACKED_ACKED)
11362306a36Sopenharmony_ci		scb->tx.delivered_mstamp = 0;
11462306a36Sopenharmony_ci}
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci/* Update the connection delivery information and generate a rate sample. */
11762306a36Sopenharmony_civoid tcp_rate_gen(struct sock *sk, u32 delivered, u32 lost,
11862306a36Sopenharmony_ci		  bool is_sack_reneg, struct rate_sample *rs)
11962306a36Sopenharmony_ci{
12062306a36Sopenharmony_ci	struct tcp_sock *tp = tcp_sk(sk);
12162306a36Sopenharmony_ci	u32 snd_us, ack_us;
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	/* Clear app limited if bubble is acked and gone. */
12462306a36Sopenharmony_ci	if (tp->app_limited && after(tp->delivered, tp->app_limited))
12562306a36Sopenharmony_ci		tp->app_limited = 0;
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci	/* TODO: there are multiple places throughout tcp_ack() to get
12862306a36Sopenharmony_ci	 * current time. Refactor the code using a new "tcp_acktag_state"
12962306a36Sopenharmony_ci	 * to carry current time, flags, stats like "tcp_sacktag_state".
13062306a36Sopenharmony_ci	 */
13162306a36Sopenharmony_ci	if (delivered)
13262306a36Sopenharmony_ci		tp->delivered_mstamp = tp->tcp_mstamp;
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	rs->acked_sacked = delivered;	/* freshly ACKed or SACKed */
13562306a36Sopenharmony_ci	rs->losses = lost;		/* freshly marked lost */
13662306a36Sopenharmony_ci	/* Return an invalid sample if no timing information is available or
13762306a36Sopenharmony_ci	 * in recovery from loss with SACK reneging. Rate samples taken during
13862306a36Sopenharmony_ci	 * a SACK reneging event may overestimate bw by including packets that
13962306a36Sopenharmony_ci	 * were SACKed before the reneg.
14062306a36Sopenharmony_ci	 */
14162306a36Sopenharmony_ci	if (!rs->prior_mstamp || is_sack_reneg) {
14262306a36Sopenharmony_ci		rs->delivered = -1;
14362306a36Sopenharmony_ci		rs->interval_us = -1;
14462306a36Sopenharmony_ci		return;
14562306a36Sopenharmony_ci	}
14662306a36Sopenharmony_ci	rs->delivered   = tp->delivered - rs->prior_delivered;
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci	rs->delivered_ce = tp->delivered_ce - rs->prior_delivered_ce;
14962306a36Sopenharmony_ci	/* delivered_ce occupies less than 32 bits in the skb control block */
15062306a36Sopenharmony_ci	rs->delivered_ce &= TCPCB_DELIVERED_CE_MASK;
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci	/* Model sending data and receiving ACKs as separate pipeline phases
15362306a36Sopenharmony_ci	 * for a window. Usually the ACK phase is longer, but with ACK
15462306a36Sopenharmony_ci	 * compression the send phase can be longer. To be safe we use the
15562306a36Sopenharmony_ci	 * longer phase.
15662306a36Sopenharmony_ci	 */
15762306a36Sopenharmony_ci	snd_us = rs->interval_us;				/* send phase */
15862306a36Sopenharmony_ci	ack_us = tcp_stamp_us_delta(tp->tcp_mstamp,
15962306a36Sopenharmony_ci				    rs->prior_mstamp); /* ack phase */
16062306a36Sopenharmony_ci	rs->interval_us = max(snd_us, ack_us);
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	/* Record both segment send and ack receive intervals */
16362306a36Sopenharmony_ci	rs->snd_interval_us = snd_us;
16462306a36Sopenharmony_ci	rs->rcv_interval_us = ack_us;
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci	/* Normally we expect interval_us >= min-rtt.
16762306a36Sopenharmony_ci	 * Note that rate may still be over-estimated when a spuriously
16862306a36Sopenharmony_ci	 * retransmistted skb was first (s)acked because "interval_us"
16962306a36Sopenharmony_ci	 * is under-estimated (up to an RTT). However continuously
17062306a36Sopenharmony_ci	 * measuring the delivery rate during loss recovery is crucial
17162306a36Sopenharmony_ci	 * for connections suffer heavy or prolonged losses.
17262306a36Sopenharmony_ci	 */
17362306a36Sopenharmony_ci	if (unlikely(rs->interval_us < tcp_min_rtt(tp))) {
17462306a36Sopenharmony_ci		if (!rs->is_retrans)
17562306a36Sopenharmony_ci			pr_debug("tcp rate: %ld %d %u %u %u\n",
17662306a36Sopenharmony_ci				 rs->interval_us, rs->delivered,
17762306a36Sopenharmony_ci				 inet_csk(sk)->icsk_ca_state,
17862306a36Sopenharmony_ci				 tp->rx_opt.sack_ok, tcp_min_rtt(tp));
17962306a36Sopenharmony_ci		rs->interval_us = -1;
18062306a36Sopenharmony_ci		return;
18162306a36Sopenharmony_ci	}
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci	/* Record the last non-app-limited or the highest app-limited bw */
18462306a36Sopenharmony_ci	if (!rs->is_app_limited ||
18562306a36Sopenharmony_ci	    ((u64)rs->delivered * tp->rate_interval_us >=
18662306a36Sopenharmony_ci	     (u64)tp->rate_delivered * rs->interval_us)) {
18762306a36Sopenharmony_ci		tp->rate_delivered = rs->delivered;
18862306a36Sopenharmony_ci		tp->rate_interval_us = rs->interval_us;
18962306a36Sopenharmony_ci		tp->rate_app_limited = rs->is_app_limited;
19062306a36Sopenharmony_ci	}
19162306a36Sopenharmony_ci}
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci/* If a gap is detected between sends, mark the socket application-limited. */
19462306a36Sopenharmony_civoid tcp_rate_check_app_limited(struct sock *sk)
19562306a36Sopenharmony_ci{
19662306a36Sopenharmony_ci	struct tcp_sock *tp = tcp_sk(sk);
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci	if (/* We have less than one packet to send. */
19962306a36Sopenharmony_ci	    tp->write_seq - tp->snd_nxt < tp->mss_cache &&
20062306a36Sopenharmony_ci	    /* Nothing in sending host's qdisc queues or NIC tx queue. */
20162306a36Sopenharmony_ci	    sk_wmem_alloc_get(sk) < SKB_TRUESIZE(1) &&
20262306a36Sopenharmony_ci	    /* We are not limited by CWND. */
20362306a36Sopenharmony_ci	    tcp_packets_in_flight(tp) < tcp_snd_cwnd(tp) &&
20462306a36Sopenharmony_ci	    /* All lost packets have been retransmitted. */
20562306a36Sopenharmony_ci	    tp->lost_out <= tp->retrans_out)
20662306a36Sopenharmony_ci		tp->app_limited =
20762306a36Sopenharmony_ci			(tp->delivered + tcp_packets_in_flight(tp)) ? : 1;
20862306a36Sopenharmony_ci}
20962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(tcp_rate_check_app_limited);
210