Debian Codegraph @

third_party/rust/neqo-transport/src/stats.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 // Tracking of some useful statistics.
8
9 use std::{
10 cell::RefCell,
11 fmt::{self, Debug},
12 ops::{Deref, DerefMut},
13 rc::Rc,
14 time::Duration,
15 };
16
17 use enum_map::EnumMap;
18 use neqo_common::{qwarn, IpTosDscp, IpTosEcn};
19 use strum::IntoEnumIterator as _;
20
21 use crate::{
22 ecn,
23 packet::{PacketNumber, PacketType},
24 };
25
26 pub const MAX_PTO_COUNTS: usize = 16;
27
28 #[derive(Default, Clone, PartialEq, Eq)]
29 pub struct FrameStats {
30 pub ack: usize,
31 pub largest_acknowledged: PacketNumber,
32
33 pub crypto: usize,
34 pub stream: usize,
35 pub reset_stream: usize,
36 pub stop_sending: usize,
37
38 pub ping: usize,
39 pub padding: usize,
40
41 pub max_streams: usize,
42 pub streams_blocked: usize,
43 pub max_data: usize,
44 pub data_blocked: usize,
45 pub max_stream_data: usize,
46 pub stream_data_blocked: usize,
47
48 pub new_connection_id: usize,
49 pub retire_connection_id: usize,
50
51 pub path_challenge: usize,
52 pub path_response: usize,
53
54 pub connection_close: usize,
55 pub handshake_done: usize,
56 pub new_token: usize,
57
58 pub ack_frequency: usize,
59 pub datagram: usize,
60 }
61
62 impl Debug for FrameStats {
63 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
64 writeln!(
65 f,
66 " crypto {} done {} token {} close {}",
67 self.crypto, self.handshake_done, self.new_token, self.connection_close,
68 )?;
69 writeln!(
70 f,
71 " ack {} (max {}) ping {} padding {}",
72 self.ack, self.largest_acknowledged, self.ping, self.padding
73 )?;
74 writeln!(
75 f,
76 " stream {} reset {} stop {}",
77 self.stream, self.reset_stream, self.stop_sending,
78 )?;
79 writeln!(
80 f,
81 " max: stream {} data {} stream_data {}",
82 self.max_streams, self.max_data, self.max_stream_data,
83 )?;
84 writeln!(
85 f,
86 " blocked: stream {} data {} stream_data {}",
87 self.streams_blocked, self.data_blocked, self.stream_data_blocked,
88 )?;
89 writeln!(f, " datagram {}", self.datagram)?;
90 writeln!(
91 f,
92 " ncid {} rcid {} pchallenge {} presponse {}",
93 self.new_connection_id,
94 self.retire_connection_id,
95 self.path_challenge,
96 self.path_response,
97 )?;
98 writeln!(f, " ack_frequency {}", self.ack_frequency)
99 }
100 }
101
102 #[cfg(test)]
103 impl FrameStats {
104 pub const fn all(&self) -> usize {
105 self.ack
106 + self.crypto
107 + self.stream
108 + self.reset_stream
109 + self.stop_sending
110 + self.ping
111 + self.padding
112 + self.max_streams
113 + self.streams_blocked
114 + self.max_data
115 + self.data_blocked
116 + self.max_stream_data
117 + self.stream_data_blocked
118 + self.new_connection_id
119 + self.retire_connection_id
120 + self.path_challenge
121 + self.path_response
122 + self.connection_close
123 + self.handshake_done
124 + self.new_token
125 + self.ack_frequency
126 + self.datagram
127 }
128 }
129
130 /// Datagram stats
131 #[derive(Default, Clone, PartialEq, Eq)]
132 pub struct DatagramStats {
133 /// The number of datagrams declared lost.
134 pub lost: usize,
135 /// The number of datagrams dropped due to being too large.
136 pub dropped_too_big: usize,
137 /// The number of datagrams dropped due to reaching the limit of the
138 /// outgoing queue.
139 pub dropped_queue_full: usize,
140 }
141
142 /// ECN counts by QUIC [`PacketType`].
143 #[derive(Default, Clone, PartialEq, Eq)]
144 pub struct EcnCount(EnumMap<PacketType, ecn::Count>);
145
146 impl Debug for EcnCount {
147 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
148 for (pt, count) in self.0 {
149 // Don't show all-zero rows.
150 if count.is_empty() {
151 continue;
152 }
153 writeln!(f, " {pt:?} {count:?}")?;
154 }
155 Ok(())
156 }
157 }
158
159 impl Deref for EcnCount {
160 type Target = EnumMap<PacketType, ecn::Count>;
161 fn deref(&self) -> &Self::Target {
162 &self.0
163 }
164 }
165
166 impl DerefMut for EcnCount {
167 fn deref_mut(&mut self) -> &mut Self::Target {
168 &mut self.0
169 }
170 }
171
172 /// Packet types and numbers of the first ECN mark transition between two marks.
173 #[derive(Default, Clone, PartialEq, Eq)]
174 pub struct EcnTransitions(EnumMap<IpTosEcn, EnumMap<IpTosEcn, Option<(PacketType, PacketNumber)>>>);
175
176 impl Deref for EcnTransitions {
177 type Target = EnumMap<IpTosEcn, EnumMap<IpTosEcn, Option<(PacketType, PacketNumber)>>>;
178 fn deref(&self) -> &Self::Target {
179 &self.0
180 }
181 }
182
183 impl DerefMut for EcnTransitions {
184 fn deref_mut(&mut self) -> &mut Self::Target {
185 &mut self.0
186 }
187 }
188
189 impl Debug for EcnTransitions {
190 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191 for from in IpTosEcn::iter() {
192 // Don't show all-None rows.
193 if self.0[from].iter().all(|(_, v)| v.is_none()) {
194 continue;
195 }
196 write!(f, " First {from:?} ")?;
197 for to in IpTosEcn::iter() {
198 // Don't show transitions that were not recorded.
199 if let Some(pkt) = self.0[from][to] {
200 write!(f, "to {to:?} {pkt:?} ")?;
201 }
202 }
203 writeln!(f)?;
204 }
205 Ok(())
206 }
207 }
208
209 /// Received packet counts by DSCP value.
210 #[derive(Default, Clone, PartialEq, Eq)]
211 pub struct DscpCount(EnumMap<IpTosDscp, usize>);
212
213 impl Debug for DscpCount {
214 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
215 for (dscp, count) in self.0 {
216 // Don't show zero counts.
217 if count == 0 {
218 continue;
219 }
220 write!(f, "{dscp:?}: {count} ")?;
221 }
222 Ok(())
223 }
224 }
225
226 impl Deref for DscpCount {
227 type Target = EnumMap<IpTosDscp, usize>;
228 fn deref(&self) -> &Self::Target {
229 &self.0
230 }
231 }
232
233 impl DerefMut for DscpCount {
234 fn deref_mut(&mut self) -> &mut Self::Target {
235 &mut self.0
236 }
237 }
238
239 /// Connection statistics
240 #[derive(Default, Clone, PartialEq, Eq)]
241 pub struct Stats {
242 pub info: String,
243
244 /// Total packets received, including all the bad ones.
245 pub packets_rx: usize,
246 /// Duplicate packets received.
247 pub dups_rx: usize,
248 /// Dropped packets or dropped garbage.
249 pub dropped_rx: usize,
250 /// The number of packet that were saved for later processing.
251 pub saved_datagrams: usize,
252
253 /// Total packets sent.
254 pub packets_tx: usize,
255 /// Total number of packets that are declared lost.
256 pub lost: usize,
257 /// Late acknowledgments, for packets that were declared lost already.
258 pub late_ack: usize,
259 /// Acknowledgments for packets that contained data that was marked
260 /// for retransmission when the PTO timer popped.
261 pub pto_ack: usize,
262 /// Number of PMTUD probes sent.
263 pub pmtud_tx: usize,
264 /// Number of PMTUD probes ACK'ed.
265 pub pmtud_ack: usize,
266 /// Number of PMTUD probes lost.
267 pub pmtud_lost: usize,
268 /// Number of times a path MTU changed unexpectedly.
269 pub pmtud_change: usize,
270 /// MTU of the local interface used for the most recent path.
271 pub pmtud_iface_mtu: usize,
272 /// Probed PMTU of the current path.
273 pub pmtud_pmtu: usize,
274
275 /// Whether the connection was resumed successfully.
276 pub resumed: bool,
277
278 /// The current, estimated round-trip time on the primary path.
279 pub rtt: Duration,
280 /// The current, estimated round-trip time variation on the primary path.
281 pub rttvar: Duration,
282 /// Whether the first RTT sample was guessed from a discarded packet.
283 pub rtt_init_guess: bool,
284
285 /// Count PTOs. Single PTOs, 2 PTOs in a row, 3 PTOs in row, etc. are counted
286 /// separately.
287 pub pto_counts: [usize; MAX_PTO_COUNTS],
288
289 /// Count frames received.
290 pub frame_rx: FrameStats,
291 /// Count frames sent.
292 pub frame_tx: FrameStats,
293
294 /// The number of incoming datagrams dropped due to reaching the limit
295 /// of the incoming queue.
296 pub incoming_datagram_dropped: usize,
297
298 pub datagram_tx: DatagramStats,
299
300 /// ECN path validation count, indexed by validation outcome.
301 pub ecn_path_validation: ecn::ValidationCount,
302 /// ECN counts for outgoing UDP datagrams, recorded locally. For coalesced packets,
303 /// counts increase for all packet types in the coalesced datagram.
304 pub ecn_tx: EcnCount,
305 /// ECN counts for outgoing UDP datagrams, returned by remote through QUIC ACKs.
306 ///
307 /// Note: Given that QUIC ACKs only carry [`Ect0`], [`Ect1`] and [`Ce`], but
308 /// never [`NotEct`], the [`NotEct`] value will always be 0.
309 ///
310 /// See also <https://www.rfc-editor.org/rfc/rfc9000.html#section-19.3.2>.
311 ///
312 /// [`Ect0`]: neqo_common::tos::IpTosEcn::Ect0
313 /// [`Ect1`]: neqo_common::tos::IpTosEcn::Ect1
314 /// [`Ce`]: neqo_common::tos::IpTosEcn::Ce
315 /// [`NotEct`]: neqo_common::tos::IpTosEcn::NotEct
316 pub ecn_tx_acked: EcnCount,
317 /// ECN counts for incoming UDP datagrams, read from IP TOS header. For coalesced packets,
318 /// counts increase for all packet types in the coalesced datagram.
319 pub ecn_rx: EcnCount,
320 /// Packet numbers of the first observed (received) ECN mark transition between two marks.
321 pub ecn_last_mark: Option<IpTosEcn>,
322 pub ecn_rx_transition: EcnTransitions,
323
324 /// Counters for DSCP values received.
325 pub dscp_rx: DscpCount,
326 }
327
328 impl Stats {
329 pub fn init(&mut self, info: String) {
330 self.info = info;
331 }
332
333 pub fn pkt_dropped(&mut self, reason: impl AsRef<str>) {
334 self.dropped_rx += 1;
335 qwarn!(
336 "[{}] Dropped received packet: {}; Total: {}",
337 self.info,
338 reason.as_ref(),
339 self.dropped_rx
340 );
341 }
342
343 /// # Panics
344 ///
345 /// When preconditions are violated.
346 pub fn add_pto_count(&mut self, count: usize) {
347 debug_assert!(count > 0);
348 if count >= MAX_PTO_COUNTS {
349 // We can't move this count any further, so stop.
350 return;
351 }
352 self.pto_counts[count - 1] += 1;
353 if count > 1 {
354 debug_assert!(self.pto_counts[count - 2] > 0);
355 self.pto_counts[count - 2] -= 1;
356 }
357 }
358 }
359
360 impl Debug for Stats {
361 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
362 writeln!(f, "stats for {}", self.info)?;
363 writeln!(
364 f,
365 " rx: {} drop {} dup {} saved {}",
366 self.packets_rx, self.dropped_rx, self.dups_rx, self.saved_datagrams
367 )?;
368 writeln!(
369 f,
370 " tx: {} lost {} lateack {} ptoack {}",
371 self.packets_tx, self.lost, self.late_ack, self.pto_ack
372 )?;
373 writeln!(
374 f,
375 " pmtud: {} sent {} acked {} lost {} change {} iface_mtu {} pmtu",
376 self.pmtud_tx,
377 self.pmtud_ack,
378 self.pmtud_lost,
379 self.pmtud_change,
380 self.pmtud_iface_mtu,
381 self.pmtud_pmtu
382 )?;
383 writeln!(f, " resumed: {}", self.resumed)?;
384 writeln!(f, " frames rx:")?;
385 self.frame_rx.fmt(f)?;
386 writeln!(f, " frames tx:")?;
387 self.frame_tx.fmt(f)?;
388 writeln!(f, " ecn:\n tx:")?;
389 self.ecn_tx.fmt(f)?;
390 writeln!(f, " acked:")?;
391 self.ecn_tx_acked.fmt(f)?;
392 writeln!(f, " rx:")?;
393 self.ecn_rx.fmt(f)?;
394 writeln!(
395 f,
396 " path validation outcomes: {:?}",
397 self.ecn_path_validation
398 )?;
399 writeln!(f, " mark transitions:")?;
400 self.ecn_rx_transition.fmt(f)?;
401 writeln!(f, " dscp: {:?}", self.dscp_rx)
402 }
403 }
404
405 #[derive(Default, Clone)]
406 pub struct StatsCell {
407 stats: Rc<RefCell<Stats>>,
408 }
409
410 impl Deref for StatsCell {
411 type Target = RefCell<Stats>;
412 fn deref(&self) -> &Self::Target {
413 &self.stats
414 }
415 }
416
417 impl Debug for StatsCell {
418 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
419 self.stats.borrow().fmt(f)
420 }
421 }