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 */
590e32dfc8SKumar Kartikeya Dwivedi #include <linux/btf.h>
600e32dfc8SKumar Kartikeya Dwivedi #include <linux/btf_ids.h>
610f8782eaSNeal Cardwell #include <linux/module.h>
620f8782eaSNeal Cardwell #include <net/tcp.h>
630f8782eaSNeal Cardwell #include <linux/inet_diag.h>
640f8782eaSNeal Cardwell #include <linux/inet.h>
650f8782eaSNeal Cardwell #include <linux/random.h>
660f8782eaSNeal Cardwell #include <linux/win_minmax.h>
670f8782eaSNeal Cardwell
680f8782eaSNeal Cardwell /* Scale factor for rate in pkt/uSec unit to avoid truncation in bandwidth
690f8782eaSNeal Cardwell * estimation. The rate unit ~= (1500 bytes / 1 usec / 2^24) ~= 715 bps.
700f8782eaSNeal Cardwell * This handles bandwidths from 0.06pps (715bps) to 256Mpps (3Tbps) in a u32.
710f8782eaSNeal Cardwell * Since the minimum window is >=4 packets, the lower bound isn't
720f8782eaSNeal Cardwell * an issue. The upper bound isn't an issue with existing technologies.
730f8782eaSNeal Cardwell */
740f8782eaSNeal Cardwell #define BW_SCALE 24
750f8782eaSNeal Cardwell #define BW_UNIT (1 << BW_SCALE)
760f8782eaSNeal Cardwell
770f8782eaSNeal Cardwell #define BBR_SCALE 8 /* scaling factor for fractions in BBR (e.g. gains) */
780f8782eaSNeal Cardwell #define BBR_UNIT (1 << BBR_SCALE)
790f8782eaSNeal Cardwell
800f8782eaSNeal Cardwell /* BBR has the following modes for deciding how fast to send: */
810f8782eaSNeal Cardwell enum bbr_mode {
820f8782eaSNeal Cardwell BBR_STARTUP, /* ramp up sending rate rapidly to fill pipe */
830f8782eaSNeal Cardwell BBR_DRAIN, /* drain any queue created during startup */
840f8782eaSNeal Cardwell BBR_PROBE_BW, /* discover, share bw: pace around estimated bw */
859b9375b5SNeal Cardwell BBR_PROBE_RTT, /* cut inflight to min to probe min_rtt */
860f8782eaSNeal Cardwell };
870f8782eaSNeal Cardwell
880f8782eaSNeal Cardwell /* BBR congestion control block */
890f8782eaSNeal Cardwell struct bbr {
900f8782eaSNeal Cardwell u32 min_rtt_us; /* min RTT in min_rtt_win_sec window */
910f8782eaSNeal Cardwell u32 min_rtt_stamp; /* timestamp of min_rtt_us */
920f8782eaSNeal Cardwell u32 probe_rtt_done_stamp; /* end time for BBR_PROBE_RTT mode */
930f8782eaSNeal Cardwell struct minmax bw; /* Max recent delivery rate in pkts/uS << 24 */
940f8782eaSNeal Cardwell u32 rtt_cnt; /* count of packet-timed rounds elapsed */
950f8782eaSNeal Cardwell u32 next_rtt_delivered; /* scb->tx.delivered at end of round */
969a568de4SEric Dumazet u64 cycle_mstamp; /* time of this cycle phase start */
970f8782eaSNeal Cardwell u32 mode:3, /* current bbr_mode in state machine */
980f8782eaSNeal Cardwell prev_ca_state:3, /* CA state on previous ACK */
990f8782eaSNeal Cardwell packet_conservation:1, /* use packet conservation? */
1000f8782eaSNeal Cardwell round_start:1, /* start of packet-timed tx->ack round? */
1010f8782eaSNeal Cardwell idle_restart:1, /* restarting after idle? */
1020f8782eaSNeal Cardwell probe_rtt_round_done:1, /* a BBR_PROBE_RTT round at 4 pkts? */
103fb998862SKevin Yang unused:13,
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 */
113c589e69bSNeal Cardwell full_bw_reached:1, /* reached full bw in Startup? */
114c589e69bSNeal 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 */
12078dc70ebSPriyaranjan Jha
12178dc70ebSPriyaranjan Jha /* For tracking ACK aggregation: */
12278dc70ebSPriyaranjan Jha u64 ack_epoch_mstamp; /* start of ACK sampling epoch */
12378dc70ebSPriyaranjan Jha u16 extra_acked[2]; /* max excess data ACKed in epoch */
12478dc70ebSPriyaranjan Jha u32 ack_epoch_acked:20, /* packets (S)ACKed in sampling epoch */
12578dc70ebSPriyaranjan Jha extra_acked_win_rtts:5, /* age of extra_acked, in round trips */
12678dc70ebSPriyaranjan Jha extra_acked_win_idx:1, /* current index in extra_acked array */
12778dc70ebSPriyaranjan Jha unused_c:6;
1280f8782eaSNeal Cardwell };
1290f8782eaSNeal Cardwell
1300f8782eaSNeal Cardwell #define CYCLE_LEN 8 /* number of phases in a pacing gain cycle */
1310f8782eaSNeal Cardwell
1320f8782eaSNeal Cardwell /* Window length of bw filter (in rounds): */
1330f8782eaSNeal Cardwell static const int bbr_bw_rtts = CYCLE_LEN + 2;
1340f8782eaSNeal Cardwell /* Window length of min_rtt filter (in sec): */
1350f8782eaSNeal Cardwell static const u32 bbr_min_rtt_win_sec = 10;
1360f8782eaSNeal Cardwell /* Minimum time (in ms) spent at bbr_cwnd_min_target in BBR_PROBE_RTT mode: */
1370f8782eaSNeal Cardwell static const u32 bbr_probe_rtt_mode_ms = 200;
1380f8782eaSNeal Cardwell /* Skip TSO below the following bandwidth (bits/sec): */
1390f8782eaSNeal Cardwell static const int bbr_min_tso_rate = 1200000;
1400f8782eaSNeal Cardwell
1411106a5adSNeal Cardwell /* Pace at ~1% below estimated bw, on average, to reduce queue at bottleneck.
1421106a5adSNeal Cardwell * In order to help drive the network toward lower queues and low latency while
1431106a5adSNeal Cardwell * maintaining high utilization, the average pacing rate aims to be slightly
1441106a5adSNeal Cardwell * lower than the estimated bandwidth. This is an important aspect of the
1451106a5adSNeal Cardwell * design.
1461106a5adSNeal Cardwell */
14797ec3eb3SNeal Cardwell static const int bbr_pacing_margin_percent = 1;
148ab408b6dSEric Dumazet
1490f8782eaSNeal Cardwell /* We use a high_gain value of 2/ln(2) because it's the smallest pacing gain
1500f8782eaSNeal Cardwell * that will allow a smoothly increasing pacing rate that will double each RTT
1510f8782eaSNeal Cardwell * and send the same number of packets per RTT that an un-paced, slow-starting
1520f8782eaSNeal Cardwell * Reno or CUBIC flow would:
1530f8782eaSNeal Cardwell */
1540f8782eaSNeal Cardwell static const int bbr_high_gain = BBR_UNIT * 2885 / 1000 + 1;
1550f8782eaSNeal Cardwell /* The pacing gain of 1/high_gain in BBR_DRAIN is calculated to typically drain
1560f8782eaSNeal Cardwell * the queue created in BBR_STARTUP in a single round:
1570f8782eaSNeal Cardwell */
1580f8782eaSNeal Cardwell static const int bbr_drain_gain = BBR_UNIT * 1000 / 2885;
1590f8782eaSNeal Cardwell /* The gain for deriving steady-state cwnd tolerates delayed/stretched ACKs: */
1600f8782eaSNeal Cardwell static const int bbr_cwnd_gain = BBR_UNIT * 2;
1610f8782eaSNeal Cardwell /* The pacing_gain values for the PROBE_BW gain cycle, to discover/share bw: */
1620f8782eaSNeal Cardwell static const int bbr_pacing_gain[] = {
1630f8782eaSNeal Cardwell BBR_UNIT * 5 / 4, /* probe for more available bw */
1640f8782eaSNeal Cardwell BBR_UNIT * 3 / 4, /* drain queue and/or yield bw to other flows */
1650f8782eaSNeal Cardwell BBR_UNIT, BBR_UNIT, BBR_UNIT, /* cruise at 1.0*bw to utilize pipe, */
1660f8782eaSNeal Cardwell BBR_UNIT, BBR_UNIT, BBR_UNIT /* without creating excess queue... */
1670f8782eaSNeal Cardwell };
1680f8782eaSNeal Cardwell /* Randomize the starting gain cycling phase over N phases: */
1690f8782eaSNeal Cardwell static const u32 bbr_cycle_rand = 7;
1700f8782eaSNeal Cardwell
1710f8782eaSNeal Cardwell /* Try to keep at least this many packets in flight, if things go smoothly. For
1720f8782eaSNeal Cardwell * smooth functioning, a sliding window protocol ACKing every other packet
1730f8782eaSNeal Cardwell * needs at least 4 packets in flight:
1740f8782eaSNeal Cardwell */
1750f8782eaSNeal Cardwell static const u32 bbr_cwnd_min_target = 4;
1760f8782eaSNeal Cardwell
1770f8782eaSNeal Cardwell /* To estimate if BBR_STARTUP mode (i.e. high_gain) has filled pipe... */
1780f8782eaSNeal Cardwell /* If bw has increased significantly (1.25x), there may be more bw available: */
1790f8782eaSNeal Cardwell static const u32 bbr_full_bw_thresh = BBR_UNIT * 5 / 4;
1800f8782eaSNeal Cardwell /* But after 3 rounds w/o significant bw growth, estimate pipe is full: */
1810f8782eaSNeal Cardwell static const u32 bbr_full_bw_cnt = 3;
1820f8782eaSNeal Cardwell
1830f8782eaSNeal Cardwell /* "long-term" ("LT") bandwidth estimator parameters... */
1840f8782eaSNeal Cardwell /* The minimum number of rounds in an LT bw sampling interval: */
1850f8782eaSNeal Cardwell static const u32 bbr_lt_intvl_min_rtts = 4;
1860f8782eaSNeal Cardwell /* If lost/delivered ratio > 20%, interval is "lossy" and we may be policed: */
1870f8782eaSNeal Cardwell static const u32 bbr_lt_loss_thresh = 50;
1880f8782eaSNeal Cardwell /* If 2 intervals have a bw ratio <= 1/8, their bw is "consistent": */
1890f8782eaSNeal Cardwell static const u32 bbr_lt_bw_ratio = BBR_UNIT / 8;
1900f8782eaSNeal Cardwell /* If 2 intervals have a bw diff <= 4 Kbit/sec their bw is "consistent": */
1910f8782eaSNeal Cardwell static const u32 bbr_lt_bw_diff = 4000 / 8;
1920f8782eaSNeal Cardwell /* If we estimate we're policed, use lt_bw for this many round trips: */
1930f8782eaSNeal Cardwell static const u32 bbr_lt_bw_max_rtts = 48;
1940f8782eaSNeal Cardwell
19578dc70ebSPriyaranjan Jha /* Gain factor for adding extra_acked to target cwnd: */
19678dc70ebSPriyaranjan Jha static const int bbr_extra_acked_gain = BBR_UNIT;
19778dc70ebSPriyaranjan Jha /* Window length of extra_acked window. */
19878dc70ebSPriyaranjan Jha static const u32 bbr_extra_acked_win_rtts = 5;
19978dc70ebSPriyaranjan Jha /* Max allowed val for ack_epoch_acked, after which sampling epoch is reset */
20078dc70ebSPriyaranjan Jha static const u32 bbr_ack_epoch_acked_reset_thresh = 1U << 20;
20178dc70ebSPriyaranjan Jha /* Time period for clamping cwnd increment due to ack aggregation */
20278dc70ebSPriyaranjan Jha static const u32 bbr_extra_acked_max_us = 100 * 1000;
20378dc70ebSPriyaranjan Jha
2045490b32dSKevin Yang static void bbr_check_probe_rtt_done(struct sock *sk);
2055490b32dSKevin Yang
2060f8782eaSNeal Cardwell /* Do we estimate that STARTUP filled the pipe? */
bbr_full_bw_reached(const struct sock * sk)2070f8782eaSNeal Cardwell static bool bbr_full_bw_reached(const struct sock *sk)
2080f8782eaSNeal Cardwell {
2090f8782eaSNeal Cardwell const struct bbr *bbr = inet_csk_ca(sk);
2100f8782eaSNeal Cardwell
211c589e69bSNeal Cardwell return bbr->full_bw_reached;
2120f8782eaSNeal Cardwell }
2130f8782eaSNeal Cardwell
2140f8782eaSNeal Cardwell /* Return the windowed max recent bandwidth sample, in pkts/uS << BW_SCALE. */
bbr_max_bw(const struct sock * sk)2150f8782eaSNeal Cardwell static u32 bbr_max_bw(const struct sock *sk)
2160f8782eaSNeal Cardwell {
2170f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
2180f8782eaSNeal Cardwell
2190f8782eaSNeal Cardwell return minmax_get(&bbr->bw);
2200f8782eaSNeal Cardwell }
2210f8782eaSNeal Cardwell
2220f8782eaSNeal Cardwell /* Return the estimated bandwidth of the path, in pkts/uS << BW_SCALE. */
bbr_bw(const struct sock * sk)2230f8782eaSNeal Cardwell static u32 bbr_bw(const struct sock *sk)
2240f8782eaSNeal Cardwell {
2250f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
2260f8782eaSNeal Cardwell
2270f8782eaSNeal Cardwell return bbr->lt_use_bw ? bbr->lt_bw : bbr_max_bw(sk);
2280f8782eaSNeal Cardwell }
2290f8782eaSNeal Cardwell
23078dc70ebSPriyaranjan Jha /* Return maximum extra acked in past k-2k round trips,
23178dc70ebSPriyaranjan Jha * where k = bbr_extra_acked_win_rtts.
23278dc70ebSPriyaranjan Jha */
bbr_extra_acked(const struct sock * sk)23378dc70ebSPriyaranjan Jha static u16 bbr_extra_acked(const struct sock *sk)
23478dc70ebSPriyaranjan Jha {
23578dc70ebSPriyaranjan Jha struct bbr *bbr = inet_csk_ca(sk);
23678dc70ebSPriyaranjan Jha
23778dc70ebSPriyaranjan Jha return max(bbr->extra_acked[0], bbr->extra_acked[1]);
23878dc70ebSPriyaranjan Jha }
23978dc70ebSPriyaranjan Jha
2400f8782eaSNeal Cardwell /* Return rate in bytes per second, optionally with a gain.
2410f8782eaSNeal Cardwell * The order here is chosen carefully to avoid overflow of u64. This should
2420f8782eaSNeal Cardwell * work for input rates of up to 2.9Tbit/sec and gain of 2.89x.
2430f8782eaSNeal Cardwell */
bbr_rate_bytes_per_sec(struct sock * sk,u64 rate,int gain)2440f8782eaSNeal Cardwell static u64 bbr_rate_bytes_per_sec(struct sock *sk, u64 rate, int gain)
2450f8782eaSNeal Cardwell {
246cadefe5fSEric Dumazet unsigned int mss = tcp_sk(sk)->mss_cache;
247cadefe5fSEric Dumazet
248cadefe5fSEric Dumazet rate *= mss;
2490f8782eaSNeal Cardwell rate *= gain;
2500f8782eaSNeal Cardwell rate >>= BBR_SCALE;
25197ec3eb3SNeal Cardwell rate *= USEC_PER_SEC / 100 * (100 - bbr_pacing_margin_percent);
2520f8782eaSNeal Cardwell return rate >> BW_SCALE;
2530f8782eaSNeal Cardwell }
2540f8782eaSNeal Cardwell
255f19fd62dSNeal Cardwell /* Convert a BBR bw and gain factor to a pacing rate in bytes per second. */
bbr_bw_to_pacing_rate(struct sock * sk,u32 bw,int gain)25676a9ebe8SEric Dumazet static unsigned long bbr_bw_to_pacing_rate(struct sock *sk, u32 bw, int gain)
257f19fd62dSNeal Cardwell {
258f19fd62dSNeal Cardwell u64 rate = bw;
259f19fd62dSNeal Cardwell
260f19fd62dSNeal Cardwell rate = bbr_rate_bytes_per_sec(sk, rate, gain);
261f19fd62dSNeal Cardwell rate = min_t(u64, rate, sk->sk_max_pacing_rate);
262f19fd62dSNeal Cardwell return rate;
263f19fd62dSNeal Cardwell }
264f19fd62dSNeal Cardwell
26579135b89SNeal Cardwell /* Initialize pacing rate to: high_gain * init_cwnd / RTT. */
bbr_init_pacing_rate_from_rtt(struct sock * sk)26679135b89SNeal Cardwell static void bbr_init_pacing_rate_from_rtt(struct sock *sk)
26779135b89SNeal Cardwell {
26879135b89SNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
26932984565SNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
27079135b89SNeal Cardwell u64 bw;
27179135b89SNeal Cardwell u32 rtt_us;
27279135b89SNeal Cardwell
27379135b89SNeal Cardwell if (tp->srtt_us) { /* any RTT sample yet? */
27479135b89SNeal Cardwell rtt_us = max(tp->srtt_us >> 3, 1U);
27532984565SNeal Cardwell bbr->has_seen_rtt = 1;
27679135b89SNeal Cardwell } else { /* no RTT sample yet */
27779135b89SNeal Cardwell rtt_us = USEC_PER_MSEC; /* use nominal default RTT */
27879135b89SNeal Cardwell }
27940570375SEric Dumazet bw = (u64)tcp_snd_cwnd(tp) * BW_UNIT;
28079135b89SNeal Cardwell do_div(bw, rtt_us);
28179135b89SNeal Cardwell sk->sk_pacing_rate = bbr_bw_to_pacing_rate(sk, bw, bbr_high_gain);
28279135b89SNeal Cardwell }
28379135b89SNeal Cardwell
2841106a5adSNeal Cardwell /* Pace using current bw estimate and a gain factor. */
bbr_set_pacing_rate(struct sock * sk,u32 bw,int gain)2850f8782eaSNeal Cardwell static void bbr_set_pacing_rate(struct sock *sk, u32 bw, int gain)
2860f8782eaSNeal Cardwell {
28732984565SNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
28832984565SNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
28976a9ebe8SEric Dumazet unsigned long rate = bbr_bw_to_pacing_rate(sk, bw, gain);
2900f8782eaSNeal Cardwell
29132984565SNeal Cardwell if (unlikely(!bbr->has_seen_rtt && tp->srtt_us))
29232984565SNeal Cardwell bbr_init_pacing_rate_from_rtt(sk);
2934aea287eSNeal Cardwell if (bbr_full_bw_reached(sk) || rate > sk->sk_pacing_rate)
2940f8782eaSNeal Cardwell sk->sk_pacing_rate = rate;
2950f8782eaSNeal Cardwell }
2960f8782eaSNeal Cardwell
297dcb8c9b4SEric Dumazet /* override sysctl_tcp_min_tso_segs */
bbr_min_tso_segs(struct sock * sk)298*400031e0SDavid Vernet __bpf_kfunc static u32 bbr_min_tso_segs(struct sock *sk)
2990f8782eaSNeal Cardwell {
300dcb8c9b4SEric Dumazet return sk->sk_pacing_rate < (bbr_min_tso_rate >> 3) ? 1 : 2;
3010f8782eaSNeal Cardwell }
3020f8782eaSNeal Cardwell
bbr_tso_segs_goal(struct sock * sk)30371abf467SEric Dumazet static u32 bbr_tso_segs_goal(struct sock *sk)
3040f8782eaSNeal Cardwell {
3050f8782eaSNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
306dcb8c9b4SEric Dumazet u32 segs, bytes;
3070f8782eaSNeal Cardwell
308dcb8c9b4SEric Dumazet /* Sort of tcp_tso_autosize() but ignoring
309dcb8c9b4SEric Dumazet * driver provided sk_gso_max_size.
310dcb8c9b4SEric Dumazet */
3117c68fa2bSEric Dumazet bytes = min_t(unsigned long,
3127c68fa2bSEric Dumazet sk->sk_pacing_rate >> READ_ONCE(sk->sk_pacing_shift),
3137c4e983cSAlexander Duyck GSO_LEGACY_MAX_SIZE - 1 - MAX_TCP_HEADER);
314dcb8c9b4SEric Dumazet segs = max_t(u32, bytes / tp->mss_cache, bbr_min_tso_segs(sk));
315dcb8c9b4SEric Dumazet
31671abf467SEric Dumazet return min(segs, 0x7FU);
3170f8782eaSNeal Cardwell }
3180f8782eaSNeal Cardwell
3190f8782eaSNeal Cardwell /* Save "last known good" cwnd so we can restore it after losses or PROBE_RTT */
bbr_save_cwnd(struct sock * sk)3200f8782eaSNeal Cardwell static void bbr_save_cwnd(struct sock *sk)
3210f8782eaSNeal Cardwell {
3220f8782eaSNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
3230f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
3240f8782eaSNeal Cardwell
3250f8782eaSNeal Cardwell if (bbr->prev_ca_state < TCP_CA_Recovery && bbr->mode != BBR_PROBE_RTT)
32640570375SEric Dumazet bbr->prior_cwnd = tcp_snd_cwnd(tp); /* this cwnd is good enough */
3270f8782eaSNeal Cardwell else /* loss recovery or BBR_PROBE_RTT have temporarily cut cwnd */
32840570375SEric Dumazet bbr->prior_cwnd = max(bbr->prior_cwnd, tcp_snd_cwnd(tp));
3290f8782eaSNeal Cardwell }
3300f8782eaSNeal Cardwell
bbr_cwnd_event(struct sock * sk,enum tcp_ca_event event)331*400031e0SDavid Vernet __bpf_kfunc static void bbr_cwnd_event(struct sock *sk, enum tcp_ca_event event)
3320f8782eaSNeal Cardwell {
3330f8782eaSNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
3340f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
3350f8782eaSNeal Cardwell
3360f8782eaSNeal Cardwell if (event == CA_EVENT_TX_START && tp->app_limited) {
3370f8782eaSNeal Cardwell bbr->idle_restart = 1;
33878dc70ebSPriyaranjan Jha bbr->ack_epoch_mstamp = tp->tcp_mstamp;
33978dc70ebSPriyaranjan Jha bbr->ack_epoch_acked = 0;
3400f8782eaSNeal Cardwell /* Avoid pointless buffer overflows: pace at est. bw if we don't
3410f8782eaSNeal Cardwell * need more speed (we're restarting from idle and app-limited).
3420f8782eaSNeal Cardwell */
3430f8782eaSNeal Cardwell if (bbr->mode == BBR_PROBE_BW)
3440f8782eaSNeal Cardwell bbr_set_pacing_rate(sk, bbr_bw(sk), BBR_UNIT);
3455490b32dSKevin Yang else if (bbr->mode == BBR_PROBE_RTT)
3465490b32dSKevin Yang bbr_check_probe_rtt_done(sk);
3470f8782eaSNeal Cardwell }
3480f8782eaSNeal Cardwell }
3490f8782eaSNeal Cardwell
350232aa8ecSPriyaranjan Jha /* Calculate bdp based on min RTT and the estimated bottleneck bandwidth:
3510f8782eaSNeal Cardwell *
352de8e1bebSLuke Hsiao * bdp = ceil(bw * min_rtt * gain)
3530f8782eaSNeal Cardwell *
3540f8782eaSNeal Cardwell * The key factor, gain, controls the amount of queue. While a small gain
3550f8782eaSNeal Cardwell * builds a smaller queue, it becomes more vulnerable to noise in RTT
3560f8782eaSNeal Cardwell * measurements (e.g., delayed ACKs or other ACK compression effects). This
3570f8782eaSNeal Cardwell * noise may cause BBR to under-estimate the rate.
3580f8782eaSNeal Cardwell */
bbr_bdp(struct sock * sk,u32 bw,int gain)359232aa8ecSPriyaranjan Jha static u32 bbr_bdp(struct sock *sk, u32 bw, int gain)
3600f8782eaSNeal Cardwell {
3610f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
362232aa8ecSPriyaranjan Jha u32 bdp;
3630f8782eaSNeal Cardwell u64 w;
3640f8782eaSNeal Cardwell
3650f8782eaSNeal Cardwell /* If we've never had a valid RTT sample, cap cwnd at the initial
3660f8782eaSNeal Cardwell * default. This should only happen when the connection is not using TCP
3670f8782eaSNeal Cardwell * timestamps and has retransmitted all of the SYN/SYNACK/data packets
3680f8782eaSNeal Cardwell * ACKed so far. In this case, an RTO can cut cwnd to 1, in which
3690f8782eaSNeal Cardwell * case we need to slow-start up toward something safe: TCP_INIT_CWND.
3700f8782eaSNeal Cardwell */
3710f8782eaSNeal Cardwell if (unlikely(bbr->min_rtt_us == ~0U)) /* no valid RTT samples yet? */
3720f8782eaSNeal Cardwell return TCP_INIT_CWND; /* be safe: cap at default initial cwnd*/
3730f8782eaSNeal Cardwell
3740f8782eaSNeal Cardwell w = (u64)bw * bbr->min_rtt_us;
3750f8782eaSNeal Cardwell
376de8e1bebSLuke Hsiao /* Apply a gain to the given value, remove the BW_SCALE shift, and
377de8e1bebSLuke Hsiao * round the value up to avoid a negative feedback loop.
378de8e1bebSLuke Hsiao */
379232aa8ecSPriyaranjan Jha bdp = (((w * gain) >> BBR_SCALE) + BW_UNIT - 1) / BW_UNIT;
380232aa8ecSPriyaranjan Jha
381232aa8ecSPriyaranjan Jha return bdp;
382232aa8ecSPriyaranjan Jha }
383232aa8ecSPriyaranjan Jha
384232aa8ecSPriyaranjan Jha /* To achieve full performance in high-speed paths, we budget enough cwnd to
385232aa8ecSPriyaranjan Jha * fit full-sized skbs in-flight on both end hosts to fully utilize the path:
386232aa8ecSPriyaranjan Jha * - one skb in sending host Qdisc,
387232aa8ecSPriyaranjan Jha * - one skb in sending host TSO/GSO engine
388232aa8ecSPriyaranjan Jha * - one skb being received by receiver host LRO/GRO/delayed-ACK engine
389232aa8ecSPriyaranjan Jha * Don't worry, at low rates (bbr_min_tso_rate) this won't bloat cwnd because
390232aa8ecSPriyaranjan Jha * in such cases tso_segs_goal is 1. The minimum cwnd is 4 packets,
391232aa8ecSPriyaranjan Jha * which allows 2 outstanding 2-packet sequences, to try to keep pipe
392232aa8ecSPriyaranjan Jha * full even with ACK-every-other-packet delayed ACKs.
393232aa8ecSPriyaranjan Jha */
bbr_quantization_budget(struct sock * sk,u32 cwnd)3946b3656a6SKevin(Yudong) Yang static u32 bbr_quantization_budget(struct sock *sk, u32 cwnd)
395232aa8ecSPriyaranjan Jha {
396232aa8ecSPriyaranjan Jha struct bbr *bbr = inet_csk_ca(sk);
3970f8782eaSNeal Cardwell
3980f8782eaSNeal Cardwell /* Allow enough full-sized skbs in flight to utilize end systems. */
39971abf467SEric Dumazet cwnd += 3 * bbr_tso_segs_goal(sk);
4000f8782eaSNeal Cardwell
4010f8782eaSNeal Cardwell /* Reduce delayed ACKs by rounding up cwnd to the next even number. */
4020f8782eaSNeal Cardwell cwnd = (cwnd + 1) & ~1U;
4030f8782eaSNeal Cardwell
404383d4709SNeal Cardwell /* Ensure gain cycling gets inflight above BDP even for small BDPs. */
4056b3656a6SKevin(Yudong) Yang if (bbr->mode == BBR_PROBE_BW && bbr->cycle_idx == 0)
406383d4709SNeal Cardwell cwnd += 2;
407383d4709SNeal Cardwell
4080f8782eaSNeal Cardwell return cwnd;
4090f8782eaSNeal Cardwell }
4100f8782eaSNeal Cardwell
411232aa8ecSPriyaranjan Jha /* Find inflight based on min RTT and the estimated bottleneck bandwidth. */
bbr_inflight(struct sock * sk,u32 bw,int gain)412232aa8ecSPriyaranjan Jha static u32 bbr_inflight(struct sock *sk, u32 bw, int gain)
413232aa8ecSPriyaranjan Jha {
414232aa8ecSPriyaranjan Jha u32 inflight;
415232aa8ecSPriyaranjan Jha
416232aa8ecSPriyaranjan Jha inflight = bbr_bdp(sk, bw, gain);
4176b3656a6SKevin(Yudong) Yang inflight = bbr_quantization_budget(sk, inflight);
418232aa8ecSPriyaranjan Jha
419232aa8ecSPriyaranjan Jha return inflight;
420232aa8ecSPriyaranjan Jha }
421232aa8ecSPriyaranjan Jha
422a87c83d5SNeal Cardwell /* With pacing at lower layers, there's often less data "in the network" than
423a87c83d5SNeal Cardwell * "in flight". With TSQ and departure time pacing at lower layers (e.g. fq),
424a87c83d5SNeal Cardwell * we often have several skbs queued in the pacing layer with a pre-scheduled
425a87c83d5SNeal Cardwell * earliest departure time (EDT). BBR adapts its pacing rate based on the
426a87c83d5SNeal Cardwell * inflight level that it estimates has already been "baked in" by previous
427a87c83d5SNeal Cardwell * departure time decisions. We calculate a rough estimate of the number of our
428a87c83d5SNeal Cardwell * packets that might be in the network at the earliest departure time for the
429a87c83d5SNeal Cardwell * next skb scheduled:
430a87c83d5SNeal Cardwell * in_network_at_edt = inflight_at_edt - (EDT - now) * bw
431a87c83d5SNeal Cardwell * If we're increasing inflight, then we want to know if the transmit of the
432a87c83d5SNeal Cardwell * EDT skb will push inflight above the target, so inflight_at_edt includes
433a87c83d5SNeal Cardwell * bbr_tso_segs_goal() from the skb departing at EDT. If decreasing inflight,
434a87c83d5SNeal Cardwell * then estimate if inflight will sink too low just before the EDT transmit.
435a87c83d5SNeal Cardwell */
bbr_packets_in_net_at_edt(struct sock * sk,u32 inflight_now)436a87c83d5SNeal Cardwell static u32 bbr_packets_in_net_at_edt(struct sock *sk, u32 inflight_now)
437a87c83d5SNeal Cardwell {
438a87c83d5SNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
439a87c83d5SNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
440a87c83d5SNeal Cardwell u64 now_ns, edt_ns, interval_us;
441a87c83d5SNeal Cardwell u32 interval_delivered, inflight_at_edt;
442a87c83d5SNeal Cardwell
443a87c83d5SNeal Cardwell now_ns = tp->tcp_clock_cache;
444a87c83d5SNeal Cardwell edt_ns = max(tp->tcp_wstamp_ns, now_ns);
445a87c83d5SNeal Cardwell interval_us = div_u64(edt_ns - now_ns, NSEC_PER_USEC);
446a87c83d5SNeal Cardwell interval_delivered = (u64)bbr_bw(sk) * interval_us >> BW_SCALE;
447a87c83d5SNeal Cardwell inflight_at_edt = inflight_now;
448a87c83d5SNeal Cardwell if (bbr->pacing_gain > BBR_UNIT) /* increasing inflight */
449a87c83d5SNeal Cardwell inflight_at_edt += bbr_tso_segs_goal(sk); /* include EDT skb */
450a87c83d5SNeal Cardwell if (interval_delivered >= inflight_at_edt)
451a87c83d5SNeal Cardwell return 0;
452a87c83d5SNeal Cardwell return inflight_at_edt - interval_delivered;
453a87c83d5SNeal Cardwell }
454a87c83d5SNeal Cardwell
45578dc70ebSPriyaranjan Jha /* Find the cwnd increment based on estimate of ack aggregation */
bbr_ack_aggregation_cwnd(struct sock * sk)45678dc70ebSPriyaranjan Jha static u32 bbr_ack_aggregation_cwnd(struct sock *sk)
45778dc70ebSPriyaranjan Jha {
45878dc70ebSPriyaranjan Jha u32 max_aggr_cwnd, aggr_cwnd = 0;
45978dc70ebSPriyaranjan Jha
46078dc70ebSPriyaranjan Jha if (bbr_extra_acked_gain && bbr_full_bw_reached(sk)) {
46178dc70ebSPriyaranjan Jha max_aggr_cwnd = ((u64)bbr_bw(sk) * bbr_extra_acked_max_us)
46278dc70ebSPriyaranjan Jha / BW_UNIT;
46378dc70ebSPriyaranjan Jha aggr_cwnd = (bbr_extra_acked_gain * bbr_extra_acked(sk))
46478dc70ebSPriyaranjan Jha >> BBR_SCALE;
46578dc70ebSPriyaranjan Jha aggr_cwnd = min(aggr_cwnd, max_aggr_cwnd);
46678dc70ebSPriyaranjan Jha }
46778dc70ebSPriyaranjan Jha
46878dc70ebSPriyaranjan Jha return aggr_cwnd;
46978dc70ebSPriyaranjan Jha }
47078dc70ebSPriyaranjan Jha
4710f8782eaSNeal Cardwell /* An optimization in BBR to reduce losses: On the first round of recovery, we
4720f8782eaSNeal Cardwell * follow the packet conservation principle: send P packets per P packets acked.
4730f8782eaSNeal Cardwell * After that, we slow-start and send at most 2*P packets per P packets acked.
4740f8782eaSNeal Cardwell * After recovery finishes, or upon undo, we restore the cwnd we had when
4750f8782eaSNeal Cardwell * recovery started (capped by the target cwnd based on estimated BDP).
4760f8782eaSNeal Cardwell *
4770f8782eaSNeal Cardwell * TODO(ycheng/ncardwell): implement a rate-based approach.
4780f8782eaSNeal Cardwell */
bbr_set_cwnd_to_recover_or_restore(struct sock * sk,const struct rate_sample * rs,u32 acked,u32 * new_cwnd)4790f8782eaSNeal Cardwell static bool bbr_set_cwnd_to_recover_or_restore(
4800f8782eaSNeal Cardwell struct sock *sk, const struct rate_sample *rs, u32 acked, u32 *new_cwnd)
4810f8782eaSNeal Cardwell {
4820f8782eaSNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
4830f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
4840f8782eaSNeal Cardwell u8 prev_state = bbr->prev_ca_state, state = inet_csk(sk)->icsk_ca_state;
48540570375SEric Dumazet u32 cwnd = tcp_snd_cwnd(tp);
4860f8782eaSNeal Cardwell
4870f8782eaSNeal Cardwell /* An ACK for P pkts should release at most 2*P packets. We do this
4880f8782eaSNeal Cardwell * in two steps. First, here we deduct the number of lost packets.
4890f8782eaSNeal Cardwell * Then, in bbr_set_cwnd() we slow start up toward the target cwnd.
4900f8782eaSNeal Cardwell */
4910f8782eaSNeal Cardwell if (rs->losses > 0)
4920f8782eaSNeal Cardwell cwnd = max_t(s32, cwnd - rs->losses, 1);
4930f8782eaSNeal Cardwell
4940f8782eaSNeal Cardwell if (state == TCP_CA_Recovery && prev_state != TCP_CA_Recovery) {
4950f8782eaSNeal Cardwell /* Starting 1st round of Recovery, so do packet conservation. */
4960f8782eaSNeal Cardwell bbr->packet_conservation = 1;
4970f8782eaSNeal Cardwell bbr->next_rtt_delivered = tp->delivered; /* start round now */
4980f8782eaSNeal Cardwell /* Cut unused cwnd from app behavior, TSQ, or TSO deferral: */
4990f8782eaSNeal Cardwell cwnd = tcp_packets_in_flight(tp) + acked;
5000f8782eaSNeal Cardwell } else if (prev_state >= TCP_CA_Recovery && state < TCP_CA_Recovery) {
5010f8782eaSNeal Cardwell /* Exiting loss recovery; restore cwnd saved before recovery. */
502fb998862SKevin Yang cwnd = max(cwnd, bbr->prior_cwnd);
5030f8782eaSNeal Cardwell bbr->packet_conservation = 0;
5040f8782eaSNeal Cardwell }
5050f8782eaSNeal Cardwell bbr->prev_ca_state = state;
5060f8782eaSNeal Cardwell
5070f8782eaSNeal Cardwell if (bbr->packet_conservation) {
5080f8782eaSNeal Cardwell *new_cwnd = max(cwnd, tcp_packets_in_flight(tp) + acked);
5090f8782eaSNeal Cardwell return true; /* yes, using packet conservation */
5100f8782eaSNeal Cardwell }
5110f8782eaSNeal Cardwell *new_cwnd = cwnd;
5120f8782eaSNeal Cardwell return false;
5130f8782eaSNeal Cardwell }
5140f8782eaSNeal Cardwell
5150f8782eaSNeal Cardwell /* Slow-start up toward target cwnd (if bw estimate is growing, or packet loss
5160f8782eaSNeal Cardwell * has drawn us down below target), or snap down to target if we're above it.
5170f8782eaSNeal Cardwell */
bbr_set_cwnd(struct sock * sk,const struct rate_sample * rs,u32 acked,u32 bw,int gain)5180f8782eaSNeal Cardwell static void bbr_set_cwnd(struct sock *sk, const struct rate_sample *rs,
5190f8782eaSNeal Cardwell u32 acked, u32 bw, int gain)
5200f8782eaSNeal Cardwell {
5210f8782eaSNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
5220f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
52340570375SEric Dumazet u32 cwnd = tcp_snd_cwnd(tp), target_cwnd = 0;
5240f8782eaSNeal Cardwell
5250f8782eaSNeal Cardwell if (!acked)
5268e995bf1SKevin Yang goto done; /* no packet fully ACKed; just apply caps */
5270f8782eaSNeal Cardwell
5280f8782eaSNeal Cardwell if (bbr_set_cwnd_to_recover_or_restore(sk, rs, acked, &cwnd))
5290f8782eaSNeal Cardwell goto done;
5300f8782eaSNeal Cardwell
531232aa8ecSPriyaranjan Jha target_cwnd = bbr_bdp(sk, bw, gain);
53278dc70ebSPriyaranjan Jha
53378dc70ebSPriyaranjan Jha /* Increment the cwnd to account for excess ACKed data that seems
53478dc70ebSPriyaranjan Jha * due to aggregation (of data and/or ACKs) visible in the ACK stream.
53578dc70ebSPriyaranjan Jha */
53678dc70ebSPriyaranjan Jha target_cwnd += bbr_ack_aggregation_cwnd(sk);
5376b3656a6SKevin(Yudong) Yang target_cwnd = bbr_quantization_budget(sk, target_cwnd);
53878dc70ebSPriyaranjan Jha
53978dc70ebSPriyaranjan Jha /* If we're below target cwnd, slow start cwnd toward target cwnd. */
5400f8782eaSNeal Cardwell if (bbr_full_bw_reached(sk)) /* only cut cwnd if we filled the pipe */
5410f8782eaSNeal Cardwell cwnd = min(cwnd + acked, target_cwnd);
5420f8782eaSNeal Cardwell else if (cwnd < target_cwnd || tp->delivered < TCP_INIT_CWND)
5430f8782eaSNeal Cardwell cwnd = cwnd + acked;
5440f8782eaSNeal Cardwell cwnd = max(cwnd, bbr_cwnd_min_target);
5450f8782eaSNeal Cardwell
5460f8782eaSNeal Cardwell done:
54740570375SEric Dumazet tcp_snd_cwnd_set(tp, min(cwnd, tp->snd_cwnd_clamp)); /* apply global cap */
5480f8782eaSNeal Cardwell if (bbr->mode == BBR_PROBE_RTT) /* drain queue, refresh min_rtt */
54940570375SEric Dumazet tcp_snd_cwnd_set(tp, min(tcp_snd_cwnd(tp), bbr_cwnd_min_target));
5500f8782eaSNeal Cardwell }
5510f8782eaSNeal Cardwell
5520f8782eaSNeal Cardwell /* End cycle phase if it's time and/or we hit the phase's in-flight target. */
bbr_is_next_cycle_phase(struct sock * sk,const struct rate_sample * rs)5530f8782eaSNeal Cardwell static bool bbr_is_next_cycle_phase(struct sock *sk,
5540f8782eaSNeal Cardwell const struct rate_sample *rs)
5550f8782eaSNeal Cardwell {
5560f8782eaSNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
5570f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
5580f8782eaSNeal Cardwell bool is_full_length =
5599a568de4SEric Dumazet tcp_stamp_us_delta(tp->delivered_mstamp, bbr->cycle_mstamp) >
5600f8782eaSNeal Cardwell bbr->min_rtt_us;
5610f8782eaSNeal Cardwell u32 inflight, bw;
5620f8782eaSNeal Cardwell
5630f8782eaSNeal Cardwell /* The pacing_gain of 1.0 paces at the estimated bw to try to fully
5640f8782eaSNeal Cardwell * use the pipe without increasing the queue.
5650f8782eaSNeal Cardwell */
5660f8782eaSNeal Cardwell if (bbr->pacing_gain == BBR_UNIT)
5670f8782eaSNeal Cardwell return is_full_length; /* just use wall clock time */
5680f8782eaSNeal Cardwell
569a87c83d5SNeal Cardwell inflight = bbr_packets_in_net_at_edt(sk, rs->prior_in_flight);
5700f8782eaSNeal Cardwell bw = bbr_max_bw(sk);
5710f8782eaSNeal Cardwell
5720f8782eaSNeal Cardwell /* A pacing_gain > 1.0 probes for bw by trying to raise inflight to at
5730f8782eaSNeal Cardwell * least pacing_gain*BDP; this may take more than min_rtt if min_rtt is
5740f8782eaSNeal Cardwell * small (e.g. on a LAN). We do not persist if packets are lost, since
5750f8782eaSNeal Cardwell * a path with small buffers may not hold that much.
5760f8782eaSNeal Cardwell */
5770f8782eaSNeal Cardwell if (bbr->pacing_gain > BBR_UNIT)
5780f8782eaSNeal Cardwell return is_full_length &&
5790f8782eaSNeal Cardwell (rs->losses || /* perhaps pacing_gain*BDP won't fit */
580232aa8ecSPriyaranjan Jha inflight >= bbr_inflight(sk, bw, bbr->pacing_gain));
5810f8782eaSNeal Cardwell
5820f8782eaSNeal Cardwell /* A pacing_gain < 1.0 tries to drain extra queue we added if bw
5830f8782eaSNeal Cardwell * probing didn't find more bw. If inflight falls to match BDP then we
5840f8782eaSNeal Cardwell * estimate queue is drained; persisting would underutilize the pipe.
5850f8782eaSNeal Cardwell */
5860f8782eaSNeal Cardwell return is_full_length ||
587232aa8ecSPriyaranjan Jha inflight <= bbr_inflight(sk, bw, BBR_UNIT);
5880f8782eaSNeal Cardwell }
5890f8782eaSNeal Cardwell
bbr_advance_cycle_phase(struct sock * sk)5900f8782eaSNeal Cardwell static void bbr_advance_cycle_phase(struct sock *sk)
5910f8782eaSNeal Cardwell {
5920f8782eaSNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
5930f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
5940f8782eaSNeal Cardwell
5950f8782eaSNeal Cardwell bbr->cycle_idx = (bbr->cycle_idx + 1) & (CYCLE_LEN - 1);
5960f8782eaSNeal Cardwell bbr->cycle_mstamp = tp->delivered_mstamp;
5970f8782eaSNeal Cardwell }
5980f8782eaSNeal Cardwell
5990f8782eaSNeal Cardwell /* Gain cycling: cycle pacing gain to converge to fair share of available bw. */
bbr_update_cycle_phase(struct sock * sk,const struct rate_sample * rs)6000f8782eaSNeal Cardwell static void bbr_update_cycle_phase(struct sock *sk,
6010f8782eaSNeal Cardwell const struct rate_sample *rs)
6020f8782eaSNeal Cardwell {
6030f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
6040f8782eaSNeal Cardwell
6053aff3b4bSNeal Cardwell if (bbr->mode == BBR_PROBE_BW && bbr_is_next_cycle_phase(sk, rs))
6060f8782eaSNeal Cardwell bbr_advance_cycle_phase(sk);
6070f8782eaSNeal Cardwell }
6080f8782eaSNeal Cardwell
bbr_reset_startup_mode(struct sock * sk)6090f8782eaSNeal Cardwell static void bbr_reset_startup_mode(struct sock *sk)
6100f8782eaSNeal Cardwell {
6110f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
6120f8782eaSNeal Cardwell
6130f8782eaSNeal Cardwell bbr->mode = BBR_STARTUP;
6140f8782eaSNeal Cardwell }
6150f8782eaSNeal Cardwell
bbr_reset_probe_bw_mode(struct sock * sk)6160f8782eaSNeal Cardwell static void bbr_reset_probe_bw_mode(struct sock *sk)
6170f8782eaSNeal Cardwell {
6180f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
6190f8782eaSNeal Cardwell
6200f8782eaSNeal Cardwell bbr->mode = BBR_PROBE_BW;
6218032bf12SJason A. Donenfeld bbr->cycle_idx = CYCLE_LEN - 1 - get_random_u32_below(bbr_cycle_rand);
6220f8782eaSNeal Cardwell bbr_advance_cycle_phase(sk); /* flip to next phase of gain cycle */
6230f8782eaSNeal Cardwell }
6240f8782eaSNeal Cardwell
bbr_reset_mode(struct sock * sk)6250f8782eaSNeal Cardwell static void bbr_reset_mode(struct sock *sk)
6260f8782eaSNeal Cardwell {
6270f8782eaSNeal Cardwell if (!bbr_full_bw_reached(sk))
6280f8782eaSNeal Cardwell bbr_reset_startup_mode(sk);
6290f8782eaSNeal Cardwell else
6300f8782eaSNeal Cardwell bbr_reset_probe_bw_mode(sk);
6310f8782eaSNeal Cardwell }
6320f8782eaSNeal Cardwell
6330f8782eaSNeal Cardwell /* Start a new long-term sampling interval. */
bbr_reset_lt_bw_sampling_interval(struct sock * sk)6340f8782eaSNeal Cardwell static void bbr_reset_lt_bw_sampling_interval(struct sock *sk)
6350f8782eaSNeal Cardwell {
6360f8782eaSNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
6370f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
6380f8782eaSNeal Cardwell
6399a568de4SEric Dumazet bbr->lt_last_stamp = div_u64(tp->delivered_mstamp, USEC_PER_MSEC);
6400f8782eaSNeal Cardwell bbr->lt_last_delivered = tp->delivered;
6410f8782eaSNeal Cardwell bbr->lt_last_lost = tp->lost;
6420f8782eaSNeal Cardwell bbr->lt_rtt_cnt = 0;
6430f8782eaSNeal Cardwell }
6440f8782eaSNeal Cardwell
6450f8782eaSNeal Cardwell /* Completely reset long-term bandwidth sampling. */
bbr_reset_lt_bw_sampling(struct sock * sk)6460f8782eaSNeal Cardwell static void bbr_reset_lt_bw_sampling(struct sock *sk)
6470f8782eaSNeal Cardwell {
6480f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
6490f8782eaSNeal Cardwell
6500f8782eaSNeal Cardwell bbr->lt_bw = 0;
6510f8782eaSNeal Cardwell bbr->lt_use_bw = 0;
6520f8782eaSNeal Cardwell bbr->lt_is_sampling = false;
6530f8782eaSNeal Cardwell bbr_reset_lt_bw_sampling_interval(sk);
6540f8782eaSNeal Cardwell }
6550f8782eaSNeal Cardwell
6560f8782eaSNeal Cardwell /* Long-term bw sampling interval is done. Estimate whether we're policed. */
bbr_lt_bw_interval_done(struct sock * sk,u32 bw)6570f8782eaSNeal Cardwell static void bbr_lt_bw_interval_done(struct sock *sk, u32 bw)
6580f8782eaSNeal Cardwell {
6590f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
6600f8782eaSNeal Cardwell u32 diff;
6610f8782eaSNeal Cardwell
6620f8782eaSNeal Cardwell if (bbr->lt_bw) { /* do we have bw from a previous interval? */
6630f8782eaSNeal Cardwell /* Is new bw close to the lt_bw from the previous interval? */
6640f8782eaSNeal Cardwell diff = abs(bw - bbr->lt_bw);
6650f8782eaSNeal Cardwell if ((diff * BBR_UNIT <= bbr_lt_bw_ratio * bbr->lt_bw) ||
6660f8782eaSNeal Cardwell (bbr_rate_bytes_per_sec(sk, diff, BBR_UNIT) <=
6670f8782eaSNeal Cardwell bbr_lt_bw_diff)) {
6680f8782eaSNeal Cardwell /* All criteria are met; estimate we're policed. */
6690f8782eaSNeal Cardwell bbr->lt_bw = (bw + bbr->lt_bw) >> 1; /* avg 2 intvls */
6700f8782eaSNeal Cardwell bbr->lt_use_bw = 1;
6710f8782eaSNeal Cardwell bbr->pacing_gain = BBR_UNIT; /* try to avoid drops */
6720f8782eaSNeal Cardwell bbr->lt_rtt_cnt = 0;
6730f8782eaSNeal Cardwell return;
6740f8782eaSNeal Cardwell }
6750f8782eaSNeal Cardwell }
6760f8782eaSNeal Cardwell bbr->lt_bw = bw;
6770f8782eaSNeal Cardwell bbr_reset_lt_bw_sampling_interval(sk);
6780f8782eaSNeal Cardwell }
6790f8782eaSNeal Cardwell
6800f8782eaSNeal Cardwell /* Token-bucket traffic policers are common (see "An Internet-Wide Analysis of
6810f8782eaSNeal Cardwell * Traffic Policing", SIGCOMM 2016). BBR detects token-bucket policers and
6820f8782eaSNeal Cardwell * explicitly models their policed rate, to reduce unnecessary losses. We
6830f8782eaSNeal Cardwell * estimate that we're policed if we see 2 consecutive sampling intervals with
6840f8782eaSNeal Cardwell * consistent throughput and high packet loss. If we think we're being policed,
6850f8782eaSNeal Cardwell * set lt_bw to the "long-term" average delivery rate from those 2 intervals.
6860f8782eaSNeal Cardwell */
bbr_lt_bw_sampling(struct sock * sk,const struct rate_sample * rs)6870f8782eaSNeal Cardwell static void bbr_lt_bw_sampling(struct sock *sk, const struct rate_sample *rs)
6880f8782eaSNeal Cardwell {
6890f8782eaSNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
6900f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
6910f8782eaSNeal Cardwell u32 lost, delivered;
6920f8782eaSNeal Cardwell u64 bw;
6939a568de4SEric Dumazet u32 t;
6940f8782eaSNeal Cardwell
6950f8782eaSNeal Cardwell if (bbr->lt_use_bw) { /* already using long-term rate, lt_bw? */
6960f8782eaSNeal Cardwell if (bbr->mode == BBR_PROBE_BW && bbr->round_start &&
6970f8782eaSNeal Cardwell ++bbr->lt_rtt_cnt >= bbr_lt_bw_max_rtts) {
6980f8782eaSNeal Cardwell bbr_reset_lt_bw_sampling(sk); /* stop using lt_bw */
6990f8782eaSNeal Cardwell bbr_reset_probe_bw_mode(sk); /* restart gain cycling */
7000f8782eaSNeal Cardwell }
7010f8782eaSNeal Cardwell return;
7020f8782eaSNeal Cardwell }
7030f8782eaSNeal Cardwell
7040f8782eaSNeal Cardwell /* Wait for the first loss before sampling, to let the policer exhaust
7050f8782eaSNeal Cardwell * its tokens and estimate the steady-state rate allowed by the policer.
7060f8782eaSNeal Cardwell * Starting samples earlier includes bursts that over-estimate the bw.
7070f8782eaSNeal Cardwell */
7080f8782eaSNeal Cardwell if (!bbr->lt_is_sampling) {
7090f8782eaSNeal Cardwell if (!rs->losses)
7100f8782eaSNeal Cardwell return;
7110f8782eaSNeal Cardwell bbr_reset_lt_bw_sampling_interval(sk);
7120f8782eaSNeal Cardwell bbr->lt_is_sampling = true;
7130f8782eaSNeal Cardwell }
7140f8782eaSNeal Cardwell
7150f8782eaSNeal Cardwell /* To avoid underestimates, reset sampling if we run out of data. */
7160f8782eaSNeal Cardwell if (rs->is_app_limited) {
7170f8782eaSNeal Cardwell bbr_reset_lt_bw_sampling(sk);
7180f8782eaSNeal Cardwell return;
7190f8782eaSNeal Cardwell }
7200f8782eaSNeal Cardwell
7210f8782eaSNeal Cardwell if (bbr->round_start)
7220f8782eaSNeal Cardwell bbr->lt_rtt_cnt++; /* count round trips in this interval */
7230f8782eaSNeal Cardwell if (bbr->lt_rtt_cnt < bbr_lt_intvl_min_rtts)
7240f8782eaSNeal Cardwell return; /* sampling interval needs to be longer */
7250f8782eaSNeal Cardwell if (bbr->lt_rtt_cnt > 4 * bbr_lt_intvl_min_rtts) {
7260f8782eaSNeal Cardwell bbr_reset_lt_bw_sampling(sk); /* interval is too long */
7270f8782eaSNeal Cardwell return;
7280f8782eaSNeal Cardwell }
7290f8782eaSNeal Cardwell
7300f8782eaSNeal Cardwell /* End sampling interval when a packet is lost, so we estimate the
7310f8782eaSNeal Cardwell * policer tokens were exhausted. Stopping the sampling before the
7320f8782eaSNeal Cardwell * tokens are exhausted under-estimates the policed rate.
7330f8782eaSNeal Cardwell */
7340f8782eaSNeal Cardwell if (!rs->losses)
7350f8782eaSNeal Cardwell return;
7360f8782eaSNeal Cardwell
7370f8782eaSNeal Cardwell /* Calculate packets lost and delivered in sampling interval. */
7380f8782eaSNeal Cardwell lost = tp->lost - bbr->lt_last_lost;
7390f8782eaSNeal Cardwell delivered = tp->delivered - bbr->lt_last_delivered;
7400f8782eaSNeal Cardwell /* Is loss rate (lost/delivered) >= lt_loss_thresh? If not, wait. */
7410f8782eaSNeal Cardwell if (!delivered || (lost << BBR_SCALE) < bbr_lt_loss_thresh * delivered)
7420f8782eaSNeal Cardwell return;
7430f8782eaSNeal Cardwell
7440f8782eaSNeal Cardwell /* Find average delivery rate in this sampling interval. */
7459a568de4SEric Dumazet t = div_u64(tp->delivered_mstamp, USEC_PER_MSEC) - bbr->lt_last_stamp;
7469a568de4SEric Dumazet if ((s32)t < 1)
7479a568de4SEric Dumazet return; /* interval is less than one ms, so wait */
7489a568de4SEric Dumazet /* Check if can multiply without overflow */
7499a568de4SEric Dumazet if (t >= ~0U / USEC_PER_MSEC) {
7500f8782eaSNeal Cardwell bbr_reset_lt_bw_sampling(sk); /* interval too long; reset */
7510f8782eaSNeal Cardwell return;
7520f8782eaSNeal Cardwell }
7539a568de4SEric Dumazet t *= USEC_PER_MSEC;
7540f8782eaSNeal Cardwell bw = (u64)delivered * BW_UNIT;
7550f8782eaSNeal Cardwell do_div(bw, t);
7560f8782eaSNeal Cardwell bbr_lt_bw_interval_done(sk, bw);
7570f8782eaSNeal Cardwell }
7580f8782eaSNeal Cardwell
7590f8782eaSNeal Cardwell /* Estimate the bandwidth based on how fast packets are delivered */
bbr_update_bw(struct sock * sk,const struct rate_sample * rs)7600f8782eaSNeal Cardwell static void bbr_update_bw(struct sock *sk, const struct rate_sample *rs)
7610f8782eaSNeal Cardwell {
7620f8782eaSNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
7630f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
7640f8782eaSNeal Cardwell u64 bw;
7650f8782eaSNeal Cardwell
7660f8782eaSNeal Cardwell bbr->round_start = 0;
7670f8782eaSNeal Cardwell if (rs->delivered < 0 || rs->interval_us <= 0)
7680f8782eaSNeal Cardwell return; /* Not a valid observation */
7690f8782eaSNeal Cardwell
7700f8782eaSNeal Cardwell /* See if we've reached the next RTT */
7710f8782eaSNeal Cardwell if (!before(rs->prior_delivered, bbr->next_rtt_delivered)) {
7720f8782eaSNeal Cardwell bbr->next_rtt_delivered = tp->delivered;
7730f8782eaSNeal Cardwell bbr->rtt_cnt++;
7740f8782eaSNeal Cardwell bbr->round_start = 1;
7750f8782eaSNeal Cardwell bbr->packet_conservation = 0;
7760f8782eaSNeal Cardwell }
7770f8782eaSNeal Cardwell
7780f8782eaSNeal Cardwell bbr_lt_bw_sampling(sk, rs);
7790f8782eaSNeal Cardwell
7800f8782eaSNeal Cardwell /* Divide delivered by the interval to find a (lower bound) bottleneck
7810f8782eaSNeal Cardwell * bandwidth sample. Delivered is in packets and interval_us in uS and
7820f8782eaSNeal Cardwell * ratio will be <<1 for most connections. So delivered is first scaled.
7830f8782eaSNeal Cardwell */
7845b2f1f30SWen Yang bw = div64_long((u64)rs->delivered * BW_UNIT, rs->interval_us);
7850f8782eaSNeal Cardwell
7860f8782eaSNeal Cardwell /* If this sample is application-limited, it is likely to have a very
7870f8782eaSNeal Cardwell * low delivered count that represents application behavior rather than
7880f8782eaSNeal Cardwell * the available network rate. Such a sample could drag down estimated
7890f8782eaSNeal Cardwell * bw, causing needless slow-down. Thus, to continue to send at the
7900f8782eaSNeal Cardwell * last measured network rate, we filter out app-limited samples unless
7910f8782eaSNeal Cardwell * they describe the path bw at least as well as our bw model.
7920f8782eaSNeal Cardwell *
7930f8782eaSNeal Cardwell * So the goal during app-limited phase is to proceed with the best
7940f8782eaSNeal Cardwell * network rate no matter how long. We automatically leave this
7950f8782eaSNeal Cardwell * phase when app writes faster than the network can deliver :)
7960f8782eaSNeal Cardwell */
7970f8782eaSNeal Cardwell if (!rs->is_app_limited || bw >= bbr_max_bw(sk)) {
7980f8782eaSNeal Cardwell /* Incorporate new sample into our max bw filter. */
7990f8782eaSNeal Cardwell minmax_running_max(&bbr->bw, bbr_bw_rtts, bbr->rtt_cnt, bw);
8000f8782eaSNeal Cardwell }
8010f8782eaSNeal Cardwell }
8020f8782eaSNeal Cardwell
80378dc70ebSPriyaranjan Jha /* Estimates the windowed max degree of ack aggregation.
80478dc70ebSPriyaranjan Jha * This is used to provision extra in-flight data to keep sending during
80578dc70ebSPriyaranjan Jha * inter-ACK silences.
80678dc70ebSPriyaranjan Jha *
80778dc70ebSPriyaranjan Jha * Degree of ack aggregation is estimated as extra data acked beyond expected.
80878dc70ebSPriyaranjan Jha *
80978dc70ebSPriyaranjan Jha * max_extra_acked = "maximum recent excess data ACKed beyond max_bw * interval"
81078dc70ebSPriyaranjan Jha * cwnd += max_extra_acked
81178dc70ebSPriyaranjan Jha *
81278dc70ebSPriyaranjan Jha * Max extra_acked is clamped by cwnd and bw * bbr_extra_acked_max_us (100 ms).
81378dc70ebSPriyaranjan Jha * Max filter is an approximate sliding window of 5-10 (packet timed) round
81478dc70ebSPriyaranjan Jha * trips.
81578dc70ebSPriyaranjan Jha */
bbr_update_ack_aggregation(struct sock * sk,const struct rate_sample * rs)81678dc70ebSPriyaranjan Jha static void bbr_update_ack_aggregation(struct sock *sk,
81778dc70ebSPriyaranjan Jha const struct rate_sample *rs)
81878dc70ebSPriyaranjan Jha {
81978dc70ebSPriyaranjan Jha u32 epoch_us, expected_acked, extra_acked;
82078dc70ebSPriyaranjan Jha struct bbr *bbr = inet_csk_ca(sk);
82178dc70ebSPriyaranjan Jha struct tcp_sock *tp = tcp_sk(sk);
82278dc70ebSPriyaranjan Jha
82378dc70ebSPriyaranjan Jha if (!bbr_extra_acked_gain || rs->acked_sacked <= 0 ||
82478dc70ebSPriyaranjan Jha rs->delivered < 0 || rs->interval_us <= 0)
82578dc70ebSPriyaranjan Jha return;
82678dc70ebSPriyaranjan Jha
82778dc70ebSPriyaranjan Jha if (bbr->round_start) {
82878dc70ebSPriyaranjan Jha bbr->extra_acked_win_rtts = min(0x1F,
82978dc70ebSPriyaranjan Jha bbr->extra_acked_win_rtts + 1);
83078dc70ebSPriyaranjan Jha if (bbr->extra_acked_win_rtts >= bbr_extra_acked_win_rtts) {
83178dc70ebSPriyaranjan Jha bbr->extra_acked_win_rtts = 0;
83278dc70ebSPriyaranjan Jha bbr->extra_acked_win_idx = bbr->extra_acked_win_idx ?
83378dc70ebSPriyaranjan Jha 0 : 1;
83478dc70ebSPriyaranjan Jha bbr->extra_acked[bbr->extra_acked_win_idx] = 0;
83578dc70ebSPriyaranjan Jha }
83678dc70ebSPriyaranjan Jha }
83778dc70ebSPriyaranjan Jha
83878dc70ebSPriyaranjan Jha /* Compute how many packets we expected to be delivered over epoch. */
83978dc70ebSPriyaranjan Jha epoch_us = tcp_stamp_us_delta(tp->delivered_mstamp,
84078dc70ebSPriyaranjan Jha bbr->ack_epoch_mstamp);
84178dc70ebSPriyaranjan Jha expected_acked = ((u64)bbr_bw(sk) * epoch_us) / BW_UNIT;
84278dc70ebSPriyaranjan Jha
84378dc70ebSPriyaranjan Jha /* Reset the aggregation epoch if ACK rate is below expected rate or
84478dc70ebSPriyaranjan Jha * significantly large no. of ack received since epoch (potentially
84578dc70ebSPriyaranjan Jha * quite old epoch).
84678dc70ebSPriyaranjan Jha */
84778dc70ebSPriyaranjan Jha if (bbr->ack_epoch_acked <= expected_acked ||
84878dc70ebSPriyaranjan Jha (bbr->ack_epoch_acked + rs->acked_sacked >=
84978dc70ebSPriyaranjan Jha bbr_ack_epoch_acked_reset_thresh)) {
85078dc70ebSPriyaranjan Jha bbr->ack_epoch_acked = 0;
85178dc70ebSPriyaranjan Jha bbr->ack_epoch_mstamp = tp->delivered_mstamp;
85278dc70ebSPriyaranjan Jha expected_acked = 0;
85378dc70ebSPriyaranjan Jha }
85478dc70ebSPriyaranjan Jha
85578dc70ebSPriyaranjan Jha /* Compute excess data delivered, beyond what was expected. */
85678dc70ebSPriyaranjan Jha bbr->ack_epoch_acked = min_t(u32, 0xFFFFF,
85778dc70ebSPriyaranjan Jha bbr->ack_epoch_acked + rs->acked_sacked);
85878dc70ebSPriyaranjan Jha extra_acked = bbr->ack_epoch_acked - expected_acked;
85940570375SEric Dumazet extra_acked = min(extra_acked, tcp_snd_cwnd(tp));
86078dc70ebSPriyaranjan Jha if (extra_acked > bbr->extra_acked[bbr->extra_acked_win_idx])
86178dc70ebSPriyaranjan Jha bbr->extra_acked[bbr->extra_acked_win_idx] = extra_acked;
86278dc70ebSPriyaranjan Jha }
86378dc70ebSPriyaranjan Jha
8640f8782eaSNeal Cardwell /* Estimate when the pipe is full, using the change in delivery rate: BBR
8650f8782eaSNeal Cardwell * estimates that STARTUP filled the pipe if the estimated bw hasn't changed by
8660f8782eaSNeal Cardwell * at least bbr_full_bw_thresh (25%) after bbr_full_bw_cnt (3) non-app-limited
8670f8782eaSNeal Cardwell * rounds. Why 3 rounds: 1: rwin autotuning grows the rwin, 2: we fill the
8680f8782eaSNeal Cardwell * higher rwin, 3: we get higher delivery rate samples. Or transient
8690f8782eaSNeal Cardwell * cross-traffic or radio noise can go away. CUBIC Hystart shares a similar
8700f8782eaSNeal Cardwell * design goal, but uses delay and inter-ACK spacing instead of bandwidth.
8710f8782eaSNeal Cardwell */
bbr_check_full_bw_reached(struct sock * sk,const struct rate_sample * rs)8720f8782eaSNeal Cardwell static void bbr_check_full_bw_reached(struct sock *sk,
8730f8782eaSNeal Cardwell const struct rate_sample *rs)
8740f8782eaSNeal Cardwell {
8750f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
8760f8782eaSNeal Cardwell u32 bw_thresh;
8770f8782eaSNeal Cardwell
8780f8782eaSNeal Cardwell if (bbr_full_bw_reached(sk) || !bbr->round_start || rs->is_app_limited)
8790f8782eaSNeal Cardwell return;
8800f8782eaSNeal Cardwell
8810f8782eaSNeal Cardwell bw_thresh = (u64)bbr->full_bw * bbr_full_bw_thresh >> BBR_SCALE;
8820f8782eaSNeal Cardwell if (bbr_max_bw(sk) >= bw_thresh) {
8830f8782eaSNeal Cardwell bbr->full_bw = bbr_max_bw(sk);
8840f8782eaSNeal Cardwell bbr->full_bw_cnt = 0;
8850f8782eaSNeal Cardwell return;
8860f8782eaSNeal Cardwell }
8870f8782eaSNeal Cardwell ++bbr->full_bw_cnt;
888c589e69bSNeal Cardwell bbr->full_bw_reached = bbr->full_bw_cnt >= bbr_full_bw_cnt;
8890f8782eaSNeal Cardwell }
8900f8782eaSNeal Cardwell
8910f8782eaSNeal Cardwell /* If pipe is probably full, drain the queue and then enter steady-state. */
bbr_check_drain(struct sock * sk,const struct rate_sample * rs)8920f8782eaSNeal Cardwell static void bbr_check_drain(struct sock *sk, const struct rate_sample *rs)
8930f8782eaSNeal Cardwell {
8940f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
8950f8782eaSNeal Cardwell
8960f8782eaSNeal Cardwell if (bbr->mode == BBR_STARTUP && bbr_full_bw_reached(sk)) {
8970f8782eaSNeal Cardwell bbr->mode = BBR_DRAIN; /* drain queue we created */
89853794570SYousuk Seung tcp_sk(sk)->snd_ssthresh =
899232aa8ecSPriyaranjan Jha bbr_inflight(sk, bbr_max_bw(sk), BBR_UNIT);
9000f8782eaSNeal Cardwell } /* fall through to check if in-flight is already small: */
9010f8782eaSNeal Cardwell if (bbr->mode == BBR_DRAIN &&
902a87c83d5SNeal Cardwell bbr_packets_in_net_at_edt(sk, tcp_packets_in_flight(tcp_sk(sk))) <=
903232aa8ecSPriyaranjan Jha bbr_inflight(sk, bbr_max_bw(sk), BBR_UNIT))
9040f8782eaSNeal Cardwell bbr_reset_probe_bw_mode(sk); /* we estimate queue is drained */
9050f8782eaSNeal Cardwell }
9060f8782eaSNeal Cardwell
bbr_check_probe_rtt_done(struct sock * sk)907fb998862SKevin Yang static void bbr_check_probe_rtt_done(struct sock *sk)
908fb998862SKevin Yang {
909fb998862SKevin Yang struct tcp_sock *tp = tcp_sk(sk);
910fb998862SKevin Yang struct bbr *bbr = inet_csk_ca(sk);
911fb998862SKevin Yang
912fb998862SKevin Yang if (!(bbr->probe_rtt_done_stamp &&
913fb998862SKevin Yang after(tcp_jiffies32, bbr->probe_rtt_done_stamp)))
914fb998862SKevin Yang return;
915fb998862SKevin Yang
916fb998862SKevin Yang bbr->min_rtt_stamp = tcp_jiffies32; /* wait a while until PROBE_RTT */
91740570375SEric Dumazet tcp_snd_cwnd_set(tp, max(tcp_snd_cwnd(tp), bbr->prior_cwnd));
918fb998862SKevin Yang bbr_reset_mode(sk);
919fb998862SKevin Yang }
920fb998862SKevin Yang
9210f8782eaSNeal Cardwell /* The goal of PROBE_RTT mode is to have BBR flows cooperatively and
9220f8782eaSNeal Cardwell * periodically drain the bottleneck queue, to converge to measure the true
9230f8782eaSNeal Cardwell * min_rtt (unloaded propagation delay). This allows the flows to keep queues
9240f8782eaSNeal Cardwell * small (reducing queuing delay and packet loss) and achieve fairness among
9250f8782eaSNeal Cardwell * BBR flows.
9260f8782eaSNeal Cardwell *
9270f8782eaSNeal Cardwell * The min_rtt filter window is 10 seconds. When the min_rtt estimate expires,
9280f8782eaSNeal Cardwell * we enter PROBE_RTT mode and cap the cwnd at bbr_cwnd_min_target=4 packets.
9290f8782eaSNeal Cardwell * After at least bbr_probe_rtt_mode_ms=200ms and at least one packet-timed
9300f8782eaSNeal Cardwell * round trip elapsed with that flight size <= 4, we leave PROBE_RTT mode and
9310f8782eaSNeal Cardwell * re-enter the previous mode. BBR uses 200ms to approximately bound the
9320f8782eaSNeal Cardwell * performance penalty of PROBE_RTT's cwnd capping to roughly 2% (200ms/10s).
9330f8782eaSNeal Cardwell *
9340f8782eaSNeal Cardwell * Note that flows need only pay 2% if they are busy sending over the last 10
9350f8782eaSNeal Cardwell * seconds. Interactive applications (e.g., Web, RPCs, video chunks) often have
9360f8782eaSNeal Cardwell * natural silences or low-rate periods within 10 seconds where the rate is low
9370f8782eaSNeal Cardwell * enough for long enough to drain its queue in the bottleneck. We pick up
9380f8782eaSNeal Cardwell * these min RTT measurements opportunistically with our min_rtt filter. :-)
9390f8782eaSNeal Cardwell */
bbr_update_min_rtt(struct sock * sk,const struct rate_sample * rs)9400f8782eaSNeal Cardwell static void bbr_update_min_rtt(struct sock *sk, const struct rate_sample *rs)
9410f8782eaSNeal Cardwell {
9420f8782eaSNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
9430f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
9440f8782eaSNeal Cardwell bool filter_expired;
9450f8782eaSNeal Cardwell
9460f8782eaSNeal Cardwell /* Track min RTT seen in the min_rtt_win_sec filter window: */
9472660bfa8SEric Dumazet filter_expired = after(tcp_jiffies32,
9480f8782eaSNeal Cardwell bbr->min_rtt_stamp + bbr_min_rtt_win_sec * HZ);
9490f8782eaSNeal Cardwell if (rs->rtt_us >= 0 &&
9501b9e2a8cSRyan Sharpelletti (rs->rtt_us < bbr->min_rtt_us ||
951e4286603SYuchung Cheng (filter_expired && !rs->is_ack_delayed))) {
9520f8782eaSNeal Cardwell bbr->min_rtt_us = rs->rtt_us;
9532660bfa8SEric Dumazet bbr->min_rtt_stamp = tcp_jiffies32;
9540f8782eaSNeal Cardwell }
9550f8782eaSNeal Cardwell
9560f8782eaSNeal Cardwell if (bbr_probe_rtt_mode_ms > 0 && filter_expired &&
9570f8782eaSNeal Cardwell !bbr->idle_restart && bbr->mode != BBR_PROBE_RTT) {
9580f8782eaSNeal Cardwell bbr->mode = BBR_PROBE_RTT; /* dip, drain queue */
9590f8782eaSNeal Cardwell bbr_save_cwnd(sk); /* note cwnd so we can restore it */
9600f8782eaSNeal Cardwell bbr->probe_rtt_done_stamp = 0;
9610f8782eaSNeal Cardwell }
9620f8782eaSNeal Cardwell
9630f8782eaSNeal Cardwell if (bbr->mode == BBR_PROBE_RTT) {
9640f8782eaSNeal Cardwell /* Ignore low rate samples during this mode. */
9650f8782eaSNeal Cardwell tp->app_limited =
9660f8782eaSNeal Cardwell (tp->delivered + tcp_packets_in_flight(tp)) ? : 1;
9670f8782eaSNeal Cardwell /* Maintain min packets in flight for max(200 ms, 1 round). */
9680f8782eaSNeal Cardwell if (!bbr->probe_rtt_done_stamp &&
9690f8782eaSNeal Cardwell tcp_packets_in_flight(tp) <= bbr_cwnd_min_target) {
9702660bfa8SEric Dumazet bbr->probe_rtt_done_stamp = tcp_jiffies32 +
9710f8782eaSNeal Cardwell msecs_to_jiffies(bbr_probe_rtt_mode_ms);
9720f8782eaSNeal Cardwell bbr->probe_rtt_round_done = 0;
9730f8782eaSNeal Cardwell bbr->next_rtt_delivered = tp->delivered;
9740f8782eaSNeal Cardwell } else if (bbr->probe_rtt_done_stamp) {
9750f8782eaSNeal Cardwell if (bbr->round_start)
9760f8782eaSNeal Cardwell bbr->probe_rtt_round_done = 1;
977fb998862SKevin Yang if (bbr->probe_rtt_round_done)
978fb998862SKevin Yang bbr_check_probe_rtt_done(sk);
9790f8782eaSNeal Cardwell }
9800f8782eaSNeal Cardwell }
981e6e6a278SNeal Cardwell /* Restart after idle ends only once we process a new S/ACK for data */
982e6e6a278SNeal Cardwell if (rs->delivered > 0)
9830f8782eaSNeal Cardwell bbr->idle_restart = 0;
9840f8782eaSNeal Cardwell }
9850f8782eaSNeal Cardwell
bbr_update_gains(struct sock * sk)986cf33e25cSNeal Cardwell static void bbr_update_gains(struct sock *sk)
987cf33e25cSNeal Cardwell {
988cf33e25cSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
989cf33e25cSNeal Cardwell
990cf33e25cSNeal Cardwell switch (bbr->mode) {
991cf33e25cSNeal Cardwell case BBR_STARTUP:
992cf33e25cSNeal Cardwell bbr->pacing_gain = bbr_high_gain;
993cf33e25cSNeal Cardwell bbr->cwnd_gain = bbr_high_gain;
994cf33e25cSNeal Cardwell break;
995cf33e25cSNeal Cardwell case BBR_DRAIN:
996cf33e25cSNeal Cardwell bbr->pacing_gain = bbr_drain_gain; /* slow, to drain */
997cf33e25cSNeal Cardwell bbr->cwnd_gain = bbr_high_gain; /* keep cwnd */
998cf33e25cSNeal Cardwell break;
999cf33e25cSNeal Cardwell case BBR_PROBE_BW:
1000cf33e25cSNeal Cardwell bbr->pacing_gain = (bbr->lt_use_bw ?
1001cf33e25cSNeal Cardwell BBR_UNIT :
1002cf33e25cSNeal Cardwell bbr_pacing_gain[bbr->cycle_idx]);
1003cf33e25cSNeal Cardwell bbr->cwnd_gain = bbr_cwnd_gain;
1004cf33e25cSNeal Cardwell break;
1005cf33e25cSNeal Cardwell case BBR_PROBE_RTT:
1006cf33e25cSNeal Cardwell bbr->pacing_gain = BBR_UNIT;
1007cf33e25cSNeal Cardwell bbr->cwnd_gain = BBR_UNIT;
1008cf33e25cSNeal Cardwell break;
1009cf33e25cSNeal Cardwell default:
1010cf33e25cSNeal Cardwell WARN_ONCE(1, "BBR bad mode: %u\n", bbr->mode);
1011cf33e25cSNeal Cardwell break;
1012cf33e25cSNeal Cardwell }
1013cf33e25cSNeal Cardwell }
1014cf33e25cSNeal Cardwell
bbr_update_model(struct sock * sk,const struct rate_sample * rs)10150f8782eaSNeal Cardwell static void bbr_update_model(struct sock *sk, const struct rate_sample *rs)
10160f8782eaSNeal Cardwell {
10170f8782eaSNeal Cardwell bbr_update_bw(sk, rs);
101878dc70ebSPriyaranjan Jha bbr_update_ack_aggregation(sk, rs);
10190f8782eaSNeal Cardwell bbr_update_cycle_phase(sk, rs);
10200f8782eaSNeal Cardwell bbr_check_full_bw_reached(sk, rs);
10210f8782eaSNeal Cardwell bbr_check_drain(sk, rs);
10220f8782eaSNeal Cardwell bbr_update_min_rtt(sk, rs);
1023cf33e25cSNeal Cardwell bbr_update_gains(sk);
10240f8782eaSNeal Cardwell }
10250f8782eaSNeal Cardwell
bbr_main(struct sock * sk,const struct rate_sample * rs)1026*400031e0SDavid Vernet __bpf_kfunc static void bbr_main(struct sock *sk, const struct rate_sample *rs)
10270f8782eaSNeal Cardwell {
10280f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
10290f8782eaSNeal Cardwell u32 bw;
10300f8782eaSNeal Cardwell
10310f8782eaSNeal Cardwell bbr_update_model(sk, rs);
10320f8782eaSNeal Cardwell
10330f8782eaSNeal Cardwell bw = bbr_bw(sk);
10340f8782eaSNeal Cardwell bbr_set_pacing_rate(sk, bw, bbr->pacing_gain);
10350f8782eaSNeal Cardwell bbr_set_cwnd(sk, rs, rs->acked_sacked, bw, bbr->cwnd_gain);
10360f8782eaSNeal Cardwell }
10370f8782eaSNeal Cardwell
bbr_init(struct sock * sk)1038*400031e0SDavid Vernet __bpf_kfunc static void bbr_init(struct sock *sk)
10390f8782eaSNeal Cardwell {
10400f8782eaSNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
10410f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
10420f8782eaSNeal Cardwell
10430f8782eaSNeal Cardwell bbr->prior_cwnd = 0;
104453794570SYousuk Seung tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
10450f8782eaSNeal Cardwell bbr->rtt_cnt = 0;
10466de035feSNeal Cardwell bbr->next_rtt_delivered = tp->delivered;
10470f8782eaSNeal Cardwell bbr->prev_ca_state = TCP_CA_Open;
10480f8782eaSNeal Cardwell bbr->packet_conservation = 0;
10490f8782eaSNeal Cardwell
10500f8782eaSNeal Cardwell bbr->probe_rtt_done_stamp = 0;
10510f8782eaSNeal Cardwell bbr->probe_rtt_round_done = 0;
10520f8782eaSNeal Cardwell bbr->min_rtt_us = tcp_min_rtt(tp);
10532660bfa8SEric Dumazet bbr->min_rtt_stamp = tcp_jiffies32;
10540f8782eaSNeal Cardwell
10550f8782eaSNeal Cardwell minmax_reset(&bbr->bw, bbr->rtt_cnt, 0); /* init max bw to 0 */
10560f8782eaSNeal Cardwell
105732984565SNeal Cardwell bbr->has_seen_rtt = 0;
105879135b89SNeal Cardwell bbr_init_pacing_rate_from_rtt(sk);
10590f8782eaSNeal Cardwell
10600f8782eaSNeal Cardwell bbr->round_start = 0;
10610f8782eaSNeal Cardwell bbr->idle_restart = 0;
1062c589e69bSNeal Cardwell bbr->full_bw_reached = 0;
10630f8782eaSNeal Cardwell bbr->full_bw = 0;
10640f8782eaSNeal Cardwell bbr->full_bw_cnt = 0;
10659a568de4SEric Dumazet bbr->cycle_mstamp = 0;
10660f8782eaSNeal Cardwell bbr->cycle_idx = 0;
10670f8782eaSNeal Cardwell bbr_reset_lt_bw_sampling(sk);
10680f8782eaSNeal Cardwell bbr_reset_startup_mode(sk);
1069218af599SEric Dumazet
107078dc70ebSPriyaranjan Jha bbr->ack_epoch_mstamp = tp->tcp_mstamp;
107178dc70ebSPriyaranjan Jha bbr->ack_epoch_acked = 0;
107278dc70ebSPriyaranjan Jha bbr->extra_acked_win_rtts = 0;
107378dc70ebSPriyaranjan Jha bbr->extra_acked_win_idx = 0;
107478dc70ebSPriyaranjan Jha bbr->extra_acked[0] = 0;
107578dc70ebSPriyaranjan Jha bbr->extra_acked[1] = 0;
107678dc70ebSPriyaranjan Jha
1077218af599SEric Dumazet cmpxchg(&sk->sk_pacing_status, SK_PACING_NONE, SK_PACING_NEEDED);
10780f8782eaSNeal Cardwell }
10790f8782eaSNeal Cardwell
bbr_sndbuf_expand(struct sock * sk)1080*400031e0SDavid Vernet __bpf_kfunc static u32 bbr_sndbuf_expand(struct sock *sk)
10810f8782eaSNeal Cardwell {
10820f8782eaSNeal Cardwell /* Provision 3 * cwnd since BBR may slow-start even during recovery. */
10830f8782eaSNeal Cardwell return 3;
10840f8782eaSNeal Cardwell }
10850f8782eaSNeal Cardwell
10860f8782eaSNeal Cardwell /* In theory BBR does not need to undo the cwnd since it does not
10870f8782eaSNeal Cardwell * always reduce cwnd on losses (see bbr_main()). Keep it for now.
10880f8782eaSNeal Cardwell */
bbr_undo_cwnd(struct sock * sk)1089*400031e0SDavid Vernet __bpf_kfunc static u32 bbr_undo_cwnd(struct sock *sk)
10900f8782eaSNeal Cardwell {
10912f6c498eSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
10922f6c498eSNeal Cardwell
10932f6c498eSNeal Cardwell bbr->full_bw = 0; /* spurious slow-down; reset full pipe detection */
10942f6c498eSNeal Cardwell bbr->full_bw_cnt = 0;
1095600647d4SNeal Cardwell bbr_reset_lt_bw_sampling(sk);
109640570375SEric Dumazet return tcp_snd_cwnd(tcp_sk(sk));
10970f8782eaSNeal Cardwell }
10980f8782eaSNeal Cardwell
10990f8782eaSNeal Cardwell /* Entering loss recovery, so save cwnd for when we exit or undo recovery. */
bbr_ssthresh(struct sock * sk)1100*400031e0SDavid Vernet __bpf_kfunc static u32 bbr_ssthresh(struct sock *sk)
11010f8782eaSNeal Cardwell {
11020f8782eaSNeal Cardwell bbr_save_cwnd(sk);
110353794570SYousuk Seung return tcp_sk(sk)->snd_ssthresh;
11040f8782eaSNeal Cardwell }
11050f8782eaSNeal Cardwell
bbr_get_info(struct sock * sk,u32 ext,int * attr,union tcp_cc_info * info)11060f8782eaSNeal Cardwell static size_t bbr_get_info(struct sock *sk, u32 ext, int *attr,
11070f8782eaSNeal Cardwell union tcp_cc_info *info)
11080f8782eaSNeal Cardwell {
11090f8782eaSNeal Cardwell if (ext & (1 << (INET_DIAG_BBRINFO - 1)) ||
11100f8782eaSNeal Cardwell ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
11110f8782eaSNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
11120f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
11130f8782eaSNeal Cardwell u64 bw = bbr_bw(sk);
11140f8782eaSNeal Cardwell
11150f8782eaSNeal Cardwell bw = bw * tp->mss_cache * USEC_PER_SEC >> BW_SCALE;
11160f8782eaSNeal Cardwell memset(&info->bbr, 0, sizeof(info->bbr));
11170f8782eaSNeal Cardwell info->bbr.bbr_bw_lo = (u32)bw;
11180f8782eaSNeal Cardwell info->bbr.bbr_bw_hi = (u32)(bw >> 32);
11190f8782eaSNeal Cardwell info->bbr.bbr_min_rtt = bbr->min_rtt_us;
11200f8782eaSNeal Cardwell info->bbr.bbr_pacing_gain = bbr->pacing_gain;
11210f8782eaSNeal Cardwell info->bbr.bbr_cwnd_gain = bbr->cwnd_gain;
11220f8782eaSNeal Cardwell *attr = INET_DIAG_BBRINFO;
11230f8782eaSNeal Cardwell return sizeof(info->bbr);
11240f8782eaSNeal Cardwell }
11250f8782eaSNeal Cardwell return 0;
11260f8782eaSNeal Cardwell }
11270f8782eaSNeal Cardwell
bbr_set_state(struct sock * sk,u8 new_state)1128*400031e0SDavid Vernet __bpf_kfunc static void bbr_set_state(struct sock *sk, u8 new_state)
11290f8782eaSNeal Cardwell {
11300f8782eaSNeal Cardwell struct bbr *bbr = inet_csk_ca(sk);
11310f8782eaSNeal Cardwell
11320f8782eaSNeal Cardwell if (new_state == TCP_CA_Loss) {
11330f8782eaSNeal Cardwell struct rate_sample rs = { .losses = 1 };
11340f8782eaSNeal Cardwell
11350f8782eaSNeal Cardwell bbr->prev_ca_state = TCP_CA_Loss;
11360f8782eaSNeal Cardwell bbr->full_bw = 0;
11370f8782eaSNeal Cardwell bbr->round_start = 1; /* treat RTO like end of a round */
11380f8782eaSNeal Cardwell bbr_lt_bw_sampling(sk, &rs);
11390f8782eaSNeal Cardwell }
11400f8782eaSNeal Cardwell }
11410f8782eaSNeal Cardwell
11420f8782eaSNeal Cardwell static struct tcp_congestion_ops tcp_bbr_cong_ops __read_mostly = {
11430f8782eaSNeal Cardwell .flags = TCP_CONG_NON_RESTRICTED,
11440f8782eaSNeal Cardwell .name = "bbr",
11450f8782eaSNeal Cardwell .owner = THIS_MODULE,
11460f8782eaSNeal Cardwell .init = bbr_init,
11470f8782eaSNeal Cardwell .cong_control = bbr_main,
11480f8782eaSNeal Cardwell .sndbuf_expand = bbr_sndbuf_expand,
11490f8782eaSNeal Cardwell .undo_cwnd = bbr_undo_cwnd,
11500f8782eaSNeal Cardwell .cwnd_event = bbr_cwnd_event,
11510f8782eaSNeal Cardwell .ssthresh = bbr_ssthresh,
1152dcb8c9b4SEric Dumazet .min_tso_segs = bbr_min_tso_segs,
11530f8782eaSNeal Cardwell .get_info = bbr_get_info,
11540f8782eaSNeal Cardwell .set_state = bbr_set_state,
11550f8782eaSNeal Cardwell };
11560f8782eaSNeal Cardwell
1157a4703e31SKumar Kartikeya Dwivedi BTF_SET8_START(tcp_bbr_check_kfunc_ids)
11580e32dfc8SKumar Kartikeya Dwivedi #ifdef CONFIG_X86
11590e32dfc8SKumar Kartikeya Dwivedi #ifdef CONFIG_DYNAMIC_FTRACE
1160a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bbr_init)
1161a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bbr_main)
1162a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bbr_sndbuf_expand)
1163a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bbr_undo_cwnd)
1164a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bbr_cwnd_event)
1165a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bbr_ssthresh)
1166a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bbr_min_tso_segs)
1167a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bbr_set_state)
11680e32dfc8SKumar Kartikeya Dwivedi #endif
11690e32dfc8SKumar Kartikeya Dwivedi #endif
1170a4703e31SKumar Kartikeya Dwivedi BTF_SET8_END(tcp_bbr_check_kfunc_ids)
11710e32dfc8SKumar Kartikeya Dwivedi
1172b202d844SKumar Kartikeya Dwivedi static const struct btf_kfunc_id_set tcp_bbr_kfunc_set = {
1173b202d844SKumar Kartikeya Dwivedi .owner = THIS_MODULE,
1174a4703e31SKumar Kartikeya Dwivedi .set = &tcp_bbr_check_kfunc_ids,
1175b202d844SKumar Kartikeya Dwivedi };
11760e32dfc8SKumar Kartikeya Dwivedi
bbr_register(void)11770f8782eaSNeal Cardwell static int __init bbr_register(void)
11780f8782eaSNeal Cardwell {
11790e32dfc8SKumar Kartikeya Dwivedi int ret;
11800e32dfc8SKumar Kartikeya Dwivedi
11810f8782eaSNeal Cardwell BUILD_BUG_ON(sizeof(struct bbr) > ICSK_CA_PRIV_SIZE);
1182b202d844SKumar Kartikeya Dwivedi
1183b202d844SKumar Kartikeya Dwivedi ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &tcp_bbr_kfunc_set);
1184b202d844SKumar Kartikeya Dwivedi if (ret < 0)
11850e32dfc8SKumar Kartikeya Dwivedi return ret;
1186b202d844SKumar Kartikeya Dwivedi return tcp_register_congestion_control(&tcp_bbr_cong_ops);
11870f8782eaSNeal Cardwell }
11880f8782eaSNeal Cardwell
bbr_unregister(void)11890f8782eaSNeal Cardwell static void __exit bbr_unregister(void)
11900f8782eaSNeal Cardwell {
11910f8782eaSNeal Cardwell tcp_unregister_congestion_control(&tcp_bbr_cong_ops);
11920f8782eaSNeal Cardwell }
11930f8782eaSNeal Cardwell
11940f8782eaSNeal Cardwell module_init(bbr_register);
11950f8782eaSNeal Cardwell module_exit(bbr_unregister);
11960f8782eaSNeal Cardwell
11970f8782eaSNeal Cardwell MODULE_AUTHOR("Van Jacobson <vanj@google.com>");
11980f8782eaSNeal Cardwell MODULE_AUTHOR("Neal Cardwell <ncardwell@google.com>");
11990f8782eaSNeal Cardwell MODULE_AUTHOR("Yuchung Cheng <ycheng@google.com>");
12000f8782eaSNeal Cardwell MODULE_AUTHOR("Soheil Hassas Yeganeh <soheil@google.com>");
12010f8782eaSNeal Cardwell MODULE_LICENSE("Dual BSD/GPL");
12020f8782eaSNeal Cardwell MODULE_DESCRIPTION("TCP BBR (Bottleneck Bandwidth and RTT)");
1203