1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/module.h>
16#include <linux/skbuff.h>
17#include <linux/inet_diag.h>
18#include <asm/div64.h>
19#include <net/tcp.h>
20
21#define ALPHA_SHIFT 7
22#define ALPHA_SCALE (1u<<ALPHA_SHIFT)
23#define ALPHA_MIN ((3*ALPHA_SCALE)/10)
24#define ALPHA_MAX (10*ALPHA_SCALE)
25#define ALPHA_BASE ALPHA_SCALE
26#define U32_MAX ((u32)~0U)
27#define RTT_MAX (U32_MAX / ALPHA_MAX)
28
29#define BETA_SHIFT 6
30#define BETA_SCALE (1u<<BETA_SHIFT)
31#define BETA_MIN (BETA_SCALE/8)
32#define BETA_MAX (BETA_SCALE/2)
33#define BETA_BASE BETA_MAX
34
35static int win_thresh __read_mostly = 15;
36module_param(win_thresh, int, 0);
37MODULE_PARM_DESC(win_thresh, "Window threshold for starting adaptive sizing");
38
39static int theta __read_mostly = 5;
40module_param(theta, int, 0);
41MODULE_PARM_DESC(theta, "# of fast RTT's before full growth");
42
43
44struct illinois {
45 u64 sum_rtt;
46 u16 cnt_rtt;
47 u32 base_rtt;
48 u32 max_rtt;
49 u32 end_seq;
50 u32 alpha;
51 u32 beta;
52 u16 acked;
53 u8 rtt_above;
54 u8 rtt_low;
55};
56
57static void rtt_reset(struct sock *sk)
58{
59 struct tcp_sock *tp = tcp_sk(sk);
60 struct illinois *ca = inet_csk_ca(sk);
61
62 ca->end_seq = tp->snd_nxt;
63 ca->cnt_rtt = 0;
64 ca->sum_rtt = 0;
65
66
67}
68
69static void tcp_illinois_init(struct sock *sk)
70{
71 struct illinois *ca = inet_csk_ca(sk);
72
73 ca->alpha = ALPHA_MAX;
74 ca->beta = BETA_BASE;
75 ca->base_rtt = 0x7fffffff;
76 ca->max_rtt = 0;
77
78 ca->acked = 0;
79 ca->rtt_low = 0;
80 ca->rtt_above = 0;
81
82 rtt_reset(sk);
83}
84
85
86static void tcp_illinois_acked(struct sock *sk, u32 pkts_acked, s32 rtt)
87{
88 struct illinois *ca = inet_csk_ca(sk);
89
90 ca->acked = pkts_acked;
91
92
93 if (rtt < 0)
94 return;
95
96
97 if (rtt > RTT_MAX)
98 rtt = RTT_MAX;
99
100
101 if (ca->base_rtt > rtt)
102 ca->base_rtt = rtt;
103
104
105 if (ca->max_rtt < rtt)
106 ca->max_rtt = rtt;
107
108 ++ca->cnt_rtt;
109 ca->sum_rtt += rtt;
110}
111
112
113static inline u32 max_delay(const struct illinois *ca)
114{
115 return ca->max_rtt - ca->base_rtt;
116}
117
118
119static inline u32 avg_delay(const struct illinois *ca)
120{
121 u64 t = ca->sum_rtt;
122
123 do_div(t, ca->cnt_rtt);
124 return t - ca->base_rtt;
125}
126
127
128
129
130
131
132
133
134
135
136
137
138
139static u32 alpha(struct illinois *ca, u32 da, u32 dm)
140{
141 u32 d1 = dm / 100;
142
143 if (da <= d1) {
144
145 if (!ca->rtt_above)
146 return ALPHA_MAX;
147
148
149
150
151 if (++ca->rtt_low < theta)
152 return ca->alpha;
153
154 ca->rtt_low = 0;
155 ca->rtt_above = 0;
156 return ALPHA_MAX;
157 }
158
159 ca->rtt_above = 1;
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177 dm -= d1;
178 da -= d1;
179 return (dm * ALPHA_MAX) /
180 (dm + (da * (ALPHA_MAX - ALPHA_MIN)) / ALPHA_MIN);
181}
182
183
184
185
186
187
188
189
190
191static u32 beta(u32 da, u32 dm)
192{
193 u32 d2, d3;
194
195 d2 = dm / 10;
196 if (da <= d2)
197 return BETA_MIN;
198
199 d3 = (8 * dm) / 10;
200 if (da >= d3 || d3 <= d2)
201 return BETA_MAX;
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216 return (BETA_MIN * d3 - BETA_MAX * d2 + (BETA_MAX - BETA_MIN) * da)
217 / (d3 - d2);
218}
219
220
221static void update_params(struct sock *sk)
222{
223 struct tcp_sock *tp = tcp_sk(sk);
224 struct illinois *ca = inet_csk_ca(sk);
225
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);
232
233 ca->alpha = alpha(ca, da, dm);
234 ca->beta = beta(da, dm);
235 }
236
237 rtt_reset(sk);
238}
239
240
241
242
243static void tcp_illinois_state(struct sock *sk, u8 new_state)
244{
245 struct illinois *ca = inet_csk_ca(sk);
246
247 if (new_state == TCP_CA_Loss) {
248 ca->alpha = ALPHA_BASE;
249 ca->beta = BETA_BASE;
250 ca->rtt_low = 0;
251 ca->rtt_above = 0;
252 rtt_reset(sk);
253 }
254}
255
256
257
258
259static void tcp_illinois_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
260{
261 struct tcp_sock *tp = tcp_sk(sk);
262 struct illinois *ca = inet_csk_ca(sk);
263
264 if (after(ack, ca->end_seq))
265 update_params(sk);
266
267
268 if (!tcp_is_cwnd_limited(sk, in_flight))
269 return;
270
271
272 if (tp->snd_cwnd <= tp->snd_ssthresh)
273 tcp_slow_start(tp);
274
275 else {
276 u32 delta;
277
278
279 tp->snd_cwnd_cnt += ca->acked;
280 ca->acked = 1;
281
282
283
284
285 delta = (tp->snd_cwnd_cnt * ca->alpha) >> ALPHA_SHIFT;
286 if (delta >= tp->snd_cwnd) {
287 tp->snd_cwnd = min(tp->snd_cwnd + delta / tp->snd_cwnd,
288 (u32) tp->snd_cwnd_clamp);
289 tp->snd_cwnd_cnt = 0;
290 }
291 }
292}
293
294static u32 tcp_illinois_ssthresh(struct sock *sk)
295{
296 struct tcp_sock *tp = tcp_sk(sk);
297 struct illinois *ca = inet_csk_ca(sk);
298
299
300 return max(tp->snd_cwnd - ((tp->snd_cwnd * ca->beta) >> BETA_SHIFT), 2U);
301}
302
303
304
305static void tcp_illinois_info(struct sock *sk, u32 ext,
306 struct sk_buff *skb)
307{
308 const struct illinois *ca = inet_csk_ca(sk);
309
310 if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
311 struct tcpvegas_info info = {
312 .tcpv_enabled = 1,
313 .tcpv_rttcnt = ca->cnt_rtt,
314 .tcpv_minrtt = ca->base_rtt,
315 };
316
317 if (info.tcpv_rttcnt > 0) {
318 u64 t = ca->sum_rtt;
319
320 do_div(t, info.tcpv_rttcnt);
321 info.tcpv_rtt = t;
322 }
323 nla_put(skb, INET_DIAG_VEGASINFO, sizeof(info), &info);
324 }
325}
326
327static struct tcp_congestion_ops tcp_illinois __read_mostly = {
328 .flags = TCP_CONG_RTT_STAMP,
329 .init = tcp_illinois_init,
330 .ssthresh = tcp_illinois_ssthresh,
331 .min_cwnd = tcp_reno_min_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,
336
337 .owner = THIS_MODULE,
338 .name = "illinois",
339};
340
341static int __init tcp_illinois_register(void)
342{
343 BUILD_BUG_ON(sizeof(struct illinois) > ICSK_CA_PRIV_SIZE);
344 return tcp_register_congestion_control(&tcp_illinois);
345}
346
347static void __exit tcp_illinois_unregister(void)
348{
349 tcp_unregister_congestion_control(&tcp_illinois);
350}
351
352module_init(tcp_illinois_register);
353module_exit(tcp_illinois_unregister);
354
355MODULE_AUTHOR("Stephen Hemminger, Shao Liu");
356MODULE_LICENSE("GPL");
357MODULE_DESCRIPTION("TCP Illinois");
358MODULE_VERSION("1.0");
359