-
Notifications
You must be signed in to change notification settings - Fork 6
/
tcp_inigo.c
581 lines (485 loc) · 16 KB
/
tcp_inigo.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/* TCP Inigo congestion control.
*
* This is an implementation of TCP Inigo, which takes the measure of
* the extent of congestion introduced in DCTCP and applies it to
* networks outside the data center.
*
* https://www.soe.ucsc.edu/research/technical-reports/UCSC-SOE-14-14
*
* The motivation behind the RTT fairness functionality comes from
* the 2nd DCTCP paper listed below.
*
* Authors:
*
* Andrew Shewmaker <[email protected]>
*
* Forked from DataCenter TCP (DCTCP) congestion control.
*
* http://simula.stanford.edu/~alizade/Site/DCTCP.html
*
* This is an implementation of DCTCP, an enhancement to the
* TCP congestion control algorithm designed for data centers. DCTCP
* leverages Explicit Congestion Notification (ECN) in the network to
* provide multi-bit feedback to the end hosts. DCTCP's goal is to meet
* the following three data center transport requirements:
*
* - High burst tolerance (incast due to partition/aggregate)
* - Low latency (short flows, queries)
* - High throughput (continuous data updates, large file transfers)
* with commodity shallow buffered switches
*
* The algorithm is described in detail in the following two papers:
*
* 1) Mohammad Alizadeh, Albert Greenberg, David A. Maltz, Jitendra Padhye,
* Parveen Patel, Balaji Prabhakar, Sudipta Sengupta, and Murari Sridharan:
* "Data Center TCP (DCTCP)", Data Center Networks session
* Proc. ACM SIGCOMM, New Delhi, 2010.
* http://simula.stanford.edu/~alizade/Site/DCTCP_files/inigo-final.pdf
*
* 2) Mohammad Alizadeh, Adel Javanmard, and Balaji Prabhakar:
* "Analysis of DCTCP: Stability, Convergence, and Fairness"
* Proc. ACM SIGMETRICS, San Jose, 2011.
* http://simula.stanford.edu/~alizade/Site/DCTCP_files/inigo_analysis-full.pdf
*
* Initial prototype from Abdul Kabbani, Masato Yasuda and Mohammad Alizadeh.
*
* Authors:
*
* Daniel Borkmann <[email protected]>
* Florian Westphal <[email protected]>
* Glenn Judd <[email protected]>
*
* RTT Fallback:
*
* Andrew Shewmaker <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*/
#include <linux/module.h>
#include <linux/mm.h>
#include <net/tcp.h>
#include <linux/inet_diag.h>
#define DCTCP_MAX_ALPHA 1024U
#define INIGO_MIN_FAIRNESS 3U // alpha sensitivity of 684 / 1024
#define INIGO_MAX_FAIRNESS 512U // alpha sensitivity of 4 / 1024
#define INIGO_MAX_MARK 1024U
struct inigo {
u32 acked_bytes_ecn;
u32 acked_bytes_total;
u32 prior_snd_una;
u32 prior_rcv_nxt;
u32 inigo_alpha;
u32 next_seq;
u32 delayed_ack_reserved;
u32 rtt_min;
u32 rtts_late;
u32 rtts_observed;
u8 ce_state;
};
static unsigned int inigo_shift_g __read_mostly = 4; /* g = 1/2^4 */
module_param(inigo_shift_g, uint, 0644);
MODULE_PARM_DESC(inigo_shift_g, "parameter g for updating inigo_alpha");
static unsigned int inigo_alpha_on_init __read_mostly = DCTCP_MAX_ALPHA;
module_param(inigo_alpha_on_init, uint, 0644);
MODULE_PARM_DESC(inigo_alpha_on_init, "parameter for initial alpha value");
static unsigned int inigo_clamp_alpha_on_loss __read_mostly;
module_param(inigo_clamp_alpha_on_loss, uint, 0644);
MODULE_PARM_DESC(inigo_clamp_alpha_on_loss,
"parameter for clamping alpha on loss");
static unsigned int markthresh __read_mostly = 174;
module_param(markthresh, uint, 0644);
MODULE_PARM_DESC(markthresh, "rtts > rtt_min + rtt_min * markthresh / 1024"
" are considered marks of congestion, defaults to 174 out of 1024");
static unsigned int slowstart_rtt_observations_needed __read_mostly = 8;
module_param(slowstart_rtt_observations_needed, uint, 0644);
MODULE_PARM_DESC(slowstart_rtt_observations_needed, "minimum number of RTT observations needed"
" to exit slowstart, defaults to 8");
static unsigned int rtt_fairness __read_mostly = 10;
module_param(rtt_fairness, uint, 0644);
MODULE_PARM_DESC(rtt_fairness, "if non-zero, react to congestion every x acks during cong avoid,"
" 0 indicates once per window, otherwise 3 < x < 512, defaults to 10");
static unsigned int inigo_force_ecn __read_mostly = 0;
module_param(inigo_force_ecn, uint, 0644);
MODULE_PARM_DESC(inigo_force_ecn, "force use of ecn (needed for Mininet testing of fallback)");
static struct tcp_congestion_ops inigo;
static struct tcp_congestion_ops inigo_rtt;
static void inigo_reset(const struct tcp_sock *tp, struct inigo *ca)
{
ca->next_seq = tp->snd_nxt;
ca->acked_bytes_ecn = 0;
ca->acked_bytes_total = 0;
}
static void inigo_init(struct sock *sk)
{
struct tcp_sock *tp = tcp_sk(sk);
struct inigo *ca = inet_csk_ca(sk);
const struct inet_sock *inet = inet_sk(sk);
if (rtt_fairness != 0)
rtt_fairness = clamp(rtt_fairness, INIGO_MIN_FAIRNESS, INIGO_MAX_FAIRNESS);
ca->inigo_alpha = 0;
ca->rtt_min = USEC_PER_SEC;
ca->rtts_late = 0;
ca->rtts_observed = 0;
if (inigo_force_ecn || (tp->ecn_flags & TCP_ECN_OK) ||
(sk->sk_state == TCP_LISTEN ||
sk->sk_state == TCP_CLOSE)) {
bool use_ecn = sock_net(sk)->ipv4.sysctl_tcp_ecn == 1 || tcp_ca_needs_ecn(sk);
const struct dst_entry *dst = __sk_dst_get(sk);
pr_info("inigo: ecn enabled %pI4 force=%u ecn_ok=%u state=%u use_ecn=%u dst_feature=%u\n",
&inet->inet_saddr, inigo_force_ecn, tp->ecn_flags & TCP_ECN_OK, sk->sk_state, use_ecn, dst && dst_feature(dst, RTAX_FEATURE_ECN));
inigo.flags |= TCP_CONG_NEEDS_ECN;
//TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_ECE | TCPHDR_CWR;
tp->ecn_flags = TCP_ECN_OK;
INET_ECN_xmit(sk);
ca->inigo_alpha = min(inigo_alpha_on_init, DCTCP_MAX_ALPHA);
ca->prior_snd_una = tp->snd_una;
ca->prior_rcv_nxt = tp->rcv_nxt;
ca->delayed_ack_reserved = 0;
ca->ce_state = 0;
inigo_reset(tp, ca);
return;
}
/* No ECN support? Fall back to RTT. Also need to clear
* ECT from sk since it is set during 3WHS for DCTCP.
*/
pr_info("inigo: ecn disabled %pI4 force=%u ecn_ok=%u state=%u\n", &inet->inet_saddr, inigo_force_ecn, tp->ecn_flags & TCP_ECN_OK, sk->sk_state);
inet_csk(sk)->icsk_ca_ops = &inigo_rtt;
INET_ECN_dontxmit(sk);
}
static u32 inigo_ssthresh(struct sock *sk)
{
const struct inigo *ca = inet_csk_ca(sk);
struct tcp_sock *tp = tcp_sk(sk);
return max(tp->snd_cwnd - ((tp->snd_cwnd * ca->inigo_alpha) >> 11U), 2U);
}
static u32 inigo_rtt_ssthresh(struct sock *sk)
{
const struct inigo *ca = inet_csk_ca(sk);
struct tcp_sock *tp = tcp_sk(sk);
u32 nsubwnd = 1;
if (rtt_fairness) {
nsubwnd = tp->snd_cwnd;
if (do_div(nsubwnd, rtt_fairness))
nsubwnd++;
}
return max(tp->snd_cwnd - ((tp->snd_cwnd * ca->inigo_alpha) >> 11U) / nsubwnd, 2U);
}
/* Minimal DCTP CE state machine:
*
* S: 0 <- last pkt was non-CE
* 1 <- last pkt was CE
*/
static void inigo_ce_state_0_to_1(struct sock *sk)
{
struct inigo *ca = inet_csk_ca(sk);
struct tcp_sock *tp = tcp_sk(sk);
/* State has changed from CE=0 to CE=1 and delayed
* ACK has not sent yet.
*/
if (!ca->ce_state && ca->delayed_ack_reserved) {
u32 tmp_rcv_nxt;
/* Save current rcv_nxt. */
tmp_rcv_nxt = tp->rcv_nxt;
/* Generate previous ack with CE=0. */
tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
tp->rcv_nxt = ca->prior_rcv_nxt;
tcp_send_ack(sk);
/* Recover current rcv_nxt. */
tp->rcv_nxt = tmp_rcv_nxt;
}
ca->prior_rcv_nxt = tp->rcv_nxt;
ca->ce_state = 1;
tp->ecn_flags |= TCP_ECN_DEMAND_CWR;
}
static void inigo_ce_state_1_to_0(struct sock *sk)
{
struct inigo *ca = inet_csk_ca(sk);
struct tcp_sock *tp = tcp_sk(sk);
/* State has changed from CE=1 to CE=0 and delayed
* ACK has not sent yet.
*/
if (ca->ce_state && ca->delayed_ack_reserved) {
u32 tmp_rcv_nxt;
/* Save current rcv_nxt. */
tmp_rcv_nxt = tp->rcv_nxt;
/* Generate previous ack with CE=1. */
tp->ecn_flags |= TCP_ECN_DEMAND_CWR;
tp->rcv_nxt = ca->prior_rcv_nxt;
tcp_send_ack(sk);
/* Recover current rcv_nxt. */
tp->rcv_nxt = tmp_rcv_nxt;
}
ca->prior_rcv_nxt = tp->rcv_nxt;
ca->ce_state = 0;
tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
}
static void inigo_update_rtt_alpha(struct inigo *ca)
{
u32 alpha = ca->inigo_alpha;
u32 marks = ca->rtts_late;
u32 total = ca->rtts_observed;
/* alpha = (1 - g) * alpha + g * F */
alpha -= min_not_zero(alpha, alpha >> inigo_shift_g);
if (marks) {
/* If shift_g == 1, a 32bit value would overflow
* after 8 M.
*/
marks <<= (10 - inigo_shift_g);
do_div(marks, max(1U, total));
alpha = min(alpha + (u32)marks, DCTCP_MAX_ALPHA);
}
ca->inigo_alpha = alpha;
}
/* The cwnd reduction in CWR and Recovery use the PRR algorithm
* https://datatracker.ietf.org/doc/draft-ietf-tcpm-proportional-rate-reduction/
* It computes the number of packets to send (sndcnt) based on packets newly
* delivered:
* 1) If the packets in flight is larger than ssthresh, PRR spreads the
* cwnd reductions across a full RTT.
* 2) If packets in flight is lower than ssthresh (such as due to excess
* losses and/or application stalls), do not perform any further cwnd
* reductions, but instead slow start up to ssthresh.
*/
static void inigo_init_cwnd_reduction(struct sock *sk)
{
struct inigo *ca = inet_csk_ca(sk);
struct tcp_sock *tp = tcp_sk(sk);
tp->high_seq = tp->snd_nxt;
tp->tlp_high_seq = 0;
// tp->snd_cwnd_cnt = 0; commented out because of rtt-fairness support
tp->prior_cwnd = tp->snd_cwnd;
tp->prr_delivered = 0;
tp->prr_out = 0;
tp->snd_ssthresh = inigo_rtt_ssthresh(sk);
ca->rtts_late = 0;
ca->rtts_observed = 0;
}
/* Enter CWR state. Disable cwnd undo since congestion is proven with ECN or Delay */
void inigo_enter_cwr(struct sock *sk)
{
struct tcp_sock *tp = tcp_sk(sk);
tp->prior_ssthresh = 0;
if (inet_csk(sk)->icsk_ca_state < TCP_CA_CWR) {
tp->undo_marker = 0;
inigo_init_cwnd_reduction(sk);
tcp_set_ca_state(sk, TCP_CA_CWR);
}
}
static void inigo_update_alpha(struct sock *sk, u32 flags)
{
struct tcp_sock *tp = tcp_sk(sk);
struct inigo *ca = inet_csk_ca(sk);
u32 acked_bytes = tp->snd_una - ca->prior_snd_una;
/* If ack did not advance snd_una, count dupack as MSS size.
* If ack did update window, do not count it at all.
*/
if (acked_bytes == 0 && !(flags & CA_ACK_WIN_UPDATE))
acked_bytes = inet_csk(sk)->icsk_ack.rcv_mss;
if (acked_bytes) {
if (flags & CA_ACK_ECE) {
if (ca->acked_bytes_ecn == 0 && tcp_in_slow_start(tp)) {
tp->snd_ssthresh = tp->snd_cwnd;
}
ca->acked_bytes_ecn += acked_bytes;
}
ca->acked_bytes_total += acked_bytes;
ca->prior_snd_una = tp->snd_una;
}
/* Expired RTT */
if (!before(tp->snd_una, ca->next_seq)) {
u64 bytes_ecn = ca->acked_bytes_ecn;
u32 alpha = ca->inigo_alpha;
/* alpha = (1 - g) * alpha + g * F */
alpha -= min_not_zero(alpha, alpha >> inigo_shift_g);
if (bytes_ecn) {
/* If inigo_shift_g == 1, a 32bit value would overflow
* after 8 Mbytes.
*/
bytes_ecn <<= (10 - inigo_shift_g);
do_div(bytes_ecn, max(1U, ca->acked_bytes_total));
alpha = min(alpha + (u32)bytes_ecn, DCTCP_MAX_ALPHA);
}
/* inigo_alpha can be read from inigo_get_info() without
* synchro, so we ask compiler to not use inigo_alpha
* as a temporary variable in prior operations.
*/
WRITE_ONCE(ca->inigo_alpha, alpha);
inigo_reset(tp, ca);
/* Fall back to RTT-based congestion control if alpha stays low
* even though RTTs are increasing during the window.
*/
if (ca->acked_bytes_ecn == 0 && ca->rtts_late > 1 && ca->rtts_observed >= slowstart_rtt_observations_needed) {
pr_info("inigo: ecn unconfigured, falling back to RTT-based congestion control\n");
inet_csk(sk)->icsk_ca_ops = &inigo_rtt;
INET_ECN_dontxmit(sk);
/*
inigo_update_rtt_alpha(ca);
if (ca->inigo_alpha) {
inigo_enter_cwr(sk);
return;
}
*/
}
ca->rtts_late = 0;
}
}
static void inigo_state(struct sock *sk, u8 new_state)
{
if (inigo_clamp_alpha_on_loss && new_state == TCP_CA_Loss) {
struct inigo *ca = inet_csk_ca(sk);
/* If this extension is enabled, we clamp inigo_alpha to
* max on packet loss; the motivation is that inigo_alpha
* is an indicator to the extend of congestion and packet
* loss is an indicator of extreme congestion; setting
* this in practice turned out to be beneficial, and
* effectively assumes total congestion which reduces the
* window by half.
*/
ca->inigo_alpha = DCTCP_MAX_ALPHA;
}
}
static void inigo_update_ack_reserved(struct sock *sk, enum tcp_ca_event ev)
{
struct inigo *ca = inet_csk_ca(sk);
switch (ev) {
case CA_EVENT_DELAYED_ACK:
if (!ca->delayed_ack_reserved)
ca->delayed_ack_reserved = 1;
break;
case CA_EVENT_NON_DELAYED_ACK:
if (ca->delayed_ack_reserved)
ca->delayed_ack_reserved = 0;
break;
default:
/* Don't care for the rest. */
break;
}
}
static void inigo_cwnd_event(struct sock *sk, enum tcp_ca_event ev)
{
switch (ev) {
case CA_EVENT_ECN_IS_CE:
inigo_ce_state_0_to_1(sk);
break;
case CA_EVENT_ECN_NO_CE:
inigo_ce_state_1_to_0(sk);
break;
case CA_EVENT_DELAYED_ACK:
case CA_EVENT_NON_DELAYED_ACK:
inigo_update_ack_reserved(sk, ev);
break;
default:
/* Don't care for the rest. */
break;
}
}
/* This is the same as newer tcp_slow_start(). It is only here for while inigo
* is being built out of tree against older kernels that don't do it this way.
*/
u32 inigo_slow_start(struct tcp_sock *tp, u32 acked)
{
u32 cwnd = tp->snd_cwnd + acked;
if (cwnd > tp->snd_ssthresh)
cwnd = tp->snd_ssthresh + 1;
acked -= cwnd - tp->snd_cwnd;
tp->snd_cwnd = min(cwnd, tp->snd_cwnd_clamp);
return acked;
}
void inigo_cong_avoid_ai(struct sock *sk, u32 w, u32 acked)
{
struct inigo *ca = inet_csk_ca(sk);
struct tcp_sock *tp = tcp_sk(sk);
u32 interval = tp->snd_cwnd;
if (tp->snd_cwnd_cnt >= w) {
if (tp->snd_cwnd < tp->snd_cwnd_clamp) {
tp->snd_cwnd++;
if (rtt_fairness)
tp->snd_cwnd++;
}
tp->snd_cwnd_cnt = 0;
}
if (rtt_fairness)
interval = min(interval, rtt_fairness);
if (tp->snd_cwnd_cnt >= interval) {
if (tp->snd_cwnd_cnt % interval == 0 || tp->snd_cwnd_cnt >= w) {
inigo_update_rtt_alpha(ca);
if (ca->inigo_alpha)
inigo_enter_cwr(sk);
}
}
if (tp->snd_cwnd_cnt < w) {
tp->snd_cwnd_cnt += acked;
}
}
void inigo_cong_avoid(struct sock *sk, u32 ack, u32 acked)
{
struct inigo *ca = inet_csk_ca(sk);
struct tcp_sock *tp = tcp_sk(sk);
if (!tcp_is_cwnd_limited(sk)) {
return;
}
if (tp->snd_cwnd <= tp->snd_ssthresh) {
if (ca->rtts_observed >= slowstart_rtt_observations_needed) {
inigo_update_rtt_alpha(ca);
if (ca->inigo_alpha) {
inigo_enter_cwr(sk);
return;
}
}
/* In "safe" area, increase. */
acked = inigo_slow_start(tp, acked);
if (!acked)
return;
}
/* In dangerous area, increase slowly. */
inigo_cong_avoid_ai(sk, tp->snd_cwnd, acked);
}
static void inigo_pkts_acked(struct sock *sk, u32 num_acked, s32 rtt)
{
struct inigo *ca = inet_csk_ca(sk);
/* Some calls are for duplicates without timetamps */
if (rtt <= 0)
return;
ca->rtts_observed++;
ca->rtt_min = min((u32) rtt, ca->rtt_min);
/* Mimic DCTCP's ECN marking threshhold of approximately 0.17*BDP */
if ((u32) rtt > (ca->rtt_min + (ca->rtt_min * markthresh / INIGO_MAX_MARK)))
ca->rtts_late++;
}
static struct tcp_congestion_ops inigo __read_mostly = {
.init = inigo_init,
.in_ack_event = inigo_update_alpha,
.cwnd_event = inigo_cwnd_event,
.ssthresh = inigo_ssthresh,
.cong_avoid = tcp_reno_cong_avoid,
.pkts_acked = inigo_pkts_acked,
.set_state = inigo_state,
.owner = THIS_MODULE,
.name = "inigo",
};
static struct tcp_congestion_ops inigo_rtt __read_mostly = {
.ssthresh = inigo_rtt_ssthresh,
.cong_avoid = inigo_cong_avoid,
.pkts_acked = inigo_pkts_acked,
.owner = THIS_MODULE,
.name = "inigo-rtt",
};
static int __init inigo_register(void)
{
BUILD_BUG_ON(sizeof(struct inigo) > ICSK_CA_PRIV_SIZE);
return tcp_register_congestion_control(&inigo);
}
static void __exit inigo_unregister(void)
{
tcp_unregister_congestion_control(&inigo);
}
module_init(inigo_register);
module_exit(inigo_unregister);
MODULE_AUTHOR("Daniel Borkmann <[email protected]>");
MODULE_AUTHOR("Florian Westphal <[email protected]>");
MODULE_AUTHOR("Glenn Judd <[email protected]>");
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("TCP Inigo");