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