1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include <linux/mm.h>
35#include <linux/module.h>
36#include <linux/skbuff.h>
37#include <linux/inet_diag.h>
38
39#include <net/tcp.h>
40
41#include "tcp_vegas.h"
42
43static int alpha = 2;
44static int beta = 4;
45static int gamma = 1;
46
47module_param(alpha, int, 0644);
48MODULE_PARM_DESC(alpha, "lower bound of packets in network");
49module_param(beta, int, 0644);
50MODULE_PARM_DESC(beta, "upper bound of packets in network");
51module_param(gamma, int, 0644);
52MODULE_PARM_DESC(gamma, "limit on increase (scale by 2)");
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70static void vegas_enable(struct sock *sk)
71{
72 const struct tcp_sock *tp = tcp_sk(sk);
73 struct vegas *vegas = inet_csk_ca(sk);
74
75
76 vegas->doing_vegas_now = 1;
77
78
79 vegas->beg_snd_nxt = tp->snd_nxt;
80
81 vegas->cntRTT = 0;
82 vegas->minRTT = 0x7fffffff;
83}
84
85
86static inline void vegas_disable(struct sock *sk)
87{
88 struct vegas *vegas = inet_csk_ca(sk);
89
90 vegas->doing_vegas_now = 0;
91}
92
93void tcp_vegas_init(struct sock *sk)
94{
95 struct vegas *vegas = inet_csk_ca(sk);
96
97 vegas->baseRTT = 0x7fffffff;
98 vegas_enable(sk);
99}
100EXPORT_SYMBOL_GPL(tcp_vegas_init);
101
102
103
104
105
106
107
108
109
110void tcp_vegas_pkts_acked(struct sock *sk, const struct ack_sample *sample)
111{
112 struct vegas *vegas = inet_csk_ca(sk);
113 u32 vrtt;
114
115 if (sample->rtt_us < 0)
116 return;
117
118
119 vrtt = sample->rtt_us + 1;
120
121
122 if (vrtt < vegas->baseRTT)
123 vegas->baseRTT = vrtt;
124
125
126
127
128 vegas->minRTT = min(vegas->minRTT, vrtt);
129 vegas->cntRTT++;
130}
131EXPORT_SYMBOL_GPL(tcp_vegas_pkts_acked);
132
133void tcp_vegas_state(struct sock *sk, u8 ca_state)
134{
135 if (ca_state == TCP_CA_Open)
136 vegas_enable(sk);
137 else
138 vegas_disable(sk);
139}
140EXPORT_SYMBOL_GPL(tcp_vegas_state);
141
142
143
144
145
146
147
148
149
150
151void tcp_vegas_cwnd_event(struct sock *sk, enum tcp_ca_event event)
152{
153 if (event == CA_EVENT_CWND_RESTART ||
154 event == CA_EVENT_TX_START)
155 tcp_vegas_init(sk);
156}
157EXPORT_SYMBOL_GPL(tcp_vegas_cwnd_event);
158
159static inline u32 tcp_vegas_ssthresh(struct tcp_sock *tp)
160{
161 return min(tp->snd_ssthresh, tp->snd_cwnd);
162}
163
164static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack, u32 acked)
165{
166 struct tcp_sock *tp = tcp_sk(sk);
167 struct vegas *vegas = inet_csk_ca(sk);
168
169 if (!vegas->doing_vegas_now) {
170 tcp_reno_cong_avoid(sk, ack, acked);
171 return;
172 }
173
174 if (after(ack, vegas->beg_snd_nxt)) {
175
176
177
178
179
180 vegas->beg_snd_nxt = tp->snd_nxt;
181
182
183
184
185
186
187
188
189
190
191 if (vegas->cntRTT <= 2) {
192
193
194
195 tcp_reno_cong_avoid(sk, ack, acked);
196 } else {
197 u32 rtt, diff;
198 u64 target_cwnd;
199
200
201
202
203
204
205
206
207
208
209
210
211 rtt = vegas->minRTT;
212
213
214
215
216
217
218
219 target_cwnd = (u64)tp->snd_cwnd * vegas->baseRTT;
220 do_div(target_cwnd, rtt);
221
222
223
224
225
226 diff = tp->snd_cwnd * (rtt-vegas->baseRTT) / vegas->baseRTT;
227
228 if (diff > gamma && tcp_in_slow_start(tp)) {
229
230
231
232
233
234
235
236
237
238
239
240 tp->snd_cwnd = min(tp->snd_cwnd, (u32)target_cwnd+1);
241 tp->snd_ssthresh = tcp_vegas_ssthresh(tp);
242
243 } else if (tcp_in_slow_start(tp)) {
244
245 tcp_slow_start(tp, acked);
246 } else {
247
248
249
250
251
252 if (diff > beta) {
253
254
255
256 tp->snd_cwnd--;
257 tp->snd_ssthresh
258 = tcp_vegas_ssthresh(tp);
259 } else if (diff < alpha) {
260
261
262
263 tp->snd_cwnd++;
264 } else {
265
266
267
268 }
269 }
270
271 if (tp->snd_cwnd < 2)
272 tp->snd_cwnd = 2;
273 else if (tp->snd_cwnd > tp->snd_cwnd_clamp)
274 tp->snd_cwnd = tp->snd_cwnd_clamp;
275
276 tp->snd_ssthresh = tcp_current_ssthresh(sk);
277 }
278
279
280 vegas->cntRTT = 0;
281 vegas->minRTT = 0x7fffffff;
282 }
283
284 else if (tcp_in_slow_start(tp))
285 tcp_slow_start(tp, acked);
286}
287
288
289size_t tcp_vegas_get_info(struct sock *sk, u32 ext, int *attr,
290 union tcp_cc_info *info)
291{
292 const struct vegas *ca = inet_csk_ca(sk);
293
294 if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
295 info->vegas.tcpv_enabled = ca->doing_vegas_now,
296 info->vegas.tcpv_rttcnt = ca->cntRTT,
297 info->vegas.tcpv_rtt = ca->baseRTT,
298 info->vegas.tcpv_minrtt = ca->minRTT,
299
300 *attr = INET_DIAG_VEGASINFO;
301 return sizeof(struct tcpvegas_info);
302 }
303 return 0;
304}
305EXPORT_SYMBOL_GPL(tcp_vegas_get_info);
306
307static struct tcp_congestion_ops tcp_vegas __read_mostly = {
308 .init = tcp_vegas_init,
309 .ssthresh = tcp_reno_ssthresh,
310 .undo_cwnd = tcp_reno_undo_cwnd,
311 .cong_avoid = tcp_vegas_cong_avoid,
312 .pkts_acked = tcp_vegas_pkts_acked,
313 .set_state = tcp_vegas_state,
314 .cwnd_event = tcp_vegas_cwnd_event,
315 .get_info = tcp_vegas_get_info,
316
317 .owner = THIS_MODULE,
318 .name = "vegas",
319};
320
321static int __init tcp_vegas_register(void)
322{
323 BUILD_BUG_ON(sizeof(struct vegas) > ICSK_CA_PRIV_SIZE);
324 tcp_register_congestion_control(&tcp_vegas);
325 return 0;
326}
327
328static void __exit tcp_vegas_unregister(void)
329{
330 tcp_unregister_congestion_control(&tcp_vegas);
331}
332
333module_init(tcp_vegas_register);
334module_exit(tcp_vegas_unregister);
335
336MODULE_AUTHOR("Stephen Hemminger");
337MODULE_LICENSE("GPL");
338MODULE_DESCRIPTION("TCP Vegas");
339