datagram_socket/
socket_stats.rs1use std::ops::Deref;
28use std::sync::atomic::AtomicI64;
29use std::sync::atomic::Ordering;
30use std::sync::Arc;
31use std::sync::RwLock;
32use std::time::Duration;
33use std::time::SystemTime;
34
35pub trait AsSocketStats {
36 fn as_socket_stats(&self) -> SocketStats;
37
38 fn as_quic_stats(&self) -> Option<&Arc<QuicAuditStats>> {
39 None
40 }
41}
42
43#[derive(Debug, Clone, Copy, Default)]
44pub struct SocketStats {
45 pub pmtu: u16,
46 pub rtt_us: i64,
47 pub min_rtt_us: i64,
48 pub rtt_var_us: i64,
49 pub cwnd: u64,
50 pub packets_sent: u64,
51 pub packets_recvd: u64,
52 pub packets_lost: u64,
53 pub packets_retrans: u64,
54 pub bytes_sent: u64,
55 pub bytes_recvd: u64,
56 pub bytes_lost: u64,
57 pub bytes_retrans: u64,
58 pub bytes_unsent: u64,
59 pub delivery_rate: u64,
60}
61
62type BoxError = Box<dyn std::error::Error + Send + Sync>;
63
64#[derive(Debug)]
65pub struct QuicAuditStats {
66 recvd_conn_close_transport_error_code: AtomicI64,
68 sent_conn_close_transport_error_code: AtomicI64,
70 recvd_conn_close_application_error_code: AtomicI64,
72 sent_conn_close_application_error_code: AtomicI64,
74 transport_handshake_duration_us: AtomicI64,
76 transport_handshake_start: Arc<RwLock<Option<SystemTime>>>,
78 connection_close_reason: RwLock<Option<BoxError>>,
80 pub quic_connection_id: Vec<u8>,
83}
84
85impl QuicAuditStats {
86 #[inline]
87 pub fn new(quic_connection_id: Vec<u8>) -> Self {
88 Self {
89 recvd_conn_close_transport_error_code: AtomicI64::new(-1),
90 sent_conn_close_transport_error_code: AtomicI64::new(-1),
91 recvd_conn_close_application_error_code: AtomicI64::new(-1),
92 sent_conn_close_application_error_code: AtomicI64::new(-1),
93 transport_handshake_duration_us: AtomicI64::new(-1),
94 transport_handshake_start: Arc::new(RwLock::new(None)),
95 connection_close_reason: RwLock::new(None),
96 quic_connection_id,
97 }
98 }
99
100 #[inline]
101 pub fn recvd_conn_close_transport_error_code(&self) -> i64 {
102 self.recvd_conn_close_transport_error_code
103 .load(Ordering::SeqCst)
104 }
105
106 #[inline]
107 pub fn sent_conn_close_transport_error_code(&self) -> i64 {
108 self.sent_conn_close_transport_error_code
109 .load(Ordering::SeqCst)
110 }
111
112 #[inline]
113 pub fn recvd_conn_close_application_error_code(&self) -> i64 {
114 self.recvd_conn_close_application_error_code
115 .load(Ordering::SeqCst)
116 }
117
118 #[inline]
119 pub fn sent_conn_close_application_error_code(&self) -> i64 {
120 self.sent_conn_close_application_error_code
121 .load(Ordering::SeqCst)
122 }
123
124 #[inline]
125 pub fn set_recvd_conn_close_transport_error_code(
126 &self, recvd_conn_close_transport_error_code: i64,
127 ) {
128 self.recvd_conn_close_transport_error_code
129 .store(recvd_conn_close_transport_error_code, Ordering::SeqCst)
130 }
131
132 #[inline]
133 pub fn set_sent_conn_close_transport_error_code(
134 &self, sent_conn_close_transport_error_code: i64,
135 ) {
136 self.sent_conn_close_transport_error_code
137 .store(sent_conn_close_transport_error_code, Ordering::SeqCst)
138 }
139
140 #[inline]
141 pub fn set_recvd_conn_close_application_error_code(
142 &self, recvd_conn_close_application_error_code: i64,
143 ) {
144 self.recvd_conn_close_application_error_code
145 .store(recvd_conn_close_application_error_code, Ordering::SeqCst)
146 }
147
148 #[inline]
149 pub fn set_sent_conn_close_application_error_code(
150 &self, sent_conn_close_application_error_code: i64,
151 ) {
152 self.sent_conn_close_application_error_code
153 .store(sent_conn_close_application_error_code, Ordering::SeqCst)
154 }
155
156 #[inline]
157 pub fn transport_handshake_duration_us(&self) -> i64 {
158 self.transport_handshake_duration_us.load(Ordering::SeqCst)
159 }
160
161 #[inline]
162 pub fn set_transport_handshake_start(&self, start_time: SystemTime) {
163 *self.transport_handshake_start.write().unwrap() = Some(start_time);
164 }
165
166 #[inline]
167 pub fn set_transport_handshake_duration(&self, duration: Duration) {
168 let dur = i64::try_from(duration.as_micros()).unwrap_or(-1);
169 self.transport_handshake_duration_us
170 .store(dur, Ordering::SeqCst);
171 }
172
173 #[inline]
174 pub fn transport_handshake_start(&self) -> Arc<RwLock<Option<SystemTime>>> {
175 Arc::clone(&self.transport_handshake_start)
176 }
177
178 #[inline]
179 pub fn connection_close_reason(
180 &self,
181 ) -> impl Deref<Target = Option<BoxError>> + '_ {
182 self.connection_close_reason.read().unwrap()
183 }
184
185 #[inline]
186 pub fn set_connection_close_reason(&self, error: BoxError) {
187 *self.connection_close_reason.write().unwrap() = Some(error);
188 }
189}
190
191#[derive(Debug, Copy, Clone, PartialEq, Eq)]
192pub enum StreamClosureKind {
193 None,
194 Implicit,
195 Explicit,
196}