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