rwebtransport 0.2.2 → 0.2.5

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 (99) hide show
  1. package/Cargo.lock +523 -140
  2. package/Cargo.toml +5 -5
  3. package/README.md +8 -4
  4. package/crates/echo-server/src/bin/gen-test-cert.rs +1 -1
  5. package/crates/native/Cargo.toml +2 -0
  6. package/crates/native/src/config.rs +69 -8
  7. package/crates/native/src/driver.rs +253 -90
  8. package/crates/native/src/lib.rs +80 -57
  9. package/crates/native/src/parser_memory.rs +203 -0
  10. package/crates/native/src/server.rs +215 -183
  11. package/crates/native/src/session.rs +1341 -243
  12. package/crates/native/src/test_support.rs +119 -0
  13. package/crates/wtcore/Cargo.toml +1 -0
  14. package/crates/wtcore/src/h3.rs +250 -46
  15. package/dist/index.js +218 -75
  16. package/dist/index.js.map +7 -1
  17. package/dist/index.mjs +214 -69
  18. package/dist/index.mjs.map +7 -1
  19. package/dist/native.d.ts +10 -4
  20. package/dist/server.d.ts +1 -0
  21. package/package.json +84 -78
  22. package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
  23. package/prebuilds/linux-arm64/rwebtransport.node +0 -0
  24. package/prebuilds/linux-x64/rwebtransport.node +0 -0
  25. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  26. package/scripts/build-linux-prebuilt.sh +46 -0
  27. package/scripts/build-ts.js +41 -0
  28. package/scripts/build.js +10 -3
  29. package/vendor/quiche/AGENTS.md +10 -0
  30. package/vendor/quiche/Cargo.lock +239 -248
  31. package/vendor/quiche/Cargo.toml +23 -10
  32. package/vendor/quiche/examples/Makefile +4 -4
  33. package/vendor/quiche/examples/client.rs +10 -28
  34. package/vendor/quiche/examples/http3-client.rs +20 -34
  35. package/vendor/quiche/examples/http3-server.rs +47 -78
  36. package/vendor/quiche/examples/server.rs +19 -49
  37. package/vendor/quiche/include/quiche.h +35 -4
  38. package/vendor/quiche/src/build.rs +17 -0
  39. package/vendor/quiche/src/cid.rs +129 -61
  40. package/vendor/quiche/src/crypto/boringssl.rs +98 -86
  41. package/vendor/quiche/src/crypto/mod.rs +64 -77
  42. package/vendor/quiche/src/dgram.rs +2 -3
  43. package/vendor/quiche/src/error.rs +22 -20
  44. package/vendor/quiche/src/ffi.rs +558 -280
  45. package/vendor/quiche/src/flowcontrol.rs +0 -7
  46. package/vendor/quiche/src/frame.rs +134 -135
  47. package/vendor/quiche/src/h3/ffi.rs +73 -60
  48. package/vendor/quiche/src/h3/frame.rs +70 -90
  49. package/vendor/quiche/src/h3/mod.rs +1024 -593
  50. package/vendor/quiche/src/h3/qpack/decoder.rs +248 -34
  51. package/vendor/quiche/src/h3/qpack/dynamic_table.rs +859 -0
  52. package/vendor/quiche/src/h3/qpack/encoder.rs +14 -17
  53. package/vendor/quiche/src/h3/qpack/mod.rs +48 -0
  54. package/vendor/quiche/src/h3/qpack/static_table.rs +86 -73
  55. package/vendor/quiche/src/h3/stream.rs +1009 -194
  56. package/vendor/quiche/src/lib.rs +1361 -1195
  57. package/vendor/quiche/src/minmax.rs +2 -6
  58. package/vendor/quiche/src/packet.rs +420 -552
  59. package/vendor/quiche/src/path.rs +202 -72
  60. package/vendor/quiche/src/pmtud.rs +18 -16
  61. package/vendor/quiche/src/ranges.rs +184 -82
  62. package/vendor/quiche/src/recovery/AGENTS.md +5 -2
  63. package/vendor/quiche/src/recovery/bandwidth.rs +59 -40
  64. package/vendor/quiche/src/recovery/bytes_in_flight.rs +2 -2
  65. package/vendor/quiche/src/recovery/congestion/cubic.rs +38 -47
  66. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +54 -60
  67. package/vendor/quiche/src/recovery/congestion/hystart.rs +6 -10
  68. package/vendor/quiche/src/recovery/congestion/mod.rs +110 -42
  69. package/vendor/quiche/src/recovery/congestion/prr.rs +4 -1
  70. package/vendor/quiche/src/recovery/congestion/recovery.rs +91 -95
  71. package/vendor/quiche/src/recovery/congestion/reno.rs +17 -12
  72. package/vendor/quiche/src/recovery/congestion/test_sender.rs +8 -27
  73. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +159 -247
  74. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +9 -14
  75. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +18 -15
  76. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +43 -28
  77. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +349 -76
  78. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +147 -195
  79. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +29 -27
  80. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min.rs +180 -0
  81. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min_tests.rs +221 -0
  82. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm.rs +395 -0
  83. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm_tests.rs +473 -0
  84. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/mod.rs +175 -0
  85. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +33 -26
  86. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +107 -60
  87. package/vendor/quiche/src/recovery/gcongestion/mod.rs +64 -11
  88. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +34 -25
  89. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +81 -86
  90. package/vendor/quiche/src/recovery/mod.rs +414 -173
  91. package/vendor/quiche/src/recovery/rtt.rs +8 -5
  92. package/vendor/quiche/src/stream/mod.rs +864 -133
  93. package/vendor/quiche/src/stream/recv_buf.rs +476 -172
  94. package/vendor/quiche/src/stream/send_buf.rs +343 -81
  95. package/vendor/quiche/src/test_utils.rs +78 -64
  96. package/vendor/quiche/src/tests.rs +2559 -1335
  97. package/vendor/quiche/src/tls/boringssl.rs +60 -63
  98. package/vendor/quiche/src/tls/mod.rs +146 -193
  99. package/vendor/quiche/src/transport_params.rs +87 -103
