1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//! This module contains data structures for buffering incoming and outgoing data
//! in Quic streams.
use crate::varint::VarInt;
use alloc::collections::{vec_deque, VecDeque};
use bytes::BytesMut;
mod request;
mod slot;
#[cfg(test)]
mod tests;
use request::Request;
use slot::Slot;
/// Enumerates error that can occur while inserting data into the Receive Buffer
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum ReceiveBufferError {
/// An invalid data range was provided
OutOfRange,
}
/// The default buffer size for slots that the [`ReceiveBuffer`] uses.
///
/// This value was picked as it is typically used for the default memory page size.
const MIN_BUFFER_ALLOCATION_SIZE: usize = 4096;
//= https://www.rfc-editor.org/rfc/rfc9000#section-2.2
//# Endpoints MUST be able to deliver stream data to an application as an
//# ordered byte-stream.
/// `ReceiveBuffer` is a buffer structure for combining chunks of bytes in an
/// ordered stream, which might arrive out of order.
///
/// `ReceiveBuffer` will accumulate the bytes, and provide them to its users
/// once a contiguous range of bytes at the current position of the stream has
/// been accumulated.
///
/// `ReceiveBuffer` is optimized for minimizing memory allocations and for
/// offering it's users chunks of sizes that minimize call overhead.
///
/// If data is received in smaller chunks, only the first chunk will trigger a
/// memory allocation. All other chunks can be copied into the already allocated
/// region.
///
/// When users want to consume data from the buffer, the consumable part of the
/// internal receive buffer is split off and passed back to the caller. Due to
/// this chunk being a view onto a reference-counted internal buffer of type
/// [`BytesMut`] this is also efficient and does not require additional memory
/// allocation or copy.
///
/// ## Usage
///
/// ```rust,ignore
/// use s2n_quic_transport::buffer::ReceiveBuffer;
///
/// let mut buffer = ReceiveBuffer::new();
///
/// // write a chunk of bytes at offset 4, which can not be consumed yet
/// assert!(buffer.write_at(4u32.into(), &[4, 5, 6, 7]).is_ok());
/// assert_eq!(0, buffer.len());
/// assert_eq!(None, buffer.pop());
///
/// // write a chunk of bytes at offset 0, which allows for consumption
/// assert!(buffer.write_at(0u32.into(), &[0, 1, 2, 3]).is_ok());
/// assert_eq!(8, buffer.len());
///
/// // Pop chunks. Since they all fitted into a single internal buffer,
/// // they will be returned in combined fashion.
/// assert_eq!(&[0u8, 1, 2, 3, 4, 5, 6, 7], &buffer.pop().unwrap()[..]);
/// ```
#[derive(Debug)]
pub struct ReceiveBuffer {
slots: VecDeque<Slot>,
start_offset: u64,
}
impl Default for ReceiveBuffer {
fn default() -> Self {
Self::new()
}
}
impl ReceiveBuffer {
/// Creates a new `ReceiveBuffer`
pub fn new() -> ReceiveBuffer {
ReceiveBuffer {
slots: VecDeque::new(),
start_offset: 0,
}
}
/// Returns the amount of bytes available for reading.
/// This equals the amount of data that is stored in contiguous fashion at
/// the start of the buffer.
#[inline]
pub fn len(&self) -> usize {
self.report().0
}
/// Returns true if no bytes are available for reading
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Returns the number of bytes and chunks available for consumption
#[inline]
pub fn report(&self) -> (usize, usize) {
let mut bytes = 0;
let mut chunks = 0;
for chunk in self.iter() {
bytes += chunk.len();
chunks += 1;
}
(bytes, chunks)
}
/// Pushes a slice at a certain offset
#[inline]
pub fn write_at(&mut self, offset: VarInt, data: &[u8]) -> Result<(), ReceiveBufferError> {
// create a request
let request = Request::new(offset, data)?;
// trim off any data that we've already read
let (_, mut request) = request.split(self.start_offset);
// if the request is empty we're done
if request.is_empty() {
return Ok(());
}
// start from the back with the assumption that most data arrives in order
for mut idx in (0..self.slots.len()).rev() {
let slot = &mut self.slots[idx];
let slot::Outcome { lower, mid, upper } = slot.try_write(request);
// if this slot was completed, we should try and unsplit with the next slot
if slot.is_full() {
let current_block =
Self::align_offset(slot.start(), Self::allocation_size(slot.start()));
let end = slot.end();
if let Some(next) = self.slots.get(idx + 1) {
let next_block =
Self::align_offset(next.start(), Self::allocation_size(next.start()));
if next.start() == end && current_block == next_block {
if let Some(next) = self.slots.remove(idx + 1) {
self.slots[idx].unsplit(next);
} else {
debug_assert!(false, "slot should be available");
unsafe {
// Safety: we've already checked that `idx + 1` exists
core::hint::unreachable_unchecked();
}
}
}
}
}
idx += 1;
self.allocate_request(idx, upper);
if let Some(mid) = mid {
self.insert(idx, mid);
}
request = lower;
if request.is_empty() {
break;
}
}
self.allocate_request(0, request);
self.check_consistency();
Ok(())
}
/// Iterates over all of the chunks waiting to be received
#[inline]
pub fn iter(&self) -> impl Iterator<Item = &[u8]> {
Iter::new(self)
}
/// Pops a buffer from the front of the receive queue if available
#[inline]
pub fn pop(&mut self) -> Option<BytesMut> {
self.pop_transform(|buffer| buffer.split())
}
/// Pops a buffer from the front of the receive queue, who's length is always guaranteed to be
/// less than the provided `watermark`.
#[inline]
pub fn pop_watermarked(&mut self, watermark: usize) -> Option<BytesMut> {
self.pop_transform(|buffer| {
// make sure the buffer doesn't exceed the watermark
let watermark = watermark.min(buffer.len());
// if the watermark is 0 then don't needlessly increment refcounts
if watermark == 0 {
return BytesMut::new();
}
buffer.split_to(watermark)
})
}
/// Pops a buffer from the front of the receive queue as long as the `transform` function returns a
/// non-empty buffer.
#[inline]
fn pop_transform<F: Fn(&mut BytesMut) -> BytesMut>(
&mut self,
transform: F,
) -> Option<BytesMut> {
let slot = self.slots.front_mut()?;
if !slot.is_occupied(self.start_offset) {
return None;
}
let buffer = slot.data_mut();
let out = transform(buffer);
// filter out empty buffers
if out.is_empty() {
return None;
}
slot.add_start(out.len());
if slot.start() == slot.end_allocated() {
// remove empty buffers
self.slots.pop_front();
}
self.start_offset += out.len() as u64;
Some(out)
}
/// Returns the amount of data that had already been consumed from the
/// receive buffer.
#[inline]
pub fn consumed_len(&self) -> u64 {
self.start_offset
}
/// Returns the total amount of contiguous received data.
///
/// This includes the already consumed data as well as the data that is still
/// buffered and available for consumption.
#[inline]
pub fn total_received_len(&self) -> u64 {
self.consumed_len() + self.len() as u64
}
/// Resets the receive buffer.
///
/// This will drop all previously received data.
#[inline]
pub fn reset(&mut self) {
self.slots.clear();
self.start_offset = Default::default();
}
#[inline(always)]
fn insert(&mut self, idx: usize, slot: Slot) {
if self.slots.len() < idx {
debug_assert_eq!(self.slots.len() + 1, idx);
self.slots.push_back(slot);
} else {
self.slots.insert(idx, slot);
}
}
#[inline]
fn allocate_request(&mut self, mut idx: usize, mut request: Request) {
while !request.is_empty() {
let size = Self::allocation_size(request.start());
let offset = Self::align_offset(request.start(), size);
let buffer = BytesMut::with_capacity(size);
let end = offset + size as u64;
let mut slot = Slot::new(offset, end, buffer);
let slot::Outcome { lower, mid, upper } = slot.try_write(request);
debug_assert!(lower.is_empty(), "lower requests should always be empty");
// first insert the newly-created Slot
self.insert(idx, slot);
idx += 1;
// check if we have a mid-slot and insert that as well
if let Some(mid) = mid {
self.insert(idx, mid);
idx += 1;
}
// set the current request to the upper slot and loop
request = upper;
}
}
/// Aligns an offset to a certain alignment size
#[inline]
fn align_offset(offset: u64, alignment: usize) -> u64 {
(offset / (alignment as u64)) * (alignment as u64)
}
/// Returns the desired allocation size for the given offset
///
/// The allocation size gradually increases as the offset increases. This is under
/// the assumption that streams that receive a lot of data will continue to receive
/// a lot of data.
///
/// The current table is as follows:
///
/// | offset | allocation size |
/// |----------------|-----------------|
/// | 0 | 4096 |
/// | 65536 | 16384 |
/// | 262144 | 32768 |
/// | >=1048575 | 65536 |
#[inline]
fn allocation_size(offset: u64) -> usize {
for pow in (2..=4).rev() {
let mult = 1 << pow;
let square = mult * mult;
let min_offset = (MIN_BUFFER_ALLOCATION_SIZE * square) as u64;
let allocation_size = MIN_BUFFER_ALLOCATION_SIZE * mult;
if offset >= min_offset {
return allocation_size;
}
}
MIN_BUFFER_ALLOCATION_SIZE
}
#[inline]
fn check_consistency(&self) {
if cfg!(debug_assertions) {
let mut prev_end = self.start_offset;
for slot in &self.slots {
assert!(slot.start() >= prev_end, "{self:#?}");
prev_end = slot.end_allocated();
}
}
}
}
pub struct Iter<'a> {
prev_end: u64,
inner: vec_deque::Iter<'a, Slot>,
}
impl<'a> Iter<'a> {
#[inline]
fn new(buffer: &'a ReceiveBuffer) -> Self {
Self {
prev_end: buffer.start_offset,
inner: buffer.slots.iter(),
}
}
}
impl<'a> Iterator for Iter<'a> {
type Item = &'a [u8];
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let slot = self.inner.next()?;
if !slot.is_occupied(self.prev_end) {
return None;
}
self.prev_end = slot.end();
Some(slot.as_slice())
}
}