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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
|
This software was developed by Lawrence Stewart while studying at the Centre
for Advanced Internet Architectures, Swinburne University of Technology, made
possible in part by a grant from the Cisco University Research Program Fund
at Community Foundation Silicon Valley.
Portions of this software were developed at the Centre for Advanced
Internet Architectures, Swinburne University of Technology, Melbourne,
Australia by David Hayes under sponsorship from the FreeBSD Foundation.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
The congestion control framework, its header, and the CUBIC and NewReno
congestion control modules came from FreeBSD, and are therefore licensed
under the 2-Clause BSD License.
/*
* Copyright (c) 2007-2008
* Swinburne University of Technology, Melbourne, Australia.
* Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
* Copyright (c) 2010 The FreeBSD Foundation
* All rights reserved.
* Copyright (c) 2017 by Delphix. All rights reserved.
*
* This software was developed at the Centre for Advanced Internet
* Architectures, Swinburne University of Technology, by Lawrence Stewart and
* James Healy, made possible in part by a grant from the Cisco University
* Research Program Fund at Community Foundation Silicon Valley.
*
* Portions of this software were developed at the Centre for Advanced
* Internet Architectures, Swinburne University of Technology, Melbourne,
* Australia by David Hayes under sponsorship from the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* This software was first released in 2007 by James Healy and Lawrence Stewart
* whilst working on the NewTCP research project at Swinburne University of
* Technology's Centre for Advanced Internet Architectures, Melbourne,
* Australia, which was made possible in part by a grant from the Cisco
* University Research Program Fund at Community Foundation Silicon Valley.
* More details are available at:
* http://caia.swin.edu.au/urp/newtcp/
*/
#include <sys/param.h>
#include <sys/errno.h>
#include <sys/systm.h>
#include <sys/queue.h>
#include <inet/cc.h>
#include <inet/tcp.h>
#include <sys/sdt.h>
#define CC_KMODDIR "cc"
/*
* List of available cc algorithms on the current system. Access is
* synchronized using cc_list_lock.
*/
static STAILQ_HEAD(cc_head, cc_algo) cc_list = STAILQ_HEAD_INITIALIZER(cc_list);
static kmutex_t cc_list_lock;
static struct modlmisc cc_modlmisc = {
&mod_miscops,
"Pluggable Congestion Control Framework"
};
static struct modlinkage cc_modlinkage = {
MODREV_1,
&cc_modlmisc,
NULL
};
/*
* Initialise CC subsystem on system boot.
*/
int
_init(void)
{
STAILQ_INIT(&cc_list);
return (mod_install(&cc_modlinkage));
}
int
_fini(void)
{
return (EBUSY);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&cc_modlinkage, modinfop));
}
int
cc_walk_algos(cc_walk_func_t *func, void *cd)
{
struct cc_algo *algo;
int ret = 0;
mutex_enter(&cc_list_lock);
STAILQ_FOREACH(algo, &cc_list, entries) {
if ((ret = func(cd, algo)) != 0) {
break;
}
}
mutex_exit(&cc_list_lock);
return (ret);
}
/*
* Search for an algorithm of a given name, and return the corresponding set of
* operations. If there is no algorithm with the given name present, then this
* function returns NULL.
*
* Since this function is passed names from userland, it needs to be paranoid
* about the string, in case it's missing a terminating NUL character.
*/
struct cc_algo *
cc_load_algo(const char *name)
{
struct cc_algo *algo;
boolean_t found = B_FALSE;
if (strnlen(name, CC_ALGO_NAME_MAX) >= CC_ALGO_NAME_MAX) {
return (NULL);
}
mutex_enter(&cc_list_lock);
STAILQ_FOREACH(algo, &cc_list, entries) {
if (strncmp(algo->name, name, CC_ALGO_NAME_MAX) == 0) {
found = B_TRUE;
break;
}
}
mutex_exit(&cc_list_lock);
return (found ? algo : NULL);
}
/*
* Returns non-zero on success, 0 on failure.
*/
int
cc_deregister_algo(struct cc_algo *remove_cc)
{
struct cc_algo *funcs, *tmpfuncs;
int err = ENOENT;
mutex_enter(&cc_list_lock);
STAILQ_FOREACH_SAFE(funcs, &cc_list, entries, tmpfuncs) {
if (funcs == remove_cc) {
STAILQ_REMOVE(&cc_list, funcs, cc_algo, entries);
err = 0;
break;
}
}
mutex_exit(&cc_list_lock);
return (err);
}
/*
* Returns 0 on success, non-zero on failure.
*/
int
cc_register_algo(struct cc_algo *add_cc)
{
struct cc_algo *funcs;
size_t nlen;
int err = 0;
nlen = strnlen(add_cc->name, CC_ALGO_NAME_MAX);
if (nlen == 0 || nlen >= CC_ALGO_NAME_MAX) {
return (EINVAL);
}
/*
* Iterate over list of registered CC algorithms and make sure
* we're not trying to add a duplicate.
*/
mutex_enter(&cc_list_lock);
STAILQ_FOREACH(funcs, &cc_list, entries) {
if (strncmp(funcs->name, add_cc->name, CC_ALGO_NAME_MAX) == 0)
err = EEXIST;
}
if (err == 0)
STAILQ_INSERT_TAIL(&cc_list, add_cc, entries);
mutex_exit(&cc_list_lock);
return (err);
}
/*
* Copyright (c) 2008-2010 Lawrence Stewart <lstewart@freebsd.org>
* Copyright (c) 2010 The FreeBSD Foundation
* All rights reserved.
* Copyright (c) 2017 by Delphix. All rights reserved.
* Copyright 2019 Joyent, Inc.
* Copyright 2020 RackTop Systems, Inc.
*
* This software was developed by Lawrence Stewart while studying at the Centre
* for Advanced Internet Architectures, Swinburne University of Technology, made
* possible in part by a grant from the Cisco University Research Program Fund
* at Community Foundation Silicon Valley.
*
* Portions of this software were developed at the Centre for Advanced
* Internet Architectures, Swinburne University of Technology, Melbourne,
* Australia by David Hayes under sponsorship from the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* An implementation of the CUBIC congestion control algorithm for FreeBSD,
* based on the Internet Draft "draft-rhee-tcpm-cubic-02" by Rhee, Xu and Ha.
* Originally released as part of the NewTCP research project at Swinburne
* University of Technology's Centre for Advanced Internet Architectures,
* Melbourne, Australia, which was made possible in part by a grant from the
* Cisco University Research Program Fund at Community Foundation Silicon
* Valley. More details are available at:
* http://caia.swin.edu.au/urp/newtcp/
*/
#include <sys/errno.h>
#include <sys/types.h>
#include <sys/kmem.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/modctl.h>
#include <sys/time.h>
#include <inet/tcp_impl.h>
#include <inet/cc.h>
#include <inet/cc/cc_cubic.h>
#include <inet/cc/cc_module.h>
static struct modlmisc cc_cubic_modlmisc = {
&mod_miscops,
"Cubic Congestion Control"
};
static struct modlinkage cc_cubic_modlinkage = {
MODREV_1,
&cc_cubic_modlmisc,
NULL
};
/*
* cubic uses the NewReno implementation of after_idle and uses NewReno's
* ack_received callback during slow start.
*/
static struct cc_algo *newreno_cc_algo;
static void cubic_ack_received(struct cc_var *ccv, uint16_t type);
static void cubic_cb_destroy(struct cc_var *ccv);
static int cubic_cb_init(struct cc_var *ccv);
static void cubic_cong_signal(struct cc_var *ccv, uint32_t type);
static void cubic_conn_init(struct cc_var *ccv);
static void cubic_post_recovery(struct cc_var *ccv);
static void cubic_record_rtt(struct cc_var *ccv);
static void cubic_ssthresh_update(struct cc_var *ccv);
static void cubic_after_idle(struct cc_var *ccv);
struct cubic {
/* Cubic K in fixed point form with CUBIC_SHIFT worth of precision. */
int64_t K;
/* Sum of RTT samples across an epoch in nanoseconds. */
hrtime_t sum_rtt_nsecs;
/* cwnd at the most recent congestion event. */
uint32_t max_cwnd;
/* cwnd at the previous congestion event. */
uint32_t prev_max_cwnd;
/* Number of congestion events. */
uint32_t num_cong_events;
/* Minimum observed rtt in nanoseconds. */
hrtime_t min_rtt_nsecs;
/* Mean observed rtt between congestion epochs. */
hrtime_t mean_rtt_nsecs;
/* ACKs since last congestion event. */
int epoch_ack_count;
/* Time of last congestion event in nanoseconds. */
hrtime_t t_last_cong;
};
struct cc_algo cubic_cc_algo = {
.name = "cubic",
.ack_received = cubic_ack_received,
.cb_destroy = cubic_cb_destroy,
.cb_init = cubic_cb_init,
.cong_signal = cubic_cong_signal,
.conn_init = cubic_conn_init,
.post_recovery = cubic_post_recovery,
.after_idle = cubic_after_idle,
};
int
_init(void)
{
int err;
if ((newreno_cc_algo = cc_load_algo("newreno")) == NULL)
return (EINVAL);
if ((err = cc_register_algo(&cubic_cc_algo)) == 0) {
if ((err = mod_install(&cc_cubic_modlinkage)) != 0)
(void) cc_deregister_algo(&cubic_cc_algo);
}
return (err);
}
int
_fini(void)
{
/* XXX Not unloadable for now */
return (EBUSY);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&cc_cubic_modlinkage, modinfop));
}
static void
cubic_ack_received(struct cc_var *ccv, uint16_t type)
{
struct cubic *cubic_data;
uint32_t w_tf, w_cubic_next;
hrtime_t nsecs_since_cong;
cubic_data = ccv->cc_data;
cubic_record_rtt(ccv);
/*
* Regular ACK and we're not in cong/fast recovery and we're cwnd
* limited and we're either not doing ABC or are slow starting or are
* doing ABC and we've sent a cwnd's worth of bytes.
*/
if (type == CC_ACK && !IN_RECOVERY(ccv->flags) &&
(ccv->flags & CCF_CWND_LIMITED) && (!CC_ABC(ccv) ||
CCV(ccv, tcp_cwnd) <= CCV(ccv, tcp_cwnd_ssthresh) ||
(CC_ABC(ccv) && (ccv->flags & CCF_ABC_SENTAWND)))) {
/* Use the logic in NewReno ack_received() for slow start. */
if (CCV(ccv, tcp_cwnd) <= CCV(ccv, tcp_cwnd_ssthresh) ||
cubic_data->min_rtt_nsecs == TCPTV_SRTTBASE)
newreno_cc_algo->ack_received(ccv, type);
else {
nsecs_since_cong = gethrtime() -
cubic_data->t_last_cong;
/*
* The mean RTT is used to best reflect the equations in
* the I-D. Using min_rtt in the tf_cwnd calculation
* causes w_tf to grow much faster than it should if the
* RTT is dominated by network buffering rather than
* propagation delay.
*/
w_tf = tf_cwnd(nsecs_since_cong,
cubic_data->mean_rtt_nsecs, cubic_data->max_cwnd,
CCV(ccv, tcp_mss));
w_cubic_next = cubic_cwnd(nsecs_since_cong +
cubic_data->mean_rtt_nsecs, cubic_data->max_cwnd,
CCV(ccv, tcp_mss), cubic_data->K);
ccv->flags &= ~CCF_ABC_SENTAWND;
if (w_cubic_next < w_tf) {
/*
* TCP-friendly region, follow tf
* cwnd growth.
*/
if (CCV(ccv, tcp_cwnd) < w_tf)
CCV(ccv, tcp_cwnd) = w_tf;
} else if (CCV(ccv, tcp_cwnd) < w_cubic_next) {
/*
* Concave or convex region, follow CUBIC
* cwnd growth.
*/
if (CC_ABC(ccv))
CCV(ccv, tcp_cwnd) = MIN(w_cubic_next,
INT_MAX);
else
CCV(ccv, tcp_cwnd) += MAX(1,
((MIN(w_cubic_next, INT_MAX) -
CCV(ccv, tcp_cwnd)) *
CCV(ccv, tcp_mss)) /
CCV(ccv, tcp_cwnd));
}
/*
* If we're not in slow start and we're probing for a
* new cwnd limit at the start of a connection
* (happens when hostcache has a relevant entry),
* keep updating our current estimate of the
* max_cwnd.
*/
if (cubic_data->num_cong_events == 0 &&
cubic_data->max_cwnd < CCV(ccv, tcp_cwnd)) {
cubic_data->max_cwnd = CCV(ccv, tcp_cwnd);
cubic_data->K = cubic_k(cubic_data->max_cwnd /
CCV(ccv, tcp_mss));
}
}
}
}
/*
* This is a Cubic specific implementation of after_idle.
* - Reset cwnd by calling New Reno implementation of after_idle.
* - Reset t_last_cong.
*/
static void
cubic_after_idle(struct cc_var *ccv)
{
struct cubic *cubic_data;
cubic_data = ccv->cc_data;
cubic_data->max_cwnd = max(cubic_data->max_cwnd, CCV(ccv, tcp_cwnd));
cubic_data->K = cubic_k(cubic_data->max_cwnd / CCV(ccv, tcp_mss));
newreno_cc_algo->after_idle(ccv);
cubic_data->t_last_cong = gethrtime();
}
static void
cubic_cb_destroy(struct cc_var *ccv)
{
if (ccv->cc_data != NULL)
kmem_free(ccv->cc_data, sizeof (struct cubic));
}
static int
cubic_cb_init(struct cc_var *ccv)
{
struct cubic *cubic_data;
cubic_data = kmem_zalloc(sizeof (struct cubic), KM_NOSLEEP);
if (cubic_data == NULL)
return (ENOMEM);
/* Init some key variables with sensible defaults. */
cubic_data->t_last_cong = gethrtime();
cubic_data->min_rtt_nsecs = TCPTV_SRTTBASE;
cubic_data->mean_rtt_nsecs = 1;
ccv->cc_data = cubic_data;
return (0);
}
/*
* Perform any necessary tasks before we enter congestion recovery.
*/
static void
cubic_cong_signal(struct cc_var *ccv, uint32_t type)
{
struct cubic *cubic_data;
uint32_t cwin;
uint32_t mss;
cubic_data = ccv->cc_data;
cwin = CCV(ccv, tcp_cwnd);
mss = CCV(ccv, tcp_mss);
switch (type) {
case CC_NDUPACK:
if (!IN_FASTRECOVERY(ccv->flags)) {
if (!IN_CONGRECOVERY(ccv->flags)) {
cubic_ssthresh_update(ccv);
cubic_data->num_cong_events++;
cubic_data->prev_max_cwnd =
cubic_data->max_cwnd;
cubic_data->max_cwnd = cwin;
CCV(ccv, tcp_cwnd) =
CCV(ccv, tcp_cwnd_ssthresh);
}
ENTER_RECOVERY(ccv->flags);
}
break;
case CC_ECN:
if (!IN_CONGRECOVERY(ccv->flags)) {
cubic_ssthresh_update(ccv);
cubic_data->num_cong_events++;
cubic_data->prev_max_cwnd = cubic_data->max_cwnd;
cubic_data->max_cwnd = cwin;
cubic_data->t_last_cong = gethrtime();
CCV(ccv, tcp_cwnd) = CCV(ccv, tcp_cwnd_ssthresh);
ENTER_CONGRECOVERY(ccv->flags);
}
break;
case CC_RTO:
/*
* Grab the current time and record it so we know when the
* most recent congestion event was.
*/
cubic_data->num_cong_events++;
cubic_data->t_last_cong = gethrtime();
cubic_ssthresh_update(ccv);
cubic_data->max_cwnd = cwin;
CCV(ccv, tcp_cwnd) = mss;
break;
}
}
static void
cubic_conn_init(struct cc_var *ccv)
{
struct cubic *cubic_data;
cubic_data = ccv->cc_data;
/*
* Ensure we have a sane initial value for max_cwnd recorded. Without
* this here bad things happen when entries from the TCP hostcache
* get used.
*/
cubic_data->max_cwnd = CCV(ccv, tcp_cwnd);
}
/*
* Perform any necessary tasks before we exit congestion recovery.
*/
static void
cubic_post_recovery(struct cc_var *ccv)
{
struct cubic *cubic_data;
uint32_t mss, pipe;
cubic_data = ccv->cc_data;
/* Fast convergence heuristic. */
if (cubic_data->max_cwnd < cubic_data->prev_max_cwnd) {
cubic_data->max_cwnd = (cubic_data->max_cwnd * CUBIC_FC_FACTOR)
>> CUBIC_SHIFT;
}
/*
* There is a risk that if the cwnd becomes less than mss, and
* we do not get enough acks to drive it back up beyond mss,
* we will stop transmitting data altogether.
*
* The Cubic RFC defines values in terms of units of mss. Therefore
* we must make sure we have at least 1 mss to make progress
* since the algorthm is written that way.
*/
mss = CCV(ccv, tcp_mss);
if (IN_FASTRECOVERY(ccv->flags)) {
/*
* If inflight data is less than ssthresh, set cwnd
* conservatively to avoid a burst of data, as suggested in
* the NewReno RFC. Otherwise, use the CUBIC method.
*/
pipe = CCV(ccv, tcp_snxt) - CCV(ccv, tcp_suna);
if (pipe < CCV(ccv, tcp_cwnd_ssthresh)) {
/*
* Ensure that cwnd does not collapse to 1 MSS under
* adverse conditions. Implements RFC6582
*/
CCV(ccv, tcp_cwnd) = MAX(pipe, mss) + mss;
} else {
/* Update cwnd based on beta and adjusted max_cwnd. */
CCV(ccv, tcp_cwnd) = max(mss, ((CUBIC_BETA *
cubic_data->max_cwnd) >> CUBIC_SHIFT));
}
} else {
CCV(ccv, tcp_cwnd) = max(mss, CCV(ccv, tcp_cwnd));
}
cubic_data->t_last_cong = gethrtime();
/* Calculate the average RTT between congestion epochs. */
if (cubic_data->epoch_ack_count > 0 &&
cubic_data->sum_rtt_nsecs >= cubic_data->epoch_ack_count) {
cubic_data->mean_rtt_nsecs =
(cubic_data->sum_rtt_nsecs / cubic_data->epoch_ack_count);
}
cubic_data->epoch_ack_count = 0;
cubic_data->sum_rtt_nsecs = 0;
cubic_data->K = cubic_k(cubic_data->max_cwnd / mss);
}
/*
* Record the min RTT and sum samples for the epoch average RTT calculation.
*/
static void
cubic_record_rtt(struct cc_var *ccv)
{
struct cubic *cubic_data;
int t_srtt_nsecs;
/* Ignore srtt until a min number of samples have been taken. */
if (CCV(ccv, tcp_rtt_update) >= CUBIC_MIN_RTT_SAMPLES) {
cubic_data = ccv->cc_data;
/* tcp_rtt_sa is 8 * smoothed RTT in nanoseconds */
t_srtt_nsecs = CCV(ccv, tcp_rtt_sa) >> 3;
/*
* Record the current SRTT as our minrtt if it's the smallest
* we've seen or minrtt is currently equal to its initialized
* value.
*
* XXXLAS: Should there be some hysteresis for minrtt?
*/
if ((t_srtt_nsecs < cubic_data->min_rtt_nsecs ||
cubic_data->min_rtt_nsecs == TCPTV_SRTTBASE)) {
cubic_data->min_rtt_nsecs = max(1, t_srtt_nsecs);
/*
* If the connection is within its first congestion
* epoch, ensure we prime mean_rtt_nsecs with a
* reasonable value until the epoch average RTT is
* calculated in cubic_post_recovery().
*/
if (cubic_data->min_rtt_nsecs >
cubic_data->mean_rtt_nsecs)
cubic_data->mean_rtt_nsecs =
cubic_data->min_rtt_nsecs;
}
/* Sum samples for epoch average RTT calculation. */
cubic_data->sum_rtt_nsecs += t_srtt_nsecs;
cubic_data->epoch_ack_count++;
}
}
/*
* Update the ssthresh in the event of congestion.
*/
static void
cubic_ssthresh_update(struct cc_var *ccv)
{
struct cubic *cubic_data;
cubic_data = ccv->cc_data;
/*
* On the first congestion event, set ssthresh to cwnd * 0.5, on
* subsequent congestion events, set it to cwnd * beta.
*/
if (cubic_data->num_cong_events == 0)
CCV(ccv, tcp_cwnd_ssthresh) = CCV(ccv, tcp_cwnd) >> 1;
else
CCV(ccv, tcp_cwnd_ssthresh) =
(CCV(ccv, tcp_cwnd) * CUBIC_BETA) >> CUBIC_SHIFT;
}
/*
* Copyright (c) 2008-2010 Lawrence Stewart <lstewart@freebsd.org>
* Copyright (c) 2010 The FreeBSD Foundation
* All rights reserved.
* Copyright (c) 2017 by Delphix. All rights reserved.
* Copyright 2019 Joyent, Inc.
* Copyright 2020 RackTop Systems, Inc.
*
* This software was developed by Lawrence Stewart while studying at the Centre
* for Advanced Internet Architectures, Swinburne University of Technology, made
* possible in part by a grant from the Cisco University Research Program Fund
* at Community Foundation Silicon Valley.
*
* Portions of this software were developed at the Centre for Advanced
* Internet Architectures, Swinburne University of Technology, Melbourne,
* Australia by David Hayes under sponsorship from the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef _NETINET_CC_CUBIC_H_
#define _NETINET_CC_CUBIC_H_
/* Number of bits of precision for fixed point math calcs. */
#define CUBIC_SHIFT 8
#define CUBIC_SHIFT_4 32
/* 0.5 << CUBIC_SHIFT. */
#define RENO_BETA 128
/* ~0.8 << CUBIC_SHIFT. */
#define CUBIC_BETA 204
/* ~0.2 << CUBIC_SHIFT. */
#define ONE_SUB_CUBIC_BETA 51
/* 3 * ONE_SUB_CUBIC_BETA. */
#define THREE_X_PT2 153
/* (2 << CUBIC_SHIFT) - ONE_SUB_CUBIC_BETA. */
#define TWO_SUB_PT2 461
/* ~0.4 << CUBIC_SHIFT. */
#define CUBIC_C_FACTOR 102
/* CUBIC fast convergence factor: ~0.9 << CUBIC_SHIFT. */
#define CUBIC_FC_FACTOR 230
/* Don't trust s_rtt until this many rtt samples have been taken. */
#define CUBIC_MIN_RTT_SAMPLES 8
/*
* (2^21)^3 is long max. Dividing (2^63) by Cubic_C_factor
* and taking cube-root yields 448845 as the effective useful limit
*/
#define CUBED_ROOT_MAX_ULONG 448845
/* Userland only bits. */
#ifndef _KERNEL
extern int hz;
/*
* Implementation based on the formulae found in the CUBIC Internet Draft
* "draft-rhee-tcpm-cubic-02".
*
* Note BETA used in cc_cubic is equal to (1-beta) in the I-D
*/
static __inline float
theoretical_cubic_k(double wmax_pkts)
{
double C;
C = 0.4;
return (pow((wmax_pkts * 0.2) / C, (1.0 / 3.0)) * pow(2, CUBIC_SHIFT));
}
static __inline uint32_t
theoretical_cubic_cwnd(int ticks_since_cong, uint32_t wmax, uint32_t smss)
{
double C, wmax_pkts;
C = 0.4;
wmax_pkts = wmax / (double)smss;
return (smss * (wmax_pkts +
(C * pow(ticks_since_cong / (double)hz -
theoretical_cubic_k(wmax_pkts) / pow(2, CUBIC_SHIFT), 3.0))));
}
static __inline uint32_t
theoretical_reno_cwnd(int ticks_since_cong, int rtt_ticks, uint32_t wmax,
uint32_t smss)
{
return ((wmax * 0.5) + ((ticks_since_cong / (float)rtt_ticks) * smss));
}
static __inline uint32_t
theoretical_tf_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
uint32_t smss)
{
return ((wmax * 0.8) + ((3 * 0.2) / (2 - 0.2) *
(ticks_since_cong / (float)rtt_ticks) * smss));
}
#endif /* !_KERNEL */
/*
* Compute the CUBIC K value used in the cwnd calculation, using an
* implementation of eqn 2 in the I-D. The method used
* here is adapted from Apple Computer Technical Report #KT-32.
*/
static __inline int64_t
cubic_k(uint32_t wmax_pkts)
{
int64_t s, K;
uint16_t p;
K = s = 0;
p = 0;
/* (wmax * beta)/C with CUBIC_SHIFT worth of precision. */
s = ((wmax_pkts * ONE_SUB_CUBIC_BETA) << CUBIC_SHIFT) / CUBIC_C_FACTOR;
/* Rebase s to be between 1 and 1/8 with a shift of CUBIC_SHIFT. */
while (s >= 256) {
s >>= 3;
p++;
}
/*
* Some magic constants taken from the Apple TR with appropriate
* shifts: 275 == 1.072302 << CUBIC_SHIFT, 98 == 0.3812513 <<
* CUBIC_SHIFT, 120 == 0.46946116 << CUBIC_SHIFT.
*/
K = (((s * 275) >> CUBIC_SHIFT) + 98) -
(((s * s * 120) >> CUBIC_SHIFT) >> CUBIC_SHIFT);
/* Multiply by 2^p to undo the rebasing of s from above. */
return (K <<= p);
}
/*
* Compute the new cwnd value using an implementation of eqn 1 from the I-D.
* Thanks to Kip Macy for help debugging this function.
*
* XXXLAS: Characterise bounds for overflow.
*/
static __inline uint32_t
cubic_cwnd(hrtime_t nsecs_since_cong, uint32_t wmax, uint32_t smss, int64_t K)
{
int64_t t, cwnd;
/*
* Convert nsecs_since_cong to milliseconds, with CUBIC_SHIFT worth
* of precision.
*/
t = NSEC2MSEC(nsecs_since_cong << CUBIC_SHIFT);
/*
* K is the time period in seconds that it will take to reach wmax. The
* value is kept in fixed point form with CUBIC_SHIFT worth of
* precision.
*
* For comparison with t, we convert K to milliseconds, and then convert
* the result back to seconds.
*
* cwnd = t - K, with CUBIC_SHIFT worth of precision.
*/
cwnd = (t - K * MILLISEC) / MILLISEC;
if (cwnd > CUBED_ROOT_MAX_ULONG)
return (INT_MAX);
if (cwnd < -CUBED_ROOT_MAX_ULONG)
return (0);
/* cwnd = (t - K)^3, with CUBIC_SHIFT^3 worth of precision. */
cwnd *= (cwnd * cwnd);
/*
* C(t - K)^3 + wmax
* The down shift by CUBIC_SHIFT_4 is because cwnd has 4 lots of
* CUBIC_SHIFT included in the value. 3 from the cubing of cwnd above,
* and an extra from multiplying through by CUBIC_C_FACTOR.
*/
cwnd = ((cwnd * CUBIC_C_FACTOR * smss) >> CUBIC_SHIFT_4) + wmax;
/*
* for negative cwnd, limiting to zero as lower bound
*/
return (max(0, cwnd));
}
/*
* Compute an approximation of the "TCP friendly" cwnd some number of
* nanoseconds after a congestion event that is designed to yield the same
* average cwnd as NewReno while using CUBIC's beta of 0.8. RTT should be the
* average RTT estimate for the path measured over the previous congestion
* epoch and wmax is the value of cwnd at the last congestion event.
*/
static __inline uint32_t
tf_cwnd(hrtime_t nsecs_since_cong, hrtime_t rtt_nsecs, uint32_t wmax,
uint32_t smss)
{
/* Equation 4 of I-D. */
return (((wmax * CUBIC_BETA) + (((THREE_X_PT2 * nsecs_since_cong *
smss) << CUBIC_SHIFT) / TWO_SUB_PT2 / rtt_nsecs)) >> CUBIC_SHIFT);
}
#endif /* _NETINET_CC_CUBIC_H_ */
/*
* Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
* All rights reserved.
* Copyright (c) 2017 by Delphix. All rights reserved.
*
* This software was developed by Lawrence Stewart while studying at the Centre
* for Advanced Internet Architectures, Swinburne University of Technology, made
* possible in part by a grant from the Cisco University Research Program Fund
* at Community Foundation Silicon Valley.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
/*
* This software was first released in 2009 by Lawrence Stewart as part of the
* NewTCP research project at Swinburne University of Technology's Centre for
* Advanced Internet Architectures, Melbourne, Australia, which was made
* possible in part by a grant from the Cisco University Research Program Fund
* at Community Foundation Silicon Valley. More details are available at:
* http://caia.swin.edu.au/urp/newtcp/
*/
#ifndef _NETINET_CC_MODULE_H_
#define _NETINET_CC_MODULE_H_
#define CCV(ccv, what) (ccv)->ccvc.tcp->what
#define CCSV(ccv, what) (ccv)->ccvc.tcp->tcp_tcps->what
#define CCV_PROTO(ccv) (ccv)->ccvc.tcp
#define CC_ABC(ccv) (ccv)->ccvc.tcp->tcp_tcps->tcps_abc
#define CC_ABC_L_VAR(ccv) (ccv)->ccvc.tcp->tcp_tcps->tcps_abc_l_var
#define TCPTV_SRTTBASE 0
#endif /* _NETINET_CC_MODULE_H_ */
/*
* Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
* The Regents of the University of California.
* Copyright (c) 2007-2008,2010
* Swinburne University of Technology, Melbourne, Australia.
* Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
* Copyright (c) 2010 The FreeBSD Foundation
* All rights reserved.
* Copyright (c) 2017 by Delphix. All rights reserved.
* Copyright 2020 RackTop Systems, Inc.
*
* This software was developed at the Centre for Advanced Internet
* Architectures, Swinburne University of Technology, by Lawrence Stewart, James
* Healy and David Hayes, made possible in part by a grant from the Cisco
* University Research Program Fund at Community Foundation Silicon Valley.
*
* Portions of this software were developed at the Centre for Advanced
* Internet Architectures, Swinburne University of Technology, Melbourne,
* Australia by David Hayes under sponsorship from the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* This software was first released in 2007 by James Healy and Lawrence Stewart
* whilst working on the NewTCP research project at Swinburne University of
* Technology's Centre for Advanced Internet Architectures, Melbourne,
* Australia, which was made possible in part by a grant from the Cisco
* University Research Program Fund at Community Foundation Silicon Valley.
* More details are available at:
* http://caia.swin.edu.au/urp/newtcp/
*/
#include <sys/errno.h>
#include <inet/tcp.h>
#include <inet/tcp_impl.h>
#include <inet/cc.h>
#include <inet/cc/cc_module.h>
static void newreno_ack_received(struct cc_var *ccv, uint16_t type);
static void newreno_after_idle(struct cc_var *ccv);
static void newreno_cong_signal(struct cc_var *ccv, uint32_t type);
static void newreno_post_recovery(struct cc_var *ccv);
static struct modlmisc cc_newreno_modlmisc = {
&mod_miscops,
"New Reno Congestion Control"
};
static struct modlinkage cc_newreno_modlinkage = {
MODREV_1,
&cc_newreno_modlmisc,
NULL
};
struct cc_algo newreno_cc_algo = {
.name = "newreno",
.ack_received = newreno_ack_received,
.after_idle = newreno_after_idle,
.cong_signal = newreno_cong_signal,
.post_recovery = newreno_post_recovery,
};
int
_init(void)
{
int err;
if ((err = cc_register_algo(&newreno_cc_algo)) == 0) {
if ((err = mod_install(&cc_newreno_modlinkage)) != 0)
(void) cc_deregister_algo(&newreno_cc_algo);
}
return (err);
}
int
_fini(void)
{
/* XXX Not unloadable for now */
return (EBUSY);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&cc_newreno_modlinkage, modinfop));
}
static void
newreno_ack_received(struct cc_var *ccv, uint16_t type)
{
if (type == CC_ACK && !IN_RECOVERY(ccv->flags) &&
(ccv->flags & CCF_CWND_LIMITED)) {
uint_t cw = CCV(ccv, tcp_cwnd);
uint_t incr = CCV(ccv, tcp_mss);
/*
* Regular in-order ACK, open the congestion window.
* Method depends on which congestion control state we're
* in (slow start or cong avoid) and if ABC (RFC 3465) is
* enabled.
*
* slow start: cwnd <= ssthresh
* cong avoid: cwnd > ssthresh
*
* slow start and ABC (RFC 3465):
* Grow cwnd exponentially by the amount of data
* ACKed capping the max increment per ACK to
* (abc_l_var * maxseg) bytes.
*
* slow start without ABC (RFC 5681):
* Grow cwnd exponentially by maxseg per ACK.
*
* cong avoid and ABC (RFC 3465):
* Grow cwnd linearly by maxseg per RTT for each
* cwnd worth of ACKed data.
*
* cong avoid without ABC (RFC 5681):
* Grow cwnd linearly by approximately maxseg per RTT using
* maxseg^2 / cwnd per ACK as the increment.
* If cwnd > maxseg^2, fix the cwnd increment at 1 byte to
* avoid capping cwnd.
*/
if (cw > CCV(ccv, tcp_cwnd_ssthresh)) {
if (CC_ABC(ccv)) {
if (ccv->flags & CCF_ABC_SENTAWND)
ccv->flags &= ~CCF_ABC_SENTAWND;
else
incr = 0;
} else
incr = max((incr * incr / cw), 1);
} else if (CC_ABC(ccv)) {
/*
* In slow-start with ABC enabled and no RTO in sight?
* (Must not use abc_l_var > 1 if slow starting after
* an RTO.
*/
if (ccv->flags & CCF_RTO) {
incr = min(ccv->bytes_this_ack,
CCV(ccv, tcp_mss));
} else {
incr = min(ccv->bytes_this_ack,
CC_ABC_L_VAR(ccv) * CCV(ccv, tcp_mss));
}
}
/* ABC is on by default, so incr equals 0 frequently. */
if (incr > 0)
CCV(ccv, tcp_cwnd) = min(cw + incr,
TCP_MAXWIN << CCV(ccv, tcp_snd_ws));
}
}
static void
newreno_after_idle(struct cc_var *ccv)
{
int rw;
/*
* If we've been idle for more than one retransmit timeout the old
* congestion window is no longer current and we have to reduce it to
* the restart window before we can transmit again.
*
* The restart window is the initial window or the last CWND, whichever
* is smaller.
*
* This is done to prevent us from flooding the path with a full CWND at
* wirespeed, overloading router and switch buffers along the way.
*
* See RFC5681 Section 4.1. "Restarting Idle Connections".
*/
if (CCV(ccv, tcp_init_cwnd) != 0) {
/*
* The TCP_INIT_CWND socket option was used to override the
* default.
*/
rw = CCV(ccv, tcp_init_cwnd) * CCV(ccv, tcp_mss);
} else if (CCSV(ccv, tcps_slow_start_initial) != 0) {
/* The _slow_start_initial tunable was explicitly set. */
rw = min(TCP_MAX_INIT_CWND, CCSV(ccv, tcps_slow_start_initial))
* CCV(ccv, tcp_mss);
} else {
/* Do RFC 3390 */
rw = min(4 * CCV(ccv, tcp_mss),
max(2 * CCV(ccv, tcp_mss), 4380));
}
CCV(ccv, tcp_cwnd) = min(rw, CCV(ccv, tcp_cwnd));
}
/*
* Perform any necessary tasks before we enter congestion recovery.
*/
static void
newreno_cong_signal(struct cc_var *ccv, uint32_t type)
{
uint32_t cwin, ssthresh_on_loss;
uint32_t mss;
cwin = CCV(ccv, tcp_cwnd);
mss = CCV(ccv, tcp_mss);
ssthresh_on_loss =
max((CCV(ccv, tcp_snxt) - CCV(ccv, tcp_suna)) / 2 / mss, 2)
* mss;
/* Catch algos which mistakenly leak private signal types. */
ASSERT((type & CC_SIGPRIVMASK) == 0);
cwin = max(cwin / 2 / mss, 2) * mss;
switch (type) {
case CC_NDUPACK:
if (!IN_FASTRECOVERY(ccv->flags)) {
if (!IN_CONGRECOVERY(ccv->flags)) {
CCV(ccv, tcp_cwnd_ssthresh) = ssthresh_on_loss;
CCV(ccv, tcp_cwnd) = cwin;
}
ENTER_RECOVERY(ccv->flags);
}
break;
case CC_ECN:
if (!IN_CONGRECOVERY(ccv->flags)) {
CCV(ccv, tcp_cwnd_ssthresh) = ssthresh_on_loss;
CCV(ccv, tcp_cwnd) = cwin;
ENTER_CONGRECOVERY(ccv->flags);
}
break;
case CC_RTO:
CCV(ccv, tcp_cwnd_ssthresh) = ssthresh_on_loss;
CCV(ccv, tcp_cwnd) = mss;
break;
}
}
/*
* Perform any necessary tasks before we exit congestion recovery.
*/
static void
newreno_post_recovery(struct cc_var *ccv)
{
uint32_t pipe;
if (IN_FASTRECOVERY(ccv->flags)) {
/*
* Fast recovery will conclude after returning from this
* function. Window inflation should have left us with
* approximately cwnd_ssthresh outstanding data. But in case we
* would be inclined to send a burst, better to do it via the
* slow start mechanism.
*/
pipe = CCV(ccv, tcp_snxt) - CCV(ccv, tcp_suna);
if (pipe < CCV(ccv, tcp_cwnd_ssthresh)) {
/*
* Ensure that cwnd does not collapse to 1 MSS under
* adverse conditions. Implements RFC6582
*/
CCV(ccv, tcp_cwnd) = MAX(pipe, CCV(ccv, tcp_mss)) +
CCV(ccv, tcp_mss);
} else if (CCV(ccv, tcp_cwnd) > CCV(ccv, tcp_cwnd_ssthresh)) {
CCV(ccv, tcp_cwnd) = CCV(ccv, tcp_cwnd_ssthresh);
}
}
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2017 by Delphix. All rights reserved.
*/
/*
* The TCP congestion control algorithm extracted from the pre-framework
* implementation of TCP congestion control.
*/
#include <sys/errno.h>
#include <inet/tcp.h>
#include <inet/tcp_impl.h>
#include <inet/cc.h>
#include <inet/cc/cc_module.h>
static void sunreno_ack_received(struct cc_var *ccv, uint16_t type);
static void sunreno_after_idle(struct cc_var *ccv);
static void sunreno_cong_signal(struct cc_var *ccv, uint32_t type);
static void sunreno_post_recovery(struct cc_var *ccv);
#define CC_SUNRENO_ALGO_NAME "sunreno"
static struct modlmisc cc_sunreno_modlmisc = {
&mod_miscops,
"SUNReno Congestion Control"
};
static struct modlinkage cc_sunreno_modlinkage = {
MODREV_1,
&cc_sunreno_modlmisc,
NULL
};
struct cc_algo sunreno_cc_algo = {
.name = CC_SUNRENO_ALGO_NAME,
.ack_received = sunreno_ack_received,
.after_idle = sunreno_after_idle,
.cong_signal = sunreno_cong_signal,
.post_recovery = sunreno_post_recovery,
};
int
_init(void)
{
int err;
if ((err = cc_register_algo(&sunreno_cc_algo)) == 0) {
if ((err = mod_install(&cc_sunreno_modlinkage)) != 0)
(void) cc_deregister_algo(&sunreno_cc_algo);
}
return (err);
}
int
_fini(void)
{
return (EBUSY);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&cc_sunreno_modlinkage, modinfop));
}
static void
sunreno_ack_received(struct cc_var *ccv, uint16_t type)
{
uint32_t add;
uint32_t cwnd;
int mss;
if (type == CC_ACK && !IN_RECOVERY(ccv->flags)) {
mss = CCV(ccv, tcp_mss);
cwnd = CCV(ccv, tcp_cwnd);
add = mss;
if (cwnd >= CCV(ccv, tcp_cwnd_ssthresh)) {
/*
* This is to prevent an increase of less than 1 MSS of
* tcp_cwnd. With partial increase, tcp_wput_data()
* may send out tinygrams in order to preserve mblk
* boundaries.
*
* By initializing tcp_cwnd_cnt to new tcp_cwnd and
* decrementing it by 1 MSS for every ACKs, tcp_cwnd is
* increased by 1 MSS for every RTTs.
*/
if (CCV(ccv, tcp_cwnd_cnt) <= 0) {
CCV(ccv, tcp_cwnd_cnt) = cwnd + add;
} else {
CCV(ccv, tcp_cwnd_cnt) -= add;
add = 0;
}
}
CCV(ccv, tcp_cwnd) = MIN(cwnd + add, CCV(ccv, tcp_cwnd_max));
}
}
static void
sunreno_after_idle(struct cc_var *ccv)
{
int32_t num_sack_blk = 0;
int mss;
if (CCV(ccv, tcp_snd_sack_ok) && CCV(ccv, tcp_num_sack_blk) > 0) {
int32_t opt_len;
num_sack_blk = MIN(CCV(ccv, tcp_max_sack_blk),
CCV(ccv, tcp_num_sack_blk));
opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN *
2 + TCPOPT_HEADER_LEN;
mss = CCV(ccv, tcp_mss) - opt_len;
} else {
mss = CCV(ccv, tcp_mss);
}
TCP_SET_INIT_CWND(CCV_PROTO(ccv), mss,
CCSV(ccv, tcps_slow_start_after_idle));
}
/*
* Perform any necessary tasks before we enter congestion recovery.
*/
static void
sunreno_cong_signal(struct cc_var *ccv, uint32_t type)
{
int npkt;
int mss;
/* Catch algos which mistakenly leak private signal types. */
ASSERT((type & CC_SIGPRIVMASK) == 0);
mss = CCV(ccv, tcp_mss);
npkt = ((CCV(ccv, tcp_snxt) - CCV(ccv, tcp_suna)) >> 1) / mss;
switch (type) {
case CC_NDUPACK:
if (!IN_FASTRECOVERY(ccv->flags)) {
if (!IN_CONGRECOVERY(ccv->flags)) {
CCV(ccv, tcp_cwnd_ssthresh) = MAX(npkt, 2) *
mss;
CCV(ccv, tcp_cwnd) = (npkt +
CCV(ccv, tcp_dupack_cnt)) * mss;
}
ENTER_RECOVERY(ccv->flags);
}
break;
case CC_ECN:
if (!IN_CONGRECOVERY(ccv->flags) && !CCV(ccv, tcp_cwr)) {
CCV(ccv, tcp_cwnd_ssthresh) = MAX(npkt, 2) * mss;
CCV(ccv, tcp_cwnd) = npkt * mss;
if (CCV(ccv, tcp_cwnd) == 0) {
/*
* This makes sure that when the ACK comes
* back, we will increase tcp_cwnd by 1 MSS.
*/
CCV(ccv, tcp_cwnd_cnt) = 0;
}
ENTER_CONGRECOVERY(ccv->flags);
}
break;
case CC_RTO:
/*
* After retransmission, we need to do slow start. Set the
* ssthresh to one half of current effective window and cwnd to
* one MSS. Also reset tcp_cwnd_cnt.
*
* Note that if tcp_ssthresh is reduced because of ECN, do not
* reduce it again unless it is already one window of data away
* (tcp_cwr should then be cleared) or this is a timeout for a
* retransmitted segment.
*/
if (!CCV(ccv, tcp_cwr) || CCV(ccv, tcp_rexmit)) {
if (CCV(ccv, tcp_timer_backoff) != 0)
npkt = CCV(ccv, tcp_cwnd_ssthresh) / 2 / mss;
CCV(ccv, tcp_cwnd_ssthresh) = MAX(npkt, 2) * mss;
}
CCV(ccv, tcp_cwnd) = mss;
CCV(ccv, tcp_cwnd_cnt) = 0;
break;
}
}
/*
* Perform any necessary tasks before we exit congestion recovery.
*/
static void
sunreno_post_recovery(struct cc_var *ccv)
{
/*
* Restore the congestion window back to ssthresh as per RFC 5681
* section 3.2.
*/
if (IN_FASTRECOVERY(ccv->flags)) {
if (CCV(ccv, tcp_cwnd) > CCV(ccv, tcp_cwnd_ssthresh)) {
CCV(ccv, tcp_cwnd) = CCV(ccv, tcp_cwnd_ssthresh);
}
}
CCV(ccv, tcp_cwnd_cnt) = 0;
}
|