| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
// Building a stream of ordered bytes to give the application from a series of |
| 8 |
// incoming STREAM frames. |
| 9 |
|
| 10 |
#![allow( |
| 11 |
clippy::module_name_repetitions, |
| 12 |
reason = "<https://github.com/mozilla/neqo/issues/2284#issuecomment-2782711813>" |
| 13 |
)] |
| 14 |
|
| 15 |
use std::{ |
| 16 |
cell::RefCell, |
| 17 |
cmp::max, |
| 18 |
collections::BTreeMap, |
| 19 |
fmt::Debug, |
| 20 |
mem, |
| 21 |
rc::{Rc, Weak}, |
| 22 |
time::{Duration, Instant}, |
| 23 |
}; |
| 24 |
|
| 25 |
use neqo_common::{qtrace, Role}; |
| 26 |
use smallvec::SmallVec; |
| 27 |
use strum::Display; |
| 28 |
|
| 29 |
use crate::{ |
| 30 |
events::ConnectionEvents, |
| 31 |
fc::ReceiverFlowControl, |
| 32 |
frame::FrameType, |
| 33 |
packet::PacketBuilder, |
| 34 |
recovery::{RecoveryToken, StreamRecoveryToken}, |
| 35 |
send_stream::SendStreams, |
| 36 |
stats::FrameStats, |
| 37 |
stream_id::StreamId, |
| 38 |
AppError, Error, Res, |
| 39 |
}; |
| 40 |
|
| 41 |
pub const INITIAL_RECV_WINDOW_SIZE: usize = 1024 * 1024; |
| 42 |
|
| 43 |
/// Limit for the maximum amount of bytes active on a single stream, i.e. limit |
| 44 |
/// for the size of the stream receive window. |
| 45 |
/// |
| 46 |
/// A value of 10 MiB allows for: |
| 47 |
/// |
| 48 |
/// - 10ms rtt and 8.3 GBit/s |
| 49 |
/// - 20ms rtt and 4.2 GBit/s |
| 50 |
/// - 40ms rtt and 2.1 GBit/s |
| 51 |
/// - 100ms rtt and 0.8 GBit/s |
| 52 |
/// |
| 53 |
/// Keep in sync with [`crate::send_stream::MAX_SEND_BUFFER_SIZE`]. |
| 54 |
pub const MAX_RECV_WINDOW_SIZE: u64 = 10 * 1024 * 1024; |
| 55 |
|
| 56 |
#[derive(Debug, Default)] |
| 57 |
pub struct RecvStreams { |
| 58 |
streams: BTreeMap<StreamId, RecvStream>, |
| 59 |
keep_alive: Weak<()>, |
| 60 |
} |
| 61 |
|
| 62 |
impl RecvStreams { |
| 63 |
pub fn write_frames( |
| 64 |
&mut self, |
| 65 |
builder: &mut PacketBuilder, |
| 66 |
tokens: &mut Vec<RecoveryToken>, |
| 67 |
stats: &mut FrameStats, |
| 68 |
now: Instant, |
| 69 |
rtt: Duration, |
| 70 |
) { |
| 71 |
for stream in self.streams.values_mut() { |
| 72 |
stream.write_frame(builder, tokens, stats, now, rtt); |
| 73 |
if builder.is_full() { |
| 74 |
return; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
pub fn insert(&mut self, id: StreamId, stream: RecvStream) { |
| 80 |
self.streams.insert(id, stream); |
| 81 |
} |
| 82 |
|
| 83 |
#[allow( |
| 84 |
clippy::allow_attributes, |
| 85 |
clippy::missing_errors_doc, |
| 86 |
reason = "OK here." |
| 87 |
)] |
| 88 |
pub fn get_mut(&mut self, id: StreamId) -> Res<&mut RecvStream> { |
| 89 |
self.streams.get_mut(&id).ok_or(Error::InvalidStreamId) |
| 90 |
} |
| 91 |
|
| 92 |
#[allow( |
| 93 |
clippy::allow_attributes, |
| 94 |
clippy::missing_errors_doc, |
| 95 |
reason = "OK here." |
| 96 |
)] |
| 97 |
pub fn keep_alive(&mut self, id: StreamId, k: bool) -> Res<()> { |
| 98 |
let self_ka = &mut self.keep_alive; |
| 99 |
let s = self.streams.get_mut(&id).ok_or(Error::InvalidStreamId)?; |
| 100 |
s.keep_alive = k.then(|| { |
| 101 |
self_ka.upgrade().unwrap_or_else(|| { |
| 102 |
let r = Rc::new(()); |
| 103 |
*self_ka = Rc::downgrade(&r); |
| 104 |
r |
| 105 |
}) |
| 106 |
}); |
| 107 |
Ok(()) |
| 108 |
} |
| 109 |
|
| 110 |
#[must_use] |
| 111 |
pub fn need_keep_alive(&self) -> bool { |
| 112 |
self.keep_alive.strong_count() > 0 |
| 113 |
} |
| 114 |
|
| 115 |
pub fn clear(&mut self) { |
| 116 |
self.streams.clear(); |
| 117 |
} |
| 118 |
|
| 119 |
pub fn clear_terminal(&mut self, send_streams: &SendStreams, role: Role) -> (u64, u64) { |
| 120 |
let mut removed_bidi = 0; |
| 121 |
let mut removed_uni = 0; |
| 122 |
self.streams.retain(|id, s| { |
| 123 |
let dead = s.is_terminal() && (id.is_uni() || !send_streams.exists(*id)); |
| 124 |
if dead && id.is_remote_initiated(role) { |
| 125 |
if id.is_bidi() { |
| 126 |
removed_bidi += 1; |
| 127 |
} else { |
| 128 |
removed_uni += 1; |
| 129 |
} |
| 130 |
} |
| 131 |
!dead |
| 132 |
}); |
| 133 |
|
| 134 |
(removed_bidi, removed_uni) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/// Holds data not yet read by application. Orders and dedupes data ranges |
| 139 |
/// from incoming STREAM frames. |
| 140 |
#[derive(Debug, Default)] |
| 141 |
pub struct RxStreamOrderer { |
| 142 |
data_ranges: BTreeMap<u64, Vec<u8>>, // (start_offset, data) |
| 143 |
retired: u64, // Number of bytes the application has read |
| 144 |
received: u64, // The number of bytes stored in `data_ranges` |
| 145 |
} |
| 146 |
|
| 147 |
impl RxStreamOrderer { |
| 148 |
#[must_use] |
| 149 |
pub fn new() -> Self { |
| 150 |
Self::default() |
| 151 |
} |
| 152 |
|
| 153 |
/// Process an incoming stream frame off the wire. This may result in data |
| 154 |
/// being available to upper layers if frame is not out of order (ooo) or |
| 155 |
/// if the frame fills a gap. |
| 156 |
/// # Panics |
| 157 |
/// Only when `u64` values cannot be converted to `usize`, which only |
| 158 |
/// happens on 32-bit machines that hold far too much data at the same time. |
| 159 |
pub fn inbound_frame(&mut self, mut new_start: u64, mut new_data: &[u8]) { |
| 160 |
qtrace!("Inbound data offset={new_start} len={}", new_data.len()); |
| 161 |
|
| 162 |
// Get entry before where new entry would go, so we can see if we already |
| 163 |
// have the new bytes. |
| 164 |
// Avoid copies and duplicated data. |
| 165 |
let new_end = new_start + u64::try_from(new_data.len()).expect("usize fits in u64"); |
| 166 |
|
| 167 |
if new_end <= self.retired { |
| 168 |
// Range already read by application, this frame is very late and unneeded. |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
if new_start < self.retired { |
| 173 |
new_data = |
| 174 |
&new_data[usize::try_from(self.retired - new_start).expect("u64 fits in usize")..]; |
| 175 |
new_start = self.retired; |
| 176 |
} |
| 177 |
|
| 178 |
if new_data.is_empty() { |
| 179 |
// No data to insert |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
let extend = if let Some((&prev_start, prev_vec)) = |
| 184 |
self.data_ranges.range_mut(..=new_start).next_back() |
| 185 |
{ |
| 186 |
let prev_end = prev_start + u64::try_from(prev_vec.len()).expect("usize fits in u64"); |
| 187 |
if new_end > prev_end { |
| 188 |
// PPPPPP -> PPPPPP |
| 189 |
// NNNNNN NN |
| 190 |
// NNNNNNNN NN |
| 191 |
// Add a range containing only new data |
| 192 |
// (In-order frames will take this path, with no overlap) |
| 193 |
let overlap = prev_end.saturating_sub(new_start); |
| 194 |
qtrace!("New frame {new_start}-{new_end} received, overlap: {overlap}"); |
| 195 |
new_start += overlap; |
| 196 |
new_data = &new_data[usize::try_from(overlap).expect("u64 fits in usize")..]; |
| 197 |
// If it is small enough, extend the previous buffer. |
| 198 |
// This can't always extend, because otherwise the buffer could end up |
| 199 |
// growing indefinitely without being released. |
| 200 |
prev_vec.len() < 4096 && prev_end == new_start |
| 201 |
} else { |
| 202 |
// PPPPPP -> PPPPPP |
| 203 |
// NNNN |
| 204 |
// NNNN |
| 205 |
// Do nothing |
| 206 |
qtrace!("Dropping frame with already-received range {new_start}-{new_end}"); |
| 207 |
return; |
| 208 |
} |
| 209 |
} else { |
| 210 |
qtrace!("New frame {new_start}-{new_end} received"); |
| 211 |
false |
| 212 |
}; |
| 213 |
|
| 214 |
let mut to_add = new_data; |
| 215 |
if self |
| 216 |
.data_ranges |
| 217 |
.last_entry() |
| 218 |
.is_some_and(|e| *e.key() >= new_start) |
| 219 |
{ |
| 220 |
// Is this at the end (common case)? If so, nothing to do in this block |
| 221 |
// Common case: |
| 222 |
// PPPPPP -> PPPPPP |
| 223 |
// NNNNNNN NNNNNNN |
| 224 |
// or |
| 225 |
// PPPPPP -> PPPPPP |
| 226 |
// NNNNNNN NNNNNNN |
| 227 |
// |
| 228 |
// Not the common case, handle possible overlap with next entries |
| 229 |
// PPPPPP AAA -> PPPPPP |
| 230 |
// NNNNNNN NNNNNNN |
| 231 |
// or |
| 232 |
// PPPPPP AAAA -> PPPPPP AAAA |
| 233 |
// NNNNNNN NNNNN |
| 234 |
// or (this is where to_remove is used) |
| 235 |
// PPPPPP AA -> PPPPPP |
| 236 |
// NNNNNNN NNNNNNN |
| 237 |
|
| 238 |
let mut to_remove = SmallVec::<[_; 8]>::new(); |
| 239 |
|
| 240 |
for (&next_start, next_data) in self.data_ranges.range_mut(new_start..) { |
| 241 |
let next_end = |
| 242 |
next_start + u64::try_from(next_data.len()).expect("usize fits in u64"); |
| 243 |
let overlap = new_end.saturating_sub(next_start); |
| 244 |
if overlap == 0 { |
| 245 |
// Fills in the hole, exactly (probably common) |
| 246 |
break; |
| 247 |
} else if next_end >= new_end { |
| 248 |
qtrace!( |
| 249 |
"New frame {new_start}-{new_end} overlaps with next frame by {overlap}, truncating" |
| 250 |
); |
| 251 |
let truncate_to = |
| 252 |
new_data.len() - usize::try_from(overlap).expect("u64 fits in usize"); |
| 253 |
to_add = &new_data[..truncate_to]; |
| 254 |
break; |
| 255 |
} |
| 256 |
qtrace!( |
| 257 |
"New frame {new_start}-{new_end} spans entire next frame {next_start}-{next_end}, replacing" |
| 258 |
); |
| 259 |
to_remove.push(next_start); |
| 260 |
// Continue, since we may have more overlaps |
| 261 |
} |
| 262 |
|
| 263 |
for start in to_remove { |
| 264 |
self.data_ranges.remove(&start); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
if !to_add.is_empty() { |
| 269 |
self.received += u64::try_from(to_add.len()).expect("usize fits in u64"); |
| 270 |
if extend { |
| 271 |
if let Some((_, buf)) = self.data_ranges.range_mut(..=new_start).next_back() { |
| 272 |
buf.extend_from_slice(to_add); |
| 273 |
} |
| 274 |
} else { |
| 275 |
self.data_ranges.insert(new_start, to_add.to_vec()); |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
/// Are any bytes readable? |
| 281 |
#[must_use] |
| 282 |
pub fn data_ready(&self) -> bool { |
| 283 |
self.data_ranges |
| 284 |
.keys() |
| 285 |
.next() |
| 286 |
.is_some_and(|&start| start <= self.retired) |
| 287 |
} |
| 288 |
|
| 289 |
/// How many bytes are readable? |
| 290 |
fn bytes_ready(&self) -> usize { |
| 291 |
let mut prev_end = self.retired; |
| 292 |
self.data_ranges |
| 293 |
.iter() |
| 294 |
.map(|(start_offset, data)| { |
| 295 |
// All ranges don't overlap but we could have partially |
| 296 |
// retired some of the first entry's data. |
| 297 |
let data_len = data.len() as u64 - self.retired.saturating_sub(*start_offset); |
| 298 |
(start_offset, data_len) |
| 299 |
}) |
| 300 |
.take_while(|(start_offset, data_len)| { |
| 301 |
if **start_offset <= prev_end { |
| 302 |
prev_end += data_len; |
| 303 |
true |
| 304 |
} else { |
| 305 |
false |
| 306 |
} |
| 307 |
}) |
| 308 |
// Accumulate, but saturate at usize::MAX. |
| 309 |
.fold(0, |acc: usize, (_, data_len)| { |
| 310 |
acc.saturating_add(usize::try_from(data_len).unwrap_or(usize::MAX)) |
| 311 |
}) |
| 312 |
} |
| 313 |
|
| 314 |
/// Bytes read by the application. |
| 315 |
#[must_use] |
| 316 |
pub const fn retired(&self) -> u64 { |
| 317 |
self.retired |
| 318 |
} |
| 319 |
|
| 320 |
#[must_use] |
| 321 |
pub const fn received(&self) -> u64 { |
| 322 |
self.received |
| 323 |
} |
| 324 |
|
| 325 |
/// Data bytes buffered. Could be more than `bytes_readable` if there are |
| 326 |
/// ranges missing. |
| 327 |
fn buffered(&self) -> u64 { |
| 328 |
self.data_ranges |
| 329 |
.iter() |
| 330 |
.map(|(&start, data)| data.len() as u64 - (self.retired.saturating_sub(start))) |
| 331 |
.sum() |
| 332 |
} |
| 333 |
|
| 334 |
/// Copy received data (if any) into the buffer. Returns bytes copied. |
| 335 |
fn read(&mut self, buf: &mut [u8]) -> usize { |
| 336 |
qtrace!("Reading {} bytes, {} available", buf.len(), self.buffered()); |
| 337 |
let mut copied = 0; |
| 338 |
|
| 339 |
for (&range_start, range_data) in &mut self.data_ranges { |
| 340 |
let mut keep = false; |
| 341 |
if self.retired >= range_start { |
| 342 |
// Frame data has new contiguous bytes. |
| 343 |
let copy_offset = usize::try_from(max(range_start, self.retired) - range_start) |
| 344 |
.expect("u64 fits in usize"); |
| 345 |
assert!(range_data.len() >= copy_offset); |
| 346 |
let available = range_data.len() - copy_offset; |
| 347 |
let space = buf.len() - copied; |
| 348 |
let copy_bytes = if available > space { |
| 349 |
keep = true; |
| 350 |
space |
| 351 |
} else { |
| 352 |
available |
| 353 |
}; |
| 354 |
|
| 355 |
if copy_bytes > 0 { |
| 356 |
let copy_slc = &range_data[copy_offset..copy_offset + copy_bytes]; |
| 357 |
buf[copied..copied + copy_bytes].copy_from_slice(copy_slc); |
| 358 |
copied += copy_bytes; |
| 359 |
self.retired += u64::try_from(copy_bytes).expect("usize fits in u64"); |
| 360 |
} |
| 361 |
} else { |
| 362 |
// The data in the buffer isn't contiguous. |
| 363 |
keep = true; |
| 364 |
} |
| 365 |
if keep { |
| 366 |
let mut keep = self.data_ranges.split_off(&range_start); |
| 367 |
mem::swap(&mut self.data_ranges, &mut keep); |
| 368 |
return copied; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
self.data_ranges.clear(); |
| 373 |
copied |
| 374 |
} |
| 375 |
|
| 376 |
/// Extend the given Vector with any available data. |
| 377 |
pub fn read_to_end(&mut self, buf: &mut Vec<u8>) -> usize { |
| 378 |
let orig_len = buf.len(); |
| 379 |
buf.resize(orig_len + self.bytes_ready(), 0); |
| 380 |
self.read(&mut buf[orig_len..]) |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
/// QUIC receiving states, based on -transport 3.2. |
| 385 |
#[derive(Debug, Display)] |
| 386 |
// Because a dead_code warning is easier than clippy::unused_self, see https://github.com/rust-lang/rust/issues/68408 |
| 387 |
enum RecvStreamState { |
| 388 |
Recv { |
| 389 |
fc: ReceiverFlowControl<StreamId>, |
| 390 |
session_fc: Rc<RefCell<ReceiverFlowControl<()>>>, |
| 391 |
recv_buf: RxStreamOrderer, |
| 392 |
}, |
| 393 |
SizeKnown { |
| 394 |
fc: ReceiverFlowControl<StreamId>, |
| 395 |
session_fc: Rc<RefCell<ReceiverFlowControl<()>>>, |
| 396 |
recv_buf: RxStreamOrderer, |
| 397 |
}, |
| 398 |
DataRecvd { |
| 399 |
fc: ReceiverFlowControl<StreamId>, |
| 400 |
session_fc: Rc<RefCell<ReceiverFlowControl<()>>>, |
| 401 |
recv_buf: RxStreamOrderer, |
| 402 |
}, |
| 403 |
DataRead { |
| 404 |
final_received: u64, |
| 405 |
final_read: u64, |
| 406 |
}, |
| 407 |
AbortReading { |
| 408 |
fc: ReceiverFlowControl<StreamId>, |
| 409 |
session_fc: Rc<RefCell<ReceiverFlowControl<()>>>, |
| 410 |
final_size_reached: bool, |
| 411 |
frame_needed: bool, |
| 412 |
err: AppError, |
| 413 |
final_received: u64, |
| 414 |
final_read: u64, |
| 415 |
}, |
| 416 |
WaitForReset { |
| 417 |
fc: ReceiverFlowControl<StreamId>, |
| 418 |
session_fc: Rc<RefCell<ReceiverFlowControl<()>>>, |
| 419 |
final_received: u64, |
| 420 |
final_read: u64, |
| 421 |
}, |
| 422 |
ResetRecvd { |
| 423 |
final_received: u64, |
| 424 |
final_read: u64, |
| 425 |
}, |
| 426 |
// Defined by spec but we don't use it: ResetRead |
| 427 |
} |
| 428 |
|
| 429 |
impl RecvStreamState { |
| 430 |
fn new( |
| 431 |
max_bytes: u64, |
| 432 |
stream_id: StreamId, |
| 433 |
session_fc: Rc<RefCell<ReceiverFlowControl<()>>>, |
| 434 |
) -> Self { |
| 435 |
Self::Recv { |
| 436 |
fc: ReceiverFlowControl::new(stream_id, max_bytes), |
| 437 |
recv_buf: RxStreamOrderer::new(), |
| 438 |
session_fc, |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
const fn recv_buf(&self) -> Option<&RxStreamOrderer> { |
| 443 |
match self { |
| 444 |
Self::Recv { recv_buf, .. } |
| 445 |
| Self::SizeKnown { recv_buf, .. } |
| 446 |
| Self::DataRecvd { recv_buf, .. } => Some(recv_buf), |
| 447 |
Self::DataRead { .. } |
| 448 |
| Self::AbortReading { .. } |
| 449 |
| Self::WaitForReset { .. } |
| 450 |
| Self::ResetRecvd { .. } => None, |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
fn flow_control_consume_data(&mut self, consumed: u64, fin: bool) -> Res<()> { |
| 455 |
let (fc, session_fc, final_size_reached, retire_data) = match self { |
| 456 |
Self::Recv { fc, session_fc, .. } => (fc, session_fc, false, false), |
| 457 |
Self::WaitForReset { fc, session_fc, .. } => (fc, session_fc, false, true), |
| 458 |
Self::SizeKnown { fc, session_fc, .. } | Self::DataRecvd { fc, session_fc, .. } => { |
| 459 |
(fc, session_fc, true, false) |
| 460 |
} |
| 461 |
Self::AbortReading { |
| 462 |
fc, |
| 463 |
session_fc, |
| 464 |
final_size_reached, |
| 465 |
.. |
| 466 |
} => { |
| 467 |
let old_final_size_reached = *final_size_reached; |
| 468 |
*final_size_reached |= fin; |
| 469 |
(fc, session_fc, old_final_size_reached, true) |
| 470 |
} |
| 471 |
Self::DataRead { .. } | Self::ResetRecvd { .. } => { |
| 472 |
return Ok(()); |
| 473 |
} |
| 474 |
}; |
| 475 |
|
| 476 |
// Check final size: |
| 477 |
let final_size_ok = match (fin, final_size_reached) { |
| 478 |
(true, true) => consumed == fc.consumed(), |
| 479 |
(false, true) => consumed <= fc.consumed(), |
| 480 |
(true, false) => consumed >= fc.consumed(), |
| 481 |
(false, false) => true, |
| 482 |
}; |
| 483 |
|
| 484 |
if !final_size_ok { |
| 485 |
return Err(Error::FinalSizeError); |
| 486 |
} |
| 487 |
|
| 488 |
let new_bytes_consumed = fc.set_consumed(consumed)?; |
| 489 |
session_fc.borrow_mut().consume(new_bytes_consumed)?; |
| 490 |
if retire_data { |
| 491 |
// Let's also retire this data since the stream has been aborted |
| 492 |
RecvStream::flow_control_retire_data(fc.consumed() - fc.retired(), fc, session_fc); |
| 493 |
} |
| 494 |
Ok(()) |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
// See https://www.w3.org/TR/webtransport/#receive-stream-stats |
| 499 |
#[derive(Debug, Clone, Copy)] |
| 500 |
pub struct RecvStreamStats { |
| 501 |
// An indicator of progress on how many of the server application’s bytes |
| 502 |
// intended for this stream have been received so far. |
| 503 |
// Only sequential bytes up to, but not including, the first missing byte, |
| 504 |
// are counted. This number can only increase. |
| 505 |
pub bytes_received: u64, |
| 506 |
// The total number of bytes the application has successfully read from this |
| 507 |
// stream. This number can only increase, and is always less than or equal |
| 508 |
// to bytes_received. |
| 509 |
pub bytes_read: u64, |
| 510 |
} |
| 511 |
|
| 512 |
impl RecvStreamStats { |
| 513 |
#[must_use] |
| 514 |
pub const fn new(bytes_received: u64, bytes_read: u64) -> Self { |
| 515 |
Self { |
| 516 |
bytes_received, |
| 517 |
bytes_read, |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
#[must_use] |
| 522 |
pub const fn bytes_received(&self) -> u64 { |
| 523 |
self.bytes_received |
| 524 |
} |
| 525 |
|
| 526 |
#[must_use] |
| 527 |
pub const fn bytes_read(&self) -> u64 { |
| 528 |
self.bytes_read |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
/// Implement a QUIC receive stream. |
| 533 |
#[derive(Debug)] |
| 534 |
pub struct RecvStream { |
| 535 |
stream_id: StreamId, |
| 536 |
state: RecvStreamState, |
| 537 |
conn_events: ConnectionEvents, |
| 538 |
keep_alive: Option<Rc<()>>, |
| 539 |
} |
| 540 |
|
| 541 |
impl RecvStream { |
| 542 |
pub fn new( |
| 543 |
stream_id: StreamId, |
| 544 |
max_stream_data: u64, |
| 545 |
session_fc: Rc<RefCell<ReceiverFlowControl<()>>>, |
| 546 |
conn_events: ConnectionEvents, |
| 547 |
) -> Self { |
| 548 |
Self { |
| 549 |
stream_id, |
| 550 |
state: RecvStreamState::new(max_stream_data, stream_id, session_fc), |
| 551 |
conn_events, |
| 552 |
keep_alive: None, |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
fn set_state(&mut self, new_state: RecvStreamState) { |
| 557 |
debug_assert_ne!( |
| 558 |
mem::discriminant(&self.state), |
| 559 |
mem::discriminant(&new_state) |
| 560 |
); |
| 561 |
qtrace!( |
| 562 |
"RecvStream {} state {} -> {}", |
| 563 |
self.stream_id.as_u64(), |
| 564 |
self.state, |
| 565 |
new_state |
| 566 |
); |
| 567 |
|
| 568 |
match new_state { |
| 569 |
// Receiving all data, or receiving or requesting RESET_STREAM |
| 570 |
// is cause to stop keepalives. |
| 571 |
RecvStreamState::DataRecvd { .. } |
| 572 |
| RecvStreamState::AbortReading { .. } |
| 573 |
| RecvStreamState::ResetRecvd { .. } => { |
| 574 |
self.keep_alive = None; |
| 575 |
} |
| 576 |
// Once all the data is read, generate an event. |
| 577 |
RecvStreamState::DataRead { .. } => { |
| 578 |
self.conn_events.recv_stream_complete(self.stream_id); |
| 579 |
} |
| 580 |
_ => {} |
| 581 |
} |
| 582 |
|
| 583 |
self.state = new_state; |
| 584 |
} |
| 585 |
|
| 586 |
#[must_use] |
| 587 |
pub const fn stats(&self) -> RecvStreamStats { |
| 588 |
match &self.state { |
| 589 |
RecvStreamState::Recv { recv_buf, .. } |
| 590 |
| RecvStreamState::SizeKnown { recv_buf, .. } |
| 591 |
| RecvStreamState::DataRecvd { recv_buf, .. } => { |
| 592 |
let received = recv_buf.received(); |
| 593 |
let read = recv_buf.retired(); |
| 594 |
RecvStreamStats::new(received, read) |
| 595 |
} |
| 596 |
RecvStreamState::AbortReading { |
| 597 |
final_received, |
| 598 |
final_read, |
| 599 |
.. |
| 600 |
} |
| 601 |
| RecvStreamState::WaitForReset { |
| 602 |
final_received, |
| 603 |
final_read, |
| 604 |
.. |
| 605 |
} |
| 606 |
| RecvStreamState::DataRead { |
| 607 |
final_received, |
| 608 |
final_read, |
| 609 |
} |
| 610 |
| RecvStreamState::ResetRecvd { |
| 611 |
final_received, |
| 612 |
final_read, |
| 613 |
} => { |
| 614 |
let received = *final_received; |
| 615 |
let read = *final_read; |
| 616 |
RecvStreamStats::new(received, read) |
| 617 |
} |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
/// # Errors |
| 622 |
/// When the incoming data violates flow control limits. |
| 623 |
/// # Panics |
| 624 |
/// Only when `u64` values are so big that they can't fit in a `usize`, which |
| 625 |
/// only happens on a 32-bit machine that has far too much unread data. |
| 626 |
pub fn inbound_stream_frame(&mut self, fin: bool, offset: u64, data: &[u8]) -> Res<()> { |
| 627 |
// We should post a DataReadable event only once when we change from no-data-ready to |
| 628 |
// data-ready. Therefore remember the state before processing a new frame. |
| 629 |
let already_data_ready = self.data_ready(); |
| 630 |
let new_end = offset + u64::try_from(data.len())?; |
| 631 |
|
| 632 |
self.state.flow_control_consume_data(new_end, fin)?; |
| 633 |
|
| 634 |
match &mut self.state { |
| 635 |
RecvStreamState::Recv { |
| 636 |
recv_buf, |
| 637 |
fc, |
| 638 |
session_fc, |
| 639 |
} => { |
| 640 |
recv_buf.inbound_frame(offset, data); |
| 641 |
if fin { |
| 642 |
let all_recv = |
| 643 |
fc.consumed() == recv_buf.retired() + recv_buf.bytes_ready() as u64; |
| 644 |
let buf = mem::replace(recv_buf, RxStreamOrderer::new()); |
| 645 |
let fc_copy = mem::take(fc); |
| 646 |
let session_fc_copy = mem::take(session_fc); |
| 647 |
if all_recv { |
| 648 |
self.set_state(RecvStreamState::DataRecvd { |
| 649 |
fc: fc_copy, |
| 650 |
session_fc: session_fc_copy, |
| 651 |
recv_buf: buf, |
| 652 |
}); |
| 653 |
} else { |
| 654 |
self.set_state(RecvStreamState::SizeKnown { |
| 655 |
fc: fc_copy, |
| 656 |
session_fc: session_fc_copy, |
| 657 |
recv_buf: buf, |
| 658 |
}); |
| 659 |
} |
| 660 |
} |
| 661 |
} |
| 662 |
RecvStreamState::SizeKnown { |
| 663 |
recv_buf, |
| 664 |
fc, |
| 665 |
session_fc, |
| 666 |
} => { |
| 667 |
recv_buf.inbound_frame(offset, data); |
| 668 |
if fc.consumed() == recv_buf.retired() + recv_buf.bytes_ready() as u64 { |
| 669 |
let buf = mem::replace(recv_buf, RxStreamOrderer::new()); |
| 670 |
let fc_copy = mem::take(fc); |
| 671 |
let session_fc_copy = mem::take(session_fc); |
| 672 |
self.set_state(RecvStreamState::DataRecvd { |
| 673 |
fc: fc_copy, |
| 674 |
session_fc: session_fc_copy, |
| 675 |
recv_buf: buf, |
| 676 |
}); |
| 677 |
} |
| 678 |
} |
| 679 |
RecvStreamState::DataRecvd { .. } |
| 680 |
| RecvStreamState::DataRead { .. } |
| 681 |
| RecvStreamState::AbortReading { .. } |
| 682 |
| RecvStreamState::WaitForReset { .. } |
| 683 |
| RecvStreamState::ResetRecvd { .. } => { |
| 684 |
qtrace!("data received when we are in state {}", self.state); |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
if !already_data_ready && (self.data_ready() || self.needs_to_inform_app_about_fin()) { |
| 689 |
self.conn_events.recv_stream_readable(self.stream_id); |
| 690 |
} |
| 691 |
|
| 692 |
Ok(()) |
| 693 |
} |
| 694 |
|
| 695 |
/// # Errors |
| 696 |
/// When the reset occurs at an invalid point. |
| 697 |
pub fn reset(&mut self, application_error_code: AppError, final_size: u64) -> Res<()> { |
| 698 |
self.state.flow_control_consume_data(final_size, true)?; |
| 699 |
match &mut self.state { |
| 700 |
RecvStreamState::Recv { |
| 701 |
fc, |
| 702 |
session_fc, |
| 703 |
recv_buf, |
| 704 |
} |
| 705 |
| RecvStreamState::SizeKnown { |
| 706 |
fc, |
| 707 |
session_fc, |
| 708 |
recv_buf, |
| 709 |
} => { |
| 710 |
// make flow control consumes new data that not really exist. |
| 711 |
Self::flow_control_retire_data(final_size - fc.retired(), fc, session_fc); |
| 712 |
self.conn_events |
| 713 |
.recv_stream_reset(self.stream_id, application_error_code); |
| 714 |
let received = recv_buf.received(); |
| 715 |
let read = recv_buf.retired(); |
| 716 |
self.set_state(RecvStreamState::ResetRecvd { |
| 717 |
final_received: received, |
| 718 |
final_read: read, |
| 719 |
}); |
| 720 |
} |
| 721 |
RecvStreamState::AbortReading { |
| 722 |
fc, |
| 723 |
session_fc, |
| 724 |
final_received, |
| 725 |
final_read, |
| 726 |
.. |
| 727 |
} |
| 728 |
| RecvStreamState::WaitForReset { |
| 729 |
fc, |
| 730 |
session_fc, |
| 731 |
final_received, |
| 732 |
final_read, |
| 733 |
} => { |
| 734 |
// make flow control consumes new data that not really exist. |
| 735 |
Self::flow_control_retire_data(final_size - fc.retired(), fc, session_fc); |
| 736 |
self.conn_events |
| 737 |
.recv_stream_reset(self.stream_id, application_error_code); |
| 738 |
let received = *final_received; |
| 739 |
let read = *final_read; |
| 740 |
self.set_state(RecvStreamState::ResetRecvd { |
| 741 |
final_received: received, |
| 742 |
final_read: read, |
| 743 |
}); |
| 744 |
} |
| 745 |
_ => { |
| 746 |
// Ignore reset if in DataRecvd, DataRead, or ResetRecvd |
| 747 |
} |
| 748 |
} |
| 749 |
Ok(()) |
| 750 |
} |
| 751 |
|
| 752 |
fn flow_control_retire_data( |
| 753 |
new_read: u64, |
| 754 |
fc: &mut ReceiverFlowControl<StreamId>, |
| 755 |
session_fc: &Rc<RefCell<ReceiverFlowControl<()>>>, |
| 756 |
) { |
| 757 |
if new_read > 0 { |
| 758 |
fc.add_retired(new_read); |
| 759 |
session_fc.borrow_mut().add_retired(new_read); |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
/// Send a flow control update. |
| 764 |
/// This is used when a peer declares that they are blocked. |
| 765 |
/// This sends `MAX_STREAM_DATA` if there is any increase possible. |
| 766 |
pub fn send_flowc_update(&mut self) { |
| 767 |
if let RecvStreamState::Recv { fc, .. } = &mut self.state { |
| 768 |
fc.send_flowc_update(); |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
pub fn set_stream_max_data(&mut self, max_data: u64) { |
| 773 |
if let RecvStreamState::Recv { fc, .. } = &mut self.state { |
| 774 |
fc.set_max_active(max_data); |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
#[must_use] |
| 779 |
pub const fn is_terminal(&self) -> bool { |
| 780 |
matches!( |
| 781 |
self.state, |
| 782 |
RecvStreamState::ResetRecvd { .. } | RecvStreamState::DataRead { .. } |
| 783 |
) |
| 784 |
} |
| 785 |
|
| 786 |
// App got all data but did not get the fin signal. |
| 787 |
const fn needs_to_inform_app_about_fin(&self) -> bool { |
| 788 |
matches!(self.state, RecvStreamState::DataRecvd { .. }) |
| 789 |
} |
| 790 |
|
| 791 |
fn data_ready(&self) -> bool { |
| 792 |
self.state |
| 793 |
.recv_buf() |
| 794 |
.is_some_and(RxStreamOrderer::data_ready) |
| 795 |
} |
| 796 |
|
| 797 |
/// # Errors |
| 798 |
/// `NoMoreData` if data and fin bit were previously read by the application. |
| 799 |
pub fn read(&mut self, buf: &mut [u8]) -> Res<(usize, bool)> { |
| 800 |
let data_recvd_state = matches!(self.state, RecvStreamState::DataRecvd { .. }); |
| 801 |
match &mut self.state { |
| 802 |
RecvStreamState::Recv { |
| 803 |
recv_buf, |
| 804 |
fc, |
| 805 |
session_fc, |
| 806 |
} |
| 807 |
| RecvStreamState::SizeKnown { |
| 808 |
recv_buf, |
| 809 |
fc, |
| 810 |
session_fc, |
| 811 |
.. |
| 812 |
} |
| 813 |
| RecvStreamState::DataRecvd { |
| 814 |
recv_buf, |
| 815 |
fc, |
| 816 |
session_fc, |
| 817 |
} => { |
| 818 |
let bytes_read = recv_buf.read(buf); |
| 819 |
Self::flow_control_retire_data(u64::try_from(bytes_read)?, fc, session_fc); |
| 820 |
let fin_read = if data_recvd_state { |
| 821 |
if recv_buf.buffered() == 0 { |
| 822 |
let received = recv_buf.received(); |
| 823 |
let read = recv_buf.retired(); |
| 824 |
self.set_state(RecvStreamState::DataRead { |
| 825 |
final_received: received, |
| 826 |
final_read: read, |
| 827 |
}); |
| 828 |
true |
| 829 |
} else { |
| 830 |
false |
| 831 |
} |
| 832 |
} else { |
| 833 |
false |
| 834 |
}; |
| 835 |
Ok((bytes_read, fin_read)) |
| 836 |
} |
| 837 |
RecvStreamState::DataRead { .. } |
| 838 |
| RecvStreamState::AbortReading { .. } |
| 839 |
| RecvStreamState::WaitForReset { .. } |
| 840 |
| RecvStreamState::ResetRecvd { .. } => Err(Error::NoMoreData), |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
pub fn stop_sending(&mut self, err: AppError) { |
| 845 |
qtrace!("stop_sending called when in state {}", self.state); |
| 846 |
match &mut self.state { |
| 847 |
RecvStreamState::Recv { |
| 848 |
fc, |
| 849 |
session_fc, |
| 850 |
recv_buf, |
| 851 |
} |
| 852 |
| RecvStreamState::SizeKnown { |
| 853 |
fc, |
| 854 |
session_fc, |
| 855 |
recv_buf, |
| 856 |
} => { |
| 857 |
// Retire data |
| 858 |
Self::flow_control_retire_data(fc.consumed() - fc.retired(), fc, session_fc); |
| 859 |
let fc_copy = mem::take(fc); |
| 860 |
let session_fc_copy = mem::take(session_fc); |
| 861 |
let received = recv_buf.received(); |
| 862 |
let read = recv_buf.retired(); |
| 863 |
self.set_state(RecvStreamState::AbortReading { |
| 864 |
fc: fc_copy, |
| 865 |
session_fc: session_fc_copy, |
| 866 |
final_size_reached: matches!(self.state, RecvStreamState::SizeKnown { .. }), |
| 867 |
frame_needed: true, |
| 868 |
err, |
| 869 |
final_received: received, |
| 870 |
final_read: read, |
| 871 |
}); |
| 872 |
} |
| 873 |
RecvStreamState::DataRecvd { |
| 874 |
fc, |
| 875 |
session_fc, |
| 876 |
recv_buf, |
| 877 |
} => { |
| 878 |
Self::flow_control_retire_data(fc.consumed() - fc.retired(), fc, session_fc); |
| 879 |
let received = recv_buf.received(); |
| 880 |
let read = recv_buf.retired(); |
| 881 |
self.set_state(RecvStreamState::DataRead { |
| 882 |
final_received: received, |
| 883 |
final_read: read, |
| 884 |
}); |
| 885 |
} |
| 886 |
RecvStreamState::DataRead { .. } |
| 887 |
| RecvStreamState::AbortReading { .. } |
| 888 |
| RecvStreamState::WaitForReset { .. } |
| 889 |
| RecvStreamState::ResetRecvd { .. } => { |
| 890 |
// Already in terminal state |
| 891 |
} |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
/// Maybe write a `MAX_STREAM_DATA` frame. |
| 896 |
pub fn write_frame( |
| 897 |
&mut self, |
| 898 |
builder: &mut PacketBuilder, |
| 899 |
tokens: &mut Vec<RecoveryToken>, |
| 900 |
stats: &mut FrameStats, |
| 901 |
now: Instant, |
| 902 |
rtt: Duration, |
| 903 |
) { |
| 904 |
match &mut self.state { |
| 905 |
// Maybe send MAX_STREAM_DATA |
| 906 |
RecvStreamState::Recv { fc, .. } => fc.write_frames(builder, tokens, stats, now, rtt), |
| 907 |
// Maybe send STOP_SENDING |
| 908 |
RecvStreamState::AbortReading { |
| 909 |
frame_needed, err, .. |
| 910 |
} => { |
| 911 |
if *frame_needed |
| 912 |
&& builder.write_varint_frame(&[ |
| 913 |
FrameType::StopSending.into(), |
| 914 |
self.stream_id.as_u64(), |
| 915 |
*err, |
| 916 |
]) |
| 917 |
{ |
| 918 |
tokens.push(RecoveryToken::Stream(StreamRecoveryToken::StopSending { |
| 919 |
stream_id: self.stream_id, |
| 920 |
})); |
| 921 |
stats.stop_sending += 1; |
| 922 |
*frame_needed = false; |
| 923 |
} |
| 924 |
} |
| 925 |
_ => {} |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
pub fn max_stream_data_lost(&mut self, maximum_data: u64) { |
| 930 |
if let RecvStreamState::Recv { fc, .. } = &mut self.state { |
| 931 |
fc.frame_lost(maximum_data); |
| 932 |
} |
| 933 |
} |
| 934 |
|
| 935 |
pub fn stop_sending_lost(&mut self) { |
| 936 |
if let RecvStreamState::AbortReading { frame_needed, .. } = &mut self.state { |
| 937 |
*frame_needed = true; |
| 938 |
} |
| 939 |
} |
| 940 |
|
| 941 |
pub fn stop_sending_acked(&mut self) { |
| 942 |
if let RecvStreamState::AbortReading { |
| 943 |
fc, |
| 944 |
session_fc, |
| 945 |
final_size_reached, |
| 946 |
final_received, |
| 947 |
final_read, |
| 948 |
.. |
| 949 |
} = &mut self.state |
| 950 |
{ |
| 951 |
let received = *final_received; |
| 952 |
let read = *final_read; |
| 953 |
if *final_size_reached { |
| 954 |
// We already know the final_size of the stream therefore we |
| 955 |
// do not need to wait for RESET. |
| 956 |
self.set_state(RecvStreamState::ResetRecvd { |
| 957 |
final_received: received, |
| 958 |
final_read: read, |
| 959 |
}); |
| 960 |
} else { |
| 961 |
let fc_copy = mem::take(fc); |
| 962 |
let session_fc_copy = mem::take(session_fc); |
| 963 |
self.set_state(RecvStreamState::WaitForReset { |
| 964 |
fc: fc_copy, |
| 965 |
session_fc: session_fc_copy, |
| 966 |
final_received: received, |
| 967 |
final_read: read, |
| 968 |
}); |
| 969 |
} |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
#[cfg(test)] |
| 974 |
#[must_use] |
| 975 |
pub const fn has_frames_to_write(&self) -> bool { |
| 976 |
if let RecvStreamState::Recv { fc, .. } = &self.state { |
| 977 |
fc.frame_needed() |
| 978 |
} else { |
| 979 |
false |
| 980 |
} |
| 981 |
} |
| 982 |
|
| 983 |
#[cfg(test)] |
| 984 |
#[must_use] |
| 985 |
pub const fn fc(&self) -> Option<&ReceiverFlowControl<StreamId>> { |
| 986 |
match &self.state { |
| 987 |
RecvStreamState::Recv { fc, .. } |
| 988 |
| RecvStreamState::SizeKnown { fc, .. } |
| 989 |
| RecvStreamState::DataRecvd { fc, .. } |
| 990 |
| RecvStreamState::AbortReading { fc, .. } |
| 991 |
| RecvStreamState::WaitForReset { fc, .. } => Some(fc), |
| 992 |
_ => None, |
| 993 |
} |
| 994 |
} |
| 995 |
} |
| 996 |
|
| 997 |
#[cfg(test)] |
| 998 |
mod tests { |
| 999 |
use std::{ |
| 1000 |
cell::RefCell, |
| 1001 |
fmt::Debug, |
| 1002 |
ops::Range, |
| 1003 |
rc::Rc, |
| 1004 |
time::{Duration, Instant}, |
| 1005 |
}; |
| 1006 |
|
| 1007 |
use neqo_common::{qtrace, Encoder}; |
| 1008 |
|
| 1009 |
use super::RecvStream; |
| 1010 |
use crate::{ |
| 1011 |
fc::{ReceiverFlowControl, WINDOW_UPDATE_FRACTION}, |
| 1012 |
packet::PacketBuilder, |
| 1013 |
recv_stream::RxStreamOrderer, |
| 1014 |
stats::FrameStats, |
| 1015 |
ConnectionEvents, Error, StreamId, INITIAL_RECV_WINDOW_SIZE, |
| 1016 |
}; |
| 1017 |
|
| 1018 |
const SESSION_WINDOW: usize = 1024; |
| 1019 |
|
| 1020 |
fn recv_ranges(ranges: &[Range<u64>], available: usize) { |
| 1021 |
const ZEROES: &[u8] = &[0; 100]; |
| 1022 |
qtrace!("recv_ranges {ranges:?}"); |
| 1023 |
|
| 1024 |
let mut s = RxStreamOrderer::default(); |
| 1025 |
for r in ranges { |
| 1026 |
let data = &ZEROES[..usize::try_from(r.end - r.start).unwrap()]; |
| 1027 |
s.inbound_frame(r.start, data); |
| 1028 |
} |
| 1029 |
|
| 1030 |
let mut buf = [0xff; 100]; |
| 1031 |
let mut total_recvd = 0; |
| 1032 |
loop { |
| 1033 |
let recvd = s.read(&mut buf[..]); |
| 1034 |
qtrace!("recv_ranges read {recvd}"); |
| 1035 |
total_recvd += recvd; |
| 1036 |
if recvd == 0 { |
| 1037 |
assert_eq!(total_recvd, available); |
| 1038 |
break; |
| 1039 |
} |
| 1040 |
} |
| 1041 |
} |
| 1042 |
|
| 1043 |
#[test] |
| 1044 |
#[expect( |
| 1045 |
clippy::single_range_in_vec_init, |
| 1046 |
reason = "Because that lint makes no sense here." |
| 1047 |
)] |
| 1048 |
fn recv_noncontiguous() { |
| 1049 |
// Non-contiguous with the start, no data available. |
| 1050 |
recv_ranges(&[10..20], 0); |
| 1051 |
} |
| 1052 |
|
| 1053 |
/// Overlaps with the start of a 10..20 range of bytes. |
| 1054 |
#[test] |
| 1055 |
fn recv_overlap_start() { |
| 1056 |
// Overlap the start, with a larger new value. |
| 1057 |
// More overlap than not. |
| 1058 |
recv_ranges(&[10..20, 4..18, 0..4], 20); |
| 1059 |
// Overlap the start, with a larger new value. |
| 1060 |
// Less overlap than not. |
| 1061 |
recv_ranges(&[10..20, 2..15, 0..2], 20); |
| 1062 |
// Overlap the start, with a smaller new value. |
| 1063 |
// More overlap than not. |
| 1064 |
recv_ranges(&[10..20, 8..14, 0..8], 20); |
| 1065 |
// Overlap the start, with a smaller new value. |
| 1066 |
// Less overlap than not. |
| 1067 |
recv_ranges(&[10..20, 6..13, 0..6], 20); |
| 1068 |
|
| 1069 |
// Again with some of the first range split in two. |
| 1070 |
recv_ranges(&[10..11, 11..20, 4..18, 0..4], 20); |
| 1071 |
recv_ranges(&[10..11, 11..20, 2..15, 0..2], 20); |
| 1072 |
recv_ranges(&[10..11, 11..20, 8..14, 0..8], 20); |
| 1073 |
recv_ranges(&[10..11, 11..20, 6..13, 0..6], 20); |
| 1074 |
|
| 1075 |
// Again with a gap in the first range. |
| 1076 |
recv_ranges(&[10..11, 12..20, 4..18, 0..4], 20); |
| 1077 |
recv_ranges(&[10..11, 12..20, 2..15, 0..2], 20); |
| 1078 |
recv_ranges(&[10..11, 12..20, 8..14, 0..8], 20); |
| 1079 |
recv_ranges(&[10..11, 12..20, 6..13, 0..6], 20); |
| 1080 |
} |
| 1081 |
|
| 1082 |
/// Overlaps with the end of a 10..20 range of bytes. |
| 1083 |
#[test] |
| 1084 |
fn recv_overlap_end() { |
| 1085 |
// Overlap the end, with a larger new value. |
| 1086 |
// More overlap than not. |
| 1087 |
recv_ranges(&[10..20, 12..25, 0..10], 25); |
| 1088 |
// Overlap the end, with a larger new value. |
| 1089 |
// Less overlap than not. |
| 1090 |
recv_ranges(&[10..20, 17..33, 0..10], 33); |
| 1091 |
// Overlap the end, with a smaller new value. |
| 1092 |
// More overlap than not. |
| 1093 |
recv_ranges(&[10..20, 15..21, 0..10], 21); |
| 1094 |
// Overlap the end, with a smaller new value. |
| 1095 |
// Less overlap than not. |
| 1096 |
recv_ranges(&[10..20, 17..25, 0..10], 25); |
| 1097 |
|
| 1098 |
// Again with some of the first range split in two. |
| 1099 |
recv_ranges(&[10..19, 19..20, 12..25, 0..10], 25); |
| 1100 |
recv_ranges(&[10..19, 19..20, 17..33, 0..10], 33); |
| 1101 |
recv_ranges(&[10..19, 19..20, 15..21, 0..10], 21); |
| 1102 |
recv_ranges(&[10..19, 19..20, 17..25, 0..10], 25); |
| 1103 |
|
| 1104 |
// Again with a gap in the first range. |
| 1105 |
recv_ranges(&[10..18, 19..20, 12..25, 0..10], 25); |
| 1106 |
recv_ranges(&[10..18, 19..20, 17..33, 0..10], 33); |
| 1107 |
recv_ranges(&[10..18, 19..20, 15..21, 0..10], 21); |
| 1108 |
recv_ranges(&[10..18, 19..20, 17..25, 0..10], 25); |
| 1109 |
} |
| 1110 |
|
| 1111 |
/// Complete overlaps with the start of a 10..20 range of bytes. |
| 1112 |
#[test] |
| 1113 |
fn recv_overlap_complete() { |
| 1114 |
// Complete overlap, more at the end. |
| 1115 |
recv_ranges(&[10..20, 9..23, 0..9], 23); |
| 1116 |
// Complete overlap, more at the start. |
| 1117 |
recv_ranges(&[10..20, 3..23, 0..3], 23); |
| 1118 |
// Complete overlap, to end. |
| 1119 |
recv_ranges(&[10..20, 5..20, 0..5], 20); |
| 1120 |
// Complete overlap, from start. |
| 1121 |
recv_ranges(&[10..20, 10..27, 0..10], 27); |
| 1122 |
// Complete overlap, from 0 and more. |
| 1123 |
recv_ranges(&[10..20, 0..23], 23); |
| 1124 |
|
| 1125 |
// Again with the first range split in two. |
| 1126 |
recv_ranges(&[10..14, 14..20, 9..23, 0..9], 23); |
| 1127 |
recv_ranges(&[10..14, 14..20, 3..23, 0..3], 23); |
| 1128 |
recv_ranges(&[10..14, 14..20, 5..20, 0..5], 20); |
| 1129 |
recv_ranges(&[10..14, 14..20, 10..27, 0..10], 27); |
| 1130 |
recv_ranges(&[10..14, 14..20, 0..23], 23); |
| 1131 |
|
| 1132 |
// Again with the a gap in the first range. |
| 1133 |
recv_ranges(&[10..13, 14..20, 9..23, 0..9], 23); |
| 1134 |
recv_ranges(&[10..13, 14..20, 3..23, 0..3], 23); |
| 1135 |
recv_ranges(&[10..13, 14..20, 5..20, 0..5], 20); |
| 1136 |
recv_ranges(&[10..13, 14..20, 10..27, 0..10], 27); |
| 1137 |
recv_ranges(&[10..13, 14..20, 0..23], 23); |
| 1138 |
} |
| 1139 |
|
| 1140 |
/// An overlap with no new bytes. |
| 1141 |
#[test] |
| 1142 |
fn recv_overlap_duplicate() { |
| 1143 |
recv_ranges(&[10..20, 11..12, 0..10], 20); |
| 1144 |
recv_ranges(&[10..20, 10..15, 0..10], 20); |
| 1145 |
recv_ranges(&[10..20, 14..20, 0..10], 20); |
| 1146 |
// Now with the first range split. |
| 1147 |
recv_ranges(&[10..14, 14..20, 10..15, 0..10], 20); |
| 1148 |
recv_ranges(&[10..15, 16..20, 21..25, 10..25, 0..10], 25); |
| 1149 |
} |
| 1150 |
|
| 1151 |
/// Reading exactly one chunk works, when the next chunk starts immediately. |
| 1152 |
#[test] |
| 1153 |
fn stop_reading_at_chunk() { |
| 1154 |
const CHUNK_SIZE: usize = 10; |
| 1155 |
const EXTRA_SIZE: usize = 3; |
| 1156 |
let mut s = RxStreamOrderer::new(); |
| 1157 |
|
| 1158 |
// Add three chunks. |
| 1159 |
s.inbound_frame(0, &[0; CHUNK_SIZE]); |
| 1160 |
let offset = u64::try_from(CHUNK_SIZE).unwrap(); |
| 1161 |
s.inbound_frame(offset, &[0; EXTRA_SIZE]); |
| 1162 |
let offset = u64::try_from(CHUNK_SIZE + EXTRA_SIZE).unwrap(); |
| 1163 |
s.inbound_frame(offset, &[0; EXTRA_SIZE]); |
| 1164 |
|
| 1165 |
// Read, providing only enough space for the first. |
| 1166 |
let mut buf = [0; 100]; |
| 1167 |
let count = s.read(&mut buf[..CHUNK_SIZE]); |
| 1168 |
assert_eq!(count, CHUNK_SIZE); |
| 1169 |
let count = s.read(&mut buf[..]); |
| 1170 |
assert_eq!(count, EXTRA_SIZE * 2); |
| 1171 |
} |
| 1172 |
|
| 1173 |
#[test] |
| 1174 |
fn recv_overlap_while_reading() { |
| 1175 |
let mut s = RxStreamOrderer::new(); |
| 1176 |
|
| 1177 |
// Add a chunk |
| 1178 |
s.inbound_frame(0, &[0; 150]); |
| 1179 |
assert_eq!(s.data_ranges[&0].len(), 150); |
| 1180 |
// Read, providing only enough space for the first 100. |
| 1181 |
let mut buf = [0; 100]; |
| 1182 |
let count = s.read(&mut buf[..]); |
| 1183 |
assert_eq!(count, 100); |
| 1184 |
assert_eq!(s.retired, 100); |
| 1185 |
|
| 1186 |
// Add a second frame that overlaps. |
| 1187 |
// This shouldn't truncate the first frame, as we're already |
| 1188 |
// Reading from it. |
| 1189 |
s.inbound_frame(120, &[0; 60]); |
| 1190 |
assert_eq!(s.data_ranges[&0].len(), 180); |
| 1191 |
// Read second part of first frame and all of the second frame |
| 1192 |
let count = s.read(&mut buf[..]); |
| 1193 |
assert_eq!(count, 80); |
| 1194 |
} |
| 1195 |
|
| 1196 |
/// Reading exactly one chunk works, when there is a gap. |
| 1197 |
#[test] |
| 1198 |
fn stop_reading_at_gap() { |
| 1199 |
const CHUNK_SIZE: usize = 10; |
| 1200 |
const EXTRA_SIZE: usize = 3; |
| 1201 |
let mut s = RxStreamOrderer::new(); |
| 1202 |
|
| 1203 |
// Add three chunks. |
| 1204 |
s.inbound_frame(0, &[0; CHUNK_SIZE]); |
| 1205 |
let offset = u64::try_from(CHUNK_SIZE + EXTRA_SIZE).unwrap(); |
| 1206 |
s.inbound_frame(offset, &[0; EXTRA_SIZE]); |
| 1207 |
|
| 1208 |
// Read, providing only enough space for the first chunk. |
| 1209 |
let mut buf = [0; 100]; |
| 1210 |
let count = s.read(&mut buf[..CHUNK_SIZE]); |
| 1211 |
assert_eq!(count, CHUNK_SIZE); |
| 1212 |
|
| 1213 |
// Now fill the gap and ensure that everything can be read. |
| 1214 |
let offset = u64::try_from(CHUNK_SIZE).unwrap(); |
| 1215 |
s.inbound_frame(offset, &[0; EXTRA_SIZE]); |
| 1216 |
let count = s.read(&mut buf[..]); |
| 1217 |
assert_eq!(count, EXTRA_SIZE * 2); |
| 1218 |
} |
| 1219 |
|
| 1220 |
/// Reading exactly one chunk works, when there is a gap. |
| 1221 |
#[test] |
| 1222 |
fn stop_reading_in_chunk() { |
| 1223 |
const CHUNK_SIZE: usize = 10; |
| 1224 |
const EXTRA_SIZE: usize = 3; |
| 1225 |
let mut s = RxStreamOrderer::new(); |
| 1226 |
|
| 1227 |
// Add two chunks. |
| 1228 |
s.inbound_frame(0, &[0; CHUNK_SIZE]); |
| 1229 |
let offset = u64::try_from(CHUNK_SIZE).unwrap(); |
| 1230 |
s.inbound_frame(offset, &[0; EXTRA_SIZE]); |
| 1231 |
|
| 1232 |
// Read, providing only enough space for some of the first chunk. |
| 1233 |
let mut buf = [0; 100]; |
| 1234 |
let count = s.read(&mut buf[..CHUNK_SIZE - EXTRA_SIZE]); |
| 1235 |
assert_eq!(count, CHUNK_SIZE - EXTRA_SIZE); |
| 1236 |
|
| 1237 |
let count = s.read(&mut buf[..]); |
| 1238 |
assert_eq!(count, EXTRA_SIZE * 2); |
| 1239 |
} |
| 1240 |
|
| 1241 |
/// Read one byte at a time. |
| 1242 |
#[test] |
| 1243 |
fn read_byte_at_a_time() { |
| 1244 |
const CHUNK_SIZE: usize = 10; |
| 1245 |
const EXTRA_SIZE: usize = 3; |
| 1246 |
let mut s = RxStreamOrderer::new(); |
| 1247 |
|
| 1248 |
// Add two chunks. |
| 1249 |
s.inbound_frame(0, &[0; CHUNK_SIZE]); |
| 1250 |
let offset = u64::try_from(CHUNK_SIZE).unwrap(); |
| 1251 |
s.inbound_frame(offset, &[0; EXTRA_SIZE]); |
| 1252 |
|
| 1253 |
let mut buf = [0; 1]; |
| 1254 |
for _ in 0..CHUNK_SIZE + EXTRA_SIZE { |
| 1255 |
let count = s.read(&mut buf[..]); |
| 1256 |
assert_eq!(count, 1); |
| 1257 |
} |
| 1258 |
assert_eq!(0, s.read(&mut buf[..])); |
| 1259 |
} |
| 1260 |
|
| 1261 |
fn check_stats(stream: &RecvStream, expected_received: u64, expected_read: u64) { |
| 1262 |
let stream_stats = stream.stats(); |
| 1263 |
assert_eq!(expected_received, stream_stats.bytes_received()); |
| 1264 |
assert_eq!(expected_read, stream_stats.bytes_read()); |
| 1265 |
} |
| 1266 |
|
| 1267 |
#[test] |
| 1268 |
fn stream_rx() { |
| 1269 |
let conn_events = ConnectionEvents::default(); |
| 1270 |
|
| 1271 |
let mut s = RecvStream::new( |
| 1272 |
StreamId::from(567), |
| 1273 |
1024, |
| 1274 |
Rc::new(RefCell::new(ReceiverFlowControl::new((), 1024 * 1024))), |
| 1275 |
conn_events, |
| 1276 |
); |
| 1277 |
|
| 1278 |
// test receiving a contig frame and reading it works |
| 1279 |
s.inbound_stream_frame(false, 0, &[1; 10]).unwrap(); |
| 1280 |
assert!(s.data_ready()); |
| 1281 |
check_stats(&s, 10, 0); |
| 1282 |
|
| 1283 |
let mut buf = vec![0u8; 100]; |
| 1284 |
assert_eq!(s.read(&mut buf).unwrap(), (10, false)); |
| 1285 |
assert_eq!(s.state.recv_buf().unwrap().retired(), 10); |
| 1286 |
assert_eq!(s.state.recv_buf().unwrap().buffered(), 0); |
| 1287 |
|
| 1288 |
check_stats(&s, 10, 10); |
| 1289 |
|
| 1290 |
// test receiving a noncontig frame |
| 1291 |
s.inbound_stream_frame(false, 12, &[2; 12]).unwrap(); |
| 1292 |
assert!(!s.data_ready()); |
| 1293 |
assert_eq!(s.read(&mut buf).unwrap(), (0, false)); |
| 1294 |
assert_eq!(s.state.recv_buf().unwrap().retired(), 10); |
| 1295 |
assert_eq!(s.state.recv_buf().unwrap().buffered(), 12); |
| 1296 |
|
| 1297 |
check_stats(&s, 22, 10); |
| 1298 |
|
| 1299 |
// another frame that overlaps the first |
| 1300 |
s.inbound_stream_frame(false, 14, &[3; 8]).unwrap(); |
| 1301 |
assert!(!s.data_ready()); |
| 1302 |
assert_eq!(s.state.recv_buf().unwrap().retired(), 10); |
| 1303 |
assert_eq!(s.state.recv_buf().unwrap().buffered(), 12); |
| 1304 |
|
| 1305 |
check_stats(&s, 22, 10); |
| 1306 |
|
| 1307 |
// fill in the gap, but with a FIN |
| 1308 |
s.inbound_stream_frame(true, 10, &[4; 6]).unwrap_err(); |
| 1309 |
assert!(!s.data_ready()); |
| 1310 |
assert_eq!(s.read(&mut buf).unwrap(), (0, false)); |
| 1311 |
assert_eq!(s.state.recv_buf().unwrap().retired(), 10); |
| 1312 |
assert_eq!(s.state.recv_buf().unwrap().buffered(), 12); |
| 1313 |
|
| 1314 |
check_stats(&s, 22, 10); |
| 1315 |
|
| 1316 |
// fill in the gap |
| 1317 |
s.inbound_stream_frame(false, 10, &[5; 10]).unwrap(); |
| 1318 |
assert!(s.data_ready()); |
| 1319 |
assert_eq!(s.state.recv_buf().unwrap().retired(), 10); |
| 1320 |
assert_eq!(s.state.recv_buf().unwrap().buffered(), 14); |
| 1321 |
|
| 1322 |
check_stats(&s, 24, 10); |
| 1323 |
|
| 1324 |
// a legit FIN |
| 1325 |
s.inbound_stream_frame(true, 24, &[6; 18]).unwrap(); |
| 1326 |
assert_eq!(s.state.recv_buf().unwrap().retired(), 10); |
| 1327 |
assert_eq!(s.state.recv_buf().unwrap().buffered(), 32); |
| 1328 |
assert!(s.data_ready()); |
| 1329 |
assert_eq!(s.read(&mut buf).unwrap(), (32, true)); |
| 1330 |
|
| 1331 |
check_stats(&s, 42, 42); |
| 1332 |
|
| 1333 |
// Stream now no longer readable (is in DataRead state) |
| 1334 |
s.read(&mut buf).unwrap_err(); |
| 1335 |
} |
| 1336 |
|
| 1337 |
fn check_chunks(s: &RxStreamOrderer, expected: &[(u64, usize)]) { |
| 1338 |
assert_eq!(s.data_ranges.len(), expected.len()); |
| 1339 |
for ((start, buf), (expected_start, expected_len)) in s.data_ranges.iter().zip(expected) { |
| 1340 |
assert_eq!((*start, buf.len()), (*expected_start, *expected_len)); |
| 1341 |
} |
| 1342 |
} |
| 1343 |
|
| 1344 |
// Test deduplication when the new data is at the end. |
| 1345 |
#[test] |
| 1346 |
fn stream_rx_dedupe_tail() { |
| 1347 |
let mut s = RxStreamOrderer::new(); |
| 1348 |
|
| 1349 |
s.inbound_frame(0, &[1; 6]); |
| 1350 |
check_chunks(&s, &[(0, 6)]); |
| 1351 |
|
| 1352 |
// New data that overlaps entirely (starting from the head), is ignored. |
| 1353 |
s.inbound_frame(0, &[2; 3]); |
| 1354 |
check_chunks(&s, &[(0, 6)]); |
| 1355 |
|
| 1356 |
// New data that overlaps at the tail has any new data appended. |
| 1357 |
s.inbound_frame(2, &[3; 6]); |
| 1358 |
check_chunks(&s, &[(0, 8)]); |
| 1359 |
|
| 1360 |
// New data that overlaps entirely (up to the tail), is ignored. |
| 1361 |
s.inbound_frame(4, &[4; 4]); |
| 1362 |
check_chunks(&s, &[(0, 8)]); |
| 1363 |
|
| 1364 |
// New data that overlaps, starting from the beginning is appended too. |
| 1365 |
s.inbound_frame(0, &[5; 10]); |
| 1366 |
check_chunks(&s, &[(0, 10)]); |
| 1367 |
|
| 1368 |
// New data that is entirely subsumed is ignored. |
| 1369 |
s.inbound_frame(2, &[6; 2]); |
| 1370 |
check_chunks(&s, &[(0, 10)]); |
| 1371 |
|
| 1372 |
let mut buf = [0; 16]; |
| 1373 |
assert_eq!(s.read(&mut buf[..]), 10); |
| 1374 |
assert_eq!(buf[..10], [1, 1, 1, 1, 1, 1, 3, 3, 5, 5]); |
| 1375 |
} |
| 1376 |
|
| 1377 |
/// When chunks are added before existing data, they aren't merged. |
| 1378 |
#[test] |
| 1379 |
fn stream_rx_dedupe_head() { |
| 1380 |
let mut s = RxStreamOrderer::new(); |
| 1381 |
|
| 1382 |
s.inbound_frame(1, &[6; 6]); |
| 1383 |
check_chunks(&s, &[(1, 6)]); |
| 1384 |
|
| 1385 |
// Insertion before an existing chunk causes truncation of the new chunk. |
| 1386 |
s.inbound_frame(0, &[7; 6]); |
| 1387 |
check_chunks(&s, &[(0, 1), (1, 6)]); |
| 1388 |
|
| 1389 |
// Perfect overlap with existing slices has no effect. |
| 1390 |
s.inbound_frame(0, &[8; 7]); |
| 1391 |
check_chunks(&s, &[(0, 1), (1, 6)]); |
| 1392 |
|
| 1393 |
let mut buf = [0; 16]; |
| 1394 |
assert_eq!(s.read(&mut buf[..]), 7); |
| 1395 |
assert_eq!(buf[..7], [7, 6, 6, 6, 6, 6, 6]); |
| 1396 |
} |
| 1397 |
|
| 1398 |
#[test] |
| 1399 |
fn stream_rx_dedupe_new_tail() { |
| 1400 |
let mut s = RxStreamOrderer::new(); |
| 1401 |
|
| 1402 |
s.inbound_frame(1, &[6; 6]); |
| 1403 |
check_chunks(&s, &[(1, 6)]); |
| 1404 |
|
| 1405 |
// Insertion before an existing chunk causes truncation of the new chunk. |
| 1406 |
s.inbound_frame(0, &[7; 6]); |
| 1407 |
check_chunks(&s, &[(0, 1), (1, 6)]); |
| 1408 |
|
| 1409 |
// New data at the end causes the tail to be added to the first chunk, |
| 1410 |
// replacing later chunks entirely. |
| 1411 |
s.inbound_frame(0, &[9; 8]); |
| 1412 |
check_chunks(&s, &[(0, 8)]); |
| 1413 |
|
| 1414 |
let mut buf = [0; 16]; |
| 1415 |
assert_eq!(s.read(&mut buf[..]), 8); |
| 1416 |
assert_eq!(buf[..8], [7, 9, 9, 9, 9, 9, 9, 9]); |
| 1417 |
} |
| 1418 |
|
| 1419 |
#[test] |
| 1420 |
fn stream_rx_dedupe_replace() { |
| 1421 |
let mut s = RxStreamOrderer::new(); |
| 1422 |
|
| 1423 |
s.inbound_frame(2, &[6; 6]); |
| 1424 |
check_chunks(&s, &[(2, 6)]); |
| 1425 |
|
| 1426 |
// Insertion before an existing chunk causes truncation of the new chunk. |
| 1427 |
s.inbound_frame(1, &[7; 6]); |
| 1428 |
check_chunks(&s, &[(1, 1), (2, 6)]); |
| 1429 |
|
| 1430 |
// New data at the start and end replaces all the slices. |
| 1431 |
s.inbound_frame(0, &[9; 10]); |
| 1432 |
check_chunks(&s, &[(0, 10)]); |
| 1433 |
|
| 1434 |
let mut buf = [0; 16]; |
| 1435 |
assert_eq!(s.read(&mut buf[..]), 10); |
| 1436 |
assert_eq!(buf[..10], [9; 10]); |
| 1437 |
} |
| 1438 |
|
| 1439 |
#[test] |
| 1440 |
fn trim_retired() { |
| 1441 |
let mut s = RxStreamOrderer::new(); |
| 1442 |
|
| 1443 |
let mut buf = [0; 18]; |
| 1444 |
s.inbound_frame(0, &[1; 10]); |
| 1445 |
|
| 1446 |
// Partially read slices are retained. |
| 1447 |
assert_eq!(s.read(&mut buf[..6]), 6); |
| 1448 |
check_chunks(&s, &[(0, 10)]); |
| 1449 |
|
| 1450 |
// Partially read slices are kept and so are added to. |
| 1451 |
s.inbound_frame(3, &buf[..10]); |
| 1452 |
check_chunks(&s, &[(0, 13)]); |
| 1453 |
|
| 1454 |
// Wholly read pieces are dropped. |
| 1455 |
assert_eq!(s.read(&mut buf[..]), 7); |
| 1456 |
assert!(s.data_ranges.is_empty()); |
| 1457 |
|
| 1458 |
// New data that overlaps with retired data is trimmed. |
| 1459 |
s.inbound_frame(0, &buf[..]); |
| 1460 |
check_chunks(&s, &[(13, 5)]); |
| 1461 |
} |
| 1462 |
|
| 1463 |
#[test] |
| 1464 |
fn stream_flowc_update() { |
| 1465 |
let mut s = create_stream(1024 * INITIAL_RECV_WINDOW_SIZE as u64); |
| 1466 |
let mut buf = vec![0u8; INITIAL_RECV_WINDOW_SIZE + 100]; // Make it overlarge |
| 1467 |
|
| 1468 |
assert!(!s.has_frames_to_write()); |
| 1469 |
let big_buf = vec![0; INITIAL_RECV_WINDOW_SIZE]; |
| 1470 |
s.inbound_stream_frame(false, 0, &big_buf).unwrap(); |
| 1471 |
assert!(!s.has_frames_to_write()); |
| 1472 |
assert_eq!(s.read(&mut buf).unwrap(), (INITIAL_RECV_WINDOW_SIZE, false)); |
| 1473 |
assert!(!s.data_ready()); |
| 1474 |
|
| 1475 |
// flow msg generated! |
| 1476 |
assert!(s.has_frames_to_write()); |
| 1477 |
|
| 1478 |
// consume it |
| 1479 |
let mut builder = PacketBuilder::short(Encoder::new(), false, None::<&[u8]>); |
| 1480 |
let mut token = Vec::new(); |
| 1481 |
s.write_frame( |
| 1482 |
&mut builder, |
| 1483 |
&mut token, |
| 1484 |
&mut FrameStats::default(), |
| 1485 |
Instant::now(), |
| 1486 |
Duration::from_millis(100), |
| 1487 |
); |
| 1488 |
|
| 1489 |
// it should be gone |
| 1490 |
assert!(!s.has_frames_to_write()); |
| 1491 |
} |
| 1492 |
|
| 1493 |
fn create_stream(session_fc: u64) -> RecvStream { |
| 1494 |
let conn_events = ConnectionEvents::default(); |
| 1495 |
RecvStream::new( |
| 1496 |
StreamId::from(67), |
| 1497 |
INITIAL_RECV_WINDOW_SIZE as u64, |
| 1498 |
Rc::new(RefCell::new(ReceiverFlowControl::new((), session_fc))), |
| 1499 |
conn_events, |
| 1500 |
) |
| 1501 |
} |
| 1502 |
|
| 1503 |
#[test] |
| 1504 |
fn stream_max_stream_data() { |
| 1505 |
let mut s = create_stream(1024 * INITIAL_RECV_WINDOW_SIZE as u64); |
| 1506 |
assert!(!s.has_frames_to_write()); |
| 1507 |
let big_buf = vec![0; INITIAL_RECV_WINDOW_SIZE]; |
| 1508 |
s.inbound_stream_frame(false, 0, &big_buf).unwrap(); |
| 1509 |
s.inbound_stream_frame(false, INITIAL_RECV_WINDOW_SIZE as u64, &[1; 1]) |
| 1510 |
.unwrap_err(); |
| 1511 |
} |
| 1512 |
|
| 1513 |
#[test] |
| 1514 |
fn stream_orderer_bytes_ready() { |
| 1515 |
let mut rx_ord = RxStreamOrderer::new(); |
| 1516 |
|
| 1517 |
rx_ord.inbound_frame(0, &[1; 6]); |
| 1518 |
assert_eq!(rx_ord.bytes_ready(), 6); |
| 1519 |
assert_eq!(rx_ord.buffered(), 6); |
| 1520 |
assert_eq!(rx_ord.retired(), 0); |
| 1521 |
|
| 1522 |
// read some so there's an offset into the first frame |
| 1523 |
let mut buf = [0u8; 10]; |
| 1524 |
rx_ord.read(&mut buf[..2]); |
| 1525 |
assert_eq!(rx_ord.bytes_ready(), 4); |
| 1526 |
assert_eq!(rx_ord.buffered(), 4); |
| 1527 |
assert_eq!(rx_ord.retired(), 2); |
| 1528 |
|
| 1529 |
// an overlapping frame |
| 1530 |
rx_ord.inbound_frame(5, &[2; 6]); |
| 1531 |
assert_eq!(rx_ord.bytes_ready(), 9); |
| 1532 |
assert_eq!(rx_ord.buffered(), 9); |
| 1533 |
assert_eq!(rx_ord.retired(), 2); |
| 1534 |
|
| 1535 |
// a noncontig frame |
| 1536 |
rx_ord.inbound_frame(20, &[3; 6]); |
| 1537 |
assert_eq!(rx_ord.bytes_ready(), 9); |
| 1538 |
assert_eq!(rx_ord.buffered(), 15); |
| 1539 |
assert_eq!(rx_ord.retired(), 2); |
| 1540 |
|
| 1541 |
// an old frame |
| 1542 |
rx_ord.inbound_frame(0, &[4; 2]); |
| 1543 |
assert_eq!(rx_ord.bytes_ready(), 9); |
| 1544 |
assert_eq!(rx_ord.buffered(), 15); |
| 1545 |
assert_eq!(rx_ord.retired(), 2); |
| 1546 |
} |
| 1547 |
|
| 1548 |
#[test] |
| 1549 |
fn no_stream_flowc_event_after_exiting_recv() { |
| 1550 |
let mut s = create_stream(1024 * INITIAL_RECV_WINDOW_SIZE as u64); |
| 1551 |
let mut buf = vec![0; INITIAL_RECV_WINDOW_SIZE]; |
| 1552 |
// Write from buf at first. |
| 1553 |
s.inbound_stream_frame(false, 0, &buf).unwrap(); |
| 1554 |
// Then read into it. |
| 1555 |
s.read(&mut buf).unwrap(); |
| 1556 |
assert!(s.has_frames_to_write()); |
| 1557 |
s.inbound_stream_frame(true, INITIAL_RECV_WINDOW_SIZE as u64, &[]) |
| 1558 |
.unwrap(); |
| 1559 |
assert!(!s.has_frames_to_write()); |
| 1560 |
} |
| 1561 |
|
| 1562 |
fn create_stream_with_fc( |
| 1563 |
session_fc: Rc<RefCell<ReceiverFlowControl<()>>>, |
| 1564 |
fc_limit: u64, |
| 1565 |
) -> RecvStream { |
| 1566 |
RecvStream::new( |
| 1567 |
StreamId::from(567), |
| 1568 |
fc_limit, |
| 1569 |
session_fc, |
| 1570 |
ConnectionEvents::default(), |
| 1571 |
) |
| 1572 |
} |
| 1573 |
|
| 1574 |
fn create_stream_session_flow_control() -> (RecvStream, Rc<RefCell<ReceiverFlowControl<()>>>) { |
| 1575 |
static_assertions::const_assert!(INITIAL_RECV_WINDOW_SIZE > SESSION_WINDOW); |
| 1576 |
let session_fc = Rc::new(RefCell::new(ReceiverFlowControl::new( |
| 1577 |
(), |
| 1578 |
u64::try_from(SESSION_WINDOW).unwrap(), |
| 1579 |
))); |
| 1580 |
( |
| 1581 |
create_stream_with_fc(Rc::clone(&session_fc), INITIAL_RECV_WINDOW_SIZE as u64), |
| 1582 |
session_fc, |
| 1583 |
) |
| 1584 |
} |
| 1585 |
|
| 1586 |
#[test] |
| 1587 |
fn session_flow_control() { |
| 1588 |
let (mut s, session_fc) = create_stream_session_flow_control(); |
| 1589 |
|
| 1590 |
s.inbound_stream_frame(false, 0, &[0; SESSION_WINDOW]) |
| 1591 |
.unwrap(); |
| 1592 |
assert!(!session_fc.borrow().frame_needed()); |
| 1593 |
// The buffer is big enough to hold SESSION_WINDOW, this will make sure that we always |
| 1594 |
// read everything from he stream. |
| 1595 |
let mut buf = [0; 2 * SESSION_WINDOW]; |
| 1596 |
s.read(&mut buf).unwrap(); |
| 1597 |
assert!(session_fc.borrow().frame_needed()); |
| 1598 |
// consume it |
| 1599 |
let mut builder = PacketBuilder::short(Encoder::new(), false, None::<&[u8]>); |
| 1600 |
let mut token = Vec::new(); |
| 1601 |
session_fc |
| 1602 |
.borrow_mut() |
| 1603 |
.write_frames(&mut builder, &mut token, &mut FrameStats::default()); |
| 1604 |
|
| 1605 |
// Switch to SizeKnown state |
| 1606 |
s.inbound_stream_frame(true, 2 * u64::try_from(SESSION_WINDOW).unwrap() - 1, &[0]) |
| 1607 |
.unwrap(); |
| 1608 |
assert!(!session_fc.borrow().frame_needed()); |
| 1609 |
// Receive new data that can be read. |
| 1610 |
s.inbound_stream_frame( |
| 1611 |
false, |
| 1612 |
u64::try_from(SESSION_WINDOW).unwrap(), |
| 1613 |
&[0; SESSION_WINDOW / 2 + 1], |
| 1614 |
) |
| 1615 |
.unwrap(); |
| 1616 |
assert!(!session_fc.borrow().frame_needed()); |
| 1617 |
s.read(&mut buf).unwrap(); |
| 1618 |
assert!(session_fc.borrow().frame_needed()); |
| 1619 |
// consume it |
| 1620 |
let mut builder = PacketBuilder::short(Encoder::new(), false, None::<&[u8]>); |
| 1621 |
let mut token = Vec::new(); |
| 1622 |
session_fc |
| 1623 |
.borrow_mut() |
| 1624 |
.write_frames(&mut builder, &mut token, &mut FrameStats::default()); |
| 1625 |
|
| 1626 |
// Test DataRecvd state |
| 1627 |
let session_fc = Rc::new(RefCell::new(ReceiverFlowControl::new( |
| 1628 |
(), |
| 1629 |
u64::try_from(SESSION_WINDOW).unwrap(), |
| 1630 |
))); |
| 1631 |
let mut s = RecvStream::new( |
| 1632 |
StreamId::from(567), |
| 1633 |
INITIAL_RECV_WINDOW_SIZE as u64, |
| 1634 |
Rc::clone(&session_fc), |
| 1635 |
ConnectionEvents::default(), |
| 1636 |
); |
| 1637 |
|
| 1638 |
s.inbound_stream_frame(true, 0, &[0; SESSION_WINDOW]) |
| 1639 |
.unwrap(); |
| 1640 |
assert!(!session_fc.borrow().frame_needed()); |
| 1641 |
s.read(&mut buf).unwrap(); |
| 1642 |
assert!(session_fc.borrow().frame_needed()); |
| 1643 |
} |
| 1644 |
|
| 1645 |
#[test] |
| 1646 |
fn session_flow_control_reset() { |
| 1647 |
let (mut s, session_fc) = create_stream_session_flow_control(); |
| 1648 |
|
| 1649 |
s.inbound_stream_frame(false, 0, &[0; SESSION_WINDOW / 2]) |
| 1650 |
.unwrap(); |
| 1651 |
assert!(!session_fc.borrow().frame_needed()); |
| 1652 |
|
| 1653 |
s.reset( |
| 1654 |
Error::NoError.code(), |
| 1655 |
u64::try_from(SESSION_WINDOW).unwrap(), |
| 1656 |
) |
| 1657 |
.unwrap(); |
| 1658 |
assert!(session_fc.borrow().frame_needed()); |
| 1659 |
} |
| 1660 |
|
| 1661 |
fn check_fc<T: Debug>(fc: &ReceiverFlowControl<T>, consumed: u64, retired: u64) { |
| 1662 |
assert_eq!(fc.consumed(), consumed); |
| 1663 |
assert_eq!(fc.retired(), retired); |
| 1664 |
} |
| 1665 |
|
| 1666 |
/// Test consuming the flow control in `RecvStreamState::Recv` |
| 1667 |
#[test] |
| 1668 |
fn fc_state_recv_1() { |
| 1669 |
const SW: u64 = 1024; |
| 1670 |
const SW_US: usize = 1024; |
| 1671 |
let fc = Rc::new(RefCell::new(ReceiverFlowControl::new((), SW))); |
| 1672 |
let mut s = create_stream_with_fc(Rc::clone(&fc), SW * 3 / 4); |
| 1673 |
|
| 1674 |
check_fc(&fc.borrow(), 0, 0); |
| 1675 |
check_fc(s.fc().unwrap(), 0, 0); |
| 1676 |
|
| 1677 |
s.inbound_stream_frame(false, 0, &[0; SW_US / 4]).unwrap(); |
| 1678 |
|
| 1679 |
check_fc(&fc.borrow(), SW / 4, 0); |
| 1680 |
check_fc(s.fc().unwrap(), SW / 4, 0); |
| 1681 |
} |
| 1682 |
|
| 1683 |
/// Test consuming the flow control in `RecvStreamState::Recv` |
| 1684 |
/// with multiple streams |
| 1685 |
#[test] |
| 1686 |
fn fc_state_recv_2() { |
| 1687 |
const SW: u64 = 1024; |
| 1688 |
const SW_US: usize = 1024; |
| 1689 |
let fc = Rc::new(RefCell::new(ReceiverFlowControl::new((), SW))); |
| 1690 |
let mut s1 = create_stream_with_fc(Rc::clone(&fc), SW * 3 / 4); |
| 1691 |
let mut s2 = create_stream_with_fc(Rc::clone(&fc), SW * 3 / 4); |
| 1692 |
|
| 1693 |
check_fc(&fc.borrow(), 0, 0); |
| 1694 |
check_fc(s1.fc().unwrap(), 0, 0); |
| 1695 |
check_fc(s2.fc().unwrap(), 0, 0); |
| 1696 |
|
| 1697 |
s1.inbound_stream_frame(false, 0, &[0; SW_US / 4]).unwrap(); |
| 1698 |
|
| 1699 |
check_fc(&fc.borrow(), SW / 4, 0); |
| 1700 |
check_fc(s1.fc().unwrap(), SW / 4, 0); |
| 1701 |
check_fc(s2.fc().unwrap(), 0, 0); |
| 1702 |
|
| 1703 |
s2.inbound_stream_frame(false, 0, &[0; SW_US / 4]).unwrap(); |
| 1704 |
|
| 1705 |
check_fc(&fc.borrow(), SW / 2, 0); |
| 1706 |
check_fc(s1.fc().unwrap(), SW / 4, 0); |
| 1707 |
check_fc(s2.fc().unwrap(), SW / 4, 0); |
| 1708 |
} |
| 1709 |
|
| 1710 |
/// Test retiring the flow control in `RecvStreamState::Recv` |
| 1711 |
/// with multiple streams |
| 1712 |
#[test] |
| 1713 |
fn fc_state_recv_3() { |
| 1714 |
const SW: u64 = 1024; |
| 1715 |
const SW_US: usize = 1024; |
| 1716 |
let fc = Rc::new(RefCell::new(ReceiverFlowControl::new((), SW))); |
| 1717 |
let mut s1 = create_stream_with_fc(Rc::clone(&fc), SW * 3 / 4); |
| 1718 |
let mut s2 = create_stream_with_fc(Rc::clone(&fc), SW * 3 / 4); |
| 1719 |
|
| 1720 |
check_fc(&fc.borrow(), 0, 0); |
| 1721 |
check_fc(s1.fc().unwrap(), 0, 0); |
| 1722 |
check_fc(s2.fc().unwrap(), 0, 0); |
| 1723 |
|
| 1724 |
s1.inbound_stream_frame(false, 0, &[0; SW_US / 4]).unwrap(); |
| 1725 |
s2.inbound_stream_frame(false, 0, &[0; SW_US / 4]).unwrap(); |
| 1726 |
check_fc(&fc.borrow(), SW / 2, 0); |
| 1727 |
check_fc(s1.fc().unwrap(), SW / 4, 0); |
| 1728 |
check_fc(s2.fc().unwrap(), SW / 4, 0); |
| 1729 |
|
| 1730 |
// Read data |
| 1731 |
let mut buf = [1; SW_US]; |
| 1732 |
assert_eq!(s1.read(&mut buf).unwrap(), (SW_US / 4, false)); |
| 1733 |
check_fc(&fc.borrow(), SW / 2, SW / 4); |
| 1734 |
check_fc(s1.fc().unwrap(), SW / 4, SW / 4); |
| 1735 |
check_fc(s2.fc().unwrap(), SW / 4, 0); |
| 1736 |
|
| 1737 |
assert_eq!(s2.read(&mut buf).unwrap(), (SW_US / 4, false)); |
| 1738 |
check_fc(&fc.borrow(), SW / 2, SW / 2); |
| 1739 |
check_fc(s1.fc().unwrap(), SW / 4, SW / 4); |
| 1740 |
check_fc(s2.fc().unwrap(), SW / 4, SW / 4); |
| 1741 |
|
| 1742 |
// Read when there is no more date to be read will not change fc. |
| 1743 |
assert_eq!(s1.read(&mut buf).unwrap(), (0, false)); |
| 1744 |
check_fc(&fc.borrow(), SW / 2, SW / 2); |
| 1745 |
check_fc(s1.fc().unwrap(), SW / 4, SW / 4); |
| 1746 |
check_fc(s2.fc().unwrap(), SW / 4, SW / 4); |
| 1747 |
|
| 1748 |
// Receiving more data on a stream. |
| 1749 |
s1.inbound_stream_frame(false, SW / 4, &[0; SW_US / 4]) |
| 1750 |
.unwrap(); |
| 1751 |
check_fc(&fc.borrow(), SW * 3 / 4, SW / 2); |
| 1752 |
check_fc(s1.fc().unwrap(), SW / 2, SW / 4); |
| 1753 |
check_fc(s2.fc().unwrap(), SW / 4, SW / 4); |
| 1754 |
|
| 1755 |
// Read data |
| 1756 |
assert_eq!(s1.read(&mut buf).unwrap(), (SW_US / 4, false)); |
| 1757 |
check_fc(&fc.borrow(), SW * 3 / 4, SW * 3 / 4); |
| 1758 |
check_fc(s1.fc().unwrap(), SW / 2, SW / 2); |
| 1759 |
check_fc(s2.fc().unwrap(), SW / 4, SW / 4); |
| 1760 |
} |
| 1761 |
|
| 1762 |
/// Test consuming the flow control in `RecvStreamState::Recv` - duplicate data |
| 1763 |
#[test] |
| 1764 |
fn fc_state_recv_4() { |
| 1765 |
const SW: u64 = 1024; |
| 1766 |
const SW_US: usize = 1024; |
| 1767 |
let fc = Rc::new(RefCell::new(ReceiverFlowControl::new((), SW))); |
| 1768 |
let mut s = create_stream_with_fc(Rc::clone(&fc), SW * 3 / 4); |
| 1769 |
|
| 1770 |
check_fc(&fc.borrow(), 0, 0); |
| 1771 |
check_fc(s.fc().unwrap(), 0, 0); |
| 1772 |
|
| 1773 |
s.inbound_stream_frame(false, 0, &[0; SW_US / 4]).unwrap(); |
| 1774 |
|
| 1775 |
check_fc(&fc.borrow(), SW / 4, 0); |
| 1776 |
check_fc(s.fc().unwrap(), SW / 4, 0); |
| 1777 |
|
| 1778 |
// Receiving duplicate frames (already consumed data) will not cause an error or |
| 1779 |
// change fc. |
| 1780 |
s.inbound_stream_frame(false, 0, &[0; SW_US / 8]).unwrap(); |
| 1781 |
check_fc(&fc.borrow(), SW / 4, 0); |
| 1782 |
check_fc(s.fc().unwrap(), SW / 4, 0); |
| 1783 |
} |
| 1784 |
|
| 1785 |
/// Test consuming the flow control in `RecvStreamState::Recv` - filling a gap in the |
| 1786 |
/// data stream. |
| 1787 |
#[test] |
| 1788 |
fn fc_state_recv_5() { |
| 1789 |
const SW: u64 = 1024; |
| 1790 |
const SW_US: usize = 1024; |
| 1791 |
let fc = Rc::new(RefCell::new(ReceiverFlowControl::new((), SW))); |
| 1792 |
let mut s = create_stream_with_fc(Rc::clone(&fc), SW * 3 / 4); |
| 1793 |
|
| 1794 |
// Receive out of order data. |
| 1795 |
s.inbound_stream_frame(false, SW / 8, &[0; SW_US / 8]) |
| 1796 |
.unwrap(); |
| 1797 |
check_fc(&fc.borrow(), SW / 4, 0); |
| 1798 |
check_fc(s.fc().unwrap(), SW / 4, 0); |
| 1799 |
|
| 1800 |
// Filling in the gap will not change fc. |
| 1801 |
s.inbound_stream_frame(false, 0, &[0; SW_US / 8]).unwrap(); |
| 1802 |
check_fc(&fc.borrow(), SW / 4, 0); |
| 1803 |
check_fc(s.fc().unwrap(), SW / 4, 0); |
| 1804 |
} |
| 1805 |
|
| 1806 |
/// Test consuming the flow control in `RecvStreamState::Recv` - receiving frame past |
| 1807 |
/// the flow control will cause an error. |
| 1808 |
#[test] |
| 1809 |
fn fc_state_recv_6() { |
| 1810 |
const SW: u64 = 1024; |
| 1811 |
const SW_US: usize = 1024; |
| 1812 |
let fc = Rc::new(RefCell::new(ReceiverFlowControl::new((), SW))); |
| 1813 |
let mut s = create_stream_with_fc(Rc::clone(&fc), SW * 3 / 4); |
| 1814 |
|
| 1815 |
// Receiving frame past the flow control will cause an error. |
| 1816 |
assert_eq!( |
| 1817 |
s.inbound_stream_frame(false, 0, &[0; SW_US * 3 / 4 + 1]), |
| 1818 |
Err(Error::FlowControlError) |
| 1819 |
); |
| 1820 |
} |
| 1821 |
|
| 1822 |
/// Test that the flow controls will send updates. |
| 1823 |
#[expect( |
| 1824 |
clippy::too_many_lines, |
| 1825 |
clippy::cast_possible_truncation, |
| 1826 |
reason = "This is test code." |
| 1827 |
)] |
| 1828 |
#[test] |
| 1829 |
fn fc_state_recv_7() { |
| 1830 |
const CONNECTION_WINDOW: u64 = 1024; |
| 1831 |
const CONNECTION_WINDOW_US: usize = CONNECTION_WINDOW as usize; |
| 1832 |
|
| 1833 |
const STREAM_WINDOW: u64 = CONNECTION_WINDOW / 2; |
| 1834 |
const STREAM_WINDOW_US: usize = STREAM_WINDOW as usize; |
| 1835 |
|
| 1836 |
const WINDOW_UPDATE_FRACTION_US: usize = WINDOW_UPDATE_FRACTION as usize; |
| 1837 |
|
| 1838 |
let fc = Rc::new(RefCell::new(ReceiverFlowControl::new( |
| 1839 |
(), |
| 1840 |
CONNECTION_WINDOW, |
| 1841 |
))); |
| 1842 |
let mut s = create_stream_with_fc(Rc::clone(&fc), STREAM_WINDOW); |
| 1843 |
|
| 1844 |
check_fc(&fc.borrow(), 0, 0); |
| 1845 |
check_fc(s.fc().unwrap(), 0, 0); |
| 1846 |
|
| 1847 |
// Receive data up to but not over the fc update trigger point. |
| 1848 |
s.inbound_stream_frame(false, 0, &[0; STREAM_WINDOW_US / WINDOW_UPDATE_FRACTION_US]) |
| 1849 |
.unwrap(); |
| 1850 |
let mut buf = [1; CONNECTION_WINDOW_US]; |
| 1851 |
assert_eq!( |
| 1852 |
s.read(&mut buf).unwrap(), |
| 1853 |
(STREAM_WINDOW_US / WINDOW_UPDATE_FRACTION_US, false) |
| 1854 |
); |
| 1855 |
check_fc( |
| 1856 |
&fc.borrow(), |
| 1857 |
STREAM_WINDOW / WINDOW_UPDATE_FRACTION, |
| 1858 |
STREAM_WINDOW / WINDOW_UPDATE_FRACTION, |
| 1859 |
); |
| 1860 |
check_fc( |
| 1861 |
s.fc().unwrap(), |
| 1862 |
STREAM_WINDOW / WINDOW_UPDATE_FRACTION, |
| 1863 |
STREAM_WINDOW / WINDOW_UPDATE_FRACTION, |
| 1864 |
); |
| 1865 |
|
| 1866 |
// Still no fc update needed. |
| 1867 |
assert!(!fc.borrow().frame_needed()); |
| 1868 |
assert!(!s.fc().unwrap().frame_needed()); |
| 1869 |
|
| 1870 |
// Receive one more byte that will cause a fc update after it is read. |
| 1871 |
s.inbound_stream_frame(false, STREAM_WINDOW / WINDOW_UPDATE_FRACTION, &[0]) |
| 1872 |
.unwrap(); |
| 1873 |
check_fc( |
| 1874 |
&fc.borrow(), |
| 1875 |
STREAM_WINDOW / WINDOW_UPDATE_FRACTION + 1, |
| 1876 |
STREAM_WINDOW / WINDOW_UPDATE_FRACTION, |
| 1877 |
); |
| 1878 |
check_fc( |
| 1879 |
s.fc().unwrap(), |
| 1880 |
STREAM_WINDOW / WINDOW_UPDATE_FRACTION + 1, |
| 1881 |
STREAM_WINDOW / WINDOW_UPDATE_FRACTION, |
| 1882 |
); |
| 1883 |
// Only consuming data does not cause a fc update to be sent. |
| 1884 |
assert!(!fc.borrow().frame_needed()); |
| 1885 |
assert!(!s.fc().unwrap().frame_needed()); |
| 1886 |
|
| 1887 |
assert_eq!(s.read(&mut buf).unwrap(), (1, false)); |
| 1888 |
check_fc( |
| 1889 |
&fc.borrow(), |
| 1890 |
STREAM_WINDOW / WINDOW_UPDATE_FRACTION + 1, |
| 1891 |
STREAM_WINDOW / WINDOW_UPDATE_FRACTION + 1, |
| 1892 |
); |
| 1893 |
check_fc( |
| 1894 |
s.fc().unwrap(), |
| 1895 |
STREAM_WINDOW / WINDOW_UPDATE_FRACTION + 1, |
| 1896 |
STREAM_WINDOW / WINDOW_UPDATE_FRACTION + 1, |
| 1897 |
); |
| 1898 |
// Data are retired and the stream fc will send an update. |
| 1899 |
assert!(!fc.borrow().frame_needed()); |
| 1900 |
assert!(s.fc().unwrap().frame_needed()); |
| 1901 |
|
| 1902 |
// Receive more data to increase fc further. |
| 1903 |
s.inbound_stream_frame( |
| 1904 |
false, |
| 1905 |
STREAM_WINDOW / WINDOW_UPDATE_FRACTION, |
| 1906 |
&[0; STREAM_WINDOW_US / WINDOW_UPDATE_FRACTION_US], |
| 1907 |
) |
| 1908 |
.unwrap(); |
| 1909 |
assert_eq!( |
| 1910 |
s.read(&mut buf).unwrap(), |
| 1911 |
(STREAM_WINDOW_US / WINDOW_UPDATE_FRACTION_US - 1, false) |
| 1912 |
); |
| 1913 |
check_fc( |
| 1914 |
&fc.borrow(), |
| 1915 |
STREAM_WINDOW * 2 / WINDOW_UPDATE_FRACTION, |
| 1916 |
STREAM_WINDOW * 2 / WINDOW_UPDATE_FRACTION, |
| 1917 |
); |
| 1918 |
check_fc( |
| 1919 |
s.fc().unwrap(), |
| 1920 |
STREAM_WINDOW * 2 / WINDOW_UPDATE_FRACTION, |
| 1921 |
STREAM_WINDOW * 2 / WINDOW_UPDATE_FRACTION, |
| 1922 |
); |
| 1923 |
assert!(!fc.borrow().frame_needed()); |
| 1924 |
assert!(s.fc().unwrap().frame_needed()); |
| 1925 |
|
| 1926 |
// Write the fc update frame |
| 1927 |
let mut builder = PacketBuilder::short(Encoder::new(), false, None::<&[u8]>); |
| 1928 |
let mut token = Vec::new(); |
| 1929 |
let mut stats = FrameStats::default(); |
| 1930 |
fc.borrow_mut() |
| 1931 |
.write_frames(&mut builder, &mut token, &mut stats); |
| 1932 |
assert_eq!(stats.max_data, 0); |
| 1933 |
s.write_frame( |
| 1934 |
&mut builder, |
| 1935 |
&mut token, |
| 1936 |
&mut stats, |
| 1937 |
Instant::now(), |
| 1938 |
Duration::from_millis(100), |
| 1939 |
); |
| 1940 |
assert_eq!(stats.max_stream_data, 1); |
| 1941 |
|
| 1942 |
// Receive 1 byte that will cause a session fc update after it is read. |
| 1943 |
s.inbound_stream_frame(false, STREAM_WINDOW * 2 / WINDOW_UPDATE_FRACTION, &[0]) |
| 1944 |
.unwrap(); |
| 1945 |
assert_eq!(s.read(&mut buf).unwrap(), (1, false)); |
| 1946 |
check_fc( |
| 1947 |
&fc.borrow(), |
| 1948 |
STREAM_WINDOW * 2 / WINDOW_UPDATE_FRACTION + 1, |
| 1949 |
STREAM_WINDOW * 2 / WINDOW_UPDATE_FRACTION + 1, |
| 1950 |
); |
| 1951 |
check_fc( |
| 1952 |
s.fc().unwrap(), |
| 1953 |
STREAM_WINDOW * 2 / WINDOW_UPDATE_FRACTION + 1, |
| 1954 |
STREAM_WINDOW * 2 / WINDOW_UPDATE_FRACTION + 1, |
| 1955 |
); |
| 1956 |
assert!(fc.borrow().frame_needed()); |
| 1957 |
assert!(!s.fc().unwrap().frame_needed()); |
| 1958 |
fc.borrow_mut() |
| 1959 |
.write_frames(&mut builder, &mut token, &mut stats); |
| 1960 |
assert_eq!(stats.max_data, 1); |
| 1961 |
s.write_frame( |
| 1962 |
&mut builder, |
| 1963 |
&mut token, |
| 1964 |
&mut stats, |
| 1965 |
Instant::now(), |
| 1966 |
Duration::from_millis(100), |
| 1967 |
); |
| 1968 |
assert_eq!(stats.max_stream_data, 1); |
| 1969 |
} |
| 1970 |
|
| 1971 |
/// Test flow control in `RecvStreamState::SizeKnown` |
| 1972 |
#[test] |
| 1973 |
fn fc_state_size_known() { |
| 1974 |
const SW: u64 = 1024; |
| 1975 |
const SW_US: usize = 1024; |
| 1976 |
let fc = Rc::new(RefCell::new(ReceiverFlowControl::new((), SW))); |
| 1977 |
|
| 1978 |
let mut s = create_stream_with_fc(Rc::clone(&fc), SW); |
| 1979 |
|
| 1980 |
check_fc(&fc.borrow(), 0, 0); |
| 1981 |
check_fc(s.fc().unwrap(), 0, 0); |
| 1982 |
|
| 1983 |
s.inbound_stream_frame(true, SW / 4, &[0; SW_US / 4]) |
| 1984 |
.unwrap(); |
| 1985 |
check_fc(&fc.borrow(), SW / 2, 0); |
| 1986 |
check_fc(s.fc().unwrap(), SW / 2, 0); |
| 1987 |
|
| 1988 |
// Receiving duplicate frames (already consumed data) will not cause an error or |
| 1989 |
// change fc. |
| 1990 |
s.inbound_stream_frame(true, SW / 4, &[0; SW_US / 4]) |
| 1991 |
.unwrap(); |
| 1992 |
check_fc(&fc.borrow(), SW / 2, 0); |
| 1993 |
check_fc(s.fc().unwrap(), SW / 2, 0); |
| 1994 |
|
| 1995 |
// The stream can still receive duplicate data without a fin bit. |
| 1996 |
s.inbound_stream_frame(false, SW / 4, &[0; SW_US / 4]) |
| 1997 |
.unwrap(); |
| 1998 |
check_fc(&fc.borrow(), SW / 2, 0); |
| 1999 |
check_fc(s.fc().unwrap(), SW / 2, 0); |
| 2000 |
|
| 2001 |
// Receiving frame past the final size of a stream will return an error. |
| 2002 |
assert_eq!( |
| 2003 |
s.inbound_stream_frame(true, SW / 4, &[0; SW_US / 4 + 1]), |
| 2004 |
Err(Error::FinalSizeError) |
| 2005 |
); |
| 2006 |
check_fc(&fc.borrow(), SW / 2, 0); |
| 2007 |
check_fc(s.fc().unwrap(), SW / 2, 0); |
| 2008 |
|
| 2009 |
// Add new data to the gap will not change fc. |
| 2010 |
s.inbound_stream_frame(false, SW / 8, &[0; SW_US / 8]) |
| 2011 |
.unwrap(); |
| 2012 |
check_fc(&fc.borrow(), SW / 2, 0); |
| 2013 |
check_fc(s.fc().unwrap(), SW / 2, 0); |
| 2014 |
|
| 2015 |
// Fill the gap |
| 2016 |
s.inbound_stream_frame(false, 0, &[0; SW_US / 8]).unwrap(); |
| 2017 |
check_fc(&fc.borrow(), SW / 2, 0); |
| 2018 |
check_fc(s.fc().unwrap(), SW / 2, 0); |
| 2019 |
|
| 2020 |
// Read all data |
| 2021 |
let mut buf = [1; SW_US]; |
| 2022 |
assert_eq!(s.read(&mut buf).unwrap(), (SW_US / 2, true)); |
| 2023 |
// the stream does not have fc any more. We can only check the session fc. |
| 2024 |
check_fc(&fc.borrow(), SW / 2, SW / 2); |
| 2025 |
assert!(s.fc().is_none()); |
| 2026 |
} |
| 2027 |
|
| 2028 |
/// Test flow control in `RecvStreamState::DataRecvd` |
| 2029 |
#[test] |
| 2030 |
fn fc_state_data_recv() { |
| 2031 |
const SW: u64 = 1024; |
| 2032 |
const SW_US: usize = 1024; |
| 2033 |
let fc = Rc::new(RefCell::new(ReceiverFlowControl::new((), SW))); |
| 2034 |
|
| 2035 |
let mut s = create_stream_with_fc(Rc::clone(&fc), SW); |
| 2036 |
|
| 2037 |
check_fc(&fc.borrow(), 0, 0); |
| 2038 |
check_fc(s.fc().unwrap(), 0, 0); |
| 2039 |
|
| 2040 |
s.inbound_stream_frame(true, 0, &[0; SW_US / 2]).unwrap(); |
| 2041 |
check_fc(&fc.borrow(), SW / 2, 0); |
| 2042 |
check_fc(s.fc().unwrap(), SW / 2, 0); |
| 2043 |
|
| 2044 |
// Receiving duplicate frames (already consumed data) will not cause an error or |
| 2045 |
// change fc. |
| 2046 |
s.inbound_stream_frame(true, SW / 4, &[0; SW_US / 4]) |
| 2047 |
.unwrap(); |
| 2048 |
check_fc(&fc.borrow(), SW / 2, 0); |
| 2049 |
check_fc(s.fc().unwrap(), SW / 2, 0); |
| 2050 |
|
| 2051 |
// The stream can still receive duplicate data without a fin bit. |
| 2052 |
s.inbound_stream_frame(false, SW / 4, &[0; SW_US / 4]) |
| 2053 |
.unwrap(); |
| 2054 |
check_fc(&fc.borrow(), SW / 2, 0); |
| 2055 |
check_fc(s.fc().unwrap(), SW / 2, 0); |
| 2056 |
|
| 2057 |
// Receiving frame past the final size of a stream will return an error. |
| 2058 |
assert_eq!( |
| 2059 |
s.inbound_stream_frame(true, SW / 4, &[0; SW_US / 4 + 1]), |
| 2060 |
Err(Error::FinalSizeError) |
| 2061 |
); |
| 2062 |
check_fc(&fc.borrow(), SW / 2, 0); |
| 2063 |
check_fc(s.fc().unwrap(), SW / 2, 0); |
| 2064 |
|
| 2065 |
// Read all data |
| 2066 |
let mut buf = [1; SW_US]; |
| 2067 |
assert_eq!(s.read(&mut buf).unwrap(), (SW_US / 2, true)); |
| 2068 |
// the stream does not have fc any more. We can only check the session fc. |
| 2069 |
check_fc(&fc.borrow(), SW / 2, SW / 2); |
| 2070 |
assert!(s.fc().is_none()); |
| 2071 |
} |
| 2072 |
|
| 2073 |
/// Test flow control in `RecvStreamState::DataRead` |
| 2074 |
#[test] |
| 2075 |
fn fc_state_data_read() { |
| 2076 |
const SW: u64 = 1024; |
| 2077 |
const SW_US: usize = 1024; |
| 2078 |
let fc = Rc::new(RefCell::new(ReceiverFlowControl::new((), SW))); |
| 2079 |
|
| 2080 |
let mut s = create_stream_with_fc(Rc::clone(&fc), SW * 3 / 4); |
| 2081 |
check_fc(&fc.borrow(), 0, 0); |
| 2082 |
check_fc(s.fc().unwrap(), 0, 0); |
| 2083 |
|
| 2084 |
s.inbound_stream_frame(true, 0, &[0; SW_US / 2]).unwrap(); |
| 2085 |
check_fc(&fc.borrow(), SW / 2, 0); |
| 2086 |
check_fc(s.fc().unwrap(), SW / 2, 0); |
| 2087 |
|
| 2088 |
let mut buf = [1; SW_US]; |
| 2089 |
assert_eq!(s.read(&mut buf).unwrap(), (SW_US / 2, true)); |
| 2090 |
// the stream does not have fc any more. We can only check the session fc. |
| 2091 |
check_fc(&fc.borrow(), SW / 2, SW / 2); |
| 2092 |
assert!(s.fc().is_none()); |
| 2093 |
|
| 2094 |
// Receiving duplicate frames (already consumed data) will not cause an error or |
| 2095 |
// change fc. |
| 2096 |
s.inbound_stream_frame(true, 0, &[0; SW_US / 2]).unwrap(); |
| 2097 |
// the stream does not have fc any more. We can only check the session fc. |
| 2098 |
check_fc(&fc.borrow(), SW / 2, SW / 2); |
| 2099 |
assert!(s.fc().is_none()); |
| 2100 |
|
| 2101 |
// Receiving frame past the final size of a stream or the stream's fc limit |
| 2102 |
// will NOT return an error. |
| 2103 |
s.inbound_stream_frame(true, 0, &[0; SW_US / 2 + 1]) |
| 2104 |
.unwrap(); |
| 2105 |
s.inbound_stream_frame(true, 0, &[0; SW_US * 3 / 4 + 1]) |
| 2106 |
.unwrap(); |
| 2107 |
check_fc(&fc.borrow(), SW / 2, SW / 2); |
| 2108 |
assert!(s.fc().is_none()); |
| 2109 |
} |
| 2110 |
|
| 2111 |
/// Test flow control in `RecvStreamState::AbortReading` and final size is known |
| 2112 |
#[test] |
| 2113 |
fn fc_state_abort_reading_1() { |
| 2114 |
const SW: u64 = 1024; |
| 2115 |
const SW_US: usize = 1024; |
| 2116 |
let fc = Rc::new(RefCell::new(ReceiverFlowControl::new((), SW))); |
| 2117 |
|
| 2118 |
let mut s = create_stream_with_fc(Rc::clone(&fc), SW * 3 / 4); |
| 2119 |
check_fc(&fc.borrow(), 0, 0); |
| 2120 |
check_fc(s.fc().unwrap(), 0, 0); |
| 2121 |
|
| 2122 |
s.inbound_stream_frame(true, SW / 4, &[0; SW_US / 4]) |
| 2123 |
.unwrap(); |
| 2124 |
check_fc(&fc.borrow(), SW / 2, 0); |
| 2125 |
check_fc(s.fc().unwrap(), SW / 2, 0); |
| 2126 |
|
| 2127 |
s.stop_sending(Error::NoError.code()); |
| 2128 |
// All data will de retired |
| 2129 |
check_fc(&fc.borrow(), SW / 2, SW / 2); |
| 2130 |
check_fc(s.fc().unwrap(), SW / 2, SW / 2); |
| 2131 |
|
| 2132 |
// Receiving duplicate frames (already consumed data) will not cause an error or |
| 2133 |
// change fc. |
| 2134 |
s.inbound_stream_frame(true, 0, &[0; SW_US / 2]).unwrap(); |
| 2135 |
check_fc(&fc.borrow(), SW / 2, SW / 2); |
| 2136 |
check_fc(s.fc().unwrap(), SW / 2, SW / 2); |
| 2137 |
|
| 2138 |
// The stream can still receive duplicate data without a fin bit. |
| 2139 |
s.inbound_stream_frame(false, SW / 4, &[0; SW_US / 4]) |
| 2140 |
.unwrap(); |
| 2141 |
check_fc(&fc.borrow(), SW / 2, SW / 2); |
| 2142 |
check_fc(s.fc().unwrap(), SW / 2, SW / 2); |
| 2143 |
|
| 2144 |
// Receiving frame past the final size of a stream will return an error. |
| 2145 |
assert_eq!( |
| 2146 |
s.inbound_stream_frame(true, SW / 4, &[0; SW_US / 4 + 1]), |
| 2147 |
Err(Error::FinalSizeError) |
| 2148 |
); |
| 2149 |
check_fc(&fc.borrow(), SW / 2, SW / 2); |
| 2150 |
check_fc(s.fc().unwrap(), SW / 2, SW / 2); |
| 2151 |
} |
| 2152 |
|
| 2153 |
/// Test flow control in `RecvStreamState::AbortReading` and final size is unknown |
| 2154 |
#[test] |
| 2155 |
fn fc_state_abort_reading_2() { |
| 2156 |
const SW: u64 = 1024; |
| 2157 |
const SW_US: usize = 1024; |
| 2158 |
let fc = Rc::new(RefCell::new(ReceiverFlowControl::new((), SW))); |
| 2159 |
|
| 2160 |
let mut s = create_stream_with_fc(Rc::clone(&fc), SW * 3 / 4); |
| 2161 |
check_fc(&fc.borrow(), 0, 0); |
| 2162 |
check_fc(s.fc().unwrap(), 0, 0); |
| 2163 |
|
| 2164 |
s.inbound_stream_frame(false, 0, &[0; SW_US / 2]).unwrap(); |
| 2165 |
check_fc(&fc.borrow(), SW / 2, 0); |
| 2166 |
check_fc(s.fc().unwrap(), SW / 2, 0); |
| 2167 |
|
| 2168 |
s.stop_sending(Error::NoError.code()); |
| 2169 |
// All data will de retired |
| 2170 |
check_fc(&fc.borrow(), SW / 2, SW / 2); |
| 2171 |
check_fc(s.fc().unwrap(), SW / 2, SW / 2); |
| 2172 |
|
| 2173 |
// Receiving duplicate frames (already consumed data) will not cause an error or |
| 2174 |
// change fc. |
| 2175 |
s.inbound_stream_frame(false, 0, &[0; SW_US / 2]).unwrap(); |
| 2176 |
check_fc(&fc.borrow(), SW / 2, SW / 2); |
| 2177 |
check_fc(s.fc().unwrap(), SW / 2, SW / 2); |
| 2178 |
|
| 2179 |
// Receiving data past the flow control limit will cause an error. |
| 2180 |
assert_eq!( |
| 2181 |
s.inbound_stream_frame(false, 0, &[0; SW_US * 3 / 4 + 1]), |
| 2182 |
Err(Error::FlowControlError) |
| 2183 |
); |
| 2184 |
|
| 2185 |
// The stream can still receive duplicate data without a fin bit. |
| 2186 |
s.inbound_stream_frame(false, SW / 4, &[0; SW_US / 4]) |
| 2187 |
.unwrap(); |
| 2188 |
check_fc(&fc.borrow(), SW / 2, SW / 2); |
| 2189 |
check_fc(s.fc().unwrap(), SW / 2, SW / 2); |
| 2190 |
|
| 2191 |
// Receiving more data will case the data to be retired. |
| 2192 |
// The stream can still receive duplicate data without a fin bit. |
| 2193 |
s.inbound_stream_frame(false, SW / 2, &[0; 10]).unwrap(); |
| 2194 |
check_fc(&fc.borrow(), SW / 2 + 10, SW / 2 + 10); |
| 2195 |
check_fc(s.fc().unwrap(), SW / 2 + 10, SW / 2 + 10); |
| 2196 |
|
| 2197 |
// We can still receive the final size. |
| 2198 |
s.inbound_stream_frame(true, SW / 2, &[0; 20]).unwrap(); |
| 2199 |
check_fc(&fc.borrow(), SW / 2 + 20, SW / 2 + 20); |
| 2200 |
check_fc(s.fc().unwrap(), SW / 2 + 20, SW / 2 + 20); |
| 2201 |
|
| 2202 |
// Receiving frame past the final size of a stream will return an error. |
| 2203 |
assert_eq!( |
| 2204 |
s.inbound_stream_frame(true, SW / 2, &[0; 21]), |
| 2205 |
Err(Error::FinalSizeError) |
| 2206 |
); |
| 2207 |
check_fc(&fc.borrow(), SW / 2 + 20, SW / 2 + 20); |
| 2208 |
check_fc(s.fc().unwrap(), SW / 2 + 20, SW / 2 + 20); |
| 2209 |
} |
| 2210 |
|
| 2211 |
/// Test flow control in `RecvStreamState::WaitForReset` |
| 2212 |
#[test] |
| 2213 |
fn fc_state_wait_for_reset() { |
| 2214 |
const SW: u64 = 1024; |
| 2215 |
const SW_US: usize = 1024; |
| 2216 |
let fc = Rc::new(RefCell::new(ReceiverFlowControl::new((), SW))); |
| 2217 |
|
| 2218 |
let mut s = create_stream_with_fc(Rc::clone(&fc), SW * 3 / 4); |
| 2219 |
check_fc(&fc.borrow(), 0, 0); |
| 2220 |
check_fc(s.fc().unwrap(), 0, 0); |
| 2221 |
|
| 2222 |
s.inbound_stream_frame(false, 0, &[0; SW_US / 2]).unwrap(); |
| 2223 |
check_fc(&fc.borrow(), SW / 2, 0); |
| 2224 |
check_fc(s.fc().unwrap(), SW / 2, 0); |
| 2225 |
|
| 2226 |
s.stop_sending(Error::NoError.code()); |
| 2227 |
check_fc(&fc.borrow(), SW / 2, SW / 2); |
| 2228 |
check_fc(s.fc().unwrap(), SW / 2, SW / 2); |
| 2229 |
|
| 2230 |
s.stop_sending_acked(); |
| 2231 |
check_fc(&fc.borrow(), SW / 2, SW / 2); |
| 2232 |
check_fc(s.fc().unwrap(), SW / 2, SW / 2); |
| 2233 |
|
| 2234 |
// Receiving duplicate frames (already consumed data) will not cause an error or |
| 2235 |
// change fc. |
| 2236 |
s.inbound_stream_frame(false, 0, &[0; SW_US / 2]).unwrap(); |
| 2237 |
check_fc(&fc.borrow(), SW / 2, SW / 2); |
| 2238 |
check_fc(s.fc().unwrap(), SW / 2, SW / 2); |
| 2239 |
|
| 2240 |
// Receiving data past the flow control limit will cause an error. |
| 2241 |
assert_eq!( |
| 2242 |
s.inbound_stream_frame(false, 0, &[0; SW_US * 3 / 4 + 1]), |
| 2243 |
Err(Error::FlowControlError) |
| 2244 |
); |
| 2245 |
|
| 2246 |
// The stream can still receive duplicate data without a fin bit. |
| 2247 |
s.inbound_stream_frame(false, SW / 4, &[0; SW_US / 4]) |
| 2248 |
.unwrap(); |
| 2249 |
check_fc(&fc.borrow(), SW / 2, SW / 2); |
| 2250 |
check_fc(s.fc().unwrap(), SW / 2, SW / 2); |
| 2251 |
|
| 2252 |
// Receiving more data will case the data to be retired. |
| 2253 |
// The stream can still receive duplicate data without a fin bit. |
| 2254 |
s.inbound_stream_frame(false, SW / 2, &[0; 10]).unwrap(); |
| 2255 |
check_fc(&fc.borrow(), SW / 2 + 10, SW / 2 + 10); |
| 2256 |
check_fc(s.fc().unwrap(), SW / 2 + 10, SW / 2 + 10); |
| 2257 |
} |
| 2258 |
} |