xref: /linux/net/ipv4/tcp_yeah.c (revision ab520be8cd5d56867fc95cfbc34b90880faf1f9d)
1 /*
2  *
3  *   YeAH TCP
4  *
5  * For further details look at:
6  *   https://web.archive.org/web/20080316215752/http://wil.cs.caltech.edu/pfldnet2007/paper/YeAH_TCP.pdf
7  *
8  */
9 #include <linux/mm.h>
10 #include <linux/module.h>
11 #include <linux/skbuff.h>
12 #include <linux/inet_diag.h>
13 
14 #include <net/tcp.h>
15 
16 #include "tcp_vegas.h"
17 
18 #define TCP_YEAH_ALPHA       80 /* number of packets queued at the bottleneck */
19 #define TCP_YEAH_GAMMA        1 /* fraction of queue to be removed per rtt */
20 #define TCP_YEAH_DELTA        3 /* log minimum fraction of cwnd to be removed on loss */
21 #define TCP_YEAH_EPSILON      1 /* log maximum fraction to be removed on early decongestion */
22 #define TCP_YEAH_PHY          8 /* maximum delta from base */
23 #define TCP_YEAH_RHO         16 /* minimum number of consecutive rtt to consider competition on loss */
24 #define TCP_YEAH_ZETA        50 /* minimum number of state switches to reset reno_count */
25 
26 #define TCP_SCALABLE_AI_CNT	 100U
27 
28 /* YeAH variables */
29 struct yeah {
30 	struct vegas vegas;	/* must be first */
31 
32 	/* YeAH */
33 	u32 lastQ;
34 	u32 doing_reno_now;
35 
36 	u32 reno_count;
37 	u32 fast_count;
38 
39 	u32 pkts_acked;
40 	u32 loss_cwnd;
41 };
42 
43 static void tcp_yeah_init(struct sock *sk)
44 {
45 	struct tcp_sock *tp = tcp_sk(sk);
46 	struct yeah *yeah = inet_csk_ca(sk);
47 
48 	tcp_vegas_init(sk);
49 
50 	yeah->doing_reno_now = 0;
51 	yeah->lastQ = 0;
52 
53 	yeah->reno_count = 2;
54 
55 	/* Ensure the MD arithmetic works.  This is somewhat pedantic,
56 	 * since I don't think we will see a cwnd this large. :) */
57 	tp->snd_cwnd_clamp = min_t(u32, tp->snd_cwnd_clamp, 0xffffffff/128);
58 }
59 
60 static void tcp_yeah_pkts_acked(struct sock *sk,
61 				const struct ack_sample *sample)
62 {
63 	const struct inet_connection_sock *icsk = inet_csk(sk);
64 	struct yeah *yeah = inet_csk_ca(sk);
65 
66 	if (icsk->icsk_ca_state == TCP_CA_Open)
67 		yeah->pkts_acked = sample->pkts_acked;
68 
69 	tcp_vegas_pkts_acked(sk, sample);
70 }
71 
72 static void tcp_yeah_cong_avoid(struct sock *sk, u32 ack, u32 acked)
73 {
74 	struct tcp_sock *tp = tcp_sk(sk);
75 	struct yeah *yeah = inet_csk_ca(sk);
76 
77 	if (!tcp_is_cwnd_limited(sk))
78 		return;
79 
80 	if (tcp_in_slow_start(tp))
81 		tcp_slow_start(tp, acked);
82 
83 	else if (!yeah->doing_reno_now) {
84 		/* Scalable */
85 
86 		tp->snd_cwnd_cnt += yeah->pkts_acked;
87 		if (tp->snd_cwnd_cnt > min(tp->snd_cwnd, TCP_SCALABLE_AI_CNT)) {
88 			if (tp->snd_cwnd < tp->snd_cwnd_clamp)
89 				tp->snd_cwnd++;
90 			tp->snd_cwnd_cnt = 0;
91 		}
92 
93 		yeah->pkts_acked = 1;
94 
95 	} else {
96 		/* Reno */
97 		tcp_cong_avoid_ai(tp, tp->snd_cwnd, 1);
98 	}
99 
100 	/* The key players are v_vegas.beg_snd_una and v_beg_snd_nxt.
101 	 *
102 	 * These are so named because they represent the approximate values
103 	 * of snd_una and snd_nxt at the beginning of the current RTT. More
104 	 * precisely, they represent the amount of data sent during the RTT.
105 	 * At the end of the RTT, when we receive an ACK for v_beg_snd_nxt,
106 	 * we will calculate that (v_beg_snd_nxt - v_vegas.beg_snd_una) outstanding
107 	 * bytes of data have been ACKed during the course of the RTT, giving
108 	 * an "actual" rate of:
109 	 *
110 	 *     (v_beg_snd_nxt - v_vegas.beg_snd_una) / (rtt duration)
111 	 *
112 	 * Unfortunately, v_vegas.beg_snd_una is not exactly equal to snd_una,
113 	 * because delayed ACKs can cover more than one segment, so they
114 	 * don't line up yeahly with the boundaries of RTTs.
115 	 *
116 	 * Another unfortunate fact of life is that delayed ACKs delay the
117 	 * advance of the left edge of our send window, so that the number
118 	 * of bytes we send in an RTT is often less than our cwnd will allow.
119 	 * So we keep track of our cwnd separately, in v_beg_snd_cwnd.
120 	 */
121 
122 	if (after(ack, yeah->vegas.beg_snd_nxt)) {
123 		/* We do the Vegas calculations only if we got enough RTT
124 		 * samples that we can be reasonably sure that we got
125 		 * at least one RTT sample that wasn't from a delayed ACK.
126 		 * If we only had 2 samples total,
127 		 * then that means we're getting only 1 ACK per RTT, which
128 		 * means they're almost certainly delayed ACKs.
129 		 * If  we have 3 samples, we should be OK.
130 		 */
131 
132 		if (yeah->vegas.cntRTT > 2) {
133 			u32 rtt, queue;
134 			u64 bw;
135 
136 			/* We have enough RTT samples, so, using the Vegas
137 			 * algorithm, we determine if we should increase or
138 			 * decrease cwnd, and by how much.
139 			 */
140 
141 			/* Pluck out the RTT we are using for the Vegas
142 			 * calculations. This is the min RTT seen during the
143 			 * last RTT. Taking the min filters out the effects
144 			 * of delayed ACKs, at the cost of noticing congestion
145 			 * a bit later.
146 			 */
147 			rtt = yeah->vegas.minRTT;
148 
149 			/* Compute excess number of packets above bandwidth
150 			 * Avoid doing full 64 bit divide.
151 			 */
152 			bw = tp->snd_cwnd;
153 			bw *= rtt - yeah->vegas.baseRTT;
154 			do_div(bw, rtt);
155 			queue = bw;
156 
157 			if (queue > TCP_YEAH_ALPHA ||
158 			    rtt - yeah->vegas.baseRTT > (yeah->vegas.baseRTT / TCP_YEAH_PHY)) {
159 				if (queue > TCP_YEAH_ALPHA &&
160 				    tp->snd_cwnd > yeah->reno_count) {
161 					u32 reduction = min(queue / TCP_YEAH_GAMMA ,
162 							    tp->snd_cwnd >> TCP_YEAH_EPSILON);
163 
164 					tp->snd_cwnd -= reduction;
165 
166 					tp->snd_cwnd = max(tp->snd_cwnd,
167 							   yeah->reno_count);
168 
169 					tp->snd_ssthresh = tp->snd_cwnd;
170 				}
171 
172 				if (yeah->reno_count <= 2)
173 					yeah->reno_count = max(tp->snd_cwnd>>1, 2U);
174 				else
175 					yeah->reno_count++;
176 
177 				yeah->doing_reno_now = min(yeah->doing_reno_now + 1,
178 							   0xffffffU);
179 			} else {
180 				yeah->fast_count++;
181 
182 				if (yeah->fast_count > TCP_YEAH_ZETA) {
183 					yeah->reno_count = 2;
184 					yeah->fast_count = 0;
185 				}
186 
187 				yeah->doing_reno_now = 0;
188 			}
189 
190 			yeah->lastQ = queue;
191 		}
192 
193 		/* Save the extent of the current window so we can use this
194 		 * at the end of the next RTT.
195 		 */
196 		yeah->vegas.beg_snd_una  = yeah->vegas.beg_snd_nxt;
197 		yeah->vegas.beg_snd_nxt  = tp->snd_nxt;
198 		yeah->vegas.beg_snd_cwnd = tp->snd_cwnd;
199 
200 		/* Wipe the slate clean for the next RTT. */
201 		yeah->vegas.cntRTT = 0;
202 		yeah->vegas.minRTT = 0x7fffffff;
203 	}
204 }
205 
206 static u32 tcp_yeah_ssthresh(struct sock *sk)
207 {
208 	const struct tcp_sock *tp = tcp_sk(sk);
209 	struct yeah *yeah = inet_csk_ca(sk);
210 	u32 reduction;
211 
212 	if (yeah->doing_reno_now < TCP_YEAH_RHO) {
213 		reduction = yeah->lastQ;
214 
215 		reduction = min(reduction, max(tp->snd_cwnd>>1, 2U));
216 
217 		reduction = max(reduction, tp->snd_cwnd >> TCP_YEAH_DELTA);
218 	} else
219 		reduction = max(tp->snd_cwnd>>1, 2U);
220 
221 	yeah->fast_count = 0;
222 	yeah->reno_count = max(yeah->reno_count>>1, 2U);
223 	yeah->loss_cwnd = tp->snd_cwnd;
224 
225 	return max_t(int, tp->snd_cwnd - reduction, 2);
226 }
227 
228 static u32 tcp_yeah_cwnd_undo(struct sock *sk)
229 {
230 	const struct yeah *yeah = inet_csk_ca(sk);
231 
232 	return max(tcp_sk(sk)->snd_cwnd, yeah->loss_cwnd);
233 }
234 
235 static struct tcp_congestion_ops tcp_yeah __read_mostly = {
236 	.init		= tcp_yeah_init,
237 	.ssthresh	= tcp_yeah_ssthresh,
238 	.undo_cwnd      = tcp_yeah_cwnd_undo,
239 	.cong_avoid	= tcp_yeah_cong_avoid,
240 	.set_state	= tcp_vegas_state,
241 	.cwnd_event	= tcp_vegas_cwnd_event,
242 	.get_info	= tcp_vegas_get_info,
243 	.pkts_acked	= tcp_yeah_pkts_acked,
244 
245 	.owner		= THIS_MODULE,
246 	.name		= "yeah",
247 };
248 
249 static int __init tcp_yeah_register(void)
250 {
251 	BUG_ON(sizeof(struct yeah) > ICSK_CA_PRIV_SIZE);
252 	tcp_register_congestion_control(&tcp_yeah);
253 	return 0;
254 }
255 
256 static void __exit tcp_yeah_unregister(void)
257 {
258 	tcp_unregister_congestion_control(&tcp_yeah);
259 }
260 
261 module_init(tcp_yeah_register);
262 module_exit(tcp_yeah_unregister);
263 
264 MODULE_AUTHOR("Angelo P. Castellani");
265 MODULE_LICENSE("GPL");
266 MODULE_DESCRIPTION("YeAH TCP");
267