// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 use core::mem::size_of; use s2n_quic_core::{ inet::{ethernet, ipv4, udp}, path::{MaxMtu, MaxMtuError}, }; use s2n_quic_xdp::umem::DEFAULT_FRAME_SIZE; use tokio::runtime::Handle; /// Calculate how much a packet will need for fixed-size headers const MIN_FRAME_OVERHEAD: u16 = (size_of::() + size_of::() + size_of::()) as _; #[derive(Debug)] #[must_use = "Builders do nothing without calling `build`"] pub struct Builder { rx: Rx, tx: Tx, max_mtu: MaxMtu, handle: Option, } impl Default for Builder<(), ()> { fn default() -> Self { Self { rx: (), tx: (), max_mtu: MaxMtu::try_from(DEFAULT_FRAME_SIZE as u16 - MIN_FRAME_OVERHEAD).unwrap(), handle: None, } } } impl Builder { /// Sets the tokio runtime handle for the provider pub fn with_handle(mut self, handle: Handle) -> Self { self.handle = Some(handle); self } /// Sets the UMEM frame size for the provider pub fn with_frame_size(mut self, frame_size: u16) -> Result { self.max_mtu = frame_size.saturating_sub(MIN_FRAME_OVERHEAD).try_into()?; Ok(self) } /// Sets the RX implementation for the provider pub fn with_rx(self, rx: NewRx) -> Builder where NewRx: super::rx::Rx, { let Self { tx, handle, max_mtu, .. } = self; Builder { rx, tx, handle, max_mtu, } } /// Sets the TX implementation for the provider pub fn with_tx(self, tx: NewTx) -> Builder where NewTx: super::tx::Tx, { let Self { rx, handle, max_mtu, .. } = self; Builder { rx, tx, handle, max_mtu, } } } impl Builder where Rx: 'static + super::rx::Rx + Send, Tx: 'static + super::tx::Tx + Send, { pub fn build(self) -> super::Provider { let Self { rx, tx, handle, max_mtu, } = self; super::Provider { rx, tx, handle, max_mtu, } } }