bbrv3.patch 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604
  1. diff --git a/include/net/tcp.h b/include/net/tcp.h
  2. index afe1ca2cc..8eebfafdd 100644
  3. --- a/include/net/tcp.h
  4. +++ b/include/net/tcp.h
  5. @@ -1112,6 +1112,12 @@ struct tcp_congestion_ops {
  6. /* override sysctl_tcp_min_tso_segs */
  7. u32 (*min_tso_segs)(struct sock *sk);
  8. + /* call when skb is marked lost (optional) */
  9. + void (*skb_marked_lost)(struct sock *sk, const struct sk_buff *skb);
  10. +
  11. + /* set TSO segment count (optional, for new congestion controls) */
  12. + u32 (*tso_segs)(struct sock *sk, unsigned int mss_now);
  13. +
  14. /* call when packets are delivered to update cwnd and pacing rate,
  15. * after all the ca_state processing. (optional)
  16. */
  17. diff --git a/include/uapi/linux/inet_diag.h b/include/uapi/linux/inet_diag.h
  18. index 50655de04..8d69bd9b7 100644
  19. --- a/include/uapi/linux/inet_diag.h
  20. +++ b/include/uapi/linux/inet_diag.h
  21. @@ -223,12 +223,37 @@ struct tcp_dctcp_info {
  22. /* INET_DIAG_BBRINFO */
  23. struct tcp_bbr_info {
  24. + /* Original 6 fields - unchanged offsets for ABI backward compatibility */
  25. /* u64 bw: max-filtered BW (app throughput) estimate in Byte per sec: */
  26. __u32 bbr_bw_lo; /* lower 32 bits of bw */
  27. __u32 bbr_bw_hi; /* upper 32 bits of bw */
  28. __u32 bbr_min_rtt; /* min-filtered RTT in uSec */
  29. __u32 bbr_pacing_gain; /* pacing gain shifted left 8 bits */
  30. __u32 bbr_cwnd_gain; /* cwnd gain shifted left 8 bits */
  31. + /* BBRv3+ new fields - appended for extended information */
  32. + __u32 bbr_bw_hi_lsb; /* lower 32 bits of bw_hi */
  33. + __u32 bbr_bw_hi_msb; /* upper 32 bits of bw_hi */
  34. + __u32 bbr_bw_lo_lsb; /* lower 32 bits of bw_lo */
  35. + __u32 bbr_bw_lo_msb; /* upper 32 bits of bw_lo */
  36. + __u8 bbr_mode; /* current bbr_mode in state machine */
  37. + __u8 bbr_phase; /* current state machine phase */
  38. + __u8 unused1; /* alignment padding; not used yet */
  39. + __u8 bbr_version; /* BBR algorithm version */
  40. + __u32 bbr_inflight_lo; /* lower short-term data volume bound */
  41. + __u32 bbr_inflight_hi; /* higher long-term data volume bound */
  42. + __u32 bbr_extra_acked; /* max excess packets ACKed in epoch */
  43. +};
  44. +
  45. +/* TCP BBR congestion control bbr_phase as reported in netlink/ss stats */
  46. +enum tcp_bbr_phase {
  47. + BBR_PHASE_INVALID = 0,
  48. + BBR_PHASE_STARTUP = 1,
  49. + BBR_PHASE_DRAIN = 2,
  50. + BBR_PHASE_PROBE_RTT = 3,
  51. + BBR_PHASE_PROBE_BW_UP = 4,
  52. + BBR_PHASE_PROBE_BW_DOWN = 5,
  53. + BBR_PHASE_PROBE_BW_CRUISE = 6,
  54. + BBR_PHASE_PROBE_BW_REFILL = 7,
  55. };
  56. union tcp_cc_info {
  57. diff --git a/net/ipv4/Kconfig b/net/ipv4/Kconfig
  58. index 2dfb12230..afc41f6f7 100644
  59. --- a/net/ipv4/Kconfig
  60. +++ b/net/ipv4/Kconfig
  61. @@ -678,6 +678,27 @@ config TCP_CONG_BBR
  62. AQM schemes that do not provide a delay signal. It requires the fq
  63. ("Fair Queue") pacing packet scheduler.
  64. +config TCP_CONG_BBRV3
  65. + bool "BBRv3 TCP"
  66. + default y
  67. + depends on TCP_CONG_BBR || TCP_CONG_BBR=y
  68. + help
  69. + BBRv3 (Bottleneck Bandwidth and RTT version 3) TCP congestion
  70. + control. This is an enhanced version of BBR with improved
  71. + fairness, ECN support, and loss handling.
  72. +
  73. + BBRv3 builds on BBRv1 with additional features:
  74. + - Rate probing with bounded risk of loss
  75. + - ECN-aware congestion avoidance
  76. + - Improved fairness with Reno/CUBIC flows
  77. + - Loss detection and recovery improvements
  78. +
  79. + To use this module, you must also have:
  80. + - FQ pacing scheduler (CONFIG_NET_SCH_FQ)
  81. + - Optionally: ECN support (CONFIG_TCP_ECN)
  82. +
  83. + If unsure, say Y.
  84. +
  85. choice
  86. prompt "Default TCP congestion control"
  87. default DEFAULT_CUBIC
  88. @@ -715,6 +736,9 @@ choice
  89. config DEFAULT_BBR
  90. bool "BBR" if TCP_CONG_BBR=y
  91. + config DEFAULT_BBRV3
  92. + bool "BBRv3" if TCP_CONG_BBRV3=y
  93. +
  94. config DEFAULT_RENO
  95. bool "Reno"
  96. endchoice
  97. @@ -739,6 +763,7 @@ config DEFAULT_TCP_CONG
  98. default "dctcp" if DEFAULT_DCTCP
  99. default "cdg" if DEFAULT_CDG
  100. default "bbr" if DEFAULT_BBR
  101. + default "bbrv3" if DEFAULT_BBRV3
  102. default "cubic"
  103. config TCP_MD5SIG
  104. diff --git a/net/ipv4/Makefile b/net/ipv4/Makefile
  105. index bbdd9c44f..bd5d416a4 100644
  106. --- a/net/ipv4/Makefile
  107. +++ b/net/ipv4/Makefile
  108. @@ -46,6 +46,7 @@ obj-$(CONFIG_INET_TCP_DIAG) += tcp_diag.o
  109. obj-$(CONFIG_INET_UDP_DIAG) += udp_diag.o
  110. obj-$(CONFIG_INET_RAW_DIAG) += raw_diag.o
  111. obj-$(CONFIG_TCP_CONG_BBR) += tcp_bbr.o
  112. +obj-$(CONFIG_TCP_CONG_BBRV3) += tcp_bbrv3.o
  113. obj-$(CONFIG_TCP_CONG_BIC) += tcp_bic.o
  114. obj-$(CONFIG_TCP_CONG_CDG) += tcp_cdg.o
  115. obj-$(CONFIG_TCP_CONG_CUBIC) += tcp_cubic.o
  116. diff --git a/net/ipv4/tcp_dctcp.h b/net/ipv4/tcp_dctcp.h
  117. index d69a77cbd..b99744ea9 100644
  118. --- a/net/ipv4/tcp_dctcp.h
  119. +++ b/net/ipv4/tcp_dctcp.h
  120. @@ -1,6 +1,14 @@
  121. +/* SPDX-License-Identifier: GPL-2.0 */
  122. #ifndef _TCP_DCTCP_H
  123. #define _TCP_DCTCP_H
  124. +/*
  125. + * DCTCP ECE ACK helper functions for BBRv3
  126. + *
  127. + * These functions implement the minimal DCTP CE state machine needed
  128. + * by BBRv3 for ECN-based congestion avoidance.
  129. + */
  130. +
  131. static inline void dctcp_ece_ack_cwr(struct sock *sk, u32 ce_state)
  132. {
  133. struct tcp_sock *tp = tcp_sk(sk);
  134. @@ -37,4 +45,41 @@ static inline void dctcp_ece_ack_update(struct sock *sk, enum tcp_ca_event evt,
  135. dctcp_ece_ack_cwr(sk, new_ce_state);
  136. }
  137. -#endif
  138. +/*
  139. + * State for PLB (Protective Load Balancing) for a single TCP connection.
  140. + * PLB is an optional feature for datacenter load balancing.
  141. + */
  142. +struct tcp_plb_state {
  143. + u8 consec_cong_rounds:5, /* consecutive congested rounds */
  144. + pause_until_set:1; /* pause_until is valid */
  145. + u32 pause_until; /* don't rehash until this jiffie */
  146. +};
  147. +
  148. +static inline void tcp_plb_init(const struct sock *sk,
  149. + struct tcp_plb_state *plb)
  150. +{
  151. + memset(plb, 0, sizeof(*plb));
  152. +}
  153. +
  154. +static inline void tcp_plb_update_state(const struct sock *sk,
  155. + struct tcp_plb_state *plb,
  156. + const int cong_ratio)
  157. +{
  158. + /* PLB is optional - stub implementation for kernels without
  159. + * the full PLB infrastructure (sysctl_tcp_plb_enabled, etc.)
  160. + */
  161. +}
  162. +
  163. +static inline void tcp_plb_check_rehash(struct sock *sk,
  164. + struct tcp_plb_state *plb)
  165. +{
  166. + /* Stub - PLB rehash not available without full infrastructure */
  167. +}
  168. +
  169. +static inline void tcp_plb_update_state_upon_rto(struct sock *sk,
  170. + struct tcp_plb_state *plb)
  171. +{
  172. + /* Stub - RTO state update not available without full infrastructure */
  173. +}
  174. +
  175. +#endif /* _TCP_DCTCP_H */
  176. diff --git a/net/ipv4/tcp_bbrv3.c b/net/ipv4/tcp_bbrv3.c
  177. new file mode 100644
  178. index 000000000..b7e8825e1
  179. --- /dev/null
  180. +++ b/net/ipv4/tcp_bbrv3.c
  181. @@ -0,0 +1,2415 @@
  182. +/* BBR (Bottleneck Bandwidth and RTT) congestion control
  183. + *
  184. + * BBR is a model-based congestion control algorithm that aims for low queues,
  185. + * low loss, and (bounded) Reno/CUBIC coexistence. To maintain a model of the
  186. + * network path, it uses measurements of bandwidth and RTT, as well as (if they
  187. + * occur) packet loss and/or shallow-threshold ECN signals. Note that although
  188. + * it can use ECN or loss signals explicitly, it does not require either; it
  189. + * can bound its in-flight data based on its estimate of the BDP.
  190. + *
  191. + * The model has both higher and lower bounds for the operating range:
  192. + * lo: bw_lo, inflight_lo: conservative short-term lower bound
  193. + * hi: bw_hi, inflight_hi: robust long-term upper bound
  194. + * The bandwidth-probing time scale is (a) extended dynamically based on
  195. + * estimated BDP to improve coexistence with Reno/CUBIC; (b) bounded by
  196. + * an interactive wall-clock time-scale to be more scalable and responsive
  197. + * than Reno and CUBIC.
  198. + *
  199. + * Here is a state transition diagram for BBR:
  200. + *
  201. + * |
  202. + * V
  203. + * +---> STARTUP ----+
  204. + * | | |
  205. + * | V |
  206. + * | DRAIN ----+
  207. + * | | |
  208. + * | V |
  209. + * +---> PROBE_BW ----+
  210. + * | ^ | |
  211. + * | | | |
  212. + * | +----+ |
  213. + * | |
  214. + * +---- PROBE_RTT <--+
  215. + *
  216. + * A BBR flow starts in STARTUP, and ramps up its sending rate quickly.
  217. + * When it estimates the pipe is full, it enters DRAIN to drain the queue.
  218. + * In steady state a BBR flow only uses PROBE_BW and PROBE_RTT.
  219. + * A long-lived BBR flow spends the vast majority of its time remaining
  220. + * (repeatedly) in PROBE_BW, fully probing and utilizing the pipe's bandwidth
  221. + * in a fair manner, with a small, bounded queue. *If* a flow has been
  222. + * continuously sending for the entire min_rtt window, and hasn't seen an RTT
  223. + * sample that matches or decreases its min_rtt estimate for 10 seconds, then
  224. + * it briefly enters PROBE_RTT to cut inflight to a minimum value to re-probe
  225. + * the path's two-way propagation delay (min_rtt). When exiting PROBE_RTT, if
  226. + * we estimated that we reached the full bw of the pipe then we enter PROBE_BW;
  227. + * otherwise we enter STARTUP to try to fill the pipe.
  228. + *
  229. + * BBR is described in detail in:
  230. + * "BBR: Congestion-Based Congestion Control",
  231. + * Neal Cardwell, Yuchung Cheng, C. Stephen Gunn, Soheil Hassas Yeganeh,
  232. + * Van Jacobson. ACM Queue, Vol. 14 No. 5, September-October 2016.
  233. + *
  234. + * There is a public e-mail list for discussing BBR development and testing:
  235. + * https://groups.google.com/forum/#!forum/bbr-dev
  236. + *
  237. + * NOTE: BBR might be used with the fq qdisc ("man tc-fq") with pacing enabled,
  238. + * otherwise TCP stack falls back to an internal pacing using one high
  239. + * resolution timer per TCP socket and may use more resources.
  240. + */
  241. +#include <linux/btf.h>
  242. +#include <linux/btf_ids.h>
  243. +#include <linux/module.h>
  244. +#include <net/tcp.h>
  245. +#include <linux/inet_diag.h>
  246. +#include <linux/inet.h>
  247. +#include <linux/random.h>
  248. +#include <linux/win_minmax.h>
  249. +
  250. +#include <trace/events/tcp.h>
  251. +#include "tcp_dctcp.h"
  252. +
  253. +/* BTF kfunc compatibility macros for kernels that don't have the newer BTF API */
  254. +#ifndef BTF_KFUNCS_START
  255. +#define BTF_KFUNCS_START(name) BTF_SET8_START(name)
  256. +#define BTF_KFUNCS_END(name) BTF_SET8_END(name)
  257. +#define __bpf_kfunc static
  258. +#endif
  259. +
  260. +#define BBR_VERSION 3
  261. +
  262. +#define bbr_param(sk,name) (bbr_ ## name)
  263. +
  264. +/* Scale factor for rate in pkt/uSec unit to avoid truncation in bandwidth
  265. + * estimation. The rate unit ~= (1500 bytes / 1 usec / 2^24) ~= 715 bps.
  266. + * This handles bandwidths from 0.06pps (715bps) to 256Mpps (3Tbps) in a u32.
  267. + * Since the minimum window is >=4 packets, the lower bound isn't
  268. + * an issue. The upper bound isn't an issue with existing technologies.
  269. + */
  270. +#define BW_SCALE 24
  271. +#define BW_UNIT (1 << BW_SCALE)
  272. +
  273. +#define BBR_SCALE 8 /* scaling factor for fractions in BBR (e.g. gains) */
  274. +#define BBR_UNIT (1 << BBR_SCALE)
  275. +
  276. +/* BBR has the following modes for deciding how fast to send: */
  277. +enum bbr_mode {
  278. + BBR_STARTUP, /* ramp up sending rate rapidly to fill pipe */
  279. + BBR_DRAIN, /* drain any queue created during startup */
  280. + BBR_PROBE_BW, /* discover, share bw: pace around estimated bw */
  281. + BBR_PROBE_RTT, /* cut inflight to min to probe min_rtt */
  282. +};
  283. +
  284. +/* How does the incoming ACK stream relate to our bandwidth probing? */
  285. +enum bbr_ack_phase {
  286. + BBR_ACKS_INIT, /* not probing; not getting probe feedback */
  287. + BBR_ACKS_REFILLING, /* sending at est. bw to fill pipe */
  288. + BBR_ACKS_PROBE_STARTING, /* inflight rising to probe bw */
  289. + BBR_ACKS_PROBE_FEEDBACK, /* getting feedback from bw probing */
  290. + BBR_ACKS_PROBE_STOPPING, /* stopped probing; still getting feedback */
  291. +};
  292. +
  293. +/* BBR congestion control block */
  294. +struct bbr {
  295. + u32 min_rtt_us; /* min RTT in min_rtt_win_sec window */
  296. + u32 min_rtt_stamp; /* timestamp of min_rtt_us */
  297. + u32 probe_rtt_done_stamp; /* end time for BBR_PROBE_RTT mode */
  298. + u32 probe_rtt_min_us; /* min RTT in probe_rtt_win_ms win */
  299. + u32 probe_rtt_min_stamp; /* timestamp of probe_rtt_min_us*/
  300. + u32 next_rtt_delivered; /* scb->tx.delivered at end of round */
  301. + u64 cycle_mstamp; /* time of this cycle phase start */
  302. + u32 mode:2, /* current bbr_mode in state machine */
  303. + prev_ca_state:3, /* CA state on previous ACK */
  304. + round_start:1, /* start of packet-timed tx->ack round? */
  305. + ce_state:1, /* If most recent data has CE bit set */
  306. + bw_probe_up_rounds:5, /* cwnd-limited rounds in PROBE_UP */
  307. + try_fast_path:1, /* can we take fast path? */
  308. + idle_restart:1, /* restarting after idle? */
  309. + probe_rtt_round_done:1, /* a BBR_PROBE_RTT round at 4 pkts? */
  310. + init_cwnd:7, /* initial cwnd */
  311. + unused_1:10;
  312. + u32 pacing_gain:10, /* current gain for setting pacing rate */
  313. + cwnd_gain:10, /* current gain for setting cwnd */
  314. + full_bw_reached:1, /* reached full bw in Startup? */
  315. + full_bw_cnt:2, /* number of rounds without large bw gains */
  316. + cycle_idx:2, /* current index in pacing_gain cycle array */
  317. + has_seen_rtt:1, /* have we seen an RTT sample yet? */
  318. + unused_2:6;
  319. + u32 prior_cwnd; /* prior cwnd upon entering loss recovery */
  320. + u32 full_bw; /* recent bw, to estimate if pipe is full */
  321. +
  322. + /* For tracking ACK aggregation: */
  323. + u64 ack_epoch_mstamp; /* start of ACK sampling epoch */
  324. + u16 extra_acked[2]; /* max excess data ACKed in epoch */
  325. + u32 ack_epoch_acked:20, /* packets (S)ACKed in sampling epoch */
  326. + extra_acked_win_rtts:5, /* age of extra_acked, in round trips */
  327. + extra_acked_win_idx:1, /* current index in extra_acked array */
  328. + /* BBR v3 state: */
  329. + full_bw_now:1, /* recently reached full bw plateau? */
  330. + startup_ecn_rounds:2, /* consecutive hi ECN STARTUP rounds */
  331. + loss_in_cycle:1, /* packet loss in this cycle? */
  332. + ecn_in_cycle:1, /* ECN in this cycle? */
  333. + unused_3:1;
  334. + u32 loss_round_delivered; /* scb->tx.delivered ending loss round */
  335. + u32 undo_bw_lo; /* bw_lo before latest losses */
  336. + u32 undo_inflight_lo; /* inflight_lo before latest losses */
  337. + u32 undo_inflight_hi; /* inflight_hi before latest losses */
  338. + u32 bw_latest; /* max delivered bw in last round trip */
  339. + u32 bw_lo; /* lower bound on sending bandwidth */
  340. + u32 bw_hi[2]; /* max recent measured bw sample */
  341. + u32 inflight_latest; /* max delivered data in last round trip */
  342. + u32 inflight_lo; /* lower bound of inflight data range */
  343. + u32 inflight_hi; /* upper bound of inflight data range */
  344. + u32 bw_probe_up_cnt; /* packets delivered per inflight_hi incr */
  345. + u32 bw_probe_up_acks; /* packets (S)ACKed since inflight_hi incr */
  346. + u32 probe_wait_us; /* PROBE_DOWN until next clock-driven probe */
  347. + u32 prior_rcv_nxt; /* tp->rcv_nxt when CE state last changed */
  348. + u32 ecn_eligible:1, /* sender can use ECN (RTT, handshake)? */
  349. + ecn_alpha:9, /* EWMA delivered_ce/delivered; 0..256 */
  350. + bw_probe_samples:1, /* rate samples reflect bw probing? */
  351. + prev_probe_too_high:1, /* did last PROBE_UP go too high? */
  352. + stopped_risky_probe:1, /* last PROBE_UP stopped due to risk? */
  353. + rounds_since_probe:8, /* packet-timed rounds since probed bw */
  354. + loss_round_start:1, /* loss_round_delivered round trip? */
  355. + loss_in_round:1, /* loss marked in this round trip? */
  356. + ecn_in_round:1, /* ECN marked in this round trip? */
  357. + ack_phase:3, /* bbr_ack_phase: meaning of ACKs */
  358. + loss_events_in_round:4,/* losses in STARTUP round */
  359. + initialized:1; /* has bbr_init() been called? */
  360. + u32 alpha_last_delivered; /* tp->delivered at alpha update */
  361. + u32 alpha_last_delivered_ce; /* tp->delivered_ce at alpha update */
  362. +
  363. + u8 unused_4; /* to preserve alignment */
  364. + struct tcp_plb_state plb;
  365. +};
  366. +
  367. +struct bbr_context {
  368. + u32 sample_bw;
  369. +};
  370. +
  371. +/* Window length of min_rtt filter (in sec): */
  372. +static const u32 bbr_min_rtt_win_sec = 10;
  373. +/* Minimum time (in ms) spent at bbr_cwnd_min_target in BBR_PROBE_RTT mode: */
  374. +static const u32 bbr_probe_rtt_mode_ms = 200;
  375. +/* Window length of probe_rtt_min_us filter (in ms), and consequently the
  376. + * typical interval between PROBE_RTT mode entries. The default is 5000ms.
  377. + * Note that bbr_probe_rtt_win_ms must be <= bbr_min_rtt_win_sec * MSEC_PER_SEC
  378. + */
  379. +static const u32 bbr_probe_rtt_win_ms = 5000;
  380. +/* Proportion of cwnd to estimated BDP in PROBE_RTT, in units of BBR_UNIT: */
  381. +static const u32 bbr_probe_rtt_cwnd_gain = BBR_UNIT * 1 / 2;
  382. +
  383. +/* Use min_rtt to help adapt TSO burst size, with smaller min_rtt resulting
  384. + * in bigger TSO bursts. We cut the RTT-based allowance in half
  385. + * for every 2^9 usec (aka 512 us) of RTT, so that the RTT-based allowance
  386. + * is below 1500 bytes after 6 * ~500 usec = 3ms.
  387. + */
  388. +static const u32 bbr_tso_rtt_shift = 9;
  389. +
  390. +/* Pace at ~1% below estimated bw, on average, to reduce queue at bottleneck.
  391. + * In order to help drive the network toward lower queues and low latency while
  392. + * maintaining high utilization, the average pacing rate aims to be slightly
  393. + * lower than the estimated bandwidth. This is an important aspect of the
  394. + * design.
  395. + */
  396. +static const int bbr_pacing_margin_percent = 1;
  397. +
  398. +/* We use a startup_pacing_gain of 4*ln(2) because it's the smallest value
  399. + * that will allow a smoothly increasing pacing rate that will double each RTT
  400. + * and send the same number of packets per RTT that an un-paced, slow-starting
  401. + * Reno or CUBIC flow would:
  402. + */
  403. +static const int bbr_startup_pacing_gain = BBR_UNIT * 277 / 100 + 1;
  404. +/* The gain for deriving startup cwnd: */
  405. +static const int bbr_startup_cwnd_gain = BBR_UNIT * 2;
  406. +/* The pacing gain in BBR_DRAIN is calculated to typically drain
  407. + * the queue created in BBR_STARTUP in a single round:
  408. + */
  409. +static const int bbr_drain_gain = BBR_UNIT * 1000 / 2885;
  410. +/* The gain for deriving steady-state cwnd tolerates delayed/stretched ACKs: */
  411. +static const int bbr_cwnd_gain = BBR_UNIT * 2;
  412. +/* The pacing_gain values for the PROBE_BW gain cycle, to discover/share bw: */
  413. +static const int bbr_pacing_gain[] = {
  414. + BBR_UNIT * 5 / 4, /* UP: probe for more available bw */
  415. + BBR_UNIT * 91 / 100, /* DOWN: drain queue and/or yield bw */
  416. + BBR_UNIT, /* CRUISE: try to use pipe w/ some headroom */
  417. + BBR_UNIT, /* REFILL: refill pipe to estimated 100% */
  418. +};
  419. +enum bbr_pacing_gain_phase {
  420. + BBR_BW_PROBE_UP = 0, /* push up inflight to probe for bw/vol */
  421. + BBR_BW_PROBE_DOWN = 1, /* drain excess inflight from the queue */
  422. + BBR_BW_PROBE_CRUISE = 2, /* use pipe, w/ headroom in queue/pipe */
  423. + BBR_BW_PROBE_REFILL = 3, /* refill the pipe again to 100% */
  424. +};
  425. +
  426. +/* Try to keep at least this many packets in flight, if things go smoothly. For
  427. + * smooth functioning, a sliding window protocol ACKing every other packet
  428. + * needs at least 4 packets in flight:
  429. + */
  430. +static const u32 bbr_cwnd_min_target = 4;
  431. +
  432. +/* To estimate if BBR_STARTUP or BBR_BW_PROBE_UP has filled pipe... */
  433. +/* If bw has increased significantly (1.25x), there may be more bw available: */
  434. +static const u32 bbr_full_bw_thresh = BBR_UNIT * 5 / 4;
  435. +/* But after 3 rounds w/o significant bw growth, estimate pipe is full: */
  436. +static const u32 bbr_full_bw_cnt = 3;
  437. +
  438. +/* Gain factor for adding extra_acked to target cwnd: */
  439. +static const int bbr_extra_acked_gain = BBR_UNIT;
  440. +/* Window length of extra_acked window. */
  441. +static const u32 bbr_extra_acked_win_rtts = 5;
  442. +/* Max allowed val for ack_epoch_acked, after which sampling epoch is reset */
  443. +static const u32 bbr_ack_epoch_acked_reset_thresh = 1U << 20;
  444. +/* Time period for clamping cwnd increment due to ack aggregation */
  445. +static const u32 bbr_extra_acked_max_us = 100 * 1000;
  446. +
  447. +/* Flags to control BBR ECN-related behavior... */
  448. +
  449. +/* Ensure ACKs only ACK packets with consistent ECN CE status? */
  450. +static const bool bbr_precise_ece_ack = true;
  451. +
  452. +/* Max RTT (in usec) at which to use sender-side ECN logic.
  453. + * Disabled when 0 (ECN allowed at any RTT).
  454. + */
  455. +static const u32 bbr_ecn_max_rtt_us = 5000;
  456. +
  457. +/* On losses, scale down inflight and pacing rate by beta scaled by BBR_SCALE.
  458. + * No loss response when 0.
  459. + */
  460. +static const u32 bbr_beta = BBR_UNIT * 30 / 100;
  461. +
  462. +/* Gain factor for ECN mark ratio samples, scaled by BBR_SCALE (1/16 = 6.25%) */
  463. +static const u32 bbr_ecn_alpha_gain = BBR_UNIT * 1 / 16;
  464. +
  465. +/* The initial value for ecn_alpha; 1.0 allows a flow to respond quickly
  466. + * to congestion if the bottleneck is congested when the flow starts up.
  467. + */
  468. +static const u32 bbr_ecn_alpha_init = BBR_UNIT;
  469. +
  470. +/* On ECN, cut inflight_lo to (1 - ecn_factor * ecn_alpha) scaled by BBR_SCALE.
  471. + * No ECN based bounding when 0.
  472. + */
  473. +static const u32 bbr_ecn_factor = BBR_UNIT * 1 / 3; /* 1/3 = 33% */
  474. +
  475. +/* Estimate bw probing has gone too far if CE ratio exceeds this threshold.
  476. + * Scaled by BBR_SCALE. Disabled when 0.
  477. + */
  478. +static const u32 bbr_ecn_thresh = BBR_UNIT * 1 / 2; /* 1/2 = 50% */
  479. +
  480. +/* If non-zero, if in a cycle with no losses but some ECN marks, after ECN
  481. + * clears then make the first round's increment to inflight_hi the following
  482. + * fraction of inflight_hi.
  483. + */
  484. +static const u32 bbr_ecn_reprobe_gain = BBR_UNIT * 1 / 2;
  485. +
  486. +/* Estimate bw probing has gone too far if loss rate exceeds this level. */
  487. +static const u32 bbr_loss_thresh = BBR_UNIT * 2 / 100; /* 2% loss */
  488. +
  489. +/* Slow down for a packet loss recovered by TLP? */
  490. +static const bool bbr_loss_probe_recovery = true;
  491. +
  492. +/* Exit STARTUP if number of loss marking events in a Recovery round is >= N,
  493. + * and loss rate is higher than bbr_loss_thresh.
  494. + * Disabled if 0.
  495. + */
  496. +static const u32 bbr_full_loss_cnt = 6;
  497. +
  498. +/* Exit STARTUP if number of round trips with ECN mark rate above ecn_thresh
  499. + * meets this count.
  500. + */
  501. +static const u32 bbr_full_ecn_cnt = 2;
  502. +
  503. +/* Fraction of unutilized headroom to try to leave in path upon high loss. */
  504. +static const u32 bbr_inflight_headroom = BBR_UNIT * 15 / 100;
  505. +
  506. +/* How much do we increase cwnd_gain when probing for bandwidth in
  507. + * BBR_BW_PROBE_UP? This specifies the increment in units of
  508. + * BBR_UNIT/4. The default is 1, meaning 0.25.
  509. + * The min value is 0 (meaning 0.0); max is 3 (meaning 0.75).
  510. + */
  511. +static const u32 bbr_bw_probe_cwnd_gain = 1;
  512. +
  513. +/* Max number of packet-timed rounds to wait before probing for bandwidth. If
  514. + * we want to tolerate 1% random loss per round, and not have this cut our
  515. + * inflight too much, we must probe for bw periodically on roughly this scale.
  516. + * If low, limits Reno/CUBIC coexistence; if high, limits loss tolerance.
  517. + * We aim to be fair with Reno/CUBIC up to a BDP of at least:
  518. + * BDP = 25Mbps * .030sec /(1514bytes) = 61.9 packets
  519. + */
  520. +static const u32 bbr_bw_probe_max_rounds = 63;
  521. +
  522. +/* Max amount of randomness to inject in round counting for Reno-coexistence.
  523. + */
  524. +static const u32 bbr_bw_probe_rand_rounds = 2;
  525. +
  526. +/* Use BBR-native probe time scale starting at this many usec.
  527. + * We aim to be fair with Reno/CUBIC up to an inter-loss time epoch of at least:
  528. + * BDP*RTT = 25Mbps * .030sec /(1514bytes) * 0.030sec = 1.9 secs
  529. + */
  530. +static const u32 bbr_bw_probe_base_us = 2 * USEC_PER_SEC; /* 2 secs */
  531. +
  532. +/* Use BBR-native probes spread over this many usec: */
  533. +static const u32 bbr_bw_probe_rand_us = 1 * USEC_PER_SEC; /* 1 secs */
  534. +
  535. +/* Use fast path if app-limited, no loss/ECN, and target cwnd was reached? */
  536. +static const bool bbr_fast_path = true;
  537. +
  538. +/* Use fast ack mode? */
  539. +static const bool bbr_fast_ack_mode = true;
  540. +
  541. +static u32 bbr_max_bw(const struct sock *sk);
  542. +static u32 bbr_bw(const struct sock *sk);
  543. +static void bbr_exit_probe_rtt(struct sock *sk);
  544. +static void bbr_reset_congestion_signals(struct sock *sk);
  545. +static void bbr_run_loss_probe_recovery(struct sock *sk);
  546. +
  547. +static void bbr_check_probe_rtt_done(struct sock *sk);
  548. +
  549. +/* This connection can use ECN if both endpoints have signaled ECN support in
  550. + * the handshake and the per-route settings indicated this is a
  551. + * shallow-threshold ECN environment, meaning both:
  552. + * (a) ECN CE marks indicate low-latency/shallow-threshold congestion, and
  553. + * (b) TCP endpoints provide precise ACKs that only ACK data segments
  554. + * with consistent ECN CE status
  555. + */
  556. +static bool bbr_can_use_ecn(const struct sock *sk)
  557. +{
  558. + return (tcp_sk(sk)->ecn_flags & TCP_ECN_OK) &&
  559. + (tcp_sk(sk)->ecn_flags & TCP_ECN_LOW);
  560. +}
  561. +
  562. +/* Do we estimate that STARTUP filled the pipe? */
  563. +static bool bbr_full_bw_reached(const struct sock *sk)
  564. +{
  565. + const struct bbr *bbr = inet_csk_ca(sk);
  566. +
  567. + return bbr->full_bw_reached;
  568. +}
  569. +
  570. +/* Return the windowed max recent bandwidth sample, in pkts/uS << BW_SCALE. */
  571. +static u32 bbr_max_bw(const struct sock *sk)
  572. +{
  573. + const struct bbr *bbr = inet_csk_ca(sk);
  574. +
  575. + return max(bbr->bw_hi[0], bbr->bw_hi[1]);
  576. +}
  577. +
  578. +/* Return the estimated bandwidth of the path, in pkts/uS << BW_SCALE. */
  579. +static u32 bbr_bw(const struct sock *sk)
  580. +{
  581. + const struct bbr *bbr = inet_csk_ca(sk);
  582. +
  583. + return min(bbr_max_bw(sk), bbr->bw_lo);
  584. +}
  585. +
  586. +/* Return maximum extra acked in past k-2k round trips,
  587. + * where k = bbr_extra_acked_win_rtts.
  588. + */
  589. +static u16 bbr_extra_acked(const struct sock *sk)
  590. +{
  591. + struct bbr *bbr = inet_csk_ca(sk);
  592. +
  593. + return max(bbr->extra_acked[0], bbr->extra_acked[1]);
  594. +}
  595. +
  596. +/* Return rate in bytes per second, optionally with a gain.
  597. + * The order here is chosen carefully to avoid overflow of u64. This should
  598. + * work for input rates of up to 2.9Tbit/sec and gain of 2.89x.
  599. + */
  600. +static u64 bbr_rate_bytes_per_sec(struct sock *sk, u64 rate, int gain,
  601. + int margin)
  602. +{
  603. + unsigned int mss = tcp_sk(sk)->mss_cache;
  604. +
  605. + rate *= mss;
  606. + rate *= gain;
  607. + rate >>= BBR_SCALE;
  608. + rate *= USEC_PER_SEC / 100 * (100 - margin);
  609. + rate >>= BW_SCALE;
  610. + rate = max(rate, 1ULL);
  611. + return rate;
  612. +}
  613. +
  614. +static u64 bbr_bw_bytes_per_sec(struct sock *sk, u64 rate)
  615. +{
  616. + return bbr_rate_bytes_per_sec(sk, rate, BBR_UNIT, 0);
  617. +}
  618. +
  619. +/* Convert a BBR bw and gain factor to a pacing rate in bytes per second. */
  620. +static unsigned long bbr_bw_to_pacing_rate(struct sock *sk, u32 bw, int gain)
  621. +{
  622. + u64 rate = bw;
  623. +
  624. + rate = bbr_rate_bytes_per_sec(sk, rate, gain,
  625. + bbr_pacing_margin_percent);
  626. + rate = min_t(u64, rate, READ_ONCE(sk->sk_max_pacing_rate));
  627. + return rate;
  628. +}
  629. +
  630. +/* Initialize pacing rate to: startup_pacing_gain * init_cwnd / RTT. */
  631. +static void bbr_init_pacing_rate_from_rtt(struct sock *sk)
  632. +{
  633. + struct tcp_sock *tp = tcp_sk(sk);
  634. + struct bbr *bbr = inet_csk_ca(sk);
  635. + u64 bw;
  636. + u32 rtt_us;
  637. +
  638. + if (tp->srtt_us) { /* any RTT sample yet? */
  639. + rtt_us = max(tp->srtt_us >> 3, 1U);
  640. + bbr->has_seen_rtt = 1;
  641. + } else { /* no RTT sample yet */
  642. + rtt_us = USEC_PER_MSEC; /* use nominal default RTT */
  643. + }
  644. + bw = (u64)tcp_snd_cwnd(tp) * BW_UNIT;
  645. + do_div(bw, rtt_us);
  646. + WRITE_ONCE(sk->sk_pacing_rate,
  647. + bbr_bw_to_pacing_rate(sk, bw,
  648. + bbr_param(sk, startup_pacing_gain)));
  649. +}
  650. +
  651. +/* Pace using current bw estimate and a gain factor. */
  652. +static void bbr_set_pacing_rate(struct sock *sk, u32 bw, int gain)
  653. +{
  654. + struct tcp_sock *tp = tcp_sk(sk);
  655. + struct bbr *bbr = inet_csk_ca(sk);
  656. + unsigned long rate = bbr_bw_to_pacing_rate(sk, bw, gain);
  657. +
  658. + if (unlikely(!bbr->has_seen_rtt && tp->srtt_us))
  659. + bbr_init_pacing_rate_from_rtt(sk);
  660. + if (bbr_full_bw_reached(sk) || rate > READ_ONCE(sk->sk_pacing_rate))
  661. + WRITE_ONCE(sk->sk_pacing_rate, rate);
  662. +}
  663. +
  664. +/* Return the number of segments BBR would like in a TSO/GSO skb, given a
  665. + * particular max gso size as a constraint. TODO: make this simpler and more
  666. + * consistent by switching bbr to just call tcp_tso_autosize().
  667. + */
  668. +static u32 bbr_tso_segs_generic(struct sock *sk, unsigned int mss_now,
  669. + u32 gso_max_size)
  670. +{
  671. + struct bbr *bbr = inet_csk_ca(sk);
  672. + u32 segs, r;
  673. + u64 bytes;
  674. +
  675. + /* Budget a TSO/GSO burst size allowance based on bw (pacing_rate). */
  676. + bytes = READ_ONCE(sk->sk_pacing_rate) >> READ_ONCE(sk->sk_pacing_shift);
  677. +
  678. + /* Budget a TSO/GSO burst size allowance based on min_rtt. For every
  679. + * K = 2^tso_rtt_shift microseconds of min_rtt, halve the burst.
  680. + * The min_rtt-based burst allowance is: 64 KBytes / 2^(min_rtt/K)
  681. + */
  682. + if (bbr_param(sk, tso_rtt_shift)) {
  683. + r = bbr->min_rtt_us >> bbr_param(sk, tso_rtt_shift);
  684. + if (r < BITS_PER_TYPE(u32)) /* prevent undefined behavior */
  685. + bytes += GSO_LEGACY_MAX_SIZE >> r;
  686. + }
  687. +
  688. + bytes = min_t(u32, bytes, gso_max_size - 1 - MAX_TCP_HEADER);
  689. + segs = max_t(u32, bytes / mss_now,
  690. + sock_net(sk)->ipv4.sysctl_tcp_min_tso_segs);
  691. + return segs;
  692. +}
  693. +
  694. +/* Custom tcp_tso_autosize() for BBR, used at transmit time to cap skb size. */
  695. +__bpf_kfunc static u32 bbr_tso_segs(struct sock *sk, unsigned int mss_now)
  696. +{
  697. + return bbr_tso_segs_generic(sk, mss_now, sk->sk_gso_max_size);
  698. +}
  699. +
  700. +/* Like bbr_tso_segs(), using mss_cache, ignoring driver's sk_gso_max_size. */
  701. +static u32 bbr_tso_segs_goal(struct sock *sk)
  702. +{
  703. + struct tcp_sock *tp = tcp_sk(sk);
  704. +
  705. + return bbr_tso_segs_generic(sk, tp->mss_cache, GSO_LEGACY_MAX_SIZE);
  706. +}
  707. +
  708. +/* Save "last known good" cwnd so we can restore it after losses or PROBE_RTT */
  709. +static void bbr_save_cwnd(struct sock *sk)
  710. +{
  711. + struct tcp_sock *tp = tcp_sk(sk);
  712. + struct bbr *bbr = inet_csk_ca(sk);
  713. +
  714. + if (bbr->prev_ca_state < TCP_CA_Recovery && bbr->mode != BBR_PROBE_RTT)
  715. + bbr->prior_cwnd = tcp_snd_cwnd(tp); /* this cwnd is good enough */
  716. + else /* loss recovery or BBR_PROBE_RTT have temporarily cut cwnd */
  717. + bbr->prior_cwnd = max(bbr->prior_cwnd, tcp_snd_cwnd(tp));
  718. +}
  719. +
  720. +__bpf_kfunc static void bbr_cwnd_event(struct sock *sk, enum tcp_ca_event event)
  721. +{
  722. + struct tcp_sock *tp = tcp_sk(sk);
  723. + struct bbr *bbr = inet_csk_ca(sk);
  724. +
  725. + if (event == CA_EVENT_TX_START) {
  726. + if (!tp->app_limited)
  727. + return;
  728. + bbr->idle_restart = 1;
  729. + bbr->ack_epoch_mstamp = tp->tcp_mstamp;
  730. + bbr->ack_epoch_acked = 0;
  731. + /* Avoid pointless buffer overflows: pace at est. bw if we don't
  732. + * need more speed (we're restarting from idle and app-limited).
  733. + */
  734. + if (bbr->mode == BBR_PROBE_BW)
  735. + bbr_set_pacing_rate(sk, bbr_bw(sk), BBR_UNIT);
  736. + else if (bbr->mode == BBR_PROBE_RTT)
  737. + bbr_check_probe_rtt_done(sk);
  738. + } else if ((event == CA_EVENT_ECN_IS_CE ||
  739. + event == CA_EVENT_ECN_NO_CE) &&
  740. + bbr_can_use_ecn(sk) &&
  741. + bbr_param(sk, precise_ece_ack)) {
  742. + u32 state = bbr->ce_state;
  743. + dctcp_ece_ack_update(sk, event, &bbr->prior_rcv_nxt, &state);
  744. + bbr->ce_state = state;
  745. + } else if (event == CA_EVENT_TLP_RECOVERY &&
  746. + bbr_param(sk, loss_probe_recovery)) {
  747. + bbr_run_loss_probe_recovery(sk);
  748. + }
  749. +}
  750. +
  751. +/* Calculate bdp based on min RTT and the estimated bottleneck bandwidth:
  752. + *
  753. + * bdp = ceil(bw * min_rtt * gain)
  754. + *
  755. + * The key factor, gain, controls the amount of queue. While a small gain
  756. + * builds a smaller queue, it becomes more vulnerable to noise in RTT
  757. + * measurements (e.g., delayed ACKs or other ACK compression effects). This
  758. + * noise may cause BBR to under-estimate the rate.
  759. + */
  760. +static u32 bbr_bdp(struct sock *sk, u32 bw, int gain)
  761. +{
  762. + struct bbr *bbr = inet_csk_ca(sk);
  763. + u32 bdp;
  764. + u64 w;
  765. +
  766. + /* If we've never had a valid RTT sample, cap cwnd at the initial
  767. + * default. This should only happen when the connection is not using TCP
  768. + * timestamps and has retransmitted all of the SYN/SYNACK/data packets
  769. + * ACKed so far. In this case, an RTO can cut cwnd to 1, in which
  770. + * case we need to slow-start up toward something safe: initial cwnd.
  771. + */
  772. + if (unlikely(bbr->min_rtt_us == ~0U)) /* no valid RTT samples yet? */
  773. + return bbr->init_cwnd; /* be safe: cap at initial cwnd */
  774. +
  775. + w = (u64)bw * bbr->min_rtt_us;
  776. +
  777. + /* Apply a gain to the given value, remove the BW_SCALE shift, and
  778. + * round the value up to avoid a negative feedback loop.
  779. + */
  780. + bdp = (((w * gain) >> BBR_SCALE) + BW_UNIT - 1) / BW_UNIT;
  781. +
  782. + return bdp;
  783. +}
  784. +
  785. +/* To achieve full performance in high-speed paths, we budget enough cwnd to
  786. + * fit full-sized skbs in-flight on both end hosts to fully utilize the path:
  787. + * - one skb in sending host Qdisc,
  788. + * - one skb in sending host TSO/GSO engine
  789. + * - one skb being received by receiver host LRO/GRO/delayed-ACK engine
  790. + * Don't worry, at low rates this won't bloat cwnd because
  791. + * in such cases tso_segs_goal is small. The minimum cwnd is 4 packets,
  792. + * which allows 2 outstanding 2-packet sequences, to try to keep pipe
  793. + * full even with ACK-every-other-packet delayed ACKs.
  794. + */
  795. +static u32 bbr_quantization_budget(struct sock *sk, u32 cwnd)
  796. +{
  797. + struct bbr *bbr = inet_csk_ca(sk);
  798. + u32 tso_segs_goal;
  799. +
  800. + tso_segs_goal = 3 * bbr_tso_segs_goal(sk);
  801. +
  802. + /* Allow enough full-sized skbs in flight to utilize end systems. */
  803. + cwnd = max_t(u32, cwnd, tso_segs_goal);
  804. + cwnd = max_t(u32, cwnd, bbr_param(sk, cwnd_min_target));
  805. + /* Ensure gain cycling gets inflight above BDP even for small BDPs. */
  806. + if (bbr->mode == BBR_PROBE_BW && bbr->cycle_idx == BBR_BW_PROBE_UP)
  807. + cwnd += 2;
  808. +
  809. + return cwnd;
  810. +}
  811. +
  812. +/* Find inflight based on min RTT and the estimated bottleneck bandwidth. */
  813. +static u32 bbr_inflight(struct sock *sk, u32 bw, int gain)
  814. +{
  815. + u32 inflight;
  816. +
  817. + inflight = bbr_bdp(sk, bw, gain);
  818. + inflight = bbr_quantization_budget(sk, inflight);
  819. +
  820. + return inflight;
  821. +}
  822. +
  823. +/* With pacing at lower layers, there's often less data "in the network" than
  824. + * "in flight". With TSQ and departure time pacing at lower layers (e.g. fq),
  825. + * we often have several skbs queued in the pacing layer with a pre-scheduled
  826. + * earliest departure time (EDT). BBR adapts its pacing rate based on the
  827. + * inflight level that it estimates has already been "baked in" by previous
  828. + * departure time decisions. We calculate a rough estimate of the number of our
  829. + * packets that might be in the network at the earliest departure time for the
  830. + * next skb scheduled:
  831. + * in_network_at_edt = inflight_at_edt - (EDT - now) * bw
  832. + * If we're increasing inflight, then we want to know if the transmit of the
  833. + * EDT skb will push inflight above the target, so inflight_at_edt includes
  834. + * bbr_tso_segs_goal() from the skb departing at EDT. If decreasing inflight,
  835. + * then estimate if inflight will sink too low just before the EDT transmit.
  836. + */
  837. +static u32 bbr_packets_in_net_at_edt(struct sock *sk, u32 inflight_now)
  838. +{
  839. + struct tcp_sock *tp = tcp_sk(sk);
  840. + struct bbr *bbr = inet_csk_ca(sk);
  841. + u64 now_ns, edt_ns, interval_us;
  842. + u32 interval_delivered, inflight_at_edt;
  843. +
  844. + now_ns = tp->tcp_clock_cache;
  845. + edt_ns = max(tp->tcp_wstamp_ns, now_ns);
  846. + interval_us = div_u64(edt_ns - now_ns, NSEC_PER_USEC);
  847. + interval_delivered = (u64)bbr_bw(sk) * interval_us >> BW_SCALE;
  848. + inflight_at_edt = inflight_now;
  849. + if (bbr->pacing_gain > BBR_UNIT) /* increasing inflight */
  850. + inflight_at_edt += bbr_tso_segs_goal(sk); /* include EDT skb */
  851. + if (interval_delivered >= inflight_at_edt)
  852. + return 0;
  853. + return inflight_at_edt - interval_delivered;
  854. +}
  855. +
  856. +/* Find the cwnd increment based on estimate of ack aggregation */
  857. +static u32 bbr_ack_aggregation_cwnd(struct sock *sk)
  858. +{
  859. + u32 max_aggr_cwnd, aggr_cwnd = 0;
  860. +
  861. + if (bbr_param(sk, extra_acked_gain)) {
  862. + max_aggr_cwnd = ((u64)bbr_bw(sk) * bbr_extra_acked_max_us)
  863. + / BW_UNIT;
  864. + aggr_cwnd = (bbr_param(sk, extra_acked_gain) * bbr_extra_acked(sk))
  865. + >> BBR_SCALE;
  866. + aggr_cwnd = min(aggr_cwnd, max_aggr_cwnd);
  867. + }
  868. +
  869. + return aggr_cwnd;
  870. +}
  871. +
  872. +/* Returns the cwnd for PROBE_RTT mode. */
  873. +static u32 bbr_probe_rtt_cwnd(struct sock *sk)
  874. +{
  875. + return max_t(u32, bbr_param(sk, cwnd_min_target),
  876. + bbr_bdp(sk, bbr_bw(sk), bbr_param(sk, probe_rtt_cwnd_gain)));
  877. +}
  878. +
  879. +/* Slow-start up toward target cwnd (if bw estimate is growing, or packet loss
  880. + * has drawn us down below target), or snap down to target if we're above it.
  881. + */
  882. +static void bbr_set_cwnd(struct sock *sk, const struct rate_sample *rs,
  883. + u32 acked, u32 bw, int gain, u32 cwnd,
  884. + struct bbr_context *ctx)
  885. +{
  886. + struct tcp_sock *tp = tcp_sk(sk);
  887. + struct bbr *bbr = inet_csk_ca(sk);
  888. + u32 target_cwnd = 0;
  889. +
  890. + if (!acked)
  891. + goto done; /* no packet fully ACKed; just apply caps */
  892. +
  893. + target_cwnd = bbr_bdp(sk, bw, gain);
  894. +
  895. + /* Increment the cwnd to account for excess ACKed data that seems
  896. + * due to aggregation (of data and/or ACKs) visible in the ACK stream.
  897. + */
  898. + target_cwnd += bbr_ack_aggregation_cwnd(sk);
  899. + target_cwnd = bbr_quantization_budget(sk, target_cwnd);
  900. +
  901. + /* Update cwnd and enable fast path if cwnd reaches target_cwnd. */
  902. + bbr->try_fast_path = 0;
  903. + if (bbr_full_bw_reached(sk)) { /* only cut cwnd if we filled the pipe */
  904. + cwnd += acked;
  905. + if (cwnd >= target_cwnd) {
  906. + cwnd = target_cwnd;
  907. + bbr->try_fast_path = 1;
  908. + }
  909. + } else if (cwnd < target_cwnd || cwnd < 2 * bbr->init_cwnd) {
  910. + cwnd += acked;
  911. + } else {
  912. + bbr->try_fast_path = 1;
  913. + }
  914. +
  915. + cwnd = max_t(u32, cwnd, bbr_param(sk, cwnd_min_target));
  916. +done:
  917. + tcp_snd_cwnd_set(tp, min(cwnd, tp->snd_cwnd_clamp)); /* global cap */
  918. + if (bbr->mode == BBR_PROBE_RTT) /* drain queue, refresh min_rtt */
  919. + tcp_snd_cwnd_set(tp, min_t(u32, tcp_snd_cwnd(tp),
  920. + bbr_probe_rtt_cwnd(sk)));
  921. +}
  922. +
  923. +static void bbr_reset_startup_mode(struct sock *sk)
  924. +{
  925. + struct bbr *bbr = inet_csk_ca(sk);
  926. +
  927. + bbr->mode = BBR_STARTUP;
  928. +}
  929. +
  930. +/* See if we have reached next round trip. Upon start of the new round,
  931. + * returns packets delivered since previous round start plus this ACK.
  932. + */
  933. +static u32 bbr_update_round_start(struct sock *sk,
  934. + const struct rate_sample *rs, struct bbr_context *ctx)
  935. +{
  936. + struct tcp_sock *tp = tcp_sk(sk);
  937. + struct bbr *bbr = inet_csk_ca(sk);
  938. + u32 round_delivered = 0;
  939. +
  940. + bbr->round_start = 0;
  941. +
  942. + /* See if we've reached the next RTT */
  943. + if (rs->interval_us > 0 &&
  944. + !before(rs->prior_delivered, bbr->next_rtt_delivered)) {
  945. + round_delivered = tp->delivered - bbr->next_rtt_delivered;
  946. + bbr->next_rtt_delivered = tp->delivered;
  947. + bbr->round_start = 1;
  948. + }
  949. + return round_delivered;
  950. +}
  951. +
  952. +/* Calculate the bandwidth based on how fast packets are delivered */
  953. +static void bbr_calculate_bw_sample(struct sock *sk,
  954. + const struct rate_sample *rs, struct bbr_context *ctx)
  955. +{
  956. + u64 bw = 0;
  957. +
  958. + /* Divide delivered by the interval to find a (lower bound) bottleneck
  959. + * bandwidth sample. Delivered is in packets and interval_us in uS and
  960. + * ratio will be <<1 for most connections. So delivered is first scaled.
  961. + * Round up to allow growth at low rates, even with integer division.
  962. + */
  963. + if (rs->interval_us > 0) {
  964. + if (WARN_ONCE(rs->delivered < 0,
  965. + "negative delivered: %d interval_us: %ld\n",
  966. + rs->delivered, rs->interval_us))
  967. + return;
  968. +
  969. + bw = DIV_ROUND_UP_ULL((u64)rs->delivered * BW_UNIT, rs->interval_us);
  970. + }
  971. +
  972. + ctx->sample_bw = bw;
  973. +}
  974. +
  975. +/* Estimates the windowed max degree of ack aggregation.
  976. + * This is used to provision extra in-flight data to keep sending during
  977. + * inter-ACK silences.
  978. + *
  979. + * Degree of ack aggregation is estimated as extra data acked beyond expected.
  980. + *
  981. + * max_extra_acked = "maximum recent excess data ACKed beyond max_bw * interval"
  982. + * cwnd += max_extra_acked
  983. + *
  984. + * Max extra_acked is clamped by cwnd and bw * bbr_extra_acked_max_us (100 ms).
  985. + * Max filter is an approximate sliding window of 5-10 (packet timed) round
  986. + * trips for non-startup phase, and 1-2 round trips for startup.
  987. + */
  988. +static void bbr_update_ack_aggregation(struct sock *sk,
  989. + const struct rate_sample *rs)
  990. +{
  991. + u32 epoch_us, expected_acked, extra_acked;
  992. + struct bbr *bbr = inet_csk_ca(sk);
  993. + struct tcp_sock *tp = tcp_sk(sk);
  994. + u32 extra_acked_win_rtts_thresh = bbr_param(sk, extra_acked_win_rtts);
  995. +
  996. + if (!bbr_param(sk, extra_acked_gain) || rs->acked_sacked <= 0 ||
  997. + rs->delivered < 0 || rs->interval_us <= 0)
  998. + return;
  999. +
  1000. + if (bbr->round_start) {
  1001. + bbr->extra_acked_win_rtts = min(0x1F,
  1002. + bbr->extra_acked_win_rtts + 1);
  1003. + if (!bbr_full_bw_reached(sk))
  1004. + extra_acked_win_rtts_thresh = 1;
  1005. + if (bbr->extra_acked_win_rtts >=
  1006. + extra_acked_win_rtts_thresh) {
  1007. + bbr->extra_acked_win_rtts = 0;
  1008. + bbr->extra_acked_win_idx = bbr->extra_acked_win_idx ?
  1009. + 0 : 1;
  1010. + bbr->extra_acked[bbr->extra_acked_win_idx] = 0;
  1011. + }
  1012. + }
  1013. +
  1014. + /* Compute how many packets we expected to be delivered over epoch. */
  1015. + epoch_us = tcp_stamp_us_delta(tp->delivered_mstamp,
  1016. + bbr->ack_epoch_mstamp);
  1017. + expected_acked = ((u64)bbr_bw(sk) * epoch_us) / BW_UNIT;
  1018. +
  1019. + /* Reset the aggregation epoch if ACK rate is below expected rate or
  1020. + * significantly large no. of ack received since epoch (potentially
  1021. + * quite old epoch).
  1022. + */
  1023. + if (bbr->ack_epoch_acked <= expected_acked ||
  1024. + (bbr->ack_epoch_acked + rs->acked_sacked >=
  1025. + bbr_ack_epoch_acked_reset_thresh)) {
  1026. + bbr->ack_epoch_acked = 0;
  1027. + bbr->ack_epoch_mstamp = tp->delivered_mstamp;
  1028. + expected_acked = 0;
  1029. + }
  1030. +
  1031. + /* Compute excess data delivered, beyond what was expected. */
  1032. + bbr->ack_epoch_acked = min_t(u32, 0xFFFFF,
  1033. + bbr->ack_epoch_acked + rs->acked_sacked);
  1034. + extra_acked = bbr->ack_epoch_acked - expected_acked;
  1035. + extra_acked = min(extra_acked, tcp_snd_cwnd(tp));
  1036. + if (extra_acked > bbr->extra_acked[bbr->extra_acked_win_idx])
  1037. + bbr->extra_acked[bbr->extra_acked_win_idx] = extra_acked;
  1038. +}
  1039. +
  1040. +static void bbr_check_probe_rtt_done(struct sock *sk)
  1041. +{
  1042. + struct tcp_sock *tp = tcp_sk(sk);
  1043. + struct bbr *bbr = inet_csk_ca(sk);
  1044. +
  1045. + if (!(bbr->probe_rtt_done_stamp &&
  1046. + after(tcp_jiffies32, bbr->probe_rtt_done_stamp)))
  1047. + return;
  1048. +
  1049. + bbr->probe_rtt_min_stamp = tcp_jiffies32; /* schedule next PROBE_RTT */
  1050. + tcp_snd_cwnd_set(tp, max(tcp_snd_cwnd(tp), bbr->prior_cwnd));
  1051. + bbr_exit_probe_rtt(sk);
  1052. +}
  1053. +
  1054. +/* The goal of PROBE_RTT mode is to have BBR flows cooperatively and
  1055. + * periodically drain the bottleneck queue, to converge to measure the true
  1056. + * min_rtt (unloaded propagation delay). This allows the flows to keep queues
  1057. + * small (reducing queuing delay and packet loss) and achieve fairness among
  1058. + * BBR flows.
  1059. + *
  1060. + * The min_rtt filter window is 10 seconds. When the min_rtt estimate expires,
  1061. + * we enter PROBE_RTT mode and cap the cwnd at bbr_cwnd_min_target=4 packets.
  1062. + * After at least bbr_probe_rtt_mode_ms=200ms and at least one packet-timed
  1063. + * round trip elapsed with that flight size <= 4, we leave PROBE_RTT mode and
  1064. + * re-enter the previous mode. BBR uses 200ms to approximately bound the
  1065. + * performance penalty of PROBE_RTT's cwnd capping to roughly 2% (200ms/10s).
  1066. + *
  1067. + * Note that flows need only pay 2% if they are busy sending over the last 10
  1068. + * seconds. Interactive applications (e.g., Web, RPCs, video chunks) often have
  1069. + * natural silences or low-rate periods within 10 seconds where the rate is low
  1070. + * enough for long enough to drain its queue in the bottleneck. We pick up
  1071. + * these min RTT measurements opportunistically with our min_rtt filter. :-)
  1072. + */
  1073. +static void bbr_update_min_rtt(struct sock *sk, const struct rate_sample *rs)
  1074. +{
  1075. + struct tcp_sock *tp = tcp_sk(sk);
  1076. + struct bbr *bbr = inet_csk_ca(sk);
  1077. + bool probe_rtt_expired, min_rtt_expired;
  1078. + u32 expire;
  1079. +
  1080. + /* Track min RTT in probe_rtt_win_ms to time next PROBE_RTT state. */
  1081. + expire = bbr->probe_rtt_min_stamp +
  1082. + msecs_to_jiffies(bbr_param(sk, probe_rtt_win_ms));
  1083. + probe_rtt_expired = after(tcp_jiffies32, expire);
  1084. + if (rs->rtt_us >= 0 &&
  1085. + (rs->rtt_us < bbr->probe_rtt_min_us ||
  1086. + (probe_rtt_expired && !rs->is_ack_delayed))) {
  1087. + bbr->probe_rtt_min_us = rs->rtt_us;
  1088. + bbr->probe_rtt_min_stamp = tcp_jiffies32;
  1089. + }
  1090. + /* Track min RTT seen in the min_rtt_win_sec filter window: */
  1091. + expire = bbr->min_rtt_stamp + bbr_param(sk, min_rtt_win_sec) * HZ;
  1092. + min_rtt_expired = after(tcp_jiffies32, expire);
  1093. + if (bbr->probe_rtt_min_us <= bbr->min_rtt_us ||
  1094. + min_rtt_expired) {
  1095. + bbr->min_rtt_us = bbr->probe_rtt_min_us;
  1096. + bbr->min_rtt_stamp = bbr->probe_rtt_min_stamp;
  1097. + }
  1098. +
  1099. + if (bbr_param(sk, probe_rtt_mode_ms) > 0 && probe_rtt_expired &&
  1100. + !bbr->idle_restart && bbr->mode != BBR_PROBE_RTT) {
  1101. + bbr->mode = BBR_PROBE_RTT; /* dip, drain queue */
  1102. + bbr_save_cwnd(sk); /* note cwnd so we can restore it */
  1103. + bbr->probe_rtt_done_stamp = 0;
  1104. + bbr->ack_phase = BBR_ACKS_PROBE_STOPPING;
  1105. + bbr->next_rtt_delivered = tp->delivered;
  1106. + }
  1107. +
  1108. + if (bbr->mode == BBR_PROBE_RTT) {
  1109. + /* Ignore low rate samples during this mode. */
  1110. + tp->app_limited =
  1111. + (tp->delivered + tcp_packets_in_flight(tp)) ? : 1;
  1112. + /* Maintain min packets in flight for max(200 ms, 1 round). */
  1113. + if (!bbr->probe_rtt_done_stamp &&
  1114. + tcp_packets_in_flight(tp) <= bbr_probe_rtt_cwnd(sk)) {
  1115. + bbr->probe_rtt_done_stamp = tcp_jiffies32 +
  1116. + msecs_to_jiffies(bbr_param(sk, probe_rtt_mode_ms));
  1117. + bbr->probe_rtt_round_done = 0;
  1118. + bbr->next_rtt_delivered = tp->delivered;
  1119. + } else if (bbr->probe_rtt_done_stamp) {
  1120. + if (bbr->round_start)
  1121. + bbr->probe_rtt_round_done = 1;
  1122. + if (bbr->probe_rtt_round_done)
  1123. + bbr_check_probe_rtt_done(sk);
  1124. + }
  1125. + }
  1126. + /* Restart after idle ends only once we process a new S/ACK for data */
  1127. + if (rs->delivered > 0)
  1128. + bbr->idle_restart = 0;
  1129. +}
  1130. +
  1131. +static void bbr_update_gains(struct sock *sk)
  1132. +{
  1133. + struct bbr *bbr = inet_csk_ca(sk);
  1134. +
  1135. + switch (bbr->mode) {
  1136. + case BBR_STARTUP:
  1137. + bbr->pacing_gain = bbr_param(sk, startup_pacing_gain);
  1138. + bbr->cwnd_gain = bbr_param(sk, startup_cwnd_gain);
  1139. + break;
  1140. + case BBR_DRAIN:
  1141. + bbr->pacing_gain = bbr_param(sk, drain_gain); /* slow, to drain */
  1142. + bbr->cwnd_gain = bbr_param(sk, startup_cwnd_gain); /* keep cwnd */
  1143. + break;
  1144. + case BBR_PROBE_BW:
  1145. + bbr->pacing_gain = bbr_pacing_gain[bbr->cycle_idx];
  1146. + bbr->cwnd_gain = bbr_param(sk, cwnd_gain);
  1147. + if (bbr_param(sk, bw_probe_cwnd_gain) &&
  1148. + bbr->cycle_idx == BBR_BW_PROBE_UP)
  1149. + bbr->cwnd_gain +=
  1150. + BBR_UNIT * bbr_param(sk, bw_probe_cwnd_gain) / 4;
  1151. + break;
  1152. + case BBR_PROBE_RTT:
  1153. + bbr->pacing_gain = BBR_UNIT;
  1154. + bbr->cwnd_gain = BBR_UNIT;
  1155. + break;
  1156. + default:
  1157. + WARN_ONCE(1, "BBR bad mode: %u\n", bbr->mode);
  1158. + break;
  1159. + }
  1160. +}
  1161. +
  1162. +__bpf_kfunc static u32 bbr_sndbuf_expand(struct sock *sk)
  1163. +{
  1164. + /* Provision 3 * cwnd since BBR may slow-start even during recovery. */
  1165. + return 3;
  1166. +}
  1167. +
  1168. +/* Incorporate a new bw sample into the current window of our max filter. */
  1169. +static void bbr_take_max_bw_sample(struct sock *sk, u32 bw)
  1170. +{
  1171. + struct bbr *bbr = inet_csk_ca(sk);
  1172. +
  1173. + bbr->bw_hi[1] = max(bw, bbr->bw_hi[1]);
  1174. +}
  1175. +
  1176. +/* Keep max of last 1-2 cycles. Each PROBE_BW cycle, flip filter window. */
  1177. +static void bbr_advance_max_bw_filter(struct sock *sk)
  1178. +{
  1179. + struct bbr *bbr = inet_csk_ca(sk);
  1180. +
  1181. + if (!bbr->bw_hi[1])
  1182. + return; /* no samples in this window; remember old window */
  1183. + bbr->bw_hi[0] = bbr->bw_hi[1];
  1184. + bbr->bw_hi[1] = 0;
  1185. +}
  1186. +
  1187. +/* Reset the estimator for reaching full bandwidth based on bw plateau. */
  1188. +static void bbr_reset_full_bw(struct sock *sk)
  1189. +{
  1190. + struct bbr *bbr = inet_csk_ca(sk);
  1191. +
  1192. + bbr->full_bw = 0;
  1193. + bbr->full_bw_cnt = 0;
  1194. + bbr->full_bw_now = 0;
  1195. +}
  1196. +
  1197. +/* How much do we want in flight? Our BDP, unless congestion cut cwnd. */
  1198. +static u32 bbr_target_inflight(struct sock *sk)
  1199. +{
  1200. + u32 bdp = bbr_inflight(sk, bbr_bw(sk), BBR_UNIT);
  1201. +
  1202. + return min(bdp, tcp_sk(sk)->snd_cwnd);
  1203. +}
  1204. +
  1205. +static bool bbr_is_probing_bandwidth(struct sock *sk)
  1206. +{
  1207. + struct bbr *bbr = inet_csk_ca(sk);
  1208. +
  1209. + return (bbr->mode == BBR_STARTUP) ||
  1210. + (bbr->mode == BBR_PROBE_BW &&
  1211. + (bbr->cycle_idx == BBR_BW_PROBE_REFILL ||
  1212. + bbr->cycle_idx == BBR_BW_PROBE_UP));
  1213. +}
  1214. +
  1215. +/* Has the given amount of time elapsed since we marked the phase start? */
  1216. +static bool bbr_has_elapsed_in_phase(const struct sock *sk, u32 interval_us)
  1217. +{
  1218. + const struct tcp_sock *tp = tcp_sk(sk);
  1219. + const struct bbr *bbr = inet_csk_ca(sk);
  1220. +
  1221. + return tcp_stamp_us_delta(tp->tcp_mstamp,
  1222. + bbr->cycle_mstamp + interval_us) > 0;
  1223. +}
  1224. +
  1225. +static void bbr_handle_queue_too_high_in_startup(struct sock *sk)
  1226. +{
  1227. + struct bbr *bbr = inet_csk_ca(sk);
  1228. + u32 bdp; /* estimated BDP in packets, with quantization budget */
  1229. +
  1230. + bbr->full_bw_reached = 1;
  1231. +
  1232. + bdp = bbr_inflight(sk, bbr_max_bw(sk), BBR_UNIT);
  1233. + bbr->inflight_hi = max(bdp, bbr->inflight_latest);
  1234. +}
  1235. +
  1236. +/* Exit STARTUP upon N consecutive rounds with ECN mark rate > ecn_thresh. */
  1237. +static void bbr_check_ecn_too_high_in_startup(struct sock *sk, u32 ce_ratio)
  1238. +{
  1239. + struct bbr *bbr = inet_csk_ca(sk);
  1240. +
  1241. + if (bbr_full_bw_reached(sk) || !bbr->ecn_eligible ||
  1242. + !bbr_param(sk, full_ecn_cnt) || !bbr_param(sk, ecn_thresh))
  1243. + return;
  1244. +
  1245. + if (ce_ratio >= bbr_param(sk, ecn_thresh))
  1246. + bbr->startup_ecn_rounds++;
  1247. + else
  1248. + bbr->startup_ecn_rounds = 0;
  1249. +
  1250. + if (bbr->startup_ecn_rounds >= bbr_param(sk, full_ecn_cnt)) {
  1251. + bbr_handle_queue_too_high_in_startup(sk);
  1252. + return;
  1253. + }
  1254. +}
  1255. +
  1256. +/* Updates ecn_alpha and returns ce_ratio. -1 if not available. */
  1257. +static int bbr_update_ecn_alpha(struct sock *sk)
  1258. +{
  1259. + struct tcp_sock *tp = tcp_sk(sk);
  1260. + struct net *net = sock_net(sk);
  1261. + struct bbr *bbr = inet_csk_ca(sk);
  1262. + s32 delivered, delivered_ce;
  1263. + u64 alpha, ce_ratio;
  1264. + u32 gain;
  1265. + bool want_ecn_alpha;
  1266. +
  1267. + /* See if we should use ECN sender logic for this connection. */
  1268. + if (!bbr->ecn_eligible && bbr_can_use_ecn(sk) &&
  1269. + !!bbr_param(sk, ecn_factor) &&
  1270. + (bbr->min_rtt_us <= bbr_ecn_max_rtt_us ||
  1271. + !bbr_ecn_max_rtt_us))
  1272. + bbr->ecn_eligible = 1;
  1273. +
  1274. + /* Skip updating alpha only if not ECN-eligible and PLB is disabled. */
  1275. + want_ecn_alpha = (bbr->ecn_eligible ||
  1276. + (bbr_can_use_ecn(sk) &&
  1277. + READ_ONCE(net->ipv4.sysctl_tcp_plb_enabled)));
  1278. + if (!want_ecn_alpha)
  1279. + return -1;
  1280. +
  1281. + delivered = tp->delivered - bbr->alpha_last_delivered;
  1282. + delivered_ce = tp->delivered_ce - bbr->alpha_last_delivered_ce;
  1283. +
  1284. + if (delivered == 0 || /* avoid divide by zero */
  1285. + WARN_ON_ONCE(delivered < 0 || delivered_ce < 0)) /* backwards? */
  1286. + return -1;
  1287. +
  1288. + BUILD_BUG_ON(BBR_SCALE != TCP_PLB_SCALE);
  1289. + ce_ratio = (u64)delivered_ce << BBR_SCALE;
  1290. + do_div(ce_ratio, delivered);
  1291. +
  1292. + gain = bbr_param(sk, ecn_alpha_gain);
  1293. + alpha = ((BBR_UNIT - gain) * bbr->ecn_alpha) >> BBR_SCALE;
  1294. + alpha += (gain * ce_ratio) >> BBR_SCALE;
  1295. + bbr->ecn_alpha = min_t(u32, alpha, BBR_UNIT);
  1296. +
  1297. + bbr->alpha_last_delivered = tp->delivered;
  1298. + bbr->alpha_last_delivered_ce = tp->delivered_ce;
  1299. +
  1300. + bbr_check_ecn_too_high_in_startup(sk, ce_ratio);
  1301. + return (int)ce_ratio;
  1302. +}
  1303. +
  1304. +/* Protective Load Balancing (PLB). PLB rehashes outgoing data (to a new IPv6
  1305. + * flow label) if it encounters sustained congestion in the form of ECN marks.
  1306. + */
  1307. +static void bbr_plb(struct sock *sk, const struct rate_sample *rs, int ce_ratio)
  1308. +{
  1309. + struct bbr *bbr = inet_csk_ca(sk);
  1310. +
  1311. + if (bbr->round_start && ce_ratio >= 0)
  1312. + tcp_plb_update_state(sk, &bbr->plb, ce_ratio);
  1313. +
  1314. + tcp_plb_check_rehash(sk, &bbr->plb);
  1315. +}
  1316. +
  1317. +/* Each round trip of BBR_BW_PROBE_UP, double volume of probing data. */
  1318. +static void bbr_raise_inflight_hi_slope(struct sock *sk)
  1319. +{
  1320. + struct tcp_sock *tp = tcp_sk(sk);
  1321. + struct bbr *bbr = inet_csk_ca(sk);
  1322. + u32 growth_this_round, cnt;
  1323. +
  1324. + /* Calculate "slope": packets S/Acked per inflight_hi increment. */
  1325. + growth_this_round = 1 << bbr->bw_probe_up_rounds;
  1326. + bbr->bw_probe_up_rounds = min(bbr->bw_probe_up_rounds + 1, 30);
  1327. + cnt = tcp_snd_cwnd(tp) / growth_this_round;
  1328. + cnt = max(cnt, 1U);
  1329. + bbr->bw_probe_up_cnt = cnt;
  1330. +}
  1331. +
  1332. +/* In BBR_BW_PROBE_UP, not seeing high loss/ECN/queue, so raise inflight_hi. */
  1333. +static void bbr_probe_inflight_hi_upward(struct sock *sk,
  1334. + const struct rate_sample *rs)
  1335. +{
  1336. + struct tcp_sock *tp = tcp_sk(sk);
  1337. + struct bbr *bbr = inet_csk_ca(sk);
  1338. + u32 delta;
  1339. +
  1340. + if (!tp->is_cwnd_limited || tcp_snd_cwnd(tp) < bbr->inflight_hi)
  1341. + return; /* not fully using inflight_hi, so don't grow it */
  1342. +
  1343. + /* For each bw_probe_up_cnt packets ACKed, increase inflight_hi by 1. */
  1344. + bbr->bw_probe_up_acks += rs->acked_sacked;
  1345. + if (bbr->bw_probe_up_acks >= bbr->bw_probe_up_cnt) {
  1346. + delta = bbr->bw_probe_up_acks / bbr->bw_probe_up_cnt;
  1347. + bbr->bw_probe_up_acks -= delta * bbr->bw_probe_up_cnt;
  1348. + bbr->inflight_hi += delta;
  1349. + bbr->try_fast_path = 0; /* Need to update cwnd */
  1350. + }
  1351. +
  1352. + if (bbr->round_start)
  1353. + bbr_raise_inflight_hi_slope(sk);
  1354. +}
  1355. +
  1356. +/* Does loss/ECN rate for this sample say inflight is "too high"?
  1357. + * This is used by both the bbr_check_loss_too_high_in_startup() function,
  1358. + * and in PROBE_UP.
  1359. + */
  1360. +static bool bbr_is_inflight_too_high(const struct sock *sk,
  1361. + const struct rate_sample *rs)
  1362. +{
  1363. + const struct bbr *bbr = inet_csk_ca(sk);
  1364. + u32 loss_thresh, ecn_thresh;
  1365. +
  1366. + if (rs->lost > 0 && rs->tx_in_flight) {
  1367. + loss_thresh = (u64)rs->tx_in_flight * bbr_param(sk, loss_thresh) >>
  1368. + BBR_SCALE;
  1369. + if (rs->lost > loss_thresh) {
  1370. + return true;
  1371. + }
  1372. + }
  1373. +
  1374. + if (rs->delivered_ce > 0 && rs->delivered > 0 &&
  1375. + bbr->ecn_eligible && !!bbr_param(sk, ecn_thresh)) {
  1376. + ecn_thresh = (u64)rs->delivered * bbr_param(sk, ecn_thresh) >>
  1377. + BBR_SCALE;
  1378. + if (rs->delivered_ce > ecn_thresh) {
  1379. + return true;
  1380. + }
  1381. + }
  1382. +
  1383. + return false;
  1384. +}
  1385. +
  1386. +/* Calculate the tx_in_flight level that corresponded to excessive loss.
  1387. + * We find "lost_prefix" segs of the skb where loss rate went too high,
  1388. + * by solving for "lost_prefix" in the following equation:
  1389. + * lost / inflight >= loss_thresh
  1390. + * (lost_prev + lost_prefix) / (inflight_prev + lost_prefix) >= loss_thresh
  1391. + * Then we take that equation, convert it to fixed point, and
  1392. + * round up to the nearest packet.
  1393. + */
  1394. +static u32 bbr_inflight_hi_from_lost_skb(const struct sock *sk,
  1395. + const struct rate_sample *rs,
  1396. + const struct sk_buff *skb)
  1397. +{
  1398. + const struct tcp_sock *tp = tcp_sk(sk);
  1399. + u32 loss_thresh = bbr_param(sk, loss_thresh);
  1400. + u32 pcount, divisor, inflight_hi;
  1401. + s32 inflight_prev, lost_prev;
  1402. + u64 loss_budget, lost_prefix;
  1403. +
  1404. + pcount = tcp_skb_pcount(skb);
  1405. +
  1406. + /* How much data was in flight before this skb? */
  1407. + inflight_prev = rs->tx_in_flight - pcount;
  1408. + if (inflight_prev < 0) {
  1409. + WARN_ONCE(tcp_skb_tx_in_flight_is_suspicious(
  1410. + pcount,
  1411. + TCP_SKB_CB(skb)->sacked,
  1412. + rs->tx_in_flight),
  1413. + "tx_in_flight: %u pcount: %u reneg: %u",
  1414. + rs->tx_in_flight, pcount, tcp_sk(sk)->is_sack_reneg);
  1415. + return ~0U;
  1416. + }
  1417. +
  1418. + /* How much inflight data was marked lost before this skb? */
  1419. + lost_prev = rs->lost - pcount;
  1420. + if (WARN_ONCE(lost_prev < 0,
  1421. + "cwnd: %u ca: %d out: %u lost: %u pif: %u "
  1422. + "tx_in_flight: %u tx.lost: %u tp->lost: %u rs->lost: %d "
  1423. + "lost_prev: %d pcount: %d seq: %u end_seq: %u reneg: %u",
  1424. + tcp_snd_cwnd(tp), inet_csk(sk)->icsk_ca_state,
  1425. + tp->packets_out, tp->lost_out, tcp_packets_in_flight(tp),
  1426. + rs->tx_in_flight, TCP_SKB_CB(skb)->tx.lost, tp->lost,
  1427. + rs->lost, lost_prev, pcount,
  1428. + TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
  1429. + tp->is_sack_reneg))
  1430. + return ~0U;
  1431. +
  1432. + /* At what prefix of this lost skb did losss rate exceed loss_thresh? */
  1433. + loss_budget = (u64)inflight_prev * loss_thresh + BBR_UNIT - 1;
  1434. + loss_budget >>= BBR_SCALE;
  1435. + if (lost_prev >= loss_budget) {
  1436. + lost_prefix = 0; /* previous losses crossed loss_thresh */
  1437. + } else {
  1438. + lost_prefix = loss_budget - lost_prev;
  1439. + lost_prefix <<= BBR_SCALE;
  1440. + divisor = BBR_UNIT - loss_thresh;
  1441. + if (WARN_ON_ONCE(!divisor)) /* loss_thresh is 8 bits */
  1442. + return ~0U;
  1443. + do_div(lost_prefix, divisor);
  1444. + }
  1445. +
  1446. + inflight_hi = inflight_prev + lost_prefix;
  1447. + return inflight_hi;
  1448. +}
  1449. +
  1450. +/* If loss/ECN rates during probing indicated we may have overfilled a
  1451. + * buffer, return an operating point that tries to leave unutilized headroom in
  1452. + * the path for other flows, for fairness convergence and lower RTTs and loss.
  1453. + */
  1454. +static u32 bbr_inflight_with_headroom(const struct sock *sk)
  1455. +{
  1456. + struct bbr *bbr = inet_csk_ca(sk);
  1457. + u32 headroom, headroom_fraction;
  1458. +
  1459. + if (bbr->inflight_hi == ~0U)
  1460. + return ~0U;
  1461. +
  1462. + headroom_fraction = bbr_param(sk, inflight_headroom);
  1463. + headroom = ((u64)bbr->inflight_hi * headroom_fraction) >> BBR_SCALE;
  1464. + headroom = max(headroom, 1U);
  1465. + return max_t(s32, bbr->inflight_hi - headroom,
  1466. + bbr_param(sk, cwnd_min_target));
  1467. +}
  1468. +
  1469. +/* Bound cwnd to a sensible level, based on our current probing state
  1470. + * machine phase and model of a good inflight level (inflight_lo, inflight_hi).
  1471. + */
  1472. +static void bbr_bound_cwnd_for_inflight_model(struct sock *sk)
  1473. +{
  1474. + struct tcp_sock *tp = tcp_sk(sk);
  1475. + struct bbr *bbr = inet_csk_ca(sk);
  1476. + u32 cap;
  1477. +
  1478. + /* tcp_rcv_synsent_state_process() currently calls tcp_ack()
  1479. + * and thus cong_control() without first initializing us(!).
  1480. + */
  1481. + if (!bbr->initialized)
  1482. + return;
  1483. +
  1484. + cap = ~0U;
  1485. + if (bbr->mode == BBR_PROBE_BW &&
  1486. + bbr->cycle_idx != BBR_BW_PROBE_CRUISE) {
  1487. + /* Probe to see if more packets fit in the path. */
  1488. + cap = bbr->inflight_hi;
  1489. + } else {
  1490. + if (bbr->mode == BBR_PROBE_RTT ||
  1491. + (bbr->mode == BBR_PROBE_BW &&
  1492. + bbr->cycle_idx == BBR_BW_PROBE_CRUISE))
  1493. + cap = bbr_inflight_with_headroom(sk);
  1494. + }
  1495. + /* Adapt to any loss/ECN since our last bw probe. */
  1496. + cap = min(cap, bbr->inflight_lo);
  1497. +
  1498. + cap = max_t(u32, cap, bbr_param(sk, cwnd_min_target));
  1499. + tcp_snd_cwnd_set(tp, min(cap, tcp_snd_cwnd(tp)));
  1500. +}
  1501. +
  1502. +/* How should we multiplicatively cut bw or inflight limits based on ECN? */
  1503. +static u32 bbr_ecn_cut(struct sock *sk)
  1504. +{
  1505. + struct bbr *bbr = inet_csk_ca(sk);
  1506. +
  1507. + return BBR_UNIT -
  1508. + ((bbr->ecn_alpha * bbr_param(sk, ecn_factor)) >> BBR_SCALE);
  1509. +}
  1510. +
  1511. +/* Init lower bounds if have not inited yet. */
  1512. +static void bbr_init_lower_bounds(struct sock *sk, bool init_bw)
  1513. +{
  1514. + struct tcp_sock *tp = tcp_sk(sk);
  1515. + struct bbr *bbr = inet_csk_ca(sk);
  1516. +
  1517. + if (init_bw && bbr->bw_lo == ~0U)
  1518. + bbr->bw_lo = bbr_max_bw(sk);
  1519. + if (bbr->inflight_lo == ~0U)
  1520. + bbr->inflight_lo = tcp_snd_cwnd(tp);
  1521. +}
  1522. +
  1523. +/* Reduce bw and inflight to (1 - beta). */
  1524. +static void bbr_loss_lower_bounds(struct sock *sk, u32 *bw, u32 *inflight)
  1525. +{
  1526. + struct bbr* bbr = inet_csk_ca(sk);
  1527. + u32 loss_cut = BBR_UNIT - bbr_param(sk, beta);
  1528. +
  1529. + *bw = max_t(u32, bbr->bw_latest,
  1530. + (u64)bbr->bw_lo * loss_cut >> BBR_SCALE);
  1531. + *inflight = max_t(u32, bbr->inflight_latest,
  1532. + (u64)bbr->inflight_lo * loss_cut >> BBR_SCALE);
  1533. +}
  1534. +
  1535. +/* Reduce inflight to (1 - alpha*ecn_factor). */
  1536. +static void bbr_ecn_lower_bounds(struct sock *sk, u32 *inflight)
  1537. +{
  1538. + struct bbr *bbr = inet_csk_ca(sk);
  1539. + u32 ecn_cut = bbr_ecn_cut(sk);
  1540. +
  1541. + *inflight = (u64)bbr->inflight_lo * ecn_cut >> BBR_SCALE;
  1542. +}
  1543. +
  1544. +/* Estimate a short-term lower bound on the capacity available now, based
  1545. + * on measurements of the current delivery process and recent history. When we
  1546. + * are seeing loss/ECN at times when we are not probing bw, then conservatively
  1547. + * move toward flow balance by multiplicatively cutting our short-term
  1548. + * estimated safe rate and volume of data (bw_lo and inflight_lo). We use a
  1549. + * multiplicative decrease in order to converge to a lower capacity in time
  1550. + * logarithmic in the magnitude of the decrease.
  1551. + *
  1552. + * However, we do not cut our short-term estimates lower than the current rate
  1553. + * and volume of delivered data from this round trip, since from the current
  1554. + * delivery process we can estimate the measured capacity available now.
  1555. + *
  1556. + * Anything faster than that approach would knowingly risk high loss, which can
  1557. + * cause low bw for Reno/CUBIC and high loss recovery latency for
  1558. + * request/response flows using any congestion control.
  1559. + */
  1560. +static void bbr_adapt_lower_bounds(struct sock *sk,
  1561. + const struct rate_sample *rs)
  1562. +{
  1563. + struct bbr *bbr = inet_csk_ca(sk);
  1564. + u32 ecn_inflight_lo = ~0U;
  1565. +
  1566. + /* We only use lower-bound estimates when not probing bw.
  1567. + * When probing we need to push inflight higher to probe bw.
  1568. + */
  1569. + if (bbr_is_probing_bandwidth(sk))
  1570. + return;
  1571. +
  1572. + /* ECN response. */
  1573. + if (bbr->ecn_in_round && !!bbr_param(sk, ecn_factor)) {
  1574. + bbr_init_lower_bounds(sk, false);
  1575. + bbr_ecn_lower_bounds(sk, &ecn_inflight_lo);
  1576. + }
  1577. +
  1578. + /* Loss response. */
  1579. + if (bbr->loss_in_round) {
  1580. + bbr_init_lower_bounds(sk, true);
  1581. + bbr_loss_lower_bounds(sk, &bbr->bw_lo, &bbr->inflight_lo);
  1582. + }
  1583. +
  1584. + /* Adjust to the lower of the levels implied by loss/ECN. */
  1585. + bbr->inflight_lo = min(bbr->inflight_lo, ecn_inflight_lo);
  1586. + bbr->bw_lo = max(1U, bbr->bw_lo);
  1587. +}
  1588. +
  1589. +/* Reset any short-term lower-bound adaptation to congestion, so that we can
  1590. + * push our inflight up.
  1591. + */
  1592. +static void bbr_reset_lower_bounds(struct sock *sk)
  1593. +{
  1594. + struct bbr *bbr = inet_csk_ca(sk);
  1595. +
  1596. + bbr->bw_lo = ~0U;
  1597. + bbr->inflight_lo = ~0U;
  1598. +}
  1599. +
  1600. +/* After bw probing (STARTUP/PROBE_UP), reset signals before entering a state
  1601. + * machine phase where we adapt our lower bound based on congestion signals.
  1602. + */
  1603. +static void bbr_reset_congestion_signals(struct sock *sk)
  1604. +{
  1605. + struct bbr *bbr = inet_csk_ca(sk);
  1606. +
  1607. + bbr->loss_in_round = 0;
  1608. + bbr->ecn_in_round = 0;
  1609. + bbr->loss_in_cycle = 0;
  1610. + bbr->ecn_in_cycle = 0;
  1611. + bbr->bw_latest = 0;
  1612. + bbr->inflight_latest = 0;
  1613. +}
  1614. +
  1615. +static void bbr_exit_loss_recovery(struct sock *sk)
  1616. +{
  1617. + struct tcp_sock *tp = tcp_sk(sk);
  1618. + struct bbr *bbr = inet_csk_ca(sk);
  1619. +
  1620. + tcp_snd_cwnd_set(tp, max(tcp_snd_cwnd(tp), bbr->prior_cwnd));
  1621. + bbr->try_fast_path = 0; /* bound cwnd using latest model */
  1622. +}
  1623. +
  1624. +/* Update rate and volume of delivered data from latest round trip. */
  1625. +static void bbr_update_latest_delivery_signals(
  1626. + struct sock *sk, const struct rate_sample *rs, struct bbr_context *ctx)
  1627. +{
  1628. + struct tcp_sock *tp = tcp_sk(sk);
  1629. + struct bbr *bbr = inet_csk_ca(sk);
  1630. +
  1631. + bbr->loss_round_start = 0;
  1632. + if (rs->interval_us <= 0 || !rs->acked_sacked)
  1633. + return; /* Not a valid observation */
  1634. +
  1635. + bbr->bw_latest = max_t(u32, bbr->bw_latest, ctx->sample_bw);
  1636. + bbr->inflight_latest = max_t(u32, bbr->inflight_latest, rs->delivered);
  1637. +
  1638. + if (!before(rs->prior_delivered, bbr->loss_round_delivered)) {
  1639. + bbr->loss_round_delivered = tp->delivered;
  1640. + bbr->loss_round_start = 1; /* mark start of new round trip */
  1641. + }
  1642. +}
  1643. +
  1644. +/* Once per round, reset filter for latest rate and volume of delivered data. */
  1645. +static void bbr_advance_latest_delivery_signals(
  1646. + struct sock *sk, const struct rate_sample *rs, struct bbr_context *ctx)
  1647. +{
  1648. + struct bbr *bbr = inet_csk_ca(sk);
  1649. +
  1650. + /* If ACK matches a TLP retransmit, persist the filter. If we detect
  1651. + * that a TLP retransmit plugged a tail loss, we'll want to remember
  1652. + * how much data the path delivered before the tail loss.
  1653. + */
  1654. + if (bbr->loss_round_start && !rs->is_acking_tlp_retrans_seq) {
  1655. + bbr->bw_latest = ctx->sample_bw;
  1656. + bbr->inflight_latest = rs->delivered;
  1657. + }
  1658. +}
  1659. +
  1660. +/* Update (most of) our congestion signals: track the recent rate and volume of
  1661. + * delivered data, presence of loss, and EWMA degree of ECN marking.
  1662. + */
  1663. +static void bbr_update_congestion_signals(
  1664. + struct sock *sk, const struct rate_sample *rs, struct bbr_context *ctx)
  1665. +{
  1666. + struct bbr *bbr = inet_csk_ca(sk);
  1667. + u64 bw;
  1668. +
  1669. + if (rs->interval_us <= 0 || !rs->acked_sacked)
  1670. + return; /* Not a valid observation */
  1671. + bw = ctx->sample_bw;
  1672. +
  1673. + if (!rs->is_app_limited || bw >= bbr_max_bw(sk))
  1674. + bbr_take_max_bw_sample(sk, bw);
  1675. +
  1676. + bbr->loss_in_round |= (rs->losses > 0);
  1677. +
  1678. + if (!bbr->loss_round_start)
  1679. + return; /* skip the per-round-trip updates */
  1680. + /* Now do per-round-trip updates. */
  1681. + bbr_adapt_lower_bounds(sk, rs);
  1682. +
  1683. + bbr->loss_in_round = 0;
  1684. + bbr->ecn_in_round = 0;
  1685. +}
  1686. +
  1687. +/* Bandwidth probing can cause loss. To help coexistence with loss-based
  1688. + * congestion control we spread out our probing in a Reno-conscious way. Due to
  1689. + * the shape of the Reno sawtooth, the time required between loss epochs for an
  1690. + * idealized Reno flow is a number of round trips that is the BDP of that
  1691. + * flow. We count packet-timed round trips directly, since measured RTT can
  1692. + * vary widely, and Reno is driven by packet-timed round trips.
  1693. + */
  1694. +static bool bbr_is_reno_coexistence_probe_time(struct sock *sk)
  1695. +{
  1696. + struct bbr *bbr = inet_csk_ca(sk);
  1697. + u32 rounds;
  1698. +
  1699. + /* Random loss can shave some small percentage off of our inflight
  1700. + * in each round. To survive this, flows need robust periodic probes.
  1701. + */
  1702. + rounds = min_t(u32, bbr_param(sk, bw_probe_max_rounds), bbr_target_inflight(sk));
  1703. + return bbr->rounds_since_probe >= rounds;
  1704. +}
  1705. +
  1706. +/* How long do we want to wait before probing for bandwidth (and risking
  1707. + * loss)? We randomize the wait, for better mixing and fairness convergence.
  1708. + *
  1709. + * We bound the Reno-coexistence inter-bw-probe time to be 62-63 round trips.
  1710. + * This is calculated to allow fairness with a 25Mbps, 30ms Reno flow,
  1711. + * (eg 4K video to a broadband user):
  1712. + * BDP = 25Mbps * .030sec /(1514bytes) = 61.9 packets
  1713. + *
  1714. + * We bound the BBR-native inter-bw-probe wall clock time to be:
  1715. + * (a) higher than 2 sec: to try to avoid causing loss for a long enough time
  1716. + * to allow Reno at 30ms to get 4K video bw, the inter-bw-probe time must
  1717. + * be at least: 25Mbps * .030sec / (1514bytes) * 0.030sec = 1.9secs
  1718. + * (b) lower than 3 sec: to ensure flows can start probing in a reasonable
  1719. + * amount of time to discover unutilized bw on human-scale interactive
  1720. + * time-scales (e.g. perhaps traffic from a web page download that we
  1721. + * were competing with is now complete).
  1722. + */
  1723. +static void bbr_pick_probe_wait(struct sock *sk)
  1724. +{
  1725. + struct bbr *bbr = inet_csk_ca(sk);
  1726. +
  1727. + /* Decide the random round-trip bound for wait until probe: */
  1728. + bbr->rounds_since_probe =
  1729. + get_random_u32_below(bbr_param(sk, bw_probe_rand_rounds));
  1730. + /* Decide the random wall clock bound for wait until probe: */
  1731. + bbr->probe_wait_us = bbr_param(sk, bw_probe_base_us) +
  1732. + get_random_u32_below(bbr_param(sk, bw_probe_rand_us));
  1733. +}
  1734. +
  1735. +static void bbr_set_cycle_idx(struct sock *sk, int cycle_idx)
  1736. +{
  1737. + struct bbr *bbr = inet_csk_ca(sk);
  1738. +
  1739. + bbr->cycle_idx = cycle_idx;
  1740. + /* New phase, so need to update cwnd and pacing rate. */
  1741. + bbr->try_fast_path = 0;
  1742. +}
  1743. +
  1744. +/* Send at estimated bw to fill the pipe, but not queue. We need this phase
  1745. + * before PROBE_UP, because as soon as we send faster than the available bw
  1746. + * we will start building a queue, and if the buffer is shallow we can cause
  1747. + * loss. If we do not fill the pipe before we cause this loss, our bw_hi and
  1748. + * inflight_hi estimates will underestimate.
  1749. + */
  1750. +static void bbr_start_bw_probe_refill(struct sock *sk, u32 bw_probe_up_rounds)
  1751. +{
  1752. + struct tcp_sock *tp = tcp_sk(sk);
  1753. + struct bbr *bbr = inet_csk_ca(sk);
  1754. +
  1755. + bbr_reset_lower_bounds(sk);
  1756. + bbr->bw_probe_up_rounds = bw_probe_up_rounds;
  1757. + bbr->bw_probe_up_acks = 0;
  1758. + bbr->stopped_risky_probe = 0;
  1759. + bbr->ack_phase = BBR_ACKS_REFILLING;
  1760. + bbr->next_rtt_delivered = tp->delivered;
  1761. + bbr_set_cycle_idx(sk, BBR_BW_PROBE_REFILL);
  1762. +}
  1763. +
  1764. +/* Now probe max deliverable data rate and volume. */
  1765. +static void bbr_start_bw_probe_up(struct sock *sk, struct bbr_context *ctx)
  1766. +{
  1767. + struct tcp_sock *tp = tcp_sk(sk);
  1768. + struct bbr *bbr = inet_csk_ca(sk);
  1769. +
  1770. + bbr->ack_phase = BBR_ACKS_PROBE_STARTING;
  1771. + bbr->next_rtt_delivered = tp->delivered;
  1772. + bbr->cycle_mstamp = tp->tcp_mstamp;
  1773. + bbr_reset_full_bw(sk);
  1774. + bbr->full_bw = ctx->sample_bw;
  1775. + bbr_set_cycle_idx(sk, BBR_BW_PROBE_UP);
  1776. + bbr_raise_inflight_hi_slope(sk);
  1777. +}
  1778. +
  1779. +/* Start a new PROBE_BW probing cycle of some wall clock length. Pick a wall
  1780. + * clock time at which to probe beyond an inflight that we think to be
  1781. + * safe. This will knowingly risk packet loss, so we want to do this rarely, to
  1782. + * keep packet loss rates low. Also start a round-trip counter, to probe faster
  1783. + * if we estimate a Reno flow at our BDP would probe faster.
  1784. + */
  1785. +static void bbr_start_bw_probe_down(struct sock *sk)
  1786. +{
  1787. + struct tcp_sock *tp = tcp_sk(sk);
  1788. + struct bbr *bbr = inet_csk_ca(sk);
  1789. +
  1790. + bbr_reset_congestion_signals(sk);
  1791. + bbr->bw_probe_up_cnt = ~0U; /* not growing inflight_hi any more */
  1792. + bbr_pick_probe_wait(sk);
  1793. + bbr->cycle_mstamp = tp->tcp_mstamp; /* start wall clock */
  1794. + bbr->ack_phase = BBR_ACKS_PROBE_STOPPING;
  1795. + bbr->next_rtt_delivered = tp->delivered;
  1796. + bbr_set_cycle_idx(sk, BBR_BW_PROBE_DOWN);
  1797. +}
  1798. +
  1799. +/* Cruise: maintain what we estimate to be a neutral, conservative
  1800. + * operating point, without attempting to probe up for bandwidth or down for
  1801. + * RTT, and only reducing inflight in response to loss/ECN signals.
  1802. + */
  1803. +static void bbr_start_bw_probe_cruise(struct sock *sk)
  1804. +{
  1805. + struct bbr *bbr = inet_csk_ca(sk);
  1806. +
  1807. + if (bbr->inflight_lo != ~0U)
  1808. + bbr->inflight_lo = min(bbr->inflight_lo, bbr->inflight_hi);
  1809. +
  1810. + bbr_set_cycle_idx(sk, BBR_BW_PROBE_CRUISE);
  1811. +}
  1812. +
  1813. +/* Loss and/or ECN rate is too high while probing.
  1814. + * Adapt (once per bw probe) by cutting inflight_hi and then restarting cycle.
  1815. + */
  1816. +static void bbr_handle_inflight_too_high(struct sock *sk,
  1817. + const struct rate_sample *rs)
  1818. +{
  1819. + struct bbr *bbr = inet_csk_ca(sk);
  1820. + const u32 beta = bbr_param(sk, beta);
  1821. +
  1822. + bbr->prev_probe_too_high = 1;
  1823. + bbr->bw_probe_samples = 0; /* only react once per probe */
  1824. + /* If we are app-limited then we are not robustly
  1825. + * probing the max volume of inflight data we think
  1826. + * might be safe (analogous to how app-limited bw
  1827. + * samples are not known to be robustly probing bw).
  1828. + */
  1829. + if (!rs->is_app_limited) {
  1830. + bbr->inflight_hi = max_t(u32, rs->tx_in_flight,
  1831. + (u64)bbr_target_inflight(sk) *
  1832. + (BBR_UNIT - beta) >> BBR_SCALE);
  1833. + }
  1834. + if (bbr->mode == BBR_PROBE_BW && bbr->cycle_idx == BBR_BW_PROBE_UP)
  1835. + bbr_start_bw_probe_down(sk);
  1836. +}
  1837. +
  1838. +/* If we're seeing bw and loss samples reflecting our bw probing, adapt
  1839. + * using the signals we see. If loss or ECN mark rate gets too high, then adapt
  1840. + * inflight_hi downward. If we're able to push inflight higher without such
  1841. + * signals, push higher: adapt inflight_hi upward.
  1842. + */
  1843. +static bool bbr_adapt_upper_bounds(struct sock *sk,
  1844. + const struct rate_sample *rs,
  1845. + struct bbr_context *ctx)
  1846. +{
  1847. + struct bbr *bbr = inet_csk_ca(sk);
  1848. +
  1849. + /* Track when we'll see bw/loss samples resulting from our bw probes. */
  1850. + if (bbr->ack_phase == BBR_ACKS_PROBE_STARTING && bbr->round_start)
  1851. + bbr->ack_phase = BBR_ACKS_PROBE_FEEDBACK;
  1852. + if (bbr->ack_phase == BBR_ACKS_PROBE_STOPPING && bbr->round_start) {
  1853. + /* End of samples from bw probing phase. */
  1854. + bbr->bw_probe_samples = 0;
  1855. + bbr->ack_phase = BBR_ACKS_INIT;
  1856. + /* At this point in the cycle, our current bw sample is also
  1857. + * our best recent chance at finding the highest available bw
  1858. + * for this flow. So now is the best time to forget the bw
  1859. + * samples from the previous cycle, by advancing the window.
  1860. + */
  1861. + if (bbr->mode == BBR_PROBE_BW && !rs->is_app_limited)
  1862. + bbr_advance_max_bw_filter(sk);
  1863. + /* If we had an inflight_hi, then probed and pushed inflight all
  1864. + * the way up to hit that inflight_hi without seeing any
  1865. + * high loss/ECN in all the resulting ACKs from that probing,
  1866. + * then probe up again, this time letting inflight persist at
  1867. + * inflight_hi for a round trip, then accelerating beyond.
  1868. + */
  1869. + if (bbr->mode == BBR_PROBE_BW &&
  1870. + bbr->stopped_risky_probe && !bbr->prev_probe_too_high) {
  1871. + bbr_start_bw_probe_refill(sk, 0);
  1872. + return true; /* yes, decided state transition */
  1873. + }
  1874. + }
  1875. + if (bbr_is_inflight_too_high(sk, rs)) {
  1876. + if (bbr->bw_probe_samples) /* sample is from bw probing? */
  1877. + bbr_handle_inflight_too_high(sk, rs);
  1878. + } else {
  1879. + /* Loss/ECN rate is declared safe. Adjust upper bound upward. */
  1880. +
  1881. + if (bbr->inflight_hi == ~0U)
  1882. + return false; /* no excess queue signals yet */
  1883. +
  1884. + /* To be resilient to random loss, we must raise bw/inflight_hi
  1885. + * if we observe in any phase that a higher level is safe.
  1886. + */
  1887. + if (rs->tx_in_flight > bbr->inflight_hi) {
  1888. + bbr->inflight_hi = rs->tx_in_flight;
  1889. + }
  1890. +
  1891. + if (bbr->mode == BBR_PROBE_BW &&
  1892. + bbr->cycle_idx == BBR_BW_PROBE_UP)
  1893. + bbr_probe_inflight_hi_upward(sk, rs);
  1894. + }
  1895. +
  1896. + return false;
  1897. +}
  1898. +
  1899. +/* Check if it's time to probe for bandwidth now, and if so, kick it off. */
  1900. +static bool bbr_check_time_to_probe_bw(struct sock *sk,
  1901. + const struct rate_sample *rs)
  1902. +{
  1903. + struct bbr *bbr = inet_csk_ca(sk);
  1904. + u32 n;
  1905. +
  1906. + /* If we seem to be at an operating point where we are not seeing loss
  1907. + * but we are seeing ECN marks, then when the ECN marks cease we reprobe
  1908. + * quickly (in case cross-traffic has ceased and freed up bw).
  1909. + */
  1910. + if (bbr_param(sk, ecn_reprobe_gain) && bbr->ecn_eligible &&
  1911. + bbr->ecn_in_cycle && !bbr->loss_in_cycle &&
  1912. + inet_csk(sk)->icsk_ca_state == TCP_CA_Open) {
  1913. + /* Calculate n so that when bbr_raise_inflight_hi_slope()
  1914. + * computes growth_this_round as 2^n it will be roughly the
  1915. + * desired volume of data (inflight_hi*ecn_reprobe_gain).
  1916. + */
  1917. + n = ilog2((((u64)bbr->inflight_hi *
  1918. + bbr_param(sk, ecn_reprobe_gain)) >> BBR_SCALE));
  1919. + bbr_start_bw_probe_refill(sk, n);
  1920. + return true;
  1921. + }
  1922. +
  1923. + if (bbr_has_elapsed_in_phase(sk, bbr->probe_wait_us) ||
  1924. + bbr_is_reno_coexistence_probe_time(sk)) {
  1925. + bbr_start_bw_probe_refill(sk, 0);
  1926. + return true;
  1927. + }
  1928. + return false;
  1929. +}
  1930. +
  1931. +/* Is it time to transition from PROBE_DOWN to PROBE_CRUISE? */
  1932. +static bool bbr_check_time_to_cruise(struct sock *sk, u32 inflight, u32 bw)
  1933. +{
  1934. + /* Always need to pull inflight down to leave headroom in queue. */
  1935. + if (inflight > bbr_inflight_with_headroom(sk))
  1936. + return false;
  1937. +
  1938. + return inflight <= bbr_inflight(sk, bw, BBR_UNIT);
  1939. +}
  1940. +
  1941. +/* PROBE_BW state machine: cruise, refill, probe for bw, or drain? */
  1942. +static void bbr_update_cycle_phase(struct sock *sk,
  1943. + const struct rate_sample *rs,
  1944. + struct bbr_context *ctx)
  1945. +{
  1946. + struct tcp_sock *tp = tcp_sk(sk);
  1947. + struct bbr *bbr = inet_csk_ca(sk);
  1948. + bool is_bw_probe_done = false;
  1949. + u32 inflight, bw;
  1950. +
  1951. + if (!bbr_full_bw_reached(sk))
  1952. + return;
  1953. +
  1954. + /* In DRAIN, PROBE_BW, or PROBE_RTT, adjust upper bounds. */
  1955. + if (bbr_adapt_upper_bounds(sk, rs, ctx))
  1956. + return; /* already decided state transition */
  1957. +
  1958. + if (bbr->mode != BBR_PROBE_BW)
  1959. + return;
  1960. +
  1961. + inflight = bbr_packets_in_net_at_edt(sk, rs->prior_in_flight);
  1962. + bw = bbr_max_bw(sk);
  1963. +
  1964. + switch (bbr->cycle_idx) {
  1965. + /* First we spend most of our time cruising with a pacing_gain of 1.0,
  1966. + * which paces at the estimated bw, to try to fully use the pipe
  1967. + * without building queue. If we encounter loss/ECN marks, we adapt
  1968. + * by slowing down.
  1969. + */
  1970. + case BBR_BW_PROBE_CRUISE:
  1971. + if (bbr_check_time_to_probe_bw(sk, rs))
  1972. + return; /* already decided state transition */
  1973. + break;
  1974. +
  1975. + /* After cruising, when it's time to probe, we first "refill": we send
  1976. + * at the estimated bw to fill the pipe, before probing higher and
  1977. + * knowingly risking overflowing the bottleneck buffer (causing loss).
  1978. + */
  1979. + case BBR_BW_PROBE_REFILL:
  1980. + if (bbr->round_start) {
  1981. + /* After one full round trip of sending in REFILL, we
  1982. + * start to see bw samples reflecting our REFILL, which
  1983. + * may be putting too much data in flight.
  1984. + */
  1985. + bbr->bw_probe_samples = 1;
  1986. + bbr_start_bw_probe_up(sk, ctx);
  1987. + }
  1988. + break;
  1989. +
  1990. + /* After we refill the pipe, we probe by using a pacing_gain > 1.0, to
  1991. + * probe for bw. If we have not seen loss/ECN, we try to raise inflight
  1992. + * to at least pacing_gain*BDP; note that this may take more than
  1993. + * min_rtt if min_rtt is small (e.g. on a LAN).
  1994. + *
  1995. + * We terminate PROBE_UP bandwidth probing upon any of the following:
  1996. + *
  1997. + * (1) We've pushed inflight up to hit the inflight_hi target set in the
  1998. + * most recent previous bw probe phase. Thus we want to start
  1999. + * draining the queue immediately because it's very likely the most
  2000. + * recently sent packets will fill the queue and cause drops.
  2001. + * (2) If inflight_hi has not limited bandwidth growth recently, and
  2002. + * yet delivered bandwidth has not increased much recently
  2003. + * (bbr->full_bw_now).
  2004. + * (3) Loss filter says loss rate is "too high".
  2005. + * (4) ECN filter says ECN mark rate is "too high".
  2006. + *
  2007. + * (1) (2) checked here, (3) (4) checked in bbr_is_inflight_too_high()
  2008. + */
  2009. + case BBR_BW_PROBE_UP:
  2010. + if (bbr->prev_probe_too_high &&
  2011. + inflight >= bbr->inflight_hi) {
  2012. + bbr->stopped_risky_probe = 1;
  2013. + is_bw_probe_done = true;
  2014. + } else {
  2015. + if (tp->is_cwnd_limited &&
  2016. + tcp_snd_cwnd(tp) >= bbr->inflight_hi) {
  2017. + /* inflight_hi is limiting bw growth */
  2018. + bbr_reset_full_bw(sk);
  2019. + bbr->full_bw = ctx->sample_bw;
  2020. + } else if (bbr->full_bw_now) {
  2021. + /* Plateau in estimated bw. Pipe looks full. */
  2022. + is_bw_probe_done = true;
  2023. + }
  2024. + }
  2025. + if (is_bw_probe_done) {
  2026. + bbr->prev_probe_too_high = 0; /* no loss/ECN (yet) */
  2027. + bbr_start_bw_probe_down(sk); /* restart w/ down */
  2028. + }
  2029. + break;
  2030. +
  2031. + /* After probing in PROBE_UP, we have usually accumulated some data in
  2032. + * the bottleneck buffer (if bw probing didn't find more bw). We next
  2033. + * enter PROBE_DOWN to try to drain any excess data from the queue. To
  2034. + * do this, we use a pacing_gain < 1.0. We hold this pacing gain until
  2035. + * our inflight is less then that target cruising point, which is the
  2036. + * minimum of (a) the amount needed to leave headroom, and (b) the
  2037. + * estimated BDP. Once inflight falls to match the target, we estimate
  2038. + * the queue is drained; persisting would underutilize the pipe.
  2039. + */
  2040. + case BBR_BW_PROBE_DOWN:
  2041. + if (bbr_check_time_to_probe_bw(sk, rs))
  2042. + return; /* already decided state transition */
  2043. + if (bbr_check_time_to_cruise(sk, inflight, bw))
  2044. + bbr_start_bw_probe_cruise(sk);
  2045. + break;
  2046. +
  2047. + default:
  2048. + WARN_ONCE(1, "BBR invalid cycle index %u\n", bbr->cycle_idx);
  2049. + }
  2050. +}
  2051. +
  2052. +/* Exiting PROBE_RTT, so return to bandwidth probing in STARTUP or PROBE_BW. */
  2053. +static void bbr_exit_probe_rtt(struct sock *sk)
  2054. +{
  2055. + struct bbr *bbr = inet_csk_ca(sk);
  2056. +
  2057. + bbr_reset_lower_bounds(sk);
  2058. + if (bbr_full_bw_reached(sk)) {
  2059. + bbr->mode = BBR_PROBE_BW;
  2060. + /* Raising inflight after PROBE_RTT may cause loss, so reset
  2061. + * the PROBE_BW clock and schedule the next bandwidth probe for
  2062. + * a friendly and randomized future point in time.
  2063. + */
  2064. + bbr_start_bw_probe_down(sk);
  2065. + /* Since we are exiting PROBE_RTT, we know inflight is
  2066. + * below our estimated BDP, so it is reasonable to cruise.
  2067. + */
  2068. + bbr_start_bw_probe_cruise(sk);
  2069. + } else {
  2070. + bbr->mode = BBR_STARTUP;
  2071. + }
  2072. +}
  2073. +
  2074. +/* Exit STARTUP based on loss rate > 1% and loss gaps in round >= N. Wait until
  2075. + * the end of the round in recovery to get a good estimate of how many packets
  2076. + * have been lost, and how many we need to drain with a low pacing rate.
  2077. + */
  2078. +static void bbr_check_loss_too_high_in_startup(struct sock *sk,
  2079. + const struct rate_sample *rs)
  2080. +{
  2081. + struct bbr *bbr = inet_csk_ca(sk);
  2082. +
  2083. + if (bbr_full_bw_reached(sk))
  2084. + return;
  2085. +
  2086. + /* For STARTUP exit, check the loss rate at the end of each round trip
  2087. + * of Recovery episodes in STARTUP. We check the loss rate at the end
  2088. + * of the round trip to filter out noisy/low loss and have a better
  2089. + * sense of inflight (extent of loss), so we can drain more accurately.
  2090. + */
  2091. + if (rs->losses && bbr->loss_events_in_round < 0xf)
  2092. + bbr->loss_events_in_round++; /* update saturating counter */
  2093. + if (bbr_param(sk, full_loss_cnt) && bbr->loss_round_start &&
  2094. + inet_csk(sk)->icsk_ca_state == TCP_CA_Recovery &&
  2095. + bbr->loss_events_in_round >= bbr_param(sk, full_loss_cnt) &&
  2096. + bbr_is_inflight_too_high(sk, rs)) {
  2097. + bbr_handle_queue_too_high_in_startup(sk);
  2098. + return;
  2099. + }
  2100. + if (bbr->loss_round_start)
  2101. + bbr->loss_events_in_round = 0;
  2102. +}
  2103. +
  2104. +/* Estimate when the pipe is full, using the change in delivery rate: BBR
  2105. + * estimates bw probing filled the pipe if the estimated bw hasn't changed by
  2106. + * at least bbr_full_bw_thresh (25%) after bbr_full_bw_cnt (3) non-app-limited
  2107. + * rounds. Why 3 rounds: 1: rwin autotuning grows the rwin, 2: we fill the
  2108. + * higher rwin, 3: we get higher delivery rate samples. Or transient
  2109. + * cross-traffic or radio noise can go away. CUBIC Hystart shares a similar
  2110. + * design goal, but uses delay and inter-ACK spacing instead of bandwidth.
  2111. + */
  2112. +static void bbr_check_full_bw_reached(struct sock *sk,
  2113. + const struct rate_sample *rs,
  2114. + struct bbr_context *ctx)
  2115. +{
  2116. + struct bbr *bbr = inet_csk_ca(sk);
  2117. + u32 bw_thresh, full_cnt, thresh;
  2118. +
  2119. + if (bbr->full_bw_now || rs->is_app_limited)
  2120. + return;
  2121. +
  2122. + thresh = bbr_param(sk, full_bw_thresh);
  2123. + full_cnt = bbr_param(sk, full_bw_cnt);
  2124. + bw_thresh = (u64)bbr->full_bw * thresh >> BBR_SCALE;
  2125. + if (ctx->sample_bw >= bw_thresh) {
  2126. + bbr_reset_full_bw(sk);
  2127. + bbr->full_bw = ctx->sample_bw;
  2128. + return;
  2129. + }
  2130. + if (!bbr->round_start)
  2131. + return;
  2132. + ++bbr->full_bw_cnt;
  2133. + bbr->full_bw_now = bbr->full_bw_cnt >= full_cnt;
  2134. + bbr->full_bw_reached |= bbr->full_bw_now;
  2135. +}
  2136. +
  2137. +/* If pipe is probably full, drain the queue and then enter steady-state. */
  2138. +static void bbr_check_drain(struct sock *sk, const struct rate_sample *rs,
  2139. + struct bbr_context *ctx)
  2140. +{
  2141. + struct bbr *bbr = inet_csk_ca(sk);
  2142. +
  2143. + if (bbr->mode == BBR_STARTUP && bbr_full_bw_reached(sk)) {
  2144. + bbr->mode = BBR_DRAIN; /* drain queue we created */
  2145. + /* Set ssthresh to export purely for monitoring, to signal
  2146. + * completion of initial STARTUP by setting to a non-
  2147. + * TCP_INFINITE_SSTHRESH value (ssthresh is not used by BBR).
  2148. + */
  2149. + tcp_sk(sk)->snd_ssthresh =
  2150. + bbr_inflight(sk, bbr_max_bw(sk), BBR_UNIT);
  2151. + bbr_reset_congestion_signals(sk);
  2152. + } /* fall through to check if in-flight is already small: */
  2153. + if (bbr->mode == BBR_DRAIN &&
  2154. + bbr_packets_in_net_at_edt(sk, tcp_packets_in_flight(tcp_sk(sk))) <=
  2155. + bbr_inflight(sk, bbr_max_bw(sk), BBR_UNIT)) {
  2156. + bbr->mode = BBR_PROBE_BW;
  2157. + bbr_start_bw_probe_down(sk);
  2158. + }
  2159. +}
  2160. +
  2161. +static void bbr_update_model(struct sock *sk, const struct rate_sample *rs,
  2162. + struct bbr_context *ctx)
  2163. +{
  2164. + bbr_update_congestion_signals(sk, rs, ctx);
  2165. + bbr_update_ack_aggregation(sk, rs);
  2166. + bbr_check_loss_too_high_in_startup(sk, rs);
  2167. + bbr_check_full_bw_reached(sk, rs, ctx);
  2168. + bbr_check_drain(sk, rs, ctx);
  2169. + bbr_update_cycle_phase(sk, rs, ctx);
  2170. + bbr_update_min_rtt(sk, rs);
  2171. +}
  2172. +
  2173. +/* Fast path for app-limited case.
  2174. + *
  2175. + * On each ack, we execute bbr state machine, which primarily consists of:
  2176. + * 1) update model based on new rate sample, and
  2177. + * 2) update control based on updated model or state change.
  2178. + *
  2179. + * There are certain workload/scenarios, e.g. app-limited case, where
  2180. + * either we can skip updating model or we can skip update of both model
  2181. + * as well as control. This provides signifcant softirq cpu savings for
  2182. + * processing incoming acks.
  2183. + *
  2184. + * In case of app-limited, if there is no congestion (loss/ecn) and
  2185. + * if observed bw sample is less than current estimated bw, then we can
  2186. + * skip some of the computation in bbr state processing:
  2187. + *
  2188. + * - if there is no rtt/mode/phase change: In this case, since all the
  2189. + * parameters of the network model are constant, we can skip model
  2190. + * as well control update.
  2191. + *
  2192. + * - else we can skip rest of the model update. But we still need to
  2193. + * update the control to account for the new rtt/mode/phase.
  2194. + *
  2195. + * Returns whether we can take fast path or not.
  2196. + */
  2197. +static bool bbr_run_fast_path(struct sock *sk, bool *update_model,
  2198. + const struct rate_sample *rs, struct bbr_context *ctx)
  2199. +{
  2200. + struct bbr *bbr = inet_csk_ca(sk);
  2201. + u32 prev_min_rtt_us, prev_mode;
  2202. +
  2203. + if (bbr_param(sk, fast_path) && bbr->try_fast_path &&
  2204. + rs->is_app_limited && ctx->sample_bw < bbr_max_bw(sk) &&
  2205. + !bbr->loss_in_round && !bbr->ecn_in_round ) {
  2206. + prev_mode = bbr->mode;
  2207. + prev_min_rtt_us = bbr->min_rtt_us;
  2208. + bbr_check_drain(sk, rs, ctx);
  2209. + bbr_update_cycle_phase(sk, rs, ctx);
  2210. + bbr_update_min_rtt(sk, rs);
  2211. +
  2212. + if (bbr->mode == prev_mode &&
  2213. + bbr->min_rtt_us == prev_min_rtt_us &&
  2214. + bbr->try_fast_path) {
  2215. + return true;
  2216. + }
  2217. +
  2218. + /* Skip model update, but control still needs to be updated */
  2219. + *update_model = false;
  2220. + }
  2221. + return false;
  2222. +}
  2223. +
  2224. +__bpf_kfunc static void bbr_main(struct sock *sk, u32 ack, int flag,
  2225. + const struct rate_sample *rs)
  2226. +{
  2227. + struct tcp_sock *tp = tcp_sk(sk);
  2228. + struct bbr *bbr = inet_csk_ca(sk);
  2229. + struct bbr_context ctx = { 0 };
  2230. + bool update_model = true;
  2231. + u32 bw, round_delivered;
  2232. + int ce_ratio = -1;
  2233. +
  2234. + round_delivered = bbr_update_round_start(sk, rs, &ctx);
  2235. + if (bbr->round_start) {
  2236. + bbr->rounds_since_probe =
  2237. + min_t(s32, bbr->rounds_since_probe + 1, 0xFF);
  2238. + ce_ratio = bbr_update_ecn_alpha(sk);
  2239. + }
  2240. + bbr_plb(sk, rs, ce_ratio);
  2241. +
  2242. + bbr->ecn_in_round |= (bbr->ecn_eligible && rs->is_ece);
  2243. + bbr_calculate_bw_sample(sk, rs, &ctx);
  2244. + bbr_update_latest_delivery_signals(sk, rs, &ctx);
  2245. +
  2246. + if (bbr_run_fast_path(sk, &update_model, rs, &ctx))
  2247. + goto out;
  2248. +
  2249. + if (update_model)
  2250. + bbr_update_model(sk, rs, &ctx);
  2251. +
  2252. + bbr_update_gains(sk);
  2253. + bw = bbr_bw(sk);
  2254. + bbr_set_pacing_rate(sk, bw, bbr->pacing_gain);
  2255. + bbr_set_cwnd(sk, rs, rs->acked_sacked, bw, bbr->cwnd_gain,
  2256. + tcp_snd_cwnd(tp), &ctx);
  2257. + bbr_bound_cwnd_for_inflight_model(sk);
  2258. +
  2259. +out:
  2260. + bbr_advance_latest_delivery_signals(sk, rs, &ctx);
  2261. + bbr->prev_ca_state = inet_csk(sk)->icsk_ca_state;
  2262. + bbr->loss_in_cycle |= rs->lost > 0;
  2263. + bbr->ecn_in_cycle |= rs->delivered_ce > 0;
  2264. +}
  2265. +
  2266. +__bpf_kfunc static void bbr_init(struct sock *sk)
  2267. +{
  2268. + struct tcp_sock *tp = tcp_sk(sk);
  2269. + struct bbr *bbr = inet_csk_ca(sk);
  2270. +
  2271. + bbr->initialized = 1;
  2272. +
  2273. + bbr->init_cwnd = min(0x7FU, tcp_snd_cwnd(tp));
  2274. + bbr->prior_cwnd = tp->prior_cwnd;
  2275. + tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
  2276. + bbr->next_rtt_delivered = tp->delivered;
  2277. + bbr->prev_ca_state = TCP_CA_Open;
  2278. +
  2279. + bbr->probe_rtt_done_stamp = 0;
  2280. + bbr->probe_rtt_round_done = 0;
  2281. + bbr->probe_rtt_min_us = tcp_min_rtt(tp);
  2282. + bbr->probe_rtt_min_stamp = tcp_jiffies32;
  2283. + bbr->min_rtt_us = tcp_min_rtt(tp);
  2284. + bbr->min_rtt_stamp = tcp_jiffies32;
  2285. +
  2286. + bbr->has_seen_rtt = 0;
  2287. + bbr_init_pacing_rate_from_rtt(sk);
  2288. +
  2289. + bbr->round_start = 0;
  2290. + bbr->idle_restart = 0;
  2291. + bbr->full_bw_reached = 0;
  2292. + bbr->full_bw = 0;
  2293. + bbr->full_bw_cnt = 0;
  2294. + bbr->cycle_mstamp = 0;
  2295. + bbr->cycle_idx = 0;
  2296. +
  2297. + bbr_reset_startup_mode(sk);
  2298. +
  2299. + bbr->ack_epoch_mstamp = tp->tcp_mstamp;
  2300. + bbr->ack_epoch_acked = 0;
  2301. + bbr->extra_acked_win_rtts = 0;
  2302. + bbr->extra_acked_win_idx = 0;
  2303. + bbr->extra_acked[0] = 0;
  2304. + bbr->extra_acked[1] = 0;
  2305. +
  2306. + bbr->ce_state = 0;
  2307. + bbr->prior_rcv_nxt = tp->rcv_nxt;
  2308. + bbr->try_fast_path = 0;
  2309. +
  2310. + cmpxchg(&sk->sk_pacing_status, SK_PACING_NONE, SK_PACING_NEEDED);
  2311. +
  2312. + /* Start sampling ECN mark rate after first full flight is ACKed: */
  2313. + bbr->loss_round_delivered = tp->delivered + 1;
  2314. + bbr->loss_round_start = 0;
  2315. + bbr->undo_bw_lo = 0;
  2316. + bbr->undo_inflight_lo = 0;
  2317. + bbr->undo_inflight_hi = 0;
  2318. + bbr->loss_events_in_round = 0;
  2319. + bbr->startup_ecn_rounds = 0;
  2320. + bbr_reset_congestion_signals(sk);
  2321. + bbr->bw_lo = ~0U;
  2322. + bbr->bw_hi[0] = 0;
  2323. + bbr->bw_hi[1] = 0;
  2324. + bbr->inflight_lo = ~0U;
  2325. + bbr->inflight_hi = ~0U;
  2326. + bbr_reset_full_bw(sk);
  2327. + bbr->bw_probe_up_cnt = ~0U;
  2328. + bbr->bw_probe_up_acks = 0;
  2329. + bbr->bw_probe_up_rounds = 0;
  2330. + bbr->probe_wait_us = 0;
  2331. + bbr->stopped_risky_probe = 0;
  2332. + bbr->ack_phase = BBR_ACKS_INIT;
  2333. + bbr->rounds_since_probe = 0;
  2334. + bbr->bw_probe_samples = 0;
  2335. + bbr->prev_probe_too_high = 0;
  2336. + bbr->ecn_eligible = 0;
  2337. + bbr->ecn_alpha = bbr_param(sk, ecn_alpha_init);
  2338. + bbr->alpha_last_delivered = 0;
  2339. + bbr->alpha_last_delivered_ce = 0;
  2340. + bbr->plb.pause_until = 0;
  2341. +
  2342. + tp->fast_ack_mode = bbr_fast_ack_mode ? 1 : 0;
  2343. +
  2344. + if (bbr_can_use_ecn(sk))
  2345. + tp->ecn_flags |= TCP_ECN_ECT_PERMANENT;
  2346. +}
  2347. +
  2348. +/* BBR marks the current round trip as a loss round. */
  2349. +static void bbr_note_loss(struct sock *sk)
  2350. +{
  2351. + struct tcp_sock *tp = tcp_sk(sk);
  2352. + struct bbr *bbr = inet_csk_ca(sk);
  2353. +
  2354. + /* Capture "current" data over the full round trip of loss, to
  2355. + * have a better chance of observing the full capacity of the path.
  2356. + */
  2357. + if (!bbr->loss_in_round) /* first loss in this round trip? */
  2358. + bbr->loss_round_delivered = tp->delivered; /* set round trip */
  2359. + bbr->loss_in_round = 1;
  2360. + bbr->loss_in_cycle = 1;
  2361. +}
  2362. +
  2363. +/* Core TCP stack informs us that the given skb was just marked lost. */
  2364. +__bpf_kfunc static void bbr_skb_marked_lost(struct sock *sk,
  2365. + const struct sk_buff *skb)
  2366. +{
  2367. + struct tcp_sock *tp = tcp_sk(sk);
  2368. + struct bbr *bbr = inet_csk_ca(sk);
  2369. + struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
  2370. + struct rate_sample rs = {};
  2371. +
  2372. + bbr_note_loss(sk);
  2373. +
  2374. + if (!bbr->bw_probe_samples)
  2375. + return; /* not an skb sent while probing for bandwidth */
  2376. + if (unlikely(!scb->tx.delivered_mstamp))
  2377. + return; /* skb was SACKed, reneged, marked lost; ignore it */
  2378. + /* We are probing for bandwidth. Construct a rate sample that
  2379. + * estimates what happened in the flight leading up to this lost skb,
  2380. + * then see if the loss rate went too high, and if so at which packet.
  2381. + */
  2382. + rs.tx_in_flight = scb->tx.in_flight;
  2383. + rs.lost = tp->lost - scb->tx.lost;
  2384. + rs.is_app_limited = scb->tx.is_app_limited;
  2385. + if (bbr_is_inflight_too_high(sk, &rs)) {
  2386. + rs.tx_in_flight = bbr_inflight_hi_from_lost_skb(sk, &rs, skb);
  2387. + bbr_handle_inflight_too_high(sk, &rs);
  2388. + }
  2389. +}
  2390. +
  2391. +static void bbr_run_loss_probe_recovery(struct sock *sk)
  2392. +{
  2393. + struct tcp_sock *tp = tcp_sk(sk);
  2394. + struct bbr *bbr = inet_csk_ca(sk);
  2395. + struct rate_sample rs = {0};
  2396. +
  2397. + bbr_note_loss(sk);
  2398. +
  2399. + if (!bbr->bw_probe_samples)
  2400. + return; /* not sent while probing for bandwidth */
  2401. + /* We are probing for bandwidth. Construct a rate sample that
  2402. + * estimates what happened in the flight leading up to this
  2403. + * loss, then see if the loss rate went too high.
  2404. + */
  2405. + rs.lost = 1; /* TLP probe repaired loss of a single segment */
  2406. + rs.tx_in_flight = bbr->inflight_latest + rs.lost;
  2407. + rs.is_app_limited = tp->tlp_orig_data_app_limited;
  2408. + if (bbr_is_inflight_too_high(sk, &rs))
  2409. + bbr_handle_inflight_too_high(sk, &rs);
  2410. +}
  2411. +
  2412. +/* Revert short-term model if current loss recovery event was spurious. */
  2413. +__bpf_kfunc static u32 bbr_undo_cwnd(struct sock *sk)
  2414. +{
  2415. + struct bbr *bbr = inet_csk_ca(sk);
  2416. +
  2417. + bbr_reset_full_bw(sk); /* spurious slow-down; reset full bw detector */
  2418. + bbr->loss_in_round = 0;
  2419. +
  2420. + /* Revert to cwnd and other state saved before loss episode. */
  2421. + bbr->bw_lo = max(bbr->bw_lo, bbr->undo_bw_lo);
  2422. + bbr->inflight_lo = max(bbr->inflight_lo, bbr->undo_inflight_lo);
  2423. + bbr->inflight_hi = max(bbr->inflight_hi, bbr->undo_inflight_hi);
  2424. + bbr->try_fast_path = 0; /* take slow path to set proper cwnd, pacing */
  2425. + return bbr->prior_cwnd;
  2426. +}
  2427. +
  2428. +/* Entering loss recovery, so save state for when we undo recovery. */
  2429. +__bpf_kfunc static u32 bbr_ssthresh(struct sock *sk)
  2430. +{
  2431. + struct bbr *bbr = inet_csk_ca(sk);
  2432. +
  2433. + bbr_save_cwnd(sk);
  2434. + /* For undo, save state that adapts based on loss signal. */
  2435. + bbr->undo_bw_lo = bbr->bw_lo;
  2436. + bbr->undo_inflight_lo = bbr->inflight_lo;
  2437. + bbr->undo_inflight_hi = bbr->inflight_hi;
  2438. + return tcp_sk(sk)->snd_ssthresh;
  2439. +}
  2440. +
  2441. +static enum tcp_bbr_phase bbr_get_phase(struct bbr *bbr)
  2442. +{
  2443. + switch (bbr->mode) {
  2444. + case BBR_STARTUP:
  2445. + return BBR_PHASE_STARTUP;
  2446. + case BBR_DRAIN:
  2447. + return BBR_PHASE_DRAIN;
  2448. + case BBR_PROBE_BW:
  2449. + break;
  2450. + case BBR_PROBE_RTT:
  2451. + return BBR_PHASE_PROBE_RTT;
  2452. + default:
  2453. + return BBR_PHASE_INVALID;
  2454. + }
  2455. + switch (bbr->cycle_idx) {
  2456. + case BBR_BW_PROBE_UP:
  2457. + return BBR_PHASE_PROBE_BW_UP;
  2458. + case BBR_BW_PROBE_DOWN:
  2459. + return BBR_PHASE_PROBE_BW_DOWN;
  2460. + case BBR_BW_PROBE_CRUISE:
  2461. + return BBR_PHASE_PROBE_BW_CRUISE;
  2462. + case BBR_BW_PROBE_REFILL:
  2463. + return BBR_PHASE_PROBE_BW_REFILL;
  2464. + default:
  2465. + return BBR_PHASE_INVALID;
  2466. + }
  2467. +}
  2468. +
  2469. +static size_t bbr_get_info(struct sock *sk, u32 ext, int *attr,
  2470. + union tcp_cc_info *info)
  2471. +{
  2472. + if (ext & (1 << (INET_DIAG_BBRINFO - 1)) ||
  2473. + ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
  2474. + struct bbr *bbr = inet_csk_ca(sk);
  2475. + u64 bw = bbr_bw_bytes_per_sec(sk, bbr_bw(sk));
  2476. + u64 bw_hi = bbr_bw_bytes_per_sec(sk, bbr_max_bw(sk));
  2477. + u64 bw_lo = bbr->bw_lo == ~0U ?
  2478. + ~0ULL : bbr_bw_bytes_per_sec(sk, bbr->bw_lo);
  2479. + struct tcp_bbr_info *bbr_info = &info->bbr;
  2480. +
  2481. + memset(bbr_info, 0, sizeof(*bbr_info));
  2482. + bbr_info->bbr_bw_lo = (u32)bw;
  2483. + bbr_info->bbr_bw_hi = (u32)(bw >> 32);
  2484. + bbr_info->bbr_min_rtt = bbr->min_rtt_us;
  2485. + bbr_info->bbr_pacing_gain = bbr->pacing_gain;
  2486. + bbr_info->bbr_cwnd_gain = bbr->cwnd_gain;
  2487. + bbr_info->bbr_bw_hi_lsb = (u32)bw_hi;
  2488. + bbr_info->bbr_bw_hi_msb = (u32)(bw_hi >> 32);
  2489. + bbr_info->bbr_bw_lo_lsb = (u32)bw_lo;
  2490. + bbr_info->bbr_bw_lo_msb = (u32)(bw_lo >> 32);
  2491. + bbr_info->bbr_mode = bbr->mode;
  2492. + bbr_info->bbr_phase = (__u8)bbr_get_phase(bbr);
  2493. + bbr_info->bbr_version = (__u8)BBR_VERSION;
  2494. + bbr_info->bbr_inflight_lo = bbr->inflight_lo;
  2495. + bbr_info->bbr_inflight_hi = bbr->inflight_hi;
  2496. + bbr_info->bbr_extra_acked = bbr_extra_acked(sk);
  2497. + *attr = INET_DIAG_BBRINFO;
  2498. + return sizeof(*bbr_info);
  2499. + }
  2500. + return 0;
  2501. +}
  2502. +
  2503. +__bpf_kfunc static void bbr_set_state(struct sock *sk, u8 new_state)
  2504. +{
  2505. + struct tcp_sock *tp = tcp_sk(sk);
  2506. + struct bbr *bbr = inet_csk_ca(sk);
  2507. +
  2508. + if (new_state == TCP_CA_Loss) {
  2509. +
  2510. + bbr->prev_ca_state = TCP_CA_Loss;
  2511. + tcp_plb_update_state_upon_rto(sk, &bbr->plb);
  2512. + /* The tcp_write_timeout() call to sk_rethink_txhash() likely
  2513. + * repathed this flow, so re-learn the min network RTT on the
  2514. + * new path:
  2515. + */
  2516. + bbr_reset_full_bw(sk);
  2517. + if (!bbr_is_probing_bandwidth(sk) && bbr->inflight_lo == ~0U) {
  2518. + /* bbr_adapt_lower_bounds() needs cwnd before
  2519. + * we suffered an RTO, to update inflight_lo:
  2520. + */
  2521. + bbr->inflight_lo =
  2522. + max(tcp_snd_cwnd(tp), bbr->prior_cwnd);
  2523. + }
  2524. + } else if (bbr->prev_ca_state == TCP_CA_Loss &&
  2525. + new_state != TCP_CA_Loss) {
  2526. + bbr_exit_loss_recovery(sk);
  2527. + }
  2528. +}
  2529. +
  2530. +
  2531. +static struct tcp_congestion_ops tcp_bbrv3_cong_ops __read_mostly = {
  2532. + .flags = TCP_CONG_NON_RESTRICTED | TCP_CONG_WANTS_CE_EVENTS,
  2533. + .name = "bbrv3",
  2534. + .owner = THIS_MODULE,
  2535. + .init = bbr_init,
  2536. + .cong_control = bbr_main,
  2537. + .sndbuf_expand = bbr_sndbuf_expand,
  2538. + .skb_marked_lost = bbr_skb_marked_lost,
  2539. + .undo_cwnd = bbr_undo_cwnd,
  2540. + .cwnd_event = bbr_cwnd_event,
  2541. + .ssthresh = bbr_ssthresh,
  2542. + .tso_segs = bbr_tso_segs,
  2543. + .get_info = bbr_get_info,
  2544. + .set_state = bbr_set_state,
  2545. +};
  2546. +
  2547. +BTF_KFUNCS_START(tcp_bbrv3_check_kfunc_ids)
  2548. +BTF_ID_FLAGS(func, bbr_init)
  2549. +BTF_ID_FLAGS(func, bbr_main)
  2550. +BTF_ID_FLAGS(func, bbr_sndbuf_expand)
  2551. +BTF_ID_FLAGS(func, bbr_skb_marked_lost)
  2552. +BTF_ID_FLAGS(func, bbr_undo_cwnd)
  2553. +BTF_ID_FLAGS(func, bbr_cwnd_event)
  2554. +BTF_ID_FLAGS(func, bbr_ssthresh)
  2555. +BTF_ID_FLAGS(func, bbr_tso_segs)
  2556. +BTF_ID_FLAGS(func, bbr_set_state)
  2557. +BTF_KFUNCS_END(tcp_bbrv3_check_kfunc_ids)
  2558. +
  2559. +static const struct btf_kfunc_id_set tcp_bbrv3_kfunc_set = {
  2560. + .owner = THIS_MODULE,
  2561. + .set = &tcp_bbrv3_check_kfunc_ids,
  2562. +};
  2563. +
  2564. +static int __init bbrv3_register(void)
  2565. +{
  2566. + int ret;
  2567. +
  2568. + BUILD_BUG_ON(sizeof(struct bbr) > ICSK_CA_PRIV_SIZE);
  2569. +
  2570. + ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &tcp_bbrv3_kfunc_set);
  2571. + if (ret < 0)
  2572. + return ret;
  2573. + return tcp_register_congestion_control(&tcp_bbrv3_cong_ops);
  2574. +}
  2575. +
  2576. +static void __exit bbrv3_unregister(void)
  2577. +{
  2578. + tcp_unregister_congestion_control(&tcp_bbrv3_cong_ops);
  2579. +}
  2580. +
  2581. +module_init(bbrv3_register);
  2582. +module_exit(bbrv3_unregister);
  2583. +
  2584. +MODULE_AUTHOR("Van Jacobson <vanj@google.com>");
  2585. +MODULE_AUTHOR("Neal Cardwell <ncardwell@google.com>");
  2586. +MODULE_AUTHOR("Yuchung Cheng <ycheng@google.com>");
  2587. +MODULE_AUTHOR("Soheil Hassas Yeganeh <soheil@google.com>");
  2588. +MODULE_AUTHOR("Priyaranjan Jha <priyarjha@google.com>");
  2589. +MODULE_AUTHOR("Yousuk Seung <ysseung@google.com>");
  2590. +MODULE_AUTHOR("Kevin Yang <yyd@google.com>");
  2591. +MODULE_AUTHOR("Arjun Roy <arjunroy@google.com>");
  2592. +MODULE_AUTHOR("David Morley <morleyd@google.com>");
  2593. +
  2594. +MODULE_LICENSE("Dual BSD/GPL");
  2595. +MODULE_DESCRIPTION("TCP BBRv3 (Bottleneck Bandwidth and RTT Version 3)");
  2596. +MODULE_VERSION(__stringify(BBR_VERSION));