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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
/*
* TCP NV: TCP with Congestion Avoidance
*
* TCP-NV is a successor of TCP-Vegas that has been developed to
* deal with the issues that occur in modern networks.
* Like TCP-Vegas, TCP-NV supports true congestion avoidance,
* the ability to detect congestion before packet losses occur.
* When congestion (queue buildup) starts to occur, TCP-NV
* predicts what the cwnd size should be for the current
* throughput and it reduces the cwnd proportionally to
* the difference between the current cwnd and the predicted cwnd.
*
* NV is only recommeneded for traffic within a data center, and when
* all the flows are NV (at least those within the data center). This
* is due to the inherent unfairness between flows using losses to
* detect congestion (congestion control) and those that use queue
* buildup to detect congestion (congestion avoidance).
*
* Note: High NIC coalescence values may lower the performance of NV
* due to the increased noise in RTT values. In particular, we have
* seen issues with rx-frames values greater than 8.
*
* TODO:
* 1) Add mechanism to deal with reverse congestion.
*/
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/math64.h>
#include <net/tcp.h>
#include <linux/inet_diag.h>
/* TCP NV parameters
*
* nv_pad Max number of queued packets allowed in network
* nv_pad_buffer Do not grow cwnd if this closed to nv_pad
* nv_reset_period How often (in) seconds)to reset min_rtt
* nv_min_cwnd Don't decrease cwnd below this if there are no losses
* nv_cong_dec_mult Decrease cwnd by X% (30%) of congestion when detected
* nv_ssthresh_factor On congestion set ssthresh to this * <desired cwnd> / 8
* nv_rtt_factor RTT averaging factor
* nv_loss_dec_factor Decrease cwnd to this (80%) when losses occur
* nv_dec_eval_min_calls Wait this many RTT measurements before dec cwnd
* nv_inc_eval_min_calls Wait this many RTT measurements before inc cwnd
* nv_ssthresh_eval_min_calls Wait this many RTT measurements before stopping
* slow-start due to congestion
* nv_stop_rtt_cnt Only grow cwnd for this many RTTs after non-congestion
* nv_rtt_min_cnt Wait these many RTTs before making congesion decision
* nv_cwnd_growth_rate_neg
* nv_cwnd_growth_rate_pos
* How quickly to double growth rate (not rate) of cwnd when not
* congested. One value (nv_cwnd_growth_rate_neg) for when
* rate < 1 pkt/RTT (after losses). The other (nv_cwnd_growth_rate_pos)
* otherwise.
*/
static int nv_pad __read_mostly = 10;
static int nv_pad_buffer __read_mostly = 2;
static int nv_reset_period __read_mostly = 5; /* in seconds */
static int nv_min_cwnd __read_mostly = 2;
static int nv_cong_dec_mult __read_mostly = 30 * 128 / 100; /* = 30% */
static int nv_ssthresh_factor __read_mostly = 8; /* = 1 */
static int nv_rtt_factor __read_mostly = 128; /* = 1/2*old + 1/2*new */
static int nv_loss_dec_factor __read_mostly = 819; /* => 80% */
static int nv_cwnd_growth_rate_neg __read_mostly = 8;
static int nv_cwnd_growth_rate_pos __read_mostly; /* 0 => fixed like Reno */
static int nv_dec_eval_min_calls __read_mostly = 60;
static int nv_inc_eval_min_calls __read_mostly = 20;
static int nv_ssthresh_eval_min_calls __read_mostly = 30;
static int nv_stop_rtt_cnt __read_mostly = 10;
static int nv_rtt_min_cnt __read_mostly = 2;
module_param(nv_pad, int, 0644);
MODULE_PARM_DESC(nv_pad, "max queued packets allowed in network");
module_param(nv_reset_period, int, 0644);
MODULE_PARM_DESC(nv_reset_period, "nv_min_rtt reset period (secs)");
module_param(nv_min_cwnd, int, 0644);
MODULE_PARM_DESC(nv_min_cwnd, "NV will not decrease cwnd below this value"
" without losses");
/* TCP NV Parameters */
struct tcpnv {
unsigned long nv_min_rtt_reset_jiffies; /* when to switch to
* nv_min_rtt_new */
s8 cwnd_growth_factor; /* Current cwnd growth factor,
* < 0 => less than 1 packet/RTT */
u8 available8;
u16 available16;
u8 nv_allow_cwnd_growth:1, /* whether cwnd can grow */
nv_reset:1, /* whether to reset values */
nv_catchup:1; /* whether we are growing because
* of temporary cwnd decrease */
u8 nv_eval_call_cnt; /* call count since last eval */
u8 nv_min_cwnd; /* nv won't make a ca decision if cwnd is
* smaller than this. It may grow to handle
* TSO, LRO and interrupt coalescence because
* with these a small cwnd cannot saturate
* the link. Note that this is different from
* the file local nv_min_cwnd */
u8 nv_rtt_cnt; /* RTTs without making ca decision */;
u32 nv_last_rtt; /* last rtt */
u32 nv_min_rtt; /* active min rtt. Used to determine slope */
u32 nv_min_rtt_new; /* min rtt for future use */
u32 nv_base_rtt; /* If non-zero it represents the threshold for
* congestion */
u32 nv_lower_bound_rtt; /* Used in conjunction with nv_base_rtt. It is
* set to 80% of nv_base_rtt. It helps reduce
* unfairness between flows */
u32 nv_rtt_max_rate; /* max rate seen during current RTT */
u32 nv_rtt_start_seq; /* current RTT ends when packet arrives
* acking beyond nv_rtt_start_seq */
u32 nv_last_snd_una; /* Previous value of tp->snd_una. It is
* used to determine bytes acked since last
* call to bictcp_acked */
u32 nv_no_cong_cnt; /* Consecutive no congestion decisions */
};
#define NV_INIT_RTT U32_MAX
#define NV_MIN_CWND 4
#define NV_MIN_CWND_GROW 2
#define NV_TSO_CWND_BOUND 80
static inline void tcpnv_reset(struct tcpnv *ca, struct sock *sk)
{
struct tcp_sock *tp = tcp_sk(sk);
ca->nv_reset = 0;
ca->nv_no_cong_cnt = 0;
ca->nv_rtt_cnt = 0;
ca->nv_last_rtt = 0;
ca->nv_rtt_max_rate = 0;
ca->nv_rtt_start_seq = tp->snd_una;
ca->nv_eval_call_cnt = 0;
ca->nv_last_snd_una = tp->snd_una;
}
static void tcpnv_init(struct sock *sk)
{
struct tcpnv *ca = inet_csk_ca(sk);
int base_rtt;
tcpnv_reset(ca, sk);
/* See if base_rtt is available from socket_ops bpf program.
* It is meant to be used in environments, such as communication
* within a datacenter, where we have reasonable estimates of
* RTTs
*/
base_rtt = tcp_call_bpf(sk, BPF_SOCK_OPS_BASE_RTT);
if (base_rtt > 0) {
ca->nv_base_rtt = base_rtt;
ca->nv_lower_bound_rtt = (base_rtt * 205) >> 8; /* 80% */
} else {
ca->nv_base_rtt = 0;
ca->nv_lower_bound_rtt = 0;
}
ca->nv_allow_cwnd_growth = 1;
ca->nv_min_rtt_reset_jiffies = jiffies + 2 * HZ;
ca->nv_min_rtt = NV_INIT_RTT;
ca->nv_min_rtt_new = NV_INIT_RTT;
ca->nv_min_cwnd = NV_MIN_CWND;
ca->nv_catchup = 0;
ca->cwnd_growth_factor = 0;
}
/* If provided, apply upper (base_rtt) and lower (lower_bound_rtt)
* bounds to RTT.
*/
inline u32 nv_get_bounded_rtt(struct tcpnv *ca, u32 val)
{
if (ca->nv_lower_bound_rtt > 0 && val < ca->nv_lower_bound_rtt)
return ca->nv_lower_bound_rtt;
else if (ca->nv_base_rtt > 0 && val > ca->nv_base_rtt)
return ca->nv_base_rtt;
else
return val;
}
static void tcpnv_cong_avoid(struct sock *sk, u32 ack, u32 acked)
{
struct tcp_sock *tp = tcp_sk(sk);
struct tcpnv *ca = inet_csk_ca(sk);
u32 cnt;
if (!tcp_is_cwnd_limited(sk))
return;
/* Only grow cwnd if NV has not detected congestion */
if (!ca->nv_allow_cwnd_growth)
return;
if (tcp_in_slow_start(tp)) {
acked = tcp_slow_start(tp, acked);
if (!acked)
return;
}
if (ca->cwnd_growth_factor < 0) {
cnt = tp->snd_cwnd << -ca->cwnd_growth_factor;
tcp_cong_avoid_ai(tp, cnt, acked);
} else {
cnt = max(4U, tp->snd_cwnd >> ca->cwnd_growth_factor);
tcp_cong_avoid_ai(tp, cnt, acked);
}
}
static u32 tcpnv_recalc_ssthresh(struct sock *sk)
{
const struct tcp_sock *tp = tcp_sk(sk);
return max((tp->snd_cwnd * nv_loss_dec_factor) >> 10, 2U);
}
static void tcpnv_state(struct sock *sk, u8 new_state)
{
struct tcpnv *ca = inet_csk_ca(sk);
if (new_state == TCP_CA_Open && ca->nv_reset) {
tcpnv_reset(ca, sk);
} else if (new_state == TCP_CA_Loss || new_state == TCP_CA_CWR ||
new_state == TCP_CA_Recovery) {
ca->nv_reset = 1;
ca->nv_allow_cwnd_growth = 0;
if (new_state == TCP_CA_Loss) {
/* Reset cwnd growth factor to Reno value */
if (ca->cwnd_growth_factor > 0)
ca->cwnd_growth_factor = 0;
/* Decrease growth rate if allowed */
if (nv_cwnd_growth_rate_neg > 0 &&
ca->cwnd_growth_factor > -8)
ca->cwnd_growth_factor--;
}
}
}
/* Do congestion avoidance calculations for TCP-NV
*/
static void tcpnv_acked(struct sock *sk, const struct ack_sample *sample)
{
const struct inet_connection_sock *icsk = inet_csk<
|