quiche/recovery/gcongestion/
recovery.rs

1use crate::packet;
2use crate::recovery::OnLossDetectionTimeoutOutcome;
3use crate::recovery::INITIAL_TIME_THRESHOLD_OVERHEAD;
4use crate::recovery::TIME_THRESHOLD_OVERHEAD_MULTIPLIER;
5use crate::Error;
6use crate::Result;
7
8use std::collections::VecDeque;
9use std::time::Duration;
10use std::time::Instant;
11
12use smallvec::SmallVec;
13
14#[cfg(feature = "qlog")]
15use qlog::events::EventData;
16
17#[cfg(feature = "qlog")]
18use crate::recovery::QlogMetrics;
19
20use crate::frame;
21
22use crate::recovery::bytes_in_flight::BytesInFlight;
23use crate::recovery::gcongestion::Bandwidth;
24use crate::recovery::rtt::RttStats;
25use crate::recovery::CongestionControlAlgorithm;
26use crate::recovery::HandshakeStatus;
27use crate::recovery::LossDetectionTimer;
28use crate::recovery::OnAckReceivedOutcome;
29use crate::recovery::RangeSet;
30use crate::recovery::RecoveryConfig;
31use crate::recovery::RecoveryOps;
32use crate::recovery::RecoveryStats;
33use crate::recovery::ReleaseDecision;
34use crate::recovery::Sent;
35use crate::recovery::StartupExit;
36use crate::recovery::GRANULARITY;
37use crate::recovery::INITIAL_PACKET_THRESHOLD;
38use crate::recovery::INITIAL_TIME_THRESHOLD;
39use crate::recovery::MAX_OUTSTANDING_NON_ACK_ELICITING;
40use crate::recovery::MAX_PACKET_THRESHOLD;
41use crate::recovery::MAX_PTO_PROBES_COUNT;
42use crate::recovery::PACKET_REORDER_TIME_THRESHOLD;
43
44use super::bbr2::BBRv2;
45use super::pacer::Pacer;
46use super::Acked;
47use super::Lost;
48
49// Congestion Control
50const MAX_WINDOW_PACKETS: usize = 20_000;
51
52#[derive(Debug)]
53struct SentPacket {
54    pkt_num: u64,
55    status: SentStatus,
56}
57
58#[derive(Debug)]
59enum SentStatus {
60    Sent {
61        time_sent: Instant,
62        ack_eliciting: bool,
63        in_flight: bool,
64        has_data: bool,
65        is_pmtud_probe: bool,
66        sent_bytes: usize,
67        frames: SmallVec<[frame::Frame; 1]>,
68    },
69    Acked,
70    Lost,
71}
72
73impl SentStatus {
74    fn ack(&mut self) -> Self {
75        std::mem::replace(self, SentStatus::Acked)
76    }
77
78    fn lose(&mut self) -> Self {
79        if !matches!(self, SentStatus::Acked) {
80            std::mem::replace(self, SentStatus::Lost)
81        } else {
82            SentStatus::Acked
83        }
84    }
85}
86
87#[derive(Default)]
88struct RecoveryEpoch {
89    /// The time the most recent ack-eliciting packet was sent.
90    time_of_last_ack_eliciting_packet: Option<Instant>,
91
92    /// The largest packet number acknowledged in the packet number space so
93    /// far.
94    largest_acked_packet: Option<u64>,
95
96    /// The time at which the next packet in that packet number space can be
97    /// considered lost based on exceeding the reordering window in time.
98    loss_time: Option<Instant>,
99
100    /// An association of packet numbers in a packet number space to information
101    /// about them.
102    sent_packets: VecDeque<SentPacket>,
103
104    loss_probes: usize,
105    pkts_in_flight: usize,
106
107    acked_frames: Vec<frame::Frame>,
108    lost_frames: Vec<frame::Frame>,
109
110    /// The largest packet number sent in the packet number space so far.
111    #[allow(dead_code)]
112    test_largest_sent_pkt_num_on_path: Option<u64>,
113}
114
115struct AckedDetectionResult {
116    acked_bytes: usize,
117    spurious_losses: usize,
118    spurious_pkt_thresh: Option<u64>,
119    has_ack_eliciting: bool,
120}
121
122struct LossDetectionResult {
123    lost_bytes: usize,
124    lost_packets: usize,
125
126    pmtud_lost_bytes: usize,
127    pmtud_lost_packets: SmallVec<[u64; 1]>,
128}
129
130impl RecoveryEpoch {
131    /// Discard the Epoch state and return the total size of unacked packets
132    /// that were discarded
133    fn discard(&mut self, cc: &mut Pacer) -> usize {
134        let unacked_bytes = self
135            .sent_packets
136            .drain(..)
137            .map(|p| {
138                if let SentPacket {
139                    status:
140                        SentStatus::Sent {
141                            in_flight,
142                            sent_bytes,
143                            ..
144                        },
145                    pkt_num,
146                } = p
147                {
148                    cc.on_packet_neutered(pkt_num);
149                    if in_flight {
150                        return sent_bytes;
151                    }
152                }
153                0
154            })
155            .sum();
156
157        std::mem::take(&mut self.sent_packets);
158        self.time_of_last_ack_eliciting_packet = None;
159        self.loss_time = None;
160        self.loss_probes = 0;
161        self.pkts_in_flight = 0;
162
163        unacked_bytes
164    }
165
166    // `peer_sent_ack_ranges` should not be used without validation.
167    fn detect_and_remove_acked_packets(
168        &mut self, peer_sent_ack_ranges: &RangeSet, newly_acked: &mut Vec<Acked>,
169        skip_pn: Option<u64>, trace_id: &str,
170    ) -> Result<AckedDetectionResult> {
171        newly_acked.clear();
172
173        let mut acked_bytes = 0;
174        let mut spurious_losses = 0;
175        let mut spurious_pkt_thresh = None;
176        let mut has_ack_eliciting = false;
177
178        let largest_ack_received = peer_sent_ack_ranges.last().unwrap();
179        let largest_acked = self
180            .largest_acked_packet
181            .unwrap_or(0)
182            .max(largest_ack_received);
183
184        for peer_sent_range in peer_sent_ack_ranges.iter() {
185            if skip_pn.is_some_and(|skip_pn| peer_sent_range.contains(&skip_pn)) {
186                // https://www.rfc-editor.org/rfc/rfc9000#section-13.1
187                // An endpoint SHOULD treat receipt of an acknowledgment
188                // for a packet it did not send as
189                // a connection error of type PROTOCOL_VIOLATION
190                return Err(Error::OptimisticAckDetected);
191            }
192
193            // Because packets always have incrementing numbers, they are always
194            // in sorted order.
195            let start = if self
196                .sent_packets
197                .front()
198                .filter(|e| e.pkt_num >= peer_sent_range.start)
199                .is_some()
200            {
201                // Usually it will be the first packet.
202                0
203            } else {
204                self.sent_packets
205                    .binary_search_by_key(&peer_sent_range.start, |p| p.pkt_num)
206                    .unwrap_or_else(|e| e)
207            };
208
209            for SentPacket { pkt_num, status } in
210                self.sent_packets.range_mut(start..)
211            {
212                if *pkt_num < peer_sent_range.end {
213                    match status.ack() {
214                        SentStatus::Sent {
215                            time_sent,
216                            in_flight,
217                            sent_bytes,
218                            frames,
219                            ack_eliciting,
220                            ..
221                        } => {
222                            if in_flight {
223                                self.pkts_in_flight -= 1;
224                                acked_bytes += sent_bytes;
225                            }
226                            newly_acked.push(Acked {
227                                pkt_num: *pkt_num,
228                                time_sent,
229                            });
230
231                            self.acked_frames.extend(frames);
232
233                            has_ack_eliciting |= ack_eliciting;
234
235                            trace!("{trace_id} packet newly acked {pkt_num}");
236                        },
237
238                        SentStatus::Acked => {},
239                        SentStatus::Lost => {
240                            // An acked packet was already declared lost
241                            spurious_losses += 1;
242                            spurious_pkt_thresh
243                                .get_or_insert(largest_acked - *pkt_num + 1);
244                        },
245                    }
246                } else {
247                    break;
248                }
249            }
250        }
251
252        self.drain_acked_and_lost_packets();
253
254        Ok(AckedDetectionResult {
255            acked_bytes,
256            spurious_losses,
257            spurious_pkt_thresh,
258            has_ack_eliciting,
259        })
260    }
261
262    fn detect_and_remove_lost_packets(
263        &mut self, loss_delay: Duration, pkt_thresh: Option<u64>, now: Instant,
264        newly_lost: &mut Vec<Lost>,
265    ) -> LossDetectionResult {
266        newly_lost.clear();
267        let mut lost_bytes = 0;
268        self.loss_time = None;
269
270        let lost_send_time = now.checked_sub(loss_delay).unwrap();
271        let largest_acked = self.largest_acked_packet.unwrap_or(0);
272        let mut pmtud_lost_bytes = 0;
273        let mut pmtud_lost_packets = SmallVec::new();
274
275        for SentPacket { pkt_num, status } in &mut self.sent_packets {
276            if *pkt_num > largest_acked {
277                break;
278            }
279
280            if let SentStatus::Sent { time_sent, .. } = status {
281                let loss_by_time = *time_sent <= lost_send_time;
282                let loss_by_pkt = match pkt_thresh {
283                    Some(pkt_thresh) => largest_acked >= *pkt_num + pkt_thresh,
284                    None => false,
285                };
286
287                if loss_by_time || loss_by_pkt {
288                    if let SentStatus::Sent {
289                        in_flight,
290                        sent_bytes,
291                        frames,
292                        is_pmtud_probe,
293                        ..
294                    } = status.lose()
295                    {
296                        self.lost_frames.extend(frames);
297
298                        if in_flight {
299                            self.pkts_in_flight -= 1;
300
301                            if is_pmtud_probe {
302                                pmtud_lost_bytes += sent_bytes;
303                                pmtud_lost_packets.push(*pkt_num);
304                                // Do not track PMTUD probes losses
305                                continue;
306                            }
307
308                            lost_bytes += sent_bytes;
309                        }
310
311                        newly_lost.push(Lost {
312                            packet_number: *pkt_num,
313                            bytes_lost: sent_bytes,
314                        });
315                    }
316                } else {
317                    self.loss_time = Some(*time_sent + loss_delay);
318                    break;
319                }
320            }
321        }
322
323        LossDetectionResult {
324            lost_bytes,
325            lost_packets: newly_lost.len(),
326
327            pmtud_lost_bytes,
328            pmtud_lost_packets,
329        }
330    }
331
332    /// Remove packets that were already handled from the front of the queue,
333    /// but avoid removing packets from the middle of the queue to avoid
334    /// compaction
335    fn drain_acked_and_lost_packets(&mut self) {
336        while let Some(SentPacket {
337            status: SentStatus::Acked | SentStatus::Lost,
338            ..
339        }) = self.sent_packets.front()
340        {
341            self.sent_packets.pop_front();
342        }
343    }
344
345    fn least_unacked(&self) -> u64 {
346        for pkt in self.sent_packets.iter() {
347            if let SentPacket {
348                pkt_num,
349                status: SentStatus::Sent { .. },
350            } = pkt
351            {
352                return *pkt_num;
353            }
354        }
355
356        self.largest_acked_packet.unwrap_or(0) + 1
357    }
358}
359
360struct LossThreshold {
361    pkt_thresh: Option<u64>,
362    time_thresh: f64,
363
364    // # Experiment: enable_relaxed_loss_threshold
365    //
366    // If `Some` this will disable pkt_thresh on the first loss and then double
367    // time_thresh on subsequent loss.
368    //
369    // The actual threshold is calcualted as `1.0 +
370    // INITIAL_TIME_THRESHOLD_OVERHEAD` and equivalent to the initial value
371    // of INITIAL_TIME_THRESHOLD.
372    time_thresh_overhead: Option<f64>,
373}
374
375impl LossThreshold {
376    fn new(recovery_config: &RecoveryConfig) -> Self {
377        let time_thresh_overhead =
378            if recovery_config.enable_relaxed_loss_threshold {
379                Some(INITIAL_TIME_THRESHOLD_OVERHEAD)
380            } else {
381                None
382            };
383        LossThreshold {
384            pkt_thresh: Some(INITIAL_PACKET_THRESHOLD),
385            time_thresh: INITIAL_TIME_THRESHOLD,
386            time_thresh_overhead,
387        }
388    }
389
390    fn pkt_thresh(&self) -> Option<u64> {
391        self.pkt_thresh
392    }
393
394    fn time_thresh(&self) -> f64 {
395        self.time_thresh
396    }
397
398    fn on_spurious_loss(&mut self, new_pkt_thresh: u64) {
399        match &mut self.time_thresh_overhead {
400            Some(time_thresh_overhead) => {
401                if self.pkt_thresh.is_some() {
402                    // Disable packet threshold on first spurious loss.
403                    self.pkt_thresh = None;
404                } else {
405                    // Double time threshold but cap it at `1.0`, which ends up
406                    // being 2x the RTT.
407                    *time_thresh_overhead *= TIME_THRESHOLD_OVERHEAD_MULTIPLIER;
408                    *time_thresh_overhead = time_thresh_overhead.min(1.0);
409
410                    self.time_thresh = 1.0 + *time_thresh_overhead;
411                }
412            },
413            None => {
414                let new_packet_threshold = self
415                    .pkt_thresh
416                    .expect("packet threshold should always be Some when `enable_relaxed_loss_threshold` is false")
417                    .max(new_pkt_thresh.min(MAX_PACKET_THRESHOLD));
418                self.pkt_thresh = Some(new_packet_threshold);
419
420                self.time_thresh = PACKET_REORDER_TIME_THRESHOLD;
421            },
422        }
423    }
424}
425
426pub struct GRecovery {
427    epochs: [RecoveryEpoch; packet::Epoch::count()],
428
429    loss_timer: LossDetectionTimer,
430
431    pto_count: u32,
432
433    rtt_stats: RttStats,
434
435    recovery_stats: RecoveryStats,
436
437    pub lost_count: usize,
438
439    pub lost_spurious_count: usize,
440
441    loss_thresh: LossThreshold,
442
443    bytes_in_flight: BytesInFlight,
444
445    bytes_sent: usize,
446
447    pub bytes_lost: u64,
448
449    max_datagram_size: usize,
450
451    #[cfg(feature = "qlog")]
452    qlog_metrics: QlogMetrics,
453
454    #[cfg(feature = "qlog")]
455    qlog_prev_cc_state: &'static str,
456
457    /// How many non-ack-eliciting packets have been sent.
458    outstanding_non_ack_eliciting: usize,
459
460    /// A resusable list of acks.
461    newly_acked: Vec<Acked>,
462
463    /// A [`Vec`] that can be reused for calls of
464    /// [`Self::detect_and_remove_lost_packets`] to avoid allocations
465    lost_reuse: Vec<Lost>,
466
467    pacer: Pacer,
468}
469
470impl GRecovery {
471    pub fn new(recovery_config: &RecoveryConfig) -> Option<Self> {
472        let cc = match recovery_config.cc_algorithm {
473            CongestionControlAlgorithm::Bbr2Gcongestion => BBRv2::new(
474                recovery_config.initial_congestion_window_packets,
475                MAX_WINDOW_PACKETS,
476                recovery_config.max_send_udp_payload_size,
477                recovery_config.initial_rtt,
478                recovery_config.custom_bbr_params.as_ref(),
479            ),
480            _ => return None,
481        };
482
483        Some(Self {
484            epochs: Default::default(),
485            rtt_stats: RttStats::new(
486                recovery_config.initial_rtt,
487                recovery_config.max_ack_delay,
488            ),
489            recovery_stats: RecoveryStats::default(),
490            loss_timer: Default::default(),
491            pto_count: 0,
492
493            lost_count: 0,
494            lost_spurious_count: 0,
495
496            loss_thresh: LossThreshold::new(recovery_config),
497            bytes_in_flight: Default::default(),
498            bytes_sent: 0,
499            bytes_lost: 0,
500
501            max_datagram_size: recovery_config.max_send_udp_payload_size,
502
503            #[cfg(feature = "qlog")]
504            qlog_metrics: QlogMetrics::default(),
505
506            #[cfg(feature = "qlog")]
507            qlog_prev_cc_state: "",
508
509            outstanding_non_ack_eliciting: 0,
510
511            pacer: Pacer::new(
512                recovery_config.pacing,
513                cc,
514                recovery_config
515                    .max_pacing_rate
516                    .map(Bandwidth::from_mbits_per_second),
517            ),
518
519            newly_acked: Vec::new(),
520            lost_reuse: Vec::new(),
521        })
522    }
523
524    fn detect_and_remove_lost_packets(
525        &mut self, epoch: packet::Epoch, now: Instant,
526    ) -> (usize, usize) {
527        let loss_delay =
528            self.rtt_stats.loss_delay(self.loss_thresh.time_thresh());
529        let lost = &mut self.lost_reuse;
530
531        let LossDetectionResult {
532            lost_bytes,
533            lost_packets,
534            pmtud_lost_bytes,
535            pmtud_lost_packets,
536        } = self.epochs[epoch].detect_and_remove_lost_packets(
537            loss_delay,
538            self.loss_thresh.pkt_thresh(),
539            now,
540            lost,
541        );
542
543        self.bytes_in_flight
544            .saturating_subtract(lost_bytes + pmtud_lost_bytes, now);
545
546        for pkt in pmtud_lost_packets {
547            self.pacer.on_packet_neutered(pkt);
548        }
549
550        (lost_bytes, lost_packets)
551    }
552
553    fn loss_time_and_space(&self) -> (Option<Instant>, packet::Epoch) {
554        let mut epoch = packet::Epoch::Initial;
555        let mut time = self.epochs[epoch].loss_time;
556
557        // Iterate over all packet number spaces starting from Handshake.
558        for e in [packet::Epoch::Handshake, packet::Epoch::Application] {
559            let new_time = self.epochs[e].loss_time;
560            if time.is_none() || new_time < time {
561                time = new_time;
562                epoch = e;
563            }
564        }
565
566        (time, epoch)
567    }
568
569    fn pto_time_and_space(
570        &self, handshake_status: HandshakeStatus, now: Instant,
571    ) -> (Option<Instant>, packet::Epoch) {
572        let mut duration = self.pto() * (1 << self.pto_count);
573
574        // Arm PTO from now when there are no inflight packets.
575        if self.bytes_in_flight.is_zero() {
576            if handshake_status.has_handshake_keys {
577                return (Some(now + duration), packet::Epoch::Handshake);
578            } else {
579                return (Some(now + duration), packet::Epoch::Initial);
580            }
581        }
582
583        let mut pto_timeout = None;
584        let mut pto_space = packet::Epoch::Initial;
585
586        // Iterate over all packet number spaces.
587        for &e in packet::Epoch::epochs(
588            packet::Epoch::Initial..=packet::Epoch::Application,
589        ) {
590            if self.epochs[e].pkts_in_flight == 0 {
591                continue;
592            }
593
594            if e == packet::Epoch::Application {
595                // Skip Application Data until handshake completes.
596                if !handshake_status.completed {
597                    return (pto_timeout, pto_space);
598                }
599
600                // Include max_ack_delay and backoff for Application Data.
601                duration +=
602                    self.rtt_stats.max_ack_delay * 2_u32.pow(self.pto_count);
603            }
604
605            let new_time = self.epochs[e]
606                .time_of_last_ack_eliciting_packet
607                .map(|t| t + duration);
608
609            if pto_timeout.is_none() || new_time < pto_timeout {
610                pto_timeout = new_time;
611                pto_space = e;
612            }
613        }
614
615        (pto_timeout, pto_space)
616    }
617
618    fn set_loss_detection_timer(
619        &mut self, handshake_status: HandshakeStatus, now: Instant,
620    ) {
621        if let (Some(earliest_loss_time), _) = self.loss_time_and_space() {
622            // Time threshold loss detection.
623            self.loss_timer.update(earliest_loss_time);
624            return;
625        }
626
627        if self.bytes_in_flight.is_zero() &&
628            handshake_status.peer_verified_address
629        {
630            self.loss_timer.clear();
631            return;
632        }
633
634        // PTO timer.
635        if let (Some(timeout), _) = self.pto_time_and_space(handshake_status, now)
636        {
637            self.loss_timer.update(timeout);
638        }
639    }
640}
641
642impl RecoveryOps for GRecovery {
643    fn lost_count(&self) -> usize {
644        self.lost_count
645    }
646
647    fn bytes_lost(&self) -> u64 {
648        self.bytes_lost
649    }
650
651    fn should_elicit_ack(&self, epoch: packet::Epoch) -> bool {
652        self.epochs[epoch].loss_probes > 0 ||
653            self.outstanding_non_ack_eliciting >=
654                MAX_OUTSTANDING_NON_ACK_ELICITING
655    }
656
657    fn get_acked_frames(&mut self, epoch: packet::Epoch) -> Vec<frame::Frame> {
658        std::mem::take(&mut self.epochs[epoch].acked_frames)
659    }
660
661    fn get_lost_frames(&mut self, epoch: packet::Epoch) -> Vec<frame::Frame> {
662        std::mem::take(&mut self.epochs[epoch].lost_frames)
663    }
664
665    fn get_largest_acked_on_epoch(&self, epoch: packet::Epoch) -> Option<u64> {
666        self.epochs[epoch].largest_acked_packet
667    }
668
669    fn has_lost_frames(&self, epoch: packet::Epoch) -> bool {
670        !self.epochs[epoch].lost_frames.is_empty()
671    }
672
673    fn loss_probes(&self, epoch: packet::Epoch) -> usize {
674        self.epochs[epoch].loss_probes
675    }
676
677    #[cfg(test)]
678    fn inc_loss_probes(&mut self, epoch: packet::Epoch) {
679        self.epochs[epoch].loss_probes += 1;
680    }
681
682    fn ping_sent(&mut self, epoch: packet::Epoch) {
683        self.epochs[epoch].loss_probes =
684            self.epochs[epoch].loss_probes.saturating_sub(1);
685    }
686
687    fn on_packet_sent(
688        &mut self, pkt: Sent, epoch: packet::Epoch,
689        handshake_status: HandshakeStatus, now: Instant, trace_id: &str,
690    ) {
691        let time_sent = self.get_next_release_time().time(now).unwrap_or(now);
692
693        let epoch = &mut self.epochs[epoch];
694
695        let ack_eliciting = pkt.ack_eliciting;
696        let in_flight = pkt.in_flight;
697        let is_pmtud_probe = pkt.is_pmtud_probe;
698        let pkt_num = pkt.pkt_num;
699        let sent_bytes = pkt.size;
700
701        if let Some(SentPacket { pkt_num, .. }) = epoch.sent_packets.back() {
702            assert!(*pkt_num < pkt.pkt_num, "Packet numbers must increase");
703        }
704
705        let status = SentStatus::Sent {
706            time_sent,
707            ack_eliciting,
708            in_flight,
709            is_pmtud_probe,
710            has_data: pkt.has_data,
711            sent_bytes,
712            frames: pkt.frames,
713        };
714
715        #[cfg(test)]
716        {
717            epoch.test_largest_sent_pkt_num_on_path = epoch
718                .test_largest_sent_pkt_num_on_path
719                .max(Some(pkt.pkt_num));
720        }
721
722        epoch.sent_packets.push_back(SentPacket { pkt_num, status });
723
724        if ack_eliciting {
725            epoch.time_of_last_ack_eliciting_packet = Some(time_sent);
726            self.outstanding_non_ack_eliciting = 0;
727        } else {
728            self.outstanding_non_ack_eliciting += 1;
729        }
730
731        if in_flight {
732            self.pacer.on_packet_sent(
733                time_sent,
734                self.bytes_in_flight.get(),
735                pkt_num,
736                sent_bytes,
737                pkt.has_data,
738                &self.rtt_stats,
739            );
740
741            self.bytes_in_flight.add(sent_bytes, now);
742            epoch.pkts_in_flight += 1;
743            self.set_loss_detection_timer(handshake_status, time_sent);
744        }
745
746        self.bytes_sent += sent_bytes;
747
748        trace!("{trace_id} {self:?}");
749    }
750
751    fn get_packet_send_time(&self, now: Instant) -> Instant {
752        self.pacer.get_next_release_time().time(now).unwrap_or(now)
753    }
754
755    // `peer_sent_ack_ranges` should not be used without validation.
756    fn on_ack_received(
757        &mut self, peer_sent_ack_ranges: &RangeSet, ack_delay: u64,
758        epoch: packet::Epoch, handshake_status: HandshakeStatus, now: Instant,
759        skip_pn: Option<u64>, trace_id: &str,
760    ) -> Result<OnAckReceivedOutcome> {
761        let prior_in_flight = self.bytes_in_flight.get();
762
763        let AckedDetectionResult {
764            acked_bytes,
765            spurious_losses,
766            spurious_pkt_thresh,
767            has_ack_eliciting,
768        } = self.epochs[epoch].detect_and_remove_acked_packets(
769            peer_sent_ack_ranges,
770            &mut self.newly_acked,
771            skip_pn,
772            trace_id,
773        )?;
774
775        self.lost_spurious_count += spurious_losses;
776        if let Some(thresh) = spurious_pkt_thresh {
777            self.loss_thresh.on_spurious_loss(thresh);
778        }
779
780        if self.newly_acked.is_empty() {
781            return Ok(OnAckReceivedOutcome {
782                acked_bytes,
783                spurious_losses,
784                ..Default::default()
785            });
786        }
787
788        self.bytes_in_flight.saturating_subtract(acked_bytes, now);
789
790        let largest_newly_acked = self.newly_acked.last().unwrap();
791
792        // Update `largest_acked_packet` based on the validated `newly_acked`
793        // value.
794        let largest_acked_pkt_num = self.epochs[epoch]
795            .largest_acked_packet
796            .unwrap_or(0)
797            .max(largest_newly_acked.pkt_num);
798        self.epochs[epoch].largest_acked_packet = Some(largest_acked_pkt_num);
799
800        // Check if largest packet is newly acked.
801        let update_rtt = largest_newly_acked.pkt_num == largest_acked_pkt_num &&
802            has_ack_eliciting;
803        if update_rtt {
804            let latest_rtt = now - largest_newly_acked.time_sent;
805            self.rtt_stats.update_rtt(
806                latest_rtt,
807                Duration::from_micros(ack_delay),
808                now,
809                handshake_status.completed,
810            );
811        }
812
813        let (lost_bytes, lost_packets) =
814            self.detect_and_remove_lost_packets(epoch, now);
815
816        self.pacer.on_congestion_event(
817            update_rtt,
818            prior_in_flight,
819            self.bytes_in_flight.get(),
820            now,
821            &self.newly_acked,
822            &self.lost_reuse,
823            self.epochs[epoch].least_unacked(),
824            &self.rtt_stats,
825            &mut self.recovery_stats,
826        );
827
828        self.pto_count = 0;
829        self.lost_count += lost_packets;
830
831        self.set_loss_detection_timer(handshake_status, now);
832
833        trace!("{trace_id} {self:?}");
834
835        Ok(OnAckReceivedOutcome {
836            lost_packets,
837            lost_bytes,
838            acked_bytes,
839            spurious_losses,
840        })
841    }
842
843    fn on_loss_detection_timeout(
844        &mut self, handshake_status: HandshakeStatus, now: Instant,
845        trace_id: &str,
846    ) -> OnLossDetectionTimeoutOutcome {
847        let (earliest_loss_time, epoch) = self.loss_time_and_space();
848
849        if earliest_loss_time.is_some() {
850            let prior_in_flight = self.bytes_in_flight.get();
851
852            let (lost_bytes, lost_packets) =
853                self.detect_and_remove_lost_packets(epoch, now);
854
855            self.pacer.on_congestion_event(
856                false,
857                prior_in_flight,
858                self.bytes_in_flight.get(),
859                now,
860                &[],
861                &self.lost_reuse,
862                self.epochs[epoch].least_unacked(),
863                &self.rtt_stats,
864                &mut self.recovery_stats,
865            );
866
867            self.lost_count += lost_packets;
868
869            self.set_loss_detection_timer(handshake_status, now);
870
871            trace!("{trace_id} {self:?}");
872            return OnLossDetectionTimeoutOutcome {
873                lost_packets,
874                lost_bytes,
875            };
876        }
877
878        let epoch = if self.bytes_in_flight.get() > 0 {
879            // Send new data if available, else retransmit old data. If neither
880            // is available, send a single PING frame.
881            let (_, e) = self.pto_time_and_space(handshake_status, now);
882
883            e
884        } else {
885            // Client sends an anti-deadlock packet: Initial is padded to earn
886            // more anti-amplification credit, a Handshake packet proves address
887            // ownership.
888            if handshake_status.has_handshake_keys {
889                packet::Epoch::Handshake
890            } else {
891                packet::Epoch::Initial
892            }
893        };
894
895        self.pto_count += 1;
896
897        let epoch = &mut self.epochs[epoch];
898
899        epoch.loss_probes = MAX_PTO_PROBES_COUNT.min(self.pto_count as usize);
900
901        // Skip packets that have already been acked or lost, and packets
902        // that don't contain either CRYPTO or STREAM frames and only return as
903        // many packets as the number of probe packets that will be sent.
904        let unacked_frames = epoch
905            .sent_packets
906            .iter_mut()
907            .filter_map(|p| {
908                if let SentStatus::Sent {
909                    has_data: true,
910                    frames,
911                    ..
912                } = &p.status
913                {
914                    Some(frames)
915                } else {
916                    None
917                }
918            })
919            .take(epoch.loss_probes)
920            .flatten()
921            .filter(|f| !matches!(f, frame::Frame::DatagramHeader { .. }));
922
923        // Retransmit the frames from the oldest sent packets on PTO. However
924        // the packets are not actually declared lost (so there is no effect to
925        // congestion control), we just reschedule the data they carried.
926        //
927        // This will also trigger sending an ACK and retransmitting frames like
928        // HANDSHAKE_DONE and MAX_DATA / MAX_STREAM_DATA as well, in addition
929        // to CRYPTO and STREAM, if the original packet carried them.
930        epoch.lost_frames.extend(unacked_frames.cloned());
931
932        self.pacer
933            .on_retransmission_timeout(!epoch.lost_frames.is_empty());
934
935        self.set_loss_detection_timer(handshake_status, now);
936
937        trace!("{trace_id} {self:?}");
938        OnLossDetectionTimeoutOutcome {
939            lost_packets: 0,
940            lost_bytes: 0,
941        }
942    }
943
944    fn on_pkt_num_space_discarded(
945        &mut self, epoch: packet::Epoch, handshake_status: HandshakeStatus,
946        now: Instant,
947    ) {
948        let epoch = &mut self.epochs[epoch];
949        self.bytes_in_flight
950            .saturating_subtract(epoch.discard(&mut self.pacer), now);
951        self.set_loss_detection_timer(handshake_status, now);
952    }
953
954    fn on_path_change(
955        &mut self, epoch: packet::Epoch, now: Instant, _trace_id: &str,
956    ) -> (usize, usize) {
957        let (lost_bytes, lost_packets) =
958            self.detect_and_remove_lost_packets(epoch, now);
959
960        (lost_packets, lost_bytes)
961    }
962
963    fn loss_detection_timer(&self) -> Option<Instant> {
964        self.loss_timer.time
965    }
966
967    fn cwnd(&self) -> usize {
968        self.pacer.get_congestion_window()
969    }
970
971    fn cwnd_available(&self) -> usize {
972        // Ignore cwnd when sending probe packets.
973        if self.epochs.iter().any(|e| e.loss_probes > 0) {
974            return usize::MAX;
975        }
976
977        self.cwnd().saturating_sub(self.bytes_in_flight.get())
978    }
979
980    fn rtt(&self) -> Duration {
981        self.rtt_stats.rtt()
982    }
983
984    fn min_rtt(&self) -> Option<Duration> {
985        self.rtt_stats.min_rtt()
986    }
987
988    fn max_rtt(&self) -> Option<Duration> {
989        self.rtt_stats.max_rtt()
990    }
991
992    fn rttvar(&self) -> Duration {
993        self.rtt_stats.rttvar()
994    }
995
996    fn pto(&self) -> Duration {
997        let r = &self.rtt_stats;
998        r.rtt() + (r.rttvar() * 4).max(GRANULARITY)
999    }
1000
1001    /// The most recent data delivery rate estimate.
1002    fn delivery_rate(&self) -> Bandwidth {
1003        self.pacer.bandwidth_estimate(&self.rtt_stats)
1004    }
1005
1006    fn max_bandwidth(&self) -> Option<Bandwidth> {
1007        Some(self.pacer.max_bandwidth())
1008    }
1009
1010    /// Statistics from when a CCA first exited the startup phase.
1011    fn startup_exit(&self) -> Option<StartupExit> {
1012        self.recovery_stats.startup_exit
1013    }
1014
1015    fn max_datagram_size(&self) -> usize {
1016        self.max_datagram_size
1017    }
1018
1019    fn pmtud_update_max_datagram_size(&mut self, new_max_datagram_size: usize) {
1020        self.max_datagram_size = new_max_datagram_size;
1021        self.pacer.update_mss(self.max_datagram_size);
1022    }
1023
1024    fn update_max_datagram_size(&mut self, new_max_datagram_size: usize) {
1025        self.pmtud_update_max_datagram_size(
1026            self.max_datagram_size.min(new_max_datagram_size),
1027        )
1028    }
1029
1030    // FIXME only used by gcongestion
1031    fn on_app_limited(&mut self) {
1032        self.pacer.on_app_limited(self.bytes_in_flight.get())
1033    }
1034
1035    #[cfg(test)]
1036    fn sent_packets_len(&self, epoch: packet::Epoch) -> usize {
1037        self.epochs[epoch].sent_packets.len()
1038    }
1039
1040    #[cfg(test)]
1041    fn in_flight_count(&self, epoch: packet::Epoch) -> usize {
1042        self.epochs[epoch].pkts_in_flight
1043    }
1044
1045    #[cfg(test)]
1046    fn bytes_in_flight(&self) -> usize {
1047        self.bytes_in_flight.get()
1048    }
1049
1050    fn bytes_in_flight_duration(&self) -> Duration {
1051        self.bytes_in_flight.get_duration()
1052    }
1053
1054    #[cfg(test)]
1055    fn pacing_rate(&self) -> u64 {
1056        self.pacer
1057            .pacing_rate(self.bytes_in_flight.get(), &self.rtt_stats)
1058            .to_bytes_per_period(Duration::from_secs(1))
1059    }
1060
1061    #[cfg(test)]
1062    fn pto_count(&self) -> u32 {
1063        self.pto_count
1064    }
1065
1066    #[cfg(test)]
1067    fn pkt_thresh(&self) -> Option<u64> {
1068        self.loss_thresh.pkt_thresh()
1069    }
1070
1071    #[cfg(test)]
1072    fn time_thresh(&self) -> f64 {
1073        self.loss_thresh.time_thresh()
1074    }
1075
1076    #[cfg(test)]
1077    fn lost_spurious_count(&self) -> usize {
1078        self.lost_spurious_count
1079    }
1080
1081    #[cfg(test)]
1082    fn detect_lost_packets_for_test(
1083        &mut self, epoch: packet::Epoch, now: Instant,
1084    ) -> (usize, usize) {
1085        let ret = self.detect_and_remove_lost_packets(epoch, now);
1086        self.epochs[epoch].drain_acked_and_lost_packets();
1087        ret
1088    }
1089
1090    #[cfg(test)]
1091    fn largest_sent_pkt_num_on_path(&self, epoch: packet::Epoch) -> Option<u64> {
1092        self.epochs[epoch].test_largest_sent_pkt_num_on_path
1093    }
1094
1095    #[cfg(test)]
1096    fn app_limited(&self) -> bool {
1097        self.pacer.is_app_limited(self.bytes_in_flight.get())
1098    }
1099
1100    // FIXME only used by congestion
1101    fn update_app_limited(&mut self, _v: bool) {
1102        // TODO
1103    }
1104
1105    // FIXME only used by congestion
1106    fn delivery_rate_update_app_limited(&mut self, _v: bool) {
1107        // TODO
1108    }
1109
1110    fn update_max_ack_delay(&mut self, max_ack_delay: Duration) {
1111        self.rtt_stats.max_ack_delay = max_ack_delay;
1112    }
1113
1114    fn get_next_release_time(&self) -> ReleaseDecision {
1115        self.pacer.get_next_release_time()
1116    }
1117
1118    fn gcongestion_enabled(&self) -> bool {
1119        true
1120    }
1121
1122    #[cfg(feature = "qlog")]
1123    fn state_str(&self, _now: Instant) -> &'static str {
1124        self.pacer.state_str()
1125    }
1126
1127    #[cfg(feature = "qlog")]
1128    fn get_updated_qlog_event_data(&mut self) -> Option<EventData> {
1129        let qlog_metrics = QlogMetrics {
1130            min_rtt: *self.rtt_stats.min_rtt,
1131            smoothed_rtt: self.rtt(),
1132            latest_rtt: self.rtt_stats.latest_rtt(),
1133            rttvar: self.rtt_stats.rttvar(),
1134            cwnd: self.cwnd() as u64,
1135            bytes_in_flight: self.bytes_in_flight.get() as u64,
1136            ssthresh: self.pacer.ssthresh(),
1137            pacing_rate: self.delivery_rate().to_bytes_per_second(),
1138        };
1139
1140        self.qlog_metrics.maybe_update(qlog_metrics)
1141    }
1142
1143    #[cfg(feature = "qlog")]
1144    fn get_updated_qlog_cc_state(
1145        &mut self, now: Instant,
1146    ) -> Option<&'static str> {
1147        let cc_state = self.state_str(now);
1148        if cc_state != self.qlog_prev_cc_state {
1149            self.qlog_prev_cc_state = cc_state;
1150            Some(cc_state)
1151        } else {
1152            None
1153        }
1154    }
1155
1156    fn send_quantum(&self) -> usize {
1157        let pacing_rate = self
1158            .pacer
1159            .pacing_rate(self.bytes_in_flight.get(), &self.rtt_stats);
1160
1161        let floor = if pacing_rate < Bandwidth::from_kbits_per_second(1200) {
1162            self.max_datagram_size
1163        } else {
1164            2 * self.max_datagram_size
1165        };
1166
1167        pacing_rate
1168            .to_bytes_per_period(ReleaseDecision::EQUAL_THRESHOLD)
1169            .min(64 * 1024)
1170            .max(floor as u64) as usize
1171    }
1172}
1173
1174impl std::fmt::Debug for GRecovery {
1175    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
1176        write!(f, "timer={:?} ", self.loss_detection_timer())?;
1177        write!(f, "rtt_stats={:?} ", self.rtt_stats)?;
1178        write!(f, "bytes_in_flight={} ", self.bytes_in_flight.get())?;
1179        write!(f, "{:?} ", self.pacer)?;
1180        Ok(())
1181    }
1182}
1183
1184#[cfg(test)]
1185mod tests {
1186    use super::*;
1187    use crate::Config;
1188
1189    #[test]
1190    fn loss_threshold() {
1191        let config = Config::new(crate::PROTOCOL_VERSION).unwrap();
1192        let recovery_config = RecoveryConfig::from_config(&config);
1193        assert_eq!(recovery_config.enable_relaxed_loss_threshold, false);
1194
1195        let mut loss_thresh = LossThreshold::new(&recovery_config);
1196        assert_eq!(loss_thresh.time_thresh_overhead, None);
1197        assert_eq!(loss_thresh.pkt_thresh().unwrap(), INITIAL_PACKET_THRESHOLD);
1198        assert_eq!(loss_thresh.time_thresh(), INITIAL_TIME_THRESHOLD);
1199
1200        // First spurious loss.
1201        loss_thresh.on_spurious_loss(INITIAL_PACKET_THRESHOLD);
1202        assert_eq!(loss_thresh.pkt_thresh().unwrap(), INITIAL_PACKET_THRESHOLD);
1203        assert_eq!(loss_thresh.time_thresh(), PACKET_REORDER_TIME_THRESHOLD);
1204
1205        // Packet gaps < INITIAL_PACKET_THRESHOLD will NOT change packet
1206        // threshold.
1207        for packet_gap in 0..INITIAL_PACKET_THRESHOLD {
1208            loss_thresh.on_spurious_loss(packet_gap);
1209
1210            // Packet threshold only increases once the packet gap increases.
1211            assert_eq!(
1212                loss_thresh.pkt_thresh().unwrap(),
1213                INITIAL_PACKET_THRESHOLD
1214            );
1215            assert_eq!(loss_thresh.time_thresh(), PACKET_REORDER_TIME_THRESHOLD);
1216        }
1217
1218        // Subsequent spurious loss with packet_gaps > INITIAL_PACKET_THRESHOLD.
1219        // Test values much larger than MAX_PACKET_THRESHOLD, i.e.
1220        // `MAX_PACKET_THRESHOLD * 2`
1221        for packet_gap in INITIAL_PACKET_THRESHOLD + 1..MAX_PACKET_THRESHOLD * 2 {
1222            loss_thresh.on_spurious_loss(packet_gap);
1223
1224            // Packet threshold is equal to packet gap beyond
1225            // INITIAL_PACKET_THRESHOLD, but capped
1226            // at MAX_PACKET_THRESHOLD.
1227            let new_packet_threshold = if packet_gap < MAX_PACKET_THRESHOLD {
1228                packet_gap
1229            } else {
1230                MAX_PACKET_THRESHOLD
1231            };
1232            assert_eq!(loss_thresh.pkt_thresh().unwrap(), new_packet_threshold);
1233            assert_eq!(loss_thresh.time_thresh(), PACKET_REORDER_TIME_THRESHOLD);
1234        }
1235        // Packet threshold is capped at MAX_PACKET_THRESHOLD
1236        assert_eq!(loss_thresh.pkt_thresh().unwrap(), MAX_PACKET_THRESHOLD);
1237        assert_eq!(loss_thresh.time_thresh(), PACKET_REORDER_TIME_THRESHOLD);
1238
1239        // Packet threshold is monotonically increasing
1240        loss_thresh.on_spurious_loss(INITIAL_PACKET_THRESHOLD);
1241        assert_eq!(loss_thresh.pkt_thresh().unwrap(), MAX_PACKET_THRESHOLD);
1242        assert_eq!(loss_thresh.time_thresh(), PACKET_REORDER_TIME_THRESHOLD);
1243    }
1244
1245    #[test]
1246    fn relaxed_loss_threshold() {
1247        // The max time threshold when operating in relaxed loss mode.
1248        const MAX_TIME_THRESHOLD: f64 = 2.0;
1249
1250        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
1251        config.set_enable_relaxed_loss_threshold(true);
1252        let recovery_config = RecoveryConfig::from_config(&config);
1253        assert!(recovery_config.enable_relaxed_loss_threshold);
1254
1255        let mut loss_thresh = LossThreshold::new(&recovery_config);
1256        assert_eq!(
1257            loss_thresh.time_thresh_overhead,
1258            Some(INITIAL_TIME_THRESHOLD_OVERHEAD)
1259        );
1260        assert_eq!(loss_thresh.pkt_thresh().unwrap(), INITIAL_PACKET_THRESHOLD);
1261        assert_eq!(loss_thresh.time_thresh(), INITIAL_TIME_THRESHOLD);
1262
1263        // First spurious loss.
1264        loss_thresh.on_spurious_loss(INITIAL_PACKET_THRESHOLD);
1265        assert_eq!(loss_thresh.pkt_thresh(), None);
1266        assert_eq!(loss_thresh.time_thresh(), INITIAL_TIME_THRESHOLD);
1267
1268        // Subsequent spurious loss.
1269        for subsequent_loss_count in 1..100 {
1270            // Double the overhead until it caps at `2.0`.
1271            //
1272            // It takes `3` rounds of doubling for INITIAL_TIME_THRESHOLD_OVERHEAD
1273            // to equal `1.0`.
1274            let new_time_threshold = if subsequent_loss_count <= 3 {
1275                1.0 + INITIAL_TIME_THRESHOLD_OVERHEAD *
1276                    2_f64.powi(subsequent_loss_count as i32)
1277            } else {
1278                2.0
1279            };
1280
1281            loss_thresh.on_spurious_loss(subsequent_loss_count);
1282            assert_eq!(loss_thresh.pkt_thresh(), None);
1283            assert_eq!(loss_thresh.time_thresh(), new_time_threshold);
1284        }
1285        // Time threshold is capped at 2.0.
1286        assert_eq!(loss_thresh.pkt_thresh(), None);
1287        assert_eq!(loss_thresh.time_thresh(), MAX_TIME_THRESHOLD);
1288    }
1289}