blob: 5ce6cd85680b66d52cbdcb804811d54389e6ae0f [file] [log] [blame]
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -08001/*
2 * TCP CUBIC: Binary Increase Congestion control for TCP v2.0
3 *
4 * This is from the implementation of CUBIC TCP in
5 * Injong Rhee, Lisong Xu.
6 * "CUBIC: A New TCP-Friendly High-Speed TCP Variant
7 * in PFLDnet 2005
8 * Available from:
9 * http://www.csc.ncsu.edu/faculty/rhee/export/bitcp/cubic-paper.pdf
10 *
11 * Unless CUBIC is enabled and congestion window is large
12 * this behaves the same as the original Reno.
13 */
14
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080015#include <linux/mm.h>
16#include <linux/module.h>
17#include <net/tcp.h>
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -080018#include <asm/div64.h>
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080019
20#define BICTCP_BETA_SCALE 1024 /* Scale factor beta calculation
21 * max_cwnd = snd_cwnd * beta
22 */
23#define BICTCP_B 4 /*
24 * In binary search,
25 * go to point (max+min)/N
26 */
27#define BICTCP_HZ 10 /* BIC HZ 2^10 = 1024 */
28
29static int fast_convergence = 1;
30static int max_increment = 16;
31static int beta = 819; /* = 819/1024 (BICTCP_BETA_SCALE) */
32static int initial_ssthresh = 100;
33static int bic_scale = 41;
34static int tcp_friendliness = 1;
35
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -080036static u32 cube_rtt_scale;
37static u32 beta_scale;
38static u64 cube_factor;
39
40/* Note parameters that are used for precomputing scale factors are read-only */
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080041module_param(fast_convergence, int, 0644);
42MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
43module_param(max_increment, int, 0644);
44MODULE_PARM_DESC(max_increment, "Limit on increment allowed during binary search");
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -080045module_param(beta, int, 0444);
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080046MODULE_PARM_DESC(beta, "beta for multiplicative increase");
47module_param(initial_ssthresh, int, 0644);
48MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -080049module_param(bic_scale, int, 0444);
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080050MODULE_PARM_DESC(bic_scale, "scale (scaled by 1024) value for bic function (bic_scale/1024)");
51module_param(tcp_friendliness, int, 0644);
52MODULE_PARM_DESC(tcp_friendliness, "turn on/off tcp friendliness");
53
Stephen Hemminger9eb2d622005-12-21 19:32:36 -080054#include <asm/div64.h>
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080055
56/* BIC TCP Parameters */
57struct bictcp {
58 u32 cnt; /* increase cwnd by 1 after ACKs */
59 u32 last_max_cwnd; /* last maximum snd_cwnd */
60 u32 loss_cwnd; /* congestion window at last loss */
61 u32 last_cwnd; /* the last snd_cwnd */
62 u32 last_time; /* time when updated last_cwnd */
63 u32 bic_origin_point;/* origin point of bic function */
64 u32 bic_K; /* time to origin point from the beginning of the current epoch */
65 u32 delay_min; /* min delay */
66 u32 epoch_start; /* beginning of an epoch */
67 u32 ack_cnt; /* number of acks */
68 u32 tcp_cwnd; /* estimated tcp cwnd */
69#define ACK_RATIO_SHIFT 4
70 u32 delayed_ack; /* estimate the ratio of Packets/ACKs << 4 */
71};
72
73static inline void bictcp_reset(struct bictcp *ca)
74{
75 ca->cnt = 0;
76 ca->last_max_cwnd = 0;
77 ca->loss_cwnd = 0;
78 ca->last_cwnd = 0;
79 ca->last_time = 0;
80 ca->bic_origin_point = 0;
81 ca->bic_K = 0;
82 ca->delay_min = 0;
83 ca->epoch_start = 0;
84 ca->delayed_ack = 2 << ACK_RATIO_SHIFT;
85 ca->ack_cnt = 0;
86 ca->tcp_cwnd = 0;
87}
88
89static void bictcp_init(struct sock *sk)
90{
91 bictcp_reset(inet_csk_ca(sk));
92 if (initial_ssthresh)
93 tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
94}
95
Stephen Hemminger9eb2d622005-12-21 19:32:36 -080096/* 64bit divisor, dividend and result. dynamic precision */
97static inline u_int64_t div64_64(u_int64_t dividend, u_int64_t divisor)
98{
99 u_int32_t d = divisor;
100
101 if (divisor > 0xffffffffULL) {
102 unsigned int shift = fls(divisor >> 32);
103
104 d = divisor >> shift;
105 dividend >>= shift;
106 }
107
108 /* avoid 64 bit division if possible */
109 if (dividend >> 32)
110 do_div(dividend, d);
111 else
112 dividend = (uint32_t) dividend / d;
113
114 return dividend;
115}
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800116
117/*
Stephen Hemminger9eb2d622005-12-21 19:32:36 -0800118 * calculate the cubic root of x using Newton-Raphson
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800119 */
Stephen Hemminger9eb2d622005-12-21 19:32:36 -0800120static u32 cubic_root(u64 a)
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800121{
Stephen Hemminger9eb2d622005-12-21 19:32:36 -0800122 u32 x, x1;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800123
Stephen Hemminger9eb2d622005-12-21 19:32:36 -0800124 /* Initial estimate is based on:
125 * cbrt(x) = exp(log(x) / 3)
126 */
127 x = 1u << (fls64(a)/3);
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800128
Stephen Hemminger9eb2d622005-12-21 19:32:36 -0800129 /*
130 * Iteration based on:
131 * 2
132 * x = ( 2 * x + a / x ) / 3
133 * k+1 k k
134 */
135 do {
136 x1 = x;
137 x = (2 * x + (uint32_t) div64_64(a, x*x)) / 3;
138 } while (abs(x1 - x) > 1);
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800139
Stephen Hemminger9eb2d622005-12-21 19:32:36 -0800140 return x;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800141}
142
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800143/*
144 * Compute congestion window to use.
145 */
146static inline void bictcp_update(struct bictcp *ca, u32 cwnd)
147{
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800148 u64 offs;
149 u32 delta, t, bic_target, min_cnt, max_cnt;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800150
151 ca->ack_cnt++; /* count the number of ACKs */
152
153 if (ca->last_cwnd == cwnd &&
154 (s32)(tcp_time_stamp - ca->last_time) <= HZ / 32)
155 return;
156
157 ca->last_cwnd = cwnd;
158 ca->last_time = tcp_time_stamp;
159
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800160 if (ca->epoch_start == 0) {
161 ca->epoch_start = tcp_time_stamp; /* record the beginning of an epoch */
162 ca->ack_cnt = 1; /* start counting */
163 ca->tcp_cwnd = cwnd; /* syn with cubic */
164
165 if (ca->last_max_cwnd <= cwnd) {
166 ca->bic_K = 0;
167 ca->bic_origin_point = cwnd;
168 } else {
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800169 /* Compute new K based on
170 * (wmax-cwnd) * (srtt>>3 / HZ) / c * 2^(3*bictcp_HZ)
171 */
172 ca->bic_K = cubic_root(cube_factor
173 * (ca->last_max_cwnd - cwnd));
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800174 ca->bic_origin_point = ca->last_max_cwnd;
175 }
176 }
177
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900178 /* cubic function - calc*/
179 /* calculate c * time^3 / rtt,
180 * while considering overflow in calculation of time^3
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800181 * (so time^3 is done by using 64 bit)
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800182 * and without the support of division of 64bit numbers
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800183 * (so all divisions are done by using 32 bit)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900184 * also NOTE the unit of those veriables
185 * time = (t - K) / 2^bictcp_HZ
186 * c = bic_scale >> 10
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800187 * rtt = (srtt >> 3) / HZ
188 * !!! The following code does not have overflow problems,
189 * if the cwnd < 1 million packets !!!
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900190 */
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800191
192 /* change the unit from HZ to bictcp_HZ */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900193 t = ((tcp_time_stamp + (ca->delay_min>>3) - ca->epoch_start)
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800194 << BICTCP_HZ) / HZ;
195
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900196 if (t < ca->bic_K) /* t - K */
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800197 offs = ca->bic_K - t;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900198 else
199 offs = t - ca->bic_K;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800200
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800201 /* c/rtt * (t-K)^3 */
202 delta = (cube_rtt_scale * offs * offs * offs) >> (10+3*BICTCP_HZ);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900203 if (t < ca->bic_K) /* below origin*/
204 bic_target = ca->bic_origin_point - delta;
205 else /* above origin*/
206 bic_target = ca->bic_origin_point + delta;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800207
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900208 /* cubic function - calc bictcp_cnt*/
209 if (bic_target > cwnd) {
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800210 ca->cnt = cwnd / (bic_target - cwnd);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900211 } else {
212 ca->cnt = 100 * cwnd; /* very small increment*/
213 }
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800214
215 if (ca->delay_min > 0) {
216 /* max increment = Smax * rtt / 0.1 */
217 min_cnt = (cwnd * HZ * 8)/(10 * max_increment * ca->delay_min);
218 if (ca->cnt < min_cnt)
219 ca->cnt = min_cnt;
220 }
221
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900222 /* slow start and low utilization */
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800223 if (ca->loss_cwnd == 0) /* could be aggressive in slow start */
224 ca->cnt = 50;
225
226 /* TCP Friendly */
227 if (tcp_friendliness) {
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800228 u32 scale = beta_scale;
229 delta = (cwnd * scale) >> 3;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900230 while (ca->ack_cnt > delta) { /* update tcp cwnd */
231 ca->ack_cnt -= delta;
232 ca->tcp_cwnd++;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800233 }
234
235 if (ca->tcp_cwnd > cwnd){ /* if bic is slower than tcp */
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800236 delta = ca->tcp_cwnd - cwnd;
237 max_cnt = cwnd / delta;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800238 if (ca->cnt > max_cnt)
239 ca->cnt = max_cnt;
240 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900241 }
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800242
243 ca->cnt = (ca->cnt << ACK_RATIO_SHIFT) / ca->delayed_ack;
244 if (ca->cnt == 0) /* cannot be zero */
245 ca->cnt = 1;
246}
247
248
249/* Keep track of minimum rtt */
250static inline void measure_delay(struct sock *sk)
251{
252 const struct tcp_sock *tp = tcp_sk(sk);
253 struct bictcp *ca = inet_csk_ca(sk);
254 u32 delay;
255
256 /* No time stamp */
257 if (!(tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) ||
258 /* Discard delay samples right after fast recovery */
259 (s32)(tcp_time_stamp - ca->epoch_start) < HZ)
260 return;
261
Stephen Hemminger22119242006-10-25 23:04:12 -0700262 delay = (tcp_time_stamp - tp->rx_opt.rcv_tsecr)<<3;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800263 if (delay == 0)
264 delay = 1;
265
266 /* first time call or link delay decreases */
267 if (ca->delay_min == 0 || ca->delay_min > delay)
268 ca->delay_min = delay;
269}
270
271static void bictcp_cong_avoid(struct sock *sk, u32 ack,
272 u32 seq_rtt, u32 in_flight, int data_acked)
273{
274 struct tcp_sock *tp = tcp_sk(sk);
275 struct bictcp *ca = inet_csk_ca(sk);
276
277 if (data_acked)
278 measure_delay(sk);
279
280 if (!tcp_is_cwnd_limited(sk, in_flight))
281 return;
282
283 if (tp->snd_cwnd <= tp->snd_ssthresh)
284 tcp_slow_start(tp);
285 else {
286 bictcp_update(ca, tp->snd_cwnd);
287
288 /* In dangerous area, increase slowly.
289 * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
290 */
291 if (tp->snd_cwnd_cnt >= ca->cnt) {
292 if (tp->snd_cwnd < tp->snd_cwnd_clamp)
293 tp->snd_cwnd++;
294 tp->snd_cwnd_cnt = 0;
295 } else
296 tp->snd_cwnd_cnt++;
297 }
298
299}
300
301static u32 bictcp_recalc_ssthresh(struct sock *sk)
302{
303 const struct tcp_sock *tp = tcp_sk(sk);
304 struct bictcp *ca = inet_csk_ca(sk);
305
306 ca->epoch_start = 0; /* end of epoch */
307
308 /* Wmax and fast convergence */
309 if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence)
310 ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta))
311 / (2 * BICTCP_BETA_SCALE);
312 else
313 ca->last_max_cwnd = tp->snd_cwnd;
314
315 ca->loss_cwnd = tp->snd_cwnd;
316
317 return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
318}
319
320static u32 bictcp_undo_cwnd(struct sock *sk)
321{
322 struct bictcp *ca = inet_csk_ca(sk);
323
324 return max(tcp_sk(sk)->snd_cwnd, ca->last_max_cwnd);
325}
326
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800327static void bictcp_state(struct sock *sk, u8 new_state)
328{
329 if (new_state == TCP_CA_Loss)
330 bictcp_reset(inet_csk_ca(sk));
331}
332
333/* Track delayed acknowledgment ratio using sliding window
334 * ratio = (15*ratio + sample) / 16
335 */
336static void bictcp_acked(struct sock *sk, u32 cnt)
337{
338 const struct inet_connection_sock *icsk = inet_csk(sk);
339
340 if (cnt > 0 && icsk->icsk_ca_state == TCP_CA_Open) {
341 struct bictcp *ca = inet_csk_ca(sk);
342 cnt -= ca->delayed_ack >> ACK_RATIO_SHIFT;
343 ca->delayed_ack += cnt;
344 }
345}
346
347
348static struct tcp_congestion_ops cubictcp = {
349 .init = bictcp_init,
350 .ssthresh = bictcp_recalc_ssthresh,
351 .cong_avoid = bictcp_cong_avoid,
352 .set_state = bictcp_state,
353 .undo_cwnd = bictcp_undo_cwnd,
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800354 .pkts_acked = bictcp_acked,
355 .owner = THIS_MODULE,
356 .name = "cubic",
357};
358
359static int __init cubictcp_register(void)
360{
Alexey Dobriyan74975d42006-08-25 17:10:33 -0700361 BUILD_BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800362
363 /* Precompute a bunch of the scaling factors that are used per-packet
364 * based on SRTT of 100ms
365 */
366
367 beta_scale = 8*(BICTCP_BETA_SCALE+beta)/ 3 / (BICTCP_BETA_SCALE - beta);
368
Stephen Hemminger22119242006-10-25 23:04:12 -0700369 cube_rtt_scale = (bic_scale * 10); /* 1024*c/rtt */
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800370
371 /* calculate the "K" for (wmax-cwnd) = c/rtt * K^3
372 * so K = cubic_root( (wmax-cwnd)*rtt/c )
373 * the unit of K is bictcp_HZ=2^10, not HZ
374 *
375 * c = bic_scale >> 10
376 * rtt = 100ms
377 *
378 * the following code has been designed and tested for
379 * cwnd < 1 million packets
380 * RTT < 100 seconds
381 * HZ < 1,000,00 (corresponding to 10 nano-second)
382 */
383
384 /* 1/c * 2^2*bictcp_HZ * srtt */
385 cube_factor = 1ull << (10+3*BICTCP_HZ); /* 2^40 */
386
387 /* divide by bic_scale and by constant Srtt (100ms) */
388 do_div(cube_factor, bic_scale * 10);
389
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800390 return tcp_register_congestion_control(&cubictcp);
391}
392
393static void __exit cubictcp_unregister(void)
394{
395 tcp_unregister_congestion_control(&cubictcp);
396}
397
398module_init(cubictcp_register);
399module_exit(cubictcp_unregister);
400
401MODULE_AUTHOR("Sangtae Ha, Stephen Hemminger");
402MODULE_LICENSE("GPL");
403MODULE_DESCRIPTION("CUBIC TCP");
404MODULE_VERSION("2.0");