af_unix: clean up net/unix/af_unix.c garbage.c sysctl_net_unix.c
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv4 / tcp_cubic.c
CommitLineData
df3271f3 1/*
6b3d6263
SH
2 * TCP CUBIC: Binary Increase Congestion control for TCP v2.2
3 * Home page:
4 * http://netsrv.csc.ncsu.edu/twiki/bin/view/Main/BIC
df3271f3
SH
5 * This is from the implementation of CUBIC TCP in
6 * Injong Rhee, Lisong Xu.
7 * "CUBIC: A New TCP-Friendly High-Speed TCP Variant
8 * in PFLDnet 2005
9 * Available from:
6b3d6263 10 * http://netsrv.csc.ncsu.edu/export/cubic-paper.pdf
df3271f3
SH
11 *
12 * Unless CUBIC is enabled and congestion window is large
13 * this behaves the same as the original Reno.
14 */
15
df3271f3
SH
16#include <linux/mm.h>
17#include <linux/module.h>
6f6d6a1a 18#include <linux/math64.h>
df3271f3 19#include <net/tcp.h>
df3271f3
SH
20
21#define BICTCP_BETA_SCALE 1024 /* Scale factor beta calculation
22 * max_cwnd = snd_cwnd * beta
23 */
df3271f3
SH
24#define BICTCP_HZ 10 /* BIC HZ 2^10 = 1024 */
25
59758f44 26static int fast_convergence __read_mostly = 1;
6b3d6263 27static int beta __read_mostly = 717; /* = 717/1024 (BICTCP_BETA_SCALE) */
66e1e3b2 28static int initial_ssthresh __read_mostly;
59758f44
SH
29static int bic_scale __read_mostly = 41;
30static int tcp_friendliness __read_mostly = 1;
df3271f3 31
59758f44
SH
32static u32 cube_rtt_scale __read_mostly;
33static u32 beta_scale __read_mostly;
34static u64 cube_factor __read_mostly;
89b3d9aa
SH
35
36/* Note parameters that are used for precomputing scale factors are read-only */
df3271f3
SH
37module_param(fast_convergence, int, 0644);
38MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
6b3d6263 39module_param(beta, int, 0644);
df3271f3
SH
40MODULE_PARM_DESC(beta, "beta for multiplicative increase");
41module_param(initial_ssthresh, int, 0644);
42MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
89b3d9aa 43module_param(bic_scale, int, 0444);
df3271f3
SH
44MODULE_PARM_DESC(bic_scale, "scale (scaled by 1024) value for bic function (bic_scale/1024)");
45module_param(tcp_friendliness, int, 0644);
46MODULE_PARM_DESC(tcp_friendliness, "turn on/off tcp friendliness");
47
df3271f3
SH
48/* BIC TCP Parameters */
49struct bictcp {
50 u32 cnt; /* increase cwnd by 1 after ACKs */
51 u32 last_max_cwnd; /* last maximum snd_cwnd */
52 u32 loss_cwnd; /* congestion window at last loss */
53 u32 last_cwnd; /* the last snd_cwnd */
54 u32 last_time; /* time when updated last_cwnd */
55 u32 bic_origin_point;/* origin point of bic function */
56 u32 bic_K; /* time to origin point from the beginning of the current epoch */
57 u32 delay_min; /* min delay */
58 u32 epoch_start; /* beginning of an epoch */
59 u32 ack_cnt; /* number of acks */
60 u32 tcp_cwnd; /* estimated tcp cwnd */
61#define ACK_RATIO_SHIFT 4
62 u32 delayed_ack; /* estimate the ratio of Packets/ACKs << 4 */
63};
64
65static inline void bictcp_reset(struct bictcp *ca)
66{
67 ca->cnt = 0;
68 ca->last_max_cwnd = 0;
69 ca->loss_cwnd = 0;
70 ca->last_cwnd = 0;
71 ca->last_time = 0;
72 ca->bic_origin_point = 0;
73 ca->bic_K = 0;
74 ca->delay_min = 0;
75 ca->epoch_start = 0;
76 ca->delayed_ack = 2 << ACK_RATIO_SHIFT;
77 ca->ack_cnt = 0;
78 ca->tcp_cwnd = 0;
79}
80
81static void bictcp_init(struct sock *sk)
82{
83 bictcp_reset(inet_csk_ca(sk));
84 if (initial_ssthresh)
85 tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
86}
87
7e58886b
SH
88/* calculate the cubic root of x using a table lookup followed by one
89 * Newton-Raphson iteration.
90 * Avg err ~= 0.195%
df3271f3 91 */
9eb2d627 92static u32 cubic_root(u64 a)
df3271f3 93{
7e58886b
SH
94 u32 x, b, shift;
95 /*
96 * cbrt(x) MSB values for x MSB values in [0..63].
97 * Precomputed then refined by hand - Willy Tarreau
98 *
99 * For x in [0..63],
100 * v = cbrt(x << 18) - 1
101 * cbrt(x) = (v[x] + 10) >> 6
9eb2d627 102 */
7e58886b
SH
103 static const u8 v[] = {
104 /* 0x00 */ 0, 54, 54, 54, 118, 118, 118, 118,
105 /* 0x08 */ 123, 129, 134, 138, 143, 147, 151, 156,
106 /* 0x10 */ 157, 161, 164, 168, 170, 173, 176, 179,
107 /* 0x18 */ 181, 185, 187, 190, 192, 194, 197, 199,
108 /* 0x20 */ 200, 202, 204, 206, 209, 211, 213, 215,
109 /* 0x28 */ 217, 219, 221, 222, 224, 225, 227, 229,
110 /* 0x30 */ 231, 232, 234, 236, 237, 239, 240, 242,
111 /* 0x38 */ 244, 245, 246, 248, 250, 251, 252, 254,
112 };
113
114 b = fls64(a);
115 if (b < 7) {
116 /* a in [0..63] */
117 return ((u32)v[(u32)a] + 35) >> 6;
118 }
119
120 b = ((b * 84) >> 8) - 1;
121 shift = (a >> (b * 3));
122
123 x = ((u32)(((u32)v[shift] + 10) << b)) >> 6;
124
125 /*
126 * Newton-Raphson iteration
127 * 2
128 * x = ( 2 * x + a / x ) / 3
129 * k+1 k k
130 */
6f6d6a1a 131 x = (2 * x + (u32)div64_u64(a, (u64)x * (u64)(x - 1)));
7e58886b 132 x = ((x * 341) >> 10);
9eb2d627 133 return x;
df3271f3
SH
134}
135
df3271f3
SH
136/*
137 * Compute congestion window to use.
138 */
139static inline void bictcp_update(struct bictcp *ca, u32 cwnd)
140{
89b3d9aa 141 u64 offs;
6b3d6263 142 u32 delta, t, bic_target, max_cnt;
df3271f3
SH
143
144 ca->ack_cnt++; /* count the number of ACKs */
145
146 if (ca->last_cwnd == cwnd &&
147 (s32)(tcp_time_stamp - ca->last_time) <= HZ / 32)
148 return;
149
150 ca->last_cwnd = cwnd;
151 ca->last_time = tcp_time_stamp;
152
df3271f3
SH
153 if (ca->epoch_start == 0) {
154 ca->epoch_start = tcp_time_stamp; /* record the beginning of an epoch */
155 ca->ack_cnt = 1; /* start counting */
156 ca->tcp_cwnd = cwnd; /* syn with cubic */
157
158 if (ca->last_max_cwnd <= cwnd) {
159 ca->bic_K = 0;
160 ca->bic_origin_point = cwnd;
161 } else {
89b3d9aa
SH
162 /* Compute new K based on
163 * (wmax-cwnd) * (srtt>>3 / HZ) / c * 2^(3*bictcp_HZ)
164 */
165 ca->bic_K = cubic_root(cube_factor
166 * (ca->last_max_cwnd - cwnd));
df3271f3
SH
167 ca->bic_origin_point = ca->last_max_cwnd;
168 }
169 }
170
e905a9ed
YH
171 /* cubic function - calc*/
172 /* calculate c * time^3 / rtt,
173 * while considering overflow in calculation of time^3
89b3d9aa 174 * (so time^3 is done by using 64 bit)
df3271f3 175 * and without the support of division of 64bit numbers
89b3d9aa 176 * (so all divisions are done by using 32 bit)
e905a9ed
YH
177 * also NOTE the unit of those veriables
178 * time = (t - K) / 2^bictcp_HZ
179 * c = bic_scale >> 10
df3271f3
SH
180 * rtt = (srtt >> 3) / HZ
181 * !!! The following code does not have overflow problems,
182 * if the cwnd < 1 million packets !!!
e905a9ed 183 */
df3271f3
SH
184
185 /* change the unit from HZ to bictcp_HZ */
e905a9ed 186 t = ((tcp_time_stamp + (ca->delay_min>>3) - ca->epoch_start)
df3271f3
SH
187 << BICTCP_HZ) / HZ;
188
e905a9ed 189 if (t < ca->bic_K) /* t - K */
89b3d9aa 190 offs = ca->bic_K - t;
e905a9ed
YH
191 else
192 offs = t - ca->bic_K;
df3271f3 193
89b3d9aa
SH
194 /* c/rtt * (t-K)^3 */
195 delta = (cube_rtt_scale * offs * offs * offs) >> (10+3*BICTCP_HZ);
e905a9ed
YH
196 if (t < ca->bic_K) /* below origin*/
197 bic_target = ca->bic_origin_point - delta;
198 else /* above origin*/
199 bic_target = ca->bic_origin_point + delta;
df3271f3 200
e905a9ed
YH
201 /* cubic function - calc bictcp_cnt*/
202 if (bic_target > cwnd) {
df3271f3 203 ca->cnt = cwnd / (bic_target - cwnd);
e905a9ed
YH
204 } else {
205 ca->cnt = 100 * cwnd; /* very small increment*/
206 }
df3271f3 207
df3271f3
SH
208 /* TCP Friendly */
209 if (tcp_friendliness) {
89b3d9aa
SH
210 u32 scale = beta_scale;
211 delta = (cwnd * scale) >> 3;
e905a9ed
YH
212 while (ca->ack_cnt > delta) { /* update tcp cwnd */
213 ca->ack_cnt -= delta;
214 ca->tcp_cwnd++;
df3271f3
SH
215 }
216
217 if (ca->tcp_cwnd > cwnd){ /* if bic is slower than tcp */
89b3d9aa
SH
218 delta = ca->tcp_cwnd - cwnd;
219 max_cnt = cwnd / delta;
df3271f3
SH
220 if (ca->cnt > max_cnt)
221 ca->cnt = max_cnt;
222 }
e905a9ed 223 }
df3271f3
SH
224
225 ca->cnt = (ca->cnt << ACK_RATIO_SHIFT) / ca->delayed_ack;
226 if (ca->cnt == 0) /* cannot be zero */
227 ca->cnt = 1;
228}
229
c3a05c60 230static void bictcp_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
df3271f3
SH
231{
232 struct tcp_sock *tp = tcp_sk(sk);
233 struct bictcp *ca = inet_csk_ca(sk);
234
df3271f3
SH
235 if (!tcp_is_cwnd_limited(sk, in_flight))
236 return;
237
238 if (tp->snd_cwnd <= tp->snd_ssthresh)
239 tcp_slow_start(tp);
240 else {
241 bictcp_update(ca, tp->snd_cwnd);
242
243 /* In dangerous area, increase slowly.
244 * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
245 */
246 if (tp->snd_cwnd_cnt >= ca->cnt) {
247 if (tp->snd_cwnd < tp->snd_cwnd_clamp)
248 tp->snd_cwnd++;
249 tp->snd_cwnd_cnt = 0;
250 } else
251 tp->snd_cwnd_cnt++;
252 }
253
254}
255
256static u32 bictcp_recalc_ssthresh(struct sock *sk)
257{
258 const struct tcp_sock *tp = tcp_sk(sk);
259 struct bictcp *ca = inet_csk_ca(sk);
260
261 ca->epoch_start = 0; /* end of epoch */
262
263 /* Wmax and fast convergence */
264 if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence)
265 ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta))
266 / (2 * BICTCP_BETA_SCALE);
267 else
268 ca->last_max_cwnd = tp->snd_cwnd;
269
270 ca->loss_cwnd = tp->snd_cwnd;
271
272 return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
273}
274
275static u32 bictcp_undo_cwnd(struct sock *sk)
276{
277 struct bictcp *ca = inet_csk_ca(sk);
278
279 return max(tcp_sk(sk)->snd_cwnd, ca->last_max_cwnd);
280}
281
df3271f3
SH
282static void bictcp_state(struct sock *sk, u8 new_state)
283{
284 if (new_state == TCP_CA_Loss)
285 bictcp_reset(inet_csk_ca(sk));
286}
287
288/* Track delayed acknowledgment ratio using sliding window
289 * ratio = (15*ratio + sample) / 16
290 */
30cfd0ba 291static void bictcp_acked(struct sock *sk, u32 cnt, s32 rtt_us)
df3271f3
SH
292{
293 const struct inet_connection_sock *icsk = inet_csk(sk);
e7d0c885
SH
294 struct bictcp *ca = inet_csk_ca(sk);
295 u32 delay;
df3271f3 296
35e86941 297 if (icsk->icsk_ca_state == TCP_CA_Open) {
df3271f3
SH
298 cnt -= ca->delayed_ack >> ACK_RATIO_SHIFT;
299 ca->delayed_ack += cnt;
300 }
df3271f3 301
e7d0c885
SH
302 /* Some calls are for duplicates without timetamps */
303 if (rtt_us < 0)
304 return;
305
306 /* Discard delay samples right after fast recovery */
307 if ((s32)(tcp_time_stamp - ca->epoch_start) < HZ)
308 return;
309
310 delay = usecs_to_jiffies(rtt_us) << 3;
311 if (delay == 0)
312 delay = 1;
313
314 /* first time call or link delay decreases */
315 if (ca->delay_min == 0 || ca->delay_min > delay)
316 ca->delay_min = delay;
317}
df3271f3
SH
318
319static struct tcp_congestion_ops cubictcp = {
320 .init = bictcp_init,
321 .ssthresh = bictcp_recalc_ssthresh,
322 .cong_avoid = bictcp_cong_avoid,
323 .set_state = bictcp_state,
324 .undo_cwnd = bictcp_undo_cwnd,
df3271f3
SH
325 .pkts_acked = bictcp_acked,
326 .owner = THIS_MODULE,
327 .name = "cubic",
328};
329
330static int __init cubictcp_register(void)
331{
74975d40 332 BUILD_BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
89b3d9aa
SH
333
334 /* Precompute a bunch of the scaling factors that are used per-packet
335 * based on SRTT of 100ms
336 */
337
338 beta_scale = 8*(BICTCP_BETA_SCALE+beta)/ 3 / (BICTCP_BETA_SCALE - beta);
339
22119240 340 cube_rtt_scale = (bic_scale * 10); /* 1024*c/rtt */
89b3d9aa
SH
341
342 /* calculate the "K" for (wmax-cwnd) = c/rtt * K^3
343 * so K = cubic_root( (wmax-cwnd)*rtt/c )
344 * the unit of K is bictcp_HZ=2^10, not HZ
345 *
346 * c = bic_scale >> 10
347 * rtt = 100ms
348 *
349 * the following code has been designed and tested for
350 * cwnd < 1 million packets
351 * RTT < 100 seconds
352 * HZ < 1,000,00 (corresponding to 10 nano-second)
353 */
354
355 /* 1/c * 2^2*bictcp_HZ * srtt */
356 cube_factor = 1ull << (10+3*BICTCP_HZ); /* 2^40 */
357
358 /* divide by bic_scale and by constant Srtt (100ms) */
359 do_div(cube_factor, bic_scale * 10);
360
df3271f3
SH
361 return tcp_register_congestion_control(&cubictcp);
362}
363
364static void __exit cubictcp_unregister(void)
365{
366 tcp_unregister_congestion_control(&cubictcp);
367}
368
369module_init(cubictcp_register);
370module_exit(cubictcp_unregister);
371
372MODULE_AUTHOR("Sangtae Ha, Stephen Hemminger");
373MODULE_LICENSE("GPL");
374MODULE_DESCRIPTION("CUBIC TCP");
6b3d6263 375MODULE_VERSION("2.2");