tcp_bbr.c 42 KB

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