quinn_proto/connection/
stats.rs

1//! Connection statistics
2
3use crate::{Dir, Duration, frame::Frame};
4
5/// Statistics about UDP datagrams transmitted or received on a connection
6#[derive(Default, Debug, Copy, Clone)]
7#[non_exhaustive]
8pub struct UdpStats {
9    /// The amount of UDP datagrams observed
10    pub datagrams: u64,
11    /// The total amount of bytes which have been transferred inside UDP datagrams
12    pub bytes: u64,
13    /// The amount of I/O operations executed
14    ///
15    /// Can be less than `datagrams` when GSO, GRO, and/or batched system calls are in use.
16    pub ios: u64,
17}
18
19impl UdpStats {
20    pub(crate) fn on_sent(&mut self, datagrams: u64, bytes: usize) {
21        self.datagrams += datagrams;
22        self.bytes += bytes as u64;
23        self.ios += 1;
24    }
25}
26
27/// Number of frames transmitted of each frame type
28#[derive(Default, Copy, Clone)]
29#[non_exhaustive]
30#[allow(missing_docs)]
31pub struct FrameStats {
32    pub acks: u64,
33    pub ack_frequency: u64,
34    pub crypto: u64,
35    pub connection_close: u64,
36    pub data_blocked: u64,
37    pub datagram: u64,
38    pub handshake_done: u8,
39    pub immediate_ack: u64,
40    pub max_data: u64,
41    pub max_stream_data: u64,
42    pub max_streams_bidi: u64,
43    pub max_streams_uni: u64,
44    pub new_connection_id: u64,
45    pub new_token: u64,
46    pub path_challenge: u64,
47    pub path_response: u64,
48    pub ping: u64,
49    pub reset_stream: u64,
50    pub retire_connection_id: u64,
51    pub stream_data_blocked: u64,
52    pub streams_blocked_bidi: u64,
53    pub streams_blocked_uni: u64,
54    pub stop_sending: u64,
55    pub stream: u64,
56}
57
58impl FrameStats {
59    pub(crate) fn record(&mut self, frame: &Frame) {
60        match frame {
61            Frame::Padding => {}
62            Frame::Ping => self.ping += 1,
63            Frame::Ack(_) => self.acks += 1,
64            Frame::ResetStream(_) => self.reset_stream += 1,
65            Frame::StopSending(_) => self.stop_sending += 1,
66            Frame::Crypto(_) => self.crypto += 1,
67            Frame::Datagram(_) => self.datagram += 1,
68            Frame::NewToken(_) => self.new_token += 1,
69            Frame::MaxData(_) => self.max_data += 1,
70            Frame::MaxStreamData { .. } => self.max_stream_data += 1,
71            Frame::MaxStreams { dir, .. } => {
72                if *dir == Dir::Bi {
73                    self.max_streams_bidi += 1;
74                } else {
75                    self.max_streams_uni += 1;
76                }
77            }
78            Frame::DataBlocked { .. } => self.data_blocked += 1,
79            Frame::Stream(_) => self.stream += 1,
80            Frame::StreamDataBlocked { .. } => self.stream_data_blocked += 1,
81            Frame::StreamsBlocked { dir, .. } => {
82                if *dir == Dir::Bi {
83                    self.streams_blocked_bidi += 1;
84                } else {
85                    self.streams_blocked_uni += 1;
86                }
87            }
88            Frame::NewConnectionId(_) => self.new_connection_id += 1,
89            Frame::RetireConnectionId { .. } => self.retire_connection_id += 1,
90            Frame::PathChallenge(_) => self.path_challenge += 1,
91            Frame::PathResponse(_) => self.path_response += 1,
92            Frame::Close(_) => self.connection_close += 1,
93            Frame::AckFrequency(_) => self.ack_frequency += 1,
94            Frame::ImmediateAck => self.immediate_ack += 1,
95            Frame::HandshakeDone => self.handshake_done = self.handshake_done.saturating_add(1),
96        }
97    }
98}
99
100impl std::fmt::Debug for FrameStats {
101    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102        f.debug_struct("FrameStats")
103            .field("ACK", &self.acks)
104            .field("ACK_FREQUENCY", &self.ack_frequency)
105            .field("CONNECTION_CLOSE", &self.connection_close)
106            .field("CRYPTO", &self.crypto)
107            .field("DATA_BLOCKED", &self.data_blocked)
108            .field("DATAGRAM", &self.datagram)
109            .field("HANDSHAKE_DONE", &self.handshake_done)
110            .field("IMMEDIATE_ACK", &self.immediate_ack)
111            .field("MAX_DATA", &self.max_data)
112            .field("MAX_STREAM_DATA", &self.max_stream_data)
113            .field("MAX_STREAMS_BIDI", &self.max_streams_bidi)
114            .field("MAX_STREAMS_UNI", &self.max_streams_uni)
115            .field("NEW_CONNECTION_ID", &self.new_connection_id)
116            .field("NEW_TOKEN", &self.new_token)
117            .field("PATH_CHALLENGE", &self.path_challenge)
118            .field("PATH_RESPONSE", &self.path_response)
119            .field("PING", &self.ping)
120            .field("RESET_STREAM", &self.reset_stream)
121            .field("RETIRE_CONNECTION_ID", &self.retire_connection_id)
122            .field("STREAM_DATA_BLOCKED", &self.stream_data_blocked)
123            .field("STREAMS_BLOCKED_BIDI", &self.streams_blocked_bidi)
124            .field("STREAMS_BLOCKED_UNI", &self.streams_blocked_uni)
125            .field("STOP_SENDING", &self.stop_sending)
126            .field("STREAM", &self.stream)
127            .finish()
128    }
129}
130
131/// Statistics related to a transmission path
132#[derive(Debug, Default, Copy, Clone)]
133#[non_exhaustive]
134pub struct PathStats {
135    /// Current best estimate of this connection's latency (round-trip-time)
136    pub rtt: Duration,
137    /// Current congestion window of the connection
138    pub cwnd: u64,
139    /// Congestion events on the connection
140    pub congestion_events: u64,
141    /// The amount of packets lost on this path
142    pub lost_packets: u64,
143    /// The amount of bytes lost on this path
144    pub lost_bytes: u64,
145    /// The amount of packets sent on this path
146    pub sent_packets: u64,
147    /// The amount of PLPMTUD probe packets sent on this path (also counted by `sent_packets`)
148    pub sent_plpmtud_probes: u64,
149    /// The amount of PLPMTUD probe packets lost on this path (ignored by `lost_packets` and
150    /// `lost_bytes`)
151    pub lost_plpmtud_probes: u64,
152    /// The number of times a black hole was detected in the path
153    pub black_holes_detected: u64,
154    /// Largest UDP payload size the path currently supports
155    pub current_mtu: u16,
156}
157
158/// Connection statistics
159#[derive(Debug, Default, Copy, Clone)]
160#[non_exhaustive]
161pub struct ConnectionStats {
162    /// Statistics about UDP datagrams transmitted on a connection
163    pub udp_tx: UdpStats,
164    /// Statistics about UDP datagrams received on a connection
165    pub udp_rx: UdpStats,
166    /// Statistics about frames transmitted on a connection
167    pub frame_tx: FrameStats,
168    /// Statistics about frames received on a connection
169    pub frame_rx: FrameStats,
170    /// Statistics related to the current transmission path
171    pub path: PathStats,
172}