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
|
/*
* ngtcp2
*
* Copyright (c) 2021 ngtcp2 contributors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "ngtcp2_bbr.h"
#include <assert.h>
#include <string.h>
#include "ngtcp2_log.h"
#include "ngtcp2_macro.h"
#include "ngtcp2_mem.h"
#include "ngtcp2_rcvry.h"
#include "ngtcp2_rst.h"
#include "ngtcp2_conn_stat.h"
#define NGTCP2_BBR_MAX_BW_FILTERLEN 2
#define NGTCP2_BBR_EXTRA_ACKED_FILTERLEN 10
#define NGTCP2_BBR_STARTUP_PACING_GAIN_H 277
#define NGTCP2_BBR_STARTUP_CWND_GAIN_H 200
#define NGTCP2_BBR_PROBE_RTT_CWND_GAIN_H 50
#define NGTCP2_BBR_BETA_NUMER 7
#define NGTCP2_BBR_BETA_DENOM 10
#define NGTCP2_BBR_LOSS_THRESH_NUMER 2
#define NGTCP2_BBR_LOSS_THRESH_DENOM 100
#define NGTCP2_BBR_HEADROOM_NUMER 15
#define NGTCP2_BBR_HEADROOM_DENOM 100
#define NGTCP2_BBR_PROBE_RTT_INTERVAL (5 * NGTCP2_SECONDS)
#define NGTCP2_BBR_MIN_RTT_FILTERLEN (10 * NGTCP2_SECONDS)
#define NGTCP2_BBR_PROBE_RTT_DURATION (200 * NGTCP2_MILLISECONDS)
#define NGTCP2_BBR_PACING_MARGIN_PERCENT 1
static void bbr_on_init(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
ngtcp2_tstamp initial_ts);
static void bbr_on_transmit(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts);
static void bbr_reset_congestion_signals(ngtcp2_cc_bbr *bbr);
static void bbr_reset_lower_bounds(ngtcp2_cc_bbr *bbr);
static void bbr_init_round_counting(ngtcp2_cc_bbr *bbr);
static void bbr_init_full_pipe(ngtcp2_cc_bbr *bbr);
static void bbr_init_pacing_rate(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat);
static void bbr_set_pacing_rate_with_gain(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
uint64_t pacing_gain_h);
static void bbr_set_pacing_rate(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat);
static void bbr_enter_startup(ngtcp2_cc_bbr *bbr);
static void bbr_check_startup_done(ngtcp2_cc_bbr *bbr,
const ngtcp2_cc_ack *ack);
static void bbr_update_on_ack(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack, ngtcp2_tstamp ts);
static void bbr_update_model_and_state(ngtcp2_cc_bbr *cc,
ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack,
ngtcp2_tstamp ts);
static void bbr_update_control_parameters(ngtcp2_cc_bbr *cc,
ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack);
static void bbr_update_on_loss(ngtcp2_cc_bbr *cc, ngtcp2_conn_stat *cstat,
const ngtcp2_cc_pkt *pkt, ngtcp2_tstamp ts);
static void bbr_update_latest_delivery_signals(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat);
static void bbr_advance_latest_delivery_signals(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat);
static void bbr_update_congestion_signals(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack);
static void bbr_adapt_lower_bounds_from_congestion(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat);
static void bbr_init_lower_bounds(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat);
static void bbr_loss_lower_bounds(ngtcp2_cc_bbr *bbr);
static void bbr_bound_bw_for_model(ngtcp2_cc_bbr *bbr);
static void bbr_update_max_bw(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack);
static void bbr_update_round(ngtcp2_cc_bbr *bbr, const ngtcp2_cc_ack *ack);
static void bbr_start_round(ngtcp2_cc_bbr *bbr);
static int bbr_is_in_probe_bw_state(ngtcp2_cc_bbr *bbr);
static void bbr_update_ack_aggregation(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack,
ngtcp2_tstamp ts);
static void bbr_enter_drain(ngtcp2_cc_bbr *bbr);
static void bbr_check_drain(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts);
static void bbr_enter_probe_bw(ngtcp2_cc_bbr *bbr, ngtcp2_tstamp ts);
static void bbr_start_probe_bw_down(ngtcp2_cc_bbr *bbr, ngtcp2_tstamp ts);
static void bbr_start_probe_bw_cruise(ngtcp2_cc_bbr *bbr);
static void bbr_start_probe_bw_refill(ngtcp2_cc_bbr *bbr);
static void bbr_start_probe_bw_up(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts);
static void bbr_update_probe_bw_cycle_phase(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack,
ngtcp2_tstamp ts);
static int bbr_check_time_to_cruise(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts);
static int bbr_has_elapsed_in_phase(ngtcp2_cc_bbr *bbr,
ngtcp2_duration interval, ngtcp2_tstamp ts);
static uint64_t bbr_inflight_with_headroom(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat);
static void bbr_raise_inflight_hi_slope(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat);
static void bbr_probe_inflight_hi_upward(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack);
static void bbr_adapt_upper_bounds(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack, ngtcp2_tstamp ts);
static int bbr_check_time_to_probe_bw(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts);
static void bbr_pick_probe_wait(ngtcp2_cc_bbr *bbr);
static int bbr_is_reno_coexistence_probe_time(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat);
static uint64_t bbr_target_inflight(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat);
static int bbr_check_inflight_too_high(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts);
static int is_inflight_too_high(const ngtcp2_rs *rs);
static void bbr_handle_inflight_too_high(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
const ngtcp2_rs *rs, ngtcp2_tstamp ts);
static void bbr_handle_lost_packet(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
const ngtcp2_cc_pkt *pkt, ngtcp2_tstamp ts);
static uint64_t bbr_inflight_hi_from_lost_packet(ngtcp2_cc_bbr *bbr,
const ngtcp2_rs *rs,
const ngtcp2_cc_pkt *pkt);
static void bbr_update_min_rtt(ngtcp2_cc_bbr *bbr, const ngtcp2_cc_ack *ack,
ngtcp2_tstamp ts);
static void bbr_check_probe_rtt(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts);
static void bbr_enter_probe_rtt(ngtcp2_cc_bbr *bbr);
static void bbr_handle_probe_rtt(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts);
static void bbr_check_probe_rtt_done(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat, ngtcp2_tstamp ts);
static void bbr_mark_connection_app_limited(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat);
static void bbr_exit_probe_rtt(ngtcp2_cc_bbr *bbr, ngtcp2_tstamp ts);
static void bbr_handle_restart_from_idle(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts);
static uint64_t bbr_bdp_multiple(ngtcp2_cc_bbr *bbr, uint64_t bw,
uint64_t gain_h);
static uint64_t bbr_quantization_budget(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
uint64_t inflight);
static uint64_t bbr_inflight(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
uint64_t bw, uint64_t gain_h);
static void bbr_update_max_inflight(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat);
static void bbr_update_offload_budget(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat);
static uint64_t min_pipe_cwnd(size_t max_udp_payload_size);
static void bbr_advance_max_bw_filter(ngtcp2_cc_bbr *bbr);
static void bbr_modulate_cwnd_for_recovery(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack);
static void bbr_save_cwnd(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat);
static void bbr_restore_cwnd(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat);
static uint64_t bbr_probe_rtt_cwnd(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat);
static void bbr_bound_cwnd_for_probe_rtt(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat);
static void bbr_set_cwnd(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack);
static void bbr_bound_cwnd_for_model(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat);
static void bbr_set_send_quantum(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat);
static int in_congestion_recovery(const ngtcp2_conn_stat *cstat,
ngtcp2_tstamp sent_time);
static void bbr_handle_recovery(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack, ngtcp2_tstamp ts);
static void bbr_on_init(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
ngtcp2_tstamp initial_ts) {
ngtcp2_window_filter_init(&bbr->max_bw_filter, NGTCP2_BBR_MAX_BW_FILTERLEN);
ngtcp2_window_filter_init(&bbr->extra_acked_filter,
NGTCP2_BBR_EXTRA_ACKED_FILTERLEN);
bbr->min_rtt = UINT64_MAX;
bbr->min_rtt_stamp = initial_ts;
/* remark: Use UINT64_MAX instead of 0 for consistency. */
bbr->probe_rtt_done_stamp = UINT64_MAX;
bbr->probe_rtt_round_done = 0;
bbr->prior_cwnd = 0;
bbr->idle_restart = 0;
bbr->extra_acked_interval_start = initial_ts;
bbr->extra_acked_delivered = 0;
bbr_reset_congestion_signals(bbr);
bbr_reset_lower_bounds(bbr);
bbr_init_round_counting(bbr);
bbr_init_full_pipe(bbr);
bbr_init_pacing_rate(bbr, cstat);
bbr_enter_startup(bbr);
cstat->send_quantum = cstat->max_tx_udp_payload_size * 10;
/* Missing in documentation */
bbr->loss_round_start = 0;
bbr->loss_round_delivered = UINT64_MAX;
bbr->rounds_since_bw_probe = 0;
bbr->max_bw = 0;
bbr->bw = 0;
bbr->cycle_count = 0;
bbr->extra_acked = 0;
bbr->bytes_lost_in_round = 0;
bbr->loss_events_in_round = 0;
bbr->offload_budget = 0;
bbr->probe_up_cnt = UINT64_MAX;
bbr->cycle_stamp = UINT64_MAX;
bbr->ack_phase = 0;
bbr->bw_probe_wait = 0;
bbr->bw_probe_samples = 0;
bbr->bw_probe_up_rounds = 0;
bbr->bw_probe_up_acks = 0;
bbr->inflight_hi = UINT64_MAX;
bbr->bw_hi = UINT64_MAX;
bbr->probe_rtt_expired = 0;
bbr->probe_rtt_min_delay = UINT64_MAX;
bbr->probe_rtt_min_stamp = initial_ts;
bbr->in_loss_recovery = 0;
bbr->packet_conservation = 0;
bbr->max_inflight = 0;
bbr->congestion_recovery_start_ts = UINT64_MAX;
bbr->congestion_recovery_next_round_delivered = 0;
bbr->prior_inflight_lo = 0;
bbr->prior_inflight_hi = 0;
bbr->prior_bw_lo = 0;
}
static void bbr_reset_congestion_signals(ngtcp2_cc_bbr *bbr) {
bbr->loss_in_round = 0;
bbr->bw_latest = 0;
bbr->inflight_latest = 0;
}
static void bbr_reset_lower_bounds(ngtcp2_cc_bbr *bbr) {
bbr->bw_lo = UINT64_MAX;
bbr->inflight_lo = UINT64_MAX;
}
static void bbr_init_round_counting(ngtcp2_cc_bbr *bbr) {
bbr->next_round_delivered = 0;
bbr->round_start = 0;
bbr->round_count = 0;
}
static void bbr_init_full_pipe(ngtcp2_cc_bbr *bbr) {
bbr->filled_pipe = 0;
bbr->full_bw = 0;
bbr->full_bw_count = 0;
}
static void bbr_check_startup_full_bandwidth(ngtcp2_cc_bbr *bbr) {
if (bbr->filled_pipe || !bbr->round_start || bbr->rst->rs.is_app_limited) {
return;
}
if (bbr->max_bw * 100 >= bbr->full_bw * 125) {
bbr->full_bw = bbr->max_bw;
bbr->full_bw_count = 0;
}
++bbr->full_bw_count;
if (bbr->full_bw_count >= 3) {
bbr->filled_pipe = 1;
ngtcp2_log_info(bbr->cc.log, NGTCP2_LOG_EVENT_CCA,
"bbr filled pipe, full_bw=%" PRIu64, bbr->full_bw);
}
}
static void bbr_check_startup_high_loss(ngtcp2_cc_bbr *bbr,
const ngtcp2_cc_ack *ack) {
if (bbr->filled_pipe || !bbr->round_start || bbr->rst->rs.is_app_limited) {
return;
}
if (bbr->loss_events_in_round <= 3) {
return;
}
/* loss_thresh = 2% */
if (bbr->bytes_lost_in_round * 100 <= ack->prior_bytes_in_flight * 2) {
return;
}
bbr->filled_pipe = 1;
}
static void bbr_init_pacing_rate(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat) {
cstat->pacing_interval = NGTCP2_MILLISECONDS * 100 /
NGTCP2_BBR_STARTUP_PACING_GAIN_H / bbr->initial_cwnd;
}
static void bbr_set_pacing_rate_with_gain(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
uint64_t pacing_gain_h) {
ngtcp2_duration interval;
if (bbr->bw == 0) {
return;
}
interval = NGTCP2_SECONDS * 100 * 100 / pacing_gain_h / bbr->bw /
(100 - NGTCP2_BBR_PACING_MARGIN_PERCENT);
if (bbr->filled_pipe || interval < cstat->pacing_interval) {
cstat->pacing_interval = interval;
}
}
static void bbr_set_pacing_rate(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat) {
bbr_set_pacing_rate_with_gain(bbr, cstat, bbr->pacing_gain_h);
}
static void bbr_enter_startup(ngtcp2_cc_bbr *bbr) {
ngtcp2_log_info(bbr->cc.log, NGTCP2_LOG_EVENT_CCA, "bbr enter Startup");
bbr->state = NGTCP2_BBR_STATE_STARTUP;
bbr->pacing_gain_h = NGTCP2_BBR_STARTUP_PACING_GAIN_H;
bbr->cwnd_gain_h = NGTCP2_BBR_STARTUP_CWND_GAIN_H;
}
static void bbr_check_startup_done(ngtcp2_cc_bbr *bbr,
const ngtcp2_cc_ack *ack) {
bbr_check_startup_full_bandwidth(bbr);
bbr_check_startup_high_loss(bbr, ack);
if (bbr->state == NGTCP2_BBR_STATE_STARTUP && bbr->filled_pipe) {
bbr_enter_drain(bbr);
}
}
static void bbr_on_transmit(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts) {
bbr_handle_restart_from_idle(bbr, cstat, ts);
}
static void bbr_update_on_ack(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack, ngtcp2_tstamp ts) {
bbr_update_model_and_state(bbr, cstat, ack, ts);
bbr_update_control_parameters(bbr, cstat, ack);
}
static void bbr_update_model_and_state(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack,
ngtcp2_tstamp ts) {
bbr_update_latest_delivery_signals(bbr, cstat);
bbr_update_congestion_signals(bbr, cstat, ack);
bbr_update_ack_aggregation(bbr, cstat, ack, ts);
bbr_check_startup_done(bbr, ack);
bbr_check_drain(bbr, cstat, ts);
bbr_update_probe_bw_cycle_phase(bbr, cstat, ack, ts);
bbr_update_min_rtt(bbr, ack, ts);
bbr_check_probe_rtt(bbr, cstat, ts);
bbr_advance_latest_delivery_signals(bbr, cstat);
bbr_bound_bw_for_model(bbr);
}
static void bbr_update_control_parameters(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack) {
bbr_set_pacing_rate(bbr, cstat);
bbr_set_send_quantum(bbr, cstat);
bbr_set_cwnd(bbr, cstat, ack);
}
static void bbr_update_on_loss(ngtcp2_cc_bbr *cc, ngtcp2_conn_stat *cstat,
const ngtcp2_cc_pkt *pkt, ngtcp2_tstamp ts) {
bbr_handle_lost_packet(cc, cstat, pkt, ts);
}
static void bbr_update_latest_delivery_signals(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat) {
bbr->loss_round_start = 0;
bbr->bw_latest = ngtcp2_max(bbr->bw_latest, cstat->delivery_rate_sec);
bbr->inflight_latest =
ngtcp2_max(bbr->inflight_latest, bbr->rst->rs.delivered);
if (bbr->rst->rs.prior_delivered >= bbr->loss_round_delivered) {
bbr->loss_round_delivered = bbr->rst->delivered;
bbr->loss_round_start = 1;
}
}
static void bbr_advance_latest_delivery_signals(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat) {
if (bbr->loss_round_start) {
bbr->bw_latest = cstat->delivery_rate_sec;
bbr->inflight_latest = bbr->rst->rs.delivered;
}
}
static void bbr_update_congestion_signals(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack) {
bbr_update_max_bw(bbr, cstat, ack);
if (ack->bytes_lost) {
bbr->bytes_lost_in_round += ack->bytes_lost;
++bbr->loss_events_in_round;
if (!bbr->loss_in_round) {
bbr->loss_in_round = 1;
bbr->loss_round_delivered = bbr->rst->delivered;
}
}
if (!bbr->loss_round_start) {
return;
}
bbr_adapt_lower_bounds_from_congestion(bbr, cstat);
bbr->loss_in_round = 0;
}
static void bbr_adapt_lower_bounds_from_congestion(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat) {
if (bbr_is_in_probe_bw_state(bbr)) {
return;
}
if (bbr->loss_in_round) {
bbr_init_lower_bounds(bbr, cstat);
bbr_loss_lower_bounds(bbr);
}
}
static void bbr_init_lower_bounds(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat) {
if (bbr->bw_lo == UINT64_MAX) {
bbr->bw_lo = bbr->max_bw;
}
if (bbr->inflight_lo == UINT64_MAX) {
bbr->inflight_lo = cstat->cwnd;
}
}
static void bbr_loss_lower_bounds(ngtcp2_cc_bbr *bbr) {
bbr->bw_lo =
ngtcp2_max_uint64(bbr->bw_latest, bbr->bw_lo * NGTCP2_BBR_BETA_NUMER /
NGTCP2_BBR_BETA_DENOM);
bbr->inflight_lo = ngtcp2_max_uint64(
bbr->inflight_latest,
bbr->inflight_lo * NGTCP2_BBR_BETA_NUMER / NGTCP2_BBR_BETA_DENOM);
}
static void bbr_bound_bw_for_model(ngtcp2_cc_bbr *bbr) {
bbr->bw = ngtcp2_min(bbr->max_bw, bbr->bw_lo);
bbr->bw = ngtcp2_min(bbr->bw, bbr->bw_hi);
}
static void bbr_update_max_bw(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack) {
bbr_update_round(bbr, ack);
if (cstat->delivery_rate_sec >= bbr->max_bw || !bbr->rst->rs.is_app_limited) {
ngtcp2_window_filter_update(&bbr->max_bw_filter, cstat->delivery_rate_sec,
bbr->cycle_count);
bbr->max_bw = ngtcp2_window_filter_get_best(&bbr->max_bw_filter);
}
}
static void bbr_update_round(ngtcp2_cc_bbr *bbr, const ngtcp2_cc_ack *ack) {
if (ack->pkt_delivered >= bbr->next_round_delivered) {
bbr_start_round(bbr);
++bbr->round_count;
++bbr->rounds_since_bw_probe;
bbr->round_start = 1;
bbr->bytes_lost_in_round = 0;
bbr->loss_events_in_round = 0;
bbr->rst->is_cwnd_limited = 0;
return;
}
bbr->round_start = 0;
}
static void bbr_start_round(ngtcp2_cc_bbr *bbr) {
bbr->next_round_delivered = bbr->rst->delivered;
}
static int bbr_is_in_probe_bw_state(ngtcp2_cc_bbr *bbr) {
switch (bbr->state) {
case NGTCP2_BBR_STATE_PROBE_BW_DOWN:
case NGTCP2_BBR_STATE_PROBE_BW_CRUISE:
case NGTCP2_BBR_STATE_PROBE_BW_REFILL:
case NGTCP2_BBR_STATE_PROBE_BW_UP:
return 1;
default:
return 0;
}
}
static void bbr_update_ack_aggregation(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack,
ngtcp2_tstamp ts) {
ngtcp2_duration interval = ts - bbr->extra_acked_interval_start;
uint64_t expected_delivered = bbr->bw * interval / NGTCP2_SECONDS;
uint64_t extra;
if (bbr->extra_acked_delivered <= expected_delivered) {
bbr->extra_acked_delivered = 0;
bbr->extra_acked_interval_start = ts;
expected_delivered = 0;
}
bbr->extra_acked_delivered += ack->bytes_delivered;
extra = bbr->extra_acked_delivered - expected_delivered;
extra = ngtcp2_min(extra, cstat->cwnd);
ngtcp2_window_filter_update(&bbr->extra_acked_filter, extra,
bbr->round_count);
bbr->extra_acked = ngtcp2_window_filter_get_best(&bbr->extra_acked_filter);
}
static void bbr_enter_drain(ngtcp2_cc_bbr *bbr) {
ngtcp2_log_info(bbr->cc.log, NGTCP2_LOG_EVENT_CCA, "bbr enter Drain");
bbr->state = NGTCP2_BBR_STATE_DRAIN;
bbr->pacing_gain_h = 100 * 100 / NGTCP2_BBR_STARTUP_CWND_GAIN_H;
bbr->cwnd_gain_h = NGTCP2_BBR_STARTUP_CWND_GAIN_H;
}
static void bbr_check_drain(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts) {
if (bbr->state == NGTCP2_BBR_STATE_DRAIN &&
cstat->bytes_in_flight <= bbr_inflight(bbr, cstat, bbr->bw, 100)) {
bbr_enter_probe_bw(bbr, ts);
}
}
static void bbr_enter_probe_bw(ngtcp2_cc_bbr *bbr, ngtcp2_tstamp ts) {
bbr_start_probe_bw_down(bbr, ts);
}
static void bbr_start_probe_bw_down(ngtcp2_cc_bbr *bbr, ngtcp2_tstamp ts) {
ngtcp2_log_info(bbr->cc.log, NGTCP2_LOG_EVENT_CCA, "bbr start ProbeBW_DOWN");
bbr_reset_congestion_signals(bbr);
bbr->probe_up_cnt = UINT64_MAX;
bbr_pick_probe_wait(bbr);
bbr->cycle_stamp = ts;
bbr->ack_phase = NGTCP2_BBR_ACK_PHASE_ACKS_PROBE_STOPPING;
bbr_start_round(bbr);
bbr->state = NGTCP2_BBR_STATE_PROBE_BW_DOWN;
bbr->pacing_gain_h = 90;
bbr->cwnd_gain_h = 200;
}
static void bbr_start_probe_bw_cruise(ngtcp2_cc_bbr *bbr) {
ngtcp2_log_info(bbr->cc.log, NGTCP2_LOG_EVENT_CCA,
"bbr start ProbeBW_CRUISE");
bbr->state = NGTCP2_BBR_STATE_PROBE_BW_CRUISE;
bbr->pacing_gain_h = 100;
bbr->cwnd_gain_h = 200;
}
static void bbr_start_probe_bw_refill(ngtcp2_cc_bbr *bbr) {
ngtcp2_log_info(bbr->cc.log, NGTCP2_LOG_EVENT_CCA,
"bbr start ProbeBW_REFILL");
bbr_reset_lower_bounds(bbr);
bbr->bw_probe_up_rounds = 0;
bbr->bw_probe_up_acks = 0;
bbr->ack_phase = NGTCP2_BBR_ACK_PHASE_ACKS_REFILLING;
bbr_start_round(bbr);
bbr->state = NGTCP2_BBR_STATE_PROBE_BW_REFILL;
bbr->pacing_gain_h = 100;
bbr->cwnd_gain_h = 200;
}
static void bbr_start_probe_bw_up(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts) {
ngtcp2_log_info(bbr->cc.log, NGTCP2_LOG_EVENT_CCA, "bbr start ProbeBW_UP");
bbr->ack_phase = NGTCP2_BBR_ACK_PHASE_ACKS_PROBE_STARTING;
bbr_start_round(bbr);
bbr->cycle_stamp = ts;
bbr->state = NGTCP2_BBR_STATE_PROBE_BW_UP;
bbr->pacing_gain_h = 125;
bbr->cwnd_gain_h = 225;
bbr_raise_inflight_hi_slope(bbr, cstat);
}
static void bbr_update_probe_bw_cycle_phase(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack,
ngtcp2_tstamp ts) {
if (!bbr->filled_pipe) {
return;
}
bbr_adapt_upper_bounds(bbr, cstat, ack, ts);
if (!bbr_is_in_probe_bw_state(bbr)) {
return;
}
switch (bbr->state) {
case NGTCP2_BBR_STATE_PROBE_BW_DOWN:
if (bbr_check_time_to_probe_bw(bbr, cstat, ts)) {
return;
}
if (bbr_check_time_to_cruise(bbr, cstat, ts)) {
bbr_start_probe_bw_cruise(bbr);
}
break;
case NGTCP2_BBR_STATE_PROBE_BW_CRUISE:
if (bbr_check_time_to_probe_bw(bbr, cstat, ts)) {
return;
}
break;
case NGTCP2_BBR_STATE_PROBE_BW_REFILL:
if (bbr->round_start) {
bbr->bw_probe_samples = 1;
bbr_start_probe_bw_up(bbr, cstat, ts);
}
break;
case NGTCP2_BBR_STATE_PROBE_BW_UP:
if (bbr_has_elapsed_in_phase(bbr, bbr->min_rtt, ts) &&
cstat->bytes_in_flight > bbr_inflight(bbr, cstat, bbr->max_bw, 125)) {
bbr_start_probe_bw_down(bbr, ts);
}
break;
default:
break;
}
}
static int bbr_check_time_to_cruise(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts) {
(void)ts;
if (cstat->bytes_in_flight > bbr_inflight_with_headroom(bbr, cstat)) {
return 0;
}
if (cstat->bytes_in_flight <= bbr_inflight(bbr, cstat, bbr->max_bw, 100)) {
return 1;
}
return 0;
}
static int bbr_has_elapsed_in_phase(ngtcp2_cc_bbr *bbr,
ngtcp2_duration interval,
ngtcp2_tstamp ts) {
return ts > bbr->cycle_stamp + interval;
}
static uint64_t bbr_inflight_with_headroom(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat) {
uint64_t headroom;
uint64_t mpcwnd;
if (bbr->inflight_hi == UINT64_MAX) {
return UINT64_MAX;
}
headroom = ngtcp2_max_uint64(cstat->max_tx_udp_payload_size,
bbr->inflight_hi * NGTCP2_BBR_HEADROOM_NUMER /
NGTCP2_BBR_HEADROOM_DENOM);
mpcwnd = min_pipe_cwnd(cstat->max_tx_udp_payload_size);
if (bbr->inflight_hi > headroom) {
return ngtcp2_max_uint64(bbr->inflight_hi - headroom, mpcwnd);
}
return mpcwnd;
}
static void bbr_raise_inflight_hi_slope(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat) {
uint64_t growth_this_round = cstat->max_tx_udp_payload_size
<< bbr->bw_probe_up_rounds;
bbr->bw_probe_up_rounds = ngtcp2_min_size(bbr->bw_probe_up_rounds + 1, 30);
bbr->probe_up_cnt = ngtcp2_max_uint64(cstat->cwnd / growth_this_round, 1) *
cstat->max_tx_udp_payload_size;
}
static void bbr_probe_inflight_hi_upward(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack) {
uint64_t delta;
if (!bbr->rst->is_cwnd_limited || cstat->cwnd < bbr->inflight_hi) {
return;
}
bbr->bw_probe_up_acks += ack->bytes_delivered;
if (bbr->bw_probe_up_acks >= bbr->probe_up_cnt) {
delta = bbr->bw_probe_up_acks / bbr->probe_up_cnt;
bbr->bw_probe_up_acks -= delta * bbr->probe_up_cnt;
bbr->inflight_hi += delta * cstat->max_tx_udp_payload_size;
}
if (bbr->round_start) {
bbr_raise_inflight_hi_slope(bbr, cstat);
}
}
static void bbr_adapt_upper_bounds(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack, ngtcp2_tstamp ts) {
if (bbr->ack_phase == NGTCP2_BBR_ACK_PHASE_ACKS_PROBE_STARTING &&
bbr->round_start) {
bbr->ack_phase = NGTCP2_BBR_ACK_PHASE_ACKS_PROBE_FEEDBACK;
}
if (bbr->ack_phase == NGTCP2_BBR_ACK_PHASE_ACKS_PROBE_STOPPING &&
bbr->round_start) {
if (bbr_is_in_probe_bw_state(bbr) && !bbr->rst->rs.is_app_limited) {
bbr_advance_max_bw_filter(bbr);
}
}
if (!bbr_check_inflight_too_high(bbr, cstat, ts)) {
/* bbr->bw_hi never be updated */
if (bbr->inflight_hi == UINT64_MAX /* || bbr->bw_hi == UINT64_MAX */) {
return;
}
if (bbr->rst->rs.tx_in_flight > bbr->inflight_hi) {
bbr->inflight_hi = bbr->rst->rs.tx_in_flight;
}
if (cstat->delivery_rate_sec > bbr->bw_hi) {
bbr->bw_hi = cstat->delivery_rate_sec;
}
if (bbr->state == NGTCP2_BBR_STATE_PROBE_BW_UP) {
bbr_probe_inflight_hi_upward(bbr, cstat, ack);
}
}
}
static int bbr_check_time_to_probe_bw(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts) {
if (bbr_has_elapsed_in_phase(bbr, bbr->bw_probe_wait, ts) ||
bbr_is_reno_coexistence_probe_time(bbr, cstat)) {
bbr_start_probe_bw_refill(bbr);
return 1;
}
return 0;
}
static void bbr_pick_probe_wait(ngtcp2_cc_bbr *bbr) {
uint8_t rand;
bbr->rand(&rand, 1, &bbr->rand_ctx);
bbr->rounds_since_bw_probe = (uint64_t)(rand * 2 / 256);
bbr->rand(&rand, 1, &bbr->rand_ctx);
bbr->bw_probe_wait =
2 * NGTCP2_SECONDS + (ngtcp2_tstamp)(NGTCP2_SECONDS * rand / 255);
}
static int bbr_is_reno_coexistence_probe_time(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat) {
uint64_t reno_rounds =
bbr_target_inflight(bbr, cstat) / cstat->max_tx_udp_payload_size;
return bbr->rounds_since_bw_probe >= ngtcp2_min(reno_rounds, 63);
}
static uint64_t bbr_target_inflight(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat) {
uint64_t bdp = bbr_inflight(bbr, cstat, bbr->bw, 100);
return ngtcp2_min(bdp, cstat->cwnd);
}
static int bbr_check_inflight_too_high(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts) {
if (is_inflight_too_high(&bbr->rst->rs)) {
if (bbr->bw_probe_samples) {
bbr_handle_inflight_too_high(bbr, cstat, &bbr->rst->rs, ts);
}
return 1;
}
return 0;
}
static int is_inflight_too_high(const ngtcp2_rs *rs) {
return rs->lost * NGTCP2_BBR_LOSS_THRESH_DENOM >
rs->tx_in_flight * NGTCP2_BBR_LOSS_THRESH_NUMER;
}
static void bbr_handle_inflight_too_high(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
const ngtcp2_rs *rs,
ngtcp2_tstamp ts) {
bbr->bw_probe_samples = 0;
if (!rs->is_app_limited) {
bbr->inflight_hi = ngtcp2_max_uint64(
rs->tx_in_flight, bbr_target_inflight(bbr, cstat) *
NGTCP2_BBR_BETA_NUMER / NGTCP2_BBR_BETA_DENOM);
}
if (bbr->state == NGTCP2_BBR_STATE_PROBE_BW_UP) {
bbr_start_probe_bw_down(bbr, ts);
}
}
static void bbr_handle_lost_packet(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
const ngtcp2_cc_pkt *pkt, ngtcp2_tstamp ts) {
ngtcp2_rs rs = {0};
if (!bbr->bw_probe_samples) {
return;
}
rs.tx_in_flight = pkt->tx_in_flight;
/* bbr->rst->lost is not incremented for pkt yet */
rs.lost = bbr->rst->lost + pkt->pktlen - pkt->lost;
rs.is_app_limited = pkt->is_app_limited;
if (is_inflight_too_high(&rs)) {
rs.tx_in_flight = bbr_inflight_hi_from_lost_packet(bbr, &rs, pkt);
bbr_handle_inflight_too_high(bbr, cstat, &rs, ts);
}
}
static uint64_t bbr_inflight_hi_from_lost_packet(ngtcp2_cc_bbr *bbr,
const ngtcp2_rs *rs,
const ngtcp2_cc_pkt *pkt) {
uint64_t inflight_prev, lost_prev, lost_prefix;
(void)bbr;
assert(rs->tx_in_flight >= pkt->pktlen);
inflight_prev = rs->tx_in_flight - pkt->pktlen;
assert(rs->lost >= pkt->pktlen);
lost_prev = rs->lost - pkt->pktlen;
if (inflight_prev * NGTCP2_BBR_LOSS_THRESH_NUMER <
lost_prev * NGTCP2_BBR_LOSS_THRESH_DENOM) {
return inflight_prev;
}
lost_prefix = (inflight_prev * NGTCP2_BBR_LOSS_THRESH_NUMER -
lost_prev * NGTCP2_BBR_LOSS_THRESH_DENOM) /
(NGTCP2_BBR_LOSS_THRESH_DENOM - NGTCP2_BBR_LOSS_THRESH_NUMER);
return inflight_prev + lost_prefix;
}
static void bbr_update_min_rtt(ngtcp2_cc_bbr *bbr, const ngtcp2_cc_ack *ack,
ngtcp2_tstamp ts) {
int min_rtt_expired;
bbr->probe_rtt_expired =
ts > bbr->probe_rtt_min_stamp + NGTCP2_BBR_PROBE_RTT_INTERVAL;
if (ack->rtt != UINT64_MAX &&
(ack->rtt < bbr->probe_rtt_min_delay || bbr->probe_rtt_expired)) {
bbr->probe_rtt_min_delay = ack->rtt;
bbr->probe_rtt_min_stamp = ts;
}
min_rtt_expired = ts > bbr->min_rtt_stamp + NGTCP2_BBR_MIN_RTT_FILTERLEN;
if (bbr->probe_rtt_min_delay < bbr->min_rtt || min_rtt_expired) {
bbr->min_rtt = bbr->probe_rtt_min_delay;
bbr->min_rtt_stamp = bbr->probe_rtt_min_stamp;
ngtcp2_log_info(bbr->cc.log, NGTCP2_LOG_EVENT_CCA,
"bbr update min_rtt=%" PRIu64, bbr->min_rtt);
}
}
static void bbr_check_probe_rtt(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts) {
if (bbr->state != NGTCP2_BBR_STATE_PROBE_RTT && bbr->probe_rtt_expired &&
!bbr->idle_restart) {
bbr_enter_probe_rtt(bbr);
bbr_save_cwnd(bbr, cstat);
bbr->probe_rtt_done_stamp = UINT64_MAX;
bbr->ack_phase = NGTCP2_BBR_ACK_PHASE_ACKS_PROBE_STOPPING;
bbr_start_round(bbr);
}
if (bbr->state == NGTCP2_BBR_STATE_PROBE_RTT) {
bbr_handle_probe_rtt(bbr, cstat, ts);
}
if (bbr->rst->rs.delivered) {
bbr->idle_restart = 0;
}
}
static void bbr_enter_probe_rtt(ngtcp2_cc_bbr *bbr) {
ngtcp2_log_info(bbr->cc.log, NGTCP2_LOG_EVENT_CCA, "bbr enter ProbeRTT");
bbr->state = NGTCP2_BBR_STATE_PROBE_RTT;
bbr->pacing_gain_h = 100;
bbr->cwnd_gain_h = NGTCP2_BBR_PROBE_RTT_CWND_GAIN_H;
}
static void bbr_handle_probe_rtt(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts) {
bbr_mark_connection_app_limited(bbr, cstat);
if (bbr->probe_rtt_done_stamp == UINT64_MAX &&
cstat->bytes_in_flight <= bbr_probe_rtt_cwnd(bbr, cstat)) {
bbr->probe_rtt_done_stamp = ts + NGTCP2_BBR_PROBE_RTT_DURATION;
bbr->probe_rtt_round_done = 0;
bbr_start_round(bbr);
return;
}
if (bbr->probe_rtt_done_stamp != UINT64_MAX) {
if (bbr->round_start) {
bbr->probe_rtt_round_done = 1;
}
if (bbr->probe_rtt_round_done) {
bbr_check_probe_rtt_done(bbr, cstat, ts);
}
}
}
static void bbr_check_probe_rtt_done(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts) {
if (bbr->probe_rtt_done_stamp != UINT64_MAX &&
ts > bbr->probe_rtt_done_stamp) {
bbr->probe_rtt_min_stamp = ts;
bbr_restore_cwnd(bbr, cstat);
bbr_exit_probe_rtt(bbr, ts);
}
}
static void bbr_mark_connection_app_limited(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat) {
uint64_t app_limited = bbr->rst->delivered + cstat->bytes_in_flight;
if (app_limited) {
bbr->rst->app_limited = app_limited;
} else {
bbr->rst->app_limited = cstat->max_tx_udp_payload_size;
}
}
static void bbr_exit_probe_rtt(ngtcp2_cc_bbr *bbr, ngtcp2_tstamp ts) {
bbr_reset_lower_bounds(bbr);
if (bbr->filled_pipe) {
bbr_start_probe_bw_down(bbr, ts);
bbr_start_probe_bw_cruise(bbr);
} else {
bbr_enter_startup(bbr);
}
}
static void bbr_handle_restart_from_idle(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts) {
if (cstat->bytes_in_flight == 0 && bbr->rst->app_limited) {
ngtcp2_log_info(bbr->cc.log, NGTCP2_LOG_EVENT_CCA, "bbr restart from idle");
bbr->idle_restart = 1;
bbr->extra_acked_interval_start = ts;
if (bbr_is_in_probe_bw_state(bbr)) {
bbr_set_pacing_rate_with_gain(bbr, cstat, 100);
} else if (bbr->state == NGTCP2_BBR_STATE_PROBE_RTT) {
bbr_check_probe_rtt_done(bbr, cstat, ts);
}
}
}
static uint64_t bbr_bdp_multiple(ngtcp2_cc_bbr *bbr, uint64_t bw,
uint64_t gain_h) {
uint64_t bdp;
if (bbr->min_rtt == UINT64_MAX) {
return bbr->initial_cwnd;
}
bdp = bw * bbr->min_rtt / NGTCP2_SECONDS;
return (uint64_t)(bdp * gain_h / 100);
}
static uint64_t min_pipe_cwnd(size_t max_udp_payload_size) {
return max_udp_payload_size * 4;
}
static uint64_t bbr_quantization_budget(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
uint64_t inflight) {
bbr_update_offload_budget(bbr, cstat);
inflight = ngtcp2_max(inflight, bbr->offload_budget);
inflight = ngtcp2_max_uint64(inflight,
min_pipe_cwnd(cstat->max_tx_udp_payload_size));
if (bbr->state == NGTCP2_BBR_STATE_PROBE_BW_UP) {
inflight += 2 * cstat->max_tx_udp_payload_size;
}
return inflight;
}
static uint64_t bbr_inflight(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
uint64_t bw, uint64_t gain_h) {
uint64_t inflight = bbr_bdp_multiple(bbr, bw, gain_h);
return bbr_quantization_budget(bbr, cstat, inflight);
}
static void bbr_update_max_inflight(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat) {
uint64_t inflight;
/* Not documented */
/* bbr_update_aggregation_budget(bbr); */
inflight =
bbr_bdp_multiple(bbr, bbr->bw, bbr->cwnd_gain_h) + bbr->extra_acked;
bbr->max_inflight = bbr_quantization_budget(bbr, cstat, inflight);
}
static void bbr_update_offload_budget(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat) {
bbr->offload_budget = 3 * cstat->send_quantum;
}
static void bbr_advance_max_bw_filter(ngtcp2_cc_bbr *bbr) {
++bbr->cycle_count;
}
static void bbr_modulate_cwnd_for_recovery(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack) {
if (ack->bytes_lost > 0) {
if (cstat->cwnd > ack->bytes_lost) {
cstat->cwnd -= ack->bytes_lost;
cstat->cwnd =
ngtcp2_max_uint64(cstat->cwnd, 2 * cstat->max_tx_udp_payload_size);
} else {
cstat->cwnd = 2 * cstat->max_tx_udp_payload_size;
}
}
if (bbr->packet_conservation) {
cstat->cwnd = ngtcp2_max_uint64(cstat->cwnd, cstat->bytes_in_flight +
ack->bytes_delivered);
}
}
static void bbr_save_cwnd(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat) {
if (!bbr->in_loss_recovery && bbr->state != NGTCP2_BBR_STATE_PROBE_RTT) {
bbr->prior_cwnd = cstat->cwnd;
return;
}
bbr->prior_cwnd = ngtcp2_max(bbr->prior_cwnd, cstat->cwnd);
}
static void bbr_restore_cwnd(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat) {
cstat->cwnd = ngtcp2_max(cstat->cwnd, bbr->prior_cwnd);
}
static uint64_t bbr_probe_rtt_cwnd(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat) {
uint64_t probe_rtt_cwnd =
bbr_bdp_multiple(bbr, bbr->bw, NGTCP2_BBR_PROBE_RTT_CWND_GAIN_H);
uint64_t mpcwnd = min_pipe_cwnd(cstat->max_tx_udp_payload_size);
return ngtcp2_max(probe_rtt_cwnd, mpcwnd);
}
static void bbr_bound_cwnd_for_probe_rtt(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat) {
uint64_t probe_rtt_cwnd;
if (bbr->state == NGTCP2_BBR_STATE_PROBE_RTT) {
probe_rtt_cwnd = bbr_probe_rtt_cwnd(bbr, cstat);
cstat->cwnd = ngtcp2_min(cstat->cwnd, probe_rtt_cwnd);
}
}
static void bbr_set_cwnd(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack) {
uint64_t mpcwnd;
bbr_update_max_inflight(bbr, cstat);
bbr_modulate_cwnd_for_recovery(bbr, cstat, ack);
if (!bbr->packet_conservation) {
if (bbr->filled_pipe) {
cstat->cwnd += ack->bytes_delivered;
cstat->cwnd = ngtcp2_min(cstat->cwnd, bbr->max_inflight);
} else if (cstat->cwnd < bbr->max_inflight ||
bbr->rst->delivered < bbr->initial_cwnd) {
cstat->cwnd += ack->bytes_delivered;
}
mpcwnd = min_pipe_cwnd(cstat->max_tx_udp_payload_size);
cstat->cwnd = ngtcp2_max(cstat->cwnd, mpcwnd);
}
bbr_bound_cwnd_for_probe_rtt(bbr, cstat);
bbr_bound_cwnd_for_model(bbr, cstat);
}
static void bbr_bound_cwnd_for_model(ngtcp2_cc_bbr *bbr,
ngtcp2_conn_stat *cstat) {
uint64_t cap = UINT64_MAX;
uint64_t mpcwnd = min_pipe_cwnd(cstat->max_tx_udp_payload_size);
if (bbr_is_in_probe_bw_state(bbr) &&
bbr->state != NGTCP2_BBR_STATE_PROBE_BW_CRUISE) {
cap = bbr->inflight_hi;
} else if (bbr->state == NGTCP2_BBR_STATE_PROBE_RTT ||
bbr->state == NGTCP2_BBR_STATE_PROBE_BW_CRUISE) {
cap = bbr_inflight_with_headroom(bbr, cstat);
}
cap = ngtcp2_min(cap, bbr->inflight_lo);
cap = ngtcp2_max(cap, mpcwnd);
cstat->cwnd = ngtcp2_min(cstat->cwnd, cap);
}
static void bbr_set_send_quantum(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat) {
size_t floor, send_quantum;
(void)bbr;
if (cstat->pacing_interval > (NGTCP2_SECONDS * 8 * 10 / 12) >> 20) {
floor = cstat->max_tx_udp_payload_size;
} else {
floor = 2 * cstat->max_tx_udp_payload_size;
}
if (cstat->pacing_interval) {
send_quantum = (size_t)(NGTCP2_MILLISECONDS / cstat->pacing_interval);
send_quantum = ngtcp2_min(send_quantum, 64 * 1024);
} else {
send_quantum = 64 * 1024;
}
cstat->send_quantum = ngtcp2_max(send_quantum, floor);
}
static int in_congestion_recovery(const ngtcp2_conn_stat *cstat,
ngtcp2_tstamp sent_time) {
return cstat->congestion_recovery_start_ts != UINT64_MAX &&
sent_time <= cstat->congestion_recovery_start_ts;
}
static void bbr_handle_recovery(ngtcp2_cc_bbr *bbr, ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack, ngtcp2_tstamp ts) {
if (bbr->in_loss_recovery) {
if (ts - cstat->congestion_recovery_start_ts >= cstat->smoothed_rtt) {
bbr->packet_conservation = 0;
}
if (ack->largest_pkt_sent_ts != UINT64_MAX &&
!in_congestion_recovery(cstat, ack->largest_pkt_sent_ts)) {
bbr->in_loss_recovery = 0;
bbr->packet_conservation = 0;
bbr_restore_cwnd(bbr, cstat);
}
return;
}
if (bbr->congestion_recovery_start_ts != UINT64_MAX) {
bbr->in_loss_recovery = 1;
bbr_save_cwnd(bbr, cstat);
cstat->cwnd =
cstat->bytes_in_flight +
ngtcp2_max(ack->bytes_delivered, cstat->max_tx_udp_payload_size);
cstat->congestion_recovery_start_ts = bbr->congestion_recovery_start_ts;
bbr->congestion_recovery_start_ts = UINT64_MAX;
bbr->packet_conservation = 1;
bbr->congestion_recovery_next_round_delivered = bbr->rst->delivered;
bbr->prior_inflight_hi = bbr->inflight_hi;
bbr->prior_inflight_lo = bbr->inflight_lo;
bbr->prior_bw_lo = bbr->bw_lo;
}
}
static void bbr_cc_on_pkt_lost(ngtcp2_cc *cc, ngtcp2_conn_stat *cstat,
const ngtcp2_cc_pkt *pkt, ngtcp2_tstamp ts) {
ngtcp2_cc_bbr *bbr = ngtcp2_struct_of(cc, ngtcp2_cc_bbr, cc);
bbr_update_on_loss(bbr, cstat, pkt, ts);
}
static void bbr_cc_congestion_event(ngtcp2_cc *cc, ngtcp2_conn_stat *cstat,
ngtcp2_tstamp sent_ts, ngtcp2_tstamp ts) {
ngtcp2_cc_bbr *bbr = ngtcp2_struct_of(cc, ngtcp2_cc_bbr, cc);
if (bbr->in_loss_recovery ||
bbr->congestion_recovery_start_ts != UINT64_MAX ||
in_congestion_recovery(cstat, sent_ts)) {
return;
}
bbr->congestion_recovery_start_ts = ts;
}
static void bbr_cc_on_spurious_congestion(ngtcp2_cc *cc,
ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts) {
ngtcp2_cc_bbr *bbr = ngtcp2_struct_of(cc, ngtcp2_cc_bbr, cc);
(void)ts;
bbr->congestion_recovery_start_ts = UINT64_MAX;
cstat->congestion_recovery_start_ts = UINT64_MAX;
if (bbr->in_loss_recovery) {
bbr->in_loss_recovery = 0;
bbr->packet_conservation = 0;
bbr_restore_cwnd(bbr, cstat);
bbr->full_bw_count = 0;
bbr->loss_in_round = 0;
bbr->inflight_lo = ngtcp2_max(bbr->inflight_lo, bbr->prior_inflight_lo);
bbr->inflight_hi = ngtcp2_max(bbr->inflight_hi, bbr->prior_inflight_hi);
bbr->bw_lo = ngtcp2_max(bbr->bw_lo, bbr->prior_bw_lo);
}
}
static void bbr_cc_on_persistent_congestion(ngtcp2_cc *cc,
ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts) {
ngtcp2_cc_bbr *bbr = ngtcp2_struct_of(cc, ngtcp2_cc_bbr, cc);
(void)ts;
cstat->congestion_recovery_start_ts = UINT64_MAX;
bbr->congestion_recovery_start_ts = UINT64_MAX;
bbr->in_loss_recovery = 0;
bbr->packet_conservation = 0;
bbr_save_cwnd(bbr, cstat);
cstat->cwnd = cstat->bytes_in_flight + cstat->max_tx_udp_payload_size;
cstat->cwnd = ngtcp2_max_uint64(
cstat->cwnd, min_pipe_cwnd(cstat->max_tx_udp_payload_size));
}
static void bbr_cc_on_ack_recv(ngtcp2_cc *cc, ngtcp2_conn_stat *cstat,
const ngtcp2_cc_ack *ack, ngtcp2_tstamp ts) {
ngtcp2_cc_bbr *bbr = ngtcp2_struct_of(cc, ngtcp2_cc_bbr, cc);
bbr_handle_recovery(bbr, cstat, ack, ts);
bbr_update_on_ack(bbr, cstat, ack, ts);
}
static void bbr_cc_on_pkt_sent(ngtcp2_cc *cc, ngtcp2_conn_stat *cstat,
const ngtcp2_cc_pkt *pkt) {
ngtcp2_cc_bbr *bbr = ngtcp2_struct_of(cc, ngtcp2_cc_bbr, cc);
bbr_on_transmit(bbr, cstat, pkt->sent_ts);
}
static void bbr_cc_reset(ngtcp2_cc *cc, ngtcp2_conn_stat *cstat,
ngtcp2_tstamp ts) {
ngtcp2_cc_bbr *bbr = ngtcp2_struct_of(cc, ngtcp2_cc_bbr, cc);
bbr_on_init(bbr, cstat, ts);
}
void ngtcp2_cc_bbr_init(ngtcp2_cc_bbr *bbr, ngtcp2_log *log,
ngtcp2_conn_stat *cstat, ngtcp2_rst *rst,
ngtcp2_tstamp initial_ts, ngtcp2_rand rand,
const ngtcp2_rand_ctx *rand_ctx) {
memset(bbr, 0, sizeof(*bbr));
bbr->cc.log = log;
bbr->cc.on_pkt_lost = bbr_cc_on_pkt_lost;
bbr->cc.congestion_event = bbr_cc_congestion_event;
bbr->cc.on_spurious_congestion = bbr_cc_on_spurious_congestion;
bbr->cc.on_persistent_congestion = bbr_cc_on_persistent_congestion;
bbr->cc.on_ack_recv = bbr_cc_on_ack_recv;
bbr->cc.on_pkt_sent = bbr_cc_on_pkt_sent;
bbr->cc.reset = bbr_cc_reset;
bbr->rst = rst;
bbr->rand = rand;
bbr->rand_ctx = *rand_ctx;
bbr->initial_cwnd = cstat->cwnd;
bbr_on_init(bbr, cstat, initial_ts);
}
|