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
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, u32 cnt, s32 rtt_us)
112{
113 struct vegas *vegas = inet_csk_ca(sk);
114 u32 vrtt;
115
116 if (rtt_us < 0)
117 return;
118
119
120 vrtt = 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
137 if (ca_state == TCP_CA_Open)
138 vegas_enable(sk);
139 else
140 vegas_disable(sk);
141}
142EXPORT_SYMBOL_GPL(tcp_vegas_state);
143
144
145
146
147
148
149
150
151
152
153void tcp_vegas_cwnd_event(struct sock *sk, enum tcp_ca_event event)
154{
155 if (event == CA_EVENT_CWND_RESTART ||
156 event == CA_EVENT_TX_START)
157 tcp_vegas_init(sk);
158}
159EXPORT_SYMBOL_GPL(tcp_vegas_cwnd_event);
160
161static inline u32 tcp_vegas_ssthresh(struct tcp_sock *tp)
162{
163 return min(tp->snd_ssthresh, tp->snd_cwnd-1);
164}
165
166static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
167{
168 struct tcp_sock *tp = tcp_sk(sk);
169 struct vegas *vegas = inet_csk_ca(sk);
170
171 if (!vegas->doing_vegas_now) {
172 tcp_reno_cong_avoid(sk, ack, in_flight);
173 return;
174 }
175
176 if (after(ack, vegas->beg_snd_nxt)) {
177
178
179
180
181
182 vegas->beg_snd_nxt = tp->snd_nxt;
183
184
185
186
187
188
189
190
191
192
193 if (vegas->cntRTT <= 2) {
194
195
196
197 tcp_reno_cong_avoid(sk, ack, in_flight);
198 } else {
199 u32 rtt, diff;
200 u64 target_cwnd;
201
202
203
204
205
206
207
208
209
210
211
212
213 rtt = vegas->minRTT;
214
215
216
217
218
219
220
221 target_cwnd = tp->snd_cwnd * vegas->baseRTT / rtt;
222
223
224
225
226
227 diff = tp->snd_cwnd * (rtt-vegas->baseRTT) / vegas->baseRTT;
228
229 if (diff > gamma && tp->snd_cwnd <= tp->snd_ssthresh) {
230
231
232
233
234
235
236
237
238
239
240
241 tp->snd_cwnd = min(tp->snd_cwnd, (u32)target_cwnd+1);
242 tp->snd_ssthresh = tcp_vegas_ssthresh(tp);
243
244 } else if (tp->snd_cwnd <= tp->snd_ssthresh) {
245
246 tcp_slow_start(tp);
247 } else {
248
249
250
251
252
253 if (diff > beta) {
254
255
256
257 tp->snd_cwnd--;
258 tp->snd_ssthresh
259 = tcp_vegas_ssthresh(tp);
260 } else if (diff < alpha) {
261
262
263
264 tp->snd_cwnd++;
265 } else {
266
267
268
269 }
270 }
271
272 if (tp->snd_cwnd < 2)
273 tp->snd_cwnd = 2;
274 else if (tp->snd_cwnd > tp->snd_cwnd_clamp)
275 tp->snd_cwnd = tp->snd_cwnd_clamp;
276
277 tp->snd_ssthresh = tcp_current_ssthresh(sk);
278 }
279
280
281 vegas->cntRTT = 0;
282 vegas->minRTT = 0x7fffffff;
283 }
284
285 else if (tp->snd_cwnd <= tp->snd_ssthresh)
286 tcp_slow_start(tp);
287
288}
289
290
291void tcp_vegas_get_info(struct sock *sk, u32 ext, struct sk_buff *skb)
292{
293 const struct vegas *ca = inet_csk_ca(sk);
294 if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
295 struct tcpvegas_info info = {
296 .tcpv_enabled = ca->doing_vegas_now,
297 .tcpv_rttcnt = ca->cntRTT,
298 .tcpv_rtt = ca->baseRTT,
299 .tcpv_minrtt = ca->minRTT,
300 };
301
302 nla_put(skb, INET_DIAG_VEGASINFO, sizeof(info), &info);
303 }
304}
305EXPORT_SYMBOL_GPL(tcp_vegas_get_info);
306
307static struct tcp_congestion_ops tcp_vegas __read_mostly = {
308 .flags = TCP_CONG_RTT_STAMP,
309 .init = tcp_vegas_init,
310 .ssthresh = tcp_reno_ssthresh,
311 .cong_avoid = tcp_vegas_cong_avoid,
312 .min_cwnd = tcp_reno_min_cwnd,
313 .pkts_acked = tcp_vegas_pkts_acked,
314 .set_state = tcp_vegas_state,
315 .cwnd_event = tcp_vegas_cwnd_event,
316 .get_info = tcp_vegas_get_info,
317
318 .owner = THIS_MODULE,
319 .name = "vegas",
320};
321
322static int __init tcp_vegas_register(void)
323{
324 BUILD_BUG_ON(sizeof(struct vegas) > ICSK_CA_PRIV_SIZE);
325 tcp_register_congestion_control(&tcp_vegas);
326 return 0;
327}
328
329static void __exit tcp_vegas_unregister(void)
330{
331 tcp_unregister_congestion_control(&tcp_vegas);
332}
333
334module_init(tcp_vegas_register);
335module_exit(tcp_vegas_unregister);
336
337MODULE_AUTHOR("Stephen Hemminger");
338MODULE_LICENSE("GPL");
339MODULE_DESCRIPTION("TCP Vegas");
340