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#include <linux/mm.h>
28#include <linux/module.h>
29#include <linux/math64.h>
30#include <net/tcp.h>
31#include <linux/inet_diag.h>
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57static int nv_pad __read_mostly = 10;
58static int nv_pad_buffer __read_mostly = 2;
59static int nv_reset_period __read_mostly = 5;
60static int nv_min_cwnd __read_mostly = 2;
61static int nv_cong_dec_mult __read_mostly = 30 * 128 / 100;
62static int nv_ssthresh_factor __read_mostly = 8;
63static int nv_rtt_factor __read_mostly = 128;
64static int nv_loss_dec_factor __read_mostly = 512;
65static int nv_cwnd_growth_rate_neg __read_mostly = 8;
66static int nv_cwnd_growth_rate_pos __read_mostly;
67static int nv_dec_eval_min_calls __read_mostly = 60;
68static int nv_inc_eval_min_calls __read_mostly = 20;
69static int nv_ssthresh_eval_min_calls __read_mostly = 30;
70static int nv_stop_rtt_cnt __read_mostly = 10;
71static int nv_rtt_min_cnt __read_mostly = 2;
72
73module_param(nv_pad, int, 0644);
74MODULE_PARM_DESC(nv_pad, "max queued packets allowed in network");
75module_param(nv_reset_period, int, 0644);
76MODULE_PARM_DESC(nv_reset_period, "nv_min_rtt reset period (secs)");
77module_param(nv_min_cwnd, int, 0644);
78MODULE_PARM_DESC(nv_min_cwnd, "NV will not decrease cwnd below this value"
79 " without losses");
80
81
82struct tcpnv {
83 unsigned long nv_min_rtt_reset_jiffies;
84
85 s8 cwnd_growth_factor;
86
87 u8 available8;
88 u16 available16;
89 u32 loss_cwnd;
90 u8 nv_allow_cwnd_growth:1,
91 nv_reset:1,
92 nv_catchup:1;
93
94 u8 nv_eval_call_cnt;
95 u8 nv_min_cwnd;
96
97
98
99
100
101 u8 nv_rtt_cnt; ;
102 u32 nv_last_rtt;
103 u32 nv_min_rtt;
104 u32 nv_min_rtt_new;
105 u32 nv_rtt_max_rate;
106 u32 nv_rtt_start_seq;
107
108 u32 nv_last_snd_una;
109
110
111 u32 nv_no_cong_cnt;
112};
113
114#define NV_INIT_RTT U32_MAX
115#define NV_MIN_CWND 4
116#define NV_MIN_CWND_GROW 2
117#define NV_TSO_CWND_BOUND 80
118
119static inline void tcpnv_reset(struct tcpnv *ca, struct sock *sk)
120{
121 struct tcp_sock *tp = tcp_sk(sk);
122
123 ca->nv_reset = 0;
124 ca->loss_cwnd = 0;
125 ca->nv_no_cong_cnt = 0;
126 ca->nv_rtt_cnt = 0;
127 ca->nv_last_rtt = 0;
128 ca->nv_rtt_max_rate = 0;
129 ca->nv_rtt_start_seq = tp->snd_una;
130 ca->nv_eval_call_cnt = 0;
131 ca->nv_last_snd_una = tp->snd_una;
132}
133
134static void tcpnv_init(struct sock *sk)
135{
136 struct tcpnv *ca = inet_csk_ca(sk);
137
138 tcpnv_reset(ca, sk);
139
140 ca->nv_allow_cwnd_growth = 1;
141 ca->nv_min_rtt_reset_jiffies = jiffies + 2 * HZ;
142 ca->nv_min_rtt = NV_INIT_RTT;
143 ca->nv_min_rtt_new = NV_INIT_RTT;
144 ca->nv_min_cwnd = NV_MIN_CWND;
145 ca->nv_catchup = 0;
146 ca->cwnd_growth_factor = 0;
147}
148
149static void tcpnv_cong_avoid(struct sock *sk, u32 ack, u32 acked)
150{
151 struct tcp_sock *tp = tcp_sk(sk);
152 struct tcpnv *ca = inet_csk_ca(sk);
153 u32 cnt;
154
155 if (!tcp_is_cwnd_limited(sk))
156 return;
157
158
159 if (!ca->nv_allow_cwnd_growth)
160 return;
161
162 if (tcp_in_slow_start(tp)) {
163 acked = tcp_slow_start(tp, acked);
164 if (!acked)
165 return;
166 }
167
168 if (ca->cwnd_growth_factor < 0) {
169 cnt = tp->snd_cwnd << -ca->cwnd_growth_factor;
170 tcp_cong_avoid_ai(tp, cnt, acked);
171 } else {
172 cnt = max(4U, tp->snd_cwnd >> ca->cwnd_growth_factor);
173 tcp_cong_avoid_ai(tp, cnt, acked);
174 }
175}
176
177static u32 tcpnv_recalc_ssthresh(struct sock *sk)
178{
179 const struct tcp_sock *tp = tcp_sk(sk);
180 struct tcpnv *ca = inet_csk_ca(sk);
181
182 ca->loss_cwnd = tp->snd_cwnd;
183 return max((tp->snd_cwnd * nv_loss_dec_factor) >> 10, 2U);
184}
185
186static u32 tcpnv_undo_cwnd(struct sock *sk)
187{
188 struct tcpnv *ca = inet_csk_ca(sk);
189
190 return max(tcp_sk(sk)->snd_cwnd, ca->loss_cwnd);
191}
192
193static void tcpnv_state(struct sock *sk, u8 new_state)
194{
195 struct tcpnv *ca = inet_csk_ca(sk);
196
197 if (new_state == TCP_CA_Open && ca->nv_reset) {
198 tcpnv_reset(ca, sk);
199 } else if (new_state == TCP_CA_Loss || new_state == TCP_CA_CWR ||
200 new_state == TCP_CA_Recovery) {
201 ca->nv_reset = 1;
202 ca->nv_allow_cwnd_growth = 0;
203 if (new_state == TCP_CA_Loss) {
204
205 if (ca->cwnd_growth_factor > 0)
206 ca->cwnd_growth_factor = 0;
207
208 if (nv_cwnd_growth_rate_neg > 0 &&
209 ca->cwnd_growth_factor > -8)
210 ca->cwnd_growth_factor--;
211 }
212 }
213}
214
215
216
217static void tcpnv_acked(struct sock *sk, const struct ack_sample *sample)
218{
219 const struct inet_connection_sock *icsk = inet_csk(sk);
220 struct tcp_sock *tp = tcp_sk(sk);
221 struct tcpnv *ca = inet_csk_ca(sk);
222 unsigned long now = jiffies;
223 s64 rate64 = 0;
224 u32 rate, max_win, cwnd_by_slope;
225 u32 avg_rtt;
226 u32 bytes_acked = 0;
227
228
229 if (sample->rtt_us < 0)
230 return;
231
232
233 if (icsk->icsk_ca_state != TCP_CA_Open &&
234 icsk->icsk_ca_state != TCP_CA_Disorder)
235 return;
236
237
238 if (ca->nv_catchup && tp->snd_cwnd >= nv_min_cwnd) {
239 ca->nv_catchup = 0;
240 ca->nv_allow_cwnd_growth = 0;
241 }
242
243 bytes_acked = tp->snd_una - ca->nv_last_snd_una;
244 ca->nv_last_snd_una = tp->snd_una;
245
246 if (sample->in_flight == 0)
247 return;
248
249
250 if (nv_rtt_factor > 0) {
251 if (ca->nv_last_rtt > 0) {
252 avg_rtt = (((u64)sample->rtt_us) * nv_rtt_factor +
253 ((u64)ca->nv_last_rtt)
254 * (256 - nv_rtt_factor)) >> 8;
255 } else {
256 avg_rtt = sample->rtt_us;
257 ca->nv_min_rtt = avg_rtt << 1;
258 }
259 ca->nv_last_rtt = avg_rtt;
260 } else {
261 avg_rtt = sample->rtt_us;
262 }
263
264
265 rate64 = ((u64)sample->in_flight) * 8000000;
266 rate = (u32)div64_u64(rate64, (u64)(avg_rtt * 100));
267
268
269
270
271
272 if (ca->nv_rtt_max_rate < rate)
273 ca->nv_rtt_max_rate = rate;
274
275
276 if (ca->nv_eval_call_cnt < 255)
277 ca->nv_eval_call_cnt++;
278
279
280 if (avg_rtt < ca->nv_min_rtt)
281 ca->nv_min_rtt = avg_rtt;
282
283
284 if (avg_rtt < ca->nv_min_rtt_new)
285 ca->nv_min_rtt_new = avg_rtt;
286
287
288
289
290
291
292
293
294
295
296 if (time_after_eq(now, ca->nv_min_rtt_reset_jiffies)) {
297 unsigned char rand;
298
299 ca->nv_min_rtt = ca->nv_min_rtt_new;
300 ca->nv_min_rtt_new = NV_INIT_RTT;
301 get_random_bytes(&rand, 1);
302 ca->nv_min_rtt_reset_jiffies =
303 now + ((nv_reset_period * (384 + rand) * HZ) >> 9);
304
305
306
307 ca->nv_min_cwnd = max(ca->nv_min_cwnd / 2, NV_MIN_CWND);
308 }
309
310
311 if (before(ca->nv_rtt_start_seq, tp->snd_una)) {
312 ca->nv_rtt_start_seq = tp->snd_nxt;
313 if (ca->nv_rtt_cnt < 0xff)
314
315 ca->nv_rtt_cnt++;
316
317
318
319
320
321
322 if (ca->nv_eval_call_cnt == 1 &&
323 bytes_acked >= (ca->nv_min_cwnd - 1) * tp->mss_cache &&
324 ca->nv_min_cwnd < (NV_TSO_CWND_BOUND + 1)) {
325 ca->nv_min_cwnd = min(ca->nv_min_cwnd
326 + NV_MIN_CWND_GROW,
327 NV_TSO_CWND_BOUND + 1);
328 ca->nv_rtt_start_seq = tp->snd_nxt +
329 ca->nv_min_cwnd * tp->mss_cache;
330 ca->nv_eval_call_cnt = 0;
331 ca->nv_allow_cwnd_growth = 1;
332 return;
333 }
334
335
336
337
338
339 cwnd_by_slope = (u32)
340 div64_u64(((u64)ca->nv_rtt_max_rate) * ca->nv_min_rtt,
341 (u64)(80000 * tp->mss_cache));
342 max_win = cwnd_by_slope + nv_pad;
343
344
345
346
347
348 if (tp->snd_cwnd > max_win) {
349
350
351
352
353
354
355
356 if (ca->nv_rtt_cnt < nv_rtt_min_cnt) {
357 return;
358 } else if (tp->snd_ssthresh == TCP_INFINITE_SSTHRESH) {
359 if (ca->nv_eval_call_cnt <
360 nv_ssthresh_eval_min_calls)
361 return;
362
363 } else if (ca->nv_eval_call_cnt <
364 nv_dec_eval_min_calls) {
365 if (ca->nv_allow_cwnd_growth &&
366 ca->nv_rtt_cnt > nv_stop_rtt_cnt)
367 ca->nv_allow_cwnd_growth = 0;
368 return;
369 }
370
371
372 ca->nv_allow_cwnd_growth = 0;
373 tp->snd_ssthresh =
374 (nv_ssthresh_factor * max_win) >> 3;
375 if (tp->snd_cwnd - max_win > 2) {
376
377 int dec;
378
379 dec = max(2U, ((tp->snd_cwnd - max_win) *
380 nv_cong_dec_mult) >> 7);
381 tp->snd_cwnd -= dec;
382 } else if (nv_cong_dec_mult > 0) {
383 tp->snd_cwnd = max_win;
384 }
385 if (ca->cwnd_growth_factor > 0)
386 ca->cwnd_growth_factor = 0;
387 ca->nv_no_cong_cnt = 0;
388 } else if (tp->snd_cwnd <= max_win - nv_pad_buffer) {
389
390 if (ca->nv_eval_call_cnt < nv_inc_eval_min_calls)
391 return;
392
393 ca->nv_allow_cwnd_growth = 1;
394 ca->nv_no_cong_cnt++;
395 if (ca->cwnd_growth_factor < 0 &&
396 nv_cwnd_growth_rate_neg > 0 &&
397 ca->nv_no_cong_cnt > nv_cwnd_growth_rate_neg) {
398 ca->cwnd_growth_factor++;
399 ca->nv_no_cong_cnt = 0;
400 } else if (ca->cwnd_growth_factor >= 0 &&
401 nv_cwnd_growth_rate_pos > 0 &&
402 ca->nv_no_cong_cnt >
403 nv_cwnd_growth_rate_pos) {
404 ca->cwnd_growth_factor++;
405 ca->nv_no_cong_cnt = 0;
406 }
407 } else {
408
409 return;
410 }
411
412
413 ca->nv_eval_call_cnt = 0;
414 ca->nv_rtt_cnt = 0;
415 ca->nv_rtt_max_rate = 0;
416
417
418
419
420
421 if (tp->snd_cwnd < nv_min_cwnd)
422 tp->snd_cwnd = nv_min_cwnd;
423 }
424}
425
426
427size_t tcpnv_get_info(struct sock *sk, u32 ext, int *attr,
428 union tcp_cc_info *info)
429{
430 const struct tcpnv *ca = inet_csk_ca(sk);
431
432 if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
433 info->vegas.tcpv_enabled = 1;
434 info->vegas.tcpv_rttcnt = ca->nv_rtt_cnt;
435 info->vegas.tcpv_rtt = ca->nv_last_rtt;
436 info->vegas.tcpv_minrtt = ca->nv_min_rtt;
437
438 *attr = INET_DIAG_VEGASINFO;
439 return sizeof(struct tcpvegas_info);
440 }
441 return 0;
442}
443EXPORT_SYMBOL_GPL(tcpnv_get_info);
444
445static struct tcp_congestion_ops tcpnv __read_mostly = {
446 .init = tcpnv_init,
447 .ssthresh = tcpnv_recalc_ssthresh,
448 .cong_avoid = tcpnv_cong_avoid,
449 .set_state = tcpnv_state,
450 .undo_cwnd = tcpnv_undo_cwnd,
451 .pkts_acked = tcpnv_acked,
452 .get_info = tcpnv_get_info,
453
454 .owner = THIS_MODULE,
455 .name = "nv",
456};
457
458static int __init tcpnv_register(void)
459{
460 BUILD_BUG_ON(sizeof(struct tcpnv) > ICSK_CA_PRIV_SIZE);
461
462 return tcp_register_congestion_control(&tcpnv);
463}
464
465static void __exit tcpnv_unregister(void)
466{
467 tcp_unregister_congestion_control(&tcpnv);
468}
469
470module_init(tcpnv_register);
471module_exit(tcpnv_unregister);
472
473MODULE_AUTHOR("Lawrence Brakmo");
474MODULE_LICENSE("GPL");
475MODULE_DESCRIPTION("TCP NV");
476MODULE_VERSION("1.0");
477