tcp_bbr.c 42 KB

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