rwebtransport 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/Cargo.lock +666 -0
  2. package/Cargo.toml +31 -0
  3. package/LICENSE +202 -0
  4. package/NOTICE +41 -0
  5. package/README.md +550 -0
  6. package/crates/echo-server/Cargo.toml +20 -0
  7. package/crates/echo-server/src/main.rs +666 -0
  8. package/crates/native/Cargo.toml +23 -0
  9. package/crates/native/src/config.rs +129 -0
  10. package/crates/native/src/driver.rs +463 -0
  11. package/crates/native/src/lib.rs +821 -0
  12. package/crates/native/src/server.rs +718 -0
  13. package/crates/native/src/session.rs +1191 -0
  14. package/crates/native/src/tls.rs +96 -0
  15. package/crates/wtcore/Cargo.toml +15 -0
  16. package/crates/wtcore/src/h3.rs +226 -0
  17. package/crates/wtcore/src/lib.rs +9 -0
  18. package/dist/datagrams.d.ts +16 -0
  19. package/dist/errors.d.ts +10 -0
  20. package/dist/index.d.ts +7 -0
  21. package/dist/index.js +1512 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/index.mjs +1473 -0
  24. package/dist/index.mjs.map +1 -0
  25. package/dist/loader.d.ts +2 -0
  26. package/dist/native.d.ts +226 -0
  27. package/dist/server.d.ts +44 -0
  28. package/dist/streams.d.ts +14 -0
  29. package/dist/types.d.ts +24 -0
  30. package/dist/webtransport.d.ts +20 -0
  31. package/package.json +79 -0
  32. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  33. package/scripts/bench.mjs +180 -0
  34. package/scripts/build.js +67 -0
  35. package/scripts/postinstall.js +67 -0
  36. package/scripts/setup-unix.sh +129 -0
  37. package/scripts/setup-windows.ps1 +229 -0
  38. package/vendor/quiche/AGENTS.md +75 -0
  39. package/vendor/quiche/COPYING +23 -0
  40. package/vendor/quiche/Cargo.lock +1212 -0
  41. package/vendor/quiche/Cargo.toml +215 -0
  42. package/vendor/quiche/README.md +466 -0
  43. package/vendor/quiche/examples/Makefile +46 -0
  44. package/vendor/quiche/examples/README.md +20 -0
  45. package/vendor/quiche/examples/cert-big.crt +90 -0
  46. package/vendor/quiche/examples/cert.crt +18 -0
  47. package/vendor/quiche/examples/cert.key +28 -0
  48. package/vendor/quiche/examples/client.c +343 -0
  49. package/vendor/quiche/examples/client.rs +281 -0
  50. package/vendor/quiche/examples/gen-certs.sh +15 -0
  51. package/vendor/quiche/examples/http3-client.c +473 -0
  52. package/vendor/quiche/examples/http3-client.rs +349 -0
  53. package/vendor/quiche/examples/http3-server.c +614 -0
  54. package/vendor/quiche/examples/http3-server.rs +680 -0
  55. package/vendor/quiche/examples/qpack-decode.rs +87 -0
  56. package/vendor/quiche/examples/qpack-encode.rs +88 -0
  57. package/vendor/quiche/examples/rootca.crt +21 -0
  58. package/vendor/quiche/examples/server.c +529 -0
  59. package/vendor/quiche/examples/server.rs +529 -0
  60. package/vendor/quiche/include/quiche.h +1282 -0
  61. package/vendor/quiche/quiche.svg +1 -0
  62. package/vendor/quiche/src/buffers.rs +105 -0
  63. package/vendor/quiche/src/build.rs +66 -0
  64. package/vendor/quiche/src/cid.rs +1079 -0
  65. package/vendor/quiche/src/crypto/boringssl.rs +373 -0
  66. package/vendor/quiche/src/crypto/mod.rs +671 -0
  67. package/vendor/quiche/src/dgram.rs +118 -0
  68. package/vendor/quiche/src/error.rs +263 -0
  69. package/vendor/quiche/src/ffi.rs +2290 -0
  70. package/vendor/quiche/src/flowcontrol.rs +246 -0
  71. package/vendor/quiche/src/frame.rs +2172 -0
  72. package/vendor/quiche/src/h3/AGENTS.md +54 -0
  73. package/vendor/quiche/src/h3/ffi.rs +442 -0
  74. package/vendor/quiche/src/h3/frame.rs +1404 -0
  75. package/vendor/quiche/src/h3/mod.rs +8223 -0
  76. package/vendor/quiche/src/h3/qpack/decoder.rs +288 -0
  77. package/vendor/quiche/src/h3/qpack/encoder.rs +235 -0
  78. package/vendor/quiche/src/h3/qpack/mod.rs +202 -0
  79. package/vendor/quiche/src/h3/qpack/static_table.rs +323 -0
  80. package/vendor/quiche/src/h3/stream.rs +1595 -0
  81. package/vendor/quiche/src/lib.rs +9556 -0
  82. package/vendor/quiche/src/minmax.rs +446 -0
  83. package/vendor/quiche/src/packet.rs +2334 -0
  84. package/vendor/quiche/src/path.rs +1374 -0
  85. package/vendor/quiche/src/pmtud.rs +652 -0
  86. package/vendor/quiche/src/rand.rs +64 -0
  87. package/vendor/quiche/src/range_buf.rs +186 -0
  88. package/vendor/quiche/src/ranges.rs +755 -0
  89. package/vendor/quiche/src/recovery/AGENTS.md +80 -0
  90. package/vendor/quiche/src/recovery/bandwidth.rs +565 -0
  91. package/vendor/quiche/src/recovery/bytes_in_flight.rs +179 -0
  92. package/vendor/quiche/src/recovery/congestion/cubic.rs +1038 -0
  93. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +514 -0
  94. package/vendor/quiche/src/recovery/congestion/hystart.rs +241 -0
  95. package/vendor/quiche/src/recovery/congestion/mod.rs +361 -0
  96. package/vendor/quiche/src/recovery/congestion/prr.rs +238 -0
  97. package/vendor/quiche/src/recovery/congestion/recovery.rs +1170 -0
  98. package/vendor/quiche/src/recovery/congestion/reno.rs +291 -0
  99. package/vendor/quiche/src/recovery/congestion/test_sender.rs +201 -0
  100. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +1947 -0
  101. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +158 -0
  102. package/vendor/quiche/src/recovery/gcongestion/bbr.rs +37 -0
  103. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +120 -0
  104. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +318 -0
  105. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +784 -0
  106. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +729 -0
  107. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +175 -0
  108. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +190 -0
  109. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +855 -0
  110. package/vendor/quiche/src/recovery/gcongestion/mod.rs +275 -0
  111. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +294 -0
  112. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +1384 -0
  113. package/vendor/quiche/src/recovery/mod.rs +3086 -0
  114. package/vendor/quiche/src/recovery/rtt.rs +152 -0
  115. package/vendor/quiche/src/stream/mod.rs +2210 -0
  116. package/vendor/quiche/src/stream/recv_buf.rs +1236 -0
  117. package/vendor/quiche/src/stream/send_buf.rs +849 -0
  118. package/vendor/quiche/src/test_utils.rs +681 -0
  119. package/vendor/quiche/src/tests.rs +12922 -0
  120. package/vendor/quiche/src/tls/boringssl.rs +375 -0
  121. package/vendor/quiche/src/tls/mod.rs +1245 -0
  122. package/vendor/quiche/src/transport_params.rs +628 -0
