ant_quic/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    pub add_address: u64,
57    pub punch_me_now: u64,
58    pub remove_address: u64,
59    pub observed_address: u64,
60}
61
62impl FrameStats {
63    pub(crate) fn record(&mut self, frame: &Frame) {
64        match frame {
65            Frame::Padding => {}
66            Frame::Ping => self.ping += 1,
67            Frame::Ack(_) => self.acks += 1,
68            Frame::ResetStream(_) => self.reset_stream += 1,
69            Frame::StopSending(_) => self.stop_sending += 1,
70            Frame::Crypto(_) => self.crypto += 1,
71            Frame::Datagram(_) => self.datagram += 1,
72            Frame::NewToken(_) => self.new_token += 1,
73            Frame::MaxData(_) => self.max_data += 1,
74            Frame::MaxStreamData { .. } => self.max_stream_data += 1,
75            Frame::MaxStreams { dir, .. } => {
76                if *dir == Dir::Bi {
77                    self.max_streams_bidi += 1;
78                } else {
79                    self.max_streams_uni += 1;
80                }
81            }
82            Frame::DataBlocked { .. } => self.data_blocked += 1,
83            Frame::Stream(_) => self.stream += 1,
84            Frame::StreamDataBlocked { .. } => self.stream_data_blocked += 1,
85            Frame::StreamsBlocked { dir, .. } => {
86                if *dir == Dir::Bi {
87                    self.streams_blocked_bidi += 1;
88                } else {
89                    self.streams_blocked_uni += 1;
90                }
91            }
92            Frame::NewConnectionId(_) => self.new_connection_id += 1,
93            Frame::RetireConnectionId { .. } => self.retire_connection_id += 1,
94            Frame::PathChallenge(_) => self.path_challenge += 1,
95            Frame::PathResponse(_) => self.path_response += 1,
96            Frame::Close(_) => self.connection_close += 1,
97            Frame::AckFrequency(_) => self.ack_frequency += 1,
98            Frame::ImmediateAck => self.immediate_ack += 1,
99            Frame::HandshakeDone => self.handshake_done = self.handshake_done.saturating_add(1),
100            Frame::AddAddress(_) => self.add_address += 1,
101            Frame::PunchMeNow(_) => self.punch_me_now += 1,
102            Frame::RemoveAddress(_) => self.remove_address += 1,
103            Frame::ObservedAddress(_) => self.observed_address += 1,
104        }
105    }
106}
107
108impl std::fmt::Debug for FrameStats {
109    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
110        f.debug_struct("FrameStats")
111            .field("ACK", &self.acks)
112            .field("ACK_FREQUENCY", &self.ack_frequency)
113            .field("CONNECTION_CLOSE", &self.connection_close)
114            .field("CRYPTO", &self.crypto)
115            .field("DATA_BLOCKED", &self.data_blocked)
116            .field("DATAGRAM", &self.datagram)
117            .field("HANDSHAKE_DONE", &self.handshake_done)
118            .field("IMMEDIATE_ACK", &self.immediate_ack)
119            .field("MAX_DATA", &self.max_data)
120            .field("MAX_STREAM_DATA", &self.max_stream_data)
121            .field("MAX_STREAMS_BIDI", &self.max_streams_bidi)
122            .field("MAX_STREAMS_UNI", &self.max_streams_uni)
123            .field("NEW_CONNECTION_ID", &self.new_connection_id)
124            .field("NEW_TOKEN", &self.new_token)
125            .field("PATH_CHALLENGE", &self.path_challenge)
126            .field("PATH_RESPONSE", &self.path_response)
127            .field("PING", &self.ping)
128            .field("RESET_STREAM", &self.reset_stream)
129            .field("RETIRE_CONNECTION_ID", &self.retire_connection_id)
130            .field("STREAM_DATA_BLOCKED", &self.stream_data_blocked)
131            .field("STREAMS_BLOCKED_BIDI", &self.streams_blocked_bidi)
132            .field("STREAMS_BLOCKED_UNI", &self.streams_blocked_uni)
133            .field("STOP_SENDING", &self.stop_sending)
134            .field("STREAM", &self.stream)
135            .field("ADD_ADDRESS", &self.add_address)
136            .field("PUNCH_ME_NOW", &self.punch_me_now)
137            .field("REMOVE_ADDRESS", &self.remove_address)
138            .field("OBSERVED_ADDRESS", &self.observed_address)
139            .finish()
140    }
141}
142
143/// Statistics related to a transmission path
144#[derive(Debug, Default, Copy, Clone)]
145#[non_exhaustive]
146pub struct PathStats {
147    /// Current best estimate of this connection's latency (round-trip-time)
148    pub rtt: Duration,
149    /// Current congestion window of the connection
150    pub cwnd: u64,
151    /// Congestion events on the connection
152    pub congestion_events: u64,
153    /// The amount of packets lost on this path
154    pub lost_packets: u64,
155    /// The amount of bytes lost on this path
156    pub lost_bytes: u64,
157    /// The amount of packets sent on this path
158    pub sent_packets: u64,
159    /// The amount of PLPMTUD probe packets sent on this path (also counted by `sent_packets`)
160    pub sent_plpmtud_probes: u64,
161    /// The amount of PLPMTUD probe packets lost on this path (ignored by `lost_packets` and
162    /// `lost_bytes`)
163    pub lost_plpmtud_probes: u64,
164    /// The number of times a black hole was detected in the path
165    pub black_holes_detected: u64,
166    /// Largest UDP payload size the path currently supports
167    pub current_mtu: u16,
168}
169
170/// Connection statistics
171#[derive(Debug, Default, Copy, Clone)]
172#[non_exhaustive]
173pub struct ConnectionStats {
174    /// Statistics about UDP datagrams transmitted on a connection
175    pub udp_tx: UdpStats,
176    /// Statistics about UDP datagrams received on a connection
177    pub udp_rx: UdpStats,
178    /// Statistics about frames transmitted on a connection
179    pub frame_tx: FrameStats,
180    /// Statistics about frames received on a connection
181    pub frame_rx: FrameStats,
182    /// Statistics related to the current transmission path
183    pub path: PathStats,
184}