Commit | Line | Data |
---|---|---|
b87d8561 SH |
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 | ||
b87d8561 SH |
34 | #include <linux/mm.h> |
35 | #include <linux/module.h> | |
36 | #include <linux/skbuff.h> | |
a8c2190e | 37 | #include <linux/inet_diag.h> |
b87d8561 SH |
38 | |
39 | #include <net/tcp.h> | |
40 | ||
7752237e SH |
41 | #include "tcp_vegas.h" |
42 | ||
8d3a564d DL |
43 | static int alpha = 2; |
44 | static int beta = 4; | |
45 | static int gamma = 1; | |
b87d8561 SH |
46 | |
47 | module_param(alpha, int, 0644); | |
8d3a564d | 48 | MODULE_PARM_DESC(alpha, "lower bound of packets in network"); |
b87d8561 | 49 | module_param(beta, int, 0644); |
8d3a564d | 50 | MODULE_PARM_DESC(beta, "upper bound of packets in network"); |
b87d8561 SH |
51 | module_param(gamma, int, 0644); |
52 | MODULE_PARM_DESC(gamma, "limit on increase (scale by 2)"); | |
53 | ||
b87d8561 SH |
54 | /* There are several situations when we must "re-start" Vegas: |
55 | * | |
56 | * o when a connection is established | |
57 | * o after an RTO | |
58 | * o after fast recovery | |
59 | * o when we send a packet and there is no outstanding | |
60 | * unacknowledged data (restarting an idle connection) | |
61 | * | |
62 | * In these circumstances we cannot do a Vegas calculation at the | |
63 | * end of the first RTT, because any calculation we do is using | |
64 | * stale info -- both the saved cwnd and congestion feedback are | |
65 | * stale. | |
66 | * | |
67 | * Instead we must wait until the completion of an RTT during | |
68 | * which we actually receive ACKs. | |
69 | */ | |
7752237e | 70 | static void vegas_enable(struct sock *sk) |
b87d8561 | 71 | { |
6687e988 ACM |
72 | const struct tcp_sock *tp = tcp_sk(sk); |
73 | struct vegas *vegas = inet_csk_ca(sk); | |
b87d8561 SH |
74 | |
75 | /* Begin taking Vegas samples next time we send something. */ | |
76 | vegas->doing_vegas_now = 1; | |
77 | ||
78 | /* Set the beginning of the next send window. */ | |
79 | vegas->beg_snd_nxt = tp->snd_nxt; | |
80 | ||
81 | vegas->cntRTT = 0; | |
82 | vegas->minRTT = 0x7fffffff; | |
83 | } | |
84 | ||
85 | /* Stop taking Vegas samples for now. */ | |
6687e988 | 86 | static inline void vegas_disable(struct sock *sk) |
b87d8561 | 87 | { |
6687e988 | 88 | struct vegas *vegas = inet_csk_ca(sk); |
b87d8561 SH |
89 | |
90 | vegas->doing_vegas_now = 0; | |
91 | } | |
92 | ||
7752237e | 93 | void tcp_vegas_init(struct sock *sk) |
b87d8561 | 94 | { |
6687e988 | 95 | struct vegas *vegas = inet_csk_ca(sk); |
b87d8561 SH |
96 | |
97 | vegas->baseRTT = 0x7fffffff; | |
6687e988 | 98 | vegas_enable(sk); |
b87d8561 | 99 | } |
7752237e | 100 | EXPORT_SYMBOL_GPL(tcp_vegas_init); |
b87d8561 SH |
101 | |
102 | /* Do RTT sampling needed for Vegas. | |
103 | * Basically we: | |
104 | * o min-filter RTT samples from within an RTT to get the current | |
105 | * propagation delay + queuing delay (we are min-filtering to try to | |
106 | * avoid the effects of delayed ACKs) | |
107 | * o min-filter RTT samples from a much longer window (forever for now) | |
108 | * to find the propagation delay (baseRTT) | |
109 | */ | |
30cfd0ba | 110 | void tcp_vegas_pkts_acked(struct sock *sk, u32 cnt, s32 rtt_us) |
b87d8561 | 111 | { |
6687e988 | 112 | struct vegas *vegas = inet_csk_ca(sk); |
164891aa SH |
113 | u32 vrtt; |
114 | ||
30cfd0ba | 115 | if (rtt_us < 0) |
b9ce204f IJ |
116 | return; |
117 | ||
164891aa | 118 | /* Never allow zero rtt or baseRTT */ |
30cfd0ba | 119 | vrtt = rtt_us + 1; |
b87d8561 SH |
120 | |
121 | /* Filter to find propagation delay: */ | |
122 | if (vrtt < vegas->baseRTT) | |
123 | vegas->baseRTT = vrtt; | |
124 | ||
125 | /* Find the min RTT during the last RTT to find | |
126 | * the current prop. delay + queuing delay: | |
127 | */ | |
128 | vegas->minRTT = min(vegas->minRTT, vrtt); | |
129 | vegas->cntRTT++; | |
130 | } | |
7752237e | 131 | EXPORT_SYMBOL_GPL(tcp_vegas_pkts_acked); |
b87d8561 | 132 | |
7752237e | 133 | void tcp_vegas_state(struct sock *sk, u8 ca_state) |
b87d8561 | 134 | { |
b87d8561 | 135 | if (ca_state == TCP_CA_Open) |
6687e988 | 136 | vegas_enable(sk); |
b87d8561 | 137 | else |
6687e988 | 138 | vegas_disable(sk); |
b87d8561 | 139 | } |
7752237e | 140 | EXPORT_SYMBOL_GPL(tcp_vegas_state); |
b87d8561 SH |
141 | |
142 | /* | |
143 | * If the connection is idle and we are restarting, | |
144 | * then we don't want to do any Vegas calculations | |
145 | * until we get fresh RTT samples. So when we | |
146 | * restart, we reset our Vegas state to a clean | |
147 | * slate. After we get acks for this flight of | |
148 | * packets, _then_ we can make Vegas calculations | |
149 | * again. | |
150 | */ | |
7752237e | 151 | void tcp_vegas_cwnd_event(struct sock *sk, enum tcp_ca_event event) |
b87d8561 SH |
152 | { |
153 | if (event == CA_EVENT_CWND_RESTART || | |
154 | event == CA_EVENT_TX_START) | |
6687e988 | 155 | tcp_vegas_init(sk); |
b87d8561 | 156 | } |
7752237e | 157 | EXPORT_SYMBOL_GPL(tcp_vegas_cwnd_event); |
b87d8561 | 158 | |
c80a5cdf DL |
159 | static inline u32 tcp_vegas_ssthresh(struct tcp_sock *tp) |
160 | { | |
161 | return min(tp->snd_ssthresh, tp->snd_cwnd-1); | |
162 | } | |
163 | ||
24901551 | 164 | static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack, u32 acked) |
b87d8561 | 165 | { |
6687e988 ACM |
166 | struct tcp_sock *tp = tcp_sk(sk); |
167 | struct vegas *vegas = inet_csk_ca(sk); | |
b87d8561 | 168 | |
ab59859d | 169 | if (!vegas->doing_vegas_now) { |
24901551 | 170 | tcp_reno_cong_avoid(sk, ack, acked); |
ab59859d HH |
171 | return; |
172 | } | |
b87d8561 | 173 | |
b87d8561 SH |
174 | if (after(ack, vegas->beg_snd_nxt)) { |
175 | /* Do the Vegas once-per-RTT cwnd adjustment. */ | |
b87d8561 SH |
176 | |
177 | /* Save the extent of the current window so we can use this | |
178 | * at the end of the next RTT. | |
179 | */ | |
b87d8561 | 180 | vegas->beg_snd_nxt = tp->snd_nxt; |
b87d8561 | 181 | |
b87d8561 SH |
182 | /* We do the Vegas calculations only if we got enough RTT |
183 | * samples that we can be reasonably sure that we got | |
184 | * at least one RTT sample that wasn't from a delayed ACK. | |
185 | * If we only had 2 samples total, | |
186 | * then that means we're getting only 1 ACK per RTT, which | |
187 | * means they're almost certainly delayed ACKs. | |
188 | * If we have 3 samples, we should be OK. | |
189 | */ | |
190 | ||
191 | if (vegas->cntRTT <= 2) { | |
192 | /* We don't have enough RTT samples to do the Vegas | |
193 | * calculation, so we'll behave like Reno. | |
194 | */ | |
24901551 | 195 | tcp_reno_cong_avoid(sk, ack, acked); |
b87d8561 | 196 | } else { |
15913114 LA |
197 | u32 rtt, diff; |
198 | u64 target_cwnd; | |
b87d8561 SH |
199 | |
200 | /* We have enough RTT samples, so, using the Vegas | |
201 | * algorithm, we determine if we should increase or | |
202 | * decrease cwnd, and by how much. | |
203 | */ | |
204 | ||
205 | /* Pluck out the RTT we are using for the Vegas | |
206 | * calculations. This is the min RTT seen during the | |
207 | * last RTT. Taking the min filters out the effects | |
208 | * of delayed ACKs, at the cost of noticing congestion | |
209 | * a bit later. | |
210 | */ | |
211 | rtt = vegas->minRTT; | |
212 | ||
213 | /* Calculate the cwnd we should have, if we weren't | |
214 | * going too fast. | |
215 | * | |
216 | * This is: | |
217 | * (actual rate in segments) * baseRTT | |
b87d8561 | 218 | */ |
1f74e613 CP |
219 | target_cwnd = (u64)tp->snd_cwnd * vegas->baseRTT; |
220 | do_div(target_cwnd, rtt); | |
b87d8561 SH |
221 | |
222 | /* Calculate the difference between the window we had, | |
223 | * and the window we would like to have. This quantity | |
224 | * is the "Diff" from the Arizona Vegas papers. | |
b87d8561 | 225 | */ |
8d3a564d | 226 | diff = tp->snd_cwnd * (rtt-vegas->baseRTT) / vegas->baseRTT; |
b87d8561 | 227 | |
071d5080 | 228 | if (diff > gamma && tcp_in_slow_start(tp)) { |
c940587b XDW |
229 | /* Going too fast. Time to slow down |
230 | * and switch to congestion avoidance. | |
231 | */ | |
c940587b XDW |
232 | |
233 | /* Set cwnd to match the actual rate | |
234 | * exactly: | |
235 | * cwnd = (actual rate) * baseRTT | |
236 | * Then we add 1 because the integer | |
237 | * truncation robs us of full link | |
238 | * utilization. | |
239 | */ | |
8d3a564d | 240 | tp->snd_cwnd = min(tp->snd_cwnd, (u32)target_cwnd+1); |
c80a5cdf | 241 | tp->snd_ssthresh = tcp_vegas_ssthresh(tp); |
b87d8561 | 242 | |
071d5080 | 243 | } else if (tcp_in_slow_start(tp)) { |
c940587b | 244 | /* Slow start. */ |
9f9843a7 | 245 | tcp_slow_start(tp, acked); |
b87d8561 SH |
246 | } else { |
247 | /* Congestion avoidance. */ | |
b87d8561 SH |
248 | |
249 | /* Figure out where we would like cwnd | |
250 | * to be. | |
251 | */ | |
252 | if (diff > beta) { | |
253 | /* The old window was too fast, so | |
254 | * we slow down. | |
255 | */ | |
8d3a564d | 256 | tp->snd_cwnd--; |
c80a5cdf DL |
257 | tp->snd_ssthresh |
258 | = tcp_vegas_ssthresh(tp); | |
b87d8561 SH |
259 | } else if (diff < alpha) { |
260 | /* We don't have enough extra packets | |
261 | * in the network, so speed up. | |
262 | */ | |
8d3a564d | 263 | tp->snd_cwnd++; |
b87d8561 SH |
264 | } else { |
265 | /* Sending just as fast as we | |
266 | * should be. | |
267 | */ | |
b87d8561 | 268 | } |
b87d8561 | 269 | } |
b87d8561 | 270 | |
7faffa1c SH |
271 | if (tp->snd_cwnd < 2) |
272 | tp->snd_cwnd = 2; | |
273 | else if (tp->snd_cwnd > tp->snd_cwnd_clamp) | |
274 | tp->snd_cwnd = tp->snd_cwnd_clamp; | |
a6af2d6b DL |
275 | |
276 | tp->snd_ssthresh = tcp_current_ssthresh(sk); | |
7faffa1c | 277 | } |
b87d8561 | 278 | |
5b495613 TY |
279 | /* Wipe the slate clean for the next RTT. */ |
280 | vegas->cntRTT = 0; | |
281 | vegas->minRTT = 0x7fffffff; | |
282 | } | |
74cb8798 | 283 | /* Use normal slow start */ |
071d5080 | 284 | else if (tcp_in_slow_start(tp)) |
9f9843a7 | 285 | tcp_slow_start(tp, acked); |
b87d8561 SH |
286 | } |
287 | ||
288 | /* Extract info for Tcp socket info provided via netlink. */ | |
64f40ff5 ED |
289 | size_t tcp_vegas_get_info(struct sock *sk, u32 ext, int *attr, |
290 | union tcp_cc_info *info) | |
b87d8561 | 291 | { |
6687e988 | 292 | const struct vegas *ca = inet_csk_ca(sk); |
64f40ff5 | 293 | |
73c1f4a0 | 294 | if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) { |
64f40ff5 ED |
295 | info->vegas.tcpv_enabled = ca->doing_vegas_now, |
296 | info->vegas.tcpv_rttcnt = ca->cntRTT, | |
297 | info->vegas.tcpv_rtt = ca->baseRTT, | |
298 | info->vegas.tcpv_minrtt = ca->minRTT, | |
299 | ||
300 | *attr = INET_DIAG_VEGASINFO; | |
301 | return sizeof(struct tcpvegas_info); | |
b87d8561 | 302 | } |
521f1cf1 | 303 | return 0; |
b87d8561 | 304 | } |
7752237e | 305 | EXPORT_SYMBOL_GPL(tcp_vegas_get_info); |
b87d8561 | 306 | |
a252bebe | 307 | static struct tcp_congestion_ops tcp_vegas __read_mostly = { |
b87d8561 SH |
308 | .init = tcp_vegas_init, |
309 | .ssthresh = tcp_reno_ssthresh, | |
310 | .cong_avoid = tcp_vegas_cong_avoid, | |
164891aa | 311 | .pkts_acked = tcp_vegas_pkts_acked, |
b87d8561 SH |
312 | .set_state = tcp_vegas_state, |
313 | .cwnd_event = tcp_vegas_cwnd_event, | |
314 | .get_info = tcp_vegas_get_info, | |
315 | ||
316 | .owner = THIS_MODULE, | |
317 | .name = "vegas", | |
318 | }; | |
319 | ||
320 | static int __init tcp_vegas_register(void) | |
321 | { | |
74975d40 | 322 | BUILD_BUG_ON(sizeof(struct vegas) > ICSK_CA_PRIV_SIZE); |
b87d8561 SH |
323 | tcp_register_congestion_control(&tcp_vegas); |
324 | return 0; | |
325 | } | |
326 | ||
327 | static void __exit tcp_vegas_unregister(void) | |
328 | { | |
329 | tcp_unregister_congestion_control(&tcp_vegas); | |
330 | } | |
331 | ||
332 | module_init(tcp_vegas_register); | |
333 | module_exit(tcp_vegas_unregister); | |
334 | ||
335 | MODULE_AUTHOR("Stephen Hemminger"); | |
336 | MODULE_LICENSE("GPL"); | |
337 | MODULE_DESCRIPTION("TCP Vegas"); |