Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #include <net/tcp.h>
0003 
0004 /* The bandwidth estimator estimates the rate at which the network
0005  * can currently deliver outbound data packets for this flow. At a high
0006  * level, it operates by taking a delivery rate sample for each ACK.
0007  *
0008  * A rate sample records the rate at which the network delivered packets
0009  * for this flow, calculated over the time interval between the transmission
0010  * of a data packet and the acknowledgment of that packet.
0011  *
0012  * Specifically, over the interval between each transmit and corresponding ACK,
0013  * the estimator generates a delivery rate sample. Typically it uses the rate
0014  * at which packets were acknowledged. However, the approach of using only the
0015  * acknowledgment rate faces a challenge under the prevalent ACK decimation or
0016  * compression: packets can temporarily appear to be delivered much quicker
0017  * than the bottleneck rate. Since it is physically impossible to do that in a
0018  * sustained fashion, when the estimator notices that the ACK rate is faster
0019  * than the transmit rate, it uses the latter:
0020  *
0021  *    send_rate = #pkts_delivered/(last_snd_time - first_snd_time)
0022  *    ack_rate  = #pkts_delivered/(last_ack_time - first_ack_time)
0023  *    bw = min(send_rate, ack_rate)
0024  *
0025  * Notice the estimator essentially estimates the goodput, not always the
0026  * network bottleneck link rate when the sending or receiving is limited by
0027  * other factors like applications or receiver window limits.  The estimator
0028  * deliberately avoids using the inter-packet spacing approach because that
0029  * approach requires a large number of samples and sophisticated filtering.
0030  *
0031  * TCP flows can often be application-limited in request/response workloads.
0032  * The estimator marks a bandwidth sample as application-limited if there
0033  * was some moment during the sampled window of packets when there was no data
0034  * ready to send in the write queue.
0035  */
0036 
0037 /* Snapshot the current delivery information in the skb, to generate
0038  * a rate sample later when the skb is (s)acked in tcp_rate_skb_delivered().
0039  */
0040 void tcp_rate_skb_sent(struct sock *sk, struct sk_buff *skb)
0041 {
0042     struct tcp_sock *tp = tcp_sk(sk);
0043 
0044      /* In general we need to start delivery rate samples from the
0045       * time we received the most recent ACK, to ensure we include
0046       * the full time the network needs to deliver all in-flight
0047       * packets. If there are no packets in flight yet, then we
0048       * know that any ACKs after now indicate that the network was
0049       * able to deliver those packets completely in the sampling
0050       * interval between now and the next ACK.
0051       *
0052       * Note that we use packets_out instead of tcp_packets_in_flight(tp)
0053       * because the latter is a guess based on RTO and loss-marking
0054       * heuristics. We don't want spurious RTOs or loss markings to cause
0055       * a spuriously small time interval, causing a spuriously high
0056       * bandwidth estimate.
0057       */
0058     if (!tp->packets_out) {
0059         u64 tstamp_us = tcp_skb_timestamp_us(skb);
0060 
0061         tp->first_tx_mstamp  = tstamp_us;
0062         tp->delivered_mstamp = tstamp_us;
0063     }
0064 
0065     TCP_SKB_CB(skb)->tx.first_tx_mstamp = tp->first_tx_mstamp;
0066     TCP_SKB_CB(skb)->tx.delivered_mstamp    = tp->delivered_mstamp;
0067     TCP_SKB_CB(skb)->tx.delivered       = tp->delivered;
0068     TCP_SKB_CB(skb)->tx.delivered_ce    = tp->delivered_ce;
0069     TCP_SKB_CB(skb)->tx.is_app_limited  = tp->app_limited ? 1 : 0;
0070 }
0071 
0072 /* When an skb is sacked or acked, we fill in the rate sample with the (prior)
0073  * delivery information when the skb was last transmitted.
0074  *
0075  * If an ACK (s)acks multiple skbs (e.g., stretched-acks), this function is
0076  * called multiple times. We favor the information from the most recently
0077  * sent skb, i.e., the skb with the most recently sent time and the highest
0078  * sequence.
0079  */
0080 void tcp_rate_skb_delivered(struct sock *sk, struct sk_buff *skb,
0081                 struct rate_sample *rs)
0082 {
0083     struct tcp_sock *tp = tcp_sk(sk);
0084     struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
0085     u64 tx_tstamp;
0086 
0087     if (!scb->tx.delivered_mstamp)
0088         return;
0089 
0090     tx_tstamp = tcp_skb_timestamp_us(skb);
0091     if (!rs->prior_delivered ||
0092         tcp_skb_sent_after(tx_tstamp, tp->first_tx_mstamp,
0093                    scb->end_seq, rs->last_end_seq)) {
0094         rs->prior_delivered_ce  = scb->tx.delivered_ce;
0095         rs->prior_delivered  = scb->tx.delivered;
0096         rs->prior_mstamp     = scb->tx.delivered_mstamp;
0097         rs->is_app_limited   = scb->tx.is_app_limited;
0098         rs->is_retrans       = scb->sacked & TCPCB_RETRANS;
0099         rs->last_end_seq     = scb->end_seq;
0100 
0101         /* Record send time of most recently ACKed packet: */
0102         tp->first_tx_mstamp  = tx_tstamp;
0103         /* Find the duration of the "send phase" of this window: */
0104         rs->interval_us = tcp_stamp_us_delta(tp->first_tx_mstamp,
0105                              scb->tx.first_tx_mstamp);
0106 
0107     }
0108     /* Mark off the skb delivered once it's sacked to avoid being
0109      * used again when it's cumulatively acked. For acked packets
0110      * we don't need to reset since it'll be freed soon.
0111      */
0112     if (scb->sacked & TCPCB_SACKED_ACKED)
0113         scb->tx.delivered_mstamp = 0;
0114 }
0115 
0116 /* Update the connection delivery information and generate a rate sample. */
0117 void tcp_rate_gen(struct sock *sk, u32 delivered, u32 lost,
0118           bool is_sack_reneg, struct rate_sample *rs)
0119 {
0120     struct tcp_sock *tp = tcp_sk(sk);
0121     u32 snd_us, ack_us;
0122 
0123     /* Clear app limited if bubble is acked and gone. */
0124     if (tp->app_limited && after(tp->delivered, tp->app_limited))
0125         tp->app_limited = 0;
0126 
0127     /* TODO: there are multiple places throughout tcp_ack() to get
0128      * current time. Refactor the code using a new "tcp_acktag_state"
0129      * to carry current time, flags, stats like "tcp_sacktag_state".
0130      */
0131     if (delivered)
0132         tp->delivered_mstamp = tp->tcp_mstamp;
0133 
0134     rs->acked_sacked = delivered;   /* freshly ACKed or SACKed */
0135     rs->losses = lost;      /* freshly marked lost */
0136     /* Return an invalid sample if no timing information is available or
0137      * in recovery from loss with SACK reneging. Rate samples taken during
0138      * a SACK reneging event may overestimate bw by including packets that
0139      * were SACKed before the reneg.
0140      */
0141     if (!rs->prior_mstamp || is_sack_reneg) {
0142         rs->delivered = -1;
0143         rs->interval_us = -1;
0144         return;
0145     }
0146     rs->delivered   = tp->delivered - rs->prior_delivered;
0147 
0148     rs->delivered_ce = tp->delivered_ce - rs->prior_delivered_ce;
0149     /* delivered_ce occupies less than 32 bits in the skb control block */
0150     rs->delivered_ce &= TCPCB_DELIVERED_CE_MASK;
0151 
0152     /* Model sending data and receiving ACKs as separate pipeline phases
0153      * for a window. Usually the ACK phase is longer, but with ACK
0154      * compression the send phase can be longer. To be safe we use the
0155      * longer phase.
0156      */
0157     snd_us = rs->interval_us;               /* send phase */
0158     ack_us = tcp_stamp_us_delta(tp->tcp_mstamp,
0159                     rs->prior_mstamp); /* ack phase */
0160     rs->interval_us = max(snd_us, ack_us);
0161 
0162     /* Record both segment send and ack receive intervals */
0163     rs->snd_interval_us = snd_us;
0164     rs->rcv_interval_us = ack_us;
0165 
0166     /* Normally we expect interval_us >= min-rtt.
0167      * Note that rate may still be over-estimated when a spuriously
0168      * retransmistted skb was first (s)acked because "interval_us"
0169      * is under-estimated (up to an RTT). However continuously
0170      * measuring the delivery rate during loss recovery is crucial
0171      * for connections suffer heavy or prolonged losses.
0172      */
0173     if (unlikely(rs->interval_us < tcp_min_rtt(tp))) {
0174         if (!rs->is_retrans)
0175             pr_debug("tcp rate: %ld %d %u %u %u\n",
0176                  rs->interval_us, rs->delivered,
0177                  inet_csk(sk)->icsk_ca_state,
0178                  tp->rx_opt.sack_ok, tcp_min_rtt(tp));
0179         rs->interval_us = -1;
0180         return;
0181     }
0182 
0183     /* Record the last non-app-limited or the highest app-limited bw */
0184     if (!rs->is_app_limited ||
0185         ((u64)rs->delivered * tp->rate_interval_us >=
0186          (u64)tp->rate_delivered * rs->interval_us)) {
0187         tp->rate_delivered = rs->delivered;
0188         tp->rate_interval_us = rs->interval_us;
0189         tp->rate_app_limited = rs->is_app_limited;
0190     }
0191 }
0192 
0193 /* If a gap is detected between sends, mark the socket application-limited. */
0194 void tcp_rate_check_app_limited(struct sock *sk)
0195 {
0196     struct tcp_sock *tp = tcp_sk(sk);
0197 
0198     if (/* We have less than one packet to send. */
0199         tp->write_seq - tp->snd_nxt < tp->mss_cache &&
0200         /* Nothing in sending host's qdisc queues or NIC tx queue. */
0201         sk_wmem_alloc_get(sk) < SKB_TRUESIZE(1) &&
0202         /* We are not limited by CWND. */
0203         tcp_packets_in_flight(tp) < tcp_snd_cwnd(tp) &&
0204         /* All lost packets have been retransmitted. */
0205         tp->lost_out <= tp->retrans_out)
0206         tp->app_limited =
0207             (tp->delivered + tcp_packets_in_flight(tp)) ? : 1;
0208 }
0209 EXPORT_SYMBOL_GPL(tcp_rate_check_app_limited);