0001-net-tcp-backport-BBRv3-to-android14-5.15.patch 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460
  1. diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
  2. index 0cdd99e5f..77c523e8a 100644
  3. --- a/include/linux/netdevice.h
  4. +++ b/include/linux/netdevice.h
  5. @@ -2285,6 +2285,7 @@ struct net_device {
  6. /* for setting kernel sock attribute on TCP connection setup */
  7. #define GSO_MAX_SIZE 65536
  8. +#define GSO_LEGACY_MAX_SIZE 65536u
  9. unsigned int gso_max_size;
  10. #define GSO_MAX_SEGS 65535
  11. u16 gso_max_segs;
  12. diff --git a/include/linux/tcp.h b/include/linux/tcp.h
  13. index 6a5785f05..b46b1735a 100644
  14. --- a/include/linux/tcp.h
  15. +++ b/include/linux/tcp.h
  16. @@ -226,7 +226,13 @@ struct tcp_sock {
  17. u8 compressed_ack;
  18. u8 dup_ack_counter:2,
  19. tlp_retrans:1, /* TLP is a retransmission */
  20. +#ifndef __GENKSYMS__
  21. + fast_ack_mode:2, /* which fast ack mode ? */
  22. + tlp_orig_data_app_limited:1, /* app-limited before TLP rtx? */
  23. + unused:2;
  24. +#else
  25. unused:5;
  26. +#endif
  27. u32 chrono_start; /* Start time in jiffies of a TCP chrono */
  28. u32 chrono_stat[3]; /* Time in jiffies for chrono_stat stats */
  29. u8 chrono_type:2, /* current chronograph type */
  30. diff --git a/include/net/tcp.h b/include/net/tcp.h
  31. index f87f50479..9c3222e07 100644
  32. --- a/include/net/tcp.h
  33. +++ b/include/net/tcp.h
  34. @@ -53,6 +53,23 @@ int tcp_orphan_count_sum(void);
  35. void tcp_time_wait(struct sock *sk, int state, int timeo);
  36. +struct tcp_plb_sysctl_params {
  37. + u8 sysctl_tcp_plb_enabled;
  38. + u8 sysctl_tcp_plb_idle_rehash_rounds;
  39. + u8 sysctl_tcp_plb_rehash_rounds;
  40. + u8 sysctl_tcp_plb_suspend_rto_sec;
  41. + int sysctl_tcp_plb_cong_thresh;
  42. +};
  43. +
  44. +struct tcp_plb_net_context {
  45. + struct tcp_plb_sysctl_params params;
  46. + struct ctl_table_header *sysctl_header;
  47. +};
  48. +
  49. +extern unsigned int tcp_plb_net_id;
  50. +
  51. +struct tcp_plb_net_context *tcp_get_plb_ctx(struct net *net);
  52. +
  53. #define MAX_TCP_HEADER L1_CACHE_ALIGN(128 + MAX_HEADER)
  54. #define MAX_TCP_OPTION_SPACE 40
  55. #define TCP_MIN_SND_MSS 48
  56. @@ -364,6 +381,8 @@ static inline void tcp_dec_quickack_mode(struct sock *sk)
  57. #define TCP_ECN_QUEUE_CWR 2
  58. #define TCP_ECN_DEMAND_CWR 4
  59. #define TCP_ECN_SEEN 8
  60. +#define TCP_ECN_LOW 16
  61. +#define TCP_ECN_ECT_PERMANENT 32
  62. enum tcp_tw_status {
  63. TCP_TW_SUCCESS = 0,
  64. @@ -709,6 +728,15 @@ static inline void tcp_fast_path_check(struct sock *sk)
  65. tcp_fast_path_on(tp);
  66. }
  67. +static inline void tcp_set_ecn_low_from_dst(struct sock *sk,
  68. + const struct dst_entry *dst)
  69. +{
  70. + struct tcp_sock *tp = tcp_sk(sk);
  71. +
  72. + if (dst_feature(dst, RTAX_FEATURE_ECN_LOW))
  73. + tp->ecn_flags |= TCP_ECN_LOW;
  74. +}
  75. +
  76. /* Compute the actual rto_min value */
  77. static inline u32 tcp_rto_min(struct sock *sk)
  78. {
  79. @@ -805,6 +833,11 @@ static inline u32 tcp_stamp_us_delta(u64 t1, u64 t0)
  80. return max_t(s64, t1 - t0, 0);
  81. }
  82. +static inline u32 tcp_stamp32_us_delta(u32 t1, u32 t0)
  83. +{
  84. + return max_t(s32, t1 - t0, 0);
  85. +}
  86. +
  87. static inline u32 tcp_skb_timestamp(const struct sk_buff *skb)
  88. {
  89. return tcp_ns_to_ts(skb->skb_mstamp_ns);
  90. @@ -873,15 +906,38 @@ struct tcp_skb_cb {
  91. union {
  92. struct {
  93. /* There is space for up to 24 bytes */
  94. +#define TCPCB_IN_FLIGHT_BITS 20
  95. +#define TCPCB_IN_FLIGHT_MAX ((1U << TCPCB_IN_FLIGHT_BITS) - 1)
  96. __u32 in_flight:30,/* Bytes in flight at transmit */
  97. is_app_limited:1, /* cwnd not fully used? */
  98. unused:1;
  99. /* pkts S/ACKed so far upon tx of skb, incl retrans: */
  100. __u32 delivered;
  101. +#ifdef __GENKSYMS__
  102. /* start of send pipeline phase */
  103. u64 first_tx_mstamp;
  104. /* when we reached the "delivered" count */
  105. u64 delivered_mstamp;
  106. +#else
  107. + /* start of send pipeline phase */
  108. + union {
  109. + u64 __kabi_placeholder_f_tx;
  110. + struct {
  111. + u32 first_tx_mstamp;
  112. +#define TCPCB_DELIVERED_CE_MASK ((1U<<20) - 1)
  113. + u32 delivered_ce:20, /* packets in flight at transmit */
  114. + unused2:12;
  115. + };
  116. + };
  117. + /* when we reached the "delivered" count */
  118. + union {
  119. + u64 __kabi_placeholder_del_mst;
  120. + struct {
  121. + u32 delivered_mstamp;
  122. + u32 lost;
  123. + };
  124. + };
  125. +#endif
  126. } tx; /* only used for outgoing skbs */
  127. union {
  128. struct inet_skb_parm h4;
  129. @@ -984,6 +1040,9 @@ enum tcp_ca_event {
  130. CA_EVENT_LOSS, /* loss timeout */
  131. CA_EVENT_ECN_NO_CE, /* ECT set, but not CE marked */
  132. CA_EVENT_ECN_IS_CE, /* received CE marked IP packet */
  133. +#ifndef __GENKSYMS__
  134. + CA_EVENT_TLP_RECOVERY, /* a lost segment was repaired by TLP probe */
  135. +#endif
  136. };
  137. /* Information about inbound ACK, passed to cong_ops->in_ack_event() */
  138. @@ -1006,7 +1065,11 @@ enum tcp_ca_ack_event_flags {
  139. #define TCP_CONG_NON_RESTRICTED 0x1
  140. /* Requires ECN/ECT set on all packets */
  141. #define TCP_CONG_NEEDS_ECN 0x2
  142. -#define TCP_CONG_MASK (TCP_CONG_NON_RESTRICTED | TCP_CONG_NEEDS_ECN)
  143. +/* Wants notification of CE events (CA_EVENT_ECN_IS_CE, CA_EVENT_ECN_NO_CE). */
  144. +#define TCP_CONG_WANTS_CE_EVENTS 0x4
  145. +#define TCP_CONG_MASK (TCP_CONG_NON_RESTRICTED | \
  146. + TCP_CONG_NEEDS_ECN | \
  147. + TCP_CONG_WANTS_CE_EVENTS)
  148. union tcp_cc_info;
  149. @@ -1039,6 +1102,15 @@ struct rate_sample {
  150. bool is_app_limited; /* is sample from packet with bubble in pipe? */
  151. bool is_retrans; /* is sample from retransmission? */
  152. bool is_ack_delayed; /* is this (likely) a delayed ACK? */
  153. +#ifndef __GENKSYMS__
  154. + bool is_acking_tlp_retrans_seq; /* ACKed a TLP retransmit sequence? */
  155. + bool is_ece; /* did this ACK have ECN marked? */
  156. + u32 tx_in_flight; /* packets in flight at starting timestamp */
  157. + s32 lost; /* number of packets lost over interval */
  158. + s32 delivered_ce; /* packets delivered w/ CE mark over interval */
  159. + u32 prior_delivered_ce;/* tp->delivered_ce at "prior_mstamp" */
  160. + u32 prior_lost; /* tp->lost at "prior_mstamp" */
  161. +#endif
  162. };
  163. struct tcp_congestion_ops {
  164. @@ -1126,6 +1198,14 @@ static inline char *tcp_ca_get_name_by_key(u32 key, char *buffer)
  165. }
  166. #endif
  167. +static inline bool tcp_ca_wants_ce_events(const struct sock *sk)
  168. +{
  169. + const struct inet_connection_sock *icsk = inet_csk(sk);
  170. +
  171. + return icsk->icsk_ca_ops->flags & (TCP_CONG_NEEDS_ECN |
  172. + TCP_CONG_WANTS_CE_EVENTS);
  173. +}
  174. +
  175. static inline bool tcp_ca_needs_ecn(const struct sock *sk)
  176. {
  177. const struct inet_connection_sock *icsk = inet_csk(sk);
  178. @@ -1163,6 +1243,21 @@ static inline bool tcp_skb_sent_after(u64 t1, u64 t2, u32 seq1, u32 seq2)
  179. return t1 > t2 || (t1 == t2 && after(seq1, seq2));
  180. }
  181. +/* If a retransmit failed due to local qdisc congestion or other local issues,
  182. + * then we may have called tcp_set_skb_tso_segs() to increase the number of
  183. + * segments in the skb without increasing the tx.in_flight. In all other cases,
  184. + * the tx.in_flight should be at least as big as the pcount of the sk_buff. We
  185. + * do not have the state to know whether a retransmit failed due to local qdisc
  186. + * congestion or other local issues, so to avoid spurious warnings we consider
  187. + * that any skb marked lost may have suffered that fate.
  188. + */
  189. +static inline bool tcp_skb_tx_in_flight_is_suspicious(u32 skb_pcount,
  190. + u32 skb_sacked_flags,
  191. + u32 tx_in_flight)
  192. +{
  193. + return (skb_pcount > tx_in_flight) && !(skb_sacked_flags & TCPCB_LOST);
  194. +}
  195. +
  196. /* These functions determine how the current flow behaves in respect of SACK
  197. * handling. SACK is negotiated with the peer, and therefore it can vary
  198. * between different flows.
  199. @@ -2118,6 +2213,111 @@ extern void tcp_rack_advance(struct tcp_sock *tp, u8 sacked, u32 end_seq,
  200. extern void tcp_rack_reo_timeout(struct sock *sk);
  201. extern void tcp_rack_update_reo_wnd(struct sock *sk, struct rate_sample *rs);
  202. +/* tcp_plb.c */
  203. +
  204. +/*
  205. + * Scaling factor for fractions in PLB. For example, tcp_plb_update_state
  206. + * expects cong_ratio which represents fraction of traffic that experienced
  207. + * congestion over a single RTT. In order to avoid floating point operations,
  208. + * this fraction should be mapped to (1 << TCP_PLB_SCALE) and passed in.
  209. + */
  210. +#define TCP_PLB_SCALE 8
  211. +
  212. +/* State for PLB (Protective Load Balancing) for a single TCP connection. */
  213. +struct tcp_plb_state {
  214. + u8 consec_cong_rounds:5, /* consecutive congested rounds */
  215. + unused:3;
  216. + u32 pause_until; /* jiffies32 when PLB can resume rerouting */
  217. +};
  218. +
  219. +static inline void tcp_plb_init(const struct sock *sk,
  220. + struct tcp_plb_state *plb)
  221. +{
  222. + plb->consec_cong_rounds = 0;
  223. + plb->pause_until = 0;
  224. +}
  225. +void tcp_plb_update_state(const struct sock *sk, struct tcp_plb_state *plb,
  226. + const int cong_ratio);
  227. +void tcp_plb_check_rehash(struct sock *sk, struct tcp_plb_state *plb);
  228. +void tcp_plb_update_state_upon_rto(struct sock *sk, struct tcp_plb_state *plb);
  229. +
  230. +/* BBR3 congestion control block */
  231. +struct bbr3 {
  232. + u32 min_rtt_us; /* min RTT in min_rtt_win_sec window */
  233. + u32 min_rtt_stamp; /* timestamp of min_rtt_us */
  234. + u32 probe_rtt_done_stamp; /* end time for BBR_PROBE_RTT mode */
  235. + u32 probe_rtt_min_us; /* min RTT in probe_rtt_win_ms win */
  236. + u32 probe_rtt_min_stamp; /* timestamp of probe_rtt_min_us*/
  237. + u32 next_rtt_delivered; /* scb->tx.delivered at end of round */
  238. + u64 cycle_mstamp; /* time of this cycle phase start */
  239. + u32 mode:2, /* current bbr_mode in state machine */
  240. + prev_ca_state:3, /* CA state on previous ACK */
  241. + round_start:1, /* start of packet-timed tx->ack round? */
  242. + ce_state:1, /* If most recent data has CE bit set */
  243. + bw_probe_up_rounds:5, /* cwnd-limited rounds in PROBE_UP */
  244. + try_fast_path:1, /* can we take fast path? */
  245. + idle_restart:1, /* restarting after idle? */
  246. + probe_rtt_round_done:1, /* a BBR_PROBE_RTT round at 4 pkts? */
  247. + init_cwnd:7, /* initial cwnd */
  248. + unused_1:10;
  249. + u32 pacing_gain:10, /* current gain for setting pacing rate */
  250. + cwnd_gain:10, /* current gain for setting cwnd */
  251. + full_bw_reached:1, /* reached full bw in Startup? */
  252. + full_bw_cnt:2, /* number of rounds without large bw gains */
  253. + cycle_idx:2, /* current index in pacing_gain cycle array */
  254. + has_seen_rtt:1, /* have we seen an RTT sample yet? */
  255. + unused_2:6;
  256. + u32 prior_cwnd; /* prior cwnd upon entering loss recovery */
  257. + u32 full_bw; /* recent bw, to estimate if pipe is full */
  258. +
  259. + /* For tracking ACK aggregation: */
  260. + u64 ack_epoch_mstamp; /* start of ACK sampling epoch */
  261. + u16 extra_acked[2]; /* max excess data ACKed in epoch */
  262. + u32 ack_epoch_acked:20, /* packets (S)ACKed in sampling epoch */
  263. + extra_acked_win_rtts:5, /* age of extra_acked, in round trips */
  264. + extra_acked_win_idx:1, /* current index in extra_acked array */
  265. + /* BBR v3 state: */
  266. + full_bw_now:1, /* recently reached full bw plateau? */
  267. + startup_ecn_rounds:2, /* consecutive hi ECN STARTUP rounds */
  268. + loss_in_cycle:1, /* packet loss in this cycle? */
  269. + ecn_in_cycle:1, /* ECN in this cycle? */
  270. + unused_3:1;
  271. + u32 loss_round_delivered; /* scb->tx.delivered ending loss round */
  272. + u32 undo_bw_lo; /* bw_lo before latest losses */
  273. + u32 undo_inflight_lo; /* inflight_lo before latest losses */
  274. + u32 undo_inflight_hi; /* inflight_hi before latest losses */
  275. + u32 bw_latest; /* max delivered bw in last round trip */
  276. + u32 bw_lo; /* lower bound on sending bandwidth */
  277. + u32 bw_hi[2]; /* max recent measured bw sample */
  278. + u32 inflight_latest; /* max delivered data in last round trip */
  279. + u32 inflight_lo; /* lower bound of inflight data range */
  280. + u32 inflight_hi; /* upper bound of inflight data range */
  281. + u32 bw_probe_up_cnt; /* packets delivered per inflight_hi incr */
  282. + u32 bw_probe_up_acks; /* packets (S)ACKed since inflight_hi incr */
  283. + u32 probe_wait_us; /* PROBE_DOWN until next clock-driven probe */
  284. + u32 prior_rcv_nxt; /* tp->rcv_nxt when CE state last changed */
  285. + u32 ecn_eligible:1, /* sender can use ECN (RTT, handshake)? */
  286. + ecn_alpha:9, /* EWMA delivered_ce/delivered; 0..256 */
  287. + bw_probe_samples:1, /* rate samples reflect bw probing? */
  288. + prev_probe_too_high:1, /* did last PROBE_UP go too high? */
  289. + stopped_risky_probe:1, /* last PROBE_UP stopped due to risk? */
  290. + rounds_since_probe:8, /* packet-timed rounds since probed bw */
  291. + loss_round_start:1, /* loss_round_delivered round trip? */
  292. + loss_in_round:1, /* loss marked in this round trip? */
  293. + ecn_in_round:1, /* ECN marked in this round trip? */
  294. + ack_phase:3, /* bbr_ack_phase: meaning of ACKs */
  295. + loss_events_in_round:4,/* losses in STARTUP round */
  296. + initialized:1; /* has bbr_init() been called? */
  297. + u32 alpha_last_delivered; /* tp->delivered at alpha update */
  298. + u32 alpha_last_delivered_ce; /* tp->delivered_ce at alpha update */
  299. +
  300. + u8 unused_4; /* to preserve alignment */
  301. + struct tcp_plb_state plb;
  302. +
  303. + /* react to a specific lost skb (optional) */
  304. + void (*skb_marked_lost)(struct sock *sk, const struct sk_buff *skb);
  305. +};
  306. +
  307. /* At how many usecs into the future should the RTO fire? */
  308. static inline s64 tcp_rto_delta_us(const struct sock *sk)
  309. {
  310. diff --git a/include/uapi/linux/inet_diag.h b/include/uapi/linux/inet_diag.h
  311. index 20ee93f0f..06f979332 100644
  312. --- a/include/uapi/linux/inet_diag.h
  313. +++ b/include/uapi/linux/inet_diag.h
  314. @@ -229,6 +229,31 @@ struct tcp_bbr_info {
  315. __u32 bbr_min_rtt; /* min-filtered RTT in uSec */
  316. __u32 bbr_pacing_gain; /* pacing gain shifted left 8 bits */
  317. __u32 bbr_cwnd_gain; /* cwnd gain shifted left 8 bits */
  318. +#ifndef __GENKSYMS__
  319. + __u32 bbr_bw_hi_lsb; /* lower 32 bits of bw_hi */
  320. + __u32 bbr_bw_hi_msb; /* upper 32 bits of bw_hi */
  321. + __u32 bbr_bw_lo_lsb; /* lower 32 bits of bw_lo */
  322. + __u32 bbr_bw_lo_msb; /* upper 32 bits of bw_lo */
  323. + __u8 bbr_mode; /* current bbr_mode in state machine */
  324. + __u8 bbr_phase; /* current state machine phase */
  325. + __u8 unused1; /* alignment padding; not used yet */
  326. + __u8 bbr_version; /* BBR algorithm version */
  327. + __u32 bbr_inflight_lo; /* lower short-term data volume bound */
  328. + __u32 bbr_inflight_hi; /* higher long-term data volume bound */
  329. + __u32 bbr_extra_acked; /* max excess packets ACKed in epoch */
  330. +#endif
  331. +};
  332. +
  333. +/* TCP BBR congestion control bbr_phase as reported in netlink/ss stats. */
  334. +enum tcp_bbr_phase {
  335. + BBR_PHASE_INVALID = 0,
  336. + BBR_PHASE_STARTUP = 1,
  337. + BBR_PHASE_DRAIN = 2,
  338. + BBR_PHASE_PROBE_RTT = 3,
  339. + BBR_PHASE_PROBE_BW_UP = 4,
  340. + BBR_PHASE_PROBE_BW_DOWN = 5,
  341. + BBR_PHASE_PROBE_BW_CRUISE = 6,
  342. + BBR_PHASE_PROBE_BW_REFILL = 7,
  343. };
  344. union tcp_cc_info {
  345. diff --git a/include/uapi/linux/rtnetlink.h b/include/uapi/linux/rtnetlink.h
  346. index e5c15614e..cdd84d54c 100644
  347. --- a/include/uapi/linux/rtnetlink.h
  348. +++ b/include/uapi/linux/rtnetlink.h
  349. @@ -497,9 +497,11 @@ enum {
  350. #define RTAX_FEATURE_SACK (1 << 1)
  351. #define RTAX_FEATURE_TIMESTAMP (1 << 2)
  352. #define RTAX_FEATURE_ALLFRAG (1 << 3)
  353. +#define RTAX_FEATURE_TCP_USEC_TS (1 << 4)
  354. +#define RTAX_FEATURE_ECN_LOW (1 << 5)
  355. #define RTAX_FEATURE_MASK (RTAX_FEATURE_ECN | RTAX_FEATURE_SACK | \
  356. - RTAX_FEATURE_TIMESTAMP | RTAX_FEATURE_ALLFRAG)
  357. + RTAX_FEATURE_TIMESTAMP | RTAX_FEATURE_ALLFRAG | RTAX_FEATURE_TCP_USEC_TS | RTAX_FEATURE_ECN_LOW)
  358. struct rta_session {
  359. __u8 proto;
  360. diff --git a/net/ipv4/Kconfig b/net/ipv4/Kconfig
  361. index 23b06063e..40c796b97 100644
  362. --- a/net/ipv4/Kconfig
  363. +++ b/net/ipv4/Kconfig
  364. @@ -679,6 +679,24 @@ config TCP_CONG_BBR
  365. AQM schemes that do not provide a delay signal. It requires the fq
  366. ("Fair Queue") pacing packet scheduler.
  367. +config TCP_CONG_BBR3
  368. + tristate "BBRv3 TCP"
  369. + default n
  370. + help
  371. +
  372. + BBRv3 (Bottleneck Bandwidth and RTT version 3) TCP congestion control is a
  373. + model-based congestion control algorithm that aims to maximize
  374. + network utilization, keep queues and retransmit rates low, and to be
  375. + able to coexist with Reno/CUBIC in common scenarios. It builds an
  376. + explicit model of the network path. It tolerates a targeted degree
  377. + of random packet loss and delay. It can operate over LAN, WAN,
  378. + cellular, wifi, or cable modem links, and can use shallow-threshold
  379. + ECN signals. It can coexist to some degree with flows that use
  380. + loss-based congestion control, and can operate with shallow buffers,
  381. + deep buffers, bufferbloat, policers, or AQM schemes that do not
  382. + provide a delay signal. It requires pacing, using either TCP internal
  383. + pacing or the fq ("Fair Queue") pacing packet scheduler.
  384. +
  385. choice
  386. prompt "Default TCP congestion control"
  387. default DEFAULT_CUBIC
  388. @@ -715,6 +733,9 @@ choice
  389. config DEFAULT_BBR
  390. bool "BBR" if TCP_CONG_BBR=y
  391. +
  392. + config DEFAULT_BBR3
  393. + bool "BBR3" if TCP_CONG_BBR3=y
  394. config DEFAULT_RENO
  395. bool "Reno"
  396. @@ -740,6 +761,7 @@ config DEFAULT_TCP_CONG
  397. default "dctcp" if DEFAULT_DCTCP
  398. default "cdg" if DEFAULT_CDG
  399. default "bbr" if DEFAULT_BBR
  400. + default "bbr3" if DEFAULT_BBR3
  401. default "cubic"
  402. config TCP_MD5SIG
  403. diff --git a/net/ipv4/Makefile b/net/ipv4/Makefile
  404. index d1c8d4beb..6b7a435d3 100644
  405. --- a/net/ipv4/Makefile
  406. +++ b/net/ipv4/Makefile
  407. @@ -10,7 +10,7 @@ obj-y := route.o inetpeer.o protocol.o \
  408. tcp.o tcp_input.o tcp_output.o tcp_timer.o tcp_ipv4.o \
  409. tcp_minisocks.o tcp_cong.o tcp_metrics.o tcp_fastopen.o \
  410. tcp_rate.o tcp_recovery.o tcp_ulp.o \
  411. - tcp_offload.o datagram.o raw.o udp.o udplite.o \
  412. + tcp_offload.o tcp_plb.o datagram.o raw.o udp.o udplite.o \
  413. udp_offload.o arp.o icmp.o devinet.o af_inet.o igmp.o \
  414. fib_frontend.o fib_semantics.o fib_trie.o fib_notifier.o \
  415. inet_fragment.o ping.o ip_tunnel_core.o gre_offload.o \
  416. @@ -47,6 +47,7 @@ obj-$(CONFIG_INET_TCP_DIAG) += tcp_diag.o
  417. obj-$(CONFIG_INET_UDP_DIAG) += udp_diag.o
  418. obj-$(CONFIG_INET_RAW_DIAG) += raw_diag.o
  419. obj-$(CONFIG_TCP_CONG_BBR) += tcp_bbr.o
  420. +obj-$(CONFIG_TCP_CONG_BBR3) += tcp_bbr3.o
  421. obj-$(CONFIG_TCP_CONG_BIC) += tcp_bic.o
  422. obj-$(CONFIG_TCP_CONG_CDG) += tcp_cdg.o
  423. obj-$(CONFIG_TCP_CONG_CUBIC) += tcp_cubic.o
  424. diff --git a/net/ipv4/sysctl_net_ipv4.c b/net/ipv4/sysctl_net_ipv4.c
  425. index d1ca9aa6d..c3edd6cf5 100644
  426. --- a/net/ipv4/sysctl_net_ipv4.c
  427. +++ b/net/ipv4/sysctl_net_ipv4.c
  428. @@ -28,6 +28,9 @@
  429. #include <net/ping.h>
  430. #include <net/protocol.h>
  431. #include <net/netevent.h>
  432. +#ifndef __GENKSYMS__
  433. +#include <net/netns/generic.h>
  434. +#endif
  435. static int two = 2;
  436. static int three __maybe_unused = 3;
  437. @@ -53,10 +56,125 @@ static u32 u32_max_div_HZ = UINT_MAX / HZ;
  438. static int one_day_secs = 24 * 3600;
  439. static u32 fib_multipath_hash_fields_all_mask __maybe_unused =
  440. FIB_MULTIPATH_HASH_FIELD_ALL_MASK;
  441. +static u8 tcp_plb_max_rounds = 31;
  442. +static int tcp_plb_max_cong_thresh = 256;
  443. /* obsolete */
  444. static int sysctl_tcp_low_latency __read_mostly;
  445. +unsigned int tcp_plb_net_id __read_mostly;
  446. +
  447. +struct tcp_plb_net_context *tcp_get_plb_ctx(struct net *net)
  448. +{
  449. + return net_generic(net, tcp_plb_net_id);
  450. +}
  451. +EXPORT_SYMBOL_GPL(tcp_get_plb_ctx);
  452. +
  453. +static struct ctl_table plb_ctl_table_template[] = {
  454. + {
  455. + .procname = "tcp_plb_enabled",
  456. + .maxlen = sizeof(u8),
  457. + .mode = 0644,
  458. + .proc_handler = proc_dou8vec_minmax,
  459. + .extra1 = SYSCTL_ZERO,
  460. + .extra2 = SYSCTL_ONE,
  461. + },
  462. + {
  463. + .procname = "tcp_plb_idle_rehash_rounds",
  464. + .maxlen = sizeof(u8),
  465. + .mode = 0644,
  466. + .proc_handler = proc_dou8vec_minmax,
  467. + .extra2 = &tcp_plb_max_rounds,
  468. + },
  469. + {
  470. + .procname = "tcp_plb_rehash_rounds",
  471. + .maxlen = sizeof(u8),
  472. + .mode = 0644,
  473. + .proc_handler = proc_dou8vec_minmax,
  474. + .extra2 = &tcp_plb_max_rounds,
  475. + },
  476. + {
  477. + .procname = "tcp_plb_suspend_rto_sec",
  478. + .maxlen = sizeof(u8),
  479. + .mode = 0644,
  480. + .proc_handler = proc_dou8vec_minmax,
  481. + },
  482. + {
  483. + .procname = "tcp_plb_cong_thresh",
  484. + .maxlen = sizeof(int),
  485. + .mode = 0644,
  486. + .proc_handler = proc_dointvec_minmax,
  487. + .extra1 = SYSCTL_ZERO,
  488. + .extra2 = &tcp_plb_max_cong_thresh,
  489. + },
  490. + { }
  491. +};
  492. +
  493. +static int __net_init plb_net_init(struct net *net)
  494. +{
  495. + struct tcp_plb_net_context *ctx;
  496. + struct ctl_table *table;
  497. + int i;
  498. +
  499. + ctx = tcp_get_plb_ctx(net);
  500. + if (!ctx)
  501. + return -ENOMEM;
  502. +
  503. + ctx->params.sysctl_tcp_plb_enabled = 0;
  504. + ctx->params.sysctl_tcp_plb_idle_rehash_rounds = tcp_plb_max_rounds;
  505. + ctx->params.sysctl_tcp_plb_rehash_rounds = tcp_plb_max_rounds;
  506. + ctx->params.sysctl_tcp_plb_suspend_rto_sec = 0;
  507. + ctx->params.sysctl_tcp_plb_cong_thresh = tcp_plb_max_cong_thresh;
  508. +
  509. + table = kmemdup(plb_ctl_table_template, sizeof(plb_ctl_table_template), GFP_KERNEL);
  510. + if (!table) {
  511. + return -ENOMEM;
  512. + }
  513. +
  514. + for (i = 0; table[i].procname; i++) {
  515. + if (strcmp(table[i].procname, "tcp_plb_enabled") == 0)
  516. + table[i].data = &ctx->params.sysctl_tcp_plb_enabled;
  517. + else if (strcmp(table[i].procname, "tcp_plb_idle_rehash_rounds") == 0)
  518. + table[i].data = &ctx->params.sysctl_tcp_plb_idle_rehash_rounds;
  519. + else if (strcmp(table[i].procname, "tcp_plb_rehash_rounds") == 0)
  520. + table[i].data = &ctx->params.sysctl_tcp_plb_rehash_rounds;
  521. + else if (strcmp(table[i].procname, "tcp_plb_suspend_rto_sec") == 0)
  522. + table[i].data = &ctx->params.sysctl_tcp_plb_suspend_rto_sec;
  523. + else if (strcmp(table[i].procname, "tcp_plb_cong_thresh") == 0)
  524. + table[i].data = &ctx->params.sysctl_tcp_plb_cong_thresh;
  525. + }
  526. +
  527. + ctx->sysctl_header = register_net_sysctl(net, "net/ipv4", table);
  528. + if (!ctx->sysctl_header) {
  529. + kfree(table);
  530. + return -ENOMEM;
  531. + }
  532. +
  533. + return 0;
  534. +}
  535. +
  536. +static void __net_exit plb_net_exit(struct net *net)
  537. +{
  538. + struct tcp_plb_net_context *ctx = tcp_get_plb_ctx(net);
  539. + struct ctl_table *table;
  540. +
  541. + if (!ctx)
  542. + return;
  543. +
  544. + if (ctx->sysctl_header) {
  545. + table = ctx->sysctl_header->ctl_table_arg;
  546. + unregister_net_sysctl_table(ctx->sysctl_header);
  547. + kfree(table);
  548. + }
  549. +}
  550. +
  551. +static struct pernet_operations plb_net_ops = {
  552. + .init = plb_net_init,
  553. + .exit = plb_net_exit,
  554. + .id = &tcp_plb_net_id,
  555. + .size = sizeof(struct tcp_plb_net_context),
  556. +};
  557. +
  558. /* Update system visible IP port range */
  559. static void set_local_port_range(struct net *net, int range[2])
  560. {
  561. @@ -1453,6 +1571,12 @@ static __init int sysctl_ipv4_init(void)
  562. return -ENOMEM;
  563. }
  564. + if (register_pernet_subsys(&plb_net_ops)) {
  565. + unregister_pernet_subsys(&ipv4_sysctl_ops);
  566. + unregister_net_sysctl_table(hdr);
  567. + return -ENOMEM;
  568. + }
  569. +
  570. return 0;
  571. }
  572. diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
  573. index 63b9e2999..4305219ea 100644
  574. --- a/net/ipv4/tcp.c
  575. +++ b/net/ipv4/tcp.c
  576. @@ -3108,6 +3108,7 @@ int tcp_disconnect(struct sock *sk, int flags)
  577. tp->rx_opt.dsack = 0;
  578. tp->rx_opt.num_sacks = 0;
  579. tp->rcv_ooopack = 0;
  580. + tp->fast_ack_mode = 0;
  581. /* Clean up fastopen related fields */
  582. diff --git a/net/ipv4/tcp_bbr3.c b/net/ipv4/tcp_bbr3.c
  583. new file mode 100644
  584. index 000000000..b3868c541
  585. --- /dev/null
  586. +++ b/net/ipv4/tcp_bbr3.c
  587. @@ -0,0 +1,2364 @@
  588. +/* BBR (Bottleneck Bandwidth and RTT) congestion control
  589. + *
  590. + * BBR is a model-based congestion control algorithm that aims for low queues,
  591. + * low loss, and (bounded) Reno/CUBIC coexistence. To maintain a model of the
  592. + * network path, it uses measurements of bandwidth and RTT, as well as (if they
  593. + * occur) packet loss and/or shallow-threshold ECN signals. Note that although
  594. + * it can use ECN or loss signals explicitly, it does not require either; it
  595. + * can bound its in-flight data based on its estimate of the BDP.
  596. + *
  597. + * The model has both higher and lower bounds for the operating range:
  598. + * lo: bw_lo, inflight_lo: conservative short-term lower bound
  599. + * hi: bw_hi, inflight_hi: robust long-term upper bound
  600. + * The bandwidth-probing time scale is (a) extended dynamically based on
  601. + * estimated BDP to improve coexistence with Reno/CUBIC; (b) bounded by
  602. + * an interactive wall-clock time-scale to be more scalable and responsive
  603. + * than Reno and CUBIC.
  604. + *
  605. + * Here is a state transition diagram for BBR:
  606. + *
  607. + * |
  608. + * V
  609. + * +---> STARTUP ----+
  610. + * | | |
  611. + * | V |
  612. + * | DRAIN ----+
  613. + * | | |
  614. + * | V |
  615. + * +---> PROBE_BW ----+
  616. + * | ^ | |
  617. + * | | | |
  618. + * | +----+ |
  619. + * | |
  620. + * +---- PROBE_RTT <--+
  621. + *
  622. + * A BBR flow starts in STARTUP, and ramps up its sending rate quickly.
  623. + * When it estimates the pipe is full, it enters DRAIN to drain the queue.
  624. + * In steady state a BBR flow only uses PROBE_BW and PROBE_RTT.
  625. + * A long-lived BBR flow spends the vast majority of its time remaining
  626. + * (repeatedly) in PROBE_BW, fully probing and utilizing the pipe's bandwidth
  627. + * in a fair manner, with a small, bounded queue. *If* a flow has been
  628. + * continuously sending for the entire min_rtt window, and hasn't seen an RTT
  629. + * sample that matches or decreases its min_rtt estimate for 10 seconds, then
  630. + * it briefly enters PROBE_RTT to cut inflight to a minimum value to re-probe
  631. + * the path's two-way propagation delay (min_rtt). When exiting PROBE_RTT, if
  632. + * we estimated that we reached the full bw of the pipe then we enter PROBE_BW;
  633. + * otherwise we enter STARTUP to try to fill the pipe.
  634. + *
  635. + * BBR is described in detail in:
  636. + * "BBR: Congestion-Based Congestion Control",
  637. + * Neal Cardwell, Yuchung Cheng, C. Stephen Gunn, Soheil Hassas Yeganeh,
  638. + * Van Jacobson. ACM Queue, Vol. 14 No. 5, September-October 2016.
  639. + *
  640. + * There is a public e-mail list for discussing BBR development and testing:
  641. + * https://groups.google.com/forum/#!forum/bbr-dev
  642. + *
  643. + * NOTE: BBR might be used with the fq qdisc ("man tc-fq") with pacing enabled,
  644. + * otherwise TCP stack falls back to an internal pacing using one high
  645. + * resolution timer per TCP socket and may use more resources.
  646. + */
  647. +
  648. +#include <linux/module.h>
  649. +#include <net/tcp.h>
  650. +#include <linux/inet_diag.h>
  651. +#include <linux/inet.h>
  652. +#include <linux/random.h>
  653. +#include <linux/win_minmax.h>
  654. +
  655. +#include <trace/events/tcp.h>
  656. +#include "tcp_dctcp.h"
  657. +
  658. +#define BBR_VERSION 3
  659. +
  660. +#define bbr_param(sk,name) (bbr_ ## name)
  661. +
  662. +/* Scale factor for rate in pkt/uSec unit to avoid truncation in bandwidth
  663. + * estimation. The rate unit ~= (1500 bytes / 1 usec / 2^24) ~= 715 bps.
  664. + * This handles bandwidths from 0.06pps (715bps) to 256Mpps (3Tbps) in a u32.
  665. + * Since the minimum window is >=4 packets, the lower bound isn't
  666. + * an issue. The upper bound isn't an issue with existing technologies.
  667. + */
  668. +#define BW_SCALE 24
  669. +#define BW_UNIT (1 << BW_SCALE)
  670. +
  671. +#define BBR_SCALE 8 /* scaling factor for fractions in BBR (e.g. gains) */
  672. +#define BBR_UNIT (1 << BBR_SCALE)
  673. +
  674. +/* BBR has the following modes for deciding how fast to send: */
  675. +enum bbr_mode {
  676. + BBR_STARTUP, /* ramp up sending rate rapidly to fill pipe */
  677. + BBR_DRAIN, /* drain any queue created during startup */
  678. + BBR_PROBE_BW, /* discover, share bw: pace around estimated bw */
  679. + BBR_PROBE_RTT, /* cut inflight to min to probe min_rtt */
  680. +};
  681. +
  682. +/* How does the incoming ACK stream relate to our bandwidth probing? */
  683. +enum bbr_ack_phase {
  684. + BBR_ACKS_INIT, /* not probing; not getting probe feedback */
  685. + BBR_ACKS_REFILLING, /* sending at est. bw to fill pipe */
  686. + BBR_ACKS_PROBE_STARTING, /* inflight rising to probe bw */
  687. + BBR_ACKS_PROBE_FEEDBACK, /* getting feedback from bw probing */
  688. + BBR_ACKS_PROBE_STOPPING, /* stopped probing; still getting feedback */
  689. +};
  690. +
  691. +struct bbr_context {
  692. + u32 sample_bw;
  693. +};
  694. +
  695. +/* Window length of min_rtt filter (in sec): */
  696. +static const u32 bbr_min_rtt_win_sec = 10;
  697. +/* Minimum time (in ms) spent at bbr_cwnd_min_target in BBR_PROBE_RTT mode: */
  698. +static const u32 bbr_probe_rtt_mode_ms = 200;
  699. +/* Window length of probe_rtt_min_us filter (in ms), and consequently the
  700. + * typical interval between PROBE_RTT mode entries. The default is 5000ms.
  701. + * Note that bbr_probe_rtt_win_ms must be <= bbr_min_rtt_win_sec * MSEC_PER_SEC
  702. + */
  703. +static const u32 bbr_probe_rtt_win_ms = 5000;
  704. +/* Proportion of cwnd to estimated BDP in PROBE_RTT, in units of BBR_UNIT: */
  705. +static const u32 bbr_probe_rtt_cwnd_gain = BBR_UNIT * 1 / 2;
  706. +
  707. +/* Use min_rtt to help adapt TSO burst size, with smaller min_rtt resulting
  708. + * in bigger TSO bursts. We cut the RTT-based allowance in half
  709. + * for every 2^9 usec (aka 512 us) of RTT, so that the RTT-based allowance
  710. + * is below 1500 bytes after 6 * ~500 usec = 3ms.
  711. + */
  712. +static const u32 bbr_tso_rtt_shift = 9;
  713. +
  714. +/* Pace at ~1% below estimated bw, on average, to reduce queue at bottleneck.
  715. + * In order to help drive the network toward lower queues and low latency while
  716. + * maintaining high utilization, the average pacing rate aims to be slightly
  717. + * lower than the estimated bandwidth. This is an important aspect of the
  718. + * design.
  719. + */
  720. +static const int bbr_pacing_margin_percent = 1;
  721. +
  722. +/* We use a startup_pacing_gain of 4*ln(2) because it's the smallest value
  723. + * that will allow a smoothly increasing pacing rate that will double each RTT
  724. + * and send the same number of packets per RTT that an un-paced, slow-starting
  725. + * Reno or CUBIC flow would:
  726. + */
  727. +static const int bbr_startup_pacing_gain = BBR_UNIT * 277 / 100 + 1;
  728. +/* The gain for deriving startup cwnd: */
  729. +static const int bbr_startup_cwnd_gain = BBR_UNIT * 2;
  730. +/* The pacing gain in BBR_DRAIN is calculated to typically drain
  731. + * the queue created in BBR_STARTUP in a single round:
  732. + */
  733. +static const int bbr_drain_gain = BBR_UNIT * 1000 / 2885;
  734. +/* The gain for deriving steady-state cwnd tolerates delayed/stretched ACKs: */
  735. +static const int bbr_cwnd_gain = BBR_UNIT * 2;
  736. +/* The pacing_gain values for the PROBE_BW gain cycle, to discover/share bw: */
  737. +static const int bbr_pacing_gain[] = {
  738. + BBR_UNIT * 5 / 4, /* UP: probe for more available bw */
  739. + BBR_UNIT * 91 / 100, /* DOWN: drain queue and/or yield bw */
  740. + BBR_UNIT, /* CRUISE: try to use pipe w/ some headroom */
  741. + BBR_UNIT, /* REFILL: refill pipe to estimated 100% */
  742. +};
  743. +enum bbr_pacing_gain_phase {
  744. + BBR_BW_PROBE_UP = 0, /* push up inflight to probe for bw/vol */
  745. + BBR_BW_PROBE_DOWN = 1, /* drain excess inflight from the queue */
  746. + BBR_BW_PROBE_CRUISE = 2, /* use pipe, w/ headroom in queue/pipe */
  747. + BBR_BW_PROBE_REFILL = 3, /* refill the pipe again to 100% */
  748. +};
  749. +
  750. +/* Try to keep at least this many packets in flight, if things go smoothly. For
  751. + * smooth functioning, a sliding window protocol ACKing every other packet
  752. + * needs at least 4 packets in flight:
  753. + */
  754. +static const u32 bbr_cwnd_min_target = 4;
  755. +
  756. +/* To estimate if BBR_STARTUP or BBR_BW_PROBE_UP has filled pipe... */
  757. +/* If bw has increased significantly (1.25x), there may be more bw available: */
  758. +static const u32 bbr_full_bw_thresh = BBR_UNIT * 5 / 4;
  759. +/* But after 3 rounds w/o significant bw growth, estimate pipe is full: */
  760. +static const u32 bbr_full_bw_cnt = 3;
  761. +
  762. +/* Gain factor for adding extra_acked to target cwnd: */
  763. +static const int bbr_extra_acked_gain = BBR_UNIT;
  764. +/* Window length of extra_acked window. */
  765. +static const u32 bbr_extra_acked_win_rtts = 5;
  766. +/* Max allowed val for ack_epoch_acked, after which sampling epoch is reset */
  767. +static const u32 bbr_ack_epoch_acked_reset_thresh = 1U << 20;
  768. +/* Time period for clamping cwnd increment due to ack aggregation */
  769. +static const u32 bbr_extra_acked_max_us = 100 * 1000;
  770. +
  771. +/* Flags to control BBR ECN-related behavior... */
  772. +
  773. +/* Ensure ACKs only ACK packets with consistent ECN CE status? */
  774. +static const bool bbr_precise_ece_ack = true;
  775. +
  776. +/* Max RTT (in usec) at which to use sender-side ECN logic.
  777. + * Disabled when 0 (ECN allowed at any RTT).
  778. + */
  779. +static const u32 bbr_ecn_max_rtt_us = 5000;
  780. +
  781. +/* On losses, scale down inflight and pacing rate by beta scaled by BBR_SCALE.
  782. + * No loss response when 0.
  783. + */
  784. +static const u32 bbr_beta = BBR_UNIT * 30 / 100;
  785. +
  786. +/* Gain factor for ECN mark ratio samples, scaled by BBR_SCALE (1/16 = 6.25%) */
  787. +static const u32 bbr_ecn_alpha_gain = BBR_UNIT * 1 / 16;
  788. +
  789. +/* The initial value for ecn_alpha; 1.0 allows a flow to respond quickly
  790. + * to congestion if the bottleneck is congested when the flow starts up.
  791. + */
  792. +static const u32 bbr_ecn_alpha_init = BBR_UNIT;
  793. +
  794. +/* On ECN, cut inflight_lo to (1 - ecn_factor * ecn_alpha) scaled by BBR_SCALE.
  795. + * No ECN based bounding when 0.
  796. + */
  797. +static const u32 bbr_ecn_factor = BBR_UNIT * 1 / 3; /* 1/3 = 33% */
  798. +
  799. +/* Estimate bw probing has gone too far if CE ratio exceeds this threshold.
  800. + * Scaled by BBR_SCALE. Disabled when 0.
  801. + */
  802. +static const u32 bbr_ecn_thresh = BBR_UNIT * 1 / 2; /* 1/2 = 50% */
  803. +
  804. +/* If non-zero, if in a cycle with no losses but some ECN marks, after ECN
  805. + * clears then make the first round's increment to inflight_hi the following
  806. + * fraction of inflight_hi.
  807. + */
  808. +static const u32 bbr_ecn_reprobe_gain = BBR_UNIT * 1 / 2;
  809. +
  810. +/* Estimate bw probing has gone too far if loss rate exceeds this level. */
  811. +static const u32 bbr_loss_thresh = BBR_UNIT * 2 / 100; /* 2% loss */
  812. +
  813. +/* Slow down for a packet loss recovered by TLP? */
  814. +static const bool bbr_loss_probe_recovery = true;
  815. +
  816. +/* Exit STARTUP if number of loss marking events in a Recovery round is >= N,
  817. + * and loss rate is higher than bbr_loss_thresh.
  818. + * Disabled if 0.
  819. + */
  820. +static const u32 bbr_full_loss_cnt = 6;
  821. +
  822. +/* Exit STARTUP if number of round trips with ECN mark rate above ecn_thresh
  823. + * meets this count.
  824. + */
  825. +static const u32 bbr_full_ecn_cnt = 2;
  826. +
  827. +/* Fraction of unutilized headroom to try to leave in path upon high loss. */
  828. +static const u32 bbr_inflight_headroom = BBR_UNIT * 15 / 100;
  829. +
  830. +/* How much do we increase cwnd_gain when probing for bandwidth in
  831. + * BBR_BW_PROBE_UP? This specifies the increment in units of
  832. + * BBR_UNIT/4. The default is 1, meaning 0.25.
  833. + * The min value is 0 (meaning 0.0); max is 3 (meaning 0.75).
  834. + */
  835. +static const u32 bbr_bw_probe_cwnd_gain = 1;
  836. +
  837. +/* Max number of packet-timed rounds to wait before probing for bandwidth. If
  838. + * we want to tolerate 1% random loss per round, and not have this cut our
  839. + * inflight too much, we must probe for bw periodically on roughly this scale.
  840. + * If low, limits Reno/CUBIC coexistence; if high, limits loss tolerance.
  841. + * We aim to be fair with Reno/CUBIC up to a BDP of at least:
  842. + * BDP = 25Mbps * .030sec /(1514bytes) = 61.9 packets
  843. + */
  844. +static const u32 bbr_bw_probe_max_rounds = 63;
  845. +
  846. +/* Max amount of randomness to inject in round counting for Reno-coexistence.
  847. + */
  848. +static const u32 bbr_bw_probe_rand_rounds = 2;
  849. +
  850. +/* Use BBR-native probe time scale starting at this many usec.
  851. + * We aim to be fair with Reno/CUBIC up to an inter-loss time epoch of at least:
  852. + * BDP*RTT = 25Mbps * .030sec /(1514bytes) * 0.030sec = 1.9 secs
  853. + */
  854. +static const u32 bbr_bw_probe_base_us = 2 * USEC_PER_SEC; /* 2 secs */
  855. +
  856. +/* Use BBR-native probes spread over this many usec: */
  857. +static const u32 bbr_bw_probe_rand_us = 1 * USEC_PER_SEC; /* 1 secs */
  858. +
  859. +/* Use fast path if app-limited, no loss/ECN, and target cwnd was reached? */
  860. +static const bool bbr_fast_path = true;
  861. +
  862. +/* Use fast ack mode? */
  863. +static const bool bbr_fast_ack_mode = true;
  864. +
  865. +static u32 bbr_max_bw(const struct sock *sk);
  866. +static u32 bbr_bw(const struct sock *sk);
  867. +static void bbr_exit_probe_rtt(struct sock *sk);
  868. +static void bbr_reset_congestion_signals(struct sock *sk);
  869. +static void bbr_run_loss_probe_recovery(struct sock *sk);
  870. +
  871. +static void bbr_check_probe_rtt_done(struct sock *sk);
  872. +
  873. +static void bbr3_skb_marked_lost(struct sock *sk, const struct sk_buff *skb);
  874. +
  875. +static inline struct bbr3 *bbr3_get_priv(const struct sock *sk)
  876. +{
  877. + /* Read the address value out of slot 0 of the 104-byte array space */
  878. + return *(struct bbr3 **)inet_csk_ca(sk);
  879. +}
  880. +
  881. +/* This connection can use ECN if both endpoints have signaled ECN support in
  882. + * the handshake and the per-route settings indicated this is a
  883. + * shallow-threshold ECN environment, meaning both:
  884. + * (a) ECN CE marks indicate low-latency/shallow-threshold congestion, and
  885. + * (b) TCP endpoints provide precise ACKs that only ACK data segments
  886. + * with consistent ECN CE status
  887. + */
  888. +static bool bbr_can_use_ecn(const struct sock *sk)
  889. +{
  890. + return (tcp_sk(sk)->ecn_flags & TCP_ECN_OK) &&
  891. + (tcp_sk(sk)->ecn_flags & TCP_ECN_LOW);
  892. +}
  893. +
  894. +/* Do we estimate that STARTUP filled the pipe? */
  895. +static bool bbr_full_bw_reached(const struct sock *sk)
  896. +{
  897. + const struct bbr3 *bbr = bbr3_get_priv(sk);
  898. +
  899. + return bbr->full_bw_reached;
  900. +}
  901. +
  902. +/* Return the windowed max recent bandwidth sample, in pkts/uS << BW_SCALE. */
  903. +static u32 bbr_max_bw(const struct sock *sk)
  904. +{
  905. + const struct bbr3 *bbr = bbr3_get_priv(sk);
  906. +
  907. + return max(bbr->bw_hi[0], bbr->bw_hi[1]);
  908. +}
  909. +
  910. +/* Return the estimated bandwidth of the path, in pkts/uS << BW_SCALE. */
  911. +static u32 bbr_bw(const struct sock *sk)
  912. +{
  913. + const struct bbr3 *bbr = bbr3_get_priv(sk);
  914. +
  915. + return min(bbr_max_bw(sk), bbr->bw_lo);
  916. +}
  917. +
  918. +/* Return maximum extra acked in past k-2k round trips,
  919. + * where k = bbr_extra_acked_win_rtts.
  920. + */
  921. +static u16 bbr_extra_acked(const struct sock *sk)
  922. +{
  923. + struct bbr3 *bbr = bbr3_get_priv(sk);
  924. +
  925. + return max(bbr->extra_acked[0], bbr->extra_acked[1]);
  926. +}
  927. +
  928. +/* Return rate in bytes per second, optionally with a gain.
  929. + * The order here is chosen carefully to avoid overflow of u64. This should
  930. + * work for input rates of up to 2.9Tbit/sec and gain of 2.89x.
  931. + */
  932. +static u64 bbr_rate_bytes_per_sec(struct sock *sk, u64 rate, int gain,
  933. + int margin)
  934. +{
  935. + unsigned int mss = tcp_sk(sk)->mss_cache;
  936. +
  937. + rate *= mss;
  938. + rate *= gain;
  939. + rate >>= BBR_SCALE;
  940. + rate *= USEC_PER_SEC / 100 * (100 - margin);
  941. + rate >>= BW_SCALE;
  942. + rate = max(rate, 1ULL);
  943. + return rate;
  944. +}
  945. +
  946. +static u64 bbr_bw_bytes_per_sec(struct sock *sk, u64 rate)
  947. +{
  948. + return bbr_rate_bytes_per_sec(sk, rate, BBR_UNIT, 0);
  949. +}
  950. +
  951. +/* Convert a BBR bw and gain factor to a pacing rate in bytes per second. */
  952. +static unsigned long bbr_bw_to_pacing_rate(struct sock *sk, u32 bw, int gain)
  953. +{
  954. + u64 rate = bw;
  955. +
  956. + rate = bbr_rate_bytes_per_sec(sk, rate, gain,
  957. + bbr_pacing_margin_percent);
  958. + rate = min_t(u64, rate, READ_ONCE(sk->sk_max_pacing_rate));
  959. + return rate;
  960. +}
  961. +
  962. +/* Initialize pacing rate to: startup_pacing_gain * init_cwnd / RTT. */
  963. +static void bbr_init_pacing_rate_from_rtt(struct sock *sk)
  964. +{
  965. + struct tcp_sock *tp = tcp_sk(sk);
  966. + struct bbr3 *bbr = bbr3_get_priv(sk);
  967. + u64 bw;
  968. + u32 rtt_us;
  969. +
  970. + if (tp->srtt_us) { /* any RTT sample yet? */
  971. + rtt_us = max(tp->srtt_us >> 3, 1U);
  972. + bbr->has_seen_rtt = 1;
  973. + } else { /* no RTT sample yet */
  974. + rtt_us = USEC_PER_MSEC; /* use nominal default RTT */
  975. + }
  976. + bw = (u64)tcp_snd_cwnd(tp) * BW_UNIT;
  977. + do_div(bw, rtt_us);
  978. + WRITE_ONCE(sk->sk_pacing_rate,
  979. + bbr_bw_to_pacing_rate(sk, bw,
  980. + bbr_param(sk, startup_pacing_gain)));
  981. +}
  982. +
  983. +/* Pace using current bw estimate and a gain factor. */
  984. +static void bbr_set_pacing_rate(struct sock *sk, u32 bw, int gain)
  985. +{
  986. + struct tcp_sock *tp = tcp_sk(sk);
  987. + struct bbr3 *bbr = bbr3_get_priv(sk);
  988. + unsigned long rate = bbr_bw_to_pacing_rate(sk, bw, gain);
  989. +
  990. + if (unlikely(!bbr->has_seen_rtt && tp->srtt_us))
  991. + bbr_init_pacing_rate_from_rtt(sk);
  992. + if (bbr_full_bw_reached(sk) || rate > READ_ONCE(sk->sk_pacing_rate))
  993. + WRITE_ONCE(sk->sk_pacing_rate, rate);
  994. +}
  995. +
  996. +/* Return the number of segments BBR would like in a TSO/GSO skb, given a
  997. + * particular max gso size as a constraint. TODO: make this simpler and more
  998. + * consistent by switching bbr to just call tcp_tso_autosize().
  999. + */
  1000. +static u32 bbr_tso_segs_generic(struct sock *sk, unsigned int mss_now,
  1001. + u32 gso_max_size)
  1002. +{
  1003. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1004. + u32 segs, r;
  1005. + u64 bytes;
  1006. +
  1007. + if(!bbr || !bbr->initialized)
  1008. + return 2;
  1009. +
  1010. + /* Budget a TSO/GSO burst size allowance based on bw (pacing_rate). */
  1011. + bytes = READ_ONCE(sk->sk_pacing_rate) >> READ_ONCE(sk->sk_pacing_shift);
  1012. +
  1013. + /* Budget a TSO/GSO burst size allowance based on min_rtt. For every
  1014. + * K = 2^tso_rtt_shift microseconds of min_rtt, halve the burst.
  1015. + * The min_rtt-based burst allowance is: 64 KBytes / 2^(min_rtt/K)
  1016. + */
  1017. + if (bbr_param(sk, tso_rtt_shift)) {
  1018. + r = bbr->min_rtt_us >> bbr_param(sk, tso_rtt_shift);
  1019. + if (r < BITS_PER_TYPE(u32)) /* prevent undefined behavior */
  1020. + bytes += GSO_LEGACY_MAX_SIZE >> r;
  1021. + }
  1022. +
  1023. + bytes = min_t(u32, bytes, gso_max_size - 1 - MAX_TCP_HEADER);
  1024. + segs = max_t(u32, bytes / mss_now,
  1025. + sock_net(sk)->ipv4.sysctl_tcp_min_tso_segs);
  1026. + return segs;
  1027. +}
  1028. +
  1029. +/* Custom tcp_tso_autosize() for BBR, used at transmit time to cap skb size. */
  1030. +static u32 bbr3_tso_segs(struct sock *sk)
  1031. +{
  1032. + unsigned int mss_now = tcp_current_mss(sk);
  1033. + return bbr_tso_segs_generic(sk, mss_now, sk->sk_gso_max_size);
  1034. +}
  1035. +
  1036. +/* Like bbr2_tso_segs(), using mss_cache, ignoring driver's sk_gso_max_size. */
  1037. +static u32 bbr_tso_segs_goal(struct sock *sk)
  1038. +{
  1039. + struct tcp_sock *tp = tcp_sk(sk);
  1040. +
  1041. + return bbr_tso_segs_generic(sk, tp->mss_cache, GSO_LEGACY_MAX_SIZE);
  1042. +}
  1043. +
  1044. +/* Save "last known good" cwnd so we can restore it after losses or PROBE_RTT */
  1045. +static void bbr_save_cwnd(struct sock *sk)
  1046. +{
  1047. + struct tcp_sock *tp = tcp_sk(sk);
  1048. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1049. +
  1050. + if (bbr->prev_ca_state < TCP_CA_Recovery && bbr->mode != BBR_PROBE_RTT)
  1051. + bbr->prior_cwnd = tcp_snd_cwnd(tp); /* this cwnd is good enough */
  1052. + else /* loss recovery or BBR_PROBE_RTT have temporarily cut cwnd */
  1053. + bbr->prior_cwnd = max(bbr->prior_cwnd, tcp_snd_cwnd(tp));
  1054. +}
  1055. +
  1056. +static void bbr3_cwnd_event(struct sock *sk, enum tcp_ca_event event)
  1057. +{
  1058. + struct tcp_sock *tp = tcp_sk(sk);
  1059. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1060. +
  1061. + if (event == CA_EVENT_TX_START) {
  1062. + if (!tp->app_limited)
  1063. + return;
  1064. + bbr->idle_restart = 1;
  1065. + bbr->ack_epoch_mstamp = tp->tcp_mstamp;
  1066. + bbr->ack_epoch_acked = 0;
  1067. + /* Avoid pointless buffer overflows: pace at est. bw if we don't
  1068. + * need more speed (we're restarting from idle and app-limited).
  1069. + */
  1070. + if (bbr->mode == BBR_PROBE_BW)
  1071. + bbr_set_pacing_rate(sk, bbr_bw(sk), BBR_UNIT);
  1072. + else if (bbr->mode == BBR_PROBE_RTT)
  1073. + bbr_check_probe_rtt_done(sk);
  1074. + } else if ((event == CA_EVENT_ECN_IS_CE ||
  1075. + event == CA_EVENT_ECN_NO_CE) &&
  1076. + bbr_can_use_ecn(sk) &&
  1077. + bbr_param(sk, precise_ece_ack)) {
  1078. + u32 state = bbr->ce_state;
  1079. + dctcp_ece_ack_update(sk, event, &bbr->prior_rcv_nxt, &state);
  1080. + bbr->ce_state = state;
  1081. + } else if (event == CA_EVENT_TLP_RECOVERY &&
  1082. + bbr_param(sk, loss_probe_recovery)) {
  1083. + bbr_run_loss_probe_recovery(sk);
  1084. + }
  1085. +}
  1086. +
  1087. +/* Calculate bdp based on min RTT and the estimated bottleneck bandwidth:
  1088. + *
  1089. + * bdp = ceil(bw * min_rtt * gain)
  1090. + *
  1091. + * The key factor, gain, controls the amount of queue. While a small gain
  1092. + * builds a smaller queue, it becomes more vulnerable to noise in RTT
  1093. + * measurements (e.g., delayed ACKs or other ACK compression effects). This
  1094. + * noise may cause BBR to under-estimate the rate.
  1095. + */
  1096. +static u32 bbr_bdp(struct sock *sk, u32 bw, int gain)
  1097. +{
  1098. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1099. + u32 bdp;
  1100. + u64 w;
  1101. +
  1102. + /* If we've never had a valid RTT sample, cap cwnd at the initial
  1103. + * default. This should only happen when the connection is not using TCP
  1104. + * timestamps and has retransmitted all of the SYN/SYNACK/data packets
  1105. + * ACKed so far. In this case, an RTO can cut cwnd to 1, in which
  1106. + * case we need to slow-start up toward something safe: initial cwnd.
  1107. + */
  1108. + if (unlikely(bbr->min_rtt_us == ~0U)) /* no valid RTT samples yet? */
  1109. + return bbr->init_cwnd; /* be safe: cap at initial cwnd */
  1110. +
  1111. + w = (u64)bw * bbr->min_rtt_us;
  1112. +
  1113. + /* Apply a gain to the given value, remove the BW_SCALE shift, and
  1114. + * round the value up to avoid a negative feedback loop.
  1115. + */
  1116. + bdp = (((w * gain) >> BBR_SCALE) + BW_UNIT - 1) / BW_UNIT;
  1117. +
  1118. + return bdp;
  1119. +}
  1120. +
  1121. +/* To achieve full performance in high-speed paths, we budget enough cwnd to
  1122. + * fit full-sized skbs in-flight on both end hosts to fully utilize the path:
  1123. + * - one skb in sending host Qdisc,
  1124. + * - one skb in sending host TSO/GSO engine
  1125. + * - one skb being received by receiver host LRO/GRO/delayed-ACK engine
  1126. + * Don't worry, at low rates this won't bloat cwnd because
  1127. + * in such cases tso_segs_goal is small. The minimum cwnd is 4 packets,
  1128. + * which allows 2 outstanding 2-packet sequences, to try to keep pipe
  1129. + * full even with ACK-every-other-packet delayed ACKs.
  1130. + */
  1131. +static u32 bbr_quantization_budget(struct sock *sk, u32 cwnd)
  1132. +{
  1133. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1134. + u32 tso_segs_goal;
  1135. +
  1136. + tso_segs_goal = 3 * bbr_tso_segs_goal(sk);
  1137. +
  1138. + /* Allow enough full-sized skbs in flight to utilize end systems. */
  1139. + cwnd = max_t(u32, cwnd, tso_segs_goal);
  1140. + cwnd = max_t(u32, cwnd, bbr_param(sk, cwnd_min_target));
  1141. + /* Ensure gain cycling gets inflight above BDP even for small BDPs. */
  1142. + if (bbr->mode == BBR_PROBE_BW && bbr->cycle_idx == BBR_BW_PROBE_UP)
  1143. + cwnd += 2;
  1144. +
  1145. + return cwnd;
  1146. +}
  1147. +
  1148. +/* Find inflight based on min RTT and the estimated bottleneck bandwidth. */
  1149. +static u32 bbr_inflight(struct sock *sk, u32 bw, int gain)
  1150. +{
  1151. + u32 inflight;
  1152. +
  1153. + inflight = bbr_bdp(sk, bw, gain);
  1154. + inflight = bbr_quantization_budget(sk, inflight);
  1155. +
  1156. + return inflight;
  1157. +}
  1158. +
  1159. +/* With pacing at lower layers, there's often less data "in the network" than
  1160. + * "in flight". With TSQ and departure time pacing at lower layers (e.g. fq),
  1161. + * we often have several skbs queued in the pacing layer with a pre-scheduled
  1162. + * earliest departure time (EDT). BBR adapts its pacing rate based on the
  1163. + * inflight level that it estimates has already been "baked in" by previous
  1164. + * departure time decisions. We calculate a rough estimate of the number of our
  1165. + * packets that might be in the network at the earliest departure time for the
  1166. + * next skb scheduled:
  1167. + * in_network_at_edt = inflight_at_edt - (EDT - now) * bw
  1168. + * If we're increasing inflight, then we want to know if the transmit of the
  1169. + * EDT skb will push inflight above the target, so inflight_at_edt includes
  1170. + * bbr_tso_segs_goal() from the skb departing at EDT. If decreasing inflight,
  1171. + * then estimate if inflight will sink too low just before the EDT transmit.
  1172. + */
  1173. +static u32 bbr_packets_in_net_at_edt(struct sock *sk, u32 inflight_now)
  1174. +{
  1175. + struct tcp_sock *tp = tcp_sk(sk);
  1176. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1177. + u64 now_ns, edt_ns, interval_us;
  1178. + u32 interval_delivered, inflight_at_edt;
  1179. +
  1180. + now_ns = tp->tcp_clock_cache;
  1181. + edt_ns = max(tp->tcp_wstamp_ns, now_ns);
  1182. + interval_us = div_u64(edt_ns - now_ns, NSEC_PER_USEC);
  1183. + interval_delivered = (u64)bbr_bw(sk) * interval_us >> BW_SCALE;
  1184. + inflight_at_edt = inflight_now;
  1185. + if (bbr->pacing_gain > BBR_UNIT) /* increasing inflight */
  1186. + inflight_at_edt += bbr_tso_segs_goal(sk); /* include EDT skb */
  1187. + if (interval_delivered >= inflight_at_edt)
  1188. + return 0;
  1189. + return inflight_at_edt - interval_delivered;
  1190. +}
  1191. +
  1192. +/* Find the cwnd increment based on estimate of ack aggregation */
  1193. +static u32 bbr_ack_aggregation_cwnd(struct sock *sk)
  1194. +{
  1195. + u32 max_aggr_cwnd, aggr_cwnd = 0;
  1196. +
  1197. + if (bbr_param(sk, extra_acked_gain)) {
  1198. + max_aggr_cwnd = ((u64)bbr_bw(sk) * bbr_extra_acked_max_us)
  1199. + / BW_UNIT;
  1200. + aggr_cwnd = (bbr_param(sk, extra_acked_gain) * bbr_extra_acked(sk))
  1201. + >> BBR_SCALE;
  1202. + aggr_cwnd = min(aggr_cwnd, max_aggr_cwnd);
  1203. + }
  1204. +
  1205. + return aggr_cwnd;
  1206. +}
  1207. +
  1208. +/* Returns the cwnd for PROBE_RTT mode. */
  1209. +static u32 bbr_probe_rtt_cwnd(struct sock *sk)
  1210. +{
  1211. + return max_t(u32, bbr_param(sk, cwnd_min_target),
  1212. + bbr_bdp(sk, bbr_bw(sk), bbr_param(sk, probe_rtt_cwnd_gain)));
  1213. +}
  1214. +
  1215. +/* Slow-start up toward target cwnd (if bw estimate is growing, or packet loss
  1216. + * has drawn us down below target), or snap down to target if we're above it.
  1217. + */
  1218. +static void bbr_set_cwnd(struct sock *sk, const struct rate_sample *rs,
  1219. + u32 acked, u32 bw, int gain, u32 cwnd,
  1220. + struct bbr_context *ctx)
  1221. +{
  1222. + struct tcp_sock *tp = tcp_sk(sk);
  1223. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1224. + u32 target_cwnd = 0;
  1225. +
  1226. + if (!acked)
  1227. + goto done; /* no packet fully ACKed; just apply caps */
  1228. +
  1229. + target_cwnd = bbr_bdp(sk, bw, gain);
  1230. +
  1231. + /* Increment the cwnd to account for excess ACKed data that seems
  1232. + * due to aggregation (of data and/or ACKs) visible in the ACK stream.
  1233. + */
  1234. + target_cwnd += bbr_ack_aggregation_cwnd(sk);
  1235. + target_cwnd = bbr_quantization_budget(sk, target_cwnd);
  1236. +
  1237. + /* Update cwnd and enable fast path if cwnd reaches target_cwnd. */
  1238. + bbr->try_fast_path = 0;
  1239. + if (bbr_full_bw_reached(sk)) { /* only cut cwnd if we filled the pipe */
  1240. + cwnd += acked;
  1241. + if (cwnd >= target_cwnd) {
  1242. + cwnd = target_cwnd;
  1243. + bbr->try_fast_path = 1;
  1244. + }
  1245. + } else if (cwnd < target_cwnd || cwnd < 2 * bbr->init_cwnd) {
  1246. + cwnd += acked;
  1247. + } else {
  1248. + bbr->try_fast_path = 1;
  1249. + }
  1250. +
  1251. + cwnd = max_t(u32, cwnd, bbr_param(sk, cwnd_min_target));
  1252. +done:
  1253. + tcp_snd_cwnd_set(tp, min(cwnd, tp->snd_cwnd_clamp)); /* global cap */
  1254. + if (bbr->mode == BBR_PROBE_RTT) /* drain queue, refresh min_rtt */
  1255. + tcp_snd_cwnd_set(tp, min_t(u32, tcp_snd_cwnd(tp),
  1256. + bbr_probe_rtt_cwnd(sk)));
  1257. +}
  1258. +
  1259. +static void bbr_reset_startup_mode(struct sock *sk)
  1260. +{
  1261. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1262. +
  1263. + bbr->mode = BBR_STARTUP;
  1264. +}
  1265. +
  1266. +/* See if we have reached next round trip. Upon start of the new round,
  1267. + * returns packets delivered since previous round start plus this ACK.
  1268. + */
  1269. +static u32 bbr_update_round_start(struct sock *sk,
  1270. + const struct rate_sample *rs, struct bbr_context *ctx)
  1271. +{
  1272. + struct tcp_sock *tp = tcp_sk(sk);
  1273. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1274. + u32 round_delivered = 0;
  1275. +
  1276. + bbr->round_start = 0;
  1277. +
  1278. + /* See if we've reached the next RTT */
  1279. + if (rs->interval_us > 0 &&
  1280. + !before(rs->prior_delivered, bbr->next_rtt_delivered)) {
  1281. + round_delivered = tp->delivered - bbr->next_rtt_delivered;
  1282. + bbr->next_rtt_delivered = tp->delivered;
  1283. + bbr->round_start = 1;
  1284. + }
  1285. + return round_delivered;
  1286. +}
  1287. +
  1288. +/* Calculate the bandwidth based on how fast packets are delivered */
  1289. +static void bbr_calculate_bw_sample(struct sock *sk,
  1290. + const struct rate_sample *rs, struct bbr_context *ctx)
  1291. +{
  1292. + u64 bw = 0;
  1293. +
  1294. + /* Divide delivered by the interval to find a (lower bound) bottleneck
  1295. + * bandwidth sample. Delivered is in packets and interval_us in uS and
  1296. + * ratio will be <<1 for most connections. So delivered is first scaled.
  1297. + * Round up to allow growth at low rates, even with integer division.
  1298. + */
  1299. + if (rs->interval_us > 0) {
  1300. + if (WARN_ONCE(rs->delivered < 0,
  1301. + "negative delivered: %d interval_us: %ld\n",
  1302. + rs->delivered, rs->interval_us))
  1303. + return;
  1304. +
  1305. + bw = DIV_ROUND_UP_ULL((u64)rs->delivered * BW_UNIT, rs->interval_us);
  1306. + }
  1307. +
  1308. + ctx->sample_bw = bw;
  1309. +}
  1310. +
  1311. +/* Estimates the windowed max degree of ack aggregation.
  1312. + * This is used to provision extra in-flight data to keep sending during
  1313. + * inter-ACK silences.
  1314. + *
  1315. + * Degree of ack aggregation is estimated as extra data acked beyond expected.
  1316. + *
  1317. + * max_extra_acked = "maximum recent excess data ACKed beyond max_bw * interval"
  1318. + * cwnd += max_extra_acked
  1319. + *
  1320. + * Max extra_acked is clamped by cwnd and bw * bbr_extra_acked_max_us (100 ms).
  1321. + * Max filter is an approximate sliding window of 5-10 (packet timed) round
  1322. + * trips for non-startup phase, and 1-2 round trips for startup.
  1323. + */
  1324. +static void bbr_update_ack_aggregation(struct sock *sk,
  1325. + const struct rate_sample *rs)
  1326. +{
  1327. + u32 epoch_us, expected_acked, extra_acked;
  1328. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1329. + struct tcp_sock *tp = tcp_sk(sk);
  1330. + u32 extra_acked_win_rtts_thresh = bbr_param(sk, extra_acked_win_rtts);
  1331. +
  1332. + if (!bbr_param(sk, extra_acked_gain) || rs->acked_sacked <= 0 ||
  1333. + rs->delivered < 0 || rs->interval_us <= 0)
  1334. + return;
  1335. +
  1336. + if (bbr->round_start) {
  1337. + bbr->extra_acked_win_rtts = min(0x1F,
  1338. + bbr->extra_acked_win_rtts + 1);
  1339. + if (!bbr_full_bw_reached(sk))
  1340. + extra_acked_win_rtts_thresh = 1;
  1341. + if (bbr->extra_acked_win_rtts >=
  1342. + extra_acked_win_rtts_thresh) {
  1343. + bbr->extra_acked_win_rtts = 0;
  1344. + bbr->extra_acked_win_idx = bbr->extra_acked_win_idx ?
  1345. + 0 : 1;
  1346. + bbr->extra_acked[bbr->extra_acked_win_idx] = 0;
  1347. + }
  1348. + }
  1349. +
  1350. + /* Compute how many packets we expected to be delivered over epoch. */
  1351. + epoch_us = tcp_stamp_us_delta(tp->delivered_mstamp,
  1352. + bbr->ack_epoch_mstamp);
  1353. + expected_acked = ((u64)bbr_bw(sk) * epoch_us) / BW_UNIT;
  1354. +
  1355. + /* Reset the aggregation epoch if ACK rate is below expected rate or
  1356. + * significantly large no. of ack received since epoch (potentially
  1357. + * quite old epoch).
  1358. + */
  1359. + if (bbr->ack_epoch_acked <= expected_acked ||
  1360. + (bbr->ack_epoch_acked + rs->acked_sacked >=
  1361. + bbr_ack_epoch_acked_reset_thresh)) {
  1362. + bbr->ack_epoch_acked = 0;
  1363. + bbr->ack_epoch_mstamp = tp->delivered_mstamp;
  1364. + expected_acked = 0;
  1365. + }
  1366. +
  1367. + /* Compute excess data delivered, beyond what was expected. */
  1368. + bbr->ack_epoch_acked = min_t(u32, 0xFFFFF,
  1369. + bbr->ack_epoch_acked + rs->acked_sacked);
  1370. + extra_acked = bbr->ack_epoch_acked - expected_acked;
  1371. + extra_acked = min(extra_acked, tcp_snd_cwnd(tp));
  1372. + if (extra_acked > bbr->extra_acked[bbr->extra_acked_win_idx])
  1373. + bbr->extra_acked[bbr->extra_acked_win_idx] = extra_acked;
  1374. +}
  1375. +
  1376. +static void bbr_check_probe_rtt_done(struct sock *sk)
  1377. +{
  1378. + struct tcp_sock *tp = tcp_sk(sk);
  1379. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1380. +
  1381. + if (!(bbr->probe_rtt_done_stamp &&
  1382. + after(tcp_jiffies32, bbr->probe_rtt_done_stamp)))
  1383. + return;
  1384. +
  1385. + bbr->probe_rtt_min_stamp = tcp_jiffies32; /* schedule next PROBE_RTT */
  1386. + tcp_snd_cwnd_set(tp, max(tcp_snd_cwnd(tp), bbr->prior_cwnd));
  1387. + bbr_exit_probe_rtt(sk);
  1388. +}
  1389. +
  1390. +/* The goal of PROBE_RTT mode is to have BBR flows cooperatively and
  1391. + * periodically drain the bottleneck queue, to converge to measure the true
  1392. + * min_rtt (unloaded propagation delay). This allows the flows to keep queues
  1393. + * small (reducing queuing delay and packet loss) and achieve fairness among
  1394. + * BBR flows.
  1395. + *
  1396. + * The min_rtt filter window is 10 seconds. When the min_rtt estimate expires,
  1397. + * we enter PROBE_RTT mode and cap the cwnd at bbr_cwnd_min_target=4 packets.
  1398. + * After at least bbr_probe_rtt_mode_ms=200ms and at least one packet-timed
  1399. + * round trip elapsed with that flight size <= 4, we leave PROBE_RTT mode and
  1400. + * re-enter the previous mode. BBR uses 200ms to approximately bound the
  1401. + * performance penalty of PROBE_RTT's cwnd capping to roughly 2% (200ms/10s).
  1402. + *
  1403. + * Note that flows need only pay 2% if they are busy sending over the last 10
  1404. + * seconds. Interactive applications (e.g., Web, RPCs, video chunks) often have
  1405. + * natural silences or low-rate periods within 10 seconds where the rate is low
  1406. + * enough for long enough to drain its queue in the bottleneck. We pick up
  1407. + * these min RTT measurements opportunistically with our min_rtt filter. :-)
  1408. + */
  1409. +static void bbr_update_min_rtt(struct sock *sk, const struct rate_sample *rs)
  1410. +{
  1411. + struct tcp_sock *tp = tcp_sk(sk);
  1412. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1413. + bool probe_rtt_expired, min_rtt_expired;
  1414. + u32 expire;
  1415. +
  1416. + /* Track min RTT in probe_rtt_win_ms to time next PROBE_RTT state. */
  1417. + expire = bbr->probe_rtt_min_stamp +
  1418. + msecs_to_jiffies(bbr_param(sk, probe_rtt_win_ms));
  1419. + probe_rtt_expired = after(tcp_jiffies32, expire);
  1420. + if (rs->rtt_us >= 0 &&
  1421. + (rs->rtt_us < bbr->probe_rtt_min_us ||
  1422. + (probe_rtt_expired && !rs->is_ack_delayed))) {
  1423. + bbr->probe_rtt_min_us = rs->rtt_us;
  1424. + bbr->probe_rtt_min_stamp = tcp_jiffies32;
  1425. + }
  1426. + /* Track min RTT seen in the min_rtt_win_sec filter window: */
  1427. + expire = bbr->min_rtt_stamp + bbr_param(sk, min_rtt_win_sec) * HZ;
  1428. + min_rtt_expired = after(tcp_jiffies32, expire);
  1429. + if (bbr->probe_rtt_min_us <= bbr->min_rtt_us ||
  1430. + min_rtt_expired) {
  1431. + bbr->min_rtt_us = bbr->probe_rtt_min_us;
  1432. + bbr->min_rtt_stamp = bbr->probe_rtt_min_stamp;
  1433. + }
  1434. +
  1435. + if (bbr_param(sk, probe_rtt_mode_ms) > 0 && probe_rtt_expired &&
  1436. + !bbr->idle_restart && bbr->mode != BBR_PROBE_RTT) {
  1437. + bbr->mode = BBR_PROBE_RTT; /* dip, drain queue */
  1438. + bbr_save_cwnd(sk); /* note cwnd so we can restore it */
  1439. + bbr->probe_rtt_done_stamp = 0;
  1440. + bbr->ack_phase = BBR_ACKS_PROBE_STOPPING;
  1441. + bbr->next_rtt_delivered = tp->delivered;
  1442. + }
  1443. +
  1444. + if (bbr->mode == BBR_PROBE_RTT) {
  1445. + /* Ignore low rate samples during this mode. */
  1446. + tp->app_limited =
  1447. + (tp->delivered + tcp_packets_in_flight(tp)) ? : 1;
  1448. + /* Maintain min packets in flight for max(200 ms, 1 round). */
  1449. + if (!bbr->probe_rtt_done_stamp &&
  1450. + tcp_packets_in_flight(tp) <= bbr_probe_rtt_cwnd(sk)) {
  1451. + bbr->probe_rtt_done_stamp = tcp_jiffies32 +
  1452. + msecs_to_jiffies(bbr_param(sk, probe_rtt_mode_ms));
  1453. + bbr->probe_rtt_round_done = 0;
  1454. + bbr->next_rtt_delivered = tp->delivered;
  1455. + } else if (bbr->probe_rtt_done_stamp) {
  1456. + if (bbr->round_start)
  1457. + bbr->probe_rtt_round_done = 1;
  1458. + if (bbr->probe_rtt_round_done)
  1459. + bbr_check_probe_rtt_done(sk);
  1460. + }
  1461. + }
  1462. + /* Restart after idle ends only once we process a new S/ACK for data */
  1463. + if (rs->delivered > 0)
  1464. + bbr->idle_restart = 0;
  1465. +}
  1466. +
  1467. +static void bbr_update_gains(struct sock *sk)
  1468. +{
  1469. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1470. +
  1471. + switch (bbr->mode) {
  1472. + case BBR_STARTUP:
  1473. + bbr->pacing_gain = bbr_param(sk, startup_pacing_gain);
  1474. + bbr->cwnd_gain = bbr_param(sk, startup_cwnd_gain);
  1475. + break;
  1476. + case BBR_DRAIN:
  1477. + bbr->pacing_gain = bbr_param(sk, drain_gain); /* slow, to drain */
  1478. + bbr->cwnd_gain = bbr_param(sk, startup_cwnd_gain); /* keep cwnd */
  1479. + break;
  1480. + case BBR_PROBE_BW:
  1481. + bbr->pacing_gain = bbr_pacing_gain[bbr->cycle_idx];
  1482. + bbr->cwnd_gain = bbr_param(sk, cwnd_gain);
  1483. + if (bbr_param(sk, bw_probe_cwnd_gain) &&
  1484. + bbr->cycle_idx == BBR_BW_PROBE_UP)
  1485. + bbr->cwnd_gain +=
  1486. + BBR_UNIT * bbr_param(sk, bw_probe_cwnd_gain) / 4;
  1487. + break;
  1488. + case BBR_PROBE_RTT:
  1489. + bbr->pacing_gain = BBR_UNIT;
  1490. + bbr->cwnd_gain = BBR_UNIT;
  1491. + break;
  1492. + default:
  1493. + WARN_ONCE(1, "BBR bad mode: %u\n", bbr->mode);
  1494. + break;
  1495. + }
  1496. +}
  1497. +
  1498. +static u32 bbr3_sndbuf_expand(struct sock *sk)
  1499. +{
  1500. + /* Provision 3 * cwnd since BBR may slow-start even during recovery. */
  1501. + return 3;
  1502. +}
  1503. +
  1504. +/* Incorporate a new bw sample into the current window of our max filter. */
  1505. +static void bbr_take_max_bw_sample(struct sock *sk, u32 bw)
  1506. +{
  1507. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1508. +
  1509. + bbr->bw_hi[1] = max(bw, bbr->bw_hi[1]);
  1510. +}
  1511. +
  1512. +/* Keep max of last 1-2 cycles. Each PROBE_BW cycle, flip filter window. */
  1513. +static void bbr_advance_max_bw_filter(struct sock *sk)
  1514. +{
  1515. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1516. +
  1517. + if (!bbr->bw_hi[1])
  1518. + return; /* no samples in this window; remember old window */
  1519. + bbr->bw_hi[0] = bbr->bw_hi[1];
  1520. + bbr->bw_hi[1] = 0;
  1521. +}
  1522. +
  1523. +/* Reset the estimator for reaching full bandwidth based on bw plateau. */
  1524. +static void bbr_reset_full_bw(struct sock *sk)
  1525. +{
  1526. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1527. +
  1528. + bbr->full_bw = 0;
  1529. + bbr->full_bw_cnt = 0;
  1530. + bbr->full_bw_now = 0;
  1531. +}
  1532. +
  1533. +/* How much do we want in flight? Our BDP, unless congestion cut cwnd. */
  1534. +static u32 bbr_target_inflight(struct sock *sk)
  1535. +{
  1536. + u32 bdp = bbr_inflight(sk, bbr_bw(sk), BBR_UNIT);
  1537. +
  1538. + return min(bdp, tcp_sk(sk)->snd_cwnd);
  1539. +}
  1540. +
  1541. +static bool bbr_is_probing_bandwidth(struct sock *sk)
  1542. +{
  1543. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1544. +
  1545. + return (bbr->mode == BBR_STARTUP) ||
  1546. + (bbr->mode == BBR_PROBE_BW &&
  1547. + (bbr->cycle_idx == BBR_BW_PROBE_REFILL ||
  1548. + bbr->cycle_idx == BBR_BW_PROBE_UP));
  1549. +}
  1550. +
  1551. +/* Has the given amount of time elapsed since we marked the phase start? */
  1552. +static bool bbr_has_elapsed_in_phase(const struct sock *sk, u32 interval_us)
  1553. +{
  1554. + const struct tcp_sock *tp = tcp_sk(sk);
  1555. + const struct bbr3 *bbr = bbr3_get_priv(sk);
  1556. +
  1557. + return tcp_stamp_us_delta(tp->tcp_mstamp,
  1558. + bbr->cycle_mstamp + interval_us) > 0;
  1559. +}
  1560. +
  1561. +static void bbr_handle_queue_too_high_in_startup(struct sock *sk)
  1562. +{
  1563. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1564. + u32 bdp; /* estimated BDP in packets, with quantization budget */
  1565. +
  1566. + bbr->full_bw_reached = 1;
  1567. +
  1568. + bdp = bbr_inflight(sk, bbr_max_bw(sk), BBR_UNIT);
  1569. + bbr->inflight_hi = max(bdp, bbr->inflight_latest);
  1570. +}
  1571. +
  1572. +/* Exit STARTUP upon N consecutive rounds with ECN mark rate > ecn_thresh. */
  1573. +static void bbr_check_ecn_too_high_in_startup(struct sock *sk, u32 ce_ratio)
  1574. +{
  1575. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1576. +
  1577. + if (bbr_full_bw_reached(sk) || !bbr->ecn_eligible ||
  1578. + !bbr_param(sk, full_ecn_cnt) || !bbr_param(sk, ecn_thresh))
  1579. + return;
  1580. +
  1581. + if (ce_ratio >= bbr_param(sk, ecn_thresh))
  1582. + bbr->startup_ecn_rounds++;
  1583. + else
  1584. + bbr->startup_ecn_rounds = 0;
  1585. +
  1586. + if (bbr->startup_ecn_rounds >= bbr_param(sk, full_ecn_cnt)) {
  1587. + bbr_handle_queue_too_high_in_startup(sk);
  1588. + return;
  1589. + }
  1590. +}
  1591. +
  1592. +/* Updates ecn_alpha and returns ce_ratio. -1 if not available. */
  1593. +static int bbr_update_ecn_alpha(struct sock *sk)
  1594. +{
  1595. + struct tcp_sock *tp = tcp_sk(sk);
  1596. + struct net *net = sock_net(sk);
  1597. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1598. + struct tcp_plb_net_context *ctx;
  1599. + bool plb_enabled = false;
  1600. + s32 delivered, delivered_ce;
  1601. + u64 alpha, ce_ratio;
  1602. + u32 gain;
  1603. + bool want_ecn_alpha;
  1604. +
  1605. + /* See if we should use ECN sender logic for this connection. */
  1606. + if (!bbr->ecn_eligible && bbr_can_use_ecn(sk) &&
  1607. + !!bbr_param(sk, ecn_factor) &&
  1608. + (bbr->min_rtt_us <= bbr_ecn_max_rtt_us ||
  1609. + !bbr_ecn_max_rtt_us))
  1610. + bbr->ecn_eligible = 1;
  1611. +
  1612. + ctx = tcp_get_plb_ctx(net);
  1613. + plb_enabled = !ctx ? false : !!READ_ONCE(ctx->params.sysctl_tcp_plb_enabled);
  1614. +
  1615. + /* Skip updating alpha only if not ECN-eligible and PLB is disabled. */
  1616. + want_ecn_alpha = (bbr->ecn_eligible ||
  1617. + (bbr_can_use_ecn(sk) &&
  1618. + plb_enabled));
  1619. + if (!want_ecn_alpha)
  1620. + return -1;
  1621. +
  1622. + delivered = tp->delivered - bbr->alpha_last_delivered;
  1623. + delivered_ce = tp->delivered_ce - bbr->alpha_last_delivered_ce;
  1624. +
  1625. + if (delivered == 0 || /* avoid divide by zero */
  1626. + WARN_ON_ONCE(delivered < 0 || delivered_ce < 0)) /* backwards? */
  1627. + return -1;
  1628. +
  1629. + BUILD_BUG_ON(BBR_SCALE != TCP_PLB_SCALE);
  1630. + ce_ratio = (u64)delivered_ce << BBR_SCALE;
  1631. + do_div(ce_ratio, delivered);
  1632. +
  1633. + gain = bbr_param(sk, ecn_alpha_gain);
  1634. + alpha = ((BBR_UNIT - gain) * bbr->ecn_alpha) >> BBR_SCALE;
  1635. + alpha += (gain * ce_ratio) >> BBR_SCALE;
  1636. + bbr->ecn_alpha = min_t(u32, alpha, BBR_UNIT);
  1637. +
  1638. + bbr->alpha_last_delivered = tp->delivered;
  1639. + bbr->alpha_last_delivered_ce = tp->delivered_ce;
  1640. +
  1641. + bbr_check_ecn_too_high_in_startup(sk, ce_ratio);
  1642. + return (int)ce_ratio;
  1643. +}
  1644. +
  1645. +/* Protective Load Balancing (PLB). PLB rehashes outgoing data (to a new IPv6
  1646. + * flow label) if it encounters sustained congestion in the form of ECN marks.
  1647. + */
  1648. +static void bbr_plb(struct sock *sk, const struct rate_sample *rs, int ce_ratio)
  1649. +{
  1650. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1651. +
  1652. + if (bbr->round_start && ce_ratio >= 0)
  1653. + tcp_plb_update_state(sk, &bbr->plb, ce_ratio);
  1654. +
  1655. + tcp_plb_check_rehash(sk, &bbr->plb);
  1656. +}
  1657. +
  1658. +/* Each round trip of BBR_BW_PROBE_UP, double volume of probing data. */
  1659. +static void bbr_raise_inflight_hi_slope(struct sock *sk)
  1660. +{
  1661. + struct tcp_sock *tp = tcp_sk(sk);
  1662. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1663. + u32 growth_this_round, cnt;
  1664. +
  1665. + /* Calculate "slope": packets S/Acked per inflight_hi increment. */
  1666. + growth_this_round = 1 << bbr->bw_probe_up_rounds;
  1667. + bbr->bw_probe_up_rounds = min(bbr->bw_probe_up_rounds + 1, 30);
  1668. + cnt = tcp_snd_cwnd(tp) / growth_this_round;
  1669. + cnt = max(cnt, 1U);
  1670. + bbr->bw_probe_up_cnt = cnt;
  1671. +}
  1672. +
  1673. +/* In BBR_BW_PROBE_UP, not seeing high loss/ECN/queue, so raise inflight_hi. */
  1674. +static void bbr_probe_inflight_hi_upward(struct sock *sk,
  1675. + const struct rate_sample *rs)
  1676. +{
  1677. + struct tcp_sock *tp = tcp_sk(sk);
  1678. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1679. + u32 delta;
  1680. +
  1681. + if (!tp->is_cwnd_limited || tcp_snd_cwnd(tp) < bbr->inflight_hi)
  1682. + return; /* not fully using inflight_hi, so don't grow it */
  1683. +
  1684. + /* For each bw_probe_up_cnt packets ACKed, increase inflight_hi by 1. */
  1685. + bbr->bw_probe_up_acks += rs->acked_sacked;
  1686. + if (bbr->bw_probe_up_acks >= bbr->bw_probe_up_cnt) {
  1687. + delta = bbr->bw_probe_up_acks / bbr->bw_probe_up_cnt;
  1688. + bbr->bw_probe_up_acks -= delta * bbr->bw_probe_up_cnt;
  1689. + bbr->inflight_hi += delta;
  1690. + bbr->try_fast_path = 0; /* Need to update cwnd */
  1691. + }
  1692. +
  1693. + if (bbr->round_start)
  1694. + bbr_raise_inflight_hi_slope(sk);
  1695. +}
  1696. +
  1697. +/* Does loss/ECN rate for this sample say inflight is "too high"?
  1698. + * This is used by both the bbr_check_loss_too_high_in_startup() function,
  1699. + * and in PROBE_UP.
  1700. + */
  1701. +static bool bbr_is_inflight_too_high(const struct sock *sk,
  1702. + const struct rate_sample *rs)
  1703. +{
  1704. + const struct bbr3 *bbr = bbr3_get_priv(sk);
  1705. + u32 loss_thresh, ecn_thresh;
  1706. +
  1707. + if (rs->lost > 0 && rs->tx_in_flight) {
  1708. + loss_thresh = (u64)rs->tx_in_flight * bbr_param(sk, loss_thresh) >>
  1709. + BBR_SCALE;
  1710. + if (rs->lost > loss_thresh) {
  1711. + return true;
  1712. + }
  1713. + }
  1714. +
  1715. + if (rs->delivered_ce > 0 && rs->delivered > 0 &&
  1716. + bbr->ecn_eligible && !!bbr_param(sk, ecn_thresh)) {
  1717. + ecn_thresh = (u64)rs->delivered * bbr_param(sk, ecn_thresh) >>
  1718. + BBR_SCALE;
  1719. + if (rs->delivered_ce > ecn_thresh) {
  1720. + return true;
  1721. + }
  1722. + }
  1723. +
  1724. + return false;
  1725. +}
  1726. +
  1727. +/* Calculate the tx_in_flight level that corresponded to excessive loss.
  1728. + * We find "lost_prefix" segs of the skb where loss rate went too high,
  1729. + * by solving for "lost_prefix" in the following equation:
  1730. + * lost / inflight >= loss_thresh
  1731. + * (lost_prev + lost_prefix) / (inflight_prev + lost_prefix) >= loss_thresh
  1732. + * Then we take that equation, convert it to fixed point, and
  1733. + * round up to the nearest packet.
  1734. + */
  1735. +static u32 bbr_inflight_hi_from_lost_skb(const struct sock *sk,
  1736. + const struct rate_sample *rs,
  1737. + const struct sk_buff *skb)
  1738. +{
  1739. + const struct tcp_sock *tp = tcp_sk(sk);
  1740. + u32 loss_thresh = bbr_param(sk, loss_thresh);
  1741. + u32 pcount, divisor, inflight_hi;
  1742. + s32 inflight_prev, lost_prev;
  1743. + u64 loss_budget, lost_prefix;
  1744. +
  1745. + pcount = tcp_skb_pcount(skb);
  1746. +
  1747. + /* How much data was in flight before this skb? */
  1748. + inflight_prev = rs->tx_in_flight - pcount;
  1749. + if (inflight_prev < 0) {
  1750. + WARN_ONCE(tcp_skb_tx_in_flight_is_suspicious(
  1751. + pcount,
  1752. + TCP_SKB_CB(skb)->sacked,
  1753. + rs->tx_in_flight),
  1754. + "tx_in_flight: %u pcount: %u reneg: %u",
  1755. + rs->tx_in_flight, pcount, tcp_sk(sk)->is_sack_reneg);
  1756. + return ~0U;
  1757. + }
  1758. +
  1759. + /* How much inflight data was marked lost before this skb? */
  1760. + lost_prev = rs->lost - pcount;
  1761. + if (WARN_ONCE(lost_prev < 0,
  1762. + "cwnd: %u ca: %d out: %u lost: %u pif: %u "
  1763. + "tx_in_flight: %u tx.lost: %u tp->lost: %u rs->lost: %d "
  1764. + "lost_prev: %d pcount: %d seq: %u end_seq: %u reneg: %u",
  1765. + tcp_snd_cwnd(tp), inet_csk(sk)->icsk_ca_state,
  1766. + tp->packets_out, tp->lost_out, tcp_packets_in_flight(tp),
  1767. + rs->tx_in_flight, TCP_SKB_CB(skb)->tx.lost, tp->lost,
  1768. + rs->lost, lost_prev, pcount,
  1769. + TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
  1770. + tp->is_sack_reneg))
  1771. + return ~0U;
  1772. +
  1773. + /* At what prefix of this lost skb did losss rate exceed loss_thresh? */
  1774. + loss_budget = (u64)inflight_prev * loss_thresh + BBR_UNIT - 1;
  1775. + loss_budget >>= BBR_SCALE;
  1776. + if (lost_prev >= loss_budget) {
  1777. + lost_prefix = 0; /* previous losses crossed loss_thresh */
  1778. + } else {
  1779. + lost_prefix = loss_budget - lost_prev;
  1780. + lost_prefix <<= BBR_SCALE;
  1781. + divisor = BBR_UNIT - loss_thresh;
  1782. + if (WARN_ON_ONCE(!divisor)) /* loss_thresh is 8 bits */
  1783. + return ~0U;
  1784. + do_div(lost_prefix, divisor);
  1785. + }
  1786. +
  1787. + inflight_hi = inflight_prev + lost_prefix;
  1788. + return inflight_hi;
  1789. +}
  1790. +
  1791. +/* If loss/ECN rates during probing indicated we may have overfilled a
  1792. + * buffer, return an operating point that tries to leave unutilized headroom in
  1793. + * the path for other flows, for fairness convergence and lower RTTs and loss.
  1794. + */
  1795. +static u32 bbr_inflight_with_headroom(const struct sock *sk)
  1796. +{
  1797. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1798. + u32 headroom, headroom_fraction;
  1799. +
  1800. + if (bbr->inflight_hi == ~0U)
  1801. + return ~0U;
  1802. +
  1803. + headroom_fraction = bbr_param(sk, inflight_headroom);
  1804. + headroom = ((u64)bbr->inflight_hi * headroom_fraction) >> BBR_SCALE;
  1805. + headroom = max(headroom, 1U);
  1806. + return max_t(s32, bbr->inflight_hi - headroom,
  1807. + bbr_param(sk, cwnd_min_target));
  1808. +}
  1809. +
  1810. +/* Bound cwnd to a sensible level, based on our current probing state
  1811. + * machine phase and model of a good inflight level (inflight_lo, inflight_hi).
  1812. + */
  1813. +static void bbr_bound_cwnd_for_inflight_model(struct sock *sk)
  1814. +{
  1815. + struct tcp_sock *tp = tcp_sk(sk);
  1816. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1817. + u32 cap;
  1818. +
  1819. + /* tcp_rcv_synsent_state_process() currently calls tcp_ack()
  1820. + * and thus cong_control() without first initializing us(!).
  1821. + */
  1822. + if (!bbr->initialized)
  1823. + return;
  1824. +
  1825. + cap = ~0U;
  1826. + if (bbr->mode == BBR_PROBE_BW &&
  1827. + bbr->cycle_idx != BBR_BW_PROBE_CRUISE) {
  1828. + /* Probe to see if more packets fit in the path. */
  1829. + cap = bbr->inflight_hi;
  1830. + } else {
  1831. + if (bbr->mode == BBR_PROBE_RTT ||
  1832. + (bbr->mode == BBR_PROBE_BW &&
  1833. + bbr->cycle_idx == BBR_BW_PROBE_CRUISE))
  1834. + cap = bbr_inflight_with_headroom(sk);
  1835. + }
  1836. + /* Adapt to any loss/ECN since our last bw probe. */
  1837. + cap = min(cap, bbr->inflight_lo);
  1838. +
  1839. + cap = max_t(u32, cap, bbr_param(sk, cwnd_min_target));
  1840. + tcp_snd_cwnd_set(tp, min(cap, tcp_snd_cwnd(tp)));
  1841. +}
  1842. +
  1843. +/* How should we multiplicatively cut bw or inflight limits based on ECN? */
  1844. +static u32 bbr_ecn_cut(struct sock *sk)
  1845. +{
  1846. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1847. +
  1848. + return BBR_UNIT -
  1849. + ((bbr->ecn_alpha * bbr_param(sk, ecn_factor)) >> BBR_SCALE);
  1850. +}
  1851. +
  1852. +/* Init lower bounds if have not inited yet. */
  1853. +static void bbr_init_lower_bounds(struct sock *sk, bool init_bw)
  1854. +{
  1855. + struct tcp_sock *tp = tcp_sk(sk);
  1856. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1857. +
  1858. + if (init_bw && bbr->bw_lo == ~0U)
  1859. + bbr->bw_lo = bbr_max_bw(sk);
  1860. + if (bbr->inflight_lo == ~0U)
  1861. + bbr->inflight_lo = tcp_snd_cwnd(tp);
  1862. +}
  1863. +
  1864. +/* Reduce bw and inflight to (1 - beta). */
  1865. +static void bbr_loss_lower_bounds(struct sock *sk, u32 *bw, u32 *inflight)
  1866. +{
  1867. + struct bbr3* bbr = bbr3_get_priv(sk);
  1868. + u32 loss_cut = BBR_UNIT - bbr_param(sk, beta);
  1869. +
  1870. + *bw = max_t(u32, bbr->bw_latest,
  1871. + (u64)bbr->bw_lo * loss_cut >> BBR_SCALE);
  1872. + *inflight = max_t(u32, bbr->inflight_latest,
  1873. + (u64)bbr->inflight_lo * loss_cut >> BBR_SCALE);
  1874. +}
  1875. +
  1876. +/* Reduce inflight to (1 - alpha*ecn_factor). */
  1877. +static void bbr_ecn_lower_bounds(struct sock *sk, u32 *inflight)
  1878. +{
  1879. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1880. + u32 ecn_cut = bbr_ecn_cut(sk);
  1881. +
  1882. + *inflight = (u64)bbr->inflight_lo * ecn_cut >> BBR_SCALE;
  1883. +}
  1884. +
  1885. +/* Estimate a short-term lower bound on the capacity available now, based
  1886. + * on measurements of the current delivery process and recent history. When we
  1887. + * are seeing loss/ECN at times when we are not probing bw, then conservatively
  1888. + * move toward flow balance by multiplicatively cutting our short-term
  1889. + * estimated safe rate and volume of data (bw_lo and inflight_lo). We use a
  1890. + * multiplicative decrease in order to converge to a lower capacity in time
  1891. + * logarithmic in the magnitude of the decrease.
  1892. + *
  1893. + * However, we do not cut our short-term estimates lower than the current rate
  1894. + * and volume of delivered data from this round trip, since from the current
  1895. + * delivery process we can estimate the measured capacity available now.
  1896. + *
  1897. + * Anything faster than that approach would knowingly risk high loss, which can
  1898. + * cause low bw for Reno/CUBIC and high loss recovery latency for
  1899. + * request/response flows using any congestion control.
  1900. + */
  1901. +static void bbr_adapt_lower_bounds(struct sock *sk,
  1902. + const struct rate_sample *rs)
  1903. +{
  1904. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1905. + u32 ecn_inflight_lo = ~0U;
  1906. +
  1907. + /* We only use lower-bound estimates when not probing bw.
  1908. + * When probing we need to push inflight higher to probe bw.
  1909. + */
  1910. + if (bbr_is_probing_bandwidth(sk))
  1911. + return;
  1912. +
  1913. + /* ECN response. */
  1914. + if (bbr->ecn_in_round && !!bbr_param(sk, ecn_factor)) {
  1915. + bbr_init_lower_bounds(sk, false);
  1916. + bbr_ecn_lower_bounds(sk, &ecn_inflight_lo);
  1917. + }
  1918. +
  1919. + /* Loss response. */
  1920. + if (bbr->loss_in_round) {
  1921. + bbr_init_lower_bounds(sk, true);
  1922. + bbr_loss_lower_bounds(sk, &bbr->bw_lo, &bbr->inflight_lo);
  1923. + }
  1924. +
  1925. + /* Adjust to the lower of the levels implied by loss/ECN. */
  1926. + bbr->inflight_lo = min(bbr->inflight_lo, ecn_inflight_lo);
  1927. + bbr->bw_lo = max(1U, bbr->bw_lo);
  1928. +}
  1929. +
  1930. +/* Reset any short-term lower-bound adaptation to congestion, so that we can
  1931. + * push our inflight up.
  1932. + */
  1933. +static void bbr_reset_lower_bounds(struct sock *sk)
  1934. +{
  1935. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1936. +
  1937. + bbr->bw_lo = ~0U;
  1938. + bbr->inflight_lo = ~0U;
  1939. +}
  1940. +
  1941. +/* After bw probing (STARTUP/PROBE_UP), reset signals before entering a state
  1942. + * machine phase where we adapt our lower bound based on congestion signals.
  1943. + */
  1944. +static void bbr_reset_congestion_signals(struct sock *sk)
  1945. +{
  1946. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1947. +
  1948. + bbr->loss_in_round = 0;
  1949. + bbr->ecn_in_round = 0;
  1950. + bbr->loss_in_cycle = 0;
  1951. + bbr->ecn_in_cycle = 0;
  1952. + bbr->bw_latest = 0;
  1953. + bbr->inflight_latest = 0;
  1954. +}
  1955. +
  1956. +static void bbr_exit_loss_recovery(struct sock *sk)
  1957. +{
  1958. + struct tcp_sock *tp = tcp_sk(sk);
  1959. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1960. +
  1961. + tcp_snd_cwnd_set(tp, max(tcp_snd_cwnd(tp), bbr->prior_cwnd));
  1962. + bbr->try_fast_path = 0; /* bound cwnd using latest model */
  1963. +}
  1964. +
  1965. +/* Update rate and volume of delivered data from latest round trip. */
  1966. +static void bbr_update_latest_delivery_signals(
  1967. + struct sock *sk, const struct rate_sample *rs, struct bbr_context *ctx)
  1968. +{
  1969. + struct tcp_sock *tp = tcp_sk(sk);
  1970. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1971. +
  1972. + bbr->loss_round_start = 0;
  1973. + if (rs->interval_us <= 0 || !rs->acked_sacked)
  1974. + return; /* Not a valid observation */
  1975. +
  1976. + bbr->bw_latest = max_t(u32, bbr->bw_latest, ctx->sample_bw);
  1977. + bbr->inflight_latest = max_t(u32, bbr->inflight_latest, rs->delivered);
  1978. +
  1979. + if (!before(rs->prior_delivered, bbr->loss_round_delivered)) {
  1980. + bbr->loss_round_delivered = tp->delivered;
  1981. + bbr->loss_round_start = 1; /* mark start of new round trip */
  1982. + }
  1983. +}
  1984. +
  1985. +/* Once per round, reset filter for latest rate and volume of delivered data. */
  1986. +static void bbr_advance_latest_delivery_signals(
  1987. + struct sock *sk, const struct rate_sample *rs, struct bbr_context *ctx)
  1988. +{
  1989. + struct bbr3 *bbr = bbr3_get_priv(sk);
  1990. +
  1991. + /* If ACK matches a TLP retransmit, persist the filter. If we detect
  1992. + * that a TLP retransmit plugged a tail loss, we'll want to remember
  1993. + * how much data the path delivered before the tail loss.
  1994. + */
  1995. + if (bbr->loss_round_start && !rs->is_acking_tlp_retrans_seq) {
  1996. + bbr->bw_latest = ctx->sample_bw;
  1997. + bbr->inflight_latest = rs->delivered;
  1998. + }
  1999. +}
  2000. +
  2001. +/* Update (most of) our congestion signals: track the recent rate and volume of
  2002. + * delivered data, presence of loss, and EWMA degree of ECN marking.
  2003. + */
  2004. +static void bbr_update_congestion_signals(
  2005. + struct sock *sk, const struct rate_sample *rs, struct bbr_context *ctx)
  2006. +{
  2007. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2008. + u64 bw;
  2009. +
  2010. + if (rs->interval_us <= 0 || !rs->acked_sacked)
  2011. + return; /* Not a valid observation */
  2012. + bw = ctx->sample_bw;
  2013. +
  2014. + if (!rs->is_app_limited || bw >= bbr_max_bw(sk))
  2015. + bbr_take_max_bw_sample(sk, bw);
  2016. +
  2017. + bbr->loss_in_round |= (rs->losses > 0);
  2018. +
  2019. + if (!bbr->loss_round_start)
  2020. + return; /* skip the per-round-trip updates */
  2021. + /* Now do per-round-trip updates. */
  2022. + bbr_adapt_lower_bounds(sk, rs);
  2023. +
  2024. + bbr->loss_in_round = 0;
  2025. + bbr->ecn_in_round = 0;
  2026. +}
  2027. +
  2028. +/* Bandwidth probing can cause loss. To help coexistence with loss-based
  2029. + * congestion control we spread out our probing in a Reno-conscious way. Due to
  2030. + * the shape of the Reno sawtooth, the time required between loss epochs for an
  2031. + * idealized Reno flow is a number of round trips that is the BDP of that
  2032. + * flow. We count packet-timed round trips directly, since measured RTT can
  2033. + * vary widely, and Reno is driven by packet-timed round trips.
  2034. + */
  2035. +static bool bbr_is_reno_coexistence_probe_time(struct sock *sk)
  2036. +{
  2037. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2038. + u32 rounds;
  2039. +
  2040. + /* Random loss can shave some small percentage off of our inflight
  2041. + * in each round. To survive this, flows need robust periodic probes.
  2042. + */
  2043. + rounds = min_t(u32, bbr_param(sk, bw_probe_max_rounds), bbr_target_inflight(sk));
  2044. + return bbr->rounds_since_probe >= rounds;
  2045. +}
  2046. +
  2047. +/* How long do we want to wait before probing for bandwidth (and risking
  2048. + * loss)? We randomize the wait, for better mixing and fairness convergence.
  2049. + *
  2050. + * We bound the Reno-coexistence inter-bw-probe time to be 62-63 round trips.
  2051. + * This is calculated to allow fairness with a 25Mbps, 30ms Reno flow,
  2052. + * (eg 4K video to a broadband user):
  2053. + * BDP = 25Mbps * .030sec /(1514bytes) = 61.9 packets
  2054. + *
  2055. + * We bound the BBR-native inter-bw-probe wall clock time to be:
  2056. + * (a) higher than 2 sec: to try to avoid causing loss for a long enough time
  2057. + * to allow Reno at 30ms to get 4K video bw, the inter-bw-probe time must
  2058. + * be at least: 25Mbps * .030sec / (1514bytes) * 0.030sec = 1.9secs
  2059. + * (b) lower than 3 sec: to ensure flows can start probing in a reasonable
  2060. + * amount of time to discover unutilized bw on human-scale interactive
  2061. + * time-scales (e.g. perhaps traffic from a web page download that we
  2062. + * were competing with is now complete).
  2063. + */
  2064. +static void bbr_pick_probe_wait(struct sock *sk)
  2065. +{
  2066. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2067. +
  2068. + /* Decide the random round-trip bound for wait until probe: */
  2069. + bbr->rounds_since_probe =
  2070. + prandom_u32_max(bbr_param(sk, bw_probe_rand_rounds));
  2071. + /* Decide the random wall clock bound for wait until probe: */
  2072. + bbr->probe_wait_us = bbr_param(sk, bw_probe_base_us) +
  2073. + prandom_u32_max(bbr_param(sk, bw_probe_rand_us));
  2074. +}
  2075. +
  2076. +static void bbr_set_cycle_idx(struct sock *sk, int cycle_idx)
  2077. +{
  2078. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2079. +
  2080. + bbr->cycle_idx = cycle_idx;
  2081. + /* New phase, so need to update cwnd and pacing rate. */
  2082. + bbr->try_fast_path = 0;
  2083. +}
  2084. +
  2085. +/* Send at estimated bw to fill the pipe, but not queue. We need this phase
  2086. + * before PROBE_UP, because as soon as we send faster than the available bw
  2087. + * we will start building a queue, and if the buffer is shallow we can cause
  2088. + * loss. If we do not fill the pipe before we cause this loss, our bw_hi and
  2089. + * inflight_hi estimates will underestimate.
  2090. + */
  2091. +static void bbr_start_bw_probe_refill(struct sock *sk, u32 bw_probe_up_rounds)
  2092. +{
  2093. + struct tcp_sock *tp = tcp_sk(sk);
  2094. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2095. +
  2096. + bbr_reset_lower_bounds(sk);
  2097. + bbr->bw_probe_up_rounds = bw_probe_up_rounds;
  2098. + bbr->bw_probe_up_acks = 0;
  2099. + bbr->stopped_risky_probe = 0;
  2100. + bbr->ack_phase = BBR_ACKS_REFILLING;
  2101. + bbr->next_rtt_delivered = tp->delivered;
  2102. + bbr_set_cycle_idx(sk, BBR_BW_PROBE_REFILL);
  2103. +}
  2104. +
  2105. +/* Now probe max deliverable data rate and volume. */
  2106. +static void bbr_start_bw_probe_up(struct sock *sk, struct bbr_context *ctx)
  2107. +{
  2108. + struct tcp_sock *tp = tcp_sk(sk);
  2109. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2110. +
  2111. + bbr->ack_phase = BBR_ACKS_PROBE_STARTING;
  2112. + bbr->next_rtt_delivered = tp->delivered;
  2113. + bbr->cycle_mstamp = tp->tcp_mstamp;
  2114. + bbr_reset_full_bw(sk);
  2115. + bbr->full_bw = ctx->sample_bw;
  2116. + bbr_set_cycle_idx(sk, BBR_BW_PROBE_UP);
  2117. + bbr_raise_inflight_hi_slope(sk);
  2118. +}
  2119. +
  2120. +/* Start a new PROBE_BW probing cycle of some wall clock length. Pick a wall
  2121. + * clock time at which to probe beyond an inflight that we think to be
  2122. + * safe. This will knowingly risk packet loss, so we want to do this rarely, to
  2123. + * keep packet loss rates low. Also start a round-trip counter, to probe faster
  2124. + * if we estimate a Reno flow at our BDP would probe faster.
  2125. + */
  2126. +static void bbr_start_bw_probe_down(struct sock *sk)
  2127. +{
  2128. + struct tcp_sock *tp = tcp_sk(sk);
  2129. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2130. +
  2131. + bbr_reset_congestion_signals(sk);
  2132. + bbr->bw_probe_up_cnt = ~0U; /* not growing inflight_hi any more */
  2133. + bbr_pick_probe_wait(sk);
  2134. + bbr->cycle_mstamp = tp->tcp_mstamp; /* start wall clock */
  2135. + bbr->ack_phase = BBR_ACKS_PROBE_STOPPING;
  2136. + bbr->next_rtt_delivered = tp->delivered;
  2137. + bbr_set_cycle_idx(sk, BBR_BW_PROBE_DOWN);
  2138. +}
  2139. +
  2140. +/* Cruise: maintain what we estimate to be a neutral, conservative
  2141. + * operating point, without attempting to probe up for bandwidth or down for
  2142. + * RTT, and only reducing inflight in response to loss/ECN signals.
  2143. + */
  2144. +static void bbr_start_bw_probe_cruise(struct sock *sk)
  2145. +{
  2146. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2147. +
  2148. + if (bbr->inflight_lo != ~0U)
  2149. + bbr->inflight_lo = min(bbr->inflight_lo, bbr->inflight_hi);
  2150. +
  2151. + bbr_set_cycle_idx(sk, BBR_BW_PROBE_CRUISE);
  2152. +}
  2153. +
  2154. +/* Loss and/or ECN rate is too high while probing.
  2155. + * Adapt (once per bw probe) by cutting inflight_hi and then restarting cycle.
  2156. + */
  2157. +static void bbr_handle_inflight_too_high(struct sock *sk,
  2158. + const struct rate_sample *rs)
  2159. +{
  2160. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2161. + const u32 beta = bbr_param(sk, beta);
  2162. +
  2163. + bbr->prev_probe_too_high = 1;
  2164. + bbr->bw_probe_samples = 0; /* only react once per probe */
  2165. + /* If we are app-limited then we are not robustly
  2166. + * probing the max volume of inflight data we think
  2167. + * might be safe (analogous to how app-limited bw
  2168. + * samples are not known to be robustly probing bw).
  2169. + */
  2170. + if (!rs->is_app_limited) {
  2171. + bbr->inflight_hi = max_t(u32, rs->tx_in_flight,
  2172. + (u64)bbr_target_inflight(sk) *
  2173. + (BBR_UNIT - beta) >> BBR_SCALE);
  2174. + }
  2175. + if (bbr->mode == BBR_PROBE_BW && bbr->cycle_idx == BBR_BW_PROBE_UP)
  2176. + bbr_start_bw_probe_down(sk);
  2177. +}
  2178. +
  2179. +/* If we're seeing bw and loss samples reflecting our bw probing, adapt
  2180. + * using the signals we see. If loss or ECN mark rate gets too high, then adapt
  2181. + * inflight_hi downward. If we're able to push inflight higher without such
  2182. + * signals, push higher: adapt inflight_hi upward.
  2183. + */
  2184. +static bool bbr_adapt_upper_bounds(struct sock *sk,
  2185. + const struct rate_sample *rs,
  2186. + struct bbr_context *ctx)
  2187. +{
  2188. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2189. +
  2190. + /* Track when we'll see bw/loss samples resulting from our bw probes. */
  2191. + if (bbr->ack_phase == BBR_ACKS_PROBE_STARTING && bbr->round_start)
  2192. + bbr->ack_phase = BBR_ACKS_PROBE_FEEDBACK;
  2193. + if (bbr->ack_phase == BBR_ACKS_PROBE_STOPPING && bbr->round_start) {
  2194. + /* End of samples from bw probing phase. */
  2195. + bbr->bw_probe_samples = 0;
  2196. + bbr->ack_phase = BBR_ACKS_INIT;
  2197. + /* At this point in the cycle, our current bw sample is also
  2198. + * our best recent chance at finding the highest available bw
  2199. + * for this flow. So now is the best time to forget the bw
  2200. + * samples from the previous cycle, by advancing the window.
  2201. + */
  2202. + if (bbr->mode == BBR_PROBE_BW && !rs->is_app_limited)
  2203. + bbr_advance_max_bw_filter(sk);
  2204. + /* If we had an inflight_hi, then probed and pushed inflight all
  2205. + * the way up to hit that inflight_hi without seeing any
  2206. + * high loss/ECN in all the resulting ACKs from that probing,
  2207. + * then probe up again, this time letting inflight persist at
  2208. + * inflight_hi for a round trip, then accelerating beyond.
  2209. + */
  2210. + if (bbr->mode == BBR_PROBE_BW &&
  2211. + bbr->stopped_risky_probe && !bbr->prev_probe_too_high) {
  2212. + bbr_start_bw_probe_refill(sk, 0);
  2213. + return true; /* yes, decided state transition */
  2214. + }
  2215. + }
  2216. + if (bbr_is_inflight_too_high(sk, rs)) {
  2217. + if (bbr->bw_probe_samples) /* sample is from bw probing? */
  2218. + bbr_handle_inflight_too_high(sk, rs);
  2219. + } else {
  2220. + /* Loss/ECN rate is declared safe. Adjust upper bound upward. */
  2221. +
  2222. + if (bbr->inflight_hi == ~0U)
  2223. + return false; /* no excess queue signals yet */
  2224. +
  2225. + /* To be resilient to random loss, we must raise bw/inflight_hi
  2226. + * if we observe in any phase that a higher level is safe.
  2227. + */
  2228. + if (rs->tx_in_flight > bbr->inflight_hi) {
  2229. + bbr->inflight_hi = rs->tx_in_flight;
  2230. + }
  2231. +
  2232. + if (bbr->mode == BBR_PROBE_BW &&
  2233. + bbr->cycle_idx == BBR_BW_PROBE_UP)
  2234. + bbr_probe_inflight_hi_upward(sk, rs);
  2235. + }
  2236. +
  2237. + return false;
  2238. +}
  2239. +
  2240. +/* Check if it's time to probe for bandwidth now, and if so, kick it off. */
  2241. +static bool bbr_check_time_to_probe_bw(struct sock *sk,
  2242. + const struct rate_sample *rs)
  2243. +{
  2244. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2245. + u32 n;
  2246. +
  2247. + /* If we seem to be at an operating point where we are not seeing loss
  2248. + * but we are seeing ECN marks, then when the ECN marks cease we reprobe
  2249. + * quickly (in case cross-traffic has ceased and freed up bw).
  2250. + */
  2251. + if (bbr_param(sk, ecn_reprobe_gain) && bbr->ecn_eligible &&
  2252. + bbr->ecn_in_cycle && !bbr->loss_in_cycle &&
  2253. + inet_csk(sk)->icsk_ca_state == TCP_CA_Open) {
  2254. + /* Calculate n so that when bbr_raise_inflight_hi_slope()
  2255. + * computes growth_this_round as 2^n it will be roughly the
  2256. + * desired volume of data (inflight_hi*ecn_reprobe_gain).
  2257. + */
  2258. + n = ilog2((((u64)bbr->inflight_hi *
  2259. + bbr_param(sk, ecn_reprobe_gain)) >> BBR_SCALE));
  2260. + bbr_start_bw_probe_refill(sk, n);
  2261. + return true;
  2262. + }
  2263. +
  2264. + if (bbr_has_elapsed_in_phase(sk, bbr->probe_wait_us) ||
  2265. + bbr_is_reno_coexistence_probe_time(sk)) {
  2266. + bbr_start_bw_probe_refill(sk, 0);
  2267. + return true;
  2268. + }
  2269. + return false;
  2270. +}
  2271. +
  2272. +/* Is it time to transition from PROBE_DOWN to PROBE_CRUISE? */
  2273. +static bool bbr_check_time_to_cruise(struct sock *sk, u32 inflight, u32 bw)
  2274. +{
  2275. + /* Always need to pull inflight down to leave headroom in queue. */
  2276. + if (inflight > bbr_inflight_with_headroom(sk))
  2277. + return false;
  2278. +
  2279. + return inflight <= bbr_inflight(sk, bw, BBR_UNIT);
  2280. +}
  2281. +
  2282. +/* PROBE_BW state machine: cruise, refill, probe for bw, or drain? */
  2283. +static void bbr_update_cycle_phase(struct sock *sk,
  2284. + const struct rate_sample *rs,
  2285. + struct bbr_context *ctx)
  2286. +{
  2287. + struct tcp_sock *tp = tcp_sk(sk);
  2288. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2289. + bool is_bw_probe_done = false;
  2290. + u32 inflight, bw;
  2291. +
  2292. + if (!bbr_full_bw_reached(sk))
  2293. + return;
  2294. +
  2295. + /* In DRAIN, PROBE_BW, or PROBE_RTT, adjust upper bounds. */
  2296. + if (bbr_adapt_upper_bounds(sk, rs, ctx))
  2297. + return; /* already decided state transition */
  2298. +
  2299. + if (bbr->mode != BBR_PROBE_BW)
  2300. + return;
  2301. +
  2302. + inflight = bbr_packets_in_net_at_edt(sk, rs->prior_in_flight);
  2303. + bw = bbr_max_bw(sk);
  2304. +
  2305. + switch (bbr->cycle_idx) {
  2306. + /* First we spend most of our time cruising with a pacing_gain of 1.0,
  2307. + * which paces at the estimated bw, to try to fully use the pipe
  2308. + * without building queue. If we encounter loss/ECN marks, we adapt
  2309. + * by slowing down.
  2310. + */
  2311. + case BBR_BW_PROBE_CRUISE:
  2312. + if (bbr_check_time_to_probe_bw(sk, rs))
  2313. + return; /* already decided state transition */
  2314. + break;
  2315. +
  2316. + /* After cruising, when it's time to probe, we first "refill": we send
  2317. + * at the estimated bw to fill the pipe, before probing higher and
  2318. + * knowingly risking overflowing the bottleneck buffer (causing loss).
  2319. + */
  2320. + case BBR_BW_PROBE_REFILL:
  2321. + if (bbr->round_start) {
  2322. + /* After one full round trip of sending in REFILL, we
  2323. + * start to see bw samples reflecting our REFILL, which
  2324. + * may be putting too much data in flight.
  2325. + */
  2326. + bbr->bw_probe_samples = 1;
  2327. + bbr_start_bw_probe_up(sk, ctx);
  2328. + }
  2329. + break;
  2330. +
  2331. + /* After we refill the pipe, we probe by using a pacing_gain > 1.0, to
  2332. + * probe for bw. If we have not seen loss/ECN, we try to raise inflight
  2333. + * to at least pacing_gain*BDP; note that this may take more than
  2334. + * min_rtt if min_rtt is small (e.g. on a LAN).
  2335. + *
  2336. + * We terminate PROBE_UP bandwidth probing upon any of the following:
  2337. + *
  2338. + * (1) We've pushed inflight up to hit the inflight_hi target set in the
  2339. + * most recent previous bw probe phase. Thus we want to start
  2340. + * draining the queue immediately because it's very likely the most
  2341. + * recently sent packets will fill the queue and cause drops.
  2342. + * (2) If inflight_hi has not limited bandwidth growth recently, and
  2343. + * yet delivered bandwidth has not increased much recently
  2344. + * (bbr->full_bw_now).
  2345. + * (3) Loss filter says loss rate is "too high".
  2346. + * (4) ECN filter says ECN mark rate is "too high".
  2347. + *
  2348. + * (1) (2) checked here, (3) (4) checked in bbr_is_inflight_too_high()
  2349. + */
  2350. + case BBR_BW_PROBE_UP:
  2351. + if (bbr->prev_probe_too_high &&
  2352. + inflight >= bbr->inflight_hi) {
  2353. + bbr->stopped_risky_probe = 1;
  2354. + is_bw_probe_done = true;
  2355. + } else {
  2356. + if (tp->is_cwnd_limited &&
  2357. + tcp_snd_cwnd(tp) >= bbr->inflight_hi) {
  2358. + /* inflight_hi is limiting bw growth */
  2359. + bbr_reset_full_bw(sk);
  2360. + bbr->full_bw = ctx->sample_bw;
  2361. + } else if (bbr->full_bw_now) {
  2362. + /* Plateau in estimated bw. Pipe looks full. */
  2363. + is_bw_probe_done = true;
  2364. + }
  2365. + }
  2366. + if (is_bw_probe_done) {
  2367. + bbr->prev_probe_too_high = 0; /* no loss/ECN (yet) */
  2368. + bbr_start_bw_probe_down(sk); /* restart w/ down */
  2369. + }
  2370. + break;
  2371. +
  2372. + /* After probing in PROBE_UP, we have usually accumulated some data in
  2373. + * the bottleneck buffer (if bw probing didn't find more bw). We next
  2374. + * enter PROBE_DOWN to try to drain any excess data from the queue. To
  2375. + * do this, we use a pacing_gain < 1.0. We hold this pacing gain until
  2376. + * our inflight is less then that target cruising point, which is the
  2377. + * minimum of (a) the amount needed to leave headroom, and (b) the
  2378. + * estimated BDP. Once inflight falls to match the target, we estimate
  2379. + * the queue is drained; persisting would underutilize the pipe.
  2380. + */
  2381. + case BBR_BW_PROBE_DOWN:
  2382. + if (bbr_check_time_to_probe_bw(sk, rs))
  2383. + return; /* already decided state transition */
  2384. + if (bbr_check_time_to_cruise(sk, inflight, bw))
  2385. + bbr_start_bw_probe_cruise(sk);
  2386. + break;
  2387. +
  2388. + default:
  2389. + WARN_ONCE(1, "BBR invalid cycle index %u\n", bbr->cycle_idx);
  2390. + }
  2391. +}
  2392. +
  2393. +/* Exiting PROBE_RTT, so return to bandwidth probing in STARTUP or PROBE_BW. */
  2394. +static void bbr_exit_probe_rtt(struct sock *sk)
  2395. +{
  2396. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2397. +
  2398. + bbr_reset_lower_bounds(sk);
  2399. + if (bbr_full_bw_reached(sk)) {
  2400. + bbr->mode = BBR_PROBE_BW;
  2401. + /* Raising inflight after PROBE_RTT may cause loss, so reset
  2402. + * the PROBE_BW clock and schedule the next bandwidth probe for
  2403. + * a friendly and randomized future point in time.
  2404. + */
  2405. + bbr_start_bw_probe_down(sk);
  2406. + /* Since we are exiting PROBE_RTT, we know inflight is
  2407. + * below our estimated BDP, so it is reasonable to cruise.
  2408. + */
  2409. + bbr_start_bw_probe_cruise(sk);
  2410. + } else {
  2411. + bbr->mode = BBR_STARTUP;
  2412. + }
  2413. +}
  2414. +
  2415. +/* Exit STARTUP based on loss rate > 1% and loss gaps in round >= N. Wait until
  2416. + * the end of the round in recovery to get a good estimate of how many packets
  2417. + * have been lost, and how many we need to drain with a low pacing rate.
  2418. + */
  2419. +static void bbr_check_loss_too_high_in_startup(struct sock *sk,
  2420. + const struct rate_sample *rs)
  2421. +{
  2422. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2423. +
  2424. + if (bbr_full_bw_reached(sk))
  2425. + return;
  2426. +
  2427. + /* For STARTUP exit, check the loss rate at the end of each round trip
  2428. + * of Recovery episodes in STARTUP. We check the loss rate at the end
  2429. + * of the round trip to filter out noisy/low loss and have a better
  2430. + * sense of inflight (extent of loss), so we can drain more accurately.
  2431. + */
  2432. + if (rs->losses && bbr->loss_events_in_round < 0xf)
  2433. + bbr->loss_events_in_round++; /* update saturating counter */
  2434. + if (bbr_param(sk, full_loss_cnt) && bbr->loss_round_start &&
  2435. + inet_csk(sk)->icsk_ca_state == TCP_CA_Recovery &&
  2436. + bbr->loss_events_in_round >= bbr_param(sk, full_loss_cnt) &&
  2437. + bbr_is_inflight_too_high(sk, rs)) {
  2438. + bbr_handle_queue_too_high_in_startup(sk);
  2439. + return;
  2440. + }
  2441. + if (bbr->loss_round_start)
  2442. + bbr->loss_events_in_round = 0;
  2443. +}
  2444. +
  2445. +/* Estimate when the pipe is full, using the change in delivery rate: BBR
  2446. + * estimates bw probing filled the pipe if the estimated bw hasn't changed by
  2447. + * at least bbr_full_bw_thresh (25%) after bbr_full_bw_cnt (3) non-app-limited
  2448. + * rounds. Why 3 rounds: 1: rwin autotuning grows the rwin, 2: we fill the
  2449. + * higher rwin, 3: we get higher delivery rate samples. Or transient
  2450. + * cross-traffic or radio noise can go away. CUBIC Hystart shares a similar
  2451. + * design goal, but uses delay and inter-ACK spacing instead of bandwidth.
  2452. + */
  2453. +static void bbr_check_full_bw_reached(struct sock *sk,
  2454. + const struct rate_sample *rs,
  2455. + struct bbr_context *ctx)
  2456. +{
  2457. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2458. + u32 bw_thresh, full_cnt, thresh;
  2459. +
  2460. + if (bbr->full_bw_now || rs->is_app_limited)
  2461. + return;
  2462. +
  2463. + thresh = bbr_param(sk, full_bw_thresh);
  2464. + full_cnt = bbr_param(sk, full_bw_cnt);
  2465. + bw_thresh = (u64)bbr->full_bw * thresh >> BBR_SCALE;
  2466. + if (ctx->sample_bw >= bw_thresh) {
  2467. + bbr_reset_full_bw(sk);
  2468. + bbr->full_bw = ctx->sample_bw;
  2469. + return;
  2470. + }
  2471. + if (!bbr->round_start)
  2472. + return;
  2473. + ++bbr->full_bw_cnt;
  2474. + bbr->full_bw_now = bbr->full_bw_cnt >= full_cnt;
  2475. + bbr->full_bw_reached |= bbr->full_bw_now;
  2476. +}
  2477. +
  2478. +/* If pipe is probably full, drain the queue and then enter steady-state. */
  2479. +static void bbr_check_drain(struct sock *sk, const struct rate_sample *rs,
  2480. + struct bbr_context *ctx)
  2481. +{
  2482. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2483. +
  2484. + if (bbr->mode == BBR_STARTUP && bbr_full_bw_reached(sk)) {
  2485. + bbr->mode = BBR_DRAIN; /* drain queue we created */
  2486. + /* Set ssthresh to export purely for monitoring, to signal
  2487. + * completion of initial STARTUP by setting to a non-
  2488. + * TCP_INFINITE_SSTHRESH value (ssthresh is not used by BBR).
  2489. + */
  2490. + tcp_sk(sk)->snd_ssthresh =
  2491. + bbr_inflight(sk, bbr_max_bw(sk), BBR_UNIT);
  2492. + bbr_reset_congestion_signals(sk);
  2493. + } /* fall through to check if in-flight is already small: */
  2494. + if (bbr->mode == BBR_DRAIN &&
  2495. + bbr_packets_in_net_at_edt(sk, tcp_packets_in_flight(tcp_sk(sk))) <=
  2496. + bbr_inflight(sk, bbr_max_bw(sk), BBR_UNIT)) {
  2497. + bbr->mode = BBR_PROBE_BW;
  2498. + bbr_start_bw_probe_down(sk);
  2499. + }
  2500. +}
  2501. +
  2502. +static void bbr_update_model(struct sock *sk, const struct rate_sample *rs,
  2503. + struct bbr_context *ctx)
  2504. +{
  2505. + bbr_update_congestion_signals(sk, rs, ctx);
  2506. + bbr_update_ack_aggregation(sk, rs);
  2507. + bbr_check_loss_too_high_in_startup(sk, rs);
  2508. + bbr_check_full_bw_reached(sk, rs, ctx);
  2509. + bbr_check_drain(sk, rs, ctx);
  2510. + bbr_update_cycle_phase(sk, rs, ctx);
  2511. + bbr_update_min_rtt(sk, rs);
  2512. +}
  2513. +
  2514. +/* Fast path for app-limited case.
  2515. + *
  2516. + * On each ack, we execute bbr state machine, which primarily consists of:
  2517. + * 1) update model based on new rate sample, and
  2518. + * 2) update control based on updated model or state change.
  2519. + *
  2520. + * There are certain workload/scenarios, e.g. app-limited case, where
  2521. + * either we can skip updating model or we can skip update of both model
  2522. + * as well as control. This provides signifcant softirq cpu savings for
  2523. + * processing incoming acks.
  2524. + *
  2525. + * In case of app-limited, if there is no congestion (loss/ecn) and
  2526. + * if observed bw sample is less than current estimated bw, then we can
  2527. + * skip some of the computation in bbr state processing:
  2528. + *
  2529. + * - if there is no rtt/mode/phase change: In this case, since all the
  2530. + * parameters of the network model are constant, we can skip model
  2531. + * as well control update.
  2532. + *
  2533. + * - else we can skip rest of the model update. But we still need to
  2534. + * update the control to account for the new rtt/mode/phase.
  2535. + *
  2536. + * Returns whether we can take fast path or not.
  2537. + */
  2538. +static bool bbr_run_fast_path(struct sock *sk, bool *update_model,
  2539. + const struct rate_sample *rs, struct bbr_context *ctx)
  2540. +{
  2541. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2542. + u32 prev_min_rtt_us, prev_mode;
  2543. +
  2544. + if (bbr_param(sk, fast_path) && bbr->try_fast_path &&
  2545. + rs->is_app_limited && ctx->sample_bw < bbr_max_bw(sk) &&
  2546. + !bbr->loss_in_round && !bbr->ecn_in_round ) {
  2547. + prev_mode = bbr->mode;
  2548. + prev_min_rtt_us = bbr->min_rtt_us;
  2549. + bbr_check_drain(sk, rs, ctx);
  2550. + bbr_update_cycle_phase(sk, rs, ctx);
  2551. + bbr_update_min_rtt(sk, rs);
  2552. +
  2553. + if (bbr->mode == prev_mode &&
  2554. + bbr->min_rtt_us == prev_min_rtt_us &&
  2555. + bbr->try_fast_path) {
  2556. + return true;
  2557. + }
  2558. +
  2559. + /* Skip model update, but control still needs to be updated */
  2560. + *update_model = false;
  2561. + }
  2562. + return false;
  2563. +}
  2564. +
  2565. +static void bbr3_main(struct sock *sk, const struct rate_sample *rs)
  2566. +{
  2567. + struct tcp_sock *tp = tcp_sk(sk);
  2568. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2569. + struct bbr_context ctx = { 0 };
  2570. + bool update_model = true;
  2571. + u32 bw, round_delivered;
  2572. + int ce_ratio = -1;
  2573. +
  2574. + if(!bbr || !bbr->initialized)
  2575. + return;
  2576. +
  2577. + round_delivered = bbr_update_round_start(sk, rs, &ctx);
  2578. + if (bbr->round_start) {
  2579. + bbr->rounds_since_probe =
  2580. + min_t(s32, bbr->rounds_since_probe + 1, 0xFF);
  2581. + ce_ratio = bbr_update_ecn_alpha(sk);
  2582. + }
  2583. + bbr_plb(sk, rs, ce_ratio);
  2584. +
  2585. + bbr->ecn_in_round |= (bbr->ecn_eligible && rs->is_ece);
  2586. + bbr_calculate_bw_sample(sk, rs, &ctx);
  2587. + bbr_update_latest_delivery_signals(sk, rs, &ctx);
  2588. +
  2589. + if (bbr_run_fast_path(sk, &update_model, rs, &ctx))
  2590. + goto out;
  2591. +
  2592. + if (update_model)
  2593. + bbr_update_model(sk, rs, &ctx);
  2594. +
  2595. + bbr_update_gains(sk);
  2596. + bw = bbr_bw(sk);
  2597. + bbr_set_pacing_rate(sk, bw, bbr->pacing_gain);
  2598. + bbr_set_cwnd(sk, rs, rs->acked_sacked, bw, bbr->cwnd_gain,
  2599. + tcp_snd_cwnd(tp), &ctx);
  2600. + bbr_bound_cwnd_for_inflight_model(sk);
  2601. +
  2602. +out:
  2603. + bbr_advance_latest_delivery_signals(sk, rs, &ctx);
  2604. + bbr->prev_ca_state = inet_csk(sk)->icsk_ca_state;
  2605. + bbr->loss_in_cycle |= rs->lost > 0;
  2606. + bbr->ecn_in_cycle |= rs->delivered_ce > 0;
  2607. +}
  2608. +
  2609. +static void bbr3_init(struct sock *sk)
  2610. +{
  2611. + struct tcp_sock *tp = tcp_sk(sk);
  2612. + struct bbr3 **bbr3_ptr = (struct bbr3 **)inet_csk_ca(sk);
  2613. + struct bbr3 *bbr;
  2614. +
  2615. + bbr = kmalloc(sizeof(struct bbr3), GFP_ATOMIC);
  2616. + if (unlikely(!bbr))
  2617. + return;
  2618. +
  2619. + memset(bbr, 0, sizeof(struct bbr3));
  2620. +
  2621. + *bbr3_ptr = bbr;
  2622. +
  2623. + bbr->skb_marked_lost = bbr3_skb_marked_lost;
  2624. +
  2625. + bbr->initialized = 1;
  2626. +
  2627. + bbr->init_cwnd = min(0x7FU, tcp_snd_cwnd(tp));
  2628. + bbr->prior_cwnd = tp->prior_cwnd;
  2629. + tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
  2630. + bbr->next_rtt_delivered = tp->delivered;
  2631. + bbr->prev_ca_state = TCP_CA_Open;
  2632. +
  2633. + bbr->probe_rtt_done_stamp = 0;
  2634. + bbr->probe_rtt_round_done = 0;
  2635. + bbr->probe_rtt_min_us = tcp_min_rtt(tp);
  2636. + bbr->probe_rtt_min_stamp = tcp_jiffies32;
  2637. + bbr->min_rtt_us = tcp_min_rtt(tp);
  2638. + bbr->min_rtt_stamp = tcp_jiffies32;
  2639. +
  2640. + bbr->has_seen_rtt = 0;
  2641. + bbr_init_pacing_rate_from_rtt(sk);
  2642. +
  2643. + bbr->round_start = 0;
  2644. + bbr->idle_restart = 0;
  2645. + bbr->full_bw_reached = 0;
  2646. + bbr->full_bw = 0;
  2647. + bbr->full_bw_cnt = 0;
  2648. + bbr->cycle_mstamp = 0;
  2649. + bbr->cycle_idx = 0;
  2650. +
  2651. + bbr_reset_startup_mode(sk);
  2652. +
  2653. + bbr->ack_epoch_mstamp = tp->tcp_mstamp;
  2654. + bbr->ack_epoch_acked = 0;
  2655. + bbr->extra_acked_win_rtts = 0;
  2656. + bbr->extra_acked_win_idx = 0;
  2657. + bbr->extra_acked[0] = 0;
  2658. + bbr->extra_acked[1] = 0;
  2659. +
  2660. + bbr->ce_state = 0;
  2661. + bbr->prior_rcv_nxt = tp->rcv_nxt;
  2662. + bbr->try_fast_path = 0;
  2663. +
  2664. + cmpxchg(&sk->sk_pacing_status, SK_PACING_NONE, SK_PACING_NEEDED);
  2665. +
  2666. + /* Start sampling ECN mark rate after first full flight is ACKed: */
  2667. + bbr->loss_round_delivered = tp->delivered + 1;
  2668. + bbr->loss_round_start = 0;
  2669. + bbr->undo_bw_lo = 0;
  2670. + bbr->undo_inflight_lo = 0;
  2671. + bbr->undo_inflight_hi = 0;
  2672. + bbr->loss_events_in_round = 0;
  2673. + bbr->startup_ecn_rounds = 0;
  2674. + bbr_reset_congestion_signals(sk);
  2675. + bbr->bw_lo = ~0U;
  2676. + bbr->bw_hi[0] = 0;
  2677. + bbr->bw_hi[1] = 0;
  2678. + bbr->inflight_lo = ~0U;
  2679. + bbr->inflight_hi = ~0U;
  2680. + bbr_reset_full_bw(sk);
  2681. + bbr->bw_probe_up_cnt = ~0U;
  2682. + bbr->bw_probe_up_acks = 0;
  2683. + bbr->bw_probe_up_rounds = 0;
  2684. + bbr->probe_wait_us = 0;
  2685. + bbr->stopped_risky_probe = 0;
  2686. + bbr->ack_phase = BBR_ACKS_INIT;
  2687. + bbr->rounds_since_probe = 0;
  2688. + bbr->bw_probe_samples = 0;
  2689. + bbr->prev_probe_too_high = 0;
  2690. + bbr->ecn_eligible = 0;
  2691. + bbr->ecn_alpha = bbr_param(sk, ecn_alpha_init);
  2692. + bbr->alpha_last_delivered = 0;
  2693. + bbr->alpha_last_delivered_ce = 0;
  2694. + bbr->plb.pause_until = 0;
  2695. +
  2696. + tp->fast_ack_mode = bbr_fast_ack_mode ? 1 : 0;
  2697. +
  2698. + if (bbr_can_use_ecn(sk))
  2699. + tp->ecn_flags |= TCP_ECN_ECT_PERMANENT;
  2700. +}
  2701. +
  2702. +__used noinline static void bbr3_release(struct sock *sk)
  2703. +{
  2704. + struct bbr3 **bbr3_ptr = (struct bbr3 **)inet_csk_ca(sk);
  2705. +
  2706. + if (*bbr3_ptr) {
  2707. + kfree(*bbr3_ptr);
  2708. + *bbr3_ptr = NULL;
  2709. + }
  2710. +}
  2711. +
  2712. +/* BBR marks the current round trip as a loss round. */
  2713. +static void bbr_note_loss(struct sock *sk)
  2714. +{
  2715. + struct tcp_sock *tp = tcp_sk(sk);
  2716. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2717. +
  2718. + /* Capture "current" data over the full round trip of loss, to
  2719. + * have a better chance of observing the full capacity of the path.
  2720. + */
  2721. + if (!bbr->loss_in_round) /* first loss in this round trip? */
  2722. + bbr->loss_round_delivered = tp->delivered; /* set round trip */
  2723. + bbr->loss_in_round = 1;
  2724. + bbr->loss_in_cycle = 1;
  2725. +}
  2726. +
  2727. +/* Core TCP stack informs us that the given skb was just marked lost. */
  2728. +static void bbr3_skb_marked_lost(struct sock *sk,
  2729. + const struct sk_buff *skb)
  2730. +{
  2731. + struct tcp_sock *tp = tcp_sk(sk);
  2732. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2733. + struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
  2734. + struct rate_sample rs = {};
  2735. +
  2736. + bbr_note_loss(sk);
  2737. +
  2738. + if (!bbr->bw_probe_samples)
  2739. + return; /* not an skb sent while probing for bandwidth */
  2740. + if (unlikely(!scb->tx.delivered_mstamp))
  2741. + return; /* skb was SACKed, reneged, marked lost; ignore it */
  2742. + /* We are probing for bandwidth. Construct a rate sample that
  2743. + * estimates what happened in the flight leading up to this lost skb,
  2744. + * then see if the loss rate went too high, and if so at which packet.
  2745. + */
  2746. + rs.tx_in_flight = scb->tx.in_flight;
  2747. + rs.lost = tp->lost - scb->tx.lost;
  2748. + rs.is_app_limited = scb->tx.is_app_limited;
  2749. + if (bbr_is_inflight_too_high(sk, &rs)) {
  2750. + rs.tx_in_flight = bbr_inflight_hi_from_lost_skb(sk, &rs, skb);
  2751. + bbr_handle_inflight_too_high(sk, &rs);
  2752. + }
  2753. +}
  2754. +
  2755. +static void bbr_run_loss_probe_recovery(struct sock *sk)
  2756. +{
  2757. + struct tcp_sock *tp = tcp_sk(sk);
  2758. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2759. + struct rate_sample rs = {0};
  2760. +
  2761. + bbr_note_loss(sk);
  2762. +
  2763. + if (!bbr->bw_probe_samples)
  2764. + return; /* not sent while probing for bandwidth */
  2765. + /* We are probing for bandwidth. Construct a rate sample that
  2766. + * estimates what happened in the flight leading up to this
  2767. + * loss, then see if the loss rate went too high.
  2768. + */
  2769. + rs.lost = 1; /* TLP probe repaired loss of a single segment */
  2770. + rs.tx_in_flight = bbr->inflight_latest + rs.lost;
  2771. + rs.is_app_limited = tp->tlp_orig_data_app_limited;
  2772. + if (bbr_is_inflight_too_high(sk, &rs))
  2773. + bbr_handle_inflight_too_high(sk, &rs);
  2774. +}
  2775. +
  2776. +/* Revert short-term model if current loss recovery event was spurious. */
  2777. +static u32 bbr3_undo_cwnd(struct sock *sk)
  2778. +{
  2779. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2780. +
  2781. + if(!bbr || !bbr->initialized)
  2782. + return tcp_sk(sk)->snd_cwnd;
  2783. +
  2784. + bbr_reset_full_bw(sk); /* spurious slow-down; reset full bw detector */
  2785. + bbr->loss_in_round = 0;
  2786. +
  2787. + /* Revert to cwnd and other state saved before loss episode. */
  2788. + bbr->bw_lo = max(bbr->bw_lo, bbr->undo_bw_lo);
  2789. + bbr->inflight_lo = max(bbr->inflight_lo, bbr->undo_inflight_lo);
  2790. + bbr->inflight_hi = max(bbr->inflight_hi, bbr->undo_inflight_hi);
  2791. + bbr->try_fast_path = 0; /* take slow path to set proper cwnd, pacing */
  2792. + return bbr->prior_cwnd;
  2793. +}
  2794. +
  2795. +/* Entering loss recovery, so save state for when we undo recovery. */
  2796. +static u32 bbr3_ssthresh(struct sock *sk)
  2797. +{
  2798. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2799. +
  2800. + if(!bbr || !bbr->initialized)
  2801. + return tcp_sk(sk)->snd_ssthresh;
  2802. +
  2803. + bbr_save_cwnd(sk);
  2804. + /* For undo, save state that adapts based on loss signal. */
  2805. + bbr->undo_bw_lo = bbr->bw_lo;
  2806. + bbr->undo_inflight_lo = bbr->inflight_lo;
  2807. + bbr->undo_inflight_hi = bbr->inflight_hi;
  2808. + return tcp_sk(sk)->snd_ssthresh;
  2809. +}
  2810. +
  2811. +static enum tcp_bbr_phase bbr_get_phase(struct bbr3 *bbr)
  2812. +{
  2813. + switch (bbr->mode) {
  2814. + case BBR_STARTUP:
  2815. + return BBR_PHASE_STARTUP;
  2816. + case BBR_DRAIN:
  2817. + return BBR_PHASE_DRAIN;
  2818. + case BBR_PROBE_BW:
  2819. + break;
  2820. + case BBR_PROBE_RTT:
  2821. + return BBR_PHASE_PROBE_RTT;
  2822. + default:
  2823. + return BBR_PHASE_INVALID;
  2824. + }
  2825. + switch (bbr->cycle_idx) {
  2826. + case BBR_BW_PROBE_UP:
  2827. + return BBR_PHASE_PROBE_BW_UP;
  2828. + case BBR_BW_PROBE_DOWN:
  2829. + return BBR_PHASE_PROBE_BW_DOWN;
  2830. + case BBR_BW_PROBE_CRUISE:
  2831. + return BBR_PHASE_PROBE_BW_CRUISE;
  2832. + case BBR_BW_PROBE_REFILL:
  2833. + return BBR_PHASE_PROBE_BW_REFILL;
  2834. + default:
  2835. + return BBR_PHASE_INVALID;
  2836. + }
  2837. +}
  2838. +
  2839. +static size_t bbr3_get_info(struct sock *sk, u32 ext, int *attr,
  2840. + union tcp_cc_info *info)
  2841. +{
  2842. + if (ext & (1 << (INET_DIAG_BBRINFO - 1)) ||
  2843. + ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
  2844. + struct tcp_bbr_info *bbr_info;
  2845. + u64 bw, bw_hi, bw_lo;
  2846. +
  2847. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2848. + if(!bbr || !bbr->initialized)
  2849. + return 0;
  2850. +
  2851. + bw = bbr_bw_bytes_per_sec(sk, bbr_bw(sk));
  2852. + bw_hi = bbr_bw_bytes_per_sec(sk, bbr_max_bw(sk));
  2853. + bw_lo = bbr->bw_lo == ~0U ?
  2854. + ~0ULL : bbr_bw_bytes_per_sec(sk, bbr->bw_lo);
  2855. + bbr_info = &info->bbr;
  2856. +
  2857. + memset(bbr_info, 0, sizeof(*bbr_info));
  2858. + bbr_info->bbr_bw_lo = (u32)bw;
  2859. + bbr_info->bbr_bw_hi = (u32)(bw >> 32);
  2860. + bbr_info->bbr_min_rtt = bbr->min_rtt_us;
  2861. + bbr_info->bbr_pacing_gain = bbr->pacing_gain;
  2862. + bbr_info->bbr_cwnd_gain = bbr->cwnd_gain;
  2863. + bbr_info->bbr_bw_hi_lsb = (u32)bw_hi;
  2864. + bbr_info->bbr_bw_hi_msb = (u32)(bw_hi >> 32);
  2865. + bbr_info->bbr_bw_lo_lsb = (u32)bw_lo;
  2866. + bbr_info->bbr_bw_lo_msb = (u32)(bw_lo >> 32);
  2867. + bbr_info->bbr_mode = bbr->mode;
  2868. + bbr_info->bbr_phase = (__u8)bbr_get_phase(bbr);
  2869. + bbr_info->bbr_version = (__u8)BBR_VERSION;
  2870. + bbr_info->bbr_inflight_lo = bbr->inflight_lo;
  2871. + bbr_info->bbr_inflight_hi = bbr->inflight_hi;
  2872. + bbr_info->bbr_extra_acked = bbr_extra_acked(sk);
  2873. + *attr = INET_DIAG_BBRINFO;
  2874. + return sizeof(*bbr_info);
  2875. + }
  2876. + return 0;
  2877. +}
  2878. +
  2879. +static void bbr3_set_state(struct sock *sk, u8 new_state)
  2880. +{
  2881. + struct tcp_sock *tp = tcp_sk(sk);
  2882. + struct bbr3 *bbr = bbr3_get_priv(sk);
  2883. +
  2884. + if(!bbr || !bbr->initialized)
  2885. + return;
  2886. +
  2887. + if (new_state == TCP_CA_Loss) {
  2888. +
  2889. + bbr->prev_ca_state = TCP_CA_Loss;
  2890. + tcp_plb_update_state_upon_rto(sk, &bbr->plb);
  2891. + /* The tcp_write_timeout() call to sk_rethink_txhash() likely
  2892. + * repathed this flow, so re-learn the min network RTT on the
  2893. + * new path:
  2894. + */
  2895. + bbr_reset_full_bw(sk);
  2896. + if (!bbr_is_probing_bandwidth(sk) && bbr->inflight_lo == ~0U) {
  2897. + /* bbr_adapt_lower_bounds() needs cwnd before
  2898. + * we suffered an RTO, to update inflight_lo:
  2899. + */
  2900. + bbr->inflight_lo =
  2901. + max(tcp_snd_cwnd(tp), bbr->prior_cwnd);
  2902. + }
  2903. + } else if (bbr->prev_ca_state == TCP_CA_Loss &&
  2904. + new_state != TCP_CA_Loss) {
  2905. + bbr_exit_loss_recovery(sk);
  2906. + }
  2907. +}
  2908. +
  2909. +
  2910. +static struct tcp_congestion_ops tcp_bbr_cong_ops __read_mostly = {
  2911. + .flags = TCP_CONG_NON_RESTRICTED | TCP_CONG_WANTS_CE_EVENTS,
  2912. + .name = "bbr3",
  2913. + .owner = THIS_MODULE,
  2914. + .init = bbr3_init,
  2915. + .release = bbr3_release,
  2916. + .cong_control = bbr3_main,
  2917. + .sndbuf_expand = bbr3_sndbuf_expand,
  2918. + .undo_cwnd = bbr3_undo_cwnd,
  2919. + .cwnd_event = bbr3_cwnd_event,
  2920. + .ssthresh = bbr3_ssthresh,
  2921. + .min_tso_segs = bbr3_tso_segs,
  2922. + .get_info = bbr3_get_info,
  2923. + .set_state = bbr3_set_state,
  2924. +};
  2925. +
  2926. +static int __init bbr3_register(void)
  2927. +{
  2928. + return tcp_register_congestion_control(&tcp_bbr_cong_ops);
  2929. +}
  2930. +
  2931. +static void __exit bbr3_unregister(void)
  2932. +{
  2933. + tcp_unregister_congestion_control(&tcp_bbr_cong_ops);
  2934. +}
  2935. +
  2936. +module_init(bbr3_register);
  2937. +module_exit(bbr3_unregister);
  2938. +
  2939. +MODULE_AUTHOR("Van Jacobson <vanj@google.com>");
  2940. +MODULE_AUTHOR("Neal Cardwell <ncardwell@google.com>");
  2941. +MODULE_AUTHOR("Yuchung Cheng <ycheng@google.com>");
  2942. +MODULE_AUTHOR("Soheil Hassas Yeganeh <soheil@google.com>");
  2943. +MODULE_AUTHOR("Priyaranjan Jha <priyarjha@google.com>");
  2944. +MODULE_AUTHOR("Yousuk Seung <ysseung@google.com>");
  2945. +MODULE_AUTHOR("Kevin Yang <yyd@google.com>");
  2946. +MODULE_AUTHOR("Arjun Roy <arjunroy@google.com>");
  2947. +MODULE_AUTHOR("David Morley <morleyd@google.com>");
  2948. +
  2949. +MODULE_LICENSE("Dual BSD/GPL");
  2950. +MODULE_DESCRIPTION("TCP BBR (Bottleneck Bandwidth and RTT)");
  2951. +MODULE_VERSION(__stringify(BBR_VERSION));
  2952. diff --git a/net/ipv4/tcp_cong.c b/net/ipv4/tcp_cong.c
  2953. index f43db30a7..d2cd30d4e 100644
  2954. --- a/net/ipv4/tcp_cong.c
  2955. +++ b/net/ipv4/tcp_cong.c
  2956. @@ -179,6 +179,7 @@ void tcp_init_congestion_control(struct sock *sk)
  2957. struct inet_connection_sock *icsk = inet_csk(sk);
  2958. tcp_sk(sk)->prior_ssthresh = 0;
  2959. + tcp_sk(sk)->fast_ack_mode = 0;
  2960. if (icsk->icsk_ca_ops->init)
  2961. icsk->icsk_ca_ops->init(sk);
  2962. if (tcp_ca_needs_ecn(sk))
  2963. diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
  2964. index 7925cff2f..6d7c8a25e 100644
  2965. --- a/net/ipv4/tcp_input.c
  2966. +++ b/net/ipv4/tcp_input.c
  2967. @@ -362,7 +362,7 @@ static void __tcp_ecn_check_ce(struct sock *sk, const struct sk_buff *skb)
  2968. tcp_enter_quickack_mode(sk, 2);
  2969. break;
  2970. case INET_ECN_CE:
  2971. - if (tcp_ca_needs_ecn(sk))
  2972. + if (tcp_ca_wants_ce_events(sk))
  2973. tcp_ca_event(sk, CA_EVENT_ECN_IS_CE);
  2974. if (!(tp->ecn_flags & TCP_ECN_DEMAND_CWR)) {
  2975. @@ -373,7 +373,7 @@ static void __tcp_ecn_check_ce(struct sock *sk, const struct sk_buff *skb)
  2976. tp->ecn_flags |= TCP_ECN_SEEN;
  2977. break;
  2978. default:
  2979. - if (tcp_ca_needs_ecn(sk))
  2980. + if (tcp_ca_wants_ce_events(sk))
  2981. tcp_ca_event(sk, CA_EVENT_ECN_NO_CE);
  2982. tp->ecn_flags |= TCP_ECN_SEEN;
  2983. break;
  2984. @@ -1093,7 +1093,17 @@ static void tcp_verify_retransmit_hint(struct tcp_sock *tp, struct sk_buff *skb)
  2985. */
  2986. static void tcp_notify_skb_loss_event(struct tcp_sock *tp, const struct sk_buff *skb)
  2987. {
  2988. + struct sock *sk = (struct sock *)tp;
  2989. + const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops;
  2990. + struct bbr3 *bbr3 = NULL;
  2991. +
  2992. tp->lost += tcp_skb_pcount(skb);
  2993. + if(ca_ops && strncmp(ca_ops->name, "bbr3", 4) == 0)
  2994. + {
  2995. + bbr3 = *(struct bbr3 **)inet_csk_ca(sk);
  2996. + if (bbr3 && bbr3->skb_marked_lost)
  2997. + bbr3->skb_marked_lost(sk, skb);
  2998. + }
  2999. }
  3000. void tcp_mark_skb_lost(struct sock *sk, struct sk_buff *skb)
  3001. @@ -1465,6 +1475,17 @@ static bool tcp_shifted_skb(struct sock *sk, struct sk_buff *prev,
  3002. WARN_ON_ONCE(tcp_skb_pcount(skb) < pcount);
  3003. tcp_skb_pcount_add(skb, -pcount);
  3004. + /* Adjust tx.in_flight as pcount is shifted from skb to prev. */
  3005. + if (WARN_ONCE(TCP_SKB_CB(skb)->tx.in_flight < pcount,
  3006. + "prev in_flight: %u skb in_flight: %u pcount: %u",
  3007. + TCP_SKB_CB(prev)->tx.in_flight,
  3008. + TCP_SKB_CB(skb)->tx.in_flight,
  3009. + pcount))
  3010. + TCP_SKB_CB(skb)->tx.in_flight = 0;
  3011. + else
  3012. + TCP_SKB_CB(skb)->tx.in_flight -= pcount;
  3013. + TCP_SKB_CB(prev)->tx.in_flight += pcount;
  3014. +
  3015. /* When we're adding to gso_segs == 1, gso_size will be zero,
  3016. * in theory this shouldn't be necessary but as long as DSACK
  3017. * code can come after this skb later on it's better to keep
  3018. @@ -3759,7 +3780,8 @@ static void tcp_replace_ts_recent(struct tcp_sock *tp, u32 seq)
  3019. /* This routine deals with acks during a TLP episode and ends an episode by
  3020. * resetting tlp_high_seq. Ref: TLP algorithm in draft-ietf-tcpm-rack
  3021. */
  3022. -static void tcp_process_tlp_ack(struct sock *sk, u32 ack, int flag)
  3023. +static void tcp_process_tlp_ack(struct sock *sk, u32 ack, int flag,
  3024. + struct rate_sample *rs)
  3025. {
  3026. struct tcp_sock *tp = tcp_sk(sk);
  3027. @@ -3776,6 +3798,7 @@ static void tcp_process_tlp_ack(struct sock *sk, u32 ack, int flag)
  3028. /* ACK advances: there was a loss, so reduce cwnd. Reset
  3029. * tlp_high_seq in tcp_init_cwnd_reduction()
  3030. */
  3031. + tcp_ca_event(sk, CA_EVENT_TLP_RECOVERY);
  3032. tcp_init_cwnd_reduction(sk);
  3033. tcp_set_ca_state(sk, TCP_CA_CWR);
  3034. tcp_end_cwnd_reduction(sk);
  3035. @@ -3786,6 +3809,11 @@ static void tcp_process_tlp_ack(struct sock *sk, u32 ack, int flag)
  3036. FLAG_NOT_DUP | FLAG_DATA_SACKED))) {
  3037. /* Pure dupack: original and TLP probe arrived; no loss */
  3038. tp->tlp_high_seq = 0;
  3039. + } else {
  3040. + /* This ACK matches a TLP retransmit. We cannot yet tell if
  3041. + * this ACK is for the original or the TLP retransmit.
  3042. + */
  3043. + rs->is_acking_tlp_retrans_seq = 1;
  3044. }
  3045. }
  3046. @@ -3905,6 +3933,7 @@ static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
  3047. prior_fack = tcp_is_sack(tp) ? tcp_highest_sack_seq(tp) : tp->snd_una;
  3048. rs.prior_in_flight = tcp_packets_in_flight(tp);
  3049. + tcp_rate_check_app_limited(sk);
  3050. /* ts_recent update must be made after we are sure that the packet
  3051. * is in window.
  3052. @@ -3970,7 +3999,7 @@ static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
  3053. tcp_in_ack_event(sk, flag);
  3054. if (tp->tlp_high_seq)
  3055. - tcp_process_tlp_ack(sk, ack, flag);
  3056. + tcp_process_tlp_ack(sk, ack, flag, &rs);
  3057. if (tcp_ack_is_dubious(sk, flag)) {
  3058. if (!(flag & (FLAG_SND_UNA_ADVANCED |
  3059. @@ -3994,6 +4023,7 @@ static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
  3060. delivered = tcp_newly_delivered(sk, delivered, flag);
  3061. lost = tp->lost - lost; /* freshly marked lost */
  3062. rs.is_ack_delayed = !!(flag & FLAG_ACK_MAYBE_DELAYED);
  3063. + rs.is_ece = !!(flag & FLAG_ECE);
  3064. tcp_rate_gen(sk, delivered, lost, is_sack_reneg, sack_state.rate);
  3065. tcp_cong_control(sk, ack, delivered, flag, sack_state.rate);
  3066. tcp_xmit_recovery(sk, rexmit);
  3067. @@ -4014,7 +4044,7 @@ static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
  3068. tcp_ack_probe(sk);
  3069. if (tp->tlp_high_seq)
  3070. - tcp_process_tlp_ack(sk, ack, flag);
  3071. + tcp_process_tlp_ack(sk, ack, flag, &rs);
  3072. return 1;
  3073. old_ack:
  3074. @@ -5585,13 +5615,14 @@ static void __tcp_ack_snd_check(struct sock *sk, int ofo_possible)
  3075. /* More than one full frame received... */
  3076. if (((tp->rcv_nxt - tp->rcv_wup) > inet_csk(sk)->icsk_ack.rcv_mss &&
  3077. + (tp->fast_ack_mode == 1 ||
  3078. /* ... and right edge of window advances far enough.
  3079. * (tcp_recvmsg() will send ACK otherwise).
  3080. * If application uses SO_RCVLOWAT, we want send ack now if
  3081. * we have not received enough bytes to satisfy the condition.
  3082. */
  3083. (tp->rcv_nxt - tp->copied_seq < sk->sk_rcvlowat ||
  3084. - __tcp_select_window(sk) >= tp->rcv_wnd)) ||
  3085. + __tcp_select_window(sk) >= tp->rcv_wnd))) ||
  3086. /* We ACK each frame or... */
  3087. tcp_in_quickack_mode(sk) ||
  3088. /* Protocol state mandates a one-time immediate ACK */
  3089. diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
  3090. index ca3ffe46a..9df881f5d 100644
  3091. --- a/net/ipv4/tcp_ipv4.c
  3092. +++ b/net/ipv4/tcp_ipv4.c
  3093. @@ -3144,7 +3144,8 @@ static void __net_exit tcp_sk_exit(struct net *net)
  3094. static int __net_init tcp_sk_init(struct net *net)
  3095. {
  3096. - int cnt;
  3097. + int cnt;
  3098. + struct tcp_plb_net_context *ctx, *init_ctx;
  3099. net->ipv4.sysctl_tcp_ecn = 2;
  3100. net->ipv4.sysctl_tcp_ecn_fallback = 1;
  3101. @@ -3219,6 +3220,18 @@ static int __net_init tcp_sk_init(struct net *net)
  3102. net->ipv4.sysctl_tcp_fastopen_blackhole_timeout = 0;
  3103. atomic_set(&net->ipv4.tfo_active_disable_times, 0);
  3104. + ctx = tcp_get_plb_ctx(net);
  3105. + init_ctx = tcp_get_plb_ctx(&init_net);
  3106. +
  3107. + if (ctx && init_ctx && !net_eq(net, &init_net))
  3108. + {
  3109. + ctx->params.sysctl_tcp_plb_enabled = init_ctx->params.sysctl_tcp_plb_enabled;
  3110. + ctx->params.sysctl_tcp_plb_idle_rehash_rounds = init_ctx->params.sysctl_tcp_plb_idle_rehash_rounds;
  3111. + ctx->params.sysctl_tcp_plb_rehash_rounds = init_ctx->params.sysctl_tcp_plb_rehash_rounds;
  3112. + ctx->params.sysctl_tcp_plb_suspend_rto_sec = init_ctx->params.sysctl_tcp_plb_suspend_rto_sec;
  3113. + ctx->params.sysctl_tcp_plb_cong_thresh = init_ctx->params.sysctl_tcp_plb_cong_thresh;
  3114. + }
  3115. +
  3116. /* Reno is always built in */
  3117. if (!net_eq(net, &init_net) &&
  3118. bpf_try_module_get(init_net.ipv4.tcp_congestion_control,
  3119. diff --git a/net/ipv4/tcp_minisocks.c b/net/ipv4/tcp_minisocks.c
  3120. index f92a78c04..c0ee5ed34 100644
  3121. --- a/net/ipv4/tcp_minisocks.c
  3122. +++ b/net/ipv4/tcp_minisocks.c
  3123. @@ -409,6 +409,8 @@ void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst)
  3124. u32 ca_key = dst_metric(dst, RTAX_CC_ALGO);
  3125. bool ca_got_dst = false;
  3126. + tcp_set_ecn_low_from_dst(sk, dst);
  3127. +
  3128. if (ca_key != TCP_CA_UNSPEC) {
  3129. const struct tcp_congestion_ops *ca;
  3130. diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c
  3131. index dd63832c1..9116a5455 100644
  3132. --- a/net/ipv4/tcp_output.c
  3133. +++ b/net/ipv4/tcp_output.c
  3134. @@ -322,10 +322,9 @@ static void tcp_ecn_send_syn(struct sock *sk, struct sk_buff *skb)
  3135. bool bpf_needs_ecn = tcp_bpf_ca_needs_ecn(sk);
  3136. bool use_ecn = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_ecn) == 1 ||
  3137. tcp_ca_needs_ecn(sk) || bpf_needs_ecn;
  3138. + const struct dst_entry *dst = __sk_dst_get(sk);
  3139. if (!use_ecn) {
  3140. - const struct dst_entry *dst = __sk_dst_get(sk);
  3141. -
  3142. if (dst && dst_feature(dst, RTAX_FEATURE_ECN))
  3143. use_ecn = true;
  3144. }
  3145. @@ -337,6 +336,9 @@ static void tcp_ecn_send_syn(struct sock *sk, struct sk_buff *skb)
  3146. tp->ecn_flags = TCP_ECN_OK;
  3147. if (tcp_ca_needs_ecn(sk) || bpf_needs_ecn)
  3148. INET_ECN_xmit(sk);
  3149. +
  3150. + if (dst)
  3151. + tcp_set_ecn_low_from_dst(sk, dst);
  3152. }
  3153. }
  3154. @@ -374,7 +376,8 @@ static void tcp_ecn_send(struct sock *sk, struct sk_buff *skb,
  3155. th->cwr = 1;
  3156. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  3157. }
  3158. - } else if (!tcp_ca_needs_ecn(sk)) {
  3159. + } else if (!(tp->ecn_flags & TCP_ECN_ECT_PERMANENT) &&
  3160. + !tcp_ca_needs_ecn(sk)) {
  3161. /* ACK or retransmitted segment: clear ECT|CE */
  3162. INET_ECN_dontxmit(sk);
  3163. }
  3164. @@ -1535,7 +1538,7 @@ int tcp_fragment(struct sock *sk, enum tcp_queue tcp_queue,
  3165. {
  3166. struct tcp_sock *tp = tcp_sk(sk);
  3167. struct sk_buff *buff;
  3168. - int nsize, old_factor;
  3169. + int nsize, old_factor, inflight_prev;
  3170. long limit;
  3171. int nlen;
  3172. u8 flags;
  3173. @@ -1614,6 +1617,30 @@ int tcp_fragment(struct sock *sk, enum tcp_queue tcp_queue,
  3174. if (diff)
  3175. tcp_adjust_pcount(sk, skb, diff);
  3176. +
  3177. + inflight_prev = TCP_SKB_CB(skb)->tx.in_flight - old_factor;
  3178. + if (inflight_prev < 0) {
  3179. + WARN_ONCE(tcp_skb_tx_in_flight_is_suspicious(
  3180. + old_factor,
  3181. + TCP_SKB_CB(skb)->sacked,
  3182. + TCP_SKB_CB(skb)->tx.in_flight),
  3183. + "inconsistent: tx.in_flight: %u "
  3184. + "old_factor: %d mss: %u sacked: %u "
  3185. + "1st pcount: %d 2nd pcount: %d "
  3186. + "1st len: %u 2nd len: %u ",
  3187. + TCP_SKB_CB(skb)->tx.in_flight, old_factor,
  3188. + mss_now, TCP_SKB_CB(skb)->sacked,
  3189. + tcp_skb_pcount(skb), tcp_skb_pcount(buff),
  3190. + skb->len, buff->len);
  3191. + inflight_prev = 0;
  3192. + }
  3193. + /* Set 1st tx.in_flight as if 1st were sent by itself: */
  3194. + TCP_SKB_CB(skb)->tx.in_flight = inflight_prev +
  3195. + tcp_skb_pcount(skb);
  3196. + /* Set 2nd tx.in_flight with new 1st and 2nd pcounts: */
  3197. + TCP_SKB_CB(buff)->tx.in_flight = inflight_prev +
  3198. + tcp_skb_pcount(skb) +
  3199. + tcp_skb_pcount(buff);
  3200. }
  3201. /* Link BUFF into the send queue. */
  3202. @@ -2870,6 +2897,7 @@ void tcp_send_loss_probe(struct sock *sk)
  3203. if (WARN_ON(!skb || !tcp_skb_pcount(skb)))
  3204. goto rearm_timer;
  3205. + tp->tlp_orig_data_app_limited = TCP_SKB_CB(skb)->tx.is_app_limited;
  3206. if (__tcp_retransmit_skb(sk, skb, 1))
  3207. goto rearm_timer;
  3208. diff --git a/net/ipv4/tcp_plb.c b/net/ipv4/tcp_plb.c
  3209. new file mode 100644
  3210. index 000000000..18f4d3bad
  3211. --- /dev/null
  3212. +++ b/net/ipv4/tcp_plb.c
  3213. @@ -0,0 +1,122 @@
  3214. +/* Protective Load Balancing (PLB)
  3215. + *
  3216. + * PLB was designed to reduce link load imbalance across datacenter
  3217. + * switches. PLB is a host-based optimization; it leverages congestion
  3218. + * signals from the transport layer to randomly change the path of the
  3219. + * connection experiencing sustained congestion. PLB prefers to repath
  3220. + * after idle periods to minimize packet reordering. It repaths by
  3221. + * changing the IPv6 Flow Label on the packets of a connection, which
  3222. + * datacenter switches include as part of ECMP/WCMP hashing.
  3223. + *
  3224. + * PLB is described in detail in:
  3225. + *
  3226. + * Mubashir Adnan Qureshi, Yuchung Cheng, Qianwen Yin, Qiaobin Fu,
  3227. + * Gautam Kumar, Masoud Moshref, Junhua Yan, Van Jacobson,
  3228. + * David Wetherall,Abdul Kabbani:
  3229. + * "PLB: Congestion Signals are Simple and Effective for
  3230. + * Network Load Balancing"
  3231. + * In ACM SIGCOMM 2022, Amsterdam Netherlands.
  3232. + *
  3233. + */
  3234. +
  3235. +#include <net/tcp.h>
  3236. +
  3237. +/* Called once per round-trip to update PLB state for a connection. */
  3238. +void tcp_plb_update_state(const struct sock *sk, struct tcp_plb_state *plb,
  3239. + const int cong_ratio)
  3240. +{
  3241. + struct net *net = sock_net(sk);
  3242. + struct tcp_plb_net_context *ctx;
  3243. +
  3244. + ctx = tcp_get_plb_ctx(net);
  3245. + if (!ctx)
  3246. + return;
  3247. +
  3248. + if (!READ_ONCE(ctx->params.sysctl_tcp_plb_enabled))
  3249. + return;
  3250. +
  3251. + if (cong_ratio >= 0) {
  3252. + if (cong_ratio < READ_ONCE(ctx->params.sysctl_tcp_plb_cong_thresh))
  3253. + plb->consec_cong_rounds = 0;
  3254. + else if (plb->consec_cong_rounds <
  3255. + READ_ONCE(ctx->params.sysctl_tcp_plb_rehash_rounds))
  3256. + plb->consec_cong_rounds++;
  3257. + }
  3258. +}
  3259. +EXPORT_SYMBOL_GPL(tcp_plb_update_state);
  3260. +
  3261. +/* Check whether recent congestion has been persistent enough to warrant
  3262. + * a load balancing decision that switches the connection to another path.
  3263. + */
  3264. +void tcp_plb_check_rehash(struct sock *sk, struct tcp_plb_state *plb)
  3265. +{
  3266. + struct net *net = sock_net(sk);
  3267. + struct tcp_plb_net_context *ctx;
  3268. + u32 max_suspend;
  3269. + bool forced_rehash = false, idle_rehash = false;
  3270. +
  3271. + ctx = tcp_get_plb_ctx(net);
  3272. + if (!ctx)
  3273. + return;
  3274. +
  3275. + if (!READ_ONCE(ctx->params.sysctl_tcp_plb_enabled))
  3276. + return;
  3277. +
  3278. + forced_rehash = plb->consec_cong_rounds >=
  3279. + READ_ONCE(ctx->params.sysctl_tcp_plb_rehash_rounds);
  3280. + /* If sender goes idle then we check whether to rehash. */
  3281. + idle_rehash = READ_ONCE(ctx->params.sysctl_tcp_plb_idle_rehash_rounds) &&
  3282. + !tcp_sk(sk)->packets_out &&
  3283. + plb->consec_cong_rounds >=
  3284. + READ_ONCE(ctx->params.sysctl_tcp_plb_idle_rehash_rounds);
  3285. +
  3286. + if (!forced_rehash && !idle_rehash)
  3287. + return;
  3288. +
  3289. + /* Note that tcp_jiffies32 can wrap; we detect wraps by checking for
  3290. + * cases where the max suspension end is before the actual suspension
  3291. + * end. We clear pause_until to 0 to indicate there is no recent
  3292. + * RTO event that constrains PLB rehashing.
  3293. + */
  3294. + max_suspend = 2 * READ_ONCE(ctx->params.sysctl_tcp_plb_suspend_rto_sec) * HZ;
  3295. + if (plb->pause_until &&
  3296. + (!before(tcp_jiffies32, plb->pause_until) ||
  3297. + before(tcp_jiffies32 + max_suspend, plb->pause_until)))
  3298. + plb->pause_until = 0;
  3299. +
  3300. + if (plb->pause_until)
  3301. + return;
  3302. +
  3303. + sk_rethink_txhash(sk);
  3304. + plb->consec_cong_rounds = 0;
  3305. +}
  3306. +EXPORT_SYMBOL_GPL(tcp_plb_check_rehash);
  3307. +
  3308. +/* Upon RTO, disallow load balancing for a while, to avoid having load
  3309. + * balancing decisions switch traffic to a black-holed path that was
  3310. + * previously avoided with a sk_rethink_txhash() call at RTO time.
  3311. + */
  3312. +void tcp_plb_update_state_upon_rto(struct sock *sk, struct tcp_plb_state *plb)
  3313. +{
  3314. + struct net *net = sock_net(sk);
  3315. + struct tcp_plb_net_context *ctx;
  3316. + u32 pause;
  3317. +
  3318. + ctx = tcp_get_plb_ctx(net);
  3319. + if (!ctx)
  3320. + return;
  3321. +
  3322. + if (!READ_ONCE(ctx->params.sysctl_tcp_plb_enabled))
  3323. + return;
  3324. +
  3325. + pause = READ_ONCE(ctx->params.sysctl_tcp_plb_suspend_rto_sec) * HZ;
  3326. + pause += prandom_u32_max(pause);
  3327. + plb->pause_until = tcp_jiffies32 + pause;
  3328. +
  3329. + /* Reset PLB state upon RTO, since an RTO causes a sk_rethink_txhash() call
  3330. + * that may switch this connection to a path with completely different
  3331. + * congestion characteristics.
  3332. + */
  3333. + plb->consec_cong_rounds = 0;
  3334. +}
  3335. +EXPORT_SYMBOL_GPL(tcp_plb_update_state_upon_rto);
  3336. diff --git a/net/ipv4/tcp_rate.c b/net/ipv4/tcp_rate.c
  3337. index 042e27f54..d2a3b6f68 100644
  3338. --- a/net/ipv4/tcp_rate.c
  3339. +++ b/net/ipv4/tcp_rate.c
  3340. @@ -37,6 +37,25 @@
  3341. /* Snapshot the current delivery information in the skb, to generate
  3342. * a rate sample later when the skb is (s)acked in tcp_rate_skb_delivered().
  3343. */
  3344. +
  3345. +void tcp_set_tx_in_flight(struct sock *sk, struct sk_buff *skb)
  3346. +{
  3347. + struct tcp_sock *tp = tcp_sk(sk);
  3348. + u32 in_flight;
  3349. +
  3350. + /* Check, sanitize, and record packets in flight after skb was sent. */
  3351. + in_flight = tcp_packets_in_flight(tp) + tcp_skb_pcount(skb);
  3352. + if (WARN_ONCE(in_flight > TCPCB_IN_FLIGHT_MAX,
  3353. + "insane in_flight %u cc %s mss %u "
  3354. + "cwnd %u pif %u %u %u %u\n",
  3355. + in_flight, inet_csk(sk)->icsk_ca_ops->name,
  3356. + tp->mss_cache, tp->snd_cwnd,
  3357. + tp->packets_out, tp->retrans_out,
  3358. + tp->sacked_out, tp->lost_out))
  3359. + in_flight = TCPCB_IN_FLIGHT_MAX;
  3360. + TCP_SKB_CB(skb)->tx.in_flight = in_flight;
  3361. +}
  3362. +
  3363. void tcp_rate_skb_sent(struct sock *sk, struct sk_buff *skb)
  3364. {
  3365. struct tcp_sock *tp = tcp_sk(sk);
  3366. @@ -65,7 +84,10 @@ void tcp_rate_skb_sent(struct sock *sk, struct sk_buff *skb)
  3367. TCP_SKB_CB(skb)->tx.first_tx_mstamp = tp->first_tx_mstamp;
  3368. TCP_SKB_CB(skb)->tx.delivered_mstamp = tp->delivered_mstamp;
  3369. TCP_SKB_CB(skb)->tx.delivered = tp->delivered;
  3370. + TCP_SKB_CB(skb)->tx.delivered_ce = tp->delivered_ce;
  3371. + TCP_SKB_CB(skb)->tx.lost = tp->lost;
  3372. TCP_SKB_CB(skb)->tx.is_app_limited = tp->app_limited ? 1 : 0;
  3373. + tcp_set_tx_in_flight(sk, skb);
  3374. }
  3375. /* When an skb is sacked or acked, we fill in the rate sample with the (prior)
  3376. @@ -90,16 +112,19 @@ void tcp_rate_skb_delivered(struct sock *sk, struct sk_buff *skb,
  3377. if (!rs->prior_delivered ||
  3378. tcp_skb_sent_after(tx_tstamp, tp->first_tx_mstamp,
  3379. scb->end_seq, rs->last_end_seq)) {
  3380. + rs->prior_lost = scb->tx.lost;
  3381. + rs->prior_delivered_ce = scb->tx.delivered_ce;
  3382. rs->prior_delivered = scb->tx.delivered;
  3383. rs->prior_mstamp = scb->tx.delivered_mstamp;
  3384. rs->is_app_limited = scb->tx.is_app_limited;
  3385. rs->is_retrans = scb->sacked & TCPCB_RETRANS;
  3386. + rs->tx_in_flight = scb->tx.in_flight;
  3387. rs->last_end_seq = scb->end_seq;
  3388. /* Record send time of most recently ACKed packet: */
  3389. tp->first_tx_mstamp = tx_tstamp;
  3390. /* Find the duration of the "send phase" of this window: */
  3391. - rs->interval_us = tcp_stamp_us_delta(tp->first_tx_mstamp,
  3392. + rs->interval_us = tcp_stamp32_us_delta(tp->first_tx_mstamp,
  3393. scb->tx.first_tx_mstamp);
  3394. }
  3395. @@ -142,6 +167,11 @@ void tcp_rate_gen(struct sock *sk, u32 delivered, u32 lost,
  3396. return;
  3397. }
  3398. rs->delivered = tp->delivered - rs->prior_delivered;
  3399. + rs->lost = tp->lost - rs->prior_lost;
  3400. +
  3401. + rs->delivered_ce = tp->delivered_ce - rs->prior_delivered_ce;
  3402. + /* delivered_ce occupies less than 32 bits in the skb control block */
  3403. + rs->delivered_ce &= TCPCB_DELIVERED_CE_MASK;
  3404. /* Model sending data and receiving ACKs as separate pipeline phases
  3405. * for a window. Usually the ACK phase is longer, but with ACK