2 * TCP Vegas congestion control
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
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
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.
34 * TCP Compound based on TCP Vegas
36 * further details can be found here:
37 * ftp://ftp.research.microsoft.com/pub/tr/TR-2005-86.pdf
40 #include <linux/config.h>
42 #include <linux/module.h>
43 #include <linux/skbuff.h>
44 #include <linux/inet_diag.h>
48 /* Default values of the Vegas variables, in fixed-point representation
49 * with V_PARAM_SHIFT bits to the right of the binary point.
51 #define V_PARAM_SHIFT 1
53 #define TCP_COMPOUND_ALPHA 3U
54 #define TCP_COMPOUND_BETA 1U
55 #define TCP_COMPOUND_GAMMA 30
56 #define TCP_COMPOUND_ZETA 1
58 /* TCP compound variables */
60 u32 beg_snd_nxt; /* right edge during last RTT */
61 u32 beg_snd_una; /* left edge during last RTT */
62 u32 beg_snd_cwnd; /* saves the size of the cwnd */
63 u8 doing_vegas_now; /* if true, do vegas for this RTT */
64 u16 cntRTT; /* # of RTTs measured within last RTT */
65 u32 minRTT; /* min of RTTs measured within last RTT (in usec) */
66 u32 baseRTT; /* the min of all Vegas RTT measurements seen (in usec) */
72 /* There are several situations when we must "re-start" Vegas:
74 * o when a connection is established
76 * o after fast recovery
77 * o when we send a packet and there is no outstanding
78 * unacknowledged data (restarting an idle connection)
80 * In these circumstances we cannot do a Vegas calculation at the
81 * end of the first RTT, because any calculation we do is using
82 * stale info -- both the saved cwnd and congestion feedback are
85 * Instead we must wait until the completion of an RTT during
86 * which we actually receive ACKs.
88 static inline void vegas_enable(struct sock *sk)
90 const struct tcp_sock *tp = tcp_sk(sk);
91 struct compound *vegas = inet_csk_ca(sk);
93 /* Begin taking Vegas samples next time we send something. */
94 vegas->doing_vegas_now = 1;
96 /* Set the beginning of the next send window. */
97 vegas->beg_snd_nxt = tp->snd_nxt;
100 vegas->minRTT = 0x7fffffff;
103 /* Stop taking Vegas samples for now. */
104 static inline void vegas_disable(struct sock *sk)
106 struct compound *vegas = inet_csk_ca(sk);
108 vegas->doing_vegas_now = 0;
111 static void tcp_compound_init(struct sock *sk)
113 struct compound *vegas = inet_csk_ca(sk);
114 const struct tcp_sock *tp = tcp_sk(sk);
116 vegas->baseRTT = 0x7fffffff;
120 vegas->cwnd = tp->snd_cwnd;
123 /* Do RTT sampling needed for Vegas.
125 * o min-filter RTT samples from within an RTT to get the current
126 * propagation delay + queuing delay (we are min-filtering to try to
127 * avoid the effects of delayed ACKs)
128 * o min-filter RTT samples from a much longer window (forever for now)
129 * to find the propagation delay (baseRTT)
131 static void tcp_compound_rtt_calc(struct sock *sk, u32 usrtt)
133 struct compound *vegas = inet_csk_ca(sk);
134 u32 vrtt = usrtt + 1; /* Never allow zero rtt or baseRTT */
136 /* Filter to find propagation delay: */
137 if (vrtt < vegas->baseRTT)
138 vegas->baseRTT = vrtt;
140 /* Find the min RTT during the last RTT to find
141 * the current prop. delay + queuing delay:
144 vegas->minRTT = min(vegas->minRTT, vrtt);
148 static void tcp_compound_state(struct sock *sk, u8 ca_state)
151 if (ca_state == TCP_CA_Open)
158 /* 64bit divisor, dividend and result. dynamic precision */
159 static inline u64 div64_64(u64 dividend, u64 divisor)
163 if (divisor > 0xffffffffULL) {
164 unsigned int shift = fls(divisor >> 32);
166 d = divisor >> shift;
170 /* avoid 64 bit division if possible */
174 dividend = (u32) dividend / d;
179 /* calculate the quartic root of "a" using Newton-Raphson */
180 static u32 qroot(u64 a)
184 /* Initial estimate is based on:
185 * qrt(x) = exp(log(x) / 4)
187 x = 1u << (fls64(a) >> 2);
190 * Iteration based on:
192 * x = ( 3 * x + a / x ) / 4
202 x = (3 * x + (u32) div64_64(a, x3)) / 4;
203 } while (abs(x1 - x) > 1);
210 * If the connection is idle and we are restarting,
211 * then we don't want to do any Vegas calculations
212 * until we get fresh RTT samples. So when we
213 * restart, we reset our Vegas state to a clean
214 * slate. After we get acks for this flight of
215 * packets, _then_ we can make Vegas calculations
218 static void tcp_compound_cwnd_event(struct sock *sk, enum tcp_ca_event event)
220 if (event == CA_EVENT_CWND_RESTART || event == CA_EVENT_TX_START)
221 tcp_compound_init(sk);
224 static void tcp_compound_cong_avoid(struct sock *sk, u32 ack,
225 u32 seq_rtt, u32 in_flight, int flag)
227 struct tcp_sock *tp = tcp_sk(sk);
228 struct compound *vegas = inet_csk_ca(sk);
231 if (vegas->cwnd + vegas->dwnd > tp->snd_cwnd) {
232 if (vegas->cwnd > tp->snd_cwnd || vegas->dwnd > tp->snd_cwnd) {
233 vegas->cwnd = tp->snd_cwnd;
236 vegas->cwnd = tp->snd_cwnd - vegas->dwnd;
240 if (!tcp_is_cwnd_limited(sk, in_flight))
243 if (vegas->cwnd <= tp->snd_ssthresh)
245 else if (tp->snd_cwnd_cnt < tp->snd_cwnd)
248 if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
250 tp->snd_cwnd_cnt = 0;
253 if (inc && tp->snd_cwnd < tp->snd_cwnd_clamp)
256 /* The key players are v_beg_snd_una and v_beg_snd_nxt.
258 * These are so named because they represent the approximate values
259 * of snd_una and snd_nxt at the beginning of the current RTT. More
260 * precisely, they represent the amount of data sent during the RTT.
261 * At the end of the RTT, when we receive an ACK for v_beg_snd_nxt,
262 * we will calculate that (v_beg_snd_nxt - v_beg_snd_una) outstanding
263 * bytes of data have been ACKed during the course of the RTT, giving
264 * an "actual" rate of:
266 * (v_beg_snd_nxt - v_beg_snd_una) / (rtt duration)
268 * Unfortunately, v_beg_snd_una is not exactly equal to snd_una,
269 * because delayed ACKs can cover more than one segment, so they
270 * don't line up nicely with the boundaries of RTTs.
272 * Another unfortunate fact of life is that delayed ACKs delay the
273 * advance of the left edge of our send window, so that the number
274 * of bytes we send in an RTT is often less than our cwnd will allow.
275 * So we keep track of our cwnd separately, in v_beg_snd_cwnd.
278 if (after(ack, vegas->beg_snd_nxt)) {
279 /* Do the Vegas once-per-RTT cwnd adjustment. */
280 u32 old_wnd, old_snd_cwnd;
282 /* Here old_wnd is essentially the window of data that was
283 * sent during the previous RTT, and has all
284 * been acknowledged in the course of the RTT that ended
285 * with the ACK we just received. Likewise, old_snd_cwnd
286 * is the cwnd during the previous RTT.
291 old_wnd = (vegas->beg_snd_nxt - vegas->beg_snd_una) /
293 old_snd_cwnd = vegas->beg_snd_cwnd;
295 /* Save the extent of the current window so we can use this
296 * at the end of the next RTT.
298 vegas->beg_snd_una = vegas->beg_snd_nxt;
299 vegas->beg_snd_nxt = tp->snd_nxt;
300 vegas->beg_snd_cwnd = tp->snd_cwnd;
302 /* We do the Vegas calculations only if we got enough RTT
303 * samples that we can be reasonably sure that we got
304 * at least one RTT sample that wasn't from a delayed ACK.
305 * If we only had 2 samples total,
306 * then that means we're getting only 1 ACK per RTT, which
307 * means they're almost certainly delayed ACKs.
308 * If we have 3 samples, we should be OK.
311 if (vegas->cntRTT > 2) {
312 u32 rtt, target_cwnd, diff;
315 /* We have enough RTT samples, so, using the Vegas
316 * algorithm, we determine if we should increase or
317 * decrease cwnd, and by how much.
320 /* Pluck out the RTT we are using for the Vegas
321 * calculations. This is the min RTT seen during the
322 * last RTT. Taking the min filters out the effects
323 * of delayed ACKs, at the cost of noticing congestion
328 /* Calculate the cwnd we should have, if we weren't
332 * (actual rate in segments) * baseRTT
333 * We keep it as a fixed point number with
334 * V_PARAM_SHIFT bits to the right of the binary point.
339 brtt = vegas->baseRTT;
340 target_cwnd = ((old_wnd * brtt)
341 << V_PARAM_SHIFT) / rtt;
343 /* Calculate the difference between the window we had,
344 * and the window we would like to have. This quantity
345 * is the "Diff" from the Arizona Vegas papers.
347 * Again, this is a fixed point number with
348 * V_PARAM_SHIFT bits to the right of the binary
352 diff = (old_wnd << V_PARAM_SHIFT) - target_cwnd;
356 if (diff < (TCP_COMPOUND_GAMMA << V_PARAM_SHIFT)) {
361 * The TCP Compound paper describes the choice
362 * of "k" determines the agressiveness,
363 * ie. slope of the response function.
365 * For same value as HSTCP would be 0.8
366 * but for computaional reasons, both the
367 * original authors and this implementation
371 x = qroot(v * v * v) >> TCP_COMPOUND_ALPHA;
379 } else if ((dwnd << V_PARAM_SHIFT) <
380 (diff * TCP_COMPOUND_BETA))
384 ((dwnd << V_PARAM_SHIFT) -
386 TCP_COMPOUND_BETA)) >> V_PARAM_SHIFT;
392 /* Wipe the slate clean for the next RTT. */
394 vegas->minRTT = 0x7fffffff;
397 tp->snd_cwnd = vegas->cwnd + vegas->dwnd;
400 /* Extract info for Tcp socket info provided via netlink. */
401 static void tcp_compound_get_info(struct sock *sk, u32 ext, struct sk_buff *skb)
403 const struct compound *ca = inet_csk_ca(sk);
404 if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
405 struct tcpvegas_info *info;
407 info = RTA_DATA(__RTA_PUT(skb, INET_DIAG_VEGASINFO,
410 info->tcpv_enabled = ca->doing_vegas_now;
411 info->tcpv_rttcnt = ca->cntRTT;
412 info->tcpv_rtt = ca->baseRTT;
413 info->tcpv_minrtt = ca->minRTT;
418 static struct tcp_congestion_ops tcp_compound = {
419 .init = tcp_compound_init,
420 .ssthresh = tcp_reno_ssthresh,
421 .cong_avoid = tcp_compound_cong_avoid,
422 .rtt_sample = tcp_compound_rtt_calc,
423 .set_state = tcp_compound_state,
424 .cwnd_event = tcp_compound_cwnd_event,
425 .get_info = tcp_compound_get_info,
427 .owner = THIS_MODULE,
431 static int __init tcp_compound_register(void)
433 BUG_ON(sizeof(struct compound) > ICSK_CA_PRIV_SIZE);
434 tcp_register_congestion_control(&tcp_compound);
438 static void __exit tcp_compound_unregister(void)
440 tcp_unregister_congestion_control(&tcp_compound);
443 module_init(tcp_compound_register);
444 module_exit(tcp_compound_unregister);
446 MODULE_AUTHOR("Angelo P. Castellani, Stephen Hemminger");
447 MODULE_LICENSE("GPL");
448 MODULE_DESCRIPTION("TCP Compound");