blob: b49dedcda52df949c159db5b12635c9414a1538a [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
Stephen Hemminger77522372007-04-23 22:28:23 -070041#include "tcp_vegas.h"
42
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070043/* 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
David S. Millerd2e4bdc2006-11-28 14:37:38 -080047static int alpha = 2<<V_PARAM_SHIFT;
48static int beta = 4<<V_PARAM_SHIFT;
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070049static int gamma = 1<<V_PARAM_SHIFT;
50
51module_param(alpha, int, 0644);
52MODULE_PARM_DESC(alpha, "lower bound of packets in network (scale by 2)");
53module_param(beta, int, 0644);
54MODULE_PARM_DESC(beta, "upper bound of packets in network (scale by 2)");
55module_param(gamma, int, 0644);
56MODULE_PARM_DESC(gamma, "limit on increase (scale by 2)");
57
58
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070059/* 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 */
Stephen Hemminger77522372007-04-23 22:28:23 -070075static void vegas_enable(struct sock *sk)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070076{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030077 const struct tcp_sock *tp = tcp_sk(sk);
78 struct vegas *vegas = inet_csk_ca(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070079
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. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030091static inline void vegas_disable(struct sock *sk)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070092{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030093 struct vegas *vegas = inet_csk_ca(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070094
95 vegas->doing_vegas_now = 0;
96}
97
Stephen Hemminger77522372007-04-23 22:28:23 -070098void tcp_vegas_init(struct sock *sk)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070099{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300100 struct vegas *vegas = inet_csk_ca(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700101
102 vegas->baseRTT = 0x7fffffff;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300103 vegas_enable(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700104}
Stephen Hemminger77522372007-04-23 22:28:23 -0700105EXPORT_SYMBOL_GPL(tcp_vegas_init);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700106
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 */
Stephen Hemminger30cfd0b2007-07-25 23:49:34 -0700115void tcp_vegas_pkts_acked(struct sock *sk, u32 cnt, s32 rtt_us)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700116{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300117 struct vegas *vegas = inet_csk_ca(sk);
Stephen Hemminger164891a2007-04-23 22:26:16 -0700118 u32 vrtt;
119
Stephen Hemminger30cfd0b2007-07-25 23:49:34 -0700120 if (rtt_us < 0)
Ilpo Järvinenb9ce2042007-06-15 15:08:43 -0700121 return;
122
Stephen Hemminger164891a2007-04-23 22:26:16 -0700123 /* Never allow zero rtt or baseRTT */
Stephen Hemminger30cfd0b2007-07-25 23:49:34 -0700124 vrtt = rtt_us + 1;
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700125
126 /* Filter to find propagation delay: */
127 if (vrtt < vegas->baseRTT)
128 vegas->baseRTT = vrtt;
129
130 /* Find the min RTT during the last RTT to find
131 * the current prop. delay + queuing delay:
132 */
133 vegas->minRTT = min(vegas->minRTT, vrtt);
134 vegas->cntRTT++;
135}
Stephen Hemminger77522372007-04-23 22:28:23 -0700136EXPORT_SYMBOL_GPL(tcp_vegas_pkts_acked);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700137
Stephen Hemminger77522372007-04-23 22:28:23 -0700138void tcp_vegas_state(struct sock *sk, u8 ca_state)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700139{
140
141 if (ca_state == TCP_CA_Open)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300142 vegas_enable(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700143 else
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300144 vegas_disable(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700145}
Stephen Hemminger77522372007-04-23 22:28:23 -0700146EXPORT_SYMBOL_GPL(tcp_vegas_state);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700147
148/*
149 * If the connection is idle and we are restarting,
150 * then we don't want to do any Vegas calculations
151 * until we get fresh RTT samples. So when we
152 * restart, we reset our Vegas state to a clean
153 * slate. After we get acks for this flight of
154 * packets, _then_ we can make Vegas calculations
155 * again.
156 */
Stephen Hemminger77522372007-04-23 22:28:23 -0700157void tcp_vegas_cwnd_event(struct sock *sk, enum tcp_ca_event event)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700158{
159 if (event == CA_EVENT_CWND_RESTART ||
160 event == CA_EVENT_TX_START)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300161 tcp_vegas_init(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700162}
Stephen Hemminger77522372007-04-23 22:28:23 -0700163EXPORT_SYMBOL_GPL(tcp_vegas_cwnd_event);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700164
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300165static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack,
Stephen Hemminger16751342007-07-16 18:35:52 -0700166 u32 in_flight, int flag)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700167{
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)
Stephen Hemminger16751342007-07-16 18:35:52 -0700172 return tcp_reno_cong_avoid(sk, ack, 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
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700218 /* We do the Vegas calculations only if we got enough RTT
219 * samples that we can be reasonably sure that we got
220 * at least one RTT sample that wasn't from a delayed ACK.
221 * If we only had 2 samples total,
222 * then that means we're getting only 1 ACK per RTT, which
223 * means they're almost certainly delayed ACKs.
224 * If we have 3 samples, we should be OK.
225 */
226
227 if (vegas->cntRTT <= 2) {
228 /* We don't have enough RTT samples to do the Vegas
229 * calculation, so we'll behave like Reno.
230 */
Stephen Hemminger16751342007-07-16 18:35:52 -0700231 tcp_reno_cong_avoid(sk, ack, in_flight, flag);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700232 } else {
233 u32 rtt, target_cwnd, diff;
234
235 /* We have enough RTT samples, so, using the Vegas
236 * algorithm, we determine if we should increase or
237 * decrease cwnd, and by how much.
238 */
239
240 /* Pluck out the RTT we are using for the Vegas
241 * calculations. This is the min RTT seen during the
242 * last RTT. Taking the min filters out the effects
243 * of delayed ACKs, at the cost of noticing congestion
244 * a bit later.
245 */
246 rtt = vegas->minRTT;
247
248 /* Calculate the cwnd we should have, if we weren't
249 * going too fast.
250 *
251 * This is:
252 * (actual rate in segments) * baseRTT
253 * We keep it as a fixed point number with
254 * V_PARAM_SHIFT bits to the right of the binary point.
255 */
256 target_cwnd = ((old_wnd * vegas->baseRTT)
257 << V_PARAM_SHIFT) / rtt;
258
259 /* Calculate the difference between the window we had,
260 * and the window we would like to have. This quantity
261 * is the "Diff" from the Arizona Vegas papers.
262 *
263 * Again, this is a fixed point number with
264 * V_PARAM_SHIFT bits to the right of the binary
265 * point.
266 */
267 diff = (old_wnd << V_PARAM_SHIFT) - target_cwnd;
268
Stephen Hemminger7faffa12005-11-10 17:07:24 -0800269 if (tp->snd_cwnd <= tp->snd_ssthresh) {
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700270 /* Slow start. */
271 if (diff > gamma) {
272 /* Going too fast. Time to slow down
273 * and switch to congestion avoidance.
274 */
275 tp->snd_ssthresh = 2;
276
277 /* Set cwnd to match the actual rate
278 * exactly:
279 * cwnd = (actual rate) * baseRTT
280 * Then we add 1 because the integer
281 * truncation robs us of full link
282 * utilization.
283 */
284 tp->snd_cwnd = min(tp->snd_cwnd,
285 (target_cwnd >>
286 V_PARAM_SHIFT)+1);
287
288 }
Stephen Hemminger7faffa12005-11-10 17:07:24 -0800289 tcp_slow_start(tp);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700290 } else {
291 /* Congestion avoidance. */
292 u32 next_snd_cwnd;
293
294 /* Figure out where we would like cwnd
295 * to be.
296 */
297 if (diff > beta) {
298 /* The old window was too fast, so
299 * we slow down.
300 */
301 next_snd_cwnd = old_snd_cwnd - 1;
302 } else if (diff < alpha) {
303 /* We don't have enough extra packets
304 * in the network, so speed up.
305 */
306 next_snd_cwnd = old_snd_cwnd + 1;
307 } else {
308 /* Sending just as fast as we
309 * should be.
310 */
311 next_snd_cwnd = old_snd_cwnd;
312 }
313
314 /* Adjust cwnd upward or downward, toward the
315 * desired value.
316 */
317 if (next_snd_cwnd > tp->snd_cwnd)
318 tp->snd_cwnd++;
319 else if (next_snd_cwnd < tp->snd_cwnd)
320 tp->snd_cwnd--;
321 }
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700322
Stephen Hemminger7faffa12005-11-10 17:07:24 -0800323 if (tp->snd_cwnd < 2)
324 tp->snd_cwnd = 2;
325 else if (tp->snd_cwnd > tp->snd_cwnd_clamp)
326 tp->snd_cwnd = tp->snd_cwnd_clamp;
327 }
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700328
Thomas Young5b495612005-12-06 16:16:34 -0800329 /* Wipe the slate clean for the next RTT. */
330 vegas->cntRTT = 0;
331 vegas->minRTT = 0x7fffffff;
332 }
Thomas Young74cb8792006-01-04 13:59:32 -0800333 /* Use normal slow start */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900334 else if (tp->snd_cwnd <= tp->snd_ssthresh)
Thomas Young74cb8792006-01-04 13:59:32 -0800335 tcp_slow_start(tp);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900336
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700337}
338
339/* Extract info for Tcp socket info provided via netlink. */
Stephen Hemminger77522372007-04-23 22:28:23 -0700340void tcp_vegas_get_info(struct sock *sk, u32 ext, struct sk_buff *skb)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700341{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300342 const struct vegas *ca = inet_csk_ca(sk);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300343 if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
Thomas Grafe9195d62007-03-22 23:27:01 -0700344 struct tcpvegas_info info = {
345 .tcpv_enabled = ca->doing_vegas_now,
346 .tcpv_rttcnt = ca->cntRTT,
347 .tcpv_rtt = ca->baseRTT,
348 .tcpv_minrtt = ca->minRTT,
349 };
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700350
Thomas Grafe9195d62007-03-22 23:27:01 -0700351 nla_put(skb, INET_DIAG_VEGASINFO, sizeof(info), &info);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700352 }
353}
Stephen Hemminger77522372007-04-23 22:28:23 -0700354EXPORT_SYMBOL_GPL(tcp_vegas_get_info);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700355
356static struct tcp_congestion_ops tcp_vegas = {
Stephen Hemminger164891a2007-04-23 22:26:16 -0700357 .flags = TCP_CONG_RTT_STAMP,
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700358 .init = tcp_vegas_init,
359 .ssthresh = tcp_reno_ssthresh,
360 .cong_avoid = tcp_vegas_cong_avoid,
361 .min_cwnd = tcp_reno_min_cwnd,
Stephen Hemminger164891a2007-04-23 22:26:16 -0700362 .pkts_acked = tcp_vegas_pkts_acked,
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700363 .set_state = tcp_vegas_state,
364 .cwnd_event = tcp_vegas_cwnd_event,
365 .get_info = tcp_vegas_get_info,
366
367 .owner = THIS_MODULE,
368 .name = "vegas",
369};
370
371static int __init tcp_vegas_register(void)
372{
Alexey Dobriyan74975d42006-08-25 17:10:33 -0700373 BUILD_BUG_ON(sizeof(struct vegas) > ICSK_CA_PRIV_SIZE);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700374 tcp_register_congestion_control(&tcp_vegas);
375 return 0;
376}
377
378static void __exit tcp_vegas_unregister(void)
379{
380 tcp_unregister_congestion_control(&tcp_vegas);
381}
382
383module_init(tcp_vegas_register);
384module_exit(tcp_vegas_unregister);
385
386MODULE_AUTHOR("Stephen Hemminger");
387MODULE_LICENSE("GPL");
388MODULE_DESCRIPTION("TCP Vegas");