1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * TCP CUBIC: Binary Increase Congestion control for TCP v2.3
4*4882a593Smuzhiyun * Home page:
5*4882a593Smuzhiyun * http://netsrv.csc.ncsu.edu/twiki/bin/view/Main/BIC
6*4882a593Smuzhiyun * This is from the implementation of CUBIC TCP in
7*4882a593Smuzhiyun * Sangtae Ha, Injong Rhee and Lisong Xu,
8*4882a593Smuzhiyun * "CUBIC: A New TCP-Friendly High-Speed TCP Variant"
9*4882a593Smuzhiyun * in ACM SIGOPS Operating System Review, July 2008.
10*4882a593Smuzhiyun * Available from:
11*4882a593Smuzhiyun * http://netsrv.csc.ncsu.edu/export/cubic_a_new_tcp_2008.pdf
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * CUBIC integrates a new slow start algorithm, called HyStart.
14*4882a593Smuzhiyun * The details of HyStart are presented in
15*4882a593Smuzhiyun * Sangtae Ha and Injong Rhee,
16*4882a593Smuzhiyun * "Taming the Elephants: New TCP Slow Start", NCSU TechReport 2008.
17*4882a593Smuzhiyun * Available from:
18*4882a593Smuzhiyun * http://netsrv.csc.ncsu.edu/export/hystart_techreport_2008.pdf
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * All testing results are available from:
21*4882a593Smuzhiyun * http://netsrv.csc.ncsu.edu/wiki/index.php/TCP_Testing
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * Unless CUBIC is enabled and congestion window is large
24*4882a593Smuzhiyun * this behaves the same as the original Reno.
25*4882a593Smuzhiyun */
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #include <linux/mm.h>
28*4882a593Smuzhiyun #include <linux/module.h>
29*4882a593Smuzhiyun #include <linux/math64.h>
30*4882a593Smuzhiyun #include <net/tcp.h>
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #define BICTCP_BETA_SCALE 1024 /* Scale factor beta calculation
33*4882a593Smuzhiyun * max_cwnd = snd_cwnd * beta
34*4882a593Smuzhiyun */
35*4882a593Smuzhiyun #define BICTCP_HZ 10 /* BIC HZ 2^10 = 1024 */
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun /* Two methods of hybrid slow start */
38*4882a593Smuzhiyun #define HYSTART_ACK_TRAIN 0x1
39*4882a593Smuzhiyun #define HYSTART_DELAY 0x2
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun /* Number of delay samples for detecting the increase of delay */
42*4882a593Smuzhiyun #define HYSTART_MIN_SAMPLES 8
43*4882a593Smuzhiyun #define HYSTART_DELAY_MIN (4000U) /* 4 ms */
44*4882a593Smuzhiyun #define HYSTART_DELAY_MAX (16000U) /* 16 ms */
45*4882a593Smuzhiyun #define HYSTART_DELAY_THRESH(x) clamp(x, HYSTART_DELAY_MIN, HYSTART_DELAY_MAX)
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun static int fast_convergence __read_mostly = 1;
48*4882a593Smuzhiyun static int beta __read_mostly = 717; /* = 717/1024 (BICTCP_BETA_SCALE) */
49*4882a593Smuzhiyun static int initial_ssthresh __read_mostly;
50*4882a593Smuzhiyun static int bic_scale __read_mostly = 41;
51*4882a593Smuzhiyun static int tcp_friendliness __read_mostly = 1;
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun static int hystart __read_mostly = 1;
54*4882a593Smuzhiyun static int hystart_detect __read_mostly = HYSTART_ACK_TRAIN | HYSTART_DELAY;
55*4882a593Smuzhiyun static int hystart_low_window __read_mostly = 16;
56*4882a593Smuzhiyun static int hystart_ack_delta_us __read_mostly = 2000;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun static u32 cube_rtt_scale __read_mostly;
59*4882a593Smuzhiyun static u32 beta_scale __read_mostly;
60*4882a593Smuzhiyun static u64 cube_factor __read_mostly;
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /* Note parameters that are used for precomputing scale factors are read-only */
63*4882a593Smuzhiyun module_param(fast_convergence, int, 0644);
64*4882a593Smuzhiyun MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
65*4882a593Smuzhiyun module_param(beta, int, 0644);
66*4882a593Smuzhiyun MODULE_PARM_DESC(beta, "beta for multiplicative increase");
67*4882a593Smuzhiyun module_param(initial_ssthresh, int, 0644);
68*4882a593Smuzhiyun MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
69*4882a593Smuzhiyun module_param(bic_scale, int, 0444);
70*4882a593Smuzhiyun MODULE_PARM_DESC(bic_scale, "scale (scaled by 1024) value for bic function (bic_scale/1024)");
71*4882a593Smuzhiyun module_param(tcp_friendliness, int, 0644);
72*4882a593Smuzhiyun MODULE_PARM_DESC(tcp_friendliness, "turn on/off tcp friendliness");
73*4882a593Smuzhiyun module_param(hystart, int, 0644);
74*4882a593Smuzhiyun MODULE_PARM_DESC(hystart, "turn on/off hybrid slow start algorithm");
75*4882a593Smuzhiyun module_param(hystart_detect, int, 0644);
76*4882a593Smuzhiyun MODULE_PARM_DESC(hystart_detect, "hybrid slow start detection mechanisms"
77*4882a593Smuzhiyun " 1: packet-train 2: delay 3: both packet-train and delay");
78*4882a593Smuzhiyun module_param(hystart_low_window, int, 0644);
79*4882a593Smuzhiyun MODULE_PARM_DESC(hystart_low_window, "lower bound cwnd for hybrid slow start");
80*4882a593Smuzhiyun module_param(hystart_ack_delta_us, int, 0644);
81*4882a593Smuzhiyun MODULE_PARM_DESC(hystart_ack_delta_us, "spacing between ack's indicating train (usecs)");
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /* BIC TCP Parameters */
84*4882a593Smuzhiyun struct bictcp {
85*4882a593Smuzhiyun u32 cnt; /* increase cwnd by 1 after ACKs */
86*4882a593Smuzhiyun u32 last_max_cwnd; /* last maximum snd_cwnd */
87*4882a593Smuzhiyun u32 last_cwnd; /* the last snd_cwnd */
88*4882a593Smuzhiyun u32 last_time; /* time when updated last_cwnd */
89*4882a593Smuzhiyun u32 bic_origin_point;/* origin point of bic function */
90*4882a593Smuzhiyun u32 bic_K; /* time to origin point
91*4882a593Smuzhiyun from the beginning of the current epoch */
92*4882a593Smuzhiyun u32 delay_min; /* min delay (usec) */
93*4882a593Smuzhiyun u32 epoch_start; /* beginning of an epoch */
94*4882a593Smuzhiyun u32 ack_cnt; /* number of acks */
95*4882a593Smuzhiyun u32 tcp_cwnd; /* estimated tcp cwnd */
96*4882a593Smuzhiyun u16 unused;
97*4882a593Smuzhiyun u8 sample_cnt; /* number of samples to decide curr_rtt */
98*4882a593Smuzhiyun u8 found; /* the exit point is found? */
99*4882a593Smuzhiyun u32 round_start; /* beginning of each round */
100*4882a593Smuzhiyun u32 end_seq; /* end_seq of the round */
101*4882a593Smuzhiyun u32 last_ack; /* last time when the ACK spacing is close */
102*4882a593Smuzhiyun u32 curr_rtt; /* the minimum rtt of current round */
103*4882a593Smuzhiyun };
104*4882a593Smuzhiyun
bictcp_reset(struct bictcp * ca)105*4882a593Smuzhiyun static inline void bictcp_reset(struct bictcp *ca)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun ca->cnt = 0;
108*4882a593Smuzhiyun ca->last_max_cwnd = 0;
109*4882a593Smuzhiyun ca->last_cwnd = 0;
110*4882a593Smuzhiyun ca->last_time = 0;
111*4882a593Smuzhiyun ca->bic_origin_point = 0;
112*4882a593Smuzhiyun ca->bic_K = 0;
113*4882a593Smuzhiyun ca->delay_min = 0;
114*4882a593Smuzhiyun ca->epoch_start = 0;
115*4882a593Smuzhiyun ca->ack_cnt = 0;
116*4882a593Smuzhiyun ca->tcp_cwnd = 0;
117*4882a593Smuzhiyun ca->found = 0;
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun
bictcp_clock_us(const struct sock * sk)120*4882a593Smuzhiyun static inline u32 bictcp_clock_us(const struct sock *sk)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun return tcp_sk(sk)->tcp_mstamp;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun
bictcp_hystart_reset(struct sock * sk)125*4882a593Smuzhiyun static inline void bictcp_hystart_reset(struct sock *sk)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun struct tcp_sock *tp = tcp_sk(sk);
128*4882a593Smuzhiyun struct bictcp *ca = inet_csk_ca(sk);
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun ca->round_start = ca->last_ack = bictcp_clock_us(sk);
131*4882a593Smuzhiyun ca->end_seq = tp->snd_nxt;
132*4882a593Smuzhiyun ca->curr_rtt = ~0U;
133*4882a593Smuzhiyun ca->sample_cnt = 0;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun
bictcp_init(struct sock * sk)136*4882a593Smuzhiyun static void bictcp_init(struct sock *sk)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun struct bictcp *ca = inet_csk_ca(sk);
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun bictcp_reset(ca);
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun if (hystart)
143*4882a593Smuzhiyun bictcp_hystart_reset(sk);
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun if (!hystart && initial_ssthresh)
146*4882a593Smuzhiyun tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun
bictcp_cwnd_event(struct sock * sk,enum tcp_ca_event event)149*4882a593Smuzhiyun static void bictcp_cwnd_event(struct sock *sk, enum tcp_ca_event event)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun if (event == CA_EVENT_TX_START) {
152*4882a593Smuzhiyun struct bictcp *ca = inet_csk_ca(sk);
153*4882a593Smuzhiyun u32 now = tcp_jiffies32;
154*4882a593Smuzhiyun s32 delta;
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun delta = now - tcp_sk(sk)->lsndtime;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun /* We were application limited (idle) for a while.
159*4882a593Smuzhiyun * Shift epoch_start to keep cwnd growth to cubic curve.
160*4882a593Smuzhiyun */
161*4882a593Smuzhiyun if (ca->epoch_start && delta > 0) {
162*4882a593Smuzhiyun ca->epoch_start += delta;
163*4882a593Smuzhiyun if (after(ca->epoch_start, now))
164*4882a593Smuzhiyun ca->epoch_start = now;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun return;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun /* calculate the cubic root of x using a table lookup followed by one
171*4882a593Smuzhiyun * Newton-Raphson iteration.
172*4882a593Smuzhiyun * Avg err ~= 0.195%
173*4882a593Smuzhiyun */
cubic_root(u64 a)174*4882a593Smuzhiyun static u32 cubic_root(u64 a)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun u32 x, b, shift;
177*4882a593Smuzhiyun /*
178*4882a593Smuzhiyun * cbrt(x) MSB values for x MSB values in [0..63].
179*4882a593Smuzhiyun * Precomputed then refined by hand - Willy Tarreau
180*4882a593Smuzhiyun *
181*4882a593Smuzhiyun * For x in [0..63],
182*4882a593Smuzhiyun * v = cbrt(x << 18) - 1
183*4882a593Smuzhiyun * cbrt(x) = (v[x] + 10) >> 6
184*4882a593Smuzhiyun */
185*4882a593Smuzhiyun static const u8 v[] = {
186*4882a593Smuzhiyun /* 0x00 */ 0, 54, 54, 54, 118, 118, 118, 118,
187*4882a593Smuzhiyun /* 0x08 */ 123, 129, 134, 138, 143, 147, 151, 156,
188*4882a593Smuzhiyun /* 0x10 */ 157, 161, 164, 168, 170, 173, 176, 179,
189*4882a593Smuzhiyun /* 0x18 */ 181, 185, 187, 190, 192, 194, 197, 199,
190*4882a593Smuzhiyun /* 0x20 */ 200, 202, 204, 206, 209, 211, 213, 215,
191*4882a593Smuzhiyun /* 0x28 */ 217, 219, 221, 222, 224, 225, 227, 229,
192*4882a593Smuzhiyun /* 0x30 */ 231, 232, 234, 236, 237, 239, 240, 242,
193*4882a593Smuzhiyun /* 0x38 */ 244, 245, 246, 248, 250, 251, 252, 254,
194*4882a593Smuzhiyun };
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun b = fls64(a);
197*4882a593Smuzhiyun if (b < 7) {
198*4882a593Smuzhiyun /* a in [0..63] */
199*4882a593Smuzhiyun return ((u32)v[(u32)a] + 35) >> 6;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun b = ((b * 84) >> 8) - 1;
203*4882a593Smuzhiyun shift = (a >> (b * 3));
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun x = ((u32)(((u32)v[shift] + 10) << b)) >> 6;
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun /*
208*4882a593Smuzhiyun * Newton-Raphson iteration
209*4882a593Smuzhiyun * 2
210*4882a593Smuzhiyun * x = ( 2 * x + a / x ) / 3
211*4882a593Smuzhiyun * k+1 k k
212*4882a593Smuzhiyun */
213*4882a593Smuzhiyun x = (2 * x + (u32)div64_u64(a, (u64)x * (u64)(x - 1)));
214*4882a593Smuzhiyun x = ((x * 341) >> 10);
215*4882a593Smuzhiyun return x;
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun /*
219*4882a593Smuzhiyun * Compute congestion window to use.
220*4882a593Smuzhiyun */
bictcp_update(struct bictcp * ca,u32 cwnd,u32 acked)221*4882a593Smuzhiyun static inline void bictcp_update(struct bictcp *ca, u32 cwnd, u32 acked)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun u32 delta, bic_target, max_cnt;
224*4882a593Smuzhiyun u64 offs, t;
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun ca->ack_cnt += acked; /* count the number of ACKed packets */
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun if (ca->last_cwnd == cwnd &&
229*4882a593Smuzhiyun (s32)(tcp_jiffies32 - ca->last_time) <= HZ / 32)
230*4882a593Smuzhiyun return;
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun /* The CUBIC function can update ca->cnt at most once per jiffy.
233*4882a593Smuzhiyun * On all cwnd reduction events, ca->epoch_start is set to 0,
234*4882a593Smuzhiyun * which will force a recalculation of ca->cnt.
235*4882a593Smuzhiyun */
236*4882a593Smuzhiyun if (ca->epoch_start && tcp_jiffies32 == ca->last_time)
237*4882a593Smuzhiyun goto tcp_friendliness;
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun ca->last_cwnd = cwnd;
240*4882a593Smuzhiyun ca->last_time = tcp_jiffies32;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun if (ca->epoch_start == 0) {
243*4882a593Smuzhiyun ca->epoch_start = tcp_jiffies32; /* record beginning */
244*4882a593Smuzhiyun ca->ack_cnt = acked; /* start counting */
245*4882a593Smuzhiyun ca->tcp_cwnd = cwnd; /* syn with cubic */
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun if (ca->last_max_cwnd <= cwnd) {
248*4882a593Smuzhiyun ca->bic_K = 0;
249*4882a593Smuzhiyun ca->bic_origin_point = cwnd;
250*4882a593Smuzhiyun } else {
251*4882a593Smuzhiyun /* Compute new K based on
252*4882a593Smuzhiyun * (wmax-cwnd) * (srtt>>3 / HZ) / c * 2^(3*bictcp_HZ)
253*4882a593Smuzhiyun */
254*4882a593Smuzhiyun ca->bic_K = cubic_root(cube_factor
255*4882a593Smuzhiyun * (ca->last_max_cwnd - cwnd));
256*4882a593Smuzhiyun ca->bic_origin_point = ca->last_max_cwnd;
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun /* cubic function - calc*/
261*4882a593Smuzhiyun /* calculate c * time^3 / rtt,
262*4882a593Smuzhiyun * while considering overflow in calculation of time^3
263*4882a593Smuzhiyun * (so time^3 is done by using 64 bit)
264*4882a593Smuzhiyun * and without the support of division of 64bit numbers
265*4882a593Smuzhiyun * (so all divisions are done by using 32 bit)
266*4882a593Smuzhiyun * also NOTE the unit of those veriables
267*4882a593Smuzhiyun * time = (t - K) / 2^bictcp_HZ
268*4882a593Smuzhiyun * c = bic_scale >> 10
269*4882a593Smuzhiyun * rtt = (srtt >> 3) / HZ
270*4882a593Smuzhiyun * !!! The following code does not have overflow problems,
271*4882a593Smuzhiyun * if the cwnd < 1 million packets !!!
272*4882a593Smuzhiyun */
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun t = (s32)(tcp_jiffies32 - ca->epoch_start);
275*4882a593Smuzhiyun t += usecs_to_jiffies(ca->delay_min);
276*4882a593Smuzhiyun /* change the unit from HZ to bictcp_HZ */
277*4882a593Smuzhiyun t <<= BICTCP_HZ;
278*4882a593Smuzhiyun do_div(t, HZ);
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun if (t < ca->bic_K) /* t - K */
281*4882a593Smuzhiyun offs = ca->bic_K - t;
282*4882a593Smuzhiyun else
283*4882a593Smuzhiyun offs = t - ca->bic_K;
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun /* c/rtt * (t-K)^3 */
286*4882a593Smuzhiyun delta = (cube_rtt_scale * offs * offs * offs) >> (10+3*BICTCP_HZ);
287*4882a593Smuzhiyun if (t < ca->bic_K) /* below origin*/
288*4882a593Smuzhiyun bic_target = ca->bic_origin_point - delta;
289*4882a593Smuzhiyun else /* above origin*/
290*4882a593Smuzhiyun bic_target = ca->bic_origin_point + delta;
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun /* cubic function - calc bictcp_cnt*/
293*4882a593Smuzhiyun if (bic_target > cwnd) {
294*4882a593Smuzhiyun ca->cnt = cwnd / (bic_target - cwnd);
295*4882a593Smuzhiyun } else {
296*4882a593Smuzhiyun ca->cnt = 100 * cwnd; /* very small increment*/
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun /*
300*4882a593Smuzhiyun * The initial growth of cubic function may be too conservative
301*4882a593Smuzhiyun * when the available bandwidth is still unknown.
302*4882a593Smuzhiyun */
303*4882a593Smuzhiyun if (ca->last_max_cwnd == 0 && ca->cnt > 20)
304*4882a593Smuzhiyun ca->cnt = 20; /* increase cwnd 5% per RTT */
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun tcp_friendliness:
307*4882a593Smuzhiyun /* TCP Friendly */
308*4882a593Smuzhiyun if (tcp_friendliness) {
309*4882a593Smuzhiyun u32 scale = beta_scale;
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun delta = (cwnd * scale) >> 3;
312*4882a593Smuzhiyun while (ca->ack_cnt > delta) { /* update tcp cwnd */
313*4882a593Smuzhiyun ca->ack_cnt -= delta;
314*4882a593Smuzhiyun ca->tcp_cwnd++;
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun if (ca->tcp_cwnd > cwnd) { /* if bic is slower than tcp */
318*4882a593Smuzhiyun delta = ca->tcp_cwnd - cwnd;
319*4882a593Smuzhiyun max_cnt = cwnd / delta;
320*4882a593Smuzhiyun if (ca->cnt > max_cnt)
321*4882a593Smuzhiyun ca->cnt = max_cnt;
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun /* The maximum rate of cwnd increase CUBIC allows is 1 packet per
326*4882a593Smuzhiyun * 2 packets ACKed, meaning cwnd grows at 1.5x per RTT.
327*4882a593Smuzhiyun */
328*4882a593Smuzhiyun ca->cnt = max(ca->cnt, 2U);
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun
bictcp_cong_avoid(struct sock * sk,u32 ack,u32 acked)331*4882a593Smuzhiyun static void bictcp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun struct tcp_sock *tp = tcp_sk(sk);
334*4882a593Smuzhiyun struct bictcp *ca = inet_csk_ca(sk);
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun if (!tcp_is_cwnd_limited(sk))
337*4882a593Smuzhiyun return;
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun if (tcp_in_slow_start(tp)) {
340*4882a593Smuzhiyun acked = tcp_slow_start(tp, acked);
341*4882a593Smuzhiyun if (!acked)
342*4882a593Smuzhiyun return;
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun bictcp_update(ca, tp->snd_cwnd, acked);
345*4882a593Smuzhiyun tcp_cong_avoid_ai(tp, ca->cnt, acked);
346*4882a593Smuzhiyun }
347*4882a593Smuzhiyun
bictcp_recalc_ssthresh(struct sock * sk)348*4882a593Smuzhiyun static u32 bictcp_recalc_ssthresh(struct sock *sk)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun const struct tcp_sock *tp = tcp_sk(sk);
351*4882a593Smuzhiyun struct bictcp *ca = inet_csk_ca(sk);
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun ca->epoch_start = 0; /* end of epoch */
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun /* Wmax and fast convergence */
356*4882a593Smuzhiyun if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence)
357*4882a593Smuzhiyun ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta))
358*4882a593Smuzhiyun / (2 * BICTCP_BETA_SCALE);
359*4882a593Smuzhiyun else
360*4882a593Smuzhiyun ca->last_max_cwnd = tp->snd_cwnd;
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun
bictcp_state(struct sock * sk,u8 new_state)365*4882a593Smuzhiyun static void bictcp_state(struct sock *sk, u8 new_state)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun if (new_state == TCP_CA_Loss) {
368*4882a593Smuzhiyun bictcp_reset(inet_csk_ca(sk));
369*4882a593Smuzhiyun bictcp_hystart_reset(sk);
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun /* Account for TSO/GRO delays.
374*4882a593Smuzhiyun * Otherwise short RTT flows could get too small ssthresh, since during
375*4882a593Smuzhiyun * slow start we begin with small TSO packets and ca->delay_min would
376*4882a593Smuzhiyun * not account for long aggregation delay when TSO packets get bigger.
377*4882a593Smuzhiyun * Ideally even with a very small RTT we would like to have at least one
378*4882a593Smuzhiyun * TSO packet being sent and received by GRO, and another one in qdisc layer.
379*4882a593Smuzhiyun * We apply another 100% factor because @rate is doubled at this point.
380*4882a593Smuzhiyun * We cap the cushion to 1ms.
381*4882a593Smuzhiyun */
hystart_ack_delay(struct sock * sk)382*4882a593Smuzhiyun static u32 hystart_ack_delay(struct sock *sk)
383*4882a593Smuzhiyun {
384*4882a593Smuzhiyun unsigned long rate;
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun rate = READ_ONCE(sk->sk_pacing_rate);
387*4882a593Smuzhiyun if (!rate)
388*4882a593Smuzhiyun return 0;
389*4882a593Smuzhiyun return min_t(u64, USEC_PER_MSEC,
390*4882a593Smuzhiyun div64_ul((u64)GSO_MAX_SIZE * 4 * USEC_PER_SEC, rate));
391*4882a593Smuzhiyun }
392*4882a593Smuzhiyun
hystart_update(struct sock * sk,u32 delay)393*4882a593Smuzhiyun static void hystart_update(struct sock *sk, u32 delay)
394*4882a593Smuzhiyun {
395*4882a593Smuzhiyun struct tcp_sock *tp = tcp_sk(sk);
396*4882a593Smuzhiyun struct bictcp *ca = inet_csk_ca(sk);
397*4882a593Smuzhiyun u32 threshold;
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun if (after(tp->snd_una, ca->end_seq))
400*4882a593Smuzhiyun bictcp_hystart_reset(sk);
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun if (hystart_detect & HYSTART_ACK_TRAIN) {
403*4882a593Smuzhiyun u32 now = bictcp_clock_us(sk);
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun /* first detection parameter - ack-train detection */
406*4882a593Smuzhiyun if ((s32)(now - ca->last_ack) <= hystart_ack_delta_us) {
407*4882a593Smuzhiyun ca->last_ack = now;
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun threshold = ca->delay_min + hystart_ack_delay(sk);
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun /* Hystart ack train triggers if we get ack past
412*4882a593Smuzhiyun * ca->delay_min/2.
413*4882a593Smuzhiyun * Pacing might have delayed packets up to RTT/2
414*4882a593Smuzhiyun * during slow start.
415*4882a593Smuzhiyun */
416*4882a593Smuzhiyun if (sk->sk_pacing_status == SK_PACING_NONE)
417*4882a593Smuzhiyun threshold >>= 1;
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun if ((s32)(now - ca->round_start) > threshold) {
420*4882a593Smuzhiyun ca->found = 1;
421*4882a593Smuzhiyun pr_debug("hystart_ack_train (%u > %u) delay_min %u (+ ack_delay %u) cwnd %u\n",
422*4882a593Smuzhiyun now - ca->round_start, threshold,
423*4882a593Smuzhiyun ca->delay_min, hystart_ack_delay(sk), tp->snd_cwnd);
424*4882a593Smuzhiyun NET_INC_STATS(sock_net(sk),
425*4882a593Smuzhiyun LINUX_MIB_TCPHYSTARTTRAINDETECT);
426*4882a593Smuzhiyun NET_ADD_STATS(sock_net(sk),
427*4882a593Smuzhiyun LINUX_MIB_TCPHYSTARTTRAINCWND,
428*4882a593Smuzhiyun tp->snd_cwnd);
429*4882a593Smuzhiyun tp->snd_ssthresh = tp->snd_cwnd;
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun if (hystart_detect & HYSTART_DELAY) {
435*4882a593Smuzhiyun /* obtain the minimum delay of more than sampling packets */
436*4882a593Smuzhiyun if (ca->curr_rtt > delay)
437*4882a593Smuzhiyun ca->curr_rtt = delay;
438*4882a593Smuzhiyun if (ca->sample_cnt < HYSTART_MIN_SAMPLES) {
439*4882a593Smuzhiyun ca->sample_cnt++;
440*4882a593Smuzhiyun } else {
441*4882a593Smuzhiyun if (ca->curr_rtt > ca->delay_min +
442*4882a593Smuzhiyun HYSTART_DELAY_THRESH(ca->delay_min >> 3)) {
443*4882a593Smuzhiyun ca->found = 1;
444*4882a593Smuzhiyun NET_INC_STATS(sock_net(sk),
445*4882a593Smuzhiyun LINUX_MIB_TCPHYSTARTDELAYDETECT);
446*4882a593Smuzhiyun NET_ADD_STATS(sock_net(sk),
447*4882a593Smuzhiyun LINUX_MIB_TCPHYSTARTDELAYCWND,
448*4882a593Smuzhiyun tp->snd_cwnd);
449*4882a593Smuzhiyun tp->snd_ssthresh = tp->snd_cwnd;
450*4882a593Smuzhiyun }
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun }
454*4882a593Smuzhiyun
bictcp_acked(struct sock * sk,const struct ack_sample * sample)455*4882a593Smuzhiyun static void bictcp_acked(struct sock *sk, const struct ack_sample *sample)
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun const struct tcp_sock *tp = tcp_sk(sk);
458*4882a593Smuzhiyun struct bictcp *ca = inet_csk_ca(sk);
459*4882a593Smuzhiyun u32 delay;
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun /* Some calls are for duplicates without timetamps */
462*4882a593Smuzhiyun if (sample->rtt_us < 0)
463*4882a593Smuzhiyun return;
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun /* Discard delay samples right after fast recovery */
466*4882a593Smuzhiyun if (ca->epoch_start && (s32)(tcp_jiffies32 - ca->epoch_start) < HZ)
467*4882a593Smuzhiyun return;
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun delay = sample->rtt_us;
470*4882a593Smuzhiyun if (delay == 0)
471*4882a593Smuzhiyun delay = 1;
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun /* first time call or link delay decreases */
474*4882a593Smuzhiyun if (ca->delay_min == 0 || ca->delay_min > delay)
475*4882a593Smuzhiyun ca->delay_min = delay;
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun /* hystart triggers when cwnd is larger than some threshold */
478*4882a593Smuzhiyun if (!ca->found && tcp_in_slow_start(tp) && hystart &&
479*4882a593Smuzhiyun tp->snd_cwnd >= hystart_low_window)
480*4882a593Smuzhiyun hystart_update(sk, delay);
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun static struct tcp_congestion_ops cubictcp __read_mostly = {
484*4882a593Smuzhiyun .init = bictcp_init,
485*4882a593Smuzhiyun .ssthresh = bictcp_recalc_ssthresh,
486*4882a593Smuzhiyun .cong_avoid = bictcp_cong_avoid,
487*4882a593Smuzhiyun .set_state = bictcp_state,
488*4882a593Smuzhiyun .undo_cwnd = tcp_reno_undo_cwnd,
489*4882a593Smuzhiyun .cwnd_event = bictcp_cwnd_event,
490*4882a593Smuzhiyun .pkts_acked = bictcp_acked,
491*4882a593Smuzhiyun .owner = THIS_MODULE,
492*4882a593Smuzhiyun .name = "cubic",
493*4882a593Smuzhiyun };
494*4882a593Smuzhiyun
cubictcp_register(void)495*4882a593Smuzhiyun static int __init cubictcp_register(void)
496*4882a593Smuzhiyun {
497*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun /* Precompute a bunch of the scaling factors that are used per-packet
500*4882a593Smuzhiyun * based on SRTT of 100ms
501*4882a593Smuzhiyun */
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun beta_scale = 8*(BICTCP_BETA_SCALE+beta) / 3
504*4882a593Smuzhiyun / (BICTCP_BETA_SCALE - beta);
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun cube_rtt_scale = (bic_scale * 10); /* 1024*c/rtt */
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun /* calculate the "K" for (wmax-cwnd) = c/rtt * K^3
509*4882a593Smuzhiyun * so K = cubic_root( (wmax-cwnd)*rtt/c )
510*4882a593Smuzhiyun * the unit of K is bictcp_HZ=2^10, not HZ
511*4882a593Smuzhiyun *
512*4882a593Smuzhiyun * c = bic_scale >> 10
513*4882a593Smuzhiyun * rtt = 100ms
514*4882a593Smuzhiyun *
515*4882a593Smuzhiyun * the following code has been designed and tested for
516*4882a593Smuzhiyun * cwnd < 1 million packets
517*4882a593Smuzhiyun * RTT < 100 seconds
518*4882a593Smuzhiyun * HZ < 1,000,00 (corresponding to 10 nano-second)
519*4882a593Smuzhiyun */
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun /* 1/c * 2^2*bictcp_HZ * srtt */
522*4882a593Smuzhiyun cube_factor = 1ull << (10+3*BICTCP_HZ); /* 2^40 */
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun /* divide by bic_scale and by constant Srtt (100ms) */
525*4882a593Smuzhiyun do_div(cube_factor, bic_scale * 10);
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun return tcp_register_congestion_control(&cubictcp);
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun
cubictcp_unregister(void)530*4882a593Smuzhiyun static void __exit cubictcp_unregister(void)
531*4882a593Smuzhiyun {
532*4882a593Smuzhiyun tcp_unregister_congestion_control(&cubictcp);
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun module_init(cubictcp_register);
536*4882a593Smuzhiyun module_exit(cubictcp_unregister);
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun MODULE_AUTHOR("Sangtae Ha, Stephen Hemminger");
539*4882a593Smuzhiyun MODULE_LICENSE("GPL");
540*4882a593Smuzhiyun MODULE_DESCRIPTION("CUBIC TCP");
541*4882a593Smuzhiyun MODULE_VERSION("2.3");
542