blob: c7bf5b26bf0c24837034eaee0189847d49ded6e9 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -08002/*
Sangtae Haae27e982008-10-29 00:07:18 -04003 * TCP CUBIC: Binary Increase Congestion control for TCP v2.3
Sangtae Ha6b3d6262008-03-04 14:17:41 -08004 * Home page:
5 * http://netsrv.csc.ncsu.edu/twiki/bin/view/Main/BIC
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -08006 * This is from the implementation of CUBIC TCP in
Sangtae Haae27e982008-10-29 00:07:18 -04007 * Sangtae Ha, Injong Rhee and Lisong Xu,
8 * "CUBIC: A New TCP-Friendly High-Speed TCP Variant"
9 * in ACM SIGOPS Operating System Review, July 2008.
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080010 * Available from:
Sangtae Haae27e982008-10-29 00:07:18 -040011 * http://netsrv.csc.ncsu.edu/export/cubic_a_new_tcp_2008.pdf
12 *
13 * CUBIC integrates a new slow start algorithm, called HyStart.
14 * The details of HyStart are presented in
15 * Sangtae Ha and Injong Rhee,
16 * "Taming the Elephants: New TCP Slow Start", NCSU TechReport 2008.
17 * Available from:
18 * http://netsrv.csc.ncsu.edu/export/hystart_techreport_2008.pdf
19 *
20 * All testing results are available from:
21 * http://netsrv.csc.ncsu.edu/wiki/index.php/TCP_Testing
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080022 *
23 * Unless CUBIC is enabled and congestion window is large
24 * this behaves the same as the original Reno.
25 */
26
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080027#include <linux/mm.h>
28#include <linux/module.h>
Roman Zippel6f6d6a12008-05-01 04:34:28 -070029#include <linux/math64.h>
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080030#include <net/tcp.h>
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080031
32#define BICTCP_BETA_SCALE 1024 /* Scale factor beta calculation
33 * max_cwnd = snd_cwnd * beta
34 */
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080035#define BICTCP_HZ 10 /* BIC HZ 2^10 = 1024 */
36
Sangtae Haae27e982008-10-29 00:07:18 -040037/* Two methods of hybrid slow start */
38#define HYSTART_ACK_TRAIN 0x1
39#define HYSTART_DELAY 0x2
40
41/* Number of delay samples for detecting the increase of delay */
42#define HYSTART_MIN_SAMPLES 8
Eric Dumazetcff04e22019-12-23 12:27:52 -080043#define HYSTART_DELAY_MIN (4000U) /* 4 ms */
44#define HYSTART_DELAY_MAX (16000U) /* 16 ms */
Sangtae Haae27e982008-10-29 00:07:18 -040045#define HYSTART_DELAY_THRESH(x) clamp(x, HYSTART_DELAY_MIN, HYSTART_DELAY_MAX)
46
Stephen Hemminger59758f42007-02-12 13:15:20 -080047static int fast_convergence __read_mostly = 1;
Sangtae Ha6b3d6262008-03-04 14:17:41 -080048static int beta __read_mostly = 717; /* = 717/1024 (BICTCP_BETA_SCALE) */
David S. Miller66e1e3b2007-06-13 01:03:53 -070049static int initial_ssthresh __read_mostly;
Stephen Hemminger59758f42007-02-12 13:15:20 -080050static int bic_scale __read_mostly = 41;
51static int tcp_friendliness __read_mostly = 1;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080052
Sangtae Haae27e982008-10-29 00:07:18 -040053static int hystart __read_mostly = 1;
54static int hystart_detect __read_mostly = HYSTART_ACK_TRAIN | HYSTART_DELAY;
55static int hystart_low_window __read_mostly = 16;
Eric Dumazetcff04e22019-12-23 12:27:52 -080056static int hystart_ack_delta_us __read_mostly = 2000;
Sangtae Haae27e982008-10-29 00:07:18 -040057
Stephen Hemminger59758f42007-02-12 13:15:20 -080058static u32 cube_rtt_scale __read_mostly;
59static u32 beta_scale __read_mostly;
60static u64 cube_factor __read_mostly;
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -080061
62/* Note parameters that are used for precomputing scale factors are read-only */
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080063module_param(fast_convergence, int, 0644);
64MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
Sangtae Ha6b3d6262008-03-04 14:17:41 -080065module_param(beta, int, 0644);
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080066MODULE_PARM_DESC(beta, "beta for multiplicative increase");
67module_param(initial_ssthresh, int, 0644);
68MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -080069module_param(bic_scale, int, 0444);
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080070MODULE_PARM_DESC(bic_scale, "scale (scaled by 1024) value for bic function (bic_scale/1024)");
71module_param(tcp_friendliness, int, 0644);
72MODULE_PARM_DESC(tcp_friendliness, "turn on/off tcp friendliness");
Sangtae Haae27e982008-10-29 00:07:18 -040073module_param(hystart, int, 0644);
74MODULE_PARM_DESC(hystart, "turn on/off hybrid slow start algorithm");
75module_param(hystart_detect, int, 0644);
Chema Gonzalezd6ecf322017-04-18 19:22:23 -070076MODULE_PARM_DESC(hystart_detect, "hybrid slow start detection mechanisms"
Sangtae Haae27e982008-10-29 00:07:18 -040077 " 1: packet-train 2: delay 3: both packet-train and delay");
78module_param(hystart_low_window, int, 0644);
79MODULE_PARM_DESC(hystart_low_window, "lower bound cwnd for hybrid slow start");
Eric Dumazetcff04e22019-12-23 12:27:52 -080080module_param(hystart_ack_delta_us, int, 0644);
81MODULE_PARM_DESC(hystart_ack_delta_us, "spacing between ack's indicating train (usecs)");
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080082
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080083/* BIC TCP Parameters */
84struct bictcp {
85 u32 cnt; /* increase cwnd by 1 after ACKs */
stephen hemminger688d1942014-08-29 23:32:05 -070086 u32 last_max_cwnd; /* last maximum snd_cwnd */
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080087 u32 last_cwnd; /* the last snd_cwnd */
88 u32 last_time; /* time when updated last_cwnd */
89 u32 bic_origin_point;/* origin point of bic function */
stephen hemminger688d1942014-08-29 23:32:05 -070090 u32 bic_K; /* time to origin point
91 from the beginning of the current epoch */
Eric Dumazetcff04e22019-12-23 12:27:52 -080092 u32 delay_min; /* min delay (usec) */
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080093 u32 epoch_start; /* beginning of an epoch */
94 u32 ack_cnt; /* number of acks */
95 u32 tcp_cwnd; /* estimated tcp cwnd */
Neal Cardwell9cd981d2015-01-28 20:01:38 -050096 u16 unused;
Sangtae Haae27e982008-10-29 00:07:18 -040097 u8 sample_cnt; /* number of samples to decide curr_rtt */
98 u8 found; /* the exit point is found? */
99 u32 round_start; /* beginning of each round */
100 u32 end_seq; /* end_seq of the round */
stephen hemminger17a6e9f2011-03-14 07:52:15 +0000101 u32 last_ack; /* last time when the ACK spacing is close */
Sangtae Haae27e982008-10-29 00:07:18 -0400102 u32 curr_rtt; /* the minimum rtt of current round */
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800103};
104
105static inline void bictcp_reset(struct bictcp *ca)
106{
107 ca->cnt = 0;
108 ca->last_max_cwnd = 0;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800109 ca->last_cwnd = 0;
110 ca->last_time = 0;
111 ca->bic_origin_point = 0;
112 ca->bic_K = 0;
113 ca->delay_min = 0;
114 ca->epoch_start = 0;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800115 ca->ack_cnt = 0;
116 ca->tcp_cwnd = 0;
Sangtae Haae27e982008-10-29 00:07:18 -0400117 ca->found = 0;
118}
119
Eric Dumazetcff04e22019-12-23 12:27:52 -0800120static inline u32 bictcp_clock_us(const struct sock *sk)
stephen hemminger17a6e9f2011-03-14 07:52:15 +0000121{
Eric Dumazetcff04e22019-12-23 12:27:52 -0800122 return tcp_sk(sk)->tcp_mstamp;
stephen hemminger17a6e9f2011-03-14 07:52:15 +0000123}
124
Sangtae Haae27e982008-10-29 00:07:18 -0400125static inline void bictcp_hystart_reset(struct sock *sk)
126{
127 struct tcp_sock *tp = tcp_sk(sk);
128 struct bictcp *ca = inet_csk_ca(sk);
129
Eric Dumazetcff04e22019-12-23 12:27:52 -0800130 ca->round_start = ca->last_ack = bictcp_clock_us(sk);
Sangtae Haae27e982008-10-29 00:07:18 -0400131 ca->end_seq = tp->snd_nxt;
Eric Dumazet35821fc2019-12-23 12:27:51 -0800132 ca->curr_rtt = ~0U;
Sangtae Haae27e982008-10-29 00:07:18 -0400133 ca->sample_cnt = 0;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800134}
135
136static void bictcp_init(struct sock *sk)
137{
Neal Cardwell5a45f002012-01-18 17:47:59 +0000138 struct bictcp *ca = inet_csk_ca(sk);
139
140 bictcp_reset(ca);
Sangtae Haae27e982008-10-29 00:07:18 -0400141
142 if (hystart)
143 bictcp_hystart_reset(sk);
144
145 if (!hystart && initial_ssthresh)
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800146 tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
147}
148
Eric Dumazet30927522015-09-09 21:55:07 -0700149static void bictcp_cwnd_event(struct sock *sk, enum tcp_ca_event event)
150{
151 if (event == CA_EVENT_TX_START) {
Eric Dumazet30927522015-09-09 21:55:07 -0700152 struct bictcp *ca = inet_csk_ca(sk);
Eric Dumazetd635fbe2017-05-16 14:00:03 -0700153 u32 now = tcp_jiffies32;
Eric Dumazetc2e72042015-09-17 08:38:00 -0700154 s32 delta;
155
156 delta = now - tcp_sk(sk)->lsndtime;
Eric Dumazet30927522015-09-09 21:55:07 -0700157
158 /* We were application limited (idle) for a while.
159 * Shift epoch_start to keep cwnd growth to cubic curve.
160 */
Eric Dumazetc2e72042015-09-17 08:38:00 -0700161 if (ca->epoch_start && delta > 0) {
Eric Dumazet30927522015-09-09 21:55:07 -0700162 ca->epoch_start += delta;
Eric Dumazetc2e72042015-09-17 08:38:00 -0700163 if (after(ca->epoch_start, now))
164 ca->epoch_start = now;
165 }
Eric Dumazet30927522015-09-09 21:55:07 -0700166 return;
167 }
168}
169
Stephen Hemminger7e588862007-03-22 12:10:58 -0700170/* calculate the cubic root of x using a table lookup followed by one
171 * Newton-Raphson iteration.
172 * Avg err ~= 0.195%
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800173 */
Stephen Hemminger9eb2d622005-12-21 19:32:36 -0800174static u32 cubic_root(u64 a)
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800175{
Stephen Hemminger7e588862007-03-22 12:10:58 -0700176 u32 x, b, shift;
177 /*
178 * cbrt(x) MSB values for x MSB values in [0..63].
179 * Precomputed then refined by hand - Willy Tarreau
180 *
181 * For x in [0..63],
182 * v = cbrt(x << 18) - 1
183 * cbrt(x) = (v[x] + 10) >> 6
Stephen Hemminger9eb2d622005-12-21 19:32:36 -0800184 */
Stephen Hemminger7e588862007-03-22 12:10:58 -0700185 static const u8 v[] = {
186 /* 0x00 */ 0, 54, 54, 54, 118, 118, 118, 118,
187 /* 0x08 */ 123, 129, 134, 138, 143, 147, 151, 156,
188 /* 0x10 */ 157, 161, 164, 168, 170, 173, 176, 179,
189 /* 0x18 */ 181, 185, 187, 190, 192, 194, 197, 199,
190 /* 0x20 */ 200, 202, 204, 206, 209, 211, 213, 215,
191 /* 0x28 */ 217, 219, 221, 222, 224, 225, 227, 229,
192 /* 0x30 */ 231, 232, 234, 236, 237, 239, 240, 242,
193 /* 0x38 */ 244, 245, 246, 248, 250, 251, 252, 254,
194 };
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800195
Stephen Hemminger7e588862007-03-22 12:10:58 -0700196 b = fls64(a);
197 if (b < 7) {
198 /* a in [0..63] */
199 return ((u32)v[(u32)a] + 35) >> 6;
200 }
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800201
Stephen Hemminger7e588862007-03-22 12:10:58 -0700202 b = ((b * 84) >> 8) - 1;
203 shift = (a >> (b * 3));
204
205 x = ((u32)(((u32)v[shift] + 10) << b)) >> 6;
206
207 /*
208 * Newton-Raphson iteration
209 * 2
210 * x = ( 2 * x + a / x ) / 3
211 * k+1 k k
212 */
Roman Zippel6f6d6a12008-05-01 04:34:28 -0700213 x = (2 * x + (u32)div64_u64(a, (u64)x * (u64)(x - 1)));
Stephen Hemminger7e588862007-03-22 12:10:58 -0700214 x = ((x * 341) >> 10);
Stephen Hemminger9eb2d622005-12-21 19:32:36 -0800215 return x;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800216}
217
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800218/*
219 * Compute congestion window to use.
220 */
Neal Cardwell9cd981d2015-01-28 20:01:38 -0500221static inline void bictcp_update(struct bictcp *ca, u32 cwnd, u32 acked)
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800222{
Eric Dumazet2ed0edf2013-08-05 17:10:15 -0700223 u32 delta, bic_target, max_cnt;
224 u64 offs, t;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800225
Neal Cardwell9cd981d2015-01-28 20:01:38 -0500226 ca->ack_cnt += acked; /* count the number of ACKed packets */
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800227
228 if (ca->last_cwnd == cwnd &&
Eric Dumazetac35f562017-05-16 14:00:06 -0700229 (s32)(tcp_jiffies32 - ca->last_time) <= HZ / 32)
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800230 return;
231
Neal Cardwelld6b1a8a2015-01-28 20:01:39 -0500232 /* The CUBIC function can update ca->cnt at most once per jiffy.
233 * On all cwnd reduction events, ca->epoch_start is set to 0,
234 * which will force a recalculation of ca->cnt.
235 */
Eric Dumazetac35f562017-05-16 14:00:06 -0700236 if (ca->epoch_start && tcp_jiffies32 == ca->last_time)
Neal Cardwelld6b1a8a2015-01-28 20:01:39 -0500237 goto tcp_friendliness;
238
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800239 ca->last_cwnd = cwnd;
Eric Dumazetac35f562017-05-16 14:00:06 -0700240 ca->last_time = tcp_jiffies32;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800241
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800242 if (ca->epoch_start == 0) {
Eric Dumazetac35f562017-05-16 14:00:06 -0700243 ca->epoch_start = tcp_jiffies32; /* record beginning */
Neal Cardwell9cd981d2015-01-28 20:01:38 -0500244 ca->ack_cnt = acked; /* start counting */
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800245 ca->tcp_cwnd = cwnd; /* syn with cubic */
246
247 if (ca->last_max_cwnd <= cwnd) {
248 ca->bic_K = 0;
249 ca->bic_origin_point = cwnd;
250 } else {
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800251 /* Compute new K based on
252 * (wmax-cwnd) * (srtt>>3 / HZ) / c * 2^(3*bictcp_HZ)
253 */
254 ca->bic_K = cubic_root(cube_factor
255 * (ca->last_max_cwnd - cwnd));
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800256 ca->bic_origin_point = ca->last_max_cwnd;
257 }
258 }
259
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900260 /* cubic function - calc*/
261 /* calculate c * time^3 / rtt,
262 * while considering overflow in calculation of time^3
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800263 * (so time^3 is done by using 64 bit)
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800264 * and without the support of division of 64bit numbers
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800265 * (so all divisions are done by using 32 bit)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900266 * also NOTE the unit of those veriables
267 * time = (t - K) / 2^bictcp_HZ
268 * c = bic_scale >> 10
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800269 * rtt = (srtt >> 3) / HZ
270 * !!! The following code does not have overflow problems,
271 * if the cwnd < 1 million packets !!!
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900272 */
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800273
Eric Dumazetac35f562017-05-16 14:00:06 -0700274 t = (s32)(tcp_jiffies32 - ca->epoch_start);
Eric Dumazetcff04e22019-12-23 12:27:52 -0800275 t += usecs_to_jiffies(ca->delay_min);
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800276 /* change the unit from HZ to bictcp_HZ */
Eric Dumazet2ed0edf2013-08-05 17:10:15 -0700277 t <<= BICTCP_HZ;
278 do_div(t, HZ);
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800279
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900280 if (t < ca->bic_K) /* t - K */
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800281 offs = ca->bic_K - t;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900282 else
283 offs = t - ca->bic_K;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800284
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800285 /* c/rtt * (t-K)^3 */
286 delta = (cube_rtt_scale * offs * offs * offs) >> (10+3*BICTCP_HZ);
stephen hemminger688d1942014-08-29 23:32:05 -0700287 if (t < ca->bic_K) /* below origin*/
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900288 bic_target = ca->bic_origin_point - delta;
stephen hemminger688d1942014-08-29 23:32:05 -0700289 else /* above origin*/
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900290 bic_target = ca->bic_origin_point + delta;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800291
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900292 /* cubic function - calc bictcp_cnt*/
293 if (bic_target > cwnd) {
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800294 ca->cnt = cwnd / (bic_target - cwnd);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900295 } else {
296 ca->cnt = 100 * cwnd; /* very small increment*/
297 }
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800298
Sangtae Hab5ccd072011-03-14 07:52:18 +0000299 /*
300 * The initial growth of cubic function may be too conservative
301 * when the available bandwidth is still unknown.
302 */
Neal Cardwell5a45f002012-01-18 17:47:59 +0000303 if (ca->last_max_cwnd == 0 && ca->cnt > 20)
Sangtae Hab5ccd072011-03-14 07:52:18 +0000304 ca->cnt = 20; /* increase cwnd 5% per RTT */
305
Neal Cardwelld6b1a8a2015-01-28 20:01:39 -0500306tcp_friendliness:
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800307 /* TCP Friendly */
308 if (tcp_friendliness) {
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800309 u32 scale = beta_scale;
stephen hemminger688d1942014-08-29 23:32:05 -0700310
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800311 delta = (cwnd * scale) >> 3;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900312 while (ca->ack_cnt > delta) { /* update tcp cwnd */
313 ca->ack_cnt -= delta;
314 ca->tcp_cwnd++;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800315 }
316
stephen hemminger688d1942014-08-29 23:32:05 -0700317 if (ca->tcp_cwnd > cwnd) { /* if bic is slower than tcp */
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800318 delta = ca->tcp_cwnd - cwnd;
319 max_cnt = cwnd / delta;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800320 if (ca->cnt > max_cnt)
321 ca->cnt = max_cnt;
322 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900323 }
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800324
Neal Cardwelld578e182015-03-10 17:17:04 -0400325 /* The maximum rate of cwnd increase CUBIC allows is 1 packet per
326 * 2 packets ACKed, meaning cwnd grows at 1.5x per RTT.
327 */
328 ca->cnt = max(ca->cnt, 2U);
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800329}
330
Eric Dumazet24901552014-05-02 21:18:05 -0700331static void bictcp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800332{
333 struct tcp_sock *tp = tcp_sk(sk);
334 struct bictcp *ca = inet_csk_ca(sk);
335
Eric Dumazet24901552014-05-02 21:18:05 -0700336 if (!tcp_is_cwnd_limited(sk))
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800337 return;
338
Yuchung Cheng071d5082015-07-09 13:16:29 -0700339 if (tcp_in_slow_start(tp)) {
Sangtae Haae27e982008-10-29 00:07:18 -0400340 if (hystart && after(ack, ca->end_seq))
341 bictcp_hystart_reset(sk);
Neal Cardwell9cd981d2015-01-28 20:01:38 -0500342 acked = tcp_slow_start(tp, acked);
343 if (!acked)
344 return;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800345 }
Neal Cardwell9cd981d2015-01-28 20:01:38 -0500346 bictcp_update(ca, tp->snd_cwnd, acked);
347 tcp_cong_avoid_ai(tp, ca->cnt, acked);
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800348}
349
350static u32 bictcp_recalc_ssthresh(struct sock *sk)
351{
352 const struct tcp_sock *tp = tcp_sk(sk);
353 struct bictcp *ca = inet_csk_ca(sk);
354
355 ca->epoch_start = 0; /* end of epoch */
356
357 /* Wmax and fast convergence */
358 if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence)
359 ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta))
360 / (2 * BICTCP_BETA_SCALE);
361 else
362 ca->last_max_cwnd = tp->snd_cwnd;
363
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800364 return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
365}
366
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800367static void bictcp_state(struct sock *sk, u8 new_state)
368{
Sangtae Haae27e982008-10-29 00:07:18 -0400369 if (new_state == TCP_CA_Loss) {
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800370 bictcp_reset(inet_csk_ca(sk));
Sangtae Haae27e982008-10-29 00:07:18 -0400371 bictcp_hystart_reset(sk);
372 }
373}
374
Eric Dumazetf278b992019-12-30 06:06:19 -0800375/* Account for TSO/GRO delays.
376 * Otherwise short RTT flows could get too small ssthresh, since during
377 * slow start we begin with small TSO packets and ca->delay_min would
378 * not account for long aggregation delay when TSO packets get bigger.
379 * Ideally even with a very small RTT we would like to have at least one
380 * TSO packet being sent and received by GRO, and another one in qdisc layer.
381 * We apply another 100% factor because @rate is doubled at this point.
382 * We cap the cushion to 1ms.
383 */
384static u32 hystart_ack_delay(struct sock *sk)
385{
386 unsigned long rate;
387
388 rate = READ_ONCE(sk->sk_pacing_rate);
389 if (!rate)
390 return 0;
391 return min_t(u64, USEC_PER_MSEC,
392 div64_ul((u64)GSO_MAX_SIZE * 4 * USEC_PER_SEC, rate));
393}
394
Sangtae Haae27e982008-10-29 00:07:18 -0400395static void hystart_update(struct sock *sk, u32 delay)
396{
397 struct tcp_sock *tp = tcp_sk(sk);
398 struct bictcp *ca = inet_csk_ca(sk);
Eric Dumazetede656e2019-12-23 12:27:54 -0800399 u32 threshold;
Sangtae Haae27e982008-10-29 00:07:18 -0400400
Eric Dumazet6e3a8a92014-12-04 16:13:23 -0800401 if (hystart_detect & HYSTART_ACK_TRAIN) {
Eric Dumazetcff04e22019-12-23 12:27:52 -0800402 u32 now = bictcp_clock_us(sk);
Sangtae Haae27e982008-10-29 00:07:18 -0400403
404 /* first detection parameter - ack-train detection */
Eric Dumazetcff04e22019-12-23 12:27:52 -0800405 if ((s32)(now - ca->last_ack) <= hystart_ack_delta_us) {
stephen hemminger17a6e9f2011-03-14 07:52:15 +0000406 ca->last_ack = now;
Eric Dumazetede656e2019-12-23 12:27:54 -0800407
Eric Dumazetf278b992019-12-30 06:06:19 -0800408 threshold = ca->delay_min + hystart_ack_delay(sk);
409
Eric Dumazetede656e2019-12-23 12:27:54 -0800410 /* Hystart ack train triggers if we get ack past
411 * ca->delay_min/2.
412 * Pacing might have delayed packets up to RTT/2
413 * during slow start.
414 */
415 if (sk->sk_pacing_status == SK_PACING_NONE)
416 threshold >>= 1;
417
418 if ((s32)(now - ca->round_start) > threshold) {
Eric Dumazet473900a2019-12-23 12:27:50 -0800419 ca->found = 1;
Eric Dumazetf278b992019-12-30 06:06:19 -0800420 pr_debug("hystart_ack_train (%u > %u) delay_min %u (+ ack_delay %u) cwnd %u\n",
421 now - ca->round_start, threshold,
422 ca->delay_min, hystart_ack_delay(sk), tp->snd_cwnd);
Eric Dumazetc10d9312016-04-29 14:16:47 -0700423 NET_INC_STATS(sock_net(sk),
424 LINUX_MIB_TCPHYSTARTTRAINDETECT);
425 NET_ADD_STATS(sock_net(sk),
426 LINUX_MIB_TCPHYSTARTTRAINCWND,
427 tp->snd_cwnd);
Eric Dumazet6e3a8a92014-12-04 16:13:23 -0800428 tp->snd_ssthresh = tp->snd_cwnd;
429 }
Sangtae Haae27e982008-10-29 00:07:18 -0400430 }
Eric Dumazet6e3a8a92014-12-04 16:13:23 -0800431 }
Sangtae Haae27e982008-10-29 00:07:18 -0400432
Eric Dumazet6e3a8a92014-12-04 16:13:23 -0800433 if (hystart_detect & HYSTART_DELAY) {
Sangtae Haae27e982008-10-29 00:07:18 -0400434 /* obtain the minimum delay of more than sampling packets */
Neal Cardwellb3445792020-06-24 12:42:02 -0400435 if (ca->curr_rtt > delay)
436 ca->curr_rtt = delay;
Sangtae Haae27e982008-10-29 00:07:18 -0400437 if (ca->sample_cnt < HYSTART_MIN_SAMPLES) {
Sangtae Haae27e982008-10-29 00:07:18 -0400438 ca->sample_cnt++;
439 } else {
440 if (ca->curr_rtt > ca->delay_min +
Eric Dumazet42eef7a2014-12-04 16:13:49 -0800441 HYSTART_DELAY_THRESH(ca->delay_min >> 3)) {
Eric Dumazet473900a2019-12-23 12:27:50 -0800442 ca->found = 1;
Eric Dumazetc10d9312016-04-29 14:16:47 -0700443 NET_INC_STATS(sock_net(sk),
444 LINUX_MIB_TCPHYSTARTDELAYDETECT);
445 NET_ADD_STATS(sock_net(sk),
446 LINUX_MIB_TCPHYSTARTDELAYCWND,
447 tp->snd_cwnd);
Eric Dumazet6e3a8a92014-12-04 16:13:23 -0800448 tp->snd_ssthresh = tp->snd_cwnd;
449 }
Sangtae Haae27e982008-10-29 00:07:18 -0400450 }
Sangtae Haae27e982008-10-29 00:07:18 -0400451 }
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800452}
453
Lawrence Brakmo756ee172016-05-11 10:02:13 -0700454static void bictcp_acked(struct sock *sk, const struct ack_sample *sample)
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800455{
Sangtae Haae27e982008-10-29 00:07:18 -0400456 const struct tcp_sock *tp = tcp_sk(sk);
Stephen Hemmingere7d0c8852007-07-25 23:50:06 -0700457 struct bictcp *ca = inet_csk_ca(sk);
458 u32 delay;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800459
Stephen Hemmingere7d0c8852007-07-25 23:50:06 -0700460 /* Some calls are for duplicates without timetamps */
Lawrence Brakmo756ee172016-05-11 10:02:13 -0700461 if (sample->rtt_us < 0)
Stephen Hemmingere7d0c8852007-07-25 23:50:06 -0700462 return;
463
464 /* Discard delay samples right after fast recovery */
Eric Dumazetac35f562017-05-16 14:00:06 -0700465 if (ca->epoch_start && (s32)(tcp_jiffies32 - ca->epoch_start) < HZ)
Stephen Hemmingere7d0c8852007-07-25 23:50:06 -0700466 return;
467
Eric Dumazetcff04e22019-12-23 12:27:52 -0800468 delay = sample->rtt_us;
Stephen Hemmingere7d0c8852007-07-25 23:50:06 -0700469 if (delay == 0)
470 delay = 1;
471
472 /* first time call or link delay decreases */
Eric Dumazetf278b992019-12-30 06:06:19 -0800473 if (ca->delay_min == 0 || ca->delay_min > delay)
474 ca->delay_min = delay;
Sangtae Haae27e982008-10-29 00:07:18 -0400475
476 /* hystart triggers when cwnd is larger than some threshold */
Eric Dumazetf278b992019-12-30 06:06:19 -0800477 if (!ca->found && tcp_in_slow_start(tp) && hystart &&
Sangtae Haae27e982008-10-29 00:07:18 -0400478 tp->snd_cwnd >= hystart_low_window)
479 hystart_update(sk, delay);
Stephen Hemmingere7d0c8852007-07-25 23:50:06 -0700480}
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800481
Stephen Hemmingera252beb2011-03-10 00:40:17 -0800482static struct tcp_congestion_ops cubictcp __read_mostly = {
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800483 .init = bictcp_init,
484 .ssthresh = bictcp_recalc_ssthresh,
485 .cong_avoid = bictcp_cong_avoid,
486 .set_state = bictcp_state,
Yuchung Chengf1722a12017-08-03 20:38:52 -0700487 .undo_cwnd = tcp_reno_undo_cwnd,
Eric Dumazet30927522015-09-09 21:55:07 -0700488 .cwnd_event = bictcp_cwnd_event,
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800489 .pkts_acked = bictcp_acked,
490 .owner = THIS_MODULE,
491 .name = "cubic",
492};
493
494static int __init cubictcp_register(void)
495{
Alexey Dobriyan74975d42006-08-25 17:10:33 -0700496 BUILD_BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800497
498 /* Precompute a bunch of the scaling factors that are used per-packet
499 * based on SRTT of 100ms
500 */
501
stephen hemminger688d1942014-08-29 23:32:05 -0700502 beta_scale = 8*(BICTCP_BETA_SCALE+beta) / 3
503 / (BICTCP_BETA_SCALE - beta);
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800504
Stephen Hemminger22119242006-10-25 23:04:12 -0700505 cube_rtt_scale = (bic_scale * 10); /* 1024*c/rtt */
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800506
507 /* calculate the "K" for (wmax-cwnd) = c/rtt * K^3
508 * so K = cubic_root( (wmax-cwnd)*rtt/c )
509 * the unit of K is bictcp_HZ=2^10, not HZ
510 *
511 * c = bic_scale >> 10
512 * rtt = 100ms
513 *
514 * the following code has been designed and tested for
515 * cwnd < 1 million packets
516 * RTT < 100 seconds
517 * HZ < 1,000,00 (corresponding to 10 nano-second)
518 */
519
520 /* 1/c * 2^2*bictcp_HZ * srtt */
521 cube_factor = 1ull << (10+3*BICTCP_HZ); /* 2^40 */
522
523 /* divide by bic_scale and by constant Srtt (100ms) */
524 do_div(cube_factor, bic_scale * 10);
525
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800526 return tcp_register_congestion_control(&cubictcp);
527}
528
529static void __exit cubictcp_unregister(void)
530{
531 tcp_unregister_congestion_control(&cubictcp);
532}
533
534module_init(cubictcp_register);
535module_exit(cubictcp_unregister);
536
537MODULE_AUTHOR("Sangtae Ha, Stephen Hemminger");
538MODULE_LICENSE("GPL");
539MODULE_DESCRIPTION("CUBIC TCP");
Sangtae Haae27e982008-10-29 00:07:18 -0400540MODULE_VERSION("2.3");