Skip to main content

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: VecDeque<frame::Frame>,
108    lost_frames: VecDeque<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    time_sent_set_to_now: bool,
451
452    #[cfg(feature = "qlog")]
453    qlog_metrics: QlogMetrics,
454
455    #[cfg(feature = "qlog")]
456    qlog_prev_cc_state: &'static str,
457
458    /// How many non-ack-eliciting packets have been sent.
459    outstanding_non_ack_eliciting: usize,
460
461    /// A resusable list of acks.
462    newly_acked: Vec<Acked>,
463
464    /// A [`Vec`] that can be reused for calls of
465    /// [`Self::detect_and_remove_lost_packets`] to avoid allocations
466    lost_reuse: Vec<Lost>,
467
468    pacer: Pacer,
469}
470
471impl GRecovery {
472    pub fn new(recovery_config: &RecoveryConfig) -> Option<Self> {
473        let cc = match recovery_config.cc_algorithm {
474            CongestionControlAlgorithm::Bbr2Gcongestion => BBRv2::new(
475                recovery_config.initial_congestion_window_packets,
476                MAX_WINDOW_PACKETS,
477                recovery_config.max_send_udp_payload_size,
478                recovery_config.initial_rtt,
479                recovery_config.custom_bbr_params.as_ref(),
480            ),
481            _ => return None,
482        };
483
484        Some(Self {
485            epochs: Default::default(),
486            rtt_stats: RttStats::new(
487                recovery_config.initial_rtt,
488                recovery_config.max_ack_delay,
489            ),
490            recovery_stats: RecoveryStats::default(),
491            loss_timer: Default::default(),
492            pto_count: 0,
493
494            lost_count: 0,
495            lost_spurious_count: 0,
496
497            loss_thresh: LossThreshold::new(recovery_config),
498            bytes_in_flight: Default::default(),
499            bytes_sent: 0,
500            bytes_lost: 0,
501
502            max_datagram_size: recovery_config.max_send_udp_payload_size,
503            time_sent_set_to_now: cc.time_sent_set_to_now(),
504
505            #[cfg(feature = "qlog")]
506            qlog_metrics: QlogMetrics::default(),
507
508            #[cfg(feature = "qlog")]
509            qlog_prev_cc_state: "",
510
511            outstanding_non_ack_eliciting: 0,
512
513            pacer: Pacer::new(
514                recovery_config.pacing,
515                cc,
516                recovery_config
517                    .max_pacing_rate
518                    .map(Bandwidth::from_mbits_per_second),
519            ),
520
521            newly_acked: Vec::new(),
522            lost_reuse: Vec::new(),
523        })
524    }
525
526    fn detect_and_remove_lost_packets(
527        &mut self, epoch: packet::Epoch, now: Instant,
528    ) -> (usize, usize) {
529        let loss_delay =
530            self.rtt_stats.loss_delay(self.loss_thresh.time_thresh());
531        let lost = &mut self.lost_reuse;
532
533        let LossDetectionResult {
534            lost_bytes,
535            lost_packets,
536            pmtud_lost_bytes,
537            pmtud_lost_packets,
538        } = self.epochs[epoch].detect_and_remove_lost_packets(
539            loss_delay,
540            self.loss_thresh.pkt_thresh(),
541            now,
542            lost,
543        );
544
545        self.bytes_in_flight
546            .saturating_subtract(lost_bytes + pmtud_lost_bytes, now);
547
548        for pkt in pmtud_lost_packets {
549            self.pacer.on_packet_neutered(pkt);
550        }
551
552        (lost_bytes, lost_packets)
553    }
554
555    fn loss_time_and_space(&self) -> (Option<Instant>, packet::Epoch) {
556        let mut epoch = packet::Epoch::Initial;
557        let mut time = self.epochs[epoch].loss_time;
558
559        // Iterate over all packet number spaces starting from Handshake.
560        for e in [packet::Epoch::Handshake, packet::Epoch::Application] {
561            let new_time = self.epochs[e].loss_time;
562            if time.is_none() || new_time < time {
563                time = new_time;
564                epoch = e;
565            }
566        }
567
568        (time, epoch)
569    }
570
571    fn pto_time_and_space(
572        &self, handshake_status: HandshakeStatus, now: Instant,
573    ) -> (Option<Instant>, packet::Epoch) {
574        let mut duration = self.pto() * (1 << self.pto_count);
575
576        // Arm PTO from now when there are no inflight packets.
577        if self.bytes_in_flight.is_zero() {
578            if handshake_status.has_handshake_keys {
579                return (Some(now + duration), packet::Epoch::Handshake);
580            } else {
581                return (Some(now + duration), packet::Epoch::Initial);
582            }
583        }
584
585        let mut pto_timeout = None;
586        let mut pto_space = packet::Epoch::Initial;
587
588        // Iterate over all packet number spaces.
589        for &e in packet::Epoch::epochs(
590            packet::Epoch::Initial..=packet::Epoch::Application,
591        ) {
592            if self.epochs[e].pkts_in_flight == 0 {
593                continue;
594            }
595
596            if e == packet::Epoch::Application {
597                // Skip Application Data until handshake completes.
598                if !handshake_status.completed {
599                    return (pto_timeout, pto_space);
600                }
601
602                // Include max_ack_delay and backoff for Application Data.
603                duration +=
604                    self.rtt_stats.max_ack_delay * 2_u32.pow(self.pto_count);
605            }
606
607            let new_time = self.epochs[e]
608                .time_of_last_ack_eliciting_packet
609                .map(|t| t + duration);
610
611            if pto_timeout.is_none() || new_time < pto_timeout {
612                pto_timeout = new_time;
613                pto_space = e;
614            }
615        }
616
617        (pto_timeout, pto_space)
618    }
619
620    fn set_loss_detection_timer(
621        &mut self, handshake_status: HandshakeStatus, now: Instant,
622    ) {
623        if let (Some(earliest_loss_time), _) = self.loss_time_and_space() {
624            // Time threshold loss detection.
625            self.loss_timer.update(earliest_loss_time);
626            return;
627        }
628
629        if self.bytes_in_flight.is_zero() &&
630            handshake_status.peer_verified_address
631        {
632            self.loss_timer.clear();
633            return;
634        }
635
636        // PTO timer.
637        if let (Some(timeout), _) = self.pto_time_and_space(handshake_status, now)
638        {
639            self.loss_timer.update(timeout);
640        }
641    }
642}
643
644impl RecoveryOps for GRecovery {
645    fn lost_count(&self) -> usize {
646        self.lost_count
647    }
648
649    fn bytes_lost(&self) -> u64 {
650        self.bytes_lost
651    }
652
653    fn should_elicit_ack(&self, epoch: packet::Epoch) -> bool {
654        self.epochs[epoch].loss_probes > 0 ||
655            self.outstanding_non_ack_eliciting >=
656                MAX_OUTSTANDING_NON_ACK_ELICITING
657    }
658
659    fn next_acked_frame(&mut self, epoch: packet::Epoch) -> Option<frame::Frame> {
660        self.epochs[epoch].acked_frames.pop_front()
661    }
662
663    fn next_lost_frame(&mut self, epoch: packet::Epoch) -> Option<frame::Frame> {
664        self.epochs[epoch].lost_frames.pop_front()
665    }
666
667    fn get_largest_acked_on_epoch(&self, epoch: packet::Epoch) -> Option<u64> {
668        self.epochs[epoch].largest_acked_packet
669    }
670
671    fn has_lost_frames(&self, epoch: packet::Epoch) -> bool {
672        !self.epochs[epoch].lost_frames.is_empty()
673    }
674
675    fn loss_probes(&self, epoch: packet::Epoch) -> usize {
676        self.epochs[epoch].loss_probes
677    }
678
679    #[cfg(test)]
680    fn inc_loss_probes(&mut self, epoch: packet::Epoch) {
681        self.epochs[epoch].loss_probes += 1;
682    }
683
684    fn ping_sent(&mut self, epoch: packet::Epoch) {
685        self.epochs[epoch].loss_probes =
686            self.epochs[epoch].loss_probes.saturating_sub(1);
687    }
688
689    fn on_packet_sent(
690        &mut self, pkt: Sent, epoch: packet::Epoch,
691        handshake_status: HandshakeStatus, now: Instant, trace_id: &str,
692    ) {
693        let time_sent = if self.time_sent_set_to_now {
694            now
695        } else {
696            self.get_next_release_time().time(now).unwrap_or(now)
697        };
698
699        let epoch = &mut self.epochs[epoch];
700
701        let ack_eliciting = pkt.ack_eliciting;
702        let in_flight = pkt.in_flight;
703        let is_pmtud_probe = pkt.is_pmtud_probe;
704        let pkt_num = pkt.pkt_num;
705        let sent_bytes = pkt.size;
706
707        if let Some(SentPacket { pkt_num, .. }) = epoch.sent_packets.back() {
708            assert!(*pkt_num < pkt.pkt_num, "Packet numbers must increase");
709        }
710
711        let status = SentStatus::Sent {
712            time_sent,
713            ack_eliciting,
714            in_flight,
715            is_pmtud_probe,
716            has_data: pkt.has_data,
717            sent_bytes,
718            frames: pkt.frames,
719        };
720
721        #[cfg(test)]
722        {
723            epoch.test_largest_sent_pkt_num_on_path = epoch
724                .test_largest_sent_pkt_num_on_path
725                .max(Some(pkt.pkt_num));
726        }
727
728        epoch.sent_packets.push_back(SentPacket { pkt_num, status });
729
730        if ack_eliciting {
731            epoch.time_of_last_ack_eliciting_packet = Some(time_sent);
732            self.outstanding_non_ack_eliciting = 0;
733        } else {
734            self.outstanding_non_ack_eliciting += 1;
735        }
736
737        if in_flight {
738            self.pacer.on_packet_sent(
739                time_sent,
740                self.bytes_in_flight.get(),
741                pkt_num,
742                sent_bytes,
743                pkt.has_data,
744                &self.rtt_stats,
745            );
746
747            self.bytes_in_flight.add(sent_bytes, now);
748            epoch.pkts_in_flight += 1;
749            self.set_loss_detection_timer(handshake_status, time_sent);
750        }
751
752        self.bytes_sent += sent_bytes;
753
754        trace!("{trace_id} {self:?}");
755    }
756
757    fn get_packet_send_time(&self, now: Instant) -> Instant {
758        self.pacer.get_next_release_time().time(now).unwrap_or(now)
759    }
760
761    // `peer_sent_ack_ranges` should not be used without validation.
762    fn on_ack_received(
763        &mut self, peer_sent_ack_ranges: &RangeSet, ack_delay: u64,
764        epoch: packet::Epoch, handshake_status: HandshakeStatus, now: Instant,
765        skip_pn: Option<u64>, trace_id: &str,
766    ) -> Result<OnAckReceivedOutcome> {
767        let prior_in_flight = self.bytes_in_flight.get();
768
769        let AckedDetectionResult {
770            acked_bytes,
771            spurious_losses,
772            spurious_pkt_thresh,
773            has_ack_eliciting,
774        } = self.epochs[epoch].detect_and_remove_acked_packets(
775            peer_sent_ack_ranges,
776            &mut self.newly_acked,
777            skip_pn,
778            trace_id,
779        )?;
780
781        self.lost_spurious_count += spurious_losses;
782        if let Some(thresh) = spurious_pkt_thresh {
783            self.loss_thresh.on_spurious_loss(thresh);
784        }
785
786        if self.newly_acked.is_empty() {
787            return Ok(OnAckReceivedOutcome {
788                acked_bytes,
789                spurious_losses,
790                ..Default::default()
791            });
792        }
793
794        self.bytes_in_flight.saturating_subtract(acked_bytes, now);
795
796        let largest_newly_acked = self.newly_acked.last().unwrap();
797
798        // Update `largest_acked_packet` based on the validated `newly_acked`
799        // value.
800        let largest_acked_pkt_num = self.epochs[epoch]
801            .largest_acked_packet
802            .unwrap_or(0)
803            .max(largest_newly_acked.pkt_num);
804        self.epochs[epoch].largest_acked_packet = Some(largest_acked_pkt_num);
805
806        // Check if largest packet is newly acked.
807        let update_rtt = largest_newly_acked.pkt_num == largest_acked_pkt_num &&
808            has_ack_eliciting;
809        if update_rtt {
810            let latest_rtt = now - largest_newly_acked.time_sent;
811            self.rtt_stats.update_rtt(
812                latest_rtt,
813                Duration::from_micros(ack_delay),
814                now,
815                handshake_status.completed,
816            );
817        }
818
819        let (lost_bytes, lost_packets) =
820            self.detect_and_remove_lost_packets(epoch, now);
821
822        self.pacer.on_congestion_event(
823            update_rtt,
824            prior_in_flight,
825            self.bytes_in_flight.get(),
826            now,
827            &self.newly_acked,
828            &self.lost_reuse,
829            self.epochs[epoch].least_unacked(),
830            &self.rtt_stats,
831            &mut self.recovery_stats,
832        );
833
834        self.pto_count = 0;
835        self.lost_count += lost_packets;
836
837        self.set_loss_detection_timer(handshake_status, now);
838
839        trace!("{trace_id} {self:?}");
840
841        Ok(OnAckReceivedOutcome {
842            lost_packets,
843            lost_bytes,
844            acked_bytes,
845            spurious_losses,
846        })
847    }
848
849    fn on_loss_detection_timeout(
850        &mut self, handshake_status: HandshakeStatus, now: Instant,
851        trace_id: &str,
852    ) -> OnLossDetectionTimeoutOutcome {
853        let (earliest_loss_time, epoch) = self.loss_time_and_space();
854
855        if earliest_loss_time.is_some() {
856            let prior_in_flight = self.bytes_in_flight.get();
857
858            let (lost_bytes, lost_packets) =
859                self.detect_and_remove_lost_packets(epoch, now);
860
861            self.pacer.on_congestion_event(
862                false,
863                prior_in_flight,
864                self.bytes_in_flight.get(),
865                now,
866                &[],
867                &self.lost_reuse,
868                self.epochs[epoch].least_unacked(),
869                &self.rtt_stats,
870                &mut self.recovery_stats,
871            );
872
873            self.lost_count += lost_packets;
874
875            self.set_loss_detection_timer(handshake_status, now);
876
877            trace!("{trace_id} {self:?}");
878            return OnLossDetectionTimeoutOutcome {
879                lost_packets,
880                lost_bytes,
881            };
882        }
883
884        let epoch = if self.bytes_in_flight.get() > 0 {
885            // Send new data if available, else retransmit old data. If neither
886            // is available, send a single PING frame.
887            let (_, e) = self.pto_time_and_space(handshake_status, now);
888
889            e
890        } else {
891            // Client sends an anti-deadlock packet: Initial is padded to earn
892            // more anti-amplification credit, a Handshake packet proves address
893            // ownership.
894            if handshake_status.has_handshake_keys {
895                packet::Epoch::Handshake
896            } else {
897                packet::Epoch::Initial
898            }
899        };
900
901        self.pto_count += 1;
902
903        let epoch = &mut self.epochs[epoch];
904
905        epoch.loss_probes = MAX_PTO_PROBES_COUNT.min(self.pto_count as usize);
906
907        // Skip packets that have already been acked or lost, and packets
908        // that don't contain either CRYPTO or STREAM frames and only return as
909        // many packets as the number of probe packets that will be sent.
910        let unacked_frames = epoch
911            .sent_packets
912            .iter_mut()
913            .filter_map(|p| {
914                if let SentStatus::Sent {
915                    has_data: true,
916                    frames,
917                    ..
918                } = &p.status
919                {
920                    Some(frames)
921                } else {
922                    None
923                }
924            })
925            .take(epoch.loss_probes)
926            .flatten()
927            .filter(|f| !matches!(f, frame::Frame::DatagramHeader { .. }));
928
929        // Retransmit the frames from the oldest sent packets on PTO. However
930        // the packets are not actually declared lost (so there is no effect to
931        // congestion control), we just reschedule the data they carried.
932        //
933        // This will also trigger sending an ACK and retransmitting frames like
934        // HANDSHAKE_DONE and MAX_DATA / MAX_STREAM_DATA as well, in addition
935        // to CRYPTO and STREAM, if the original packet carried them.
936        epoch.lost_frames.extend(unacked_frames.cloned());
937
938        self.pacer
939            .on_retransmission_timeout(!epoch.lost_frames.is_empty());
940
941        self.set_loss_detection_timer(handshake_status, now);
942
943        trace!("{trace_id} {self:?}");
944        OnLossDetectionTimeoutOutcome {
945            lost_packets: 0,
946            lost_bytes: 0,
947        }
948    }
949
950    fn on_pkt_num_space_discarded(
951        &mut self, epoch: packet::Epoch, handshake_status: HandshakeStatus,
952        now: Instant,
953    ) {
954        let epoch = &mut self.epochs[epoch];
955        self.bytes_in_flight
956            .saturating_subtract(epoch.discard(&mut self.pacer), now);
957        self.set_loss_detection_timer(handshake_status, now);
958    }
959
960    fn on_path_change(
961        &mut self, epoch: packet::Epoch, now: Instant, _trace_id: &str,
962    ) -> (usize, usize) {
963        let (lost_bytes, lost_packets) =
964            self.detect_and_remove_lost_packets(epoch, now);
965
966        (lost_packets, lost_bytes)
967    }
968
969    fn loss_detection_timer(&self) -> Option<Instant> {
970        self.loss_timer.time
971    }
972
973    fn cwnd(&self) -> usize {
974        self.pacer.get_congestion_window()
975    }
976
977    fn cwnd_available(&self) -> usize {
978        // Ignore cwnd when sending probe packets.
979        if self.epochs.iter().any(|e| e.loss_probes > 0) {
980            return usize::MAX;
981        }
982
983        self.cwnd().saturating_sub(self.bytes_in_flight.get())
984    }
985
986    fn rtt(&self) -> Duration {
987        self.rtt_stats.rtt()
988    }
989
990    fn min_rtt(&self) -> Option<Duration> {
991        self.rtt_stats.min_rtt()
992    }
993
994    fn max_rtt(&self) -> Option<Duration> {
995        self.rtt_stats.max_rtt()
996    }
997
998    fn rttvar(&self) -> Duration {
999        self.rtt_stats.rttvar()
1000    }
1001
1002    fn pto(&self) -> Duration {
1003        let r = &self.rtt_stats;
1004        r.rtt() + (r.rttvar() * 4).max(GRANULARITY)
1005    }
1006
1007    /// The most recent data delivery rate estimate.
1008    fn delivery_rate(&self) -> Bandwidth {
1009        self.pacer.bandwidth_estimate(&self.rtt_stats)
1010    }
1011
1012    fn max_bandwidth(&self) -> Option<Bandwidth> {
1013        Some(self.pacer.max_bandwidth())
1014    }
1015
1016    /// Statistics from when a CCA first exited the startup phase.
1017    fn startup_exit(&self) -> Option<StartupExit> {
1018        self.recovery_stats.startup_exit
1019    }
1020
1021    fn max_datagram_size(&self) -> usize {
1022        self.max_datagram_size
1023    }
1024
1025    fn pmtud_update_max_datagram_size(&mut self, new_max_datagram_size: usize) {
1026        self.max_datagram_size = new_max_datagram_size;
1027        self.pacer.update_mss(self.max_datagram_size);
1028    }
1029
1030    fn update_max_datagram_size(&mut self, new_max_datagram_size: usize) {
1031        self.pmtud_update_max_datagram_size(
1032            self.max_datagram_size.min(new_max_datagram_size),
1033        )
1034    }
1035
1036    // FIXME only used by gcongestion
1037    fn on_app_limited(&mut self) {
1038        self.pacer.on_app_limited(self.bytes_in_flight.get())
1039    }
1040
1041    #[cfg(test)]
1042    fn sent_packets_len(&self, epoch: packet::Epoch) -> usize {
1043        self.epochs[epoch].sent_packets.len()
1044    }
1045
1046    #[cfg(test)]
1047    fn in_flight_count(&self, epoch: packet::Epoch) -> usize {
1048        self.epochs[epoch].pkts_in_flight
1049    }
1050
1051    fn bytes_in_flight(&self) -> usize {
1052        self.bytes_in_flight.get()
1053    }
1054
1055    fn bytes_in_flight_duration(&self) -> Duration {
1056        self.bytes_in_flight.get_duration()
1057    }
1058
1059    #[cfg(test)]
1060    fn pacing_rate(&self) -> u64 {
1061        self.pacer
1062            .pacing_rate(self.bytes_in_flight.get(), &self.rtt_stats)
1063            .to_bytes_per_period(Duration::from_secs(1))
1064    }
1065
1066    #[cfg(test)]
1067    fn pto_count(&self) -> u32 {
1068        self.pto_count
1069    }
1070
1071    #[cfg(test)]
1072    fn pkt_thresh(&self) -> Option<u64> {
1073        self.loss_thresh.pkt_thresh()
1074    }
1075
1076    #[cfg(test)]
1077    fn time_thresh(&self) -> f64 {
1078        self.loss_thresh.time_thresh()
1079    }
1080
1081    #[cfg(test)]
1082    fn lost_spurious_count(&self) -> usize {
1083        self.lost_spurious_count
1084    }
1085
1086    #[cfg(test)]
1087    fn detect_lost_packets_for_test(
1088        &mut self, epoch: packet::Epoch, now: Instant,
1089    ) -> (usize, usize) {
1090        let ret = self.detect_and_remove_lost_packets(epoch, now);
1091        self.epochs[epoch].drain_acked_and_lost_packets();
1092        ret
1093    }
1094
1095    #[cfg(test)]
1096    fn largest_sent_pkt_num_on_path(&self, epoch: packet::Epoch) -> Option<u64> {
1097        self.epochs[epoch].test_largest_sent_pkt_num_on_path
1098    }
1099
1100    #[cfg(test)]
1101    fn app_limited(&self) -> bool {
1102        self.pacer.is_app_limited(self.bytes_in_flight.get())
1103    }
1104
1105    // FIXME only used by congestion
1106    fn update_app_limited(&mut self, _v: bool) {
1107        // TODO
1108    }
1109
1110    // FIXME only used by congestion
1111    fn delivery_rate_update_app_limited(&mut self, _v: bool) {
1112        // TODO
1113    }
1114
1115    fn update_max_ack_delay(&mut self, max_ack_delay: Duration) {
1116        self.rtt_stats.max_ack_delay = max_ack_delay;
1117    }
1118
1119    fn get_next_release_time(&self) -> ReleaseDecision {
1120        self.pacer.get_next_release_time()
1121    }
1122
1123    fn gcongestion_enabled(&self) -> bool {
1124        true
1125    }
1126
1127    #[cfg(feature = "qlog")]
1128    fn state_str(&self, _now: Instant) -> &'static str {
1129        self.pacer.state_str()
1130    }
1131
1132    #[cfg(feature = "qlog")]
1133    fn get_updated_qlog_event_data(&mut self) -> Option<EventData> {
1134        let qlog_metrics = QlogMetrics {
1135            min_rtt: *self.rtt_stats.min_rtt,
1136            smoothed_rtt: self.rtt(),
1137            latest_rtt: self.rtt_stats.latest_rtt(),
1138            rttvar: self.rtt_stats.rttvar(),
1139            cwnd: self.cwnd() as u64,
1140            bytes_in_flight: self.bytes_in_flight.get() as u64,
1141            ssthresh: self.pacer.ssthresh(),
1142            pacing_rate: self.delivery_rate().to_bytes_per_second(),
1143        };
1144
1145        self.qlog_metrics.maybe_update(qlog_metrics)
1146    }
1147
1148    #[cfg(feature = "qlog")]
1149    fn get_updated_qlog_cc_state(
1150        &mut self, now: Instant,
1151    ) -> Option<&'static str> {
1152        let cc_state = self.state_str(now);
1153        if cc_state != self.qlog_prev_cc_state {
1154            self.qlog_prev_cc_state = cc_state;
1155            Some(cc_state)
1156        } else {
1157            None
1158        }
1159    }
1160
1161    fn send_quantum(&self) -> usize {
1162        let pacing_rate = self
1163            .pacer
1164            .pacing_rate(self.bytes_in_flight.get(), &self.rtt_stats);
1165
1166        let floor = if pacing_rate < Bandwidth::from_kbits_per_second(1200) {
1167            self.max_datagram_size
1168        } else {
1169            2 * self.max_datagram_size
1170        };
1171
1172        pacing_rate
1173            .to_bytes_per_period(ReleaseDecision::EQUAL_THRESHOLD)
1174            .min(64 * 1024)
1175            .max(floor as u64) as usize
1176    }
1177}
1178
1179impl std::fmt::Debug for GRecovery {
1180    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
1181        write!(f, "timer={:?} ", self.loss_detection_timer())?;
1182        write!(f, "rtt_stats={:?} ", self.rtt_stats)?;
1183        write!(f, "bytes_in_flight={} ", self.bytes_in_flight.get())?;
1184        write!(f, "{:?} ", self.pacer)?;
1185        Ok(())
1186    }
1187}
1188
1189#[cfg(test)]
1190mod tests {
1191    use super::*;
1192    use crate::Config;
1193
1194    #[test]
1195    fn loss_threshold() {
1196        let config = Config::new(crate::PROTOCOL_VERSION).unwrap();
1197        let recovery_config = RecoveryConfig::from_config(&config);
1198        assert!(!recovery_config.enable_relaxed_loss_threshold);
1199
1200        let mut loss_thresh = LossThreshold::new(&recovery_config);
1201        assert_eq!(loss_thresh.time_thresh_overhead, None);
1202        assert_eq!(loss_thresh.pkt_thresh().unwrap(), INITIAL_PACKET_THRESHOLD);
1203        assert_eq!(loss_thresh.time_thresh(), INITIAL_TIME_THRESHOLD);
1204
1205        // First spurious loss.
1206        loss_thresh.on_spurious_loss(INITIAL_PACKET_THRESHOLD);
1207        assert_eq!(loss_thresh.pkt_thresh().unwrap(), INITIAL_PACKET_THRESHOLD);
1208        assert_eq!(loss_thresh.time_thresh(), PACKET_REORDER_TIME_THRESHOLD);
1209
1210        // Packet gaps < INITIAL_PACKET_THRESHOLD will NOT change packet
1211        // threshold.
1212        for packet_gap in 0..INITIAL_PACKET_THRESHOLD {
1213            loss_thresh.on_spurious_loss(packet_gap);
1214
1215            // Packet threshold only increases once the packet gap increases.
1216            assert_eq!(
1217                loss_thresh.pkt_thresh().unwrap(),
1218                INITIAL_PACKET_THRESHOLD
1219            );
1220            assert_eq!(loss_thresh.time_thresh(), PACKET_REORDER_TIME_THRESHOLD);
1221        }
1222
1223        // Subsequent spurious loss with packet_gaps > INITIAL_PACKET_THRESHOLD.
1224        // Test values much larger than MAX_PACKET_THRESHOLD, i.e.
1225        // `MAX_PACKET_THRESHOLD * 2`
1226        for packet_gap in INITIAL_PACKET_THRESHOLD + 1..MAX_PACKET_THRESHOLD * 2 {
1227            loss_thresh.on_spurious_loss(packet_gap);
1228
1229            // Packet threshold is equal to packet gap beyond
1230            // INITIAL_PACKET_THRESHOLD, but capped
1231            // at MAX_PACKET_THRESHOLD.
1232            let new_packet_threshold = if packet_gap < MAX_PACKET_THRESHOLD {
1233                packet_gap
1234            } else {
1235                MAX_PACKET_THRESHOLD
1236            };
1237            assert_eq!(loss_thresh.pkt_thresh().unwrap(), new_packet_threshold);
1238            assert_eq!(loss_thresh.time_thresh(), PACKET_REORDER_TIME_THRESHOLD);
1239        }
1240        // Packet threshold is capped at MAX_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        // Packet threshold is monotonically increasing
1245        loss_thresh.on_spurious_loss(INITIAL_PACKET_THRESHOLD);
1246        assert_eq!(loss_thresh.pkt_thresh().unwrap(), MAX_PACKET_THRESHOLD);
1247        assert_eq!(loss_thresh.time_thresh(), PACKET_REORDER_TIME_THRESHOLD);
1248    }
1249
1250    #[test]
1251    fn relaxed_loss_threshold() {
1252        // The max time threshold when operating in relaxed loss mode.
1253        const MAX_TIME_THRESHOLD: f64 = 2.0;
1254
1255        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
1256        config.set_enable_relaxed_loss_threshold(true);
1257        let recovery_config = RecoveryConfig::from_config(&config);
1258        assert!(recovery_config.enable_relaxed_loss_threshold);
1259
1260        let mut loss_thresh = LossThreshold::new(&recovery_config);
1261        assert_eq!(
1262            loss_thresh.time_thresh_overhead,
1263            Some(INITIAL_TIME_THRESHOLD_OVERHEAD)
1264        );
1265        assert_eq!(loss_thresh.pkt_thresh().unwrap(), INITIAL_PACKET_THRESHOLD);
1266        assert_eq!(loss_thresh.time_thresh(), INITIAL_TIME_THRESHOLD);
1267
1268        // First spurious loss.
1269        loss_thresh.on_spurious_loss(INITIAL_PACKET_THRESHOLD);
1270        assert_eq!(loss_thresh.pkt_thresh(), None);
1271        assert_eq!(loss_thresh.time_thresh(), INITIAL_TIME_THRESHOLD);
1272
1273        // Subsequent spurious loss.
1274        for subsequent_loss_count in 1..100 {
1275            // Double the overhead until it caps at `2.0`.
1276            //
1277            // It takes `3` rounds of doubling for INITIAL_TIME_THRESHOLD_OVERHEAD
1278            // to equal `1.0`.
1279            let new_time_threshold = if subsequent_loss_count <= 3 {
1280                1.0 + INITIAL_TIME_THRESHOLD_OVERHEAD *
1281                    2_f64.powi(subsequent_loss_count as i32)
1282            } else {
1283                2.0
1284            };
1285
1286            loss_thresh.on_spurious_loss(subsequent_loss_count);
1287            assert_eq!(loss_thresh.pkt_thresh(), None);
1288            assert_eq!(loss_thresh.time_thresh(), new_time_threshold);
1289        }
1290        // Time threshold is capped at 2.0.
1291        assert_eq!(loss_thresh.pkt_thresh(), None);
1292        assert_eq!(loss_thresh.time_thresh(), MAX_TIME_THRESHOLD);
1293    }
1294}