xref: /OK3568_Linux_fs/kernel/net/ipv4/tcp_vegas.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * TCP Vegas congestion control
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This is based on the congestion detection/avoidance scheme described in
6*4882a593Smuzhiyun  *    Lawrence S. Brakmo and Larry L. Peterson.
7*4882a593Smuzhiyun  *    "TCP Vegas: End to end congestion avoidance on a global internet."
8*4882a593Smuzhiyun  *    IEEE Journal on Selected Areas in Communication, 13(8):1465--1480,
9*4882a593Smuzhiyun  *    October 1995. Available from:
10*4882a593Smuzhiyun  *	ftp://ftp.cs.arizona.edu/xkernel/Papers/jsac.ps
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * See http://www.cs.arizona.edu/xkernel/ for their implementation.
13*4882a593Smuzhiyun  * The main aspects that distinguish this implementation from the
14*4882a593Smuzhiyun  * Arizona Vegas implementation are:
15*4882a593Smuzhiyun  *   o We do not change the loss detection or recovery mechanisms of
16*4882a593Smuzhiyun  *     Linux in any way. Linux already recovers from losses quite well,
17*4882a593Smuzhiyun  *     using fine-grained timers, NewReno, and FACK.
18*4882a593Smuzhiyun  *   o To avoid the performance penalty imposed by increasing cwnd
19*4882a593Smuzhiyun  *     only every-other RTT during slow start, we increase during
20*4882a593Smuzhiyun  *     every RTT during slow start, just like Reno.
21*4882a593Smuzhiyun  *   o Largely to allow continuous cwnd growth during slow start,
22*4882a593Smuzhiyun  *     we use the rate at which ACKs come back as the "actual"
23*4882a593Smuzhiyun  *     rate, rather than the rate at which data is sent.
24*4882a593Smuzhiyun  *   o To speed convergence to the right rate, we set the cwnd
25*4882a593Smuzhiyun  *     to achieve the right ("actual") rate when we exit slow start.
26*4882a593Smuzhiyun  *   o To filter out the noise caused by delayed ACKs, we use the
27*4882a593Smuzhiyun  *     minimum RTT sample observed during the last RTT to calculate
28*4882a593Smuzhiyun  *     the actual rate.
29*4882a593Smuzhiyun  *   o When the sender re-starts from idle, it waits until it has
30*4882a593Smuzhiyun  *     received ACKs for an entire flight of new data before making
31*4882a593Smuzhiyun  *     a cwnd adjustment decision. The original Vegas implementation
32*4882a593Smuzhiyun  *     assumed senders never went idle.
33*4882a593Smuzhiyun  */
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #include <linux/mm.h>
36*4882a593Smuzhiyun #include <linux/module.h>
37*4882a593Smuzhiyun #include <linux/skbuff.h>
38*4882a593Smuzhiyun #include <linux/inet_diag.h>
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #include <net/tcp.h>
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #include "tcp_vegas.h"
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun static int alpha = 2;
45*4882a593Smuzhiyun static int beta  = 4;
46*4882a593Smuzhiyun static int gamma = 1;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun module_param(alpha, int, 0644);
49*4882a593Smuzhiyun MODULE_PARM_DESC(alpha, "lower bound of packets in network");
50*4882a593Smuzhiyun module_param(beta, int, 0644);
51*4882a593Smuzhiyun MODULE_PARM_DESC(beta, "upper bound of packets in network");
52*4882a593Smuzhiyun module_param(gamma, int, 0644);
53*4882a593Smuzhiyun MODULE_PARM_DESC(gamma, "limit on increase (scale by 2)");
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun /* There are several situations when we must "re-start" Vegas:
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  *  o when a connection is established
58*4882a593Smuzhiyun  *  o after an RTO
59*4882a593Smuzhiyun  *  o after fast recovery
60*4882a593Smuzhiyun  *  o when we send a packet and there is no outstanding
61*4882a593Smuzhiyun  *    unacknowledged data (restarting an idle connection)
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * In these circumstances we cannot do a Vegas calculation at the
64*4882a593Smuzhiyun  * end of the first RTT, because any calculation we do is using
65*4882a593Smuzhiyun  * stale info -- both the saved cwnd and congestion feedback are
66*4882a593Smuzhiyun  * stale.
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  * Instead we must wait until the completion of an RTT during
69*4882a593Smuzhiyun  * which we actually receive ACKs.
70*4882a593Smuzhiyun  */
vegas_enable(struct sock * sk)71*4882a593Smuzhiyun static void vegas_enable(struct sock *sk)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 	const struct tcp_sock *tp = tcp_sk(sk);
74*4882a593Smuzhiyun 	struct vegas *vegas = inet_csk_ca(sk);
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	/* Begin taking Vegas samples next time we send something. */
77*4882a593Smuzhiyun 	vegas->doing_vegas_now = 1;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	/* Set the beginning of the next send window. */
80*4882a593Smuzhiyun 	vegas->beg_snd_nxt = tp->snd_nxt;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	vegas->cntRTT = 0;
83*4882a593Smuzhiyun 	vegas->minRTT = 0x7fffffff;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun /* Stop taking Vegas samples for now. */
vegas_disable(struct sock * sk)87*4882a593Smuzhiyun static inline void vegas_disable(struct sock *sk)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	struct vegas *vegas = inet_csk_ca(sk);
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	vegas->doing_vegas_now = 0;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun 
tcp_vegas_init(struct sock * sk)94*4882a593Smuzhiyun void tcp_vegas_init(struct sock *sk)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun 	struct vegas *vegas = inet_csk_ca(sk);
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	vegas->baseRTT = 0x7fffffff;
99*4882a593Smuzhiyun 	vegas_enable(sk);
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tcp_vegas_init);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun /* Do RTT sampling needed for Vegas.
104*4882a593Smuzhiyun  * Basically we:
105*4882a593Smuzhiyun  *   o min-filter RTT samples from within an RTT to get the current
106*4882a593Smuzhiyun  *     propagation delay + queuing delay (we are min-filtering to try to
107*4882a593Smuzhiyun  *     avoid the effects of delayed ACKs)
108*4882a593Smuzhiyun  *   o min-filter RTT samples from a much longer window (forever for now)
109*4882a593Smuzhiyun  *     to find the propagation delay (baseRTT)
110*4882a593Smuzhiyun  */
tcp_vegas_pkts_acked(struct sock * sk,const struct ack_sample * sample)111*4882a593Smuzhiyun void tcp_vegas_pkts_acked(struct sock *sk, const struct ack_sample *sample)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun 	struct vegas *vegas = inet_csk_ca(sk);
114*4882a593Smuzhiyun 	u32 vrtt;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	if (sample->rtt_us < 0)
117*4882a593Smuzhiyun 		return;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	/* Never allow zero rtt or baseRTT */
120*4882a593Smuzhiyun 	vrtt = sample->rtt_us + 1;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	/* Filter to find propagation delay: */
123*4882a593Smuzhiyun 	if (vrtt < vegas->baseRTT)
124*4882a593Smuzhiyun 		vegas->baseRTT = vrtt;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	/* Find the min RTT during the last RTT to find
127*4882a593Smuzhiyun 	 * the current prop. delay + queuing delay:
128*4882a593Smuzhiyun 	 */
129*4882a593Smuzhiyun 	vegas->minRTT = min(vegas->minRTT, vrtt);
130*4882a593Smuzhiyun 	vegas->cntRTT++;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tcp_vegas_pkts_acked);
133*4882a593Smuzhiyun 
tcp_vegas_state(struct sock * sk,u8 ca_state)134*4882a593Smuzhiyun void tcp_vegas_state(struct sock *sk, u8 ca_state)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	if (ca_state == TCP_CA_Open)
137*4882a593Smuzhiyun 		vegas_enable(sk);
138*4882a593Smuzhiyun 	else
139*4882a593Smuzhiyun 		vegas_disable(sk);
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tcp_vegas_state);
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun /*
144*4882a593Smuzhiyun  * If the connection is idle and we are restarting,
145*4882a593Smuzhiyun  * then we don't want to do any Vegas calculations
146*4882a593Smuzhiyun  * until we get fresh RTT samples.  So when we
147*4882a593Smuzhiyun  * restart, we reset our Vegas state to a clean
148*4882a593Smuzhiyun  * slate. After we get acks for this flight of
149*4882a593Smuzhiyun  * packets, _then_ we can make Vegas calculations
150*4882a593Smuzhiyun  * again.
151*4882a593Smuzhiyun  */
tcp_vegas_cwnd_event(struct sock * sk,enum tcp_ca_event event)152*4882a593Smuzhiyun void tcp_vegas_cwnd_event(struct sock *sk, enum tcp_ca_event event)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun 	if (event == CA_EVENT_CWND_RESTART ||
155*4882a593Smuzhiyun 	    event == CA_EVENT_TX_START)
156*4882a593Smuzhiyun 		tcp_vegas_init(sk);
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tcp_vegas_cwnd_event);
159*4882a593Smuzhiyun 
tcp_vegas_ssthresh(struct tcp_sock * tp)160*4882a593Smuzhiyun static inline u32 tcp_vegas_ssthresh(struct tcp_sock *tp)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun 	return  min(tp->snd_ssthresh, tp->snd_cwnd);
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun 
tcp_vegas_cong_avoid(struct sock * sk,u32 ack,u32 acked)165*4882a593Smuzhiyun static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack, u32 acked)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
168*4882a593Smuzhiyun 	struct vegas *vegas = inet_csk_ca(sk);
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	if (!vegas->doing_vegas_now) {
171*4882a593Smuzhiyun 		tcp_reno_cong_avoid(sk, ack, acked);
172*4882a593Smuzhiyun 		return;
173*4882a593Smuzhiyun 	}
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	if (after(ack, vegas->beg_snd_nxt)) {
176*4882a593Smuzhiyun 		/* Do the Vegas once-per-RTT cwnd adjustment. */
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 		/* Save the extent of the current window so we can use this
179*4882a593Smuzhiyun 		 * at the end of the next RTT.
180*4882a593Smuzhiyun 		 */
181*4882a593Smuzhiyun 		vegas->beg_snd_nxt  = tp->snd_nxt;
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 		/* We do the Vegas calculations only if we got enough RTT
184*4882a593Smuzhiyun 		 * samples that we can be reasonably sure that we got
185*4882a593Smuzhiyun 		 * at least one RTT sample that wasn't from a delayed ACK.
186*4882a593Smuzhiyun 		 * If we only had 2 samples total,
187*4882a593Smuzhiyun 		 * then that means we're getting only 1 ACK per RTT, which
188*4882a593Smuzhiyun 		 * means they're almost certainly delayed ACKs.
189*4882a593Smuzhiyun 		 * If  we have 3 samples, we should be OK.
190*4882a593Smuzhiyun 		 */
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 		if (vegas->cntRTT <= 2) {
193*4882a593Smuzhiyun 			/* We don't have enough RTT samples to do the Vegas
194*4882a593Smuzhiyun 			 * calculation, so we'll behave like Reno.
195*4882a593Smuzhiyun 			 */
196*4882a593Smuzhiyun 			tcp_reno_cong_avoid(sk, ack, acked);
197*4882a593Smuzhiyun 		} else {
198*4882a593Smuzhiyun 			u32 rtt, diff;
199*4882a593Smuzhiyun 			u64 target_cwnd;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 			/* We have enough RTT samples, so, using the Vegas
202*4882a593Smuzhiyun 			 * algorithm, we determine if we should increase or
203*4882a593Smuzhiyun 			 * decrease cwnd, and by how much.
204*4882a593Smuzhiyun 			 */
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 			/* Pluck out the RTT we are using for the Vegas
207*4882a593Smuzhiyun 			 * calculations. This is the min RTT seen during the
208*4882a593Smuzhiyun 			 * last RTT. Taking the min filters out the effects
209*4882a593Smuzhiyun 			 * of delayed ACKs, at the cost of noticing congestion
210*4882a593Smuzhiyun 			 * a bit later.
211*4882a593Smuzhiyun 			 */
212*4882a593Smuzhiyun 			rtt = vegas->minRTT;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 			/* Calculate the cwnd we should have, if we weren't
215*4882a593Smuzhiyun 			 * going too fast.
216*4882a593Smuzhiyun 			 *
217*4882a593Smuzhiyun 			 * This is:
218*4882a593Smuzhiyun 			 *     (actual rate in segments) * baseRTT
219*4882a593Smuzhiyun 			 */
220*4882a593Smuzhiyun 			target_cwnd = (u64)tp->snd_cwnd * vegas->baseRTT;
221*4882a593Smuzhiyun 			do_div(target_cwnd, rtt);
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 			/* Calculate the difference between the window we had,
224*4882a593Smuzhiyun 			 * and the window we would like to have. This quantity
225*4882a593Smuzhiyun 			 * is the "Diff" from the Arizona Vegas papers.
226*4882a593Smuzhiyun 			 */
227*4882a593Smuzhiyun 			diff = tp->snd_cwnd * (rtt-vegas->baseRTT) / vegas->baseRTT;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 			if (diff > gamma && tcp_in_slow_start(tp)) {
230*4882a593Smuzhiyun 				/* Going too fast. Time to slow down
231*4882a593Smuzhiyun 				 * and switch to congestion avoidance.
232*4882a593Smuzhiyun 				 */
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 				/* Set cwnd to match the actual rate
235*4882a593Smuzhiyun 				 * exactly:
236*4882a593Smuzhiyun 				 *   cwnd = (actual rate) * baseRTT
237*4882a593Smuzhiyun 				 * Then we add 1 because the integer
238*4882a593Smuzhiyun 				 * truncation robs us of full link
239*4882a593Smuzhiyun 				 * utilization.
240*4882a593Smuzhiyun 				 */
241*4882a593Smuzhiyun 				tp->snd_cwnd = min(tp->snd_cwnd, (u32)target_cwnd+1);
242*4882a593Smuzhiyun 				tp->snd_ssthresh = tcp_vegas_ssthresh(tp);
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 			} else if (tcp_in_slow_start(tp)) {
245*4882a593Smuzhiyun 				/* Slow start.  */
246*4882a593Smuzhiyun 				tcp_slow_start(tp, acked);
247*4882a593Smuzhiyun 			} else {
248*4882a593Smuzhiyun 				/* Congestion avoidance. */
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 				/* Figure out where we would like cwnd
251*4882a593Smuzhiyun 				 * to be.
252*4882a593Smuzhiyun 				 */
253*4882a593Smuzhiyun 				if (diff > beta) {
254*4882a593Smuzhiyun 					/* The old window was too fast, so
255*4882a593Smuzhiyun 					 * we slow down.
256*4882a593Smuzhiyun 					 */
257*4882a593Smuzhiyun 					tp->snd_cwnd--;
258*4882a593Smuzhiyun 					tp->snd_ssthresh
259*4882a593Smuzhiyun 						= tcp_vegas_ssthresh(tp);
260*4882a593Smuzhiyun 				} else if (diff < alpha) {
261*4882a593Smuzhiyun 					/* We don't have enough extra packets
262*4882a593Smuzhiyun 					 * in the network, so speed up.
263*4882a593Smuzhiyun 					 */
264*4882a593Smuzhiyun 					tp->snd_cwnd++;
265*4882a593Smuzhiyun 				} else {
266*4882a593Smuzhiyun 					/* Sending just as fast as we
267*4882a593Smuzhiyun 					 * should be.
268*4882a593Smuzhiyun 					 */
269*4882a593Smuzhiyun 				}
270*4882a593Smuzhiyun 			}
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 			if (tp->snd_cwnd < 2)
273*4882a593Smuzhiyun 				tp->snd_cwnd = 2;
274*4882a593Smuzhiyun 			else if (tp->snd_cwnd > tp->snd_cwnd_clamp)
275*4882a593Smuzhiyun 				tp->snd_cwnd = tp->snd_cwnd_clamp;
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 			tp->snd_ssthresh = tcp_current_ssthresh(sk);
278*4882a593Smuzhiyun 		}
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 		/* Wipe the slate clean for the next RTT. */
281*4882a593Smuzhiyun 		vegas->cntRTT = 0;
282*4882a593Smuzhiyun 		vegas->minRTT = 0x7fffffff;
283*4882a593Smuzhiyun 	}
284*4882a593Smuzhiyun 	/* Use normal slow start */
285*4882a593Smuzhiyun 	else if (tcp_in_slow_start(tp))
286*4882a593Smuzhiyun 		tcp_slow_start(tp, acked);
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun /* Extract info for Tcp socket info provided via netlink. */
tcp_vegas_get_info(struct sock * sk,u32 ext,int * attr,union tcp_cc_info * info)290*4882a593Smuzhiyun size_t tcp_vegas_get_info(struct sock *sk, u32 ext, int *attr,
291*4882a593Smuzhiyun 			  union tcp_cc_info *info)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	const struct vegas *ca = inet_csk_ca(sk);
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
296*4882a593Smuzhiyun 		info->vegas.tcpv_enabled = ca->doing_vegas_now;
297*4882a593Smuzhiyun 		info->vegas.tcpv_rttcnt = ca->cntRTT;
298*4882a593Smuzhiyun 		info->vegas.tcpv_rtt = ca->baseRTT;
299*4882a593Smuzhiyun 		info->vegas.tcpv_minrtt = ca->minRTT;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 		*attr = INET_DIAG_VEGASINFO;
302*4882a593Smuzhiyun 		return sizeof(struct tcpvegas_info);
303*4882a593Smuzhiyun 	}
304*4882a593Smuzhiyun 	return 0;
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(tcp_vegas_get_info);
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun static struct tcp_congestion_ops tcp_vegas __read_mostly = {
309*4882a593Smuzhiyun 	.init		= tcp_vegas_init,
310*4882a593Smuzhiyun 	.ssthresh	= tcp_reno_ssthresh,
311*4882a593Smuzhiyun 	.undo_cwnd	= tcp_reno_undo_cwnd,
312*4882a593Smuzhiyun 	.cong_avoid	= tcp_vegas_cong_avoid,
313*4882a593Smuzhiyun 	.pkts_acked	= tcp_vegas_pkts_acked,
314*4882a593Smuzhiyun 	.set_state	= tcp_vegas_state,
315*4882a593Smuzhiyun 	.cwnd_event	= tcp_vegas_cwnd_event,
316*4882a593Smuzhiyun 	.get_info	= tcp_vegas_get_info,
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
319*4882a593Smuzhiyun 	.name		= "vegas",
320*4882a593Smuzhiyun };
321*4882a593Smuzhiyun 
tcp_vegas_register(void)322*4882a593Smuzhiyun static int __init tcp_vegas_register(void)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun 	BUILD_BUG_ON(sizeof(struct vegas) > ICSK_CA_PRIV_SIZE);
325*4882a593Smuzhiyun 	tcp_register_congestion_control(&tcp_vegas);
326*4882a593Smuzhiyun 	return 0;
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun 
tcp_vegas_unregister(void)329*4882a593Smuzhiyun static void __exit tcp_vegas_unregister(void)
330*4882a593Smuzhiyun {
331*4882a593Smuzhiyun 	tcp_unregister_congestion_control(&tcp_vegas);
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun module_init(tcp_vegas_register);
335*4882a593Smuzhiyun module_exit(tcp_vegas_unregister);
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun MODULE_AUTHOR("Stephen Hemminger");
338*4882a593Smuzhiyun MODULE_LICENSE("GPL");
339*4882a593Smuzhiyun MODULE_DESCRIPTION("TCP Vegas");
340