blob: f4104eeb5f2679294bff36dbb2db954cf896f193 [file] [log] [blame]
Stephen Hemmingerb87d8562005-06-23 12:27:19 -07001/*
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
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070034#include <linux/mm.h>
35#include <linux/module.h>
36#include <linux/skbuff.h>
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -030037#include <linux/inet_diag.h>
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070038
39#include <net/tcp.h>
40
41/* Default values of the Vegas variables, in fixed-point representation
42 * with V_PARAM_SHIFT bits to the right of the binary point.
43 */
44#define V_PARAM_SHIFT 1
David S. Millerd2e4bdc2006-11-28 14:37:38 -080045static int alpha = 2<<V_PARAM_SHIFT;
46static int beta = 4<<V_PARAM_SHIFT;
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070047static int gamma = 1<<V_PARAM_SHIFT;
48
49module_param(alpha, int, 0644);
50MODULE_PARM_DESC(alpha, "lower bound of packets in network (scale by 2)");
51module_param(beta, int, 0644);
52MODULE_PARM_DESC(beta, "upper bound of packets in network (scale by 2)");
53module_param(gamma, int, 0644);
54MODULE_PARM_DESC(gamma, "limit on increase (scale by 2)");
55
56
57/* Vegas variables */
58struct vegas {
59 u32 beg_snd_nxt; /* right edge during last RTT */
60 u32 beg_snd_una; /* left edge during last RTT */
61 u32 beg_snd_cwnd; /* saves the size of the cwnd */
62 u8 doing_vegas_now;/* if true, do vegas for this RTT */
63 u16 cntRTT; /* # of RTTs measured within last RTT */
64 u32 minRTT; /* min of RTTs measured within last RTT (in usec) */
65 u32 baseRTT; /* the min of all Vegas RTT measurements seen (in usec) */
66};
67
68/* There are several situations when we must "re-start" Vegas:
69 *
70 * o when a connection is established
71 * o after an RTO
72 * o after fast recovery
73 * o when we send a packet and there is no outstanding
74 * unacknowledged data (restarting an idle connection)
75 *
76 * In these circumstances we cannot do a Vegas calculation at the
77 * end of the first RTT, because any calculation we do is using
78 * stale info -- both the saved cwnd and congestion feedback are
79 * stale.
80 *
81 * Instead we must wait until the completion of an RTT during
82 * which we actually receive ACKs.
83 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030084static inline void vegas_enable(struct sock *sk)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070085{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030086 const struct tcp_sock *tp = tcp_sk(sk);
87 struct vegas *vegas = inet_csk_ca(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070088
89 /* Begin taking Vegas samples next time we send something. */
90 vegas->doing_vegas_now = 1;
91
92 /* Set the beginning of the next send window. */
93 vegas->beg_snd_nxt = tp->snd_nxt;
94
95 vegas->cntRTT = 0;
96 vegas->minRTT = 0x7fffffff;
97}
98
99/* Stop taking Vegas samples for now. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300100static inline void vegas_disable(struct sock *sk)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700101{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300102 struct vegas *vegas = inet_csk_ca(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700103
104 vegas->doing_vegas_now = 0;
105}
106
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300107static void tcp_vegas_init(struct sock *sk)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700108{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300109 struct vegas *vegas = inet_csk_ca(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700110
111 vegas->baseRTT = 0x7fffffff;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300112 vegas_enable(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700113}
114
115/* Do RTT sampling needed for Vegas.
116 * Basically we:
117 * o min-filter RTT samples from within an RTT to get the current
118 * propagation delay + queuing delay (we are min-filtering to try to
119 * avoid the effects of delayed ACKs)
120 * o min-filter RTT samples from a much longer window (forever for now)
121 * to find the propagation delay (baseRTT)
122 */
Stephen Hemminger164891a2007-04-23 22:26:16 -0700123static void tcp_vegas_pkts_acked(struct sock *sk, u32 cnt, ktime_t last)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700124{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300125 struct vegas *vegas = inet_csk_ca(sk);
Stephen Hemminger164891a2007-04-23 22:26:16 -0700126 u32 vrtt;
127
128 /* Never allow zero rtt or baseRTT */
129 vrtt = (ktime_to_ns(net_timedelta(last)) / NSEC_PER_USEC) + 1;
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700130
131 /* Filter to find propagation delay: */
132 if (vrtt < vegas->baseRTT)
133 vegas->baseRTT = vrtt;
134
135 /* Find the min RTT during the last RTT to find
136 * the current prop. delay + queuing delay:
137 */
138 vegas->minRTT = min(vegas->minRTT, vrtt);
139 vegas->cntRTT++;
140}
141
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300142static void tcp_vegas_state(struct sock *sk, u8 ca_state)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700143{
144
145 if (ca_state == TCP_CA_Open)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300146 vegas_enable(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700147 else
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300148 vegas_disable(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700149}
150
151/*
152 * If the connection is idle and we are restarting,
153 * then we don't want to do any Vegas calculations
154 * until we get fresh RTT samples. So when we
155 * restart, we reset our Vegas state to a clean
156 * slate. After we get acks for this flight of
157 * packets, _then_ we can make Vegas calculations
158 * again.
159 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300160static void tcp_vegas_cwnd_event(struct sock *sk, enum tcp_ca_event event)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700161{
162 if (event == CA_EVENT_CWND_RESTART ||
163 event == CA_EVENT_TX_START)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300164 tcp_vegas_init(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700165}
166
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300167static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack,
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700168 u32 seq_rtt, u32 in_flight, int flag)
169{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300170 struct tcp_sock *tp = tcp_sk(sk);
171 struct vegas *vegas = inet_csk_ca(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700172
173 if (!vegas->doing_vegas_now)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300174 return tcp_reno_cong_avoid(sk, ack, seq_rtt, in_flight, flag);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700175
176 /* The key players are v_beg_snd_una and v_beg_snd_nxt.
177 *
178 * These are so named because they represent the approximate values
179 * of snd_una and snd_nxt at the beginning of the current RTT. More
180 * precisely, they represent the amount of data sent during the RTT.
181 * At the end of the RTT, when we receive an ACK for v_beg_snd_nxt,
182 * we will calculate that (v_beg_snd_nxt - v_beg_snd_una) outstanding
183 * bytes of data have been ACKed during the course of the RTT, giving
184 * an "actual" rate of:
185 *
186 * (v_beg_snd_nxt - v_beg_snd_una) / (rtt duration)
187 *
188 * Unfortunately, v_beg_snd_una is not exactly equal to snd_una,
189 * because delayed ACKs can cover more than one segment, so they
190 * don't line up nicely with the boundaries of RTTs.
191 *
192 * Another unfortunate fact of life is that delayed ACKs delay the
193 * advance of the left edge of our send window, so that the number
194 * of bytes we send in an RTT is often less than our cwnd will allow.
195 * So we keep track of our cwnd separately, in v_beg_snd_cwnd.
196 */
197
198 if (after(ack, vegas->beg_snd_nxt)) {
199 /* Do the Vegas once-per-RTT cwnd adjustment. */
200 u32 old_wnd, old_snd_cwnd;
201
202
203 /* Here old_wnd is essentially the window of data that was
204 * sent during the previous RTT, and has all
205 * been acknowledged in the course of the RTT that ended
206 * with the ACK we just received. Likewise, old_snd_cwnd
207 * is the cwnd during the previous RTT.
208 */
209 old_wnd = (vegas->beg_snd_nxt - vegas->beg_snd_una) /
210 tp->mss_cache;
211 old_snd_cwnd = vegas->beg_snd_cwnd;
212
213 /* Save the extent of the current window so we can use this
214 * at the end of the next RTT.
215 */
216 vegas->beg_snd_una = vegas->beg_snd_nxt;
217 vegas->beg_snd_nxt = tp->snd_nxt;
218 vegas->beg_snd_cwnd = tp->snd_cwnd;
219
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700220 /* We do the Vegas calculations only if we got enough RTT
221 * samples that we can be reasonably sure that we got
222 * at least one RTT sample that wasn't from a delayed ACK.
223 * If we only had 2 samples total,
224 * then that means we're getting only 1 ACK per RTT, which
225 * means they're almost certainly delayed ACKs.
226 * If we have 3 samples, we should be OK.
227 */
228
229 if (vegas->cntRTT <= 2) {
230 /* We don't have enough RTT samples to do the Vegas
231 * calculation, so we'll behave like Reno.
232 */
Jeff Garzikc0509702005-11-11 04:43:47 -0500233 tcp_reno_cong_avoid(sk, ack, seq_rtt, in_flight, flag);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700234 } else {
235 u32 rtt, target_cwnd, diff;
236
237 /* We have enough RTT samples, so, using the Vegas
238 * algorithm, we determine if we should increase or
239 * decrease cwnd, and by how much.
240 */
241
242 /* Pluck out the RTT we are using for the Vegas
243 * calculations. This is the min RTT seen during the
244 * last RTT. Taking the min filters out the effects
245 * of delayed ACKs, at the cost of noticing congestion
246 * a bit later.
247 */
248 rtt = vegas->minRTT;
249
250 /* Calculate the cwnd we should have, if we weren't
251 * going too fast.
252 *
253 * This is:
254 * (actual rate in segments) * baseRTT
255 * We keep it as a fixed point number with
256 * V_PARAM_SHIFT bits to the right of the binary point.
257 */
258 target_cwnd = ((old_wnd * vegas->baseRTT)
259 << V_PARAM_SHIFT) / rtt;
260
261 /* Calculate the difference between the window we had,
262 * and the window we would like to have. This quantity
263 * is the "Diff" from the Arizona Vegas papers.
264 *
265 * Again, this is a fixed point number with
266 * V_PARAM_SHIFT bits to the right of the binary
267 * point.
268 */
269 diff = (old_wnd << V_PARAM_SHIFT) - target_cwnd;
270
Stephen Hemminger7faffa12005-11-10 17:07:24 -0800271 if (tp->snd_cwnd <= tp->snd_ssthresh) {
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700272 /* Slow start. */
273 if (diff > gamma) {
274 /* Going too fast. Time to slow down
275 * and switch to congestion avoidance.
276 */
277 tp->snd_ssthresh = 2;
278
279 /* Set cwnd to match the actual rate
280 * exactly:
281 * cwnd = (actual rate) * baseRTT
282 * Then we add 1 because the integer
283 * truncation robs us of full link
284 * utilization.
285 */
286 tp->snd_cwnd = min(tp->snd_cwnd,
287 (target_cwnd >>
288 V_PARAM_SHIFT)+1);
289
290 }
Stephen Hemminger7faffa12005-11-10 17:07:24 -0800291 tcp_slow_start(tp);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700292 } else {
293 /* Congestion avoidance. */
294 u32 next_snd_cwnd;
295
296 /* Figure out where we would like cwnd
297 * to be.
298 */
299 if (diff > beta) {
300 /* The old window was too fast, so
301 * we slow down.
302 */
303 next_snd_cwnd = old_snd_cwnd - 1;
304 } else if (diff < alpha) {
305 /* We don't have enough extra packets
306 * in the network, so speed up.
307 */
308 next_snd_cwnd = old_snd_cwnd + 1;
309 } else {
310 /* Sending just as fast as we
311 * should be.
312 */
313 next_snd_cwnd = old_snd_cwnd;
314 }
315
316 /* Adjust cwnd upward or downward, toward the
317 * desired value.
318 */
319 if (next_snd_cwnd > tp->snd_cwnd)
320 tp->snd_cwnd++;
321 else if (next_snd_cwnd < tp->snd_cwnd)
322 tp->snd_cwnd--;
323 }
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700324
Stephen Hemminger7faffa12005-11-10 17:07:24 -0800325 if (tp->snd_cwnd < 2)
326 tp->snd_cwnd = 2;
327 else if (tp->snd_cwnd > tp->snd_cwnd_clamp)
328 tp->snd_cwnd = tp->snd_cwnd_clamp;
329 }
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700330
Thomas Young5b495612005-12-06 16:16:34 -0800331 /* Wipe the slate clean for the next RTT. */
332 vegas->cntRTT = 0;
333 vegas->minRTT = 0x7fffffff;
334 }
Thomas Young74cb8792006-01-04 13:59:32 -0800335 /* Use normal slow start */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900336 else if (tp->snd_cwnd <= tp->snd_ssthresh)
Thomas Young74cb8792006-01-04 13:59:32 -0800337 tcp_slow_start(tp);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900338
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700339}
340
341/* Extract info for Tcp socket info provided via netlink. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300342static void tcp_vegas_get_info(struct sock *sk, u32 ext,
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700343 struct sk_buff *skb)
344{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300345 const struct vegas *ca = inet_csk_ca(sk);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300346 if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
Thomas Grafe9195d62007-03-22 23:27:01 -0700347 struct tcpvegas_info info = {
348 .tcpv_enabled = ca->doing_vegas_now,
349 .tcpv_rttcnt = ca->cntRTT,
350 .tcpv_rtt = ca->baseRTT,
351 .tcpv_minrtt = ca->minRTT,
352 };
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700353
Thomas Grafe9195d62007-03-22 23:27:01 -0700354 nla_put(skb, INET_DIAG_VEGASINFO, sizeof(info), &info);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700355 }
356}
357
358static struct tcp_congestion_ops tcp_vegas = {
Stephen Hemminger164891a2007-04-23 22:26:16 -0700359 .flags = TCP_CONG_RTT_STAMP,
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700360 .init = tcp_vegas_init,
361 .ssthresh = tcp_reno_ssthresh,
362 .cong_avoid = tcp_vegas_cong_avoid,
363 .min_cwnd = tcp_reno_min_cwnd,
Stephen Hemminger164891a2007-04-23 22:26:16 -0700364 .pkts_acked = tcp_vegas_pkts_acked,
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700365 .set_state = tcp_vegas_state,
366 .cwnd_event = tcp_vegas_cwnd_event,
367 .get_info = tcp_vegas_get_info,
368
369 .owner = THIS_MODULE,
370 .name = "vegas",
371};
372
373static int __init tcp_vegas_register(void)
374{
Alexey Dobriyan74975d42006-08-25 17:10:33 -0700375 BUILD_BUG_ON(sizeof(struct vegas) > ICSK_CA_PRIV_SIZE);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700376 tcp_register_congestion_control(&tcp_vegas);
377 return 0;
378}
379
380static void __exit tcp_vegas_unregister(void)
381{
382 tcp_unregister_congestion_control(&tcp_vegas);
383}
384
385module_init(tcp_vegas_register);
386module_exit(tcp_vegas_unregister);
387
388MODULE_AUTHOR("Stephen Hemminger");
389MODULE_LICENSE("GPL");
390MODULE_DESCRIPTION("TCP Vegas");