xref: /linux/net/ipv4/tcp_cubic.c (revision 30927520dbae297182990bb21d08762bcc35ce1d)
1df3271f3SStephen Hemminger /*
2ae27e98aSSangtae Ha  * TCP CUBIC: Binary Increase Congestion control for TCP v2.3
36b3d6263SSangtae Ha  * Home page:
46b3d6263SSangtae Ha  *      http://netsrv.csc.ncsu.edu/twiki/bin/view/Main/BIC
5df3271f3SStephen Hemminger  * This is from the implementation of CUBIC TCP in
6ae27e98aSSangtae Ha  * Sangtae Ha, Injong Rhee and Lisong Xu,
7ae27e98aSSangtae Ha  *  "CUBIC: A New TCP-Friendly High-Speed TCP Variant"
8ae27e98aSSangtae Ha  *  in ACM SIGOPS Operating System Review, July 2008.
9df3271f3SStephen Hemminger  * Available from:
10ae27e98aSSangtae Ha  *  http://netsrv.csc.ncsu.edu/export/cubic_a_new_tcp_2008.pdf
11ae27e98aSSangtae Ha  *
12ae27e98aSSangtae Ha  * CUBIC integrates a new slow start algorithm, called HyStart.
13ae27e98aSSangtae Ha  * The details of HyStart are presented in
14ae27e98aSSangtae Ha  *  Sangtae Ha and Injong Rhee,
15ae27e98aSSangtae Ha  *  "Taming the Elephants: New TCP Slow Start", NCSU TechReport 2008.
16ae27e98aSSangtae Ha  * Available from:
17ae27e98aSSangtae Ha  *  http://netsrv.csc.ncsu.edu/export/hystart_techreport_2008.pdf
18ae27e98aSSangtae Ha  *
19ae27e98aSSangtae Ha  * All testing results are available from:
20ae27e98aSSangtae Ha  * http://netsrv.csc.ncsu.edu/wiki/index.php/TCP_Testing
21df3271f3SStephen Hemminger  *
22df3271f3SStephen Hemminger  * Unless CUBIC is enabled and congestion window is large
23df3271f3SStephen Hemminger  * this behaves the same as the original Reno.
24df3271f3SStephen Hemminger  */
25df3271f3SStephen Hemminger 
26df3271f3SStephen Hemminger #include <linux/mm.h>
27df3271f3SStephen Hemminger #include <linux/module.h>
286f6d6a1aSRoman Zippel #include <linux/math64.h>
29df3271f3SStephen Hemminger #include <net/tcp.h>
30df3271f3SStephen Hemminger 
31df3271f3SStephen Hemminger #define BICTCP_BETA_SCALE    1024	/* Scale factor beta calculation
32df3271f3SStephen Hemminger 					 * max_cwnd = snd_cwnd * beta
33df3271f3SStephen Hemminger 					 */
34df3271f3SStephen Hemminger #define	BICTCP_HZ		10	/* BIC HZ 2^10 = 1024 */
35df3271f3SStephen Hemminger 
36ae27e98aSSangtae Ha /* Two methods of hybrid slow start */
37ae27e98aSSangtae Ha #define HYSTART_ACK_TRAIN	0x1
38ae27e98aSSangtae Ha #define HYSTART_DELAY		0x2
39ae27e98aSSangtae Ha 
40ae27e98aSSangtae Ha /* Number of delay samples for detecting the increase of delay */
41ae27e98aSSangtae Ha #define HYSTART_MIN_SAMPLES	8
422b4636a5SSangtae Ha #define HYSTART_DELAY_MIN	(4U<<3)
43ae27e98aSSangtae Ha #define HYSTART_DELAY_MAX	(16U<<3)
44ae27e98aSSangtae Ha #define HYSTART_DELAY_THRESH(x)	clamp(x, HYSTART_DELAY_MIN, HYSTART_DELAY_MAX)
45ae27e98aSSangtae Ha 
4659758f44SStephen Hemminger static int fast_convergence __read_mostly = 1;
476b3d6263SSangtae Ha static int beta __read_mostly = 717;	/* = 717/1024 (BICTCP_BETA_SCALE) */
4866e1e3b2SDavid S. Miller static int initial_ssthresh __read_mostly;
4959758f44SStephen Hemminger static int bic_scale __read_mostly = 41;
5059758f44SStephen Hemminger static int tcp_friendliness __read_mostly = 1;
51df3271f3SStephen Hemminger 
52ae27e98aSSangtae Ha static int hystart __read_mostly = 1;
53ae27e98aSSangtae Ha static int hystart_detect __read_mostly = HYSTART_ACK_TRAIN | HYSTART_DELAY;
54ae27e98aSSangtae Ha static int hystart_low_window __read_mostly = 16;
55aac46324Sstephen hemminger static int hystart_ack_delta __read_mostly = 2;
56ae27e98aSSangtae Ha 
5759758f44SStephen Hemminger static u32 cube_rtt_scale __read_mostly;
5859758f44SStephen Hemminger static u32 beta_scale __read_mostly;
5959758f44SStephen Hemminger static u64 cube_factor __read_mostly;
6089b3d9aaSStephen Hemminger 
6189b3d9aaSStephen Hemminger /* Note parameters that are used for precomputing scale factors are read-only */
62df3271f3SStephen Hemminger module_param(fast_convergence, int, 0644);
63df3271f3SStephen Hemminger MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
646b3d6263SSangtae Ha module_param(beta, int, 0644);
65df3271f3SStephen Hemminger MODULE_PARM_DESC(beta, "beta for multiplicative increase");
66df3271f3SStephen Hemminger module_param(initial_ssthresh, int, 0644);
67df3271f3SStephen Hemminger MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
6889b3d9aaSStephen Hemminger module_param(bic_scale, int, 0444);
69df3271f3SStephen Hemminger MODULE_PARM_DESC(bic_scale, "scale (scaled by 1024) value for bic function (bic_scale/1024)");
70df3271f3SStephen Hemminger module_param(tcp_friendliness, int, 0644);
71df3271f3SStephen Hemminger MODULE_PARM_DESC(tcp_friendliness, "turn on/off tcp friendliness");
72ae27e98aSSangtae Ha module_param(hystart, int, 0644);
73ae27e98aSSangtae Ha MODULE_PARM_DESC(hystart, "turn on/off hybrid slow start algorithm");
74ae27e98aSSangtae Ha module_param(hystart_detect, int, 0644);
75ae27e98aSSangtae Ha MODULE_PARM_DESC(hystart_detect, "hyrbrid slow start detection mechanisms"
76ae27e98aSSangtae Ha 		 " 1: packet-train 2: delay 3: both packet-train and delay");
77ae27e98aSSangtae Ha module_param(hystart_low_window, int, 0644);
78ae27e98aSSangtae Ha MODULE_PARM_DESC(hystart_low_window, "lower bound cwnd for hybrid slow start");
79aac46324Sstephen hemminger module_param(hystart_ack_delta, int, 0644);
80aac46324Sstephen hemminger MODULE_PARM_DESC(hystart_ack_delta, "spacing between ack's indicating train (msecs)");
81df3271f3SStephen Hemminger 
82df3271f3SStephen Hemminger /* BIC TCP Parameters */
83df3271f3SStephen Hemminger struct bictcp {
84df3271f3SStephen Hemminger 	u32	cnt;		/* increase cwnd by 1 after ACKs */
85df3271f3SStephen Hemminger 	u32	last_max_cwnd;	/* last maximum snd_cwnd */
86df3271f3SStephen Hemminger 	u32	loss_cwnd;	/* congestion window at last loss */
87df3271f3SStephen Hemminger 	u32	last_cwnd;	/* the last snd_cwnd */
88df3271f3SStephen Hemminger 	u32	last_time;	/* time when updated last_cwnd */
89df3271f3SStephen Hemminger 	u32	bic_origin_point;/* origin point of bic function */
90688d1945Sstephen hemminger 	u32	bic_K;		/* time to origin point
91688d1945Sstephen hemminger 				   from the beginning of the current epoch */
9217a6e9f1Sstephen hemminger 	u32	delay_min;	/* min delay (msec << 3) */
93df3271f3SStephen Hemminger 	u32	epoch_start;	/* beginning of an epoch */
94df3271f3SStephen Hemminger 	u32	ack_cnt;	/* number of acks */
95df3271f3SStephen Hemminger 	u32	tcp_cwnd;	/* estimated tcp cwnd */
969cd981dcSNeal Cardwell 	u16	unused;
97ae27e98aSSangtae Ha 	u8	sample_cnt;	/* number of samples to decide curr_rtt */
98ae27e98aSSangtae Ha 	u8	found;		/* the exit point is found? */
99ae27e98aSSangtae Ha 	u32	round_start;	/* beginning of each round */
100ae27e98aSSangtae Ha 	u32	end_seq;	/* end_seq of the round */
10117a6e9f1Sstephen hemminger 	u32	last_ack;	/* last time when the ACK spacing is close */
102ae27e98aSSangtae Ha 	u32	curr_rtt;	/* the minimum rtt of current round */
103df3271f3SStephen Hemminger };
104df3271f3SStephen Hemminger 
105df3271f3SStephen Hemminger static inline void bictcp_reset(struct bictcp *ca)
106df3271f3SStephen Hemminger {
107df3271f3SStephen Hemminger 	ca->cnt = 0;
108df3271f3SStephen Hemminger 	ca->last_max_cwnd = 0;
109df3271f3SStephen Hemminger 	ca->last_cwnd = 0;
110df3271f3SStephen Hemminger 	ca->last_time = 0;
111df3271f3SStephen Hemminger 	ca->bic_origin_point = 0;
112df3271f3SStephen Hemminger 	ca->bic_K = 0;
113df3271f3SStephen Hemminger 	ca->delay_min = 0;
114df3271f3SStephen Hemminger 	ca->epoch_start = 0;
115df3271f3SStephen Hemminger 	ca->ack_cnt = 0;
116df3271f3SStephen Hemminger 	ca->tcp_cwnd = 0;
117ae27e98aSSangtae Ha 	ca->found = 0;
118ae27e98aSSangtae Ha }
119ae27e98aSSangtae Ha 
12017a6e9f1Sstephen hemminger static inline u32 bictcp_clock(void)
12117a6e9f1Sstephen hemminger {
12217a6e9f1Sstephen hemminger #if HZ < 1000
12317a6e9f1Sstephen hemminger 	return ktime_to_ms(ktime_get_real());
12417a6e9f1Sstephen hemminger #else
12517a6e9f1Sstephen hemminger 	return jiffies_to_msecs(jiffies);
12617a6e9f1Sstephen hemminger #endif
12717a6e9f1Sstephen hemminger }
12817a6e9f1Sstephen hemminger 
129ae27e98aSSangtae Ha static inline void bictcp_hystart_reset(struct sock *sk)
130ae27e98aSSangtae Ha {
131ae27e98aSSangtae Ha 	struct tcp_sock *tp = tcp_sk(sk);
132ae27e98aSSangtae Ha 	struct bictcp *ca = inet_csk_ca(sk);
133ae27e98aSSangtae Ha 
13417a6e9f1Sstephen hemminger 	ca->round_start = ca->last_ack = bictcp_clock();
135ae27e98aSSangtae Ha 	ca->end_seq = tp->snd_nxt;
136ae27e98aSSangtae Ha 	ca->curr_rtt = 0;
137ae27e98aSSangtae Ha 	ca->sample_cnt = 0;
138df3271f3SStephen Hemminger }
139df3271f3SStephen Hemminger 
140df3271f3SStephen Hemminger static void bictcp_init(struct sock *sk)
141df3271f3SStephen Hemminger {
1425a45f008SNeal Cardwell 	struct bictcp *ca = inet_csk_ca(sk);
1435a45f008SNeal Cardwell 
1445a45f008SNeal Cardwell 	bictcp_reset(ca);
1455a45f008SNeal Cardwell 	ca->loss_cwnd = 0;
146ae27e98aSSangtae Ha 
147ae27e98aSSangtae Ha 	if (hystart)
148ae27e98aSSangtae Ha 		bictcp_hystart_reset(sk);
149ae27e98aSSangtae Ha 
150ae27e98aSSangtae Ha 	if (!hystart && initial_ssthresh)
151df3271f3SStephen Hemminger 		tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
152df3271f3SStephen Hemminger }
153df3271f3SStephen Hemminger 
154*30927520SEric Dumazet static void bictcp_cwnd_event(struct sock *sk, enum tcp_ca_event event)
155*30927520SEric Dumazet {
156*30927520SEric Dumazet 	if (event == CA_EVENT_TX_START) {
157*30927520SEric Dumazet 		s32 delta = tcp_time_stamp - tcp_sk(sk)->lsndtime;
158*30927520SEric Dumazet 		struct bictcp *ca = inet_csk_ca(sk);
159*30927520SEric Dumazet 
160*30927520SEric Dumazet 		/* We were application limited (idle) for a while.
161*30927520SEric Dumazet 		 * Shift epoch_start to keep cwnd growth to cubic curve.
162*30927520SEric Dumazet 		 */
163*30927520SEric Dumazet 		if (ca->epoch_start && delta > 0)
164*30927520SEric Dumazet 			ca->epoch_start += delta;
165*30927520SEric Dumazet 		return;
166*30927520SEric Dumazet 	}
167*30927520SEric Dumazet }
168*30927520SEric Dumazet 
1697e58886bSStephen Hemminger /* calculate the cubic root of x using a table lookup followed by one
1707e58886bSStephen Hemminger  * Newton-Raphson iteration.
1717e58886bSStephen Hemminger  * Avg err ~= 0.195%
172df3271f3SStephen Hemminger  */
1739eb2d627SStephen Hemminger static u32 cubic_root(u64 a)
174df3271f3SStephen Hemminger {
1757e58886bSStephen Hemminger 	u32 x, b, shift;
1767e58886bSStephen Hemminger 	/*
1777e58886bSStephen Hemminger 	 * cbrt(x) MSB values for x MSB values in [0..63].
1787e58886bSStephen Hemminger 	 * Precomputed then refined by hand - Willy Tarreau
1797e58886bSStephen Hemminger 	 *
1807e58886bSStephen Hemminger 	 * For x in [0..63],
1817e58886bSStephen Hemminger 	 *   v = cbrt(x << 18) - 1
1827e58886bSStephen Hemminger 	 *   cbrt(x) = (v[x] + 10) >> 6
1839eb2d627SStephen Hemminger 	 */
1847e58886bSStephen Hemminger 	static const u8 v[] = {
1857e58886bSStephen Hemminger 		/* 0x00 */    0,   54,   54,   54,  118,  118,  118,  118,
1867e58886bSStephen Hemminger 		/* 0x08 */  123,  129,  134,  138,  143,  147,  151,  156,
1877e58886bSStephen Hemminger 		/* 0x10 */  157,  161,  164,  168,  170,  173,  176,  179,
1887e58886bSStephen Hemminger 		/* 0x18 */  181,  185,  187,  190,  192,  194,  197,  199,
1897e58886bSStephen Hemminger 		/* 0x20 */  200,  202,  204,  206,  209,  211,  213,  215,
1907e58886bSStephen Hemminger 		/* 0x28 */  217,  219,  221,  222,  224,  225,  227,  229,
1917e58886bSStephen Hemminger 		/* 0x30 */  231,  232,  234,  236,  237,  239,  240,  242,
1927e58886bSStephen Hemminger 		/* 0x38 */  244,  245,  246,  248,  250,  251,  252,  254,
1937e58886bSStephen Hemminger 	};
194df3271f3SStephen Hemminger 
1957e58886bSStephen Hemminger 	b = fls64(a);
1967e58886bSStephen Hemminger 	if (b < 7) {
1977e58886bSStephen Hemminger 		/* a in [0..63] */
1987e58886bSStephen Hemminger 		return ((u32)v[(u32)a] + 35) >> 6;
1997e58886bSStephen Hemminger 	}
200df3271f3SStephen Hemminger 
2017e58886bSStephen Hemminger 	b = ((b * 84) >> 8) - 1;
2027e58886bSStephen Hemminger 	shift = (a >> (b * 3));
2037e58886bSStephen Hemminger 
2047e58886bSStephen Hemminger 	x = ((u32)(((u32)v[shift] + 10) << b)) >> 6;
2057e58886bSStephen Hemminger 
2067e58886bSStephen Hemminger 	/*
2077e58886bSStephen Hemminger 	 * Newton-Raphson iteration
2087e58886bSStephen Hemminger 	 *                         2
2097e58886bSStephen Hemminger 	 * x    = ( 2 * x  +  a / x  ) / 3
2107e58886bSStephen Hemminger 	 *  k+1          k         k
2117e58886bSStephen Hemminger 	 */
2126f6d6a1aSRoman Zippel 	x = (2 * x + (u32)div64_u64(a, (u64)x * (u64)(x - 1)));
2137e58886bSStephen Hemminger 	x = ((x * 341) >> 10);
2149eb2d627SStephen Hemminger 	return x;
215df3271f3SStephen Hemminger }
216df3271f3SStephen Hemminger 
217df3271f3SStephen Hemminger /*
218df3271f3SStephen Hemminger  * Compute congestion window to use.
219df3271f3SStephen Hemminger  */
2209cd981dcSNeal Cardwell static inline void bictcp_update(struct bictcp *ca, u32 cwnd, u32 acked)
221df3271f3SStephen Hemminger {
2222ed0edf9SEric Dumazet 	u32 delta, bic_target, max_cnt;
2232ed0edf9SEric Dumazet 	u64 offs, t;
224df3271f3SStephen Hemminger 
2259cd981dcSNeal Cardwell 	ca->ack_cnt += acked;	/* count the number of ACKed packets */
226df3271f3SStephen Hemminger 
227df3271f3SStephen Hemminger 	if (ca->last_cwnd == cwnd &&
228df3271f3SStephen Hemminger 	    (s32)(tcp_time_stamp - ca->last_time) <= HZ / 32)
229df3271f3SStephen Hemminger 		return;
230df3271f3SStephen Hemminger 
231d6b1a8a9SNeal Cardwell 	/* The CUBIC function can update ca->cnt at most once per jiffy.
232d6b1a8a9SNeal Cardwell 	 * On all cwnd reduction events, ca->epoch_start is set to 0,
233d6b1a8a9SNeal Cardwell 	 * which will force a recalculation of ca->cnt.
234d6b1a8a9SNeal Cardwell 	 */
235d6b1a8a9SNeal Cardwell 	if (ca->epoch_start && tcp_time_stamp == ca->last_time)
236d6b1a8a9SNeal Cardwell 		goto tcp_friendliness;
237d6b1a8a9SNeal Cardwell 
238df3271f3SStephen Hemminger 	ca->last_cwnd = cwnd;
239df3271f3SStephen Hemminger 	ca->last_time = tcp_time_stamp;
240df3271f3SStephen Hemminger 
241df3271f3SStephen Hemminger 	if (ca->epoch_start == 0) {
242688d1945Sstephen hemminger 		ca->epoch_start = tcp_time_stamp;	/* record beginning */
2439cd981dcSNeal Cardwell 		ca->ack_cnt = acked;			/* start counting */
244df3271f3SStephen Hemminger 		ca->tcp_cwnd = cwnd;			/* syn with cubic */
245df3271f3SStephen Hemminger 
246df3271f3SStephen Hemminger 		if (ca->last_max_cwnd <= cwnd) {
247df3271f3SStephen Hemminger 			ca->bic_K = 0;
248df3271f3SStephen Hemminger 			ca->bic_origin_point = cwnd;
249df3271f3SStephen Hemminger 		} else {
25089b3d9aaSStephen Hemminger 			/* Compute new K based on
25189b3d9aaSStephen Hemminger 			 * (wmax-cwnd) * (srtt>>3 / HZ) / c * 2^(3*bictcp_HZ)
25289b3d9aaSStephen Hemminger 			 */
25389b3d9aaSStephen Hemminger 			ca->bic_K = cubic_root(cube_factor
25489b3d9aaSStephen Hemminger 					       * (ca->last_max_cwnd - cwnd));
255df3271f3SStephen Hemminger 			ca->bic_origin_point = ca->last_max_cwnd;
256df3271f3SStephen Hemminger 		}
257df3271f3SStephen Hemminger 	}
258df3271f3SStephen Hemminger 
259df3271f3SStephen Hemminger 	/* cubic function - calc*/
260df3271f3SStephen Hemminger 	/* calculate c * time^3 / rtt,
261df3271f3SStephen Hemminger 	 *  while considering overflow in calculation of time^3
26289b3d9aaSStephen Hemminger 	 * (so time^3 is done by using 64 bit)
263df3271f3SStephen Hemminger 	 * and without the support of division of 64bit numbers
26489b3d9aaSStephen Hemminger 	 * (so all divisions are done by using 32 bit)
265df3271f3SStephen Hemminger 	 *  also NOTE the unit of those veriables
266df3271f3SStephen Hemminger 	 *	  time  = (t - K) / 2^bictcp_HZ
267df3271f3SStephen Hemminger 	 *	  c = bic_scale >> 10
268df3271f3SStephen Hemminger 	 * rtt  = (srtt >> 3) / HZ
269df3271f3SStephen Hemminger 	 * !!! The following code does not have overflow problems,
270df3271f3SStephen Hemminger 	 * if the cwnd < 1 million packets !!!
271df3271f3SStephen Hemminger 	 */
272df3271f3SStephen Hemminger 
2732ed0edf9SEric Dumazet 	t = (s32)(tcp_time_stamp - ca->epoch_start);
2742ed0edf9SEric Dumazet 	t += msecs_to_jiffies(ca->delay_min >> 3);
275df3271f3SStephen Hemminger 	/* change the unit from HZ to bictcp_HZ */
2762ed0edf9SEric Dumazet 	t <<= BICTCP_HZ;
2772ed0edf9SEric Dumazet 	do_div(t, HZ);
278df3271f3SStephen Hemminger 
279df3271f3SStephen Hemminger 	if (t < ca->bic_K)		/* t - K */
28089b3d9aaSStephen Hemminger 		offs = ca->bic_K - t;
281df3271f3SStephen Hemminger 	else
28289b3d9aaSStephen Hemminger 		offs = t - ca->bic_K;
283df3271f3SStephen Hemminger 
28489b3d9aaSStephen Hemminger 	/* c/rtt * (t-K)^3 */
28589b3d9aaSStephen Hemminger 	delta = (cube_rtt_scale * offs * offs * offs) >> (10+3*BICTCP_HZ);
286df3271f3SStephen Hemminger 	if (t < ca->bic_K)                            /* below origin*/
28789b3d9aaSStephen Hemminger 		bic_target = ca->bic_origin_point - delta;
288df3271f3SStephen Hemminger 	else                                          /* above origin*/
28989b3d9aaSStephen Hemminger 		bic_target = ca->bic_origin_point + delta;
290df3271f3SStephen Hemminger 
291df3271f3SStephen Hemminger 	/* cubic function - calc bictcp_cnt*/
292df3271f3SStephen Hemminger 	if (bic_target > cwnd) {
293df3271f3SStephen Hemminger 		ca->cnt = cwnd / (bic_target - cwnd);
294df3271f3SStephen Hemminger 	} else {
295df3271f3SStephen Hemminger 		ca->cnt = 100 * cwnd;              /* very small increment*/
296df3271f3SStephen Hemminger 	}
297df3271f3SStephen Hemminger 
298b5ccd073SSangtae Ha 	/*
299b5ccd073SSangtae Ha 	 * The initial growth of cubic function may be too conservative
300b5ccd073SSangtae Ha 	 * when the available bandwidth is still unknown.
301b5ccd073SSangtae Ha 	 */
3025a45f008SNeal Cardwell 	if (ca->last_max_cwnd == 0 && ca->cnt > 20)
303b5ccd073SSangtae Ha 		ca->cnt = 20;	/* increase cwnd 5% per RTT */
304b5ccd073SSangtae Ha 
305d6b1a8a9SNeal Cardwell tcp_friendliness:
306df3271f3SStephen Hemminger 	/* TCP Friendly */
307df3271f3SStephen Hemminger 	if (tcp_friendliness) {
30889b3d9aaSStephen Hemminger 		u32 scale = beta_scale;
309688d1945Sstephen hemminger 
31089b3d9aaSStephen Hemminger 		delta = (cwnd * scale) >> 3;
31189b3d9aaSStephen Hemminger 		while (ca->ack_cnt > delta) {		/* update tcp cwnd */
31289b3d9aaSStephen Hemminger 			ca->ack_cnt -= delta;
313df3271f3SStephen Hemminger 			ca->tcp_cwnd++;
314df3271f3SStephen Hemminger 		}
315df3271f3SStephen Hemminger 
316df3271f3SStephen Hemminger 		if (ca->tcp_cwnd > cwnd) {	/* if bic is slower than tcp */
31789b3d9aaSStephen Hemminger 			delta = ca->tcp_cwnd - cwnd;
31889b3d9aaSStephen Hemminger 			max_cnt = cwnd / delta;
319df3271f3SStephen Hemminger 			if (ca->cnt > max_cnt)
320df3271f3SStephen Hemminger 				ca->cnt = max_cnt;
321df3271f3SStephen Hemminger 		}
322df3271f3SStephen Hemminger 	}
323df3271f3SStephen Hemminger 
324d578e18cSNeal Cardwell 	/* The maximum rate of cwnd increase CUBIC allows is 1 packet per
325d578e18cSNeal Cardwell 	 * 2 packets ACKed, meaning cwnd grows at 1.5x per RTT.
326d578e18cSNeal Cardwell 	 */
327d578e18cSNeal Cardwell 	ca->cnt = max(ca->cnt, 2U);
328df3271f3SStephen Hemminger }
329df3271f3SStephen Hemminger 
33024901551SEric Dumazet static void bictcp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
331df3271f3SStephen Hemminger {
332df3271f3SStephen Hemminger 	struct tcp_sock *tp = tcp_sk(sk);
333df3271f3SStephen Hemminger 	struct bictcp *ca = inet_csk_ca(sk);
334df3271f3SStephen Hemminger 
33524901551SEric Dumazet 	if (!tcp_is_cwnd_limited(sk))
336df3271f3SStephen Hemminger 		return;
337df3271f3SStephen Hemminger 
338071d5080SYuchung Cheng 	if (tcp_in_slow_start(tp)) {
339ae27e98aSSangtae Ha 		if (hystart && after(ack, ca->end_seq))
340ae27e98aSSangtae Ha 			bictcp_hystart_reset(sk);
3419cd981dcSNeal Cardwell 		acked = tcp_slow_start(tp, acked);
3429cd981dcSNeal Cardwell 		if (!acked)
3439cd981dcSNeal Cardwell 			return;
344df3271f3SStephen Hemminger 	}
3459cd981dcSNeal Cardwell 	bictcp_update(ca, tp->snd_cwnd, acked);
3469cd981dcSNeal Cardwell 	tcp_cong_avoid_ai(tp, ca->cnt, acked);
347df3271f3SStephen Hemminger }
348df3271f3SStephen Hemminger 
349df3271f3SStephen Hemminger static u32 bictcp_recalc_ssthresh(struct sock *sk)
350df3271f3SStephen Hemminger {
351df3271f3SStephen Hemminger 	const struct tcp_sock *tp = tcp_sk(sk);
352df3271f3SStephen Hemminger 	struct bictcp *ca = inet_csk_ca(sk);
353df3271f3SStephen Hemminger 
354df3271f3SStephen Hemminger 	ca->epoch_start = 0;	/* end of epoch */
355df3271f3SStephen Hemminger 
356df3271f3SStephen Hemminger 	/* Wmax and fast convergence */
357df3271f3SStephen Hemminger 	if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence)
358df3271f3SStephen Hemminger 		ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta))
359df3271f3SStephen Hemminger 			/ (2 * BICTCP_BETA_SCALE);
360df3271f3SStephen Hemminger 	else
361df3271f3SStephen Hemminger 		ca->last_max_cwnd = tp->snd_cwnd;
362df3271f3SStephen Hemminger 
363df3271f3SStephen Hemminger 	ca->loss_cwnd = tp->snd_cwnd;
364df3271f3SStephen Hemminger 
365df3271f3SStephen Hemminger 	return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
366df3271f3SStephen Hemminger }
367df3271f3SStephen Hemminger 
368df3271f3SStephen Hemminger static u32 bictcp_undo_cwnd(struct sock *sk)
369df3271f3SStephen Hemminger {
370df3271f3SStephen Hemminger 	struct bictcp *ca = inet_csk_ca(sk);
371df3271f3SStephen Hemminger 
3725a45f008SNeal Cardwell 	return max(tcp_sk(sk)->snd_cwnd, ca->loss_cwnd);
373df3271f3SStephen Hemminger }
374df3271f3SStephen Hemminger 
375df3271f3SStephen Hemminger static void bictcp_state(struct sock *sk, u8 new_state)
376df3271f3SStephen Hemminger {
377ae27e98aSSangtae Ha 	if (new_state == TCP_CA_Loss) {
378df3271f3SStephen Hemminger 		bictcp_reset(inet_csk_ca(sk));
379ae27e98aSSangtae Ha 		bictcp_hystart_reset(sk);
380ae27e98aSSangtae Ha 	}
381ae27e98aSSangtae Ha }
382ae27e98aSSangtae Ha 
383ae27e98aSSangtae Ha static void hystart_update(struct sock *sk, u32 delay)
384ae27e98aSSangtae Ha {
385ae27e98aSSangtae Ha 	struct tcp_sock *tp = tcp_sk(sk);
386ae27e98aSSangtae Ha 	struct bictcp *ca = inet_csk_ca(sk);
387ae27e98aSSangtae Ha 
3886e3a8a93SEric Dumazet 	if (ca->found & hystart_detect)
3896e3a8a93SEric Dumazet 		return;
3906e3a8a93SEric Dumazet 
3916e3a8a93SEric Dumazet 	if (hystart_detect & HYSTART_ACK_TRAIN) {
39217a6e9f1Sstephen hemminger 		u32 now = bictcp_clock();
393ae27e98aSSangtae Ha 
394ae27e98aSSangtae Ha 		/* first detection parameter - ack-train detection */
39517a6e9f1Sstephen hemminger 		if ((s32)(now - ca->last_ack) <= hystart_ack_delta) {
39617a6e9f1Sstephen hemminger 			ca->last_ack = now;
3976e3a8a93SEric Dumazet 			if ((s32)(now - ca->round_start) > ca->delay_min >> 4) {
398ae27e98aSSangtae Ha 				ca->found |= HYSTART_ACK_TRAIN;
3996e3a8a93SEric Dumazet 				NET_INC_STATS_BH(sock_net(sk),
4006e3a8a93SEric Dumazet 						 LINUX_MIB_TCPHYSTARTTRAINDETECT);
4016e3a8a93SEric Dumazet 				NET_ADD_STATS_BH(sock_net(sk),
4026e3a8a93SEric Dumazet 						 LINUX_MIB_TCPHYSTARTTRAINCWND,
4036e3a8a93SEric Dumazet 						 tp->snd_cwnd);
4046e3a8a93SEric Dumazet 				tp->snd_ssthresh = tp->snd_cwnd;
4056e3a8a93SEric Dumazet 			}
4066e3a8a93SEric Dumazet 		}
407ae27e98aSSangtae Ha 	}
408ae27e98aSSangtae Ha 
4096e3a8a93SEric Dumazet 	if (hystart_detect & HYSTART_DELAY) {
410ae27e98aSSangtae Ha 		/* obtain the minimum delay of more than sampling packets */
411ae27e98aSSangtae Ha 		if (ca->sample_cnt < HYSTART_MIN_SAMPLES) {
412ae27e98aSSangtae Ha 			if (ca->curr_rtt == 0 || ca->curr_rtt > delay)
413ae27e98aSSangtae Ha 				ca->curr_rtt = delay;
414ae27e98aSSangtae Ha 
415ae27e98aSSangtae Ha 			ca->sample_cnt++;
416ae27e98aSSangtae Ha 		} else {
417ae27e98aSSangtae Ha 			if (ca->curr_rtt > ca->delay_min +
41842eef7a0SEric Dumazet 			    HYSTART_DELAY_THRESH(ca->delay_min >> 3)) {
419ae27e98aSSangtae Ha 				ca->found |= HYSTART_DELAY;
4206e3a8a93SEric Dumazet 				NET_INC_STATS_BH(sock_net(sk),
4216e3a8a93SEric Dumazet 						 LINUX_MIB_TCPHYSTARTDELAYDETECT);
4226e3a8a93SEric Dumazet 				NET_ADD_STATS_BH(sock_net(sk),
4236e3a8a93SEric Dumazet 						 LINUX_MIB_TCPHYSTARTDELAYCWND,
4246e3a8a93SEric Dumazet 						 tp->snd_cwnd);
425ae27e98aSSangtae Ha 				tp->snd_ssthresh = tp->snd_cwnd;
426ae27e98aSSangtae Ha 			}
427df3271f3SStephen Hemminger 		}
4286e3a8a93SEric Dumazet 	}
4296e3a8a93SEric Dumazet }
430df3271f3SStephen Hemminger 
431df3271f3SStephen Hemminger /* Track delayed acknowledgment ratio using sliding window
432df3271f3SStephen Hemminger  * ratio = (15*ratio + sample) / 16
433df3271f3SStephen Hemminger  */
43430cfd0baSStephen Hemminger static void bictcp_acked(struct sock *sk, u32 cnt, s32 rtt_us)
435df3271f3SStephen Hemminger {
436ae27e98aSSangtae Ha 	const struct tcp_sock *tp = tcp_sk(sk);
437e7d0c885SStephen Hemminger 	struct bictcp *ca = inet_csk_ca(sk);
438e7d0c885SStephen Hemminger 	u32 delay;
439df3271f3SStephen Hemminger 
440e7d0c885SStephen Hemminger 	/* Some calls are for duplicates without timetamps */
441e7d0c885SStephen Hemminger 	if (rtt_us < 0)
442e7d0c885SStephen Hemminger 		return;
443e7d0c885SStephen Hemminger 
444e7d0c885SStephen Hemminger 	/* Discard delay samples right after fast recovery */
445cd6b423aSEric Dumazet 	if (ca->epoch_start && (s32)(tcp_time_stamp - ca->epoch_start) < HZ)
446e7d0c885SStephen Hemminger 		return;
447e7d0c885SStephen Hemminger 
44817a6e9f1Sstephen hemminger 	delay = (rtt_us << 3) / USEC_PER_MSEC;
449e7d0c885SStephen Hemminger 	if (delay == 0)
450e7d0c885SStephen Hemminger 		delay = 1;
451e7d0c885SStephen Hemminger 
452e7d0c885SStephen Hemminger 	/* first time call or link delay decreases */
453e7d0c885SStephen Hemminger 	if (ca->delay_min == 0 || ca->delay_min > delay)
454e7d0c885SStephen Hemminger 		ca->delay_min = delay;
455ae27e98aSSangtae Ha 
456ae27e98aSSangtae Ha 	/* hystart triggers when cwnd is larger than some threshold */
457071d5080SYuchung Cheng 	if (hystart && tcp_in_slow_start(tp) &&
458ae27e98aSSangtae Ha 	    tp->snd_cwnd >= hystart_low_window)
459ae27e98aSSangtae Ha 		hystart_update(sk, delay);
460e7d0c885SStephen Hemminger }
461df3271f3SStephen Hemminger 
462a252bebeSStephen Hemminger static struct tcp_congestion_ops cubictcp __read_mostly = {
463df3271f3SStephen Hemminger 	.init		= bictcp_init,
464df3271f3SStephen Hemminger 	.ssthresh	= bictcp_recalc_ssthresh,
465df3271f3SStephen Hemminger 	.cong_avoid	= bictcp_cong_avoid,
466df3271f3SStephen Hemminger 	.set_state	= bictcp_state,
467df3271f3SStephen Hemminger 	.undo_cwnd	= bictcp_undo_cwnd,
468*30927520SEric Dumazet 	.cwnd_event	= bictcp_cwnd_event,
469df3271f3SStephen Hemminger 	.pkts_acked     = bictcp_acked,
470df3271f3SStephen Hemminger 	.owner		= THIS_MODULE,
471df3271f3SStephen Hemminger 	.name		= "cubic",
472df3271f3SStephen Hemminger };
473df3271f3SStephen Hemminger 
474df3271f3SStephen Hemminger static int __init cubictcp_register(void)
475df3271f3SStephen Hemminger {
47674975d40SAlexey Dobriyan 	BUILD_BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
47789b3d9aaSStephen Hemminger 
47889b3d9aaSStephen Hemminger 	/* Precompute a bunch of the scaling factors that are used per-packet
47989b3d9aaSStephen Hemminger 	 * based on SRTT of 100ms
48089b3d9aaSStephen Hemminger 	 */
48189b3d9aaSStephen Hemminger 
482688d1945Sstephen hemminger 	beta_scale = 8*(BICTCP_BETA_SCALE+beta) / 3
483688d1945Sstephen hemminger 		/ (BICTCP_BETA_SCALE - beta);
48489b3d9aaSStephen Hemminger 
48522119240SStephen Hemminger 	cube_rtt_scale = (bic_scale * 10);	/* 1024*c/rtt */
48689b3d9aaSStephen Hemminger 
48789b3d9aaSStephen Hemminger 	/* calculate the "K" for (wmax-cwnd) = c/rtt * K^3
48889b3d9aaSStephen Hemminger 	 *  so K = cubic_root( (wmax-cwnd)*rtt/c )
48989b3d9aaSStephen Hemminger 	 * the unit of K is bictcp_HZ=2^10, not HZ
49089b3d9aaSStephen Hemminger 	 *
49189b3d9aaSStephen Hemminger 	 *  c = bic_scale >> 10
49289b3d9aaSStephen Hemminger 	 *  rtt = 100ms
49389b3d9aaSStephen Hemminger 	 *
49489b3d9aaSStephen Hemminger 	 * the following code has been designed and tested for
49589b3d9aaSStephen Hemminger 	 * cwnd < 1 million packets
49689b3d9aaSStephen Hemminger 	 * RTT < 100 seconds
49789b3d9aaSStephen Hemminger 	 * HZ < 1,000,00  (corresponding to 10 nano-second)
49889b3d9aaSStephen Hemminger 	 */
49989b3d9aaSStephen Hemminger 
50089b3d9aaSStephen Hemminger 	/* 1/c * 2^2*bictcp_HZ * srtt */
50189b3d9aaSStephen Hemminger 	cube_factor = 1ull << (10+3*BICTCP_HZ); /* 2^40 */
50289b3d9aaSStephen Hemminger 
50389b3d9aaSStephen Hemminger 	/* divide by bic_scale and by constant Srtt (100ms) */
50489b3d9aaSStephen Hemminger 	do_div(cube_factor, bic_scale * 10);
50589b3d9aaSStephen Hemminger 
506df3271f3SStephen Hemminger 	return tcp_register_congestion_control(&cubictcp);
507df3271f3SStephen Hemminger }
508df3271f3SStephen Hemminger 
509df3271f3SStephen Hemminger static void __exit cubictcp_unregister(void)
510df3271f3SStephen Hemminger {
511df3271f3SStephen Hemminger 	tcp_unregister_congestion_control(&cubictcp);
512df3271f3SStephen Hemminger }
513df3271f3SStephen Hemminger 
514df3271f3SStephen Hemminger module_init(cubictcp_register);
515df3271f3SStephen Hemminger module_exit(cubictcp_unregister);
516df3271f3SStephen Hemminger 
517df3271f3SStephen Hemminger MODULE_AUTHOR("Sangtae Ha, Stephen Hemminger");
518df3271f3SStephen Hemminger MODULE_LICENSE("GPL");
519df3271f3SStephen Hemminger MODULE_DESCRIPTION("CUBIC TCP");
520ae27e98aSSangtae Ha MODULE_VERSION("2.3");
521