rwebtransport 0.1.0 → 0.1.2

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 (113) hide show
  1. package/Cargo.lock +666 -666
  2. package/Cargo.toml +31 -31
  3. package/LICENSE +202 -202
  4. package/NOTICE +41 -41
  5. package/README.md +550 -550
  6. package/crates/echo-server/Cargo.toml +20 -20
  7. package/crates/echo-server/src/main.rs +666 -666
  8. package/crates/native/Cargo.toml +23 -23
  9. package/crates/native/src/config.rs +129 -129
  10. package/crates/native/src/driver.rs +463 -463
  11. package/crates/native/src/lib.rs +821 -821
  12. package/crates/native/src/server.rs +718 -718
  13. package/crates/native/src/session.rs +1191 -1191
  14. package/crates/native/src/tls.rs +96 -96
  15. package/crates/wtcore/Cargo.toml +15 -15
  16. package/crates/wtcore/src/h3.rs +226 -226
  17. package/crates/wtcore/src/lib.rs +9 -9
  18. package/dist/index.js.map +1 -1
  19. package/dist/index.mjs.map +1 -1
  20. package/package.json +80 -79
  21. package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
  22. package/prebuilds/linux-arm64/rwebtransport.node +0 -0
  23. package/prebuilds/linux-x64/rwebtransport.node +0 -0
  24. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  25. package/scripts/bench.mjs +180 -180
  26. package/scripts/build.js +67 -67
  27. package/scripts/postinstall.js +67 -67
  28. package/scripts/setup-unix.sh +129 -129
  29. package/scripts/setup-windows.ps1 +229 -229
  30. package/vendor/quiche/AGENTS.md +75 -75
  31. package/vendor/quiche/COPYING +23 -23
  32. package/vendor/quiche/Cargo.lock +1212 -1212
  33. package/vendor/quiche/Cargo.toml +215 -215
  34. package/vendor/quiche/README.md +466 -466
  35. package/vendor/quiche/examples/Makefile +46 -46
  36. package/vendor/quiche/examples/README.md +20 -20
  37. package/vendor/quiche/examples/cert-big.crt +90 -90
  38. package/vendor/quiche/examples/cert.crt +18 -18
  39. package/vendor/quiche/examples/cert.key +28 -28
  40. package/vendor/quiche/examples/client.c +343 -343
  41. package/vendor/quiche/examples/client.rs +281 -281
  42. package/vendor/quiche/examples/gen-certs.sh +15 -15
  43. package/vendor/quiche/examples/http3-client.c +473 -473
  44. package/vendor/quiche/examples/http3-client.rs +349 -349
  45. package/vendor/quiche/examples/http3-server.c +614 -614
  46. package/vendor/quiche/examples/http3-server.rs +680 -680
  47. package/vendor/quiche/examples/qpack-decode.rs +87 -87
  48. package/vendor/quiche/examples/qpack-encode.rs +88 -88
  49. package/vendor/quiche/examples/rootca.crt +21 -21
  50. package/vendor/quiche/examples/server.c +529 -529
  51. package/vendor/quiche/examples/server.rs +529 -529
  52. package/vendor/quiche/include/quiche.h +1282 -1282
  53. package/vendor/quiche/src/buffers.rs +105 -105
  54. package/vendor/quiche/src/build.rs +66 -66
  55. package/vendor/quiche/src/cid.rs +1079 -1079
  56. package/vendor/quiche/src/crypto/boringssl.rs +373 -373
  57. package/vendor/quiche/src/crypto/mod.rs +671 -671
  58. package/vendor/quiche/src/dgram.rs +118 -118
  59. package/vendor/quiche/src/error.rs +263 -263
  60. package/vendor/quiche/src/ffi.rs +2290 -2290
  61. package/vendor/quiche/src/flowcontrol.rs +246 -246
  62. package/vendor/quiche/src/frame.rs +2172 -2172
  63. package/vendor/quiche/src/h3/AGENTS.md +54 -54
  64. package/vendor/quiche/src/h3/ffi.rs +442 -442
  65. package/vendor/quiche/src/h3/frame.rs +1404 -1404
  66. package/vendor/quiche/src/h3/mod.rs +8223 -8223
  67. package/vendor/quiche/src/h3/qpack/decoder.rs +288 -288
  68. package/vendor/quiche/src/h3/qpack/encoder.rs +235 -235
  69. package/vendor/quiche/src/h3/qpack/mod.rs +202 -202
  70. package/vendor/quiche/src/h3/qpack/static_table.rs +323 -323
  71. package/vendor/quiche/src/h3/stream.rs +1595 -1595
  72. package/vendor/quiche/src/lib.rs +9556 -9556
  73. package/vendor/quiche/src/minmax.rs +446 -446
  74. package/vendor/quiche/src/packet.rs +2334 -2334
  75. package/vendor/quiche/src/path.rs +1374 -1374
  76. package/vendor/quiche/src/pmtud.rs +652 -652
  77. package/vendor/quiche/src/rand.rs +64 -64
  78. package/vendor/quiche/src/range_buf.rs +186 -186
  79. package/vendor/quiche/src/ranges.rs +755 -755
  80. package/vendor/quiche/src/recovery/AGENTS.md +80 -80
  81. package/vendor/quiche/src/recovery/bandwidth.rs +565 -565
  82. package/vendor/quiche/src/recovery/bytes_in_flight.rs +179 -179
  83. package/vendor/quiche/src/recovery/congestion/cubic.rs +1038 -1038
  84. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +514 -514
  85. package/vendor/quiche/src/recovery/congestion/hystart.rs +241 -241
  86. package/vendor/quiche/src/recovery/congestion/mod.rs +361 -361
  87. package/vendor/quiche/src/recovery/congestion/prr.rs +238 -238
  88. package/vendor/quiche/src/recovery/congestion/recovery.rs +1170 -1170
  89. package/vendor/quiche/src/recovery/congestion/reno.rs +291 -291
  90. package/vendor/quiche/src/recovery/congestion/test_sender.rs +201 -201
  91. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +1947 -1947
  92. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +158 -158
  93. package/vendor/quiche/src/recovery/gcongestion/bbr.rs +37 -37
  94. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +120 -120
  95. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +318 -318
  96. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +784 -784
  97. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +729 -729
  98. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +175 -175
  99. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +190 -190
  100. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +855 -855
  101. package/vendor/quiche/src/recovery/gcongestion/mod.rs +275 -275
  102. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +294 -294
  103. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +1384 -1384
  104. package/vendor/quiche/src/recovery/mod.rs +3086 -3086
  105. package/vendor/quiche/src/recovery/rtt.rs +152 -152
  106. package/vendor/quiche/src/stream/mod.rs +2210 -2210
  107. package/vendor/quiche/src/stream/recv_buf.rs +1236 -1236
  108. package/vendor/quiche/src/stream/send_buf.rs +849 -849
  109. package/vendor/quiche/src/test_utils.rs +681 -681
  110. package/vendor/quiche/src/tests.rs +12922 -12922
  111. package/vendor/quiche/src/tls/boringssl.rs +375 -375
  112. package/vendor/quiche/src/tls/mod.rs +1245 -1245
  113. package/vendor/quiche/src/transport_params.rs +628 -628
@@ -1,514 +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
- }
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
+ }