Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/roland/infiniband
[pandora-kernel.git] / net / ipv4 / tcp_vegas.c
1 /*
2  * TCP Vegas congestion control
3  *
4  * This is based on the congestion detection/avoidance scheme described in
5  *    Lawrence S. Brakmo and Larry L. Peterson.
6  *    "TCP Vegas: End to end congestion avoidance on a global internet."
7  *    IEEE Journal on Selected Areas in Communication, 13(8):1465--1480,
8  *    October 1995. Available from:
9  *      ftp://ftp.cs.arizona.edu/xkernel/Papers/jsac.ps
10  *
11  * See http://www.cs.arizona.edu/xkernel/ for their implementation.
12  * The main aspects that distinguish this implementation from the
13  * Arizona Vegas implementation are:
14  *   o We do not change the loss detection or recovery mechanisms of
15  *     Linux in any way. Linux already recovers from losses quite well,
16  *     using fine-grained timers, NewReno, and FACK.
17  *   o To avoid the performance penalty imposed by increasing cwnd
18  *     only every-other RTT during slow start, we increase during
19  *     every RTT during slow start, just like Reno.
20  *   o Largely to allow continuous cwnd growth during slow start,
21  *     we use the rate at which ACKs come back as the "actual"
22  *     rate, rather than the rate at which data is sent.
23  *   o To speed convergence to the right rate, we set the cwnd
24  *     to achieve the right ("actual") rate when we exit slow start.
25  *   o To filter out the noise caused by delayed ACKs, we use the
26  *     minimum RTT sample observed during the last RTT to calculate
27  *     the actual rate.
28  *   o When the sender re-starts from idle, it waits until it has
29  *     received ACKs for an entire flight of new data before making
30  *     a cwnd adjustment decision. The original Vegas implementation
31  *     assumed senders never went idle.
32  */
33
34 #include <linux/mm.h>
35 #include <linux/module.h>
36 #include <linux/skbuff.h>
37 #include <linux/inet_diag.h>
38
39 #include <net/tcp.h>
40
41 #include "tcp_vegas.h"
42
43 /* Default values of the Vegas variables, in fixed-point representation
44  * with V_PARAM_SHIFT bits to the right of the binary point.
45  */
46 #define V_PARAM_SHIFT 1
47 static int alpha = 2<<V_PARAM_SHIFT;
48 static int beta  = 4<<V_PARAM_SHIFT;
49 static int gamma = 1<<V_PARAM_SHIFT;
50
51 module_param(alpha, int, 0644);
52 MODULE_PARM_DESC(alpha, "lower bound of packets in network (scale by 2)");
53 module_param(beta, int, 0644);
54 MODULE_PARM_DESC(beta, "upper bound of packets in network (scale by 2)");
55 module_param(gamma, int, 0644);
56 MODULE_PARM_DESC(gamma, "limit on increase (scale by 2)");
57
58
59 /* There are several situations when we must "re-start" Vegas:
60  *
61  *  o when a connection is established
62  *  o after an RTO
63  *  o after fast recovery
64  *  o when we send a packet and there is no outstanding
65  *    unacknowledged data (restarting an idle connection)
66  *
67  * In these circumstances we cannot do a Vegas calculation at the
68  * end of the first RTT, because any calculation we do is using
69  * stale info -- both the saved cwnd and congestion feedback are
70  * stale.
71  *
72  * Instead we must wait until the completion of an RTT during
73  * which we actually receive ACKs.
74  */
75 static void vegas_enable(struct sock *sk)
76 {
77         const struct tcp_sock *tp = tcp_sk(sk);
78         struct vegas *vegas = inet_csk_ca(sk);
79
80         /* Begin taking Vegas samples next time we send something. */
81         vegas->doing_vegas_now = 1;
82
83         /* Set the beginning of the next send window. */
84         vegas->beg_snd_nxt = tp->snd_nxt;
85
86         vegas->cntRTT = 0;
87         vegas->minRTT = 0x7fffffff;
88 }
89
90 /* Stop taking Vegas samples for now. */
91 static inline void vegas_disable(struct sock *sk)
92 {
93         struct vegas *vegas = inet_csk_ca(sk);
94
95         vegas->doing_vegas_now = 0;
96 }
97
98 void tcp_vegas_init(struct sock *sk)
99 {
100         struct vegas *vegas = inet_csk_ca(sk);
101
102         vegas->baseRTT = 0x7fffffff;
103         vegas_enable(sk);
104 }
105 EXPORT_SYMBOL_GPL(tcp_vegas_init);
106
107 /* Do RTT sampling needed for Vegas.
108  * Basically we:
109  *   o min-filter RTT samples from within an RTT to get the current
110  *     propagation delay + queuing delay (we are min-filtering to try to
111  *     avoid the effects of delayed ACKs)
112  *   o min-filter RTT samples from a much longer window (forever for now)
113  *     to find the propagation delay (baseRTT)
114  */
115 void tcp_vegas_pkts_acked(struct sock *sk, u32 cnt, ktime_t last)
116 {
117         struct vegas *vegas = inet_csk_ca(sk);
118         u32 vrtt;
119
120         /* Never allow zero rtt or baseRTT */
121         vrtt = ktime_to_us(net_timedelta(last)) + 1;
122
123         /* Filter to find propagation delay: */
124         if (vrtt < vegas->baseRTT)
125                 vegas->baseRTT = vrtt;
126
127         /* Find the min RTT during the last RTT to find
128          * the current prop. delay + queuing delay:
129          */
130         vegas->minRTT = min(vegas->minRTT, vrtt);
131         vegas->cntRTT++;
132 }
133 EXPORT_SYMBOL_GPL(tcp_vegas_pkts_acked);
134
135 void tcp_vegas_state(struct sock *sk, u8 ca_state)
136 {
137
138         if (ca_state == TCP_CA_Open)
139                 vegas_enable(sk);
140         else
141                 vegas_disable(sk);
142 }
143 EXPORT_SYMBOL_GPL(tcp_vegas_state);
144
145 /*
146  * If the connection is idle and we are restarting,
147  * then we don't want to do any Vegas calculations
148  * until we get fresh RTT samples.  So when we
149  * restart, we reset our Vegas state to a clean
150  * slate. After we get acks for this flight of
151  * packets, _then_ we can make Vegas calculations
152  * again.
153  */
154 void tcp_vegas_cwnd_event(struct sock *sk, enum tcp_ca_event event)
155 {
156         if (event == CA_EVENT_CWND_RESTART ||
157             event == CA_EVENT_TX_START)
158                 tcp_vegas_init(sk);
159 }
160 EXPORT_SYMBOL_GPL(tcp_vegas_cwnd_event);
161
162 static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack,
163                                  u32 seq_rtt, u32 in_flight, int flag)
164 {
165         struct tcp_sock *tp = tcp_sk(sk);
166         struct vegas *vegas = inet_csk_ca(sk);
167
168         if (!vegas->doing_vegas_now)
169                 return tcp_reno_cong_avoid(sk, ack, seq_rtt, in_flight, flag);
170
171         /* The key players are v_beg_snd_una and v_beg_snd_nxt.
172          *
173          * These are so named because they represent the approximate values
174          * of snd_una and snd_nxt at the beginning of the current RTT. More
175          * precisely, they represent the amount of data sent during the RTT.
176          * At the end of the RTT, when we receive an ACK for v_beg_snd_nxt,
177          * we will calculate that (v_beg_snd_nxt - v_beg_snd_una) outstanding
178          * bytes of data have been ACKed during the course of the RTT, giving
179          * an "actual" rate of:
180          *
181          *     (v_beg_snd_nxt - v_beg_snd_una) / (rtt duration)
182          *
183          * Unfortunately, v_beg_snd_una is not exactly equal to snd_una,
184          * because delayed ACKs can cover more than one segment, so they
185          * don't line up nicely with the boundaries of RTTs.
186          *
187          * Another unfortunate fact of life is that delayed ACKs delay the
188          * advance of the left edge of our send window, so that the number
189          * of bytes we send in an RTT is often less than our cwnd will allow.
190          * So we keep track of our cwnd separately, in v_beg_snd_cwnd.
191          */
192
193         if (after(ack, vegas->beg_snd_nxt)) {
194                 /* Do the Vegas once-per-RTT cwnd adjustment. */
195                 u32 old_wnd, old_snd_cwnd;
196
197
198                 /* Here old_wnd is essentially the window of data that was
199                  * sent during the previous RTT, and has all
200                  * been acknowledged in the course of the RTT that ended
201                  * with the ACK we just received. Likewise, old_snd_cwnd
202                  * is the cwnd during the previous RTT.
203                  */
204                 old_wnd = (vegas->beg_snd_nxt - vegas->beg_snd_una) /
205                         tp->mss_cache;
206                 old_snd_cwnd = vegas->beg_snd_cwnd;
207
208                 /* Save the extent of the current window so we can use this
209                  * at the end of the next RTT.
210                  */
211                 vegas->beg_snd_una  = vegas->beg_snd_nxt;
212                 vegas->beg_snd_nxt  = tp->snd_nxt;
213                 vegas->beg_snd_cwnd = tp->snd_cwnd;
214
215                 /* We do the Vegas calculations only if we got enough RTT
216                  * samples that we can be reasonably sure that we got
217                  * at least one RTT sample that wasn't from a delayed ACK.
218                  * If we only had 2 samples total,
219                  * then that means we're getting only 1 ACK per RTT, which
220                  * means they're almost certainly delayed ACKs.
221                  * If  we have 3 samples, we should be OK.
222                  */
223
224                 if (vegas->cntRTT <= 2) {
225                         /* We don't have enough RTT samples to do the Vegas
226                          * calculation, so we'll behave like Reno.
227                          */
228                         tcp_reno_cong_avoid(sk, ack, seq_rtt, in_flight, flag);
229                 } else {
230                         u32 rtt, target_cwnd, diff;
231
232                         /* We have enough RTT samples, so, using the Vegas
233                          * algorithm, we determine if we should increase or
234                          * decrease cwnd, and by how much.
235                          */
236
237                         /* Pluck out the RTT we are using for the Vegas
238                          * calculations. This is the min RTT seen during the
239                          * last RTT. Taking the min filters out the effects
240                          * of delayed ACKs, at the cost of noticing congestion
241                          * a bit later.
242                          */
243                         rtt = vegas->minRTT;
244
245                         /* Calculate the cwnd we should have, if we weren't
246                          * going too fast.
247                          *
248                          * This is:
249                          *     (actual rate in segments) * baseRTT
250                          * We keep it as a fixed point number with
251                          * V_PARAM_SHIFT bits to the right of the binary point.
252                          */
253                         target_cwnd = ((old_wnd * vegas->baseRTT)
254                                        << V_PARAM_SHIFT) / rtt;
255
256                         /* Calculate the difference between the window we had,
257                          * and the window we would like to have. This quantity
258                          * is the "Diff" from the Arizona Vegas papers.
259                          *
260                          * Again, this is a fixed point number with
261                          * V_PARAM_SHIFT bits to the right of the binary
262                          * point.
263                          */
264                         diff = (old_wnd << V_PARAM_SHIFT) - target_cwnd;
265
266                         if (tp->snd_cwnd <= tp->snd_ssthresh) {
267                                 /* Slow start.  */
268                                 if (diff > gamma) {
269                                         /* Going too fast. Time to slow down
270                                          * and switch to congestion avoidance.
271                                          */
272                                         tp->snd_ssthresh = 2;
273
274                                         /* Set cwnd to match the actual rate
275                                          * exactly:
276                                          *   cwnd = (actual rate) * baseRTT
277                                          * Then we add 1 because the integer
278                                          * truncation robs us of full link
279                                          * utilization.
280                                          */
281                                         tp->snd_cwnd = min(tp->snd_cwnd,
282                                                            (target_cwnd >>
283                                                             V_PARAM_SHIFT)+1);
284
285                                 }
286                                 tcp_slow_start(tp);
287                         } else {
288                                 /* Congestion avoidance. */
289                                 u32 next_snd_cwnd;
290
291                                 /* Figure out where we would like cwnd
292                                  * to be.
293                                  */
294                                 if (diff > beta) {
295                                         /* The old window was too fast, so
296                                          * we slow down.
297                                          */
298                                         next_snd_cwnd = old_snd_cwnd - 1;
299                                 } else if (diff < alpha) {
300                                         /* We don't have enough extra packets
301                                          * in the network, so speed up.
302                                          */
303                                         next_snd_cwnd = old_snd_cwnd + 1;
304                                 } else {
305                                         /* Sending just as fast as we
306                                          * should be.
307                                          */
308                                         next_snd_cwnd = old_snd_cwnd;
309                                 }
310
311                                 /* Adjust cwnd upward or downward, toward the
312                                  * desired value.
313                                  */
314                                 if (next_snd_cwnd > tp->snd_cwnd)
315                                         tp->snd_cwnd++;
316                                 else if (next_snd_cwnd < tp->snd_cwnd)
317                                         tp->snd_cwnd--;
318                         }
319
320                         if (tp->snd_cwnd < 2)
321                                 tp->snd_cwnd = 2;
322                         else if (tp->snd_cwnd > tp->snd_cwnd_clamp)
323                                 tp->snd_cwnd = tp->snd_cwnd_clamp;
324                 }
325
326                 /* Wipe the slate clean for the next RTT. */
327                 vegas->cntRTT = 0;
328                 vegas->minRTT = 0x7fffffff;
329         }
330         /* Use normal slow start */
331         else if (tp->snd_cwnd <= tp->snd_ssthresh)
332                 tcp_slow_start(tp);
333
334 }
335
336 /* Extract info for Tcp socket info provided via netlink. */
337 void tcp_vegas_get_info(struct sock *sk, u32 ext, struct sk_buff *skb)
338 {
339         const struct vegas *ca = inet_csk_ca(sk);
340         if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
341                 struct tcpvegas_info info = {
342                         .tcpv_enabled = ca->doing_vegas_now,
343                         .tcpv_rttcnt = ca->cntRTT,
344                         .tcpv_rtt = ca->baseRTT,
345                         .tcpv_minrtt = ca->minRTT,
346                 };
347
348                 nla_put(skb, INET_DIAG_VEGASINFO, sizeof(info), &info);
349         }
350 }
351 EXPORT_SYMBOL_GPL(tcp_vegas_get_info);
352
353 static struct tcp_congestion_ops tcp_vegas = {
354         .flags          = TCP_CONG_RTT_STAMP,
355         .init           = tcp_vegas_init,
356         .ssthresh       = tcp_reno_ssthresh,
357         .cong_avoid     = tcp_vegas_cong_avoid,
358         .min_cwnd       = tcp_reno_min_cwnd,
359         .pkts_acked     = tcp_vegas_pkts_acked,
360         .set_state      = tcp_vegas_state,
361         .cwnd_event     = tcp_vegas_cwnd_event,
362         .get_info       = tcp_vegas_get_info,
363
364         .owner          = THIS_MODULE,
365         .name           = "vegas",
366 };
367
368 static int __init tcp_vegas_register(void)
369 {
370         BUILD_BUG_ON(sizeof(struct vegas) > ICSK_CA_PRIV_SIZE);
371         tcp_register_congestion_control(&tcp_vegas);
372         return 0;
373 }
374
375 static void __exit tcp_vegas_unregister(void)
376 {
377         tcp_unregister_congestion_control(&tcp_vegas);
378 }
379
380 module_init(tcp_vegas_register);
381 module_exit(tcp_vegas_unregister);
382
383 MODULE_AUTHOR("Stephen Hemminger");
384 MODULE_LICENSE("GPL");
385 MODULE_DESCRIPTION("TCP Vegas");