noq_proto/config/transport.rs
1#[cfg(feature = "qlog")]
2use std::path::Path;
3use std::{
4 fmt,
5 net::SocketAddr,
6 num::{NonZeroU8, NonZeroU32},
7 sync::Arc,
8};
9
10use crate::{
11 ConnectionId, Duration, INITIAL_MTU, Instant, MAX_UDP_PAYLOAD, Side, VarInt,
12 VarIntBoundsExceeded, address_discovery, congestion, connection::qlog::QlogSink,
13};
14#[cfg(feature = "qlog")]
15use crate::{QlogFactory, QlogFileFactory};
16
17/// Parameters governing the core QUIC state machine
18///
19/// Default values should be suitable for most internet applications. Applications protocols which
20/// forbid remotely-initiated streams should set `max_concurrent_bidi_streams` and
21/// `max_concurrent_uni_streams` to zero.
22///
23/// In some cases, performance or resource requirements can be improved by tuning these values to
24/// suit a particular application and/or network connection. In particular, data window sizes can be
25/// tuned for a particular expected round trip time, link capacity, and memory availability. Tuning
26/// for higher bandwidths and latencies increases worst-case memory consumption, but does not impair
27/// performance at lower bandwidths and latencies. The default configuration is tuned for a 100Mbps
28/// link with a 100ms round trip time.
29#[derive(Clone)]
30pub struct TransportConfig {
31 pub(crate) max_concurrent_bidi_streams: VarInt,
32 pub(crate) max_concurrent_uni_streams: VarInt,
33 pub(crate) max_idle_timeout: Option<VarInt>,
34 pub(crate) stream_receive_window: VarInt,
35 pub(crate) receive_window: VarInt,
36 pub(crate) send_window: u64,
37 pub(crate) send_fairness: bool,
38
39 pub(crate) packet_threshold: u32,
40 pub(crate) time_threshold: f32,
41 pub(crate) initial_rtt: Duration,
42 pub(crate) initial_mtu: u16,
43 pub(crate) min_mtu: u16,
44 pub(crate) mtu_discovery_config: Option<MtuDiscoveryConfig>,
45 pub(crate) pad_to_mtu: bool,
46 pub(crate) ack_frequency_config: Option<AckFrequencyConfig>,
47 pub(crate) max_outgoing_bytes_per_second: Option<u64>,
48
49 pub(crate) persistent_congestion_threshold: u32,
50 pub(crate) keep_alive_interval: Option<Duration>,
51 pub(crate) crypto_buffer_size: usize,
52 pub(crate) allow_spin: bool,
53 pub(crate) datagram_receive_buffer_size: Option<usize>,
54 pub(crate) datagram_send_buffer_size: usize,
55 #[cfg(test)]
56 pub(crate) deterministic_packet_numbers: bool,
57
58 pub(crate) congestion_controller_factory: Arc<dyn congestion::ControllerFactory + Send + Sync>,
59
60 pub(crate) enable_segmentation_offload: bool,
61
62 pub(crate) address_discovery_role: address_discovery::Role,
63
64 pub(crate) max_concurrent_multipath_paths: Option<NonZeroU32>,
65
66 pub(crate) default_path_max_idle_timeout: Option<Duration>,
67 pub(crate) default_path_keep_alive_interval: Option<Duration>,
68
69 pub(crate) max_remote_nat_traversal_addresses: Option<NonZeroU8>,
70 pub(crate) server_handshake_migration: bool,
71
72 #[cfg(feature = "qlog")]
73 pub(crate) qlog_factory: Option<Arc<dyn QlogFactory>>,
74}
75
76impl TransportConfig {
77 /// Maximum number of incoming bidirectional streams that may be open concurrently
78 ///
79 /// Must be nonzero for the peer to open any bidirectional streams.
80 ///
81 /// Worst-case memory use is directly proportional to `max_concurrent_bidi_streams *
82 /// stream_receive_window`, with an upper bound proportional to `receive_window`.
83 pub fn max_concurrent_bidi_streams(&mut self, value: VarInt) -> &mut Self {
84 self.max_concurrent_bidi_streams = value;
85 self
86 }
87
88 /// Variant of `max_concurrent_bidi_streams` affecting unidirectional streams
89 pub fn max_concurrent_uni_streams(&mut self, value: VarInt) -> &mut Self {
90 self.max_concurrent_uni_streams = value;
91 self
92 }
93
94 /// Maximum duration of inactivity to accept before timing out the connection.
95 ///
96 /// The true idle timeout is the minimum of this and the peer's own max idle timeout. `None`
97 /// represents an infinite timeout. Defaults to 30 seconds.
98 ///
99 /// **WARNING**: If a peer or its network path malfunctions or acts maliciously, an infinite
100 /// idle timeout can result in permanently hung futures!
101 ///
102 /// ```
103 /// # use std::{convert::TryInto, time::Duration};
104 /// # use noq_proto::{TransportConfig, VarInt, VarIntBoundsExceeded};
105 /// # fn main() -> Result<(), VarIntBoundsExceeded> {
106 /// let mut config = TransportConfig::default();
107 ///
108 /// // Set the idle timeout as `VarInt`-encoded milliseconds
109 /// config.max_idle_timeout(Some(VarInt::from_u32(10_000).into()));
110 ///
111 /// // Set the idle timeout as a `Duration`
112 /// config.max_idle_timeout(Some(Duration::from_secs(10).try_into()?));
113 /// # Ok(())
114 /// # }
115 /// ```
116 pub fn max_idle_timeout(&mut self, value: Option<IdleTimeout>) -> &mut Self {
117 self.max_idle_timeout = value.map(|t| t.0);
118 self
119 }
120
121 /// Maximum number of bytes the peer may transmit without acknowledgement on any one stream
122 /// before becoming blocked.
123 ///
124 /// This should be set to at least the expected connection latency multiplied by the maximum
125 /// desired throughput. Setting this smaller than `receive_window` helps ensure that a single
126 /// stream doesn't monopolize receive buffers, which may otherwise occur if the application
127 /// chooses not to read from a large stream for a time while still requiring data on other
128 /// streams.
129 pub fn stream_receive_window(&mut self, value: VarInt) -> &mut Self {
130 self.stream_receive_window = value;
131 self
132 }
133
134 /// Maximum number of bytes the peer may transmit across all streams of a connection before
135 /// becoming blocked.
136 ///
137 /// This should be set to at least the expected connection latency multiplied by the maximum
138 /// desired throughput. Larger values can be useful to allow maximum throughput within a
139 /// stream while another is blocked.
140 pub fn receive_window(&mut self, value: VarInt) -> &mut Self {
141 self.receive_window = value;
142 self
143 }
144
145 /// Maximum number of bytes to transmit to a peer without acknowledgment
146 ///
147 /// Provides an upper bound on memory when communicating with peers that issue large amounts of
148 /// flow control credit. Endpoints that wish to handle large numbers of connections robustly
149 /// should take care to set this low enough to guarantee memory exhaustion does not occur if
150 /// every connection uses the entire window.
151 pub fn send_window(&mut self, value: u64) -> &mut Self {
152 self.send_window = value;
153 self
154 }
155
156 /// Whether to implement fair queuing for send streams having the same priority.
157 ///
158 /// When enabled, connections schedule data from outgoing streams having the same priority in a
159 /// round-robin fashion. When disabled, streams are scheduled in the order they are written to.
160 ///
161 /// Note that this only affects streams with the same priority. Higher priority streams always
162 /// take precedence over lower priority streams.
163 ///
164 /// Disabling fairness can reduce fragmentation and protocol overhead for workloads that use
165 /// many small streams.
166 pub fn send_fairness(&mut self, value: bool) -> &mut Self {
167 self.send_fairness = value;
168 self
169 }
170
171 /// Maximum reordering in packet number space before FACK style loss detection considers a
172 /// packet lost. Should not be less than 3, per RFC5681.
173 pub fn packet_threshold(&mut self, value: u32) -> &mut Self {
174 self.packet_threshold = value;
175 self
176 }
177
178 /// Maximum reordering in time space before time based loss detection considers a packet lost,
179 /// as a factor of RTT
180 pub fn time_threshold(&mut self, value: f32) -> &mut Self {
181 self.time_threshold = value;
182 self
183 }
184
185 /// The RTT used before an RTT sample is taken
186 pub fn initial_rtt(&mut self, value: Duration) -> &mut Self {
187 self.initial_rtt = value;
188 self
189 }
190
191 /// The initial value to be used as the maximum UDP payload size before running MTU discovery
192 /// (see [`TransportConfig::mtu_discovery_config`]).
193 ///
194 /// Must be at least 1200, which is the default, and known to be safe for typical internet
195 /// applications. Larger values are more efficient, but increase the risk of packet loss due to
196 /// exceeding the network path's IP MTU. If the provided value is higher than what the network
197 /// path actually supports, packet loss will eventually trigger black hole detection and bring
198 /// it down to [`TransportConfig::min_mtu`].
199 pub fn initial_mtu(&mut self, value: u16) -> &mut Self {
200 self.initial_mtu = value.max(INITIAL_MTU);
201 self
202 }
203
204 pub(crate) fn get_initial_mtu(&self) -> u16 {
205 self.initial_mtu.max(self.min_mtu)
206 }
207
208 /// The maximum UDP payload size guaranteed to be supported by the network.
209 ///
210 /// Must be at least 1200, which is the default, and lower than or equal to
211 /// [`TransportConfig::initial_mtu`].
212 ///
213 /// Real-world MTUs can vary according to ISP, VPN, and properties of intermediate network links
214 /// outside of either endpoint's control. Extreme care should be used when raising this value
215 /// outside of private networks where these factors are fully controlled. If the provided value
216 /// is higher than what the network path actually supports, the result will be unpredictable and
217 /// catastrophic packet loss, without a possibility of repair. Prefer
218 /// [`TransportConfig::initial_mtu`] together with
219 /// [`TransportConfig::mtu_discovery_config`] to set a maximum UDP payload size that robustly
220 /// adapts to the network.
221 pub fn min_mtu(&mut self, value: u16) -> &mut Self {
222 self.min_mtu = value.max(INITIAL_MTU);
223 self
224 }
225
226 /// Specifies the MTU discovery config (see [`MtuDiscoveryConfig`] for details).
227 ///
228 /// Enabled by default.
229 pub fn mtu_discovery_config(&mut self, value: Option<MtuDiscoveryConfig>) -> &mut Self {
230 self.mtu_discovery_config = value;
231 self
232 }
233
234 /// Pad UDP datagrams carrying application data to current maximum UDP payload size
235 ///
236 /// Disabled by default. UDP datagrams containing loss probes are exempt from padding.
237 ///
238 /// Enabling this helps mitigate traffic analysis by network observers, but it increases
239 /// bandwidth usage. Without this mitigation precise plain text size of application datagrams as
240 /// well as the total size of stream write bursts can be inferred by observers under certain
241 /// conditions. This analysis requires either an uncongested connection or application datagrams
242 /// too large to be coalesced.
243 pub fn pad_to_mtu(&mut self, value: bool) -> &mut Self {
244 self.pad_to_mtu = value;
245 self
246 }
247
248 /// Specifies the ACK frequency config (see [`AckFrequencyConfig`] for details)
249 ///
250 /// The provided configuration will be ignored if the peer does not support the acknowledgement
251 /// frequency QUIC extension.
252 ///
253 /// Defaults to `None`, which disables controlling the peer's acknowledgement frequency. Even
254 /// if set to `None`, the local side still supports the acknowledgement frequency QUIC
255 /// extension and may use it in other ways.
256 pub fn ack_frequency_config(&mut self, value: Option<AckFrequencyConfig>) -> &mut Self {
257 self.ack_frequency_config = value;
258 self
259 }
260
261 /// Configures an outbound rate limit (in bytes per second) for each connection.
262 ///
263 /// Defaults to `None`, which disables rate limiting.
264 pub fn max_outgoing_bytes_per_second(&mut self, value: Option<u64>) -> &mut Self {
265 self.max_outgoing_bytes_per_second = value;
266 self
267 }
268
269 /// Number of consecutive PTOs after which network is considered to be experiencing persistent congestion.
270 pub fn persistent_congestion_threshold(&mut self, value: u32) -> &mut Self {
271 self.persistent_congestion_threshold = value;
272 self
273 }
274
275 /// Period of inactivity before sending a keep-alive packet
276 ///
277 /// Keep-alive packets prevent an inactive but otherwise healthy connection from timing out.
278 ///
279 /// `None` to disable, which is the default. Only one side of any given connection needs keep-alive
280 /// enabled for the connection to be preserved. Must be set lower than the idle_timeout of both
281 /// peers to be effective.
282 pub fn keep_alive_interval(&mut self, value: Option<Duration>) -> &mut Self {
283 self.keep_alive_interval = value;
284 self
285 }
286
287 /// Maximum quantity of out-of-order crypto layer data to buffer
288 pub fn crypto_buffer_size(&mut self, value: usize) -> &mut Self {
289 self.crypto_buffer_size = value;
290 self
291 }
292
293 /// Whether the implementation is permitted to set the spin bit on this connection
294 ///
295 /// This allows passive observers to easily judge the round trip time of a connection, which can
296 /// be useful for network administration but sacrifices a small amount of privacy.
297 pub fn allow_spin(&mut self, value: bool) -> &mut Self {
298 self.allow_spin = value;
299 self
300 }
301
302 /// Maximum number of incoming application datagram bytes to buffer, or None to disable
303 /// incoming datagrams
304 ///
305 /// The peer is forbidden to send single datagrams larger than this size. If the aggregate size
306 /// of all datagrams that have been received from the peer but not consumed by the application
307 /// exceeds this value, old datagrams are dropped until it is no longer exceeded.
308 pub fn datagram_receive_buffer_size(&mut self, value: Option<usize>) -> &mut Self {
309 self.datagram_receive_buffer_size = value;
310 self
311 }
312
313 /// Maximum number of outgoing application datagram bytes to buffer
314 ///
315 /// While datagrams are sent ASAP, it is possible for an application to generate data faster
316 /// than the link, or even the underlying hardware, can transmit them. This limits the amount of
317 /// memory that may be consumed in that case. When the send buffer is full and a new datagram is
318 /// sent, older datagrams are dropped until sufficient space is available.
319 pub fn datagram_send_buffer_size(&mut self, value: usize) -> &mut Self {
320 self.datagram_send_buffer_size = value;
321 self
322 }
323
324 /// Whether to force every packet number to be used
325 ///
326 /// By default, packet numbers are occasionally skipped to ensure peers aren't ACKing packets
327 /// before they see them.
328 #[cfg(test)]
329 pub(crate) fn deterministic_packet_numbers(&mut self, enabled: bool) -> &mut Self {
330 self.deterministic_packet_numbers = enabled;
331 self
332 }
333
334 /// How to construct new `congestion::Controller`s
335 ///
336 /// Typically the refcounted configuration of a `congestion::Controller`,
337 /// e.g. a `congestion::NewRenoConfig`.
338 ///
339 /// # Example
340 /// ```
341 /// # use noq_proto::*; use std::sync::Arc;
342 /// let mut config = TransportConfig::default();
343 /// config.congestion_controller_factory(Arc::new(congestion::NewRenoConfig::default()));
344 /// ```
345 pub fn congestion_controller_factory(
346 &mut self,
347 factory: Arc<dyn congestion::ControllerFactory + Send + Sync + 'static>,
348 ) -> &mut Self {
349 self.congestion_controller_factory = factory;
350 self
351 }
352
353 /// Whether to use "Generic Segmentation Offload" to accelerate transmits, when supported by the
354 /// environment
355 ///
356 /// Defaults to `true`.
357 ///
358 /// GSO dramatically reduces CPU consumption when sending large numbers of packets with the same
359 /// headers, such as when transmitting bulk data on a connection. However, it is not supported
360 /// by all network interface drivers or packet inspection tools. `noq-udp` will attempt to
361 /// disable GSO automatically when unavailable, but this can lead to spurious packet loss at
362 /// startup, temporarily degrading performance.
363 pub fn enable_segmentation_offload(&mut self, enabled: bool) -> &mut Self {
364 self.enable_segmentation_offload = enabled;
365 self
366 }
367
368 /// Whether to send observed address reports to peers.
369 ///
370 /// This will aid peers in inferring their reachable address, which in most NATd networks
371 /// will not be easily available to them.
372 pub fn send_observed_address_reports(&mut self, enabled: bool) -> &mut Self {
373 self.address_discovery_role.send = enabled;
374 self
375 }
376
377 /// Whether to receive observed address reports from other peers.
378 ///
379 /// Peers with the address discovery extension enabled that are willing to provide observed
380 /// address reports will do so if this transport parameter is set. In general, observed address
381 /// reports cannot be trusted. This, however, can aid the current endpoint in inferring its
382 /// reachable address, which in most NATd networks will not be easily available.
383 pub fn receive_observed_address_reports(&mut self, enabled: bool) -> &mut Self {
384 self.address_discovery_role.receive = enabled;
385 self
386 }
387
388 /// Enables the Multipath Extension for QUIC.
389 ///
390 /// Setting this to any nonzero value will enable the Multipath Extension for QUIC,
391 /// <https://datatracker.ietf.org/doc/draft-ietf-quic-multipath/>.
392 ///
393 /// The value provided specifies the number maximum number of paths this endpoint may open
394 /// concurrently when multipath is negotiated. For any path to be opened, the remote must
395 /// enable multipath as well.
396 pub fn max_concurrent_multipath_paths(&mut self, max_concurrent: u32) -> &mut Self {
397 self.max_concurrent_multipath_paths = NonZeroU32::new(max_concurrent);
398 self
399 }
400
401 /// Sets a default per-path maximum idle timeout
402 ///
403 /// If the path is idle for this long the path will be abandoned. Bear in mind this will
404 /// interact with the [`TransportConfig::max_idle_timeout`], if the last path is
405 /// abandoned the entire connection will be closed.
406 ///
407 /// You can also change this using [`Connection::set_path_max_idle_timeout`] for
408 /// existing paths.
409 ///
410 /// [`Connection::set_path_max_idle_timeout`]: crate::Connection::set_path_max_idle_timeout
411 pub fn default_path_max_idle_timeout(&mut self, timeout: Option<Duration>) -> &mut Self {
412 self.default_path_max_idle_timeout = timeout;
413 self
414 }
415
416 /// Sets a default per-path keep alive interval
417 ///
418 /// Note that this does not interact with the connection-wide
419 /// [`TransportConfig::keep_alive_interval`]. This setting will keep this path active,
420 /// [`TransportConfig::keep_alive_interval`] will keep the connection active, with no
421 /// control over which path is used for this.
422 ///
423 /// You can also change this using [`Connection::set_path_keep_alive_interval`] for
424 /// existing path.
425 ///
426 /// [`Connection::set_path_keep_alive_interval`]: crate::Connection::set_path_keep_alive_interval
427 pub fn default_path_keep_alive_interval(&mut self, interval: Option<Duration>) -> &mut Self {
428 self.default_path_keep_alive_interval = interval;
429 self
430 }
431
432 /// Get the initial max [`crate::PathId`] this endpoint allows.
433 ///
434 /// Returns `None` if multipath is disabled.
435 pub(crate) fn get_initial_max_path_id(&self) -> Option<crate::PathId> {
436 self.max_concurrent_multipath_paths
437 // a max_concurrent_multipath_paths value of 1 only allows the first path, which
438 // has id 0
439 .map(|nonzero_concurrent| nonzero_concurrent.get() - 1)
440 .map(Into::into)
441 }
442
443 /// Sets the maximum number of nat traversal addresses this endpoint allows the remote to
444 /// advertise
445 ///
446 /// Setting this to any nonzero value will enable n0's nat traversal protocol, loosely based in
447 /// the Nat Traversal Extension for QUIC, see
448 /// <https://www.ietf.org/archive/id/draft-seemann-quic-nat-traversal-02.html>
449 ///
450 /// This implementation expects the multipath extension to be enabled as well. if not yet
451 /// enabled via [`Self::max_concurrent_multipath_paths`], then that setting is set to 8.
452 pub fn max_remote_nat_traversal_addresses(&mut self, max_addresses: u8) -> &mut Self {
453 self.max_remote_nat_traversal_addresses = NonZeroU8::new(max_addresses);
454 if max_addresses != 0 && self.max_concurrent_multipath_paths.is_none() {
455 self.max_concurrent_multipath_paths(8);
456 }
457 self
458 }
459
460 /// Sets whether the server is allowed to migrate once during the handshake.
461 ///
462 /// **Enabling this is not RFC9000 compliant.**
463 ///
464 /// Defaults to `false`.
465 ///
466 /// Enabling this allows the server to migrate once during the handshake: it can send a
467 /// response from a different address than the client's initial packet was sent to. Once
468 /// an authenticated Handshake packet is received the server can no longer migrate
469 /// during the handshake (or after the handshake if not other extension enables this).
470 ///
471 /// This can be used to duplicate the client's initial packet to multiple addresses for
472 /// the server and accept the fastest response. The server will discard all but the
473 /// first such initial, considering any remaining as duplicates.
474 pub fn server_handshake_migration(&mut self, allow_migration: bool) -> &mut Self {
475 self.server_handshake_migration = allow_migration;
476 self
477 }
478
479 /// Configures qlog capturing by setting a [`QlogFactory`].
480 ///
481 /// This assigns a [`QlogFactory`] that produces qlog capture configurations for
482 /// individual connections.
483 #[cfg(feature = "qlog")]
484 pub fn qlog_factory(&mut self, factory: Arc<dyn QlogFactory>) -> &mut Self {
485 self.qlog_factory = Some(factory);
486 self
487 }
488
489 /// Configures qlog capturing through the `QLOGDIR` environment variable.
490 ///
491 /// This uses [`QlogFileFactory::from_env`] to create a factory to write qlog traces
492 /// into the directory set through the `QLOGDIR` environment variable.
493 ///
494 /// If `QLOGDIR` is not set, no traces will be written. If `QLOGDIR` is set to a path
495 /// that does not exist, it will be created.
496 ///
497 /// The files will be prefixed with `prefix`.
498 #[cfg(feature = "qlog")]
499 pub fn qlog_from_env(&mut self, prefix: &str) -> &mut Self {
500 self.qlog_factory(Arc::new(QlogFileFactory::from_env().with_prefix(prefix)))
501 }
502
503 /// Configures qlog capturing into a directory.
504 ///
505 /// This uses [`QlogFileFactory`] to create a factory to write qlog traces into
506 /// the specified directory. The files will be prefixed with `prefix`.
507 #[cfg(feature = "qlog")]
508 pub fn qlog_from_path(&mut self, path: impl AsRef<Path>, prefix: &str) -> &mut Self {
509 self.qlog_factory(Arc::new(
510 QlogFileFactory::new(path.as_ref().to_owned()).with_prefix(prefix),
511 ))
512 }
513
514 pub(crate) fn create_qlog_sink(
515 &self,
516 side: Side,
517 remote: SocketAddr,
518 initial_dst_cid: ConnectionId,
519 now: Instant,
520 ) -> QlogSink {
521 #[cfg(not(feature = "qlog"))]
522 let sink = {
523 let _ = (side, remote, initial_dst_cid, now);
524 QlogSink::default()
525 };
526
527 #[cfg(feature = "qlog")]
528 let sink = {
529 if let Some(config) = self
530 .qlog_factory
531 .as_ref()
532 .and_then(|factory| factory.for_connection(side, remote, initial_dst_cid, now))
533 {
534 QlogSink::new(config, initial_dst_cid, side, now)
535 } else {
536 QlogSink::default()
537 }
538 };
539
540 sink
541 }
542}
543
544impl Default for TransportConfig {
545 fn default() -> Self {
546 const EXPECTED_RTT: u32 = 100; // ms
547 const MAX_STREAM_BANDWIDTH: u32 = 12500 * 1000; // bytes/s
548 // Window size needed to avoid pipeline
549 // stalls
550 const STREAM_RWND: u32 = MAX_STREAM_BANDWIDTH / 1000 * EXPECTED_RTT;
551
552 Self {
553 max_concurrent_bidi_streams: 100u32.into(),
554 max_concurrent_uni_streams: 100u32.into(),
555 // 30 second default recommended by RFC 9308 ยง 3.2
556 max_idle_timeout: Some(VarInt(30_000)),
557 stream_receive_window: STREAM_RWND.into(),
558 receive_window: VarInt::MAX,
559 send_window: (8 * STREAM_RWND).into(),
560 send_fairness: true,
561
562 packet_threshold: 3,
563 time_threshold: 9.0 / 8.0,
564 initial_rtt: Duration::from_millis(333), // per spec, intentionally distinct from EXPECTED_RTT
565 initial_mtu: INITIAL_MTU,
566 min_mtu: INITIAL_MTU,
567 mtu_discovery_config: Some(MtuDiscoveryConfig::default()),
568 pad_to_mtu: false,
569 ack_frequency_config: None,
570 max_outgoing_bytes_per_second: None,
571
572 persistent_congestion_threshold: 3,
573 keep_alive_interval: None,
574 crypto_buffer_size: 16 * 1024,
575 allow_spin: true,
576 datagram_receive_buffer_size: Some(STREAM_RWND as usize),
577 datagram_send_buffer_size: 1024 * 1024,
578 #[cfg(test)]
579 deterministic_packet_numbers: false,
580
581 congestion_controller_factory: Arc::new(congestion::CubicConfig::default()),
582
583 enable_segmentation_offload: true,
584
585 address_discovery_role: address_discovery::Role::default(),
586
587 // disabled multipath by default
588 max_concurrent_multipath_paths: None,
589 default_path_max_idle_timeout: None,
590 default_path_keep_alive_interval: None,
591
592 // nat traversal disabled by default
593 max_remote_nat_traversal_addresses: None,
594 server_handshake_migration: false,
595
596 #[cfg(feature = "qlog")]
597 qlog_factory: None,
598 }
599 }
600}
601
602impl fmt::Debug for TransportConfig {
603 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
604 let Self {
605 max_concurrent_bidi_streams,
606 max_concurrent_uni_streams,
607 max_idle_timeout,
608 stream_receive_window,
609 receive_window,
610 send_window,
611 send_fairness,
612 packet_threshold,
613 time_threshold,
614 initial_rtt,
615 initial_mtu,
616 min_mtu,
617 mtu_discovery_config,
618 pad_to_mtu,
619 ack_frequency_config,
620 max_outgoing_bytes_per_second,
621 persistent_congestion_threshold,
622 keep_alive_interval,
623 crypto_buffer_size,
624 allow_spin,
625 datagram_receive_buffer_size,
626 datagram_send_buffer_size,
627 #[cfg(test)]
628 deterministic_packet_numbers: _,
629 congestion_controller_factory: _,
630 enable_segmentation_offload,
631 address_discovery_role,
632 max_concurrent_multipath_paths,
633 default_path_max_idle_timeout,
634 default_path_keep_alive_interval,
635 max_remote_nat_traversal_addresses,
636 server_handshake_migration,
637 #[cfg(feature = "qlog")]
638 qlog_factory,
639 } = self;
640 let mut s = fmt.debug_struct("TransportConfig");
641
642 s.field("max_concurrent_bidi_streams", max_concurrent_bidi_streams)
643 .field("max_concurrent_uni_streams", max_concurrent_uni_streams)
644 .field("max_idle_timeout", max_idle_timeout)
645 .field("stream_receive_window", stream_receive_window)
646 .field("receive_window", receive_window)
647 .field("send_window", send_window)
648 .field("send_fairness", send_fairness)
649 .field("packet_threshold", packet_threshold)
650 .field("time_threshold", time_threshold)
651 .field("initial_rtt", initial_rtt)
652 .field("initial_mtu", initial_mtu)
653 .field("min_mtu", min_mtu)
654 .field("mtu_discovery_config", mtu_discovery_config)
655 .field("pad_to_mtu", pad_to_mtu)
656 .field("ack_frequency_config", ack_frequency_config)
657 .field(
658 "max_outgoing_bytes_per_second",
659 max_outgoing_bytes_per_second,
660 )
661 .field(
662 "persistent_congestion_threshold",
663 persistent_congestion_threshold,
664 )
665 .field("keep_alive_interval", keep_alive_interval)
666 .field("crypto_buffer_size", crypto_buffer_size)
667 .field("allow_spin", allow_spin)
668 .field("datagram_receive_buffer_size", datagram_receive_buffer_size)
669 .field("datagram_send_buffer_size", datagram_send_buffer_size)
670 // congestion_controller_factory not debug
671 .field("enable_segmentation_offload", enable_segmentation_offload)
672 .field("address_discovery_role", address_discovery_role)
673 .field(
674 "max_concurrent_multipath_paths",
675 max_concurrent_multipath_paths,
676 )
677 .field(
678 "default_path_max_idle_timeout",
679 default_path_max_idle_timeout,
680 )
681 .field(
682 "default_path_keep_alive_interval",
683 default_path_keep_alive_interval,
684 )
685 .field(
686 "max_remote_nat_traversal_addresses",
687 max_remote_nat_traversal_addresses,
688 )
689 .field("server_handshake_migration", server_handshake_migration);
690 #[cfg(feature = "qlog")]
691 s.field("qlog_factory", &qlog_factory.is_some());
692
693 s.finish_non_exhaustive()
694 }
695}
696
697/// Parameters for controlling the peer's acknowledgement frequency
698///
699/// The parameters provided in this config will be sent to the peer at the beginning of the
700/// connection, so it can take them into account when sending acknowledgements (see each parameter's
701/// description for details on how it influences acknowledgement frequency).
702///
703/// noq's implementation follows the fourth draft of the
704/// [QUIC Acknowledgement Frequency extension](https://datatracker.ietf.org/doc/html/draft-ietf-quic-ack-frequency-04).
705/// The defaults produce behavior slightly different than the behavior without this extension,
706/// because they change the way reordered packets are handled (see
707/// [`AckFrequencyConfig::reordering_threshold`] for details).
708#[derive(Clone, Debug)]
709pub struct AckFrequencyConfig {
710 pub(crate) ack_eliciting_threshold: VarInt,
711 pub(crate) max_ack_delay: Option<Duration>,
712 pub(crate) reordering_threshold: VarInt,
713}
714
715impl AckFrequencyConfig {
716 /// The ack-eliciting threshold we will request the peer to use
717 ///
718 /// This threshold represents the number of ack-eliciting packets an endpoint may receive
719 /// without immediately sending an ACK.
720 ///
721 /// The remote peer should send at least one ACK frame when more than this number of
722 /// ack-eliciting packets have been received. A value of 0 results in a receiver immediately
723 /// acknowledging every ack-eliciting packet.
724 ///
725 /// Defaults to 1, which sends ACK frames for every other ack-eliciting packet.
726 pub fn ack_eliciting_threshold(&mut self, value: VarInt) -> &mut Self {
727 self.ack_eliciting_threshold = value;
728 self
729 }
730
731 /// The `max_ack_delay` we will request the peer to use
732 ///
733 /// This parameter represents the maximum amount of time that an endpoint waits before sending
734 /// an ACK when the ack-eliciting threshold hasn't been reached.
735 ///
736 /// The effective `max_ack_delay` will be clamped to be at least the peer's `min_ack_delay`
737 /// transport parameter, and at most the greater of the current path RTT or 25ms.
738 ///
739 /// Defaults to `None`, in which case the peer's original `max_ack_delay` will be used, as
740 /// obtained from its transport parameters.
741 pub fn max_ack_delay(&mut self, value: Option<Duration>) -> &mut Self {
742 self.max_ack_delay = value;
743 self
744 }
745
746 /// The reordering threshold we will request the peer to use
747 ///
748 /// This threshold represents the amount of out-of-order packets that will trigger an endpoint
749 /// to send an ACK, without waiting for `ack_eliciting_threshold` to be exceeded or for
750 /// `max_ack_delay` to be elapsed.
751 ///
752 /// A value of 0 indicates out-of-order packets do not elicit an immediate ACK. A value of 1
753 /// immediately acknowledges any packets that are received out of order (this is also the
754 /// behavior when the extension is disabled).
755 ///
756 /// It is recommended to set this value to [`TransportConfig::packet_threshold`] minus one.
757 /// Since the default value for [`TransportConfig::packet_threshold`] is 3, this value defaults
758 /// to 2.
759 pub fn reordering_threshold(&mut self, value: VarInt) -> &mut Self {
760 self.reordering_threshold = value;
761 self
762 }
763}
764
765impl Default for AckFrequencyConfig {
766 fn default() -> Self {
767 Self {
768 ack_eliciting_threshold: VarInt(1),
769 max_ack_delay: None,
770 reordering_threshold: VarInt(2),
771 }
772 }
773}
774
775/// Parameters governing MTU discovery.
776///
777/// # The why of MTU discovery
778///
779/// By design, QUIC ensures during the handshake that the network path between the client and the
780/// server is able to transmit unfragmented UDP packets with a body of 1200 bytes. In other words,
781/// once the connection is established, we know that the network path's maximum transmission unit
782/// (MTU) is of at least 1200 bytes (plus IP and UDP headers). Because of this, a QUIC endpoint can
783/// split outgoing data in packets of 1200 bytes, with confidence that the network will be able to
784/// deliver them (if the endpoint were to send bigger packets, they could prove too big and end up
785/// being dropped).
786///
787/// There is, however, a significant overhead associated to sending a packet. If the same
788/// information can be sent in fewer packets, that results in higher throughput. The amount of
789/// packets that need to be sent is inversely proportional to the MTU: the higher the MTU, the
790/// bigger the packets that can be sent, and the fewer packets that are needed to transmit a given
791/// amount of bytes.
792///
793/// Most networks have an MTU higher than 1200. Through MTU discovery, endpoints can detect the
794/// path's MTU and, if it turns out to be higher, start sending bigger packets.
795///
796/// # MTU discovery internals
797///
798/// noq implements MTU discovery through DPLPMTUD (Datagram Packetization Layer Path MTU
799/// Discovery), described in [section 14.3 of RFC
800/// 9000](https://www.rfc-editor.org/rfc/rfc9000.html#section-14.3). This method consists of sending
801/// QUIC packets padded to a particular size (called PMTU probes), and waiting to see if the remote
802/// peer responds with an ACK. If an ACK is received, that means the probe arrived at the remote
803/// peer, which in turn means that the network path's MTU is of at least the packet's size. If the
804/// probe is lost, it is sent another 2 times before concluding that the MTU is lower than the
805/// packet's size.
806///
807/// MTU discovery runs on a schedule (e.g. every 600 seconds) specified through
808/// [`MtuDiscoveryConfig::interval`]. The first run happens right after the handshake, and
809/// subsequent discoveries are scheduled to run when the interval has elapsed, starting from the
810/// last time when MTU discovery completed.
811///
812/// Since the search space for MTUs is quite big (the smallest possible MTU is 1200, and the highest
813/// is 65527), noq performs a binary search to keep the number of probes as low as possible. The
814/// lower bound of the search is equal to [`TransportConfig::initial_mtu`] in the
815/// initial MTU discovery run, and is equal to the currently discovered MTU in subsequent runs. The
816/// upper bound is determined by the minimum of [`MtuDiscoveryConfig::upper_bound`] and the
817/// `max_udp_payload_size` transport parameter received from the peer during the handshake.
818///
819/// # Black hole detection
820///
821/// If, at some point, the network path no longer accepts packets of the detected size, packet loss
822/// will eventually trigger black hole detection and reset the detected MTU to 1200. In that case,
823/// MTU discovery will be triggered after [`MtuDiscoveryConfig::black_hole_cooldown`] (ignoring the
824/// timer that was set based on [`MtuDiscoveryConfig::interval`]).
825///
826/// # Interaction between peers
827///
828/// There is no guarantee that the MTU on the path between A and B is the same as the MTU of the
829/// path between B and A. Therefore, each peer in the connection needs to run MTU discovery
830/// independently in order to discover the path's MTU.
831#[derive(Clone, Debug)]
832pub struct MtuDiscoveryConfig {
833 pub(crate) interval: Duration,
834 pub(crate) upper_bound: u16,
835 pub(crate) minimum_change: u16,
836 pub(crate) black_hole_cooldown: Duration,
837}
838
839impl MtuDiscoveryConfig {
840 /// Specifies the time to wait after completing MTU discovery before starting a new MTU
841 /// discovery run.
842 ///
843 /// Defaults to 600 seconds, as recommended by [RFC
844 /// 8899](https://www.rfc-editor.org/rfc/rfc8899).
845 pub fn interval(&mut self, value: Duration) -> &mut Self {
846 self.interval = value;
847 self
848 }
849
850 /// Specifies the upper bound to the max UDP payload size that MTU discovery will search for.
851 ///
852 /// Defaults to 1452, to stay within Ethernet's MTU when using IPv4 and IPv6. The highest
853 /// allowed value is 65527, which corresponds to the maximum permitted UDP payload on IPv6.
854 ///
855 /// It is safe to use an arbitrarily high upper bound, regardless of the network path's MTU. The
856 /// only drawback is that MTU discovery might take more time to finish.
857 pub fn upper_bound(&mut self, value: u16) -> &mut Self {
858 self.upper_bound = value.min(MAX_UDP_PAYLOAD);
859 self
860 }
861
862 /// Specifies the amount of time that MTU discovery should wait after a black hole was detected
863 /// before running again. Defaults to one minute.
864 ///
865 /// Black hole detection can be spuriously triggered in case of congestion, so it makes sense to
866 /// try MTU discovery again after a short period of time.
867 pub fn black_hole_cooldown(&mut self, value: Duration) -> &mut Self {
868 self.black_hole_cooldown = value;
869 self
870 }
871
872 /// Specifies the minimum MTU change to stop the MTU discovery phase.
873 /// Defaults to 20.
874 pub fn minimum_change(&mut self, value: u16) -> &mut Self {
875 self.minimum_change = value;
876 self
877 }
878}
879
880impl Default for MtuDiscoveryConfig {
881 fn default() -> Self {
882 Self {
883 interval: Duration::from_secs(600),
884 upper_bound: 1452,
885 black_hole_cooldown: Duration::from_secs(60),
886 minimum_change: 20,
887 }
888 }
889}
890
891/// Maximum duration of inactivity to accept before timing out the connection
892///
893/// This wraps an underlying [`VarInt`], representing the duration in milliseconds. Values can be
894/// constructed by converting directly from `VarInt`, or using `TryFrom<Duration>`.
895///
896/// ```
897/// # use std::{convert::TryFrom, time::Duration};
898/// # use noq_proto::{IdleTimeout, VarIntBoundsExceeded, VarInt};
899/// # fn main() -> Result<(), VarIntBoundsExceeded> {
900/// // A `VarInt`-encoded value in milliseconds
901/// let timeout = IdleTimeout::from(VarInt::from_u32(10_000));
902///
903/// // Try to convert a `Duration` into a `VarInt`-encoded timeout
904/// let timeout = IdleTimeout::try_from(Duration::from_secs(10))?;
905/// # Ok(())
906/// # }
907/// ```
908#[derive(Default, Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
909pub struct IdleTimeout(VarInt);
910
911impl From<VarInt> for IdleTimeout {
912 fn from(inner: VarInt) -> Self {
913 Self(inner)
914 }
915}
916
917impl std::convert::TryFrom<Duration> for IdleTimeout {
918 type Error = VarIntBoundsExceeded;
919
920 fn try_from(timeout: Duration) -> Result<Self, Self::Error> {
921 let inner = VarInt::try_from(timeout.as_millis())?;
922 Ok(Self(inner))
923 }
924}
925
926impl fmt::Debug for IdleTimeout {
927 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
928 self.0.fmt(f)
929 }
930}