-
Notifications
You must be signed in to change notification settings - Fork 22
/
tcp_ledbat.c
547 lines (466 loc) · 13.3 KB
/
tcp_ledbat.c
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
* TCP-LEDBAT
* Implement the congestion control algorithm described in
* draft-shalunov-ledbat-congestion-00.txt available at
* http://tools.ietf.org/html/draft-shalunov-ledbat-congestion-00
*
* Our implementation is derived from the TCP-LP kernel implementation
* (cfr. tcp_lp.c)
*
* Created by Silvio Valenti on tue 2nd June 2009
*/
#include <linux/module.h>
#include <net/tcp.h>
#include <linux/vmalloc.h>
/* resolution of owd */
#define LP_RESOL 1000
#define DEBUG_SLOW_START 0
#define DEBUG_DELAY 0
#define DEBUG_OWD_HZ 0
#define DEBUG_NOISE_FILTER 0
#define DEBUG_BASE_HISTO 0
/* NOTE: len are the actual length - 1 */
static int base_histo_len = 10;
static int noise_filter_len = 4;
static int target = 100;
static int gain_num = 1;
static int gain_den = 1;
static int do_ss = 0;
static int ledbat_ssthresh = 0xffff;
module_param(base_histo_len, int, 0644);
MODULE_PARM_DESC(base_histo_len, "length of the base history vector");
module_param(noise_filter_len, int, 0644);
MODULE_PARM_DESC(noise_filter_len, "length of the noise_filter vector");
module_param(target, int, 0644);
MODULE_PARM_DESC(target, "target queuing delay");
module_param(gain_num, int, 0644);
MODULE_PARM_DESC(gain_num, "multiplicative factor of the gain");
module_param(gain_den, int, 0644);
MODULE_PARM_DESC(gain_den, "multiplicative factor of the gain");
/* Our extensions to play with slow start */
#define DO_NOT_SLOWSTART 0
#define DO_SLOWSTART 1
#define DO_SLOWSTART_WITH_THRESHOLD 2
module_param(do_ss, int, 0644);
MODULE_PARM_DESC(do_ss, "do slow start: 0 no, 1 yes, 2 with_ssthresh");
module_param(ledbat_ssthresh, int, 0644);
MODULE_PARM_DESC(ledbat_ssthresh, "slow start threshold");
struct owd_circ_buf {
u32 *buffer;
u8 first;
u8 next;
u8 len;
u8 min;
};
/**
* enum tcp_ledbat_state
* @LP_VALID_RHZ: is remote HZ valid?
* @LP_VALID_OWD: is OWD valid?
* @LP_WITHIN_THR: are we within threshold?
* @LP_WITHIN_INF: are we within inference?
*
* TCP-LEDBAT's state flags.
* We create this set of state flags mainly for debugging.
*/
enum tcp_ledbat_state {
LEDBAT_VALID_RHZ = (1 << 0),
LEDBAT_VALID_OWD = (1 << 1),
LEDBAT_INCREASING = (1 << 2),
LEDBAT_CAN_SS = (1 << 3),
};
/**
* struct ledbat
*/
struct ledbat {
u32 last_rollover;
u32 remote_hz;
u32 remote_ref_time;
u32 local_ref_time;
u32 snd_cwnd_cnt; /* already in struct tcp_sock but we need 32 bits. */
u32 last_ack;
struct owd_circ_buf base_history;
struct owd_circ_buf noise_filter;
u32 flag;
};
static int ledbat_init_circbuf(struct owd_circ_buf *buffer, u16 len)
{
u32 *b = kmalloc(len * sizeof(u32), GFP_KERNEL);
if (b == NULL)
return 1;
buffer->len = len;
buffer->buffer = b;
buffer->first = 0;
buffer->next = 0;
buffer->min = 0;
return 0;
}
static void tcp_ledbat_release(struct sock *sk)
{
struct ledbat *ledbat = inet_csk_ca(sk);
kfree(ledbat->noise_filter.buffer);
kfree(ledbat->base_history.buffer);
}
/**
* tcp_ledbat_init
*/
static void tcp_ledbat_init(struct sock *sk)
{
struct ledbat *ledbat = inet_csk_ca(sk);
ledbat_init_circbuf(&(ledbat->base_history), base_histo_len + 1);
ledbat_init_circbuf(&(ledbat->noise_filter), noise_filter_len + 1);
ledbat->last_rollover = 0;
ledbat->flag = 0;
ledbat->remote_hz = 0;
ledbat->remote_ref_time = 0;
ledbat->local_ref_time = 0;
ledbat->snd_cwnd_cnt = 0;
ledbat->last_ack = 0;
if (do_ss) {
ledbat->flag |= LEDBAT_CAN_SS;
}
}
typedef u32(*ledbat_filter_function) (struct owd_circ_buf *);
static u32 ledbat_min_circ_buff(struct owd_circ_buf *b)
{
/*
The draft requires all history to be set to +infinity on initialization.
We obtain the same behavior returning +infinity in case of empty history.
*/
if (b->first == b->next)
return 0xffffffff;
return b->buffer[b->min];
}
static
u32 ledbat_current_delay(struct ledbat *ledbat, ledbat_filter_function filter)
{
return filter(&(ledbat->noise_filter));
}
static u32 ledbat_base_delay(struct ledbat *ledbat)
{
return ledbat_min_circ_buff(&(ledbat->base_history));
}
#if DEBUG_NOISE_FILTER || DEBUG_BASE_HISTO
static void print_delay(struct owd_circ_buf *cb, char *name)
{
u16 curr = cb->first;
printk(KERN_DEBUG "%s: time %u ", name, tcp_time_stamp);
while (curr != cb->next) {
printk(KERN_DEBUG "%u ", cb->buffer[curr]);
curr = (curr + 1) % cb->len;
}
printk(KERN_DEBUG "min %u, len %u, first %u, next %u\n",
cb->buffer[cb->min], cb->len, cb->first, cb->next);
}
#endif
static
u32 tcp_ledbat_ssthresh(struct sock *sk)
{
u32 res;
switch (do_ss) {
case DO_NOT_SLOWSTART:
case DO_SLOWSTART:
default:
res = tcp_reno_ssthresh(sk);
break;
case DO_SLOWSTART_WITH_THRESHOLD:
res = ledbat_ssthresh;
break;
}
return res;
}
/**
* tcp_ledbat_cong_avoid
*/
static void tcp_ledbat_cong_avoid(struct sock *sk, u32 ack, u32 acked)
// ns2 version takes also this two parameters: u32 rtt, int flag
{
struct ledbat *ledbat = inet_csk_ca(sk);
struct tcp_sock *tp = tcp_sk(sk);
s64 queue_delay;
s64 offset;
s64 cwnd;
u32 max_cwnd;
s64 current_delay;
s64 base_delay;
/*if no valid data return */
if (!(ledbat->flag & LEDBAT_VALID_OWD))
return;
max_cwnd = ((u32) (tp->snd_cwnd)) * target;
/*
This checks that we are not limited by the congestion window nor by the
application, and is basically the same check that it is performed in the
draft.
*/
if (!tcp_is_cwnd_limited(sk))
return;
if (tp->snd_cwnd <= 1)
ledbat->flag |= LEDBAT_CAN_SS;
if (do_ss >= DO_SLOWSTART && tp->snd_cwnd <= tcp_ledbat_ssthresh(sk) &&
(ledbat->flag & LEDBAT_CAN_SS)) {
#if DEBUG_SLOW_START
printk(KERN_DEBUG
"slow_start!!! clamp %d cwnd %d sshthresh %d \n",
tp->snd_cwnd_clamp, tp->snd_cwnd, tp->snd_ssthresh);
#endif
acked = tcp_slow_start(tp, acked);
if (!acked)
return;
} else {
ledbat->flag &= ~LEDBAT_CAN_SS;
}
/* This allows to eventually define new filters for the current delay. */
current_delay =
((s64) ledbat_current_delay(ledbat, &ledbat_min_circ_buff));
base_delay = ((s64) ledbat_base_delay(ledbat));
queue_delay = current_delay - base_delay;
offset = ((s64) target) - (queue_delay);
offset *= gain_num;
do_div(offset, gain_den);
/* Do not ramp more than TCP. */
if (offset > target)
offset = target;
#if DEBUG_DELAY
printk(KERN_DEBUG
"time %u, queue_delay %lld, offset %lld cwnd_cnt %u, "
"cwnd %u, delay %lld, min %lld\n",
tcp_time_stamp, queue_delay, offset, ledbat->snd_cwnd_cnt,
tp->snd_cwnd, current_delay, base_delay);
#endif
/* calculate the new cwnd_cnt */
cwnd = ledbat->snd_cwnd_cnt + offset;
if (cwnd >= 0) {
/* if we have a positive number update the cwnd_count */
ledbat->snd_cwnd_cnt = cwnd;
if (ledbat->snd_cwnd_cnt >= max_cwnd) {
/* increase the cwnd */
if (tp->snd_cwnd < tp->snd_cwnd_clamp)
tp->snd_cwnd++;
ledbat->snd_cwnd_cnt = 0;
}
} else {
/* we need to decrease the cwnd but we do not want to set it to 0! */
if (tp->snd_cwnd > 1) {
tp->snd_cwnd--;
/* set the cwnd_cnt to the max value - target */
ledbat->snd_cwnd_cnt = (tp->snd_cwnd - 1) * target;
} else {
tp->snd_cwnd_cnt = 0;
}
}
}
/**
* tcp_ledbat_remote_hz_estimator
*
* Estimate remote HZ.
* We keep on updating the estimated value, where original TCP-LP
* implementation only guesses it once and uses it forever.
*/
static u32 tcp_ledbat_remote_hz_estimator(struct sock *sk)
{
struct tcp_sock *tp = tcp_sk(sk);
struct ledbat *ledbat = inet_csk_ca(sk);
s64 rhz = ledbat->remote_hz << 6; /* remote HZ << 6 */
s64 m = 0;
if (ledbat->last_rollover == 0)
ledbat->last_rollover = tcp_time_stamp;
/* not yet record reference time
* go away!! record it before come back!! */
if (ledbat->remote_ref_time == 0 || ledbat->local_ref_time == 0)
goto out;
/* we can't calc remote HZ with no difference!! */
if (tp->rx_opt.rcv_tsval == ledbat->remote_ref_time
|| tp->rx_opt.rcv_tsecr == ledbat->local_ref_time)
goto out;
m = HZ * (tp->rx_opt.rcv_tsval -
ledbat->remote_ref_time) / (tp->rx_opt.rcv_tsecr -
ledbat->local_ref_time);
if (m < 0)
m = -m;
if (rhz > 0) {
m -= rhz >> 6; /* m is now wrong in remote HZ est */
rhz += m; /* 63/64 old + 1/64 new */
} else
rhz = m << 6;
out:
/* record time for successful remote HZ calc */
if ((rhz >> 6) > 0)
ledbat->flag |= LEDBAT_VALID_RHZ;
else
ledbat->flag &= ~LEDBAT_VALID_RHZ;
/* record reference time stamp */
ledbat->remote_ref_time = tp->rx_opt.rcv_tsval;
ledbat->local_ref_time = tp->rx_opt.rcv_tsecr;
return rhz >> 6;
}
/**
* tcp_ledbat_owd_calculator
*
* I stole this code from tcp_lp.c. Please also see comment for
* tcp_lp_owd_calculator.
*/
static u32 tcp_ledbat_owd_calculator(struct sock *sk)
{
struct tcp_sock *tp = tcp_sk(sk);
struct ledbat *ledbat = inet_csk_ca(sk);
s64 owd = 0;
ledbat->remote_hz = tcp_ledbat_remote_hz_estimator(sk);
if (ledbat->flag & LEDBAT_VALID_RHZ) {
owd = tp->rx_opt.rcv_tsval * (LP_RESOL / ledbat->remote_hz) -
tp->rx_opt.rcv_tsecr * (LP_RESOL / HZ);
if (owd < 0)
owd = -owd;
}
/* Safe net. */
if (owd > 0)
ledbat->flag |= LEDBAT_VALID_OWD;
else
ledbat->flag &= ~LEDBAT_VALID_OWD;
#if DEBUG_OWD_HZ
printk(KERN_DEBUG "my_hz %u, hz %u owd %u\n", HZ,
(u32) ledbat->remote_hz, (u32) owd);
#endif
return owd;
}
static void ledbat_add_delay(struct owd_circ_buf *cb, u32 owd)
{
u8 i;
if (cb->next == cb->first) {
/*buffer is empty */
cb->buffer[cb->next] = owd;
cb->min = cb->next;
cb->next++;
return;
}
/*set the new delay */
cb->buffer[cb->next] = owd;
/* update the min if it is the case */
if (owd < cb->buffer[cb->min])
cb->min = cb->next;
/* increment the next pointer */
cb->next = (cb->next + 1) % cb->len;
if (cb->next == cb->first) {
/* Discard the first element */
if (cb->min == cb->first) {
/* Discard the min, search a new one */
cb->min = i = (cb->first + 1) % cb->len;
while (i != cb->next) {
if (cb->buffer[i] < cb->buffer[cb->min])
cb->min = i;
i = (i + 1) % cb->len;
}
}
/* move the first */
cb->first = (cb->first + 1) % cb->len;
}
}
static void ledbat_update_current_delay(struct ledbat *ledbat, u32 owd)
{
ledbat_add_delay(&(ledbat->noise_filter), owd);
#if DEBUG_NOISE_FILTER
printk(KERN_DEBUG " added delay to noisefilter %u\n", owd);
print_delay(&(ledbat->noise_filter), "noise_filter");
#endif
}
static void ledbat_update_base_delay(struct ledbat *ledbat, u32 owd)
{
u32 last;
struct owd_circ_buf *cb = &(ledbat->base_history);
if (ledbat->base_history.next == ledbat->base_history.first) {
/* empty circular buffer */
ledbat_add_delay(cb, owd);
return;
}
if (tcp_time_stamp - ledbat->last_rollover > 60 * HZ) {
/* we have finished a minute */
#if DEBUG_BASE_HISTO
printk(KERN_DEBUG " time %u, new rollover \n", tcp_time_stamp);
#endif
ledbat->last_rollover = tcp_time_stamp;
ledbat_add_delay(cb, owd);
} else {
/* update the last value and the min if it is the case */
last = (cb->next + cb->len - 1) % cb->len;
if (owd < cb->buffer[last]) {
cb->buffer[last] = owd;
if (owd < cb->buffer[cb->min])
cb->min = last;
}
}
#if DEBUG_BASE_HISTO
printk(KERN_DEBUG " added delay to base_history %s", "\n");
print_delay(&(ledbat->base_history), "base_history");
#endif
}
/**
* tcp_ledbat_rtt_sample
*
* - calculate the owd
* - add the delay to noise filter
* - if new minute add the delay to base delay or update last delay
*/
static void tcp_ledbat_rtt_sample(struct sock *sk, u32 rtt)
{
struct ledbat *ledbat = inet_csk_ca(sk);
s64 mowd = tcp_ledbat_owd_calculator(sk);
/* sorry that we don't have valid data */
if (!(ledbat->flag & LEDBAT_VALID_RHZ)
|| !(ledbat->flag & LEDBAT_VALID_OWD)) {
return;
}
ledbat_update_current_delay(ledbat, (u32) mowd);
ledbat_update_base_delay(ledbat, (u32) mowd);
}
/**
* tcp_ledbat_pkts_acked
*/
static void tcp_ledbat_pkts_acked(struct sock *sk,
const struct ack_sample *sample)
{
struct ledbat *ledbat = inet_csk_ca(sk);
struct tcp_sock *tp = tcp_sk(sk);
if (sample->rtt_us > 0)
tcp_ledbat_rtt_sample(sk, sample->rtt_us);
if (ledbat->last_ack == 0)
ledbat->last_ack = tcp_time_stamp;
else if (after(tcp_time_stamp, ledbat->last_ack + usecs_to_jiffies(tp->srtt_us >> 3))) {
/* we haven't received an acknowledgement for more than a rtt.
Set the congestion window to 1. */
tp->snd_cwnd = 1;
}
ledbat->last_ack = tcp_time_stamp;
}
/**
* tcp_ledbat_undo_cwnd
* required starting kernel v4.10, see
* kernel commmit e97991832a4ea4a5f47d65f068a4c966a2eb5730
*/
static u32 tcp_ledbat_undo_cwnd(struct sock *sk)
{
/* this is the default v4.9 behavior */
const struct tcp_sock *tp = tcp_sk(sk);
return max(tp->snd_cwnd, tp->snd_ssthresh << 1);
}
static struct tcp_congestion_ops tcp_ledbat = {
.init = tcp_ledbat_init,
.ssthresh = tcp_ledbat_ssthresh,
.cong_avoid = tcp_ledbat_cong_avoid,
.pkts_acked = tcp_ledbat_pkts_acked,
.undo_cwnd = tcp_ledbat_undo_cwnd,
.release = tcp_ledbat_release,
.owner = THIS_MODULE,
.name = "ledbat"
};
static int __init tcp_ledbat_register(void)
{
BUILD_BUG_ON(sizeof(struct ledbat) > ICSK_CA_PRIV_SIZE);
return tcp_register_congestion_control(&tcp_ledbat);
}
static void __exit tcp_ledbat_unregister(void)
{
tcp_unregister_congestion_control(&tcp_ledbat);
}
module_init(tcp_ledbat_register);
module_exit(tcp_ledbat_unregister);
MODULE_AUTHOR("Silvio Valenti");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("TCP Ledbat (Low Extra Delay Background Transport)");