@@ -0,0 +1,473 @@
1
+ // Copyright (C) 2026, 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::Duration;
28
+ use std::time::Instant;
29
+
30
+ use crate::recovery::gcongestion::BbrRttJumpDetector;
31
+
32
+ use super::super::ActiveRttJumpDetector;
33
+ use super::super::RttJumpDetector;
34
+ use super::*;
35
+
36
+ const RTT: Duration = Duration::from_millis(50);
37
+ const RTT_2X: Duration = Duration::from_millis(100);
38
+ const RTT_3X: Duration = Duration::from_millis(150);
39
+ const HIGH_RTT: Duration = Duration::from_millis(500);
40
+ const HIGH_RTT_3X: Duration = Duration::from_millis(1500);
41
+ const LOW_RTT: Duration = Duration::from_millis(10);
42
+
43
+ fn ms(millis: u64) -> Duration {
44
+ Duration::from_millis(millis)
45
+ }
46
+
47
+ fn sample(detector: &mut RttJumpDetector, base: Instant, offset: Duration, rtt: Duration) {
48
+ sample_with_full_bandwidth(detector, base, offset, rtt, true);
49
+ }
50
+
51
+ fn sample_with_full_bandwidth(
52
+ detector: &mut RttJumpDetector,
53
+ base: Instant,
54
+ offset: Duration,
55
+ rtt: Duration,
56
+ full_bandwidth_reached: bool,
57
+ ) {
58
+ detector.on_rtt_sample(rtt, base + offset, full_bandwidth_reached);
59
+ }
60
+
61
+ fn hmm_detector() -> RttJumpDetector {
62
+ RttJumpDetector::new(BbrRttJumpDetector::Hmm)
63
+ }
64
+
65
+ fn hmm_build_baseline(detector: &mut RttJumpDetector, count: u64, base: Instant, rtt: Duration) {
66
+ for pkt in 1..=count {
67
+ sample(detector, base, ms(pkt * 10), rtt);
68
+ }
69
+ assert!(!detector.is_rtt_jump_active());
70
+ }
71
+
72
+ fn prewarmed_hmm_with_pending(
73
+ episode: HmmEpisode,
74
+ baseline: Duration,
75
+ alpha: [f32; HMM_STATE_COUNT],
76
+ pending_state: HmmState,
77
+ pending_start_time: Instant,
78
+ pending_samples: u32,
79
+ episode_counted: bool,
80
+ ) -> HmmDetector {
81
+ HmmDetector {
82
+ episode,
83
+ valid_samples: HMM_WARMUP_SAMPLES + 1,
84
+ baseline: Some(baseline),
85
+ dispersion: HMM_ELEVATION_FLOOR,
86
+ alpha,
87
+ pending_state,
88
+ pending_start_time: Some(pending_start_time),
89
+ pending_samples,
90
+ episode_counted,
91
+ ..HmmDetector::default()
92
+ }
93
+ }
94
+
95
+ fn detector_with_hmm(hmm: HmmDetector, persistent_jump_count: u64) -> RttJumpDetector {
96
+ RttJumpDetector {
97
+ persistent_jump_count,
98
+ last_persistent_jump_time: None,
99
+ detector: ActiveRttJumpDetector::Hmm(hmm),
100
+ }
101
+ }
102
+
103
+ #[test]
104
+ fn hmm_no_jump_on_stable_rtt() {
105
+ let mut detector = hmm_detector();
106
+ let base = Instant::now();
107
+
108
+ for pkt in 1..21u64 {
109
+ sample(&mut detector, base, ms(pkt * 10), RTT);
110
+ }
111
+
112
+ assert!(!detector.is_rtt_jump_active());
113
+ assert!(!detector.is_rtt_jump_persistent());
114
+ assert_eq!(detector.rtt_persistent_jump_count(), 0);
115
+ assert_eq!(detector.last_persistent_jump_time(), None);
116
+ }
117
+
118
+ #[test]
119
+ fn hmm_warmup_suppresses_cold_start_spike() {
120
+ let mut detector = hmm_detector();
121
+ let base = Instant::now();
122
+
123
+ // Models early self-queueing before the detector has enough normal
124
+ // samples to estimate the connection's operating elevation.
125
+ sample(&mut detector, base, ms(0), RTT);
126
+ sample(&mut detector, base, ms(10), RTT_3X * 2);
127
+ sample(&mut detector, base, ms(20), RTT_3X * 2);
128
+
129
+ assert!(!detector.is_rtt_jump_active());
130
+ assert_eq!(detector.rtt_persistent_jump_count(), 0);
131
+ }
132
+
133
+ #[test]
134
+ fn hmm_single_spike_does_not_commit() {
135
+ let mut detector = hmm_detector();
136
+ let base = Instant::now();
137
+
138
+ hmm_build_baseline(&mut detector, 12, base, RTT);
139
+
140
+ sample(&mut detector, base, ms(130), RTT_3X);
141
+ assert!(!detector.is_rtt_jump_active());
142
+ assert!(!detector.is_rtt_jump_persistent());
143
+ assert_eq!(detector.rtt_persistent_jump_count(), 0);
144
+ assert_eq!(detector.last_persistent_jump_time(), None);
145
+
146
+ sample(&mut detector, base, ms(140), RTT);
147
+ assert!(!detector.is_rtt_jump_active());
148
+ }
149
+
150
+ #[test]
151
+ fn hmm_sustained_step_becomes_persistent() {
152
+ let mut detector = hmm_detector();
153
+ let base = Instant::now();
154
+
155
+ hmm_build_baseline(&mut detector, 12, base, RTT);
156
+
157
+ let mut offset = 200u64;
158
+ for _ in 13u64..27 {
159
+ sample(&mut detector, base, ms(offset), RTT_3X);
160
+ offset += 100;
161
+ }
162
+
163
+ assert!(detector.is_rtt_jump_persistent());
164
+ assert_eq!(detector.rtt_persistent_jump_count(), 1);
165
+ assert!(detector.last_persistent_jump_time().is_some());
166
+ }
167
+
168
+ #[test]
169
+ fn hmm_persistent_requires_confirming_samples() {
170
+ let base = Instant::now();
171
+ let hmm = prewarmed_hmm_with_pending(
172
+ HmmEpisode::Active,
173
+ RTT,
174
+ [0.0, 0.0, 1.0],
175
+ HmmState::Persistent,
176
+ base,
177
+ 1,
178
+ false,
179
+ );
180
+ let mut detector = detector_with_hmm(hmm, 0);
181
+
182
+ sample(&mut detector, base, ms(800), RTT_3X);
183
+
184
+ assert!(!detector.is_rtt_jump_persistent());
185
+
186
+ sample(&mut detector, base, ms(900), RTT_3X);
187
+ assert!(detector.is_rtt_jump_persistent());
188
+ assert_eq!(detector.rtt_persistent_jump_count(), 1);
189
+ }
190
+
191
+ #[test]
192
+ fn hmm_persistent_requires_rtt_scaled_dwell() {
193
+ let base = Instant::now();
194
+ let hmm = prewarmed_hmm_with_pending(
195
+ HmmEpisode::Active,
196
+ HIGH_RTT,
197
+ [0.0, 0.0, 1.0],
198
+ HmmState::Persistent,
199
+ base,
200
+ HMM_PERSIST_CONFIRM_SAMPLES,
201
+ false,
202
+ );
203
+ let mut detector = detector_with_hmm(hmm, 0);
204
+
205
+ sample(&mut detector, base, ms(7000), HIGH_RTT_3X);
206
+ assert!(!detector.is_rtt_jump_persistent());
207
+
208
+ sample(&mut detector, base, ms(7600), HIGH_RTT_3X);
209
+ assert!(detector.is_rtt_jump_persistent());
210
+ assert_eq!(detector.rtt_persistent_jump_count(), 1);
211
+ }
212
+
213
+ #[test]
214
+ fn hmm_transient_high_rtt_waits_for_rtt_scaled_dwell() {
215
+ let base = Instant::now();
216
+ let mut early = prewarmed_hmm_with_pending(
217
+ HmmEpisode::Idle,
218
+ HIGH_RTT,
219
+ [0.0, 1.0, 0.0],
220
+ HmmState::Transient,
221
+ base,
222
+ 1,
223
+ false,
224
+ );
225
+
226
+ early.on_rtt_sample(HIGH_RTT_3X, base + ms(80), true);
227
+ assert!(!early.is_rtt_jump_active());
228
+
229
+ let mut late = prewarmed_hmm_with_pending(
230
+ HmmEpisode::Idle,
231
+ HIGH_RTT,
232
+ [0.0, 1.0, 0.0],
233
+ HmmState::Transient,
234
+ base,
235
+ 1,
236
+ false,
237
+ );
238
+
239
+ late.on_rtt_sample(HIGH_RTT_3X, base + HIGH_RTT_3X, true);
240
+ assert!(late.is_rtt_jump_active());
241
+ }
242
+
243
+ #[test]
244
+ fn hmm_clear_high_rtt_waits_for_rtt_scaled_dwell() {
245
+ let base = Instant::now();
246
+ let mut detector = prewarmed_hmm_with_pending(
247
+ HmmEpisode::Persistent,
248
+ HIGH_RTT,
249
+ [1.0, 0.0, 0.0],
250
+ HmmState::Normal,
251
+ base,
252
+ 1,
253
+ true,
254
+ );
255
+
256
+ detector.on_rtt_sample(HIGH_RTT, base + ms(80), true);
257
+ assert!(detector.is_rtt_jump_persistent());
258
+
259
+ detector.on_rtt_sample(HIGH_RTT, base + HIGH_RTT, true);
260
+ assert!(!detector.is_rtt_jump_active());
261
+ }
262
+
263
+ #[test]
264
+ fn hmm_clear_rebases_dispersion_to_new_normal() {
265
+ let base = Instant::now();
266
+ let mut detector = prewarmed_hmm_with_pending(
267
+ HmmEpisode::Persistent,
268
+ RTT,
269
+ [1.0, 0.0, 0.0],
270
+ HmmState::Normal,
271
+ base,
272
+ 1,
273
+ true,
274
+ );
275
+ detector.dispersion = 0.8;
276
+ let initial_dispersion = detector.dispersion;
277
+
278
+ detector.on_rtt_sample(RTT_2X, base + RTT_2X, true);
279
+
280
+ assert!(!detector.is_rtt_jump_active());
281
+ assert_eq!(detector.baseline, Some(RTT_2X));
282
+ assert!(detector.dispersion < initial_dispersion);
283
+ }
284
+
285
+ #[test]
286
+ fn hmm_low_rtt_still_respects_min_dwell() {
287
+ assert_eq!(
288
+ dwell_for_transition(HmmState::Normal, HmmState::Transient, LOW_RTT),
289
+ HMM_MIN_DWELL
290
+ );
291
+ }
292
+
293
+ #[test]
294
+ fn hmm_reentering_persistent_same_episode_does_not_recount() {
295
+ let base = Instant::now();
296
+ let hmm = prewarmed_hmm_with_pending(
297
+ HmmEpisode::Active,
298
+ RTT,
299
+ [0.0, 0.0, 1.0],
300
+ HmmState::Persistent,
301
+ base,
302
+ HMM_PERSIST_CONFIRM_SAMPLES,
303
+ true,
304
+ );
305
+ let mut detector = detector_with_hmm(hmm, 1);
306
+
307
+ sample(&mut detector, base, ms(1000), RTT_3X);
308
+
309
+ assert!(detector.is_rtt_jump_persistent());
310
+ assert_eq!(detector.rtt_persistent_jump_count(), 1);
311
+ }
312
+
313
+ #[test]
314
+ fn hmm_clear_allows_later_jump_to_recount() {
315
+ let mut detector = hmm_detector();
316
+ let base = Instant::now();
317
+
318
+ hmm_build_baseline(&mut detector, 12, base, RTT);
319
+
320
+ let mut offset = 200u64;
321
+ while !detector.is_rtt_jump_persistent() {
322
+ sample(&mut detector, base, ms(offset), RTT_3X);
323
+ offset += 100;
324
+ assert!(offset < 5000, "first persistent jump was never confirmed");
325
+ }
326
+ assert_eq!(detector.rtt_persistent_jump_count(), 1);
327
+
328
+ while detector.is_rtt_jump_active() {
329
+ sample(&mut detector, base, ms(offset), RTT);
330
+ offset += 100;
331
+ assert!(offset < 8000, "persistent jump was never cleared");
332
+ }
333
+ assert_eq!(detector.rtt_persistent_jump_count(), 1);
334
+
335
+ while !detector.is_rtt_jump_persistent() {
336
+ sample(&mut detector, base, ms(offset), RTT_3X);
337
+ offset += 100;
338
+ assert!(offset < 13000, "second persistent jump was never confirmed");
339
+ }
340
+ assert_eq!(detector.rtt_persistent_jump_count(), 2);
341
+ }
342
+
343
+ #[test]
344
+ fn hmm_sustained_plateau_does_not_clear() {
345
+ let mut detector = hmm_detector();
346
+ let base = Instant::now();
347
+
348
+ hmm_build_baseline(&mut detector, 12, base, RTT);
349
+
350
+ let mut offset = 200u64;
351
+ while !detector.is_rtt_jump_persistent() {
352
+ sample(&mut detector, base, ms(offset), RTT_3X);
353
+ offset += 100;
354
+ assert!(offset < 5000, "persistent jump was never confirmed");
355
+ }
356
+
357
+ for _ in 0..100u64 {
358
+ sample(&mut detector, base, ms(offset), RTT_3X);
359
+ assert!(
360
+ detector.is_rtt_jump_persistent(),
361
+ "sustained plateau cleared at t={offset}ms"
362
+ );
363
+ offset += 100;
364
+ }
365
+ assert_eq!(detector.rtt_persistent_jump_count(), 1);
366
+ }
367
+
368
+ #[test]
369
+ fn hmm_self_calibration_absorbs_jump_within_link_noise() {
370
+ let base = Instant::now();
371
+
372
+ let mut quiet = hmm_detector();
373
+ hmm_build_baseline(&mut quiet, 8, base, RTT);
374
+ let mut fired = false;
375
+ for pkt in 9..109u64 {
376
+ sample(&mut quiet, base, ms(pkt * 10), RTT_2X);
377
+ fired |= quiet.is_rtt_jump_active();
378
+ }
379
+ assert!(fired, "quiet link should flag a sustained 2x step");
380
+
381
+ let mut noisy = hmm_detector();
382
+ let warmup = [50u64, 100, 100, 100, 100, 100, 100, 100];
383
+ for (i, &rtt) in warmup.iter().enumerate() {
384
+ let pkt = i as u64 + 1;
385
+ sample(&mut noisy, base, ms(pkt * 10), ms(rtt));
386
+ }
387
+ let mut fired = false;
388
+ for pkt in 9..109u64 {
389
+ sample(&mut noisy, base, ms(pkt * 10), RTT_2X);
390
+ fired |= noisy.is_rtt_jump_active();
391
+ }
392
+ assert!(!fired, "noisy link should absorb the same 2x step");
393
+ }
394
+
395
+ #[test]
396
+ fn hmm_detection_is_scale_invariant() {
397
+ fn fires_for_base(base_rtt: Duration) -> bool {
398
+ let mut detector = hmm_detector();
399
+ let base = Instant::now();
400
+ hmm_build_baseline(&mut detector, 8, base, base_rtt);
401
+ let mut fired = false;
402
+ for pkt in 9..31u64 {
403
+ sample(&mut detector, base, base_rtt * pkt as u32, base_rtt * 2);
404
+ fired |= detector.is_rtt_jump_persistent();
405
+ }
406
+ fired
407
+ }
408
+
409
+ assert!(fires_for_base(RTT));
410
+ assert_eq!(fires_for_base(RTT), fires_for_base(HIGH_RTT));
411
+ }
412
+
413
+ #[test]
414
+ fn hmm_startup_grace_suppresses_commits() {
415
+ let mut detector = hmm_detector();
416
+ let base = Instant::now();
417
+
418
+ for pkt in 1..9u64 {
419
+ sample_with_full_bandwidth(&mut detector, base, ms(pkt * 10), RTT, false);
420
+ }
421
+
422
+ for pkt in 9..15u64 {
423
+ sample_with_full_bandwidth(&mut detector, base, ms(pkt * 10), RTT_3X, false);
424
+ assert!(
425
+ !detector.is_rtt_jump_active(),
426
+ "startup grace must suppress commits (sample {pkt})"
427
+ );
428
+ }
429
+ assert!(!detector.is_rtt_jump_persistent());
430
+ assert_eq!(detector.rtt_persistent_jump_count(), 0);
431
+ assert_eq!(detector.last_persistent_jump_time(), None);
432
+
433
+ let mut offset = 300u64;
434
+ let mut fired = false;
435
+ for _ in 15u64..27 {
436
+ sample(&mut detector, base, ms(offset), RTT_3X);
437
+ fired |= detector.is_rtt_jump_active();
438
+ offset += 100;
439
+ }
440
+ assert!(fired, "detection must resume after leaving Startup");
441
+ }
442
+
443
+ #[test]
444
+ fn hmm_startup_resets_pending_transition_before_detection_resumes() {
445
+ let mut detector = hmm_detector();
446
+ let base = Instant::now();
447
+
448
+ for pkt in 1..9u64 {
449
+ sample_with_full_bandwidth(&mut detector, base, ms(pkt * 10), RTT, false);
450
+ }
451
+
452
+ for pkt in 9..21u64 {
453
+ sample_with_full_bandwidth(&mut detector, base, ms(pkt * 100), RTT_3X, false);
454
+ }
455
+
456
+ sample(&mut detector, base, ms(2100), RTT_3X);
457
+ assert!(!detector.is_rtt_jump_persistent());
458
+ assert_eq!(detector.rtt_persistent_jump_count(), 0);
459
+ }
460
+
461
+ #[test]
462
+ fn hmm_forward_filter_is_deterministic_for_known_bins() {
463
+ let mut alpha = [1.0, 0.0, 0.0];
464
+
465
+ hmm_forward_step(&mut alpha, 0, &HMM_TRANSITION, &HMM_EMISSION);
466
+ assert_eq!(hmm_argmax(&alpha), HmmState::Normal);
467
+
468
+ for _ in 0..8 {
469
+ hmm_forward_step(&mut alpha, 2, &HMM_TRANSITION, &HMM_EMISSION);
470
+ }
471
+
472
+ assert_eq!(hmm_argmax(&alpha), HmmState::Persistent);
473
+ }
@@ -0,0 +1,175 @@
1
+ // Copyright (C) 2026, 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::Duration;
28
+ use std::time::Instant;
29
+
30
+ use crate::recovery::gcongestion::BbrRttJumpDetector;
31
+
32
+ mod global_min;
33
+ mod hmm;
34
+
35
+ use self::global_min::GlobalMinDetector;
36
+ use self::hmm::HmmDetector;
37
+
38
+ #[derive(Debug)]
39
+ pub(super) struct RttJumpDetector {
40
+ /// Total number of confirmed persistent RTT jump episodes.
41
+ persistent_jump_count: u64,
42
+ /// The start time of the most recently confirmed persistent RTT jump
43
+ /// episode, if any.
44
+ last_persistent_jump_time: Option<Instant>,
45
+ /// The detector selected for this connection.
46
+ detector: ActiveRttJumpDetector,
47
+ }
48
+
49
+ #[derive(Debug)]
50
+ enum ActiveRttJumpDetector {
51
+ Disabled,
52
+ GlobalMin(GlobalMinDetector),
53
+ Hmm(HmmDetector),
54
+ }
55
+
56
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
57
+ enum RttJumpUpdate {
58
+ None,
59
+ PersistentConfirmed { episode_start_time: Instant },
60
+ }
61
+
62
+ impl RttJumpDetector {
63
+ pub(super) fn new(mode: BbrRttJumpDetector) -> Self {
64
+ let detector = match mode {
65
+ BbrRttJumpDetector::Disabled => ActiveRttJumpDetector::Disabled,
66
+ BbrRttJumpDetector::GlobalMin => {
67
+ ActiveRttJumpDetector::GlobalMin(GlobalMinDetector::default())
68
+ }
69
+ BbrRttJumpDetector::Hmm => ActiveRttJumpDetector::Hmm(HmmDetector::default()),
70
+ };
71
+
72
+ Self {
73
+ persistent_jump_count: 0,
74
+ last_persistent_jump_time: None,
75
+ detector,
76
+ }
77
+ }
78
+
79
+ /// Runs the connection's selected RTT jump detector for one RTT sample.
80
+ pub(super) fn on_rtt_sample(
81
+ &mut self,
82
+ rtt_sample: Duration,
83
+ event_time: Instant,
84
+ full_bandwidth_reached: bool,
85
+ ) {
86
+ let update = match &mut self.detector {
87
+ ActiveRttJumpDetector::Disabled => RttJumpUpdate::None,
88
+ ActiveRttJumpDetector::GlobalMin(detector) => {
89
+ detector.on_rtt_sample(rtt_sample, event_time, full_bandwidth_reached)
90
+ }
91
+ ActiveRttJumpDetector::Hmm(detector) => {
92
+ detector.on_rtt_sample(rtt_sample, event_time, full_bandwidth_reached)
93
+ }
94
+ };
95
+
96
+ self.apply_update(update);
97
+ }
98
+
99
+ fn apply_update(&mut self, update: RttJumpUpdate) {
100
+ match update {
101
+ RttJumpUpdate::None => {}
102
+ RttJumpUpdate::PersistentConfirmed { episode_start_time } => {
103
+ self.persistent_jump_count += 1;
104
+ self.last_persistent_jump_time = Some(episode_start_time);
105
+ }
106
+ }
107
+ }
108
+
109
+ /// Total number of confirmed persistent RTT jump episodes.
110
+ pub(super) fn rtt_persistent_jump_count(&self) -> u64 {
111
+ self.persistent_jump_count
112
+ }
113
+
114
+ /// The start time of the most recently confirmed persistent RTT jump
115
+ /// episode, if any.
116
+ #[cfg(test)]
117
+ pub(super) fn last_persistent_jump_time(&self) -> Option<Instant> {
118
+ self.last_persistent_jump_time
119
+ }
120
+
121
+ /// Whether an RTT jump episode is currently active.
122
+ #[cfg(test)]
123
+ pub(super) fn is_rtt_jump_active(&self) -> bool {
124
+ match &self.detector {
125
+ ActiveRttJumpDetector::Disabled => false,
126
+ ActiveRttJumpDetector::GlobalMin(detector) => detector.is_rtt_jump_active(),
127
+ ActiveRttJumpDetector::Hmm(detector) => detector.is_rtt_jump_active(),
128
+ }
129
+ }
130
+
131
+ /// Whether the current RTT jump episode has been confirmed as persistent.
132
+ #[cfg(test)]
133
+ pub(super) fn is_rtt_jump_persistent(&self) -> bool {
134
+ match &self.detector {
135
+ ActiveRttJumpDetector::Disabled => false,
136
+ ActiveRttJumpDetector::GlobalMin(detector) => detector.is_rtt_jump_persistent(),
137
+ ActiveRttJumpDetector::Hmm(detector) => detector.is_rtt_jump_persistent(),
138
+ }
139
+ }
140
+ }
141
+
142
+ #[cfg(test)]
143
+ mod tests {
144
+ use std::time::Duration;
145
+ use std::time::Instant;
146
+
147
+ use crate::recovery::gcongestion::BbrRttJumpDetector;
148
+
149
+ use super::*;
150
+
151
+ const RTT_JUMP: Duration = Duration::from_millis(151);
152
+
153
+ fn ms(millis: u64) -> Duration {
154
+ Duration::from_millis(millis)
155
+ }
156
+
157
+ fn sample(detector: &mut RttJumpDetector, base: Instant, offset: Duration, rtt: Duration) {
158
+ detector.on_rtt_sample(rtt, base + offset, true);
159
+ }
160
+
161
+ #[test]
162
+ fn disabled_detector_does_not_store_active_behavior() {
163
+ let mut detector = RttJumpDetector::new(BbrRttJumpDetector::Disabled);
164
+ let base = Instant::now();
165
+
166
+ for pkt in 1..20 {
167
+ sample(&mut detector, base, ms(pkt * 100), RTT_JUMP);
168
+ }
169
+
170
+ assert!(!detector.is_rtt_jump_active());
171
+ assert!(!detector.is_rtt_jump_persistent());
172
+ assert_eq!(detector.rtt_persistent_jump_count(), 0);
173
+ assert_eq!(detector.last_persistent_jump_time(), None);
174
+ }
175
+ }