xref: /linux/net/ipv4/tcp_bbr.c (revision c589e69b508d29ed8e644dfecda453f71c02ec27)
10f8782eaSNeal Cardwell /* Bottleneck Bandwidth and RTT (BBR) congestion control
20f8782eaSNeal Cardwell  *
30f8782eaSNeal Cardwell  * BBR congestion control computes the sending rate based on the delivery
40f8782eaSNeal Cardwell  * rate (throughput) estimated from ACKs. In a nutshell:
50f8782eaSNeal Cardwell  *
60f8782eaSNeal Cardwell  *   On each ACK, update our model of the network path:
70f8782eaSNeal Cardwell  *      bottleneck_bandwidth = windowed_max(delivered / elapsed, 10 round trips)
80f8782eaSNeal Cardwell  *      min_rtt = windowed_min(rtt, 10 seconds)
90f8782eaSNeal Cardwell  *   pacing_rate = pacing_gain * bottleneck_bandwidth
100f8782eaSNeal Cardwell  *   cwnd = max(cwnd_gain * bottleneck_bandwidth * min_rtt, 4)
110f8782eaSNeal Cardwell  *
120f8782eaSNeal Cardwell  * The core algorithm does not react directly to packet losses or delays,
130f8782eaSNeal Cardwell  * although BBR may adjust the size of next send per ACK when loss is
140f8782eaSNeal Cardwell  * observed, or adjust the sending rate if it estimates there is a
150f8782eaSNeal Cardwell  * traffic policer, in order to keep the drop rate reasonable.
160f8782eaSNeal Cardwell  *
179b9375b5SNeal Cardwell  * Here is a state transition diagram for BBR:
189b9375b5SNeal Cardwell  *
199b9375b5SNeal Cardwell  *             |
209b9375b5SNeal Cardwell  *             V
219b9375b5SNeal Cardwell  *    +---> STARTUP  ----+
229b9375b5SNeal Cardwell  *    |        |         |
239b9375b5SNeal Cardwell  *    |        V         |
249b9375b5SNeal Cardwell  *    |      DRAIN   ----+
259b9375b5SNeal Cardwell  *    |        |         |
269b9375b5SNeal Cardwell  *    |        V         |
279b9375b5SNeal Cardwell  *    +---> PROBE_BW ----+
289b9375b5SNeal Cardwell  *    |      ^    |      |
299b9375b5SNeal Cardwell  *    |      |    |      |
309b9375b5SNeal Cardwell  *    |      +----+      |
319b9375b5SNeal Cardwell  *    |                  |
329b9375b5SNeal Cardwell  *    +---- PROBE_RTT <--+
339b9375b5SNeal Cardwell  *
349b9375b5SNeal Cardwell  * A BBR flow starts in STARTUP, and ramps up its sending rate quickly.
359b9375b5SNeal Cardwell  * When it estimates the pipe is full, it enters DRAIN to drain the queue.
369b9375b5SNeal Cardwell  * In steady state a BBR flow only uses PROBE_BW and PROBE_RTT.
379b9375b5SNeal Cardwell  * A long-lived BBR flow spends the vast majority of its time remaining
389b9375b5SNeal Cardwell  * (repeatedly) in PROBE_BW, fully probing and utilizing the pipe's bandwidth
399b9375b5SNeal Cardwell  * in a fair manner, with a small, bounded queue. *If* a flow has been
409b9375b5SNeal Cardwell  * continuously sending for the entire min_rtt window, and hasn't seen an RTT
419b9375b5SNeal Cardwell  * sample that matches or decreases its min_rtt estimate for 10 seconds, then
429b9375b5SNeal Cardwell  * it briefly enters PROBE_RTT to cut inflight to a minimum value to re-probe
439b9375b5SNeal Cardwell  * the path's two-way propagation delay (min_rtt). When exiting PROBE_RTT, if
449b9375b5SNeal Cardwell  * we estimated that we reached the full bw of the pipe then we enter PROBE_BW;
459b9375b5SNeal Cardwell  * otherwise we enter STARTUP to try to fill the pipe.
469b9375b5SNeal Cardwell  *
470f8782eaSNeal Cardwell  * BBR is described in detail in:
480f8782eaSNeal Cardwell  *   "BBR: Congestion-Based Congestion Control",
490f8782eaSNeal Cardwell  *   Neal Cardwell, Yuchung Cheng, C. Stephen Gunn, Soheil Hassas Yeganeh,
500f8782eaSNeal Cardwell  *   Van Jacobson. ACM Queue, Vol. 14 No. 5, September-October 2016.
510f8782eaSNeal Cardwell  *
520f8782eaSNeal Cardwell  * There is a public e-mail list for discussing BBR development and testing:
530f8782eaSNeal Cardwell  *   https://groups.google.com/forum/#!forum/bbr-dev
540f8782eaSNeal Cardwell  *
55218af599SEric Dumazet  * NOTE: BBR might be used with the fq qdisc ("man tc-fq") with pacing enabled,
56218af599SEric Dumazet  * otherwise TCP stack falls back to an internal pacing using one high
57218af599SEric Dumazet  * resolution timer per TCP socket and may use more resources.
580f8782eaSNeal Cardwell  */
590f8782eaSNeal Cardwell #include <linux/module.h>
600f8782eaSNeal Cardwell #include <net/tcp.h>
610f8782eaSNeal Cardwell #include <linux/inet_diag.h>
620f8782eaSNeal Cardwell #include <linux/inet.h>
630f8782eaSNeal Cardwell #include <linux/random.h>
640f8782eaSNeal Cardwell #include <linux/win_minmax.h>
650f8782eaSNeal Cardwell 
660f8782eaSNeal Cardwell /* Scale factor for rate in pkt/uSec unit to avoid truncation in bandwidth
670f8782eaSNeal Cardwell  * estimation. The rate unit ~= (1500 bytes / 1 usec / 2^24) ~= 715 bps.
680f8782eaSNeal Cardwell  * This handles bandwidths from 0.06pps (715bps) to 256Mpps (3Tbps) in a u32.
690f8782eaSNeal Cardwell  * Since the minimum window is >=4 packets, the lower bound isn't
700f8782eaSNeal Cardwell  * an issue. The upper bound isn't an issue with existing technologies.
710f8782eaSNeal Cardwell  */
720f8782eaSNeal Cardwell #define BW_SCALE 24
730f8782eaSNeal Cardwell #define BW_UNIT (1 << BW_SCALE)
740f8782eaSNeal Cardwell 
750f8782eaSNeal Cardwell #define BBR_SCALE 8	/* scaling factor for fractions in BBR (e.g. gains) */
760f8782eaSNeal Cardwell #define BBR_UNIT (1 << BBR_SCALE)
770f8782eaSNeal Cardwell 
780f8782eaSNeal Cardwell /* BBR has the following modes for deciding how fast to send: */
790f8782eaSNeal Cardwell enum bbr_mode {
800f8782eaSNeal Cardwell 	BBR_STARTUP,	/* ramp up sending rate rapidly to fill pipe */
810f8782eaSNeal Cardwell 	BBR_DRAIN,	/* drain any queue created during startup */
820f8782eaSNeal Cardwell 	BBR_PROBE_BW,	/* discover, share bw: pace around estimated bw */
839b9375b5SNeal Cardwell 	BBR_PROBE_RTT,	/* cut inflight to min to probe min_rtt */
840f8782eaSNeal Cardwell };
850f8782eaSNeal Cardwell 
860f8782eaSNeal Cardwell /* BBR congestion control block */
870f8782eaSNeal Cardwell struct bbr {
880f8782eaSNeal Cardwell 	u32	min_rtt_us;	        /* min RTT in min_rtt_win_sec window */
890f8782eaSNeal Cardwell 	u32	min_rtt_stamp;	        /* timestamp of min_rtt_us */
900f8782eaSNeal Cardwell 	u32	probe_rtt_done_stamp;   /* end time for BBR_PROBE_RTT mode */
910f8782eaSNeal Cardwell 	struct minmax bw;	/* Max recent delivery rate in pkts/uS << 24 */
920f8782eaSNeal Cardwell 	u32	rtt_cnt;	    /* count of packet-timed rounds elapsed */
930f8782eaSNeal Cardwell 	u32     next_rtt_delivered; /* scb->tx.delivered at end of round */
949a568de4SEric Dumazet 	u64	cycle_mstamp;	     /* time of this cycle phase start */
950f8782eaSNeal Cardwell 	u32     mode:3,		     /* current bbr_mode in state machine */
960f8782eaSNeal Cardwell 		prev_ca_state:3,     /* CA state on previous ACK */
970f8782eaSNeal Cardwell 		packet_conservation:1,  /* use packet conservation? */
980f8782eaSNeal Cardwell 		restore_cwnd:1,	     /* decided to revert cwnd to old value */
990f8782eaSNeal Cardwell 		round_start:1,	     /* start of packet-timed tx->ack round? */
1000f8782eaSNeal Cardwell 		tso_segs_goal:7,     /* segments we want in each skb we send */
1010f8782eaSNeal Cardwell 		idle_restart:1,	     /* restarting after idle? */
1020f8782eaSNeal Cardwell 		probe_rtt_round_done:1,  /* a BBR_PROBE_RTT round at 4 pkts? */
1030f8782eaSNeal Cardwell 		unused:5,
1040f8782eaSNeal Cardwell 		lt_is_sampling:1,    /* taking long-term ("LT") samples now? */
1050f8782eaSNeal Cardwell 		lt_rtt_cnt:7,	     /* round trips in long-term interval */
1060f8782eaSNeal Cardwell 		lt_use_bw:1;	     /* use lt_bw as our bw estimate? */
1070f8782eaSNeal Cardwell 	u32	lt_bw;		     /* LT est delivery rate in pkts/uS << 24 */
1080f8782eaSNeal Cardwell 	u32	lt_last_delivered;   /* LT intvl start: tp->delivered */
1090f8782eaSNeal Cardwell 	u32	lt_last_stamp;	     /* LT intvl start: tp->delivered_mstamp */
1100f8782eaSNeal Cardwell 	u32	lt_last_lost;	     /* LT intvl start: tp->lost */
1110f8782eaSNeal Cardwell 	u32	pacing_gain:10,	/* current gain for setting pacing rate */
1120f8782eaSNeal Cardwell 		cwnd_gain:10,	/* current gain for setting cwnd */
113*c589e69bSNeal Cardwell 		full_bw_reached:1,   /* reached full bw in Startup? */
114*c589e69bSNeal Cardwell 		full_bw_cnt:2,	/* number of rounds without large bw gains */
1150f8782eaSNeal Cardwell 		cycle_idx:3,	/* current index in pacing_gain cycle array */
11632984565SNeal Cardwell 		has_seen_rtt:1, /* have we seen an RTT sample yet? */
11732984565SNeal Cardwell 		unused_b:5;
1180f8782eaSNeal Cardwell 	u32	prior_cwnd;	/* prior cwnd upon entering loss recovery */
1190f8782eaSNeal Cardwell 	u32	full_bw;	/* recent bw, to estimate if pipe is full */
1200f8782eaSNeal Cardwell };
1210f8782eaSNeal Cardwell 
1220f8782eaSNeal Cardwell #define CYCLE_LEN	8	/* number of phases in a pacing gain cycle */
1230f8782eaSNeal Cardwell 
1240f8782eaSNeal Cardwell /* Window length of bw filter (in rounds): */
1250f8782eaSNeal Cardwell static const int bbr_bw_rtts = CYCLE_LEN + 2;
1260f8782eaSNeal Cardwell /* Window length of min_rtt filter (in sec): */
1270f8782eaSNeal Cardwell static const u32 bbr_min_rtt_win_sec = 10;
1280f8782eaSNeal Cardwell /* Minimum time (in ms) spent at bbr_cwnd_min_target in BBR_PROBE_RTT mode: */
1290f8782eaSNeal Cardwell static const u32 bbr_probe_rtt_mode_ms = 200;
1300f8782eaSNeal Cardwell /* Skip TSO below the following bandwidth (bits/sec): */
1310f8782eaSNeal Cardwell static const int bbr_min_tso_rate = 1200000;
1320f8782eaSNeal Cardwell 
1330f8782eaSNeal Cardwell /* We use a high_gain value of 2/ln(2) because it's the smallest pacing gain
1340f8782eaSNeal Cardwell  * that will allow a smoothly increasing pacing rate that will double each RTT
1350f8782eaSNeal Cardwell  * and send the same number of packets per RTT that an un-paced, slow-starting
1360f8782eaSNeal Cardwell  * Reno or CUBIC flow would:
1370f8782eaSNeal Cardwell  */
1380f8782eaSNeal Cardwell static const int bbr_high_gain  = BBR_UNIT * 2885 / 1000 + 1;
1390f8782eaSNeal Cardwell /* The pacing gain of 1/high_gain in BBR_DRAIN is calculated to typically drain
1400f8782eaSNeal Cardwell  * the queue created in BBR_STARTUP in a single round:
1410f8782eaSNeal Cardwell  */
1420f8782eaSNeal Cardwell static const int bbr_drain_gain = BBR_UNIT * 1000 / 2885;
1430f8782eaSNeal Cardwell /* The gain for deriving steady-state cwnd tolerates delayed/stretched ACKs: */
1440f8782eaSNeal Cardwell static const int bbr_cwnd_gain  = BBR_UNIT * 2;
1450f8782eaSNeal Cardwell /* The pacing_gain values for the PROBE_BW gain cycle, to discover/share bw: */
1460f8782eaSNeal Cardwell static const int bbr_pacing_gain[] = {
1470f8782eaSNeal Cardwell 	BBR_UNIT * 5 / 4,	/* probe for more available bw */
1480f8782eaSNeal Cardwell 	BBR_UNIT * 3 / 4,	/* drain queue and/or yield bw to other flows */
1490f8782eaSNeal Cardwell 	BBR_UNIT, BBR_UNIT, BBR_UNIT,	/* cruise at 1.0*bw to utilize pipe, */
1500f8782eaSNeal Cardwell 	BBR_UNIT, BBR_UNIT, BBR_UNIT	/* without creating excess queue... */
1510f8782eaSNeal Cardwell };
1520f8782eaSNeal Cardwell /* Randomize the starting gain cycling phase over N phases: */
1530f8782eaSNeal Cardwell static const u32 bbr_cycle_rand = 7;
1540f8782eaSNeal Cardwell 
1550f8782eaSNeal Cardwell /* Try to keep at least this many packets in flight, if things go smoothly. For
1560f8782eaSNeal Cardwell  * smooth functioning, a sliding window protocol ACKing every other packet
1570f8782eaSNeal Cardwell  * needs at least 4 packets in flight:
1580f8782eaSNeal Cardwell  */
1590f8782eaSNeal Cardwell static const u32 bbr_cwnd_min_target = 4;
1600f8782eaSNeal Cardwell 
1610f8782eaSNeal Cardwell /* To estimate if BBR_STARTUP mode (i.e. high_gain) has filled pipe... */
1620f8782eaSNeal Cardwell /* If bw has increased significantly (1.25x), there may be more bw available: */
1630f8782eaSNeal Cardwell static const u32 bbr_full_bw_thresh = BBR_UNIT * 5 / 4;
1640f8782eaSNeal Cardwell /* But after 3 rounds w/o significant bw growth, estimate pipe is full: */
1650f8782eaSNeal Cardwell static const u32 bbr_full_bw_cnt = 3;
1660f8782eaSNeal Cardwell 
1670f8782eaSNeal Cardwell /* "long-term" ("LT") bandwidth estimator parameters... */
1680f8782eaSNeal Cardwell /* The minimum number of rounds in an LT bw sampling interval: */
1690f8782eaSNeal Cardwell static const u32 bbr_lt_intvl_min_rtts = 4;
1700f8782eaSNeal Cardwell /* If lost/delivered ratio > 20%, interval is "lossy" and we may be policed: */
1710f8782eaSNeal Cardwell static const u32 bbr_lt_loss_thresh = 50;
1720f8782eaSNeal Cardwell /* If 2 intervals have a bw ratio <= 1/8, their bw is "consistent": */
1730f8782eaSNeal Cardwell static const u32 bbr_lt_bw_ratio = BBR_UNIT / 8;
1740f8782eaSNeal Cardwell /* If 2 intervals have a bw diff <= 4 Kbit/sec their bw is "consistent": */
1750f8782eaSNeal Cardwell static const u32 bbr_lt_bw_diff = 4000 / 8;
1760f8782eaSNeal Cardwell /* If we estimate we're policed, use lt_bw for this many round trips: */
1770f8782eaSNeal Cardwell static const u32 bbr_lt_bw_max_rtts = 48;
1780f8782eaSNeal Cardwell 
1790f8782eaSNeal Cardwell /* Do we estimate that STARTUP filled the pipe? */
1800f8782eaSNeal Cardwell static bool bbr_full_bw_reached(const struct sock *sk)
1810f8782eaSNeal Cardwell {
1820f8782eaSNeal Cardwell 	const struct bbr *bbr = inet_csk_ca(sk);
1830f8782eaSNeal Cardwell 
184*c589e69bSNeal Cardwell 	return bbr->full_bw_reached;
1850f8782eaSNeal Cardwell }
1860f8782eaSNeal Cardwell 
1870f8782eaSNeal Cardwell /* Return the windowed max recent bandwidth sample, in pkts/uS << BW_SCALE. */
1880f8782eaSNeal Cardwell static u32 bbr_max_bw(const struct sock *sk)
1890f8782eaSNeal Cardwell {
1900f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
1910f8782eaSNeal Cardwell 
1920f8782eaSNeal Cardwell 	return minmax_get(&bbr->bw);
1930f8782eaSNeal Cardwell }
1940f8782eaSNeal Cardwell 
1950f8782eaSNeal Cardwell /* Return the estimated bandwidth of the path, in pkts/uS << BW_SCALE. */
1960f8782eaSNeal Cardwell static u32 bbr_bw(const struct sock *sk)
1970f8782eaSNeal Cardwell {
1980f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
1990f8782eaSNeal Cardwell 
2000f8782eaSNeal Cardwell 	return bbr->lt_use_bw ? bbr->lt_bw : bbr_max_bw(sk);
2010f8782eaSNeal Cardwell }
2020f8782eaSNeal Cardwell 
2030f8782eaSNeal Cardwell /* Return rate in bytes per second, optionally with a gain.
2040f8782eaSNeal Cardwell  * The order here is chosen carefully to avoid overflow of u64. This should
2050f8782eaSNeal Cardwell  * work for input rates of up to 2.9Tbit/sec and gain of 2.89x.
2060f8782eaSNeal Cardwell  */
2070f8782eaSNeal Cardwell static u64 bbr_rate_bytes_per_sec(struct sock *sk, u64 rate, int gain)
2080f8782eaSNeal Cardwell {
2090f8782eaSNeal Cardwell 	rate *= tcp_mss_to_mtu(sk, tcp_sk(sk)->mss_cache);
2100f8782eaSNeal Cardwell 	rate *= gain;
2110f8782eaSNeal Cardwell 	rate >>= BBR_SCALE;
2120f8782eaSNeal Cardwell 	rate *= USEC_PER_SEC;
2130f8782eaSNeal Cardwell 	return rate >> BW_SCALE;
2140f8782eaSNeal Cardwell }
2150f8782eaSNeal Cardwell 
216f19fd62dSNeal Cardwell /* Convert a BBR bw and gain factor to a pacing rate in bytes per second. */
217f19fd62dSNeal Cardwell static u32 bbr_bw_to_pacing_rate(struct sock *sk, u32 bw, int gain)
218f19fd62dSNeal Cardwell {
219f19fd62dSNeal Cardwell 	u64 rate = bw;
220f19fd62dSNeal Cardwell 
221f19fd62dSNeal Cardwell 	rate = bbr_rate_bytes_per_sec(sk, rate, gain);
222f19fd62dSNeal Cardwell 	rate = min_t(u64, rate, sk->sk_max_pacing_rate);
223f19fd62dSNeal Cardwell 	return rate;
224f19fd62dSNeal Cardwell }
225f19fd62dSNeal Cardwell 
22679135b89SNeal Cardwell /* Initialize pacing rate to: high_gain * init_cwnd / RTT. */
22779135b89SNeal Cardwell static void bbr_init_pacing_rate_from_rtt(struct sock *sk)
22879135b89SNeal Cardwell {
22979135b89SNeal Cardwell 	struct tcp_sock *tp = tcp_sk(sk);
23032984565SNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
23179135b89SNeal Cardwell 	u64 bw;
23279135b89SNeal Cardwell 	u32 rtt_us;
23379135b89SNeal Cardwell 
23479135b89SNeal Cardwell 	if (tp->srtt_us) {		/* any RTT sample yet? */
23579135b89SNeal Cardwell 		rtt_us = max(tp->srtt_us >> 3, 1U);
23632984565SNeal Cardwell 		bbr->has_seen_rtt = 1;
23779135b89SNeal Cardwell 	} else {			 /* no RTT sample yet */
23879135b89SNeal Cardwell 		rtt_us = USEC_PER_MSEC;	 /* use nominal default RTT */
23979135b89SNeal Cardwell 	}
24079135b89SNeal Cardwell 	bw = (u64)tp->snd_cwnd * BW_UNIT;
24179135b89SNeal Cardwell 	do_div(bw, rtt_us);
24279135b89SNeal Cardwell 	sk->sk_pacing_rate = bbr_bw_to_pacing_rate(sk, bw, bbr_high_gain);
24379135b89SNeal Cardwell }
24479135b89SNeal Cardwell 
2450f8782eaSNeal Cardwell /* Pace using current bw estimate and a gain factor. In order to help drive the
2460f8782eaSNeal Cardwell  * network toward lower queues while maintaining high utilization and low
2470f8782eaSNeal Cardwell  * latency, the average pacing rate aims to be slightly (~1%) lower than the
2480f8782eaSNeal Cardwell  * estimated bandwidth. This is an important aspect of the design. In this
2490f8782eaSNeal Cardwell  * implementation this slightly lower pacing rate is achieved implicitly by not
2500f8782eaSNeal Cardwell  * including link-layer headers in the packet size used for the pacing rate.
2510f8782eaSNeal Cardwell  */
2520f8782eaSNeal Cardwell static void bbr_set_pacing_rate(struct sock *sk, u32 bw, int gain)
2530f8782eaSNeal Cardwell {
25432984565SNeal Cardwell 	struct tcp_sock *tp = tcp_sk(sk);
25532984565SNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
256f19fd62dSNeal Cardwell 	u32 rate = bbr_bw_to_pacing_rate(sk, bw, gain);
2570f8782eaSNeal Cardwell 
25832984565SNeal Cardwell 	if (unlikely(!bbr->has_seen_rtt && tp->srtt_us))
25932984565SNeal Cardwell 		bbr_init_pacing_rate_from_rtt(sk);
2604aea287eSNeal Cardwell 	if (bbr_full_bw_reached(sk) || rate > sk->sk_pacing_rate)
2610f8782eaSNeal Cardwell 		sk->sk_pacing_rate = rate;
2620f8782eaSNeal Cardwell }
2630f8782eaSNeal Cardwell 
2640f8782eaSNeal Cardwell /* Return count of segments we want in the skbs we send, or 0 for default. */
2650f8782eaSNeal Cardwell static u32 bbr_tso_segs_goal(struct sock *sk)
2660f8782eaSNeal Cardwell {
2670f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
2680f8782eaSNeal Cardwell 
2690f8782eaSNeal Cardwell 	return bbr->tso_segs_goal;
2700f8782eaSNeal Cardwell }
2710f8782eaSNeal Cardwell 
2720f8782eaSNeal Cardwell static void bbr_set_tso_segs_goal(struct sock *sk)
2730f8782eaSNeal Cardwell {
2740f8782eaSNeal Cardwell 	struct tcp_sock *tp = tcp_sk(sk);
2750f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
2760f8782eaSNeal Cardwell 	u32 min_segs;
2770f8782eaSNeal Cardwell 
2780f8782eaSNeal Cardwell 	min_segs = sk->sk_pacing_rate < (bbr_min_tso_rate >> 3) ? 1 : 2;
2790f8782eaSNeal Cardwell 	bbr->tso_segs_goal = min(tcp_tso_autosize(sk, tp->mss_cache, min_segs),
2800f8782eaSNeal Cardwell 				 0x7FU);
2810f8782eaSNeal Cardwell }
2820f8782eaSNeal Cardwell 
2830f8782eaSNeal Cardwell /* Save "last known good" cwnd so we can restore it after losses or PROBE_RTT */
2840f8782eaSNeal Cardwell static void bbr_save_cwnd(struct sock *sk)
2850f8782eaSNeal Cardwell {
2860f8782eaSNeal Cardwell 	struct tcp_sock *tp = tcp_sk(sk);
2870f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
2880f8782eaSNeal Cardwell 
2890f8782eaSNeal Cardwell 	if (bbr->prev_ca_state < TCP_CA_Recovery && bbr->mode != BBR_PROBE_RTT)
2900f8782eaSNeal Cardwell 		bbr->prior_cwnd = tp->snd_cwnd;  /* this cwnd is good enough */
2910f8782eaSNeal Cardwell 	else  /* loss recovery or BBR_PROBE_RTT have temporarily cut cwnd */
2920f8782eaSNeal Cardwell 		bbr->prior_cwnd = max(bbr->prior_cwnd, tp->snd_cwnd);
2930f8782eaSNeal Cardwell }
2940f8782eaSNeal Cardwell 
2950f8782eaSNeal Cardwell static void bbr_cwnd_event(struct sock *sk, enum tcp_ca_event event)
2960f8782eaSNeal Cardwell {
2970f8782eaSNeal Cardwell 	struct tcp_sock *tp = tcp_sk(sk);
2980f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
2990f8782eaSNeal Cardwell 
3000f8782eaSNeal Cardwell 	if (event == CA_EVENT_TX_START && tp->app_limited) {
3010f8782eaSNeal Cardwell 		bbr->idle_restart = 1;
3020f8782eaSNeal Cardwell 		/* Avoid pointless buffer overflows: pace at est. bw if we don't
3030f8782eaSNeal Cardwell 		 * need more speed (we're restarting from idle and app-limited).
3040f8782eaSNeal Cardwell 		 */
3050f8782eaSNeal Cardwell 		if (bbr->mode == BBR_PROBE_BW)
3060f8782eaSNeal Cardwell 			bbr_set_pacing_rate(sk, bbr_bw(sk), BBR_UNIT);
3070f8782eaSNeal Cardwell 	}
3080f8782eaSNeal Cardwell }
3090f8782eaSNeal Cardwell 
3100f8782eaSNeal Cardwell /* Find target cwnd. Right-size the cwnd based on min RTT and the
3110f8782eaSNeal Cardwell  * estimated bottleneck bandwidth:
3120f8782eaSNeal Cardwell  *
3130f8782eaSNeal Cardwell  * cwnd = bw * min_rtt * gain = BDP * gain
3140f8782eaSNeal Cardwell  *
3150f8782eaSNeal Cardwell  * The key factor, gain, controls the amount of queue. While a small gain
3160f8782eaSNeal Cardwell  * builds a smaller queue, it becomes more vulnerable to noise in RTT
3170f8782eaSNeal Cardwell  * measurements (e.g., delayed ACKs or other ACK compression effects). This
3180f8782eaSNeal Cardwell  * noise may cause BBR to under-estimate the rate.
3190f8782eaSNeal Cardwell  *
3200f8782eaSNeal Cardwell  * To achieve full performance in high-speed paths, we budget enough cwnd to
3210f8782eaSNeal Cardwell  * fit full-sized skbs in-flight on both end hosts to fully utilize the path:
3220f8782eaSNeal Cardwell  *   - one skb in sending host Qdisc,
3230f8782eaSNeal Cardwell  *   - one skb in sending host TSO/GSO engine
3240f8782eaSNeal Cardwell  *   - one skb being received by receiver host LRO/GRO/delayed-ACK engine
3250f8782eaSNeal Cardwell  * Don't worry, at low rates (bbr_min_tso_rate) this won't bloat cwnd because
3260f8782eaSNeal Cardwell  * in such cases tso_segs_goal is 1. The minimum cwnd is 4 packets,
3270f8782eaSNeal Cardwell  * which allows 2 outstanding 2-packet sequences, to try to keep pipe
3280f8782eaSNeal Cardwell  * full even with ACK-every-other-packet delayed ACKs.
3290f8782eaSNeal Cardwell  */
3300f8782eaSNeal Cardwell static u32 bbr_target_cwnd(struct sock *sk, u32 bw, int gain)
3310f8782eaSNeal Cardwell {
3320f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
3330f8782eaSNeal Cardwell 	u32 cwnd;
3340f8782eaSNeal Cardwell 	u64 w;
3350f8782eaSNeal Cardwell 
3360f8782eaSNeal Cardwell 	/* If we've never had a valid RTT sample, cap cwnd at the initial
3370f8782eaSNeal Cardwell 	 * default. This should only happen when the connection is not using TCP
3380f8782eaSNeal Cardwell 	 * timestamps and has retransmitted all of the SYN/SYNACK/data packets
3390f8782eaSNeal Cardwell 	 * ACKed so far. In this case, an RTO can cut cwnd to 1, in which
3400f8782eaSNeal Cardwell 	 * case we need to slow-start up toward something safe: TCP_INIT_CWND.
3410f8782eaSNeal Cardwell 	 */
3420f8782eaSNeal Cardwell 	if (unlikely(bbr->min_rtt_us == ~0U))	 /* no valid RTT samples yet? */
3430f8782eaSNeal Cardwell 		return TCP_INIT_CWND;  /* be safe: cap at default initial cwnd*/
3440f8782eaSNeal Cardwell 
3450f8782eaSNeal Cardwell 	w = (u64)bw * bbr->min_rtt_us;
3460f8782eaSNeal Cardwell 
3470f8782eaSNeal Cardwell 	/* Apply a gain to the given value, then remove the BW_SCALE shift. */
3480f8782eaSNeal Cardwell 	cwnd = (((w * gain) >> BBR_SCALE) + BW_UNIT - 1) / BW_UNIT;
3490f8782eaSNeal Cardwell 
3500f8782eaSNeal Cardwell 	/* Allow enough full-sized skbs in flight to utilize end systems. */
3510f8782eaSNeal Cardwell 	cwnd += 3 * bbr->tso_segs_goal;
3520f8782eaSNeal Cardwell 
3530f8782eaSNeal Cardwell 	/* Reduce delayed ACKs by rounding up cwnd to the next even number. */
3540f8782eaSNeal Cardwell 	cwnd = (cwnd + 1) & ~1U;
3550f8782eaSNeal Cardwell 
3560f8782eaSNeal Cardwell 	return cwnd;
3570f8782eaSNeal Cardwell }
3580f8782eaSNeal Cardwell 
3590f8782eaSNeal Cardwell /* An optimization in BBR to reduce losses: On the first round of recovery, we
3600f8782eaSNeal Cardwell  * follow the packet conservation principle: send P packets per P packets acked.
3610f8782eaSNeal Cardwell  * After that, we slow-start and send at most 2*P packets per P packets acked.
3620f8782eaSNeal Cardwell  * After recovery finishes, or upon undo, we restore the cwnd we had when
3630f8782eaSNeal Cardwell  * recovery started (capped by the target cwnd based on estimated BDP).
3640f8782eaSNeal Cardwell  *
3650f8782eaSNeal Cardwell  * TODO(ycheng/ncardwell): implement a rate-based approach.
3660f8782eaSNeal Cardwell  */
3670f8782eaSNeal Cardwell static bool bbr_set_cwnd_to_recover_or_restore(
3680f8782eaSNeal Cardwell 	struct sock *sk, const struct rate_sample *rs, u32 acked, u32 *new_cwnd)
3690f8782eaSNeal Cardwell {
3700f8782eaSNeal Cardwell 	struct tcp_sock *tp = tcp_sk(sk);
3710f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
3720f8782eaSNeal Cardwell 	u8 prev_state = bbr->prev_ca_state, state = inet_csk(sk)->icsk_ca_state;
3730f8782eaSNeal Cardwell 	u32 cwnd = tp->snd_cwnd;
3740f8782eaSNeal Cardwell 
3750f8782eaSNeal Cardwell 	/* An ACK for P pkts should release at most 2*P packets. We do this
3760f8782eaSNeal Cardwell 	 * in two steps. First, here we deduct the number of lost packets.
3770f8782eaSNeal Cardwell 	 * Then, in bbr_set_cwnd() we slow start up toward the target cwnd.
3780f8782eaSNeal Cardwell 	 */
3790f8782eaSNeal Cardwell 	if (rs->losses > 0)
3800f8782eaSNeal Cardwell 		cwnd = max_t(s32, cwnd - rs->losses, 1);
3810f8782eaSNeal Cardwell 
3820f8782eaSNeal Cardwell 	if (state == TCP_CA_Recovery && prev_state != TCP_CA_Recovery) {
3830f8782eaSNeal Cardwell 		/* Starting 1st round of Recovery, so do packet conservation. */
3840f8782eaSNeal Cardwell 		bbr->packet_conservation = 1;
3850f8782eaSNeal Cardwell 		bbr->next_rtt_delivered = tp->delivered;  /* start round now */
3860f8782eaSNeal Cardwell 		/* Cut unused cwnd from app behavior, TSQ, or TSO deferral: */
3870f8782eaSNeal Cardwell 		cwnd = tcp_packets_in_flight(tp) + acked;
3880f8782eaSNeal Cardwell 	} else if (prev_state >= TCP_CA_Recovery && state < TCP_CA_Recovery) {
3890f8782eaSNeal Cardwell 		/* Exiting loss recovery; restore cwnd saved before recovery. */
3900f8782eaSNeal Cardwell 		bbr->restore_cwnd = 1;
3910f8782eaSNeal Cardwell 		bbr->packet_conservation = 0;
3920f8782eaSNeal Cardwell 	}
3930f8782eaSNeal Cardwell 	bbr->prev_ca_state = state;
3940f8782eaSNeal Cardwell 
3950f8782eaSNeal Cardwell 	if (bbr->restore_cwnd) {
3960f8782eaSNeal Cardwell 		/* Restore cwnd after exiting loss recovery or PROBE_RTT. */
3970f8782eaSNeal Cardwell 		cwnd = max(cwnd, bbr->prior_cwnd);
3980f8782eaSNeal Cardwell 		bbr->restore_cwnd = 0;
3990f8782eaSNeal Cardwell 	}
4000f8782eaSNeal Cardwell 
4010f8782eaSNeal Cardwell 	if (bbr->packet_conservation) {
4020f8782eaSNeal Cardwell 		*new_cwnd = max(cwnd, tcp_packets_in_flight(tp) + acked);
4030f8782eaSNeal Cardwell 		return true;	/* yes, using packet conservation */
4040f8782eaSNeal Cardwell 	}
4050f8782eaSNeal Cardwell 	*new_cwnd = cwnd;
4060f8782eaSNeal Cardwell 	return false;
4070f8782eaSNeal Cardwell }
4080f8782eaSNeal Cardwell 
4090f8782eaSNeal Cardwell /* Slow-start up toward target cwnd (if bw estimate is growing, or packet loss
4100f8782eaSNeal Cardwell  * has drawn us down below target), or snap down to target if we're above it.
4110f8782eaSNeal Cardwell  */
4120f8782eaSNeal Cardwell static void bbr_set_cwnd(struct sock *sk, const struct rate_sample *rs,
4130f8782eaSNeal Cardwell 			 u32 acked, u32 bw, int gain)
4140f8782eaSNeal Cardwell {
4150f8782eaSNeal Cardwell 	struct tcp_sock *tp = tcp_sk(sk);
4160f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
4170f8782eaSNeal Cardwell 	u32 cwnd = 0, target_cwnd = 0;
4180f8782eaSNeal Cardwell 
4190f8782eaSNeal Cardwell 	if (!acked)
4200f8782eaSNeal Cardwell 		return;
4210f8782eaSNeal Cardwell 
4220f8782eaSNeal Cardwell 	if (bbr_set_cwnd_to_recover_or_restore(sk, rs, acked, &cwnd))
4230f8782eaSNeal Cardwell 		goto done;
4240f8782eaSNeal Cardwell 
4250f8782eaSNeal Cardwell 	/* If we're below target cwnd, slow start cwnd toward target cwnd. */
4260f8782eaSNeal Cardwell 	target_cwnd = bbr_target_cwnd(sk, bw, gain);
4270f8782eaSNeal Cardwell 	if (bbr_full_bw_reached(sk))  /* only cut cwnd if we filled the pipe */
4280f8782eaSNeal Cardwell 		cwnd = min(cwnd + acked, target_cwnd);
4290f8782eaSNeal Cardwell 	else if (cwnd < target_cwnd || tp->delivered < TCP_INIT_CWND)
4300f8782eaSNeal Cardwell 		cwnd = cwnd + acked;
4310f8782eaSNeal Cardwell 	cwnd = max(cwnd, bbr_cwnd_min_target);
4320f8782eaSNeal Cardwell 
4330f8782eaSNeal Cardwell done:
4340f8782eaSNeal Cardwell 	tp->snd_cwnd = min(cwnd, tp->snd_cwnd_clamp);	/* apply global cap */
4350f8782eaSNeal Cardwell 	if (bbr->mode == BBR_PROBE_RTT)  /* drain queue, refresh min_rtt */
4360f8782eaSNeal Cardwell 		tp->snd_cwnd = min(tp->snd_cwnd, bbr_cwnd_min_target);
4370f8782eaSNeal Cardwell }
4380f8782eaSNeal Cardwell 
4390f8782eaSNeal Cardwell /* End cycle phase if it's time and/or we hit the phase's in-flight target. */
4400f8782eaSNeal Cardwell static bool bbr_is_next_cycle_phase(struct sock *sk,
4410f8782eaSNeal Cardwell 				    const struct rate_sample *rs)
4420f8782eaSNeal Cardwell {
4430f8782eaSNeal Cardwell 	struct tcp_sock *tp = tcp_sk(sk);
4440f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
4450f8782eaSNeal Cardwell 	bool is_full_length =
4469a568de4SEric Dumazet 		tcp_stamp_us_delta(tp->delivered_mstamp, bbr->cycle_mstamp) >
4470f8782eaSNeal Cardwell 		bbr->min_rtt_us;
4480f8782eaSNeal Cardwell 	u32 inflight, bw;
4490f8782eaSNeal Cardwell 
4500f8782eaSNeal Cardwell 	/* The pacing_gain of 1.0 paces at the estimated bw to try to fully
4510f8782eaSNeal Cardwell 	 * use the pipe without increasing the queue.
4520f8782eaSNeal Cardwell 	 */
4530f8782eaSNeal Cardwell 	if (bbr->pacing_gain == BBR_UNIT)
4540f8782eaSNeal Cardwell 		return is_full_length;		/* just use wall clock time */
4550f8782eaSNeal Cardwell 
4560f8782eaSNeal Cardwell 	inflight = rs->prior_in_flight;  /* what was in-flight before ACK? */
4570f8782eaSNeal Cardwell 	bw = bbr_max_bw(sk);
4580f8782eaSNeal Cardwell 
4590f8782eaSNeal Cardwell 	/* A pacing_gain > 1.0 probes for bw by trying to raise inflight to at
4600f8782eaSNeal Cardwell 	 * least pacing_gain*BDP; this may take more than min_rtt if min_rtt is
4610f8782eaSNeal Cardwell 	 * small (e.g. on a LAN). We do not persist if packets are lost, since
4620f8782eaSNeal Cardwell 	 * a path with small buffers may not hold that much.
4630f8782eaSNeal Cardwell 	 */
4640f8782eaSNeal Cardwell 	if (bbr->pacing_gain > BBR_UNIT)
4650f8782eaSNeal Cardwell 		return is_full_length &&
4660f8782eaSNeal Cardwell 			(rs->losses ||  /* perhaps pacing_gain*BDP won't fit */
4670f8782eaSNeal Cardwell 			 inflight >= bbr_target_cwnd(sk, bw, bbr->pacing_gain));
4680f8782eaSNeal Cardwell 
4690f8782eaSNeal Cardwell 	/* A pacing_gain < 1.0 tries to drain extra queue we added if bw
4700f8782eaSNeal Cardwell 	 * probing didn't find more bw. If inflight falls to match BDP then we
4710f8782eaSNeal Cardwell 	 * estimate queue is drained; persisting would underutilize the pipe.
4720f8782eaSNeal Cardwell 	 */
4730f8782eaSNeal Cardwell 	return is_full_length ||
4740f8782eaSNeal Cardwell 		inflight <= bbr_target_cwnd(sk, bw, BBR_UNIT);
4750f8782eaSNeal Cardwell }
4760f8782eaSNeal Cardwell 
4770f8782eaSNeal Cardwell static void bbr_advance_cycle_phase(struct sock *sk)
4780f8782eaSNeal Cardwell {
4790f8782eaSNeal Cardwell 	struct tcp_sock *tp = tcp_sk(sk);
4800f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
4810f8782eaSNeal Cardwell 
4820f8782eaSNeal Cardwell 	bbr->cycle_idx = (bbr->cycle_idx + 1) & (CYCLE_LEN - 1);
4830f8782eaSNeal Cardwell 	bbr->cycle_mstamp = tp->delivered_mstamp;
4840f8782eaSNeal Cardwell 	bbr->pacing_gain = bbr_pacing_gain[bbr->cycle_idx];
4850f8782eaSNeal Cardwell }
4860f8782eaSNeal Cardwell 
4870f8782eaSNeal Cardwell /* Gain cycling: cycle pacing gain to converge to fair share of available bw. */
4880f8782eaSNeal Cardwell static void bbr_update_cycle_phase(struct sock *sk,
4890f8782eaSNeal Cardwell 				   const struct rate_sample *rs)
4900f8782eaSNeal Cardwell {
4910f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
4920f8782eaSNeal Cardwell 
4930f8782eaSNeal Cardwell 	if ((bbr->mode == BBR_PROBE_BW) && !bbr->lt_use_bw &&
4940f8782eaSNeal Cardwell 	    bbr_is_next_cycle_phase(sk, rs))
4950f8782eaSNeal Cardwell 		bbr_advance_cycle_phase(sk);
4960f8782eaSNeal Cardwell }
4970f8782eaSNeal Cardwell 
4980f8782eaSNeal Cardwell static void bbr_reset_startup_mode(struct sock *sk)
4990f8782eaSNeal Cardwell {
5000f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
5010f8782eaSNeal Cardwell 
5020f8782eaSNeal Cardwell 	bbr->mode = BBR_STARTUP;
5030f8782eaSNeal Cardwell 	bbr->pacing_gain = bbr_high_gain;
5040f8782eaSNeal Cardwell 	bbr->cwnd_gain	 = bbr_high_gain;
5050f8782eaSNeal Cardwell }
5060f8782eaSNeal Cardwell 
5070f8782eaSNeal Cardwell static void bbr_reset_probe_bw_mode(struct sock *sk)
5080f8782eaSNeal Cardwell {
5090f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
5100f8782eaSNeal Cardwell 
5110f8782eaSNeal Cardwell 	bbr->mode = BBR_PROBE_BW;
5120f8782eaSNeal Cardwell 	bbr->pacing_gain = BBR_UNIT;
5130f8782eaSNeal Cardwell 	bbr->cwnd_gain = bbr_cwnd_gain;
5140f8782eaSNeal Cardwell 	bbr->cycle_idx = CYCLE_LEN - 1 - prandom_u32_max(bbr_cycle_rand);
5150f8782eaSNeal Cardwell 	bbr_advance_cycle_phase(sk);	/* flip to next phase of gain cycle */
5160f8782eaSNeal Cardwell }
5170f8782eaSNeal Cardwell 
5180f8782eaSNeal Cardwell static void bbr_reset_mode(struct sock *sk)
5190f8782eaSNeal Cardwell {
5200f8782eaSNeal Cardwell 	if (!bbr_full_bw_reached(sk))
5210f8782eaSNeal Cardwell 		bbr_reset_startup_mode(sk);
5220f8782eaSNeal Cardwell 	else
5230f8782eaSNeal Cardwell 		bbr_reset_probe_bw_mode(sk);
5240f8782eaSNeal Cardwell }
5250f8782eaSNeal Cardwell 
5260f8782eaSNeal Cardwell /* Start a new long-term sampling interval. */
5270f8782eaSNeal Cardwell static void bbr_reset_lt_bw_sampling_interval(struct sock *sk)
5280f8782eaSNeal Cardwell {
5290f8782eaSNeal Cardwell 	struct tcp_sock *tp = tcp_sk(sk);
5300f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
5310f8782eaSNeal Cardwell 
5329a568de4SEric Dumazet 	bbr->lt_last_stamp = div_u64(tp->delivered_mstamp, USEC_PER_MSEC);
5330f8782eaSNeal Cardwell 	bbr->lt_last_delivered = tp->delivered;
5340f8782eaSNeal Cardwell 	bbr->lt_last_lost = tp->lost;
5350f8782eaSNeal Cardwell 	bbr->lt_rtt_cnt = 0;
5360f8782eaSNeal Cardwell }
5370f8782eaSNeal Cardwell 
5380f8782eaSNeal Cardwell /* Completely reset long-term bandwidth sampling. */
5390f8782eaSNeal Cardwell static void bbr_reset_lt_bw_sampling(struct sock *sk)
5400f8782eaSNeal Cardwell {
5410f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
5420f8782eaSNeal Cardwell 
5430f8782eaSNeal Cardwell 	bbr->lt_bw = 0;
5440f8782eaSNeal Cardwell 	bbr->lt_use_bw = 0;
5450f8782eaSNeal Cardwell 	bbr->lt_is_sampling = false;
5460f8782eaSNeal Cardwell 	bbr_reset_lt_bw_sampling_interval(sk);
5470f8782eaSNeal Cardwell }
5480f8782eaSNeal Cardwell 
5490f8782eaSNeal Cardwell /* Long-term bw sampling interval is done. Estimate whether we're policed. */
5500f8782eaSNeal Cardwell static void bbr_lt_bw_interval_done(struct sock *sk, u32 bw)
5510f8782eaSNeal Cardwell {
5520f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
5530f8782eaSNeal Cardwell 	u32 diff;
5540f8782eaSNeal Cardwell 
5550f8782eaSNeal Cardwell 	if (bbr->lt_bw) {  /* do we have bw from a previous interval? */
5560f8782eaSNeal Cardwell 		/* Is new bw close to the lt_bw from the previous interval? */
5570f8782eaSNeal Cardwell 		diff = abs(bw - bbr->lt_bw);
5580f8782eaSNeal Cardwell 		if ((diff * BBR_UNIT <= bbr_lt_bw_ratio * bbr->lt_bw) ||
5590f8782eaSNeal Cardwell 		    (bbr_rate_bytes_per_sec(sk, diff, BBR_UNIT) <=
5600f8782eaSNeal Cardwell 		     bbr_lt_bw_diff)) {
5610f8782eaSNeal Cardwell 			/* All criteria are met; estimate we're policed. */
5620f8782eaSNeal Cardwell 			bbr->lt_bw = (bw + bbr->lt_bw) >> 1;  /* avg 2 intvls */
5630f8782eaSNeal Cardwell 			bbr->lt_use_bw = 1;
5640f8782eaSNeal Cardwell 			bbr->pacing_gain = BBR_UNIT;  /* try to avoid drops */
5650f8782eaSNeal Cardwell 			bbr->lt_rtt_cnt = 0;
5660f8782eaSNeal Cardwell 			return;
5670f8782eaSNeal Cardwell 		}
5680f8782eaSNeal Cardwell 	}
5690f8782eaSNeal Cardwell 	bbr->lt_bw = bw;
5700f8782eaSNeal Cardwell 	bbr_reset_lt_bw_sampling_interval(sk);
5710f8782eaSNeal Cardwell }
5720f8782eaSNeal Cardwell 
5730f8782eaSNeal Cardwell /* Token-bucket traffic policers are common (see "An Internet-Wide Analysis of
5740f8782eaSNeal Cardwell  * Traffic Policing", SIGCOMM 2016). BBR detects token-bucket policers and
5750f8782eaSNeal Cardwell  * explicitly models their policed rate, to reduce unnecessary losses. We
5760f8782eaSNeal Cardwell  * estimate that we're policed if we see 2 consecutive sampling intervals with
5770f8782eaSNeal Cardwell  * consistent throughput and high packet loss. If we think we're being policed,
5780f8782eaSNeal Cardwell  * set lt_bw to the "long-term" average delivery rate from those 2 intervals.
5790f8782eaSNeal Cardwell  */
5800f8782eaSNeal Cardwell static void bbr_lt_bw_sampling(struct sock *sk, const struct rate_sample *rs)
5810f8782eaSNeal Cardwell {
5820f8782eaSNeal Cardwell 	struct tcp_sock *tp = tcp_sk(sk);
5830f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
5840f8782eaSNeal Cardwell 	u32 lost, delivered;
5850f8782eaSNeal Cardwell 	u64 bw;
5869a568de4SEric Dumazet 	u32 t;
5870f8782eaSNeal Cardwell 
5880f8782eaSNeal Cardwell 	if (bbr->lt_use_bw) {	/* already using long-term rate, lt_bw? */
5890f8782eaSNeal Cardwell 		if (bbr->mode == BBR_PROBE_BW && bbr->round_start &&
5900f8782eaSNeal Cardwell 		    ++bbr->lt_rtt_cnt >= bbr_lt_bw_max_rtts) {
5910f8782eaSNeal Cardwell 			bbr_reset_lt_bw_sampling(sk);    /* stop using lt_bw */
5920f8782eaSNeal Cardwell 			bbr_reset_probe_bw_mode(sk);  /* restart gain cycling */
5930f8782eaSNeal Cardwell 		}
5940f8782eaSNeal Cardwell 		return;
5950f8782eaSNeal Cardwell 	}
5960f8782eaSNeal Cardwell 
5970f8782eaSNeal Cardwell 	/* Wait for the first loss before sampling, to let the policer exhaust
5980f8782eaSNeal Cardwell 	 * its tokens and estimate the steady-state rate allowed by the policer.
5990f8782eaSNeal Cardwell 	 * Starting samples earlier includes bursts that over-estimate the bw.
6000f8782eaSNeal Cardwell 	 */
6010f8782eaSNeal Cardwell 	if (!bbr->lt_is_sampling) {
6020f8782eaSNeal Cardwell 		if (!rs->losses)
6030f8782eaSNeal Cardwell 			return;
6040f8782eaSNeal Cardwell 		bbr_reset_lt_bw_sampling_interval(sk);
6050f8782eaSNeal Cardwell 		bbr->lt_is_sampling = true;
6060f8782eaSNeal Cardwell 	}
6070f8782eaSNeal Cardwell 
6080f8782eaSNeal Cardwell 	/* To avoid underestimates, reset sampling if we run out of data. */
6090f8782eaSNeal Cardwell 	if (rs->is_app_limited) {
6100f8782eaSNeal Cardwell 		bbr_reset_lt_bw_sampling(sk);
6110f8782eaSNeal Cardwell 		return;
6120f8782eaSNeal Cardwell 	}
6130f8782eaSNeal Cardwell 
6140f8782eaSNeal Cardwell 	if (bbr->round_start)
6150f8782eaSNeal Cardwell 		bbr->lt_rtt_cnt++;	/* count round trips in this interval */
6160f8782eaSNeal Cardwell 	if (bbr->lt_rtt_cnt < bbr_lt_intvl_min_rtts)
6170f8782eaSNeal Cardwell 		return;		/* sampling interval needs to be longer */
6180f8782eaSNeal Cardwell 	if (bbr->lt_rtt_cnt > 4 * bbr_lt_intvl_min_rtts) {
6190f8782eaSNeal Cardwell 		bbr_reset_lt_bw_sampling(sk);  /* interval is too long */
6200f8782eaSNeal Cardwell 		return;
6210f8782eaSNeal Cardwell 	}
6220f8782eaSNeal Cardwell 
6230f8782eaSNeal Cardwell 	/* End sampling interval when a packet is lost, so we estimate the
6240f8782eaSNeal Cardwell 	 * policer tokens were exhausted. Stopping the sampling before the
6250f8782eaSNeal Cardwell 	 * tokens are exhausted under-estimates the policed rate.
6260f8782eaSNeal Cardwell 	 */
6270f8782eaSNeal Cardwell 	if (!rs->losses)
6280f8782eaSNeal Cardwell 		return;
6290f8782eaSNeal Cardwell 
6300f8782eaSNeal Cardwell 	/* Calculate packets lost and delivered in sampling interval. */
6310f8782eaSNeal Cardwell 	lost = tp->lost - bbr->lt_last_lost;
6320f8782eaSNeal Cardwell 	delivered = tp->delivered - bbr->lt_last_delivered;
6330f8782eaSNeal Cardwell 	/* Is loss rate (lost/delivered) >= lt_loss_thresh? If not, wait. */
6340f8782eaSNeal Cardwell 	if (!delivered || (lost << BBR_SCALE) < bbr_lt_loss_thresh * delivered)
6350f8782eaSNeal Cardwell 		return;
6360f8782eaSNeal Cardwell 
6370f8782eaSNeal Cardwell 	/* Find average delivery rate in this sampling interval. */
6389a568de4SEric Dumazet 	t = div_u64(tp->delivered_mstamp, USEC_PER_MSEC) - bbr->lt_last_stamp;
6399a568de4SEric Dumazet 	if ((s32)t < 1)
6409a568de4SEric Dumazet 		return;		/* interval is less than one ms, so wait */
6419a568de4SEric Dumazet 	/* Check if can multiply without overflow */
6429a568de4SEric Dumazet 	if (t >= ~0U / USEC_PER_MSEC) {
6430f8782eaSNeal Cardwell 		bbr_reset_lt_bw_sampling(sk);  /* interval too long; reset */
6440f8782eaSNeal Cardwell 		return;
6450f8782eaSNeal Cardwell 	}
6469a568de4SEric Dumazet 	t *= USEC_PER_MSEC;
6470f8782eaSNeal Cardwell 	bw = (u64)delivered * BW_UNIT;
6480f8782eaSNeal Cardwell 	do_div(bw, t);
6490f8782eaSNeal Cardwell 	bbr_lt_bw_interval_done(sk, bw);
6500f8782eaSNeal Cardwell }
6510f8782eaSNeal Cardwell 
6520f8782eaSNeal Cardwell /* Estimate the bandwidth based on how fast packets are delivered */
6530f8782eaSNeal Cardwell static void bbr_update_bw(struct sock *sk, const struct rate_sample *rs)
6540f8782eaSNeal Cardwell {
6550f8782eaSNeal Cardwell 	struct tcp_sock *tp = tcp_sk(sk);
6560f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
6570f8782eaSNeal Cardwell 	u64 bw;
6580f8782eaSNeal Cardwell 
6590f8782eaSNeal Cardwell 	bbr->round_start = 0;
6600f8782eaSNeal Cardwell 	if (rs->delivered < 0 || rs->interval_us <= 0)
6610f8782eaSNeal Cardwell 		return; /* Not a valid observation */
6620f8782eaSNeal Cardwell 
6630f8782eaSNeal Cardwell 	/* See if we've reached the next RTT */
6640f8782eaSNeal Cardwell 	if (!before(rs->prior_delivered, bbr->next_rtt_delivered)) {
6650f8782eaSNeal Cardwell 		bbr->next_rtt_delivered = tp->delivered;
6660f8782eaSNeal Cardwell 		bbr->rtt_cnt++;
6670f8782eaSNeal Cardwell 		bbr->round_start = 1;
6680f8782eaSNeal Cardwell 		bbr->packet_conservation = 0;
6690f8782eaSNeal Cardwell 	}
6700f8782eaSNeal Cardwell 
6710f8782eaSNeal Cardwell 	bbr_lt_bw_sampling(sk, rs);
6720f8782eaSNeal Cardwell 
6730f8782eaSNeal Cardwell 	/* Divide delivered by the interval to find a (lower bound) bottleneck
6740f8782eaSNeal Cardwell 	 * bandwidth sample. Delivered is in packets and interval_us in uS and
6750f8782eaSNeal Cardwell 	 * ratio will be <<1 for most connections. So delivered is first scaled.
6760f8782eaSNeal Cardwell 	 */
6770f8782eaSNeal Cardwell 	bw = (u64)rs->delivered * BW_UNIT;
6780f8782eaSNeal Cardwell 	do_div(bw, rs->interval_us);
6790f8782eaSNeal Cardwell 
6800f8782eaSNeal Cardwell 	/* If this sample is application-limited, it is likely to have a very
6810f8782eaSNeal Cardwell 	 * low delivered count that represents application behavior rather than
6820f8782eaSNeal Cardwell 	 * the available network rate. Such a sample could drag down estimated
6830f8782eaSNeal Cardwell 	 * bw, causing needless slow-down. Thus, to continue to send at the
6840f8782eaSNeal Cardwell 	 * last measured network rate, we filter out app-limited samples unless
6850f8782eaSNeal Cardwell 	 * they describe the path bw at least as well as our bw model.
6860f8782eaSNeal Cardwell 	 *
6870f8782eaSNeal Cardwell 	 * So the goal during app-limited phase is to proceed with the best
6880f8782eaSNeal Cardwell 	 * network rate no matter how long. We automatically leave this
6890f8782eaSNeal Cardwell 	 * phase when app writes faster than the network can deliver :)
6900f8782eaSNeal Cardwell 	 */
6910f8782eaSNeal Cardwell 	if (!rs->is_app_limited || bw >= bbr_max_bw(sk)) {
6920f8782eaSNeal Cardwell 		/* Incorporate new sample into our max bw filter. */
6930f8782eaSNeal Cardwell 		minmax_running_max(&bbr->bw, bbr_bw_rtts, bbr->rtt_cnt, bw);
6940f8782eaSNeal Cardwell 	}
6950f8782eaSNeal Cardwell }
6960f8782eaSNeal Cardwell 
6970f8782eaSNeal Cardwell /* Estimate when the pipe is full, using the change in delivery rate: BBR
6980f8782eaSNeal Cardwell  * estimates that STARTUP filled the pipe if the estimated bw hasn't changed by
6990f8782eaSNeal Cardwell  * at least bbr_full_bw_thresh (25%) after bbr_full_bw_cnt (3) non-app-limited
7000f8782eaSNeal Cardwell  * rounds. Why 3 rounds: 1: rwin autotuning grows the rwin, 2: we fill the
7010f8782eaSNeal Cardwell  * higher rwin, 3: we get higher delivery rate samples. Or transient
7020f8782eaSNeal Cardwell  * cross-traffic or radio noise can go away. CUBIC Hystart shares a similar
7030f8782eaSNeal Cardwell  * design goal, but uses delay and inter-ACK spacing instead of bandwidth.
7040f8782eaSNeal Cardwell  */
7050f8782eaSNeal Cardwell static void bbr_check_full_bw_reached(struct sock *sk,
7060f8782eaSNeal Cardwell 				      const struct rate_sample *rs)
7070f8782eaSNeal Cardwell {
7080f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
7090f8782eaSNeal Cardwell 	u32 bw_thresh;
7100f8782eaSNeal Cardwell 
7110f8782eaSNeal Cardwell 	if (bbr_full_bw_reached(sk) || !bbr->round_start || rs->is_app_limited)
7120f8782eaSNeal Cardwell 		return;
7130f8782eaSNeal Cardwell 
7140f8782eaSNeal Cardwell 	bw_thresh = (u64)bbr->full_bw * bbr_full_bw_thresh >> BBR_SCALE;
7150f8782eaSNeal Cardwell 	if (bbr_max_bw(sk) >= bw_thresh) {
7160f8782eaSNeal Cardwell 		bbr->full_bw = bbr_max_bw(sk);
7170f8782eaSNeal Cardwell 		bbr->full_bw_cnt = 0;
7180f8782eaSNeal Cardwell 		return;
7190f8782eaSNeal Cardwell 	}
7200f8782eaSNeal Cardwell 	++bbr->full_bw_cnt;
721*c589e69bSNeal Cardwell 	bbr->full_bw_reached = bbr->full_bw_cnt >= bbr_full_bw_cnt;
7220f8782eaSNeal Cardwell }
7230f8782eaSNeal Cardwell 
7240f8782eaSNeal Cardwell /* If pipe is probably full, drain the queue and then enter steady-state. */
7250f8782eaSNeal Cardwell static void bbr_check_drain(struct sock *sk, const struct rate_sample *rs)
7260f8782eaSNeal Cardwell {
7270f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
7280f8782eaSNeal Cardwell 
7290f8782eaSNeal Cardwell 	if (bbr->mode == BBR_STARTUP && bbr_full_bw_reached(sk)) {
7300f8782eaSNeal Cardwell 		bbr->mode = BBR_DRAIN;	/* drain queue we created */
7310f8782eaSNeal Cardwell 		bbr->pacing_gain = bbr_drain_gain;	/* pace slow to drain */
7320f8782eaSNeal Cardwell 		bbr->cwnd_gain = bbr_high_gain;	/* maintain cwnd */
7330f8782eaSNeal Cardwell 	}	/* fall through to check if in-flight is already small: */
7340f8782eaSNeal Cardwell 	if (bbr->mode == BBR_DRAIN &&
7350f8782eaSNeal Cardwell 	    tcp_packets_in_flight(tcp_sk(sk)) <=
7360f8782eaSNeal Cardwell 	    bbr_target_cwnd(sk, bbr_max_bw(sk), BBR_UNIT))
7370f8782eaSNeal Cardwell 		bbr_reset_probe_bw_mode(sk);  /* we estimate queue is drained */
7380f8782eaSNeal Cardwell }
7390f8782eaSNeal Cardwell 
7400f8782eaSNeal Cardwell /* The goal of PROBE_RTT mode is to have BBR flows cooperatively and
7410f8782eaSNeal Cardwell  * periodically drain the bottleneck queue, to converge to measure the true
7420f8782eaSNeal Cardwell  * min_rtt (unloaded propagation delay). This allows the flows to keep queues
7430f8782eaSNeal Cardwell  * small (reducing queuing delay and packet loss) and achieve fairness among
7440f8782eaSNeal Cardwell  * BBR flows.
7450f8782eaSNeal Cardwell  *
7460f8782eaSNeal Cardwell  * The min_rtt filter window is 10 seconds. When the min_rtt estimate expires,
7470f8782eaSNeal Cardwell  * we enter PROBE_RTT mode and cap the cwnd at bbr_cwnd_min_target=4 packets.
7480f8782eaSNeal Cardwell  * After at least bbr_probe_rtt_mode_ms=200ms and at least one packet-timed
7490f8782eaSNeal Cardwell  * round trip elapsed with that flight size <= 4, we leave PROBE_RTT mode and
7500f8782eaSNeal Cardwell  * re-enter the previous mode. BBR uses 200ms to approximately bound the
7510f8782eaSNeal Cardwell  * performance penalty of PROBE_RTT's cwnd capping to roughly 2% (200ms/10s).
7520f8782eaSNeal Cardwell  *
7530f8782eaSNeal Cardwell  * Note that flows need only pay 2% if they are busy sending over the last 10
7540f8782eaSNeal Cardwell  * seconds. Interactive applications (e.g., Web, RPCs, video chunks) often have
7550f8782eaSNeal Cardwell  * natural silences or low-rate periods within 10 seconds where the rate is low
7560f8782eaSNeal Cardwell  * enough for long enough to drain its queue in the bottleneck. We pick up
7570f8782eaSNeal Cardwell  * these min RTT measurements opportunistically with our min_rtt filter. :-)
7580f8782eaSNeal Cardwell  */
7590f8782eaSNeal Cardwell static void bbr_update_min_rtt(struct sock *sk, const struct rate_sample *rs)
7600f8782eaSNeal Cardwell {
7610f8782eaSNeal Cardwell 	struct tcp_sock *tp = tcp_sk(sk);
7620f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
7630f8782eaSNeal Cardwell 	bool filter_expired;
7640f8782eaSNeal Cardwell 
7650f8782eaSNeal Cardwell 	/* Track min RTT seen in the min_rtt_win_sec filter window: */
7662660bfa8SEric Dumazet 	filter_expired = after(tcp_jiffies32,
7670f8782eaSNeal Cardwell 			       bbr->min_rtt_stamp + bbr_min_rtt_win_sec * HZ);
7680f8782eaSNeal Cardwell 	if (rs->rtt_us >= 0 &&
7690f8782eaSNeal Cardwell 	    (rs->rtt_us <= bbr->min_rtt_us || filter_expired)) {
7700f8782eaSNeal Cardwell 		bbr->min_rtt_us = rs->rtt_us;
7712660bfa8SEric Dumazet 		bbr->min_rtt_stamp = tcp_jiffies32;
7720f8782eaSNeal Cardwell 	}
7730f8782eaSNeal Cardwell 
7740f8782eaSNeal Cardwell 	if (bbr_probe_rtt_mode_ms > 0 && filter_expired &&
7750f8782eaSNeal Cardwell 	    !bbr->idle_restart && bbr->mode != BBR_PROBE_RTT) {
7760f8782eaSNeal Cardwell 		bbr->mode = BBR_PROBE_RTT;  /* dip, drain queue */
7770f8782eaSNeal Cardwell 		bbr->pacing_gain = BBR_UNIT;
7780f8782eaSNeal Cardwell 		bbr->cwnd_gain = BBR_UNIT;
7790f8782eaSNeal Cardwell 		bbr_save_cwnd(sk);  /* note cwnd so we can restore it */
7800f8782eaSNeal Cardwell 		bbr->probe_rtt_done_stamp = 0;
7810f8782eaSNeal Cardwell 	}
7820f8782eaSNeal Cardwell 
7830f8782eaSNeal Cardwell 	if (bbr->mode == BBR_PROBE_RTT) {
7840f8782eaSNeal Cardwell 		/* Ignore low rate samples during this mode. */
7850f8782eaSNeal Cardwell 		tp->app_limited =
7860f8782eaSNeal Cardwell 			(tp->delivered + tcp_packets_in_flight(tp)) ? : 1;
7870f8782eaSNeal Cardwell 		/* Maintain min packets in flight for max(200 ms, 1 round). */
7880f8782eaSNeal Cardwell 		if (!bbr->probe_rtt_done_stamp &&
7890f8782eaSNeal Cardwell 		    tcp_packets_in_flight(tp) <= bbr_cwnd_min_target) {
7902660bfa8SEric Dumazet 			bbr->probe_rtt_done_stamp = tcp_jiffies32 +
7910f8782eaSNeal Cardwell 				msecs_to_jiffies(bbr_probe_rtt_mode_ms);
7920f8782eaSNeal Cardwell 			bbr->probe_rtt_round_done = 0;
7930f8782eaSNeal Cardwell 			bbr->next_rtt_delivered = tp->delivered;
7940f8782eaSNeal Cardwell 		} else if (bbr->probe_rtt_done_stamp) {
7950f8782eaSNeal Cardwell 			if (bbr->round_start)
7960f8782eaSNeal Cardwell 				bbr->probe_rtt_round_done = 1;
7970f8782eaSNeal Cardwell 			if (bbr->probe_rtt_round_done &&
7982660bfa8SEric Dumazet 			    after(tcp_jiffies32, bbr->probe_rtt_done_stamp)) {
7992660bfa8SEric Dumazet 				bbr->min_rtt_stamp = tcp_jiffies32;
8000f8782eaSNeal Cardwell 				bbr->restore_cwnd = 1;  /* snap to prior_cwnd */
8010f8782eaSNeal Cardwell 				bbr_reset_mode(sk);
8020f8782eaSNeal Cardwell 			}
8030f8782eaSNeal Cardwell 		}
8040f8782eaSNeal Cardwell 	}
8050f8782eaSNeal Cardwell 	bbr->idle_restart = 0;
8060f8782eaSNeal Cardwell }
8070f8782eaSNeal Cardwell 
8080f8782eaSNeal Cardwell static void bbr_update_model(struct sock *sk, const struct rate_sample *rs)
8090f8782eaSNeal Cardwell {
8100f8782eaSNeal Cardwell 	bbr_update_bw(sk, rs);
8110f8782eaSNeal Cardwell 	bbr_update_cycle_phase(sk, rs);
8120f8782eaSNeal Cardwell 	bbr_check_full_bw_reached(sk, rs);
8130f8782eaSNeal Cardwell 	bbr_check_drain(sk, rs);
8140f8782eaSNeal Cardwell 	bbr_update_min_rtt(sk, rs);
8150f8782eaSNeal Cardwell }
8160f8782eaSNeal Cardwell 
8170f8782eaSNeal Cardwell static void bbr_main(struct sock *sk, const struct rate_sample *rs)
8180f8782eaSNeal Cardwell {
8190f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
8200f8782eaSNeal Cardwell 	u32 bw;
8210f8782eaSNeal Cardwell 
8220f8782eaSNeal Cardwell 	bbr_update_model(sk, rs);
8230f8782eaSNeal Cardwell 
8240f8782eaSNeal Cardwell 	bw = bbr_bw(sk);
8250f8782eaSNeal Cardwell 	bbr_set_pacing_rate(sk, bw, bbr->pacing_gain);
8260f8782eaSNeal Cardwell 	bbr_set_tso_segs_goal(sk);
8270f8782eaSNeal Cardwell 	bbr_set_cwnd(sk, rs, rs->acked_sacked, bw, bbr->cwnd_gain);
8280f8782eaSNeal Cardwell }
8290f8782eaSNeal Cardwell 
8300f8782eaSNeal Cardwell static void bbr_init(struct sock *sk)
8310f8782eaSNeal Cardwell {
8320f8782eaSNeal Cardwell 	struct tcp_sock *tp = tcp_sk(sk);
8330f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
8340f8782eaSNeal Cardwell 
8350f8782eaSNeal Cardwell 	bbr->prior_cwnd = 0;
8360f8782eaSNeal Cardwell 	bbr->tso_segs_goal = 0;	 /* default segs per skb until first ACK */
8370f8782eaSNeal Cardwell 	bbr->rtt_cnt = 0;
8380f8782eaSNeal Cardwell 	bbr->next_rtt_delivered = 0;
8390f8782eaSNeal Cardwell 	bbr->prev_ca_state = TCP_CA_Open;
8400f8782eaSNeal Cardwell 	bbr->packet_conservation = 0;
8410f8782eaSNeal Cardwell 
8420f8782eaSNeal Cardwell 	bbr->probe_rtt_done_stamp = 0;
8430f8782eaSNeal Cardwell 	bbr->probe_rtt_round_done = 0;
8440f8782eaSNeal Cardwell 	bbr->min_rtt_us = tcp_min_rtt(tp);
8452660bfa8SEric Dumazet 	bbr->min_rtt_stamp = tcp_jiffies32;
8460f8782eaSNeal Cardwell 
8470f8782eaSNeal Cardwell 	minmax_reset(&bbr->bw, bbr->rtt_cnt, 0);  /* init max bw to 0 */
8480f8782eaSNeal Cardwell 
84932984565SNeal Cardwell 	bbr->has_seen_rtt = 0;
85079135b89SNeal Cardwell 	bbr_init_pacing_rate_from_rtt(sk);
8510f8782eaSNeal Cardwell 
8520f8782eaSNeal Cardwell 	bbr->restore_cwnd = 0;
8530f8782eaSNeal Cardwell 	bbr->round_start = 0;
8540f8782eaSNeal Cardwell 	bbr->idle_restart = 0;
855*c589e69bSNeal Cardwell 	bbr->full_bw_reached = 0;
8560f8782eaSNeal Cardwell 	bbr->full_bw = 0;
8570f8782eaSNeal Cardwell 	bbr->full_bw_cnt = 0;
8589a568de4SEric Dumazet 	bbr->cycle_mstamp = 0;
8590f8782eaSNeal Cardwell 	bbr->cycle_idx = 0;
8600f8782eaSNeal Cardwell 	bbr_reset_lt_bw_sampling(sk);
8610f8782eaSNeal Cardwell 	bbr_reset_startup_mode(sk);
862218af599SEric Dumazet 
863218af599SEric Dumazet 	cmpxchg(&sk->sk_pacing_status, SK_PACING_NONE, SK_PACING_NEEDED);
8640f8782eaSNeal Cardwell }
8650f8782eaSNeal Cardwell 
8660f8782eaSNeal Cardwell static u32 bbr_sndbuf_expand(struct sock *sk)
8670f8782eaSNeal Cardwell {
8680f8782eaSNeal Cardwell 	/* Provision 3 * cwnd since BBR may slow-start even during recovery. */
8690f8782eaSNeal Cardwell 	return 3;
8700f8782eaSNeal Cardwell }
8710f8782eaSNeal Cardwell 
8720f8782eaSNeal Cardwell /* In theory BBR does not need to undo the cwnd since it does not
8730f8782eaSNeal Cardwell  * always reduce cwnd on losses (see bbr_main()). Keep it for now.
8740f8782eaSNeal Cardwell  */
8750f8782eaSNeal Cardwell static u32 bbr_undo_cwnd(struct sock *sk)
8760f8782eaSNeal Cardwell {
8770f8782eaSNeal Cardwell 	return tcp_sk(sk)->snd_cwnd;
8780f8782eaSNeal Cardwell }
8790f8782eaSNeal Cardwell 
8800f8782eaSNeal Cardwell /* Entering loss recovery, so save cwnd for when we exit or undo recovery. */
8810f8782eaSNeal Cardwell static u32 bbr_ssthresh(struct sock *sk)
8820f8782eaSNeal Cardwell {
8830f8782eaSNeal Cardwell 	bbr_save_cwnd(sk);
8840f8782eaSNeal Cardwell 	return TCP_INFINITE_SSTHRESH;	 /* BBR does not use ssthresh */
8850f8782eaSNeal Cardwell }
8860f8782eaSNeal Cardwell 
8870f8782eaSNeal Cardwell static size_t bbr_get_info(struct sock *sk, u32 ext, int *attr,
8880f8782eaSNeal Cardwell 			   union tcp_cc_info *info)
8890f8782eaSNeal Cardwell {
8900f8782eaSNeal Cardwell 	if (ext & (1 << (INET_DIAG_BBRINFO - 1)) ||
8910f8782eaSNeal Cardwell 	    ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
8920f8782eaSNeal Cardwell 		struct tcp_sock *tp = tcp_sk(sk);
8930f8782eaSNeal Cardwell 		struct bbr *bbr = inet_csk_ca(sk);
8940f8782eaSNeal Cardwell 		u64 bw = bbr_bw(sk);
8950f8782eaSNeal Cardwell 
8960f8782eaSNeal Cardwell 		bw = bw * tp->mss_cache * USEC_PER_SEC >> BW_SCALE;
8970f8782eaSNeal Cardwell 		memset(&info->bbr, 0, sizeof(info->bbr));
8980f8782eaSNeal Cardwell 		info->bbr.bbr_bw_lo		= (u32)bw;
8990f8782eaSNeal Cardwell 		info->bbr.bbr_bw_hi		= (u32)(bw >> 32);
9000f8782eaSNeal Cardwell 		info->bbr.bbr_min_rtt		= bbr->min_rtt_us;
9010f8782eaSNeal Cardwell 		info->bbr.bbr_pacing_gain	= bbr->pacing_gain;
9020f8782eaSNeal Cardwell 		info->bbr.bbr_cwnd_gain		= bbr->cwnd_gain;
9030f8782eaSNeal Cardwell 		*attr = INET_DIAG_BBRINFO;
9040f8782eaSNeal Cardwell 		return sizeof(info->bbr);
9050f8782eaSNeal Cardwell 	}
9060f8782eaSNeal Cardwell 	return 0;
9070f8782eaSNeal Cardwell }
9080f8782eaSNeal Cardwell 
9090f8782eaSNeal Cardwell static void bbr_set_state(struct sock *sk, u8 new_state)
9100f8782eaSNeal Cardwell {
9110f8782eaSNeal Cardwell 	struct bbr *bbr = inet_csk_ca(sk);
9120f8782eaSNeal Cardwell 
9130f8782eaSNeal Cardwell 	if (new_state == TCP_CA_Loss) {
9140f8782eaSNeal Cardwell 		struct rate_sample rs = { .losses = 1 };
9150f8782eaSNeal Cardwell 
9160f8782eaSNeal Cardwell 		bbr->prev_ca_state = TCP_CA_Loss;
9170f8782eaSNeal Cardwell 		bbr->full_bw = 0;
9180f8782eaSNeal Cardwell 		bbr->round_start = 1;	/* treat RTO like end of a round */
9190f8782eaSNeal Cardwell 		bbr_lt_bw_sampling(sk, &rs);
9200f8782eaSNeal Cardwell 	}
9210f8782eaSNeal Cardwell }
9220f8782eaSNeal Cardwell 
9230f8782eaSNeal Cardwell static struct tcp_congestion_ops tcp_bbr_cong_ops __read_mostly = {
9240f8782eaSNeal Cardwell 	.flags		= TCP_CONG_NON_RESTRICTED,
9250f8782eaSNeal Cardwell 	.name		= "bbr",
9260f8782eaSNeal Cardwell 	.owner		= THIS_MODULE,
9270f8782eaSNeal Cardwell 	.init		= bbr_init,
9280f8782eaSNeal Cardwell 	.cong_control	= bbr_main,
9290f8782eaSNeal Cardwell 	.sndbuf_expand	= bbr_sndbuf_expand,
9300f8782eaSNeal Cardwell 	.undo_cwnd	= bbr_undo_cwnd,
9310f8782eaSNeal Cardwell 	.cwnd_event	= bbr_cwnd_event,
9320f8782eaSNeal Cardwell 	.ssthresh	= bbr_ssthresh,
9330f8782eaSNeal Cardwell 	.tso_segs_goal	= bbr_tso_segs_goal,
9340f8782eaSNeal Cardwell 	.get_info	= bbr_get_info,
9350f8782eaSNeal Cardwell 	.set_state	= bbr_set_state,
9360f8782eaSNeal Cardwell };
9370f8782eaSNeal Cardwell 
9380f8782eaSNeal Cardwell static int __init bbr_register(void)
9390f8782eaSNeal Cardwell {
9400f8782eaSNeal Cardwell 	BUILD_BUG_ON(sizeof(struct bbr) > ICSK_CA_PRIV_SIZE);
9410f8782eaSNeal Cardwell 	return tcp_register_congestion_control(&tcp_bbr_cong_ops);
9420f8782eaSNeal Cardwell }
9430f8782eaSNeal Cardwell 
9440f8782eaSNeal Cardwell static void __exit bbr_unregister(void)
9450f8782eaSNeal Cardwell {
9460f8782eaSNeal Cardwell 	tcp_unregister_congestion_control(&tcp_bbr_cong_ops);
9470f8782eaSNeal Cardwell }
9480f8782eaSNeal Cardwell 
9490f8782eaSNeal Cardwell module_init(bbr_register);
9500f8782eaSNeal Cardwell module_exit(bbr_unregister);
9510f8782eaSNeal Cardwell 
9520f8782eaSNeal Cardwell MODULE_AUTHOR("Van Jacobson <vanj@google.com>");
9530f8782eaSNeal Cardwell MODULE_AUTHOR("Neal Cardwell <ncardwell@google.com>");
9540f8782eaSNeal Cardwell MODULE_AUTHOR("Yuchung Cheng <ycheng@google.com>");
9550f8782eaSNeal Cardwell MODULE_AUTHOR("Soheil Hassas Yeganeh <soheil@google.com>");
9560f8782eaSNeal Cardwell MODULE_LICENSE("Dual BSD/GPL");
9570f8782eaSNeal Cardwell MODULE_DESCRIPTION("TCP BBR (Bottleneck Bandwidth and RTT)");
958