quinn_proto/congestion.rs
1//! Logic for controlling the rate at which data is sent
2
3use crate::Instant;
4use crate::connection::RttEstimator;
5use std::any::Any;
6use std::sync::Arc;
7
8mod bbr;
9mod cubic;
10mod new_reno;
11
12pub use bbr::{Bbr, BbrConfig};
13pub use cubic::{Cubic, CubicConfig};
14pub use new_reno::{NewReno, NewRenoConfig};
15
16/// Common interface for different congestion controllers
17pub trait Controller: Send + Sync {
18 /// One or more packets were just sent
19 #[allow(unused_variables)]
20 fn on_sent(&mut self, now: Instant, bytes: u64, last_packet_number: u64) {}
21
22 /// Packet deliveries were confirmed
23 ///
24 /// `app_limited` indicates whether the connection was blocked on outgoing
25 /// application data prior to receiving these acknowledgements.
26 #[allow(unused_variables)]
27 fn on_ack(
28 &mut self,
29 now: Instant,
30 sent: Instant,
31 bytes: u64,
32 app_limited: bool,
33 rtt: &RttEstimator,
34 ) {
35 }
36
37 /// Packets are acked in batches, all with the same `now` argument. This indicates one of those batches has completed.
38 #[allow(unused_variables)]
39 fn on_end_acks(
40 &mut self,
41 now: Instant,
42 in_flight: u64,
43 app_limited: bool,
44 largest_packet_num_acked: Option<u64>,
45 ) {
46 }
47
48 /// Packets were deemed lost or marked congested
49 ///
50 /// `in_persistent_congestion` indicates whether all packets sent within the persistent
51 /// congestion threshold period ending when the most recent packet in this batch was sent were
52 /// lost.
53 /// `lost_bytes` indicates how many bytes were lost. This value will be 0 for ECN triggers.
54 fn on_congestion_event(
55 &mut self,
56 now: Instant,
57 sent: Instant,
58 is_persistent_congestion: bool,
59 lost_bytes: u64,
60 );
61
62 /// The known MTU for the current network path has been updated
63 fn on_mtu_update(&mut self, new_mtu: u16);
64
65 /// Number of ack-eliciting bytes that may be in flight
66 fn window(&self) -> u64;
67
68 /// Retrieve implementation-specific metrics used to populate `qlog` traces when they are enabled
69 fn metrics(&self) -> ControllerMetrics {
70 ControllerMetrics {
71 congestion_window: self.window(),
72 ssthresh: None,
73 pacing_rate: None,
74 }
75 }
76
77 /// Duplicate the controller's state
78 fn clone_box(&self) -> Box<dyn Controller>;
79
80 /// Initial congestion window
81 fn initial_window(&self) -> u64;
82
83 /// Returns Self for use in down-casting to extract implementation details
84 fn into_any(self: Box<Self>) -> Box<dyn Any>;
85}
86
87/// Common congestion controller metrics
88#[derive(Default)]
89#[non_exhaustive]
90pub struct ControllerMetrics {
91 /// Congestion window (bytes)
92 pub congestion_window: u64,
93 /// Slow start threshold (bytes)
94 pub ssthresh: Option<u64>,
95 /// Pacing rate (bits/s)
96 pub pacing_rate: Option<u64>,
97}
98
99/// Constructs controllers on demand
100pub trait ControllerFactory {
101 /// Construct a fresh `Controller`
102 fn build(self: Arc<Self>, now: Instant, current_mtu: u16) -> Box<dyn Controller>;
103}
104
105const BASE_DATAGRAM_SIZE: u64 = 1200;