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 /// Duplicate the controller's state
69 fn clone_box(&self) -> Box<dyn Controller>;
70
71 /// Initial congestion window
72 fn initial_window(&self) -> u64;
73
74 /// Returns Self for use in down-casting to extract implementation details
75 fn into_any(self: Box<Self>) -> Box<dyn Any>;
76}
77
78/// Constructs controllers on demand
79pub trait ControllerFactory {
80 /// Construct a fresh `Controller`
81 fn build(self: Arc<Self>, now: Instant, current_mtu: u16) -> Box<dyn Controller>;
82}
83
84const BASE_DATAGRAM_SIZE: u64 = 1200;