use std::{fmt, sync::Arc};
#[cfg(feature = "qlog")]
use std::{io, sync::Mutex, time::Instant};
#[cfg(feature = "qlog")]
use qlog::streamer::QlogStreamer;
#[cfg(feature = "qlog")]
use crate::QlogStream;
use crate::{
Duration, INITIAL_MTU, MAX_UDP_PAYLOAD, VarInt, VarIntBoundsExceeded, congestion,
connection::qlog::QlogSink, transport_parameters::TransportParameterConfig,
};
pub struct TransportConfig {
pub(crate) max_concurrent_bidi_streams: VarInt,
pub(crate) max_concurrent_uni_streams: VarInt,
pub(crate) max_idle_timeout: Option<VarInt>,
pub(crate) stream_receive_window: VarInt,
pub(crate) receive_window: VarInt,
pub(crate) send_window: u64,
pub(crate) send_fairness: bool,
pub(crate) packet_threshold: u32,
pub(crate) time_threshold: f32,
pub(crate) initial_rtt: Duration,
pub(crate) initial_mtu: u16,
pub(crate) min_mtu: u16,
pub(crate) mtu_discovery_config: Option<MtuDiscoveryConfig>,
pub(crate) pad_to_mtu: bool,
pub(crate) ack_frequency_config: Option<AckFrequencyConfig>,
pub(crate) max_outgoing_bytes_per_second: Option<u64>,
pub(crate) persistent_congestion_threshold: u32,
pub(crate) keep_alive_interval: Option<Duration>,
pub(crate) crypto_buffer_size: usize,
pub(crate) allow_spin: bool,
pub(crate) datagram_receive_buffer_size: Option<usize>,
pub(crate) datagram_send_buffer_size: usize,
#[cfg(test)]
pub(crate) deterministic_packet_numbers: bool,
pub(crate) congestion_controller_factory: Arc<dyn congestion::ControllerFactory + Send + Sync>,
pub(crate) enable_segmentation_offload: bool,
pub(crate) qlog_sink: QlogSink,
pub(crate) transport_parameter_config: Option<TransportParameterConfig>,
}
impl TransportConfig {
pub fn max_concurrent_bidi_streams(&mut self, value: VarInt) -> &mut Self {
self.max_concurrent_bidi_streams = value;
self
}
pub fn max_concurrent_uni_streams(&mut self, value: VarInt) -> &mut Self {
self.max_concurrent_uni_streams = value;
self
}
pub fn max_idle_timeout(&mut self, value: Option<IdleTimeout>) -> &mut Self {
self.max_idle_timeout = value.map(|t| t.0);
self
}
pub fn stream_receive_window(&mut self, value: VarInt) -> &mut Self {
self.stream_receive_window = value;
self
}
pub fn receive_window(&mut self, value: VarInt) -> &mut Self {
self.receive_window = value;
self
}
pub fn send_window(&mut self, value: u64) -> &mut Self {
self.send_window = value;
self
}
pub fn send_fairness(&mut self, value: bool) -> &mut Self {
self.send_fairness = value;
self
}
pub fn packet_threshold(&mut self, value: u32) -> &mut Self {
self.packet_threshold = value;
self
}
pub fn time_threshold(&mut self, value: f32) -> &mut Self {
self.time_threshold = value;
self
}
pub fn initial_rtt(&mut self, value: Duration) -> &mut Self {
self.initial_rtt = value;
self
}
pub fn initial_mtu(&mut self, value: u16) -> &mut Self {
self.initial_mtu = value.max(INITIAL_MTU);
self
}
pub(crate) fn get_initial_mtu(&self) -> u16 {
self.initial_mtu.max(self.min_mtu)
}
pub fn min_mtu(&mut self, value: u16) -> &mut Self {
self.min_mtu = value.max(INITIAL_MTU);
self
}
pub fn mtu_discovery_config(&mut self, value: Option<MtuDiscoveryConfig>) -> &mut Self {
self.mtu_discovery_config = value;
self
}
pub fn pad_to_mtu(&mut self, value: bool) -> &mut Self {
self.pad_to_mtu = value;
self
}
pub fn ack_frequency_config(&mut self, value: Option<AckFrequencyConfig>) -> &mut Self {
self.ack_frequency_config = value;
self
}
pub fn max_outgoing_bytes_per_second(&mut self, value: Option<u64>) -> &mut Self {
self.max_outgoing_bytes_per_second = value;
self
}
pub fn persistent_congestion_threshold(&mut self, value: u32) -> &mut Self {
self.persistent_congestion_threshold = value;
self
}
pub fn keep_alive_interval(&mut self, value: Option<Duration>) -> &mut Self {
self.keep_alive_interval = value;
self
}
pub fn crypto_buffer_size(&mut self, value: usize) -> &mut Self {
self.crypto_buffer_size = value;
self
}
pub fn allow_spin(&mut self, value: bool) -> &mut Self {
self.allow_spin = value;
self
}
pub fn datagram_receive_buffer_size(&mut self, value: Option<usize>) -> &mut Self {
self.datagram_receive_buffer_size = value;
self
}
pub fn datagram_send_buffer_size(&mut self, value: usize) -> &mut Self {
self.datagram_send_buffer_size = value;
self
}
#[cfg(test)]
pub(crate) fn deterministic_packet_numbers(&mut self, enabled: bool) -> &mut Self {
self.deterministic_packet_numbers = enabled;
self
}
pub fn congestion_controller_factory(
&mut self,
factory: Arc<dyn congestion::ControllerFactory + Send + Sync + 'static>,
) -> &mut Self {
self.congestion_controller_factory = factory;
self
}
pub fn enable_segmentation_offload(&mut self, enabled: bool) -> &mut Self {
self.enable_segmentation_offload = enabled;
self
}
#[cfg(feature = "qlog")]
pub fn qlog_stream(&mut self, stream: Option<QlogStream>) -> &mut Self {
self.qlog_sink = stream.into();
self
}
pub fn transport_parameter_config(&mut self, config: TransportParameterConfig) -> &mut Self {
self.transport_parameter_config = Some(config);
self
}
}
impl Default for TransportConfig {
fn default() -> Self {
const EXPECTED_RTT: u32 = 100; const MAX_STREAM_BANDWIDTH: u32 = 12500 * 1000; const STREAM_RWND: u32 = MAX_STREAM_BANDWIDTH / 1000 * EXPECTED_RTT;
Self {
max_concurrent_bidi_streams: 100u32.into(),
max_concurrent_uni_streams: 100u32.into(),
max_idle_timeout: Some(VarInt(30_000)),
stream_receive_window: STREAM_RWND.into(),
receive_window: VarInt::MAX,
send_window: (8 * STREAM_RWND).into(),
send_fairness: true,
packet_threshold: 3,
time_threshold: 9.0 / 8.0,
initial_rtt: Duration::from_millis(333), initial_mtu: INITIAL_MTU,
min_mtu: INITIAL_MTU,
mtu_discovery_config: Some(MtuDiscoveryConfig::default()),
pad_to_mtu: false,
ack_frequency_config: None,
max_outgoing_bytes_per_second: None,
persistent_congestion_threshold: 3,
keep_alive_interval: None,
crypto_buffer_size: 16 * 1024,
allow_spin: true,
datagram_receive_buffer_size: Some(STREAM_RWND as usize),
datagram_send_buffer_size: 1024 * 1024,
#[cfg(test)]
deterministic_packet_numbers: false,
congestion_controller_factory: Arc::new(congestion::CubicConfig::default()),
enable_segmentation_offload: true,
qlog_sink: QlogSink::default(),
transport_parameter_config: None,
}
}
}
impl fmt::Debug for TransportConfig {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let Self {
max_concurrent_bidi_streams,
max_concurrent_uni_streams,
max_idle_timeout,
stream_receive_window,
receive_window,
send_window,
send_fairness,
packet_threshold,
time_threshold,
initial_rtt,
initial_mtu,
min_mtu,
mtu_discovery_config,
pad_to_mtu,
ack_frequency_config,
max_outgoing_bytes_per_second,
persistent_congestion_threshold,
keep_alive_interval,
crypto_buffer_size,
allow_spin,
datagram_receive_buffer_size,
datagram_send_buffer_size,
#[cfg(test)]
deterministic_packet_numbers: _,
congestion_controller_factory: _,
enable_segmentation_offload,
qlog_sink,
transport_parameter_config: _,
} = self;
let mut s = fmt.debug_struct("TransportConfig");
s.field("max_concurrent_bidi_streams", max_concurrent_bidi_streams)
.field("max_concurrent_uni_streams", max_concurrent_uni_streams)
.field("max_idle_timeout", max_idle_timeout)
.field("stream_receive_window", stream_receive_window)
.field("receive_window", receive_window)
.field("send_window", send_window)
.field("send_fairness", send_fairness)
.field("packet_threshold", packet_threshold)
.field("time_threshold", time_threshold)
.field("initial_rtt", initial_rtt)
.field("initial_mtu", initial_mtu)
.field("min_mtu", min_mtu)
.field("mtu_discovery_config", mtu_discovery_config)
.field("pad_to_mtu", pad_to_mtu)
.field("ack_frequency_config", ack_frequency_config)
.field(
"max_outgoing_bytes_per_second",
max_outgoing_bytes_per_second,
)
.field(
"persistent_congestion_threshold",
persistent_congestion_threshold,
)
.field("keep_alive_interval", keep_alive_interval)
.field("crypto_buffer_size", crypto_buffer_size)
.field("allow_spin", allow_spin)
.field("datagram_receive_buffer_size", datagram_receive_buffer_size)
.field("datagram_send_buffer_size", datagram_send_buffer_size)
.field("enable_segmentation_offload", enable_segmentation_offload);
if cfg!(feature = "qlog") {
s.field("qlog_stream", &qlog_sink.is_enabled());
}
s.finish_non_exhaustive()
}
}
#[derive(Clone, Debug)]
pub struct AckFrequencyConfig {
pub(crate) ack_eliciting_threshold: VarInt,
pub(crate) max_ack_delay: Option<Duration>,
pub(crate) reordering_threshold: VarInt,
}
impl AckFrequencyConfig {
pub fn ack_eliciting_threshold(&mut self, value: VarInt) -> &mut Self {
self.ack_eliciting_threshold = value;
self
}
pub fn max_ack_delay(&mut self, value: Option<Duration>) -> &mut Self {
self.max_ack_delay = value;
self
}
pub fn reordering_threshold(&mut self, value: VarInt) -> &mut Self {
self.reordering_threshold = value;
self
}
}
impl Default for AckFrequencyConfig {
fn default() -> Self {
Self {
ack_eliciting_threshold: VarInt(1),
max_ack_delay: None,
reordering_threshold: VarInt(2),
}
}
}
#[cfg(feature = "qlog")]
pub struct QlogConfig {
writer: Option<Box<dyn io::Write + Send + Sync>>,
title: Option<String>,
description: Option<String>,
start_time: Instant,
}
#[cfg(feature = "qlog")]
impl QlogConfig {
pub fn writer(&mut self, writer: Box<dyn io::Write + Send + Sync>) -> &mut Self {
self.writer = Some(writer);
self
}
pub fn title(&mut self, title: Option<String>) -> &mut Self {
self.title = title;
self
}
pub fn description(&mut self, description: Option<String>) -> &mut Self {
self.description = description;
self
}
pub fn start_time(&mut self, start_time: Instant) -> &mut Self {
self.start_time = start_time;
self
}
pub fn into_stream(self) -> Option<QlogStream> {
use tracing::warn;
let writer = self.writer?;
let trace = qlog::TraceSeq::new(
self.title.clone(),
self.description.clone(),
None,
Some(qlog::VantagePoint {
name: None,
ty: qlog::VantagePointType::Unknown,
flow: None,
}),
vec![],
);
let mut streamer = QlogStreamer::new(
self.title,
self.description,
self.start_time,
trace,
qlog::events::EventImportance::Core,
qlog::streamer::EventTimePrecision::NanoSeconds,
writer,
);
match streamer.start_log() {
Ok(()) => Some(QlogStream(Arc::new(Mutex::new(streamer)))),
Err(e) => {
warn!("could not initialize endpoint qlog streamer: {e}");
None
}
}
}
}
#[cfg(feature = "qlog")]
impl Default for QlogConfig {
fn default() -> Self {
Self {
writer: None,
title: None,
description: None,
start_time: Instant::now(),
}
}
}
#[derive(Clone, Debug)]
pub struct MtuDiscoveryConfig {
pub(crate) interval: Duration,
pub(crate) upper_bound: u16,
pub(crate) minimum_change: u16,
pub(crate) black_hole_cooldown: Duration,
}
impl MtuDiscoveryConfig {
pub fn interval(&mut self, value: Duration) -> &mut Self {
self.interval = value;
self
}
pub fn upper_bound(&mut self, value: u16) -> &mut Self {
self.upper_bound = value.min(MAX_UDP_PAYLOAD);
self
}
pub fn black_hole_cooldown(&mut self, value: Duration) -> &mut Self {
self.black_hole_cooldown = value;
self
}
pub fn minimum_change(&mut self, value: u16) -> &mut Self {
self.minimum_change = value;
self
}
}
impl Default for MtuDiscoveryConfig {
fn default() -> Self {
Self {
interval: Duration::from_secs(600),
upper_bound: 1452,
black_hole_cooldown: Duration::from_secs(60),
minimum_change: 20,
}
}
}
#[derive(Default, Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct IdleTimeout(VarInt);
impl From<VarInt> for IdleTimeout {
fn from(inner: VarInt) -> Self {
Self(inner)
}
}
impl TryFrom<Duration> for IdleTimeout {
type Error = VarIntBoundsExceeded;
fn try_from(timeout: Duration) -> Result<Self, Self::Error> {
let inner = VarInt::try_from(timeout.as_millis())?;
Ok(Self(inner))
}
}
impl fmt::Debug for IdleTimeout {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}