bbrv3-android14-6.1.patch 99 KB

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