6f08adbda54e3e198b86632152bb53b01d8b6cef
[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, x1;
100
101         /* Initial estimate is based on:
102          * cbrt(x) = exp(log(x) / 3)
103          */
104         x = 1u << (fls64(a)/3);
105
106         /*
107          * Iteration based on:
108          *                         2
109          * x    = ( 2 * x  +  a / x  ) / 3
110          *  k+1          k         k
111          */
112         do {
113                 x1 = x;
114                 x = (2 * x + (uint32_t) div64_64(a, x*x)) / 3;
115         } while (abs(x1 - x) > 1);
116
117         return x;
118 }
119
120 /*
121  * Compute congestion window to use.
122  */
123 static inline void bictcp_update(struct bictcp *ca, u32 cwnd)
124 {
125         u64 offs;
126         u32 delta, t, bic_target, min_cnt, max_cnt;
127
128         ca->ack_cnt++;  /* count the number of ACKs */
129
130         if (ca->last_cwnd == cwnd &&
131             (s32)(tcp_time_stamp - ca->last_time) <= HZ / 32)
132                 return;
133
134         ca->last_cwnd = cwnd;
135         ca->last_time = tcp_time_stamp;
136
137         if (ca->epoch_start == 0) {
138                 ca->epoch_start = tcp_time_stamp;       /* record the beginning of an epoch */
139                 ca->ack_cnt = 1;                        /* start counting */
140                 ca->tcp_cwnd = cwnd;                    /* syn with cubic */
141
142                 if (ca->last_max_cwnd <= cwnd) {
143                         ca->bic_K = 0;
144                         ca->bic_origin_point = cwnd;
145                 } else {
146                         /* Compute new K based on
147                          * (wmax-cwnd) * (srtt>>3 / HZ) / c * 2^(3*bictcp_HZ)
148                          */
149                         ca->bic_K = cubic_root(cube_factor
150                                                * (ca->last_max_cwnd - cwnd));
151                         ca->bic_origin_point = ca->last_max_cwnd;
152                 }
153         }
154
155         /* cubic function - calc*/
156         /* calculate c * time^3 / rtt,
157          *  while considering overflow in calculation of time^3
158          * (so time^3 is done by using 64 bit)
159          * and without the support of division of 64bit numbers
160          * (so all divisions are done by using 32 bit)
161          *  also NOTE the unit of those veriables
162          *        time  = (t - K) / 2^bictcp_HZ
163          *        c = bic_scale >> 10
164          * rtt  = (srtt >> 3) / HZ
165          * !!! The following code does not have overflow problems,
166          * if the cwnd < 1 million packets !!!
167          */
168
169         /* change the unit from HZ to bictcp_HZ */
170         t = ((tcp_time_stamp + (ca->delay_min>>3) - ca->epoch_start)
171              << BICTCP_HZ) / HZ;
172
173         if (t < ca->bic_K)              /* t - K */
174                 offs = ca->bic_K - t;
175         else
176                 offs = t - ca->bic_K;
177
178         /* c/rtt * (t-K)^3 */
179         delta = (cube_rtt_scale * offs * offs * offs) >> (10+3*BICTCP_HZ);
180         if (t < ca->bic_K)                                      /* below origin*/
181                 bic_target = ca->bic_origin_point - delta;
182         else                                                    /* above origin*/
183                 bic_target = ca->bic_origin_point + delta;
184
185         /* cubic function - calc bictcp_cnt*/
186         if (bic_target > cwnd) {
187                 ca->cnt = cwnd / (bic_target - cwnd);
188         } else {
189                 ca->cnt = 100 * cwnd;              /* very small increment*/
190         }
191
192         if (ca->delay_min > 0) {
193                 /* max increment = Smax * rtt / 0.1  */
194                 min_cnt = (cwnd * HZ * 8)/(10 * max_increment * ca->delay_min);
195                 if (ca->cnt < min_cnt)
196                         ca->cnt = min_cnt;
197         }
198
199         /* slow start and low utilization  */
200         if (ca->loss_cwnd == 0)         /* could be aggressive in slow start */
201                 ca->cnt = 50;
202
203         /* TCP Friendly */
204         if (tcp_friendliness) {
205                 u32 scale = beta_scale;
206                 delta = (cwnd * scale) >> 3;
207                 while (ca->ack_cnt > delta) {           /* update tcp cwnd */
208                         ca->ack_cnt -= delta;
209                         ca->tcp_cwnd++;
210                 }
211
212                 if (ca->tcp_cwnd > cwnd){       /* if bic is slower than tcp */
213                         delta = ca->tcp_cwnd - cwnd;
214                         max_cnt = cwnd / delta;
215                         if (ca->cnt > max_cnt)
216                                 ca->cnt = max_cnt;
217                 }
218         }
219
220         ca->cnt = (ca->cnt << ACK_RATIO_SHIFT) / ca->delayed_ack;
221         if (ca->cnt == 0)                       /* cannot be zero */
222                 ca->cnt = 1;
223 }
224
225
226 /* Keep track of minimum rtt */
227 static inline void measure_delay(struct sock *sk)
228 {
229         const struct tcp_sock *tp = tcp_sk(sk);
230         struct bictcp *ca = inet_csk_ca(sk);
231         u32 delay;
232
233         /* No time stamp */
234         if (!(tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) ||
235              /* Discard delay samples right after fast recovery */
236             (s32)(tcp_time_stamp - ca->epoch_start) < HZ)
237                 return;
238
239         delay = (tcp_time_stamp - tp->rx_opt.rcv_tsecr)<<3;
240         if (delay == 0)
241                 delay = 1;
242
243         /* first time call or link delay decreases */
244         if (ca->delay_min == 0 || ca->delay_min > delay)
245                 ca->delay_min = delay;
246 }
247
248 static void bictcp_cong_avoid(struct sock *sk, u32 ack,
249                               u32 seq_rtt, u32 in_flight, int data_acked)
250 {
251         struct tcp_sock *tp = tcp_sk(sk);
252         struct bictcp *ca = inet_csk_ca(sk);
253
254         if (data_acked)
255                 measure_delay(sk);
256
257         if (!tcp_is_cwnd_limited(sk, in_flight))
258                 return;
259
260         if (tp->snd_cwnd <= tp->snd_ssthresh)
261                 tcp_slow_start(tp);
262         else {
263                 bictcp_update(ca, tp->snd_cwnd);
264
265                 /* In dangerous area, increase slowly.
266                  * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
267                  */
268                 if (tp->snd_cwnd_cnt >= ca->cnt) {
269                         if (tp->snd_cwnd < tp->snd_cwnd_clamp)
270                                 tp->snd_cwnd++;
271                         tp->snd_cwnd_cnt = 0;
272                 } else
273                         tp->snd_cwnd_cnt++;
274         }
275
276 }
277
278 static u32 bictcp_recalc_ssthresh(struct sock *sk)
279 {
280         const struct tcp_sock *tp = tcp_sk(sk);
281         struct bictcp *ca = inet_csk_ca(sk);
282
283         ca->epoch_start = 0;    /* end of epoch */
284
285         /* Wmax and fast convergence */
286         if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence)
287                 ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta))
288                         / (2 * BICTCP_BETA_SCALE);
289         else
290                 ca->last_max_cwnd = tp->snd_cwnd;
291
292         ca->loss_cwnd = tp->snd_cwnd;
293
294         return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
295 }
296
297 static u32 bictcp_undo_cwnd(struct sock *sk)
298 {
299         struct bictcp *ca = inet_csk_ca(sk);
300
301         return max(tcp_sk(sk)->snd_cwnd, ca->last_max_cwnd);
302 }
303
304 static void bictcp_state(struct sock *sk, u8 new_state)
305 {
306         if (new_state == TCP_CA_Loss)
307                 bictcp_reset(inet_csk_ca(sk));
308 }
309
310 /* Track delayed acknowledgment ratio using sliding window
311  * ratio = (15*ratio + sample) / 16
312  */
313 static void bictcp_acked(struct sock *sk, u32 cnt)
314 {
315         const struct inet_connection_sock *icsk = inet_csk(sk);
316
317         if (cnt > 0 && icsk->icsk_ca_state == TCP_CA_Open) {
318                 struct bictcp *ca = inet_csk_ca(sk);
319                 cnt -= ca->delayed_ack >> ACK_RATIO_SHIFT;
320                 ca->delayed_ack += cnt;
321         }
322 }
323
324
325 static struct tcp_congestion_ops cubictcp = {
326         .init           = bictcp_init,
327         .ssthresh       = bictcp_recalc_ssthresh,
328         .cong_avoid     = bictcp_cong_avoid,
329         .set_state      = bictcp_state,
330         .undo_cwnd      = bictcp_undo_cwnd,
331         .pkts_acked     = bictcp_acked,
332         .owner          = THIS_MODULE,
333         .name           = "cubic",
334 };
335
336 static int __init cubictcp_register(void)
337 {
338         BUILD_BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
339
340         /* Precompute a bunch of the scaling factors that are used per-packet
341          * based on SRTT of 100ms
342          */
343
344         beta_scale = 8*(BICTCP_BETA_SCALE+beta)/ 3 / (BICTCP_BETA_SCALE - beta);
345
346         cube_rtt_scale = (bic_scale * 10);      /* 1024*c/rtt */
347
348         /* calculate the "K" for (wmax-cwnd) = c/rtt * K^3
349          *  so K = cubic_root( (wmax-cwnd)*rtt/c )
350          * the unit of K is bictcp_HZ=2^10, not HZ
351          *
352          *  c = bic_scale >> 10
353          *  rtt = 100ms
354          *
355          * the following code has been designed and tested for
356          * cwnd < 1 million packets
357          * RTT < 100 seconds
358          * HZ < 1,000,00  (corresponding to 10 nano-second)
359          */
360
361         /* 1/c * 2^2*bictcp_HZ * srtt */
362         cube_factor = 1ull << (10+3*BICTCP_HZ); /* 2^40 */
363
364         /* divide by bic_scale and by constant Srtt (100ms) */
365         do_div(cube_factor, bic_scale * 10);
366
367         return tcp_register_congestion_control(&cubictcp);
368 }
369
370 static void __exit cubictcp_unregister(void)
371 {
372         tcp_unregister_congestion_control(&cubictcp);
373 }
374
375 module_init(cubictcp_register);
376 module_exit(cubictcp_unregister);
377
378 MODULE_AUTHOR("Sangtae Ha, Stephen Hemminger");
379 MODULE_LICENSE("GPL");
380 MODULE_DESCRIPTION("CUBIC TCP");
381 MODULE_VERSION("2.0");