ant_quic/config/
transport.rs

1use std::{fmt, sync::Arc};
2
3use crate::{Duration, INITIAL_MTU, MAX_UDP_PAYLOAD, VarInt, VarIntBoundsExceeded, congestion};
4
5/// Parameters governing the core QUIC state machine
6///
7/// Default values should be suitable for most internet applications. Applications protocols which
8/// forbid remotely-initiated streams should set `max_concurrent_bidi_streams` and
9/// `max_concurrent_uni_streams` to zero.
10///
11/// In some cases, performance or resource requirements can be improved by tuning these values to
12/// suit a particular application and/or network connection. In particular, data window sizes can be
13/// tuned for a particular expected round trip time, link capacity, and memory availability. Tuning
14/// for higher bandwidths and latencies increases worst-case memory consumption, but does not impair
15/// performance at lower bandwidths and latencies. The default configuration is tuned for a 100Mbps
16/// link with a 100ms round trip time.
17pub struct TransportConfig {
18    pub(crate) max_concurrent_bidi_streams: VarInt,
19    pub(crate) max_concurrent_uni_streams: VarInt,
20    pub(crate) max_idle_timeout: Option<VarInt>,
21    pub(crate) stream_receive_window: VarInt,
22    pub(crate) receive_window: VarInt,
23    pub(crate) send_window: u64,
24    pub(crate) send_fairness: bool,
25
26    pub(crate) packet_threshold: u32,
27    pub(crate) time_threshold: f32,
28    pub(crate) initial_rtt: Duration,
29    pub(crate) initial_mtu: u16,
30    pub(crate) min_mtu: u16,
31    pub(crate) mtu_discovery_config: Option<MtuDiscoveryConfig>,
32    pub(crate) pad_to_mtu: bool,
33    pub(crate) ack_frequency_config: Option<AckFrequencyConfig>,
34
35    pub(crate) persistent_congestion_threshold: u32,
36    pub(crate) keep_alive_interval: Option<Duration>,
37    pub(crate) crypto_buffer_size: usize,
38    pub(crate) allow_spin: bool,
39    pub(crate) datagram_receive_buffer_size: Option<usize>,
40    pub(crate) datagram_send_buffer_size: usize,
41    #[cfg(test)]
42    pub(crate) deterministic_packet_numbers: bool,
43
44    pub(crate) congestion_controller_factory: Arc<dyn congestion::ControllerFactory + Send + Sync>,
45
46    pub(crate) enable_segmentation_offload: bool,
47    
48    /// NAT traversal configuration
49    pub(crate) nat_traversal_config: Option<crate::transport_parameters::NatTraversalConfig>,
50}
51
52impl TransportConfig {
53    /// Maximum number of incoming bidirectional streams that may be open concurrently
54    ///
55    /// Must be nonzero for the peer to open any bidirectional streams.
56    ///
57    /// Worst-case memory use is directly proportional to `max_concurrent_bidi_streams *
58    /// stream_receive_window`, with an upper bound proportional to `receive_window`.
59    pub fn max_concurrent_bidi_streams(&mut self, value: VarInt) -> &mut Self {
60        self.max_concurrent_bidi_streams = value;
61        self
62    }
63
64    /// Variant of `max_concurrent_bidi_streams` affecting unidirectional streams
65    pub fn max_concurrent_uni_streams(&mut self, value: VarInt) -> &mut Self {
66        self.max_concurrent_uni_streams = value;
67        self
68    }
69
70    /// Maximum duration of inactivity to accept before timing out the connection.
71    ///
72    /// The true idle timeout is the minimum of this and the peer's own max idle timeout. `None`
73    /// represents an infinite timeout. Defaults to 30 seconds.
74    ///
75    /// **WARNING**: If a peer or its network path malfunctions or acts maliciously, an infinite
76    /// idle timeout can result in permanently hung futures!
77    ///
78    /// ```
79    /// # use std::{convert::TryInto, time::Duration};
80    /// # use quinn_proto::{TransportConfig, VarInt, VarIntBoundsExceeded};
81    /// # fn main() -> Result<(), VarIntBoundsExceeded> {
82    /// let mut config = TransportConfig::default();
83    ///
84    /// // Set the idle timeout as `VarInt`-encoded milliseconds
85    /// config.max_idle_timeout(Some(VarInt::from_u32(10_000).into()));
86    ///
87    /// // Set the idle timeout as a `Duration`
88    /// config.max_idle_timeout(Some(Duration::from_secs(10).try_into()?));
89    /// # Ok(())
90    /// # }
91    /// ```
92    pub fn max_idle_timeout(&mut self, value: Option<IdleTimeout>) -> &mut Self {
93        self.max_idle_timeout = value.map(|t| t.0);
94        self
95    }
96
97    /// Maximum number of bytes the peer may transmit without acknowledgement on any one stream
98    /// before becoming blocked.
99    ///
100    /// This should be set to at least the expected connection latency multiplied by the maximum
101    /// desired throughput. Setting this smaller than `receive_window` helps ensure that a single
102    /// stream doesn't monopolize receive buffers, which may otherwise occur if the application
103    /// chooses not to read from a large stream for a time while still requiring data on other
104    /// streams.
105    pub fn stream_receive_window(&mut self, value: VarInt) -> &mut Self {
106        self.stream_receive_window = value;
107        self
108    }
109
110    /// Maximum number of bytes the peer may transmit across all streams of a connection before
111    /// becoming blocked.
112    ///
113    /// This should be set to at least the expected connection latency multiplied by the maximum
114    /// desired throughput. Larger values can be useful to allow maximum throughput within a
115    /// stream while another is blocked.
116    pub fn receive_window(&mut self, value: VarInt) -> &mut Self {
117        self.receive_window = value;
118        self
119    }
120
121    /// Maximum number of bytes to transmit to a peer without acknowledgment
122    ///
123    /// Provides an upper bound on memory when communicating with peers that issue large amounts of
124    /// flow control credit. Endpoints that wish to handle large numbers of connections robustly
125    /// should take care to set this low enough to guarantee memory exhaustion does not occur if
126    /// every connection uses the entire window.
127    pub fn send_window(&mut self, value: u64) -> &mut Self {
128        self.send_window = value;
129        self
130    }
131
132    /// Whether to implement fair queuing for send streams having the same priority.
133    ///
134    /// When enabled, connections schedule data from outgoing streams having the same priority in a
135    /// round-robin fashion. When disabled, streams are scheduled in the order they are written to.
136    ///
137    /// Note that this only affects streams with the same priority. Higher priority streams always
138    /// take precedence over lower priority streams.
139    ///
140    /// Disabling fairness can reduce fragmentation and protocol overhead for workloads that use
141    /// many small streams.
142    pub fn send_fairness(&mut self, value: bool) -> &mut Self {
143        self.send_fairness = value;
144        self
145    }
146
147    /// Maximum reordering in packet number space before FACK style loss detection considers a
148    /// packet lost. Should not be less than 3, per RFC5681.
149    pub fn packet_threshold(&mut self, value: u32) -> &mut Self {
150        self.packet_threshold = value;
151        self
152    }
153
154    /// Maximum reordering in time space before time based loss detection considers a packet lost,
155    /// as a factor of RTT
156    pub fn time_threshold(&mut self, value: f32) -> &mut Self {
157        self.time_threshold = value;
158        self
159    }
160
161    /// The RTT used before an RTT sample is taken
162    pub fn initial_rtt(&mut self, value: Duration) -> &mut Self {
163        self.initial_rtt = value;
164        self
165    }
166
167    /// The initial value to be used as the maximum UDP payload size before running MTU discovery
168    /// (see [`TransportConfig::mtu_discovery_config`]).
169    ///
170    /// Must be at least 1200, which is the default, and known to be safe for typical internet
171    /// applications. Larger values are more efficient, but increase the risk of packet loss due to
172    /// exceeding the network path's IP MTU. If the provided value is higher than what the network
173    /// path actually supports, packet loss will eventually trigger black hole detection and bring
174    /// it down to [`TransportConfig::min_mtu`].
175    pub fn initial_mtu(&mut self, value: u16) -> &mut Self {
176        self.initial_mtu = value.max(INITIAL_MTU);
177        self
178    }
179
180    pub(crate) fn get_initial_mtu(&self) -> u16 {
181        self.initial_mtu.max(self.min_mtu)
182    }
183
184    /// The maximum UDP payload size guaranteed to be supported by the network.
185    ///
186    /// Must be at least 1200, which is the default, and lower than or equal to
187    /// [`TransportConfig::initial_mtu`].
188    ///
189    /// Real-world MTUs can vary according to ISP, VPN, and properties of intermediate network links
190    /// outside of either endpoint's control. Extreme care should be used when raising this value
191    /// outside of private networks where these factors are fully controlled. If the provided value
192    /// is higher than what the network path actually supports, the result will be unpredictable and
193    /// catastrophic packet loss, without a possibility of repair. Prefer
194    /// [`TransportConfig::initial_mtu`] together with
195    /// [`TransportConfig::mtu_discovery_config`] to set a maximum UDP payload size that robustly
196    /// adapts to the network.
197    pub fn min_mtu(&mut self, value: u16) -> &mut Self {
198        self.min_mtu = value.max(INITIAL_MTU);
199        self
200    }
201
202    /// Specifies the MTU discovery config (see [`MtuDiscoveryConfig`] for details).
203    ///
204    /// Enabled by default.
205    pub fn mtu_discovery_config(&mut self, value: Option<MtuDiscoveryConfig>) -> &mut Self {
206        self.mtu_discovery_config = value;
207        self
208    }
209
210    /// Pad UDP datagrams carrying application data to current maximum UDP payload size
211    ///
212    /// Disabled by default. UDP datagrams containing loss probes are exempt from padding.
213    ///
214    /// Enabling this helps mitigate traffic analysis by network observers, but it increases
215    /// bandwidth usage. Without this mitigation precise plain text size of application datagrams as
216    /// well as the total size of stream write bursts can be inferred by observers under certain
217    /// conditions. This analysis requires either an uncongested connection or application datagrams
218    /// too large to be coalesced.
219    pub fn pad_to_mtu(&mut self, value: bool) -> &mut Self {
220        self.pad_to_mtu = value;
221        self
222    }
223
224    /// Specifies the ACK frequency config (see [`AckFrequencyConfig`] for details)
225    ///
226    /// The provided configuration will be ignored if the peer does not support the acknowledgement
227    /// frequency QUIC extension.
228    ///
229    /// Defaults to `None`, which disables controlling the peer's acknowledgement frequency. Even
230    /// if set to `None`, the local side still supports the acknowledgement frequency QUIC
231    /// extension and may use it in other ways.
232    pub fn ack_frequency_config(&mut self, value: Option<AckFrequencyConfig>) -> &mut Self {
233        self.ack_frequency_config = value;
234        self
235    }
236
237    /// Number of consecutive PTOs after which network is considered to be experiencing persistent congestion.
238    pub fn persistent_congestion_threshold(&mut self, value: u32) -> &mut Self {
239        self.persistent_congestion_threshold = value;
240        self
241    }
242
243    /// Period of inactivity before sending a keep-alive packet
244    ///
245    /// Keep-alive packets prevent an inactive but otherwise healthy connection from timing out.
246    ///
247    /// `None` to disable, which is the default. Only one side of any given connection needs keep-alive
248    /// enabled for the connection to be preserved. Must be set lower than the idle_timeout of both
249    /// peers to be effective.
250    pub fn keep_alive_interval(&mut self, value: Option<Duration>) -> &mut Self {
251        self.keep_alive_interval = value;
252        self
253    }
254
255    /// Maximum quantity of out-of-order crypto layer data to buffer
256    pub fn crypto_buffer_size(&mut self, value: usize) -> &mut Self {
257        self.crypto_buffer_size = value;
258        self
259    }
260
261    /// Whether the implementation is permitted to set the spin bit on this connection
262    ///
263    /// This allows passive observers to easily judge the round trip time of a connection, which can
264    /// be useful for network administration but sacrifices a small amount of privacy.
265    pub fn allow_spin(&mut self, value: bool) -> &mut Self {
266        self.allow_spin = value;
267        self
268    }
269
270    /// Maximum number of incoming application datagram bytes to buffer, or None to disable
271    /// incoming datagrams
272    ///
273    /// The peer is forbidden to send single datagrams larger than this size. If the aggregate size
274    /// of all datagrams that have been received from the peer but not consumed by the application
275    /// exceeds this value, old datagrams are dropped until it is no longer exceeded.
276    pub fn datagram_receive_buffer_size(&mut self, value: Option<usize>) -> &mut Self {
277        self.datagram_receive_buffer_size = value;
278        self
279    }
280
281    /// Maximum number of outgoing application datagram bytes to buffer
282    ///
283    /// While datagrams are sent ASAP, it is possible for an application to generate data faster
284    /// than the link, or even the underlying hardware, can transmit them. This limits the amount of
285    /// memory that may be consumed in that case. When the send buffer is full and a new datagram is
286    /// sent, older datagrams are dropped until sufficient space is available.
287    pub fn datagram_send_buffer_size(&mut self, value: usize) -> &mut Self {
288        self.datagram_send_buffer_size = value;
289        self
290    }
291
292    /// Whether to force every packet number to be used
293    ///
294    /// By default, packet numbers are occasionally skipped to ensure peers aren't ACKing packets
295    /// before they see them.
296    #[cfg(test)]
297    pub(crate) fn deterministic_packet_numbers(&mut self, enabled: bool) -> &mut Self {
298        self.deterministic_packet_numbers = enabled;
299        self
300    }
301
302    /// How to construct new `congestion::Controller`s
303    ///
304    /// Typically the refcounted configuration of a `congestion::Controller`,
305    /// e.g. a `congestion::NewRenoConfig`.
306    ///
307    /// # Example
308    /// ```
309    /// # use quinn_proto::*; use std::sync::Arc;
310    /// let mut config = TransportConfig::default();
311    /// config.congestion_controller_factory(Arc::new(congestion::NewRenoConfig::default()));
312    /// ```
313    pub fn congestion_controller_factory(
314        &mut self,
315        factory: Arc<dyn congestion::ControllerFactory + Send + Sync + 'static>,
316    ) -> &mut Self {
317        self.congestion_controller_factory = factory;
318        self
319    }
320
321    /// Whether to use "Generic Segmentation Offload" to accelerate transmits, when supported by the
322    /// environment
323    ///
324    /// Defaults to `true`.
325    ///
326    /// GSO dramatically reduces CPU consumption when sending large numbers of packets with the same
327    /// headers, such as when transmitting bulk data on a connection. However, it is not supported
328    /// by all network interface drivers or packet inspection tools. `quinn-udp` will attempt to
329    /// disable GSO automatically when unavailable, but this can lead to spurious packet loss at
330    /// startup, temporarily degrading performance.
331    pub fn enable_segmentation_offload(&mut self, enabled: bool) -> &mut Self {
332        self.enable_segmentation_offload = enabled;
333        self
334    }
335
336    /// Configure NAT traversal capabilities for this connection
337    ///
338    /// When enabled, this connection will support QUIC NAT traversal extensions including:
339    /// - Address candidate advertisement and validation
340    /// - Coordinated hole punching through bootstrap nodes
341    /// - Multi-path connectivity testing
342    /// - Automatic path migration for NAT rebinding
343    ///
344    /// This is required for P2P connections through NATs in Autonomi networks.
345    /// Pass `None` to disable NAT traversal or use the high-level NAT traversal API
346    /// to create appropriate configurations.
347    pub fn nat_traversal_config(&mut self, config: Option<crate::transport_parameters::NatTraversalConfig>) -> &mut Self {
348        self.nat_traversal_config = config;
349        self
350    }
351}
352
353impl Default for TransportConfig {
354    fn default() -> Self {
355        const EXPECTED_RTT: u32 = 100; // ms
356        const MAX_STREAM_BANDWIDTH: u32 = 12500 * 1000; // bytes/s
357        // Window size needed to avoid pipeline
358        // stalls
359        const STREAM_RWND: u32 = MAX_STREAM_BANDWIDTH / 1000 * EXPECTED_RTT;
360
361        Self {
362            max_concurrent_bidi_streams: 100u32.into(),
363            max_concurrent_uni_streams: 100u32.into(),
364            // 30 second default recommended by RFC 9308 ยง 3.2
365            max_idle_timeout: Some(VarInt(30_000)),
366            stream_receive_window: STREAM_RWND.into(),
367            receive_window: VarInt::MAX,
368            send_window: (8 * STREAM_RWND).into(),
369            send_fairness: true,
370
371            packet_threshold: 3,
372            time_threshold: 9.0 / 8.0,
373            initial_rtt: Duration::from_millis(333), // per spec, intentionally distinct from EXPECTED_RTT
374            initial_mtu: INITIAL_MTU,
375            min_mtu: INITIAL_MTU,
376            mtu_discovery_config: Some(MtuDiscoveryConfig::default()),
377            pad_to_mtu: false,
378            ack_frequency_config: None,
379
380            persistent_congestion_threshold: 3,
381            keep_alive_interval: None,
382            crypto_buffer_size: 16 * 1024,
383            allow_spin: true,
384            datagram_receive_buffer_size: Some(STREAM_RWND as usize),
385            datagram_send_buffer_size: 1024 * 1024,
386            #[cfg(test)]
387            deterministic_packet_numbers: false,
388
389            congestion_controller_factory: Arc::new(congestion::CubicConfig::default()),
390
391            enable_segmentation_offload: true,
392            nat_traversal_config: None,
393        }
394    }
395}
396
397impl fmt::Debug for TransportConfig {
398    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
399        let Self {
400            max_concurrent_bidi_streams,
401            max_concurrent_uni_streams,
402            max_idle_timeout,
403            stream_receive_window,
404            receive_window,
405            send_window,
406            send_fairness,
407            packet_threshold,
408            time_threshold,
409            initial_rtt,
410            initial_mtu,
411            min_mtu,
412            mtu_discovery_config,
413            pad_to_mtu,
414            ack_frequency_config,
415            persistent_congestion_threshold,
416            keep_alive_interval,
417            crypto_buffer_size,
418            allow_spin,
419            datagram_receive_buffer_size,
420            datagram_send_buffer_size,
421            #[cfg(test)]
422                deterministic_packet_numbers: _,
423            congestion_controller_factory: _,
424            enable_segmentation_offload,
425            nat_traversal_config,
426        } = self;
427        fmt.debug_struct("TransportConfig")
428            .field("max_concurrent_bidi_streams", max_concurrent_bidi_streams)
429            .field("max_concurrent_uni_streams", max_concurrent_uni_streams)
430            .field("max_idle_timeout", max_idle_timeout)
431            .field("stream_receive_window", stream_receive_window)
432            .field("receive_window", receive_window)
433            .field("send_window", send_window)
434            .field("send_fairness", send_fairness)
435            .field("packet_threshold", packet_threshold)
436            .field("time_threshold", time_threshold)
437            .field("initial_rtt", initial_rtt)
438            .field("initial_mtu", initial_mtu)
439            .field("min_mtu", min_mtu)
440            .field("mtu_discovery_config", mtu_discovery_config)
441            .field("pad_to_mtu", pad_to_mtu)
442            .field("ack_frequency_config", ack_frequency_config)
443            .field(
444                "persistent_congestion_threshold",
445                persistent_congestion_threshold,
446            )
447            .field("keep_alive_interval", keep_alive_interval)
448            .field("crypto_buffer_size", crypto_buffer_size)
449            .field("allow_spin", allow_spin)
450            .field("datagram_receive_buffer_size", datagram_receive_buffer_size)
451            .field("datagram_send_buffer_size", datagram_send_buffer_size)
452            // congestion_controller_factory not debug
453            .field("enable_segmentation_offload", enable_segmentation_offload)
454            .field("nat_traversal_config", nat_traversal_config)
455            .finish_non_exhaustive()
456    }
457}
458
459/// Parameters for controlling the peer's acknowledgement frequency
460///
461/// The parameters provided in this config will be sent to the peer at the beginning of the
462/// connection, so it can take them into account when sending acknowledgements (see each parameter's
463/// description for details on how it influences acknowledgement frequency).
464///
465/// Quinn's implementation follows the fourth draft of the
466/// [QUIC Acknowledgement Frequency extension](https://datatracker.ietf.org/doc/html/draft-ietf-quic-ack-frequency-04).
467/// The defaults produce behavior slightly different than the behavior without this extension,
468/// because they change the way reordered packets are handled (see
469/// [`AckFrequencyConfig::reordering_threshold`] for details).
470#[derive(Clone, Debug)]
471pub struct AckFrequencyConfig {
472    pub(crate) ack_eliciting_threshold: VarInt,
473    pub(crate) max_ack_delay: Option<Duration>,
474    pub(crate) reordering_threshold: VarInt,
475}
476
477impl AckFrequencyConfig {
478    /// The ack-eliciting threshold we will request the peer to use
479    ///
480    /// This threshold represents the number of ack-eliciting packets an endpoint may receive
481    /// without immediately sending an ACK.
482    ///
483    /// The remote peer should send at least one ACK frame when more than this number of
484    /// ack-eliciting packets have been received. A value of 0 results in a receiver immediately
485    /// acknowledging every ack-eliciting packet.
486    ///
487    /// Defaults to 1, which sends ACK frames for every other ack-eliciting packet.
488    pub fn ack_eliciting_threshold(&mut self, value: VarInt) -> &mut Self {
489        self.ack_eliciting_threshold = value;
490        self
491    }
492
493    /// The `max_ack_delay` we will request the peer to use
494    ///
495    /// This parameter represents the maximum amount of time that an endpoint waits before sending
496    /// an ACK when the ack-eliciting threshold hasn't been reached.
497    ///
498    /// The effective `max_ack_delay` will be clamped to be at least the peer's `min_ack_delay`
499    /// transport parameter, and at most the greater of the current path RTT or 25ms.
500    ///
501    /// Defaults to `None`, in which case the peer's original `max_ack_delay` will be used, as
502    /// obtained from its transport parameters.
503    pub fn max_ack_delay(&mut self, value: Option<Duration>) -> &mut Self {
504        self.max_ack_delay = value;
505        self
506    }
507
508    /// The reordering threshold we will request the peer to use
509    ///
510    /// This threshold represents the amount of out-of-order packets that will trigger an endpoint
511    /// to send an ACK, without waiting for `ack_eliciting_threshold` to be exceeded or for
512    /// `max_ack_delay` to be elapsed.
513    ///
514    /// A value of 0 indicates out-of-order packets do not elicit an immediate ACK. A value of 1
515    /// immediately acknowledges any packets that are received out of order (this is also the
516    /// behavior when the extension is disabled).
517    ///
518    /// It is recommended to set this value to [`TransportConfig::packet_threshold`] minus one.
519    /// Since the default value for [`TransportConfig::packet_threshold`] is 3, this value defaults
520    /// to 2.
521    pub fn reordering_threshold(&mut self, value: VarInt) -> &mut Self {
522        self.reordering_threshold = value;
523        self
524    }
525}
526
527impl Default for AckFrequencyConfig {
528    fn default() -> Self {
529        Self {
530            ack_eliciting_threshold: VarInt(1),
531            max_ack_delay: None,
532            reordering_threshold: VarInt(2),
533        }
534    }
535}
536
537/// Parameters governing MTU discovery.
538///
539/// # The why of MTU discovery
540///
541/// By design, QUIC ensures during the handshake that the network path between the client and the
542/// server is able to transmit unfragmented UDP packets with a body of 1200 bytes. In other words,
543/// once the connection is established, we know that the network path's maximum transmission unit
544/// (MTU) is of at least 1200 bytes (plus IP and UDP headers). Because of this, a QUIC endpoint can
545/// split outgoing data in packets of 1200 bytes, with confidence that the network will be able to
546/// deliver them (if the endpoint were to send bigger packets, they could prove too big and end up
547/// being dropped).
548///
549/// There is, however, a significant overhead associated to sending a packet. If the same
550/// information can be sent in fewer packets, that results in higher throughput. The amount of
551/// packets that need to be sent is inversely proportional to the MTU: the higher the MTU, the
552/// bigger the packets that can be sent, and the fewer packets that are needed to transmit a given
553/// amount of bytes.
554///
555/// Most networks have an MTU higher than 1200. Through MTU discovery, endpoints can detect the
556/// path's MTU and, if it turns out to be higher, start sending bigger packets.
557///
558/// # MTU discovery internals
559///
560/// Quinn implements MTU discovery through DPLPMTUD (Datagram Packetization Layer Path MTU
561/// Discovery), described in [section 14.3 of RFC
562/// 9000](https://www.rfc-editor.org/rfc/rfc9000.html#section-14.3). This method consists of sending
563/// QUIC packets padded to a particular size (called PMTU probes), and waiting to see if the remote
564/// peer responds with an ACK. If an ACK is received, that means the probe arrived at the remote
565/// peer, which in turn means that the network path's MTU is of at least the packet's size. If the
566/// probe is lost, it is sent another 2 times before concluding that the MTU is lower than the
567/// packet's size.
568///
569/// MTU discovery runs on a schedule (e.g. every 600 seconds) specified through
570/// [`MtuDiscoveryConfig::interval`]. The first run happens right after the handshake, and
571/// subsequent discoveries are scheduled to run when the interval has elapsed, starting from the
572/// last time when MTU discovery completed.
573///
574/// Since the search space for MTUs is quite big (the smallest possible MTU is 1200, and the highest
575/// is 65527), Quinn performs a binary search to keep the number of probes as low as possible. The
576/// lower bound of the search is equal to [`TransportConfig::initial_mtu`] in the
577/// initial MTU discovery run, and is equal to the currently discovered MTU in subsequent runs. The
578/// upper bound is determined by the minimum of [`MtuDiscoveryConfig::upper_bound`] and the
579/// `max_udp_payload_size` transport parameter received from the peer during the handshake.
580///
581/// # Black hole detection
582///
583/// If, at some point, the network path no longer accepts packets of the detected size, packet loss
584/// will eventually trigger black hole detection and reset the detected MTU to 1200. In that case,
585/// MTU discovery will be triggered after [`MtuDiscoveryConfig::black_hole_cooldown`] (ignoring the
586/// timer that was set based on [`MtuDiscoveryConfig::interval`]).
587///
588/// # Interaction between peers
589///
590/// There is no guarantee that the MTU on the path between A and B is the same as the MTU of the
591/// path between B and A. Therefore, each peer in the connection needs to run MTU discovery
592/// independently in order to discover the path's MTU.
593#[derive(Clone, Debug)]
594pub struct MtuDiscoveryConfig {
595    pub(crate) interval: Duration,
596    pub(crate) upper_bound: u16,
597    pub(crate) minimum_change: u16,
598    pub(crate) black_hole_cooldown: Duration,
599}
600
601impl MtuDiscoveryConfig {
602    /// Specifies the time to wait after completing MTU discovery before starting a new MTU
603    /// discovery run.
604    ///
605    /// Defaults to 600 seconds, as recommended by [RFC
606    /// 8899](https://www.rfc-editor.org/rfc/rfc8899).
607    pub fn interval(&mut self, value: Duration) -> &mut Self {
608        self.interval = value;
609        self
610    }
611
612    /// Specifies the upper bound to the max UDP payload size that MTU discovery will search for.
613    ///
614    /// Defaults to 1452, to stay within Ethernet's MTU when using IPv4 and IPv6. The highest
615    /// allowed value is 65527, which corresponds to the maximum permitted UDP payload on IPv6.
616    ///
617    /// It is safe to use an arbitrarily high upper bound, regardless of the network path's MTU. The
618    /// only drawback is that MTU discovery might take more time to finish.
619    pub fn upper_bound(&mut self, value: u16) -> &mut Self {
620        self.upper_bound = value.min(MAX_UDP_PAYLOAD);
621        self
622    }
623
624    /// Specifies the amount of time that MTU discovery should wait after a black hole was detected
625    /// before running again. Defaults to one minute.
626    ///
627    /// Black hole detection can be spuriously triggered in case of congestion, so it makes sense to
628    /// try MTU discovery again after a short period of time.
629    pub fn black_hole_cooldown(&mut self, value: Duration) -> &mut Self {
630        self.black_hole_cooldown = value;
631        self
632    }
633
634    /// Specifies the minimum MTU change to stop the MTU discovery phase.
635    /// Defaults to 20.
636    pub fn minimum_change(&mut self, value: u16) -> &mut Self {
637        self.minimum_change = value;
638        self
639    }
640}
641
642impl Default for MtuDiscoveryConfig {
643    fn default() -> Self {
644        Self {
645            interval: Duration::from_secs(600),
646            upper_bound: 1452,
647            black_hole_cooldown: Duration::from_secs(60),
648            minimum_change: 20,
649        }
650    }
651}
652
653/// Maximum duration of inactivity to accept before timing out the connection
654///
655/// This wraps an underlying [`VarInt`], representing the duration in milliseconds. Values can be
656/// constructed by converting directly from `VarInt`, or using `TryFrom<Duration>`.
657///
658/// ```
659/// # use std::{convert::TryFrom, time::Duration};
660/// # use quinn_proto::{IdleTimeout, VarIntBoundsExceeded, VarInt};
661/// # fn main() -> Result<(), VarIntBoundsExceeded> {
662/// // A `VarInt`-encoded value in milliseconds
663/// let timeout = IdleTimeout::from(VarInt::from_u32(10_000));
664///
665/// // Try to convert a `Duration` into a `VarInt`-encoded timeout
666/// let timeout = IdleTimeout::try_from(Duration::from_secs(10))?;
667/// # Ok(())
668/// # }
669/// ```
670#[derive(Default, Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
671pub struct IdleTimeout(VarInt);
672
673impl From<VarInt> for IdleTimeout {
674    fn from(inner: VarInt) -> Self {
675        Self(inner)
676    }
677}
678
679impl std::convert::TryFrom<Duration> for IdleTimeout {
680    type Error = VarIntBoundsExceeded;
681
682    fn try_from(timeout: Duration) -> Result<Self, Self::Error> {
683        let inner = VarInt::try_from(timeout.as_millis())?;
684        Ok(Self(inner))
685    }
686}
687
688impl fmt::Debug for IdleTimeout {
689    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
690        self.0.fmt(f)
691    }
692}