• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static int alpha = 2;
44 static int beta  = 4;
45 static int gamma = 1;
46 
47 module_param(alpha, int, 0644);
48 MODULE_PARM_DESC(alpha, "lower bound of packets in network");
49 module_param(beta, int, 0644);
50 MODULE_PARM_DESC(beta, "upper bound of packets in network");
51 module_param(gamma, int, 0644);
52 MODULE_PARM_DESC(gamma, "limit on increase (scale by 2)");
53 
54 
55 /* 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  */
vegas_enable(struct sock * sk)71 static void vegas_enable(struct sock *sk)
72 {
73 	const struct tcp_sock *tp = tcp_sk(sk);
74 	struct vegas *vegas = inet_csk_ca(sk);
75 
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. */
vegas_disable(struct sock * sk)87 static inline void vegas_disable(struct sock *sk)
88 {
89 	struct vegas *vegas = inet_csk_ca(sk);
90 
91 	vegas->doing_vegas_now = 0;
92 }
93 
tcp_vegas_init(struct sock * sk)94 void tcp_vegas_init(struct sock *sk)
95 {
96 	struct vegas *vegas = inet_csk_ca(sk);
97 
98 	vegas->baseRTT = 0x7fffffff;
99 	vegas_enable(sk);
100 }
101 EXPORT_SYMBOL_GPL(tcp_vegas_init);
102 
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  */
tcp_vegas_pkts_acked(struct sock * sk,u32 cnt,s32 rtt_us)111 void tcp_vegas_pkts_acked(struct sock *sk, u32 cnt, s32 rtt_us)
112 {
113 	struct vegas *vegas = inet_csk_ca(sk);
114 	u32 vrtt;
115 
116 	if (rtt_us < 0)
117 		return;
118 
119 	/* Never allow zero rtt or baseRTT */
120 	vrtt = rtt_us + 1;
121 
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 }
132 EXPORT_SYMBOL_GPL(tcp_vegas_pkts_acked);
133 
tcp_vegas_state(struct sock * sk,u8 ca_state)134 void tcp_vegas_state(struct sock *sk, u8 ca_state)
135 {
136 
137 	if (ca_state == TCP_CA_Open)
138 		vegas_enable(sk);
139 	else
140 		vegas_disable(sk);
141 }
142 EXPORT_SYMBOL_GPL(tcp_vegas_state);
143 
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  */
tcp_vegas_cwnd_event(struct sock * sk,enum tcp_ca_event event)153 void tcp_vegas_cwnd_event(struct sock *sk, enum tcp_ca_event event)
154 {
155 	if (event == CA_EVENT_CWND_RESTART ||
156 	    event == CA_EVENT_TX_START)
157 		tcp_vegas_init(sk);
158 }
159 EXPORT_SYMBOL_GPL(tcp_vegas_cwnd_event);
160 
tcp_vegas_cong_avoid(struct sock * sk,u32 ack,u32 in_flight)161 static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
162 {
163 	struct tcp_sock *tp = tcp_sk(sk);
164 	struct vegas *vegas = inet_csk_ca(sk);
165 
166 	if (!vegas->doing_vegas_now) {
167 		tcp_reno_cong_avoid(sk, ack, in_flight);
168 		return;
169 	}
170 
171 	if (after(ack, vegas->beg_snd_nxt)) {
172 		/* Do the Vegas once-per-RTT cwnd adjustment. */
173 
174 		/* Save the extent of the current window so we can use this
175 		 * at the end of the next RTT.
176 		 */
177 		vegas->beg_snd_nxt  = tp->snd_nxt;
178 
179 		/* We do the Vegas calculations only if we got enough RTT
180 		 * samples that we can be reasonably sure that we got
181 		 * at least one RTT sample that wasn't from a delayed ACK.
182 		 * If we only had 2 samples total,
183 		 * then that means we're getting only 1 ACK per RTT, which
184 		 * means they're almost certainly delayed ACKs.
185 		 * If  we have 3 samples, we should be OK.
186 		 */
187 
188 		if (vegas->cntRTT <= 2) {
189 			/* We don't have enough RTT samples to do the Vegas
190 			 * calculation, so we'll behave like Reno.
191 			 */
192 			tcp_reno_cong_avoid(sk, ack, in_flight);
193 		} else {
194 			u32 rtt, diff;
195 			u64 target_cwnd;
196 
197 			/* We have enough RTT samples, so, using the Vegas
198 			 * algorithm, we determine if we should increase or
199 			 * decrease cwnd, and by how much.
200 			 */
201 
202 			/* Pluck out the RTT we are using for the Vegas
203 			 * calculations. This is the min RTT seen during the
204 			 * last RTT. Taking the min filters out the effects
205 			 * of delayed ACKs, at the cost of noticing congestion
206 			 * a bit later.
207 			 */
208 			rtt = vegas->minRTT;
209 
210 			/* Calculate the cwnd we should have, if we weren't
211 			 * going too fast.
212 			 *
213 			 * This is:
214 			 *     (actual rate in segments) * baseRTT
215 			 */
216 			target_cwnd = tp->snd_cwnd * vegas->baseRTT / rtt;
217 
218 			/* Calculate the difference between the window we had,
219 			 * and the window we would like to have. This quantity
220 			 * is the "Diff" from the Arizona Vegas papers.
221 			 */
222 			diff = tp->snd_cwnd * (rtt-vegas->baseRTT) / vegas->baseRTT;
223 
224 			if (diff > gamma && tp->snd_ssthresh > 2 ) {
225 				/* Going too fast. Time to slow down
226 				 * and switch to congestion avoidance.
227 				 */
228 				tp->snd_ssthresh = 2;
229 
230 				/* Set cwnd to match the actual rate
231 				 * exactly:
232 				 *   cwnd = (actual rate) * baseRTT
233 				 * Then we add 1 because the integer
234 				 * truncation robs us of full link
235 				 * utilization.
236 				 */
237 				tp->snd_cwnd = min(tp->snd_cwnd, (u32)target_cwnd+1);
238 
239 			} else if (tp->snd_cwnd <= tp->snd_ssthresh) {
240 				/* Slow start.  */
241 				tcp_slow_start(tp);
242 			} else {
243 				/* Congestion avoidance. */
244 
245 				/* Figure out where we would like cwnd
246 				 * to be.
247 				 */
248 				if (diff > beta) {
249 					/* The old window was too fast, so
250 					 * we slow down.
251 					 */
252 					tp->snd_cwnd--;
253 				} else if (diff < alpha) {
254 					/* We don't have enough extra packets
255 					 * in the network, so speed up.
256 					 */
257 					tp->snd_cwnd++;
258 				} else {
259 					/* Sending just as fast as we
260 					 * should be.
261 					 */
262 				}
263 			}
264 
265 			if (tp->snd_cwnd < 2)
266 				tp->snd_cwnd = 2;
267 			else if (tp->snd_cwnd > tp->snd_cwnd_clamp)
268 				tp->snd_cwnd = tp->snd_cwnd_clamp;
269 
270 			tp->snd_ssthresh = tcp_current_ssthresh(sk);
271 		}
272 
273 		/* Wipe the slate clean for the next RTT. */
274 		vegas->cntRTT = 0;
275 		vegas->minRTT = 0x7fffffff;
276 	}
277 	/* Use normal slow start */
278 	else if (tp->snd_cwnd <= tp->snd_ssthresh)
279 		tcp_slow_start(tp);
280 
281 }
282 
283 /* Extract info for Tcp socket info provided via netlink. */
tcp_vegas_get_info(struct sock * sk,u32 ext,struct sk_buff * skb)284 void tcp_vegas_get_info(struct sock *sk, u32 ext, struct sk_buff *skb)
285 {
286 	const struct vegas *ca = inet_csk_ca(sk);
287 	if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
288 		struct tcpvegas_info info = {
289 			.tcpv_enabled = ca->doing_vegas_now,
290 			.tcpv_rttcnt = ca->cntRTT,
291 			.tcpv_rtt = ca->baseRTT,
292 			.tcpv_minrtt = ca->minRTT,
293 		};
294 
295 		nla_put(skb, INET_DIAG_VEGASINFO, sizeof(info), &info);
296 	}
297 }
298 EXPORT_SYMBOL_GPL(tcp_vegas_get_info);
299 
300 static struct tcp_congestion_ops tcp_vegas = {
301 	.flags		= TCP_CONG_RTT_STAMP,
302 	.init		= tcp_vegas_init,
303 	.ssthresh	= tcp_reno_ssthresh,
304 	.cong_avoid	= tcp_vegas_cong_avoid,
305 	.min_cwnd	= tcp_reno_min_cwnd,
306 	.pkts_acked	= tcp_vegas_pkts_acked,
307 	.set_state	= tcp_vegas_state,
308 	.cwnd_event	= tcp_vegas_cwnd_event,
309 	.get_info	= tcp_vegas_get_info,
310 
311 	.owner		= THIS_MODULE,
312 	.name		= "vegas",
313 };
314 
tcp_vegas_register(void)315 static int __init tcp_vegas_register(void)
316 {
317 	BUILD_BUG_ON(sizeof(struct vegas) > ICSK_CA_PRIV_SIZE);
318 	tcp_register_congestion_control(&tcp_vegas);
319 	return 0;
320 }
321 
tcp_vegas_unregister(void)322 static void __exit tcp_vegas_unregister(void)
323 {
324 	tcp_unregister_congestion_control(&tcp_vegas);
325 }
326 
327 module_init(tcp_vegas_register);
328 module_exit(tcp_vegas_unregister);
329 
330 MODULE_AUTHOR("Stephen Hemminger");
331 MODULE_LICENSE("GPL");
332 MODULE_DESCRIPTION("TCP Vegas");
333