xref: /linux/net/ipv4/tcp_vegas.c (revision 6687e988d9aeaccad6774e6a8304f681f3ec0a03)
1b87d8561SStephen Hemminger /*
2b87d8561SStephen Hemminger  * TCP Vegas congestion control
3b87d8561SStephen Hemminger  *
4b87d8561SStephen Hemminger  * This is based on the congestion detection/avoidance scheme described in
5b87d8561SStephen Hemminger  *    Lawrence S. Brakmo and Larry L. Peterson.
6b87d8561SStephen Hemminger  *    "TCP Vegas: End to end congestion avoidance on a global internet."
7b87d8561SStephen Hemminger  *    IEEE Journal on Selected Areas in Communication, 13(8):1465--1480,
8b87d8561SStephen Hemminger  *    October 1995. Available from:
9b87d8561SStephen Hemminger  *	ftp://ftp.cs.arizona.edu/xkernel/Papers/jsac.ps
10b87d8561SStephen Hemminger  *
11b87d8561SStephen Hemminger  * See http://www.cs.arizona.edu/xkernel/ for their implementation.
12b87d8561SStephen Hemminger  * The main aspects that distinguish this implementation from the
13b87d8561SStephen Hemminger  * Arizona Vegas implementation are:
14b87d8561SStephen Hemminger  *   o We do not change the loss detection or recovery mechanisms of
15b87d8561SStephen Hemminger  *     Linux in any way. Linux already recovers from losses quite well,
16b87d8561SStephen Hemminger  *     using fine-grained timers, NewReno, and FACK.
17b87d8561SStephen Hemminger  *   o To avoid the performance penalty imposed by increasing cwnd
18b87d8561SStephen Hemminger  *     only every-other RTT during slow start, we increase during
19b87d8561SStephen Hemminger  *     every RTT during slow start, just like Reno.
20b87d8561SStephen Hemminger  *   o Largely to allow continuous cwnd growth during slow start,
21b87d8561SStephen Hemminger  *     we use the rate at which ACKs come back as the "actual"
22b87d8561SStephen Hemminger  *     rate, rather than the rate at which data is sent.
23b87d8561SStephen Hemminger  *   o To speed convergence to the right rate, we set the cwnd
24b87d8561SStephen Hemminger  *     to achieve the right ("actual") rate when we exit slow start.
25b87d8561SStephen Hemminger  *   o To filter out the noise caused by delayed ACKs, we use the
26b87d8561SStephen Hemminger  *     minimum RTT sample observed during the last RTT to calculate
27b87d8561SStephen Hemminger  *     the actual rate.
28b87d8561SStephen Hemminger  *   o When the sender re-starts from idle, it waits until it has
29b87d8561SStephen Hemminger  *     received ACKs for an entire flight of new data before making
30b87d8561SStephen Hemminger  *     a cwnd adjustment decision. The original Vegas implementation
31b87d8561SStephen Hemminger  *     assumed senders never went idle.
32b87d8561SStephen Hemminger  */
33b87d8561SStephen Hemminger 
34b87d8561SStephen Hemminger #include <linux/config.h>
35b87d8561SStephen Hemminger #include <linux/mm.h>
36b87d8561SStephen Hemminger #include <linux/module.h>
37b87d8561SStephen Hemminger #include <linux/skbuff.h>
38b87d8561SStephen Hemminger #include <linux/tcp_diag.h>
39b87d8561SStephen Hemminger 
40b87d8561SStephen Hemminger #include <net/tcp.h>
41b87d8561SStephen Hemminger 
42b87d8561SStephen Hemminger /* Default values of the Vegas variables, in fixed-point representation
43b87d8561SStephen Hemminger  * with V_PARAM_SHIFT bits to the right of the binary point.
44b87d8561SStephen Hemminger  */
45b87d8561SStephen Hemminger #define V_PARAM_SHIFT 1
46b87d8561SStephen Hemminger static int alpha = 1<<V_PARAM_SHIFT;
47b87d8561SStephen Hemminger static int beta  = 3<<V_PARAM_SHIFT;
48b87d8561SStephen Hemminger static int gamma = 1<<V_PARAM_SHIFT;
49b87d8561SStephen Hemminger 
50b87d8561SStephen Hemminger module_param(alpha, int, 0644);
51b87d8561SStephen Hemminger MODULE_PARM_DESC(alpha, "lower bound of packets in network (scale by 2)");
52b87d8561SStephen Hemminger module_param(beta, int, 0644);
53b87d8561SStephen Hemminger MODULE_PARM_DESC(beta, "upper bound of packets in network (scale by 2)");
54b87d8561SStephen Hemminger module_param(gamma, int, 0644);
55b87d8561SStephen Hemminger MODULE_PARM_DESC(gamma, "limit on increase (scale by 2)");
56b87d8561SStephen Hemminger 
57b87d8561SStephen Hemminger 
58b87d8561SStephen Hemminger /* Vegas variables */
59b87d8561SStephen Hemminger struct vegas {
60b87d8561SStephen Hemminger 	u32	beg_snd_nxt;	/* right edge during last RTT */
61b87d8561SStephen Hemminger 	u32	beg_snd_una;	/* left edge  during last RTT */
62b87d8561SStephen Hemminger 	u32	beg_snd_cwnd;	/* saves the size of the cwnd */
63b87d8561SStephen Hemminger 	u8	doing_vegas_now;/* if true, do vegas for this RTT */
64b87d8561SStephen Hemminger 	u16	cntRTT;		/* # of RTTs measured within last RTT */
65b87d8561SStephen Hemminger 	u32	minRTT;		/* min of RTTs measured within last RTT (in usec) */
66b87d8561SStephen Hemminger 	u32	baseRTT;	/* the min of all Vegas RTT measurements seen (in usec) */
67b87d8561SStephen Hemminger };
68b87d8561SStephen Hemminger 
69b87d8561SStephen Hemminger /* There are several situations when we must "re-start" Vegas:
70b87d8561SStephen Hemminger  *
71b87d8561SStephen Hemminger  *  o when a connection is established
72b87d8561SStephen Hemminger  *  o after an RTO
73b87d8561SStephen Hemminger  *  o after fast recovery
74b87d8561SStephen Hemminger  *  o when we send a packet and there is no outstanding
75b87d8561SStephen Hemminger  *    unacknowledged data (restarting an idle connection)
76b87d8561SStephen Hemminger  *
77b87d8561SStephen Hemminger  * In these circumstances we cannot do a Vegas calculation at the
78b87d8561SStephen Hemminger  * end of the first RTT, because any calculation we do is using
79b87d8561SStephen Hemminger  * stale info -- both the saved cwnd and congestion feedback are
80b87d8561SStephen Hemminger  * stale.
81b87d8561SStephen Hemminger  *
82b87d8561SStephen Hemminger  * Instead we must wait until the completion of an RTT during
83b87d8561SStephen Hemminger  * which we actually receive ACKs.
84b87d8561SStephen Hemminger  */
85*6687e988SArnaldo Carvalho de Melo static inline void vegas_enable(struct sock *sk)
86b87d8561SStephen Hemminger {
87*6687e988SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
88*6687e988SArnaldo Carvalho de Melo 	struct vegas *vegas = inet_csk_ca(sk);
89b87d8561SStephen Hemminger 
90b87d8561SStephen Hemminger 	/* Begin taking Vegas samples next time we send something. */
91b87d8561SStephen Hemminger 	vegas->doing_vegas_now = 1;
92b87d8561SStephen Hemminger 
93b87d8561SStephen Hemminger 	/* Set the beginning of the next send window. */
94b87d8561SStephen Hemminger 	vegas->beg_snd_nxt = tp->snd_nxt;
95b87d8561SStephen Hemminger 
96b87d8561SStephen Hemminger 	vegas->cntRTT = 0;
97b87d8561SStephen Hemminger 	vegas->minRTT = 0x7fffffff;
98b87d8561SStephen Hemminger }
99b87d8561SStephen Hemminger 
100b87d8561SStephen Hemminger /* Stop taking Vegas samples for now. */
101*6687e988SArnaldo Carvalho de Melo static inline void vegas_disable(struct sock *sk)
102b87d8561SStephen Hemminger {
103*6687e988SArnaldo Carvalho de Melo 	struct vegas *vegas = inet_csk_ca(sk);
104b87d8561SStephen Hemminger 
105b87d8561SStephen Hemminger 	vegas->doing_vegas_now = 0;
106b87d8561SStephen Hemminger }
107b87d8561SStephen Hemminger 
108*6687e988SArnaldo Carvalho de Melo static void tcp_vegas_init(struct sock *sk)
109b87d8561SStephen Hemminger {
110*6687e988SArnaldo Carvalho de Melo 	struct vegas *vegas = inet_csk_ca(sk);
111b87d8561SStephen Hemminger 
112b87d8561SStephen Hemminger 	vegas->baseRTT = 0x7fffffff;
113*6687e988SArnaldo Carvalho de Melo 	vegas_enable(sk);
114b87d8561SStephen Hemminger }
115b87d8561SStephen Hemminger 
116b87d8561SStephen Hemminger /* Do RTT sampling needed for Vegas.
117b87d8561SStephen Hemminger  * Basically we:
118b87d8561SStephen Hemminger  *   o min-filter RTT samples from within an RTT to get the current
119b87d8561SStephen Hemminger  *     propagation delay + queuing delay (we are min-filtering to try to
120b87d8561SStephen Hemminger  *     avoid the effects of delayed ACKs)
121b87d8561SStephen Hemminger  *   o min-filter RTT samples from a much longer window (forever for now)
122b87d8561SStephen Hemminger  *     to find the propagation delay (baseRTT)
123b87d8561SStephen Hemminger  */
124*6687e988SArnaldo Carvalho de Melo static void tcp_vegas_rtt_calc(struct sock *sk, u32 usrtt)
125b87d8561SStephen Hemminger {
126*6687e988SArnaldo Carvalho de Melo 	struct vegas *vegas = inet_csk_ca(sk);
127b87d8561SStephen Hemminger 	u32 vrtt = usrtt + 1; /* Never allow zero rtt or baseRTT */
128b87d8561SStephen Hemminger 
129b87d8561SStephen Hemminger 	/* Filter to find propagation delay: */
130b87d8561SStephen Hemminger 	if (vrtt < vegas->baseRTT)
131b87d8561SStephen Hemminger 		vegas->baseRTT = vrtt;
132b87d8561SStephen Hemminger 
133b87d8561SStephen Hemminger 	/* Find the min RTT during the last RTT to find
134b87d8561SStephen Hemminger 	 * the current prop. delay + queuing delay:
135b87d8561SStephen Hemminger 	 */
136b87d8561SStephen Hemminger 	vegas->minRTT = min(vegas->minRTT, vrtt);
137b87d8561SStephen Hemminger 	vegas->cntRTT++;
138b87d8561SStephen Hemminger }
139b87d8561SStephen Hemminger 
140*6687e988SArnaldo Carvalho de Melo static void tcp_vegas_state(struct sock *sk, u8 ca_state)
141b87d8561SStephen Hemminger {
142b87d8561SStephen Hemminger 
143b87d8561SStephen Hemminger 	if (ca_state == TCP_CA_Open)
144*6687e988SArnaldo Carvalho de Melo 		vegas_enable(sk);
145b87d8561SStephen Hemminger 	else
146*6687e988SArnaldo Carvalho de Melo 		vegas_disable(sk);
147b87d8561SStephen Hemminger }
148b87d8561SStephen Hemminger 
149b87d8561SStephen Hemminger /*
150b87d8561SStephen Hemminger  * If the connection is idle and we are restarting,
151b87d8561SStephen Hemminger  * then we don't want to do any Vegas calculations
152b87d8561SStephen Hemminger  * until we get fresh RTT samples.  So when we
153b87d8561SStephen Hemminger  * restart, we reset our Vegas state to a clean
154b87d8561SStephen Hemminger  * slate. After we get acks for this flight of
155b87d8561SStephen Hemminger  * packets, _then_ we can make Vegas calculations
156b87d8561SStephen Hemminger  * again.
157b87d8561SStephen Hemminger  */
158*6687e988SArnaldo Carvalho de Melo static void tcp_vegas_cwnd_event(struct sock *sk, enum tcp_ca_event event)
159b87d8561SStephen Hemminger {
160b87d8561SStephen Hemminger 	if (event == CA_EVENT_CWND_RESTART ||
161b87d8561SStephen Hemminger 	    event == CA_EVENT_TX_START)
162*6687e988SArnaldo Carvalho de Melo 		tcp_vegas_init(sk);
163b87d8561SStephen Hemminger }
164b87d8561SStephen Hemminger 
165*6687e988SArnaldo Carvalho de Melo static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack,
166b87d8561SStephen Hemminger 				 u32 seq_rtt, u32 in_flight, int flag)
167b87d8561SStephen Hemminger {
168*6687e988SArnaldo Carvalho de Melo 	struct tcp_sock *tp = tcp_sk(sk);
169*6687e988SArnaldo Carvalho de Melo 	struct vegas *vegas = inet_csk_ca(sk);
170b87d8561SStephen Hemminger 
171b87d8561SStephen Hemminger 	if (!vegas->doing_vegas_now)
172*6687e988SArnaldo Carvalho de Melo 		return tcp_reno_cong_avoid(sk, ack, seq_rtt, in_flight, flag);
173b87d8561SStephen Hemminger 
174b87d8561SStephen Hemminger 	/* The key players are v_beg_snd_una and v_beg_snd_nxt.
175b87d8561SStephen Hemminger 	 *
176b87d8561SStephen Hemminger 	 * These are so named because they represent the approximate values
177b87d8561SStephen Hemminger 	 * of snd_una and snd_nxt at the beginning of the current RTT. More
178b87d8561SStephen Hemminger 	 * precisely, they represent the amount of data sent during the RTT.
179b87d8561SStephen Hemminger 	 * At the end of the RTT, when we receive an ACK for v_beg_snd_nxt,
180b87d8561SStephen Hemminger 	 * we will calculate that (v_beg_snd_nxt - v_beg_snd_una) outstanding
181b87d8561SStephen Hemminger 	 * bytes of data have been ACKed during the course of the RTT, giving
182b87d8561SStephen Hemminger 	 * an "actual" rate of:
183b87d8561SStephen Hemminger 	 *
184b87d8561SStephen Hemminger 	 *     (v_beg_snd_nxt - v_beg_snd_una) / (rtt duration)
185b87d8561SStephen Hemminger 	 *
186b87d8561SStephen Hemminger 	 * Unfortunately, v_beg_snd_una is not exactly equal to snd_una,
187b87d8561SStephen Hemminger 	 * because delayed ACKs can cover more than one segment, so they
188b87d8561SStephen Hemminger 	 * don't line up nicely with the boundaries of RTTs.
189b87d8561SStephen Hemminger 	 *
190b87d8561SStephen Hemminger 	 * Another unfortunate fact of life is that delayed ACKs delay the
191b87d8561SStephen Hemminger 	 * advance of the left edge of our send window, so that the number
192b87d8561SStephen Hemminger 	 * of bytes we send in an RTT is often less than our cwnd will allow.
193b87d8561SStephen Hemminger 	 * So we keep track of our cwnd separately, in v_beg_snd_cwnd.
194b87d8561SStephen Hemminger 	 */
195b87d8561SStephen Hemminger 
196b87d8561SStephen Hemminger 	if (after(ack, vegas->beg_snd_nxt)) {
197b87d8561SStephen Hemminger 		/* Do the Vegas once-per-RTT cwnd adjustment. */
198b87d8561SStephen Hemminger 		u32 old_wnd, old_snd_cwnd;
199b87d8561SStephen Hemminger 
200b87d8561SStephen Hemminger 
201b87d8561SStephen Hemminger 		/* Here old_wnd is essentially the window of data that was
202b87d8561SStephen Hemminger 		 * sent during the previous RTT, and has all
203b87d8561SStephen Hemminger 		 * been acknowledged in the course of the RTT that ended
204b87d8561SStephen Hemminger 		 * with the ACK we just received. Likewise, old_snd_cwnd
205b87d8561SStephen Hemminger 		 * is the cwnd during the previous RTT.
206b87d8561SStephen Hemminger 		 */
207b87d8561SStephen Hemminger 		old_wnd = (vegas->beg_snd_nxt - vegas->beg_snd_una) /
208b87d8561SStephen Hemminger 			tp->mss_cache;
209b87d8561SStephen Hemminger 		old_snd_cwnd = vegas->beg_snd_cwnd;
210b87d8561SStephen Hemminger 
211b87d8561SStephen Hemminger 		/* Save the extent of the current window so we can use this
212b87d8561SStephen Hemminger 		 * at the end of the next RTT.
213b87d8561SStephen Hemminger 		 */
214b87d8561SStephen Hemminger 		vegas->beg_snd_una  = vegas->beg_snd_nxt;
215b87d8561SStephen Hemminger 		vegas->beg_snd_nxt  = tp->snd_nxt;
216b87d8561SStephen Hemminger 		vegas->beg_snd_cwnd = tp->snd_cwnd;
217b87d8561SStephen Hemminger 
218b87d8561SStephen Hemminger 		/* Take into account the current RTT sample too, to
219b87d8561SStephen Hemminger 		 * decrease the impact of delayed acks. This double counts
220b87d8561SStephen Hemminger 		 * this sample since we count it for the next window as well,
221b87d8561SStephen Hemminger 		 * but that's not too awful, since we're taking the min,
222b87d8561SStephen Hemminger 		 * rather than averaging.
223b87d8561SStephen Hemminger 		 */
224*6687e988SArnaldo Carvalho de Melo 		tcp_vegas_rtt_calc(sk, seq_rtt * 1000);
225b87d8561SStephen Hemminger 
226b87d8561SStephen Hemminger 		/* We do the Vegas calculations only if we got enough RTT
227b87d8561SStephen Hemminger 		 * samples that we can be reasonably sure that we got
228b87d8561SStephen Hemminger 		 * at least one RTT sample that wasn't from a delayed ACK.
229b87d8561SStephen Hemminger 		 * If we only had 2 samples total,
230b87d8561SStephen Hemminger 		 * then that means we're getting only 1 ACK per RTT, which
231b87d8561SStephen Hemminger 		 * means they're almost certainly delayed ACKs.
232b87d8561SStephen Hemminger 		 * If  we have 3 samples, we should be OK.
233b87d8561SStephen Hemminger 		 */
234b87d8561SStephen Hemminger 
235b87d8561SStephen Hemminger 		if (vegas->cntRTT <= 2) {
236b87d8561SStephen Hemminger 			/* We don't have enough RTT samples to do the Vegas
237b87d8561SStephen Hemminger 			 * calculation, so we'll behave like Reno.
238b87d8561SStephen Hemminger 			 */
239b87d8561SStephen Hemminger 			if (tp->snd_cwnd > tp->snd_ssthresh)
240b87d8561SStephen Hemminger 				tp->snd_cwnd++;
241b87d8561SStephen Hemminger 		} else {
242b87d8561SStephen Hemminger 			u32 rtt, target_cwnd, diff;
243b87d8561SStephen Hemminger 
244b87d8561SStephen Hemminger 			/* We have enough RTT samples, so, using the Vegas
245b87d8561SStephen Hemminger 			 * algorithm, we determine if we should increase or
246b87d8561SStephen Hemminger 			 * decrease cwnd, and by how much.
247b87d8561SStephen Hemminger 			 */
248b87d8561SStephen Hemminger 
249b87d8561SStephen Hemminger 			/* Pluck out the RTT we are using for the Vegas
250b87d8561SStephen Hemminger 			 * calculations. This is the min RTT seen during the
251b87d8561SStephen Hemminger 			 * last RTT. Taking the min filters out the effects
252b87d8561SStephen Hemminger 			 * of delayed ACKs, at the cost of noticing congestion
253b87d8561SStephen Hemminger 			 * a bit later.
254b87d8561SStephen Hemminger 			 */
255b87d8561SStephen Hemminger 			rtt = vegas->minRTT;
256b87d8561SStephen Hemminger 
257b87d8561SStephen Hemminger 			/* Calculate the cwnd we should have, if we weren't
258b87d8561SStephen Hemminger 			 * going too fast.
259b87d8561SStephen Hemminger 			 *
260b87d8561SStephen Hemminger 			 * This is:
261b87d8561SStephen Hemminger 			 *     (actual rate in segments) * baseRTT
262b87d8561SStephen Hemminger 			 * We keep it as a fixed point number with
263b87d8561SStephen Hemminger 			 * V_PARAM_SHIFT bits to the right of the binary point.
264b87d8561SStephen Hemminger 			 */
265b87d8561SStephen Hemminger 			target_cwnd = ((old_wnd * vegas->baseRTT)
266b87d8561SStephen Hemminger 				       << V_PARAM_SHIFT) / rtt;
267b87d8561SStephen Hemminger 
268b87d8561SStephen Hemminger 			/* Calculate the difference between the window we had,
269b87d8561SStephen Hemminger 			 * and the window we would like to have. This quantity
270b87d8561SStephen Hemminger 			 * is the "Diff" from the Arizona Vegas papers.
271b87d8561SStephen Hemminger 			 *
272b87d8561SStephen Hemminger 			 * Again, this is a fixed point number with
273b87d8561SStephen Hemminger 			 * V_PARAM_SHIFT bits to the right of the binary
274b87d8561SStephen Hemminger 			 * point.
275b87d8561SStephen Hemminger 			 */
276b87d8561SStephen Hemminger 			diff = (old_wnd << V_PARAM_SHIFT) - target_cwnd;
277b87d8561SStephen Hemminger 
278b87d8561SStephen Hemminger 			if (tp->snd_cwnd < tp->snd_ssthresh) {
279b87d8561SStephen Hemminger 				/* Slow start.  */
280b87d8561SStephen Hemminger 				if (diff > gamma) {
281b87d8561SStephen Hemminger 					/* Going too fast. Time to slow down
282b87d8561SStephen Hemminger 					 * and switch to congestion avoidance.
283b87d8561SStephen Hemminger 					 */
284b87d8561SStephen Hemminger 					tp->snd_ssthresh = 2;
285b87d8561SStephen Hemminger 
286b87d8561SStephen Hemminger 					/* Set cwnd to match the actual rate
287b87d8561SStephen Hemminger 					 * exactly:
288b87d8561SStephen Hemminger 					 *   cwnd = (actual rate) * baseRTT
289b87d8561SStephen Hemminger 					 * Then we add 1 because the integer
290b87d8561SStephen Hemminger 					 * truncation robs us of full link
291b87d8561SStephen Hemminger 					 * utilization.
292b87d8561SStephen Hemminger 					 */
293b87d8561SStephen Hemminger 					tp->snd_cwnd = min(tp->snd_cwnd,
294b87d8561SStephen Hemminger 							   (target_cwnd >>
295b87d8561SStephen Hemminger 							    V_PARAM_SHIFT)+1);
296b87d8561SStephen Hemminger 
297b87d8561SStephen Hemminger 				}
298b87d8561SStephen Hemminger 			} else {
299b87d8561SStephen Hemminger 				/* Congestion avoidance. */
300b87d8561SStephen Hemminger 				u32 next_snd_cwnd;
301b87d8561SStephen Hemminger 
302b87d8561SStephen Hemminger 				/* Figure out where we would like cwnd
303b87d8561SStephen Hemminger 				 * to be.
304b87d8561SStephen Hemminger 				 */
305b87d8561SStephen Hemminger 				if (diff > beta) {
306b87d8561SStephen Hemminger 					/* The old window was too fast, so
307b87d8561SStephen Hemminger 					 * we slow down.
308b87d8561SStephen Hemminger 					 */
309b87d8561SStephen Hemminger 					next_snd_cwnd = old_snd_cwnd - 1;
310b87d8561SStephen Hemminger 				} else if (diff < alpha) {
311b87d8561SStephen Hemminger 					/* We don't have enough extra packets
312b87d8561SStephen Hemminger 					 * in the network, so speed up.
313b87d8561SStephen Hemminger 					 */
314b87d8561SStephen Hemminger 					next_snd_cwnd = old_snd_cwnd + 1;
315b87d8561SStephen Hemminger 				} else {
316b87d8561SStephen Hemminger 					/* Sending just as fast as we
317b87d8561SStephen Hemminger 					 * should be.
318b87d8561SStephen Hemminger 					 */
319b87d8561SStephen Hemminger 					next_snd_cwnd = old_snd_cwnd;
320b87d8561SStephen Hemminger 				}
321b87d8561SStephen Hemminger 
322b87d8561SStephen Hemminger 				/* Adjust cwnd upward or downward, toward the
323b87d8561SStephen Hemminger 				 * desired value.
324b87d8561SStephen Hemminger 				 */
325b87d8561SStephen Hemminger 				if (next_snd_cwnd > tp->snd_cwnd)
326b87d8561SStephen Hemminger 					tp->snd_cwnd++;
327b87d8561SStephen Hemminger 				else if (next_snd_cwnd < tp->snd_cwnd)
328b87d8561SStephen Hemminger 					tp->snd_cwnd--;
329b87d8561SStephen Hemminger 			}
330b87d8561SStephen Hemminger 		}
331b87d8561SStephen Hemminger 
332b87d8561SStephen Hemminger 		/* Wipe the slate clean for the next RTT. */
333b87d8561SStephen Hemminger 		vegas->cntRTT = 0;
334b87d8561SStephen Hemminger 		vegas->minRTT = 0x7fffffff;
335b87d8561SStephen Hemminger 	}
336b87d8561SStephen Hemminger 
337b87d8561SStephen Hemminger 	/* The following code is executed for every ack we receive,
338b87d8561SStephen Hemminger 	 * except for conditions checked in should_advance_cwnd()
339b87d8561SStephen Hemminger 	 * before the call to tcp_cong_avoid(). Mainly this means that
340b87d8561SStephen Hemminger 	 * we only execute this code if the ack actually acked some
341b87d8561SStephen Hemminger 	 * data.
342b87d8561SStephen Hemminger 	 */
343b87d8561SStephen Hemminger 
344b87d8561SStephen Hemminger 	/* If we are in slow start, increase our cwnd in response to this ACK.
345b87d8561SStephen Hemminger 	 * (If we are not in slow start then we are in congestion avoidance,
346b87d8561SStephen Hemminger 	 * and adjust our congestion window only once per RTT. See the code
347b87d8561SStephen Hemminger 	 * above.)
348b87d8561SStephen Hemminger 	 */
349b87d8561SStephen Hemminger 	if (tp->snd_cwnd <= tp->snd_ssthresh)
350b87d8561SStephen Hemminger 		tp->snd_cwnd++;
351b87d8561SStephen Hemminger 
352b87d8561SStephen Hemminger 	/* to keep cwnd from growing without bound */
353b87d8561SStephen Hemminger 	tp->snd_cwnd = min_t(u32, tp->snd_cwnd, tp->snd_cwnd_clamp);
354b87d8561SStephen Hemminger 
355b87d8561SStephen Hemminger 	/* Make sure that we are never so timid as to reduce our cwnd below
356b87d8561SStephen Hemminger 	 * 2 MSS.
357b87d8561SStephen Hemminger 	 *
358b87d8561SStephen Hemminger 	 * Going below 2 MSS would risk huge delayed ACKs from our receiver.
359b87d8561SStephen Hemminger 	 */
360b87d8561SStephen Hemminger 	tp->snd_cwnd = max(tp->snd_cwnd, 2U);
361b87d8561SStephen Hemminger }
362b87d8561SStephen Hemminger 
363b87d8561SStephen Hemminger /* Extract info for Tcp socket info provided via netlink. */
364*6687e988SArnaldo Carvalho de Melo static void tcp_vegas_get_info(struct sock *sk, u32 ext,
365b87d8561SStephen Hemminger 			       struct sk_buff *skb)
366b87d8561SStephen Hemminger {
367*6687e988SArnaldo Carvalho de Melo 	const struct vegas *ca = inet_csk_ca(sk);
368b87d8561SStephen Hemminger 	if (ext & (1<<(TCPDIAG_VEGASINFO-1))) {
369b87d8561SStephen Hemminger 		struct tcpvegas_info *info;
370b87d8561SStephen Hemminger 
371b87d8561SStephen Hemminger 		info = RTA_DATA(__RTA_PUT(skb, TCPDIAG_VEGASINFO,
372b87d8561SStephen Hemminger 					  sizeof(*info)));
373b87d8561SStephen Hemminger 
374b87d8561SStephen Hemminger 		info->tcpv_enabled = ca->doing_vegas_now;
375b87d8561SStephen Hemminger 		info->tcpv_rttcnt = ca->cntRTT;
376b87d8561SStephen Hemminger 		info->tcpv_rtt = ca->baseRTT;
377b87d8561SStephen Hemminger 		info->tcpv_minrtt = ca->minRTT;
378b87d8561SStephen Hemminger 	rtattr_failure:	;
379b87d8561SStephen Hemminger 	}
380b87d8561SStephen Hemminger }
381b87d8561SStephen Hemminger 
382b87d8561SStephen Hemminger static struct tcp_congestion_ops tcp_vegas = {
383b87d8561SStephen Hemminger 	.init		= tcp_vegas_init,
384b87d8561SStephen Hemminger 	.ssthresh	= tcp_reno_ssthresh,
385b87d8561SStephen Hemminger 	.cong_avoid	= tcp_vegas_cong_avoid,
386b87d8561SStephen Hemminger 	.min_cwnd	= tcp_reno_min_cwnd,
387b87d8561SStephen Hemminger 	.rtt_sample	= tcp_vegas_rtt_calc,
388b87d8561SStephen Hemminger 	.set_state	= tcp_vegas_state,
389b87d8561SStephen Hemminger 	.cwnd_event	= tcp_vegas_cwnd_event,
390b87d8561SStephen Hemminger 	.get_info	= tcp_vegas_get_info,
391b87d8561SStephen Hemminger 
392b87d8561SStephen Hemminger 	.owner		= THIS_MODULE,
393b87d8561SStephen Hemminger 	.name		= "vegas",
394b87d8561SStephen Hemminger };
395b87d8561SStephen Hemminger 
396b87d8561SStephen Hemminger static int __init tcp_vegas_register(void)
397b87d8561SStephen Hemminger {
398*6687e988SArnaldo Carvalho de Melo 	BUG_ON(sizeof(struct vegas) > ICSK_CA_PRIV_SIZE);
399b87d8561SStephen Hemminger 	tcp_register_congestion_control(&tcp_vegas);
400b87d8561SStephen Hemminger 	return 0;
401b87d8561SStephen Hemminger }
402b87d8561SStephen Hemminger 
403b87d8561SStephen Hemminger static void __exit tcp_vegas_unregister(void)
404b87d8561SStephen Hemminger {
405b87d8561SStephen Hemminger 	tcp_unregister_congestion_control(&tcp_vegas);
406b87d8561SStephen Hemminger }
407b87d8561SStephen Hemminger 
408b87d8561SStephen Hemminger module_init(tcp_vegas_register);
409b87d8561SStephen Hemminger module_exit(tcp_vegas_unregister);
410b87d8561SStephen Hemminger 
411b87d8561SStephen Hemminger MODULE_AUTHOR("Stephen Hemminger");
412b87d8561SStephen Hemminger MODULE_LICENSE("GPL");
413b87d8561SStephen Hemminger MODULE_DESCRIPTION("TCP Vegas");
414