iroh_quinn_proto/connection/
paths.rs

1use std::{cmp, net::SocketAddr};
2
3use identity_hash::IntMap;
4use thiserror::Error;
5use tracing::{debug, trace};
6
7use super::{
8    PathError, PathStats,
9    mtud::MtuDiscovery,
10    pacing::Pacer,
11    spaces::{PacketNumberSpace, SentPacket},
12};
13use crate::{
14    ConnectionId, Duration, Instant, TIMER_GRANULARITY, TransportConfig, VarInt,
15    coding::{self, Decodable, Encodable},
16    congestion,
17    frame::ObservedAddr,
18    packet::SpaceId,
19};
20
21#[cfg(feature = "qlog")]
22use qlog::events::quic::RecoveryMetricsUpdated;
23
24/// Id representing different paths when using multipath extension
25#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default)]
26pub struct PathId(pub(crate) u32);
27
28impl std::hash::Hash for PathId {
29    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
30        state.write_u32(self.0);
31    }
32}
33
34impl identity_hash::IdentityHashable for PathId {}
35
36impl Decodable for PathId {
37    fn decode<B: bytes::Buf>(r: &mut B) -> coding::Result<Self> {
38        let v = VarInt::decode(r)?;
39        let v = u32::try_from(v.0).map_err(|_| coding::UnexpectedEnd)?;
40        Ok(Self(v))
41    }
42}
43
44impl Encodable for PathId {
45    fn encode<B: bytes::BufMut>(&self, w: &mut B) {
46        VarInt(self.0.into()).encode(w)
47    }
48}
49
50impl PathId {
51    /// The maximum path ID allowed.
52    pub const MAX: Self = Self(u32::MAX);
53
54    /// The 0 path id.
55    pub const ZERO: Self = Self(0);
56
57    /// The number of bytes this [`PathId`] uses when encoded as a [`VarInt`]
58    pub(crate) const fn size(&self) -> usize {
59        VarInt(self.0 as u64).size()
60    }
61
62    /// Saturating integer addition. Computes self + rhs, saturating at the numeric bounds instead
63    /// of overflowing.
64    pub fn saturating_add(self, rhs: impl Into<Self>) -> Self {
65        let rhs = rhs.into();
66        let inner = self.0.saturating_add(rhs.0);
67        Self(inner)
68    }
69
70    /// Saturating integer subtraction. Computes self - rhs, saturating at the numeric bounds
71    /// instead of overflowing.
72    pub fn saturating_sub(self, rhs: impl Into<Self>) -> Self {
73        let rhs = rhs.into();
74        let inner = self.0.saturating_sub(rhs.0);
75        Self(inner)
76    }
77
78    /// Get the next [`PathId`]
79    pub(crate) fn next(&self) -> Self {
80        self.saturating_add(Self(1))
81    }
82
83    /// Get the underlying u32
84    pub(crate) fn as_u32(&self) -> u32 {
85        self.0
86    }
87}
88
89impl std::fmt::Display for PathId {
90    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
91        self.0.fmt(f)
92    }
93}
94
95impl<T: Into<u32>> From<T> for PathId {
96    fn from(source: T) -> Self {
97        Self(source.into())
98    }
99}
100
101/// State needed for a single path ID.
102///
103/// A single path ID can migrate according to the rules in RFC9000 §9, either voluntary or
104/// involuntary. We need to keep the [`PathData`] of the previously used such path available
105/// in order to defend against migration attacks (see RFC9000 §9.3.1, §9.3.2 and §9.3.3) as
106/// well as to support path probing (RFC9000 §9.1).
107#[derive(Debug)]
108pub(super) struct PathState {
109    pub(super) data: PathData,
110    pub(super) prev: Option<(ConnectionId, PathData)>,
111}
112
113impl PathState {
114    /// Update counters to account for a packet becoming acknowledged, lost, or abandoned
115    pub(super) fn remove_in_flight(&mut self, packet: &SentPacket) {
116        // Visit known paths from newest to oldest to find the one `pn` was sent on
117        for path_data in [&mut self.data]
118            .into_iter()
119            .chain(self.prev.as_mut().map(|(_, data)| data))
120        {
121            if path_data.remove_in_flight(packet) {
122                return;
123            }
124        }
125    }
126}
127
128#[derive(Debug)]
129pub(super) struct SentChallengeInfo {
130    /// When was the challenge sent on the wire.
131    pub(super) sent_instant: Instant,
132    /// The remote to which this path challenge was sent.
133    pub(super) remote: SocketAddr,
134}
135
136/// Description of a particular network path
137#[derive(Debug)]
138pub(super) struct PathData {
139    pub(super) remote: SocketAddr,
140    pub(super) rtt: RttEstimator,
141    /// Whether we're enabling ECN on outgoing packets
142    pub(super) sending_ecn: bool,
143    /// Congestion controller state
144    pub(super) congestion: Box<dyn congestion::Controller>,
145    /// Pacing state
146    pub(super) pacing: Pacer,
147    /// Actually sent challenges (on the wire).
148    pub(super) challenges_sent: IntMap<u64, SentChallengeInfo>,
149    /// Whether to *immediately* trigger another PATH_CHALLENGE.
150    ///
151    /// This is picked up by [`super::Connection::space_can_send`].
152    pub(super) send_new_challenge: bool,
153    /// Pending responses to PATH_CHALLENGE frames
154    pub(super) path_responses: PathResponses,
155    /// Whether we're certain the peer can both send and receive on this address
156    ///
157    /// Initially equal to `use_stateless_retry` for servers, and becomes false again on every
158    /// migration. Always true for clients.
159    pub(super) validated: bool,
160    /// Total size of all UDP datagrams sent on this path
161    pub(super) total_sent: u64,
162    /// Total size of all UDP datagrams received on this path
163    pub(super) total_recvd: u64,
164    /// The state of the MTU discovery process
165    pub(super) mtud: MtuDiscovery,
166    /// Packet number of the first packet sent after an RTT sample was collected on this path
167    ///
168    /// Used in persistent congestion determination.
169    pub(super) first_packet_after_rtt_sample: Option<(SpaceId, u64)>,
170    /// The in-flight packets and bytes
171    ///
172    /// Note that this is across all spaces on this path
173    pub(super) in_flight: InFlight,
174    /// Whether this path has had it's remote address reported back to the peer. This only happens
175    /// if both peers agree to so based on their transport parameters.
176    pub(super) observed_addr_sent: bool,
177    /// Observed address frame with the largest sequence number received from the peer on this path.
178    pub(super) last_observed_addr_report: Option<ObservedAddr>,
179    /// The QUIC-MULTIPATH path status
180    pub(super) status: PathStatusState,
181    /// Number of the first packet sent on this path
182    ///
183    /// With RFC9000 §9 style migration (i.e. not multipath) the PathId does not change and
184    /// hence packet numbers continue. This is used to determine whether a packet was sent
185    /// on such an earlier path. Insufficient to determine if a packet was sent on a later
186    /// path.
187    first_packet: Option<u64>,
188    /// The number of times a PTO has been sent without receiving an ack.
189    pub(super) pto_count: u32,
190
191    //
192    // Per-path idle & keep alive
193    //
194    /// Idle timeout for the path
195    ///
196    /// If expired, the path will be abandoned.  This is different from the connection-wide
197    /// idle timeout which closes the connection if expired.
198    pub(super) idle_timeout: Option<Duration>,
199    /// Keep alives to send on this path
200    ///
201    /// There is also a connection-level keep alive configured in the
202    /// [`TransportParameters`].  This triggers activity on any path which can keep the
203    /// connection alive.
204    ///
205    /// [`TransportParameters`]: crate::transport_parameters::TransportParameters
206    pub(super) keep_alive: Option<Duration>,
207
208    /// Whether the path has already been considered opened from an application perspective
209    ///
210    /// This means, for paths other than the original [`PathId::ZERO`], a first path challenge has
211    /// been responded to, regardless of the initial validation status of the path. This state is
212    /// irreversible, since it's not affected by the path being closed.
213    pub(super) open: bool,
214
215    /// The time at which this path state should've received a PATH_ABANDON already.
216    ///
217    /// Receiving data on this path generates a transport error after that point in time.
218    /// This is checked in [`crate::Connection::on_packet_authenticated`].
219    ///
220    /// If set to `None`, then this path isn't abandoned yet and is allowed to receive data.
221    pub(super) last_allowed_receive: Option<Instant>,
222
223    /// Snapshot of the qlog recovery metrics
224    #[cfg(feature = "qlog")]
225    recovery_metrics: RecoveryMetrics,
226
227    /// Tag uniquely identifying a path in a connection
228    generation: u64,
229}
230
231impl PathData {
232    pub(super) fn new(
233        remote: SocketAddr,
234        allow_mtud: bool,
235        peer_max_udp_payload_size: Option<u16>,
236        generation: u64,
237        now: Instant,
238        config: &TransportConfig,
239    ) -> Self {
240        let congestion = config
241            .congestion_controller_factory
242            .clone()
243            .build(now, config.get_initial_mtu());
244        Self {
245            remote,
246            rtt: RttEstimator::new(config.initial_rtt),
247            sending_ecn: true,
248            pacing: Pacer::new(
249                config.initial_rtt,
250                congestion.initial_window(),
251                config.get_initial_mtu(),
252                now,
253            ),
254            congestion,
255            challenges_sent: Default::default(),
256            send_new_challenge: false,
257            path_responses: PathResponses::default(),
258            validated: false,
259            total_sent: 0,
260            total_recvd: 0,
261            mtud: config
262                .mtu_discovery_config
263                .as_ref()
264                .filter(|_| allow_mtud)
265                .map_or(
266                    MtuDiscovery::disabled(config.get_initial_mtu(), config.min_mtu),
267                    |mtud_config| {
268                        MtuDiscovery::new(
269                            config.get_initial_mtu(),
270                            config.min_mtu,
271                            peer_max_udp_payload_size,
272                            mtud_config.clone(),
273                        )
274                    },
275                ),
276            first_packet_after_rtt_sample: None,
277            in_flight: InFlight::new(),
278            observed_addr_sent: false,
279            last_observed_addr_report: None,
280            status: Default::default(),
281            first_packet: None,
282            pto_count: 0,
283            idle_timeout: config.default_path_max_idle_timeout,
284            keep_alive: config.default_path_keep_alive_interval,
285            open: false,
286            last_allowed_receive: None,
287            #[cfg(feature = "qlog")]
288            recovery_metrics: RecoveryMetrics::default(),
289            generation,
290        }
291    }
292
293    /// Create a new path from a previous one.
294    ///
295    /// This should only be called when migrating paths.
296    pub(super) fn from_previous(
297        remote: SocketAddr,
298        prev: &Self,
299        generation: u64,
300        now: Instant,
301    ) -> Self {
302        let congestion = prev.congestion.clone_box();
303        let smoothed_rtt = prev.rtt.get();
304        Self {
305            remote,
306            rtt: prev.rtt,
307            pacing: Pacer::new(smoothed_rtt, congestion.window(), prev.current_mtu(), now),
308            sending_ecn: true,
309            congestion,
310            challenges_sent: Default::default(),
311            send_new_challenge: false,
312            path_responses: PathResponses::default(),
313            validated: false,
314            total_sent: 0,
315            total_recvd: 0,
316            mtud: prev.mtud.clone(),
317            first_packet_after_rtt_sample: prev.first_packet_after_rtt_sample,
318            in_flight: InFlight::new(),
319            observed_addr_sent: false,
320            last_observed_addr_report: None,
321            status: prev.status.clone(),
322            first_packet: None,
323            pto_count: 0,
324            idle_timeout: prev.idle_timeout,
325            keep_alive: prev.keep_alive,
326            open: false,
327            last_allowed_receive: None,
328            #[cfg(feature = "qlog")]
329            recovery_metrics: prev.recovery_metrics.clone(),
330            generation,
331        }
332    }
333
334    /// Whether we're in the process of validating this path with PATH_CHALLENGEs
335    pub(super) fn is_validating_path(&self) -> bool {
336        !self.challenges_sent.is_empty() || self.send_new_challenge
337    }
338
339    /// Indicates whether we're a server that hasn't validated the peer's address and hasn't
340    /// received enough data from the peer to permit sending `bytes_to_send` additional bytes
341    pub(super) fn anti_amplification_blocked(&self, bytes_to_send: u64) -> bool {
342        !self.validated && self.total_recvd * 3 < self.total_sent + bytes_to_send
343    }
344
345    /// Returns the path's current MTU
346    pub(super) fn current_mtu(&self) -> u16 {
347        self.mtud.current_mtu()
348    }
349
350    /// Account for transmission of `packet` with number `pn` in `space`
351    pub(super) fn sent(&mut self, pn: u64, packet: SentPacket, space: &mut PacketNumberSpace) {
352        self.in_flight.insert(&packet);
353        if self.first_packet.is_none() {
354            self.first_packet = Some(pn);
355        }
356        if let Some(forgotten) = space.sent(pn, packet) {
357            self.remove_in_flight(&forgotten);
358        }
359    }
360
361    /// Remove `packet` with number `pn` from this path's congestion control counters, or return
362    /// `false` if `pn` was sent before this path was established.
363    pub(super) fn remove_in_flight(&mut self, packet: &SentPacket) -> bool {
364        if packet.path_generation != self.generation {
365            return false;
366        }
367        self.in_flight.remove(packet);
368        true
369    }
370
371    /// Increment the total size of sent UDP datagrams
372    pub(super) fn inc_total_sent(&mut self, inc: u64) {
373        self.total_sent = self.total_sent.saturating_add(inc);
374        if !self.validated {
375            trace!(
376                remote = %self.remote,
377                anti_amplification_budget = %(self.total_recvd * 3).saturating_sub(self.total_sent),
378                "anti amplification budget decreased"
379            );
380        }
381    }
382
383    /// Increment the total size of received UDP datagrams
384    pub(super) fn inc_total_recvd(&mut self, inc: u64) {
385        self.total_recvd = self.total_recvd.saturating_add(inc);
386        if !self.validated {
387            trace!(
388                remote = %self.remote,
389                anti_amplification_budget = %(self.total_recvd * 3).saturating_sub(self.total_sent),
390                "anti amplification budget increased"
391            );
392        }
393    }
394
395    /// The earliest time at which a sent challenge is considered lost.
396    pub(super) fn earliest_expiring_challenge(&self) -> Option<Instant> {
397        if self.challenges_sent.is_empty() {
398            return None;
399        }
400        let pto = self.rtt.pto_base();
401        self.challenges_sent
402            .values()
403            .map(|info| info.sent_instant)
404            .min()
405            .map(|sent_instant| sent_instant + pto)
406    }
407
408    /// Handle receiving a PATH_RESPONSE.
409    pub(super) fn on_path_response_received(
410        &mut self,
411        now: Instant,
412        token: u64,
413        remote: SocketAddr,
414    ) -> OnPathResponseReceived {
415        match self.challenges_sent.get(&token) {
416            // Response to an on-path PathChallenge
417            Some(info) if info.remote == remote && self.remote == remote => {
418                let sent_instant = info.sent_instant;
419                if !std::mem::replace(&mut self.validated, true) {
420                    trace!("new path validated");
421                }
422                // Clear any other on-path sent challenge.
423                self.challenges_sent
424                    .retain(|_token, info| info.remote != remote);
425
426                self.send_new_challenge = false;
427
428                // This RTT can only be used for the initial RTT, not as a normal
429                // sample: https://www.rfc-editor.org/rfc/rfc9002#section-6.2.2-2.
430                let rtt = now.saturating_duration_since(sent_instant);
431                self.rtt.reset_initial_rtt(rtt);
432
433                let was_open = std::mem::replace(&mut self.open, true);
434                OnPathResponseReceived::OnPath { was_open }
435            }
436            // Response to an off-path PathChallenge
437            Some(info) if info.remote == remote => {
438                self.challenges_sent
439                    .retain(|_token, info| info.remote != remote);
440                OnPathResponseReceived::OffPath
441            }
442            // Response to a PathChallenge we recognize, but from an invalid remote
443            Some(info) => OnPathResponseReceived::Invalid {
444                expected: info.remote,
445            },
446            // Response to an unknown PathChallenge
447            None => OnPathResponseReceived::Unknown,
448        }
449    }
450
451    #[cfg(feature = "qlog")]
452    pub(super) fn qlog_recovery_metrics(
453        &mut self,
454        path_id: PathId,
455    ) -> Option<RecoveryMetricsUpdated> {
456        let controller_metrics = self.congestion.metrics();
457
458        let metrics = RecoveryMetrics {
459            min_rtt: Some(self.rtt.min),
460            smoothed_rtt: Some(self.rtt.get()),
461            latest_rtt: Some(self.rtt.latest),
462            rtt_variance: Some(self.rtt.var),
463            pto_count: Some(self.pto_count),
464            bytes_in_flight: Some(self.in_flight.bytes),
465            packets_in_flight: Some(self.in_flight.ack_eliciting),
466
467            congestion_window: Some(controller_metrics.congestion_window),
468            ssthresh: controller_metrics.ssthresh,
469            pacing_rate: controller_metrics.pacing_rate,
470        };
471
472        let event = metrics.to_qlog_event(path_id, &self.recovery_metrics);
473        self.recovery_metrics = metrics;
474        event
475    }
476
477    /// Return how long we need to wait before sending `bytes_to_send`
478    ///
479    /// See [`Pacer::delay`].
480    pub(super) fn pacing_delay(&mut self, bytes_to_send: u64, now: Instant) -> Option<Instant> {
481        let smoothed_rtt = self.rtt.get();
482        self.pacing.delay(
483            smoothed_rtt,
484            bytes_to_send,
485            self.current_mtu(),
486            self.congestion.window(),
487            now,
488        )
489    }
490
491    /// Updates the last observed address report received on this path.
492    ///
493    /// If the address was updated, it's returned to be informed to the application.
494    #[must_use = "updated observed address must be reported to the application"]
495    pub(super) fn update_observed_addr_report(
496        &mut self,
497        observed: ObservedAddr,
498    ) -> Option<SocketAddr> {
499        match self.last_observed_addr_report.as_mut() {
500            Some(prev) => {
501                if prev.seq_no >= observed.seq_no {
502                    // frames that do not increase the sequence number on this path are ignored
503                    None
504                } else if prev.ip == observed.ip && prev.port == observed.port {
505                    // keep track of the last seq_no but do not report the address as updated
506                    prev.seq_no = observed.seq_no;
507                    None
508                } else {
509                    let addr = observed.socket_addr();
510                    self.last_observed_addr_report = Some(observed);
511                    Some(addr)
512                }
513            }
514            None => {
515                let addr = observed.socket_addr();
516                self.last_observed_addr_report = Some(observed);
517                Some(addr)
518            }
519        }
520    }
521
522    pub(crate) fn remote_status(&self) -> Option<PathStatus> {
523        self.status.remote_status.map(|(_seq, status)| status)
524    }
525
526    pub(crate) fn local_status(&self) -> PathStatus {
527        self.status.local_status
528    }
529
530    pub(super) fn generation(&self) -> u64 {
531        self.generation
532    }
533}
534
535pub(super) enum OnPathResponseReceived {
536    /// This response validates the path on its current remote address.
537    OnPath { was_open: bool },
538    /// This response is valid, but it's for a remote other than the path's current remote address.
539    OffPath,
540    /// The received token is unknown.
541    Unknown,
542    /// The response is invalid.
543    Invalid {
544        /// The remote that was expected for this token.
545        expected: SocketAddr,
546    },
547}
548
549/// Congestion metrics as described in [`recovery_metrics_updated`].
550///
551/// [`recovery_metrics_updated`]: https://datatracker.ietf.org/doc/html/draft-ietf-quic-qlog-quic-events.html#name-recovery_metrics_updated
552#[cfg(feature = "qlog")]
553#[derive(Default, Clone, PartialEq, Debug)]
554#[non_exhaustive]
555struct RecoveryMetrics {
556    pub min_rtt: Option<Duration>,
557    pub smoothed_rtt: Option<Duration>,
558    pub latest_rtt: Option<Duration>,
559    pub rtt_variance: Option<Duration>,
560    pub pto_count: Option<u32>,
561    pub bytes_in_flight: Option<u64>,
562    pub packets_in_flight: Option<u64>,
563    pub congestion_window: Option<u64>,
564    pub ssthresh: Option<u64>,
565    pub pacing_rate: Option<u64>,
566}
567
568#[cfg(feature = "qlog")]
569impl RecoveryMetrics {
570    /// Retain only values that have been updated since the last snapshot.
571    fn retain_updated(&self, previous: &Self) -> Self {
572        macro_rules! keep_if_changed {
573            ($name:ident) => {
574                if previous.$name == self.$name {
575                    None
576                } else {
577                    self.$name
578                }
579            };
580        }
581
582        Self {
583            min_rtt: keep_if_changed!(min_rtt),
584            smoothed_rtt: keep_if_changed!(smoothed_rtt),
585            latest_rtt: keep_if_changed!(latest_rtt),
586            rtt_variance: keep_if_changed!(rtt_variance),
587            pto_count: keep_if_changed!(pto_count),
588            bytes_in_flight: keep_if_changed!(bytes_in_flight),
589            packets_in_flight: keep_if_changed!(packets_in_flight),
590            congestion_window: keep_if_changed!(congestion_window),
591            ssthresh: keep_if_changed!(ssthresh),
592            pacing_rate: keep_if_changed!(pacing_rate),
593        }
594    }
595
596    /// Emit a `MetricsUpdated` event containing only updated values
597    fn to_qlog_event(&self, path_id: PathId, previous: &Self) -> Option<RecoveryMetricsUpdated> {
598        let updated = self.retain_updated(previous);
599
600        if updated == Self::default() {
601            return None;
602        }
603
604        Some(RecoveryMetricsUpdated {
605            min_rtt: updated.min_rtt.map(|rtt| rtt.as_secs_f32()),
606            smoothed_rtt: updated.smoothed_rtt.map(|rtt| rtt.as_secs_f32()),
607            latest_rtt: updated.latest_rtt.map(|rtt| rtt.as_secs_f32()),
608            rtt_variance: updated.rtt_variance.map(|rtt| rtt.as_secs_f32()),
609            pto_count: updated
610                .pto_count
611                .map(|count| count.try_into().unwrap_or(u16::MAX)),
612            bytes_in_flight: updated.bytes_in_flight,
613            packets_in_flight: updated.packets_in_flight,
614            congestion_window: updated.congestion_window,
615            ssthresh: updated.ssthresh,
616            pacing_rate: updated.pacing_rate,
617            path_id: Some(path_id.as_u32() as u64),
618        })
619    }
620}
621
622/// RTT estimation for a particular network path
623#[derive(Copy, Clone, Debug)]
624pub struct RttEstimator {
625    /// The most recent RTT measurement made when receiving an ack for a previously unacked packet
626    latest: Duration,
627    /// The smoothed RTT of the connection, computed as described in RFC6298
628    smoothed: Option<Duration>,
629    /// The RTT variance, computed as described in RFC6298
630    var: Duration,
631    /// The minimum RTT seen in the connection, ignoring ack delay.
632    min: Duration,
633}
634
635impl RttEstimator {
636    pub(super) fn new(initial_rtt: Duration) -> Self {
637        Self {
638            latest: initial_rtt,
639            smoothed: None,
640            var: initial_rtt / 2,
641            min: initial_rtt,
642        }
643    }
644
645    /// Resets the estimator using a new initial_rtt value.
646    ///
647    /// This only resets the initial_rtt **if** no samples have been recorded yet. If there
648    /// are any recorded samples the initial estimate can not be adjusted after the fact.
649    ///
650    /// This is useful when you receive a PATH_RESPONSE in the first packet received on a
651    /// new path. In this case you can use the delay of the PATH_CHALLENGE-PATH_RESPONSE as
652    /// the initial RTT to get a better expected estimation.
653    ///
654    /// A PATH_CHALLENGE-PATH_RESPONSE pair later in the connection should not be used
655    /// explicitly as an estimation since PATH_CHALLENGE is an ACK-eliciting packet itself
656    /// already.
657    pub(crate) fn reset_initial_rtt(&mut self, initial_rtt: Duration) {
658        if self.smoothed.is_none() {
659            self.latest = initial_rtt;
660            self.var = initial_rtt / 2;
661            self.min = initial_rtt;
662        }
663    }
664
665    /// The current best RTT estimation.
666    pub fn get(&self) -> Duration {
667        self.smoothed.unwrap_or(self.latest)
668    }
669
670    /// Conservative estimate of RTT
671    ///
672    /// Takes the maximum of smoothed and latest RTT, as recommended
673    /// in 6.1.2 of the recovery spec (draft 29).
674    pub fn conservative(&self) -> Duration {
675        self.get().max(self.latest)
676    }
677
678    /// Minimum RTT registered so far for this estimator.
679    pub fn min(&self) -> Duration {
680        self.min
681    }
682
683    /// PTO computed as described in RFC9002#6.2.1.
684    pub(crate) fn pto_base(&self) -> Duration {
685        self.get() + cmp::max(4 * self.var, TIMER_GRANULARITY)
686    }
687
688    /// Records an RTT sample.
689    pub(crate) fn update(&mut self, ack_delay: Duration, rtt: Duration) {
690        self.latest = rtt;
691        // https://www.rfc-editor.org/rfc/rfc9002.html#section-5.2-3:
692        // min_rtt does not adjust for ack_delay to avoid underestimating.
693        self.min = cmp::min(self.min, self.latest);
694        // Based on RFC6298.
695        if let Some(smoothed) = self.smoothed {
696            let adjusted_rtt = if self.min + ack_delay <= self.latest {
697                self.latest - ack_delay
698            } else {
699                self.latest
700            };
701            let var_sample = smoothed.abs_diff(adjusted_rtt);
702            self.var = (3 * self.var + var_sample) / 4;
703            self.smoothed = Some((7 * smoothed + adjusted_rtt) / 8);
704        } else {
705            self.smoothed = Some(self.latest);
706            self.var = self.latest / 2;
707            self.min = self.latest;
708        }
709    }
710}
711
712#[derive(Default, Debug)]
713pub(crate) struct PathResponses {
714    pending: Vec<PathResponse>,
715}
716
717impl PathResponses {
718    pub(crate) fn push(&mut self, packet: u64, token: u64, remote: SocketAddr) {
719        /// Arbitrary permissive limit to prevent abuse
720        const MAX_PATH_RESPONSES: usize = 16;
721        let response = PathResponse {
722            packet,
723            token,
724            remote,
725        };
726        let existing = self.pending.iter_mut().find(|x| x.remote == remote);
727        if let Some(existing) = existing {
728            // Update a queued response
729            if existing.packet <= packet {
730                *existing = response;
731            }
732            return;
733        }
734        if self.pending.len() < MAX_PATH_RESPONSES {
735            self.pending.push(response);
736        } else {
737            // We don't expect to ever hit this with well-behaved peers, so we don't bother dropping
738            // older challenges.
739            trace!("ignoring excessive PATH_CHALLENGE");
740        }
741    }
742
743    pub(crate) fn pop_off_path(&mut self, remote: SocketAddr) -> Option<(u64, SocketAddr)> {
744        let response = *self.pending.last()?;
745        if response.remote == remote {
746            // We don't bother searching further because we expect that the on-path response will
747            // get drained in the immediate future by a call to `pop_on_path`
748            return None;
749        }
750        self.pending.pop();
751        Some((response.token, response.remote))
752    }
753
754    pub(crate) fn pop_on_path(&mut self, remote: SocketAddr) -> Option<u64> {
755        let response = *self.pending.last()?;
756        if response.remote != remote {
757            // We don't bother searching further because we expect that the off-path response will
758            // get drained in the immediate future by a call to `pop_off_path`
759            return None;
760        }
761        self.pending.pop();
762        Some(response.token)
763    }
764
765    pub(crate) fn is_empty(&self) -> bool {
766        self.pending.is_empty()
767    }
768}
769
770#[derive(Copy, Clone, Debug)]
771struct PathResponse {
772    /// The packet number the corresponding PATH_CHALLENGE was received in
773    packet: u64,
774    /// The token of the PATH_CHALLENGE
775    token: u64,
776    /// The address the corresponding PATH_CHALLENGE was received from
777    remote: SocketAddr,
778}
779
780/// Summary statistics of packets that have been sent on a particular path, but which have not yet
781/// been acked or deemed lost
782#[derive(Debug)]
783pub(super) struct InFlight {
784    /// Sum of the sizes of all sent packets considered "in flight" by congestion control
785    ///
786    /// The size does not include IP or UDP overhead. Packets only containing ACK frames do not
787    /// count towards this to ensure congestion control does not impede congestion feedback.
788    pub(super) bytes: u64,
789    /// Number of packets in flight containing frames other than ACK and PADDING
790    ///
791    /// This can be 0 even when bytes is not 0 because PADDING frames cause a packet to be
792    /// considered "in flight" by congestion control. However, if this is nonzero, bytes will always
793    /// also be nonzero.
794    pub(super) ack_eliciting: u64,
795}
796
797impl InFlight {
798    fn new() -> Self {
799        Self {
800            bytes: 0,
801            ack_eliciting: 0,
802        }
803    }
804
805    fn insert(&mut self, packet: &SentPacket) {
806        self.bytes += u64::from(packet.size);
807        self.ack_eliciting += u64::from(packet.ack_eliciting);
808    }
809
810    /// Update counters to account for a packet becoming acknowledged, lost, or abandoned
811    fn remove(&mut self, packet: &SentPacket) {
812        self.bytes -= u64::from(packet.size);
813        self.ack_eliciting -= u64::from(packet.ack_eliciting);
814    }
815}
816
817/// State for QUIC-MULTIPATH PATH_STATUS_AVAILABLE and PATH_STATUS_BACKUP frames
818#[derive(Debug, Clone, Default)]
819pub(super) struct PathStatusState {
820    /// The local status
821    local_status: PathStatus,
822    /// Local sequence number, for both PATH_STATUS_AVAILABLE and PATH_STATUS_BACKUP
823    ///
824    /// This is the number of the *next* path status frame to be sent.
825    local_seq: VarInt,
826    /// The status set by the remote
827    remote_status: Option<(VarInt, PathStatus)>,
828}
829
830impl PathStatusState {
831    /// To be called on received PATH_STATUS_AVAILABLE/PATH_STATUS_BACKUP frames
832    pub(super) fn remote_update(&mut self, status: PathStatus, seq: VarInt) {
833        if self.remote_status.is_some_and(|(curr, _)| curr >= seq) {
834            return trace!(%seq, "ignoring path status update");
835        }
836
837        let prev = self.remote_status.replace((seq, status)).map(|(_, s)| s);
838        if prev != Some(status) {
839            debug!(?status, ?seq, "remote changed path status");
840        }
841    }
842
843    /// Updates the local status
844    ///
845    /// If the local status changed, the previous value is returned
846    pub(super) fn local_update(&mut self, status: PathStatus) -> Option<PathStatus> {
847        if self.local_status == status {
848            return None;
849        }
850
851        self.local_seq = self.local_seq.saturating_add(1u8);
852        Some(std::mem::replace(&mut self.local_status, status))
853    }
854
855    pub(crate) fn seq(&self) -> VarInt {
856        self.local_seq
857    }
858}
859
860/// The QUIC-MULTIPATH path status
861///
862/// See section "3.3 Path Status Management":
863/// <https://quicwg.org/multipath/draft-ietf-quic-multipath.html#name-path-status-management>
864#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
865pub enum PathStatus {
866    /// Paths marked with as available will be used when scheduling packets
867    ///
868    /// If multiple paths are available, packets will be scheduled on whichever has
869    /// capacity.
870    #[default]
871    Available,
872    /// Paths marked as backup will only be used if there are no available paths
873    ///
874    /// If the max_idle_timeout is specified the path will be kept alive so that it does not
875    /// expire.
876    Backup,
877}
878
879/// Application events about paths
880#[derive(Debug, Clone, PartialEq, Eq)]
881pub enum PathEvent {
882    /// A new path has been opened
883    Opened {
884        /// Which path is now open
885        id: PathId,
886    },
887    /// A path has been closed
888    Closed {
889        /// Which path has been closed
890        id: PathId,
891        /// Error code supplied by the peer
892        /// See <https://www.ietf.org/archive/id/draft-ietf-quic-multipath-14.html#name-error-codes>
893        /// for a list of known errors.
894        error_code: VarInt,
895    },
896    /// All remaining state for a path has been removed
897    ///
898    /// The [`PathEvent::Closed`] would have been emitted for this path earlier.
899    Abandoned {
900        /// Which path had its state dropped
901        id: PathId,
902        /// The final path stats, they are no longer available via [`Connection::stats`]
903        ///
904        /// [`Connection::stats`]: super::Connection::stats
905        path_stats: PathStats,
906    },
907    /// Path was closed locally
908    LocallyClosed {
909        /// Path for which the error occurred
910        id: PathId,
911        /// The error that occurred
912        error: PathError,
913    },
914    /// The remote changed the status of the path
915    ///
916    /// The local status is not changed because of this event. It is up to the application
917    /// to update the local status, which is used for packet scheduling, when the remote
918    /// changes the status.
919    RemoteStatus {
920        /// Path which has changed status
921        id: PathId,
922        /// The new status set by the remote
923        status: PathStatus,
924    },
925    /// Received an observation of our external address from the peer.
926    ObservedAddr {
927        /// Path over which the observed address was reported, [`PathId::ZERO`] when multipath is
928        /// not negotiated
929        id: PathId,
930        /// The address observed by the remote over this path
931        addr: SocketAddr,
932    },
933}
934
935/// Error from setting path status
936#[derive(Debug, Error, Clone, PartialEq, Eq)]
937pub enum SetPathStatusError {
938    /// Error indicating that a path has not been opened or has already been abandoned
939    #[error("closed path")]
940    ClosedPath,
941    /// Error indicating that this operation requires multipath to be negotiated whereas it hasn't been
942    #[error("multipath not negotiated")]
943    MultipathNotNegotiated,
944}
945
946/// Error indicating that a path has not been opened or has already been abandoned
947#[derive(Debug, Default, Error, Clone, PartialEq, Eq)]
948#[error("closed path")]
949pub struct ClosedPath {
950    pub(super) _private: (),
951}
952
953#[cfg(test)]
954mod tests {
955    use super::*;
956
957    #[test]
958    fn test_path_id_saturating_add() {
959        // add within range behaves normally
960        let large: PathId = u16::MAX.into();
961        let next = u32::from(u16::MAX) + 1;
962        assert_eq!(large.saturating_add(1u8), PathId::from(next));
963
964        // outside range saturates
965        assert_eq!(PathId::MAX.saturating_add(1u8), PathId::MAX)
966    }
967}