@@ -0,0 +1,158 @@
1
+ // Copyright (c) 2016 The Chromium Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style license that can be
3
+ // found in the LICENSE file.
4
+
5
+ // Implements Kathleen Nichols' algorithm for tracking the minimum (or maximum)
6
+ // estimate of a stream of samples over some fixed time interval. (E.g.,
7
+ // the minimum RTT over the past five minutes.) The algorithm keeps track of
8
+ // the best, second best, and third best min (or max) estimates, maintaining an
9
+ // invariant that the measurement time of the n'th best >= n-1'th best.
10
+
11
+ // The algorithm works as follows. On a reset, all three estimates are set to
12
+ // the same sample. The second best estimate is then recorded in the second
13
+ // quarter of the window, and a third best estimate is recorded in the second
14
+ // half of the window, bounding the worst case error when the true min is
15
+ // monotonically increasing (or true max is monotonically decreasing) over the
16
+ // window.
17
+ //
18
+ // A new best sample replaces all three estimates, since the new best is lower
19
+ // (or higher) than everything else in the window and it is the most recent.
20
+ // The window thus effectively gets reset on every new min. The same property
21
+ // holds true for second best and third best estimates. Specifically, when a
22
+ // sample arrives that is better than the second best but not better than the
23
+ // best, it replaces the second and third best estimates but not the best
24
+ // estimate. Similarly, a sample that is better than the third best estimate
25
+ // but not the other estimates replaces only the third best estimate.
26
+ //
27
+ // Finally, when the best expires, it is replaced by the second best, which in
28
+ // turn is replaced by the third best. The newest sample replaces the third
29
+ // best.
30
+
31
+ use std::ops::Div;
32
+ use std::ops::Sub;
33
+
34
+ #[derive(Debug, Clone, Copy)]
35
+ struct Sample<T, I> {
36
+ sample: T,
37
+ time: I,
38
+ }
39
+
40
+ #[derive(Debug)]
41
+ pub struct WindowedFilter<T, I, D> {
42
+ window_length: D,
43
+ estimates: [Option<Sample<T, I>>; 3],
44
+ }
45
+
46
+ impl<T, I, D> WindowedFilter<T, I, D>
47
+ where
48
+ T: Ord + Copy,
49
+ I: Sub<I, Output = D> + Copy,
50
+ D: Ord + Div<usize, Output = D> + Copy,
51
+ {
52
+ pub fn new(window_length: D) -> Self {
53
+ WindowedFilter {
54
+ window_length,
55
+ estimates: [None, None, None],
56
+ }
57
+ }
58
+
59
+ pub fn reset(&mut self, new_sample: T, new_time: I) {
60
+ let sample = Some(Sample {
61
+ sample: new_sample,
62
+ time: new_time,
63
+ });
64
+
65
+ self.estimates = [sample, sample, sample];
66
+ }
67
+
68
+ pub fn get_best(&self) -> Option<T> {
69
+ self.estimates[0].as_ref().map(|e| e.sample)
70
+ }
71
+
72
+ pub fn get_second_best(&self) -> Option<T> {
73
+ self.estimates[1].as_ref().map(|e| e.sample)
74
+ }
75
+
76
+ pub fn get_third_best(&self) -> Option<T> {
77
+ self.estimates[2].as_ref().map(|e| e.sample)
78
+ }
79
+
80
+ pub fn clear(&mut self) {
81
+ self.estimates = [None, None, None];
82
+ }
83
+
84
+ pub fn update(&mut self, new_sample: T, new_time: I) {
85
+ // Reset all estimates if they have not yet been initialized, if new
86
+ // sample is a new best, or if the newest recorded estimate is too
87
+ // old.
88
+ if match &self.estimates[0] {
89
+ None => true,
90
+ Some(best) if new_sample > best.sample => true,
91
+ _ =>
92
+ new_time - self.estimates[2].as_ref().unwrap().time >
93
+ self.window_length,
94
+ } {
95
+ return self.reset(new_sample, new_time);
96
+ }
97
+
98
+ if new_sample > self.estimates[1].unwrap().sample {
99
+ self.estimates[1] = Some(Sample {
100
+ sample: new_sample,
101
+ time: new_time,
102
+ });
103
+ self.estimates[2] = self.estimates[1];
104
+ } else if new_sample > self.estimates[2].unwrap().sample {
105
+ self.estimates[2] = Some(Sample {
106
+ sample: new_sample,
107
+ time: new_time,
108
+ });
109
+ }
110
+
111
+ // Expire and update estimates as necessary.
112
+ if new_time - self.estimates[0].unwrap().time > self.window_length {
113
+ // The best estimate hasn't been updated for an entire window, so
114
+ // promote second and third best estimates.
115
+ self.estimates[0] = self.estimates[1];
116
+ self.estimates[1] = self.estimates[2];
117
+ self.estimates[2] = Some(Sample {
118
+ sample: new_sample,
119
+ time: new_time,
120
+ });
121
+ // Need to iterate one more time. Check if the new best estimate is
122
+ // outside the window as well, since it may also have been recorded a
123
+ // long time ago. Don't need to iterate once more since we cover that
124
+ // case at the beginning of the method.
125
+ if new_time - self.estimates[0].unwrap().time > self.window_length {
126
+ self.estimates[0] = self.estimates[1];
127
+ self.estimates[1] = self.estimates[2];
128
+ }
129
+ return;
130
+ }
131
+
132
+ if self.estimates[1].unwrap().sample == self.estimates[0].unwrap().sample &&
133
+ new_time - self.estimates[1].unwrap().time > self.window_length / 4
134
+ {
135
+ // A quarter of the window has passed without a better sample, so the
136
+ // second-best estimate is taken from the second quarter of the
137
+ // window.
138
+ self.estimates[1] = Some(Sample {
139
+ sample: new_sample,
140
+ time: new_time,
141
+ });
142
+ self.estimates[2] = self.estimates[1];
143
+ return;
144
+ }
145
+
146
+ if self.estimates[2].unwrap().sample == self.estimates[1].unwrap().sample &&
147
+ new_time - self.estimates[2].unwrap().time > self.window_length / 2
148
+ {
149
+ // We've passed a half of the window without a better estimate, so
150
+ // take a third-best estimate from the second half of the
151
+ // window.
152
+ self.estimates[2] = Some(Sample {
153
+ sample: new_sample,
154
+ time: new_time,
155
+ });
156
+ }
157
+ }
158
+ }
@@ -0,0 +1,37 @@
1
+ // Copyright (c) 2016 The Chromium Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style license that can be
3
+ // found in the LICENSE file.
4
+
5
+ // Copyright (C) 2023, Cloudflare, Inc.
6
+ // All rights reserved.
7
+ //
8
+ // Redistribution and use in source and binary forms, with or without
9
+ // modification, are permitted provided that the following conditions are
10
+ // met:
11
+ //
12
+ // * Redistributions of source code must retain the above copyright notice,
13
+ // this list of conditions and the following disclaimer.
14
+ //
15
+ // * Redistributions in binary form must reproduce the above copyright
16
+ // notice, this list of conditions and the following disclaimer in the
17
+ // documentation and/or other materials provided with the distribution.
18
+ //
19
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20
+ // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21
+ // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22
+ // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23
+ // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
+ // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
+ // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
+ // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
+ // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
+ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
+ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+
31
+ mod bandwidth_sampler;
32
+ mod windowed_filter;
33
+
34
+ pub use bandwidth_sampler::BandwidthSampler;
35
+ pub use bandwidth_sampler::SendTimeState;
36
+
37
+ use super::Acked;
@@ -0,0 +1,120 @@
1
+ // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style license that can be
3
+ // found in the LICENSE file.
4
+
5
+ // Copyright (C) 2023, Cloudflare, Inc.
6
+ // All rights reserved.
7
+ //
8
+ // Redistribution and use in source and binary forms, with or without
9
+ // modification, are permitted provided that the following conditions are
10
+ // met:
11
+ //
12
+ // * Redistributions of source code must retain the above copyright notice,
13
+ // this list of conditions and the following disclaimer.
14
+ //
15
+ // * Redistributions in binary form must reproduce the above copyright
16
+ // notice, this list of conditions and the following disclaimer in the
17
+ // documentation and/or other materials provided with the distribution.
18
+ //
19
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20
+ // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21
+ // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22
+ // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23
+ // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
+ // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
+ // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
+ // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
+ // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
+ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
+ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+
31
+ use std::time::Instant;
32
+
33
+ use crate::recovery::gcongestion::bbr2::Params;
34
+ use crate::recovery::gcongestion::Acked;
35
+ use crate::recovery::gcongestion::Lost;
36
+ use crate::recovery::RecoveryStats;
37
+
38
+ use super::mode::Cycle;
39
+ use super::mode::Mode;
40
+ use super::mode::ModeImpl;
41
+ use super::network_model::BBRv2NetworkModel;
42
+ use super::BBRv2CongestionEvent;
43
+ use super::Limits;
44
+
45
+ #[derive(Debug)]
46
+ pub(super) struct Drain {
47
+ pub(super) model: BBRv2NetworkModel,
48
+ pub(super) cycle: Cycle,
49
+ }
50
+
51
+ impl ModeImpl for Drain {
52
+ #[cfg(feature = "qlog")]
53
+ fn state_str(&self) -> &'static str {
54
+ "bbr_drain"
55
+ }
56
+
57
+ fn is_probing_for_bandwidth(&self) -> bool {
58
+ false
59
+ }
60
+
61
+ fn on_congestion_event(
62
+ mut self, _prior_in_flight: usize, event_time: Instant,
63
+ _acked_packets: &[Acked], _lost_packets: &[Lost],
64
+ congestion_event: &mut BBRv2CongestionEvent,
65
+ _target_bytes_inflight: usize, params: &Params,
66
+ _recovery_stats: &mut RecoveryStats, _cwnd: usize,
67
+ ) -> Mode {
68
+ self.model.set_pacing_gain(params.drain_pacing_gain);
69
+ // Only STARTUP can transition to DRAIN, both of them use the same cwnd
70
+ // gain.
71
+ self.model.set_cwnd_gain(params.drain_cwnd_gain);
72
+
73
+ let drain_target = self.drain_target();
74
+ if congestion_event.bytes_in_flight <= drain_target {
75
+ return self.into_probe_bw(
76
+ event_time,
77
+ Some(congestion_event),
78
+ params,
79
+ );
80
+ }
81
+
82
+ Mode::Drain(self)
83
+ }
84
+
85
+ fn get_cwnd_limits(&self, _params: &Params) -> Limits<usize> {
86
+ Limits {
87
+ lo: 0,
88
+ hi: self.model.inflight_lo(),
89
+ }
90
+ }
91
+
92
+ fn on_exit_quiescence(
93
+ self, _now: Instant, _quiescence_start_time: Instant, _params: &Params,
94
+ ) -> Mode {
95
+ Mode::Drain(self)
96
+ }
97
+
98
+ fn enter(
99
+ &mut self, _: Instant, _: Option<&BBRv2CongestionEvent>, _params: &Params,
100
+ ) {
101
+ }
102
+
103
+ fn leave(&mut self, _: Instant, _: Option<&BBRv2CongestionEvent>) {}
104
+ }
105
+
106
+ impl Drain {
107
+ fn into_probe_bw(
108
+ mut self, now: Instant, congestion_event: Option<&BBRv2CongestionEvent>,
109
+ params: &Params,
110
+ ) -> Mode {
111
+ self.leave(now, congestion_event);
112
+ let mut next_mode = Mode::probe_bw(self.model, self.cycle);
113
+ next_mode.enter(now, congestion_event, params);
114
+ next_mode
115
+ }
116
+
117
+ fn drain_target(&self) -> usize {
118
+ self.model.bdp0()
119
+ }
120
+ }
@@ -0,0 +1,318 @@
1
+ // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style license that can be
3
+ // found in the LICENSE file.
4
+
5
+ // Copyright (C) 2023, Cloudflare, Inc.
6
+ // All rights reserved.
7
+ //
8
+ // Redistribution and use in source and binary forms, with or without
9
+ // modification, are permitted provided that the following conditions are
10
+ // met:
11
+ //
12
+ // * Redistributions of source code must retain the above copyright notice,
13
+ // this list of conditions and the following disclaimer.
14
+ //
15
+ // * Redistributions in binary form must reproduce the above copyright
16
+ // notice, this list of conditions and the following disclaimer in the
17
+ // documentation and/or other materials provided with the distribution.
18
+ //
19
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20
+ // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21
+ // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22
+ // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23
+ // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
+ // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
+ // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
+ // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
+ // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
+ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
+ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+
31
+ use std::fmt::Debug;
32
+ use std::time::Duration;
33
+ use std::time::Instant;
34
+
35
+ use crate::recovery::gcongestion::bbr2::Params;
36
+ use crate::recovery::gcongestion::Lost;
37
+ use crate::recovery::RecoveryStats;
38
+
39
+ use super::drain::Drain;
40
+ use super::network_model::BBRv2NetworkModel;
41
+ use super::probe_bw::ProbeBW;
42
+ use super::probe_rtt::ProbeRTT;
43
+ use super::startup::Startup;
44
+ use super::Acked;
45
+ use super::BBRv2CongestionEvent;
46
+ use super::Limits;
47
+
48
+ #[derive(Debug, Default, PartialEq)]
49
+ pub(super) enum CyclePhase {
50
+ #[default]
51
+ NotStarted,
52
+ Up,
53
+ Down,
54
+ Cruise,
55
+ Refill,
56
+ }
57
+
58
+ impl CyclePhase {
59
+ pub(super) fn pacing_gain(&self, params: &Params) -> f32 {
60
+ match self {
61
+ CyclePhase::Up => params.probe_bw_probe_up_pacing_gain,
62
+ CyclePhase::Down => params.probe_bw_probe_down_pacing_gain,
63
+ _ => params.probe_bw_default_pacing_gain,
64
+ }
65
+ }
66
+
67
+ pub(super) fn cwnd_gain(&self, params: &Params) -> f32 {
68
+ match self {
69
+ CyclePhase::Up => params.probe_bw_up_cwnd_gain,
70
+ _ => params.probe_bw_cwnd_gain,
71
+ }
72
+ }
73
+ }
74
+
75
+ #[derive(Debug)]
76
+ pub(super) struct Cycle {
77
+ pub(super) start_time: Instant,
78
+ pub(super) phase: CyclePhase,
79
+ pub(super) rounds_in_phase: usize,
80
+ pub(super) phase_start_time: Instant,
81
+ pub(super) rounds_since_probe: usize,
82
+ pub(super) probe_wait_time: Option<Duration>,
83
+ pub(super) probe_up_rounds: usize,
84
+ pub(super) probe_up_bytes: Option<usize>,
85
+ pub(super) probe_up_acked: usize,
86
+ pub(super) probe_up_app_limited_since_inflight_hi_limited: bool,
87
+ // Whether max bandwidth filter window has advanced in this cycle. It is
88
+ // advanced once per cycle.
89
+ pub(super) has_advanced_max_bw: bool,
90
+ pub(super) is_sample_from_probing: bool,
91
+
92
+ pub(super) last_cycle_probed_too_high: bool,
93
+ pub(super) last_cycle_stopped_risky_probe: bool,
94
+ }
95
+
96
+ impl Default for Cycle {
97
+ fn default() -> Self {
98
+ let now = Instant::now();
99
+
100
+ Cycle {
101
+ start_time: now,
102
+ phase_start_time: now,
103
+
104
+ phase: CyclePhase::NotStarted,
105
+ rounds_in_phase: 0,
106
+ rounds_since_probe: 0,
107
+ probe_wait_time: None,
108
+ probe_up_rounds: 0,
109
+ probe_up_bytes: None,
110
+ probe_up_acked: 0,
111
+ probe_up_app_limited_since_inflight_hi_limited: false,
112
+ has_advanced_max_bw: false,
113
+ is_sample_from_probing: false,
114
+ last_cycle_probed_too_high: false,
115
+ last_cycle_stopped_risky_probe: false,
116
+ }
117
+ }
118
+ }
119
+
120
+ #[enum_dispatch::enum_dispatch]
121
+ pub(super) trait ModeImpl: Debug {
122
+ #[cfg(feature = "qlog")]
123
+ fn state_str(&self) -> &'static str;
124
+
125
+ fn enter(
126
+ &mut self, now: Instant, congestion_event: Option<&BBRv2CongestionEvent>,
127
+ params: &Params,
128
+ );
129
+
130
+ fn leave(
131
+ &mut self, now: Instant, congestion_event: Option<&BBRv2CongestionEvent>,
132
+ );
133
+
134
+ fn is_probing_for_bandwidth(&self) -> bool;
135
+
136
+ #[allow(clippy::too_many_arguments)]
137
+ fn on_congestion_event(
138
+ self, prior_in_flight: usize, event_time: Instant,
139
+ acked_packets: &[Acked], lost_packets: &[Lost],
140
+ congestion_event: &mut BBRv2CongestionEvent,
141
+ target_bytes_inflight: usize, params: &Params,
142
+ recovery_stats: &mut RecoveryStats, cwnd: usize,
143
+ ) -> Mode;
144
+
145
+ fn get_cwnd_limits(&self, params: &Params) -> Limits<usize>;
146
+
147
+ fn on_exit_quiescence(
148
+ self, now: Instant, quiescence_start_time: Instant, params: &Params,
149
+ ) -> Mode;
150
+ }
151
+
152
+ #[enum_dispatch::enum_dispatch(ModeImpl)]
153
+ #[derive(Debug)]
154
+ pub(super) enum Mode {
155
+ Startup(Startup),
156
+ Drain(Drain),
157
+ ProbeBW(ProbeBW),
158
+ ProbeRTT(ProbeRTT),
159
+ Placheolder(Placeholder),
160
+ }
161
+
162
+ impl Default for Mode {
163
+ fn default() -> Self {
164
+ Mode::Placheolder(Placeholder {})
165
+ }
166
+ }
167
+
168
+ impl Mode {
169
+ pub(super) fn startup(model: BBRv2NetworkModel) -> Self {
170
+ Mode::Startup(Startup { model })
171
+ }
172
+
173
+ pub(super) fn drain(model: BBRv2NetworkModel) -> Self {
174
+ Mode::Drain(Drain {
175
+ model,
176
+ cycle: Default::default(),
177
+ })
178
+ }
179
+
180
+ pub(super) fn probe_bw(model: BBRv2NetworkModel, cycle: Cycle) -> Self {
181
+ Mode::ProbeBW(ProbeBW { model, cycle })
182
+ }
183
+
184
+ pub(super) fn probe_rtt(model: BBRv2NetworkModel, cycle: Cycle) -> Self {
185
+ Mode::ProbeRTT(ProbeRTT::new(model, cycle))
186
+ }
187
+
188
+ #[allow(clippy::too_many_arguments)]
189
+ pub(super) fn do_on_congestion_event(
190
+ &mut self, prior_in_flight: usize, event_time: Instant,
191
+ acked_packets: &[Acked], lost_packets: &[Lost],
192
+ congestion_event: &mut BBRv2CongestionEvent,
193
+ target_bytes_inflight: usize, params: &Params,
194
+ recovery_stats: &mut RecoveryStats, cwnd: usize,
195
+ ) -> bool {
196
+ let mode_before = std::mem::discriminant(self);
197
+
198
+ *self = std::mem::take(self).on_congestion_event(
199
+ prior_in_flight,
200
+ event_time,
201
+ acked_packets,
202
+ lost_packets,
203
+ congestion_event,
204
+ target_bytes_inflight,
205
+ params,
206
+ recovery_stats,
207
+ cwnd,
208
+ );
209
+
210
+ let mode_after = std::mem::discriminant(self);
211
+
212
+ mode_before != mode_after
213
+ }
214
+
215
+ pub(super) fn do_on_exit_quiescence(
216
+ &mut self, now: Instant, quiescence_start_time: Instant, params: &Params,
217
+ ) {
218
+ *self = std::mem::take(self).on_exit_quiescence(
219
+ now,
220
+ quiescence_start_time,
221
+ params,
222
+ )
223
+ }
224
+
225
+ pub fn network_model(&self) -> &BBRv2NetworkModel {
226
+ match self {
227
+ Mode::Startup(Startup { model }) => model,
228
+ Mode::Drain(Drain { model, .. }) => model,
229
+ Mode::ProbeBW(ProbeBW { model, .. }) => model,
230
+ Mode::ProbeRTT(ProbeRTT { model, .. }) => model,
231
+ Mode::Placheolder(_) => unreachable!(),
232
+ }
233
+ }
234
+
235
+ pub fn network_model_mut(&mut self) -> &mut BBRv2NetworkModel {
236
+ match self {
237
+ Mode::Startup(Startup { model }) => model,
238
+ Mode::Drain(Drain { model, .. }) => model,
239
+ Mode::ProbeBW(ProbeBW { model, .. }) => model,
240
+ Mode::ProbeRTT(ProbeRTT { model, .. }) => model,
241
+ Mode::Placheolder(_) => unreachable!(),
242
+ }
243
+ }
244
+ }
245
+
246
+ #[derive(Debug, Default)]
247
+ pub(super) struct Placeholder {}
248
+
249
+ impl ModeImpl for Placeholder {
250
+ #[cfg(feature = "qlog")]
251
+ fn state_str(&self) -> &'static str {
252
+ unreachable!()
253
+ }
254
+
255
+ fn enter(
256
+ &mut self, _: Instant, _: Option<&BBRv2CongestionEvent>, _params: &Params,
257
+ ) {
258
+ unreachable!()
259
+ }
260
+
261
+ fn leave(&mut self, _: Instant, _: Option<&BBRv2CongestionEvent>) {
262
+ unreachable!()
263
+ }
264
+
265
+ fn is_probing_for_bandwidth(&self) -> bool {
266
+ unreachable!()
267
+ }
268
+
269
+ fn on_congestion_event(
270
+ self, _: usize, _: Instant, _: &[Acked], _: &[Lost],
271
+ _: &mut BBRv2CongestionEvent, _: usize, _params: &Params,
272
+ _recovery_stats: &mut RecoveryStats, _cwnd: usize,
273
+ ) -> Mode {
274
+ unreachable!()
275
+ }
276
+
277
+ fn get_cwnd_limits(&self, _params: &Params) -> Limits<usize> {
278
+ unreachable!()
279
+ }
280
+
281
+ fn on_exit_quiescence(
282
+ self, _: Instant, _: Instant, _params: &Params,
283
+ ) -> Mode {
284
+ unreachable!()
285
+ }
286
+ }
287
+
288
+ #[cfg(test)]
289
+ mod tests {
290
+ use super::*;
291
+ use crate::recovery::gcongestion::bbr2::DEFAULT_PARAMS;
292
+ use crate::BbrParams;
293
+
294
+ #[test]
295
+ fn cycle_params() {
296
+ let custom_bbr_settings = BbrParams {
297
+ probe_bw_up_cwnd_gain: Some(2.25),
298
+ probe_bw_cwnd_gain: Some(2.0),
299
+ ..Default::default()
300
+ };
301
+ let params = &DEFAULT_PARAMS.with_overrides(&custom_bbr_settings);
302
+
303
+ assert_eq!(CyclePhase::Up.pacing_gain(params), 1.25);
304
+ assert_eq!(CyclePhase::Up.cwnd_gain(params), 2.25);
305
+
306
+ assert_eq!(CyclePhase::Down.pacing_gain(params), 0.9);
307
+ assert_eq!(CyclePhase::Down.cwnd_gain(params), 2.0);
308
+
309
+ assert_eq!(CyclePhase::NotStarted.pacing_gain(params), 1.0);
310
+ assert_eq!(CyclePhase::NotStarted.cwnd_gain(params), 2.0);
311
+
312
+ assert_eq!(CyclePhase::Cruise.pacing_gain(params), 1.0);
313
+ assert_eq!(CyclePhase::Cruise.cwnd_gain(params), 2.0);
314
+
315
+ assert_eq!(CyclePhase::Refill.pacing_gain(params), 1.0);
316
+ assert_eq!(CyclePhase::Refill.cwnd_gain(params), 2.0);
317
+ }
318
+ }