ant_quic/connection/
stats.rs

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