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