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