Debian Codegraph @

third_party/rust/neqo-transport/src/sender.rs

raw
1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
6
7 // Congestion control
8
9 use std::{
10 fmt::{self, Display},
11 time::{Duration, Instant},
12 };
13
14 use neqo_common::{qdebug, qlog::NeqoQlog};
15
16 use crate::{
17 cc::{ClassicCongestionControl, CongestionControl, CongestionControlAlgorithm, Cubic, NewReno},
18 pace::Pacer,
19 pmtud::Pmtud,
20 recovery::SentPacket,
21 rtt::RttEstimate,
22 ConnectionParameters, Stats,
23 };
24
25 /// The number of packets we allow to burst from the pacer.
26 pub const PACING_BURST_SIZE: usize = 2;
27
28 #[derive(Debug)]
29 pub struct PacketSender {
30 cc: Box<dyn CongestionControl>,
31 pacer: Pacer,
32 }
33
34 impl Display for PacketSender {
35 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36 write!(f, "{} {}", self.cc, self.pacer)
37 }
38 }
39
40 impl PacketSender {
41 #[must_use]
42 pub fn new(conn_params: &ConnectionParameters, pmtud: Pmtud, now: Instant) -> Self {
43 let mtu = pmtud.plpmtu();
44 Self {
45 cc: match conn_params.get_cc_algorithm() {
46 CongestionControlAlgorithm::NewReno => {
47 Box::new(ClassicCongestionControl::new(NewReno::default(), pmtud))
48 }
49 CongestionControlAlgorithm::Cubic => {
50 Box::new(ClassicCongestionControl::new(Cubic::default(), pmtud))
51 }
52 },
53 pacer: Pacer::new(
54 conn_params.pacing_enabled(),
55 now,
56 mtu * PACING_BURST_SIZE,
57 mtu,
58 ),
59 }
60 }
61
62 pub fn set_qlog(&mut self, qlog: NeqoQlog) {
63 self.cc.set_qlog(qlog);
64 }
65
66 pub fn pmtud(&self) -> &Pmtud {
67 self.cc.pmtud()
68 }
69
70 pub fn pmtud_mut(&mut self) -> &mut Pmtud {
71 self.cc.pmtud_mut()
72 }
73
74 #[must_use]
75 pub fn cwnd(&self) -> usize {
76 self.cc.cwnd()
77 }
78
79 #[must_use]
80 pub fn cwnd_avail(&self) -> usize {
81 self.cc.cwnd_avail()
82 }
83
84 #[cfg(test)]
85 #[must_use]
86 pub fn cwnd_min(&self) -> usize {
87 self.cc.cwnd_min()
88 }
89
90 fn maybe_update_pacer_mtu(&mut self) {
91 let current_mtu = self.pmtud().plpmtu();
92 if current_mtu != self.pacer.mtu() {
93 qdebug!(
94 "PLPMTU changed from {} to {current_mtu}, updating pacer",
95 self.pacer.mtu()
96 );
97 self.pacer.set_mtu(current_mtu);
98 }
99 }
100
101 pub fn on_packets_acked(
102 &mut self,
103 acked_pkts: &[SentPacket],
104 rtt_est: &RttEstimate,
105 now: Instant,
106 stats: &mut Stats,
107 ) {
108 self.cc.on_packets_acked(acked_pkts, rtt_est, now);
109 self.pmtud_mut().on_packets_acked(acked_pkts, now, stats);
110 self.maybe_update_pacer_mtu();
111 }
112
113 /// Called when packets are lost. Returns true if the congestion window was reduced.
114 pub fn on_packets_lost(
115 &mut self,
116 first_rtt_sample_time: Option<Instant>,
117 prev_largest_acked_sent: Option<Instant>,
118 pto: Duration,
119 lost_packets: &[SentPacket],
120 stats: &mut Stats,
121 now: Instant,
122 ) -> bool {
123 let ret = self.cc.on_packets_lost(
124 first_rtt_sample_time,
125 prev_largest_acked_sent,
126 pto,
127 lost_packets,
128 now,
129 );
130 // Call below may change the size of MTU probes, so it needs to happen after the CC
131 // reaction above, which needs to ignore probes based on their size.
132 self.pmtud_mut().on_packets_lost(lost_packets, stats, now);
133 self.maybe_update_pacer_mtu();
134 ret
135 }
136
137 /// Called when ECN CE mark received. Returns true if the congestion window was reduced.
138 pub fn on_ecn_ce_received(&mut self, largest_acked_pkt: &SentPacket, now: Instant) -> bool {
139 self.cc.on_ecn_ce_received(largest_acked_pkt, now)
140 }
141
142 pub fn discard(&mut self, pkt: &SentPacket, now: Instant) {
143 self.cc.discard(pkt, now);
144 }
145
146 /// When we migrate, the congestion controller for the previously active path drops
147 /// all bytes in flight.
148 pub fn discard_in_flight(&mut self, now: Instant) {
149 self.cc.discard_in_flight(now);
150 }
151
152 pub fn on_packet_sent(&mut self, pkt: &SentPacket, rtt: Duration, now: Instant) {
153 self.pacer
154 .spend(pkt.time_sent(), rtt, self.cc.cwnd(), pkt.len());
155 self.cc.on_packet_sent(pkt, now);
156 }
157
158 #[must_use]
159 pub fn next_paced(&self, rtt: Duration) -> Option<Instant> {
160 // Only pace if there are bytes in flight.
161 (self.cc.bytes_in_flight() > 0).then(|| self.pacer.next(rtt, self.cc.cwnd()))
162 }
163
164 #[must_use]
165 pub fn recovery_packet(&self) -> bool {
166 self.cc.recovery_packet()
167 }
168 }