Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 1 | /* Bottleneck Bandwidth and RTT (BBR) congestion control |
| 2 | * |
| 3 | * BBR congestion control computes the sending rate based on the delivery |
| 4 | * rate (throughput) estimated from ACKs. In a nutshell: |
| 5 | * |
| 6 | * On each ACK, update our model of the network path: |
| 7 | * bottleneck_bandwidth = windowed_max(delivered / elapsed, 10 round trips) |
| 8 | * min_rtt = windowed_min(rtt, 10 seconds) |
| 9 | * pacing_rate = pacing_gain * bottleneck_bandwidth |
| 10 | * cwnd = max(cwnd_gain * bottleneck_bandwidth * min_rtt, 4) |
| 11 | * |
| 12 | * The core algorithm does not react directly to packet losses or delays, |
| 13 | * although BBR may adjust the size of next send per ACK when loss is |
| 14 | * observed, or adjust the sending rate if it estimates there is a |
| 15 | * traffic policer, in order to keep the drop rate reasonable. |
| 16 | * |
Neal Cardwell | 9b9375b | 2016-10-27 13:26:37 -0400 | [diff] [blame] | 17 | * Here is a state transition diagram for BBR: |
| 18 | * |
| 19 | * | |
| 20 | * V |
| 21 | * +---> STARTUP ----+ |
| 22 | * | | | |
| 23 | * | V | |
| 24 | * | DRAIN ----+ |
| 25 | * | | | |
| 26 | * | V | |
| 27 | * +---> PROBE_BW ----+ |
| 28 | * | ^ | | |
| 29 | * | | | | |
| 30 | * | +----+ | |
| 31 | * | | |
| 32 | * +---- PROBE_RTT <--+ |
| 33 | * |
| 34 | * A BBR flow starts in STARTUP, and ramps up its sending rate quickly. |
| 35 | * When it estimates the pipe is full, it enters DRAIN to drain the queue. |
| 36 | * In steady state a BBR flow only uses PROBE_BW and PROBE_RTT. |
| 37 | * A long-lived BBR flow spends the vast majority of its time remaining |
| 38 | * (repeatedly) in PROBE_BW, fully probing and utilizing the pipe's bandwidth |
| 39 | * in a fair manner, with a small, bounded queue. *If* a flow has been |
| 40 | * continuously sending for the entire min_rtt window, and hasn't seen an RTT |
| 41 | * sample that matches or decreases its min_rtt estimate for 10 seconds, then |
| 42 | * it briefly enters PROBE_RTT to cut inflight to a minimum value to re-probe |
| 43 | * the path's two-way propagation delay (min_rtt). When exiting PROBE_RTT, if |
| 44 | * we estimated that we reached the full bw of the pipe then we enter PROBE_BW; |
| 45 | * otherwise we enter STARTUP to try to fill the pipe. |
| 46 | * |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 47 | * BBR is described in detail in: |
| 48 | * "BBR: Congestion-Based Congestion Control", |
| 49 | * Neal Cardwell, Yuchung Cheng, C. Stephen Gunn, Soheil Hassas Yeganeh, |
| 50 | * Van Jacobson. ACM Queue, Vol. 14 No. 5, September-October 2016. |
| 51 | * |
| 52 | * There is a public e-mail list for discussing BBR development and testing: |
| 53 | * https://groups.google.com/forum/#!forum/bbr-dev |
| 54 | * |
Eric Dumazet | 218af59 | 2017-05-16 04:24:36 -0700 | [diff] [blame] | 55 | * NOTE: BBR might be used with the fq qdisc ("man tc-fq") with pacing enabled, |
| 56 | * otherwise TCP stack falls back to an internal pacing using one high |
| 57 | * resolution timer per TCP socket and may use more resources. |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 58 | */ |
| 59 | #include <linux/module.h> |
| 60 | #include <net/tcp.h> |
| 61 | #include <linux/inet_diag.h> |
| 62 | #include <linux/inet.h> |
| 63 | #include <linux/random.h> |
| 64 | #include <linux/win_minmax.h> |
| 65 | |
| 66 | /* Scale factor for rate in pkt/uSec unit to avoid truncation in bandwidth |
| 67 | * estimation. The rate unit ~= (1500 bytes / 1 usec / 2^24) ~= 715 bps. |
| 68 | * This handles bandwidths from 0.06pps (715bps) to 256Mpps (3Tbps) in a u32. |
| 69 | * Since the minimum window is >=4 packets, the lower bound isn't |
| 70 | * an issue. The upper bound isn't an issue with existing technologies. |
| 71 | */ |
| 72 | #define BW_SCALE 24 |
| 73 | #define BW_UNIT (1 << BW_SCALE) |
| 74 | |
| 75 | #define BBR_SCALE 8 /* scaling factor for fractions in BBR (e.g. gains) */ |
| 76 | #define BBR_UNIT (1 << BBR_SCALE) |
| 77 | |
| 78 | /* BBR has the following modes for deciding how fast to send: */ |
| 79 | enum bbr_mode { |
| 80 | BBR_STARTUP, /* ramp up sending rate rapidly to fill pipe */ |
| 81 | BBR_DRAIN, /* drain any queue created during startup */ |
| 82 | BBR_PROBE_BW, /* discover, share bw: pace around estimated bw */ |
Neal Cardwell | 9b9375b | 2016-10-27 13:26:37 -0400 | [diff] [blame] | 83 | BBR_PROBE_RTT, /* cut inflight to min to probe min_rtt */ |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | /* BBR congestion control block */ |
| 87 | struct bbr { |
| 88 | u32 min_rtt_us; /* min RTT in min_rtt_win_sec window */ |
| 89 | u32 min_rtt_stamp; /* timestamp of min_rtt_us */ |
| 90 | u32 probe_rtt_done_stamp; /* end time for BBR_PROBE_RTT mode */ |
| 91 | struct minmax bw; /* Max recent delivery rate in pkts/uS << 24 */ |
| 92 | u32 rtt_cnt; /* count of packet-timed rounds elapsed */ |
| 93 | u32 next_rtt_delivered; /* scb->tx.delivered at end of round */ |
Eric Dumazet | 9a568de | 2017-05-16 14:00:14 -0700 | [diff] [blame] | 94 | u64 cycle_mstamp; /* time of this cycle phase start */ |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 95 | u32 mode:3, /* current bbr_mode in state machine */ |
| 96 | prev_ca_state:3, /* CA state on previous ACK */ |
| 97 | packet_conservation:1, /* use packet conservation? */ |
| 98 | restore_cwnd:1, /* decided to revert cwnd to old value */ |
| 99 | round_start:1, /* start of packet-timed tx->ack round? */ |
| 100 | tso_segs_goal:7, /* segments we want in each skb we send */ |
| 101 | idle_restart:1, /* restarting after idle? */ |
| 102 | probe_rtt_round_done:1, /* a BBR_PROBE_RTT round at 4 pkts? */ |
| 103 | unused:5, |
| 104 | lt_is_sampling:1, /* taking long-term ("LT") samples now? */ |
| 105 | lt_rtt_cnt:7, /* round trips in long-term interval */ |
| 106 | lt_use_bw:1; /* use lt_bw as our bw estimate? */ |
| 107 | u32 lt_bw; /* LT est delivery rate in pkts/uS << 24 */ |
| 108 | u32 lt_last_delivered; /* LT intvl start: tp->delivered */ |
| 109 | u32 lt_last_stamp; /* LT intvl start: tp->delivered_mstamp */ |
| 110 | u32 lt_last_lost; /* LT intvl start: tp->lost */ |
| 111 | u32 pacing_gain:10, /* current gain for setting pacing rate */ |
| 112 | cwnd_gain:10, /* current gain for setting cwnd */ |
| 113 | full_bw_cnt:3, /* number of rounds without large bw gains */ |
| 114 | cycle_idx:3, /* current index in pacing_gain cycle array */ |
| 115 | unused_b:6; |
| 116 | u32 prior_cwnd; /* prior cwnd upon entering loss recovery */ |
| 117 | u32 full_bw; /* recent bw, to estimate if pipe is full */ |
| 118 | }; |
| 119 | |
| 120 | #define CYCLE_LEN 8 /* number of phases in a pacing gain cycle */ |
| 121 | |
| 122 | /* Window length of bw filter (in rounds): */ |
| 123 | static const int bbr_bw_rtts = CYCLE_LEN + 2; |
| 124 | /* Window length of min_rtt filter (in sec): */ |
| 125 | static const u32 bbr_min_rtt_win_sec = 10; |
| 126 | /* Minimum time (in ms) spent at bbr_cwnd_min_target in BBR_PROBE_RTT mode: */ |
| 127 | static const u32 bbr_probe_rtt_mode_ms = 200; |
| 128 | /* Skip TSO below the following bandwidth (bits/sec): */ |
| 129 | static const int bbr_min_tso_rate = 1200000; |
| 130 | |
| 131 | /* We use a high_gain value of 2/ln(2) because it's the smallest pacing gain |
| 132 | * that will allow a smoothly increasing pacing rate that will double each RTT |
| 133 | * and send the same number of packets per RTT that an un-paced, slow-starting |
| 134 | * Reno or CUBIC flow would: |
| 135 | */ |
| 136 | static const int bbr_high_gain = BBR_UNIT * 2885 / 1000 + 1; |
| 137 | /* The pacing gain of 1/high_gain in BBR_DRAIN is calculated to typically drain |
| 138 | * the queue created in BBR_STARTUP in a single round: |
| 139 | */ |
| 140 | static const int bbr_drain_gain = BBR_UNIT * 1000 / 2885; |
| 141 | /* The gain for deriving steady-state cwnd tolerates delayed/stretched ACKs: */ |
| 142 | static const int bbr_cwnd_gain = BBR_UNIT * 2; |
| 143 | /* The pacing_gain values for the PROBE_BW gain cycle, to discover/share bw: */ |
| 144 | static const int bbr_pacing_gain[] = { |
| 145 | BBR_UNIT * 5 / 4, /* probe for more available bw */ |
| 146 | BBR_UNIT * 3 / 4, /* drain queue and/or yield bw to other flows */ |
| 147 | BBR_UNIT, BBR_UNIT, BBR_UNIT, /* cruise at 1.0*bw to utilize pipe, */ |
| 148 | BBR_UNIT, BBR_UNIT, BBR_UNIT /* without creating excess queue... */ |
| 149 | }; |
| 150 | /* Randomize the starting gain cycling phase over N phases: */ |
| 151 | static const u32 bbr_cycle_rand = 7; |
| 152 | |
| 153 | /* Try to keep at least this many packets in flight, if things go smoothly. For |
| 154 | * smooth functioning, a sliding window protocol ACKing every other packet |
| 155 | * needs at least 4 packets in flight: |
| 156 | */ |
| 157 | static const u32 bbr_cwnd_min_target = 4; |
| 158 | |
| 159 | /* To estimate if BBR_STARTUP mode (i.e. high_gain) has filled pipe... */ |
| 160 | /* If bw has increased significantly (1.25x), there may be more bw available: */ |
| 161 | static const u32 bbr_full_bw_thresh = BBR_UNIT * 5 / 4; |
| 162 | /* But after 3 rounds w/o significant bw growth, estimate pipe is full: */ |
| 163 | static const u32 bbr_full_bw_cnt = 3; |
| 164 | |
| 165 | /* "long-term" ("LT") bandwidth estimator parameters... */ |
| 166 | /* The minimum number of rounds in an LT bw sampling interval: */ |
| 167 | static const u32 bbr_lt_intvl_min_rtts = 4; |
| 168 | /* If lost/delivered ratio > 20%, interval is "lossy" and we may be policed: */ |
| 169 | static const u32 bbr_lt_loss_thresh = 50; |
| 170 | /* If 2 intervals have a bw ratio <= 1/8, their bw is "consistent": */ |
| 171 | static const u32 bbr_lt_bw_ratio = BBR_UNIT / 8; |
| 172 | /* If 2 intervals have a bw diff <= 4 Kbit/sec their bw is "consistent": */ |
| 173 | static const u32 bbr_lt_bw_diff = 4000 / 8; |
| 174 | /* If we estimate we're policed, use lt_bw for this many round trips: */ |
| 175 | static const u32 bbr_lt_bw_max_rtts = 48; |
| 176 | |
| 177 | /* Do we estimate that STARTUP filled the pipe? */ |
| 178 | static bool bbr_full_bw_reached(const struct sock *sk) |
| 179 | { |
| 180 | const struct bbr *bbr = inet_csk_ca(sk); |
| 181 | |
| 182 | return bbr->full_bw_cnt >= bbr_full_bw_cnt; |
| 183 | } |
| 184 | |
| 185 | /* Return the windowed max recent bandwidth sample, in pkts/uS << BW_SCALE. */ |
| 186 | static u32 bbr_max_bw(const struct sock *sk) |
| 187 | { |
| 188 | struct bbr *bbr = inet_csk_ca(sk); |
| 189 | |
| 190 | return minmax_get(&bbr->bw); |
| 191 | } |
| 192 | |
| 193 | /* Return the estimated bandwidth of the path, in pkts/uS << BW_SCALE. */ |
| 194 | static u32 bbr_bw(const struct sock *sk) |
| 195 | { |
| 196 | struct bbr *bbr = inet_csk_ca(sk); |
| 197 | |
| 198 | return bbr->lt_use_bw ? bbr->lt_bw : bbr_max_bw(sk); |
| 199 | } |
| 200 | |
| 201 | /* Return rate in bytes per second, optionally with a gain. |
| 202 | * The order here is chosen carefully to avoid overflow of u64. This should |
| 203 | * work for input rates of up to 2.9Tbit/sec and gain of 2.89x. |
| 204 | */ |
| 205 | static u64 bbr_rate_bytes_per_sec(struct sock *sk, u64 rate, int gain) |
| 206 | { |
| 207 | rate *= tcp_mss_to_mtu(sk, tcp_sk(sk)->mss_cache); |
| 208 | rate *= gain; |
| 209 | rate >>= BBR_SCALE; |
| 210 | rate *= USEC_PER_SEC; |
| 211 | return rate >> BW_SCALE; |
| 212 | } |
| 213 | |
Neal Cardwell | f19fd62 | 2017-07-14 17:49:22 -0400 | [diff] [blame] | 214 | /* Convert a BBR bw and gain factor to a pacing rate in bytes per second. */ |
| 215 | static u32 bbr_bw_to_pacing_rate(struct sock *sk, u32 bw, int gain) |
| 216 | { |
| 217 | u64 rate = bw; |
| 218 | |
| 219 | rate = bbr_rate_bytes_per_sec(sk, rate, gain); |
| 220 | rate = min_t(u64, rate, sk->sk_max_pacing_rate); |
| 221 | return rate; |
| 222 | } |
| 223 | |
Neal Cardwell | 79135b8 | 2017-07-14 17:49:23 -0400 | [diff] [blame^] | 224 | /* Initialize pacing rate to: high_gain * init_cwnd / RTT. */ |
| 225 | static void bbr_init_pacing_rate_from_rtt(struct sock *sk) |
| 226 | { |
| 227 | struct tcp_sock *tp = tcp_sk(sk); |
| 228 | u64 bw; |
| 229 | u32 rtt_us; |
| 230 | |
| 231 | if (tp->srtt_us) { /* any RTT sample yet? */ |
| 232 | rtt_us = max(tp->srtt_us >> 3, 1U); |
| 233 | } else { /* no RTT sample yet */ |
| 234 | rtt_us = USEC_PER_MSEC; /* use nominal default RTT */ |
| 235 | } |
| 236 | bw = (u64)tp->snd_cwnd * BW_UNIT; |
| 237 | do_div(bw, rtt_us); |
| 238 | sk->sk_pacing_rate = bbr_bw_to_pacing_rate(sk, bw, bbr_high_gain); |
| 239 | } |
| 240 | |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 241 | /* Pace using current bw estimate and a gain factor. In order to help drive the |
| 242 | * network toward lower queues while maintaining high utilization and low |
| 243 | * latency, the average pacing rate aims to be slightly (~1%) lower than the |
| 244 | * estimated bandwidth. This is an important aspect of the design. In this |
| 245 | * implementation this slightly lower pacing rate is achieved implicitly by not |
| 246 | * including link-layer headers in the packet size used for the pacing rate. |
| 247 | */ |
| 248 | static void bbr_set_pacing_rate(struct sock *sk, u32 bw, int gain) |
| 249 | { |
Neal Cardwell | f19fd62 | 2017-07-14 17:49:22 -0400 | [diff] [blame] | 250 | u32 rate = bbr_bw_to_pacing_rate(sk, bw, gain); |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 251 | |
Neal Cardwell | 4aea287 | 2017-07-14 17:49:21 -0400 | [diff] [blame] | 252 | if (bbr_full_bw_reached(sk) || rate > sk->sk_pacing_rate) |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 253 | sk->sk_pacing_rate = rate; |
| 254 | } |
| 255 | |
| 256 | /* Return count of segments we want in the skbs we send, or 0 for default. */ |
| 257 | static u32 bbr_tso_segs_goal(struct sock *sk) |
| 258 | { |
| 259 | struct bbr *bbr = inet_csk_ca(sk); |
| 260 | |
| 261 | return bbr->tso_segs_goal; |
| 262 | } |
| 263 | |
| 264 | static void bbr_set_tso_segs_goal(struct sock *sk) |
| 265 | { |
| 266 | struct tcp_sock *tp = tcp_sk(sk); |
| 267 | struct bbr *bbr = inet_csk_ca(sk); |
| 268 | u32 min_segs; |
| 269 | |
| 270 | min_segs = sk->sk_pacing_rate < (bbr_min_tso_rate >> 3) ? 1 : 2; |
| 271 | bbr->tso_segs_goal = min(tcp_tso_autosize(sk, tp->mss_cache, min_segs), |
| 272 | 0x7FU); |
| 273 | } |
| 274 | |
| 275 | /* Save "last known good" cwnd so we can restore it after losses or PROBE_RTT */ |
| 276 | static void bbr_save_cwnd(struct sock *sk) |
| 277 | { |
| 278 | struct tcp_sock *tp = tcp_sk(sk); |
| 279 | struct bbr *bbr = inet_csk_ca(sk); |
| 280 | |
| 281 | if (bbr->prev_ca_state < TCP_CA_Recovery && bbr->mode != BBR_PROBE_RTT) |
| 282 | bbr->prior_cwnd = tp->snd_cwnd; /* this cwnd is good enough */ |
| 283 | else /* loss recovery or BBR_PROBE_RTT have temporarily cut cwnd */ |
| 284 | bbr->prior_cwnd = max(bbr->prior_cwnd, tp->snd_cwnd); |
| 285 | } |
| 286 | |
| 287 | static void bbr_cwnd_event(struct sock *sk, enum tcp_ca_event event) |
| 288 | { |
| 289 | struct tcp_sock *tp = tcp_sk(sk); |
| 290 | struct bbr *bbr = inet_csk_ca(sk); |
| 291 | |
| 292 | if (event == CA_EVENT_TX_START && tp->app_limited) { |
| 293 | bbr->idle_restart = 1; |
| 294 | /* Avoid pointless buffer overflows: pace at est. bw if we don't |
| 295 | * need more speed (we're restarting from idle and app-limited). |
| 296 | */ |
| 297 | if (bbr->mode == BBR_PROBE_BW) |
| 298 | bbr_set_pacing_rate(sk, bbr_bw(sk), BBR_UNIT); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /* Find target cwnd. Right-size the cwnd based on min RTT and the |
| 303 | * estimated bottleneck bandwidth: |
| 304 | * |
| 305 | * cwnd = bw * min_rtt * gain = BDP * gain |
| 306 | * |
| 307 | * The key factor, gain, controls the amount of queue. While a small gain |
| 308 | * builds a smaller queue, it becomes more vulnerable to noise in RTT |
| 309 | * measurements (e.g., delayed ACKs or other ACK compression effects). This |
| 310 | * noise may cause BBR to under-estimate the rate. |
| 311 | * |
| 312 | * To achieve full performance in high-speed paths, we budget enough cwnd to |
| 313 | * fit full-sized skbs in-flight on both end hosts to fully utilize the path: |
| 314 | * - one skb in sending host Qdisc, |
| 315 | * - one skb in sending host TSO/GSO engine |
| 316 | * - one skb being received by receiver host LRO/GRO/delayed-ACK engine |
| 317 | * Don't worry, at low rates (bbr_min_tso_rate) this won't bloat cwnd because |
| 318 | * in such cases tso_segs_goal is 1. The minimum cwnd is 4 packets, |
| 319 | * which allows 2 outstanding 2-packet sequences, to try to keep pipe |
| 320 | * full even with ACK-every-other-packet delayed ACKs. |
| 321 | */ |
| 322 | static u32 bbr_target_cwnd(struct sock *sk, u32 bw, int gain) |
| 323 | { |
| 324 | struct bbr *bbr = inet_csk_ca(sk); |
| 325 | u32 cwnd; |
| 326 | u64 w; |
| 327 | |
| 328 | /* If we've never had a valid RTT sample, cap cwnd at the initial |
| 329 | * default. This should only happen when the connection is not using TCP |
| 330 | * timestamps and has retransmitted all of the SYN/SYNACK/data packets |
| 331 | * ACKed so far. In this case, an RTO can cut cwnd to 1, in which |
| 332 | * case we need to slow-start up toward something safe: TCP_INIT_CWND. |
| 333 | */ |
| 334 | if (unlikely(bbr->min_rtt_us == ~0U)) /* no valid RTT samples yet? */ |
| 335 | return TCP_INIT_CWND; /* be safe: cap at default initial cwnd*/ |
| 336 | |
| 337 | w = (u64)bw * bbr->min_rtt_us; |
| 338 | |
| 339 | /* Apply a gain to the given value, then remove the BW_SCALE shift. */ |
| 340 | cwnd = (((w * gain) >> BBR_SCALE) + BW_UNIT - 1) / BW_UNIT; |
| 341 | |
| 342 | /* Allow enough full-sized skbs in flight to utilize end systems. */ |
| 343 | cwnd += 3 * bbr->tso_segs_goal; |
| 344 | |
| 345 | /* Reduce delayed ACKs by rounding up cwnd to the next even number. */ |
| 346 | cwnd = (cwnd + 1) & ~1U; |
| 347 | |
| 348 | return cwnd; |
| 349 | } |
| 350 | |
| 351 | /* An optimization in BBR to reduce losses: On the first round of recovery, we |
| 352 | * follow the packet conservation principle: send P packets per P packets acked. |
| 353 | * After that, we slow-start and send at most 2*P packets per P packets acked. |
| 354 | * After recovery finishes, or upon undo, we restore the cwnd we had when |
| 355 | * recovery started (capped by the target cwnd based on estimated BDP). |
| 356 | * |
| 357 | * TODO(ycheng/ncardwell): implement a rate-based approach. |
| 358 | */ |
| 359 | static bool bbr_set_cwnd_to_recover_or_restore( |
| 360 | struct sock *sk, const struct rate_sample *rs, u32 acked, u32 *new_cwnd) |
| 361 | { |
| 362 | struct tcp_sock *tp = tcp_sk(sk); |
| 363 | struct bbr *bbr = inet_csk_ca(sk); |
| 364 | u8 prev_state = bbr->prev_ca_state, state = inet_csk(sk)->icsk_ca_state; |
| 365 | u32 cwnd = tp->snd_cwnd; |
| 366 | |
| 367 | /* An ACK for P pkts should release at most 2*P packets. We do this |
| 368 | * in two steps. First, here we deduct the number of lost packets. |
| 369 | * Then, in bbr_set_cwnd() we slow start up toward the target cwnd. |
| 370 | */ |
| 371 | if (rs->losses > 0) |
| 372 | cwnd = max_t(s32, cwnd - rs->losses, 1); |
| 373 | |
| 374 | if (state == TCP_CA_Recovery && prev_state != TCP_CA_Recovery) { |
| 375 | /* Starting 1st round of Recovery, so do packet conservation. */ |
| 376 | bbr->packet_conservation = 1; |
| 377 | bbr->next_rtt_delivered = tp->delivered; /* start round now */ |
| 378 | /* Cut unused cwnd from app behavior, TSQ, or TSO deferral: */ |
| 379 | cwnd = tcp_packets_in_flight(tp) + acked; |
| 380 | } else if (prev_state >= TCP_CA_Recovery && state < TCP_CA_Recovery) { |
| 381 | /* Exiting loss recovery; restore cwnd saved before recovery. */ |
| 382 | bbr->restore_cwnd = 1; |
| 383 | bbr->packet_conservation = 0; |
| 384 | } |
| 385 | bbr->prev_ca_state = state; |
| 386 | |
| 387 | if (bbr->restore_cwnd) { |
| 388 | /* Restore cwnd after exiting loss recovery or PROBE_RTT. */ |
| 389 | cwnd = max(cwnd, bbr->prior_cwnd); |
| 390 | bbr->restore_cwnd = 0; |
| 391 | } |
| 392 | |
| 393 | if (bbr->packet_conservation) { |
| 394 | *new_cwnd = max(cwnd, tcp_packets_in_flight(tp) + acked); |
| 395 | return true; /* yes, using packet conservation */ |
| 396 | } |
| 397 | *new_cwnd = cwnd; |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | /* Slow-start up toward target cwnd (if bw estimate is growing, or packet loss |
| 402 | * has drawn us down below target), or snap down to target if we're above it. |
| 403 | */ |
| 404 | static void bbr_set_cwnd(struct sock *sk, const struct rate_sample *rs, |
| 405 | u32 acked, u32 bw, int gain) |
| 406 | { |
| 407 | struct tcp_sock *tp = tcp_sk(sk); |
| 408 | struct bbr *bbr = inet_csk_ca(sk); |
| 409 | u32 cwnd = 0, target_cwnd = 0; |
| 410 | |
| 411 | if (!acked) |
| 412 | return; |
| 413 | |
| 414 | if (bbr_set_cwnd_to_recover_or_restore(sk, rs, acked, &cwnd)) |
| 415 | goto done; |
| 416 | |
| 417 | /* If we're below target cwnd, slow start cwnd toward target cwnd. */ |
| 418 | target_cwnd = bbr_target_cwnd(sk, bw, gain); |
| 419 | if (bbr_full_bw_reached(sk)) /* only cut cwnd if we filled the pipe */ |
| 420 | cwnd = min(cwnd + acked, target_cwnd); |
| 421 | else if (cwnd < target_cwnd || tp->delivered < TCP_INIT_CWND) |
| 422 | cwnd = cwnd + acked; |
| 423 | cwnd = max(cwnd, bbr_cwnd_min_target); |
| 424 | |
| 425 | done: |
| 426 | tp->snd_cwnd = min(cwnd, tp->snd_cwnd_clamp); /* apply global cap */ |
| 427 | if (bbr->mode == BBR_PROBE_RTT) /* drain queue, refresh min_rtt */ |
| 428 | tp->snd_cwnd = min(tp->snd_cwnd, bbr_cwnd_min_target); |
| 429 | } |
| 430 | |
| 431 | /* End cycle phase if it's time and/or we hit the phase's in-flight target. */ |
| 432 | static bool bbr_is_next_cycle_phase(struct sock *sk, |
| 433 | const struct rate_sample *rs) |
| 434 | { |
| 435 | struct tcp_sock *tp = tcp_sk(sk); |
| 436 | struct bbr *bbr = inet_csk_ca(sk); |
| 437 | bool is_full_length = |
Eric Dumazet | 9a568de | 2017-05-16 14:00:14 -0700 | [diff] [blame] | 438 | tcp_stamp_us_delta(tp->delivered_mstamp, bbr->cycle_mstamp) > |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 439 | bbr->min_rtt_us; |
| 440 | u32 inflight, bw; |
| 441 | |
| 442 | /* The pacing_gain of 1.0 paces at the estimated bw to try to fully |
| 443 | * use the pipe without increasing the queue. |
| 444 | */ |
| 445 | if (bbr->pacing_gain == BBR_UNIT) |
| 446 | return is_full_length; /* just use wall clock time */ |
| 447 | |
| 448 | inflight = rs->prior_in_flight; /* what was in-flight before ACK? */ |
| 449 | bw = bbr_max_bw(sk); |
| 450 | |
| 451 | /* A pacing_gain > 1.0 probes for bw by trying to raise inflight to at |
| 452 | * least pacing_gain*BDP; this may take more than min_rtt if min_rtt is |
| 453 | * small (e.g. on a LAN). We do not persist if packets are lost, since |
| 454 | * a path with small buffers may not hold that much. |
| 455 | */ |
| 456 | if (bbr->pacing_gain > BBR_UNIT) |
| 457 | return is_full_length && |
| 458 | (rs->losses || /* perhaps pacing_gain*BDP won't fit */ |
| 459 | inflight >= bbr_target_cwnd(sk, bw, bbr->pacing_gain)); |
| 460 | |
| 461 | /* A pacing_gain < 1.0 tries to drain extra queue we added if bw |
| 462 | * probing didn't find more bw. If inflight falls to match BDP then we |
| 463 | * estimate queue is drained; persisting would underutilize the pipe. |
| 464 | */ |
| 465 | return is_full_length || |
| 466 | inflight <= bbr_target_cwnd(sk, bw, BBR_UNIT); |
| 467 | } |
| 468 | |
| 469 | static void bbr_advance_cycle_phase(struct sock *sk) |
| 470 | { |
| 471 | struct tcp_sock *tp = tcp_sk(sk); |
| 472 | struct bbr *bbr = inet_csk_ca(sk); |
| 473 | |
| 474 | bbr->cycle_idx = (bbr->cycle_idx + 1) & (CYCLE_LEN - 1); |
| 475 | bbr->cycle_mstamp = tp->delivered_mstamp; |
| 476 | bbr->pacing_gain = bbr_pacing_gain[bbr->cycle_idx]; |
| 477 | } |
| 478 | |
| 479 | /* Gain cycling: cycle pacing gain to converge to fair share of available bw. */ |
| 480 | static void bbr_update_cycle_phase(struct sock *sk, |
| 481 | const struct rate_sample *rs) |
| 482 | { |
| 483 | struct bbr *bbr = inet_csk_ca(sk); |
| 484 | |
| 485 | if ((bbr->mode == BBR_PROBE_BW) && !bbr->lt_use_bw && |
| 486 | bbr_is_next_cycle_phase(sk, rs)) |
| 487 | bbr_advance_cycle_phase(sk); |
| 488 | } |
| 489 | |
| 490 | static void bbr_reset_startup_mode(struct sock *sk) |
| 491 | { |
| 492 | struct bbr *bbr = inet_csk_ca(sk); |
| 493 | |
| 494 | bbr->mode = BBR_STARTUP; |
| 495 | bbr->pacing_gain = bbr_high_gain; |
| 496 | bbr->cwnd_gain = bbr_high_gain; |
| 497 | } |
| 498 | |
| 499 | static void bbr_reset_probe_bw_mode(struct sock *sk) |
| 500 | { |
| 501 | struct bbr *bbr = inet_csk_ca(sk); |
| 502 | |
| 503 | bbr->mode = BBR_PROBE_BW; |
| 504 | bbr->pacing_gain = BBR_UNIT; |
| 505 | bbr->cwnd_gain = bbr_cwnd_gain; |
| 506 | bbr->cycle_idx = CYCLE_LEN - 1 - prandom_u32_max(bbr_cycle_rand); |
| 507 | bbr_advance_cycle_phase(sk); /* flip to next phase of gain cycle */ |
| 508 | } |
| 509 | |
| 510 | static void bbr_reset_mode(struct sock *sk) |
| 511 | { |
| 512 | if (!bbr_full_bw_reached(sk)) |
| 513 | bbr_reset_startup_mode(sk); |
| 514 | else |
| 515 | bbr_reset_probe_bw_mode(sk); |
| 516 | } |
| 517 | |
| 518 | /* Start a new long-term sampling interval. */ |
| 519 | static void bbr_reset_lt_bw_sampling_interval(struct sock *sk) |
| 520 | { |
| 521 | struct tcp_sock *tp = tcp_sk(sk); |
| 522 | struct bbr *bbr = inet_csk_ca(sk); |
| 523 | |
Eric Dumazet | 9a568de | 2017-05-16 14:00:14 -0700 | [diff] [blame] | 524 | bbr->lt_last_stamp = div_u64(tp->delivered_mstamp, USEC_PER_MSEC); |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 525 | bbr->lt_last_delivered = tp->delivered; |
| 526 | bbr->lt_last_lost = tp->lost; |
| 527 | bbr->lt_rtt_cnt = 0; |
| 528 | } |
| 529 | |
| 530 | /* Completely reset long-term bandwidth sampling. */ |
| 531 | static void bbr_reset_lt_bw_sampling(struct sock *sk) |
| 532 | { |
| 533 | struct bbr *bbr = inet_csk_ca(sk); |
| 534 | |
| 535 | bbr->lt_bw = 0; |
| 536 | bbr->lt_use_bw = 0; |
| 537 | bbr->lt_is_sampling = false; |
| 538 | bbr_reset_lt_bw_sampling_interval(sk); |
| 539 | } |
| 540 | |
| 541 | /* Long-term bw sampling interval is done. Estimate whether we're policed. */ |
| 542 | static void bbr_lt_bw_interval_done(struct sock *sk, u32 bw) |
| 543 | { |
| 544 | struct bbr *bbr = inet_csk_ca(sk); |
| 545 | u32 diff; |
| 546 | |
| 547 | if (bbr->lt_bw) { /* do we have bw from a previous interval? */ |
| 548 | /* Is new bw close to the lt_bw from the previous interval? */ |
| 549 | diff = abs(bw - bbr->lt_bw); |
| 550 | if ((diff * BBR_UNIT <= bbr_lt_bw_ratio * bbr->lt_bw) || |
| 551 | (bbr_rate_bytes_per_sec(sk, diff, BBR_UNIT) <= |
| 552 | bbr_lt_bw_diff)) { |
| 553 | /* All criteria are met; estimate we're policed. */ |
| 554 | bbr->lt_bw = (bw + bbr->lt_bw) >> 1; /* avg 2 intvls */ |
| 555 | bbr->lt_use_bw = 1; |
| 556 | bbr->pacing_gain = BBR_UNIT; /* try to avoid drops */ |
| 557 | bbr->lt_rtt_cnt = 0; |
| 558 | return; |
| 559 | } |
| 560 | } |
| 561 | bbr->lt_bw = bw; |
| 562 | bbr_reset_lt_bw_sampling_interval(sk); |
| 563 | } |
| 564 | |
| 565 | /* Token-bucket traffic policers are common (see "An Internet-Wide Analysis of |
| 566 | * Traffic Policing", SIGCOMM 2016). BBR detects token-bucket policers and |
| 567 | * explicitly models their policed rate, to reduce unnecessary losses. We |
| 568 | * estimate that we're policed if we see 2 consecutive sampling intervals with |
| 569 | * consistent throughput and high packet loss. If we think we're being policed, |
| 570 | * set lt_bw to the "long-term" average delivery rate from those 2 intervals. |
| 571 | */ |
| 572 | static void bbr_lt_bw_sampling(struct sock *sk, const struct rate_sample *rs) |
| 573 | { |
| 574 | struct tcp_sock *tp = tcp_sk(sk); |
| 575 | struct bbr *bbr = inet_csk_ca(sk); |
| 576 | u32 lost, delivered; |
| 577 | u64 bw; |
Eric Dumazet | 9a568de | 2017-05-16 14:00:14 -0700 | [diff] [blame] | 578 | u32 t; |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 579 | |
| 580 | if (bbr->lt_use_bw) { /* already using long-term rate, lt_bw? */ |
| 581 | if (bbr->mode == BBR_PROBE_BW && bbr->round_start && |
| 582 | ++bbr->lt_rtt_cnt >= bbr_lt_bw_max_rtts) { |
| 583 | bbr_reset_lt_bw_sampling(sk); /* stop using lt_bw */ |
| 584 | bbr_reset_probe_bw_mode(sk); /* restart gain cycling */ |
| 585 | } |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | /* Wait for the first loss before sampling, to let the policer exhaust |
| 590 | * its tokens and estimate the steady-state rate allowed by the policer. |
| 591 | * Starting samples earlier includes bursts that over-estimate the bw. |
| 592 | */ |
| 593 | if (!bbr->lt_is_sampling) { |
| 594 | if (!rs->losses) |
| 595 | return; |
| 596 | bbr_reset_lt_bw_sampling_interval(sk); |
| 597 | bbr->lt_is_sampling = true; |
| 598 | } |
| 599 | |
| 600 | /* To avoid underestimates, reset sampling if we run out of data. */ |
| 601 | if (rs->is_app_limited) { |
| 602 | bbr_reset_lt_bw_sampling(sk); |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | if (bbr->round_start) |
| 607 | bbr->lt_rtt_cnt++; /* count round trips in this interval */ |
| 608 | if (bbr->lt_rtt_cnt < bbr_lt_intvl_min_rtts) |
| 609 | return; /* sampling interval needs to be longer */ |
| 610 | if (bbr->lt_rtt_cnt > 4 * bbr_lt_intvl_min_rtts) { |
| 611 | bbr_reset_lt_bw_sampling(sk); /* interval is too long */ |
| 612 | return; |
| 613 | } |
| 614 | |
| 615 | /* End sampling interval when a packet is lost, so we estimate the |
| 616 | * policer tokens were exhausted. Stopping the sampling before the |
| 617 | * tokens are exhausted under-estimates the policed rate. |
| 618 | */ |
| 619 | if (!rs->losses) |
| 620 | return; |
| 621 | |
| 622 | /* Calculate packets lost and delivered in sampling interval. */ |
| 623 | lost = tp->lost - bbr->lt_last_lost; |
| 624 | delivered = tp->delivered - bbr->lt_last_delivered; |
| 625 | /* Is loss rate (lost/delivered) >= lt_loss_thresh? If not, wait. */ |
| 626 | if (!delivered || (lost << BBR_SCALE) < bbr_lt_loss_thresh * delivered) |
| 627 | return; |
| 628 | |
| 629 | /* Find average delivery rate in this sampling interval. */ |
Eric Dumazet | 9a568de | 2017-05-16 14:00:14 -0700 | [diff] [blame] | 630 | t = div_u64(tp->delivered_mstamp, USEC_PER_MSEC) - bbr->lt_last_stamp; |
| 631 | if ((s32)t < 1) |
| 632 | return; /* interval is less than one ms, so wait */ |
| 633 | /* Check if can multiply without overflow */ |
| 634 | if (t >= ~0U / USEC_PER_MSEC) { |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 635 | bbr_reset_lt_bw_sampling(sk); /* interval too long; reset */ |
| 636 | return; |
| 637 | } |
Eric Dumazet | 9a568de | 2017-05-16 14:00:14 -0700 | [diff] [blame] | 638 | t *= USEC_PER_MSEC; |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 639 | bw = (u64)delivered * BW_UNIT; |
| 640 | do_div(bw, t); |
| 641 | bbr_lt_bw_interval_done(sk, bw); |
| 642 | } |
| 643 | |
| 644 | /* Estimate the bandwidth based on how fast packets are delivered */ |
| 645 | static void bbr_update_bw(struct sock *sk, const struct rate_sample *rs) |
| 646 | { |
| 647 | struct tcp_sock *tp = tcp_sk(sk); |
| 648 | struct bbr *bbr = inet_csk_ca(sk); |
| 649 | u64 bw; |
| 650 | |
| 651 | bbr->round_start = 0; |
| 652 | if (rs->delivered < 0 || rs->interval_us <= 0) |
| 653 | return; /* Not a valid observation */ |
| 654 | |
| 655 | /* See if we've reached the next RTT */ |
| 656 | if (!before(rs->prior_delivered, bbr->next_rtt_delivered)) { |
| 657 | bbr->next_rtt_delivered = tp->delivered; |
| 658 | bbr->rtt_cnt++; |
| 659 | bbr->round_start = 1; |
| 660 | bbr->packet_conservation = 0; |
| 661 | } |
| 662 | |
| 663 | bbr_lt_bw_sampling(sk, rs); |
| 664 | |
| 665 | /* Divide delivered by the interval to find a (lower bound) bottleneck |
| 666 | * bandwidth sample. Delivered is in packets and interval_us in uS and |
| 667 | * ratio will be <<1 for most connections. So delivered is first scaled. |
| 668 | */ |
| 669 | bw = (u64)rs->delivered * BW_UNIT; |
| 670 | do_div(bw, rs->interval_us); |
| 671 | |
| 672 | /* If this sample is application-limited, it is likely to have a very |
| 673 | * low delivered count that represents application behavior rather than |
| 674 | * the available network rate. Such a sample could drag down estimated |
| 675 | * bw, causing needless slow-down. Thus, to continue to send at the |
| 676 | * last measured network rate, we filter out app-limited samples unless |
| 677 | * they describe the path bw at least as well as our bw model. |
| 678 | * |
| 679 | * So the goal during app-limited phase is to proceed with the best |
| 680 | * network rate no matter how long. We automatically leave this |
| 681 | * phase when app writes faster than the network can deliver :) |
| 682 | */ |
| 683 | if (!rs->is_app_limited || bw >= bbr_max_bw(sk)) { |
| 684 | /* Incorporate new sample into our max bw filter. */ |
| 685 | minmax_running_max(&bbr->bw, bbr_bw_rtts, bbr->rtt_cnt, bw); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | /* Estimate when the pipe is full, using the change in delivery rate: BBR |
| 690 | * estimates that STARTUP filled the pipe if the estimated bw hasn't changed by |
| 691 | * at least bbr_full_bw_thresh (25%) after bbr_full_bw_cnt (3) non-app-limited |
| 692 | * rounds. Why 3 rounds: 1: rwin autotuning grows the rwin, 2: we fill the |
| 693 | * higher rwin, 3: we get higher delivery rate samples. Or transient |
| 694 | * cross-traffic or radio noise can go away. CUBIC Hystart shares a similar |
| 695 | * design goal, but uses delay and inter-ACK spacing instead of bandwidth. |
| 696 | */ |
| 697 | static void bbr_check_full_bw_reached(struct sock *sk, |
| 698 | const struct rate_sample *rs) |
| 699 | { |
| 700 | struct bbr *bbr = inet_csk_ca(sk); |
| 701 | u32 bw_thresh; |
| 702 | |
| 703 | if (bbr_full_bw_reached(sk) || !bbr->round_start || rs->is_app_limited) |
| 704 | return; |
| 705 | |
| 706 | bw_thresh = (u64)bbr->full_bw * bbr_full_bw_thresh >> BBR_SCALE; |
| 707 | if (bbr_max_bw(sk) >= bw_thresh) { |
| 708 | bbr->full_bw = bbr_max_bw(sk); |
| 709 | bbr->full_bw_cnt = 0; |
| 710 | return; |
| 711 | } |
| 712 | ++bbr->full_bw_cnt; |
| 713 | } |
| 714 | |
| 715 | /* If pipe is probably full, drain the queue and then enter steady-state. */ |
| 716 | static void bbr_check_drain(struct sock *sk, const struct rate_sample *rs) |
| 717 | { |
| 718 | struct bbr *bbr = inet_csk_ca(sk); |
| 719 | |
| 720 | if (bbr->mode == BBR_STARTUP && bbr_full_bw_reached(sk)) { |
| 721 | bbr->mode = BBR_DRAIN; /* drain queue we created */ |
| 722 | bbr->pacing_gain = bbr_drain_gain; /* pace slow to drain */ |
| 723 | bbr->cwnd_gain = bbr_high_gain; /* maintain cwnd */ |
| 724 | } /* fall through to check if in-flight is already small: */ |
| 725 | if (bbr->mode == BBR_DRAIN && |
| 726 | tcp_packets_in_flight(tcp_sk(sk)) <= |
| 727 | bbr_target_cwnd(sk, bbr_max_bw(sk), BBR_UNIT)) |
| 728 | bbr_reset_probe_bw_mode(sk); /* we estimate queue is drained */ |
| 729 | } |
| 730 | |
| 731 | /* The goal of PROBE_RTT mode is to have BBR flows cooperatively and |
| 732 | * periodically drain the bottleneck queue, to converge to measure the true |
| 733 | * min_rtt (unloaded propagation delay). This allows the flows to keep queues |
| 734 | * small (reducing queuing delay and packet loss) and achieve fairness among |
| 735 | * BBR flows. |
| 736 | * |
| 737 | * The min_rtt filter window is 10 seconds. When the min_rtt estimate expires, |
| 738 | * we enter PROBE_RTT mode and cap the cwnd at bbr_cwnd_min_target=4 packets. |
| 739 | * After at least bbr_probe_rtt_mode_ms=200ms and at least one packet-timed |
| 740 | * round trip elapsed with that flight size <= 4, we leave PROBE_RTT mode and |
| 741 | * re-enter the previous mode. BBR uses 200ms to approximately bound the |
| 742 | * performance penalty of PROBE_RTT's cwnd capping to roughly 2% (200ms/10s). |
| 743 | * |
| 744 | * Note that flows need only pay 2% if they are busy sending over the last 10 |
| 745 | * seconds. Interactive applications (e.g., Web, RPCs, video chunks) often have |
| 746 | * natural silences or low-rate periods within 10 seconds where the rate is low |
| 747 | * enough for long enough to drain its queue in the bottleneck. We pick up |
| 748 | * these min RTT measurements opportunistically with our min_rtt filter. :-) |
| 749 | */ |
| 750 | static void bbr_update_min_rtt(struct sock *sk, const struct rate_sample *rs) |
| 751 | { |
| 752 | struct tcp_sock *tp = tcp_sk(sk); |
| 753 | struct bbr *bbr = inet_csk_ca(sk); |
| 754 | bool filter_expired; |
| 755 | |
| 756 | /* Track min RTT seen in the min_rtt_win_sec filter window: */ |
Eric Dumazet | 2660bfa8 | 2017-05-16 14:00:05 -0700 | [diff] [blame] | 757 | filter_expired = after(tcp_jiffies32, |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 758 | bbr->min_rtt_stamp + bbr_min_rtt_win_sec * HZ); |
| 759 | if (rs->rtt_us >= 0 && |
| 760 | (rs->rtt_us <= bbr->min_rtt_us || filter_expired)) { |
| 761 | bbr->min_rtt_us = rs->rtt_us; |
Eric Dumazet | 2660bfa8 | 2017-05-16 14:00:05 -0700 | [diff] [blame] | 762 | bbr->min_rtt_stamp = tcp_jiffies32; |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | if (bbr_probe_rtt_mode_ms > 0 && filter_expired && |
| 766 | !bbr->idle_restart && bbr->mode != BBR_PROBE_RTT) { |
| 767 | bbr->mode = BBR_PROBE_RTT; /* dip, drain queue */ |
| 768 | bbr->pacing_gain = BBR_UNIT; |
| 769 | bbr->cwnd_gain = BBR_UNIT; |
| 770 | bbr_save_cwnd(sk); /* note cwnd so we can restore it */ |
| 771 | bbr->probe_rtt_done_stamp = 0; |
| 772 | } |
| 773 | |
| 774 | if (bbr->mode == BBR_PROBE_RTT) { |
| 775 | /* Ignore low rate samples during this mode. */ |
| 776 | tp->app_limited = |
| 777 | (tp->delivered + tcp_packets_in_flight(tp)) ? : 1; |
| 778 | /* Maintain min packets in flight for max(200 ms, 1 round). */ |
| 779 | if (!bbr->probe_rtt_done_stamp && |
| 780 | tcp_packets_in_flight(tp) <= bbr_cwnd_min_target) { |
Eric Dumazet | 2660bfa8 | 2017-05-16 14:00:05 -0700 | [diff] [blame] | 781 | bbr->probe_rtt_done_stamp = tcp_jiffies32 + |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 782 | msecs_to_jiffies(bbr_probe_rtt_mode_ms); |
| 783 | bbr->probe_rtt_round_done = 0; |
| 784 | bbr->next_rtt_delivered = tp->delivered; |
| 785 | } else if (bbr->probe_rtt_done_stamp) { |
| 786 | if (bbr->round_start) |
| 787 | bbr->probe_rtt_round_done = 1; |
| 788 | if (bbr->probe_rtt_round_done && |
Eric Dumazet | 2660bfa8 | 2017-05-16 14:00:05 -0700 | [diff] [blame] | 789 | after(tcp_jiffies32, bbr->probe_rtt_done_stamp)) { |
| 790 | bbr->min_rtt_stamp = tcp_jiffies32; |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 791 | bbr->restore_cwnd = 1; /* snap to prior_cwnd */ |
| 792 | bbr_reset_mode(sk); |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | bbr->idle_restart = 0; |
| 797 | } |
| 798 | |
| 799 | static void bbr_update_model(struct sock *sk, const struct rate_sample *rs) |
| 800 | { |
| 801 | bbr_update_bw(sk, rs); |
| 802 | bbr_update_cycle_phase(sk, rs); |
| 803 | bbr_check_full_bw_reached(sk, rs); |
| 804 | bbr_check_drain(sk, rs); |
| 805 | bbr_update_min_rtt(sk, rs); |
| 806 | } |
| 807 | |
| 808 | static void bbr_main(struct sock *sk, const struct rate_sample *rs) |
| 809 | { |
| 810 | struct bbr *bbr = inet_csk_ca(sk); |
| 811 | u32 bw; |
| 812 | |
| 813 | bbr_update_model(sk, rs); |
| 814 | |
| 815 | bw = bbr_bw(sk); |
| 816 | bbr_set_pacing_rate(sk, bw, bbr->pacing_gain); |
| 817 | bbr_set_tso_segs_goal(sk); |
| 818 | bbr_set_cwnd(sk, rs, rs->acked_sacked, bw, bbr->cwnd_gain); |
| 819 | } |
| 820 | |
| 821 | static void bbr_init(struct sock *sk) |
| 822 | { |
| 823 | struct tcp_sock *tp = tcp_sk(sk); |
| 824 | struct bbr *bbr = inet_csk_ca(sk); |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 825 | |
| 826 | bbr->prior_cwnd = 0; |
| 827 | bbr->tso_segs_goal = 0; /* default segs per skb until first ACK */ |
| 828 | bbr->rtt_cnt = 0; |
| 829 | bbr->next_rtt_delivered = 0; |
| 830 | bbr->prev_ca_state = TCP_CA_Open; |
| 831 | bbr->packet_conservation = 0; |
| 832 | |
| 833 | bbr->probe_rtt_done_stamp = 0; |
| 834 | bbr->probe_rtt_round_done = 0; |
| 835 | bbr->min_rtt_us = tcp_min_rtt(tp); |
Eric Dumazet | 2660bfa8 | 2017-05-16 14:00:05 -0700 | [diff] [blame] | 836 | bbr->min_rtt_stamp = tcp_jiffies32; |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 837 | |
| 838 | minmax_reset(&bbr->bw, bbr->rtt_cnt, 0); /* init max bw to 0 */ |
| 839 | |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 840 | sk->sk_pacing_rate = 0; /* force an update of sk_pacing_rate */ |
Neal Cardwell | 79135b8 | 2017-07-14 17:49:23 -0400 | [diff] [blame^] | 841 | bbr_init_pacing_rate_from_rtt(sk); |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 842 | |
| 843 | bbr->restore_cwnd = 0; |
| 844 | bbr->round_start = 0; |
| 845 | bbr->idle_restart = 0; |
| 846 | bbr->full_bw = 0; |
| 847 | bbr->full_bw_cnt = 0; |
Eric Dumazet | 9a568de | 2017-05-16 14:00:14 -0700 | [diff] [blame] | 848 | bbr->cycle_mstamp = 0; |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 849 | bbr->cycle_idx = 0; |
| 850 | bbr_reset_lt_bw_sampling(sk); |
| 851 | bbr_reset_startup_mode(sk); |
Eric Dumazet | 218af59 | 2017-05-16 04:24:36 -0700 | [diff] [blame] | 852 | |
| 853 | cmpxchg(&sk->sk_pacing_status, SK_PACING_NONE, SK_PACING_NEEDED); |
Neal Cardwell | 0f8782e | 2016-09-19 23:39:23 -0400 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | static u32 bbr_sndbuf_expand(struct sock *sk) |
| 857 | { |
| 858 | /* Provision 3 * cwnd since BBR may slow-start even during recovery. */ |
| 859 | return 3; |
| 860 | } |
| 861 | |
| 862 | /* In theory BBR does not need to undo the cwnd since it does not |
| 863 | * always reduce cwnd on losses (see bbr_main()). Keep it for now. |
| 864 | */ |
| 865 | static u32 bbr_undo_cwnd(struct sock *sk) |
| 866 | { |
| 867 | return tcp_sk(sk)->snd_cwnd; |
| 868 | } |
| 869 | |
| 870 | /* Entering loss recovery, so save cwnd for when we exit or undo recovery. */ |
| 871 | static u32 bbr_ssthresh(struct sock *sk) |
| 872 | { |
| 873 | bbr_save_cwnd(sk); |
| 874 | return TCP_INFINITE_SSTHRESH; /* BBR does not use ssthresh */ |
| 875 | } |
| 876 | |
| 877 | static size_t bbr_get_info(struct sock *sk, u32 ext, int *attr, |
| 878 | union tcp_cc_info *info) |
| 879 | { |
| 880 | if (ext & (1 << (INET_DIAG_BBRINFO - 1)) || |
| 881 | ext & (1 << (INET_DIAG_VEGASINFO - 1))) { |
| 882 | struct tcp_sock *tp = tcp_sk(sk); |
| 883 | struct bbr *bbr = inet_csk_ca(sk); |
| 884 | u64 bw = bbr_bw(sk); |
| 885 | |
| 886 | bw = bw * tp->mss_cache * USEC_PER_SEC >> BW_SCALE; |
| 887 | memset(&info->bbr, 0, sizeof(info->bbr)); |
| 888 | info->bbr.bbr_bw_lo = (u32)bw; |
| 889 | info->bbr.bbr_bw_hi = (u32)(bw >> 32); |
| 890 | info->bbr.bbr_min_rtt = bbr->min_rtt_us; |
| 891 | info->bbr.bbr_pacing_gain = bbr->pacing_gain; |
| 892 | info->bbr.bbr_cwnd_gain = bbr->cwnd_gain; |
| 893 | *attr = INET_DIAG_BBRINFO; |
| 894 | return sizeof(info->bbr); |
| 895 | } |
| 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | static void bbr_set_state(struct sock *sk, u8 new_state) |
| 900 | { |
| 901 | struct bbr *bbr = inet_csk_ca(sk); |
| 902 | |
| 903 | if (new_state == TCP_CA_Loss) { |
| 904 | struct rate_sample rs = { .losses = 1 }; |
| 905 | |
| 906 | bbr->prev_ca_state = TCP_CA_Loss; |
| 907 | bbr->full_bw = 0; |
| 908 | bbr->round_start = 1; /* treat RTO like end of a round */ |
| 909 | bbr_lt_bw_sampling(sk, &rs); |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | static struct tcp_congestion_ops tcp_bbr_cong_ops __read_mostly = { |
| 914 | .flags = TCP_CONG_NON_RESTRICTED, |
| 915 | .name = "bbr", |
| 916 | .owner = THIS_MODULE, |
| 917 | .init = bbr_init, |
| 918 | .cong_control = bbr_main, |
| 919 | .sndbuf_expand = bbr_sndbuf_expand, |
| 920 | .undo_cwnd = bbr_undo_cwnd, |
| 921 | .cwnd_event = bbr_cwnd_event, |
| 922 | .ssthresh = bbr_ssthresh, |
| 923 | .tso_segs_goal = bbr_tso_segs_goal, |
| 924 | .get_info = bbr_get_info, |
| 925 | .set_state = bbr_set_state, |
| 926 | }; |
| 927 | |
| 928 | static int __init bbr_register(void) |
| 929 | { |
| 930 | BUILD_BUG_ON(sizeof(struct bbr) > ICSK_CA_PRIV_SIZE); |
| 931 | return tcp_register_congestion_control(&tcp_bbr_cong_ops); |
| 932 | } |
| 933 | |
| 934 | static void __exit bbr_unregister(void) |
| 935 | { |
| 936 | tcp_unregister_congestion_control(&tcp_bbr_cong_ops); |
| 937 | } |
| 938 | |
| 939 | module_init(bbr_register); |
| 940 | module_exit(bbr_unregister); |
| 941 | |
| 942 | MODULE_AUTHOR("Van Jacobson <vanj@google.com>"); |
| 943 | MODULE_AUTHOR("Neal Cardwell <ncardwell@google.com>"); |
| 944 | MODULE_AUTHOR("Yuchung Cheng <ycheng@google.com>"); |
| 945 | MODULE_AUTHOR("Soheil Hassas Yeganeh <soheil@google.com>"); |
| 946 | MODULE_LICENSE("Dual BSD/GPL"); |
| 947 | MODULE_DESCRIPTION("TCP BBR (Bottleneck Bandwidth and RTT)"); |