blob: b7d296a8ac6d2366e2470607d579ef1bac5cc3ab [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
34#include <linux/config.h>
35#include <linux/mm.h>
36#include <linux/module.h>
37#include <linux/skbuff.h>
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -030038#include <linux/inet_diag.h>
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070039
40#include <net/tcp.h>
41
42/* Default values of the Vegas variables, in fixed-point representation
43 * with V_PARAM_SHIFT bits to the right of the binary point.
44 */
45#define V_PARAM_SHIFT 1
46static int alpha = 1<<V_PARAM_SHIFT;
47static int beta = 3<<V_PARAM_SHIFT;
48static int gamma = 1<<V_PARAM_SHIFT;
49
50module_param(alpha, int, 0644);
51MODULE_PARM_DESC(alpha, "lower bound of packets in network (scale by 2)");
52module_param(beta, int, 0644);
53MODULE_PARM_DESC(beta, "upper bound of packets in network (scale by 2)");
54module_param(gamma, int, 0644);
55MODULE_PARM_DESC(gamma, "limit on increase (scale by 2)");
56
57
58/* Vegas variables */
59struct vegas {
60 u32 beg_snd_nxt; /* right edge during last RTT */
61 u32 beg_snd_una; /* left edge during last RTT */
62 u32 beg_snd_cwnd; /* saves the size of the cwnd */
63 u8 doing_vegas_now;/* if true, do vegas for this RTT */
64 u16 cntRTT; /* # of RTTs measured within last RTT */
65 u32 minRTT; /* min of RTTs measured within last RTT (in usec) */
66 u32 baseRTT; /* the min of all Vegas RTT measurements seen (in usec) */
67};
68
69/* There are several situations when we must "re-start" Vegas:
70 *
71 * o when a connection is established
72 * o after an RTO
73 * o after fast recovery
74 * o when we send a packet and there is no outstanding
75 * unacknowledged data (restarting an idle connection)
76 *
77 * In these circumstances we cannot do a Vegas calculation at the
78 * end of the first RTT, because any calculation we do is using
79 * stale info -- both the saved cwnd and congestion feedback are
80 * stale.
81 *
82 * Instead we must wait until the completion of an RTT during
83 * which we actually receive ACKs.
84 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030085static inline void vegas_enable(struct sock *sk)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070086{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030087 const struct tcp_sock *tp = tcp_sk(sk);
88 struct vegas *vegas = inet_csk_ca(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070089
90 /* Begin taking Vegas samples next time we send something. */
91 vegas->doing_vegas_now = 1;
92
93 /* Set the beginning of the next send window. */
94 vegas->beg_snd_nxt = tp->snd_nxt;
95
96 vegas->cntRTT = 0;
97 vegas->minRTT = 0x7fffffff;
98}
99
100/* Stop taking Vegas samples for now. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300101static inline void vegas_disable(struct sock *sk)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700102{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300103 struct vegas *vegas = inet_csk_ca(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700104
105 vegas->doing_vegas_now = 0;
106}
107
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300108static void tcp_vegas_init(struct sock *sk)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700109{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300110 struct vegas *vegas = inet_csk_ca(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700111
112 vegas->baseRTT = 0x7fffffff;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300113 vegas_enable(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700114}
115
116/* Do RTT sampling needed for Vegas.
117 * Basically we:
118 * o min-filter RTT samples from within an RTT to get the current
119 * propagation delay + queuing delay (we are min-filtering to try to
120 * avoid the effects of delayed ACKs)
121 * o min-filter RTT samples from a much longer window (forever for now)
122 * to find the propagation delay (baseRTT)
123 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300124static void tcp_vegas_rtt_calc(struct sock *sk, u32 usrtt)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700125{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300126 struct vegas *vegas = inet_csk_ca(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700127 u32 vrtt = usrtt + 1; /* Never allow zero rtt or baseRTT */
128
129 /* Filter to find propagation delay: */
130 if (vrtt < vegas->baseRTT)
131 vegas->baseRTT = vrtt;
132
133 /* Find the min RTT during the last RTT to find
134 * the current prop. delay + queuing delay:
135 */
136 vegas->minRTT = min(vegas->minRTT, vrtt);
137 vegas->cntRTT++;
138}
139
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300140static void tcp_vegas_state(struct sock *sk, u8 ca_state)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700141{
142
143 if (ca_state == TCP_CA_Open)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300144 vegas_enable(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700145 else
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300146 vegas_disable(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700147}
148
149/*
150 * If the connection is idle and we are restarting,
151 * then we don't want to do any Vegas calculations
152 * until we get fresh RTT samples. So when we
153 * restart, we reset our Vegas state to a clean
154 * slate. After we get acks for this flight of
155 * packets, _then_ we can make Vegas calculations
156 * again.
157 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300158static void tcp_vegas_cwnd_event(struct sock *sk, enum tcp_ca_event event)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700159{
160 if (event == CA_EVENT_CWND_RESTART ||
161 event == CA_EVENT_TX_START)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300162 tcp_vegas_init(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700163}
164
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300165static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack,
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700166 u32 seq_rtt, u32 in_flight, int flag)
167{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300168 struct tcp_sock *tp = tcp_sk(sk);
169 struct vegas *vegas = inet_csk_ca(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700170
171 if (!vegas->doing_vegas_now)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300172 return tcp_reno_cong_avoid(sk, ack, seq_rtt, in_flight, flag);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700173
174 /* The key players are v_beg_snd_una and v_beg_snd_nxt.
175 *
176 * These are so named because they represent the approximate values
177 * of snd_una and snd_nxt at the beginning of the current RTT. More
178 * precisely, they represent the amount of data sent during the RTT.
179 * At the end of the RTT, when we receive an ACK for v_beg_snd_nxt,
180 * we will calculate that (v_beg_snd_nxt - v_beg_snd_una) outstanding
181 * bytes of data have been ACKed during the course of the RTT, giving
182 * an "actual" rate of:
183 *
184 * (v_beg_snd_nxt - v_beg_snd_una) / (rtt duration)
185 *
186 * Unfortunately, v_beg_snd_una is not exactly equal to snd_una,
187 * because delayed ACKs can cover more than one segment, so they
188 * don't line up nicely with the boundaries of RTTs.
189 *
190 * Another unfortunate fact of life is that delayed ACKs delay the
191 * advance of the left edge of our send window, so that the number
192 * of bytes we send in an RTT is often less than our cwnd will allow.
193 * So we keep track of our cwnd separately, in v_beg_snd_cwnd.
194 */
195
196 if (after(ack, vegas->beg_snd_nxt)) {
197 /* Do the Vegas once-per-RTT cwnd adjustment. */
198 u32 old_wnd, old_snd_cwnd;
199
200
201 /* Here old_wnd is essentially the window of data that was
202 * sent during the previous RTT, and has all
203 * been acknowledged in the course of the RTT that ended
204 * with the ACK we just received. Likewise, old_snd_cwnd
205 * is the cwnd during the previous RTT.
206 */
207 old_wnd = (vegas->beg_snd_nxt - vegas->beg_snd_una) /
208 tp->mss_cache;
209 old_snd_cwnd = vegas->beg_snd_cwnd;
210
211 /* Save the extent of the current window so we can use this
212 * at the end of the next RTT.
213 */
214 vegas->beg_snd_una = vegas->beg_snd_nxt;
215 vegas->beg_snd_nxt = tp->snd_nxt;
216 vegas->beg_snd_cwnd = tp->snd_cwnd;
217
218 /* Take into account the current RTT sample too, to
219 * decrease the impact of delayed acks. This double counts
220 * this sample since we count it for the next window as well,
221 * but that's not too awful, since we're taking the min,
222 * rather than averaging.
223 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300224 tcp_vegas_rtt_calc(sk, seq_rtt * 1000);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700225
226 /* We do the Vegas calculations only if we got enough RTT
227 * samples that we can be reasonably sure that we got
228 * at least one RTT sample that wasn't from a delayed ACK.
229 * If we only had 2 samples total,
230 * then that means we're getting only 1 ACK per RTT, which
231 * means they're almost certainly delayed ACKs.
232 * If we have 3 samples, we should be OK.
233 */
234
235 if (vegas->cntRTT <= 2) {
236 /* We don't have enough RTT samples to do the Vegas
237 * calculation, so we'll behave like Reno.
238 */
Jeff Garzikc0509702005-11-11 04:43:47 -0500239 tcp_reno_cong_avoid(sk, ack, seq_rtt, in_flight, flag);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700240 } else {
241 u32 rtt, target_cwnd, diff;
242
243 /* We have enough RTT samples, so, using the Vegas
244 * algorithm, we determine if we should increase or
245 * decrease cwnd, and by how much.
246 */
247
248 /* Pluck out the RTT we are using for the Vegas
249 * calculations. This is the min RTT seen during the
250 * last RTT. Taking the min filters out the effects
251 * of delayed ACKs, at the cost of noticing congestion
252 * a bit later.
253 */
254 rtt = vegas->minRTT;
255
256 /* Calculate the cwnd we should have, if we weren't
257 * going too fast.
258 *
259 * This is:
260 * (actual rate in segments) * baseRTT
261 * We keep it as a fixed point number with
262 * V_PARAM_SHIFT bits to the right of the binary point.
263 */
264 target_cwnd = ((old_wnd * vegas->baseRTT)
265 << V_PARAM_SHIFT) / rtt;
266
267 /* Calculate the difference between the window we had,
268 * and the window we would like to have. This quantity
269 * is the "Diff" from the Arizona Vegas papers.
270 *
271 * Again, this is a fixed point number with
272 * V_PARAM_SHIFT bits to the right of the binary
273 * point.
274 */
275 diff = (old_wnd << V_PARAM_SHIFT) - target_cwnd;
276
Stephen Hemminger7faffa12005-11-10 17:07:24 -0800277 if (tp->snd_cwnd <= tp->snd_ssthresh) {
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700278 /* Slow start. */
279 if (diff > gamma) {
280 /* Going too fast. Time to slow down
281 * and switch to congestion avoidance.
282 */
283 tp->snd_ssthresh = 2;
284
285 /* Set cwnd to match the actual rate
286 * exactly:
287 * cwnd = (actual rate) * baseRTT
288 * Then we add 1 because the integer
289 * truncation robs us of full link
290 * utilization.
291 */
292 tp->snd_cwnd = min(tp->snd_cwnd,
293 (target_cwnd >>
294 V_PARAM_SHIFT)+1);
295
296 }
Stephen Hemminger7faffa12005-11-10 17:07:24 -0800297 tcp_slow_start(tp);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700298 } else {
299 /* Congestion avoidance. */
300 u32 next_snd_cwnd;
301
302 /* Figure out where we would like cwnd
303 * to be.
304 */
305 if (diff > beta) {
306 /* The old window was too fast, so
307 * we slow down.
308 */
309 next_snd_cwnd = old_snd_cwnd - 1;
310 } else if (diff < alpha) {
311 /* We don't have enough extra packets
312 * in the network, so speed up.
313 */
314 next_snd_cwnd = old_snd_cwnd + 1;
315 } else {
316 /* Sending just as fast as we
317 * should be.
318 */
319 next_snd_cwnd = old_snd_cwnd;
320 }
321
322 /* Adjust cwnd upward or downward, toward the
323 * desired value.
324 */
325 if (next_snd_cwnd > tp->snd_cwnd)
326 tp->snd_cwnd++;
327 else if (next_snd_cwnd < tp->snd_cwnd)
328 tp->snd_cwnd--;
329 }
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700330
Stephen Hemminger7faffa12005-11-10 17:07:24 -0800331 if (tp->snd_cwnd < 2)
332 tp->snd_cwnd = 2;
333 else if (tp->snd_cwnd > tp->snd_cwnd_clamp)
334 tp->snd_cwnd = tp->snd_cwnd_clamp;
335 }
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700336 }
337
Stephen Hemminger7faffa12005-11-10 17:07:24 -0800338 /* Wipe the slate clean for the next RTT. */
339 vegas->cntRTT = 0;
340 vegas->minRTT = 0x7fffffff;
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700341}
342
343/* Extract info for Tcp socket info provided via netlink. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300344static void tcp_vegas_get_info(struct sock *sk, u32 ext,
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700345 struct sk_buff *skb)
346{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300347 const struct vegas *ca = inet_csk_ca(sk);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300348 if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700349 struct tcpvegas_info *info;
350
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300351 info = RTA_DATA(__RTA_PUT(skb, INET_DIAG_VEGASINFO,
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700352 sizeof(*info)));
353
354 info->tcpv_enabled = ca->doing_vegas_now;
355 info->tcpv_rttcnt = ca->cntRTT;
356 info->tcpv_rtt = ca->baseRTT;
357 info->tcpv_minrtt = ca->minRTT;
358 rtattr_failure: ;
359 }
360}
361
362static struct tcp_congestion_ops tcp_vegas = {
363 .init = tcp_vegas_init,
364 .ssthresh = tcp_reno_ssthresh,
365 .cong_avoid = tcp_vegas_cong_avoid,
366 .min_cwnd = tcp_reno_min_cwnd,
367 .rtt_sample = tcp_vegas_rtt_calc,
368 .set_state = tcp_vegas_state,
369 .cwnd_event = tcp_vegas_cwnd_event,
370 .get_info = tcp_vegas_get_info,
371
372 .owner = THIS_MODULE,
373 .name = "vegas",
374};
375
376static int __init tcp_vegas_register(void)
377{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300378 BUG_ON(sizeof(struct vegas) > ICSK_CA_PRIV_SIZE);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700379 tcp_register_congestion_control(&tcp_vegas);
380 return 0;
381}
382
383static void __exit tcp_vegas_unregister(void)
384{
385 tcp_unregister_congestion_control(&tcp_vegas);
386}
387
388module_init(tcp_vegas_register);
389module_exit(tcp_vegas_unregister);
390
391MODULE_AUTHOR("Stephen Hemminger");
392MODULE_LICENSE("GPL");
393MODULE_DESCRIPTION("TCP Vegas");