1 // SPDX-License-Identifier: GPL-2.0-only
3 * TCP Illinois congestion control.
5 * http://www.ews.uiuc.edu/~shaoliu/tcpillinois/index.html
7 * The algorithm is described in:
8 * "TCP-Illinois: A Loss and Delay-Based Congestion Control Algorithm
9 * for High-Speed Networks"
10 * http://tamerbasar.csl.illinois.edu/LiuBasarSrikantPerfEvalArtJun2008.pdf
12 * Implemented from description in paper and ns-2 simulation.
13 * Copyright (C) 2007 Stephen Hemminger <shemminger@linux-foundation.org>
16 #include <linux/module.h>
17 #include <linux/skbuff.h>
18 #include <linux/inet_diag.h>
19 #include <asm/div64.h>
23 #define ALPHA_SCALE (1u<<ALPHA_SHIFT)
24 #define ALPHA_MIN ((3*ALPHA_SCALE)/10) /* ~0.3 */
25 #define ALPHA_MAX (10*ALPHA_SCALE) /* 10.0 */
26 #define ALPHA_BASE ALPHA_SCALE /* 1.0 */
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, const struct ack_sample *sample)
88 struct illinois *ca = inet_csk_ca(sk);
89 s32 rtt_us = sample->rtt_us;
91 ca->acked = sample->pkts_acked;
93 /* dup ack, no rtt sample */
97 /* ignore bogus values, this prevents wraparound in alpha math */
101 /* keep track of minimum RTT seen so far */
102 if (ca->base_rtt > rtt_us)
103 ca->base_rtt = rtt_us;
106 if (ca->max_rtt < rtt_us)
107 ca->max_rtt = rtt_us;
110 ca->sum_rtt += rtt_us;
113 /* Maximum queuing delay */
114 static inline u32 max_delay(const struct illinois *ca)
116 return ca->max_rtt - ca->base_rtt;
119 /* Average queuing delay */
120 static inline u32 avg_delay(const struct illinois *ca)
124 do_div(t, ca->cnt_rtt);
125 return t - ca->base_rtt;
129 * Compute value of alpha used for additive increase.
130 * If small window then use 1.0, equivalent to Reno.
132 * For larger windows, adjust based on average delay.
133 * A. If average delay is at minimum (we are uncongested),
134 * then use large alpha (10.0) to increase faster.
135 * B. If average delay is at maximum (getting congested)
136 * then use small alpha (0.3)
138 * The result is a convex window growth curve.
140 static u32 alpha(struct illinois *ca, u32 da, u32 dm)
142 u32 d1 = dm / 100; /* Low threshold */
145 /* If never got out of low delay zone, then use max */
149 /* Wait for 5 good RTT's before allowing alpha to go alpha max.
150 * This prevents one good RTT from causing sudden window increase.
152 if (++ca->rtt_low < theta)
165 * (dm - d1) amin amax
166 * k1 = -------------------
170 * k2 = ---------------- - d1
180 return (dm * ALPHA_MAX) /
181 (dm + (da * (ALPHA_MAX - ALPHA_MIN)) / ALPHA_MIN);
185 * Beta used for multiplicative decrease.
186 * For small window sizes returns same value as Reno (0.5)
188 * If delay is small (10% of max) then beta = 1/8
189 * If delay is up to 80% of max then beta = 1/2
190 * In between is a linear function
192 static u32 beta(u32 da, u32 dm)
201 if (da >= d3 || d3 <= d2)
208 * k3 = -------------------
217 return (BETA_MIN * d3 - BETA_MAX * d2 + (BETA_MAX - BETA_MIN) * da)
221 /* Update alpha and beta values once per RTT */
222 static void update_params(struct sock *sk)
224 struct tcp_sock *tp = tcp_sk(sk);
225 struct illinois *ca = inet_csk_ca(sk);
227 if (tp->snd_cwnd < win_thresh) {
228 ca->alpha = ALPHA_BASE;
229 ca->beta = BETA_BASE;
230 } else if (ca->cnt_rtt > 0) {
231 u32 dm = max_delay(ca);
232 u32 da = avg_delay(ca);
234 ca->alpha = alpha(ca, da, dm);
235 ca->beta = beta(da, dm);
242 * In case of loss, reset to default values
244 static void tcp_illinois_state(struct sock *sk, u8 new_state)
246 struct illinois *ca = inet_csk_ca(sk);
248 if (new_state == TCP_CA_Loss) {
249 ca->alpha = ALPHA_BASE;
250 ca->beta = BETA_BASE;
258 * Increase window in response to successful acknowledgment.
260 static void tcp_illinois_cong_avoid(struct sock *sk, u32 ack, u32 acked)
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))
273 if (tcp_in_slow_start(tp))
274 tcp_slow_start(tp, acked);
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 - ((tp->snd_cwnd * ca->beta) >> BETA_SHIFT), 2U);
304 /* Extract info for Tcp socket info provided via netlink. */
305 static size_t tcp_illinois_info(struct sock *sk, u32 ext, int *attr,
306 union tcp_cc_info *info)
308 const struct illinois *ca = inet_csk_ca(sk);
310 if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
311 info->vegas.tcpv_enabled = 1;
312 info->vegas.tcpv_rttcnt = ca->cnt_rtt;
313 info->vegas.tcpv_minrtt = ca->base_rtt;
314 info->vegas.tcpv_rtt = 0;
316 if (info->vegas.tcpv_rttcnt > 0) {
319 do_div(t, info->vegas.tcpv_rttcnt);
320 info->vegas.tcpv_rtt = t;
322 *attr = INET_DIAG_VEGASINFO;
323 return sizeof(struct tcpvegas_info);
328 static struct tcp_congestion_ops tcp_illinois __read_mostly = {
329 .init = tcp_illinois_init,
330 .ssthresh = tcp_illinois_ssthresh,
331 .undo_cwnd = tcp_reno_undo_cwnd,
332 .cong_avoid = tcp_illinois_cong_avoid,
333 .set_state = tcp_illinois_state,
334 .get_info = tcp_illinois_info,
335 .pkts_acked = tcp_illinois_acked,
337 .owner = THIS_MODULE,
341 static int __init tcp_illinois_register(void)
343 BUILD_BUG_ON(sizeof(struct illinois) > ICSK_CA_PRIV_SIZE);
344 return tcp_register_congestion_control(&tcp_illinois);
347 static void __exit tcp_illinois_unregister(void)
349 tcp_unregister_congestion_control(&tcp_illinois);
352 module_init(tcp_illinois_register);
353 module_exit(tcp_illinois_unregister);
355 MODULE_AUTHOR("Stephen Hemminger, Shao Liu");
356 MODULE_LICENSE("GPL");
357 MODULE_DESCRIPTION("TCP Illinois");
358 MODULE_VERSION("1.0");