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,784 @@
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::bbr::BandwidthSampler;
36
+ use crate::recovery::gcongestion::bbr2::Params;
37
+ use crate::recovery::gcongestion::Bandwidth;
38
+ use crate::recovery::gcongestion::Lost;
39
+
40
+ use super::Acked;
41
+ use super::BBRv2CongestionEvent;
42
+ use super::BwLoMode;
43
+
44
+ pub(super) const DEFAULT_MSS: usize = 1300;
45
+
46
+ #[derive(Debug)]
47
+ struct RoundTripCounter {
48
+ round_trip_count: usize,
49
+ last_sent_packet: u64,
50
+ // The last sent packet number of the current round trip.
51
+ end_of_round_trip: Option<u64>,
52
+ }
53
+
54
+ impl RoundTripCounter {
55
+ /// Must be called in ascending packet number order.
56
+ fn on_packet_sent(&mut self, packet_number: u64) {
57
+ self.last_sent_packet = packet_number;
58
+ }
59
+
60
+ /// Return whether a round trip has just completed.
61
+ fn on_packets_acked(&mut self, last_acked_packet: u64) -> bool {
62
+ match self.end_of_round_trip {
63
+ Some(pkt) if last_acked_packet <= pkt => false,
64
+ _ => {
65
+ self.round_trip_count += 1;
66
+ self.end_of_round_trip = Some(self.last_sent_packet);
67
+ true
68
+ },
69
+ }
70
+ }
71
+
72
+ fn restart_round(&mut self) {
73
+ self.end_of_round_trip = Some(self.last_sent_packet)
74
+ }
75
+ }
76
+
77
+ #[derive(Debug)]
78
+ struct MinRttFilter {
79
+ min_rtt: Duration,
80
+ min_rtt_timestamp: Instant,
81
+ }
82
+
83
+ impl MinRttFilter {
84
+ fn get(&self) -> Duration {
85
+ self.min_rtt
86
+ }
87
+
88
+ fn get_timestamps(&self) -> Instant {
89
+ self.min_rtt_timestamp
90
+ }
91
+
92
+ fn update(&mut self, sample_rtt: Duration, now: Instant) {
93
+ if sample_rtt < self.min_rtt {
94
+ self.min_rtt = sample_rtt;
95
+ self.min_rtt_timestamp = now;
96
+ }
97
+ }
98
+
99
+ fn force_update(&mut self, sample_rtt: Duration, now: Instant) {
100
+ self.min_rtt = sample_rtt;
101
+ self.min_rtt_timestamp = now;
102
+ }
103
+ }
104
+
105
+ #[derive(Debug)]
106
+ struct MaxBandwidthFilter {
107
+ max_bandwidth: [Bandwidth; 2],
108
+ }
109
+
110
+ impl MaxBandwidthFilter {
111
+ fn get(&self) -> Bandwidth {
112
+ self.max_bandwidth[0].max(self.max_bandwidth[1])
113
+ }
114
+
115
+ fn update(&mut self, sample: Bandwidth) {
116
+ self.max_bandwidth[1] = self.max_bandwidth[1].max(sample);
117
+ }
118
+
119
+ fn advance(&mut self) {
120
+ if self.max_bandwidth[1] == Bandwidth::zero() {
121
+ return;
122
+ }
123
+
124
+ self.max_bandwidth[0] = self.max_bandwidth[1];
125
+ self.max_bandwidth[1] = Bandwidth::zero();
126
+ }
127
+ }
128
+
129
+ /// Bbr2NetworkModel takes low level congestion signals(packets sent/acked/lost)
130
+ /// as input and produces BBRv2 model parameters like inflight_(hi|lo),
131
+ /// bandwidth_(hi|lo), bandwidth and rtt estimates, etc.
132
+ #[derive(Debug)]
133
+ pub(super) struct BBRv2NetworkModel {
134
+ round_trip_counter: RoundTripCounter,
135
+ /// Bandwidth sampler provides BBR with the bandwidth measurements at
136
+ /// individual points.
137
+ bandwidth_sampler: BandwidthSampler,
138
+ /// The filter that tracks the maximum bandwidth over multiple recent round
139
+ /// trips.
140
+ max_bandwidth_filter: MaxBandwidthFilter,
141
+ min_rtt_filter: MinRttFilter,
142
+ /// Bytes lost in the current round. Updated once per congestion event.
143
+ bytes_lost_in_round: usize,
144
+ /// Number of loss marking events in the current round.
145
+ loss_events_in_round: usize,
146
+
147
+ /// A max of bytes delivered among all congestion events in the current
148
+ /// round. A congestions event's bytes delivered is the total bytes
149
+ /// acked between time Ts and Ta, which is the time when the largest
150
+ /// acked packet(within the congestion event) was sent and acked,
151
+ /// respectively.
152
+ max_bytes_delivered_in_round: usize,
153
+
154
+ /// The minimum bytes in flight during this round.
155
+ min_bytes_in_flight_in_round: usize,
156
+ /// True if sending was limited by inflight_hi anytime in the current round.
157
+ inflight_hi_limited_in_round: bool,
158
+
159
+ /// Max bandwidth in the current round. Updated once per congestion event.
160
+ bandwidth_latest: Bandwidth,
161
+ /// Max bandwidth of recent rounds. Updated once per round.
162
+ bandwidth_lo: Option<Bandwidth>,
163
+ prior_bandwidth_lo: Option<Bandwidth>,
164
+
165
+ /// Max inflight in the current round. Updated once per congestion event.
166
+ inflight_latest: usize,
167
+ /// Max inflight of recent rounds. Updated once per round.
168
+ inflight_lo: usize,
169
+ inflight_hi: usize,
170
+
171
+ cwnd_gain: f32,
172
+ pacing_gain: f32,
173
+
174
+ /// Whether we are cwnd limited prior to the start of the current
175
+ /// aggregation epoch.
176
+ cwnd_limited_before_aggregation_epoch: bool,
177
+
178
+ /// STARTUP-centric fields which experimentally used by PROBE_UP.
179
+ full_bandwidth_reached: bool,
180
+ full_bandwidth_baseline: Bandwidth,
181
+ rounds_without_bandwidth_growth: usize,
182
+
183
+ /// Used by STARTUP and PROBE_UP to decide when to exit.
184
+ rounds_with_queueing: usize,
185
+
186
+ /// Determines whether app limited rounds with no bandwidth growth count
187
+ /// towards the rounds threshold to exit startup.
188
+ ignore_app_limited_for_no_bandwidth_growth: bool,
189
+
190
+ /// The most recent send rate from the BandwidthSampler.
191
+ latest_send_rate: Option<Bandwidth>,
192
+ /// The most recent ack rate from the BandwidthSampler.
193
+ latest_ack_rate: Option<Bandwidth>,
194
+ }
195
+
196
+ impl BBRv2NetworkModel {
197
+ pub(super) fn new(params: &Params, initial_rtt: Duration) -> Self {
198
+ BBRv2NetworkModel {
199
+ min_bytes_in_flight_in_round: usize::MAX,
200
+ inflight_hi_limited_in_round: false,
201
+ bandwidth_sampler: BandwidthSampler::new(
202
+ params.initial_max_ack_height_filter_window,
203
+ params.enable_overestimate_avoidance,
204
+ params.choose_a0_point_fix,
205
+ ),
206
+ round_trip_counter: RoundTripCounter {
207
+ round_trip_count: 0,
208
+ last_sent_packet: 0,
209
+ end_of_round_trip: None,
210
+ },
211
+ min_rtt_filter: MinRttFilter {
212
+ min_rtt: initial_rtt,
213
+ min_rtt_timestamp: Instant::now(),
214
+ },
215
+ max_bandwidth_filter: MaxBandwidthFilter {
216
+ max_bandwidth: [Bandwidth::zero(), Bandwidth::zero()],
217
+ },
218
+ cwnd_limited_before_aggregation_epoch: false,
219
+ cwnd_gain: params.startup_cwnd_gain,
220
+ pacing_gain: params.startup_pacing_gain,
221
+ full_bandwidth_reached: false,
222
+ bytes_lost_in_round: 0,
223
+ loss_events_in_round: 0,
224
+ max_bytes_delivered_in_round: 0,
225
+ bandwidth_latest: Bandwidth::zero(),
226
+ bandwidth_lo: None,
227
+ prior_bandwidth_lo: None,
228
+ inflight_latest: 0,
229
+ inflight_lo: usize::MAX,
230
+ inflight_hi: usize::MAX,
231
+
232
+ full_bandwidth_baseline: Bandwidth::zero(),
233
+ rounds_without_bandwidth_growth: 0,
234
+ rounds_with_queueing: 0,
235
+
236
+ ignore_app_limited_for_no_bandwidth_growth: params
237
+ .ignore_app_limited_for_no_bandwidth_growth,
238
+
239
+ latest_send_rate: None,
240
+ latest_ack_rate: None,
241
+ }
242
+ }
243
+
244
+ #[cfg(feature = "qlog")]
245
+ pub(super) fn send_rate(&self) -> Option<Bandwidth> {
246
+ self.latest_send_rate
247
+ }
248
+
249
+ #[cfg(feature = "qlog")]
250
+ pub(super) fn ack_rate(&self) -> Option<Bandwidth> {
251
+ self.latest_ack_rate
252
+ }
253
+
254
+ pub(super) fn max_ack_height(&self) -> usize {
255
+ self.bandwidth_sampler.max_ack_height().unwrap_or(0)
256
+ }
257
+
258
+ pub(super) fn bandwidth_estimate(&self) -> Bandwidth {
259
+ match (self.bandwidth_lo, self.max_bandwidth()) {
260
+ (None, b) => b,
261
+ (Some(a), b) => a.min(b),
262
+ }
263
+ }
264
+
265
+ pub(super) fn bdp(&self, bandwidth: Bandwidth, gain: f32) -> usize {
266
+ (bandwidth * gain).to_bytes_per_period(self.min_rtt()) as usize
267
+ }
268
+
269
+ pub(super) fn bdp1(&self, bandwidth: Bandwidth) -> usize {
270
+ self.bdp(bandwidth, 1.0)
271
+ }
272
+
273
+ pub(super) fn bdp0(&self) -> usize {
274
+ self.bdp1(self.max_bandwidth())
275
+ }
276
+
277
+ pub(super) fn min_rtt(&self) -> Duration {
278
+ self.min_rtt_filter.get()
279
+ }
280
+
281
+ pub(super) fn min_rtt_timestamp(&self) -> Instant {
282
+ self.min_rtt_filter.get_timestamps()
283
+ }
284
+
285
+ pub(super) fn max_bandwidth(&self) -> Bandwidth {
286
+ self.max_bandwidth_filter.get()
287
+ }
288
+
289
+ pub(super) fn on_packet_sent(
290
+ &mut self, sent_time: Instant, bytes_in_flight: usize,
291
+ packet_number: u64, bytes: usize, is_retransmissible: bool,
292
+ ) {
293
+ // Updating the min here ensures a more realistic (0) value when flows
294
+ // exit quiescence.
295
+ self.min_bytes_in_flight_in_round =
296
+ self.min_bytes_in_flight_in_round.min(bytes_in_flight);
297
+
298
+ if bytes_in_flight + bytes >= self.inflight_hi {
299
+ self.inflight_hi_limited_in_round = true;
300
+ }
301
+ self.round_trip_counter.on_packet_sent(packet_number);
302
+
303
+ self.bandwidth_sampler.on_packet_sent(
304
+ sent_time,
305
+ packet_number,
306
+ bytes,
307
+ bytes_in_flight,
308
+ is_retransmissible,
309
+ );
310
+ }
311
+
312
+ pub(super) fn on_congestion_event_start(
313
+ &mut self, acked_packets: &[Acked], lost_packets: &[Lost],
314
+ congestion_event: &mut BBRv2CongestionEvent, params: &Params,
315
+ ) {
316
+ let prior_bytes_acked = self.total_bytes_acked();
317
+ let prior_bytes_lost = self.total_bytes_lost();
318
+
319
+ let event_time = congestion_event.event_time;
320
+
321
+ congestion_event.end_of_round_trip =
322
+ if let Some(largest_acked) = acked_packets.last() {
323
+ self.round_trip_counter
324
+ .on_packets_acked(largest_acked.pkt_num)
325
+ } else {
326
+ false
327
+ };
328
+
329
+ let sample = self.bandwidth_sampler.on_congestion_event(
330
+ event_time,
331
+ acked_packets,
332
+ lost_packets,
333
+ Some(self.max_bandwidth()),
334
+ self.bandwidth_lo.unwrap_or(Bandwidth::infinite()),
335
+ self.round_trip_count(),
336
+ );
337
+
338
+ if sample.extra_acked == 0 {
339
+ self.cwnd_limited_before_aggregation_epoch = congestion_event
340
+ .prior_bytes_in_flight >=
341
+ congestion_event.prior_cwnd;
342
+ }
343
+
344
+ if sample.last_packet_send_state.is_valid {
345
+ congestion_event.last_packet_send_state =
346
+ sample.last_packet_send_state;
347
+ }
348
+
349
+ // Avoid updating `max_bandwidth_filter` if a) this is a loss-only event,
350
+ // or b) all packets in `acked_packets` did not generate valid
351
+ // samples. (e.g. ack of ack-only packets). In both cases,
352
+ // total_bytes_acked() will not change.
353
+ if let Some(sample_max) = sample.sample_max_bandwidth {
354
+ if prior_bytes_acked != self.total_bytes_acked() {
355
+ congestion_event.sample_max_bandwidth = Some(sample_max);
356
+ if !sample.sample_is_app_limited ||
357
+ sample_max > self.max_bandwidth()
358
+ {
359
+ self.max_bandwidth_filter.update(sample_max);
360
+ }
361
+ }
362
+ }
363
+
364
+ if let Some(rtt_sample) = sample.sample_rtt {
365
+ congestion_event.sample_min_rtt = Some(rtt_sample);
366
+ self.min_rtt_filter.update(rtt_sample, event_time);
367
+ }
368
+
369
+ self.latest_send_rate = sample.sample_max_send_rate;
370
+ self.latest_ack_rate = sample.sample_max_ack_rate;
371
+
372
+ congestion_event.bytes_acked =
373
+ self.total_bytes_acked() - prior_bytes_acked;
374
+ congestion_event.bytes_lost = self.total_bytes_lost() - prior_bytes_lost;
375
+
376
+ congestion_event.bytes_in_flight = congestion_event
377
+ .prior_bytes_in_flight
378
+ .saturating_sub(congestion_event.bytes_acked)
379
+ .saturating_sub(congestion_event.bytes_lost);
380
+
381
+ if congestion_event.bytes_lost > 0 {
382
+ self.bytes_lost_in_round += congestion_event.bytes_lost;
383
+ self.loss_events_in_round += 1;
384
+ }
385
+
386
+ if congestion_event.bytes_acked > 0 &&
387
+ congestion_event.last_packet_send_state.is_valid &&
388
+ self.total_bytes_acked() >
389
+ congestion_event.last_packet_send_state.total_bytes_acked
390
+ {
391
+ let bytes_delivered = self.total_bytes_acked() -
392
+ congestion_event.last_packet_send_state.total_bytes_acked;
393
+ self.max_bytes_delivered_in_round =
394
+ self.max_bytes_delivered_in_round.max(bytes_delivered);
395
+ }
396
+
397
+ self.min_bytes_in_flight_in_round = self
398
+ .min_bytes_in_flight_in_round
399
+ .min(congestion_event.bytes_in_flight);
400
+
401
+ // `bandwidth_latest` and `inflight_latest` only increased within a
402
+ // round.
403
+ if sample.sample_max_bandwidth > Some(self.bandwidth_latest) {
404
+ self.bandwidth_latest = sample.sample_max_bandwidth.unwrap();
405
+ }
406
+
407
+ if sample.sample_max_inflight > self.inflight_latest {
408
+ self.inflight_latest = sample.sample_max_inflight;
409
+ }
410
+
411
+ // Adapt lower bounds(bandwidth_lo and inflight_lo).
412
+ self.adapt_lower_bounds(congestion_event, params);
413
+
414
+ if !congestion_event.end_of_round_trip {
415
+ return;
416
+ }
417
+
418
+ if let Some(bandwidth) = sample.sample_max_bandwidth {
419
+ self.bandwidth_latest = bandwidth;
420
+ }
421
+
422
+ if sample.sample_max_inflight > 0 {
423
+ self.inflight_latest = sample.sample_max_inflight;
424
+ }
425
+ }
426
+
427
+ pub(super) fn on_packet_neutered(&mut self, packet_number: u64) {
428
+ self.bandwidth_sampler.on_packet_neutered(packet_number)
429
+ }
430
+
431
+ fn adapt_lower_bounds(
432
+ &mut self, congestion_event: &BBRv2CongestionEvent, params: &Params,
433
+ ) {
434
+ if params.bw_lo_mode == BwLoMode::Default {
435
+ if !congestion_event.end_of_round_trip ||
436
+ congestion_event.is_probing_for_bandwidth
437
+ {
438
+ return;
439
+ }
440
+
441
+ if self.bytes_lost_in_round > 0 {
442
+ if self.bandwidth_lo.is_none() {
443
+ self.bandwidth_lo = Some(self.max_bandwidth());
444
+ }
445
+
446
+ self.bandwidth_lo = Some(
447
+ self.bandwidth_latest
448
+ .max(self.bandwidth_lo.unwrap() * (1.0 - params.beta)),
449
+ );
450
+
451
+ if self.inflight_lo == usize::MAX {
452
+ self.inflight_lo = congestion_event.prior_cwnd;
453
+ }
454
+
455
+ let inflight_lo_new =
456
+ (self.inflight_lo as f32 * (1.0 - params.beta)) as usize;
457
+ self.inflight_lo = self.inflight_latest.max(inflight_lo_new);
458
+ }
459
+ return;
460
+ }
461
+
462
+ if congestion_event.bytes_lost == 0 {
463
+ return;
464
+ }
465
+
466
+ // Ignore losses from packets sent when probing for more bandwidth in
467
+ // STARTUP or PROBE_UP when they're lost in DRAIN or PROBE_DOWN.
468
+ if self.pacing_gain() < 1. {
469
+ return;
470
+ }
471
+
472
+ // Decrease bandwidth_lo whenever there is loss.
473
+ // Set `bandwidth_lo`if it is not yet set.
474
+ if self.bandwidth_lo.is_none() {
475
+ self.bandwidth_lo = Some(self.max_bandwidth());
476
+ }
477
+
478
+ // Save `bandwidth_lo` if it hasn't already been saved.
479
+ if self.prior_bandwidth_lo.is_none() {
480
+ self.prior_bandwidth_lo = self.bandwidth_lo;
481
+ }
482
+
483
+ match params.bw_lo_mode {
484
+ BwLoMode::Default => unreachable!("Handled above"),
485
+ BwLoMode::MinRttReduction => {
486
+ let reduction = Bandwidth::from_bytes_and_time_delta(
487
+ congestion_event.bytes_lost,
488
+ self.min_rtt(),
489
+ );
490
+
491
+ self.bandwidth_lo = self
492
+ .bandwidth_lo
493
+ .map(|b| (b - reduction).unwrap_or(Bandwidth::zero()));
494
+ },
495
+ BwLoMode::InflightReduction => {
496
+ // Use a max of BDP and inflight to avoid starving app-limited
497
+ // flows.
498
+ let effective_inflight =
499
+ self.bdp0().max(congestion_event.prior_bytes_in_flight);
500
+ // This could use bytes_lost_in_round if the bandwidth_lo_ was
501
+ // saved when entering 'recovery', but this BBRv2
502
+ // implementation doesn't have recovery defined.
503
+ self.bandwidth_lo = self.bandwidth_lo.map(|b| {
504
+ b * ((effective_inflight as f64 -
505
+ congestion_event.bytes_lost as f64) /
506
+ effective_inflight as f64)
507
+ });
508
+ },
509
+ BwLoMode::CwndReduction => {
510
+ self.bandwidth_lo = self.bandwidth_lo.map(|b| {
511
+ b * ((congestion_event.prior_cwnd as f64 -
512
+ congestion_event.bytes_lost as f64) /
513
+ congestion_event.prior_cwnd as f64)
514
+ });
515
+ },
516
+ }
517
+
518
+ let mut last_bandwidth = self.bandwidth_latest;
519
+ // sample_max_bandwidth will be None if the loss is triggered by a timer
520
+ // expiring. Ideally we'd use the most recent bandwidth sample,
521
+ // but bandwidth_latest is safer than None.
522
+ if let Some(sample_max_bandwidth) = congestion_event.sample_max_bandwidth
523
+ {
524
+ // bandwidth_latest is the max bandwidth for the round, but to allow
525
+ // fast, conservation style response to loss, use the last sample.
526
+ last_bandwidth = sample_max_bandwidth;
527
+ }
528
+ if self.pacing_gain > params.full_bw_threshold {
529
+ // In STARTUP, `pacing_gain` is applied to `bandwidth_lo` in
530
+ // update_pacing_rate, so this backs that multiplication out to allow
531
+ // the pacing rate to decrease, but not below
532
+ // last_bandwidth * full_bw_threshold.
533
+ self.bandwidth_lo = self.bandwidth_lo.max(Some(
534
+ last_bandwidth * (params.full_bw_threshold / self.pacing_gain),
535
+ ));
536
+ } else {
537
+ // Ensure bandwidth_lo isn't lower than last_bandwidth.
538
+ self.bandwidth_lo = self.bandwidth_lo.max(Some(last_bandwidth))
539
+ }
540
+ // If it's the end of the round, ensure bandwidth_lo doesn't decrease more
541
+ // than beta.
542
+ if congestion_event.end_of_round_trip {
543
+ self.bandwidth_lo = self.bandwidth_lo.max(
544
+ self.prior_bandwidth_lo
545
+ .take()
546
+ .map(|b| b * (1.0 - params.beta)),
547
+ )
548
+ }
549
+ // These modes ignore inflight_lo as well.
550
+ }
551
+
552
+ pub(super) fn on_congestion_event_finish(
553
+ &mut self, least_unacked_packet: u64,
554
+ congestion_event: &BBRv2CongestionEvent,
555
+ ) {
556
+ if congestion_event.end_of_round_trip {
557
+ self.on_new_round();
558
+ }
559
+
560
+ self.bandwidth_sampler
561
+ .remove_obsolete_packets(least_unacked_packet);
562
+ }
563
+
564
+ pub(super) fn maybe_expire_min_rtt(
565
+ &mut self, congestion_event: &BBRv2CongestionEvent, params: &Params,
566
+ ) -> bool {
567
+ if congestion_event.sample_min_rtt.is_none() {
568
+ return false;
569
+ }
570
+
571
+ if congestion_event.event_time <
572
+ self.min_rtt_filter.min_rtt_timestamp + params.probe_rtt_period
573
+ {
574
+ return false;
575
+ }
576
+
577
+ self.min_rtt_filter.force_update(
578
+ congestion_event.sample_min_rtt.unwrap(),
579
+ congestion_event.event_time,
580
+ );
581
+
582
+ true
583
+ }
584
+
585
+ pub(super) fn is_inflight_too_high(
586
+ &self, congestion_event: &BBRv2CongestionEvent, max_loss_events: usize,
587
+ params: &Params,
588
+ ) -> bool {
589
+ let send_state = &congestion_event.last_packet_send_state;
590
+
591
+ if !send_state.is_valid {
592
+ // Not enough information.
593
+ return false;
594
+ }
595
+
596
+ if self.loss_events_in_round < max_loss_events {
597
+ return false;
598
+ }
599
+
600
+ // TODO(vlad): BytesInFlight(send_state);
601
+ let inflight_at_send = send_state.bytes_in_flight;
602
+
603
+ let bytes_lost_in_round = self.bytes_lost_in_round;
604
+
605
+ if inflight_at_send > 0 && bytes_lost_in_round > 0 {
606
+ let lost_in_round_threshold =
607
+ (inflight_at_send as f32 * params.loss_threshold) as usize;
608
+ if bytes_lost_in_round > lost_in_round_threshold {
609
+ return true;
610
+ }
611
+ }
612
+
613
+ false
614
+ }
615
+
616
+ pub(super) fn restart_round_early(&mut self) {
617
+ self.on_new_round();
618
+ self.round_trip_counter.restart_round();
619
+ self.rounds_with_queueing = 0;
620
+ }
621
+
622
+ fn on_new_round(&mut self) {
623
+ self.bytes_lost_in_round = 0;
624
+ self.loss_events_in_round = 0;
625
+ self.max_bytes_delivered_in_round = 0;
626
+ self.min_bytes_in_flight_in_round = usize::MAX;
627
+ self.inflight_hi_limited_in_round = false;
628
+ }
629
+
630
+ pub(super) fn has_bandwidth_growth(
631
+ &mut self, congestion_event: &BBRv2CongestionEvent, params: &Params,
632
+ ) -> bool {
633
+ let threshold = self.full_bandwidth_baseline * params.full_bw_threshold;
634
+
635
+ if self.max_bandwidth() >= threshold {
636
+ self.full_bandwidth_baseline = self.max_bandwidth();
637
+ self.rounds_without_bandwidth_growth = 0;
638
+ return true;
639
+ }
640
+
641
+ if !congestion_event.last_packet_send_state.is_valid {
642
+ // last_packet_send_state not available because the
643
+ // congestion event did not contain any non-ACK frames.
644
+ return false;
645
+ }
646
+
647
+ let ignore_round = self.ignore_app_limited_for_no_bandwidth_growth &&
648
+ congestion_event.last_packet_send_state.is_app_limited;
649
+
650
+ if !ignore_round {
651
+ self.rounds_without_bandwidth_growth += 1;
652
+ }
653
+
654
+ // full_bandwidth_reached is only set to true when not app-limited
655
+ if self.rounds_without_bandwidth_growth >= params.startup_full_bw_rounds &&
656
+ !congestion_event.last_packet_send_state.is_app_limited
657
+ {
658
+ self.full_bandwidth_reached = true;
659
+ }
660
+
661
+ false
662
+ }
663
+
664
+ pub(super) fn queueing_threshold_extra_bytes(&self) -> usize {
665
+ // TODO(vlad): 2 * mss
666
+ 2 * DEFAULT_MSS
667
+ }
668
+
669
+ pub(super) fn check_persistent_queue(
670
+ &mut self, target_gain: f32, params: &Params,
671
+ ) {
672
+ let target = self
673
+ .bdp(self.max_bandwidth(), target_gain)
674
+ .max(self.bdp0() + self.queueing_threshold_extra_bytes());
675
+
676
+ if self.min_bytes_in_flight_in_round < target {
677
+ self.rounds_with_queueing = 0;
678
+ return;
679
+ }
680
+
681
+ self.rounds_with_queueing += 1;
682
+ #[allow(clippy::absurd_extreme_comparisons)]
683
+ if self.rounds_with_queueing >= params.max_startup_queue_rounds {
684
+ self.full_bandwidth_reached = true;
685
+ }
686
+ }
687
+
688
+ pub(super) fn max_bytes_delivered_in_round(&self) -> usize {
689
+ self.max_bytes_delivered_in_round
690
+ }
691
+
692
+ pub(super) fn total_bytes_acked(&self) -> usize {
693
+ self.bandwidth_sampler.total_bytes_acked()
694
+ }
695
+
696
+ pub(super) fn total_bytes_lost(&self) -> usize {
697
+ self.bandwidth_sampler.total_bytes_lost()
698
+ }
699
+
700
+ fn round_trip_count(&self) -> usize {
701
+ self.round_trip_counter.round_trip_count
702
+ }
703
+
704
+ pub(super) fn full_bandwidth_reached(&self) -> bool {
705
+ self.full_bandwidth_reached
706
+ }
707
+
708
+ pub(super) fn set_full_bandwidth_reached(&mut self) {
709
+ self.full_bandwidth_reached = true
710
+ }
711
+
712
+ pub(super) fn pacing_gain(&self) -> f32 {
713
+ self.pacing_gain
714
+ }
715
+
716
+ pub(super) fn set_pacing_gain(&mut self, pacing_gain: f32) {
717
+ self.pacing_gain = pacing_gain
718
+ }
719
+
720
+ pub(super) fn cwnd_gain(&self) -> f32 {
721
+ self.cwnd_gain
722
+ }
723
+
724
+ pub(super) fn set_cwnd_gain(&mut self, cwnd_gain: f32) {
725
+ self.cwnd_gain = cwnd_gain
726
+ }
727
+
728
+ pub(super) fn inflight_hi(&self) -> usize {
729
+ self.inflight_hi
730
+ }
731
+
732
+ pub(super) fn inflight_hi_with_headroom(&self, params: &Params) -> usize {
733
+ let headroom =
734
+ (self.inflight_hi as f32 * params.inflight_hi_headroom) as usize;
735
+ self.inflight_hi.saturating_sub(headroom)
736
+ }
737
+
738
+ pub(super) fn set_inflight_hi(&mut self, new_inflight_hi: usize) {
739
+ self.inflight_hi = new_inflight_hi
740
+ }
741
+
742
+ pub(super) fn inflight_hi_default(&self) -> usize {
743
+ usize::MAX
744
+ }
745
+
746
+ pub(super) fn inflight_lo(&self) -> usize {
747
+ self.inflight_lo
748
+ }
749
+
750
+ pub(super) fn clear_inflight_lo(&mut self) {
751
+ self.inflight_lo = usize::MAX
752
+ }
753
+
754
+ pub(super) fn cap_inflight_lo(&mut self, cap: usize) {
755
+ if self.inflight_lo != usize::MAX {
756
+ self.inflight_lo = cap.min(self.inflight_lo)
757
+ }
758
+ }
759
+
760
+ pub(super) fn clear_bandwidth_lo(&mut self) {
761
+ self.bandwidth_lo = None
762
+ }
763
+
764
+ pub(super) fn advance_max_bandwidth_filter(&mut self) {
765
+ self.max_bandwidth_filter.advance()
766
+ }
767
+
768
+ pub(super) fn postpone_min_rtt_timestamp(&mut self, duration: Duration) {
769
+ self.min_rtt_filter
770
+ .force_update(self.min_rtt(), self.min_rtt_timestamp().add(duration));
771
+ }
772
+
773
+ pub(super) fn on_app_limited(&mut self) {
774
+ self.bandwidth_sampler.on_app_limited()
775
+ }
776
+
777
+ pub(super) fn loss_events_in_round(&self) -> usize {
778
+ self.loss_events_in_round
779
+ }
780
+
781
+ pub(super) fn rounds_with_queueing(&self) -> usize {
782
+ self.rounds_with_queueing
783
+ }
784
+ }