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,241 @@
1
+ // Copyright (C) 2020, Cloudflare, Inc.
2
+ // All rights reserved.
3
+ //
4
+ // Redistribution and use in source and binary forms, with or without
5
+ // modification, are permitted provided that the following conditions are
6
+ // met:
7
+ //
8
+ // * Redistributions of source code must retain the above copyright notice,
9
+ // this list of conditions and the following disclaimer.
10
+ //
11
+ // * Redistributions in binary form must reproduce the above copyright
12
+ // notice, this list of conditions and the following disclaimer in the
13
+ // documentation and/or other materials provided with the distribution.
14
+ //
15
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16
+ // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17
+ // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18
+ // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19
+ // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
+ // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
+ // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
+ // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
+ // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+
27
+ //! HyStart++
28
+ //!
29
+ //! This implementation is based on the following I-D:
30
+ //!
31
+ //! <https://datatracker.ietf.org/doc/html/draft-ietf-tcpm-hystartplusplus-04>
32
+
33
+ use std::cmp;
34
+ use std::time::Duration;
35
+ use std::time::Instant;
36
+
37
+ use super::Acked;
38
+
39
+ /// Constants from I-D.
40
+ const MIN_RTT_THRESH: Duration = Duration::from_millis(4);
41
+
42
+ const MAX_RTT_THRESH: Duration = Duration::from_millis(16);
43
+
44
+ pub const N_RTT_SAMPLE: usize = 8;
45
+
46
+ pub const CSS_GROWTH_DIVISOR: usize = 4;
47
+
48
+ pub const CSS_ROUNDS: usize = 5;
49
+
50
+ #[derive(Default)]
51
+ pub struct Hystart {
52
+ enabled: bool,
53
+
54
+ window_end: Option<u64>,
55
+
56
+ last_round_min_rtt: Duration,
57
+
58
+ current_round_min_rtt: Duration,
59
+
60
+ css_baseline_min_rtt: Duration,
61
+
62
+ rtt_sample_count: usize,
63
+
64
+ css_start_time: Option<Instant>,
65
+
66
+ css_round_count: usize,
67
+ }
68
+
69
+ impl std::fmt::Debug for Hystart {
70
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
71
+ write!(f, "window_end={:?} ", self.window_end)?;
72
+ write!(f, "last_round_min_rtt={:?} ", self.last_round_min_rtt)?;
73
+ write!(f, "current_round_min_rtt={:?} ", self.current_round_min_rtt)?;
74
+ write!(f, "css_baseline_min_rtt={:?} ", self.css_baseline_min_rtt)?;
75
+ write!(f, "rtt_sample_count={:?} ", self.rtt_sample_count)?;
76
+ write!(f, "css_start_time={:?} ", self.css_start_time)?;
77
+ write!(f, "css_round_count={:?}", self.css_round_count)?;
78
+
79
+ Ok(())
80
+ }
81
+ }
82
+
83
+ impl Hystart {
84
+ pub fn new(enabled: bool) -> Self {
85
+ Self {
86
+ enabled,
87
+
88
+ last_round_min_rtt: Duration::MAX,
89
+
90
+ current_round_min_rtt: Duration::MAX,
91
+
92
+ css_baseline_min_rtt: Duration::MAX,
93
+
94
+ ..Default::default()
95
+ }
96
+ }
97
+
98
+ pub fn enabled(&self) -> bool {
99
+ self.enabled
100
+ }
101
+
102
+ pub fn css_start_time(&self) -> Option<Instant> {
103
+ self.css_start_time
104
+ }
105
+
106
+ pub fn in_css(&self) -> bool {
107
+ self.enabled && self.css_start_time().is_some()
108
+ }
109
+
110
+ pub fn start_round(&mut self, pkt_num: u64) {
111
+ if self.window_end.is_none() {
112
+ self.window_end = Some(pkt_num);
113
+
114
+ self.last_round_min_rtt = self.current_round_min_rtt;
115
+
116
+ self.current_round_min_rtt = Duration::MAX;
117
+
118
+ self.rtt_sample_count = 0;
119
+ }
120
+ }
121
+
122
+ // On receiving ACK. Returns true if need to enter Congestion Avoidance.
123
+ pub fn on_packet_acked(
124
+ &mut self, packet: &Acked, rtt: Duration, now: Instant,
125
+ ) -> bool {
126
+ if !self.enabled {
127
+ return false;
128
+ }
129
+
130
+ self.current_round_min_rtt = cmp::min(self.current_round_min_rtt, rtt);
131
+
132
+ self.rtt_sample_count += 1;
133
+
134
+ // Slow Start.
135
+ if self.css_start_time().is_none() {
136
+ if self.rtt_sample_count >= N_RTT_SAMPLE &&
137
+ self.current_round_min_rtt != Duration::MAX &&
138
+ self.last_round_min_rtt != Duration::MAX
139
+ {
140
+ // clamp(min_rtt_thresh, last_round_min_rtt/8,
141
+ // max_rtt_thresh)
142
+ let rtt_thresh =
143
+ cmp::max(self.last_round_min_rtt / 8, MIN_RTT_THRESH);
144
+ let rtt_thresh = cmp::min(rtt_thresh, MAX_RTT_THRESH);
145
+
146
+ // Check if we can exit to CSS.
147
+ if self.current_round_min_rtt >=
148
+ self.last_round_min_rtt.saturating_add(rtt_thresh)
149
+ {
150
+ self.css_baseline_min_rtt = self.current_round_min_rtt;
151
+ self.css_start_time = Some(now);
152
+ }
153
+ }
154
+ } else {
155
+ // Conservative Slow Start.
156
+ if self.rtt_sample_count >= N_RTT_SAMPLE {
157
+ self.rtt_sample_count = 0;
158
+
159
+ if self.current_round_min_rtt < self.css_baseline_min_rtt {
160
+ self.css_baseline_min_rtt = Duration::MAX;
161
+
162
+ // Back to Slow Start.
163
+ self.css_start_time = None;
164
+ self.css_round_count = 0;
165
+ }
166
+ }
167
+ }
168
+
169
+ // Check if we reached the end of the round.
170
+ if let Some(end_pkt_num) = self.window_end {
171
+ if packet.pkt_num >= end_pkt_num {
172
+ // Start of a new round.
173
+ self.window_end = None;
174
+
175
+ if self.css_start_time().is_some() {
176
+ self.css_round_count += 1;
177
+
178
+ // End of CSS - exit to congestion avoidance.
179
+ if self.css_round_count >= CSS_ROUNDS {
180
+ self.css_round_count = 0;
181
+ return true;
182
+ }
183
+ }
184
+ }
185
+ }
186
+
187
+ false
188
+ }
189
+
190
+ // Return a cwnd increment during CSS (Conservative Slow Start).
191
+ pub fn css_cwnd_inc(&self, pkt_size: usize) -> usize {
192
+ pkt_size / CSS_GROWTH_DIVISOR
193
+ }
194
+
195
+ // Exit HyStart++ when entering congestion avoidance.
196
+ pub fn congestion_event(&mut self) {
197
+ self.window_end = None;
198
+ self.css_start_time = None;
199
+ }
200
+ }
201
+
202
+ #[cfg(test)]
203
+ mod tests {
204
+ use super::*;
205
+
206
+ #[test]
207
+ fn start_round() {
208
+ let mut hspp = Hystart::default();
209
+ let pkt_num = 100;
210
+
211
+ hspp.start_round(pkt_num);
212
+
213
+ assert_eq!(hspp.window_end, Some(pkt_num));
214
+ assert_eq!(hspp.current_round_min_rtt, Duration::MAX);
215
+ }
216
+
217
+ #[test]
218
+ fn css_cwnd_inc() {
219
+ let hspp = Hystart::default();
220
+ let datagram_size = 1200;
221
+
222
+ let css_cwnd_inc = hspp.css_cwnd_inc(datagram_size);
223
+
224
+ assert_eq!(datagram_size / CSS_GROWTH_DIVISOR, css_cwnd_inc);
225
+ }
226
+
227
+ #[test]
228
+ fn congestion_event() {
229
+ let mut hspp = Hystart::default();
230
+ let pkt_num = 100;
231
+
232
+ hspp.start_round(pkt_num);
233
+
234
+ assert_eq!(hspp.window_end, Some(pkt_num));
235
+
236
+ // When moving into CA mode, window_end should be cleared.
237
+ hspp.congestion_event();
238
+
239
+ assert_eq!(hspp.window_end, None);
240
+ }
241
+ }
@@ -0,0 +1,361 @@
1
+ // Copyright (C) 2024, Cloudflare, Inc.
2
+ // All rights reserved.
3
+ //
4
+ // Redistribution and use in source and binary forms, with or without
5
+ // modification, are permitted provided that the following conditions are
6
+ // met:
7
+ //
8
+ // * Redistributions of source code must retain the above copyright notice,
9
+ // this list of conditions and the following disclaimer.
10
+ //
11
+ // * Redistributions in binary form must reproduce the above copyright
12
+ // notice, this list of conditions and the following disclaimer in the
13
+ // documentation and/or other materials provided with the distribution.
14
+ //
15
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16
+ // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17
+ // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18
+ // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19
+ // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
+ // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
+ // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
+ // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
+ // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+
27
+ use std::time::Instant;
28
+
29
+ use self::recovery::Acked;
30
+ use super::bandwidth::Bandwidth;
31
+ use super::RecoveryConfig;
32
+ use super::Sent;
33
+ use crate::recovery::rtt;
34
+ use crate::recovery::rtt::RttStats;
35
+ use crate::recovery::CongestionControlAlgorithm;
36
+ use crate::StartupExit;
37
+ use crate::StartupExitReason;
38
+
39
+ pub struct SsThresh {
40
+ // Current slow start threshold. Defaults to usize::MAX which
41
+ // indicates we're still in the initial slow start phase.
42
+ ssthresh: usize,
43
+
44
+ // Information about the slow start exit, if it already happened.
45
+ // Set on the first call to update().
46
+ startup_exit: Option<StartupExit>,
47
+ }
48
+
49
+ impl Default for SsThresh {
50
+ fn default() -> Self {
51
+ Self {
52
+ ssthresh: usize::MAX,
53
+ startup_exit: None,
54
+ }
55
+ }
56
+ }
57
+
58
+ impl SsThresh {
59
+ fn get(&self) -> usize {
60
+ self.ssthresh
61
+ }
62
+
63
+ fn startup_exit(&self) -> Option<StartupExit> {
64
+ self.startup_exit
65
+ }
66
+
67
+ fn update(&mut self, ssthresh: usize, in_css: bool) {
68
+ if self.startup_exit.is_none() {
69
+ let reason = if in_css {
70
+ // Exit happened in conservative slow start, attribute
71
+ // the exit to CSS.
72
+ StartupExitReason::ConservativeSlowStartRounds
73
+ } else {
74
+ // In normal slow start, attribute the exit to loss.
75
+ StartupExitReason::Loss
76
+ };
77
+ self.startup_exit = Some(StartupExit::new(ssthresh, None, reason));
78
+ }
79
+ self.ssthresh = ssthresh;
80
+ }
81
+ }
82
+
83
+ pub struct Congestion {
84
+ // Congestion control.
85
+ pub(crate) cc_ops: &'static CongestionControlOps,
86
+
87
+ cubic_state: cubic::State,
88
+
89
+ // HyStart++.
90
+ pub(crate) hystart: hystart::Hystart,
91
+
92
+ // RFC6937 PRR.
93
+ pub(crate) prr: prr::PRR,
94
+
95
+ // The maximum size of a data aggregate scheduled and
96
+ // transmitted together.
97
+ send_quantum: usize,
98
+
99
+ pub(crate) congestion_window: usize,
100
+
101
+ pub(crate) ssthresh: SsThresh,
102
+
103
+ bytes_acked_sl: usize,
104
+
105
+ bytes_acked_ca: usize,
106
+
107
+ pub(crate) congestion_recovery_start_time: Option<Instant>,
108
+
109
+ pub(crate) app_limited: bool,
110
+
111
+ pub(crate) delivery_rate: delivery_rate::Rate,
112
+
113
+ /// Initial congestion window size in terms of packet count.
114
+ pub(crate) initial_congestion_window_packets: usize,
115
+
116
+ max_datagram_size: usize,
117
+
118
+ pub(crate) lost_count: usize,
119
+
120
+ pub(crate) enable_cubic_idle_restart_fix: bool,
121
+ }
122
+
123
+ impl Congestion {
124
+ pub(crate) fn from_config(recovery_config: &RecoveryConfig) -> Self {
125
+ let initial_congestion_window = recovery_config.max_send_udp_payload_size *
126
+ recovery_config.initial_congestion_window_packets;
127
+
128
+ let mut cc = Congestion {
129
+ congestion_window: initial_congestion_window,
130
+
131
+ ssthresh: Default::default(),
132
+
133
+ bytes_acked_sl: 0,
134
+
135
+ bytes_acked_ca: 0,
136
+
137
+ congestion_recovery_start_time: None,
138
+
139
+ cc_ops: recovery_config.cc_algorithm.into(),
140
+
141
+ cubic_state: cubic::State::default(),
142
+
143
+ app_limited: false,
144
+
145
+ lost_count: 0,
146
+
147
+ initial_congestion_window_packets: recovery_config
148
+ .initial_congestion_window_packets,
149
+
150
+ max_datagram_size: recovery_config.max_send_udp_payload_size,
151
+
152
+ send_quantum: initial_congestion_window,
153
+
154
+ delivery_rate: delivery_rate::Rate::default(),
155
+
156
+ hystart: hystart::Hystart::new(recovery_config.hystart),
157
+
158
+ prr: prr::PRR::default(),
159
+
160
+ enable_cubic_idle_restart_fix: recovery_config
161
+ .enable_cubic_idle_restart_fix,
162
+ };
163
+
164
+ (cc.cc_ops.on_init)(&mut cc);
165
+
166
+ cc
167
+ }
168
+
169
+ pub(crate) fn in_congestion_recovery(&self, sent_time: Instant) -> bool {
170
+ match self.congestion_recovery_start_time {
171
+ Some(congestion_recovery_start_time) =>
172
+ sent_time <= congestion_recovery_start_time,
173
+
174
+ None => false,
175
+ }
176
+ }
177
+
178
+ /// The most recent data delivery rate estimate.
179
+ pub(crate) fn delivery_rate(&self) -> Bandwidth {
180
+ self.delivery_rate.sample_delivery_rate()
181
+ }
182
+
183
+ pub(crate) fn send_quantum(&self) -> usize {
184
+ self.send_quantum
185
+ }
186
+
187
+ pub(crate) fn congestion_window(&self) -> usize {
188
+ self.congestion_window
189
+ }
190
+
191
+ fn update_app_limited(&mut self, v: bool) {
192
+ self.app_limited = v;
193
+ }
194
+
195
+ #[allow(clippy::too_many_arguments)]
196
+ pub(crate) fn on_packet_sent(
197
+ &mut self, bytes_in_flight: usize, sent_bytes: usize, now: Instant,
198
+ pkt: &mut Sent, bytes_lost: u64, in_flight: bool,
199
+ ) {
200
+ if in_flight {
201
+ self.update_app_limited(
202
+ (bytes_in_flight + sent_bytes) < self.congestion_window,
203
+ );
204
+
205
+ (self.cc_ops.on_packet_sent)(self, sent_bytes, bytes_in_flight, now);
206
+
207
+ self.prr.on_packet_sent(sent_bytes);
208
+
209
+ // HyStart++: Start of the round in a slow start.
210
+ if self.hystart.enabled() &&
211
+ self.congestion_window < self.ssthresh.get()
212
+ {
213
+ self.hystart.start_round(pkt.pkt_num);
214
+ }
215
+ }
216
+
217
+ pkt.time_sent = now;
218
+
219
+ // bytes_in_flight is already updated. Use previous value.
220
+ self.delivery_rate
221
+ .on_packet_sent(pkt, bytes_in_flight, bytes_lost);
222
+ }
223
+
224
+ pub(crate) fn on_packets_acked(
225
+ &mut self, bytes_in_flight: usize, acked: &mut Vec<Acked>,
226
+ rtt_stats: &RttStats, now: Instant,
227
+ ) {
228
+ // Update delivery rate sample per acked packet.
229
+ for pkt in acked.iter() {
230
+ self.delivery_rate.update_rate_sample(pkt, now);
231
+ }
232
+
233
+ // Fill in a rate sample.
234
+ self.delivery_rate.generate_rate_sample(*rtt_stats.min_rtt);
235
+
236
+ // Call congestion control hooks.
237
+ (self.cc_ops.on_packets_acked)(
238
+ self,
239
+ bytes_in_flight,
240
+ acked,
241
+ now,
242
+ rtt_stats,
243
+ );
244
+ }
245
+ }
246
+
247
+ pub(crate) struct CongestionControlOps {
248
+ pub on_init: fn(r: &mut Congestion),
249
+
250
+ pub on_packet_sent: fn(
251
+ r: &mut Congestion,
252
+ sent_bytes: usize,
253
+ bytes_in_flight: usize,
254
+ now: Instant,
255
+ ),
256
+
257
+ pub on_packets_acked: fn(
258
+ r: &mut Congestion,
259
+ bytes_in_flight: usize,
260
+ packets: &mut Vec<Acked>,
261
+ now: Instant,
262
+ rtt_stats: &RttStats,
263
+ ),
264
+
265
+ pub congestion_event: fn(
266
+ r: &mut Congestion,
267
+ bytes_in_flight: usize,
268
+ lost_bytes: usize,
269
+ largest_lost_packet: &Sent,
270
+ now: Instant,
271
+ ),
272
+
273
+ pub checkpoint: fn(r: &mut Congestion),
274
+
275
+ pub rollback: fn(r: &mut Congestion) -> bool,
276
+
277
+ #[cfg(feature = "qlog")]
278
+ pub state_str: fn(r: &Congestion, now: Instant) -> &'static str,
279
+
280
+ pub debug_fmt: fn(
281
+ r: &Congestion,
282
+ formatter: &mut std::fmt::Formatter,
283
+ ) -> std::fmt::Result,
284
+ }
285
+
286
+ impl From<CongestionControlAlgorithm> for &'static CongestionControlOps {
287
+ fn from(algo: CongestionControlAlgorithm) -> Self {
288
+ match algo {
289
+ CongestionControlAlgorithm::Reno => &reno::RENO,
290
+ CongestionControlAlgorithm::CUBIC => &cubic::CUBIC,
291
+ // Bbr2Gcongestion is routed to the congestion implementation in
292
+ // the gcongestion directory by Recovery::new_with_config;
293
+ // LegacyRecovery never gets a RecoveryConfig with the
294
+ // Bbr2Gcongestion algorithm.
295
+ CongestionControlAlgorithm::Bbr2Gcongestion => unreachable!(),
296
+ }
297
+ }
298
+ }
299
+
300
+ #[cfg(test)]
301
+ mod tests {
302
+ use super::*;
303
+
304
+ #[test]
305
+ fn ssthresh_init() {
306
+ let ssthresh: SsThresh = Default::default();
307
+ assert_eq!(ssthresh.get(), usize::MAX);
308
+ assert_eq!(ssthresh.startup_exit(), None);
309
+ }
310
+
311
+ #[test]
312
+ fn ssthresh_in_css() {
313
+ let expected_startup_exit = StartupExit::new(
314
+ 1000,
315
+ None,
316
+ StartupExitReason::ConservativeSlowStartRounds,
317
+ );
318
+ let mut ssthresh: SsThresh = Default::default();
319
+ ssthresh.update(1000, true);
320
+ assert_eq!(ssthresh.get(), 1000);
321
+ assert_eq!(ssthresh.startup_exit(), Some(expected_startup_exit));
322
+
323
+ ssthresh.update(2000, true);
324
+ assert_eq!(ssthresh.get(), 2000);
325
+ // startup_exit is only updated on the first update.
326
+ assert_eq!(ssthresh.startup_exit(), Some(expected_startup_exit));
327
+
328
+ ssthresh.update(500, false);
329
+ assert_eq!(ssthresh.get(), 500);
330
+ assert_eq!(ssthresh.startup_exit(), Some(expected_startup_exit));
331
+ }
332
+
333
+ #[test]
334
+ fn ssthresh_in_slow_start() {
335
+ let expected_startup_exit =
336
+ StartupExit::new(1000, None, StartupExitReason::Loss);
337
+ let mut ssthresh: SsThresh = Default::default();
338
+ ssthresh.update(1000, false);
339
+ assert_eq!(ssthresh.get(), 1000);
340
+ assert_eq!(ssthresh.startup_exit(), Some(expected_startup_exit));
341
+
342
+ ssthresh.update(2000, true);
343
+ assert_eq!(ssthresh.get(), 2000);
344
+ // startup_exit is only updated on the first update.
345
+ assert_eq!(ssthresh.startup_exit(), Some(expected_startup_exit));
346
+
347
+ ssthresh.update(500, false);
348
+ assert_eq!(ssthresh.get(), 500);
349
+ assert_eq!(ssthresh.startup_exit(), Some(expected_startup_exit));
350
+ }
351
+ }
352
+
353
+ mod cubic;
354
+ mod delivery_rate;
355
+ mod hystart;
356
+ mod prr;
357
+ pub(crate) mod recovery;
358
+ mod reno;
359
+
360
+ #[cfg(test)]
361
+ mod test_sender;