0e6cdfeb207a15af1f9c51453f35cd8df8cd586a
[powerpc.git] / net / ipv4 / tcp_cubic.c
1 /*
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
15 #include <linux/mm.h>
16 #include <linux/module.h>
17 #include <net/tcp.h>
18 #include <asm/div64.h>
19
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
29 static int fast_convergence __read_mostly = 1;
30 static int max_increment __read_mostly = 16;
31 static int beta __read_mostly = 819;    /* = 819/1024 (BICTCP_BETA_SCALE) */
32 static int initial_ssthresh __read_mostly = 100;
33 static int bic_scale __read_mostly = 41;
34 static int tcp_friendliness __read_mostly = 1;
35
36 static u32 cube_rtt_scale __read_mostly;
37 static u32 beta_scale __read_mostly;
38 static u64 cube_factor __read_mostly;
39
40 /* Note parameters that are used for precomputing scale factors are read-only */
41 module_param(fast_convergence, int, 0644);
42 MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
43 module_param(max_increment, int, 0644);
44 MODULE_PARM_DESC(max_increment, "Limit on increment allowed during binary search");
45 module_param(beta, int, 0444);
46 MODULE_PARM_DESC(beta, "beta for multiplicative increase");
47 module_param(initial_ssthresh, int, 0644);
48 MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
49 module_param(bic_scale, int, 0444);
50 MODULE_PARM_DESC(bic_scale, "scale (scaled by 1024) value for bic function (bic_scale/1024)");
51 module_param(tcp_friendliness, int, 0644);
52 MODULE_PARM_DESC(tcp_friendliness, "turn on/off tcp friendliness");
53
54 /* BIC TCP Parameters */
55 struct 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
71 static 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
87 static 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
94 /*
95  * calculate the cubic root of x using Newton-Raphson
96  */
97 static u32 cubic_root(u64 a)
98 {
99         u32 x;
100
101         /* Initial estimate is based on:
102          * cbrt(x) = exp(log(x) / 3)
103          */
104         x = 1u << (fls64(a)/3);
105
106         /* 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;
110
111         return x;
112 }
113
114 /*
115  * Compute congestion window to use.
116  */
117 static inline void bictcp_update(struct bictcp *ca, u32 cwnd)
118 {
119         u64 offs;
120         u32 delta, t, bic_target, min_cnt, max_cnt;
121
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
131         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 {
140                         /* 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));
145                         ca->bic_origin_point = ca->last_max_cwnd;
146                 }
147         }
148
149         /* cubic function - calc*/
150         /* calculate c * time^3 / rtt,
151          *  while considering overflow in calculation of time^3
152          * (so time^3 is done by using 64 bit)
153          * and without the support of division of 64bit numbers
154          * (so all divisions are done by using 32 bit)
155          *  also NOTE the unit of those veriables
156          *        time  = (t - K) / 2^bictcp_HZ
157          *        c = bic_scale >> 10
158          * rtt  = (srtt >> 3) / HZ
159          * !!! The following code does not have overflow problems,
160          * if the cwnd < 1 million packets !!!
161          */
162
163         /* change the unit from HZ to bictcp_HZ */
164         t = ((tcp_time_stamp + (ca->delay_min>>3) - ca->epoch_start)
165              << BICTCP_HZ) / HZ;
166
167         if (t < ca->bic_K)              /* t - K */
168                 offs = ca->bic_K - t;
169         else
170                 offs = t - ca->bic_K;
171
172         /* c/rtt * (t-K)^3 */
173         delta = (cube_rtt_scale * offs * offs * offs) >> (10+3*BICTCP_HZ);
174         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;
178
179         /* cubic function - calc bictcp_cnt*/
180         if (bic_target > cwnd) {
181                 ca->cnt = cwnd / (bic_target - cwnd);
182         } else {
183                 ca->cnt = 100 * cwnd;              /* very small increment*/
184         }
185
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
193         /* slow start and low utilization  */
194         if (ca->loss_cwnd == 0)         /* could be aggressive in slow start */
195                 ca->cnt = 50;
196
197         /* TCP Friendly */
198         if (tcp_friendliness) {
199                 u32 scale = beta_scale;
200                 delta = (cwnd * scale) >> 3;
201                 while (ca->ack_cnt > delta) {           /* update tcp cwnd */
202                         ca->ack_cnt -= delta;
203                         ca->tcp_cwnd++;
204                 }
205
206                 if (ca->tcp_cwnd > cwnd){       /* if bic is slower than tcp */
207                         delta = ca->tcp_cwnd - cwnd;
208                         max_cnt = cwnd / delta;
209                         if (ca->cnt > max_cnt)
210                                 ca->cnt = max_cnt;
211                 }
212         }
213
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 */
221 static 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
233         delay = (tcp_time_stamp - tp->rx_opt.rcv_tsecr)<<3;
234         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
242 static 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
272 static 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
291 static 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
298 static 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  */
307 static 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
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",
328 };
329
330 static int __init cubictcp_register(void)
331 {
332         BUILD_BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
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
340         cube_rtt_scale = (bic_scale * 10);      /* 1024*c/rtt */
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
361         return tcp_register_congestion_control(&cubictcp);
362 }
363
364 static void __exit cubictcp_unregister(void)
365 {
366         tcp_unregister_congestion_control(&cubictcp);
367 }
368
369 module_init(cubictcp_register);
370 module_exit(cubictcp_unregister);
371
372 MODULE_AUTHOR("Sangtae Ha, Stephen Hemminger");
373 MODULE_LICENSE("GPL");
374 MODULE_DESCRIPTION("CUBIC TCP");
375 MODULE_VERSION("2.0");