blob: 0e6cdfeb207a15af1f9c51453f35cd8df8cd586a [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
Stephen Hemminger59758f42007-02-12 13:15:20 -080029static int fast_convergence __read_mostly = 1;
30static int max_increment __read_mostly = 16;
31static int beta __read_mostly = 819; /* = 819/1024 (BICTCP_BETA_SCALE) */
32static int initial_ssthresh __read_mostly = 100;
33static int bic_scale __read_mostly = 41;
34static int tcp_friendliness __read_mostly = 1;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080035
Stephen Hemminger59758f42007-02-12 13:15:20 -080036static u32 cube_rtt_scale __read_mostly;
37static u32 beta_scale __read_mostly;
38static u64 cube_factor __read_mostly;
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -080039
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 Hemmingerdf3271f2005-12-13 23:13:28 -080054/* BIC TCP Parameters */
55struct bictcp {
56 u32 cnt; /* increase cwnd by 1 after ACKs */
57 u32 last_max_cwnd; /* last maximum snd_cwnd */
58 u32 loss_cwnd; /* congestion window at last loss */
59 u32 last_cwnd; /* the last snd_cwnd */
60 u32 last_time; /* time when updated last_cwnd */
61 u32 bic_origin_point;/* origin point of bic function */
62 u32 bic_K; /* time to origin point from the beginning of the current epoch */
63 u32 delay_min; /* min delay */
64 u32 epoch_start; /* beginning of an epoch */
65 u32 ack_cnt; /* number of acks */
66 u32 tcp_cwnd; /* estimated tcp cwnd */
67#define ACK_RATIO_SHIFT 4
68 u32 delayed_ack; /* estimate the ratio of Packets/ACKs << 4 */
69};
70
71static inline void bictcp_reset(struct bictcp *ca)
72{
73 ca->cnt = 0;
74 ca->last_max_cwnd = 0;
75 ca->loss_cwnd = 0;
76 ca->last_cwnd = 0;
77 ca->last_time = 0;
78 ca->bic_origin_point = 0;
79 ca->bic_K = 0;
80 ca->delay_min = 0;
81 ca->epoch_start = 0;
82 ca->delayed_ack = 2 << ACK_RATIO_SHIFT;
83 ca->ack_cnt = 0;
84 ca->tcp_cwnd = 0;
85}
86
87static void bictcp_init(struct sock *sk)
88{
89 bictcp_reset(inet_csk_ca(sk));
90 if (initial_ssthresh)
91 tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
92}
93
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080094/*
Stephen Hemminger9eb2d622005-12-21 19:32:36 -080095 * calculate the cubic root of x using Newton-Raphson
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080096 */
Stephen Hemminger9eb2d622005-12-21 19:32:36 -080097static u32 cubic_root(u64 a)
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -080098{
Stephen Hemmingerc5f58772007-03-25 20:21:15 -070099 u32 x;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800100
Stephen Hemminger9eb2d622005-12-21 19:32:36 -0800101 /* Initial estimate is based on:
102 * cbrt(x) = exp(log(x) / 3)
103 */
104 x = 1u << (fls64(a)/3);
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800105
Stephen Hemmingerc5f58772007-03-25 20:21:15 -0700106 /* converges to 32 bits in 3 iterations */
107 x = (2 * x + (u32)div64_64(a, (u64)x*(u64)x)) / 3;
108 x = (2 * x + (u32)div64_64(a, (u64)x*(u64)x)) / 3;
109 x = (2 * x + (u32)div64_64(a, (u64)x*(u64)x)) / 3;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800110
Stephen Hemminger9eb2d622005-12-21 19:32:36 -0800111 return x;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800112}
113
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800114/*
115 * Compute congestion window to use.
116 */
117static inline void bictcp_update(struct bictcp *ca, u32 cwnd)
118{
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800119 u64 offs;
120 u32 delta, t, bic_target, min_cnt, max_cnt;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800121
122 ca->ack_cnt++; /* count the number of ACKs */
123
124 if (ca->last_cwnd == cwnd &&
125 (s32)(tcp_time_stamp - ca->last_time) <= HZ / 32)
126 return;
127
128 ca->last_cwnd = cwnd;
129 ca->last_time = tcp_time_stamp;
130
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800131 if (ca->epoch_start == 0) {
132 ca->epoch_start = tcp_time_stamp; /* record the beginning of an epoch */
133 ca->ack_cnt = 1; /* start counting */
134 ca->tcp_cwnd = cwnd; /* syn with cubic */
135
136 if (ca->last_max_cwnd <= cwnd) {
137 ca->bic_K = 0;
138 ca->bic_origin_point = cwnd;
139 } else {
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800140 /* Compute new K based on
141 * (wmax-cwnd) * (srtt>>3 / HZ) / c * 2^(3*bictcp_HZ)
142 */
143 ca->bic_K = cubic_root(cube_factor
144 * (ca->last_max_cwnd - cwnd));
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800145 ca->bic_origin_point = ca->last_max_cwnd;
146 }
147 }
148
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900149 /* cubic function - calc*/
150 /* calculate c * time^3 / rtt,
151 * while considering overflow in calculation of time^3
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800152 * (so time^3 is done by using 64 bit)
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800153 * and without the support of division of 64bit numbers
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800154 * (so all divisions are done by using 32 bit)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900155 * also NOTE the unit of those veriables
156 * time = (t - K) / 2^bictcp_HZ
157 * c = bic_scale >> 10
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800158 * rtt = (srtt >> 3) / HZ
159 * !!! The following code does not have overflow problems,
160 * if the cwnd < 1 million packets !!!
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900161 */
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800162
163 /* change the unit from HZ to bictcp_HZ */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900164 t = ((tcp_time_stamp + (ca->delay_min>>3) - ca->epoch_start)
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800165 << BICTCP_HZ) / HZ;
166
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900167 if (t < ca->bic_K) /* t - K */
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800168 offs = ca->bic_K - t;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900169 else
170 offs = t - ca->bic_K;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800171
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800172 /* c/rtt * (t-K)^3 */
173 delta = (cube_rtt_scale * offs * offs * offs) >> (10+3*BICTCP_HZ);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900174 if (t < ca->bic_K) /* below origin*/
175 bic_target = ca->bic_origin_point - delta;
176 else /* above origin*/
177 bic_target = ca->bic_origin_point + delta;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800178
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900179 /* cubic function - calc bictcp_cnt*/
180 if (bic_target > cwnd) {
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800181 ca->cnt = cwnd / (bic_target - cwnd);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900182 } else {
183 ca->cnt = 100 * cwnd; /* very small increment*/
184 }
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800185
186 if (ca->delay_min > 0) {
187 /* max increment = Smax * rtt / 0.1 */
188 min_cnt = (cwnd * HZ * 8)/(10 * max_increment * ca->delay_min);
189 if (ca->cnt < min_cnt)
190 ca->cnt = min_cnt;
191 }
192
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900193 /* slow start and low utilization */
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800194 if (ca->loss_cwnd == 0) /* could be aggressive in slow start */
195 ca->cnt = 50;
196
197 /* TCP Friendly */
198 if (tcp_friendliness) {
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800199 u32 scale = beta_scale;
200 delta = (cwnd * scale) >> 3;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900201 while (ca->ack_cnt > delta) { /* update tcp cwnd */
202 ca->ack_cnt -= delta;
203 ca->tcp_cwnd++;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800204 }
205
206 if (ca->tcp_cwnd > cwnd){ /* if bic is slower than tcp */
Stephen Hemminger89b3d9a2005-12-21 19:32:08 -0800207 delta = ca->tcp_cwnd - cwnd;
208 max_cnt = cwnd / delta;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800209 if (ca->cnt > max_cnt)
210 ca->cnt = max_cnt;
211 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900212 }
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800213
214 ca->cnt = (ca->cnt << ACK_RATIO_SHIFT) / ca->delayed_ack;
215 if (ca->cnt == 0) /* cannot be zero */
216 ca->cnt = 1;
217}
218
219
220/* Keep track of minimum rtt */
221static inline void measure_delay(struct sock *sk)
222{
223 const struct tcp_sock *tp = tcp_sk(sk);
224 struct bictcp *ca = inet_csk_ca(sk);
225 u32 delay;
226
227 /* No time stamp */
228 if (!(tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) ||
229 /* Discard delay samples right after fast recovery */
230 (s32)(tcp_time_stamp - ca->epoch_start) < HZ)
231 return;
232
Stephen Hemminger22119242006-10-25 23:04:12 -0700233 delay = (tcp_time_stamp - tp->rx_opt.rcv_tsecr)<<3;
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800234 if (delay == 0)
235 delay = 1;
236
237 /* first time call or link delay decreases */
238 if (ca->delay_min == 0 || ca->delay_min > delay)
239 ca->delay_min = delay;
240}
241
242static void bictcp_cong_avoid(struct sock *sk, u32 ack,
243 u32 seq_rtt, u32 in_flight, int data_acked)
244{
245 struct tcp_sock *tp = tcp_sk(sk);
246 struct bictcp *ca = inet_csk_ca(sk);
247
248 if (data_acked)
249 measure_delay(sk);
250
251 if (!tcp_is_cwnd_limited(sk, in_flight))
252 return;
253
254 if (tp->snd_cwnd <= tp->snd_ssthresh)
255 tcp_slow_start(tp);
256 else {
257 bictcp_update(ca, tp->snd_cwnd);
258
259 /* In dangerous area, increase slowly.
260 * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
261 */
262 if (tp->snd_cwnd_cnt >= ca->cnt) {
263 if (tp->snd_cwnd < tp->snd_cwnd_clamp)
264 tp->snd_cwnd++;
265 tp->snd_cwnd_cnt = 0;
266 } else
267 tp->snd_cwnd_cnt++;
268 }
269
270}
271
272static u32 bictcp_recalc_ssthresh(struct sock *sk)
273{
274 const struct tcp_sock *tp = tcp_sk(sk);
275 struct bictcp *ca = inet_csk_ca(sk);
276
277 ca->epoch_start = 0; /* end of epoch */
278
279 /* Wmax and fast convergence */
280 if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence)
281 ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta))
282 / (2 * BICTCP_BETA_SCALE);
283 else
284 ca->last_max_cwnd = tp->snd_cwnd;
285
286 ca->loss_cwnd = tp->snd_cwnd;
287
288 return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
289}
290
291static u32 bictcp_undo_cwnd(struct sock *sk)
292{
293 struct bictcp *ca = inet_csk_ca(sk);
294
295 return max(tcp_sk(sk)->snd_cwnd, ca->last_max_cwnd);
296}
297
Stephen Hemmingerdf3271f2005-12-13 23:13:28 -0800298static void bictcp_state(struct sock *sk, u8 new_state)
299{
300 if (new_state == TCP_CA_Loss)
301 bictcp_reset(inet_csk_ca(sk));
302}
303
304/* Track delayed acknowledgment ratio using sliding window
305 * ratio = (15*ratio + sample) / 16
306 */
307static void bictcp_acked(struct sock *sk, u32 cnt)
308{
309 const struct inet_connection_sock *icsk = inet_csk(sk);
310
311 if (cnt > 0 && icsk->icsk_ca_state == TCP_CA_Open) {
312 struct bictcp *ca = inet_csk_ca(sk);
313 cnt -= ca->delayed_ack >> ACK_RATIO_SHIFT;
314 ca->delayed_ack += cnt;
315 }
316}
317
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,
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");
375MODULE_VERSION("2.0");