blob: 4a1221e5e8ee2ec8b7d1a5157aa063f6a3f8011e [file] [log] [blame]
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -08001/*
Sangtae Ha6b3d6262008-03-04 14:17:41 -08002 * TCP CUBIC: Binary Increase Congestion control for TCP v2.2
3 * Home page:
4 * http://netsrv.csc.ncsu.edu/twiki/bin/view/Main/BIC
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -08005 * 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:
Sangtae Ha6b3d6262008-03-04 14:17:41 -080010 * http://netsrv.csc.ncsu.edu/export/cubic-paper.pdf
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080011 *
12 * Unless CUBIC is enabled and congestion window is large
13 * this behaves the same as the original Reno.
14 */
15
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080016#include <linux/mm.h>
17#include <linux/module.h>
Roman Zippel6f6d6a12008-05-01 04:34:28 -070018#include <linux/math64.h>
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080019#include <net/tcp.h>
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080020
21#define BICTCP_BETA_SCALE 1024 /* Scale factor beta calculation
22 * max_cwnd = snd_cwnd * beta
23 */
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080024#define BICTCP_HZ 10 /* BIC HZ 2^10 = 1024 */
25
Stephen Hemminger59758f42007-02-12 13:15:20 -080026static int fast_convergence __read_mostly = 1;
Sangtae Ha6b3d6262008-03-04 14:17:41 -080027static int beta __read_mostly = 717; /* = 717/1024 (BICTCP_BETA_SCALE) */
David S. Miller66e1e3b2007-06-13 01:03:53 -070028static int initial_ssthresh __read_mostly;
Stephen Hemminger59758f42007-02-12 13:15:20 -080029static int bic_scale __read_mostly = 41;
30static int tcp_friendliness __read_mostly = 1;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080031
Stephen Hemminger59758f42007-02-12 13:15:20 -080032static u32 cube_rtt_scale __read_mostly;
33static u32 beta_scale __read_mostly;
34static u64 cube_factor __read_mostly;
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -080035
36/* Note parameters that are used for precomputing scale factors are read-only */
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080037module_param(fast_convergence, int, 0644);
38MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
Sangtae Ha6b3d6262008-03-04 14:17:41 -080039module_param(beta, int, 0644);
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080040MODULE_PARM_DESC(beta, "beta for multiplicative increase");
41module_param(initial_ssthresh, int, 0644);
42MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -080043module_param(bic_scale, int, 0444);
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080044MODULE_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
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080048/* 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
Stephen Hemminger7e588862007-03-22 12:10:58 -070088/* calculate the cubic root of x using a table lookup followed by one
89 * Newton-Raphson iteration.
90 * Avg err ~= 0.195%
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080091 */
Stephen Hemminger9eb2d622005-12-21 19:32:36 -080092static u32 cubic_root(u64 a)
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080093{
Stephen Hemminger7e588862007-03-22 12:10:58 -070094 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
Stephen Hemminger9eb2d622005-12-21 19:32:36 -0800102 */
Stephen Hemminger7e588862007-03-22 12:10:58 -0700103 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 };
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800113
Stephen Hemminger7e588862007-03-22 12:10:58 -0700114 b = fls64(a);
115 if (b < 7) {
116 /* a in [0..63] */
117 return ((u32)v[(u32)a] + 35) >> 6;
118 }
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800119
Stephen Hemminger7e588862007-03-22 12:10:58 -0700120 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 */
Roman Zippel6f6d6a12008-05-01 04:34:28 -0700131 x = (2 * x + (u32)div64_u64(a, (u64)x * (u64)(x - 1)));
Stephen Hemminger7e588862007-03-22 12:10:58 -0700132 x = ((x * 341) >> 10);
Stephen Hemminger9eb2d622005-12-21 19:32:36 -0800133 return x;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800134}
135
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800136/*
137 * Compute congestion window to use.
138 */
139static inline void bictcp_update(struct bictcp *ca, u32 cwnd)
140{
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800141 u64 offs;
Sangtae Ha6b3d6262008-03-04 14:17:41 -0800142 u32 delta, t, bic_target, max_cnt;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800143
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
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800153 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 {
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800162 /* 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));
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800167 ca->bic_origin_point = ca->last_max_cwnd;
168 }
169 }
170
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900171 /* cubic function - calc*/
172 /* calculate c * time^3 / rtt,
173 * while considering overflow in calculation of time^3
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800174 * (so time^3 is done by using 64 bit)
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800175 * and without the support of division of 64bit numbers
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800176 * (so all divisions are done by using 32 bit)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900177 * also NOTE the unit of those veriables
178 * time = (t - K) / 2^bictcp_HZ
179 * c = bic_scale >> 10
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800180 * rtt = (srtt >> 3) / HZ
181 * !!! The following code does not have overflow problems,
182 * if the cwnd < 1 million packets !!!
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900183 */
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800184
185 /* change the unit from HZ to bictcp_HZ */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900186 t = ((tcp_time_stamp + (ca->delay_min>>3) - ca->epoch_start)
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800187 << BICTCP_HZ) / HZ;
188
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900189 if (t < ca->bic_K) /* t - K */
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800190 offs = ca->bic_K - t;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900191 else
192 offs = t - ca->bic_K;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800193
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800194 /* c/rtt * (t-K)^3 */
195 delta = (cube_rtt_scale * offs * offs * offs) >> (10+3*BICTCP_HZ);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900196 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;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800200
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900201 /* cubic function - calc bictcp_cnt*/
202 if (bic_target > cwnd) {
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800203 ca->cnt = cwnd / (bic_target - cwnd);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900204 } else {
205 ca->cnt = 100 * cwnd; /* very small increment*/
206 }
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800207
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800208 /* TCP Friendly */
209 if (tcp_friendliness) {
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800210 u32 scale = beta_scale;
211 delta = (cwnd * scale) >> 3;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900212 while (ca->ack_cnt > delta) { /* update tcp cwnd */
213 ca->ack_cnt -= delta;
214 ca->tcp_cwnd++;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800215 }
216
217 if (ca->tcp_cwnd > cwnd){ /* if bic is slower than tcp */
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800218 delta = ca->tcp_cwnd - cwnd;
219 max_cnt = cwnd / delta;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800220 if (ca->cnt > max_cnt)
221 ca->cnt = max_cnt;
222 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900223 }
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800224
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
Ilpo Järvinenc3a05c62007-12-02 00:47:59 +0200230static void bictcp_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800231{
232 struct tcp_sock *tp = tcp_sk(sk);
233 struct bictcp *ca = inet_csk_ca(sk);
234
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800235 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
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800282static 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 */
Stephen Hemminger30cfd0b2007-07-25 23:49:34 -0700291static void bictcp_acked(struct sock *sk, u32 cnt, s32 rtt_us)
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800292{
293 const struct inet_connection_sock *icsk = inet_csk(sk);
Stephen Hemmingere7d0c8852007-07-25 23:50:06 -0700294 struct bictcp *ca = inet_csk_ca(sk);
295 u32 delay;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800296
Ilpo Järvinen35e86942007-05-31 10:16:47 +0300297 if (icsk->icsk_ca_state == TCP_CA_Open) {
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800298 cnt -= ca->delayed_ack >> ACK_RATIO_SHIFT;
299 ca->delayed_ack += cnt;
300 }
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800301
Stephen Hemmingere7d0c8852007-07-25 23:50:06 -0700302 /* 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}
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800318
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,
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800325 .pkts_acked = bictcp_acked,
326 .owner = THIS_MODULE,
327 .name = "cubic",
328};
329
330static int __init cubictcp_register(void)
331{
Alexey Dobriyan74975d42006-08-25 17:10:33 -0700332 BUILD_BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800333
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
Stephen Hemminger22119242006-10-25 23:04:12 -0700340 cube_rtt_scale = (bic_scale * 10); /* 1024*c/rtt */
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800341
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
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800361 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");
Sangtae Ha6b3d6262008-03-04 14:17:41 -0800375MODULE_VERSION("2.2");