tcp_tsunami.c 32 KB

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