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,514 @@
1
+ // Copyright (C) 2020-2022, 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
+ //! Delivery rate estimation.
28
+ //!
29
+ //! This implements the algorithm for estimating delivery rate as described in
30
+ //! <https://tools.ietf.org/html/draft-cheng-iccrg-delivery-rate-estimation-01>
31
+
32
+ use std::time::Duration;
33
+ use std::time::Instant;
34
+
35
+ use crate::recovery::bandwidth::Bandwidth;
36
+
37
+ use super::Acked;
38
+ use super::Sent;
39
+
40
+ #[derive(Debug)]
41
+ pub struct Rate {
42
+ delivered: usize,
43
+
44
+ delivered_time: Instant,
45
+
46
+ first_sent_time: Instant,
47
+
48
+ // Packet number of the last sent packet with app limited.
49
+ end_of_app_limited: u64,
50
+
51
+ // Packet number of the last sent packet.
52
+ last_sent_packet: u64,
53
+
54
+ // Packet number of the largest acked packet.
55
+ largest_acked: u64,
56
+
57
+ // Sample of rate estimation.
58
+ rate_sample: RateSample,
59
+ }
60
+
61
+ impl Default for Rate {
62
+ fn default() -> Self {
63
+ let now = Instant::now();
64
+
65
+ Rate {
66
+ delivered: 0,
67
+
68
+ delivered_time: now,
69
+
70
+ first_sent_time: now,
71
+
72
+ end_of_app_limited: 0,
73
+
74
+ last_sent_packet: 0,
75
+
76
+ largest_acked: 0,
77
+
78
+ rate_sample: RateSample::new(),
79
+ }
80
+ }
81
+ }
82
+
83
+ impl Rate {
84
+ pub fn on_packet_sent(
85
+ &mut self, pkt: &mut Sent, bytes_in_flight: usize, bytes_lost: u64,
86
+ ) {
87
+ // No packets in flight.
88
+ if bytes_in_flight == 0 {
89
+ self.first_sent_time = pkt.time_sent;
90
+ self.delivered_time = pkt.time_sent;
91
+ }
92
+
93
+ pkt.first_sent_time = self.first_sent_time;
94
+ pkt.delivered_time = self.delivered_time;
95
+ pkt.delivered = self.delivered;
96
+ pkt.is_app_limited = self.app_limited();
97
+ pkt.tx_in_flight = bytes_in_flight;
98
+ pkt.lost = bytes_lost;
99
+
100
+ self.last_sent_packet = pkt.pkt_num;
101
+ }
102
+
103
+ // Update the delivery rate sample when a packet is acked.
104
+ pub fn update_rate_sample(&mut self, pkt: &Acked, now: Instant) {
105
+ self.delivered += pkt.size;
106
+ self.delivered_time = now;
107
+
108
+ // Update info using the newest packet. If rate_sample is not yet
109
+ // initialized, initialize with the first packet.
110
+ if self.rate_sample.prior_time.is_none() ||
111
+ pkt.delivered >= self.rate_sample.prior_delivered
112
+ {
113
+ self.rate_sample.prior_delivered = pkt.delivered;
114
+ self.rate_sample.prior_time = Some(pkt.delivered_time);
115
+ self.rate_sample.is_app_limited = pkt.is_app_limited;
116
+ self.rate_sample.send_elapsed =
117
+ pkt.time_sent.saturating_duration_since(pkt.first_sent_time);
118
+ self.rate_sample.rtt = pkt.rtt;
119
+ self.rate_sample.ack_elapsed = self
120
+ .delivered_time
121
+ .saturating_duration_since(pkt.delivered_time);
122
+
123
+ self.first_sent_time = pkt.time_sent;
124
+ }
125
+
126
+ self.largest_acked = self.largest_acked.max(pkt.pkt_num);
127
+ }
128
+
129
+ pub fn generate_rate_sample(&mut self, min_rtt: Duration) {
130
+ // End app-limited phase if bubble is ACKed and gone.
131
+ if self.app_limited() && self.largest_acked > self.end_of_app_limited {
132
+ self.update_app_limited(false);
133
+ }
134
+
135
+ if self.rate_sample.prior_time.is_some() {
136
+ let interval = self
137
+ .rate_sample
138
+ .send_elapsed
139
+ .max(self.rate_sample.ack_elapsed);
140
+
141
+ self.rate_sample.delivered =
142
+ self.delivered - self.rate_sample.prior_delivered;
143
+ self.rate_sample.interval = interval;
144
+
145
+ if interval < min_rtt {
146
+ self.rate_sample.interval = Duration::ZERO;
147
+
148
+ // No reliable sample.
149
+ return;
150
+ }
151
+
152
+ if !interval.is_zero() {
153
+ let rate_sample_bandwidth = {
154
+ let rate_sample_bytes_per_second = (self.rate_sample.delivered
155
+ as f64 /
156
+ interval.as_secs_f64())
157
+ as u64;
158
+
159
+ Bandwidth::from_bytes_per_second(rate_sample_bytes_per_second)
160
+ };
161
+
162
+ // Match the [linux] implementation and only generate a new
163
+ // sample delivery rate if either:
164
+ // - the sample was not app_limited
165
+ // - the new rate is higher than the previous value
166
+ //
167
+ // [linux] https://github.com/torvalds/linux/commit/eb8329e0a04db0061f714f033b4454326ba147f4
168
+ if !self.rate_sample.is_app_limited ||
169
+ rate_sample_bandwidth > self.rate_sample.bandwidth
170
+ {
171
+ self.update_delivery_rate(rate_sample_bandwidth);
172
+ }
173
+ }
174
+ }
175
+ }
176
+
177
+ fn update_delivery_rate(&mut self, bandwidth: Bandwidth) {
178
+ self.rate_sample.bandwidth = bandwidth;
179
+ }
180
+
181
+ pub fn update_app_limited(&mut self, v: bool) {
182
+ self.end_of_app_limited =
183
+ if v { self.last_sent_packet.max(1) } else { 0 };
184
+ }
185
+
186
+ pub fn app_limited(&mut self) -> bool {
187
+ self.end_of_app_limited != 0
188
+ }
189
+
190
+ #[cfg(test)]
191
+ pub fn delivered(&self) -> usize {
192
+ self.delivered
193
+ }
194
+
195
+ pub fn sample_delivery_rate(&self) -> Bandwidth {
196
+ self.rate_sample.bandwidth
197
+ }
198
+
199
+ #[cfg(test)]
200
+ pub fn sample_is_app_limited(&self) -> bool {
201
+ self.rate_sample.is_app_limited
202
+ }
203
+ }
204
+
205
+ #[derive(Debug)]
206
+ struct RateSample {
207
+ // The sample delivery_rate in bytes/sec
208
+ bandwidth: Bandwidth,
209
+
210
+ is_app_limited: bool,
211
+
212
+ interval: Duration,
213
+
214
+ delivered: usize,
215
+
216
+ prior_delivered: usize,
217
+
218
+ prior_time: Option<Instant>,
219
+
220
+ send_elapsed: Duration,
221
+
222
+ ack_elapsed: Duration,
223
+
224
+ rtt: Duration,
225
+ }
226
+
227
+ impl RateSample {
228
+ const fn new() -> Self {
229
+ RateSample {
230
+ bandwidth: Bandwidth::zero(),
231
+ is_app_limited: false,
232
+ interval: Duration::ZERO,
233
+ delivered: 0,
234
+ prior_delivered: 0,
235
+ prior_time: None,
236
+ send_elapsed: Duration::ZERO,
237
+ ack_elapsed: Duration::ZERO,
238
+ rtt: Duration::ZERO,
239
+ }
240
+ }
241
+ }
242
+
243
+ #[cfg(test)]
244
+ mod tests {
245
+ use super::*;
246
+
247
+ use crate::packet;
248
+ use crate::ranges;
249
+ use crate::recovery::congestion::recovery::LegacyRecovery;
250
+ use crate::recovery::HandshakeStatus;
251
+ use crate::recovery::RecoveryOps;
252
+ use crate::test_utils;
253
+ use crate::Config;
254
+ use crate::OnAckReceivedOutcome;
255
+ use std::ops::Range;
256
+
257
+ // A [RateSample](delivery_rate::RateSample) is app_limited if it was
258
+ // generated when the [Rate](delivery_rate::Rate) was app_limited.
259
+ //
260
+ // The following test generates RateSamples before and after Rate is
261
+ // app_limited and asserts on app_limited status for samples.
262
+ #[test]
263
+ fn sample_is_app_limited() {
264
+ let config = Config::new(0xbabababa).unwrap();
265
+ let mut r = LegacyRecovery::new(&config);
266
+ let mut now = Instant::now();
267
+ let mss = r.max_datagram_size();
268
+
269
+ // Not App Limited prior to any activity
270
+ assert!(!r.congestion.delivery_rate.app_limited());
271
+ assert_eq!(r.congestion.delivery_rate.end_of_app_limited, 0);
272
+ assert!(!r.congestion.delivery_rate.sample_is_app_limited());
273
+
274
+ // Send/Ack first batch to generate a new delivery_rate sample.
275
+ let rtt = Duration::from_secs(2);
276
+ helper_send_and_ack_packets(&mut r, 0..4, now, rtt, mss);
277
+
278
+ // Marking Rate as app_limited.
279
+ r.delivery_rate_update_app_limited(true);
280
+ assert!(r.congestion.delivery_rate.app_limited());
281
+ assert_eq!(r.congestion.delivery_rate.end_of_app_limited, 3);
282
+
283
+ // Rate is app_limited.
284
+ assert!(r.congestion.delivery_rate.app_limited());
285
+ assert!(!r.congestion.delivery_rate.sample_is_app_limited());
286
+
287
+ // Send/Ack second batch to generate a new delivery_rate sample.
288
+ now += rtt;
289
+ helper_send_and_ack_packets(&mut r, 4..8, now, rtt, mss);
290
+
291
+ // Rate is no longer app limited since we sent a packet larger than
292
+ // `end_of_app_limited`
293
+ assert!(!r.congestion.delivery_rate.app_limited());
294
+ assert_eq!(r.congestion.delivery_rate.end_of_app_limited, 0);
295
+ // The RateSample is also app_limited
296
+ assert!(r.congestion.delivery_rate.sample_is_app_limited());
297
+ }
298
+
299
+ // A [RateSample](delivery_rate::RateSample) is is only updated if either not
300
+ // app_limited or greater than the previous value.
301
+ #[test]
302
+ fn app_limited_delivery_rate() {
303
+ // confirm that rate sample is not generated when app limited
304
+ let config = Config::new(0xbabababa).unwrap();
305
+ let mut r = LegacyRecovery::new(&config);
306
+ let mut now = Instant::now();
307
+ let mss = r.max_datagram_size();
308
+
309
+ // Not App Limited prior to any activity
310
+ assert!(!r.congestion.delivery_rate.app_limited());
311
+ assert_eq!(r.congestion.delivery_rate.end_of_app_limited, 0);
312
+ assert!(!r.congestion.delivery_rate.sample_is_app_limited());
313
+
314
+ // First batch
315
+ // Send/Ack first batch to generate a new delivery_rate sample.
316
+ let mut rtt = Duration::from_secs(2);
317
+ helper_send_and_ack_packets(&mut r, 0..2, now, rtt, mss);
318
+
319
+ // Marking Rate as app_limited.
320
+ r.delivery_rate_update_app_limited(true);
321
+ assert!(r.congestion.delivery_rate.app_limited());
322
+ assert_eq!(r.congestion.delivery_rate.end_of_app_limited, 1);
323
+ assert!(!r.congestion.delivery_rate.sample_is_app_limited());
324
+
325
+ let first_delivery_rate = r.delivery_rate().to_bytes_per_second();
326
+ let expected_delivery_rate = (mss * 2) as u64 / rtt.as_secs();
327
+ assert_eq!(expected_delivery_rate, 1200);
328
+ assert_eq!(first_delivery_rate, expected_delivery_rate);
329
+
330
+ // Second batch
331
+ // Since Rtt is larger, the delivery_rate will be smaller and not generate
332
+ // a new RateSample.
333
+ now += rtt;
334
+ rtt = Duration::from_secs(4);
335
+ helper_send_and_ack_packets(&mut r, 2..4, now, rtt, mss);
336
+
337
+ // Rate is no longer app limited since we sent a packet larger than
338
+ // `end_of_app_limited`
339
+ assert!(!r.congestion.delivery_rate.app_limited());
340
+ assert_eq!(r.congestion.delivery_rate.end_of_app_limited, 0);
341
+ // The RateSample is also app_limited
342
+ assert!(r.congestion.delivery_rate.sample_is_app_limited());
343
+
344
+ // Delivery rate NOT updated since the delivery rate is less than previous
345
+ // value
346
+ let expected_delivery_rate = (mss * 2) as u64 / rtt.as_secs();
347
+ assert_eq!(expected_delivery_rate, 600);
348
+ let app_limited_delivery_rate = r.delivery_rate().to_bytes_per_second();
349
+ assert_eq!(app_limited_delivery_rate, first_delivery_rate);
350
+
351
+ // Marking Rate as app_limited.
352
+ r.delivery_rate_update_app_limited(true);
353
+ assert!(r.congestion.delivery_rate.app_limited());
354
+ assert_eq!(r.congestion.delivery_rate.end_of_app_limited, 3);
355
+ // The RateSample is also app_limited
356
+ assert!(r.congestion.delivery_rate.sample_is_app_limited());
357
+
358
+ // Third batch
359
+ // Since Rtt is smaller, the delivery_rate will be larger and not generate
360
+ // a new RateSample even when app_limited.
361
+ now += rtt;
362
+ rtt = Duration::from_secs(1);
363
+ helper_send_and_ack_packets(&mut r, 4..6, now, rtt, mss);
364
+
365
+ // Rate is no longer app limited since we sent a packet larger than
366
+ // `end_of_app_limited`
367
+ assert!(!r.congestion.delivery_rate.app_limited());
368
+ assert_eq!(r.congestion.delivery_rate.end_of_app_limited, 0);
369
+ // The RateSample is also app_limited
370
+ assert!(r.congestion.delivery_rate.sample_is_app_limited());
371
+
372
+ // Delivery rate NOT updated since the delivery rate is less than previous
373
+ // value
374
+ let expected_delivery_rate = (mss * 2) as u64 / rtt.as_secs();
375
+ assert_eq!(expected_delivery_rate, 2400);
376
+ let app_limited_delivery_rate = r.delivery_rate().to_bytes_per_second();
377
+ assert_eq!(app_limited_delivery_rate, expected_delivery_rate);
378
+ }
379
+
380
+ #[test]
381
+ fn rate_check() {
382
+ let config = Config::new(0xbabababa).unwrap();
383
+ let mut r = LegacyRecovery::new(&config);
384
+
385
+ let now = Instant::now();
386
+ let mss = r.max_datagram_size();
387
+
388
+ // Send 2 packets.
389
+ for pn in 0..2 {
390
+ let pkt = test_utils::helper_packet_sent(pn, now, mss);
391
+
392
+ r.on_packet_sent(
393
+ pkt,
394
+ packet::Epoch::Application,
395
+ HandshakeStatus::default(),
396
+ now,
397
+ "",
398
+ );
399
+ }
400
+
401
+ let rtt = Duration::from_millis(50);
402
+ let now = now + rtt;
403
+
404
+ // Ack 2 packets.
405
+ for pn in 0..2 {
406
+ let acked = Acked {
407
+ pkt_num: pn,
408
+ time_sent: now,
409
+ size: mss,
410
+ rtt,
411
+ delivered: 0,
412
+ delivered_time: now,
413
+ first_sent_time: now.checked_sub(rtt).unwrap(),
414
+ is_app_limited: false,
415
+ };
416
+
417
+ r.congestion.delivery_rate.update_rate_sample(&acked, now);
418
+ }
419
+
420
+ // Update rate sample after 1 rtt.
421
+ r.congestion.delivery_rate.generate_rate_sample(rtt);
422
+
423
+ // Bytes acked so far.
424
+ assert_eq!(r.congestion.delivery_rate.delivered(), 2400);
425
+
426
+ // Estimated delivery rate = (1200 x 2) / 0.05s = 48000.
427
+ assert_eq!(r.delivery_rate().to_bytes_per_second(), 48000);
428
+ }
429
+
430
+ #[test]
431
+ fn app_limited_cwnd_full() {
432
+ let config = Config::new(0xbabababa).unwrap();
433
+ let mut r = LegacyRecovery::new(&config);
434
+
435
+ let now = Instant::now();
436
+ let mss = r.max_datagram_size();
437
+
438
+ // Not App Limited prior to any activity
439
+ assert!(!r.app_limited());
440
+ assert!(!r.congestion.delivery_rate.sample_is_app_limited());
441
+
442
+ // Send 10 packets to fill cwnd.
443
+ for pn in 0..5 {
444
+ let pkt = test_utils::helper_packet_sent(pn, now, mss);
445
+ r.on_packet_sent(
446
+ pkt,
447
+ packet::Epoch::Application,
448
+ HandshakeStatus::default(),
449
+ now,
450
+ "",
451
+ );
452
+ }
453
+
454
+ // App Limited after sending partial cwnd worth of data
455
+ assert!(r.app_limited());
456
+ assert!(!r.congestion.delivery_rate.sample_is_app_limited());
457
+
458
+ for pn in 5..10 {
459
+ let pkt = test_utils::helper_packet_sent(pn, now, mss);
460
+ r.on_packet_sent(
461
+ pkt,
462
+ packet::Epoch::Application,
463
+ HandshakeStatus::default(),
464
+ now,
465
+ "",
466
+ );
467
+ }
468
+
469
+ // Not App Limited after sending full cwnd worth of data
470
+ assert!(!r.app_limited());
471
+ assert!(!r.congestion.delivery_rate.sample_is_app_limited());
472
+ }
473
+
474
+ fn helper_send_and_ack_packets(
475
+ recovery: &mut LegacyRecovery, range: Range<u64>, now: Instant,
476
+ rtt: Duration, mss: usize,
477
+ ) {
478
+ for pn in range.clone() {
479
+ let pkt = test_utils::helper_packet_sent(pn, now, mss);
480
+ recovery.on_packet_sent(
481
+ pkt,
482
+ packet::Epoch::Application,
483
+ HandshakeStatus::default(),
484
+ now,
485
+ "",
486
+ );
487
+ }
488
+
489
+ let packet_count = range.clone().count();
490
+
491
+ // Ack packets, which generates a new delivery_rate
492
+ let mut acked = ranges::RangeSet::default();
493
+ acked.insert(range);
494
+
495
+ let ack_outcome = recovery
496
+ .on_ack_received(
497
+ &acked,
498
+ 25,
499
+ packet::Epoch::Application,
500
+ HandshakeStatus::default(),
501
+ now + rtt,
502
+ None,
503
+ "",
504
+ )
505
+ .unwrap();
506
+
507
+ assert_eq!(ack_outcome, OnAckReceivedOutcome {
508
+ lost_packets: 0,
509
+ lost_bytes: 0,
510
+ acked_bytes: mss * packet_count,
511
+ spurious_losses: 0,
512
+ });
513
+ }
514
+ }