2 * TCP Illinois congestion control.
4 * http://www.ews.uiuc.edu/~shaoliu/tcpillinois/index.html
6 * The algorithm is described in:
7 * "TCP-Illinois: A Loss and Delay-Based Congestion Control Algorithm
8 * for High-Speed Networks"
9 * http://www.ews.uiuc.edu/~shaoliu/papersandslides/liubassri06perf.pdf
11 * Implemented from description in paper and ns-2 simulation.
12 * Copyright (C) 2007 Stephen Hemminger <shemminger@linux-foundation.org>
15 #include <linux/module.h>
16 #include <linux/skbuff.h>
17 #include <linux/inet_diag.h>
18 #include <asm/div64.h>
22 #define ALPHA_SCALE (1u<<ALPHA_SHIFT)
23 #define ALPHA_MIN ((3*ALPHA_SCALE)/10) /* ~0.3 */
24 #define ALPHA_MAX (10*ALPHA_SCALE) /* 10.0 */
25 #define ALPHA_BASE ALPHA_SCALE /* 1.0 */
26 #define U32_MAX ((u32)~0U)
27 #define RTT_MAX (U32_MAX / ALPHA_MAX) /* 3.3 secs */
30 #define BETA_SCALE (1u<<BETA_SHIFT)
31 #define BETA_MIN (BETA_SCALE/8) /* 0.125 */
32 #define BETA_MAX (BETA_SCALE/2) /* 0.5 */
33 #define BETA_BASE BETA_MAX
35 static int win_thresh __read_mostly = 15;
36 module_param(win_thresh, int, 0);
37 MODULE_PARM_DESC(win_thresh, "Window threshold for starting adaptive sizing");
39 static int theta __read_mostly = 5;
40 module_param(theta, int, 0);
41 MODULE_PARM_DESC(theta, "# of fast RTT's before full growth");
43 /* TCP Illinois Parameters */
45 u64 sum_rtt; /* sum of rtt's measured within last rtt */
46 u16 cnt_rtt; /* # of rtts measured within last rtt */
47 u32 base_rtt; /* min of all rtt in usec */
48 u32 max_rtt; /* max of all rtt in usec */
49 u32 end_seq; /* right edge of current RTT */
50 u32 alpha; /* Additive increase */
51 u32 beta; /* Muliplicative decrease */
52 u16 acked; /* # packets acked by current ACK */
53 u8 rtt_above; /* average rtt has gone above threshold */
54 u8 rtt_low; /* # of rtts measurements below threshold */
57 static void rtt_reset(struct sock *sk)
59 struct tcp_sock *tp = tcp_sk(sk);
60 struct illinois *ca = inet_csk_ca(sk);
62 ca->end_seq = tp->snd_nxt;
66 /* TODO: age max_rtt? */
69 static void tcp_illinois_init(struct sock *sk)
71 struct illinois *ca = inet_csk_ca(sk);
73 ca->alpha = ALPHA_MAX;
75 ca->base_rtt = 0x7fffffff;
85 /* Measure RTT for each ack. */
86 static void tcp_illinois_acked(struct sock *sk, u32 pkts_acked, s32 rtt)
88 struct illinois *ca = inet_csk_ca(sk);
90 ca->acked = pkts_acked;
92 /* dup ack, no rtt sample */
96 /* ignore bogus values, this prevents wraparound in alpha math */
100 /* keep track of minimum RTT seen so far */
101 if (ca->base_rtt > rtt)
105 if (ca->max_rtt < rtt)
112 /* Maximum queuing delay */
113 static inline u32 max_delay(const struct illinois *ca)
115 return ca->max_rtt - ca->base_rtt;
118 /* Average queuing delay */
119 static inline u32 avg_delay(const struct illinois *ca)
123 do_div(t, ca->cnt_rtt);
124 return t - ca->base_rtt;
128 * Compute value of alpha used for additive increase.
129 * If small window then use 1.0, equivalent to Reno.
131 * For larger windows, adjust based on average delay.
132 * A. If average delay is at minimum (we are uncongested),
133 * then use large alpha (10.0) to increase faster.
134 * B. If average delay is at maximum (getting congested)
135 * then use small alpha (0.3)
137 * The result is a convex window growth curve.
139 static u32 alpha(struct illinois *ca, u32 da, u32 dm)
141 u32 d1 = dm / 100; /* Low threshold */
144 /* If never got out of low delay zone, then use max */
148 /* Wait for 5 good RTT's before allowing alpha to go alpha max.
149 * This prevents one good RTT from causing sudden window increase.
151 if (++ca->rtt_low < theta)
164 * (dm - d1) amin amax
165 * k1 = -------------------
169 * k2 = ---------------- - d1
179 return (dm * ALPHA_MAX) /
180 (dm + (da * (ALPHA_MAX - ALPHA_MIN)) / ALPHA_MIN);
184 * Beta used for multiplicative decrease.
185 * For small window sizes returns same value as Reno (0.5)
187 * If delay is small (10% of max) then beta = 1/8
188 * If delay is up to 80% of max then beta = 1/2
189 * In between is a linear function
191 static u32 beta(u32 da, u32 dm)
200 if (da >= d3 || d3 <= d2)
207 * k3 = -------------------
216 return (BETA_MIN * d3 - BETA_MAX * d2 + (BETA_MAX - BETA_MIN) * da)
220 /* Update alpha and beta values once per RTT */
221 static void update_params(struct sock *sk)
223 struct tcp_sock *tp = tcp_sk(sk);
224 struct illinois *ca = inet_csk_ca(sk);
226 if (tp->snd_cwnd < win_thresh) {
227 ca->alpha = ALPHA_BASE;
228 ca->beta = BETA_BASE;
229 } else if (ca->cnt_rtt > 0) {
230 u32 dm = max_delay(ca);
231 u32 da = avg_delay(ca);
233 ca->alpha = alpha(ca, da, dm);
234 ca->beta = beta(da, dm);
241 * In case of loss, reset to default values
243 static void tcp_illinois_state(struct sock *sk, u8 new_state)
245 struct illinois *ca = inet_csk_ca(sk);
247 if (new_state == TCP_CA_Loss) {
248 ca->alpha = ALPHA_BASE;
249 ca->beta = BETA_BASE;
257 * Increase window in response to successful acknowledgment.
259 static void tcp_illinois_cong_avoid(struct sock *sk, u32 ack,
260 u32 in_flight, int flag)
262 struct tcp_sock *tp = tcp_sk(sk);
263 struct illinois *ca = inet_csk_ca(sk);
265 if (after(ack, ca->end_seq))
268 /* RFC2861 only increase cwnd if fully utilized */
269 if (!tcp_is_cwnd_limited(sk, in_flight))
273 if (tp->snd_cwnd <= tp->snd_ssthresh)
279 /* snd_cwnd_cnt is # of packets since last cwnd increment */
280 tp->snd_cwnd_cnt += ca->acked;
283 /* This is close approximation of:
284 * tp->snd_cwnd += alpha/tp->snd_cwnd
286 delta = (tp->snd_cwnd_cnt * ca->alpha) >> ALPHA_SHIFT;
287 if (delta >= tp->snd_cwnd) {
288 tp->snd_cwnd = min(tp->snd_cwnd + delta / tp->snd_cwnd,
289 (u32) tp->snd_cwnd_clamp);
290 tp->snd_cwnd_cnt = 0;
295 static u32 tcp_illinois_ssthresh(struct sock *sk)
297 struct tcp_sock *tp = tcp_sk(sk);
298 struct illinois *ca = inet_csk_ca(sk);
300 /* Multiplicative decrease */
301 return max((tp->snd_cwnd * ca->beta) >> BETA_SHIFT, 2U);
305 /* Extract info for Tcp socket info provided via netlink. */
306 static void tcp_illinois_info(struct sock *sk, u32 ext,
309 const struct illinois *ca = inet_csk_ca(sk);
311 if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
312 struct tcpvegas_info info = {
314 .tcpv_rttcnt = ca->cnt_rtt,
315 .tcpv_minrtt = ca->base_rtt,
319 do_div(t, ca->cnt_rtt);
322 nla_put(skb, INET_DIAG_VEGASINFO, sizeof(info), &info);
326 static struct tcp_congestion_ops tcp_illinois = {
327 .flags = TCP_CONG_RTT_STAMP,
328 .init = tcp_illinois_init,
329 .ssthresh = tcp_illinois_ssthresh,
330 .min_cwnd = tcp_reno_min_cwnd,
331 .cong_avoid = tcp_illinois_cong_avoid,
332 .set_state = tcp_illinois_state,
333 .get_info = tcp_illinois_info,
334 .pkts_acked = tcp_illinois_acked,
336 .owner = THIS_MODULE,
340 static int __init tcp_illinois_register(void)
342 BUILD_BUG_ON(sizeof(struct illinois) > ICSK_CA_PRIV_SIZE);
343 return tcp_register_congestion_control(&tcp_illinois);
346 static void __exit tcp_illinois_unregister(void)
348 tcp_unregister_congestion_control(&tcp_illinois);
351 module_init(tcp_illinois_register);
352 module_exit(tcp_illinois_unregister);
354 MODULE_AUTHOR("Stephen Hemminger, Shao Liu");
355 MODULE_LICENSE("GPL");
356 MODULE_DESCRIPTION("TCP Illinois");
357 MODULE_VERSION("1.0");