hugetlb: reserve huge pages for reliable MAP_PRIVATE hugetlbfs mappings until fork()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv4 / tcp_cubic.c
blob4a1221e5e8ee2ec8b7d1a5157aa063f6a3f8011e
1 /*
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
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:
10 * http://netsrv.csc.ncsu.edu/export/cubic-paper.pdf
12 * Unless CUBIC is enabled and congestion window is large
13 * this behaves the same as the original Reno.
16 #include <linux/mm.h>
17 #include <linux/module.h>
18 #include <linux/math64.h>
19 #include <net/tcp.h>
21 #define BICTCP_BETA_SCALE 1024 /* Scale factor beta calculation
22 * max_cwnd = snd_cwnd * beta
24 #define BICTCP_HZ 10 /* BIC HZ 2^10 = 1024 */
26 static int fast_convergence __read_mostly = 1;
27 static int beta __read_mostly = 717; /* = 717/1024 (BICTCP_BETA_SCALE) */
28 static int initial_ssthresh __read_mostly;
29 static int bic_scale __read_mostly = 41;
30 static int tcp_friendliness __read_mostly = 1;
32 static u32 cube_rtt_scale __read_mostly;
33 static u32 beta_scale __read_mostly;
34 static u64 cube_factor __read_mostly;
36 /* Note parameters that are used for precomputing scale factors are read-only */
37 module_param(fast_convergence, int, 0644);
38 MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
39 module_param(beta, int, 0644);
40 MODULE_PARM_DESC(beta, "beta for multiplicative increase");
41 module_param(initial_ssthresh, int, 0644);
42 MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
43 module_param(bic_scale, int, 0444);
44 MODULE_PARM_DESC(bic_scale, "scale (scaled by 1024) value for bic function (bic_scale/1024)");
45 module_param(tcp_friendliness, int, 0644);
46 MODULE_PARM_DESC(tcp_friendliness, "turn on/off tcp friendliness");
48 /* BIC TCP Parameters */
49 struct 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 */
65 static inline void bictcp_reset(struct bictcp *ca)
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;
81 static void bictcp_init(struct sock *sk)
83 bictcp_reset(inet_csk_ca(sk));
84 if (initial_ssthresh)
85 tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
88 /* calculate the cubic root of x using a table lookup followed by one
89 * Newton-Raphson iteration.
90 * Avg err ~= 0.195%
92 static u32 cubic_root(u64 a)
94 u32 x, b, shift;
96 * cbrt(x) MSB values for x MSB values in [0..63].
97 * Precomputed then refined by hand - Willy Tarreau
99 * For x in [0..63],
100 * v = cbrt(x << 18) - 1
101 * cbrt(x) = (v[x] + 10) >> 6
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,
114 b = fls64(a);
115 if (b < 7) {
116 /* a in [0..63] */
117 return ((u32)v[(u32)a] + 35) >> 6;
120 b = ((b * 84) >> 8) - 1;
121 shift = (a >> (b * 3));
123 x = ((u32)(((u32)v[shift] + 10) << b)) >> 6;
126 * Newton-Raphson iteration
128 * x = ( 2 * x + a / x ) / 3
129 * k+1 k k
131 x = (2 * x + (u32)div64_u64(a, (u64)x * (u64)(x - 1)));
132 x = ((x * 341) >> 10);
133 return x;
137 * Compute congestion window to use.
139 static inline void bictcp_update(struct bictcp *ca, u32 cwnd)
141 u64 offs;
142 u32 delta, t, bic_target, max_cnt;
144 ca->ack_cnt++; /* count the number of ACKs */
146 if (ca->last_cwnd == cwnd &&
147 (s32)(tcp_time_stamp - ca->last_time) <= HZ / 32)
148 return;
150 ca->last_cwnd = cwnd;
151 ca->last_time = tcp_time_stamp;
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 */
158 if (ca->last_max_cwnd <= cwnd) {
159 ca->bic_K = 0;
160 ca->bic_origin_point = cwnd;
161 } else {
162 /* Compute new K based on
163 * (wmax-cwnd) * (srtt>>3 / HZ) / c * 2^(3*bictcp_HZ)
165 ca->bic_K = cubic_root(cube_factor
166 * (ca->last_max_cwnd - cwnd));
167 ca->bic_origin_point = ca->last_max_cwnd;
171 /* cubic function - calc*/
172 /* calculate c * time^3 / rtt,
173 * while considering overflow in calculation of time^3
174 * (so time^3 is done by using 64 bit)
175 * and without the support of division of 64bit numbers
176 * (so all divisions are done by using 32 bit)
177 * also NOTE the unit of those veriables
178 * time = (t - K) / 2^bictcp_HZ
179 * c = bic_scale >> 10
180 * rtt = (srtt >> 3) / HZ
181 * !!! The following code does not have overflow problems,
182 * if the cwnd < 1 million packets !!!
185 /* change the unit from HZ to bictcp_HZ */
186 t = ((tcp_time_stamp + (ca->delay_min>>3) - ca->epoch_start)
187 << BICTCP_HZ) / HZ;
189 if (t < ca->bic_K) /* t - K */
190 offs = ca->bic_K - t;
191 else
192 offs = t - ca->bic_K;
194 /* c/rtt * (t-K)^3 */
195 delta = (cube_rtt_scale * offs * offs * offs) >> (10+3*BICTCP_HZ);
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;
201 /* cubic function - calc bictcp_cnt*/
202 if (bic_target > cwnd) {
203 ca->cnt = cwnd / (bic_target - cwnd);
204 } else {
205 ca->cnt = 100 * cwnd; /* very small increment*/
208 /* TCP Friendly */
209 if (tcp_friendliness) {
210 u32 scale = beta_scale;
211 delta = (cwnd * scale) >> 3;
212 while (ca->ack_cnt > delta) { /* update tcp cwnd */
213 ca->ack_cnt -= delta;
214 ca->tcp_cwnd++;
217 if (ca->tcp_cwnd > cwnd){ /* if bic is slower than tcp */
218 delta = ca->tcp_cwnd - cwnd;
219 max_cnt = cwnd / delta;
220 if (ca->cnt > max_cnt)
221 ca->cnt = max_cnt;
225 ca->cnt = (ca->cnt << ACK_RATIO_SHIFT) / ca->delayed_ack;
226 if (ca->cnt == 0) /* cannot be zero */
227 ca->cnt = 1;
230 static void bictcp_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
232 struct tcp_sock *tp = tcp_sk(sk);
233 struct bictcp *ca = inet_csk_ca(sk);
235 if (!tcp_is_cwnd_limited(sk, in_flight))
236 return;
238 if (tp->snd_cwnd <= tp->snd_ssthresh)
239 tcp_slow_start(tp);
240 else {
241 bictcp_update(ca, tp->snd_cwnd);
243 /* In dangerous area, increase slowly.
244 * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
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++;
256 static u32 bictcp_recalc_ssthresh(struct sock *sk)
258 const struct tcp_sock *tp = tcp_sk(sk);
259 struct bictcp *ca = inet_csk_ca(sk);
261 ca->epoch_start = 0; /* end of epoch */
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;
270 ca->loss_cwnd = tp->snd_cwnd;
272 return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
275 static u32 bictcp_undo_cwnd(struct sock *sk)
277 struct bictcp *ca = inet_csk_ca(sk);
279 return max(tcp_sk(sk)->snd_cwnd, ca->last_max_cwnd);
282 static void bictcp_state(struct sock *sk, u8 new_state)
284 if (new_state == TCP_CA_Loss)
285 bictcp_reset(inet_csk_ca(sk));
288 /* Track delayed acknowledgment ratio using sliding window
289 * ratio = (15*ratio + sample) / 16
291 static void bictcp_acked(struct sock *sk, u32 cnt, s32 rtt_us)
293 const struct inet_connection_sock *icsk = inet_csk(sk);
294 struct bictcp *ca = inet_csk_ca(sk);
295 u32 delay;
297 if (icsk->icsk_ca_state == TCP_CA_Open) {
298 cnt -= ca->delayed_ack >> ACK_RATIO_SHIFT;
299 ca->delayed_ack += cnt;
302 /* Some calls are for duplicates without timetamps */
303 if (rtt_us < 0)
304 return;
306 /* Discard delay samples right after fast recovery */
307 if ((s32)(tcp_time_stamp - ca->epoch_start) < HZ)
308 return;
310 delay = usecs_to_jiffies(rtt_us) << 3;
311 if (delay == 0)
312 delay = 1;
314 /* first time call or link delay decreases */
315 if (ca->delay_min == 0 || ca->delay_min > delay)
316 ca->delay_min = delay;
319 static 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,
325 .pkts_acked = bictcp_acked,
326 .owner = THIS_MODULE,
327 .name = "cubic",
330 static int __init cubictcp_register(void)
332 BUILD_BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
334 /* Precompute a bunch of the scaling factors that are used per-packet
335 * based on SRTT of 100ms
338 beta_scale = 8*(BICTCP_BETA_SCALE+beta)/ 3 / (BICTCP_BETA_SCALE - beta);
340 cube_rtt_scale = (bic_scale * 10); /* 1024*c/rtt */
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
346 * c = bic_scale >> 10
347 * rtt = 100ms
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)
355 /* 1/c * 2^2*bictcp_HZ * srtt */
356 cube_factor = 1ull << (10+3*BICTCP_HZ); /* 2^40 */
358 /* divide by bic_scale and by constant Srtt (100ms) */
359 do_div(cube_factor, bic_scale * 10);
361 return tcp_register_congestion_control(&cubictcp);
364 static void __exit cubictcp_unregister(void)
366 tcp_unregister_congestion_control(&cubictcp);
369 module_init(cubictcp_register);
370 module_exit(cubictcp_unregister);
372 MODULE_AUTHOR("Sangtae Ha, Stephen Hemminger");
373 MODULE_LICENSE("GPL");
374 MODULE_DESCRIPTION("CUBIC TCP");
375 MODULE_VERSION("2.2");