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,729 @@
1
+ // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style license that can be
3
+ // found in the LICENSE file.
4
+
5
+ // Copyright (C) 2023, Cloudflare, Inc.
6
+ // All rights reserved.
7
+ //
8
+ // Redistribution and use in source and binary forms, with or without
9
+ // modification, are permitted provided that the following conditions are
10
+ // met:
11
+ //
12
+ // * Redistributions of source code must retain the above copyright notice,
13
+ // this list of conditions and the following disclaimer.
14
+ //
15
+ // * Redistributions in binary form must reproduce the above copyright
16
+ // notice, this list of conditions and the following disclaimer in the
17
+ // documentation and/or other materials provided with the distribution.
18
+ //
19
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20
+ // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21
+ // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22
+ // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23
+ // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
+ // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
+ // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
+ // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
+ // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
+ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
+ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+
31
+ use std::ops::Add;
32
+ use std::time::Duration;
33
+ use std::time::Instant;
34
+
35
+ use crate::recovery::gcongestion::bbr2::Params;
36
+ use crate::recovery::gcongestion::Acked;
37
+ use crate::recovery::gcongestion::Lost;
38
+ use crate::recovery::RecoveryStats;
39
+
40
+ use super::mode::Cycle;
41
+ use super::mode::CyclePhase;
42
+ use super::mode::Mode;
43
+ use super::mode::ModeImpl;
44
+ use super::network_model::BBRv2NetworkModel;
45
+ use super::network_model::DEFAULT_MSS;
46
+ use super::BBRv2CongestionEvent;
47
+ use super::BwLoMode;
48
+ use super::Limits;
49
+
50
+ #[derive(Debug)]
51
+ pub(super) struct ProbeBW {
52
+ pub(super) model: BBRv2NetworkModel,
53
+ pub(super) cycle: Cycle,
54
+ }
55
+
56
+ #[derive(PartialEq, PartialOrd)]
57
+ enum AdaptUpperBoundsResult {
58
+ AdaptedOk,
59
+ AdaptedProbedTooHigh,
60
+ NotAdaptedInflightHighNotSet,
61
+ NotAdaptedInvalidSample,
62
+ }
63
+
64
+ impl ModeImpl for ProbeBW {
65
+ #[cfg(feature = "qlog")]
66
+ fn state_str(&self) -> &'static str {
67
+ match self.cycle.phase {
68
+ CyclePhase::NotStarted => unreachable!(),
69
+ CyclePhase::Up => "bbr_probe_bw_up",
70
+ CyclePhase::Down => "bbr_probe_bw_down",
71
+ CyclePhase::Cruise => "bbr_probe_bw_cruise",
72
+ CyclePhase::Refill => "bbr_probe_bw_refill",
73
+ }
74
+ }
75
+
76
+ fn enter(
77
+ &mut self, now: Instant,
78
+ _congestion_event: Option<&BBRv2CongestionEvent>, params: &Params,
79
+ ) {
80
+ self.cycle.start_time = now;
81
+
82
+ match self.cycle.phase {
83
+ CyclePhase::NotStarted => {
84
+ // First time entering PROBE_BW. Start a new probing cycle.
85
+ self.enter_probe_down(false, false, now, params)
86
+ },
87
+ CyclePhase::Cruise => self.enter_probe_cruise(now),
88
+ CyclePhase::Refill =>
89
+ self.enter_probe_refill(self.cycle.probe_up_rounds, now),
90
+ CyclePhase::Up | CyclePhase::Down => {},
91
+ }
92
+ }
93
+
94
+ fn on_congestion_event(
95
+ mut self, prior_in_flight: usize, event_time: Instant, _: &[Acked],
96
+ _: &[Lost], congestion_event: &mut BBRv2CongestionEvent,
97
+ target_bytes_inflight: usize, params: &Params,
98
+ _recovery_stats: &mut RecoveryStats, _cwnd: usize,
99
+ ) -> Mode {
100
+ if congestion_event.end_of_round_trip {
101
+ if self.cycle.start_time != event_time {
102
+ self.cycle.rounds_since_probe += 1;
103
+ }
104
+
105
+ if self.cycle.phase_start_time != event_time {
106
+ self.cycle.rounds_in_phase += 1;
107
+ }
108
+ }
109
+
110
+ let mut switch_to_probe_rtt = false;
111
+
112
+ match self.cycle.phase {
113
+ CyclePhase::NotStarted => unreachable!(),
114
+ CyclePhase::Up => self.update_probe_up(
115
+ prior_in_flight,
116
+ target_bytes_inflight,
117
+ congestion_event,
118
+ params,
119
+ ),
120
+ CyclePhase::Down => {
121
+ self.update_probe_down(
122
+ target_bytes_inflight,
123
+ congestion_event,
124
+ params,
125
+ );
126
+ if self.cycle.phase != CyclePhase::Down &&
127
+ self.model.maybe_expire_min_rtt(congestion_event, params)
128
+ {
129
+ switch_to_probe_rtt = true;
130
+ }
131
+ },
132
+ CyclePhase::Cruise => self.update_probe_cruise(
133
+ target_bytes_inflight,
134
+ congestion_event,
135
+ params,
136
+ ),
137
+ CyclePhase::Refill => self.update_probe_refill(
138
+ target_bytes_inflight,
139
+ congestion_event,
140
+ params,
141
+ ),
142
+ }
143
+
144
+ // Do not need to set the gains if switching to PROBE_RTT, they will be
145
+ // set when `ProbeRTT::enter` is called.
146
+ if !switch_to_probe_rtt {
147
+ self.model
148
+ .set_pacing_gain(self.cycle.phase.pacing_gain(params));
149
+ self.model.set_cwnd_gain(self.cycle.phase.cwnd_gain(params));
150
+ }
151
+
152
+ if switch_to_probe_rtt {
153
+ self.into_probe_rtt(event_time, Some(congestion_event), params)
154
+ } else {
155
+ Mode::ProbeBW(self)
156
+ }
157
+ }
158
+
159
+ fn get_cwnd_limits(&self, params: &Params) -> Limits<usize> {
160
+ if self.cycle.phase == CyclePhase::Cruise {
161
+ let limit = self
162
+ .model
163
+ .inflight_lo()
164
+ .min(self.model.inflight_hi_with_headroom(params));
165
+ return Limits::no_greater_than(limit);
166
+ }
167
+
168
+ if self.cycle.phase == CyclePhase::Up &&
169
+ params.probe_up_ignore_inflight_hi
170
+ {
171
+ // Similar to STARTUP.
172
+ return Limits::no_greater_than(self.model.inflight_lo());
173
+ }
174
+
175
+ Limits::no_greater_than(
176
+ self.model.inflight_lo().min(self.model.inflight_hi()),
177
+ )
178
+ }
179
+
180
+ fn is_probing_for_bandwidth(&self) -> bool {
181
+ self.cycle.phase == CyclePhase::Refill ||
182
+ self.cycle.phase == CyclePhase::Up
183
+ }
184
+
185
+ fn on_exit_quiescence(
186
+ mut self, now: Instant, quiescence_start_time: Instant, _params: &Params,
187
+ ) -> Mode {
188
+ self.model
189
+ .postpone_min_rtt_timestamp(now - quiescence_start_time);
190
+ Mode::ProbeBW(self)
191
+ }
192
+
193
+ fn leave(
194
+ &mut self, _now: Instant,
195
+ _congestion_event: Option<&BBRv2CongestionEvent>,
196
+ ) {
197
+ }
198
+ }
199
+
200
+ impl ProbeBW {
201
+ fn enter_probe_down(
202
+ &mut self, probed_too_high: bool, stopped_risky_probe: bool,
203
+ now: Instant, params: &Params,
204
+ ) {
205
+ let cycle = &mut self.cycle;
206
+ cycle.last_cycle_probed_too_high = probed_too_high;
207
+ cycle.last_cycle_stopped_risky_probe = stopped_risky_probe;
208
+
209
+ cycle.phase = CyclePhase::Down;
210
+ cycle.start_time = now;
211
+ cycle.phase_start_time = now;
212
+ cycle.rounds_in_phase = 0;
213
+
214
+ if params.bw_lo_mode != BwLoMode::Default {
215
+ // Clear bandwidth lo if it was set in PROBE_UP, because losses in
216
+ // PROBE_UP should not permanently change bandwidth_lo.
217
+ // It's possible for bandwidth_lo to be set during REFILL, but if that
218
+ // was a valid value, it'll quickly be rediscovered.
219
+ self.model.clear_bandwidth_lo();
220
+ }
221
+
222
+ // Pick probe wait time.
223
+ // TODO(vlad): actually pick time
224
+ cycle.rounds_since_probe = 0;
225
+ cycle.probe_wait_time = Some(
226
+ params.probe_bw_probe_base_duration + Duration::from_micros(500),
227
+ );
228
+
229
+ cycle.probe_up_bytes = None;
230
+ cycle.probe_up_app_limited_since_inflight_hi_limited = false;
231
+ cycle.has_advanced_max_bw = false;
232
+ self.model.restart_round_early();
233
+ }
234
+
235
+ fn enter_probe_cruise(&mut self, now: Instant) {
236
+ if self.cycle.phase == CyclePhase::Down {
237
+ self.exit_probe_down();
238
+ }
239
+
240
+ let cycle = &mut self.cycle;
241
+
242
+ self.model.cap_inflight_lo(self.model.inflight_hi());
243
+ cycle.phase = CyclePhase::Cruise;
244
+ cycle.phase_start_time = now;
245
+ cycle.rounds_in_phase = 0;
246
+ cycle.is_sample_from_probing = false;
247
+ }
248
+
249
+ fn enter_probe_refill(&mut self, probe_up_rounds: usize, now: Instant) {
250
+ if self.cycle.phase == CyclePhase::Down {
251
+ self.exit_probe_down();
252
+ }
253
+
254
+ let cycle = &mut self.cycle;
255
+
256
+ cycle.phase = CyclePhase::Refill;
257
+ cycle.phase_start_time = now;
258
+ cycle.rounds_in_phase = 0;
259
+
260
+ cycle.is_sample_from_probing = false;
261
+ cycle.last_cycle_stopped_risky_probe = false;
262
+
263
+ self.model.clear_bandwidth_lo();
264
+ self.model.clear_inflight_lo();
265
+ cycle.probe_up_rounds = probe_up_rounds;
266
+ cycle.probe_up_acked = 0;
267
+ self.model.restart_round_early();
268
+ }
269
+
270
+ fn enter_probe_up(&mut self, now: Instant, cwnd: usize) {
271
+ let cycle = &mut self.cycle;
272
+
273
+ cycle.phase = CyclePhase::Up;
274
+ cycle.phase_start_time = now;
275
+ cycle.rounds_in_phase = 0;
276
+ cycle.is_sample_from_probing = true;
277
+ self.raise_inflight_high_slope(cwnd);
278
+ self.model.restart_round_early();
279
+ }
280
+
281
+ fn exit_probe_down(&mut self) {
282
+ if !self.cycle.has_advanced_max_bw {
283
+ self.model.advance_max_bandwidth_filter();
284
+ self.cycle.has_advanced_max_bw = true;
285
+ }
286
+ }
287
+
288
+ fn update_probe_down(
289
+ &mut self, target_bytes_inflight: usize,
290
+ congestion_event: &BBRv2CongestionEvent, params: &Params,
291
+ ) {
292
+ if self.cycle.rounds_in_phase == 1 && congestion_event.end_of_round_trip {
293
+ self.cycle.is_sample_from_probing = false;
294
+
295
+ if !congestion_event.last_packet_send_state.is_app_limited {
296
+ self.model.advance_max_bandwidth_filter();
297
+ self.cycle.has_advanced_max_bw = true;
298
+ }
299
+
300
+ if self.cycle.last_cycle_stopped_risky_probe &&
301
+ !self.cycle.last_cycle_probed_too_high
302
+ {
303
+ self.enter_probe_refill(0, congestion_event.event_time);
304
+ return;
305
+ }
306
+ }
307
+
308
+ self.maybe_adapt_upper_bounds(
309
+ target_bytes_inflight,
310
+ congestion_event,
311
+ params,
312
+ );
313
+
314
+ if self.is_time_to_probe_bandwidth(
315
+ target_bytes_inflight,
316
+ congestion_event,
317
+ params,
318
+ ) {
319
+ self.enter_probe_refill(0, congestion_event.event_time);
320
+ return;
321
+ }
322
+
323
+ // This exit condition is experimental code from Google quiche which
324
+ // diverges from the RFC. Use `disable_probe_down_early_exit` to override
325
+ // the behavior.
326
+ if self.has_stayed_long_enough_in_probe_down(congestion_event, params) {
327
+ self.enter_probe_cruise(congestion_event.event_time);
328
+ return;
329
+ }
330
+
331
+ let inflight_with_headroom = self.model.inflight_hi_with_headroom(params);
332
+ let bytes_in_flight = congestion_event.bytes_in_flight;
333
+
334
+ if bytes_in_flight > inflight_with_headroom {
335
+ // Stay in PROBE_DOWN.
336
+ return;
337
+ }
338
+
339
+ // Transition to PROBE_CRUISE iff we've drained to target.
340
+ let bdp = self.model.bdp0();
341
+
342
+ if bytes_in_flight < bdp {
343
+ self.enter_probe_cruise(congestion_event.event_time);
344
+ }
345
+ }
346
+
347
+ fn update_probe_cruise(
348
+ &mut self, target_bytes_inflight: usize,
349
+ congestion_event: &BBRv2CongestionEvent, params: &Params,
350
+ ) {
351
+ self.maybe_adapt_upper_bounds(
352
+ target_bytes_inflight,
353
+ congestion_event,
354
+ params,
355
+ );
356
+
357
+ if self.is_time_to_probe_bandwidth(
358
+ target_bytes_inflight,
359
+ congestion_event,
360
+ params,
361
+ ) {
362
+ self.enter_probe_refill(0, congestion_event.event_time);
363
+ }
364
+ }
365
+
366
+ fn update_probe_refill(
367
+ &mut self, target_bytes_inflight: usize,
368
+ congestion_event: &BBRv2CongestionEvent, params: &Params,
369
+ ) {
370
+ self.maybe_adapt_upper_bounds(
371
+ target_bytes_inflight,
372
+ congestion_event,
373
+ params,
374
+ );
375
+
376
+ if self.cycle.rounds_in_phase > 0 && congestion_event.end_of_round_trip {
377
+ self.enter_probe_up(
378
+ congestion_event.event_time,
379
+ congestion_event.prior_cwnd,
380
+ );
381
+ }
382
+ }
383
+
384
+ fn update_probe_up(
385
+ &mut self, prior_in_flight: usize, target_bytes_inflight: usize,
386
+ congestion_event: &BBRv2CongestionEvent, params: &Params,
387
+ ) {
388
+ if self.maybe_adapt_upper_bounds(
389
+ target_bytes_inflight,
390
+ congestion_event,
391
+ params,
392
+ ) == AdaptUpperBoundsResult::AdaptedProbedTooHigh
393
+ {
394
+ self.enter_probe_down(
395
+ true,
396
+ false,
397
+ congestion_event.event_time,
398
+ params,
399
+ );
400
+ return;
401
+ }
402
+
403
+ self.probe_inflight_high_upward(congestion_event, params);
404
+
405
+ let mut is_risky = false;
406
+ let mut is_queuing = false;
407
+ if self.cycle.last_cycle_probed_too_high &&
408
+ prior_in_flight >= self.model.inflight_hi()
409
+ {
410
+ is_risky = true;
411
+ } else if self.cycle.rounds_in_phase > 0 {
412
+ if params.max_probe_up_queue_rounds > 0 {
413
+ if congestion_event.end_of_round_trip {
414
+ self.model
415
+ .check_persistent_queue(params.full_bw_threshold, params);
416
+ if self.model.rounds_with_queueing() >=
417
+ params.max_probe_up_queue_rounds
418
+ {
419
+ is_queuing = true;
420
+ }
421
+ }
422
+ } else {
423
+ let mut queuing_threshold_extra_bytes =
424
+ self.model.queueing_threshold_extra_bytes();
425
+ if params.add_ack_height_to_queueing_threshold {
426
+ queuing_threshold_extra_bytes += self.model.max_ack_height();
427
+ }
428
+ let queuing_threshold = (params.full_bw_threshold *
429
+ self.model.bdp0() as f32)
430
+ as usize +
431
+ queuing_threshold_extra_bytes;
432
+
433
+ is_queuing =
434
+ congestion_event.bytes_in_flight >= queuing_threshold;
435
+ }
436
+ }
437
+
438
+ if is_risky || is_queuing {
439
+ self.enter_probe_down(
440
+ false,
441
+ is_risky,
442
+ congestion_event.event_time,
443
+ params,
444
+ );
445
+ }
446
+ }
447
+
448
+ fn is_time_to_probe_bandwidth(
449
+ &self, target_bytes_inflight: usize,
450
+ congestion_event: &BBRv2CongestionEvent, params: &Params,
451
+ ) -> bool {
452
+ if self.has_cycle_lasted(
453
+ self.cycle.probe_wait_time.unwrap(),
454
+ congestion_event,
455
+ ) {
456
+ return true;
457
+ }
458
+
459
+ if self.is_time_to_probe_for_reno_coexistence(
460
+ target_bytes_inflight,
461
+ 1.0,
462
+ congestion_event,
463
+ params,
464
+ ) {
465
+ return true;
466
+ }
467
+
468
+ false
469
+ }
470
+
471
+ fn maybe_adapt_upper_bounds(
472
+ &mut self, target_bytes_inflight: usize,
473
+ congestion_event: &BBRv2CongestionEvent, params: &Params,
474
+ ) -> AdaptUpperBoundsResult {
475
+ let send_state = congestion_event.last_packet_send_state;
476
+
477
+ if !send_state.is_valid {
478
+ return AdaptUpperBoundsResult::NotAdaptedInvalidSample;
479
+ }
480
+
481
+ // TODO(vlad): use BytesInFlight?
482
+ let mut inflight_at_send = send_state.bytes_in_flight;
483
+ if params.use_bytes_delivered_for_inflight_hi {
484
+ inflight_at_send = self.model.total_bytes_acked() -
485
+ congestion_event.last_packet_send_state.total_bytes_acked;
486
+ }
487
+
488
+ if self.cycle.is_sample_from_probing {
489
+ if self.model.is_inflight_too_high(
490
+ congestion_event,
491
+ params.probe_bw_full_loss_count,
492
+ params,
493
+ ) {
494
+ self.cycle.is_sample_from_probing = false;
495
+ if !send_state.is_app_limited ||
496
+ params.max_probe_up_queue_rounds > 0
497
+ {
498
+ let inflight_target = (target_bytes_inflight as f32 *
499
+ (1.0 - params.beta))
500
+ as usize;
501
+
502
+ let mut new_inflight_hi =
503
+ inflight_at_send.max(inflight_target);
504
+
505
+ if params.limit_inflight_hi_by_max_delivered {
506
+ new_inflight_hi = self
507
+ .model
508
+ .max_bytes_delivered_in_round()
509
+ .max(new_inflight_hi);
510
+ }
511
+
512
+ self.model.set_inflight_hi(new_inflight_hi);
513
+ }
514
+ return AdaptUpperBoundsResult::AdaptedProbedTooHigh;
515
+ }
516
+ return AdaptUpperBoundsResult::AdaptedOk;
517
+ }
518
+
519
+ if self.model.inflight_hi() == self.model.inflight_hi_default() {
520
+ return AdaptUpperBoundsResult::NotAdaptedInflightHighNotSet;
521
+ }
522
+
523
+ // Raise the upper bound for inflight.
524
+ if inflight_at_send > self.model.inflight_hi() {
525
+ self.model.set_inflight_hi(inflight_at_send);
526
+ }
527
+
528
+ AdaptUpperBoundsResult::AdaptedOk
529
+ }
530
+
531
+ fn has_cycle_lasted(
532
+ &self, duration: Duration, congestion_event: &BBRv2CongestionEvent,
533
+ ) -> bool {
534
+ (congestion_event.event_time - self.cycle.start_time) > duration
535
+ }
536
+
537
+ fn has_phase_lasted(
538
+ &self, duration: Duration, congestion_event: &BBRv2CongestionEvent,
539
+ ) -> bool {
540
+ (congestion_event.event_time - self.cycle.phase_start_time) > duration
541
+ }
542
+
543
+ fn is_time_to_probe_for_reno_coexistence(
544
+ &self, target_bytes_inflight: usize, probe_wait_fraction: f64,
545
+ _congestion_event: &BBRv2CongestionEvent, params: &Params,
546
+ ) -> bool {
547
+ if !params.enable_reno_coexistence {
548
+ return false;
549
+ }
550
+
551
+ let mut rounds = params.probe_bw_probe_max_rounds;
552
+ if params.probe_bw_probe_reno_gain > 0.0 {
553
+ let reno_rounds = (params.probe_bw_probe_reno_gain *
554
+ target_bytes_inflight as f32 /
555
+ DEFAULT_MSS as f32) as usize;
556
+ rounds = reno_rounds.min(rounds);
557
+ }
558
+
559
+ self.cycle.rounds_since_probe >=
560
+ (rounds as f64 * probe_wait_fraction) as usize
561
+ }
562
+
563
+ // Used to prevent a BBR2 flow from staying in PROBE_DOWN for too
564
+ // long, as seen in some multi-sender simulator tests.
565
+ //
566
+ // This is experimental code from Google quiche and diverges from the RFC. Use
567
+ // `disable_probe_down_early_exit` to override the behavior.
568
+ // - RFC: https://www.ietf.org/archive/id/draft-ietf-ccwg-bbr-02.html#name-probebw_down
569
+ // - Google quiche: https://github.com/google/quiche/blob/b370e7a/quiche/quic/core/congestion_control/bbr2_probe_bw.cc#L142
570
+ fn has_stayed_long_enough_in_probe_down(
571
+ &self, congestion_event: &BBRv2CongestionEvent, params: &Params,
572
+ ) -> bool {
573
+ if params.disable_probe_down_early_exit {
574
+ return false;
575
+ }
576
+
577
+ // Stay in PROBE_DOWN for at most the time of a min rtt, as it is done
578
+ // in BBRv1.
579
+ self.has_phase_lasted(self.model.min_rtt(), congestion_event)
580
+ }
581
+
582
+ fn raise_inflight_high_slope(&mut self, cwnd: usize) {
583
+ let growth_this_round = 1usize << self.cycle.probe_up_rounds;
584
+ // The number 30 below means `growth_this_round` is capped at 1G and the
585
+ // lower bound of `probe_up_bytes` is (practically) 1 mss, at this
586
+ // speed `inflight_hi`` grows by approximately 1 packet per packet acked.
587
+ self.cycle.probe_up_rounds = self.cycle.probe_up_rounds.add(1).min(30);
588
+ let probe_up_bytes = cwnd / growth_this_round;
589
+ self.cycle.probe_up_bytes = Some(probe_up_bytes.max(DEFAULT_MSS));
590
+ }
591
+
592
+ fn probe_inflight_high_upward(
593
+ &mut self, congestion_event: &BBRv2CongestionEvent, params: &Params,
594
+ ) {
595
+ if params.probe_up_ignore_inflight_hi {
596
+ // When inflight_hi is disabled in PROBE_UP, it increases when
597
+ // the number of bytes delivered in a round is larger inflight_hi.
598
+ return;
599
+ } else {
600
+ // TODO(vlad): probe_up_simplify_inflight_hi?
601
+ if congestion_event.prior_bytes_in_flight <
602
+ congestion_event.prior_cwnd
603
+ {
604
+ // Not fully utilizing cwnd, so can't safely grow.
605
+ return;
606
+ }
607
+
608
+ if congestion_event.prior_cwnd < self.model.inflight_hi() {
609
+ // Not fully using inflight_hi, so don't grow it.
610
+ return;
611
+ }
612
+
613
+ self.cycle.probe_up_acked += congestion_event.bytes_acked;
614
+ }
615
+
616
+ if let Some(probe_up_bytes) = self.cycle.probe_up_bytes {
617
+ if self.cycle.probe_up_acked >= probe_up_bytes {
618
+ let delta = self.cycle.probe_up_acked / probe_up_bytes;
619
+ // probe_up_acked is set to the remainder mod probe_up_bytes;
620
+ // probe_up_acked >= delta * probe_up_bytes so this
621
+ // can't underflow.
622
+ self.cycle.probe_up_acked -= delta * probe_up_bytes;
623
+ let new_inflight_hi =
624
+ self.model.inflight_hi() + delta * DEFAULT_MSS;
625
+ if new_inflight_hi > self.model.inflight_hi() {
626
+ self.model.set_inflight_hi(new_inflight_hi);
627
+ }
628
+ }
629
+ }
630
+
631
+ if congestion_event.end_of_round_trip {
632
+ self.raise_inflight_high_slope(congestion_event.prior_cwnd);
633
+ }
634
+ }
635
+
636
+ fn into_probe_rtt(
637
+ mut self, now: Instant, congestion_event: Option<&BBRv2CongestionEvent>,
638
+ params: &Params,
639
+ ) -> Mode {
640
+ self.leave(now, congestion_event);
641
+ let mut next_mode = Mode::probe_rtt(self.model, self.cycle);
642
+ next_mode.enter(now, congestion_event, params);
643
+ next_mode
644
+ }
645
+ }
646
+
647
+ #[cfg(test)]
648
+ mod tests {
649
+ use rstest::rstest;
650
+
651
+ use super::*;
652
+ use crate::recovery::gcongestion::bbr2::SendTimeState;
653
+ use crate::recovery::gcongestion::bbr2::DEFAULT_PARAMS;
654
+
655
+ #[rstest]
656
+ fn probe_upward(#[values(100, 10_000, 65_536, 300_000)] step: usize) {
657
+ let test_event =
658
+ |probe_bw: &ProbeBW, bytes_acked: usize, end_of_round_trip: bool| {
659
+ BBRv2CongestionEvent {
660
+ event_time: probe_bw.cycle.start_time,
661
+ prior_cwnd: probe_bw.model.inflight_hi(),
662
+ prior_bytes_in_flight: probe_bw.model.inflight_hi(),
663
+ bytes_in_flight: 0,
664
+ bytes_acked,
665
+ bytes_lost: 0,
666
+ end_of_round_trip,
667
+ is_probing_for_bandwidth: true,
668
+ sample_max_bandwidth: None,
669
+ sample_min_rtt: None,
670
+ last_packet_send_state: SendTimeState::default(),
671
+ }
672
+ };
673
+
674
+ let do_probe_up = |probe_bw: &mut ProbeBW,
675
+ params: &Params,
676
+ total_bytes: usize| {
677
+ let mut remaining = total_bytes;
678
+ loop {
679
+ let congestion_event =
680
+ test_event(probe_bw, step.min(remaining), false);
681
+
682
+ probe_bw.probe_inflight_high_upward(&congestion_event, params);
683
+
684
+ if remaining > step {
685
+ remaining -= step;
686
+ } else {
687
+ break;
688
+ }
689
+ }
690
+ };
691
+
692
+ let params = &DEFAULT_PARAMS;
693
+ let model = BBRv2NetworkModel::new(params, Duration::from_millis(333));
694
+ let cycle = Cycle::default();
695
+ let mut probe_bw = ProbeBW { model, cycle };
696
+ probe_bw.model.set_inflight_hi(100_000);
697
+ probe_bw.raise_inflight_high_slope(100_000);
698
+ assert_eq!(probe_bw.cycle.probe_up_rounds, 1);
699
+ assert_eq!(probe_bw.cycle.probe_up_bytes, Some(100_000));
700
+
701
+ assert_eq!(probe_bw.model.inflight_hi(), 100_000);
702
+ do_probe_up(&mut probe_bw, params, 1_000_000);
703
+ // End inflight_hi should be independent of step size.
704
+ assert_eq!(probe_bw.model.inflight_hi(), 113_000);
705
+
706
+ // Slope is increased at the end of round by decreasing probe_up_bytes.
707
+ probe_bw.probe_inflight_high_upward(
708
+ &test_event(&probe_bw, 10_000, true),
709
+ params,
710
+ );
711
+ assert_eq!(probe_bw.cycle.probe_up_rounds, 2);
712
+ assert_eq!(probe_bw.cycle.probe_up_bytes, Some(56500));
713
+
714
+ do_probe_up(&mut probe_bw, params, 1_000_000);
715
+ // End inflight_hi should be independent of step size.
716
+ assert_eq!(probe_bw.model.inflight_hi(), 135_100);
717
+
718
+ probe_bw.probe_inflight_high_upward(
719
+ &test_event(&probe_bw, 10_000, true),
720
+ params,
721
+ );
722
+ assert_eq!(probe_bw.cycle.probe_up_rounds, 3);
723
+ assert_eq!(probe_bw.cycle.probe_up_bytes, Some(33775));
724
+
725
+ do_probe_up(&mut probe_bw, params, 1_000_000);
726
+ // End inflight_hi should be independent of step size.
727
+ assert_eq!(probe_bw.model.inflight_hi(), 174_100);
728
+ }
729
+ }