xref: /linux/net/ipv4/tcp_cubic.c (revision 7e58886b45bc4a309aeaa8178ef89ff767daaf7f)
1df3271f3SStephen Hemminger /*
2df3271f3SStephen Hemminger  * TCP CUBIC: Binary Increase Congestion control for TCP v2.0
3df3271f3SStephen Hemminger  *
4df3271f3SStephen Hemminger  * This is from the implementation of CUBIC TCP in
5df3271f3SStephen Hemminger  * Injong Rhee, Lisong Xu.
6df3271f3SStephen Hemminger  *  "CUBIC: A New TCP-Friendly High-Speed TCP Variant
7df3271f3SStephen Hemminger  *  in PFLDnet 2005
8df3271f3SStephen Hemminger  * Available from:
9df3271f3SStephen Hemminger  *  http://www.csc.ncsu.edu/faculty/rhee/export/bitcp/cubic-paper.pdf
10df3271f3SStephen Hemminger  *
11df3271f3SStephen Hemminger  * Unless CUBIC is enabled and congestion window is large
12df3271f3SStephen Hemminger  * this behaves the same as the original Reno.
13df3271f3SStephen Hemminger  */
14df3271f3SStephen Hemminger 
15df3271f3SStephen Hemminger #include <linux/mm.h>
16df3271f3SStephen Hemminger #include <linux/module.h>
17df3271f3SStephen Hemminger #include <net/tcp.h>
1889b3d9aaSStephen Hemminger #include <asm/div64.h>
19df3271f3SStephen Hemminger 
20df3271f3SStephen Hemminger #define BICTCP_BETA_SCALE    1024	/* Scale factor beta calculation
21df3271f3SStephen Hemminger 					 * max_cwnd = snd_cwnd * beta
22df3271f3SStephen Hemminger 					 */
23df3271f3SStephen Hemminger #define BICTCP_B		4	 /*
24df3271f3SStephen Hemminger 					  * In binary search,
25df3271f3SStephen Hemminger 					  * go to point (max+min)/N
26df3271f3SStephen Hemminger 					  */
27df3271f3SStephen Hemminger #define	BICTCP_HZ		10	/* BIC HZ 2^10 = 1024 */
28df3271f3SStephen Hemminger 
2959758f44SStephen Hemminger static int fast_convergence __read_mostly = 1;
3059758f44SStephen Hemminger static int max_increment __read_mostly = 16;
3159758f44SStephen Hemminger static int beta __read_mostly = 819;	/* = 819/1024 (BICTCP_BETA_SCALE) */
3259758f44SStephen Hemminger static int initial_ssthresh __read_mostly = 100;
3359758f44SStephen Hemminger static int bic_scale __read_mostly = 41;
3459758f44SStephen Hemminger static int tcp_friendliness __read_mostly = 1;
35df3271f3SStephen Hemminger 
3659758f44SStephen Hemminger static u32 cube_rtt_scale __read_mostly;
3759758f44SStephen Hemminger static u32 beta_scale __read_mostly;
3859758f44SStephen Hemminger static u64 cube_factor __read_mostly;
3989b3d9aaSStephen Hemminger 
4089b3d9aaSStephen Hemminger /* Note parameters that are used for precomputing scale factors are read-only */
41df3271f3SStephen Hemminger module_param(fast_convergence, int, 0644);
42df3271f3SStephen Hemminger MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
43df3271f3SStephen Hemminger module_param(max_increment, int, 0644);
44df3271f3SStephen Hemminger MODULE_PARM_DESC(max_increment, "Limit on increment allowed during binary search");
4589b3d9aaSStephen Hemminger module_param(beta, int, 0444);
46df3271f3SStephen Hemminger MODULE_PARM_DESC(beta, "beta for multiplicative increase");
47df3271f3SStephen Hemminger module_param(initial_ssthresh, int, 0644);
48df3271f3SStephen Hemminger MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
4989b3d9aaSStephen Hemminger module_param(bic_scale, int, 0444);
50df3271f3SStephen Hemminger MODULE_PARM_DESC(bic_scale, "scale (scaled by 1024) value for bic function (bic_scale/1024)");
51df3271f3SStephen Hemminger module_param(tcp_friendliness, int, 0644);
52df3271f3SStephen Hemminger MODULE_PARM_DESC(tcp_friendliness, "turn on/off tcp friendliness");
53df3271f3SStephen Hemminger 
54df3271f3SStephen Hemminger /* BIC TCP Parameters */
55df3271f3SStephen Hemminger struct bictcp {
56df3271f3SStephen Hemminger 	u32	cnt;		/* increase cwnd by 1 after ACKs */
57df3271f3SStephen Hemminger 	u32 	last_max_cwnd;	/* last maximum snd_cwnd */
58df3271f3SStephen Hemminger 	u32	loss_cwnd;	/* congestion window at last loss */
59df3271f3SStephen Hemminger 	u32	last_cwnd;	/* the last snd_cwnd */
60df3271f3SStephen Hemminger 	u32	last_time;	/* time when updated last_cwnd */
61df3271f3SStephen Hemminger 	u32	bic_origin_point;/* origin point of bic function */
62df3271f3SStephen Hemminger 	u32	bic_K;		/* time to origin point from the beginning of the current epoch */
63df3271f3SStephen Hemminger 	u32	delay_min;	/* min delay */
64df3271f3SStephen Hemminger 	u32	epoch_start;	/* beginning of an epoch */
65df3271f3SStephen Hemminger 	u32	ack_cnt;	/* number of acks */
66df3271f3SStephen Hemminger 	u32	tcp_cwnd;	/* estimated tcp cwnd */
67df3271f3SStephen Hemminger #define ACK_RATIO_SHIFT	4
68df3271f3SStephen Hemminger 	u32	delayed_ack;	/* estimate the ratio of Packets/ACKs << 4 */
69df3271f3SStephen Hemminger };
70df3271f3SStephen Hemminger 
71df3271f3SStephen Hemminger static inline void bictcp_reset(struct bictcp *ca)
72df3271f3SStephen Hemminger {
73df3271f3SStephen Hemminger 	ca->cnt = 0;
74df3271f3SStephen Hemminger 	ca->last_max_cwnd = 0;
75df3271f3SStephen Hemminger 	ca->loss_cwnd = 0;
76df3271f3SStephen Hemminger 	ca->last_cwnd = 0;
77df3271f3SStephen Hemminger 	ca->last_time = 0;
78df3271f3SStephen Hemminger 	ca->bic_origin_point = 0;
79df3271f3SStephen Hemminger 	ca->bic_K = 0;
80df3271f3SStephen Hemminger 	ca->delay_min = 0;
81df3271f3SStephen Hemminger 	ca->epoch_start = 0;
82df3271f3SStephen Hemminger 	ca->delayed_ack = 2 << ACK_RATIO_SHIFT;
83df3271f3SStephen Hemminger 	ca->ack_cnt = 0;
84df3271f3SStephen Hemminger 	ca->tcp_cwnd = 0;
85df3271f3SStephen Hemminger }
86df3271f3SStephen Hemminger 
87df3271f3SStephen Hemminger static void bictcp_init(struct sock *sk)
88df3271f3SStephen Hemminger {
89df3271f3SStephen Hemminger 	bictcp_reset(inet_csk_ca(sk));
90df3271f3SStephen Hemminger 	if (initial_ssthresh)
91df3271f3SStephen Hemminger 		tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
92df3271f3SStephen Hemminger }
93df3271f3SStephen Hemminger 
94*7e58886bSStephen Hemminger /* calculate the cubic root of x using a table lookup followed by one
95*7e58886bSStephen Hemminger  * Newton-Raphson iteration.
96*7e58886bSStephen Hemminger  * Avg err ~= 0.195%
97df3271f3SStephen Hemminger  */
989eb2d627SStephen Hemminger static u32 cubic_root(u64 a)
99df3271f3SStephen Hemminger {
100*7e58886bSStephen Hemminger 	u32 x, b, shift;
101*7e58886bSStephen Hemminger 	/*
102*7e58886bSStephen Hemminger 	 * cbrt(x) MSB values for x MSB values in [0..63].
103*7e58886bSStephen Hemminger 	 * Precomputed then refined by hand - Willy Tarreau
104*7e58886bSStephen Hemminger 	 *
105*7e58886bSStephen Hemminger 	 * For x in [0..63],
106*7e58886bSStephen Hemminger 	 *   v = cbrt(x << 18) - 1
107*7e58886bSStephen Hemminger 	 *   cbrt(x) = (v[x] + 10) >> 6
1089eb2d627SStephen Hemminger 	 */
109*7e58886bSStephen Hemminger 	static const u8 v[] = {
110*7e58886bSStephen Hemminger 		/* 0x00 */    0,   54,   54,   54,  118,  118,  118,  118,
111*7e58886bSStephen Hemminger 		/* 0x08 */  123,  129,  134,  138,  143,  147,  151,  156,
112*7e58886bSStephen Hemminger 		/* 0x10 */  157,  161,  164,  168,  170,  173,  176,  179,
113*7e58886bSStephen Hemminger 		/* 0x18 */  181,  185,  187,  190,  192,  194,  197,  199,
114*7e58886bSStephen Hemminger 		/* 0x20 */  200,  202,  204,  206,  209,  211,  213,  215,
115*7e58886bSStephen Hemminger 		/* 0x28 */  217,  219,  221,  222,  224,  225,  227,  229,
116*7e58886bSStephen Hemminger 		/* 0x30 */  231,  232,  234,  236,  237,  239,  240,  242,
117*7e58886bSStephen Hemminger 		/* 0x38 */  244,  245,  246,  248,  250,  251,  252,  254,
118*7e58886bSStephen Hemminger 	};
119df3271f3SStephen Hemminger 
120*7e58886bSStephen Hemminger 	b = fls64(a);
121*7e58886bSStephen Hemminger 	if (b < 7) {
122*7e58886bSStephen Hemminger 		/* a in [0..63] */
123*7e58886bSStephen Hemminger 		return ((u32)v[(u32)a] + 35) >> 6;
124*7e58886bSStephen Hemminger 	}
125df3271f3SStephen Hemminger 
126*7e58886bSStephen Hemminger 	b = ((b * 84) >> 8) - 1;
127*7e58886bSStephen Hemminger 	shift = (a >> (b * 3));
128*7e58886bSStephen Hemminger 
129*7e58886bSStephen Hemminger 	x = ((u32)(((u32)v[shift] + 10) << b)) >> 6;
130*7e58886bSStephen Hemminger 
131*7e58886bSStephen Hemminger 	/*
132*7e58886bSStephen Hemminger 	 * Newton-Raphson iteration
133*7e58886bSStephen Hemminger 	 *                         2
134*7e58886bSStephen Hemminger 	 * x    = ( 2 * x  +  a / x  ) / 3
135*7e58886bSStephen Hemminger 	 *  k+1          k         k
136*7e58886bSStephen Hemminger 	 */
137*7e58886bSStephen Hemminger 	x = (2 * x + (u32)div64_64(a, (u64)x * (u64)(x - 1)));
138*7e58886bSStephen Hemminger 	x = ((x * 341) >> 10);
1399eb2d627SStephen Hemminger 	return x;
140df3271f3SStephen Hemminger }
141df3271f3SStephen Hemminger 
142df3271f3SStephen Hemminger /*
143df3271f3SStephen Hemminger  * Compute congestion window to use.
144df3271f3SStephen Hemminger  */
145df3271f3SStephen Hemminger static inline void bictcp_update(struct bictcp *ca, u32 cwnd)
146df3271f3SStephen Hemminger {
14789b3d9aaSStephen Hemminger 	u64 offs;
14889b3d9aaSStephen Hemminger 	u32 delta, t, bic_target, min_cnt, max_cnt;
149df3271f3SStephen Hemminger 
150df3271f3SStephen Hemminger 	ca->ack_cnt++;	/* count the number of ACKs */
151df3271f3SStephen Hemminger 
152df3271f3SStephen Hemminger 	if (ca->last_cwnd == cwnd &&
153df3271f3SStephen Hemminger 	    (s32)(tcp_time_stamp - ca->last_time) <= HZ / 32)
154df3271f3SStephen Hemminger 		return;
155df3271f3SStephen Hemminger 
156df3271f3SStephen Hemminger 	ca->last_cwnd = cwnd;
157df3271f3SStephen Hemminger 	ca->last_time = tcp_time_stamp;
158df3271f3SStephen Hemminger 
159df3271f3SStephen Hemminger 	if (ca->epoch_start == 0) {
160df3271f3SStephen Hemminger 		ca->epoch_start = tcp_time_stamp;	/* record the beginning of an epoch */
161df3271f3SStephen Hemminger 		ca->ack_cnt = 1;			/* start counting */
162df3271f3SStephen Hemminger 		ca->tcp_cwnd = cwnd;			/* syn with cubic */
163df3271f3SStephen Hemminger 
164df3271f3SStephen Hemminger 		if (ca->last_max_cwnd <= cwnd) {
165df3271f3SStephen Hemminger 			ca->bic_K = 0;
166df3271f3SStephen Hemminger 			ca->bic_origin_point = cwnd;
167df3271f3SStephen Hemminger 		} else {
16889b3d9aaSStephen Hemminger 			/* Compute new K based on
16989b3d9aaSStephen Hemminger 			 * (wmax-cwnd) * (srtt>>3 / HZ) / c * 2^(3*bictcp_HZ)
17089b3d9aaSStephen Hemminger 			 */
17189b3d9aaSStephen Hemminger 			ca->bic_K = cubic_root(cube_factor
17289b3d9aaSStephen Hemminger 					       * (ca->last_max_cwnd - cwnd));
173df3271f3SStephen Hemminger 			ca->bic_origin_point = ca->last_max_cwnd;
174df3271f3SStephen Hemminger 		}
175df3271f3SStephen Hemminger 	}
176df3271f3SStephen Hemminger 
177df3271f3SStephen Hemminger 	/* cubic function - calc*/
178df3271f3SStephen Hemminger 	/* calculate c * time^3 / rtt,
179df3271f3SStephen Hemminger 	 *  while considering overflow in calculation of time^3
18089b3d9aaSStephen Hemminger 	 * (so time^3 is done by using 64 bit)
181df3271f3SStephen Hemminger 	 * and without the support of division of 64bit numbers
18289b3d9aaSStephen Hemminger 	 * (so all divisions are done by using 32 bit)
183df3271f3SStephen Hemminger 	 *  also NOTE the unit of those veriables
184df3271f3SStephen Hemminger 	 *	  time  = (t - K) / 2^bictcp_HZ
185df3271f3SStephen Hemminger 	 *	  c = bic_scale >> 10
186df3271f3SStephen Hemminger 	 * rtt  = (srtt >> 3) / HZ
187df3271f3SStephen Hemminger 	 * !!! The following code does not have overflow problems,
188df3271f3SStephen Hemminger 	 * if the cwnd < 1 million packets !!!
189df3271f3SStephen Hemminger 	 */
190df3271f3SStephen Hemminger 
191df3271f3SStephen Hemminger 	/* change the unit from HZ to bictcp_HZ */
19222119240SStephen Hemminger 	t = ((tcp_time_stamp + (ca->delay_min>>3) - ca->epoch_start)
193df3271f3SStephen Hemminger 	     << BICTCP_HZ) / HZ;
194df3271f3SStephen Hemminger 
195df3271f3SStephen Hemminger 	if (t < ca->bic_K)		/* t - K */
19689b3d9aaSStephen Hemminger 		offs = ca->bic_K - t;
197df3271f3SStephen Hemminger 	else
19889b3d9aaSStephen Hemminger 		offs = t - ca->bic_K;
199df3271f3SStephen Hemminger 
20089b3d9aaSStephen Hemminger 	/* c/rtt * (t-K)^3 */
20189b3d9aaSStephen Hemminger 	delta = (cube_rtt_scale * offs * offs * offs) >> (10+3*BICTCP_HZ);
202df3271f3SStephen Hemminger 	if (t < ca->bic_K)                                	/* below origin*/
20389b3d9aaSStephen Hemminger 		bic_target = ca->bic_origin_point - delta;
204df3271f3SStephen Hemminger 	else                                                	/* above origin*/
20589b3d9aaSStephen Hemminger 		bic_target = ca->bic_origin_point + delta;
206df3271f3SStephen Hemminger 
207df3271f3SStephen Hemminger 	/* cubic function - calc bictcp_cnt*/
208df3271f3SStephen Hemminger 	if (bic_target > cwnd) {
209df3271f3SStephen Hemminger 		ca->cnt = cwnd / (bic_target - cwnd);
210df3271f3SStephen Hemminger 	} else {
211df3271f3SStephen Hemminger 		ca->cnt = 100 * cwnd;              /* very small increment*/
212df3271f3SStephen Hemminger 	}
213df3271f3SStephen Hemminger 
214df3271f3SStephen Hemminger 	if (ca->delay_min > 0) {
215df3271f3SStephen Hemminger 		/* max increment = Smax * rtt / 0.1  */
216df3271f3SStephen Hemminger 		min_cnt = (cwnd * HZ * 8)/(10 * max_increment * ca->delay_min);
217df3271f3SStephen Hemminger 		if (ca->cnt < min_cnt)
218df3271f3SStephen Hemminger 			ca->cnt = min_cnt;
219df3271f3SStephen Hemminger 	}
220df3271f3SStephen Hemminger 
221df3271f3SStephen Hemminger 	/* slow start and low utilization  */
222df3271f3SStephen Hemminger 	if (ca->loss_cwnd == 0)		/* could be aggressive in slow start */
223df3271f3SStephen Hemminger 		ca->cnt = 50;
224df3271f3SStephen Hemminger 
225df3271f3SStephen Hemminger 	/* TCP Friendly */
226df3271f3SStephen Hemminger 	if (tcp_friendliness) {
22789b3d9aaSStephen Hemminger 		u32 scale = beta_scale;
22889b3d9aaSStephen Hemminger 		delta = (cwnd * scale) >> 3;
22989b3d9aaSStephen Hemminger 		while (ca->ack_cnt > delta) {		/* update tcp cwnd */
23089b3d9aaSStephen Hemminger 			ca->ack_cnt -= delta;
231df3271f3SStephen Hemminger 			ca->tcp_cwnd++;
232df3271f3SStephen Hemminger 		}
233df3271f3SStephen Hemminger 
234df3271f3SStephen Hemminger 		if (ca->tcp_cwnd > cwnd){	/* if bic is slower than tcp */
23589b3d9aaSStephen Hemminger 			delta = ca->tcp_cwnd - cwnd;
23689b3d9aaSStephen Hemminger 			max_cnt = cwnd / delta;
237df3271f3SStephen Hemminger 			if (ca->cnt > max_cnt)
238df3271f3SStephen Hemminger 				ca->cnt = max_cnt;
239df3271f3SStephen Hemminger 		}
240df3271f3SStephen Hemminger 	}
241df3271f3SStephen Hemminger 
242df3271f3SStephen Hemminger 	ca->cnt = (ca->cnt << ACK_RATIO_SHIFT) / ca->delayed_ack;
243df3271f3SStephen Hemminger 	if (ca->cnt == 0)			/* cannot be zero */
244df3271f3SStephen Hemminger 		ca->cnt = 1;
245df3271f3SStephen Hemminger }
246df3271f3SStephen Hemminger 
247df3271f3SStephen Hemminger 
248df3271f3SStephen Hemminger /* Keep track of minimum rtt */
249df3271f3SStephen Hemminger static inline void measure_delay(struct sock *sk)
250df3271f3SStephen Hemminger {
251df3271f3SStephen Hemminger 	const struct tcp_sock *tp = tcp_sk(sk);
252df3271f3SStephen Hemminger 	struct bictcp *ca = inet_csk_ca(sk);
253df3271f3SStephen Hemminger 	u32 delay;
254df3271f3SStephen Hemminger 
255df3271f3SStephen Hemminger 	/* No time stamp */
256df3271f3SStephen Hemminger 	if (!(tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) ||
257df3271f3SStephen Hemminger 	     /* Discard delay samples right after fast recovery */
258df3271f3SStephen Hemminger 	    (s32)(tcp_time_stamp - ca->epoch_start) < HZ)
259df3271f3SStephen Hemminger 		return;
260df3271f3SStephen Hemminger 
26122119240SStephen Hemminger 	delay = (tcp_time_stamp - tp->rx_opt.rcv_tsecr)<<3;
262df3271f3SStephen Hemminger 	if (delay == 0)
263df3271f3SStephen Hemminger 		delay = 1;
264df3271f3SStephen Hemminger 
265df3271f3SStephen Hemminger 	/* first time call or link delay decreases */
266df3271f3SStephen Hemminger 	if (ca->delay_min == 0 || ca->delay_min > delay)
267df3271f3SStephen Hemminger 		ca->delay_min = delay;
268df3271f3SStephen Hemminger }
269df3271f3SStephen Hemminger 
270df3271f3SStephen Hemminger static void bictcp_cong_avoid(struct sock *sk, u32 ack,
271df3271f3SStephen Hemminger 			      u32 seq_rtt, u32 in_flight, int data_acked)
272df3271f3SStephen Hemminger {
273df3271f3SStephen Hemminger 	struct tcp_sock *tp = tcp_sk(sk);
274df3271f3SStephen Hemminger 	struct bictcp *ca = inet_csk_ca(sk);
275df3271f3SStephen Hemminger 
276df3271f3SStephen Hemminger 	if (data_acked)
277df3271f3SStephen Hemminger 		measure_delay(sk);
278df3271f3SStephen Hemminger 
279df3271f3SStephen Hemminger 	if (!tcp_is_cwnd_limited(sk, in_flight))
280df3271f3SStephen Hemminger 		return;
281df3271f3SStephen Hemminger 
282df3271f3SStephen Hemminger 	if (tp->snd_cwnd <= tp->snd_ssthresh)
283df3271f3SStephen Hemminger 		tcp_slow_start(tp);
284df3271f3SStephen Hemminger 	else {
285df3271f3SStephen Hemminger 		bictcp_update(ca, tp->snd_cwnd);
286df3271f3SStephen Hemminger 
287df3271f3SStephen Hemminger 		/* In dangerous area, increase slowly.
288df3271f3SStephen Hemminger 		 * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
289df3271f3SStephen Hemminger 		 */
290df3271f3SStephen Hemminger 		if (tp->snd_cwnd_cnt >= ca->cnt) {
291df3271f3SStephen Hemminger 			if (tp->snd_cwnd < tp->snd_cwnd_clamp)
292df3271f3SStephen Hemminger 				tp->snd_cwnd++;
293df3271f3SStephen Hemminger 			tp->snd_cwnd_cnt = 0;
294df3271f3SStephen Hemminger 		} else
295df3271f3SStephen Hemminger 			tp->snd_cwnd_cnt++;
296df3271f3SStephen Hemminger 	}
297df3271f3SStephen Hemminger 
298df3271f3SStephen Hemminger }
299df3271f3SStephen Hemminger 
300df3271f3SStephen Hemminger static u32 bictcp_recalc_ssthresh(struct sock *sk)
301df3271f3SStephen Hemminger {
302df3271f3SStephen Hemminger 	const struct tcp_sock *tp = tcp_sk(sk);
303df3271f3SStephen Hemminger 	struct bictcp *ca = inet_csk_ca(sk);
304df3271f3SStephen Hemminger 
305df3271f3SStephen Hemminger 	ca->epoch_start = 0;	/* end of epoch */
306df3271f3SStephen Hemminger 
307df3271f3SStephen Hemminger 	/* Wmax and fast convergence */
308df3271f3SStephen Hemminger 	if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence)
309df3271f3SStephen Hemminger 		ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta))
310df3271f3SStephen Hemminger 			/ (2 * BICTCP_BETA_SCALE);
311df3271f3SStephen Hemminger 	else
312df3271f3SStephen Hemminger 		ca->last_max_cwnd = tp->snd_cwnd;
313df3271f3SStephen Hemminger 
314df3271f3SStephen Hemminger 	ca->loss_cwnd = tp->snd_cwnd;
315df3271f3SStephen Hemminger 
316df3271f3SStephen Hemminger 	return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
317df3271f3SStephen Hemminger }
318df3271f3SStephen Hemminger 
319df3271f3SStephen Hemminger static u32 bictcp_undo_cwnd(struct sock *sk)
320df3271f3SStephen Hemminger {
321df3271f3SStephen Hemminger 	struct bictcp *ca = inet_csk_ca(sk);
322df3271f3SStephen Hemminger 
323df3271f3SStephen Hemminger 	return max(tcp_sk(sk)->snd_cwnd, ca->last_max_cwnd);
324df3271f3SStephen Hemminger }
325df3271f3SStephen Hemminger 
326df3271f3SStephen Hemminger static void bictcp_state(struct sock *sk, u8 new_state)
327df3271f3SStephen Hemminger {
328df3271f3SStephen Hemminger 	if (new_state == TCP_CA_Loss)
329df3271f3SStephen Hemminger 		bictcp_reset(inet_csk_ca(sk));
330df3271f3SStephen Hemminger }
331df3271f3SStephen Hemminger 
332df3271f3SStephen Hemminger /* Track delayed acknowledgment ratio using sliding window
333df3271f3SStephen Hemminger  * ratio = (15*ratio + sample) / 16
334df3271f3SStephen Hemminger  */
335df3271f3SStephen Hemminger static void bictcp_acked(struct sock *sk, u32 cnt)
336df3271f3SStephen Hemminger {
337df3271f3SStephen Hemminger 	const struct inet_connection_sock *icsk = inet_csk(sk);
338df3271f3SStephen Hemminger 
339df3271f3SStephen Hemminger 	if (cnt > 0 && icsk->icsk_ca_state == TCP_CA_Open) {
340df3271f3SStephen Hemminger 		struct bictcp *ca = inet_csk_ca(sk);
341df3271f3SStephen Hemminger 		cnt -= ca->delayed_ack >> ACK_RATIO_SHIFT;
342df3271f3SStephen Hemminger 		ca->delayed_ack += cnt;
343df3271f3SStephen Hemminger 	}
344df3271f3SStephen Hemminger }
345df3271f3SStephen Hemminger 
346df3271f3SStephen Hemminger 
347df3271f3SStephen Hemminger static struct tcp_congestion_ops cubictcp = {
348df3271f3SStephen Hemminger 	.init		= bictcp_init,
349df3271f3SStephen Hemminger 	.ssthresh	= bictcp_recalc_ssthresh,
350df3271f3SStephen Hemminger 	.cong_avoid	= bictcp_cong_avoid,
351df3271f3SStephen Hemminger 	.set_state	= bictcp_state,
352df3271f3SStephen Hemminger 	.undo_cwnd	= bictcp_undo_cwnd,
353df3271f3SStephen Hemminger 	.pkts_acked     = bictcp_acked,
354df3271f3SStephen Hemminger 	.owner		= THIS_MODULE,
355df3271f3SStephen Hemminger 	.name		= "cubic",
356df3271f3SStephen Hemminger };
357df3271f3SStephen Hemminger 
358df3271f3SStephen Hemminger static int __init cubictcp_register(void)
359df3271f3SStephen Hemminger {
36074975d40SAlexey Dobriyan 	BUILD_BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
36189b3d9aaSStephen Hemminger 
36289b3d9aaSStephen Hemminger 	/* Precompute a bunch of the scaling factors that are used per-packet
36389b3d9aaSStephen Hemminger 	 * based on SRTT of 100ms
36489b3d9aaSStephen Hemminger 	 */
36589b3d9aaSStephen Hemminger 
36689b3d9aaSStephen Hemminger 	beta_scale = 8*(BICTCP_BETA_SCALE+beta)/ 3 / (BICTCP_BETA_SCALE - beta);
36789b3d9aaSStephen Hemminger 
36822119240SStephen Hemminger 	cube_rtt_scale = (bic_scale * 10);	/* 1024*c/rtt */
36989b3d9aaSStephen Hemminger 
37089b3d9aaSStephen Hemminger 	/* calculate the "K" for (wmax-cwnd) = c/rtt * K^3
37189b3d9aaSStephen Hemminger 	 *  so K = cubic_root( (wmax-cwnd)*rtt/c )
37289b3d9aaSStephen Hemminger 	 * the unit of K is bictcp_HZ=2^10, not HZ
37389b3d9aaSStephen Hemminger 	 *
37489b3d9aaSStephen Hemminger 	 *  c = bic_scale >> 10
37589b3d9aaSStephen Hemminger 	 *  rtt = 100ms
37689b3d9aaSStephen Hemminger 	 *
37789b3d9aaSStephen Hemminger 	 * the following code has been designed and tested for
37889b3d9aaSStephen Hemminger 	 * cwnd < 1 million packets
37989b3d9aaSStephen Hemminger 	 * RTT < 100 seconds
38089b3d9aaSStephen Hemminger 	 * HZ < 1,000,00  (corresponding to 10 nano-second)
38189b3d9aaSStephen Hemminger 	 */
38289b3d9aaSStephen Hemminger 
38389b3d9aaSStephen Hemminger 	/* 1/c * 2^2*bictcp_HZ * srtt */
38489b3d9aaSStephen Hemminger 	cube_factor = 1ull << (10+3*BICTCP_HZ); /* 2^40 */
38589b3d9aaSStephen Hemminger 
38689b3d9aaSStephen Hemminger 	/* divide by bic_scale and by constant Srtt (100ms) */
38789b3d9aaSStephen Hemminger 	do_div(cube_factor, bic_scale * 10);
38889b3d9aaSStephen Hemminger 
389df3271f3SStephen Hemminger 	return tcp_register_congestion_control(&cubictcp);
390df3271f3SStephen Hemminger }
391df3271f3SStephen Hemminger 
392df3271f3SStephen Hemminger static void __exit cubictcp_unregister(void)
393df3271f3SStephen Hemminger {
394df3271f3SStephen Hemminger 	tcp_unregister_congestion_control(&cubictcp);
395df3271f3SStephen Hemminger }
396df3271f3SStephen Hemminger 
397df3271f3SStephen Hemminger module_init(cubictcp_register);
398df3271f3SStephen Hemminger module_exit(cubictcp_unregister);
399df3271f3SStephen Hemminger 
400df3271f3SStephen Hemminger MODULE_AUTHOR("Sangtae Ha, Stephen Hemminger");
401df3271f3SStephen Hemminger MODULE_LICENSE("GPL");
402df3271f3SStephen Hemminger MODULE_DESCRIPTION("CUBIC TCP");
403df3271f3SStephen Hemminger MODULE_VERSION("2.0");
404