1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#![warn(rust_2018_idioms)]
#![allow(dead_code)]
#[allow(unused_macros)]
macro_rules! ready {
($e:expr $(,)?) => {
match $e {
std::task::Poll::Ready(t) => t,
std::task::Poll::Pending => return std::task::Poll::Pending,
}
};
}
#[cfg(unix)]
use std::os::unix::io::AsRawFd;
#[cfg(windows)]
use std::os::windows::io::AsRawSocket;
use std::sync::atomic::AtomicU64;
use std::{
net::{IpAddr, Ipv6Addr, SocketAddr},
sync::atomic::{AtomicUsize, Ordering},
time::{Duration, Instant},
};
use tracing::warn;
#[cfg(unix)]
mod cmsg;
#[cfg(unix)]
#[path = "unix.rs"]
mod imp;
#[cfg(windows)]
#[path = "windows.rs"]
mod imp;
#[cfg(not(any(unix, windows)))]
#[path = "fallback.rs"]
mod imp;
mod proto;
mod runtime;
pub use imp::UdpSocketState;
pub use proto::{EcnCodepoint, Transmit};
#[cfg(not(feature = "metal-io"))]
pub use runtime::AsyncUdpSocket;
pub use runtime::UdpSocket;
pub const BATCH_SIZE: usize = imp::BATCH_SIZE;
#[derive(Debug)]
pub struct Capabilities {
max_gso_segments: AtomicUsize,
gro_segments: usize,
}
impl Capabilities {
pub fn new() -> Self {
imp::capabilities()
}
#[inline]
pub fn max_gso_segments(&self) -> usize {
self.max_gso_segments.load(Ordering::Relaxed)
}
#[inline]
pub fn gro_segments(&self) -> usize {
self.gro_segments
}
}
impl Default for Capabilities {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Copy, Clone)]
pub struct RecvMeta {
pub addr: SocketAddr,
pub len: usize,
pub stride: usize,
pub ecn: Option<EcnCodepoint>,
pub dst_ip: Option<IpAddr>,
}
impl Default for RecvMeta {
fn default() -> Self {
Self {
addr: SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 0),
len: 0,
stride: 0,
ecn: None,
dst_ip: None,
}
}
}
const IO_ERROR_LOG_INTERVAL: Duration = std::time::Duration::from_secs(60);
fn log_sendmsg_error(
epoch: &Instant,
last_send_error: &AtomicU64,
err: impl core::fmt::Debug,
transmit: &Transmit,
) {
let d = last_send_error.load(Ordering::Relaxed);
let last = epoch.checked_add(Duration::from_nanos(d)).unwrap();
let now = Instant::now();
let interval = now.saturating_duration_since(last);
if interval > IO_ERROR_LOG_INTERVAL {
last_send_error.store(interval.as_nanos() as u64, Ordering::Relaxed);
warn!(
"sendmsg error: {:?}, Transmit: {{ destination: {:?}, src_ip: {:?}, enc: {:?}, len: {:?}, segment_size: {:?} }}",
err, transmit.destination, transmit.src_ip, transmit.ecn, transmit.contents.len(), transmit.segment_size);
}
}
pub struct UdpSockRef<'a>(socket2::SockRef<'a>);
#[cfg(unix)]
impl<'s, S> From<&'s S> for UdpSockRef<'s>
where
S: AsRawFd,
{
fn from(socket: &'s S) -> Self {
Self(socket.into())
}
}
#[cfg(windows)]
impl<'s, S> From<&'s S> for UdpSockRef<'s>
where
S: AsRawSocket,
{
fn from(socket: &'s S) -> Self {
Self(socket.into())
}
}