tcp_bbr.c 39 KB

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