blob: 48539fff6357a4e778c537b99bb9a7fd49eb43b3 [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
Doug Leith8d3a5642008-12-09 00:13:04 -080043static int alpha = 2;
44static int beta = 4;
45static int gamma = 1;
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070046
47module_param(alpha, int, 0644);
Doug Leith8d3a5642008-12-09 00:13:04 -080048MODULE_PARM_DESC(alpha, "lower bound of packets in network");
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070049module_param(beta, int, 0644);
Doug Leith8d3a5642008-12-09 00:13:04 -080050MODULE_PARM_DESC(beta, "upper bound of packets in network");
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070051module_param(gamma, int, 0644);
52MODULE_PARM_DESC(gamma, "limit on increase (scale by 2)");
53
54
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070055/* There are several situations when we must "re-start" Vegas:
56 *
57 * o when a connection is established
58 * o after an RTO
59 * o after fast recovery
60 * o when we send a packet and there is no outstanding
61 * unacknowledged data (restarting an idle connection)
62 *
63 * In these circumstances we cannot do a Vegas calculation at the
64 * end of the first RTT, because any calculation we do is using
65 * stale info -- both the saved cwnd and congestion feedback are
66 * stale.
67 *
68 * Instead we must wait until the completion of an RTT during
69 * which we actually receive ACKs.
70 */
Stephen Hemminger77522372007-04-23 22:28:23 -070071static void vegas_enable(struct sock *sk)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070072{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030073 const struct tcp_sock *tp = tcp_sk(sk);
74 struct vegas *vegas = inet_csk_ca(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070075
76 /* Begin taking Vegas samples next time we send something. */
77 vegas->doing_vegas_now = 1;
78
79 /* Set the beginning of the next send window. */
80 vegas->beg_snd_nxt = tp->snd_nxt;
81
82 vegas->cntRTT = 0;
83 vegas->minRTT = 0x7fffffff;
84}
85
86/* Stop taking Vegas samples for now. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030087static inline void vegas_disable(struct sock *sk)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070088{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030089 struct vegas *vegas = inet_csk_ca(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070090
91 vegas->doing_vegas_now = 0;
92}
93
Stephen Hemminger77522372007-04-23 22:28:23 -070094void tcp_vegas_init(struct sock *sk)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070095{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030096 struct vegas *vegas = inet_csk_ca(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -070097
98 vegas->baseRTT = 0x7fffffff;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030099 vegas_enable(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700100}
Stephen Hemminger77522372007-04-23 22:28:23 -0700101EXPORT_SYMBOL_GPL(tcp_vegas_init);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700102
103/* Do RTT sampling needed for Vegas.
104 * Basically we:
105 * o min-filter RTT samples from within an RTT to get the current
106 * propagation delay + queuing delay (we are min-filtering to try to
107 * avoid the effects of delayed ACKs)
108 * o min-filter RTT samples from a much longer window (forever for now)
109 * to find the propagation delay (baseRTT)
110 */
Stephen Hemminger30cfd0b2007-07-25 23:49:34 -0700111void tcp_vegas_pkts_acked(struct sock *sk, u32 cnt, s32 rtt_us)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700112{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300113 struct vegas *vegas = inet_csk_ca(sk);
Stephen Hemminger164891a2007-04-23 22:26:16 -0700114 u32 vrtt;
115
Stephen Hemminger30cfd0b2007-07-25 23:49:34 -0700116 if (rtt_us < 0)
Ilpo Järvinenb9ce2042007-06-15 15:08:43 -0700117 return;
118
Stephen Hemminger164891a2007-04-23 22:26:16 -0700119 /* Never allow zero rtt or baseRTT */
Stephen Hemminger30cfd0b2007-07-25 23:49:34 -0700120 vrtt = rtt_us + 1;
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700121
122 /* Filter to find propagation delay: */
123 if (vrtt < vegas->baseRTT)
124 vegas->baseRTT = vrtt;
125
126 /* Find the min RTT during the last RTT to find
127 * the current prop. delay + queuing delay:
128 */
129 vegas->minRTT = min(vegas->minRTT, vrtt);
130 vegas->cntRTT++;
131}
Stephen Hemminger77522372007-04-23 22:28:23 -0700132EXPORT_SYMBOL_GPL(tcp_vegas_pkts_acked);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700133
Stephen Hemminger77522372007-04-23 22:28:23 -0700134void tcp_vegas_state(struct sock *sk, u8 ca_state)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700135{
136
137 if (ca_state == TCP_CA_Open)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300138 vegas_enable(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700139 else
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300140 vegas_disable(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700141}
Stephen Hemminger77522372007-04-23 22:28:23 -0700142EXPORT_SYMBOL_GPL(tcp_vegas_state);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700143
144/*
145 * If the connection is idle and we are restarting,
146 * then we don't want to do any Vegas calculations
147 * until we get fresh RTT samples. So when we
148 * restart, we reset our Vegas state to a clean
149 * slate. After we get acks for this flight of
150 * packets, _then_ we can make Vegas calculations
151 * again.
152 */
Stephen Hemminger77522372007-04-23 22:28:23 -0700153void tcp_vegas_cwnd_event(struct sock *sk, enum tcp_ca_event event)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700154{
155 if (event == CA_EVENT_CWND_RESTART ||
156 event == CA_EVENT_TX_START)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300157 tcp_vegas_init(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700158}
Stephen Hemminger77522372007-04-23 22:28:23 -0700159EXPORT_SYMBOL_GPL(tcp_vegas_cwnd_event);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700160
Doug Leithc80a5cd2009-05-25 22:44:59 -0700161static inline u32 tcp_vegas_ssthresh(struct tcp_sock *tp)
162{
163 return min(tp->snd_ssthresh, tp->snd_cwnd-1);
164}
165
Yuchung Cheng9f9843a72013-10-31 11:07:31 -0700166static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack, u32 acked,
167 u32 in_flight)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700168{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300169 struct tcp_sock *tp = tcp_sk(sk);
170 struct vegas *vegas = inet_csk_ca(sk);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700171
Harvey Harrisonab598592008-05-01 02:47:38 -0700172 if (!vegas->doing_vegas_now) {
Yuchung Cheng9f9843a72013-10-31 11:07:31 -0700173 tcp_reno_cong_avoid(sk, ack, acked, in_flight);
Harvey Harrisonab598592008-05-01 02:47:38 -0700174 return;
175 }
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700176
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700177 if (after(ack, vegas->beg_snd_nxt)) {
178 /* Do the Vegas once-per-RTT cwnd adjustment. */
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700179
180 /* Save the extent of the current window so we can use this
181 * at the end of the next RTT.
182 */
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700183 vegas->beg_snd_nxt = tp->snd_nxt;
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700184
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700185 /* We do the Vegas calculations only if we got enough RTT
186 * samples that we can be reasonably sure that we got
187 * at least one RTT sample that wasn't from a delayed ACK.
188 * If we only had 2 samples total,
189 * then that means we're getting only 1 ACK per RTT, which
190 * means they're almost certainly delayed ACKs.
191 * If we have 3 samples, we should be OK.
192 */
193
194 if (vegas->cntRTT <= 2) {
195 /* We don't have enough RTT samples to do the Vegas
196 * calculation, so we'll behave like Reno.
197 */
Yuchung Cheng9f9843a72013-10-31 11:07:31 -0700198 tcp_reno_cong_avoid(sk, ack, acked, in_flight);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700199 } else {
Lachlan Andrew15913112008-04-30 01:04:03 -0700200 u32 rtt, diff;
201 u64 target_cwnd;
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700202
203 /* We have enough RTT samples, so, using the Vegas
204 * algorithm, we determine if we should increase or
205 * decrease cwnd, and by how much.
206 */
207
208 /* Pluck out the RTT we are using for the Vegas
209 * calculations. This is the min RTT seen during the
210 * last RTT. Taking the min filters out the effects
211 * of delayed ACKs, at the cost of noticing congestion
212 * a bit later.
213 */
214 rtt = vegas->minRTT;
215
216 /* Calculate the cwnd we should have, if we weren't
217 * going too fast.
218 *
219 * This is:
220 * (actual rate in segments) * baseRTT
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700221 */
Doug Leith8d3a5642008-12-09 00:13:04 -0800222 target_cwnd = tp->snd_cwnd * vegas->baseRTT / rtt;
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700223
224 /* Calculate the difference between the window we had,
225 * and the window we would like to have. This quantity
226 * is the "Diff" from the Arizona Vegas papers.
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700227 */
Doug Leith8d3a5642008-12-09 00:13:04 -0800228 diff = tp->snd_cwnd * (rtt-vegas->baseRTT) / vegas->baseRTT;
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700229
Doug Leithc80a5cd2009-05-25 22:44:59 -0700230 if (diff > gamma && tp->snd_cwnd <= tp->snd_ssthresh) {
Xiaoliang (David) Weic9405872007-10-29 20:24:36 -0700231 /* Going too fast. Time to slow down
232 * and switch to congestion avoidance.
233 */
Xiaoliang (David) Weic9405872007-10-29 20:24:36 -0700234
235 /* Set cwnd to match the actual rate
236 * exactly:
237 * cwnd = (actual rate) * baseRTT
238 * Then we add 1 because the integer
239 * truncation robs us of full link
240 * utilization.
241 */
Doug Leith8d3a5642008-12-09 00:13:04 -0800242 tp->snd_cwnd = min(tp->snd_cwnd, (u32)target_cwnd+1);
Doug Leithc80a5cd2009-05-25 22:44:59 -0700243 tp->snd_ssthresh = tcp_vegas_ssthresh(tp);
Xiaoliang (David) Weic9405872007-10-29 20:24:36 -0700244
245 } else if (tp->snd_cwnd <= tp->snd_ssthresh) {
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700246 /* Slow start. */
Yuchung Cheng9f9843a72013-10-31 11:07:31 -0700247 tcp_slow_start(tp, acked);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700248 } else {
249 /* Congestion avoidance. */
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700250
251 /* Figure out where we would like cwnd
252 * to be.
253 */
254 if (diff > beta) {
255 /* The old window was too fast, so
256 * we slow down.
257 */
Doug Leith8d3a5642008-12-09 00:13:04 -0800258 tp->snd_cwnd--;
Doug Leithc80a5cd2009-05-25 22:44:59 -0700259 tp->snd_ssthresh
260 = tcp_vegas_ssthresh(tp);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700261 } else if (diff < alpha) {
262 /* We don't have enough extra packets
263 * in the network, so speed up.
264 */
Doug Leith8d3a5642008-12-09 00:13:04 -0800265 tp->snd_cwnd++;
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700266 } else {
267 /* Sending just as fast as we
268 * should be.
269 */
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700270 }
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700271 }
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700272
Stephen Hemminger7faffa12005-11-10 17:07:24 -0800273 if (tp->snd_cwnd < 2)
274 tp->snd_cwnd = 2;
275 else if (tp->snd_cwnd > tp->snd_cwnd_clamp)
276 tp->snd_cwnd = tp->snd_cwnd_clamp;
Doug Leitha6af2d62008-12-04 17:17:18 -0800277
278 tp->snd_ssthresh = tcp_current_ssthresh(sk);
Stephen Hemminger7faffa12005-11-10 17:07:24 -0800279 }
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700280
Thomas Young5b495612005-12-06 16:16:34 -0800281 /* Wipe the slate clean for the next RTT. */
282 vegas->cntRTT = 0;
283 vegas->minRTT = 0x7fffffff;
284 }
Thomas Young74cb8792006-01-04 13:59:32 -0800285 /* Use normal slow start */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900286 else if (tp->snd_cwnd <= tp->snd_ssthresh)
Yuchung Cheng9f9843a72013-10-31 11:07:31 -0700287 tcp_slow_start(tp, acked);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900288
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700289}
290
291/* Extract info for Tcp socket info provided via netlink. */
Stephen Hemminger77522372007-04-23 22:28:23 -0700292void tcp_vegas_get_info(struct sock *sk, u32 ext, struct sk_buff *skb)
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700293{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300294 const struct vegas *ca = inet_csk_ca(sk);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300295 if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
Thomas Grafe9195d62007-03-22 23:27:01 -0700296 struct tcpvegas_info info = {
297 .tcpv_enabled = ca->doing_vegas_now,
298 .tcpv_rttcnt = ca->cntRTT,
299 .tcpv_rtt = ca->baseRTT,
300 .tcpv_minrtt = ca->minRTT,
301 };
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700302
Thomas Grafe9195d62007-03-22 23:27:01 -0700303 nla_put(skb, INET_DIAG_VEGASINFO, sizeof(info), &info);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700304 }
305}
Stephen Hemminger77522372007-04-23 22:28:23 -0700306EXPORT_SYMBOL_GPL(tcp_vegas_get_info);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700307
Stephen Hemmingera252beb2011-03-10 00:40:17 -0800308static struct tcp_congestion_ops tcp_vegas __read_mostly = {
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700309 .init = tcp_vegas_init,
310 .ssthresh = tcp_reno_ssthresh,
311 .cong_avoid = tcp_vegas_cong_avoid,
Stephen Hemminger164891a2007-04-23 22:26:16 -0700312 .pkts_acked = tcp_vegas_pkts_acked,
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700313 .set_state = tcp_vegas_state,
314 .cwnd_event = tcp_vegas_cwnd_event,
315 .get_info = tcp_vegas_get_info,
316
317 .owner = THIS_MODULE,
318 .name = "vegas",
319};
320
321static int __init tcp_vegas_register(void)
322{
Alexey Dobriyan74975d42006-08-25 17:10:33 -0700323 BUILD_BUG_ON(sizeof(struct vegas) > ICSK_CA_PRIV_SIZE);
Stephen Hemmingerb87d8562005-06-23 12:27:19 -0700324 tcp_register_congestion_control(&tcp_vegas);
325 return 0;
326}
327
328static void __exit tcp_vegas_unregister(void)
329{
330 tcp_unregister_congestion_control(&tcp_vegas);
331}
332
333module_init(tcp_vegas_register);
334module_exit(tcp_vegas_unregister);
335
336MODULE_AUTHOR("Stephen Hemminger");
337MODULE_LICENSE("GPL");
338MODULE_DESCRIPTION("TCP Vegas");